Skip to content

Commit

Permalink
support unsafe_routes for use with port_forwarding and user_tun
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Aug 5, 2024
1 parent 80d96a1 commit 5be9458
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions overlay/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,43 @@ package overlay
import (
"io"
"net/netip"
"sync/atomic"

"github.com/gaissmai/bart"
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/config"
"gvisor.dev/gvisor/pkg/buffer"
)

func NewUserDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr netip.Prefix, routines int) (Device, error) {
return NewUserDevice(tunCidr)
d, err := NewUserDevice(tunCidr)
if err != nil {
return nil, err
}

_, routes, err := getAllRoutesFromConfig(c, tunCidr, true)
if err != nil {
return nil, err
}

routeTree, err := makeRouteTree(l, routes, true)
if err != nil {
return nil, err
}

newDefaultMTU := c.GetInt("tun.mtu", DefaultMTU)
for i, r := range routes {
if r.MTU == 0 {
routes[i].MTU = newDefaultMTU
}
}

d.routeTree.Store(routeTree)

return d, nil
}

func NewUserDevice(tunCidr netip.Prefix) (Device, error) {
func NewUserDevice(tunCidr netip.Prefix) (*UserDevice, error) {
// these pipes guarantee each write/read will match 1:1
return &UserDevice{
tunCidr: tunCidr,
Expand All @@ -27,14 +53,19 @@ type UserDevice struct {

outboundChannel chan *buffer.View
inboundChannel chan *buffer.View

routeTree atomic.Pointer[bart.Table[netip.Addr]]
}

func (d *UserDevice) Activate() error {
return nil
}
func (d *UserDevice) Cidr() netip.Prefix { return d.tunCidr }
func (d *UserDevice) Name() string { return "faketun0" }
func (d *UserDevice) RouteFor(ip netip.Addr) netip.Addr { return ip }
func (d *UserDevice) RouteFor(ip netip.Addr) netip.Addr {
r, _ := d.routeTree.Load().Lookup(ip)
return r
}
func (d *UserDevice) NewMultiQueueReader() (io.ReadWriteCloser, error) {
return d, nil
}
Expand Down

0 comments on commit 5be9458

Please sign in to comment.