summaryrefslogtreecommitdiffhomepage
path: root/prober/tun_darwin.go
blob: 45c5415acd8d077b1865f3bc1eb4c3d0f9b92c34 (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
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

//go:build darwin

package prober

import (
	"fmt"
	"net/netip"
	"os/exec"

	"go4.org/netipx"
)

const tunName = "utun"

func configureTUN(addr netip.Prefix, tunname string) error {
	cmd := exec.Command("ifconfig", tunname, "inet", addr.String(), addr.Addr().String())
	res, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("failed to add address: %w (%s)", err, string(res))
	}

	net := netipx.PrefixIPNet(addr)
	nip := net.IP.Mask(net.Mask)
	nstr := fmt.Sprintf("%v/%d", nip, addr.Bits())
	cmd = exec.Command("route", "-q", "-n", "add", "-inet", nstr, "-iface", addr.Addr().String())
	res, err = cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("failed to add route: %w (%s)", err, string(res))
	}

	return nil
}