outside.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. // readOutsidePackets processes one received underlay packet.
  21. // Message payloads are decrypted IN PLACE, so packet must stay untouched
  22. // by the caller until the batcher for queue q has been flushed
  23. func (f *Interface) readOutsidePackets(via ViaSender, scratch []byte, packet []byte, h *header.H, fwPacket *firewall.Packet, lhf *LightHouseHandler, nb []byte, q int, localCache firewall.ConntrackCache, meta udp.RxMeta) {
  24. err := h.Parse(packet)
  25. if err != nil {
  26. // Hole punch packets are 0 or 1 byte big, so lets ignore printing those errors
  27. // TODO: record metrics for rx holepunch/punchy packets?
  28. if len(packet) > 1 {
  29. f.messageMetrics.RxInvalid(1)
  30. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  31. f.l.Debug("Error while parsing inbound packet",
  32. "from", via,
  33. "error", err,
  34. "packet", packet,
  35. )
  36. }
  37. }
  38. return
  39. }
  40. if h.Version != header.Version {
  41. f.messageMetrics.RxInvalid(1)
  42. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  43. f.l.Debug("Unexpected header version received", "from", via)
  44. }
  45. return
  46. }
  47. // Check before processing to see if this is a expected type/subtype
  48. if !h.IsValidSubType() {
  49. f.messageMetrics.RxInvalid(1)
  50. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  51. f.l.Debug("Unexpected packet received", "from", via)
  52. }
  53. return
  54. }
  55. if !via.IsRelayed {
  56. if f.myVpnNetworksTable.Contains(via.UdpAddr.Addr()) {
  57. f.messageMetrics.RxInvalid(1)
  58. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  59. f.l.Debug("Refusing to process double encrypted packet", "from", via)
  60. }
  61. return
  62. }
  63. }
  64. // don't keep Rx metrics for message type, since you can see those in the tun metrics
  65. if h.Type != header.Message {
  66. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  67. }
  68. // Unencrypted packets
  69. switch h.Type {
  70. case header.Handshake:
  71. f.handshakeManager.HandleIncoming(via, packet, h)
  72. return
  73. case header.RecvError:
  74. f.handleRecvError(via.UdpAddr, h)
  75. return
  76. }
  77. // Relay packets are special
  78. isMessageRelay := (h.Type == header.Message && h.Subtype == header.MessageRelay)
  79. var hostinfo *HostInfo
  80. if isMessageRelay {
  81. hostinfo = f.hostMap.QueryRelayIndex(h.RemoteIndex)
  82. } else {
  83. hostinfo = f.hostMap.QueryIndex(h.RemoteIndex)
  84. }
  85. // At this point we should have a valid existing tunnel, verify and send
  86. // recvError if necessary
  87. if hostinfo == nil || hostinfo.ConnectionState == nil {
  88. if !via.IsRelayed {
  89. f.maybeSendRecvError(via.UdpAddr, h.RemoteIndex)
  90. }
  91. return
  92. }
  93. // All remaining packets are encrypted
  94. if isMessageRelay {
  95. // Relay packets are special, this branch should always early-return
  96. err = hostinfo.ConnectionState.VerifyRelay(f.l, h.MessageCounter, packet, nb)
  97. if err != nil {
  98. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  99. hostinfo.logger(f.l).Debug("Failed to verify relay packet", "error", err, "from", via, "header", h)
  100. }
  101. return
  102. }
  103. f.handleOutsideRelayPacket(hostinfo, via, scratch, packet, h, fwPacket, lhf, nb, q, localCache, meta)
  104. return
  105. }
  106. out, err := hostinfo.ConnectionState.Decrypt(f.l, h.MessageCounter, packet, nb)
  107. if err != nil {
  108. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  109. hostinfo.logger(f.l).Debug("Failed to decrypt packet", "error", err, "from", via, "header", h)
  110. }
  111. return
  112. }
  113. // Roam before we respond
  114. f.handleHostRoaming(hostinfo, via)
  115. f.connectionManager.In(hostinfo)
  116. switch h.Type {
  117. case header.Message:
  118. switch h.Subtype {
  119. case header.MessageNone:
  120. f.handleOutsideMessagePacket(hostinfo, out, scratch, fwPacket, nb, q, localCache, meta)
  121. default:
  122. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected message subtype seen", "from", via, "header", h)
  123. return
  124. }
  125. case header.LightHouse:
  126. //TODO: assert via is not relayed
  127. lhf.HandleRequest(via.UdpAddr, hostinfo.vpnAddrs, out, f)
  128. case header.Test:
  129. switch h.Subtype {
  130. case header.TestReply:
  131. // No-op, useful for the Roaming and connectionManager side-effects above
  132. case header.TestRequest:
  133. const maxCipherOverhead = 16 //todo we use this too often, needs a real importable const
  134. const maxOverhead = header.Len + header.Len + maxCipherOverhead + maxCipherOverhead
  135. if maxOverhead+len(out) <= len(scratch) {
  136. f.send(header.Test, header.TestReply, hostinfo.ConnectionState, hostinfo, out, nb, scratch[:0])
  137. return
  138. } else if f.l.Enabled(context.Background(), slog.LevelDebug) {
  139. hostinfo.logger(f.l).Debug("dropping oversized test request", "payloadLen", len(out), "from", via)
  140. return
  141. }
  142. default:
  143. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected test subtype seen", "from", via, "header", h)
  144. return
  145. }
  146. case header.CloseTunnel:
  147. hostinfo.logger(f.l).Info("Close tunnel received, tearing down.", "from", via)
  148. f.closeTunnel(hostinfo)
  149. case header.Control:
  150. f.relayManager.HandleControlMsg(hostinfo, out, f)
  151. default:
  152. hostinfo.logger(f.l).Error("IsValidSubType was true, but unexpected message type seen", "from", via, "header", h)
  153. }
  154. }
  155. func (f *Interface) handleOutsideRelayPacket(hostinfo *HostInfo, via ViaSender, scratch []byte, packet []byte, h *header.H, fwPacket *firewall.Packet, lhf *LightHouseHandler, nb []byte, q int, localCache firewall.ConntrackCache, meta udp.RxMeta) {
  156. // Successfully validated the thing. Get rid of the Relay header and the AEAD tag
  157. signedPayload := packet[header.Len : len(packet)-hostinfo.ConnectionState.dKey.Overhead()]
  158. // Pull the Roaming parts up here, and return in all call paths.
  159. f.handleHostRoaming(hostinfo, via)
  160. // Track usage of both the HostInfo and the Relay for the received & authenticated packet
  161. f.connectionManager.In(hostinfo)
  162. f.connectionManager.RelayUsed(h.RemoteIndex)
  163. relay, ok := hostinfo.relayState.QueryRelayForByIdx(h.RemoteIndex)
  164. if !ok {
  165. // The only way this happens is if hostmap has an index to the correct HostInfo, but the HostInfo is missing
  166. // its internal mapping. This should never happen.
  167. hostinfo.logger(f.l).Error("HostInfo missing remote relay index", "relayRemoteIndex", h.RemoteIndex)
  168. return
  169. }
  170. switch relay.Type {
  171. case TerminalType:
  172. // If I am the target of this relay, process the unwrapped packet
  173. // From this recursive point, all these variables are 'burned'. We shouldn't rely on them again.
  174. via = ViaSender{
  175. UdpAddr: via.UdpAddr,
  176. relayHI: hostinfo,
  177. relay: relay,
  178. IsRelayed: true,
  179. }
  180. f.readOutsidePackets(via, scratch, signedPayload, h, fwPacket, lhf, nb, q, localCache, meta)
  181. case ForwardingType:
  182. // Find the target HostInfo relay object
  183. targetHI, targetRelay, err := f.hostMap.QueryVpnAddrsRelayFor(hostinfo.vpnAddrs, relay.PeerAddr)
  184. if err != nil {
  185. hostinfo.logger(f.l).Info("Failed to find target host info by ip",
  186. "relayTo", relay.PeerAddr,
  187. "relayFrom", hostinfo.vpnAddrs[0],
  188. "error", err,
  189. )
  190. return
  191. }
  192. // If that relay is Established, forward the payload through it
  193. if targetRelay.State == Established {
  194. switch targetRelay.Type {
  195. case ForwardingType:
  196. // Forward this packet through the relay tunnel, rebuilding it in place.
  197. // Encode overwrites the old outer header, and the new AEAD tag lands where the old one was
  198. fwdBuf := packet[:0:len(packet)] // Cap to len(packet) to protect memory from a larger parent buffer
  199. //todo it would potentially be nice to batch these
  200. f.SendVia(targetHI, targetRelay, signedPayload, nb, fwdBuf, true)
  201. case TerminalType:
  202. hostinfo.logger(f.l).Error("Unexpected Relay Type of Terminal")
  203. return
  204. default:
  205. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  206. hostinfo.logger(f.l).Debug("Unexpected targetRelay Type", "from", via, "relayType", targetRelay.Type)
  207. }
  208. return
  209. }
  210. } else {
  211. hostinfo.logger(f.l).Info("Unexpected target relay state",
  212. "relayTo", relay.PeerAddr,
  213. "relayFrom", hostinfo.vpnAddrs[0],
  214. "targetRelayState", targetRelay.State,
  215. )
  216. return
  217. }
  218. default:
  219. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  220. hostinfo.logger(f.l).Debug("Unexpected relay type", "from", via, "relayType", relay.Type)
  221. }
  222. }
  223. }
  224. // closeTunnel closes a tunnel locally, it does not send a closeTunnel packet to the remote
  225. func (f *Interface) closeTunnel(hostInfo *HostInfo) {
  226. final := f.hostMap.DeleteHostInfo(hostInfo)
  227. if final {
  228. // We no longer have any tunnels with this vpn addr, clear learned lighthouse state to lower memory usage
  229. f.lightHouse.DeleteVpnAddrs(hostInfo.vpnAddrs)
  230. }
  231. }
  232. // sendCloseTunnel is a helper function to send a proper close tunnel packet to a remote
  233. func (f *Interface) sendCloseTunnel(h *HostInfo) {
  234. f.send(header.CloseTunnel, 0, h.ConnectionState, h, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
  235. }
  236. func (f *Interface) handleHostRoaming(hostinfo *HostInfo, via ViaSender) {
  237. curRemote := hostinfo.GetRemote()
  238. if !via.IsRelayed && curRemote != via.UdpAddr {
  239. if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) {
  240. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  241. hostinfo.logger(f.l).Debug("lighthouse.remote_allow_list denied roaming", "newAddr", via.UdpAddr)
  242. }
  243. return
  244. }
  245. if !hostinfo.lastRoam.IsZero() && via.UdpAddr == hostinfo.lastRoamRemote && time.Since(hostinfo.lastRoam) < RoamingSuppressSeconds*time.Second {
  246. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  247. hostinfo.logger(f.l).Debug("Suppressing roam back to previous remote",
  248. "suppressSeconds", RoamingSuppressSeconds,
  249. "udpAddr", curRemote,
  250. "newAddr", via.UdpAddr,
  251. )
  252. }
  253. return
  254. }
  255. hostinfo.logger(f.l).Info("Host roamed to new udp ip/port.",
  256. "udpAddr", curRemote,
  257. "newAddr", via.UdpAddr,
  258. )
  259. hostinfo.lastRoam = time.Now()
  260. hostinfo.lastRoamRemote = curRemote
  261. hostinfo.SetRemote(via.UdpAddr)
  262. }
  263. }
  264. var (
  265. ErrPacketTooShort = errors.New("packet is too short")
  266. ErrUnknownIPVersion = errors.New("packet is an unknown ip version")
  267. ErrIPv4InvalidHeaderLength = errors.New("invalid ipv4 header length")
  268. ErrIPv4PacketTooShort = errors.New("ipv4 packet is too short")
  269. ErrIPv6PacketTooShort = errors.New("ipv6 packet is too short")
  270. ErrIPv6CouldNotFindPayload = errors.New("could not find payload in ipv6 packet")
  271. )
  272. // newPacket validates and parses the interesting bits for the firewall out of the ip and sub protocol headers
  273. func newPacket(data []byte, incoming bool, fp *firewall.Packet) error {
  274. if len(data) < 1 {
  275. return ErrPacketTooShort
  276. }
  277. version := int((data[0] >> 4) & 0x0f)
  278. switch version {
  279. case ipv4.Version:
  280. return parseV4(data, incoming, fp)
  281. case ipv6.Version:
  282. return parseV6(data, incoming, fp)
  283. }
  284. return ErrUnknownIPVersion
  285. }
  286. func parseV6(data []byte, incoming bool, fp *firewall.Packet) error {
  287. dataLen := len(data)
  288. if dataLen < ipv6.HeaderLen {
  289. return ErrIPv6PacketTooShort
  290. }
  291. if incoming {
  292. fp.RemoteAddr, _ = netip.AddrFromSlice(data[8:24])
  293. fp.LocalAddr, _ = netip.AddrFromSlice(data[24:40])
  294. } else {
  295. fp.LocalAddr, _ = netip.AddrFromSlice(data[8:24])
  296. fp.RemoteAddr, _ = netip.AddrFromSlice(data[24:40])
  297. }
  298. protoAt := 6 // NextHeader is at 6 bytes into the ipv6 header
  299. offset := ipv6.HeaderLen // Start at the end of the ipv6 header
  300. next := 0
  301. for {
  302. if protoAt >= dataLen {
  303. break
  304. }
  305. proto := layers.IPProtocol(data[protoAt])
  306. switch proto {
  307. case layers.IPProtocolESP, layers.IPProtocolNoNextHeader:
  308. fp.Protocol = uint8(proto)
  309. fp.RemotePort = 0
  310. fp.LocalPort = 0
  311. fp.Fragment = false
  312. return nil
  313. case layers.IPProtocolICMPv6:
  314. if dataLen < offset+6 {
  315. return ErrIPv6PacketTooShort
  316. }
  317. fp.Protocol = uint8(proto)
  318. fp.LocalPort = 0 //incoming vs outgoing doesn't matter for icmpv6
  319. icmptype := data[offset+1]
  320. switch icmptype {
  321. case layers.ICMPv6TypeEchoRequest, layers.ICMPv6TypeEchoReply:
  322. fp.RemotePort = binary.BigEndian.Uint16(data[offset+4 : offset+6]) //identifier
  323. default:
  324. fp.RemotePort = 0
  325. }
  326. fp.Fragment = false
  327. return nil
  328. case layers.IPProtocolTCP, layers.IPProtocolUDP:
  329. if dataLen < offset+4 {
  330. return ErrIPv6PacketTooShort
  331. }
  332. fp.Protocol = uint8(proto)
  333. if incoming {
  334. fp.RemotePort = binary.BigEndian.Uint16(data[offset : offset+2])
  335. fp.LocalPort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
  336. } else {
  337. fp.LocalPort = binary.BigEndian.Uint16(data[offset : offset+2])
  338. fp.RemotePort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
  339. }
  340. fp.Fragment = false
  341. return nil
  342. case layers.IPProtocolIPv6Fragment:
  343. // Fragment header is 8 bytes, need at least offset+4 to read the offset field
  344. if dataLen < offset+8 {
  345. return ErrIPv6PacketTooShort
  346. }
  347. // Check if this is the first fragment
  348. fragmentOffset := binary.BigEndian.Uint16(data[offset+2:offset+4]) &^ uint16(0x7) // Remove the reserved and M flag bits
  349. if fragmentOffset != 0 {
  350. // Non-first fragment, use what we have now and stop processing
  351. fp.Protocol = data[offset]
  352. fp.Fragment = true
  353. fp.RemotePort = 0
  354. fp.LocalPort = 0
  355. return nil
  356. }
  357. // The next loop should be the transport layer since we are the first fragment
  358. next = 8 // Fragment headers are always 8 bytes
  359. case layers.IPProtocolAH:
  360. // Auth headers, used by IPSec, have a different meaning for header length
  361. if dataLen <= offset+1 {
  362. break
  363. }
  364. next = (int(data[offset+1]) + 2) << 2
  365. default:
  366. // Normal ipv6 header length processing
  367. if dataLen <= offset+1 {
  368. break
  369. }
  370. next = (int(data[offset+1]) + 1) << 3
  371. }
  372. if next <= 0 {
  373. // Safety check, each ipv6 header has to be at least 8 bytes
  374. next = 8
  375. }
  376. protoAt = offset
  377. offset = offset + next
  378. }
  379. return ErrIPv6CouldNotFindPayload
  380. }
  381. func parseV4(data []byte, incoming bool, fp *firewall.Packet) error {
  382. // Do we at least have an ipv4 header worth of data?
  383. if len(data) < ipv4.HeaderLen {
  384. return ErrIPv4PacketTooShort
  385. }
  386. // Adjust our start position based on the advertised ip header length
  387. ihl := int(data[0]&0x0f) << 2
  388. // Well-formed ip header length?
  389. if ihl < ipv4.HeaderLen {
  390. return ErrIPv4InvalidHeaderLength
  391. }
  392. // Check if this is the second or further fragment of a fragmented packet.
  393. flagsfrags := binary.BigEndian.Uint16(data[6:8])
  394. fp.Fragment = (flagsfrags & 0x1FFF) != 0
  395. // Firewall handles protocol checks
  396. fp.Protocol = data[9]
  397. // Accounting for a variable header length, do we have enough data for our src/dst tuples?
  398. minLen := ihl
  399. if !fp.Fragment {
  400. if fp.Protocol == firewall.ProtoICMP {
  401. minLen += minFwPacketLen + 2
  402. } else {
  403. minLen += minFwPacketLen
  404. }
  405. }
  406. if len(data) < minLen {
  407. return ErrIPv4InvalidHeaderLength
  408. }
  409. if incoming { // Firewall packets are locally oriented
  410. fp.RemoteAddr, _ = netip.AddrFromSlice(data[12:16])
  411. fp.LocalAddr, _ = netip.AddrFromSlice(data[16:20])
  412. } else {
  413. fp.LocalAddr, _ = netip.AddrFromSlice(data[12:16])
  414. fp.RemoteAddr, _ = netip.AddrFromSlice(data[16:20])
  415. }
  416. if fp.Fragment {
  417. fp.RemotePort = 0
  418. fp.LocalPort = 0
  419. } else if fp.Protocol == firewall.ProtoICMP { //note that orientation doesn't matter on ICMP
  420. fp.RemotePort = binary.BigEndian.Uint16(data[ihl+4 : ihl+6]) //identifier
  421. fp.LocalPort = 0 //code would be uint16(data[ihl+1])
  422. } else if incoming {
  423. fp.RemotePort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port
  424. fp.LocalPort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port
  425. } else {
  426. fp.LocalPort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port
  427. fp.RemotePort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port
  428. }
  429. return nil
  430. }
  431. // decrypt authenticates and decrypts a Message packet IN PLACE: the dst is
  432. // packet[header.Len:header.Len], the exact-alias append pattern
  433. // (ciphertext[:0]) that crypto/cipher.AEAD.Open documents, so the plaintext
  434. // lands where the ciphertext sat and no separate plaintext buffer exists.
  435. // On a failed auth the AEAD zeroes the would-be plaintext region (both
  436. // AES-GCM and ChaCha20-Poly1305 do) but never writes outside it, so the
  437. // other segments of a shared GRO receive row stay intact; nothing reads a
  438. // packet after its decrypt fails. The returned slice aliases packet.
  439. func (f *Interface) decrypt(hostinfo *HostInfo, mc uint64, packet []byte, nb []byte) ([]byte, error) {
  440. out, err := hostinfo.ConnectionState.dKey.DecryptDanger(packet[header.Len:header.Len], packet[:header.Len], packet[header.Len:], mc, nb)
  441. if err != nil {
  442. return nil, err
  443. }
  444. if !hostinfo.ConnectionState.window.Update(f.l, mc) {
  445. return nil, ErrOutOfWindow
  446. }
  447. return out, nil
  448. }
  449. // 2-bit IP-level ECN codepoints (lower bits of IPv4 ToS / IPv6 TC).
  450. const (
  451. ecnNotECT = 0x00
  452. ecnECT1 = 0x01
  453. ecnECT0 = 0x02
  454. ecnCE = 0x03
  455. )
  456. // applyOuterECN folds an outer CE mark from the underlay into the inner
  457. // IP header per RFC 6040 normal mode. It mutates pkt[1] in place. Other
  458. // codepoints are advisory only and leave the inner unchanged.
  459. //
  460. // Merge cases (outer × inner → action):
  461. //
  462. // outer != CE : no-op (inner is authoritative)
  463. // outer == CE, inner Not-ECT : log; cannot propagate to a non-ECN host
  464. // outer == CE, inner ECT/CE : rewrite inner ECN to CE
  465. func applyOuterECN(pkt []byte, outerECN byte, hostinfo *HostInfo, l *slog.Logger) {
  466. if outerECN&ecnCE != ecnCE || len(pkt) < 2 {
  467. return
  468. }
  469. switch pkt[0] >> 4 {
  470. case 4:
  471. switch pkt[1] & 0x03 {
  472. case ecnNotECT:
  473. if l.Enabled(context.Background(), slog.LevelDebug) {
  474. hostinfo.logger(l).Debug("RFC 6040: outer CE on inner Not-ECT, leaving inner unchanged")
  475. }
  476. case ecnCE:
  477. // Already CE.
  478. default:
  479. // Rewriting the ToS byte invalidates the IPv4 header checksum, so
  480. // patch it incrementally per RFC 1624 (HC' = ~(~HC + ~m + m')). The
  481. // ToS is the low byte of the 16-bit word at pkt[0:2]; the header
  482. // checksum lives at pkt[10:12]. A header too short to carry a
  483. // checksum can't be fixed up here, so leave it for newPacket to
  484. // reject rather than emit a mangled packet.
  485. if len(pkt) < ipv4.HeaderLen {
  486. return
  487. }
  488. m := binary.BigEndian.Uint16(pkt[0:2])
  489. pkt[1] = (pkt[1] &^ 0x03) | ecnCE
  490. mNew := binary.BigEndian.Uint16(pkt[0:2])
  491. sum := uint32(^binary.BigEndian.Uint16(pkt[10:12])) + uint32(^m) + uint32(mNew)
  492. for sum > 0xffff {
  493. sum = (sum >> 16) + (sum & 0xffff)
  494. }
  495. binary.BigEndian.PutUint16(pkt[10:12], ^uint16(sum))
  496. }
  497. case 6:
  498. switch (pkt[1] >> 4) & 0x03 {
  499. case ecnNotECT:
  500. if l.Enabled(context.Background(), slog.LevelDebug) {
  501. hostinfo.logger(l).Debug("RFC 6040: outer CE on inner Not-ECT, leaving inner unchanged")
  502. }
  503. case ecnCE:
  504. // Already CE.
  505. default:
  506. pkt[1] = (pkt[1] &^ 0x30) | (ecnCE << 4)
  507. }
  508. }
  509. }
  510. func (f *Interface) handleOutsideMessagePacket(hostinfo *HostInfo, out []byte, scratch []byte, fwPacket *firewall.Packet, nb []byte, q int, localCache firewall.ConntrackCache, meta udp.RxMeta) {
  511. // RFC 6040 normal-mode combine: fold any outer CE mark stamped by the
  512. // underlay into the inner header before firewall + TUN write. Other
  513. // outer codepoints are advisory only — we keep the inner unchanged.
  514. if f.ecnEnabled.Load() {
  515. applyOuterECN(out, meta.OuterECN, hostinfo, f.l)
  516. }
  517. err := newPacket(out, true, fwPacket)
  518. if err != nil {
  519. hostinfo.logger(f.l).Warn("Error while validating inbound packet",
  520. "error", err,
  521. "packet", out,
  522. )
  523. return
  524. }
  525. dropReason := f.firewall.Drop(*fwPacket, true, hostinfo, f.pki.GetCAPool(), localCache)
  526. if dropReason != nil {
  527. f.rejectOutside(out, hostinfo.ConnectionState, hostinfo, nb, scratch, q)
  528. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  529. hostinfo.logger(f.l).Debug("dropping inbound packet",
  530. "fwPacket", fwPacket,
  531. "reason", dropReason,
  532. )
  533. }
  534. return
  535. }
  536. err = f.batchers[q].Commit(out)
  537. if err != nil {
  538. f.l.Error("Failed to write to tun", "error", err)
  539. }
  540. }
  541. func (f *Interface) maybeSendRecvError(endpoint netip.AddrPort, index uint32) {
  542. if f.sendRecvErrorConfig.ShouldRecvError(endpoint) {
  543. f.sendRecvError(endpoint, index)
  544. }
  545. }
  546. func (f *Interface) sendRecvError(endpoint netip.AddrPort, index uint32) {
  547. f.messageMetrics.Tx(header.RecvError, 0, 1)
  548. b := header.Encode(make([]byte, header.Len), header.Version, header.RecvError, 0, index, 0)
  549. _ = f.outside.WriteTo(b, endpoint)
  550. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  551. f.l.Debug("Recv error sent",
  552. "index", index,
  553. "udpAddr", endpoint,
  554. )
  555. }
  556. }
  557. func (f *Interface) handleRecvError(addr netip.AddrPort, h *header.H) {
  558. if !f.acceptRecvErrorConfig.ShouldRecvError(addr) {
  559. f.l.Debug("Recv error received, ignoring",
  560. "index", h.RemoteIndex,
  561. "udpAddr", addr,
  562. )
  563. return
  564. }
  565. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  566. f.l.Debug("Recv error received",
  567. "index", h.RemoteIndex,
  568. "udpAddr", addr,
  569. )
  570. }
  571. hostinfo := f.hostMap.QueryReverseIndex(h.RemoteIndex)
  572. if hostinfo == nil {
  573. f.l.Debug("Did not find remote index in main hostmap", "remoteIndex", h.RemoteIndex)
  574. return
  575. }
  576. hr := hostinfo.GetRemote()
  577. if hr.IsValid() && hr != addr {
  578. f.l.Info("Someone spoofing recv_errors?",
  579. "addr", addr,
  580. "hostinfoRemote", hr,
  581. )
  582. return
  583. }
  584. f.closeTunnel(hostinfo)
  585. // We also delete it from pending hostmap to allow for fast reconnect.
  586. f.handshakeManager.DeleteHostInfo(hostinfo)
  587. }