| 1 |
package overlay |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log/slog" |
| 6 |
"net" |
| 7 |
"net/netip" |
| 8 |
|
| 9 |
"github.com/slackhq/nebula/config" |
| 10 |
"github.com/slackhq/nebula/util" |
| 11 |
) |
| 12 |
|
| 13 |
const DefaultMTU = 1300 |
| 14 |
|
| 15 |
type NameError struct { |
| 16 |
Name string |
| 17 |
Underlying error |
| 18 |
} |
| 19 |
|
| 20 |
func (e *NameError) Error() string { |
| 21 |
return fmt.Sprintf("could not set tun device name: %s because %s", e.Name, e.Underlying) |
| 22 |
} |
| 23 |
|
| 24 |
// TODO: We may be able to remove routines |
| 25 |
type DeviceFactory func(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error) |
| 26 |
|
| 27 |
func NewDeviceFromConfig(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error) { |
| 28 |
switch { |
| 29 |
case c.GetBool("tun.disabled", false): |
| 30 |
tun := newDisabledTun(vpnNetworks, c.GetInt("tun.tx_queue", 500), c.GetBool("stats.message_metrics", false), l) |
| 31 |
return tun, nil |
| 32 |
|
| 33 |
default: |
| 34 |
return newTun(c, l, vpnNetworks, routines > 1) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func NewFdDeviceFromConfig(fd *int) DeviceFactory { |
| 39 |
return func(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error) { |
| 40 |
return newTunFromFd(c, l, *fd, vpnNetworks) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func getAllRoutesFromConfig(c *config.C, vpnNetworks []netip.Prefix, initial bool) (bool, []Route, error) { |
| 45 |
if !initial && !c.HasChanged("tun.routes") && !c.HasChanged("tun.unsafe_routes") { |
| 46 |
return false, nil, nil |
| 47 |
} |
| 48 |
|
| 49 |
routes, err := parseRoutes(c, vpnNetworks) |
| 50 |
if err != nil { |
| 51 |
return true, nil, util.NewContextualError("Could not parse tun.routes", nil, err) |
| 52 |
} |
| 53 |
|
| 54 |
unsafeRoutes, err := parseUnsafeRoutes(c, vpnNetworks) |
| 55 |
if err != nil { |
| 56 |
return true, nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err) |
| 57 |
} |
| 58 |
|
| 59 |
routes = append(routes, unsafeRoutes...) |
| 60 |
return true, routes, nil |
| 61 |
} |
| 62 |
|
| 63 |
// findRemovedRoutes will return all routes that are not present in the newRoutes list and would affect the system route table. |
| 64 |
// Via is not used to evaluate since it does not affect the system route table. |
| 65 |
func findRemovedRoutes(newRoutes, oldRoutes []Route) []Route { |
| 66 |
var removed []Route |
| 67 |
has := func(entry Route) bool { |
| 68 |
for _, check := range newRoutes { |
| 69 |
if check.Equal(entry) { |
| 70 |
return true |
| 71 |
} |
| 72 |
} |
| 73 |
return false |
| 74 |
} |
| 75 |
|
| 76 |
for _, oldEntry := range oldRoutes { |
| 77 |
if !has(oldEntry) { |
| 78 |
removed = append(removed, oldEntry) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return removed |
| 83 |
} |
| 84 |
|
| 85 |
func prefixToMask(prefix netip.Prefix) netip.Addr { |
| 86 |
pLen := 128 |
| 87 |
if prefix.Addr().Is4() { |
| 88 |
pLen = 32 |
| 89 |
} |
| 90 |
|
| 91 |
addr, _ := netip.AddrFromSlice(net.CIDRMask(prefix.Bits(), pLen)) |
| 92 |
return addr |
| 93 |
} |
| 94 |
|
| 95 |
func flipBytes(b []byte) []byte { |
| 96 |
for i := 0; i < len(b); i++ { |
| 97 |
b[i] ^= 0xFF |
| 98 |
} |
| 99 |
return b |
| 100 |
} |
| 101 |
func orBytes(a []byte, b []byte) []byte { |
| 102 |
ret := make([]byte, len(a)) |
| 103 |
for i := 0; i < len(a); i++ { |
| 104 |
ret[i] = a[i] | b[i] |
| 105 |
} |
| 106 |
return ret |
| 107 |
} |
| 108 |
|
| 109 |
func getBroadcast(cidr netip.Prefix) netip.Addr { |
| 110 |
broadcast, _ := netip.AddrFromSlice( |
| 111 |
orBytes( |
| 112 |
cidr.Addr().AsSlice(), |
| 113 |
flipBytes(prefixToMask(cidr).AsSlice()), |
| 114 |
), |
| 115 |
) |
| 116 |
return broadcast |
| 117 |
} |
| 118 |
|
| 119 |
func selectGateway(dest netip.Prefix, gateways []netip.Prefix) (netip.Prefix, error) { |
| 120 |
for _, gateway := range gateways { |
| 121 |
if dest.Addr().Is4() && gateway.Addr().Is4() { |
| 122 |
return gateway, nil |
| 123 |
} |
| 124 |
|
| 125 |
if dest.Addr().Is6() && gateway.Addr().Is6() { |
| 126 |
return gateway, nil |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return netip.Prefix{}, fmt.Errorf("no gateway found for %v in the list of vpn networks", dest) |
| 131 |
} |