inside.go 6.4 KB

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