outside.go 18 KB

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