-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
1246 lines (1178 loc) · 38.6 KB
/
Copy pathstack.go
File metadata and controls
1246 lines (1178 loc) · 38.6 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package mipstack implements the mihomo IP stack (MIPS), a small userspace
// IPv4/IPv6 endpoint stack for applications that exchange complete packets
// with an L3 link. It implements active and passive TCP, connected and
// unconnected UDP and IP protocol sockets, and the ICMP behavior required by
// those transports.
package mipstack
import (
"context"
"crypto/rand"
"encoding/binary"
"errors"
"io"
"net"
"net/netip"
"os"
"sync"
"sync/atomic"
"syscall"
"time"
)
const (
// dynamicPortFirst is the first IANA dynamic client port.
dynamicPortFirst = 49152
// dynamicPortCount is the size of the IANA dynamic port range.
dynamicPortCount = 1 << 14
// fallbackPortFirst keeps well-known ports out of automatic allocation.
fallbackPortFirst = 1024
// fallbackPortCount is used only after the IANA dynamic range is exhausted.
fallbackPortCount = dynamicPortFirst - fallbackPortFirst
// defaultMTU matches the conventional Ethernet payload MTU.
defaultMTU = 1500
// ipv6MinimumMTU is both the minimum configured IPv6 link MTU and the
// maximum complete ICMPv6 error size required by RFC 4443.
ipv6MinimumMTU = 1280
// outboundPacketQueue bounds packets waiting for the embedding link.
outboundPacketQueue = 256
// loopbackPacketQueue bounds asynchronous local delivery and prevents a
// socket actor from recursively entering its own protocol handler.
loopbackPacketQueue = 256
// pathMTUMaximumEntries bounds destinations learned from authenticated
// transport tuples so long-running proxy workloads cannot grow the cache
// without limit.
pathMTUMaximumEntries = 1024
// pathMTULifetime eventually retries the controller-provided link MTU after
// a transient lower-path constraint disappears.
pathMTULifetime = 10 * time.Minute
// controlResponseRate is the sustained number of unsolicited control
// responses permitted per class and second.
controlResponseRate = 100
// controlResponseBurst permits short diagnostic bursts without allowing a
// packet flood to monopolize the outbound queue.
controlResponseBurst = 200
)
var (
// ErrClosed is returned after the stack has been closed.
ErrClosed = net.ErrClosed
// ErrNotStarted is returned when packet or socket I/O is attempted before
// Start.
ErrNotStarted = errors.New("mipstack: stack is not started")
// ErrNoPorts reports exhaustion of all automatically allocated,
// non-privileged ports.
ErrNoPorts = errors.New("mipstack: no automatic ports available")
// ErrResourceLimit reports exhaustion of a bounded in-memory socket or
// protocol resource.
ErrResourceLimit = errors.New("mipstack: resource limit reached")
)
// Config configures a Stack.
type Config struct {
// LocalAddresses lists addresses that may receive packets and be selected
// as transport endpoints.
LocalAddresses []netip.Prefix
// MTU bounds packets emitted by Read. Zero selects 1500.
MTU uint32
// Routes optionally restrict admitted unicast destinations and provide a
// preferred source. Nil installs one default route per configured address
// family; a non-nil empty slice installs no routes.
Routes []Route
// MaxTCPConnections optionally bounds active, handshaking, and TIME_WAIT
// connections. Zero leaves the number unbounded; per-listener queues and
// per-connection buffers remain independently bounded.
MaxTCPConnections int
// CongestionControl selects the TCP congestion-control algorithm. The zero
// value selects CUBIC.
CongestionControl CongestionControl
}
// Stack converts raw IPv4/IPv6 packets to application TCP, UDP, and IP
// protocol sockets.
type Stack struct {
network atomic.Pointer[networkState]
outbound chan []byte
loopback chan []byte
mu sync.RWMutex
started bool
closed bool
tcp map[tcpKey]*TCPConn
tcpPassive tcpPassiveEndpoints
udp map[udpKey]*UDPConn
udpReuse udpReuseEndpoints
ip ipEndpoints
nextPort [2]automaticPortCursor
pathMTUMu sync.RWMutex
pathMTU map[netip.Addr]pathMTUEntry
packetID atomic.Uint32
closeCh chan struct{}
timestampEpoch time.Time
fragmentMu sync.Mutex
fragments map[fragmentKey]*fragmentSet
fragmentBytes int
controlMu sync.Mutex
controlLimiters [controlResponseClassCount]tokenBucket
stats stackCounters
}
// StackStats is a point-in-time snapshot of stack activity. Counters are
// monotonic except ActiveTCPConnections, ActiveTCPListeners,
// ActiveUDPSockets, and ActiveIPSockets.
type StackStats struct {
// InboundPackets counts complete packets presented to the stack.
InboundPackets uint64
// InboundDroppedPackets counts invalid packets and bounded-queue drops.
InboundDroppedPackets uint64
// OutboundPackets counts complete packets accepted by the device queue.
OutboundPackets uint64
// LoopbackPackets counts locally routed packets that bypassed the link.
LoopbackPackets uint64
// ActiveTCPConnections includes handshakes, established flows, and
// TIME_WAIT actors.
ActiveTCPConnections uint64
// ActiveTCPListeners is the current number of passive TCP endpoints.
ActiveTCPListeners uint64
// ActiveUDPSockets is the current number of open packet sockets.
ActiveUDPSockets uint64
// ActiveIPSockets is the current number of open protocol sockets.
ActiveIPSockets uint64
// TCPRetransmissions counts all SYN, data, FIN, SACK, RACK, and tail-probe
// retransmissions.
TCPRetransmissions uint64
// TCPSACKRetransmissions counts retransmissions selected by the SACK
// scoreboard, including its RACK-confirmed subset.
TCPSACKRetransmissions uint64
// TCPRACKRetransmissions counts the time-based subset of SACK recovery.
TCPRACKRetransmissions uint64
// TCPTailLossProbes counts probes sent before the ordinary RTO.
TCPTailLossProbes uint64
// TCPZeroWindowProbes counts persist probes sent while the peer advertises
// a closed receive window.
TCPZeroWindowProbes uint64
// TCPKeepAliveProbes counts probes sent after configured receive inactivity.
TCPKeepAliveProbes uint64
// PathMTUUpdates counts accepted destination PMTU reductions.
PathMTUUpdates uint64
// PathMTUBlackHoleReductions counts PMTU reductions inferred from repeated
// TCP timeouts rather than ICMP.
PathMTUBlackHoleReductions uint64
// FragmentEvictions counts incomplete datagrams removed for capacity.
FragmentEvictions uint64
// FragmentTimeouts counts incomplete datagrams removed for age.
FragmentTimeouts uint64
// RateLimitedControlResponses counts suppressed TCP RST and challenge ACK,
// ICMP unreachable, parameter-problem, and ICMP echo replies.
RateLimitedControlResponses uint64
}
// stackCounters stores concurrently updated statistics.
type stackCounters struct {
inboundPackets atomic.Uint64
inboundDroppedPackets atomic.Uint64
outboundPackets atomic.Uint64
loopbackPackets atomic.Uint64
activeTCPConnections atomic.Uint64
activeTCPListeners atomic.Uint64
activeUDPSockets atomic.Uint64
activeIPSockets atomic.Uint64
tcpRetransmissions atomic.Uint64
tcpSACKRetransmissions atomic.Uint64
tcpRACKRetransmissions atomic.Uint64
tcpTailLossProbes atomic.Uint64
tcpZeroWindowProbes atomic.Uint64
tcpKeepAliveProbes atomic.Uint64
pathMTUUpdates atomic.Uint64
pathMTUBlackHoleReductions atomic.Uint64
fragmentEvictions atomic.Uint64
fragmentTimeouts atomic.Uint64
rateLimitedControlResponses atomic.Uint64
}
// controlResponseClass separates independent control-plane token buckets.
type controlResponseClass uint8
const (
// controlResponseTCPReset limits resets for unbound TCP tuples.
controlResponseTCPReset controlResponseClass = iota
// controlResponseTCPChallengeACK limits RFC 5961 acknowledgements for
// suspicious segments on established tuples.
controlResponseTCPChallengeACK
// controlResponsePortUnreachable limits ICMP errors for unbound UDP ports.
controlResponsePortUnreachable
// controlResponseEchoReply limits ICMP echo replies.
controlResponseEchoReply
// controlResponseParameterProblem limits errors for unsupported IPv6
// options and upper-layer protocols.
controlResponseParameterProblem
// controlResponseClassCount is the number of independent token buckets.
controlResponseClassCount
)
// tokenBucket is one lock-protected control-response limiter.
type tokenBucket struct {
tokens float64
updated time.Time
}
// pathMTUEntry is one learned destination MTU and its last confirmation.
type pathMTUEntry struct {
mtu int
updated time.Time
}
// udpKey identifies one specific or wildcard local UDP endpoint.
type udpKey struct {
address netip.Addr
port uint16
}
// tcpKey is the four-tuple used to dispatch inbound TCP segments.
type tcpKey struct {
local netip.AddrPort
remote netip.AddrPort
}
// automaticPortCursor remembers the next randomized position in the primary
// IANA range and its lower, non-privileged fallback range.
type automaticPortCursor struct {
dynamic uint16
fallback uint16
}
// New constructs an inactive-socket stack.
func New(config Config) (*Stack, error) {
state, err := buildNetworkState(config)
if err != nil {
return nil, err
}
ports4, err := randomAutomaticPortCursor()
if err != nil {
return nil, err
}
ports6, err := randomAutomaticPortCursor()
if err != nil {
return nil, err
}
stack := &Stack{
outbound: make(chan []byte, outboundPacketQueue), loopback: make(chan []byte, loopbackPacketQueue),
tcp: make(map[tcpKey]*TCPConn), udp: make(map[udpKey]*UDPConn),
nextPort: [2]automaticPortCursor{ports4, ports6}, pathMTU: make(map[netip.Addr]pathMTUEntry),
closeCh: make(chan struct{}), timestampEpoch: time.Now(), fragments: make(map[fragmentKey]*fragmentSet),
}
stack.network.Store(state)
return stack, nil
}
// UpdateConfig atomically replaces addresses, routes, the link MTU, congestion
// control, and the optional TCP connection limit.
// Sockets bound to removed addresses or destinations without a remaining
// route are closed. Other TCP connections immediately reclamp their MSS.
func (s *Stack) UpdateConfig(config Config) error {
state, err := buildNetworkState(config)
if err != nil {
return err
}
s.mu.Lock()
if s.closed {
s.mu.Unlock()
return ErrClosed
}
previous := s.network.Swap(state)
if previous == nil || previous.mtu != state.mtu {
s.pathMTUMu.Lock()
s.pathMTU = make(map[netip.Addr]pathMTUEntry)
s.pathMTUMu.Unlock()
}
tcpConnections := make([]*TCPConn, 0, len(s.tcp))
for _, connection := range s.tcp {
tcpConnections = append(tcpConnections, connection)
}
tcpPassive := s.tcpPassive
udpConnections := s.udpConnectionsLocked()
ip := s.ip
s.mu.Unlock()
if tcpPassive != nil {
tcpPassive.updateConfig(s, state)
}
for _, connection := range tcpConnections {
_, routed := state.routeFor(connection.key.remote.Addr())
if !networkStateHasLocal(state, connection.key.local.Addr()) {
connection.abortWithoutReset(syscall.EADDRNOTAVAIL)
continue
}
if !routed {
connection.abortWithoutReset(syscall.ENETUNREACH)
continue
}
select {
case connection.pathMTUUpdate <- struct{}{}:
default:
}
}
for _, connection := range udpConnections {
if connection.dual && !networkStateHasFamily(state, false) && !networkStateHasFamily(state, true) ||
!connection.dual && connection.local.IsUnspecified() && !networkStateHasFamily(state, connection.v6) ||
connection.local.IsValid() && !connection.local.IsUnspecified() && !networkStateHasLocal(state, connection.local) {
s.closeUDP(connection)
continue
}
if connection.remote.IsValid() {
if _, routed := state.routeFor(connection.remote.Addr()); !routed {
s.closeUDP(connection)
}
}
}
if ip != nil {
ip.updateConfig(s, state)
}
return nil
}
// LocalAddresses returns an independent snapshot of all configured local
// addresses in configuration order.
func (s *Stack) LocalAddresses() []netip.Addr {
return append([]netip.Addr(nil), s.network.Load().sources...)
}
// RouteFor returns the selected route for one unicast destination.
func (s *Stack) RouteFor(destination netip.Addr) (Route, error) {
destination = destination.Unmap()
if !destination.IsValid() || destination.IsUnspecified() || destination.IsMulticast() || destination.Zone() != "" {
return Route{}, syscall.EINVAL
}
route, exists := s.network.Load().routeFor(destination)
if !exists {
return Route{}, syscall.ENETUNREACH
}
return route, nil
}
// networkStateHasLocal reports membership in an immutable configuration.
func networkStateHasLocal(state *networkState, address netip.Addr) bool {
_, exists := state.local[address.Unmap()]
return exists
}
// networkStateHasFamily reports whether one configured source belongs to the
// requested address family.
func networkStateHasFamily(state *networkState, v6 bool) bool {
for _, source := range state.sources {
if source.Is6() == v6 {
return true
}
}
return false
}
// listenAddress validates a listen network and canonicalizes a generic
// wildcard to the same dual-stack IPv6 representation used by net.Listen.
func listenAddress(state *networkState, network, protocol string, address netip.Addr) (netip.Addr, bool, error) {
if err := validateListenNetwork(network, protocol, address); err != nil {
return netip.Addr{}, false, err
}
switch network {
case protocol + "4":
if !networkStateHasFamily(state, false) {
return netip.Addr{}, false, syscall.EADDRNOTAVAIL
}
if !address.IsValid() {
address = netip.IPv4Unspecified()
}
return address, false, nil
case protocol + "6":
if !networkStateHasFamily(state, true) {
return netip.Addr{}, false, syscall.EADDRNOTAVAIL
}
if !address.IsValid() {
address = netip.IPv6Unspecified()
}
return address, false, nil
case protocol:
}
if address.IsValid() && !address.IsUnspecified() {
return address, false, nil
}
have4 := networkStateHasFamily(state, false)
have6 := networkStateHasFamily(state, true)
if have6 {
return netip.IPv6Unspecified(), have4, nil
}
if have4 {
return netip.IPv4Unspecified(), false, nil
}
return netip.Addr{}, false, syscall.EADDRNOTAVAIL
}
// validateListenNetwork checks a listener's protocol name and an explicitly
// supplied address family before stack lifecycle or binding errors.
func validateListenNetwork(network, protocol string, address netip.Addr) error {
switch network {
case protocol:
return nil
case protocol + "4":
if address.IsValid() && address.Is6() {
return syscall.EAFNOSUPPORT
}
return nil
case protocol + "6":
if address.IsValid() && address.Is4() {
return syscall.EAFNOSUPPORT
}
return nil
default:
return net.UnknownNetworkError(network)
}
}
// mtuFor returns the unexpired destination PMTU, or the managed link MTU.
func (s *Stack) mtuFor(destination netip.Addr) int {
destination = destination.Unmap()
linkMTU := s.network.Load().mtu
now := time.Now()
s.pathMTUMu.RLock()
entry, exists := s.pathMTU[destination]
s.pathMTUMu.RUnlock()
if exists && now.Sub(entry.updated) < pathMTULifetime && entry.mtu < linkMTU {
return entry.mtu
}
if exists && now.Sub(entry.updated) >= pathMTULifetime {
s.pathMTUMu.Lock()
current, currentExists := s.pathMTU[destination]
if currentExists && now.Sub(current.updated) >= pathMTULifetime {
delete(s.pathMTU, destination)
}
s.pathMTUMu.Unlock()
}
return linkMTU
}
// pathMTUExpiry returns the time at which a destination PMTU should be probed
// upward. A past expiry remains actionable so a connection that raced cache
// expiry while starting is woken immediately; mtuFor then removes the entry.
func (s *Stack) pathMTUExpiry(destination netip.Addr) (time.Time, bool) {
destination = destination.Unmap()
linkMTU := s.network.Load().mtu
s.pathMTUMu.RLock()
entry, exists := s.pathMTU[destination]
s.pathMTUMu.RUnlock()
if !exists || entry.mtu >= linkMTU {
return time.Time{}, false
}
return entry.updated.Add(pathMTULifetime), true
}
// notifyTCPPathMTU wakes all established and handshaking flows to one
// destination except an optional actor that is already applying the change.
// The PMTU lock is never held while acquiring the socket registry lock.
func (s *Stack) notifyTCPPathMTU(destination netip.Addr, except *TCPConn) {
destination = destination.Unmap()
s.mu.RLock()
for key, connection := range s.tcp {
if connection == nil || connection == except || key.remote.Addr() != destination {
continue
}
select {
case connection.pathMTUUpdate <- struct{}{}:
default:
}
}
s.mu.RUnlock()
}
// observePathMTU records a validated ICMP next-hop MTU reduction.
func (s *Stack) observePathMTU(destination netip.Addr, mtu uint32) bool {
destination = destination.Unmap()
minimum := uint32(68)
if destination.Is6() {
minimum = 1280
}
if !destination.IsValid() || mtu == 0 {
return false
}
if mtu < minimum {
mtu = minimum
}
if mtu >= uint32(s.network.Load().mtu) {
return false
}
now := time.Now()
s.pathMTUMu.Lock()
defer s.pathMTUMu.Unlock()
current, exists := s.pathMTU[destination]
if exists && current.mtu <= int(mtu) && now.Sub(current.updated) < pathMTULifetime {
current.updated = now
s.pathMTU[destination] = current
return false
}
if !exists && len(s.pathMTU) >= pathMTUMaximumEntries {
var oldestAddress netip.Addr
var oldest pathMTUEntry
for address, entry := range s.pathMTU {
if !oldestAddress.IsValid() || entry.updated.Before(oldest.updated) {
oldestAddress, oldest = address, entry
}
}
delete(s.pathMTU, oldestAddress)
}
s.pathMTU[destination] = pathMTUEntry{mtu: int(mtu), updated: now}
s.stats.pathMTUUpdates.Add(1)
return true
}
// Stats returns a consistent-enough lock-free snapshot of stack counters.
// Concurrent activity may become visible across adjacent fields at slightly
// different instants.
func (s *Stack) Stats() StackStats {
return StackStats{
InboundPackets: s.stats.inboundPackets.Load(),
InboundDroppedPackets: s.stats.inboundDroppedPackets.Load(),
OutboundPackets: s.stats.outboundPackets.Load(),
LoopbackPackets: s.stats.loopbackPackets.Load(),
ActiveTCPConnections: s.stats.activeTCPConnections.Load(),
ActiveTCPListeners: s.stats.activeTCPListeners.Load(),
ActiveUDPSockets: s.stats.activeUDPSockets.Load(),
ActiveIPSockets: s.stats.activeIPSockets.Load(),
TCPRetransmissions: s.stats.tcpRetransmissions.Load(),
TCPSACKRetransmissions: s.stats.tcpSACKRetransmissions.Load(),
TCPRACKRetransmissions: s.stats.tcpRACKRetransmissions.Load(),
TCPTailLossProbes: s.stats.tcpTailLossProbes.Load(),
TCPZeroWindowProbes: s.stats.tcpZeroWindowProbes.Load(),
TCPKeepAliveProbes: s.stats.tcpKeepAliveProbes.Load(),
PathMTUUpdates: s.stats.pathMTUUpdates.Load(),
PathMTUBlackHoleReductions: s.stats.pathMTUBlackHoleReductions.Load(),
FragmentEvictions: s.stats.fragmentEvictions.Load(),
FragmentTimeouts: s.stats.fragmentTimeouts.Load(),
RateLimitedControlResponses: s.stats.rateLimitedControlResponses.Load(),
}
}
// allowControlResponse consumes one token from a control-response class.
func (s *Stack) allowControlResponse(class controlResponseClass) bool {
now := time.Now()
s.controlMu.Lock()
bucket := &s.controlLimiters[class]
if bucket.updated.IsZero() {
bucket.tokens = controlResponseBurst
} else {
bucket.tokens += now.Sub(bucket.updated).Seconds() * controlResponseRate
if bucket.tokens > controlResponseBurst {
bucket.tokens = controlResponseBurst
}
}
bucket.updated = now
allowed := bucket.tokens >= 1
if allowed {
bucket.tokens--
}
s.controlMu.Unlock()
if !allowed {
s.stats.rateLimitedControlResponses.Add(1)
}
return allowed
}
// Start activates packet and socket I/O and starts background maintenance.
// Repeated calls do not start additional workers.
func (s *Stack) Start() error {
s.mu.Lock()
if s.closed {
s.mu.Unlock()
return ErrClosed
}
if s.started {
s.mu.Unlock()
return nil
}
s.started = true
s.mu.Unlock()
go s.runFragmentCleaner()
go s.runLoopback()
return nil
}
// runLoopback serializes local delivery outside the sending socket actor.
func (s *Stack) runLoopback() {
for {
select {
case packet := <-s.loopback:
_ = s.handlePacket(packet)
case <-s.closeCh:
return
}
}
}
// ready reports whether the stack has started and has not closed.
func (s *Stack) ready() error {
s.mu.RLock()
defer s.mu.RUnlock()
if s.closed {
return ErrClosed
}
if !s.started {
return ErrNotStarted
}
return nil
}
// sourceFor selects a local address in the destination's family. An address
// whose configured prefix contains the destination wins by longest prefix;
// otherwise the first address in that family is used as the default-route
// source.
func (s *Stack) sourceFor(destination netip.Addr) (netip.Addr, error) {
return s.network.Load().sourceFor(destination, netip.Addr{})
}
// sourceForRequested validates an explicit source or selects one automatically.
func (s *Stack) sourceForRequested(destination, requested netip.Addr) (netip.Addr, error) {
return s.network.Load().sourceFor(destination, requested)
}
// randomPortOffset randomizes the first candidate in one automatic range.
func randomPortOffset(count uint32) (uint16, error) {
var raw [4]byte
if _, err := rand.Read(raw[:]); err != nil {
return 0, err
}
return uint16(binary.BigEndian.Uint32(raw[:]) % count), nil
}
// randomAutomaticPortCursor independently randomizes both allocation ranges.
func randomAutomaticPortCursor() (automaticPortCursor, error) {
dynamic, err := randomPortOffset(dynamicPortCount)
if err != nil {
return automaticPortCursor{}, err
}
fallback, err := randomPortOffset(fallbackPortCount)
if err != nil {
return automaticPortCursor{}, err
}
return automaticPortCursor{dynamic: dynamic, fallback: fallback}, nil
}
// allocateAutomaticPort selects an available IANA dynamic port first, then
// falls back to the lower non-privileged range only after a complete scan.
func allocateAutomaticPort(cursor *automaticPortCursor, available func(uint16) bool) (uint16, error) {
ranges := [...]struct {
first uint32
count uint32
cursor *uint16
}{
{dynamicPortFirst, dynamicPortCount, &cursor.dynamic},
{fallbackPortFirst, fallbackPortCount, &cursor.fallback},
}
for _, portRange := range ranges {
start := uint32(*portRange.cursor)
for offset := uint32(0); offset < portRange.count; offset++ {
position := (start + offset) % portRange.count
port := uint16(portRange.first + position)
if !available(port) {
continue
}
*portRange.cursor = uint16((position + 1) % portRange.count)
return port, nil
}
}
return 0, ErrNoPorts
}
// isLocal reports whether address belongs to this stack.
func (s *Stack) isLocal(address netip.Addr) bool {
return networkStateHasLocal(s.network.Load(), address)
}
// allocateUDPPortLocked reserves one collision-free automatic local endpoint
// while s.mu is held.
func (s *Stack) allocateUDPPortLocked(binding udpSocketBinding, address netip.Addr, dual bool) (uint16, error) {
index := 0
if address.Is6() {
index = 1
}
return allocateAutomaticPort(&s.nextPort[index], func(port uint16) bool {
return s.udpEndpointAvailableLocked(binding, address, port, dual)
})
}
// udpEndpointAvailableLocked reports whether address and port can be bound
// without overlapping a wildcard or exact endpoint while s.mu is held.
func (s *Stack) udpEndpointAvailableLocked(binding udpSocketBinding, address netip.Addr, port uint16, dual bool) bool {
if !binding.available(s, address, port, dual) {
return false
}
for key, connection := range s.udp {
if key.port == port && listenAddressesOverlap(key.address, connection.dual, address, dual) {
return false
}
}
return true
}
// listenAddressesOverlap reports whether two single-interface bindings cover
// at least one common local address family and address.
func listenAddressesOverlap(left netip.Addr, leftDual bool, right netip.Addr, rightDual bool) bool {
if leftDual || rightDual {
if left.IsUnspecified() && right.IsUnspecified() {
return true
}
if leftDual && right.Is4() || rightDual && left.Is4() {
return true
}
}
return left.Is6() == right.Is6() && (left.IsUnspecified() || right.IsUnspecified() || left == right)
}
// allocateTCPPortLocked selects a local port whose complete four-tuple is not
// active or in TIME_WAIT while s.mu is held.
func (s *Stack) allocateTCPPortLocked(local netip.Addr, remote netip.AddrPort) (uint16, error) {
index := 0
if remote.Addr().Is6() {
index = 1
}
return allocateAutomaticPort(&s.nextPort[index], func(port uint16) bool {
if s.tcpPortListenedLocked(local, port) {
return false
}
key := tcpKey{local: netip.AddrPortFrom(local, port), remote: remote}
if _, exists := s.tcp[key]; exists {
return false
}
return true
})
}
// tcpPortListenedLocked reports whether a wildcard or exact listener owns a
// local TCP endpoint while s.mu is held.
func (s *Stack) tcpPortListenedLocked(local netip.Addr, port uint16) bool {
return s.tcpPassive != nil && s.tcpPassive.portListened(local, port)
}
// ListenUDP binds an unconnected UDP packet socket. Network must be udp, udp4,
// or udp6. A wildcard with udp uses one dual-stack endpoint when both families
// are configured. Port zero selects an automatic port.
func (s *Stack) ListenUDP(ctx context.Context, network string, local netip.AddrPort) (net.PacketConn, error) {
return s.listenUDP(ctx, network, local, exclusiveUDPSocketBinding{})
}
// listenUDP contains validation, automatic port allocation, and socket
// construction shared by the ordinary and optional REUSEPORT entry points.
func (s *Stack) listenUDP(ctx context.Context, network string, local netip.AddrPort, binding udpSocketBinding) (net.PacketConn, error) {
address := local.Addr().Unmap()
local = netip.AddrPortFrom(address, local.Port())
target := udpNetAddr(local)
wrap := func(err error) (net.PacketConn, error) {
return nil, socketOperationError("listen", network, nil, target, err)
}
if err := validateListenNetwork(network, "udp", address); err != nil {
return wrap(err)
}
if address.IsValid() && (address.IsMulticast() || address.Zone() != "") {
return wrap(errors.New("mipstack: invalid UDP listen address"))
}
if address.IsValid() && !address.IsUnspecified() && !s.isLocal(address) {
return wrap(syscall.EADDRNOTAVAIL)
}
if err := ctx.Err(); err != nil {
return wrap(err)
}
if err := s.ready(); err != nil {
return wrap(err)
}
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return wrap(ErrClosed)
}
state := s.network.Load()
address, dual, err := listenAddress(state, network, "udp", address)
if err != nil {
return wrap(err)
}
if !address.IsUnspecified() && !networkStateHasLocal(state, address) {
return wrap(syscall.EADDRNOTAVAIL)
}
local = netip.AddrPortFrom(address, local.Port())
port := local.Port()
if port == 0 {
port, err = s.allocateUDPPortLocked(binding, address, dual)
if err != nil {
return wrap(err)
}
} else if !s.udpEndpointAvailableLocked(binding, address, port, dual) {
return wrap(syscall.EADDRINUSE)
}
connection := newUDPConn(s, network, port, address.Is6(), address, netip.AddrPort{})
connection.dual = dual
if err = binding.register(s, connection); err != nil {
return wrap(err)
}
s.stats.activeUDPSockets.Add(1)
return connection, nil
}
// DialUDP creates a connected UDP socket for one IPv4 or IPv6 remote endpoint.
// Network must be udp, udp4, or udp6. A zero source selects both address and
// port automatically; an unspecified source address selects only the address.
func (s *Stack) DialUDP(ctx context.Context, network string, source, remote netip.AddrPort) (net.Conn, error) {
remote = netip.AddrPortFrom(remote.Addr().Unmap(), remote.Port())
target := udpNetAddr(remote)
wrap := func(source net.Addr, err error) (net.Conn, error) {
return nil, socketOperationError("dial", network, source, target, err)
}
if err := validateTransportNetwork(network, "udp", remote.Addr()); err != nil {
return wrap(nil, err)
}
if !remote.IsValid() || remote.Port() == 0 || remote.Addr().IsUnspecified() || remote.Addr().IsMulticast() || remote.Addr().Zone() != "" {
return wrap(nil, errors.New("mipstack: invalid UDP destination"))
}
if err := ctx.Err(); err != nil {
return wrap(nil, err)
}
if err := s.ready(); err != nil {
return wrap(nil, err)
}
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return wrap(nil, ErrClosed)
}
local, err := s.localEndpointFor(network, remote, source)
if err != nil {
return wrap(nil, err)
}
localAddress := net.UDPAddrFromAddrPort(local)
port := local.Port()
if port == 0 {
port, err = s.allocateUDPPortLocked(exclusiveUDPSocketBinding{}, local.Addr(), false)
if err != nil {
return wrap(localAddress, err)
}
} else if !s.udpEndpointAvailableLocked(exclusiveUDPSocketBinding{}, local.Addr(), port, false) {
return wrap(localAddress, syscall.EADDRINUSE)
}
local = netip.AddrPortFrom(local.Addr(), port)
connection := newUDPConn(s, network, port, remote.Addr().Is6(), local.Addr(), remote)
s.udp[udpKey{address: local.Addr(), port: port}] = connection
s.stats.activeUDPSockets.Add(1)
return connection, nil
}
// validateTransportNetwork accepts the network names used by the net package's
// netip-based DialTCP and DialUDP methods and enforces an explicit IP family.
func validateTransportNetwork(network, protocol string, remote netip.Addr) error {
switch network {
case protocol:
return nil
case protocol + "4":
if remote.IsValid() && remote.Is6() {
return syscall.EAFNOSUPPORT
}
return nil
case protocol + "6":
if remote.IsValid() && remote.Is4() {
return syscall.EAFNOSUPPORT
}
return nil
default:
return net.UnknownNetworkError(network)
}
}
// localEndpointFor resolves and validates the local side of an active socket.
func (s *Stack) localEndpointFor(network string, remote, requested netip.AddrPort) (netip.AddrPort, error) {
requestedAddress := requested.Addr()
if requestedAddress.IsValid() {
requestedAddress = requestedAddress.Unmap()
if requestedAddress.Zone() != "" || requestedAddress.IsMulticast() {
return netip.AddrPort{}, syscall.EINVAL
}
if requestedAddress.Is6() != remote.Addr().Is6() && (!requestedAddress.IsUnspecified() || network[len(network)-1] == '4' || network[len(network)-1] == '6') {
family := "IPv6"
if remote.Addr().Is4() {
family = "IPv4"
}
return netip.AddrPort{}, &net.AddrError{Err: "non-" + family + " address", Addr: requestedAddress.String()}
}
}
address, err := s.sourceForRequested(remote.Addr(), requestedAddress)
if err != nil {
return netip.AddrPort{}, err
}
return netip.AddrPortFrom(address, requested.Port()), nil
}
// udpNetAddr returns a standard UDP address when endpoint is valid.
func udpNetAddr(endpoint netip.AddrPort) net.Addr {
return net.UDPAddrFromAddrPort(endpoint)
}
// Read copies one complete outbound IP packet into buffers[0] at offset. Its
// data-plane signature matches tun.Device.Read, but Stack always has batch
// size one.
func (s *Stack) Read(buffers [][]byte, sizes []int, offset int) (int, error) {
if err := s.ready(); err != nil {
if errors.Is(err, ErrClosed) {
return 0, os.ErrClosed
}
return 0, err
}
if len(buffers) == 0 || len(sizes) == 0 {
return 0, errors.New("mipstack: Read requires one buffer and size")
}
if offset < 0 || offset > len(buffers[0]) {
return 0, errors.New("mipstack: invalid Read offset")
}
select {
case <-s.closeCh:
return 0, os.ErrClosed
default:
}
select {
case packet := <-s.outbound:
if len(packet) > len(buffers[0])-offset {
return 0, io.ErrShortBuffer
}
sizes[0] = copy(buffers[0][offset:], packet)
return 1, nil
case <-s.closeCh:
return 0, os.ErrClosed
}
}
// Write delivers complete inbound IP packets from buffers at offset. Invalid,
// unrelated, and unsupported packets are silently discarded.
func (s *Stack) Write(buffers [][]byte, offset int) (int, error) {
if err := s.ready(); err != nil {
if errors.Is(err, ErrClosed) {
return 0, os.ErrClosed
}
return 0, err
}
count := 0
for _, buffer := range buffers {
if offset < 0 || offset > len(buffer) {
return count, errors.New("mipstack: invalid Write offset")
}
if len(buffer) == offset {
continue
}
if err := s.handlePacket(buffer[offset:]); err != nil {
return count, err
}
count++
}
return count, nil
}
// writePacket queues one complete outbound IP packet for Read.
func (s *Stack) writePacket(packet []byte) error {
select {
case <-s.closeCh:
return ErrClosed
default:
}
queue, loopback := s.outputQueue(packet)
if loopback {
select {
case queue <- packet:
s.recordOutput(true)
return nil
case <-s.closeCh:
return ErrClosed
default:
// runLoopback is the sole consumer and may itself be emitting a
// reply. Blocking it on its own full queue would deadlock all local
// traffic, so overload is reported to the producing socket.
return ErrResourceLimit
}
}
select {
case queue <- packet:
s.recordOutput(false)
return nil
case <-s.closeCh:
return ErrClosed
}