github.com/flowerwrong/netstack@v0.0.0-20191009141956-e5848263af28/tcpip/stack/registration.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package stack 16 17 import ( 18 "github.com/FlowerWrong/netstack/sleep" 19 "github.com/FlowerWrong/netstack/tcpip" 20 "github.com/FlowerWrong/netstack/tcpip/buffer" 21 "github.com/FlowerWrong/netstack/waiter" 22 ) 23 24 // NetworkEndpointID is the identifier of a network layer protocol endpoint. 25 // Currently the local address is sufficient because all supported protocols 26 // (i.e., IPv4 and IPv6) have different sizes for their addresses. 27 type NetworkEndpointID struct { 28 LocalAddress tcpip.Address 29 } 30 31 // TransportEndpointID is the identifier of a transport layer protocol endpoint. 32 // 33 // +stateify savable 34 type TransportEndpointID struct { 35 // LocalPort is the local port associated with the endpoint. 36 LocalPort uint16 37 38 // LocalAddress is the local [network layer] address associated with 39 // the endpoint. 40 LocalAddress tcpip.Address 41 42 // RemotePort is the remote port associated with the endpoint. 43 RemotePort uint16 44 45 // RemoteAddress it the remote [network layer] address associated with 46 // the endpoint. 47 RemoteAddress tcpip.Address 48 } 49 50 // ControlType is the type of network control message. 51 type ControlType int 52 53 // The following are the allowed values for ControlType values. 54 const ( 55 ControlPacketTooBig ControlType = iota 56 ControlPortUnreachable 57 ControlUnknown 58 ) 59 60 // TransportEndpoint is the interface that needs to be implemented by transport 61 // protocol (e.g., tcp, udp) endpoints that can handle packets. 62 type TransportEndpoint interface { 63 // HandlePacket is called by the stack when new packets arrive to 64 // this transport endpoint. 65 HandlePacket(r *Route, id TransportEndpointID, vv buffer.VectorisedView) 66 67 // HandleControlPacket is called by the stack when new control (e.g., 68 // ICMP) packets arrive to this transport endpoint. 69 HandleControlPacket(id TransportEndpointID, typ ControlType, extra uint32, vv buffer.VectorisedView) 70 } 71 72 // RawTransportEndpoint is the interface that needs to be implemented by raw 73 // transport protocol endpoints. RawTransportEndpoints receive the entire 74 // packet - including the link, network, and transport headers - as delivered 75 // to netstack. 76 type RawTransportEndpoint interface { 77 // HandlePacket is called by the stack when new packets arrive to 78 // this transport endpoint. The packet contains all data from the link 79 // layer up. 80 HandlePacket(r *Route, netHeader buffer.View, packet buffer.VectorisedView) 81 } 82 83 // TransportProtocol is the interface that needs to be implemented by transport 84 // protocols (e.g., tcp, udp) that want to be part of the networking stack. 85 type TransportProtocol interface { 86 // Number returns the transport protocol number. 87 Number() tcpip.TransportProtocolNumber 88 89 // NewEndpoint creates a new endpoint of the transport protocol. 90 NewEndpoint(stack *Stack, netProto tcpip.NetworkProtocolNumber, waitQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) 91 92 // NewRawEndpoint creates a new raw endpoint of the transport protocol. 93 NewRawEndpoint(stack *Stack, netProto tcpip.NetworkProtocolNumber, waitQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) 94 95 // MinimumPacketSize returns the minimum valid packet size of this 96 // transport protocol. The stack automatically drops any packets smaller 97 // than this targeted at this protocol. 98 MinimumPacketSize() int 99 100 // ParsePorts returns the source and destination ports stored in a 101 // packet of this protocol. 102 ParsePorts(v buffer.View) (src, dst uint16, err *tcpip.Error) 103 104 // HandleUnknownDestinationPacket handles packets targeted at this 105 // protocol but that don't match any existing endpoint. For example, 106 // it is targeted at a port that have no listeners. 107 // 108 // The return value indicates whether the packet was well-formed (for 109 // stats purposes only). 110 HandleUnknownDestinationPacket(r *Route, id TransportEndpointID, netHeader buffer.View, vv buffer.VectorisedView) bool 111 112 // SetOption allows enabling/disabling protocol specific features. 113 // SetOption returns an error if the option is not supported or the 114 // provided option value is invalid. 115 SetOption(option interface{}) *tcpip.Error 116 117 // Option allows retrieving protocol specific option values. 118 // Option returns an error if the option is not supported or the 119 // provided option value is invalid. 120 Option(option interface{}) *tcpip.Error 121 } 122 123 // TransportDispatcher contains the methods used by the network stack to deliver 124 // packets to the appropriate transport endpoint after it has been handled by 125 // the network layer. 126 type TransportDispatcher interface { 127 // DeliverTransportPacket delivers packets to the appropriate 128 // transport protocol endpoint. It also returns the network layer 129 // header for the enpoint to inspect or pass up the stack. 130 DeliverTransportPacket(r *Route, protocol tcpip.TransportProtocolNumber, netHeader buffer.View, vv buffer.VectorisedView) 131 132 // DeliverTransportControlPacket delivers control packets to the 133 // appropriate transport protocol endpoint. 134 DeliverTransportControlPacket(local, remote tcpip.Address, net tcpip.NetworkProtocolNumber, trans tcpip.TransportProtocolNumber, typ ControlType, extra uint32, vv buffer.VectorisedView) 135 } 136 137 // PacketLooping specifies where an outbound packet should be sent. 138 type PacketLooping byte 139 140 const ( 141 // PacketOut indicates that the packet should be passed to the link 142 // endpoint. 143 PacketOut PacketLooping = 1 << iota 144 145 // PacketLoop indicates that the packet should be handled locally. 146 PacketLoop 147 ) 148 149 // NetworkEndpoint is the interface that needs to be implemented by endpoints 150 // of network layer protocols (e.g., ipv4, ipv6). 151 type NetworkEndpoint interface { 152 // DefaultTTL is the default time-to-live value (or hop limit, in ipv6) 153 // for this endpoint. 154 DefaultTTL() uint8 155 156 // MTU is the maximum transmission unit for this endpoint. This is 157 // generally calculated as the MTU of the underlying data link endpoint 158 // minus the network endpoint max header length. 159 MTU() uint32 160 161 // Capabilities returns the set of capabilities supported by the 162 // underlying link-layer endpoint. 163 Capabilities() LinkEndpointCapabilities 164 165 // MaxHeaderLength returns the maximum size the network (and lower 166 // level layers combined) headers can have. Higher levels use this 167 // information to reserve space in the front of the packets they're 168 // building. 169 MaxHeaderLength() uint16 170 171 // WritePacket writes a packet to the given destination address and 172 // protocol. 173 WritePacket(r *Route, gso *GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.TransportProtocolNumber, ttl uint8, loop PacketLooping) *tcpip.Error 174 175 // WriteHeaderIncludedPacket writes a packet that includes a network 176 // header to the given destination address. 177 WriteHeaderIncludedPacket(r *Route, payload buffer.VectorisedView, loop PacketLooping) *tcpip.Error 178 179 // ID returns the network protocol endpoint ID. 180 ID() *NetworkEndpointID 181 182 // PrefixLen returns the network endpoint's subnet prefix length in bits. 183 PrefixLen() int 184 185 // NICID returns the id of the NIC this endpoint belongs to. 186 NICID() tcpip.NICID 187 188 // HandlePacket is called by the link layer when new packets arrive to 189 // this network endpoint. 190 HandlePacket(r *Route, vv buffer.VectorisedView) 191 192 // Close is called when the endpoint is reomved from a stack. 193 Close() 194 } 195 196 // NetworkProtocol is the interface that needs to be implemented by network 197 // protocols (e.g., ipv4, ipv6) that want to be part of the networking stack. 198 type NetworkProtocol interface { 199 // Number returns the network protocol number. 200 Number() tcpip.NetworkProtocolNumber 201 202 // MinimumPacketSize returns the minimum valid packet size of this 203 // network protocol. The stack automatically drops any packets smaller 204 // than this targeted at this protocol. 205 MinimumPacketSize() int 206 207 // DefaultPrefixLen returns the protocol's default prefix length. 208 DefaultPrefixLen() int 209 210 // ParsePorts returns the source and destination addresses stored in a 211 // packet of this protocol. 212 ParseAddresses(v buffer.View) (src, dst tcpip.Address) 213 214 // NewEndpoint creates a new endpoint of this protocol. 215 NewEndpoint(nicid tcpip.NICID, addrWithPrefix tcpip.AddressWithPrefix, linkAddrCache LinkAddressCache, dispatcher TransportDispatcher, sender LinkEndpoint) (NetworkEndpoint, *tcpip.Error) 216 217 // SetOption allows enabling/disabling protocol specific features. 218 // SetOption returns an error if the option is not supported or the 219 // provided option value is invalid. 220 SetOption(option interface{}) *tcpip.Error 221 222 // Option allows retrieving protocol specific option values. 223 // Option returns an error if the option is not supported or the 224 // provided option value is invalid. 225 Option(option interface{}) *tcpip.Error 226 } 227 228 // NetworkDispatcher contains the methods used by the network stack to deliver 229 // packets to the appropriate network endpoint after it has been handled by 230 // the data link layer. 231 type NetworkDispatcher interface { 232 // DeliverNetworkPacket finds the appropriate network protocol 233 // endpoint and hands the packet over for further processing. 234 DeliverNetworkPacket(linkEP LinkEndpoint, remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) 235 } 236 237 // LinkEndpointCapabilities is the type associated with the capabilities 238 // supported by a link-layer endpoint. It is a set of bitfields. 239 type LinkEndpointCapabilities uint 240 241 // The following are the supported link endpoint capabilities. 242 const ( 243 CapabilityNone LinkEndpointCapabilities = 0 244 // CapabilityTXChecksumOffload indicates that the link endpoint supports 245 // checksum computation for outgoing packets and the stack can skip 246 // computing checksums when sending packets. 247 CapabilityTXChecksumOffload LinkEndpointCapabilities = 1 << iota 248 // CapabilityRXChecksumOffload indicates that the link endpoint supports 249 // checksum verification on received packets and that it's safe for the 250 // stack to skip checksum verification. 251 CapabilityRXChecksumOffload 252 CapabilityResolutionRequired 253 CapabilitySaveRestore 254 CapabilityDisconnectOk 255 CapabilityLoopback 256 CapabilityGSO 257 ) 258 259 // LinkEndpoint is the interface implemented by data link layer protocols (e.g., 260 // ethernet, loopback, raw) and used by network layer protocols to send packets 261 // out through the implementer's data link endpoint. 262 type LinkEndpoint interface { 263 // MTU is the maximum transmission unit for this endpoint. This is 264 // usually dictated by the backing physical network; when such a 265 // physical network doesn't exist, the limit is generally 64k, which 266 // includes the maximum size of an IP packet. 267 MTU() uint32 268 269 // Capabilities returns the set of capabilities supported by the 270 // endpoint. 271 Capabilities() LinkEndpointCapabilities 272 273 // MaxHeaderLength returns the maximum size the data link (and 274 // lower level layers combined) headers can have. Higher levels use this 275 // information to reserve space in the front of the packets they're 276 // building. 277 MaxHeaderLength() uint16 278 279 // LinkAddress returns the link address (typically a MAC) of the 280 // link endpoint. 281 LinkAddress() tcpip.LinkAddress 282 283 // WritePacket writes a packet with the given protocol through the given 284 // route. 285 // 286 // To participate in transparent bridging, a LinkEndpoint implementation 287 // should call eth.Encode with header.EthernetFields.SrcAddr set to 288 // r.LocalLinkAddress if it is provided. 289 WritePacket(r *Route, gso *GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) *tcpip.Error 290 291 // Attach attaches the data link layer endpoint to the network-layer 292 // dispatcher of the stack. 293 Attach(dispatcher NetworkDispatcher) 294 295 // IsAttached returns whether a NetworkDispatcher is attached to the 296 // endpoint. 297 IsAttached() bool 298 299 // Wait waits for any worker goroutines owned by the endpoint to stop. 300 // 301 // For now, requesting that an endpoint's worker goroutine(s) stop is 302 // implementation specific. 303 // 304 // Wait will not block if the endpoint hasn't started any goroutines 305 // yet, even if it might later. 306 Wait() 307 } 308 309 // InjectableLinkEndpoint is a LinkEndpoint where inbound packets are 310 // delivered via the Inject method. 311 type InjectableLinkEndpoint interface { 312 LinkEndpoint 313 314 // Inject injects an inbound packet. 315 Inject(protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) 316 317 // WriteRawPacket writes a fully formed outbound packet directly to the link. 318 // 319 // dest is used by endpoints with multiple raw destinations. 320 WriteRawPacket(dest tcpip.Address, packet []byte) *tcpip.Error 321 } 322 323 // A LinkAddressResolver is an extension to a NetworkProtocol that 324 // can resolve link addresses. 325 type LinkAddressResolver interface { 326 // LinkAddressRequest sends a request for the LinkAddress of addr. 327 // The request is sent on linkEP with localAddr as the source. 328 // 329 // A valid response will cause the discovery protocol's network 330 // endpoint to call AddLinkAddress. 331 LinkAddressRequest(addr, localAddr tcpip.Address, linkEP LinkEndpoint) *tcpip.Error 332 333 // ResolveStaticAddress attempts to resolve address without sending 334 // requests. It either resolves the name immediately or returns the 335 // empty LinkAddress. 336 // 337 // It can be used to resolve broadcast addresses for example. 338 ResolveStaticAddress(addr tcpip.Address) (tcpip.LinkAddress, bool) 339 340 // LinkAddressProtocol returns the network protocol of the 341 // addresses this this resolver can resolve. 342 LinkAddressProtocol() tcpip.NetworkProtocolNumber 343 } 344 345 // A LinkAddressCache caches link addresses. 346 type LinkAddressCache interface { 347 // CheckLocalAddress determines if the given local address exists, and if it 348 // does not exist. 349 CheckLocalAddress(nicid tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) tcpip.NICID 350 351 // AddLinkAddress adds a link address to the cache. 352 AddLinkAddress(nicid tcpip.NICID, addr tcpip.Address, linkAddr tcpip.LinkAddress) 353 354 // GetLinkAddress looks up the cache to translate address to link address (e.g. IP -> MAC). 355 // If the LinkEndpoint requests address resolution and there is a LinkAddressResolver 356 // registered with the network protocol, the cache attempts to resolve the address 357 // and returns ErrWouldBlock. Waker is notified when address resolution is 358 // complete (success or not). 359 // 360 // If address resolution is required, ErrNoLinkAddress and a notification channel is 361 // returned for the top level caller to block. Channel is closed once address resolution 362 // is complete (success or not). 363 GetLinkAddress(nicid tcpip.NICID, addr, localAddr tcpip.Address, protocol tcpip.NetworkProtocolNumber, w *sleep.Waker) (tcpip.LinkAddress, <-chan struct{}, *tcpip.Error) 364 365 // RemoveWaker removes a waker that has been added in GetLinkAddress(). 366 RemoveWaker(nicid tcpip.NICID, addr tcpip.Address, waker *sleep.Waker) 367 } 368 369 // UnassociatedEndpointFactory produces endpoints for writing packets not 370 // associated with a particular transport protocol. Such endpoints can be used 371 // to write arbitrary packets that include the IP header. 372 type UnassociatedEndpointFactory interface { 373 NewUnassociatedRawEndpoint(stack *Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) 374 } 375 376 // GSOType is the type of GSO segments. 377 // 378 // +stateify savable 379 type GSOType int 380 381 // Types of gso segments. 382 const ( 383 GSONone GSOType = iota 384 GSOTCPv4 385 GSOTCPv6 386 ) 387 388 // GSO contains generic segmentation offload properties. 389 // 390 // +stateify savable 391 type GSO struct { 392 // Type is one of GSONone, GSOTCPv4, etc. 393 Type GSOType 394 // NeedsCsum is set if the checksum offload is enabled. 395 NeedsCsum bool 396 // CsumOffset is offset after that to place checksum. 397 CsumOffset uint16 398 399 // Mss is maximum segment size. 400 MSS uint16 401 // L3Len is L3 (IP) header length. 402 L3HdrLen uint16 403 404 // MaxSize is maximum GSO packet size. 405 MaxSize uint32 406 } 407 408 // GSOEndpoint provides access to GSO properties. 409 type GSOEndpoint interface { 410 // GSOMaxSize returns the maximum GSO packet size. 411 GSOMaxSize() uint32 412 }