8 use PVE
::JSONSchema
qw(get_standard_option);
16 use PVE
::Tools
qw(run_command lock_file);
19 my $clusterfw_conf_filename = "/etc/pve/firewall/cluster.fw";
21 # dynamically include PVE::QemuServer and PVE::OpenVZ
22 # to avoid dependency problems
25 require PVE
::QemuServer
;
26 $have_qemu_server = 1;
32 $have_pve_manager = 1;
37 my $nodename = PVE
::INotify
::nodename
();
39 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
40 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
42 my $default_log_level = 'info';
44 my $log_level_hash = {
55 # imported/converted from: /usr/share/shorewall/macro.*
58 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
59 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
62 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
65 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
68 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
69 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
72 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
73 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
76 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
79 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
80 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
81 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
84 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
85 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
88 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
91 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
92 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
95 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
96 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
99 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
102 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
103 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
106 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
109 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
112 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
113 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
114 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
115 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
118 { action
=> 'PARAM', proto
=> '47' },
119 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
122 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
125 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
126 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
129 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
132 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
135 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
138 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
141 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
144 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
147 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
150 { action
=> 'PARAM', proto
=> '94' },
151 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
154 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
157 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
160 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
161 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
164 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
165 { action
=> 'PARAM', proto
=> '50' },
166 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
167 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
170 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
171 { action
=> 'PARAM', proto
=> '51' },
172 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
173 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
176 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
177 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
178 { action
=> 'PARAM', proto
=> '50' },
179 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
180 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
181 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
184 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
187 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
190 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
193 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
196 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
199 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
200 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
203 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
206 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
209 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
212 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
215 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
216 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
217 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
220 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
223 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
226 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
229 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
232 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
235 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
236 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
239 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
240 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
243 { action
=> 'PARAM', proto
=> '89' },
246 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
249 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
250 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
253 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
256 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
259 { action
=> 'PARAM', proto
=> '47' },
260 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
261 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
264 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
267 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
270 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
273 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
276 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
277 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
280 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
283 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
286 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
289 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
292 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
295 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
296 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
297 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
298 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
301 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
302 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
303 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
304 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
305 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
306 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
307 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
308 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
311 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
314 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
317 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
320 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
321 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
324 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
327 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
330 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
333 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
334 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
335 { action
=> 'PARAM', proto
=> '41' },
336 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
339 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
342 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
345 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
346 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
349 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
352 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
355 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
358 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
361 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
362 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
365 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
368 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
371 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
372 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
375 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
378 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
381 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
385 my $pve_fw_parsed_macros;
386 my $pve_fw_preferred_macro_names = {};
388 my $pve_std_chains = {
389 'PVEFW-SET-ACCEPT-MARK' => [
390 "-j MARK --set-mark 1",
392 'PVEFW-DropBroadcast' => [
393 # same as shorewall 'Broadcast'
394 # simply DROP BROADCAST/MULTICAST/ANYCAST
395 # we can use this to reduce logging
396 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
397 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
398 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
399 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
402 # same as shorewall 'reject'
403 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
404 { action
=> 'DROP', source
=> '224.0.0.0/4' },
405 { action
=> 'DROP', proto
=> 'icmp' },
406 "-p tcp -j REJECT --reject-with tcp-reset",
407 "-p udp -j REJECT --reject-with icmp-port-unreachable",
408 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
409 "-j REJECT --reject-with icmp-host-prohibited",
412 # same as shorewall 'Drop', which is equal to DROP,
413 # but REJECT/DROP some packages to reduce logging,
414 # and ACCEPT critical ICMP types
415 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
416 # we are not interested in BROADCAST/MULTICAST/ANYCAST
417 { action
=> 'PVEFW-DropBroadcast' },
418 # ACCEPT critical ICMP types
419 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
420 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
421 # Drop packets with INVALID state
422 "-m conntrack --ctstate INVALID -j DROP",
423 # Drop Microsoft SMB noise
424 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
425 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
426 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
427 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
428 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
429 # Drop new/NotSyn traffic so that it doesn't get logged
430 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
432 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
435 # same as shorewall 'Reject', which is equal to Reject,
436 # but REJECT/DROP some packages to reduce logging,
437 # and ACCEPT critical ICMP types
438 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
439 # we are not interested in BROADCAST/MULTICAST/ANYCAST
440 { action
=> 'PVEFW-DropBroadcast' },
441 # ACCEPT critical ICMP types
442 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
443 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
444 # Drop packets with INVALID state
445 "-m conntrack --ctstate INVALID -j DROP",
446 # Drop Microsoft SMB noise
447 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
448 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
449 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
450 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
451 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
452 # Drop new/NotSyn traffic so that it doesn't get logged
453 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
455 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
457 'PVEFW-tcpflags' => [
458 # same as shorewall tcpflags action.
459 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
460 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
461 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
462 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
463 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
464 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
467 # same as shorewall smurfs action
468 # Filter packets for smurfs (packets with a broadcast address as the source).
469 "-s 0.0.0.0/32 -j RETURN",
470 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
471 "-s 224.0.0.0/4 -g PVEFW-smurflog",
475 # iptables -p icmp -h
476 my $icmp_type_names = {
479 'destination-unreachable' => 1,
480 'network-unreachable' => 1,
481 'host-unreachable' => 1,
482 'protocol-unreachable' => 1,
483 'port-unreachable' => 1,
484 'fragmentation-needed' => 1,
485 'source-route-failed' => 1,
486 'network-unknown' => 1,
488 'network-prohibited' => 1,
489 'host-prohibited' => 1,
490 'TOS-network-unreachable' => 1,
491 'TOS-host-unreachable' => 1,
492 'communication-prohibited' => 1,
493 'host-precedence-violation' => 1,
494 'precedence-cutoff' => 1,
495 'source-quench' => 1,
497 'network-redirect' => 1,
498 'host-redirect' => 1,
499 'TOS-network-redirect' => 1,
500 'TOS-host-redirect' => 1,
502 'router-advertisement' => 1,
503 'router-solicitation' => 1,
504 'time-exceeded' => 1,
505 'ttl-zero-during-transit' => 1,
506 'ttl-zero-during-reassembly' => 1,
507 'parameter-problem' => 1,
508 'ip-header-bad' => 1,
509 'required-option-missing' => 1,
510 'timestamp-request' => 1,
511 'timestamp-reply' => 1,
512 'address-mask-request' => 1,
513 'address-mask-reply' => 1,
516 sub init_firewall_macros
{
518 $pve_fw_parsed_macros = {};
520 foreach my $k (keys %$pve_fw_macros) {
521 my $lc_name = lc($k);
522 my $macro = $pve_fw_macros->{$k};
523 $pve_fw_preferred_macro_names->{$lc_name} = $k;
524 $pve_fw_parsed_macros->{$k} = $macro;
528 init_firewall_macros
();
532 sub get_etc_services
{
534 return $etc_services if $etc_services;
536 my $filename = "/etc/services";
538 my $fh = IO
::File-
>new($filename, O_RDONLY
);
540 warn "unable to read '$filename' - $!\n";
546 while (my $line = <$fh>) {
548 next if $line =~m/^#/;
549 next if ($line =~m/^\s*$/);
551 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
552 $services->{byid
}->{$2}->{name
} = $1;
553 $services->{byid
}->{$2}->{port
} = $2;
554 $services->{byid
}->{$2}->{$3} = 1;
555 $services->{byname
}->{$1} = $services->{byid
}->{$2};
561 $etc_services = $services;
564 return $etc_services;
569 sub get_etc_protocols
{
570 return $etc_protocols if $etc_protocols;
572 my $filename = "/etc/protocols";
574 my $fh = IO
::File-
>new($filename, O_RDONLY
);
576 warn "unable to read '$filename' - $!\n";
582 while (my $line = <$fh>) {
584 next if $line =~m/^#/;
585 next if ($line =~m/^\s*$/);
587 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
588 $protocols->{byid
}->{$2}->{name
} = $1;
589 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
595 $etc_protocols = $protocols;
597 return $etc_protocols;
600 sub parse_address_list
{
604 foreach my $aor (split(/,/, $str)) {
605 if (!Net
::IP-
>new($aor)) {
606 my $err = Net
::IP
::Error
();
607 die "invalid IP address: $err\n";
615 sub parse_port_name_number_or_range
{
618 my $services = PVE
::Firewall
::get_etc_services
();
620 foreach my $item (split(/,/, $str)) {
624 foreach my $pon (split(':', $item, 2)) {
625 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
626 if ($pon =~ m/^\d+$/){
627 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
628 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
631 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
639 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
640 sub pve_fw_verify_port_spec
{
643 parse_port_name_number_or_range
($portstr);
648 PVE
::JSONSchema
::register_format
('pve-fw-v4addr-spec', \
&pve_fw_verify_v4addr_spec
);
649 sub pve_fw_verify_v4addr_spec
{
652 parse_address_list
($list);
657 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
658 sub pve_fw_verify_protocol_spec
{
661 my $protocols = get_etc_protocols
();
663 die "unknown protocol '$proto'\n" if $proto &&
664 !(defined($protocols->{byname
}->{$proto}) ||
665 defined($protocols->{byid
}->{$proto}));
671 # helper function for API
673 my $rule_properties = {
675 description
=> "Update rule at position <pos>.",
689 enum
=> ['in', 'out', 'group'],
694 enum
=> ['ACCEPT', 'DROP', 'REJECT'],
701 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
703 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
707 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
711 type
=> 'string', format
=> 'pve-fw-protocol-spec',
719 type
=> 'string', format
=> 'pve-fw-port-spec',
723 type
=> 'string', format
=> 'pve-fw-port-spec',
732 sub cleanup_fw_rule
{
733 my ($rule, $digest, $pos) = @_;
737 foreach my $k (keys %$rule) {
738 next if !$rule_properties->{$k};
740 next if !defined($v);
742 $r->{digest
} = $digest;
749 sub add_rule_properties
{
750 my ($properties) = @_;
752 foreach my $k (keys %$rule_properties) {
753 $properties->{$k} = $rule_properties->{$k};
760 my ($rule, $param) = @_;
762 foreach my $k (keys %$rule_properties) {
763 if (defined(my $v = $param->{$k})) {
764 if ($v eq '' || $v eq '-') {
777 my $bridge_firewall_enabled = 0;
779 sub enable_bridge_firewall
{
781 return if $bridge_firewall_enabled; # only once
783 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
784 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
786 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
787 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
789 $bridge_firewall_enabled = 1;
792 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
797 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
800 sub iptables_restore_cmdlist
{
803 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
806 sub ipset_restore_cmdlist
{
809 run_command
(" /usr/sbin/ipset restore", input
=> $cmdlist);
812 sub iptables_get_chains
{
816 # check what chains we want to track
817 my $is_pvefw_chain = sub {
820 return 1 if $name =~ m/^PVEFW-\S+$/;
822 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
824 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
826 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
828 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT|IPS)$/;
829 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
841 return if $line =~ m/^#/;
842 return if $line =~ m/^\s*$/;
844 if ($line =~ m/^\*(\S+)$/) {
849 return if $table ne 'filter';
851 if ($line =~ m/^:(\S+)\s/) {
853 return if !&$is_pvefw_chain($chain);
854 $res->{$chain} = "unknown";
855 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
856 my ($chain, $sig) = ($1, $2);
857 return if !&$is_pvefw_chain($chain);
858 $res->{$chain} = $sig;
859 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
862 # simply ignore the rest
867 run_command
("/sbin/iptables-save", outfunc
=> $parser);
869 return wantarray ?
($res, $hooks) : $res;
872 sub iptables_chain_digest
{
874 my $digest = Digest
::SHA-
>new('sha1');
875 foreach my $rule (@$rules) { # order is important
878 return $digest->b64digest;
881 sub ipset_chain_digest
{
883 my $digest = Digest
::SHA-
>new('sha1');
884 foreach my $rule (sort @$rules) { # note: sorted
887 return $digest->b64digest;
890 sub ipset_get_chains
{
898 return if $line =~ m/^#/;
899 return if $line =~ m/^\s*$/;
900 if ($line =~ m/^(\S+)\s(\S+)\s(\S+)/) {
901 push @{$chains->{$2}}, $line;
903 # simply ignore the rest
908 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
910 # compute digest for each chain
911 foreach my $chain (keys %$chains) {
912 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
918 sub iptables_chain_exist
{
922 iptables
("-n --list $chain");
929 sub iptables_rule_exist
{
933 iptables
("-C $rule");
940 sub ruleset_generate_cmdstr
{
941 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
943 return if defined($rule->{enable
}) && !$rule->{enable
};
945 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
947 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
948 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
949 my $nbsource = $rule->{source
} ? parse_address_list
( $rule->{source
}) : 0;
950 my $nbdest = $rule->{dest
} ? parse_address_list
($rule->{dest
}) : 0;
954 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
955 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
957 push @cmd, "-m iprange --src-range" if $nbsource > 1;
958 push @cmd, "-s $rule->{source}" if $rule->{source
};
959 push @cmd, "-m iprange --dst-range" if $nbdest > 1;
960 push @cmd, "-d $rule->{dest}" if $rule->{dest
};
962 if ($rule->{proto
}) {
963 push @cmd, "-p $rule->{proto}";
966 $multiport++ if $nbdport > 1;
967 $multiport++ if $nbsport > 1;
969 push @cmd, "--match multiport" if $multiport;
971 die "multiport: option '--sports' cannot be used together with '--dports'\n"
972 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
974 if ($rule->{dport
}) {
975 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
976 # Note: we use dport to store --icmp-type
977 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
978 push @cmd, "-m icmp --icmp-type $rule->{dport}";
981 if ($multiport == 2) {
982 push @cmd, "--ports $rule->{dport}";
984 push @cmd, "--dports $rule->{dport}";
987 push @cmd, "--dport $rule->{dport}";
992 if ($rule->{sport
}) {
994 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
996 push @cmd, "--sport $rule->{sport}";
999 } elsif ($rule->{dport
} || $rule->{sport
}) {
1000 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
1001 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
1004 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1006 if (my $action = $rule->{action
}) {
1007 $action = $actions->{$action} if defined($actions->{$action});
1008 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1009 push @cmd, $goto ?
"-g $action" : "-j $action";
1012 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1015 my $apply_macro = sub {
1016 my ($macro_name, $param) = @_;
1018 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1019 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1023 foreach my $templ (@$macro_rules) {
1025 my $param_used = {};
1026 foreach my $k (keys %$templ) {
1027 my $v = $templ->{$k};
1028 if ($v eq 'PARAM') {
1030 $param_used->{$k} = 1;
1031 } elsif ($v eq 'DEST') {
1032 $v = $param->{dest
};
1033 $param_used->{dest
} = 1;
1034 } elsif ($v eq 'SOURCE') {
1035 $v = $param->{source
};
1036 $param_used->{source
} = 1;
1039 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1042 foreach my $k (keys %$param) {
1043 next if $k eq 'macro';
1044 next if !defined($param->{$k});
1045 next if $param_used->{$k};
1046 if (defined($rule->{$k})) {
1047 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1048 if $rule->{$k} ne $param->{$k};
1050 $rule->{$k} = $param->{$k};
1053 push @$rules, $rule;
1059 sub ruleset_generate_rule
{
1060 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1064 if ($rule->{macro}) {
1065 $rules = &$apply_macro($rule->{macro}, $rule);
1070 foreach my $tmp (@$rules) {
1071 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $tmp, $actions, $goto)) {
1072 ruleset_addrule
($ruleset, $chain, $cmdstr);
1077 sub ruleset_generate_rule_insert
{
1078 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1080 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1082 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
1083 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1087 sub ruleset_create_chain
{
1088 my ($ruleset, $chain) = @_;
1090 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1091 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1093 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1095 $ruleset->{$chain} = [];
1098 sub ruleset_chain_exist
{
1099 my ($ruleset, $chain) = @_;
1101 return $ruleset->{$chain} ?
1 : undef;
1104 sub ruleset_addrule
{
1105 my ($ruleset, $chain, $rule) = @_;
1107 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1109 push @{$ruleset->{$chain}}, "-A $chain $rule";
1112 sub ruleset_insertrule
{
1113 my ($ruleset, $chain, $rule) = @_;
1115 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1117 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1120 sub get_log_rule_base
{
1121 my ($chain, $vmid, $msg, $loglevel) = @_;
1123 die "internal error - no log level" if !defined($loglevel);
1125 $vmid = 0 if !defined($vmid);
1127 # Note: we use special format for prefix to pass further
1128 # info to log daemon (VMID, LOGVELEL and CHAIN)
1130 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1133 sub ruleset_addlog {
1134 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1136 return if !defined($loglevel);
1138 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1140 $logrule = "$rule $logrule" if defined($rule);
1142 ruleset_addrule($ruleset, $chain, $logrule)
1145 sub generate_bridge_chains {
1146 my ($ruleset, $hostfw_conf, $bridge, $routing_table) = @_;
1148 my $options = $hostfw_conf->{options} || {};
1150 die "error
: detected direct route to bridge
'$bridge'\n"
1151 if !$options->{allow_bridge_route} && $routing_table->{$bridge};
1153 if (!ruleset_chain_exist($ruleset, "$bridge-FW
")) {
1154 ruleset_create_chain($ruleset, "$bridge-FW
");
1155 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-FW
");
1156 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-FW
");
1159 if (!ruleset_chain_exist($ruleset, "$bridge-OUT
")) {
1160 ruleset_create_chain($ruleset, "$bridge-OUT
");
1161 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-in
-j
$bridge-OUT
");
1162 ruleset_insertrule($ruleset, "PVEFW-INPUT
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-OUT
");
1165 if (!ruleset_chain_exist($ruleset, "$bridge-IN
")) {
1166 ruleset_create_chain($ruleset, "$bridge-IN
");
1167 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j
$bridge-IN
");
1168 ruleset_addrule($ruleset, "$bridge-FW
", "-m mark
--mark
1 -j ACCEPT
");
1169 # accept traffic to unmanaged bridge ports
1170 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j ACCEPT
");
1174 sub ruleset_add_chain_policy {
1175 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
1177 if ($policy eq 'ACCEPT') {
1179 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
1180 { ACCEPT => $accept_action});
1182 } elsif ($policy eq 'DROP') {
1184 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1186 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1188 ruleset_addrule($ruleset, $chain, "-j DROP
");
1189 } elsif ($policy eq 'REJECT') {
1190 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1192 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1194 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1197 die "internal error
: unknown policy
'$policy'";
1201 sub ruleset_create_vm_chain {
1202 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
1204 ruleset_create_chain($ruleset, $chain);
1205 my $accept = generate_nfqueue($options);
1207 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1208 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1211 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1212 if ($direction eq 'OUT') {
1213 ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK',
1214 proto => 'udp', sport => 68, dport => 67 });
1216 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT',
1217 proto => 'udp', sport => 67, dport => 68 });
1221 if ($options->{tcpflags}) {
1222 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1225 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1226 if($direction eq 'OUT'){
1227 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-g PVEFW-SET-ACCEPT-MARK
");
1230 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1232 if ($direction eq 'OUT') {
1233 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1234 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1236 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1242 sub ruleset_generate_vm_rules {
1243 my ($ruleset, $rules, $cluster_conf, $chain, $netid, $direction, $options) = @_;
1245 my $lc_direction = lc($direction);
1247 foreach my $rule (@$rules) {
1248 next if $rule->{iface} && $rule->{iface} ne $netid;
1249 next if !$rule->{enable};
1250 if ($rule->{type} eq 'group') {
1251 my $group_chain = "GROUP-
$rule->{action
}-$direction";
1252 if(!ruleset_chain_exist($ruleset, $group_chain)){
1253 generate_group_rules($ruleset, $cluster_conf, $rule->{action});
1255 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1256 if ($direction eq 'OUT'){
1257 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j RETURN
");
1259 my $accept = generate_nfqueue($options);
1260 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$accept");
1264 next if $rule->{type} ne $lc_direction;
1265 if ($direction eq 'OUT') {
1266 ruleset_generate_rule($ruleset, $chain, $rule,
1267 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1269 my $accept = generate_nfqueue($options);
1270 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject
" });
1276 sub generate_nfqueue {
1280 if($options->{ips}){
1281 $action = "NFQUEUE
";
1282 if($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1283 if(defined($3) && defined($1)) {
1284 $action .= " --queue-balance
$1:$3";
1285 }elsif (defined($1)) {
1286 $action .= " --queue-num
$1";
1289 $action .= " --queue-bypass
";
1297 sub ruleset_generate_vm_ipsrules {
1298 my ($ruleset, $options, $direction, $iface, $bridge) = @_;
1300 if ($options->{ips} && $direction eq 'IN') {
1301 my $nfqueue = generate_nfqueue($options);
1303 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1304 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1307 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1308 ruleset_create_chain($ruleset, "$bridge-IPS
");
1309 ruleset_insertrule($ruleset, "PVEFW-IPS
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-IPS
");
1312 ruleset_addrule($ruleset, "$bridge-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1316 sub generate_venet_rules_direction {
1317 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
1319 parse_address_list($ip); # make sure we have a valid $ip list
1321 my $lc_direction = lc($direction);
1323 my $rules = $vmfw_conf->{rules};
1325 my $options = $vmfw_conf->{options};
1326 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1328 my $chain = "venet0-
$vmid-$direction";
1330 ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction);
1332 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $chain, 'venet', $direction);
1337 if ($direction eq 'OUT') {
1338 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1340 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1343 my $accept = generate_nfqueue($options);
1344 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1345 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1347 # plug into FORWARD, INPUT and OUTPUT chain
1348 if ($direction eq 'OUT') {
1349 ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD
", {
1352 iface_in => 'venet0'});
1354 ruleset_generate_rule_insert($ruleset, "PVEFW-INPUT
", {
1357 iface_in => 'venet0'});
1359 ruleset_generate_rule($ruleset, "PVEFW-FORWARD
", {
1362 iface_out => 'venet0'});
1364 ruleset_generate_rule($ruleset, "PVEFW-OUTPUT
", {
1367 iface_out => 'venet0'});
1371 sub generate_tap_rules_direction {
1372 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_;
1374 my $lc_direction = lc($direction);
1376 my $rules = $vmfw_conf->{rules};
1378 my $options = $vmfw_conf->{options};
1379 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1381 my $tapchain = "$iface-$direction";
1383 ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction);
1385 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $tapchain, $netid, $direction, $options);
1387 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface, $bridge);
1392 if ($direction eq 'OUT') {
1393 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1395 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1398 my $accept = generate_nfqueue($options);
1399 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1400 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
1402 # plug the tap chain to bridge chain
1403 if ($direction eq 'IN') {
1404 ruleset_insertrule($ruleset, "$bridge-IN
",
1405 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
1407 ruleset_insertrule($ruleset, "$bridge-OUT
",
1408 "-m physdev
--physdev-in
$iface -j
$tapchain");
1412 sub enable_host_firewall {
1413 my ($ruleset, $hostfw_conf, $cluster_conf) = @_;
1415 # fixme: allow security groups
1417 my $options = $hostfw_conf->{options};
1418 my $rules = $hostfw_conf->{rules};
1420 # host inbound firewall
1421 my $chain = "PVEFW-HOST-IN
";
1422 ruleset_create_chain($ruleset, $chain);
1424 my $loglevel = get_option_log_level($options, "log_level_in
");
1426 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1427 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1430 if ($options->{tcpflags}) {
1431 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1434 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1435 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1436 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
1437 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1438 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1439 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1441 # we use RETURN because we need to check also tap rules
1442 my $accept_action = 'RETURN';
1444 foreach my $rule (@$rules) {
1445 next if $rule->{type} ne 'in';
1446 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1449 # implement input policy
1450 my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1451 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1453 # host outbound firewall
1454 $chain = "PVEFW-HOST-OUT
";
1455 ruleset_create_chain($ruleset, $chain);
1457 $loglevel = get_option_log_level($options, "log_level_out
");
1459 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1460 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1461 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
1462 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1463 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1464 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1466 # we use RETURN because we may want to check other thigs later
1467 $accept_action = 'RETURN';
1469 foreach my $rule (@$rules) {
1470 next if $rule->{type} ne 'out';
1471 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1474 # implement output policy
1475 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1476 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1478 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
1479 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
1482 sub generate_group_rules {
1483 my ($ruleset, $cluster_conf, $group) = @_;
1484 die "no such security group
'$group'\n" if !$cluster_conf->{groups}->{$group};
1486 my $rules = $cluster_conf->{groups}->{$group};
1488 my $chain = "GROUP-
${group
}-IN
";
1490 ruleset_create_chain($ruleset, $chain);
1491 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1493 foreach my $rule (@$rules) {
1494 next if $rule->{type} ne 'in';
1495 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1498 $chain = "GROUP-
${group
}-OUT
";
1500 ruleset_create_chain($ruleset, $chain);
1501 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1503 foreach my $rule (@$rules) {
1504 next if $rule->{type} ne 'out';
1505 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1506 # check also other tap rules later
1507 ruleset_generate_rule($ruleset, $chain, $rule,
1508 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" });
1513 my $valid_netdev_names = {};
1514 for (my $i = 0; $i < $MAX_NETS; $i++) {
1515 $valid_netdev_names->{"net
$i"} = 1;
1519 my ($line, $need_iface, $allow_groups) = @_;
1521 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1523 # we can add single line comments to the end of the rule
1524 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
1526 # we can disable a rule when prefixed with '|'
1529 $enable = 0 if $line =~ s/^\|//;
1531 my @data = split(/\s+/, $line);
1532 my $expected_elements = $need_iface ? 8 : 7;
1534 die "wrong number of rule elements
\n" if scalar(@data) > $expected_elements;
1537 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1539 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1542 die "incomplete rule
\n" if ! ($type && $action);
1548 if ($type eq 'in' || $type eq 'out') {
1549 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1551 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1553 my $preferred_name = $pve_fw_preferred_macro_names->{lc($1)};
1554 die "unknown
macro '$1'\n" if !$preferred_name;
1555 $macro = $preferred_name;
1557 die "unknown action
'$action'\n";
1559 } elsif ($type eq 'group') {
1560 die "wrong number of rule elements
\n" if scalar(@data) != 3;
1561 die "groups disabled
\n" if !$allow_groups;
1563 die "invalid characters
in group name
\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1565 die "unknown rule type
'$type'\n";
1569 $iface = undef if $iface && $iface eq '-';
1572 $proto = undef if $proto && $proto eq '-';
1573 pve_fw_verify_protocol_spec($proto) if $proto;
1575 $source = undef if $source && $source eq '-';
1576 $dest = undef if $dest && $dest eq '-';
1578 $dport = undef if $dport && $dport eq '-';
1579 $sport = undef if $sport && $sport eq '-';
1581 parse_port_name_number_or_range($dport) if defined($dport);
1582 parse_port_name_number_or_range($sport) if defined($sport);
1584 parse_address_list($source) if $source;
1585 parse_address_list($dest) if $dest;
1590 comment => $comment,
1602 sub parse_vmfw_option {
1607 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
1609 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags|ips):\s*(0|1)\s*$/i) {
1612 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1614 $value = $2 ? lc($3) : '';
1615 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1618 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
1623 die "can
't parse option '$line'\n"
1626 return ($opt, $value);
1629 sub parse_hostfw_option {
1634 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1636 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags|allow_bridge_route|optimize):\s*(0|1)\s*$/i) {
1639 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1641 $value = $2 ? lc($3) : '';
1642 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1645 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1650 die "can't parse option
'$line'\n"
1653 return ($opt, $value);
1656 sub parse_clusterfw_option {
1661 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
1666 die "can
't parse option '$line'\n"
1669 return ($opt, $value);
1672 sub parse_vm_fw_rules {
1673 my ($filename, $fh) = @_;
1675 my $res = { rules => [], options => {}};
1679 my $digest = Digest::SHA->new('sha1
');
1681 while (defined(my $line = <$fh>)) {
1682 $digest->add($line);
1684 next if $line =~ m/^#/;
1685 next if $line =~ m/^\s*$/;
1687 my $linenr = $fh->input_line_number();
1688 my $prefix = "$filename (line $linenr)";
1690 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1692 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1696 warn "$prefix: skip line - no section";
1700 next if !$res->{$section}; # skip undefined section
1702 if ($section eq 'options
') {
1704 my ($opt, $value) = parse_vmfw_option($line);
1705 $res->{options}->{$opt} = $value;
1707 warn "$prefix: $@" if $@;
1712 eval { $rule = parse_fw_rule($line, 1, 1); };
1714 warn "$prefix: $err";
1718 push @{$res->{$section}}, $rule;
1721 $res->{digest} = $digest->b64digest;
1726 sub parse_host_fw_rules {
1727 my ($filename, $fh) = @_;
1729 my $res = { rules => [], options => {}};
1733 my $digest = Digest::SHA->new('sha1
');
1735 while (defined(my $line = <$fh>)) {
1736 $digest->add($line);
1738 next if $line =~ m/^#/;
1739 next if $line =~ m/^\s*$/;
1741 my $linenr = $fh->input_line_number();
1742 my $prefix = "$filename (line $linenr)";
1744 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1746 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1750 warn "$prefix: skip line - no section";
1754 next if !$res->{$section}; # skip undefined section
1756 if ($section eq 'options
') {
1758 my ($opt, $value) = parse_hostfw_option($line);
1759 $res->{options}->{$opt} = $value;
1761 warn "$prefix: $@" if $@;
1766 eval { $rule = parse_fw_rule($line, 1, 1); };
1768 warn "$prefix: $err";
1772 push @{$res->{$section}}, $rule;
1775 $res->{digest} = $digest->b64digest;
1780 sub parse_cluster_fw_rules {
1781 my ($filename, $fh) = @_;
1786 my $res = { rules => [], options => {}, groups => {}, ipset => {} };
1788 my $digest = Digest::SHA->new('sha1
');
1790 while (defined(my $line = <$fh>)) {
1791 $digest->add($line);
1793 next if $line =~ m/^#/;
1794 next if $line =~ m/^\s*$/;
1796 my $linenr = $fh->input_line_number();
1797 my $prefix = "$filename (line $linenr)";
1799 if ($line =~ m/^\[options\]$/i) {
1800 $section = 'options
';
1804 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1805 $section = 'groups
';
1810 if ($line =~ m/^\[rules\]$/i) {
1815 if ($line =~ m/^\[ipgroup\s+(\S+)\]\s*$/i) {
1818 $res->{$section}->{$group}->{type} = 'hash
:ip family inet hashsize
1024 maxelem
65536 ';
1822 if ($line =~ m/^\[netgroup\s+(\S+)\]\s*$/i) {
1825 $res->{$section}->{$group}->{type} = 'hash
:net family inet hashsize
1024 maxelem
65536 ';
1830 warn "$prefix: skip line - no section";
1834 if ($section eq 'options
') {
1836 my ($opt, $value) = parse_clusterfw_option($line);
1837 $res->{options}->{$opt} = $value;
1839 warn "$prefix: $@" if $@;
1840 } elsif ($section eq 'rules
') {
1842 eval { $rule = parse_fw_rule($line, 1, 1); };
1844 warn "$prefix: $err";
1847 push @{$res->{$section}}, $rule;
1848 } elsif ($section eq 'groups
') {
1850 eval { $rule = parse_fw_rule($line, 0, 0); };
1852 warn "$prefix: $err";
1855 push @{$res->{$section}->{$group}}, $rule;
1856 } elsif ($section eq 'ipset
') {
1859 if (!Net::IP->new($line)) {
1860 warn "$prefix: $line is not an valid ip address";
1863 push @{$res->{$section}->{$group}->{ip}}, $line;
1867 $res->{digest} = $digest->b64digest;
1873 my ($code, @param) = @_;
1877 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1884 sub read_local_vm_config {
1889 my $vmdata = { openvz => $openvz, qemu => $qemu };
1891 my $vmlist = PVE::Cluster::get_vmlist();
1892 return $vmdata if !$vmlist || !$vmlist->{ids};
1893 my $ids = $vmlist->{ids};
1895 foreach my $vmid (keys %$ids) {
1896 next if !$vmid; # skip VE0
1897 my $d = $ids->{$vmid};
1898 next if !$d->{node} || $d->{node} ne $nodename;
1899 next if !$d->{type};
1900 if ($d->{type} eq 'openvz
') {
1901 if ($have_pve_manager) {
1902 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
1903 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1904 $openvz->{$vmid} = $conf;
1907 } elsif ($d->{type} eq 'qemu
') {
1908 if ($have_qemu_server) {
1909 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1910 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1911 $qemu->{$vmid} = $conf;
1920 sub load_vmfw_conf {
1925 my $filename = "/etc/pve/firewall/$vmid.fw";
1926 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1927 $vmfw_conf = parse_vm_fw_rules($filename, $fh);
1933 sub read_vm_firewall_configs {
1935 my $vmfw_configs = {};
1937 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1938 my $vmfw_conf = load_vmfw_conf($vmid);
1939 next if !$vmfw_conf->{options}; # skip if file does not exists
1940 $vmfw_configs->{$vmid} = $vmfw_conf;
1943 return $vmfw_configs;
1946 sub get_option_log_level {
1947 my ($options, $k) = @_;
1949 my $v = $options->{$k};
1950 $v = $default_log_level if !defined($v);
1952 return undef if $v eq '' || $v eq 'nolog
';
1954 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1956 return $v if ($v >= 0) && ($v <= 7);
1958 warn "unknown log level ($k = '$v')\n";
1963 sub generate_std_chains {
1964 my ($ruleset, $options) = @_;
1966 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
1968 # same as shorewall smurflog.
1969 my $chain = 'PVEFW-smurflog
';
1971 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
1972 push @{$pve_std_chains->{$chain}}, "-j DROP";
1974 # same as shorewall logflags action.
1975 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
1976 $chain = 'PVEFW-logflags
';
1977 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
1978 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
1979 push @{$pve_std_chains->{$chain}}, "-j DROP";
1981 foreach my $chain (keys %$pve_std_chains) {
1982 ruleset_create_chain($ruleset, $chain);
1983 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1985 ruleset_generate_rule($ruleset, $chain, $rule);
1987 ruleset_addrule($ruleset, $chain, $rule);
1993 sub generate_ipset_chains {
1994 my ($ipset_ruleset, $options) = @_;
1996 foreach my $ipset (keys %{$options->{ipset}}) {
1997 generate_ipset($ipset_ruleset, $ipset, $options->{ipset}->{$ipset});
2001 sub generate_ipset {
2002 my ($ipset_ruleset, $name, $options) = @_;
2004 push @{$ipset_ruleset->{$name}}, "create $name $options->{type}";
2006 foreach my $ip (@{$options->{ip}}) {
2007 push @{$ipset_ruleset->{$name}}, "add $name $ip";
2011 sub save_pvefw_status {
2014 die "unknown status '$status' - internal error"
2015 if $status !~ m/^(stopped|active)$/;
2017 mkdir dirname($pve_fw_status_filename);
2018 PVE::Tools::file_set_contents($pve_fw_status_filename, $status);
2021 sub read_pvefw_status {
2023 my $status = 'unknown
';
2025 return 'stopped
' if ! -f $pve_fw_status_filename;
2028 $status = PVE::Tools::file_get_contents($pve_fw_status_filename);
2035 # fixme: move to pve-common PVE::ProcFSTools
2036 sub read_proc_net_route {
2037 my $filename = "/proc/net/route";
2041 my $fh = IO::File->new ($filename, "r");
2042 return $res if !$fh;
2044 my $int_to_quad = sub {
2045 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
2048 while (defined(my $line = <$fh>)) {
2049 next if $line =~/^Iface\s+Destination/; # skip head
2050 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
2051 push @{$res->{$iface}}, {
2052 dest => &$int_to_quad(hex($dest)),
2053 gateway => &$int_to_quad(hex($gateway)),
2054 mask => &$int_to_quad(hex($mask)),
2063 sub load_clusterfw_conf {
2065 my $cluster_conf = {};
2066 if (my $fh = IO::File->new($clusterfw_conf_filename, O_RDONLY)) {
2067 $cluster_conf = parse_cluster_fw_rules($clusterfw_conf_filename, $fh);
2070 return $cluster_conf;
2073 my $rules_to_conf = sub {
2074 my ($rules, $need_iface) = @_;
2078 foreach my $rule (@$rules) {
2079 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
') {
2080 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2081 $raw .= uc($rule->{type});
2082 $raw .= " " . $rule->{action};
2083 $raw .= " " . ($rule->{iface} || '-') if $need_iface;
2084 $raw .= " " . ($rule->{source} || '-');
2085 $raw .= " " . ($rule->{dest} || '-');
2086 $raw .= " " . ($rule->{proto} || '-');
2087 $raw .= " " . ($rule->{dport} || '-');
2088 $raw .= " " . ($rule->{sport} || '-');
2089 $raw .= " # " . encode('utf8
', $rule->{comment})
2090 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2093 die "implement me '$rule->{type
}'";
2100 sub save_clusterfw_conf {
2101 my ($cluster_conf) = @_;
2105 my $options = $cluster_conf->{options};
2106 if (scalar(keys %$options)) {
2107 $raw .= "[OPTIONS]\n\n";
2108 foreach my $opt (keys %$options) {
2109 $raw .= "$opt: $options->{$opt}\n";
2116 my $rules = $cluster_conf->{rules};
2117 if (scalar(@$rules)) {
2118 $raw .= "[RULES]\n\n";
2119 $raw .= &$rules_to_conf($rules, 1);
2123 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
2124 my $rules = $cluster_conf->{groups}->{$group};
2125 $raw .= "[group $group]\n\n";
2126 $raw .= &$rules_to_conf($rules, 0);
2130 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
2133 sub load_hostfw_conf {
2135 my $hostfw_conf = {};
2136 my $filename = "/etc/pve/local/host.fw";
2137 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2138 $hostfw_conf = parse_host_fw_rules($filename, $fh);
2140 return $hostfw_conf;
2144 my $vmdata = read_local_vm_config();
2145 my $vmfw_configs = read_vm_firewall_configs($vmdata);
2147 my $routing_table = read_proc_net_route();
2149 my $cluster_conf = load_clusterfw_conf();
2151 my $ipset_ruleset = {};
2152 generate_ipset_chains($ipset_ruleset, $cluster_conf);
2156 ruleset_create_chain($ruleset, "PVEFW-INPUT");
2157 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
2159 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
2161 my $hostfw_conf = load_hostfw_conf();
2162 my $hostfw_options = $hostfw_conf->{options} || {};
2164 generate_std_chains($ruleset, $hostfw_options);
2166 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
2168 enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf) if $hostfw_enable;
2170 # generate firewall rules for QEMU VMs
2171 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2172 my $conf = $vmdata->{qemu}->{$vmid};
2173 my $vmfw_conf = $vmfw_configs->{$vmid};
2174 next if !$vmfw_conf;
2175 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
2177 foreach my $netid (keys %$conf) {
2178 next if $netid !~ m/^net(\d+)$/;
2179 my $net = PVE::QemuServer::parse_net($conf->{$netid});
2181 my $iface = "tap${vmid}i$1";
2183 my $bridge = $net->{bridge};
2184 next if !$bridge; # fixme: ?
2186 $bridge .= "v$net->{tag}" if $net->{tag};
2188 generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table);
2190 my $macaddr = $net->{macaddr};
2191 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2192 $vmfw_conf, $vmid, $bridge, 'IN
');
2193 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2194 $vmfw_conf, $vmid, $bridge, 'OUT
');
2198 # generate firewall rules for OpenVZ containers
2199 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2200 my $conf = $vmdata->{openvz}->{$vmid};
2202 my $vmfw_conf = $vmfw_configs->{$vmid};
2203 next if !$vmfw_conf;
2204 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
2206 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
2207 my $ip = $conf->{ip_address}->{value};
2208 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'IN
');
2209 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'OUT
');
2212 if ($conf->{netif} && $conf->{netif}->{value}) {
2213 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
2214 foreach my $netid (keys %$netif) {
2215 my $d = $netif->{$netid};
2216 my $bridge = $d->{bridge};
2218 warn "no bridge device for CT $vmid iface '$netid'\n";
2222 generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table);
2224 my $macaddr = $d->{mac};
2225 my $iface = $d->{host_ifname};
2226 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2227 $vmfw_conf, $vmid, $bridge, 'IN
');
2228 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2229 $vmfw_conf, $vmid, $bridge, 'OUT
');
2234 if($hostfw_options->{optimize}){
2236 my $accept = ruleset_chain_exist($ruleset, "PVEFW-IPS") ? "PVEFW-IPS" : "ACCEPT";
2237 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept");
2238 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate INVALID -j DROP");
2241 # fixme: what log level should we use here?
2242 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
2244 # fixme: should we really block inter-bridge traffic?
2246 # always allow traffic from containers?
2247 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN");
2249 # disable interbridge routing
2250 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
2251 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
2252 ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+");
2253 ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+");
2254 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
2255 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
2257 return wantarray ? ($ruleset, $hostfw_conf, $ipset_ruleset) : $ruleset;
2260 sub get_ruleset_status {
2261 my ($ruleset, $verbose, $ipset) = @_;
2263 my $active_chains = undef;
2265 $active_chains = ipset_get_chains();
2267 $active_chains = iptables_get_chains();
2270 my $statushash = {};
2272 foreach my $chain (sort keys %$ruleset) {
2275 $sig = ipset_chain_digest($ruleset->{$chain});
2277 $sig = iptables_chain_digest($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 $statushash = get_ruleset_status($ruleset, $verbose);
2324 # create missing chains first
2325 foreach my $chain (sort keys %$ruleset) {
2326 my $stat = $statushash->{$chain};
2327 die "internal error" if !$stat;
2328 next if $stat->{action} ne 'create
';
2330 $cmdlist .= ":$chain - [0:0]\n";
2333 my $rule = "INPUT -j PVEFW-INPUT";
2334 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2335 $cmdlist .= "-A $rule\n";
2337 $rule = "OUTPUT -j PVEFW-OUTPUT";
2338 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2339 $cmdlist .= "-A $rule\n";
2342 $rule = "FORWARD -j PVEFW-FORWARD";
2343 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2344 $cmdlist .= "-A $rule\n";
2347 foreach my $chain (sort keys %$ruleset) {
2348 my $stat = $statushash->{$chain};
2349 die "internal error" if !$stat;
2351 if ($stat->{action} eq 'update
' || $stat->{action} eq 'create
') {
2352 $cmdlist .= "-F $chain\n";
2353 foreach my $cmd (@{$ruleset->{$chain}}) {
2354 $cmdlist .= "$cmd\n";
2356 $cmdlist .= print_sig_rule($chain, $stat->{sig});
2357 } elsif ($stat->{action} eq 'delete') {
2358 die "internal error"; # this should not happen
2359 } elsif ($stat->{action} eq 'exists') {
2362 die "internal error - unknown status '$stat->{action
}'";
2366 foreach my $chain (keys %$statushash) {
2367 next if $statushash->{$chain}->{action} ne 'delete';
2368 $cmdlist .= "-F $chain\n";
2370 foreach my $chain (keys %$statushash) {
2371 next if $statushash->{$chain}->{action} ne 'delete';
2372 next if $chain eq 'PVEFW-INPUT
';
2373 next if $chain eq 'PVEFW-OUTPUT
';
2374 next if $chain eq 'PVEFW-FORWARD
';
2375 $cmdlist .= "-X $chain\n";
2378 my $changes = $cmdlist ne "*filter\n" ? 1 : 0;
2380 $cmdlist .= "COMMIT\n";
2382 return wantarray ? ($cmdlist, $changes) : $cmdlist;
2385 sub get_ipset_cmdlist {
2386 my ($ruleset, $verbose) = @_;
2390 my $statushash = get_ruleset_status($ruleset, $verbose, 1);
2392 foreach my $chain (sort keys %$ruleset) {
2393 my $stat = $statushash->{$chain};
2394 die "internal error" if !$stat;
2396 if ($stat->{action} eq 'create
') {
2397 foreach my $cmd (@{$ruleset->{$chain}}) {
2398 $cmdlist .= "$cmd\n";
2402 if ($stat->{action} eq 'update
') {
2403 my $chain_swap = $chain."_swap";
2405 foreach my $cmd (@{$ruleset->{$chain}}) {
2406 $cmd =~ s/$chain/$chain_swap/;
2407 $cmdlist .= "$cmd\n";
2410 $cmdlist .= "swap $chain_swap $chain\n";
2411 $cmdlist .= "flush $chain_swap\n";
2412 $cmdlist .= "destroy $chain_swap\n";
2417 foreach my $chain (keys %$statushash) {
2418 next if $statushash->{$chain}->{action} ne 'delete';
2420 $cmdlist .= "flush $chain\n";
2421 $cmdlist .= "destroy $chain\n";
2424 my $changes = $cmdlist ? 1 : 0;
2426 return wantarray ? ($cmdlist, $changes) : $cmdlist;
2430 my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_;
2432 enable_bridge_firewall();
2434 update_nf_conntrack_max($hostfw_conf);
2436 my $ipsetcmdlist = get_ipset_cmdlist($ipset_ruleset, $verbose);
2438 my $cmdlist = get_ruleset_cmdlist($ruleset, $verbose);
2440 print $ipsetcmdlist if $verbose;
2442 print $cmdlist if $verbose;
2444 ipset_restore_cmdlist($ipsetcmdlist);
2446 iptables_restore_cmdlist($cmdlist);
2448 # test: re-read status and check if everything is up to date
2449 my $statushash = get_ruleset_status($ruleset);
2452 foreach my $chain (sort keys %$ruleset) {
2453 my $stat = $statushash->{$chain};
2454 if ($stat->{action} ne 'exists') {
2455 warn "unable to update chain '$chain'\n";
2460 die "unable to apply firewall changes\n" if $errors;
2463 sub update_nf_conntrack_max {
2464 my ($hostfw_conf) = @_;
2466 my $max = 65536; # reasonable default
2468 my $options = $hostfw_conf->{options} || {};
2470 if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) {
2471 $max = $options->{nf_conntrack_max};
2472 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
2475 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
2476 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
2478 my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max);
2480 if ($current != $max) {
2481 my $hashsize = int($max/4);
2482 PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize);
2483 PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max);
2487 sub remove_pvefw_chains {
2489 my ($chash, $hooks) = iptables_get_chains();
2490 my $cmdlist = "*filter\n";
2492 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2494 $cmdlist .= "-D $h -j PVEFW-$h\n";
2498 foreach my $chain (keys %$chash) {
2499 $cmdlist .= "-F $chain\n";
2502 foreach my $chain (keys %$chash) {
2503 $cmdlist .= "-X $chain\n";
2505 $cmdlist .= "COMMIT\n";
2507 iptables_restore_cmdlist
($cmdlist);
2511 my ($start, $verbose) = @_;
2514 my $status = read_pvefw_status
();
2516 my ($ruleset, $hostfw_conf, $ipset_ruleset) = compile
();
2518 if ($start || $status eq 'active') {
2520 save_pvefw_status
('active') if ($status ne 'active');
2522 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $verbose);
2524 print "Firewall not active (status = $status)\n" if $verbose;