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>
This commit is contained in:
Fletcher Nichol 2021-06-02 12:44:15 -06:00 committed by Brad Fitzpatrick
parent 144c68b80b
commit a49df5cfda

View File

@ -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)