| 1 |
package overlay |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log/slog" |
| 6 |
"math" |
| 7 |
"net" |
| 8 |
"net/netip" |
| 9 |
"runtime" |
| 10 |
"strconv" |
| 11 |
|
| 12 |
"github.com/gaissmai/bart" |
| 13 |
"github.com/slackhq/nebula/config" |
| 14 |
"github.com/slackhq/nebula/routing" |
| 15 |
) |
| 16 |
|
| 17 |
type Route struct { |
| 18 |
MTU int |
| 19 |
Metric int |
| 20 |
Cidr netip.Prefix |
| 21 |
Via routing.Gateways |
| 22 |
Install bool |
| 23 |
} |
| 24 |
|
| 25 |
// Equal determines if a route that could be installed in the system route table is equal to another |
| 26 |
// Via is ignored since that is only consumed within nebula itself |
| 27 |
func (r Route) Equal(t Route) bool { |
| 28 |
if r.Cidr != t.Cidr { |
| 29 |
return false |
| 30 |
} |
| 31 |
if r.Metric != t.Metric { |
| 32 |
return false |
| 33 |
} |
| 34 |
if r.MTU != t.MTU { |
| 35 |
return false |
| 36 |
} |
| 37 |
if r.Install != t.Install { |
| 38 |
return false |
| 39 |
} |
| 40 |
return true |
| 41 |
} |
| 42 |
|
| 43 |
func (r Route) String() string { |
| 44 |
s := r.Cidr.String() |
| 45 |
if r.Metric != 0 { |
| 46 |
s += fmt.Sprintf(" metric: %v", r.Metric) |
| 47 |
} |
| 48 |
return s |
| 49 |
} |
| 50 |
|
| 51 |
func makeRouteTree(l *slog.Logger, routes []Route, allowMTU bool) (*bart.Table[routing.Gateways], error) { |
| 52 |
routeTree := new(bart.Table[routing.Gateways]) |
| 53 |
for _, r := range routes { |
| 54 |
if !allowMTU && r.MTU > 0 { |
| 55 |
l.Warn("route MTU is not supported on this platform", |
| 56 |
"goos", runtime.GOOS, |
| 57 |
"route", r, |
| 58 |
) |
| 59 |
} |
| 60 |
|
| 61 |
gateways := r.Via |
| 62 |
if len(gateways) > 0 { |
| 63 |
routing.CalculateBucketsForGateways(gateways) |
| 64 |
routeTree.Insert(r.Cidr, gateways) |
| 65 |
} |
| 66 |
} |
| 67 |
return routeTree, nil |
| 68 |
} |
| 69 |
|
| 70 |
func parseRoutes(c *config.C, networks []netip.Prefix) ([]Route, error) { |
| 71 |
var err error |
| 72 |
|
| 73 |
r := c.Get("tun.routes") |
| 74 |
if r == nil { |
| 75 |
return []Route{}, nil |
| 76 |
} |
| 77 |
|
| 78 |
rawRoutes, ok := r.([]any) |
| 79 |
if !ok { |
| 80 |
return nil, fmt.Errorf("tun.routes is not an array") |
| 81 |
} |
| 82 |
|
| 83 |
if len(rawRoutes) < 1 { |
| 84 |
return []Route{}, nil |
| 85 |
} |
| 86 |
|
| 87 |
routes := make([]Route, len(rawRoutes)) |
| 88 |
for i, r := range rawRoutes { |
| 89 |
m, ok := r.(map[string]any) |
| 90 |
if !ok { |
| 91 |
return nil, fmt.Errorf("entry %v in tun.routes is invalid", i+1) |
| 92 |
} |
| 93 |
|
| 94 |
rMtu, ok := m["mtu"] |
| 95 |
if !ok { |
| 96 |
return nil, fmt.Errorf("entry %v.mtu in tun.routes is not present", i+1) |
| 97 |
} |
| 98 |
|
| 99 |
mtu, ok := rMtu.(int) |
| 100 |
if !ok { |
| 101 |
mtu, err = strconv.Atoi(rMtu.(string)) |
| 102 |
if err != nil { |
| 103 |
return nil, fmt.Errorf("entry %v.mtu in tun.routes is not an integer: %v", i+1, err) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if mtu < 500 { |
| 108 |
return nil, fmt.Errorf("entry %v.mtu in tun.routes is below 500: %v", i+1, mtu) |
| 109 |
} |
| 110 |
|
| 111 |
rRoute, ok := m["route"] |
| 112 |
if !ok { |
| 113 |
return nil, fmt.Errorf("entry %v.route in tun.routes is not present", i+1) |
| 114 |
} |
| 115 |
|
| 116 |
r := Route{ |
| 117 |
Install: true, |
| 118 |
MTU: mtu, |
| 119 |
} |
| 120 |
|
| 121 |
r.Cidr, err = netip.ParsePrefix(fmt.Sprintf("%v", rRoute)) |
| 122 |
if err != nil { |
| 123 |
return nil, fmt.Errorf("entry %v.route in tun.routes failed to parse: %v", i+1, err) |
| 124 |
} |
| 125 |
|
| 126 |
found := false |
| 127 |
for _, network := range networks { |
| 128 |
if network.Contains(r.Cidr.Addr()) && r.Cidr.Bits() >= network.Bits() { |
| 129 |
found = true |
| 130 |
break |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if !found { |
| 135 |
return nil, fmt.Errorf( |
| 136 |
"entry %v.route in tun.routes is not contained within the configured vpn networks; route: %v, networks: %v", |
| 137 |
i+1, |
| 138 |
r.Cidr.String(), |
| 139 |
networks, |
| 140 |
) |
| 141 |
} |
| 142 |
|
| 143 |
routes[i] = r |
| 144 |
} |
| 145 |
|
| 146 |
return routes, nil |
| 147 |
} |
| 148 |
|
| 149 |
func parseUnsafeRoutes(c *config.C, networks []netip.Prefix) ([]Route, error) { |
| 150 |
var err error |
| 151 |
|
| 152 |
r := c.Get("tun.unsafe_routes") |
| 153 |
if r == nil { |
| 154 |
return []Route{}, nil |
| 155 |
} |
| 156 |
|
| 157 |
rawRoutes, ok := r.([]any) |
| 158 |
if !ok { |
| 159 |
return nil, fmt.Errorf("tun.unsafe_routes is not an array") |
| 160 |
} |
| 161 |
|
| 162 |
if len(rawRoutes) < 1 { |
| 163 |
return []Route{}, nil |
| 164 |
} |
| 165 |
|
| 166 |
routes := make([]Route, len(rawRoutes)) |
| 167 |
for i, r := range rawRoutes { |
| 168 |
m, ok := r.(map[string]any) |
| 169 |
if !ok { |
| 170 |
return nil, fmt.Errorf("entry %v in tun.unsafe_routes is invalid", i+1) |
| 171 |
} |
| 172 |
|
| 173 |
var mtu int |
| 174 |
if rMtu, ok := m["mtu"]; ok { |
| 175 |
mtu, ok = rMtu.(int) |
| 176 |
if !ok { |
| 177 |
mtu, err = strconv.Atoi(rMtu.(string)) |
| 178 |
if err != nil { |
| 179 |
return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is not an integer: %v", i+1, err) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if mtu != 0 && mtu < 500 { |
| 184 |
return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is below 500: %v", i+1, mtu) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
rMetric, ok := m["metric"] |
| 189 |
if !ok { |
| 190 |
rMetric = 0 |
| 191 |
} |
| 192 |
|
| 193 |
metric, ok := rMetric.(int) |
| 194 |
if !ok { |
| 195 |
_, err = strconv.ParseInt(rMetric.(string), 10, 32) |
| 196 |
if err != nil { |
| 197 |
return nil, fmt.Errorf("entry %v.metric in tun.unsafe_routes is not an integer: %v", i+1, err) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if metric < 0 || metric > math.MaxInt32 { |
| 202 |
return nil, fmt.Errorf("entry %v.metric in tun.unsafe_routes is not in range (0-%d) : %v", i+1, math.MaxInt32, metric) |
| 203 |
} |
| 204 |
|
| 205 |
rVia, ok := m["via"] |
| 206 |
if !ok { |
| 207 |
return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes is not present", i+1) |
| 208 |
} |
| 209 |
|
| 210 |
var gateways routing.Gateways |
| 211 |
|
| 212 |
switch via := rVia.(type) { |
| 213 |
case string: |
| 214 |
viaIp, err := netip.ParseAddr(via) |
| 215 |
if err != nil { |
| 216 |
return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes failed to parse address: %v", i+1, err) |
| 217 |
} |
| 218 |
|
| 219 |
gateways = routing.Gateways{routing.NewGateway(viaIp, 1)} |
| 220 |
|
| 221 |
case []any: |
| 222 |
gateways = make(routing.Gateways, len(via)) |
| 223 |
for ig, v := range via { |
| 224 |
gatewayMap, ok := v.(map[string]any) |
| 225 |
if !ok { |
| 226 |
return nil, fmt.Errorf("entry %v in tun.unsafe_routes[%v].via is invalid", i+1, ig+1) |
| 227 |
} |
| 228 |
|
| 229 |
rGateway, ok := gatewayMap["gateway"] |
| 230 |
if !ok { |
| 231 |
return nil, fmt.Errorf("entry .gateway in tun.unsafe_routes[%v].via[%v] is not present", i+1, ig+1) |
| 232 |
} |
| 233 |
|
| 234 |
parsedGateway, ok := rGateway.(string) |
| 235 |
if !ok { |
| 236 |
return nil, fmt.Errorf("entry .gateway in tun.unsafe_routes[%v].via[%v] is not a string", i+1, ig+1) |
| 237 |
} |
| 238 |
|
| 239 |
gatewayIp, err := netip.ParseAddr(parsedGateway) |
| 240 |
if err != nil { |
| 241 |
return nil, fmt.Errorf("entry .gateway in tun.unsafe_routes[%v].via[%v] failed to parse address: %v", i+1, ig+1, err) |
| 242 |
} |
| 243 |
|
| 244 |
rGatewayWeight, ok := gatewayMap["weight"] |
| 245 |
if !ok { |
| 246 |
rGatewayWeight = 1 |
| 247 |
} |
| 248 |
|
| 249 |
gatewayWeight, ok := rGatewayWeight.(int) |
| 250 |
if !ok { |
| 251 |
_, err = strconv.ParseInt(rGatewayWeight.(string), 10, 32) |
| 252 |
if err != nil { |
| 253 |
return nil, fmt.Errorf("entry .weight in tun.unsafe_routes[%v].via[%v] is not an integer", i+1, ig+1) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if gatewayWeight < 1 || gatewayWeight > math.MaxInt32 { |
| 258 |
return nil, fmt.Errorf("entry .weight in tun.unsafe_routes[%v].via[%v] is not in range (1-%d) : %v", i+1, ig+1, math.MaxInt32, gatewayWeight) |
| 259 |
} |
| 260 |
|
| 261 |
gateways[ig] = routing.NewGateway(gatewayIp, gatewayWeight) |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
default: |
| 266 |
return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes is not a string or list of gateways: found %T", i+1, rVia) |
| 267 |
} |
| 268 |
|
| 269 |
rRoute, ok := m["route"] |
| 270 |
if !ok { |
| 271 |
return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes is not present", i+1) |
| 272 |
} |
| 273 |
|
| 274 |
install := true |
| 275 |
rInstall, ok := m["install"] |
| 276 |
if ok { |
| 277 |
install, err = strconv.ParseBool(fmt.Sprintf("%v", rInstall)) |
| 278 |
if err != nil { |
| 279 |
return nil, fmt.Errorf("entry %v.install in tun.unsafe_routes is not a boolean: %v", i+1, err) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
r := Route{ |
| 284 |
Via: gateways, |
| 285 |
MTU: mtu, |
| 286 |
Metric: metric, |
| 287 |
Install: install, |
| 288 |
} |
| 289 |
|
| 290 |
r.Cidr, err = netip.ParsePrefix(fmt.Sprintf("%v", rRoute)) |
| 291 |
if err != nil { |
| 292 |
return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes failed to parse: %v", i+1, err) |
| 293 |
} |
| 294 |
|
| 295 |
for _, network := range networks { |
| 296 |
if network.Contains(r.Cidr.Addr()) { |
| 297 |
return nil, fmt.Errorf( |
| 298 |
"entry %v.route in tun.unsafe_routes is contained within the configured vpn networks; route: %v, network: %v", |
| 299 |
i+1, |
| 300 |
r.Cidr.String(), |
| 301 |
network.String(), |
| 302 |
) |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
routes[i] = r |
| 307 |
} |
| 308 |
|
| 309 |
return routes, nil |
| 310 |
} |
| 311 |
|
| 312 |
func ipWithin(o *net.IPNet, i *net.IPNet) bool { |
| 313 |
// Make sure o contains the lowest form of i |
| 314 |
if !o.Contains(i.IP.Mask(i.Mask)) { |
| 315 |
return false |
| 316 |
} |
| 317 |
|
| 318 |
// Find the max ip in i |
| 319 |
ip4 := i.IP.To4() |
| 320 |
if ip4 == nil { |
| 321 |
return false |
| 322 |
} |
| 323 |
|
| 324 |
last := make(net.IP, len(ip4)) |
| 325 |
copy(last, ip4) |
| 326 |
for x := range ip4 { |
| 327 |
last[x] |= ^i.Mask[x] |
| 328 |
} |
| 329 |
|
| 330 |
// Make sure o contains the max |
| 331 |
if !o.Contains(last) { |
| 332 |
return false |
| 333 |
} |
| 334 |
|
| 335 |
return true |
| 336 |
} |