2020-11-10 05:22:36 +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.
|
|
|
|
|
|
|
|
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"
|
|
|
|
"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"
|
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{
|
|
|
|
Srcs: make([]netaddr.IPPrefix, 0, len(r.SrcIPs)),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
zeroIP6 = netaddr.IPFrom16([16]byte{})
|
|
|
|
)
|
|
|
|
|
2020-12-14 16:21:41 +00:00
|
|
|
// parseIPSet parses arg as one:
|
|
|
|
//
|
|
|
|
// * 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")
|
|
|
|
//
|
|
|
|
// 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?
|
|
|
|
func parseIPSet(arg string, bits *int) ([]netaddr.IPPrefix, error) {
|
|
|
|
if arg == "*" {
|
|
|
|
// User explicitly requested wildcard.
|
2020-11-12 22:14:11 +00:00
|
|
|
return []netaddr.IPPrefix{
|
2021-05-15 01:07:28 +00:00
|
|
|
netaddr.IPPrefixFrom(zeroIP4, 0),
|
|
|
|
netaddr.IPPrefixFrom(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)
|
|
|
|
}
|
|
|
|
return []netaddr.IPPrefix{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
|
|
|
}
|
2021-05-15 01:07:28 +00:00
|
|
|
return []netaddr.IPPrefix{netaddr.IPPrefixFrom(ip, bits8)}, nil
|
2020-11-10 05:22:36 +00:00
|
|
|
}
|