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 <fnichol@nichol.ca>
pull/2108/head
Fletcher Nichol 4 years ago committed by Brad Fitzpatrick
parent 144c68b80b
commit a49df5cfda

@ -55,6 +55,13 @@ func (r *openbsdRouter) Up() error {
return nil return nil
} }
func inet(p netaddr.IPPrefix) string {
if p.IP().Is6() {
return "inet6"
}
return "inet"
}
func (r *openbsdRouter) Set(cfg *Config) error { func (r *openbsdRouter) Set(cfg *Config) error {
if cfg == nil { if cfg == nil {
cfg = &shutdownConfig cfg = &shutdownConfig
@ -172,9 +179,13 @@ func (r *openbsdRouter) Set(cfg *Config) error {
net := route.IPNet() net := route.IPNet()
nip := net.IP.Mask(net.Mask) nip := net.IP.Mask(net.Mask)
nstr := fmt.Sprintf("%v/%d", nip, route.Bits()) 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", routedel := []string{"route", "-q", "-n",
"del", "-inet", nstr, "del", "-" + inet(route), nstr,
"-iface", localAddr4.IP().String()} "-iface", dst}
out, err := cmd(routedel...).CombinedOutput() out, err := cmd(routedel...).CombinedOutput()
if err != nil { if err != nil {
r.logf("route del failed: %v: %v\n%s", routedel, err, out) 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() net := route.IPNet()
nip := net.IP.Mask(net.Mask) nip := net.IP.Mask(net.Mask)
nstr := fmt.Sprintf("%v/%d", nip, route.Bits()) 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", routeadd := []string{"route", "-q", "-n",
"add", "-inet", nstr, "add", "-" + inet(route), nstr,
"-iface", localAddr4.IP().String()} "-iface", dst}
out, err := cmd(routeadd...).CombinedOutput() out, err := cmd(routeadd...).CombinedOutput()
if err != nil { if err != nil {
r.logf("addr add failed: %v: %v\n%s", routeadd, err, out) r.logf("addr add failed: %v: %v\n%s", routeadd, err, out)

Loading…
Cancel
Save