handshake_ix.go 22 KB

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