summaryrefslogtreecommitdiffhomepage
path: root/net/netmon/interfaces.go
blob: c7a2cb213e89386d66db124249a60b654e03adb5 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package netmon

import (
	"errors"
	"net"

	"tailscale.com/syncs"
)

type ifProps struct {
	mu    syncs.Mutex
	name  string // interface name, if known/set
	index int    // interface index, if known/set
}

// tsIfProps tracks the properties (name and index) of the tailscale interface.
// There is only one tailscale interface per tailscaled instance.
var tsIfProps ifProps

func (p *ifProps) tsIfName() string {
	p.mu.Lock()
	defer p.mu.Unlock()
	return p.name
}

func (p *ifProps) tsIfIndex() int {
	p.mu.Lock()
	defer p.mu.Unlock()
	return p.index
}

func (p *ifProps) set(ifName string, ifIndex int) {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.name = ifName
	p.index = ifIndex
}

// TODO (barnstar): This doesn't need the Monitor receiver anymore but we're
// keeping it for API compatibility to avoid a breaking change.  This can be
// removed when the various clients have switched to SetTailscaleInterfaceProps
func (m *Monitor) SetTailscaleInterfaceName(ifName string) {
	SetTailscaleInterfaceProps(ifName, 0)
}

// SetTailscaleInterfaceProps sets the name of the Tailscale interface and
// its index for use by various listeners/dialers.  If the index is zero,
// an attempt will be made to look it up by name.  This makes no attempt
// to validate that the interface exists at the time of calling.
//
// If this method is called, it is the responsibility of the caller to
// update the interface name and index if they change.
//
// This should be called as early as possible during tailscaled startup.
func SetTailscaleInterfaceProps(ifName string, ifIndex int) {
	if ifIndex != 0 {
		tsIfProps.set(ifName, ifIndex)
		return
	}

	ifaces, err := net.Interfaces()
	if err != nil {
		return
	}

	for _, iface := range ifaces {
		if iface.Name == ifName {
			ifIndex = iface.Index
			break
		}
	}

	tsIfProps.set(ifName, ifIndex)
}

// TailscaleInterfaceName returns the name of the Tailscale interface.
// For example, "tailscale0", "tun0", "utun3", etc or an error if unset.
//
// Callers must handle errors, as the Tailscale interface
// name may not be set in some environments.
func TailscaleInterfaceName() (string, error) {
	name := tsIfProps.tsIfName()
	if name == "" {
		return "", errors.New("Tailscale interface name not set")
	}
	return name, nil
}

// TailscaleInterfaceIndex returns the index of the Tailscale interface or
// an error if unset.
//
// Callers must handle errors, as the Tailscale interface
// index may not be set in some environments.
func TailscaleInterfaceIndex() (int, error) {
	index := tsIfProps.tsIfIndex()
	if index == 0 {
		return 0, errors.New("Tailscale interface index not set")
	}
	return index, nil
}