2
0

firewall.go 24 KB

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