2020-02-12 06:57:45 +00: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.
|
|
|
|
|
2020-04-30 20:20:09 +00:00
|
|
|
package router
|
2020-02-12 06:57:45 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-08 00:18:18 +00:00
|
|
|
"errors"
|
2020-02-12 06:57:45 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2020-05-08 05:17:30 +00:00
|
|
|
"inet.af/netaddr"
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-12 06:57:45 +00:00
|
|
|
)
|
|
|
|
|
2020-02-16 06:26:19 +00:00
|
|
|
// For now this router only supports the WireGuard userspace implementation.
|
|
|
|
// There is an experimental kernel version in the works for OpenBSD:
|
2020-02-12 06:57:45 +00:00
|
|
|
// https://git.zx2c4.com/wireguard-openbsd.
|
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
type openbsdRouter struct {
|
2020-02-12 23:58:49 +00:00
|
|
|
logf logger.Logf
|
2020-02-12 06:57:45 +00:00
|
|
|
tunname string
|
2020-05-08 05:17:30 +00:00
|
|
|
local netaddr.IPPrefix
|
|
|
|
routes map[netaddr.IPPrefix]struct{}
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
dnsConfig DNSConfig
|
2020-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
func newUserspaceRouter(logf logger.Logf, _ *device.Device, tundev tun.Device) (Router, error) {
|
2020-02-14 23:03:25 +00:00
|
|
|
tunname, err := tundev.Name()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-17 02:37:45 +00:00
|
|
|
return &openbsdRouter{
|
2020-02-12 06:57:45 +00:00
|
|
|
logf: logf,
|
|
|
|
tunname: tunname,
|
2020-02-14 23:03:25 +00:00
|
|
|
}, nil
|
2020-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmd(args ...string) *exec.Cmd {
|
|
|
|
if len(args) == 0 {
|
|
|
|
log.Fatalf("exec.Cmd(%#v) invalid; need argv[0]\n", args)
|
|
|
|
}
|
|
|
|
return exec.Command(args[0], args[1:]...)
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
func (r *openbsdRouter) Up() error {
|
2020-02-12 06:57:45 +00:00
|
|
|
ifup := []string{"ifconfig", r.tunname, "up"}
|
|
|
|
if out, err := cmd(ifup...).CombinedOutput(); err != nil {
|
|
|
|
r.logf("running ifconfig failed: %v\n%s", err, out)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-12 07:08:52 +00:00
|
|
|
func (r *openbsdRouter) Set(cfg *Config) error {
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = &shutdownConfig
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:18:18 +00:00
|
|
|
// TODO: support configuring multiple local addrs on interface.
|
2020-05-12 07:08:52 +00:00
|
|
|
if len(cfg.LocalAddrs) != 1 {
|
2020-05-08 00:18:18 +00:00
|
|
|
return errors.New("freebsd doesn't support setting multiple local addrs yet")
|
|
|
|
}
|
2020-05-12 07:08:52 +00:00
|
|
|
localAddr := cfg.LocalAddrs[0]
|
2020-05-08 00:18:18 +00:00
|
|
|
|
2020-02-12 06:57:45 +00:00
|
|
|
var errq error
|
|
|
|
|
2020-05-08 00:18:18 +00:00
|
|
|
if localAddr != r.local {
|
2020-05-08 05:17:30 +00:00
|
|
|
if r.local != (netaddr.IPPrefix{}) {
|
2020-02-12 06:57:45 +00:00
|
|
|
addrdel := []string{"ifconfig", r.tunname,
|
|
|
|
"inet", r.local.String(), "-alias"}
|
|
|
|
out, err := cmd(addrdel...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
r.logf("addr del failed: %v: %v\n%s", addrdel, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
routedel := []string{"route", "-q", "-n",
|
|
|
|
"del", "-inet", r.local.String(),
|
|
|
|
"-iface", r.local.IP.String()}
|
|
|
|
if out, err := cmd(routedel...).CombinedOutput(); err != nil {
|
|
|
|
r.logf("route del failed: %v: %v\n%s", routedel, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addradd := []string{"ifconfig", r.tunname,
|
2020-05-08 00:18:18 +00:00
|
|
|
"inet", localAddr.String(), "alias"}
|
2020-02-12 06:57:45 +00:00
|
|
|
out, err := cmd(addradd...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
r.logf("addr add failed: %v: %v\n%s", addradd, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
routeadd := []string{"route", "-q", "-n",
|
2020-05-08 00:18:18 +00:00
|
|
|
"add", "-inet", localAddr.String(),
|
|
|
|
"-iface", localAddr.IP.String()}
|
2020-02-12 06:57:45 +00:00
|
|
|
if out, err := cmd(routeadd...).CombinedOutput(); err != nil {
|
|
|
|
r.logf("route add failed: %v: %v\n%s", routeadd, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 05:17:30 +00:00
|
|
|
newRoutes := make(map[netaddr.IPPrefix]struct{})
|
2020-05-12 07:08:52 +00:00
|
|
|
for _, route := range cfg.Routes {
|
2020-05-08 01:07:13 +00:00
|
|
|
newRoutes[route] = struct{}{}
|
2020-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
for route := range r.routes {
|
|
|
|
if _, keep := newRoutes[route]; !keep {
|
|
|
|
net := route.IPNet()
|
|
|
|
nip := net.IP.Mask(net.Mask)
|
2020-05-08 05:17:30 +00:00
|
|
|
nstr := fmt.Sprintf("%v/%d", nip, route.Bits)
|
2020-02-12 06:57:45 +00:00
|
|
|
routedel := []string{"route", "-q", "-n",
|
|
|
|
"del", "-inet", nstr,
|
2020-05-08 00:18:18 +00:00
|
|
|
"-iface", localAddr.IP.String()}
|
2020-02-12 06:57:45 +00:00
|
|
|
out, err := cmd(routedel...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
r.logf("route del failed: %v: %v\n%s", routedel, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for route := range newRoutes {
|
|
|
|
if _, exists := r.routes[route]; !exists {
|
|
|
|
net := route.IPNet()
|
|
|
|
nip := net.IP.Mask(net.Mask)
|
2020-05-08 05:17:30 +00:00
|
|
|
nstr := fmt.Sprintf("%v/%d", nip, route.Bits)
|
2020-02-12 06:57:45 +00:00
|
|
|
routeadd := []string{"route", "-q", "-n",
|
|
|
|
"add", "-inet", nstr,
|
2020-05-08 00:18:18 +00:00
|
|
|
"-iface", localAddr.IP.String()}
|
2020-02-12 06:57:45 +00:00
|
|
|
out, err := cmd(routeadd...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
r.logf("addr add failed: %v: %v\n%s", routeadd, err, out)
|
|
|
|
if errq == nil {
|
|
|
|
errq = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:18:18 +00:00
|
|
|
r.local = localAddr
|
2020-02-12 06:57:45 +00:00
|
|
|
r.routes = newRoutes
|
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
if !r.dnsConfig.EquivalentTo(cfg.DNSConfig) {
|
|
|
|
if err := dnsDirectUp(cfg.DNSConfig); err != nil {
|
|
|
|
errq = fmt.Errorf("dns up: direct: %v", err)
|
|
|
|
} else {
|
|
|
|
r.dnsConfig = cfg.DNSConfig
|
|
|
|
}
|
2020-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errq
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
func (r *openbsdRouter) Close() error {
|
2020-07-14 13:12:00 +00:00
|
|
|
cleanup(r.logf, r.tunname)
|
2020-02-16 06:26:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
func cleanup(logf logger.Logf, interfaceName string) {
|
|
|
|
if err := dnsDirectDown(); err != nil {
|
|
|
|
logf("dns down: direct: %v", err)
|
2020-02-16 06:26:19 +00:00
|
|
|
}
|
2020-07-14 13:12:00 +00:00
|
|
|
out, err := cmd("ifconfig", interfaceName, "down").CombinedOutput()
|
2020-02-16 06:26:19 +00:00
|
|
|
if err != nil {
|
2020-07-14 13:12:00 +00:00
|
|
|
logf("ifconfig down: %v\n%s", err, out)
|
2020-02-16 06:26:19 +00:00
|
|
|
}
|
|
|
|
}
|