handshake_ix.go 19 KB

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