outside.go 10.0 KB

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