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.
|
|
|
|
|
|
|
|
package wgengine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-12 06:57:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// For now this router only supports the userspace WireGuard implementations.
|
|
|
|
//
|
2020-02-17 02:37:45 +00:00
|
|
|
// Work is currently underway for an in-kernel FreeBSD implementation of wireguard
|
|
|
|
// https://svnweb.freebsd.org/base?view=revision&revision=357986
|
2020-02-12 06:57:45 +00:00
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
type freebsdRouter struct {
|
2020-02-12 23:58:49 +00:00
|
|
|
logf logger.Logf
|
2020-02-12 06:57:45 +00:00
|
|
|
tunname string
|
|
|
|
local wgcfg.CIDR
|
|
|
|
routes map[wgcfg.CIDR]struct{}
|
|
|
|
}
|
|
|
|
|
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 &freebsdRouter{
|
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 *freebsdRouter) 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-02-17 02:37:45 +00:00
|
|
|
func (r *freebsdRouter) SetRoutes(rs RouteSettings) error {
|
2020-02-12 06:57:45 +00:00
|
|
|
var errq error
|
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
// Update the address.
|
2020-02-12 06:57:45 +00:00
|
|
|
if rs.LocalAddr != r.local {
|
2020-02-17 02:37:45 +00:00
|
|
|
// If the interface is already set, remove it.
|
2020-02-12 06:57:45 +00:00
|
|
|
if r.local != (wgcfg.CIDR{}) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
// Add the interface.
|
2020-02-12 06:57:45 +00:00
|
|
|
addradd := []string{"ifconfig", r.tunname,
|
2020-02-17 02:37:45 +00:00
|
|
|
"inet", rs.LocalAddr.String(), rs.LocalAddr.IP.String()}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newRoutes := make(map[wgcfg.CIDR]struct{})
|
|
|
|
for _, peer := range rs.Cfg.Peers {
|
|
|
|
for _, route := range peer.AllowedIPs {
|
|
|
|
newRoutes[route] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 02:37:45 +00:00
|
|
|
// Delete any pre-existing routes.
|
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)
|
|
|
|
nstr := fmt.Sprintf("%v/%d", nip, route.Mask)
|
|
|
|
routedel := []string{"route", "-q", "-n",
|
|
|
|
"del", "-inet", nstr,
|
2020-02-17 02:37:45 +00:00
|
|
|
"-iface", r.tunname}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 02:37:45 +00:00
|
|
|
// Add the routes.
|
2020-02-12 06:57:45 +00:00
|
|
|
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.Mask)
|
|
|
|
routeadd := []string{"route", "-q", "-n",
|
|
|
|
"add", "-inet", nstr,
|
2020-02-17 02:37:45 +00:00
|
|
|
"-iface", r.tunname}
|
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-02-17 02:37:45 +00:00
|
|
|
// Store the interface and routes so we know what to change on an update.
|
2020-02-12 06:57:45 +00:00
|
|
|
r.local = rs.LocalAddr
|
|
|
|
r.routes = newRoutes
|
|
|
|
|
|
|
|
if err := r.replaceResolvConf(rs.DNS, rs.DNSDomains); err != nil {
|
|
|
|
errq = fmt.Errorf("replacing resolv.conf failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errq
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
func (r *freebsdRouter) Close() error {
|
2020-02-12 06:57:45 +00:00
|
|
|
out, err := cmd("ifconfig", r.tunname, "down").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
r.logf("running ifconfig failed: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.restoreResolvConf(); err != nil {
|
|
|
|
r.logf("failed to restore system resolv.conf: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-13 00:16:05 +00:00
|
|
|
return nil
|
2020-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mbaillie): these are no-ops for now. They could re-use the Linux funcs
|
|
|
|
// (sans systemd parts), but I note Linux DNS is disabled(?) so leaving for now.
|
2020-02-19 16:53:35 +00:00
|
|
|
func (r *freebsdRouter) replaceResolvConf(_ []wgcfg.IP, _ []string) error { return nil }
|
|
|
|
func (r *freebsdRouter) restoreResolvConf() error { return nil }
|