outside.go 18 KB

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