mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.1 KiB
Go
159 lines
4.1 KiB
Go
5 years ago
|
// 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"
|
||
|
"net"
|
||
|
"os/exec"
|
||
|
|
||
|
"github.com/tailscale/wireguard-go/device"
|
||
|
"github.com/tailscale/wireguard-go/tun"
|
||
|
"github.com/tailscale/wireguard-go/wgcfg"
|
||
5 years ago
|
"tailscale.com/types/logger"
|
||
5 years ago
|
)
|
||
|
|
||
|
// For now this router only supports the userspace WireGuard implementations.
|
||
|
//
|
||
5 years ago
|
// Work is currently underway for an in-kernel FreeBSD implementation of wireguard
|
||
|
// https://svnweb.freebsd.org/base?view=revision&revision=357986
|
||
5 years ago
|
|
||
5 years ago
|
type freebsdRouter struct {
|
||
5 years ago
|
logf logger.Logf
|
||
5 years ago
|
tunname string
|
||
|
local wgcfg.CIDR
|
||
|
routes map[wgcfg.CIDR]struct{}
|
||
|
}
|
||
|
|
||
5 years ago
|
func newUserspaceRouter(logf logger.Logf, _ *device.Device, tundev tun.Device) (Router, error) {
|
||
5 years ago
|
tunname, err := tundev.Name()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 years ago
|
return &freebsdRouter{
|
||
5 years ago
|
logf: logf,
|
||
|
tunname: tunname,
|
||
5 years ago
|
}, nil
|
||
5 years ago
|
}
|
||
|
|
||
|
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:]...)
|
||
|
}
|
||
|
|
||
5 years ago
|
func (r *freebsdRouter) Up() error {
|
||
5 years ago
|
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
|
||
|
}
|
||
|
|
||
5 years ago
|
func (r *freebsdRouter) SetRoutes(rs RouteSettings) error {
|
||
5 years ago
|
var errq error
|
||
|
|
||
5 years ago
|
// Update the address.
|
||
5 years ago
|
if rs.LocalAddr != r.local {
|
||
5 years ago
|
// If the interface is already set, remove it.
|
||
5 years ago
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Add the interface.
|
||
5 years ago
|
addradd := []string{"ifconfig", r.tunname,
|
||
5 years ago
|
"inet", rs.LocalAddr.String(), rs.LocalAddr.IP.String()}
|
||
5 years ago
|
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{}{}
|
||
|
}
|
||
|
}
|
||
5 years ago
|
// Delete any pre-existing routes.
|
||
5 years ago
|
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,
|
||
5 years ago
|
"-iface", r.tunname}
|
||
5 years ago
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
5 years ago
|
// Add the routes.
|
||
5 years ago
|
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,
|
||
5 years ago
|
"-iface", r.tunname}
|
||
5 years ago
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Store the interface and routes so we know what to change on an update.
|
||
5 years ago
|
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
|
||
|
}
|
||
|
|
||
5 years ago
|
func (r *freebsdRouter) Close() error {
|
||
5 years ago
|
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)
|
||
|
}
|
||
|
|
||
5 years ago
|
return nil
|
||
5 years ago
|
}
|
||
|
|
||
|
// 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.
|
||
5 years ago
|
func (r *freebsdRouter) replaceResolvConf(_ []net.IP, _ []string) error { return nil }
|
||
|
func (r *freebsdRouter) restoreResolvConf() error { return nil }
|