inside.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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) {
  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. ci := hostinfo.ConnectionState
  27. if ci.ready == false {
  28. // Because we might be sending stored packets, lock here to stop new things going to
  29. // the packet queue.
  30. ci.queueLock.Lock()
  31. if !ci.ready {
  32. hostinfo.cachePacket(message, 0, packet, f.sendMessageNow)
  33. ci.queueLock.Unlock()
  34. return
  35. }
  36. ci.queueLock.Unlock()
  37. }
  38. dropReason := f.firewall.Drop(packet, *fwPacket, false, hostinfo, trustedCAs)
  39. if dropReason == nil {
  40. f.sendNoMetrics(message, 0, ci, hostinfo, hostinfo.remote, packet, nb, out)
  41. if f.lightHouse != nil && *ci.messageCounter%5000 == 0 {
  42. f.lightHouse.Query(fwPacket.RemoteIP, f)
  43. }
  44. } else if l.Level >= logrus.DebugLevel {
  45. hostinfo.logger().
  46. WithField("fwPacket", fwPacket).
  47. WithField("reason", dropReason).
  48. Debugln("dropping outbound packet")
  49. }
  50. }
  51. func (f *Interface) getOrHandshake(vpnIp uint32) *HostInfo {
  52. if f.hostMap.vpnCIDR.Contains(int2ip(vpnIp)) == false {
  53. vpnIp = f.hostMap.queryUnsafeRoute(vpnIp)
  54. }
  55. hostinfo, err := f.hostMap.PromoteBestQueryVpnIP(vpnIp, f)
  56. //if err != nil || hostinfo.ConnectionState == nil {
  57. if err != nil {
  58. hostinfo, err = f.handshakeManager.pendingHostMap.QueryVpnIP(vpnIp)
  59. if err != nil {
  60. hostinfo = f.handshakeManager.AddVpnIP(vpnIp)
  61. }
  62. }
  63. ci := hostinfo.ConnectionState
  64. if ci != nil && ci.eKey != nil && ci.ready {
  65. return hostinfo
  66. }
  67. if ci == nil {
  68. // if we don't have a connection state, then send a handshake initiation
  69. ci = f.newConnectionState(true, noise.HandshakeIX, []byte{}, 0)
  70. // FIXME: Maybe make XX selectable, but probably not since psk makes it nearly pointless for us.
  71. //ci = f.newConnectionState(true, noise.HandshakeXX, []byte{}, 0)
  72. hostinfo.ConnectionState = ci
  73. } else if ci.eKey == nil {
  74. // if we don't have any state at all, create it
  75. }
  76. // If we have already created the handshake packet, we don't want to call the function at all.
  77. if !hostinfo.HandshakeReady {
  78. ixHandshakeStage0(f, vpnIp, hostinfo)
  79. // FIXME: Maybe make XX selectable, but probably not since psk makes it nearly pointless for us.
  80. //xx_handshakeStage0(f, ip, hostinfo)
  81. // If this is a static host, we don't need to wait for the HostQueryReply
  82. // We can trigger the handshake right now
  83. if _, ok := f.lightHouse.staticList[vpnIp]; ok {
  84. select {
  85. case f.handshakeManager.trigger <- vpnIp:
  86. default:
  87. }
  88. }
  89. }
  90. return hostinfo
  91. }
  92. func (f *Interface) sendMessageNow(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  93. fp := &FirewallPacket{}
  94. err := newPacket(p, false, fp)
  95. if err != nil {
  96. l.Warnf("error while parsing outgoing packet for firewall check; %v", err)
  97. return
  98. }
  99. // check if packet is in outbound fw rules
  100. dropReason := f.firewall.Drop(p, *fp, false, hostInfo, trustedCAs)
  101. if dropReason != nil {
  102. if l.Level >= logrus.DebugLevel {
  103. l.WithField("fwPacket", fp).
  104. WithField("reason", dropReason).
  105. Debugln("dropping cached packet")
  106. }
  107. return
  108. }
  109. f.sendNoMetrics(message, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out)
  110. if f.lightHouse != nil && *hostInfo.ConnectionState.messageCounter%5000 == 0 {
  111. f.lightHouse.Query(fp.RemoteIP, f)
  112. }
  113. }
  114. // SendMessageToVpnIp handles real ip:port lookup and sends to the current best known address for vpnIp
  115. func (f *Interface) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  116. hostInfo := f.getOrHandshake(vpnIp)
  117. if !hostInfo.ConnectionState.ready {
  118. // Because we might be sending stored packets, lock here to stop new things going to
  119. // the packet queue.
  120. hostInfo.ConnectionState.queueLock.Lock()
  121. if !hostInfo.ConnectionState.ready {
  122. hostInfo.cachePacket(t, st, p, f.sendMessageToVpnIp)
  123. hostInfo.ConnectionState.queueLock.Unlock()
  124. return
  125. }
  126. hostInfo.ConnectionState.queueLock.Unlock()
  127. }
  128. f.sendMessageToVpnIp(t, st, hostInfo, p, nb, out)
  129. return
  130. }
  131. func (f *Interface) sendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  132. f.send(t, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out)
  133. }
  134. // SendMessageToAll handles real ip:port lookup and sends to all known addresses for vpnIp
  135. func (f *Interface) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  136. hostInfo := f.getOrHandshake(vpnIp)
  137. if hostInfo.ConnectionState.ready == false {
  138. // Because we might be sending stored packets, lock here to stop new things going to
  139. // the packet queue.
  140. hostInfo.ConnectionState.queueLock.Lock()
  141. if !hostInfo.ConnectionState.ready {
  142. hostInfo.cachePacket(t, st, p, f.sendMessageToAll)
  143. hostInfo.ConnectionState.queueLock.Unlock()
  144. return
  145. }
  146. hostInfo.ConnectionState.queueLock.Unlock()
  147. }
  148. f.sendMessageToAll(t, st, hostInfo, p, nb, out)
  149. return
  150. }
  151. func (f *Interface) sendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, b []byte) {
  152. for _, r := range hostInfo.RemoteUDPAddrs() {
  153. f.send(t, st, hostInfo.ConnectionState, hostInfo, r, p, nb, b)
  154. }
  155. }
  156. func (f *Interface) send(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) {
  157. f.messageMetrics.Tx(t, st, 1)
  158. f.sendNoMetrics(t, st, ci, hostinfo, remote, p, nb, out)
  159. }
  160. func (f *Interface) sendNoMetrics(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) {
  161. if ci.eKey == nil {
  162. //TODO: log warning
  163. return
  164. }
  165. var err error
  166. //TODO: enable if we do more than 1 tun queue
  167. //ci.writeLock.Lock()
  168. c := atomic.AddUint64(ci.messageCounter, 1)
  169. //l.WithField("trace", string(debug.Stack())).Error("out Header ", &Header{Version, t, st, 0, hostinfo.remoteIndexId, c}, p)
  170. out = HeaderEncode(out, Version, uint8(t), uint8(st), hostinfo.remoteIndexId, c)
  171. f.connectionManager.Out(hostinfo.hostId)
  172. out, err = ci.eKey.EncryptDanger(out, out, p, c, nb)
  173. //TODO: see above note on lock
  174. //ci.writeLock.Unlock()
  175. if err != nil {
  176. hostinfo.logger().WithError(err).
  177. WithField("udpAddr", remote).WithField("counter", c).
  178. WithField("attemptedCounter", ci.messageCounter).
  179. Error("Failed to encrypt outgoing packet")
  180. return
  181. }
  182. err = f.outside.WriteTo(out, remote)
  183. if err != nil {
  184. hostinfo.logger().WithError(err).
  185. WithField("udpAddr", remote).Error("Failed to write outgoing packet")
  186. }
  187. }
  188. func isMulticast(ip uint32) bool {
  189. // Class D multicast
  190. if (((ip >> 24) & 0xff) & 0xf0) == 0xe0 {
  191. return true
  192. }
  193. return false
  194. }