2021-03-27 04:03:21 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-10-21 17:36:30 +00:00
|
|
|
//go:build !js
|
|
|
|
|
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"
|
|
|
|
|
2021-05-25 19:42:22 +00:00
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2022-01-24 18:52:57 +00:00
|
|
|
"tailscale.com/envknob"
|
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 {
|
2022-09-14 19:49:39 +00:00
|
|
|
tunMTU := DefaultMTU
|
|
|
|
if mtu, ok := envknob.LookupInt("TS_DEBUG_MTU"); ok {
|
|
|
|
tunMTU = mtu
|
|
|
|
}
|
2021-07-23 16:45:04 +00:00
|
|
|
dev, err = tun.CreateTUN(tunName, tunMTU)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|