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 (
|
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2020-05-08 05:17:30 +00:00
|
|
|
"inet.af/netaddr"
|
2021-03-26 00:41:51 +00:00
|
|
|
"tailscale.com/net/dns"
|
2020-04-30 20:20:09 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-02-04 21:12:42 +00:00
|
|
|
"tailscale.com/types/preftype"
|
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.
|
2020-04-30 20:20:09 +00:00
|
|
|
func New(logf logger.Logf, wgdev *device.Device, tundev tun.Device) (Router, error) {
|
2020-07-14 13:12:00 +00:00
|
|
|
logf = logger.WithPrefix(logf, "router: ")
|
2020-04-30 20:20:09 +00:00
|
|
|
return newUserspaceRouter(logf, wgdev, tundev)
|
|
|
|
}
|
|
|
|
|
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-31 20:27:09 +00:00
|
|
|
mconfig := dns.ManagerConfig{
|
|
|
|
Logf: logf,
|
|
|
|
InterfaceName: interfaceName,
|
|
|
|
Cleanup: true,
|
|
|
|
}
|
|
|
|
dns := dns.NewManager(mconfig)
|
|
|
|
if err := dns.Down(); err != nil {
|
|
|
|
logf("dns down: %v", err)
|
|
|
|
}
|
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 {
|
2020-05-13 22:35:22 +00:00
|
|
|
LocalAddrs []netaddr.IPPrefix
|
|
|
|
Routes []netaddr.IPPrefix // routes to point into the Tailscale interface
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
DNS dns.Config
|
2020-07-14 13:12:00 +00:00
|
|
|
|
2020-05-13 22:35:22 +00:00
|
|
|
// Linux-only things below, ignored on other platforms.
|
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
SubnetRoutes []netaddr.IPPrefix // subnets being advertised to other Tailscale nodes
|
|
|
|
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
|
|
|
|
|
|
|
// 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{}
|