2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-03-27 04:03:21 +00:00
|
|
|
|
2023-08-30 12:23:38 +00:00
|
|
|
//go:build !wasm && !plan9 && !tamago
|
2021-10-21 17:36:30 +00:00
|
|
|
|
2021-03-27 04:03:21 +00:00
|
|
|
// Package tun creates a tuntap device, working around OS-specific
|
|
|
|
// quirks if necessary.
|
2021-03-27 05:07:19 +00:00
|
|
|
package tstun
|
2021-03-27 04:03:21 +00:00
|
|
|
|
|
|
|
import (
|
2021-07-23 16:45:04 +00:00
|
|
|
"errors"
|
2021-03-27 04:03:21 +00:00
|
|
|
"runtime"
|
2021-07-23 16:45:04 +00:00
|
|
|
"strings"
|
2021-03-27 04:03:21 +00:00
|
|
|
"time"
|
|
|
|
|
2022-12-09 23:12:20 +00:00
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2021-03-27 04:03:21 +00:00
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
2021-07-23 16:45:04 +00:00
|
|
|
// createTAP is non-nil on Linux.
|
|
|
|
var createTAP func(tapName, bridgeName string) (tun.Device, error)
|
|
|
|
|
2021-04-06 04:45:56 +00:00
|
|
|
// New returns a tun.Device for the requested device name, along with
|
|
|
|
// the OS-dependent name that was allocated to the device.
|
|
|
|
func New(logf logger.Logf, tunName string) (tun.Device, string, error) {
|
2021-07-23 16:45:04 +00:00
|
|
|
var dev tun.Device
|
|
|
|
var err error
|
|
|
|
if strings.HasPrefix(tunName, "tap:") {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return nil, "", errors.New("tap only works on Linux")
|
|
|
|
}
|
2022-11-07 15:33:23 +00:00
|
|
|
if createTAP == nil { // if the ts_omit_tap tag is used
|
|
|
|
return nil, "", errors.New("tap is not supported in this build")
|
|
|
|
}
|
2021-07-23 16:45:04 +00:00
|
|
|
f := strings.Split(tunName, ":")
|
|
|
|
var tapName, bridgeName string
|
|
|
|
switch len(f) {
|
|
|
|
case 2:
|
|
|
|
tapName = f[1]
|
|
|
|
case 3:
|
|
|
|
tapName, bridgeName = f[1], f[2]
|
|
|
|
default:
|
|
|
|
return nil, "", errors.New("bogus tap argument")
|
|
|
|
}
|
|
|
|
dev, err = createTAP(tapName, bridgeName)
|
|
|
|
} else {
|
2023-09-22 15:49:09 +00:00
|
|
|
dev, err = tun.CreateTUN(tunName, int(DefaultTUNMTU()))
|
2021-07-23 16:45:04 +00:00
|
|
|
}
|
2021-03-27 04:03:21 +00:00
|
|
|
if err != nil {
|
2021-04-06 04:45:56 +00:00
|
|
|
return nil, "", err
|
2021-03-27 04:03:21 +00:00
|
|
|
}
|
|
|
|
if err := waitInterfaceUp(dev, 90*time.Second, logf); err != nil {
|
2021-04-06 04:45:56 +00:00
|
|
|
dev.Close()
|
|
|
|
return nil, "", err
|
2021-03-27 04:03:21 +00:00
|
|
|
}
|
2022-02-22 07:33:23 +00:00
|
|
|
if err := setLinkAttrs(dev); err != nil {
|
|
|
|
logf("setting link attributes: %v", err)
|
|
|
|
}
|
2021-04-06 04:45:56 +00:00
|
|
|
name, err := interfaceName(dev)
|
|
|
|
if err != nil {
|
|
|
|
dev.Close()
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return dev, name, nil
|
2021-03-27 04:03:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 18:24:25 +00:00
|
|
|
// tunDiagnoseFailure, if non-nil, does OS-specific diagnostics of why
|
|
|
|
// TUN failed to work.
|
2022-07-16 21:10:20 +00:00
|
|
|
var tunDiagnoseFailure func(tunName string, logf logger.Logf, err error)
|
2021-08-16 18:24:25 +00:00
|
|
|
|
2021-03-27 04:03:21 +00:00
|
|
|
// Diagnose tries to explain a tuntap device creation failure.
|
|
|
|
// It pokes around the system and logs some diagnostic info that might
|
|
|
|
// help debug why tun creation failed. Because device creation has
|
|
|
|
// already failed and the program's about to end, log a lot.
|
2022-07-16 21:10:20 +00:00
|
|
|
//
|
|
|
|
// The tunName is the name of the tun device that was requested but failed.
|
|
|
|
// The err error is how the tun creation failed.
|
|
|
|
func Diagnose(logf logger.Logf, tunName string, err error) {
|
2021-08-16 18:24:25 +00:00
|
|
|
if tunDiagnoseFailure != nil {
|
2022-07-16 21:10:20 +00:00
|
|
|
tunDiagnoseFailure(tunName, logf, err)
|
2021-08-16 18:24:25 +00:00
|
|
|
} else {
|
2021-03-27 04:03:21 +00:00
|
|
|
logf("no TUN failure diagnostics for OS %q", runtime.GOOS)
|
|
|
|
}
|
|
|
|
}
|