2020-06-14 13:18:38 +01:00
|
|
|
// Copyright (c) 2020 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.
|
|
|
|
|
|
|
|
// +build darwin freebsd
|
|
|
|
|
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/types/logger"
|
2020-08-11 18:04:07 +01:00
|
|
|
"tailscale.com/version"
|
2020-07-31 21:27:09 +01:00
|
|
|
"tailscale.com/wgengine/router/dns"
|
2020-06-14 13:18:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type userspaceBSDRouter struct {
|
|
|
|
logf logger.Logf
|
|
|
|
tunname string
|
|
|
|
local netaddr.IPPrefix
|
|
|
|
routes map[netaddr.IPPrefix]struct{}
|
2020-07-14 14:12:00 +01:00
|
|
|
|
2020-07-31 21:27:09 +01:00
|
|
|
dns *dns.Manager
|
2020-06-14 13:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newUserspaceBSDRouter(logf logger.Logf, _ *device.Device, tundev tun.Device) (Router, error) {
|
|
|
|
tunname, err := tundev.Name()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-31 21:27:09 +01:00
|
|
|
|
|
|
|
mconfig := dns.ManagerConfig{
|
|
|
|
Logf: logf,
|
|
|
|
InterfaceName: tunname,
|
|
|
|
}
|
|
|
|
|
2020-06-14 13:18:38 +01:00
|
|
|
return &userspaceBSDRouter{
|
|
|
|
logf: logf,
|
|
|
|
tunname: tunname,
|
2020-07-31 21:27:09 +01:00
|
|
|
dns: dns.NewManager(mconfig),
|
2020-06-14 13:18:38 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 14:12:00 +01:00
|
|
|
func cmd(args ...string) *exec.Cmd {
|
2020-06-14 13:18:38 +01:00
|
|
|
if len(args) == 0 {
|
2020-09-01 21:27:42 +01:00
|
|
|
log.Fatalf("exec.Cmd(%#v) invalid; need argv[0]", args)
|
2020-06-14 13:18:38 +01:00
|
|
|
}
|
|
|
|
return exec.Command(args[0], args[1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userspaceBSDRouter) Up() error {
|
|
|
|
ifup := []string{"ifconfig", r.tunname, "up"}
|
2020-07-14 14:12:00 +01:00
|
|
|
if out, err := cmd(ifup...).CombinedOutput(); err != nil {
|
2020-06-14 13:18:38 +01:00
|
|
|
r.logf("running ifconfig failed: %v\n%s", err, out)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userspaceBSDRouter) Set(cfg *Config) error {
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = &shutdownConfig
|
|
|
|
}
|
|
|
|
if len(cfg.LocalAddrs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// TODO: support configuring multiple local addrs on interface.
|
|
|
|
if len(cfg.LocalAddrs) != 1 {
|
|
|
|
return errors.New("freebsd doesn't support setting multiple local addrs yet")
|
|
|
|
}
|
|
|
|
localAddr := cfg.LocalAddrs[0]
|
|
|
|
|
|
|
|
var errq error
|
|
|
|
|
|
|
|
// Update the address.
|
|
|
|
if localAddr != r.local {
|
|
|
|
// If the interface is already set, remove it.
|
|
|
|
if r.local != (netaddr.IPPrefix{}) {
|
|
|
|
addrdel := []string{"ifconfig", r.tunname,
|
|
|
|
"inet", r.local.String(), "-alias"}
|
2020-07-14 14:12:00 +01:00
|
|
|
out, err := cmd(addrdel...).CombinedOutput()
|
2020-06-14 13:18:38 +01:00
|
|
|
if err != nil {
|
|
|
|
r.logf("addr del failed: %v: %v\n%s", addrdel, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the interface.
|
|
|
|
addradd := []string{"ifconfig", r.tunname,
|
|
|
|
"inet", localAddr.String(), localAddr.IP.String()}
|
2020-07-14 14:12:00 +01:00
|
|
|
out, err := cmd(addradd...).CombinedOutput()
|
2020-06-14 13:18:38 +01:00
|
|
|
if err != nil {
|
|
|
|
r.logf("addr add failed: %v: %v\n%s", addradd, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newRoutes := make(map[netaddr.IPPrefix]struct{})
|
|
|
|
for _, route := range cfg.Routes {
|
|
|
|
newRoutes[route] = struct{}{}
|
|
|
|
}
|
|
|
|
// Delete any pre-existing routes.
|
|
|
|
for route := range r.routes {
|
|
|
|
if _, keep := newRoutes[route]; !keep {
|
|
|
|
net := route.IPNet()
|
|
|
|
nip := net.IP.Mask(net.Mask)
|
|
|
|
nstr := fmt.Sprintf("%v/%d", nip, route.Bits)
|
2020-08-11 18:04:07 +01:00
|
|
|
del := "del"
|
|
|
|
if version.OS() == "macOS" {
|
|
|
|
del = "delete"
|
|
|
|
}
|
2020-06-14 13:18:38 +01:00
|
|
|
routedel := []string{"route", "-q", "-n",
|
2020-08-11 18:04:07 +01:00
|
|
|
del, "-inet", nstr,
|
2020-06-14 13:18:38 +01:00
|
|
|
"-iface", r.tunname}
|
2020-07-14 14:12:00 +01:00
|
|
|
out, err := cmd(routedel...).CombinedOutput()
|
2020-06-14 13:18:38 +01:00
|
|
|
if err != nil {
|
|
|
|
r.logf("route del failed: %v: %v\n%s", routedel, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add the routes.
|
|
|
|
for route := range newRoutes {
|
|
|
|
if _, exists := r.routes[route]; !exists {
|
|
|
|
net := route.IPNet()
|
|
|
|
nip := net.IP.Mask(net.Mask)
|
|
|
|
nstr := fmt.Sprintf("%v/%d", nip, route.Bits)
|
|
|
|
routeadd := []string{"route", "-q", "-n",
|
|
|
|
"add", "-inet", nstr,
|
|
|
|
"-iface", r.tunname}
|
2020-07-14 14:12:00 +01:00
|
|
|
out, err := cmd(routeadd...).CombinedOutput()
|
2020-06-14 13:18:38 +01:00
|
|
|
if err != nil {
|
|
|
|
r.logf("addr add failed: %v: %v\n%s", routeadd, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the interface and routes so we know what to change on an update.
|
|
|
|
r.local = localAddr
|
|
|
|
r.routes = newRoutes
|
|
|
|
|
2020-07-31 21:27:09 +01:00
|
|
|
if err := r.dns.Set(cfg.DNS); err != nil {
|
|
|
|
errq = fmt.Errorf("dns set: %v", err)
|
2020-06-14 13:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return errq
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userspaceBSDRouter) Close() error {
|
2020-07-31 21:27:09 +01:00
|
|
|
if err := r.dns.Down(); err != nil {
|
2020-07-30 02:09:18 +01:00
|
|
|
r.logf("dns down: %v", err)
|
|
|
|
}
|
|
|
|
// No interface cleanup is necessary during normal shutdown.
|
2020-06-14 13:18:38 +01:00
|
|
|
return nil
|
|
|
|
}
|