8 use PVE
::JSONSchema
qw(get_standard_option);
16 use PVE
::Tools
qw(run_command lock_file);
19 # dynamically include PVE::QemuServer and PVE::OpenVZ
20 # to avoid dependency problems
23 require PVE
::QemuServer
;
24 $have_qemu_server = 1;
30 $have_pve_manager = 1;
35 my $nodename = PVE
::INotify
::nodename
();
37 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
38 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
40 my $default_log_level = 'info';
42 my $log_level_hash = {
53 # imported/converted from: /usr/share/shorewall/macro.*
56 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
57 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
60 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
63 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
66 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
67 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
70 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
71 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
74 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
77 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
78 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
79 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
82 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
83 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
86 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
89 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
90 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
93 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
94 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
97 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
100 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
101 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
104 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
107 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
110 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
111 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
112 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
113 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
116 { action
=> 'PARAM', proto
=> '47' },
117 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
120 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
123 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
124 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
127 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
130 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
133 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
136 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
139 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
142 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
145 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
148 { action
=> 'PARAM', proto
=> '94' },
149 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
152 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
155 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
158 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
159 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
162 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
163 { action
=> 'PARAM', proto
=> '50' },
164 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
165 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
168 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
169 { action
=> 'PARAM', proto
=> '51' },
170 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
171 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
174 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
175 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
176 { action
=> 'PARAM', proto
=> '50' },
177 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
178 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
179 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
182 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
185 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
188 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
191 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
194 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
197 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
198 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
201 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
204 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
207 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
210 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
213 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
214 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
215 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
218 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
221 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
224 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
227 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
230 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
233 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
234 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
237 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
238 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
241 { action
=> 'PARAM', proto
=> '89' },
244 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
247 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
248 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
251 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
254 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
257 { action
=> 'PARAM', proto
=> '47' },
258 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
259 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
262 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
265 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
268 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
271 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
274 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
275 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
278 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
281 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
284 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
287 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
290 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
293 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
294 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
295 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
296 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
299 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
300 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
301 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
302 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
303 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
304 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
305 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
306 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
309 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
312 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
315 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
318 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
319 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
322 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
325 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
328 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
331 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
332 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
333 { action
=> 'PARAM', proto
=> '41' },
334 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
337 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
340 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
343 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
344 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
347 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
350 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
353 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
356 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
359 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
360 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
363 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
366 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
369 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
370 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
373 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
376 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
379 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
383 my $pve_fw_parsed_macros;
384 my $pve_fw_preferred_macro_names = {};
386 my $pve_std_chains = {
387 'PVEFW-SET-ACCEPT-MARK' => [
388 "-j MARK --set-mark 1",
390 'PVEFW-DropBroadcast' => [
391 # same as shorewall 'Broadcast'
392 # simply DROP BROADCAST/MULTICAST/ANYCAST
393 # we can use this to reduce logging
394 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
395 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
396 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
397 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
400 # same as shorewall 'reject'
401 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
402 { action
=> 'DROP', source
=> '224.0.0.0/4' },
403 { action
=> 'DROP', proto
=> 'icmp' },
404 "-p tcp -j REJECT --reject-with tcp-reset",
405 "-p udp -j REJECT --reject-with icmp-port-unreachable",
406 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
407 "-j REJECT --reject-with icmp-host-prohibited",
410 # same as shorewall 'Drop', which is equal to DROP,
411 # but REJECT/DROP some packages to reduce logging,
412 # and ACCEPT critical ICMP types
413 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
414 # we are not interested in BROADCAST/MULTICAST/ANYCAST
415 { action
=> 'PVEFW-DropBroadcast' },
416 # ACCEPT critical ICMP types
417 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
418 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
419 # Drop packets with INVALID state
420 "-m conntrack --ctstate INVALID -j DROP",
421 # Drop Microsoft SMB noise
422 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
423 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
424 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
425 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
426 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
427 # Drop new/NotSyn traffic so that it doesn't get logged
428 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
430 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
433 # same as shorewall 'Reject', which is equal to Reject,
434 # but REJECT/DROP some packages to reduce logging,
435 # and ACCEPT critical ICMP types
436 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
437 # we are not interested in BROADCAST/MULTICAST/ANYCAST
438 { action
=> 'PVEFW-DropBroadcast' },
439 # ACCEPT critical ICMP types
440 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
441 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
442 # Drop packets with INVALID state
443 "-m conntrack --ctstate INVALID -j DROP",
444 # Drop Microsoft SMB noise
445 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
446 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
447 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
448 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
449 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
450 # Drop new/NotSyn traffic so that it doesn't get logged
451 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
453 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
455 'PVEFW-tcpflags' => [
456 # same as shorewall tcpflags action.
457 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
458 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
459 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
460 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
461 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
462 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
465 # same as shorewall smurfs action
466 # Filter packets for smurfs (packets with a broadcast address as the source).
467 "-s 0.0.0.0/32 -j RETURN",
468 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
469 "-s 224.0.0.0/4 -g PVEFW-smurflog",
473 # iptables -p icmp -h
474 my $icmp_type_names = {
477 'destination-unreachable' => 1,
478 'network-unreachable' => 1,
479 'host-unreachable' => 1,
480 'protocol-unreachable' => 1,
481 'port-unreachable' => 1,
482 'fragmentation-needed' => 1,
483 'source-route-failed' => 1,
484 'network-unknown' => 1,
486 'network-prohibited' => 1,
487 'host-prohibited' => 1,
488 'TOS-network-unreachable' => 1,
489 'TOS-host-unreachable' => 1,
490 'communication-prohibited' => 1,
491 'host-precedence-violation' => 1,
492 'precedence-cutoff' => 1,
493 'source-quench' => 1,
495 'network-redirect' => 1,
496 'host-redirect' => 1,
497 'TOS-network-redirect' => 1,
498 'TOS-host-redirect' => 1,
500 'router-advertisement' => 1,
501 'router-solicitation' => 1,
502 'time-exceeded' => 1,
503 'ttl-zero-during-transit' => 1,
504 'ttl-zero-during-reassembly' => 1,
505 'parameter-problem' => 1,
506 'ip-header-bad' => 1,
507 'required-option-missing' => 1,
508 'timestamp-request' => 1,
509 'timestamp-reply' => 1,
510 'address-mask-request' => 1,
511 'address-mask-reply' => 1,
514 sub init_firewall_macros
{
516 $pve_fw_parsed_macros = {};
518 foreach my $k (keys %$pve_fw_macros) {
519 my $lc_name = lc($k);
520 my $macro = $pve_fw_macros->{$k};
521 $pve_fw_preferred_macro_names->{$lc_name} = $k;
522 $pve_fw_parsed_macros->{$k} = $macro;
526 init_firewall_macros
();
530 sub get_etc_services
{
532 return $etc_services if $etc_services;
534 my $filename = "/etc/services";
536 my $fh = IO
::File-
>new($filename, O_RDONLY
);
538 warn "unable to read '$filename' - $!\n";
544 while (my $line = <$fh>) {
546 next if $line =~m/^#/;
547 next if ($line =~m/^\s*$/);
549 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
550 $services->{byid
}->{$2}->{name
} = $1;
551 $services->{byid
}->{$2}->{port
} = $2;
552 $services->{byid
}->{$2}->{$3} = 1;
553 $services->{byname
}->{$1} = $services->{byid
}->{$2};
559 $etc_services = $services;
562 return $etc_services;
567 sub get_etc_protocols
{
568 return $etc_protocols if $etc_protocols;
570 my $filename = "/etc/protocols";
572 my $fh = IO
::File-
>new($filename, O_RDONLY
);
574 warn "unable to read '$filename' - $!\n";
580 while (my $line = <$fh>) {
582 next if $line =~m/^#/;
583 next if ($line =~m/^\s*$/);
585 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
586 $protocols->{byid
}->{$2}->{name
} = $1;
587 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
593 $etc_protocols = $protocols;
595 return $etc_protocols;
598 sub parse_address_list
{
602 foreach my $aor (split(/,/, $str)) {
603 if (!Net
::IP-
>new($aor)) {
604 my $err = Net
::IP
::Error
();
605 die "invalid IP address: $err\n";
613 sub parse_port_name_number_or_range
{
616 my $services = PVE
::Firewall
::get_etc_services
();
618 foreach my $item (split(/,/, $str)) {
622 foreach my $pon (split(':', $item, 2)) {
623 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
624 if ($pon =~ m/^\d+$/){
625 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
626 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
629 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
637 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
638 sub pve_fw_verify_port_spec
{
641 parse_port_name_number_or_range
($portstr);
646 PVE
::JSONSchema
::register_format
('pve-fw-v4addr-spec', \
&pve_fw_verify_v4addr_spec
);
647 sub pve_fw_verify_v4addr_spec
{
650 parse_address_list
($list);
655 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
656 sub pve_fw_verify_protocol_spec
{
659 my $protocols = get_etc_protocols
();
661 die "unknown protocol '$proto'\n" if $proto &&
662 !(defined($protocols->{byname
}->{$proto}) ||
663 defined($protocols->{byid
}->{$proto}));
669 # helper function for API
671 my $rule_properties = {
673 description
=> "Update rule at position <pos>.",
687 enum
=> ['in', 'out', 'group'],
692 enum
=> ['ACCEPT', 'DROP', 'REJECT'],
699 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
701 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
705 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
709 type
=> 'string', format
=> 'pve-fw-protocol-spec',
717 type
=> 'string', format
=> 'pve-fw-port-spec',
721 type
=> 'string', format
=> 'pve-fw-port-spec',
730 sub cleanup_fw_rule
{
731 my ($rule, $digest, $pos) = @_;
735 foreach my $k (keys %$rule) {
736 next if !$rule_properties->{$k};
738 next if !defined($v);
740 $r->{digest
} = $digest;
747 sub add_rule_properties
{
748 my ($properties) = @_;
750 foreach my $k (keys %$rule_properties) {
751 $properties->{$k} = $rule_properties->{$k};
758 my ($rule, $param) = @_;
760 foreach my $k (keys %$rule_properties) {
761 if (defined(my $v = $param->{$k})) {
762 if ($v eq '' || $v eq '-') {
775 my $bridge_firewall_enabled = 0;
777 sub enable_bridge_firewall
{
779 return if $bridge_firewall_enabled; # only once
781 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
782 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
784 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
785 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
787 $bridge_firewall_enabled = 1;
790 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
795 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
798 sub iptables_restore_cmdlist
{
801 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
804 sub iptables_get_chains
{
808 # check what chains we want to track
809 my $is_pvefw_chain = sub {
812 return 1 if $name =~ m/^PVEFW-\S+$/;
814 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
816 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
818 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
820 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT|IPS)$/;
821 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
831 return if $line =~ m/^#/;
832 return if $line =~ m/^\s*$/;
834 if ($line =~ m/^\*(\S+)$/) {
839 return if $table ne 'filter';
841 if ($line =~ m/^:(\S+)\s/) {
843 return if !&$is_pvefw_chain($chain);
844 $res->{$chain} = "unknown";
845 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
846 my ($chain, $sig) = ($1, $2);
847 return if !&$is_pvefw_chain($chain);
848 $res->{$chain} = $sig;
850 # simply ignore the rest
855 run_command
("/sbin/iptables-save", outfunc
=> $parser);
860 sub iptables_chain_exist
{
864 iptables
("-n --list $chain");
871 sub iptables_rule_exist
{
875 iptables
("-C $rule");
882 sub ruleset_generate_cmdstr
{
883 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
885 return if defined($rule->{enable
}) && !$rule->{enable
};
887 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
889 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
890 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
891 my $nbsource = $rule->{source
} ? parse_address_list
( $rule->{source
}) : 0;
892 my $nbdest = $rule->{dest
} ? parse_address_list
($rule->{dest
}) : 0;
896 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
897 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
899 push @cmd, "-m iprange --src-range" if $nbsource > 1;
900 push @cmd, "-s $rule->{source}" if $rule->{source
};
901 push @cmd, "-m iprange --dst-range" if $nbdest > 1;
902 push @cmd, "-d $rule->{dest}" if $rule->{dest
};
904 if ($rule->{proto
}) {
905 push @cmd, "-p $rule->{proto}";
908 $multiport++ if $nbdport > 1;
909 $multiport++ if $nbsport > 1;
911 push @cmd, "--match multiport" if $multiport;
913 die "multiport: option '--sports' cannot be used together with '--dports'\n"
914 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
916 if ($rule->{dport
}) {
917 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
918 # Note: we use dport to store --icmp-type
919 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
920 push @cmd, "-m icmp --icmp-type $rule->{dport}";
923 if ($multiport == 2) {
924 push @cmd, "--ports $rule->{dport}";
926 push @cmd, "--dports $rule->{dport}";
929 push @cmd, "--dport $rule->{dport}";
934 if ($rule->{sport
}) {
936 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
938 push @cmd, "--sport $rule->{sport}";
941 } elsif ($rule->{dport
} || $rule->{sport
}) {
942 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
943 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
946 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
948 if (my $action = $rule->{action
}) {
949 $action = $actions->{$action} if defined($actions->{$action});
950 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
951 push @cmd, $goto ?
"-g $action" : "-j $action";
954 return scalar(@cmd) ?
join(' ', @cmd) : undef;
957 my $apply_macro = sub {
958 my ($macro_name, $param) = @_;
960 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
961 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
965 foreach my $templ (@$macro_rules) {
968 foreach my $k (keys %$templ) {
969 my $v = $templ->{$k};
972 $param_used->{$k} = 1;
973 } elsif ($v eq 'DEST') {
975 $param_used->{dest
} = 1;
976 } elsif ($v eq 'SOURCE') {
977 $v = $param->{source
};
978 $param_used->{source
} = 1;
981 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
984 foreach my $k (keys %$param) {
985 next if $k eq 'macro';
986 next if !defined($param->{$k});
987 next if $param_used->{$k};
988 if (defined($rule->{$k})) {
989 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
990 if $rule->{$k} ne $param->{$k};
992 $rule->{$k} = $param->{$k};
1001 sub ruleset_generate_rule
{
1002 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1006 if ($rule->{macro}) {
1007 $rules = &$apply_macro($rule->{macro}, $rule);
1012 foreach my $tmp (@$rules) {
1013 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $tmp, $actions, $goto)) {
1014 ruleset_addrule
($ruleset, $chain, $cmdstr);
1019 sub ruleset_generate_rule_insert
{
1020 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1022 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1024 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
1025 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1029 sub ruleset_create_chain
{
1030 my ($ruleset, $chain) = @_;
1032 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1033 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1035 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1037 $ruleset->{$chain} = [];
1040 sub ruleset_chain_exist
{
1041 my ($ruleset, $chain) = @_;
1043 return $ruleset->{$chain} ?
1 : undef;
1046 sub ruleset_addrule
{
1047 my ($ruleset, $chain, $rule) = @_;
1049 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1051 push @{$ruleset->{$chain}}, "-A $chain $rule";
1054 sub ruleset_insertrule
{
1055 my ($ruleset, $chain, $rule) = @_;
1057 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1059 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1062 sub get_log_rule_base
{
1063 my ($chain, $vmid, $msg, $loglevel) = @_;
1065 die "internal error - no log level" if !defined($loglevel);
1067 $vmid = 0 if !defined($vmid);
1069 # Note: we use special format for prefix to pass further
1070 # info to log daemon (VMID, LOGVELEL and CHAIN)
1072 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1075 sub ruleset_addlog {
1076 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1078 return if !defined($loglevel);
1080 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1082 $logrule = "$rule $logrule" if defined($rule);
1084 ruleset_addrule($ruleset, $chain, $logrule)
1087 sub generate_bridge_chains {
1088 my ($ruleset, $hostfw_conf, $bridge, $routing_table) = @_;
1090 my $options = $hostfw_conf->{options} || {};
1092 die "error
: detected direct route to bridge
'$bridge'\n"
1093 if !$options->{allow_bridge_route} && $routing_table->{$bridge};
1095 if (!ruleset_chain_exist($ruleset, "$bridge-FW
")) {
1096 ruleset_create_chain($ruleset, "$bridge-FW
");
1097 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-FW
");
1098 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-FW
");
1101 if (!ruleset_chain_exist($ruleset, "$bridge-OUT
")) {
1102 ruleset_create_chain($ruleset, "$bridge-OUT
");
1103 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-in
-j
$bridge-OUT
");
1104 ruleset_insertrule($ruleset, "PVEFW-INPUT
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-OUT
");
1107 if (!ruleset_chain_exist($ruleset, "$bridge-IN
")) {
1108 ruleset_create_chain($ruleset, "$bridge-IN
");
1109 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j
$bridge-IN
");
1110 ruleset_addrule($ruleset, "$bridge-FW
", "-m mark
--mark
1 -j ACCEPT
");
1111 # accept traffic to unmanaged bridge ports
1112 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j ACCEPT
");
1116 sub ruleset_add_chain_policy {
1117 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
1119 if ($policy eq 'ACCEPT') {
1121 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
1122 { ACCEPT => $accept_action});
1124 } elsif ($policy eq 'DROP') {
1126 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1128 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1130 ruleset_addrule($ruleset, $chain, "-j DROP
");
1131 } elsif ($policy eq 'REJECT') {
1132 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1134 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1136 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1139 die "internal error
: unknown policy
'$policy'";
1143 sub ruleset_create_vm_chain {
1144 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
1146 ruleset_create_chain($ruleset, $chain);
1147 my $accept = generate_nfqueue($options);
1149 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1150 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1153 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1154 if ($direction eq 'OUT') {
1155 ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK',
1156 proto => 'udp', sport => 68, dport => 67 });
1158 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT',
1159 proto => 'udp', sport => 67, dport => 68 });
1163 if ($options->{tcpflags}) {
1164 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1167 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1168 if($direction eq 'OUT'){
1169 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-g PVEFW-SET-ACCEPT-MARK
");
1172 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1174 if ($direction eq 'OUT') {
1175 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1176 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1178 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1184 sub ruleset_generate_vm_rules {
1185 my ($ruleset, $rules, $groups_conf, $chain, $netid, $direction, $options) = @_;
1187 my $lc_direction = lc($direction);
1189 foreach my $rule (@$rules) {
1190 next if $rule->{iface} && $rule->{iface} ne $netid;
1191 next if !$rule->{enable};
1192 if ($rule->{type} eq 'group') {
1193 my $group_chain = "GROUP-
$rule->{action
}-$direction";
1194 if(!ruleset_chain_exist($ruleset, $group_chain)){
1195 generate_group_rules($ruleset, $groups_conf, $rule->{action});
1197 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1198 if ($direction eq 'OUT'){
1199 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j RETURN
");
1201 my $accept = generate_nfqueue($options);
1202 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$accept");
1206 next if $rule->{type} ne $lc_direction;
1207 if ($direction eq 'OUT') {
1208 ruleset_generate_rule($ruleset, $chain, $rule,
1209 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1211 my $accept = generate_nfqueue($options);
1212 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject
" });
1218 sub generate_nfqueue {
1222 if($options->{ips}){
1223 $action = "NFQUEUE
";
1224 if($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1225 if(defined($3) && defined($1)) {
1226 $action .= " --queue-balance
$1:$3";
1227 }elsif (defined($1)) {
1228 $action .= " --queue-num
$1";
1231 $action .= " --queue-bypass
";
1239 sub ruleset_generate_vm_ipsrules {
1240 my ($ruleset, $options, $direction, $iface, $bridge) = @_;
1242 if ($options->{ips} && $direction eq 'IN') {
1243 my $nfqueue = generate_nfqueue($options);
1245 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1246 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1249 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1250 ruleset_create_chain($ruleset, "$bridge-IPS
");
1251 ruleset_insertrule($ruleset, "PVEFW-IPS
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-IPS
");
1254 ruleset_addrule($ruleset, "$bridge-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1258 sub generate_venet_rules_direction {
1259 my ($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
1261 parse_address_list($ip); # make sure we have a valid $ip list
1263 my $lc_direction = lc($direction);
1265 my $rules = $vmfw_conf->{rules};
1267 my $options = $vmfw_conf->{options};
1268 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1270 my $chain = "venet0-
$vmid-$direction";
1272 ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction);
1274 ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $chain, 'venet', $direction);
1279 if ($direction eq 'OUT') {
1280 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1282 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1285 my $accept = generate_nfqueue($options);
1286 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1287 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1289 # plug into FORWARD, INPUT and OUTPUT chain
1290 if ($direction eq 'OUT') {
1291 ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD
", {
1294 iface_in => 'venet0'});
1296 ruleset_generate_rule_insert($ruleset, "PVEFW-INPUT
", {
1299 iface_in => 'venet0'});
1301 ruleset_generate_rule($ruleset, "PVEFW-FORWARD
", {
1304 iface_out => 'venet0'});
1306 ruleset_generate_rule($ruleset, "PVEFW-OUTPUT
", {
1309 iface_out => 'venet0'});
1313 sub generate_tap_rules_direction {
1314 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_;
1316 my $lc_direction = lc($direction);
1318 my $rules = $vmfw_conf->{rules};
1320 my $options = $vmfw_conf->{options};
1321 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1323 my $tapchain = "$iface-$direction";
1325 ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction);
1327 ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $tapchain, $netid, $direction, $options);
1329 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface, $bridge);
1334 if ($direction eq 'OUT') {
1335 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1337 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1340 my $accept = generate_nfqueue($options);
1341 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1342 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
1344 # plug the tap chain to bridge chain
1345 if ($direction eq 'IN') {
1346 ruleset_insertrule($ruleset, "$bridge-IN
",
1347 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
1349 ruleset_insertrule($ruleset, "$bridge-OUT
",
1350 "-m physdev
--physdev-in
$iface -j
$tapchain");
1354 sub enable_host_firewall {
1355 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
1357 # fixme: allow security groups
1359 my $options = $hostfw_conf->{options};
1360 my $rules = $hostfw_conf->{rules};
1362 # host inbound firewall
1363 my $chain = "PVEFW-HOST-IN
";
1364 ruleset_create_chain($ruleset, $chain);
1366 my $loglevel = get_option_log_level($options, "log_level_in
");
1368 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1369 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1372 if ($options->{tcpflags}) {
1373 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1376 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1377 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1378 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
1379 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1380 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1381 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1383 # we use RETURN because we need to check also tap rules
1384 my $accept_action = 'RETURN';
1386 foreach my $rule (@$rules) {
1387 next if $rule->{type} ne 'in';
1388 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1391 # implement input policy
1392 my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1393 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1395 # host outbound firewall
1396 $chain = "PVEFW-HOST-OUT
";
1397 ruleset_create_chain($ruleset, $chain);
1399 $loglevel = get_option_log_level($options, "log_level_out
");
1401 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1402 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1403 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
1404 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1405 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1406 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1408 # we use RETURN because we may want to check other thigs later
1409 $accept_action = 'RETURN';
1411 foreach my $rule (@$rules) {
1412 next if $rule->{type} ne 'out';
1413 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1416 # implement output policy
1417 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1418 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1420 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
1421 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
1424 sub generate_group_rules {
1425 my ($ruleset, $groups_conf, $group) = @_;
1426 die "no such security group
'$group'\n" if !$groups_conf->{rules}->{$group};
1428 my $rules = $groups_conf->{rules}->{$group};
1430 my $chain = "GROUP-
${group
}-IN
";
1432 ruleset_create_chain($ruleset, $chain);
1433 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1435 foreach my $rule (@$rules) {
1436 next if $rule->{type} ne 'in';
1437 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1440 $chain = "GROUP-
${group
}-OUT
";
1442 ruleset_create_chain($ruleset, $chain);
1443 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1445 foreach my $rule (@$rules) {
1446 next if $rule->{type} ne 'out';
1447 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1448 # check also other tap rules later
1449 ruleset_generate_rule($ruleset, $chain, $rule,
1450 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" });
1455 my $valid_netdev_names = {};
1456 for (my $i = 0; $i < $MAX_NETS; $i++) {
1457 $valid_netdev_names->{"net
$i"} = 1;
1461 my ($line, $need_iface, $allow_groups) = @_;
1463 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1465 # we can add single line comments to the end of the rule
1466 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
1468 # we can disable a rule when prefixed with '|'
1471 $enable = 0 if $line =~ s/^\|//;
1473 my @data = split(/\s+/, $line);
1474 my $expected_elements = $need_iface ? 8 : 7;
1476 die "wrong number of rule elements
\n" if scalar(@data) > $expected_elements;
1479 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1481 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1484 die "incomplete rule
\n" if ! ($type && $action);
1490 if ($type eq 'in' || $type eq 'out') {
1491 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1493 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1495 my $preferred_name = $pve_fw_preferred_macro_names->{lc($1)};
1496 die "unknown
macro '$1'\n" if !$preferred_name;
1497 $macro = $preferred_name;
1499 die "unknown action
'$action'\n";
1501 } elsif ($type eq 'group') {
1502 die "wrong number of rule elements
\n" if scalar(@data) != 3;
1503 die "groups disabled
\n" if !$allow_groups;
1505 die "invalid characters
in group name
\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1507 die "unknown rule type
'$type'\n";
1511 $iface = undef if $iface && $iface eq '-';
1514 $proto = undef if $proto && $proto eq '-';
1515 pve_fw_verify_protocol_spec($proto) if $proto;
1517 $source = undef if $source && $source eq '-';
1518 $dest = undef if $dest && $dest eq '-';
1520 $dport = undef if $dport && $dport eq '-';
1521 $sport = undef if $sport && $sport eq '-';
1523 parse_port_name_number_or_range($dport) if defined($dport);
1524 parse_port_name_number_or_range($sport) if defined($sport);
1526 parse_address_list($source) if $source;
1527 parse_address_list($dest) if $dest;
1532 comment => $comment,
1544 sub parse_vmfw_option {
1549 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
1551 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags|ips):\s*(0|1)\s*$/i) {
1554 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1556 $value = $2 ? lc($3) : '';
1557 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1560 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
1565 die "can
't parse option '$line'\n"
1568 return ($opt, $value);
1571 sub parse_hostfw_option {
1576 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1578 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags|allow_bridge_route|optimize):\s*(0|1)\s*$/i) {
1581 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1583 $value = $2 ? lc($3) : '';
1584 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1587 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1592 die "can't parse option
'$line'\n"
1595 return ($opt, $value);
1598 sub parse_vm_fw_rules {
1599 my ($filename, $fh) = @_;
1601 my $res = { rules => [], options => {}};
1605 my $digest = Digest::SHA->new('sha1');
1607 while (defined(my $line = <$fh>)) {
1608 $digest->add($line);
1610 next if $line =~ m/^#/;
1611 next if $line =~ m/^\s*$/;
1613 my $linenr = $fh->input_line_number();
1614 my $prefix = "$filename (line
$linenr)";
1616 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1618 warn "$prefix: ignore unknown section
'$section'\n" if !$res->{$section};
1622 warn "$prefix: skip line
- no section
";
1626 next if !$res->{$section}; # skip undefined section
1628 if ($section eq 'options') {
1630 my ($opt, $value) = parse_vmfw_option($line);
1631 $res->{options}->{$opt} = $value;
1633 warn "$prefix: $@" if $@;
1638 eval { $rule = parse_fw_rule($line, 1, 1); };
1640 warn "$prefix: $err";
1644 push @{$res->{$section}}, $rule;
1647 $res->{digest} = $digest->b64digest;
1652 sub parse_host_fw_rules {
1653 my ($filename, $fh) = @_;
1655 my $res = { rules => [], options => {}};
1659 my $digest = Digest::SHA->new('sha1');
1661 while (defined(my $line = <$fh>)) {
1662 $digest->add($line);
1664 next if $line =~ m/^#/;
1665 next if $line =~ m/^\s*$/;
1667 my $linenr = $fh->input_line_number();
1668 my $prefix = "$filename (line
$linenr)";
1670 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1672 warn "$prefix: ignore unknown section
'$section'\n" if !$res->{$section};
1676 warn "$prefix: skip line
- no section
";
1680 next if !$res->{$section}; # skip undefined section
1682 if ($section eq 'options') {
1684 my ($opt, $value) = parse_hostfw_option($line);
1685 $res->{options}->{$opt} = $value;
1687 warn "$prefix: $@" if $@;
1692 eval { $rule = parse_fw_rule($line, 1, 1); };
1694 warn "$prefix: $err";
1698 push @{$res->{$section}}, $rule;
1701 $res->{digest} = $digest->b64digest;
1706 sub parse_group_fw_rules {
1707 my ($filename, $fh) = @_;
1712 my $res = { rules => {} };
1714 my $digest = Digest::SHA->new('sha1');
1716 while (defined(my $line = <$fh>)) {
1717 $digest->add($line);
1719 next if $line =~ m/^#/;
1720 next if $line =~ m/^\s*$/;
1722 my $linenr = $fh->input_line_number();
1723 my $prefix = "$filename (line
$linenr)";
1725 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1730 if (!$section || !$group) {
1731 warn "$prefix: skip line
- no section
";
1736 eval { $rule = parse_fw_rule($line, 0, 0); };
1738 warn "$prefix: $err";
1742 push @{$res->{$section}->{$group}}, $rule;
1745 $res->{digest} = $digest->b64digest;
1751 my ($code, @param) = @_;
1755 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1762 sub read_local_vm_config {
1767 my $vmdata = { openvz => $openvz, qemu => $qemu };
1769 my $vmlist = PVE::Cluster::get_vmlist();
1770 return $vmdata if !$vmlist || !$vmlist->{ids};
1771 my $ids = $vmlist->{ids};
1773 foreach my $vmid (keys %$ids) {
1774 next if !$vmid; # skip VE0
1775 my $d = $ids->{$vmid};
1776 next if !$d->{node} || $d->{node} ne $nodename;
1777 next if !$d->{type};
1778 if ($d->{type} eq 'openvz') {
1779 if ($have_pve_manager) {
1780 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
1781 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1782 $openvz->{$vmid} = $conf;
1785 } elsif ($d->{type} eq 'qemu') {
1786 if ($have_qemu_server) {
1787 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1788 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1789 $qemu->{$vmid} = $conf;
1798 sub load_vmfw_conf {
1803 my $filename = "/etc/pve
/firewall/$vmid.fw
";
1804 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1805 $vmfw_conf = parse_vm_fw_rules($filename, $fh);
1811 sub read_vm_firewall_configs {
1813 my $vmfw_configs = {};
1815 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1816 my $vmfw_conf = load_vmfw_conf($vmid);
1817 next if !$vmfw_conf->{options}; # skip if file does not exists
1818 $vmfw_configs->{$vmid} = $vmfw_conf;
1821 return $vmfw_configs;
1824 sub get_option_log_level {
1825 my ($options, $k) = @_;
1827 my $v = $options->{$k};
1828 $v = $default_log_level if !defined($v);
1830 return undef if $v eq '' || $v eq 'nolog';
1832 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1834 return $v if ($v >= 0) && ($v <= 7);
1836 warn "unknown
log level
($k = '$v')\n";
1841 sub generate_std_chains {
1842 my ($ruleset, $options) = @_;
1844 my $loglevel = get_option_log_level($options, 'smurf_log_level');
1846 # same as shorewall smurflog.
1847 my $chain = 'PVEFW-smurflog';
1849 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP
: ", $loglevel) if $loglevel;
1850 push @{$pve_std_chains->{$chain}}, "-j DROP
";
1852 # same as shorewall logflags action.
1853 $loglevel = get_option_log_level($options, 'tcp_flags_log_level');
1854 $chain = 'PVEFW-logflags';
1855 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
1856 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP
: ", $loglevel) if $loglevel;
1857 push @{$pve_std_chains->{$chain}}, "-j DROP
";
1859 foreach my $chain (keys %$pve_std_chains) {
1860 ruleset_create_chain($ruleset, $chain);
1861 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1863 ruleset_generate_rule($ruleset, $chain, $rule);
1865 ruleset_addrule($ruleset, $chain, $rule);
1871 sub save_pvefw_status {
1874 die "unknown status
'$status' - internal error
"
1875 if $status !~ m/^(stopped|active)$/;
1877 mkdir dirname($pve_fw_status_filename);
1878 PVE::Tools::file_set_contents($pve_fw_status_filename, $status);
1881 sub read_pvefw_status {
1883 my $status = 'unknown';
1885 return 'stopped' if ! -f $pve_fw_status_filename;
1888 $status = PVE::Tools::file_get_contents($pve_fw_status_filename);
1895 # fixme: move to pve-common PVE::ProcFSTools
1896 sub read_proc_net_route {
1897 my $filename = "/proc/net
/route
";
1901 my $fh = IO::File->new ($filename, "r
");
1902 return $res if !$fh;
1904 my $int_to_quad = sub {
1905 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
1908 while (defined(my $line = <$fh>)) {
1909 next if $line =~/^Iface\s+Destination/; # skip head
1910 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
1911 push @{$res->{$iface}}, {
1912 dest => &$int_to_quad(hex($dest)),
1913 gateway => &$int_to_quad(hex($gateway)),
1914 mask => &$int_to_quad(hex($mask)),
1923 sub load_security_groups {
1925 my $groups_conf = {};
1926 my $filename = "/etc/pve
/firewall/groups
.fw
";
1927 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1928 $groups_conf = parse_group_fw_rules($filename, $fh);
1931 return $groups_conf;
1934 sub save_security_groups {
1935 my ($groups_conf) = @_;
1938 my $filename = "/etc/pve
/firewall/groups
.fw
";
1940 foreach my $group (sort keys %{$groups_conf->{rules}}) {
1941 my $rules = $groups_conf->{rules}->{$group};
1942 $raw .= "[group
$group]\n\n";
1944 foreach my $rule (@$rules) {
1945 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
1946 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
1947 $raw .= uc($rule->{type});
1948 $raw .= " " . $rule->{action};
1949 $raw .= " " . ($rule->{source} || '-');
1950 $raw .= " " . ($rule->{dest} || '-');
1951 $raw .= " " . ($rule->{proto} || '-');
1952 $raw .= " " . ($rule->{dport} || '-');
1953 $raw .= " " . ($rule->{sport} || '-');
1954 $raw .= " # " . encode('utf8', $rule->{comment})
1955 if $rule->{comment
} && $rule->{comment
} !~ m/^\s*$/;
1958 die "implement me '$rule->{type}'";
1965 PVE
::Tools
::file_set_contents
($filename, $raw);
1968 sub load_hostfw_conf
{
1970 my $hostfw_conf = {};
1971 my $filename = "/etc/pve/local/host.fw";
1972 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1973 $hostfw_conf = parse_host_fw_rules
($filename, $fh);
1975 return $hostfw_conf;
1979 my $vmdata = read_local_vm_config
();
1980 my $vmfw_configs = read_vm_firewall_configs
($vmdata);
1982 my $routing_table = read_proc_net_route
();
1984 my $groups_conf = load_security_groups
();
1988 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1989 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1991 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1993 my $hostfw_conf = load_hostfw_conf
();
1994 my $hostfw_options = $hostfw_conf->{options
} || {};
1996 generate_std_chains
($ruleset, $hostfw_options);
1998 my $hostfw_enable = !(defined($hostfw_options->{enable
}) && ($hostfw_options->{enable
} == 0));
2000 enable_host_firewall
($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
2002 my $ips_enable = undef;
2004 # generate firewall rules for QEMU VMs
2005 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
2006 my $conf = $vmdata->{qemu
}->{$vmid};
2007 my $vmfw_conf = $vmfw_configs->{$vmid};
2008 next if !$vmfw_conf;
2009 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
2011 $ips_enable = 1 if $vmfw_conf->{options
}->{ips
};
2013 foreach my $netid (keys %$conf) {
2014 next if $netid !~ m/^net(\d+)$/;
2015 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
2017 my $iface = "tap${vmid}i$1";
2019 my $bridge = $net->{bridge
};
2020 next if !$bridge; # fixme: ?
2022 $bridge .= "v$net->{tag}" if $net->{tag
};
2024 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge, $routing_table);
2026 my $macaddr = $net->{macaddr
};
2027 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr,
2028 $vmfw_conf, $vmid, $bridge, 'IN');
2029 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr,
2030 $vmfw_conf, $vmid, $bridge, 'OUT');
2034 # generate firewall rules for OpenVZ containers
2035 foreach my $vmid (keys %{$vmdata->{openvz
}}) {
2036 my $conf = $vmdata->{openvz
}->{$vmid};
2038 my $vmfw_conf = $vmfw_configs->{$vmid};
2039 next if !$vmfw_conf;
2040 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
2042 $ips_enable = 1 if $vmfw_conf->{options
}->{ips
};
2044 if ($conf->{ip_address
} && $conf->{ip_address
}->{value
}) {
2045 my $ip = $conf->{ip_address
}->{value
};
2046 generate_venet_rules_direction
($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'IN');
2047 generate_venet_rules_direction
($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'OUT');
2050 if ($conf->{netif
} && $conf->{netif
}->{value
}) {
2051 my $netif = PVE
::OpenVZ
::parse_netif
($conf->{netif
}->{value
});
2052 foreach my $netid (keys %$netif) {
2053 my $d = $netif->{$netid};
2054 my $bridge = $d->{bridge
};
2056 warn "no bridge device for CT $vmid iface '$netid'\n";
2060 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge, $routing_table);
2062 my $macaddr = $d->{mac
};
2063 my $iface = $d->{host_ifname
};
2064 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr,
2065 $vmfw_conf, $vmid, $bridge, 'IN');
2066 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr,
2067 $vmfw_conf, $vmid, $bridge, 'OUT');
2072 if($hostfw_options->{optimize
}){
2074 my $accept = $ips_enable ?
"PVEFW-IPS" : "ACCEPT";
2075 ruleset_insertrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept");
2076 ruleset_insertrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate INVALID -j DROP");
2079 # fixme: what log level should we use here?
2080 my $loglevel = get_option_log_level
($hostfw_options, "log_level_out");
2082 # fixme: should we really block inter-bridge traffic?
2084 # always allow traffic from containers?
2085 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN");
2087 # disable interbridge routing
2088 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
2089 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
2090 ruleset_addlog
($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+");
2091 ruleset_addlog
($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+");
2092 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
2093 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
2095 return wantarray ?
($ruleset, $hostfw_conf) : $ruleset;
2098 sub get_ruleset_status
{
2099 my ($ruleset, $verbose) = @_;
2101 my $active_chains = iptables_get_chains
();
2103 my $statushash = {};
2105 foreach my $chain (sort keys %$ruleset) {
2106 my $digest = Digest
::SHA-
>new('sha1');
2107 foreach my $cmd (@{$ruleset->{$chain}}) {
2108 $digest->add("$cmd\n");
2110 my $sig = $digest->b64digest;
2111 $statushash->{$chain}->{sig
} = $sig;
2113 my $oldsig = $active_chains->{$chain};
2114 if (!defined($oldsig)) {
2115 $statushash->{$chain}->{action
} = 'create';
2117 if ($oldsig eq $sig) {
2118 $statushash->{$chain}->{action
} = 'exists';
2120 $statushash->{$chain}->{action
} = 'update';
2123 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
2124 foreach my $cmd (@{$ruleset->{$chain}}) {
2125 print "\t$cmd\n" if $verbose;
2129 foreach my $chain (sort keys %$active_chains) {
2130 if (!defined($ruleset->{$chain})) {
2131 my $sig = $active_chains->{$chain};
2132 $statushash->{$chain}->{action
} = 'delete';
2133 $statushash->{$chain}->{sig
} = $sig;
2134 print "delete $chain ($sig)\n" if $verbose;
2144 get_ruleset_status
($ruleset, 1);
2147 sub print_sig_rule
{
2148 my ($chain, $sig) = @_;
2150 # We just use this to store a SHA1 checksum used to detect changes
2151 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
2154 sub get_rulset_cmdlist {
2155 my ($ruleset, $verbose) = @_;
2157 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
2159 my $statushash = get_ruleset_status($ruleset, $verbose);
2161 # create missing chains first
2162 foreach my $chain (sort keys %$ruleset) {
2163 my $stat = $statushash->{$chain};
2164 die "internal error
" if !$stat;
2165 next if $stat->{action} ne 'create';
2167 $cmdlist .= ":$chain - [0:0]\n";
2170 my $rule = "INPUT
-j PVEFW-INPUT
";
2171 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2172 $cmdlist .= "-A
$rule\n";
2174 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
2175 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2176 $cmdlist .= "-A
$rule\n";
2179 $rule = "FORWARD
-j PVEFW-FORWARD
";
2180 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2181 $cmdlist .= "-A
$rule\n";
2184 foreach my $chain (sort keys %$ruleset) {
2185 my $stat = $statushash->{$chain};
2186 die "internal error
" if !$stat;
2188 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
2189 $cmdlist .= "-F
$chain\n";
2190 foreach my $cmd (@{$ruleset->{$chain}}) {
2191 $cmdlist .= "$cmd\n";
2193 $cmdlist .= print_sig_rule($chain, $stat->{sig});
2194 } elsif ($stat->{action} eq 'delete') {
2195 die "internal error
"; # this should not happen
2196 } elsif ($stat->{action} eq 'exists') {
2199 die "internal error
- unknown status
'$stat->{action}'";
2203 foreach my $chain (keys %$statushash) {
2204 next if $statushash->{$chain}->{action} ne 'delete';
2205 $cmdlist .= "-F
$chain\n";
2207 foreach my $chain (keys %$statushash) {
2208 next if $statushash->{$chain}->{action} ne 'delete';
2209 next if $chain eq 'PVEFW-INPUT';
2210 next if $chain eq 'PVEFW-OUTPUT';
2211 next if $chain eq 'PVEFW-FORWARD';
2212 $cmdlist .= "-X
$chain\n";
2215 $cmdlist .= "COMMIT
\n";
2221 my ($ruleset, $hostfw_conf, $verbose) = @_;
2223 enable_bridge_firewall();
2225 update_nf_conntrack_max($hostfw_conf);
2227 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
2229 print $cmdlist if $verbose;
2231 iptables_restore_cmdlist($cmdlist);
2233 # test: re-read status and check if everything is up to date
2234 my $statushash = get_ruleset_status($ruleset);
2237 foreach my $chain (sort keys %$ruleset) {
2238 my $stat = $statushash->{$chain};
2239 if ($stat->{action} ne 'exists') {
2240 warn "unable to update chain
'$chain'\n";
2245 die "unable to apply firewall changes
\n" if $errors;
2248 sub update_nf_conntrack_max {
2249 my ($hostfw_conf) = @_;
2251 my $max = 65536; # reasonable default
2253 my $options = $hostfw_conf->{options} || {};
2255 if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) {
2256 $max = $options->{nf_conntrack_max};
2257 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
2260 my $filename_nf_conntrack_max = "/proc/sys
/net/nf_conntrack_max
";
2261 my $filename_hashsize = "/sys/module
/nf_conntrack/parameters
/hashsize
";
2263 my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max);
2265 if ($current != $max) {
2266 my $hashsize = int($max/4);
2267 PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize);
2268 PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max);
2273 my ($start, $verbose) = @_;
2276 my $status = read_pvefw_status();
2278 my ($ruleset, $hostfw_conf) = PVE::Firewall::compile();
2280 if ($start || $status eq 'active') {
2282 save_pvefw_status('active') if ($status ne 'active');
2284 apply_ruleset($ruleset, $hostfw_conf, $verbose);
2286 print "Firewall
not active
(status
= $status)\n" if $verbose;