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