gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/header/eth.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  	"encoding/binary"
    19  
    20  	"gvisor.dev/gvisor/pkg/tcpip"
    21  )
    22  
    23  const (
    24  	dstMAC  = 0
    25  	srcMAC  = 6
    26  	ethType = 12
    27  )
    28  
    29  // EthernetFields contains the fields of an ethernet frame header. It is used to
    30  // describe the fields of a frame that needs to be encoded.
    31  type EthernetFields struct {
    32  	// SrcAddr is the "MAC source" field of an ethernet frame header.
    33  	SrcAddr tcpip.LinkAddress
    34  
    35  	// DstAddr is the "MAC destination" field of an ethernet frame header.
    36  	DstAddr tcpip.LinkAddress
    37  
    38  	// Type is the "ethertype" field of an ethernet frame header.
    39  	Type tcpip.NetworkProtocolNumber
    40  }
    41  
    42  // Ethernet represents an ethernet frame header stored in a byte array.
    43  type Ethernet []byte
    44  
    45  const (
    46  	// EthernetMinimumSize is the minimum size of a valid ethernet frame.
    47  	EthernetMinimumSize = 14
    48  
    49  	// EthernetMaximumSize is the maximum size of a valid ethernet frame.
    50  	EthernetMaximumSize = 18
    51  
    52  	// EthernetAddressSize is the size, in bytes, of an ethernet address.
    53  	EthernetAddressSize = 6
    54  
    55  	// UnspecifiedEthernetAddress is the unspecified ethernet address
    56  	// (all bits set to 0).
    57  	UnspecifiedEthernetAddress = tcpip.LinkAddress("\x00\x00\x00\x00\x00\x00")
    58  
    59  	// EthernetBroadcastAddress is an ethernet address that addresses every node
    60  	// on a local link.
    61  	EthernetBroadcastAddress = tcpip.LinkAddress("\xff\xff\xff\xff\xff\xff")
    62  
    63  	// unicastMulticastFlagMask is the mask of the least significant bit in
    64  	// the first octet (in network byte order) of an ethernet address that
    65  	// determines whether the ethernet address is a unicast or multicast. If
    66  	// the masked bit is a 1, then the address is a multicast, unicast
    67  	// otherwise.
    68  	//
    69  	// See the IEEE Std 802-2001 document for more details. Specifically,
    70  	// section 9.2.1 of http://ieee802.org/secmail/pdfocSP2xXA6d.pdf:
    71  	// "A 48-bit universal address consists of two parts. The first 24 bits
    72  	// correspond to the OUI as assigned by the IEEE, expect that the
    73  	// assignee may set the LSB of the first octet to 1 for group addresses
    74  	// or set it to 0 for individual addresses."
    75  	unicastMulticastFlagMask = 1
    76  
    77  	// unicastMulticastFlagByteIdx is the byte that holds the
    78  	// unicast/multicast flag. See unicastMulticastFlagMask.
    79  	unicastMulticastFlagByteIdx = 0
    80  )
    81  
    82  const (
    83  	// EthernetProtocolAll is a catch-all for all protocols carried inside
    84  	// an ethernet frame. It is mainly used to create packet sockets that
    85  	// capture all traffic.
    86  	EthernetProtocolAll tcpip.NetworkProtocolNumber = 0x0003
    87  
    88  	// EthernetProtocolPUP is the PARC Universal Packet protocol ethertype.
    89  	EthernetProtocolPUP tcpip.NetworkProtocolNumber = 0x0200
    90  )
    91  
    92  // Ethertypes holds the protocol numbers describing the payload of an ethernet
    93  // frame. These types aren't necessarily supported by netstack, but can be used
    94  // to catch all traffic of a type via packet endpoints.
    95  var Ethertypes = []tcpip.NetworkProtocolNumber{
    96  	EthernetProtocolAll,
    97  	EthernetProtocolPUP,
    98  }
    99  
   100  // SourceAddress returns the "MAC source" field of the ethernet frame header.
   101  func (b Ethernet) SourceAddress() tcpip.LinkAddress {
   102  	return tcpip.LinkAddress(b[srcMAC:][:EthernetAddressSize])
   103  }
   104  
   105  // DestinationAddress returns the "MAC destination" field of the ethernet frame
   106  // header.
   107  func (b Ethernet) DestinationAddress() tcpip.LinkAddress {
   108  	return tcpip.LinkAddress(b[dstMAC:][:EthernetAddressSize])
   109  }
   110  
   111  // Type returns the "ethertype" field of the ethernet frame header.
   112  func (b Ethernet) Type() tcpip.NetworkProtocolNumber {
   113  	return tcpip.NetworkProtocolNumber(binary.BigEndian.Uint16(b[ethType:]))
   114  }
   115  
   116  // Encode encodes all the fields of the ethernet frame header.
   117  func (b Ethernet) Encode(e *EthernetFields) {
   118  	binary.BigEndian.PutUint16(b[ethType:], uint16(e.Type))
   119  	copy(b[srcMAC:][:EthernetAddressSize], e.SrcAddr)
   120  	copy(b[dstMAC:][:EthernetAddressSize], e.DstAddr)
   121  }
   122  
   123  // IsMulticastEthernetAddress returns true if the address is a multicast
   124  // ethernet address.
   125  func IsMulticastEthernetAddress(addr tcpip.LinkAddress) bool {
   126  	if len(addr) != EthernetAddressSize {
   127  		return false
   128  	}
   129  
   130  	return addr[unicastMulticastFlagByteIdx]&unicastMulticastFlagMask != 0
   131  }
   132  
   133  // IsValidUnicastEthernetAddress returns true if the address is a unicast
   134  // ethernet address.
   135  func IsValidUnicastEthernetAddress(addr tcpip.LinkAddress) bool {
   136  	if len(addr) != EthernetAddressSize {
   137  		return false
   138  	}
   139  
   140  	if addr == UnspecifiedEthernetAddress {
   141  		return false
   142  	}
   143  
   144  	if addr[unicastMulticastFlagByteIdx]&unicastMulticastFlagMask != 0 {
   145  		return false
   146  	}
   147  
   148  	return true
   149  }
   150  
   151  // EthernetAddressFromMulticastIPv4Address returns a multicast Ethernet address
   152  // for a multicast IPv4 address.
   153  //
   154  // addr MUST be a multicast IPv4 address.
   155  func EthernetAddressFromMulticastIPv4Address(addr tcpip.Address) tcpip.LinkAddress {
   156  	var linkAddrBytes [EthernetAddressSize]byte
   157  	// RFC 1112 Host Extensions for IP Multicasting
   158  	//
   159  	// 6.4. Extensions to an Ethernet Local Network Module:
   160  	//
   161  	// An IP host group address is mapped to an Ethernet multicast
   162  	// address by placing the low-order 23-bits of the IP address
   163  	// into the low-order 23 bits of the Ethernet multicast address
   164  	// 01-00-5E-00-00-00 (hex).
   165  	addrBytes := addr.As4()
   166  	linkAddrBytes[0] = 0x1
   167  	linkAddrBytes[2] = 0x5e
   168  	linkAddrBytes[3] = addrBytes[1] & 0x7F
   169  	copy(linkAddrBytes[4:], addrBytes[IPv4AddressSize-2:])
   170  	return tcpip.LinkAddress(linkAddrBytes[:])
   171  }
   172  
   173  // EthernetAddressFromMulticastIPv6Address returns a multicast Ethernet address
   174  // for a multicast IPv6 address.
   175  //
   176  // addr MUST be a multicast IPv6 address.
   177  func EthernetAddressFromMulticastIPv6Address(addr tcpip.Address) tcpip.LinkAddress {
   178  	// RFC 2464 Transmission of IPv6 Packets over Ethernet Networks
   179  	//
   180  	// 7. Address Mapping -- Multicast
   181  	//
   182  	// An IPv6 packet with a multicast destination address DST,
   183  	// consisting of the sixteen octets DST[1] through DST[16], is
   184  	// transmitted to the Ethernet multicast address whose first
   185  	// two octets are the value 3333 hexadecimal and whose last
   186  	// four octets are the last four octets of DST.
   187  	addrBytes := addr.As16()
   188  	linkAddrBytes := []byte(addrBytes[IPv6AddressSize-EthernetAddressSize:])
   189  	linkAddrBytes[0] = 0x33
   190  	linkAddrBytes[1] = 0x33
   191  	return tcpip.LinkAddress(linkAddrBytes[:])
   192  }