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