Repository navigation
Expand file tree
/
Copy pathflow_nat.go
More file actions
374 lines (345 loc) · 9.65 KB
/
Copy pathflow_nat.go
File metadata and controls
374 lines (345 loc) · 9.65 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package tun
import (
"net/netip"
"runtime"
"slices"
"sync"
"github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/contrab/maphash"
)
const (
natSelectorMin = 49152
natSelectorMax = 65535
)
var (
natDefaultRanges = []SelectorRange{{Start: natSelectorMin, Count: natSelectorMax - natSelectorMin + 1}}
portNATAccess sync.Mutex
portNATs = make(map[Port]*portNAT)
)
type portNAT struct {
port Port
references int
hasher maphash.Hasher[flowKey]
shardMask uint32
shards []natShard
access sync.RWMutex
selectors map[natSelectorKey]*natSelector
mappings map[natMappingKey]*natMapping
counter uint32
mappingCounter uint32
}
type natShard struct {
access sync.RWMutex
flows map[flowKey]*forwardFlow
}
type natSelectorKey struct {
protocol uint8
portAddress netip.Addr
selector uint16
}
type natSelector struct {
references uint32
mapping *natMapping
}
type natMappingKey struct {
dispatcher *ForwardDispatcher
portAddress netip.Addr
client netip.AddrPort
server netip.AddrPort
}
type natMapping struct {
key natMappingKey
selector uint16
reverseRule rewriteRule
flows []*forwardFlow
anchor *forwardFlow
}
type natReservation struct {
selectorKey natSelectorKey
reverseKey flowKey
mapping *natMapping
}
func acquirePortNAT(port Port) *portNAT {
portNATAccess.Lock()
defer portNATAccess.Unlock()
nat, loaded := portNATs[port]
if !loaded {
shardCount := 1
for shardCount < runtime.GOMAXPROCS(0) {
shardCount <<= 1
}
nat = &portNAT{
port: port,
hasher: maphash.NewHasher[flowKey](),
shardMask: uint32(shardCount - 1),
shards: make([]natShard, shardCount),
selectors: make(map[natSelectorKey]*natSelector),
mappings: make(map[natMappingKey]*natMapping),
}
for i := range nat.shards {
nat.shards[i].flows = make(map[flowKey]*forwardFlow)
}
portNATs[port] = nat
}
nat.references++
return nat
}
func releasePortNAT(nat *portNAT) {
portNATAccess.Lock()
defer portNATAccess.Unlock()
nat.references--
if nat.references == 0 {
delete(portNATs, nat.port)
}
}
func (n *portNAT) shard(key flowKey) *natShard {
return &n.shards[n.hasher.Hash32(key)&n.shardMask]
}
func (n *portNAT) lookup(key flowKey) *forwardFlow {
shard := n.shard(key)
shard.access.RLock()
flow := shard.flows[key]
shard.access.RUnlock()
return flow
}
func (n *portNAT) insertIfAbsent(key flowKey, flow *forwardFlow) bool {
shard := n.shard(key)
shard.access.Lock()
defer shard.access.Unlock()
_, occupied := shard.flows[key]
if occupied {
return false
}
shard.flows[key] = flow
return true
}
func (n *portNAT) replace(key flowKey, flow *forwardFlow) {
shard := n.shard(key)
shard.access.Lock()
shard.flows[key] = flow
shard.access.Unlock()
}
func (n *portNAT) delete(key flowKey, flow *forwardFlow) {
shard := n.shard(key)
shard.access.Lock()
if shard.flows[key] == flow {
delete(shard.flows, key)
}
shard.access.Unlock()
}
func (n *portNAT) reverseKeyFor(protocol uint8, portAddress netip.Addr, server netip.AddrPort, selector uint16) flowKey {
if isICMPProtocol(protocol) {
return flowKey{
protocol: protocol,
source: netip.AddrPortFrom(server.Addr(), selector),
destination: netip.AddrPortFrom(portAddress, selector),
}
}
return flowKey{
protocol: protocol,
source: server,
destination: netip.AddrPortFrom(portAddress, selector),
}
}
func (n *portNAT) selectorRanges(protocol uint8) (ranges []SelectorRange, ranged bool) {
if isICMPProtocol(protocol) {
return natDefaultRanges, false
}
rangedPort, isRanged := portCapability[PortWithSelectorRange](n.port)
if !isRanged {
return natDefaultRanges, false
}
return rangedPort.PortSelectorRanges(protocol), true
}
func withinRanges(ranges []SelectorRange, selector uint16) bool {
return slices.ContainsFunc(ranges, func(selectorRange SelectorRange) bool {
return selector >= selectorRange.Start && selector-selectorRange.Start < selectorRange.Count
})
}
func allocateSelector(ranges []SelectorRange, counter *uint32, claim func(candidate uint16) bool) bool {
var total uint32
for _, selectorRange := range ranges {
total += uint32(selectorRange.Count)
}
for range total {
*counter++
index := *counter % total
for _, selectorRange := range ranges {
if index < uint32(selectorRange.Count) {
if claim(selectorRange.Start + uint16(index)) {
return true
}
break
}
index -= uint32(selectorRange.Count)
}
}
return false
}
func (n *portNAT) reserve(dispatcher *ForwardDispatcher, protocol uint8, portAddress netip.Addr, client netip.AddrPort, server netip.AddrPort) (natReservation, bool) {
reservation := natReservation{
selectorKey: natSelectorKey{protocol: protocol, portAddress: portAddress},
}
isUDP := protocol == uint8(header.UDPProtocolNumber)
var mappingKey natMappingKey
if isUDP {
mappingKey = natMappingKey{
dispatcher: dispatcher,
portAddress: portAddress,
client: client,
}
switch dispatcher.udpMapping {
case NATMappingAddressDependent:
mappingKey.server = netip.AddrPortFrom(server.Addr(), 0)
case NATMappingAddressAndPortDependent:
mappingKey.server = server
}
}
n.access.Lock()
defer n.access.Unlock()
var existingMapping *natMapping
if isUDP {
existingMapping = n.mappings[mappingKey]
if existingMapping != nil {
reverseKey := n.reverseKeyFor(protocol, portAddress, server, existingMapping.selector)
if n.insertIfAbsent(reverseKey, nil) {
reservation.selectorKey.selector = existingMapping.selector
reservation.reverseKey = reverseKey
reservation.mapping = existingMapping
n.selectors[reservation.selectorKey].references++
return reservation, true
}
}
}
ranges, ranged := n.selectorRanges(protocol)
reserver, hasReserver := portCapability[PortWithSelectorReservation](n.port)
claim := func(candidate uint16) bool {
selectorKey := natSelectorKey{protocol: protocol, portAddress: portAddress, selector: candidate}
reverseKey := n.reverseKeyFor(protocol, portAddress, server, candidate)
entry := n.selectors[selectorKey]
if entry != nil {
if isUDP || !n.insertIfAbsent(reverseKey, nil) {
return false
}
entry.references++
} else {
address := netip.AddrPortFrom(portAddress, candidate)
if hasReserver && !reserver.ReserveSelector(protocol, address) {
return false
}
if !n.insertIfAbsent(reverseKey, nil) {
if hasReserver {
reserver.ReleaseSelector(protocol, address)
}
return false
}
n.selectors[selectorKey] = &natSelector{references: 1}
}
reservation.selectorKey = selectorKey
reservation.reverseKey = reverseKey
return true
}
claimed := client.Port() != 0 && (!ranged || withinRanges(ranges, client.Port())) && claim(client.Port())
if !claimed {
counter := &n.counter
if isUDP {
counter = &n.mappingCounter
}
claimed = allocateSelector(ranges, counter, claim)
}
if !claimed {
return reservation, false
}
if isUDP && existingMapping == nil {
reservation.mapping = &natMapping{
key: mappingKey,
selector: reservation.selectorKey.selector,
reverseRule: rewriteRule{
destinationAddress: addrToTCPIP(client.Addr()),
destinationPort: client.Port(),
rewriteDestinationPort: true,
},
}
n.mappings[mappingKey] = reservation.mapping
n.selectors[reservation.selectorKey].mapping = reservation.mapping
}
return reservation, true
}
func (n *portNAT) publish(flow *forwardFlow, reservation natReservation) {
flow.selectorKey = reservation.selectorKey
flow.reverseKey = reservation.reverseKey
flow.forwardRule.sourcePort = reservation.selectorKey.selector
mapping := reservation.mapping
if mapping == nil {
n.replace(reservation.reverseKey, flow)
return
}
n.access.Lock()
defer n.access.Unlock()
flow.mapping = mapping
mapping.flows = append(mapping.flows, flow)
if mapping.anchor == nil || mapping.anchor.closed.Load() {
mapping.anchor = flow
}
n.replace(reservation.reverseKey, flow)
}
func (n *portNAT) release(flow *forwardFlow) {
n.delete(flow.reverseKey, flow)
n.access.Lock()
defer n.access.Unlock()
entry := n.selectors[flow.selectorKey]
mapping := flow.mapping
if mapping != nil {
mapping.flows = slices.DeleteFunc(mapping.flows, func(owned *forwardFlow) bool { return owned == flow })
if mapping.anchor == flow {
mapping.anchor = mapping.liveFlowLocked()
}
}
entry.references--
if entry.references > 0 {
return
}
delete(n.selectors, flow.selectorKey)
if mapping != nil {
delete(n.mappings, mapping.key)
}
reserver, hasReserver := portCapability[PortWithSelectorReservation](n.port)
if hasReserver {
reserver.ReleaseSelector(flow.protocol, netip.AddrPortFrom(flow.selectorKey.portAddress, flow.selectorKey.selector))
}
}
func (m *natMapping) liveFlowLocked() *forwardFlow {
var fallback *forwardFlow
for _, flow := range m.flows {
if !flow.closed.Load() {
return flow
}
fallback = flow
}
return fallback
}
func (n *portNAT) lookupMapping(portAddress netip.Addr, selector uint16, peer netip.Addr) (*natMapping, *forwardFlow) {
n.access.RLock()
defer n.access.RUnlock()
entry := n.selectors[natSelectorKey{protocol: uint8(header.UDPProtocolNumber), portAddress: portAddress, selector: selector}]
if entry == nil || entry.mapping == nil {
return nil, nil
}
mapping := entry.mapping
switch mapping.key.dispatcher.udpFiltering {
case NATFilteringAddressAndPortDependent:
return nil, nil
case NATFilteringAddressDependent:
if !slices.ContainsFunc(mapping.flows, func(flow *forwardFlow) bool { return flow.serverAddress == peer }) {
return nil, nil
}
}
flow := mapping.anchor
if flow == nil || flow.closed.Load() {
flow = mapping.liveFlowLocked()
}
if flow == nil {
return nil, nil
}
return mapping, flow
}