2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-06-04 23:42:44 +01:00
|
|
|
|
|
|
|
package packet
|
|
|
|
|
|
|
|
import (
|
2020-11-11 03:09:47 +00:00
|
|
|
"encoding/binary"
|
2020-12-20 00:43:25 +00:00
|
|
|
"errors"
|
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 05:14:09 +01:00
|
|
|
"net/netip"
|
2020-07-07 20:25:32 +01:00
|
|
|
|
2021-03-20 04:05:51 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2020-06-04 23:42:44 +01:00
|
|
|
)
|
|
|
|
|
2020-11-11 06:26:00 +00:00
|
|
|
// ip4HeaderLength is the length of an IPv4 header with no IP options.
|
|
|
|
const ip4HeaderLength = 20
|
|
|
|
|
|
|
|
// IP4Header represents an IPv4 packet header.
|
2020-11-09 23:34:03 +00:00
|
|
|
type IP4Header struct {
|
2021-03-20 04:05:51 +00:00
|
|
|
IPProto ipproto.Proto
|
2020-06-04 23:42:44 +01:00
|
|
|
IPID uint16
|
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 05:14:09 +01:00
|
|
|
Src netip.Addr
|
|
|
|
Dst netip.Addr
|
2020-06-04 23:42:44 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 06:26:00 +00:00
|
|
|
// Len implements Header.
|
|
|
|
func (h IP4Header) Len() int {
|
2020-11-10 09:00:35 +00:00
|
|
|
return ip4HeaderLength
|
2020-06-04 23:42:44 +01:00
|
|
|
}
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
var errWrongFamily = errors.New("wrong address family for src/dst IP")
|
|
|
|
|
2020-11-11 06:26:00 +00:00
|
|
|
// Marshal implements Header.
|
2020-11-09 23:34:03 +00:00
|
|
|
func (h IP4Header) Marshal(buf []byte) error {
|
2020-11-11 06:26:00 +00:00
|
|
|
if len(buf) < h.Len() {
|
2020-06-04 23:42:44 +01:00
|
|
|
return errSmallBuffer
|
|
|
|
}
|
|
|
|
if len(buf) > maxPacketLength {
|
|
|
|
return errLargePacket
|
|
|
|
}
|
2020-12-20 00:43:25 +00:00
|
|
|
if !h.Src.Is4() || !h.Dst.Is4() {
|
|
|
|
return errWrongFamily
|
|
|
|
}
|
2020-06-04 23:42:44 +01:00
|
|
|
|
2020-11-11 06:26:00 +00:00
|
|
|
buf[0] = 0x40 | (byte(h.Len() >> 2)) // IPv4 + IHL
|
|
|
|
buf[1] = 0x00 // DSCP + ECN
|
|
|
|
binary.BigEndian.PutUint16(buf[2:4], uint16(len(buf))) // Total length
|
|
|
|
binary.BigEndian.PutUint16(buf[4:6], h.IPID) // ID
|
|
|
|
binary.BigEndian.PutUint16(buf[6:8], 0) // Flags + fragment offset
|
|
|
|
buf[8] = 64 // TTL
|
|
|
|
buf[9] = uint8(h.IPProto) // Inner protocol
|
|
|
|
// Blank checksum. This is necessary even though we overwrite
|
|
|
|
// it later, because the checksum computation runs over these
|
|
|
|
// bytes and expects them to be zero.
|
|
|
|
binary.BigEndian.PutUint16(buf[10:12], 0)
|
2020-12-20 00:43:25 +00:00
|
|
|
src := h.Src.As4()
|
|
|
|
dst := h.Dst.As4()
|
|
|
|
copy(buf[12:16], src[:])
|
|
|
|
copy(buf[16:20], dst[:])
|
2020-11-11 06:26:00 +00:00
|
|
|
|
|
|
|
binary.BigEndian.PutUint16(buf[10:12], ip4Checksum(buf[0:20])) // Checksum
|
2020-06-04 23:42:44 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-11 06:26:00 +00:00
|
|
|
// ToResponse implements Header.
|
|
|
|
func (h *IP4Header) ToResponse() {
|
2020-12-20 00:43:25 +00:00
|
|
|
h.Src, h.Dst = h.Dst, h.Src
|
2020-11-11 06:26:00 +00:00
|
|
|
// Flip the bits in the IPID. If incoming IPIDs are distinct, so are these.
|
|
|
|
h.IPID = ^h.IPID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ip4Checksum computes an IPv4 checksum, as specified in
|
|
|
|
// https://tools.ietf.org/html/rfc1071
|
|
|
|
func ip4Checksum(b []byte) uint16 {
|
|
|
|
var ac uint32
|
|
|
|
i := 0
|
|
|
|
n := len(b)
|
|
|
|
for n >= 2 {
|
|
|
|
ac += uint32(binary.BigEndian.Uint16(b[i : i+2]))
|
|
|
|
n -= 2
|
|
|
|
i += 2
|
|
|
|
}
|
|
|
|
if n == 1 {
|
|
|
|
ac += uint32(b[i]) << 8
|
|
|
|
}
|
|
|
|
for (ac >> 16) > 0 {
|
|
|
|
ac = (ac >> 16) + (ac & 0xffff)
|
|
|
|
}
|
|
|
|
return uint16(^ac)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ip4PseudoHeaderOffset is the number of bytes by which the IPv4 UDP
|
|
|
|
// pseudo-header is smaller than the real IPv4 header.
|
|
|
|
const ip4PseudoHeaderOffset = 8
|
|
|
|
|
|
|
|
// marshalPseudo serializes h into buf in the "pseudo-header" form
|
|
|
|
// required when calculating UDP checksums. The pseudo-header starts
|
|
|
|
// at buf[ip4PseudoHeaderOffset] so as to abut the following UDP
|
|
|
|
// header, while leaving enough space in buf for a full IPv4 header.
|
|
|
|
func (h IP4Header) marshalPseudo(buf []byte) error {
|
|
|
|
if len(buf) < h.Len() {
|
2020-06-04 23:42:44 +01:00
|
|
|
return errSmallBuffer
|
|
|
|
}
|
|
|
|
if len(buf) > maxPacketLength {
|
|
|
|
return errLargePacket
|
|
|
|
}
|
|
|
|
|
2020-11-11 06:26:00 +00:00
|
|
|
length := len(buf) - h.Len()
|
2020-12-20 00:43:25 +00:00
|
|
|
src, dst := h.Src.As4(), h.Dst.As4()
|
|
|
|
copy(buf[8:12], src[:])
|
|
|
|
copy(buf[12:16], dst[:])
|
2020-06-04 23:42:44 +01:00
|
|
|
buf[16] = 0x0
|
|
|
|
buf[17] = uint8(h.IPProto)
|
2020-11-11 03:09:47 +00:00
|
|
|
binary.BigEndian.PutUint16(buf[18:20], uint16(length))
|
2020-06-04 23:42:44 +01:00
|
|
|
return nil
|
|
|
|
}
|