inside.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package nebula
  2. import (
  3. "sync/atomic"
  4. "github.com/flynn/noise"
  5. "github.com/sirupsen/logrus"
  6. )
  7. func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *FirewallPacket, nb, out []byte, q int, localCache ConntrackCache) {
  8. err := newPacket(packet, false, fwPacket)
  9. if err != nil {
  10. l.WithField("packet", packet).Debugf("Error while validating outbound packet: %s", err)
  11. return
  12. }
  13. // Ignore local broadcast packets
  14. if f.dropLocalBroadcast && fwPacket.RemoteIP == f.localBroadcast {
  15. return
  16. }
  17. // Ignore packets from self to self
  18. if fwPacket.RemoteIP == f.lightHouse.myIp {
  19. return
  20. }
  21. // Ignore broadcast packets
  22. if f.dropMulticast && isMulticast(fwPacket.RemoteIP) {
  23. return
  24. }
  25. hostinfo := f.getOrHandshake(fwPacket.RemoteIP)
  26. if hostinfo == nil {
  27. if l.Level >= logrus.DebugLevel {
  28. l.WithField("vpnIp", IntIp(fwPacket.RemoteIP)).
  29. WithField("fwPacket", fwPacket).
  30. Debugln("dropping outbound packet, vpnIp not in our CIDR or in unsafe routes")
  31. }
  32. return
  33. }
  34. ci := hostinfo.ConnectionState
  35. if ci.ready == false {
  36. // Because we might be sending stored packets, lock here to stop new things going to
  37. // the packet queue.
  38. ci.queueLock.Lock()
  39. if !ci.ready {
  40. hostinfo.cachePacket(message, 0, packet, f.sendMessageNow)
  41. ci.queueLock.Unlock()
  42. return
  43. }
  44. ci.queueLock.Unlock()
  45. }
  46. dropReason := f.firewall.Drop(packet, *fwPacket, false, hostinfo, trustedCAs, localCache)
  47. if dropReason == nil {
  48. mc := f.sendNoMetrics(message, 0, ci, hostinfo, hostinfo.remote, packet, nb, out, q)
  49. if f.lightHouse != nil && mc%5000 == 0 {
  50. f.lightHouse.Query(fwPacket.RemoteIP, f)
  51. }
  52. } else if l.Level >= logrus.DebugLevel {
  53. hostinfo.logger().
  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 uint32) *HostInfo {
  61. if f.hostMap.vpnCIDR.Contains(int2ip(vpnIp)) == false {
  62. vpnIp = f.hostMap.queryUnsafeRoute(vpnIp)
  63. if vpnIp == 0 {
  64. return nil
  65. }
  66. }
  67. hostinfo, err := f.hostMap.PromoteBestQueryVpnIP(vpnIp, f)
  68. //if err != nil || hostinfo.ConnectionState == nil {
  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. if ci == nil {
  80. // if we don't have a connection state, then send a handshake initiation
  81. ci = f.newConnectionState(true, noise.HandshakeIX, []byte{}, 0)
  82. // FIXME: Maybe make XX selectable, but probably not since psk makes it nearly pointless for us.
  83. //ci = f.newConnectionState(true, noise.HandshakeXX, []byte{}, 0)
  84. hostinfo.ConnectionState = ci
  85. } else if ci.eKey == nil {
  86. // if we don't have any state at all, create it
  87. }
  88. // If we have already created the handshake packet, we don't want to call the function at all.
  89. if !hostinfo.HandshakeReady {
  90. hostinfo.Lock()
  91. defer hostinfo.Unlock()
  92. if !hostinfo.HandshakeReady {
  93. ixHandshakeStage0(f, vpnIp, hostinfo)
  94. // FIXME: Maybe make XX selectable, but probably not since psk makes it nearly pointless for us.
  95. //xx_handshakeStage0(f, ip, hostinfo)
  96. // If this is a static host, we don't need to wait for the HostQueryReply
  97. // We can trigger the handshake right now
  98. if _, ok := f.lightHouse.staticList[vpnIp]; ok {
  99. select {
  100. case f.handshakeManager.trigger <- vpnIp:
  101. default:
  102. }
  103. }
  104. }
  105. }
  106. return hostinfo
  107. }
  108. func (f *Interface) sendMessageNow(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  109. fp := &FirewallPacket{}
  110. err := newPacket(p, false, fp)
  111. if err != nil {
  112. l.Warnf("error while parsing outgoing packet for firewall check; %v", err)
  113. return
  114. }
  115. // check if packet is in outbound fw rules
  116. dropReason := f.firewall.Drop(p, *fp, false, hostInfo, trustedCAs, nil)
  117. if dropReason != nil {
  118. if l.Level >= logrus.DebugLevel {
  119. l.WithField("fwPacket", fp).
  120. WithField("reason", dropReason).
  121. Debugln("dropping cached packet")
  122. }
  123. return
  124. }
  125. messageCounter := f.sendNoMetrics(message, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out, 0)
  126. if f.lightHouse != nil && messageCounter%5000 == 0 {
  127. f.lightHouse.Query(fp.RemoteIP, f)
  128. }
  129. }
  130. // SendMessageToVpnIp handles real ip:port lookup and sends to the current best known address for vpnIp
  131. func (f *Interface) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  132. hostInfo := f.getOrHandshake(vpnIp)
  133. if hostInfo == nil {
  134. if l.Level >= logrus.DebugLevel {
  135. l.WithField("vpnIp", IntIp(vpnIp)).
  136. Debugln("dropping SendMessageToVpnIp, vpnIp not in our CIDR or in unsafe routes")
  137. }
  138. return
  139. }
  140. if !hostInfo.ConnectionState.ready {
  141. // Because we might be sending stored packets, lock here to stop new things going to
  142. // the packet queue.
  143. hostInfo.ConnectionState.queueLock.Lock()
  144. if !hostInfo.ConnectionState.ready {
  145. hostInfo.cachePacket(t, st, p, f.sendMessageToVpnIp)
  146. hostInfo.ConnectionState.queueLock.Unlock()
  147. return
  148. }
  149. hostInfo.ConnectionState.queueLock.Unlock()
  150. }
  151. f.sendMessageToVpnIp(t, st, hostInfo, p, nb, out)
  152. return
  153. }
  154. func (f *Interface) sendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  155. f.send(t, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out)
  156. }
  157. // SendMessageToAll handles real ip:port lookup and sends to all known addresses for vpnIp
  158. func (f *Interface) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  159. hostInfo := f.getOrHandshake(vpnIp)
  160. if hostInfo == nil {
  161. if l.Level >= logrus.DebugLevel {
  162. l.WithField("vpnIp", IntIp(vpnIp)).
  163. Debugln("dropping SendMessageToAll, vpnIp not in our CIDR or in unsafe routes")
  164. }
  165. return
  166. }
  167. if hostInfo.ConnectionState.ready == false {
  168. // Because we might be sending stored packets, lock here to stop new things going to
  169. // the packet queue.
  170. hostInfo.ConnectionState.queueLock.Lock()
  171. if !hostInfo.ConnectionState.ready {
  172. hostInfo.cachePacket(t, st, p, f.sendMessageToAll)
  173. hostInfo.ConnectionState.queueLock.Unlock()
  174. return
  175. }
  176. hostInfo.ConnectionState.queueLock.Unlock()
  177. }
  178. f.sendMessageToAll(t, st, hostInfo, p, nb, out)
  179. return
  180. }
  181. func (f *Interface) sendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, b []byte) {
  182. for _, r := range hostInfo.RemoteUDPAddrs() {
  183. f.send(t, st, hostInfo.ConnectionState, hostInfo, r, p, nb, b)
  184. }
  185. }
  186. func (f *Interface) send(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) {
  187. f.messageMetrics.Tx(t, st, 1)
  188. f.sendNoMetrics(t, st, ci, hostinfo, remote, p, nb, out, 0)
  189. }
  190. func (f *Interface) sendNoMetrics(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte, q int) uint64 {
  191. if ci.eKey == nil {
  192. //TODO: log warning
  193. return 0
  194. }
  195. var err error
  196. //TODO: enable if we do more than 1 tun queue
  197. //ci.writeLock.Lock()
  198. c := atomic.AddUint64(&ci.atomicMessageCounter, 1)
  199. //l.WithField("trace", string(debug.Stack())).Error("out Header ", &Header{Version, t, st, 0, hostinfo.remoteIndexId, c}, p)
  200. out = HeaderEncode(out, Version, uint8(t), uint8(st), hostinfo.remoteIndexId, c)
  201. f.connectionManager.Out(hostinfo.hostId)
  202. // Query our LH if we haven't since the last time we've been rebound, this will cause the remote to punch against
  203. // all our IPs and enable a faster roaming.
  204. if hostinfo.lastRebindCount != f.rebindCount {
  205. //NOTE: there is an update hole if a tunnel isn't used and exactly 256 rebinds occur before the tunnel is
  206. // finally used again. This tunnel would eventually be torn down and recreated if this action didn't help.
  207. f.lightHouse.Query(hostinfo.hostId, f)
  208. hostinfo.lastRebindCount = f.rebindCount
  209. if l.Level >= logrus.DebugLevel {
  210. l.WithField("vpnIp", hostinfo.hostId).Debug("Lighthouse update triggered for punch due to rebind counter")
  211. }
  212. }
  213. out, err = ci.eKey.EncryptDanger(out, out, p, c, nb)
  214. //TODO: see above note on lock
  215. //ci.writeLock.Unlock()
  216. if err != nil {
  217. hostinfo.logger().WithError(err).
  218. WithField("udpAddr", remote).WithField("counter", c).
  219. WithField("attemptedCounter", c).
  220. Error("Failed to encrypt outgoing packet")
  221. return c
  222. }
  223. err = f.writers[q].WriteTo(out, remote)
  224. if err != nil {
  225. hostinfo.logger().WithError(err).
  226. WithField("udpAddr", remote).Error("Failed to write outgoing packet")
  227. }
  228. return c
  229. }
  230. func isMulticast(ip uint32) bool {
  231. // Class D multicast
  232. if (((ip >> 24) & 0xff) & 0xf0) == 0xe0 {
  233. return true
  234. }
  235. return false
  236. }