Skip to content

Commit c10af01

Browse files
authored
Merge pull request #880 from maiqueb/mac-spoof-improv-read-only-required-chain-on-cni-del
bridge: read only required chain on cni del instead of the entire ruleset
2 parents 9cf1a09 + 135292e commit c10af01

File tree

7 files changed

+57
-18
lines changed

7 files changed

+57
-18
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/d2g/dhcp4server v0.0.0-20181031114812-7d4a0a7f59a5
1515
github.com/godbus/dbus/v5 v5.1.0
1616
github.com/mattn/go-shellwords v1.0.12
17-
github.com/networkplumbing/go-nft v0.2.0
17+
github.com/networkplumbing/go-nft v0.3.0
1818
github.com/onsi/ginkgo/v2 v2.9.2
1919
github.com/onsi/gomega v1.27.6
2020
github.com/opencontainers/selinux v1.11.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
486486
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
487487
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
488488
github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
489-
github.com/networkplumbing/go-nft v0.2.0 h1:eKapmyVUt/3VGfhYaDos5yeprm+LPt881UeksmKKZHY=
490-
github.com/networkplumbing/go-nft v0.2.0/go.mod h1:HnnM+tYvlGAsMU7yoYwXEVLLiDW9gdMmb5HoGcwpuQs=
489+
github.com/networkplumbing/go-nft v0.3.0 h1:IIc6yHjN85KyJx21p3ZEsO0iBMYHNXux22rc9Q8TfFw=
490+
github.com/networkplumbing/go-nft v0.3.0/go.mod h1:HnnM+tYvlGAsMU7yoYwXEVLLiDW9gdMmb5HoGcwpuQs=
491491
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
492492
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
493493
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=

pkg/link/spoofcheck.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package link
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"os"
21+
"time"
2022

2123
"github.com/networkplumbing/go-nft/nft"
2224
"github.com/networkplumbing/go-nft/nft/schema"
@@ -29,7 +31,7 @@ const (
2931

3032
type NftConfigurer interface {
3133
Apply(*nft.Config) error
32-
Read() (*nft.Config, error)
34+
Read(filterCommands ...string) (*nft.Config, error)
3335
}
3436

3537
type SpoofChecker struct {
@@ -45,8 +47,11 @@ func (dnc defaultNftConfigurer) Apply(cfg *nft.Config) error {
4547
return nft.ApplyConfig(cfg)
4648
}
4749

48-
func (dnc defaultNftConfigurer) Read() (*nft.Config, error) {
49-
return nft.ReadConfig()
50+
func (dnc defaultNftConfigurer) Read(filterCommands ...string) (*nft.Config, error) {
51+
const timeout = 55 * time.Second
52+
ctxWithTimeout, cancelFunc := context.WithTimeout(context.Background(), timeout)
53+
defer cancelFunc()
54+
return nft.ReadConfigContext(ctxWithTimeout, filterCommands...)
5055
}
5156

5257
func NewSpoofChecker(iface, macAddress, refID string) *SpoofChecker {
@@ -109,7 +114,7 @@ func (sc *SpoofChecker) Setup() error {
109114
// interface is removed.
110115
func (sc *SpoofChecker) Teardown() error {
111116
ifaceChain := sc.ifaceChain()
112-
currentConfig, ifaceMatchRuleErr := sc.configurer.Read()
117+
currentConfig, ifaceMatchRuleErr := sc.configurer.Read(listChainBridgeNatPrerouting()...)
113118
if ifaceMatchRuleErr == nil {
114119
expectedRuleToFind := sc.matchIfaceJumpToChainRule(preRoutingBaseChainName, ifaceChain.Name)
115120
// It is safer to exclude the statement matching, avoiding cases where a current statement includes
@@ -241,3 +246,7 @@ func ruleComment(id string) string {
241246
const refIDPrefix = "macspoofchk-"
242247
return refIDPrefix + id
243248
}
249+
250+
func listChainBridgeNatPrerouting() []string {
251+
return []string{"chain", "bridge", natTableName, preRoutingBaseChainName}
252+
}

pkg/link/spoofcheck_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (a *configurerStub) Apply(c *nft.Config) error {
288288
return nil
289289
}
290290

291-
func (a *configurerStub) Read() (*nft.Config, error) {
291+
func (a *configurerStub) Read(_ ...string) (*nft.Config, error) {
292292
if a.failReadConfig {
293293
return nil, fmt.Errorf(errorReadText)
294294
}

vendor/github.com/networkplumbing/go-nft/nft/config.go

Lines changed: 27 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/networkplumbing/go-nft/nft/exec/exec.go

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ github.com/google/pprof/profile
103103
# github.com/mattn/go-shellwords v1.0.12
104104
## explicit; go 1.13
105105
github.com/mattn/go-shellwords
106-
# github.com/networkplumbing/go-nft v0.2.0
106+
# github.com/networkplumbing/go-nft v0.3.0
107107
## explicit; go 1.16
108108
github.com/networkplumbing/go-nft/nft
109109
github.com/networkplumbing/go-nft/nft/config

0 commit comments

Comments
 (0)