Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/unsafe-route-reload'
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmaguire committed Feb 26, 2024
2 parents cc8b3cc + 92d8b55 commit f2a0f31
Show file tree
Hide file tree
Showing 7 changed files with 609 additions and 183 deletions.
25 changes: 25 additions & 0 deletions overlay/route.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package overlay

import (
"bytes"
"fmt"
"math"
"net"
Expand All @@ -21,6 +22,30 @@ type Route struct {
Install bool
}

func (r Route) Equal(t Route) bool {
if !r.Cidr.IP.Equal(t.Cidr.IP) {
return false
}
if !bytes.Equal(r.Cidr.Mask, t.Cidr.Mask) {
return false
}
if r.Metric != t.Metric {
return false
}
if r.MTU != t.MTU {
return false
}
return true
}

func (r Route) String() string {
s := r.Cidr.String()
if r.Metric != 0 {
s += fmt.Sprintf(" metric: %v", r.Metric)
}
return s
}

func makeRouteTree(l *logrus.Logger, routes []Route, allowMTU bool) (*cidr.Tree4[iputil.VpnIp], error) {
routeTree := cidr.NewTree4[iputil.VpnIp]()
for _, r := range routes {
Expand Down
77 changes: 39 additions & 38 deletions overlay/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,61 @@ import (

const DefaultMTU = 1300

// TODO: We may be able to remove routines
type DeviceFactory func(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error)

func NewDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error) {
routes, err := parseRoutes(c, tunCidr)
if err != nil {
return nil, util.NewContextualError("Could not parse tun.routes", nil, err)
}

unsafeRoutes, err := parseUnsafeRoutes(c, tunCidr)
if err != nil {
return nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
}
routes = append(routes, unsafeRoutes...)

switch {
case c.GetBool("tun.disabled", false):
tun := newDisabledTun(tunCidr, c.GetInt("tun.tx_queue", 500), c.GetBool("stats.message_metrics", false), l)
return tun, nil

default:
return newTun(
l,
c.GetString("tun.dev", ""),
tunCidr,
c.GetInt("tun.mtu", DefaultMTU),
routes,
c.GetInt("tun.tx_queue", 500),
routines > 1,
c.GetBool("tun.use_system_route_table", false),
)
return newTun(c, l, tunCidr, routines > 1)

Check failure on line 23 in overlay/tun.go

View workflow job for this annotation

GitHub Actions / Build all and test on ubuntu-linux

not enough arguments in call to newTun

Check failure on line 23 in overlay/tun.go

View workflow job for this annotation

GitHub Actions / Build and test on macos-11

not enough arguments in call to newTun

Check failure on line 23 in overlay/tun.go

View workflow job for this annotation

GitHub Actions / Build and test on linux with boringcrypto

not enough arguments in call to newTun
}
}

func NewFdDeviceFromConfig(fd *int) DeviceFactory {
return func(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error) {
routes, err := parseRoutes(c, tunCidr)
if err != nil {
return nil, util.NewContextualError("Could not parse tun.routes", nil, err)
}
return newTunFromFd(c, l, *fd, tunCidr)

Check failure on line 29 in overlay/tun.go

View workflow job for this annotation

GitHub Actions / Build all and test on ubuntu-linux

not enough arguments in call to newTunFromFd

Check failure on line 29 in overlay/tun.go

View workflow job for this annotation

GitHub Actions / Build and test on macos-11

not enough arguments in call to newTunFromFd

Check failure on line 29 in overlay/tun.go

View workflow job for this annotation

GitHub Actions / Build and test on linux with boringcrypto

not enough arguments in call to newTunFromFd
}
}

func getAllRoutesFromConfig(c *config.C, cidr *net.IPNet, initial bool) (bool, []Route, error) {
if !initial && !c.HasChanged("tun.routes") && !c.HasChanged("tun.unsafe_routes") {
return false, nil, nil
}

unsafeRoutes, err := parseUnsafeRoutes(c, tunCidr)
if err != nil {
return nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
routes, err := parseRoutes(c, cidr)
if err != nil {
return true, nil, util.NewContextualError("Could not parse tun.routes", nil, err)
}

unsafeRoutes, err := parseUnsafeRoutes(c, cidr)
if err != nil {
return true, nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
}

routes = append(routes, unsafeRoutes...)
return true, routes, nil
}

func findRemovedRoutes(newRoutes, oldRoutes []Route) []Route {
var removed []Route
has := func(entry Route) bool {
for _, check := range newRoutes {
if check.Equal(entry) {
return true
}
}
routes = append(routes, unsafeRoutes...)
return newTunFromFd(
l,
*fd,
tunCidr,
c.GetInt("tun.mtu", DefaultMTU),
routes,
c.GetInt("tun.tx_queue", 500),
c.GetBool("tun.use_system_route_table", false),
)
return false
}

for _, oldEntry := range oldRoutes {
if !has(oldEntry) {
removed = append(removed, oldEntry)
}
}

return removed
}
Loading

0 comments on commit f2a0f31

Please sign in to comment.