2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
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"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
"net/netip"
|
2023-08-17 05:09:53 +00:00
|
|
|
"slices"
|
2020-02-05 22:16:58 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
"go4.org/netipx"
|
2022-01-24 18:52:57 +00:00
|
|
|
"tailscale.com/envknob"
|
2021-01-11 20:46:45 +00:00
|
|
|
"tailscale.com/net/flowtrack"
|
2022-07-25 03:08:42 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2020-11-10 00:16:04 +00:00
|
|
|
"tailscale.com/net/packet"
|
2024-06-16 01:20:17 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2023-07-25 04:07:00 +00:00
|
|
|
"tailscale.com/tailcfg"
|
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"
|
2024-06-16 01:20:17 +00:00
|
|
|
"tailscale.com/types/views"
|
2023-07-25 04:07:00 +00:00
|
|
|
"tailscale.com/util/mak"
|
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.
|
2024-06-16 01:20:17 +00:00
|
|
|
local func(netip.Addr) bool
|
2022-03-18 18:48:40 +00:00
|
|
|
|
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.
|
2024-06-16 01:20:17 +00:00
|
|
|
logIPs func(netip.Addr) bool
|
2022-03-18 18:48:40 +00:00
|
|
|
|
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
|
2022-03-18 18:48:40 +00:00
|
|
|
|
|
|
|
// cap4 and cap6 are the subsets of the matches that are about
|
|
|
|
// capability grants, partitioned by source IP address family.
|
|
|
|
cap4, cap6 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
|
2023-01-11 21:45:34 +00:00
|
|
|
lru *flowtrack.Cache[struct{}] // from flowtrack.Tuple -> struct{}
|
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 {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
any4 := netip.PrefixFrom(netaddr.IPv4(0, 0, 0, 0), 0)
|
2022-08-02 20:38:11 +00:00
|
|
|
any6 := netip.PrefixFrom(netip.AddrFrom16([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},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Srcs: []netip.Prefix{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},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Srcs: []netip.Prefix{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
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
var sb netipx.IPSetBuilder
|
2021-02-22 22:34:15 +00:00
|
|
|
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.
|
2022-07-25 03:08:42 +00:00
|
|
|
func NewAllowNone(logf logger.Logf, logIPs *netipx.IPSet) *Filter {
|
|
|
|
return New(nil, &netipx.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.
|
2022-07-25 03:08:42 +00:00
|
|
|
func NewShieldsUpFilter(localNets *netipx.IPSet, logIPs *netipx.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.
|
2024-06-16 01:20:17 +00:00
|
|
|
func New(matches []Match, localNets, logIPs *netipx.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{
|
2023-01-11 21:45:34 +00:00
|
|
|
lru: &flowtrack.Cache[struct{}]{MaxEntries: lruMax},
|
2020-03-25 07:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-16 01:20:17 +00:00
|
|
|
|
|
|
|
containsFunc := func(s *netipx.IPSet) func(netip.Addr) bool {
|
|
|
|
if s == nil {
|
|
|
|
return tsaddr.FalseContainsIPFunc()
|
|
|
|
}
|
|
|
|
return tsaddr.NewContainsIPFunc(views.SliceOf(s.Prefixes()))
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
f := &Filter{
|
2020-11-10 04:12:21 +00:00
|
|
|
logf: logf,
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
matches4: matchesFamily(matches, netip.Addr.Is4),
|
|
|
|
matches6: matchesFamily(matches, netip.Addr.Is6),
|
|
|
|
cap4: capMatchesFunc(matches, netip.Addr.Is4),
|
|
|
|
cap6: capMatchesFunc(matches, netip.Addr.Is6),
|
2024-06-16 01:20:17 +00:00
|
|
|
local: containsFunc(localNets),
|
|
|
|
logIPs: containsFunc(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.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func matchesFamily(ms matches, keep func(netip.Addr) bool) matches {
|
2020-12-20 00:43:25 +00:00
|
|
|
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 {
|
2022-07-25 03:08:42 +00:00
|
|
|
if keep(src.Addr()) {
|
2020-12-20 00:43:25 +00:00
|
|
|
retm.Srcs = append(retm.Srcs, src)
|
|
|
|
}
|
|
|
|
}
|
2024-06-16 01:20:17 +00:00
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
for _, dst := range m.Dsts {
|
2022-07-25 03:08:42 +00:00
|
|
|
if keep(dst.Net.Addr()) {
|
2020-12-20 00:43:25 +00:00
|
|
|
retm.Dsts = append(retm.Dsts, dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(retm.Srcs) > 0 && len(retm.Dsts) > 0 {
|
2024-06-16 01:20:17 +00:00
|
|
|
retm.SrcsContains = tsaddr.NewContainsIPFunc(views.SliceOf(retm.Srcs))
|
2020-12-20 00:43:25 +00:00
|
|
|
ret = append(ret, retm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-03-18 18:48:40 +00:00
|
|
|
// capMatchesFunc returns a copy of the subset of ms for which keep(srcNet.IP)
|
|
|
|
// and the match is a capability grant.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func capMatchesFunc(ms matches, keep func(netip.Addr) bool) matches {
|
2022-03-18 18:48:40 +00:00
|
|
|
var ret matches
|
|
|
|
for _, m := range ms {
|
|
|
|
if len(m.Caps) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
retm := Match{Caps: m.Caps}
|
|
|
|
for _, src := range m.Srcs {
|
2022-07-25 03:08:42 +00:00
|
|
|
if keep(src.Addr()) {
|
2022-03-18 18:48:40 +00:00
|
|
|
retm.Srcs = append(retm.Srcs, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(retm.Srcs) > 0 {
|
2024-06-16 01:20:17 +00:00
|
|
|
retm.SrcsContains = tsaddr.NewContainsIPFunc(views.SliceOf(retm.Srcs))
|
2022-03-18 18:48:40 +00:00
|
|
|
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?
|
2022-08-02 16:33:46 +00:00
|
|
|
// 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
|
2022-08-02 16:33:46 +00:00
|
|
|
// 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.
|
2021-06-25 14:10:26 +00:00
|
|
|
func init() {
|
2022-01-24 18:52:57 +00:00
|
|
|
if envknob.String("TS_DEBUG_FILTER_RATE_LIMIT_LOGS") != "all" {
|
2021-06-25 14:10:26 +00:00
|
|
|
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) {
|
2024-06-16 01:20:17 +00:00
|
|
|
if runflags == 0 || !f.loggingAllowed(q) {
|
2021-03-10 00:10:30 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2023-12-02 22:30:33 +00:00
|
|
|
// Check determines whether traffic from srcIP to dstIP:dstPort is allowed
|
|
|
|
// using protocol proto.
|
|
|
|
func (f *Filter) Check(srcIP, dstIP netip.Addr, dstPort uint16, proto ipproto.Proto) 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()):
|
2022-09-25 18:29:55 +00:00
|
|
|
// Mismatched address families, no filters will
|
2020-11-11 07:23:17 +00:00
|
|
|
// match.
|
|
|
|
return Drop
|
|
|
|
case srcIP.Is4():
|
|
|
|
pkt.IPVersion = 4
|
|
|
|
case srcIP.Is6():
|
|
|
|
pkt.IPVersion = 6
|
|
|
|
default:
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
pkt.Src = netip.AddrPortFrom(srcIP, 0)
|
|
|
|
pkt.Dst = netip.AddrPortFrom(dstIP, dstPort)
|
2023-12-02 22:30:33 +00:00
|
|
|
pkt.IPProto = proto
|
|
|
|
if proto == ipproto.TCP {
|
|
|
|
pkt.TCPFlags = packet.TCPSyn
|
|
|
|
}
|
2020-11-10 07:22:23 +00:00
|
|
|
|
|
|
|
return f.RunIn(pkt, 0)
|
|
|
|
}
|
|
|
|
|
2023-12-02 22:30:33 +00:00
|
|
|
// CheckTCP determines whether TCP traffic from srcIP to dstIP:dstPort
|
|
|
|
// is allowed.
|
|
|
|
func (f *Filter) CheckTCP(srcIP, dstIP netip.Addr, dstPort uint16) Response {
|
|
|
|
return f.Check(srcIP, dstIP, dstPort, ipproto.TCP)
|
|
|
|
}
|
|
|
|
|
2023-07-25 04:07:00 +00:00
|
|
|
// CapsWithValues appends to base the capabilities that srcIP has talking
|
2022-03-18 18:48:40 +00:00
|
|
|
// to dstIP.
|
2023-07-25 04:07:00 +00:00
|
|
|
func (f *Filter) CapsWithValues(srcIP, dstIP netip.Addr) tailcfg.PeerCapMap {
|
2022-03-18 18:48:40 +00:00
|
|
|
var mm matches
|
|
|
|
switch {
|
|
|
|
case srcIP.Is4():
|
|
|
|
mm = f.cap4
|
|
|
|
case srcIP.Is6():
|
|
|
|
mm = f.cap6
|
|
|
|
}
|
2023-07-25 04:07:00 +00:00
|
|
|
var out tailcfg.PeerCapMap
|
2022-03-18 18:48:40 +00:00
|
|
|
for _, m := range mm {
|
2024-06-16 01:20:17 +00:00
|
|
|
if !m.SrcsContains(srcIP) {
|
2022-03-18 18:48:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, cm := range m.Caps {
|
|
|
|
if cm.Cap != "" && cm.Dst.Contains(dstIP) {
|
2023-07-25 04:07:00 +00:00
|
|
|
prev, ok := out[cm.Cap]
|
|
|
|
if !ok {
|
|
|
|
mak.Set(&out, cm.Cap, slices.Clone(cm.Values))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out[cm.Cap] = append(prev, cm.Values...)
|
2022-03-18 18:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-25 04:07:00 +00:00
|
|
|
return out
|
2022-03-18 18:48:40 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2023-01-14 16:08:46 +00:00
|
|
|
if r == Accept || r == Drop {
|
2020-02-05 22:16:58 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-01-14 16:24:16 +00:00
|
|
|
var unknownProtoStringCache sync.Map // ipproto.Proto -> string
|
|
|
|
|
|
|
|
func unknownProtoString(proto ipproto.Proto) string {
|
|
|
|
if v, ok := unknownProtoStringCache.Load(proto); ok {
|
|
|
|
return v.(string)
|
|
|
|
}
|
|
|
|
s := fmt.Sprintf("unknown-protocol-%d", proto)
|
|
|
|
unknownProtoStringCache.Store(proto, s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
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.
|
2024-06-16 01:20:17 +00:00
|
|
|
if !f.local(q.Dst.Addr()) {
|
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:
|
2021-12-04 19:52:39 +00:00
|
|
|
if f.matches4.matchProtoAndIPsOnlyIfAllPorts(q) {
|
2022-12-22 20:51:06 +00:00
|
|
|
return Accept, "other-portless ok"
|
2021-12-04 19:52:39 +00:00
|
|
|
}
|
2023-01-14 16:24:16 +00:00
|
|
|
return Drop, unknownProtoString(q.IPProto)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
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.
|
2024-06-16 01:20:17 +00:00
|
|
|
if !f.local(q.Dst.Addr()) {
|
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:
|
2021-12-04 19:52:39 +00:00
|
|
|
if f.matches6.matchProtoAndIPsOnlyIfAllPorts(q) {
|
2022-12-22 20:51:06 +00:00
|
|
|
return Accept, "other-portless ok"
|
2021-12-04 19:52:39 +00:00
|
|
|
}
|
2023-01-14 16:24:16 +00:00
|
|
|
return Drop, unknownProtoString(q.IPProto)
|
2020-11-11 07:23:17 +00:00
|
|
|
}
|
|
|
|
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()
|
2023-01-11 21:45:34 +00:00
|
|
|
f.state.lru.Add(tuple, struct{}{})
|
2021-03-20 04:05:51 +00:00
|
|
|
f.state.mu.Unlock()
|
2020-11-11 07:23:17 +00:00
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
return Accept, "ok out"
|
|
|
|
}
|
|
|
|
|
2022-09-25 18:29:55 +00:00
|
|
|
// direction is whether a packet was flowing into this machine, or
|
2020-11-10 05:22:36 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
if q.Dst.Addr().IsMulticast() {
|
2020-12-20 00:43:25 +00:00
|
|
|
f.logRateLimit(rf, q, dir, Drop, "multicast")
|
|
|
|
return Drop
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if q.Dst.Addr().IsLinkLocalUnicast() && q.Dst.Addr() != 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
|
|
|
|
2022-12-22 20:51:06 +00:00
|
|
|
if q.IPProto == 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 {
|
2024-06-16 01:20:17 +00:00
|
|
|
return f.logIPs(p.Src.Addr()) && f.logIPs(p.Dst.Addr())
|
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
|
|
|
}
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
return p.Dst.Addr().IsMulticast() || (p.Dst.Addr().IsLinkLocalUnicast() && p.Dst.Addr() != gcpDNSAddr) || p.IPProto == ipproto.IGMP
|
2020-07-29 05:10:58 +00:00
|
|
|
}
|