blob: e7104360c58fe5cf444fcd0e5fa59721c8458dc5 [file] [log] [blame]
Zack Williamse695dea2020-11-19 17:17:40 -07001#!/usr/sbin/nft -f
2{#
3SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org>
4SPDX-License-Identifier: Apache-2.0
5#}
6
7flush ruleset
8
9# Primary rules
10table inet filter {
Zack Williams71e48922020-12-09 13:23:54 -070011 chain input {
Wei-Yu Chen6509d742021-07-29 21:14:51 +080012 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 Williams71e48922020-12-09 13:23:54 -070037 }
38 chain forward {
Wei-Yu Chen6509d742021-07-29 21:14:51 +080039 type filter hook forward priority 0; policy accept;
Zack Williams71e48922020-12-09 13:23:54 -070040 }
41 chain output {
Wei-Yu Chen6509d742021-07-29 21:14:51 +080042 type filter hook output priority 0; policy accept;
Zack Williams71e48922020-12-09 13:23:54 -070043 }
Zack Williamse695dea2020-11-19 17:17:40 -070044}
45
46# NAT
47table ip nat {
Zack Williams71e48922020-12-09 13:23:54 -070048 chain prerouting {
49 type nat hook prerouting priority -100;
50 }
Zack Williamse695dea2020-11-19 17:17:40 -070051
Zack Williams71e48922020-12-09 13:23:54 -070052 chain postrouting {
53 type nat hook postrouting priority 100;
Wei-Yu Chen6509d742021-07-29 21:14:51 +080054 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 Williams71e48922020-12-09 13:23:54 -070063 }
Zack Williamse695dea2020-11-19 17:17:40 -070064}