github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/tcpip/stack/nic.go (about)

     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
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  
    21  	"github.com/nicocha30/gvisor-ligolo/pkg/atomicbitops"
    22  	"github.com/nicocha30/gvisor-ligolo/pkg/tcpip"
    23  	"github.com/nicocha30/gvisor-ligolo/pkg/tcpip/header"
    24  )
    25  
    26  type linkResolver struct {
    27  	resolver LinkAddressResolver
    28  
    29  	neigh neighborCache
    30  }
    31  
    32  var _ NetworkInterface = (*nic)(nil)
    33  var _ NetworkDispatcher = (*nic)(nil)
    34  
    35  // nic represents a "network interface card" to which the networking stack is
    36  // attached.
    37  type nic struct {
    38  	NetworkLinkEndpoint
    39  
    40  	stack   *Stack
    41  	id      tcpip.NICID
    42  	name    string
    43  	context NICContext
    44  
    45  	stats sharedStats
    46  
    47  	// enableDisableMu is used to synchronize attempts to enable/disable the NIC.
    48  	// Without this mutex, calls to enable/disable the NIC may interleave and
    49  	// leave the NIC in an inconsistent state.
    50  	enableDisableMu nicRWMutex
    51  
    52  	// The network endpoints themselves may be modified by calling the interface's
    53  	// methods, but the map reference and entries must be constant.
    54  	networkEndpoints          map[tcpip.NetworkProtocolNumber]NetworkEndpoint
    55  	linkAddrResolvers         map[tcpip.NetworkProtocolNumber]*linkResolver
    56  	duplicateAddressDetectors map[tcpip.NetworkProtocolNumber]DuplicateAddressDetector
    57  
    58  	// enabled indicates whether the NIC is enabled.
    59  	enabled atomicbitops.Bool
    60  
    61  	// spoofing indicates whether the NIC is spoofing.
    62  	spoofing atomicbitops.Bool
    63  
    64  	// promiscuous indicates whether the NIC is promiscuous.
    65  	promiscuous atomicbitops.Bool
    66  
    67  	// linkResQueue holds packets that are waiting for link resolution to
    68  	// complete.
    69  	linkResQueue packetsPendingLinkResolution
    70  
    71  	// packetEPsMu protects annotated fields below.
    72  	packetEPsMu packetEPsRWMutex
    73  
    74  	// eps is protected by the mutex, but the values contained in it are not.
    75  	//
    76  	// +checklocks:packetEPsMu
    77  	packetEPs map[tcpip.NetworkProtocolNumber]*packetEndpointList
    78  
    79  	qDisc QueueingDiscipline
    80  
    81  	gro groDispatcher
    82  }
    83  
    84  // makeNICStats initializes the NIC statistics and associates them to the global
    85  // NIC statistics.
    86  func makeNICStats(global tcpip.NICStats) sharedStats {
    87  	var stats sharedStats
    88  	tcpip.InitStatCounters(reflect.ValueOf(&stats.local).Elem())
    89  	stats.init(&stats.local, &global)
    90  	return stats
    91  }
    92  
    93  type packetEndpointList struct {
    94  	mu packetEndpointListRWMutex
    95  
    96  	// eps is protected by mu, but the contained PacketEndpoint values are not.
    97  	//
    98  	// +checklocks:mu
    99  	eps []PacketEndpoint
   100  }
   101  
   102  func (p *packetEndpointList) add(ep PacketEndpoint) {
   103  	p.mu.Lock()
   104  	defer p.mu.Unlock()
   105  	p.eps = append(p.eps, ep)
   106  }
   107  
   108  func (p *packetEndpointList) remove(ep PacketEndpoint) {
   109  	p.mu.Lock()
   110  	defer p.mu.Unlock()
   111  	for i, epOther := range p.eps {
   112  		if epOther == ep {
   113  			p.eps = append(p.eps[:i], p.eps[i+1:]...)
   114  			break
   115  		}
   116  	}
   117  }
   118  
   119  func (p *packetEndpointList) len() int {
   120  	p.mu.RLock()
   121  	defer p.mu.RUnlock()
   122  	return len(p.eps)
   123  }
   124  
   125  // forEach calls fn with each endpoints in p while holding the read lock on p.
   126  func (p *packetEndpointList) forEach(fn func(PacketEndpoint)) {
   127  	p.mu.RLock()
   128  	defer p.mu.RUnlock()
   129  	for _, ep := range p.eps {
   130  		fn(ep)
   131  	}
   132  }
   133  
   134  var _ QueueingDiscipline = (*delegatingQueueingDiscipline)(nil)
   135  
   136  type delegatingQueueingDiscipline struct {
   137  	LinkWriter
   138  }
   139  
   140  func (*delegatingQueueingDiscipline) Close() {}
   141  
   142  // WritePacket passes the packet through to the underlying LinkWriter's WritePackets.
   143  func (qDisc *delegatingQueueingDiscipline) WritePacket(pkt PacketBufferPtr) tcpip.Error {
   144  	var pkts PacketBufferList
   145  	pkts.PushBack(pkt)
   146  	_, err := qDisc.LinkWriter.WritePackets(pkts)
   147  	return err
   148  }
   149  
   150  // newNIC returns a new NIC using the default NDP configurations from stack.
   151  func newNIC(stack *Stack, id tcpip.NICID, ep LinkEndpoint, opts NICOptions) *nic {
   152  	// TODO(b/141011931): Validate a LinkEndpoint (ep) is valid. For
   153  	// example, make sure that the link address it provides is a valid
   154  	// unicast ethernet address.
   155  
   156  	// If no queueing discipline was specified provide a stub implementation that
   157  	// just delegates to the lower link endpoint.
   158  	qDisc := opts.QDisc
   159  	if qDisc == nil {
   160  		qDisc = &delegatingQueueingDiscipline{LinkWriter: ep}
   161  	}
   162  
   163  	// TODO(b/143357959): RFC 8200 section 5 requires that IPv6 endpoints
   164  	// observe an MTU of at least 1280 bytes. Ensure that this requirement
   165  	// of IPv6 is supported on this endpoint's LinkEndpoint.
   166  	nic := &nic{
   167  		NetworkLinkEndpoint:       ep,
   168  		stack:                     stack,
   169  		id:                        id,
   170  		name:                      opts.Name,
   171  		context:                   opts.Context,
   172  		stats:                     makeNICStats(stack.Stats().NICs),
   173  		networkEndpoints:          make(map[tcpip.NetworkProtocolNumber]NetworkEndpoint),
   174  		linkAddrResolvers:         make(map[tcpip.NetworkProtocolNumber]*linkResolver),
   175  		duplicateAddressDetectors: make(map[tcpip.NetworkProtocolNumber]DuplicateAddressDetector),
   176  		qDisc:                     qDisc,
   177  	}
   178  	nic.linkResQueue.init(nic)
   179  
   180  	nic.packetEPsMu.Lock()
   181  	defer nic.packetEPsMu.Unlock()
   182  
   183  	nic.packetEPs = make(map[tcpip.NetworkProtocolNumber]*packetEndpointList)
   184  
   185  	resolutionRequired := ep.Capabilities()&CapabilityResolutionRequired != 0
   186  
   187  	for _, netProto := range stack.networkProtocols {
   188  		netNum := netProto.Number()
   189  		netEP := netProto.NewEndpoint(nic, nic)
   190  		nic.networkEndpoints[netNum] = netEP
   191  
   192  		if resolutionRequired {
   193  			if r, ok := netEP.(LinkAddressResolver); ok {
   194  				l := &linkResolver{resolver: r}
   195  				l.neigh.init(nic, r)
   196  				nic.linkAddrResolvers[r.LinkAddressProtocol()] = l
   197  			}
   198  		}
   199  
   200  		if d, ok := netEP.(DuplicateAddressDetector); ok {
   201  			nic.duplicateAddressDetectors[d.DuplicateAddressProtocol()] = d
   202  		}
   203  	}
   204  
   205  	nic.gro.init(opts.GROTimeout)
   206  	nic.NetworkLinkEndpoint.Attach(nic)
   207  
   208  	return nic
   209  }
   210  
   211  func (n *nic) getNetworkEndpoint(proto tcpip.NetworkProtocolNumber) NetworkEndpoint {
   212  	return n.networkEndpoints[proto]
   213  }
   214  
   215  // Enabled implements NetworkInterface.
   216  func (n *nic) Enabled() bool {
   217  	return n.enabled.Load()
   218  }
   219  
   220  // setEnabled sets the enabled status for the NIC.
   221  //
   222  // Returns true if the enabled status was updated.
   223  //
   224  // +checklocks:n.enableDisableMu
   225  func (n *nic) setEnabled(v bool) bool {
   226  	return n.enabled.Swap(v) != v
   227  }
   228  
   229  // disable disables n.
   230  //
   231  // It undoes the work done by enable.
   232  func (n *nic) disable() {
   233  	n.enableDisableMu.Lock()
   234  	defer n.enableDisableMu.Unlock()
   235  	n.disableLocked()
   236  }
   237  
   238  // disableLocked disables n.
   239  //
   240  // It undoes the work done by enable.
   241  //
   242  // +checklocks:n.enableDisableMu
   243  func (n *nic) disableLocked() {
   244  	if !n.Enabled() {
   245  		return
   246  	}
   247  
   248  	// TODO(gvisor.dev/issue/1491): Should Routes that are currently bound to n be
   249  	// invalidated? Currently, Routes will continue to work when a NIC is enabled
   250  	// again, and applications may not know that the underlying NIC was ever
   251  	// disabled.
   252  
   253  	for _, ep := range n.networkEndpoints {
   254  		ep.Disable()
   255  
   256  		// Clear the neighbour table (including static entries) as we cannot
   257  		// guarantee that the current neighbour table will be valid when the NIC is
   258  		// enabled again.
   259  		//
   260  		// This matches linux's behaviour at the time of writing:
   261  		// https://github.com/torvalds/linux/blob/71c061d2443814de15e177489d5cc00a4a253ef3/net/core/neighbour.c#L371
   262  		netProto := ep.NetworkProtocolNumber()
   263  		switch err := n.clearNeighbors(netProto); err.(type) {
   264  		case nil, *tcpip.ErrNotSupported:
   265  		default:
   266  			panic(fmt.Sprintf("n.clearNeighbors(%d): %s", netProto, err))
   267  		}
   268  	}
   269  
   270  	if !n.setEnabled(false) {
   271  		panic("should have only done work to disable the NIC if it was enabled")
   272  	}
   273  }
   274  
   275  // enable enables n.
   276  //
   277  // If the stack has IPv6 enabled, enable will join the IPv6 All-Nodes Multicast
   278  // address (ff02::1), start DAD for permanent addresses, and start soliciting
   279  // routers if the stack is not operating as a router. If the stack is also
   280  // configured to auto-generate a link-local address, one will be generated.
   281  func (n *nic) enable() tcpip.Error {
   282  	n.enableDisableMu.Lock()
   283  	defer n.enableDisableMu.Unlock()
   284  
   285  	if !n.setEnabled(true) {
   286  		return nil
   287  	}
   288  
   289  	for _, ep := range n.networkEndpoints {
   290  		if err := ep.Enable(); err != nil {
   291  			return err
   292  		}
   293  	}
   294  
   295  	return nil
   296  }
   297  
   298  // remove detaches NIC from the link endpoint and releases network endpoint
   299  // resources. This guarantees no packets between this NIC and the network
   300  // stack.
   301  func (n *nic) remove() tcpip.Error {
   302  	n.enableDisableMu.Lock()
   303  
   304  	n.disableLocked()
   305  
   306  	for _, ep := range n.networkEndpoints {
   307  		ep.Close()
   308  	}
   309  
   310  	n.enableDisableMu.Unlock()
   311  
   312  	// Shutdown GRO.
   313  	n.gro.close()
   314  
   315  	// Drain and drop any packets pending link resolution.
   316  	// We must not hold n.enableDisableMu here.
   317  	n.linkResQueue.cancel()
   318  
   319  	// Prevent packets from going down to the link before shutting the link down.
   320  	n.qDisc.Close()
   321  	n.NetworkLinkEndpoint.Attach(nil)
   322  
   323  	return nil
   324  }
   325  
   326  // setPromiscuousMode enables or disables promiscuous mode.
   327  func (n *nic) setPromiscuousMode(enable bool) {
   328  	n.promiscuous.Store(enable)
   329  }
   330  
   331  // Promiscuous implements NetworkInterface.
   332  func (n *nic) Promiscuous() bool {
   333  	return n.promiscuous.Load()
   334  }
   335  
   336  // IsLoopback implements NetworkInterface.
   337  func (n *nic) IsLoopback() bool {
   338  	return n.NetworkLinkEndpoint.Capabilities()&CapabilityLoopback != 0
   339  }
   340  
   341  // WritePacket implements NetworkEndpoint.
   342  func (n *nic) WritePacket(r *Route, pkt PacketBufferPtr) tcpip.Error {
   343  	routeInfo, _, err := r.resolvedFields(nil)
   344  	switch err.(type) {
   345  	case nil:
   346  		pkt.EgressRoute = routeInfo
   347  		return n.writePacket(pkt)
   348  	case *tcpip.ErrWouldBlock:
   349  		// As per relevant RFCs, we should queue packets while we wait for link
   350  		// resolution to complete.
   351  		//
   352  		// RFC 1122 section 2.3.2.2 (for IPv4):
   353  		//   The link layer SHOULD save (rather than discard) at least
   354  		//   one (the latest) packet of each set of packets destined to
   355  		//   the same unresolved IP address, and transmit the saved
   356  		//   packet when the address has been resolved.
   357  		//
   358  		// RFC 4861 section 7.2.2 (for IPv6):
   359  		//   While waiting for address resolution to complete, the sender MUST, for
   360  		//   each neighbor, retain a small queue of packets waiting for address
   361  		//   resolution to complete. The queue MUST hold at least one packet, and
   362  		//   MAY contain more. However, the number of queued packets per neighbor
   363  		//   SHOULD be limited to some small value. When a queue overflows, the new
   364  		//   arrival SHOULD replace the oldest entry. Once address resolution
   365  		//   completes, the node transmits any queued packets.
   366  		return n.linkResQueue.enqueue(r, pkt)
   367  	default:
   368  		return err
   369  	}
   370  }
   371  
   372  // WritePacketToRemote implements NetworkInterface.
   373  func (n *nic) WritePacketToRemote(remoteLinkAddr tcpip.LinkAddress, pkt PacketBufferPtr) tcpip.Error {
   374  	pkt.EgressRoute = RouteInfo{
   375  		routeInfo: routeInfo{
   376  			NetProto:         pkt.NetworkProtocolNumber,
   377  			LocalLinkAddress: n.LinkAddress(),
   378  		},
   379  		RemoteLinkAddress: remoteLinkAddr,
   380  	}
   381  	return n.writePacket(pkt)
   382  }
   383  
   384  func (n *nic) writePacket(pkt PacketBufferPtr) tcpip.Error {
   385  	n.NetworkLinkEndpoint.AddHeader(pkt)
   386  	return n.writeRawPacket(pkt)
   387  }
   388  
   389  func (n *nic) writeRawPacketWithLinkHeaderInPayload(pkt PacketBufferPtr) tcpip.Error {
   390  	if !n.NetworkLinkEndpoint.ParseHeader(pkt) {
   391  		return &tcpip.ErrMalformedHeader{}
   392  	}
   393  	return n.writeRawPacket(pkt)
   394  }
   395  
   396  func (n *nic) writeRawPacket(pkt PacketBufferPtr) tcpip.Error {
   397  	// Always an outgoing packet.
   398  	pkt.PktType = tcpip.PacketOutgoing
   399  	if err := n.qDisc.WritePacket(pkt); err != nil {
   400  		if _, ok := err.(*tcpip.ErrNoBufferSpace); ok {
   401  			n.stats.txPacketsDroppedNoBufferSpace.Increment()
   402  		}
   403  		return err
   404  	}
   405  
   406  	n.stats.tx.packets.Increment()
   407  	n.stats.tx.bytes.IncrementBy(uint64(pkt.Size()))
   408  	return nil
   409  }
   410  
   411  // setSpoofing enables or disables address spoofing.
   412  func (n *nic) setSpoofing(enable bool) {
   413  	n.spoofing.Store(enable)
   414  }
   415  
   416  // Spoofing implements NetworkInterface.
   417  func (n *nic) Spoofing() bool {
   418  	return n.spoofing.Load()
   419  }
   420  
   421  // primaryAddress returns an address that can be used to communicate with
   422  // remoteAddr.
   423  func (n *nic) primaryEndpoint(protocol tcpip.NetworkProtocolNumber, remoteAddr tcpip.Address) AssignableAddressEndpoint {
   424  	ep := n.getNetworkEndpoint(protocol)
   425  	if ep == nil {
   426  		return nil
   427  	}
   428  
   429  	addressableEndpoint, ok := ep.(AddressableEndpoint)
   430  	if !ok {
   431  		return nil
   432  	}
   433  
   434  	return addressableEndpoint.AcquireOutgoingPrimaryAddress(remoteAddr, n.Spoofing())
   435  }
   436  
   437  type getAddressBehaviour int
   438  
   439  const (
   440  	// spoofing indicates that the NIC's spoofing flag should be observed when
   441  	// getting a NIC's address endpoint.
   442  	spoofing getAddressBehaviour = iota
   443  
   444  	// promiscuous indicates that the NIC's promiscuous flag should be observed
   445  	// when getting a NIC's address endpoint.
   446  	promiscuous
   447  )
   448  
   449  func (n *nic) getAddress(protocol tcpip.NetworkProtocolNumber, dst tcpip.Address) AssignableAddressEndpoint {
   450  	return n.getAddressOrCreateTemp(protocol, dst, CanBePrimaryEndpoint, promiscuous)
   451  }
   452  
   453  func (n *nic) hasAddress(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) bool {
   454  	ep := n.getAddressOrCreateTempInner(protocol, addr, false, NeverPrimaryEndpoint)
   455  	if ep != nil {
   456  		ep.DecRef()
   457  		return true
   458  	}
   459  
   460  	return false
   461  }
   462  
   463  // findEndpoint finds the endpoint, if any, with the given address.
   464  func (n *nic) findEndpoint(protocol tcpip.NetworkProtocolNumber, address tcpip.Address, peb PrimaryEndpointBehavior) AssignableAddressEndpoint {
   465  	return n.getAddressOrCreateTemp(protocol, address, peb, spoofing)
   466  }
   467  
   468  // getAddressEpOrCreateTemp returns the address endpoint for the given protocol
   469  // and address.
   470  //
   471  // If none exists a temporary one may be created if we are in promiscuous mode
   472  // or spoofing. Promiscuous mode will only be checked if promiscuous is true.
   473  // Similarly, spoofing will only be checked if spoofing is true.
   474  //
   475  // If the address is the IPv4 broadcast address for an endpoint's network, that
   476  // endpoint will be returned.
   477  func (n *nic) getAddressOrCreateTemp(protocol tcpip.NetworkProtocolNumber, address tcpip.Address, peb PrimaryEndpointBehavior, tempRef getAddressBehaviour) AssignableAddressEndpoint {
   478  	var spoofingOrPromiscuous bool
   479  	switch tempRef {
   480  	case spoofing:
   481  		spoofingOrPromiscuous = n.Spoofing()
   482  	case promiscuous:
   483  		spoofingOrPromiscuous = n.Promiscuous()
   484  	}
   485  	return n.getAddressOrCreateTempInner(protocol, address, spoofingOrPromiscuous, peb)
   486  }
   487  
   488  // getAddressOrCreateTempInner is like getAddressEpOrCreateTemp except a boolean
   489  // is passed to indicate whether or not we should generate temporary endpoints.
   490  func (n *nic) getAddressOrCreateTempInner(protocol tcpip.NetworkProtocolNumber, address tcpip.Address, createTemp bool, peb PrimaryEndpointBehavior) AssignableAddressEndpoint {
   491  	ep := n.getNetworkEndpoint(protocol)
   492  	if ep == nil {
   493  		return nil
   494  	}
   495  
   496  	addressableEndpoint, ok := ep.(AddressableEndpoint)
   497  	if !ok {
   498  		return nil
   499  	}
   500  
   501  	return addressableEndpoint.AcquireAssignedAddress(address, createTemp, peb)
   502  }
   503  
   504  // addAddress adds a new address to n, so that it starts accepting packets
   505  // targeted at the given address (and network protocol).
   506  func (n *nic) addAddress(protocolAddress tcpip.ProtocolAddress, properties AddressProperties) tcpip.Error {
   507  	ep := n.getNetworkEndpoint(protocolAddress.Protocol)
   508  	if ep == nil {
   509  		return &tcpip.ErrUnknownProtocol{}
   510  	}
   511  
   512  	addressableEndpoint, ok := ep.(AddressableEndpoint)
   513  	if !ok {
   514  		return &tcpip.ErrNotSupported{}
   515  	}
   516  
   517  	addressEndpoint, err := addressableEndpoint.AddAndAcquirePermanentAddress(protocolAddress.AddressWithPrefix, properties)
   518  	if err == nil {
   519  		// We have no need for the address endpoint.
   520  		addressEndpoint.DecRef()
   521  	}
   522  	return err
   523  }
   524  
   525  // allPermanentAddresses returns all permanent addresses associated with
   526  // this NIC.
   527  func (n *nic) allPermanentAddresses() []tcpip.ProtocolAddress {
   528  	var addrs []tcpip.ProtocolAddress
   529  	for p, ep := range n.networkEndpoints {
   530  		addressableEndpoint, ok := ep.(AddressableEndpoint)
   531  		if !ok {
   532  			continue
   533  		}
   534  
   535  		for _, a := range addressableEndpoint.PermanentAddresses() {
   536  			addrs = append(addrs, tcpip.ProtocolAddress{Protocol: p, AddressWithPrefix: a})
   537  		}
   538  	}
   539  	return addrs
   540  }
   541  
   542  // primaryAddresses returns the primary addresses associated with this NIC.
   543  func (n *nic) primaryAddresses() []tcpip.ProtocolAddress {
   544  	var addrs []tcpip.ProtocolAddress
   545  	for p, ep := range n.networkEndpoints {
   546  		addressableEndpoint, ok := ep.(AddressableEndpoint)
   547  		if !ok {
   548  			continue
   549  		}
   550  
   551  		for _, a := range addressableEndpoint.PrimaryAddresses() {
   552  			addrs = append(addrs, tcpip.ProtocolAddress{Protocol: p, AddressWithPrefix: a})
   553  		}
   554  	}
   555  	return addrs
   556  }
   557  
   558  // PrimaryAddress implements NetworkInterface.
   559  func (n *nic) PrimaryAddress(proto tcpip.NetworkProtocolNumber) (tcpip.AddressWithPrefix, tcpip.Error) {
   560  	ep := n.getNetworkEndpoint(proto)
   561  	if ep == nil {
   562  		return tcpip.AddressWithPrefix{}, &tcpip.ErrUnknownProtocol{}
   563  	}
   564  
   565  	addressableEndpoint, ok := ep.(AddressableEndpoint)
   566  	if !ok {
   567  		return tcpip.AddressWithPrefix{}, &tcpip.ErrNotSupported{}
   568  	}
   569  
   570  	return addressableEndpoint.MainAddress(), nil
   571  }
   572  
   573  // removeAddress removes an address from n.
   574  func (n *nic) removeAddress(addr tcpip.Address) tcpip.Error {
   575  	for _, ep := range n.networkEndpoints {
   576  		addressableEndpoint, ok := ep.(AddressableEndpoint)
   577  		if !ok {
   578  			continue
   579  		}
   580  
   581  		switch err := addressableEndpoint.RemovePermanentAddress(addr); err.(type) {
   582  		case *tcpip.ErrBadLocalAddress:
   583  			continue
   584  		default:
   585  			return err
   586  		}
   587  	}
   588  
   589  	return &tcpip.ErrBadLocalAddress{}
   590  }
   591  
   592  func (n *nic) setAddressLifetimes(addr tcpip.Address, lifetimes AddressLifetimes) tcpip.Error {
   593  	for _, ep := range n.networkEndpoints {
   594  		ep, ok := ep.(AddressableEndpoint)
   595  		if !ok {
   596  			continue
   597  		}
   598  
   599  		switch err := ep.SetLifetimes(addr, lifetimes); err.(type) {
   600  		case *tcpip.ErrBadLocalAddress:
   601  			continue
   602  		default:
   603  			return err
   604  		}
   605  	}
   606  
   607  	return &tcpip.ErrBadLocalAddress{}
   608  }
   609  
   610  func (n *nic) getLinkAddress(addr, localAddr tcpip.Address, protocol tcpip.NetworkProtocolNumber, onResolve func(LinkResolutionResult)) tcpip.Error {
   611  	linkRes, ok := n.linkAddrResolvers[protocol]
   612  	if !ok {
   613  		return &tcpip.ErrNotSupported{}
   614  	}
   615  
   616  	if linkAddr, ok := linkRes.resolver.ResolveStaticAddress(addr); ok {
   617  		onResolve(LinkResolutionResult{LinkAddress: linkAddr, Err: nil})
   618  		return nil
   619  	}
   620  
   621  	_, _, err := linkRes.neigh.entry(addr, localAddr, onResolve)
   622  	return err
   623  }
   624  
   625  func (n *nic) neighbors(protocol tcpip.NetworkProtocolNumber) ([]NeighborEntry, tcpip.Error) {
   626  	if linkRes, ok := n.linkAddrResolvers[protocol]; ok {
   627  		return linkRes.neigh.entries(), nil
   628  	}
   629  
   630  	return nil, &tcpip.ErrNotSupported{}
   631  }
   632  
   633  func (n *nic) addStaticNeighbor(addr tcpip.Address, protocol tcpip.NetworkProtocolNumber, linkAddress tcpip.LinkAddress) tcpip.Error {
   634  	if linkRes, ok := n.linkAddrResolvers[protocol]; ok {
   635  		linkRes.neigh.addStaticEntry(addr, linkAddress)
   636  		return nil
   637  	}
   638  
   639  	return &tcpip.ErrNotSupported{}
   640  }
   641  
   642  func (n *nic) removeNeighbor(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) tcpip.Error {
   643  	if linkRes, ok := n.linkAddrResolvers[protocol]; ok {
   644  		if !linkRes.neigh.removeEntry(addr) {
   645  			return &tcpip.ErrBadAddress{}
   646  		}
   647  		return nil
   648  	}
   649  
   650  	return &tcpip.ErrNotSupported{}
   651  }
   652  
   653  func (n *nic) clearNeighbors(protocol tcpip.NetworkProtocolNumber) tcpip.Error {
   654  	if linkRes, ok := n.linkAddrResolvers[protocol]; ok {
   655  		linkRes.neigh.clear()
   656  		return nil
   657  	}
   658  
   659  	return &tcpip.ErrNotSupported{}
   660  }
   661  
   662  // joinGroup adds a new endpoint for the given multicast address, if none
   663  // exists yet. Otherwise it just increments its count.
   664  func (n *nic) joinGroup(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) tcpip.Error {
   665  	// TODO(b/143102137): When implementing MLD, make sure MLD packets are
   666  	// not sent unless a valid link-local address is available for use on n
   667  	// as an MLD packet's source address must be a link-local address as
   668  	// outlined in RFC 3810 section 5.
   669  
   670  	ep := n.getNetworkEndpoint(protocol)
   671  	if ep == nil {
   672  		return &tcpip.ErrNotSupported{}
   673  	}
   674  
   675  	gep, ok := ep.(GroupAddressableEndpoint)
   676  	if !ok {
   677  		return &tcpip.ErrNotSupported{}
   678  	}
   679  
   680  	return gep.JoinGroup(addr)
   681  }
   682  
   683  // leaveGroup decrements the count for the given multicast address, and when it
   684  // reaches zero removes the endpoint for this address.
   685  func (n *nic) leaveGroup(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) tcpip.Error {
   686  	ep := n.getNetworkEndpoint(protocol)
   687  	if ep == nil {
   688  		return &tcpip.ErrNotSupported{}
   689  	}
   690  
   691  	gep, ok := ep.(GroupAddressableEndpoint)
   692  	if !ok {
   693  		return &tcpip.ErrNotSupported{}
   694  	}
   695  
   696  	return gep.LeaveGroup(addr)
   697  }
   698  
   699  // isInGroup returns true if n has joined the multicast group addr.
   700  func (n *nic) isInGroup(addr tcpip.Address) bool {
   701  	for _, ep := range n.networkEndpoints {
   702  		gep, ok := ep.(GroupAddressableEndpoint)
   703  		if !ok {
   704  			continue
   705  		}
   706  
   707  		if gep.IsInGroup(addr) {
   708  			return true
   709  		}
   710  	}
   711  
   712  	return false
   713  }
   714  
   715  // DeliverNetworkPacket finds the appropriate network protocol endpoint and
   716  // hands the packet over for further processing. This function is called when
   717  // the NIC receives a packet from the link endpoint.
   718  func (n *nic) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt PacketBufferPtr) {
   719  	enabled := n.Enabled()
   720  	// If the NIC is not yet enabled, don't receive any packets.
   721  	if !enabled {
   722  		n.stats.disabledRx.packets.Increment()
   723  		n.stats.disabledRx.bytes.IncrementBy(uint64(pkt.Data().Size()))
   724  		return
   725  	}
   726  
   727  	n.stats.rx.packets.Increment()
   728  	n.stats.rx.bytes.IncrementBy(uint64(pkt.Data().Size()))
   729  
   730  	networkEndpoint := n.getNetworkEndpoint(protocol)
   731  	if networkEndpoint == nil {
   732  		n.stats.unknownL3ProtocolRcvdPacketCounts.Increment(uint64(protocol))
   733  		return
   734  	}
   735  
   736  	pkt.RXChecksumValidated = n.NetworkLinkEndpoint.Capabilities()&CapabilityRXChecksumOffload != 0
   737  
   738  	n.gro.dispatch(pkt, protocol, networkEndpoint)
   739  }
   740  
   741  func (n *nic) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt PacketBufferPtr) {
   742  	// Deliver to interested packet endpoints without holding NIC lock.
   743  	var packetEPPkt PacketBufferPtr
   744  	defer func() {
   745  		if !packetEPPkt.IsNil() {
   746  			packetEPPkt.DecRef()
   747  		}
   748  	}()
   749  	deliverPacketEPs := func(ep PacketEndpoint) {
   750  		if packetEPPkt.IsNil() {
   751  			// Packet endpoints hold the full packet.
   752  			//
   753  			// We perform a deep copy because higher-level endpoints may point to
   754  			// the middle of a view that is held by a packet endpoint. Save/Restore
   755  			// does not support overlapping slices and will panic in this case.
   756  			//
   757  			// TODO(https://gvisor.dev/issue/6517): Avoid this copy once S/R supports
   758  			// overlapping slices (e.g. by passing a shallow copy of pkt to the packet
   759  			// endpoint).
   760  			packetEPPkt = NewPacketBuffer(PacketBufferOptions{
   761  				Payload: BufferSince(pkt.LinkHeader()),
   762  			})
   763  			// If a link header was populated in the original packet buffer, then
   764  			// populate it in the packet buffer we provide to packet endpoints as
   765  			// packet endpoints inspect link headers.
   766  			packetEPPkt.LinkHeader().Consume(len(pkt.LinkHeader().Slice()))
   767  			packetEPPkt.PktType = pkt.PktType
   768  			// Assume the packet is for us if the packet type is unset.
   769  			// The packet type is set to PacketOutgoing when sending packets so
   770  			// this may only be unset for incoming packets where link endpoints
   771  			// have not set it.
   772  			if packetEPPkt.PktType == 0 {
   773  				packetEPPkt.PktType = tcpip.PacketHost
   774  			}
   775  		}
   776  
   777  		clone := packetEPPkt.Clone()
   778  		defer clone.DecRef()
   779  		ep.HandlePacket(n.id, protocol, clone)
   780  	}
   781  
   782  	n.packetEPsMu.Lock()
   783  	// Are any packet type sockets listening for this network protocol?
   784  	protoEPs, protoEPsOK := n.packetEPs[protocol]
   785  	// Other packet type sockets that are listening for all protocols.
   786  	anyEPs, anyEPsOK := n.packetEPs[header.EthernetProtocolAll]
   787  	n.packetEPsMu.Unlock()
   788  
   789  	// On Linux, only ETH_P_ALL endpoints get outbound packets.
   790  	if pkt.PktType != tcpip.PacketOutgoing && protoEPsOK {
   791  		protoEPs.forEach(deliverPacketEPs)
   792  	}
   793  	if anyEPsOK {
   794  		anyEPs.forEach(deliverPacketEPs)
   795  	}
   796  }
   797  
   798  // DeliverTransportPacket delivers the packets to the appropriate transport
   799  // protocol endpoint.
   800  func (n *nic) DeliverTransportPacket(protocol tcpip.TransportProtocolNumber, pkt PacketBufferPtr) TransportPacketDisposition {
   801  	state, ok := n.stack.transportProtocols[protocol]
   802  	if !ok {
   803  		n.stats.unknownL4ProtocolRcvdPacketCounts.Increment(uint64(protocol))
   804  		return TransportPacketProtocolUnreachable
   805  	}
   806  
   807  	transProto := state.proto
   808  
   809  	if len(pkt.TransportHeader().Slice()) == 0 {
   810  		n.stats.malformedL4RcvdPackets.Increment()
   811  		return TransportPacketHandled
   812  	}
   813  
   814  	srcPort, dstPort, err := transProto.ParsePorts(pkt.TransportHeader().Slice())
   815  	if err != nil {
   816  		n.stats.malformedL4RcvdPackets.Increment()
   817  		return TransportPacketHandled
   818  	}
   819  
   820  	netProto, ok := n.stack.networkProtocols[pkt.NetworkProtocolNumber]
   821  	if !ok {
   822  		panic(fmt.Sprintf("expected network protocol = %d, have = %#v", pkt.NetworkProtocolNumber, n.stack.networkProtocolNumbers()))
   823  	}
   824  
   825  	src, dst := netProto.ParseAddresses(pkt.NetworkHeader().Slice())
   826  	id := TransportEndpointID{
   827  		LocalPort:     dstPort,
   828  		LocalAddress:  dst,
   829  		RemotePort:    srcPort,
   830  		RemoteAddress: src,
   831  	}
   832  	if n.stack.demux.deliverPacket(protocol, pkt, id) {
   833  		return TransportPacketHandled
   834  	}
   835  
   836  	// Try to deliver to per-stack default handler.
   837  	if state.defaultHandler != nil {
   838  		if state.defaultHandler(id, pkt) {
   839  			return TransportPacketHandled
   840  		}
   841  	}
   842  
   843  	// We could not find an appropriate destination for this packet so
   844  	// give the protocol specific error handler a chance to handle it.
   845  	// If it doesn't handle it then we should do so.
   846  	switch res := transProto.HandleUnknownDestinationPacket(id, pkt); res {
   847  	case UnknownDestinationPacketMalformed:
   848  		n.stats.malformedL4RcvdPackets.Increment()
   849  		return TransportPacketHandled
   850  	case UnknownDestinationPacketUnhandled:
   851  		return TransportPacketDestinationPortUnreachable
   852  	case UnknownDestinationPacketHandled:
   853  		return TransportPacketHandled
   854  	default:
   855  		panic(fmt.Sprintf("unrecognized result from HandleUnknownDestinationPacket = %d", res))
   856  	}
   857  }
   858  
   859  // DeliverTransportError implements TransportDispatcher.
   860  func (n *nic) DeliverTransportError(local, remote tcpip.Address, net tcpip.NetworkProtocolNumber, trans tcpip.TransportProtocolNumber, transErr TransportError, pkt PacketBufferPtr) {
   861  	state, ok := n.stack.transportProtocols[trans]
   862  	if !ok {
   863  		return
   864  	}
   865  
   866  	transProto := state.proto
   867  
   868  	// ICMPv4 only guarantees that 8 bytes of the transport protocol will
   869  	// be present in the payload. We know that the ports are within the
   870  	// first 8 bytes for all known transport protocols.
   871  	transHeader, ok := pkt.Data().PullUp(8)
   872  	if !ok {
   873  		return
   874  	}
   875  
   876  	srcPort, dstPort, err := transProto.ParsePorts(transHeader)
   877  	if err != nil {
   878  		return
   879  	}
   880  
   881  	id := TransportEndpointID{srcPort, local, dstPort, remote}
   882  	if n.stack.demux.deliverError(n, net, trans, transErr, pkt, id) {
   883  		return
   884  	}
   885  }
   886  
   887  // DeliverRawPacket implements TransportDispatcher.
   888  func (n *nic) DeliverRawPacket(protocol tcpip.TransportProtocolNumber, pkt PacketBufferPtr) {
   889  	// For ICMPv4 only we validate the header length for compatibility with
   890  	// raw(7) ICMP_FILTER. The same check is made in Linux here:
   891  	// https://github.com/torvalds/linux/blob/70585216/net/ipv4/raw.c#L189.
   892  	if protocol == header.ICMPv4ProtocolNumber && len(pkt.TransportHeader().Slice())+pkt.Data().Size() < header.ICMPv4MinimumSize {
   893  		return
   894  	}
   895  	n.stack.demux.deliverRawPacket(protocol, pkt)
   896  }
   897  
   898  // ID implements NetworkInterface.
   899  func (n *nic) ID() tcpip.NICID {
   900  	return n.id
   901  }
   902  
   903  // Name implements NetworkInterface.
   904  func (n *nic) Name() string {
   905  	return n.name
   906  }
   907  
   908  // nudConfigs gets the NUD configurations for n.
   909  func (n *nic) nudConfigs(protocol tcpip.NetworkProtocolNumber) (NUDConfigurations, tcpip.Error) {
   910  	if linkRes, ok := n.linkAddrResolvers[protocol]; ok {
   911  		return linkRes.neigh.config(), nil
   912  	}
   913  
   914  	return NUDConfigurations{}, &tcpip.ErrNotSupported{}
   915  }
   916  
   917  // setNUDConfigs sets the NUD configurations for n.
   918  //
   919  // Note, if c contains invalid NUD configuration values, it will be fixed to
   920  // use default values for the erroneous values.
   921  func (n *nic) setNUDConfigs(protocol tcpip.NetworkProtocolNumber, c NUDConfigurations) tcpip.Error {
   922  	if linkRes, ok := n.linkAddrResolvers[protocol]; ok {
   923  		c.resetInvalidFields()
   924  		linkRes.neigh.setConfig(c)
   925  		return nil
   926  	}
   927  
   928  	return &tcpip.ErrNotSupported{}
   929  }
   930  
   931  func (n *nic) registerPacketEndpoint(netProto tcpip.NetworkProtocolNumber, ep PacketEndpoint) tcpip.Error {
   932  	n.packetEPsMu.Lock()
   933  	defer n.packetEPsMu.Unlock()
   934  
   935  	eps, ok := n.packetEPs[netProto]
   936  	if !ok {
   937  		eps = new(packetEndpointList)
   938  		n.packetEPs[netProto] = eps
   939  	}
   940  	eps.add(ep)
   941  
   942  	return nil
   943  }
   944  
   945  func (n *nic) unregisterPacketEndpoint(netProto tcpip.NetworkProtocolNumber, ep PacketEndpoint) {
   946  	n.packetEPsMu.Lock()
   947  	defer n.packetEPsMu.Unlock()
   948  
   949  	eps, ok := n.packetEPs[netProto]
   950  	if !ok {
   951  		return
   952  	}
   953  	eps.remove(ep)
   954  	if eps.len() == 0 {
   955  		delete(n.packetEPs, netProto)
   956  	}
   957  }
   958  
   959  // isValidForOutgoing returns true if the endpoint can be used to send out a
   960  // packet. It requires the endpoint to not be marked expired (i.e., its address
   961  // has been removed) unless the NIC is in spoofing mode, or temporary.
   962  func (n *nic) isValidForOutgoing(ep AssignableAddressEndpoint) bool {
   963  	return n.Enabled() && ep.IsAssigned(n.Spoofing())
   964  }
   965  
   966  // HandleNeighborProbe implements NetworkInterface.
   967  func (n *nic) HandleNeighborProbe(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address, linkAddr tcpip.LinkAddress) tcpip.Error {
   968  	if l, ok := n.linkAddrResolvers[protocol]; ok {
   969  		l.neigh.handleProbe(addr, linkAddr)
   970  		return nil
   971  	}
   972  
   973  	return &tcpip.ErrNotSupported{}
   974  }
   975  
   976  // HandleNeighborConfirmation implements NetworkInterface.
   977  func (n *nic) HandleNeighborConfirmation(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address, linkAddr tcpip.LinkAddress, flags ReachabilityConfirmationFlags) tcpip.Error {
   978  	if l, ok := n.linkAddrResolvers[protocol]; ok {
   979  		l.neigh.handleConfirmation(addr, linkAddr, flags)
   980  		return nil
   981  	}
   982  
   983  	return &tcpip.ErrNotSupported{}
   984  }
   985  
   986  // CheckLocalAddress implements NetworkInterface.
   987  func (n *nic) CheckLocalAddress(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) bool {
   988  	if n.Spoofing() {
   989  		return true
   990  	}
   991  
   992  	if addressEndpoint := n.getAddressOrCreateTempInner(protocol, addr, false /* createTemp */, NeverPrimaryEndpoint); addressEndpoint != nil {
   993  		addressEndpoint.DecRef()
   994  		return true
   995  	}
   996  
   997  	return false
   998  }
   999  
  1000  func (n *nic) checkDuplicateAddress(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address, h DADCompletionHandler) (DADCheckAddressDisposition, tcpip.Error) {
  1001  	d, ok := n.duplicateAddressDetectors[protocol]
  1002  	if !ok {
  1003  		return 0, &tcpip.ErrNotSupported{}
  1004  	}
  1005  
  1006  	return d.CheckDuplicateAddress(addr, h), nil
  1007  }
  1008  
  1009  func (n *nic) setForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) (bool, tcpip.Error) {
  1010  	ep := n.getNetworkEndpoint(protocol)
  1011  	if ep == nil {
  1012  		return false, &tcpip.ErrUnknownProtocol{}
  1013  	}
  1014  
  1015  	forwardingEP, ok := ep.(ForwardingNetworkEndpoint)
  1016  	if !ok {
  1017  		return false, &tcpip.ErrNotSupported{}
  1018  	}
  1019  
  1020  	return forwardingEP.SetForwarding(enable), nil
  1021  }
  1022  
  1023  func (n *nic) forwarding(protocol tcpip.NetworkProtocolNumber) (bool, tcpip.Error) {
  1024  	ep := n.getNetworkEndpoint(protocol)
  1025  	if ep == nil {
  1026  		return false, &tcpip.ErrUnknownProtocol{}
  1027  	}
  1028  
  1029  	forwardingEP, ok := ep.(ForwardingNetworkEndpoint)
  1030  	if !ok {
  1031  		return false, &tcpip.ErrNotSupported{}
  1032  	}
  1033  
  1034  	return forwardingEP.Forwarding(), nil
  1035  }
  1036  
  1037  func (n *nic) multicastForwardingEndpoint(protocol tcpip.NetworkProtocolNumber) (MulticastForwardingNetworkEndpoint, tcpip.Error) {
  1038  	ep := n.getNetworkEndpoint(protocol)
  1039  	if ep == nil {
  1040  		return nil, &tcpip.ErrUnknownProtocol{}
  1041  	}
  1042  
  1043  	forwardingEP, ok := ep.(MulticastForwardingNetworkEndpoint)
  1044  	if !ok {
  1045  		return nil, &tcpip.ErrNotSupported{}
  1046  	}
  1047  
  1048  	return forwardingEP, nil
  1049  }
  1050  
  1051  func (n *nic) setMulticastForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) (bool, tcpip.Error) {
  1052  	ep, err := n.multicastForwardingEndpoint(protocol)
  1053  	if err != nil {
  1054  		return false, err
  1055  	}
  1056  
  1057  	return ep.SetMulticastForwarding(enable), nil
  1058  }
  1059  
  1060  func (n *nic) multicastForwarding(protocol tcpip.NetworkProtocolNumber) (bool, tcpip.Error) {
  1061  	ep, err := n.multicastForwardingEndpoint(protocol)
  1062  	if err != nil {
  1063  		return false, err
  1064  	}
  1065  
  1066  	return ep.MulticastForwarding(), nil
  1067  }