firewall.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. package nebula
  2. import (
  3. "context"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "hash/fnv"
  9. "log/slog"
  10. "net/netip"
  11. "reflect"
  12. "slices"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. "github.com/gaissmai/bart"
  18. "github.com/rcrowley/go-metrics"
  19. "github.com/slackhq/nebula/cert"
  20. "github.com/slackhq/nebula/config"
  21. "github.com/slackhq/nebula/firewall"
  22. "github.com/slackhq/nebula/iputil"
  23. )
  24. type FirewallInterface interface {
  25. AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, cidr, localCidr string, caName string, caSha string) error
  26. }
  27. type conn struct {
  28. Expires time.Time // Time when this conntrack entry will expire
  29. // record why the original connection passed the firewall, so we can re-validate
  30. // after ruleset changes. Note, rulesVersion is a uint16 so that these two
  31. // fields pack for free after the uint32 above
  32. incoming bool
  33. rulesVersion uint16
  34. }
  35. // TODO: need conntrack max tracked connections handling
  36. type Firewall struct {
  37. Conntrack *FirewallConntrack
  38. InRules *FirewallTable
  39. OutRules *FirewallTable
  40. InboundSendReject bool
  41. OutboundSendReject bool
  42. //TODO: we should have many more options for TCP, an option for ICMP, and mimic the kernel a bit better
  43. // https://www.kernel.org/doc/Documentation/networking/nf_conntrack-sysctl.txt
  44. TCPTimeout time.Duration //linux: 5 days max
  45. UDPTimeout time.Duration //linux: 180s max
  46. DefaultTimeout time.Duration //linux: 600s
  47. // routableNetworks describes the vpn addresses as well as any unsafe networks issued to us in the certificate.
  48. // The vpn addresses are a full bit match while the unsafe networks only match the prefix
  49. routableNetworks *bart.Lite
  50. // assignedNetworks is a list of vpn networks assigned to us in the certificate.
  51. assignedNetworks []netip.Prefix
  52. // unsafeNetworks is the list of unsafe networks issued to us in the certificate
  53. unsafeNetworks []netip.Prefix
  54. rules string
  55. rulesVersion uint16
  56. defaultLocalCIDRAny bool
  57. incomingMetrics firewallMetrics
  58. outgoingMetrics firewallMetrics
  59. l *slog.Logger
  60. }
  61. type firewallMetrics struct {
  62. droppedLocalAddr metrics.Counter
  63. droppedRemoteAddr metrics.Counter
  64. droppedNoRule metrics.Counter
  65. }
  66. type FirewallConntrack struct {
  67. sync.Mutex
  68. Conns map[firewall.Packet]*conn
  69. TimerWheel *TimerWheel[firewall.Packet]
  70. }
  71. // FirewallTable is the entry point for a rule, the evaluation order is:
  72. // Proto AND port AND (CA SHA or CA name) AND local CIDR AND (group OR groups OR name OR remote CIDR)
  73. type FirewallTable struct {
  74. TCP firewallPort
  75. UDP firewallPort
  76. ICMP firewallPort
  77. AnyProto firewallPort
  78. }
  79. func newFirewallTable() *FirewallTable {
  80. return &FirewallTable{
  81. TCP: firewallPort{},
  82. UDP: firewallPort{},
  83. ICMP: firewallPort{},
  84. AnyProto: firewallPort{},
  85. }
  86. }
  87. type FirewallCA struct {
  88. Any *FirewallRule
  89. CANames map[string]*FirewallRule
  90. CAShas map[string]*FirewallRule
  91. }
  92. type FirewallRule struct {
  93. // Any makes Hosts, Groups, and CIDR irrelevant
  94. Any *firewallLocalCIDR
  95. Hosts map[string]*firewallLocalCIDR
  96. Groups []*firewallGroups
  97. CIDR *bart.Table[*firewallLocalCIDR]
  98. }
  99. type firewallGroups struct {
  100. Groups []string
  101. LocalCIDR *firewallLocalCIDR
  102. }
  103. // Even though ports are uint16, int32 maps are faster for lookup
  104. // Plus we can use `-1` for fragment rules
  105. type firewallPort map[int32]*FirewallCA
  106. type firewallLocalCIDR struct {
  107. Any bool
  108. LocalCIDR *bart.Lite
  109. }
  110. // NewFirewall creates a new Firewall object. A TimerWheel is created for you from the provided timeouts.
  111. // The certificate provided should be the highest version loaded in memory.
  112. func NewFirewall(l *slog.Logger, tcpTimeout, UDPTimeout, defaultTimeout time.Duration, c cert.Certificate) *Firewall {
  113. //TODO: error on 0 duration
  114. var tmin, tmax time.Duration
  115. if tcpTimeout < UDPTimeout {
  116. tmin = tcpTimeout
  117. tmax = UDPTimeout
  118. } else {
  119. tmin = UDPTimeout
  120. tmax = tcpTimeout
  121. }
  122. if defaultTimeout < tmin {
  123. tmin = defaultTimeout
  124. } else if defaultTimeout > tmax {
  125. tmax = defaultTimeout
  126. }
  127. routableNetworks := new(bart.Lite)
  128. var assignedNetworks []netip.Prefix
  129. for _, network := range c.Networks() {
  130. nprefix := netip.PrefixFrom(network.Addr(), network.Addr().BitLen())
  131. routableNetworks.Insert(nprefix)
  132. assignedNetworks = append(assignedNetworks, network)
  133. }
  134. unsafeNetworks := c.UnsafeNetworks()
  135. for _, n := range unsafeNetworks {
  136. routableNetworks.Insert(n)
  137. }
  138. return &Firewall{
  139. Conntrack: &FirewallConntrack{
  140. Conns: make(map[firewall.Packet]*conn),
  141. TimerWheel: NewTimerWheel[firewall.Packet](tmin, tmax),
  142. },
  143. InRules: newFirewallTable(),
  144. OutRules: newFirewallTable(),
  145. TCPTimeout: tcpTimeout,
  146. UDPTimeout: UDPTimeout,
  147. DefaultTimeout: defaultTimeout,
  148. routableNetworks: routableNetworks,
  149. assignedNetworks: assignedNetworks,
  150. unsafeNetworks: unsafeNetworks,
  151. l: l,
  152. incomingMetrics: firewallMetrics{
  153. droppedLocalAddr: metrics.GetOrRegisterCounter("firewall.incoming.dropped.local_addr", nil),
  154. droppedRemoteAddr: metrics.GetOrRegisterCounter("firewall.incoming.dropped.remote_addr", nil),
  155. droppedNoRule: metrics.GetOrRegisterCounter("firewall.incoming.dropped.no_rule", nil),
  156. },
  157. outgoingMetrics: firewallMetrics{
  158. droppedLocalAddr: metrics.GetOrRegisterCounter("firewall.outgoing.dropped.local_addr", nil),
  159. droppedRemoteAddr: metrics.GetOrRegisterCounter("firewall.outgoing.dropped.remote_addr", nil),
  160. droppedNoRule: metrics.GetOrRegisterCounter("firewall.outgoing.dropped.no_rule", nil),
  161. },
  162. }
  163. }
  164. func NewFirewallFromConfig(l *slog.Logger, cs *CertState, c *config.C) (*Firewall, error) {
  165. certificate := cs.getCertificate(cert.Version2)
  166. if certificate == nil {
  167. certificate = cs.getCertificate(cert.Version1)
  168. }
  169. if certificate == nil {
  170. panic("No certificate available to reconfigure the firewall")
  171. }
  172. fw := NewFirewall(
  173. l,
  174. c.GetDuration("firewall.conntrack.tcp_timeout", time.Minute*12),
  175. c.GetDuration("firewall.conntrack.udp_timeout", time.Minute*3),
  176. c.GetDuration("firewall.conntrack.default_timeout", time.Minute*10),
  177. certificate,
  178. //TODO: max_connections
  179. )
  180. fw.defaultLocalCIDRAny = c.GetBool("firewall.default_local_cidr_any", false)
  181. inboundAction := c.GetString("firewall.inbound_action", "drop")
  182. switch inboundAction {
  183. case "reject":
  184. fw.InboundSendReject = true
  185. case "drop":
  186. fw.InboundSendReject = false
  187. default:
  188. l.Warn("invalid firewall.inbound_action, defaulting to `drop`", "action", inboundAction)
  189. fw.InboundSendReject = false
  190. }
  191. outboundAction := c.GetString("firewall.outbound_action", "drop")
  192. switch outboundAction {
  193. case "reject":
  194. fw.OutboundSendReject = true
  195. case "drop":
  196. fw.OutboundSendReject = false
  197. default:
  198. l.Warn("invalid firewall.outbound_action, defaulting to `drop`", "action", outboundAction)
  199. fw.OutboundSendReject = false
  200. }
  201. err := AddFirewallRulesFromConfig(l, false, c, fw)
  202. if err != nil {
  203. return nil, err
  204. }
  205. err = AddFirewallRulesFromConfig(l, true, c, fw)
  206. if err != nil {
  207. return nil, err
  208. }
  209. return fw, nil
  210. }
  211. // AddRule properly creates the in memory rule structure for a firewall table.
  212. func (f *Firewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, cidr, localCidr, caName string, caSha string) error {
  213. var (
  214. ft *FirewallTable
  215. fp firewallPort
  216. )
  217. if incoming {
  218. ft = f.InRules
  219. } else {
  220. ft = f.OutRules
  221. }
  222. switch proto {
  223. case iputil.IPProtocolTCP:
  224. fp = ft.TCP
  225. case iputil.IPProtocolUDP:
  226. fp = ft.UDP
  227. case iputil.IPProtocolICMP, iputil.IPProtocolICMPv6:
  228. //ICMP traffic doesn't have ports, so we always coerce to "any", even if a value is provided
  229. if startPort != firewall.PortAny {
  230. f.l.Warn("ignoring port specification for ICMP firewall rule", "startPort", startPort)
  231. }
  232. startPort = firewall.PortAny
  233. endPort = firewall.PortAny
  234. fp = ft.ICMP
  235. case firewall.ProtoAny:
  236. fp = ft.AnyProto
  237. default:
  238. return fmt.Errorf("unknown protocol %v", proto)
  239. }
  240. // We need this rule string because we generate a hash. Removing this will break firewall reload.
  241. ruleString := fmt.Sprintf(
  242. "incoming: %v, proto: %v, startPort: %v, endPort: %v, groups: %v, host: %v, ip: %v, localIp: %v, caName: %v, caSha: %s",
  243. incoming, proto, startPort, endPort, groups, host, cidr, localCidr, caName, caSha,
  244. )
  245. f.rules += ruleString + "\n"
  246. direction := "incoming"
  247. if !incoming {
  248. direction = "outgoing"
  249. }
  250. f.l.Info("Firewall rule added",
  251. "firewallRule", m{"direction": direction, "proto": proto, "startPort": startPort, "endPort": endPort, "groups": groups, "host": host, "cidr": cidr, "localCidr": localCidr, "caName": caName, "caSha": caSha},
  252. )
  253. return fp.addRule(f, startPort, endPort, groups, host, cidr, localCidr, 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 *slog.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.([]any)
  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. r, err := convertRule(l, t, table, i)
  287. if err != nil {
  288. return fmt.Errorf("%s rule #%v; %s", table, i, err)
  289. }
  290. if r.Code != "" && r.Port != "" {
  291. return fmt.Errorf("%s rule #%v; only one of port or code should be provided", table, i)
  292. }
  293. if r.Host == "" && len(r.Groups) == 0 && r.Cidr == "" && r.LocalCidr == "" && r.CAName == "" && r.CASha == "" {
  294. 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)
  295. }
  296. var sPort, errPort string
  297. if r.Code != "" {
  298. errPort = "code"
  299. sPort = r.Code
  300. } else {
  301. errPort = "port"
  302. sPort = r.Port
  303. }
  304. var proto uint8
  305. var startPort, endPort int32
  306. switch r.Proto {
  307. case "any":
  308. proto = firewall.ProtoAny
  309. startPort, endPort, err = parsePort(sPort)
  310. case "tcp":
  311. proto = iputil.IPProtocolTCP
  312. startPort, endPort, err = parsePort(sPort)
  313. case "udp":
  314. proto = iputil.IPProtocolUDP
  315. startPort, endPort, err = parsePort(sPort)
  316. case "icmp":
  317. proto = iputil.IPProtocolICMP
  318. startPort = firewall.PortAny
  319. endPort = firewall.PortAny
  320. if sPort != "" {
  321. l.Warn("ignoring port specification for ICMP firewall rule", "port", sPort)
  322. }
  323. default:
  324. return fmt.Errorf("%s rule #%v; proto was not understood; `%s`", table, i, r.Proto)
  325. }
  326. if err != nil {
  327. return fmt.Errorf("%s rule #%v; %s %s", table, i, errPort, err)
  328. }
  329. if r.Cidr != "" && r.Cidr != "any" {
  330. _, err = netip.ParsePrefix(r.Cidr)
  331. if err != nil {
  332. return fmt.Errorf("%s rule #%v; cidr did not parse; %s", table, i, err)
  333. }
  334. }
  335. if r.LocalCidr != "" && r.LocalCidr != "any" {
  336. _, err = netip.ParsePrefix(r.LocalCidr)
  337. if err != nil {
  338. return fmt.Errorf("%s rule #%v; local_cidr did not parse; %s", table, i, err)
  339. }
  340. }
  341. if warning := r.sanity(); warning != nil {
  342. l.Warn("firewall rule sanity check",
  343. "table", table,
  344. "rule", i,
  345. "warning", warning,
  346. )
  347. }
  348. err = fw.AddRule(inbound, proto, startPort, endPort, r.Groups, r.Host, r.Cidr, r.LocalCidr, r.CAName, r.CASha)
  349. if err != nil {
  350. return fmt.Errorf("%s rule #%v; `%s`", table, i, err)
  351. }
  352. }
  353. return nil
  354. }
  355. var ErrUnknownNetworkType = errors.New("unknown network type")
  356. var ErrPeerRejected = errors.New("remote address is not within a network that we handle")
  357. var ErrInvalidRemoteIP = errors.New("remote address is not in remote certificate networks")
  358. var ErrInvalidLocalIP = errors.New("local address is not in list of handled local addresses")
  359. var ErrNoMatchingRule = errors.New("no matching rule in firewall table")
  360. // Drop returns an error if the packet should be dropped, explaining why. It
  361. // returns nil if the packet should not be dropped.
  362. func (f *Firewall) Drop(fp firewall.Packet, incoming bool, h *HostInfo, caPool *cert.CAPool, localCache firewall.ConntrackCache) error {
  363. // Make sure remote address matches nebula certificate, and determine how to treat it
  364. if h.networks == nil {
  365. // Simple case: Certificate has one address and no unsafe networks
  366. if h.vpnAddrs[0] != fp.RemoteAddr {
  367. f.metrics(incoming).droppedRemoteAddr.Inc(1)
  368. return ErrInvalidRemoteIP
  369. }
  370. } else {
  371. nwType, ok := h.networks.Lookup(fp.RemoteAddr)
  372. if !ok {
  373. f.metrics(incoming).droppedRemoteAddr.Inc(1)
  374. return ErrInvalidRemoteIP
  375. }
  376. switch nwType {
  377. case NetworkTypeVPN:
  378. break // nothing special
  379. case NetworkTypeVPNPeer:
  380. f.metrics(incoming).droppedRemoteAddr.Inc(1)
  381. return ErrPeerRejected // reject for now, one day this may have different FW rules
  382. case NetworkTypeUnsafe:
  383. break // nothing special, one day this may have different FW rules
  384. default:
  385. f.metrics(incoming).droppedRemoteAddr.Inc(1)
  386. return ErrUnknownNetworkType //should never happen
  387. }
  388. }
  389. // Make sure we are supposed to be handling this local ip address
  390. if !f.routableNetworks.Contains(fp.LocalAddr) {
  391. f.metrics(incoming).droppedLocalAddr.Inc(1)
  392. return ErrInvalidLocalIP
  393. }
  394. // Check if we spoke to this tuple, if we did then allow this packet
  395. if f.inConns(fp, h, caPool, localCache) {
  396. return nil
  397. }
  398. table := f.OutRules
  399. if incoming {
  400. table = f.InRules
  401. }
  402. // We now know which firewall table to check against
  403. if !table.match(fp, incoming, h.ConnectionState.peerCert, caPool) {
  404. f.metrics(incoming).droppedNoRule.Inc(1)
  405. return ErrNoMatchingRule
  406. }
  407. // We always want to conntrack since it is a faster operation
  408. f.addConn(fp, incoming)
  409. return nil
  410. }
  411. func (f *Firewall) metrics(incoming bool) firewallMetrics {
  412. if incoming {
  413. return f.incomingMetrics
  414. } else {
  415. return f.outgoingMetrics
  416. }
  417. }
  418. // Destroy cleans up any known cyclical references so the object can be freed by GC. This should be called if a new
  419. // firewall object is created
  420. func (f *Firewall) Destroy() {
  421. //TODO: clean references if/when needed
  422. }
  423. func (f *Firewall) EmitStats() {
  424. conntrack := f.Conntrack
  425. conntrack.Lock()
  426. conntrackCount := len(conntrack.Conns)
  427. conntrack.Unlock()
  428. metrics.GetOrRegisterGauge("firewall.conntrack.count", nil).Update(int64(conntrackCount))
  429. metrics.GetOrRegisterGauge("firewall.rules.version", nil).Update(int64(f.rulesVersion))
  430. metrics.GetOrRegisterGauge("firewall.rules.hash", nil).Update(int64(f.GetRuleHashFNV()))
  431. }
  432. func (f *Firewall) inConns(fp firewall.Packet, h *HostInfo, caPool *cert.CAPool, localCache firewall.ConntrackCache) bool {
  433. if localCache != nil {
  434. if _, ok := localCache[fp]; ok {
  435. return true
  436. }
  437. }
  438. conntrack := f.Conntrack
  439. conntrack.Lock()
  440. // Purge every time we test
  441. ep, has := conntrack.TimerWheel.Purge()
  442. if has {
  443. f.evict(ep)
  444. }
  445. c, ok := conntrack.Conns[fp]
  446. if !ok {
  447. conntrack.Unlock()
  448. return false
  449. }
  450. if c.rulesVersion != f.rulesVersion {
  451. // This conntrack entry was for an older rule set, validate
  452. // it still passes with the current rule set
  453. table := f.OutRules
  454. if c.incoming {
  455. table = f.InRules
  456. }
  457. // We now know which firewall table to check against
  458. if !table.match(fp, c.incoming, h.ConnectionState.peerCert, caPool) {
  459. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  460. h.logger(f.l).Debug("dropping old conntrack entry, does not match new ruleset",
  461. "fwPacket", fp,
  462. "incoming", c.incoming,
  463. "rulesVersion", f.rulesVersion,
  464. "oldRulesVersion", c.rulesVersion,
  465. )
  466. }
  467. delete(conntrack.Conns, fp)
  468. conntrack.Unlock()
  469. return false
  470. }
  471. if f.l.Enabled(context.Background(), slog.LevelDebug) {
  472. h.logger(f.l).Debug("keeping old conntrack entry, does match new ruleset",
  473. "fwPacket", fp,
  474. "incoming", c.incoming,
  475. "rulesVersion", f.rulesVersion,
  476. "oldRulesVersion", c.rulesVersion,
  477. )
  478. }
  479. c.rulesVersion = f.rulesVersion
  480. }
  481. switch fp.Protocol {
  482. case iputil.IPProtocolTCP:
  483. c.Expires = time.Now().Add(f.TCPTimeout)
  484. case iputil.IPProtocolUDP:
  485. c.Expires = time.Now().Add(f.UDPTimeout)
  486. default:
  487. c.Expires = time.Now().Add(f.DefaultTimeout)
  488. }
  489. conntrack.Unlock()
  490. if localCache != nil {
  491. localCache[fp] = struct{}{}
  492. }
  493. return true
  494. }
  495. func (f *Firewall) addConn(fp firewall.Packet, incoming bool) {
  496. var timeout time.Duration
  497. c := &conn{}
  498. switch fp.Protocol {
  499. case iputil.IPProtocolTCP:
  500. timeout = f.TCPTimeout
  501. case iputil.IPProtocolUDP:
  502. timeout = f.UDPTimeout
  503. default:
  504. timeout = f.DefaultTimeout
  505. }
  506. conntrack := f.Conntrack
  507. conntrack.Lock()
  508. if _, ok := conntrack.Conns[fp]; !ok {
  509. conntrack.TimerWheel.Advance(time.Now())
  510. conntrack.TimerWheel.Add(fp, timeout)
  511. }
  512. // Record which rulesVersion allowed this connection, so we can retest after
  513. // firewall reload
  514. c.incoming = incoming
  515. c.rulesVersion = f.rulesVersion
  516. c.Expires = time.Now().Add(timeout)
  517. conntrack.Conns[fp] = c
  518. conntrack.Unlock()
  519. }
  520. // Evict checks if a conntrack entry has expired, if so it is removed, if not it is re-added to the wheel
  521. // Caller must own the connMutex lock!
  522. func (f *Firewall) evict(p firewall.Packet) {
  523. // Are we still tracking this conn?
  524. conntrack := f.Conntrack
  525. t, ok := conntrack.Conns[p]
  526. if !ok {
  527. return
  528. }
  529. newT := t.Expires.Sub(time.Now())
  530. // Timeout is in the future, re-add the timer
  531. if newT > 0 {
  532. conntrack.TimerWheel.Advance(time.Now())
  533. conntrack.TimerWheel.Add(p, newT)
  534. return
  535. }
  536. // This conn is done
  537. delete(conntrack.Conns, p)
  538. }
  539. func (ft *FirewallTable) match(p firewall.Packet, incoming bool, c *cert.CachedCertificate, caPool *cert.CAPool) bool {
  540. if ft.AnyProto.match(p, incoming, c, caPool) {
  541. return true
  542. }
  543. switch p.Protocol {
  544. case iputil.IPProtocolTCP:
  545. if ft.TCP.match(p, incoming, c, caPool) {
  546. return true
  547. }
  548. case iputil.IPProtocolUDP:
  549. if ft.UDP.match(p, incoming, c, caPool) {
  550. return true
  551. }
  552. case iputil.IPProtocolICMP, iputil.IPProtocolICMPv6:
  553. if ft.ICMP.match(p, incoming, c, caPool) {
  554. return true
  555. }
  556. }
  557. return false
  558. }
  559. func (fp firewallPort) addRule(f *Firewall, startPort int32, endPort int32, groups []string, host string, cidr, localCidr, caName string, caSha string) error {
  560. if startPort > endPort {
  561. return fmt.Errorf("start port was lower than end port")
  562. }
  563. for i := startPort; i <= endPort; i++ {
  564. if _, ok := fp[i]; !ok {
  565. fp[i] = &FirewallCA{
  566. CANames: make(map[string]*FirewallRule),
  567. CAShas: make(map[string]*FirewallRule),
  568. }
  569. }
  570. if err := fp[i].addRule(f, groups, host, cidr, localCidr, caName, caSha); err != nil {
  571. return err
  572. }
  573. }
  574. return nil
  575. }
  576. func (fp firewallPort) match(p firewall.Packet, incoming bool, c *cert.CachedCertificate, caPool *cert.CAPool) bool {
  577. // We don't have any allowed ports, bail
  578. if fp == nil {
  579. return false
  580. }
  581. // this branch is here to catch traffic from FirewallTable.Any.match and FirewallTable.ICMP.match
  582. if p.Protocol == iputil.IPProtocolICMP || p.Protocol == iputil.IPProtocolICMPv6 {
  583. // port numbers are re-used for connection tracking of ICMP,
  584. // but we don't want to actually filter on them.
  585. return fp[firewall.PortAny].match(p, c, caPool)
  586. }
  587. var port int32
  588. if p.Fragment {
  589. port = firewall.PortFragment
  590. } else if incoming {
  591. port = int32(p.LocalPort)
  592. } else {
  593. port = int32(p.RemotePort)
  594. }
  595. if fp[port].match(p, c, caPool) {
  596. return true
  597. }
  598. return fp[firewall.PortAny].match(p, c, caPool)
  599. }
  600. func (fc *FirewallCA) addRule(f *Firewall, groups []string, host string, cidr, localCidr, caName, caSha string) error {
  601. fr := func() *FirewallRule {
  602. return &FirewallRule{
  603. Hosts: make(map[string]*firewallLocalCIDR),
  604. Groups: make([]*firewallGroups, 0),
  605. CIDR: new(bart.Table[*firewallLocalCIDR]),
  606. }
  607. }
  608. if caSha == "" && caName == "" {
  609. if fc.Any == nil {
  610. fc.Any = fr()
  611. }
  612. return fc.Any.addRule(f, groups, host, cidr, localCidr)
  613. }
  614. if caSha != "" {
  615. if _, ok := fc.CAShas[caSha]; !ok {
  616. fc.CAShas[caSha] = fr()
  617. }
  618. err := fc.CAShas[caSha].addRule(f, groups, host, cidr, localCidr)
  619. if err != nil {
  620. return err
  621. }
  622. }
  623. if caName != "" {
  624. if _, ok := fc.CANames[caName]; !ok {
  625. fc.CANames[caName] = fr()
  626. }
  627. err := fc.CANames[caName].addRule(f, groups, host, cidr, localCidr)
  628. if err != nil {
  629. return err
  630. }
  631. }
  632. return nil
  633. }
  634. func (fc *FirewallCA) match(p firewall.Packet, c *cert.CachedCertificate, caPool *cert.CAPool) bool {
  635. if fc == nil {
  636. return false
  637. }
  638. if fc.Any.match(p, c) {
  639. return true
  640. }
  641. if t, ok := fc.CAShas[c.Certificate.Issuer()]; ok {
  642. if t.match(p, c) {
  643. return true
  644. }
  645. }
  646. s, err := caPool.GetCAForCert(c.Certificate)
  647. if err != nil {
  648. return false
  649. }
  650. // A version 3 certificate names its CA by the fingerprint of the CA's v2 form,
  651. // which is matched above. A rule may also use the v3 CA's own fingerprint.
  652. if c.Certificate.Version() == cert.Version3 {
  653. if t, ok := fc.CAShas[s.Fingerprint]; ok && t.match(p, c) {
  654. return true
  655. }
  656. }
  657. return fc.CANames[s.Certificate.Name()].match(p, c)
  658. }
  659. func (fr *FirewallRule) addRule(f *Firewall, groups []string, host, cidr, localCidr string) error {
  660. flc := func() *firewallLocalCIDR {
  661. return &firewallLocalCIDR{
  662. LocalCIDR: new(bart.Lite),
  663. }
  664. }
  665. if fr.isAny(groups, host, cidr) {
  666. if fr.Any == nil {
  667. fr.Any = flc()
  668. }
  669. return fr.Any.addRule(f, localCidr)
  670. }
  671. if len(groups) > 0 {
  672. nlc := flc()
  673. err := nlc.addRule(f, localCidr)
  674. if err != nil {
  675. return err
  676. }
  677. fr.Groups = append(fr.Groups, &firewallGroups{
  678. Groups: groups,
  679. LocalCIDR: nlc,
  680. })
  681. }
  682. if host != "" {
  683. nlc := fr.Hosts[host]
  684. if nlc == nil {
  685. nlc = flc()
  686. }
  687. err := nlc.addRule(f, localCidr)
  688. if err != nil {
  689. return err
  690. }
  691. fr.Hosts[host] = nlc
  692. }
  693. if cidr != "" {
  694. c, err := netip.ParsePrefix(cidr)
  695. if err != nil {
  696. return err
  697. }
  698. nlc, _ := fr.CIDR.Get(c)
  699. if nlc == nil {
  700. nlc = flc()
  701. }
  702. err = nlc.addRule(f, localCidr)
  703. if err != nil {
  704. return err
  705. }
  706. fr.CIDR.Insert(c, nlc)
  707. }
  708. return nil
  709. }
  710. func (fr *FirewallRule) isAny(groups []string, host string, cidr string) bool {
  711. if len(groups) == 0 && host == "" && cidr == "" {
  712. return true
  713. }
  714. if slices.Contains(groups, "any") {
  715. return true
  716. }
  717. if host == "any" {
  718. return true
  719. }
  720. if cidr == "any" {
  721. return true
  722. }
  723. return false
  724. }
  725. func (fr *FirewallRule) match(p firewall.Packet, c *cert.CachedCertificate) bool {
  726. if fr == nil {
  727. return false
  728. }
  729. // Shortcut path for if groups, hosts, or cidr contained an `any`
  730. if fr.Any.match(p, c) {
  731. return true
  732. }
  733. // Need any of group, host, or cidr to match
  734. for _, sg := range fr.Groups {
  735. found := false
  736. for _, g := range sg.Groups {
  737. if _, ok := c.InvertedGroups[g]; !ok {
  738. found = false
  739. break
  740. }
  741. found = true
  742. }
  743. if found && sg.LocalCIDR.match(p, c) {
  744. return true
  745. }
  746. }
  747. if fr.Hosts != nil {
  748. if flc, ok := fr.Hosts[c.Certificate.Name()]; ok {
  749. if flc.match(p, c) {
  750. return true
  751. }
  752. }
  753. }
  754. for _, v := range fr.CIDR.Supernets(netip.PrefixFrom(p.RemoteAddr, p.RemoteAddr.BitLen())) {
  755. if v.match(p, c) {
  756. return true
  757. }
  758. }
  759. return false
  760. }
  761. func (flc *firewallLocalCIDR) addRule(f *Firewall, localCidr string) error {
  762. if localCidr == "any" {
  763. flc.Any = true
  764. return nil
  765. }
  766. if localCidr == "" {
  767. if len(f.unsafeNetworks) == 0 || f.defaultLocalCIDRAny {
  768. flc.Any = true
  769. return nil
  770. }
  771. for _, network := range f.assignedNetworks {
  772. flc.LocalCIDR.Insert(network)
  773. }
  774. return nil
  775. }
  776. c, err := netip.ParsePrefix(localCidr)
  777. if err != nil {
  778. return err
  779. }
  780. flc.LocalCIDR.Insert(c)
  781. return nil
  782. }
  783. func (flc *firewallLocalCIDR) match(p firewall.Packet, c *cert.CachedCertificate) bool {
  784. if flc == nil {
  785. return false
  786. }
  787. if flc.Any {
  788. return true
  789. }
  790. return flc.LocalCIDR.Contains(p.LocalAddr)
  791. }
  792. type rule struct {
  793. Port string
  794. Code string
  795. Proto string
  796. Host string
  797. Groups []string
  798. Cidr string
  799. LocalCidr string
  800. CAName string
  801. CASha string
  802. }
  803. func convertRule(l *slog.Logger, p any, table string, i int) (rule, error) {
  804. r := rule{}
  805. m, ok := p.(map[string]any)
  806. if !ok {
  807. return r, errors.New("could not parse rule")
  808. }
  809. toString := func(k string, m map[string]any) string {
  810. v, ok := m[k]
  811. if !ok {
  812. return ""
  813. }
  814. return fmt.Sprintf("%v", v)
  815. }
  816. r.Port = toString("port", m)
  817. r.Code = toString("code", m)
  818. r.Proto = toString("proto", m)
  819. r.Host = toString("host", m)
  820. r.Cidr = toString("cidr", m)
  821. r.LocalCidr = toString("local_cidr", m)
  822. r.CAName = toString("ca_name", m)
  823. r.CASha = toString("ca_sha", m)
  824. // Make sure group isn't an array
  825. if v, ok := m["group"].([]any); ok {
  826. if len(v) > 1 {
  827. return r, errors.New("group should contain a single value, an array with more than one entry was provided")
  828. }
  829. l.Warn("group was an array with a single value, converting to simple value",
  830. "table", table,
  831. "rule", i,
  832. )
  833. m["group"] = v[0]
  834. }
  835. singleGroup := toString("group", m)
  836. if rg, ok := m["groups"]; ok {
  837. switch reflect.TypeOf(rg).Kind() {
  838. case reflect.Slice:
  839. v := reflect.ValueOf(rg)
  840. r.Groups = make([]string, v.Len())
  841. for i := 0; i < v.Len(); i++ {
  842. r.Groups[i] = v.Index(i).Interface().(string)
  843. }
  844. case reflect.String:
  845. r.Groups = []string{rg.(string)}
  846. default:
  847. r.Groups = []string{fmt.Sprintf("%v", rg)}
  848. }
  849. }
  850. //flatten group vs groups
  851. if singleGroup != "" {
  852. // Check if we have both groups and group provided in the rule config
  853. if len(r.Groups) > 0 {
  854. return r, fmt.Errorf("only one of group or groups should be defined, both provided")
  855. }
  856. r.Groups = []string{singleGroup}
  857. }
  858. return r, nil
  859. }
  860. // sanity returns an error if the rule would be evaluated in a way that would short-circuit a configured check on a wildcard value
  861. // rules are evaluated as "port AND proto AND (ca_sha OR ca_name) AND (host OR group OR groups OR cidr) AND local_cidr"
  862. func (r *rule) sanity() error {
  863. //port, proto, local_cidr are AND, no need to check here
  864. //ca_sha and ca_name don't have a wildcard value, no need to check here
  865. groupsEmpty := len(r.Groups) == 0
  866. hostEmpty := r.Host == ""
  867. cidrEmpty := r.Cidr == ""
  868. if (groupsEmpty && hostEmpty && cidrEmpty) == true {
  869. return nil //no content!
  870. }
  871. groupsHasAny := slices.Contains(r.Groups, "any")
  872. if groupsHasAny && len(r.Groups) > 1 {
  873. return fmt.Errorf("groups spec [%s] contains the group '\"any\". This rule will ignore the other groups specified", r.Groups)
  874. }
  875. if r.Host == "any" {
  876. if !groupsEmpty {
  877. return fmt.Errorf("groups specified as %s, but host=any will match any host, regardless of groups", r.Groups)
  878. }
  879. if !cidrEmpty {
  880. return fmt.Errorf("cidr specified as %s, but host=any will match any host, regardless of cidr", r.Cidr)
  881. }
  882. }
  883. if groupsHasAny {
  884. if !hostEmpty && r.Host != "any" {
  885. return fmt.Errorf("groups spec [%s] contains the group '\"any\". This rule will ignore the specified host %s", r.Groups, r.Host)
  886. }
  887. if !cidrEmpty {
  888. return fmt.Errorf("groups spec [%s] contains the group '\"any\". This rule will ignore the specified cidr %s", r.Groups, r.Cidr)
  889. }
  890. }
  891. if r.Code != "" {
  892. return fmt.Errorf("code specified as [%s]. Support for 'code' will be dropped in a future release, as it has never been functional", r.Code)
  893. }
  894. //todo alert on cidr-any
  895. return nil
  896. }
  897. func parsePort(s string) (int32, int32, error) {
  898. const notAPort int32 = -2
  899. if s == "any" {
  900. return firewall.PortAny, firewall.PortAny, nil
  901. }
  902. if s == "fragment" {
  903. return firewall.PortFragment, firewall.PortFragment, nil
  904. }
  905. if !strings.Contains(s, `-`) {
  906. rPort, err := parsePortValue("", s)
  907. if err != nil {
  908. return notAPort, notAPort, err
  909. }
  910. return rPort, rPort, nil
  911. }
  912. sPorts := strings.SplitN(s, `-`, 2)
  913. for i := range sPorts {
  914. sPorts[i] = strings.Trim(sPorts[i], " ")
  915. }
  916. if len(sPorts) != 2 || sPorts[0] == "" || sPorts[1] == "" {
  917. return notAPort, notAPort, fmt.Errorf("appears to be a range but could not be parsed; `%s`", s)
  918. }
  919. startPort, err := parsePortValue("beginning range ", sPorts[0])
  920. if err != nil {
  921. return notAPort, notAPort, err
  922. }
  923. endPort, err := parsePortValue("ending range ", sPorts[1])
  924. if err != nil {
  925. return notAPort, notAPort, err
  926. }
  927. if startPort == firewall.PortAny {
  928. endPort = firewall.PortAny
  929. }
  930. return startPort, endPort, nil
  931. }
  932. // parsePortValue accepts a base-10 decimal in [0, 65535] and returns it
  933. // widened to int32. Using strconv.ParseUint with bitSize 16 rejects
  934. // negative input, out-of-range input (>65535), and any non-decimal byte
  935. // by construction, so the int32 widening that follows is provably safe
  936. // and cannot collide with firewall.PortAny (0) or firewall.PortFragment
  937. // (-1) via integer truncation.
  938. //
  939. // prefix is prepended to both error messages so callers can disambiguate
  940. // the single-port path (prefix="") from the range bounds (prefix="beginning
  941. // range " / "ending range "), preserving the historical error strings.
  942. func parsePortValue(prefix, s string) (int32, error) {
  943. n, err := strconv.ParseUint(s, 10, 16)
  944. if err == nil {
  945. return int32(n), nil
  946. }
  947. if errors.Is(err, strconv.ErrRange) {
  948. return 0, fmt.Errorf("%sout of range [0,65535]; `%s`", prefix, s)
  949. }
  950. return 0, fmt.Errorf("%swas not a number; `%s`", prefix, s)
  951. }