firewall.go 25 KB

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