Repository navigation
Expand file tree
/
Copy pathicmp_error.go
More file actions
203 lines (195 loc) · 6.7 KB
/
Copy pathicmp_error.go
File metadata and controls
203 lines (195 loc) · 6.7 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
package tun
import (
"encoding/binary"
"net/netip"
"github.com/sagernet/sing-tun/gtcpip/header"
)
type ICMPError uint8
const (
ICMPErrorNoRoute ICMPError = iota
ICMPErrorAddressUnreachable
ICMPErrorPortUnreachable
ICMPErrorSourcePolicy
ICMPErrorHopLimitExceeded
ICMPErrorPacketTooBig
)
func BuildICMPError(packet []byte, errorType ICMPError, source netip.Addr, mtu uint32, headroom int) ([]byte, bool) {
switch header.IPVersion(packet) {
case header.IPv4Version:
ipHdr := header.IPv4(packet)
if !ipHdr.IsValid(len(packet)) || ipHdr.FragmentOffset() != 0 {
return nil, false
}
sourceAddr := ipHdr.SourceAddr()
if sourceAddr.IsUnspecified() || sourceAddr.IsMulticast() {
return nil, false
}
destinationAddr := ipHdr.DestinationAddr()
if destinationAddr.IsMulticast() || destinationAddr == netip.AddrFrom4([4]byte{0xff, 0xff, 0xff, 0xff}) {
return nil, false
}
if ipHdr.TransportProtocol() == header.ICMPv4ProtocolNumber {
if len(ipHdr.Payload()) < header.ICMPv4MinimumSize {
return nil, false
}
switch header.ICMPv4(ipHdr.Payload()).Type() {
case header.ICMPv4DstUnreachable, header.ICMPv4SrcQuench, header.ICMPv4Redirect, header.ICMPv4TimeExceeded, header.ICMPv4ParamProblem:
return nil, false
}
}
if !source.Is4() {
source = destinationAddr
}
return buildICMPv4Error(ipHdr, errorType, source, mtu, headroom), true
case header.IPv6Version:
ipHdr := header.IPv6(packet)
if !ipHdr.IsValid(len(packet)) {
return nil, false
}
sourceAddr := ipHdr.SourceAddr()
if sourceAddr.IsUnspecified() || sourceAddr.IsMulticast() {
return nil, false
}
destinationAddr := ipHdr.DestinationAddr()
if destinationAddr.IsMulticast() && errorType != ICMPErrorPacketTooBig {
return nil, false
}
if ipHdr.TransportProtocol() == header.ICMPv6ProtocolNumber {
if len(ipHdr.Payload()) < header.ICMPv6MinimumSize {
return nil, false
}
icmpType := header.ICMPv6(ipHdr.Payload()).Type()
if icmpType < header.ICMPv6EchoRequest || icmpType == header.ICMPv6RedirectMsg {
return nil, false
}
}
if !source.Is6() || source.Is4In6() {
source = destinationAddr
}
return buildICMPv6Error(ipHdr, errorType, source, mtu, headroom), true
default:
return nil, false
}
}
func buildICMPv4Error(packet header.IPv4, errorType ICMPError, source netip.Addr, mtu uint32, headroom int) []byte {
icmpType := header.ICMPv4DstUnreachable
var icmpCode header.ICMPv4Code
switch errorType {
case ICMPErrorNoRoute, ICMPErrorAddressUnreachable:
icmpCode = header.ICMPv4HostUnreachable
case ICMPErrorPortUnreachable:
icmpCode = header.ICMPv4PortUnreachable
case ICMPErrorSourcePolicy:
icmpCode = header.ICMPv4AdminProhibited
case ICMPErrorHopLimitExceeded:
icmpType = header.ICMPv4TimeExceeded
icmpCode = header.ICMPv4TTLExceeded
case ICMPErrorPacketTooBig:
icmpCode = header.ICMPv4FragmentationNeeded
}
const maxPayloadLength = header.IPv4MinimumProcessableDatagramSize - header.IPv4MinimumSize - header.ICMPv4MinimumSize
payloadLength := min(int(packet.TotalLength()), len(packet), maxPayloadLength)
size := header.IPv4MinimumSize + header.ICMPv4MinimumSize + payloadLength
buffer := make([]byte, headroom+size)
response := header.IPv4(buffer[headroom:])
response.Encode(&header.IPv4Fields{
TotalLength: uint16(size),
TTL: synthesizedTTL,
Protocol: uint8(header.ICMPv4ProtocolNumber),
SrcAddr: source,
DstAddr: packet.SourceAddr(),
})
response.SetChecksum(^response.CalculateChecksum())
icmpHdr := header.ICMPv4(response.Payload())
icmpHdr.SetType(icmpType)
icmpHdr.SetCode(icmpCode)
if errorType == ICMPErrorPacketTooBig {
icmpHdr.SetMTU(uint16(min(max(mtu, header.IPv4MinimumMTU), 0xffff)))
}
copy(icmpHdr.Payload(), packet[:payloadLength])
icmpHdr.SetChecksum(header.ICMPv4Checksum(icmpHdr, 0))
return buffer
}
func buildICMPv6Error(packet header.IPv6, errorType ICMPError, source netip.Addr, mtu uint32, headroom int) []byte {
icmpType := header.ICMPv6DstUnreachable
var icmpCode header.ICMPv6Code
switch errorType {
case ICMPErrorNoRoute:
icmpCode = header.ICMPv6NetworkUnreachable
case ICMPErrorAddressUnreachable:
icmpCode = header.ICMPv6AddressUnreachable
case ICMPErrorPortUnreachable:
icmpCode = header.ICMPv6PortUnreachable
case ICMPErrorSourcePolicy:
icmpCode = header.ICMPv6Policy
case ICMPErrorHopLimitExceeded:
icmpType = header.ICMPv6TimeExceeded
icmpCode = header.ICMPv6HopLimitExceeded
case ICMPErrorPacketTooBig:
icmpType = header.ICMPv6PacketTooBig
icmpCode = header.ICMPv6UnusedCode
}
const maxPayloadLength = header.IPv6MinimumMTU - header.IPv6MinimumSize - header.ICMPv6ErrorHeaderSize
payloadLength := min(header.IPv6MinimumSize+int(packet.PayloadLength()), len(packet), maxPayloadLength)
size := header.IPv6MinimumSize + header.ICMPv6ErrorHeaderSize + payloadLength
buffer := make([]byte, headroom+size)
response := header.IPv6(buffer[headroom:])
response.Encode(&header.IPv6Fields{
PayloadLength: uint16(header.ICMPv6ErrorHeaderSize + payloadLength),
TransportProtocol: header.ICMPv6ProtocolNumber,
HopLimit: synthesizedTTL,
SrcAddr: source,
DstAddr: packet.SourceAddr(),
})
icmpHdr := header.ICMPv6(response.Payload())
icmpHdr.SetType(icmpType)
icmpHdr.SetCode(icmpCode)
if errorType == ICMPErrorPacketTooBig {
icmpHdr.SetMTU(max(mtu, header.IPv6MinimumMTU))
}
copy(icmpHdr[header.ICMPv6ErrorHeaderSize:], packet[:payloadLength])
icmpHdr.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{
Header: icmpHdr,
Src: response.SourceAddressSlice(),
Dst: response.DestinationAddressSlice(),
}))
return buffer
}
func IPTransportProtocol(packet []byte) (uint8, bool) {
switch header.IPVersion(packet) {
case header.IPv4Version:
ipHdr := header.IPv4(packet)
if !ipHdr.IsValid(len(packet)) {
return 0, false
}
return ipHdr.Protocol(), true
case header.IPv6Version:
ipHdr := header.IPv6(packet)
if !ipHdr.IsValid(len(packet)) {
return 0, false
}
protocol := ipHdr.NextHeader()
payload := ipHdr.Payload()
for {
var fragment, transportPresent bool
protocol, payload, fragment, transportPresent = skipIPv6ExtensionHeaders(protocol, payload)
if transportPresent {
return protocol, true
}
if !fragment || len(payload) < header.IPv6FragmentHeaderSize {
return 0, false
}
protocol = payload[0]
if binary.BigEndian.Uint16(payload[2:])>>3 != 0 {
switch header.IPv6ExtensionHeaderIdentifier(protocol) {
case header.IPv6HopByHopOptionsExtHdrIdentifier, header.IPv6RoutingExtHdrIdentifier, header.IPv6DestinationOptionsExtHdrIdentifier, header.IPv6FragmentExtHdrIdentifier:
return 0, false
}
return protocol, true
}
payload = payload[header.IPv6FragmentHeaderSize:]
}
default:
return 0, false
}
}