summaryrefslogtreecommitdiffhomepage
path: root/prober/tun_linux.go
blob: 7a28a4b3f829e2a8197d520eb582398ac54fbee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

//go:build linux

package prober

import (
	"fmt"
	"net/netip"

	"github.com/tailscale/netlink"
	"go4.org/netipx"
)

const tunName = "derpprobe"

func configureTUN(addr netip.Prefix, tunname string) error {
	link, err := netlink.LinkByName(tunname)
	if err != nil {
		return fmt.Errorf("failed to look up link %q: %w", tunname, err)
	}

	// We need to bring the TUN device up before assigning an address. This
	// allows the OS to automatically create a route for it. Otherwise, we'd
	// have to manually create the route.
	if err := netlink.LinkSetUp(link); err != nil {
		return fmt.Errorf("failed to bring tun %q up: %w", tunname, err)
	}

	if err := netlink.AddrReplace(link, &netlink.Addr{IPNet: netipx.PrefixIPNet(addr)}); err != nil {
		return fmt.Errorf("failed to add address: %w", err)
	}

	return nil
}