Zack Williams | e695dea | 2020-11-19 17:17:40 -0700 | [diff] [blame] | 1 | #!/usr/sbin/nft -f |
| 2 | {# |
| 3 | SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org> |
| 4 | SPDX-License-Identifier: Apache-2.0 |
| 5 | #} |
| 6 | |
| 7 | flush ruleset |
| 8 | |
| 9 | # Primary rules |
| 10 | table inet filter { |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 11 | chain input { |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame^] | 12 | type filter hook input priority 0; policy drop; |
| 13 | |
| 14 | # The basic rules to accept ICMP and established connection |
| 15 | iif "lo" accept |
| 16 | ip protocol icmp accept |
| 17 | ct state established,related accept |
| 18 | ct state invalid drop |
| 19 | |
| 20 | {% if "services" in netprep_nftables %} |
| 21 | ## The service present on this server |
| 22 | {% for item in netprep_nftables["services"] %} |
| 23 | # For service {{ item["name"] }} |
| 24 | iif "{{ netprep_nftables["external_if"] }}" {{ item["protocol"] }} dport {{ item["port"]}} accept |
| 25 | {% endfor %} |
| 26 | {% endif %} |
| 27 | |
| 28 | # Allow SSH on all interfaces |
| 29 | tcp dport ssh accept |
| 30 | |
| 31 | {% if "allow_subnets" in netprep_nftables %} |
| 32 | # The ingress traffic restriction of internal networks |
| 33 | {% for item in netprep_nftables["allow_subnets"] %} |
| 34 | iif "{{ netprep_nftables["internal_if"] }}" ip saddr {{ item }} accept |
| 35 | {% endfor %} |
| 36 | {% endif %} |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 37 | } |
| 38 | chain forward { |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame^] | 39 | type filter hook forward priority 0; policy accept; |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 40 | } |
| 41 | chain output { |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame^] | 42 | type filter hook output priority 0; policy accept; |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 43 | } |
Zack Williams | e695dea | 2020-11-19 17:17:40 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | # NAT |
| 47 | table ip nat { |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 48 | chain prerouting { |
| 49 | type nat hook prerouting priority -100; |
| 50 | } |
Zack Williams | e695dea | 2020-11-19 17:17:40 -0700 | [diff] [blame] | 51 | |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 52 | chain postrouting { |
| 53 | type nat hook postrouting priority 100; |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame^] | 54 | oifname "{{ netprep_nftables["internal_if"] }}" masquerade; |
| 55 | {% if "ue_routing" in netprep_nftables %} |
| 56 | {% for src_subnet in netprep_nftables["ue_routing"]["src_subnets"] %} |
| 57 | {% for ue_subnet in netprep_nftables["ue_routing"]["ue_subnets"] %} |
| 58 | ip saddr {{ src_subnet }} ip daddr {{ ue_subnet }} counter snat to {{ netprep_nftables["ue_routing"]["snat_addr"] }}; |
| 59 | {% endfor %} |
| 60 | {% endfor %} |
| 61 | |
| 62 | {% endif %} |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 63 | } |
Zack Williams | e695dea | 2020-11-19 17:17:40 -0700 | [diff] [blame] | 64 | } |