firewall.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. package nebula
  2. import (
  3. "crypto/sha256"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "net"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "sync/atomic"
  15. "time"
  16. "github.com/rcrowley/go-metrics"
  17. "github.com/sirupsen/logrus"
  18. "github.com/slackhq/nebula/cert"
  19. )
  20. const (
  21. fwProtoAny = 0 // When we want to handle HOPOPT (0) we can change this, if ever
  22. fwProtoTCP = 6
  23. fwProtoUDP = 17
  24. fwProtoICMP = 1
  25. fwPortAny = 0 // Special value for matching `port: any`
  26. fwPortFragment = -1 // Special value for matching `port: fragment`
  27. )
  28. const tcpACK = 0x10
  29. const tcpFIN = 0x01
  30. type FirewallInterface interface {
  31. AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip *net.IPNet, caName string, caSha string) error
  32. }
  33. type conn struct {
  34. Expires time.Time // Time when this conntrack entry will expire
  35. Sent time.Time // If tcp rtt tracking is enabled this will be when Seq was last set
  36. Seq uint32 // If tcp rtt tracking is enabled this will be the seq we are looking for an ack
  37. // record why the original connection passed the firewall, so we can re-validate
  38. // after ruleset changes. Note, rulesVersion is a uint16 so that these two
  39. // fields pack for free after the uint32 above
  40. incoming bool
  41. rulesVersion uint16
  42. }
  43. // TODO: need conntrack max tracked connections handling
  44. type Firewall struct {
  45. Conntrack *FirewallConntrack
  46. InRules *FirewallTable
  47. OutRules *FirewallTable
  48. //TODO: we should have many more options for TCP, an option for ICMP, and mimic the kernel a bit better
  49. // https://www.kernel.org/doc/Documentation/networking/nf_conntrack-sysctl.txt
  50. TCPTimeout time.Duration //linux: 5 days max
  51. UDPTimeout time.Duration //linux: 180s max
  52. DefaultTimeout time.Duration //linux: 600s
  53. // Used to ensure we don't emit local packets for ips we don't own
  54. localIps *CIDRTree
  55. rules string
  56. rulesVersion uint16
  57. trackTCPRTT bool
  58. metricTCPRTT metrics.Histogram
  59. }
  60. type FirewallConntrack struct {
  61. sync.Mutex
  62. Conns map[FirewallPacket]*conn
  63. TimerWheel *TimerWheel
  64. }
  65. type FirewallTable struct {
  66. TCP firewallPort
  67. UDP firewallPort
  68. ICMP firewallPort
  69. AnyProto firewallPort
  70. }
  71. func newFirewallTable() *FirewallTable {
  72. return &FirewallTable{
  73. TCP: firewallPort{},
  74. UDP: firewallPort{},
  75. ICMP: firewallPort{},
  76. AnyProto: firewallPort{},
  77. }
  78. }
  79. type FirewallCA struct {
  80. Any *FirewallRule
  81. CANames map[string]*FirewallRule
  82. CAShas map[string]*FirewallRule
  83. }
  84. type FirewallRule struct {
  85. // Any makes Hosts, Groups, and CIDR irrelevant
  86. Any bool
  87. Hosts map[string]struct{}
  88. Groups [][]string
  89. CIDR *CIDRTree
  90. }
  91. // Even though ports are uint16, int32 maps are faster for lookup
  92. // Plus we can use `-1` for fragment rules
  93. type firewallPort map[int32]*FirewallCA
  94. type FirewallPacket struct {
  95. LocalIP uint32
  96. RemoteIP uint32
  97. LocalPort uint16
  98. RemotePort uint16
  99. Protocol uint8
  100. Fragment bool
  101. }
  102. func (fp *FirewallPacket) Copy() *FirewallPacket {
  103. return &FirewallPacket{
  104. LocalIP: fp.LocalIP,
  105. RemoteIP: fp.RemoteIP,
  106. LocalPort: fp.LocalPort,
  107. RemotePort: fp.RemotePort,
  108. Protocol: fp.Protocol,
  109. Fragment: fp.Fragment,
  110. }
  111. }
  112. func (fp FirewallPacket) MarshalJSON() ([]byte, error) {
  113. var proto string
  114. switch fp.Protocol {
  115. case fwProtoTCP:
  116. proto = "tcp"
  117. case fwProtoICMP:
  118. proto = "icmp"
  119. case fwProtoUDP:
  120. proto = "udp"
  121. default:
  122. proto = fmt.Sprintf("unknown %v", fp.Protocol)
  123. }
  124. return json.Marshal(m{
  125. "LocalIP": int2ip(fp.LocalIP).String(),
  126. "RemoteIP": int2ip(fp.RemoteIP).String(),
  127. "LocalPort": fp.LocalPort,
  128. "RemotePort": fp.RemotePort,
  129. "Protocol": proto,
  130. "Fragment": fp.Fragment,
  131. })
  132. }
  133. // NewFirewall creates a new Firewall object. A TimerWheel is created for you from the provided timeouts.
  134. func NewFirewall(tcpTimeout, UDPTimeout, defaultTimeout time.Duration, c *cert.NebulaCertificate) *Firewall {
  135. //TODO: error on 0 duration
  136. var min, max time.Duration
  137. if tcpTimeout < UDPTimeout {
  138. min = tcpTimeout
  139. max = UDPTimeout
  140. } else {
  141. min = UDPTimeout
  142. max = tcpTimeout
  143. }
  144. if defaultTimeout < min {
  145. min = defaultTimeout
  146. } else if defaultTimeout > max {
  147. max = defaultTimeout
  148. }
  149. localIps := NewCIDRTree()
  150. for _, ip := range c.Details.Ips {
  151. localIps.AddCIDR(&net.IPNet{IP: ip.IP, Mask: net.IPMask{255, 255, 255, 255}}, struct{}{})
  152. }
  153. for _, n := range c.Details.Subnets {
  154. localIps.AddCIDR(n, struct{}{})
  155. }
  156. return &Firewall{
  157. Conntrack: &FirewallConntrack{
  158. Conns: make(map[FirewallPacket]*conn),
  159. TimerWheel: NewTimerWheel(min, max),
  160. },
  161. InRules: newFirewallTable(),
  162. OutRules: newFirewallTable(),
  163. TCPTimeout: tcpTimeout,
  164. UDPTimeout: UDPTimeout,
  165. DefaultTimeout: defaultTimeout,
  166. localIps: localIps,
  167. metricTCPRTT: metrics.GetOrRegisterHistogram("network.tcp.rtt", nil, metrics.NewExpDecaySample(1028, 0.015)),
  168. }
  169. }
  170. func NewFirewallFromConfig(nc *cert.NebulaCertificate, c *Config) (*Firewall, error) {
  171. fw := NewFirewall(
  172. c.GetDuration("firewall.conntrack.tcp_timeout", time.Minute*12),
  173. c.GetDuration("firewall.conntrack.udp_timeout", time.Minute*3),
  174. c.GetDuration("firewall.conntrack.default_timeout", time.Minute*10),
  175. nc,
  176. //TODO: max_connections
  177. )
  178. err := AddFirewallRulesFromConfig(false, c, fw)
  179. if err != nil {
  180. return nil, err
  181. }
  182. err = AddFirewallRulesFromConfig(true, c, fw)
  183. if err != nil {
  184. return nil, err
  185. }
  186. return fw, nil
  187. }
  188. // AddRule properly creates the in memory rule structure for a firewall table.
  189. func (f *Firewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip *net.IPNet, caName string, caSha string) error {
  190. // Under gomobile, stringing a nil pointer with fmt causes an abort in debug mode for iOS
  191. // https://github.com/golang/go/issues/14131
  192. sIp := ""
  193. if ip != nil {
  194. sIp = ip.String()
  195. }
  196. // We need this rule string because we generate a hash. Removing this will break firewall reload.
  197. ruleString := fmt.Sprintf(
  198. "incoming: %v, proto: %v, startPort: %v, endPort: %v, groups: %v, host: %v, ip: %v, caName: %v, caSha: %s",
  199. incoming, proto, startPort, endPort, groups, host, sIp, caName, caSha,
  200. )
  201. f.rules += ruleString + "\n"
  202. direction := "incoming"
  203. if !incoming {
  204. direction = "outgoing"
  205. }
  206. l.WithField("firewallRule", m{"direction": direction, "proto": proto, "startPort": startPort, "endPort": endPort, "groups": groups, "host": host, "ip": sIp, "caName": caName, "caSha": caSha}).
  207. Info("Firewall rule added")
  208. var (
  209. ft *FirewallTable
  210. fp firewallPort
  211. )
  212. if incoming {
  213. ft = f.InRules
  214. } else {
  215. ft = f.OutRules
  216. }
  217. switch proto {
  218. case fwProtoTCP:
  219. fp = ft.TCP
  220. case fwProtoUDP:
  221. fp = ft.UDP
  222. case fwProtoICMP:
  223. fp = ft.ICMP
  224. case fwProtoAny:
  225. fp = ft.AnyProto
  226. default:
  227. return fmt.Errorf("unknown protocol %v", proto)
  228. }
  229. return fp.addRule(startPort, endPort, groups, host, ip, caName, caSha)
  230. }
  231. // GetRuleHash returns a hash representation of all inbound and outbound rules
  232. func (f *Firewall) GetRuleHash() string {
  233. sum := sha256.Sum256([]byte(f.rules))
  234. return hex.EncodeToString(sum[:])
  235. }
  236. func AddFirewallRulesFromConfig(inbound bool, config *Config, fw FirewallInterface) error {
  237. var table string
  238. if inbound {
  239. table = "firewall.inbound"
  240. } else {
  241. table = "firewall.outbound"
  242. }
  243. r := config.Get(table)
  244. if r == nil {
  245. return nil
  246. }
  247. rs, ok := r.([]interface{})
  248. if !ok {
  249. return fmt.Errorf("%s failed to parse, should be an array of rules", table)
  250. }
  251. for i, t := range rs {
  252. var groups []string
  253. r, err := convertRule(t, table, i)
  254. if err != nil {
  255. return fmt.Errorf("%s rule #%v; %s", table, i, err)
  256. }
  257. if r.Code != "" && r.Port != "" {
  258. return fmt.Errorf("%s rule #%v; only one of port or code should be provided", table, i)
  259. }
  260. if r.Host == "" && len(r.Groups) == 0 && r.Group == "" && r.Cidr == "" && r.CAName == "" && r.CASha == "" {
  261. return fmt.Errorf("%s rule #%v; at least one of host, group, cidr, ca_name, or ca_sha must be provided", table, i)
  262. }
  263. if len(r.Groups) > 0 {
  264. groups = r.Groups
  265. }
  266. if r.Group != "" {
  267. // Check if we have both groups and group provided in the rule config
  268. if len(groups) > 0 {
  269. return fmt.Errorf("%s rule #%v; only one of group or groups should be defined, both provided", table, i)
  270. }
  271. groups = []string{r.Group}
  272. }
  273. var sPort, errPort string
  274. if r.Code != "" {
  275. errPort = "code"
  276. sPort = r.Code
  277. } else {
  278. errPort = "port"
  279. sPort = r.Port
  280. }
  281. startPort, endPort, err := parsePort(sPort)
  282. if err != nil {
  283. return fmt.Errorf("%s rule #%v; %s %s", table, i, errPort, err)
  284. }
  285. var proto uint8
  286. switch r.Proto {
  287. case "any":
  288. proto = fwProtoAny
  289. case "tcp":
  290. proto = fwProtoTCP
  291. case "udp":
  292. proto = fwProtoUDP
  293. case "icmp":
  294. proto = fwProtoICMP
  295. default:
  296. return fmt.Errorf("%s rule #%v; proto was not understood; `%s`", table, i, r.Proto)
  297. }
  298. var cidr *net.IPNet
  299. if r.Cidr != "" {
  300. _, cidr, err = net.ParseCIDR(r.Cidr)
  301. if err != nil {
  302. return fmt.Errorf("%s rule #%v; cidr did not parse; %s", table, i, err)
  303. }
  304. }
  305. err = fw.AddRule(inbound, proto, startPort, endPort, groups, r.Host, cidr, r.CAName, r.CASha)
  306. if err != nil {
  307. return fmt.Errorf("%s rule #%v; `%s`", table, i, err)
  308. }
  309. }
  310. return nil
  311. }
  312. var ErrInvalidRemoteIP = errors.New("remote IP is not in remote certificate subnets")
  313. var ErrInvalidLocalIP = errors.New("local IP is not in list of handled local IPs")
  314. var ErrNoMatchingRule = errors.New("no matching rule in firewall table")
  315. // Drop returns an error if the packet should be dropped, explaining why. It
  316. // returns nil if the packet should not be dropped.
  317. func (f *Firewall) Drop(packet []byte, fp FirewallPacket, incoming bool, h *HostInfo, caPool *cert.NebulaCAPool, localCache ConntrackCache) error {
  318. // Check if we spoke to this tuple, if we did then allow this packet
  319. if f.inConns(packet, fp, incoming, h, caPool, localCache) {
  320. return nil
  321. }
  322. // Make sure remote address matches nebula certificate
  323. if remoteCidr := h.remoteCidr; remoteCidr != nil {
  324. if remoteCidr.Contains(fp.RemoteIP) == nil {
  325. return ErrInvalidRemoteIP
  326. }
  327. } else {
  328. // Simple case: Certificate has one IP and no subnets
  329. if fp.RemoteIP != h.hostId {
  330. return ErrInvalidRemoteIP
  331. }
  332. }
  333. // Make sure we are supposed to be handling this local ip address
  334. if f.localIps.Contains(fp.LocalIP) == nil {
  335. return ErrInvalidLocalIP
  336. }
  337. table := f.OutRules
  338. if incoming {
  339. table = f.InRules
  340. }
  341. // We now know which firewall table to check against
  342. if !table.match(fp, incoming, h.ConnectionState.peerCert, caPool) {
  343. return ErrNoMatchingRule
  344. }
  345. // We always want to conntrack since it is a faster operation
  346. f.addConn(packet, fp, incoming)
  347. return nil
  348. }
  349. // Destroy cleans up any known cyclical references so the object can be free'd my GC. This should be called if a new
  350. // firewall object is created
  351. func (f *Firewall) Destroy() {
  352. //TODO: clean references if/when needed
  353. }
  354. func (f *Firewall) EmitStats() {
  355. conntrack := f.Conntrack
  356. conntrack.Lock()
  357. conntrackCount := len(conntrack.Conns)
  358. conntrack.Unlock()
  359. metrics.GetOrRegisterGauge("firewall.conntrack.count", nil).Update(int64(conntrackCount))
  360. metrics.GetOrRegisterGauge("firewall.rules.version", nil).Update(int64(f.rulesVersion))
  361. }
  362. func (f *Firewall) inConns(packet []byte, fp FirewallPacket, incoming bool, h *HostInfo, caPool *cert.NebulaCAPool, localCache ConntrackCache) bool {
  363. if localCache != nil {
  364. if _, ok := localCache[fp]; ok {
  365. return true
  366. }
  367. }
  368. conntrack := f.Conntrack
  369. conntrack.Lock()
  370. // Purge every time we test
  371. ep, has := conntrack.TimerWheel.Purge()
  372. if has {
  373. f.evict(ep)
  374. }
  375. c, ok := conntrack.Conns[fp]
  376. if !ok {
  377. conntrack.Unlock()
  378. return false
  379. }
  380. if c.rulesVersion != f.rulesVersion {
  381. // This conntrack entry was for an older rule set, validate
  382. // it still passes with the current rule set
  383. table := f.OutRules
  384. if c.incoming {
  385. table = f.InRules
  386. }
  387. // We now know which firewall table to check against
  388. if !table.match(fp, c.incoming, h.ConnectionState.peerCert, caPool) {
  389. if l.Level >= logrus.DebugLevel {
  390. h.logger().
  391. WithField("fwPacket", fp).
  392. WithField("incoming", c.incoming).
  393. WithField("rulesVersion", f.rulesVersion).
  394. WithField("oldRulesVersion", c.rulesVersion).
  395. Debugln("dropping old conntrack entry, does not match new ruleset")
  396. }
  397. delete(conntrack.Conns, fp)
  398. conntrack.Unlock()
  399. return false
  400. }
  401. if l.Level >= logrus.DebugLevel {
  402. h.logger().
  403. WithField("fwPacket", fp).
  404. WithField("incoming", c.incoming).
  405. WithField("rulesVersion", f.rulesVersion).
  406. WithField("oldRulesVersion", c.rulesVersion).
  407. Debugln("keeping old conntrack entry, does match new ruleset")
  408. }
  409. c.rulesVersion = f.rulesVersion
  410. }
  411. switch fp.Protocol {
  412. case fwProtoTCP:
  413. c.Expires = time.Now().Add(f.TCPTimeout)
  414. if incoming {
  415. f.checkTCPRTT(c, packet)
  416. } else {
  417. setTCPRTTTracking(c, packet)
  418. }
  419. case fwProtoUDP:
  420. c.Expires = time.Now().Add(f.UDPTimeout)
  421. default:
  422. c.Expires = time.Now().Add(f.DefaultTimeout)
  423. }
  424. conntrack.Unlock()
  425. if localCache != nil {
  426. localCache[fp] = struct{}{}
  427. }
  428. return true
  429. }
  430. func (f *Firewall) addConn(packet []byte, fp FirewallPacket, incoming bool) {
  431. var timeout time.Duration
  432. c := &conn{}
  433. switch fp.Protocol {
  434. case fwProtoTCP:
  435. timeout = f.TCPTimeout
  436. if !incoming {
  437. setTCPRTTTracking(c, packet)
  438. }
  439. case fwProtoUDP:
  440. timeout = f.UDPTimeout
  441. default:
  442. timeout = f.DefaultTimeout
  443. }
  444. conntrack := f.Conntrack
  445. conntrack.Lock()
  446. if _, ok := conntrack.Conns[fp]; !ok {
  447. conntrack.TimerWheel.Add(fp, timeout)
  448. }
  449. // Record which rulesVersion allowed this connection, so we can retest after
  450. // firewall reload
  451. c.incoming = incoming
  452. c.rulesVersion = f.rulesVersion
  453. c.Expires = time.Now().Add(timeout)
  454. conntrack.Conns[fp] = c
  455. conntrack.Unlock()
  456. }
  457. // Evict checks if a conntrack entry has expired, if so it is removed, if not it is re-added to the wheel
  458. // Caller must own the connMutex lock!
  459. func (f *Firewall) evict(p FirewallPacket) {
  460. //TODO: report a stat if the tcp rtt tracking was never resolved?
  461. // Are we still tracking this conn?
  462. conntrack := f.Conntrack
  463. t, ok := conntrack.Conns[p]
  464. if !ok {
  465. return
  466. }
  467. newT := t.Expires.Sub(time.Now())
  468. // Timeout is in the future, re-add the timer
  469. if newT > 0 {
  470. conntrack.TimerWheel.Add(p, newT)
  471. return
  472. }
  473. // This conn is done
  474. delete(conntrack.Conns, p)
  475. }
  476. func (ft *FirewallTable) match(p FirewallPacket, incoming bool, c *cert.NebulaCertificate, caPool *cert.NebulaCAPool) bool {
  477. if ft.AnyProto.match(p, incoming, c, caPool) {
  478. return true
  479. }
  480. switch p.Protocol {
  481. case fwProtoTCP:
  482. if ft.TCP.match(p, incoming, c, caPool) {
  483. return true
  484. }
  485. case fwProtoUDP:
  486. if ft.UDP.match(p, incoming, c, caPool) {
  487. return true
  488. }
  489. case fwProtoICMP:
  490. if ft.ICMP.match(p, incoming, c, caPool) {
  491. return true
  492. }
  493. }
  494. return false
  495. }
  496. func (fp firewallPort) addRule(startPort int32, endPort int32, groups []string, host string, ip *net.IPNet, caName string, caSha string) error {
  497. if startPort > endPort {
  498. return fmt.Errorf("start port was lower than end port")
  499. }
  500. for i := startPort; i <= endPort; i++ {
  501. if _, ok := fp[i]; !ok {
  502. fp[i] = &FirewallCA{
  503. CANames: make(map[string]*FirewallRule),
  504. CAShas: make(map[string]*FirewallRule),
  505. }
  506. }
  507. if err := fp[i].addRule(groups, host, ip, caName, caSha); err != nil {
  508. return err
  509. }
  510. }
  511. return nil
  512. }
  513. func (fp firewallPort) match(p FirewallPacket, incoming bool, c *cert.NebulaCertificate, caPool *cert.NebulaCAPool) bool {
  514. // We don't have any allowed ports, bail
  515. if fp == nil {
  516. return false
  517. }
  518. var port int32
  519. if p.Fragment {
  520. port = fwPortFragment
  521. } else if incoming {
  522. port = int32(p.LocalPort)
  523. } else {
  524. port = int32(p.RemotePort)
  525. }
  526. if fp[port].match(p, c, caPool) {
  527. return true
  528. }
  529. return fp[fwPortAny].match(p, c, caPool)
  530. }
  531. func (fc *FirewallCA) addRule(groups []string, host string, ip *net.IPNet, caName, caSha string) error {
  532. fr := func() *FirewallRule {
  533. return &FirewallRule{
  534. Hosts: make(map[string]struct{}),
  535. Groups: make([][]string, 0),
  536. CIDR: NewCIDRTree(),
  537. }
  538. }
  539. if caSha == "" && caName == "" {
  540. if fc.Any == nil {
  541. fc.Any = fr()
  542. }
  543. return fc.Any.addRule(groups, host, ip)
  544. }
  545. if caSha != "" {
  546. if _, ok := fc.CAShas[caSha]; !ok {
  547. fc.CAShas[caSha] = fr()
  548. }
  549. err := fc.CAShas[caSha].addRule(groups, host, ip)
  550. if err != nil {
  551. return err
  552. }
  553. }
  554. if caName != "" {
  555. if _, ok := fc.CANames[caName]; !ok {
  556. fc.CANames[caName] = fr()
  557. }
  558. err := fc.CANames[caName].addRule(groups, host, ip)
  559. if err != nil {
  560. return err
  561. }
  562. }
  563. return nil
  564. }
  565. func (fc *FirewallCA) match(p FirewallPacket, c *cert.NebulaCertificate, caPool *cert.NebulaCAPool) bool {
  566. if fc == nil {
  567. return false
  568. }
  569. if fc.Any.match(p, c) {
  570. return true
  571. }
  572. if t, ok := fc.CAShas[c.Details.Issuer]; ok {
  573. if t.match(p, c) {
  574. return true
  575. }
  576. }
  577. s, err := caPool.GetCAForCert(c)
  578. if err != nil {
  579. return false
  580. }
  581. return fc.CANames[s.Details.Name].match(p, c)
  582. }
  583. func (fr *FirewallRule) addRule(groups []string, host string, ip *net.IPNet) error {
  584. if fr.Any {
  585. return nil
  586. }
  587. if fr.isAny(groups, host, ip) {
  588. fr.Any = true
  589. // If it's any we need to wipe out any pre-existing rules to save on memory
  590. fr.Groups = make([][]string, 0)
  591. fr.Hosts = make(map[string]struct{})
  592. fr.CIDR = NewCIDRTree()
  593. } else {
  594. if len(groups) > 0 {
  595. fr.Groups = append(fr.Groups, groups)
  596. }
  597. if host != "" {
  598. fr.Hosts[host] = struct{}{}
  599. }
  600. if ip != nil {
  601. fr.CIDR.AddCIDR(ip, struct{}{})
  602. }
  603. }
  604. return nil
  605. }
  606. func (fr *FirewallRule) isAny(groups []string, host string, ip *net.IPNet) bool {
  607. if len(groups) == 0 && host == "" && ip == nil {
  608. return true
  609. }
  610. for _, group := range groups {
  611. if group == "any" {
  612. return true
  613. }
  614. }
  615. if host == "any" {
  616. return true
  617. }
  618. if ip != nil && ip.Contains(net.IPv4(0, 0, 0, 0)) {
  619. return true
  620. }
  621. return false
  622. }
  623. func (fr *FirewallRule) match(p FirewallPacket, c *cert.NebulaCertificate) bool {
  624. if fr == nil {
  625. return false
  626. }
  627. // Shortcut path for if groups, hosts, or cidr contained an `any`
  628. if fr.Any {
  629. return true
  630. }
  631. // Need any of group, host, or cidr to match
  632. for _, sg := range fr.Groups {
  633. found := false
  634. for _, g := range sg {
  635. if _, ok := c.Details.InvertedGroups[g]; !ok {
  636. found = false
  637. break
  638. }
  639. found = true
  640. }
  641. if found {
  642. return true
  643. }
  644. }
  645. if fr.Hosts != nil {
  646. if _, ok := fr.Hosts[c.Details.Name]; ok {
  647. return true
  648. }
  649. }
  650. if fr.CIDR != nil && fr.CIDR.Contains(p.RemoteIP) != nil {
  651. return true
  652. }
  653. // No host, group, or cidr matched, bye bye
  654. return false
  655. }
  656. type rule struct {
  657. Port string
  658. Code string
  659. Proto string
  660. Host string
  661. Group string
  662. Groups []string
  663. Cidr string
  664. CAName string
  665. CASha string
  666. }
  667. func convertRule(p interface{}, table string, i int) (rule, error) {
  668. r := rule{}
  669. m, ok := p.(map[interface{}]interface{})
  670. if !ok {
  671. return r, errors.New("could not parse rule")
  672. }
  673. toString := func(k string, m map[interface{}]interface{}) string {
  674. v, ok := m[k]
  675. if !ok {
  676. return ""
  677. }
  678. return fmt.Sprintf("%v", v)
  679. }
  680. r.Port = toString("port", m)
  681. r.Code = toString("code", m)
  682. r.Proto = toString("proto", m)
  683. r.Host = toString("host", m)
  684. r.Cidr = toString("cidr", m)
  685. r.CAName = toString("ca_name", m)
  686. r.CASha = toString("ca_sha", m)
  687. // Make sure group isn't an array
  688. if v, ok := m["group"].([]interface{}); ok {
  689. if len(v) > 1 {
  690. return r, errors.New("group should contain a single value, an array with more than one entry was provided")
  691. }
  692. l.Warnf("%s rule #%v; group was an array with a single value, converting to simple value", table, i)
  693. m["group"] = v[0]
  694. }
  695. r.Group = toString("group", m)
  696. if rg, ok := m["groups"]; ok {
  697. switch reflect.TypeOf(rg).Kind() {
  698. case reflect.Slice:
  699. v := reflect.ValueOf(rg)
  700. r.Groups = make([]string, v.Len())
  701. for i := 0; i < v.Len(); i++ {
  702. r.Groups[i] = v.Index(i).Interface().(string)
  703. }
  704. case reflect.String:
  705. r.Groups = []string{rg.(string)}
  706. default:
  707. r.Groups = []string{fmt.Sprintf("%v", rg)}
  708. }
  709. }
  710. return r, nil
  711. }
  712. func parsePort(s string) (startPort, endPort int32, err error) {
  713. if s == "any" {
  714. startPort = fwPortAny
  715. endPort = fwPortAny
  716. } else if s == "fragment" {
  717. startPort = fwPortFragment
  718. endPort = fwPortFragment
  719. } else if strings.Contains(s, `-`) {
  720. sPorts := strings.SplitN(s, `-`, 2)
  721. sPorts[0] = strings.Trim(sPorts[0], " ")
  722. sPorts[1] = strings.Trim(sPorts[1], " ")
  723. if len(sPorts) != 2 || sPorts[0] == "" || sPorts[1] == "" {
  724. return 0, 0, fmt.Errorf("appears to be a range but could not be parsed; `%s`", s)
  725. }
  726. rStartPort, err := strconv.Atoi(sPorts[0])
  727. if err != nil {
  728. return 0, 0, fmt.Errorf("beginning range was not a number; `%s`", sPorts[0])
  729. }
  730. rEndPort, err := strconv.Atoi(sPorts[1])
  731. if err != nil {
  732. return 0, 0, fmt.Errorf("ending range was not a number; `%s`", sPorts[1])
  733. }
  734. startPort = int32(rStartPort)
  735. endPort = int32(rEndPort)
  736. if startPort == fwPortAny {
  737. endPort = fwPortAny
  738. }
  739. } else {
  740. rPort, err := strconv.Atoi(s)
  741. if err != nil {
  742. return 0, 0, fmt.Errorf("was not a number; `%s`", s)
  743. }
  744. startPort = int32(rPort)
  745. endPort = startPort
  746. }
  747. return
  748. }
  749. //TODO: write tests for these
  750. func setTCPRTTTracking(c *conn, p []byte) {
  751. if c.Seq != 0 {
  752. return
  753. }
  754. ihl := int(p[0]&0x0f) << 2
  755. // Don't track FIN packets
  756. if p[ihl+13]&tcpFIN != 0 {
  757. return
  758. }
  759. c.Seq = binary.BigEndian.Uint32(p[ihl+4 : ihl+8])
  760. c.Sent = time.Now()
  761. }
  762. func (f *Firewall) checkTCPRTT(c *conn, p []byte) bool {
  763. if c.Seq == 0 {
  764. return false
  765. }
  766. ihl := int(p[0]&0x0f) << 2
  767. if p[ihl+13]&tcpACK == 0 {
  768. return false
  769. }
  770. // Deal with wrap around, signed int cuts the ack window in half
  771. // 0 is a bad ack, no data acknowledged
  772. // positive number is a bad ack, ack is over half the window away
  773. if int32(c.Seq-binary.BigEndian.Uint32(p[ihl+8:ihl+12])) >= 0 {
  774. return false
  775. }
  776. f.metricTCPRTT.Update(time.Since(c.Sent).Nanoseconds())
  777. c.Seq = 0
  778. return true
  779. }
  780. // ConntrackCache is used as a local routine cache to know if a given flow
  781. // has been seen in the conntrack table.
  782. type ConntrackCache map[FirewallPacket]struct{}
  783. type ConntrackCacheTicker struct {
  784. cacheV uint64
  785. cacheTick uint64
  786. cache ConntrackCache
  787. }
  788. func NewConntrackCacheTicker(d time.Duration) *ConntrackCacheTicker {
  789. if d == 0 {
  790. return nil
  791. }
  792. c := &ConntrackCacheTicker{
  793. cache: ConntrackCache{},
  794. }
  795. go c.tick(d)
  796. return c
  797. }
  798. func (c *ConntrackCacheTicker) tick(d time.Duration) {
  799. for {
  800. time.Sleep(d)
  801. atomic.AddUint64(&c.cacheTick, 1)
  802. }
  803. }
  804. // Get checks if the cache ticker has moved to the next version before returning
  805. // the map. If it has moved, we reset the map.
  806. func (c *ConntrackCacheTicker) Get() ConntrackCache {
  807. if c == nil {
  808. return nil
  809. }
  810. if tick := atomic.LoadUint64(&c.cacheTick); tick != c.cacheV {
  811. c.cacheV = tick
  812. if ll := len(c.cache); ll > 0 {
  813. if l.GetLevel() == logrus.DebugLevel {
  814. l.WithField("len", ll).Debug("resetting conntrack cache")
  815. }
  816. c.cache = make(ConntrackCache, ll)
  817. }
  818. }
  819. return c.cache
  820. }