mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-13 19:54:46 +00:00
affc4530a2
It will be used for ICMPv6 next, so pass in the proto. Also, use the ipproto constants rather than hardcoding the mysterious number. Change-Id: I57b68bdd2d39fff75f82affe955aff9245de246b Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package packet
|
|
|
|
import (
|
|
"encoding/binary"
|
|
|
|
"inet.af/netaddr"
|
|
"tailscale.com/types/ipproto"
|
|
)
|
|
|
|
// ip6HeaderLength is the length of an IPv6 header with no IP options.
|
|
const ip6HeaderLength = 40
|
|
|
|
// IP6Header represents an IPv6 packet header.
|
|
type IP6Header struct {
|
|
IPProto ipproto.Proto
|
|
IPID uint32 // only lower 20 bits used
|
|
Src netaddr.IP
|
|
Dst netaddr.IP
|
|
}
|
|
|
|
// Len implements Header.
|
|
func (h IP6Header) Len() int {
|
|
return ip6HeaderLength
|
|
}
|
|
|
|
// Marshal implements Header.
|
|
func (h IP6Header) Marshal(buf []byte) error {
|
|
if len(buf) < h.Len() {
|
|
return errSmallBuffer
|
|
}
|
|
if len(buf) > maxPacketLength {
|
|
return errLargePacket
|
|
}
|
|
|
|
binary.BigEndian.PutUint32(buf[:4], h.IPID&0x000FFFFF)
|
|
buf[0] = 0x60
|
|
binary.BigEndian.PutUint16(buf[4:6], uint16(len(buf)-ip6HeaderLength)) // Total length
|
|
buf[6] = uint8(h.IPProto) // Inner protocol
|
|
buf[7] = 64 // TTL
|
|
src, dst := h.Src.As16(), h.Dst.As16()
|
|
copy(buf[8:24], src[:])
|
|
copy(buf[24:40], dst[:])
|
|
|
|
return nil
|
|
}
|
|
|
|
// ToResponse implements Header.
|
|
func (h *IP6Header) ToResponse() {
|
|
h.Src, h.Dst = h.Dst, h.Src
|
|
// Flip the bits in the IPID. If incoming IPIDs are distinct, so are these.
|
|
h.IPID = (^h.IPID) & 0x000FFFFF
|
|
}
|
|
|
|
// marshalPseudo serializes h into buf in the "pseudo-header" form
|
|
// required when calculating UDP checksums.
|
|
func (h IP6Header) marshalPseudo(buf []byte, proto ipproto.Proto) error {
|
|
if len(buf) < h.Len() {
|
|
return errSmallBuffer
|
|
}
|
|
if len(buf) > maxPacketLength {
|
|
return errLargePacket
|
|
}
|
|
|
|
src, dst := h.Src.As16(), h.Dst.As16()
|
|
copy(buf[:16], src[:])
|
|
copy(buf[16:32], dst[:])
|
|
binary.BigEndian.PutUint32(buf[32:36], uint32(len(buf)-h.Len()))
|
|
buf[36] = 0
|
|
buf[37] = 0
|
|
buf[38] = 0
|
|
buf[39] = byte(proto) // NextProto
|
|
return nil
|
|
}
|