2
0

interface.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package nebula
  2. import (
  3. "errors"
  4. "io"
  5. "net"
  6. "os"
  7. "runtime"
  8. "time"
  9. "github.com/rcrowley/go-metrics"
  10. "github.com/sirupsen/logrus"
  11. "github.com/slackhq/nebula/cert"
  12. )
  13. const mtu = 9001
  14. type Inside interface {
  15. io.ReadWriteCloser
  16. Activate() error
  17. CidrNet() *net.IPNet
  18. DeviceName() string
  19. WriteRaw([]byte) error
  20. NewMultiQueueReader() (io.ReadWriteCloser, error)
  21. }
  22. type InterfaceConfig struct {
  23. HostMap *HostMap
  24. Outside *udpConn
  25. Inside Inside
  26. certState *CertState
  27. Cipher string
  28. Firewall *Firewall
  29. ServeDns bool
  30. HandshakeManager *HandshakeManager
  31. lightHouse *LightHouse
  32. checkInterval int
  33. pendingDeletionInterval int
  34. DropLocalBroadcast bool
  35. DropMulticast bool
  36. UDPBatchSize int
  37. routines int
  38. MessageMetrics *MessageMetrics
  39. version string
  40. caPool *cert.NebulaCAPool
  41. ConntrackCacheTimeout time.Duration
  42. l *logrus.Logger
  43. }
  44. type Interface struct {
  45. hostMap *HostMap
  46. outside *udpConn
  47. inside Inside
  48. certState *CertState
  49. cipher string
  50. firewall *Firewall
  51. connectionManager *connectionManager
  52. handshakeManager *HandshakeManager
  53. serveDns bool
  54. createTime time.Time
  55. lightHouse *LightHouse
  56. localBroadcast uint32
  57. myVpnIp uint32
  58. dropLocalBroadcast bool
  59. dropMulticast bool
  60. udpBatchSize int
  61. routines int
  62. caPool *cert.NebulaCAPool
  63. // rebindCount is used to decide if an active tunnel should trigger a punch notification through a lighthouse
  64. rebindCount int8
  65. version string
  66. conntrackCacheTimeout time.Duration
  67. writers []*udpConn
  68. readers []io.ReadWriteCloser
  69. metricHandshakes metrics.Histogram
  70. messageMetrics *MessageMetrics
  71. l *logrus.Logger
  72. }
  73. func NewInterface(c *InterfaceConfig) (*Interface, error) {
  74. if c.Outside == nil {
  75. return nil, errors.New("no outside connection")
  76. }
  77. if c.Inside == nil {
  78. return nil, errors.New("no inside interface (tun)")
  79. }
  80. if c.certState == nil {
  81. return nil, errors.New("no certificate state")
  82. }
  83. if c.Firewall == nil {
  84. return nil, errors.New("no firewall rules")
  85. }
  86. ifce := &Interface{
  87. hostMap: c.HostMap,
  88. outside: c.Outside,
  89. inside: c.Inside,
  90. certState: c.certState,
  91. cipher: c.Cipher,
  92. firewall: c.Firewall,
  93. serveDns: c.ServeDns,
  94. handshakeManager: c.HandshakeManager,
  95. createTime: time.Now(),
  96. lightHouse: c.lightHouse,
  97. localBroadcast: ip2int(c.certState.certificate.Details.Ips[0].IP) | ^ip2int(c.certState.certificate.Details.Ips[0].Mask),
  98. dropLocalBroadcast: c.DropLocalBroadcast,
  99. dropMulticast: c.DropMulticast,
  100. udpBatchSize: c.UDPBatchSize,
  101. routines: c.routines,
  102. version: c.version,
  103. writers: make([]*udpConn, c.routines),
  104. readers: make([]io.ReadWriteCloser, c.routines),
  105. caPool: c.caPool,
  106. myVpnIp: ip2int(c.certState.certificate.Details.Ips[0].IP),
  107. conntrackCacheTimeout: c.ConntrackCacheTimeout,
  108. metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
  109. messageMetrics: c.MessageMetrics,
  110. l: c.l,
  111. }
  112. ifce.connectionManager = newConnectionManager(c.l, ifce, c.checkInterval, c.pendingDeletionInterval)
  113. return ifce, nil
  114. }
  115. // activate creates the interface on the host. After the interface is created, any
  116. // other services that want to bind listeners to its IP may do so successfully. However,
  117. // the interface isn't going to process anything until run() is called.
  118. func (f *Interface) activate() {
  119. // actually turn on tun dev
  120. addr, err := f.outside.LocalAddr()
  121. if err != nil {
  122. f.l.WithError(err).Error("Failed to get udp listen address")
  123. }
  124. f.l.WithField("interface", f.inside.DeviceName()).WithField("network", f.inside.CidrNet().String()).
  125. WithField("build", f.version).WithField("udpAddr", addr).
  126. Info("Nebula interface is active")
  127. metrics.GetOrRegisterGauge("routines", nil).Update(int64(f.routines))
  128. // Prepare n tun queues
  129. var reader io.ReadWriteCloser = f.inside
  130. for i := 0; i < f.routines; i++ {
  131. if i > 0 {
  132. reader, err = f.inside.NewMultiQueueReader()
  133. if err != nil {
  134. f.l.Fatal(err)
  135. }
  136. }
  137. f.readers[i] = reader
  138. }
  139. if err := f.inside.Activate(); err != nil {
  140. f.l.Fatal(err)
  141. }
  142. }
  143. func (f *Interface) run() {
  144. // Launch n queues to read packets from udp
  145. for i := 0; i < f.routines; i++ {
  146. go f.listenOut(i)
  147. }
  148. // Launch n queues to read packets from tun dev
  149. for i := 0; i < f.routines; i++ {
  150. go f.listenIn(f.readers[i], i)
  151. }
  152. }
  153. func (f *Interface) listenOut(i int) {
  154. runtime.LockOSThread()
  155. var li *udpConn
  156. // TODO clean this up with a coherent interface for each outside connection
  157. if i > 0 {
  158. li = f.writers[i]
  159. } else {
  160. li = f.outside
  161. }
  162. li.ListenOut(f, i)
  163. }
  164. func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
  165. runtime.LockOSThread()
  166. packet := make([]byte, mtu)
  167. out := make([]byte, mtu)
  168. fwPacket := &FirewallPacket{}
  169. nb := make([]byte, 12, 12)
  170. conntrackCache := NewConntrackCacheTicker(f.conntrackCacheTimeout)
  171. for {
  172. n, err := reader.Read(packet)
  173. if err != nil {
  174. f.l.WithError(err).Error("Error while reading outbound packet")
  175. // This only seems to happen when something fatal happens to the fd, so exit.
  176. os.Exit(2)
  177. }
  178. f.consumeInsidePacket(packet[:n], fwPacket, nb, out, i, conntrackCache.Get(f.l))
  179. }
  180. }
  181. func (f *Interface) RegisterConfigChangeCallbacks(c *Config) {
  182. c.RegisterReloadCallback(f.reloadCA)
  183. c.RegisterReloadCallback(f.reloadCertKey)
  184. c.RegisterReloadCallback(f.reloadFirewall)
  185. for _, udpConn := range f.writers {
  186. c.RegisterReloadCallback(udpConn.reloadConfig)
  187. }
  188. }
  189. func (f *Interface) reloadCA(c *Config) {
  190. // reload and check regardless
  191. // todo: need mutex?
  192. newCAs, err := loadCAFromConfig(f.l, c)
  193. if err != nil {
  194. f.l.WithError(err).Error("Could not refresh trusted CA certificates")
  195. return
  196. }
  197. f.caPool = newCAs
  198. f.l.WithField("fingerprints", f.caPool.GetFingerprints()).Info("Trusted CA certificates refreshed")
  199. }
  200. func (f *Interface) reloadCertKey(c *Config) {
  201. // reload and check in all cases
  202. cs, err := NewCertStateFromConfig(c)
  203. if err != nil {
  204. f.l.WithError(err).Error("Could not refresh client cert")
  205. return
  206. }
  207. // did IP in cert change? if so, don't set
  208. oldIPs := f.certState.certificate.Details.Ips
  209. newIPs := cs.certificate.Details.Ips
  210. if len(oldIPs) > 0 && len(newIPs) > 0 && oldIPs[0].String() != newIPs[0].String() {
  211. f.l.WithField("new_ip", newIPs[0]).WithField("old_ip", oldIPs[0]).Error("IP in new cert was different from old")
  212. return
  213. }
  214. f.certState = cs
  215. f.l.WithField("cert", cs.certificate).Info("Client cert refreshed from disk")
  216. }
  217. func (f *Interface) reloadFirewall(c *Config) {
  218. //TODO: need to trigger/detect if the certificate changed too
  219. if c.HasChanged("firewall") == false {
  220. f.l.Debug("No firewall config change detected")
  221. return
  222. }
  223. fw, err := NewFirewallFromConfig(f.l, f.certState.certificate, c)
  224. if err != nil {
  225. f.l.WithError(err).Error("Error while creating firewall during reload")
  226. return
  227. }
  228. oldFw := f.firewall
  229. conntrack := oldFw.Conntrack
  230. conntrack.Lock()
  231. defer conntrack.Unlock()
  232. fw.rulesVersion = oldFw.rulesVersion + 1
  233. // If rulesVersion is back to zero, we have wrapped all the way around. Be
  234. // safe and just reset conntrack in this case.
  235. if fw.rulesVersion == 0 {
  236. f.l.WithField("firewallHash", fw.GetRuleHash()).
  237. WithField("oldFirewallHash", oldFw.GetRuleHash()).
  238. WithField("rulesVersion", fw.rulesVersion).
  239. Warn("firewall rulesVersion has overflowed, resetting conntrack")
  240. } else {
  241. fw.Conntrack = conntrack
  242. }
  243. f.firewall = fw
  244. oldFw.Destroy()
  245. f.l.WithField("firewallHash", fw.GetRuleHash()).
  246. WithField("oldFirewallHash", oldFw.GetRuleHash()).
  247. WithField("rulesVersion", fw.rulesVersion).
  248. Info("New firewall has been installed")
  249. }
  250. func (f *Interface) emitStats(i time.Duration) {
  251. ticker := time.NewTicker(i)
  252. udpStats := NewUDPStatsEmitter(f.writers)
  253. for range ticker.C {
  254. f.firewall.EmitStats()
  255. f.handshakeManager.EmitStats()
  256. udpStats()
  257. }
  258. }