forked from cilium/cilium
/
dissect.go
165 lines (145 loc) · 3.87 KB
/
dissect.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
// Copyright 2016-2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bpfdebug
import (
"encoding/hex"
"fmt"
"net"
"sync"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
var (
eth layers.Ethernet
ip4 layers.IPv4
ip6 layers.IPv6
icmp4 layers.ICMPv4
icmp6 layers.ICMPv6
tcp layers.TCP
udp layers.UDP
parser = gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
ð, &ip4, &ip6, &icmp4, &icmp6, &tcp, &udp)
decoded = []gopacket.LayerType{}
lock sync.Mutex
)
func getTCPInfo() string {
info := ""
addTCPFlag := func(flag, new string) string {
if flag == "" {
return new
}
return flag + ", " + new
}
if tcp.SYN {
info = addTCPFlag(info, "SYN")
}
if tcp.ACK {
info = addTCPFlag(info, "ACK")
}
if tcp.RST {
info = addTCPFlag(info, "RST")
}
if tcp.FIN {
info = addTCPFlag(info, "FIN")
}
return info
}
// GetConnectionSummary decodes the data into layers and returns a connection
// summary in the format:
//
// - sIP:sPort -> dIP:dPort, e.g. 1.1.1.1:2000 -> 2.2.2.2:80
// - sIP -> dIP icmpCode, 1.1.1.1 -> 2.2.2.2 echo-request
func GetConnectionSummary(data []byte) string {
lock.Lock()
defer lock.Unlock()
parser.DecodeLayers(data, &decoded)
var (
srcIP, dstIP net.IP
srcPort, dstPort uint16
icmpCode, proto string
hasIP, hasEth bool
)
for _, typ := range decoded {
switch typ {
case layers.LayerTypeEthernet:
hasEth = true
case layers.LayerTypeIPv4:
hasIP = true
srcIP, dstIP = ip4.SrcIP, ip4.DstIP
case layers.LayerTypeIPv6:
hasIP = true
srcIP, dstIP = ip6.SrcIP, ip6.DstIP
case layers.LayerTypeTCP:
proto = "tcp"
srcPort, dstPort = uint16(tcp.SrcPort), uint16(tcp.DstPort)
case layers.LayerTypeUDP:
proto = "udp"
srcPort, dstPort = uint16(udp.SrcPort), uint16(udp.DstPort)
case layers.LayerTypeICMPv4:
icmpCode = icmp4.TypeCode.String()
case layers.LayerTypeICMPv6:
icmpCode = icmp6.TypeCode.String()
}
}
switch {
case icmpCode != "":
return fmt.Sprintf("%s -> %s %s", srcIP, dstIP, icmpCode)
case proto != "":
s := fmt.Sprintf("%s:%d -> %s:%d %s", srcIP, srcPort, dstIP, dstPort, proto)
if proto == "tcp" {
s += " " + getTCPInfo()
}
return s
case hasIP:
return fmt.Sprintf("%s -> %s", srcIP, dstIP)
case hasEth:
return fmt.Sprintf("%s -> %s %s", eth.SrcMAC, eth.DstMAC, eth.EthernetType.String())
}
return "[unknown]"
}
// Dissect parses and prints the provided data if dissect is set to true,
// otherwise the data is printed as HEX output
func Dissect(dissect bool, data []byte) {
if dissect {
lock.Lock()
defer lock.Unlock()
parser.DecodeLayers(data, &decoded)
for _, typ := range decoded {
switch typ {
case layers.LayerTypeEthernet:
fmt.Println(gopacket.LayerString(ð))
case layers.LayerTypeIPv4:
fmt.Println(gopacket.LayerString(&ip4))
case layers.LayerTypeIPv6:
fmt.Println(gopacket.LayerString(&ip6))
case layers.LayerTypeTCP:
fmt.Println(gopacket.LayerString(&tcp))
case layers.LayerTypeUDP:
fmt.Println(gopacket.LayerString(&udp))
case layers.LayerTypeICMPv4:
fmt.Println(gopacket.LayerString(&icmp4))
case layers.LayerTypeICMPv6:
fmt.Println(gopacket.LayerString(&icmp6))
default:
fmt.Println("Unknown layer")
}
}
if parser.Truncated {
fmt.Println(" Packet has been truncated")
}
} else {
fmt.Print(hex.Dump(data))
}
}