2020-04-30 20:20:09 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package router presents an interface to manipulate the host network
|
|
|
|
// stack's state.
|
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
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"
|
2022-06-28 22:32:09 +00:00
|
|
|
"reflect"
|
|
|
|
|
2021-05-25 19:42:22 +00:00
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2020-04-30 20:20:09 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-02-04 21:12:42 +00:00
|
|
|
"tailscale.com/types/preftype"
|
2021-07-20 20:28:06 +00:00
|
|
|
"tailscale.com/wgengine/monitor"
|
2020-04-30 20:20:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Router is responsible for managing the system network stack.
|
|
|
|
//
|
|
|
|
// There is typically only one instance of this interface per process.
|
|
|
|
type Router interface {
|
|
|
|
// Up brings the router up.
|
|
|
|
Up() error
|
|
|
|
|
2020-05-12 07:08:52 +00:00
|
|
|
// Set updates the OS network stack with a new Config. It may be
|
|
|
|
// called multiple times with identical Configs, which the
|
2020-05-08 01:07:13 +00:00
|
|
|
// implementation should handle gracefully.
|
2020-05-12 07:08:52 +00:00
|
|
|
Set(*Config) error
|
2020-04-30 20:20:09 +00:00
|
|
|
|
|
|
|
// Close closes the router.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:37:30 +00:00
|
|
|
// New returns a new Router for the current platform, using the
|
|
|
|
// provided tun device.
|
2021-07-20 20:28:06 +00:00
|
|
|
//
|
|
|
|
// If linkMon is nil, it's not used. It's currently (2021-07-20) only
|
|
|
|
// used on Linux in some situations.
|
|
|
|
func New(logf logger.Logf, tundev tun.Device, linkMon *monitor.Mon) (Router, error) {
|
2020-07-14 13:12:00 +00:00
|
|
|
logf = logger.WithPrefix(logf, "router: ")
|
2021-07-20 20:28:06 +00:00
|
|
|
return newUserspaceRouter(logf, tundev, linkMon)
|
2020-04-30 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 10:17:58 +00:00
|
|
|
// Cleanup restores the system network configuration to its original state
|
|
|
|
// in case the Tailscale daemon terminated without closing the router.
|
|
|
|
// No other state needs to be instantiated before this runs.
|
|
|
|
func Cleanup(logf logger.Logf, interfaceName string) {
|
2020-07-14 13:12:00 +00:00
|
|
|
cleanup(logf, interfaceName)
|
2020-07-13 10:17:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 07:08:52 +00:00
|
|
|
// Config is the subset of Tailscale configuration that is relevant to
|
|
|
|
// the OS's network stack.
|
|
|
|
type Config struct {
|
2021-04-01 14:50:50 +00:00
|
|
|
// LocalAddrs are the address(es) for this node. This is
|
|
|
|
// typically one IPv4/32 (the 100.x.y.z CGNAT) and one
|
|
|
|
// IPv6/128 (Tailscale ULA).
|
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
|
|
|
LocalAddrs []netip.Prefix
|
2021-04-01 14:50:50 +00:00
|
|
|
|
2021-04-08 22:56:51 +00:00
|
|
|
// Routes are the routes that point into the Tailscale
|
2021-04-01 14:50:50 +00:00
|
|
|
// interface. These are the /32 and /128 routes to peers, as
|
|
|
|
// well as any other subnets that peers are advertising and
|
|
|
|
// this node has chosen to use.
|
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
|
|
|
Routes []netip.Prefix
|
2020-05-13 22:35:22 +00:00
|
|
|
|
2021-04-08 22:56:51 +00:00
|
|
|
// LocalRoutes are the routes that should not be routed through Tailscale.
|
|
|
|
// There are no priorities set in how these routes are added, normal
|
|
|
|
// routing rules apply.
|
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
|
|
|
LocalRoutes []netip.Prefix
|
2021-04-08 22:56:51 +00:00
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
// Linux-only things below, ignored on other platforms.
|
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
|
|
|
SubnetRoutes []netip.Prefix // subnets being advertised to other Tailscale nodes
|
2021-02-04 21:12:42 +00:00
|
|
|
SNATSubnetRoutes bool // SNAT traffic to local subnets
|
|
|
|
NetfilterMode preftype.NetfilterMode // how much to manage netfilter rules
|
2020-04-30 20:20:09 +00:00
|
|
|
}
|
2020-05-12 07:08:52 +00:00
|
|
|
|
2022-06-28 22:32:09 +00:00
|
|
|
func (a *Config) Equal(b *Config) bool {
|
|
|
|
if a == nil && b == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (a == nil) != (b == nil) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return reflect.DeepEqual(a, b)
|
|
|
|
}
|
|
|
|
|
2020-05-12 07:08:52 +00:00
|
|
|
// shutdownConfig is a routing configuration that removes all router
|
|
|
|
// state from the OS. It's the config used when callers pass in a nil
|
|
|
|
// Config.
|
2020-05-13 22:35:22 +00:00
|
|
|
var shutdownConfig = Config{}
|