|
5 | 5 | {% from "iptables/map.jinja" import firewall with context %}
|
6 | 6 | {% set install = firewall.install %}
|
7 | 7 | {% set strict_mode = firewall.strict %}
|
| 8 | +{% set ipv6 = firewall.get('ipv6', False) %} |
8 | 9 | {% set global_block_nomatch = firewall.block_nomatch %}
|
9 | 10 | {% set packages = firewall.pkgs %}
|
| 11 | +{% set ipv4 = 'IPv4' %} |
| 12 | +{% set ipv6 = 'IPv6' %} |
| 13 | +{% set protocols = [ipv4] %} |
| 14 | +{% if ipv6 %} |
| 15 | +{% do protocols.append(ipv6) %} |
| 16 | +{% endif %} |
| 17 | +{% set sufixes = {ipv4: '', ipv6: '_ipv6'} %} |
10 | 18 |
|
11 | 19 | {%- if firewall.enabled %}
|
12 | 20 | {%- if install %}
|
|
22 | 30 | {%- if strict_mode %}
|
23 | 31 | # If the firewall is set to strict mode, we'll need to allow some
|
24 | 32 | # that always need access to anything
|
25 |
| - iptables_allow_localhost: |
| 33 | + {%- for protocol in protocols %} |
| 34 | + iptables_allow_localhost{{sufixes[protocol]}}: |
26 | 35 | iptables.append:
|
27 | 36 | - table: filter
|
28 | 37 | - chain: INPUT
|
29 | 38 | - jump: ACCEPT
|
| 39 | + {%- if protocol == ipv4 %} |
30 | 40 | - source: 127.0.0.1
|
| 41 | + {%- else %} |
| 42 | + - source: ::1 |
| 43 | + - family: ipv6 |
| 44 | + {%- endif %} |
31 | 45 | - save: True
|
32 | 46 |
|
33 | 47 | # Allow related/established sessions
|
34 |
| - iptables_allow_established: |
| 48 | + iptables_allow_established{{sufixes[protocol]}}: |
35 | 49 | iptables.append:
|
36 | 50 | - table: filter
|
37 | 51 | - chain: INPUT
|
38 | 52 | - jump: ACCEPT
|
39 | 53 | - match: conntrack
|
40 | 54 | - ctstate: 'RELATED,ESTABLISHED'
|
| 55 | + {%- if protocol == ipv6 %} |
| 56 | + - family: ipv6 |
| 57 | + {%- endif %} |
41 | 58 | - save: True
|
42 | 59 |
|
43 | 60 | # Set the policy to deny everything unless defined
|
44 |
| - enable_reject_policy: |
| 61 | + enable_reject_policy{{sufixes[protocol]}}: |
45 | 62 | iptables.set_policy:
|
46 | 63 | - table: filter
|
47 | 64 | - chain: INPUT
|
48 | 65 | - policy: DROP
|
| 66 | + {%- if protocol == ipv6 %} |
| 67 | + - family: ipv6 |
| 68 | + {%- endif %} |
49 | 69 | - require:
|
50 |
| - - iptables: iptables_allow_localhost |
51 |
| - - iptables: iptables_allow_established |
| 70 | + - iptables: iptables_allow_localhost{{sufixes[protocol]}} |
| 71 | + - iptables: iptables_allow_established{{sufixes[protocol]}} |
| 72 | + {%- endfor %} |
52 | 73 | {%- endif %}
|
53 | 74 |
|
54 | 75 | # Generate ipsets for all services that we have information about
|
55 |
| - {%- for service_name, service_details in firewall.get('services', {}).items() %} |
56 |
| - {% set block_nomatch = service_details.get('block_nomatch', False) %} |
57 |
| - {% set interfaces = service_details.get('interfaces','') %} |
58 |
| - {% set protos = service_details.get('protos',['tcp']) %} |
59 |
| - {% if service_details.get('comment', False) %} |
60 |
| - {% set comment = '- comment: ' + service_details.get('comment') %} |
61 |
| - {% else %} |
62 |
| - {% set comment = '' %} |
63 |
| - {% endif %} |
| 76 | + {%- for protocol in protocols %} |
| 77 | + {%- for service_name, service_details in firewall.get('services' + sufixes[protocol], {}).items() %} |
| 78 | + {% set block_nomatch = service_details.get('block_nomatch', False) %} |
| 79 | + {% set interfaces = service_details.get('interfaces','') %} |
| 80 | + {% set protos = service_details.get('protos',['tcp']) %} |
| 81 | + {% if service_details.get('comment', False) %} |
| 82 | + {% set comment = '- comment: ' + service_details.get('comment') %} |
| 83 | + {% else %} |
| 84 | + {% set comment = '' %} |
| 85 | + {% endif %} |
64 | 86 |
|
65 |
| - # Allow rules for ips/subnets |
66 |
| - {%- for ip in service_details.get('ips_allow', ['0.0.0.0/0']) %} |
67 |
| - {%- if interfaces == '' %} |
68 |
| - {%- for proto in protos %} |
69 |
| - iptables_{{service_name}}_allow_{{ip}}_{{proto}}: |
| 87 | + # Allow rules for ips/subnets |
| 88 | + {%- for ip in service_details.get('ips_allow', ['0.0.0.0/0']) %} |
| 89 | + {%- if interfaces == '' %} |
| 90 | + {%- for proto in protos %} |
| 91 | + iptables_{{service_name}}_allow_{{ip}}_{{proto}}{{sufixes[protocol]}}: |
70 | 92 | iptables.insert:
|
71 | 93 | - position: 1
|
72 | 94 | - table: filter
|
|
75 | 97 | - source: {{ ip }}
|
76 | 98 | - dport: {{ service_name }}
|
77 | 99 | - proto: {{ proto }}
|
| 100 | + {%- if protocol == ipv6 %} |
| 101 | + - family: ipv6 |
| 102 | + {%- endif %} |
78 | 103 | - save: True
|
79 | 104 | {{ comment }}
|
80 |
| - {%- endfor %} |
81 |
| - {%- else %} |
82 |
| - {%- for interface in interfaces %} |
83 |
| - {%- for proto in protos %} |
84 |
| - iptables_{{service_name}}_allow_{{ip}}_{{proto}}_{{interface}}: |
| 105 | + {%- endfor %} |
| 106 | + {%- else %} |
| 107 | + {%- for interface in interfaces %} |
| 108 | + {%- for proto in protos %} |
| 109 | + iptables_{{service_name}}_allow_{{ip}}_{{proto}}_{{interface}}{{sufixes[protocol]}}: |
85 | 110 | iptables.insert:
|
86 | 111 | - position: 1
|
87 | 112 | - table: filter
|
|
91 | 116 | - source: {{ ip }}
|
92 | 117 | - dport: {{ service_name }}
|
93 | 118 | - proto: {{ proto }}
|
| 119 | + {%- if protocol == ipv6 %} |
| 120 | + - family: ipv6 |
| 121 | + {%- endif %} |
94 | 122 | - save: True
|
95 | 123 | {{ comment }}
|
| 124 | + {%- endfor %} |
96 | 125 | {%- endfor %}
|
97 |
| - {%- endfor %} |
98 |
| - {%- endif %} |
99 |
| - {%- endfor %} |
| 126 | + {%- endif %} |
| 127 | + {%- endfor %} |
100 | 128 |
|
101 |
| - {%- if not strict_mode and global_block_nomatch or block_nomatch %} |
102 |
| - # If strict mode is disabled we may want to block anything else |
103 |
| - {%- if interfaces == '' %} |
104 |
| - {%- for proto in protos %} |
105 |
| - iptables_{{service_name}}_deny_other_{{proto}}: |
| 129 | + {%- if not strict_mode and global_block_nomatch or block_nomatch %} |
| 130 | + # If strict mode is disabled we may want to block anything else |
| 131 | + {%- if interfaces == '' %} |
| 132 | + {%- for proto in protos %} |
| 133 | + iptables_{{service_name}}_deny_other_{{proto}}{{sufixes[protocol]}}: |
106 | 134 | iptables.append:
|
107 | 135 | - position: last
|
108 | 136 | - table: filter
|
109 | 137 | - chain: INPUT
|
110 | 138 | - jump: REJECT
|
111 | 139 | - dport: {{ service_name }}
|
112 | 140 | - proto: {{ proto }}
|
| 141 | + {%- if protocol == ipv6 %} |
| 142 | + - family: ipv6 |
| 143 | + {%- endif %} |
113 | 144 | - save: True
|
114 | 145 | {{ comment }}
|
115 |
| - {%- endfor %} |
116 |
| - {%- else %} |
117 |
| - {%- for interface in interfaces %} |
118 |
| - {%- for proto in protos %} |
119 |
| - iptables_{{service_name}}_deny_other_{{proto}}_{{interface}}: |
| 146 | + {%- endfor %} |
| 147 | + {%- else %} |
| 148 | + {%- for interface in interfaces %} |
| 149 | + {%- for proto in protos %} |
| 150 | + iptables_{{service_name}}_deny_other_{{proto}}_{{interface}}{{sufixes[protocol]}}: |
120 | 151 | iptables.append:
|
121 | 152 | - position: last
|
122 | 153 | - table: filter
|
|
125 | 156 | - i: {{ interface }}
|
126 | 157 | - dport: {{ service_name }}
|
127 | 158 | - proto: {{ proto }}
|
| 159 | + {%- if protocol == ipv6 %} |
| 160 | + - family: ipv6 |
| 161 | + {%- endif %} |
128 | 162 | - save: True
|
129 | 163 | {{ comment }}
|
| 164 | + {%- endfor %} |
130 | 165 | {%- endfor %}
|
131 |
| - {%- endfor %} |
132 |
| - {%- endif %} |
| 166 | + {%- endif %} |
133 | 167 |
|
134 |
| - {%- endif %} |
| 168 | + {%- endif %} |
135 | 169 |
|
| 170 | + {%- endfor %} |
136 | 171 | {%- endfor %}
|
137 | 172 |
|
138 | 173 | # Generate rules for NAT
|
|
153 | 188 | {%- endfor %}
|
154 | 189 |
|
155 | 190 | # Generate rules for whitelisting IP classes
|
156 |
| - {%- for service_name, service_details in firewall.get('whitelist', {}).items() %} |
157 |
| - {%- for ip in service_details.get('ips_allow', []) %} |
158 |
| - iptables_{{service_name}}_allow_{{ip}}: |
159 |
| - iptables.append: |
160 |
| - - table: filter |
161 |
| - - chain: INPUT |
162 |
| - - jump: ACCEPT |
163 |
| - - source: {{ ip }} |
164 |
| - - save: True |
| 191 | + {%- for protocol in protocols %} |
| 192 | + {%- for service_name, service_details in firewall.get('whitelist', {}).items() %} |
| 193 | + {%- for ip in service_details.get('ips_allow', []) %} |
| 194 | + iptables_{{service_name}}_allow_{{ip}}{{sufixes[protocol]}}: |
| 195 | + iptables.append: |
| 196 | + - table: filter |
| 197 | + - chain: INPUT |
| 198 | + - jump: ACCEPT |
| 199 | + - source: {{ ip }} |
| 200 | + {%- if protocol == ipv6 %} |
| 201 | + - family: ipv6 |
| 202 | + {%- endif %} |
| 203 | + - save: True |
| 204 | + {%- endfor %} |
165 | 205 | {%- endfor %}
|
166 | 206 | {%- endfor %}
|
167 | 207 |
|
|
0 commit comments