github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/tcpip/header/interfaces.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 header
    16  
    17  import (
    18  	"github.com/metacubex/gvisor/pkg/tcpip"
    19  )
    20  
    21  const (
    22  	// MaxIPPacketSize is the maximum supported IP packet size, excluding
    23  	// jumbograms. The maximum IPv4 packet size is 64k-1 (total size must fit
    24  	// in 16 bits). For IPv6, the payload max size (excluding jumbograms) is
    25  	// 64k-1 (also needs to fit in 16 bits). So we use 64k - 1 + 2 * m, where
    26  	// m is the minimum IPv6 header size; we leave room for some potential
    27  	// IP options.
    28  	MaxIPPacketSize = 0xffff + 2*IPv6MinimumSize
    29  )
    30  
    31  // Transport offers generic methods to query and/or update the fields of the
    32  // header of a transport protocol buffer.
    33  type Transport interface {
    34  	// SourcePort returns the value of the "source port" field.
    35  	SourcePort() uint16
    36  
    37  	// Destination returns the value of the "destination port" field.
    38  	DestinationPort() uint16
    39  
    40  	// Checksum returns the value of the "checksum" field.
    41  	Checksum() uint16
    42  
    43  	// SetSourcePort sets the value of the "source port" field.
    44  	SetSourcePort(uint16)
    45  
    46  	// SetDestinationPort sets the value of the "destination port" field.
    47  	SetDestinationPort(uint16)
    48  
    49  	// SetChecksum sets the value of the "checksum" field.
    50  	SetChecksum(uint16)
    51  
    52  	// Payload returns the data carried in the transport buffer.
    53  	Payload() []byte
    54  }
    55  
    56  // ChecksummableTransport is a Transport that supports checksumming.
    57  type ChecksummableTransport interface {
    58  	Transport
    59  
    60  	// SetSourcePortWithChecksumUpdate sets the source port and updates
    61  	// the checksum.
    62  	//
    63  	// The receiver's checksum must be a fully calculated checksum.
    64  	SetSourcePortWithChecksumUpdate(port uint16)
    65  
    66  	// SetDestinationPortWithChecksumUpdate sets the destination port and updates
    67  	// the checksum.
    68  	//
    69  	// The receiver's checksum must be a fully calculated checksum.
    70  	SetDestinationPortWithChecksumUpdate(port uint16)
    71  
    72  	// UpdateChecksumPseudoHeaderAddress updates the checksum to reflect an
    73  	// updated address in the pseudo header.
    74  	//
    75  	// If fullChecksum is true, the receiver's checksum field is assumed to hold a
    76  	// fully calculated checksum. Otherwise, it is assumed to hold a partially
    77  	// calculated checksum which only reflects the pseudo header.
    78  	UpdateChecksumPseudoHeaderAddress(old, new tcpip.Address, fullChecksum bool)
    79  }
    80  
    81  // Network offers generic methods to query and/or update the fields of the
    82  // header of a network protocol buffer.
    83  type Network interface {
    84  	// SourceAddress returns the value of the "source address" field.
    85  	SourceAddress() tcpip.Address
    86  
    87  	// DestinationAddress returns the value of the "destination address"
    88  	// field.
    89  	DestinationAddress() tcpip.Address
    90  
    91  	// Checksum returns the value of the "checksum" field.
    92  	Checksum() uint16
    93  
    94  	// SetSourceAddress sets the value of the "source address" field.
    95  	SetSourceAddress(tcpip.Address)
    96  
    97  	// SetDestinationAddress sets the value of the "destination address"
    98  	// field.
    99  	SetDestinationAddress(tcpip.Address)
   100  
   101  	// SetChecksum sets the value of the "checksum" field.
   102  	SetChecksum(uint16)
   103  
   104  	// TransportProtocol returns the number of the transport protocol
   105  	// stored in the payload.
   106  	TransportProtocol() tcpip.TransportProtocolNumber
   107  
   108  	// Payload returns a byte slice containing the payload of the network
   109  	// packet.
   110  	Payload() []byte
   111  
   112  	// TOS returns the values of the "type of service" and "flow label" fields.
   113  	TOS() (uint8, uint32)
   114  
   115  	// SetTOS sets the values of the "type of service" and "flow label" fields.
   116  	SetTOS(t uint8, l uint32)
   117  }
   118  
   119  // ChecksummableNetwork is a Network that supports checksumming.
   120  type ChecksummableNetwork interface {
   121  	Network
   122  
   123  	// SetSourceAddressAndChecksum sets the source address and updates the
   124  	// checksum to reflect the new address.
   125  	SetSourceAddressWithChecksumUpdate(tcpip.Address)
   126  
   127  	// SetDestinationAddressAndChecksum sets the destination address and
   128  	// updates the checksum to reflect the new address.
   129  	SetDestinationAddressWithChecksumUpdate(tcpip.Address)
   130  }