Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support unsafe_routes for use with port_forwarding and user_tun #3

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions overlay/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,42 @@ package overlay
import (
"io"
"net/netip"
"sync/atomic"

"github.com/gaissmai/bart"
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/config"
)

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
or, ow := io.Pipe()
ir, iw := io.Pipe()
Expand All @@ -33,14 +59,24 @@ type UserDevice struct {

inboundReader *io.PipeReader
inboundWriter *io.PipeWriter

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 {
ptr := d.routeTree.Load()
if ptr != nil {
r, _ := d.routeTree.Load().Lookup(ip)
return r
} else {
return ip
}
}
func (d *UserDevice) NewMultiQueueReader() (io.ReadWriteCloser, error) {
return d, nil
}
Expand Down
Loading