| 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 | // TestNATExamples are based on the examples given at [1]. |
| 34 | // 1. https://wiki.nftables.org/wiki-nftables/index.php/Performing_Network_Address_Translation_(NAT) |
| 35 | // |
| 36 | // The inet family is mentioned in [1]: |
| 37 | // "Since Linux kernel 5.2, there is support for performing stateful NAT in inet family chains.". |
| 38 | // Therefore, inet family is not used in this example. |
| 39 | func TestNATExamples(t *testing.T) { |
| 40 | testlib.RunTestWithFlushTable(t, testMasqueradeExample) |
| 41 | } |
| 42 | |
| 43 | func testMasqueradeExample(t *testing.T) { |
| 44 | desiredJson, actualJson, err := setupExample(buildMasqueradeConfig) |
| 45 | assert.NoError(t, err) |
| 46 | assert.JSONEq(t, desiredJson, actualJson) |
| 47 | } |
| 48 | |
| 49 | func buildMasqueradeConfig() *nft.Config { |
| 50 | const ( |
| 51 | ip4TableName = "ip4-masquerade-example" |
| 52 | ip6TableName = "ip6-masquerade-example" |
| 53 | |
| 54 | baseChainName = "postrouting" |
| 55 | ) |
| 56 | |
| 57 | chainPriority := 100 |
| 58 | |
| 59 | return &nft.Config{schema.Root{Nftables: []schema.Nftable{ |
| 60 | {Table: &schema.Table{Family: schema.FamilyIP, Name: ip4TableName}}, |
| 61 | {Table: &schema.Table{Family: schema.FamilyIP6, Name: ip6TableName}}, |
| 62 | |
| 63 | {Chain: &schema.Chain{ |
| 64 | Family: schema.FamilyIP, |
| 65 | Table: ip4TableName, |
| 66 | Name: baseChainName, |
| 67 | Type: schema.TypeNAT, |
| 68 | Hook: schema.HookPostRouting, |
| 69 | Prio: &chainPriority, |
| 70 | Policy: schema.PolicyAccept, |
| 71 | }}, |
| 72 | |
| 73 | {Chain: &schema.Chain{ |
| 74 | Family: schema.FamilyIP6, |
| 75 | Table: ip6TableName, |
| 76 | Name: baseChainName, |
| 77 | Type: schema.TypeNAT, |
| 78 | Hook: schema.HookPostRouting, |
| 79 | Prio: &chainPriority, |
| 80 | Policy: schema.PolicyAccept, |
| 81 | }}, |
| 82 | |
| 83 | {Rule: &schema.Rule{ |
| 84 | Family: schema.FamilyIP, |
| 85 | Table: ip4TableName, |
| 86 | Chain: baseChainName, |
| 87 | Expr: []schema.Statement{ |
| 88 | {Nat: schema.Nat{Masquerade: &schema.Masquerade{Enabled: true}}}, |
| 89 | }, |
| 90 | Comment: "generic masquerade", |
| 91 | }}, |
| 92 | |
| 93 | {Rule: &schema.Rule{ |
| 94 | Family: schema.FamilyIP6, |
| 95 | Table: ip6TableName, |
| 96 | Chain: baseChainName, |
| 97 | Expr: []schema.Statement{ |
| 98 | {Nat: schema.Nat{Masquerade: &schema.Masquerade{Enabled: true}}}, |
| 99 | }, |
| 100 | Comment: "generic masquerade", |
| 101 | }}, |
| 102 | }}} |
| 103 | } |