netutil

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package netutil sets up the Linux networking for endpoints: veth pairs, routing and NAT.

Index

Constants

View Source
const (
	IsolateForwardChain = "TSLINK-ISOLATE-FWD"
	IsolateInputChain   = "TSLINK-ISOLATE-IN"
)

Host tailnet isolation keeps containers from reaching the tailnet through the host's own tailscaled. The host's tailscaled accepts all forwarded traffic to its interface (its ts-forward chain), and Docker masquerades container traffic, so without it any container reaches the tailnet with the host's identity: one on Docker's bridges with an ordinary socket, and one on tslink with raw frames (CAP_NET_RAW, which Docker grants by default) past the unreachable routes in its namespace. The rules live in the host's namespace, where no container can change them, NET_ADMIN or not.

They are in the mangle table, whose FORWARD and INPUT hooks run before the filter table's, so no ACCEPT in filter, the host tailscaled's or Docker's, gets to the traffic first. Only traffic arriving on container interfaces is dropped: a subnet router or exit node on the host still forwards from the LAN.

View Source
const DefaultMTU = 1500

DefaultMTU is the veth MTU when the network sets none.

View Source
const MaxHostEndpoints = 3

MaxHostEndpoints is how many endpoints tailscaled takes from TS_DEBUG_PRETENDPOINT; it drops more without a word.

View Source
const VethIsolateChain = "TSLINK-VETH-FWD"

VethIsolateChain holds the veth isolation, which keeps containers from reaching each other through tslink's veths. The host forwards between the veths' /30s in vethRange, so without it a container reaches another tslink container's veth address, past Docker's network isolation and past the tailnet's ACLs, with an ordinary socket through its Docker gateway or with raw frames through its veth. Nothing opens a connection to a veth address but a colocated node's tailscaled, to tailscaled's WireGuard port through its own veth, so colocated nodes keep their direct path instead of falling back to DERP; WireGuard drops what is not from a peer. Everything else forwarded there is dropped unless it answers a connection the container opened, whatever interface it arrives on: a stack file names a bridge as it likes, and a LAN host may route vethRange through the host. It does not depend on TSLINK_ISOLATE_HOST_TAILNET.

View Source
const WireGuardInputChain = "TSLINK-WG-IN"

WireGuardInputChain makes the external ports of containers' tailscaled (portMapChain) take only packets on flows the containers opened, which the host forwards. A packet that arrives first, from a peer probing a port the container advertises, has no NAT to undo and goes to the host itself: conntrack keeps an entry for it, whose reply is the tuple the container's own packets to that peer need, and as each container has a single port, they are dropped for as long as the peer keeps probing. Dropping such packets before conntrack confirms them leaves no entry behind; the container's first packet to the peer then opens the path for the peer's next. It does not depend on TSLINK_ISOLATE_HOST_TAILNET.

Variables

View Source
var DefaultPortRange = PortRange{61000, 65535}

DefaultPortRange holds the external ports of tailscaled's WireGuard traffic. It lies above Linux's default ephemeral range (32768-60999), so the host's own connections do not use them.

View Source
var ErrNoPortMapping = errors.New("no external port mapped")

ErrNoPortMapping is HostEndpoints' error for a container whose tailscaled has no external port (portMapChain) at the moment, as while its rule is missing until the next SyncPortMappings.

Functions

func CleanupNAT

func CleanupNAT(vethHost string)

CleanupNAT removes the FORWARD rules and the port mapping for a specific veth interface, and the port mappings of veths that are gone. The global MASQUERADE rule and chain structure are intentionally left in place.

func CleanupTailnetBlackhole

func CleanupTailnetBlackhole(nsPath string) error

CleanupTailnetBlackhole removes the routes added by setupTailnetBlackhole. A namespace or route that is already gone is not an error.

Note that a container that stays attached to another network regains a path to the tailnet ranges through the host once these routes are removed.

func ConfigureHostEndpoints added in v0.2.2

func ConfigureHostEndpoints(c HostEndpointsConfig)

ConfigureHostEndpoints sets the addresses HostEndpointAddrs returns.

func ConfigurePortMapping added in v0.2.1

func ConfigurePortMapping(sport int, ports PortRange)

ConfigurePortMapping sets tailscaled's UDP port in the containers and the range of external ports SetupNAT maps it to. Rules in place keep their ports.

func CreateVethPair

func CreateVethPair(baseName string, mtu int) (string, string, error)

CreateVethPair creates a veth pair with the given base name and MTU (0 for DefaultMTU). Returns the host-side name and container-side name, as VethNames. A link with the host-side name and no address in tslink's veth range is a leftover and replaced; one with such an address belongs to a live endpoint, and CreateVethPair fails rather than delete it.

func DeleteVeth

func DeleteVeth(name string) error

DeleteVeth deletes a veth interface.

func HostEndpointAddrs added in v0.2.2

func HostEndpointAddrs() ([]netip.Addr, error)

HostEndpointAddrs returns the addresses tslink nodes advertise, as configured: none, the given ones, or the host's private addresses (selectHostAddrs).

