2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-11-10 05:22:36 +00:00
|
|
|
|
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2020-12-14 16:21:41 +00:00
|
|
|
"strings"
|
2020-11-10 05:22:36 +00:00
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
"go4.org/netipx"
|
2024-06-16 18:34:11 +00:00
|
|
|
"tailscale.com/net/ipset"
|
2022-07-25 03:08:42 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2020-11-10 05:22:36 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-03-20 04:05:51 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2024-06-16 01:20:17 +00:00
|
|
|
"tailscale.com/types/views"
|
2020-11-10 05:22:36 +00:00
|
|
|
)
|
|
|
|
|
2021-03-20 04:05:51 +00:00
|
|
|
var defaultProtos = []ipproto.Proto{
|
2021-03-21 04:45:47 +00:00
|
|
|
ipproto.TCP,
|
|
|
|
ipproto.UDP,
|
|
|
|
ipproto.ICMPv4,
|
|
|
|
ipproto.ICMPv6,
|
2021-03-17 21:24:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
// MatchesFromFilterRules converts tailcfg FilterRules into Matches.
|
|
|
|
// If an error is returned, the Matches result is still valid,
|
|
|
|
// containing the rules that were successfully converted.
|
2020-11-10 05:33:41 +00:00
|
|
|
func MatchesFromFilterRules(pf []tailcfg.FilterRule) ([]Match, error) {
|
2020-11-10 05:22:36 +00:00
|
|
|
mm := make([]Match, 0, len(pf))
|
|
|
|
var erracc error
|
|
|
|
|
|
|
|
for _, r := range pf {
|
2022-05-13 20:56:53 +00:00
|
|
|
// Profiling determined that this function was spending a lot
|
|
|
|
// of time in runtime.growslice. As such, we attempt to
|
|
|
|
// pre-allocate some slices. Multipliers were chosen arbitrarily.
|
|
|
|
m := Match{
|
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: make([]netip.Prefix, 0, len(r.SrcIPs)),
|
2022-05-13 20:56:53 +00:00
|
|
|
Dsts: make([]NetPortRange, 0, 2*len(r.DstPorts)),
|
|
|
|
Caps: make([]CapMatch, 0, 3*len(r.CapGrant)),
|
|
|
|
}
|
2020-11-10 05:22:36 +00:00
|
|
|
|
2021-03-17 21:24:32 +00:00
|
|
|
if len(r.IPProto) == 0 {
|
2021-03-20 04:05:51 +00:00
|
|
|
m.IPProto = append([]ipproto.Proto(nil), defaultProtos...)
|
2021-03-17 21:24:32 +00:00
|
|
|
} else {
|
2021-03-20 04:05:51 +00:00
|
|
|
m.IPProto = make([]ipproto.Proto, 0, len(r.IPProto))
|
2021-03-17 21:24:32 +00:00
|
|
|
for _, n := range r.IPProto {
|
|
|
|
if n >= 0 && n <= 0xff {
|
2021-03-20 04:05:51 +00:00
|
|
|
m.IPProto = append(m.IPProto, ipproto.Proto(n))
|
2021-03-17 21:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:22:36 +00:00
|
|
|
for i, s := range r.SrcIPs {
|
2020-12-14 16:21:41 +00:00
|
|
|
var bits *int
|
2020-11-10 05:22:36 +00:00
|
|
|
if len(r.SrcBits) > i {
|
2020-12-14 16:21:41 +00:00
|
|
|
bits = &r.SrcBits[i]
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
2020-12-14 16:21:41 +00:00
|
|
|
nets, err := parseIPSet(s, bits)
|
2020-11-10 05:22:36 +00:00
|
|
|
if err != nil && erracc == nil {
|
|
|
|
erracc = err
|
|
|
|
continue
|
|
|
|
}
|
2020-11-12 22:14:11 +00:00
|
|
|
m.Srcs = append(m.Srcs, nets...)
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
2024-06-16 18:34:11 +00:00
|
|
|
m.SrcsContains = ipset.NewContainsIPFunc(views.SliceOf(m.Srcs))
|
2020-11-10 05:22:36 +00:00
|
|
|
|
|
|
|
for _, d := range r.DstPorts {
|
2020-12-14 16:21:41 +00:00
|
|
|
nets, err := parseIPSet(d.IP, d.Bits)
|
2020-11-10 05:22:36 +00:00
|
|
|
if err != nil && erracc == nil {
|
|
|
|
erracc = err
|
|
|
|
continue
|
|
|
|
}
|
2020-11-12 22:14:11 +00:00
|
|
|
for _, net := range nets {
|
|
|
|
m.Dsts = append(m.Dsts, NetPortRange{
|
|
|
|
Net: net,
|
|
|
|
Ports: PortRange{
|
|
|
|
First: d.Ports.First,
|
|
|
|
Last: d.Ports.Last,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
2022-03-18 18:48:40 +00:00
|
|
|
for _, cm := range r.CapGrant {
|
|
|
|
for _, dstNet := range cm.Dsts {
|
|
|
|
for _, cap := range cm.Caps {
|
|
|
|
m.Caps = append(m.Caps, CapMatch{
|
|
|
|
Dst: dstNet,
|
|
|
|
Cap: cap,
|
|
|
|
})
|
|
|
|
}
|
2023-07-25 04:07:00 +00:00
|
|
|
for cap, val := range cm.CapMap {
|
|
|
|
m.Caps = append(m.Caps, CapMatch{
|
|
|
|
Dst: dstNet,
|
|
|
|
Cap: tailcfg.PeerCapability(cap),
|
|
|
|
Values: val,
|
|
|
|
})
|
|
|
|
}
|
2022-03-18 18:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-10 05:22:36 +00:00
|
|
|
|
|
|
|
mm = append(mm, m)
|
|
|
|
}
|
|
|
|
return mm, erracc
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:23:17 +00:00
|
|
|
var (
|
|
|
|
zeroIP4 = netaddr.IPv4(0, 0, 0, 0)
|
2022-08-02 20:38:11 +00:00
|
|
|
zeroIP6 = netip.AddrFrom16([16]byte{})
|
2020-11-11 07:23:17 +00:00
|
|
|
)
|
|
|
|
|
2020-12-14 16:21:41 +00:00
|
|
|
// parseIPSet parses arg as one:
|
|
|
|
//
|
2022-08-02 16:33:46 +00:00
|
|
|
// - an IP address (IPv4 or IPv6)
|
|
|
|
// - the string "*" to match everything (both IPv4 & IPv6)
|
|
|
|
// - a CIDR (e.g. "192.168.0.0/16")
|
|
|
|
// - a range of two IPs, inclusive, separated by hyphen ("2eff::1-2eff::0800")
|
2020-12-14 16:21:41 +00:00
|
|
|
//
|
|
|
|
// bits, if non-nil, is the legacy SrcBits CIDR length to make a IP
|
|
|
|
// address (without a slash) treated as a CIDR of *bits length.
|
|
|
|
//
|
|
|
|
// TODO(bradfitz): make this return an IPSet and plumb that all
|
|
|
|
// around, and ultimately use a new version of IPSet.ContainsFunc like
|
|
|
|
// Contains16Func that works in [16]byte address, so we we can match
|
|
|
|
// at runtime without allocating?
|
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 parseIPSet(arg string, bits *int) ([]netip.Prefix, error) {
|
2020-12-14 16:21:41 +00:00
|
|
|
if arg == "*" {
|
|
|
|
// User explicitly requested wildcard.
|
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
|
|
|
return []netip.Prefix{
|
|
|
|
netip.PrefixFrom(zeroIP4, 0),
|
|
|
|
netip.PrefixFrom(zeroIP6, 0),
|
2020-11-12 22:14:11 +00:00
|
|
|
}, nil
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
2020-12-14 16:21:41 +00:00
|
|
|
if strings.Contains(arg, "/") {
|
2022-07-26 03:55:44 +00:00
|
|
|
pfx, err := netip.ParsePrefix(arg)
|
2020-12-14 16:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if pfx != pfx.Masked() {
|
|
|
|
return nil, fmt.Errorf("%v contains non-network bits set", pfx)
|
|
|
|
}
|
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
|
|
|
return []netip.Prefix{pfx}, nil
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
2020-12-14 16:21:41 +00:00
|
|
|
if strings.Count(arg, "-") == 1 {
|
2022-03-16 21:25:31 +00:00
|
|
|
ip1s, ip2s, _ := strings.Cut(arg, "-")
|
2022-07-26 03:55:44 +00:00
|
|
|
ip1, err := netip.ParseAddr(ip1s)
|
2020-12-14 16:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-26 03:55:44 +00:00
|
|
|
ip2, err := netip.ParseAddr(ip2s)
|
2020-12-14 16:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
r := netipx.IPRangeFrom(ip1, ip2)
|
2020-12-14 16:21:41 +00:00
|
|
|
if !r.Valid() {
|
|
|
|
return nil, fmt.Errorf("invalid IP range %q", arg)
|
|
|
|
}
|
|
|
|
return r.Prefixes(), nil
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
2022-07-26 03:55:44 +00:00
|
|
|
ip, err := netip.ParseAddr(arg)
|
2020-12-14 16:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid IP address %q", arg)
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
bits8 := uint8(ip.BitLen())
|
2020-12-15 20:12:56 +00:00
|
|
|
if bits != nil {
|
|
|
|
if *bits < 0 || *bits > int(bits8) {
|
|
|
|
return nil, fmt.Errorf("invalid CIDR size %d for IP %q", *bits, arg)
|
2020-12-14 16:21:41 +00:00
|
|
|
}
|
2020-12-15 20:12:56 +00:00
|
|
|
bits8 = uint8(*bits)
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|
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
|
|
|
return []netip.Prefix{netip.PrefixFrom(ip, int(bits8))}, nil
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|