-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathskbprint.go
184 lines (163 loc) · 4.9 KB
/
skbprint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/jschwinger233/skbdump/bpf"
"github.com/jschwinger233/skbdump/utils"
)
var (
idx int
strfuncs map[string]func([]byte) string
)
func init() {
strfuncs = map[string]func([]byte) string{
"Ethernet": stringifyEthernet,
"ARP": stringifyARP,
"IPv4": stringifyIPv4,
"IPv6": stringifyIPv6,
"UDP": stringifyUDP,
"TCP": stringifyTCP,
"IPSecESP": stringifyESP,
"Payload": stringifyPayload,
}
}
func skbPrint(skb *bpf.Skbdump, linktype layers.LinkType) {
idx++
fmt.Printf("%d %016x ", idx, skb.Meta.Skb)
ifname, ok := config.ifindex2name[skb.Meta.Ifindex]
if !ok {
ifname = "unknown"
}
ksym, off := utils.Addr2ksym(skb.Meta.At)
if off != 0 {
ksym = fmt.Sprintf("%s+%d", ksym, off)
}
fmt.Printf("%s@%d(%s) ", ksym, skb.Meta.Ifindex, ifname)
if skb.Meta.Ret == 1 {
fmt.Printf("rv=%x ", skb.Meta.Rax)
}
for _, outputField := range config.OutputFields {
fmt.Printf("%s=%s ", outputField, skb.Field(outputField))
}
payload := []byte{}
if skb.Meta.L2 == 0 {
for i := 0; i < 12; i++ {
payload = append(payload, 0)
}
ethertype := make([]byte, 2)
proto, err := strconv.ParseUint(skb.Field("protocol"), 10, 16)
if err != nil {
proto = 0
}
binary.BigEndian.PutUint16(ethertype, uint16(proto))
payload = append(payload, ethertype[1], ethertype[0])
}
payloadLen := 1500
if int(skb.Meta.Len) < payloadLen {
payloadLen = int(skb.Meta.Len)
}
payload = append(payload, skb.Payload[:payloadLen]...)
copy(skb.Payload[:], payload)
skb.Meta.Len = uint32(len(payload))
packet := gopacket.NewPacket(payload, layers.LayerTypeEthernet, gopacket.NoCopy)
layerNum := len(packet.Layers())
for idx, layer := range packet.Layers() {
layerType := layer.LayerType().String()
strfunc, ok := strfuncs[layerType]
if ok {
fmt.Printf("%s(%s)", layerType, strfunc(layer.LayerContents()))
} else {
fmt.Printf("%s", layerType)
}
if idx+1 < layerNum {
fmt.Printf(" | ")
} else {
fmt.Printf("\n")
}
}
}
func stringifyEthernet(data []byte) string {
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x>%02x:%02x:%02x:%02x:%02x:%02x",
data[6], data[7], data[8], data[9], data[10], data[11],
data[0], data[1], data[2], data[3], data[4], data[5],
)
}
func stringifyIPv4(data []byte) string {
src := net.IPv4(data[12], data[13], data[14], data[15])
dest := net.IPv4(data[16], data[17], data[18], data[19])
return fmt.Sprintf("%s>%s", src.String(), dest.String())
}
func stringifyARP(data []byte) string {
if !bytes.Equal(data[:2], []byte{0x0, 0x1}) {
// hardware type != ethernet
return ""
}
if !bytes.Equal(data[2:4], []byte{0x8, 0x0}) {
// proto type != ipv4
return ""
}
var senderMac, targetMac [6]byte
copy(senderMac[:], data[8:14])
senderIP := net.IPv4(data[14], data[15], data[16], data[17]).String()
copy(targetMac[:], data[18:24])
targetIP := net.IPv4(data[24], data[25], data[26], data[27]).String()
switch data[7] {
case 1:
return fmt.Sprintf("who-has %s tell %s", targetIP, senderIP)
case 2:
return fmt.Sprintf("%s is-at %02x:%02x:%02x:%02x:%02x:%02x", senderIP, senderMac[0], senderMac[1], senderMac[2], senderMac[3], senderMac[4], senderMac[5])
default:
return ""
}
return ""
}
func stringifyIPv6(data []byte) string {
src := net.IP{data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17], data[18], data[19], data[20], data[21], data[22], data[23]}
dest := net.IP{data[24], data[25], data[26], data[27], data[28], data[29], data[30], data[31], data[32], data[33], data[34], data[35], data[36], data[37], data[38], data[39]}
return fmt.Sprintf("%s>%s", src.To16().String(), dest.To16().String())
}
func stringifyUDP(data []byte) string {
sport := binary.BigEndian.Uint16(data[:2])
dport := binary.BigEndian.Uint16(data[2:4])
return fmt.Sprintf("%d>%d", sport, dport)
}
func stringifyTCP(data []byte) string {
sport := binary.BigEndian.Uint16(data[:2])
dport := binary.BigEndian.Uint16(data[2:4])
flags := []string{}
if data[13]&0b00100000 != 0 {
flags = append(flags, "U")
}
if data[13]&0b00010000 != 0 {
flags = append(flags, ".")
}
if data[13]&0b00001000 != 0 {
flags = append(flags, "P")
}
if data[13]&0b00000100 != 0 {
flags = append(flags, "R")
}
if data[13]&0b00000010 != 0 {
flags = append(flags, "S")
}
if data[13]&0b00000001 != 0 {
flags = append(flags, "F")
}
seq := binary.BigEndian.Uint32(data[4:8])
ack := binary.BigEndian.Uint32(data[8:12])
return fmt.Sprintf("%d>%d,flags=%s,seq=%d,ack=%d", sport, dport, strings.Join(flags, ""), seq%10000, ack%10000)
}
func stringifyESP(data []byte) string {
spi := binary.BigEndian.Uint32(data[:4])
seq := binary.BigEndian.Uint32(data[4:8])
return fmt.Sprintf("spi=%d,seq=%d", spi, seq)
}
func stringifyPayload(data []byte) string {
return fmt.Sprintf("%d bytes", len(data))
}