func HostEndpoints added in v0.2.2

func HostEndpoints(vethHost string) ([]netip.AddrPort, error)

HostEndpoints returns the endpoints the tailscaled behind vethHost advertises: each of HostEndpointAddrs with its external port. None if no address is configured or found. The port comes from the mappings tslink wrote or last listed, so it starts no process: a rule another program removed counts until the next SyncPortMappings maps the container again.

func LeadingJumps added in v0.3.1

func LeadingJumps(rules []string, from string) []string

LeadingJumps returns the chains of the leading run of jumps to tslink's chains among the rules of the built-in chain from, as iptables -S lists them; its policy line is skipped.

func ListenInNetNS

func ListenInNetNS(nsPath, addr string) (net.Listener, error)

ListenInNetNS opens a TCP listener on addr in the network namespace at nsPath. The socket belongs to the namespace it was created in, so it keeps listening there after the thread returns to the plugin's namespace, and only that namespace's processes reach a loopback address.

func MoveToNetNS

func MoveToNetNS(ifName string, nsPath string) error

MoveToNetNS moves a network interface to the specified network namespace.

func RemoveHostIsolation

func RemoveHostIsolation() error

RemoveHostIsolation removes the host's tailnet isolation, and tolerates its absence.

func SetupContainerSide added in v0.2.4

func SetupContainerSide(c ContainerSide) error

SetupContainerSide configures the container's end of the veth, through one netlink handle in its namespace: it brings the interface up, adds its /30 address, routes tailscaled's own traffic through it (setupBypassRoute), makes the tailnet ranges unreachable outside tailscaled (setupTailnetBlackhole) and adds the IPAM address (addIPAMAddress). It is idempotent, so recovery can run it again. A failure to add the IPAM address is only logged.

It sets no default route: Docker connects the container to its gateway network like any other.

func SetupHostIsolation

func SetupHostIsolation(bridges []string) error

SetupHostIsolation installs the host's tailnet isolation, or restores it: rules another program removed, and jumps it pushed down. bridges are the interface names of Docker's bridge networks, which containerInterfaces may not match. It changes nothing that is in place, so it can run periodically.

func SetupHostRouting

func SetupHostRouting(vethHost string, hostIP netip.Addr) error

SetupHostRouting sets up routing on the host side for internet access.

func SetupNAT

func SetupNAT(vethHost string) error

SetupNAT sets up forwarding and MASQUERADE for traffic from the container, in the chain TSLINK-FORWARD. vethHost must have its address: the container's tailscaled gets its own external port (see portMapChain).

func SetupVethIsolation

func SetupVethIsolation(port int) error

SetupVethIsolation installs the veth isolation, or restores it, letting only replies and UDP to port from tslink's veths through to them. It also installs TSLINK-WG-IN, restores the veths' forwarding and MASQUERADE (syncForwarding) and syncs their WireGuard port mappings (SyncPortMappings). IPv4 only: the veths have no other addresses than link-local IPv6 ones. It changes nothing that is in place, so it can run periodically.

func SyncPortMappings added in v0.2.1

func SyncPortMappings() error

SyncPortMappings maps the tailscaled behind each of the host's veths to a port of its own, unless it has one, and removes the mappings of addresses no veth has. It changes nothing that is in place, so it can run periodically: it maps containers whose veth an older version set up, and removes the mappings of veths that went away with their container.

func UsedVethSubnets

func UsedVethSubnets() (map[netip.Prefix]bool, error)

UsedVethSubnets returns the /30 subnets in tslink's veth range that links of the current network namespace have addresses in.

func VethIPv4

func VethIPv4(name string) (netip.Addr, error)

VethIPv4 returns the IPv4 address in tslink's veth range of the link name.

func VethNames added in v0.3.0

func VethNames(baseName string) (string, string)

VethNames returns the host-side and container-side names of the veth pair with the given base name.

Types

type ContainerSide added in v0.2.4

type ContainerSide struct {
	NSPath    string     // the container's network namespace
	Interface string     // the container end of the veth, already in NSPath
	Address   netip.Addr // its address in the veth's /30
	Gateway   netip.Addr // the host end's address
	// IPAMAddress is the address Docker's IPAM assigned to the endpoint, if any
	IPAMAddress netip.Addr
}

ContainerSide is the container's end of a tslink veth.

type HostEndpointsConfig added in v0.2.2

type HostEndpointsConfig struct {
	Off   bool
	Addrs []netip.Addr
}

HostEndpointsConfig selects the addresses tslink nodes advertise: none if Off, Addrs if given, and otherwise the host's private addresses.

type PortRange added in v0.2.1

type PortRange struct{ First, Last int }

PortRange is an inclusive range of ports.

func ParsePortRange added in v0.2.1

func ParsePortRange(s string) (PortRange, error)

ParsePortRange parses "first-last", ports from 1024 to 65535.

func (PortRange) Contains added in v0.2.1

func (r PortRange) Contains(port int) bool

Contains reports whether port is in the range.

func (PortRange) String added in v0.2.1

func (r PortRange) String() string

Jump to

Keyboard shortcuts

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