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-04-30 20:20:09 +00:00
|
|
|
package router
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-22 21:55:28 +00:00
|
|
|
"bytes"
|
2020-12-29 16:26:17 +00:00
|
|
|
"errors"
|
2020-02-05 22:16:58 +00:00
|
|
|
"fmt"
|
2023-03-03 05:04:01 +00:00
|
|
|
"net"
|
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"
|
2020-09-22 21:55:28 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
2020-02-05 22:16:58 +00:00
|
|
|
"strings"
|
2022-08-04 04:51:02 +00:00
|
|
|
"sync/atomic"
|
2021-10-28 04:36:29 +00:00
|
|
|
"syscall"
|
2021-07-20 21:10:52 +00:00
|
|
|
"time"
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-03-03 20:38:51 +00:00
|
|
|
"github.com/coreos/go-iptables/iptables"
|
2021-10-28 22:22:03 +00:00
|
|
|
"github.com/tailscale/netlink"
|
2022-12-09 23:12:20 +00:00
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2022-07-25 03:08:42 +00:00
|
|
|
"go4.org/netipx"
|
2021-10-28 04:36:29 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2021-07-20 21:10:52 +00:00
|
|
|
"golang.org/x/time/rate"
|
2022-01-24 18:52:57 +00:00
|
|
|
"tailscale.com/envknob"
|
2023-04-18 21:26:58 +00:00
|
|
|
"tailscale.com/net/netmon"
|
2020-07-07 05:33:29 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-02-04 21:12:42 +00:00
|
|
|
"tailscale.com/types/preftype"
|
2021-11-02 21:30:48 +00:00
|
|
|
"tailscale.com/util/multierr"
|
2020-09-11 02:55:09 +00:00
|
|
|
"tailscale.com/version/distro"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
const (
|
|
|
|
netfilterOff = preftype.NetfilterOff
|
|
|
|
netfilterNoDivert = preftype.NetfilterNoDivert
|
|
|
|
netfilterOn = preftype.NetfilterOn
|
|
|
|
)
|
|
|
|
|
2020-05-02 01:55:38 +00:00
|
|
|
// The following bits are added to packet marks for Tailscale use.
|
|
|
|
//
|
|
|
|
// We tried to pick bits sufficiently out of the way that it's
|
2020-05-07 18:01:46 +00:00
|
|
|
// unlikely to collide with existing uses. We have 4 bytes of mark
|
|
|
|
// bits to play with. We leave the lower byte alone on the assumption
|
|
|
|
// that sysadmins would use those. Kubernetes uses a few bits in the
|
|
|
|
// second byte, so we steer clear of that too.
|
2020-05-02 01:55:38 +00:00
|
|
|
//
|
|
|
|
// Empirically, most of the documentation on packet marks on the
|
|
|
|
// internet gives the impression that the marks are 16 bits
|
|
|
|
// wide. Based on this, we theorize that the upper two bytes are
|
2022-09-12 20:29:41 +00:00
|
|
|
// relatively unused in the wild, and so we consume bits 16:23 (the
|
|
|
|
// third byte).
|
2020-05-02 01:55:38 +00:00
|
|
|
//
|
|
|
|
// The constants are in the iptables/iproute2 string format for
|
|
|
|
// matching and setting the bits, so they can be directly embedded in
|
|
|
|
// commands.
|
|
|
|
const (
|
2022-09-12 20:29:41 +00:00
|
|
|
// The mask for reading/writing the 'firewall mask' bits on a packet.
|
|
|
|
// See the comment on the const block on why we only use the third byte.
|
|
|
|
//
|
|
|
|
// We claim bits 16:23 entirely. For now we only use the lower four
|
|
|
|
// bits, leaving the higher 4 bits for future use.
|
|
|
|
tailscaleFwmarkMask = "0xff0000"
|
|
|
|
tailscaleFwmarkMaskNum = 0xff0000
|
|
|
|
|
2020-05-02 01:55:38 +00:00
|
|
|
// Packet is from Tailscale and to a subnet route destination, so
|
|
|
|
// is allowed to be routed through this machine.
|
2020-07-22 01:24:28 +00:00
|
|
|
tailscaleSubnetRouteMark = "0x40000"
|
2021-10-28 20:11:03 +00:00
|
|
|
|
2020-05-02 01:55:38 +00:00
|
|
|
// Packet was originated by tailscaled itself, and must not be
|
|
|
|
// routed over the Tailscale network.
|
2020-05-29 00:43:15 +00:00
|
|
|
//
|
|
|
|
// Keep this in sync with tailscaleBypassMark in
|
|
|
|
// net/netns/netns_linux.go.
|
2021-10-28 20:11:03 +00:00
|
|
|
tailscaleBypassMark = "0x80000"
|
|
|
|
tailscaleBypassMarkNum = 0x80000
|
2021-04-08 22:56:51 +00:00
|
|
|
)
|
2020-07-22 18:08:08 +00:00
|
|
|
|
2020-05-15 02:58:22 +00:00
|
|
|
// netfilterRunner abstracts helpers to run netfilter commands. It
|
|
|
|
// exists purely to swap out go-iptables for a fake implementation in
|
|
|
|
// tests.
|
2020-05-14 02:17:19 +00:00
|
|
|
type netfilterRunner interface {
|
|
|
|
Insert(table, chain string, pos int, args ...string) error
|
|
|
|
Append(table, chain string, args ...string) error
|
|
|
|
Exists(table, chain string, args ...string) (bool, error)
|
|
|
|
Delete(table, chain string, args ...string) error
|
|
|
|
ClearChain(table, chain string) error
|
|
|
|
NewChain(table, chain string) error
|
|
|
|
DeleteChain(table, chain string) error
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
type linuxRouter struct {
|
2022-08-04 04:51:02 +00:00
|
|
|
closed atomic.Bool
|
2022-03-16 23:27:57 +00:00
|
|
|
logf func(fmt string, args ...any)
|
2020-05-13 22:35:22 +00:00
|
|
|
tunname string
|
2023-04-18 21:26:58 +00:00
|
|
|
netMon *netmon.Monitor
|
|
|
|
unregNetMon func()
|
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
|
|
|
addrs map[netip.Prefix]bool
|
|
|
|
routes map[netip.Prefix]bool
|
|
|
|
localRoutes map[netip.Prefix]bool
|
2020-05-13 22:35:22 +00:00
|
|
|
snatSubnetRoutes bool
|
2021-02-04 21:12:42 +00:00
|
|
|
netfilterMode preftype.NetfilterMode
|
2020-03-03 20:38:51 +00:00
|
|
|
|
2021-07-20 21:10:52 +00:00
|
|
|
// ruleRestorePending is whether a timer has been started to
|
|
|
|
// restore deleted ip rules.
|
2022-08-04 04:51:02 +00:00
|
|
|
ruleRestorePending atomic.Bool
|
2021-07-20 21:10:52 +00:00
|
|
|
ipRuleFixLimiter *rate.Limiter
|
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
// Various feature checks for the network stack.
|
2021-11-19 19:05:34 +00:00
|
|
|
ipRuleAvailable bool // whether kernel was built with IP_MULTIPLE_TABLES
|
2020-09-22 21:55:28 +00:00
|
|
|
v6Available bool
|
|
|
|
v6NATAvailable bool
|
2022-09-12 20:29:41 +00:00
|
|
|
fwmaskWorks bool // whether we can use 'ip rule...fwmark <mark>/<mask>'
|
2020-09-22 21:55:28 +00:00
|
|
|
|
2022-09-09 21:23:48 +00:00
|
|
|
// ipPolicyPrefBase is the base priority at which ip rules are installed.
|
|
|
|
ipPolicyPrefBase int
|
|
|
|
|
2020-05-14 02:17:19 +00:00
|
|
|
ipt4 netfilterRunner
|
2020-09-22 00:49:44 +00:00
|
|
|
ipt6 netfilterRunner
|
2020-05-14 02:17:19 +00:00
|
|
|
cmd commandRunner
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
func newUserspaceRouter(logf logger.Logf, tunDev tun.Device, netMon *netmon.Monitor) (Router, error) {
|
2020-02-14 23:03:25 +00:00
|
|
|
tunname, err := tunDev.Name()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-03 20:38:51 +00:00
|
|
|
ipt4, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-19 19:17:03 +00:00
|
|
|
v6err := checkIPv6(logf)
|
2020-12-29 16:26:17 +00:00
|
|
|
if v6err != nil {
|
2021-02-01 22:08:46 +00:00
|
|
|
logf("disabling tunneled IPv6 due to system IPv6 config: %v", v6err)
|
2020-12-29 16:26:17 +00:00
|
|
|
}
|
|
|
|
supportsV6 := v6err == nil
|
2020-09-28 23:46:39 +00:00
|
|
|
supportsV6NAT := supportsV6 && supportsV6NAT()
|
2020-12-29 16:26:17 +00:00
|
|
|
if supportsV6 {
|
|
|
|
logf("v6nat = %v", supportsV6NAT)
|
|
|
|
}
|
2020-09-28 23:46:39 +00:00
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
var ipt6 netfilterRunner
|
2020-09-28 23:46:39 +00:00
|
|
|
if supportsV6 {
|
2020-09-22 21:55:28 +00:00
|
|
|
// The iptables package probes for `ip6tables` and errors out
|
|
|
|
// if unavailable. We want that to be a non-fatal error.
|
|
|
|
ipt6, err = iptables.NewWithProtocol(iptables.ProtocolIPv6)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-22 00:49:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 18:44:35 +00:00
|
|
|
cmd := osCommandRunner{
|
2021-10-18 17:13:36 +00:00
|
|
|
ambientCapNetAdmin: useAmbientCaps(),
|
2021-08-18 18:44:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
return newUserspaceRouterAdvanced(logf, tunname, netMon, ipt4, ipt6, cmd, supportsV6, supportsV6NAT)
|
2020-05-14 02:17:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
func newUserspaceRouterAdvanced(logf logger.Logf, tunname string, netMon *netmon.Monitor, netfilter4, netfilter6 netfilterRunner, cmd commandRunner, supportsV6, supportsV6NAT bool) (Router, error) {
|
2021-07-20 21:10:52 +00:00
|
|
|
r := &linuxRouter{
|
2020-09-22 21:55:28 +00:00
|
|
|
logf: logf,
|
|
|
|
tunname: tunname,
|
2021-02-04 21:12:42 +00:00
|
|
|
netfilterMode: netfilterOff,
|
2023-04-18 21:26:58 +00:00
|
|
|
netMon: netMon,
|
2020-09-22 21:55:28 +00:00
|
|
|
|
2021-11-01 22:52:24 +00:00
|
|
|
v6Available: supportsV6,
|
|
|
|
v6NATAvailable: supportsV6NAT,
|
2020-09-22 21:55:28 +00:00
|
|
|
|
|
|
|
ipt4: netfilter4,
|
|
|
|
ipt6: netfilter6,
|
|
|
|
cmd: cmd,
|
2021-07-20 21:10:52 +00:00
|
|
|
|
|
|
|
ipRuleFixLimiter: rate.NewLimiter(rate.Every(5*time.Second), 10),
|
2022-09-09 21:23:48 +00:00
|
|
|
ipPolicyPrefBase: 5200,
|
2021-07-20 21:10:52 +00:00
|
|
|
}
|
2021-11-01 22:52:24 +00:00
|
|
|
if r.useIPCommand() {
|
|
|
|
r.ipRuleAvailable = (cmd.run("ip", "rule") == nil)
|
|
|
|
} else {
|
2021-11-19 19:05:34 +00:00
|
|
|
if rules, err := netlink.RuleList(netlink.FAMILY_V4); err != nil {
|
|
|
|
r.logf("error querying IP rules (does kernel have IP_MULTIPLE_TABLES?): %v", err)
|
|
|
|
r.logf("warning: running without policy routing")
|
|
|
|
} else {
|
2021-11-19 19:17:03 +00:00
|
|
|
r.logf("[v1] policy routing available; found %d rules", len(rules))
|
2021-11-19 19:05:34 +00:00
|
|
|
r.ipRuleAvailable = true
|
|
|
|
}
|
2021-11-01 22:52:24 +00:00
|
|
|
}
|
2021-07-20 21:10:52 +00:00
|
|
|
|
2022-09-12 20:29:41 +00:00
|
|
|
// To be a good denizen of the 4-byte 'fwmark' bitspace on every packet, we try to
|
|
|
|
// only use the third byte. However, support for masking to part of the fwmark bitspace
|
|
|
|
// was only added to busybox in 1.33.0. As such, we want to detect older versions and
|
|
|
|
// not issue such a stanza.
|
|
|
|
var err error
|
|
|
|
if r.fwmaskWorks, err = ipCmdSupportsFwmask(); err != nil {
|
|
|
|
r.logf("failed to determine ip command fwmask support: %v", err)
|
|
|
|
}
|
|
|
|
if r.fwmaskWorks {
|
|
|
|
r.logf("[v1] ip command supports fwmark masks")
|
|
|
|
} else {
|
|
|
|
r.logf("[v1] ip command does NOT support fwmark masks")
|
|
|
|
}
|
|
|
|
|
2022-09-09 21:23:48 +00:00
|
|
|
// A common installation of OpenWRT involves use of the 'mwan3' package.
|
|
|
|
// This package installs ip-tables rules like:
|
|
|
|
// -A mwan3_fallback_policy -m mark --mark 0x0/0x3f00 -j MARK --set-xmark 0x100/0x3f00
|
|
|
|
//
|
|
|
|
// which coupled with an ip rule:
|
|
|
|
// 2001: from all fwmark 0x100/0x3f00 lookup 1
|
|
|
|
//
|
|
|
|
// has the effect of gobbling tailscale packets, because tailscale by default installs
|
|
|
|
// its policy routing rules at priority 52xx.
|
|
|
|
//
|
|
|
|
// As such, if we are running on openWRT, detect a mwan3 config, AND detect a rule
|
|
|
|
// with a preference 2001 (corresponding to the first interface wman3 manages), we
|
2022-09-25 18:29:55 +00:00
|
|
|
// shift the priority of our policies to 13xx. This effectively puts us between mwan3's
|
2022-09-09 21:23:48 +00:00
|
|
|
// permit-by-src-ip rules and mwan3 lookup of its own routing table which would drop
|
|
|
|
// the packet.
|
|
|
|
isMWAN3, err := checkOpenWRTUsingMWAN3()
|
|
|
|
if err != nil {
|
|
|
|
r.logf("error checking mwan3 installation: %v", err)
|
|
|
|
} else if isMWAN3 {
|
|
|
|
r.ipPolicyPrefBase = 1300
|
|
|
|
r.logf("mwan3 on openWRT detected, switching policy base priority to 1300")
|
|
|
|
}
|
|
|
|
|
2023-03-03 05:04:01 +00:00
|
|
|
r.fixupWSLMTU()
|
|
|
|
|
2021-07-20 21:10:52 +00:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:29:41 +00:00
|
|
|
// ipCmdSupportsFwmask returns true if the system 'ip' binary supports using a
|
|
|
|
// fwmark stanza with a mask specified. To our knowledge, everything except busybox
|
|
|
|
// pre-1.33 supports this.
|
|
|
|
func ipCmdSupportsFwmask() (bool, error) {
|
|
|
|
ipPath, err := exec.LookPath("ip")
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("lookpath: %v", err)
|
|
|
|
}
|
|
|
|
stat, err := os.Lstat(ipPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("lstat: %v", err)
|
|
|
|
}
|
|
|
|
if stat.Mode()&os.ModeSymlink == 0 {
|
|
|
|
// Not a symlink, so can't be busybox. Must be regular ip utility.
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
linkDest, err := os.Readlink(ipPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !strings.Contains(strings.ToLower(linkDest), "busybox") {
|
|
|
|
// Not busybox, presumably supports fwmark masks.
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, the ip utility is a busybox version with an
|
|
|
|
// unknown version.
|
|
|
|
// We run `ip --version` and look for the busybox banner (which
|
|
|
|
// is a stable 'BusyBox vX.Y.Z (<builddate>)' string) to determine
|
|
|
|
// the version.
|
|
|
|
out, err := exec.Command("ip", "--version").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
major, minor, _, err := busyboxParseVersion(string(out))
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support for masks added in 1.33.0.
|
|
|
|
switch {
|
|
|
|
case major > 1:
|
|
|
|
return true, nil
|
|
|
|
case major == 1 && minor >= 33:
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func busyboxParseVersion(output string) (major, minor, patch int, err error) {
|
|
|
|
bannerStart := strings.Index(output, "BusyBox v")
|
|
|
|
if bannerStart < 0 {
|
|
|
|
return 0, 0, 0, errors.New("missing BusyBox banner")
|
|
|
|
}
|
|
|
|
bannerEnd := bannerStart + len("BusyBox v")
|
|
|
|
|
|
|
|
end := strings.Index(output[bannerEnd:], " ")
|
|
|
|
if end < 0 {
|
|
|
|
return 0, 0, 0, errors.New("missing end delimiter")
|
|
|
|
}
|
|
|
|
|
|
|
|
elements := strings.Split(output[bannerEnd:bannerEnd+end], ".")
|
|
|
|
if len(elements) < 3 {
|
|
|
|
return 0, 0, 0, fmt.Errorf("expected 3 version elements, got %d", len(elements))
|
|
|
|
}
|
|
|
|
|
|
|
|
if major, err = strconv.Atoi(elements[0]); err != nil {
|
|
|
|
return 0, 0, 0, fmt.Errorf("parsing major: %v", err)
|
|
|
|
}
|
|
|
|
if minor, err = strconv.Atoi(elements[1]); err != nil {
|
|
|
|
return 0, 0, 0, fmt.Errorf("parsing minor: %v", err)
|
|
|
|
}
|
|
|
|
if patch, err = strconv.Atoi(elements[2]); err != nil {
|
|
|
|
return 0, 0, 0, fmt.Errorf("parsing patch: %v", err)
|
|
|
|
}
|
|
|
|
return major, minor, patch, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:13:36 +00:00
|
|
|
func useAmbientCaps() bool {
|
|
|
|
if distro.Get() != distro.Synology {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-18 15:47:37 +00:00
|
|
|
return distro.DSMVersion() >= 7
|
2021-10-18 17:13:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 19:49:39 +00:00
|
|
|
var forceIPCommand = envknob.RegisterBool("TS_DEBUG_USE_IP_COMMAND")
|
2021-11-18 15:46:37 +00:00
|
|
|
|
2021-10-28 04:36:29 +00:00
|
|
|
// useIPCommand reports whether r should use the "ip" command (or its
|
|
|
|
// fake commandRunner for tests) instead of netlink.
|
|
|
|
func (r *linuxRouter) useIPCommand() bool {
|
2021-11-01 22:52:24 +00:00
|
|
|
if r.cmd == nil {
|
|
|
|
panic("invalid init")
|
|
|
|
}
|
2022-09-14 19:49:39 +00:00
|
|
|
if forceIPCommand() {
|
2021-11-18 15:46:37 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-10-28 04:36:29 +00:00
|
|
|
// In the future we might need to fall back to using the "ip"
|
|
|
|
// command if, say, netlink is blocked somewhere but the ip
|
|
|
|
// command is allowed to use netlink. For now we only use the ip
|
|
|
|
// command runner in tests.
|
|
|
|
_, ok := r.cmd.(osCommandRunner)
|
|
|
|
return !ok
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
// onIPRuleDeleted is the callback from the network monitor for when an IP
|
|
|
|
// policy rule is deleted. See Issue 1591.
|
2021-07-20 21:10:52 +00:00
|
|
|
//
|
|
|
|
// If an ip rule is deleted (with pref number 52xx, as Tailscale sets), then
|
|
|
|
// set a timer to restore our rules, in case they were deleted. The timer lets
|
|
|
|
// us do one fixup in response to a batch of rule deletes. It also lets us
|
|
|
|
// delay arbitrarily to prevent a high-speed fight over the rule between
|
2021-10-13 00:28:44 +00:00
|
|
|
// competing processes. (Although empirically, systemd doesn't fight us
|
2021-07-20 21:10:52 +00:00
|
|
|
// like that... yet.)
|
|
|
|
//
|
|
|
|
// Note that we don't care about the table number. We don't strictly even care
|
|
|
|
// about the priority number. We could just do this in response to any netlink
|
|
|
|
// change. Filtering by known priority ranges cuts back on some logspam.
|
|
|
|
func (r *linuxRouter) onIPRuleDeleted(table uint8, priority uint32) {
|
2022-09-09 21:23:48 +00:00
|
|
|
if int(priority) < r.ipPolicyPrefBase || int(priority) >= (r.ipPolicyPrefBase+100) {
|
2021-07-20 21:10:52 +00:00
|
|
|
// Not our rule.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !r.ruleRestorePending.Swap(true) {
|
|
|
|
// Another timer is already pending.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rr := r.ipRuleFixLimiter.Reserve()
|
|
|
|
if !rr.OK() {
|
|
|
|
r.ruleRestorePending.Swap(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.AfterFunc(rr.Delay()+250*time.Millisecond, func() {
|
2022-08-04 04:51:02 +00:00
|
|
|
if r.ruleRestorePending.Swap(false) && !r.closed.Load() {
|
2021-07-20 21:10:52 +00:00
|
|
|
r.logf("somebody (likely systemd-networkd) deleted ip rules; restoring Tailscale's")
|
|
|
|
r.justAddIPRules()
|
|
|
|
}
|
|
|
|
})
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *linuxRouter) Up() error {
|
2023-04-18 21:26:58 +00:00
|
|
|
if r.unregNetMon == nil && r.netMon != nil {
|
|
|
|
r.unregNetMon = r.netMon.RegisterRuleDeleteCallback(r.onIPRuleDeleted)
|
2021-07-20 21:10:52 +00:00
|
|
|
}
|
2020-05-31 08:31:01 +00:00
|
|
|
if err := r.addIPRules(); err != nil {
|
2021-11-19 18:33:15 +00:00
|
|
|
return fmt.Errorf("adding IP rules: %w", err)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2021-02-04 21:12:42 +00:00
|
|
|
if err := r.setNetfilterMode(netfilterOff); err != nil {
|
2021-11-19 18:33:15 +00:00
|
|
|
return fmt.Errorf("setting netfilter mode: %w", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
if err := r.upInterface(); err != nil {
|
2021-11-19 18:33:15 +00:00
|
|
|
return fmt.Errorf("bringing interface up: %w", err)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-01 05:34:49 +00:00
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
func (r *linuxRouter) Close() error {
|
2022-08-04 04:51:02 +00:00
|
|
|
r.closed.Store(true)
|
2023-04-18 21:26:58 +00:00
|
|
|
if r.unregNetMon != nil {
|
|
|
|
r.unregNetMon()
|
2021-07-20 21:10:52 +00:00
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
if err := r.downInterface(); err != nil {
|
2020-05-01 05:34:49 +00:00
|
|
|
return err
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-05-31 08:31:01 +00:00
|
|
|
if err := r.delIPRules(); err != nil {
|
2020-05-02 01:55:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-04 21:12:42 +00:00
|
|
|
if err := r.setNetfilterMode(netfilterOff); err != nil {
|
2020-05-02 01:55:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-08 22:56:51 +00:00
|
|
|
if err := r.delRoutes(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
|
2020-05-08 00:18:18 +00:00
|
|
|
r.addrs = nil
|
|
|
|
r.routes = nil
|
2021-04-08 22:56:51 +00:00
|
|
|
r.localRoutes = nil
|
2020-05-02 01:55:38 +00:00
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 01:07:13 +00:00
|
|
|
// Set implements the Router interface.
|
2020-05-12 07:08:52 +00:00
|
|
|
func (r *linuxRouter) Set(cfg *Config) error {
|
2020-09-17 20:40:22 +00:00
|
|
|
var errs []error
|
2020-05-12 07:08:52 +00:00
|
|
|
if cfg == nil {
|
|
|
|
cfg = &shutdownConfig
|
|
|
|
}
|
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.setNetfilterMode(cfg.NetfilterMode); err != nil {
|
2020-09-17 20:40:22 +00:00
|
|
|
errs = append(errs, err)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 22:56:51 +00:00
|
|
|
newLocalRoutes, err := cidrDiff("localRoute", r.localRoutes, cfg.LocalRoutes, r.addThrowRoute, r.delThrowRoute, r.logf)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
r.localRoutes = newLocalRoutes
|
|
|
|
|
2020-09-15 18:14:33 +00:00
|
|
|
newRoutes, err := cidrDiff("route", r.routes, cfg.Routes, r.addRoute, r.delRoute, r.logf)
|
2020-05-13 22:35:22 +00:00
|
|
|
if err != nil {
|
2020-09-17 20:40:22 +00:00
|
|
|
errs = append(errs, err)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-09-15 18:14:33 +00:00
|
|
|
r.routes = newRoutes
|
2020-05-13 22:35:22 +00:00
|
|
|
|
2020-09-15 18:14:33 +00:00
|
|
|
newAddrs, err := cidrDiff("addr", r.addrs, cfg.LocalAddrs, r.addAddress, r.delAddress, r.logf)
|
2020-05-13 22:35:22 +00:00
|
|
|
if err != nil {
|
2020-09-17 20:40:22 +00:00
|
|
|
errs = append(errs, err)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-09-15 18:14:33 +00:00
|
|
|
r.addrs = newAddrs
|
2020-05-13 22:35:22 +00:00
|
|
|
|
2020-05-11 20:16:52 +00:00
|
|
|
switch {
|
2020-05-13 22:35:22 +00:00
|
|
|
case cfg.SNATSubnetRoutes == r.snatSubnetRoutes:
|
2020-05-11 20:16:52 +00:00
|
|
|
// state already correct, nothing to do.
|
2020-05-13 22:35:22 +00:00
|
|
|
case cfg.SNATSubnetRoutes:
|
|
|
|
if err := r.addSNATRule(); err != nil {
|
2020-09-17 20:40:22 +00:00
|
|
|
errs = append(errs, err)
|
2020-05-11 20:16:52 +00:00
|
|
|
}
|
|
|
|
default:
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.delSNATRule(); err != nil {
|
2020-09-17 20:40:22 +00:00
|
|
|
errs = append(errs, err)
|
2020-05-11 20:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-13 22:35:22 +00:00
|
|
|
r.snatSubnetRoutes = cfg.SNATSubnetRoutes
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2021-11-02 21:30:48 +00:00
|
|
|
return multierr.New(errs...)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setNetfilterMode switches the router to the given netfilter
|
|
|
|
// mode. Netfilter state is created or deleted appropriately to
|
|
|
|
// reflect the new mode, and r.snatSubnetRoutes is updated to reflect
|
|
|
|
// the current state of subnet SNATing.
|
2021-02-04 21:12:42 +00:00
|
|
|
func (r *linuxRouter) setNetfilterMode(mode preftype.NetfilterMode) error {
|
2020-09-11 02:55:09 +00:00
|
|
|
if distro.Get() == distro.Synology {
|
2021-02-04 21:12:42 +00:00
|
|
|
mode = netfilterOff
|
2020-09-11 02:55:09 +00:00
|
|
|
}
|
2020-05-13 22:35:22 +00:00
|
|
|
if r.netfilterMode == mode {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Depending on the netfilter mode we switch from and to, we may
|
|
|
|
// have created the Tailscale netfilter chains. If so, we have to
|
|
|
|
// go back through existing router state, and add the netfilter
|
|
|
|
// rules for that state.
|
|
|
|
//
|
|
|
|
// This bool keeps track of whether the current state transition
|
|
|
|
// is one that requires adding rules of existing state.
|
|
|
|
reprocess := false
|
|
|
|
|
|
|
|
switch mode {
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterOff:
|
2020-05-28 09:52:33 +00:00
|
|
|
switch r.netfilterMode {
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterNoDivert:
|
2020-05-28 09:52:33 +00:00
|
|
|
if err := r.delNetfilterBase(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-28 10:59:16 +00:00
|
|
|
if err := r.delNetfilterChains(); err != nil {
|
|
|
|
r.logf("note: %v", err)
|
|
|
|
// harmless, continue.
|
|
|
|
// This can happen if someone left a ref to
|
|
|
|
// this table somewhere else.
|
|
|
|
}
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterOn:
|
2020-05-28 09:52:33 +00:00
|
|
|
if err := r.delNetfilterHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := r.delNetfilterBase(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-28 10:59:16 +00:00
|
|
|
if err := r.delNetfilterChains(); err != nil {
|
|
|
|
r.logf("note: %v", err)
|
|
|
|
// harmless, continue.
|
|
|
|
// This can happen if someone left a ref to
|
|
|
|
// this table somewhere else.
|
|
|
|
}
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
r.snatSubnetRoutes = false
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterNoDivert:
|
2020-05-13 22:35:22 +00:00
|
|
|
switch r.netfilterMode {
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterOff:
|
2020-05-13 22:35:22 +00:00
|
|
|
reprocess = true
|
2020-05-28 10:59:16 +00:00
|
|
|
if err := r.addNetfilterChains(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.addNetfilterBase(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.snatSubnetRoutes = false
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterOn:
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.delNetfilterHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterOn:
|
2020-05-28 10:59:16 +00:00
|
|
|
// Because of bugs in old version of iptables-compat,
|
|
|
|
// we can't add a "-j ts-forward" rule to FORWARD
|
|
|
|
// while ts-forward contains an "-m mark" rule. But
|
|
|
|
// we can add the row *before* populating ts-forward.
|
|
|
|
// So we have to delNetFilterBase, then add the hooks,
|
|
|
|
// then re-addNetFilterBase, just in case.
|
2020-05-13 22:35:22 +00:00
|
|
|
switch r.netfilterMode {
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterOff:
|
2020-05-13 22:35:22 +00:00
|
|
|
reprocess = true
|
2020-05-28 10:59:16 +00:00
|
|
|
if err := r.addNetfilterChains(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := r.delNetfilterBase(); err != nil {
|
2020-05-13 22:35:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := r.addNetfilterHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-28 10:59:16 +00:00
|
|
|
if err := r.addNetfilterBase(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-13 22:35:22 +00:00
|
|
|
r.snatSubnetRoutes = false
|
2021-02-04 21:12:42 +00:00
|
|
|
case netfilterNoDivert:
|
2020-05-28 10:59:16 +00:00
|
|
|
reprocess = true
|
|
|
|
if err := r.delNetfilterBase(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.addNetfilterHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-28 10:59:16 +00:00
|
|
|
if err := r.addNetfilterBase(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.snatSubnetRoutes = false
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("unhandled netfilter mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
r.netfilterMode = mode
|
|
|
|
|
|
|
|
if !reprocess {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for cidr := range r.addrs {
|
2022-07-25 03:08:42 +00:00
|
|
|
if err := r.addLoopbackRule(cidr.Addr()); err != nil {
|
2020-05-13 22:35:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
// addAddress adds an IP/mask to the tunnel interface. Fails if the
|
|
|
|
// address is already assigned to the interface, or if the addition
|
|
|
|
// fails.
|
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 (r *linuxRouter) addAddress(addr netip.Prefix) error {
|
2022-07-25 03:08:42 +00:00
|
|
|
if !r.v6Available && addr.Addr().Is6() {
|
2021-01-28 16:06:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-28 04:36:29 +00:00
|
|
|
if r.useIPCommand() {
|
|
|
|
if err := r.cmd.run("ip", "addr", "add", addr.String(), "dev", r.tunname); err != nil {
|
|
|
|
return fmt.Errorf("adding address %q to tunnel interface: %w", addr, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
link, err := r.link()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("adding address %v, %w", addr, err)
|
|
|
|
}
|
|
|
|
if err := netlink.AddrReplace(link, nlAddrOfPrefix(addr)); err != nil {
|
|
|
|
return fmt.Errorf("adding address %v from tunnel interface: %w", addr, err)
|
|
|
|
}
|
2020-05-11 23:41:29 +00:00
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if err := r.addLoopbackRule(addr.Addr()); err != nil {
|
2020-05-11 23:41:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
// delAddress removes an IP/mask from the tunnel interface. Fails if
|
|
|
|
// the address is not assigned to the interface, or if the removal
|
|
|
|
// fails.
|
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 (r *linuxRouter) delAddress(addr netip.Prefix) error {
|
2022-07-25 03:08:42 +00:00
|
|
|
if !r.v6Available && addr.Addr().Is6() {
|
2021-01-28 16:06:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if err := r.delLoopbackRule(addr.Addr()); err != nil {
|
2020-05-11 23:41:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-10-28 04:36:29 +00:00
|
|
|
if r.useIPCommand() {
|
|
|
|
if err := r.cmd.run("ip", "addr", "del", addr.String(), "dev", r.tunname); err != nil {
|
|
|
|
return fmt.Errorf("deleting address %q from tunnel interface: %w", addr, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
link, err := r.link()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("deleting address %v, %w", addr, err)
|
|
|
|
}
|
|
|
|
if err := netlink.AddrDel(link, nlAddrOfPrefix(addr)); err != nil {
|
|
|
|
return fmt.Errorf("deleting address %v from tunnel interface: %w", addr, err)
|
|
|
|
}
|
2020-05-11 23:41:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
// addLoopbackRule adds a firewall rule to permit loopback traffic to
|
|
|
|
// a local Tailscale IP.
|
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 (r *linuxRouter) addLoopbackRule(addr netip.Addr) error {
|
2021-02-04 21:12:42 +00:00
|
|
|
if r.netfilterMode == netfilterOff {
|
2020-05-13 22:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 00:05:01 +00:00
|
|
|
|
|
|
|
nf := r.ipt4
|
|
|
|
if addr.Is6() {
|
|
|
|
if !r.v6Available {
|
|
|
|
// IPv6 not available, ignore.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
nf = r.ipt6
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nf.Insert("filter", "ts-input", 1, "-i", "lo", "-s", addr.String(), "-j", "ACCEPT"); err != nil {
|
2020-05-28 09:44:44 +00:00
|
|
|
return fmt.Errorf("adding loopback allow rule for %q: %w", addr, err)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// delLoopbackRule removes the firewall rule permitting loopback
|
|
|
|
// traffic to a Tailscale IP.
|
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 (r *linuxRouter) delLoopbackRule(addr netip.Addr) error {
|
2021-02-04 21:12:42 +00:00
|
|
|
if r.netfilterMode == netfilterOff {
|
2020-05-13 22:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 00:05:01 +00:00
|
|
|
|
|
|
|
nf := r.ipt4
|
|
|
|
if addr.Is6() {
|
|
|
|
if !r.v6Available {
|
|
|
|
// IPv6 not available, ignore.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
nf = r.ipt6
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nf.Delete("filter", "ts-input", "-i", "lo", "-s", addr.String(), "-j", "ACCEPT"); err != nil {
|
2020-05-28 09:44:44 +00:00
|
|
|
return fmt.Errorf("deleting loopback allow rule for %q: %w", addr, err)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 19:22:50 +00:00
|
|
|
// addRoute adds a route for cidr, pointing to the tunnel
|
|
|
|
// interface. Fails if the route already exists, or if adding the
|
2020-05-02 01:55:38 +00:00
|
|
|
// route fails.
|
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 (r *linuxRouter) addRoute(cidr netip.Prefix) error {
|
2022-07-25 03:08:42 +00:00
|
|
|
if !r.v6Available && cidr.Addr().Is6() {
|
2021-10-28 04:36:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if r.useIPCommand() {
|
|
|
|
return r.addRouteDef([]string{normalizeCIDR(cidr), "dev", r.tunname}, cidr)
|
|
|
|
}
|
|
|
|
linkIndex, err := r.linkIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.RouteReplace(&netlink.Route{
|
|
|
|
LinkIndex: linkIndex,
|
2022-07-25 03:08:42 +00:00
|
|
|
Dst: netipx.PrefixIPNet(cidr.Masked()),
|
2021-10-28 04:36:29 +00:00
|
|
|
Table: r.routeTable(),
|
|
|
|
})
|
2021-04-08 22:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// addThrowRoute adds a throw route for the provided cidr.
|
|
|
|
// This has the effect that lookup in the routing table is terminated
|
|
|
|
// pretending that no route was found. Fails if the route already exists,
|
|
|
|
// or if adding the route fails.
|
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 (r *linuxRouter) addThrowRoute(cidr netip.Prefix) error {
|
2021-04-08 22:56:51 +00:00
|
|
|
if !r.ipRuleAvailable {
|
2021-01-28 16:06:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if !r.v6Available && cidr.Addr().Is6() {
|
2021-10-28 04:36:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if r.useIPCommand() {
|
|
|
|
return r.addRouteDef([]string{"throw", normalizeCIDR(cidr)}, cidr)
|
|
|
|
}
|
|
|
|
err := netlink.RouteReplace(&netlink.Route{
|
2022-07-25 03:08:42 +00:00
|
|
|
Dst: netipx.PrefixIPNet(cidr.Masked()),
|
2021-10-28 20:11:03 +00:00
|
|
|
Table: tailscaleRouteTable.num,
|
2021-10-28 04:36:29 +00:00
|
|
|
Type: unix.RTN_THROW,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
r.logf("THROW ERROR adding %v: %#v", cidr, err)
|
|
|
|
}
|
|
|
|
return err
|
2021-04-08 22:56:51 +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
|
|
|
func (r *linuxRouter) addRouteDef(routeDef []string, cidr netip.Prefix) error {
|
2022-07-25 03:08:42 +00:00
|
|
|
if !r.v6Available && cidr.Addr().Is6() {
|
2021-04-08 22:56:51 +00:00
|
|
|
return nil
|
2020-05-31 08:31:01 +00:00
|
|
|
}
|
2021-04-08 22:56:51 +00:00
|
|
|
args := append([]string{"ip", "route", "add"}, routeDef...)
|
2020-05-31 08:31:01 +00:00
|
|
|
if r.ipRuleAvailable {
|
2021-10-28 20:11:03 +00:00
|
|
|
args = append(args, "table", tailscaleRouteTable.ipCmdArg())
|
2020-05-31 08:31:01 +00:00
|
|
|
}
|
2021-10-14 20:17:06 +00:00
|
|
|
err := r.cmd.run(args...)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-28 04:36:29 +00:00
|
|
|
// This is an ugly hack to detect failure to add a route that
|
|
|
|
// already exists (as happens in when we're racing to add
|
|
|
|
// kernel-maintained routes when enabling exit nodes w/o Local
|
|
|
|
// LAN access, Issue 3060). Fortunately in the common case we
|
|
|
|
// use netlink directly instead and don't exercise this code.
|
2021-10-14 20:17:06 +00:00
|
|
|
if errCode(err) == 2 && strings.Contains(err.Error(), "RTNETLINK answers: File exists") {
|
|
|
|
r.logf("ignoring route add of %v; already exists", cidr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 22:22:03 +00:00
|
|
|
var (
|
|
|
|
errESRCH error = syscall.ESRCH
|
|
|
|
errENOENT error = syscall.ENOENT
|
|
|
|
errEEXIST error = syscall.EEXIST
|
|
|
|
)
|
2021-10-28 04:36:29 +00:00
|
|
|
|
2020-05-07 19:22:50 +00:00
|
|
|
// delRoute removes the route for cidr pointing to the tunnel
|
|
|
|
// interface. Fails if the route doesn't exist, or if removing the
|
|
|
|
// route fails.
|
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 (r *linuxRouter) delRoute(cidr netip.Prefix) error {
|
2022-07-25 03:08:42 +00:00
|
|
|
if !r.v6Available && cidr.Addr().Is6() {
|
2021-10-28 04:36:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if r.useIPCommand() {
|
|
|
|
return r.delRouteDef([]string{normalizeCIDR(cidr), "dev", r.tunname}, cidr)
|
|
|
|
}
|
|
|
|
linkIndex, err := r.linkIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = netlink.RouteDel(&netlink.Route{
|
|
|
|
LinkIndex: linkIndex,
|
2022-07-25 03:08:42 +00:00
|
|
|
Dst: netipx.PrefixIPNet(cidr.Masked()),
|
2021-10-28 04:36:29 +00:00
|
|
|
Table: r.routeTable(),
|
|
|
|
})
|
|
|
|
if errors.Is(err, errESRCH) {
|
|
|
|
// Didn't exist to begin with.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2021-04-08 22:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delThrowRoute removes the throw route for the cidr. Fails if the route
|
|
|
|
// doesn't exist, or if removing the route fails.
|
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 (r *linuxRouter) delThrowRoute(cidr netip.Prefix) error {
|
2021-04-08 22:56:51 +00:00
|
|
|
if !r.ipRuleAvailable {
|
2021-01-28 16:06:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if !r.v6Available && cidr.Addr().Is6() {
|
2021-10-28 04:36:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if r.useIPCommand() {
|
|
|
|
return r.delRouteDef([]string{"throw", normalizeCIDR(cidr)}, cidr)
|
|
|
|
}
|
|
|
|
err := netlink.RouteDel(&netlink.Route{
|
2022-07-25 03:08:42 +00:00
|
|
|
Dst: netipx.PrefixIPNet(cidr.Masked()),
|
2021-10-28 04:36:29 +00:00
|
|
|
Table: r.routeTable(),
|
|
|
|
Type: unix.RTN_THROW,
|
|
|
|
})
|
|
|
|
if errors.Is(err, errESRCH) {
|
|
|
|
// Didn't exist to begin with.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2021-04-08 22:56:51 +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
|
|
|
func (r *linuxRouter) delRouteDef(routeDef []string, cidr netip.Prefix) error {
|
2022-07-25 03:08:42 +00:00
|
|
|
if !r.v6Available && cidr.Addr().Is6() {
|
2021-04-08 22:56:51 +00:00
|
|
|
return nil
|
2020-05-31 08:31:01 +00:00
|
|
|
}
|
2021-04-08 22:56:51 +00:00
|
|
|
args := append([]string{"ip", "route", "del"}, routeDef...)
|
2020-05-31 08:31:01 +00:00
|
|
|
if r.ipRuleAvailable {
|
2021-10-28 20:11:03 +00:00
|
|
|
args = append(args, "table", tailscaleRouteTable.ipCmdArg())
|
2020-05-31 08:31:01 +00:00
|
|
|
}
|
2021-02-18 06:11:59 +00:00
|
|
|
err := r.cmd.run(args...)
|
|
|
|
if err != nil {
|
2021-04-08 22:56:51 +00:00
|
|
|
ok, err := r.hasRoute(routeDef, cidr)
|
2021-02-18 06:11:59 +00:00
|
|
|
if err != nil {
|
|
|
|
r.logf("warning: error checking whether %v even exists after error deleting it: %v", err)
|
|
|
|
} else {
|
|
|
|
if !ok {
|
|
|
|
r.logf("warning: tried to delete route %v but it was already gone; ignoring error", cidr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 dashFam(ip netip.Addr) string {
|
2021-02-18 06:11:59 +00:00
|
|
|
if ip.Is6() {
|
|
|
|
return "-6"
|
|
|
|
}
|
|
|
|
return "-4"
|
|
|
|
}
|
|
|
|
|
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 (r *linuxRouter) hasRoute(routeDef []string, cidr netip.Prefix) (bool, error) {
|
2022-07-25 03:08:42 +00:00
|
|
|
args := append([]string{"ip", dashFam(cidr.Addr()), "route", "show"}, routeDef...)
|
2021-02-18 06:11:59 +00:00
|
|
|
if r.ipRuleAvailable {
|
2021-10-28 20:11:03 +00:00
|
|
|
args = append(args, "table", tailscaleRouteTable.ipCmdArg())
|
2021-02-18 06:11:59 +00:00
|
|
|
}
|
|
|
|
out, err := r.cmd.output(args...)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(out) > 0, nil
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 04:36:29 +00:00
|
|
|
func (r *linuxRouter) link() (netlink.Link, error) {
|
|
|
|
link, err := netlink.LinkByName(r.tunname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to look up link %q: %w", r.tunname, err)
|
|
|
|
}
|
|
|
|
return link, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *linuxRouter) linkIndex() (int, error) {
|
|
|
|
// TODO(bradfitz): cache this? It doesn't change often, and on start-up
|
|
|
|
// hundreds of addRoute calls to add /32s can happen quickly.
|
|
|
|
link, err := r.link()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return link.Attrs().Index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// routeTable returns the route table to use.
|
|
|
|
func (r *linuxRouter) routeTable() int {
|
|
|
|
if r.ipRuleAvailable {
|
2021-10-28 20:11:03 +00:00
|
|
|
return tailscaleRouteTable.num
|
2021-10-28 04:36:29 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-05-28 07:44:09 +00:00
|
|
|
// upInterface brings up the tunnel interface.
|
2020-05-02 01:55:38 +00:00
|
|
|
func (r *linuxRouter) upInterface() error {
|
2021-10-28 04:36:29 +00:00
|
|
|
if r.useIPCommand() {
|
|
|
|
return r.cmd.run("ip", "link", "set", "dev", r.tunname, "up")
|
|
|
|
}
|
|
|
|
link, err := r.link()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bringing interface up, %w", err)
|
|
|
|
}
|
|
|
|
return netlink.LinkSetUp(link)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 07:44:09 +00:00
|
|
|
// downInterface sets the tunnel interface administratively down.
|
2020-05-02 01:55:38 +00:00
|
|
|
func (r *linuxRouter) downInterface() error {
|
2021-10-28 04:36:29 +00:00
|
|
|
if r.useIPCommand() {
|
|
|
|
return r.cmd.run("ip", "link", "set", "dev", r.tunname, "down")
|
|
|
|
}
|
|
|
|
link, err := r.link()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bringing interface down, %w", err)
|
|
|
|
}
|
|
|
|
return netlink.LinkSetDown(link)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 05:04:01 +00:00
|
|
|
// fixupWSLMTU sets the MTU on the eth0 interface to 1360 bytes if running under
|
|
|
|
// WSL, eth0 is the default route, and has the MTU 1280 bytes.
|
|
|
|
func (r *linuxRouter) fixupWSLMTU() {
|
|
|
|
if !distro.IsWSL() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.useIPCommand() {
|
|
|
|
r.logf("fixupWSLMTU: not implemented by ip command")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err := netlink.LinkByName("eth0")
|
|
|
|
if err != nil {
|
|
|
|
r.logf("warning: fixupWSLMTU: could not open eth0: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := netlink.RouteGet(net.IPv4(8, 8, 8, 8))
|
|
|
|
if err != nil || len(routes) == 0 {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("none found")
|
|
|
|
}
|
|
|
|
r.logf("fixupWSLMTU: could not get default route: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if routes[0].LinkIndex != link.Attrs().Index {
|
|
|
|
r.logf("fixupWSLMTU: default route is not via eth0")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if link.Attrs().MTU == 1280 {
|
|
|
|
if err := netlink.LinkSetMTU(link, 1360); err != nil {
|
|
|
|
r.logf("warning: fixupWSLMTU: could not raise eth0 MTU: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 21:52:27 +00:00
|
|
|
// addrFamily is an address family: IPv4 or IPv6.
|
|
|
|
type addrFamily byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
v4 = addrFamily(4)
|
|
|
|
v6 = addrFamily(6)
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f addrFamily) dashArg() string {
|
|
|
|
switch f {
|
|
|
|
case 4:
|
|
|
|
return "-4"
|
|
|
|
case 6:
|
|
|
|
return "-6"
|
|
|
|
}
|
|
|
|
panic("illegal")
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:22:03 +00:00
|
|
|
func (f addrFamily) netlinkInt() int {
|
|
|
|
switch f {
|
|
|
|
case 4:
|
|
|
|
return netlink.FAMILY_V4
|
|
|
|
case 6:
|
|
|
|
return netlink.FAMILY_V6
|
|
|
|
}
|
|
|
|
panic("illegal")
|
|
|
|
}
|
|
|
|
|
2021-10-28 21:52:27 +00:00
|
|
|
func (r *linuxRouter) addrFamilies() []addrFamily {
|
2020-09-22 21:55:28 +00:00
|
|
|
if r.v6Available {
|
2021-10-28 21:52:27 +00:00
|
|
|
return []addrFamily{v4, v6}
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
2021-10-28 21:52:27 +00:00
|
|
|
return []addrFamily{v4}
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 08:31:01 +00:00
|
|
|
// addIPRules adds the policy routing rule that avoids tailscaled
|
2020-05-02 01:55:38 +00:00
|
|
|
// routing loops. If the rule exists and appears to be a
|
|
|
|
// tailscale-managed rule, it is gracefully replaced.
|
2020-05-31 08:31:01 +00:00
|
|
|
func (r *linuxRouter) addIPRules() error {
|
|
|
|
if !r.ipRuleAvailable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 07:44:09 +00:00
|
|
|
// Clear out old rules. After that, any error adding a rule is fatal,
|
|
|
|
// because there should be no reason we add a duplicate.
|
2020-05-31 08:31:01 +00:00
|
|
|
if err := r.delIPRules(); err != nil {
|
2020-05-02 01:55:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-28 07:44:09 +00:00
|
|
|
|
2021-07-20 21:10:52 +00:00
|
|
|
return r.justAddIPRules()
|
|
|
|
}
|
|
|
|
|
2021-10-28 20:11:03 +00:00
|
|
|
// routeTable is a Linux routing table: both its name and number.
|
|
|
|
// See /etc/iproute2/rt_tables.
|
|
|
|
type routeTable struct {
|
|
|
|
name string
|
|
|
|
num int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ipCmdArg returns the string form of the table to pass to the "ip" command.
|
|
|
|
func (rt routeTable) ipCmdArg() string {
|
|
|
|
if rt.num >= 253 {
|
|
|
|
return rt.name
|
|
|
|
}
|
|
|
|
return strconv.Itoa(rt.num)
|
|
|
|
}
|
|
|
|
|
|
|
|
var routeTableByNumber = map[int]routeTable{}
|
|
|
|
|
|
|
|
func newRouteTable(name string, num int) routeTable {
|
|
|
|
rt := routeTable{name, num}
|
|
|
|
routeTableByNumber[num] = rt
|
|
|
|
return rt
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustRouteTable(num int) routeTable {
|
|
|
|
rt, ok := routeTableByNumber[num]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("unknown route table %v", num))
|
|
|
|
}
|
|
|
|
return rt
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
mainRouteTable = newRouteTable("main", 254)
|
|
|
|
defaultRouteTable = newRouteTable("default", 253)
|
|
|
|
|
|
|
|
// tailscaleRouteTable is the routing table number for Tailscale
|
|
|
|
// network routes. See addIPRules for the detailed policy routing
|
|
|
|
// logic that ends up doing lookups within that table.
|
|
|
|
//
|
|
|
|
// NOTE(danderson): We chose 52 because those are the digits above the
|
|
|
|
// letters "TS" on a qwerty keyboard, and 52 is sufficiently unlikely
|
|
|
|
// to be picked by other software.
|
|
|
|
//
|
|
|
|
// NOTE(danderson): You might wonder why we didn't pick some
|
|
|
|
// high table number like 5252, to further avoid the potential
|
|
|
|
// for collisions with other software. Unfortunately,
|
|
|
|
// Busybox's `ip` implementation believes that table numbers
|
|
|
|
// are 8-bit integers, so for maximum compatibility we had to
|
|
|
|
// stay in the 0-255 range even though linux itself supports
|
|
|
|
// larger numbers. (but nowadays we use netlink directly and
|
|
|
|
// aren't affected by the busybox binary's limitations)
|
|
|
|
tailscaleRouteTable = newRouteTable("tailscale", 52)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ipRules are the policy routing rules that Tailscale uses.
|
2022-09-09 21:23:48 +00:00
|
|
|
// The priority is the value represented here added to r.ipPolicyPrefBase,
|
|
|
|
// which is usually 5200.
|
2021-10-28 20:11:03 +00:00
|
|
|
//
|
|
|
|
// NOTE(apenwarr): We leave spaces between each pref number.
|
|
|
|
// This is so the sysadmin can override by inserting rules in
|
|
|
|
// between if they want.
|
|
|
|
//
|
|
|
|
// NOTE(apenwarr): This sequence seems complicated, right?
|
|
|
|
// If we could simply have a rule that said "match packets that
|
|
|
|
// *don't* have this fwmark", then we would only need to add one
|
|
|
|
// link to table 52 and we'd be done. Unfortunately, older kernels
|
|
|
|
// and 'ip rule' implementations (including busybox), don't support
|
|
|
|
// checking for the lack of a fwmark, only the presence. The technique
|
|
|
|
// below works even on very old kernels.
|
|
|
|
var ipRules = []netlink.Rule{
|
|
|
|
// Packets from us, tagged with our fwmark, first try the kernel's
|
|
|
|
// main routing table.
|
|
|
|
{
|
2022-09-09 21:23:48 +00:00
|
|
|
Priority: 10,
|
2021-10-28 20:11:03 +00:00
|
|
|
Mark: tailscaleBypassMarkNum,
|
|
|
|
Table: mainRouteTable.num,
|
|
|
|
},
|
|
|
|
// ...and then we try the 'default' table, for correctness,
|
|
|
|
// even though it's been empty on every Linux system I've ever seen.
|
|
|
|
{
|
2022-09-09 21:23:48 +00:00
|
|
|
Priority: 30,
|
2021-10-28 20:11:03 +00:00
|
|
|
Mark: tailscaleBypassMarkNum,
|
|
|
|
Table: defaultRouteTable.num,
|
|
|
|
},
|
|
|
|
// If neither of those matched (no default route on this system?)
|
|
|
|
// then packets from us should be aborted rather than falling through
|
|
|
|
// to the tailscale routes, because that would create routing loops.
|
|
|
|
{
|
2022-09-09 21:23:48 +00:00
|
|
|
Priority: 50,
|
2021-10-28 20:11:03 +00:00
|
|
|
Mark: tailscaleBypassMarkNum,
|
2021-10-28 22:22:03 +00:00
|
|
|
Type: unix.RTN_UNREACHABLE,
|
2021-10-28 20:11:03 +00:00
|
|
|
},
|
|
|
|
// If we get to this point, capture all packets and send them
|
|
|
|
// through to the tailscale route table. For apps other than us
|
|
|
|
// (ie. with no fwmark set), this is the first routing table, so
|
|
|
|
// it takes precedence over all the others, ie. VPN routes always
|
|
|
|
// beat non-VPN routes.
|
|
|
|
{
|
2022-09-09 21:23:48 +00:00
|
|
|
Priority: 70,
|
2021-10-28 20:11:03 +00:00
|
|
|
Table: tailscaleRouteTable.num,
|
|
|
|
},
|
|
|
|
// If that didn't match, then non-fwmark packets fall through to the
|
|
|
|
// usual rules (pref 32766 and 32767, ie. main and default).
|
|
|
|
}
|
|
|
|
|
2021-07-20 21:10:52 +00:00
|
|
|
// justAddIPRules adds policy routing rule without deleting any first.
|
|
|
|
func (r *linuxRouter) justAddIPRules() error {
|
|
|
|
if !r.ipRuleAvailable {
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-28 22:22:03 +00:00
|
|
|
if r.useIPCommand() {
|
|
|
|
return r.addIPRulesWithIPCommand()
|
|
|
|
}
|
|
|
|
var errAcc error
|
|
|
|
for _, family := range r.addrFamilies() {
|
2022-09-09 21:23:48 +00:00
|
|
|
|
2021-10-28 22:22:03 +00:00
|
|
|
for _, ru := range ipRules {
|
|
|
|
// Note: r is a value type here; safe to mutate it.
|
|
|
|
ru.Family = family.netlinkInt()
|
2022-09-12 20:29:41 +00:00
|
|
|
if ru.Mark != 0 {
|
|
|
|
ru.Mask = tailscaleFwmarkMaskNum
|
|
|
|
}
|
2021-10-28 22:22:03 +00:00
|
|
|
ru.Goto = -1
|
|
|
|
ru.SuppressIfgroup = -1
|
|
|
|
ru.SuppressPrefixlen = -1
|
|
|
|
ru.Flow = -1
|
2022-09-09 21:23:48 +00:00
|
|
|
ru.Priority += r.ipPolicyPrefBase
|
2021-10-28 22:22:03 +00:00
|
|
|
|
|
|
|
err := netlink.RuleAdd(&ru)
|
|
|
|
if errors.Is(err, errEEXIST) {
|
|
|
|
// Ignore dups.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil && errAcc == nil {
|
|
|
|
errAcc = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errAcc
|
|
|
|
}
|
2021-07-20 21:10:52 +00:00
|
|
|
|
2021-10-28 22:22:03 +00:00
|
|
|
func (r *linuxRouter) addIPRulesWithIPCommand() error {
|
2020-06-03 19:36:40 +00:00
|
|
|
rg := newRunGroup(nil, r.cmd)
|
2020-05-28 07:44:09 +00:00
|
|
|
|
2021-10-28 21:52:27 +00:00
|
|
|
for _, family := range r.addrFamilies() {
|
2022-09-09 21:23:48 +00:00
|
|
|
for _, rule := range ipRules {
|
2021-10-28 20:11:03 +00:00
|
|
|
args := []string{
|
2021-10-28 21:52:27 +00:00
|
|
|
"ip", family.dashArg(),
|
2021-10-28 20:11:03 +00:00
|
|
|
"rule", "add",
|
2022-09-09 21:23:48 +00:00
|
|
|
"pref", strconv.Itoa(rule.Priority + r.ipPolicyPrefBase),
|
2021-10-28 20:11:03 +00:00
|
|
|
}
|
2022-09-09 21:23:48 +00:00
|
|
|
if rule.Mark != 0 {
|
2022-09-12 20:29:41 +00:00
|
|
|
if r.fwmaskWorks {
|
|
|
|
args = append(args, "fwmark", fmt.Sprintf("0x%x/%s", rule.Mark, tailscaleFwmarkMask))
|
|
|
|
} else {
|
|
|
|
args = append(args, "fwmark", fmt.Sprintf("0x%x", rule.Mark))
|
|
|
|
}
|
2021-10-28 20:11:03 +00:00
|
|
|
}
|
2022-09-09 21:23:48 +00:00
|
|
|
if rule.Table != 0 {
|
|
|
|
args = append(args, "table", mustRouteTable(rule.Table).ipCmdArg())
|
2021-10-28 22:22:03 +00:00
|
|
|
}
|
2022-09-09 21:23:48 +00:00
|
|
|
if rule.Type == unix.RTN_UNREACHABLE {
|
2021-10-28 20:11:03 +00:00
|
|
|
args = append(args, "type", "unreachable")
|
|
|
|
}
|
|
|
|
rg.Run(args...)
|
|
|
|
}
|
2020-09-22 00:49:44 +00:00
|
|
|
}
|
2020-05-28 07:44:09 +00:00
|
|
|
|
|
|
|
return rg.ErrAcc
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 22:56:51 +00:00
|
|
|
// delRoutes removes any local routes that we added that would not be
|
|
|
|
// cleaned up on interface down.
|
|
|
|
func (r *linuxRouter) delRoutes() error {
|
|
|
|
for rt := range r.localRoutes {
|
|
|
|
if err := r.delThrowRoute(rt); err != nil {
|
|
|
|
r.logf("failed to delete throw route(%q): %v", rt, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// delIPRules removes the policy routing rules that avoid
|
2020-05-02 01:55:38 +00:00
|
|
|
// tailscaled routing loops, if it exists.
|
2020-05-31 08:31:01 +00:00
|
|
|
func (r *linuxRouter) delIPRules() error {
|
|
|
|
if !r.ipRuleAvailable {
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-28 22:22:03 +00:00
|
|
|
if r.useIPCommand() {
|
|
|
|
return r.delIPRulesWithIPCommand()
|
|
|
|
}
|
|
|
|
var errAcc error
|
|
|
|
for _, family := range r.addrFamilies() {
|
|
|
|
for _, ru := range ipRules {
|
|
|
|
// Note: r is a value type here; safe to mutate it.
|
|
|
|
// When deleting rules, we want to be a bit specific (mention which
|
|
|
|
// table we were routing to) but not *too* specific (fwmarks, etc).
|
|
|
|
// That leaves us some flexibility to change these values in later
|
|
|
|
// versions without having ongoing hacks for every possible
|
|
|
|
// combination.
|
|
|
|
ru.Family = family.netlinkInt()
|
|
|
|
ru.Mark = -1
|
|
|
|
ru.Mask = -1
|
|
|
|
ru.Goto = -1
|
|
|
|
ru.SuppressIfgroup = -1
|
|
|
|
ru.SuppressPrefixlen = -1
|
2022-09-12 20:29:41 +00:00
|
|
|
ru.Priority += r.ipPolicyPrefBase
|
2021-10-28 22:22:03 +00:00
|
|
|
|
|
|
|
err := netlink.RuleDel(&ru)
|
|
|
|
if errors.Is(err, errENOENT) {
|
|
|
|
// Didn't exist to begin with.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil && errAcc == nil {
|
|
|
|
errAcc = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errAcc
|
|
|
|
}
|
2020-05-31 08:31:01 +00:00
|
|
|
|
2021-10-28 22:22:03 +00:00
|
|
|
func (r *linuxRouter) delIPRulesWithIPCommand() error {
|
2020-05-28 07:44:09 +00:00
|
|
|
// Error codes: 'ip rule' returns error code 2 if the rule is a
|
|
|
|
// duplicate (add) or not found (del). It returns a different code
|
|
|
|
// for syntax errors. This is also true of busybox.
|
2020-06-03 19:36:40 +00:00
|
|
|
//
|
|
|
|
// Some older versions of iproute2 also return error code 254 for
|
|
|
|
// unknown rules during deletion.
|
|
|
|
rg := newRunGroup([]int{2, 254}, r.cmd)
|
2020-05-28 07:44:09 +00:00
|
|
|
|
2021-10-28 21:52:27 +00:00
|
|
|
for _, family := range r.addrFamilies() {
|
2020-09-22 00:49:44 +00:00
|
|
|
// When deleting rules, we want to be a bit specific (mention which
|
|
|
|
// table we were routing to) but not *too* specific (fwmarks, etc).
|
|
|
|
// That leaves us some flexibility to change these values in later
|
|
|
|
// versions without having ongoing hacks for every possible
|
|
|
|
// combination.
|
2022-09-09 21:23:48 +00:00
|
|
|
for _, rule := range ipRules {
|
2021-10-28 20:11:03 +00:00
|
|
|
args := []string{
|
2021-10-28 21:52:27 +00:00
|
|
|
"ip", family.dashArg(),
|
2021-10-28 20:11:03 +00:00
|
|
|
"rule", "del",
|
2022-09-09 21:23:48 +00:00
|
|
|
"pref", strconv.Itoa(rule.Priority + r.ipPolicyPrefBase),
|
2021-10-28 20:11:03 +00:00
|
|
|
}
|
2022-09-09 21:23:48 +00:00
|
|
|
if rule.Table != 0 {
|
|
|
|
args = append(args, "table", mustRouteTable(rule.Table).ipCmdArg())
|
2021-10-28 20:11:03 +00:00
|
|
|
} else {
|
|
|
|
args = append(args, "type", "unreachable")
|
|
|
|
}
|
|
|
|
rg.Run(args...)
|
|
|
|
}
|
2020-09-22 00:49:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 07:44:09 +00:00
|
|
|
return rg.ErrAcc
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
func (r *linuxRouter) netfilterFamilies() []netfilterRunner {
|
|
|
|
if r.v6Available {
|
|
|
|
return []netfilterRunner{r.ipt4, r.ipt6}
|
|
|
|
}
|
|
|
|
return []netfilterRunner{r.ipt4}
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:59:16 +00:00
|
|
|
// addNetfilterChains creates custom Tailscale chains in netfilter.
|
|
|
|
func (r *linuxRouter) addNetfilterChains() error {
|
2020-09-22 00:49:44 +00:00
|
|
|
create := func(ipt netfilterRunner, table, chain string) error {
|
|
|
|
err := ipt.ClearChain(table, chain)
|
2020-05-28 09:44:44 +00:00
|
|
|
if errCode(err) == 1 {
|
|
|
|
// nonexistent chain. let's create it!
|
2020-09-22 00:49:44 +00:00
|
|
|
return ipt.NewChain(table, chain)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-05-28 09:44:44 +00:00
|
|
|
return fmt.Errorf("setting up %s/%s: %w", table, chain, err)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-22 00:49:44 +00:00
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
for _, ipt := range r.netfilterFamilies() {
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := create(ipt, "filter", "ts-input"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := create(ipt, "filter", "ts-forward"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
if err := create(r.ipt4, "nat", "ts-postrouting"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.v6NATAvailable {
|
|
|
|
if err := create(r.ipt6, "nat", "ts-postrouting"); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-09-22 00:49:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addNetfilterBase adds some basic processing rules to be
|
|
|
|
// supplemented by later calls to other helpers.
|
|
|
|
func (r *linuxRouter) addNetfilterBase() error {
|
|
|
|
if err := r.addNetfilterBase4(); err != nil {
|
2020-05-13 22:35:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
if r.v6Available {
|
|
|
|
if err := r.addNetfilterBase6(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-05-28 10:59:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
|
2020-09-22 00:49:44 +00:00
|
|
|
// addNetfilterBase4 adds some basic IPv4 processing rules to be
|
|
|
|
// supplemented by later calls to other helpers.
|
|
|
|
func (r *linuxRouter) addNetfilterBase4() error {
|
2020-05-13 22:35:22 +00:00
|
|
|
// Only allow CGNAT range traffic to come from tailscale0. There
|
|
|
|
// is an exception carved out for ranges used by ChromeOS, for
|
|
|
|
// which we fall out of the Tailscale chain.
|
|
|
|
//
|
|
|
|
// Note, this will definitely break nodes that end up using the
|
|
|
|
// CGNAT range for other purposes :(.
|
2020-07-07 05:33:29 +00:00
|
|
|
args := []string{"!", "-i", r.tunname, "-s", tsaddr.ChromeOSVMRange().String(), "-j", "RETURN"}
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.ipt4.Append("filter", "ts-input", args...); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return fmt.Errorf("adding %v in v4/filter/ts-input: %w", args, err)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
2020-07-07 05:33:29 +00:00
|
|
|
args = []string{"!", "-i", r.tunname, "-s", tsaddr.CGNATRange().String(), "-j", "DROP"}
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.ipt4.Append("filter", "ts-input", args...); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return fmt.Errorf("adding %v in v4/filter/ts-input: %w", args, err)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 03:19:17 +00:00
|
|
|
// Forward all traffic from the Tailscale interface, and drop
|
|
|
|
// traffic to the tailscale interface by default. We use packet
|
|
|
|
// marks here so both filter/FORWARD and nat/POSTROUTING can match
|
|
|
|
// on these packets of interest.
|
2020-05-13 22:35:22 +00:00
|
|
|
//
|
|
|
|
// In particular, we only want to apply SNAT rules in
|
|
|
|
// nat/POSTROUTING to packets that originated from the Tailscale
|
|
|
|
// interface, but we can't match on the inbound interface in
|
2020-06-03 03:19:17 +00:00
|
|
|
// POSTROUTING. So instead, we match on the inbound interface in
|
|
|
|
// filter/FORWARD, and set a packet mark that nat/POSTROUTING can
|
|
|
|
// use to effectively run that same test again.
|
2022-09-12 20:29:41 +00:00
|
|
|
args = []string{"-i", r.tunname, "-j", "MARK", "--set-mark", tailscaleSubnetRouteMark + "/" + tailscaleFwmarkMask}
|
2020-06-03 03:19:17 +00:00
|
|
|
if err := r.ipt4.Append("filter", "ts-forward", args...); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return fmt.Errorf("adding %v in v4/filter/ts-forward: %w", args, err)
|
2020-06-03 03:19:17 +00:00
|
|
|
}
|
2022-09-12 20:29:41 +00:00
|
|
|
args = []string{"-m", "mark", "--mark", tailscaleSubnetRouteMark + "/" + tailscaleFwmarkMask, "-j", "ACCEPT"}
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.ipt4.Append("filter", "ts-forward", args...); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return fmt.Errorf("adding %v in v4/filter/ts-forward: %w", args, err)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
2020-07-07 05:33:29 +00:00
|
|
|
args = []string{"-o", r.tunname, "-s", tsaddr.CGNATRange().String(), "-j", "DROP"}
|
2020-06-03 03:19:17 +00:00
|
|
|
if err := r.ipt4.Append("filter", "ts-forward", args...); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return fmt.Errorf("adding %v in v4/filter/ts-forward: %w", args, err)
|
2020-06-03 03:19:17 +00:00
|
|
|
}
|
|
|
|
args = []string{"-o", r.tunname, "-j", "ACCEPT"}
|
2020-05-13 22:35:22 +00:00
|
|
|
if err := r.ipt4.Append("filter", "ts-forward", args...); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return fmt.Errorf("adding %v in v4/filter/ts-forward: %w", args, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addNetfilterBase4 adds some basic IPv6 processing rules to be
|
|
|
|
// supplemented by later calls to other helpers.
|
|
|
|
func (r *linuxRouter) addNetfilterBase6() error {
|
|
|
|
// TODO: only allow traffic from Tailscale's ULA range to come
|
|
|
|
// from tailscale0.
|
|
|
|
|
2022-09-12 20:29:41 +00:00
|
|
|
args := []string{"-i", r.tunname, "-j", "MARK", "--set-mark", tailscaleSubnetRouteMark + "/" + tailscaleFwmarkMask}
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := r.ipt6.Append("filter", "ts-forward", args...); err != nil {
|
|
|
|
return fmt.Errorf("adding %v in v6/filter/ts-forward: %w", args, err)
|
|
|
|
}
|
2022-09-12 20:29:41 +00:00
|
|
|
args = []string{"-m", "mark", "--mark", tailscaleSubnetRouteMark + "/" + tailscaleFwmarkMask, "-j", "ACCEPT"}
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := r.ipt6.Append("filter", "ts-forward", args...); err != nil {
|
|
|
|
return fmt.Errorf("adding %v in v6/filter/ts-forward: %w", args, err)
|
|
|
|
}
|
|
|
|
// TODO: drop forwarded traffic to tailscale0 from tailscale's ULA
|
|
|
|
// (see corresponding IPv4 CGNAT rule).
|
|
|
|
args = []string{"-o", r.tunname, "-j", "ACCEPT"}
|
|
|
|
if err := r.ipt6.Append("filter", "ts-forward", args...); err != nil {
|
|
|
|
return fmt.Errorf("adding %v in v6/filter/ts-forward: %w", args, err)
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 01:55:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:59:16 +00:00
|
|
|
// delNetfilterChains removes the custom Tailscale chains from netfilter.
|
|
|
|
func (r *linuxRouter) delNetfilterChains() error {
|
2020-09-22 00:49:44 +00:00
|
|
|
del := func(ipt netfilterRunner, table, chain string) error {
|
|
|
|
if err := ipt.ClearChain(table, chain); err != nil {
|
2020-05-28 09:44:44 +00:00
|
|
|
if errCode(err) == 1 {
|
|
|
|
// nonexistent chain. That's fine, since it's
|
|
|
|
// the desired state anyway.
|
2020-05-13 22:35:22 +00:00
|
|
|
return nil
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-05-28 09:44:44 +00:00
|
|
|
return fmt.Errorf("flushing %s/%s: %w", table, chain, err)
|
|
|
|
}
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := ipt.DeleteChain(table, chain); err != nil {
|
2020-05-28 09:44:44 +00:00
|
|
|
// this shouldn't fail, because if the chain didn't
|
|
|
|
// exist, we would have returned after ClearChain.
|
|
|
|
return fmt.Errorf("deleting %s/%s: %v", table, chain, err)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
for _, ipt := range r.netfilterFamilies() {
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := del(ipt, "filter", "ts-input"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := del(ipt, "filter", "ts-forward"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
if err := del(r.ipt4, "nat", "ts-postrouting"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.v6NATAvailable {
|
|
|
|
if err := del(r.ipt6, "nat", "ts-postrouting"); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:59:16 +00:00
|
|
|
// delNetfilterBase empties but does not remove custom Tailscale chains from
|
|
|
|
// netfilter.
|
|
|
|
func (r *linuxRouter) delNetfilterBase() error {
|
2020-09-22 00:49:44 +00:00
|
|
|
del := func(ipt netfilterRunner, table, chain string) error {
|
|
|
|
if err := ipt.ClearChain(table, chain); err != nil {
|
2020-05-28 10:59:16 +00:00
|
|
|
if errCode(err) == 1 {
|
|
|
|
// nonexistent chain. That's fine, since it's
|
|
|
|
// the desired state anyway.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("flushing %s/%s: %w", table, chain, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
for _, ipt := range r.netfilterFamilies() {
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := del(ipt, "filter", "ts-input"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := del(ipt, "filter", "ts-forward"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
if err := del(r.ipt4, "nat", "ts-postrouting"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.v6NATAvailable {
|
|
|
|
if err := del(r.ipt6, "nat", "ts-postrouting"); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-28 10:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
// addNetfilterHooks inserts calls to tailscale's netfilter chains in
|
|
|
|
// the relevant main netfilter chains. The tailscale chains must
|
|
|
|
// already exist.
|
|
|
|
func (r *linuxRouter) addNetfilterHooks() error {
|
2020-09-22 00:49:44 +00:00
|
|
|
divert := func(ipt netfilterRunner, table, chain string) error {
|
2020-05-13 22:35:22 +00:00
|
|
|
tsChain := tsChain(chain)
|
2020-05-02 01:55:38 +00:00
|
|
|
|
|
|
|
args := []string{"-j", tsChain}
|
2020-09-22 00:49:44 +00:00
|
|
|
exists, err := ipt.Exists(table, chain, args...)
|
2020-05-02 01:55:38 +00:00
|
|
|
if err != nil {
|
2020-05-28 09:44:44 +00:00
|
|
|
return fmt.Errorf("checking for %v in %s/%s: %w", args, table, chain, err)
|
2020-05-10 22:13:09 +00:00
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return nil
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := ipt.Insert(table, chain, 1, args...); err != nil {
|
2020-05-28 09:44:44 +00:00
|
|
|
return fmt.Errorf("adding %v in %s/%s: %w", args, table, chain, err)
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 22:35:22 +00:00
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
for _, ipt := range r.netfilterFamilies() {
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := divert(ipt, "filter", "INPUT"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := divert(ipt, "filter", "FORWARD"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
if err := divert(r.ipt4, "nat", "POSTROUTING"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.v6NATAvailable {
|
|
|
|
if err := divert(r.ipt6, "nat", "POSTROUTING"); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-05-13 22:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
// delNetfilterHooks deletes the calls to tailscale's netfilter chains
|
|
|
|
// in the relevant main netfilter chains.
|
|
|
|
func (r *linuxRouter) delNetfilterHooks() error {
|
2020-09-22 00:49:44 +00:00
|
|
|
del := func(ipt netfilterRunner, table, chain string) error {
|
2020-05-13 22:35:22 +00:00
|
|
|
tsChain := tsChain(chain)
|
|
|
|
args := []string{"-j", tsChain}
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := ipt.Delete(table, chain, args...); err != nil {
|
2020-05-28 09:44:44 +00:00
|
|
|
// TODO(apenwarr): check for errCode(1) here.
|
|
|
|
// Unfortunately the error code from the iptables
|
|
|
|
// module resists unwrapping, unlike with other
|
|
|
|
// calls. So we have to assume if Delete fails,
|
|
|
|
// it's because there is no such rule.
|
|
|
|
r.logf("note: deleting %v in %s/%s: %w", args, table, chain, err)
|
|
|
|
return nil
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
for _, ipt := range r.netfilterFamilies() {
|
2020-09-22 00:49:44 +00:00
|
|
|
if err := del(ipt, "filter", "INPUT"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := del(ipt, "filter", "FORWARD"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
if err := del(r.ipt4, "nat", "POSTROUTING"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.v6NATAvailable {
|
|
|
|
if err := del(r.ipt6, "nat", "POSTROUTING"); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-05-11 20:16:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addSNATRule adds a netfilter rule to SNAT traffic destined for
|
|
|
|
// local subnets.
|
|
|
|
func (r *linuxRouter) addSNATRule() error {
|
2021-02-04 21:12:42 +00:00
|
|
|
if r.netfilterMode == netfilterOff {
|
2020-05-13 22:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:29:41 +00:00
|
|
|
args := []string{"-m", "mark", "--mark", tailscaleSubnetRouteMark + "/" + tailscaleFwmarkMask, "-j", "MASQUERADE"}
|
2020-05-10 22:13:09 +00:00
|
|
|
if err := r.ipt4.Append("nat", "ts-postrouting", args...); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return fmt.Errorf("adding %v in v4/nat/ts-postrouting: %w", args, err)
|
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
if r.v6NATAvailable {
|
|
|
|
if err := r.ipt6.Append("nat", "ts-postrouting", args...); err != nil {
|
|
|
|
return fmt.Errorf("adding %v in v6/nat/ts-postrouting: %w", args, err)
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
}
|
2020-05-11 20:16:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
|
2020-05-11 20:16:52 +00:00
|
|
|
// delSNATRule removes the netfilter rule to SNAT traffic destined for
|
|
|
|
// local subnets. Fails if the rule does not exist.
|
|
|
|
func (r *linuxRouter) delSNATRule() error {
|
2021-02-04 21:12:42 +00:00
|
|
|
if r.netfilterMode == netfilterOff {
|
2020-05-13 22:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:29:41 +00:00
|
|
|
args := []string{"-m", "mark", "--mark", tailscaleSubnetRouteMark + "/" + tailscaleFwmarkMask, "-j", "MASQUERADE"}
|
2020-05-11 20:16:52 +00:00
|
|
|
if err := r.ipt4.Delete("nat", "ts-postrouting", args...); err != nil {
|
2020-09-22 00:49:44 +00:00
|
|
|
return fmt.Errorf("deleting %v in v4/nat/ts-postrouting: %w", args, err)
|
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
if r.v6NATAvailable {
|
|
|
|
if err := r.ipt6.Delete("nat", "ts-postrouting", args...); err != nil {
|
|
|
|
return fmt.Errorf("deleting %v in v6/nat/ts-postrouting: %w", args, err)
|
|
|
|
}
|
2020-05-11 20:16:52 +00:00
|
|
|
}
|
2020-05-02 01:55:38 +00:00
|
|
|
return nil
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cidrDiff calls add and del as needed to make the set of prefixes in
|
|
|
|
// old and new match. Returns a map reflecting the actual new state
|
|
|
|
// (which may be somewhere in between old and new if some commands
|
|
|
|
// failed), and any error encountered while reconfiguring.
|
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 cidrDiff(kind string, old map[netip.Prefix]bool, new []netip.Prefix, add, del func(netip.Prefix) error, logf logger.Logf) (map[netip.Prefix]bool, error) {
|
|
|
|
newMap := make(map[netip.Prefix]bool, len(new))
|
2020-05-13 22:35:22 +00:00
|
|
|
for _, cidr := range new {
|
|
|
|
newMap[cidr] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ret starts out as a copy of old, and updates as we
|
|
|
|
// add/delete. That way we can always return it and have it be the
|
|
|
|
// true state of what we've done so far.
|
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
|
|
|
ret := make(map[netip.Prefix]bool, len(old))
|
2020-05-13 22:35:22 +00:00
|
|
|
for cidr := range old {
|
|
|
|
ret[cidr] = true
|
|
|
|
}
|
|
|
|
|
2022-10-20 14:22:27 +00:00
|
|
|
// We want to add before we delete, so that if there is no overlap, we don't
|
|
|
|
// end up in a state where we have no addresses on an interface as that
|
|
|
|
// results in other kernel entities (like routes) pointing to that interface
|
|
|
|
// to also be deleted.
|
2021-02-18 06:11:59 +00:00
|
|
|
var addFail []error
|
2020-05-13 22:35:22 +00:00
|
|
|
for cidr := range newMap {
|
|
|
|
if old[cidr] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := add(cidr); err != nil {
|
|
|
|
logf("%s add failed: %v", kind, err)
|
2021-02-18 06:11:59 +00:00
|
|
|
addFail = append(addFail, err)
|
|
|
|
} else {
|
|
|
|
ret[cidr] = true
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
2021-02-18 06:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(addFail) == 1 {
|
|
|
|
return ret, addFail[0]
|
|
|
|
}
|
|
|
|
if len(addFail) > 0 {
|
|
|
|
return ret, fmt.Errorf("%d add %s failures; first was: %w", len(addFail), kind, addFail[0])
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 14:22:27 +00:00
|
|
|
var delFail []error
|
|
|
|
for cidr := range old {
|
|
|
|
if newMap[cidr] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := del(cidr); err != nil {
|
|
|
|
logf("%s del failed: %v", kind, err)
|
|
|
|
delFail = append(delFail, err)
|
|
|
|
} else {
|
|
|
|
delete(ret, cidr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(delFail) == 1 {
|
|
|
|
return ret, delFail[0]
|
|
|
|
}
|
|
|
|
if len(delFail) > 0 {
|
|
|
|
return ret, fmt.Errorf("%d delete %s failures; first was: %w", len(delFail), kind, delFail[0])
|
|
|
|
}
|
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// tsChain returns the name of the tailscale sub-chain corresponding
|
|
|
|
// to the given "parent" chain (e.g. INPUT, FORWARD, ...).
|
|
|
|
func tsChain(chain string) string {
|
|
|
|
return "ts-" + strings.ToLower(chain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalizeCIDR returns cidr as an ip/mask string, with the host bits
|
|
|
|
// of the IP address zeroed out.
|
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 normalizeCIDR(cidr netip.Prefix) string {
|
2021-02-18 05:35:33 +00:00
|
|
|
return cidr.Masked().String()
|
2020-05-13 22:35:22 +00:00
|
|
|
}
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
func cleanup(logf logger.Logf, interfaceName string) {
|
2020-07-31 20:27:09 +00:00
|
|
|
// TODO(dmytro): clean up iptables.
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
2020-09-22 21:55:28 +00:00
|
|
|
|
2020-12-29 16:26:17 +00:00
|
|
|
// checkIPv6 checks whether the system appears to have a working IPv6
|
|
|
|
// network stack. It returns an error explaining what looks wrong or
|
|
|
|
// missing. It does not check that IPv6 is currently functional or
|
|
|
|
// that there's a global address, just that the system would support
|
|
|
|
// IPv6 if it were on an IPv6 network.
|
2021-11-19 19:17:03 +00:00
|
|
|
func checkIPv6(logf logger.Logf) error {
|
2020-09-22 21:55:28 +00:00
|
|
|
_, err := os.Stat("/proc/sys/net/ipv6")
|
|
|
|
if os.IsNotExist(err) {
|
2020-12-29 16:26:17 +00:00
|
|
|
return err
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
2022-09-15 12:06:59 +00:00
|
|
|
bs, err := os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_ipv6")
|
2020-09-22 21:55:28 +00:00
|
|
|
if err != nil {
|
2023-06-27 16:00:34 +00:00
|
|
|
// Be conservative if we can't find the IPv6 configuration knob.
|
2020-12-29 16:26:17 +00:00
|
|
|
return err
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
disabled, err := strconv.ParseBool(strings.TrimSpace(string(bs)))
|
|
|
|
if err != nil {
|
2020-12-29 16:26:17 +00:00
|
|
|
return errors.New("disable_ipv6 has invalid bool")
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
if disabled {
|
2020-12-29 16:26:17 +00:00
|
|
|
return errors.New("disable_ipv6 is set")
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 22:32:50 +00:00
|
|
|
// Older kernels don't support IPv6 policy routing. Some kernels
|
|
|
|
// support policy routing but don't have this knob, so absence of
|
|
|
|
// the knob is not fatal.
|
2022-09-15 12:06:59 +00:00
|
|
|
bs, err = os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_policy")
|
2021-02-01 22:32:50 +00:00
|
|
|
if err == nil {
|
|
|
|
disabled, err = strconv.ParseBool(strings.TrimSpace(string(bs)))
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("disable_policy has invalid bool")
|
|
|
|
}
|
|
|
|
if disabled {
|
|
|
|
return errors.New("disable_policy is set")
|
|
|
|
}
|
2020-11-11 23:05:15 +00:00
|
|
|
}
|
2021-02-01 22:32:50 +00:00
|
|
|
|
2021-11-19 19:17:03 +00:00
|
|
|
if err := checkIPRuleSupportsV6(logf); err != nil {
|
2021-02-01 22:32:50 +00:00
|
|
|
return fmt.Errorf("kernel doesn't support IPv6 policy routing: %w", err)
|
2020-11-11 23:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 21:55:28 +00:00
|
|
|
// Some distros ship ip6tables separately from iptables.
|
|
|
|
if _, err := exec.LookPath("ip6tables"); err != nil {
|
2020-12-29 16:26:17 +00:00
|
|
|
return err
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 16:26:17 +00:00
|
|
|
return nil
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// supportsV6NAT returns whether the system has a "nat" table in the
|
|
|
|
// IPv6 netfilter stack.
|
|
|
|
//
|
|
|
|
// The nat table was added after the initial release of ipv6
|
|
|
|
// netfilter, so some older distros ship a kernel that can't NAT IPv6
|
|
|
|
// traffic.
|
|
|
|
func supportsV6NAT() bool {
|
2022-09-15 12:06:59 +00:00
|
|
|
bs, err := os.ReadFile("/proc/net/ip6_tables_names")
|
2020-09-22 21:55:28 +00:00
|
|
|
if err != nil {
|
|
|
|
// Can't read the file. Assume SNAT works.
|
|
|
|
return true
|
|
|
|
}
|
2022-04-20 17:14:07 +00:00
|
|
|
if bytes.Contains(bs, []byte("nat\n")) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// In nftables mode, that proc file will be empty. Try another thing:
|
|
|
|
if exec.Command("modprobe", "ip6table_nat").Run() == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2020-09-22 21:55:28 +00:00
|
|
|
}
|
2020-12-29 16:26:17 +00:00
|
|
|
|
2021-11-19 19:17:03 +00:00
|
|
|
func checkIPRuleSupportsV6(logf logger.Logf) error {
|
|
|
|
// First try just a read-only operation to ideally avoid
|
|
|
|
// having to modify any state.
|
|
|
|
if rules, err := netlink.RuleList(netlink.FAMILY_V6); err != nil {
|
|
|
|
return fmt.Errorf("querying IPv6 policy routing rules: %w", err)
|
|
|
|
} else {
|
|
|
|
if len(rules) > 0 {
|
|
|
|
logf("[v1] kernel supports IPv6 policy routing (found %d rules)", len(rules))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to actually create & delete one as a test.
|
2021-11-01 22:52:24 +00:00
|
|
|
rule := netlink.NewRule()
|
|
|
|
rule.Priority = 1234
|
|
|
|
rule.Mark = tailscaleBypassMarkNum
|
|
|
|
rule.Table = tailscaleRouteTable.num
|
2021-11-19 19:17:03 +00:00
|
|
|
rule.Family = netlink.FAMILY_V6
|
2021-02-01 22:32:50 +00:00
|
|
|
// First delete the rule unconditionally, and don't check for
|
|
|
|
// errors. This is just cleaning up anything that might be already
|
|
|
|
// there.
|
2021-11-01 22:52:24 +00:00
|
|
|
netlink.RuleDel(rule)
|
|
|
|
// And clean up on exit.
|
|
|
|
defer netlink.RuleDel(rule)
|
|
|
|
return netlink.RuleAdd(rule)
|
2020-12-29 16:26:17 +00:00
|
|
|
}
|
2021-10-28 04:36:29 +00:00
|
|
|
|
2022-09-09 21:23:48 +00:00
|
|
|
// Checks if the running openWRT system is using mwan3, based on the heuristic
|
|
|
|
// of the config file being present as well as a policy rule with a specific
|
|
|
|
// priority (2000 + 1 - first interface mwan3 manages) and non-zero mark.
|
|
|
|
func checkOpenWRTUsingMWAN3() (bool, error) {
|
|
|
|
if distro.Get() != distro.OpenWrt {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat("/etc/config/mwan3"); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rules, err := netlink.RuleList(netlink.FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, r := range rules {
|
|
|
|
// We want to match on a rule like this:
|
|
|
|
// 2001: from all fwmark 0x100/0x3f00 lookup 1
|
|
|
|
//
|
|
|
|
// We dont match on the mask because it can vary, or the
|
|
|
|
// table because I'm not sure if it can vary.
|
2022-09-28 18:01:05 +00:00
|
|
|
if r.Priority >= 2001 && r.Priority <= 2004 && r.Mark != 0 {
|
2022-09-09 21:23:48 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
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 nlAddrOfPrefix(p netip.Prefix) *netlink.Addr {
|
2021-10-28 04:36:29 +00:00
|
|
|
return &netlink.Addr{
|
2022-07-25 03:08:42 +00:00
|
|
|
IPNet: netipx.PrefixIPNet(p),
|
2021-10-28 04:36:29 +00:00
|
|
|
}
|
|
|
|
}
|