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"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
"net/netip"
|
2020-02-12 06:57:45 +00:00
|
|
|
"os/exec"
|
|
|
|
|
2022-12-09 23:12:20 +00:00
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2022-07-25 03:08:42 +00:00
|
|
|
"go4.org/netipx"
|
2021-03-26 00:41:51 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-07-20 20:28:06 +00:00
|
|
|
"tailscale.com/wgengine/monitor"
|
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
|
2021-07-20 20:28:06 +00:00
|
|
|
linkMon *monitor.Mon
|
2020-02-12 06:57:45 +00:00
|
|
|
tunname string
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
local4 netip.Prefix
|
|
|
|
local6 netip.Prefix
|
|
|
|
routes map[netip.Prefix]struct{}
|
2020-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 20:28:06 +00:00
|
|
|
func newUserspaceRouter(logf logger.Logf, tundev tun.Device, linkMon *monitor.Mon) (Router, error) {
|
2020-02-14 23:03:25 +00:00
|
|
|
tunname, err := tundev.Name()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2020-02-17 02:37:45 +00:00
|
|
|
return &openbsdRouter{
|
2020-02-12 06:57:45 +00:00
|
|
|
logf: logf,
|
2021-07-20 20:28:06 +00:00
|
|
|
linkMon: linkMon,
|
2020-02-12 06:57:45 +00:00
|
|
|
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 {
|
2020-09-01 20:27:42 +00: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
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func inet(p netip.Prefix) string {
|
2022-07-25 03:08:42 +00:00
|
|
|
if p.Addr().Is6() {
|
2021-06-02 18:44:15 +00:00
|
|
|
return "inet6"
|
|
|
|
}
|
|
|
|
return "inet"
|
|
|
|
}
|
|
|
|
|
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-07-31 20:27:09 +00:00
|
|
|
if len(cfg.LocalAddrs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-09 01:07:05 +00:00
|
|
|
numIPv4 := 0
|
|
|
|
numIPv6 := 0
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
localAddr4 := netip.Prefix{}
|
|
|
|
localAddr6 := netip.Prefix{}
|
2021-03-09 01:07:05 +00:00
|
|
|
for _, addr := range cfg.LocalAddrs {
|
2022-07-25 03:08:42 +00:00
|
|
|
if addr.Addr().Is4() {
|
2021-03-09 01:07:05 +00:00
|
|
|
numIPv4++
|
|
|
|
localAddr4 = addr
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if addr.Addr().Is6() {
|
2021-03-09 01:07:05 +00:00
|
|
|
numIPv6++
|
|
|
|
localAddr6 = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if numIPv4 > 1 || numIPv6 > 1 {
|
|
|
|
return errors.New("openbsd doesn't support setting multiple local addrs yet")
|
2020-05-08 00:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 06:57:45 +00:00
|
|
|
var errq error
|
|
|
|
|
2021-03-09 01:07:05 +00:00
|
|
|
if localAddr4 != r.local4 {
|
2022-07-25 03:08:42 +00:00
|
|
|
if r.local4.IsValid() {
|
2020-02-12 06:57:45 +00:00
|
|
|
addrdel := []string{"ifconfig", r.tunname,
|
2021-03-09 01:07:05 +00:00
|
|
|
"inet", r.local4.String(), "-alias"}
|
2020-02-12 06:57:45 +00:00
|
|
|
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",
|
2021-03-09 01:07:05 +00:00
|
|
|
"del", "-inet", r.local4.String(),
|
2022-07-25 03:08:42 +00:00
|
|
|
"-iface", r.local4.Addr().String()}
|
2020-02-12 06:57:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
if localAddr4.IsValid() {
|
2021-03-09 01:07:05 +00:00
|
|
|
addradd := []string{"ifconfig", r.tunname,
|
|
|
|
"inet", localAddr4.String(), "alias"}
|
|
|
|
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",
|
|
|
|
"add", "-inet", localAddr4.String(),
|
2022-07-25 03:08:42 +00:00
|
|
|
"-iface", localAddr4.Addr().String()}
|
2021-03-09 01:07:05 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
if localAddr6.IsValid() {
|
2021-03-09 01:07:05 +00:00
|
|
|
// in https://github.com/tailscale/tailscale/issues/1307 we made
|
|
|
|
// FreeBSD use a /48 for IPv6 addresses, which is nice because we
|
|
|
|
// don't need to additionally add routing entries. Do that here too.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
localAddr6 = netip.PrefixFrom(localAddr6.Addr(), 48)
|
2021-03-09 01:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if localAddr6 != r.local6 {
|
2022-07-25 03:08:42 +00:00
|
|
|
if r.local6.IsValid() {
|
2021-03-09 01:07:05 +00:00
|
|
|
addrdel := []string{"ifconfig", r.tunname,
|
|
|
|
"inet6", r.local6.String(), "delete"}
|
|
|
|
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-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
if localAddr6.IsValid() {
|
2021-03-09 01:07:05 +00:00
|
|
|
addradd := []string{"ifconfig", r.tunname,
|
|
|
|
"inet6", localAddr6.String()}
|
|
|
|
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
|
|
|
|
}
|
2020-02-12 06:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
newRoutes := make(map[netip.Prefix]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 {
|
2022-07-25 03:08:42 +00:00
|
|
|
net := netipx.PrefixIPNet(route)
|
2020-02-12 06:57:45 +00:00
|
|
|
nip := net.IP.Mask(net.Mask)
|
2021-05-15 01:07:28 +00:00
|
|
|
nstr := fmt.Sprintf("%v/%d", nip, route.Bits())
|
2022-07-25 03:08:42 +00:00
|
|
|
dst := localAddr4.Addr().String()
|
|
|
|
if route.Addr().Is6() {
|
|
|
|
dst = localAddr6.Addr().String()
|
2021-06-02 18:44:15 +00:00
|
|
|
}
|
2020-02-12 06:57:45 +00:00
|
|
|
routedel := []string{"route", "-q", "-n",
|
2021-06-02 18:44:15 +00:00
|
|
|
"del", "-" + inet(route), nstr,
|
|
|
|
"-iface", dst}
|
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 {
|
2022-07-25 03:08:42 +00:00
|
|
|
net := netipx.PrefixIPNet(route)
|
2020-02-12 06:57:45 +00:00
|
|
|
nip := net.IP.Mask(net.Mask)
|
2021-05-15 01:07:28 +00:00
|
|
|
nstr := fmt.Sprintf("%v/%d", nip, route.Bits())
|
2022-07-25 03:08:42 +00:00
|
|
|
dst := localAddr4.Addr().String()
|
|
|
|
if route.Addr().Is6() {
|
|
|
|
dst = localAddr6.Addr().String()
|
2021-06-02 18:44:15 +00:00
|
|
|
}
|
2020-02-12 06:57:45 +00:00
|
|
|
routeadd := []string{"route", "-q", "-n",
|
2021-06-02 18:44:15 +00:00
|
|
|
"add", "-" + inet(route), nstr,
|
|
|
|
"-iface", dst}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 01:07:05 +00:00
|
|
|
r.local4 = localAddr4
|
|
|
|
r.local6 = localAddr6
|
2020-02-12 06:57:45 +00:00
|
|
|
r.routes = newRoutes
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|