router

package
v1.90.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2025 License: BSD-3-Clause Imports: 17 Imported by: 37

Documentation

Overview

Package router presents an interface to manipulate the host network stack's state.

Index

Constants

This section is empty.

Variables

View Source
var HookCleanUp feature.Hook[func(_ logger.Logf, _ *netmon.Monitor, ifName string)]

HookCleanUp is the optional registration point for router implementations to register a cleanup function for CleanUp to use. It's meant for implementations in wgengine/router/osrouter.

View Source
var HookNewUserspaceRouter feature.Hook[func(NewOpts) (Router, error)]

HookNewUserspaceRouter is the registration point for router implementations to register a constructor for userspace routers. It's meant for implementations in wgengine/router/osrouter.

If no implementation is registered, New will return an error.

Functions

func CleanUp added in v1.64.0

func CleanUp(logf logger.Logf, netMon *netmon.Monitor, interfaceName string)

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.

Types

type CallbackRouter added in v1.8.0

type CallbackRouter struct {
	SetBoth  func(rcfg *Config, dcfg *dns.OSConfig) error
	SplitDNS bool

	// GetBaseConfigFunc optionally specifies a function to return the current DNS
	// config in response to GetBaseConfig.
	//
	// If nil, reading the current config isn't supported and GetBaseConfig()
	// will return ErrGetBaseConfigNotSupported.
	GetBaseConfigFunc func() (dns.OSConfig, error)

	// InitialMTU is the MTU the tun should be initialized with.
	// Zero means don't change the MTU from the default. This MTU
	// is applied only once, shortly after the TUN is created, and
	// ignored thereafter.
	InitialMTU uint32
	// contains filtered or unexported fields
}

CallbackRouter is an implementation of both Router and dns.OSConfigurator. When either network or DNS settings are changed, SetBoth is called with both configs. Mainly used as a shim for OSes that want to set both network and DNS configuration simultaneously (Mac, iOS, Android).

func (*CallbackRouter) Close added in v1.8.0

func (r *CallbackRouter) Close() error

func (*CallbackRouter) GetBaseConfig added in v1.8.0

func (r *CallbackRouter) GetBaseConfig() (dns.OSConfig, error)

func (*CallbackRouter) Set added in v1.8.0

func (r *CallbackRouter) Set(rcfg *Config) error

Set implements Router.

func (*CallbackRouter) SetDNS added in v1.8.0

func (r *CallbackRouter) SetDNS(dcfg dns.OSConfig) error

SetDNS implements dns.OSConfigurator.

func (*CallbackRouter) SupportsSplitDNS added in v1.8.0

func (r *CallbackRouter) SupportsSplitDNS() bool

SupportsSplitDNS implements dns.OSConfigurator.

func (*CallbackRouter) Up added in v1.8.0

func (r *CallbackRouter) Up() error

Up implements Router.

type Config

type Config struct {
	// 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).
	LocalAddrs []netip.Prefix

	// Routes are the routes that point into the Tailscale
	// 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.
	Routes []netip.Prefix

	// 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.
	LocalRoutes []netip.Prefix

	// NewMTU is currently only used by the MacOS network extension
	// app to set the MTU of the tun in the router configuration
	// callback. If zero, the MTU is unchanged.
	NewMTU int

	// SubnetRoutes is the list of subnets that this node is
	// advertising to other Tailscale nodes.
	// As of 2023-10-11, this field is only used for network
	// flow logging and is otherwise ignored.
	SubnetRoutes []netip.Prefix

	// Linux-only things below, ignored on other platforms.
	SNATSubnetRoutes  bool                   // SNAT traffic to local subnets
	StatefulFiltering bool                   // Apply stateful filtering to inbound connections
	NetfilterMode     preftype.NetfilterMode // how much to manage netfilter rules
	NetfilterKind     string                 // what kind of netfilter to use ("nftables", "iptables", or "" to auto-detect)
}

Config is the subset of Tailscale configuration that is relevant to the OS's network stack.

func (*Config) Clone added in v1.90.0

func (c *Config) Clone() *Config

func (*Config) Equal added in v1.26.2

func (a *Config) Equal(b *Config) bool

type NewOpts added in v1.90.0

type NewOpts struct {
	Logf   logger.Logf     // required
	Tun    tun.Device      // required
	NetMon *netmon.Monitor // optional
	Health *health.Tracker // required (but TODO: support optional later)
	Bus    *eventbus.Bus   // required
}

NewOpts are the options passed to the NewUserspaceRouter hook.

type PortUpdate added in v1.90.0

type PortUpdate struct {
	UDPPort         uint16
	EndpointNetwork string // either "udp4" or "udp6".
}

PortUpdate is an eventbus value, reporting the port and address family magicsock is currently listening on, so it can be threaded through firewalls and such.

type Router

type Router interface {
	// Up brings the router up.
	Up() error

	// Set updates the OS network stack with a new Config. It may be
	// called multiple times with identical Configs, which the
	// implementation should handle gracefully.
	Set(*Config) error

	// Close closes the router.
	Close() error
}

Router is responsible for managing the system network stack.

There is typically only one instance of this interface per process.

func ConsolidatingRoutes added in v1.66.0

func ConsolidatingRoutes(logf logger.Logf, router Router) Router

ConsolidatingRoutes wraps a Router with logic that consolidates Routes whenever Set is called. It attempts to consolidate cfg.Routes into the smallest possible set.

func New

func New(logf logger.Logf, tundev tun.Device, netMon *netmon.Monitor,
	health *health.Tracker, bus *eventbus.Bus,
) (Router, error)

New returns a new Router for the current platform, using the provided tun device.

If netMon is nil, it's not used. It's currently (2021-07-20) only used on Linux in some situations.

func NewFake

func NewFake(logf logger.Logf) Router

NewFake returns a Router that does nothing when called and always returns nil errors.

Directories

Path Synopsis
Package osrouter contains OS-specific router implementations.
Package osrouter contains OS-specific router implementations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL