inside.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. }
  82. return hostinfo
  83. }
  84. func (f *Interface) sendMessageNow(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  85. fp := &FirewallPacket{}
  86. err := newPacket(p, false, fp)
  87. if err != nil {
  88. l.Warnf("error while parsing outgoing packet for firewall check; %v", err)
  89. return
  90. }
  91. // check if packet is in outbound fw rules
  92. dropReason := f.firewall.Drop(p, *fp, false, hostInfo, trustedCAs)
  93. if dropReason != nil {
  94. if l.Level >= logrus.DebugLevel {
  95. l.WithField("fwPacket", fp).
  96. WithField("reason", dropReason).
  97. Debugln("dropping cached packet")
  98. }
  99. return
  100. }
  101. f.sendNoMetrics(message, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out)
  102. if f.lightHouse != nil && *hostInfo.ConnectionState.messageCounter%5000 == 0 {
  103. f.lightHouse.Query(fp.RemoteIP, f)
  104. }
  105. }
  106. // SendMessageToVpnIp handles real ip:port lookup and sends to the current best known address for vpnIp
  107. func (f *Interface) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  108. hostInfo := f.getOrHandshake(vpnIp)
  109. if !hostInfo.ConnectionState.ready {
  110. // Because we might be sending stored packets, lock here to stop new things going to
  111. // the packet queue.
  112. hostInfo.ConnectionState.queueLock.Lock()
  113. if !hostInfo.ConnectionState.ready {
  114. hostInfo.cachePacket(t, st, p, f.sendMessageToVpnIp)
  115. hostInfo.ConnectionState.queueLock.Unlock()
  116. return
  117. }
  118. hostInfo.ConnectionState.queueLock.Unlock()
  119. }
  120. f.sendMessageToVpnIp(t, st, hostInfo, p, nb, out)
  121. return
  122. }
  123. func (f *Interface) sendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, out []byte) {
  124. f.send(t, st, hostInfo.ConnectionState, hostInfo, hostInfo.remote, p, nb, out)
  125. }
  126. // SendMessageToAll handles real ip:port lookup and sends to all known addresses for vpnIp
  127. func (f *Interface) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  128. hostInfo := f.getOrHandshake(vpnIp)
  129. if hostInfo.ConnectionState.ready == false {
  130. // Because we might be sending stored packets, lock here to stop new things going to
  131. // the packet queue.
  132. hostInfo.ConnectionState.queueLock.Lock()
  133. if !hostInfo.ConnectionState.ready {
  134. hostInfo.cachePacket(t, st, p, f.sendMessageToAll)
  135. hostInfo.ConnectionState.queueLock.Unlock()
  136. return
  137. }
  138. hostInfo.ConnectionState.queueLock.Unlock()
  139. }
  140. f.sendMessageToAll(t, st, hostInfo, p, nb, out)
  141. return
  142. }
  143. func (f *Interface) sendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, hostInfo *HostInfo, p, nb, b []byte) {
  144. for _, r := range hostInfo.RemoteUDPAddrs() {
  145. f.send(t, st, hostInfo.ConnectionState, hostInfo, r, p, nb, b)
  146. }
  147. }
  148. func (f *Interface) send(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) {
  149. f.messageMetrics.Tx(t, st, 1)
  150. f.sendNoMetrics(t, st, ci, hostinfo, remote, p, nb, out)
  151. }
  152. func (f *Interface) sendNoMetrics(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) {
  153. if ci.eKey == nil {
  154. //TODO: log warning
  155. return
  156. }
  157. var err error
  158. //TODO: enable if we do more than 1 tun queue
  159. //ci.writeLock.Lock()
  160. c := atomic.AddUint64(ci.messageCounter, 1)
  161. //l.WithField("trace", string(debug.Stack())).Error("out Header ", &Header{Version, t, st, 0, hostinfo.remoteIndexId, c}, p)
  162. out = HeaderEncode(out, Version, uint8(t), uint8(st), hostinfo.remoteIndexId, c)
  163. f.connectionManager.Out(hostinfo.hostId)
  164. out, err = ci.eKey.EncryptDanger(out, out, p, c, nb)
  165. //TODO: see above note on lock
  166. //ci.writeLock.Unlock()
  167. if err != nil {
  168. hostinfo.logger().WithError(err).
  169. WithField("udpAddr", remote).WithField("counter", c).
  170. WithField("attemptedCounter", ci.messageCounter).
  171. Error("Failed to encrypt outgoing packet")
  172. return
  173. }
  174. err = f.outside.WriteTo(out, remote)
  175. if err != nil {
  176. hostinfo.logger().WithError(err).
  177. WithField("udpAddr", remote).Error("Failed to write outgoing packet")
  178. }
  179. }
  180. func isMulticast(ip uint32) bool {
  181. // Class D multicast
  182. if (((ip >> 24) & 0xff) & 0xf0) == 0xe0 {
  183. return true
  184. }
  185. return false
  186. }