firewall.go 22 KB

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