firewall.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. package nebula
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "hash/fnv"
  8. "net/netip"
  9. "reflect"
  10. "slices"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/gaissmai/bart"
  16. "github.com/rcrowley/go-metrics"
  17. "github.com/sirupsen/logrus"
  18. "github.com/slackhq/nebula/cert"
  19. "github.com/slackhq/nebula/config"
  20. "github.com/slackhq/nebula/firewall"
  21. )
  22. type FirewallInterface interface {
  23. AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, cidr, localCidr string, caName string, caSha string) error
  24. }
  25. type conn struct {
  26. Expires time.Time // Time when this conntrack entry will expire
  27. // record why the original connection passed the firewall, so we can re-validate
  28. // after ruleset changes. Note, rulesVersion is a uint16 so that these two
  29. // fields pack for free after the uint32 above
  30. incoming bool
  31. rulesVersion uint16
  32. }
  33. // TODO: need conntrack max tracked connections handling
  34. type Firewall struct {
  35. Conntrack *FirewallConntrack
  36. InRules *FirewallTable
  37. OutRules *FirewallTable
  38. InSendReject bool
  39. OutSendReject bool
  40. //TODO: we should have many more options for TCP, an option for ICMP, and mimic the kernel a bit better
  41. // https://www.kernel.org/doc/Documentation/networking/nf_conntrack-sysctl.txt
  42. TCPTimeout time.Duration //linux: 5 days max
  43. UDPTimeout time.Duration //linux: 180s max
  44. DefaultTimeout time.Duration //linux: 600s
  45. // routableNetworks describes the vpn addresses as well as any unsafe networks issued to us in the certificate.
  46. // The vpn addresses are a full bit match while the unsafe networks only match the prefix
  47. routableNetworks *bart.Lite
  48. // assignedNetworks is a list of vpn networks assigned to us in the certificate.
  49. assignedNetworks []netip.Prefix
  50. hasUnsafeNetworks bool
  51. rules string
  52. rulesVersion uint16
  53. defaultLocalCIDRAny bool
  54. incomingMetrics firewallMetrics
  55. outgoingMetrics firewallMetrics
  56. l *logrus.Logger
  57. }
  58. type firewallMetrics struct {
  59. droppedLocalAddr metrics.Counter
  60. droppedRemoteAddr metrics.Counter
  61. droppedNoRule metrics.Counter
  62. }
  63. type FirewallConntrack struct {
  64. sync.Mutex
  65. Conns map[firewall.Packet]*conn
  66. TimerWheel *TimerWheel[firewall.Packet]
  67. }
  68. // FirewallTable is the entry point for a rule, the evaluation order is:
  69. // Proto AND port AND (CA SHA or CA name) AND local CIDR AND (group OR groups OR name OR remote CIDR)
  70. type FirewallTable struct {
  71. TCP firewallPort
  72. UDP firewallPort
  73. ICMP firewallPort
  74. AnyProto firewallPort
  75. }
  76. func newFirewallTable() *FirewallTable {
  77. return &FirewallTable{
  78. TCP: firewallPort{},
  79. UDP: firewallPort{},
  80. ICMP: firewallPort{},
  81. AnyProto: firewallPort{},
  82. }
  83. }
  84. type FirewallCA struct {
  85. Any *FirewallRule
  86. CANames map[string]*FirewallRule
  87. CAShas map[string]*FirewallRule
  88. }
  89. type FirewallRule struct {
  90. // Any makes Hosts, Groups, and CIDR irrelevant
  91. Any *firewallLocalCIDR
  92. Hosts map[string]*firewallLocalCIDR
  93. Groups []*firewallGroups
  94. CIDR *bart.Table[*firewallLocalCIDR]
  95. }
  96. type firewallGroups struct {
  97. Groups []string
  98. LocalCIDR *firewallLocalCIDR
  99. }
  100. // Even though ports are uint16, int32 maps are faster for lookup
  101. // Plus we can use `-1` for fragment rules
  102. type firewallPort map[int32]*FirewallCA
  103. type firewallLocalCIDR struct {
  104. Any bool
  105. LocalCIDR *bart.Lite
  106. }
  107. // NewFirewall creates a new Firewall object. A TimerWheel is created for you from the provided timeouts.
  108. // The certificate provided should be the highest version loaded in memory.
  109. func NewFirewall(l *logrus.Logger, tcpTimeout, UDPTimeout, defaultTimeout time.Duration, c cert.Certificate) *Firewall {
  110. //TODO: error on 0 duration
  111. var tmin, tmax time.Duration
  112. if tcpTimeout < UDPTimeout {
  113. tmin = tcpTimeout
  114. tmax = UDPTimeout
  115. } else {
  116. tmin = UDPTimeout
  117. tmax = tcpTimeout
  118. }
  119. if defaultTimeout < tmin {
  120. tmin = defaultTimeout
  121. } else if defaultTimeout > tmax {
  122. tmax = defaultTimeout
  123. }
  124. routableNetworks := new(bart.Lite)
  125. var assignedNetworks []netip.Prefix
  126. for _, network := range c.Networks() {
  127. nprefix := netip.PrefixFrom(network.Addr(), network.Addr().BitLen())
  128. routableNetworks.Insert(nprefix)
  129. assignedNetworks = append(assignedNetworks, network)
  130. }
  131. hasUnsafeNetworks := false
  132. for _, n := range c.UnsafeNetworks() {
  133. routableNetworks.Insert(n)
  134. hasUnsafeNetworks = true
  135. }
  136. return &Firewall{
  137. Conntrack: &FirewallConntrack{
  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, cidr, localCidr, caName string, caSha string) error {
  211. // We need this rule string because we generate a hash. Removing this will break firewall reload.
  212. ruleString := fmt.Sprintf(
  213. "incoming: %v, proto: %v, startPort: %v, endPort: %v, groups: %v, host: %v, ip: %v, localIp: %v, caName: %v, caSha: %s",
  214. incoming, proto, startPort, endPort, groups, host, cidr, localCidr, caName, caSha,
  215. )
  216. f.rules += ruleString + "\n"
  217. direction := "incoming"
  218. if !incoming {
  219. direction = "outgoing"
  220. }
  221. f.l.WithField("firewallRule", m{"direction": direction, "proto": proto, "startPort": startPort, "endPort": endPort, "groups": groups, "host": host, "cidr": cidr, "localCidr": localCidr, "caName": caName, "caSha": caSha}).
  222. Info("Firewall rule added")
  223. var (
  224. ft *FirewallTable
  225. fp firewallPort
  226. )
  227. if incoming {
  228. ft = f.InRules
  229. } else {
  230. ft = f.OutRules
  231. }
  232. switch proto {
  233. case firewall.ProtoTCP:
  234. fp = ft.TCP
  235. case firewall.ProtoUDP:
  236. fp = ft.UDP
  237. case firewall.ProtoICMP, firewall.ProtoICMPv6:
  238. fp = ft.ICMP
  239. case firewall.ProtoAny:
  240. fp = ft.AnyProto
  241. default:
  242. return fmt.Errorf("unknown protocol %v", proto)
  243. }
  244. return fp.addRule(f, startPort, endPort, groups, host, cidr, localCidr, caName, caSha)
  245. }
  246. // GetRuleHash returns a hash representation of all inbound and outbound rules
  247. func (f *Firewall) GetRuleHash() string {
  248. sum := sha256.Sum256([]byte(f.rules))
  249. return hex.EncodeToString(sum[:])
  250. }
  251. // GetRuleHashFNV returns a uint32 FNV-1 hash representation the rules, for use as a metric value
  252. func (f *Firewall) GetRuleHashFNV() uint32 {
  253. h := fnv.New32a()
  254. h.Write([]byte(f.rules))
  255. return h.Sum32()
  256. }
  257. // GetRuleHashes returns both the sha256 and FNV-1 hashes, suitable for logging
  258. func (f *Firewall) GetRuleHashes() string {
  259. return "SHA:" + f.GetRuleHash() + ",FNV:" + strconv.FormatUint(uint64(f.GetRuleHashFNV()), 10)
  260. }
  261. func AddFirewallRulesFromConfig(l *logrus.Logger, inbound bool, c *config.C, fw FirewallInterface) error {
  262. var table string
  263. if inbound {
  264. table = "firewall.inbound"
  265. } else {
  266. table = "firewall.outbound"
  267. }
  268. r := c.Get(table)
  269. if r == nil {
  270. return nil
  271. }
  272. rs, ok := r.([]any)
  273. if !ok {
  274. return fmt.Errorf("%s failed to parse, should be an array of rules", table)
  275. }
  276. for i, t := range rs {
  277. r, err := convertRule(l, t, table, i)
  278. if err != nil {
  279. return fmt.Errorf("%s rule #%v; %s", table, i, err)
  280. }
  281. if r.Code != "" && r.Port != "" {
  282. return fmt.Errorf("%s rule #%v; only one of port or code should be provided", table, i)
  283. }
  284. if r.Host == "" && len(r.Groups) == 0 && r.Cidr == "" && r.LocalCidr == "" && r.CAName == "" && r.CASha == "" {
  285. 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)
  286. }
  287. var sPort, errPort string
  288. if r.Code != "" {
  289. errPort = "code"
  290. sPort = r.Code
  291. } else {
  292. errPort = "port"
  293. sPort = r.Port
  294. }
  295. startPort, endPort, err := parsePort(sPort)
  296. if err != nil {
  297. return fmt.Errorf("%s rule #%v; %s %s", table, i, errPort, err)
  298. }
  299. var proto uint8
  300. switch r.Proto {
  301. case "any":
  302. proto = firewall.ProtoAny
  303. case "tcp":
  304. proto = firewall.ProtoTCP
  305. case "udp":
  306. proto = firewall.ProtoUDP
  307. case "icmp":
  308. proto = firewall.ProtoICMP
  309. default:
  310. return fmt.Errorf("%s rule #%v; proto was not understood; `%s`", table, i, r.Proto)
  311. }
  312. if r.Cidr != "" && r.Cidr != "any" {
  313. _, err = netip.ParsePrefix(r.Cidr)
  314. if err != nil {
  315. return fmt.Errorf("%s rule #%v; cidr did not parse; %s", table, i, err)
  316. }
  317. }
  318. if r.LocalCidr != "" && r.LocalCidr != "any" {
  319. _, err = netip.ParsePrefix(r.LocalCidr)
  320. if err != nil {
  321. return fmt.Errorf("%s rule #%v; local_cidr did not parse; %s", table, i, err)
  322. }
  323. }
  324. if warning := r.sanity(); warning != nil {
  325. l.Warnf("%s rule #%v; %s", table, i, warning)
  326. }
  327. err = fw.AddRule(inbound, proto, startPort, endPort, r.Groups, r.Host, r.Cidr, r.LocalCidr, r.CAName, r.CASha)
  328. if err != nil {
  329. return fmt.Errorf("%s rule #%v; `%s`", table, i, err)
  330. }
  331. }
  332. return nil
  333. }
  334. var ErrUnknownNetworkType = errors.New("unknown network type")
  335. var ErrPeerRejected = errors.New("remote address is not within a network that we handle")
  336. var ErrInvalidRemoteIP = errors.New("remote address is not in remote certificate networks")
  337. var ErrInvalidLocalIP = errors.New("local address is not in list of handled local addresses")
  338. var ErrNoMatchingRule = errors.New("no matching rule in firewall table")
  339. // Drop returns an error if the packet should be dropped, explaining why. It
  340. // returns nil if the packet should not be dropped.
  341. func (f *Firewall) Drop(fp firewall.Packet, incoming bool, h *HostInfo, caPool *cert.CAPool, localCache firewall.ConntrackCache) error {
  342. // Check if we spoke to this tuple, if we did then allow this packet
  343. if f.inConns(fp, h, caPool, localCache) {
  344. return nil
  345. }
  346. // Make sure remote address matches nebula certificate, and determine how to treat it
  347. if h.networks == nil {
  348. // Simple case: Certificate has one address and no unsafe networks
  349. if h.vpnAddrs[0] != fp.RemoteAddr {
  350. f.metrics(incoming).droppedRemoteAddr.Inc(1)
  351. return ErrInvalidRemoteIP
  352. }
  353. } else {
  354. nwType, ok := h.networks.Lookup(fp.RemoteAddr)
  355. if !ok {
  356. f.metrics(incoming).droppedRemoteAddr.Inc(1)
  357. return ErrInvalidRemoteIP
  358. }
  359. switch nwType {
  360. case NetworkTypeVPN:
  361. break // nothing special
  362. case NetworkTypeVPNPeer:
  363. f.metrics(incoming).droppedRemoteAddr.Inc(1)
  364. return ErrPeerRejected // reject for now, one day this may have different FW rules
  365. case NetworkTypeUnsafe:
  366. break // nothing special, one day this may have different FW rules
  367. default:
  368. f.metrics(incoming).droppedRemoteAddr.Inc(1)
  369. return ErrUnknownNetworkType //should never happen
  370. }
  371. }
  372. // Make sure we are supposed to be handling this local ip address
  373. if !f.routableNetworks.Contains(fp.LocalAddr) {
  374. f.metrics(incoming).droppedLocalAddr.Inc(1)
  375. return ErrInvalidLocalIP
  376. }
  377. table := f.OutRules
  378. if incoming {
  379. table = f.InRules
  380. }
  381. // We now know which firewall table to check against
  382. if !table.match(fp, incoming, h.ConnectionState.peerCert, caPool) {
  383. f.metrics(incoming).droppedNoRule.Inc(1)
  384. return ErrNoMatchingRule
  385. }
  386. // We always want to conntrack since it is a faster operation
  387. f.addConn(fp, incoming)
  388. return nil
  389. }
  390. func (f *Firewall) metrics(incoming bool) firewallMetrics {
  391. if incoming {
  392. return f.incomingMetrics
  393. } else {
  394. return f.outgoingMetrics
  395. }
  396. }
  397. // Destroy cleans up any known cyclical references so the object can be free'd my GC. This should be called if a new
  398. // firewall object is created
  399. func (f *Firewall) Destroy() {
  400. //TODO: clean references if/when needed
  401. }
  402. func (f *Firewall) EmitStats() {
  403. conntrack := f.Conntrack
  404. conntrack.Lock()
  405. conntrackCount := len(conntrack.Conns)
  406. conntrack.Unlock()
  407. metrics.GetOrRegisterGauge("firewall.conntrack.count", nil).Update(int64(conntrackCount))
  408. metrics.GetOrRegisterGauge("firewall.rules.version", nil).Update(int64(f.rulesVersion))
  409. metrics.GetOrRegisterGauge("firewall.rules.hash", nil).Update(int64(f.GetRuleHashFNV()))
  410. }
  411. func (f *Firewall) inConns(fp firewall.Packet, h *HostInfo, caPool *cert.CAPool, localCache firewall.ConntrackCache) bool {
  412. if localCache != nil {
  413. if _, ok := localCache[fp]; ok {
  414. return true
  415. }
  416. }
  417. conntrack := f.Conntrack
  418. conntrack.Lock()
  419. // Purge every time we test
  420. ep, has := conntrack.TimerWheel.Purge()
  421. if has {
  422. f.evict(ep)
  423. }
  424. c, ok := conntrack.Conns[fp]
  425. if !ok {
  426. conntrack.Unlock()
  427. return false
  428. }
  429. if c.rulesVersion != f.rulesVersion {
  430. // This conntrack entry was for an older rule set, validate
  431. // it still passes with the current rule set
  432. table := f.OutRules
  433. if c.incoming {
  434. table = f.InRules
  435. }
  436. // We now know which firewall table to check against
  437. if !table.match(fp, c.incoming, h.ConnectionState.peerCert, caPool) {
  438. if f.l.Level >= logrus.DebugLevel {
  439. h.logger(f.l).
  440. WithField("fwPacket", fp).
  441. WithField("incoming", c.incoming).
  442. WithField("rulesVersion", f.rulesVersion).
  443. WithField("oldRulesVersion", c.rulesVersion).
  444. Debugln("dropping old conntrack entry, does not match new ruleset")
  445. }
  446. delete(conntrack.Conns, fp)
  447. conntrack.Unlock()
  448. return false
  449. }
  450. if f.l.Level >= logrus.DebugLevel {
  451. h.logger(f.l).
  452. WithField("fwPacket", fp).
  453. WithField("incoming", c.incoming).
  454. WithField("rulesVersion", f.rulesVersion).
  455. WithField("oldRulesVersion", c.rulesVersion).
  456. Debugln("keeping old conntrack entry, does match new ruleset")
  457. }
  458. c.rulesVersion = f.rulesVersion
  459. }
  460. switch fp.Protocol {
  461. case firewall.ProtoTCP:
  462. c.Expires = time.Now().Add(f.TCPTimeout)
  463. case firewall.ProtoUDP:
  464. c.Expires = time.Now().Add(f.UDPTimeout)
  465. default:
  466. c.Expires = time.Now().Add(f.DefaultTimeout)
  467. }
  468. conntrack.Unlock()
  469. if localCache != nil {
  470. localCache[fp] = struct{}{}
  471. }
  472. return true
  473. }
  474. func (f *Firewall) addConn(fp firewall.Packet, incoming bool) {
  475. var timeout time.Duration
  476. c := &conn{}
  477. switch fp.Protocol {
  478. case firewall.ProtoTCP:
  479. timeout = f.TCPTimeout
  480. case firewall.ProtoUDP:
  481. timeout = f.UDPTimeout
  482. default:
  483. timeout = f.DefaultTimeout
  484. }
  485. conntrack := f.Conntrack
  486. conntrack.Lock()
  487. if _, ok := conntrack.Conns[fp]; !ok {
  488. conntrack.TimerWheel.Advance(time.Now())
  489. conntrack.TimerWheel.Add(fp, timeout)
  490. }
  491. // Record which rulesVersion allowed this connection, so we can retest after
  492. // firewall reload
  493. c.incoming = incoming
  494. c.rulesVersion = f.rulesVersion
  495. c.Expires = time.Now().Add(timeout)
  496. conntrack.Conns[fp] = c
  497. conntrack.Unlock()
  498. }
  499. // Evict checks if a conntrack entry has expired, if so it is removed, if not it is re-added to the wheel
  500. // Caller must own the connMutex lock!
  501. func (f *Firewall) evict(p firewall.Packet) {
  502. // Are we still tracking this conn?
  503. conntrack := f.Conntrack
  504. t, ok := conntrack.Conns[p]
  505. if !ok {
  506. return
  507. }
  508. newT := t.Expires.Sub(time.Now())
  509. // Timeout is in the future, re-add the timer
  510. if newT > 0 {
  511. conntrack.TimerWheel.Advance(time.Now())
  512. conntrack.TimerWheel.Add(p, newT)
  513. return
  514. }
  515. // This conn is done
  516. delete(conntrack.Conns, p)
  517. }
  518. func (ft *FirewallTable) match(p firewall.Packet, incoming bool, c *cert.CachedCertificate, caPool *cert.CAPool) bool {
  519. if ft.AnyProto.match(p, incoming, c, caPool) {
  520. return true
  521. }
  522. switch p.Protocol {
  523. case firewall.ProtoTCP:
  524. if ft.TCP.match(p, incoming, c, caPool) {
  525. return true
  526. }
  527. case firewall.ProtoUDP:
  528. if ft.UDP.match(p, incoming, c, caPool) {
  529. return true
  530. }
  531. case firewall.ProtoICMP, firewall.ProtoICMPv6:
  532. if ft.ICMP.match(p, incoming, c, caPool) {
  533. return true
  534. }
  535. }
  536. return false
  537. }
  538. func (fp firewallPort) addRule(f *Firewall, startPort int32, endPort int32, groups []string, host string, cidr, localCidr, caName string, caSha string) error {
  539. if startPort > endPort {
  540. return fmt.Errorf("start port was lower than end port")
  541. }
  542. for i := startPort; i <= endPort; i++ {
  543. if _, ok := fp[i]; !ok {
  544. fp[i] = &FirewallCA{
  545. CANames: make(map[string]*FirewallRule),
  546. CAShas: make(map[string]*FirewallRule),
  547. }
  548. }
  549. if err := fp[i].addRule(f, groups, host, cidr, localCidr, caName, caSha); err != nil {
  550. return err
  551. }
  552. }
  553. return nil
  554. }
  555. func (fp firewallPort) match(p firewall.Packet, incoming bool, c *cert.CachedCertificate, caPool *cert.CAPool) bool {
  556. // We don't have any allowed ports, bail
  557. if fp == nil {
  558. return false
  559. }
  560. var port int32
  561. if p.Fragment {
  562. port = firewall.PortFragment
  563. } else if incoming {
  564. port = int32(p.LocalPort)
  565. } else {
  566. port = int32(p.RemotePort)
  567. }
  568. if fp[port].match(p, c, caPool) {
  569. return true
  570. }
  571. return fp[firewall.PortAny].match(p, c, caPool)
  572. }
  573. func (fc *FirewallCA) addRule(f *Firewall, groups []string, host string, cidr, localCidr, caName, caSha string) error {
  574. fr := func() *FirewallRule {
  575. return &FirewallRule{
  576. Hosts: make(map[string]*firewallLocalCIDR),
  577. Groups: make([]*firewallGroups, 0),
  578. CIDR: new(bart.Table[*firewallLocalCIDR]),
  579. }
  580. }
  581. if caSha == "" && caName == "" {
  582. if fc.Any == nil {
  583. fc.Any = fr()
  584. }
  585. return fc.Any.addRule(f, groups, host, cidr, localCidr)
  586. }
  587. if caSha != "" {
  588. if _, ok := fc.CAShas[caSha]; !ok {
  589. fc.CAShas[caSha] = fr()
  590. }
  591. err := fc.CAShas[caSha].addRule(f, groups, host, cidr, localCidr)
  592. if err != nil {
  593. return err
  594. }
  595. }
  596. if caName != "" {
  597. if _, ok := fc.CANames[caName]; !ok {
  598. fc.CANames[caName] = fr()
  599. }
  600. err := fc.CANames[caName].addRule(f, groups, host, cidr, localCidr)
  601. if err != nil {
  602. return err
  603. }
  604. }
  605. return nil
  606. }
  607. func (fc *FirewallCA) match(p firewall.Packet, c *cert.CachedCertificate, caPool *cert.CAPool) bool {
  608. if fc == nil {
  609. return false
  610. }
  611. if fc.Any.match(p, c) {
  612. return true
  613. }
  614. if t, ok := fc.CAShas[c.Certificate.Issuer()]; ok {
  615. if t.match(p, c) {
  616. return true
  617. }
  618. }
  619. s, err := caPool.GetCAForCert(c.Certificate)
  620. if err != nil {
  621. return false
  622. }
  623. return fc.CANames[s.Certificate.Name()].match(p, c)
  624. }
  625. func (fr *FirewallRule) addRule(f *Firewall, groups []string, host, cidr, localCidr string) error {
  626. flc := func() *firewallLocalCIDR {
  627. return &firewallLocalCIDR{
  628. LocalCIDR: new(bart.Lite),
  629. }
  630. }
  631. if fr.isAny(groups, host, cidr) {
  632. if fr.Any == nil {
  633. fr.Any = flc()
  634. }
  635. return fr.Any.addRule(f, localCidr)
  636. }
  637. if len(groups) > 0 {
  638. nlc := flc()
  639. err := nlc.addRule(f, localCidr)
  640. if err != nil {
  641. return err
  642. }
  643. fr.Groups = append(fr.Groups, &firewallGroups{
  644. Groups: groups,
  645. LocalCIDR: nlc,
  646. })
  647. }
  648. if host != "" {
  649. nlc := fr.Hosts[host]
  650. if nlc == nil {
  651. nlc = flc()
  652. }
  653. err := nlc.addRule(f, localCidr)
  654. if err != nil {
  655. return err
  656. }
  657. fr.Hosts[host] = nlc
  658. }
  659. if cidr != "" {
  660. c, err := netip.ParsePrefix(cidr)
  661. if err != nil {
  662. return err
  663. }
  664. nlc, _ := fr.CIDR.Get(c)
  665. if nlc == nil {
  666. nlc = flc()
  667. }
  668. err = nlc.addRule(f, localCidr)
  669. if err != nil {
  670. return err
  671. }
  672. fr.CIDR.Insert(c, nlc)
  673. }
  674. return nil
  675. }
  676. func (fr *FirewallRule) isAny(groups []string, host string, cidr string) bool {
  677. if len(groups) == 0 && host == "" && cidr == "" {
  678. return true
  679. }
  680. for _, group := range groups {
  681. if group == "any" {
  682. return true
  683. }
  684. }
  685. if host == "any" {
  686. return true
  687. }
  688. if cidr == "any" {
  689. return true
  690. }
  691. return false
  692. }
  693. func (fr *FirewallRule) match(p firewall.Packet, c *cert.CachedCertificate) bool {
  694. if fr == nil {
  695. return false
  696. }
  697. // Shortcut path for if groups, hosts, or cidr contained an `any`
  698. if fr.Any.match(p, c) {
  699. return true
  700. }
  701. // Need any of group, host, or cidr to match
  702. for _, sg := range fr.Groups {
  703. found := false
  704. for _, g := range sg.Groups {
  705. if _, ok := c.InvertedGroups[g]; !ok {
  706. found = false
  707. break
  708. }
  709. found = true
  710. }
  711. if found && sg.LocalCIDR.match(p, c) {
  712. return true
  713. }
  714. }
  715. if fr.Hosts != nil {
  716. if flc, ok := fr.Hosts[c.Certificate.Name()]; ok {
  717. if flc.match(p, c) {
  718. return true
  719. }
  720. }
  721. }
  722. for _, v := range fr.CIDR.Supernets(netip.PrefixFrom(p.RemoteAddr, p.RemoteAddr.BitLen())) {
  723. if v.match(p, c) {
  724. return true
  725. }
  726. }
  727. return false
  728. }
  729. func (flc *firewallLocalCIDR) addRule(f *Firewall, localCidr string) error {
  730. if localCidr == "any" {
  731. flc.Any = true
  732. return nil
  733. }
  734. if localCidr == "" {
  735. if !f.hasUnsafeNetworks || f.defaultLocalCIDRAny {
  736. flc.Any = true
  737. return nil
  738. }
  739. for _, network := range f.assignedNetworks {
  740. flc.LocalCIDR.Insert(network)
  741. }
  742. return nil
  743. }
  744. c, err := netip.ParsePrefix(localCidr)
  745. if err != nil {
  746. return err
  747. }
  748. flc.LocalCIDR.Insert(c)
  749. return nil
  750. }
  751. func (flc *firewallLocalCIDR) match(p firewall.Packet, c *cert.CachedCertificate) bool {
  752. if flc == nil {
  753. return false
  754. }
  755. if flc.Any {
  756. return true
  757. }
  758. return flc.LocalCIDR.Contains(p.LocalAddr)
  759. }
  760. type rule struct {
  761. Port string
  762. Code string
  763. Proto string
  764. Host 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. singleGroup := 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. //flatten group vs groups
  816. if singleGroup != "" {
  817. // Check if we have both groups and group provided in the rule config
  818. if len(r.Groups) > 0 {
  819. return r, fmt.Errorf("only one of group or groups should be defined, both provided")
  820. }
  821. r.Groups = []string{singleGroup}
  822. }
  823. return r, nil
  824. }
  825. // sanity returns an error if the rule would be evaluated in a way that would short-circuit a configured check on a wildcard value
  826. // rules are evaluated as "port AND proto AND (ca_sha OR ca_name) AND (host OR group OR groups OR cidr) AND local_cidr"
  827. func (r *rule) sanity() error {
  828. //port, proto, local_cidr are AND, no need to check here
  829. //ca_sha and ca_name don't have a wildcard value, no need to check here
  830. groupsEmpty := len(r.Groups) == 0
  831. hostEmpty := r.Host == ""
  832. cidrEmpty := r.Cidr == ""
  833. if (groupsEmpty && hostEmpty && cidrEmpty) == true {
  834. return nil //no content!
  835. }
  836. groupsHasAny := slices.Contains(r.Groups, "any")
  837. if groupsHasAny && len(r.Groups) > 1 {
  838. return fmt.Errorf("groups spec [%s] contains the group '\"any\". This rule will ignore the other groups specified", r.Groups)
  839. }
  840. if r.Host == "any" {
  841. if !groupsEmpty {
  842. return fmt.Errorf("groups specified as %s, but host=any will match any host, regardless of groups", r.Groups)
  843. }
  844. if !cidrEmpty {
  845. return fmt.Errorf("cidr specified as %s, but host=any will match any host, regardless of cidr", r.Cidr)
  846. }
  847. }
  848. if groupsHasAny {
  849. if !hostEmpty && r.Host != "any" {
  850. return fmt.Errorf("groups spec [%s] contains the group '\"any\". This rule will ignore the specified host %s", r.Groups, r.Host)
  851. }
  852. if !cidrEmpty {
  853. return fmt.Errorf("groups spec [%s] contains the group '\"any\". This rule will ignore the specified cidr %s", r.Groups, r.Cidr)
  854. }
  855. }
  856. //todo alert on cidr-any
  857. return nil
  858. }
  859. func parsePort(s string) (startPort, endPort int32, err error) {
  860. if s == "any" {
  861. startPort = firewall.PortAny
  862. endPort = firewall.PortAny
  863. } else if s == "fragment" {
  864. startPort = firewall.PortFragment
  865. endPort = firewall.PortFragment
  866. } else if strings.Contains(s, `-`) {
  867. sPorts := strings.SplitN(s, `-`, 2)
  868. sPorts[0] = strings.Trim(sPorts[0], " ")
  869. sPorts[1] = strings.Trim(sPorts[1], " ")
  870. if len(sPorts) != 2 || sPorts[0] == "" || sPorts[1] == "" {
  871. return 0, 0, fmt.Errorf("appears to be a range but could not be parsed; `%s`", s)
  872. }
  873. rStartPort, err := strconv.Atoi(sPorts[0])
  874. if err != nil {
  875. return 0, 0, fmt.Errorf("beginning range was not a number; `%s`", sPorts[0])
  876. }
  877. rEndPort, err := strconv.Atoi(sPorts[1])
  878. if err != nil {
  879. return 0, 0, fmt.Errorf("ending range was not a number; `%s`", sPorts[1])
  880. }
  881. startPort = int32(rStartPort)
  882. endPort = int32(rEndPort)
  883. if startPort == firewall.PortAny {
  884. endPort = firewall.PortAny
  885. }
  886. } else {
  887. rPort, err := strconv.Atoi(s)
  888. if err != nil {
  889. return 0, 0, fmt.Errorf("was not a number; `%s`", s)
  890. }
  891. startPort = int32(rPort)
  892. endPort = startPort
  893. }
  894. return
  895. }