outside.go 18 KB

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