handshake_ix.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. package nebula
  2. import (
  3. "net/netip"
  4. "slices"
  5. "time"
  6. "github.com/flynn/noise"
  7. "github.com/sirupsen/logrus"
  8. "github.com/slackhq/nebula/cert"
  9. "github.com/slackhq/nebula/header"
  10. "github.com/wadey/synctrace"
  11. )
  12. // NOISE IX Handshakes
  13. // This function constructs a handshake packet, but does not actually send it
  14. // Sending is done by the handshake manager
  15. func ixHandshakeStage0(f *Interface, hh *HandshakeHostInfo) bool {
  16. err := f.handshakeManager.allocateIndex(hh)
  17. if err != nil {
  18. f.l.WithError(err).WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  19. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to generate index")
  20. return false
  21. }
  22. // If we're connecting to a v6 address we must use a v2 cert
  23. cs := f.pki.getCertState()
  24. v := cs.initiatingVersion
  25. for _, a := range hh.hostinfo.vpnAddrs {
  26. if a.Is6() {
  27. v = cert.Version2
  28. break
  29. }
  30. }
  31. crt := cs.getCertificate(v)
  32. if crt == nil {
  33. f.l.WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  34. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).
  35. WithField("certVersion", v).
  36. Error("Unable to handshake with host because no certificate is available")
  37. return false
  38. }
  39. crtHs := cs.getHandshakeBytes(v)
  40. if crtHs == nil {
  41. f.l.WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  42. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).
  43. WithField("certVersion", v).
  44. Error("Unable to handshake with host because no certificate handshake bytes is available")
  45. }
  46. ci, err := NewConnectionState(f.l, cs, crt, true, noise.HandshakeIX)
  47. if err != nil {
  48. f.l.WithError(err).WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  49. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).
  50. WithField("certVersion", v).
  51. Error("Failed to create connection state")
  52. return false
  53. }
  54. hh.hostinfo.ConnectionState = ci
  55. hs := &NebulaHandshake{
  56. Details: &NebulaHandshakeDetails{
  57. InitiatorIndex: hh.hostinfo.localIndexId,
  58. Time: uint64(time.Now().UnixNano()),
  59. Cert: crtHs,
  60. CertVersion: uint32(v),
  61. },
  62. }
  63. hsBytes, err := hs.Marshal()
  64. if err != nil {
  65. f.l.WithError(err).WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  66. WithField("certVersion", v).
  67. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  68. return false
  69. }
  70. h := header.Encode(make([]byte, header.Len), header.Version, header.Handshake, header.HandshakeIXPSK0, 0, 1)
  71. msg, _, _, err := ci.H.WriteMessage(h, hsBytes)
  72. if err != nil {
  73. f.l.WithError(err).WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  74. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  75. return false
  76. }
  77. // We are sending handshake packet 1, so we don't expect to receive
  78. // handshake packet 1 from the responder
  79. ci.window.Update(f.l, 1)
  80. hh.hostinfo.HandshakePacket[0] = msg
  81. hh.ready = true
  82. return true
  83. }
  84. func ixHandshakeStage1(f *Interface, addr netip.AddrPort, via *ViaSender, packet []byte, h *header.H) {
  85. cs := f.pki.getCertState()
  86. crt := cs.GetDefaultCertificate()
  87. if crt == nil {
  88. f.l.WithField("udpAddr", addr).
  89. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).
  90. WithField("certVersion", cs.initiatingVersion).
  91. Error("Unable to handshake with host because no certificate is available")
  92. }
  93. ci, err := NewConnectionState(f.l, cs, crt, false, noise.HandshakeIX)
  94. if err != nil {
  95. f.l.WithError(err).WithField("udpAddr", addr).
  96. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  97. Error("Failed to create connection state")
  98. return
  99. }
  100. // Mark packet 1 as seen so it doesn't show up as missed
  101. ci.window.Update(f.l, 1)
  102. msg, _, _, err := ci.H.ReadMessage(nil, packet[header.Len:])
  103. if err != nil {
  104. f.l.WithError(err).WithField("udpAddr", addr).
  105. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  106. Error("Failed to call noise.ReadMessage")
  107. return
  108. }
  109. hs := &NebulaHandshake{}
  110. err = hs.Unmarshal(msg)
  111. if err != nil || hs.Details == nil {
  112. f.l.WithError(err).WithField("udpAddr", addr).
  113. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  114. Error("Failed unmarshal handshake message")
  115. return
  116. }
  117. rc, err := cert.Recombine(cert.Version(hs.Details.CertVersion), hs.Details.Cert, ci.H.PeerStatic(), ci.Curve())
  118. if err != nil {
  119. f.l.WithError(err).WithField("udpAddr", addr).
  120. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  121. Info("Handshake did not contain a certificate")
  122. return
  123. }
  124. remoteCert, err := f.pki.GetCAPool().VerifyCertificate(time.Now(), rc)
  125. if err != nil {
  126. fp, err := rc.Fingerprint()
  127. if err != nil {
  128. fp = "<error generating certificate fingerprint>"
  129. }
  130. e := f.l.WithError(err).WithField("udpAddr", addr).
  131. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  132. WithField("certVpnNetworks", rc.Networks()).
  133. WithField("certFingerprint", fp)
  134. if f.l.Level >= logrus.DebugLevel {
  135. e = e.WithField("cert", rc)
  136. }
  137. e.Info("Invalid certificate from host")
  138. return
  139. }
  140. if remoteCert.Certificate.Version() != ci.myCert.Version() {
  141. // We started off using the wrong certificate version, lets see if we can match the version that was sent to us
  142. rc := cs.getCertificate(remoteCert.Certificate.Version())
  143. if rc == nil {
  144. f.l.WithError(err).WithField("udpAddr", addr).
  145. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).WithField("cert", remoteCert).
  146. Info("Unable to handshake with host due to missing certificate version")
  147. return
  148. }
  149. // Record the certificate we are actually using
  150. ci.myCert = rc
  151. }
  152. if len(remoteCert.Certificate.Networks()) == 0 {
  153. f.l.WithError(err).WithField("udpAddr", addr).
  154. WithField("cert", remoteCert).
  155. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  156. Info("No networks in certificate")
  157. return
  158. }
  159. var vpnAddrs []netip.Addr
  160. var filteredNetworks []netip.Prefix
  161. certName := remoteCert.Certificate.Name()
  162. certVersion := remoteCert.Certificate.Version()
  163. fingerprint := remoteCert.Fingerprint
  164. issuer := remoteCert.Certificate.Issuer()
  165. for _, network := range remoteCert.Certificate.Networks() {
  166. vpnAddr := network.Addr()
  167. if f.myVpnAddrsTable.Contains(vpnAddr) {
  168. f.l.WithField("vpnAddr", vpnAddr).WithField("udpAddr", addr).
  169. WithField("certName", certName).
  170. WithField("certVersion", certVersion).
  171. WithField("fingerprint", fingerprint).
  172. WithField("issuer", issuer).
  173. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Refusing to handshake with myself")
  174. return
  175. }
  176. // vpnAddrs outside our vpn networks are of no use to us, filter them out
  177. if !f.myVpnNetworksTable.Contains(vpnAddr) {
  178. continue
  179. }
  180. filteredNetworks = append(filteredNetworks, network)
  181. vpnAddrs = append(vpnAddrs, vpnAddr)
  182. }
  183. if len(vpnAddrs) == 0 {
  184. f.l.WithError(err).WithField("udpAddr", addr).
  185. WithField("certName", certName).
  186. WithField("certVersion", certVersion).
  187. WithField("fingerprint", fingerprint).
  188. WithField("issuer", issuer).
  189. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("No usable vpn addresses from host, refusing handshake")
  190. return
  191. }
  192. if addr.IsValid() {
  193. // addr can be invalid when the tunnel is being relayed.
  194. // We only want to apply the remote allow list for direct tunnels here
  195. if !f.lightHouse.GetRemoteAllowList().AllowAll(vpnAddrs, addr.Addr()) {
  196. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).Debug("lighthouse.remote_allow_list denied incoming handshake")
  197. return
  198. }
  199. }
  200. myIndex, err := generateIndex(f.l)
  201. if err != nil {
  202. f.l.WithError(err).WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  203. WithField("certName", certName).
  204. WithField("certVersion", certVersion).
  205. WithField("fingerprint", fingerprint).
  206. WithField("issuer", issuer).
  207. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to generate index")
  208. return
  209. }
  210. hostinfo := &HostInfo{
  211. ConnectionState: ci,
  212. localIndexId: myIndex,
  213. remoteIndexId: hs.Details.InitiatorIndex,
  214. vpnAddrs: vpnAddrs,
  215. HandshakePacket: make(map[uint8][]byte, 0),
  216. lastHandshakeTime: hs.Details.Time,
  217. relayState: RelayState{
  218. RWMutex: synctrace.NewRWMutex("relay-state"),
  219. relays: map[netip.Addr]struct{}{},
  220. relayForByAddr: map[netip.Addr]*Relay{},
  221. relayForByIdx: map[uint32]*Relay{},
  222. },
  223. }
  224. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  225. WithField("certName", certName).
  226. WithField("certVersion", certVersion).
  227. WithField("fingerprint", fingerprint).
  228. WithField("issuer", issuer).
  229. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  230. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  231. Info("Handshake message received")
  232. hs.Details.ResponderIndex = myIndex
  233. hs.Details.Cert = cs.getHandshakeBytes(ci.myCert.Version())
  234. if hs.Details.Cert == nil {
  235. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  236. WithField("certName", certName).
  237. WithField("certVersion", certVersion).
  238. WithField("fingerprint", fingerprint).
  239. WithField("issuer", issuer).
  240. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  241. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  242. WithField("certVersion", ci.myCert.Version()).
  243. Error("Unable to handshake with host because no certificate handshake bytes is available")
  244. return
  245. }
  246. hs.Details.CertVersion = uint32(ci.myCert.Version())
  247. // Update the time in case their clock is way off from ours
  248. hs.Details.Time = uint64(time.Now().UnixNano())
  249. hsBytes, err := hs.Marshal()
  250. if err != nil {
  251. f.l.WithError(err).WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  252. WithField("certName", certName).
  253. WithField("certVersion", certVersion).
  254. WithField("fingerprint", fingerprint).
  255. WithField("issuer", issuer).
  256. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  257. return
  258. }
  259. nh := header.Encode(make([]byte, header.Len), header.Version, header.Handshake, header.HandshakeIXPSK0, hs.Details.InitiatorIndex, 2)
  260. msg, dKey, eKey, err := ci.H.WriteMessage(nh, hsBytes)
  261. if err != nil {
  262. f.l.WithError(err).WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  263. WithField("certName", certName).
  264. WithField("certVersion", certVersion).
  265. WithField("fingerprint", fingerprint).
  266. WithField("issuer", issuer).
  267. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  268. return
  269. } else if dKey == nil || eKey == nil {
  270. f.l.WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  271. WithField("certName", certName).
  272. WithField("certVersion", certVersion).
  273. WithField("fingerprint", fingerprint).
  274. WithField("issuer", issuer).
  275. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Noise did not arrive at a key")
  276. return
  277. }
  278. hostinfo.HandshakePacket[0] = make([]byte, len(packet[header.Len:]))
  279. copy(hostinfo.HandshakePacket[0], packet[header.Len:])
  280. // Regardless of whether you are the sender or receiver, you should arrive here
  281. // and complete standing up the connection.
  282. hostinfo.HandshakePacket[2] = make([]byte, len(msg))
  283. copy(hostinfo.HandshakePacket[2], msg)
  284. // We are sending handshake packet 2, so we don't expect to receive
  285. // handshake packet 2 from the initiator.
  286. ci.window.Update(f.l, 2)
  287. ci.peerCert = remoteCert
  288. ci.dKey = NewNebulaCipherState(dKey)
  289. ci.eKey = NewNebulaCipherState(eKey)
  290. hostinfo.remotes = f.lightHouse.QueryCache(vpnAddrs)
  291. hostinfo.SetRemote(addr)
  292. hostinfo.buildNetworks(filteredNetworks, remoteCert.Certificate.UnsafeNetworks())
  293. existing, err := f.handshakeManager.CheckAndComplete(hostinfo, 0, f)
  294. if err != nil {
  295. switch err {
  296. case ErrAlreadySeen:
  297. // Update remote if preferred
  298. if existing.SetRemoteIfPreferred(f.hostMap, addr) {
  299. // Send a test packet to ensure the other side has also switched to
  300. // the preferred remote
  301. f.SendMessageToVpnAddr(header.Test, header.TestRequest, vpnAddrs[0], []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  302. }
  303. msg = existing.HandshakePacket[2]
  304. f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
  305. if addr.IsValid() {
  306. err := f.outside.WriteTo(msg, addr)
  307. if err != nil {
  308. f.l.WithField("vpnAddrs", existing.vpnAddrs).WithField("udpAddr", addr).
  309. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  310. WithError(err).Error("Failed to send handshake message")
  311. } else {
  312. f.l.WithField("vpnAddrs", existing.vpnAddrs).WithField("udpAddr", addr).
  313. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  314. Info("Handshake message sent")
  315. }
  316. return
  317. } else {
  318. if via == nil {
  319. f.l.Error("Handshake send failed: both addr and via are nil.")
  320. return
  321. }
  322. hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
  323. f.SendVia(via.relayHI, via.relay, msg, make([]byte, 12), make([]byte, mtu), false)
  324. f.l.WithField("vpnAddrs", existing.vpnAddrs).WithField("relay", via.relayHI.vpnAddrs[0]).
  325. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  326. Info("Handshake message sent")
  327. return
  328. }
  329. case ErrExistingHostInfo:
  330. // This means there was an existing tunnel and this handshake was older than the one we are currently based on
  331. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  332. WithField("certName", certName).
  333. WithField("certVersion", certVersion).
  334. WithField("oldHandshakeTime", existing.lastHandshakeTime).
  335. WithField("newHandshakeTime", hostinfo.lastHandshakeTime).
  336. WithField("fingerprint", fingerprint).
  337. WithField("issuer", issuer).
  338. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  339. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  340. Info("Handshake too old")
  341. // Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
  342. f.SendMessageToVpnAddr(header.Test, header.TestRequest, vpnAddrs[0], []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  343. return
  344. case ErrLocalIndexCollision:
  345. // This means we failed to insert because of collision on localIndexId. Just let the next handshake packet retry
  346. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  347. WithField("certName", certName).
  348. WithField("certVersion", certVersion).
  349. WithField("fingerprint", fingerprint).
  350. WithField("issuer", issuer).
  351. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  352. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  353. WithField("localIndex", hostinfo.localIndexId).WithField("collision", existing.vpnAddrs).
  354. Error("Failed to add HostInfo due to localIndex collision")
  355. return
  356. default:
  357. // Shouldn't happen, but just in case someone adds a new error type to CheckAndComplete
  358. // And we forget to update it here
  359. f.l.WithError(err).WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  360. WithField("certName", certName).
  361. WithField("certVersion", certVersion).
  362. WithField("fingerprint", fingerprint).
  363. WithField("issuer", issuer).
  364. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  365. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  366. Error("Failed to add HostInfo to HostMap")
  367. return
  368. }
  369. }
  370. // Do the send
  371. f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
  372. if addr.IsValid() {
  373. err = f.outside.WriteTo(msg, addr)
  374. if err != nil {
  375. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  376. WithField("certName", certName).
  377. WithField("certVersion", certVersion).
  378. WithField("fingerprint", fingerprint).
  379. WithField("issuer", issuer).
  380. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  381. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  382. WithError(err).Error("Failed to send handshake")
  383. } else {
  384. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  385. WithField("certName", certName).
  386. WithField("certVersion", certVersion).
  387. WithField("fingerprint", fingerprint).
  388. WithField("issuer", issuer).
  389. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  390. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  391. Info("Handshake message sent")
  392. }
  393. } else {
  394. if via == nil {
  395. f.l.Error("Handshake send failed: both addr and via are nil.")
  396. return
  397. }
  398. hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
  399. // I successfully received a handshake. Just in case I marked this tunnel as 'Disestablished', ensure
  400. // it's correctly marked as working.
  401. via.relayHI.relayState.UpdateRelayForByIdxState(via.remoteIdx, Established)
  402. f.SendVia(via.relayHI, via.relay, msg, make([]byte, 12), make([]byte, mtu), false)
  403. f.l.WithField("vpnAddrs", vpnAddrs).WithField("relay", via.relayHI.vpnAddrs[0]).
  404. WithField("certName", certName).
  405. WithField("certVersion", certVersion).
  406. WithField("fingerprint", fingerprint).
  407. WithField("issuer", issuer).
  408. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  409. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  410. Info("Handshake message sent")
  411. }
  412. f.connectionManager.AddTrafficWatch(hostinfo.localIndexId)
  413. hostinfo.remotes.ResetBlockedRemotes()
  414. return
  415. }
  416. func ixHandshakeStage2(f *Interface, addr netip.AddrPort, via *ViaSender, hh *HandshakeHostInfo, packet []byte, h *header.H) bool {
  417. if hh == nil {
  418. // Nothing here to tear down, got a bogus stage 2 packet
  419. return true
  420. }
  421. hh.Lock()
  422. defer hh.Unlock()
  423. hostinfo := hh.hostinfo
  424. if addr.IsValid() {
  425. // The vpnAddr we know about is the one we tried to handshake with, use it to apply the remote allow list.
  426. if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, addr.Addr()) {
  427. f.l.WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).Debug("lighthouse.remote_allow_list denied incoming handshake")
  428. return false
  429. }
  430. }
  431. ci := hostinfo.ConnectionState
  432. msg, eKey, dKey, err := ci.H.ReadMessage(nil, packet[header.Len:])
  433. if err != nil {
  434. f.l.WithError(err).WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  435. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  436. Error("Failed to call noise.ReadMessage")
  437. // We don't want to tear down the connection on a bad ReadMessage because it could be an attacker trying
  438. // to DOS us. Every other error condition after should to allow a possible good handshake to complete in the
  439. // near future
  440. return false
  441. } else if dKey == nil || eKey == nil {
  442. f.l.WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  443. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  444. Error("Noise did not arrive at a key")
  445. // This should be impossible in IX but just in case, if we get here then there is no chance to recover
  446. // the handshake state machine. Tear it down
  447. return true
  448. }
  449. hs := &NebulaHandshake{}
  450. err = hs.Unmarshal(msg)
  451. if err != nil || hs.Details == nil {
  452. f.l.WithError(err).WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  453. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  454. // The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
  455. return true
  456. }
  457. rc, err := cert.Recombine(cert.Version(hs.Details.CertVersion), hs.Details.Cert, ci.H.PeerStatic(), ci.Curve())
  458. if err != nil {
  459. f.l.WithError(err).WithField("udpAddr", addr).
  460. WithField("vpnAddrs", hostinfo.vpnAddrs).
  461. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  462. Info("Handshake did not contain a certificate")
  463. return true
  464. }
  465. remoteCert, err := f.pki.GetCAPool().VerifyCertificate(time.Now(), rc)
  466. if err != nil {
  467. fp, err := rc.Fingerprint()
  468. if err != nil {
  469. fp = "<error generating certificate fingerprint>"
  470. }
  471. e := f.l.WithError(err).WithField("udpAddr", addr).
  472. WithField("vpnAddrs", hostinfo.vpnAddrs).
  473. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  474. WithField("certFingerprint", fp).
  475. WithField("certVpnNetworks", rc.Networks())
  476. if f.l.Level >= logrus.DebugLevel {
  477. e = e.WithField("cert", rc)
  478. }
  479. e.Info("Invalid certificate from host")
  480. return true
  481. }
  482. if len(remoteCert.Certificate.Networks()) == 0 {
  483. f.l.WithError(err).WithField("udpAddr", addr).
  484. WithField("vpnAddrs", hostinfo.vpnAddrs).
  485. WithField("cert", remoteCert).
  486. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  487. Info("No networks in certificate")
  488. return true
  489. }
  490. vpnNetworks := remoteCert.Certificate.Networks()
  491. certName := remoteCert.Certificate.Name()
  492. certVersion := remoteCert.Certificate.Version()
  493. fingerprint := remoteCert.Fingerprint
  494. issuer := remoteCert.Certificate.Issuer()
  495. hostinfo.remoteIndexId = hs.Details.ResponderIndex
  496. hostinfo.lastHandshakeTime = hs.Details.Time
  497. // Store their cert and our symmetric keys
  498. ci.peerCert = remoteCert
  499. ci.dKey = NewNebulaCipherState(dKey)
  500. ci.eKey = NewNebulaCipherState(eKey)
  501. // Make sure the current udpAddr being used is set for responding
  502. if addr.IsValid() {
  503. hostinfo.SetRemote(addr)
  504. } else {
  505. hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
  506. }
  507. var vpnAddrs []netip.Addr
  508. var filteredNetworks []netip.Prefix
  509. for _, network := range vpnNetworks {
  510. // vpnAddrs outside our vpn networks are of no use to us, filter them out
  511. vpnAddr := network.Addr()
  512. if !f.myVpnNetworksTable.Contains(vpnAddr) {
  513. continue
  514. }
  515. filteredNetworks = append(filteredNetworks, network)
  516. vpnAddrs = append(vpnAddrs, vpnAddr)
  517. }
  518. if len(vpnAddrs) == 0 {
  519. f.l.WithError(err).WithField("udpAddr", addr).
  520. WithField("certName", certName).
  521. WithField("certVersion", certVersion).
  522. WithField("fingerprint", fingerprint).
  523. WithField("issuer", issuer).
  524. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("No usable vpn addresses from host, refusing handshake")
  525. return true
  526. }
  527. // Ensure the right host responded
  528. if !slices.Contains(vpnAddrs, hostinfo.vpnAddrs[0]) {
  529. f.l.WithField("intendedVpnAddrs", hostinfo.vpnAddrs).WithField("haveVpnNetworks", vpnNetworks).
  530. WithField("udpAddr", addr).
  531. WithField("certName", certName).
  532. WithField("certVersion", certVersion).
  533. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  534. Info("Incorrect host responded to handshake")
  535. // Release our old handshake from pending, it should not continue
  536. f.handshakeManager.DeleteHostInfo(hostinfo)
  537. // Create a new hostinfo/handshake for the intended vpn ip
  538. f.handshakeManager.StartHandshake(hostinfo.vpnAddrs[0], func(newHH *HandshakeHostInfo) {
  539. // Block the current used address
  540. newHH.hostinfo.remotes = hostinfo.remotes
  541. newHH.hostinfo.remotes.BlockRemote(addr)
  542. f.l.WithField("blockedUdpAddrs", newHH.hostinfo.remotes.CopyBlockedRemotes()).
  543. WithField("vpnNetworks", vpnNetworks).
  544. WithField("remotes", newHH.hostinfo.remotes.CopyAddrs(f.hostMap.GetPreferredRanges())).
  545. Info("Blocked addresses for handshakes")
  546. // Swap the packet store to benefit the original intended recipient
  547. newHH.packetStore = hh.packetStore
  548. hh.packetStore = []*cachedPacket{}
  549. // Finally, put the correct vpn addrs in the host info, tell them to close the tunnel, and return true to tear down
  550. hostinfo.vpnAddrs = vpnAddrs
  551. f.sendCloseTunnel(hostinfo)
  552. })
  553. return true
  554. }
  555. // Mark packet 2 as seen so it doesn't show up as missed
  556. ci.window.Update(f.l, 2)
  557. duration := time.Since(hh.startTime).Nanoseconds()
  558. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  559. WithField("certName", certName).
  560. WithField("certVersion", certVersion).
  561. WithField("fingerprint", fingerprint).
  562. WithField("issuer", issuer).
  563. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  564. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  565. WithField("durationNs", duration).
  566. WithField("sentCachedPackets", len(hh.packetStore)).
  567. Info("Handshake message received")
  568. // Build up the radix for the firewall if we have subnets in the cert
  569. hostinfo.vpnAddrs = vpnAddrs
  570. hostinfo.buildNetworks(filteredNetworks, remoteCert.Certificate.UnsafeNetworks())
  571. // Complete our handshake and update metrics, this will replace any existing tunnels for the vpnAddrs here
  572. f.handshakeManager.Complete(hostinfo, f)
  573. f.connectionManager.AddTrafficWatch(hostinfo.localIndexId)
  574. if f.l.Level >= logrus.DebugLevel {
  575. hostinfo.logger(f.l).Debugf("Sending %d stored packets", len(hh.packetStore))
  576. }
  577. if len(hh.packetStore) > 0 {
  578. nb := make([]byte, 12, 12)
  579. out := make([]byte, mtu)
  580. for _, cp := range hh.packetStore {
  581. cp.callback(cp.messageType, cp.messageSubType, hostinfo, cp.packet, nb, out)
  582. }
  583. f.cachedPacketMetrics.sent.Inc(int64(len(hh.packetStore)))
  584. }
  585. hostinfo.remotes.ResetBlockedRemotes()
  586. f.metricHandshakes.Update(duration)
  587. return false
  588. }