firewall.go 25 KB

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