]> git.proxmox.com Git - pve-firewall.git/blob - src/PVE/Firewall.pm
plug venet0 chains into PVEFW-FORWARD
[pve-firewall.git] / src / PVE / Firewall.pm
1 package PVE::Firewall;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use Digest::SHA;
7 use PVE::INotify;
8 use PVE::Cluster;
9 use PVE::ProcFSTools;
10 use PVE::Tools;
11 use PVE::QemuServer;
12 use PVE::OpenVZ; # dependeny problem?!
13 use File::Basename;
14 use File::Path;
15 use IO::File;
16 use Net::IP;
17 use PVE::Tools qw(run_command lock_file);
18
19 use Data::Dumper;
20
21 my $nodename = PVE::INotify::nodename();
22
23 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
24 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
25
26 my $default_log_level = 'info';
27
28 my $log_level_hash = {
29 debug => 7,
30 info => 6,
31 notice => 5,
32 warning => 4,
33 err => 3,
34 crit => 2,
35 alert => 1,
36 emerg => 0,
37 };
38
39 # imported/converted from: /usr/share/shorewall/macro.*
40 my $pve_fw_macros = {
41 'Amanda' => [
42 { action => 'PARAM', proto => 'udp', dport => '10080' },
43 { action => 'PARAM', proto => 'tcp', dport => '10080' },
44 ],
45 'Auth' => [
46 { action => 'PARAM', proto => 'tcp', dport => '113' },
47 ],
48 'BGP' => [
49 { action => 'PARAM', proto => 'tcp', dport => '179' },
50 ],
51 'BitTorrent' => [
52 { action => 'PARAM', proto => 'tcp', dport => '6881:6889' },
53 { action => 'PARAM', proto => 'udp', dport => '6881' },
54 ],
55 'BitTorrent32' => [
56 { action => 'PARAM', proto => 'tcp', dport => '6881:6999' },
57 { action => 'PARAM', proto => 'udp', dport => '6881' },
58 ],
59 'CVS' => [
60 { action => 'PARAM', proto => 'tcp', dport => '2401' },
61 ],
62 'Citrix' => [
63 { action => 'PARAM', proto => 'tcp', dport => '1494' },
64 { action => 'PARAM', proto => 'udp', dport => '1604' },
65 { action => 'PARAM', proto => 'tcp', dport => '2598' },
66 ],
67 'DAAP' => [
68 { action => 'PARAM', proto => 'tcp', dport => '3689' },
69 { action => 'PARAM', proto => 'udp', dport => '3689' },
70 ],
71 'DCC' => [
72 { action => 'PARAM', proto => 'tcp', dport => '6277' },
73 ],
74 'DHCPfwd' => [
75 { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' },
76 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' },
77 ],
78 'DNS' => [
79 { action => 'PARAM', proto => 'udp', dport => '53' },
80 { action => 'PARAM', proto => 'tcp', dport => '53' },
81 ],
82 'Distcc' => [
83 { action => 'PARAM', proto => 'tcp', dport => '3632' },
84 ],
85 'Edonkey' => [
86 { action => 'PARAM', proto => 'tcp', dport => '4662' },
87 { action => 'PARAM', proto => 'udp', dport => '4665' },
88 ],
89 'FTP' => [
90 { action => 'PARAM', proto => 'tcp', dport => '21' },
91 ],
92 'Finger' => [
93 { action => 'PARAM', proto => 'tcp', dport => '79' },
94 ],
95 'GNUnet' => [
96 { action => 'PARAM', proto => 'tcp', dport => '2086' },
97 { action => 'PARAM', proto => 'udp', dport => '2086' },
98 { action => 'PARAM', proto => 'tcp', dport => '1080' },
99 { action => 'PARAM', proto => 'udp', dport => '1080' },
100 ],
101 'GRE' => [
102 { action => 'PARAM', proto => '47' },
103 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
104 ],
105 'Git' => [
106 { action => 'PARAM', proto => 'tcp', dport => '9418' },
107 ],
108 'Gnutella' => [
109 { action => 'PARAM', proto => 'tcp', dport => '6346' },
110 { action => 'PARAM', proto => 'udp', dport => '6346' },
111 ],
112 'HKP' => [
113 { action => 'PARAM', proto => 'tcp', dport => '11371' },
114 ],
115 'HTTP' => [
116 { action => 'PARAM', proto => 'tcp', dport => '80' },
117 ],
118 'HTTPS' => [
119 { action => 'PARAM', proto => 'tcp', dport => '443' },
120 ],
121 'ICPV2' => [
122 { action => 'PARAM', proto => 'udp', dport => '3130' },
123 ],
124 'ICQ' => [
125 { action => 'PARAM', proto => 'tcp', dport => '5190' },
126 ],
127 'IMAP' => [
128 { action => 'PARAM', proto => 'tcp', dport => '143' },
129 ],
130 'IMAPS' => [
131 { action => 'PARAM', proto => 'tcp', dport => '993' },
132 ],
133 'IPIP' => [
134 { action => 'PARAM', proto => '94' },
135 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' },
136 ],
137 'IPP' => [
138 { action => 'PARAM', proto => 'tcp', dport => '631' },
139 ],
140 'IPPbrd' => [
141 { action => 'PARAM', proto => 'udp', dport => '631' },
142 ],
143 'IPPserver' => [
144 { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' },
145 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' },
146 ],
147 'IPsec' => [
148 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
149 { action => 'PARAM', proto => '50' },
150 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
151 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
152 ],
153 'IPsecah' => [
154 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
155 { action => 'PARAM', proto => '51' },
156 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
157 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' },
158 ],
159 'IPsecnat' => [
160 { action => 'PARAM', proto => 'udp', dport => '500' },
161 { action => 'PARAM', proto => 'udp', dport => '4500' },
162 { action => 'PARAM', proto => '50' },
163 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' },
164 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' },
165 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
166 ],
167 'IRC' => [
168 { action => 'PARAM', proto => 'tcp', dport => '6667' },
169 ],
170 'JabberPlain' => [
171 { action => 'PARAM', proto => 'tcp', dport => '5222' },
172 ],
173 'JabberSecure' => [
174 { action => 'PARAM', proto => 'tcp', dport => '5223' },
175 ],
176 'Jabberd' => [
177 { action => 'PARAM', proto => 'tcp', dport => '5269' },
178 ],
179 'Jetdirect' => [
180 { action => 'PARAM', proto => 'tcp', dport => '9100' },
181 ],
182 'L2TP' => [
183 { action => 'PARAM', proto => 'udp', dport => '1701' },
184 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' },
185 ],
186 'LDAP' => [
187 { action => 'PARAM', proto => 'tcp', dport => '389' },
188 ],
189 'LDAPS' => [
190 { action => 'PARAM', proto => 'tcp', dport => '636' },
191 ],
192 'MSNP' => [
193 { action => 'PARAM', proto => 'tcp', dport => '1863' },
194 ],
195 'MSSQL' => [
196 { action => 'PARAM', proto => 'tcp', dport => '1433' },
197 ],
198 'Mail' => [
199 { action => 'PARAM', proto => 'tcp', dport => '25' },
200 { action => 'PARAM', proto => 'tcp', dport => '465' },
201 { action => 'PARAM', proto => 'tcp', dport => '587' },
202 ],
203 'Munin' => [
204 { action => 'PARAM', proto => 'tcp', dport => '4949' },
205 ],
206 'MySQL' => [
207 { action => 'PARAM', proto => 'tcp', dport => '3306' },
208 ],
209 'NNTP' => [
210 { action => 'PARAM', proto => 'tcp', dport => '119' },
211 ],
212 'NNTPS' => [
213 { action => 'PARAM', proto => 'tcp', dport => '563' },
214 ],
215 'NTP' => [
216 { action => 'PARAM', proto => 'udp', dport => '123' },
217 ],
218 'NTPbi' => [
219 { action => 'PARAM', proto => 'udp', dport => '123' },
220 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' },
221 ],
222 'NTPbrd' => [
223 { action => 'PARAM', proto => 'udp', dport => '123' },
224 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' },
225 ],
226 'OSPF' => [
227 { action => 'PARAM', proto => '89' },
228 ],
229 'OpenVPN' => [
230 { action => 'PARAM', proto => 'udp', dport => '1194' },
231 ],
232 'PCA' => [
233 { action => 'PARAM', proto => 'udp', dport => '5632' },
234 { action => 'PARAM', proto => 'tcp', dport => '5631' },
235 ],
236 'POP3' => [
237 { action => 'PARAM', proto => 'tcp', dport => '110' },
238 ],
239 'POP3S' => [
240 { action => 'PARAM', proto => 'tcp', dport => '995' },
241 ],
242 'PPtP' => [
243 { action => 'PARAM', proto => '47' },
244 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
245 { action => 'PARAM', proto => 'tcp', dport => '1723' },
246 ],
247 'Ping' => [
248 { action => 'PARAM', proto => 'icmp', dport => 'echo-request' },
249 ],
250 'PostgreSQL' => [
251 { action => 'PARAM', proto => 'tcp', dport => '5432' },
252 ],
253 'Printer' => [
254 { action => 'PARAM', proto => 'tcp', dport => '515' },
255 ],
256 'RDP' => [
257 { action => 'PARAM', proto => 'tcp', dport => '3389' },
258 ],
259 'RIPbi' => [
260 { action => 'PARAM', proto => 'udp', dport => '520' },
261 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' },
262 ],
263 'RNDC' => [
264 { action => 'PARAM', proto => 'tcp', dport => '953' },
265 ],
266 'Razor' => [
267 { action => 'ACCEPT', proto => 'tcp', dport => '2703' },
268 ],
269 'Rdate' => [
270 { action => 'PARAM', proto => 'tcp', dport => '37' },
271 ],
272 'Rsync' => [
273 { action => 'PARAM', proto => 'tcp', dport => '873' },
274 ],
275 'SANE' => [
276 { action => 'PARAM', proto => 'tcp', dport => '6566' },
277 ],
278 'SMB' => [
279 { action => 'PARAM', proto => 'udp', dport => '135,445' },
280 { action => 'PARAM', proto => 'udp', dport => '137:139' },
281 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
282 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
283 ],
284 'SMBBI' => [
285 { action => 'PARAM', proto => 'udp', dport => '135,445' },
286 { action => 'PARAM', proto => 'udp', dport => '137:139' },
287 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
288 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
289 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' },
290 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' },
291 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' },
292 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' },
293 ],
294 'SMBswat' => [
295 { action => 'PARAM', proto => 'tcp', dport => '901' },
296 ],
297 'SMTP' => [
298 { action => 'PARAM', proto => 'tcp', dport => '25' },
299 ],
300 'SMTPS' => [
301 { action => 'PARAM', proto => 'tcp', dport => '465' },
302 ],
303 'SNMP' => [
304 { action => 'PARAM', proto => 'udp', dport => '161:162' },
305 { action => 'PARAM', proto => 'tcp', dport => '161' },
306 ],
307 'SPAMD' => [
308 { action => 'PARAM', proto => 'tcp', dport => '783' },
309 ],
310 'SSH' => [
311 { action => 'PARAM', proto => 'tcp', dport => '22' },
312 ],
313 'SVN' => [
314 { action => 'PARAM', proto => 'tcp', dport => '3690' },
315 ],
316 'SixXS' => [
317 { action => 'PARAM', proto => 'tcp', dport => '3874' },
318 { action => 'PARAM', proto => 'udp', dport => '3740' },
319 { action => 'PARAM', proto => '41' },
320 { action => 'PARAM', proto => 'udp', dport => '5072,8374' },
321 ],
322 'Squid' => [
323 { action => 'PARAM', proto => 'tcp', dport => '3128' },
324 ],
325 'Submission' => [
326 { action => 'PARAM', proto => 'tcp', dport => '587' },
327 ],
328 'Syslog' => [
329 { action => 'PARAM', proto => 'udp', dport => '514' },
330 { action => 'PARAM', proto => 'tcp', dport => '514' },
331 ],
332 'TFTP' => [
333 { action => 'PARAM', proto => 'udp', dport => '69' },
334 ],
335 'Telnet' => [
336 { action => 'PARAM', proto => 'tcp', dport => '23' },
337 ],
338 'Telnets' => [
339 { action => 'PARAM', proto => 'tcp', dport => '992' },
340 ],
341 'Time' => [
342 { action => 'PARAM', proto => 'tcp', dport => '37' },
343 ],
344 'Trcrt' => [
345 { action => 'PARAM', proto => 'udp', dport => '33434:33524' },
346 { action => 'PARAM', proto => 'icmp', dport => 'echo-request' },
347 ],
348 'VNC' => [
349 { action => 'PARAM', proto => 'tcp', dport => '5900:5909' },
350 ],
351 'VNCL' => [
352 { action => 'PARAM', proto => 'tcp', dport => '5500' },
353 ],
354 'Web' => [
355 { action => 'PARAM', proto => 'tcp', dport => '80' },
356 { action => 'PARAM', proto => 'tcp', dport => '443' },
357 ],
358 'Webcache' => [
359 { action => 'PARAM', proto => 'tcp', dport => '8080' },
360 ],
361 'Webmin' => [
362 { action => 'PARAM', proto => 'tcp', dport => '10000' },
363 ],
364 'Whois' => [
365 { action => 'PARAM', proto => 'tcp', dport => '43' },
366 ],
367 };
368
369 my $pve_fw_parsed_macros;
370 my $pve_fw_preferred_macro_names = {};
371
372 my $pve_std_chains = {
373 'PVEFW-SET-ACCEPT-MARK' => [
374 "-j MARK --set-mark 1",
375 ],
376 'PVEFW-DropBroadcast' => [
377 # same as shorewall 'Broadcast'
378 # simply DROP BROADCAST/MULTICAST/ANYCAST
379 # we can use this to reduce logging
380 { action => 'DROP', dsttype => 'BROADCAST' },
381 { action => 'DROP', dsttype => 'MULTICAST' },
382 { action => 'DROP', dsttype => 'ANYCAST' },
383 { action => 'DROP', dest => '224.0.0.0/4' },
384 ],
385 'PVEFW-reject' => [
386 # same as shorewall 'reject'
387 { action => 'DROP', dsttype => 'BROADCAST' },
388 { action => 'DROP', source => '224.0.0.0/4' },
389 { action => 'DROP', proto => 'icmp' },
390 "-p tcp -j REJECT --reject-with tcp-reset",
391 "-p udp -j REJECT --reject-with icmp-port-unreachable",
392 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
393 "-j REJECT --reject-with icmp-host-prohibited",
394 ],
395 'PVEFW-Drop' => [
396 # same as shorewall 'Drop', which is equal to DROP,
397 # but REJECT/DROP some packages to reduce logging,
398 # and ACCEPT critical ICMP types
399 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
400 # we are not interested in BROADCAST/MULTICAST/ANYCAST
401 { action => 'PVEFW-DropBroadcast' },
402 # ACCEPT critical ICMP types
403 { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' },
404 { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' },
405 # Drop packets with INVALID state
406 "-m conntrack --ctstate INVALID -j DROP",
407 # Drop Microsoft SMB noise
408 { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 },
409 { action => 'DROP', proto => 'udp', dport => '137:139'},
410 { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 },
411 { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 },
412 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
413 # Drop new/NotSyn traffic so that it doesn't get logged
414 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
415 # Drop DNS replies
416 { action => 'DROP', proto => 'udp', sport => 53 },
417 ],
418 'PVEFW-Reject' => [
419 # same as shorewall 'Reject', which is equal to Reject,
420 # but REJECT/DROP some packages to reduce logging,
421 # and ACCEPT critical ICMP types
422 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
423 # we are not interested in BROADCAST/MULTICAST/ANYCAST
424 { action => 'PVEFW-DropBroadcast' },
425 # ACCEPT critical ICMP types
426 { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' },
427 { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' },
428 # Drop packets with INVALID state
429 "-m conntrack --ctstate INVALID -j DROP",
430 # Drop Microsoft SMB noise
431 { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 },
432 { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'},
433 { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 },
434 { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 },
435 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
436 # Drop new/NotSyn traffic so that it doesn't get logged
437 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
438 # Drop DNS replies
439 { action => 'DROP', proto => 'udp', sport => 53 },
440 ],
441 'PVEFW-tcpflags' => [
442 # same as shorewall tcpflags action.
443 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
444 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
445 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
446 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
447 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
448 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
449 ],
450 'PVEFW-smurfs' => [
451 # same as shorewall smurfs action
452 # Filter packets for smurfs (packets with a broadcast address as the source).
453 "-s 0.0.0.0/32 -j RETURN",
454 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
455 "-s 224.0.0.0/4 -g PVEFW-smurflog",
456 ],
457 };
458
459 # iptables -p icmp -h
460 my $icmp_type_names = {
461 any => 1,
462 'echo-reply' => 1,
463 'destination-unreachable' => 1,
464 'network-unreachable' => 1,
465 'host-unreachable' => 1,
466 'protocol-unreachable' => 1,
467 'port-unreachable' => 1,
468 'fragmentation-needed' => 1,
469 'source-route-failed' => 1,
470 'network-unknown' => 1,
471 'host-unknown' => 1,
472 'network-prohibited' => 1,
473 'host-prohibited' => 1,
474 'TOS-network-unreachable' => 1,
475 'TOS-host-unreachable' => 1,
476 'communication-prohibited' => 1,
477 'host-precedence-violation' => 1,
478 'precedence-cutoff' => 1,
479 'source-quench' => 1,
480 'redirect' => 1,
481 'network-redirect' => 1,
482 'host-redirect' => 1,
483 'TOS-network-redirect' => 1,
484 'TOS-host-redirect' => 1,
485 'echo-request' => 1,
486 'router-advertisement' => 1,
487 'router-solicitation' => 1,
488 'time-exceeded' => 1,
489 'ttl-zero-during-transit' => 1,
490 'ttl-zero-during-reassembly' => 1,
491 'parameter-problem' => 1,
492 'ip-header-bad' => 1,
493 'required-option-missing' => 1,
494 'timestamp-request' => 1,
495 'timestamp-reply' => 1,
496 'address-mask-request' => 1,
497 'address-mask-reply' => 1,
498 };
499
500 sub get_firewall_macros {
501
502 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
503
504 $pve_fw_parsed_macros = {};
505
506 foreach my $k (keys %$pve_fw_macros) {
507 my $name = lc($k);
508
509 my $macro = $pve_fw_macros->{$k};
510 $pve_fw_preferred_macro_names->{$name} = $k;
511 $pve_fw_parsed_macros->{$name} = $macro;
512 }
513
514 return $pve_fw_parsed_macros;
515 }
516
517 my $etc_services;
518
519 sub get_etc_services {
520
521 return $etc_services if $etc_services;
522
523 my $filename = "/etc/services";
524
525 my $fh = IO::File->new($filename, O_RDONLY);
526 if (!$fh) {
527 warn "unable to read '$filename' - $!\n";
528 return {};
529 }
530
531 my $services = {};
532
533 while (my $line = <$fh>) {
534 chomp ($line);
535 next if $line =~m/^#/;
536 next if ($line =~m/^\s*$/);
537
538 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
539 $services->{byid}->{$2}->{name} = $1;
540 $services->{byid}->{$2}->{port} = $2;
541 $services->{byid}->{$2}->{$3} = 1;
542 $services->{byname}->{$1} = $services->{byid}->{$2};
543 }
544 }
545
546 close($fh);
547
548 $etc_services = $services;
549
550
551 return $etc_services;
552 }
553
554 my $etc_protocols;
555
556 sub get_etc_protocols {
557 return $etc_protocols if $etc_protocols;
558
559 my $filename = "/etc/protocols";
560
561 my $fh = IO::File->new($filename, O_RDONLY);
562 if (!$fh) {
563 warn "unable to read '$filename' - $!\n";
564 return {};
565 }
566
567 my $protocols = {};
568
569 while (my $line = <$fh>) {
570 chomp ($line);
571 next if $line =~m/^#/;
572 next if ($line =~m/^\s*$/);
573
574 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
575 $protocols->{byid}->{$2}->{name} = $1;
576 $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
577 }
578 }
579
580 close($fh);
581
582 $etc_protocols = $protocols;
583
584 return $etc_protocols;
585 }
586
587 sub parse_address_list {
588 my ($str) = @_;
589
590 my $nbaor = 0;
591 foreach my $aor (split(/,/, $str)) {
592 if (!Net::IP->new($aor)) {
593 my $err = Net::IP::Error();
594 die "invalid IP address: $err\n";
595 }else{
596 $nbaor++;
597 }
598 }
599 return $nbaor;
600 }
601
602 sub parse_port_name_number_or_range {
603 my ($str) = @_;
604
605 my $services = PVE::Firewall::get_etc_services();
606 my $nbports = 0;
607 foreach my $item (split(/,/, $str)) {
608 my $portlist = "";
609 my $oldpon = undef;
610 $nbports++;
611 foreach my $pon (split(':', $item, 2)) {
612 $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port};
613 if ($pon =~ m/^\d+$/){
614 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
615 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
616 $oldpon = $pon;
617 }else{
618 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon};
619 }
620 }
621 }
622
623 return ($nbports);
624 }
625
626 my $bridge_firewall_enabled = 0;
627
628 sub enable_bridge_firewall {
629
630 return if $bridge_firewall_enabled; # only once
631
632 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
633 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
634
635 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
636 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1");
637
638 $bridge_firewall_enabled = 1;
639 }
640
641 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
642
643 sub iptables {
644 my ($cmd) = @_;
645
646 run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
647 }
648
649 sub iptables_restore_cmdlist {
650 my ($cmdlist) = @_;
651
652 run_command("/sbin/iptables-restore -n", input => $cmdlist);
653 }
654
655 sub iptables_get_chains {
656
657 my $res = {};
658
659 # check what chains we want to track
660 my $is_pvefw_chain = sub {
661 my $name = shift;
662
663 return 1 if $name =~ m/^PVEFW-\S+$/;
664
665 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
666
667 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
668
669 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
670
671 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
672 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
673
674 return undef;
675 };
676
677 my $table = '';
678
679 my $parser = sub {
680 my $line = shift;
681
682 return if $line =~ m/^#/;
683 return if $line =~ m/^\s*$/;
684
685 if ($line =~ m/^\*(\S+)$/) {
686 $table = $1;
687 return;
688 }
689
690 return if $table ne 'filter';
691
692 if ($line =~ m/^:(\S+)\s/) {
693 my $chain = $1;
694 return if !&$is_pvefw_chain($chain);
695 $res->{$chain} = "unknown";
696 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
697 my ($chain, $sig) = ($1, $2);
698 return if !&$is_pvefw_chain($chain);
699 $res->{$chain} = $sig;
700 } else {
701 # simply ignore the rest
702 return;
703 }
704 };
705
706 run_command("/sbin/iptables-save", outfunc => $parser);
707
708 return $res;
709 }
710
711 sub iptables_chain_exist {
712 my ($chain) = @_;
713
714 eval{
715 iptables("-n --list $chain");
716 };
717 return undef if $@;
718
719 return 1;
720 }
721
722 sub iptables_rule_exist {
723 my ($rule) = @_;
724
725 eval{
726 iptables("-C $rule");
727 };
728 return undef if $@;
729
730 return 1;
731 }
732
733 sub ruleset_generate_cmdstr {
734 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
735
736 return if $rule->{disable};
737
738 my @cmd = ();
739
740 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in};
741 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out};
742
743 push @cmd, "-m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
744 push @cmd, "-s $rule->{source}" if $rule->{source};
745 push @cmd, "-m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
746 push @cmd, "-d $rule->{dest}" if $rule->{dest};
747
748 if ($rule->{proto}) {
749 push @cmd, "-p $rule->{proto}";
750
751 my $multiport = 0;
752 $multiport++ if $rule->{nbdport} && ($rule->{nbdport} > 1);
753 $multiport++ if $rule->{nbsport} && ($rule->{nbsport} > 1);
754
755 push @cmd, "--match multiport" if $multiport;
756
757 die "multiport: option '--sports' cannot be used together with '--dports'\n"
758 if ($multiport == 2) && ($rule->{dport} ne $rule->{sport});
759
760 if ($rule->{dport}) {
761 if ($rule->{proto} && $rule->{proto} eq 'icmp') {
762 # Note: we use dport to store --icmp-type
763 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}});
764 push @cmd, "-m icmp --icmp-type $rule->{dport}";
765 } else {
766 if ($rule->{nbdport} && $rule->{nbdport} > 1) {
767 if ($multiport == 2) {
768 push @cmd, "--ports $rule->{dport}";
769 } else {
770 push @cmd, "--dports $rule->{dport}";
771 }
772 } else {
773 push @cmd, "--dport $rule->{dport}";
774 }
775 }
776 }
777
778 if ($rule->{sport}) {
779 if ($rule->{nbsport} && $rule->{nbsport} > 1) {
780 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
781 } else {
782 push @cmd, "--sport $rule->{sport}";
783 }
784 }
785 } elsif ($rule->{dport} || $rule->{sport}) {
786 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport};
787 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport};
788 }
789
790 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype};
791
792 if (my $action = $rule->{action}) {
793 $action = $actions->{$action} if defined($actions->{$action});
794 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
795 push @cmd, $goto ? "-g $action" : "-j $action";
796 }
797
798 return scalar(@cmd) ? join(' ', @cmd) : undef;
799 }
800
801 sub ruleset_generate_rule {
802 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
803
804 if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $rule, $actions, $goto)) {
805 ruleset_addrule($ruleset, $chain, $cmdstr);
806 }
807 }
808 sub ruleset_generate_rule_insert {
809 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
810
811 if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $rule, $actions, $goto)) {
812 ruleset_insertrule($ruleset, $chain, $cmdstr);
813 }
814 }
815
816 sub ruleset_create_chain {
817 my ($ruleset, $chain) = @_;
818
819 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
820
821 die "chain '$chain' already exists\n" if $ruleset->{$chain};
822
823 $ruleset->{$chain} = [];
824 }
825
826 sub ruleset_chain_exist {
827 my ($ruleset, $chain) = @_;
828
829 return $ruleset->{$chain} ? 1 : undef;
830 }
831
832 sub ruleset_addrule {
833 my ($ruleset, $chain, $rule) = @_;
834
835 die "no such chain '$chain'\n" if !$ruleset->{$chain};
836
837 push @{$ruleset->{$chain}}, "-A $chain $rule";
838 }
839
840 sub ruleset_insertrule {
841 my ($ruleset, $chain, $rule) = @_;
842
843 die "no such chain '$chain'\n" if !$ruleset->{$chain};
844
845 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
846 }
847
848 sub generate_bridge_chains {
849 my ($ruleset, $hostfw_conf, $bridge) = @_;
850
851 if (!ruleset_chain_exist($ruleset, "$bridge-FW")) {
852 ruleset_create_chain($ruleset, "$bridge-FW");
853 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
854 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
855 }
856
857 if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) {
858 ruleset_create_chain($ruleset, "$bridge-OUT");
859 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
860 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i $bridge -m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
861 }
862
863 if (!ruleset_chain_exist($ruleset, "$bridge-IN")) {
864 ruleset_create_chain($ruleset, "$bridge-IN");
865 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
866 ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
867 # accept traffic to unmanaged bridge ports
868 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j ACCEPT ");
869 }
870 }
871
872 sub ruleset_add_chain_policy {
873 my ($ruleset, $chain, $policy, $loglevel, $accept_action) = @_;
874
875 if ($policy eq 'ACCEPT') {
876
877 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
878 { ACCEPT => $accept_action});
879
880 } elsif ($policy eq 'DROP') {
881
882 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop");
883
884 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"$chain-dropped: \" --log-level $loglevel")
885 if defined($loglevel);
886
887 ruleset_addrule($ruleset, $chain, "-j DROP");
888 } elsif ($policy eq 'REJECT') {
889 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject");
890
891 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"$chain-reject: \" --log-level $loglevel")
892 if defined($loglevel);
893
894 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject");
895 } else {
896 # should not happen
897 die "internal error: unknown policy '$policy'";
898 }
899 }
900
901 sub ruleset_create_vm_chain {
902 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
903
904 ruleset_create_chain($ruleset, $chain);
905
906 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
907 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs");
908 }
909
910 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
911 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 67:68 -j ACCEPT");
912 }
913
914 if ($options->{tcpflags}) {
915 ruleset_addrule($ruleset, $chain, "-p tcp -j PVEFW-tcpflags");
916 }
917
918 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
919 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
920
921 if ($direction eq 'OUT') {
922 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
923 ruleset_addrule($ruleset, $chain, "-m mac ! --mac-source $macaddr -j DROP");
924 }
925 ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
926 }
927 }
928
929 sub ruleset_generate_vm_rules {
930 my ($ruleset, $rules, $groups_conf, $chain, $netid, $direction) = @_;
931
932 my $lc_direction = lc($direction);
933
934 foreach my $rule (@$rules) {
935 next if $rule->{iface} && $rule->{iface} ne $netid;
936 next if $rule->{disable};
937 if ($rule->{type} eq 'group') {
938 my $group_chain = "GROUP-$rule->{action}-$direction";
939 if(!ruleset_chain_exist($ruleset, $group_chain)){
940 generate_group_rules($ruleset, $groups_conf, $rule->{action});
941 }
942 ruleset_addrule($ruleset, $chain, "-j $group_chain");
943 ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j RETURN")
944 if $direction eq 'OUT';
945 } else {
946 next if $rule->{type} ne $lc_direction;
947 if ($direction eq 'OUT') {
948 ruleset_generate_rule($ruleset, $chain, $rule,
949 { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" });
950 } else {
951 ruleset_generate_rule($ruleset, $chain, $rule, { REJECT => "PVEFW-reject" });
952 }
953 }
954 }
955 }
956
957 sub generate_venet_rules_direction {
958 my ($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
959
960 parse_address_list($ip); # make sure we have a valid $ip list
961
962 my $lc_direction = lc($direction);
963
964 my $rules = $vmfw_conf->{rules};
965
966 my $options = $vmfw_conf->{options};
967 my $loglevel = get_option_log_level($options, "log_level_${lc_direction}");
968
969 my $chain = "venet0-$vmid-$direction";
970
971 ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction);
972
973 ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $chain, 'venet', $direction);
974
975 # implement policy
976 my $policy;
977
978 if ($direction eq 'OUT') {
979 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
980 } else {
981 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
982 }
983
984 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : "ACCEPT";
985 ruleset_add_chain_policy($ruleset, $chain, $policy, $loglevel, $accept_action);
986
987 # plug into FORWARD chain
988 if ($direction eq 'OUT') {
989 ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD", {
990 action => $chain,
991 source => $ip,
992 iface_in => 'venet0'});
993 } else {
994 ruleset_generate_rule($ruleset, "PVEFW-FORWARD", {
995 action => $chain,
996 dest => $ip,
997 iface_out => 'venet0'});
998 }
999 }
1000
1001 sub generate_tap_rules_direction {
1002 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
1003
1004 my $lc_direction = lc($direction);
1005
1006 my $rules = $vmfw_conf->{rules};
1007
1008 my $options = $vmfw_conf->{options};
1009 my $loglevel = get_option_log_level($options, "log_level_${lc_direction}");
1010
1011 my $tapchain = "$iface-$direction";
1012
1013 ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction);
1014
1015 ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $tapchain, $netid, $direction);
1016
1017 # implement policy
1018 my $policy;
1019
1020 if ($direction eq 'OUT') {
1021 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1022 } else {
1023 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1024 }
1025
1026 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : "ACCEPT";
1027 ruleset_add_chain_policy($ruleset, $tapchain, $policy, $loglevel, $accept_action);
1028
1029 # plug the tap chain to bridge chain
1030 my $physdevdirection = $direction eq 'IN' ? "out" : "in";
1031 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
1032 ruleset_insertrule($ruleset, "$bridge-$direction", $rule);
1033 }
1034
1035 sub enable_host_firewall {
1036 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
1037
1038 # fixme: allow security groups
1039
1040 my $options = $hostfw_conf->{options};
1041 my $rules = $hostfw_conf->{rules};
1042
1043 # host inbound firewall
1044 my $chain = "PVEFW-HOST-IN";
1045 ruleset_create_chain($ruleset, $chain);
1046
1047 my $loglevel = get_option_log_level($options, "log_level_in");
1048
1049 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
1050 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1051 ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT");
1052 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
1053 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
1054 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
1055
1056 # we use RETURN because we need to check also tap rules
1057 my $accept_action = 'RETURN';
1058
1059 foreach my $rule (@$rules) {
1060 next if $rule->{type} ne 'in';
1061 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" });
1062 }
1063
1064 # implement input policy
1065 my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1066 ruleset_add_chain_policy($ruleset, $chain, $policy, $loglevel, $accept_action);
1067
1068 # host outbound firewall
1069 $chain = "PVEFW-HOST-OUT";
1070 ruleset_create_chain($ruleset, $chain);
1071
1072 $loglevel = get_option_log_level($options, "log_level_out");
1073
1074 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
1075 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1076 ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT");
1077 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
1078 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
1079 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
1080
1081 # we use RETURN because we may want to check other thigs later
1082 $accept_action = 'RETURN';
1083
1084 foreach my $rule (@$rules) {
1085 next if $rule->{type} ne 'out';
1086 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" });
1087 }
1088
1089 # implement output policy
1090 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1091 ruleset_add_chain_policy($ruleset, $chain, $policy, $loglevel, $accept_action);
1092
1093 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
1094 ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
1095 }
1096
1097 sub generate_group_rules {
1098 my ($ruleset, $groups_conf, $group) = @_;
1099
1100 die "no such security group '$group'\n" if !$groups_conf->{$group};
1101
1102 my $rules = $groups_conf->{$group}->{rules};
1103
1104 my $chain = "GROUP-${group}-IN";
1105
1106 ruleset_create_chain($ruleset, $chain);
1107
1108 foreach my $rule (@$rules) {
1109 next if $rule->{type} ne 'in';
1110 ruleset_generate_rule($ruleset, $chain, $rule, { REJECT => "PVEFW-reject" });
1111 }
1112
1113 $chain = "GROUP-${group}-OUT";
1114
1115 ruleset_create_chain($ruleset, $chain);
1116 ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
1117
1118 foreach my $rule (@$rules) {
1119 next if $rule->{type} ne 'out';
1120 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1121 # check also other tap rules later
1122 ruleset_generate_rule($ruleset, $chain, $rule,
1123 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" });
1124 }
1125 }
1126
1127 my $MAX_NETS = 32;
1128 my $valid_netdev_names = {};
1129 for (my $i = 0; $i < $MAX_NETS; $i++) {
1130 $valid_netdev_names->{"net$i"} = 1;
1131 }
1132
1133 sub parse_fw_rule {
1134 my ($line, $need_iface, $allow_groups) = @_;
1135
1136 my $macros = get_firewall_macros();
1137 my $protocols = get_etc_protocols();
1138
1139 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1140
1141 # we can add single line comments to the end of the rule
1142 my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
1143
1144 # we can disable a rule when prefixed with '|'
1145 my $disable = 1 if $line =~ s/^\|//;
1146
1147 my @data = split(/\s+/, $line);
1148 my $expected_elements = $need_iface ? 8 : 7;
1149
1150 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
1151
1152 if ($need_iface) {
1153 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1154 } else {
1155 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1156 }
1157
1158 die "incomplete rule\n" if ! ($type && $action);
1159
1160 my $macro;
1161 my $macro_name;
1162
1163 $type = lc($type);
1164
1165 if ($type eq 'in' || $type eq 'out') {
1166 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1167 # OK
1168 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1169 ($macro_name, $action) = ($1, $2);
1170 my $lc_macro_name = lc($macro_name);
1171 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1172 $macro_name = $preferred_name if $preferred_name;
1173 $macro = $macros->{$lc_macro_name};
1174 die "unknown macro '$macro_name'\n" if !$macro;
1175 } else {
1176 die "unknown action '$action'\n";
1177 }
1178 } elsif ($type eq 'group') {
1179 die "wrong number of rule elements\n" if scalar(@data) != 3;
1180 die "groups disabled\n" if !$allow_groups;
1181
1182 die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1183 } else {
1184 die "unknown rule type '$type'\n";
1185 }
1186
1187 if ($need_iface) {
1188 $iface = undef if $iface && $iface eq '-';
1189 }
1190
1191 $proto = undef if $proto && $proto eq '-';
1192 die "unknown protokol '$proto'\n" if $proto &&
1193 !(defined($protocols->{byname}->{$proto}) ||
1194 defined($protocols->{byid}->{$proto}));
1195
1196 $source = undef if $source && $source eq '-';
1197 $dest = undef if $dest && $dest eq '-';
1198
1199 $dport = undef if $dport && $dport eq '-';
1200 $sport = undef if $sport && $sport eq '-';
1201
1202 my $nbsource = undef;
1203 my $nbdest = undef;
1204
1205 $nbsource = parse_address_list($source) if $source;
1206 $nbdest = parse_address_list($dest) if $dest;
1207
1208 my $rules = [];
1209
1210 my $param = {
1211 type => $type,
1212 disable => $disable,
1213 comment => $comment,
1214 action => $action,
1215 iface => $iface,
1216 source => $source,
1217 dest => $dest,
1218 nbsource => $nbsource,
1219 nbdest => $nbdest,
1220 proto => $proto,
1221 dport => $dport,
1222 sport => $sport,
1223 };
1224
1225 if ($macro) {
1226 foreach my $templ (@$macro) {
1227 my $rule = {};
1228 my $param_used = {};
1229 foreach my $k (keys %$templ) {
1230 my $v = $templ->{$k};
1231 if ($v eq 'PARAM') {
1232 $v = $param->{$k};
1233 $param_used->{$k} = 1;
1234 } elsif ($v eq 'DEST') {
1235 $v = $param->{dest};
1236 $param_used->{dest} = 1;
1237 } elsif ($v eq 'SOURCE') {
1238 $v = $param->{source};
1239 $param_used->{source} = 1;
1240 }
1241
1242 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1243 $rule->{$k} = $v;
1244 }
1245 foreach my $k (keys %$param) {
1246 next if !defined($param->{$k});
1247 next if $param_used->{$k};
1248 if (defined($rule->{$k})) {
1249 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1250 if $rule->{$k} ne $param->{$k};
1251 } else {
1252 $rule->{$k} = $param->{$k};
1253 }
1254 }
1255 push @$rules, $rule;
1256 }
1257 } else {
1258 push @$rules, $param;
1259 }
1260
1261 foreach my $rule (@$rules) {
1262 $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport})
1263 if defined($rule->{dport});
1264 $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport})
1265 if defined($rule->{sport});
1266 }
1267
1268 return $rules;
1269 }
1270
1271 sub parse_vmfw_option {
1272 my ($line) = @_;
1273
1274 my ($opt, $value);
1275
1276 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1277
1278 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1279 $opt = lc($1);
1280 $value = int($2);
1281 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1282 $opt = lc($1);
1283 $value = $2 ? lc($3) : '';
1284 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1285 $opt = lc($1);
1286 $value = uc($3);
1287 } else {
1288 chomp $line;
1289 die "can't parse option '$line'\n"
1290 }
1291
1292 return ($opt, $value);
1293 }
1294
1295 sub parse_hostfw_option {
1296 my ($line) = @_;
1297
1298 my ($opt, $value);
1299
1300 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1301
1302 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1303 $opt = lc($1);
1304 $value = int($2);
1305 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1306 $opt = lc($1);
1307 $value = $2 ? lc($3) : '';
1308 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1309 $opt = lc($1);
1310 $value = uc($3);
1311 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1312 $opt = lc($1);
1313 $value = int($2);
1314 } else {
1315 chomp $line;
1316 die "can't parse option '$line'\n"
1317 }
1318
1319 return ($opt, $value);
1320 }
1321
1322 sub parse_vm_fw_rules {
1323 my ($filename, $fh) = @_;
1324
1325 my $res = { rules => [], options => {}};
1326
1327 my $section;
1328
1329 while (defined(my $line = <$fh>)) {
1330 next if $line =~ m/^#/;
1331 next if $line =~ m/^\s*$/;
1332
1333 my $linenr = $fh->input_line_number();
1334 my $prefix = "$filename (line $linenr)";
1335
1336 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1337 $section = lc($1);
1338 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1339 next;
1340 }
1341 if (!$section) {
1342 warn "$prefix: skip line - no section";
1343 next;
1344 }
1345
1346 next if !$res->{$section}; # skip undefined section
1347
1348 if ($section eq 'options') {
1349 eval {
1350 my ($opt, $value) = parse_vmfw_option($line);
1351 $res->{options}->{$opt} = $value;
1352 };
1353 warn "$prefix: $@" if $@;
1354 next;
1355 }
1356
1357 my $rules;
1358 eval { $rules = parse_fw_rule($line, 1, 1); };
1359 if (my $err = $@) {
1360 warn "$prefix: $err";
1361 next;
1362 }
1363
1364 push @{$res->{$section}}, @$rules;
1365 }
1366
1367 return $res;
1368 }
1369
1370 sub parse_host_fw_rules {
1371 my ($filename, $fh) = @_;
1372
1373 my $res = { rules => [], options => {}};
1374
1375 my $section;
1376
1377 while (defined(my $line = <$fh>)) {
1378 next if $line =~ m/^#/;
1379 next if $line =~ m/^\s*$/;
1380
1381 my $linenr = $fh->input_line_number();
1382 my $prefix = "$filename (line $linenr)";
1383
1384 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1385 $section = lc($1);
1386 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1387 next;
1388 }
1389 if (!$section) {
1390 warn "$prefix: skip line - no section";
1391 next;
1392 }
1393
1394 next if !$res->{$section}; # skip undefined section
1395
1396 if ($section eq 'options') {
1397 eval {
1398 my ($opt, $value) = parse_hostfw_option($line);
1399 $res->{options}->{$opt} = $value;
1400 };
1401 warn "$prefix: $@" if $@;
1402 next;
1403 }
1404
1405 my $rules;
1406 eval { $rules = parse_fw_rule($line, 1, 1); };
1407 if (my $err = $@) {
1408 warn "$prefix: $err";
1409 next;
1410 }
1411
1412 push @{$res->{$section}}, @$rules;
1413 }
1414
1415 return $res;
1416 }
1417
1418 sub parse_group_fw_rules {
1419 my ($filename, $fh) = @_;
1420
1421 my $section;
1422 my $group;
1423
1424 my $res = { rules => [] };
1425
1426 while (defined(my $line = <$fh>)) {
1427 next if $line =~ m/^#/;
1428 next if $line =~ m/^\s*$/;
1429
1430 my $linenr = $fh->input_line_number();
1431 my $prefix = "$filename (line $linenr)";
1432
1433 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1434 $section = 'rules';
1435 $group = lc($1);
1436 next;
1437 }
1438 if (!$section || !$group) {
1439 warn "$prefix: skip line - no section";
1440 next;
1441 }
1442
1443 my $rules;
1444 eval { $rules = parse_fw_rule($line, 0, 0); };
1445 if (my $err = $@) {
1446 warn "$prefix: $err";
1447 next;
1448 }
1449
1450 push @{$res->{$group}->{$section}}, @$rules;
1451 }
1452
1453 return $res;
1454 }
1455
1456 sub run_locked {
1457 my ($code, @param) = @_;
1458
1459 my $timeout = 10;
1460
1461 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1462
1463 die $@ if $@;
1464
1465 return $res;
1466 }
1467
1468 sub read_local_vm_config {
1469
1470 my $openvz = {};
1471 my $qemu = {};
1472
1473 my $vmdata = { openvz => $openvz, qemu => $qemu };
1474
1475 my $vmlist = PVE::Cluster::get_vmlist();
1476 return $vmdata if !$vmlist || !$vmlist->{ids};
1477 my $ids = $vmlist->{ids};
1478
1479 foreach my $vmid (keys %$ids) {
1480 next if !$vmid; # skip VE0
1481 my $d = $ids->{$vmid};
1482 next if !$d->{node} || $d->{node} ne $nodename;
1483 next if !$d->{type};
1484 if ($d->{type} eq 'openvz') {
1485 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
1486 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1487 $openvz->{$vmid} = $conf;
1488 }
1489 } elsif ($d->{type} eq 'qemu') {
1490 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1491 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1492 $qemu->{$vmid} = $conf;
1493 }
1494 }
1495 }
1496
1497 return $vmdata;
1498 };
1499
1500 sub read_vm_firewall_configs {
1501 my ($vmdata) = @_;
1502 my $vmfw_configs = {};
1503
1504 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1505 my $filename = "/etc/pve/firewall/$vmid.fw";
1506 my $fh = IO::File->new($filename, O_RDONLY);
1507 next if !$fh;
1508
1509 $vmfw_configs->{$vmid} = parse_vm_fw_rules($filename, $fh);
1510 }
1511
1512 return $vmfw_configs;
1513 }
1514
1515 sub get_option_log_level {
1516 my ($options, $k) = @_;
1517
1518 my $v = $options->{$k};
1519 $v = $default_log_level if !defined($v);
1520
1521 return undef if $v eq '' || $v eq 'nolog';
1522
1523 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1524
1525 return $v if ($v >= 0) && ($v <= 7);
1526
1527 warn "unknown log level ($k = '$v')\n";
1528
1529 return undef;
1530 }
1531
1532 sub generate_std_chains {
1533 my ($ruleset, $options) = @_;
1534
1535 my $loglevel = get_option_log_level($options, 'smurf_log_level');
1536
1537 # same as shorewall smurflog.
1538 if (defined($loglevel)) {
1539 $pve_std_chains-> {'PVEFW-smurflog'} = [
1540 "-j LOG --log-prefix \"smurfs-dropped: \" --log-level $loglevel",
1541 "-j DROP",
1542 ];
1543 } else {
1544 $pve_std_chains-> {'PVEFW-smurflog'} = [ "-j DROP" ];
1545 }
1546
1547 # same as shorewall logflags action.
1548 $loglevel = get_option_log_level($options, 'tcp_flags_log_level');
1549 if (defined($loglevel)) {
1550 $pve_std_chains-> {'PVEFW-logflags'} = [
1551 "-j LOG --log-prefix \"logflags-dropped: \" --log-level $loglevel --log-ip-options",
1552 "-j DROP",
1553 ];
1554 } else {
1555 $pve_std_chains-> {'PVEFW-logflags'} = [ "-j DROP" ];
1556 }
1557
1558 foreach my $chain (keys %$pve_std_chains) {
1559 ruleset_create_chain($ruleset, $chain);
1560 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1561 if (ref($rule)) {
1562 ruleset_generate_rule($ruleset, $chain, $rule);
1563 } else {
1564 ruleset_addrule($ruleset, $chain, $rule);
1565 }
1566 }
1567 }
1568 }
1569
1570 sub save_pvefw_status {
1571 my ($status) = @_;
1572
1573 die "unknown status '$status' - internal error"
1574 if $status !~ m/^(stopped|active)$/;
1575
1576 mkdir dirname($pve_fw_status_filename);
1577 PVE::Tools::file_set_contents($pve_fw_status_filename, $status);
1578 }
1579
1580 sub read_pvefw_status {
1581
1582 my $status = 'unknown';
1583
1584 return 'stopped' if ! -f $pve_fw_status_filename;
1585
1586 eval {
1587 $status = PVE::Tools::file_get_contents($pve_fw_status_filename);
1588 };
1589 warn $@ if $@;
1590
1591 return $status;
1592 }
1593
1594 sub compile {
1595 my $vmdata = read_local_vm_config();
1596 my $vmfw_configs = read_vm_firewall_configs($vmdata);
1597
1598 my $groups_conf = {};
1599 my $filename = "/etc/pve/firewall/groups.fw";
1600 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1601 $groups_conf = parse_group_fw_rules($filename, $fh);
1602 }
1603
1604 my $ruleset = {};
1605
1606 ruleset_create_chain($ruleset, "PVEFW-INPUT");
1607 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
1608
1609 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
1610
1611 my $hostfw_options = {};
1612 my $hostfw_conf = {};
1613
1614 $filename = "/etc/pve/local/host.fw";
1615 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1616 $hostfw_conf = parse_host_fw_rules($filename, $fh);
1617 $hostfw_options = $hostfw_conf->{options};
1618 }
1619
1620 generate_std_chains($ruleset, $hostfw_options);
1621
1622 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
1623
1624 enable_host_firewall($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
1625
1626 # generate firewall rules for QEMU VMs
1627 foreach my $vmid (keys %{$vmdata->{qemu}}) {
1628 my $conf = $vmdata->{qemu}->{$vmid};
1629 my $vmfw_conf = $vmfw_configs->{$vmid};
1630 next if !$vmfw_conf;
1631 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
1632
1633 foreach my $netid (keys %$conf) {
1634 next if $netid !~ m/^net(\d+)$/;
1635 my $net = PVE::QemuServer::parse_net($conf->{$netid});
1636 next if !$net;
1637 my $iface = "tap${vmid}i$1";
1638
1639 my $bridge = $net->{bridge};
1640 next if !$bridge; # fixme: ?
1641
1642 $bridge .= "v$net->{tag}" if $net->{tag};
1643
1644 generate_bridge_chains($ruleset, $hostfw_conf, $bridge);
1645
1646 my $macaddr = $net->{macaddr};
1647 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1648 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1649 }
1650 }
1651
1652 # generate firewall rules for OpenVZ containers
1653 foreach my $vmid (keys %{$vmdata->{openvz}}) {
1654 my $conf = $vmdata->{openvz}->{$vmid};
1655
1656 my $vmfw_conf = $vmfw_configs->{$vmid};
1657 next if !$vmfw_conf;
1658 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
1659
1660 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
1661 my $ip = $conf->{ip_address}->{value};
1662 generate_venet_rules_direction($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'IN');
1663 generate_venet_rules_direction($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'OUT');
1664 }
1665
1666 if ($conf->{netif} && $conf->{netif}->{value}) {
1667 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
1668 foreach my $netid (keys %$netif) {
1669 my $d = $netif->{$netid};
1670 my $bridge = $d->{bridge};
1671 if (!$bridge) {
1672 warn "no bridge device for CT $vmid iface '$netid'\n";
1673 next; # fixme?
1674 }
1675 my $macaddr = $d->{host_mac};
1676 my $iface = $d->{host_ifname};
1677 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1678 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1679 }
1680 }
1681 }
1682
1683 # fixme: this is an optimization? if so, we should also drop INVALID packages?
1684 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1685
1686 # fixme: what log level should we use here?
1687 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
1688
1689 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -j ACCEPT");
1690 # disable interbridge routing
1691 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
1692 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
1693 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j LOG --log-prefix \"PVEFW-FORWARD-dropped \" --log-level $loglevel");
1694 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j LOG --log-prefix \"PVEFW-FORWARD-dropped \" --log-level $loglevel");
1695 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
1696 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
1697
1698 return wantarray ? ($ruleset, $hostfw_conf) : $ruleset;
1699 }
1700
1701 sub get_ruleset_status {
1702 my ($ruleset, $verbose) = @_;
1703
1704 my $active_chains = iptables_get_chains();
1705
1706 my $statushash = {};
1707
1708 foreach my $chain (sort keys %$ruleset) {
1709 my $digest = Digest::SHA->new('sha1');
1710 foreach my $cmd (@{$ruleset->{$chain}}) {
1711 $digest->add("$cmd\n");
1712 }
1713 my $sig = $digest->b64digest;
1714 $statushash->{$chain}->{sig} = $sig;
1715
1716 my $oldsig = $active_chains->{$chain};
1717 if (!defined($oldsig)) {
1718 $statushash->{$chain}->{action} = 'create';
1719 } else {
1720 if ($oldsig eq $sig) {
1721 $statushash->{$chain}->{action} = 'exists';
1722 } else {
1723 $statushash->{$chain}->{action} = 'update';
1724 }
1725 }
1726 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1727 foreach my $cmd (@{$ruleset->{$chain}}) {
1728 print "\t$cmd\n" if $verbose;
1729 }
1730 }
1731
1732 foreach my $chain (sort keys %$active_chains) {
1733 if (!defined($ruleset->{$chain})) {
1734 my $sig = $active_chains->{$chain};
1735 $statushash->{$chain}->{action} = 'delete';
1736 $statushash->{$chain}->{sig} = $sig;
1737 print "delete $chain ($sig)\n" if $verbose;
1738 }
1739 }
1740
1741 return $statushash;
1742 }
1743
1744 sub print_ruleset {
1745 my ($ruleset) = @_;
1746
1747 get_ruleset_status($ruleset, 1);
1748 }
1749
1750 sub print_sig_rule {
1751 my ($chain, $sig) = @_;
1752
1753 # We just use this to store a SHA1 checksum used to detect changes
1754 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1755 }
1756
1757 sub get_rulset_cmdlist {
1758 my ($ruleset, $verbose) = @_;
1759
1760 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
1761
1762 my $statushash = get_ruleset_status($ruleset, $verbose);
1763
1764 # create missing chains first
1765 foreach my $chain (sort keys %$ruleset) {
1766 my $stat = $statushash->{$chain};
1767 die "internal error" if !$stat;
1768 next if $stat->{action} ne 'create';
1769
1770 $cmdlist .= ":$chain - [0:0]\n";
1771 }
1772
1773 my $rule = "INPUT -j PVEFW-INPUT";
1774 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1775 $cmdlist .= "-A $rule\n";
1776 }
1777 $rule = "OUTPUT -j PVEFW-OUTPUT";
1778 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1779 $cmdlist .= "-A $rule\n";
1780 }
1781
1782 $rule = "FORWARD -j PVEFW-FORWARD";
1783 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1784 $cmdlist .= "-A $rule\n";
1785 }
1786
1787 foreach my $chain (sort keys %$ruleset) {
1788 my $stat = $statushash->{$chain};
1789 die "internal error" if !$stat;
1790
1791 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1792 $cmdlist .= "-F $chain\n";
1793 foreach my $cmd (@{$ruleset->{$chain}}) {
1794 $cmdlist .= "$cmd\n";
1795 }
1796 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1797 } elsif ($stat->{action} eq 'delete') {
1798 die "internal error"; # this should not happen
1799 } elsif ($stat->{action} eq 'exists') {
1800 # do nothing
1801 } else {
1802 die "internal error - unknown status '$stat->{action}'";
1803 }
1804 }
1805
1806 foreach my $chain (keys %$statushash) {
1807 next if $statushash->{$chain}->{action} ne 'delete';
1808 $cmdlist .= "-F $chain\n";
1809 }
1810 foreach my $chain (keys %$statushash) {
1811 next if $statushash->{$chain}->{action} ne 'delete';
1812 next if $chain eq 'PVEFW-INPUT';
1813 next if $chain eq 'PVEFW-OUTPUT';
1814 next if $chain eq 'PVEFW-FORWARD';
1815 $cmdlist .= "-X $chain\n";
1816 }
1817
1818 $cmdlist .= "COMMIT\n";
1819
1820 return $cmdlist;
1821 }
1822
1823 sub apply_ruleset {
1824 my ($ruleset, $verbose) = @_;
1825
1826 enable_bridge_firewall();
1827
1828 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
1829
1830 print $cmdlist if $verbose;
1831
1832 iptables_restore_cmdlist($cmdlist);
1833
1834 # test: re-read status and check if everything is up to date
1835 my $statushash = get_ruleset_status($ruleset);
1836
1837 my $errors;
1838 foreach my $chain (sort keys %$ruleset) {
1839 my $stat = $statushash->{$chain};
1840 if ($stat->{action} ne 'exists') {
1841 warn "unable to update chain '$chain'\n";
1842 $errors = 1;
1843 }
1844 }
1845
1846 die "unable to apply firewall changes\n" if $errors;
1847 }
1848
1849 sub update_nf_conntrack_max {
1850 my ($hostfw_conf) = @_;
1851
1852 my $max = 65536; # reasonable default
1853
1854 my $options = $hostfw_conf->{options} || {};
1855
1856 if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) {
1857 $max = $options->{nf_conntrack_max};
1858 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
1859 }
1860
1861 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
1862 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
1863
1864 my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max);
1865
1866 if ($current != $max) {
1867 my $hashsize = int($max/4);
1868 PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize);
1869 PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max);
1870 }
1871 }
1872
1873 sub update {
1874 my ($start, $verbose) = @_;
1875
1876 my $code = sub {
1877 my $status = read_pvefw_status();
1878
1879 my ($ruleset, $hostfw_conf) = PVE::Firewall::compile();
1880
1881 update_nf_conntrack_max($hostfw_conf);
1882
1883 if ($start || $status eq 'active') {
1884
1885 save_pvefw_status('active') if ($status ne 'active');
1886
1887 PVE::Firewall::apply_ruleset($ruleset, $verbose);
1888 } else {
1889 print "Firewall not active (status = $status)\n" if $verbose;
1890 }
1891 };
1892
1893 run_locked($code);
1894 }
1895
1896
1897 1;