handshake_ix.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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().Unix()),
  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().Unix())
  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. Info("Handshake message sent")
  238. }
  239. hostinfo.handshakeComplete(f.l)
  240. return
  241. }
  242. func ixHandshakeStage2(f *Interface, addr *udpAddr, hostinfo *HostInfo, packet []byte, h *Header) bool {
  243. if hostinfo == nil {
  244. // Nothing here to tear down, got a bogus stage 2 packet
  245. return true
  246. }
  247. hostinfo.Lock()
  248. defer hostinfo.Unlock()
  249. ci := hostinfo.ConnectionState
  250. if ci.ready {
  251. f.l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  252. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  253. Info("Handshake is already complete")
  254. //TODO: evaluate addr for preference, if we handshook with a less preferred addr we can correct quickly here
  255. // We already have a complete tunnel, there is nothing that can be done by processing further stage 1 packets
  256. return false
  257. }
  258. msg, eKey, dKey, err := ci.H.ReadMessage(nil, packet[HeaderLen:])
  259. if err != nil {
  260. f.l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  261. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  262. Error("Failed to call noise.ReadMessage")
  263. // We don't want to tear down the connection on a bad ReadMessage because it could be an attacker trying
  264. // to DOS us. Every other error condition after should to allow a possible good handshake to complete in the
  265. // near future
  266. return false
  267. } else if dKey == nil || eKey == nil {
  268. f.l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  269. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  270. Error("Noise did not arrive at a key")
  271. // This should be impossible in IX but just in case, if we get here then there is no chance to recover
  272. // the handshake state machine. Tear it down
  273. return true
  274. }
  275. hs := &NebulaHandshake{}
  276. err = proto.Unmarshal(msg, hs)
  277. if err != nil || hs.Details == nil {
  278. f.l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  279. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  280. // The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
  281. return true
  282. }
  283. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert, f.caPool)
  284. if err != nil {
  285. f.l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  286. WithField("cert", remoteCert).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  287. Error("Invalid certificate from host")
  288. // The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
  289. return true
  290. }
  291. vpnIP := ip2int(remoteCert.Details.Ips[0].IP)
  292. certName := remoteCert.Details.Name
  293. fingerprint, _ := remoteCert.Sha256Sum()
  294. // Ensure the right host responded
  295. if vpnIP != hostinfo.hostId {
  296. f.l.WithField("intendedVpnIp", IntIp(hostinfo.hostId)).WithField("haveVpnIp", IntIp(vpnIP)).
  297. WithField("udpAddr", addr).WithField("certName", certName).
  298. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  299. Info("Incorrect host responded to handshake")
  300. // Release our old handshake from pending, it should not continue
  301. f.handshakeManager.pendingHostMap.DeleteHostInfo(hostinfo)
  302. // Create a new hostinfo/handshake for the intended vpn ip
  303. //TODO: this adds it to the timer wheel in a way that aggressively retries
  304. newHostInfo := f.getOrHandshake(hostinfo.hostId)
  305. newHostInfo.Lock()
  306. // Block the current used address
  307. newHostInfo.remotes = hostinfo.remotes
  308. newHostInfo.remotes.BlockRemote(addr)
  309. // Get the correct remote list for the host we did handshake with
  310. hostinfo.remotes = f.lightHouse.QueryCache(vpnIP)
  311. f.l.WithField("blockedUdpAddrs", newHostInfo.remotes.CopyBlockedRemotes()).WithField("vpnIp", IntIp(vpnIP)).
  312. WithField("remotes", newHostInfo.remotes.CopyAddrs(f.hostMap.preferredRanges)).
  313. Info("Blocked addresses for handshakes")
  314. // Swap the packet store to benefit the original intended recipient
  315. hostinfo.ConnectionState.queueLock.Lock()
  316. newHostInfo.packetStore = hostinfo.packetStore
  317. hostinfo.packetStore = []*cachedPacket{}
  318. hostinfo.ConnectionState.queueLock.Unlock()
  319. // Finally, put the correct vpn ip in the host info, tell them to close the tunnel, and return true to tear down
  320. hostinfo.hostId = vpnIP
  321. f.sendCloseTunnel(hostinfo)
  322. newHostInfo.Unlock()
  323. return true
  324. }
  325. // Mark packet 2 as seen so it doesn't show up as missed
  326. ci.window.Update(f.l, 2)
  327. duration := time.Since(hostinfo.handshakeStart).Nanoseconds()
  328. f.l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  329. WithField("certName", certName).
  330. WithField("fingerprint", fingerprint).
  331. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  332. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  333. WithField("durationNs", duration).
  334. Info("Handshake message received")
  335. hostinfo.remoteIndexId = hs.Details.ResponderIndex
  336. hostinfo.lastHandshakeTime = hs.Details.Time
  337. // Store their cert and our symmetric keys
  338. ci.peerCert = remoteCert
  339. ci.dKey = NewNebulaCipherState(dKey)
  340. ci.eKey = NewNebulaCipherState(eKey)
  341. // Make sure the current udpAddr being used is set for responding
  342. hostinfo.SetRemote(addr)
  343. // Build up the radix for the firewall if we have subnets in the cert
  344. hostinfo.CreateRemoteCIDR(remoteCert)
  345. // Complete our handshake and update metrics, this will replace any existing tunnels for this vpnIp
  346. //TODO: Complete here does not do a race avoidance, it will just take the new tunnel. Is this ok?
  347. f.handshakeManager.Complete(hostinfo, f)
  348. hostinfo.handshakeComplete(f.l)
  349. f.metricHandshakes.Update(duration)
  350. return false
  351. }