github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/inet/inet.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 inet defines semantics for IP stacks.
    16  package inet
    17  
    18  import (
    19  	"github.com/SagerNet/gvisor/pkg/tcpip"
    20  	"github.com/SagerNet/gvisor/pkg/tcpip/stack"
    21  )
    22  
    23  // Stack represents a TCP/IP stack.
    24  type Stack interface {
    25  	// Interfaces returns all network interfaces as a mapping from interface
    26  	// indexes to interface properties. Interface indices are strictly positive
    27  	// integers.
    28  	Interfaces() map[int32]Interface
    29  
    30  	// InterfaceAddrs returns all network interface addresses as a mapping from
    31  	// interface indexes to a slice of associated interface address properties.
    32  	InterfaceAddrs() map[int32][]InterfaceAddr
    33  
    34  	// AddInterfaceAddr adds an address to the network interface identified by
    35  	// idx.
    36  	AddInterfaceAddr(idx int32, addr InterfaceAddr) error
    37  
    38  	// RemoveInterfaceAddr removes an address from the network interface
    39  	// identified by idx.
    40  	RemoveInterfaceAddr(idx int32, addr InterfaceAddr) error
    41  
    42  	// SupportsIPv6 returns true if the stack supports IPv6 connectivity.
    43  	SupportsIPv6() bool
    44  
    45  	// TCPReceiveBufferSize returns TCP receive buffer size settings.
    46  	TCPReceiveBufferSize() (TCPBufferSize, error)
    47  
    48  	// SetTCPReceiveBufferSize attempts to change TCP receive buffer size
    49  	// settings.
    50  	SetTCPReceiveBufferSize(size TCPBufferSize) error
    51  
    52  	// TCPSendBufferSize returns TCP send buffer size settings.
    53  	TCPSendBufferSize() (TCPBufferSize, error)
    54  
    55  	// SetTCPSendBufferSize attempts to change TCP send buffer size settings.
    56  	SetTCPSendBufferSize(size TCPBufferSize) error
    57  
    58  	// TCPSACKEnabled returns true if RFC 2018 TCP Selective Acknowledgements
    59  	// are enabled.
    60  	TCPSACKEnabled() (bool, error)
    61  
    62  	// SetTCPSACKEnabled attempts to change TCP selective acknowledgement
    63  	// settings.
    64  	SetTCPSACKEnabled(enabled bool) error
    65  
    66  	// TCPRecovery returns the TCP loss detection algorithm.
    67  	TCPRecovery() (TCPLossRecovery, error)
    68  
    69  	// SetTCPRecovery attempts to change TCP loss detection algorithm.
    70  	SetTCPRecovery(recovery TCPLossRecovery) error
    71  
    72  	// Statistics reports stack statistics.
    73  	Statistics(stat interface{}, arg string) error
    74  
    75  	// RouteTable returns the network stack's route table.
    76  	RouteTable() []Route
    77  
    78  	// Resume restarts the network stack after restore.
    79  	Resume()
    80  
    81  	// RegisteredEndpoints returns all endpoints which are currently registered.
    82  	RegisteredEndpoints() []stack.TransportEndpoint
    83  
    84  	// CleanupEndpoints returns endpoints currently in the cleanup state.
    85  	CleanupEndpoints() []stack.TransportEndpoint
    86  
    87  	// RestoreCleanupEndpoints adds endpoints to cleanup tracking. This is useful
    88  	// for restoring a stack after a save.
    89  	RestoreCleanupEndpoints([]stack.TransportEndpoint)
    90  
    91  	// SetForwarding enables or disables packet forwarding between NICs.
    92  	SetForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) error
    93  
    94  	// PortRange returns the UDP and TCP inclusive range of ephemeral ports
    95  	// used in both IPv4 and IPv6.
    96  	PortRange() (uint16, uint16)
    97  
    98  	// SetPortRange sets the UDP and TCP IPv4 and IPv6 ephemeral port range
    99  	// (inclusive).
   100  	SetPortRange(start uint16, end uint16) error
   101  }
   102  
   103  // Interface contains information about a network interface.
   104  type Interface struct {
   105  	// DeviceType is the device type, a Linux ARPHRD_* constant.
   106  	DeviceType uint16
   107  
   108  	// Flags is the device flags; see netdevice(7), under "Ioctls",
   109  	// "SIOCGIFFLAGS, SIOCSIFFLAGS".
   110  	Flags uint32
   111  
   112  	// Name is the device name.
   113  	Name string
   114  
   115  	// Addr is the hardware device address.
   116  	Addr []byte
   117  
   118  	// MTU is the maximum transmission unit.
   119  	MTU uint32
   120  }
   121  
   122  // InterfaceAddr contains information about a network interface address.
   123  type InterfaceAddr struct {
   124  	// Family is the address family, a Linux AF_* constant.
   125  	Family uint8
   126  
   127  	// PrefixLen is the address prefix length.
   128  	PrefixLen uint8
   129  
   130  	// Flags is the address flags.
   131  	Flags uint8
   132  
   133  	// Addr is the actual address.
   134  	Addr []byte
   135  }
   136  
   137  // TCPBufferSize contains settings controlling TCP buffer sizing.
   138  //
   139  // +stateify savable
   140  type TCPBufferSize struct {
   141  	// Min is the minimum size.
   142  	Min int
   143  
   144  	// Default is the default size.
   145  	Default int
   146  
   147  	// Max is the maximum size.
   148  	Max int
   149  }
   150  
   151  // StatDev describes one line of /proc/net/dev, i.e., stats for one network
   152  // interface.
   153  type StatDev [16]uint64
   154  
   155  // Route contains information about a network route.
   156  type Route struct {
   157  	// Family is the address family, a Linux AF_* constant.
   158  	Family uint8
   159  
   160  	// DstLen is the length of the destination address.
   161  	DstLen uint8
   162  
   163  	// SrcLen is the length of the source address.
   164  	SrcLen uint8
   165  
   166  	// TOS is the Type of Service filter.
   167  	TOS uint8
   168  
   169  	// Table is the routing table ID.
   170  	Table uint8
   171  
   172  	// Protocol is the route origin, a Linux RTPROT_* constant.
   173  	Protocol uint8
   174  
   175  	// Scope is the distance to destination, a Linux RT_SCOPE_* constant.
   176  	Scope uint8
   177  
   178  	// Type is the route origin, a Linux RTN_* constant.
   179  	Type uint8
   180  
   181  	// Flags are route flags. See rtnetlink(7) under "rtm_flags".
   182  	Flags uint32
   183  
   184  	// DstAddr is the route destination address (RTA_DST).
   185  	DstAddr []byte
   186  
   187  	// SrcAddr is the route source address (RTA_SRC).
   188  	SrcAddr []byte
   189  
   190  	// OutputInterface is the output interface index (RTA_OIF).
   191  	OutputInterface int32
   192  
   193  	// GatewayAddr is the route gateway address (RTA_GATEWAY).
   194  	GatewayAddr []byte
   195  }
   196  
   197  // Below SNMP metrics are from Linux/usr/include/linux/snmp.h.
   198  
   199  // StatSNMPIP describes Ip line of /proc/net/snmp.
   200  type StatSNMPIP [19]uint64
   201  
   202  // StatSNMPICMP describes Icmp line of /proc/net/snmp.
   203  type StatSNMPICMP [27]uint64
   204  
   205  // StatSNMPICMPMSG describes IcmpMsg line of /proc/net/snmp.
   206  type StatSNMPICMPMSG [512]uint64
   207  
   208  // StatSNMPTCP describes Tcp line of /proc/net/snmp.
   209  type StatSNMPTCP [15]uint64
   210  
   211  // StatSNMPUDP describes Udp line of /proc/net/snmp.
   212  type StatSNMPUDP [8]uint64
   213  
   214  // StatSNMPUDPLite describes UdpLite line of /proc/net/snmp.
   215  type StatSNMPUDPLite [8]uint64
   216  
   217  // TCPLossRecovery indicates TCP loss detection and recovery methods to use.
   218  type TCPLossRecovery int32
   219  
   220  // Loss recovery constants from include/net/tcp.h which are used to set
   221  // /proc/sys/net/ipv4/tcp_recovery.
   222  const (
   223  	TCP_RACK_LOSS_DETECTION TCPLossRecovery = 1 << iota
   224  	TCP_RACK_STATIC_REO_WND
   225  	TCP_RACK_NO_DUPTHRESH
   226  )