2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-11-10 09:00:35 +00:00
|
|
|
|
2020-11-10 08:04:27 +00:00
|
|
|
package packet
|
|
|
|
|
|
|
|
import (
|
2020-11-11 07:23:17 +00:00
|
|
|
"encoding/binary"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
"net/netip"
|
2020-11-10 08:04:27 +00:00
|
|
|
|
2021-03-20 04:05:51 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2020-11-10 08:04:27 +00:00
|
|
|
)
|
|
|
|
|
2020-11-11 06:26:00 +00:00
|
|
|
// ip6HeaderLength is the length of an IPv6 header with no IP options.
|
2020-11-10 09:00:35 +00:00
|
|
|
const ip6HeaderLength = 40
|
2020-11-11 07:23:17 +00:00
|
|
|
|
|
|
|
// IP6Header represents an IPv6 packet header.
|
|
|
|
type IP6Header struct {
|
2021-03-20 04:05:51 +00:00
|
|
|
IPProto ipproto.Proto
|
2020-11-11 07:23:17 +00:00
|
|
|
IPID uint32 // only lower 20 bits used
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Src netip.Addr
|
|
|
|
Dst netip.Addr
|
2020-11-11 07:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-12-20 00:43:25 +00:00
|
|
|
src, dst := h.Src.As16(), h.Dst.As16()
|
|
|
|
copy(buf[8:24], src[:])
|
|
|
|
copy(buf[24:40], dst[:])
|
2020-11-11 07:23:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToResponse implements Header.
|
|
|
|
func (h *IP6Header) ToResponse() {
|
2020-12-20 00:43:25 +00:00
|
|
|
h.Src, h.Dst = h.Dst, h.Src
|
2020-11-11 07:23:17 +00:00
|
|
|
// 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.
|
2021-12-09 05:15:46 +00:00
|
|
|
func (h IP6Header) marshalPseudo(buf []byte, proto ipproto.Proto) error {
|
2020-11-11 07:23:17 +00:00
|
|
|
if len(buf) < h.Len() {
|
|
|
|
return errSmallBuffer
|
|
|
|
}
|
|
|
|
if len(buf) > maxPacketLength {
|
|
|
|
return errLargePacket
|
|
|
|
}
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
src, dst := h.Src.As16(), h.Dst.As16()
|
|
|
|
copy(buf[:16], src[:])
|
|
|
|
copy(buf[16:32], dst[:])
|
2020-11-11 07:23:17 +00:00
|
|
|
binary.BigEndian.PutUint32(buf[32:36], uint32(len(buf)-h.Len()))
|
|
|
|
buf[36] = 0
|
|
|
|
buf[37] = 0
|
|
|
|
buf[38] = 0
|
2021-12-09 05:15:46 +00:00
|
|
|
buf[39] = byte(proto) // NextProto
|
2020-11-11 07:23:17 +00:00
|
|
|
return nil
|
|
|
|
}
|