2020-02-05 22:16:58 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
// Package filter is a stateful packet filter.
|
2020-02-05 22:16:58 +00:00
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
2020-07-30 17:57:30 +00:00
|
|
|
"fmt"
|
2021-06-25 14:10:26 +00:00
|
|
|
"os"
|
2020-02-05 22:16:58 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-11-10 04:12:21 +00:00
|
|
|
"inet.af/netaddr"
|
2021-01-11 20:46:45 +00:00
|
|
|
"tailscale.com/net/flowtrack"
|
2020-11-10 00:16:04 +00:00
|
|
|
"tailscale.com/net/packet"
|
2021-07-22 00:23:38 +00:00
|
|
|
"tailscale.com/tstime/rate"
|
2021-03-20 04:05:51 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-03-25 15:40:36 +00:00
|
|
|
// Filter is a stateful packet filter.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Filter struct {
|
2020-05-22 02:41:18 +00:00
|
|
|
logf logger.Logf
|
2021-02-22 22:34:15 +00:00
|
|
|
// local is the set of IPs prefixes that we know to be "local" to
|
|
|
|
// this node. All packets coming in over tailscale must have a
|
|
|
|
// destination within local, regardless of the policy filter
|
|
|
|
// below.
|
|
|
|
local *netaddr.IPSet
|
2021-03-10 00:10:30 +00:00
|
|
|
// logIPs is the set of IPs that are allowed to appear in flow
|
|
|
|
// logs. If a packet is to or from an IP not in logIPs, it will
|
|
|
|
// never be logged.
|
|
|
|
logIPs *netaddr.IPSet
|
2020-11-11 07:23:17 +00:00
|
|
|
// matches4 and matches6 are lists of match->action rules
|
|
|
|
// applied to all packets arriving over tailscale
|
|
|
|
// tunnels. Matches are checked in order, and processing stops
|
|
|
|
// at the first matching rule. The default policy if no rules
|
|
|
|
// match is to drop the packet.
|
2020-12-20 00:43:25 +00:00
|
|
|
matches4 matches
|
|
|
|
matches6 matches
|
2020-05-22 02:41:18 +00:00
|
|
|
// state is the connection tracking state attached to this
|
|
|
|
// filter. It is used to allow incoming traffic that is a response
|
|
|
|
// to an outbound connection that this node made, even if those
|
|
|
|
// incoming packets don't get accepted by matches above.
|
2020-12-20 00:43:25 +00:00
|
|
|
state *filterState
|
2021-01-12 20:03:41 +00:00
|
|
|
|
|
|
|
shieldsUp bool
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
// filterState is a state cache of past seen packets.
|
|
|
|
type filterState struct {
|
|
|
|
mu sync.Mutex
|
2021-01-11 20:46:45 +00:00
|
|
|
lru *flowtrack.Cache // from flowtrack.Tuple -> nil
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lruMax is the size of the LRU cache in filterState.
|
|
|
|
const lruMax = 512
|
|
|
|
|
|
|
|
// Response is a verdict from the packet filter.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Response int
|
|
|
|
|
|
|
|
const (
|
2021-01-12 20:03:41 +00:00
|
|
|
Drop Response = iota // do not continue processing packet.
|
|
|
|
DropSilently // do not continue processing packet, but also don't log
|
|
|
|
Accept // continue processing packet.
|
|
|
|
noVerdict // no verdict yet, continue running filter
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r Response) String() string {
|
|
|
|
switch r {
|
|
|
|
case Drop:
|
|
|
|
return "Drop"
|
2021-01-12 20:03:41 +00:00
|
|
|
case DropSilently:
|
|
|
|
return "DropSilently"
|
2020-02-05 22:16:58 +00:00
|
|
|
case Accept:
|
|
|
|
return "Accept"
|
|
|
|
case noVerdict:
|
|
|
|
return "noVerdict"
|
|
|
|
default:
|
|
|
|
return "???"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 20:03:41 +00:00
|
|
|
func (r Response) IsDrop() bool {
|
|
|
|
return r == Drop || r == DropSilently
|
|
|
|
}
|
|
|
|
|
2020-03-25 15:40:36 +00:00
|
|
|
// RunFlags controls the filter's debug log verbosity at runtime.
|
2020-02-05 22:16:58 +00:00
|
|
|
type RunFlags int
|
|
|
|
|
|
|
|
const (
|
2020-11-10 05:22:36 +00:00
|
|
|
LogDrops RunFlags = 1 << iota // write dropped packet info to logf
|
|
|
|
LogAccepts // write accepted packet info to logf
|
|
|
|
HexdumpDrops // print packet hexdump when logging drops
|
|
|
|
HexdumpAccepts // print packet hexdump when logging accepts
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-11-10 06:02:03 +00:00
|
|
|
// NewAllowAllForTest returns a packet filter that accepts
|
|
|
|
// everything. Use in tests only, as it permits some kinds of spoofing
|
|
|
|
// attacks to reach the OS network stack.
|
|
|
|
func NewAllowAllForTest(logf logger.Logf) *Filter {
|
2021-05-15 01:07:28 +00:00
|
|
|
any4 := netaddr.IPPrefixFrom(netaddr.IPv4(0, 0, 0, 0), 0)
|
|
|
|
any6 := netaddr.IPPrefixFrom(netaddr.IPFrom16([16]byte{}), 0)
|
2020-11-11 07:23:17 +00:00
|
|
|
ms := []Match{
|
|
|
|
{
|
2021-11-25 21:19:17 +00:00
|
|
|
IPProto: []ipproto.Proto{ipproto.TCP, ipproto.UDP, ipproto.ICMPv4},
|
|
|
|
Srcs: []netaddr.IPPrefix{any4},
|
2020-11-11 07:23:17 +00:00
|
|
|
Dsts: []NetPortRange{
|
|
|
|
{
|
|
|
|
Net: any4,
|
|
|
|
Ports: PortRange{
|
|
|
|
First: 0,
|
|
|
|
Last: 65535,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-11-25 21:19:17 +00:00
|
|
|
IPProto: []ipproto.Proto{ipproto.TCP, ipproto.UDP, ipproto.ICMPv6},
|
|
|
|
Srcs: []netaddr.IPPrefix{any6},
|
2020-11-11 07:23:17 +00:00
|
|
|
Dsts: []NetPortRange{
|
|
|
|
{
|
|
|
|
Net: any6,
|
|
|
|
Ports: PortRange{
|
|
|
|
First: 0,
|
|
|
|
Last: 65535,
|
|
|
|
},
|
2020-11-10 06:02:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-22 22:34:15 +00:00
|
|
|
var sb netaddr.IPSetBuilder
|
|
|
|
sb.AddPrefix(any4)
|
|
|
|
sb.AddPrefix(any6)
|
2021-06-02 16:04:37 +00:00
|
|
|
ipSet, _ := sb.IPSet()
|
|
|
|
return New(ms, ipSet, ipSet, nil, logf)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:40:36 +00:00
|
|
|
// NewAllowNone returns a packet filter that rejects everything.
|
2021-03-10 00:10:30 +00:00
|
|
|
func NewAllowNone(logf logger.Logf, logIPs *netaddr.IPSet) *Filter {
|
|
|
|
return New(nil, &netaddr.IPSet{}, logIPs, nil, logf)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 21:39:53 +00:00
|
|
|
// NewShieldsUpFilter returns a packet filter that rejects incoming connections.
|
|
|
|
//
|
|
|
|
// If shareStateWith is non-nil, the returned filter shares state with the previous one,
|
|
|
|
// as long as the previous one was also a shields up filter.
|
2021-03-10 00:10:30 +00:00
|
|
|
func NewShieldsUpFilter(localNets *netaddr.IPSet, logIPs *netaddr.IPSet, shareStateWith *Filter, logf logger.Logf) *Filter {
|
2021-01-22 21:39:53 +00:00
|
|
|
// Don't permit sharing state with a prior filter that wasn't a shields-up filter.
|
|
|
|
if shareStateWith != nil && !shareStateWith.shieldsUp {
|
|
|
|
shareStateWith = nil
|
|
|
|
}
|
2021-03-10 00:10:30 +00:00
|
|
|
f := New(nil, localNets, logIPs, shareStateWith, logf)
|
2021-01-12 20:03:41 +00:00
|
|
|
f.shieldsUp = true
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-05-22 02:41:18 +00:00
|
|
|
// New creates a new packet filter. The filter enforces that incoming
|
|
|
|
// packets must be destined to an IP in localNets, and must be allowed
|
|
|
|
// by matches. If shareStateWith is non-nil, the returned filter
|
2020-11-10 05:22:36 +00:00
|
|
|
// shares state with the previous one, to enable changing rules at
|
|
|
|
// runtime without breaking existing stateful flows.
|
2021-03-10 00:10:30 +00:00
|
|
|
func New(matches []Match, localNets *netaddr.IPSet, logIPs *netaddr.IPSet, shareStateWith *Filter, logf logger.Logf) *Filter {
|
2020-12-20 00:43:25 +00:00
|
|
|
var state *filterState
|
2020-03-25 07:47:55 +00:00
|
|
|
if shareStateWith != nil {
|
2020-12-20 00:43:25 +00:00
|
|
|
state = shareStateWith.state
|
2020-03-25 07:47:55 +00:00
|
|
|
} else {
|
2020-12-20 00:43:25 +00:00
|
|
|
state = &filterState{
|
2021-01-11 20:46:45 +00:00
|
|
|
lru: &flowtrack.Cache{MaxEntries: lruMax},
|
2020-03-25 07:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
f := &Filter{
|
2020-11-10 04:12:21 +00:00
|
|
|
logf: logf,
|
2020-12-20 00:43:25 +00:00
|
|
|
matches4: matchesFamily(matches, netaddr.IP.Is4),
|
|
|
|
matches6: matchesFamily(matches, netaddr.IP.Is6),
|
2021-02-22 22:34:15 +00:00
|
|
|
local: localNets,
|
2021-03-10 00:10:30 +00:00
|
|
|
logIPs: logIPs,
|
2020-12-20 00:43:25 +00:00
|
|
|
state: state,
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
// matchesFamily returns the subset of ms for which keep(srcNet.IP)
|
|
|
|
// and keep(dstNet.IP) are both true.
|
|
|
|
func matchesFamily(ms matches, keep func(netaddr.IP) bool) matches {
|
|
|
|
var ret matches
|
|
|
|
for _, m := range ms {
|
|
|
|
var retm Match
|
2021-03-17 21:24:32 +00:00
|
|
|
retm.IPProto = m.IPProto
|
2020-12-20 00:43:25 +00:00
|
|
|
for _, src := range m.Srcs {
|
2021-05-15 01:07:28 +00:00
|
|
|
if keep(src.IP()) {
|
2020-12-20 00:43:25 +00:00
|
|
|
retm.Srcs = append(retm.Srcs, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, dst := range m.Dsts {
|
2021-05-15 01:07:28 +00:00
|
|
|
if keep(dst.Net.IP()) {
|
2020-12-20 00:43:25 +00:00
|
|
|
retm.Dsts = append(retm.Dsts, dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(retm.Srcs) > 0 && len(retm.Dsts) > 0 {
|
|
|
|
ret = append(ret, retm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func maybeHexdump(flag RunFlags, b []byte) string {
|
2020-05-08 18:30:22 +00:00
|
|
|
if flag == 0 {
|
2020-02-05 22:16:58 +00:00
|
|
|
return ""
|
|
|
|
}
|
2020-05-08 18:30:22 +00:00
|
|
|
return packet.Hexdump(b) + "\n"
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(apenwarr): use a bigger bucket for specifically TCP SYN accept logging?
|
|
|
|
// Logging is a quick way to record every newly opened TCP connection, but
|
|
|
|
// we have to be cautious about flooding the logs vs letting people use
|
|
|
|
// flood protection to hide their traffic. We could use a rate limiter in
|
|
|
|
// the actual *filter* for SYN accepts, perhaps.
|
2020-05-08 18:30:22 +00:00
|
|
|
var acceptBucket = rate.NewLimiter(rate.Every(10*time.Second), 3)
|
|
|
|
var dropBucket = rate.NewLimiter(rate.Every(5*time.Second), 10)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2021-06-25 14:10:26 +00:00
|
|
|
// NOTE(Xe): This func init is used to detect
|
|
|
|
// TS_DEBUG_FILTER_RATE_LIMIT_LOGS=all, and if it matches, to
|
|
|
|
// effectively disable the limits on the log rate by setting the limit
|
|
|
|
// to 1 millisecond. This should capture everything.
|
|
|
|
func init() {
|
|
|
|
if os.Getenv("TS_DEBUG_FILTER_RATE_LIMIT_LOGS") != "all" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptBucket = rate.NewLimiter(rate.Every(time.Millisecond), 10)
|
|
|
|
dropBucket = rate.NewLimiter(rate.Every(time.Millisecond), 10)
|
|
|
|
}
|
|
|
|
|
2020-11-10 07:49:09 +00:00
|
|
|
func (f *Filter) logRateLimit(runflags RunFlags, q *packet.Parsed, dir direction, r Response, why string) {
|
2021-03-10 00:10:30 +00:00
|
|
|
if !f.loggingAllowed(q) {
|
|
|
|
return
|
|
|
|
}
|
2020-06-02 12:09:20 +00:00
|
|
|
|
2020-07-30 17:57:30 +00:00
|
|
|
if r == Drop && omitDropLogging(q, dir) {
|
2020-07-29 05:10:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-10 00:10:30 +00:00
|
|
|
var verdict string
|
2020-05-08 18:30:22 +00:00
|
|
|
if r == Drop && (runflags&LogDrops) != 0 && dropBucket.Allow() {
|
2020-06-02 12:09:20 +00:00
|
|
|
verdict = "Drop"
|
|
|
|
runflags &= HexdumpDrops
|
|
|
|
} else if r == Accept && (runflags&LogAccepts) != 0 && acceptBucket.Allow() {
|
|
|
|
verdict = "Accept"
|
|
|
|
runflags &= HexdumpAccepts
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: it is crucial that q.String() be called only if {accept,drop}Bucket.Allow() passes,
|
|
|
|
// since it causes an allocation.
|
|
|
|
if verdict != "" {
|
2020-06-08 22:19:26 +00:00
|
|
|
b := q.Buffer()
|
|
|
|
f.logf("%s: %s %d %s\n%s", verdict, q.String(), len(b), why, maybeHexdump(runflags, b))
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 07:22:23 +00:00
|
|
|
// dummyPacket is a 20-byte slice of garbage, to pass the filter
|
|
|
|
// pre-check when evaluating synthesized packets.
|
|
|
|
var dummyPacket = []byte{
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckTCP determines whether TCP traffic from srcIP to dstIP:dstPort
|
|
|
|
// is allowed.
|
|
|
|
func (f *Filter) CheckTCP(srcIP, dstIP netaddr.IP, dstPort uint16) Response {
|
2020-11-10 07:49:09 +00:00
|
|
|
pkt := &packet.Parsed{}
|
2020-11-10 07:22:23 +00:00
|
|
|
pkt.Decode(dummyPacket) // initialize private fields
|
2020-11-11 07:23:17 +00:00
|
|
|
switch {
|
|
|
|
case (srcIP.Is4() && dstIP.Is6()) || (srcIP.Is6() && srcIP.Is4()):
|
|
|
|
// Mistmatched address families, no filters will
|
|
|
|
// match.
|
|
|
|
return Drop
|
|
|
|
case srcIP.Is4():
|
|
|
|
pkt.IPVersion = 4
|
|
|
|
case srcIP.Is6():
|
|
|
|
pkt.IPVersion = 6
|
|
|
|
default:
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
2021-05-15 01:07:28 +00:00
|
|
|
pkt.Src = netaddr.IPPortFrom(srcIP, 0)
|
|
|
|
pkt.Dst = netaddr.IPPortFrom(dstIP, dstPort)
|
2021-03-21 04:45:47 +00:00
|
|
|
pkt.IPProto = ipproto.TCP
|
2020-11-10 07:22:23 +00:00
|
|
|
pkt.TCPFlags = packet.TCPSyn
|
|
|
|
|
|
|
|
return f.RunIn(pkt, 0)
|
|
|
|
}
|
|
|
|
|
2021-01-12 20:03:41 +00:00
|
|
|
// ShieldsUp reports whether this is a "shields up" (block everything
|
|
|
|
// incoming) filter.
|
|
|
|
func (f *Filter) ShieldsUp() bool { return f.shieldsUp }
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
// RunIn determines whether this node is allowed to receive q from a
|
|
|
|
// Tailscale peer.
|
2020-11-10 07:49:09 +00:00
|
|
|
func (f *Filter) RunIn(q *packet.Parsed, rf RunFlags) Response {
|
2020-07-29 05:10:58 +00:00
|
|
|
dir := in
|
|
|
|
r := f.pre(q, rf, dir)
|
2020-02-05 22:16:58 +00:00
|
|
|
if r == Accept || r == Drop {
|
|
|
|
// already logged
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:23:17 +00:00
|
|
|
var why string
|
|
|
|
switch q.IPVersion {
|
|
|
|
case 4:
|
|
|
|
r, why = f.runIn4(q)
|
|
|
|
case 6:
|
|
|
|
r, why = f.runIn6(q)
|
|
|
|
default:
|
|
|
|
r, why = Drop, "not-ip"
|
|
|
|
}
|
2020-07-29 05:10:58 +00:00
|
|
|
f.logRateLimit(rf, q, dir, r, why)
|
2020-02-05 22:16:58 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
// RunOut determines whether this node is allowed to send q to a
|
|
|
|
// Tailscale peer.
|
2020-11-10 07:49:09 +00:00
|
|
|
func (f *Filter) RunOut(q *packet.Parsed, rf RunFlags) Response {
|
2020-07-29 05:10:58 +00:00
|
|
|
dir := out
|
|
|
|
r := f.pre(q, rf, dir)
|
2020-02-05 22:16:58 +00:00
|
|
|
if r == Drop || r == Accept {
|
|
|
|
// already logged
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r, why := f.runOut(q)
|
2020-07-29 05:10:58 +00:00
|
|
|
f.logRateLimit(rf, q, dir, r, why)
|
2020-02-05 22:16:58 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:23:17 +00:00
|
|
|
func (f *Filter) runIn4(q *packet.Parsed) (r Response, why string) {
|
2020-05-22 02:41:18 +00:00
|
|
|
// A compromised peer could try to send us packets for
|
|
|
|
// destinations we didn't explicitly advertise. This check is to
|
|
|
|
// prevent that.
|
2021-05-15 01:07:28 +00:00
|
|
|
if !f.local.Contains(q.Dst.IP()) {
|
2020-05-22 02:41:18 +00:00
|
|
|
return Drop, "destination not allowed"
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
switch q.IPProto {
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv4:
|
2020-04-29 07:53:32 +00:00
|
|
|
if q.IsEchoResponse() || q.IsError() {
|
|
|
|
// ICMP responses are allowed.
|
|
|
|
// TODO(apenwarr): consider using conntrack state.
|
|
|
|
// We could choose to reject all packets that aren't
|
|
|
|
// related to an existing ICMP-Echo, TCP, or UDP
|
|
|
|
// session.
|
|
|
|
return Accept, "icmp response ok"
|
2020-11-10 04:12:21 +00:00
|
|
|
} else if f.matches4.matchIPsOnly(q) {
|
2020-04-29 07:53:32 +00:00
|
|
|
// If any port is open to an IP, allow ICMP to it.
|
2020-02-05 22:16:58 +00:00
|
|
|
return Accept, "icmp ok"
|
|
|
|
}
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.TCP:
|
2020-02-05 22:16:58 +00:00
|
|
|
// For TCP, we want to allow *outgoing* connections,
|
|
|
|
// which means we want to allow return packets on those
|
|
|
|
// connections. To make this restriction work, we need to
|
|
|
|
// allow non-SYN packets (continuation of an existing session)
|
|
|
|
// to arrive. This should be okay since a new incoming session
|
|
|
|
// can't be initiated without first sending a SYN.
|
|
|
|
// It happens to also be much faster.
|
|
|
|
// TODO(apenwarr): Skip the rest of decoding in this path?
|
2021-02-17 17:11:28 +00:00
|
|
|
if !q.IsTCPSyn() {
|
2020-02-05 22:16:58 +00:00
|
|
|
return Accept, "tcp non-syn"
|
|
|
|
}
|
2020-11-10 04:12:21 +00:00
|
|
|
if f.matches4.match(q) {
|
2020-02-05 22:16:58 +00:00
|
|
|
return Accept, "tcp ok"
|
|
|
|
}
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.UDP, ipproto.SCTP:
|
2021-03-20 04:05:51 +00:00
|
|
|
t := flowtrack.Tuple{Proto: q.IPProto, Src: q.Src, Dst: q.Dst}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
f.state.mu.Lock()
|
|
|
|
_, ok := f.state.lru.Get(t)
|
|
|
|
f.state.mu.Unlock()
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
if ok {
|
2021-03-20 04:05:51 +00:00
|
|
|
return Accept, "cached"
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-11-10 04:12:21 +00:00
|
|
|
if f.matches4.match(q) {
|
2021-03-20 04:05:51 +00:00
|
|
|
return Accept, "ok"
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.TSMP:
|
2021-01-12 20:03:41 +00:00
|
|
|
return Accept, "tsmp ok"
|
2020-02-05 22:16:58 +00:00
|
|
|
default:
|
|
|
|
return Drop, "Unknown proto"
|
|
|
|
}
|
|
|
|
return Drop, "no rules matched"
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:23:17 +00:00
|
|
|
func (f *Filter) runIn6(q *packet.Parsed) (r Response, why string) {
|
|
|
|
// A compromised peer could try to send us packets for
|
|
|
|
// destinations we didn't explicitly advertise. This check is to
|
|
|
|
// prevent that.
|
2021-05-15 01:07:28 +00:00
|
|
|
if !f.local.Contains(q.Dst.IP()) {
|
2020-11-11 07:23:17 +00:00
|
|
|
return Drop, "destination not allowed"
|
|
|
|
}
|
|
|
|
|
|
|
|
switch q.IPProto {
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.ICMPv6:
|
2020-11-11 07:23:17 +00:00
|
|
|
if q.IsEchoResponse() || q.IsError() {
|
|
|
|
// ICMP responses are allowed.
|
|
|
|
// TODO(apenwarr): consider using conntrack state.
|
|
|
|
// We could choose to reject all packets that aren't
|
|
|
|
// related to an existing ICMP-Echo, TCP, or UDP
|
|
|
|
// session.
|
|
|
|
return Accept, "icmp response ok"
|
|
|
|
} else if f.matches6.matchIPsOnly(q) {
|
|
|
|
// If any port is open to an IP, allow ICMP to it.
|
|
|
|
return Accept, "icmp ok"
|
|
|
|
}
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.TCP:
|
2020-11-11 07:23:17 +00:00
|
|
|
// For TCP, we want to allow *outgoing* connections,
|
|
|
|
// which means we want to allow return packets on those
|
|
|
|
// connections. To make this restriction work, we need to
|
|
|
|
// allow non-SYN packets (continuation of an existing session)
|
|
|
|
// to arrive. This should be okay since a new incoming session
|
|
|
|
// can't be initiated without first sending a SYN.
|
|
|
|
// It happens to also be much faster.
|
|
|
|
// TODO(apenwarr): Skip the rest of decoding in this path?
|
2021-03-21 04:45:47 +00:00
|
|
|
if q.IPProto == ipproto.TCP && !q.IsTCPSyn() {
|
2020-11-11 07:23:17 +00:00
|
|
|
return Accept, "tcp non-syn"
|
|
|
|
}
|
|
|
|
if f.matches6.match(q) {
|
|
|
|
return Accept, "tcp ok"
|
|
|
|
}
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.UDP, ipproto.SCTP:
|
2021-03-20 04:05:51 +00:00
|
|
|
t := flowtrack.Tuple{Proto: q.IPProto, Src: q.Src, Dst: q.Dst}
|
2020-11-11 07:23:17 +00:00
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
f.state.mu.Lock()
|
|
|
|
_, ok := f.state.lru.Get(t)
|
|
|
|
f.state.mu.Unlock()
|
2020-11-11 07:23:17 +00:00
|
|
|
|
|
|
|
if ok {
|
2021-03-20 04:05:51 +00:00
|
|
|
return Accept, "cached"
|
2020-11-11 07:23:17 +00:00
|
|
|
}
|
|
|
|
if f.matches6.match(q) {
|
2021-03-20 04:05:51 +00:00
|
|
|
return Accept, "ok"
|
2020-11-11 07:23:17 +00:00
|
|
|
}
|
2021-03-23 22:16:15 +00:00
|
|
|
case ipproto.TSMP:
|
|
|
|
return Accept, "tsmp ok"
|
2020-11-11 07:23:17 +00:00
|
|
|
default:
|
|
|
|
return Drop, "Unknown proto"
|
|
|
|
}
|
|
|
|
return Drop, "no rules matched"
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
// runIn runs the output-specific part of the filter logic.
|
2020-11-10 07:49:09 +00:00
|
|
|
func (f *Filter) runOut(q *packet.Parsed) (r Response, why string) {
|
2021-03-20 04:05:51 +00:00
|
|
|
switch q.IPProto {
|
|
|
|
case ipproto.UDP, ipproto.SCTP:
|
|
|
|
tuple := flowtrack.Tuple{
|
|
|
|
Proto: q.IPProto,
|
|
|
|
Src: q.Dst, Dst: q.Src, // src/dst reversed
|
|
|
|
}
|
|
|
|
f.state.mu.Lock()
|
|
|
|
f.state.lru.Add(tuple, nil)
|
|
|
|
f.state.mu.Unlock()
|
2020-11-11 07:23:17 +00:00
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
return Accept, "ok out"
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
// direction is whether a packet was flowing in to this machine, or
|
|
|
|
// flowing out.
|
2020-07-29 05:10:58 +00:00
|
|
|
type direction int
|
|
|
|
|
|
|
|
const (
|
2020-11-10 05:22:36 +00:00
|
|
|
in direction = iota // from Tailscale peer to local machine
|
|
|
|
out // from local machine to Tailscale peer
|
2020-07-29 05:10:58 +00:00
|
|
|
)
|
|
|
|
|
2020-07-30 17:57:30 +00:00
|
|
|
func (d direction) String() string {
|
|
|
|
switch d {
|
|
|
|
case in:
|
|
|
|
return "in"
|
|
|
|
case out:
|
|
|
|
return "out"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("[??dir=%d]", int(d))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
var gcpDNSAddr = netaddr.IPv4(169, 254, 169, 254)
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
// pre runs the direction-agnostic filter logic. dir is only used for
|
|
|
|
// logging.
|
2020-11-10 07:49:09 +00:00
|
|
|
func (f *Filter) pre(q *packet.Parsed, rf RunFlags, dir direction) Response {
|
2020-06-08 22:19:26 +00:00
|
|
|
if len(q.Buffer()) == 0 {
|
2020-02-05 22:16:58 +00:00
|
|
|
// wireguard keepalive packet, always permit.
|
|
|
|
return Accept
|
|
|
|
}
|
2020-06-08 22:19:26 +00:00
|
|
|
if len(q.Buffer()) < 20 {
|
2020-07-29 05:10:58 +00:00
|
|
|
f.logRateLimit(rf, q, dir, Drop, "too short")
|
2020-02-05 22:16:58 +00:00
|
|
|
return Drop
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:07:28 +00:00
|
|
|
if q.Dst.IP().IsMulticast() {
|
2020-12-20 00:43:25 +00:00
|
|
|
f.logRateLimit(rf, q, dir, Drop, "multicast")
|
|
|
|
return Drop
|
|
|
|
}
|
2021-05-15 01:07:28 +00:00
|
|
|
if q.Dst.IP().IsLinkLocalUnicast() && q.Dst.IP() != gcpDNSAddr {
|
2020-12-20 00:43:25 +00:00
|
|
|
f.logRateLimit(rf, q, dir, Drop, "link-local-unicast")
|
|
|
|
return Drop
|
2020-09-25 18:47:38 +00:00
|
|
|
}
|
2020-09-25 18:06:48 +00:00
|
|
|
|
2020-06-04 22:42:44 +00:00
|
|
|
switch q.IPProto {
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.Unknown:
|
2020-06-04 22:42:44 +00:00
|
|
|
// Unknown packets are dangerous; always drop them.
|
2020-07-29 05:10:58 +00:00
|
|
|
f.logRateLimit(rf, q, dir, Drop, "unknown")
|
2020-06-04 22:42:44 +00:00
|
|
|
return Drop
|
2021-03-21 04:45:47 +00:00
|
|
|
case ipproto.Fragment:
|
2020-02-05 22:16:58 +00:00
|
|
|
// Fragments after the first always need to be passed through.
|
2020-11-10 07:49:09 +00:00
|
|
|
// Very small fragments are considered Junk by Parsed.
|
2020-07-29 05:10:58 +00:00
|
|
|
f.logRateLimit(rf, q, dir, Accept, "fragment")
|
2020-02-05 22:16:58 +00:00
|
|
|
return Accept
|
|
|
|
}
|
|
|
|
|
|
|
|
return noVerdict
|
|
|
|
}
|
2020-07-29 05:10:58 +00:00
|
|
|
|
2021-03-10 00:10:30 +00:00
|
|
|
// loggingAllowed reports whether p can appear in logs at all.
|
|
|
|
func (f *Filter) loggingAllowed(p *packet.Parsed) bool {
|
2021-05-15 01:07:28 +00:00
|
|
|
return f.logIPs.Contains(p.Src.IP()) && f.logIPs.Contains(p.Dst.IP())
|
2021-03-10 00:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 05:10:58 +00:00
|
|
|
// omitDropLogging reports whether packet p, which has already been
|
2020-11-10 04:12:21 +00:00
|
|
|
// deemed a packet to Drop, should bypass the [rate-limited] logging.
|
|
|
|
// We don't want to log scary & spammy reject warnings for packets
|
|
|
|
// that are totally normal, like IPv6 route announcements.
|
2020-11-10 07:49:09 +00:00
|
|
|
func omitDropLogging(p *packet.Parsed, dir direction) bool {
|
2020-11-11 07:23:17 +00:00
|
|
|
if dir != out {
|
|
|
|
return false
|
2020-07-29 05:10:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:07:28 +00:00
|
|
|
return p.Dst.IP().IsMulticast() || (p.Dst.IP().IsLinkLocalUnicast() && p.Dst.IP() != gcpDNSAddr) || p.IPProto == ipproto.IGMP
|
2020-07-29 05:10:58 +00:00
|
|
|
}
|