handshake_ix.go 20 KB

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