|
| 1 | +// Copyright 2025 Google LLC. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package integration |
| 16 | + |
| 17 | +import ( |
| 18 | + "flag" |
| 19 | + "os/exec" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "github.com/google/nftables" |
| 25 | + "github.com/google/nftables/binaryutil" |
| 26 | + "github.com/google/nftables/expr" |
| 27 | + "github.com/google/nftables/internal/nftest" |
| 28 | + "github.com/vishvananda/netlink" |
| 29 | +) |
| 30 | + |
| 31 | +var enableSysTests = flag.Bool("run_system_tests", false, "Run tests that operate against the live kernel") |
| 32 | + |
| 33 | +func ifname(n string) []byte { |
| 34 | + b := make([]byte, 16) |
| 35 | + copy(b, []byte(n+"\x00")) |
| 36 | + return b |
| 37 | +} |
| 38 | + |
| 39 | +func TestNFTables(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + scriptPath string |
| 43 | + goCommands func(t *testing.T, c *nftables.Conn) |
| 44 | + expectFailure bool |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "AddTable", |
| 48 | + scriptPath: "testdata/add_table.nft", |
| 49 | + goCommands: func(t *testing.T, c *nftables.Conn) { |
| 50 | + c.FlushRuleset() |
| 51 | + |
| 52 | + c.AddTable(&nftables.Table{ |
| 53 | + Name: "test-table", |
| 54 | + Family: nftables.TableFamilyINet, |
| 55 | + }) |
| 56 | + |
| 57 | + err := c.Flush() |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Error creating table: %v", err) |
| 60 | + } |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "AddChain", |
| 65 | + scriptPath: "testdata/add_chain.nft", |
| 66 | + goCommands: func(t *testing.T, c *nftables.Conn) { |
| 67 | + c.FlushRuleset() |
| 68 | + |
| 69 | + table := c.AddTable(&nftables.Table{ |
| 70 | + Name: "test-table", |
| 71 | + Family: nftables.TableFamilyINet, |
| 72 | + }) |
| 73 | + |
| 74 | + c.AddChain(&nftables.Chain{ |
| 75 | + Name: "test-chain", |
| 76 | + Table: table, |
| 77 | + Hooknum: nftables.ChainHookOutput, |
| 78 | + Priority: nftables.ChainPriorityNATDest, |
| 79 | + Type: nftables.ChainTypeNAT, |
| 80 | + }) |
| 81 | + |
| 82 | + err := c.Flush() |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Error creating table: %v", err) |
| 85 | + } |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "AddFlowtables", |
| 90 | + scriptPath: "testdata/add_flowtables.nft", |
| 91 | + goCommands: func(t *testing.T, c *nftables.Conn) { |
| 92 | + devices := []string{"dummy0"} |
| 93 | + c.FlushRuleset() |
| 94 | + // add + delete + add for flushing all the table |
| 95 | + table := c.AddTable(&nftables.Table{ |
| 96 | + Family: nftables.TableFamilyINet, |
| 97 | + Name: "test-table", |
| 98 | + }) |
| 99 | + |
| 100 | + devicesSet := &nftables.Set{ |
| 101 | + Table: table, |
| 102 | + Name: "test-set", |
| 103 | + KeyType: nftables.TypeIFName, |
| 104 | + KeyByteOrder: binaryutil.NativeEndian, |
| 105 | + } |
| 106 | + |
| 107 | + elements := []nftables.SetElement{} |
| 108 | + for _, dev := range devices { |
| 109 | + elements = append(elements, nftables.SetElement{ |
| 110 | + Key: ifname(dev), |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + if err := c.AddSet(devicesSet, elements); err != nil { |
| 115 | + t.Errorf("failed to add Set %s : %v", devicesSet.Name, err) |
| 116 | + } |
| 117 | + |
| 118 | + flowtable := &nftables.Flowtable{ |
| 119 | + Table: table, |
| 120 | + Name: "test-flowtable", |
| 121 | + Devices: devices, |
| 122 | + Hooknum: nftables.FlowtableHookIngress, |
| 123 | + Priority: nftables.FlowtablePriorityRef(5), |
| 124 | + } |
| 125 | + c.AddFlowtable(flowtable) |
| 126 | + |
| 127 | + chain := c.AddChain(&nftables.Chain{ |
| 128 | + Name: "test-chain", |
| 129 | + Table: table, |
| 130 | + Type: nftables.ChainTypeFilter, |
| 131 | + Hooknum: nftables.ChainHookForward, |
| 132 | + Priority: nftables.ChainPriorityMangle, |
| 133 | + }) |
| 134 | + |
| 135 | + c.AddRule(&nftables.Rule{ |
| 136 | + Table: table, |
| 137 | + Chain: chain, |
| 138 | + Exprs: []expr.Any{ |
| 139 | + &expr.Meta{Key: expr.MetaKeyIIFNAME, SourceRegister: false, Register: 0x1}, |
| 140 | + &expr.Lookup{SourceRegister: 0x1, DestRegister: 0x0, IsDestRegSet: false, SetName: "test-set", Invert: true}, |
| 141 | + &expr.Verdict{Kind: expr.VerdictReturn}, |
| 142 | + }, |
| 143 | + }) |
| 144 | + |
| 145 | + c.AddRule(&nftables.Rule{ |
| 146 | + Table: table, |
| 147 | + Chain: chain, |
| 148 | + Exprs: []expr.Any{ |
| 149 | + &expr.Meta{Key: expr.MetaKeyOIFNAME, SourceRegister: false, Register: 0x1}, |
| 150 | + &expr.Lookup{SourceRegister: 0x1, DestRegister: 0x0, IsDestRegSet: false, SetName: "test-set", Invert: true}, |
| 151 | + &expr.Verdict{Kind: expr.VerdictReturn}, |
| 152 | + }, |
| 153 | + }) |
| 154 | + |
| 155 | + c.AddRule(&nftables.Rule{ |
| 156 | + Table: table, |
| 157 | + Chain: chain, |
| 158 | + Exprs: []expr.Any{ |
| 159 | + &expr.Ct{Register: 0x1, SourceRegister: false, Key: expr.CtKeySTATE, Direction: 0x0}, |
| 160 | + &expr.Bitwise{SourceRegister: 0x1, DestRegister: 0x1, Len: 0x4, Mask: binaryutil.NativeEndian.PutUint32(expr.CtStateBitESTABLISHED), Xor: binaryutil.NativeEndian.PutUint32(0)}, |
| 161 | + &expr.Cmp{Op: 0x1, Register: 0x1, Data: []uint8{0x0, 0x0, 0x0, 0x0}}, |
| 162 | + &expr.Ct{Register: 0x1, SourceRegister: false, Key: expr.CtKeyPKTS, Direction: 0x0}, |
| 163 | + &expr.Cmp{Op: expr.CmpOpGt, Register: 0x1, Data: binaryutil.NativeEndian.PutUint64(20)}, |
| 164 | + &expr.FlowOffload{Name: "test-flowtable"}, |
| 165 | + &expr.Counter{}, |
| 166 | + }, |
| 167 | + }) |
| 168 | + |
| 169 | + if err := c.Flush(); err != nil { |
| 170 | + t.Fatal(err) |
| 171 | + } |
| 172 | + }, |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + for _, tt := range tests { |
| 177 | + t.Run(tt.name, func(t *testing.T) { |
| 178 | + // Create a new network namespace to test these operations, |
| 179 | + // and tear down the namespace at test completion. |
| 180 | + c, newNS := nftest.OpenSystemConn(t, *enableSysTests) |
| 181 | + defer nftest.CleanupSystemConn(t, newNS) |
| 182 | + |
| 183 | + // Real interface must exist otherwise some nftables will fail |
| 184 | + la := netlink.NewLinkAttrs() |
| 185 | + la.Name = "dummy0" |
| 186 | + dummy := &netlink.Dummy{LinkAttrs: la} |
| 187 | + if err := netlink.LinkAdd(dummy); err != nil { |
| 188 | + t.Fatal(err) |
| 189 | + } |
| 190 | + |
| 191 | + scriptOutput, err := applyNFTRuleset(tt.scriptPath) |
| 192 | + if err != nil { |
| 193 | + t.Fatalf("Failed to apply nftables script: %v\noutput:%s", err, scriptOutput) |
| 194 | + } |
| 195 | + if len(scriptOutput) > 0 { |
| 196 | + t.Logf("nft output:\n%s", scriptOutput) |
| 197 | + } |
| 198 | + |
| 199 | + // Retrieve nftables state using nft |
| 200 | + expectedOutput, err := listNFTRuleset() |
| 201 | + if err != nil { |
| 202 | + t.Fatalf("Failed to list nftables ruleset: %v\noutput:%s", err, expectedOutput) |
| 203 | + } |
| 204 | + t.Logf("Expected output:\n%s", expectedOutput) |
| 205 | + |
| 206 | + // Program nftables using your Go code |
| 207 | + if err := flushNFTRuleset(); err != nil { |
| 208 | + t.Fatalf("Failed to flush nftables ruleset: %v", err) |
| 209 | + } |
| 210 | + tt.goCommands(t, c) |
| 211 | + |
| 212 | + // Retrieve nftables state using nft |
| 213 | + actualOutput, err := listNFTRuleset() |
| 214 | + if err != nil { |
| 215 | + t.Fatalf("Failed to list nftables ruleset: %v\noutput:%s", err, actualOutput) |
| 216 | + } |
| 217 | + |
| 218 | + t.Logf("Actual output:\n%s", actualOutput) |
| 219 | + |
| 220 | + if expectedOutput != actualOutput { |
| 221 | + t.Errorf("nftables ruleset mismatch:\n%s", cmp.Diff(expectedOutput, actualOutput)) |
| 222 | + } |
| 223 | + |
| 224 | + if err := flushNFTRuleset(); err != nil { |
| 225 | + t.Fatalf("Failed to flush nftables ruleset: %v", err) |
| 226 | + } |
| 227 | + }) |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +func applyNFTRuleset(scriptPath string) (string, error) { |
| 232 | + cmd := exec.Command("nft", "--debug=all", "-f", scriptPath) |
| 233 | + out, err := cmd.CombinedOutput() |
| 234 | + if err != nil { |
| 235 | + return string(out), err |
| 236 | + } |
| 237 | + return strings.TrimSpace(string(out)), nil |
| 238 | +} |
| 239 | + |
| 240 | +func listNFTRuleset() (string, error) { |
| 241 | + cmd := exec.Command("nft", "list", "ruleset") |
| 242 | + out, err := cmd.CombinedOutput() |
| 243 | + if err != nil { |
| 244 | + return string(out), err |
| 245 | + } |
| 246 | + return strings.TrimSpace(string(out)), nil |
| 247 | +} |
| 248 | + |
| 249 | +func flushNFTRuleset() error { |
| 250 | + cmd := exec.Command("nft", "flush", "ruleset") |
| 251 | + return cmd.Run() |
| 252 | +} |
0 commit comments