Skip to content

Commit

Permalink
Allow disabling custom routing (#1840)
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal authored Apr 12, 2024
1 parent 15a2feb commit d30cf87
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 16 deletions.
25 changes: 18 additions & 7 deletions client/internal/routemanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/netbirdio/netbird/client/internal/peer"
"github.com/netbirdio/netbird/iface"
"github.com/netbirdio/netbird/route"
nbnet "github.com/netbirdio/netbird/util/net"
"github.com/netbirdio/netbird/version"
)

Expand Down Expand Up @@ -68,6 +69,10 @@ func NewManager(ctx context.Context, pubKey string, wgInterface *iface.WGIface,

// Init sets up the routing
func (m *DefaultManager) Init() (peer.BeforeAddPeerHookFunc, peer.AfterRemovePeerHookFunc, error) {
if nbnet.CustomRoutingDisabled() {
return nil, nil, nil
}

if err := cleanupRouting(); err != nil {
log.Warnf("Failed cleaning up routing: %v", err)
}
Expand Down Expand Up @@ -99,11 +104,15 @@ func (m *DefaultManager) Stop() {
if m.serverRouter != nil {
m.serverRouter.cleanUp()
}
if err := cleanupRouting(); err != nil {
log.Errorf("Error cleaning up routing: %v", err)
} else {
log.Info("Routing cleanup complete")

if !nbnet.CustomRoutingDisabled() {
if err := cleanupRouting(); err != nil {
log.Errorf("Error cleaning up routing: %v", err)
} else {
log.Info("Routing cleanup complete")
}
}

m.ctx = nil
}

Expand Down Expand Up @@ -210,9 +219,11 @@ func (m *DefaultManager) clientRoutes(initialRoutes []*route.Route) []*route.Rou
}

func isPrefixSupported(prefix netip.Prefix) bool {
switch runtime.GOOS {
case "linux", "windows", "darwin":
return true
if !nbnet.CustomRoutingDisabled() {
switch runtime.GOOS {
case "linux", "windows", "darwin":
return true
}
}

// If prefix is too small, lets assume it is a possible default prefix which is not yet supported
Expand Down
2 changes: 1 addition & 1 deletion client/internal/routemanager/systemops_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
var ErrTableIDExists = errors.New("ID exists with different name")

var routeManager = &RouteManager{}
var isLegacy = os.Getenv("NB_USE_LEGACY_ROUTING") == "true"
var isLegacy = os.Getenv("NB_USE_LEGACY_ROUTING") == "true" || nbnet.CustomRoutingDisabled()

type ruleParams struct {
priority int
Expand Down
2 changes: 1 addition & 1 deletion client/internal/wgproxy/proxy_ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (p *WGEBPFProxy) prepareSenderRawSocket() (net.PacketConn, error) {
}

// Set the fwmark on the socket.
err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_MARK, nbnet.NetbirdFwmark)
err = nbnet.SetSocketOpt(fd)
if err != nil {
return nil, fmt.Errorf("setting fwmark failed: %w", err)
}
Expand Down
4 changes: 1 addition & 3 deletions iface/wg_configurer_kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
log "github.com/sirupsen/logrus"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

nbnet "github.com/netbirdio/netbird/util/net"
)

type wgKernelConfigurer struct {
Expand All @@ -31,7 +29,7 @@ func (c *wgKernelConfigurer) configureInterface(privateKey string, port int) err
if err != nil {
return err
}
fwmark := nbnet.NetbirdFwmark
fwmark := getFwmark()
config := wgtypes.Config{
PrivateKey: &key,
ReplacePeers: true,
Expand Down
2 changes: 1 addition & 1 deletion iface/wg_configurer_usp.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func toWgUserspaceString(wgCfg wgtypes.Config) string {
}

func getFwmark() int {
if runtime.GOOS == "linux" {
if runtime.GOOS == "linux" && !nbnet.CustomRoutingDisabled() {
return nbnet.NetbirdFwmark
}
return 0
Expand Down
12 changes: 12 additions & 0 deletions util/net/dialer_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func RemoveDialerHooks() {

// DialContext wraps the net.Dialer's DialContext method to use the custom connection
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
if CustomRoutingDisabled() {
return d.Dialer.DialContext(ctx, network, address)
}

var resolver *net.Resolver
if d.Resolver != nil {
resolver = d.Resolver
Expand Down Expand Up @@ -123,6 +127,10 @@ func callDialerHooks(ctx context.Context, connID ConnectionID, address string, r
}

func DialUDP(network string, laddr, raddr *net.UDPAddr) (*net.UDPConn, error) {
if CustomRoutingDisabled() {
return net.DialUDP(network, laddr, raddr)
}

dialer := NewDialer()
dialer.LocalAddr = laddr

Expand All @@ -143,6 +151,10 @@ func DialUDP(network string, laddr, raddr *net.UDPAddr) (*net.UDPConn, error) {
}

func DialTCP(network string, laddr, raddr *net.TCPAddr) (*net.TCPConn, error) {
if CustomRoutingDisabled() {
return net.DialTCP(network, laddr, raddr)
}

dialer := NewDialer()
dialer.LocalAddr = laddr

Expand Down
11 changes: 10 additions & 1 deletion util/net/listener_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"sync"

"github.com/pion/transport/v3"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -52,6 +53,10 @@ func RemoveListenerHooks() {
// ListenPacket listens on the network address and returns a PacketConn
// which includes support for write hooks.
func (l *ListenerConfig) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) {
if CustomRoutingDisabled() {
return l.ListenConfig.ListenPacket(ctx, network, address)
}

pc, err := l.ListenConfig.ListenPacket(ctx, network, address)
if err != nil {
return nil, fmt.Errorf("listen packet: %w", err)
Expand Down Expand Up @@ -144,7 +149,11 @@ func closeConn(id ConnectionID, conn net.PacketConn) error {

// ListenUDP listens on the network address and returns a transport.UDPConn
// which includes support for write and close hooks.
func ListenUDP(network string, laddr *net.UDPAddr) (*UDPConn, error) {
func ListenUDP(network string, laddr *net.UDPAddr) (transport.UDPConn, error) {
if CustomRoutingDisabled() {
return net.ListenUDP(network, laddr)
}

conn, err := NewListener().ListenPacket(context.Background(), network, laddr.String())
if err != nil {
return nil, fmt.Errorf("listen UDP: %w", err)
Expand Down
12 changes: 11 additions & 1 deletion util/net/net.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package net

import "github.com/google/uuid"
import (
"os"

"github.com/google/uuid"
)

const (
// NetbirdFwmark is the fwmark value used by Netbird via wireguard
NetbirdFwmark = 0x1BD00

envDisableCustomRouting = "NB_DISABLE_CUSTOM_ROUTING"
)

// ConnectionID provides a globally unique identifier for network connections.
Expand All @@ -15,3 +21,7 @@ type ConnectionID string
func GenerateConnID() ConnectionID {
return ConnectionID(uuid.NewString())
}

func CustomRoutingDisabled() bool {
return os.Getenv(envDisableCustomRouting) == "true"
}
10 changes: 9 additions & 1 deletion util/net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func SetRawSocketMark(conn syscall.RawConn) error {
var setErr error

err := conn.Control(func(fd uintptr) {
setErr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, NetbirdFwmark)
setErr = SetSocketOpt(int(fd))
})
if err != nil {
return fmt.Errorf("control: %w", err)
Expand All @@ -33,3 +33,11 @@ func SetRawSocketMark(conn syscall.RawConn) error {

return nil
}

func SetSocketOpt(fd int) error {
if CustomRoutingDisabled() {
return nil
}

return syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_MARK, NetbirdFwmark)
}

0 comments on commit d30cf87

Please sign in to comment.