gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/stack/registration.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  	"time"
    20  
    21  	"gvisor.dev/gvisor/pkg/buffer"
    22  	"gvisor.dev/gvisor/pkg/tcpip"
    23  	"gvisor.dev/gvisor/pkg/tcpip/header"
    24  	"gvisor.dev/gvisor/pkg/waiter"
    25  )
    26  
    27  // NetworkEndpointID is the identifier of a network layer protocol endpoint.
    28  // Currently the local address is sufficient because all supported protocols
    29  // (i.e., IPv4 and IPv6) have different sizes for their addresses.
    30  type NetworkEndpointID struct {
    31  	LocalAddress tcpip.Address
    32  }
    33  
    34  // TransportEndpointID is the identifier of a transport layer protocol endpoint.
    35  //
    36  // +stateify savable
    37  type TransportEndpointID struct {
    38  	// LocalPort is the local port associated with the endpoint.
    39  	LocalPort uint16
    40  
    41  	// LocalAddress is the local [network layer] address associated with
    42  	// the endpoint.
    43  	LocalAddress tcpip.Address
    44  
    45  	// RemotePort is the remote port associated with the endpoint.
    46  	RemotePort uint16
    47  
    48  	// RemoteAddress it the remote [network layer] address associated with
    49  	// the endpoint.
    50  	RemoteAddress tcpip.Address
    51  }
    52  
    53  // NetworkPacketInfo holds information about a network layer packet.
    54  //
    55  // +stateify savable
    56  type NetworkPacketInfo struct {
    57  	// LocalAddressBroadcast is true if the packet's local address is a broadcast
    58  	// address.
    59  	LocalAddressBroadcast bool
    60  
    61  	// IsForwardedPacket is true if the packet is being forwarded.
    62  	IsForwardedPacket bool
    63  }
    64  
    65  // TransportErrorKind enumerates error types that are handled by the transport
    66  // layer.
    67  type TransportErrorKind int
    68  
    69  const (
    70  	// PacketTooBigTransportError indicates that a packet did not reach its
    71  	// destination because a link on the path to the destination had an MTU that
    72  	// was too small to carry the packet.
    73  	PacketTooBigTransportError TransportErrorKind = iota
    74  
    75  	// DestinationHostUnreachableTransportError indicates that the destination
    76  	// host was unreachable.
    77  	DestinationHostUnreachableTransportError
    78  
    79  	// DestinationPortUnreachableTransportError indicates that a packet reached
    80  	// the destination host, but the transport protocol was not active on the
    81  	// destination port.
    82  	DestinationPortUnreachableTransportError
    83  
    84  	// DestinationNetworkUnreachableTransportError indicates that the destination
    85  	// network was unreachable.
    86  	DestinationNetworkUnreachableTransportError
    87  
    88  	// DestinationProtoUnreachableTransportError indicates that the destination
    89  	// protocol was unreachable.
    90  	DestinationProtoUnreachableTransportError
    91  
    92  	// SourceRouteFailedTransportError indicates that the source route failed.
    93  	SourceRouteFailedTransportError
    94  
    95  	// SourceHostIsolatedTransportError indicates that the source machine is not
    96  	// on the network.
    97  	SourceHostIsolatedTransportError
    98  
    99  	// DestinationHostDownTransportError indicates that the destination host is
   100  	// down.
   101  	DestinationHostDownTransportError
   102  )
   103  
   104  // TransportError is a marker interface for errors that may be handled by the
   105  // transport layer.
   106  type TransportError interface {
   107  	tcpip.SockErrorCause
   108  
   109  	// Kind returns the type of the transport error.
   110  	Kind() TransportErrorKind
   111  }
   112  
   113  // TransportEndpoint is the interface that needs to be implemented by transport
   114  // protocol (e.g., tcp, udp) endpoints that can handle packets.
   115  type TransportEndpoint interface {
   116  	// UniqueID returns an unique ID for this transport endpoint.
   117  	UniqueID() uint64
   118  
   119  	// HandlePacket is called by the stack when new packets arrive to this
   120  	// transport endpoint. It sets the packet buffer's transport header.
   121  	//
   122  	// HandlePacket may modify the packet.
   123  	HandlePacket(TransportEndpointID, *PacketBuffer)
   124  
   125  	// HandleError is called when the transport endpoint receives an error.
   126  	//
   127  	// HandleError takes may modify the packet buffer.
   128  	HandleError(TransportError, *PacketBuffer)
   129  
   130  	// Abort initiates an expedited endpoint teardown. It puts the endpoint
   131  	// in a closed state and frees all resources associated with it. This
   132  	// cleanup may happen asynchronously. Wait can be used to block on this
   133  	// asynchronous cleanup.
   134  	Abort()
   135  
   136  	// Wait waits for any worker goroutines owned by the endpoint to stop.
   137  	//
   138  	// An endpoint can be requested to stop its worker goroutines by calling
   139  	// its Close method.
   140  	//
   141  	// Wait will not block if the endpoint hasn't started any goroutines
   142  	// yet, even if it might later.
   143  	Wait()
   144  }
   145  
   146  // RawTransportEndpoint is the interface that needs to be implemented by raw
   147  // transport protocol endpoints. RawTransportEndpoints receive the entire
   148  // packet - including the network and transport headers - as delivered to
   149  // netstack.
   150  type RawTransportEndpoint interface {
   151  	// HandlePacket is called by the stack when new packets arrive to
   152  	// this transport endpoint. The packet contains all data from the link
   153  	// layer up.
   154  	//
   155  	// HandlePacket may modify the packet.
   156  	HandlePacket(*PacketBuffer)
   157  }
   158  
   159  // PacketEndpoint is the interface that needs to be implemented by packet
   160  // transport protocol endpoints. These endpoints receive link layer headers in
   161  // addition to whatever they contain (usually network and transport layer
   162  // headers and a payload).
   163  type PacketEndpoint interface {
   164  	// HandlePacket is called by the stack when new packets arrive that
   165  	// match the endpoint.
   166  	//
   167  	// Implementers should treat packet as immutable and should copy it
   168  	// before before modification.
   169  	//
   170  	// linkHeader may have a length of 0, in which case the PacketEndpoint
   171  	// should construct its own ethernet header for applications.
   172  	//
   173  	// HandlePacket may modify pkt.
   174  	HandlePacket(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, pkt *PacketBuffer)
   175  }
   176  
   177  // UnknownDestinationPacketDisposition enumerates the possible return values from
   178  // HandleUnknownDestinationPacket().
   179  type UnknownDestinationPacketDisposition int
   180  
   181  const (
   182  	// UnknownDestinationPacketMalformed denotes that the packet was malformed
   183  	// and no further processing should be attempted other than updating
   184  	// statistics.
   185  	UnknownDestinationPacketMalformed UnknownDestinationPacketDisposition = iota
   186  
   187  	// UnknownDestinationPacketUnhandled tells the caller that the packet was
   188  	// well formed but that the issue was not handled and the stack should take
   189  	// the default action.
   190  	UnknownDestinationPacketUnhandled
   191  
   192  	// UnknownDestinationPacketHandled tells the caller that it should do
   193  	// no further processing.
   194  	UnknownDestinationPacketHandled
   195  )
   196  
   197  // TransportProtocol is the interface that needs to be implemented by transport
   198  // protocols (e.g., tcp, udp) that want to be part of the networking stack.
   199  type TransportProtocol interface {
   200  	// Number returns the transport protocol number.
   201  	Number() tcpip.TransportProtocolNumber
   202  
   203  	// NewEndpoint creates a new endpoint of the transport protocol.
   204  	NewEndpoint(netProto tcpip.NetworkProtocolNumber, waitQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error)
   205  
   206  	// NewRawEndpoint creates a new raw endpoint of the transport protocol.
   207  	NewRawEndpoint(netProto tcpip.NetworkProtocolNumber, waitQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error)
   208  
   209  	// MinimumPacketSize returns the minimum valid packet size of this
   210  	// transport protocol. The stack automatically drops any packets smaller
   211  	// than this targeted at this protocol.
   212  	MinimumPacketSize() int
   213  
   214  	// ParsePorts returns the source and destination ports stored in a
   215  	// packet of this protocol.
   216  	ParsePorts(b []byte) (src, dst uint16, err tcpip.Error)
   217  
   218  	// HandleUnknownDestinationPacket handles packets targeted at this
   219  	// protocol that don't match any existing endpoint. For example,
   220  	// it is targeted at a port that has no listeners.
   221  	//
   222  	// HandleUnknownDestinationPacket may modify the packet if it handles
   223  	// the issue.
   224  	HandleUnknownDestinationPacket(TransportEndpointID, *PacketBuffer) UnknownDestinationPacketDisposition
   225  
   226  	// SetOption allows enabling/disabling protocol specific features.
   227  	// SetOption returns an error if the option is not supported or the
   228  	// provided option value is invalid.
   229  	SetOption(option tcpip.SettableTransportProtocolOption) tcpip.Error
   230  
   231  	// Option allows retrieving protocol specific option values.
   232  	// Option returns an error if the option is not supported or the
   233  	// provided option value is invalid.
   234  	Option(option tcpip.GettableTransportProtocolOption) tcpip.Error
   235  
   236  	// Close requests that any worker goroutines owned by the protocol
   237  	// stop.
   238  	Close()
   239  
   240  	// Wait waits for any worker goroutines owned by the protocol to stop.
   241  	Wait()
   242  
   243  	// Pause requests that any protocol level background workers pause.
   244  	Pause()
   245  
   246  	// Resume resumes any protocol level background workers that were
   247  	// previously paused by Pause.
   248  	Resume()
   249  
   250  	// Parse sets pkt.TransportHeader and trims pkt.Data appropriately. It does
   251  	// neither and returns false if pkt.Data is too small, i.e. pkt.Data.Size() <
   252  	// MinimumPacketSize()
   253  	Parse(pkt *PacketBuffer) (ok bool)
   254  }
   255  
   256  // TransportPacketDisposition is the result from attempting to deliver a packet
   257  // to the transport layer.
   258  type TransportPacketDisposition int
   259  
   260  const (
   261  	// TransportPacketHandled indicates that a transport packet was handled by the
   262  	// transport layer and callers need not take any further action.
   263  	TransportPacketHandled TransportPacketDisposition = iota
   264  
   265  	// TransportPacketProtocolUnreachable indicates that the transport
   266  	// protocol requested in the packet is not supported.
   267  	TransportPacketProtocolUnreachable
   268  
   269  	// TransportPacketDestinationPortUnreachable indicates that there weren't any
   270  	// listeners interested in the packet and the transport protocol has no means
   271  	// to notify the sender.
   272  	TransportPacketDestinationPortUnreachable
   273  )
   274  
   275  // TransportDispatcher contains the methods used by the network stack to deliver
   276  // packets to the appropriate transport endpoint after it has been handled by
   277  // the network layer.
   278  type TransportDispatcher interface {
   279  	// DeliverTransportPacket delivers packets to the appropriate
   280  	// transport protocol endpoint.
   281  	//
   282  	// pkt.NetworkHeader must be set before calling DeliverTransportPacket.
   283  	//
   284  	// DeliverTransportPacket may modify the packet.
   285  	DeliverTransportPacket(tcpip.TransportProtocolNumber, *PacketBuffer) TransportPacketDisposition
   286  
   287  	// DeliverTransportError delivers an error to the appropriate transport
   288  	// endpoint.
   289  	//
   290  	// DeliverTransportError may modify the packet buffer.
   291  	DeliverTransportError(local, remote tcpip.Address, _ tcpip.NetworkProtocolNumber, _ tcpip.TransportProtocolNumber, _ TransportError, _ *PacketBuffer)
   292  
   293  	// DeliverRawPacket delivers a packet to any subscribed raw sockets.
   294  	//
   295  	// DeliverRawPacket does NOT take ownership of the packet buffer.
   296  	DeliverRawPacket(tcpip.TransportProtocolNumber, *PacketBuffer)
   297  }
   298  
   299  // PacketLooping specifies where an outbound packet should be sent.
   300  type PacketLooping byte
   301  
   302  const (
   303  	// PacketOut indicates that the packet should be passed to the link
   304  	// endpoint.
   305  	PacketOut PacketLooping = 1 << iota
   306  
   307  	// PacketLoop indicates that the packet should be handled locally.
   308  	PacketLoop
   309  )
   310  
   311  // NetworkHeaderParams are the header parameters given as input by the
   312  // transport endpoint to the network.
   313  type NetworkHeaderParams struct {
   314  	// Protocol refers to the transport protocol number.
   315  	Protocol tcpip.TransportProtocolNumber
   316  
   317  	// TTL refers to Time To Live field of the IP-header.
   318  	TTL uint8
   319  
   320  	// TOS refers to TypeOfService or TrafficClass field of the IP-header.
   321  	TOS uint8
   322  
   323  	// DF indicates whether the DF bit should be set.
   324  	DF bool
   325  }
   326  
   327  // GroupAddressableEndpoint is an endpoint that supports group addressing.
   328  //
   329  // An endpoint is considered to support group addressing when one or more
   330  // endpoints may associate themselves with the same identifier (group address).
   331  type GroupAddressableEndpoint interface {
   332  	// JoinGroup joins the specified group.
   333  	JoinGroup(group tcpip.Address) tcpip.Error
   334  
   335  	// LeaveGroup attempts to leave the specified group.
   336  	LeaveGroup(group tcpip.Address) tcpip.Error
   337  
   338  	// IsInGroup returns true if the endpoint is a member of the specified group.
   339  	IsInGroup(group tcpip.Address) bool
   340  }
   341  
   342  // PrimaryEndpointBehavior is an enumeration of an AddressEndpoint's primary
   343  // behavior.
   344  type PrimaryEndpointBehavior int
   345  
   346  const (
   347  	// CanBePrimaryEndpoint indicates the endpoint can be used as a primary
   348  	// endpoint for new connections with no local address.
   349  	CanBePrimaryEndpoint PrimaryEndpointBehavior = iota
   350  
   351  	// FirstPrimaryEndpoint indicates the endpoint should be the first
   352  	// primary endpoint considered. If there are multiple endpoints with
   353  	// this behavior, they are ordered by recency.
   354  	FirstPrimaryEndpoint
   355  
   356  	// NeverPrimaryEndpoint indicates the endpoint should never be a
   357  	// primary endpoint.
   358  	NeverPrimaryEndpoint
   359  )
   360  
   361  func (peb PrimaryEndpointBehavior) String() string {
   362  	switch peb {
   363  	case CanBePrimaryEndpoint:
   364  		return "CanBePrimaryEndpoint"
   365  	case FirstPrimaryEndpoint:
   366  		return "FirstPrimaryEndpoint"
   367  	case NeverPrimaryEndpoint:
   368  		return "NeverPrimaryEndpoint"
   369  	default:
   370  		panic(fmt.Sprintf("unknown primary endpoint behavior: %d", peb))
   371  	}
   372  }
   373  
   374  // AddressConfigType is the method used to add an address.
   375  type AddressConfigType int
   376  
   377  const (
   378  	// AddressConfigStatic is a statically configured address endpoint that was
   379  	// added by some user-specified action (adding an explicit address, joining a
   380  	// multicast group).
   381  	AddressConfigStatic AddressConfigType = iota
   382  
   383  	// AddressConfigSlaac is an address endpoint added by SLAAC, as per RFC 4862
   384  	// section 5.5.3.
   385  	AddressConfigSlaac
   386  )
   387  
   388  // AddressLifetimes encodes an address' preferred and valid lifetimes, as well
   389  // as if the address is deprecated.
   390  type AddressLifetimes struct {
   391  	// Deprecated is whether the address is deprecated.
   392  	Deprecated bool
   393  
   394  	// PreferredUntil is the time at which the address will be deprecated.
   395  	//
   396  	// Note that for certain addresses, deprecating the address at the
   397  	// PreferredUntil time is not handled as a scheduled job by the stack, but
   398  	// is information provided by the owner as an indication of when it will
   399  	// deprecate the address.
   400  	//
   401  	// PreferredUntil should be ignored if Deprecated is true. If Deprecated
   402  	// is false, and PreferredUntil is the zero value, no information about
   403  	// the preferred lifetime can be inferred.
   404  	PreferredUntil tcpip.MonotonicTime
   405  
   406  	// ValidUntil is the time at which the address will be invalidated.
   407  	//
   408  	// Note that for certain addresses, invalidating the address at the
   409  	// ValidUntil time is not handled as a scheduled job by the stack, but
   410  	// is information provided by the owner as an indication of when it will
   411  	// invalidate the address.
   412  	//
   413  	// If ValidUntil is the zero value, no information about the valid lifetime
   414  	// can be inferred.
   415  	ValidUntil tcpip.MonotonicTime
   416  }
   417  
   418  // AddressProperties contains additional properties that can be configured when
   419  // adding an address.
   420  type AddressProperties struct {
   421  	PEB        PrimaryEndpointBehavior
   422  	ConfigType AddressConfigType
   423  	// Lifetimes encodes the address' lifetimes.
   424  	//
   425  	// Lifetimes.PreferredUntil and Lifetimes.ValidUntil are informational, i.e.
   426  	// the stack will not deprecated nor invalidate the address upon reaching
   427  	// these timestamps.
   428  	//
   429  	// If Lifetimes.Deprecated is true, the address will be added as deprecated.
   430  	Lifetimes AddressLifetimes
   431  	// Temporary is as defined in RFC 4941, but applies not only to addresses
   432  	// added via SLAAC, e.g. DHCPv6 can also add temporary addresses. Temporary
   433  	// addresses are short-lived and are not to be valid (or preferred)
   434  	// forever; hence the term temporary.
   435  	Temporary bool
   436  	Disp      AddressDispatcher
   437  }
   438  
   439  // AddressAssignmentState is an address' assignment state.
   440  type AddressAssignmentState int
   441  
   442  const (
   443  	_ AddressAssignmentState = iota
   444  
   445  	// AddressDisabled indicates the NIC the address is assigned to is disabled.
   446  	AddressDisabled
   447  
   448  	// AddressTentative indicates an address is yet to pass DAD (IPv4 addresses
   449  	// are never tentative).
   450  	AddressTentative
   451  
   452  	// AddressAssigned indicates an address is assigned.
   453  	AddressAssigned
   454  )
   455  
   456  func (state AddressAssignmentState) String() string {
   457  	switch state {
   458  	case AddressDisabled:
   459  		return "Disabled"
   460  	case AddressTentative:
   461  		return "Tentative"
   462  	case AddressAssigned:
   463  		return "Assigned"
   464  	default:
   465  		panic(fmt.Sprintf("unknown address assignment state: %d", state))
   466  	}
   467  }
   468  
   469  // AddressRemovalReason is the reason an address was removed.
   470  type AddressRemovalReason int
   471  
   472  const (
   473  	_ AddressRemovalReason = iota
   474  
   475  	// AddressRemovalManualAction indicates the address was removed explicitly
   476  	// using the stack API.
   477  	AddressRemovalManualAction
   478  
   479  	// AddressRemovalInterfaceRemoved indicates the address was removed because
   480  	// the NIC it is assigned to was removed.
   481  	AddressRemovalInterfaceRemoved
   482  
   483  	// AddressRemovalDADFailed indicates the address was removed because DAD
   484  	// failed.
   485  	AddressRemovalDADFailed
   486  
   487  	// AddressRemovalInvalidated indicates the address was removed because it
   488  	// was invalidated.
   489  	AddressRemovalInvalidated
   490  )
   491  
   492  func (reason AddressRemovalReason) String() string {
   493  	switch reason {
   494  	case AddressRemovalManualAction:
   495  		return "ManualAction"
   496  	case AddressRemovalInterfaceRemoved:
   497  		return "InterfaceRemoved"
   498  	case AddressRemovalDADFailed:
   499  		return "DADFailed"
   500  	case AddressRemovalInvalidated:
   501  		return "Invalidated"
   502  	default:
   503  		panic(fmt.Sprintf("unknown address removal reason: %d", reason))
   504  	}
   505  }
   506  
   507  // AddressDispatcher is the interface integrators can implement to receive
   508  // address-related events.
   509  type AddressDispatcher interface {
   510  	// OnChanged is called with an address' properties when they change.
   511  	//
   512  	// OnChanged is called once when the address is added with the initial state,
   513  	// and every time a property changes.
   514  	//
   515  	// The PreferredUntil and ValidUntil fields in AddressLifetimes must be
   516  	// considered informational, i.e. one must not consider an address to be
   517  	// deprecated/invalid even if the monotonic clock timestamp is past these
   518  	// deadlines. The Deprecated field indicates whether an address is
   519  	// preferred or not; and OnRemoved will be called when an address is
   520  	// removed due to invalidation.
   521  	OnChanged(AddressLifetimes, AddressAssignmentState)
   522  
   523  	// OnRemoved is called when an address is removed with the removal reason.
   524  	OnRemoved(AddressRemovalReason)
   525  }
   526  
   527  // AssignableAddressEndpoint is a reference counted address endpoint that may be
   528  // assigned to a NetworkEndpoint.
   529  type AssignableAddressEndpoint interface {
   530  	// AddressWithPrefix returns the endpoint's address.
   531  	AddressWithPrefix() tcpip.AddressWithPrefix
   532  
   533  	// Subnet returns the subnet of the endpoint's address.
   534  	Subnet() tcpip.Subnet
   535  
   536  	// IsAssigned returns whether or not the endpoint is considered bound
   537  	// to its NetworkEndpoint.
   538  	IsAssigned(allowExpired bool) bool
   539  
   540  	// TryIncRef tries to increment this endpoint's reference count.
   541  	//
   542  	// Returns true if it was successfully incremented. If it returns false, then
   543  	// the endpoint is considered expired and should no longer be used.
   544  	TryIncRef() bool
   545  
   546  	// DecRef decrements this endpoint's reference count.
   547  	DecRef()
   548  }
   549  
   550  // AddressEndpoint is an endpoint representing an address assigned to an
   551  // AddressableEndpoint.
   552  type AddressEndpoint interface {
   553  	AssignableAddressEndpoint
   554  
   555  	// GetKind returns the address kind for this endpoint.
   556  	GetKind() AddressKind
   557  
   558  	// SetKind sets the address kind for this endpoint.
   559  	SetKind(AddressKind)
   560  
   561  	// ConfigType returns the method used to add the address.
   562  	ConfigType() AddressConfigType
   563  
   564  	// Deprecated returns whether or not this endpoint is deprecated.
   565  	Deprecated() bool
   566  
   567  	// SetDeprecated sets this endpoint's deprecated status.
   568  	SetDeprecated(bool)
   569  
   570  	// Lifetimes returns this endpoint's lifetimes.
   571  	Lifetimes() AddressLifetimes
   572  
   573  	// SetLifetimes sets this endpoint's lifetimes.
   574  	//
   575  	// Note that setting preferred-until and valid-until times do not result in
   576  	// deprecation/invalidation jobs to be scheduled by the stack.
   577  	SetLifetimes(AddressLifetimes)
   578  
   579  	// Temporary returns whether or not this endpoint is temporary.
   580  	Temporary() bool
   581  
   582  	// RegisterDispatcher registers an address dispatcher.
   583  	//
   584  	// OnChanged will be called immediately on the provided address dispatcher
   585  	// with this endpoint's current state.
   586  	RegisterDispatcher(AddressDispatcher)
   587  }
   588  
   589  // AddressKind is the kind of an address.
   590  //
   591  // See the values of AddressKind for more details.
   592  type AddressKind int
   593  
   594  const (
   595  	// PermanentTentative is a permanent address endpoint that is not yet
   596  	// considered to be fully bound to an interface in the traditional
   597  	// sense. That is, the address is associated with a NIC, but packets
   598  	// destined to the address MUST NOT be accepted and MUST be silently
   599  	// dropped, and the address MUST NOT be used as a source address for
   600  	// outgoing packets. For IPv6, addresses are of this kind until NDP's
   601  	// Duplicate Address Detection (DAD) resolves. If DAD fails, the address
   602  	// is removed.
   603  	PermanentTentative AddressKind = iota
   604  
   605  	// Permanent is a permanent endpoint (vs. a temporary one) assigned to the
   606  	// NIC. Its reference count is biased by 1 to avoid removal when no route
   607  	// holds a reference to it. It is removed by explicitly removing the address
   608  	// from the NIC.
   609  	Permanent
   610  
   611  	// PermanentExpired is a permanent endpoint that had its address removed from
   612  	// the NIC, and it is waiting to be removed once no references to it are held.
   613  	//
   614  	// If the address is re-added before the endpoint is removed, its type
   615  	// changes back to Permanent.
   616  	PermanentExpired
   617  
   618  	// Temporary is an endpoint, created on a one-off basis to temporarily
   619  	// consider the NIC bound an an address that it is not explicitly bound to
   620  	// (such as a permanent address). Its reference count must not be biased by 1
   621  	// so that the address is removed immediately when references to it are no
   622  	// longer held.
   623  	//
   624  	// A temporary endpoint may be promoted to permanent if the address is added
   625  	// permanently.
   626  	Temporary
   627  )
   628  
   629  // IsPermanent returns true if the AddressKind represents a permanent address.
   630  func (k AddressKind) IsPermanent() bool {
   631  	switch k {
   632  	case Permanent, PermanentTentative:
   633  		return true
   634  	case Temporary, PermanentExpired:
   635  		return false
   636  	default:
   637  		panic(fmt.Sprintf("unrecognized address kind = %d", k))
   638  	}
   639  }
   640  
   641  // AddressableEndpoint is an endpoint that supports addressing.
   642  //
   643  // An endpoint is considered to support addressing when the endpoint may
   644  // associate itself with an identifier (address).
   645  type AddressableEndpoint interface {
   646  	// AddAndAcquirePermanentAddress adds the passed permanent address.
   647  	//
   648  	// Returns *tcpip.ErrDuplicateAddress if the address exists.
   649  	//
   650  	// Acquires and returns the AddressEndpoint for the added address.
   651  	AddAndAcquirePermanentAddress(addr tcpip.AddressWithPrefix, properties AddressProperties) (AddressEndpoint, tcpip.Error)
   652  
   653  	// RemovePermanentAddress removes the passed address if it is a permanent
   654  	// address.
   655  	//
   656  	// Returns *tcpip.ErrBadLocalAddress if the endpoint does not have the passed
   657  	// permanent address.
   658  	RemovePermanentAddress(addr tcpip.Address) tcpip.Error
   659  
   660  	// SetLifetimes sets an address' lifetimes (strictly informational) and
   661  	// whether it should be deprecated or preferred.
   662  	//
   663  	// Returns *tcpip.ErrBadLocalAddress if the endpoint does not have the passed
   664  	// address.
   665  	SetLifetimes(addr tcpip.Address, lifetimes AddressLifetimes) tcpip.Error
   666  
   667  	// MainAddress returns the endpoint's primary permanent address.
   668  	MainAddress() tcpip.AddressWithPrefix
   669  
   670  	// AcquireAssignedAddress returns an address endpoint for the passed address
   671  	// that is considered bound to the endpoint, optionally creating a temporary
   672  	// endpoint if requested and no existing address exists.
   673  	//
   674  	// The returned endpoint's reference count is incremented if readOnly is
   675  	// false.
   676  	//
   677  	// Returns nil if the specified address is not local to this endpoint.
   678  	AcquireAssignedAddress(localAddr tcpip.Address, allowTemp bool, tempPEB PrimaryEndpointBehavior, readOnly bool) AddressEndpoint
   679  
   680  	// AcquireOutgoingPrimaryAddress returns a primary address that may be used as
   681  	// a source address when sending packets to the passed remote address.
   682  	//
   683  	// If allowExpired is true, expired addresses may be returned.
   684  	//
   685  	// The returned endpoint's reference count is incremented.
   686  	//
   687  	// Returns nil if a primary address is not available.
   688  	AcquireOutgoingPrimaryAddress(remoteAddr, srcHint tcpip.Address, allowExpired bool) AddressEndpoint
   689  
   690  	// PrimaryAddresses returns the primary addresses.
   691  	PrimaryAddresses() []tcpip.AddressWithPrefix
   692  
   693  	// PermanentAddresses returns all the permanent addresses.
   694  	PermanentAddresses() []tcpip.AddressWithPrefix
   695  }
   696  
   697  // NDPEndpoint is a network endpoint that supports NDP.
   698  type NDPEndpoint interface {
   699  	NetworkEndpoint
   700  
   701  	// InvalidateDefaultRouter invalidates a default router discovered through
   702  	// NDP.
   703  	InvalidateDefaultRouter(tcpip.Address)
   704  }
   705  
   706  // NetworkInterface is a network interface.
   707  type NetworkInterface interface {
   708  	NetworkLinkEndpoint
   709  
   710  	// ID returns the interface's ID.
   711  	ID() tcpip.NICID
   712  
   713  	// IsLoopback returns true if the interface is a loopback interface.
   714  	IsLoopback() bool
   715  
   716  	// Name returns the name of the interface.
   717  	//
   718  	// May return an empty string if the interface is not configured with a name.
   719  	Name() string
   720  
   721  	// Enabled returns true if the interface is enabled.
   722  	Enabled() bool
   723  
   724  	// Promiscuous returns true if the interface is in promiscuous mode.
   725  	//
   726  	// When in promiscuous mode, the interface should accept all packets.
   727  	Promiscuous() bool
   728  
   729  	// Spoofing returns true if the interface is in spoofing mode.
   730  	//
   731  	// When in spoofing mode, the interface should consider all addresses as
   732  	// assigned to it.
   733  	Spoofing() bool
   734  
   735  	// PrimaryAddress returns the primary address associated with the interface.
   736  	//
   737  	// PrimaryAddress will return the first non-deprecated address if such an
   738  	// address exists. If no non-deprecated addresses exist, the first deprecated
   739  	// address will be returned. If no deprecated addresses exist, the zero value
   740  	// will be returned.
   741  	PrimaryAddress(tcpip.NetworkProtocolNumber) (tcpip.AddressWithPrefix, tcpip.Error)
   742  
   743  	// CheckLocalAddress returns true if the address exists on the interface.
   744  	CheckLocalAddress(tcpip.NetworkProtocolNumber, tcpip.Address) bool
   745  
   746  	// WritePacketToRemote writes the packet to the given remote link address.
   747  	WritePacketToRemote(tcpip.LinkAddress, *PacketBuffer) tcpip.Error
   748  
   749  	// WritePacket writes a packet through the given route.
   750  	//
   751  	// WritePacket may modify the packet buffer. The packet buffer's
   752  	// network and transport header must be set.
   753  	WritePacket(*Route, *PacketBuffer) tcpip.Error
   754  
   755  	// HandleNeighborProbe processes an incoming neighbor probe (e.g. ARP
   756  	// request or NDP Neighbor Solicitation).
   757  	//
   758  	// HandleNeighborProbe assumes that the probe is valid for the network
   759  	// interface the probe was received on.
   760  	HandleNeighborProbe(tcpip.NetworkProtocolNumber, tcpip.Address, tcpip.LinkAddress) tcpip.Error
   761  
   762  	// HandleNeighborConfirmation processes an incoming neighbor confirmation
   763  	// (e.g. ARP reply or NDP Neighbor Advertisement).
   764  	HandleNeighborConfirmation(tcpip.NetworkProtocolNumber, tcpip.Address, tcpip.LinkAddress, ReachabilityConfirmationFlags) tcpip.Error
   765  }
   766  
   767  // LinkResolvableNetworkEndpoint handles link resolution events.
   768  type LinkResolvableNetworkEndpoint interface {
   769  	// HandleLinkResolutionFailure is called when link resolution prevents the
   770  	// argument from having been sent.
   771  	HandleLinkResolutionFailure(*PacketBuffer)
   772  }
   773  
   774  // NetworkEndpoint is the interface that needs to be implemented by endpoints
   775  // of network layer protocols (e.g., ipv4, ipv6).
   776  type NetworkEndpoint interface {
   777  	// Enable enables the endpoint.
   778  	//
   779  	// Must only be called when the stack is in a state that allows the endpoint
   780  	// to send and receive packets.
   781  	//
   782  	// Returns *tcpip.ErrNotPermitted if the endpoint cannot be enabled.
   783  	Enable() tcpip.Error
   784  
   785  	// Enabled returns true if the endpoint is enabled.
   786  	Enabled() bool
   787  
   788  	// Disable disables the endpoint.
   789  	Disable()
   790  
   791  	// DefaultTTL is the default time-to-live value (or hop limit, in ipv6)
   792  	// for this endpoint.
   793  	DefaultTTL() uint8
   794  
   795  	// MTU is the maximum transmission unit for this endpoint. This is
   796  	// generally calculated as the MTU of the underlying data link endpoint
   797  	// minus the network endpoint max header length.
   798  	MTU() uint32
   799  
   800  	// MaxHeaderLength returns the maximum size the network (and lower
   801  	// level layers combined) headers can have. Higher levels use this
   802  	// information to reserve space in the front of the packets they're
   803  	// building.
   804  	MaxHeaderLength() uint16
   805  
   806  	// WritePacket writes a packet to the given destination address and
   807  	// protocol. It may modify pkt. pkt.TransportHeader must have
   808  	// already been set.
   809  	WritePacket(r *Route, params NetworkHeaderParams, pkt *PacketBuffer) tcpip.Error
   810  
   811  	// WriteHeaderIncludedPacket writes a packet that includes a network
   812  	// header to the given destination address. It may modify pkt.
   813  	WriteHeaderIncludedPacket(r *Route, pkt *PacketBuffer) tcpip.Error
   814  
   815  	// HandlePacket is called by the link layer when new packets arrive to
   816  	// this network endpoint. It sets pkt.NetworkHeader.
   817  	//
   818  	// HandlePacket may modify pkt.
   819  	HandlePacket(pkt *PacketBuffer)
   820  
   821  	// Close is called when the endpoint is removed from a stack.
   822  	Close()
   823  
   824  	// NetworkProtocolNumber returns the tcpip.NetworkProtocolNumber for
   825  	// this endpoint.
   826  	NetworkProtocolNumber() tcpip.NetworkProtocolNumber
   827  
   828  	// Stats returns a reference to the network endpoint stats.
   829  	Stats() NetworkEndpointStats
   830  }
   831  
   832  // NetworkEndpointStats is the interface implemented by each network endpoint
   833  // stats struct.
   834  type NetworkEndpointStats interface {
   835  	// IsNetworkEndpointStats is an empty method to implement the
   836  	// NetworkEndpointStats marker interface.
   837  	IsNetworkEndpointStats()
   838  }
   839  
   840  // IPNetworkEndpointStats is a NetworkEndpointStats that tracks IP-related
   841  // statistics.
   842  type IPNetworkEndpointStats interface {
   843  	NetworkEndpointStats
   844  
   845  	// IPStats returns the IP statistics of a network endpoint.
   846  	IPStats() *tcpip.IPStats
   847  }
   848  
   849  // ForwardingNetworkEndpoint is a network endpoint that may forward packets.
   850  type ForwardingNetworkEndpoint interface {
   851  	NetworkEndpoint
   852  
   853  	// Forwarding returns the forwarding configuration.
   854  	Forwarding() bool
   855  
   856  	// SetForwarding sets the forwarding configuration.
   857  	//
   858  	// Returns the previous forwarding configuration.
   859  	SetForwarding(bool) bool
   860  }
   861  
   862  // MulticastForwardingNetworkEndpoint is a network endpoint that may forward
   863  // multicast packets.
   864  type MulticastForwardingNetworkEndpoint interface {
   865  	ForwardingNetworkEndpoint
   866  
   867  	// MulticastForwarding returns true if multicast forwarding is enabled.
   868  	// Otherwise, returns false.
   869  	MulticastForwarding() bool
   870  
   871  	// SetMulticastForwarding sets the multicast forwarding configuration.
   872  	//
   873  	// Returns the previous forwarding configuration.
   874  	SetMulticastForwarding(bool) bool
   875  }
   876  
   877  // NetworkProtocol is the interface that needs to be implemented by network
   878  // protocols (e.g., ipv4, ipv6) that want to be part of the networking stack.
   879  type NetworkProtocol interface {
   880  	// Number returns the network protocol number.
   881  	Number() tcpip.NetworkProtocolNumber
   882  
   883  	// MinimumPacketSize returns the minimum valid packet size of this
   884  	// network protocol. The stack automatically drops any packets smaller
   885  	// than this targeted at this protocol.
   886  	MinimumPacketSize() int
   887  
   888  	// ParseAddresses returns the source and destination addresses stored in a
   889  	// packet of this protocol.
   890  	ParseAddresses(b []byte) (src, dst tcpip.Address)
   891  
   892  	// NewEndpoint creates a new endpoint of this protocol.
   893  	NewEndpoint(nic NetworkInterface, dispatcher TransportDispatcher) NetworkEndpoint
   894  
   895  	// SetOption allows enabling/disabling protocol specific features.
   896  	// SetOption returns an error if the option is not supported or the
   897  	// provided option value is invalid.
   898  	SetOption(option tcpip.SettableNetworkProtocolOption) tcpip.Error
   899  
   900  	// Option allows retrieving protocol specific option values.
   901  	// Option returns an error if the option is not supported or the
   902  	// provided option value is invalid.
   903  	Option(option tcpip.GettableNetworkProtocolOption) tcpip.Error
   904  
   905  	// Close requests that any worker goroutines owned by the protocol
   906  	// stop.
   907  	Close()
   908  
   909  	// Wait waits for any worker goroutines owned by the protocol to stop.
   910  	Wait()
   911  
   912  	// Parse sets pkt.NetworkHeader and trims pkt.Data appropriately. It
   913  	// returns:
   914  	//	- The encapsulated protocol, if present.
   915  	//	- Whether there is an encapsulated transport protocol payload (e.g. ARP
   916  	//		does not encapsulate anything).
   917  	//	- Whether pkt.Data was large enough to parse and set pkt.NetworkHeader.
   918  	Parse(pkt *PacketBuffer) (proto tcpip.TransportProtocolNumber, hasTransportHdr bool, ok bool)
   919  }
   920  
   921  // UnicastSourceAndMulticastDestination is a tuple that represents a unicast
   922  // source address and a multicast destination address.
   923  type UnicastSourceAndMulticastDestination struct {
   924  	// Source represents a unicast source address.
   925  	Source tcpip.Address
   926  	// Destination represents a multicast destination address.
   927  	Destination tcpip.Address
   928  }
   929  
   930  // MulticastRouteOutgoingInterface represents an outgoing interface in a
   931  // multicast route.
   932  type MulticastRouteOutgoingInterface struct {
   933  	// ID corresponds to the outgoing NIC.
   934  	ID tcpip.NICID
   935  
   936  	// MinTTL represents the minimum TTL/HopLimit a multicast packet must have to
   937  	// be sent through the outgoing interface.
   938  	//
   939  	// Note: a value of 0 allows all packets to be forwarded.
   940  	MinTTL uint8
   941  }
   942  
   943  // MulticastRoute is a multicast route.
   944  type MulticastRoute struct {
   945  	// ExpectedInputInterface is the interface on which packets using this route
   946  	// are expected to ingress.
   947  	ExpectedInputInterface tcpip.NICID
   948  
   949  	// OutgoingInterfaces is the set of interfaces that a multicast packet should
   950  	// be forwarded out of.
   951  	//
   952  	// This field should not be empty.
   953  	OutgoingInterfaces []MulticastRouteOutgoingInterface
   954  }
   955  
   956  // MulticastForwardingNetworkProtocol is the interface that needs to be
   957  // implemented by the network protocols that support multicast forwarding.
   958  type MulticastForwardingNetworkProtocol interface {
   959  	NetworkProtocol
   960  
   961  	// AddMulticastRoute adds a route to the multicast routing table such that
   962  	// packets matching the addresses will be forwarded using the provided route.
   963  	//
   964  	// Returns an error if the addresses or route is invalid.
   965  	AddMulticastRoute(UnicastSourceAndMulticastDestination, MulticastRoute) tcpip.Error
   966  
   967  	// RemoveMulticastRoute removes the route matching the provided addresses
   968  	// from the multicast routing table.
   969  	//
   970  	// Returns an error if the addresses are invalid or a matching route is not
   971  	// found.
   972  	RemoveMulticastRoute(UnicastSourceAndMulticastDestination) tcpip.Error
   973  
   974  	// MulticastRouteLastUsedTime returns a monotonic timestamp that
   975  	// represents the last time that the route matching the provided addresses
   976  	// was used or updated.
   977  	//
   978  	// Returns an error if the addresses are invalid or a matching route was not
   979  	// found.
   980  	MulticastRouteLastUsedTime(UnicastSourceAndMulticastDestination) (tcpip.MonotonicTime, tcpip.Error)
   981  
   982  	// EnableMulticastForwarding enables multicast forwarding for the protocol.
   983  	//
   984  	// Returns an error if the provided multicast forwarding event dispatcher is
   985  	// nil. Otherwise, returns true if the multicast forwarding was already
   986  	// enabled.
   987  	EnableMulticastForwarding(MulticastForwardingEventDispatcher) (bool, tcpip.Error)
   988  
   989  	// DisableMulticastForwarding disables multicast forwarding for the protocol.
   990  	DisableMulticastForwarding()
   991  }
   992  
   993  // MulticastPacketContext is the context in which a multicast packet triggered
   994  // a multicast forwarding event.
   995  type MulticastPacketContext struct {
   996  	// SourceAndDestination contains the unicast source address and the multicast
   997  	// destination address found in the relevant multicast packet.
   998  	SourceAndDestination UnicastSourceAndMulticastDestination
   999  	// InputInterface is the interface on which the relevant multicast packet
  1000  	// arrived.
  1001  	InputInterface tcpip.NICID
  1002  }
  1003  
  1004  // MulticastForwardingEventDispatcher is the interface that integrators should
  1005  // implement to handle multicast routing events.
  1006  type MulticastForwardingEventDispatcher interface {
  1007  	// OnMissingRoute is called when an incoming multicast packet does not match
  1008  	// any installed route.
  1009  	//
  1010  	// The packet that triggered this event may be queued so that it can be
  1011  	// transmitted once a route is installed. Even then, it may still be dropped
  1012  	// as per the routing table's GC/eviction policy.
  1013  	OnMissingRoute(MulticastPacketContext)
  1014  
  1015  	// OnUnexpectedInputInterface is called when a multicast packet arrives at an
  1016  	// interface that does not match the installed route's expected input
  1017  	// interface.
  1018  	//
  1019  	// This may be an indication of a routing loop. The packet that triggered
  1020  	// this event is dropped without being forwarded.
  1021  	OnUnexpectedInputInterface(context MulticastPacketContext, expectedInputInterface tcpip.NICID)
  1022  }
  1023  
  1024  // NetworkDispatcher contains the methods used by the network stack to deliver
  1025  // inbound/outbound packets to the appropriate network/packet(if any) endpoints.
  1026  type NetworkDispatcher interface {
  1027  	// DeliverNetworkPacket finds the appropriate network protocol endpoint
  1028  	// and hands the packet over for further processing.
  1029  	//
  1030  	//
  1031  	// If the link-layer has a header, the packet's link header must be populated.
  1032  	//
  1033  	// DeliverNetworkPacket may modify pkt.
  1034  	DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer)
  1035  
  1036  	// DeliverLinkPacket delivers a packet to any interested packet endpoints.
  1037  	//
  1038  	// This method should be called with both incoming and outgoing packets.
  1039  	//
  1040  	// If the link-layer has a header, the packet's link header must be populated.
  1041  	DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer)
  1042  }
  1043  
  1044  // LinkEndpointCapabilities is the type associated with the capabilities
  1045  // supported by a link-layer endpoint. It is a set of bitfields.
  1046  type LinkEndpointCapabilities uint
  1047  
  1048  // The following are the supported link endpoint capabilities.
  1049  const (
  1050  	CapabilityNone LinkEndpointCapabilities = 0
  1051  	// CapabilityTXChecksumOffload indicates that the link endpoint supports
  1052  	// checksum computation for outgoing packets and the stack can skip
  1053  	// computing checksums when sending packets.
  1054  	CapabilityTXChecksumOffload LinkEndpointCapabilities = 1 << iota
  1055  	// CapabilityRXChecksumOffload indicates that the link endpoint supports
  1056  	// checksum verification on received packets and that it's safe for the
  1057  	// stack to skip checksum verification.
  1058  	CapabilityRXChecksumOffload
  1059  	CapabilityResolutionRequired
  1060  	CapabilitySaveRestore
  1061  	CapabilityDisconnectOk
  1062  	CapabilityLoopback
  1063  )
  1064  
  1065  // LinkWriter is an interface that supports sending packets via a data-link
  1066  // layer endpoint. It is used with QueueingDiscipline to batch writes from
  1067  // upper layer endpoints.
  1068  type LinkWriter interface {
  1069  	// WritePackets writes packets. Must not be called with an empty list of
  1070  	// packet buffers.
  1071  	//
  1072  	// Each packet must have the link-layer header set, if the link requires
  1073  	// one.
  1074  	//
  1075  	// WritePackets may modify the packet buffers, and takes ownership of the PacketBufferList.
  1076  	// it is not safe to use the PacketBufferList after a call to WritePackets.
  1077  	WritePackets(PacketBufferList) (int, tcpip.Error)
  1078  }
  1079  
  1080  // NetworkLinkEndpoint is a data-link layer that supports sending network
  1081  // layer packets.
  1082  type NetworkLinkEndpoint interface {
  1083  	// MTU is the maximum transmission unit for this endpoint. This is
  1084  	// usually dictated by the backing physical network; when such a
  1085  	// physical network doesn't exist, the limit is generally 64k, which
  1086  	// includes the maximum size of an IP packet.
  1087  	MTU() uint32
  1088  
  1089  	// MaxHeaderLength returns the maximum size the data link (and
  1090  	// lower level layers combined) headers can have. Higher levels use this
  1091  	// information to reserve space in the front of the packets they're
  1092  	// building.
  1093  	MaxHeaderLength() uint16
  1094  
  1095  	// LinkAddress returns the link address (typically a MAC) of the
  1096  	// endpoint.
  1097  	LinkAddress() tcpip.LinkAddress
  1098  
  1099  	// Capabilities returns the set of capabilities supported by the
  1100  	// endpoint.
  1101  	Capabilities() LinkEndpointCapabilities
  1102  
  1103  	// Attach attaches the data link layer endpoint to the network-layer
  1104  	// dispatcher of the stack.
  1105  	//
  1106  	// Attach is called with a nil dispatcher when the endpoint's NIC is being
  1107  	// removed.
  1108  	Attach(dispatcher NetworkDispatcher)
  1109  
  1110  	// IsAttached returns whether a NetworkDispatcher is attached to the
  1111  	// endpoint.
  1112  	IsAttached() bool
  1113  
  1114  	// Wait waits for any worker goroutines owned by the endpoint to stop.
  1115  	//
  1116  	// For now, requesting that an endpoint's worker goroutine(s) stop is
  1117  	// implementation specific.
  1118  	//
  1119  	// Wait will not block if the endpoint hasn't started any goroutines
  1120  	// yet, even if it might later.
  1121  	Wait()
  1122  
  1123  	// ARPHardwareType returns the ARPHRD_TYPE of the link endpoint.
  1124  	//
  1125  	// See:
  1126  	// https://github.com/torvalds/linux/blob/aa0c9086b40c17a7ad94425b3b70dd1fdd7497bf/include/uapi/linux/if_arp.h#L30
  1127  	ARPHardwareType() header.ARPHardwareType
  1128  
  1129  	// AddHeader adds a link layer header to the packet if required.
  1130  	AddHeader(*PacketBuffer)
  1131  
  1132  	// ParseHeader parses the link layer header to the packet.
  1133  	ParseHeader(*PacketBuffer) bool
  1134  }
  1135  
  1136  // QueueingDiscipline provides a queueing strategy for outgoing packets (e.g
  1137  // FIFO, LIFO, Random Early Drop etc).
  1138  type QueueingDiscipline interface {
  1139  	// WritePacket writes a packet.
  1140  	//
  1141  	// WritePacket may modify the packet buffer. The packet buffer's
  1142  	// network and transport header must be set.
  1143  	//
  1144  	// To participate in transparent bridging, a LinkEndpoint implementation
  1145  	// should call eth.Encode with header.EthernetFields.SrcAddr set to
  1146  	// pkg.EgressRoute.LocalLinkAddress if it is provided.
  1147  	WritePacket(*PacketBuffer) tcpip.Error
  1148  
  1149  	Close()
  1150  }
  1151  
  1152  // LinkEndpoint is the interface implemented by data link layer protocols (e.g.,
  1153  // ethernet, loopback, raw) and used by network layer protocols to send packets
  1154  // out through the implementer's data link endpoint. When a link header exists,
  1155  // it sets each PacketBuffer's LinkHeader field before passing it up the
  1156  // stack.
  1157  type LinkEndpoint interface {
  1158  	NetworkLinkEndpoint
  1159  	LinkWriter
  1160  }
  1161  
  1162  // InjectableLinkEndpoint is a LinkEndpoint where inbound packets are
  1163  // delivered via the Inject method.
  1164  type InjectableLinkEndpoint interface {
  1165  	LinkEndpoint
  1166  
  1167  	// InjectInbound injects an inbound packet.
  1168  	InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer)
  1169  
  1170  	// InjectOutbound writes a fully formed outbound packet directly to the
  1171  	// link.
  1172  	//
  1173  	// dest is used by endpoints with multiple raw destinations.
  1174  	InjectOutbound(dest tcpip.Address, packet *buffer.View) tcpip.Error
  1175  }
  1176  
  1177  // DADResult is a marker interface for the result of a duplicate address
  1178  // detection process.
  1179  type DADResult interface {
  1180  	isDADResult()
  1181  }
  1182  
  1183  var _ DADResult = (*DADSucceeded)(nil)
  1184  
  1185  // DADSucceeded indicates DAD completed without finding any duplicate addresses.
  1186  type DADSucceeded struct{}
  1187  
  1188  func (*DADSucceeded) isDADResult() {}
  1189  
  1190  var _ DADResult = (*DADError)(nil)
  1191  
  1192  // DADError indicates DAD hit an error.
  1193  type DADError struct {
  1194  	Err tcpip.Error
  1195  }
  1196  
  1197  func (*DADError) isDADResult() {}
  1198  
  1199  var _ DADResult = (*DADAborted)(nil)
  1200  
  1201  // DADAborted indicates DAD was aborted.
  1202  type DADAborted struct{}
  1203  
  1204  func (*DADAborted) isDADResult() {}
  1205  
  1206  var _ DADResult = (*DADDupAddrDetected)(nil)
  1207  
  1208  // DADDupAddrDetected indicates DAD detected a duplicate address.
  1209  type DADDupAddrDetected struct {
  1210  	// HolderLinkAddress is the link address of the node that holds the duplicate
  1211  	// address.
  1212  	HolderLinkAddress tcpip.LinkAddress
  1213  }
  1214  
  1215  func (*DADDupAddrDetected) isDADResult() {}
  1216  
  1217  // DADCompletionHandler is a handler for DAD completion.
  1218  type DADCompletionHandler func(DADResult)
  1219  
  1220  // DADCheckAddressDisposition enumerates the possible return values from
  1221  // DAD.CheckDuplicateAddress.
  1222  type DADCheckAddressDisposition int
  1223  
  1224  const (
  1225  	_ DADCheckAddressDisposition = iota
  1226  
  1227  	// DADDisabled indicates that DAD is disabled.
  1228  	DADDisabled
  1229  
  1230  	// DADStarting indicates that DAD is starting for an address.
  1231  	DADStarting
  1232  
  1233  	// DADAlreadyRunning indicates that DAD was already started for an address.
  1234  	DADAlreadyRunning
  1235  )
  1236  
  1237  const (
  1238  	// defaultDupAddrDetectTransmits is the default number of NDP Neighbor
  1239  	// Solicitation messages to send when doing Duplicate Address Detection
  1240  	// for a tentative address.
  1241  	//
  1242  	// Default = 1 (from RFC 4862 section 5.1)
  1243  	defaultDupAddrDetectTransmits = 1
  1244  )
  1245  
  1246  // DADConfigurations holds configurations for duplicate address detection.
  1247  type DADConfigurations struct {
  1248  	// The number of Neighbor Solicitation messages to send when doing
  1249  	// Duplicate Address Detection for a tentative address.
  1250  	//
  1251  	// Note, a value of zero effectively disables DAD.
  1252  	DupAddrDetectTransmits uint8
  1253  
  1254  	// The amount of time to wait between sending Neighbor Solicitation
  1255  	// messages.
  1256  	//
  1257  	// Must be greater than or equal to 1ms.
  1258  	RetransmitTimer time.Duration
  1259  }
  1260  
  1261  // DefaultDADConfigurations returns the default DAD configurations.
  1262  func DefaultDADConfigurations() DADConfigurations {
  1263  	return DADConfigurations{
  1264  		DupAddrDetectTransmits: defaultDupAddrDetectTransmits,
  1265  		RetransmitTimer:        defaultRetransmitTimer,
  1266  	}
  1267  }
  1268  
  1269  // Validate modifies the configuration with valid values. If invalid values are
  1270  // present in the configurations, the corresponding default values are used
  1271  // instead.
  1272  func (c *DADConfigurations) Validate() {
  1273  	if c.RetransmitTimer < minimumRetransmitTimer {
  1274  		c.RetransmitTimer = defaultRetransmitTimer
  1275  	}
  1276  }
  1277  
  1278  // DuplicateAddressDetector handles checking if an address is already assigned
  1279  // to some neighboring node on the link.
  1280  type DuplicateAddressDetector interface {
  1281  	// CheckDuplicateAddress checks if an address is assigned to a neighbor.
  1282  	//
  1283  	// If DAD is already being performed for the address, the handler will be
  1284  	// called with the result of the original DAD request.
  1285  	CheckDuplicateAddress(tcpip.Address, DADCompletionHandler) DADCheckAddressDisposition
  1286  
  1287  	// SetDADConfigurations sets the configurations for DAD.
  1288  	SetDADConfigurations(c DADConfigurations)
  1289  
  1290  	// DuplicateAddressProtocol returns the network protocol the receiver can
  1291  	// perform duplicate address detection for.
  1292  	DuplicateAddressProtocol() tcpip.NetworkProtocolNumber
  1293  }
  1294  
  1295  // LinkAddressResolver handles link address resolution for a network protocol.
  1296  type LinkAddressResolver interface {
  1297  	// LinkAddressRequest sends a request for the link address of the target
  1298  	// address. The request is broadcast on the local network if a remote link
  1299  	// address is not provided.
  1300  	LinkAddressRequest(targetAddr, localAddr tcpip.Address, remoteLinkAddr tcpip.LinkAddress) tcpip.Error
  1301  
  1302  	// ResolveStaticAddress attempts to resolve address without sending
  1303  	// requests. It either resolves the name immediately or returns the
  1304  	// empty LinkAddress.
  1305  	//
  1306  	// It can be used to resolve broadcast addresses for example.
  1307  	ResolveStaticAddress(addr tcpip.Address) (tcpip.LinkAddress, bool)
  1308  
  1309  	// LinkAddressProtocol returns the network protocol of the
  1310  	// addresses this resolver can resolve.
  1311  	LinkAddressProtocol() tcpip.NetworkProtocolNumber
  1312  }
  1313  
  1314  // RawFactory produces endpoints for writing various types of raw packets.
  1315  type RawFactory interface {
  1316  	// NewUnassociatedEndpoint produces endpoints for writing packets not
  1317  	// associated with a particular transport protocol. Such endpoints can
  1318  	// be used to write arbitrary packets that include the network header.
  1319  	NewUnassociatedEndpoint(stack *Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error)
  1320  
  1321  	// NewPacketEndpoint produces endpoints for reading and writing packets
  1322  	// that include network and (when cooked is false) link layer headers.
  1323  	NewPacketEndpoint(stack *Stack, cooked bool, netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error)
  1324  }
  1325  
  1326  // GSOType is the type of GSO segments.
  1327  //
  1328  // +stateify savable
  1329  type GSOType int
  1330  
  1331  // Types of gso segments.
  1332  const (
  1333  	GSONone GSOType = iota
  1334  
  1335  	// Hardware GSO types:
  1336  	GSOTCPv4
  1337  	GSOTCPv6
  1338  
  1339  	// GSOGvisor is used for gVisor GSO segments which have to be sent by
  1340  	// endpoint.WritePackets.
  1341  	GSOGvisor
  1342  )
  1343  
  1344  // GSO contains generic segmentation offload properties.
  1345  //
  1346  // +stateify savable
  1347  type GSO struct {
  1348  	// Type is one of GSONone, GSOTCPv4, etc.
  1349  	Type GSOType
  1350  	// NeedsCsum is set if the checksum offload is enabled.
  1351  	NeedsCsum bool
  1352  	// CsumOffset is offset after that to place checksum.
  1353  	CsumOffset uint16
  1354  
  1355  	// Mss is maximum segment size.
  1356  	MSS uint16
  1357  	// L3Len is L3 (IP) header length.
  1358  	L3HdrLen uint16
  1359  
  1360  	// MaxSize is maximum GSO packet size.
  1361  	MaxSize uint32
  1362  }
  1363  
  1364  // SupportedGSO is the type of segmentation offloading supported.
  1365  type SupportedGSO int
  1366  
  1367  const (
  1368  	// GSONotSupported indicates that segmentation offloading is not supported.
  1369  	GSONotSupported SupportedGSO = iota
  1370  
  1371  	// HostGSOSupported indicates that segmentation offloading may be performed
  1372  	// by the host. This is typically true when netstack is attached to a host
  1373  	// AF_PACKET socket, and not true when attached to a unix socket or other
  1374  	// non-networking data layer.
  1375  	HostGSOSupported
  1376  
  1377  	// GVisorGSOSupported indicates that segmentation offloading may be performed
  1378  	// in gVisor.
  1379  	GVisorGSOSupported
  1380  )
  1381  
  1382  // GSOEndpoint provides access to GSO properties.
  1383  type GSOEndpoint interface {
  1384  	// GSOMaxSize returns the maximum GSO packet size.
  1385  	GSOMaxSize() uint32
  1386  
  1387  	// SupportedGSO returns the supported segmentation offloading.
  1388  	SupportedGSO() SupportedGSO
  1389  }
  1390  
  1391  // GVisorGSOMaxSize is a maximum allowed size of a software GSO segment.
  1392  // This isn't a hard limit, because it is never set into packet headers.
  1393  const GVisorGSOMaxSize = 1 << 16