Debian Codegraph @

tcpip/stack/stack.go

raw symbols SCIP
1 // Copyright 2018 The gVisor Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package stack provides the glue between networking protocols and the
16 // consumers of the networking stack.
17 //
18 // For consumers, the only function of interest is New(), everything else is
19 // provided by the tcpip/public package.
20 package stack
21
22 import (
23 "encoding/binary"
24 "fmt"
25 "io"
26 "math/rand"
27 "sync/atomic"
28 "time"
29
30 "golang.org/x/time/rate"
31 "inet.af/netstack/atomicbitops"
32 cryptorand "inet.af/netstack/rand"
33 "inet.af/netstack/sync"
34 "inet.af/netstack/tcpip"
35 "inet.af/netstack/tcpip/buffer"
36 "inet.af/netstack/tcpip/header"
37 "inet.af/netstack/tcpip/ports"
38 "inet.af/netstack/waiter"
39 )
40
41 const (
42 // DefaultTOS is the default type of service value for network endpoints.
43 DefaultTOS = 0
44 )
45
46 type transportProtocolState struct {
47 proto TransportProtocol
48 defaultHandler func(id TransportEndpointID, pkt *PacketBuffer) bool
49 }
50
51 // ResumableEndpoint is an endpoint that needs to be resumed after restore.
52 type ResumableEndpoint interface {
53 // Resume resumes an endpoint after restore. This can be used to restart
54 // background workers such as protocol goroutines. This must be called after
55 // all indirect dependencies of the endpoint has been restored, which
56 // generally implies at the end of the restore process.
57 Resume(*Stack)
58 }
59
60 // uniqueIDGenerator is a default unique ID generator.
61 type uniqueIDGenerator atomicbitops.AlignedAtomicUint64
62
63 func (u *uniqueIDGenerator) UniqueID() uint64 {
64 return ((*atomicbitops.AlignedAtomicUint64)(u)).Add(1)
65 }
66
67 // Stack is a networking stack, with all supported protocols, NICs, and route
68 // table.
69 type Stack struct {
70 transportProtocols map[tcpip.TransportProtocolNumber]*transportProtocolState
71 networkProtocols map[tcpip.NetworkProtocolNumber]NetworkProtocol
72
73 // rawFactory creates raw endpoints. If nil, raw endpoints are
74 // disabled. It is set during Stack creation and is immutable.
75 rawFactory RawFactory
76 packetEndpointWriteSupported bool
77
78 demux *transportDemuxer
79
80 stats tcpip.Stats
81
82 // LOCK ORDERING: mu > route.mu.
83 route struct {
84 mu struct {
85 sync.RWMutex
86
87 table []tcpip.Route
88 }
89 }
90
91 mu sync.RWMutex
92 nics map[tcpip.NICID]*nic
93 defaultForwardingEnabled map[tcpip.NetworkProtocolNumber]struct{}
94
95 // cleanupEndpointsMu protects cleanupEndpoints.
96 cleanupEndpointsMu sync.Mutex
97 cleanupEndpoints map[TransportEndpoint]struct{}
98
99 *ports.PortManager
100
101 // If not nil, then any new endpoints will have this probe function
102 // invoked everytime they receive a TCP segment.
103 tcpProbeFunc atomic.Value // TCPProbeFunc
104
105 // clock is used to generate user-visible times.
106 clock tcpip.Clock
107
108 // handleLocal allows non-loopback interfaces to loop packets.
109 handleLocal bool
110
111 // tables are the iptables packet filtering and manipulation rules.
112 // TODO(gvisor.dev/issue/4595): S/R this field.
113 tables *IPTables
114
115 // resumableEndpoints is a list of endpoints that need to be resumed if the
116 // stack is being restored.
117 resumableEndpoints []ResumableEndpoint
118
119 // icmpRateLimiter is a global rate limiter for all ICMP messages generated
120 // by the stack.
121 icmpRateLimiter *ICMPRateLimiter
122
123 // seed is a one-time random value initialized at stack startup.
124 //
125 // TODO(gvisor.dev/issue/940): S/R this field.
126 seed uint32
127
128 // nudConfigs is the default NUD configurations used by interfaces.
129 nudConfigs NUDConfigurations
130
131 // nudDisp is the NUD event dispatcher that is used to send the netstack
132 // integrator NUD related events.
133 nudDisp NUDDispatcher
134
135 // uniqueIDGenerator is a generator of unique identifiers.
136 uniqueIDGenerator UniqueID
137
138 // randomGenerator is an injectable pseudo random generator that can be
139 // used when a random number is required.
140 randomGenerator *rand.Rand
141
142 // secureRNG is a cryptographically secure random number generator.
143 secureRNG io.Reader
144
145 // sendBufferSize holds the min/default/max send buffer sizes for
146 // endpoints other than TCP.
147 sendBufferSize tcpip.SendBufferSizeOption
148
149 // receiveBufferSize holds the min/default/max receive buffer sizes for
150 // endpoints other than TCP.
151 receiveBufferSize tcpip.ReceiveBufferSizeOption
152
153 // tcpInvalidRateLimit is the maximal rate for sending duplicate
154 // acknowledgements in response to incoming TCP packets that are for an existing
155 // connection but that are invalid due to any of the following reasons:
156 //
157 // a) out-of-window sequence number.
158 // b) out-of-window acknowledgement number.
159 // c) PAWS check failure (when implemented).
160 //
161 // This is required to prevent potential ACK loops.
162 // Setting this to 0 will disable all rate limiting.
163 tcpInvalidRateLimit time.Duration
164
165 // tsOffsetSecret is the secret key for generating timestamp offsets
166 // initialized at stack startup.
167 tsOffsetSecret uint32
168 }
169
170 // UniqueID is an abstract generator of unique identifiers.
171 type UniqueID interface {
172 UniqueID() uint64
173 }
174
175 // NetworkProtocolFactory instantiates a network protocol.
176 //
177 // NetworkProtocolFactory must not attempt to modify the stack, it may only
178 // query the stack.
179 type NetworkProtocolFactory func(*Stack) NetworkProtocol
180
181 // TransportProtocolFactory instantiates a transport protocol.
182 //
183 // TransportProtocolFactory must not attempt to modify the stack, it may only
184 // query the stack.
185 type TransportProtocolFactory func(*Stack) TransportProtocol
186
187 // Options contains optional Stack configuration.
188 type Options struct {
189 // NetworkProtocols lists the network protocols to enable.
190 NetworkProtocols []NetworkProtocolFactory
191
192 // TransportProtocols lists the transport protocols to enable.
193 TransportProtocols []TransportProtocolFactory
194
195 // Clock is an optional clock used for timekeeping.
196 //
197 // If Clock is nil, tcpip.NewStdClock() will be used.
198 Clock tcpip.Clock
199
200 // Stats are optional statistic counters.
201 Stats tcpip.Stats
202
203 // HandleLocal indicates whether packets destined to their source
204 // should be handled by the stack internally (true) or outside the
205 // stack (false).
206 HandleLocal bool
207
208 // UniqueID is an optional generator of unique identifiers.
209 UniqueID UniqueID
210
211 // NUDConfigs is the default NUD configurations used by interfaces.
212 NUDConfigs NUDConfigurations
213
214 // NUDDisp is the NUD event dispatcher that an integrator can provide to
215 // receive NUD related events.
216 NUDDisp NUDDispatcher
217
218 // RawFactory produces raw endpoints. Raw endpoints are enabled only if
219 // this is non-nil.
220 RawFactory RawFactory
221
222 // AllowPacketEndpointWrite determines if packet endpoints support write
223 // operations.
224 AllowPacketEndpointWrite bool
225
226 // RandSource is an optional source to use to generate random
227 // numbers. If omitted it defaults to a Source seeded by the data
228 // returned by the stack secure RNG.
229 //
230 // RandSource must be thread-safe.
231 RandSource rand.Source
232
233 // IPTables are the initial iptables rules. If nil, DefaultIPTables will be
234 // used to construct the initial iptables rules.
235 // all traffic.
236 IPTables *IPTables
237
238 // DefaultIPTables is an optional iptables rules constructor that is called
239 // if IPTables is nil. If both fields are nil, iptables will allow all
240 // traffic.
241 DefaultIPTables func(clock tcpip.Clock, rand *rand.Rand) *IPTables
242
243 // SecureRNG is a cryptographically secure random number generator.
244 SecureRNG io.Reader
245 }
246
247 // TransportEndpointInfo holds useful information about a transport endpoint
248 // which can be queried by monitoring tools.
249 //
250 // +stateify savable
251 type TransportEndpointInfo struct {
252 // The following fields are initialized at creation time and are
253 // immutable.
254
255 NetProto tcpip.NetworkProtocolNumber
256 TransProto tcpip.TransportProtocolNumber
257
258 // The following fields are protected by endpoint mu.
259
260 ID TransportEndpointID
261 // BindNICID and bindAddr are set via calls to Bind(). They are used to
262 // reject attempts to send data or connect via a different NIC or
263 // address
264 BindNICID tcpip.NICID
265 BindAddr tcpip.Address
266 // RegisterNICID is the default NICID registered as a side-effect of
267 // connect or datagram write.
268 RegisterNICID tcpip.NICID
269 }
270
271 // AddrNetProtoLocked unwraps the specified address if it is a V4-mapped V6
272 // address and returns the network protocol number to be used to communicate
273 // with the specified address. It returns an error if the passed address is
274 // incompatible with the receiver.
275 //
276 // Preconditon: the parent endpoint mu must be held while calling this method.
277 func (t *TransportEndpointInfo) AddrNetProtoLocked(addr tcpip.FullAddress, v6only bool) (tcpip.FullAddress, tcpip.NetworkProtocolNumber, tcpip.Error) {
278 netProto := t.NetProto
279 switch len(addr.Addr) {
280 case header.IPv4AddressSize:
281 netProto = header.IPv4ProtocolNumber
282 case header.IPv6AddressSize:
283 if header.IsV4MappedAddress(addr.Addr) {
284 netProto = header.IPv4ProtocolNumber
285 addr.Addr = addr.Addr[header.IPv6AddressSize-header.IPv4AddressSize:]
286 if addr.Addr == header.IPv4Any {
287 addr.Addr = ""
288 }
289 }
290 }
291
292 switch len(t.ID.LocalAddress) {
293 case header.IPv4AddressSize:
294 if len(addr.Addr) == header.IPv6AddressSize {
295 return tcpip.FullAddress{}, 0, &tcpip.ErrInvalidEndpointState{}
296 }
297 case header.IPv6AddressSize:
298 if len(addr.Addr) == header.IPv4AddressSize {
299 return tcpip.FullAddress{}, 0, &tcpip.ErrNetworkUnreachable{}
300 }
301 }
302
303 switch {
304 case netProto == t.NetProto:
305 case netProto == header.IPv4ProtocolNumber && t.NetProto == header.IPv6ProtocolNumber:
306 if v6only {
307 return tcpip.FullAddress{}, 0, &tcpip.ErrNoRoute{}
308 }
309 default:
310 return tcpip.FullAddress{}, 0, &tcpip.ErrInvalidEndpointState{}
311 }
312
313 return addr, netProto, nil
314 }
315
316 // IsEndpointInfo is an empty method to implement the tcpip.EndpointInfo
317 // marker interface.
318 func (*TransportEndpointInfo) IsEndpointInfo() {}
319
320 // New allocates a new networking stack with only the requested networking and
321 // transport protocols configured with default options.
322 //
323 // Note, NDPConfigurations will be fixed before being used by the Stack. That
324 // is, if an invalid value was provided, it will be reset to the default value.
325 //
326 // Protocol options can be changed by calling the
327 // SetNetworkProtocolOption/SetTransportProtocolOption methods provided by the
328 // stack. Please refer to individual protocol implementations as to what options
329 // are supported.
330 func New(opts Options) *Stack {
331 clock := opts.Clock
332 if clock == nil {
333 clock = tcpip.NewStdClock()
334 }
335
336 if opts.UniqueID == nil {
337 opts.UniqueID = new(uniqueIDGenerator)
338 }
339
340 if opts.SecureRNG == nil {
341 opts.SecureRNG = cryptorand.Reader
342 }
343
344 randSrc := opts.RandSource
345 if randSrc == nil {
346 var v int64
347 if err := binary.Read(opts.SecureRNG, binary.LittleEndian, &v); err != nil {
348 panic(err)
349 }
350 // Source provided by rand.NewSource is not thread-safe so
351 // we wrap it in a simple thread-safe version.
352 randSrc = &lockedRandomSource{src: rand.NewSource(v)}
353 }
354 randomGenerator := rand.New(randSrc)
355
356 if opts.IPTables == nil {
357 if opts.DefaultIPTables == nil {
358 opts.DefaultIPTables = DefaultTables
359 }
360 opts.IPTables = opts.DefaultIPTables(clock, randomGenerator)
361 }
362
363 opts.NUDConfigs.resetInvalidFields()
364
365 s := &Stack{
366 transportProtocols: make(map[tcpip.TransportProtocolNumber]*transportProtocolState),
367 networkProtocols: make(map[tcpip.NetworkProtocolNumber]NetworkProtocol),
368 nics: make(map[tcpip.NICID]*nic),
369 packetEndpointWriteSupported: opts.AllowPacketEndpointWrite,
370 defaultForwardingEnabled: make(map[tcpip.NetworkProtocolNumber]struct{}),
371 cleanupEndpoints: make(map[TransportEndpoint]struct{}),
372 PortManager: ports.NewPortManager(),
373 clock: clock,
374 stats: opts.Stats.FillIn(),
375 handleLocal: opts.HandleLocal,
376 tables: opts.IPTables,
377 icmpRateLimiter: NewICMPRateLimiter(clock),
378 seed: randomGenerator.Uint32(),
379 nudConfigs: opts.NUDConfigs,
380 uniqueIDGenerator: opts.UniqueID,
381 nudDisp: opts.NUDDisp,
382 randomGenerator: randomGenerator,
383 secureRNG: opts.SecureRNG,
384 sendBufferSize: tcpip.SendBufferSizeOption{
385 Min: MinBufferSize,
386 Default: DefaultBufferSize,
387 Max: DefaultMaxBufferSize,
388 },
389 receiveBufferSize: tcpip.ReceiveBufferSizeOption{
390 Min: MinBufferSize,
391 Default: DefaultBufferSize,
392 Max: DefaultMaxBufferSize,
393 },
394 tcpInvalidRateLimit: defaultTCPInvalidRateLimit,
395 tsOffsetSecret: randomGenerator.Uint32(),
396 }
397
398 // Add specified network protocols.
399 for _, netProtoFactory := range opts.NetworkProtocols {
400 netProto := netProtoFactory(s)
401 s.networkProtocols[netProto.Number()] = netProto
402 }
403
404 // Add specified transport protocols.
405 for _, transProtoFactory := range opts.TransportProtocols {
406 transProto := transProtoFactory(s)
407 s.transportProtocols[transProto.Number()] = &transportProtocolState{
408 proto: transProto,
409 }
410 }
411
412 // Add the factory for raw endpoints, if present.
413 s.rawFactory = opts.RawFactory
414
415 // Create the global transport demuxer.
416 s.demux = newTransportDemuxer(s)
417
418 return s
419 }
420
421 // UniqueID returns a unique identifier.
422 func (s *Stack) UniqueID() uint64 {
423 return s.uniqueIDGenerator.UniqueID()
424 }
425
426 // SetNetworkProtocolOption allows configuring individual protocol level
427 // options. This method returns an error if the protocol is not supported or
428 // option is not supported by the protocol implementation or the provided value
429 // is incorrect.
430 func (s *Stack) SetNetworkProtocolOption(network tcpip.NetworkProtocolNumber, option tcpip.SettableNetworkProtocolOption) tcpip.Error {
431 netProto, ok := s.networkProtocols[network]
432 if !ok {
433 return &tcpip.ErrUnknownProtocol{}
434 }
435 return netProto.SetOption(option)
436 }
437
438 // NetworkProtocolOption allows retrieving individual protocol level option
439 // values. This method returns an error if the protocol is not supported or
440 // option is not supported by the protocol implementation.
441 // e.g.
442 // var v ipv4.MyOption
443 // err := s.NetworkProtocolOption(tcpip.IPv4ProtocolNumber, &v)
444 // if err != nil {
445 // ...
446 // }
447 func (s *Stack) NetworkProtocolOption(network tcpip.NetworkProtocolNumber, option tcpip.GettableNetworkProtocolOption) tcpip.Error {
448 netProto, ok := s.networkProtocols[network]
449 if !ok {
450 return &tcpip.ErrUnknownProtocol{}
451 }
452 return netProto.Option(option)
453 }
454
455 // SetTransportProtocolOption allows configuring individual protocol level
456 // options. This method returns an error if the protocol is not supported or
457 // option is not supported by the protocol implementation or the provided value
458 // is incorrect.
459 func (s *Stack) SetTransportProtocolOption(transport tcpip.TransportProtocolNumber, option tcpip.SettableTransportProtocolOption) tcpip.Error {
460 transProtoState, ok := s.transportProtocols[transport]
461 if !ok {
462 return &tcpip.ErrUnknownProtocol{}
463 }
464 return transProtoState.proto.SetOption(option)
465 }
466
467 // TransportProtocolOption allows retrieving individual protocol level option
468 // values. This method returns an error if the protocol is not supported or
469 // option is not supported by the protocol implementation.
470 // var v tcp.SACKEnabled
471 // if err := s.TransportProtocolOption(tcpip.TCPProtocolNumber, &v); err != nil {
472 // ...
473 // }
474 func (s *Stack) TransportProtocolOption(transport tcpip.TransportProtocolNumber, option tcpip.GettableTransportProtocolOption) tcpip.Error {
475 transProtoState, ok := s.transportProtocols[transport]
476 if !ok {
477 return &tcpip.ErrUnknownProtocol{}
478 }
479 return transProtoState.proto.Option(option)
480 }
481
482 // SetTransportProtocolHandler sets the per-stack default handler for the given
483 // protocol.
484 //
485 // It must be called only during initialization of the stack. Changing it as the
486 // stack is operating is not supported.
487 func (s *Stack) SetTransportProtocolHandler(p tcpip.TransportProtocolNumber, h func(TransportEndpointID, *PacketBuffer) bool) {
488 state := s.transportProtocols[p]
489 if state != nil {
490 state.defaultHandler = h
491 }
492 }
493
494 // Clock returns the Stack's clock for retrieving the current time and
495 // scheduling work.
496 func (s *Stack) Clock() tcpip.Clock {
497 return s.clock
498 }
499
500 // Stats returns a mutable copy of the current stats.
501 //
502 // This is not generally exported via the public interface, but is available
503 // internally.
504 func (s *Stack) Stats() tcpip.Stats {
505 return s.stats
506 }
507
508 // SetNICForwarding enables or disables packet forwarding on the specified NIC
509 // for the passed protocol.
510 func (s *Stack) SetNICForwarding(id tcpip.NICID, protocol tcpip.NetworkProtocolNumber, enable bool) tcpip.Error {
511 s.mu.RLock()
512 defer s.mu.RUnlock()
513
514 nic, ok := s.nics[id]
515 if !ok {
516 return &tcpip.ErrUnknownNICID{}
517 }
518
519 return nic.setForwarding(protocol, enable)
520 }
521
522 // NICForwarding returns the forwarding configuration for the specified NIC.
523 func (s *Stack) NICForwarding(id tcpip.NICID, protocol tcpip.NetworkProtocolNumber) (bool, tcpip.Error) {
524 s.mu.RLock()
525 defer s.mu.RUnlock()
526
527 nic, ok := s.nics[id]
528 if !ok {
529 return false, &tcpip.ErrUnknownNICID{}
530 }
531
532 return nic.forwarding(protocol)
533 }
534
535 // SetForwardingDefaultAndAllNICs sets packet forwarding for all NICs for the
536 // passed protocol and sets the default setting for newly created NICs.
537 func (s *Stack) SetForwardingDefaultAndAllNICs(protocol tcpip.NetworkProtocolNumber, enable bool) tcpip.Error {
538 s.mu.Lock()
539 defer s.mu.Unlock()
540
541 doneOnce := false
542 for id, nic := range s.nics {
543 if err := nic.setForwarding(protocol, enable); err != nil {
544 // Expect forwarding to be settable on all interfaces if it was set on
545 // one.
546 if doneOnce {
547 panic(fmt.Sprintf("nic(id=%d).setForwarding(%d, %t): %s", id, protocol, enable, err))
548 }
549
550 return err
551 }
552
553 doneOnce = true
554 }
555
556 if enable {
557 s.defaultForwardingEnabled[protocol] = struct{}{}
558 } else {
559 delete(s.defaultForwardingEnabled, protocol)
560 }
561
562 return nil
563 }
564
565 // PortRange returns the UDP and TCP inclusive range of ephemeral ports used in
566 // both IPv4 and IPv6.
567 func (s *Stack) PortRange() (uint16, uint16) {
568 return s.PortManager.PortRange()
569 }
570
571 // SetPortRange sets the UDP and TCP IPv4 and IPv6 ephemeral port range
572 // (inclusive).
573 func (s *Stack) SetPortRange(start uint16, end uint16) tcpip.Error {
574 return s.PortManager.SetPortRange(start, end)
575 }
576
577 // SetRouteTable assigns the route table to be used by this stack. It
578 // specifies which NIC to use for given destination address ranges.
579 //
580 // This method takes ownership of the table.
581 func (s *Stack) SetRouteTable(table []tcpip.Route) {
582 s.route.mu.Lock()
583 defer s.route.mu.Unlock()
584 s.route.mu.table = table
585 }
586
587 // GetRouteTable returns the route table which is currently in use.
588 func (s *Stack) GetRouteTable() []tcpip.Route {
589 s.route.mu.RLock()
590 defer s.route.mu.RUnlock()
591 return append([]tcpip.Route(nil), s.route.mu.table...)
592 }
593
594 // AddRoute appends a route to the route table.
595 func (s *Stack) AddRoute(route tcpip.Route) {
596 s.route.mu.Lock()
597 defer s.route.mu.Unlock()
598 s.route.mu.table = append(s.route.mu.table, route)
599 }
600
601 // RemoveRoutes removes matching routes from the route table.
602 func (s *Stack) RemoveRoutes(match func(tcpip.Route) bool) {
603 s.route.mu.Lock()
604 defer s.route.mu.Unlock()
605
606 var filteredRoutes []tcpip.Route
607 for _, route := range s.route.mu.table {
608 if !match(route) {
609 filteredRoutes = append(filteredRoutes, route)
610 }
611 }
612 s.route.mu.table = filteredRoutes
613 }
614
615 // NewEndpoint creates a new transport layer endpoint of the given protocol.
616 func (s *Stack) NewEndpoint(transport tcpip.TransportProtocolNumber, network tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) {
617 t, ok := s.transportProtocols[transport]
618 if !ok {
619 return nil, &tcpip.ErrUnknownProtocol{}
620 }
621
622 return t.proto.NewEndpoint(network, waiterQueue)
623 }
624
625 // NewRawEndpoint creates a new raw transport layer endpoint of the given
626 // protocol. Raw endpoints receive all traffic for a given protocol regardless
627 // of address.
628 func (s *Stack) NewRawEndpoint(transport tcpip.TransportProtocolNumber, network tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue, associated bool) (tcpip.Endpoint, tcpip.Error) {
629 if s.rawFactory == nil {
630 return nil, &tcpip.ErrNotPermitted{}
631 }
632
633 if !associated {
634 return s.rawFactory.NewUnassociatedEndpoint(s, network, transport, waiterQueue)
635 }
636
637 t, ok := s.transportProtocols[transport]
638 if !ok {
639 return nil, &tcpip.ErrUnknownProtocol{}
640 }
641
642 return t.proto.NewRawEndpoint(network, waiterQueue)
643 }
644
645 // NewPacketEndpoint creates a new packet endpoint listening for the given
646 // netProto.
647 func (s *Stack) NewPacketEndpoint(cooked bool, netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) {
648 if s.rawFactory == nil {
649 return nil, &tcpip.ErrNotPermitted{}
650 }
651
652 return s.rawFactory.NewPacketEndpoint(s, cooked, netProto, waiterQueue)
653 }
654
655 // NICContext is an opaque pointer used to store client-supplied NIC metadata.
656 type NICContext interface{}
657
658 // NICOptions specifies the configuration of a NIC as it is being created.
659 // The zero value creates an enabled, unnamed NIC.
660 type NICOptions struct {
661 // Name specifies the name of the NIC.
662 Name string
663
664 // Disabled specifies whether to avoid calling Attach on the passed
665 // LinkEndpoint.
666 Disabled bool
667
668 // Context specifies user-defined data that will be returned in stack.NICInfo
669 // for the NIC. Clients of this library can use it to add metadata that
670 // should be tracked alongside a NIC, to avoid having to keep a
671 // map[tcpip.NICID]metadata mirroring stack.Stack's nic map.
672 Context NICContext
673 }
674
675 // CreateNICWithOptions creates a NIC with the provided id, LinkEndpoint, and
676 // NICOptions. See the documentation on type NICOptions for details on how
677 // NICs can be configured.
678 //
679 // LinkEndpoint.Attach will be called to bind ep with a NetworkDispatcher.
680 func (s *Stack) CreateNICWithOptions(id tcpip.NICID, ep LinkEndpoint, opts NICOptions) tcpip.Error {
681 s.mu.Lock()
682 defer s.mu.Unlock()
683
684 // Make sure id is unique.
685 if _, ok := s.nics[id]; ok {
686 return &tcpip.ErrDuplicateNICID{}
687 }
688
689 // Make sure name is unique, unless unnamed.
690 if opts.Name != "" {
691 for _, n := range s.nics {
692 if n.Name() == opts.Name {
693 return &tcpip.ErrDuplicateNICID{}
694 }
695 }
696 }
697
698 n := newNIC(s, id, opts.Name, ep, opts.Context)
699 for proto := range s.defaultForwardingEnabled {
700 if err := n.setForwarding(proto, true); err != nil {
701 panic(fmt.Sprintf("newNIC(%d, ...).setForwarding(%d, true): %s", id, proto, err))
702 }
703 }
704 s.nics[id] = n
705 if !opts.Disabled {
706 return n.enable()
707 }
708
709 return nil
710 }
711
712 // CreateNIC creates a NIC with the provided id and LinkEndpoint and calls
713 // LinkEndpoint.Attach to bind ep with a NetworkDispatcher.
714 func (s *Stack) CreateNIC(id tcpip.NICID, ep LinkEndpoint) tcpip.Error {
715 return s.CreateNICWithOptions(id, ep, NICOptions{})
716 }
717
718 // GetLinkEndpointByName gets the link endpoint specified by name.
719 func (s *Stack) GetLinkEndpointByName(name string) LinkEndpoint {
720 s.mu.RLock()
721 defer s.mu.RUnlock()
722 for _, nic := range s.nics {
723 if nic.Name() == name {
724 return nic.LinkEndpoint
725 }
726 }
727 return nil
728 }
729
730 // EnableNIC enables the given NIC so that the link-layer endpoint can start
731 // delivering packets to it.
732 func (s *Stack) EnableNIC(id tcpip.NICID) tcpip.Error {
733 s.mu.RLock()
734 defer s.mu.RUnlock()
735
736 nic, ok := s.nics[id]
737 if !ok {
738 return &tcpip.ErrUnknownNICID{}
739 }
740
741 return nic.enable()
742 }
743
744 // DisableNIC disables the given NIC.
745 func (s *Stack) DisableNIC(id tcpip.NICID) tcpip.Error {
746 s.mu.RLock()
747 defer s.mu.RUnlock()
748
749 nic, ok := s.nics[id]
750 if !ok {
751 return &tcpip.ErrUnknownNICID{}
752 }
753
754 nic.disable()
755 return nil
756 }
757
758 // CheckNIC checks if a NIC is usable.
759 func (s *Stack) CheckNIC(id tcpip.NICID) bool {
760 s.mu.RLock()
761 defer s.mu.RUnlock()
762
763 nic, ok := s.nics[id]
764 if !ok {
765 return false
766 }
767
768 return nic.Enabled()
769 }
770
771 // RemoveNIC removes NIC and all related routes from the network stack.
772 func (s *Stack) RemoveNIC(id tcpip.NICID) tcpip.Error {
773 s.mu.Lock()
774 defer s.mu.Unlock()
775
776 return s.removeNICLocked(id)
777 }
778
779 // removeNICLocked removes NIC and all related routes from the network stack.
780 //
781 // s.mu must be locked.
782 func (s *Stack) removeNICLocked(id tcpip.NICID) tcpip.Error {
783 nic, ok := s.nics[id]
784 if !ok {
785 return &tcpip.ErrUnknownNICID{}
786 }
787 if nic.IsLoopback() {
788 return &tcpip.ErrNotSupported{}
789 }
790 delete(s.nics, id)
791
792 // Remove routes in-place. n tracks the number of routes written.
793 s.route.mu.Lock()
794 n := 0
795 for i, r := range s.route.mu.table {
796 s.route.mu.table[i] = tcpip.Route{}
797 if r.NIC != id {
798 // Keep this route.
799 s.route.mu.table[n] = r
800 n++
801 }
802 }
803 s.route.mu.table = s.route.mu.table[:n]
804 s.route.mu.Unlock()
805
806 return nic.remove()
807 }
808
809 // NICInfo captures the name and addresses assigned to a NIC.
810 type NICInfo struct {
811 Name string
812 LinkAddress tcpip.LinkAddress
813 ProtocolAddresses []tcpip.ProtocolAddress
814
815 // Flags indicate the state of the NIC.
816 Flags NICStateFlags
817
818 // MTU is the maximum transmission unit.
819 MTU uint32
820
821 Stats tcpip.NICStats
822
823 // NetworkStats holds the stats of each NetworkEndpoint bound to the NIC.
824 NetworkStats map[tcpip.NetworkProtocolNumber]NetworkEndpointStats
825
826 // Context is user-supplied data optionally supplied in CreateNICWithOptions.
827 // See type NICOptions for more details.
828 Context NICContext
829
830 // ARPHardwareType holds the ARP Hardware type of the NIC. This is the
831 // value sent in haType field of an ARP Request sent by this NIC and the
832 // value expected in the haType field of an ARP response.
833 ARPHardwareType header.ARPHardwareType
834
835 // Forwarding holds the forwarding status for each network endpoint that
836 // supports forwarding.
837 Forwarding map[tcpip.NetworkProtocolNumber]bool
838 }
839
840 // HasNIC returns true if the NICID is defined in the stack.
841 func (s *Stack) HasNIC(id tcpip.NICID) bool {
842 s.mu.RLock()
843 _, ok := s.nics[id]
844 s.mu.RUnlock()
845 return ok
846 }
847
848 // NICInfo returns a map of NICIDs to their associated information.
849 func (s *Stack) NICInfo() map[tcpip.NICID]NICInfo {
850 s.mu.RLock()
851 defer s.mu.RUnlock()
852
853 nics := make(map[tcpip.NICID]NICInfo)
854 for id, nic := range s.nics {
855 flags := NICStateFlags{
856 Up: true, // Netstack interfaces are always up.
857 Running: nic.Enabled(),
858 Promiscuous: nic.Promiscuous(),
859 Loopback: nic.IsLoopback(),
860 }
861
862 netStats := make(map[tcpip.NetworkProtocolNumber]NetworkEndpointStats)
863 for proto, netEP := range nic.networkEndpoints {
864 netStats[proto] = netEP.Stats()
865 }
866
867 info := NICInfo{
868 Name: nic.name,
869 LinkAddress: nic.LinkEndpoint.LinkAddress(),
870 ProtocolAddresses: nic.primaryAddresses(),
871 Flags: flags,
872 MTU: nic.LinkEndpoint.MTU(),
873 Stats: nic.stats.local,
874 NetworkStats: netStats,
875 Context: nic.context,
876 ARPHardwareType: nic.LinkEndpoint.ARPHardwareType(),
877 Forwarding: make(map[tcpip.NetworkProtocolNumber]bool),
878 }
879
880 for proto := range s.networkProtocols {
881 switch forwarding, err := nic.forwarding(proto); err.(type) {
882 case nil:
883 info.Forwarding[proto] = forwarding
884 case *tcpip.ErrUnknownProtocol:
885 panic(fmt.Sprintf("expected network protocol %d to be available on NIC %d", proto, nic.ID()))
886 case *tcpip.ErrNotSupported:
887 // Not all network protocols support forwarding.
888 default:
889 panic(fmt.Sprintf("nic(id=%d).forwarding(%d): %s", nic.ID(), proto, err))
890 }
891 }
892
893 nics[id] = info
894 }
895 return nics
896 }
897
898 // NICStateFlags holds information about the state of an NIC.
899 type NICStateFlags struct {
900 // Up indicates whether the interface is running.
901 Up bool
902
903 // Running indicates whether resources are allocated.
904 Running bool
905
906 // Promiscuous indicates whether the interface is in promiscuous mode.
907 Promiscuous bool
908
909 // Loopback indicates whether the interface is a loopback.
910 Loopback bool
911 }
912
913 // AddProtocolAddress adds an address to the specified NIC, possibly with extra
914 // properties.
915 func (s *Stack) AddProtocolAddress(id tcpip.NICID, protocolAddress tcpip.ProtocolAddress, properties AddressProperties) tcpip.Error {
916 s.mu.RLock()
917 defer s.mu.RUnlock()
918
919 nic, ok := s.nics[id]
920 if !ok {
921 return &tcpip.ErrUnknownNICID{}
922 }
923
924 return nic.addAddress(protocolAddress, properties)
925 }
926
927 // RemoveAddress removes an existing network-layer address from the specified
928 // NIC.
929 func (s *Stack) RemoveAddress(id tcpip.NICID, addr tcpip.Address) tcpip.Error {
930 s.mu.RLock()
931 defer s.mu.RUnlock()
932
933 if nic, ok := s.nics[id]; ok {
934 return nic.removeAddress(addr)
935 }
936
937 return &tcpip.ErrUnknownNICID{}
938 }
939
940 // AllAddresses returns a map of NICIDs to their protocol addresses (primary
941 // and non-primary).
942 func (s *Stack) AllAddresses() map[tcpip.NICID][]tcpip.ProtocolAddress {
943 s.mu.RLock()
944 defer s.mu.RUnlock()
945
946 nics := make(map[tcpip.NICID][]tcpip.ProtocolAddress)
947 for id, nic := range s.nics {
948 nics[id] = nic.allPermanentAddresses()
949 }
950 return nics
951 }
952
953 // GetMainNICAddress returns the first non-deprecated primary address and prefix
954 // for the given NIC and protocol. If no non-deprecated primary addresses exist,
955 // a deprecated address will be returned. If no deprecated addresses exist, the
956 // zero value will be returned.
957 func (s *Stack) GetMainNICAddress(id tcpip.NICID, protocol tcpip.NetworkProtocolNumber) (tcpip.AddressWithPrefix, tcpip.Error) {
958 s.mu.RLock()
959 defer s.mu.RUnlock()
960
961 nic, ok := s.nics[id]
962 if !ok {
963 return tcpip.AddressWithPrefix{}, &tcpip.ErrUnknownNICID{}
964 }
965
966 return nic.PrimaryAddress(protocol)
967 }
968
969 func (s *Stack) getAddressEP(nic *nic, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber) AssignableAddressEndpoint {
970 if len(localAddr) == 0 {
971 return nic.primaryEndpoint(netProto, remoteAddr)
972 }
973 return nic.findEndpoint(netProto, localAddr, CanBePrimaryEndpoint)
974 }
975
976 // findLocalRouteFromNICRLocked is like findLocalRouteRLocked but finds a route
977 // from the specified NIC.
978 //
979 // Precondition: s.mu must be read locked.
980 func (s *Stack) findLocalRouteFromNICRLocked(localAddressNIC *nic, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber) *Route {
981 localAddressEndpoint := localAddressNIC.getAddressOrCreateTempInner(netProto, localAddr, false /* createTemp */, NeverPrimaryEndpoint)
982 if localAddressEndpoint == nil {
983 return nil
984 }
985
986 var outgoingNIC *nic
987 // Prefer a local route to the same interface as the local address.
988 if localAddressNIC.hasAddress(netProto, remoteAddr) {
989 outgoingNIC = localAddressNIC
990 }
991
992 // If the remote address isn't owned by the local address's NIC, check all
993 // NICs.
994 if outgoingNIC == nil {
995 for _, nic := range s.nics {
996 if nic.hasAddress(netProto, remoteAddr) {
997 outgoingNIC = nic
998 break
999 }
1000 }
1001 }
1002
1003 // If the remote address is not owned by the stack, we can't return a local
1004 // route.
1005 if outgoingNIC == nil {
1006 localAddressEndpoint.DecRef()
1007 return nil
1008 }
1009
1010 r := makeLocalRoute(
1011 netProto,
1012 localAddr,
1013 remoteAddr,
1014 outgoingNIC,
1015 localAddressNIC,
1016 localAddressEndpoint,
1017 )
1018
1019 if r.IsOutboundBroadcast() {
1020 r.Release()
1021 return nil
1022 }
1023
1024 return r
1025 }
1026
1027 // findLocalRouteRLocked returns a local route.
1028 //
1029 // A local route is a route to some remote address which the stack owns. That
1030 // is, a local route is a route where packets never have to leave the stack.
1031 //
1032 // Precondition: s.mu must be read locked.
1033 func (s *Stack) findLocalRouteRLocked(localAddressNICID tcpip.NICID, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber) *Route {
1034 if len(localAddr) == 0 {
1035 localAddr = remoteAddr
1036 }
1037
1038 if localAddressNICID == 0 {
1039 for _, localAddressNIC := range s.nics {
1040 if r := s.findLocalRouteFromNICRLocked(localAddressNIC, localAddr, remoteAddr, netProto); r != nil {
1041 return r
1042 }
1043 }
1044
1045 return nil
1046 }
1047
1048 if localAddressNIC, ok := s.nics[localAddressNICID]; ok {
1049 return s.findLocalRouteFromNICRLocked(localAddressNIC, localAddr, remoteAddr, netProto)
1050 }
1051
1052 return nil
1053 }
1054
1055 // HandleLocal returns true if non-loopback interfaces are allowed to loop packets.
1056 func (s *Stack) HandleLocal() bool {
1057 return s.handleLocal
1058 }
1059
1060 func isNICForwarding(nic *nic, proto tcpip.NetworkProtocolNumber) bool {
1061 switch forwarding, err := nic.forwarding(proto); err.(type) {
1062 case nil:
1063 return forwarding
1064 case *tcpip.ErrUnknownProtocol:
1065 panic(fmt.Sprintf("expected network protocol %d to be available on NIC %d", proto, nic.ID()))
1066 case *tcpip.ErrNotSupported:
1067 // Not all network protocols support forwarding.
1068 return false
1069 default:
1070 panic(fmt.Sprintf("nic(id=%d).forwarding(%d): %s", nic.ID(), proto, err))
1071 }
1072 }
1073
1074 // FindRoute creates a route to the given destination address, leaving through
1075 // the given NIC and local address (if provided).
1076 //
1077 // If a NIC is not specified, the returned route will leave through the same
1078 // NIC as the NIC that has the local address assigned when forwarding is
1079 // disabled. If forwarding is enabled and the NIC is unspecified, the route may
1080 // leave through any interface unless the route is link-local.
1081 //
1082 // If no local address is provided, the stack will select a local address. If no
1083 // remote address is provided, the stack wil use a remote address equal to the
1084 // local address.
1085 func (s *Stack) FindRoute(id tcpip.NICID, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber, multicastLoop bool) (*Route, tcpip.Error) {
1086 s.mu.RLock()
1087 defer s.mu.RUnlock()
1088
1089 isLinkLocal := header.IsV6LinkLocalUnicastAddress(remoteAddr) || header.IsV6LinkLocalMulticastAddress(remoteAddr)
1090 isLocalBroadcast := remoteAddr == header.IPv4Broadcast
1091 isMulticast := header.IsV4MulticastAddress(remoteAddr) || header.IsV6MulticastAddress(remoteAddr)
1092 isLoopback := header.IsV4LoopbackAddress(remoteAddr) || header.IsV6LoopbackAddress(remoteAddr)
1093 needRoute := !(isLocalBroadcast || isMulticast || isLinkLocal || isLoopback)
1094
1095 if s.handleLocal && !isMulticast && !isLocalBroadcast {
1096 if r := s.findLocalRouteRLocked(id, localAddr, remoteAddr, netProto); r != nil {
1097 return r, nil
1098 }
1099 }
1100
1101 // If the interface is specified and we do not need a route, return a route
1102 // through the interface if the interface is valid and enabled.
1103 if id != 0 && !needRoute {
1104 if nic, ok := s.nics[id]; ok && nic.Enabled() {
1105 if addressEndpoint := s.getAddressEP(nic, localAddr, remoteAddr, netProto); addressEndpoint != nil {
1106 return makeRoute(
1107 netProto,
1108 "", /* gateway */
1109 localAddr,
1110 remoteAddr,
1111 nic, /* outboundNIC */
1112 nic, /* localAddressNIC*/
1113 addressEndpoint,
1114 s.handleLocal,
1115 multicastLoop,
1116 ), nil
1117 }
1118 }
1119
1120 if isLoopback {
1121 return nil, &tcpip.ErrBadLocalAddress{}
1122 }
1123 return nil, &tcpip.ErrNetworkUnreachable{}
1124 }
1125
1126 onlyGlobalAddresses := !header.IsV6LinkLocalUnicastAddress(localAddr) && !isLinkLocal
1127
1128 // Find a route to the remote with the route table.
1129 var chosenRoute tcpip.Route
1130 if r := func() *Route {
1131 s.route.mu.RLock()
1132 defer s.route.mu.RUnlock()
1133
1134 for _, route := range s.route.mu.table {
1135 if len(remoteAddr) != 0 && !route.Destination.Contains(remoteAddr) {
1136 continue
1137 }
1138
1139 nic, ok := s.nics[route.NIC]
1140 if !ok || !nic.Enabled() {
1141 continue
1142 }
1143
1144 if id == 0 || id == route.NIC {
1145 if addressEndpoint := s.getAddressEP(nic, localAddr, remoteAddr, netProto); addressEndpoint != nil {
1146 var gateway tcpip.Address
1147 if needRoute {
1148 gateway = route.Gateway
1149 }
1150 r := constructAndValidateRoute(netProto, addressEndpoint, nic /* outgoingNIC */, nic /* outgoingNIC */, gateway, localAddr, remoteAddr, s.handleLocal, multicastLoop)
1151 if r == nil {
1152 panic(fmt.Sprintf("non-forwarding route validation failed with route table entry = %#v, id = %d, localAddr = %s, remoteAddr = %s", route, id, localAddr, remoteAddr))
1153 }
1154 return r
1155 }
1156 }
1157
1158 // If the stack has forwarding enabled and we haven't found a valid route
1159 // to the remote address yet, keep track of the first valid route. We
1160 // keep iterating because we prefer routes that let us use a local
1161 // address that is assigned to the outgoing interface. There is no
1162 // requirement to do this from any RFC but simply a choice made to better
1163 // follow a strong host model which the netstack follows at the time of
1164 // writing.
1165 if onlyGlobalAddresses && chosenRoute == (tcpip.Route{}) && isNICForwarding(nic, netProto) {
1166 chosenRoute = route
1167 }
1168 }
1169
1170 return nil
1171 }(); r != nil {
1172 return r, nil
1173 }
1174
1175 if chosenRoute != (tcpip.Route{}) {
1176 // At this point we know the stack has forwarding enabled since chosenRoute is
1177 // only set when forwarding is enabled.
1178 nic, ok := s.nics[chosenRoute.NIC]
1179 if !ok {
1180 // If the route's NIC was invalid, we should not have chosen the route.
1181 panic(fmt.Sprintf("chosen route must have a valid NIC with ID = %d", chosenRoute.NIC))
1182 }
1183
1184 var gateway tcpip.Address
1185 if needRoute {
1186 gateway = chosenRoute.Gateway
1187 }
1188
1189 // Use the specified NIC to get the local address endpoint.
1190 if id != 0 {
1191 if aNIC, ok := s.nics[id]; ok {
1192 if addressEndpoint := s.getAddressEP(aNIC, localAddr, remoteAddr, netProto); addressEndpoint != nil {
1193 if r := constructAndValidateRoute(netProto, addressEndpoint, aNIC /* localAddressNIC */, nic /* outgoingNIC */, gateway, localAddr, remoteAddr, s.handleLocal, multicastLoop); r != nil {
1194 return r, nil
1195 }
1196 }
1197 }
1198
1199 return nil, &tcpip.ErrNoRoute{}
1200 }
1201
1202 if id == 0 {
1203 // If an interface is not specified, try to find a NIC that holds the local
1204 // address endpoint to construct a route.
1205 for _, aNIC := range s.nics {
1206 addressEndpoint := s.getAddressEP(aNIC, localAddr, remoteAddr, netProto)
1207 if addressEndpoint == nil {
1208 continue
1209 }
1210
1211 if r := constructAndValidateRoute(netProto, addressEndpoint, aNIC /* localAddressNIC */, nic /* outgoingNIC */, gateway, localAddr, remoteAddr, s.handleLocal, multicastLoop); r != nil {
1212 return r, nil
1213 }
1214 }
1215 }
1216 }
1217
1218 if needRoute {
1219 return nil, &tcpip.ErrNoRoute{}
1220 }
1221 if header.IsV6LoopbackAddress(remoteAddr) {
1222 return nil, &tcpip.ErrBadLocalAddress{}
1223 }
1224 return nil, &tcpip.ErrNetworkUnreachable{}
1225 }
1226
1227 // CheckNetworkProtocol checks if a given network protocol is enabled in the
1228 // stack.
1229 func (s *Stack) CheckNetworkProtocol(protocol tcpip.NetworkProtocolNumber) bool {
1230 _, ok := s.networkProtocols[protocol]
1231 return ok
1232 }
1233
1234 // CheckDuplicateAddress performs duplicate address detection for the address on
1235 // the specified interface.
1236 func (s *Stack) CheckDuplicateAddress(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address, h DADCompletionHandler) (DADCheckAddressDisposition, tcpip.Error) {
1237 nic, ok := s.nics[nicID]
1238 if !ok {
1239 return 0, &tcpip.ErrUnknownNICID{}
1240 }
1241
1242 return nic.checkDuplicateAddress(protocol, addr, h)
1243 }
1244
1245 // CheckLocalAddress determines if the given local address exists, and if it
1246 // does, returns the id of the NIC it's bound to. Returns 0 if the address
1247 // does not exist.
1248 func (s *Stack) CheckLocalAddress(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) tcpip.NICID {
1249 s.mu.RLock()
1250 defer s.mu.RUnlock()
1251
1252 // If a NIC is specified, we try to find the address there only.
1253 if nicID != 0 {
1254 nic, ok := s.nics[nicID]
1255 if !ok {
1256 return 0
1257 }
1258
1259 if nic.CheckLocalAddress(protocol, addr) {
1260 return nic.id
1261 }
1262
1263 return 0
1264 }
1265
1266 // Go through all the NICs.
1267 for _, nic := range s.nics {
1268 if nic.CheckLocalAddress(protocol, addr) {
1269 return nic.id
1270 }
1271 }
1272
1273 return 0
1274 }
1275
1276 // SetPromiscuousMode enables or disables promiscuous mode in the given NIC.
1277 func (s *Stack) SetPromiscuousMode(nicID tcpip.NICID, enable bool) tcpip.Error {
1278 s.mu.RLock()
1279 defer s.mu.RUnlock()
1280
1281 nic, ok := s.nics[nicID]
1282 if !ok {
1283 return &tcpip.ErrUnknownNICID{}
1284 }
1285
1286 nic.setPromiscuousMode(enable)
1287
1288 return nil
1289 }
1290
1291 // SetSpoofing enables or disables address spoofing in the given NIC, allowing
1292 // endpoints to bind to any address in the NIC.
1293 func (s *Stack) SetSpoofing(nicID tcpip.NICID, enable bool) tcpip.Error {
1294 s.mu.RLock()
1295 defer s.mu.RUnlock()
1296
1297 nic, ok := s.nics[nicID]
1298 if !ok {
1299 return &tcpip.ErrUnknownNICID{}
1300 }
1301
1302 nic.setSpoofing(enable)
1303
1304 return nil
1305 }
1306
1307 // LinkResolutionResult is the result of a link address resolution attempt.
1308 type LinkResolutionResult struct {
1309 LinkAddress tcpip.LinkAddress
1310 Err tcpip.Error
1311 }
1312
1313 // GetLinkAddress finds the link address corresponding to a network address.
1314 //
1315 // Returns ErrNotSupported if the stack is not configured with a link address
1316 // resolver for the specified network protocol.
1317 //
1318 // Returns ErrWouldBlock if the link address is not readily available, along
1319 // with a notification channel for the caller to block on. Triggers address
1320 // resolution asynchronously.
1321 //
1322 // onResolve will be called either immediately, if resolution is not required,
1323 // or when address resolution is complete, with the resolved link address and
1324 // whether resolution succeeded.
1325 //
1326 // If specified, the local address must be an address local to the interface
1327 // the neighbor cache belongs to. The local address is the source address of
1328 // a packet prompting NUD/link address resolution.
1329 func (s *Stack) GetLinkAddress(nicID tcpip.NICID, addr, localAddr tcpip.Address, protocol tcpip.NetworkProtocolNumber, onResolve func(LinkResolutionResult)) tcpip.Error {
1330 s.mu.RLock()
1331 nic, ok := s.nics[nicID]
1332 s.mu.RUnlock()
1333 if !ok {
1334 return &tcpip.ErrUnknownNICID{}
1335 }
1336
1337 return nic.getLinkAddress(addr, localAddr, protocol, onResolve)
1338 }
1339
1340 // Neighbors returns all IP to MAC address associations.
1341 func (s *Stack) Neighbors(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber) ([]NeighborEntry, tcpip.Error) {
1342 s.mu.RLock()
1343 nic, ok := s.nics[nicID]
1344 s.mu.RUnlock()
1345
1346 if !ok {
1347 return nil, &tcpip.ErrUnknownNICID{}
1348 }
1349
1350 return nic.neighbors(protocol)
1351 }
1352
1353 // AddStaticNeighbor statically associates an IP address to a MAC address.
1354 func (s *Stack) AddStaticNeighbor(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address, linkAddr tcpip.LinkAddress) tcpip.Error {
1355 s.mu.RLock()
1356 nic, ok := s.nics[nicID]
1357 s.mu.RUnlock()
1358
1359 if !ok {
1360 return &tcpip.ErrUnknownNICID{}
1361 }
1362
1363 return nic.addStaticNeighbor(addr, protocol, linkAddr)
1364 }
1365
1366 // RemoveNeighbor removes an IP to MAC address association previously created
1367 // either automically or by AddStaticNeighbor. Returns ErrBadAddress if there
1368 // is no association with the provided address.
1369 func (s *Stack) RemoveNeighbor(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) tcpip.Error {
1370 s.mu.RLock()
1371 nic, ok := s.nics[nicID]
1372 s.mu.RUnlock()
1373
1374 if !ok {
1375 return &tcpip.ErrUnknownNICID{}
1376 }
1377
1378 return nic.removeNeighbor(protocol, addr)
1379 }
1380
1381 // ClearNeighbors removes all IP to MAC address associations.
1382 func (s *Stack) ClearNeighbors(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber) tcpip.Error {
1383 s.mu.RLock()
1384 nic, ok := s.nics[nicID]
1385 s.mu.RUnlock()
1386
1387 if !ok {
1388 return &tcpip.ErrUnknownNICID{}
1389 }
1390
1391 return nic.clearNeighbors(protocol)
1392 }
1393
1394 // RegisterTransportEndpoint registers the given endpoint with the stack
1395 // transport dispatcher. Received packets that match the provided id will be
1396 // delivered to the given endpoint; specifying a nic is optional, but
1397 // nic-specific IDs have precedence over global ones.
1398 func (s *Stack) RegisterTransportEndpoint(netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, ep TransportEndpoint, flags ports.Flags, bindToDevice tcpip.NICID) tcpip.Error {
1399 return s.demux.registerEndpoint(netProtos, protocol, id, ep, flags, bindToDevice)
1400 }
1401
1402 // CheckRegisterTransportEndpoint checks if an endpoint can be registered with
1403 // the stack transport dispatcher.
1404 func (s *Stack) CheckRegisterTransportEndpoint(netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, flags ports.Flags, bindToDevice tcpip.NICID) tcpip.Error {
1405 return s.demux.checkEndpoint(netProtos, protocol, id, flags, bindToDevice)
1406 }
1407
1408 // UnregisterTransportEndpoint removes the endpoint with the given id from the
1409 // stack transport dispatcher.
1410 func (s *Stack) UnregisterTransportEndpoint(netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, ep TransportEndpoint, flags ports.Flags, bindToDevice tcpip.NICID) {
1411 s.demux.unregisterEndpoint(netProtos, protocol, id, ep, flags, bindToDevice)
1412 }
1413
1414 // StartTransportEndpointCleanup removes the endpoint with the given id from
1415 // the stack transport dispatcher. It also transitions it to the cleanup stage.
1416 func (s *Stack) StartTransportEndpointCleanup(netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, ep TransportEndpoint, flags ports.Flags, bindToDevice tcpip.NICID) {
1417 s.cleanupEndpointsMu.Lock()
1418 s.cleanupEndpoints[ep] = struct{}{}
1419 s.cleanupEndpointsMu.Unlock()
1420
1421 s.demux.unregisterEndpoint(netProtos, protocol, id, ep, flags, bindToDevice)
1422 }
1423
1424 // CompleteTransportEndpointCleanup removes the endpoint from the cleanup
1425 // stage.
1426 func (s *Stack) CompleteTransportEndpointCleanup(ep TransportEndpoint) {
1427 s.cleanupEndpointsMu.Lock()
1428 delete(s.cleanupEndpoints, ep)
1429 s.cleanupEndpointsMu.Unlock()
1430 }
1431
1432 // FindTransportEndpoint finds an endpoint that most closely matches the provided
1433 // id. If no endpoint is found it returns nil.
1434 func (s *Stack) FindTransportEndpoint(netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, id TransportEndpointID, nicID tcpip.NICID) TransportEndpoint {
1435 return s.demux.findTransportEndpoint(netProto, transProto, id, nicID)
1436 }
1437
1438 // RegisterRawTransportEndpoint registers the given endpoint with the stack
1439 // transport dispatcher. Received packets that match the provided transport
1440 // protocol will be delivered to the given endpoint.
1441 func (s *Stack) RegisterRawTransportEndpoint(netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, ep RawTransportEndpoint) tcpip.Error {
1442 return s.demux.registerRawEndpoint(netProto, transProto, ep)
1443 }
1444
1445 // UnregisterRawTransportEndpoint removes the endpoint for the transport
1446 // protocol from the stack transport dispatcher.
1447 func (s *Stack) UnregisterRawTransportEndpoint(netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, ep RawTransportEndpoint) {
1448 s.demux.unregisterRawEndpoint(netProto, transProto, ep)
1449 }
1450
1451 // RegisterRestoredEndpoint records e as an endpoint that has been restored on
1452 // this stack.
1453 func (s *Stack) RegisterRestoredEndpoint(e ResumableEndpoint) {
1454 s.mu.Lock()
1455 s.resumableEndpoints = append(s.resumableEndpoints, e)
1456 s.mu.Unlock()
1457 }
1458
1459 // RegisteredEndpoints returns all endpoints which are currently registered.
1460 func (s *Stack) RegisteredEndpoints() []TransportEndpoint {
1461 s.mu.Lock()
1462 defer s.mu.Unlock()
1463 var es []TransportEndpoint
1464 for _, e := range s.demux.protocol {
1465 es = append(es, e.transportEndpoints()...)
1466 }
1467 return es
1468 }
1469
1470 // CleanupEndpoints returns endpoints currently in the cleanup state.
1471 func (s *Stack) CleanupEndpoints() []TransportEndpoint {
1472 s.cleanupEndpointsMu.Lock()
1473 es := make([]TransportEndpoint, 0, len(s.cleanupEndpoints))
1474 for e := range s.cleanupEndpoints {
1475 es = append(es, e)
1476 }
1477 s.cleanupEndpointsMu.Unlock()
1478 return es
1479 }
1480
1481 // RestoreCleanupEndpoints adds endpoints to cleanup tracking. This is useful
1482 // for restoring a stack after a save.
1483 func (s *Stack) RestoreCleanupEndpoints(es []TransportEndpoint) {
1484 s.cleanupEndpointsMu.Lock()
1485 for _, e := range es {
1486 s.cleanupEndpoints[e] = struct{}{}
1487 }
1488 s.cleanupEndpointsMu.Unlock()
1489 }
1490
1491 // Close closes all currently registered transport endpoints.
1492 //
1493 // Endpoints created or modified during this call may not get closed.
1494 func (s *Stack) Close() {
1495 for _, e := range s.RegisteredEndpoints() {
1496 e.Abort()
1497 }
1498 for _, p := range s.transportProtocols {
1499 p.proto.Close()
1500 }
1501 for _, p := range s.networkProtocols {
1502 p.Close()
1503 }
1504 }
1505
1506 // Wait waits for all transport and link endpoints to halt their worker
1507 // goroutines.
1508 //
1509 // Endpoints created or modified during this call may not get waited on.
1510 //
1511 // Note that link endpoints must be stopped via an implementation specific
1512 // mechanism.
1513 func (s *Stack) Wait() {
1514 for _, e := range s.RegisteredEndpoints() {
1515 e.Wait()
1516 }
1517 for _, e := range s.CleanupEndpoints() {
1518 e.Wait()
1519 }
1520 for _, p := range s.transportProtocols {
1521 p.proto.Wait()
1522 }
1523 for _, p := range s.networkProtocols {
1524 p.Wait()
1525 }
1526
1527 s.mu.RLock()
1528 defer s.mu.RUnlock()
1529 for _, n := range s.nics {
1530 n.LinkEndpoint.Wait()
1531 }
1532 }
1533
1534 // Resume restarts the stack after a restore. This must be called after the
1535 // entire system has been restored.
1536 func (s *Stack) Resume() {
1537 // ResumableEndpoint.Resume() may call other methods on s, so we can't hold
1538 // s.mu while resuming the endpoints.
1539 s.mu.Lock()
1540 eps := s.resumableEndpoints
1541 s.resumableEndpoints = nil
1542 s.mu.Unlock()
1543 for _, e := range eps {
1544 e.Resume(s)
1545 }
1546 }
1547
1548 // RegisterPacketEndpoint registers ep with the stack, causing it to receive
1549 // all traffic of the specified netProto on the given NIC. If nicID is 0, it
1550 // receives traffic from every NIC.
1551 func (s *Stack) RegisterPacketEndpoint(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, ep PacketEndpoint) tcpip.Error {
1552 s.mu.Lock()
1553 defer s.mu.Unlock()
1554
1555 // If no NIC is specified, capture on all devices.
1556 if nicID == 0 {
1557 // Register with each NIC.
1558 for _, nic := range s.nics {
1559 if err := nic.registerPacketEndpoint(netProto, ep); err != nil {
1560 s.unregisterPacketEndpointLocked(0, netProto, ep)
1561 return err
1562 }
1563 }
1564 return nil
1565 }
1566
1567 // Capture on a specific device.
1568 nic, ok := s.nics[nicID]
1569 if !ok {
1570 return &tcpip.ErrUnknownNICID{}
1571 }
1572 if err := nic.registerPacketEndpoint(netProto, ep); err != nil {
1573 return err
1574 }
1575
1576 return nil
1577 }
1578
1579 // UnregisterPacketEndpoint unregisters ep for packets of the specified
1580 // netProto from the specified NIC. If nicID is 0, ep is unregistered from all
1581 // NICs.
1582 func (s *Stack) UnregisterPacketEndpoint(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, ep PacketEndpoint) {
1583 s.mu.Lock()
1584 defer s.mu.Unlock()
1585 s.unregisterPacketEndpointLocked(nicID, netProto, ep)
1586 }
1587
1588 func (s *Stack) unregisterPacketEndpointLocked(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, ep PacketEndpoint) {
1589 // If no NIC is specified, unregister on all devices.
1590 if nicID == 0 {
1591 // Unregister with each NIC.
1592 for _, nic := range s.nics {
1593 nic.unregisterPacketEndpoint(netProto, ep)
1594 }
1595 return
1596 }
1597
1598 // Unregister in a single device.
1599 nic, ok := s.nics[nicID]
1600 if !ok {
1601 return
1602 }
1603 nic.unregisterPacketEndpoint(netProto, ep)
1604 }
1605
1606 // WritePacketToRemote writes a payload on the specified NIC using the provided
1607 // network protocol and remote link address.
1608 func (s *Stack) WritePacketToRemote(nicID tcpip.NICID, remote tcpip.LinkAddress, netProto tcpip.NetworkProtocolNumber, payload buffer.VectorisedView) tcpip.Error {
1609 s.mu.Lock()
1610 nic, ok := s.nics[nicID]
1611 s.mu.Unlock()
1612 if !ok {
1613 return &tcpip.ErrUnknownDevice{}
1614 }
1615 pkt := NewPacketBuffer(PacketBufferOptions{
1616 ReserveHeaderBytes: int(nic.MaxHeaderLength()),
1617 Data: payload,
1618 })
1619 defer pkt.DecRef()
1620 pkt.NetworkProtocolNumber = netProto
1621 return nic.WritePacketToRemote(remote, netProto, pkt)
1622 }
1623
1624 // WriteRawPacket writes data directly to the specified NIC without adding any
1625 // headers.
1626 func (s *Stack) WriteRawPacket(nicID tcpip.NICID, proto tcpip.NetworkProtocolNumber, payload buffer.VectorisedView) tcpip.Error {
1627 s.mu.RLock()
1628 nic, ok := s.nics[nicID]
1629 s.mu.RUnlock()
1630 if !ok {
1631 return &tcpip.ErrUnknownNICID{}
1632 }
1633
1634 pkt := NewPacketBuffer(PacketBufferOptions{
1635 Data: payload,
1636 })
1637 defer pkt.DecRef()
1638 pkt.NetworkProtocolNumber = proto
1639 return nic.WriteRawPacket(pkt)
1640 }
1641
1642 // NetworkProtocolInstance returns the protocol instance in the stack for the
1643 // specified network protocol. This method is public for protocol implementers
1644 // and tests to use.
1645 func (s *Stack) NetworkProtocolInstance(num tcpip.NetworkProtocolNumber) NetworkProtocol {
1646 if p, ok := s.networkProtocols[num]; ok {
1647 return p
1648 }
1649 return nil
1650 }
1651
1652 // TransportProtocolInstance returns the protocol instance in the stack for the
1653 // specified transport protocol. This method is public for protocol implementers
1654 // and tests to use.
1655 func (s *Stack) TransportProtocolInstance(num tcpip.TransportProtocolNumber) TransportProtocol {
1656 if pState, ok := s.transportProtocols[num]; ok {
1657 return pState.proto
1658 }
1659 return nil
1660 }
1661
1662 // AddTCPProbe installs a probe function that will be invoked on every segment
1663 // received by a given TCP endpoint. The probe function is passed a copy of the
1664 // TCP endpoint state before and after processing of the segment.
1665 //
1666 // NOTE: TCPProbe is added only to endpoints created after this call. Endpoints
1667 // created prior to this call will not call the probe function.
1668 //
1669 // Further, installing two different probes back to back can result in some
1670 // endpoints calling the first one and some the second one. There is no
1671 // guarantee provided on which probe will be invoked. Ideally this should only
1672 // be called once per stack.
1673 func (s *Stack) AddTCPProbe(probe TCPProbeFunc) {
1674 s.tcpProbeFunc.Store(probe)
1675 }
1676
1677 // GetTCPProbe returns the TCPProbeFunc if installed with AddTCPProbe, nil
1678 // otherwise.
1679 func (s *Stack) GetTCPProbe() TCPProbeFunc {
1680 p := s.tcpProbeFunc.Load()
1681 if p == nil {
1682 return nil
1683 }
1684 return p.(TCPProbeFunc)
1685 }
1686
1687 // RemoveTCPProbe removes an installed TCP probe.
1688 //
1689 // NOTE: This only ensures that endpoints created after this call do not
1690 // have a probe attached. Endpoints already created will continue to invoke
1691 // TCP probe.
1692 func (s *Stack) RemoveTCPProbe() {
1693 // This must be TCPProbeFunc(nil) because atomic.Value.Store(nil) panics.
1694 s.tcpProbeFunc.Store(TCPProbeFunc(nil))
1695 }
1696
1697 // JoinGroup joins the given multicast group on the given NIC.
1698 func (s *Stack) JoinGroup(protocol tcpip.NetworkProtocolNumber, nicID tcpip.NICID, multicastAddr tcpip.Address) tcpip.Error {
1699 s.mu.RLock()
1700 defer s.mu.RUnlock()
1701
1702 if nic, ok := s.nics[nicID]; ok {
1703 return nic.joinGroup(protocol, multicastAddr)
1704 }
1705 return &tcpip.ErrUnknownNICID{}
1706 }
1707
1708 // LeaveGroup leaves the given multicast group on the given NIC.
1709 func (s *Stack) LeaveGroup(protocol tcpip.NetworkProtocolNumber, nicID tcpip.NICID, multicastAddr tcpip.Address) tcpip.Error {
1710 s.mu.RLock()
1711 defer s.mu.RUnlock()
1712
1713 if nic, ok := s.nics[nicID]; ok {
1714 return nic.leaveGroup(protocol, multicastAddr)
1715 }
1716 return &tcpip.ErrUnknownNICID{}
1717 }
1718
1719 // IsInGroup returns true if the NIC with ID nicID has joined the multicast
1720 // group multicastAddr.
1721 func (s *Stack) IsInGroup(nicID tcpip.NICID, multicastAddr tcpip.Address) (bool, tcpip.Error) {
1722 s.mu.RLock()
1723 defer s.mu.RUnlock()
1724
1725 if nic, ok := s.nics[nicID]; ok {
1726 return nic.isInGroup(multicastAddr), nil
1727 }
1728 return false, &tcpip.ErrUnknownNICID{}
1729 }
1730
1731 // IPTables returns the stack's iptables.
1732 func (s *Stack) IPTables() *IPTables {
1733 return s.tables
1734 }
1735
1736 // ICMPLimit returns the maximum number of ICMP messages that can be sent
1737 // in one second.
1738 func (s *Stack) ICMPLimit() rate.Limit {
1739 return s.icmpRateLimiter.Limit()
1740 }
1741
1742 // SetICMPLimit sets the maximum number of ICMP messages that be sent
1743 // in one second.
1744 func (s *Stack) SetICMPLimit(newLimit rate.Limit) {
1745 s.icmpRateLimiter.SetLimit(newLimit)
1746 }
1747
1748 // ICMPBurst returns the maximum number of ICMP messages that can be sent
1749 // in a single burst.
1750 func (s *Stack) ICMPBurst() int {
1751 return s.icmpRateLimiter.Burst()
1752 }
1753
1754 // SetICMPBurst sets the maximum number of ICMP messages that can be sent
1755 // in a single burst.
1756 func (s *Stack) SetICMPBurst(burst int) {
1757 s.icmpRateLimiter.SetBurst(burst)
1758 }
1759
1760 // AllowICMPMessage returns true if we the rate limiter allows at least one
1761 // ICMP message to be sent at this instant.
1762 func (s *Stack) AllowICMPMessage() bool {
1763 return s.icmpRateLimiter.Allow()
1764 }
1765
1766 // GetNetworkEndpoint returns the NetworkEndpoint with the specified protocol
1767 // number installed on the specified NIC.
1768 func (s *Stack) GetNetworkEndpoint(nicID tcpip.NICID, proto tcpip.NetworkProtocolNumber) (NetworkEndpoint, tcpip.Error) {
1769 s.mu.Lock()
1770 defer s.mu.Unlock()
1771
1772 nic, ok := s.nics[nicID]
1773 if !ok {
1774 return nil, &tcpip.ErrUnknownNICID{}
1775 }
1776
1777 return nic.getNetworkEndpoint(proto), nil
1778 }
1779
1780 // NUDConfigurations gets the per-interface NUD configurations.
1781 func (s *Stack) NUDConfigurations(id tcpip.NICID, proto tcpip.NetworkProtocolNumber) (NUDConfigurations, tcpip.Error) {
1782 s.mu.RLock()
1783 nic, ok := s.nics[id]
1784 s.mu.RUnlock()
1785
1786 if !ok {
1787 return NUDConfigurations{}, &tcpip.ErrUnknownNICID{}
1788 }
1789
1790 return nic.nudConfigs(proto)
1791 }
1792
1793 // SetNUDConfigurations sets the per-interface NUD configurations.
1794 //
1795 // Note, if c contains invalid NUD configuration values, it will be fixed to
1796 // use default values for the erroneous values.
1797 func (s *Stack) SetNUDConfigurations(id tcpip.NICID, proto tcpip.NetworkProtocolNumber, c NUDConfigurations) tcpip.Error {
1798 s.mu.RLock()
1799 nic, ok := s.nics[id]
1800 s.mu.RUnlock()
1801
1802 if !ok {
1803 return &tcpip.ErrUnknownNICID{}
1804 }
1805
1806 return nic.setNUDConfigs(proto, c)
1807 }
1808
1809 // Seed returns a 32 bit value that can be used as a seed value.
1810 //
1811 // NOTE: The seed is generated once during stack initialization only.
1812 func (s *Stack) Seed() uint32 {
1813 return s.seed
1814 }
1815
1816 // Rand returns a reference to a pseudo random generator that can be used
1817 // to generate random numbers as required.
1818 func (s *Stack) Rand() *rand.Rand {
1819 return s.randomGenerator
1820 }
1821
1822 // SecureRNG returns the stack's cryptographically secure random number
1823 // generator.
1824 func (s *Stack) SecureRNG() io.Reader {
1825 return s.secureRNG
1826 }
1827
1828 // FindNICNameFromID returns the name of the NIC for the given NICID.
1829 func (s *Stack) FindNICNameFromID(id tcpip.NICID) string {
1830 s.mu.RLock()
1831 defer s.mu.RUnlock()
1832
1833 nic, ok := s.nics[id]
1834 if !ok {
1835 return ""
1836 }
1837
1838 return nic.Name()
1839 }
1840
1841 // ParseResult indicates the result of a parsing attempt.
1842 type ParseResult int
1843
1844 const (
1845 // ParsedOK indicates that a packet was successfully parsed.
1846 ParsedOK ParseResult = iota
1847
1848 // UnknownTransportProtocol indicates that the transport protocol is unknown.
1849 UnknownTransportProtocol
1850
1851 // TransportLayerParseError indicates that the transport packet was not
1852 // successfully parsed.
1853 TransportLayerParseError
1854 )
1855
1856 // ParsePacketBufferTransport parses the provided packet buffer's transport
1857 // header.
1858 func (s *Stack) ParsePacketBufferTransport(protocol tcpip.TransportProtocolNumber, pkt *PacketBuffer) ParseResult {
1859 pkt.TransportProtocolNumber = protocol
1860 // Parse the transport header if present.
1861 state, ok := s.transportProtocols[protocol]
1862 if !ok {
1863 return UnknownTransportProtocol
1864 }
1865
1866 if !state.proto.Parse(pkt) {
1867 return TransportLayerParseError
1868 }
1869
1870 return ParsedOK
1871 }
1872
1873 // networkProtocolNumbers returns the network protocol numbers the stack is
1874 // configured with.
1875 func (s *Stack) networkProtocolNumbers() []tcpip.NetworkProtocolNumber {
1876 protos := make([]tcpip.NetworkProtocolNumber, 0, len(s.networkProtocols))
1877 for p := range s.networkProtocols {
1878 protos = append(protos, p)
1879 }
1880 return protos
1881 }
1882
1883 func isSubnetBroadcastOnNIC(nic *nic, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) bool {
1884 addressEndpoint := nic.getAddressOrCreateTempInner(protocol, addr, false /* createTemp */, NeverPrimaryEndpoint)
1885 if addressEndpoint == nil {
1886 return false
1887 }
1888
1889 subnet := addressEndpoint.Subnet()
1890 addressEndpoint.DecRef()
1891 return subnet.IsBroadcast(addr)
1892 }
1893
1894 // IsSubnetBroadcast returns true if the provided address is a subnet-local
1895 // broadcast address on the specified NIC and protocol.
1896 //
1897 // Returns false if the NIC is unknown or if the protocol is unknown or does
1898 // not support addressing.
1899 //
1900 // If the NIC is not specified, the stack will check all NICs.
1901 func (s *Stack) IsSubnetBroadcast(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) bool {
1902 s.mu.RLock()
1903 defer s.mu.RUnlock()
1904
1905 if nicID != 0 {
1906 nic, ok := s.nics[nicID]
1907 if !ok {
1908 return false
1909 }
1910
1911 return isSubnetBroadcastOnNIC(nic, protocol, addr)
1912 }
1913
1914 for _, nic := range s.nics {
1915 if isSubnetBroadcastOnNIC(nic, protocol, addr) {
1916 return true
1917 }
1918 }
1919
1920 return false
1921 }
1922
1923 // PacketEndpointWriteSupported returns true iff packet endpoints support write
1924 // operations.
1925 func (s *Stack) PacketEndpointWriteSupported() bool {
1926 return s.packetEndpointWriteSupported
1927 }