inside.go 6.4 KB

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