mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
feature/conn25: match the packet filter's protocol rules in grant enforcement
Rework protoPortRangesAllow into a port of filter.Filter.RunIn's protocol switch, taking the parsed packet rather than a protocol and a port. A packet addressed to a transit IP never reaches RunIn's own switch, since a connector does not advertise its transit IPs and runIn4 returns early at its local4 check, so reproducing the switch in the hook keeps a conn25 app grant behaving like the equivalent packet filter rule. This reverses a property of the previous commit: the check no longer sees every packet. It inherits RunIn's leniency, admitting ICMP responses and anything that is not a lone TCP SYN regardless of what the grant says. Unlike RunIn, what is admitted here is then DNATed on to the app, so a peer can reach a port its grant does not name with anything that is not a SYN. Give each hook and each matching rule its own reason string rather than sharing one. The filter logs the reason next to the packet, so a conn25 verdict now says which rule admitted it, both apart from the rest of the filter's verdicts and apart from each other. Store the grant's ranges as a views.Slice, with a TODO to decode them once per peer and app instead of once per transit IP registration. Updates tailscale/corp#49815 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
c14f7d9558
commit
0ae2c7a0f9
7 files changed
+235
-90
No files matched your search
@@ -171,7 +171,7 @@ func (c *connector) getActiveState() appctype.Conn25ConnectorState {
|
||||
for _, transitIP := range slices.SortedFunc(maps.Keys(transitToAddr), netip.Addr.Compare) {
|
||||
addr := transitToAddr[transitIP]
|
||||
var ports []string
|
||||
for _, p := range addr.ports {
|
||||
for _, p := range addr.ports.All() {
|
||||
ports = append(ports, p.String())
|
||||
}
|
||||
apiAddr := appctype.Conn25ConnectorAddressState{
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"tailscale.com/types/appctype"
|
||||
"tailscale.com/types/ipproto"
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/types/views"
|
||||
"tailscale.com/util/dnsname"
|
||||
"tailscale.com/util/must"
|
||||
)
|
||||
@@ -66,10 +67,10 @@ func TestGetActiveState(t *testing.T) {
|
||||
netip.MustParseAddr("11.0.0.1"): {
|
||||
// A mapping whose grant restricts the protocols and ports it
|
||||
// may carry; the others were granted nothing and carry none.
|
||||
netip.MustParseAddr("169.254.0.5"): {app: "zapp", addr: netip.MustParseAddr("10.0.0.5"), ports: []tailcfg.ProtoPortRange{
|
||||
netip.MustParseAddr("169.254.0.5"): {app: "zapp", addr: netip.MustParseAddr("10.0.0.5"), ports: views.SliceOf([]tailcfg.ProtoPortRange{
|
||||
{Proto: int(ipproto.TCP), Ports: tailcfg.PortRange{First: 443, Last: 443}},
|
||||
{Ports: tailcfg.PortRangeAny},
|
||||
}},
|
||||
})},
|
||||
netip.MustParseAddr("169.254.0.6"): {app: "app1", addr: netip.MustParseAddr("10.0.0.6")},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func (c *client) transitIPRoutableLocked(as *addrs) bool {
|
||||
func (c *client) linkLocalAllow(p packet.Parsed) (bool, string) {
|
||||
ok := c.isKnownTransitIP(p.Dst.Addr())
|
||||
if ok {
|
||||
return true, packetFilterAllowReason
|
||||
return true, clientEgressReason
|
||||
}
|
||||
return false, ""
|
||||
}
|
||||
|
||||
+98
-32
@@ -637,6 +637,10 @@ func (c *Conn25) handleConnectorTransitIPRequest(n tailcfg.NodeView, peerCaps ta
|
||||
// The grant that gives the peer access to the app also carries the
|
||||
// protocols and ports it may use, which get stored with the mapping
|
||||
// and enforced on every packet by [connector.ingressAllow].
|
||||
//
|
||||
// TODO(mzb): cache this per peer+app so that every transit IP a peer
|
||||
// registers for an app shares one [views.Slice], rather than each
|
||||
// registration decoding and storing its own copy.
|
||||
ports, hasCap, err := appProtoPortRanges(peerCaps, each.App)
|
||||
if !hasCap {
|
||||
resp.TransitIPs = append(resp.TransitIPs, TransitIPResponse{
|
||||
@@ -682,50 +686,87 @@ func (c *Conn25) handleConnectorTransitIPRequest(n tailcfg.NodeView, peerCaps ta
|
||||
// comes back as an empty slice with hasCap true: the app stays resolvable and
|
||||
// registrable, which keeps the misconfiguration diagnosable, but
|
||||
// [connector.ingressAllow] lets nothing through.
|
||||
func appProtoPortRanges(peerCaps tailcfg.PeerCapMap, app string) (ranges []tailcfg.ProtoPortRange, hasCap bool, err error) {
|
||||
func appProtoPortRanges(peerCaps tailcfg.PeerCapMap, app string) (ranges views.Slice[tailcfg.ProtoPortRange], hasCap bool, err error) {
|
||||
cap := peercap.Conn25Prefix.ToAttribute(app)
|
||||
if !peerCaps.HasCapability(cap) {
|
||||
return nil, false, nil
|
||||
return ranges, false, nil
|
||||
}
|
||||
ranges, err = tailcfg.UnmarshalCapJSON[tailcfg.ProtoPortRange](peerCaps, cap)
|
||||
rs, err := tailcfg.UnmarshalCapJSON[tailcfg.ProtoPortRange](peerCaps, cap)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
return ranges, true, err
|
||||
}
|
||||
return ranges, true, nil
|
||||
return views.SliceOf(rs), true, nil
|
||||
}
|
||||
|
||||
// protolessGrantProtos is the set of protocols a [tailcfg.ProtoPortRange] with
|
||||
// no protocol (e.g. "*" or "80") covers. It is the same set the packet filter
|
||||
// uses for a [tailcfg.FilterRule] that names no protocol, and the set
|
||||
// [tailcfg.ProtoPortRange] documents for a zero Proto.
|
||||
var protolessGrantProtos = []ipproto.Proto{ipproto.TCP, ipproto.UDP, ipproto.ICMPv4, ipproto.ICMPv6}
|
||||
|
||||
// protoPortRangesAllow reports whether ranges, the protocol and port
|
||||
// combinations a peer was granted for an app, permit a packet of protocol
|
||||
// proto addressed to port.
|
||||
// combinations a peer was granted for an app, permit p, and if so the reason
|
||||
// to log alongside the allow verdict.
|
||||
//
|
||||
// ICMP carries no port, so it is permitted whenever the peer was granted
|
||||
// anything at all for the app. That mirrors the packet filter, which allows
|
||||
// ICMP to any destination the source may reach on any port by any protocol
|
||||
// (see matchIPsOnly in [tailscale.com/wgengine/filter]), so a "tcp:443" grant
|
||||
// does not break ping.
|
||||
func protoPortRangesAllow(ranges []tailcfg.ProtoPortRange, proto ipproto.Proto, port uint16) bool {
|
||||
if proto == ipproto.ICMPv4 || proto == ipproto.ICMPv6 {
|
||||
return len(ranges) > 0
|
||||
}
|
||||
for _, r := range ranges {
|
||||
if r.Proto == 0 {
|
||||
if !slices.Contains(protolessGrantProtos, proto) {
|
||||
continue
|
||||
// It is modelled on [tailscale.com/wgengine/filter.Filter.RunIn], whose own
|
||||
// protocol switch a packet addressed to a transit IP never reaches: a
|
||||
// connector does not advertise its transit IPs, so runIn4 returns early at its
|
||||
// local4 check. Reproducing the switch here keeps a conn25 app grant behaving
|
||||
// like the equivalent packet filter rule, at the cost of inheriting RunIn's
|
||||
// leniency towards non-SYN TCP and ICMP responses.
|
||||
func protoPortRangesAllow(ranges views.Slice[tailcfg.ProtoPortRange], p packet.Parsed) (bool, string) {
|
||||
switch p.IPProto {
|
||||
case ipproto.ICMPv4, ipproto.ICMPv6:
|
||||
var want ipproto.Proto
|
||||
switch p.IPVersion {
|
||||
case 4:
|
||||
want = ipproto.ICMPv4
|
||||
case 6:
|
||||
want = ipproto.ICMPv6
|
||||
}
|
||||
if p.IPProto != want {
|
||||
// A diversion from RunIn, which has a separate switch per IP
|
||||
// version and so lets an ICMP type that does not match the
|
||||
// packet's IP version fall through to its default case. Refusing
|
||||
// it outright seems unlikely to matter in practice.
|
||||
return false, ""
|
||||
}
|
||||
if p.IsEchoResponse() || p.IsError() {
|
||||
// ICMP responses are allowed.
|
||||
return true, connectorICMPResponseReason
|
||||
}
|
||||
if ranges.Len() > 0 {
|
||||
// If any port is open to an IP, allow ICMP to it.
|
||||
return true, connectorICMPReason
|
||||
}
|
||||
case ipproto.TCP:
|
||||
// As in RunIn, non-SYN packets are allowed so that return traffic for
|
||||
// outgoing connections arrives; a new incoming session still cannot be
|
||||
// initiated without a SYN. Unlike RunIn, what is admitted here is then
|
||||
// DNATed on to the app, so this also lets a peer reach a port of the
|
||||
// app its grant does not name with anything that is not a SYN.
|
||||
if !p.IsTCPSyn() {
|
||||
return true, connectorTCPNonSynReason
|
||||
}
|
||||
for _, ppr := range ranges.All() {
|
||||
if (ppr.Proto == 0 || ppr.Proto == int(ipproto.TCP)) &&
|
||||
ppr.Ports.Contains(p.Dst.Port()) {
|
||||
return true, connectorTCPReason
|
||||
}
|
||||
} else if ipproto.Proto(r.Proto) != proto {
|
||||
continue
|
||||
}
|
||||
if port >= r.Ports.First && port <= r.Ports.Last {
|
||||
return true
|
||||
case ipproto.UDP, ipproto.SCTP:
|
||||
// A range naming no protocol covers UDP but not SCTP, matching the set
|
||||
// the packet filter uses for a [tailcfg.FilterRule] that names none.
|
||||
allowProtoless := p.IPProto == ipproto.UDP
|
||||
for _, ppr := range ranges.All() {
|
||||
if ((allowProtoless && ppr.Proto == 0) || ppr.Proto == int(p.IPProto)) &&
|
||||
ppr.Ports.Contains(p.Dst.Port()) {
|
||||
return true, connectorPortReason
|
||||
}
|
||||
}
|
||||
default:
|
||||
// Protocols that carry no port have to be granted in full.
|
||||
for _, ppr := range ranges.All() {
|
||||
if ppr.Proto == int(p.IPProto) && ppr.Ports == tailcfg.PortRangeAny {
|
||||
return true, connectorPortlessReason
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
return false, ""
|
||||
}
|
||||
|
||||
// TransitIPRequest details a single TransitIP allocation request from a client to a
|
||||
@@ -1465,7 +1506,32 @@ func rewriteHTTPSResponse(hdr dnsmessage.Header, question dnsmessage.Question, p
|
||||
return b.Finish()
|
||||
}
|
||||
|
||||
const packetFilterAllowReason = "app connector transit IP"
|
||||
// Reasons accompanying an allow verdict from the conn25 packet filter hooks.
|
||||
// The filter logs them next to the packet, so each one names the rule that
|
||||
// admitted it, both to tell conn25's verdicts apart from the rest of the
|
||||
// filter's and to tell them apart from each other.
|
||||
const (
|
||||
// clientEgressReason is returned for a packet the client is sending to a
|
||||
// transit IP that has been assigned to it.
|
||||
clientEgressReason = "app connector egress to transit IP"
|
||||
// connectorTransitIPReason is returned for a packet addressed to a transit
|
||||
// IP in one of this connector's pools, exempting it from the filter's
|
||||
// blanket drop of link-local destinations.
|
||||
connectorTransitIPReason = "app connector transit IP"
|
||||
// connectorUnmappedReason is returned for a packet addressed to a transit
|
||||
// IP the peer has no mapping registered for. See [connector.ingressAllow]
|
||||
// for why those are admitted.
|
||||
connectorUnmappedReason = "app connector unregistered transit IP"
|
||||
// The remaining reasons are returned by [protoPortRangesAllow], and are
|
||||
// named after the [tailscale.com/wgengine/filter.Filter.RunIn] verdicts
|
||||
// they correspond to.
|
||||
connectorICMPResponseReason = "app connector icmp response ok"
|
||||
connectorICMPReason = "app connector icmp ok"
|
||||
connectorTCPNonSynReason = "app connector tcp non-syn"
|
||||
connectorTCPReason = "app connector tcp ok"
|
||||
connectorPortReason = "app connector ok"
|
||||
connectorPortlessReason = "app connector other-portless ok"
|
||||
)
|
||||
|
||||
func (e *extension) getRouteCheckReport() ipnlocal.RouteCheckReport {
|
||||
b, ok := e.backend.(*ipnlocal.LocalBackend)
|
||||
|
||||
@@ -31,6 +31,7 @@ import (
|
||||
"tailscale.com/net/dns"
|
||||
"tailscale.com/net/dns/resolver"
|
||||
"tailscale.com/net/netmon"
|
||||
"tailscale.com/net/packet"
|
||||
"tailscale.com/net/routecheck"
|
||||
"tailscale.com/net/tsdial"
|
||||
"tailscale.com/net/tstun"
|
||||
@@ -45,6 +46,7 @@ import (
|
||||
"tailscale.com/types/key"
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/types/opt"
|
||||
"tailscale.com/types/views"
|
||||
"tailscale.com/util/dnsname"
|
||||
"tailscale.com/util/eventbus/eventbustest"
|
||||
"tailscale.com/util/must"
|
||||
@@ -4850,61 +4852,114 @@ func TestMintTokensForDNSResponse(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProtoPortRangesAllow(t *testing.T) {
|
||||
ppr := func(s string) tailcfg.ProtoPortRange {
|
||||
var p tailcfg.ProtoPortRange
|
||||
if err := p.UnmarshalText([]byte(s)); err != nil {
|
||||
t.Fatalf("parsing %q: %v", s, err)
|
||||
// ppr builds the grant values a peer holds for an app.
|
||||
ppr := func(ss ...string) views.Slice[tailcfg.ProtoPortRange] {
|
||||
var out []tailcfg.ProtoPortRange
|
||||
for _, s := range ss {
|
||||
var p tailcfg.ProtoPortRange
|
||||
if err := p.UnmarshalText([]byte(s)); err != nil {
|
||||
t.Fatalf("parsing %q: %v", s, err)
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return views.SliceOf(out)
|
||||
}
|
||||
|
||||
src := netip.MustParseAddr("100.64.0.1")
|
||||
dst := netip.MustParseAddr("169.254.0.1")
|
||||
|
||||
pkt := func(ver uint8, proto ipproto.Proto, port uint16, flags packet.TCPFlag) packet.Parsed {
|
||||
return packet.Parsed{
|
||||
IPVersion: ver,
|
||||
IPProto: proto,
|
||||
Src: netip.AddrPortFrom(src, 12345),
|
||||
Dst: netip.AddrPortFrom(dst, port),
|
||||
TCPFlags: flags,
|
||||
}
|
||||
}
|
||||
tcpSyn := func(port uint16) packet.Parsed { return pkt(4, ipproto.TCP, port, packet.TCPSyn) }
|
||||
tcp := func(port uint16, flags packet.TCPFlag) packet.Parsed { return pkt(4, ipproto.TCP, port, flags) }
|
||||
udp := func(port uint16) packet.Parsed { return pkt(4, ipproto.UDP, port, 0) }
|
||||
sctp := func(port uint16) packet.Parsed { return pkt(4, ipproto.SCTP, port, 0) }
|
||||
|
||||
// IsEchoResponse and IsError read the packet body, so ICMP cases need real
|
||||
// bytes rather than a hand-built [packet.Parsed].
|
||||
icmp4 := func(typ packet.ICMP4Type) packet.Parsed {
|
||||
buf := packet.Generate(packet.ICMP4Header{
|
||||
IP4Header: packet.IP4Header{Src: src, Dst: dst},
|
||||
Type: typ,
|
||||
Code: packet.ICMP4NoCode,
|
||||
}, []byte("payload!!"))
|
||||
var p packet.Parsed
|
||||
p.Decode(buf)
|
||||
if p.IPProto != ipproto.ICMPv4 {
|
||||
t.Fatalf("test setup error: built an %v packet, want ICMPv4", p.IPProto)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// gre is a protocol that carries no port and gets no special handling.
|
||||
const gre = ipproto.Proto(47)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
ranges []tailcfg.ProtoPortRange
|
||||
proto ipproto.Proto
|
||||
port uint16
|
||||
want bool
|
||||
name string
|
||||
ranges views.Slice[tailcfg.ProtoPortRange]
|
||||
p packet.Parsed
|
||||
want bool
|
||||
wantReason string
|
||||
}{
|
||||
{name: "nil-denies-tcp", proto: ipproto.TCP, port: 80},
|
||||
{name: "nil-denies-icmp", proto: ipproto.ICMPv4},
|
||||
{name: "empty-denies-icmp", ranges: []tailcfg.ProtoPortRange{}, proto: ipproto.ICMPv4},
|
||||
{"tcp-syn-match", ppr("tcp:443"), tcpSyn(443), true, connectorTCPReason},
|
||||
{"tcp-syn-wrong-port", ppr("tcp:443"), tcpSyn(80), false, ""},
|
||||
{"tcp-syn-wrong-proto", ppr("udp:443"), tcpSyn(443), false, ""},
|
||||
{"tcp-syn-protoless", ppr("80"), tcpSyn(80), true, connectorTCPReason},
|
||||
{"tcp-syn-wildcard", ppr("*"), tcpSyn(1), true, connectorTCPReason},
|
||||
{"tcp-syn-any-port", ppr("tcp:*"), tcpSyn(9999), true, connectorTCPReason},
|
||||
{"tcp-syn-range-first", ppr("tcp:80-90"), tcpSyn(80), true, connectorTCPReason},
|
||||
{"tcp-syn-range-last", ppr("tcp:80-90"), tcpSyn(90), true, connectorTCPReason},
|
||||
{"tcp-syn-range-low", ppr("tcp:80-90"), tcpSyn(79), false, ""},
|
||||
{"tcp-syn-range-high", ppr("tcp:80-90"), tcpSyn(91), false, ""},
|
||||
{"tcp-syn-empty-grant", ppr(), tcpSyn(443), false, ""},
|
||||
{"tcp-syn-union-first", ppr("tcp:443", "udp:53"), tcpSyn(443), true, connectorTCPReason},
|
||||
{"tcp-syn-union-neither", ppr("tcp:443", "udp:53"), tcpSyn(53), false, ""},
|
||||
|
||||
{name: "wildcard-tcp", ranges: []tailcfg.ProtoPortRange{ppr("*")}, proto: ipproto.TCP, port: 1, want: true},
|
||||
{name: "wildcard-udp", ranges: []tailcfg.ProtoPortRange{ppr("*")}, proto: ipproto.UDP, port: 65535, want: true},
|
||||
{name: "wildcard-icmp6", ranges: []tailcfg.ProtoPortRange{ppr("*")}, proto: ipproto.ICMPv6, want: true},
|
||||
// "*" is protocol-less, which means TCP+UDP+ICMP, not every protocol.
|
||||
{name: "wildcard-not-sctp", ranges: []tailcfg.ProtoPortRange{ppr("*")}, proto: ipproto.SCTP, port: 80},
|
||||
// Inherited from RunIn: anything that is not a lone SYN is admitted,
|
||||
// including to a port the grant does not name, and with no grant at
|
||||
// all. On a connector that packet then gets DNATed on to the app.
|
||||
{"tcp-ack-ungranted-port", ppr("tcp:443"), tcp(22, packet.TCPAck), true, connectorTCPNonSynReason},
|
||||
{"tcp-ack-empty-grant", ppr(), tcp(22, packet.TCPAck), true, connectorTCPNonSynReason},
|
||||
{"tcp-syn-ack-is-not-a-syn", ppr(), tcp(22, packet.TCPSynAck), true, connectorTCPNonSynReason},
|
||||
|
||||
{name: "tcp-match", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443")}, proto: ipproto.TCP, port: 443, want: true},
|
||||
{name: "tcp-wrong-port", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443")}, proto: ipproto.TCP, port: 80},
|
||||
{name: "tcp-wrong-proto", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443")}, proto: ipproto.UDP, port: 443},
|
||||
// ICMP is permitted by any grant, even a TCP-only one.
|
||||
{name: "tcp-grant-allows-icmp", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443")}, proto: ipproto.ICMPv4, want: true},
|
||||
{"udp-match", ppr("udp:53"), udp(53), true, connectorPortReason},
|
||||
{"udp-wrong-port", ppr("udp:53"), udp(54), false, ""},
|
||||
{"udp-protoless", ppr("80"), udp(80), true, connectorPortReason},
|
||||
{"udp-empty-grant", ppr(), udp(53), false, ""},
|
||||
{"sctp-match", ppr("132:9"), sctp(9), true, connectorPortReason},
|
||||
{"sctp-wrong-port", ppr("132:9"), sctp(10), false, ""},
|
||||
// A range naming no protocol covers UDP but not SCTP.
|
||||
{"sctp-protoless-denied", ppr("9"), sctp(9), false, ""},
|
||||
|
||||
{name: "tcp-any-port", ranges: []tailcfg.ProtoPortRange{ppr("tcp:*")}, proto: ipproto.TCP, port: 9999, want: true},
|
||||
{name: "tcp-any-port-not-udp", ranges: []tailcfg.ProtoPortRange{ppr("tcp:*")}, proto: ipproto.UDP, port: 9999},
|
||||
{"icmp4-request-with-grant", ppr("tcp:443"), icmp4(packet.ICMP4EchoRequest), true, connectorICMPReason},
|
||||
{"icmp4-request-empty-grant", ppr(), icmp4(packet.ICMP4EchoRequest), false, ""},
|
||||
// Responses are admitted even with no grant at all.
|
||||
{"icmp4-echo-reply-empty-grant", ppr(), icmp4(packet.ICMP4EchoReply), true, connectorICMPResponseReason},
|
||||
{"icmp4-unreachable-empty-grant", ppr(), icmp4(packet.ICMP4Unreachable), true, connectorICMPResponseReason},
|
||||
// An ICMP protocol that disagrees with the packet's IP version is
|
||||
// refused rather than falling through to the default case.
|
||||
{"icmp6-on-ipv4", ppr("*"), pkt(4, ipproto.ICMPv6, 0, 0), false, ""},
|
||||
{"icmp4-on-ipv6", ppr("*"), pkt(6, ipproto.ICMPv4, 0, 0), false, ""},
|
||||
|
||||
{name: "range-low", ranges: []tailcfg.ProtoPortRange{ppr("tcp:80-90")}, proto: ipproto.TCP, port: 79},
|
||||
{name: "range-first", ranges: []tailcfg.ProtoPortRange{ppr("tcp:80-90")}, proto: ipproto.TCP, port: 80, want: true},
|
||||
{name: "range-last", ranges: []tailcfg.ProtoPortRange{ppr("tcp:80-90")}, proto: ipproto.TCP, port: 90, want: true},
|
||||
{name: "range-high", ranges: []tailcfg.ProtoPortRange{ppr("tcp:80-90")}, proto: ipproto.TCP, port: 91},
|
||||
|
||||
{name: "protoless-tcp", ranges: []tailcfg.ProtoPortRange{ppr("80")}, proto: ipproto.TCP, port: 80, want: true},
|
||||
{name: "protoless-udp", ranges: []tailcfg.ProtoPortRange{ppr("80")}, proto: ipproto.UDP, port: 80, want: true},
|
||||
{name: "protoless-wrong-port", ranges: []tailcfg.ProtoPortRange{ppr("80")}, proto: ipproto.TCP, port: 81},
|
||||
|
||||
{name: "numeric-proto", ranges: []tailcfg.ProtoPortRange{ppr("132:9")}, proto: ipproto.SCTP, port: 9, want: true},
|
||||
|
||||
{name: "union-first", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443"), ppr("udp:53")}, proto: ipproto.TCP, port: 443, want: true},
|
||||
{name: "union-second", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443"), ppr("udp:53")}, proto: ipproto.UDP, port: 53, want: true},
|
||||
{name: "union-neither", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443"), ppr("udp:53")}, proto: ipproto.UDP, port: 443},
|
||||
// A protocol that carries no port has to be granted by name, in full.
|
||||
{"gre-all-ports", ppr("47:*"), pkt(4, gre, 0, 0), true, connectorPortlessReason},
|
||||
{"gre-narrow-ports", ppr("47:80"), pkt(4, gre, 0, 0), false, ""},
|
||||
{"gre-protoless-grant", ppr("*"), pkt(4, gre, 0, 0), false, ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := protoPortRangesAllow(tt.ranges, tt.proto, tt.port); got != tt.want {
|
||||
t.Errorf("protoPortRangesAllow(%v, %v, %d) = %v, want %v",
|
||||
tt.ranges, tt.proto, tt.port, got, tt.want)
|
||||
got, gotReason := protoPortRangesAllow(tt.ranges, tt.p)
|
||||
if got != tt.want || gotReason != tt.wantReason {
|
||||
t.Errorf("protoPortRangesAllow(%v, %v) = (%v, %q), want (%v, %q)",
|
||||
tt.ranges, &tt.p, got, gotReason, tt.want, tt.wantReason)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/tstime"
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/types/views"
|
||||
)
|
||||
|
||||
type appAddr struct {
|
||||
@@ -24,11 +25,11 @@ type appAddr struct {
|
||||
// reach through app at the moment it registered this mapping. See
|
||||
// [appProtoPortRanges] for where they come from, and
|
||||
// [connector.ingressAllow] for how they are enforced.
|
||||
ports []tailcfg.ProtoPortRange
|
||||
ports views.Slice[tailcfg.ProtoPortRange]
|
||||
expiryEntry *list.Element
|
||||
}
|
||||
|
||||
func (c *connector) handleTransitIPRequest(n tailcfg.NodeView, peerV4 netip.Addr, peerV6 netip.Addr, tipr TransitIPRequest, ports []tailcfg.ProtoPortRange) TransitIPResponse {
|
||||
func (c *connector) handleTransitIPRequest(n tailcfg.NodeView, peerV4 netip.Addr, peerV6 netip.Addr, tipr TransitIPRequest, ports views.Slice[tailcfg.ProtoPortRange]) TransitIPResponse {
|
||||
if tipr.TransitIP.Is4() != tipr.DestinationIP.Is4() {
|
||||
c.logf("[Unexpected] peer attempt to map a transit IP to dest IP did not have matching families: node: %s, tIPv4: %v dIPv4: %v",
|
||||
n.StableID(), tipr.TransitIP.Is4(), tipr.DestinationIP.Is4())
|
||||
@@ -162,7 +163,7 @@ func (c *connector) destinationInPools(dst netip.Addr) bool {
|
||||
// decided by [connector.ingressAllow].
|
||||
func (c *connector) linkLocalAllow(p packet.Parsed) (bool, string) {
|
||||
if c.transitIPInPool(p.Dst.Addr()) {
|
||||
return true, packetFilterAllowReason
|
||||
return true, connectorTransitIPReason
|
||||
}
|
||||
return false, ""
|
||||
}
|
||||
@@ -190,12 +191,9 @@ func (c *connector) ingressAllow(p packet.Parsed) (bool, string) {
|
||||
defer c.mu.Unlock()
|
||||
aa, ok := c.lookupAppAddrBySrcIPAndTransitIPLocked(p.Src.Addr(), p.Dst.Addr())
|
||||
if !ok {
|
||||
return true, packetFilterAllowReason
|
||||
return true, connectorUnmappedReason
|
||||
}
|
||||
if !protoPortRangesAllow(aa.ports, p.IPProto, p.Dst.Port()) {
|
||||
return false, ""
|
||||
}
|
||||
return true, packetFilterAllowReason
|
||||
return protoPortRangesAllow(aa.ports, p)
|
||||
}
|
||||
|
||||
// lookupAppAddrBySrcIPAndTransitIPLocked returns the mapping the peer at srcIP
|
||||
|
||||
@@ -318,6 +318,9 @@ func TestConnectorLinkLocalAllow(t *testing.T) {
|
||||
// IP with no registered mapping still gets through so the datapath can answer
|
||||
// with the re-registration TSMP, and that addresses outside the transit pools
|
||||
// are refused outright.
|
||||
//
|
||||
// The per-protocol matching itself is covered in more detail by
|
||||
// [TestProtoPortRangesAllow].
|
||||
func TestConnectorIngressAllow(t *testing.T) {
|
||||
const appName = "app"
|
||||
|
||||
@@ -332,6 +335,9 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
type probe struct {
|
||||
proto ipproto.Proto
|
||||
port uint16
|
||||
// flags are the packet's TCP flags. A TCP probe that names none is
|
||||
// sent as a lone SYN, the case the grant is actually consulted for.
|
||||
flags packet.TCPFlag
|
||||
dst netip.Addr // defaults to transitIP
|
||||
src netip.Addr // defaults to peerIP
|
||||
want bool
|
||||
@@ -375,10 +381,23 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
{proto: ipproto.TCP, port: 80, want: true},
|
||||
{proto: ipproto.UDP, port: 80, want: true},
|
||||
{proto: ipproto.TCP, port: 443, want: false},
|
||||
// A protocol-less range covers TCP+UDP+ICMP, not SCTP.
|
||||
// A protocol-less range covers TCP and UDP, not SCTP.
|
||||
{proto: ipproto.SCTP, port: 80, want: false},
|
||||
},
|
||||
},
|
||||
{
|
||||
// Following the packet filter, only a lone SYN is held to the
|
||||
// grant's ports; continuations of an existing session are let
|
||||
// through so that return traffic arrives. See
|
||||
// [protoPortRangesAllow].
|
||||
name: "tcp-non-syn-not-held-to-ports",
|
||||
grant: []tailcfg.RawMessage{`"tcp:443"`},
|
||||
probes: []probe{
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPAck, want: true},
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPSynAck, want: true},
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPSyn, want: false},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "port-range",
|
||||
grant: []tailcfg.RawMessage{`"tcp:8000-8100"`},
|
||||
@@ -472,10 +491,16 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
if !dst.IsValid() {
|
||||
dst = transitIP
|
||||
}
|
||||
flags := p.flags
|
||||
if p.proto == ipproto.TCP && flags == 0 {
|
||||
flags = packet.TCPSyn
|
||||
}
|
||||
got, _ := c.connector.ingressAllow(packet.Parsed{
|
||||
IPProto: p.proto,
|
||||
Src: netip.AddrPortFrom(src, 12345),
|
||||
Dst: netip.AddrPortFrom(dst, p.port),
|
||||
IPVersion: 4,
|
||||
IPProto: p.proto,
|
||||
TCPFlags: flags,
|
||||
Src: netip.AddrPortFrom(src, 12345),
|
||||
Dst: netip.AddrPortFrom(dst, p.port),
|
||||
})
|
||||
if got != p.want {
|
||||
t.Errorf("ingressAllow(%v %v -> %v:%d) = %v, want %v",
|
||||
@@ -579,7 +604,7 @@ func TestConnectorGrantRegistration(t *testing.T) {
|
||||
if !ok {
|
||||
t.Fatal("no mapping registered")
|
||||
}
|
||||
if diff := cmp.Diff(tt.wantPorts, aa.ports); diff != "" {
|
||||
if diff := cmp.Diff(tt.wantPorts, aa.ports.AsSlice()); diff != "" {
|
||||
t.Errorf("stored ports mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user