2021-11-24 23:20:46 +03:00
|
|
|
package inet
|
|
|
|
|
|
|
|
// SLL stands for sockaddr_ll
|
|
|
|
// https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html
|
|
|
|
|
|
|
|
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"
|
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 sllPacket2InetPacketGroup 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.SLL2_PACKET,
|
|
|
|
Description: "Linux cooked capture encapsulation v2",
|
2022-01-03 22:17:01 +03:00
|
|
|
Groups: []string{format.LINK_FRAME},
|
2021-11-24 23:20:46 +03:00
|
|
|
Dependencies: []decode.Dependency{
|
2022-04-01 17:31:55 +03:00
|
|
|
{Names: []string{format.INET_PACKET}, Group: &sllPacket2InetPacketGroup},
|
2021-11-24 23:20:46 +03:00
|
|
|
},
|
|
|
|
DecodeFn: decodeSLL2,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func decodeSLL2(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.LinkTypeLINUX_SLL2 {
|
|
|
|
d.Fatalf("wrong link type %d", lfi.Type)
|
2022-01-03 22:17:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
protcolType := d.FieldU16("protocol_type", format.EtherTypeMap, scalar.ActualHex)
|
2021-11-24 23:20:46 +03:00
|
|
|
d.FieldU16("reserved")
|
|
|
|
d.FieldU32("interface_index")
|
2021-12-02 00:48:25 +03:00
|
|
|
arpHdrType := d.FieldU16("arphdr_type", arpHdrTypeMAp)
|
|
|
|
d.FieldU8("packet_type", sllPacketTypeMap)
|
2021-12-09 12:38:34 +03:00
|
|
|
addressLength := d.FieldU8("link_address_length", d.ValidateURange(0, 8))
|
|
|
|
// "If there are more than 8 bytes, only the first 8 bytes are present"
|
|
|
|
if addressLength > 8 {
|
|
|
|
addressLength = 8
|
|
|
|
}
|
|
|
|
// TODO: maybe skip padding and always read 8 bytes?
|
2021-11-24 23:20:46 +03:00
|
|
|
d.FieldU("link_address", int(addressLength)*8)
|
|
|
|
addressDiff := 8 - addressLength
|
|
|
|
if addressDiff > 0 {
|
|
|
|
d.FieldRawLen("padding", int64(addressDiff)*8)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle other arphdr types
|
|
|
|
switch arpHdrType {
|
|
|
|
case arpHdrTypeLoopback, arpHdrTypeEther:
|
2022-05-07 13:46:34 +03:00
|
|
|
_ = d.FieldMustGet("link_address").TryScalarFn(mapUToEtherSym, scalar.ActualHex)
|
2022-04-11 23:45:46 +03:00
|
|
|
d.FieldFormatOrRawLen(
|
2022-04-01 17:31:55 +03:00
|
|
|
"payload",
|
|
|
|
d.BitsLeft(),
|
|
|
|
sllPacket2InetPacketGroup,
|
2022-04-11 23:45:46 +03:00
|
|
|
format.LinkFrameIn{Type: int(protcolType)},
|
|
|
|
)
|
2021-11-24 23:20:46 +03:00
|
|
|
default:
|
2022-04-01 17:31:55 +03:00
|
|
|
d.FieldRawLen("payload", d.BitsLeft())
|
2021-11-24 23:20:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|