From a49df5cfda0c9a66834df74e5f3bcd1a301980d6 Mon Sep 17 00:00:00 2001 From: Fletcher Nichol Date: Wed, 2 Jun 2021 12:44:15 -0600 Subject: [PATCH] wgenine/router: fix OpenBSD route creation The route creation for the `tun` device was augmented in #1469 but didn't account for adding IPv4 vs. IPv6 routes. There are 2 primary changes as a result: * Ensure that either `-inet` or `-inet6` was used in the [`route(8)`](https://man.openbsd.org/route) command * Use either the `localAddr4` or `localAddr6` for the gateway argument depending which destination network is being added The basis for the approach is based on the implementation from `router_userspace_bsd.go`, including the `inet()` helper function. Fixes #2048 References #1469 Signed-off-by: Fletcher Nichol --- wgengine/router/router_openbsd.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/wgengine/router/router_openbsd.go b/wgengine/router/router_openbsd.go index fe53910f2..5f8c26a03 100644 --- a/wgengine/router/router_openbsd.go +++ b/wgengine/router/router_openbsd.go @@ -55,6 +55,13 @@ func (r *openbsdRouter) Up() error { return nil } +func inet(p netaddr.IPPrefix) string { + if p.IP().Is6() { + return "inet6" + } + return "inet" +} + func (r *openbsdRouter) Set(cfg *Config) error { if cfg == nil { cfg = &shutdownConfig @@ -172,9 +179,13 @@ func (r *openbsdRouter) Set(cfg *Config) error { net := route.IPNet() nip := net.IP.Mask(net.Mask) nstr := fmt.Sprintf("%v/%d", nip, route.Bits()) + dst := localAddr4.IP().String() + if route.IP().Is6() { + dst = localAddr6.IP().String() + } routedel := []string{"route", "-q", "-n", - "del", "-inet", nstr, - "-iface", localAddr4.IP().String()} + "del", "-" + inet(route), nstr, + "-iface", dst} out, err := cmd(routedel...).CombinedOutput() if err != nil { r.logf("route del failed: %v: %v\n%s", routedel, err, out) @@ -189,9 +200,13 @@ func (r *openbsdRouter) Set(cfg *Config) error { net := route.IPNet() nip := net.IP.Mask(net.Mask) nstr := fmt.Sprintf("%v/%d", nip, route.Bits()) + dst := localAddr4.IP().String() + if route.IP().Is6() { + dst = localAddr6.IP().String() + } routeadd := []string{"route", "-q", "-n", - "add", "-inet", nstr, - "-iface", localAddr4.IP().String()} + "add", "-" + inet(route), nstr, + "-iface", dst} out, err := cmd(routeadd...).CombinedOutput() if err != nil { r.logf("addr add failed: %v: %v\n%s", routeadd, err, out)