inside.go 6.3 KB

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