handshake_ix.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package nebula
  2. import (
  3. "sync/atomic"
  4. "time"
  5. "github.com/flynn/noise"
  6. "github.com/golang/protobuf/proto"
  7. )
  8. // NOISE IX Handshakes
  9. // This function constructs a handshake packet, but does not actually send it
  10. // Sending is done by the handshake manager
  11. func ixHandshakeStage0(f *Interface, vpnIp uint32, hostinfo *HostInfo) {
  12. // This queries the lighthouse if we don't know a remote for the host
  13. // We do it here to provoke the lighthouse to preempt our timer wheel and trigger the stage 1 packet to send
  14. // more quickly, effect is a quicker handshake.
  15. if hostinfo.remote == nil {
  16. f.lightHouse.QueryServer(vpnIp, f)
  17. }
  18. err := f.handshakeManager.AddIndexHostInfo(hostinfo)
  19. if err != nil {
  20. f.l.WithError(err).WithField("vpnIp", IntIp(vpnIp)).
  21. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to generate index")
  22. return
  23. }
  24. ci := hostinfo.ConnectionState
  25. hsProto := &NebulaHandshakeDetails{
  26. InitiatorIndex: hostinfo.localIndexId,
  27. Time: uint64(time.Now().UnixNano()),
  28. Cert: ci.certState.rawCertificateNoKey,
  29. }
  30. hsBytes := []byte{}
  31. hs := &NebulaHandshake{
  32. Details: hsProto,
  33. }
  34. hsBytes, err = proto.Marshal(hs)
  35. if err != nil {
  36. f.l.WithError(err).WithField("vpnIp", IntIp(vpnIp)).
  37. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  38. return
  39. }
  40. header := HeaderEncode(make([]byte, HeaderLen), Version, uint8(handshake), handshakeIXPSK0, 0, 1)
  41. atomic.AddUint64(&ci.atomicMessageCounter, 1)
  42. msg, _, _, err := ci.H.WriteMessage(header, hsBytes)
  43. if err != nil {
  44. f.l.WithError(err).WithField("vpnIp", IntIp(vpnIp)).
  45. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  46. return
  47. }
  48. // We are sending handshake packet 1, so we don't expect to receive
  49. // handshake packet 1 from the responder
  50. ci.window.Update(f.l, 1)
  51. hostinfo.HandshakePacket[0] = msg
  52. hostinfo.HandshakeReady = true
  53. hostinfo.handshakeStart = time.Now()
  54. }
  55. func ixHandshakeStage1(f *Interface, addr *udpAddr, packet []byte, h *Header) {
  56. ci := f.newConnectionState(f.l, false, noise.HandshakeIX, []byte{}, 0)
  57. // Mark packet 1 as seen so it doesn't show up as missed
  58. ci.window.Update(f.l, 1)
  59. msg, _, _, err := ci.H.ReadMessage(nil, packet[HeaderLen:])
  60. if err != nil {
  61. f.l.WithError(err).WithField("udpAddr", addr).
  62. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.ReadMessage")
  63. return
  64. }
  65. hs := &NebulaHandshake{}
  66. err = proto.Unmarshal(msg, hs)
  67. /*
  68. l.Debugln("GOT INDEX: ", hs.Details.InitiatorIndex)
  69. */
  70. if err != nil || hs.Details == nil {
  71. f.l.WithError(err).WithField("udpAddr", addr).
  72. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  73. return
  74. }
  75. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert, f.caPool)
  76. if err != nil {
  77. f.l.WithError(err).WithField("udpAddr", addr).
  78. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).WithField("cert", remoteCert).
  79. Info("Invalid certificate from host")
  80. return
  81. }
  82. vpnIP := ip2int(remoteCert.Details.Ips[0].IP)
  83. certName := remoteCert.Details.Name
  84. fingerprint, _ := remoteCert.Sha256Sum()
  85. if vpnIP == ip2int(f.certState.certificate.Details.Ips[0].IP) {
  86. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  87. WithField("certName", certName).
  88. WithField("fingerprint", fingerprint).
  89. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Refusing to handshake with myself")
  90. return
  91. }
  92. myIndex, err := generateIndex(f.l)
  93. if err != nil {
  94. f.l.WithError(err).WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  95. WithField("certName", certName).
  96. WithField("fingerprint", fingerprint).
  97. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to generate index")
  98. return
  99. }
  100. hostinfo := &HostInfo{
  101. ConnectionState: ci,
  102. localIndexId: myIndex,
  103. remoteIndexId: hs.Details.InitiatorIndex,
  104. hostId: vpnIP,
  105. HandshakePacket: make(map[uint8][]byte, 0),
  106. lastHandshakeTime: hs.Details.Time,
  107. }
  108. hostinfo.Lock()
  109. defer hostinfo.Unlock()
  110. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  111. WithField("certName", certName).
  112. WithField("fingerprint", fingerprint).
  113. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  114. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  115. Info("Handshake message received")
  116. hs.Details.ResponderIndex = myIndex
  117. hs.Details.Cert = ci.certState.rawCertificateNoKey
  118. // Update the time in case their clock is way off from ours
  119. hs.Details.Time = uint64(time.Now().UnixNano())
  120. hsBytes, err := proto.Marshal(hs)
  121. if err != nil {
  122. f.l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  123. WithField("certName", certName).
  124. WithField("fingerprint", fingerprint).
  125. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  126. return
  127. }
  128. header := HeaderEncode(make([]byte, HeaderLen), Version, uint8(handshake), handshakeIXPSK0, hs.Details.InitiatorIndex, 2)
  129. msg, dKey, eKey, err := ci.H.WriteMessage(header, hsBytes)
  130. if err != nil {
  131. f.l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  132. WithField("certName", certName).
  133. WithField("fingerprint", fingerprint).
  134. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  135. return
  136. } else if dKey == nil || eKey == nil {
  137. f.l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  138. WithField("certName", certName).
  139. WithField("fingerprint", fingerprint).
  140. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Noise did not arrive at a key")
  141. return
  142. }
  143. hostinfo.HandshakePacket[0] = make([]byte, len(packet[HeaderLen:]))
  144. copy(hostinfo.HandshakePacket[0], packet[HeaderLen:])
  145. // Regardless of whether you are the sender or receiver, you should arrive here
  146. // and complete standing up the connection.
  147. hostinfo.HandshakePacket[2] = make([]byte, len(msg))
  148. copy(hostinfo.HandshakePacket[2], msg)
  149. // We are sending handshake packet 2, so we don't expect to receive
  150. // handshake packet 2 from the initiator.
  151. ci.window.Update(f.l, 2)
  152. ci.peerCert = remoteCert
  153. ci.dKey = NewNebulaCipherState(dKey)
  154. ci.eKey = NewNebulaCipherState(eKey)
  155. hostinfo.remotes = f.lightHouse.QueryCache(vpnIP)
  156. hostinfo.SetRemote(addr)
  157. hostinfo.CreateRemoteCIDR(remoteCert)
  158. // Only overwrite existing record if we should win the handshake race
  159. overwrite := vpnIP > ip2int(f.certState.certificate.Details.Ips[0].IP)
  160. existing, err := f.handshakeManager.CheckAndComplete(hostinfo, 0, overwrite, f)
  161. if err != nil {
  162. switch err {
  163. case ErrAlreadySeen:
  164. msg = existing.HandshakePacket[2]
  165. f.messageMetrics.Tx(handshake, NebulaMessageSubType(msg[1]), 1)
  166. err := f.outside.WriteTo(msg, addr)
  167. if err != nil {
  168. f.l.WithField("vpnIp", IntIp(existing.hostId)).WithField("udpAddr", addr).
  169. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  170. WithError(err).Error("Failed to send handshake message")
  171. } else {
  172. f.l.WithField("vpnIp", IntIp(existing.hostId)).WithField("udpAddr", addr).
  173. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  174. Info("Handshake message sent")
  175. }
  176. return
  177. case ErrExistingHostInfo:
  178. // This means there was an existing tunnel and this handshake was older than the one we are currently based on
  179. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  180. WithField("certName", certName).
  181. WithField("oldHandshakeTime", existing.lastHandshakeTime).
  182. WithField("newHandshakeTime", hostinfo.lastHandshakeTime).
  183. WithField("fingerprint", fingerprint).
  184. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  185. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  186. Info("Handshake too old")
  187. // Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
  188. f.SendMessageToVpnIp(test, testRequest, vpnIP, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  189. return
  190. case ErrLocalIndexCollision:
  191. // This means we failed to insert because of collision on localIndexId. Just let the next handshake packet retry
  192. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  193. WithField("certName", certName).
  194. WithField("fingerprint", fingerprint).
  195. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  196. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  197. WithField("localIndex", hostinfo.localIndexId).WithField("collision", IntIp(existing.hostId)).
  198. Error("Failed to add HostInfo due to localIndex collision")
  199. return
  200. case ErrExistingHandshake:
  201. // We have a race where both parties think they are an initiator and this tunnel lost, let the other one finish
  202. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  203. WithField("certName", certName).
  204. WithField("fingerprint", fingerprint).
  205. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  206. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  207. Error("Prevented a pending handshake race")
  208. return
  209. default:
  210. // Shouldn't happen, but just in case someone adds a new error type to CheckAndComplete
  211. // And we forget to update it here
  212. f.l.WithError(err).WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  213. WithField("certName", certName).
  214. WithField("fingerprint", fingerprint).
  215. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  216. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  217. Error("Failed to add HostInfo to HostMap")
  218. return
  219. }
  220. }
  221. // Do the send
  222. f.messageMetrics.Tx(handshake, NebulaMessageSubType(msg[1]), 1)
  223. err = f.outside.WriteTo(msg, addr)
  224. if err != nil {
  225. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  226. WithField("certName", certName).
  227. WithField("fingerprint", fingerprint).
  228. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  229. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  230. WithError(err).Error("Failed to send handshake")
  231. } else {
  232. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  233. WithField("certName", certName).
  234. WithField("fingerprint", fingerprint).
  235. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  236. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  237. WithField("sentCachedPackets", len(hostinfo.packetStore)).
  238. Info("Handshake message sent")
  239. }
  240. hostinfo.handshakeComplete(f.l, f.cachedPacketMetrics)
  241. return
  242. }
  243. func ixHandshakeStage2(f *Interface, addr *udpAddr, hostinfo *HostInfo, packet []byte, h *Header) bool {
  244. if hostinfo == nil {
  245. // Nothing here to tear down, got a bogus stage 2 packet
  246. return true
  247. }
  248. hostinfo.Lock()
  249. defer hostinfo.Unlock()
  250. ci := hostinfo.ConnectionState
  251. if ci.ready {
  252. f.l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  253. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  254. Info("Handshake is already complete")
  255. //TODO: evaluate addr for preference, if we handshook with a less preferred addr we can correct quickly here
  256. // We already have a complete tunnel, there is nothing that can be done by processing further stage 1 packets
  257. return false
  258. }
  259. msg, eKey, dKey, err := ci.H.ReadMessage(nil, packet[HeaderLen:])
  260. if err != nil {
  261. f.l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  262. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  263. Error("Failed to call noise.ReadMessage")
  264. // We don't want to tear down the connection on a bad ReadMessage because it could be an attacker trying
  265. // to DOS us. Every other error condition after should to allow a possible good handshake to complete in the
  266. // near future
  267. return false
  268. } else if dKey == nil || eKey == nil {
  269. f.l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  270. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  271. Error("Noise did not arrive at a key")
  272. // This should be impossible in IX but just in case, if we get here then there is no chance to recover
  273. // the handshake state machine. Tear it down
  274. return true
  275. }
  276. hs := &NebulaHandshake{}
  277. err = proto.Unmarshal(msg, hs)
  278. if err != nil || hs.Details == nil {
  279. f.l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  280. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  281. // The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
  282. return true
  283. }
  284. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert, f.caPool)
  285. if err != nil {
  286. f.l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  287. WithField("cert", remoteCert).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  288. Error("Invalid certificate from host")
  289. // The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
  290. return true
  291. }
  292. vpnIP := ip2int(remoteCert.Details.Ips[0].IP)
  293. certName := remoteCert.Details.Name
  294. fingerprint, _ := remoteCert.Sha256Sum()
  295. // Ensure the right host responded
  296. if vpnIP != hostinfo.hostId {
  297. f.l.WithField("intendedVpnIp", IntIp(hostinfo.hostId)).WithField("haveVpnIp", IntIp(vpnIP)).
  298. WithField("udpAddr", addr).WithField("certName", certName).
  299. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  300. Info("Incorrect host responded to handshake")
  301. // Release our old handshake from pending, it should not continue
  302. f.handshakeManager.pendingHostMap.DeleteHostInfo(hostinfo)
  303. // Create a new hostinfo/handshake for the intended vpn ip
  304. //TODO: this adds it to the timer wheel in a way that aggressively retries
  305. newHostInfo := f.getOrHandshake(hostinfo.hostId)
  306. newHostInfo.Lock()
  307. // Block the current used address
  308. newHostInfo.remotes = hostinfo.remotes
  309. newHostInfo.remotes.BlockRemote(addr)
  310. // Get the correct remote list for the host we did handshake with
  311. hostinfo.remotes = f.lightHouse.QueryCache(vpnIP)
  312. f.l.WithField("blockedUdpAddrs", newHostInfo.remotes.CopyBlockedRemotes()).WithField("vpnIp", IntIp(vpnIP)).
  313. WithField("remotes", newHostInfo.remotes.CopyAddrs(f.hostMap.preferredRanges)).
  314. Info("Blocked addresses for handshakes")
  315. // Swap the packet store to benefit the original intended recipient
  316. hostinfo.ConnectionState.queueLock.Lock()
  317. newHostInfo.packetStore = hostinfo.packetStore
  318. hostinfo.packetStore = []*cachedPacket{}
  319. hostinfo.ConnectionState.queueLock.Unlock()
  320. // Finally, put the correct vpn ip in the host info, tell them to close the tunnel, and return true to tear down
  321. hostinfo.hostId = vpnIP
  322. f.sendCloseTunnel(hostinfo)
  323. newHostInfo.Unlock()
  324. return true
  325. }
  326. // Mark packet 2 as seen so it doesn't show up as missed
  327. ci.window.Update(f.l, 2)
  328. duration := time.Since(hostinfo.handshakeStart).Nanoseconds()
  329. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  330. WithField("certName", certName).
  331. WithField("fingerprint", fingerprint).
  332. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  333. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  334. WithField("durationNs", duration).
  335. WithField("sentCachedPackets", len(hostinfo.packetStore)).
  336. Info("Handshake message received")
  337. hostinfo.remoteIndexId = hs.Details.ResponderIndex
  338. hostinfo.lastHandshakeTime = hs.Details.Time
  339. // Store their cert and our symmetric keys
  340. ci.peerCert = remoteCert
  341. ci.dKey = NewNebulaCipherState(dKey)
  342. ci.eKey = NewNebulaCipherState(eKey)
  343. // Make sure the current udpAddr being used is set for responding
  344. hostinfo.SetRemote(addr)
  345. // Build up the radix for the firewall if we have subnets in the cert
  346. hostinfo.CreateRemoteCIDR(remoteCert)
  347. // Complete our handshake and update metrics, this will replace any existing tunnels for this vpnIp
  348. //TODO: Complete here does not do a race avoidance, it will just take the new tunnel. Is this ok?
  349. f.handshakeManager.Complete(hostinfo, f)
  350. hostinfo.handshakeComplete(f.l, f.cachedPacketMetrics)
  351. f.metricHandshakes.Update(duration)
  352. return false
  353. }