github.com/flowerwrong/netstack@v0.0.0-20191009141956-e5848263af28/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  	"github.com/FlowerWrong/netstack/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  	// EthernetAddressSize is the size, in bytes, of an ethernet address.
    50  	EthernetAddressSize = 6
    51  )
    52  
    53  // SourceAddress returns the "MAC source" field of the ethernet frame header.
    54  func (b Ethernet) SourceAddress() tcpip.LinkAddress {
    55  	return tcpip.LinkAddress(b[srcMAC:][:EthernetAddressSize])
    56  }
    57  
    58  // DestinationAddress returns the "MAC destination" field of the ethernet frame
    59  // header.
    60  func (b Ethernet) DestinationAddress() tcpip.LinkAddress {
    61  	return tcpip.LinkAddress(b[dstMAC:][:EthernetAddressSize])
    62  }
    63  
    64  // Type returns the "ethertype" field of the ethernet frame header.
    65  func (b Ethernet) Type() tcpip.NetworkProtocolNumber {
    66  	return tcpip.NetworkProtocolNumber(binary.BigEndian.Uint16(b[ethType:]))
    67  }
    68  
    69  // Encode encodes all the fields of the ethernet frame header.
    70  func (b Ethernet) Encode(e *EthernetFields) {
    71  	binary.BigEndian.PutUint16(b[ethType:], uint16(e.Type))
    72  	copy(b[srcMAC:][:EthernetAddressSize], e.SrcAddr)
    73  	copy(b[dstMAC:][:EthernetAddressSize], e.DstAddr)
    74  }