fix unsafe routes darwin (#610)

With Nebula 1.4.0, if you create an unsafe_route that has a collision with an existing route on the system, the unsafe_route will be silently dropped (and the existing system route remains).

With Nebula 1.5.0, this same situation will cause Nebula to fail to start with an error (EEXIST).

This change restores the Nebula 1.4.0 behavior (but with a WARN log as well).
This commit is contained in:
Wade Simmons 2021-12-14 11:52:49 -05:00 committed by GitHub
parent 068a93d1f4
commit e434ba6523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 3 deletions

View File

@ -4,6 +4,7 @@
package overlay package overlay
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -272,6 +273,9 @@ func (t *tun) Activate() error {
copy(maskAddr.IP[:], mask[:]) copy(maskAddr.IP[:], mask[:])
err = addRoute(routeSock, routeAddr, maskAddr, linkAddr) err = addRoute(routeSock, routeAddr, maskAddr, linkAddr)
if err != nil { if err != nil {
if errors.Is(err, unix.EEXIST) {
err = fmt.Errorf("unable to add tun route, identical route already exists: %s", t.cidr)
}
return err return err
} }
@ -293,8 +297,13 @@ func (t *tun) Activate() error {
err = addRoute(routeSock, routeAddr, maskAddr, linkAddr) err = addRoute(routeSock, routeAddr, maskAddr, linkAddr)
if err != nil { if err != nil {
if errors.Is(err, unix.EEXIST) {
t.l.WithField("route", r.Cidr).
Warnf("unable to add unsafe_route, identical route already exists")
} else {
return err return err
} }
}
// TODO how to set metric // TODO how to set metric
} }
@ -354,11 +363,11 @@ func addRoute(sock int, addr, mask *netroute.Inet4Addr, link *netroute.LinkAddr)
data, err := r.Marshal() data, err := r.Marshal()
if err != nil { if err != nil {
return fmt.Errorf("failed to create route.RouteMessage: %v", err) return fmt.Errorf("failed to create route.RouteMessage: %w", err)
} }
_, err = unix.Write(sock, data[:]) _, err = unix.Write(sock, data[:])
if err != nil { if err != nil {
return fmt.Errorf("failed to write route.RouteMessage to socket: %v", err) return fmt.Errorf("failed to write route.RouteMessage to socket: %w", err)
} }
return nil return nil