9 use PVE
::JSONSchema
qw(get_standard_option);
17 use PVE
::Tools
qw(run_command lock_file);
20 my $hostfw_conf_filename = "/etc/pve/local/host.fw";
21 my $clusterfw_conf_filename = "/etc/pve/firewall/cluster.fw";
23 # dynamically include PVE::QemuServer and PVE::OpenVZ
24 # to avoid dependency problems
27 require PVE
::QemuServer
;
28 $have_qemu_server = 1;
34 $have_pve_manager = 1;
37 my $feature_ipset_nomatch = 0;
39 my (undef, undef, $release) = POSIX
::uname
();
40 if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) {
41 my ($major, $minor) = ($1, $2);
42 $feature_ipset_nomatch = 1 if ($major > 3) ||
43 ($major == 3 && $minor >= 7);
50 my $nodename = PVE
::INotify
::nodename
();
52 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
53 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
55 my $default_log_level = 'info';
57 my $log_level_hash = {
68 # imported/converted from: /usr/share/shorewall/macro.*
71 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
72 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
75 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
78 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
81 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
82 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
85 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
86 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
89 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
92 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
93 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
94 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
97 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
98 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
101 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
104 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
105 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
108 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
109 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
112 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
115 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
116 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
119 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
122 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
125 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
126 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
127 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
128 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
131 { action
=> 'PARAM', proto
=> '47' },
132 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
135 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
138 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
139 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
142 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
145 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
148 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
151 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
154 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
157 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
160 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
163 { action
=> 'PARAM', proto
=> '94' },
164 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
167 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
170 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
173 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
174 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
177 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
178 { action
=> 'PARAM', proto
=> '50' },
179 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
180 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
183 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
184 { action
=> 'PARAM', proto
=> '51' },
185 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
186 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
189 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
190 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
191 { action
=> 'PARAM', proto
=> '50' },
192 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
193 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
194 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
197 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
200 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
203 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
206 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
209 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
212 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
213 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
216 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
219 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
222 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
225 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
228 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
229 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
230 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
233 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
236 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
239 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
242 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
245 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
248 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
249 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
252 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
253 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
256 { action
=> 'PARAM', proto
=> '89' },
259 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
262 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
263 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
266 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
269 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
272 { action
=> 'PARAM', proto
=> '47' },
273 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
274 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
277 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
280 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
283 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
286 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
289 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
290 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
293 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
296 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
299 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
302 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
308 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
309 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
310 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
311 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
314 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
315 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
316 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
317 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
318 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
319 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
320 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
321 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
324 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
327 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
330 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
333 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
334 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
337 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
340 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
343 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
346 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
347 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
348 { action
=> 'PARAM', proto
=> '41' },
349 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
352 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
355 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
358 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
359 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
362 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
365 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
368 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
371 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
374 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
375 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
378 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
381 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
384 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
385 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
388 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
391 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
394 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
398 my $pve_fw_parsed_macros;
399 my $pve_fw_preferred_macro_names = {};
401 my $pve_std_chains = {
402 'PVEFW-SET-ACCEPT-MARK' => [
403 "-j MARK --set-mark 1",
405 'PVEFW-DropBroadcast' => [
406 # same as shorewall 'Broadcast'
407 # simply DROP BROADCAST/MULTICAST/ANYCAST
408 # we can use this to reduce logging
409 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
410 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
411 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
412 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
415 # same as shorewall 'reject'
416 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
417 { action
=> 'DROP', source
=> '224.0.0.0/4' },
418 { action
=> 'DROP', proto
=> 'icmp' },
419 "-p tcp -j REJECT --reject-with tcp-reset",
420 "-p udp -j REJECT --reject-with icmp-port-unreachable",
421 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
422 "-j REJECT --reject-with icmp-host-prohibited",
425 # same as shorewall 'Drop', which is equal to DROP,
426 # but REJECT/DROP some packages to reduce logging,
427 # and ACCEPT critical ICMP types
428 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
429 # we are not interested in BROADCAST/MULTICAST/ANYCAST
430 { action
=> 'PVEFW-DropBroadcast' },
431 # ACCEPT critical ICMP types
432 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
433 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
434 # Drop packets with INVALID state
435 "-m conntrack --ctstate INVALID -j DROP",
436 # Drop Microsoft SMB noise
437 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
438 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
439 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
440 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
441 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
442 # Drop new/NotSyn traffic so that it doesn't get logged
443 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
445 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
448 # same as shorewall 'Reject', which is equal to Reject,
449 # but REJECT/DROP some packages to reduce logging,
450 # and ACCEPT critical ICMP types
451 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
452 # we are not interested in BROADCAST/MULTICAST/ANYCAST
453 { action
=> 'PVEFW-DropBroadcast' },
454 # ACCEPT critical ICMP types
455 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
456 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
457 # Drop packets with INVALID state
458 "-m conntrack --ctstate INVALID -j DROP",
459 # Drop Microsoft SMB noise
460 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
461 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
462 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
463 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
464 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
465 # Drop new/NotSyn traffic so that it doesn't get logged
466 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
468 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
470 'PVEFW-tcpflags' => [
471 # same as shorewall tcpflags action.
472 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
473 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
474 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
475 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
476 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
477 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
480 # same as shorewall smurfs action
481 # Filter packets for smurfs (packets with a broadcast address as the source).
482 "-s 0.0.0.0/32 -j RETURN",
483 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
484 "-s 224.0.0.0/4 -g PVEFW-smurflog",
488 # iptables -p icmp -h
489 my $icmp_type_names = {
492 'destination-unreachable' => 1,
493 'network-unreachable' => 1,
494 'host-unreachable' => 1,
495 'protocol-unreachable' => 1,
496 'port-unreachable' => 1,
497 'fragmentation-needed' => 1,
498 'source-route-failed' => 1,
499 'network-unknown' => 1,
501 'network-prohibited' => 1,
502 'host-prohibited' => 1,
503 'TOS-network-unreachable' => 1,
504 'TOS-host-unreachable' => 1,
505 'communication-prohibited' => 1,
506 'host-precedence-violation' => 1,
507 'precedence-cutoff' => 1,
508 'source-quench' => 1,
510 'network-redirect' => 1,
511 'host-redirect' => 1,
512 'TOS-network-redirect' => 1,
513 'TOS-host-redirect' => 1,
515 'router-advertisement' => 1,
516 'router-solicitation' => 1,
517 'time-exceeded' => 1,
518 'ttl-zero-during-transit' => 1,
519 'ttl-zero-during-reassembly' => 1,
520 'parameter-problem' => 1,
521 'ip-header-bad' => 1,
522 'required-option-missing' => 1,
523 'timestamp-request' => 1,
524 'timestamp-reply' => 1,
525 'address-mask-request' => 1,
526 'address-mask-reply' => 1,
529 sub init_firewall_macros
{
531 $pve_fw_parsed_macros = {};
533 foreach my $k (keys %$pve_fw_macros) {
534 my $lc_name = lc($k);
535 my $macro = $pve_fw_macros->{$k};
536 $pve_fw_preferred_macro_names->{$lc_name} = $k;
537 $pve_fw_parsed_macros->{$k} = $macro;
541 init_firewall_macros
();
545 sub get_etc_services
{
547 return $etc_services if $etc_services;
549 my $filename = "/etc/services";
551 my $fh = IO
::File-
>new($filename, O_RDONLY
);
553 warn "unable to read '$filename' - $!\n";
559 while (my $line = <$fh>) {
561 next if $line =~m/^#/;
562 next if ($line =~m/^\s*$/);
564 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
565 $services->{byid
}->{$2}->{name
} = $1;
566 $services->{byid
}->{$2}->{port
} = $2;
567 $services->{byid
}->{$2}->{$3} = 1;
568 $services->{byname
}->{$1} = $services->{byid
}->{$2};
574 $etc_services = $services;
577 return $etc_services;
582 sub get_etc_protocols
{
583 return $etc_protocols if $etc_protocols;
585 my $filename = "/etc/protocols";
587 my $fh = IO
::File-
>new($filename, O_RDONLY
);
589 warn "unable to read '$filename' - $!\n";
595 while (my $line = <$fh>) {
597 next if $line =~m/^#/;
598 next if ($line =~m/^\s*$/);
600 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
601 $protocols->{byid
}->{$2}->{name
} = $1;
602 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
608 $etc_protocols = $protocols;
610 return $etc_protocols;
613 sub parse_address_list
{
616 return if $str !~ m/^(\+)(\S+)$/; # ipset ref
620 foreach my $elem (split(/,/, $str)) {
622 if (!Net
::IP-
>new($elem)) {
623 my $err = Net
::IP
::Error
();
624 die "invalid IP address: $err\n";
626 $iprange = 1 if $elem =~ m/-/;
629 die "you can use a range in a list\n" if $iprange && $count > 1;
632 sub parse_port_name_number_or_range
{
635 my $services = PVE
::Firewall
::get_etc_services
();
637 foreach my $item (split(/,/, $str)) {
641 foreach my $pon (split(':', $item, 2)) {
642 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
643 if ($pon =~ m/^\d+$/){
644 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
645 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
648 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
656 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
657 sub pve_fw_verify_port_spec
{
660 parse_port_name_number_or_range
($portstr);
665 PVE
::JSONSchema
::register_format
('pve-fw-v4addr-spec', \
&pve_fw_verify_v4addr_spec
);
666 sub pve_fw_verify_v4addr_spec
{
669 parse_address_list
($list);
674 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
675 sub pve_fw_verify_protocol_spec
{
678 my $protocols = get_etc_protocols
();
680 die "unknown protocol '$proto'\n" if $proto &&
681 !(defined($protocols->{byname
}->{$proto}) ||
682 defined($protocols->{byid
}->{$proto}));
688 # helper function for API
690 my $rule_properties = {
692 description
=> "Update rule at position <pos>.",
706 enum
=> ['in', 'out', 'group'],
711 enum
=> ['ACCEPT', 'DROP', 'REJECT'],
718 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
720 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
724 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
728 type
=> 'string', format
=> 'pve-fw-protocol-spec',
736 type
=> 'string', format
=> 'pve-fw-port-spec',
740 type
=> 'string', format
=> 'pve-fw-port-spec',
749 sub cleanup_fw_rule
{
750 my ($rule, $digest, $pos) = @_;
754 foreach my $k (keys %$rule) {
755 next if !$rule_properties->{$k};
757 next if !defined($v);
759 $r->{digest
} = $digest;
766 sub add_rule_properties
{
767 my ($properties) = @_;
769 foreach my $k (keys %$rule_properties) {
770 my $h = $rule_properties->{$k};
771 # copy data, so that we can modify later without side effects
772 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
778 sub delete_rule_properties
{
779 my ($rule, $delete_str) = @_;
781 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
782 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
783 if !defined($rule_properties->{$opt});
784 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
785 if $opt eq 'type' || $opt eq 'action';
786 delete $rule->{$opt};
793 my ($rule, $param) = @_;
795 foreach my $k (keys %$rule_properties) {
796 if (defined(my $v = $param->{$k})) {
797 if ($v eq '' || $v eq '-') {
810 my $bridge_firewall_enabled = 0;
812 sub enable_bridge_firewall
{
814 return if $bridge_firewall_enabled; # only once
816 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
817 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
819 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
820 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
822 $bridge_firewall_enabled = 1;
825 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
827 sub iptables_restore_cmdlist
{
830 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
833 sub ipset_restore_cmdlist
{
836 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist);
839 sub iptables_get_chains
{
843 # check what chains we want to track
844 my $is_pvefw_chain = sub {
847 return 1 if $name =~ m/^PVEFW-\S+$/;
849 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
851 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
853 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
855 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT|IPS)$/;
856 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
868 return if $line =~ m/^#/;
869 return if $line =~ m/^\s*$/;
871 if ($line =~ m/^\*(\S+)$/) {
876 return if $table ne 'filter';
878 if ($line =~ m/^:(\S+)\s/) {
880 return if !&$is_pvefw_chain($chain);
881 $res->{$chain} = "unknown";
882 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
883 my ($chain, $sig) = ($1, $2);
884 return if !&$is_pvefw_chain($chain);
885 $res->{$chain} = $sig;
886 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
889 # simply ignore the rest
894 run_command
("/sbin/iptables-save", outfunc
=> $parser);
896 return wantarray ?
($res, $hooks) : $res;
899 sub iptables_chain_digest
{
901 my $digest = Digest
::SHA-
>new('sha1');
902 foreach my $rule (@$rules) { # order is important
905 return $digest->b64digest;
908 sub ipset_chain_digest
{
911 my $digest = Digest
::SHA-
>new('sha1');
912 foreach my $rule (sort @$rules) { # note: sorted
915 return $digest->b64digest;
918 sub ipset_get_chains
{
926 return if $line =~ m/^#/;
927 return if $line =~ m/^\s*$/;
928 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
930 $line =~ s/\s+$//; # delete trailing white space
931 push @{$chains->{$chain}}, $line;
933 # simply ignore the rest
938 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
940 # compute digest for each chain
941 foreach my $chain (keys %$chains) {
942 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
948 sub ruleset_generate_cmdstr
{
949 my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf) = @_;
951 return if defined($rule->{enable
}) && !$rule->{enable
};
953 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
955 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
956 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
960 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
961 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
963 my $source = $rule->{source
};
964 my $dest = $rule->{dest
};
967 if ($source =~ m/^(\+)(\S+)$/) {
968 die "no such ipset $2" if !$cluster_conf->{ipset
}->{$2};
969 push @cmd, "-m set --match-set PVEFW-$2 src";
971 } elsif ($source =~ m/\-/){
972 push @cmd, "-m iprange --src-range $source";
975 push @cmd, "-s $source";
980 if ($dest =~ m/^(\+)(\S+)$/) {
981 die "no such ipset $2" if !$cluster_conf->{ipset
}->{$2};
982 push @cmd, "-m set --match-set PVEFW-$2 dst";
984 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
985 push @cmd, "-m iprange --dst-range $dest";
988 push @cmd, "-s $dest";
992 if ($rule->{proto
}) {
993 push @cmd, "-p $rule->{proto}";
996 $multiport++ if $nbdport > 1;
997 $multiport++ if $nbsport > 1;
999 push @cmd, "--match multiport" if $multiport;
1001 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1002 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1004 if ($rule->{dport
}) {
1005 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1006 # Note: we use dport to store --icmp-type
1007 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
1008 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1011 if ($multiport == 2) {
1012 push @cmd, "--ports $rule->{dport}";
1014 push @cmd, "--dports $rule->{dport}";
1017 push @cmd, "--dport $rule->{dport}";
1022 if ($rule->{sport
}) {
1024 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1026 push @cmd, "--sport $rule->{sport}";
1029 } elsif ($rule->{dport
} || $rule->{sport
}) {
1030 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
1031 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
1034 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1036 if (my $action = $rule->{action
}) {
1037 $action = $actions->{$action} if defined($actions->{$action});
1038 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1039 push @cmd, $goto ?
"-g $action" : "-j $action";
1042 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1045 my $apply_macro = sub {
1046 my ($macro_name, $param) = @_;
1048 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1049 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1053 foreach my $templ (@$macro_rules) {
1055 my $param_used = {};
1056 foreach my $k (keys %$templ) {
1057 my $v = $templ->{$k};
1058 if ($v eq 'PARAM') {
1060 $param_used->{$k} = 1;
1061 } elsif ($v eq 'DEST') {
1062 $v = $param->{dest
};
1063 $param_used->{dest
} = 1;
1064 } elsif ($v eq 'SOURCE') {
1065 $v = $param->{source
};
1066 $param_used->{source
} = 1;
1069 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1072 foreach my $k (keys %$param) {
1073 next if $k eq 'macro';
1074 next if !defined($param->{$k});
1075 next if $param_used->{$k};
1076 if (defined($rule->{$k})) {
1077 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1078 if $rule->{$k} ne $param->{$k};
1080 $rule->{$k} = $param->{$k};
1083 push @$rules, $rule;
1089 sub ruleset_generate_rule
{
1090 my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf) = @_;
1094 if ($rule->{macro}) {
1095 $rules = &$apply_macro($rule->{macro}, $rule);
1100 foreach my $tmp (@$rules) {
1101 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $tmp, $actions, $goto, $cluster_conf)) {
1102 ruleset_addrule
($ruleset, $chain, $cmdstr);
1107 sub ruleset_generate_rule_insert
{
1108 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1110 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1112 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
1113 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1117 sub ruleset_create_chain
{
1118 my ($ruleset, $chain) = @_;
1120 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1121 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1123 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1125 $ruleset->{$chain} = [];
1128 sub ruleset_chain_exist
{
1129 my ($ruleset, $chain) = @_;
1131 return $ruleset->{$chain} ?
1 : undef;
1134 sub ruleset_addrule
{
1135 my ($ruleset, $chain, $rule) = @_;
1137 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1139 push @{$ruleset->{$chain}}, "-A $chain $rule";
1142 sub ruleset_insertrule
{
1143 my ($ruleset, $chain, $rule) = @_;
1145 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1147 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1150 sub get_log_rule_base
{
1151 my ($chain, $vmid, $msg, $loglevel) = @_;
1153 die "internal error - no log level" if !defined($loglevel);
1155 $vmid = 0 if !defined($vmid);
1157 # Note: we use special format for prefix to pass further
1158 # info to log daemon (VMID, LOGVELEL and CHAIN)
1160 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1163 sub ruleset_addlog {
1164 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1166 return if !defined($loglevel);
1168 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1170 $logrule = "$rule $logrule" if defined($rule);
1172 ruleset_addrule($ruleset, $chain, $logrule)
1175 sub generate_bridge_chains {
1176 my ($ruleset, $hostfw_conf, $bridge, $routing_table) = @_;
1178 my $options = $hostfw_conf->{options} || {};
1180 die "error
: detected direct route to bridge
'$bridge'\n"
1181 if !$options->{allow_bridge_route} && $routing_table->{$bridge};
1183 if (!ruleset_chain_exist($ruleset, "$bridge-FW
")) {
1184 ruleset_create_chain($ruleset, "$bridge-FW
");
1185 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-FW
");
1186 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-FW
");
1189 if (!ruleset_chain_exist($ruleset, "$bridge-OUT
")) {
1190 ruleset_create_chain($ruleset, "$bridge-OUT
");
1191 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-in
-j
$bridge-OUT
");
1192 ruleset_insertrule($ruleset, "PVEFW-INPUT
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-OUT
");
1195 if (!ruleset_chain_exist($ruleset, "$bridge-IN
")) {
1196 ruleset_create_chain($ruleset, "$bridge-IN
");
1197 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j
$bridge-IN
");
1198 ruleset_addrule($ruleset, "$bridge-FW
", "-m mark
--mark
1 -j ACCEPT
");
1199 # accept traffic to unmanaged bridge ports
1200 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j ACCEPT
");
1204 sub ruleset_add_chain_policy {
1205 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
1207 if ($policy eq 'ACCEPT') {
1209 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
1210 { ACCEPT => $accept_action});
1212 } elsif ($policy eq 'DROP') {
1214 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1216 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1218 ruleset_addrule($ruleset, $chain, "-j DROP
");
1219 } elsif ($policy eq 'REJECT') {
1220 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1222 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1224 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1227 die "internal error
: unknown policy
'$policy'";
1231 sub ruleset_create_vm_chain {
1232 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
1234 ruleset_create_chain($ruleset, $chain);
1235 my $accept = generate_nfqueue($options);
1237 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1238 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1241 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1242 if ($direction eq 'OUT') {
1243 ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK',
1244 proto => 'udp', sport => 68, dport => 67 });
1246 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT',
1247 proto => 'udp', sport => 67, dport => 68 });
1251 if ($options->{tcpflags}) {
1252 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1255 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1256 if ($direction eq 'OUT') {
1257 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-g PVEFW-SET-ACCEPT-MARK
");
1259 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1262 if ($direction eq 'OUT') {
1263 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1264 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1266 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1270 sub ruleset_generate_vm_rules {
1271 my ($ruleset, $rules, $cluster_conf, $chain, $netid, $direction, $options) = @_;
1273 my $lc_direction = lc($direction);
1275 foreach my $rule (@$rules) {
1276 next if $rule->{iface} && $rule->{iface} ne $netid;
1277 next if !$rule->{enable};
1278 if ($rule->{type} eq 'group') {
1279 my $group_chain = "GROUP-
$rule->{action
}-$direction";
1280 if(!ruleset_chain_exist($ruleset, $group_chain)){
1281 generate_group_rules($ruleset, $cluster_conf, $rule->{action});
1283 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1284 if ($direction eq 'OUT'){
1285 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j RETURN
");
1287 my $accept = generate_nfqueue($options);
1288 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$accept");
1292 next if $rule->{type} ne $lc_direction;
1293 if ($direction eq 'OUT') {
1294 ruleset_generate_rule($ruleset, $chain, $rule,
1295 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1297 my $accept = generate_nfqueue($options);
1298 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1304 sub generate_nfqueue {
1308 if($options->{ips}){
1309 $action = "NFQUEUE
";
1310 if($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1311 if(defined($3) && defined($1)) {
1312 $action .= " --queue-balance
$1:$3";
1313 }elsif (defined($1)) {
1314 $action .= " --queue-num
$1";
1317 $action .= " --queue-bypass
";
1325 sub ruleset_generate_vm_ipsrules {
1326 my ($ruleset, $options, $direction, $iface, $bridge) = @_;
1328 if ($options->{ips} && $direction eq 'IN') {
1329 my $nfqueue = generate_nfqueue($options);
1331 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1332 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1335 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1336 ruleset_create_chain($ruleset, "$bridge-IPS
");
1337 ruleset_insertrule($ruleset, "PVEFW-IPS
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-IPS
");
1340 ruleset_addrule($ruleset, "$bridge-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1344 sub generate_venet_rules_direction {
1345 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
1347 parse_address_list($ip); # make sure we have a valid $ip list
1349 my $lc_direction = lc($direction);
1351 my $rules = $vmfw_conf->{rules};
1353 my $options = $vmfw_conf->{options};
1354 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1356 my $chain = "venet0-
$vmid-$direction";
1358 ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction);
1360 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $chain, 'venet', $direction);
1365 if ($direction eq 'OUT') {
1366 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1368 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1371 my $accept = generate_nfqueue($options);
1372 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1373 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1375 # plug into FORWARD, INPUT and OUTPUT chain
1376 if ($direction eq 'OUT') {
1377 ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD
", {
1380 iface_in => 'venet0'});
1382 ruleset_generate_rule_insert($ruleset, "PVEFW-INPUT
", {
1385 iface_in => 'venet0'});
1387 ruleset_generate_rule($ruleset, "PVEFW-FORWARD
", {
1390 iface_out => 'venet0'});
1392 ruleset_generate_rule($ruleset, "PVEFW-OUTPUT
", {
1395 iface_out => 'venet0'});
1399 sub generate_tap_rules_direction {
1400 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_;
1402 my $lc_direction = lc($direction);
1404 my $rules = $vmfw_conf->{rules};
1406 my $options = $vmfw_conf->{options};
1407 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1409 my $tapchain = "$iface-$direction";
1411 ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction);
1413 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $tapchain, $netid, $direction, $options);
1415 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface, $bridge);
1420 if ($direction eq 'OUT') {
1421 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1423 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1426 my $accept = generate_nfqueue($options);
1427 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1428 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
1430 # plug the tap chain to bridge chain
1431 if ($direction eq 'IN') {
1432 ruleset_insertrule($ruleset, "$bridge-IN
",
1433 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
1435 ruleset_insertrule($ruleset, "$bridge-OUT
",
1436 "-m physdev
--physdev-in
$iface -j
$tapchain");
1440 sub enable_host_firewall {
1441 my ($ruleset, $hostfw_conf, $cluster_conf) = @_;
1443 # fixme: allow security groups
1445 my $options = $hostfw_conf->{options};
1446 my $rules = $hostfw_conf->{rules};
1448 # host inbound firewall
1449 my $chain = "PVEFW-HOST-IN
";
1450 ruleset_create_chain($ruleset, $chain);
1452 my $loglevel = get_option_log_level($options, "log_level_in
");
1454 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1455 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1458 if ($options->{tcpflags}) {
1459 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1462 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1463 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1464 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
1465 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1466 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1467 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1469 # we use RETURN because we need to check also tap rules
1470 my $accept_action = 'RETURN';
1472 foreach my $rule (@$rules) {
1473 next if $rule->{type} ne 'in';
1474 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1477 # implement input policy
1478 my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1479 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1481 # host outbound firewall
1482 $chain = "PVEFW-HOST-OUT
";
1483 ruleset_create_chain($ruleset, $chain);
1485 $loglevel = get_option_log_level($options, "log_level_out
");
1487 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1488 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1489 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
1490 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1491 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1492 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1494 # we use RETURN because we may want to check other thigs later
1495 $accept_action = 'RETURN';
1497 foreach my $rule (@$rules) {
1498 next if $rule->{type} ne 'out';
1499 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1502 # implement output policy
1503 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1504 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1506 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
1507 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
1510 sub generate_group_rules {
1511 my ($ruleset, $cluster_conf, $group) = @_;
1512 die "no such security group
'$group'\n" if !$cluster_conf->{groups}->{$group};
1514 my $rules = $cluster_conf->{groups}->{$group};
1516 my $chain = "GROUP-
${group
}-IN
";
1518 ruleset_create_chain($ruleset, $chain);
1519 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1521 foreach my $rule (@$rules) {
1522 next if $rule->{type} ne 'in';
1523 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1526 $chain = "GROUP-
${group
}-OUT
";
1528 ruleset_create_chain($ruleset, $chain);
1529 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1531 foreach my $rule (@$rules) {
1532 next if $rule->{type} ne 'out';
1533 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1534 # check also other tap rules later
1535 ruleset_generate_rule($ruleset, $chain, $rule,
1536 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1541 my $valid_netdev_names = {};
1542 for (my $i = 0; $i < $MAX_NETS; $i++) {
1543 $valid_netdev_names->{"net
$i"} = 1;
1547 my ($line, $need_iface, $allow_groups) = @_;
1549 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1551 # we can add single line comments to the end of the rule
1552 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
1554 # we can disable a rule when prefixed with '|'
1557 $enable = 0 if $line =~ s/^\|//;
1559 my @data = split(/\s+/, $line);
1560 my $expected_elements = $need_iface ? 8 : 7;
1562 die "wrong number of rule elements
\n" if scalar(@data) > $expected_elements;
1565 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1567 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1570 die "incomplete rule
\n" if ! ($type && $action);
1576 if ($type eq 'in' || $type eq 'out') {
1577 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1579 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1581 my $preferred_name = $pve_fw_preferred_macro_names->{lc($1)};
1582 die "unknown
macro '$1'\n" if !$preferred_name;
1583 $macro = $preferred_name;
1585 die "unknown action
'$action'\n";
1587 } elsif ($type eq 'group') {
1588 die "wrong number of rule elements
\n" if scalar(@data) != 3;
1589 die "groups disabled
\n" if !$allow_groups;
1591 die "invalid characters
in group name
\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1593 die "unknown rule type
'$type'\n";
1597 $iface = undef if $iface && $iface eq '-';
1600 $proto = undef if $proto && $proto eq '-';
1601 pve_fw_verify_protocol_spec($proto) if $proto;
1603 $source = undef if $source && $source eq '-';
1604 $dest = undef if $dest && $dest eq '-';
1606 $dport = undef if $dport && $dport eq '-';
1607 $sport = undef if $sport && $sport eq '-';
1609 parse_port_name_number_or_range($dport) if defined($dport);
1610 parse_port_name_number_or_range($sport) if defined($sport);
1612 parse_address_list($source) if $source;
1613 parse_address_list($dest) if $dest;
1618 comment => $comment,
1630 sub parse_vmfw_option {
1635 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
1637 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags|ips):\s*(0|1)\s*$/i) {
1640 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1642 $value = $2 ? lc($3) : '';
1643 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1646 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
1651 die "can
't parse option '$line'\n"
1654 return ($opt, $value);
1657 sub parse_hostfw_option {
1662 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1664 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags|allow_bridge_route|optimize):\s*(0|1)\s*$/i) {
1667 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1669 $value = $2 ? lc($3) : '';
1670 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1673 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1678 die "can't parse option
'$line'\n"
1681 return ($opt, $value);
1684 sub parse_clusterfw_option {
1689 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
1694 die "can
't parse option '$line'\n"
1697 return ($opt, $value);
1700 sub parse_vm_fw_rules {
1701 my ($filename, $fh) = @_;
1703 my $res = { rules => [], options => {}};
1707 my $digest = Digest::SHA->new('sha1
');
1709 while (defined(my $line = <$fh>)) {
1710 $digest->add($line);
1712 next if $line =~ m/^#/;
1713 next if $line =~ m/^\s*$/;
1715 my $linenr = $fh->input_line_number();
1716 my $prefix = "$filename (line $linenr)";
1718 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1720 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1724 warn "$prefix: skip line - no section";
1728 next if !$res->{$section}; # skip undefined section
1730 if ($section eq 'options
') {
1732 my ($opt, $value) = parse_vmfw_option($line);
1733 $res->{options}->{$opt} = $value;
1735 warn "$prefix: $@" if $@;
1740 eval { $rule = parse_fw_rule($line, 1, 1); };
1742 warn "$prefix: $err";
1746 push @{$res->{$section}}, $rule;
1749 $res->{digest} = $digest->b64digest;
1754 sub parse_host_fw_rules {
1755 my ($filename, $fh) = @_;
1757 my $res = { rules => [], options => {}};
1761 my $digest = Digest::SHA->new('sha1
');
1763 while (defined(my $line = <$fh>)) {
1764 $digest->add($line);
1766 next if $line =~ m/^#/;
1767 next if $line =~ m/^\s*$/;
1769 my $linenr = $fh->input_line_number();
1770 my $prefix = "$filename (line $linenr)";
1772 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1774 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1778 warn "$prefix: skip line - no section";
1782 next if !$res->{$section}; # skip undefined section
1784 if ($section eq 'options
') {
1786 my ($opt, $value) = parse_hostfw_option($line);
1787 $res->{options}->{$opt} = $value;
1789 warn "$prefix: $@" if $@;
1794 eval { $rule = parse_fw_rule($line, 1, 1); };
1796 warn "$prefix: $err";
1800 push @{$res->{$section}}, $rule;
1803 $res->{digest} = $digest->b64digest;
1808 sub parse_cluster_fw_rules {
1809 my ($filename, $fh) = @_;
1814 my $res = { rules => [], options => {}, groups => {}, ipset => {} };
1816 my $digest = Digest::SHA->new('sha1
');
1818 while (defined(my $line = <$fh>)) {
1819 $digest->add($line);
1821 next if $line =~ m/^#/;
1822 next if $line =~ m/^\s*$/;
1824 my $linenr = $fh->input_line_number();
1825 my $prefix = "$filename (line $linenr)";
1827 if ($line =~ m/^\[options\]$/i) {
1828 $section = 'options
';
1832 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1833 $section = 'groups
';
1838 if ($line =~ m/^\[rules\]$/i) {
1843 if ($line =~ m/^\[ipset\s+(\S+)\]\s*$/i) {
1850 warn "$prefix: skip line - no section\n";
1854 if ($section eq 'options
') {
1856 my ($opt, $value) = parse_clusterfw_option($line);
1857 $res->{options}->{$opt} = $value;
1859 warn "$prefix: $@" if $@;
1860 } elsif ($section eq 'rules
') {
1862 eval { $rule = parse_fw_rule($line, 1, 1); };
1864 warn "$prefix: $err";
1867 push @{$res->{$section}}, $rule;
1868 } elsif ($section eq 'groups
') {
1870 eval { $rule = parse_fw_rule($line, 0, 0); };
1872 warn "$prefix: $err";
1875 push @{$res->{$section}->{$group}}, $rule;
1876 } elsif ($section eq 'ipset
') {
1878 $line =~ m/^(\!)?\s*((\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?)/;
1883 warn "$prefix: $line is not an valid ip address\n";
1886 if (!Net::IP->new($ip)) {
1887 warn "$prefix: $line is not an valid ip address\n";
1892 if ($feature_ipset_nomatch) {
1893 push @{$res->{$section}->{$group}}, "$ip nomatch";
1895 warn "$prefix: ignore $line - nomatch not supported by kernel\n";
1898 push @{$res->{$section}->{$group}}, $ip;
1903 $res->{digest} = $digest->b64digest;
1908 my ($code, @param) = @_;
1912 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1919 sub read_local_vm_config {
1924 my $vmdata = { openvz => $openvz, qemu => $qemu };
1926 my $vmlist = PVE::Cluster::get_vmlist();
1927 return $vmdata if !$vmlist || !$vmlist->{ids};
1928 my $ids = $vmlist->{ids};
1930 foreach my $vmid (keys %$ids) {
1931 next if !$vmid; # skip VE0
1932 my $d = $ids->{$vmid};
1933 next if !$d->{node} || $d->{node} ne $nodename;
1934 next if !$d->{type};
1935 if ($d->{type} eq 'openvz
') {
1936 if ($have_pve_manager) {
1937 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
1938 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1939 $openvz->{$vmid} = $conf;
1942 } elsif ($d->{type} eq 'qemu
') {
1943 if ($have_qemu_server) {
1944 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1945 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1946 $qemu->{$vmid} = $conf;
1955 sub load_vmfw_conf {
1960 my $filename = "/etc/pve/firewall/$vmid.fw";
1961 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1962 $vmfw_conf = parse_vm_fw_rules($filename, $fh);
1968 my $format_rules = sub {
1969 my ($rules, $need_iface) = @_;
1973 foreach my $rule (@$rules) {
1974 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
') {
1975 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
1976 $raw .= uc($rule->{type});
1977 $raw .= " " . $rule->{action};
1978 $raw .= " " . ($rule->{iface} || '-') if $need_iface;
1979 $raw .= " " . ($rule->{source} || '-');
1980 $raw .= " " . ($rule->{dest} || '-');
1981 $raw .= " " . ($rule->{proto} || '-');
1982 $raw .= " " . ($rule->{dport} || '-');
1983 $raw .= " " . ($rule->{sport} || '-');
1984 $raw .= " # " . encode('utf8
', $rule->{comment})
1985 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
1988 die "implement me '$rule->{type
}'";
1995 my $format_options = sub {
2000 $raw .= "[OPTIONS]\n\n";
2001 foreach my $opt (keys %$options) {
2002 $raw .= "$opt: $options->{$opt}\n";
2009 sub save_vmfw_conf {
2010 my ($vmid, $vmfw_conf) = @_;
2014 my $options = $vmfw_conf->{options};
2015 $raw .= &$format_options($options) if scalar(keys %$options);
2017 my $rules = $vmfw_conf->{rules};
2018 if (scalar(@$rules)) {
2019 $raw .= "[RULES]\n\n";
2020 $raw .= &$format_rules($rules, 1);
2024 my $filename = "/etc/pve/firewall/$vmid.fw";
2025 PVE::Tools::file_set_contents($filename, $raw);
2028 sub read_vm_firewall_configs {
2030 my $vmfw_configs = {};
2032 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
2033 my $vmfw_conf = load_vmfw_conf($vmid);
2034 next if !$vmfw_conf->{options}; # skip if file does not exists
2035 $vmfw_configs->{$vmid} = $vmfw_conf;
2038 return $vmfw_configs;
2041 sub get_option_log_level {
2042 my ($options, $k) = @_;
2044 my $v = $options->{$k};
2045 $v = $default_log_level if !defined($v);
2047 return undef if $v eq '' || $v eq 'nolog
';
2049 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2051 return $v if ($v >= 0) && ($v <= 7);
2053 warn "unknown log level ($k = '$v')\n";
2058 sub generate_std_chains {
2059 my ($ruleset, $options) = @_;
2061 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2063 # same as shorewall smurflog.
2064 my $chain = 'PVEFW-smurflog
';
2066 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2067 push @{$pve_std_chains->{$chain}}, "-j DROP";
2069 # same as shorewall logflags action.
2070 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2071 $chain = 'PVEFW-logflags
';
2072 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2073 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2074 push @{$pve_std_chains->{$chain}}, "-j DROP";
2076 foreach my $chain (keys %$pve_std_chains) {
2077 ruleset_create_chain($ruleset, $chain);
2078 foreach my $rule (@{$pve_std_chains->{$chain}}) {
2080 ruleset_generate_rule($ruleset, $chain, $rule);
2082 ruleset_addrule($ruleset, $chain, $rule);
2088 sub generate_ipset_chains {
2089 my ($ipset_ruleset, $fw_conf) = @_;
2091 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2092 generate_ipset($ipset_ruleset, "PVEFW-$ipset", $fw_conf->{ipset}->{$ipset});
2096 sub generate_ipset {
2097 my ($ipset_ruleset, $name, $options) = @_;
2099 my $hashsize = scalar(@$options);
2100 if ($hashsize <= 64) {
2103 $hashsize = round_powerof2($hashsize);
2106 push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize";
2108 foreach my $ip (@$options) {
2109 push @{$ipset_ruleset->{$name}}, "add $name $ip";
2113 sub round_powerof2 {
2117 $int |= $int >> $_ foreach (1,2,4,8,16);
2121 sub save_pvefw_status {
2124 die "unknown status '$status' - internal error"
2125 if $status !~ m/^(stopped|active)$/;
2127 mkdir dirname($pve_fw_status_filename);
2128 PVE::Tools::file_set_contents($pve_fw_status_filename, $status);
2131 sub read_pvefw_status {
2133 my $status = 'unknown
';
2135 return 'stopped
' if ! -f $pve_fw_status_filename;
2138 $status = PVE::Tools::file_get_contents($pve_fw_status_filename);
2145 # fixme: move to pve-common PVE::ProcFSTools
2146 sub read_proc_net_route {
2147 my $filename = "/proc/net/route";
2151 my $fh = IO::File->new ($filename, "r");
2152 return $res if !$fh;
2154 my $int_to_quad = sub {
2155 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
2158 while (defined(my $line = <$fh>)) {
2159 next if $line =~/^Iface\s+Destination/; # skip head
2160 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
2161 push @{$res->{$iface}}, {
2162 dest => &$int_to_quad(hex($dest)),
2163 gateway => &$int_to_quad(hex($gateway)),
2164 mask => &$int_to_quad(hex($mask)),
2173 sub load_clusterfw_conf {
2175 my $cluster_conf = {};
2176 if (my $fh = IO::File->new($clusterfw_conf_filename, O_RDONLY)) {
2177 $cluster_conf = parse_cluster_fw_rules($clusterfw_conf_filename, $fh);
2180 return $cluster_conf;
2183 sub save_clusterfw_conf {
2184 my ($cluster_conf) = @_;
2188 my $options = $cluster_conf->{options};
2189 $raw .= &$format_options($options) if scalar(keys %$options);
2193 my $rules = $cluster_conf->{rules};
2194 if (scalar(@$rules)) {
2195 $raw .= "[RULES]\n\n";
2196 $raw .= &$format_rules($rules, 1);
2200 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
2201 my $rules = $cluster_conf->{groups}->{$group};
2202 $raw .= "[group $group]\n\n";
2203 $raw .= &$format_rules($rules, 0);
2207 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
2210 sub load_hostfw_conf {
2212 my $hostfw_conf = {};
2213 if (my $fh = IO::File->new($hostfw_conf_filename, O_RDONLY)) {
2214 $hostfw_conf = parse_host_fw_rules($hostfw_conf_filename, $fh);
2216 return $hostfw_conf;
2219 sub save_hostfw_conf {
2220 my ($hostfw_conf) = @_;
2224 my $options = $hostfw_conf->{options};
2225 $raw .= &$format_options($options) if scalar(keys %$options);
2227 my $rules = $hostfw_conf->{rules};
2228 if (scalar(@$rules)) {
2229 $raw .= "[RULES]\n\n";
2230 $raw .= &$format_rules($rules, 1);
2234 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
2238 my $vmdata = read_local_vm_config();
2239 my $vmfw_configs = read_vm_firewall_configs($vmdata);
2241 my $routing_table = read_proc_net_route();
2243 my $cluster_conf = load_clusterfw_conf();
2245 my $ipset_ruleset = {};
2246 generate_ipset_chains($ipset_ruleset, $cluster_conf);
2250 ruleset_create_chain($ruleset, "PVEFW-INPUT");
2251 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
2253 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
2255 my $hostfw_conf = load_hostfw_conf();
2256 my $hostfw_options = $hostfw_conf->{options} || {};
2258 generate_std_chains($ruleset, $hostfw_options);
2260 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
2262 enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf) if $hostfw_enable;
2264 # generate firewall rules for QEMU VMs
2265 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2266 my $conf = $vmdata->{qemu}->{$vmid};
2267 my $vmfw_conf = $vmfw_configs->{$vmid};
2268 next if !$vmfw_conf;
2269 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
2271 foreach my $netid (keys %$conf) {
2272 next if $netid !~ m/^net(\d+)$/;
2273 my $net = PVE::QemuServer::parse_net($conf->{$netid});
2275 my $iface = "tap${vmid}i$1";
2277 my $bridge = $net->{bridge};
2278 next if !$bridge; # fixme: ?
2280 $bridge .= "v$net->{tag}" if $net->{tag};
2282 generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table);
2284 my $macaddr = $net->{macaddr};
2285 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2286 $vmfw_conf, $vmid, $bridge, 'IN
');
2287 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2288 $vmfw_conf, $vmid, $bridge, 'OUT
');
2292 # generate firewall rules for OpenVZ containers
2293 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2294 my $conf = $vmdata->{openvz}->{$vmid};
2296 my $vmfw_conf = $vmfw_configs->{$vmid};
2297 next if !$vmfw_conf;
2298 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
2300 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
2301 my $ip = $conf->{ip_address}->{value};
2302 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'IN
');
2303 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'OUT
');
2306 if ($conf->{netif} && $conf->{netif}->{value}) {
2307 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
2308 foreach my $netid (keys %$netif) {
2309 my $d = $netif->{$netid};
2310 my $bridge = $d->{bridge};
2312 warn "no bridge device for CT $vmid iface '$netid'\n";
2316 generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table);
2318 my $macaddr = $d->{mac};
2319 my $iface = $d->{host_ifname};
2320 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2321 $vmfw_conf, $vmid, $bridge, 'IN
');
2322 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
2323 $vmfw_conf, $vmid, $bridge, 'OUT
');
2328 if($hostfw_options->{optimize}){
2330 my $accept = ruleset_chain_exist($ruleset, "PVEFW-IPS") ? "PVEFW-IPS" : "ACCEPT";
2331 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept");
2332 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate INVALID -j DROP");
2335 # fixme: what log level should we use here?
2336 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
2338 # fixme: should we really block inter-bridge traffic?
2340 # always allow traffic from containers?
2341 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN");
2343 # disable interbridge routing
2344 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
2345 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
2346 ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+");
2347 ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+");
2348 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
2349 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
2351 return wantarray ? ($ruleset, $hostfw_conf, $ipset_ruleset) : $ruleset;
2354 sub get_ruleset_status {
2355 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
2357 my $statushash = {};
2359 foreach my $chain (sort keys %$ruleset) {
2360 my $sig = &$digest_fn($ruleset->{$chain});
2362 $statushash->{$chain}->{sig} = $sig;
2364 my $oldsig = $active_chains->{$chain};
2365 if (!defined($oldsig)) {
2366 $statushash->{$chain}->{action} = 'create
';
2368 if ($oldsig eq $sig) {
2369 $statushash->{$chain}->{action} = 'exists';
2371 $statushash->{$chain}->{action} = 'update
';
2374 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
2375 foreach my $cmd (@{$ruleset->{$chain}}) {
2376 print "\t$cmd\n" if $verbose;
2380 foreach my $chain (sort keys %$active_chains) {
2381 if (!defined($ruleset->{$chain})) {
2382 my $sig = $active_chains->{$chain};
2383 $statushash->{$chain}->{action} = 'delete';
2384 $statushash->{$chain}->{sig} = $sig;
2385 print "delete $chain ($sig)\n" if $verbose;
2392 sub print_sig_rule {
2393 my ($chain, $sig) = @_;
2395 # We just use this to store a SHA1 checksum used to detect changes
2396 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
2399 sub get_ruleset_cmdlist {
2400 my ($ruleset, $verbose) = @_;
2402 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
2404 my ($active_chains, $hooks) = iptables_get_chains();
2405 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
2407 # create missing chains first
2408 foreach my $chain (sort keys %$ruleset) {
2409 my $stat = $statushash->{$chain};
2410 die "internal error" if !$stat;
2411 next if $stat->{action} ne 'create
';
2413 $cmdlist .= ":$chain - [0:0]\n";
2416 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2417 if (!$hooks->{$h}) {
2418 $cmdlist .= "-A $h -j PVEFW-$h\n";
2422 foreach my $chain (sort keys %$ruleset) {
2423 my $stat = $statushash->{$chain};
2424 die "internal error" if !$stat;
2426 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
2427 $cmdlist .= "-F $chain\n";
2428 foreach my $cmd (@{$ruleset->{$chain}}) {
2429 $cmdlist .= "$cmd\n";
2431 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
2432 } elsif ($stat->{action
} eq 'delete') {
2433 die "internal error"; # this should not happen
2434 } elsif ($stat->{action
} eq 'exists') {
2437 die "internal error - unknown status '$stat->{action}'";
2441 foreach my $chain (keys %$statushash) {
2442 next if $statushash->{$chain}->{action
} ne 'delete';
2443 $cmdlist .= "-F $chain\n";
2445 foreach my $chain (keys %$statushash) {
2446 next if $statushash->{$chain}->{action
} ne 'delete';
2447 next if $chain eq 'PVEFW-INPUT';
2448 next if $chain eq 'PVEFW-OUTPUT';
2449 next if $chain eq 'PVEFW-FORWARD';
2450 $cmdlist .= "-X $chain\n";
2453 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
2455 $cmdlist .= "COMMIT\n";
2457 return wantarray ?
($cmdlist, $changes) : $cmdlist;
2460 sub get_ipset_cmdlist
{
2461 my ($ruleset, $verbose) = @_;
2465 my $delete_cmdlist = "";
2467 my $active_chains = ipset_get_chains
();
2468 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
2470 foreach my $chain (sort keys %$ruleset) {
2471 my $stat = $statushash->{$chain};
2472 die "internal error" if !$stat;
2474 if ($stat->{action
} eq 'create') {
2475 foreach my $cmd (@{$ruleset->{$chain}}) {
2476 $cmdlist .= "$cmd\n";
2480 if ($stat->{action
} eq 'update') {
2481 my $chain_swap = $chain."_swap";
2483 foreach my $cmd (@{$ruleset->{$chain}}) {
2484 $cmd =~ s/$chain/$chain_swap/;
2485 $cmdlist .= "$cmd\n";
2487 $cmdlist .= "swap $chain_swap $chain\n";
2488 $cmdlist .= "flush $chain_swap\n";
2489 $cmdlist .= "destroy $chain_swap\n";
2494 foreach my $chain (keys %$statushash) {
2495 next if $statushash->{$chain}->{action
} ne 'delete';
2497 $delete_cmdlist .= "flush $chain\n";
2498 $delete_cmdlist .= "destroy $chain\n";
2501 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
2503 return ($cmdlist, $delete_cmdlist, $changes);
2507 my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_;
2509 enable_bridge_firewall
();
2511 update_nf_conntrack_max
($hostfw_conf);
2513 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
2514 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
2516 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
2519 if ($ipset_changes) {
2520 print "ipset changes:\n";
2521 print $ipset_create_cmdlist if $ipset_create_cmdlist;
2522 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
2526 print "iptables changes:\n";
2531 ipset_restore_cmdlist
($ipset_create_cmdlist);
2533 iptables_restore_cmdlist
($cmdlist);
2535 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
2537 # test: re-read status and check if everything is up to date
2538 my $active_chains = iptables_get_chains
();
2539 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
2542 foreach my $chain (sort keys %$ruleset) {
2543 my $stat = $statushash->{$chain};
2544 if ($stat->{action
} ne 'exists') {
2545 warn "unable to update chain '$chain'\n";
2550 die "unable to apply firewall changes\n" if $errors;
2553 sub update_nf_conntrack_max
{
2554 my ($hostfw_conf) = @_;
2556 my $max = 65536; # reasonable default
2558 my $options = $hostfw_conf->{options
} || {};
2560 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
2561 $max = $options->{nf_conntrack_max
};
2562 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
2565 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
2566 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
2568 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
2570 if ($current != $max) {
2571 my $hashsize = int($max/4);
2572 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
2573 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
2577 sub remove_pvefw_chains
{
2579 my ($chash, $hooks) = iptables_get_chains
();
2580 my $cmdlist = "*filter\n";
2582 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2584 $cmdlist .= "-D $h -j PVEFW-$h\n";
2588 foreach my $chain (keys %$chash) {
2589 $cmdlist .= "-F $chain\n";
2592 foreach my $chain (keys %$chash) {
2593 $cmdlist .= "-X $chain\n";
2595 $cmdlist .= "COMMIT\n";
2597 iptables_restore_cmdlist
($cmdlist);
2601 my ($start, $verbose) = @_;
2604 my $status = read_pvefw_status
();
2606 my ($ruleset, $hostfw_conf, $ipset_ruleset) = compile
();
2608 if ($start || $status eq 'active') {
2610 save_pvefw_status
('active') if ($status ne 'active');
2612 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $verbose);
2614 print "Firewall not active (status = $status)\n" if $verbose;