2021-11-24 23:20:46 +03:00
|
|
|
package inet
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Transmission_Control_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"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2021-11-24 23:20:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-03-29 01:36:55 +03:00
|
|
|
interp.RegisterFormat(
|
2023-05-01 14:19:04 +03:00
|
|
|
format.TCP_Segment,
|
2023-03-29 01:36:55 +03:00
|
|
|
&decode.Format{
|
|
|
|
Description: "Transmission control protocol segment",
|
2023-05-01 14:19:04 +03:00
|
|
|
Groups: []*decode.Group{format.IP_Packet},
|
2023-03-29 01:36:55 +03:00
|
|
|
DecodeFn: decodeTCP,
|
|
|
|
})
|
2021-11-24 23:20:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2023-01-31 14:23:37 +03:00
|
|
|
tcpOptionEnd = 0
|
|
|
|
tcpOptionNop = 1
|
|
|
|
tcpOptionMSS = 2
|
|
|
|
tcpOptionWinscale = 3
|
|
|
|
tcpOptionSackPermitted = 4
|
|
|
|
tcpOptionSack = 5
|
|
|
|
tcpOptionTimestamp = 8
|
2021-11-24 23:20:46 +03:00
|
|
|
)
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
var tcpOptionsMap = scalar.UintMap{
|
2023-01-31 14:23:37 +03:00
|
|
|
tcpOptionEnd: {Sym: "end", Description: "End of options list"},
|
|
|
|
tcpOptionNop: {Sym: "nop", Description: "No operation"},
|
|
|
|
tcpOptionMSS: {Sym: "mss", Description: "Maximum segment size"},
|
|
|
|
tcpOptionWinscale: {Sym: "winscale", Description: "Window scale"},
|
|
|
|
tcpOptionSackPermitted: {Sym: "sack_permitted", Description: "Selective Acknowledgement permitted"},
|
|
|
|
tcpOptionSack: {Sym: "sack", Description: "Selective Acknowledgement"},
|
|
|
|
tcpOptionTimestamp: {Sym: "timestamp", Description: "Timestamp and echo of previous timestamp"},
|
2021-11-24 23:20:46 +03:00
|
|
|
}
|
|
|
|
|
2023-02-18 23:10:48 +03:00
|
|
|
func decodeTCP(d *decode.D) any {
|
2023-05-01 14:19:04 +03:00
|
|
|
var ipi format.IP_Packet_In
|
2023-02-18 23:10:48 +03:00
|
|
|
if d.ArgAs(&ipi) && ipi.Protocol != format.IPv4ProtocolTCP {
|
2022-04-01 17:31:55 +03:00
|
|
|
d.Fatalf("incorrect protocol %d", ipi.Protocol)
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
d.FieldU16("source_port", format.TCPPortMap)
|
|
|
|
d.FieldU16("destination_port", format.TCPPortMap)
|
2021-11-24 23:20:46 +03:00
|
|
|
d.FieldU32("sequence_number")
|
|
|
|
d.FieldU32("acknowledgment_number")
|
|
|
|
dataOffset := d.FieldU4("data_offset")
|
|
|
|
d.FieldU3("reserved")
|
|
|
|
d.FieldBool("ns")
|
|
|
|
d.FieldBool("cwr")
|
|
|
|
d.FieldBool("ece")
|
|
|
|
d.FieldBool("urg")
|
|
|
|
d.FieldBool("ack")
|
|
|
|
d.FieldBool("psh")
|
|
|
|
d.FieldBool("rst")
|
|
|
|
d.FieldBool("syn")
|
|
|
|
d.FieldBool("fin")
|
|
|
|
d.FieldU16("window_size")
|
|
|
|
// checksumStart := d.Pos()
|
2022-09-30 14:58:23 +03:00
|
|
|
d.FieldU16("checksum", scalar.UintHex)
|
2021-11-24 23:20:46 +03:00
|
|
|
// checksumEnd := d.Pos()
|
|
|
|
d.FieldU16("urgent_pointer")
|
|
|
|
optionsLen := (int64(dataOffset) - 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) {
|
2021-12-02 00:48:25 +03:00
|
|
|
kind := d.FieldU8("kind", tcpOptionsMap)
|
2021-11-24 23:20:46 +03:00
|
|
|
switch kind {
|
|
|
|
case tcpOptionEnd, tcpOptionNop:
|
2023-01-31 14:23:37 +03:00
|
|
|
// has no length or data
|
2021-11-24 23:20:46 +03:00
|
|
|
default:
|
|
|
|
l := d.FieldU8("length")
|
2023-01-31 14:23:37 +03:00
|
|
|
switch kind {
|
|
|
|
case tcpOptionMSS:
|
|
|
|
d.FieldU16("size")
|
|
|
|
case tcpOptionWinscale:
|
|
|
|
d.FieldU8("shift")
|
|
|
|
case tcpOptionSackPermitted:
|
|
|
|
// none
|
|
|
|
case tcpOptionSack:
|
|
|
|
d.FramedFn((int64(l-2))*8, func(d *decode.D) {
|
|
|
|
d.FieldArray("blocks", func(d *decode.D) {
|
|
|
|
for !d.End() {
|
|
|
|
d.FieldStruct("block", func(d *decode.D) {
|
|
|
|
d.FieldU32("left_edge")
|
|
|
|
d.FieldU32("right_edge")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case tcpOptionTimestamp:
|
|
|
|
d.FieldU32("value")
|
|
|
|
d.FieldU32("echo_reply")
|
|
|
|
default:
|
|
|
|
d.FieldRawLen("data", (int64(l-2))*8)
|
|
|
|
}
|
2021-11-24 23:20:46 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: need to pass ipv4 pseudo header somehow
|
|
|
|
// tcpChecksum := &checksum.IPv4{}
|
|
|
|
// d.MustCopy(tcpChecksum, d.BitBufRange(0, checksumStart))
|
|
|
|
// d.MustCopy(tcpChecksum, d.BitBufRange(checksumEnd, d.Len()-checksumEnd))
|
2022-09-30 14:58:23 +03:00
|
|
|
// _ = d.FieldMustGet("checksum").TryScalarFn(d.UintValidateBytes(tcpChecksum.Sum(nil)), scalar.Hex)
|
2021-11-24 23:20:46 +03:00
|
|
|
|
2022-04-01 17:31:55 +03:00
|
|
|
d.FieldRawLen("payload", d.BitsLeft())
|
2021-11-24 23:20:46 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|