outside.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. package nebula
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "net/netip"
  6. "time"
  7. "github.com/google/gopacket/layers"
  8. "github.com/slackhq/nebula/packet"
  9. "golang.org/x/net/ipv6"
  10. "github.com/sirupsen/logrus"
  11. "github.com/slackhq/nebula/firewall"
  12. "github.com/slackhq/nebula/header"
  13. "golang.org/x/net/ipv4"
  14. )
  15. const (
  16. minFwPacketLen = 4
  17. )
  18. func (f *Interface) readOutsidePacketFromRelay(via ViaSender, out []byte, packet []byte, h *header.H, fwPacket *firewall.Packet, lhf *LightHouseHandler, nb []byte, q int, localCache firewall.ConntrackCache, now time.Time) {
  19. //todo this is way too similar to readOutsidePacketsMany, find a way to eliminate
  20. err := h.Parse(packet)
  21. if err != nil {
  22. // Hole punch packets are 0 or 1 byte big, so lets ignore printing those errors
  23. if len(packet) > 1 {
  24. f.l.WithField("packet", packet).Infof("Error while parsing inbound packet from %s: %s", via, err)
  25. }
  26. return
  27. }
  28. //l.Error("in packet ", header, packet[HeaderLen:])
  29. if !via.IsRelayed {
  30. if f.myVpnNetworksTable.Contains(via.UdpAddr.Addr()) {
  31. if f.l.Level >= logrus.DebugLevel {
  32. f.l.WithField("from", via).Debug("Refusing to process double encrypted packet")
  33. }
  34. return
  35. }
  36. }
  37. var hostinfo *HostInfo
  38. // verify if we've seen this index before, otherwise respond to the handshake initiation
  39. if h.Type == header.Message && h.Subtype == header.MessageRelay {
  40. hostinfo = f.hostMap.QueryRelayIndex(h.RemoteIndex)
  41. } else {
  42. hostinfo = f.hostMap.QueryIndex(h.RemoteIndex)
  43. }
  44. var ci *ConnectionState
  45. if hostinfo != nil {
  46. ci = hostinfo.ConnectionState
  47. }
  48. switch h.Type {
  49. case header.Message:
  50. if !f.handleEncrypted(ci, via, h) {
  51. return
  52. }
  53. switch h.Subtype {
  54. case header.MessageNone:
  55. if !f.decryptToTun(hostinfo, h.MessageCounter, out, packet, fwPacket, nb, q, localCache, now) {
  56. return
  57. }
  58. case header.MessageRelay:
  59. //this packet already came to us via a relay
  60. if f.l.Level >= logrus.DebugLevel {
  61. f.l.WithField("from", via).Debug("Refusing to process double relayed packet")
  62. }
  63. return
  64. }
  65. case header.LightHouse:
  66. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  67. if !f.handleEncrypted(ci, via, h) {
  68. return
  69. }
  70. d, err := f.decrypt(hostinfo, h.MessageCounter, out, packet, h, nb)
  71. if err != nil {
  72. hostinfo.logger(f.l).WithError(err).WithField("from", via).
  73. WithField("packet", packet).
  74. Error("Failed to decrypt lighthouse packet")
  75. return
  76. }
  77. //TODO: assert via is not relayed
  78. lhf.HandleRequest(via.UdpAddr, hostinfo.vpnAddrs, d, f)
  79. // Fallthrough to the bottom to record incoming traffic
  80. case header.Test:
  81. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  82. if !f.handleEncrypted(ci, via, h) {
  83. return
  84. }
  85. d, err := f.decrypt(hostinfo, h.MessageCounter, out, packet, h, nb)
  86. if err != nil {
  87. hostinfo.logger(f.l).WithError(err).WithField("from", via).
  88. WithField("packet", packet).
  89. Error("Failed to decrypt test packet")
  90. return
  91. }
  92. if h.Subtype == header.TestRequest {
  93. // This testRequest might be from TryPromoteBest, so we should roam
  94. // to the new IP address before responding
  95. f.handleHostRoaming(hostinfo, via)
  96. f.send(header.Test, header.TestReply, ci, hostinfo, d, nb, out)
  97. }
  98. // Fallthrough to the bottom to record incoming traffic
  99. // Non encrypted messages below here, they should not fall through to avoid tracking incoming traffic since they
  100. // are unauthenticated
  101. case header.Handshake:
  102. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  103. f.handshakeManager.HandleIncoming(via, packet, h)
  104. return
  105. case header.RecvError:
  106. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  107. f.handleRecvError(via.UdpAddr, h)
  108. return
  109. case header.CloseTunnel:
  110. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  111. if !f.handleEncrypted(ci, via, h) {
  112. return
  113. }
  114. hostinfo.logger(f.l).WithField("from", via).
  115. Info("Close tunnel received, tearing down.")
  116. f.closeTunnel(hostinfo)
  117. return
  118. case header.Control:
  119. if !f.handleEncrypted(ci, via, h) {
  120. return
  121. }
  122. d, err := f.decrypt(hostinfo, h.MessageCounter, out, packet, h, nb)
  123. if err != nil {
  124. hostinfo.logger(f.l).WithError(err).WithField("from", via).
  125. WithField("packet", packet).
  126. Error("Failed to decrypt Control packet")
  127. return
  128. }
  129. f.relayManager.HandleControlMsg(hostinfo, d, f)
  130. default:
  131. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  132. hostinfo.logger(f.l).Debugf("Unexpected packet received from %s", via)
  133. return
  134. }
  135. f.handleHostRoaming(hostinfo, via)
  136. f.connectionManager.In(hostinfo)
  137. }
  138. func (f *Interface) readOutsidePacketsMany(packets []*packet.Packet, out []*packet.OutPacket, h *header.H, fwPacket *firewall.Packet, lhf *LightHouseHandler, nb []byte, q int, localCache firewall.ConntrackCache, now time.Time) {
  139. for i, pkt := range packets {
  140. out[i].Scratch = out[i].Scratch[:0]
  141. via := ViaSender{UdpAddr: pkt.AddrPort()}
  142. //l.Error("in packet ", header, packet[HeaderLen:])
  143. if !via.IsRelayed {
  144. if f.myVpnNetworksTable.Contains(via.UdpAddr.Addr()) {
  145. if f.l.Level >= logrus.DebugLevel {
  146. f.l.WithField("from", via).Debug("Refusing to process double encrypted packet")
  147. }
  148. return
  149. }
  150. }
  151. //todo per-segment!
  152. for segment := range pkt.Segments() {
  153. err := h.Parse(segment)
  154. if err != nil {
  155. // Hole punch packets are 0 or 1 byte big, so lets ignore printing those errors
  156. if len(segment) > 1 {
  157. f.l.WithField("packet", pkt).Infof("Error while parsing inbound packet from %s: %s", via, err)
  158. }
  159. return
  160. }
  161. var hostinfo *HostInfo
  162. // verify if we've seen this index before, otherwise respond to the handshake initiation
  163. if h.Type == header.Message && h.Subtype == header.MessageRelay {
  164. hostinfo = f.hostMap.QueryRelayIndex(h.RemoteIndex)
  165. } else {
  166. hostinfo = f.hostMap.QueryIndex(h.RemoteIndex)
  167. }
  168. var ci *ConnectionState
  169. if hostinfo != nil {
  170. ci = hostinfo.ConnectionState
  171. }
  172. switch h.Type {
  173. case header.Message:
  174. // TODO handleEncrypted sends directly to addr on error. Handle this in the tunneling case.
  175. if !f.handleEncrypted(ci, via, h) {
  176. return
  177. }
  178. switch h.Subtype {
  179. case header.MessageNone:
  180. if !f.decryptToTunDelayWrite(hostinfo, h.MessageCounter, out[i], pkt, segment, fwPacket, nb, q, localCache, now) {
  181. return
  182. }
  183. case header.MessageRelay:
  184. // The entire body is sent as AD, not encrypted.
  185. // The packet consists of a 16-byte parsed Nebula header, Associated Data-protected payload, and a trailing 16-byte AEAD signature value.
  186. // 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
  187. // otherwise malformed (meaning, there is no trailing 16 byte AEAD value), then this will result in at worst a 0-length slice
  188. // which will gracefully fail in the DecryptDanger call.
  189. signedPayload := segment[:len(segment)-hostinfo.ConnectionState.dKey.Overhead()]
  190. signatureValue := segment[len(segment)-hostinfo.ConnectionState.dKey.Overhead():]
  191. out[i].Scratch, err = hostinfo.ConnectionState.dKey.DecryptDanger(out[i].Scratch, signedPayload, signatureValue, h.MessageCounter, nb)
  192. if err != nil {
  193. return
  194. }
  195. // Successfully validated the thing. Get rid of the Relay header.
  196. signedPayload = signedPayload[header.Len:]
  197. // Pull the Roaming parts up here, and return in all call paths.
  198. f.handleHostRoaming(hostinfo, via)
  199. // Track usage of both the HostInfo and the Relay for the received & authenticated packet
  200. f.connectionManager.In(hostinfo)
  201. f.connectionManager.RelayUsed(h.RemoteIndex)
  202. relay, ok := hostinfo.relayState.QueryRelayForByIdx(h.RemoteIndex)
  203. if !ok {
  204. // The only way this happens is if hostmap has an index to the correct HostInfo, but the HostInfo is missing
  205. // its internal mapping. This should never happen.
  206. hostinfo.logger(f.l).WithFields(logrus.Fields{"vpnAddrs": hostinfo.vpnAddrs, "remoteIndex": h.RemoteIndex}).Error("HostInfo missing remote relay index")
  207. return
  208. }
  209. switch relay.Type {
  210. case TerminalType:
  211. // If I am the target of this relay, process the unwrapped packet
  212. // From this recursive point, all these variables are 'burned'. We shouldn't rely on them again.
  213. via = ViaSender{
  214. UdpAddr: via.UdpAddr,
  215. relayHI: hostinfo,
  216. remoteIdx: relay.RemoteIndex,
  217. relay: relay,
  218. IsRelayed: true,
  219. }
  220. f.readOutsidePacketFromRelay(via, out[i].Scratch[:0], signedPayload, h, fwPacket, lhf, nb, q, localCache, now)
  221. return
  222. case ForwardingType:
  223. // Find the target HostInfo relay object
  224. targetHI, targetRelay, err := f.hostMap.QueryVpnAddrsRelayFor(hostinfo.vpnAddrs, relay.PeerAddr)
  225. if err != nil {
  226. hostinfo.logger(f.l).WithField("relayTo", relay.PeerAddr).WithError(err).WithField("hostinfo.vpnAddrs", hostinfo.vpnAddrs).Info("Failed to find target host info by ip")
  227. return
  228. }
  229. // If that relay is Established, forward the payload through it
  230. if targetRelay.State == Established {
  231. switch targetRelay.Type {
  232. case ForwardingType:
  233. // Forward this packet through the relay tunnel
  234. // Find the target HostInfo
  235. f.SendVia(targetHI, targetRelay, signedPayload, nb, out[i].Scratch, false)
  236. return
  237. case TerminalType:
  238. hostinfo.logger(f.l).Error("Unexpected Relay Type of Terminal")
  239. }
  240. } else {
  241. hostinfo.logger(f.l).WithFields(logrus.Fields{"relayTo": relay.PeerAddr, "relayFrom": hostinfo.vpnAddrs[0], "targetRelayState": targetRelay.State}).Info("Unexpected target relay state")
  242. return
  243. }
  244. }
  245. }
  246. case header.LightHouse:
  247. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  248. if !f.handleEncrypted(ci, via, h) {
  249. return
  250. }
  251. d, err := f.decrypt(hostinfo, h.MessageCounter, out[i].Scratch, segment, h, nb)
  252. if err != nil {
  253. hostinfo.logger(f.l).WithError(err).WithField("udpAddr", via.UdpAddr).
  254. WithField("packet", segment).
  255. Error("Failed to decrypt lighthouse packet")
  256. return
  257. }
  258. lhf.HandleRequest(via.UdpAddr, hostinfo.vpnAddrs, d, f)
  259. // Fallthrough to the bottom to record incoming traffic
  260. case header.Test:
  261. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  262. if !f.handleEncrypted(ci, via, h) {
  263. return
  264. }
  265. d, err := f.decrypt(hostinfo, h.MessageCounter, out[i].Scratch, segment, h, nb)
  266. if err != nil {
  267. hostinfo.logger(f.l).WithError(err).WithField("udpAddr", via).
  268. WithField("packet", segment).
  269. Error("Failed to decrypt test packet")
  270. return
  271. }
  272. if h.Subtype == header.TestRequest {
  273. // This testRequest might be from TryPromoteBest, so we should roam
  274. // to the new IP address before responding
  275. f.handleHostRoaming(hostinfo, via)
  276. f.send(header.Test, header.TestReply, ci, hostinfo, d, nb, out[i].Scratch)
  277. }
  278. // Fallthrough to the bottom to record incoming traffic
  279. // Non encrypted messages below here, they should not fall through to avoid tracking incoming traffic since they
  280. // are unauthenticated
  281. case header.Handshake:
  282. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  283. f.handshakeManager.HandleIncoming(via, segment, h)
  284. return
  285. case header.RecvError:
  286. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  287. f.handleRecvError(via.UdpAddr, h)
  288. return
  289. case header.CloseTunnel:
  290. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  291. if !f.handleEncrypted(ci, via, h) {
  292. return
  293. }
  294. hostinfo.logger(f.l).WithField("udpAddr", via).
  295. Info("Close tunnel received, tearing down.")
  296. f.closeTunnel(hostinfo)
  297. return
  298. case header.Control:
  299. if !f.handleEncrypted(ci, via, h) {
  300. return
  301. }
  302. d, err := f.decrypt(hostinfo, h.MessageCounter, out[i].Scratch, segment, h, nb)
  303. if err != nil {
  304. hostinfo.logger(f.l).WithError(err).WithField("udpAddr", via).
  305. WithField("packet", segment).
  306. Error("Failed to decrypt Control packet")
  307. return
  308. }
  309. f.relayManager.HandleControlMsg(hostinfo, d, f)
  310. default:
  311. f.messageMetrics.Rx(h.Type, h.Subtype, 1)
  312. hostinfo.logger(f.l).Debugf("Unexpected packet received from %s", via)
  313. return
  314. }
  315. f.handleHostRoaming(hostinfo, via)
  316. f.connectionManager.In(hostinfo)
  317. }
  318. _, err := f.readers[q].WriteOne(out[i], false, q)
  319. if err != nil {
  320. f.l.WithError(err).Error("Failed to write packet")
  321. }
  322. }
  323. }
  324. // closeTunnel closes a tunnel locally, it does not send a closeTunnel packet to the remote
  325. func (f *Interface) closeTunnel(hostInfo *HostInfo) {
  326. final := f.hostMap.DeleteHostInfo(hostInfo)
  327. if final {
  328. // We no longer have any tunnels with this vpn addr, clear learned lighthouse state to lower memory usage
  329. f.lightHouse.DeleteVpnAddrs(hostInfo.vpnAddrs)
  330. }
  331. }
  332. // sendCloseTunnel is a helper function to send a proper close tunnel packet to a remote
  333. func (f *Interface) sendCloseTunnel(h *HostInfo) {
  334. f.send(header.CloseTunnel, 0, h.ConnectionState, h, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
  335. }
  336. func (f *Interface) handleHostRoaming(hostinfo *HostInfo, via ViaSender) {
  337. if !via.IsRelayed && hostinfo.remote != via.UdpAddr {
  338. if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) {
  339. hostinfo.logger(f.l).WithField("newAddr", via.UdpAddr).Debug("lighthouse.remote_allow_list denied roaming")
  340. return
  341. }
  342. if !hostinfo.lastRoam.IsZero() && via.UdpAddr == hostinfo.lastRoamRemote && time.Since(hostinfo.lastRoam) < RoamingSuppressSeconds*time.Second {
  343. if f.l.Level >= logrus.DebugLevel {
  344. hostinfo.logger(f.l).WithField("udpAddr", hostinfo.remote).WithField("newAddr", via.UdpAddr).
  345. Debugf("Suppressing roam back to previous remote for %d seconds", RoamingSuppressSeconds)
  346. }
  347. return
  348. }
  349. hostinfo.logger(f.l).WithField("udpAddr", hostinfo.remote).WithField("newAddr", via.UdpAddr).
  350. Info("Host roamed to new udp ip/port.")
  351. hostinfo.lastRoam = time.Now()
  352. hostinfo.lastRoamRemote = hostinfo.remote
  353. hostinfo.SetRemote(via.UdpAddr)
  354. }
  355. }
  356. // handleEncrypted returns true if a packet should be processed, false otherwise
  357. func (f *Interface) handleEncrypted(ci *ConnectionState, via ViaSender, h *header.H) bool {
  358. // If connectionstate does not exist, send a recv error, if possible, to encourage a fast reconnect
  359. if ci == nil {
  360. if !via.IsRelayed {
  361. f.maybeSendRecvError(via.UdpAddr, h.RemoteIndex)
  362. }
  363. return false
  364. }
  365. // If the window check fails, refuse to process the packet, but don't send a recv error
  366. if !ci.window.Check(f.l, h.MessageCounter) {
  367. return false
  368. }
  369. return true
  370. }
  371. var (
  372. ErrPacketTooShort = errors.New("packet is too short")
  373. ErrUnknownIPVersion = errors.New("packet is an unknown ip version")
  374. ErrIPv4InvalidHeaderLength = errors.New("invalid ipv4 header length")
  375. ErrIPv4PacketTooShort = errors.New("ipv4 packet is too short")
  376. ErrIPv6PacketTooShort = errors.New("ipv6 packet is too short")
  377. ErrIPv6CouldNotFindPayload = errors.New("could not find payload in ipv6 packet")
  378. )
  379. // newPacket validates and parses the interesting bits for the firewall out of the ip and sub protocol headers
  380. func newPacket(data []byte, incoming bool, fp *firewall.Packet) error {
  381. if len(data) < 1 {
  382. return ErrPacketTooShort
  383. }
  384. version := int((data[0] >> 4) & 0x0f)
  385. switch version {
  386. case ipv4.Version:
  387. return parseV4(data, incoming, fp)
  388. case ipv6.Version:
  389. return parseV6(data, incoming, fp)
  390. }
  391. return ErrUnknownIPVersion
  392. }
  393. func parseV6(data []byte, incoming bool, fp *firewall.Packet) error {
  394. dataLen := len(data)
  395. if dataLen < ipv6.HeaderLen {
  396. return ErrIPv6PacketTooShort
  397. }
  398. if incoming {
  399. fp.RemoteAddr, _ = netip.AddrFromSlice(data[8:24])
  400. fp.LocalAddr, _ = netip.AddrFromSlice(data[24:40])
  401. } else {
  402. fp.LocalAddr, _ = netip.AddrFromSlice(data[8:24])
  403. fp.RemoteAddr, _ = netip.AddrFromSlice(data[24:40])
  404. }
  405. protoAt := 6 // NextHeader is at 6 bytes into the ipv6 header
  406. offset := ipv6.HeaderLen // Start at the end of the ipv6 header
  407. next := 0
  408. for {
  409. if protoAt >= dataLen {
  410. break
  411. }
  412. proto := layers.IPProtocol(data[protoAt])
  413. switch proto {
  414. case layers.IPProtocolICMPv6, layers.IPProtocolESP, layers.IPProtocolNoNextHeader:
  415. fp.Protocol = uint8(proto)
  416. fp.RemotePort = 0
  417. fp.LocalPort = 0
  418. fp.Fragment = false
  419. return nil
  420. case layers.IPProtocolTCP, layers.IPProtocolUDP:
  421. if dataLen < offset+4 {
  422. return ErrIPv6PacketTooShort
  423. }
  424. fp.Protocol = uint8(proto)
  425. if incoming {
  426. fp.RemotePort = binary.BigEndian.Uint16(data[offset : offset+2])
  427. fp.LocalPort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
  428. } else {
  429. fp.LocalPort = binary.BigEndian.Uint16(data[offset : offset+2])
  430. fp.RemotePort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
  431. }
  432. fp.Fragment = false
  433. return nil
  434. case layers.IPProtocolIPv6Fragment:
  435. // Fragment header is 8 bytes, need at least offset+4 to read the offset field
  436. if dataLen < offset+8 {
  437. return ErrIPv6PacketTooShort
  438. }
  439. // Check if this is the first fragment
  440. fragmentOffset := binary.BigEndian.Uint16(data[offset+2:offset+4]) &^ uint16(0x7) // Remove the reserved and M flag bits
  441. if fragmentOffset != 0 {
  442. // Non-first fragment, use what we have now and stop processing
  443. fp.Protocol = data[offset]
  444. fp.Fragment = true
  445. fp.RemotePort = 0
  446. fp.LocalPort = 0
  447. return nil
  448. }
  449. // The next loop should be the transport layer since we are the first fragment
  450. next = 8 // Fragment headers are always 8 bytes
  451. case layers.IPProtocolAH:
  452. // Auth headers, used by IPSec, have a different meaning for header length
  453. if dataLen <= offset+1 {
  454. break
  455. }
  456. next = int(data[offset+1]+2) << 2
  457. default:
  458. // Normal ipv6 header length processing
  459. if dataLen <= offset+1 {
  460. break
  461. }
  462. next = int(data[offset+1]+1) << 3
  463. }
  464. if next <= 0 {
  465. // Safety check, each ipv6 header has to be at least 8 bytes
  466. next = 8
  467. }
  468. protoAt = offset
  469. offset = offset + next
  470. }
  471. return ErrIPv6CouldNotFindPayload
  472. }
  473. func parseV4(data []byte, incoming bool, fp *firewall.Packet) error {
  474. // Do we at least have an ipv4 header worth of data?
  475. if len(data) < ipv4.HeaderLen {
  476. return ErrIPv4PacketTooShort
  477. }
  478. // Adjust our start position based on the advertised ip header length
  479. ihl := int(data[0]&0x0f) << 2
  480. // Well-formed ip header length?
  481. if ihl < ipv4.HeaderLen {
  482. return ErrIPv4InvalidHeaderLength
  483. }
  484. // Check if this is the second or further fragment of a fragmented packet.
  485. flagsfrags := binary.BigEndian.Uint16(data[6:8])
  486. fp.Fragment = (flagsfrags & 0x1FFF) != 0
  487. // Firewall handles protocol checks
  488. fp.Protocol = data[9]
  489. // Accounting for a variable header length, do we have enough data for our src/dst tuples?
  490. minLen := ihl
  491. if !fp.Fragment && fp.Protocol != firewall.ProtoICMP {
  492. minLen += minFwPacketLen
  493. }
  494. if len(data) < minLen {
  495. return ErrIPv4InvalidHeaderLength
  496. }
  497. // Firewall packets are locally oriented
  498. if incoming {
  499. fp.RemoteAddr, _ = netip.AddrFromSlice(data[12:16])
  500. fp.LocalAddr, _ = netip.AddrFromSlice(data[16:20])
  501. if fp.Fragment || fp.Protocol == firewall.ProtoICMP {
  502. fp.RemotePort = 0
  503. fp.LocalPort = 0
  504. } else {
  505. fp.RemotePort = binary.BigEndian.Uint16(data[ihl : ihl+2])
  506. fp.LocalPort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4])
  507. }
  508. } else {
  509. fp.LocalAddr, _ = netip.AddrFromSlice(data[12:16])
  510. fp.RemoteAddr, _ = netip.AddrFromSlice(data[16:20])
  511. if fp.Fragment || fp.Protocol == firewall.ProtoICMP {
  512. fp.RemotePort = 0
  513. fp.LocalPort = 0
  514. } else {
  515. fp.LocalPort = binary.BigEndian.Uint16(data[ihl : ihl+2])
  516. fp.RemotePort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4])
  517. }
  518. }
  519. return nil
  520. }
  521. func (f *Interface) decrypt(hostinfo *HostInfo, mc uint64, out []byte, packet []byte, h *header.H, nb []byte) ([]byte, error) {
  522. var err error
  523. out, err = hostinfo.ConnectionState.dKey.DecryptDanger(out, packet[:header.Len], packet[header.Len:], mc, nb)
  524. if err != nil {
  525. return nil, err
  526. }
  527. if !hostinfo.ConnectionState.window.Update(f.l, mc) {
  528. hostinfo.logger(f.l).WithField("header", h).
  529. Debugln("dropping out of window packet")
  530. return nil, errors.New("out of window packet")
  531. }
  532. return out, nil
  533. }
  534. func (f *Interface) decryptToTunDelayWrite(hostinfo *HostInfo, messageCounter uint64, out *packet.OutPacket, pkt *packet.Packet, inSegment []byte, fwPacket *firewall.Packet, nb []byte, q int, localCache firewall.ConntrackCache, now time.Time) bool {
  535. var err error
  536. seg, err := f.readers[q].AllocSeg(out, q)
  537. if err != nil {
  538. f.l.WithError(err).Errorln("decryptToTunDelayWrite: failed to allocate segment")
  539. return false
  540. }
  541. out.SegmentPayloads[seg] = out.SegmentPayloads[seg][:0]
  542. out.SegmentPayloads[seg], err = hostinfo.ConnectionState.dKey.DecryptDanger(out.SegmentPayloads[seg], inSegment[:header.Len], inSegment[header.Len:], messageCounter, nb)
  543. if err != nil {
  544. hostinfo.logger(f.l).WithError(err).Error("Failed to decrypt packet")
  545. return false
  546. }
  547. err = newPacket(out.SegmentPayloads[seg], true, fwPacket)
  548. if err != nil {
  549. hostinfo.logger(f.l).WithError(err).WithField("packet", out).
  550. Warnf("Error while validating inbound packet")
  551. return false
  552. }
  553. if !hostinfo.ConnectionState.window.Update(f.l, messageCounter) {
  554. hostinfo.logger(f.l).WithField("fwPacket", fwPacket).
  555. Debugln("dropping out of window packet")
  556. return false
  557. }
  558. dropReason := f.firewall.Drop(*fwPacket, true, hostinfo, f.pki.GetCAPool(), localCache, now)
  559. if dropReason != nil {
  560. // NOTE: We give `packet` as the `out` here since we already decrypted from it and we don't need it anymore
  561. // This gives us a buffer to build the reject packet in
  562. f.rejectOutside(out.SegmentPayloads[seg], hostinfo.ConnectionState, hostinfo, nb, inSegment, q)
  563. if f.l.Level >= logrus.DebugLevel {
  564. hostinfo.logger(f.l).WithField("fwPacket", fwPacket).
  565. WithField("reason", dropReason).
  566. Debugln("dropping inbound packet")
  567. }
  568. return false
  569. }
  570. f.connectionManager.In(hostinfo)
  571. pkt.OutLen += len(inSegment)
  572. out.Segments[seg] = out.Segments[seg][:len(out.SegmentHeaders[seg])+len(out.SegmentPayloads[seg])]
  573. return true
  574. }
  575. func (f *Interface) decryptToTun(hostinfo *HostInfo, messageCounter uint64, out []byte, packet []byte, fwPacket *firewall.Packet, nb []byte, q int, localCache firewall.ConntrackCache, now time.Time) bool {
  576. var err error
  577. out, err = hostinfo.ConnectionState.dKey.DecryptDanger(out, packet[:header.Len], packet[header.Len:], messageCounter, nb)
  578. if err != nil {
  579. hostinfo.logger(f.l).WithError(err).Error("Failed to decrypt packet")
  580. return false
  581. }
  582. err = newPacket(out, true, fwPacket)
  583. if err != nil {
  584. hostinfo.logger(f.l).WithError(err).WithField("packet", out).
  585. Warnf("Error while validating inbound packet")
  586. return false
  587. }
  588. if !hostinfo.ConnectionState.window.Update(f.l, messageCounter) {
  589. hostinfo.logger(f.l).WithField("fwPacket", fwPacket).
  590. Debugln("dropping out of window packet")
  591. return false
  592. }
  593. dropReason := f.firewall.Drop(*fwPacket, true, hostinfo, f.pki.GetCAPool(), localCache, now)
  594. if dropReason != nil {
  595. // NOTE: We give `packet` as the `out` here since we already decrypted from it and we don't need it anymore
  596. // This gives us a buffer to build the reject packet in
  597. f.rejectOutside(out, hostinfo.ConnectionState, hostinfo, nb, packet, q)
  598. if f.l.Level >= logrus.DebugLevel {
  599. hostinfo.logger(f.l).WithField("fwPacket", fwPacket).
  600. WithField("reason", dropReason).
  601. Debugln("dropping inbound packet")
  602. }
  603. return false
  604. }
  605. f.connectionManager.In(hostinfo)
  606. _, err = f.readers[q].Write(out)
  607. if err != nil {
  608. f.l.WithError(err).Error("Failed to write to tun")
  609. }
  610. return true
  611. }
  612. func (f *Interface) maybeSendRecvError(endpoint netip.AddrPort, index uint32) {
  613. if f.sendRecvErrorConfig.ShouldSendRecvError(endpoint) {
  614. f.sendRecvError(endpoint, index)
  615. }
  616. }
  617. func (f *Interface) sendRecvError(endpoint netip.AddrPort, index uint32) {
  618. f.messageMetrics.Tx(header.RecvError, 0, 1)
  619. b := header.Encode(make([]byte, header.Len), header.Version, header.RecvError, 0, index, 0)
  620. _ = f.outside.WriteTo(b, endpoint)
  621. if f.l.Level >= logrus.DebugLevel {
  622. f.l.WithField("index", index).
  623. WithField("udpAddr", endpoint).
  624. Debug("Recv error sent")
  625. }
  626. }
  627. func (f *Interface) handleRecvError(addr netip.AddrPort, h *header.H) {
  628. if f.l.Level >= logrus.DebugLevel {
  629. f.l.WithField("index", h.RemoteIndex).
  630. WithField("udpAddr", addr).
  631. Debug("Recv error received")
  632. }
  633. hostinfo := f.hostMap.QueryReverseIndex(h.RemoteIndex)
  634. if hostinfo == nil {
  635. f.l.WithField("remoteIndex", h.RemoteIndex).Debugln("Did not find remote index in main hostmap")
  636. return
  637. }
  638. if hostinfo.remote.IsValid() && hostinfo.remote != addr {
  639. f.l.Infoln("Someone spoofing recv_errors? ", addr, hostinfo.remote)
  640. return
  641. }
  642. f.closeTunnel(hostinfo)
  643. // We also delete it from pending hostmap to allow for fast reconnect.
  644. f.handshakeManager.DeleteHostInfo(hostinfo)
  645. }