Merge pull request 'Honour shorewall INVALID_DISPOSITION and UNTRACKED_DISPOSITION' (#31) from benvin/invalid-disposition into main
Reviewed-on: #31
This commit was merged in pull request #31.
This commit is contained in:
@@ -70,21 +70,34 @@ func (c *Compiler) Compile() (*FirewallState, error) {
|
||||
}
|
||||
|
||||
func (c *Compiler) compileConntrackFastPath(state *FirewallState) error {
|
||||
invalid := c.cfg.Settings.InvalidDisposition
|
||||
if invalid == "" {
|
||||
invalid = config.PolicyDrop
|
||||
}
|
||||
for _, chain := range []string{"input", "forward", "output"} {
|
||||
state.Rules[chain] = append(state.Rules[chain],
|
||||
ManagedRule{
|
||||
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
|
||||
Chain: chain,
|
||||
Exprs: append(matchCtState(ctStateEstablished|ctStateRelated),
|
||||
&expr.Verdict{Kind: expr.VerdictAccept}),
|
||||
Tag: "ct:fastpath:" + chain,
|
||||
})
|
||||
for _, d := range []struct {
|
||||
name string
|
||||
state uint32
|
||||
action config.PolicyAction
|
||||
}{
|
||||
{"invalid", ctStateInvalid, invalid},
|
||||
{"untracked", ctStateUntracked, c.cfg.Settings.UntrackedDisposition},
|
||||
} {
|
||||
if d.action == "" || d.action == config.PolicyContinue {
|
||||
continue
|
||||
}
|
||||
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
|
||||
Chain: chain,
|
||||
Exprs: append(matchCtState(ctStateEstablished|ctStateRelated),
|
||||
&expr.Verdict{Kind: expr.VerdictAccept}),
|
||||
Tag: "ct:fastpath:" + chain,
|
||||
},
|
||||
ManagedRule{
|
||||
Chain: chain,
|
||||
Exprs: append(matchCtState(ctStateInvalid),
|
||||
&expr.Verdict{Kind: expr.VerdictDrop}),
|
||||
Tag: "ct:invalid:" + chain,
|
||||
},
|
||||
)
|
||||
Exprs: append(matchCtState(d.state), policyVerdict(d.action, c.cfg.Settings.AddressFamily)...),
|
||||
Tag: "ct:" + d.name + ":" + chain,
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3105,3 +3105,53 @@ func TestSpecCount_CommaAllMatchesExpansion(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompile_Dispositions(t *testing.T) {
|
||||
cases := []struct {
|
||||
invalid, untracked config.PolicyAction
|
||||
want map[string]expr.Any // tag prefix -> verdict expr, nil = absent
|
||||
}{
|
||||
{"", "", map[string]expr.Any{"ct:invalid:": &expr.Verdict{Kind: expr.VerdictDrop}, "ct:untracked:": nil}},
|
||||
{config.PolicyContinue, config.PolicyContinue, map[string]expr.Any{"ct:invalid:": nil, "ct:untracked:": nil}},
|
||||
{config.PolicyReject, config.PolicyAccept, map[string]expr.Any{
|
||||
"ct:invalid:": rejectExprs(0, config.FamilyINET)[0],
|
||||
"ct:untracked:": &expr.Verdict{Kind: expr.VerdictAccept},
|
||||
}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
cfg := &config.Config{
|
||||
Settings: config.Settings{
|
||||
TableName: "test",
|
||||
AddressFamily: config.FamilyINET,
|
||||
InvalidDisposition: tc.invalid,
|
||||
UntrackedDisposition: tc.untracked,
|
||||
},
|
||||
Zones: map[string]config.Zone{"fw": {Type: config.ZoneFirewall}, "net": {Type: config.ZoneIP}},
|
||||
Interfaces: []config.Interface{{Zone: "net", Interface: "eth0"}},
|
||||
Policy: []config.Policy{{Source: "all", Dest: "all", Action: config.PolicyDrop}},
|
||||
PortGroups: make(map[string]config.PortGroup),
|
||||
}
|
||||
state, err := NewCompiler(cfg).Compile()
|
||||
if err != nil {
|
||||
t.Fatalf("Compile: %v", err)
|
||||
}
|
||||
for _, chain := range []string{"input", "forward", "output"} {
|
||||
for prefix, want := range tc.want {
|
||||
var got *ManagedRule
|
||||
for i, r := range state.Rules[chain] {
|
||||
if r.Tag == prefix+chain {
|
||||
got = &state.Rules[chain][i]
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case want == nil && got != nil:
|
||||
t.Errorf("%q/%q: unexpected %s%s rule", tc.invalid, tc.untracked, prefix, chain)
|
||||
case want != nil && got == nil:
|
||||
t.Errorf("%q/%q: missing %s%s rule", tc.invalid, tc.untracked, prefix, chain)
|
||||
case want != nil && !reflect.DeepEqual(got.Exprs[len(got.Exprs)-1], want):
|
||||
t.Errorf("%q/%q: %s%s verdict = %#v, want %#v", tc.invalid, tc.untracked, prefix, chain, got.Exprs[len(got.Exprs)-1], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user