-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathflow_parse.go
More file actions
256 lines (240 loc) · 7.72 KB
/
Copy pathflow_parse.go
File metadata and controls
256 lines (240 loc) · 7.72 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package tun
import (
"encoding/binary"
"net/netip"
"github.com/sagernet/sing-tun/gtcpip/header"
)
type flowKey struct {
protocol uint8
source netip.AddrPort
destination netip.AddrPort
}
func (k flowKey) reversed() flowKey {
return flowKey{protocol: k.protocol, source: k.destination, destination: k.source}
}
type forwardPacket struct {
ipVersion uint8
protocol uint8
network header.Network
transport []byte
source netip.AddrPort
destination netip.AddrPort
tcpFlags header.TCPFlags
icmpType uint8
fragment bool
hasFlow bool
}
func (p *forwardPacket) flowKey() flowKey {
return flowKey{protocol: p.protocol, source: p.source, destination: p.destination}
}
func (p *forwardPacket) isTCPSyn() bool {
return p.protocol == uint8(header.TCPProtocolNumber) && p.tcpFlags&header.TCPFlagSyn != 0
}
func parseForwardPacket(packet []byte) (forwardPacket, bool) {
switch header.IPVersion(packet) {
case header.IPv4Version:
ipHdr := header.IPv4(packet)
if !ipHdr.IsValid(len(packet)) {
return forwardPacket{}, false
}
parsed := forwardPacket{
ipVersion: 4,
protocol: uint8(ipHdr.TransportProtocol()),
network: ipHdr,
source: netip.AddrPortFrom(ipHdr.SourceAddr(), 0),
destination: netip.AddrPortFrom(ipHdr.DestinationAddr(), 0),
}
if ipHdr.More() || ipHdr.FragmentOffset() != 0 {
parsed.fragment = true
return parsed, true
}
parsed.parseTransport(ipHdr.Payload())
return parsed, true
case header.IPv6Version:
ipHdr := header.IPv6(packet)
if !ipHdr.IsValid(len(packet)) {
return forwardPacket{}, false
}
protocol, payload, fragment, transportPresent := skipIPv6ExtensionHeaders(uint8(ipHdr.TransportProtocol()), ipHdr.Payload())
parsed := forwardPacket{
ipVersion: 6,
protocol: protocol,
network: ipHdr,
source: netip.AddrPortFrom(ipHdr.SourceAddr(), 0),
destination: netip.AddrPortFrom(ipHdr.DestinationAddr(), 0),
fragment: fragment,
}
if fragment || !transportPresent {
return parsed, true
}
parsed.parseTransport(payload)
return parsed, true
default:
return forwardPacket{}, false
}
}
func skipIPv6ExtensionHeaders(protocol uint8, payload []byte) (uint8, []byte, bool, bool) {
for {
switch header.IPv6ExtensionHeaderIdentifier(protocol) {
case header.IPv6HopByHopOptionsExtHdrIdentifier, header.IPv6RoutingExtHdrIdentifier, header.IPv6DestinationOptionsExtHdrIdentifier:
if len(payload) < 2 {
return protocol, payload, false, false
}
extensionLength := (int(payload[1]) + 1) * 8
if len(payload) < extensionLength {
return protocol, payload, false, false
}
protocol = payload[0]
payload = payload[extensionLength:]
case header.IPv6FragmentExtHdrIdentifier:
return protocol, payload, true, false
default:
return protocol, payload, false, true
}
}
}
func (p *forwardPacket) parseTransport(payload []byte) {
p.transport = payload
switch p.protocol {
case uint8(header.TCPProtocolNumber):
if len(payload) < header.TCPMinimumSize {
return
}
tcpHdr := header.TCP(payload)
p.source = netip.AddrPortFrom(p.source.Addr(), tcpHdr.SourcePort())
p.destination = netip.AddrPortFrom(p.destination.Addr(), tcpHdr.DestinationPort())
p.tcpFlags = tcpHdr.Flags()
p.hasFlow = true
case uint8(header.UDPProtocolNumber):
if len(payload) < header.UDPMinimumSize {
return
}
udpHdr := header.UDP(payload)
p.source = netip.AddrPortFrom(p.source.Addr(), udpHdr.SourcePort())
p.destination = netip.AddrPortFrom(p.destination.Addr(), udpHdr.DestinationPort())
p.hasFlow = true
case uint8(header.ICMPv4ProtocolNumber):
if len(payload) < header.ICMPv4MinimumSize {
return
}
icmpHdr := header.ICMPv4(payload)
p.icmpType = uint8(icmpHdr.Type())
switch icmpHdr.Type() {
case header.ICMPv4Echo, header.ICMPv4EchoReply:
identifier := icmpHdr.Ident()
p.source = netip.AddrPortFrom(p.source.Addr(), identifier)
p.destination = netip.AddrPortFrom(p.destination.Addr(), identifier)
p.hasFlow = true
}
case uint8(header.ICMPv6ProtocolNumber):
if len(payload) < header.ICMPv6MinimumSize {
return
}
icmpHdr := header.ICMPv6(payload)
p.icmpType = uint8(icmpHdr.Type())
switch icmpHdr.Type() {
case header.ICMPv6EchoRequest, header.ICMPv6EchoReply:
identifier := icmpHdr.Ident()
p.source = netip.AddrPortFrom(p.source.Addr(), identifier)
p.destination = netip.AddrPortFrom(p.destination.Addr(), identifier)
p.hasFlow = true
}
}
}
func (p *forwardPacket) isICMPError() bool {
switch p.protocol {
case uint8(header.ICMPv4ProtocolNumber):
switch header.ICMPv4Type(p.icmpType) {
case header.ICMPv4DstUnreachable, header.ICMPv4SrcQuench, header.ICMPv4Redirect, header.ICMPv4TimeExceeded, header.ICMPv4ParamProblem:
return true
}
return false
case uint8(header.ICMPv6ProtocolNumber):
return header.ICMPv6Type(p.icmpType).IsErrorType()
default:
return false
}
}
func (p *forwardPacket) icmpErrorInner() ([]byte, bool) {
var innerOffset int
switch p.protocol {
case uint8(header.ICMPv4ProtocolNumber):
innerOffset = header.ICMPv4MinimumSize
case uint8(header.ICMPv6ProtocolNumber):
innerOffset = header.ICMPv6ErrorHeaderSize
default:
return nil, false
}
if len(p.transport) <= innerOffset {
return nil, false
}
return p.transport[innerOffset:], true
}
type embeddedPacket struct {
network header.Network
payload []byte
protocol uint8
source netip.AddrPort
destination netip.AddrPort
}
func (p *embeddedPacket) flowKey() flowKey {
return flowKey{protocol: p.protocol, source: p.source, destination: p.destination}
}
func parseEmbedded(inner []byte) (embeddedPacket, bool) {
switch header.IPVersion(inner) {
case header.IPv4Version:
if len(inner) < header.IPv4MinimumSize {
return embeddedPacket{}, false
}
ipHdr := header.IPv4(inner)
headerLength := int(ipHdr.HeaderLength())
if headerLength < header.IPv4MinimumSize || headerLength > len(inner) {
return embeddedPacket{}, false
}
return parseEmbeddedTransport(ipHdr, inner[headerLength:], uint8(ipHdr.TransportProtocol()), ipHdr.SourceAddr(), ipHdr.DestinationAddr())
case header.IPv6Version:
if len(inner) < header.IPv6MinimumSize {
return embeddedPacket{}, false
}
ipHdr := header.IPv6(inner)
protocol, payload, _, transportPresent := skipIPv6ExtensionHeaders(uint8(ipHdr.TransportProtocol()), inner[header.IPv6MinimumSize:])
if !transportPresent {
return embeddedPacket{}, false
}
return parseEmbeddedTransport(ipHdr, payload, protocol, ipHdr.SourceAddr(), ipHdr.DestinationAddr())
default:
return embeddedPacket{}, false
}
}
func parseEmbeddedTransport(network header.Network, payload []byte, protocol uint8, source, destination netip.Addr) (embeddedPacket, bool) {
embedded := embeddedPacket{
network: network,
payload: payload,
protocol: protocol,
}
switch protocol {
case uint8(header.TCPProtocolNumber), uint8(header.UDPProtocolNumber):
if len(payload) < 4 {
return embeddedPacket{}, false
}
embedded.source = netip.AddrPortFrom(source, binary.BigEndian.Uint16(payload[0:]))
embedded.destination = netip.AddrPortFrom(destination, binary.BigEndian.Uint16(payload[2:]))
case uint8(header.ICMPv4ProtocolNumber):
if len(payload) < header.ICMPv4MinimumSize {
return embeddedPacket{}, false
}
identifier := header.ICMPv4(payload).Ident()
embedded.source = netip.AddrPortFrom(source, identifier)
embedded.destination = netip.AddrPortFrom(destination, identifier)
case uint8(header.ICMPv6ProtocolNumber):
if len(payload) < header.ICMPv6MinimumSize {
return embeddedPacket{}, false
}
identifier := header.ICMPv6(payload).Ident()
embedded.source = netip.AddrPortFrom(source, identifier)
embedded.destination = netip.AddrPortFrom(destination, identifier)
default:
return embeddedPacket{}, false
}
return embedded, true
}