firewall.go 25 KB

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