outside.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. "github.com/slackhq/nebula/udp"
  14. "golang.org/x/net/ipv4"
  15. )
  16. const (
  17. minFwPacketLen = 4
  18. )
  19. var ErrOutOfWindow = errors.New("out of window packet")
  20. 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, meta udp.RxMeta) {
  21. err := h.Parse(packet)
  22. if err != nil {
  23. // Hole punch packets are 0 or 1 byte big, so lets ignore printing those errors
  24. // TODO: record metrics for rx holepunch/punchy packets?
  25. if len(packet) > 1 {
  26. f.messageMetrics.RxInvalid(1)
  27. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  28. f.l.Debug("Error while parsing inbound packet",
  29. "from", via,
  30. "error", err,
  31. "packet", packet,
  32. )
  33. }
  34. }
  35. return
  36. }
  37. if h.Version != header.Version {
  38. f.messageMetrics.RxInvalid(1)
  39. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  40. f.l.Debug("Unexpected header version received", "from", via)
  41. }
  42. return
  43. }
  44. // Check before processing to see if this is a expected type/subtype
  45. if !h.IsValidSubType() {
  46. f.messageMetrics.RxInvalid(1)
  47. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  48. f.l.Debug("Unexpected packet received", "from", via)
  49. }
  50. return
  51. }
  52. if !via.IsRelayed {
  53. if f.myVpnNetworksTable.Contains(via.UdpAddr.Addr()) {
  54. f.messageMetrics.RxInvalid(1)
  55. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  56. f.l.Debug("Refusing to process double encrypted packet", "from", via)
  57. }
  58. return
  59. }
  60. }
  61. // don't keep Rx metrics for message type, since you can see those in the tun metrics
  62. if h.Type != header.Message {
  63. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  64. }
  65. // Unencrypted packets
  66. switch h.Type {
  67. case header.Handshake:
  68. f.handshakeManager.HandleIncoming(via, packet, h)
  69. return
  70. case header.RecvError:
  71. f.handleRecvError(via.UdpAddr, h)
  72. return
  73. }
  74. // Relay packets are special
  75. isMessageRelay := (h.Type == header.Message && h.Subtype == header.MessageRelay)
  76. var hostinfo *HostInfo
  77. if isMessageRelay {
  78. hostinfo = f.hostMap.QueryRelayIndex(h.RemoteIndex)
  79. } else {
  80. hostinfo = f.hostMap.QueryIndex(h.RemoteIndex)
  81. }
  82. // At this point we should have a valid existing tunnel, verify and send
  83. // recvError if necessary
  84. if hostinfo == nil || hostinfo.ConnectionState == nil {
  85. if !via.IsRelayed {
  86. f.maybeSendRecvError(via.UdpAddr, h.RemoteIndex)
  87. }
  88. return
  89. }
  90. // All remaining packets are encrypted
  91. ci := hostinfo.ConnectionState
  92. if !ci.window.Check(f.l, h.MessageCounter) {
  93. return
  94. }
  95. // Relay packets are special
  96. if isMessageRelay {
  97. f.handleOutsideRelayPacket(hostinfo, via, out, packet, h, fwPacket, lhf, nb, q, localCache, meta)
  98. return
  99. }
  100. out, err = f.decrypt(hostinfo, h.MessageCounter, out, packet, h, nb)
  101. if err != nil {
  102. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  103. hostinfo.logger(f.l).Debug("Failed to decrypt packet",
  104. "error", err,
  105. "from", via,
  106. "header", h,
  107. )
  108. }
  109. return
  110. }
  111. // Roam before we respond
  112. f.handleHostRoaming(hostinfo, via)
  113. f.connectionManager.In(hostinfo)
  114. switch h.Type {
  115. case header.Message:
  116. switch h.Subtype {
  117. case header.MessageNone:
  118. f.handleOutsideMessagePacket(hostinfo, out, packet, fwPacket, nb, q, localCache, meta)
  119. default:
  120. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected message subtype seen", "from", via, "header", h)
  121. return
  122. }
  123. case header.LightHouse:
  124. //TODO: assert via is not relayed
  125. lhf.HandleRequest(via.UdpAddr, hostinfo.vpnAddrs, out, f)
  126. case header.Test:
  127. switch h.Subtype {
  128. case header.TestReply:
  129. // No-op, useful for the Roaming and connectionManager side-effects above
  130. case header.TestRequest:
  131. //recycle the input packet ciphertext as our output buffer
  132. f.send(header.Test, header.TestReply, ci, hostinfo, out, nb, packet)
  133. default:
  134. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected test subtype seen", "from", via, "header", h)
  135. return
  136. }
  137. case header.CloseTunnel:
  138. hostinfo.logger(f.l).Info("Close tunnel received, tearing down.", "from", via)
  139. f.closeTunnel(hostinfo)
  140. case header.Control:
  141. f.relayManager.HandleControlMsg(hostinfo, out, f)
  142. default:
  143. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected message type seen", "from", via, "header", h)
  144. }
  145. }
  146. 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, meta udp.RxMeta) {
  147. // The entire body is sent as AD, not encrypted.
  148. // The packet consists of a 16-byte parsed Nebula header, Associated Data-protected payload, and a trailing 16-byte AEAD signature value.
  149. // 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
  150. // otherwise malformed (meaning, there is no trailing 16 byte AEAD value), then this will result in at worst a 0-length slice
  151. // which will gracefully fail in the DecryptDanger call.
  152. signedPayload := packet[:len(packet)-hostinfo.ConnectionState.dKey.Overhead()]
  153. signatureValue := packet[len(packet)-hostinfo.ConnectionState.dKey.Overhead():]
  154. var err error
  155. out, err = hostinfo.ConnectionState.dKey.DecryptDanger(out, signedPayload, signatureValue, h.MessageCounter, nb)
  156. if err != nil {
  157. return
  158. }
  159. // Advance the replay window now that the frame is authenticated
  160. if !hostinfo.ConnectionState.window.Update(f.l, h.MessageCounter) {
  161. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  162. hostinfo.logger(f.l).Debug("dropping out of window relay packet", "header", h)
  163. }
  164. return
  165. }
  166. // Successfully validated the thing. Get rid of the Relay header.
  167. signedPayload = signedPayload[header.Len:]
  168. // Pull the Roaming parts up here, and return in all call paths.
  169. f.handleHostRoaming(hostinfo, via)
  170. // Track usage of both the HostInfo and the Relay for the received & authenticated packet
  171. f.connectionManager.In(hostinfo)
  172. f.connectionManager.RelayUsed(h.RemoteIndex)
  173. relay, ok := hostinfo.relayState.QueryRelayForByIdx(h.RemoteIndex)
  174. if !ok {
  175. // The only way this happens is if hostmap has an index to the correct HostInfo, but the HostInfo is missing
  176. // its internal mapping. This should never happen.
  177. hostinfo.logger(f.l).Error("HostInfo missing remote relay index",
  178. "relayRemoteIndex", h.RemoteIndex,
  179. )
  180. return
  181. }
  182. switch relay.Type {
  183. case TerminalType:
  184. // If I am the target of this relay, process the unwrapped packet
  185. // From this recursive point, all these variables are 'burned'. We shouldn't rely on them again.
  186. via = ViaSender{
  187. UdpAddr: via.UdpAddr,
  188. relayHI: hostinfo,
  189. relay: relay,
  190. IsRelayed: true,
  191. }
  192. f.readOutsidePackets(via, out[:0], signedPayload, h, fwPacket, lhf, nb, q, localCache, meta)
  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 //todo it would potentially be nice to batch these
  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. // 2-bit IP-level ECN codepoints (lower bits of IPv4 ToS / IPv6 TC).
  453. const (
  454. ecnNotECT = 0x00
  455. ecnECT1 = 0x01
  456. ecnECT0 = 0x02
  457. ecnCE = 0x03
  458. )
  459. // applyOuterECN folds an outer CE mark from the underlay into the inner
  460. // IP header per RFC 6040 normal mode. It mutates pkt[1] in place. Other
  461. // codepoints are advisory only and leave the inner unchanged.
  462. //
  463. // Merge cases (outer × inner → action):
  464. //
  465. // outer != CE : no-op (inner is authoritative)
  466. // outer == CE, inner Not-ECT : log; cannot propagate to a non-ECN host
  467. // outer == CE, inner ECT/CE : rewrite inner ECN to CE
  468. func applyOuterECN(pkt []byte, outerECN byte, hostinfo *HostInfo, l *slog.Logger) {
  469. if outerECN&ecnCE != ecnCE || len(pkt) < 2 {
  470. return
  471. }
  472. switch pkt[0] >> 4 {
  473. case 4:
  474. switch pkt[1] & 0x03 {
  475. case ecnNotECT:
  476. if l.Enabled(context.Background(), slog.LevelDebug) {
  477. hostinfo.logger(l).Debug("RFC 6040: outer CE on inner Not-ECT, leaving inner unchanged")
  478. }
  479. case ecnCE:
  480. // Already CE.
  481. default:
  482. // Rewriting the ToS byte invalidates the IPv4 header checksum, so
  483. // patch it incrementally per RFC 1624 (HC' = ~(~HC + ~m + m')). The
  484. // ToS is the low byte of the 16-bit word at pkt[0:2]; the header
  485. // checksum lives at pkt[10:12]. A header too short to carry a
  486. // checksum can't be fixed up here, so leave it for newPacket to
  487. // reject rather than emit a mangled packet.
  488. if len(pkt) < ipv4.HeaderLen {
  489. return
  490. }
  491. m := binary.BigEndian.Uint16(pkt[0:2])
  492. pkt[1] = (pkt[1] &^ 0x03) | ecnCE
  493. mNew := binary.BigEndian.Uint16(pkt[0:2])
  494. sum := uint32(^binary.BigEndian.Uint16(pkt[10:12])) + uint32(^m) + uint32(mNew)
  495. for sum > 0xffff {
  496. sum = (sum >> 16) + (sum & 0xffff)
  497. }
  498. binary.BigEndian.PutUint16(pkt[10:12], ^uint16(sum))
  499. }
  500. case 6:
  501. switch (pkt[1] >> 4) & 0x03 {
  502. case ecnNotECT:
  503. if l.Enabled(context.Background(), slog.LevelDebug) {
  504. hostinfo.logger(l).Debug("RFC 6040: outer CE on inner Not-ECT, leaving inner unchanged")
  505. }
  506. case ecnCE:
  507. // Already CE.
  508. default:
  509. pkt[1] = (pkt[1] &^ 0x30) | (ecnCE << 4)
  510. }
  511. }
  512. }
  513. func (f *Interface) handleOutsideMessagePacket(hostinfo *HostInfo, out []byte, packet []byte, fwPacket *firewall.Packet, nb []byte, q int, localCache firewall.ConntrackCache, meta udp.RxMeta) {
  514. // RFC 6040 normal-mode combine: fold any outer CE mark stamped by the
  515. // underlay into the inner header before firewall + TUN write. Other
  516. // outer codepoints are advisory only — we keep the inner unchanged.
  517. if f.ecnEnabled.Load() {
  518. applyOuterECN(out, meta.OuterECN, hostinfo, f.l)
  519. }
  520. err := newPacket(out, true, fwPacket)
  521. if err != nil {
  522. hostinfo.logger(f.l).Warn("Error while validating inbound packet",
  523. "error", err,
  524. "packet", out,
  525. )
  526. return
  527. }
  528. dropReason := f.firewall.Drop(*fwPacket, true, hostinfo, f.pki.GetCAPool(), localCache)
  529. if dropReason != nil {
  530. // NOTE: We give `packet` as the `out` here since we already decrypted from it and we don't need it anymore
  531. // This gives us a buffer to build the reject packet in. With UDP GRO this is a single segment of a shared
  532. // recvmmsg row whose capacity runs to the end of the whole row, so cap it to its own length (cap==len) to
  533. // keep the reject builder from writing past this segment into the next, not-yet-processed coalesced segment.
  534. f.rejectOutside(out, hostinfo.ConnectionState, hostinfo, nb, packet[:len(packet):len(packet)], q)
  535. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  536. hostinfo.logger(f.l).Debug("dropping inbound packet",
  537. "fwPacket", fwPacket,
  538. "reason", dropReason,
  539. )
  540. }
  541. return
  542. }
  543. err = f.batchers[q].Commit(out)
  544. if err != nil {
  545. f.l.Error("Failed to write to tun", "error", err)
  546. }
  547. }
  548. func (f *Interface) maybeSendRecvError(endpoint netip.AddrPort, index uint32) {
  549. if f.sendRecvErrorConfig.ShouldRecvError(endpoint) {
  550. f.sendRecvError(endpoint, index)
  551. }
  552. }
  553. func (f *Interface) sendRecvError(endpoint netip.AddrPort, index uint32) {
  554. f.messageMetrics.Tx(header.RecvError, 0, 1)
  555. b := header.Encode(make([]byte, header.Len), header.Version, header.RecvError, 0, index, 0)
  556. _ = f.outside.WriteTo(b, endpoint)
  557. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  558. f.l.Debug("Recv error sent",
  559. "index", index,
  560. "udpAddr", endpoint,
  561. )
  562. }
  563. }
  564. func (f *Interface) handleRecvError(addr netip.AddrPort, h *header.H) {
  565. if !f.acceptRecvErrorConfig.ShouldRecvError(addr) {
  566. f.l.Debug("Recv error received, ignoring",
  567. "index", h.RemoteIndex,
  568. "udpAddr", addr,
  569. )
  570. return
  571. }
  572. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  573. f.l.Debug("Recv error received",
  574. "index", h.RemoteIndex,
  575. "udpAddr", addr,
  576. )
  577. }
  578. hostinfo := f.hostMap.QueryReverseIndex(h.RemoteIndex)
  579. if hostinfo == nil {
  580. f.l.Debug("Did not find remote index in main hostmap", "remoteIndex", h.RemoteIndex)
  581. return
  582. }
  583. hr := hostinfo.GetRemote()
  584. if hr.IsValid() && hr != addr {
  585. f.l.Info("Someone spoofing recv_errors?",
  586. "addr", addr,
  587. "hostinfoRemote", hr,
  588. )
  589. return
  590. }
  591. f.closeTunnel(hostinfo)
  592. // We also delete it from pending hostmap to allow for fast reconnect.
  593. f.handshakeManager.DeleteHostInfo(hostinfo)
  594. }