2022-01-03 22:17:01 +03:00
|
|
|
package inet
|
|
|
|
|
|
|
|
// TODO: rename NetworkLayer? wireshark calls it "Family", pcap-linktype(7) calls it "network-layer protocol"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2022-01-03 22:17:01 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
|
|
|
)
|
|
|
|
|
2022-04-01 17:31:55 +03:00
|
|
|
var bsdLoopbackFrameInetPacketGroup decode.Group
|
2022-01-03 22:17:01 +03:00
|
|
|
|
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
interp.RegisterFormat(decode.Format{
|
2022-01-03 22:17:01 +03:00
|
|
|
Name: format.BSD_LOOPBACK_FRAME,
|
|
|
|
Description: "BSD loopback frame",
|
|
|
|
Groups: []string{format.LINK_FRAME},
|
|
|
|
Dependencies: []decode.Dependency{
|
2022-04-01 17:31:55 +03:00
|
|
|
{Names: []string{format.INET_PACKET}, Group: &bsdLoopbackFrameInetPacketGroup},
|
2022-01-03 22:17:01 +03:00
|
|
|
},
|
|
|
|
DecodeFn: decodeLoopbackFrame,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-04-01 17:31:55 +03:00
|
|
|
bsdLoopbackNetworkLayerIPv4 = 0x2
|
|
|
|
bsdLoopbackNetworkLayerIPv6 = 0x1e
|
2022-01-03 22:17:01 +03:00
|
|
|
)
|
|
|
|
|
2022-04-01 17:31:55 +03:00
|
|
|
var bsdLoopbackFrameNetworkLayerEtherType = map[uint64]int{
|
|
|
|
bsdLoopbackNetworkLayerIPv4: format.EtherTypeIPv4,
|
|
|
|
bsdLoopbackNetworkLayerIPv6: format.EtherTypeIPv6,
|
2022-01-03 22:17:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var bsdLookbackNetworkLayerMap = scalar.UToScalar{
|
|
|
|
bsdLoopbackNetworkLayerIPv4: {Sym: "ipv4", Description: `Internet protocol v4`},
|
2022-04-01 17:31:55 +03:00
|
|
|
bsdLoopbackNetworkLayerIPv6: {Sym: "ipv6", Description: `Internet protocol v6`},
|
2022-01-03 22:17:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func decodeLoopbackFrame(d *decode.D, in any) any {
|
2022-04-01 17:31:55 +03:00
|
|
|
if lfi, ok := in.(format.LinkFrameIn); ok {
|
|
|
|
if lfi.Type != format.LinkTypeNULL {
|
|
|
|
d.Fatalf("wrong link type %d", lfi.Type)
|
2022-01-03 22:17:01 +03:00
|
|
|
}
|
2022-04-01 17:31:55 +03:00
|
|
|
// TODO: where is this documented?
|
|
|
|
if lfi.IsLittleEndian {
|
2022-01-03 22:17:01 +03:00
|
|
|
d.Endian = decode.LittleEndian
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if no LinkFrameIn assume big endian for now
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
networkLayer := d.FieldU32("network_layer", bsdLookbackNetworkLayerMap, scalar.ActualHex)
|
2022-04-01 17:31:55 +03:00
|
|
|
|
2022-04-11 23:45:46 +03:00
|
|
|
d.FieldFormatOrRawLen(
|
2022-04-01 17:31:55 +03:00
|
|
|
"payload",
|
|
|
|
d.BitsLeft(),
|
|
|
|
bsdLoopbackFrameInetPacketGroup,
|
|
|
|
// TODO: unknown mapped to ether type 0 is ok?
|
2022-04-11 23:45:46 +03:00
|
|
|
format.InetPacketIn{EtherType: bsdLoopbackFrameNetworkLayerEtherType[networkLayer]},
|
|
|
|
)
|
2022-01-03 22:17:01 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|