inside.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package nebula
  2. import (
  3. "sync/atomic"
  4. "github.com/sirupsen/logrus"
  5. "github.com/slackhq/nebula/firewall"
  6. "github.com/slackhq/nebula/header"
  7. "github.com/slackhq/nebula/iputil"
  8. "github.com/slackhq/nebula/udp"
  9. )
  10. func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet, nb, out []byte, q int, localCache firewall.ConntrackCache) {
  11. err := newPacket(packet, false, fwPacket)
  12. if err != nil {
  13. f.l.WithField("packet", packet).Debugf("Error while validating outbound packet: %s", err)
  14. return
  15. }
  16. // Ignore local broadcast packets
  17. if f.dropLocalBroadcast && fwPacket.RemoteIP == f.localBroadcast {
  18. return
  19. }
  20. // Ignore packets from self to self
  21. if fwPacket.RemoteIP == f.myVpnIp {
  22. return
  23. }
  24. // Ignore broadcast packets
  25. if f.dropMulticast && isMulticast(fwPacket.RemoteIP) {
  26. return
  27. }
  28. hostinfo := f.getOrHandshake(fwPacket.RemoteIP)
  29. if hostinfo == nil {
  30. if f.l.Level >= logrus.DebugLevel {
  31. f.l.WithField("vpnIp", fwPacket.RemoteIP).
  32. WithField("fwPacket", fwPacket).
  33. Debugln("dropping outbound packet, vpnIp not in our CIDR or in unsafe routes")
  34. }
  35. return
  36. }
  37. ci := hostinfo.ConnectionState
  38. if ci.ready == false {
  39. // Because we might be sending stored packets, lock here to stop new things going to
  40. // the packet queue.
  41. ci.queueLock.Lock()
  42. if !ci.ready {
  43. hostinfo.cachePacket(f.l, header.Message, 0, packet, f.sendMessageNow, f.cachedPacketMetrics)
  44. ci.queueLock.Unlock()
  45. return
  46. }
  47. ci.queueLock.Unlock()
  48. }
  49. dropReason := f.firewall.Drop(packet, *fwPacket, false, hostinfo, f.caPool, localCache)
  50. if dropReason == nil {
  51. f.sendNoMetrics(header.Message, 0, ci, hostinfo, hostinfo.remote, packet, nb, out, q)
  52. } else if f.l.Level >= logrus.DebugLevel {
  53. hostinfo.logger(f.l).
  54. WithField("fwPacket", fwPacket).
  55. WithField("reason", dropReason).
  56. Debugln("dropping outbound packet")
  57. }
  58. }
  59. // getOrHandshake returns nil if the vpnIp is not routable
  60. func (f *Interface) getOrHandshake(vpnIp iputil.VpnIp) *HostInfo {
  61. //TODO: we can find contains without converting back to bytes
  62. if f.hostMap.vpnCIDR.Contains(vpnIp.ToIP()) == false {
  63. vpnIp = f.hostMap.queryUnsafeRoute(vpnIp)
  64. if vpnIp == 0 {
  65. return nil
  66. }
  67. }
  68. hostinfo, err := f.hostMap.PromoteBestQueryVpnIp(vpnIp, f)
  69. if err != nil {
  70. hostinfo, err = f.handshakeManager.pendingHostMap.QueryVpnIp(vpnIp)
  71. if err != nil {
  72. hostinfo = f.handshakeManager.AddVpnIp(vpnIp)
  73. }
  74. }
  75. ci := hostinfo.ConnectionState
  76. if ci != nil && ci.eKey != nil && ci.ready {
  77. return hostinfo
  78. }
  79. // Handshake is not ready, we need to grab the lock now before we start the handshake process
  80. hostinfo.Lock()
  81. defer hostinfo.Unlock()
  82. // Double check, now that we have the lock
  83. ci = hostinfo.ConnectionState
  84. if ci != nil && ci.eKey != nil && ci.ready {
  85. return hostinfo
  86. }
  87. // Create a connection state if we don't have one yet
  88. if ci == nil {
  89. // Generate a PSK based on our config, this may be nil
  90. p, err := f.psk.MakeFor(vpnIp)
  91. if err != nil {
  92. //TODO: This isn't fatal specifically but it's pretty bad
  93. f.l.WithError(err).WithField("vpnIp", vpnIp).Error("Failed to get a PSK KDF")
  94. return hostinfo
  95. }
  96. ci, err = f.newConnectionState(f.l, true, p)
  97. if err != nil {
  98. f.l.WithError(err).WithField("vpnIp", vpnIp).Error("Failed to get a connection state")
  99. return hostinfo
  100. }
  101. hostinfo.ConnectionState = ci
  102. }
  103. // If we have already created the handshake packet, we don't want to call the function at all.
  104. if !hostinfo.HandshakeReady {
  105. ixHandshakeStage0(f, vpnIp, hostinfo)
  106. // If this is a static host, we don't need to wait for the HostQueryReply
  107. // We can trigger the handshake right now
  108. if _, ok := f.lightHouse.staticList[vpnIp]; ok {
  109. select {
  110. case f.handshakeManager.trigger <- vpnIp:
  111. default:
  112. }
  113. }
  114. }
  115. return hostinfo
  116. }
  117. func (f *Interface) sendMessageNow(t header.MessageType, st header.MessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  118. fp := &firewall.Packet{}
  119. err := newPacket(p, false, fp)
  120. if err != nil {
  121. f.l.Warnf("error while parsing outgoing packet for firewall check; %v", err)
  122. return
  123. }
  124. // check if packet is in outbound fw rules
  125. dropReason := f.firewall.Drop(p, *fp, false, hostInfo, f.caPool, nil)
  126. if dropReason != nil {
  127. if f.l.Level >= logrus.DebugLevel {
  128. f.l.WithField("fwPacket", fp).
  129. WithField("reason", dropReason).
  130. Debugln("dropping cached packet")
  131. }
  132. return
  133. }
  134. f.sendNoMetrics(header.Message, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out, 0)
  135. }
  136. // SendMessageToVpnIp handles real ip:port lookup and sends to the current best known address for vpnIp
  137. func (f *Interface) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, nb, out []byte) {
  138. hostInfo := f.getOrHandshake(vpnIp)
  139. if hostInfo == nil {
  140. if f.l.Level >= logrus.DebugLevel {
  141. f.l.WithField("vpnIp", vpnIp).
  142. Debugln("dropping SendMessageToVpnIp, vpnIp not in our CIDR or in unsafe routes")
  143. }
  144. return
  145. }
  146. if !hostInfo.ConnectionState.ready {
  147. // Because we might be sending stored packets, lock here to stop new things going to
  148. // the packet queue.
  149. hostInfo.ConnectionState.queueLock.Lock()
  150. if !hostInfo.ConnectionState.ready {
  151. hostInfo.cachePacket(f.l, t, st, p, f.sendMessageToVpnIp, f.cachedPacketMetrics)
  152. hostInfo.ConnectionState.queueLock.Unlock()
  153. return
  154. }
  155. hostInfo.ConnectionState.queueLock.Unlock()
  156. }
  157. f.sendMessageToVpnIp(t, st, hostInfo, p, nb, out)
  158. return
  159. }
  160. func (f *Interface) sendMessageToVpnIp(t header.MessageType, st header.MessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  161. f.send(t, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out)
  162. }
  163. func (f *Interface) send(t header.MessageType, st header.MessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udp.Addr, p, nb, out []byte) {
  164. f.messageMetrics.Tx(t, st, 1)
  165. f.sendNoMetrics(t, st, ci, hostinfo, remote, p, nb, out, 0)
  166. }
  167. func (f *Interface) sendNoMetrics(t header.MessageType, st header.MessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udp.Addr, p, nb, out []byte, q int) {
  168. if ci.eKey == nil {
  169. //TODO: log warning
  170. return
  171. }
  172. var err error
  173. //TODO: enable if we do more than 1 tun queue
  174. //ci.writeLock.Lock()
  175. c := atomic.AddUint64(&ci.atomicMessageCounter, 1)
  176. //l.WithField("trace", string(debug.Stack())).Error("out Header ", &Header{Version, t, st, 0, hostinfo.remoteIndexId, c}, p)
  177. out = header.Encode(out, header.Version, t, st, hostinfo.remoteIndexId, c)
  178. f.connectionManager.Out(hostinfo.vpnIp)
  179. // Query our LH if we haven't since the last time we've been rebound, this will cause the remote to punch against
  180. // all our IPs and enable a faster roaming.
  181. if t != header.CloseTunnel && hostinfo.lastRebindCount != f.rebindCount {
  182. //NOTE: there is an update hole if a tunnel isn't used and exactly 256 rebinds occur before the tunnel is
  183. // finally used again. This tunnel would eventually be torn down and recreated if this action didn't help.
  184. f.lightHouse.QueryServer(hostinfo.vpnIp, f)
  185. hostinfo.lastRebindCount = f.rebindCount
  186. if f.l.Level >= logrus.DebugLevel {
  187. f.l.WithField("vpnIp", hostinfo.vpnIp).Debug("Lighthouse update triggered for punch due to rebind counter")
  188. }
  189. }
  190. out, err = ci.eKey.EncryptDanger(out, out, p, c, nb)
  191. //TODO: see above note on lock
  192. //ci.writeLock.Unlock()
  193. if err != nil {
  194. hostinfo.logger(f.l).WithError(err).
  195. WithField("udpAddr", remote).WithField("counter", c).
  196. WithField("attemptedCounter", c).
  197. Error("Failed to encrypt outgoing packet")
  198. return
  199. }
  200. err = f.writers[q].WriteTo(out, remote)
  201. if err != nil {
  202. hostinfo.logger(f.l).WithError(err).
  203. WithField("udpAddr", remote).Error("Failed to write outgoing packet")
  204. }
  205. return
  206. }
  207. func isMulticast(ip iputil.VpnIp) bool {
  208. // Class D multicast
  209. if (((ip >> 24) & 0xff) & 0xf0) == 0xe0 {
  210. return true
  211. }
  212. return false
  213. }