inside.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package nebula
  2. import (
  3. "sync/atomic"
  4. "github.com/flynn/noise"
  5. "github.com/sirupsen/logrus"
  6. )
  7. func (f *Interface) consumeInsidePacket(st NebulaMessageSubType, packet []byte, fwPacket *FirewallPacket, nb, out []byte) {
  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, st, 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)
  47. if dropReason == nil {
  48. mc := f.sendNoMetrics(message, st, ci, hostinfo, hostinfo.remote, packet, nb, out)
  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. ixHandshakeStage0(f, vpnIp, hostinfo)
  91. // FIXME: Maybe make XX selectable, but probably not since psk makes it nearly pointless for us.
  92. //xx_handshakeStage0(f, ip, hostinfo)
  93. // If this is a static host, we don't need to wait for the HostQueryReply
  94. // We can trigger the handshake right now
  95. if _, ok := f.lightHouse.staticList[vpnIp]; ok {
  96. select {
  97. case f.handshakeManager.trigger <- vpnIp:
  98. default:
  99. }
  100. }
  101. }
  102. return hostinfo
  103. }
  104. func (f *Interface) sendMessageNow(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  105. fp := &FirewallPacket{}
  106. err := newPacket(p, false, fp)
  107. if err != nil {
  108. l.Warnf("error while parsing outgoing packet for firewall check; %v", err)
  109. return
  110. }
  111. // check if packet is in outbound fw rules
  112. dropReason := f.firewall.Drop(p, *fp, false, hostInfo, trustedCAs)
  113. if dropReason != nil {
  114. if l.Level >= logrus.DebugLevel {
  115. l.WithField("fwPacket", fp).
  116. WithField("reason", dropReason).
  117. Debugln("dropping cached packet")
  118. }
  119. return
  120. }
  121. f.sendNoMetrics(message, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out)
  122. if f.lightHouse != nil && *hostInfo.ConnectionState.messageCounter%5000 == 0 {
  123. f.lightHouse.Query(fp.RemoteIP, f)
  124. }
  125. }
  126. // SendMessageToVpnIp handles real ip:port lookup and sends to the current best known address for vpnIp
  127. func (f *Interface) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  128. hostInfo := f.getOrHandshake(vpnIp)
  129. if hostInfo == nil {
  130. if l.Level >= logrus.DebugLevel {
  131. l.WithField("vpnIp", IntIp(vpnIp)).
  132. Debugln("dropping SendMessageToVpnIp, vpnIp not in our CIDR or in unsafe routes")
  133. }
  134. return
  135. }
  136. if !hostInfo.ConnectionState.ready {
  137. // Because we might be sending stored packets, lock here to stop new things going to
  138. // the packet queue.
  139. hostInfo.ConnectionState.queueLock.Lock()
  140. if !hostInfo.ConnectionState.ready {
  141. hostInfo.cachePacket(t, st, p, f.sendMessageToVpnIp)
  142. hostInfo.ConnectionState.queueLock.Unlock()
  143. return
  144. }
  145. hostInfo.ConnectionState.queueLock.Unlock()
  146. }
  147. f.sendMessageToVpnIp(t, st, hostInfo, p, nb, out)
  148. return
  149. }
  150. func (f *Interface) sendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  151. f.send(t, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out)
  152. }
  153. // SendMessageToAll handles real ip:port lookup and sends to all known addresses for vpnIp
  154. func (f *Interface) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  155. hostInfo := f.getOrHandshake(vpnIp)
  156. if hostInfo == nil {
  157. if l.Level >= logrus.DebugLevel {
  158. l.WithField("vpnIp", IntIp(vpnIp)).
  159. Debugln("dropping SendMessageToAll, vpnIp not in our CIDR or in unsafe routes")
  160. }
  161. return
  162. }
  163. if hostInfo.ConnectionState.ready == false {
  164. // Because we might be sending stored packets, lock here to stop new things going to
  165. // the packet queue.
  166. hostInfo.ConnectionState.queueLock.Lock()
  167. if !hostInfo.ConnectionState.ready {
  168. hostInfo.cachePacket(t, st, p, f.sendMessageToAll)
  169. hostInfo.ConnectionState.queueLock.Unlock()
  170. return
  171. }
  172. hostInfo.ConnectionState.queueLock.Unlock()
  173. }
  174. f.sendMessageToAll(t, st, hostInfo, p, nb, out)
  175. return
  176. }
  177. func (f *Interface) sendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, b []byte) {
  178. for _, r := range hostInfo.RemoteUDPAddrs() {
  179. f.send(t, st, hostInfo.ConnectionState, hostInfo, r, p, nb, b)
  180. }
  181. }
  182. func (f *Interface) send(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) {
  183. f.messageMetrics.Tx(t, st, 1)
  184. f.sendNoMetrics(t, st, ci, hostinfo, remote, p, nb, out)
  185. }
  186. func (f *Interface) sendNoMetrics(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) uint64 {
  187. if ci.eKey == nil {
  188. //TODO: log warning
  189. return 0
  190. }
  191. var err error
  192. //TODO: enable if we do more than 1 tun queue
  193. //ci.writeLock.Lock()
  194. c := atomic.AddUint64(ci.messageCounter, 1)
  195. //l.WithField("trace", string(debug.Stack())).Error("out Header ", &Header{Version, t, st, 0, hostinfo.remoteIndexId, c}, p)
  196. out = HeaderEncode(out, Version, uint8(t), uint8(st), hostinfo.remoteIndexId, c)
  197. f.connectionManager.Out(hostinfo.hostId)
  198. out, err = ci.eKey.EncryptDanger(out, out, p, c, nb)
  199. //TODO: see above note on lock
  200. //ci.writeLock.Unlock()
  201. if err != nil {
  202. hostinfo.logger().WithError(err).
  203. WithField("udpAddr", remote).WithField("counter", c).
  204. WithField("attemptedCounter", ci.messageCounter).
  205. Error("Failed to encrypt outgoing packet")
  206. return c
  207. }
  208. err = f.outside.WriteTo(out, remote)
  209. if err != nil {
  210. hostinfo.logger().WithError(err).
  211. WithField("udpAddr", remote).Error("Failed to write outgoing packet")
  212. }
  213. return c
  214. }
  215. func isMulticast(ip uint32) bool {
  216. // Class D multicast
  217. if (((ip >> 24) & 0xff) & 0xf0) == 0xe0 {
  218. return true
  219. }
  220. return false
  221. }