outside.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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, q)
  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. SockIdx: via.SockIdx,
  180. }
  181. f.readOutsidePackets(via, scratch, signedPayload, h, fwPacket, lhf, nb, q, localCache, meta)
  182. case ForwardingType:
  183. // Find the target HostInfo relay object
  184. targetHI, targetRelay, err := f.hostMap.QueryVpnAddrsRelayFor(hostinfo.vpnAddrs, relay.PeerAddr)
  185. if err != nil {
  186. hostinfo.logger(f.l).Info("Failed to find target host info by ip",
  187. "relayTo", relay.PeerAddr,
  188. "relayFrom", hostinfo.vpnAddrs[0],
  189. "error", err,
  190. )
  191. return
  192. }
  193. // If that relay is Established, forward the payload through it
  194. if targetRelay.State == Established {
  195. switch targetRelay.Type {
  196. case ForwardingType:
  197. // Forward this packet through the relay tunnel, rebuilding it in place.
  198. // Encode overwrites the old outer header, and the new AEAD tag lands where the old one was
  199. fwdBuf := packet[:0:len(packet)] // Cap to len(packet) to protect memory from a larger parent buffer
  200. //todo it would potentially be nice to batch these
  201. f.SendVia(targetHI, targetRelay, signedPayload, nb, fwdBuf, true)
  202. case TerminalType:
  203. hostinfo.logger(f.l).Error("Unexpected Relay Type of Terminal")
  204. return
  205. default:
  206. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  207. hostinfo.logger(f.l).Debug("Unexpected targetRelay Type", "from", via, "relayType", targetRelay.Type)
  208. }
  209. return
  210. }
  211. } else {
  212. hostinfo.logger(f.l).Info("Unexpected target relay state",
  213. "relayTo", relay.PeerAddr,
  214. "relayFrom", hostinfo.vpnAddrs[0],
  215. "targetRelayState", targetRelay.State,
  216. )
  217. return
  218. }
  219. default:
  220. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  221. hostinfo.logger(f.l).Debug("Unexpected relay type", "from", via, "relayType", relay.Type)
  222. }
  223. }
  224. }
  225. // closeTunnel closes a tunnel locally, it does not send a closeTunnel packet to the remote
  226. func (f *Interface) closeTunnel(hostInfo *HostInfo) {
  227. final := f.hostMap.DeleteHostInfo(hostInfo)
  228. if final {
  229. // We no longer have any tunnels with this vpn addr, clear learned lighthouse state to lower memory usage
  230. f.lightHouse.DeleteVpnAddrs(hostInfo.vpnAddrs)
  231. }
  232. }
  233. // sendCloseTunnel is a helper function to send a proper close tunnel packet to a remote
  234. func (f *Interface) sendCloseTunnel(h *HostInfo) {
  235. f.send(header.CloseTunnel, 0, h.ConnectionState, h, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
  236. }
  237. func (f *Interface) handleHostRoaming(hostinfo *HostInfo, via ViaSender) {
  238. curRemote := hostinfo.GetRemote()
  239. if !via.IsRelayed && curRemote != via.UdpAddr {
  240. if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) {
  241. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  242. hostinfo.logger(f.l).Debug("lighthouse.remote_allow_list denied roaming", "newAddr", via.UdpAddr)
  243. }
  244. return
  245. }
  246. if !hostinfo.lastRoam.IsZero() && via.UdpAddr == hostinfo.lastRoamRemote && time.Since(hostinfo.lastRoam) < RoamingSuppressSeconds*time.Second {
  247. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  248. hostinfo.logger(f.l).Debug("Suppressing roam back to previous remote",
  249. "suppressSeconds", RoamingSuppressSeconds,
  250. "udpAddr", curRemote,
  251. "newAddr", via.UdpAddr,
  252. )
  253. }
  254. return
  255. }
  256. hostinfo.logger(f.l).Info("Host roamed to new udp ip/port.",
  257. "udpAddr", curRemote,
  258. "newAddr", via.UdpAddr,
  259. )
  260. hostinfo.lastRoam = time.Now()
  261. hostinfo.lastRoamRemote = curRemote
  262. hostinfo.SetRemote(via.UdpAddr)
  263. }
  264. }
  265. var (
  266. ErrPacketTooShort = errors.New("packet is too short")
  267. ErrUnknownIPVersion = errors.New("packet is an unknown ip version")
  268. ErrIPv4InvalidHeaderLength = errors.New("invalid ipv4 header length")
  269. ErrIPv4PacketTooShort = errors.New("ipv4 packet is too short")
  270. ErrIPv6PacketTooShort = errors.New("ipv6 packet is too short")
  271. ErrIPv6CouldNotFindPayload = errors.New("could not find payload in ipv6 packet")
  272. )
  273. // newPacket validates and parses the interesting bits for the firewall out of the ip and sub protocol headers
  274. func newPacket(data []byte, incoming bool, fp *firewall.Packet) error {
  275. if len(data) < 1 {
  276. return ErrPacketTooShort
  277. }
  278. version := int((data[0] >> 4) & 0x0f)
  279. switch version {
  280. case ipv4.Version:
  281. return parseV4(data, incoming, fp)
  282. case ipv6.Version:
  283. return parseV6(data, incoming, fp)
  284. }
  285. return ErrUnknownIPVersion
  286. }
  287. func parseV6(data []byte, incoming bool, fp *firewall.Packet) error {
  288. dataLen := len(data)
  289. if dataLen < ipv6.HeaderLen {
  290. return ErrIPv6PacketTooShort
  291. }
  292. if incoming {
  293. fp.RemoteAddr, _ = netip.AddrFromSlice(data[8:24])
  294. fp.LocalAddr, _ = netip.AddrFromSlice(data[24:40])
  295. } else {
  296. fp.LocalAddr, _ = netip.AddrFromSlice(data[8:24])
  297. fp.RemoteAddr, _ = netip.AddrFromSlice(data[24:40])
  298. }
  299. protoAt := 6 // NextHeader is at 6 bytes into the ipv6 header
  300. offset := ipv6.HeaderLen // Start at the end of the ipv6 header
  301. next := 0
  302. for {
  303. if protoAt >= dataLen {
  304. break
  305. }
  306. proto := layers.IPProtocol(data[protoAt])
  307. switch proto {
  308. case layers.IPProtocolESP, layers.IPProtocolNoNextHeader:
  309. fp.Protocol = uint8(proto)
  310. fp.RemotePort = 0
  311. fp.LocalPort = 0
  312. fp.Fragment = false
  313. return nil
  314. case layers.IPProtocolICMPv6:
  315. if dataLen < offset+6 {
  316. return ErrIPv6PacketTooShort
  317. }
  318. fp.Protocol = uint8(proto)
  319. fp.LocalPort = 0 //incoming vs outgoing doesn't matter for icmpv6
  320. icmptype := data[offset+1]
  321. switch icmptype {
  322. case layers.ICMPv6TypeEchoRequest, layers.ICMPv6TypeEchoReply:
  323. fp.RemotePort = binary.BigEndian.Uint16(data[offset+4 : offset+6]) //identifier
  324. default:
  325. fp.RemotePort = 0
  326. }
  327. fp.Fragment = false
  328. return nil
  329. case layers.IPProtocolTCP, layers.IPProtocolUDP:
  330. if dataLen < offset+4 {
  331. return ErrIPv6PacketTooShort
  332. }
  333. fp.Protocol = uint8(proto)
  334. if incoming {
  335. fp.RemotePort = binary.BigEndian.Uint16(data[offset : offset+2])
  336. fp.LocalPort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
  337. } else {
  338. fp.LocalPort = binary.BigEndian.Uint16(data[offset : offset+2])
  339. fp.RemotePort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
  340. }
  341. fp.Fragment = false
  342. return nil
  343. case layers.IPProtocolIPv6Fragment:
  344. // Fragment header is 8 bytes, need at least offset+4 to read the offset field
  345. if dataLen < offset+8 {
  346. return ErrIPv6PacketTooShort
  347. }
  348. // Check if this is the first fragment
  349. fragmentOffset := binary.BigEndian.Uint16(data[offset+2:offset+4]) &^ uint16(0x7) // Remove the reserved and M flag bits
  350. if fragmentOffset != 0 {
  351. // Non-first fragment, use what we have now and stop processing
  352. fp.Protocol = data[offset]
  353. fp.Fragment = true
  354. fp.RemotePort = 0
  355. fp.LocalPort = 0
  356. return nil
  357. }
  358. // The next loop should be the transport layer since we are the first fragment
  359. next = 8 // Fragment headers are always 8 bytes
  360. case layers.IPProtocolAH:
  361. // Auth headers, used by IPSec, have a different meaning for header length
  362. if dataLen <= offset+1 {
  363. break
  364. }
  365. next = (int(data[offset+1]) + 2) << 2
  366. default:
  367. // Normal ipv6 header length processing
  368. if dataLen <= offset+1 {
  369. break
  370. }
  371. next = (int(data[offset+1]) + 1) << 3
  372. }
  373. if next <= 0 {
  374. // Safety check, each ipv6 header has to be at least 8 bytes
  375. next = 8
  376. }
  377. protoAt = offset
  378. offset = offset + next
  379. }
  380. return ErrIPv6CouldNotFindPayload
  381. }
  382. func parseV4(data []byte, incoming bool, fp *firewall.Packet) error {
  383. // Do we at least have an ipv4 header worth of data?
  384. if len(data) < ipv4.HeaderLen {
  385. return ErrIPv4PacketTooShort
  386. }
  387. // Adjust our start position based on the advertised ip header length
  388. ihl := int(data[0]&0x0f) << 2
  389. // Well-formed ip header length?
  390. if ihl < ipv4.HeaderLen {
  391. return ErrIPv4InvalidHeaderLength
  392. }
  393. // Check if this is the second or further fragment of a fragmented packet.
  394. flagsfrags := binary.BigEndian.Uint16(data[6:8])
  395. fp.Fragment = (flagsfrags & 0x1FFF) != 0
  396. // Firewall handles protocol checks
  397. fp.Protocol = data[9]
  398. // Accounting for a variable header length, do we have enough data for our src/dst tuples?
  399. minLen := ihl
  400. if !fp.Fragment {
  401. if fp.Protocol == firewall.ProtoICMP {
  402. minLen += minFwPacketLen + 2
  403. } else {
  404. minLen += minFwPacketLen
  405. }
  406. }
  407. if len(data) < minLen {
  408. return ErrIPv4InvalidHeaderLength
  409. }
  410. if incoming { // Firewall packets are locally oriented
  411. fp.RemoteAddr, _ = netip.AddrFromSlice(data[12:16])
  412. fp.LocalAddr, _ = netip.AddrFromSlice(data[16:20])
  413. } else {
  414. fp.LocalAddr, _ = netip.AddrFromSlice(data[12:16])
  415. fp.RemoteAddr, _ = netip.AddrFromSlice(data[16:20])
  416. }
  417. if fp.Fragment {
  418. fp.RemotePort = 0
  419. fp.LocalPort = 0
  420. } else if fp.Protocol == firewall.ProtoICMP { //note that orientation doesn't matter on ICMP
  421. fp.RemotePort = binary.BigEndian.Uint16(data[ihl+4 : ihl+6]) //identifier
  422. fp.LocalPort = 0 //code would be uint16(data[ihl+1])
  423. } else if incoming {
  424. fp.RemotePort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port
  425. fp.LocalPort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port
  426. } else {
  427. fp.LocalPort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port
  428. fp.RemotePort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port
  429. }
  430. return nil
  431. }
  432. // decrypt authenticates and decrypts a Message packet IN PLACE: the dst is
  433. // packet[header.Len:header.Len], the exact-alias append pattern
  434. // (ciphertext[:0]) that crypto/cipher.AEAD.Open documents, so the plaintext
  435. // lands where the ciphertext sat and no separate plaintext buffer exists.
  436. // On a failed auth the AEAD zeroes the would-be plaintext region (both
  437. // AES-GCM and ChaCha20-Poly1305 do) but never writes outside it, so the
  438. // other segments of a shared GRO receive row stay intact; nothing reads a
  439. // packet after its decrypt fails. The returned slice aliases packet.
  440. func (f *Interface) decrypt(hostinfo *HostInfo, mc uint64, packet []byte, nb []byte) ([]byte, error) {
  441. out, err := hostinfo.ConnectionState.dKey.DecryptDanger(packet[header.Len:header.Len], packet[:header.Len], packet[header.Len:], mc, nb)
  442. if err != nil {
  443. return nil, err
  444. }
  445. if !hostinfo.ConnectionState.window.Update(f.l, mc) {
  446. return nil, ErrOutOfWindow
  447. }
  448. return out, nil
  449. }
  450. // 2-bit IP-level ECN codepoints (lower bits of IPv4 ToS / IPv6 TC).
  451. const (
  452. ecnNotECT = 0x00
  453. ecnECT1 = 0x01
  454. ecnECT0 = 0x02
  455. ecnCE = 0x03
  456. )
  457. // applyOuterECN folds an outer CE mark from the underlay into the inner
  458. // IP header per RFC 6040 normal mode. It mutates pkt[1] in place. Other
  459. // codepoints are advisory only and leave the inner unchanged.
  460. //
  461. // Merge cases (outer × inner → action):
  462. //
  463. // outer != CE : no-op (inner is authoritative)
  464. // outer == CE, inner Not-ECT : log; cannot propagate to a non-ECN host
  465. // outer == CE, inner ECT/CE : rewrite inner ECN to CE
  466. func applyOuterECN(pkt []byte, outerECN byte, hostinfo *HostInfo, l *slog.Logger) {
  467. if outerECN&ecnCE != ecnCE || len(pkt) < 2 {
  468. return
  469. }
  470. switch pkt[0] >> 4 {
  471. case 4:
  472. switch pkt[1] & 0x03 {
  473. case ecnNotECT:
  474. if l.Enabled(context.Background(), slog.LevelDebug) {
  475. hostinfo.logger(l).Debug("RFC 6040: outer CE on inner Not-ECT, leaving inner unchanged")
  476. }
  477. case ecnCE:
  478. // Already CE.
  479. default:
  480. // Rewriting the ToS byte invalidates the IPv4 header checksum, so
  481. // patch it incrementally per RFC 1624 (HC' = ~(~HC + ~m + m')). The
  482. // ToS is the low byte of the 16-bit word at pkt[0:2]; the header
  483. // checksum lives at pkt[10:12]. A header too short to carry a
  484. // checksum can't be fixed up here, so leave it for newPacket to
  485. // reject rather than emit a mangled packet.
  486. if len(pkt) < ipv4.HeaderLen {
  487. return
  488. }
  489. m := binary.BigEndian.Uint16(pkt[0:2])
  490. pkt[1] = (pkt[1] &^ 0x03) | ecnCE
  491. mNew := binary.BigEndian.Uint16(pkt[0:2])
  492. sum := uint32(^binary.BigEndian.Uint16(pkt[10:12])) + uint32(^m) + uint32(mNew)
  493. for sum > 0xffff {
  494. sum = (sum >> 16) + (sum & 0xffff)
  495. }
  496. binary.BigEndian.PutUint16(pkt[10:12], ^uint16(sum))
  497. }
  498. case 6:
  499. switch (pkt[1] >> 4) & 0x03 {
  500. case ecnNotECT:
  501. if l.Enabled(context.Background(), slog.LevelDebug) {
  502. hostinfo.logger(l).Debug("RFC 6040: outer CE on inner Not-ECT, leaving inner unchanged")
  503. }
  504. case ecnCE:
  505. // Already CE.
  506. default:
  507. pkt[1] = (pkt[1] &^ 0x30) | (ecnCE << 4)
  508. }
  509. }
  510. }
  511. func (f *Interface) handleOutsideMessagePacket(hostinfo *HostInfo, out []byte, scratch []byte, fwPacket *firewall.Packet, nb []byte, q int, localCache firewall.ConntrackCache, meta udp.RxMeta) {
  512. // RFC 6040 normal-mode combine: fold any outer CE mark stamped by the
  513. // underlay into the inner header before firewall + TUN write. Other
  514. // outer codepoints are advisory only — we keep the inner unchanged.
  515. if f.ecnEnabled.Load() {
  516. applyOuterECN(out, meta.OuterECN, hostinfo, f.l)
  517. }
  518. err := newPacket(out, true, fwPacket)
  519. if err != nil {
  520. hostinfo.logger(f.l).Warn("Error while validating inbound packet",
  521. "error", err,
  522. "packet", out,
  523. )
  524. return
  525. }
  526. dropReason := f.firewall.Drop(*fwPacket, true, hostinfo, f.pki.GetCAPool(), localCache)
  527. if dropReason != nil {
  528. f.rejectOutside(out, hostinfo.ConnectionState, hostinfo, nb, scratch)
  529. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  530. hostinfo.logger(f.l).Debug("dropping inbound packet",
  531. "fwPacket", fwPacket,
  532. "reason", dropReason,
  533. )
  534. }
  535. return
  536. }
  537. err = f.batchers[q].Commit(out)
  538. if err != nil {
  539. f.l.Error("Failed to write to tun", "error", err)
  540. }
  541. }
  542. func (f *Interface) maybeSendRecvError(endpoint netip.AddrPort, index uint32, q int) {
  543. if f.sendRecvErrorConfig.ShouldRecvError(endpoint) {
  544. f.sendRecvError(endpoint, index, q)
  545. }
  546. }
  547. // sendRecvError replies from the socket the offending packet arrived on (q).
  548. // A lane peer's spoof guard compares our source addr against the lane's
  549. // remote, so a reply from the base port would be discarded.
  550. func (f *Interface) sendRecvError(endpoint netip.AddrPort, index uint32, q int) {
  551. f.messageMetrics.Tx(header.RecvError, 0, 1)
  552. b := header.Encode(make([]byte, header.Len), header.Version, header.RecvError, 0, index, 0)
  553. _ = f.writers[q].WriteTo(b, endpoint)
  554. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  555. f.l.Debug("Recv error sent",
  556. "index", index,
  557. "udpAddr", endpoint,
  558. )
  559. }
  560. }
  561. func (f *Interface) handleRecvError(addr netip.AddrPort, h *header.H) {
  562. if !f.acceptRecvErrorConfig.ShouldRecvError(addr) {
  563. f.l.Debug("Recv error received, ignoring",
  564. "index", h.RemoteIndex,
  565. "udpAddr", addr,
  566. )
  567. return
  568. }
  569. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  570. f.l.Debug("Recv error received",
  571. "index", h.RemoteIndex,
  572. "udpAddr", addr,
  573. )
  574. }
  575. hostinfo := f.hostMap.QueryReverseIndex(h.RemoteIndex)
  576. if hostinfo == nil {
  577. f.l.Debug("Did not find remote index in main hostmap", "remoteIndex", h.RemoteIndex)
  578. return
  579. }
  580. hr := hostinfo.GetRemote()
  581. if hr.IsValid() && hr != addr {
  582. f.l.Info("Someone spoofing recv_errors?",
  583. "addr", addr,
  584. "hostinfoRemote", hr,
  585. )
  586. return
  587. }
  588. f.closeTunnel(hostinfo)
  589. // We also delete it from pending hostmap to allow for fast reconnect.
  590. f.handshakeManager.DeleteHostInfo(hostinfo)
  591. }