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 | |
Wei-Yu Chen | e5fb477 | 2021-09-09 14:41:07 +0800 | [diff] [blame^] | 31 | {% if "interface_subnets" in netprep_nftables %} |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame] | 32 | # The ingress traffic restriction of internal networks |
Wei-Yu Chen | e5fb477 | 2021-09-09 14:41:07 +0800 | [diff] [blame^] | 33 | {% for interface in netprep_nftables["interface_subnets"] %} |
| 34 | {% for item in netprep_nftables["interface_subnets"][interface] %} |
| 35 | iif "{{ interface }}" ip saddr {{ item }} accept |
| 36 | {% endfor %} |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame] | 37 | {% endfor %} |
| 38 | {% endif %} |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 39 | } |
| 40 | chain forward { |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame] | 41 | type filter hook forward priority 0; policy accept; |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 42 | } |
| 43 | chain output { |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame] | 44 | type filter hook output priority 0; policy accept; |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 45 | } |
Zack Williams | e695dea | 2020-11-19 17:17:40 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | # NAT |
| 49 | table ip nat { |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 50 | chain prerouting { |
| 51 | type nat hook prerouting priority -100; |
| 52 | } |
Zack Williams | e695dea | 2020-11-19 17:17:40 -0700 | [diff] [blame] | 53 | |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 54 | chain postrouting { |
| 55 | type nat hook postrouting priority 100; |
Wei-Yu Chen | 6509d74 | 2021-07-29 21:14:51 +0800 | [diff] [blame] | 56 | oifname "{{ netprep_nftables["internal_if"] }}" masquerade; |
| 57 | {% if "ue_routing" in netprep_nftables %} |
| 58 | {% for src_subnet in netprep_nftables["ue_routing"]["src_subnets"] %} |
| 59 | {% for ue_subnet in netprep_nftables["ue_routing"]["ue_subnets"] %} |
| 60 | ip saddr {{ src_subnet }} ip daddr {{ ue_subnet }} counter snat to {{ netprep_nftables["ue_routing"]["snat_addr"] }}; |
| 61 | {% endfor %} |
| 62 | {% endfor %} |
| 63 | |
| 64 | {% endif %} |
Zack Williams | 71e4892 | 2020-12-09 13:23:54 -0700 | [diff] [blame] | 65 | } |
Zack Williams | e695dea | 2020-11-19 17:17:40 -0700 | [diff] [blame] | 66 | } |