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 21:20:09 +01:00
|
|
|
package router
|
2020-02-12 06:57:45 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-08 01:18:18 +01: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 06:17:30 +01:00
|
|
|
"inet.af/netaddr"
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-07-31 21:27:09 +01:00
|
|
|
"tailscale.com/wgengine/router/dns"
|
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 06:17:30 +01:00
|
|
|
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-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-07-31 21:27:09 +01:00
|
|
|
|
|
|
|
mconfig := dns.ManagerConfig{
|
|
|
|
Logf: logf,
|
|
|
|
InterfaceName: tunname,
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
return &openbsdRouter{
|
2020-02-12 06:57:45 +00:00
|
|
|
logf: logf,
|
|
|
|
tunname: tunname,
|
2020-07-31 21:27:09 +01:00
|
|
|
dns: dns.NewManager(mconfig),
|
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 {
|
2020-09-01 21:27:42 +01:00
|
|
|
log.Fatalf("exec.Cmd(%#v) invalid; need argv[0]", args)
|
2020-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
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 08:08:52 +01:00
|
|
|
func (r *openbsdRouter) Set(cfg *Config) error {
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = &shutdownConfig
|
|
|
|
}
|
|
|
|
|
2020-05-08 01:18:18 +01:00
|
|
|
// TODO: support configuring multiple local addrs on interface.
|
2020-07-31 21:27:09 +01:00
|
|
|
if len(cfg.LocalAddrs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(cfg.LocalAddrs) > 1 {
|
2020-05-08 01:18:18 +01:00
|
|
|
return errors.New("freebsd doesn't support setting multiple local addrs yet")
|
|
|
|
}
|
2020-05-12 08:08:52 +01:00
|
|
|
localAddr := cfg.LocalAddrs[0]
|
2020-05-08 01:18:18 +01:00
|
|
|
|
2020-02-12 06:57:45 +00:00
|
|
|
var errq error
|
|
|
|
|
2020-05-08 01:18:18 +01:00
|
|
|
if localAddr != r.local {
|
2020-12-24 20:33:55 +00:00
|
|
|
if !r.local.IsZero() {
|
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 01:18:18 +01: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 01:18:18 +01: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 06:17:30 +01:00
|
|
|
newRoutes := make(map[netaddr.IPPrefix]struct{})
|
2020-05-12 08:08:52 +01:00
|
|
|
for _, route := range cfg.Routes {
|
2020-05-08 02:07:13 +01: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 06:17:30 +01: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 01:18:18 +01: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 06:17:30 +01: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 01:18:18 +01: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 01:18:18 +01:00
|
|
|
r.local = localAddr
|
2020-02-12 06:57:45 +00:00
|
|
|
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-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errq
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
func (r *openbsdRouter) Close() error {
|
2020-07-31 21:27:09 +01:00
|
|
|
if err := r.dns.Down(); err != nil {
|
|
|
|
return fmt.Errorf("dns down: %v", err)
|
|
|
|
}
|
2020-07-14 14:12:00 +01:00
|
|
|
cleanup(r.logf, r.tunname)
|
2020-02-16 06:26:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 14:12:00 +01:00
|
|
|
func cleanup(logf logger.Logf, interfaceName string) {
|
|
|
|
out, err := cmd("ifconfig", interfaceName, "down").CombinedOutput()
|
2020-02-16 06:26:19 +00:00
|
|
|
if err != nil {
|
2020-07-14 14:12:00 +01:00
|
|
|
logf("ifconfig down: %v\n%s", err, out)
|
2020-02-16 06:26:19 +00:00
|
|
|
}
|
|
|
|
}
|