github.com/noisysockets/netstack@v0.6.0/pkg/tcpip/tcpip.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 tcpip provides the interfaces and related types that users of the
    16  // tcpip stack will use in order to create endpoints used to send and receive
    17  // data over the network stack.
    18  //
    19  // The starting point is the creation and configuration of a stack. A stack can
    20  // be created by calling the New() function of the tcpip/stack/stack package;
    21  // configuring a stack involves creating NICs (via calls to Stack.CreateNIC()),
    22  // adding network addresses (via calls to Stack.AddProtocolAddress()), and
    23  // setting a route table (via a call to Stack.SetRouteTable()).
    24  //
    25  // Once a stack is configured, endpoints can be created by calling
    26  // Stack.NewEndpoint(). Such endpoints can be used to send/receive data, connect
    27  // to peers, listen for connections, accept connections, etc., depending on the
    28  // transport protocol selected.
    29  package tcpip
    30  
    31  import (
    32  	"bytes"
    33  	"errors"
    34  	"fmt"
    35  	"io"
    36  	"math"
    37  	"math/bits"
    38  	"reflect"
    39  	"strconv"
    40  	"strings"
    41  	"time"
    42  
    43  	"github.com/noisysockets/netstack/pkg/atomicbitops"
    44  	"github.com/noisysockets/netstack/pkg/sync"
    45  	"github.com/noisysockets/netstack/pkg/waiter"
    46  )
    47  
    48  // Using the header package here would cause an import cycle.
    49  const (
    50  	ipv4AddressSize    = 4
    51  	ipv4ProtocolNumber = 0x0800
    52  	ipv6AddressSize    = 16
    53  	ipv6ProtocolNumber = 0x86dd
    54  )
    55  
    56  // Errors related to Subnet
    57  var (
    58  	errSubnetLengthMismatch = errors.New("subnet length of address and mask differ")
    59  	errSubnetAddressMasked  = errors.New("subnet address has bits set outside the mask")
    60  )
    61  
    62  // ErrSaveRejection indicates a failed save due to unsupported networking state.
    63  // This type of errors is only used for save logic.
    64  type ErrSaveRejection struct {
    65  	Err error
    66  }
    67  
    68  // Error returns a sensible description of the save rejection error.
    69  func (e *ErrSaveRejection) Error() string {
    70  	return "save rejected due to unsupported networking state: " + e.Err.Error()
    71  }
    72  
    73  // MonotonicTime is a monotonic clock reading.
    74  //
    75  // +stateify savable
    76  type MonotonicTime struct {
    77  	nanoseconds int64
    78  }
    79  
    80  // String implements Stringer.
    81  func (mt MonotonicTime) String() string {
    82  	return strconv.FormatInt(mt.nanoseconds, 10)
    83  }
    84  
    85  // MonotonicTimeInfinite returns the monotonic timestamp as far away in the
    86  // future as possible.
    87  func MonotonicTimeInfinite() MonotonicTime {
    88  	return MonotonicTime{nanoseconds: math.MaxInt64}
    89  }
    90  
    91  // Before reports whether the monotonic clock reading mt is before u.
    92  func (mt MonotonicTime) Before(u MonotonicTime) bool {
    93  	return mt.nanoseconds < u.nanoseconds
    94  }
    95  
    96  // After reports whether the monotonic clock reading mt is after u.
    97  func (mt MonotonicTime) After(u MonotonicTime) bool {
    98  	return mt.nanoseconds > u.nanoseconds
    99  }
   100  
   101  // Add returns the monotonic clock reading mt+d.
   102  func (mt MonotonicTime) Add(d time.Duration) MonotonicTime {
   103  	return MonotonicTime{
   104  		nanoseconds: time.Unix(0, mt.nanoseconds).Add(d).Sub(time.Unix(0, 0)).Nanoseconds(),
   105  	}
   106  }
   107  
   108  // Sub returns the duration mt-u. If the result exceeds the maximum (or minimum)
   109  // value that can be stored in a Duration, the maximum (or minimum) duration
   110  // will be returned. To compute t-d for a duration d, use t.Add(-d).
   111  func (mt MonotonicTime) Sub(u MonotonicTime) time.Duration {
   112  	return time.Unix(0, mt.nanoseconds).Sub(time.Unix(0, u.nanoseconds))
   113  }
   114  
   115  // Milliseconds returns the time in milliseconds.
   116  func (mt MonotonicTime) Milliseconds() int64 {
   117  	return mt.nanoseconds / 1e6
   118  }
   119  
   120  // A Clock provides the current time and schedules work for execution.
   121  //
   122  // Times returned by a Clock should always be used for application-visible
   123  // time. Only monotonic times should be used for netstack internal timekeeping.
   124  type Clock interface {
   125  	// Now returns the current local time.
   126  	Now() time.Time
   127  
   128  	// NowMonotonic returns the current monotonic clock reading.
   129  	NowMonotonic() MonotonicTime
   130  
   131  	// AfterFunc waits for the duration to elapse and then calls f in its own
   132  	// goroutine. It returns a Timer that can be used to cancel the call using
   133  	// its Stop method.
   134  	AfterFunc(d time.Duration, f func()) Timer
   135  }
   136  
   137  // Timer represents a single event. A Timer must be created with
   138  // Clock.AfterFunc.
   139  type Timer interface {
   140  	// Stop prevents the Timer from firing. It returns true if the call stops the
   141  	// timer, false if the timer has already expired or been stopped.
   142  	//
   143  	// If Stop returns false, then the timer has already expired and the function
   144  	// f of Clock.AfterFunc(d, f) has been started in its own goroutine; Stop
   145  	// does not wait for f to complete before returning. If the caller needs to
   146  	// know whether f is completed, it must coordinate with f explicitly.
   147  	Stop() bool
   148  
   149  	// Reset changes the timer to expire after duration d.
   150  	//
   151  	// Reset should be invoked only on stopped or expired timers. If the timer is
   152  	// known to have expired, Reset can be used directly. Otherwise, the caller
   153  	// must coordinate with the function f of Clock.AfterFunc(d, f).
   154  	Reset(d time.Duration)
   155  }
   156  
   157  // Address is a byte slice cast as a string that represents the address of a
   158  // network node. Or, in the case of unix endpoints, it may represent a path.
   159  //
   160  // +stateify savable
   161  type Address struct {
   162  	addr   [16]byte
   163  	length int
   164  }
   165  
   166  // AddrFrom4 converts addr to an Address.
   167  func AddrFrom4(addr [4]byte) Address {
   168  	ret := Address{
   169  		length: 4,
   170  	}
   171  	// It's guaranteed that copy will return 4.
   172  	copy(ret.addr[:], addr[:])
   173  	return ret
   174  }
   175  
   176  // AddrFrom4Slice converts addr to an Address. It panics if len(addr) != 4.
   177  func AddrFrom4Slice(addr []byte) Address {
   178  	if len(addr) != 4 {
   179  		panic(fmt.Sprintf("bad address length for address %v", addr))
   180  	}
   181  	ret := Address{
   182  		length: 4,
   183  	}
   184  	// It's guaranteed that copy will return 4.
   185  	copy(ret.addr[:], addr)
   186  	return ret
   187  }
   188  
   189  // AddrFrom16 converts addr to an Address.
   190  func AddrFrom16(addr [16]byte) Address {
   191  	ret := Address{
   192  		length: 16,
   193  	}
   194  	// It's guaranteed that copy will return 16.
   195  	copy(ret.addr[:], addr[:])
   196  	return ret
   197  }
   198  
   199  // AddrFrom16Slice converts addr to an Address. It panics if len(addr) != 16.
   200  func AddrFrom16Slice(addr []byte) Address {
   201  	if len(addr) != 16 {
   202  		panic(fmt.Sprintf("bad address length for address %v", addr))
   203  	}
   204  	ret := Address{
   205  		length: 16,
   206  	}
   207  	// It's guaranteed that copy will return 16.
   208  	copy(ret.addr[:], addr)
   209  	return ret
   210  }
   211  
   212  // AddrFromSlice converts addr to an Address. It returns the Address zero value
   213  // if len(addr) != 4 or 16.
   214  func AddrFromSlice(addr []byte) Address {
   215  	switch len(addr) {
   216  	case ipv4AddressSize:
   217  		return AddrFrom4Slice(addr)
   218  	case ipv6AddressSize:
   219  		return AddrFrom16Slice(addr)
   220  	}
   221  	return Address{}
   222  }
   223  
   224  // As4 returns a as a 4 byte array. It panics if the address length is not 4.
   225  func (a Address) As4() [4]byte {
   226  	if a.Len() != 4 {
   227  		panic(fmt.Sprintf("bad address length for address %v", a.addr))
   228  	}
   229  	return [4]byte(a.addr[:4])
   230  }
   231  
   232  // As16 returns a as a 16 byte array. It panics if the address length is not 16.
   233  func (a Address) As16() [16]byte {
   234  	if a.Len() != 16 {
   235  		panic(fmt.Sprintf("bad address length for address %v", a.addr))
   236  	}
   237  	return [16]byte(a.addr[:16])
   238  }
   239  
   240  // AsSlice returns a as a byte slice. Callers should be careful as it can
   241  // return a window into existing memory.
   242  //
   243  // +checkescape
   244  func (a *Address) AsSlice() []byte {
   245  	return a.addr[:a.length]
   246  }
   247  
   248  // BitLen returns the length in bits of a.
   249  func (a Address) BitLen() int {
   250  	return a.Len() * 8
   251  }
   252  
   253  // Len returns the length in bytes of a.
   254  func (a Address) Len() int {
   255  	return a.length
   256  }
   257  
   258  // WithPrefix returns the address with a prefix that represents a point subnet.
   259  func (a Address) WithPrefix() AddressWithPrefix {
   260  	return AddressWithPrefix{
   261  		Address:   a,
   262  		PrefixLen: a.BitLen(),
   263  	}
   264  }
   265  
   266  // Unspecified returns true if the address is unspecified.
   267  func (a Address) Unspecified() bool {
   268  	for _, b := range a.addr {
   269  		if b != 0 {
   270  			return false
   271  		}
   272  	}
   273  	return true
   274  }
   275  
   276  // Equal returns whether a and other are equal. It exists for use by the cmp
   277  // library.
   278  func (a Address) Equal(other Address) bool {
   279  	return a == other
   280  }
   281  
   282  // MatchingPrefix returns the matching prefix length in bits.
   283  //
   284  // Panics if b and a have different lengths.
   285  func (a Address) MatchingPrefix(b Address) uint8 {
   286  	const bitsInAByte = 8
   287  
   288  	if a.Len() != b.Len() {
   289  		panic(fmt.Sprintf("addresses %s and %s do not have the same length", a, b))
   290  	}
   291  
   292  	var prefix uint8
   293  	for i := 0; i < a.length; i++ {
   294  		aByte := a.addr[i]
   295  		bByte := b.addr[i]
   296  
   297  		if aByte == bByte {
   298  			prefix += bitsInAByte
   299  			continue
   300  		}
   301  
   302  		// Count the remaining matching bits in the byte from MSbit to LSBbit.
   303  		mask := uint8(1) << (bitsInAByte - 1)
   304  		for {
   305  			if aByte&mask == bByte&mask {
   306  				prefix++
   307  				mask >>= 1
   308  				continue
   309  			}
   310  
   311  			break
   312  		}
   313  
   314  		break
   315  	}
   316  
   317  	return prefix
   318  }
   319  
   320  // AddressMask is a bitmask for an address.
   321  //
   322  // +stateify savable
   323  type AddressMask struct {
   324  	mask   [16]byte
   325  	length int
   326  }
   327  
   328  // MaskFrom returns a Mask based on str.
   329  //
   330  // MaskFrom may allocate, and so should not be in hot paths.
   331  func MaskFrom(str string) AddressMask {
   332  	mask := AddressMask{length: len(str)}
   333  	copy(mask.mask[:], str)
   334  	return mask
   335  }
   336  
   337  // MaskFromBytes returns a Mask based on bs.
   338  func MaskFromBytes(bs []byte) AddressMask {
   339  	mask := AddressMask{length: len(bs)}
   340  	copy(mask.mask[:], bs)
   341  	return mask
   342  }
   343  
   344  // String implements Stringer.
   345  func (m AddressMask) String() string {
   346  	return fmt.Sprintf("%x", m.mask)
   347  }
   348  
   349  // AsSlice returns a as a byte slice. Callers should be careful as it can
   350  // return a window into existing memory.
   351  func (m *AddressMask) AsSlice() []byte {
   352  	return []byte(m.mask[:m.length])
   353  }
   354  
   355  // BitLen returns the length of the mask in bits.
   356  func (m AddressMask) BitLen() int {
   357  	return m.length * 8
   358  }
   359  
   360  // Len returns the length of the mask in bytes.
   361  func (m AddressMask) Len() int {
   362  	return m.length
   363  }
   364  
   365  // Prefix returns the number of bits before the first host bit.
   366  func (m AddressMask) Prefix() int {
   367  	p := 0
   368  	for _, b := range m.mask[:m.length] {
   369  		p += bits.LeadingZeros8(^b)
   370  	}
   371  	return p
   372  }
   373  
   374  // Equal returns whether m and other are equal. It exists for use by the cmp
   375  // library.
   376  func (m AddressMask) Equal(other AddressMask) bool {
   377  	return m == other
   378  }
   379  
   380  // Subnet is a subnet defined by its address and mask.
   381  type Subnet struct {
   382  	address Address
   383  	mask    AddressMask
   384  }
   385  
   386  // NewSubnet creates a new Subnet, checking that the address and mask are the same length.
   387  func NewSubnet(a Address, m AddressMask) (Subnet, error) {
   388  	if a.Len() != m.Len() {
   389  		return Subnet{}, errSubnetLengthMismatch
   390  	}
   391  	for i := 0; i < a.Len(); i++ {
   392  		if a.addr[i]&^m.mask[i] != 0 {
   393  			return Subnet{}, errSubnetAddressMasked
   394  		}
   395  	}
   396  	return Subnet{a, m}, nil
   397  }
   398  
   399  // String implements Stringer.
   400  func (s Subnet) String() string {
   401  	return fmt.Sprintf("%s/%d", s.ID(), s.Prefix())
   402  }
   403  
   404  // Contains returns true iff the address is of the same length and matches the
   405  // subnet address and mask.
   406  func (s *Subnet) Contains(a Address) bool {
   407  	if a.Len() != s.address.Len() {
   408  		return false
   409  	}
   410  	for i := 0; i < a.Len(); i++ {
   411  		if a.addr[i]&s.mask.mask[i] != s.address.addr[i] {
   412  			return false
   413  		}
   414  	}
   415  	return true
   416  }
   417  
   418  // ID returns the subnet ID.
   419  func (s *Subnet) ID() Address {
   420  	return s.address
   421  }
   422  
   423  // Bits returns the number of ones (network bits) and zeros (host bits) in the
   424  // subnet mask.
   425  func (s *Subnet) Bits() (ones int, zeros int) {
   426  	ones = s.mask.Prefix()
   427  	return ones, s.mask.BitLen() - ones
   428  }
   429  
   430  // Prefix returns the number of bits before the first host bit.
   431  func (s *Subnet) Prefix() int {
   432  	return s.mask.Prefix()
   433  }
   434  
   435  // Mask returns the subnet mask.
   436  func (s *Subnet) Mask() AddressMask {
   437  	return s.mask
   438  }
   439  
   440  // Broadcast returns the subnet's broadcast address.
   441  func (s *Subnet) Broadcast() Address {
   442  	addrCopy := s.address
   443  	for i := 0; i < addrCopy.Len(); i++ {
   444  		addrCopy.addr[i] |= ^s.mask.mask[i]
   445  	}
   446  	return addrCopy
   447  }
   448  
   449  // IsBroadcast returns true if the address is considered a broadcast address.
   450  func (s *Subnet) IsBroadcast(address Address) bool {
   451  	// Only IPv4 supports the notion of a broadcast address.
   452  	if address.Len() != ipv4AddressSize {
   453  		return false
   454  	}
   455  
   456  	// Normally, we would just compare address with the subnet's broadcast
   457  	// address but there is an exception where a simple comparison is not
   458  	// correct. This exception is for /31 and /32 IPv4 subnets where all
   459  	// addresses are considered valid host addresses.
   460  	//
   461  	// For /31 subnets, the case is easy. RFC 3021 Section 2.1 states that
   462  	// both addresses in a /31 subnet "MUST be interpreted as host addresses."
   463  	//
   464  	// For /32, the case is a bit more vague. RFC 3021 makes no mention of /32
   465  	// subnets. However, the same reasoning applies - if an exception is not
   466  	// made, then there do not exist any host addresses in a /32 subnet. RFC
   467  	// 4632 Section 3.1 also vaguely implies this interpretation by referring
   468  	// to addresses in /32 subnets as "host routes."
   469  	return s.Prefix() <= 30 && s.Broadcast() == address
   470  }
   471  
   472  // Equal returns true if this Subnet is equal to the given Subnet.
   473  func (s Subnet) Equal(o Subnet) bool {
   474  	// If this changes, update Route.Equal accordingly.
   475  	return s == o
   476  }
   477  
   478  // NICID is a number that uniquely identifies a NIC.
   479  type NICID int32
   480  
   481  // ShutdownFlags represents flags that can be passed to the Shutdown() method
   482  // of the Endpoint interface.
   483  type ShutdownFlags int
   484  
   485  // Values of the flags that can be passed to the Shutdown() method. They can
   486  // be OR'ed together.
   487  const (
   488  	ShutdownRead ShutdownFlags = 1 << iota
   489  	ShutdownWrite
   490  )
   491  
   492  // PacketType is used to indicate the destination of the packet.
   493  type PacketType uint8
   494  
   495  const (
   496  	// PacketHost indicates a packet addressed to the local host.
   497  	PacketHost PacketType = iota
   498  
   499  	// PacketOtherHost indicates an outgoing packet addressed to
   500  	// another host caught by a NIC in promiscuous mode.
   501  	PacketOtherHost
   502  
   503  	// PacketOutgoing for a packet originating from the local host
   504  	// that is looped back to a packet socket.
   505  	PacketOutgoing
   506  
   507  	// PacketBroadcast indicates a link layer broadcast packet.
   508  	PacketBroadcast
   509  
   510  	// PacketMulticast indicates a link layer multicast packet.
   511  	PacketMulticast
   512  )
   513  
   514  // FullAddress represents a full transport node address, as required by the
   515  // Connect() and Bind() methods.
   516  //
   517  // +stateify savable
   518  type FullAddress struct {
   519  	// NIC is the ID of the NIC this address refers to.
   520  	//
   521  	// This may not be used by all endpoint types.
   522  	NIC NICID
   523  
   524  	// Addr is the network address.
   525  	Addr Address
   526  
   527  	// Port is the transport port.
   528  	//
   529  	// This may not be used by all endpoint types.
   530  	Port uint16
   531  
   532  	// LinkAddr is the link layer address.
   533  	LinkAddr LinkAddress
   534  }
   535  
   536  // Payloader is an interface that provides data.
   537  //
   538  // This interface allows the endpoint to request the amount of data it needs
   539  // based on internal buffers without exposing them.
   540  type Payloader interface {
   541  	io.Reader
   542  
   543  	// Len returns the number of bytes of the unread portion of the
   544  	// Reader.
   545  	Len() int
   546  }
   547  
   548  var _ Payloader = (*bytes.Buffer)(nil)
   549  var _ Payloader = (*bytes.Reader)(nil)
   550  
   551  var _ io.Writer = (*SliceWriter)(nil)
   552  
   553  // SliceWriter implements io.Writer for slices.
   554  type SliceWriter []byte
   555  
   556  // Write implements io.Writer.Write.
   557  func (s *SliceWriter) Write(b []byte) (int, error) {
   558  	n := copy(*s, b)
   559  	*s = (*s)[n:]
   560  	var err error
   561  	if n != len(b) {
   562  		err = io.ErrShortWrite
   563  	}
   564  	return n, err
   565  }
   566  
   567  var _ io.Writer = (*LimitedWriter)(nil)
   568  
   569  // A LimitedWriter writes to W but limits the amount of data copied to just N
   570  // bytes. Each call to Write updates N to reflect the new amount remaining.
   571  type LimitedWriter struct {
   572  	W io.Writer
   573  	N int64
   574  }
   575  
   576  func (l *LimitedWriter) Write(p []byte) (int, error) {
   577  	pLen := int64(len(p))
   578  	if pLen > l.N {
   579  		p = p[:l.N]
   580  	}
   581  	n, err := l.W.Write(p)
   582  	n64 := int64(n)
   583  	if err == nil && n64 != pLen {
   584  		err = io.ErrShortWrite
   585  	}
   586  	l.N -= n64
   587  	return n, err
   588  }
   589  
   590  // SendableControlMessages contains socket control messages that can be written.
   591  //
   592  // +stateify savable
   593  type SendableControlMessages struct {
   594  	// HasTTL indicates whether TTL is valid/set.
   595  	HasTTL bool
   596  
   597  	// TTL is the IPv4 Time To Live of the associated packet.
   598  	TTL uint8
   599  
   600  	// HasHopLimit indicates whether HopLimit is valid/set.
   601  	HasHopLimit bool
   602  
   603  	// HopLimit is the IPv6 Hop Limit of the associated packet.
   604  	HopLimit uint8
   605  
   606  	// HasIPv6PacketInfo indicates whether IPv6PacketInfo is set.
   607  	HasIPv6PacketInfo bool
   608  
   609  	// IPv6PacketInfo holds interface and address data on an incoming packet.
   610  	IPv6PacketInfo IPv6PacketInfo
   611  }
   612  
   613  // ReceivableControlMessages contains socket control messages that can be
   614  // received.
   615  //
   616  // +stateify savable
   617  type ReceivableControlMessages struct {
   618  	// Timestamp is the time that the last packet used to create the read data
   619  	// was received.
   620  	Timestamp time.Time `state:".(int64)"`
   621  
   622  	// HasInq indicates whether Inq is valid/set.
   623  	HasInq bool
   624  
   625  	// Inq is the number of bytes ready to be received.
   626  	Inq int32
   627  
   628  	// HasTOS indicates whether TOS is valid/set.
   629  	HasTOS bool
   630  
   631  	// TOS is the IPv4 type of service of the associated packet.
   632  	TOS uint8
   633  
   634  	// HasTTL indicates whether TTL is valid/set.
   635  	HasTTL bool
   636  
   637  	// TTL is the IPv4 Time To Live of the associated packet.
   638  	TTL uint8
   639  
   640  	// HasHopLimit indicates whether HopLimit is valid/set.
   641  	HasHopLimit bool
   642  
   643  	// HopLimit is the IPv6 Hop Limit of the associated packet.
   644  	HopLimit uint8
   645  
   646  	// HasTimestamp indicates whether Timestamp is valid/set.
   647  	HasTimestamp bool
   648  
   649  	// HasTClass indicates whether TClass is valid/set.
   650  	HasTClass bool
   651  
   652  	// TClass is the IPv6 traffic class of the associated packet.
   653  	TClass uint32
   654  
   655  	// HasIPPacketInfo indicates whether PacketInfo is set.
   656  	HasIPPacketInfo bool
   657  
   658  	// PacketInfo holds interface and address data on an incoming packet.
   659  	PacketInfo IPPacketInfo
   660  
   661  	// HasIPv6PacketInfo indicates whether IPv6PacketInfo is set.
   662  	HasIPv6PacketInfo bool
   663  
   664  	// IPv6PacketInfo holds interface and address data on an incoming packet.
   665  	IPv6PacketInfo IPv6PacketInfo
   666  
   667  	// HasOriginalDestinationAddress indicates whether OriginalDstAddress is
   668  	// set.
   669  	HasOriginalDstAddress bool
   670  
   671  	// OriginalDestinationAddress holds the original destination address
   672  	// and port of the incoming packet.
   673  	OriginalDstAddress FullAddress
   674  
   675  	// SockErr is the dequeued socket error on recvmsg(MSG_ERRQUEUE).
   676  	SockErr *SockError
   677  }
   678  
   679  // PacketOwner is used to get UID and GID of the packet.
   680  type PacketOwner interface {
   681  	// KUID returns KUID of the packet.
   682  	KUID() uint32
   683  
   684  	// KGID returns KGID of the packet.
   685  	KGID() uint32
   686  }
   687  
   688  // ReadOptions contains options for Endpoint.Read.
   689  type ReadOptions struct {
   690  	// Peek indicates whether this read is a peek.
   691  	Peek bool
   692  
   693  	// NeedRemoteAddr indicates whether to return the remote address, if
   694  	// supported.
   695  	NeedRemoteAddr bool
   696  
   697  	// NeedLinkPacketInfo indicates whether to return the link-layer information,
   698  	// if supported.
   699  	NeedLinkPacketInfo bool
   700  }
   701  
   702  // ReadResult represents result for a successful Endpoint.Read.
   703  type ReadResult struct {
   704  	// Count is the number of bytes received and written to the buffer.
   705  	Count int
   706  
   707  	// Total is the number of bytes of the received packet. This can be used to
   708  	// determine whether the read is truncated.
   709  	Total int
   710  
   711  	// ControlMessages is the control messages received.
   712  	ControlMessages ReceivableControlMessages
   713  
   714  	// RemoteAddr is the remote address if ReadOptions.NeedAddr is true.
   715  	RemoteAddr FullAddress
   716  
   717  	// LinkPacketInfo is the link-layer information of the received packet if
   718  	// ReadOptions.NeedLinkPacketInfo is true.
   719  	LinkPacketInfo LinkPacketInfo
   720  }
   721  
   722  // Endpoint is the interface implemented by transport protocols (e.g., tcp, udp)
   723  // that exposes functionality like read, write, connect, etc. to users of the
   724  // networking stack.
   725  type Endpoint interface {
   726  	// Close puts the endpoint in a closed state and frees all resources
   727  	// associated with it. Close initiates the teardown process, the
   728  	// Endpoint may not be fully closed when Close returns.
   729  	Close()
   730  
   731  	// Abort initiates an expedited endpoint teardown. As compared to
   732  	// Close, Abort prioritizes closing the Endpoint quickly over cleanly.
   733  	// Abort is best effort; implementing Abort with Close is acceptable.
   734  	Abort()
   735  
   736  	// Read reads data from the endpoint and optionally writes to dst.
   737  	//
   738  	// This method does not block if there is no data pending; in this case,
   739  	// ErrWouldBlock is returned.
   740  	//
   741  	// If non-zero number of bytes are successfully read and written to dst, err
   742  	// must be nil. Otherwise, if dst failed to write anything, ErrBadBuffer
   743  	// should be returned.
   744  	Read(io.Writer, ReadOptions) (ReadResult, Error)
   745  
   746  	// Write writes data to the endpoint's peer. This method does not block if
   747  	// the data cannot be written.
   748  	//
   749  	// Unlike io.Writer.Write, Endpoint.Write transfers ownership of any bytes
   750  	// successfully written to the Endpoint. That is, if a call to
   751  	// Write(SlicePayload{data}) returns (n, err), it may retain data[:n], and
   752  	// the caller should not use data[:n] after Write returns.
   753  	//
   754  	// Note that unlike io.Writer.Write, it is not an error for Write to
   755  	// perform a partial write (if n > 0, no error may be returned). Only
   756  	// stream (TCP) Endpoints may return partial writes, and even then only
   757  	// in the case where writing additional data would block. Other Endpoints
   758  	// will either write the entire message or return an error.
   759  	Write(Payloader, WriteOptions) (int64, Error)
   760  
   761  	// Connect connects the endpoint to its peer. Specifying a NIC is
   762  	// optional.
   763  	//
   764  	// There are three classes of return values:
   765  	//	nil -- the attempt to connect succeeded.
   766  	//	ErrConnectStarted/ErrAlreadyConnecting -- the connect attempt started
   767  	//		but hasn't completed yet. In this case, the caller must call Connect
   768  	//		or GetSockOpt(ErrorOption) when the endpoint becomes writable to
   769  	//		get the actual result. The first call to Connect after the socket has
   770  	//		connected returns nil. Calling connect again results in ErrAlreadyConnected.
   771  	//	Anything else -- the attempt to connect failed.
   772  	//
   773  	// If address.Addr is empty, this means that Endpoint has to be
   774  	// disconnected if this is supported, otherwise
   775  	// ErrAddressFamilyNotSupported must be returned.
   776  	Connect(address FullAddress) Error
   777  
   778  	// Disconnect disconnects the endpoint from its peer.
   779  	Disconnect() Error
   780  
   781  	// Shutdown closes the read and/or write end of the endpoint connection
   782  	// to its peer.
   783  	Shutdown(flags ShutdownFlags) Error
   784  
   785  	// Listen puts the endpoint in "listen" mode, which allows it to accept
   786  	// new connections.
   787  	Listen(backlog int) Error
   788  
   789  	// Accept returns a new endpoint if a peer has established a connection
   790  	// to an endpoint previously set to listen mode. This method does not
   791  	// block if no new connections are available.
   792  	//
   793  	// The returned Queue is the wait queue for the newly created endpoint.
   794  	//
   795  	// If peerAddr is not nil then it is populated with the peer address of the
   796  	// returned endpoint.
   797  	Accept(peerAddr *FullAddress) (Endpoint, *waiter.Queue, Error)
   798  
   799  	// Bind binds the endpoint to a specific local address and port.
   800  	// Specifying a NIC is optional.
   801  	Bind(address FullAddress) Error
   802  
   803  	// GetLocalAddress returns the address to which the endpoint is bound.
   804  	GetLocalAddress() (FullAddress, Error)
   805  
   806  	// GetRemoteAddress returns the address to which the endpoint is
   807  	// connected.
   808  	GetRemoteAddress() (FullAddress, Error)
   809  
   810  	// Readiness returns the current readiness of the endpoint. For example,
   811  	// if waiter.EventIn is set, the endpoint is immediately readable.
   812  	Readiness(mask waiter.EventMask) waiter.EventMask
   813  
   814  	// SetSockOpt sets a socket option.
   815  	SetSockOpt(opt SettableSocketOption) Error
   816  
   817  	// SetSockOptInt sets a socket option, for simple cases where a value
   818  	// has the int type.
   819  	SetSockOptInt(opt SockOptInt, v int) Error
   820  
   821  	// GetSockOpt gets a socket option.
   822  	GetSockOpt(opt GettableSocketOption) Error
   823  
   824  	// GetSockOptInt gets a socket option for simple cases where a return
   825  	// value has the int type.
   826  	GetSockOptInt(SockOptInt) (int, Error)
   827  
   828  	// State returns a socket's lifecycle state. The returned value is
   829  	// protocol-specific and is primarily used for diagnostics.
   830  	State() uint32
   831  
   832  	// ModerateRecvBuf should be called everytime data is copied to the user
   833  	// space. This allows for dynamic tuning of recv buffer space for a
   834  	// given socket.
   835  	//
   836  	// NOTE: This method is a no-op for sockets other than TCP.
   837  	ModerateRecvBuf(copied int)
   838  
   839  	// Info returns a copy to the transport endpoint info.
   840  	Info() EndpointInfo
   841  
   842  	// Stats returns a reference to the endpoint stats.
   843  	Stats() EndpointStats
   844  
   845  	// SetOwner sets the task owner to the endpoint owner.
   846  	SetOwner(owner PacketOwner)
   847  
   848  	// LastError clears and returns the last error reported by the endpoint.
   849  	LastError() Error
   850  
   851  	// SocketOptions returns the structure which contains all the socket
   852  	// level options.
   853  	SocketOptions() *SocketOptions
   854  }
   855  
   856  // EndpointWithPreflight is the interface implemented by endpoints that need
   857  // to expose the `Preflight` method for preparing the endpoint prior to
   858  // calling `Write`.
   859  type EndpointWithPreflight interface {
   860  	// Prepares the endpoint for writes using the provided WriteOptions,
   861  	// returning an error if the options were incompatible with the endpoint's
   862  	// current state.
   863  	Preflight(WriteOptions) Error
   864  }
   865  
   866  // LinkPacketInfo holds Link layer information for a received packet.
   867  //
   868  // +stateify savable
   869  type LinkPacketInfo struct {
   870  	// Protocol is the NetworkProtocolNumber for the packet.
   871  	Protocol NetworkProtocolNumber
   872  
   873  	// PktType is used to indicate the destination of the packet.
   874  	PktType PacketType
   875  }
   876  
   877  // EndpointInfo is the interface implemented by each endpoint info struct.
   878  type EndpointInfo interface {
   879  	// IsEndpointInfo is an empty method to implement the tcpip.EndpointInfo
   880  	// marker interface.
   881  	IsEndpointInfo()
   882  }
   883  
   884  // EndpointStats is the interface implemented by each endpoint stats struct.
   885  type EndpointStats interface {
   886  	// IsEndpointStats is an empty method to implement the tcpip.EndpointStats
   887  	// marker interface.
   888  	IsEndpointStats()
   889  }
   890  
   891  // WriteOptions contains options for Endpoint.Write.
   892  type WriteOptions struct {
   893  	// If To is not nil, write to the given address instead of the endpoint's
   894  	// peer.
   895  	To *FullAddress
   896  
   897  	// More has the same semantics as Linux's MSG_MORE.
   898  	More bool
   899  
   900  	// EndOfRecord has the same semantics as Linux's MSG_EOR.
   901  	EndOfRecord bool
   902  
   903  	// Atomic means that all data fetched from Payloader must be written to the
   904  	// endpoint. If Atomic is false, then data fetched from the Payloader may be
   905  	// discarded if available endpoint buffer space is insufficient.
   906  	Atomic bool
   907  
   908  	// ControlMessages contains optional overrides used when writing a packet.
   909  	ControlMessages SendableControlMessages
   910  }
   911  
   912  // SockOptInt represents socket options which values have the int type.
   913  type SockOptInt int
   914  
   915  const (
   916  	// KeepaliveCountOption is used by SetSockOptInt/GetSockOptInt to
   917  	// specify the number of un-ACKed TCP keepalives that will be sent
   918  	// before the connection is closed.
   919  	KeepaliveCountOption SockOptInt = iota
   920  
   921  	// IPv4TOSOption is used by SetSockOptInt/GetSockOptInt to specify TOS
   922  	// for all subsequent outgoing IPv4 packets from the endpoint.
   923  	IPv4TOSOption
   924  
   925  	// IPv6TrafficClassOption is used by SetSockOptInt/GetSockOptInt to
   926  	// specify TOS for all subsequent outgoing IPv6 packets from the
   927  	// endpoint.
   928  	IPv6TrafficClassOption
   929  
   930  	// MaxSegOption is used by SetSockOptInt/GetSockOptInt to set/get the
   931  	// current Maximum Segment Size(MSS) value as specified using the
   932  	// TCP_MAXSEG option.
   933  	MaxSegOption
   934  
   935  	// MTUDiscoverOption is used to set/get the path MTU discovery setting.
   936  	//
   937  	// NOTE: Setting this option to any other value than PMTUDiscoveryDont
   938  	// is not supported and will fail as such, and getting this option will
   939  	// always return PMTUDiscoveryDont.
   940  	MTUDiscoverOption
   941  
   942  	// MulticastTTLOption is used by SetSockOptInt/GetSockOptInt to control
   943  	// the default TTL value for multicast messages. The default is 1.
   944  	MulticastTTLOption
   945  
   946  	// ReceiveQueueSizeOption is used in GetSockOptInt to specify that the
   947  	// number of unread bytes in the input buffer should be returned.
   948  	ReceiveQueueSizeOption
   949  
   950  	// SendQueueSizeOption is used in GetSockOptInt to specify that the
   951  	// number of unread bytes in the output buffer should be returned.
   952  	SendQueueSizeOption
   953  
   954  	// IPv4TTLOption is used by SetSockOptInt/GetSockOptInt to control the default
   955  	// TTL value for unicast messages.
   956  	//
   957  	// The default is configured by DefaultTTLOption. A UseDefaultIPv4TTL value
   958  	// configures the endpoint to use the default.
   959  	IPv4TTLOption
   960  
   961  	// IPv6HopLimitOption is used by SetSockOptInt/GetSockOptInt to control the
   962  	// default hop limit value for unicast messages.
   963  	//
   964  	// The default is configured by DefaultTTLOption. A UseDefaultIPv6HopLimit
   965  	// value configures the endpoint to use the default.
   966  	IPv6HopLimitOption
   967  
   968  	// TCPSynCountOption is used by SetSockOptInt/GetSockOptInt to specify
   969  	// the number of SYN retransmits that TCP should send before aborting
   970  	// the attempt to connect. It cannot exceed 255.
   971  	//
   972  	// NOTE: This option is currently only stubbed out and is no-op.
   973  	TCPSynCountOption
   974  
   975  	// TCPWindowClampOption is used by SetSockOptInt/GetSockOptInt to bound
   976  	// the size of the advertised window to this value.
   977  	//
   978  	// NOTE: This option is currently only stubed out and is a no-op
   979  	TCPWindowClampOption
   980  
   981  	// IPv6Checksum is used to request the stack to populate and validate the IPv6
   982  	// checksum for transport level headers.
   983  	IPv6Checksum
   984  )
   985  
   986  const (
   987  	// UseDefaultIPv4TTL is the IPv4TTLOption value that configures an endpoint to
   988  	// use the default ttl currently configured by the IPv4 protocol (see
   989  	// DefaultTTLOption).
   990  	UseDefaultIPv4TTL = 0
   991  
   992  	// UseDefaultIPv6HopLimit is the IPv6HopLimitOption value that configures an
   993  	// endpoint to use the default hop limit currently configured by the IPv6
   994  	// protocol (see DefaultTTLOption).
   995  	UseDefaultIPv6HopLimit = -1
   996  )
   997  
   998  const (
   999  	// PMTUDiscoveryWant is a setting of the MTUDiscoverOption to use
  1000  	// per-route settings.
  1001  	PMTUDiscoveryWant int = iota
  1002  
  1003  	// PMTUDiscoveryDont is a setting of the MTUDiscoverOption to disable
  1004  	// path MTU discovery.
  1005  	PMTUDiscoveryDont
  1006  
  1007  	// PMTUDiscoveryDo is a setting of the MTUDiscoverOption to always do
  1008  	// path MTU discovery.
  1009  	PMTUDiscoveryDo
  1010  
  1011  	// PMTUDiscoveryProbe is a setting of the MTUDiscoverOption to set DF
  1012  	// but ignore path MTU.
  1013  	PMTUDiscoveryProbe
  1014  )
  1015  
  1016  // GettableNetworkProtocolOption is a marker interface for network protocol
  1017  // options that may be queried.
  1018  type GettableNetworkProtocolOption interface {
  1019  	isGettableNetworkProtocolOption()
  1020  }
  1021  
  1022  // SettableNetworkProtocolOption is a marker interface for network protocol
  1023  // options that may be set.
  1024  type SettableNetworkProtocolOption interface {
  1025  	isSettableNetworkProtocolOption()
  1026  }
  1027  
  1028  // DefaultTTLOption is used by stack.(*Stack).NetworkProtocolOption to specify
  1029  // a default TTL.
  1030  type DefaultTTLOption uint8
  1031  
  1032  func (*DefaultTTLOption) isGettableNetworkProtocolOption() {}
  1033  
  1034  func (*DefaultTTLOption) isSettableNetworkProtocolOption() {}
  1035  
  1036  // GettableTransportProtocolOption is a marker interface for transport protocol
  1037  // options that may be queried.
  1038  type GettableTransportProtocolOption interface {
  1039  	isGettableTransportProtocolOption()
  1040  }
  1041  
  1042  // SettableTransportProtocolOption is a marker interface for transport protocol
  1043  // options that may be set.
  1044  type SettableTransportProtocolOption interface {
  1045  	isSettableTransportProtocolOption()
  1046  }
  1047  
  1048  // TCPSACKEnabled the SACK option for TCP.
  1049  //
  1050  // See: https://tools.ietf.org/html/rfc2018.
  1051  type TCPSACKEnabled bool
  1052  
  1053  func (*TCPSACKEnabled) isGettableTransportProtocolOption() {}
  1054  
  1055  func (*TCPSACKEnabled) isSettableTransportProtocolOption() {}
  1056  
  1057  // TCPRecovery is the loss deteoction algorithm used by TCP.
  1058  type TCPRecovery int32
  1059  
  1060  func (*TCPRecovery) isGettableTransportProtocolOption() {}
  1061  
  1062  func (*TCPRecovery) isSettableTransportProtocolOption() {}
  1063  
  1064  // TCPAlwaysUseSynCookies indicates unconditional usage of syncookies.
  1065  type TCPAlwaysUseSynCookies bool
  1066  
  1067  func (*TCPAlwaysUseSynCookies) isGettableTransportProtocolOption() {}
  1068  
  1069  func (*TCPAlwaysUseSynCookies) isSettableTransportProtocolOption() {}
  1070  
  1071  const (
  1072  	// TCPRACKLossDetection indicates RACK is used for loss detection and
  1073  	// recovery.
  1074  	TCPRACKLossDetection TCPRecovery = 1 << iota
  1075  
  1076  	// TCPRACKStaticReoWnd indicates the reordering window should not be
  1077  	// adjusted when DSACK is received.
  1078  	TCPRACKStaticReoWnd
  1079  
  1080  	// TCPRACKNoDupTh indicates RACK should not consider the classic three
  1081  	// duplicate acknowledgements rule to mark the segments as lost. This
  1082  	// is used when reordering is not detected.
  1083  	TCPRACKNoDupTh
  1084  )
  1085  
  1086  // TCPDelayEnabled enables/disables Nagle's algorithm in TCP.
  1087  type TCPDelayEnabled bool
  1088  
  1089  func (*TCPDelayEnabled) isGettableTransportProtocolOption() {}
  1090  
  1091  func (*TCPDelayEnabled) isSettableTransportProtocolOption() {}
  1092  
  1093  // TCPSendBufferSizeRangeOption is the send buffer size range for TCP.
  1094  type TCPSendBufferSizeRangeOption struct {
  1095  	Min     int
  1096  	Default int
  1097  	Max     int
  1098  }
  1099  
  1100  func (*TCPSendBufferSizeRangeOption) isGettableTransportProtocolOption() {}
  1101  
  1102  func (*TCPSendBufferSizeRangeOption) isSettableTransportProtocolOption() {}
  1103  
  1104  // TCPReceiveBufferSizeRangeOption is the receive buffer size range for TCP.
  1105  type TCPReceiveBufferSizeRangeOption struct {
  1106  	Min     int
  1107  	Default int
  1108  	Max     int
  1109  }
  1110  
  1111  func (*TCPReceiveBufferSizeRangeOption) isGettableTransportProtocolOption() {}
  1112  
  1113  func (*TCPReceiveBufferSizeRangeOption) isSettableTransportProtocolOption() {}
  1114  
  1115  // TCPAvailableCongestionControlOption is the supported congestion control
  1116  // algorithms for TCP
  1117  type TCPAvailableCongestionControlOption string
  1118  
  1119  func (*TCPAvailableCongestionControlOption) isGettableTransportProtocolOption() {}
  1120  
  1121  func (*TCPAvailableCongestionControlOption) isSettableTransportProtocolOption() {}
  1122  
  1123  // TCPModerateReceiveBufferOption enables/disables receive buffer moderation
  1124  // for TCP.
  1125  type TCPModerateReceiveBufferOption bool
  1126  
  1127  func (*TCPModerateReceiveBufferOption) isGettableTransportProtocolOption() {}
  1128  
  1129  func (*TCPModerateReceiveBufferOption) isSettableTransportProtocolOption() {}
  1130  
  1131  // GettableSocketOption is a marker interface for socket options that may be
  1132  // queried.
  1133  type GettableSocketOption interface {
  1134  	isGettableSocketOption()
  1135  }
  1136  
  1137  // SettableSocketOption is a marker interface for socket options that may be
  1138  // configured.
  1139  type SettableSocketOption interface {
  1140  	isSettableSocketOption()
  1141  }
  1142  
  1143  // ICMPv6Filter specifies a filter for ICMPv6 types.
  1144  //
  1145  // +stateify savable
  1146  type ICMPv6Filter struct {
  1147  	// DenyType indicates if an ICMP type should be blocked.
  1148  	//
  1149  	// The ICMPv6 type field is 8 bits so there are up to 256 different ICMPv6
  1150  	// types.
  1151  	DenyType [8]uint32
  1152  }
  1153  
  1154  // ShouldDeny returns true iff the ICMPv6 Type should be denied.
  1155  func (f *ICMPv6Filter) ShouldDeny(icmpType uint8) bool {
  1156  	const bitsInUint32 = 32
  1157  	i := icmpType / bitsInUint32
  1158  	b := icmpType % bitsInUint32
  1159  	return f.DenyType[i]&(1<<b) != 0
  1160  }
  1161  
  1162  func (*ICMPv6Filter) isGettableSocketOption() {}
  1163  
  1164  func (*ICMPv6Filter) isSettableSocketOption() {}
  1165  
  1166  // EndpointState represents the state of an endpoint.
  1167  type EndpointState uint8
  1168  
  1169  // CongestionControlState indicates the current congestion control state for
  1170  // TCP sender.
  1171  type CongestionControlState int
  1172  
  1173  const (
  1174  	// Open indicates that the sender is receiving acks in order and
  1175  	// no loss or dupACK's etc have been detected.
  1176  	Open CongestionControlState = iota
  1177  	// RTORecovery indicates that an RTO has occurred and the sender
  1178  	// has entered an RTO based recovery phase.
  1179  	RTORecovery
  1180  	// FastRecovery indicates that the sender has entered FastRecovery
  1181  	// based on receiving nDupAck's. This state is entered only when
  1182  	// SACK is not in use.
  1183  	FastRecovery
  1184  	// SACKRecovery indicates that the sender has entered SACK based
  1185  	// recovery.
  1186  	SACKRecovery
  1187  	// Disorder indicates the sender either received some SACK blocks
  1188  	// or dupACK's.
  1189  	Disorder
  1190  )
  1191  
  1192  // TCPInfoOption is used by GetSockOpt to expose TCP statistics.
  1193  //
  1194  // TODO(b/64800844): Add and populate stat fields.
  1195  type TCPInfoOption struct {
  1196  	// RTT is the smoothed round trip time.
  1197  	RTT time.Duration
  1198  
  1199  	// RTTVar is the round trip time variation.
  1200  	RTTVar time.Duration
  1201  
  1202  	// RTO is the retransmission timeout for the endpoint.
  1203  	RTO time.Duration
  1204  
  1205  	// State is the current endpoint protocol state.
  1206  	State EndpointState
  1207  
  1208  	// CcState is the congestion control state.
  1209  	CcState CongestionControlState
  1210  
  1211  	// SndCwnd is the congestion window, in packets.
  1212  	SndCwnd uint32
  1213  
  1214  	// SndSsthresh is the threshold between slow start and congestion
  1215  	// avoidance.
  1216  	SndSsthresh uint32
  1217  
  1218  	// ReorderSeen indicates if reordering is seen in the endpoint.
  1219  	ReorderSeen bool
  1220  }
  1221  
  1222  func (*TCPInfoOption) isGettableSocketOption() {}
  1223  
  1224  // KeepaliveIdleOption is used by SetSockOpt/GetSockOpt to specify the time a
  1225  // connection must remain idle before the first TCP keepalive packet is sent.
  1226  // Once this time is reached, KeepaliveIntervalOption is used instead.
  1227  type KeepaliveIdleOption time.Duration
  1228  
  1229  func (*KeepaliveIdleOption) isGettableSocketOption() {}
  1230  
  1231  func (*KeepaliveIdleOption) isSettableSocketOption() {}
  1232  
  1233  // KeepaliveIntervalOption is used by SetSockOpt/GetSockOpt to specify the
  1234  // interval between sending TCP keepalive packets.
  1235  type KeepaliveIntervalOption time.Duration
  1236  
  1237  func (*KeepaliveIntervalOption) isGettableSocketOption() {}
  1238  
  1239  func (*KeepaliveIntervalOption) isSettableSocketOption() {}
  1240  
  1241  // TCPUserTimeoutOption is used by SetSockOpt/GetSockOpt to specify a user
  1242  // specified timeout for a given TCP connection.
  1243  // See: RFC5482 for details.
  1244  type TCPUserTimeoutOption time.Duration
  1245  
  1246  func (*TCPUserTimeoutOption) isGettableSocketOption() {}
  1247  
  1248  func (*TCPUserTimeoutOption) isSettableSocketOption() {}
  1249  
  1250  // CongestionControlOption is used by SetSockOpt/GetSockOpt to set/get
  1251  // the current congestion control algorithm.
  1252  type CongestionControlOption string
  1253  
  1254  func (*CongestionControlOption) isGettableSocketOption() {}
  1255  
  1256  func (*CongestionControlOption) isSettableSocketOption() {}
  1257  
  1258  func (*CongestionControlOption) isGettableTransportProtocolOption() {}
  1259  
  1260  func (*CongestionControlOption) isSettableTransportProtocolOption() {}
  1261  
  1262  // TCPLingerTimeoutOption is used by SetSockOpt/GetSockOpt to set/get the
  1263  // maximum duration for which a socket lingers in the TCP_FIN_WAIT_2 state
  1264  // before being marked closed.
  1265  type TCPLingerTimeoutOption time.Duration
  1266  
  1267  func (*TCPLingerTimeoutOption) isGettableSocketOption() {}
  1268  
  1269  func (*TCPLingerTimeoutOption) isSettableSocketOption() {}
  1270  
  1271  func (*TCPLingerTimeoutOption) isGettableTransportProtocolOption() {}
  1272  
  1273  func (*TCPLingerTimeoutOption) isSettableTransportProtocolOption() {}
  1274  
  1275  // TCPTimeWaitTimeoutOption is used by SetSockOpt/GetSockOpt to set/get the
  1276  // maximum duration for which a socket lingers in the TIME_WAIT state
  1277  // before being marked closed.
  1278  type TCPTimeWaitTimeoutOption time.Duration
  1279  
  1280  func (*TCPTimeWaitTimeoutOption) isGettableSocketOption() {}
  1281  
  1282  func (*TCPTimeWaitTimeoutOption) isSettableSocketOption() {}
  1283  
  1284  func (*TCPTimeWaitTimeoutOption) isGettableTransportProtocolOption() {}
  1285  
  1286  func (*TCPTimeWaitTimeoutOption) isSettableTransportProtocolOption() {}
  1287  
  1288  // TCPDeferAcceptOption is used by SetSockOpt/GetSockOpt to allow a
  1289  // accept to return a completed connection only when there is data to be
  1290  // read. This usually means the listening socket will drop the final ACK
  1291  // for a handshake till the specified timeout until a segment with data arrives.
  1292  type TCPDeferAcceptOption time.Duration
  1293  
  1294  func (*TCPDeferAcceptOption) isGettableSocketOption() {}
  1295  
  1296  func (*TCPDeferAcceptOption) isSettableSocketOption() {}
  1297  
  1298  // TCPMinRTOOption is use by SetSockOpt/GetSockOpt to allow overriding
  1299  // default MinRTO used by the Stack.
  1300  type TCPMinRTOOption time.Duration
  1301  
  1302  func (*TCPMinRTOOption) isGettableSocketOption() {}
  1303  
  1304  func (*TCPMinRTOOption) isSettableSocketOption() {}
  1305  
  1306  func (*TCPMinRTOOption) isGettableTransportProtocolOption() {}
  1307  
  1308  func (*TCPMinRTOOption) isSettableTransportProtocolOption() {}
  1309  
  1310  // TCPMaxRTOOption is use by SetSockOpt/GetSockOpt to allow overriding
  1311  // default MaxRTO used by the Stack.
  1312  type TCPMaxRTOOption time.Duration
  1313  
  1314  func (*TCPMaxRTOOption) isGettableSocketOption() {}
  1315  
  1316  func (*TCPMaxRTOOption) isSettableSocketOption() {}
  1317  
  1318  func (*TCPMaxRTOOption) isGettableTransportProtocolOption() {}
  1319  
  1320  func (*TCPMaxRTOOption) isSettableTransportProtocolOption() {}
  1321  
  1322  // TCPMaxRetriesOption is used by SetSockOpt/GetSockOpt to set/get the
  1323  // maximum number of retransmits after which we time out the connection.
  1324  type TCPMaxRetriesOption uint64
  1325  
  1326  func (*TCPMaxRetriesOption) isGettableSocketOption() {}
  1327  
  1328  func (*TCPMaxRetriesOption) isSettableSocketOption() {}
  1329  
  1330  func (*TCPMaxRetriesOption) isGettableTransportProtocolOption() {}
  1331  
  1332  func (*TCPMaxRetriesOption) isSettableTransportProtocolOption() {}
  1333  
  1334  // TCPSynRetriesOption is used by SetSockOpt/GetSockOpt to specify stack-wide
  1335  // default for number of times SYN is retransmitted before aborting a connect.
  1336  type TCPSynRetriesOption uint8
  1337  
  1338  func (*TCPSynRetriesOption) isGettableSocketOption() {}
  1339  
  1340  func (*TCPSynRetriesOption) isSettableSocketOption() {}
  1341  
  1342  func (*TCPSynRetriesOption) isGettableTransportProtocolOption() {}
  1343  
  1344  func (*TCPSynRetriesOption) isSettableTransportProtocolOption() {}
  1345  
  1346  // MulticastInterfaceOption is used by SetSockOpt/GetSockOpt to specify a
  1347  // default interface for multicast.
  1348  type MulticastInterfaceOption struct {
  1349  	NIC           NICID
  1350  	InterfaceAddr Address
  1351  }
  1352  
  1353  func (*MulticastInterfaceOption) isGettableSocketOption() {}
  1354  
  1355  func (*MulticastInterfaceOption) isSettableSocketOption() {}
  1356  
  1357  // MembershipOption is used to identify a multicast membership on an interface.
  1358  type MembershipOption struct {
  1359  	NIC           NICID
  1360  	InterfaceAddr Address
  1361  	MulticastAddr Address
  1362  }
  1363  
  1364  // AddMembershipOption identifies a multicast group to join on some interface.
  1365  type AddMembershipOption MembershipOption
  1366  
  1367  func (*AddMembershipOption) isSettableSocketOption() {}
  1368  
  1369  // RemoveMembershipOption identifies a multicast group to leave on some
  1370  // interface.
  1371  type RemoveMembershipOption MembershipOption
  1372  
  1373  func (*RemoveMembershipOption) isSettableSocketOption() {}
  1374  
  1375  // SocketDetachFilterOption is used by SetSockOpt to detach a previously attached
  1376  // classic BPF filter on a given endpoint.
  1377  type SocketDetachFilterOption int
  1378  
  1379  func (*SocketDetachFilterOption) isSettableSocketOption() {}
  1380  
  1381  // OriginalDestinationOption is used to get the original destination address
  1382  // and port of a redirected packet.
  1383  type OriginalDestinationOption FullAddress
  1384  
  1385  func (*OriginalDestinationOption) isGettableSocketOption() {}
  1386  
  1387  // TCPTimeWaitReuseOption is used stack.(*Stack).TransportProtocolOption to
  1388  // specify if the stack can reuse the port bound by an endpoint in TIME-WAIT for
  1389  // new connections when it is safe from protocol viewpoint.
  1390  type TCPTimeWaitReuseOption uint8
  1391  
  1392  func (*TCPTimeWaitReuseOption) isGettableSocketOption() {}
  1393  
  1394  func (*TCPTimeWaitReuseOption) isSettableSocketOption() {}
  1395  
  1396  func (*TCPTimeWaitReuseOption) isGettableTransportProtocolOption() {}
  1397  
  1398  func (*TCPTimeWaitReuseOption) isSettableTransportProtocolOption() {}
  1399  
  1400  const (
  1401  	// TCPTimeWaitReuseDisabled indicates reuse of port bound by endpoints in TIME-WAIT cannot
  1402  	// be reused for new connections.
  1403  	TCPTimeWaitReuseDisabled TCPTimeWaitReuseOption = iota
  1404  
  1405  	// TCPTimeWaitReuseGlobal indicates reuse of port bound by endpoints in TIME-WAIT can
  1406  	// be reused for new connections irrespective of the src/dest addresses.
  1407  	TCPTimeWaitReuseGlobal
  1408  
  1409  	// TCPTimeWaitReuseLoopbackOnly indicates reuse of port bound by endpoint in TIME-WAIT can
  1410  	// only be reused if the connection was a connection over loopback. i.e src/dest addresses
  1411  	// are loopback addresses.
  1412  	TCPTimeWaitReuseLoopbackOnly
  1413  )
  1414  
  1415  // LingerOption is used by SetSockOpt/GetSockOpt to set/get the
  1416  // duration for which a socket lingers before returning from Close.
  1417  //
  1418  // +marshal
  1419  // +stateify savable
  1420  type LingerOption struct {
  1421  	Enabled bool
  1422  	Timeout time.Duration
  1423  }
  1424  
  1425  // IPPacketInfo is the message structure for IP_PKTINFO.
  1426  //
  1427  // +stateify savable
  1428  type IPPacketInfo struct {
  1429  	// NIC is the ID of the NIC to be used.
  1430  	NIC NICID
  1431  
  1432  	// LocalAddr is the local address.
  1433  	LocalAddr Address
  1434  
  1435  	// DestinationAddr is the destination address found in the IP header.
  1436  	DestinationAddr Address
  1437  }
  1438  
  1439  // IPv6PacketInfo is the message structure for IPV6_PKTINFO.
  1440  //
  1441  // +stateify savable
  1442  type IPv6PacketInfo struct {
  1443  	Addr Address
  1444  	NIC  NICID
  1445  }
  1446  
  1447  // SendBufferSizeOption is used by stack.(Stack*).Option/SetOption to
  1448  // get/set the default, min and max send buffer sizes.
  1449  type SendBufferSizeOption struct {
  1450  	// Min is the minimum size for send buffer.
  1451  	Min int
  1452  
  1453  	// Default is the default size for send buffer.
  1454  	Default int
  1455  
  1456  	// Max is the maximum size for send buffer.
  1457  	Max int
  1458  }
  1459  
  1460  // ReceiveBufferSizeOption is used by stack.(Stack*).Option/SetOption to
  1461  // get/set the default, min and max receive buffer sizes.
  1462  type ReceiveBufferSizeOption struct {
  1463  	// Min is the minimum size for send buffer.
  1464  	Min int
  1465  
  1466  	// Default is the default size for send buffer.
  1467  	Default int
  1468  
  1469  	// Max is the maximum size for send buffer.
  1470  	Max int
  1471  }
  1472  
  1473  // GetSendBufferLimits is used to get the send buffer size limits.
  1474  type GetSendBufferLimits func(StackHandler) SendBufferSizeOption
  1475  
  1476  // GetStackSendBufferLimits is used to get default, min and max send buffer size.
  1477  func GetStackSendBufferLimits(so StackHandler) SendBufferSizeOption {
  1478  	var ss SendBufferSizeOption
  1479  	if err := so.Option(&ss); err != nil {
  1480  		panic(fmt.Sprintf("s.Option(%#v) = %s", ss, err))
  1481  	}
  1482  	return ss
  1483  }
  1484  
  1485  // GetReceiveBufferLimits is used to get the send buffer size limits.
  1486  type GetReceiveBufferLimits func(StackHandler) ReceiveBufferSizeOption
  1487  
  1488  // GetStackReceiveBufferLimits is used to get default, min and max send buffer size.
  1489  func GetStackReceiveBufferLimits(so StackHandler) ReceiveBufferSizeOption {
  1490  	var ss ReceiveBufferSizeOption
  1491  	if err := so.Option(&ss); err != nil {
  1492  		panic(fmt.Sprintf("s.Option(%#v) = %s", ss, err))
  1493  	}
  1494  	return ss
  1495  }
  1496  
  1497  // Route is a row in the routing table. It specifies through which NIC (and
  1498  // gateway) sets of packets should be routed. A row is considered viable if the
  1499  // masked target address matches the destination address in the row.
  1500  type Route struct {
  1501  	// Destination must contain the target address for this row to be viable.
  1502  	Destination Subnet
  1503  
  1504  	// Gateway is the gateway to be used if this row is viable.
  1505  	Gateway Address
  1506  
  1507  	// NIC is the id of the nic to be used if this row is viable.
  1508  	NIC NICID
  1509  
  1510  	// SourceHint indicates a preferred source address to use when NICs
  1511  	// have multiple addresses.
  1512  	SourceHint Address
  1513  }
  1514  
  1515  // String implements the fmt.Stringer interface.
  1516  func (r Route) String() string {
  1517  	var out strings.Builder
  1518  	_, _ = fmt.Fprintf(&out, "%s", r.Destination)
  1519  	if r.Gateway.length > 0 {
  1520  		_, _ = fmt.Fprintf(&out, " via %s", r.Gateway)
  1521  	}
  1522  	_, _ = fmt.Fprintf(&out, " nic %d", r.NIC)
  1523  	return out.String()
  1524  }
  1525  
  1526  // Equal returns true if the given Route is equal to this Route.
  1527  func (r Route) Equal(to Route) bool {
  1528  	// NOTE: This relies on the fact that r.Destination == to.Destination
  1529  	return r.Destination.Equal(to.Destination) && r.Gateway == to.Gateway && r.NIC == to.NIC
  1530  }
  1531  
  1532  // TransportProtocolNumber is the number of a transport protocol.
  1533  type TransportProtocolNumber uint32
  1534  
  1535  // NetworkProtocolNumber is the EtherType of a network protocol in an Ethernet
  1536  // frame.
  1537  //
  1538  // See: https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml
  1539  type NetworkProtocolNumber uint32
  1540  
  1541  // A StatCounter keeps track of a statistic.
  1542  //
  1543  // +stateify savable
  1544  type StatCounter struct {
  1545  	count atomicbitops.Uint64
  1546  }
  1547  
  1548  // Increment adds one to the counter.
  1549  func (s *StatCounter) Increment() {
  1550  	s.IncrementBy(1)
  1551  }
  1552  
  1553  // Decrement minuses one to the counter.
  1554  func (s *StatCounter) Decrement() {
  1555  	s.IncrementBy(^uint64(0))
  1556  }
  1557  
  1558  // Value returns the current value of the counter.
  1559  func (s *StatCounter) Value() uint64 {
  1560  	return s.count.Load()
  1561  }
  1562  
  1563  // IncrementBy increments the counter by v.
  1564  func (s *StatCounter) IncrementBy(v uint64) {
  1565  	s.count.Add(v)
  1566  }
  1567  
  1568  func (s *StatCounter) String() string {
  1569  	return strconv.FormatUint(s.Value(), 10)
  1570  }
  1571  
  1572  // A MultiCounterStat keeps track of two counters at once.
  1573  type MultiCounterStat struct {
  1574  	a *StatCounter
  1575  	b *StatCounter
  1576  }
  1577  
  1578  // Init sets both internal counters to point to a and b.
  1579  func (m *MultiCounterStat) Init(a, b *StatCounter) {
  1580  	m.a = a
  1581  	m.b = b
  1582  }
  1583  
  1584  // Increment adds one to the counters.
  1585  func (m *MultiCounterStat) Increment() {
  1586  	m.a.Increment()
  1587  	m.b.Increment()
  1588  }
  1589  
  1590  // IncrementBy increments the counters by v.
  1591  func (m *MultiCounterStat) IncrementBy(v uint64) {
  1592  	m.a.IncrementBy(v)
  1593  	m.b.IncrementBy(v)
  1594  }
  1595  
  1596  // ICMPv4PacketStats enumerates counts for all ICMPv4 packet types.
  1597  type ICMPv4PacketStats struct {
  1598  	// LINT.IfChange(ICMPv4PacketStats)
  1599  
  1600  	// EchoRequest is the number of ICMPv4 echo packets counted.
  1601  	EchoRequest *StatCounter
  1602  
  1603  	// EchoReply is the number of ICMPv4 echo reply packets counted.
  1604  	EchoReply *StatCounter
  1605  
  1606  	// DstUnreachable is the number of ICMPv4 destination unreachable packets
  1607  	// counted.
  1608  	DstUnreachable *StatCounter
  1609  
  1610  	// SrcQuench is the number of ICMPv4 source quench packets counted.
  1611  	SrcQuench *StatCounter
  1612  
  1613  	// Redirect is the number of ICMPv4 redirect packets counted.
  1614  	Redirect *StatCounter
  1615  
  1616  	// TimeExceeded is the number of ICMPv4 time exceeded packets counted.
  1617  	TimeExceeded *StatCounter
  1618  
  1619  	// ParamProblem is the number of ICMPv4 parameter problem packets counted.
  1620  	ParamProblem *StatCounter
  1621  
  1622  	// Timestamp is the number of ICMPv4 timestamp packets counted.
  1623  	Timestamp *StatCounter
  1624  
  1625  	// TimestampReply is the number of ICMPv4 timestamp reply packets counted.
  1626  	TimestampReply *StatCounter
  1627  
  1628  	// InfoRequest is the number of ICMPv4 information request packets counted.
  1629  	InfoRequest *StatCounter
  1630  
  1631  	// InfoReply is the number of ICMPv4 information reply packets counted.
  1632  	InfoReply *StatCounter
  1633  
  1634  	// LINT.ThenChange(network/ipv4/stats.go:multiCounterICMPv4PacketStats)
  1635  }
  1636  
  1637  // ICMPv4SentPacketStats collects outbound ICMPv4-specific stats.
  1638  type ICMPv4SentPacketStats struct {
  1639  	// LINT.IfChange(ICMPv4SentPacketStats)
  1640  
  1641  	ICMPv4PacketStats
  1642  
  1643  	// Dropped is the number of ICMPv4 packets dropped due to link layer errors.
  1644  	Dropped *StatCounter
  1645  
  1646  	// RateLimited is the number of ICMPv4 packets dropped due to rate limit being
  1647  	// exceeded.
  1648  	RateLimited *StatCounter
  1649  
  1650  	// LINT.ThenChange(network/ipv4/stats.go:multiCounterICMPv4SentPacketStats)
  1651  }
  1652  
  1653  // ICMPv4ReceivedPacketStats collects inbound ICMPv4-specific stats.
  1654  type ICMPv4ReceivedPacketStats struct {
  1655  	// LINT.IfChange(ICMPv4ReceivedPacketStats)
  1656  
  1657  	ICMPv4PacketStats
  1658  
  1659  	// Invalid is the number of invalid ICMPv4 packets received.
  1660  	Invalid *StatCounter
  1661  
  1662  	// LINT.ThenChange(network/ipv4/stats.go:multiCounterICMPv4ReceivedPacketStats)
  1663  }
  1664  
  1665  // ICMPv4Stats collects ICMPv4-specific stats.
  1666  type ICMPv4Stats struct {
  1667  	// LINT.IfChange(ICMPv4Stats)
  1668  
  1669  	// PacketsSent contains statistics about sent packets.
  1670  	PacketsSent ICMPv4SentPacketStats
  1671  
  1672  	// PacketsReceived contains statistics about received packets.
  1673  	PacketsReceived ICMPv4ReceivedPacketStats
  1674  
  1675  	// LINT.ThenChange(network/ipv4/stats.go:multiCounterICMPv4Stats)
  1676  }
  1677  
  1678  // ICMPv6PacketStats enumerates counts for all ICMPv6 packet types.
  1679  type ICMPv6PacketStats struct {
  1680  	// LINT.IfChange(ICMPv6PacketStats)
  1681  
  1682  	// EchoRequest is the number of ICMPv6 echo request packets counted.
  1683  	EchoRequest *StatCounter
  1684  
  1685  	// EchoReply is the number of ICMPv6 echo reply packets counted.
  1686  	EchoReply *StatCounter
  1687  
  1688  	// DstUnreachable is the number of ICMPv6 destination unreachable packets
  1689  	// counted.
  1690  	DstUnreachable *StatCounter
  1691  
  1692  	// PacketTooBig is the number of ICMPv6 packet too big packets counted.
  1693  	PacketTooBig *StatCounter
  1694  
  1695  	// TimeExceeded is the number of ICMPv6 time exceeded packets counted.
  1696  	TimeExceeded *StatCounter
  1697  
  1698  	// ParamProblem is the number of ICMPv6 parameter problem packets counted.
  1699  	ParamProblem *StatCounter
  1700  
  1701  	// RouterSolicit is the number of ICMPv6 router solicit packets counted.
  1702  	RouterSolicit *StatCounter
  1703  
  1704  	// RouterAdvert is the number of ICMPv6 router advert packets counted.
  1705  	RouterAdvert *StatCounter
  1706  
  1707  	// NeighborSolicit is the number of ICMPv6 neighbor solicit packets counted.
  1708  	NeighborSolicit *StatCounter
  1709  
  1710  	// NeighborAdvert is the number of ICMPv6 neighbor advert packets counted.
  1711  	NeighborAdvert *StatCounter
  1712  
  1713  	// RedirectMsg is the number of ICMPv6 redirect message packets counted.
  1714  	RedirectMsg *StatCounter
  1715  
  1716  	// MulticastListenerQuery is the number of Multicast Listener Query messages
  1717  	// counted.
  1718  	MulticastListenerQuery *StatCounter
  1719  
  1720  	// MulticastListenerReport is the number of Multicast Listener Report messages
  1721  	// counted.
  1722  	MulticastListenerReport *StatCounter
  1723  
  1724  	// MulticastListenerReportV2 is the number of Multicast Listener Report
  1725  	// messages counted.
  1726  	MulticastListenerReportV2 *StatCounter
  1727  
  1728  	// MulticastListenerDone is the number of Multicast Listener Done messages
  1729  	// counted.
  1730  	MulticastListenerDone *StatCounter
  1731  
  1732  	// LINT.ThenChange(network/ipv6/stats.go:multiCounterICMPv6PacketStats)
  1733  }
  1734  
  1735  // ICMPv6SentPacketStats collects outbound ICMPv6-specific stats.
  1736  type ICMPv6SentPacketStats struct {
  1737  	// LINT.IfChange(ICMPv6SentPacketStats)
  1738  
  1739  	ICMPv6PacketStats
  1740  
  1741  	// Dropped is the number of ICMPv6 packets dropped due to link layer errors.
  1742  	Dropped *StatCounter
  1743  
  1744  	// RateLimited is the number of ICMPv6 packets dropped due to rate limit being
  1745  	// exceeded.
  1746  	RateLimited *StatCounter
  1747  
  1748  	// LINT.ThenChange(network/ipv6/stats.go:multiCounterICMPv6SentPacketStats)
  1749  }
  1750  
  1751  // ICMPv6ReceivedPacketStats collects inbound ICMPv6-specific stats.
  1752  type ICMPv6ReceivedPacketStats struct {
  1753  	// LINT.IfChange(ICMPv6ReceivedPacketStats)
  1754  
  1755  	ICMPv6PacketStats
  1756  
  1757  	// Unrecognized is the number of ICMPv6 packets received that the transport
  1758  	// layer does not know how to parse.
  1759  	Unrecognized *StatCounter
  1760  
  1761  	// Invalid is the number of invalid ICMPv6 packets received.
  1762  	Invalid *StatCounter
  1763  
  1764  	// RouterOnlyPacketsDroppedByHost is the number of ICMPv6 packets dropped due
  1765  	// to being router-specific packets.
  1766  	RouterOnlyPacketsDroppedByHost *StatCounter
  1767  
  1768  	// LINT.ThenChange(network/ipv6/stats.go:multiCounterICMPv6ReceivedPacketStats)
  1769  }
  1770  
  1771  // ICMPv6Stats collects ICMPv6-specific stats.
  1772  type ICMPv6Stats struct {
  1773  	// LINT.IfChange(ICMPv6Stats)
  1774  
  1775  	// PacketsSent contains statistics about sent packets.
  1776  	PacketsSent ICMPv6SentPacketStats
  1777  
  1778  	// PacketsReceived contains statistics about received packets.
  1779  	PacketsReceived ICMPv6ReceivedPacketStats
  1780  
  1781  	// LINT.ThenChange(network/ipv6/stats.go:multiCounterICMPv6Stats)
  1782  }
  1783  
  1784  // ICMPStats collects ICMP-specific stats (both v4 and v6).
  1785  type ICMPStats struct {
  1786  	// V4 contains the ICMPv4-specifics stats.
  1787  	V4 ICMPv4Stats
  1788  
  1789  	// V6 contains the ICMPv4-specifics stats.
  1790  	V6 ICMPv6Stats
  1791  }
  1792  
  1793  // IGMPPacketStats enumerates counts for all IGMP packet types.
  1794  type IGMPPacketStats struct {
  1795  	// LINT.IfChange(IGMPPacketStats)
  1796  
  1797  	// MembershipQuery is the number of Membership Query messages counted.
  1798  	MembershipQuery *StatCounter
  1799  
  1800  	// V1MembershipReport is the number of Version 1 Membership Report messages
  1801  	// counted.
  1802  	V1MembershipReport *StatCounter
  1803  
  1804  	// V2MembershipReport is the number of Version 2 Membership Report messages
  1805  	// counted.
  1806  	V2MembershipReport *StatCounter
  1807  
  1808  	// V3MembershipReport is the number of Version 3 Membership Report messages
  1809  	// counted.
  1810  	V3MembershipReport *StatCounter
  1811  
  1812  	// LeaveGroup is the number of Leave Group messages counted.
  1813  	LeaveGroup *StatCounter
  1814  
  1815  	// LINT.ThenChange(network/ipv4/stats.go:multiCounterIGMPPacketStats)
  1816  }
  1817  
  1818  // IGMPSentPacketStats collects outbound IGMP-specific stats.
  1819  type IGMPSentPacketStats struct {
  1820  	// LINT.IfChange(IGMPSentPacketStats)
  1821  
  1822  	IGMPPacketStats
  1823  
  1824  	// Dropped is the number of IGMP packets dropped.
  1825  	Dropped *StatCounter
  1826  
  1827  	// LINT.ThenChange(network/ipv4/stats.go:multiCounterIGMPSentPacketStats)
  1828  }
  1829  
  1830  // IGMPReceivedPacketStats collects inbound IGMP-specific stats.
  1831  type IGMPReceivedPacketStats struct {
  1832  	// LINT.IfChange(IGMPReceivedPacketStats)
  1833  
  1834  	IGMPPacketStats
  1835  
  1836  	// Invalid is the number of invalid IGMP packets received.
  1837  	Invalid *StatCounter
  1838  
  1839  	// ChecksumErrors is the number of IGMP packets dropped due to bad checksums.
  1840  	ChecksumErrors *StatCounter
  1841  
  1842  	// Unrecognized is the number of unrecognized messages counted, these are
  1843  	// silently ignored for forward-compatibilty.
  1844  	Unrecognized *StatCounter
  1845  
  1846  	// LINT.ThenChange(network/ipv4/stats.go:multiCounterIGMPReceivedPacketStats)
  1847  }
  1848  
  1849  // IGMPStats collects IGMP-specific stats.
  1850  type IGMPStats struct {
  1851  	// LINT.IfChange(IGMPStats)
  1852  
  1853  	// PacketsSent contains statistics about sent packets.
  1854  	PacketsSent IGMPSentPacketStats
  1855  
  1856  	// PacketsReceived contains statistics about received packets.
  1857  	PacketsReceived IGMPReceivedPacketStats
  1858  
  1859  	// LINT.ThenChange(network/ipv4/stats.go:multiCounterIGMPStats)
  1860  }
  1861  
  1862  // IPForwardingStats collects stats related to IP forwarding (both v4 and v6).
  1863  type IPForwardingStats struct {
  1864  	// LINT.IfChange(IPForwardingStats)
  1865  
  1866  	// Unrouteable is the number of IP packets received which were dropped
  1867  	// because a route to their destination could not be constructed.
  1868  	Unrouteable *StatCounter
  1869  
  1870  	// ExhaustedTTL is the number of IP packets received which were dropped
  1871  	// because their TTL was exhausted.
  1872  	ExhaustedTTL *StatCounter
  1873  
  1874  	// InitializingSource is the number of IP packets which were dropped
  1875  	// because they contained a source address that may only be used on the local
  1876  	// network as part of initialization work.
  1877  	InitializingSource *StatCounter
  1878  
  1879  	// LinkLocalSource is the number of IP packets which were dropped
  1880  	// because they contained a link-local source address.
  1881  	LinkLocalSource *StatCounter
  1882  
  1883  	// LinkLocalDestination is the number of IP packets which were dropped
  1884  	// because they contained a link-local destination address.
  1885  	LinkLocalDestination *StatCounter
  1886  
  1887  	// PacketTooBig is the number of IP packets which were dropped because they
  1888  	// were too big for the outgoing MTU.
  1889  	PacketTooBig *StatCounter
  1890  
  1891  	// HostUnreachable is the number of IP packets received which could not be
  1892  	// successfully forwarded due to an unresolvable next hop.
  1893  	HostUnreachable *StatCounter
  1894  
  1895  	// ExtensionHeaderProblem is the number of IP packets which were dropped
  1896  	// because of a problem encountered when processing an IPv6 extension
  1897  	// header.
  1898  	ExtensionHeaderProblem *StatCounter
  1899  
  1900  	// UnexpectedMulticastInputInterface is the number of multicast packets that
  1901  	// were received on an interface that did not match the corresponding route's
  1902  	// expected input interface.
  1903  	UnexpectedMulticastInputInterface *StatCounter
  1904  
  1905  	// UnknownOutputEndpoint is the number of packets that could not be forwarded
  1906  	// because the output endpoint could not be found.
  1907  	UnknownOutputEndpoint *StatCounter
  1908  
  1909  	// NoMulticastPendingQueueBufferSpace is the number of multicast packets that
  1910  	// were dropped due to insufficient buffer space in the pending packet queue.
  1911  	NoMulticastPendingQueueBufferSpace *StatCounter
  1912  
  1913  	// OutgoingDeviceNoBufferSpace is the number of packets that were dropped due
  1914  	// to insufficient space in the outgoing device.
  1915  	OutgoingDeviceNoBufferSpace *StatCounter
  1916  
  1917  	// Errors is the number of IP packets received which could not be
  1918  	// successfully forwarded.
  1919  	Errors *StatCounter
  1920  
  1921  	// LINT.ThenChange(network/internal/ip/stats.go:MultiCounterIPForwardingStats)
  1922  }
  1923  
  1924  // IPStats collects IP-specific stats (both v4 and v6).
  1925  type IPStats struct {
  1926  	// LINT.IfChange(IPStats)
  1927  
  1928  	// PacketsReceived is the number of IP packets received from the link layer.
  1929  	PacketsReceived *StatCounter
  1930  
  1931  	// ValidPacketsReceived is the number of valid IP packets that reached the IP
  1932  	// layer.
  1933  	ValidPacketsReceived *StatCounter
  1934  
  1935  	// DisabledPacketsReceived is the number of IP packets received from the link
  1936  	// layer when the IP layer is disabled.
  1937  	DisabledPacketsReceived *StatCounter
  1938  
  1939  	// InvalidDestinationAddressesReceived is the number of IP packets received
  1940  	// with an unknown or invalid destination address.
  1941  	InvalidDestinationAddressesReceived *StatCounter
  1942  
  1943  	// InvalidSourceAddressesReceived is the number of IP packets received with a
  1944  	// source address that should never have been received on the wire.
  1945  	InvalidSourceAddressesReceived *StatCounter
  1946  
  1947  	// PacketsDelivered is the number of incoming IP packets that are successfully
  1948  	// delivered to the transport layer.
  1949  	PacketsDelivered *StatCounter
  1950  
  1951  	// PacketsSent is the number of IP packets sent via WritePacket.
  1952  	PacketsSent *StatCounter
  1953  
  1954  	// OutgoingPacketErrors is the number of IP packets which failed to write to a
  1955  	// link-layer endpoint.
  1956  	OutgoingPacketErrors *StatCounter
  1957  
  1958  	// MalformedPacketsReceived is the number of IP Packets that were dropped due
  1959  	// to the IP packet header failing validation checks.
  1960  	MalformedPacketsReceived *StatCounter
  1961  
  1962  	// MalformedFragmentsReceived is the number of IP Fragments that were dropped
  1963  	// due to the fragment failing validation checks.
  1964  	MalformedFragmentsReceived *StatCounter
  1965  
  1966  	// IPTablesPreroutingDropped is the number of IP packets dropped in the
  1967  	// Prerouting chain.
  1968  	IPTablesPreroutingDropped *StatCounter
  1969  
  1970  	// IPTablesInputDropped is the number of IP packets dropped in the Input
  1971  	// chain.
  1972  	IPTablesInputDropped *StatCounter
  1973  
  1974  	// IPTablesForwardDropped is the number of IP packets dropped in the Forward
  1975  	// chain.
  1976  	IPTablesForwardDropped *StatCounter
  1977  
  1978  	// IPTablesOutputDropped is the number of IP packets dropped in the Output
  1979  	// chain.
  1980  	IPTablesOutputDropped *StatCounter
  1981  
  1982  	// IPTablesPostroutingDropped is the number of IP packets dropped in the
  1983  	// Postrouting chain.
  1984  	IPTablesPostroutingDropped *StatCounter
  1985  
  1986  	// TODO(https://gvisor.dev/issues/5529): Move the IPv4-only option stats out
  1987  	// of IPStats.
  1988  	// OptionTimestampReceived is the number of Timestamp options seen.
  1989  	OptionTimestampReceived *StatCounter
  1990  
  1991  	// OptionRecordRouteReceived is the number of Record Route options seen.
  1992  	OptionRecordRouteReceived *StatCounter
  1993  
  1994  	// OptionRouterAlertReceived is the number of Router Alert options seen.
  1995  	OptionRouterAlertReceived *StatCounter
  1996  
  1997  	// OptionUnknownReceived is the number of unknown IP options seen.
  1998  	OptionUnknownReceived *StatCounter
  1999  
  2000  	// Forwarding collects stats related to IP forwarding.
  2001  	Forwarding IPForwardingStats
  2002  
  2003  	// LINT.ThenChange(network/internal/ip/stats.go:MultiCounterIPStats)
  2004  }
  2005  
  2006  // ARPStats collects ARP-specific stats.
  2007  type ARPStats struct {
  2008  	// LINT.IfChange(ARPStats)
  2009  
  2010  	// PacketsReceived is the number of ARP packets received from the link layer.
  2011  	PacketsReceived *StatCounter
  2012  
  2013  	// DisabledPacketsReceived is the number of ARP packets received from the link
  2014  	// layer when the ARP layer is disabled.
  2015  	DisabledPacketsReceived *StatCounter
  2016  
  2017  	// MalformedPacketsReceived is the number of ARP packets that were dropped due
  2018  	// to being malformed.
  2019  	MalformedPacketsReceived *StatCounter
  2020  
  2021  	// RequestsReceived is the number of ARP requests received.
  2022  	RequestsReceived *StatCounter
  2023  
  2024  	// RequestsReceivedUnknownTargetAddress is the number of ARP requests that
  2025  	// were targeted to an interface different from the one it was received on.
  2026  	RequestsReceivedUnknownTargetAddress *StatCounter
  2027  
  2028  	// OutgoingRequestInterfaceHasNoLocalAddressErrors is the number of failures
  2029  	// to send an ARP request because the interface has no network address
  2030  	// assigned to it.
  2031  	OutgoingRequestInterfaceHasNoLocalAddressErrors *StatCounter
  2032  
  2033  	// OutgoingRequestBadLocalAddressErrors is the number of failures to send an
  2034  	// ARP request with a bad local address.
  2035  	OutgoingRequestBadLocalAddressErrors *StatCounter
  2036  
  2037  	// OutgoingRequestsDropped is the number of ARP requests which failed to write
  2038  	// to a link-layer endpoint.
  2039  	OutgoingRequestsDropped *StatCounter
  2040  
  2041  	// OutgoingRequestSent is the number of ARP requests successfully written to a
  2042  	// link-layer endpoint.
  2043  	OutgoingRequestsSent *StatCounter
  2044  
  2045  	// RepliesReceived is the number of ARP replies received.
  2046  	RepliesReceived *StatCounter
  2047  
  2048  	// OutgoingRepliesDropped is the number of ARP replies which failed to write
  2049  	// to a link-layer endpoint.
  2050  	OutgoingRepliesDropped *StatCounter
  2051  
  2052  	// OutgoingRepliesSent is the number of ARP replies successfully written to a
  2053  	// link-layer endpoint.
  2054  	OutgoingRepliesSent *StatCounter
  2055  
  2056  	// LINT.ThenChange(network/arp/stats.go:multiCounterARPStats)
  2057  }
  2058  
  2059  // TCPStats collects TCP-specific stats.
  2060  type TCPStats struct {
  2061  	// ActiveConnectionOpenings is the number of connections opened
  2062  	// successfully via Connect.
  2063  	ActiveConnectionOpenings *StatCounter
  2064  
  2065  	// PassiveConnectionOpenings is the number of connections opened
  2066  	// successfully via Listen.
  2067  	PassiveConnectionOpenings *StatCounter
  2068  
  2069  	// CurrentEstablished is the number of TCP connections for which the
  2070  	// current state is ESTABLISHED.
  2071  	CurrentEstablished *StatCounter
  2072  
  2073  	// CurrentConnected is the number of TCP connections that
  2074  	// are in connected state.
  2075  	CurrentConnected *StatCounter
  2076  
  2077  	// EstablishedResets is the number of times TCP connections have made
  2078  	// a direct transition to the CLOSED state from either the
  2079  	// ESTABLISHED state or the CLOSE-WAIT state.
  2080  	EstablishedResets *StatCounter
  2081  
  2082  	// EstablishedClosed is the number of times established TCP connections
  2083  	// made a transition to CLOSED state.
  2084  	EstablishedClosed *StatCounter
  2085  
  2086  	// EstablishedTimedout is the number of times an established connection
  2087  	// was reset because of keep-alive time out.
  2088  	EstablishedTimedout *StatCounter
  2089  
  2090  	// ListenOverflowSynDrop is the number of times the listen queue overflowed
  2091  	// and a SYN was dropped.
  2092  	ListenOverflowSynDrop *StatCounter
  2093  
  2094  	// ListenOverflowAckDrop is the number of times the final ACK
  2095  	// in the handshake was dropped due to overflow.
  2096  	ListenOverflowAckDrop *StatCounter
  2097  
  2098  	// ListenOverflowCookieSent is the number of times a SYN cookie was sent.
  2099  	ListenOverflowSynCookieSent *StatCounter
  2100  
  2101  	// ListenOverflowSynCookieRcvd is the number of times a valid SYN
  2102  	// cookie was received.
  2103  	ListenOverflowSynCookieRcvd *StatCounter
  2104  
  2105  	// ListenOverflowInvalidSynCookieRcvd is the number of times an invalid SYN cookie
  2106  	// was received.
  2107  	ListenOverflowInvalidSynCookieRcvd *StatCounter
  2108  
  2109  	// FailedConnectionAttempts is the number of calls to Connect or Listen
  2110  	// (active and passive openings, respectively) that end in an error.
  2111  	FailedConnectionAttempts *StatCounter
  2112  
  2113  	// ValidSegmentsReceived is the number of TCP segments received that
  2114  	// the transport layer successfully parsed.
  2115  	ValidSegmentsReceived *StatCounter
  2116  
  2117  	// InvalidSegmentsReceived is the number of TCP segments received that
  2118  	// the transport layer could not parse.
  2119  	InvalidSegmentsReceived *StatCounter
  2120  
  2121  	// SegmentsSent is the number of TCP segments sent.
  2122  	SegmentsSent *StatCounter
  2123  
  2124  	// SegmentSendErrors is the number of TCP segments failed to be sent.
  2125  	SegmentSendErrors *StatCounter
  2126  
  2127  	// ResetsSent is the number of TCP resets sent.
  2128  	ResetsSent *StatCounter
  2129  
  2130  	// ResetsReceived is the number of TCP resets received.
  2131  	ResetsReceived *StatCounter
  2132  
  2133  	// Retransmits is the number of TCP segments retransmitted.
  2134  	Retransmits *StatCounter
  2135  
  2136  	// FastRecovery is the number of times Fast Recovery was used to
  2137  	// recover from packet loss.
  2138  	FastRecovery *StatCounter
  2139  
  2140  	// SACKRecovery is the number of times SACK Recovery was used to
  2141  	// recover from packet loss.
  2142  	SACKRecovery *StatCounter
  2143  
  2144  	// TLPRecovery is the number of times recovery was accomplished by the tail
  2145  	// loss probe.
  2146  	TLPRecovery *StatCounter
  2147  
  2148  	// SlowStartRetransmits is the number of segments retransmitted in slow
  2149  	// start.
  2150  	SlowStartRetransmits *StatCounter
  2151  
  2152  	// FastRetransmit is the number of segments retransmitted in fast
  2153  	// recovery.
  2154  	FastRetransmit *StatCounter
  2155  
  2156  	// Timeouts is the number of times the RTO expired.
  2157  	Timeouts *StatCounter
  2158  
  2159  	// ChecksumErrors is the number of segments dropped due to bad checksums.
  2160  	ChecksumErrors *StatCounter
  2161  
  2162  	// FailedPortReservations is the number of times TCP failed to reserve
  2163  	// a port.
  2164  	FailedPortReservations *StatCounter
  2165  
  2166  	// SegmentsAckedWithDSACK is the number of segments acknowledged with
  2167  	// DSACK.
  2168  	SegmentsAckedWithDSACK *StatCounter
  2169  
  2170  	// SpuriousRecovery is the number of times the connection entered loss
  2171  	// recovery spuriously.
  2172  	SpuriousRecovery *StatCounter
  2173  
  2174  	// SpuriousRTORecovery is the number of spurious RTOs.
  2175  	SpuriousRTORecovery *StatCounter
  2176  
  2177  	// ForwardMaxInFlightDrop is the number of connection requests that are
  2178  	// dropped due to exceeding the maximum number of in-flight connection
  2179  	// requests.
  2180  	ForwardMaxInFlightDrop *StatCounter
  2181  }
  2182  
  2183  // UDPStats collects UDP-specific stats.
  2184  type UDPStats struct {
  2185  	// PacketsReceived is the number of UDP datagrams received via
  2186  	// HandlePacket.
  2187  	PacketsReceived *StatCounter
  2188  
  2189  	// UnknownPortErrors is the number of incoming UDP datagrams dropped
  2190  	// because they did not have a known destination port.
  2191  	UnknownPortErrors *StatCounter
  2192  
  2193  	// ReceiveBufferErrors is the number of incoming UDP datagrams dropped
  2194  	// due to the receiving buffer being in an invalid state.
  2195  	ReceiveBufferErrors *StatCounter
  2196  
  2197  	// MalformedPacketsReceived is the number of incoming UDP datagrams
  2198  	// dropped due to the UDP header being in a malformed state.
  2199  	MalformedPacketsReceived *StatCounter
  2200  
  2201  	// PacketsSent is the number of UDP datagrams sent via sendUDP.
  2202  	PacketsSent *StatCounter
  2203  
  2204  	// PacketSendErrors is the number of datagrams failed to be sent.
  2205  	PacketSendErrors *StatCounter
  2206  
  2207  	// ChecksumErrors is the number of datagrams dropped due to bad checksums.
  2208  	ChecksumErrors *StatCounter
  2209  }
  2210  
  2211  // NICNeighborStats holds metrics for the neighbor table.
  2212  type NICNeighborStats struct {
  2213  	// LINT.IfChange(NICNeighborStats)
  2214  
  2215  	// UnreachableEntryLookups counts the number of lookups performed on an
  2216  	// entry in Unreachable state.
  2217  	UnreachableEntryLookups *StatCounter
  2218  
  2219  	// DroppedConfirmationForNoninitiatedNeighbor counts the number of neighbor
  2220  	// responses that were dropped because they didn't match an entry in the
  2221  	// cache.
  2222  	DroppedConfirmationForNoninitiatedNeighbor *StatCounter
  2223  
  2224  	// DroppedInvalidLinkAddressConfirmations counts the number of neighbor
  2225  	// responses that were ignored because they had an invalid source link-layer
  2226  	// address.
  2227  	DroppedInvalidLinkAddressConfirmations *StatCounter
  2228  
  2229  	// LINT.ThenChange(stack/nic_stats.go:multiCounterNICNeighborStats)
  2230  }
  2231  
  2232  // NICPacketStats holds basic packet statistics.
  2233  type NICPacketStats struct {
  2234  	// LINT.IfChange(NICPacketStats)
  2235  
  2236  	// Packets is the number of packets counted.
  2237  	Packets *StatCounter
  2238  
  2239  	// Bytes is the number of bytes counted.
  2240  	Bytes *StatCounter
  2241  
  2242  	// LINT.ThenChange(stack/nic_stats.go:multiCounterNICPacketStats)
  2243  }
  2244  
  2245  // IntegralStatCounterMap holds a map associating integral keys with
  2246  // StatCounters.
  2247  type IntegralStatCounterMap struct {
  2248  	mu sync.RWMutex
  2249  	// +checklocks:mu
  2250  	counterMap map[uint64]*StatCounter
  2251  }
  2252  
  2253  // Keys returns all keys present in the map.
  2254  func (m *IntegralStatCounterMap) Keys() []uint64 {
  2255  	m.mu.RLock()
  2256  	defer m.mu.RUnlock()
  2257  	var keys []uint64
  2258  	for k := range m.counterMap {
  2259  		keys = append(keys, k)
  2260  	}
  2261  	return keys
  2262  }
  2263  
  2264  // Get returns the counter mapped by the provided key.
  2265  func (m *IntegralStatCounterMap) Get(key uint64) (*StatCounter, bool) {
  2266  	m.mu.RLock()
  2267  	defer m.mu.RUnlock()
  2268  	counter, ok := m.counterMap[key]
  2269  	return counter, ok
  2270  }
  2271  
  2272  // Init initializes the map.
  2273  func (m *IntegralStatCounterMap) Init() {
  2274  	m.mu.Lock()
  2275  	defer m.mu.Unlock()
  2276  	m.counterMap = make(map[uint64]*StatCounter)
  2277  }
  2278  
  2279  // Increment increments the counter associated with the provided key.
  2280  func (m *IntegralStatCounterMap) Increment(key uint64) {
  2281  	m.mu.RLock()
  2282  	counter, ok := m.counterMap[key]
  2283  	m.mu.RUnlock()
  2284  
  2285  	if !ok {
  2286  		m.mu.Lock()
  2287  		counter, ok = m.counterMap[key]
  2288  		if !ok {
  2289  			counter = new(StatCounter)
  2290  			m.counterMap[key] = counter
  2291  		}
  2292  		m.mu.Unlock()
  2293  	}
  2294  	counter.Increment()
  2295  }
  2296  
  2297  // A MultiIntegralStatCounterMap keeps track of two integral counter maps at
  2298  // once.
  2299  type MultiIntegralStatCounterMap struct {
  2300  	a *IntegralStatCounterMap
  2301  	b *IntegralStatCounterMap
  2302  }
  2303  
  2304  // Init sets the internal integral counter maps to point to a and b.
  2305  func (m *MultiIntegralStatCounterMap) Init(a, b *IntegralStatCounterMap) {
  2306  	m.a = a
  2307  	m.b = b
  2308  }
  2309  
  2310  // Increment increments the counter in each map corresponding to the
  2311  // provided key.
  2312  func (m *MultiIntegralStatCounterMap) Increment(key uint64) {
  2313  	m.a.Increment(key)
  2314  	m.b.Increment(key)
  2315  }
  2316  
  2317  // NICStats holds NIC statistics.
  2318  type NICStats struct {
  2319  	// LINT.IfChange(NICStats)
  2320  
  2321  	// UnknownL3ProtocolRcvdPacketCounts records the number of packets received
  2322  	// for each unknown or unsupported network protocol number.
  2323  	UnknownL3ProtocolRcvdPacketCounts *IntegralStatCounterMap
  2324  
  2325  	// UnknownL4ProtocolRcvdPacketCounts records the number of packets received
  2326  	// for each unknown or unsupported transport protocol number.
  2327  	UnknownL4ProtocolRcvdPacketCounts *IntegralStatCounterMap
  2328  
  2329  	// MalformedL4RcvdPackets is the number of packets received by a NIC that
  2330  	// could not be delivered to a transport endpoint because the L4 header could
  2331  	// not be parsed.
  2332  	MalformedL4RcvdPackets *StatCounter
  2333  
  2334  	// Tx contains statistics about transmitted packets.
  2335  	Tx NICPacketStats
  2336  
  2337  	// TxPacketsDroppedNoBufferSpace is the number of packets dropepd due to the
  2338  	// NIC not having enough buffer space to send the packet.
  2339  	//
  2340  	// Packets may be dropped with a no buffer space error when the device TX
  2341  	// queue is full.
  2342  	TxPacketsDroppedNoBufferSpace *StatCounter
  2343  
  2344  	// Rx contains statistics about received packets.
  2345  	Rx NICPacketStats
  2346  
  2347  	// DisabledRx contains statistics about received packets on disabled NICs.
  2348  	DisabledRx NICPacketStats
  2349  
  2350  	// Neighbor contains statistics about neighbor entries.
  2351  	Neighbor NICNeighborStats
  2352  
  2353  	// LINT.ThenChange(stack/nic_stats.go:multiCounterNICStats)
  2354  }
  2355  
  2356  // FillIn returns a copy of s with nil fields initialized to new StatCounters.
  2357  func (s NICStats) FillIn() NICStats {
  2358  	InitStatCounters(reflect.ValueOf(&s).Elem())
  2359  	return s
  2360  }
  2361  
  2362  // Stats holds statistics about the networking stack.
  2363  type Stats struct {
  2364  	// TODO(https://gvisor.dev/issues/5986): Make the DroppedPackets stat less
  2365  	// ambiguous.
  2366  
  2367  	// DroppedPackets is the number of packets dropped at the transport layer.
  2368  	DroppedPackets *StatCounter
  2369  
  2370  	// NICs is an aggregation of every NIC's statistics. These should not be
  2371  	// incremented using this field, but using the relevant NIC multicounters.
  2372  	NICs NICStats
  2373  
  2374  	// ICMP is an aggregation of every NetworkEndpoint's ICMP statistics (both v4
  2375  	// and v6). These should not be incremented using this field, but using the
  2376  	// relevant NetworkEndpoint ICMP multicounters.
  2377  	ICMP ICMPStats
  2378  
  2379  	// IGMP is an aggregation of every NetworkEndpoint's IGMP statistics. These
  2380  	// should not be incremented using this field, but using the relevant
  2381  	// NetworkEndpoint IGMP multicounters.
  2382  	IGMP IGMPStats
  2383  
  2384  	// IP is an aggregation of every NetworkEndpoint's IP statistics. These should
  2385  	// not be incremented using this field, but using the relevant NetworkEndpoint
  2386  	// IP multicounters.
  2387  	IP IPStats
  2388  
  2389  	// ARP is an aggregation of every NetworkEndpoint's ARP statistics. These
  2390  	// should not be incremented using this field, but using the relevant
  2391  	// NetworkEndpoint ARP multicounters.
  2392  	ARP ARPStats
  2393  
  2394  	// TCP holds TCP-specific stats.
  2395  	TCP TCPStats
  2396  
  2397  	// UDP holds UDP-specific stats.
  2398  	UDP UDPStats
  2399  }
  2400  
  2401  // ReceiveErrors collects packet receive errors within transport endpoint.
  2402  //
  2403  // +stateify savable
  2404  type ReceiveErrors struct {
  2405  	// ReceiveBufferOverflow is the number of received packets dropped
  2406  	// due to the receive buffer being full.
  2407  	ReceiveBufferOverflow StatCounter
  2408  
  2409  	// MalformedPacketsReceived is the number of incoming packets
  2410  	// dropped due to the packet header being in a malformed state.
  2411  	MalformedPacketsReceived StatCounter
  2412  
  2413  	// ClosedReceiver is the number of received packets dropped because
  2414  	// of receiving endpoint state being closed.
  2415  	ClosedReceiver StatCounter
  2416  
  2417  	// ChecksumErrors is the number of packets dropped due to bad checksums.
  2418  	ChecksumErrors StatCounter
  2419  }
  2420  
  2421  // SendErrors collects packet send errors within the transport layer for an
  2422  // endpoint.
  2423  //
  2424  // +stateify savable
  2425  type SendErrors struct {
  2426  	// SendToNetworkFailed is the number of packets failed to be written to
  2427  	// the network endpoint.
  2428  	SendToNetworkFailed StatCounter
  2429  
  2430  	// NoRoute is the number of times we failed to resolve IP route.
  2431  	NoRoute StatCounter
  2432  }
  2433  
  2434  // ReadErrors collects segment read errors from an endpoint read call.
  2435  //
  2436  // +stateify savable
  2437  type ReadErrors struct {
  2438  	// ReadClosed is the number of received packet drops because the endpoint
  2439  	// was shutdown for read.
  2440  	ReadClosed StatCounter
  2441  
  2442  	// InvalidEndpointState is the number of times we found the endpoint state
  2443  	// to be unexpected.
  2444  	InvalidEndpointState StatCounter
  2445  
  2446  	// NotConnected is the number of times we tried to read but found that the
  2447  	// endpoint was not connected.
  2448  	NotConnected StatCounter
  2449  }
  2450  
  2451  // WriteErrors collects packet write errors from an endpoint write call.
  2452  //
  2453  // +stateify savable
  2454  type WriteErrors struct {
  2455  	// WriteClosed is the number of packet drops because the endpoint
  2456  	// was shutdown for write.
  2457  	WriteClosed StatCounter
  2458  
  2459  	// InvalidEndpointState is the number of times we found the endpoint state
  2460  	// to be unexpected.
  2461  	InvalidEndpointState StatCounter
  2462  
  2463  	// InvalidArgs is the number of times invalid input arguments were
  2464  	// provided for endpoint Write call.
  2465  	InvalidArgs StatCounter
  2466  }
  2467  
  2468  // TransportEndpointStats collects statistics about the endpoint.
  2469  //
  2470  // +stateify savable
  2471  type TransportEndpointStats struct {
  2472  	// PacketsReceived is the number of successful packet receives.
  2473  	PacketsReceived StatCounter
  2474  
  2475  	// PacketsSent is the number of successful packet sends.
  2476  	PacketsSent StatCounter
  2477  
  2478  	// ReceiveErrors collects packet receive errors within transport layer.
  2479  	ReceiveErrors ReceiveErrors
  2480  
  2481  	// ReadErrors collects packet read errors from an endpoint read call.
  2482  	ReadErrors ReadErrors
  2483  
  2484  	// SendErrors collects packet send errors within the transport layer.
  2485  	SendErrors SendErrors
  2486  
  2487  	// WriteErrors collects packet write errors from an endpoint write call.
  2488  	WriteErrors WriteErrors
  2489  }
  2490  
  2491  // IsEndpointStats is an empty method to implement the tcpip.EndpointStats
  2492  // marker interface.
  2493  func (*TransportEndpointStats) IsEndpointStats() {}
  2494  
  2495  // InitStatCounters initializes v's fields with nil StatCounter fields to new
  2496  // StatCounters.
  2497  func InitStatCounters(v reflect.Value) {
  2498  	for i := 0; i < v.NumField(); i++ {
  2499  		v := v.Field(i)
  2500  		if s, ok := v.Addr().Interface().(**StatCounter); ok {
  2501  			if *s == nil {
  2502  				*s = new(StatCounter)
  2503  			}
  2504  		} else if s, ok := v.Addr().Interface().(**IntegralStatCounterMap); ok {
  2505  			if *s == nil {
  2506  				*s = new(IntegralStatCounterMap)
  2507  				(*s).Init()
  2508  			}
  2509  		} else {
  2510  			InitStatCounters(v)
  2511  		}
  2512  	}
  2513  }
  2514  
  2515  // FillIn returns a copy of s with nil fields initialized to new StatCounters.
  2516  func (s Stats) FillIn() Stats {
  2517  	InitStatCounters(reflect.ValueOf(&s).Elem())
  2518  	return s
  2519  }
  2520  
  2521  // Clone clones a copy of the TransportEndpointStats into dst by atomically
  2522  // reading each field.
  2523  func (src *TransportEndpointStats) Clone(dst *TransportEndpointStats) {
  2524  	clone(reflect.ValueOf(dst).Elem(), reflect.ValueOf(src).Elem())
  2525  }
  2526  
  2527  func clone(dst reflect.Value, src reflect.Value) {
  2528  	for i := 0; i < dst.NumField(); i++ {
  2529  		d := dst.Field(i)
  2530  		s := src.Field(i)
  2531  		if c, ok := s.Addr().Interface().(*StatCounter); ok {
  2532  			d.Addr().Interface().(*StatCounter).IncrementBy(c.Value())
  2533  		} else {
  2534  			clone(d, s)
  2535  		}
  2536  	}
  2537  }
  2538  
  2539  // String implements the fmt.Stringer interface.
  2540  func (a Address) String() string {
  2541  	switch l := a.Len(); l {
  2542  	case 4:
  2543  		return fmt.Sprintf("%d.%d.%d.%d", int(a.addr[0]), int(a.addr[1]), int(a.addr[2]), int(a.addr[3]))
  2544  	case 16:
  2545  		// Find the longest subsequence of hexadecimal zeros.
  2546  		start, end := -1, -1
  2547  		for i := 0; i < a.Len(); i += 2 {
  2548  			j := i
  2549  			for j < a.Len() && a.addr[j] == 0 && a.addr[j+1] == 0 {
  2550  				j += 2
  2551  			}
  2552  			if j > i+2 && j-i > end-start {
  2553  				start, end = i, j
  2554  			}
  2555  		}
  2556  
  2557  		var b strings.Builder
  2558  		for i := 0; i < a.Len(); i += 2 {
  2559  			if i == start {
  2560  				b.WriteString("::")
  2561  				i = end
  2562  				if end >= a.Len() {
  2563  					break
  2564  				}
  2565  			} else if i > 0 {
  2566  				b.WriteByte(':')
  2567  			}
  2568  			v := uint16(a.addr[i+0])<<8 | uint16(a.addr[i+1])
  2569  			if v == 0 {
  2570  				b.WriteByte('0')
  2571  			} else {
  2572  				const digits = "0123456789abcdef"
  2573  				for i := uint(3); i < 4; i-- {
  2574  					if v := v >> (i * 4); v != 0 {
  2575  						b.WriteByte(digits[v&0xf])
  2576  					}
  2577  				}
  2578  			}
  2579  		}
  2580  		return b.String()
  2581  	default:
  2582  		return fmt.Sprintf("%x", a.addr[:l])
  2583  	}
  2584  }
  2585  
  2586  // To4 converts the IPv4 address to a 4-byte representation.
  2587  // If the address is not an IPv4 address, To4 returns the empty Address.
  2588  func (a Address) To4() Address {
  2589  	const (
  2590  		ipv4len = 4
  2591  		ipv6len = 16
  2592  	)
  2593  	if a.Len() == ipv4len {
  2594  		return a
  2595  	}
  2596  	if a.Len() == ipv6len &&
  2597  		isZeros(a.addr[:10]) &&
  2598  		a.addr[10] == 0xff &&
  2599  		a.addr[11] == 0xff {
  2600  		return AddrFrom4Slice(a.addr[12:16])
  2601  	}
  2602  	return Address{}
  2603  }
  2604  
  2605  // isZeros reports whether addr is all zeros.
  2606  func isZeros(addr []byte) bool {
  2607  	for _, b := range addr {
  2608  		if b != 0 {
  2609  			return false
  2610  		}
  2611  	}
  2612  	return true
  2613  }
  2614  
  2615  // LinkAddress is a byte slice cast as a string that represents a link address.
  2616  // It is typically a 6-byte MAC address.
  2617  type LinkAddress string
  2618  
  2619  // String implements the fmt.Stringer interface.
  2620  func (a LinkAddress) String() string {
  2621  	switch len(a) {
  2622  	case 6:
  2623  		return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", a[0], a[1], a[2], a[3], a[4], a[5])
  2624  	default:
  2625  		return fmt.Sprintf("%x", []byte(a))
  2626  	}
  2627  }
  2628  
  2629  // ParseMACAddress parses an IEEE 802 address.
  2630  //
  2631  // It must be in the format aa:bb:cc:dd:ee:ff or aa-bb-cc-dd-ee-ff.
  2632  func ParseMACAddress(s string) (LinkAddress, error) {
  2633  	parts := strings.FieldsFunc(s, func(c rune) bool {
  2634  		return c == ':' || c == '-'
  2635  	})
  2636  	if len(parts) != 6 {
  2637  		return "", fmt.Errorf("inconsistent parts: %s", s)
  2638  	}
  2639  	addr := make([]byte, 0, len(parts))
  2640  	for _, part := range parts {
  2641  		u, err := strconv.ParseUint(part, 16, 8)
  2642  		if err != nil {
  2643  			return "", fmt.Errorf("invalid hex digits: %s", s)
  2644  		}
  2645  		addr = append(addr, byte(u))
  2646  	}
  2647  	return LinkAddress(addr), nil
  2648  }
  2649  
  2650  // AddressWithPrefix is an address with its subnet prefix length.
  2651  //
  2652  // +stateify savable
  2653  type AddressWithPrefix struct {
  2654  	// Address is a network address.
  2655  	Address Address
  2656  
  2657  	// PrefixLen is the subnet prefix length.
  2658  	PrefixLen int
  2659  }
  2660  
  2661  // String implements the fmt.Stringer interface.
  2662  func (a AddressWithPrefix) String() string {
  2663  	return fmt.Sprintf("%s/%d", a.Address, a.PrefixLen)
  2664  }
  2665  
  2666  // Subnet converts the address and prefix into a Subnet value and returns it.
  2667  func (a AddressWithPrefix) Subnet() Subnet {
  2668  	addrLen := a.Address.length
  2669  	if a.PrefixLen <= 0 {
  2670  		return Subnet{
  2671  			address: Address{length: addrLen},
  2672  			mask:    AddressMask{length: addrLen},
  2673  		}
  2674  	}
  2675  	if a.PrefixLen >= addrLen*8 {
  2676  		sub := Subnet{
  2677  			address: a.Address,
  2678  			mask:    AddressMask{length: addrLen},
  2679  		}
  2680  		for i := 0; i < addrLen; i++ {
  2681  			sub.mask.mask[i] = 0xff
  2682  		}
  2683  		return sub
  2684  	}
  2685  
  2686  	sa := Address{length: addrLen}
  2687  	sm := AddressMask{length: addrLen}
  2688  	n := uint(a.PrefixLen)
  2689  	for i := 0; i < addrLen; i++ {
  2690  		if n >= 8 {
  2691  			sa.addr[i] = a.Address.addr[i]
  2692  			sm.mask[i] = 0xff
  2693  			n -= 8
  2694  			continue
  2695  		}
  2696  		sm.mask[i] = ^byte(0xff >> n)
  2697  		sa.addr[i] = a.Address.addr[i] & sm.mask[i]
  2698  		n = 0
  2699  	}
  2700  
  2701  	// For extra caution, call NewSubnet rather than directly creating the Subnet
  2702  	// value. If that fails it indicates a serious bug in this code, so panic is
  2703  	// in order.
  2704  	s, err := NewSubnet(sa, sm)
  2705  	if err != nil {
  2706  		panic("invalid subnet: " + err.Error())
  2707  	}
  2708  	return s
  2709  }
  2710  
  2711  // ProtocolAddress is an address and the network protocol it is associated
  2712  // with.
  2713  type ProtocolAddress struct {
  2714  	// Protocol is the protocol of the address.
  2715  	Protocol NetworkProtocolNumber
  2716  
  2717  	// AddressWithPrefix is a network address with its subnet prefix length.
  2718  	AddressWithPrefix AddressWithPrefix
  2719  }
  2720  
  2721  var (
  2722  	// danglingEndpointsMu protects access to danglingEndpoints.
  2723  	danglingEndpointsMu sync.Mutex
  2724  
  2725  	// danglingEndpoints tracks all dangling endpoints no longer owned by the app.
  2726  	danglingEndpoints = make(map[Endpoint]struct{})
  2727  )
  2728  
  2729  // GetDanglingEndpoints returns all dangling endpoints.
  2730  func GetDanglingEndpoints() []Endpoint {
  2731  	danglingEndpointsMu.Lock()
  2732  	es := make([]Endpoint, 0, len(danglingEndpoints))
  2733  	for e := range danglingEndpoints {
  2734  		es = append(es, e)
  2735  	}
  2736  	danglingEndpointsMu.Unlock()
  2737  	return es
  2738  }
  2739  
  2740  // ReleaseDanglingEndpoints clears out all all reference counted objects held by
  2741  // dangling endpoints.
  2742  func ReleaseDanglingEndpoints() {
  2743  	// Get the dangling endpoints first to avoid locking around Release(), which
  2744  	// can cause a lock inversion with endpoint.mu and danglingEndpointsMu.
  2745  	// Calling Release on a dangling endpoint that has been deleted is a noop.
  2746  	eps := GetDanglingEndpoints()
  2747  	for _, ep := range eps {
  2748  		ep.Abort()
  2749  	}
  2750  }
  2751  
  2752  // AddDanglingEndpoint adds a dangling endpoint.
  2753  func AddDanglingEndpoint(e Endpoint) {
  2754  	danglingEndpointsMu.Lock()
  2755  	danglingEndpoints[e] = struct{}{}
  2756  	danglingEndpointsMu.Unlock()
  2757  }
  2758  
  2759  // DeleteDanglingEndpoint removes a dangling endpoint.
  2760  func DeleteDanglingEndpoint(e Endpoint) {
  2761  	danglingEndpointsMu.Lock()
  2762  	delete(danglingEndpoints, e)
  2763  	danglingEndpointsMu.Unlock()
  2764  }
  2765  
  2766  // AsyncLoading is the global barrier for asynchronous endpoint loading
  2767  // activities.
  2768  var AsyncLoading sync.WaitGroup