inside.go 6.6 KB

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