github.com/FlowerWrong/netstack@v0.0.0-20191009141956-e5848263af28/tcpip/header/ipv6.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  	"strings"
    20  
    21  	"github.com/FlowerWrong/netstack/tcpip"
    22  )
    23  
    24  const (
    25  	versTCFL = 0
    26  	// IPv6PayloadLenOffset is the offset of the PayloadLength field in
    27  	// IPv6 header.
    28  	IPv6PayloadLenOffset = 4
    29  	nextHdr              = 6
    30  	hopLimit             = 7
    31  	v6SrcAddr            = 8
    32  	v6DstAddr            = v6SrcAddr + IPv6AddressSize
    33  )
    34  
    35  // IPv6Fields contains the fields of an IPv6 packet. It is used to describe the
    36  // fields of a packet that needs to be encoded.
    37  type IPv6Fields struct {
    38  	// TrafficClass is the "traffic class" field of an IPv6 packet.
    39  	TrafficClass uint8
    40  
    41  	// FlowLabel is the "flow label" field of an IPv6 packet.
    42  	FlowLabel uint32
    43  
    44  	// PayloadLength is the "payload length" field of an IPv6 packet.
    45  	PayloadLength uint16
    46  
    47  	// NextHeader is the "next header" field of an IPv6 packet.
    48  	NextHeader uint8
    49  
    50  	// HopLimit is the "hop limit" field of an IPv6 packet.
    51  	HopLimit uint8
    52  
    53  	// SrcAddr is the "source ip address" of an IPv6 packet.
    54  	SrcAddr tcpip.Address
    55  
    56  	// DstAddr is the "destination ip address" of an IPv6 packet.
    57  	DstAddr tcpip.Address
    58  }
    59  
    60  // IPv6 represents an ipv6 header stored in a byte array.
    61  // Most of the methods of IPv6 access to the underlying slice without
    62  // checking the boundaries and could panic because of 'index out of range'.
    63  // Always call IsValid() to validate an instance of IPv6 before using other methods.
    64  type IPv6 []byte
    65  
    66  const (
    67  	// IPv6MinimumSize is the minimum size of a valid IPv6 packet.
    68  	IPv6MinimumSize = 40
    69  
    70  	// IPv6AddressSize is the size, in bytes, of an IPv6 address.
    71  	IPv6AddressSize = 16
    72  
    73  	// IPv6ProtocolNumber is IPv6's network protocol number.
    74  	IPv6ProtocolNumber tcpip.NetworkProtocolNumber = 0x86dd
    75  
    76  	// IPv6Version is the version of the ipv6 protocol.
    77  	IPv6Version = 6
    78  
    79  	// IPv6AllNodesMulticastAddress is a link-local multicast group that
    80  	// all IPv6 nodes MUST join, as per RFC 4291, section 2.8. Packets
    81  	// destined to this address will reach all nodes on a link.
    82  	//
    83  	// The address is ff02::1.
    84  	IPv6AllNodesMulticastAddress tcpip.Address = "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"
    85  
    86  	// IPv6MinimumMTU is the minimum MTU required by IPv6, per RFC 2460,
    87  	// section 5.
    88  	IPv6MinimumMTU = 1280
    89  
    90  	// IPv6Any is the non-routable IPv6 "any" meta address.
    91  	IPv6Any tcpip.Address = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    92  )
    93  
    94  // IPv6EmptySubnet is the empty IPv6 subnet.
    95  var IPv6EmptySubnet = func() tcpip.Subnet {
    96  	subnet, err := tcpip.NewSubnet(IPv6Any, tcpip.AddressMask(IPv6Any))
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  	return subnet
   101  }()
   102  
   103  // PayloadLength returns the value of the "payload length" field of the ipv6
   104  // header.
   105  func (b IPv6) PayloadLength() uint16 {
   106  	return binary.BigEndian.Uint16(b[IPv6PayloadLenOffset:])
   107  }
   108  
   109  // HopLimit returns the value of the "hop limit" field of the ipv6 header.
   110  func (b IPv6) HopLimit() uint8 {
   111  	return b[hopLimit]
   112  }
   113  
   114  // NextHeader returns the value of the "next header" field of the ipv6 header.
   115  func (b IPv6) NextHeader() uint8 {
   116  	return b[nextHdr]
   117  }
   118  
   119  // TransportProtocol implements Network.TransportProtocol.
   120  func (b IPv6) TransportProtocol() tcpip.TransportProtocolNumber {
   121  	return tcpip.TransportProtocolNumber(b.NextHeader())
   122  }
   123  
   124  // Payload implements Network.Payload.
   125  func (b IPv6) Payload() []byte {
   126  	return b[IPv6MinimumSize:][:b.PayloadLength()]
   127  }
   128  
   129  // SourceAddress returns the "source address" field of the ipv6 header.
   130  func (b IPv6) SourceAddress() tcpip.Address {
   131  	return tcpip.Address(b[v6SrcAddr:][:IPv6AddressSize])
   132  }
   133  
   134  // DestinationAddress returns the "destination address" field of the ipv6
   135  // header.
   136  func (b IPv6) DestinationAddress() tcpip.Address {
   137  	return tcpip.Address(b[v6DstAddr:][:IPv6AddressSize])
   138  }
   139  
   140  // Checksum implements Network.Checksum. Given that IPv6 doesn't have a
   141  // checksum, it just returns 0.
   142  func (IPv6) Checksum() uint16 {
   143  	return 0
   144  }
   145  
   146  // TOS returns the "traffic class" and "flow label" fields of the ipv6 header.
   147  func (b IPv6) TOS() (uint8, uint32) {
   148  	v := binary.BigEndian.Uint32(b[versTCFL:])
   149  	return uint8(v >> 20), v & 0xfffff
   150  }
   151  
   152  // SetTOS sets the "traffic class" and "flow label" fields of the ipv6 header.
   153  func (b IPv6) SetTOS(t uint8, l uint32) {
   154  	vtf := (6 << 28) | (uint32(t) << 20) | (l & 0xfffff)
   155  	binary.BigEndian.PutUint32(b[versTCFL:], vtf)
   156  }
   157  
   158  // SetPayloadLength sets the "payload length" field of the ipv6 header.
   159  func (b IPv6) SetPayloadLength(payloadLength uint16) {
   160  	binary.BigEndian.PutUint16(b[IPv6PayloadLenOffset:], payloadLength)
   161  }
   162  
   163  // SetSourceAddress sets the "source address" field of the ipv6 header.
   164  func (b IPv6) SetSourceAddress(addr tcpip.Address) {
   165  	copy(b[v6SrcAddr:][:IPv6AddressSize], addr)
   166  }
   167  
   168  // SetDestinationAddress sets the "destination address" field of the ipv6
   169  // header.
   170  func (b IPv6) SetDestinationAddress(addr tcpip.Address) {
   171  	copy(b[v6DstAddr:][:IPv6AddressSize], addr)
   172  }
   173  
   174  // SetNextHeader sets the value of the "next header" field of the ipv6 header.
   175  func (b IPv6) SetNextHeader(v uint8) {
   176  	b[nextHdr] = v
   177  }
   178  
   179  // SetChecksum implements Network.SetChecksum. Given that IPv6 doesn't have a
   180  // checksum, it is empty.
   181  func (IPv6) SetChecksum(uint16) {
   182  }
   183  
   184  // Encode encodes all the fields of the ipv6 header.
   185  func (b IPv6) Encode(i *IPv6Fields) {
   186  	b.SetTOS(i.TrafficClass, i.FlowLabel)
   187  	b.SetPayloadLength(i.PayloadLength)
   188  	b[nextHdr] = i.NextHeader
   189  	b[hopLimit] = i.HopLimit
   190  	b.SetSourceAddress(i.SrcAddr)
   191  	b.SetDestinationAddress(i.DstAddr)
   192  }
   193  
   194  // IsValid performs basic validation on the packet.
   195  func (b IPv6) IsValid(pktSize int) bool {
   196  	if len(b) < IPv6MinimumSize {
   197  		return false
   198  	}
   199  
   200  	dlen := int(b.PayloadLength())
   201  	if dlen > pktSize-IPv6MinimumSize {
   202  		return false
   203  	}
   204  
   205  	if IPVersion(b) != IPv6Version {
   206  		return false
   207  	}
   208  
   209  	return true
   210  }
   211  
   212  // IsV4MappedAddress determines if the provided address is an IPv4 mapped
   213  // address by checking if its prefix is 0:0:0:0:0:ffff::/96.
   214  func IsV4MappedAddress(addr tcpip.Address) bool {
   215  	if len(addr) != IPv6AddressSize {
   216  		return false
   217  	}
   218  
   219  	return strings.HasPrefix(string(addr), "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff")
   220  }
   221  
   222  // IsV6MulticastAddress determines if the provided address is an IPv6
   223  // multicast address (anything starting with FF).
   224  func IsV6MulticastAddress(addr tcpip.Address) bool {
   225  	if len(addr) != IPv6AddressSize {
   226  		return false
   227  	}
   228  	return addr[0] == 0xff
   229  }
   230  
   231  // IsV6UnicastAddress determines if the provided address is a valid IPv6
   232  // unicast (and specified) address. That is, IsV6UnicastAddress returns
   233  // true if addr contains IPv6AddressSize bytes, is not the unspecified
   234  // address and is not a multicast address.
   235  func IsV6UnicastAddress(addr tcpip.Address) bool {
   236  	if len(addr) != IPv6AddressSize {
   237  		return false
   238  	}
   239  
   240  	// Must not be unspecified
   241  	if addr == IPv6Any {
   242  		return false
   243  	}
   244  
   245  	// Return if not a multicast.
   246  	return addr[0] != 0xff
   247  }
   248  
   249  // SolicitedNodeAddr computes the solicited-node multicast address. This is
   250  // used for NDP. Described in RFC 4291. The argument must be a full-length IPv6
   251  // address.
   252  func SolicitedNodeAddr(addr tcpip.Address) tcpip.Address {
   253  	const solicitedNodeMulticastPrefix = "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff"
   254  	return solicitedNodeMulticastPrefix + addr[len(addr)-3:]
   255  }
   256  
   257  // LinkLocalAddr computes the default IPv6 link-local address from a link-layer
   258  // (MAC) address.
   259  func LinkLocalAddr(linkAddr tcpip.LinkAddress) tcpip.Address {
   260  	// Convert a 48-bit MAC to an EUI-64 and then prepend the link-local
   261  	// header, FE80::.
   262  	//
   263  	// The conversion is very nearly:
   264  	//	aa:bb:cc:dd:ee:ff => FE80::Aabb:ccFF:FEdd:eeff
   265  	// Note the capital A. The conversion aa->Aa involves a bit flip.
   266  	lladdrb := [16]byte{
   267  		0:  0xFE,
   268  		1:  0x80,
   269  		8:  linkAddr[0] ^ 2,
   270  		9:  linkAddr[1],
   271  		10: linkAddr[2],
   272  		11: 0xFF,
   273  		12: 0xFE,
   274  		13: linkAddr[3],
   275  		14: linkAddr[4],
   276  		15: linkAddr[5],
   277  	}
   278  	return tcpip.Address(lladdrb[:])
   279  }
   280  
   281  // IsV6LinkLocalAddress determines if the provided address is an IPv6
   282  // link-local address (fe80::/10).
   283  func IsV6LinkLocalAddress(addr tcpip.Address) bool {
   284  	if len(addr) != IPv6AddressSize {
   285  		return false
   286  	}
   287  	return addr[0] == 0xfe && (addr[1]&0xc0) == 0x80
   288  }