2021-11-24 23:20:46 +03:00
|
|
|
package inet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
2022-01-24 23:21:48 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
2021-11-24 23:20:46 +03:00
|
|
|
"github.com/wader/fq/pkg/checksum"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2021-11-24 23:20:46 +03:00
|
|
|
)
|
|
|
|
|
2022-04-01 17:31:55 +03:00
|
|
|
var ipv4IpPacketGroup decode.Group
|
2021-11-24 23:20:46 +03:00
|
|
|
|
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
interp.RegisterFormat(decode.Format{
|
2021-11-24 23:20:46 +03:00
|
|
|
Name: format.IPV4_PACKET,
|
|
|
|
Description: "Internet protocol v4 packet",
|
2022-04-01 17:31:55 +03:00
|
|
|
Groups: []string{format.INET_PACKET},
|
2021-11-24 23:20:46 +03:00
|
|
|
Dependencies: []decode.Dependency{
|
2022-04-01 17:31:55 +03:00
|
|
|
{Names: []string{format.IP_PACKET}, Group: &ipv4IpPacketGroup},
|
2021-11-24 23:20:46 +03:00
|
|
|
},
|
|
|
|
DecodeFn: decodeIPv4,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ipv4OptionEnd = 0
|
|
|
|
ipv4OptionNop = 1
|
|
|
|
)
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var ipv4OptionsMap = scalar.UToScalar{
|
2021-11-24 23:20:46 +03:00
|
|
|
ipv4OptionEnd: {Sym: "end", Description: "End of options list"},
|
|
|
|
ipv4OptionNop: {Sym: "nop", Description: "No operation"},
|
|
|
|
2: {Description: "Security"},
|
|
|
|
3: {Description: "Loose Source Routing"},
|
|
|
|
9: {Description: "Strict Source Routing"},
|
|
|
|
7: {Description: "Record Route"},
|
|
|
|
8: {Description: "Stream ID"},
|
|
|
|
4: {Description: "Internet Timestamp"},
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var mapUToIPv4Sym = scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
2021-11-24 23:20:46 +03:00
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], uint32(s.ActualU()))
|
|
|
|
s.Sym = net.IP(b[:]).String()
|
|
|
|
return s, nil
|
2021-12-02 00:48:25 +03:00
|
|
|
})
|
2021-11-24 23:20:46 +03:00
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func decodeIPv4(d *decode.D, in any) any {
|
2022-04-01 17:31:55 +03:00
|
|
|
if ipi, ok := in.(format.InetPacketIn); ok && ipi.EtherType != format.EtherTypeIPv4 {
|
|
|
|
d.Fatalf("incorrect ethertype %d", ipi.EtherType)
|
|
|
|
}
|
|
|
|
|
2021-11-24 23:20:46 +03:00
|
|
|
d.FieldU4("version")
|
|
|
|
ihl := d.FieldU4("ihl")
|
|
|
|
d.FieldU6("dscp")
|
|
|
|
d.FieldU2("ecn")
|
|
|
|
totalLength := d.FieldU16("total_length")
|
|
|
|
d.FieldU16("identification")
|
|
|
|
d.FieldU1("reserved")
|
|
|
|
d.FieldBool("dont_fragment")
|
|
|
|
moreFragments := d.FieldBool("more_fragments")
|
|
|
|
fragmentOffset := d.FieldU13("fragment_offset")
|
|
|
|
d.FieldU8("ttl")
|
2021-12-02 00:48:25 +03:00
|
|
|
protocol := d.FieldU8("protocol", format.IPv4ProtocolMap)
|
2021-11-24 23:20:46 +03:00
|
|
|
checksumStart := d.Pos()
|
2022-05-07 13:46:34 +03:00
|
|
|
d.FieldU16("header_checksum", scalar.ActualHex)
|
2021-11-24 23:20:46 +03:00
|
|
|
checksumEnd := d.Pos()
|
2022-05-07 13:46:34 +03:00
|
|
|
d.FieldU32("source_ip", mapUToIPv4Sym, scalar.ActualHex)
|
|
|
|
d.FieldU32("destination_ip", mapUToIPv4Sym, scalar.ActualHex)
|
2021-11-24 23:20:46 +03:00
|
|
|
optionsLen := (int64(ihl) - 5) * 8 * 4
|
|
|
|
if optionsLen > 0 {
|
2022-01-13 20:34:59 +03:00
|
|
|
d.FramedFn(optionsLen, func(d *decode.D) {
|
2021-11-24 23:20:46 +03:00
|
|
|
d.FieldArray("options", func(d *decode.D) {
|
|
|
|
for !d.End() {
|
|
|
|
d.FieldStruct("option", func(d *decode.D) {
|
|
|
|
d.FieldBool("copied")
|
|
|
|
d.FieldU2("class")
|
2021-12-02 00:48:25 +03:00
|
|
|
kind := d.FieldU5("number", ipv4OptionsMap)
|
2021-11-24 23:20:46 +03:00
|
|
|
switch kind {
|
|
|
|
case ipv4OptionEnd, ipv4OptionNop:
|
|
|
|
default:
|
|
|
|
l := d.FieldU8("length")
|
|
|
|
d.FieldRawLen("data", (int64(l-2))*8)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
headerEnd := d.Pos()
|
|
|
|
|
|
|
|
ipv4Checksum := &checksum.IPv4{}
|
2022-06-30 13:13:36 +03:00
|
|
|
d.Copy(ipv4Checksum, bitio.NewIOReader(d.BitBufRange(0, checksumStart)))
|
|
|
|
d.Copy(ipv4Checksum, bitio.NewIOReader(d.BitBufRange(checksumEnd, headerEnd-checksumEnd)))
|
2022-05-07 13:46:34 +03:00
|
|
|
_ = d.FieldMustGet("header_checksum").TryScalarFn(d.ValidateUBytes(ipv4Checksum.Sum(nil)), scalar.ActualHex)
|
2021-11-24 23:20:46 +03:00
|
|
|
|
|
|
|
dataLen := int64(totalLength-(ihl*4)) * 8
|
2022-04-01 17:31:55 +03:00
|
|
|
|
|
|
|
if moreFragments || fragmentOffset > 0 {
|
|
|
|
d.FieldRawLen("payload", dataLen)
|
2022-04-11 23:45:46 +03:00
|
|
|
} else {
|
|
|
|
d.FieldFormatOrRawLen(
|
|
|
|
"payload",
|
|
|
|
dataLen,
|
|
|
|
ipv4IpPacketGroup,
|
|
|
|
format.IPPacketIn{Protocol: int(protocol)},
|
|
|
|
)
|
2021-11-24 23:20:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|