Skip to content

Commit 1bbd057

Browse files
committed
chore: merge upstream changes into mjholub/firewall-errors-wrap
1 parent 6966e3b commit 1bbd057

File tree

2 files changed

+46
-95
lines changed

2 files changed

+46
-95
lines changed

daemon/firewall/common/common.go

+33-82
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@ var (
1717
RestoreChains = true
1818
BackupChains = true
1919
ReloadConf = true
20-
21-
DefaultCheckInterval = 10 * time.Second
22-
RulesCheckerDisabled = "0s"
2320
)
2421

2522
type (
2623
callback func()
2724
callbackBool func() bool
2825

26+
stopChecker struct {
27+
ch chan bool
28+
sync.RWMutex
29+
}
30+
2931
// Common holds common fields and functionality of both firewalls,
3032
// iptables and nftables.
3133
Common struct {
32-
RulesChecker *time.Ticker
33-
ErrChan chan string
34-
stopChecker chan bool
35-
RulesCheckInterval time.Duration
36-
QueueNum uint16
37-
Running bool
38-
Intercepting bool
39-
FwEnabled bool
34+
RulesChecker *time.Ticker
35+
stopCheckerChan *stopChecker
36+
QueueNum uint16
37+
Running bool
38+
Intercepting bool
39+
FwEnabled bool
4040
sync.RWMutex
4141
}
4242
// FirewallError is a type that holds both IPv4 and IPv6 errors.
@@ -77,46 +77,15 @@ func (s *stopChecker) exit() <-chan bool {
7777
return s.ch
7878
}
7979

80-
// ErrorsChan returns the channel where the errors are sent to.
81-
func (c *Common) ErrorsChan() <-chan string {
82-
return c.ErrChan
83-
}
84-
85-
// ErrChanEmpty checks if the errors channel is empty.
86-
func (c *Common) ErrChanEmpty() bool {
87-
return len(c.ErrChan) == 0
88-
}
89-
90-
// SendError sends an error to the channel of errors.
91-
func (c *Common) SendError(err string) {
92-
log.Warning("%s", err)
80+
func (s *stopChecker) stop() {
81+
s.Lock()
82+
defer s.Unlock()
9383

94-
if len(c.ErrChan) >= cap(c.ErrChan) {
95-
log.Debug("fw errors channel full, emptying errChan")
96-
for e := range c.ErrChan {
97-
log.Warning("%s", e)
98-
if c.ErrChanEmpty() {
99-
break
100-
}
101-
}
102-
return
103-
}
104-
select {
105-
case c.ErrChan <- err:
106-
case <-time.After(100 * time.Millisecond):
107-
log.Warning("SendError() channel locked? REVIEW")
108-
}
109-
}
110-
111-
func (c *Common) SetRulesCheckerInterval(interval string) {
112-
dur, err := time.ParseDuration(interval)
113-
if err != nil {
114-
log.Warning("Invalid rules checker interval (falling back to %s): %s", DefaultCheckInterval, err)
115-
c.RulesCheckInterval = DefaultCheckInterval
116-
return
84+
if s.ch != nil {
85+
s.ch <- true
86+
close(s.ch)
87+
s.ch = nil
11788
}
118-
119-
c.RulesCheckInterval = dur
12089
}
12190

12291
// SetQueueNum sets the queue number used by the firewall.
@@ -128,6 +97,7 @@ func (c *Common) SetQueueNum(qNum *int) {
12897
if qNum != nil {
12998
c.QueueNum = uint16(*qNum)
13099
}
100+
131101
}
132102

133103
// IsRunning returns if the firewall is running or not.
@@ -160,38 +130,25 @@ func (c *Common) IsIntercepting() bool {
160130
func (c *Common) NewRulesChecker(areRulesLoaded callbackBool, reloadRules callback) {
161131
c.Lock()
162132
defer c.Unlock()
163-
if c.RulesCheckInterval.String() == RulesCheckerDisabled {
164-
log.Info("Fw rules checker disabled ...")
165-
return
133+
if c.stopCheckerChan != nil {
134+
c.stopCheckerChan.stop()
135+
c.stopCheckerChan = nil
166136
}
167137

168-
if c.RulesChecker != nil {
169-
c.RulesChecker.Stop()
170-
select {
171-
case c.stopChecker <- true:
172-
case <-time.After(5 * time.Millisecond):
173-
log.Error("NewRulesChecker: timed out stopping monitor rules")
174-
}
175-
}
176-
c.stopChecker = make(chan bool, 1)
177-
log.Info("Starting new fw checker every %s ...", c.RulesCheckInterval)
178-
c.RulesChecker = time.NewTicker(c.RulesCheckInterval)
138+
c.stopCheckerChan = &stopChecker{ch: make(chan bool, 1)}
139+
c.RulesChecker = time.NewTicker(time.Second * 15)
179140

180-
go startCheckingRules(c.stopChecker, c.RulesChecker, areRulesLoaded, reloadRules)
141+
go c.startCheckingRules(areRulesLoaded, reloadRules)
181142
}
182143

183144
// StartCheckingRules monitors if our rules are loaded.
184145
// If the rules to intercept traffic are not loaded, we'll try to insert them again.
185-
func startCheckingRules(exitChan <-chan bool, rulesChecker *time.Ticker, areRulesLoaded callbackBool, reloadRules callback) {
146+
func (c *Common) startCheckingRules(areRulesLoaded callbackBool, reloadRules callback) {
186147
for {
187148
select {
188-
case <-exitChan:
149+
case <-c.stopCheckerChan.exit():
189150
goto Exit
190-
case _, active := <-rulesChecker.C:
191-
if !active {
192-
goto Exit
193-
}
194-
151+
case <-c.RulesChecker.C:
195152
if areRulesLoaded() == false {
196153
reloadRules()
197154
}
@@ -204,20 +161,14 @@ Exit:
204161

205162
// StopCheckingRules stops checking if firewall rules are loaded.
206163
func (c *Common) StopCheckingRules() {
207-
c.Lock()
208-
defer c.Unlock()
164+
c.RLock()
165+
defer c.RUnlock()
209166

210167
if c.RulesChecker != nil {
211-
select {
212-
case c.stopChecker <- true:
213-
close(c.stopChecker)
214-
case <-time.After(5 * time.Millisecond):
215-
// We should not arrive here
216-
log.Error("StopCheckingRules: timed out stopping monitor rules")
217-
}
218-
219168
c.RulesChecker.Stop()
220-
c.RulesChecker = nil
169+
}
170+
if c.stopCheckerChan != nil {
171+
c.stopCheckerChan.stop()
221172
}
222173
}
223174

daemon/firewall/iptables/iptables.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ const (
4141

4242
// SystemRule blabla
4343
type SystemRule struct {
44-
Rule *config.FwRule
4544
Table string
4645
Chain string
46+
Rule *config.FwRule
4747
}
4848

4949
// SystemChains keeps track of the fw rules that have been added to the system.
@@ -54,13 +54,16 @@ type SystemChains struct {
5454

5555
// Iptables struct holds the fields of the iptables fw
5656
type Iptables struct {
57+
config.Config
58+
common.Common
59+
60+
bin string
61+
bin6 string
62+
5763
regexRulesQuery *regexp.Regexp
5864
regexSystemRulesQuery *regexp.Regexp
59-
bin string
60-
bin6 string
61-
chains SystemChains
62-
common.Common
63-
config.Config
65+
66+
chains SystemChains
6467

6568
sync.Mutex
6669
}
@@ -93,18 +96,16 @@ func (ipt *Iptables) Name() string {
9396

9497
// Init inserts the firewall rules and starts monitoring for firewall
9598
// changes.
96-
func (ipt *Iptables) Init(qNum *int, configPath, monitorInterval string) {
99+
func (ipt *Iptables) Init(qNum *int) {
97100
if ipt.IsRunning() {
98101
return
99102
}
100103
ipt.SetQueueNum(qNum)
101-
ipt.SetRulesCheckerInterval(monitorInterval)
102-
ipt.ErrChan = make(chan string, 100)
103104

104105
// In order to clean up any existing firewall rule before start,
105106
// we need to load the fw configuration first to know what rules
106107
// were configured.
107-
ipt.NewSystemFwConfig(configPath, ipt.preloadConfCallback, ipt.reloadRulesCallback)
108+
ipt.NewSystemFwConfig(ipt.preloadConfCallback, ipt.reloadRulesCallback)
108109
ipt.LoadDiskConfiguration(!common.ReloadConf)
109110

110111
// start from a clean state
@@ -117,7 +118,6 @@ func (ipt *Iptables) Init(qNum *int, configPath, monitorInterval string) {
117118

118119
// Stop deletes the firewall rules, allowing network traffic.
119120
func (ipt *Iptables) Stop() {
120-
ipt.ErrChan = make(chan string, 100)
121121
if ipt.Running == false {
122122
return
123123
}
@@ -141,9 +141,9 @@ func IsAvailable() error {
141141
// EnableInterception adds fw rules to intercept connections.
142142
func (ipt *Iptables) EnableInterception() {
143143
if err := ipt.QueueConnections(common.EnableRule, true); err != nil {
144-
log.Fatal("Error while running conntrack firewall rule: %s %s", err)
144+
log.Fatal("Error while running conntrack firewall rule: %v", err.AsError())
145145
} else if err = ipt.QueueDNSResponses(common.EnableRule, true); err != nil {
146-
log.Error("Error while running DNS firewall rule: %s %s", err)
146+
log.Error("Error while running DNS firewall rule: %v", err.AsError())
147147
}
148148
// start monitoring firewall rules to intercept network traffic
149149
ipt.NewRulesChecker(ipt.AreRulesLoaded, ipt.reloadRulesCallback)

0 commit comments

Comments
 (0)