firewall.go 24 KB

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