handshake_ix.go 19 KB

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