handshake_ix.go 22 KB

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