1
0

outside.go 18 KB

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