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