Debian Codegraph @

tests/example/nomacspoofing_test.go

raw symbols SCIP
1 /*
2 * This file is part of the go-nft project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Copyright 2021 Red Hat, Inc.
17 *
18 */
19
20 package example
21
22 import (
23 "testing"
24
25 assert "github.com/stretchr/testify/require"
26
27 "github.com/networkplumbing/go-nft/tests/testlib"
28
29 "github.com/networkplumbing/go-nft/nft"
30 "github.com/networkplumbing/go-nft/nft/schema"
31 )
32
33 func TestNoMacSpoofingExample(t *testing.T) {
34 testlib.RunTestWithFlushTable(t, testNoMacSpoofingExample)
35 }
36
37 func testNoMacSpoofingExample(t *testing.T) {
38 // The definition of no-mac-spoofing, is to prevent users from modifying an interface mac-address and keep
39 // connectivity.
40 // A simple implementation is to allow only a pre-defined number of mac-addresses on egress (filtering the source
41 // mac) and ingress (filtering the destination mac).
42
43 // User input
44 var (
45 ifaceName = "nic0"
46 macAddress = "00:00:00:00:00:01"
47 )
48
49 desiredConfig := buildNoMacSpoofingConfigImperatively(ifaceName, macAddress)
50 assert.Equal(t, desiredConfig, buildNoMacSpoofingConfigDecleratively(ifaceName, macAddress))
51 assert.NoError(t, nft.ApplyConfig(desiredConfig))
52
53 actualConfig, err := nft.ReadConfig()
54 assert.NoError(t, err)
55
56 expectedNftablesEntries := len(desiredConfig.Nftables) + 1 // +1 for the metainfo entry.
57 assert.Len(t, actualConfig.Nftables, expectedNftablesEntries)
58
59 desiredConfig = testlib.NormalizeConfigForComparison(desiredConfig)
60 actualConfig = testlib.NormalizeConfigForComparison(actualConfig)
61
62 desiredJson, err := desiredConfig.ToJSON()
63 assert.NoError(t, err)
64 actualJson, err := actualConfig.ToJSON()
65 assert.NoError(t, err)
66 assert.Equal(t, string(desiredJson), string(actualJson))
67 }
68
69 func buildNoMacSpoofingConfigImperatively(ifaceName string, macAddress string) *nft.Config {
70 // Configuration Details
71 var (
72 baseChainName = "preroute-bridge"
73 ifaceChainName = "example-iface-" + ifaceName
74 macChainName = ifaceChainName + "-mac"
75 )
76 config := nft.NewConfig()
77
78 table := nft.NewTable("example", nft.FamilyBridge)
79 config.AddTable(table)
80
81 chainType, chainHook, chainPrio, chainPolicy := nft.TypeFilter, nft.HookPreRouting, -300, nft.PolicyAccept
82 baseChain := nft.NewChain(table, baseChainName, &chainType, &chainHook, &chainPrio, &chainPolicy)
83 config.AddChain(baseChain)
84
85 ifaceChain := nft.NewRegularChain(table, ifaceChainName)
86 config.AddChain(ifaceChain)
87
88 macChain := nft.NewRegularChain(table, macChainName)
89 config.AddChain(macChain)
90
91 matchIfaceAndJump := []schema.Statement{
92 {Match: &schema.Match{
93 Op: schema.OperEQ,
94 Left: schema.Expression{RowData: []byte(`{"meta":{"key":"iifname"}}`)},
95 Right: schema.Expression{String: &ifaceName},
96 }},
97 {Verdict: schema.Verdict{Jump: &schema.ToTarget{Target: ifaceChainName}}},
98 }
99 matchIfaceRule := nft.NewRule(table, baseChain, matchIfaceAndJump, nil, nil, "match input interface name")
100 config.AddRule(matchIfaceRule)
101
102 jumpToMACChain := []schema.Statement{
103 {Verdict: schema.Verdict{Jump: &schema.ToTarget{Target: macChainName}}},
104 }
105 ifaceRule := nft.NewRule(table, ifaceChain, jumpToMACChain, nil, nil, "redirect to mac-chain")
106 config.AddRule(ifaceRule)
107
108 matchSrcMacAndReturn := []schema.Statement{
109 {Match: &schema.Match{
110 Op: schema.OperEQ,
111 Left: schema.Expression{Payload: &schema.Payload{
112 Protocol: schema.PayloadProtocolEther,
113 Field: schema.PayloadFieldEtherSAddr,
114 }},
115 Right: schema.Expression{String: &macAddress},
116 }},
117 {Verdict: schema.Return()},
118 }
119 matchSrcMacRule := nft.NewRule(table, macChain, matchSrcMacAndReturn, nil, nil, "match source mac address")
120 config.AddRule(matchSrcMacRule)
121
122 drop := []schema.Statement{{Verdict: schema.Drop()}}
123 // When multiple rules are added to a chain, index allows to define an order between them.
124 macRulesIndex := nft.NewRuleIndex()
125 dropRule := nft.NewRule(table, macChain, drop, nil, macRulesIndex.Next(), "drop all the rest")
126 config.AddRule(dropRule)
127
128 return config
129 }
130
131 func buildNoMacSpoofingConfigDecleratively(ifaceName string, macAddress string) *nft.Config {
132 // Configuration Details
133 const tableName = "example"
134 var (
135 baseChainName = "preroute-bridge"
136 ifaceChainName = "example-iface-" + ifaceName
137 macChainName = ifaceChainName + "-mac"
138
139 chainPriority = -300
140
141 macRulesIndex = nft.NewRuleIndex()
142 )
143
144 return &nft.Config{schema.Root{Nftables: []schema.Nftable{
145 {Table: &schema.Table{Family: schema.FamilyBridge, Name: tableName}},
146
147 {Chain: &schema.Chain{
148 Family: schema.FamilyBridge,
149 Table: tableName,
150 Name: baseChainName,
151 Type: schema.TypeFilter,
152 Hook: schema.HookPreRouting,
153 Prio: &chainPriority,
154 Policy: schema.PolicyAccept,
155 }},
156 {Chain: &schema.Chain{
157 Family: schema.FamilyBridge,
158 Table: tableName,
159 Name: ifaceChainName,
160 }},
161 {Chain: &schema.Chain{
162 Family: schema.FamilyBridge,
163 Table: tableName,
164 Name: macChainName,
165 }},
166
167 {Rule: &schema.Rule{
168 Family: schema.FamilyBridge,
169 Table: tableName,
170 Chain: baseChainName,
171 Expr: []schema.Statement{
172 {Match: &schema.Match{
173 Op: schema.OperEQ,
174 Left: schema.Expression{RowData: []byte(`{"meta":{"key":"iifname"}}`)},
175 Right: schema.Expression{String: &ifaceName},
176 }},
177 {Verdict: schema.Verdict{Jump: &schema.ToTarget{Target: ifaceChainName}}},
178 },
179 Comment: "match input interface name",
180 }},
181 {Rule: &schema.Rule{
182 Family: schema.FamilyBridge,
183 Table: tableName,
184 Chain: ifaceChainName,
185 Expr: []schema.Statement{
186 {Verdict: schema.Verdict{Jump: &schema.ToTarget{Target: macChainName}}},
187 },
188 Comment: "redirect to mac-chain",
189 }},
190 {Rule: &schema.Rule{
191 Family: schema.FamilyBridge,
192 Table: tableName,
193 Chain: macChainName,
194 Expr: []schema.Statement{
195 {Match: &schema.Match{
196 Op: schema.OperEQ,
197 Left: schema.Expression{Payload: &schema.Payload{
198 Protocol: schema.PayloadProtocolEther,
199 Field: schema.PayloadFieldEtherSAddr,
200 }},
201 Right: schema.Expression{String: &macAddress},
202 }},
203 {Verdict: schema.Return()},
204 },
205 Comment: "match source mac address",
206 }},
207 {Rule: &schema.Rule{
208 Family: schema.FamilyBridge,
209 Table: tableName,
210 Chain: macChainName,
211 Index: macRulesIndex.Next(),
212 Expr: []schema.Statement{{Verdict: schema.Drop()}},
213 Comment: "drop all the rest",
214 }},
215 }}}
216 }