handshake_ix.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. package nebula
  2. import (
  3. "time"
  4. "github.com/flynn/noise"
  5. "github.com/slackhq/nebula/header"
  6. "github.com/slackhq/nebula/iputil"
  7. "github.com/slackhq/nebula/udp"
  8. )
  9. // NOISE IX Handshakes
  10. // This function constructs a handshake packet, but does not actually send it
  11. // Sending is done by the handshake manager
  12. func ixHandshakeStage0(f *Interface, vpnIp iputil.VpnIp, hostinfo *HostInfo) {
  13. // This queries the lighthouse if we don't know a remote for the host
  14. // We do it here to provoke the lighthouse to preempt our timer wheel and trigger the stage 1 packet to send
  15. // more quickly, effect is a quicker handshake.
  16. if hostinfo.remote == nil {
  17. f.lightHouse.QueryServer(vpnIp, f)
  18. }
  19. err := f.handshakeManager.AddIndexHostInfo(hostinfo)
  20. if err != nil {
  21. f.l.WithError(err).WithField("vpnIp", vpnIp).
  22. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to generate index")
  23. return
  24. }
  25. ci := hostinfo.ConnectionState
  26. hsProto := &NebulaHandshakeDetails{
  27. InitiatorIndex: hostinfo.localIndexId,
  28. Time: uint64(time.Now().UnixNano()),
  29. Cert: ci.certState.rawCertificateNoKey,
  30. }
  31. hsBytes := []byte{}
  32. hs := &NebulaHandshake{
  33. Details: hsProto,
  34. }
  35. hsBytes, err = hs.Marshal()
  36. if err != nil {
  37. f.l.WithError(err).WithField("vpnIp", vpnIp).
  38. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  39. return
  40. }
  41. h := header.Encode(make([]byte, header.Len), header.Version, header.Handshake, header.HandshakeIXPSK0, 0, 1)
  42. ci.messageCounter.Add(1)
  43. msg, _, _, err := ci.H.WriteMessage(h, hsBytes)
  44. if err != nil {
  45. f.l.WithError(err).WithField("vpnIp", vpnIp).
  46. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  47. return
  48. }
  49. // We are sending handshake packet 1, so we don't expect to receive
  50. // handshake packet 1 from the responder
  51. ci.window.Update(f.l, 1)
  52. hostinfo.HandshakePacket[0] = msg
  53. hostinfo.HandshakeReady = true
  54. hostinfo.handshakeStart = time.Now()
  55. }
  56. func ixHandshakeStage1(f *Interface, addr *udp.Addr, via interface{}, packet []byte, h *header.H) {
  57. ci := f.newConnectionState(f.l, false, noise.HandshakeIX, []byte{}, 0)
  58. // Mark packet 1 as seen so it doesn't show up as missed
  59. ci.window.Update(f.l, 1)
  60. msg, _, _, err := ci.H.ReadMessage(nil, packet[header.Len:])
  61. if err != nil {
  62. f.l.WithError(err).WithField("udpAddr", addr).
  63. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.ReadMessage")
  64. return
  65. }
  66. hs := &NebulaHandshake{}
  67. err = hs.Unmarshal(msg)
  68. /*
  69. l.Debugln("GOT INDEX: ", hs.Details.InitiatorIndex)
  70. */
  71. if err != nil || hs.Details == nil {
  72. f.l.WithError(err).WithField("udpAddr", addr).
  73. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  74. return
  75. }
  76. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert, f.caPool)
  77. if err != nil {
  78. f.l.WithError(err).WithField("udpAddr", addr).
  79. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).WithField("cert", remoteCert).
  80. Info("Invalid certificate from host")
  81. return
  82. }
  83. vpnIp := iputil.Ip2VpnIp(remoteCert.Details.Ips[0].IP)
  84. certName := remoteCert.Details.Name
  85. fingerprint, _ := remoteCert.Sha256Sum()
  86. issuer := remoteCert.Details.Issuer
  87. if vpnIp == f.myVpnIp {
  88. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  89. WithField("certName", certName).
  90. WithField("fingerprint", fingerprint).
  91. WithField("issuer", issuer).
  92. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Refusing to handshake with myself")
  93. return
  94. }
  95. if addr != nil {
  96. if !f.lightHouse.GetRemoteAllowList().Allow(vpnIp, addr.IP) {
  97. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).Debug("lighthouse.remote_allow_list denied incoming handshake")
  98. return
  99. }
  100. }
  101. myIndex, err := generateIndex(f.l)
  102. if err != nil {
  103. f.l.WithError(err).WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  104. WithField("certName", certName).
  105. WithField("fingerprint", fingerprint).
  106. WithField("issuer", issuer).
  107. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to generate index")
  108. return
  109. }
  110. hostinfo := &HostInfo{
  111. ConnectionState: ci,
  112. localIndexId: myIndex,
  113. remoteIndexId: hs.Details.InitiatorIndex,
  114. vpnIp: vpnIp,
  115. HandshakePacket: make(map[uint8][]byte, 0),
  116. lastHandshakeTime: hs.Details.Time,
  117. relayState: RelayState{
  118. relays: map[iputil.VpnIp]struct{}{},
  119. relayForByIp: map[iputil.VpnIp]*Relay{},
  120. relayForByIdx: map[uint32]*Relay{},
  121. },
  122. }
  123. hostinfo.Lock()
  124. defer hostinfo.Unlock()
  125. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  126. WithField("certName", certName).
  127. WithField("fingerprint", fingerprint).
  128. WithField("issuer", issuer).
  129. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  130. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  131. Info("Handshake message received")
  132. hs.Details.ResponderIndex = myIndex
  133. hs.Details.Cert = ci.certState.rawCertificateNoKey
  134. // Update the time in case their clock is way off from ours
  135. hs.Details.Time = uint64(time.Now().UnixNano())
  136. hsBytes, err := hs.Marshal()
  137. if err != nil {
  138. f.l.WithError(err).WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).
  139. WithField("certName", certName).
  140. WithField("fingerprint", fingerprint).
  141. WithField("issuer", issuer).
  142. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  143. return
  144. }
  145. nh := header.Encode(make([]byte, header.Len), header.Version, header.Handshake, header.HandshakeIXPSK0, hs.Details.InitiatorIndex, 2)
  146. msg, dKey, eKey, err := ci.H.WriteMessage(nh, hsBytes)
  147. if err != nil {
  148. f.l.WithError(err).WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).
  149. WithField("certName", certName).
  150. WithField("fingerprint", fingerprint).
  151. WithField("issuer", issuer).
  152. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  153. return
  154. } else if dKey == nil || eKey == nil {
  155. f.l.WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).
  156. WithField("certName", certName).
  157. WithField("fingerprint", fingerprint).
  158. WithField("issuer", issuer).
  159. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Noise did not arrive at a key")
  160. return
  161. }
  162. hostinfo.HandshakePacket[0] = make([]byte, len(packet[header.Len:]))
  163. copy(hostinfo.HandshakePacket[0], packet[header.Len:])
  164. // Regardless of whether you are the sender or receiver, you should arrive here
  165. // and complete standing up the connection.
  166. hostinfo.HandshakePacket[2] = make([]byte, len(msg))
  167. copy(hostinfo.HandshakePacket[2], msg)
  168. // We are sending handshake packet 2, so we don't expect to receive
  169. // handshake packet 2 from the initiator.
  170. ci.window.Update(f.l, 2)
  171. ci.peerCert = remoteCert
  172. ci.dKey = NewNebulaCipherState(dKey)
  173. ci.eKey = NewNebulaCipherState(eKey)
  174. hostinfo.remotes = f.lightHouse.QueryCache(vpnIp)
  175. hostinfo.SetRemote(addr)
  176. hostinfo.CreateRemoteCIDR(remoteCert)
  177. // Only overwrite existing record if we should win the handshake race
  178. overwrite := vpnIp > f.myVpnIp
  179. existing, err := f.handshakeManager.CheckAndComplete(hostinfo, 0, overwrite, f)
  180. if err != nil {
  181. switch err {
  182. case ErrAlreadySeen:
  183. // Update remote if preferred (Note we have to switch to locking
  184. // the existing hostinfo, and then switch back so the defer Unlock
  185. // higher in this function still works)
  186. hostinfo.Unlock()
  187. existing.Lock()
  188. // Update remote if preferred
  189. if existing.SetRemoteIfPreferred(f.hostMap, addr) {
  190. // Send a test packet to ensure the other side has also switched to
  191. // the preferred remote
  192. f.SendMessageToVpnIp(header.Test, header.TestRequest, vpnIp, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  193. }
  194. existing.Unlock()
  195. hostinfo.Lock()
  196. msg = existing.HandshakePacket[2]
  197. f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
  198. if addr != nil {
  199. err := f.outside.WriteTo(msg, addr)
  200. if err != nil {
  201. f.l.WithField("vpnIp", existing.vpnIp).WithField("udpAddr", addr).
  202. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  203. WithError(err).Error("Failed to send handshake message")
  204. } else {
  205. f.l.WithField("vpnIp", existing.vpnIp).WithField("udpAddr", addr).
  206. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  207. Info("Handshake message sent")
  208. }
  209. return
  210. } else {
  211. via2 := via.(*ViaSender)
  212. if via2 == nil {
  213. f.l.Error("Handshake send failed: both addr and via are nil.")
  214. return
  215. }
  216. hostinfo.relayState.InsertRelayTo(via2.relayHI.vpnIp)
  217. f.SendVia(via2.relayHI, via2.relay, msg, make([]byte, 12), make([]byte, mtu), false)
  218. f.l.WithField("vpnIp", existing.vpnIp).WithField("relay", via2.relayHI.vpnIp).
  219. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  220. Info("Handshake message sent")
  221. return
  222. }
  223. case ErrExistingHostInfo:
  224. // This means there was an existing tunnel and this handshake was older than the one we are currently based on
  225. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  226. WithField("certName", certName).
  227. WithField("oldHandshakeTime", existing.lastHandshakeTime).
  228. WithField("newHandshakeTime", hostinfo.lastHandshakeTime).
  229. WithField("fingerprint", fingerprint).
  230. WithField("issuer", issuer).
  231. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  232. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  233. Info("Handshake too old")
  234. // Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
  235. f.SendMessageToVpnIp(header.Test, header.TestRequest, vpnIp, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  236. return
  237. case ErrLocalIndexCollision:
  238. // This means we failed to insert because of collision on localIndexId. Just let the next handshake packet retry
  239. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  240. WithField("certName", certName).
  241. WithField("fingerprint", fingerprint).
  242. WithField("issuer", issuer).
  243. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  244. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  245. WithField("localIndex", hostinfo.localIndexId).WithField("collision", existing.vpnIp).
  246. Error("Failed to add HostInfo due to localIndex collision")
  247. return
  248. case ErrExistingHandshake:
  249. // We have a race where both parties think they are an initiator and this tunnel lost, let the other one finish
  250. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  251. WithField("certName", certName).
  252. WithField("fingerprint", fingerprint).
  253. WithField("issuer", issuer).
  254. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  255. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  256. Error("Prevented a pending handshake race")
  257. return
  258. default:
  259. // Shouldn't happen, but just in case someone adds a new error type to CheckAndComplete
  260. // And we forget to update it here
  261. f.l.WithError(err).WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  262. WithField("certName", certName).
  263. WithField("fingerprint", fingerprint).
  264. WithField("issuer", issuer).
  265. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  266. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  267. Error("Failed to add HostInfo to HostMap")
  268. return
  269. }
  270. }
  271. // Do the send
  272. f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
  273. if addr != nil {
  274. err = f.outside.WriteTo(msg, addr)
  275. if err != nil {
  276. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  277. WithField("certName", certName).
  278. WithField("fingerprint", fingerprint).
  279. WithField("issuer", issuer).
  280. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  281. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  282. WithError(err).Error("Failed to send handshake")
  283. } else {
  284. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  285. WithField("certName", certName).
  286. WithField("fingerprint", fingerprint).
  287. WithField("issuer", issuer).
  288. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  289. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  290. WithField("sentCachedPackets", len(hostinfo.packetStore)).
  291. Info("Handshake message sent")
  292. }
  293. } else {
  294. via2 := via.(*ViaSender)
  295. if via2 == nil {
  296. f.l.Error("Handshake send failed: both addr and via are nil.")
  297. return
  298. }
  299. hostinfo.relayState.InsertRelayTo(via2.relayHI.vpnIp)
  300. f.SendVia(via2.relayHI, via2.relay, msg, make([]byte, 12), make([]byte, mtu), false)
  301. f.l.WithField("vpnIp", vpnIp).WithField("relay", via2.relayHI.vpnIp).
  302. WithField("certName", certName).
  303. WithField("fingerprint", fingerprint).
  304. WithField("issuer", issuer).
  305. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  306. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  307. WithField("sentCachedPackets", len(hostinfo.packetStore)).
  308. Info("Handshake message sent")
  309. }
  310. hostinfo.handshakeComplete(f.l, f.cachedPacketMetrics)
  311. return
  312. }
  313. func ixHandshakeStage2(f *Interface, addr *udp.Addr, via interface{}, hostinfo *HostInfo, packet []byte, h *header.H) bool {
  314. if hostinfo == nil {
  315. // Nothing here to tear down, got a bogus stage 2 packet
  316. return true
  317. }
  318. hostinfo.Lock()
  319. defer hostinfo.Unlock()
  320. if addr != nil {
  321. if !f.lightHouse.GetRemoteAllowList().Allow(hostinfo.vpnIp, addr.IP) {
  322. f.l.WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).Debug("lighthouse.remote_allow_list denied incoming handshake")
  323. return false
  324. }
  325. }
  326. ci := hostinfo.ConnectionState
  327. if ci.ready {
  328. f.l.WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).
  329. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  330. Info("Handshake is already complete")
  331. // Update remote if preferred
  332. if hostinfo.SetRemoteIfPreferred(f.hostMap, addr) {
  333. // Send a test packet to ensure the other side has also switched to
  334. // the preferred remote
  335. f.SendMessageToVpnIp(header.Test, header.TestRequest, hostinfo.vpnIp, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  336. }
  337. // We already have a complete tunnel, there is nothing that can be done by processing further stage 1 packets
  338. return false
  339. }
  340. msg, eKey, dKey, err := ci.H.ReadMessage(nil, packet[header.Len:])
  341. if err != nil {
  342. f.l.WithError(err).WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).
  343. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  344. Error("Failed to call noise.ReadMessage")
  345. // We don't want to tear down the connection on a bad ReadMessage because it could be an attacker trying
  346. // to DOS us. Every other error condition after should to allow a possible good handshake to complete in the
  347. // near future
  348. return false
  349. } else if dKey == nil || eKey == nil {
  350. f.l.WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).
  351. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  352. Error("Noise did not arrive at a key")
  353. // This should be impossible in IX but just in case, if we get here then there is no chance to recover
  354. // the handshake state machine. Tear it down
  355. return true
  356. }
  357. hs := &NebulaHandshake{}
  358. err = hs.Unmarshal(msg)
  359. if err != nil || hs.Details == nil {
  360. f.l.WithError(err).WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).
  361. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  362. // The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
  363. return true
  364. }
  365. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert, f.caPool)
  366. if err != nil {
  367. f.l.WithError(err).WithField("vpnIp", hostinfo.vpnIp).WithField("udpAddr", addr).
  368. WithField("cert", remoteCert).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  369. Error("Invalid certificate from host")
  370. // The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
  371. return true
  372. }
  373. vpnIp := iputil.Ip2VpnIp(remoteCert.Details.Ips[0].IP)
  374. certName := remoteCert.Details.Name
  375. fingerprint, _ := remoteCert.Sha256Sum()
  376. issuer := remoteCert.Details.Issuer
  377. // Ensure the right host responded
  378. if vpnIp != hostinfo.vpnIp {
  379. f.l.WithField("intendedVpnIp", hostinfo.vpnIp).WithField("haveVpnIp", vpnIp).
  380. WithField("udpAddr", addr).WithField("certName", certName).
  381. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  382. Info("Incorrect host responded to handshake")
  383. // Release our old handshake from pending, it should not continue
  384. f.handshakeManager.pendingHostMap.DeleteHostInfo(hostinfo)
  385. // Create a new hostinfo/handshake for the intended vpn ip
  386. //TODO: this adds it to the timer wheel in a way that aggressively retries
  387. newHostInfo := f.getOrHandshake(hostinfo.vpnIp)
  388. newHostInfo.Lock()
  389. // Block the current used address
  390. newHostInfo.remotes = hostinfo.remotes
  391. newHostInfo.remotes.BlockRemote(addr)
  392. // Get the correct remote list for the host we did handshake with
  393. hostinfo.remotes = f.lightHouse.QueryCache(vpnIp)
  394. f.l.WithField("blockedUdpAddrs", newHostInfo.remotes.CopyBlockedRemotes()).WithField("vpnIp", vpnIp).
  395. WithField("remotes", newHostInfo.remotes.CopyAddrs(f.hostMap.preferredRanges)).
  396. Info("Blocked addresses for handshakes")
  397. // Swap the packet store to benefit the original intended recipient
  398. hostinfo.ConnectionState.queueLock.Lock()
  399. newHostInfo.packetStore = hostinfo.packetStore
  400. hostinfo.packetStore = []*cachedPacket{}
  401. hostinfo.ConnectionState.queueLock.Unlock()
  402. // Finally, put the correct vpn ip in the host info, tell them to close the tunnel, and return true to tear down
  403. hostinfo.vpnIp = vpnIp
  404. f.sendCloseTunnel(hostinfo)
  405. newHostInfo.Unlock()
  406. return true
  407. }
  408. // Mark packet 2 as seen so it doesn't show up as missed
  409. ci.window.Update(f.l, 2)
  410. duration := time.Since(hostinfo.handshakeStart).Nanoseconds()
  411. f.l.WithField("vpnIp", vpnIp).WithField("udpAddr", addr).
  412. WithField("certName", certName).
  413. WithField("fingerprint", fingerprint).
  414. WithField("issuer", issuer).
  415. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  416. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  417. WithField("durationNs", duration).
  418. WithField("sentCachedPackets", len(hostinfo.packetStore)).
  419. Info("Handshake message received")
  420. hostinfo.remoteIndexId = hs.Details.ResponderIndex
  421. hostinfo.lastHandshakeTime = hs.Details.Time
  422. // Store their cert and our symmetric keys
  423. ci.peerCert = remoteCert
  424. ci.dKey = NewNebulaCipherState(dKey)
  425. ci.eKey = NewNebulaCipherState(eKey)
  426. // Make sure the current udpAddr being used is set for responding
  427. if addr != nil {
  428. hostinfo.SetRemote(addr)
  429. } else {
  430. via2 := via.(*ViaSender)
  431. hostinfo.relayState.InsertRelayTo(via2.relayHI.vpnIp)
  432. }
  433. // Build up the radix for the firewall if we have subnets in the cert
  434. hostinfo.CreateRemoteCIDR(remoteCert)
  435. // Complete our handshake and update metrics, this will replace any existing tunnels for this vpnIp
  436. //TODO: Complete here does not do a race avoidance, it will just take the new tunnel. Is this ok?
  437. f.handshakeManager.Complete(hostinfo, f)
  438. hostinfo.handshakeComplete(f.l, f.cachedPacketMetrics)
  439. f.metricHandshakes.Update(duration)
  440. return false
  441. }