wgengine/packet: rename types to reflect their v4-only-ness, document.

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/890/head
David Anderson 4 years ago
parent ebd96bf4a9
commit 19df6a2ee2

@ -77,8 +77,8 @@ const (
) )
type tuple struct { type tuple struct {
SrcIP packet.IP SrcIP packet.IP4
DstIP packet.IP DstIP packet.IP4
SrcPort uint16 SrcPort uint16
DstPort uint16 DstPort uint16
} }
@ -412,7 +412,7 @@ func omitDropLogging(p *packet.ParsedPacket, dir direction) bool {
// it doesn't know about, so parse it out ourselves if needed. // it doesn't know about, so parse it out ourselves if needed.
ipProto := p.IPProto ipProto := p.IPProto
if ipProto == 0 && len(b) > 8 { if ipProto == 0 && len(b) > 8 {
ipProto = packet.IPProto(b[9]) ipProto = packet.IP4Proto(b[9])
} }
// Omit logging about outgoing IGMP. // Omit logging about outgoing IGMP.
if ipProto == packet.IGMP { if ipProto == packet.IGMP {

@ -16,17 +16,13 @@ import (
"tailscale.com/wgengine/packet" "tailscale.com/wgengine/packet"
) )
// Type aliases only in test code: (but ideally nowhere)
type ParsedPacket = packet.ParsedPacket
type IP = packet.IP
var Unknown = packet.Unknown var Unknown = packet.Unknown
var ICMP = packet.ICMP var ICMP = packet.ICMP
var TCP = packet.TCP var TCP = packet.TCP
var UDP = packet.UDP var UDP = packet.UDP
var Fragment = packet.Fragment var Fragment = packet.Fragment
func nets(ips []IP) []Net { func nets(ips []packet.IP4) []Net {
out := make([]Net, 0, len(ips)) out := make([]Net, 0, len(ips))
for _, ip := range ips { for _, ip := range ips {
out = append(out, Net{ip, Netmask(32)}) out = append(out, Net{ip, Netmask(32)})
@ -34,35 +30,35 @@ func nets(ips []IP) []Net {
return out return out
} }
func ippr(ip IP, start, end uint16) []NetPortRange { func ippr(ip packet.IP4, start, end uint16) []NetPortRange {
return []NetPortRange{ return []NetPortRange{
NetPortRange{Net{ip, Netmask(32)}, PortRange{start, end}}, NetPortRange{Net{ip, Netmask(32)}, PortRange{start, end}},
} }
} }
func netpr(ip IP, bits int, start, end uint16) []NetPortRange { func netpr(ip packet.IP4, bits int, start, end uint16) []NetPortRange {
return []NetPortRange{ return []NetPortRange{
NetPortRange{Net{ip, Netmask(bits)}, PortRange{start, end}}, NetPortRange{Net{ip, Netmask(bits)}, PortRange{start, end}},
} }
} }
var matches = Matches{ var matches = Matches{
{Srcs: nets([]IP{0x08010101, 0x08020202}), Dsts: []NetPortRange{ {Srcs: nets([]packet.IP4{0x08010101, 0x08020202}), Dsts: []NetPortRange{
NetPortRange{Net{0x01020304, Netmask(32)}, PortRange{22, 22}}, NetPortRange{Net{0x01020304, Netmask(32)}, PortRange{22, 22}},
NetPortRange{Net{0x05060708, Netmask(32)}, PortRange{23, 24}}, NetPortRange{Net{0x05060708, Netmask(32)}, PortRange{23, 24}},
}}, }},
{Srcs: nets([]IP{0x08010101, 0x08020202}), Dsts: ippr(0x05060708, 27, 28)}, {Srcs: nets([]packet.IP4{0x08010101, 0x08020202}), Dsts: ippr(0x05060708, 27, 28)},
{Srcs: nets([]IP{0x02020202}), Dsts: ippr(0x08010101, 22, 22)}, {Srcs: nets([]packet.IP4{0x02020202}), Dsts: ippr(0x08010101, 22, 22)},
{Srcs: []Net{NetAny}, Dsts: ippr(0x647a6232, 0, 65535)}, {Srcs: []Net{NetAny}, Dsts: ippr(0x647a6232, 0, 65535)},
{Srcs: []Net{NetAny}, Dsts: netpr(0, 0, 443, 443)}, {Srcs: []Net{NetAny}, Dsts: netpr(0, 0, 443, 443)},
{Srcs: nets([]IP{0x99010101, 0x99010102, 0x99030303}), Dsts: ippr(0x01020304, 999, 999)}, {Srcs: nets([]packet.IP4{0x99010101, 0x99010102, 0x99030303}), Dsts: ippr(0x01020304, 999, 999)},
} }
func newFilter(logf logger.Logf) *Filter { func newFilter(logf logger.Logf) *Filter {
// Expects traffic to 100.122.98.50, 1.2.3.4, 5.6.7.8, // Expects traffic to 100.122.98.50, 1.2.3.4, 5.6.7.8,
// 102.102.102.102, 119.119.119.119, 8.1.0.0/16 // 102.102.102.102, 119.119.119.119, 8.1.0.0/16
localNets := nets([]IP{0x647a6232, 0x01020304, 0x05060708, 0x66666666, 0x77777777}) localNets := nets([]packet.IP4{0x647a6232, 0x01020304, 0x05060708, 0x66666666, 0x77777777})
localNets = append(localNets, Net{IP(0x08010000), Netmask(16)}) localNets = append(localNets, Net{packet.IP4(0x08010000), Netmask(16)})
return New(matches, localNets, nil, logf) return New(matches, localNets, nil, logf)
} }
@ -87,7 +83,7 @@ func TestFilter(t *testing.T) {
type InOut struct { type InOut struct {
want Response want Response
p ParsedPacket p packet.ParsedPacket
} }
tests := []InOut{ tests := []InOut{
// Basic // Basic
@ -147,7 +143,7 @@ func TestNoAllocs(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got := int(testing.AllocsPerRun(1000, func() { got := int(testing.AllocsPerRun(1000, func() {
q := &ParsedPacket{} q := &packet.ParsedPacket{}
q.Decode(test.packet) q.Decode(test.packet)
if test.in { if test.in {
acl.RunIn(q, 0) acl.RunIn(q, 0)
@ -170,7 +166,7 @@ func TestParseIP(t *testing.T) {
want Net want Net
wantErr string wantErr string
}{ }{
{"8.8.8.8", 24, Net{IP: packet.NewIP(net.ParseIP("8.8.8.8")), Mask: packet.NewIP(net.ParseIP("255.255.255.0"))}, ""}, {"8.8.8.8", 24, Net{IP: packet.NewIP4(net.ParseIP("8.8.8.8")), Mask: packet.NewIP4(net.ParseIP("255.255.255.0"))}, ""},
{"8.8.8.8", 33, Net{}, `invalid CIDR size 33 for host "8.8.8.8"`}, {"8.8.8.8", 33, Net{}, `invalid CIDR size 33 for host "8.8.8.8"`},
{"8.8.8.8", -1, Net{}, `invalid CIDR size -1 for host "8.8.8.8"`}, {"8.8.8.8", -1, Net{}, `invalid CIDR size -1 for host "8.8.8.8"`},
{"0.0.0.0", 24, Net{}, `ports="0.0.0.0": to allow all IP addresses, use *:port, not 0.0.0.0:port`}, {"0.0.0.0", 24, Net{}, `ports="0.0.0.0": to allow all IP addresses, use *:port, not 0.0.0.0:port`},
@ -220,7 +216,7 @@ func BenchmarkFilter(b *testing.B) {
for _, bench := range benches { for _, bench := range benches {
b.Run(bench.name, func(b *testing.B) { b.Run(bench.name, func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
q := &ParsedPacket{} q := &packet.ParsedPacket{}
q.Decode(bench.packet) q.Decode(bench.packet)
// This branch seems to have no measurable impact on performance. // This branch seems to have no measurable impact on performance.
if bench.in { if bench.in {
@ -249,7 +245,7 @@ func TestPreFilter(t *testing.T) {
} }
f := NewAllowNone(t.Logf) f := NewAllowNone(t.Logf)
for _, testPacket := range packets { for _, testPacket := range packets {
p := &ParsedPacket{} p := &packet.ParsedPacket{}
p.Decode(testPacket.b) p.Decode(testPacket.b)
got := f.pre(p, LogDrops|LogAccepts, in) got := f.pre(p, LogDrops|LogAccepts, in)
if got != testPacket.want { if got != testPacket.want {
@ -258,8 +254,8 @@ func TestPreFilter(t *testing.T) {
} }
} }
func parsed(proto packet.IPProto, src, dst packet.IP, sport, dport uint16) ParsedPacket { func parsed(proto packet.IP4Proto, src, dst packet.IP4, sport, dport uint16) packet.ParsedPacket {
return ParsedPacket{ return packet.ParsedPacket{
IPProto: proto, IPProto: proto,
SrcIP: src, SrcIP: src,
DstIP: dst, DstIP: dst,
@ -271,7 +267,7 @@ func parsed(proto packet.IPProto, src, dst packet.IP, sport, dport uint16) Parse
// rawpacket generates a packet with given source and destination ports and IPs // rawpacket generates a packet with given source and destination ports and IPs
// and resizes the header to trimLength if it is nonzero. // and resizes the header to trimLength if it is nonzero.
func rawpacket(proto packet.IPProto, src, dst packet.IP, sport, dport uint16, trimLength int) []byte { func rawpacket(proto packet.IP4Proto, src, dst packet.IP4, sport, dport uint16, trimLength int) []byte {
var headerLength int var headerLength int
switch proto { switch proto {
@ -325,8 +321,8 @@ func rawpacket(proto packet.IPProto, src, dst packet.IP, sport, dport uint16, tr
} }
// rawdefault calls rawpacket with default ports and IPs. // rawdefault calls rawpacket with default ports and IPs.
func rawdefault(proto packet.IPProto, trimLength int) []byte { func rawdefault(proto packet.IP4Proto, trimLength int) []byte {
ip := IP(0x08080808) // 8.8.8.8 ip := packet.IP4(0x08080808) // 8.8.8.8
port := uint16(53) port := uint16(53)
return rawpacket(proto, ip, ip, port, port, trimLength) return rawpacket(proto, ip, ip, port, port, trimLength)
} }
@ -381,19 +377,19 @@ func TestOmitDropLogging(t *testing.T) {
}, },
{ {
name: "v4_multicast_out_low", name: "v4_multicast_out_low",
pkt: &packet.ParsedPacket{IPVersion: 4, DstIP: packet.NewIP(net.ParseIP("224.0.0.0"))}, pkt: &packet.ParsedPacket{IPVersion: 4, DstIP: packet.NewIP4(net.ParseIP("224.0.0.0"))},
dir: out, dir: out,
want: true, want: true,
}, },
{ {
name: "v4_multicast_out_high", name: "v4_multicast_out_high",
pkt: &packet.ParsedPacket{IPVersion: 4, DstIP: packet.NewIP(net.ParseIP("239.255.255.255"))}, pkt: &packet.ParsedPacket{IPVersion: 4, DstIP: packet.NewIP4(net.ParseIP("239.255.255.255"))},
dir: out, dir: out,
want: true, want: true,
}, },
{ {
name: "v4_link_local_unicast", name: "v4_link_local_unicast",
pkt: &packet.ParsedPacket{IPVersion: 4, DstIP: packet.NewIP(net.ParseIP("169.254.1.2"))}, pkt: &packet.ParsedPacket{IPVersion: 4, DstIP: packet.NewIP4(net.ParseIP("169.254.1.2"))},
dir: out, dir: out,
want: true, want: true,
}, },

@ -13,16 +13,16 @@ import (
"tailscale.com/wgengine/packet" "tailscale.com/wgengine/packet"
) )
func NewIP(ip net.IP) packet.IP { func NewIP(ip net.IP) packet.IP4 {
return packet.NewIP(ip) return packet.NewIP4(ip)
} }
type Net struct { type Net struct {
IP packet.IP IP packet.IP4
Mask packet.IP Mask packet.IP4
} }
func (n Net) Includes(ip packet.IP) bool { func (n Net) Includes(ip packet.IP4) bool {
return (n.IP & n.Mask) == (ip & n.Mask) return (n.IP & n.Mask) == (ip & n.Mask)
} }
@ -42,11 +42,11 @@ func (n Net) String() string {
} }
var NetAny = Net{0, 0} var NetAny = Net{0, 0}
var NetNone = Net{^packet.IP(0), ^packet.IP(0)} var NetNone = Net{^packet.IP4(0), ^packet.IP4(0)}
func Netmask(bits int) packet.IP { func Netmask(bits int) packet.IP4 {
b := ^uint32((1 << (32 - bits)) - 1) b := ^uint32((1 << (32 - bits)) - 1)
return packet.IP(b) return packet.IP4(b)
} }
type PortRange struct { type PortRange struct {
@ -124,7 +124,7 @@ func (m Matches) Clone() (res Matches) {
return res return res
} }
func ipInList(ip packet.IP, netlist []Net) bool { func ipInList(ip packet.IP4, netlist []Net) bool {
for _, net := range netlist { for _, net := range netlist {
if net.Includes(ip) { if net.Includes(ip) {
return true return true

@ -0,0 +1,16 @@
// 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 contains packet parsing and marshaling utilities.
//
// ParsedPacket provides allocation-free minimal packet header
// decoding, for use in packet filtering. The other types in the
// package are for constructing and marshaling packets into []bytes.
//
// To support allocation-free parsing, this package defines IPv4 and
// IPv6 address types. You should prefer to use netaddr's types,
// except where you absolutely need allocation-free IP handling
// (i.e. in the tunnel datapath) and are willing to implement all
// codepaths and data structures twice, once per IP family.
package packet

@ -4,41 +4,41 @@
package packet package packet
type ICMPType uint8 type ICMP4Type uint8
const ( const (
ICMPEchoReply ICMPType = 0x00 ICMP4EchoReply ICMP4Type = 0x00
ICMPEchoRequest ICMPType = 0x08 ICMP4EchoRequest ICMP4Type = 0x08
ICMPUnreachable ICMPType = 0x03 ICMP4Unreachable ICMP4Type = 0x03
ICMPTimeExceeded ICMPType = 0x0b ICMP4TimeExceeded ICMP4Type = 0x0b
) )
func (t ICMPType) String() string { func (t ICMP4Type) String() string {
switch t { switch t {
case ICMPEchoReply: case ICMP4EchoReply:
return "EchoReply" return "EchoReply"
case ICMPEchoRequest: case ICMP4EchoRequest:
return "EchoRequest" return "EchoRequest"
case ICMPUnreachable: case ICMP4Unreachable:
return "Unreachable" return "Unreachable"
case ICMPTimeExceeded: case ICMP4TimeExceeded:
return "TimeExceeded" return "TimeExceeded"
default: default:
return "Unknown" return "Unknown"
} }
} }
type ICMPCode uint8 type ICMP4Code uint8
const ( const (
ICMPNoCode ICMPCode = 0 ICMP4NoCode ICMP4Code = 0
) )
// ICMPHeader represents an ICMP packet header. // ICMPHeader represents an ICMP packet header.
type ICMPHeader struct { type ICMP4Header struct {
IPHeader IP4Header
Type ICMPType Type ICMP4Type
Code ICMPCode Code ICMP4Code
} }
const ( const (
@ -47,11 +47,11 @@ const (
icmpAllHeadersLength = ipHeaderLength + icmpHeaderLength icmpAllHeadersLength = ipHeaderLength + icmpHeaderLength
) )
func (ICMPHeader) Len() int { func (ICMP4Header) Len() int {
return icmpAllHeadersLength return icmpAllHeadersLength
} }
func (h ICMPHeader) Marshal(buf []byte) error { func (h ICMP4Header) Marshal(buf []byte) error {
if len(buf) < icmpAllHeadersLength { if len(buf) < icmpAllHeadersLength {
return errSmallBuffer return errSmallBuffer
} }
@ -64,15 +64,17 @@ func (h ICMPHeader) Marshal(buf []byte) error {
buf[20] = uint8(h.Type) buf[20] = uint8(h.Type)
buf[21] = uint8(h.Code) buf[21] = uint8(h.Code)
h.IPHeader.Marshal(buf) h.IP4Header.Marshal(buf)
put16(buf[22:24], ipChecksum(buf)) put16(buf[22:24], ipChecksum(buf))
return nil return nil
} }
func (h *ICMPHeader) ToResponse() { func (h *ICMP4Header) ToResponse() {
h.Type = ICMPEchoReply // TODO: this doesn't implement ToResponse correctly, as it
h.Code = ICMPNoCode // assumes the ICMP request type.
h.IPHeader.ToResponse() h.Type = ICMP4EchoReply
h.Code = ICMP4NoCode
h.IP4Header.ToResponse()
} }

@ -11,61 +11,62 @@ import (
"inet.af/netaddr" "inet.af/netaddr"
) )
// IP is an IPv4 address. // IP4 is an IPv4 address.
type IP uint32 type IP4 uint32
// NewIP converts a standard library IP address into an IP. // NewIP converts a standard library IP address into an IP.
// It panics if b is not an IPv4 address. // It panics if b is not an IPv4 address.
func NewIP(b net.IP) IP { func NewIP4(b net.IP) IP4 {
b4 := b.To4() b4 := b.To4()
if b4 == nil { if b4 == nil {
panic(fmt.Sprintf("To4(%v) failed", b)) panic(fmt.Sprintf("To4(%v) failed", b))
} }
return IP(get32(b4)) return IP4(get32(b4))
} }
// IPFromNetaddr converts a netaddr.IP to an IP. // IPFromNetaddr converts a netaddr.IP to an IP.
func IPFromNetaddr(ip netaddr.IP) IP { func IP4FromNetaddr(ip netaddr.IP) IP4 {
ipbytes := ip.As4() ipbytes := ip.As4()
return IP(get32(ipbytes[:])) return IP4(get32(ipbytes[:]))
} }
// Netaddr converts an IP to a netaddr.IP. // Netaddr converts an IP to a netaddr.IP.
func (ip IP) Netaddr() netaddr.IP { func (ip IP4) Netaddr() netaddr.IP {
return netaddr.IPv4(byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)) return netaddr.IPv4(byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip))
} }
func (ip IP) String() string { func (ip IP4) String() string {
return fmt.Sprintf("%d.%d.%d.%d", byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)) return fmt.Sprintf("%d.%d.%d.%d", byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip))
} }
func (ip IP) IsMulticast() bool { func (ip IP4) IsMulticast() bool {
return byte(ip>>24)&0xf0 == 0xe0 return byte(ip>>24)&0xf0 == 0xe0
} }
func (ip IP) IsLinkLocalUnicast() bool { func (ip IP4) IsLinkLocalUnicast() bool {
return byte(ip>>24) == 169 && byte(ip>>16) == 254 return byte(ip>>24) == 169 && byte(ip>>16) == 254
} }
// IPProto is either a real IP protocol (ITCP, UDP, ...) or an special value like Unknown. // IP4Proto is either a real IP protocol (TCP, UDP, ...) or an special
// If it is a real IP protocol, its value corresponds to its IP protocol number. // value like Unknown. If it is a real IP protocol, its value
type IPProto uint8 // corresponds to its IP protocol number.
type IP4Proto uint8
const ( const (
// Unknown represents an unknown or unsupported protocol; it's deliberately the zero value. // Unknown represents an unknown or unsupported protocol; it's deliberately the zero value.
Unknown IPProto = 0x00 Unknown IP4Proto = 0x00
ICMP IPProto = 0x01 ICMP IP4Proto = 0x01
IGMP IPProto = 0x02 IGMP IP4Proto = 0x02
ICMPv6 IPProto = 0x3a ICMPv6 IP4Proto = 0x3a
TCP IPProto = 0x06 TCP IP4Proto = 0x06
UDP IPProto = 0x11 UDP IP4Proto = 0x11
// Fragment is a special value. It's not really an IPProto value // Fragment is a special value. It's not really an IPProto value
// so we're using the unassigned 0xFF value. // so we're using the unassigned 0xFF value.
// TODO(dmytro): special values should be taken out of here. // TODO(dmytro): special values should be taken out of here.
Fragment IPProto = 0xFF Fragment IP4Proto = 0xFF
) )
func (p IPProto) String() string { func (p IP4Proto) String() string {
switch p { switch p {
case Fragment: case Fragment:
return "Frag" return "Frag"
@ -81,20 +82,20 @@ func (p IPProto) String() string {
} }
// IPHeader represents an IP packet header. // IPHeader represents an IP packet header.
type IPHeader struct { type IP4Header struct {
IPProto IPProto IPProto IP4Proto
IPID uint16 IPID uint16
SrcIP IP SrcIP IP4
DstIP IP DstIP IP4
} }
const ipHeaderLength = 20 const ipHeaderLength = 20
func (IPHeader) Len() int { func (IP4Header) Len() int {
return ipHeaderLength return ipHeaderLength
} }
func (h IPHeader) Marshal(buf []byte) error { func (h IP4Header) Marshal(buf []byte) error {
if len(buf) < ipHeaderLength { if len(buf) < ipHeaderLength {
return errSmallBuffer return errSmallBuffer
} }
@ -118,11 +119,10 @@ func (h IPHeader) Marshal(buf []byte) error {
return nil return nil
} }
// MarshalPseudo serializes the header into buf in pseudo format. // MarshalPseudo serializes the header into buf in the "pseudo-header"
// It clobbers the header region, which is the first h.Length() bytes of buf. // form required when calculating UDP checksums. Overwrites the first
// It explicitly initializes every byte of the header region, // h.Length() bytes of buf.
// so pre-zeroing it on reuse is not required. It does not allocate memory. func (h IP4Header) MarshalPseudo(buf []byte) error {
func (h IPHeader) MarshalPseudo(buf []byte) error {
if len(buf) < ipHeaderLength { if len(buf) < ipHeaderLength {
return errSmallBuffer return errSmallBuffer
} }
@ -140,7 +140,8 @@ func (h IPHeader) MarshalPseudo(buf []byte) error {
return nil return nil
} }
func (h *IPHeader) ToResponse() { // ToResponse implements Header.
func (h *IP4Header) ToResponse() {
h.SrcIP, h.DstIP = h.DstIP, h.SrcIP h.SrcIP, h.DstIP = h.DstIP, h.SrcIP
// Flip the bits in the IPID. If incoming IPIDs are distinct, so are these. // Flip the bits in the IPID. If incoming IPIDs are distinct, so are these.
h.IPID = ^h.IPID h.IPID = ^h.IPID

@ -44,9 +44,9 @@ type ParsedPacket struct {
length int length int
IPVersion uint8 // 4, 6, or 0 IPVersion uint8 // 4, 6, or 0
IPProto IPProto // IP subprotocol (UDP, TCP, etc); the NextHeader field for IPv6 IPProto IP4Proto // IP subprotocol (UDP, TCP, etc); the NextHeader field for IPv6
SrcIP IP // IP source address (not used for IPv6) SrcIP IP4 // IP source address (not used for IPv6)
DstIP IP // IP destination address (not used for IPv6) DstIP IP4 // IP destination address (not used for IPv6)
SrcPort uint16 // TCP/UDP source port SrcPort uint16 // TCP/UDP source port
DstPort uint16 // TCP/UDP destination port DstPort uint16 // TCP/UDP destination port
TCPFlags uint8 // TCP flags (SYN, ACK, etc) TCPFlags uint8 // TCP flags (SYN, ACK, etc)
@ -73,7 +73,7 @@ func (p *ParsedPacket) String() string {
return sb.String() return sb.String()
} }
func writeIPPort(sb *strbuilder.Builder, ip IP, port uint16) { func writeIPPort(sb *strbuilder.Builder, ip IP4, port uint16) {
sb.WriteUint(uint64(byte(ip >> 24))) sb.WriteUint(uint64(byte(ip >> 24)))
sb.WriteByte('.') sb.WriteByte('.')
sb.WriteUint(uint64(byte(ip >> 16))) sb.WriteUint(uint64(byte(ip >> 16)))
@ -122,9 +122,9 @@ func (q *ParsedPacket) Decode(b []byte) {
q.IPVersion = (b[0] & 0xF0) >> 4 q.IPVersion = (b[0] & 0xF0) >> 4
switch q.IPVersion { switch q.IPVersion {
case 4: case 4:
q.IPProto = IPProto(b[9]) q.IPProto = IP4Proto(b[9])
case 6: case 6:
q.IPProto = IPProto(b[6]) // "Next Header" field q.IPProto = IP4Proto(b[6]) // "Next Header" field
return return
default: default:
q.IPVersion = 0 q.IPVersion = 0
@ -140,8 +140,8 @@ func (q *ParsedPacket) Decode(b []byte) {
} }
// If it's valid IPv4, then the IP addresses are valid // If it's valid IPv4, then the IP addresses are valid
q.SrcIP = IP(get32(b[12:16])) q.SrcIP = IP4(get32(b[12:16]))
q.DstIP = IP(get32(b[16:20])) q.DstIP = IP4(get32(b[16:20]))
q.subofs = int((b[0] & 0x0F) << 2) q.subofs = int((b[0] & 0x0F) << 2)
sub := b[q.subofs:] sub := b[q.subofs:]
@ -224,9 +224,9 @@ func (q *ParsedPacket) Decode(b []byte) {
} }
} }
func (q *ParsedPacket) IPHeader() IPHeader { func (q *ParsedPacket) IPHeader() IP4Header {
ipid := get16(q.b[4:6]) ipid := get16(q.b[4:6])
return IPHeader{ return IP4Header{
IPID: ipid, IPID: ipid,
IPProto: q.IPProto, IPProto: q.IPProto,
SrcIP: q.SrcIP, SrcIP: q.SrcIP,
@ -234,17 +234,17 @@ func (q *ParsedPacket) IPHeader() IPHeader {
} }
} }
func (q *ParsedPacket) ICMPHeader() ICMPHeader { func (q *ParsedPacket) ICMPHeader() ICMP4Header {
return ICMPHeader{ return ICMP4Header{
IPHeader: q.IPHeader(), IP4Header: q.IPHeader(),
Type: ICMPType(q.b[q.subofs+0]), Type: ICMP4Type(q.b[q.subofs+0]),
Code: ICMPCode(q.b[q.subofs+1]), Code: ICMP4Code(q.b[q.subofs+1]),
} }
} }
func (q *ParsedPacket) UDPHeader() UDPHeader { func (q *ParsedPacket) UDPHeader() UDP4Header {
return UDPHeader{ return UDP4Header{
IPHeader: q.IPHeader(), IP4Header: q.IPHeader(),
SrcPort: q.SrcPort, SrcPort: q.SrcPort,
DstPort: q.DstPort, DstPort: q.DstPort,
} }
@ -284,8 +284,8 @@ func (q *ParsedPacket) IsTCPSyn() bool {
// IsError reports whether q is an IPv4 ICMP "Error" packet. // IsError reports whether q is an IPv4 ICMP "Error" packet.
func (q *ParsedPacket) IsError() bool { func (q *ParsedPacket) IsError() bool {
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 { if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
switch ICMPType(q.b[q.subofs]) { switch ICMP4Type(q.b[q.subofs]) {
case ICMPUnreachable, ICMPTimeExceeded: case ICMP4Unreachable, ICMP4TimeExceeded:
return true return true
} }
} }
@ -295,8 +295,8 @@ func (q *ParsedPacket) IsError() bool {
// IsEchoRequest reports whether q is an IPv4 ICMP Echo Request. // IsEchoRequest reports whether q is an IPv4 ICMP Echo Request.
func (q *ParsedPacket) IsEchoRequest() bool { func (q *ParsedPacket) IsEchoRequest() bool {
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 { if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
return ICMPType(q.b[q.subofs]) == ICMPEchoRequest && return ICMP4Type(q.b[q.subofs]) == ICMP4EchoRequest &&
ICMPCode(q.b[q.subofs+1]) == ICMPNoCode ICMP4Code(q.b[q.subofs+1]) == ICMP4NoCode
} }
return false return false
} }
@ -304,8 +304,8 @@ func (q *ParsedPacket) IsEchoRequest() bool {
// IsEchoRequest reports whether q is an IPv4 ICMP Echo Response. // IsEchoRequest reports whether q is an IPv4 ICMP Echo Response.
func (q *ParsedPacket) IsEchoResponse() bool { func (q *ParsedPacket) IsEchoResponse() bool {
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 { if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
return ICMPType(q.b[q.subofs]) == ICMPEchoReply && return ICMP4Type(q.b[q.subofs]) == ICMP4EchoReply &&
ICMPCode(q.b[q.subofs+1]) == ICMPNoCode ICMP4Code(q.b[q.subofs+1]) == ICMP4NoCode
} }
return false return false
} }

@ -11,9 +11,9 @@ import (
"testing" "testing"
) )
func TestIPString(t *testing.T) { func TestIP4String(t *testing.T) {
const str = "1.2.3.4" const str = "1.2.3.4"
ip := NewIP(net.ParseIP(str)) ip := NewIP4(net.ParseIP(str))
var got string var got string
allocs := testing.AllocsPerRun(1000, func() { allocs := testing.AllocsPerRun(1000, func() {
@ -49,8 +49,8 @@ var icmpRequestDecode = ParsedPacket{
IPVersion: 4, IPVersion: 4,
IPProto: ICMP, IPProto: ICMP,
SrcIP: NewIP(net.ParseIP("1.2.3.4")), SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP(net.ParseIP("5.6.7.8")), DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 0, SrcPort: 0,
DstPort: 0, DstPort: 0,
} }
@ -75,8 +75,8 @@ var icmpReplyDecode = ParsedPacket{
IPVersion: 4, IPVersion: 4,
IPProto: ICMP, IPProto: ICMP,
SrcIP: NewIP(net.ParseIP("1.2.3.4")), SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP(net.ParseIP("5.6.7.8")), DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 0, SrcPort: 0,
DstPort: 0, DstPort: 0,
} }
@ -131,8 +131,8 @@ var tcpPacketDecode = ParsedPacket{
IPVersion: 4, IPVersion: 4,
IPProto: TCP, IPProto: TCP,
SrcIP: NewIP(net.ParseIP("1.2.3.4")), SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP(net.ParseIP("5.6.7.8")), DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 123, SrcPort: 123,
DstPort: 567, DstPort: 567,
TCPFlags: TCPSynAck, TCPFlags: TCPSynAck,
@ -159,8 +159,8 @@ var udpRequestDecode = ParsedPacket{
IPVersion: 4, IPVersion: 4,
IPProto: UDP, IPProto: UDP,
SrcIP: NewIP(net.ParseIP("1.2.3.4")), SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP(net.ParseIP("5.6.7.8")), DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 123, SrcPort: 123,
DstPort: 567, DstPort: 567,
} }
@ -185,8 +185,8 @@ var udpReplyDecode = ParsedPacket{
length: len(udpReplyBuffer), length: len(udpReplyBuffer),
IPProto: UDP, IPProto: UDP,
SrcIP: NewIP(net.ParseIP("1.2.3.4")), SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP(net.ParseIP("5.6.7.8")), DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 567, SrcPort: 567,
DstPort: 123, DstPort: 123,
} }

@ -5,8 +5,8 @@
package packet package packet
// UDPHeader represents an UDP packet header. // UDPHeader represents an UDP packet header.
type UDPHeader struct { type UDP4Header struct {
IPHeader IP4Header
SrcPort uint16 SrcPort uint16
DstPort uint16 DstPort uint16
} }
@ -17,11 +17,11 @@ const (
udpTotalHeaderLength = ipHeaderLength + udpHeaderLength udpTotalHeaderLength = ipHeaderLength + udpHeaderLength
) )
func (UDPHeader) Len() int { func (UDP4Header) Len() int {
return udpTotalHeaderLength return udpTotalHeaderLength
} }
func (h UDPHeader) Marshal(buf []byte) error { func (h UDP4Header) Marshal(buf []byte) error {
if len(buf) < udpTotalHeaderLength { if len(buf) < udpTotalHeaderLength {
return errSmallBuffer return errSmallBuffer
} }
@ -31,23 +31,23 @@ func (h UDPHeader) Marshal(buf []byte) error {
// The caller does not need to set this. // The caller does not need to set this.
h.IPProto = UDP h.IPProto = UDP
length := len(buf) - h.IPHeader.Len() length := len(buf) - h.IP4Header.Len()
put16(buf[20:22], h.SrcPort) put16(buf[20:22], h.SrcPort)
put16(buf[22:24], h.DstPort) put16(buf[22:24], h.DstPort)
put16(buf[24:26], uint16(length)) put16(buf[24:26], uint16(length))
put16(buf[26:28], 0) // blank checksum put16(buf[26:28], 0) // blank checksum
h.IPHeader.MarshalPseudo(buf) h.IP4Header.MarshalPseudo(buf)
// UDP checksum with IP pseudo header. // UDP checksum with IP pseudo header.
put16(buf[26:28], ipChecksum(buf[8:])) put16(buf[26:28], ipChecksum(buf[8:]))
h.IPHeader.Marshal(buf) h.IP4Header.Marshal(buf)
return nil return nil
} }
func (h *UDPHeader) ToResponse() { func (h *UDP4Header) ToResponse() {
h.SrcPort, h.DstPort = h.DstPort, h.SrcPort h.SrcPort, h.DstPort = h.DstPort, h.SrcPort
h.IPHeader.ToResponse() h.IP4Header.ToResponse()
} }

@ -136,7 +136,7 @@ func WrapTUN(logf logger.Logf, tdev tun.Device) *TUN {
// destination (the map keys). // destination (the map keys).
// //
// The map ownership passes to the TUN. It must be non-nil. // The map ownership passes to the TUN. It must be non-nil.
func (t *TUN) SetDestIPActivityFuncs(m map[packet.IP]func()) { func (t *TUN) SetDestIPActivityFuncs(m map[packet.IP4]func()) {
t.destIPActivity.Store(m) t.destIPActivity.Store(m)
} }
@ -282,7 +282,7 @@ func (t *TUN) Read(buf []byte, offset int) (int, error) {
defer parsedPacketPool.Put(p) defer parsedPacketPool.Put(p)
p.Decode(buf[offset : offset+n]) p.Decode(buf[offset : offset+n])
if m, ok := t.destIPActivity.Load().(map[packet.IP]func()); ok { if m, ok := t.destIPActivity.Load().(map[packet.IP4]func()); ok {
if fn := m[p.DstIP]; fn != nil { if fn := m[p.DstIP]; fn != nil {
fn() fn()
} }

@ -16,9 +16,9 @@ import (
"tailscale.com/wgengine/packet" "tailscale.com/wgengine/packet"
) )
func udp(src, dst packet.IP, sport, dport uint16) []byte { func udp(src, dst packet.IP4, sport, dport uint16) []byte {
header := &packet.UDPHeader{ header := &packet.UDP4Header{
IPHeader: packet.IPHeader{ IP4Header: packet.IP4Header{
SrcIP: src, SrcIP: src,
DstIP: dst, DstIP: dst,
IPID: 0, IPID: 0,
@ -29,11 +29,11 @@ func udp(src, dst packet.IP, sport, dport uint16) []byte {
return packet.Generate(header, []byte("udp_payload")) return packet.Generate(header, []byte("udp_payload"))
} }
func filterNet(ip, mask packet.IP) filter.Net { func filterNet(ip, mask packet.IP4) filter.Net {
return filter.Net{IP: ip, Mask: mask} return filter.Net{IP: ip, Mask: mask}
} }
func nets(ips []packet.IP) []filter.Net { func nets(ips []packet.IP4) []filter.Net {
out := make([]filter.Net, 0, len(ips)) out := make([]filter.Net, 0, len(ips))
for _, ip := range ips { for _, ip := range ips {
out = append(out, filterNet(ip, filter.Netmask(32))) out = append(out, filterNet(ip, filter.Netmask(32)))
@ -41,7 +41,7 @@ func nets(ips []packet.IP) []filter.Net {
return out return out
} }
func ippr(ip packet.IP, start, end uint16) []filter.NetPortRange { func ippr(ip packet.IP4, start, end uint16) []filter.NetPortRange {
return []filter.NetPortRange{ return []filter.NetPortRange{
filter.NetPortRange{ filter.NetPortRange{
Net: filterNet(ip, filter.Netmask(32)), Net: filterNet(ip, filter.Netmask(32)),
@ -52,11 +52,11 @@ func ippr(ip packet.IP, start, end uint16) []filter.NetPortRange {
func setfilter(logf logger.Logf, tun *TUN) { func setfilter(logf logger.Logf, tun *TUN) {
matches := filter.Matches{ matches := filter.Matches{
{Srcs: nets([]packet.IP{0x05060708}), Dsts: ippr(0x01020304, 89, 90)}, {Srcs: nets([]packet.IP4{0x05060708}), Dsts: ippr(0x01020304, 89, 90)},
{Srcs: nets([]packet.IP{0x01020304}), Dsts: ippr(0x05060708, 98, 98)}, {Srcs: nets([]packet.IP4{0x01020304}), Dsts: ippr(0x05060708, 98, 98)},
} }
localNets := []filter.Net{ localNets := []filter.Net{
filterNet(packet.IP(0x01020304), filter.Netmask(16)), filterNet(packet.IP4(0x01020304), filter.Netmask(16)),
} }
tun.SetFilter(filter.New(matches, localNets, nil, logf)) tun.SetFilter(filter.New(matches, localNets, nil, logf))
} }

@ -99,7 +99,7 @@ type userspaceEngine struct {
// localAddrs is the set of IP addresses assigned to the local // localAddrs is the set of IP addresses assigned to the local
// tunnel interface. It's used to reflect local packets // tunnel interface. It's used to reflect local packets
// incorrectly sent to us. // incorrectly sent to us.
localAddrs atomic.Value // of map[packet.IP]bool localAddrs atomic.Value // of map[packet.IP4]bool
wgLock sync.Mutex // serializes all wgdev operations; see lock order comment below wgLock sync.Mutex // serializes all wgdev operations; see lock order comment below
lastCfgFull wgcfg.Config lastCfgFull wgcfg.Config
@ -108,8 +108,8 @@ type userspaceEngine struct {
lastEngineSigTrim string // of trimmed wireguard config lastEngineSigTrim string // of trimmed wireguard config
recvActivityAt map[tailcfg.DiscoKey]time.Time recvActivityAt map[tailcfg.DiscoKey]time.Time
trimmedDisco map[tailcfg.DiscoKey]bool // set of disco keys of peers currently excluded from wireguard config trimmedDisco map[tailcfg.DiscoKey]bool // set of disco keys of peers currently excluded from wireguard config
sentActivityAt map[packet.IP]*int64 // value is atomic int64 of unixtime sentActivityAt map[packet.IP4]*int64 // value is atomic int64 of unixtime
destIPActivityFuncs map[packet.IP]func() destIPActivityFuncs map[packet.IP4]func()
mu sync.Mutex // guards following; see lock order comment below mu sync.Mutex // guards following; see lock order comment below
closing bool // Close was called (even if we're still closing) closing bool // Close was called (even if we're still closing)
@ -206,7 +206,7 @@ func newUserspaceEngineAdvanced(conf EngineConfig) (_ Engine, reterr error) {
resolver: tsdns.NewResolver(rconf), resolver: tsdns.NewResolver(rconf),
pingers: make(map[wgcfg.Key]*pinger), pingers: make(map[wgcfg.Key]*pinger),
} }
e.localAddrs.Store(map[packet.IP]bool{}) e.localAddrs.Store(map[packet.IP4]bool{})
e.linkState, _ = getLinkState() e.linkState, _ = getLinkState()
logf("link state: %+v", e.linkState) logf("link state: %+v", e.linkState)
@ -410,8 +410,8 @@ func (e *userspaceEngine) handleLocalPackets(p *packet.ParsedPacket, t *tstun.TU
return filter.Accept return filter.Accept
} }
func (e *userspaceEngine) isLocalAddr(ip packet.IP) bool { func (e *userspaceEngine) isLocalAddr(ip packet.IP4) bool {
localAddrs, ok := e.localAddrs.Load().(map[packet.IP]bool) localAddrs, ok := e.localAddrs.Load().(map[packet.IP4]bool)
if !ok { if !ok {
e.logf("[unexpected] e.localAddrs was nil, can't check for loopback packet") e.logf("[unexpected] e.localAddrs was nil, can't check for loopback packet")
return false return false
@ -447,10 +447,10 @@ func (e *userspaceEngine) pollResolver() {
continue continue
} }
h := packet.UDPHeader{ h := packet.UDP4Header{
IPHeader: packet.IPHeader{ IP4Header: packet.IP4Header{
SrcIP: packet.IP(magicDNSIP), SrcIP: packet.IP4(magicDNSIP),
DstIP: packet.IPFromNetaddr(resp.Addr.IP), DstIP: packet.IP4FromNetaddr(resp.Addr.IP),
}, },
SrcPort: magicDNSPort, SrcPort: magicDNSPort,
DstPort: resp.Addr.Port, DstPort: resp.Addr.Port,
@ -487,7 +487,7 @@ func (p *pinger) close() {
<-p.done <-p.done
} }
func (p *pinger) run(ctx context.Context, peerKey wgcfg.Key, ips []wgcfg.IP, srcIP packet.IP) { func (p *pinger) run(ctx context.Context, peerKey wgcfg.Key, ips []wgcfg.IP, srcIP packet.IP4) {
defer func() { defer func() {
p.e.mu.Lock() p.e.mu.Lock()
if p.e.pingers[peerKey] == p { if p.e.pingers[peerKey] == p {
@ -498,12 +498,12 @@ func (p *pinger) run(ctx context.Context, peerKey wgcfg.Key, ips []wgcfg.IP, src
close(p.done) close(p.done)
}() }()
header := packet.ICMPHeader{ header := packet.ICMP4Header{
IPHeader: packet.IPHeader{ IP4Header: packet.IP4Header{
SrcIP: srcIP, SrcIP: srcIP,
}, },
Type: packet.ICMPEchoRequest, Type: packet.ICMP4EchoRequest,
Code: packet.ICMPNoCode, Code: packet.ICMP4NoCode,
} }
// sendFreq is slightly longer than sprayFreq in magicsock to ensure // sendFreq is slightly longer than sprayFreq in magicsock to ensure
@ -513,9 +513,9 @@ func (p *pinger) run(ctx context.Context, peerKey wgcfg.Key, ips []wgcfg.IP, src
const stopAfter = 3 * time.Second const stopAfter = 3 * time.Second
start := time.Now() start := time.Now()
var dstIPs []packet.IP var dstIPs []packet.IP4
for _, ip := range ips { for _, ip := range ips {
dstIPs = append(dstIPs, packet.NewIP(ip.IP())) dstIPs = append(dstIPs, packet.NewIP4(ip.IP()))
} }
payload := []byte("magicsock_spray") // no meaning payload := []byte("magicsock_spray") // no meaning
@ -551,11 +551,11 @@ func (p *pinger) run(ctx context.Context, peerKey wgcfg.Key, ips []wgcfg.IP, src
// have advertised discovery keys. // have advertised discovery keys.
func (e *userspaceEngine) pinger(peerKey wgcfg.Key, ips []wgcfg.IP) { func (e *userspaceEngine) pinger(peerKey wgcfg.Key, ips []wgcfg.IP) {
e.logf("generating initial ping traffic to %s (%v)", peerKey.ShortString(), ips) e.logf("generating initial ping traffic to %s (%v)", peerKey.ShortString(), ips)
var srcIP packet.IP var srcIP packet.IP4
e.wgLock.Lock() e.wgLock.Lock()
if len(e.lastCfgFull.Addresses) > 0 { if len(e.lastCfgFull.Addresses) > 0 {
srcIP = packet.NewIP(e.lastCfgFull.Addresses[0].IP.IP()) srcIP = packet.NewIP4(e.lastCfgFull.Addresses[0].IP.IP())
} }
e.wgLock.Unlock() e.wgLock.Unlock()
@ -681,7 +681,7 @@ func (e *userspaceEngine) isActiveSince(dk tailcfg.DiscoKey, ip wgcfg.IP, t time
if e.recvActivityAt[dk].After(t) { if e.recvActivityAt[dk].After(t) {
return true return true
} }
pip := packet.IP(binary.BigEndian.Uint32(ip.Addr[12:])) pip := packet.IP4(binary.BigEndian.Uint32(ip.Addr[12:]))
timePtr, ok := e.sentActivityAt[pip] timePtr, ok := e.sentActivityAt[pip]
if !ok { if !ok {
return false return false
@ -792,12 +792,12 @@ func (e *userspaceEngine) updateActivityMapsLocked(trackDisco []tailcfg.DiscoKey
e.recvActivityAt = mr e.recvActivityAt = mr
oldTime := e.sentActivityAt oldTime := e.sentActivityAt
e.sentActivityAt = make(map[packet.IP]*int64, len(oldTime)) e.sentActivityAt = make(map[packet.IP4]*int64, len(oldTime))
oldFunc := e.destIPActivityFuncs oldFunc := e.destIPActivityFuncs
e.destIPActivityFuncs = make(map[packet.IP]func(), len(oldFunc)) e.destIPActivityFuncs = make(map[packet.IP4]func(), len(oldFunc))
for _, wip := range trackIPs { for _, wip := range trackIPs {
pip := packet.IP(binary.BigEndian.Uint32(wip.Addr[12:])) pip := packet.IP4(binary.BigEndian.Uint32(wip.Addr[12:]))
timePtr := oldTime[pip] timePtr := oldTime[pip]
if timePtr == nil { if timePtr == nil {
timePtr = new(int64) timePtr = new(int64)
@ -837,13 +837,13 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config)
panic("routerCfg must not be nil") panic("routerCfg must not be nil")
} }
localAddrs := map[packet.IP]bool{} localAddrs := map[packet.IP4]bool{}
for _, addr := range routerCfg.LocalAddrs { for _, addr := range routerCfg.LocalAddrs {
// TODO: ipv6 // TODO: ipv6
if !addr.IP.Is4() { if !addr.IP.Is4() {
continue continue
} }
localAddrs[packet.IPFromNetaddr(addr.IP)] = true localAddrs[packet.IP4FromNetaddr(addr.IP)] = true
} }
e.localAddrs.Store(localAddrs) e.localAddrs.Store(localAddrs)

Loading…
Cancel
Save