| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"log/slog" |
| 8 |
"net/netip" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/google/gopacket/layers" |
| 12 |
"golang.org/x/net/ipv6" |
| 13 |
|
| 14 |
"github.com/slackhq/nebula/firewall" |
| 15 |
"github.com/slackhq/nebula/header" |
| 16 |
"golang.org/x/net/ipv4" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
minFwPacketLen = 4 |
| 21 |
) |
| 22 |
|
| 23 |
var ErrOutOfWindow = errors.New("out of window packet") |
| 24 |
|
| 25 |
func (f *Interface) readOutsidePackets(via ViaSender, out []byte, packet []byte, h *header.H, fwPacket *firewall.Packet, lhf *LightHouseHandler, nb []byte, q int, localCache firewall.ConntrackCache) { |
| 26 |
err := h.Parse(packet) |
| 27 |
if err != nil { |
| 28 |
// Hole punch packets are 0 or 1 byte big, so lets ignore printing those errors |
| 29 |
// TODO: record metrics for rx holepunch/punchy packets? |
| 30 |
if len(packet) > 1 { |
| 31 |
f.messageMetrics.RxInvalid(1) |
| 32 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 33 |
f.l.Debug("Error while parsing inbound packet", |
| 34 |
"from", via, |
| 35 |
"error", err, |
| 36 |
"packet", packet, |
| 37 |
) |
| 38 |
} |
| 39 |
} |
| 40 |
return |
| 41 |
} |
| 42 |
|
| 43 |
if h.Version != header.Version { |
| 44 |
f.messageMetrics.RxInvalid(1) |
| 45 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 46 |
f.l.Debug("Unexpected header version received", "from", via) |
| 47 |
} |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
// Check before processing to see if this is a expected type/subtype |
| 52 |
if !h.IsValidSubType() { |
| 53 |
f.messageMetrics.RxInvalid(1) |
| 54 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 55 |
f.l.Debug("Unexpected packet received", "from", via) |
| 56 |
} |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
if !via.IsRelayed { |
| 61 |
if f.myVpnNetworksTable.Contains(via.UdpAddr.Addr()) { |
| 62 |
f.messageMetrics.RxInvalid(1) |
| 63 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 64 |
f.l.Debug("Refusing to process double encrypted packet", "from", via) |
| 65 |
} |
| 66 |
return |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// don't keep Rx metrics for message type, since you can see those in the tun metrics |
| 71 |
if h.Type != header.Message { |
| 72 |
f.messageMetrics.Rx(h.Type, h.Subtype, 1) |
| 73 |
} |
| 74 |
|
| 75 |
// Unencrypted packets |
| 76 |
switch h.Type { |
| 77 |
case header.Handshake: |
| 78 |
f.handshakeManager.HandleIncoming(via, packet, h) |
| 79 |
return |
| 80 |
|
| 81 |
case header.RecvError: |
| 82 |
f.handleRecvError(via.UdpAddr, h) |
| 83 |
return |
| 84 |
} |
| 85 |
|
| 86 |
// Relay packets are special |
| 87 |
isMessageRelay := (h.Type == header.Message && h.Subtype == header.MessageRelay) |
| 88 |
|
| 89 |
var hostinfo *HostInfo |
| 90 |
if isMessageRelay { |
| 91 |
hostinfo = f.hostMap.QueryRelayIndex(h.RemoteIndex) |
| 92 |
} else { |
| 93 |
hostinfo = f.hostMap.QueryIndex(h.RemoteIndex) |
| 94 |
} |
| 95 |
|
| 96 |
// At this point we should have a valid existing tunnel, verify and send |
| 97 |
// recvError if necessary |
| 98 |
if hostinfo == nil || hostinfo.ConnectionState == nil { |
| 99 |
if !via.IsRelayed { |
| 100 |
f.maybeSendRecvError(via.UdpAddr, h.RemoteIndex) |
| 101 |
} |
| 102 |
return |
| 103 |
} |
| 104 |
|
| 105 |
if len(packet) < header.Len+hostinfo.ConnectionState.dKey.Overhead() { |
| 106 |
f.messageMetrics.RxInvalid(1) |
| 107 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 108 |
f.l.Debug("packet too small", "from", via, "length", len(packet)) |
| 109 |
} |
| 110 |
return |
| 111 |
} |
| 112 |
|
| 113 |
// All remaining packets are encrypted |
| 114 |
if isMessageRelay { |
| 115 |
// Relay packets are special, this branch should always early-return |
| 116 |
if err = hostinfo.ConnectionState.VerifyRelay(f.l, h.MessageCounter, packet, nb); err != nil { |
| 117 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 118 |
hostinfo.logger(f.l).Debug("Failed to verify relay packet", "error", err, "from", via, "header", h) |
| 119 |
} |
| 120 |
return |
| 121 |
} |
| 122 |
f.handleOutsideRelayPacket(hostinfo, via, out, packet, h, fwPacket, lhf, nb, q, localCache) |
| 123 |
return |
| 124 |
} |
| 125 |
|
| 126 |
out, err = hostinfo.ConnectionState.Decrypt(f.l, h.MessageCounter, out, packet, nb) |
| 127 |
if err != nil { |
| 128 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 129 |
hostinfo.logger(f.l).Debug("Failed to decrypt packet", "error", err, "from", via, "header", h) |
| 130 |
} |
| 131 |
return |
| 132 |
} |
| 133 |
|
| 134 |
// Roam before we respond |
| 135 |
f.handleHostRoaming(hostinfo, via) |
| 136 |
f.connectionManager.In(hostinfo) |
| 137 |
|
| 138 |
switch h.Type { |
| 139 |
case header.Message: |
| 140 |
switch h.Subtype { |
| 141 |
case header.MessageNone: |
| 142 |
f.handleOutsideMessagePacket(hostinfo, out, packet, fwPacket, nb, q, localCache) |
| 143 |
default: |
| 144 |
hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected message subtype seen", "from", via, "header", h) |
| 145 |
return |
| 146 |
} |
| 147 |
|
| 148 |
case header.LightHouse: |
| 149 |
//TODO: assert via is not relayed |
| 150 |
lhf.HandleRequest(via.UdpAddr, hostinfo.vpnAddrs, out, f) |
| 151 |
|
| 152 |
case header.Test: |
| 153 |
switch h.Subtype { |
| 154 |
case header.TestReply: |
| 155 |
// No-op, useful for the Roaming and connectionManager side-effects above |
| 156 |
case header.TestRequest: |
| 157 |
//recycle the input packet ciphertext as our output buffer |
| 158 |
f.send(header.Test, header.TestReply, hostinfo.ConnectionState, hostinfo, out, nb, packet) |
| 159 |
default: |
| 160 |
hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected test subtype seen", "from", via, "header", h) |
| 161 |
return |
| 162 |
} |
| 163 |
|
| 164 |
case header.CloseTunnel: |
| 165 |
hostinfo.logger(f.l).Info("Close tunnel received, tearing down.", "from", via) |
| 166 |
f.closeTunnel(hostinfo) |
| 167 |
|
| 168 |
case header.Control: |
| 169 |
f.relayManager.HandleControlMsg(hostinfo, out, f) |
| 170 |
|
| 171 |
default: |
| 172 |
hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected message type seen", "from", via, "header", h) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func (f *Interface) handleOutsideRelayPacket(hostinfo *HostInfo, via ViaSender, out []byte, packet []byte, h *header.H, fwPacket *firewall.Packet, lhf *LightHouseHandler, nb []byte, q int, localCache firewall.ConntrackCache) { |
| 177 |
// Successfully validated the thing. Get rid of the Relay header and the AEAD tag |
| 178 |
signedPayload := packet[header.Len : len(packet)-hostinfo.ConnectionState.dKey.Overhead()] |
| 179 |
// Pull the Roaming parts up here, and return in all call paths. |
| 180 |
f.handleHostRoaming(hostinfo, via) |
| 181 |
// Track usage of both the HostInfo and the Relay for the received & authenticated packet |
| 182 |
f.connectionManager.In(hostinfo) |
| 183 |
f.connectionManager.RelayUsed(h.RemoteIndex) |
| 184 |
|
| 185 |
relay, ok := hostinfo.relayState.QueryRelayForByIdx(h.RemoteIndex) |
| 186 |
if !ok { |
| 187 |
// The only way this happens is if hostmap has an index to the correct HostInfo, but the HostInfo is missing |
| 188 |
// its internal mapping. This should never happen. |
| 189 |
hostinfo.logger(f.l).Error("HostInfo missing remote relay index", |
| 190 |
"relayRemoteIndex", h.RemoteIndex, |
| 191 |
) |
| 192 |
return |
| 193 |
} |
| 194 |
|
| 195 |
switch relay.Type { |
| 196 |
case TerminalType: |
| 197 |
// If I am the target of this relay, process the unwrapped packet |
| 198 |
// From this recursive point, all these variables are 'burned'. We shouldn't rely on them again. |
| 199 |
via = ViaSender{ |
| 200 |
UdpAddr: via.UdpAddr, |
| 201 |
relayHI: hostinfo, |
| 202 |
relay: relay, |
| 203 |
IsRelayed: true, |
| 204 |
} |
| 205 |
f.readOutsidePackets(via, out[:0], signedPayload, h, fwPacket, lhf, nb, q, localCache) |
| 206 |
case ForwardingType: |
| 207 |
// Find the target HostInfo relay object |
| 208 |
targetHI, targetRelay, err := f.hostMap.QueryVpnAddrsRelayFor(hostinfo.vpnAddrs, relay.PeerAddr) |
| 209 |
if err != nil { |
| 210 |
hostinfo.logger(f.l).Info("Failed to find target host info by ip", |
| 211 |
"relayTo", relay.PeerAddr, |
| 212 |
"relayFrom", hostinfo.vpnAddrs[0], |
| 213 |
"error", err, |
| 214 |
) |
| 215 |
return |
| 216 |
} |
| 217 |
|
| 218 |
// If that relay is Established, forward the payload through it |
| 219 |
if targetRelay.State == Established { |
| 220 |
switch targetRelay.Type { |
| 221 |
case ForwardingType: |
| 222 |
// Forward this packet through the relay tunnel, rebuilding it in place. |
| 223 |
// Encode overwrites the old outer header, and the new AEAD tag lands where the old one was |
| 224 |
fwdBuf := packet[:0:len(packet)] // Cap to len(packet) to protect memory from a larger parent buffer |
| 225 |
f.SendVia(targetHI, targetRelay, signedPayload, nb, fwdBuf, true) |
| 226 |
case TerminalType: |
| 227 |
hostinfo.logger(f.l).Error("Unexpected Relay Type of Terminal") |
| 228 |
return |
| 229 |
default: |
| 230 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 231 |
hostinfo.logger(f.l).Debug("Unexpected targetRelay Type", "from", via, "relayType", targetRelay.Type) |
| 232 |
} |
| 233 |
return |
| 234 |
} |
| 235 |
} else { |
| 236 |
hostinfo.logger(f.l).Info("Unexpected target relay state", |
| 237 |
"relayTo", relay.PeerAddr, |
| 238 |
"relayFrom", hostinfo.vpnAddrs[0], |
| 239 |
"targetRelayState", targetRelay.State, |
| 240 |
) |
| 241 |
return |
| 242 |
} |
| 243 |
default: |
| 244 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 245 |
hostinfo.logger(f.l).Debug("Unexpected relay type", "from", via, "relayType", relay.Type) |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// closeTunnel closes a tunnel locally, it does not send a closeTunnel packet to the remote |
| 251 |
func (f *Interface) closeTunnel(hostInfo *HostInfo) { |
| 252 |
final := f.hostMap.DeleteHostInfo(hostInfo) |
| 253 |
if final { |
| 254 |
// We no longer have any tunnels with this vpn addr, clear learned lighthouse state to lower memory usage |
| 255 |
f.lightHouse.DeleteVpnAddrs(hostInfo.vpnAddrs) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// sendCloseTunnel is a helper function to send a proper close tunnel packet to a remote |
| 260 |
func (f *Interface) sendCloseTunnel(h *HostInfo) { |
| 261 |
f.send(header.CloseTunnel, 0, h.ConnectionState, h, []byte{}, make([]byte, 12, 12), make([]byte, mtu)) |
| 262 |
} |
| 263 |
|
| 264 |
func (f *Interface) handleHostRoaming(hostinfo *HostInfo, via ViaSender) { |
| 265 |
curRemote := hostinfo.GetRemote() |
| 266 |
if !via.IsRelayed && curRemote != via.UdpAddr { |
| 267 |
if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) { |
| 268 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 269 |
hostinfo.logger(f.l).Debug("lighthouse.remote_allow_list denied roaming", "newAddr", via.UdpAddr) |
| 270 |
} |
| 271 |
return |
| 272 |
} |
| 273 |
|
| 274 |
if !hostinfo.lastRoam.IsZero() && via.UdpAddr == hostinfo.lastRoamRemote && time.Since(hostinfo.lastRoam) < RoamingSuppressSeconds*time.Second { |
| 275 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 276 |
hostinfo.logger(f.l).Debug("Suppressing roam back to previous remote", |
| 277 |
"suppressSeconds", RoamingSuppressSeconds, |
| 278 |
"udpAddr", curRemote, |
| 279 |
"newAddr", via.UdpAddr, |
| 280 |
) |
| 281 |
} |
| 282 |
return |
| 283 |
} |
| 284 |
|
| 285 |
hostinfo.logger(f.l).Info("Host roamed to new udp ip/port.", |
| 286 |
"udpAddr", curRemote, |
| 287 |
"newAddr", via.UdpAddr, |
| 288 |
) |
| 289 |
hostinfo.lastRoam = time.Now() |
| 290 |
hostinfo.lastRoamRemote = curRemote |
| 291 |
hostinfo.SetRemote(via.UdpAddr) |
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
var ( |
| 297 |
ErrPacketTooShort = errors.New("packet is too short") |
| 298 |
ErrUnknownIPVersion = errors.New("packet is an unknown ip version") |
| 299 |
ErrIPv4InvalidHeaderLength = errors.New("invalid ipv4 header length") |
| 300 |
ErrIPv4PacketTooShort = errors.New("ipv4 packet is too short") |
| 301 |
ErrIPv6PacketTooShort = errors.New("ipv6 packet is too short") |
| 302 |
ErrIPv6CouldNotFindPayload = errors.New("could not find payload in ipv6 packet") |
| 303 |
) |
| 304 |
|
| 305 |
// newPacket validates and parses the interesting bits for the firewall out of the ip and sub protocol headers |
| 306 |
func newPacket(data []byte, incoming bool, fp *firewall.Packet) error { |
| 307 |
if len(data) < 1 { |
| 308 |
return ErrPacketTooShort |
| 309 |
} |
| 310 |
|
| 311 |
version := int((data[0] >> 4) & 0x0f) |
| 312 |
switch version { |
| 313 |
case ipv4.Version: |
| 314 |
return parseV4(data, incoming, fp) |
| 315 |
case ipv6.Version: |
| 316 |
return parseV6(data, incoming, fp) |
| 317 |
} |
| 318 |
return ErrUnknownIPVersion |
| 319 |
} |
| 320 |
|
| 321 |
func parseV6(data []byte, incoming bool, fp *firewall.Packet) error { |
| 322 |
dataLen := len(data) |
| 323 |
if dataLen < ipv6.HeaderLen { |
| 324 |
return ErrIPv6PacketTooShort |
| 325 |
} |
| 326 |
|
| 327 |
if incoming { |
| 328 |
fp.RemoteAddr, _ = netip.AddrFromSlice(data[8:24]) |
| 329 |
fp.LocalAddr, _ = netip.AddrFromSlice(data[24:40]) |
| 330 |
} else { |
| 331 |
fp.LocalAddr, _ = netip.AddrFromSlice(data[8:24]) |
| 332 |
fp.RemoteAddr, _ = netip.AddrFromSlice(data[24:40]) |
| 333 |
} |
| 334 |
|
| 335 |
protoAt := 6 // NextHeader is at 6 bytes into the ipv6 header |
| 336 |
offset := ipv6.HeaderLen // Start at the end of the ipv6 header |
| 337 |
next := 0 |
| 338 |
for { |
| 339 |
if protoAt >= dataLen { |
| 340 |
break |
| 341 |
} |
| 342 |
proto := layers.IPProtocol(data[protoAt]) |
| 343 |
|
| 344 |
switch proto { |
| 345 |
case layers.IPProtocolESP, layers.IPProtocolNoNextHeader: |
| 346 |
fp.Protocol = uint8(proto) |
| 347 |
fp.RemotePort = 0 |
| 348 |
fp.LocalPort = 0 |
| 349 |
fp.Fragment = false |
| 350 |
return nil |
| 351 |
|
| 352 |
case layers.IPProtocolICMPv6: |
| 353 |
if dataLen < offset+6 { |
| 354 |
return ErrIPv6PacketTooShort |
| 355 |
} |
| 356 |
fp.Protocol = uint8(proto) |
| 357 |
fp.LocalPort = 0 //incoming vs outgoing doesn't matter for icmpv6 |
| 358 |
icmptype := data[offset+1] |
| 359 |
switch icmptype { |
| 360 |
case layers.ICMPv6TypeEchoRequest, layers.ICMPv6TypeEchoReply: |
| 361 |
fp.RemotePort = binary.BigEndian.Uint16(data[offset+4 : offset+6]) //identifier |
| 362 |
default: |
| 363 |
fp.RemotePort = 0 |
| 364 |
} |
| 365 |
fp.Fragment = false |
| 366 |
return nil |
| 367 |
|
| 368 |
case layers.IPProtocolTCP, layers.IPProtocolUDP: |
| 369 |
if dataLen < offset+4 { |
| 370 |
return ErrIPv6PacketTooShort |
| 371 |
} |
| 372 |
|
| 373 |
fp.Protocol = uint8(proto) |
| 374 |
if incoming { |
| 375 |
fp.RemotePort = binary.BigEndian.Uint16(data[offset : offset+2]) |
| 376 |
fp.LocalPort = binary.BigEndian.Uint16(data[offset+2 : offset+4]) |
| 377 |
} else { |
| 378 |
fp.LocalPort = binary.BigEndian.Uint16(data[offset : offset+2]) |
| 379 |
fp.RemotePort = binary.BigEndian.Uint16(data[offset+2 : offset+4]) |
| 380 |
} |
| 381 |
|
| 382 |
fp.Fragment = false |
| 383 |
return nil |
| 384 |
|
| 385 |
case layers.IPProtocolIPv6Fragment: |
| 386 |
// Fragment header is 8 bytes, need at least offset+4 to read the offset field |
| 387 |
if dataLen < offset+8 { |
| 388 |
return ErrIPv6PacketTooShort |
| 389 |
} |
| 390 |
|
| 391 |
// Check if this is the first fragment |
| 392 |
fragmentOffset := binary.BigEndian.Uint16(data[offset+2:offset+4]) &^ uint16(0x7) // Remove the reserved and M flag bits |
| 393 |
if fragmentOffset != 0 { |
| 394 |
// Non-first fragment, use what we have now and stop processing |
| 395 |
fp.Protocol = data[offset] |
| 396 |
fp.Fragment = true |
| 397 |
fp.RemotePort = 0 |
| 398 |
fp.LocalPort = 0 |
| 399 |
return nil |
| 400 |
} |
| 401 |
|
| 402 |
// The next loop should be the transport layer since we are the first fragment |
| 403 |
next = 8 // Fragment headers are always 8 bytes |
| 404 |
|
| 405 |
case layers.IPProtocolAH: |
| 406 |
// Auth headers, used by IPSec, have a different meaning for header length |
| 407 |
if dataLen <= offset+1 { |
| 408 |
break |
| 409 |
} |
| 410 |
next = (int(data[offset+1]) + 2) << 2 |
| 411 |
|
| 412 |
default: |
| 413 |
// Normal ipv6 header length processing |
| 414 |
if dataLen <= offset+1 { |
| 415 |
break |
| 416 |
} |
| 417 |
next = (int(data[offset+1]) + 1) << 3 |
| 418 |
} |
| 419 |
|
| 420 |
if next <= 0 { |
| 421 |
// Safety check, each ipv6 header has to be at least 8 bytes |
| 422 |
next = 8 |
| 423 |
} |
| 424 |
|
| 425 |
protoAt = offset |
| 426 |
offset = offset + next |
| 427 |
} |
| 428 |
|
| 429 |
return ErrIPv6CouldNotFindPayload |
| 430 |
} |
| 431 |
|
| 432 |
func parseV4(data []byte, incoming bool, fp *firewall.Packet) error { |
| 433 |
// Do we at least have an ipv4 header worth of data? |
| 434 |
if len(data) < ipv4.HeaderLen { |
| 435 |
return ErrIPv4PacketTooShort |
| 436 |
} |
| 437 |
|
| 438 |
// Adjust our start position based on the advertised ip header length |
| 439 |
ihl := int(data[0]&0x0f) << 2 |
| 440 |
|
| 441 |
// Well-formed ip header length? |
| 442 |
if ihl < ipv4.HeaderLen { |
| 443 |
return ErrIPv4InvalidHeaderLength |
| 444 |
} |
| 445 |
|
| 446 |
// Check if this is the second or further fragment of a fragmented packet. |
| 447 |
flagsfrags := binary.BigEndian.Uint16(data[6:8]) |
| 448 |
fp.Fragment = (flagsfrags & 0x1FFF) != 0 |
| 449 |
|
| 450 |
// Firewall handles protocol checks |
| 451 |
fp.Protocol = data[9] |
| 452 |
|
| 453 |
// Accounting for a variable header length, do we have enough data for our src/dst tuples? |
| 454 |
minLen := ihl |
| 455 |
if !fp.Fragment { |
| 456 |
if fp.Protocol == firewall.ProtoICMP { |
| 457 |
minLen += minFwPacketLen + 2 |
| 458 |
} else { |
| 459 |
minLen += minFwPacketLen |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
if len(data) < minLen { |
| 464 |
return ErrIPv4InvalidHeaderLength |
| 465 |
} |
| 466 |
|
| 467 |
if incoming { // Firewall packets are locally oriented |
| 468 |
fp.RemoteAddr, _ = netip.AddrFromSlice(data[12:16]) |
| 469 |
fp.LocalAddr, _ = netip.AddrFromSlice(data[16:20]) |
| 470 |
} else { |
| 471 |
fp.LocalAddr, _ = netip.AddrFromSlice(data[12:16]) |
| 472 |
fp.RemoteAddr, _ = netip.AddrFromSlice(data[16:20]) |
| 473 |
} |
| 474 |
|
| 475 |
if fp.Fragment { |
| 476 |
fp.RemotePort = 0 |
| 477 |
fp.LocalPort = 0 |
| 478 |
} else if fp.Protocol == firewall.ProtoICMP { //note that orientation doesn't matter on ICMP |
| 479 |
fp.RemotePort = binary.BigEndian.Uint16(data[ihl+4 : ihl+6]) //identifier |
| 480 |
fp.LocalPort = 0 //code would be uint16(data[ihl+1]) |
| 481 |
} else if incoming { |
| 482 |
fp.RemotePort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port |
| 483 |
fp.LocalPort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port |
| 484 |
} else { |
| 485 |
fp.LocalPort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port |
| 486 |
fp.RemotePort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port |
| 487 |
} |
| 488 |
|
| 489 |
return nil |
| 490 |
} |
| 491 |
|
| 492 |
func (f *Interface) handleOutsideMessagePacket(hostinfo *HostInfo, out []byte, packet []byte, fwPacket *firewall.Packet, nb []byte, q int, localCache firewall.ConntrackCache) { |
| 493 |
err := newPacket(out, true, fwPacket) |
| 494 |
if err != nil { |
| 495 |
hostinfo.logger(f.l).Warn("Error while validating inbound packet", |
| 496 |
"error", err, |
| 497 |
"packet", out, |
| 498 |
) |
| 499 |
return |
| 500 |
} |
| 501 |
|
| 502 |
dropReason := f.firewall.Drop(*fwPacket, true, hostinfo, f.pki.GetCAPool(), localCache) |
| 503 |
if dropReason != nil { |
| 504 |
// NOTE: We give `packet` as the `out` here since we already decrypted from it and we don't need it anymore |
| 505 |
// This gives us a buffer to build the reject packet in |
| 506 |
f.rejectOutside(out, hostinfo.ConnectionState, hostinfo, nb, packet, q) |
| 507 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 508 |
hostinfo.logger(f.l).Debug("dropping inbound packet", |
| 509 |
"fwPacket", fwPacket, |
| 510 |
"reason", dropReason, |
| 511 |
) |
| 512 |
} |
| 513 |
return |
| 514 |
} |
| 515 |
|
| 516 |
_, err = f.readers[q].Write(out) |
| 517 |
if err != nil { |
| 518 |
f.l.Error("Failed to write to tun", "error", err) |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
func (f *Interface) maybeSendRecvError(endpoint netip.AddrPort, index uint32) { |
| 523 |
if f.sendRecvErrorConfig.ShouldRecvError(endpoint) { |
| 524 |
f.sendRecvError(endpoint, index) |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
func (f *Interface) sendRecvError(endpoint netip.AddrPort, index uint32) { |
| 529 |
f.messageMetrics.Tx(header.RecvError, 0, 1) |
| 530 |
|
| 531 |
b := header.Encode(make([]byte, header.Len), header.Version, header.RecvError, 0, index, 0) |
| 532 |
_ = f.outside.WriteTo(b, endpoint) |
| 533 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 534 |
f.l.Debug("Recv error sent", |
| 535 |
"index", index, |
| 536 |
"udpAddr", endpoint, |
| 537 |
) |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
func (f *Interface) handleRecvError(addr netip.AddrPort, h *header.H) { |
| 542 |
if !f.acceptRecvErrorConfig.ShouldRecvError(addr) { |
| 543 |
f.l.Debug("Recv error received, ignoring", |
| 544 |
"index", h.RemoteIndex, |
| 545 |
"udpAddr", addr, |
| 546 |
) |
| 547 |
return |
| 548 |
} |
| 549 |
|
| 550 |
if f.l.Enabled(context.Background(), slog.LevelDebug) { |
| 551 |
f.l.Debug("Recv error received", |
| 552 |
"index", h.RemoteIndex, |
| 553 |
"udpAddr", addr, |
| 554 |
) |
| 555 |
} |
| 556 |
|
| 557 |
hostinfo := f.hostMap.QueryReverseIndex(h.RemoteIndex) |
| 558 |
if hostinfo == nil { |
| 559 |
f.l.Debug("Did not find remote index in main hostmap", "remoteIndex", h.RemoteIndex) |
| 560 |
return |
| 561 |
} |
| 562 |
|
| 563 |
hr := hostinfo.GetRemote() |
| 564 |
if hr.IsValid() && hr != addr { |
| 565 |
f.l.Info("Someone spoofing recv_errors?", |
| 566 |
"addr", addr, |
| 567 |
"hostinfoRemote", hr, |
| 568 |
) |
| 569 |
return |
| 570 |
} |
| 571 |
|
| 572 |
f.closeTunnel(hostinfo) |
| 573 |
// We also delete it from pending hostmap to allow for fast reconnect. |
| 574 |
f.handshakeManager.DeleteHostInfo(hostinfo) |
| 575 |
} |