outside.go 18 KB

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