outside.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. package nebula
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "errors"
  6. "log/slog"
  7. "net/netip"
  8. "time"
  9. "golang.org/x/net/ipv6"
  10. "github.com/slackhq/nebula/firewall"
  11. "github.com/slackhq/nebula/header"
  12. "github.com/slackhq/nebula/iputil"
  13. "github.com/slackhq/nebula/overlay/batch"
  14. "golang.org/x/net/ipv4"
  15. )
  16. const (
  17. minFwPacketLen = 4
  18. )
  19. var ErrOutOfWindow = errors.New("out of window packet")
  20. // readOutsidePackets processes one received underlay packet.
  21. // Message payloads are decrypted IN PLACE, so packet must stay untouched
  22. // by the caller until the batcher for queue q has been flushed
  23. func (f *Interface) readOutsidePackets(via ViaSender, packet []byte, rxc *rxContext) {
  24. h := rxc.h
  25. err := h.Parse(packet)
  26. if err != nil {
  27. // Hole punch packets are 0 or 1 byte big, so lets ignore printing those errors
  28. // TODO: record metrics for rx holepunch/punchy packets?
  29. if len(packet) > 1 {
  30. f.messageMetrics.RxInvalid(1)
  31. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  32. f.l.Debug("Error while parsing inbound packet",
  33. "from", via,
  34. "error", err,
  35. "packet", packet,
  36. )
  37. }
  38. }
  39. return
  40. }
  41. if h.Version != header.Version {
  42. f.messageMetrics.RxInvalid(1)
  43. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  44. f.l.Debug("Unexpected header version received", "from", via)
  45. }
  46. return
  47. }
  48. // Check before processing to see if this is a expected type/subtype
  49. if !h.IsValidSubType() {
  50. f.messageMetrics.RxInvalid(1)
  51. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  52. f.l.Debug("Unexpected packet received", "from", via)
  53. }
  54. return
  55. }
  56. if !via.IsRelayed {
  57. if f.myVpnNetworksTable.Contains(via.UdpAddr.Addr()) {
  58. f.messageMetrics.RxInvalid(1)
  59. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  60. f.l.Debug("Refusing to process double encrypted packet", "from", via)
  61. }
  62. return
  63. }
  64. }
  65. // don't keep Rx metrics for message type, since you can see those in the tun metrics
  66. if h.Type != header.Message {
  67. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  68. }
  69. // Unencrypted packets
  70. switch h.Type {
  71. case header.Handshake:
  72. f.handshakeManager.HandleIncoming(via, packet, h)
  73. return
  74. case header.RecvError:
  75. f.handleRecvError(via.UdpAddr, h)
  76. return
  77. }
  78. // Relay packets are special
  79. isMessageRelay := (h.Type == header.Message && h.Subtype == header.MessageRelay)
  80. var hostinfo *HostInfo
  81. if isMessageRelay {
  82. hostinfo = f.hostMap.QueryRelayIndex(h.RemoteIndex)
  83. } else {
  84. hostinfo = f.hostMap.QueryIndexCached(h.RemoteIndex, rxc.hostmapCache)
  85. }
  86. // At this point we should have a valid existing tunnel, verify and send
  87. // recvError if necessary
  88. if hostinfo == nil || hostinfo.ConnectionState == nil {
  89. if !via.IsRelayed {
  90. f.maybeSendRecvError(via.UdpAddr, h.RemoteIndex)
  91. }
  92. return
  93. }
  94. if len(packet) < header.Len+hostinfo.ConnectionState.dKey.Overhead() {
  95. f.messageMetrics.RxInvalid(1)
  96. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  97. f.l.Debug("packet too small", "from", via, "length", len(packet))
  98. }
  99. return
  100. }
  101. // All remaining packets are encrypted
  102. if isMessageRelay {
  103. // Relay packets are special, this branch should always early-return
  104. err = hostinfo.ConnectionState.VerifyRelay(f.l, h.MessageCounter, packet, rxc.nb)
  105. if err != nil {
  106. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  107. hostinfo.logger(f.l).Debug("Failed to verify relay packet", "error", err, "from", via, "header", h)
  108. }
  109. return
  110. }
  111. f.handleOutsideRelayPacket(hostinfo, via, packet, rxc)
  112. return
  113. }
  114. out, err := hostinfo.ConnectionState.Decrypt(f.l, h.MessageCounter, packet, rxc.nb)
  115. if !f.finishOutsideDecrypt(hostinfo, via, h.MessageCounter, err) {
  116. return
  117. }
  118. switch h.Type {
  119. case header.Message:
  120. switch h.Subtype {
  121. case header.MessageNone:
  122. f.handleOutsideMessagePacket(hostinfo, h.MessageCounter, out, rxc)
  123. default:
  124. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected message subtype seen", "from", via, "header", h)
  125. return
  126. }
  127. case header.LightHouse:
  128. //TODO: assert via is not relayed
  129. rxc.lhh.HandleRequest(via.UdpAddr, hostinfo.vpnAddrs, out, f)
  130. case header.Test:
  131. switch h.Subtype {
  132. case header.TestReply:
  133. // No-op, useful for the Roaming and connectionManager side-effects above
  134. case header.TestRequest:
  135. if header.MaxOverhead+len(out) > len(rxc.scratch) {
  136. // A reply that cannot fit in scratch is dropped no matter the log level.
  137. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  138. hostinfo.logger(f.l).Debug("dropping oversized test request", "payloadLen", len(out), "from", via)
  139. }
  140. return
  141. }
  142. f.send(header.Test, header.TestReply, hostinfo.ConnectionState, hostinfo, out, rxc.nb, rxc.scratch[:0])
  143. default:
  144. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected test subtype seen", "from", via, "header", h)
  145. return
  146. }
  147. case header.CloseTunnel:
  148. hostinfo.logger(f.l).Info("Close tunnel received, tearing down.", "from", via)
  149. f.closeTunnel(hostinfo)
  150. case header.Control:
  151. f.relayManager.HandleControlMsg(hostinfo, out, f)
  152. default:
  153. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected message type seen", "from", via, "header", h)
  154. }
  155. }
  156. func (f *Interface) handleOutsideRelayPacket(hostinfo *HostInfo, via ViaSender, packet []byte, rxc *rxContext) {
  157. h := rxc.h
  158. // Successfully validated the thing. Get rid of the Relay header and the AEAD tag
  159. signedPayload := packet[header.Len : len(packet)-hostinfo.ConnectionState.dKey.Overhead()]
  160. // Pull the Roaming parts up here, and return in all call paths.
  161. f.handleHostRoaming(hostinfo, via)
  162. // Track usage of both the HostInfo and the Relay for the received & authenticated packet
  163. f.connectionManager.In(hostinfo)
  164. f.connectionManager.RelayUsed(h.RemoteIndex)
  165. relay, ok := hostinfo.relayState.QueryRelayForByIdx(h.RemoteIndex)
  166. if !ok {
  167. // The only way this happens is if hostmap has an index to the correct HostInfo, but the HostInfo is missing
  168. // its internal mapping. This should never happen.
  169. hostinfo.logger(f.l).Error("HostInfo missing remote relay index", "relayRemoteIndex", h.RemoteIndex)
  170. return
  171. }
  172. switch relay.Type {
  173. case TerminalType:
  174. // If I am the target of this relay, process the unwrapped packet
  175. // From this recursive point, all these variables are 'burned'. We shouldn't rely on them again.
  176. via = ViaSender{
  177. UdpAddr: via.UdpAddr,
  178. relayHI: hostinfo,
  179. relay: relay,
  180. IsRelayed: true,
  181. }
  182. f.readOutsidePackets(via, signedPayload, rxc)
  183. case ForwardingType:
  184. // Find the target HostInfo relay object
  185. targetHI, targetRelay, err := f.hostMap.QueryVpnAddrsRelayFor(hostinfo.vpnAddrs, relay.PeerAddr)
  186. if err != nil {
  187. hostinfo.logger(f.l).Info("Failed to find target host info by ip",
  188. "relayTo", relay.PeerAddr,
  189. "relayFrom", hostinfo.vpnAddrs[0],
  190. "error", err,
  191. )
  192. return
  193. }
  194. // If that relay is Established, forward the payload through it
  195. if targetRelay.State == Established {
  196. switch targetRelay.Type {
  197. case ForwardingType:
  198. // Forward this packet through the relay tunnel, rebuilding it in place.
  199. // Encode overwrites the old outer header, and the new AEAD tag lands where the old one was
  200. fwdBuf := packet[:0]
  201. //todo it would potentially be nice to batch these
  202. f.SendVia(targetHI, targetRelay, signedPayload, rxc.nb, fwdBuf, true, rxc.q)
  203. case TerminalType:
  204. hostinfo.logger(f.l).Error("Unexpected Relay Type of Terminal")
  205. return
  206. default:
  207. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  208. hostinfo.logger(f.l).Debug("Unexpected targetRelay Type", "from", via, "relayType", targetRelay.Type)
  209. }
  210. return
  211. }
  212. } else {
  213. hostinfo.logger(f.l).Info("Unexpected target relay state",
  214. "relayTo", relay.PeerAddr,
  215. "relayFrom", hostinfo.vpnAddrs[0],
  216. "targetRelayState", targetRelay.State,
  217. )
  218. return
  219. }
  220. default:
  221. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  222. hostinfo.logger(f.l).Debug("Unexpected relay type", "from", via, "relayType", relay.Type)
  223. }
  224. }
  225. }
  226. // closeTunnel closes a tunnel locally, it does not send a closeTunnel packet to the remote
  227. func (f *Interface) closeTunnel(hostInfo *HostInfo) {
  228. // Hold the hostmap lock across both the delete and the lighthouse eviction,
  229. // to avoid a race with handshake completion.
  230. f.hostMap.Lock()
  231. defer f.hostMap.Unlock()
  232. if !f.hostMap.unlockedDeleteHostInfo(hostInfo) {
  233. return
  234. }
  235. // We no longer have any tunnels with this vpn addr, clear learned lighthouse state to lower memory usage
  236. f.lightHouse.DeleteVpnAddrs(hostInfo.vpnAddrs)
  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. curRemote := hostinfo.GetRemote()
  244. if !via.IsRelayed && curRemote != via.UdpAddr {
  245. if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) {
  246. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  247. hostinfo.logger(f.l).Debug("lighthouse.remote_allow_list denied roaming", "newAddr", via.UdpAddr)
  248. }
  249. return
  250. }
  251. if !hostinfo.lastRoam.IsZero() && via.UdpAddr == hostinfo.lastRoamRemote && time.Since(hostinfo.lastRoam) < RoamingSuppressSeconds*time.Second {
  252. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  253. hostinfo.logger(f.l).Debug("Suppressing roam back to previous remote",
  254. "suppressSeconds", RoamingSuppressSeconds,
  255. "udpAddr", curRemote,
  256. "newAddr", via.UdpAddr,
  257. )
  258. }
  259. return
  260. }
  261. hostinfo.logger(f.l).Info("Host roamed to new udp ip/port.",
  262. "udpAddr", curRemote,
  263. "newAddr", via.UdpAddr,
  264. )
  265. hostinfo.lastRoam = time.Now()
  266. hostinfo.lastRoamRemote = curRemote
  267. hostinfo.SetRemote(via.UdpAddr)
  268. }
  269. }
  270. var (
  271. ErrPacketTooShort = errors.New("packet is too short")
  272. ErrUnknownIPVersion = errors.New("packet is an unknown ip version")
  273. ErrIPv4InvalidHeaderLength = errors.New("invalid ipv4 header length")
  274. ErrIPv4PacketTooShort = errors.New("ipv4 packet is too short")
  275. ErrIPv6PacketTooShort = errors.New("ipv6 packet is too short")
  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.ParsedPacket) error {
  279. // fp is reused across packets; reset the parse byproducts so an early-error return cannot
  280. // leak the previous packet's offsets.
  281. fp.IPHdrLen = 0
  282. fp.FragAny = false
  283. if len(data) < 1 {
  284. return ErrPacketTooShort
  285. }
  286. version := int((data[0] >> 4) & 0x0f)
  287. switch version {
  288. case ipv4.Version:
  289. return parseV4(data, incoming, fp)
  290. case ipv6.Version:
  291. return parseV6(data, incoming, fp)
  292. }
  293. return ErrUnknownIPVersion
  294. }
  295. func parseV6(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
  296. dataLen := len(data)
  297. if dataLen < ipv6.HeaderLen {
  298. return ErrIPv6PacketTooShort
  299. }
  300. if incoming {
  301. fp.RemoteAddr, _ = netip.AddrFromSlice(data[8:24])
  302. fp.LocalAddr, _ = netip.AddrFromSlice(data[24:40])
  303. } else {
  304. fp.LocalAddr, _ = netip.AddrFromSlice(data[8:24])
  305. fp.RemoteAddr, _ = netip.AddrFromSlice(data[24:40])
  306. }
  307. // Walk the extension header chain to the upper layer protocol. iputil.IPv6FindUpperProtocol is the single
  308. // source of truth for which headers are extension headers, so this stays in lockstep with the reject path
  309. // and cannot drift into misreading an unknown protocol (SCTP, GRE, etc.) as a forged transport.
  310. proto, offset, isFragment, anyFragment, err := iputil.IPv6FindUpperProtocol(data)
  311. if err != nil {
  312. return err
  313. }
  314. fp.Protocol = proto
  315. fp.Fragment = isFragment
  316. fp.FragAny = anyFragment
  317. fp.IPHdrLen = offset
  318. if isFragment {
  319. // Non-first fragments carry no transport header, so we have no ports to read
  320. fp.RemotePort = 0
  321. fp.LocalPort = 0
  322. return nil
  323. }
  324. switch proto {
  325. case iputil.IPProtocolICMPv6:
  326. // An ICMPv6 message is at least type, code and checksum, 4 bytes. Only echo carries more than we read.
  327. if dataLen < offset+4 {
  328. return ErrIPv6PacketTooShort
  329. }
  330. fp.LocalPort = 0 //incoming vs outgoing doesn't matter for icmpv6
  331. switch data[offset] { //icmp type
  332. case iputil.ICMPv6TypeEchoRequest, iputil.ICMPv6TypeEchoReply:
  333. if dataLen < offset+6 {
  334. return ErrIPv6PacketTooShort
  335. }
  336. fp.RemotePort = binary.BigEndian.Uint16(data[offset+4 : offset+6]) //identifier
  337. default:
  338. fp.RemotePort = 0
  339. }
  340. case iputil.IPProtocolTCP, iputil.IPProtocolUDP:
  341. if dataLen < offset+4 {
  342. return ErrIPv6PacketTooShort
  343. }
  344. if incoming {
  345. fp.RemotePort = binary.BigEndian.Uint16(data[offset : offset+2])
  346. fp.LocalPort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
  347. } else {
  348. fp.LocalPort = binary.BigEndian.Uint16(data[offset : offset+2])
  349. fp.RemotePort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
  350. }
  351. default:
  352. // don't set ports for protocols Nebula doesn't inspect
  353. fp.RemotePort = 0
  354. fp.LocalPort = 0
  355. }
  356. return nil
  357. }
  358. func parseV4(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
  359. // Do we at least have an ipv4 header worth of data?
  360. if len(data) < ipv4.HeaderLen {
  361. return ErrIPv4PacketTooShort
  362. }
  363. // Adjust our start position based on the advertised ip header length
  364. ihl := int(data[0]&0x0f) << 2
  365. // Well-formed ip header length?
  366. if ihl < ipv4.HeaderLen {
  367. return ErrIPv4InvalidHeaderLength
  368. }
  369. // Check if this is the second or further fragment of a fragmented packet.
  370. flagsfrags := binary.BigEndian.Uint16(data[6:8])
  371. fp.Fragment = (flagsfrags & 0x1FFF) != 0
  372. // Any fragmentation at all (MF or offset): first fragments have readable ports for the
  373. // firewall but must never be coalesced.
  374. fp.FragAny = (flagsfrags & 0x3fff) != 0
  375. fp.IPHdrLen = ihl
  376. // Firewall handles protocol checks
  377. fp.Protocol = data[9]
  378. // Accounting for a variable header length, do we have enough data for our src/dst tuples?
  379. minLen := ihl
  380. if !fp.Fragment {
  381. if fp.Protocol == iputil.IPProtocolICMP {
  382. minLen += minFwPacketLen + 2
  383. } else {
  384. minLen += minFwPacketLen
  385. }
  386. }
  387. if len(data) < minLen {
  388. return ErrIPv4InvalidHeaderLength
  389. }
  390. if incoming { // Firewall packets are locally oriented
  391. fp.RemoteAddr, _ = netip.AddrFromSlice(data[12:16])
  392. fp.LocalAddr, _ = netip.AddrFromSlice(data[16:20])
  393. } else {
  394. fp.LocalAddr, _ = netip.AddrFromSlice(data[12:16])
  395. fp.RemoteAddr, _ = netip.AddrFromSlice(data[16:20])
  396. }
  397. if fp.Fragment {
  398. fp.RemotePort = 0
  399. fp.LocalPort = 0
  400. } else if fp.Protocol == iputil.IPProtocolICMP { //note that orientation doesn't matter on ICMP
  401. fp.RemotePort = binary.BigEndian.Uint16(data[ihl+4 : ihl+6]) //identifier
  402. fp.LocalPort = 0 //code would be uint16(data[ihl+1])
  403. } else if incoming {
  404. fp.RemotePort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port
  405. fp.LocalPort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port
  406. } else {
  407. fp.LocalPort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port
  408. fp.RemotePort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port
  409. }
  410. return nil
  411. }
  412. // finishOutsideDecrypt logs a failed decrypt, or roams and records the traffic for an authenticated packet before it
  413. // is handled. It reports whether the packet authenticated.
  414. func (f *Interface) finishOutsideDecrypt(hostinfo *HostInfo, via ViaSender, counter uint64, err error) bool {
  415. if err != nil {
  416. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  417. hostinfo.logger(f.l).Debug("Failed to decrypt packet", "error", err, "from", via, "counter", counter)
  418. }
  419. return false
  420. }
  421. // Roam before we respond
  422. f.handleHostRoaming(hostinfo, via)
  423. f.connectionManager.In(hostinfo)
  424. return true
  425. }
  426. // flushOutside ends a receive batch: it writes out the tun coalescer and drops the batch's hostmap cache.
  427. func (f *Interface) flushOutside(rxc *rxContext) {
  428. if err := f.batchers[rxc.q].Flush(); err != nil {
  429. f.l.Error("Failed to flush tun coalescer", "error", err)
  430. }
  431. clear(rxc.hostmapCache)
  432. }
  433. func (f *Interface) handleOutsideMessagePacket(hostinfo *HostInfo, messageCounter uint64, out []byte, rxc *rxContext) {
  434. err := newPacket(out, true, rxc.fwPacket)
  435. if err != nil {
  436. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  437. hostinfo.logger(f.l).Debug("Error while validating inbound packet", "error", err, "packet", out)
  438. return
  439. }
  440. }
  441. dropReason := f.firewall.Drop(rxc.fwPacket.Packet, true, hostinfo, f.pki.GetCAPool(), rxc.ctCache.Get())
  442. if dropReason != nil {
  443. f.rejectOutside(out, hostinfo.ConnectionState, hostinfo, rxc.nb, rxc.scratch, rxc.q)
  444. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  445. hostinfo.logger(f.l).Debug("dropping inbound packet", "fwPacket", rxc.fwPacket, "reason", dropReason)
  446. }
  447. return
  448. }
  449. err = f.batchers[rxc.q].Commit(out, batch.SortKey{Epoch: hostinfo.ConnectionState.epoch, Counter: messageCounter}, rxc.fwPacket)
  450. if err != nil {
  451. f.l.Error("Failed to write to tun", "error", err)
  452. }
  453. }
  454. func (f *Interface) maybeSendRecvError(endpoint netip.AddrPort, index uint32) {
  455. if f.sendRecvErrorConfig.ShouldRecvError(endpoint) {
  456. f.sendRecvError(endpoint, index)
  457. }
  458. }
  459. func (f *Interface) sendRecvError(endpoint netip.AddrPort, index uint32) {
  460. f.messageMetrics.Tx(header.RecvError, 0, 1)
  461. b := header.Encode(make([]byte, header.Len), header.Version, header.RecvError, 0, index, 0)
  462. _ = f.outside.WriteTo(b, endpoint)
  463. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  464. f.l.Debug("Recv error sent",
  465. "index", index,
  466. "udpAddr", endpoint,
  467. )
  468. }
  469. }
  470. func (f *Interface) handleRecvError(addr netip.AddrPort, h *header.H) {
  471. if !f.acceptRecvErrorConfig.ShouldRecvError(addr) {
  472. f.l.Debug("Recv error received, ignoring",
  473. "index", h.RemoteIndex,
  474. "udpAddr", addr,
  475. )
  476. return
  477. }
  478. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  479. f.l.Debug("Recv error received",
  480. "index", h.RemoteIndex,
  481. "udpAddr", addr,
  482. )
  483. }
  484. hostinfo := f.hostMap.QueryReverseIndex(h.RemoteIndex)
  485. if hostinfo == nil {
  486. f.l.Debug("Did not find remote index in main hostmap", "remoteIndex", h.RemoteIndex)
  487. return
  488. }
  489. hr := hostinfo.GetRemote()
  490. if hr.IsValid() && hr != addr {
  491. f.l.Info("Someone spoofing recv_errors?",
  492. "addr", addr,
  493. "hostinfoRemote", hr,
  494. )
  495. return
  496. }
  497. f.closeTunnel(hostinfo)
  498. // We also delete it from pending hostmap to allow for fast reconnect.
  499. f.handshakeManager.DeleteHostInfo(hostinfo)
  500. }