handshake_ix.go 19 KB

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