| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"log/slog" |
| 9 |
"net" |
| 10 |
"net/netip" |
| 11 |
"slices" |
| 12 |
"strconv" |
| 13 |
"sync" |
| 14 |
"sync/atomic" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/gaissmai/bart" |
| 18 |
"github.com/slackhq/nebula/cert" |
| 19 |
"github.com/slackhq/nebula/config" |
| 20 |
"github.com/slackhq/nebula/header" |
| 21 |
"github.com/slackhq/nebula/logging" |
| 22 |
"github.com/slackhq/nebula/udp" |
| 23 |
"github.com/slackhq/nebula/util" |
| 24 |
) |
| 25 |
|
| 26 |
var ErrHostNotKnown = errors.New("host not known") |
| 27 |
var ErrBadDetailsVpnAddr = errors.New("invalid packet, malformed detailsVpnAddr") |
| 28 |
|
| 29 |
type LightHouse struct { |
| 30 |
//TODO: We need a timer wheel to kick out vpnAddrs that haven't reported in a long time |
| 31 |
sync.RWMutex //Because we concurrently read and write to our maps |
| 32 |
ctx context.Context |
| 33 |
amLighthouse bool |
| 34 |
|
| 35 |
myVpnNetworks []netip.Prefix |
| 36 |
myVpnNetworksTable *bart.Lite |
| 37 |
punchy *Punchy |
| 38 |
|
| 39 |
// localAddrsFn enumerates the underlay addresses we advertise. It is a field so tests can supply simulated |
| 40 |
// addresses rather than whatever this machine's NICs happen to be. Set it before Start. |
| 41 |
localAddrsFn func(*LocalAllowList) []netip.Addr |
| 42 |
|
| 43 |
// Local cache of answers from light houses |
| 44 |
// map of vpn addr to answers |
| 45 |
addrMap map[netip.Addr]*RemoteList |
| 46 |
|
| 47 |
// filters remote addresses allowed for each host |
| 48 |
// - When we are a lighthouse, this filters what addresses we store and |
| 49 |
// respond with. |
| 50 |
// - When we are not a lighthouse, this filters which addresses we accept |
| 51 |
// from lighthouses. |
| 52 |
remoteAllowList atomic.Pointer[RemoteAllowList] |
| 53 |
|
| 54 |
// filters local addresses that we advertise to lighthouses |
| 55 |
localAllowList atomic.Pointer[LocalAllowList] |
| 56 |
|
| 57 |
// used to trigger the HandshakeManager when we receive HostQueryReply |
| 58 |
handshakeTrigger chan<- netip.Addr |
| 59 |
|
| 60 |
// staticList exists to avoid having a bool in each addrMap entry |
| 61 |
// since static should be rare |
| 62 |
staticList atomic.Pointer[map[netip.Addr]struct{}] |
| 63 |
lighthouses atomic.Pointer[[]netip.Addr] |
| 64 |
|
| 65 |
interval atomic.Int64 |
| 66 |
updateCancel context.CancelFunc |
| 67 |
ifce EncWriter |
| 68 |
nebulaPort uint32 // 32 bits because protobuf does not have a uint16 |
| 69 |
|
| 70 |
advertiseAddrs atomic.Pointer[[]netip.AddrPort] |
| 71 |
|
| 72 |
// Addr's of relays that can be used by peers to access me |
| 73 |
relaysForMe atomic.Pointer[[]netip.Addr] |
| 74 |
|
| 75 |
updateTrigger chan struct{} |
| 76 |
queryChan chan netip.Addr |
| 77 |
|
| 78 |
calculatedRemotes atomic.Pointer[bart.Table[[]*calculatedRemote]] // Maps VpnAddr to []*calculatedRemote |
| 79 |
|
| 80 |
metrics *MessageMetrics |
| 81 |
l *slog.Logger |
| 82 |
} |
| 83 |
|
| 84 |
// NewLightHouseFromConfig will build a Lighthouse struct from the values provided in the config object |
| 85 |
// addrMap should be nil unless this is during a config reload |
| 86 |
func NewLightHouseFromConfig(ctx context.Context, l *slog.Logger, c *config.C, cs *CertState, pc udp.Conn, p *Punchy) (*LightHouse, error) { |
| 87 |
amLighthouse := c.GetBool("lighthouse.am_lighthouse", false) |
| 88 |
nebulaPort := uint32(c.GetInt("listen.port", 0)) |
| 89 |
if amLighthouse && nebulaPort == 0 { |
| 90 |
return nil, util.NewContextualError("lighthouse.am_lighthouse enabled on node but no port number is set in config", nil, nil) |
| 91 |
} |
| 92 |
|
| 93 |
// If port is dynamic, discover it |
| 94 |
if nebulaPort == 0 && pc != nil { |
| 95 |
uPort, err := pc.LocalAddr() |
| 96 |
if err != nil { |
| 97 |
return nil, util.NewContextualError("Failed to get listening port", nil, err) |
| 98 |
} |
| 99 |
nebulaPort = uint32(uPort.Port()) |
| 100 |
} |
| 101 |
|
| 102 |
h := LightHouse{ |
| 103 |
ctx: ctx, |
| 104 |
amLighthouse: amLighthouse, |
| 105 |
myVpnNetworks: cs.myVpnNetworks, |
| 106 |
myVpnNetworksTable: cs.myVpnNetworksTable, |
| 107 |
addrMap: make(map[netip.Addr]*RemoteList), |
| 108 |
nebulaPort: nebulaPort, |
| 109 |
punchy: p, |
| 110 |
updateTrigger: make(chan struct{}, 1), |
| 111 |
queryChan: make(chan netip.Addr, c.GetUint32("handshakes.query_buffer", 64)), |
| 112 |
l: l, |
| 113 |
} |
| 114 |
h.localAddrsFn = func(al *LocalAllowList) []netip.Addr { |
| 115 |
return localAddrs(h.l, al) |
| 116 |
} |
| 117 |
|
| 118 |
lighthouses := make([]netip.Addr, 0) |
| 119 |
h.lighthouses.Store(&lighthouses) |
| 120 |
staticList := make(map[netip.Addr]struct{}) |
| 121 |
h.staticList.Store(&staticList) |
| 122 |
|
| 123 |
if c.GetBool("stats.lighthouse_metrics", false) { |
| 124 |
h.metrics = newLighthouseMetrics() |
| 125 |
} |
| 126 |
|
| 127 |
err := h.reload(c, true) |
| 128 |
if err != nil { |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
|
| 132 |
c.RegisterReloadCallback(func(c *config.C) { |
| 133 |
err := h.reload(c, false) |
| 134 |
switch v := err.(type) { |
| 135 |
case *util.ContextualError: |
| 136 |
v.Log(l) |
| 137 |
case error: |
| 138 |
l.Error("failed to reload lighthouse", "error", err) |
| 139 |
} |
| 140 |
}) |
| 141 |
|
| 142 |
h.startQueryWorker() |
| 143 |
|
| 144 |
return &h, nil |
| 145 |
} |
| 146 |
|
| 147 |
func (lh *LightHouse) GetStaticHostList() map[netip.Addr]struct{} { |
| 148 |
return *lh.staticList.Load() |
| 149 |
} |
| 150 |
|
| 151 |
func (lh *LightHouse) GetLighthouses() []netip.Addr { |
| 152 |
return *lh.lighthouses.Load() |
| 153 |
} |
| 154 |
|
| 155 |
func (lh *LightHouse) GetRemoteAllowList() *RemoteAllowList { |
| 156 |
return lh.remoteAllowList.Load() |
| 157 |
} |
| 158 |
|
| 159 |
func (lh *LightHouse) GetLocalAllowList() *LocalAllowList { |
| 160 |
return lh.localAllowList.Load() |
| 161 |
} |
| 162 |
|
| 163 |
func (lh *LightHouse) GetAdvertiseAddrs() []netip.AddrPort { |
| 164 |
return *lh.advertiseAddrs.Load() |
| 165 |
} |
| 166 |
|
| 167 |
func (lh *LightHouse) GetRelaysForMe() []netip.Addr { |
| 168 |
return *lh.relaysForMe.Load() |
| 169 |
} |
| 170 |
|
| 171 |
func (lh *LightHouse) getCalculatedRemotes() *bart.Table[[]*calculatedRemote] { |
| 172 |
return lh.calculatedRemotes.Load() |
| 173 |
} |
| 174 |
|
| 175 |
func (lh *LightHouse) GetUpdateInterval() int64 { |
| 176 |
return lh.interval.Load() |
| 177 |
} |
| 178 |
|
| 179 |
func (lh *LightHouse) reload(c *config.C, initial bool) error { |
| 180 |
if initial || c.HasChanged("lighthouse.advertise_addrs") { |
| 181 |
rawAdvAddrs := c.GetStringSlice("lighthouse.advertise_addrs", []string{}) |
| 182 |
advAddrs := make([]netip.AddrPort, 0) |
| 183 |
|
| 184 |
for i, rawAddr := range rawAdvAddrs { |
| 185 |
host, sport, err := net.SplitHostPort(rawAddr) |
| 186 |
if err != nil { |
| 187 |
return util.NewContextualError("Unable to parse lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, err) |
| 188 |
} |
| 189 |
|
| 190 |
addrs, err := net.DefaultResolver.LookupNetIP(context.Background(), "ip", host) |
| 191 |
if err != nil { |
| 192 |
return util.NewContextualError("Unable to lookup lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, err) |
| 193 |
} |
| 194 |
if len(addrs) == 0 { |
| 195 |
return util.NewContextualError("Unable to lookup lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, nil) |
| 196 |
} |
| 197 |
|
| 198 |
port, err := strconv.Atoi(sport) |
| 199 |
if err != nil { |
| 200 |
return util.NewContextualError("Unable to parse port in lighthouse.advertise_addrs entry", m{"addr": rawAddr, "entry": i + 1}, err) |
| 201 |
} |
| 202 |
|
| 203 |
if port == 0 { |
| 204 |
port = int(lh.nebulaPort) |
| 205 |
} |
| 206 |
|
| 207 |
//TODO: we could technically insert all returned addrs instead of just the first one if a dns lookup was used |
| 208 |
addr := addrs[0].Unmap() |
| 209 |
if lh.myVpnNetworksTable.Contains(addr) { |
| 210 |
lh.l.Warn("Ignoring lighthouse.advertise_addrs report because it is within the nebula network range", |
| 211 |
"addr", rawAddr, |
| 212 |
"entry", i+1, |
| 213 |
) |
| 214 |
continue |
| 215 |
} |
| 216 |
|
| 217 |
advAddrs = append(advAddrs, netip.AddrPortFrom(addr, uint16(port))) |
| 218 |
} |
| 219 |
|
| 220 |
lh.advertiseAddrs.Store(&advAddrs) |
| 221 |
|
| 222 |
if !initial { |
| 223 |
lh.l.Info("lighthouse.advertise_addrs has changed") |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if initial || c.HasChanged("lighthouse.interval") { |
| 228 |
lh.interval.Store(int64(c.GetInt("lighthouse.interval", 10))) |
| 229 |
|
| 230 |
if !initial { |
| 231 |
lh.l.Info("lighthouse.interval changed", |
| 232 |
"interval", lh.interval.Load(), |
| 233 |
) |
| 234 |
|
| 235 |
if lh.updateCancel != nil { |
| 236 |
// May not always have a running routine |
| 237 |
lh.updateCancel() |
| 238 |
} |
| 239 |
|
| 240 |
lh.StartUpdateWorker() |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if initial || c.HasChanged("lighthouse.remote_allow_list") || c.HasChanged("lighthouse.remote_allow_ranges") { |
| 245 |
ral, err := NewRemoteAllowListFromConfig(c, "lighthouse.remote_allow_list", "lighthouse.remote_allow_ranges") |
| 246 |
if err != nil { |
| 247 |
return util.NewContextualError("Invalid lighthouse.remote_allow_list", nil, err) |
| 248 |
} |
| 249 |
|
| 250 |
lh.remoteAllowList.Store(ral) |
| 251 |
if !initial { |
| 252 |
lh.l.Info("lighthouse.remote_allow_list and/or lighthouse.remote_allow_ranges has changed") |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if initial || c.HasChanged("lighthouse.local_allow_list") { |
| 257 |
lal, err := NewLocalAllowListFromConfig(c, "lighthouse.local_allow_list") |
| 258 |
if err != nil { |
| 259 |
return util.NewContextualError("Invalid lighthouse.local_allow_list", nil, err) |
| 260 |
} |
| 261 |
|
| 262 |
lh.localAllowList.Store(lal) |
| 263 |
if !initial { |
| 264 |
lh.l.Info("lighthouse.local_allow_list has changed") |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if initial || c.HasChanged("lighthouse.calculated_remotes") { |
| 269 |
cr, err := NewCalculatedRemotesFromConfig(c, "lighthouse.calculated_remotes") |
| 270 |
if err != nil { |
| 271 |
return util.NewContextualError("Invalid lighthouse.calculated_remotes", nil, err) |
| 272 |
} |
| 273 |
|
| 274 |
lh.calculatedRemotes.Store(cr) |
| 275 |
if !initial { |
| 276 |
lh.l.Info("lighthouse.calculated_remotes has changed") |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
//NOTE: many things will get much simpler when we combine static_host_map and lighthouse.hosts in config |
| 281 |
if initial || c.HasChanged("static_host_map") || c.HasChanged("static_map.cadence") || c.HasChanged("static_map.network") || c.HasChanged("static_map.lookup_timeout") { |
| 282 |
// Clean up. Entries still in the static_host_map will be re-built. |
| 283 |
ourselves := lh.myVpnNetworks[0].Addr() |
| 284 |
oldStaticList := lh.staticList.Load() |
| 285 |
if oldStaticList != nil { |
| 286 |
lh.RLock() |
| 287 |
for staticVpnAddr := range *oldStaticList { |
| 288 |
if am, ok := lh.addrMap[staticVpnAddr]; ok && am != nil { |
| 289 |
am.ResetForOwner(ourselves) |
| 290 |
} |
| 291 |
} |
| 292 |
lh.RUnlock() |
| 293 |
} |
| 294 |
|
| 295 |
// Build a new list based on current config. |
| 296 |
staticList := make(map[netip.Addr]struct{}) |
| 297 |
err := lh.loadStaticMap(c, staticList) |
| 298 |
if err != nil { |
| 299 |
return err |
| 300 |
} |
| 301 |
|
| 302 |
// For entries removed from static_host_map, stop the DNS goroutine and drop the cached addrs. |
| 303 |
// All addrs must come from the lighthouses now that it's no longer a static host. |
| 304 |
if oldStaticList != nil { |
| 305 |
lh.RLock() |
| 306 |
for staticVpnAddr := range *oldStaticList { |
| 307 |
if _, stillStatic := staticList[staticVpnAddr]; stillStatic { |
| 308 |
continue |
| 309 |
} |
| 310 |
if am, ok := lh.addrMap[staticVpnAddr]; ok && am != nil { |
| 311 |
am.ClearHostnameResults() |
| 312 |
} |
| 313 |
} |
| 314 |
lh.RUnlock() |
| 315 |
} |
| 316 |
|
| 317 |
lh.staticList.Store(&staticList) |
| 318 |
if !initial { |
| 319 |
if c.HasChanged("static_host_map") { |
| 320 |
lh.l.Info("static_host_map has changed") |
| 321 |
} |
| 322 |
if c.HasChanged("static_map.cadence") { |
| 323 |
lh.l.Info("static_map.cadence has changed") |
| 324 |
} |
| 325 |
if c.HasChanged("static_map.network") { |
| 326 |
lh.l.Info("static_map.network has changed") |
| 327 |
} |
| 328 |
if c.HasChanged("static_map.lookup_timeout") { |
| 329 |
lh.l.Info("static_map.lookup_timeout has changed") |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if initial || c.HasChanged("lighthouse.hosts") { |
| 335 |
lhList, err := lh.parseLighthouses(c) |
| 336 |
if err != nil { |
| 337 |
return err |
| 338 |
} |
| 339 |
|
| 340 |
lh.lighthouses.Store(&lhList) |
| 341 |
if !initial { |
| 342 |
//NOTE: we are not tearing down existing lighthouse connections because they might be used for non lighthouse traffic |
| 343 |
lh.l.Info("lighthouse.hosts has changed") |
| 344 |
lh.TriggerUpdate() |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
if initial || c.HasChanged("relay.relays") { |
| 349 |
switch c.GetBool("relay.am_relay", false) { |
| 350 |
case true: |
| 351 |
// Relays aren't allowed to specify other relays |
| 352 |
if len(c.GetStringSlice("relay.relays", nil)) > 0 { |
| 353 |
lh.l.Info("Ignoring relays from config because am_relay is true") |
| 354 |
} |
| 355 |
relaysForMe := []netip.Addr{} |
| 356 |
lh.relaysForMe.Store(&relaysForMe) |
| 357 |
case false: |
| 358 |
relaysForMe := []netip.Addr{} |
| 359 |
for _, v := range c.GetStringSlice("relay.relays", nil) { |
| 360 |
configRIP, err := netip.ParseAddr(v) |
| 361 |
if err != nil { |
| 362 |
lh.l.Warn("Parse relay from config failed", |
| 363 |
"relay", v, |
| 364 |
"error", err, |
| 365 |
) |
| 366 |
} else { |
| 367 |
lh.l.Info("Read relay from config", "relay", v) |
| 368 |
relaysForMe = append(relaysForMe, configRIP) |
| 369 |
} |
| 370 |
} |
| 371 |
lh.relaysForMe.Store(&relaysForMe) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
return nil |
| 376 |
} |
| 377 |
|
| 378 |
func (lh *LightHouse) parseLighthouses(c *config.C) ([]netip.Addr, error) { |
| 379 |
lhs := c.GetStringSlice("lighthouse.hosts", []string{}) |
| 380 |
if lh.amLighthouse && len(lhs) != 0 { |
| 381 |
lh.l.Warn("lighthouse.am_lighthouse enabled on node but upstream lighthouses exist in config") |
| 382 |
} |
| 383 |
out := make([]netip.Addr, len(lhs)) |
| 384 |
|
| 385 |
for i, host := range lhs { |
| 386 |
addr, err := netip.ParseAddr(host) |
| 387 |
if err != nil { |
| 388 |
return nil, util.NewContextualError("Unable to parse lighthouse host entry", m{"host": host, "entry": i + 1}, err) |
| 389 |
} |
| 390 |
|
| 391 |
if !lh.myVpnNetworksTable.Contains(addr) { |
| 392 |
lh.l.Warn("lighthouse host is not within our networks, lighthouse functionality will work but layer 3 network traffic to the lighthouse will not", |
| 393 |
"vpnAddr", addr, |
| 394 |
"networks", lh.myVpnNetworks, |
| 395 |
) |
| 396 |
} |
| 397 |
out[i] = addr |
| 398 |
} |
| 399 |
|
| 400 |
if !lh.amLighthouse && len(out) == 0 { |
| 401 |
lh.l.Warn("No lighthouse.hosts configured, this host will only be able to initiate tunnels with static_host_map entries") |
| 402 |
} |
| 403 |
|
| 404 |
staticList := lh.GetStaticHostList() |
| 405 |
for i := range out { |
| 406 |
if _, ok := staticList[out[i]]; !ok { |
| 407 |
return nil, fmt.Errorf("lighthouse %s does not have a static_host_map entry", out[i]) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
return out, nil |
| 412 |
} |
| 413 |
|
| 414 |
func getStaticMapCadence(c *config.C) (time.Duration, error) { |
| 415 |
cadence := c.GetString("static_map.cadence", "30s") |
| 416 |
d, err := time.ParseDuration(cadence) |
| 417 |
if err != nil { |
| 418 |
return 0, err |
| 419 |
} |
| 420 |
return d, nil |
| 421 |
} |
| 422 |
|
| 423 |
func getStaticMapLookupTimeout(c *config.C) (time.Duration, error) { |
| 424 |
lookupTimeout := c.GetString("static_map.lookup_timeout", "250ms") |
| 425 |
d, err := time.ParseDuration(lookupTimeout) |
| 426 |
if err != nil { |
| 427 |
return 0, err |
| 428 |
} |
| 429 |
return d, nil |
| 430 |
} |
| 431 |
|
| 432 |
func getStaticMapNetwork(c *config.C) (string, error) { |
| 433 |
network := c.GetString("static_map.network", "ip4") |
| 434 |
if network != "ip" && network != "ip4" && network != "ip6" { |
| 435 |
return "", fmt.Errorf("static_map.network must be one of ip, ip4, or ip6") |
| 436 |
} |
| 437 |
return network, nil |
| 438 |
} |
| 439 |
|
| 440 |
func (lh *LightHouse) loadStaticMap(c *config.C, staticList map[netip.Addr]struct{}) error { |
| 441 |
d, err := getStaticMapCadence(c) |
| 442 |
if err != nil { |
| 443 |
return err |
| 444 |
} |
| 445 |
|
| 446 |
network, err := getStaticMapNetwork(c) |
| 447 |
if err != nil { |
| 448 |
return err |
| 449 |
} |
| 450 |
|
| 451 |
lookupTimeout, err := getStaticMapLookupTimeout(c) |
| 452 |
if err != nil { |
| 453 |
return err |
| 454 |
} |
| 455 |
|
| 456 |
shm := c.GetMap("static_host_map", map[string]any{}) |
| 457 |
i := 0 |
| 458 |
|
| 459 |
for k, v := range shm { |
| 460 |
vpnAddr, err := netip.ParseAddr(fmt.Sprintf("%v", k)) |
| 461 |
if err != nil { |
| 462 |
return util.NewContextualError("Unable to parse static_host_map entry", m{"host": k, "entry": i + 1}, err) |
| 463 |
} |
| 464 |
|
| 465 |
if !lh.myVpnNetworksTable.Contains(vpnAddr) { |
| 466 |
lh.l.Warn("static_host_map key is not within our networks, layer 3 network traffic to this host will not work", |
| 467 |
"vpnAddr", vpnAddr, |
| 468 |
"networks", lh.myVpnNetworks, |
| 469 |
"entry", i+1, |
| 470 |
) |
| 471 |
} |
| 472 |
|
| 473 |
vals, ok := v.([]any) |
| 474 |
if !ok { |
| 475 |
vals = []any{v} |
| 476 |
} |
| 477 |
remoteAddrs := []string{} |
| 478 |
for _, v := range vals { |
| 479 |
remoteAddrs = append(remoteAddrs, fmt.Sprintf("%v", v)) |
| 480 |
} |
| 481 |
|
| 482 |
err = lh.addStaticRemotes(i, d, network, lookupTimeout, vpnAddr, remoteAddrs, staticList) |
| 483 |
if err != nil { |
| 484 |
return err |
| 485 |
} |
| 486 |
i++ |
| 487 |
} |
| 488 |
|
| 489 |
return nil |
| 490 |
} |
| 491 |
|
| 492 |
func (lh *LightHouse) Query(vpnAddr netip.Addr) *RemoteList { |
| 493 |
if !lh.IsLighthouseAddr(vpnAddr) { |
| 494 |
lh.QueryServer(vpnAddr) |
| 495 |
} |
| 496 |
lh.RLock() |
| 497 |
if v, ok := lh.addrMap[vpnAddr]; ok { |
| 498 |
lh.RUnlock() |
| 499 |
return v |
| 500 |
} |
| 501 |
lh.RUnlock() |
| 502 |
return nil |
| 503 |
} |
| 504 |
|
| 505 |
// QueryServer is asynchronous so no reply should be expected |
| 506 |
func (lh *LightHouse) QueryServer(vpnAddr netip.Addr) { |
| 507 |
// Don't put lighthouse addrs in the query channel because we can't query lighthouses about lighthouses |
| 508 |
if lh.amLighthouse || lh.IsLighthouseAddr(vpnAddr) { |
| 509 |
return |
| 510 |
} |
| 511 |
|
| 512 |
lh.queryChan <- vpnAddr |
| 513 |
} |
| 514 |
|
| 515 |
func (lh *LightHouse) QueryCache(vpnAddrs []netip.Addr) *RemoteList { |
| 516 |
lh.RLock() |
| 517 |
if v, ok := lh.addrMap[vpnAddrs[0]]; ok { |
| 518 |
lh.RUnlock() |
| 519 |
return v |
| 520 |
} |
| 521 |
lh.RUnlock() |
| 522 |
|
| 523 |
lh.Lock() |
| 524 |
defer lh.Unlock() |
| 525 |
// Add an entry if we don't already have one |
| 526 |
return lh.unlockedGetRemoteList(vpnAddrs) //todo CERT-V2 this contains addrmap lookups we could potentially skip |
| 527 |
} |
| 528 |
|
| 529 |
// queryAndPrepMessage is a lock helper on RemoteList, assisting the caller to build a lighthouse message containing |
| 530 |
// details from the remote list. It looks for a hit in the addrMap and a hit in the RemoteList under the owner vpnAddr |
| 531 |
// If one is found then f() is called with proper locking, f() must return result of n.MarshalTo() |
| 532 |
func (lh *LightHouse) queryAndPrepMessage(vpnAddr netip.Addr, f func(*cache) (int, error)) (bool, int, error) { |
| 533 |
lh.RLock() |
| 534 |
// Do we have an entry in the main cache? |
| 535 |
if v, ok := lh.addrMap[vpnAddr]; ok { |
| 536 |
// Swap lh lock for remote list lock |
| 537 |
v.RLock() |
| 538 |
defer v.RUnlock() |
| 539 |
|
| 540 |
lh.RUnlock() |
| 541 |
|
| 542 |
// We may be asking about a non primary address so lets get the primary address |
| 543 |
if slices.Contains(v.vpnAddrs, vpnAddr) { |
| 544 |
vpnAddr = v.vpnAddrs[0] |
| 545 |
} |
| 546 |
c := v.cache[vpnAddr] |
| 547 |
// Make sure we have |
| 548 |
if c != nil { |
| 549 |
n, err := f(c) |
| 550 |
return true, n, err |
| 551 |
} |
| 552 |
return false, 0, nil |
| 553 |
} |
| 554 |
lh.RUnlock() |
| 555 |
return false, 0, nil |
| 556 |
} |
| 557 |
|
| 558 |
func (lh *LightHouse) DeleteVpnAddrs(allVpnAddrs []netip.Addr) { |
| 559 |
// First we check the static host map. If any of the VpnAddrs to be deleted are present, do nothing. |
| 560 |
staticList := lh.GetStaticHostList() |
| 561 |
for _, addr := range allVpnAddrs { |
| 562 |
if _, ok := staticList[addr]; ok { |
| 563 |
return |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
// None of the VpnAddrs were present. Now we can do the deletes. |
| 568 |
lh.Lock() |
| 569 |
rm, ok := lh.addrMap[allVpnAddrs[0]] |
| 570 |
if ok { |
| 571 |
debugEnabled := lh.l.Enabled(context.Background(), slog.LevelDebug) |
| 572 |
for _, addr := range allVpnAddrs { |
| 573 |
srm := lh.addrMap[addr] |
| 574 |
if srm == rm { |
| 575 |
delete(lh.addrMap, addr) |
| 576 |
if debugEnabled { |
| 577 |
lh.l.Debug("deleting from lighthouse", "vpnAddr", addr) |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
lh.Unlock() |
| 583 |
} |
| 584 |
|
| 585 |
// AddStaticRemote adds a static host entry for vpnAddr as ourselves as the owner |
| 586 |
// We are the owner because we don't want a lighthouse server to advertise for static hosts it was configured with |
| 587 |
// And we don't want a lighthouse query reply to interfere with our learned cache if we are a client |
| 588 |
// NOTE: this function should not interact with any hot path objects, like lh.staticList, the caller should handle it |
| 589 |
func (lh *LightHouse) addStaticRemotes(i int, d time.Duration, network string, timeout time.Duration, vpnAddr netip.Addr, toAddrs []string, staticList map[netip.Addr]struct{}) error { |
| 590 |
lh.Lock() |
| 591 |
am := lh.unlockedGetRemoteList([]netip.Addr{vpnAddr}) |
| 592 |
am.Lock() |
| 593 |
defer am.Unlock() |
| 594 |
ctx := lh.ctx |
| 595 |
lh.Unlock() |
| 596 |
|
| 597 |
hr, err := NewHostnameResults(ctx, lh.l, d, network, timeout, toAddrs, func() { |
| 598 |
// This callback runs whenever the DNS hostname resolver finds a different set of addr's |
| 599 |
// in its resolution for hostnames. |
| 600 |
am.Lock() |
| 601 |
defer am.Unlock() |
| 602 |
am.shouldRebuild = true |
| 603 |
}) |
| 604 |
if err != nil { |
| 605 |
return util.NewContextualError("Static host address could not be parsed", m{"vpnAddr": vpnAddr, "entry": i + 1}, err) |
| 606 |
} |
| 607 |
am.unlockedSetHostnamesResults(hr) |
| 608 |
|
| 609 |
for _, addrPort := range hr.GetAddrs() { |
| 610 |
if !lh.shouldAdd([]netip.Addr{vpnAddr}, addrPort.Addr()) { |
| 611 |
continue |
| 612 |
} |
| 613 |
switch { |
| 614 |
case addrPort.Addr().Is4(): |
| 615 |
am.unlockedPrependV4(lh.myVpnNetworks[0].Addr(), netAddrToProtoV4AddrPort(addrPort.Addr(), addrPort.Port())) |
| 616 |
case addrPort.Addr().Is6(): |
| 617 |
am.unlockedPrependV6(lh.myVpnNetworks[0].Addr(), netAddrToProtoV6AddrPort(addrPort.Addr(), addrPort.Port())) |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
// Mark it as static in the caller provided map |
| 622 |
staticList[vpnAddr] = struct{}{} |
| 623 |
return nil |
| 624 |
} |
| 625 |
|
| 626 |
// addCalculatedRemotes adds any calculated remotes based on the |
| 627 |
// lighthouse.calculated_remotes configuration. It returns true if any |
| 628 |
// calculated remotes were added |
| 629 |
func (lh *LightHouse) addCalculatedRemotes(vpnAddr netip.Addr) bool { |
| 630 |
tree := lh.getCalculatedRemotes() |
| 631 |
if tree == nil { |
| 632 |
return false |
| 633 |
} |
| 634 |
calculatedRemotes, ok := tree.Lookup(vpnAddr) |
| 635 |
if !ok { |
| 636 |
return false |
| 637 |
} |
| 638 |
|
| 639 |
var calculatedV4 []*V4AddrPort |
| 640 |
var calculatedV6 []*V6AddrPort |
| 641 |
for _, cr := range calculatedRemotes { |
| 642 |
if vpnAddr.Is4() { |
| 643 |
c := cr.ApplyV4(vpnAddr) |
| 644 |
if c != nil { |
| 645 |
calculatedV4 = append(calculatedV4, c) |
| 646 |
} |
| 647 |
} else if vpnAddr.Is6() { |
| 648 |
c := cr.ApplyV6(vpnAddr) |
| 649 |
if c != nil { |
| 650 |
calculatedV6 = append(calculatedV6, c) |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
lh.Lock() |
| 656 |
am := lh.unlockedGetRemoteList([]netip.Addr{vpnAddr}) |
| 657 |
am.Lock() |
| 658 |
defer am.Unlock() |
| 659 |
lh.Unlock() |
| 660 |
|
| 661 |
if len(calculatedV4) > 0 { |
| 662 |
am.unlockedSetV4(lh.myVpnNetworks[0].Addr(), vpnAddr, calculatedV4, lh.unlockedShouldAddV4) |
| 663 |
} |
| 664 |
|
| 665 |
if len(calculatedV6) > 0 { |
| 666 |
am.unlockedSetV6(lh.myVpnNetworks[0].Addr(), vpnAddr, calculatedV6, lh.unlockedShouldAddV6) |
| 667 |
} |
| 668 |
|
| 669 |
return len(calculatedV4) > 0 || len(calculatedV6) > 0 |
| 670 |
} |
| 671 |
|
| 672 |
// unlockedGetRemoteList assumes you have the lh lock |
| 673 |
func (lh *LightHouse) unlockedGetRemoteList(allAddrs []netip.Addr) *RemoteList { |
| 674 |
// before we go and make a new remotelist, we need to make sure we don't have one for any of this set of vpnaddrs yet |
| 675 |
for i, addr := range allAddrs { |
| 676 |
am, ok := lh.addrMap[addr] |
| 677 |
if ok { |
| 678 |
if i != 0 { |
| 679 |
lh.addrMap[allAddrs[0]] = am |
| 680 |
} |
| 681 |
return am |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
am := NewRemoteList(allAddrs, lh.shouldAdd) |
| 686 |
for _, addr := range allAddrs { |
| 687 |
lh.addrMap[addr] = am |
| 688 |
} |
| 689 |
return am |
| 690 |
} |
| 691 |
|
| 692 |
func (lh *LightHouse) shouldAdd(vpnAddrs []netip.Addr, to netip.Addr) bool { |
| 693 |
allow := lh.GetRemoteAllowList().AllowAll(vpnAddrs, to) |
| 694 |
if lh.l.Enabled(context.Background(), logging.LevelTrace) { |
| 695 |
lh.l.Log(context.Background(), logging.LevelTrace, "remoteAllowList.Allow", |
| 696 |
"vpnAddrs", vpnAddrs, |
| 697 |
"udpAddr", to, |
| 698 |
"allow", allow, |
| 699 |
) |
| 700 |
} |
| 701 |
if !allow { |
| 702 |
return false |
| 703 |
} |
| 704 |
|
| 705 |
if lh.myVpnNetworksTable.Contains(to) { |
| 706 |
return false |
| 707 |
} |
| 708 |
|
| 709 |
return true |
| 710 |
} |
| 711 |
|
| 712 |
// unlockedShouldAddV4 checks if to is allowed by our allow list |
| 713 |
func (lh *LightHouse) unlockedShouldAddV4(vpnAddr netip.Addr, to *V4AddrPort) bool { |
| 714 |
udpAddr := protoV4AddrPortToNetAddrPort(to) |
| 715 |
allow := lh.GetRemoteAllowList().Allow(vpnAddr, udpAddr.Addr()) |
| 716 |
if lh.l.Enabled(context.Background(), logging.LevelTrace) { |
| 717 |
lh.l.Log(context.Background(), logging.LevelTrace, "remoteAllowList.Allow", |
| 718 |
"vpnAddr", vpnAddr, |
| 719 |
"udpAddr", udpAddr, |
| 720 |
"allow", allow, |
| 721 |
) |
| 722 |
} |
| 723 |
|
| 724 |
if !allow { |
| 725 |
return false |
| 726 |
} |
| 727 |
|
| 728 |
if lh.myVpnNetworksTable.Contains(udpAddr.Addr()) { |
| 729 |
return false |
| 730 |
} |
| 731 |
|
| 732 |
return true |
| 733 |
} |
| 734 |
|
| 735 |
// unlockedShouldAddV6 checks if to is allowed by our allow list |
| 736 |
func (lh *LightHouse) unlockedShouldAddV6(vpnAddr netip.Addr, to *V6AddrPort) bool { |
| 737 |
udpAddr := protoV6AddrPortToNetAddrPort(to) |
| 738 |
allow := lh.GetRemoteAllowList().Allow(vpnAddr, udpAddr.Addr()) |
| 739 |
if lh.l.Enabled(context.Background(), logging.LevelTrace) { |
| 740 |
lh.l.Log(context.Background(), logging.LevelTrace, "remoteAllowList.Allow", |
| 741 |
"vpnAddr", vpnAddr, |
| 742 |
"udpAddr", udpAddr, |
| 743 |
"allow", allow, |
| 744 |
) |
| 745 |
} |
| 746 |
|
| 747 |
if !allow { |
| 748 |
return false |
| 749 |
} |
| 750 |
|
| 751 |
if lh.myVpnNetworksTable.Contains(udpAddr.Addr()) { |
| 752 |
return false |
| 753 |
} |
| 754 |
|
| 755 |
return true |
| 756 |
} |
| 757 |
|
| 758 |
func (lh *LightHouse) IsLighthouseAddr(vpnAddr netip.Addr) bool { |
| 759 |
l := lh.GetLighthouses() |
| 760 |
return slices.Contains(l, vpnAddr) |
| 761 |
} |
| 762 |
|
| 763 |
func (lh *LightHouse) IsAnyLighthouseAddr(vpnAddrs []netip.Addr) bool { |
| 764 |
l := lh.GetLighthouses() |
| 765 |
for i := range vpnAddrs { |
| 766 |
if slices.Contains(l, vpnAddrs[i]) { |
| 767 |
return true |
| 768 |
} |
| 769 |
} |
| 770 |
return false |
| 771 |
} |
| 772 |
|
| 773 |
func (lh *LightHouse) startQueryWorker() { |
| 774 |
if lh.amLighthouse { |
| 775 |
return |
| 776 |
} |
| 777 |
|
| 778 |
go func() { |
| 779 |
nb := make([]byte, 12, 12) |
| 780 |
out := make([]byte, mtu) |
| 781 |
|
| 782 |
for { |
| 783 |
select { |
| 784 |
case <-lh.ctx.Done(): |
| 785 |
return |
| 786 |
case addr := <-lh.queryChan: |
| 787 |
lh.innerQueryServer(addr, nb, out) |
| 788 |
} |
| 789 |
} |
| 790 |
}() |
| 791 |
} |
| 792 |
|
| 793 |
func (lh *LightHouse) innerQueryServer(addr netip.Addr, nb, out []byte) { |
| 794 |
if lh.IsLighthouseAddr(addr) { |
| 795 |
return |
| 796 |
} |
| 797 |
|
| 798 |
msg := &NebulaMeta{ |
| 799 |
Type: NebulaMeta_HostQuery, |
| 800 |
Details: &NebulaMetaDetails{}, |
| 801 |
} |
| 802 |
|
| 803 |
var v1Query, v2Query []byte |
| 804 |
var err error |
| 805 |
var v cert.Version |
| 806 |
queried := 0 |
| 807 |
lighthouses := lh.GetLighthouses() |
| 808 |
|
| 809 |
for _, lhVpnAddr := range lighthouses { |
| 810 |
hi := lh.ifce.GetHostInfo(lhVpnAddr) |
| 811 |
if hi != nil { |
| 812 |
v = hi.ConnectionState.myCert.Version() |
| 813 |
} else { |
| 814 |
v = lh.ifce.GetCertState().initiatingVersion |
| 815 |
} |
| 816 |
|
| 817 |
if v == cert.Version1 { |
| 818 |
if !addr.Is4() { |
| 819 |
lh.l.Error("Can't query lighthouse for v6 address using a v1 protocol", |
| 820 |
"queryVpnAddr", addr, |
| 821 |
"lighthouseAddr", lhVpnAddr, |
| 822 |
) |
| 823 |
continue |
| 824 |
} |
| 825 |
|
| 826 |
if v1Query == nil { |
| 827 |
b := addr.As4() |
| 828 |
msg.Details.VpnAddr = nil |
| 829 |
msg.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:]) |
| 830 |
|
| 831 |
v1Query, err = msg.Marshal() |
| 832 |
if err != nil { |
| 833 |
lh.l.Error("Failed to marshal lighthouse v1 query payload", |
| 834 |
"error", err, |
| 835 |
"queryVpnAddr", addr, |
| 836 |
"lighthouseAddr", lhVpnAddr, |
| 837 |
) |
| 838 |
continue |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
lh.ifce.SendMessageToVpnAddr(header.LightHouse, 0, lhVpnAddr, v1Query, nb, out) |
| 843 |
queried++ |
| 844 |
|
| 845 |
} else if v == cert.Version2 { |
| 846 |
if v2Query == nil { |
| 847 |
msg.Details.OldVpnAddr = 0 |
| 848 |
msg.Details.VpnAddr = netAddrToProtoAddr(addr) |
| 849 |
|
| 850 |
v2Query, err = msg.Marshal() |
| 851 |
if err != nil { |
| 852 |
lh.l.Error("Failed to marshal lighthouse v2 query payload", |
| 853 |
"error", err, |
| 854 |
"queryVpnAddr", addr, |
| 855 |
"lighthouseAddr", lhVpnAddr, |
| 856 |
) |
| 857 |
continue |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
lh.ifce.SendMessageToVpnAddr(header.LightHouse, 0, lhVpnAddr, v2Query, nb, out) |
| 862 |
queried++ |
| 863 |
|
| 864 |
} else { |
| 865 |
lh.l.Debug("unsupported protocol version", |
| 866 |
"op", "query", |
| 867 |
"queryVpnAddr", addr, |
| 868 |
"version", v, |
| 869 |
) |
| 870 |
continue |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
lh.metricTx(NebulaMeta_HostQuery, int64(queried)) |
| 875 |
} |
| 876 |
|
| 877 |
func (lh *LightHouse) StartUpdateWorker() { |
| 878 |
interval := lh.GetUpdateInterval() |
| 879 |
if lh.amLighthouse || interval == 0 { |
| 880 |
return |
| 881 |
} |
| 882 |
|
| 883 |
clockSource := time.NewTicker(time.Second * time.Duration(interval)) |
| 884 |
updateCtx, cancel := context.WithCancel(lh.ctx) |
| 885 |
lh.updateCancel = cancel |
| 886 |
|
| 887 |
go func() { |
| 888 |
defer clockSource.Stop() |
| 889 |
|
| 890 |
for { |
| 891 |
lh.SendUpdate() |
| 892 |
|
| 893 |
select { |
| 894 |
case <-updateCtx.Done(): |
| 895 |
return |
| 896 |
case <-clockSource.C: |
| 897 |
continue |
| 898 |
case <-lh.updateTrigger: |
| 899 |
continue |
| 900 |
} |
| 901 |
} |
| 902 |
}() |
| 903 |
} |
| 904 |
|
| 905 |
// TriggerUpdate requests an immediate lighthouse update. This is a non-blocking |
| 906 |
// operation intended to be called after a handshake completes with a lighthouse, |
| 907 |
// so the lighthouse has our current addresses without waiting for the next |
| 908 |
// periodic update. |
| 909 |
func (lh *LightHouse) TriggerUpdate() { |
| 910 |
select { |
| 911 |
case lh.updateTrigger <- struct{}{}: |
| 912 |
default: |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
func (lh *LightHouse) SendUpdate() { |
| 917 |
var v4 []*V4AddrPort |
| 918 |
var v6 []*V6AddrPort |
| 919 |
|
| 920 |
for _, e := range lh.GetAdvertiseAddrs() { |
| 921 |
if e.Addr().Is4() { |
| 922 |
v4 = append(v4, netAddrToProtoV4AddrPort(e.Addr(), e.Port())) |
| 923 |
} else { |
| 924 |
v6 = append(v6, netAddrToProtoV6AddrPort(e.Addr(), e.Port())) |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
lal := lh.GetLocalAllowList() |
| 929 |
for _, e := range lh.localAddrsFn(lal) { |
| 930 |
if lh.myVpnNetworksTable.Contains(e) { |
| 931 |
continue |
| 932 |
} |
| 933 |
|
| 934 |
// Only add addrs that aren't my VPN/tun networks |
| 935 |
if e.Is4() { |
| 936 |
v4 = append(v4, netAddrToProtoV4AddrPort(e, uint16(lh.nebulaPort))) |
| 937 |
} else { |
| 938 |
v6 = append(v6, netAddrToProtoV6AddrPort(e, uint16(lh.nebulaPort))) |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
nb := make([]byte, 12, 12) |
| 943 |
out := make([]byte, mtu) |
| 944 |
|
| 945 |
var v1Update, v2Update []byte |
| 946 |
var err error |
| 947 |
updated := 0 |
| 948 |
lighthouses := lh.GetLighthouses() |
| 949 |
|
| 950 |
for _, lhVpnAddr := range lighthouses { |
| 951 |
var v cert.Version |
| 952 |
hi := lh.ifce.GetHostInfo(lhVpnAddr) |
| 953 |
if hi != nil { |
| 954 |
v = hi.ConnectionState.myCert.Version() |
| 955 |
} else { |
| 956 |
v = lh.ifce.GetCertState().initiatingVersion |
| 957 |
} |
| 958 |
if v == cert.Version1 { |
| 959 |
if v1Update == nil { |
| 960 |
if !lh.myVpnNetworks[0].Addr().Is4() { |
| 961 |
lh.l.Warn("cannot update lighthouse using v1 protocol without an IPv4 address", |
| 962 |
"lighthouseAddr", lhVpnAddr, |
| 963 |
) |
| 964 |
continue |
| 965 |
} |
| 966 |
var relays []uint32 |
| 967 |
for _, r := range lh.GetRelaysForMe() { |
| 968 |
if !r.Is4() { |
| 969 |
continue |
| 970 |
} |
| 971 |
b := r.As4() |
| 972 |
relays = append(relays, binary.BigEndian.Uint32(b[:])) |
| 973 |
} |
| 974 |
b := lh.myVpnNetworks[0].Addr().As4() |
| 975 |
msg := NebulaMeta{ |
| 976 |
Type: NebulaMeta_HostUpdateNotification, |
| 977 |
Details: &NebulaMetaDetails{ |
| 978 |
V4AddrPorts: v4, |
| 979 |
V6AddrPorts: v6, |
| 980 |
OldRelayVpnAddrs: relays, |
| 981 |
OldVpnAddr: binary.BigEndian.Uint32(b[:]), |
| 982 |
}, |
| 983 |
} |
| 984 |
|
| 985 |
v1Update, err = msg.Marshal() |
| 986 |
if err != nil { |
| 987 |
lh.l.Error("Error while marshaling for lighthouse v1 update", |
| 988 |
"error", err, |
| 989 |
"lighthouseAddr", lhVpnAddr, |
| 990 |
) |
| 991 |
continue |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
lh.ifce.SendMessageToVpnAddr(header.LightHouse, 0, lhVpnAddr, v1Update, nb, out) |
| 996 |
updated++ |
| 997 |
|
| 998 |
} else if v == cert.Version2 { |
| 999 |
if v2Update == nil { |
| 1000 |
var relays []*Addr |
| 1001 |
for _, r := range lh.GetRelaysForMe() { |
| 1002 |
relays = append(relays, netAddrToProtoAddr(r)) |
| 1003 |
} |
| 1004 |
|
| 1005 |
msg := NebulaMeta{ |
| 1006 |
Type: NebulaMeta_HostUpdateNotification, |
| 1007 |
Details: &NebulaMetaDetails{ |
| 1008 |
V4AddrPorts: v4, |
| 1009 |
V6AddrPorts: v6, |
| 1010 |
RelayVpnAddrs: relays, |
| 1011 |
}, |
| 1012 |
} |
| 1013 |
|
| 1014 |
v2Update, err = msg.Marshal() |
| 1015 |
if err != nil { |
| 1016 |
lh.l.Error("Error while marshaling for lighthouse v2 update", |
| 1017 |
"error", err, |
| 1018 |
"lighthouseAddr", lhVpnAddr, |
| 1019 |
) |
| 1020 |
continue |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
lh.ifce.SendMessageToVpnAddr(header.LightHouse, 0, lhVpnAddr, v2Update, nb, out) |
| 1025 |
updated++ |
| 1026 |
|
| 1027 |
} else { |
| 1028 |
lh.l.Debug("unsupported protocol version", |
| 1029 |
"op", "update", |
| 1030 |
"version", v, |
| 1031 |
) |
| 1032 |
continue |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
lh.metricTx(NebulaMeta_HostUpdateNotification, int64(updated)) |
| 1037 |
} |
| 1038 |
|
| 1039 |
type LightHouseHandler struct { |
| 1040 |
lh *LightHouse |
| 1041 |
nb []byte |
| 1042 |
out []byte |
| 1043 |
pb []byte |
| 1044 |
meta *NebulaMeta |
| 1045 |
l *slog.Logger |
| 1046 |
} |
| 1047 |
|
| 1048 |
func (lh *LightHouse) NewRequestHandler() *LightHouseHandler { |
| 1049 |
lhh := &LightHouseHandler{ |
| 1050 |
lh: lh, |
| 1051 |
nb: make([]byte, 12, 12), |
| 1052 |
out: make([]byte, mtu), |
| 1053 |
l: lh.l, |
| 1054 |
pb: make([]byte, mtu), |
| 1055 |
|
| 1056 |
meta: &NebulaMeta{ |
| 1057 |
Details: &NebulaMetaDetails{}, |
| 1058 |
}, |
| 1059 |
} |
| 1060 |
|
| 1061 |
return lhh |
| 1062 |
} |
| 1063 |
|
| 1064 |
func (lh *LightHouse) metricRx(t NebulaMeta_MessageType, i int64) { |
| 1065 |
lh.metrics.Rx(header.MessageType(t), 0, i) |
| 1066 |
} |
| 1067 |
|
| 1068 |
func (lh *LightHouse) metricTx(t NebulaMeta_MessageType, i int64) { |
| 1069 |
lh.metrics.Tx(header.MessageType(t), 0, i) |
| 1070 |
} |
| 1071 |
|
| 1072 |
// This method is similar to Reset(), but it re-uses the pointer structs |
| 1073 |
// so that we don't have to re-allocate them |
| 1074 |
func (lhh *LightHouseHandler) resetMeta() *NebulaMeta { |
| 1075 |
details := lhh.meta.Details |
| 1076 |
lhh.meta.Reset() |
| 1077 |
|
| 1078 |
// Keep the array memory around |
| 1079 |
details.V4AddrPorts = details.V4AddrPorts[:0] |
| 1080 |
details.V6AddrPorts = details.V6AddrPorts[:0] |
| 1081 |
details.RelayVpnAddrs = details.RelayVpnAddrs[:0] |
| 1082 |
details.OldRelayVpnAddrs = details.OldRelayVpnAddrs[:0] |
| 1083 |
details.OldVpnAddr = 0 |
| 1084 |
details.VpnAddr = nil |
| 1085 |
lhh.meta.Details = details |
| 1086 |
|
| 1087 |
return lhh.meta |
| 1088 |
} |
| 1089 |
|
| 1090 |
func (lhh *LightHouseHandler) HandleRequest(rAddr netip.AddrPort, fromVpnAddrs []netip.Addr, p []byte, w EncWriter) { |
| 1091 |
n := lhh.resetMeta() |
| 1092 |
err := n.Unmarshal(p) |
| 1093 |
if err != nil { |
| 1094 |
lhh.l.Error("Failed to unmarshal lighthouse packet", |
| 1095 |
"error", err, |
| 1096 |
"vpnAddrs", fromVpnAddrs, |
| 1097 |
"udpAddr", rAddr, |
| 1098 |
) |
| 1099 |
return |
| 1100 |
} |
| 1101 |
|
| 1102 |
if n.Details == nil { |
| 1103 |
lhh.l.Error("Invalid lighthouse update", |
| 1104 |
"vpnAddrs", fromVpnAddrs, |
| 1105 |
"udpAddr", rAddr, |
| 1106 |
) |
| 1107 |
return |
| 1108 |
} |
| 1109 |
|
| 1110 |
lhh.lh.metricRx(n.Type, 1) |
| 1111 |
|
| 1112 |
switch n.Type { |
| 1113 |
case NebulaMeta_HostQuery: |
| 1114 |
lhh.handleHostQuery(n, fromVpnAddrs, rAddr, w) |
| 1115 |
|
| 1116 |
case NebulaMeta_HostQueryReply: |
| 1117 |
lhh.handleHostQueryReply(n, fromVpnAddrs) |
| 1118 |
|
| 1119 |
case NebulaMeta_HostUpdateNotification: |
| 1120 |
lhh.handleHostUpdateNotification(n, fromVpnAddrs, w) |
| 1121 |
|
| 1122 |
case NebulaMeta_HostMovedNotification: |
| 1123 |
case NebulaMeta_HostPunchNotification: |
| 1124 |
lhh.handleHostPunchNotification(n, fromVpnAddrs, w) |
| 1125 |
|
| 1126 |
case NebulaMeta_HostUpdateNotificationAck: |
| 1127 |
// noop |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
func (lhh *LightHouseHandler) handleHostQuery(n *NebulaMeta, fromVpnAddrs []netip.Addr, addr netip.AddrPort, w EncWriter) { |
| 1132 |
// Exit if we don't answer queries |
| 1133 |
if !lhh.lh.amLighthouse { |
| 1134 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1135 |
lhh.l.Debug("I don't answer queries, but received one", "from", addr) |
| 1136 |
} |
| 1137 |
return |
| 1138 |
} |
| 1139 |
|
| 1140 |
queryVpnAddr, useVersion, err := n.Details.GetVpnAddrAndVersion() |
| 1141 |
if err != nil { |
| 1142 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1143 |
lhh.l.Debug("Dropping malformed HostQuery", |
| 1144 |
"from", fromVpnAddrs, |
| 1145 |
"details", n.Details, |
| 1146 |
) |
| 1147 |
} |
| 1148 |
return |
| 1149 |
} |
| 1150 |
if useVersion == cert.Version1 && queryVpnAddr.Is6() { |
| 1151 |
// this case really shouldn't be possible to represent, but reject it anyway. |
| 1152 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1153 |
lhh.l.Debug("invalid vpn addr for v1 handleHostQuery", |
| 1154 |
"vpnAddrs", fromVpnAddrs, |
| 1155 |
"queryVpnAddr", queryVpnAddr, |
| 1156 |
) |
| 1157 |
} |
| 1158 |
return |
| 1159 |
} |
| 1160 |
|
| 1161 |
found, ln, err := lhh.lh.queryAndPrepMessage(queryVpnAddr, func(c *cache) (int, error) { |
| 1162 |
n = lhh.resetMeta() |
| 1163 |
n.Type = NebulaMeta_HostQueryReply |
| 1164 |
if useVersion == cert.Version1 { |
| 1165 |
b := queryVpnAddr.As4() |
| 1166 |
n.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:]) |
| 1167 |
} else { |
| 1168 |
n.Details.VpnAddr = netAddrToProtoAddr(queryVpnAddr) |
| 1169 |
} |
| 1170 |
|
| 1171 |
lhh.coalesceAnswers(useVersion, c, n) |
| 1172 |
|
| 1173 |
return n.MarshalTo(lhh.pb) |
| 1174 |
}) |
| 1175 |
|
| 1176 |
if !found { |
| 1177 |
return |
| 1178 |
} |
| 1179 |
|
| 1180 |
if err != nil { |
| 1181 |
lhh.l.Error("Failed to marshal lighthouse host query reply", |
| 1182 |
"error", err, |
| 1183 |
"vpnAddrs", fromVpnAddrs, |
| 1184 |
) |
| 1185 |
return |
| 1186 |
} |
| 1187 |
|
| 1188 |
lhh.lh.metricTx(NebulaMeta_HostQueryReply, 1) |
| 1189 |
w.SendMessageToVpnAddr(header.LightHouse, 0, fromVpnAddrs[0], lhh.pb[:ln], lhh.nb, lhh.out[:0]) |
| 1190 |
|
| 1191 |
lhh.sendHostPunchNotification(n, fromVpnAddrs, queryVpnAddr, w) |
| 1192 |
} |
| 1193 |
|
| 1194 |
// sendHostPunchNotification signals the other side to punch some zero byte udp packets |
| 1195 |
func (lhh *LightHouseHandler) sendHostPunchNotification(n *NebulaMeta, fromVpnAddrs []netip.Addr, punchNotifDest netip.Addr, w EncWriter) { |
| 1196 |
whereToPunch := fromVpnAddrs[0] |
| 1197 |
found, ln, err := lhh.lh.queryAndPrepMessage(whereToPunch, func(c *cache) (int, error) { |
| 1198 |
n = lhh.resetMeta() |
| 1199 |
n.Type = NebulaMeta_HostPunchNotification |
| 1200 |
targetHI := lhh.lh.ifce.GetHostInfo(punchNotifDest) |
| 1201 |
var useVersion cert.Version |
| 1202 |
if targetHI == nil { |
| 1203 |
useVersion = lhh.lh.ifce.GetCertState().initiatingVersion |
| 1204 |
} else { |
| 1205 |
crt := targetHI.GetCert().Certificate |
| 1206 |
useVersion = crt.Version() |
| 1207 |
// we can only retarget if we have a hostinfo |
| 1208 |
newDest, ok := findNetworkUnion(crt.Networks(), fromVpnAddrs) |
| 1209 |
if ok { |
| 1210 |
whereToPunch = newDest |
| 1211 |
} else { |
| 1212 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1213 |
lhh.l.Debug("unable to punch to host, no addresses in common", |
| 1214 |
"to", crt.Networks(), |
| 1215 |
) |
| 1216 |
} |
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
if useVersion == cert.Version1 { |
| 1221 |
if !whereToPunch.Is4() { |
| 1222 |
return 0, fmt.Errorf("invalid vpn addr for v1 handleHostQuery") |
| 1223 |
} |
| 1224 |
b := whereToPunch.As4() |
| 1225 |
n.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:]) |
| 1226 |
} else if useVersion == cert.Version2 { |
| 1227 |
n.Details.VpnAddr = netAddrToProtoAddr(whereToPunch) |
| 1228 |
} else { |
| 1229 |
return 0, errors.New("unsupported version") |
| 1230 |
} |
| 1231 |
lhh.coalesceAnswers(useVersion, c, n) |
| 1232 |
|
| 1233 |
return n.MarshalTo(lhh.pb) |
| 1234 |
}) |
| 1235 |
|
| 1236 |
if !found { |
| 1237 |
return |
| 1238 |
} |
| 1239 |
|
| 1240 |
if err != nil { |
| 1241 |
lhh.l.Error("Failed to marshal lighthouse host was queried for", |
| 1242 |
"error", err, |
| 1243 |
"vpnAddrs", fromVpnAddrs, |
| 1244 |
) |
| 1245 |
return |
| 1246 |
} |
| 1247 |
|
| 1248 |
lhh.lh.metricTx(NebulaMeta_HostPunchNotification, 1) |
| 1249 |
w.SendMessageToVpnAddr(header.LightHouse, 0, punchNotifDest, lhh.pb[:ln], lhh.nb, lhh.out[:0]) |
| 1250 |
} |
| 1251 |
|
| 1252 |
func (lhh *LightHouseHandler) coalesceAnswers(v cert.Version, c *cache, n *NebulaMeta) { |
| 1253 |
if c.v4 != nil { |
| 1254 |
if c.v4.learned != nil { |
| 1255 |
n.Details.V4AddrPorts = append(n.Details.V4AddrPorts, c.v4.learned) |
| 1256 |
} |
| 1257 |
if c.v4.reported != nil && len(c.v4.reported) > 0 { |
| 1258 |
n.Details.V4AddrPorts = append(n.Details.V4AddrPorts, c.v4.reported...) |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
if c.v6 != nil { |
| 1263 |
if c.v6.learned != nil { |
| 1264 |
n.Details.V6AddrPorts = append(n.Details.V6AddrPorts, c.v6.learned) |
| 1265 |
} |
| 1266 |
if c.v6.reported != nil && len(c.v6.reported) > 0 { |
| 1267 |
n.Details.V6AddrPorts = append(n.Details.V6AddrPorts, c.v6.reported...) |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
if c.relay != nil { |
| 1272 |
if v == cert.Version1 { |
| 1273 |
b := [4]byte{} |
| 1274 |
for _, r := range c.relay.relay { |
| 1275 |
if !r.Is4() { |
| 1276 |
continue |
| 1277 |
} |
| 1278 |
b = r.As4() |
| 1279 |
n.Details.OldRelayVpnAddrs = append(n.Details.OldRelayVpnAddrs, binary.BigEndian.Uint32(b[:])) |
| 1280 |
} |
| 1281 |
} else if v == cert.Version2 { |
| 1282 |
for _, r := range c.relay.relay { |
| 1283 |
n.Details.RelayVpnAddrs = append(n.Details.RelayVpnAddrs, netAddrToProtoAddr(r)) |
| 1284 |
} |
| 1285 |
} else { |
| 1286 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1287 |
lhh.l.Debug("unsupported protocol version", |
| 1288 |
"op", "coalesceAnswers", |
| 1289 |
"version", v, |
| 1290 |
) |
| 1291 |
} |
| 1292 |
} |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
func (lhh *LightHouseHandler) handleHostQueryReply(n *NebulaMeta, fromVpnAddrs []netip.Addr) { |
| 1297 |
if !lhh.lh.IsAnyLighthouseAddr(fromVpnAddrs) { |
| 1298 |
return |
| 1299 |
} |
| 1300 |
|
| 1301 |
certVpnAddr, _, err := n.Details.GetVpnAddrAndVersion() |
| 1302 |
if err != nil { |
| 1303 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1304 |
lhh.l.Error("dropping malformed HostQueryReply", |
| 1305 |
"error", err, |
| 1306 |
"vpnAddrs", fromVpnAddrs, |
| 1307 |
) |
| 1308 |
} |
| 1309 |
return |
| 1310 |
} |
| 1311 |
relays := n.Details.GetRelays() |
| 1312 |
|
| 1313 |
lhh.lh.Lock() |
| 1314 |
am := lhh.lh.unlockedGetRemoteList([]netip.Addr{certVpnAddr}) |
| 1315 |
am.Lock() |
| 1316 |
lhh.lh.Unlock() |
| 1317 |
|
| 1318 |
am.unlockedSetV4(fromVpnAddrs[0], certVpnAddr, n.Details.V4AddrPorts, lhh.lh.unlockedShouldAddV4) |
| 1319 |
am.unlockedSetV6(fromVpnAddrs[0], certVpnAddr, n.Details.V6AddrPorts, lhh.lh.unlockedShouldAddV6) |
| 1320 |
am.unlockedSetRelay(fromVpnAddrs[0], relays) |
| 1321 |
am.Unlock() |
| 1322 |
|
| 1323 |
// Non-blocking attempt to trigger, skip if it would block |
| 1324 |
select { |
| 1325 |
case lhh.lh.handshakeTrigger <- certVpnAddr: |
| 1326 |
default: |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
func (lhh *LightHouseHandler) handleHostUpdateNotification(n *NebulaMeta, fromVpnAddrs []netip.Addr, w EncWriter) { |
| 1331 |
if !lhh.lh.amLighthouse { |
| 1332 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1333 |
lhh.l.Debug("I am not a lighthouse, do not take host updates", "from", fromVpnAddrs) |
| 1334 |
} |
| 1335 |
return |
| 1336 |
} |
| 1337 |
|
| 1338 |
// not using GetVpnAddrAndVersion because we don't want to error on a blank detailsVpnAddr |
| 1339 |
var detailsVpnAddr netip.Addr |
| 1340 |
var useVersion cert.Version |
| 1341 |
if n.Details.OldVpnAddr != 0 { //v1 always sets this field |
| 1342 |
b := [4]byte{} |
| 1343 |
binary.BigEndian.PutUint32(b[:], n.Details.OldVpnAddr) |
| 1344 |
detailsVpnAddr = netip.AddrFrom4(b) |
| 1345 |
useVersion = cert.Version1 |
| 1346 |
} else if n.Details.VpnAddr != nil { //this field is "optional" in v2, but if it's set, we should enforce it |
| 1347 |
detailsVpnAddr = protoAddrToNetAddr(n.Details.VpnAddr) |
| 1348 |
useVersion = cert.Version2 |
| 1349 |
} else { |
| 1350 |
detailsVpnAddr = netip.Addr{} |
| 1351 |
useVersion = cert.Version2 |
| 1352 |
} |
| 1353 |
|
| 1354 |
//Simple check that the host sent this not someone else, if detailsVpnAddr is filled |
| 1355 |
if detailsVpnAddr.IsValid() && !slices.Contains(fromVpnAddrs, detailsVpnAddr) { |
| 1356 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1357 |
lhh.l.Debug("Host sent invalid update", |
| 1358 |
"vpnAddrs", fromVpnAddrs, |
| 1359 |
"answer", detailsVpnAddr, |
| 1360 |
) |
| 1361 |
} |
| 1362 |
return |
| 1363 |
} |
| 1364 |
|
| 1365 |
relays := n.Details.GetRelays() |
| 1366 |
|
| 1367 |
lhh.lh.Lock() |
| 1368 |
am := lhh.lh.unlockedGetRemoteList(fromVpnAddrs) |
| 1369 |
am.Lock() |
| 1370 |
lhh.lh.Unlock() |
| 1371 |
|
| 1372 |
am.unlockedSetV4(fromVpnAddrs[0], fromVpnAddrs[0], n.Details.V4AddrPorts, lhh.lh.unlockedShouldAddV4) |
| 1373 |
am.unlockedSetV6(fromVpnAddrs[0], fromVpnAddrs[0], n.Details.V6AddrPorts, lhh.lh.unlockedShouldAddV6) |
| 1374 |
am.unlockedSetRelay(fromVpnAddrs[0], relays) |
| 1375 |
am.Unlock() |
| 1376 |
|
| 1377 |
n = lhh.resetMeta() |
| 1378 |
n.Type = NebulaMeta_HostUpdateNotificationAck |
| 1379 |
switch useVersion { |
| 1380 |
case cert.Version1: |
| 1381 |
if !fromVpnAddrs[0].Is4() { |
| 1382 |
lhh.l.Error("Can not send HostUpdateNotificationAck for a ipv6 vpn ip in a v1 message", |
| 1383 |
"vpnAddrs", fromVpnAddrs, |
| 1384 |
) |
| 1385 |
return |
| 1386 |
} |
| 1387 |
vpnAddrB := fromVpnAddrs[0].As4() |
| 1388 |
n.Details.OldVpnAddr = binary.BigEndian.Uint32(vpnAddrB[:]) |
| 1389 |
case cert.Version2: |
| 1390 |
// do nothing, we want to send a blank message |
| 1391 |
default: |
| 1392 |
lhh.l.Error("invalid protocol version", "useVersion", useVersion) |
| 1393 |
return |
| 1394 |
} |
| 1395 |
|
| 1396 |
ln, err := n.MarshalTo(lhh.pb) |
| 1397 |
if err != nil { |
| 1398 |
lhh.l.Error("Failed to marshal lighthouse host update ack", |
| 1399 |
"error", err, |
| 1400 |
"vpnAddrs", fromVpnAddrs, |
| 1401 |
) |
| 1402 |
return |
| 1403 |
} |
| 1404 |
|
| 1405 |
lhh.lh.metricTx(NebulaMeta_HostUpdateNotificationAck, 1) |
| 1406 |
w.SendMessageToVpnAddr(header.LightHouse, 0, fromVpnAddrs[0], lhh.pb[:ln], lhh.nb, lhh.out[:0]) |
| 1407 |
} |
| 1408 |
|
| 1409 |
func (lhh *LightHouseHandler) handleHostPunchNotification(n *NebulaMeta, fromVpnAddrs []netip.Addr, w EncWriter) { |
| 1410 |
//It's possible the lighthouse is communicating with us using a non primary vpn addr, |
| 1411 |
//which means we need to compare all fromVpnAddrs against all configured lighthouse vpn addrs. |
| 1412 |
if !lhh.lh.IsAnyLighthouseAddr(fromVpnAddrs) { |
| 1413 |
return |
| 1414 |
} |
| 1415 |
|
| 1416 |
detailsVpnAddr, _, err := n.Details.GetVpnAddrAndVersion() |
| 1417 |
if err != nil { |
| 1418 |
if lhh.l.Enabled(context.Background(), slog.LevelDebug) { |
| 1419 |
lhh.l.Debug("dropping invalid HostPunchNotification", |
| 1420 |
"details", n.Details, |
| 1421 |
"error", err, |
| 1422 |
) |
| 1423 |
} |
| 1424 |
return |
| 1425 |
} |
| 1426 |
|
| 1427 |
remoteAllowList := lhh.lh.GetRemoteAllowList() |
| 1428 |
for _, a := range n.Details.V4AddrPorts { |
| 1429 |
if a == nil { |
| 1430 |
continue |
| 1431 |
} |
| 1432 |
b := protoV4AddrPortToNetAddrPort(a) |
| 1433 |
if remoteAllowList.Allow(detailsVpnAddr, b.Addr()) { |
| 1434 |
lhh.lh.punchy.Schedule(b, detailsVpnAddr) |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
for _, a := range n.Details.V6AddrPorts { |
| 1439 |
if a == nil { |
| 1440 |
continue |
| 1441 |
} |
| 1442 |
b := protoV6AddrPortToNetAddrPort(a) |
| 1443 |
if remoteAllowList.Allow(detailsVpnAddr, b.Addr()) { |
| 1444 |
lhh.lh.punchy.Schedule(b, detailsVpnAddr) |
| 1445 |
} |
| 1446 |
} |
| 1447 |
|
| 1448 |
// This sends a nebula test packet to the host trying to contact us. In the case |
| 1449 |
// of a double nat or other difficult scenario, this may help establish |
| 1450 |
// a tunnel. ScheduleRespond is a no-op when punchy.respond is disabled. |
| 1451 |
lhh.lh.punchy.ScheduleRespond(detailsVpnAddr) |
| 1452 |
} |
| 1453 |
|
| 1454 |
func protoAddrToNetAddr(addr *Addr) netip.Addr { |
| 1455 |
b := [16]byte{} |
| 1456 |
binary.BigEndian.PutUint64(b[:8], addr.Hi) |
| 1457 |
binary.BigEndian.PutUint64(b[8:], addr.Lo) |
| 1458 |
return netip.AddrFrom16(b).Unmap() |
| 1459 |
} |
| 1460 |
|
| 1461 |
func protoV4AddrPortToNetAddrPort(ap *V4AddrPort) netip.AddrPort { |
| 1462 |
b := [4]byte{} |
| 1463 |
binary.BigEndian.PutUint32(b[:], ap.Addr) |
| 1464 |
return netip.AddrPortFrom(netip.AddrFrom4(b), uint16(ap.Port)) |
| 1465 |
} |
| 1466 |
|
| 1467 |
func protoV6AddrPortToNetAddrPort(ap *V6AddrPort) netip.AddrPort { |
| 1468 |
b := [16]byte{} |
| 1469 |
binary.BigEndian.PutUint64(b[:8], ap.Hi) |
| 1470 |
binary.BigEndian.PutUint64(b[8:], ap.Lo) |
| 1471 |
return netip.AddrPortFrom(netip.AddrFrom16(b).Unmap(), uint16(ap.Port)) |
| 1472 |
} |
| 1473 |
|
| 1474 |
func netAddrToProtoAddr(addr netip.Addr) *Addr { |
| 1475 |
b := addr.As16() |
| 1476 |
return &Addr{ |
| 1477 |
Hi: binary.BigEndian.Uint64(b[:8]), |
| 1478 |
Lo: binary.BigEndian.Uint64(b[8:]), |
| 1479 |
} |
| 1480 |
} |
| 1481 |
|
| 1482 |
func netAddrToProtoV4AddrPort(addr netip.Addr, port uint16) *V4AddrPort { |
| 1483 |
v4Addr := addr.As4() |
| 1484 |
return &V4AddrPort{ |
| 1485 |
Addr: binary.BigEndian.Uint32(v4Addr[:]), |
| 1486 |
Port: uint32(port), |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
func netAddrToProtoV6AddrPort(addr netip.Addr, port uint16) *V6AddrPort { |
| 1491 |
v6Addr := addr.As16() |
| 1492 |
return &V6AddrPort{ |
| 1493 |
Hi: binary.BigEndian.Uint64(v6Addr[:8]), |
| 1494 |
Lo: binary.BigEndian.Uint64(v6Addr[8:]), |
| 1495 |
Port: uint32(port), |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
func (d *NebulaMetaDetails) GetRelays() []netip.Addr { |
| 1500 |
var relays []netip.Addr |
| 1501 |
if len(d.OldRelayVpnAddrs) > 0 { |
| 1502 |
b := [4]byte{} |
| 1503 |
for _, r := range d.OldRelayVpnAddrs { |
| 1504 |
binary.BigEndian.PutUint32(b[:], r) |
| 1505 |
relays = append(relays, netip.AddrFrom4(b)) |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
if len(d.RelayVpnAddrs) > 0 { |
| 1510 |
for _, r := range d.RelayVpnAddrs { |
| 1511 |
if r != nil { |
| 1512 |
relays = append(relays, protoAddrToNetAddr(r)) |
| 1513 |
} |
| 1514 |
} |
| 1515 |
} |
| 1516 |
return relays |
| 1517 |
} |
| 1518 |
|
| 1519 |
// FindNetworkUnion returns the first netip.Addr contained in the list of provided netip.Prefix, if able |
| 1520 |
func findNetworkUnion(prefixes []netip.Prefix, addrs []netip.Addr) (netip.Addr, bool) { |
| 1521 |
for i := range prefixes { |
| 1522 |
for j := range addrs { |
| 1523 |
if prefixes[i].Contains(addrs[j]) { |
| 1524 |
return addrs[j], true |
| 1525 |
} |
| 1526 |
} |
| 1527 |
} |
| 1528 |
return netip.Addr{}, false |
| 1529 |
} |
| 1530 |
|
| 1531 |
func (d *NebulaMetaDetails) GetVpnAddrAndVersion() (netip.Addr, cert.Version, error) { |
| 1532 |
if d.OldVpnAddr != 0 { |
| 1533 |
b := [4]byte{} |
| 1534 |
binary.BigEndian.PutUint32(b[:], d.OldVpnAddr) |
| 1535 |
detailsVpnAddr := netip.AddrFrom4(b) |
| 1536 |
return detailsVpnAddr, cert.Version1, nil |
| 1537 |
} else if d.VpnAddr != nil { |
| 1538 |
detailsVpnAddr := protoAddrToNetAddr(d.VpnAddr) |
| 1539 |
return detailsVpnAddr, cert.Version2, nil |
| 1540 |
} else { |
| 1541 |
return netip.Addr{}, cert.Version1, ErrBadDetailsVpnAddr |
| 1542 |
} |
| 1543 |
} |