8 use PVE
::JSONSchema
qw(get_standard_option);
16 use PVE
::Tools
qw(run_command lock_file);
19 my $hostfw_conf_filename = "/etc/pve/local/host.fw";
20 my $clusterfw_conf_filename = "/etc/pve/firewall/cluster.fw";
22 # dynamically include PVE::QemuServer and PVE::OpenVZ
23 # to avoid dependency problems
26 require PVE
::QemuServer
;
27 $have_qemu_server = 1;
33 $have_pve_manager = 1;
38 my $nodename = PVE
::INotify
::nodename
();
40 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
41 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
43 my $default_log_level = 'info';
45 my $log_level_hash = {
56 # imported/converted from: /usr/share/shorewall/macro.*
59 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
60 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
63 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
66 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
69 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
70 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
73 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
74 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
77 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
80 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
81 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
82 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
85 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
86 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
89 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
92 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
93 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
96 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
97 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
100 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
103 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
104 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
107 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
110 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
113 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
114 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
115 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
116 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
119 { action
=> 'PARAM', proto
=> '47' },
120 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
123 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
126 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
127 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
130 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
133 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
136 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
139 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
142 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
145 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
148 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
151 { action
=> 'PARAM', proto
=> '94' },
152 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
155 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
158 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
161 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
162 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
165 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
166 { action
=> 'PARAM', proto
=> '50' },
167 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
168 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
171 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
172 { action
=> 'PARAM', proto
=> '51' },
173 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
174 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
177 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
178 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
179 { action
=> 'PARAM', proto
=> '50' },
180 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
181 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
182 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
185 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
188 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
191 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
194 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
197 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
200 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
201 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
204 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
207 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
210 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
213 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
216 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
217 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
218 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
221 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
224 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
227 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
230 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
233 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
236 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
237 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
240 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
241 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
244 { action
=> 'PARAM', proto
=> '89' },
247 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
250 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
251 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
254 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
257 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
260 { action
=> 'PARAM', proto
=> '47' },
261 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
262 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
265 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
268 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
271 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
274 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
277 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
278 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
281 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
284 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
287 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
290 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
293 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
296 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
297 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
298 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
299 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
302 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
303 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
304 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
306 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
307 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
308 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
309 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
312 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
315 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
318 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
321 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
322 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
325 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
328 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
331 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
334 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
335 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
336 { action
=> 'PARAM', proto
=> '41' },
337 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
340 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
343 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
346 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
347 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
350 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
353 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
356 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
359 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
362 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
363 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
366 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
369 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
372 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
373 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
376 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
379 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
382 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
386 my $pve_fw_parsed_macros;
387 my $pve_fw_preferred_macro_names = {};
389 my $pve_std_chains = {
390 'PVEFW-SET-ACCEPT-MARK' => [
391 "-j MARK --set-mark 1",
393 'PVEFW-DropBroadcast' => [
394 # same as shorewall 'Broadcast'
395 # simply DROP BROADCAST/MULTICAST/ANYCAST
396 # we can use this to reduce logging
397 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
398 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
399 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
400 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
403 # same as shorewall 'reject'
404 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
405 { action
=> 'DROP', source
=> '224.0.0.0/4' },
406 { action
=> 'DROP', proto
=> 'icmp' },
407 "-p tcp -j REJECT --reject-with tcp-reset",
408 "-p udp -j REJECT --reject-with icmp-port-unreachable",
409 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
410 "-j REJECT --reject-with icmp-host-prohibited",
413 # same as shorewall 'Drop', which is equal to DROP,
414 # but REJECT/DROP some packages to reduce logging,
415 # and ACCEPT critical ICMP types
416 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
417 # we are not interested in BROADCAST/MULTICAST/ANYCAST
418 { action
=> 'PVEFW-DropBroadcast' },
419 # ACCEPT critical ICMP types
420 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
421 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
422 # Drop packets with INVALID state
423 "-m conntrack --ctstate INVALID -j DROP",
424 # Drop Microsoft SMB noise
425 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
426 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
427 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
428 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
429 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
430 # Drop new/NotSyn traffic so that it doesn't get logged
431 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
433 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
436 # same as shorewall 'Reject', which is equal to Reject,
437 # but REJECT/DROP some packages to reduce logging,
438 # and ACCEPT critical ICMP types
439 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
440 # we are not interested in BROADCAST/MULTICAST/ANYCAST
441 { action
=> 'PVEFW-DropBroadcast' },
442 # ACCEPT critical ICMP types
443 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
444 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
445 # Drop packets with INVALID state
446 "-m conntrack --ctstate INVALID -j DROP",
447 # Drop Microsoft SMB noise
448 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
449 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
450 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
451 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
452 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
453 # Drop new/NotSyn traffic so that it doesn't get logged
454 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
456 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
458 'PVEFW-tcpflags' => [
459 # same as shorewall tcpflags action.
460 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
461 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
462 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
463 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
464 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
465 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
468 # same as shorewall smurfs action
469 # Filter packets for smurfs (packets with a broadcast address as the source).
470 "-s 0.0.0.0/32 -j RETURN",
471 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
472 "-s 224.0.0.0/4 -g PVEFW-smurflog",
476 # iptables -p icmp -h
477 my $icmp_type_names = {
480 'destination-unreachable' => 1,
481 'network-unreachable' => 1,
482 'host-unreachable' => 1,
483 'protocol-unreachable' => 1,
484 'port-unreachable' => 1,
485 'fragmentation-needed' => 1,
486 'source-route-failed' => 1,
487 'network-unknown' => 1,
489 'network-prohibited' => 1,
490 'host-prohibited' => 1,
491 'TOS-network-unreachable' => 1,
492 'TOS-host-unreachable' => 1,
493 'communication-prohibited' => 1,
494 'host-precedence-violation' => 1,
495 'precedence-cutoff' => 1,
496 'source-quench' => 1,
498 'network-redirect' => 1,
499 'host-redirect' => 1,
500 'TOS-network-redirect' => 1,
501 'TOS-host-redirect' => 1,
503 'router-advertisement' => 1,
504 'router-solicitation' => 1,
505 'time-exceeded' => 1,
506 'ttl-zero-during-transit' => 1,
507 'ttl-zero-during-reassembly' => 1,
508 'parameter-problem' => 1,
509 'ip-header-bad' => 1,
510 'required-option-missing' => 1,
511 'timestamp-request' => 1,
512 'timestamp-reply' => 1,
513 'address-mask-request' => 1,
514 'address-mask-reply' => 1,
517 sub init_firewall_macros
{
519 $pve_fw_parsed_macros = {};
521 foreach my $k (keys %$pve_fw_macros) {
522 my $lc_name = lc($k);
523 my $macro = $pve_fw_macros->{$k};
524 $pve_fw_preferred_macro_names->{$lc_name} = $k;
525 $pve_fw_parsed_macros->{$k} = $macro;
529 init_firewall_macros
();
533 sub get_etc_services
{
535 return $etc_services if $etc_services;
537 my $filename = "/etc/services";
539 my $fh = IO
::File-
>new($filename, O_RDONLY
);
541 warn "unable to read '$filename' - $!\n";
547 while (my $line = <$fh>) {
549 next if $line =~m/^#/;
550 next if ($line =~m/^\s*$/);
552 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
553 $services->{byid
}->{$2}->{name
} = $1;
554 $services->{byid
}->{$2}->{port
} = $2;
555 $services->{byid
}->{$2}->{$3} = 1;
556 $services->{byname
}->{$1} = $services->{byid
}->{$2};
562 $etc_services = $services;
565 return $etc_services;
570 sub get_etc_protocols
{
571 return $etc_protocols if $etc_protocols;
573 my $filename = "/etc/protocols";
575 my $fh = IO
::File-
>new($filename, O_RDONLY
);
577 warn "unable to read '$filename' - $!\n";
583 while (my $line = <$fh>) {
585 next if $line =~m/^#/;
586 next if ($line =~m/^\s*$/);
588 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
589 $protocols->{byid
}->{$2}->{name
} = $1;
590 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
596 $etc_protocols = $protocols;
598 return $etc_protocols;
601 sub parse_address_list
{
605 foreach my $aor (split(/,/, $str)) {
606 if (!Net
::IP-
>new($aor)) {
607 my $err = Net
::IP
::Error
();
608 die "invalid IP address: $err\n";
616 sub parse_port_name_number_or_range
{
619 my $services = PVE
::Firewall
::get_etc_services
();
621 foreach my $item (split(/,/, $str)) {
625 foreach my $pon (split(':', $item, 2)) {
626 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
627 if ($pon =~ m/^\d+$/){
628 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
629 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
632 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
640 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
641 sub pve_fw_verify_port_spec
{
644 parse_port_name_number_or_range
($portstr);
649 PVE
::JSONSchema
::register_format
('pve-fw-v4addr-spec', \
&pve_fw_verify_v4addr_spec
);
650 sub pve_fw_verify_v4addr_spec
{
653 parse_address_list
($list);
658 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
659 sub pve_fw_verify_protocol_spec
{
662 my $protocols = get_etc_protocols
();
664 die "unknown protocol '$proto'\n" if $proto &&
665 !(defined($protocols->{byname
}->{$proto}) ||
666 defined($protocols->{byid
}->{$proto}));
672 # helper function for API
674 my $rule_properties = {
676 description
=> "Update rule at position <pos>.",
690 enum
=> ['in', 'out', 'group'],
695 enum
=> ['ACCEPT', 'DROP', 'REJECT'],
702 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
704 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
708 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
712 type
=> 'string', format
=> 'pve-fw-protocol-spec',
720 type
=> 'string', format
=> 'pve-fw-port-spec',
724 type
=> 'string', format
=> 'pve-fw-port-spec',
733 sub cleanup_fw_rule
{
734 my ($rule, $digest, $pos) = @_;
738 foreach my $k (keys %$rule) {
739 next if !$rule_properties->{$k};
741 next if !defined($v);
743 $r->{digest
} = $digest;
750 sub add_rule_properties
{
751 my ($properties) = @_;
753 foreach my $k (keys %$rule_properties) {
754 $properties->{$k} = $rule_properties->{$k};
761 my ($rule, $param) = @_;
763 foreach my $k (keys %$rule_properties) {
764 if (defined(my $v = $param->{$k})) {
765 if ($v eq '' || $v eq '-') {
778 my $bridge_firewall_enabled = 0;
780 sub enable_bridge_firewall
{
782 return if $bridge_firewall_enabled; # only once
784 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
785 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
787 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
788 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
790 $bridge_firewall_enabled = 1;
793 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
795 sub iptables_restore_cmdlist
{
798 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
801 sub ipset_restore_cmdlist
{
804 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist);
807 sub iptables_get_chains
{
811 # check what chains we want to track
812 my $is_pvefw_chain = sub {
815 return 1 if $name =~ m/^PVEFW-\S+$/;
817 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
819 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
821 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
823 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT|IPS)$/;
824 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
836 return if $line =~ m/^#/;
837 return if $line =~ m/^\s*$/;
839 if ($line =~ m/^\*(\S+)$/) {
844 return if $table ne 'filter';
846 if ($line =~ m/^:(\S+)\s/) {
848 return if !&$is_pvefw_chain($chain);
849 $res->{$chain} = "unknown";
850 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
851 my ($chain, $sig) = ($1, $2);
852 return if !&$is_pvefw_chain($chain);
853 $res->{$chain} = $sig;
854 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
857 # simply ignore the rest
862 run_command
("/sbin/iptables-save", outfunc
=> $parser);
864 return wantarray ?
($res, $hooks) : $res;
867 sub iptables_chain_digest
{
869 my $digest = Digest
::SHA-
>new('sha1');
870 foreach my $rule (@$rules) { # order is important
873 return $digest->b64digest;
876 sub ipset_chain_digest
{
879 my $digest = Digest
::SHA-
>new('sha1');
880 foreach my $rule (sort @$rules) { # note: sorted
883 return $digest->b64digest;
886 sub ipset_get_chains
{
894 return if $line =~ m/^#/;
895 return if $line =~ m/^\s*$/;
896 if ($line =~ m/^(?:\S+)\s(\S+)\s(?:\S+).*/) {
898 $line =~ s/\s+$//; # delete trailing white space
899 push @{$chains->{$chain}}, $line;
901 # simply ignore the rest
906 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
908 # compute digest for each chain
909 foreach my $chain (keys %$chains) {
910 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
916 sub ruleset_generate_cmdstr
{
917 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
919 return if defined($rule->{enable
}) && !$rule->{enable
};
921 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
923 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
924 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
925 my $nbsource = $rule->{source
} ? parse_address_list
( $rule->{source
}) : 0;
926 my $nbdest = $rule->{dest
} ? parse_address_list
($rule->{dest
}) : 0;
930 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
931 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
933 push @cmd, "-m iprange --src-range" if $nbsource > 1;
934 push @cmd, "-s $rule->{source}" if $rule->{source
};
935 push @cmd, "-m iprange --dst-range" if $nbdest > 1;
936 push @cmd, "-d $rule->{dest}" if $rule->{dest
};
938 if ($rule->{proto
}) {
939 push @cmd, "-p $rule->{proto}";
942 $multiport++ if $nbdport > 1;
943 $multiport++ if $nbsport > 1;
945 push @cmd, "--match multiport" if $multiport;
947 die "multiport: option '--sports' cannot be used together with '--dports'\n"
948 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
950 if ($rule->{dport
}) {
951 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
952 # Note: we use dport to store --icmp-type
953 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
954 push @cmd, "-m icmp --icmp-type $rule->{dport}";
957 if ($multiport == 2) {
958 push @cmd, "--ports $rule->{dport}";
960 push @cmd, "--dports $rule->{dport}";
963 push @cmd, "--dport $rule->{dport}";
968 if ($rule->{sport
}) {
970 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
972 push @cmd, "--sport $rule->{sport}";
975 } elsif ($rule->{dport
} || $rule->{sport
}) {
976 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
977 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
980 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
982 if (my $action = $rule->{action
}) {
983 $action = $actions->{$action} if defined($actions->{$action});
984 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
985 push @cmd, $goto ?
"-g $action" : "-j $action";
988 return scalar(@cmd) ?
join(' ', @cmd) : undef;
991 my $apply_macro = sub {
992 my ($macro_name, $param) = @_;
994 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
995 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
999 foreach my $templ (@$macro_rules) {
1001 my $param_used = {};
1002 foreach my $k (keys %$templ) {
1003 my $v = $templ->{$k};
1004 if ($v eq 'PARAM') {
1006 $param_used->{$k} = 1;
1007 } elsif ($v eq 'DEST') {
1008 $v = $param->{dest
};
1009 $param_used->{dest
} = 1;
1010 } elsif ($v eq 'SOURCE') {
1011 $v = $param->{source
};
1012 $param_used->{source
} = 1;
1015 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1018 foreach my $k (keys %$param) {
1019 next if $k eq 'macro';
1020 next if !defined($param->{$k});
1021 next if $param_used->{$k};
1022 if (defined($rule->{$k})) {
1023 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1024 if $rule->{$k} ne $param->{$k};
1026 $rule->{$k} = $param->{$k};
1029 push @$rules, $rule;
1035 sub ruleset_generate_rule
{
1036 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1040 if ($rule->{macro}) {
1041 $rules = &$apply_macro($rule->{macro}, $rule);
1046 foreach my $tmp (@$rules) {
1047 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $tmp, $actions, $goto)) {
1048 ruleset_addrule
($ruleset, $chain, $cmdstr);
1053 sub ruleset_generate_rule_insert
{
1054 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1056 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1058 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
1059 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1063 sub ruleset_create_chain
{
1064 my ($ruleset, $chain) = @_;
1066 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1067 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1069 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1071 $ruleset->{$chain} = [];
1074 sub ruleset_chain_exist
{
1075 my ($ruleset, $chain) = @_;
1077 return $ruleset->{$chain} ?
1 : undef;
1080 sub ruleset_addrule
{
1081 my ($ruleset, $chain, $rule) = @_;
1083 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1085 push @{$ruleset->{$chain}}, "-A $chain $rule";
1088 sub ruleset_insertrule
{
1089 my ($ruleset, $chain, $rule) = @_;
1091 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1093 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1096 sub get_log_rule_base
{
1097 my ($chain, $vmid, $msg, $loglevel) = @_;
1099 die "internal error - no log level" if !defined($loglevel);
1101 $vmid = 0 if !defined($vmid);
1103 # Note: we use special format for prefix to pass further
1104 # info to log daemon (VMID, LOGVELEL and CHAIN)
1106 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1109 sub ruleset_addlog {
1110 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1112 return if !defined($loglevel);
1114 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1116 $logrule = "$rule $logrule" if defined($rule);
1118 ruleset_addrule($ruleset, $chain, $logrule)
1121 sub generate_bridge_chains {
1122 my ($ruleset, $hostfw_conf, $bridge, $routing_table) = @_;
1124 my $options = $hostfw_conf->{options} || {};
1126 die "error
: detected direct route to bridge
'$bridge'\n"
1127 if !$options->{allow_bridge_route} && $routing_table->{$bridge};
1129 if (!ruleset_chain_exist($ruleset, "$bridge-FW
")) {
1130 ruleset_create_chain($ruleset, "$bridge-FW
");
1131 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-FW
");
1132 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-FW
");
1135 if (!ruleset_chain_exist($ruleset, "$bridge-OUT
")) {
1136 ruleset_create_chain($ruleset, "$bridge-OUT
");
1137 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-in
-j
$bridge-OUT
");
1138 ruleset_insertrule($ruleset, "PVEFW-INPUT
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-OUT
");
1141 if (!ruleset_chain_exist($ruleset, "$bridge-IN
")) {
1142 ruleset_create_chain($ruleset, "$bridge-IN
");
1143 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j
$bridge-IN
");
1144 ruleset_addrule($ruleset, "$bridge-FW
", "-m mark
--mark
1 -j ACCEPT
");
1145 # accept traffic to unmanaged bridge ports
1146 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j ACCEPT
");
1150 sub ruleset_add_chain_policy {
1151 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
1153 if ($policy eq 'ACCEPT') {
1155 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
1156 { ACCEPT => $accept_action});
1158 } elsif ($policy eq 'DROP') {
1160 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1162 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1164 ruleset_addrule($ruleset, $chain, "-j DROP
");
1165 } elsif ($policy eq 'REJECT') {
1166 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1168 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1170 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1173 die "internal error
: unknown policy
'$policy'";
1177 sub ruleset_create_vm_chain {
1178 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
1180 ruleset_create_chain($ruleset, $chain);
1181 my $accept = generate_nfqueue($options);
1183 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1184 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1187 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1188 if ($direction eq 'OUT') {
1189 ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK',
1190 proto => 'udp', sport => 68, dport => 67 });
1192 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT',
1193 proto => 'udp', sport => 67, dport => 68 });
1197 if ($options->{tcpflags}) {
1198 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1201 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1202 if($direction eq 'OUT'){
1203 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-g PVEFW-SET-ACCEPT-MARK
");
1206 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1208 if ($direction eq 'OUT') {
1209 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1210 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1212 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1218 sub ruleset_generate_vm_rules {
1219 my ($ruleset, $rules, $cluster_conf, $chain, $netid, $direction, $options) = @_;
1221 my $lc_direction = lc($direction);
1223 foreach my $rule (@$rules) {
1224 next if $rule->{iface} && $rule->{iface} ne $netid;
1225 next if !$rule->{enable};
1226 if ($rule->{type} eq 'group') {
1227 my $group_chain = "GROUP-
$rule->{action
}-$direction";
1228 if(!ruleset_chain_exist($ruleset, $group_chain)){
1229 generate_group_rules($ruleset, $cluster_conf, $rule->{action});
1231 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1232 if ($direction eq 'OUT'){
1233 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j RETURN
");
1235 my $accept = generate_nfqueue($options);
1236 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$accept");
1240 next if $rule->{type} ne $lc_direction;
1241 if ($direction eq 'OUT') {
1242 ruleset_generate_rule($ruleset, $chain, $rule,
1243 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1245 my $accept = generate_nfqueue($options);
1246 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject
" });
1252 sub generate_nfqueue {
1256 if($options->{ips}){
1257 $action = "NFQUEUE
";
1258 if($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1259 if(defined($3) && defined($1)) {
1260 $action .= " --queue-balance
$1:$3";
1261 }elsif (defined($1)) {
1262 $action .= " --queue-num
$1";
1265 $action .= " --queue-bypass
";
1273 sub ruleset_generate_vm_ipsrules {
1274 my ($ruleset, $options, $direction, $iface, $bridge) = @_;
1276 if ($options->{ips} && $direction eq 'IN') {
1277 my $nfqueue = generate_nfqueue($options);
1279 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1280 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1283 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1284 ruleset_create_chain($ruleset, "$bridge-IPS
");
1285 ruleset_insertrule($ruleset, "PVEFW-IPS
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-IPS
");
1288 ruleset_addrule($ruleset, "$bridge-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1292 sub generate_venet_rules_direction {
1293 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
1295 parse_address_list($ip); # make sure we have a valid $ip list
1297 my $lc_direction = lc($direction);
1299 my $rules = $vmfw_conf->{rules};
1301 my $options = $vmfw_conf->{options};
1302 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1304 my $chain = "venet0-
$vmid-$direction";
1306 ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction);
1308 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $chain, 'venet', $direction);
1313 if ($direction eq 'OUT') {
1314 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1316 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1319 my $accept = generate_nfqueue($options);
1320 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1321 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1323 # plug into FORWARD, INPUT and OUTPUT chain
1324 if ($direction eq 'OUT') {
1325 ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD
", {
1328 iface_in => 'venet0'});
1330 ruleset_generate_rule_insert($ruleset, "PVEFW-INPUT
", {
1333 iface_in => 'venet0'});
1335 ruleset_generate_rule($ruleset, "PVEFW-FORWARD
", {
1338 iface_out => 'venet0'});
1340 ruleset_generate_rule($ruleset, "PVEFW-OUTPUT
", {
1343 iface_out => 'venet0'});
1347 sub generate_tap_rules_direction {
1348 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_;
1350 my $lc_direction = lc($direction);
1352 my $rules = $vmfw_conf->{rules};
1354 my $options = $vmfw_conf->{options};
1355 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1357 my $tapchain = "$iface-$direction";
1359 ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction);
1361 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $tapchain, $netid, $direction, $options);
1363 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface, $bridge);
1368 if ($direction eq 'OUT') {
1369 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1371 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1374 my $accept = generate_nfqueue($options);
1375 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1376 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
1378 # plug the tap chain to bridge chain
1379 if ($direction eq 'IN') {
1380 ruleset_insertrule($ruleset, "$bridge-IN
",
1381 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
1383 ruleset_insertrule($ruleset, "$bridge-OUT
",
1384 "-m physdev
--physdev-in
$iface -j
$tapchain");
1388 sub enable_host_firewall {
1389 my ($ruleset, $hostfw_conf, $cluster_conf) = @_;
1391 # fixme: allow security groups
1393 my $options = $hostfw_conf->{options};
1394 my $rules = $hostfw_conf->{rules};
1396 # host inbound firewall
1397 my $chain = "PVEFW-HOST-IN
";
1398 ruleset_create_chain($ruleset, $chain);
1400 my $loglevel = get_option_log_level($options, "log_level_in
");
1402 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1403 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1406 if ($options->{tcpflags}) {
1407 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1410 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1411 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1412 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
1413 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1414 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1415 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1417 # we use RETURN because we need to check also tap rules
1418 my $accept_action = 'RETURN';
1420 foreach my $rule (@$rules) {
1421 next if $rule->{type} ne 'in';
1422 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1425 # implement input policy
1426 my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1427 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1429 # host outbound firewall
1430 $chain = "PVEFW-HOST-OUT
";
1431 ruleset_create_chain($ruleset, $chain);
1433 $loglevel = get_option_log_level($options, "log_level_out
");
1435 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1436 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1437 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
1438 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1439 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1440 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1442 # we use RETURN because we may want to check other thigs later
1443 $accept_action = 'RETURN';
1445 foreach my $rule (@$rules) {
1446 next if $rule->{type} ne 'out';
1447 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1450 # implement output policy
1451 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1452 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1454 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
1455 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
1458 sub generate_group_rules {
1459 my ($ruleset, $cluster_conf, $group) = @_;
1460 die "no such security group
'$group'\n" if !$cluster_conf->{groups}->{$group};
1462 my $rules = $cluster_conf->{groups}->{$group};
1464 my $chain = "GROUP-
${group
}-IN
";
1466 ruleset_create_chain($ruleset, $chain);
1467 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1469 foreach my $rule (@$rules) {
1470 next if $rule->{type} ne 'in';
1471 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1474 $chain = "GROUP-
${group
}-OUT
";
1476 ruleset_create_chain($ruleset, $chain);
1477 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1479 foreach my $rule (@$rules) {
1480 next if $rule->{type} ne 'out';
1481 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1482 # check also other tap rules later
1483 ruleset_generate_rule($ruleset, $chain, $rule,
1484 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" });
1489 my $valid_netdev_names = {};
1490 for (my $i = 0; $i < $MAX_NETS; $i++) {
1491 $valid_netdev_names->{"net
$i"} = 1;
1495 my ($line, $need_iface, $allow_groups) = @_;
1497 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1499 # we can add single line comments to the end of the rule
1500 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
1502 # we can disable a rule when prefixed with '|'
1505 $enable = 0 if $line =~ s/^\|//;
1507 my @data = split(/\s+/, $line);
1508 my $expected_elements = $need_iface ? 8 : 7;
1510 die "wrong number of rule elements
\n" if scalar(@data) > $expected_elements;
1513 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1515 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1518 die "incomplete rule
\n" if ! ($type && $action);
1524 if ($type eq 'in' || $type eq 'out') {
1525 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1527 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1529 my $preferred_name = $pve_fw_preferred_macro_names->{lc($1)};
1530 die "unknown
macro '$1'\n" if !$preferred_name;
1531 $macro = $preferred_name;
1533 die "unknown action
'$action'\n";
1535 } elsif ($type eq 'group') {
1536 die "wrong number of rule elements
\n" if scalar(@data) != 3;
1537 die "groups disabled
\n" if !$allow_groups;
1539 die "invalid characters
in group name
\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1541 die "unknown rule type
'$type'\n";
1545 $iface = undef if $iface && $iface eq '-';
1548 $proto = undef if $proto && $proto eq '-';
1549 pve_fw_verify_protocol_spec($proto) if $proto;
1551 $source = undef if $source && $source eq '-';
1552 $dest = undef if $dest && $dest eq '-';
1554 $dport = undef if $dport && $dport eq '-';
1555 $sport = undef if $sport && $sport eq '-';
1557 parse_port_name_number_or_range($dport) if defined($dport);
1558 parse_port_name_number_or_range($sport) if defined($sport);
1560 parse_address_list($source) if $source;
1561 parse_address_list($dest) if $dest;
1566 comment => $comment,
1578 sub parse_vmfw_option {
1583 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
1585 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags|ips):\s*(0|1)\s*$/i) {
1588 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1590 $value = $2 ? lc($3) : '';
1591 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1594 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
1599 die "can
't parse option '$line'\n"
1602 return ($opt, $value);
1605 sub parse_hostfw_option {
1610 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1612 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags|allow_bridge_route|optimize):\s*(0|1)\s*$/i) {
1615 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1617 $value = $2 ? lc($3) : '';
1618 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1621 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1626 die "can't parse option
'$line'\n"
1629 return ($opt, $value);
1632 sub parse_clusterfw_option {
1637 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
1642 die "can
't parse option '$line'\n"
1645 return ($opt, $value);
1648 sub parse_vm_fw_rules {
1649 my ($filename, $fh) = @_;
1651 my $res = { rules => [], options => {}};
1655 my $digest = Digest::SHA->new('sha1
');
1657 while (defined(my $line = <$fh>)) {
1658 $digest->add($line);
1660 next if $line =~ m/^#/;
1661 next if $line =~ m/^\s*$/;
1663 my $linenr = $fh->input_line_number();
1664 my $prefix = "$filename (line $linenr)";
1666 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1668 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1672 warn "$prefix: skip line - no section";
1676 next if !$res->{$section}; # skip undefined section
1678 if ($section eq 'options
') {
1680 my ($opt, $value) = parse_vmfw_option($line);
1681 $res->{options}->{$opt} = $value;
1683 warn "$prefix: $@" if $@;
1688 eval { $rule = parse_fw_rule($line, 1, 1); };
1690 warn "$prefix: $err";
1694 push @{$res->{$section}}, $rule;
1697 $res->{digest} = $digest->b64digest;
1702 sub parse_host_fw_rules {
1703 my ($filename, $fh) = @_;
1705 my $res = { rules => [], options => {}};
1709 my $digest = Digest::SHA->new('sha1
');
1711 while (defined(my $line = <$fh>)) {
1712 $digest->add($line);
1714 next if $line =~ m/^#/;
1715 next if $line =~ m/^\s*$/;
1717 my $linenr = $fh->input_line_number();
1718 my $prefix = "$filename (line $linenr)";
1720 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1722 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1726 warn "$prefix: skip line - no section";
1730 next if !$res->{$section}; # skip undefined section
1732 if ($section eq 'options
') {
1734 my ($opt, $value) = parse_hostfw_option($line);
1735 $res->{options}->{$opt} = $value;
1737 warn "$prefix: $@" if $@;
1742 eval { $rule = parse_fw_rule($line, 1, 1); };
1744 warn "$prefix: $err";
1748 push @{$res->{$section}}, $rule;
1751 $res->{digest} = $digest->b64digest;
1756 sub parse_cluster_fw_rules {
1757 my ($filename, $fh) = @_;
1762 my $res = { rules => [], options => {}, groups => {}, ipset => {} };
1764 my $digest = Digest::SHA->new('sha1
');
1766 while (defined(my $line = <$fh>)) {
1767 $digest->add($line);
1769 next if $line =~ m/^#/;
1770 next if $line =~ m/^\s*$/;
1772 my $linenr = $fh->input_line_number();
1773 my $prefix = "$filename (line $linenr)";
1775 if ($line =~ m/^\[options\]$/i) {
1776 $section = 'options
';
1780 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1781 $section = 'groups
';
1786 if ($line =~ m/^\[rules\]$/i) {
1791 if ($line =~ m/^\[netgroup\s+(\S+)\]\s*$/i) {
1798 warn "$prefix: skip line - no section";
1802 if ($section eq 'options
') {
1804 my ($opt, $value) = parse_clusterfw_option($line);
1805 $res->{options}->{$opt} = $value;
1807 warn "$prefix: $@" if $@;
1808 } elsif ($section eq 'rules
') {
1810 eval { $rule = parse_fw_rule($line, 1, 1); };
1812 warn "$prefix: $err";
1815 push @{$res->{$section}}, $rule;
1816 } elsif ($section eq 'groups
') {
1818 eval { $rule = parse_fw_rule($line, 0, 0); };
1820 warn "$prefix: $err";
1823 push @{$res->{$section}->{$group}}, $rule;
1824 } elsif ($section eq 'ipset
') {
1826 $line =~ m/^(\!)?(\s)?((\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?)/;
1831 warn "$prefix: $line is not an valid ip address";
1834 if (!Net::IP->new($ip)) {
1835 warn "$prefix: $line is not an valid ip address";
1838 $ip .= " nomatch" if $nomatch;
1840 push @{$res->{$section}->{$group}}, $ip;
1844 $res->{digest} = $digest->b64digest;
1849 my ($code, @param) = @_;
1853 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1860 sub read_local_vm_config {
1865 my $vmdata = { openvz => $openvz, qemu => $qemu };
1867 my $vmlist = PVE::Cluster::get_vmlist();
1868 return $vmdata if !$vmlist || !$vmlist->{ids};
1869 my $ids = $vmlist->{ids};
1871 foreach my $vmid (keys %$ids) {
1872 next if !$vmid; # skip VE0
1873 my $d = $ids->{$vmid};
1874 next if !$d->{node} || $d->{node} ne $nodename;
1875 next if !$d->{type};
1876 if ($d->{type} eq 'openvz
') {
1877 if ($have_pve_manager) {
1878 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
1879 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1880 $openvz->{$vmid} = $conf;
1883 } elsif ($d->{type} eq 'qemu
') {
1884 if ($have_qemu_server) {
1885 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1886 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1887 $qemu->{$vmid} = $conf;
1896 sub load_vmfw_conf {
1901 my $filename = "/etc/pve/firewall/$vmid.fw";
1902 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1903 $vmfw_conf = parse_vm_fw_rules($filename, $fh);
1909 sub read_vm_firewall_configs {
1911 my $vmfw_configs = {};
1913 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1914 my $vmfw_conf = load_vmfw_conf($vmid);
1915 next if !$vmfw_conf->{options}; # skip if file does not exists
1916 $vmfw_configs->{$vmid} = $vmfw_conf;
1919 return $vmfw_configs;
1922 sub get_option_log_level {
1923 my ($options, $k) = @_;
1925 my $v = $options->{$k};
1926 $v = $default_log_level if !defined($v);
1928 return undef if $v eq '' || $v eq 'nolog
';
1930 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1932 return $v if ($v >= 0) && ($v <= 7);
1934 warn "unknown log level ($k = '$v')\n";
1939 sub generate_std_chains {
1940 my ($ruleset, $options) = @_;
1942 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
1944 # same as shorewall smurflog.
1945 my $chain = 'PVEFW-smurflog
';
1947 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
1948 push @{$pve_std_chains->{$chain}}, "-j DROP";
1950 # same as shorewall logflags action.
1951 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
1952 $chain = 'PVEFW-logflags
';
1953 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
1954 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
1955 push @{$pve_std_chains->{$chain}}, "-j DROP";
1957 foreach my $chain (keys %$pve_std_chains) {
1958 ruleset_create_chain($ruleset, $chain);
1959 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1961 ruleset_generate_rule($ruleset, $chain, $rule);
1963 ruleset_addrule($ruleset, $chain, $rule);
1969 sub generate_ipset_chains {
1970 my ($ipset_ruleset, $fw_conf) = @_;
1972 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
1973 generate_ipset($ipset_ruleset, $ipset, $fw_conf->{ipset}->{$ipset});
1977 sub generate_ipset {
1978 my ($ipset_ruleset, $name, $options) = @_;
1980 my $hashsize = scalar(@$options);
1981 if ($hashsize <= 64) {
1984 $hashsize = round_powerof2($hashsize);
1987 push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize";
1989 foreach my $ip (@$options) {
1990 push @{$ipset_ruleset->{$name}}, "add $name $ip";
1994 sub round_powerof2 {
1998 $int |= $int >> $_ foreach (1,2,4,8,16);
2002 sub save_pvefw_status {
2005 die "unknown status '$status' - internal error"
2006 if $status !~ m/^(stopped|active)$/;
2008 mkdir dirname($pve_fw_status_filename);
2009 PVE::Tools::file_set_contents($pve_fw_status_filename, $status);
2012 sub read_pvefw_status {
2014 my $status = 'unknown
';
2016 return 'stopped
' if ! -f $pve_fw_status_filename;
2019 $status = PVE::Tools::file_get_contents($pve_fw_status_filename);
2026 # fixme: move to pve-common PVE::ProcFSTools
2027 sub read_proc_net_route {
2028 my $filename = "/proc/net/route";
2032 my $fh = IO::File->new ($filename, "r");
2033 return $res if !$fh;
2035 my $int_to_quad = sub {
2036 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
2039 while (defined(my $line = <$fh>)) {
2040 next if $line =~/^Iface\s+Destination/; # skip head
2041 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
2042 push @{$res->{$iface}}, {
2043 dest => &$int_to_quad(hex($dest)),
2044 gateway => &$int_to_quad(hex($gateway)),
2045 mask => &$int_to_quad(hex($mask)),
2054 sub load_clusterfw_conf {
2056 my $cluster_conf = {};
2057 if (my $fh = IO::File->new($clusterfw_conf_filename, O_RDONLY)) {
2058 $cluster_conf = parse_cluster_fw_rules($clusterfw_conf_filename, $fh);
2061 return $cluster_conf;
2064 my $format_rules = sub {
2065 my ($rules, $need_iface) = @_;
2069 foreach my $rule (@$rules) {
2070 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
') {
2071 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2072 $raw .= uc($rule->{type});
2073 $raw .= " " . $rule->{action};
2074 $raw .= " " . ($rule->{iface} || '-') if $need_iface;
2075 $raw .= " " . ($rule->{source} || '-');
2076 $raw .= " " . ($rule->{dest} || '-');
2077 $raw .= " " . ($rule->{proto} || '-');
2078 $raw .= " " . ($rule->{dport} || '-');
2079 $raw .= " " . ($rule->{sport} || '-');
2080 $raw .= " # " . encode('utf8
', $rule->{comment})
2081 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2084 die "implement me '$rule->{type
}'";
2091 my $format_options = sub {
2092 my ($raw, $options) = @_;
2094 $raw .= "[OPTIONS]\n\n";
2095 foreach my $opt (keys %$options) {
2096 $raw .= "$opt: $options->{$opt}\n";
2101 sub save_clusterfw_conf {
2102 my ($cluster_conf) = @_;
2106 my $options = $cluster_conf->{options};
2107 &$format_options($raw, $options) if scalar(keys %$options);
2111 my $rules = $cluster_conf->{rules};
2112 if (scalar(@$rules)) {
2113 $raw .= "[RULES]\n\n";
2114 $raw .= &$format_rules($rules, 1);
2118 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
2119 my $rules = $cluster_conf->{groups}->{$group};
2120 $raw .= "[group $group]\n\n";
2121 $raw .= &$format_rules($rules, 0);
2125 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
2128 sub load_hostfw_conf {
2130 my $hostfw_conf = {};
2131 if (my $fh = IO::File->new($hostfw_conf_filename, O_RDONLY)) {
2132 $hostfw_conf = parse_host_fw_rules($hostfw_conf_filename, $fh);
2134 return $hostfw_conf;
2137 sub save_hostfw_conf {
2138 my ($hostfw_conf) = @_;
2142 my $options = $hostfw_conf->{options};
2143 &$format_options($raw, $options) if scalar(keys %$options);
2145 my $rules = $hostfw_conf->{rules};
2146 if (scalar(@$rules)) {
2147 $raw .= "[RULES]\n\n";
2148 $raw .= &$format_rules($rules, 1);
2152 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
2156 my $vmdata = read_local_vm_config();
2157 my $vmfw_configs = read_vm_firewall_configs($vmdata);
2159 my $routing_table = read_proc_net_route();
2161 my $cluster_conf = load_clusterfw_conf();
2163 my $ipset_ruleset = {};
2164 generate_ipset_chains($ipset_ruleset, $cluster_conf);
2168 ruleset_create_chain($ruleset, "PVEFW-INPUT");
2169 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
2171 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
2173 my $hostfw_conf = load_hostfw_conf();
2174 my $hostfw_options = $hostfw_conf->{options} || {};
2176 generate_std_chains($ruleset, $hostfw_options);
2178 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
2180 enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf) if $hostfw_enable;
2182 # generate firewall rules for QEMU VMs
2183 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2184 my $conf = $vmdata->{qemu}->{$vmid};
2185 my $vmfw_conf = $vmfw_configs->{$vmid};
2186 next if !$vmfw_conf;
2187 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
2189 foreach my $netid (keys %$conf) {
2190 next if $netid !~ m/^net(\d+)$/;
2191 my $net = PVE::QemuServer::parse_net($conf->{$netid});
2193 my $iface = "tap${vmid}i$1";
2195 my $bridge = $net->{bridge};
2196 next if !$bridge; # fixme: ?
2198 $bridge .= "v$net->{tag}" if $net->{tag};
2200 generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table);
2202 my $macaddr = $net->{macaddr};
2203 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2204 $vmfw_conf, $vmid, $bridge, 'IN
');
2205 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2206 $vmfw_conf, $vmid, $bridge, 'OUT
');
2210 # generate firewall rules for OpenVZ containers
2211 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2212 my $conf = $vmdata->{openvz}->{$vmid};
2214 my $vmfw_conf = $vmfw_configs->{$vmid};
2215 next if !$vmfw_conf;
2216 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
2218 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
2219 my $ip = $conf->{ip_address}->{value};
2220 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'IN
');
2221 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'OUT
');
2224 if ($conf->{netif} && $conf->{netif}->{value}) {
2225 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
2226 foreach my $netid (keys %$netif) {
2227 my $d = $netif->{$netid};
2228 my $bridge = $d->{bridge};
2230 warn "no bridge device for CT $vmid iface '$netid'\n";
2234 generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table);
2236 my $macaddr = $d->{mac};
2237 my $iface = $d->{host_ifname};
2238 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2239 $vmfw_conf, $vmid, $bridge, 'IN
');
2240 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2241 $vmfw_conf, $vmid, $bridge, 'OUT
');
2246 if($hostfw_options->{optimize}){
2248 my $accept = ruleset_chain_exist($ruleset, "PVEFW-IPS") ? "PVEFW-IPS" : "ACCEPT";
2249 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept");
2250 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate INVALID -j DROP");
2253 # fixme: what log level should we use here?
2254 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
2256 # fixme: should we really block inter-bridge traffic?
2258 # always allow traffic from containers?
2259 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN");
2261 # disable interbridge routing
2262 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
2263 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
2264 ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+");
2265 ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+");
2266 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
2267 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
2269 return wantarray ? ($ruleset, $hostfw_conf, $ipset_ruleset) : $ruleset;
2272 sub get_ruleset_status {
2273 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
2275 my $statushash = {};
2277 foreach my $chain (sort keys %$ruleset) {
2278 my $sig = &$digest_fn($ruleset->{$chain});
2280 $statushash->{$chain}->{sig} = $sig;
2282 my $oldsig = $active_chains->{$chain};
2283 if (!defined($oldsig)) {
2284 $statushash->{$chain}->{action} = 'create
';
2286 if ($oldsig eq $sig) {
2287 $statushash->{$chain}->{action} = 'exists';
2289 $statushash->{$chain}->{action} = 'update
';
2292 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
2293 foreach my $cmd (@{$ruleset->{$chain}}) {
2294 print "\t$cmd\n" if $verbose;
2298 foreach my $chain (sort keys %$active_chains) {
2299 if (!defined($ruleset->{$chain})) {
2300 my $sig = $active_chains->{$chain};
2301 $statushash->{$chain}->{action} = 'delete';
2302 $statushash->{$chain}->{sig} = $sig;
2303 print "delete $chain ($sig)\n" if $verbose;
2310 sub print_sig_rule {
2311 my ($chain, $sig) = @_;
2313 # We just use this to store a SHA1 checksum used to detect changes
2314 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
2317 sub get_ruleset_cmdlist {
2318 my ($ruleset, $verbose) = @_;
2320 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
2322 my ($active_chains, $hooks) = iptables_get_chains();
2323 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
2325 # create missing chains first
2326 foreach my $chain (sort keys %$ruleset) {
2327 my $stat = $statushash->{$chain};
2328 die "internal error" if !$stat;
2329 next if $stat->{action} ne 'create
';
2331 $cmdlist .= ":$chain - [0:0]\n";
2334 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2335 if (!$hooks->{$h}) {
2336 $cmdlist .= "-A $h -j PVEFW-$h\n";
2340 foreach my $chain (sort keys %$ruleset) {
2341 my $stat = $statushash->{$chain};
2342 die "internal error" if !$stat;
2344 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
2345 $cmdlist .= "-F $chain\n";
2346 foreach my $cmd (@{$ruleset->{$chain}}) {
2347 $cmdlist .= "$cmd\n";
2349 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
2350 } elsif ($stat->{action
} eq 'delete') {
2351 die "internal error"; # this should not happen
2352 } elsif ($stat->{action
} eq 'exists') {
2355 die "internal error - unknown status '$stat->{action}'";
2359 foreach my $chain (keys %$statushash) {
2360 next if $statushash->{$chain}->{action
} ne 'delete';
2361 $cmdlist .= "-F $chain\n";
2363 foreach my $chain (keys %$statushash) {
2364 next if $statushash->{$chain}->{action
} ne 'delete';
2365 next if $chain eq 'PVEFW-INPUT';
2366 next if $chain eq 'PVEFW-OUTPUT';
2367 next if $chain eq 'PVEFW-FORWARD';
2368 $cmdlist .= "-X $chain\n";
2371 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
2373 $cmdlist .= "COMMIT\n";
2375 return wantarray ?
($cmdlist, $changes) : $cmdlist;
2378 sub get_ipset_cmdlist
{
2379 my ($ruleset, $verbose) = @_;
2383 my $delete_cmdlist = "";
2385 my $active_chains = ipset_get_chains
();
2386 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
2388 foreach my $chain (sort keys %$ruleset) {
2389 my $stat = $statushash->{$chain};
2390 die "internal error" if !$stat;
2392 if ($stat->{action
} eq 'create') {
2393 foreach my $cmd (@{$ruleset->{$chain}}) {
2394 $cmdlist .= "$cmd\n";
2398 if ($stat->{action
} eq 'update') {
2399 my $chain_swap = $chain."_swap";
2401 foreach my $cmd (@{$ruleset->{$chain}}) {
2402 $cmd =~ s/$chain/$chain_swap/;
2403 $cmdlist .= "$cmd\n";
2405 $cmdlist .= "swap $chain_swap $chain\n";
2406 $cmdlist .= "flush $chain_swap\n";
2407 $cmdlist .= "destroy $chain_swap\n";
2412 foreach my $chain (keys %$statushash) {
2413 next if $statushash->{$chain}->{action
} ne 'delete';
2415 $delete_cmdlist .= "flush $chain\n";
2416 $delete_cmdlist .= "destroy $chain\n";
2419 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
2421 return ($cmdlist, $delete_cmdlist, $changes);
2425 my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_;
2427 enable_bridge_firewall
();
2429 update_nf_conntrack_max
($hostfw_conf);
2431 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
2432 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
2434 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
2437 if ($ipset_changes) {
2438 print "ipset changes:\n";
2439 print $ipset_create_cmdlist if $ipset_create_cmdlist;
2440 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
2444 print "iptables changes:\n";
2449 ipset_restore_cmdlist
($ipset_create_cmdlist);
2451 iptables_restore_cmdlist
($cmdlist);
2453 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
2455 # test: re-read status and check if everything is up to date
2456 my $active_chains = iptables_get_chains
();
2457 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
2460 foreach my $chain (sort keys %$ruleset) {
2461 my $stat = $statushash->{$chain};
2462 if ($stat->{action
} ne 'exists') {
2463 warn "unable to update chain '$chain'\n";
2468 die "unable to apply firewall changes\n" if $errors;
2471 sub update_nf_conntrack_max
{
2472 my ($hostfw_conf) = @_;
2474 my $max = 65536; # reasonable default
2476 my $options = $hostfw_conf->{options
} || {};
2478 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
2479 $max = $options->{nf_conntrack_max
};
2480 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
2483 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
2484 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
2486 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
2488 if ($current != $max) {
2489 my $hashsize = int($max/4);
2490 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
2491 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
2495 sub remove_pvefw_chains
{
2497 my ($chash, $hooks) = iptables_get_chains
();
2498 my $cmdlist = "*filter\n";
2500 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2502 $cmdlist .= "-D $h -j PVEFW-$h\n";
2506 foreach my $chain (keys %$chash) {
2507 $cmdlist .= "-F $chain\n";
2510 foreach my $chain (keys %$chash) {
2511 $cmdlist .= "-X $chain\n";
2513 $cmdlist .= "COMMIT\n";
2515 iptables_restore_cmdlist
($cmdlist);
2519 my ($start, $verbose) = @_;
2522 my $status = read_pvefw_status
();
2524 my ($ruleset, $hostfw_conf, $ipset_ruleset) = compile
();
2526 if ($start || $status eq 'active') {
2528 save_pvefw_status
('active') if ($status ne 'active');
2530 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $verbose);
2532 print "Firewall not active (status = $status)\n" if $verbose;