]>
Commit | Line | Data |
---|---|---|
b6360c3f DM |
1 | package PVE::Firewall; |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | use Data::Dumper; | |
09d5f68e | 6 | use Digest::SHA; |
f789653a | 7 | use PVE::Tools; |
b6360c3f | 8 | use PVE::QemuServer; |
5e1267a5 DM |
9 | use File::Path; |
10 | use IO::File; | |
ecbea048 | 11 | use Net::IP; |
06320eb0 | 12 | use PVE::Tools qw(run_command lock_file); |
ecbea048 | 13 | |
5e1267a5 | 14 | use Data::Dumper; |
b6360c3f | 15 | |
06320eb0 DM |
16 | my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; |
17 | ||
857f62c8 | 18 | # imported/converted from: /usr/share/shorewall/macro.* |
961b4928 | 19 | my $pve_fw_macros = { |
857f62c8 DM |
20 | 'Amanda' => [ |
21 | { action => 'PARAM', proto => 'udp', dport => '10080' }, | |
22 | { action => 'PARAM', proto => 'tcp', dport => '10080' }, | |
23 | ], | |
24 | 'Auth' => [ | |
25 | { action => 'PARAM', proto => 'tcp', dport => '113' }, | |
26 | ], | |
27 | 'BGP' => [ | |
28 | { action => 'PARAM', proto => 'tcp', dport => '179' }, | |
29 | ], | |
30 | 'BitTorrent' => [ | |
961b4928 | 31 | { action => 'PARAM', proto => 'tcp', dport => '6881:6889' }, |
857f62c8 DM |
32 | { action => 'PARAM', proto => 'udp', dport => '6881' }, |
33 | ], | |
34 | 'BitTorrent32' => [ | |
35 | { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, | |
36 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
37 | ], | |
38 | 'CVS' => [ | |
39 | { action => 'PARAM', proto => 'tcp', dport => '2401' }, | |
40 | ], | |
41 | 'Citrix' => [ | |
42 | { action => 'PARAM', proto => 'tcp', dport => '1494' }, | |
43 | { action => 'PARAM', proto => 'udp', dport => '1604' }, | |
44 | { action => 'PARAM', proto => 'tcp', dport => '2598' }, | |
45 | ], | |
46 | 'DAAP' => [ | |
47 | { action => 'PARAM', proto => 'tcp', dport => '3689' }, | |
48 | { action => 'PARAM', proto => 'udp', dport => '3689' }, | |
49 | ], | |
50 | 'DCC' => [ | |
51 | { action => 'PARAM', proto => 'tcp', dport => '6277' }, | |
52 | ], | |
53 | 'DHCPfwd' => [ | |
54 | { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
55 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
56 | ], | |
57 | 'DNS' => [ | |
58 | { action => 'PARAM', proto => 'udp', dport => '53' }, | |
59 | { action => 'PARAM', proto => 'tcp', dport => '53' }, | |
60 | ], | |
61 | 'Distcc' => [ | |
62 | { action => 'PARAM', proto => 'tcp', dport => '3632' }, | |
63 | ], | |
64 | 'Edonkey' => [ | |
65 | { action => 'PARAM', proto => 'tcp', dport => '4662' }, | |
66 | { action => 'PARAM', proto => 'udp', dport => '4665' }, | |
961b4928 | 67 | ], |
857f62c8 DM |
68 | 'FTP' => [ |
69 | { action => 'PARAM', proto => 'tcp', dport => '21' }, | |
70 | ], | |
71 | 'Finger' => [ | |
72 | { action => 'PARAM', proto => 'tcp', dport => '79' }, | |
73 | ], | |
74 | 'GNUnet' => [ | |
75 | { action => 'PARAM', proto => 'tcp', dport => '2086' }, | |
76 | { action => 'PARAM', proto => 'udp', dport => '2086' }, | |
77 | { action => 'PARAM', proto => 'tcp', dport => '1080' }, | |
78 | { action => 'PARAM', proto => 'udp', dport => '1080' }, | |
79 | ], | |
80 | 'GRE' => [ | |
81 | { action => 'PARAM', proto => '47' }, | |
82 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
83 | ], | |
84 | 'Git' => [ | |
85 | { action => 'PARAM', proto => 'tcp', dport => '9418' }, | |
86 | ], | |
87 | 'Gnutella' => [ | |
88 | { action => 'PARAM', proto => 'tcp', dport => '6346' }, | |
89 | { action => 'PARAM', proto => 'udp', dport => '6346' }, | |
90 | ], | |
91 | 'HKP' => [ | |
92 | { action => 'PARAM', proto => 'tcp', dport => '11371' }, | |
93 | ], | |
94 | 'HTTP' => [ | |
961b4928 DM |
95 | { action => 'PARAM', proto => 'tcp', dport => '80' }, |
96 | ], | |
857f62c8 DM |
97 | 'HTTPS' => [ |
98 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
99 | ], | |
100 | 'ICPV2' => [ | |
101 | { action => 'PARAM', proto => 'udp', dport => '3130' }, | |
102 | ], | |
103 | 'ICQ' => [ | |
104 | { action => 'PARAM', proto => 'tcp', dport => '5190' }, | |
105 | ], | |
106 | 'IMAP' => [ | |
107 | { action => 'PARAM', proto => 'tcp', dport => '143' }, | |
108 | ], | |
109 | 'IMAPS' => [ | |
110 | { action => 'PARAM', proto => 'tcp', dport => '993' }, | |
111 | ], | |
112 | 'IPIP' => [ | |
113 | { action => 'PARAM', proto => '94' }, | |
114 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' }, | |
115 | ], | |
116 | 'IPP' => [ | |
117 | { action => 'PARAM', proto => 'tcp', dport => '631' }, | |
118 | ], | |
119 | 'IPPbrd' => [ | |
120 | { action => 'PARAM', proto => 'udp', dport => '631' }, | |
121 | ], | |
122 | 'IPPserver' => [ | |
123 | { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' }, | |
124 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' }, | |
125 | ], | |
126 | 'IPsec' => [ | |
127 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
128 | { action => 'PARAM', proto => '50' }, | |
129 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
130 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
131 | ], | |
132 | 'IPsecah' => [ | |
133 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
134 | { action => 'PARAM', proto => '51' }, | |
135 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
136 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' }, | |
137 | ], | |
138 | 'IPsecnat' => [ | |
139 | { action => 'PARAM', proto => 'udp', dport => '500' }, | |
140 | { action => 'PARAM', proto => 'udp', dport => '4500' }, | |
141 | { action => 'PARAM', proto => '50' }, | |
142 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' }, | |
143 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' }, | |
144 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
145 | ], | |
146 | 'IRC' => [ | |
147 | { action => 'PARAM', proto => 'tcp', dport => '6667' }, | |
148 | ], | |
149 | 'JabberPlain' => [ | |
150 | { action => 'PARAM', proto => 'tcp', dport => '5222' }, | |
151 | ], | |
152 | 'JabberSecure' => [ | |
153 | { action => 'PARAM', proto => 'tcp', dport => '5223' }, | |
154 | ], | |
155 | 'Jabberd' => [ | |
156 | { action => 'PARAM', proto => 'tcp', dport => '5269' }, | |
157 | ], | |
158 | 'Jetdirect' => [ | |
159 | { action => 'PARAM', proto => 'tcp', dport => '9100' }, | |
160 | ], | |
161 | 'L2TP' => [ | |
162 | { action => 'PARAM', proto => 'udp', dport => '1701' }, | |
163 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' }, | |
164 | ], | |
165 | 'LDAP' => [ | |
166 | { action => 'PARAM', proto => 'tcp', dport => '389' }, | |
167 | ], | |
168 | 'LDAPS' => [ | |
169 | { action => 'PARAM', proto => 'tcp', dport => '636' }, | |
170 | ], | |
171 | 'MSNP' => [ | |
172 | { action => 'PARAM', proto => 'tcp', dport => '1863' }, | |
173 | ], | |
174 | 'MSSQL' => [ | |
175 | { action => 'PARAM', proto => 'tcp', dport => '1433' }, | |
176 | ], | |
177 | 'Mail' => [ | |
178 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
179 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
180 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
181 | ], | |
182 | 'Munin' => [ | |
183 | { action => 'PARAM', proto => 'tcp', dport => '4949' }, | |
184 | ], | |
185 | 'MySQL' => [ | |
186 | { action => 'PARAM', proto => 'tcp', dport => '3306' }, | |
187 | ], | |
188 | 'NNTP' => [ | |
189 | { action => 'PARAM', proto => 'tcp', dport => '119' }, | |
190 | ], | |
191 | 'NNTPS' => [ | |
192 | { action => 'PARAM', proto => 'tcp', dport => '563' }, | |
193 | ], | |
194 | 'NTP' => [ | |
195 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
196 | ], | |
197 | 'NTPbi' => [ | |
198 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
199 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' }, | |
200 | ], | |
201 | 'NTPbrd' => [ | |
202 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
203 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' }, | |
204 | ], | |
205 | 'OSPF' => [ | |
206 | { action => 'PARAM', proto => '89' }, | |
207 | ], | |
208 | 'OpenVPN' => [ | |
209 | { action => 'PARAM', proto => 'udp', dport => '1194' }, | |
210 | ], | |
211 | 'PCA' => [ | |
212 | { action => 'PARAM', proto => 'udp', dport => '5632' }, | |
213 | { action => 'PARAM', proto => 'tcp', dport => '5631' }, | |
214 | ], | |
215 | 'POP3' => [ | |
216 | { action => 'PARAM', proto => 'tcp', dport => '110' }, | |
217 | ], | |
218 | 'POP3S' => [ | |
219 | { action => 'PARAM', proto => 'tcp', dport => '995' }, | |
220 | ], | |
221 | 'PPtP' => [ | |
222 | { action => 'PARAM', proto => '47' }, | |
223 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
224 | { action => 'PARAM', proto => 'tcp', dport => '1723' }, | |
225 | ], | |
226 | 'Ping' => [ | |
227 | { action => 'PARAM', proto => 'icmp', dport => '8' }, | |
228 | ], | |
229 | 'PostgreSQL' => [ | |
230 | { action => 'PARAM', proto => 'tcp', dport => '5432' }, | |
231 | ], | |
232 | 'Printer' => [ | |
233 | { action => 'PARAM', proto => 'tcp', dport => '515' }, | |
234 | ], | |
235 | 'RDP' => [ | |
236 | { action => 'PARAM', proto => 'tcp', dport => '3389' }, | |
237 | ], | |
238 | 'RIPbi' => [ | |
239 | { action => 'PARAM', proto => 'udp', dport => '520' }, | |
240 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' }, | |
241 | ], | |
242 | 'RNDC' => [ | |
243 | { action => 'PARAM', proto => 'tcp', dport => '953' }, | |
244 | ], | |
245 | 'Razor' => [ | |
246 | { action => 'ACCEPT', proto => 'tcp', dport => '2703' }, | |
247 | ], | |
248 | 'Rdate' => [ | |
249 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
250 | ], | |
251 | 'Rsync' => [ | |
252 | { action => 'PARAM', proto => 'tcp', dport => '873' }, | |
253 | ], | |
254 | 'SANE' => [ | |
255 | { action => 'PARAM', proto => 'tcp', dport => '6566' }, | |
256 | ], | |
257 | 'SMB' => [ | |
258 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
259 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
260 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
261 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
262 | ], | |
263 | 'SMBBI' => [ | |
264 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
265 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
266 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
267 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
268 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' }, | |
269 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' }, | |
270 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
271 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' }, | |
272 | ], | |
273 | 'SMBswat' => [ | |
274 | { action => 'PARAM', proto => 'tcp', dport => '901' }, | |
275 | ], | |
276 | 'SMTP' => [ | |
277 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
278 | ], | |
279 | 'SMTPS' => [ | |
280 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
281 | ], | |
282 | 'SNMP' => [ | |
283 | { action => 'PARAM', proto => 'udp', dport => '161:162' }, | |
284 | { action => 'PARAM', proto => 'tcp', dport => '161' }, | |
285 | ], | |
286 | 'SPAMD' => [ | |
287 | { action => 'PARAM', proto => 'tcp', dport => '783' }, | |
288 | ], | |
289 | 'SSH' => [ | |
290 | { action => 'PARAM', proto => 'tcp', dport => '22' }, | |
291 | ], | |
292 | 'SVN' => [ | |
293 | { action => 'PARAM', proto => 'tcp', dport => '3690' }, | |
294 | ], | |
295 | 'SixXS' => [ | |
296 | { action => 'PARAM', proto => 'tcp', dport => '3874' }, | |
297 | { action => 'PARAM', proto => 'udp', dport => '3740' }, | |
298 | { action => 'PARAM', proto => '41' }, | |
299 | { action => 'PARAM', proto => 'udp', dport => '5072,8374' }, | |
300 | ], | |
301 | 'Squid' => [ | |
302 | { action => 'PARAM', proto => 'tcp', dport => '3128' }, | |
303 | ], | |
304 | 'Submission' => [ | |
305 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
306 | ], | |
307 | 'Syslog' => [ | |
308 | { action => 'PARAM', proto => 'udp', dport => '514' }, | |
309 | { action => 'PARAM', proto => 'tcp', dport => '514' }, | |
310 | ], | |
311 | 'TFTP' => [ | |
312 | { action => 'PARAM', proto => 'udp', dport => '69' }, | |
313 | ], | |
314 | 'Telnet' => [ | |
315 | { action => 'PARAM', proto => 'tcp', dport => '23' }, | |
316 | ], | |
317 | 'Telnets' => [ | |
318 | { action => 'PARAM', proto => 'tcp', dport => '992' }, | |
319 | ], | |
320 | 'Time' => [ | |
321 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
322 | ], | |
323 | 'Trcrt' => [ | |
324 | { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, | |
325 | { action => 'PARAM', proto => 'icmp', dport => '8' }, | |
326 | ], | |
327 | 'VNC' => [ | |
328 | { action => 'PARAM', proto => 'tcp', dport => '5900:5909' }, | |
329 | ], | |
330 | 'VNCL' => [ | |
331 | { action => 'PARAM', proto => 'tcp', dport => '5500' }, | |
332 | ], | |
333 | 'Web' => [ | |
334 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
961b4928 DM |
335 | { action => 'PARAM', proto => 'tcp', dport => '443' }, |
336 | ], | |
857f62c8 DM |
337 | 'Webcache' => [ |
338 | { action => 'PARAM', proto => 'tcp', dport => '8080' }, | |
339 | ], | |
340 | 'Webmin' => [ | |
341 | { action => 'PARAM', proto => 'tcp', dport => '10000' }, | |
342 | ], | |
343 | 'Whois' => [ | |
344 | { action => 'PARAM', proto => 'tcp', dport => '43' }, | |
345 | ], | |
961b4928 DM |
346 | }; |
347 | ||
348 | my $pve_fw_parsed_macros; | |
349 | my $pve_fw_preferred_macro_names = {}; | |
3a616aa0 | 350 | |
d8f2505e DM |
351 | my $pve_std_chains = { |
352 | 'PVEFW-SET-ACCEPT-MARK' => [ | |
353 | "-j MARK --set-mark 1", | |
354 | ], | |
79929d9c DM |
355 | 'PVEFW-DropBroadcast' => [ |
356 | # same as shorewall 'Broadcast' | |
357 | # simply DROP BROADCAST/MULTICAST/ANYCAST | |
358 | # we can use this to reduce logging | |
359 | { action => 'DROP', dsttype => 'BROADCAST' }, | |
360 | { action => 'DROP', dsttype => 'MULTICAST' }, | |
361 | { action => 'DROP', dsttype => 'ANYCAST' }, | |
362 | { action => 'DROP', dest => '224.0.0.0/4' }, | |
363 | ], | |
364 | 'PVEFW-reject' => [ | |
365 | # same as shorewall 'reject' | |
366 | { action => 'DROP', dsttype => 'BROADCAST' }, | |
367 | { action => 'DROP', source => '224.0.0.0/4' }, | |
368 | { action => 'DROP', proto => 'icmp' }, | |
369 | "-p tcp -j REJECT --reject-with tcp-reset", | |
370 | "-p udp -j REJECT --reject-with icmp-port-unreachable", | |
371 | "-p icmp -j REJECT --reject-with icmp-host-unreachable", | |
372 | "-j REJECT --reject-with icmp-host-prohibited", | |
373 | ], | |
374 | 'PVEFW-Drop' => [ | |
375 | # same as shorewall 'Drop', which is equal to DROP, | |
376 | # but REJECT/DROP some packages to reduce logging, | |
377 | # and ACCEPT critical ICMP types | |
378 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
379 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
380 | { action => 'PVEFW-DropBroadcast' }, | |
381 | # ACCEPT critical ICMP types | |
382 | { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, | |
383 | { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, | |
384 | # Drop packets with INVALID state | |
385 | "-m conntrack --ctstate INVALID -j DROP", | |
386 | # Drop Microsoft SMB noise | |
387 | { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
388 | { action => 'DROP', proto => 'udp', dport => '137:139'}, | |
389 | { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
390 | { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
391 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
392 | # Drop new/NotSyn traffic so that it doesn't get logged | |
393 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
394 | # Drop DNS replies | |
395 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
396 | ], | |
397 | 'PVEFW-Reject' => [ | |
398 | # same as shorewall 'Reject', which is equal to Reject, | |
399 | # but REJECT/DROP some packages to reduce logging, | |
400 | # and ACCEPT critical ICMP types | |
401 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
402 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
403 | { action => 'PVEFW-DropBroadcast' }, | |
404 | # ACCEPT critical ICMP types | |
405 | { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, | |
406 | { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, | |
407 | # Drop packets with INVALID state | |
408 | "-m conntrack --ctstate INVALID -j DROP", | |
409 | # Drop Microsoft SMB noise | |
410 | { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
411 | { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, | |
412 | { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
413 | { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
414 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
415 | # Drop new/NotSyn traffic so that it doesn't get logged | |
416 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
417 | # Drop DNS replies | |
418 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
419 | ], | |
d8f2505e DM |
420 | }; |
421 | ||
4b586518 DM |
422 | # iptables -p icmp -h |
423 | my $icmp_type_names = { | |
424 | any => 1, | |
425 | 'echo-reply' => 1, | |
426 | 'destination-unreachable' => 1, | |
427 | 'network-unreachable' => 1, | |
428 | 'host-unreachable' => 1, | |
429 | 'protocol-unreachable' => 1, | |
430 | 'port-unreachable' => 1, | |
431 | 'fragmentation-needed' => 1, | |
432 | 'source-route-failed' => 1, | |
433 | 'network-unknown' => 1, | |
434 | 'host-unknown' => 1, | |
435 | 'network-prohibited' => 1, | |
436 | 'host-prohibited' => 1, | |
437 | 'TOS-network-unreachable' => 1, | |
438 | 'TOS-host-unreachable' => 1, | |
439 | 'communication-prohibited' => 1, | |
440 | 'host-precedence-violation' => 1, | |
441 | 'precedence-cutoff' => 1, | |
442 | 'source-quench' => 1, | |
443 | 'redirect' => 1, | |
444 | 'network-redirect' => 1, | |
445 | 'host-redirect' => 1, | |
446 | 'TOS-network-redirect' => 1, | |
447 | 'TOS-host-redirect' => 1, | |
448 | 'echo-request' => 1, | |
449 | 'router-advertisement' => 1, | |
450 | 'router-solicitation' => 1, | |
451 | 'time-exceeded' => 1, | |
452 | 'ttl-zero-during-transit' => 1, | |
453 | 'ttl-zero-during-reassembly' => 1, | |
454 | 'parameter-problem' => 1, | |
455 | 'ip-header-bad' => 1, | |
456 | 'required-option-missing' => 1, | |
457 | 'timestamp-request' => 1, | |
458 | 'timestamp-reply' => 1, | |
459 | 'address-mask-request' => 1, | |
460 | 'address-mask-reply' => 1, | |
461 | }; | |
462 | ||
a332200b | 463 | sub get_firewall_macros { |
9aab3127 | 464 | |
961b4928 | 465 | return $pve_fw_parsed_macros if $pve_fw_parsed_macros; |
6158271d | 466 | |
961b4928 | 467 | $pve_fw_parsed_macros = {}; |
9aab3127 | 468 | |
961b4928 DM |
469 | foreach my $k (keys %$pve_fw_macros) { |
470 | my $name = lc($k); | |
a332200b | 471 | |
961b4928 DM |
472 | my $macro = $pve_fw_macros->{$k}; |
473 | $pve_fw_preferred_macro_names->{$name} = $k; | |
474 | $pve_fw_parsed_macros->{$name} = $macro; | |
475 | } | |
a332200b | 476 | |
961b4928 | 477 | return $pve_fw_parsed_macros; |
9aab3127 DM |
478 | } |
479 | ||
fcba0beb DM |
480 | my $etc_services; |
481 | ||
482 | sub get_etc_services { | |
483 | ||
484 | return $etc_services if $etc_services; | |
485 | ||
486 | my $filename = "/etc/services"; | |
487 | ||
488 | my $fh = IO::File->new($filename, O_RDONLY); | |
489 | if (!$fh) { | |
490 | warn "unable to read '$filename' - $!\n"; | |
491 | return {}; | |
492 | } | |
493 | ||
494 | my $services = {}; | |
495 | ||
496 | while (my $line = <$fh>) { | |
497 | chomp ($line); | |
498 | next if $line =~m/^#/; | |
499 | next if ($line =~m/^\s*$/); | |
500 | ||
501 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
502 | $services->{byid}->{$2}->{name} = $1; | |
35b66e9d | 503 | $services->{byid}->{$2}->{port} = $2; |
fcba0beb DM |
504 | $services->{byid}->{$2}->{$3} = 1; |
505 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
506 | } | |
507 | } | |
508 | ||
509 | close($fh); | |
510 | ||
6158271d DM |
511 | $etc_services = $services; |
512 | ||
3a616aa0 | 513 | |
fcba0beb DM |
514 | return $etc_services; |
515 | } | |
516 | ||
517 | my $etc_protocols; | |
518 | ||
519 | sub get_etc_protocols { | |
520 | return $etc_protocols if $etc_protocols; | |
521 | ||
522 | my $filename = "/etc/protocols"; | |
523 | ||
524 | my $fh = IO::File->new($filename, O_RDONLY); | |
525 | if (!$fh) { | |
526 | warn "unable to read '$filename' - $!\n"; | |
527 | return {}; | |
528 | } | |
529 | ||
530 | my $protocols = {}; | |
531 | ||
532 | while (my $line = <$fh>) { | |
533 | chomp ($line); | |
534 | next if $line =~m/^#/; | |
535 | next if ($line =~m/^\s*$/); | |
536 | ||
537 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
538 | $protocols->{byid}->{$2}->{name} = $1; | |
539 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
540 | } | |
541 | } | |
542 | ||
543 | close($fh); | |
544 | ||
545 | $etc_protocols = $protocols; | |
546 | ||
547 | return $etc_protocols; | |
548 | } | |
549 | ||
ecbea048 DM |
550 | sub parse_address_list { |
551 | my ($str) = @_; | |
552 | ||
d6de1dc2 | 553 | my $nbaor = 0; |
ecbea048 DM |
554 | foreach my $aor (split(/,/, $str)) { |
555 | if (!Net::IP->new($aor)) { | |
556 | my $err = Net::IP::Error(); | |
557 | die "invalid IP address: $err\n"; | |
d6de1dc2 AD |
558 | }else{ |
559 | $nbaor++; | |
ecbea048 DM |
560 | } |
561 | } | |
d6de1dc2 | 562 | return $nbaor; |
ecbea048 | 563 | } |
dddd9413 | 564 | |
fcba0beb DM |
565 | sub parse_port_name_number_or_range { |
566 | my ($str) = @_; | |
567 | ||
568 | my $services = PVE::Firewall::get_etc_services(); | |
4cdbb3b7 | 569 | my $nbports = 0; |
fcba0beb | 570 | foreach my $item (split(/,/, $str)) { |
4cdbb3b7 | 571 | my $portlist = ""; |
35b66e9d | 572 | my $oldpon = undef; |
4b586518 | 573 | $nbports++; |
fcba0beb | 574 | foreach my $pon (split(':', $item, 2)) { |
35b66e9d | 575 | $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port}; |
4cdbb3b7 | 576 | if ($pon =~ m/^\d+$/){ |
857f62c8 | 577 | die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535; |
35b66e9d AD |
578 | die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon); |
579 | $oldpon = $pon; | |
4cdbb3b7 AD |
580 | }else{ |
581 | die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; | |
582 | } | |
fcba0beb DM |
583 | } |
584 | } | |
585 | ||
4cdbb3b7 | 586 | return ($nbports); |
fcba0beb DM |
587 | } |
588 | ||
780bcc0f DM |
589 | my $bridge_firewall_enabled = 0; |
590 | ||
591 | sub enable_bridge_firewall { | |
592 | ||
593 | return if $bridge_firewall_enabled; # only once | |
594 | ||
595 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables"); | |
596 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables"); | |
597 | ||
598 | $bridge_firewall_enabled = 1; | |
599 | } | |
600 | ||
8cebfa6f | 601 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; |
dddd9413 | 602 | |
3a616aa0 AD |
603 | sub iptables { |
604 | my ($cmd) = @_; | |
605 | ||
606 | run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); | |
607 | } | |
608 | ||
b16e818e DM |
609 | sub iptables_restore_cmdlist { |
610 | my ($cmdlist) = @_; | |
3a616aa0 | 611 | |
3fa83edf | 612 | run_command("/sbin/iptables-restore -n", input => $cmdlist); |
3a616aa0 AD |
613 | } |
614 | ||
de2a57cd DM |
615 | sub iptables_get_chains { |
616 | ||
617 | my $res = {}; | |
618 | ||
619 | # check what chains we want to track | |
620 | my $is_pvefw_chain = sub { | |
621 | my $name = shift; | |
622 | ||
dec84fcd DM |
623 | return 1 if $name =~ m/^PVEFW-\S+$/; |
624 | ||
de2a57cd | 625 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; |
5927497d | 626 | return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/; |
f50656c2 | 627 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; |
de2a57cd DM |
628 | |
629 | return undef; | |
630 | }; | |
631 | ||
632 | my $table = ''; | |
633 | ||
de2a57cd DM |
634 | my $parser = sub { |
635 | my $line = shift; | |
636 | ||
637 | return if $line =~ m/^#/; | |
638 | return if $line =~ m/^\s*$/; | |
639 | ||
640 | if ($line =~ m/^\*(\S+)$/) { | |
641 | $table = $1; | |
642 | return; | |
643 | } | |
644 | ||
645 | return if $table ne 'filter'; | |
646 | ||
647 | if ($line =~ m/^:(\S+)\s/) { | |
648 | my $chain = $1; | |
649 | return if !&$is_pvefw_chain($chain); | |
3fa83edf | 650 | $res->{$chain} = "unknown"; |
09d5f68e | 651 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { |
3fa83edf | 652 | my ($chain, $sig) = ($1, $2); |
de2a57cd | 653 | return if !&$is_pvefw_chain($chain); |
3fa83edf | 654 | $res->{$chain} = $sig; |
de2a57cd DM |
655 | } else { |
656 | # simply ignore the rest | |
657 | return; | |
658 | } | |
659 | }; | |
660 | ||
661 | run_command("/sbin/iptables-save", outfunc => $parser); | |
662 | ||
de2a57cd DM |
663 | return $res; |
664 | } | |
665 | ||
3a616aa0 AD |
666 | sub iptables_chain_exist { |
667 | my ($chain) = @_; | |
668 | ||
669 | eval{ | |
670 | iptables("-n --list $chain"); | |
671 | }; | |
672 | return undef if $@; | |
673 | ||
674 | return 1; | |
675 | } | |
676 | ||
677 | sub iptables_rule_exist { | |
678 | my ($rule) = @_; | |
679 | ||
680 | eval{ | |
681 | iptables("-C $rule"); | |
682 | }; | |
683 | return undef if $@; | |
684 | ||
685 | return 1; | |
686 | } | |
687 | ||
3fa83edf | 688 | sub ruleset_generate_rule { |
4e6112f9 | 689 | my ($ruleset, $chain, $rule, $goto) = @_; |
3a616aa0 | 690 | |
3fa83edf | 691 | my $cmd = ''; |
3a616aa0 | 692 | |
d6de1dc2 | 693 | $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1; |
3a616aa0 | 694 | $cmd .= " -s $rule->{source}" if $rule->{source}; |
d6de1dc2 | 695 | $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1; |
8c32a215 | 696 | $cmd .= " -d $rule->{dest}" if $rule->{dest}; |
3a616aa0 | 697 | $cmd .= " -p $rule->{proto}" if $rule->{proto}; |
4b586518 DM |
698 | |
699 | if (($rule->{nbdport} && $rule->{nbdport} > 1) || | |
700 | ($rule->{nbsport} && $rule->{nbsport} > 1)) { | |
701 | $cmd .= " --match multiport" | |
702 | } | |
703 | ||
704 | if ($rule->{dport}) { | |
705 | if ($rule->{proto} && $rule->{proto} eq 'icmp') { | |
706 | # Note: we use dport to store --icmp-type | |
707 | die "unknown icmp-type\n" if !$icmp_type_names->{$rule->{dport}}; | |
708 | $cmd .= " -m icmp --icmp-type $rule->{dport}"; | |
709 | } else { | |
710 | if ($rule->{nbdport} && $rule->{nbdport} > 1) { | |
711 | $cmd .= " --dports $rule->{dport}"; | |
712 | } else { | |
713 | $cmd .= " --dport $rule->{dport}"; | |
714 | } | |
715 | } | |
716 | } | |
717 | ||
718 | if ($rule->{sport}) { | |
719 | if ($rule->{nbsport} && $rule->{nbsport} > 1) { | |
720 | $cmd .= " --sports $rule->{sport}"; | |
721 | } else { | |
722 | $cmd .= " --sport $rule->{sport}"; | |
723 | } | |
724 | } | |
725 | ||
726 | $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; | |
4e6112f9 DM |
727 | |
728 | if (my $action = $rule->{action}) { | |
729 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
730 | $cmd .= $goto ? " -g $action" : " -j $action"; | |
731 | }; | |
3a616aa0 | 732 | |
3fa83edf DM |
733 | ruleset_addrule($ruleset, $chain, $cmd) if $cmd; |
734 | } | |
735 | ||
736 | sub ruleset_create_chain { | |
737 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 738 | |
d050c724 DM |
739 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; |
740 | ||
3fa83edf DM |
741 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; |
742 | ||
743 | $ruleset->{$chain} = []; | |
3a616aa0 AD |
744 | } |
745 | ||
3fa83edf DM |
746 | sub ruleset_chain_exist { |
747 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 748 | |
3fa83edf DM |
749 | return $ruleset->{$chain} ? 1 : undef; |
750 | } | |
3a616aa0 | 751 | |
3fa83edf DM |
752 | sub ruleset_addrule { |
753 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 754 | |
3fa83edf | 755 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 756 | |
3fa83edf DM |
757 | push @{$ruleset->{$chain}}, "-A $chain $rule"; |
758 | } | |
3a616aa0 | 759 | |
3fa83edf DM |
760 | sub ruleset_insertrule { |
761 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 762 | |
3fa83edf | 763 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 764 | |
3fa83edf DM |
765 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; |
766 | } | |
3a616aa0 | 767 | |
3fa83edf DM |
768 | sub generate_bridge_chains { |
769 | my ($ruleset, $bridge) = @_; | |
3a616aa0 | 770 | |
dec84fcd DM |
771 | if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){ |
772 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
7fab95bc | 773 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); |
3a616aa0 AD |
774 | } |
775 | ||
5927497d DM |
776 | if (!ruleset_chain_exist($ruleset, "$bridge-FW")) { |
777 | ruleset_create_chain($ruleset, "$bridge-FW"); | |
778 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW"); | |
779 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW"); | |
9fcad984 AD |
780 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing |
781 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing | |
3fa83edf | 782 | } |
3a616aa0 | 783 | |
3fa83edf DM |
784 | if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) { |
785 | ruleset_create_chain($ruleset, "$bridge-OUT"); | |
5927497d | 786 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT"); |
9fcad984 AD |
787 | } |
788 | ||
789 | if (!ruleset_chain_exist($ruleset, "$bridge-IN")) { | |
790 | ruleset_create_chain($ruleset, "$bridge-IN"); | |
5927497d | 791 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN"); |
fdb0bf20 | 792 | ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT"); |
3fa83edf DM |
793 | } |
794 | } | |
3a616aa0 AD |
795 | |
796 | sub generate_tap_rules_direction { | |
fa9c4a6f DM |
797 | my ($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_; |
798 | ||
799 | my $rules = $vmfw_conf->{lc($direction)}; | |
800 | my $options = $vmfw_conf->{options}; | |
3a616aa0 AD |
801 | |
802 | my $tapchain = "$iface-$direction"; | |
803 | ||
3fa83edf | 804 | ruleset_create_chain($ruleset, $tapchain); |
3a616aa0 | 805 | |
7fab95bc DM |
806 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP"); |
807 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
3a616aa0 | 808 | |
c29f55c9 DM |
809 | if ($direction eq 'OUT' && defined($macaddr)) { |
810 | ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP"); | |
811 | } | |
812 | ||
3fa83edf | 813 | if ($rules) { |
3a616aa0 AD |
814 | foreach my $rule (@$rules) { |
815 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
bfbfa8b6 | 816 | # we go to $bridge-IN if accept in out rules |
3a616aa0 | 817 | if($rule->{action} =~ m/^(GROUP-(\S+))$/){ |
3fa83edf DM |
818 | $rule->{action} .= "-$direction"; |
819 | # generate empty group rule if don't exist | |
820 | if(!ruleset_chain_exist($ruleset, $rule->{action})){ | |
51bae274 | 821 | generate_group_rules($ruleset, $group_rules, $2); |
3fa83edf | 822 | } |
4e6112f9 | 823 | ruleset_generate_rule($ruleset, $tapchain, $rule); |
7b291cab | 824 | ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -j RETURN") |
31ff3ef6 | 825 | if $direction eq 'OUT'; |
4e6112f9 | 826 | } else { |
fdb0bf20 | 827 | $rule->{action} = "PVEFW-SET-ACCEPT-MARK" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT'; |
4e6112f9 | 828 | ruleset_generate_rule($ruleset, $tapchain, $rule); |
3a616aa0 | 829 | } |
4e6112f9 | 830 | } |
3a616aa0 AD |
831 | } |
832 | ||
ccae0b50 DM |
833 | # implement policy |
834 | my $policy; | |
835 | ||
836 | if ($direction eq 'OUT') { | |
837 | $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default | |
838 | } else { | |
839 | $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default | |
840 | } | |
841 | ||
842 | if ($policy eq 'ACCEPT') { | |
843 | if ($direction eq 'OUT') { | |
fdb0bf20 | 844 | ruleset_addrule($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK"); |
ccae0b50 DM |
845 | } else { |
846 | ruleset_addrule($ruleset, $tapchain, "-j ACCEPT"); | |
847 | } | |
848 | } elsif ($policy eq 'DROP') { | |
25c7b224 | 849 | ruleset_addrule($ruleset, $tapchain, "-j PVEFW-Drop"); |
ccae0b50 DM |
850 | ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4"); |
851 | ruleset_addrule($ruleset, $tapchain, "-j DROP"); | |
852 | } elsif ($policy eq 'REJECT') { | |
25c7b224 | 853 | ruleset_addrule($ruleset, $tapchain, "-j PVEFW-Reject"); |
ccae0b50 | 854 | ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level 4"); |
25c7b224 | 855 | ruleset_addrule($ruleset, $tapchain, "-g PVEFW-reject"); |
ccae0b50 DM |
856 | } else { |
857 | # should not happen | |
858 | die "internal error: unknown policy '$policy'"; | |
859 | } | |
3a616aa0 | 860 | |
3fa83edf DM |
861 | # plug the tap chain to bridge chain |
862 | my $physdevdirection = $direction eq 'IN' ? "out" : "in"; | |
863 | my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain"; | |
864 | ruleset_insertrule($ruleset, "$bridge-$direction", $rule); | |
3a616aa0 | 865 | |
3fa83edf DM |
866 | if ($direction eq 'OUT'){ |
867 | # add tap->host rules | |
868 | my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain"; | |
dec84fcd | 869 | ruleset_addrule($ruleset, "PVEFW-INPUT", $rule); |
3a616aa0 AD |
870 | } |
871 | } | |
872 | ||
0bd5f137 | 873 | sub enablehostfw { |
51bae274 | 874 | my ($ruleset, $rules, $group_rules) = @_; |
0bd5f137 | 875 | |
51bae274 | 876 | # fixme: allow security groups |
0bd5f137 | 877 | |
3fa83edf | 878 | # host inbound firewall |
dec84fcd DM |
879 | my $chain = "PVEFW-HOST-IN"; |
880 | ruleset_create_chain($ruleset, $chain); | |
0bd5f137 | 881 | |
7fab95bc DM |
882 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
883 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
884 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); |
885 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 886 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 887 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 888 | |
3fa83edf DM |
889 | if ($rules->{in}) { |
890 | foreach my $rule (@{$rules->{in}}) { | |
891 | # we use RETURN because we need to check also tap rules | |
892 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 893 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 894 | } |
0bd5f137 AD |
895 | } |
896 | ||
dec84fcd DM |
897 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4"); |
898 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
0bd5f137 | 899 | |
3fa83edf | 900 | # host outbound firewall |
aadd745e | 901 | $chain = "PVEFW-HOST-OUT"; |
dec84fcd DM |
902 | ruleset_create_chain($ruleset, $chain); |
903 | ||
7fab95bc DM |
904 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
905 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
906 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); |
907 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 908 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 909 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 910 | |
3fa83edf DM |
911 | if ($rules->{out}) { |
912 | foreach my $rule (@{$rules->{out}}) { | |
913 | # we use RETURN because we need to check also tap rules | |
914 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 915 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 916 | } |
0bd5f137 AD |
917 | } |
918 | ||
dec84fcd DM |
919 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4"); |
920 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
6158271d | 921 | |
dec84fcd DM |
922 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); |
923 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
9d31b418 AD |
924 | } |
925 | ||
926 | sub generate_group_rules { | |
51bae274 | 927 | my ($ruleset, $group_rules, $group) = @_; |
9d31b418 | 928 | |
51bae274 | 929 | my $rules = $group_rules->{$group}; |
9d31b418 | 930 | |
51bae274 | 931 | die "no such security group '$group'\n" if !$rules; |
6158271d | 932 | |
3fa83edf | 933 | my $chain = "GROUP-${group}-IN"; |
9d31b418 | 934 | |
3fa83edf | 935 | ruleset_create_chain($ruleset, $chain); |
9d31b418 | 936 | |
3fa83edf DM |
937 | if ($rules->{in}) { |
938 | foreach my $rule (@{$rules->{in}}) { | |
939 | ruleset_generate_rule($ruleset, $chain, $rule); | |
9d31b418 AD |
940 | } |
941 | } | |
942 | ||
3fa83edf | 943 | $chain = "GROUP-${group}-OUT"; |
9d31b418 | 944 | |
3fa83edf | 945 | ruleset_create_chain($ruleset, $chain); |
4e6112f9 | 946 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark |
9d31b418 | 947 | |
3fa83edf DM |
948 | if ($rules->{out}) { |
949 | foreach my $rule (@{$rules->{out}}) { | |
6158271d DM |
950 | # we go the PVEFW-SET-ACCEPT-MARK Instead of ACCEPT) because we need to |
951 | # check also other tap rules (and group rules can be set on any bridge, | |
4e6112f9 DM |
952 | # so we can't go to VMBRXX-IN) |
953 | $rule->{action} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action} eq 'ACCEPT'; | |
6a6c7af7 | 954 | ruleset_generate_rule($ruleset, $chain, $rule); |
9d31b418 AD |
955 | } |
956 | } | |
9d31b418 AD |
957 | } |
958 | ||
51bae274 DM |
959 | my $MAX_NETS = 32; |
960 | my $valid_netdev_names = {}; | |
961 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
962 | $valid_netdev_names->{"net$i"} = 1; | |
963 | } | |
5e1267a5 | 964 | |
51bae274 DM |
965 | sub parse_fw_rule { |
966 | my ($line, $need_iface, $allow_groups) = @_; | |
5e1267a5 | 967 | |
a332200b | 968 | my $macros = get_firewall_macros(); |
fcba0beb | 969 | my $protocols = get_etc_protocols(); |
51bae274 DM |
970 | |
971 | my ($action, $iface, $source, $dest, $proto, $dport, $sport); | |
972 | ||
973 | $line =~ s/#.*$//; | |
974 | ||
975 | my @data = split(/\s+/, $line); | |
976 | my $expected_elements = $need_iface ? 7 : 6; | |
977 | ||
978 | die "wrong number of rule elements\n" if scalar(@data) > $expected_elements; | |
979 | ||
980 | if ($need_iface) { | |
981 | ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data | |
982 | } else { | |
983 | ($action, $source, $dest, $proto, $dport, $sport) = @data; | |
984 | } | |
985 | ||
986 | die "incomplete rule\n" if !$action; | |
987 | ||
961b4928 DM |
988 | my $macro; |
989 | my $macro_name; | |
990 | ||
51bae274 DM |
991 | if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) { |
992 | # OK | |
993 | } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) { | |
994 | # OK | |
995 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
961b4928 DM |
996 | ($macro_name, $action) = ($1, $2); |
997 | my $lc_macro_name = lc($macro_name); | |
998 | my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name}; | |
999 | $macro_name = $preferred_name if $preferred_name; | |
1000 | $macro = $macros->{$lc_macro_name}; | |
1001 | die "unknown macro '$macro_name'\n" if !$macro; | |
51bae274 DM |
1002 | } else { |
1003 | die "unknown action '$action'\n"; | |
1004 | } | |
1005 | ||
1006 | if ($need_iface) { | |
1007 | $iface = undef if $iface && $iface eq '-'; | |
6158271d | 1008 | die "unknown interface '$iface'\n" |
51bae274 DM |
1009 | if defined($iface) && !$valid_netdev_names->{$iface}; |
1010 | } | |
1011 | ||
1012 | $proto = undef if $proto && $proto eq '-'; | |
6158271d | 1013 | die "unknown protokol '$proto'\n" if $proto && |
51bae274 DM |
1014 | !(defined($protocols->{byname}->{$proto}) || |
1015 | defined($protocols->{byid}->{$proto})); | |
1016 | ||
1017 | $source = undef if $source && $source eq '-'; | |
1018 | $dest = undef if $dest && $dest eq '-'; | |
1019 | ||
1020 | $dport = undef if $dport && $dport eq '-'; | |
1021 | $sport = undef if $sport && $sport eq '-'; | |
1022 | ||
51bae274 DM |
1023 | my $nbsource = undef; |
1024 | my $nbdest = undef; | |
1025 | ||
1026 | $nbsource = parse_address_list($source) if $source; | |
1027 | $nbdest = parse_address_list($dest) if $dest; | |
6158271d | 1028 | |
961b4928 | 1029 | my $rules = []; |
51bae274 | 1030 | |
961b4928 | 1031 | my $param = { |
51bae274 | 1032 | action => $action, |
51bae274 DM |
1033 | iface => $iface, |
1034 | source => $source, | |
1035 | dest => $dest, | |
1036 | nbsource => $nbsource, | |
1037 | nbdest => $nbdest, | |
1038 | proto => $proto, | |
1039 | dport => $dport, | |
1040 | sport => $sport, | |
51bae274 | 1041 | }; |
961b4928 DM |
1042 | |
1043 | if ($macro) { | |
1044 | foreach my $templ (@$macro) { | |
1045 | my $rule = {}; | |
1046 | foreach my $k (keys %$templ) { | |
1047 | my $v = $templ->{$k}; | |
857f62c8 DM |
1048 | if ($v eq 'PARAM') { |
1049 | $v = $param->{$k}; | |
1050 | } elsif ($v eq 'DEST') { | |
1051 | $v = $param->{dest}; | |
1052 | } elsif ($v eq 'SOURCE') { | |
1053 | $v = $param->{source}; | |
1054 | } | |
1055 | ||
961b4928 DM |
1056 | die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v); |
1057 | $rule->{$k} = $v; | |
1058 | } | |
1059 | push @$rules, $rule; | |
1060 | } | |
1061 | } else { | |
1062 | push @$rules, $param; | |
1063 | } | |
1064 | ||
1065 | foreach my $rule (@$rules) { | |
6158271d | 1066 | $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport}) |
961b4928 | 1067 | if defined($rule->{dport}); |
6158271d | 1068 | $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport}) |
961b4928 DM |
1069 | if defined($rule->{sport}); |
1070 | } | |
1071 | ||
1072 | return $rules; | |
51bae274 DM |
1073 | } |
1074 | ||
85c6eaed DM |
1075 | sub parse_fw_option { |
1076 | my ($line) = @_; | |
1077 | ||
1078 | my ($opt, $value); | |
1079 | ||
1080 | if ($line =~ m/^enable:\s*(0|1)\s*$/i) { | |
1081 | $opt = 'enable'; | |
1082 | $value = int($1); | |
1083 | } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
1084 | $opt = lc($1); | |
1085 | $value = uc($3); | |
1086 | } else { | |
1087 | chomp $line; | |
1088 | die "can't parse option '$line'\n" | |
1089 | } | |
1090 | ||
1091 | return ($opt, $value); | |
1092 | } | |
1093 | ||
51bae274 DM |
1094 | sub parse_vm_fw_rules { |
1095 | my ($filename, $fh) = @_; | |
1096 | ||
85c6eaed | 1097 | my $res = { in => [], out => [], options => {}}; |
51bae274 DM |
1098 | |
1099 | my $section; | |
1100 | ||
5e1267a5 DM |
1101 | while (defined(my $line = <$fh>)) { |
1102 | next if $line =~ m/^#/; | |
1103 | next if $line =~ m/^\s*$/; | |
1104 | ||
961b4928 DM |
1105 | my $linenr = $fh->input_line_number(); |
1106 | my $prefix = "$filename (line $linenr)"; | |
1107 | ||
85c6eaed | 1108 | if ($line =~ m/^\[(\S+)\]\s*$/i) { |
5e1267a5 | 1109 | $section = lc($1); |
85c6eaed | 1110 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; |
5e1267a5 DM |
1111 | next; |
1112 | } | |
51bae274 | 1113 | if (!$section) { |
961b4928 | 1114 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
1115 | next; |
1116 | } | |
1117 | ||
85c6eaed DM |
1118 | next if !$res->{$section}; # skip undefined section |
1119 | ||
1120 | if ($section eq 'options') { | |
6158271d DM |
1121 | eval { |
1122 | my ($opt, $value) = parse_fw_option($line); | |
85c6eaed DM |
1123 | $res->{options}->{$opt} = $value; |
1124 | }; | |
1125 | warn "$prefix: $@" if $@; | |
1126 | next; | |
1127 | } | |
1128 | ||
961b4928 DM |
1129 | my $rules; |
1130 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
51bae274 | 1131 | if (my $err = $@) { |
961b4928 | 1132 | warn "$prefix: $err"; |
5e1267a5 DM |
1133 | next; |
1134 | } | |
1135 | ||
961b4928 | 1136 | push @{$res->{$section}}, @$rules; |
51bae274 DM |
1137 | } |
1138 | ||
1139 | return $res; | |
1140 | } | |
1141 | ||
1142 | sub parse_host_fw_rules { | |
1143 | my ($filename, $fh) = @_; | |
1144 | ||
1145 | my $res = { in => [], out => [] }; | |
1146 | ||
1147 | my $section; | |
1148 | ||
1149 | while (defined(my $line = <$fh>)) { | |
1150 | next if $line =~ m/^#/; | |
1151 | next if $line =~ m/^\s*$/; | |
1152 | ||
961b4928 DM |
1153 | my $linenr = $fh->input_line_number(); |
1154 | my $prefix = "$filename (line $linenr)"; | |
1155 | ||
51bae274 DM |
1156 | if ($line =~ m/^\[(in|out)\]\s*$/i) { |
1157 | $section = lc($1); | |
5e1267a5 DM |
1158 | next; |
1159 | } | |
51bae274 | 1160 | if (!$section) { |
961b4928 | 1161 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
1162 | next; |
1163 | } | |
1164 | ||
961b4928 DM |
1165 | my $rules; |
1166 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
ecbea048 | 1167 | if (my $err = $@) { |
961b4928 | 1168 | warn "$prefix: $err"; |
ecbea048 | 1169 | next; |
ecbea048 DM |
1170 | } |
1171 | ||
961b4928 | 1172 | push @{$res->{$section}}, @$rules; |
51bae274 | 1173 | } |
ecbea048 | 1174 | |
51bae274 DM |
1175 | return $res; |
1176 | } | |
4cdbb3b7 | 1177 | |
51bae274 DM |
1178 | sub parse_group_fw_rules { |
1179 | my ($filename, $fh) = @_; | |
5e1267a5 | 1180 | |
51bae274 DM |
1181 | my $section; |
1182 | my $group; | |
1183 | ||
1184 | my $res = { in => [], out => [] }; | |
6158271d | 1185 | |
51bae274 DM |
1186 | while (defined(my $line = <$fh>)) { |
1187 | next if $line =~ m/^#/; | |
1188 | next if $line =~ m/^\s*$/; | |
1189 | ||
961b4928 DM |
1190 | my $linenr = $fh->input_line_number(); |
1191 | my $prefix = "$filename (line $linenr)"; | |
1192 | ||
51bae274 DM |
1193 | if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) { |
1194 | $section = lc($1); | |
1195 | $group = lc($2); | |
1196 | next; | |
1197 | } | |
1198 | if (!$section || !$group) { | |
961b4928 | 1199 | warn "$prefix: skip line - no section"; |
51bae274 DM |
1200 | next; |
1201 | } | |
1202 | ||
961b4928 DM |
1203 | my $rules; |
1204 | eval { $rules = parse_fw_rule($line, 0, 0); }; | |
51bae274 | 1205 | if (my $err = $@) { |
961b4928 | 1206 | warn "$prefix: $err"; |
51bae274 DM |
1207 | next; |
1208 | } | |
1209 | ||
961b4928 | 1210 | push @{$res->{$group}->{$section}}, @$rules; |
5e1267a5 DM |
1211 | } |
1212 | ||
1213 | return $res; | |
1214 | } | |
1215 | ||
06320eb0 DM |
1216 | sub run_locked { |
1217 | my ($code, @param) = @_; | |
1218 | ||
1219 | my $timeout = 10; | |
1220 | ||
1221 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
1222 | ||
1223 | die $@ if $@; | |
1224 | ||
1225 | return $res; | |
1226 | } | |
1227 | ||
5e1267a5 DM |
1228 | sub read_local_vm_config { |
1229 | ||
1230 | my $openvz = {}; | |
1231 | ||
1232 | my $qemu = {}; | |
1233 | ||
1234 | my $list = PVE::QemuServer::config_list(); | |
1235 | ||
1236 | foreach my $vmid (keys %$list) { | |
5e1267a5 DM |
1237 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); |
1238 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
1239 | $qemu->{$vmid} = $conf; | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
1244 | ||
1245 | return $vmdata; | |
1246 | }; | |
1247 | ||
1248 | sub read_vm_firewall_rules { | |
1249 | my ($vmdata) = @_; | |
1250 | my $rules = {}; | |
1251 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
1252 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
1253 | my $fh = IO::File->new($filename, O_RDONLY); | |
1254 | next if !$fh; | |
1255 | ||
51bae274 | 1256 | $rules->{$vmid} = parse_vm_fw_rules($filename, $fh); |
5e1267a5 DM |
1257 | } |
1258 | ||
1259 | return $rules; | |
1260 | } | |
1261 | ||
d8f2505e DM |
1262 | sub generate_std_chains { |
1263 | my ($ruleset) = @_; | |
1264 | ||
1265 | foreach my $chain (keys %$pve_std_chains) { | |
1266 | ruleset_create_chain($ruleset, $chain); | |
1267 | foreach my $rule (@{$pve_std_chains->{$chain}}) { | |
1268 | if (ref($rule)) { | |
1269 | ruleset_generate_rule($ruleset, $chain, $rule); | |
1270 | } else { | |
1271 | ruleset_addrule($ruleset, $chain, $rule); | |
1272 | } | |
1273 | } | |
1274 | } | |
1275 | } | |
1276 | ||
5e1267a5 | 1277 | sub compile { |
5e1267a5 DM |
1278 | my $vmdata = read_local_vm_config(); |
1279 | my $rules = read_vm_firewall_rules($vmdata); | |
6158271d | 1280 | |
51bae274 DM |
1281 | my $group_rules = {}; |
1282 | my $filename = "/etc/pve/firewall/groups.fw"; | |
1283 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1284 | $group_rules = parse_group_fw_rules($filename, $fh); | |
1285 | } | |
5e1267a5 | 1286 | |
3fa83edf DM |
1287 | #print Dumper($rules); |
1288 | ||
1289 | my $ruleset = {}; | |
1290 | ||
dec84fcd DM |
1291 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); |
1292 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
fadb13dd | 1293 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); |
3fa83edf | 1294 | |
d8f2505e | 1295 | generate_std_chains($ruleset); |
4e6112f9 | 1296 | |
fadb13dd | 1297 | my $enable_hostfw = 0; |
51bae274 DM |
1298 | $filename = "/etc/pve/local/host.fw"; |
1299 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1300 | my $host_rules = parse_host_fw_rules($filename, $fh); | |
fadb13dd DM |
1301 | |
1302 | $enable_hostfw = 1; | |
1303 | ||
51bae274 DM |
1304 | enablehostfw($ruleset, $host_rules, $group_rules); |
1305 | } | |
3fa83edf | 1306 | |
6158271d | 1307 | # generate firewall rules for QEMU VMs |
3fa83edf DM |
1308 | foreach my $vmid (keys %{$vmdata->{qemu}}) { |
1309 | my $conf = $vmdata->{qemu}->{$vmid}; | |
fa9c4a6f DM |
1310 | my $vmfw_conf = $rules->{$vmid}; |
1311 | next if !$vmfw_conf; | |
1312 | next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); | |
3fa83edf DM |
1313 | |
1314 | foreach my $netid (keys %$conf) { | |
1315 | next if $netid !~ m/^net(\d+)$/; | |
1316 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
1317 | next if !$net; | |
1318 | my $iface = "tap${vmid}i$1"; | |
5e1267a5 | 1319 | |
3fa83edf DM |
1320 | my $bridge = $net->{bridge}; |
1321 | next if !$bridge; # fixme: ? | |
1322 | ||
1323 | $bridge .= "v$net->{tag}" if $net->{tag}; | |
1324 | ||
fadb13dd | 1325 | |
3fa83edf DM |
1326 | generate_bridge_chains($ruleset, $bridge); |
1327 | ||
c29f55c9 | 1328 | my $macaddr = $net->{macaddr}; |
fa9c4a6f DM |
1329 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN'); |
1330 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT'); | |
3fa83edf DM |
1331 | } |
1332 | } | |
c0413e35 | 1333 | |
fadb13dd DM |
1334 | if ($enable_hostfw) { |
1335 | # allow traffic from lo (ourself) | |
1336 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT"); | |
1337 | } | |
c0413e35 | 1338 | |
3fa83edf DM |
1339 | return $ruleset; |
1340 | } | |
1341 | ||
1342 | sub get_ruleset_status { | |
1343 | my ($ruleset, $verbose) = @_; | |
1344 | ||
1345 | my $active_chains = iptables_get_chains(); | |
1346 | ||
1347 | my $statushash = {}; | |
1348 | ||
1349 | foreach my $chain (sort keys %$ruleset) { | |
09d5f68e | 1350 | my $digest = Digest::SHA->new('sha1'); |
3fa83edf DM |
1351 | foreach my $cmd (@{$ruleset->{$chain}}) { |
1352 | $digest->add("$cmd\n"); | |
1353 | } | |
1354 | my $sig = $digest->b64digest; | |
1355 | $statushash->{$chain}->{sig} = $sig; | |
1356 | ||
1357 | my $oldsig = $active_chains->{$chain}; | |
1358 | if (!defined($oldsig)) { | |
1359 | $statushash->{$chain}->{action} = 'create'; | |
1360 | } else { | |
1361 | if ($oldsig eq $sig) { | |
1362 | $statushash->{$chain}->{action} = 'exists'; | |
1363 | } else { | |
1364 | $statushash->{$chain}->{action} = 'update'; | |
1365 | } | |
1366 | } | |
1367 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
1368 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1369 | print "\t$cmd\n" if $verbose; | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | foreach my $chain (sort keys %$active_chains) { | |
1374 | if (!defined($ruleset->{$chain})) { | |
1375 | my $sig = $active_chains->{$chain}; | |
1376 | $statushash->{$chain}->{action} = 'delete'; | |
1377 | $statushash->{$chain}->{sig} = $sig; | |
1378 | print "delete $chain ($sig)\n" if $verbose; | |
1379 | } | |
6158271d | 1380 | } |
3fa83edf DM |
1381 | |
1382 | return $statushash; | |
1383 | } | |
1384 | ||
1385 | sub print_ruleset { | |
1386 | my ($ruleset) = @_; | |
1387 | ||
1388 | get_ruleset_status($ruleset, 1); | |
1389 | } | |
1390 | ||
1391 | sub print_sig_rule { | |
1392 | my ($chain, $sig) = @_; | |
1393 | ||
09d5f68e DM |
1394 | # We just use this to store a SHA1 checksum used to detect changes |
1395 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
b6360c3f DM |
1396 | } |
1397 | ||
a84f4d96 DM |
1398 | sub apply_ruleset { |
1399 | my ($ruleset, $verbose) = @_; | |
3fa83edf | 1400 | |
a84f4d96 | 1401 | enable_bridge_firewall(); |
3fa83edf DM |
1402 | |
1403 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
1404 | ||
1405 | my $statushash = get_ruleset_status($ruleset, $verbose); | |
1406 | ||
1407 | # create missing chains first | |
1408 | foreach my $chain (sort keys %$ruleset) { | |
1409 | my $stat = $statushash->{$chain}; | |
1410 | die "internal error" if !$stat; | |
1411 | next if $stat->{action} ne 'create'; | |
886aba9c | 1412 | |
3fa83edf DM |
1413 | $cmdlist .= ":$chain - [0:0]\n"; |
1414 | } | |
1415 | ||
dec84fcd | 1416 | my $rule = "INPUT -j PVEFW-INPUT"; |
3fa83edf DM |
1417 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1418 | $cmdlist .= "-A $rule\n"; | |
1419 | } | |
dec84fcd | 1420 | $rule = "OUTPUT -j PVEFW-OUTPUT"; |
3fa83edf DM |
1421 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1422 | $cmdlist .= "-A $rule\n"; | |
1423 | } | |
1424 | ||
dec84fcd | 1425 | $rule = "FORWARD -j PVEFW-FORWARD"; |
3fa83edf DM |
1426 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1427 | $cmdlist .= "-A $rule\n"; | |
1428 | } | |
1429 | ||
1430 | foreach my $chain (sort keys %$ruleset) { | |
1431 | my $stat = $statushash->{$chain}; | |
1432 | die "internal error" if !$stat; | |
1433 | ||
1434 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
1435 | $cmdlist .= "-F $chain\n"; | |
1436 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1437 | $cmdlist .= "$cmd\n"; | |
1438 | } | |
1439 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
1440 | } elsif ($stat->{action} eq 'delete') { | |
f5d28682 | 1441 | die "internal error"; # this should not happen |
3fa83edf DM |
1442 | } elsif ($stat->{action} eq 'exists') { |
1443 | # do nothing | |
1444 | } else { | |
1445 | die "internal error - unknown status '$stat->{action}'"; | |
1446 | } | |
1447 | } | |
1448 | ||
f5d28682 DM |
1449 | foreach my $chain (keys %$statushash) { |
1450 | next if $statushash->{$chain}->{action} ne 'delete'; | |
1451 | $cmdlist .= "-F $chain\n"; | |
1452 | } | |
1453 | foreach my $chain (keys %$statushash) { | |
1454 | next if $statushash->{$chain}->{action} ne 'delete'; | |
fadb13dd DM |
1455 | next if $chain eq 'PVEFW-INPUT'; |
1456 | next if $chain eq 'PVEFW-OUTPUT'; | |
1457 | next if $chain eq 'PVEFW-FORWARD'; | |
f5d28682 DM |
1458 | $cmdlist .= "-X $chain\n"; |
1459 | } | |
1460 | ||
3fa83edf DM |
1461 | $cmdlist .= "COMMIT\n"; |
1462 | ||
1463 | print $cmdlist if $verbose; | |
1464 | ||
1465 | iptables_restore_cmdlist($cmdlist); | |
1466 | ||
6158271d | 1467 | # test: re-read status and check if everything is up to date |
3fa83edf DM |
1468 | $statushash = get_ruleset_status($ruleset); |
1469 | ||
1470 | my $errors; | |
1471 | foreach my $chain (sort keys %$ruleset) { | |
1472 | my $stat = $statushash->{$chain}; | |
1473 | if ($stat->{action} ne 'exists') { | |
1474 | warn "unable to update chain '$chain'\n"; | |
1475 | $errors = 1; | |
1476 | } | |
1477 | } | |
b6360c3f | 1478 | |
3fa83edf | 1479 | die "unable to apply firewall changes\n" if $errors; |
b6360c3f DM |
1480 | } |
1481 | ||
b6360c3f | 1482 | 1; |