github.com/polevpn/netstack@v1.10.9/tcpip/header/gue.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  const (
    18  	typeHLen   = 0
    19  	encapProto = 1
    20  )
    21  
    22  // GUEFields contains the fields of a GUE packet. It is used to describe the
    23  // fields of a packet that needs to be encoded.
    24  type GUEFields struct {
    25  	// Type is the "type" field of the GUE header.
    26  	Type uint8
    27  
    28  	// Control is the "control" field of the GUE header.
    29  	Control bool
    30  
    31  	// HeaderLength is the "header length" field of the GUE header. It must
    32  	// be at least 4 octets, and a multiple of 4 as well.
    33  	HeaderLength uint8
    34  
    35  	// Protocol is the "protocol" field of the GUE header. This is one of
    36  	// the IPPROTO_* values.
    37  	Protocol uint8
    38  }
    39  
    40  // GUE represents a Generic UDP Encapsulation header stored in a byte array, the
    41  // fields are described in https://tools.ietf.org/html/draft-ietf-nvo3-gue-01.
    42  type GUE []byte
    43  
    44  const (
    45  	// GUEMinimumSize is the minimum size of a valid GUE packet.
    46  	GUEMinimumSize = 4
    47  )
    48  
    49  // TypeAndControl returns the GUE packet type (top 3 bits of the first byte,
    50  // which includes the control bit).
    51  func (b GUE) TypeAndControl() uint8 {
    52  	return b[typeHLen] >> 5
    53  }
    54  
    55  // HeaderLength returns the total length of the GUE header.
    56  func (b GUE) HeaderLength() uint8 {
    57  	return 4 + 4*(b[typeHLen]&0x1f)
    58  }
    59  
    60  // Protocol returns the protocol field of the GUE header.
    61  func (b GUE) Protocol() uint8 {
    62  	return b[encapProto]
    63  }
    64  
    65  // Encode encodes all the fields of the GUE header.
    66  func (b GUE) Encode(i *GUEFields) {
    67  	ctl := uint8(0)
    68  	if i.Control {
    69  		ctl = 1 << 5
    70  	}
    71  	b[typeHLen] = ctl | i.Type<<6 | (i.HeaderLength-4)/4
    72  	b[encapProto] = i.Protocol
    73  }