outside.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package nebula
  2. import (
  3. "encoding/binary"
  4. "github.com/flynn/noise"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/sirupsen/logrus"
  7. "github.com/slackhq/nebula/cert"
  8. // "github.com/google/gopacket"
  9. // "github.com/google/gopacket/layers"
  10. // "encoding/binary"
  11. "errors"
  12. "fmt"
  13. "time"
  14. "golang.org/x/net/ipv4"
  15. )
  16. const (
  17. minFwPacketLen = 4
  18. )
  19. func (f *Interface) readOutsidePackets(addr *udpAddr, out []byte, packet []byte, header *Header, fwPacket *FirewallPacket, nb []byte) {
  20. err := header.Parse(packet)
  21. if err != nil {
  22. // TODO: best if we return this and let caller log
  23. // TODO: Might be better to send the literal []byte("holepunch") packet and ignore that?
  24. // Hole punch packets are 0 or 1 byte big, so lets ignore printing those errors
  25. if len(packet) > 1 {
  26. l.WithField("packet", packet).Infof("Error while parsing inbound packet from %s: %s", addr, err)
  27. }
  28. return
  29. }
  30. //l.Error("in packet ", header, packet[HeaderLen:])
  31. // verify if we've seen this index before, otherwise respond to the handshake initiation
  32. hostinfo, err := f.hostMap.QueryIndex(header.RemoteIndex)
  33. var ci *ConnectionState
  34. if err == nil {
  35. ci = hostinfo.ConnectionState
  36. }
  37. switch header.Type {
  38. case message:
  39. if !f.handleEncrypted(ci, addr, header) {
  40. return
  41. }
  42. f.decryptToTun(hostinfo, header.MessageCounter, out, packet, fwPacket, nb)
  43. // Fallthrough to the bottom to record incoming traffic
  44. case lightHouse:
  45. f.messageMetrics.Rx(header.Type, header.Subtype, 1)
  46. if !f.handleEncrypted(ci, addr, header) {
  47. return
  48. }
  49. d, err := f.decrypt(hostinfo, header.MessageCounter, out, packet, header, nb)
  50. if err != nil {
  51. hostinfo.logger().WithError(err).WithField("udpAddr", addr).
  52. WithField("packet", packet).
  53. Error("Failed to decrypt lighthouse packet")
  54. //TODO: maybe after build 64 is out? 06/14/2018 - NB
  55. //f.sendRecvError(net.Addr(addr), header.RemoteIndex)
  56. return
  57. }
  58. f.lightHouse.HandleRequest(addr, hostinfo.hostId, d, hostinfo.GetCert(), f)
  59. // Fallthrough to the bottom to record incoming traffic
  60. case test:
  61. f.messageMetrics.Rx(header.Type, header.Subtype, 1)
  62. if !f.handleEncrypted(ci, addr, header) {
  63. return
  64. }
  65. d, err := f.decrypt(hostinfo, header.MessageCounter, out, packet, header, nb)
  66. if err != nil {
  67. hostinfo.logger().WithError(err).WithField("udpAddr", addr).
  68. WithField("packet", packet).
  69. Error("Failed to decrypt test packet")
  70. //TODO: maybe after build 64 is out? 06/14/2018 - NB
  71. //f.sendRecvError(net.Addr(addr), header.RemoteIndex)
  72. return
  73. }
  74. if header.Subtype == testRequest {
  75. // This testRequest might be from TryPromoteBest, so we should roam
  76. // to the new IP address before responding
  77. f.handleHostRoaming(hostinfo, addr)
  78. f.send(test, testReply, ci, hostinfo, hostinfo.remote, d, nb, out)
  79. }
  80. // Fallthrough to the bottom to record incoming traffic
  81. // Non encrypted messages below here, they should not fall through to avoid tracking incoming traffic since they
  82. // are unauthenticated
  83. case handshake:
  84. f.messageMetrics.Rx(header.Type, header.Subtype, 1)
  85. HandleIncomingHandshake(f, addr, packet, header, hostinfo)
  86. return
  87. case recvError:
  88. f.messageMetrics.Rx(header.Type, header.Subtype, 1)
  89. // TODO: Remove this with recv_error deprecation
  90. f.handleRecvError(addr, header)
  91. return
  92. case closeTunnel:
  93. f.messageMetrics.Rx(header.Type, header.Subtype, 1)
  94. if !f.handleEncrypted(ci, addr, header) {
  95. return
  96. }
  97. hostinfo.logger().WithField("udpAddr", addr).
  98. Info("Close tunnel received, tearing down.")
  99. f.closeTunnel(hostinfo)
  100. return
  101. default:
  102. f.messageMetrics.Rx(header.Type, header.Subtype, 1)
  103. hostinfo.logger().Debugf("Unexpected packet received from %s", addr)
  104. return
  105. }
  106. f.handleHostRoaming(hostinfo, addr)
  107. f.connectionManager.In(hostinfo.hostId)
  108. }
  109. func (f *Interface) closeTunnel(hostInfo *HostInfo) {
  110. //TODO: this would be better as a single function in ConnectionManager that handled locks appropriately
  111. f.connectionManager.ClearIP(hostInfo.hostId)
  112. f.connectionManager.ClearPendingDeletion(hostInfo.hostId)
  113. f.lightHouse.DeleteVpnIP(hostInfo.hostId)
  114. f.hostMap.DeleteVpnIP(hostInfo.hostId)
  115. f.hostMap.DeleteIndex(hostInfo.localIndexId)
  116. }
  117. func (f *Interface) handleHostRoaming(hostinfo *HostInfo, addr *udpAddr) {
  118. if hostDidRoam(hostinfo.remote, addr) {
  119. if !f.lightHouse.remoteAllowList.Allow(udp2ipInt(addr)) {
  120. hostinfo.logger().WithField("newAddr", addr).Debug("lighthouse.remote_allow_list denied roaming")
  121. return
  122. }
  123. if !hostinfo.lastRoam.IsZero() && addr.Equals(hostinfo.lastRoamRemote) && time.Since(hostinfo.lastRoam) < RoamingSupressSeconds*time.Second {
  124. if l.Level >= logrus.DebugLevel {
  125. hostinfo.logger().WithField("udpAddr", hostinfo.remote).WithField("newAddr", addr).
  126. Debugf("Supressing roam back to previous remote for %d seconds", RoamingSupressSeconds)
  127. }
  128. return
  129. }
  130. hostinfo.logger().WithField("udpAddr", hostinfo.remote).WithField("newAddr", addr).
  131. Info("Host roamed to new udp ip/port.")
  132. hostinfo.lastRoam = time.Now()
  133. remoteCopy := *hostinfo.remote
  134. hostinfo.lastRoamRemote = &remoteCopy
  135. hostinfo.SetRemote(*addr)
  136. if f.lightHouse.amLighthouse {
  137. f.lightHouse.AddRemote(hostinfo.hostId, addr, false)
  138. }
  139. }
  140. }
  141. func (f *Interface) handleEncrypted(ci *ConnectionState, addr *udpAddr, header *Header) bool {
  142. // If connectionstate exists and the replay protector allows, process packet
  143. // Else, send recv errors for 300 seconds after a restart to allow fast reconnection.
  144. if ci == nil || !ci.window.Check(header.MessageCounter) {
  145. f.sendRecvError(addr, header.RemoteIndex)
  146. return false
  147. }
  148. return true
  149. }
  150. // newPacket validates and parses the interesting bits for the firewall out of the ip and sub protocol headers
  151. func newPacket(data []byte, incoming bool, fp *FirewallPacket) error {
  152. // Do we at least have an ipv4 header worth of data?
  153. if len(data) < ipv4.HeaderLen {
  154. return fmt.Errorf("packet is less than %v bytes", ipv4.HeaderLen)
  155. }
  156. // Is it an ipv4 packet?
  157. if int((data[0]>>4)&0x0f) != 4 {
  158. return fmt.Errorf("packet is not ipv4, type: %v", int((data[0]>>4)&0x0f))
  159. }
  160. // Adjust our start position based on the advertised ip header length
  161. ihl := int(data[0]&0x0f) << 2
  162. // Well formed ip header length?
  163. if ihl < ipv4.HeaderLen {
  164. return fmt.Errorf("packet had an invalid header length: %v", ihl)
  165. }
  166. // Check if this is the second or further fragment of a fragmented packet.
  167. flagsfrags := binary.BigEndian.Uint16(data[6:8])
  168. fp.Fragment = (flagsfrags & 0x1FFF) != 0
  169. // Firewall handles protocol checks
  170. fp.Protocol = data[9]
  171. // Accounting for a variable header length, do we have enough data for our src/dst tuples?
  172. minLen := ihl
  173. if !fp.Fragment && fp.Protocol != fwProtoICMP {
  174. minLen += minFwPacketLen
  175. }
  176. if len(data) < minLen {
  177. return fmt.Errorf("packet is less than %v bytes, ip header len: %v", minLen, ihl)
  178. }
  179. // Firewall packets are locally oriented
  180. if incoming {
  181. fp.RemoteIP = binary.BigEndian.Uint32(data[12:16])
  182. fp.LocalIP = binary.BigEndian.Uint32(data[16:20])
  183. if fp.Fragment || fp.Protocol == fwProtoICMP {
  184. fp.RemotePort = 0
  185. fp.LocalPort = 0
  186. } else {
  187. fp.RemotePort = binary.BigEndian.Uint16(data[ihl : ihl+2])
  188. fp.LocalPort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4])
  189. }
  190. } else {
  191. fp.LocalIP = binary.BigEndian.Uint32(data[12:16])
  192. fp.RemoteIP = binary.BigEndian.Uint32(data[16:20])
  193. if fp.Fragment || fp.Protocol == fwProtoICMP {
  194. fp.RemotePort = 0
  195. fp.LocalPort = 0
  196. } else {
  197. fp.LocalPort = binary.BigEndian.Uint16(data[ihl : ihl+2])
  198. fp.RemotePort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4])
  199. }
  200. }
  201. return nil
  202. }
  203. func (f *Interface) decrypt(hostinfo *HostInfo, mc uint64, out []byte, packet []byte, header *Header, nb []byte) ([]byte, error) {
  204. var err error
  205. out, err = hostinfo.ConnectionState.dKey.DecryptDanger(out, packet[:HeaderLen], packet[HeaderLen:], mc, nb)
  206. if err != nil {
  207. return nil, err
  208. }
  209. if !hostinfo.ConnectionState.window.Update(mc) {
  210. hostinfo.logger().WithField("header", header).
  211. Debugln("dropping out of window packet")
  212. return nil, errors.New("out of window packet")
  213. }
  214. return out, nil
  215. }
  216. func (f *Interface) decryptToTun(hostinfo *HostInfo, messageCounter uint64, out []byte, packet []byte, fwPacket *FirewallPacket, nb []byte) {
  217. var err error
  218. out, err = hostinfo.ConnectionState.dKey.DecryptDanger(out, packet[:HeaderLen], packet[HeaderLen:], messageCounter, nb)
  219. if err != nil {
  220. hostinfo.logger().WithError(err).Error("Failed to decrypt packet")
  221. //TODO: maybe after build 64 is out? 06/14/2018 - NB
  222. //f.sendRecvError(hostinfo.remote, header.RemoteIndex)
  223. return
  224. }
  225. err = newPacket(out, true, fwPacket)
  226. if err != nil {
  227. hostinfo.logger().WithError(err).WithField("packet", out).
  228. Warnf("Error while validating inbound packet")
  229. return
  230. }
  231. if !hostinfo.ConnectionState.window.Update(messageCounter) {
  232. hostinfo.logger().WithField("fwPacket", fwPacket).
  233. Debugln("dropping out of window packet")
  234. return
  235. }
  236. dropReason := f.firewall.Drop(out, *fwPacket, true, hostinfo, trustedCAs)
  237. if dropReason != nil {
  238. if l.Level >= logrus.DebugLevel {
  239. hostinfo.logger().WithField("fwPacket", fwPacket).
  240. WithField("reason", dropReason).
  241. Debugln("dropping inbound packet")
  242. }
  243. return
  244. }
  245. f.connectionManager.In(hostinfo.hostId)
  246. err = f.inside.WriteRaw(out)
  247. if err != nil {
  248. l.WithError(err).Error("Failed to write to tun")
  249. }
  250. }
  251. func (f *Interface) sendRecvError(endpoint *udpAddr, index uint32) {
  252. f.messageMetrics.Tx(recvError, 0, 1)
  253. //TODO: this should be a signed message so we can trust that we should drop the index
  254. b := HeaderEncode(make([]byte, HeaderLen), Version, uint8(recvError), 0, index, 0)
  255. f.outside.WriteTo(b, endpoint)
  256. if l.Level >= logrus.DebugLevel {
  257. l.WithField("index", index).
  258. WithField("udpAddr", endpoint).
  259. Debug("Recv error sent")
  260. }
  261. }
  262. func (f *Interface) handleRecvError(addr *udpAddr, h *Header) {
  263. // This flag is to stop caring about recv_error from old versions
  264. // This should go away when the old version is gone from prod
  265. if l.Level >= logrus.DebugLevel {
  266. l.WithField("index", h.RemoteIndex).
  267. WithField("udpAddr", addr).
  268. Debug("Recv error received")
  269. }
  270. hostinfo, err := f.hostMap.QueryReverseIndex(h.RemoteIndex)
  271. if err != nil {
  272. l.Debugln(err, ": ", h.RemoteIndex)
  273. return
  274. }
  275. if !hostinfo.RecvErrorExceeded() {
  276. return
  277. }
  278. if hostinfo.remote != nil && hostinfo.remote.String() != addr.String() {
  279. l.Infoln("Someone spoofing recv_errors? ", addr, hostinfo.remote)
  280. return
  281. }
  282. id := hostinfo.localIndexId
  283. host := hostinfo.hostId
  284. // We delete this host from the main hostmap
  285. f.hostMap.DeleteIndex(id)
  286. f.hostMap.DeleteVpnIP(host)
  287. // We also delete it from pending to allow for
  288. // fast reconnect. We must null the connectionstate
  289. // or a counter reuse may happen
  290. hostinfo.ConnectionState = nil
  291. f.handshakeManager.DeleteIndex(id)
  292. f.handshakeManager.DeleteVpnIP(host)
  293. }
  294. /*
  295. func (f *Interface) sendMeta(ci *ConnectionState, endpoint *net.UDPAddr, meta *NebulaMeta) {
  296. if ci.eKey != nil {
  297. //TODO: log error?
  298. return
  299. }
  300. msg, err := proto.Marshal(meta)
  301. if err != nil {
  302. l.Debugln("failed to encode header")
  303. }
  304. c := ci.messageCounter
  305. b := HeaderEncode(nil, Version, uint8(metadata), 0, hostinfo.remoteIndexId, c)
  306. ci.messageCounter++
  307. msg := ci.eKey.EncryptDanger(b, nil, msg, c)
  308. //msg := ci.eKey.EncryptDanger(b, nil, []byte(fmt.Sprintf("%d", counter)), c)
  309. f.outside.WriteTo(msg, endpoint)
  310. }
  311. */
  312. func RecombineCertAndValidate(h *noise.HandshakeState, rawCertBytes []byte) (*cert.NebulaCertificate, error) {
  313. pk := h.PeerStatic()
  314. if pk == nil {
  315. return nil, errors.New("no peer static key was present")
  316. }
  317. if rawCertBytes == nil {
  318. return nil, errors.New("provided payload was empty")
  319. }
  320. r := &cert.RawNebulaCertificate{}
  321. err := proto.Unmarshal(rawCertBytes, r)
  322. if err != nil {
  323. return nil, fmt.Errorf("error unmarshaling cert: %s", err)
  324. }
  325. // If the Details are nil, just exit to avoid crashing
  326. if r.Details == nil {
  327. return nil, fmt.Errorf("certificate did not contain any details")
  328. }
  329. r.Details.PublicKey = pk
  330. recombined, err := proto.Marshal(r)
  331. if err != nil {
  332. return nil, fmt.Errorf("error while recombining certificate: %s", err)
  333. }
  334. c, _ := cert.UnmarshalNebulaCertificate(recombined)
  335. isValid, err := c.Verify(time.Now(), trustedCAs)
  336. if err != nil {
  337. return c, fmt.Errorf("certificate validation failed: %s", err)
  338. } else if !isValid {
  339. // This case should never happen but here's to defensive programming!
  340. return c, errors.New("certificate validation failed but did not return an error")
  341. }
  342. return c, nil
  343. }