firewall.go 26 KB

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