15 use PVE
::Tools
qw(run_command lock_file);
18 # dynamically include PVE::QemuServer and PVE::OpenVZ
19 # to avoid dependency problems
22 require PVE
::QemuServer
;
23 $have_qemu_server = 1;
29 $have_pve_manager = 1;
34 my $nodename = PVE
::INotify
::nodename
();
36 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
37 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
39 my $default_log_level = 'info';
41 my $log_level_hash = {
52 # imported/converted from: /usr/share/shorewall/macro.*
55 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
56 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
59 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
62 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
65 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
66 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
69 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
70 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
73 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
76 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
77 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
78 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
81 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
82 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
85 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
88 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
89 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
92 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
93 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
96 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
99 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
100 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
103 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
106 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
109 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
110 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
111 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
112 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
115 { action
=> 'PARAM', proto
=> '47' },
116 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
119 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
122 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
123 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
126 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
129 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
132 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
135 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
138 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
141 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
144 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
147 { action
=> 'PARAM', proto
=> '94' },
148 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
151 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
154 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
157 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
158 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
161 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
162 { action
=> 'PARAM', proto
=> '50' },
163 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
164 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
167 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
168 { action
=> 'PARAM', proto
=> '51' },
169 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
170 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
173 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
174 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
175 { action
=> 'PARAM', proto
=> '50' },
176 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
177 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
178 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
181 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
184 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
187 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
190 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
193 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
196 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
197 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
200 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
203 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
206 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
209 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
212 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
213 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
214 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
217 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
220 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
223 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
226 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
229 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
232 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
233 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
236 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
237 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
240 { action
=> 'PARAM', proto
=> '89' },
243 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
246 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
247 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
250 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
253 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
256 { action
=> 'PARAM', proto
=> '47' },
257 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
258 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
261 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
264 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
267 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
270 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
273 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
274 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
277 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
280 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
283 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
286 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
289 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
292 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
293 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
294 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
295 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
298 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
299 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
300 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
301 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
302 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
303 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
304 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
305 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
308 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
311 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
314 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
317 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
318 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
321 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
324 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
327 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
330 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
331 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
332 { action
=> 'PARAM', proto
=> '41' },
333 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
336 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
339 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
342 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
343 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
346 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
349 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
352 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
355 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
358 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
359 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
362 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
365 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
368 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
369 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
372 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
375 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
378 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
382 my $pve_fw_parsed_macros;
383 my $pve_fw_preferred_macro_names = {};
385 my $pve_std_chains = {
386 'PVEFW-SET-ACCEPT-MARK' => [
387 "-j MARK --set-mark 1",
389 'PVEFW-DropBroadcast' => [
390 # same as shorewall 'Broadcast'
391 # simply DROP BROADCAST/MULTICAST/ANYCAST
392 # we can use this to reduce logging
393 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
394 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
395 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
396 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
399 # same as shorewall 'reject'
400 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
401 { action
=> 'DROP', source
=> '224.0.0.0/4' },
402 { action
=> 'DROP', proto
=> 'icmp' },
403 "-p tcp -j REJECT --reject-with tcp-reset",
404 "-p udp -j REJECT --reject-with icmp-port-unreachable",
405 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
406 "-j REJECT --reject-with icmp-host-prohibited",
409 # same as shorewall 'Drop', which is equal to DROP,
410 # but REJECT/DROP some packages to reduce logging,
411 # and ACCEPT critical ICMP types
412 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
413 # we are not interested in BROADCAST/MULTICAST/ANYCAST
414 { action
=> 'PVEFW-DropBroadcast' },
415 # ACCEPT critical ICMP types
416 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
417 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
418 # Drop packets with INVALID state
419 "-m conntrack --ctstate INVALID -j DROP",
420 # Drop Microsoft SMB noise
421 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
422 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
423 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
424 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
425 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
426 # Drop new/NotSyn traffic so that it doesn't get logged
427 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
429 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
432 # same as shorewall 'Reject', which is equal to Reject,
433 # but REJECT/DROP some packages to reduce logging,
434 # and ACCEPT critical ICMP types
435 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
436 # we are not interested in BROADCAST/MULTICAST/ANYCAST
437 { action
=> 'PVEFW-DropBroadcast' },
438 # ACCEPT critical ICMP types
439 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
440 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
441 # Drop packets with INVALID state
442 "-m conntrack --ctstate INVALID -j DROP",
443 # Drop Microsoft SMB noise
444 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
445 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
446 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
447 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
448 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
449 # Drop new/NotSyn traffic so that it doesn't get logged
450 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
452 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
454 'PVEFW-tcpflags' => [
455 # same as shorewall tcpflags action.
456 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
457 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
458 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
459 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
460 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
461 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
464 # same as shorewall smurfs action
465 # Filter packets for smurfs (packets with a broadcast address as the source).
466 "-s 0.0.0.0/32 -j RETURN",
467 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
468 "-s 224.0.0.0/4 -g PVEFW-smurflog",
472 # iptables -p icmp -h
473 my $icmp_type_names = {
476 'destination-unreachable' => 1,
477 'network-unreachable' => 1,
478 'host-unreachable' => 1,
479 'protocol-unreachable' => 1,
480 'port-unreachable' => 1,
481 'fragmentation-needed' => 1,
482 'source-route-failed' => 1,
483 'network-unknown' => 1,
485 'network-prohibited' => 1,
486 'host-prohibited' => 1,
487 'TOS-network-unreachable' => 1,
488 'TOS-host-unreachable' => 1,
489 'communication-prohibited' => 1,
490 'host-precedence-violation' => 1,
491 'precedence-cutoff' => 1,
492 'source-quench' => 1,
494 'network-redirect' => 1,
495 'host-redirect' => 1,
496 'TOS-network-redirect' => 1,
497 'TOS-host-redirect' => 1,
499 'router-advertisement' => 1,
500 'router-solicitation' => 1,
501 'time-exceeded' => 1,
502 'ttl-zero-during-transit' => 1,
503 'ttl-zero-during-reassembly' => 1,
504 'parameter-problem' => 1,
505 'ip-header-bad' => 1,
506 'required-option-missing' => 1,
507 'timestamp-request' => 1,
508 'timestamp-reply' => 1,
509 'address-mask-request' => 1,
510 'address-mask-reply' => 1,
513 sub get_firewall_macros
{
515 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
517 $pve_fw_parsed_macros = {};
519 foreach my $k (keys %$pve_fw_macros) {
522 my $macro = $pve_fw_macros->{$k};
523 $pve_fw_preferred_macro_names->{$name} = $k;
524 $pve_fw_parsed_macros->{$name} = $macro;
527 return $pve_fw_parsed_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 # helper function for API
640 sub cleanup_fw_rule
{
641 my ($rule, $digest, $pos) = @_;
645 foreach my $k (keys %$rule) {
646 next if $k eq 'nbdport';
647 next if $k eq 'nbsport';
649 next if !defined($v);
651 $r->{digest
} = $digest;
658 my $rule_properties = {
660 description
=> "Update rule at position <pos>.",
672 enum
=> ['in', 'out', 'group'],
708 sub add_rule_properties
{
709 my ($properties) = @_;
711 foreach my $k (keys %$rule_properties) {
712 $properties->{$k} = $rule_properties->{$k};
719 my ($rule, $param) = @_;
721 foreach my $k (keys %$rule_properties) {
722 if (defined(my $v = $param->{$k})) {
723 if ($v eq '' || $v eq '-') {
736 my $bridge_firewall_enabled = 0;
738 sub enable_bridge_firewall
{
740 return if $bridge_firewall_enabled; # only once
742 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
743 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
745 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
746 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
748 $bridge_firewall_enabled = 1;
751 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
756 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
759 sub iptables_restore_cmdlist
{
762 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
765 sub iptables_get_chains
{
769 # check what chains we want to track
770 my $is_pvefw_chain = sub {
773 return 1 if $name =~ m/^PVEFW-\S+$/;
775 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
777 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
779 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
781 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT|IPS)$/;
782 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
792 return if $line =~ m/^#/;
793 return if $line =~ m/^\s*$/;
795 if ($line =~ m/^\*(\S+)$/) {
800 return if $table ne 'filter';
802 if ($line =~ m/^:(\S+)\s/) {
804 return if !&$is_pvefw_chain($chain);
805 $res->{$chain} = "unknown";
806 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
807 my ($chain, $sig) = ($1, $2);
808 return if !&$is_pvefw_chain($chain);
809 $res->{$chain} = $sig;
811 # simply ignore the rest
816 run_command
("/sbin/iptables-save", outfunc
=> $parser);
821 sub iptables_chain_exist
{
825 iptables
("-n --list $chain");
832 sub iptables_rule_exist
{
836 iptables
("-C $rule");
843 sub ruleset_generate_cmdstr
{
844 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
846 return if defined($rule->{enable
}) && !$rule->{enable
};
850 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
851 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
853 push @cmd, "-m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
854 push @cmd, "-s $rule->{source}" if $rule->{source
};
855 push @cmd, "-m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
856 push @cmd, "-d $rule->{dest}" if $rule->{dest
};
858 if ($rule->{proto
}) {
859 push @cmd, "-p $rule->{proto}";
862 $multiport++ if $rule->{nbdport
} && ($rule->{nbdport
} > 1);
863 $multiport++ if $rule->{nbsport
} && ($rule->{nbsport
} > 1);
865 push @cmd, "--match multiport" if $multiport;
867 die "multiport: option '--sports' cannot be used together with '--dports'\n"
868 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
870 if ($rule->{dport
}) {
871 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
872 # Note: we use dport to store --icmp-type
873 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
874 push @cmd, "-m icmp --icmp-type $rule->{dport}";
876 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
877 if ($multiport == 2) {
878 push @cmd, "--ports $rule->{dport}";
880 push @cmd, "--dports $rule->{dport}";
883 push @cmd, "--dport $rule->{dport}";
888 if ($rule->{sport
}) {
889 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
890 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
892 push @cmd, "--sport $rule->{sport}";
895 } elsif ($rule->{dport
} || $rule->{sport
}) {
896 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
897 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
900 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
902 if (my $action = $rule->{action
}) {
903 $action = $actions->{$action} if defined($actions->{$action});
904 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
905 push @cmd, $goto ?
"-g $action" : "-j $action";
908 return scalar(@cmd) ?
join(' ', @cmd) : undef;
911 sub ruleset_generate_rule
{
912 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
914 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
915 ruleset_addrule
($ruleset, $chain, $cmdstr);
919 sub ruleset_generate_rule_insert
{
920 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
922 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
923 ruleset_insertrule
($ruleset, $chain, $cmdstr);
927 sub ruleset_create_chain
{
928 my ($ruleset, $chain) = @_;
930 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
931 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
933 die "chain '$chain' already exists\n" if $ruleset->{$chain};
935 $ruleset->{$chain} = [];
938 sub ruleset_chain_exist
{
939 my ($ruleset, $chain) = @_;
941 return $ruleset->{$chain} ?
1 : undef;
944 sub ruleset_addrule
{
945 my ($ruleset, $chain, $rule) = @_;
947 die "no such chain '$chain'\n" if !$ruleset->{$chain};
949 push @{$ruleset->{$chain}}, "-A $chain $rule";
952 sub ruleset_insertrule
{
953 my ($ruleset, $chain, $rule) = @_;
955 die "no such chain '$chain'\n" if !$ruleset->{$chain};
957 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
960 sub get_log_rule_base
{
961 my ($chain, $vmid, $msg, $loglevel) = @_;
963 die "internal error - no log level" if !defined($loglevel);
965 $vmid = 0 if !defined($vmid);
967 # Note: we use special format for prefix to pass further
968 # info to log daemon (VMID, LOGVELEL and CHAIN)
970 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
974 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
976 return if !defined($loglevel);
978 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
980 $logrule = "$rule $logrule" if defined($rule);
982 ruleset_addrule($ruleset, $chain, $logrule)
985 sub generate_bridge_chains {
986 my ($ruleset, $hostfw_conf, $bridge, $routing_table) = @_;
988 my $options = $hostfw_conf->{options} || {};
990 die "error
: detected direct route to bridge
'$bridge'\n"
991 if !$options->{allow_bridge_route} && $routing_table->{$bridge};
993 if (!ruleset_chain_exist($ruleset, "$bridge-FW
")) {
994 ruleset_create_chain($ruleset, "$bridge-FW
");
995 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-FW
");
996 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-FW
");
999 if (!ruleset_chain_exist($ruleset, "$bridge-OUT
")) {
1000 ruleset_create_chain($ruleset, "$bridge-OUT
");
1001 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-in
-j
$bridge-OUT
");
1002 ruleset_insertrule($ruleset, "PVEFW-INPUT
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-OUT
");
1005 if (!ruleset_chain_exist($ruleset, "$bridge-IN
")) {
1006 ruleset_create_chain($ruleset, "$bridge-IN
");
1007 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j
$bridge-IN
");
1008 ruleset_addrule($ruleset, "$bridge-FW
", "-m mark
--mark
1 -j ACCEPT
");
1009 # accept traffic to unmanaged bridge ports
1010 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j ACCEPT
");
1014 sub ruleset_add_chain_policy {
1015 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
1017 if ($policy eq 'ACCEPT') {
1019 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
1020 { ACCEPT => $accept_action});
1022 } elsif ($policy eq 'DROP') {
1024 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1026 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1028 ruleset_addrule($ruleset, $chain, "-j DROP
");
1029 } elsif ($policy eq 'REJECT') {
1030 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1032 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1034 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1037 die "internal error
: unknown policy
'$policy'";
1041 sub ruleset_create_vm_chain {
1042 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
1044 ruleset_create_chain($ruleset, $chain);
1045 my $accept = generate_nfqueue($options);
1047 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1048 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1051 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1052 if ($direction eq 'OUT') {
1053 ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK',
1054 proto => 'udp', sport => 68, dport => 67 });
1056 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT',
1057 proto => 'udp', sport => 67, dport => 68 });
1061 if ($options->{tcpflags}) {
1062 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1065 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1066 if($direction eq 'OUT'){
1067 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-g PVEFW-SET-ACCEPT-MARK
");
1070 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1072 if ($direction eq 'OUT') {
1073 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1074 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1076 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1082 sub ruleset_generate_vm_rules {
1083 my ($ruleset, $rules, $groups_conf, $chain, $netid, $direction, $options) = @_;
1085 my $lc_direction = lc($direction);
1087 foreach my $rule (@$rules) {
1088 next if $rule->{iface} && $rule->{iface} ne $netid;
1089 next if !$rule->{enable};
1090 if ($rule->{type} eq 'group') {
1091 my $group_chain = "GROUP-
$rule->{action
}-$direction";
1092 if(!ruleset_chain_exist($ruleset, $group_chain)){
1093 generate_group_rules($ruleset, $groups_conf, $rule->{action});
1095 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1096 if ($direction eq 'OUT'){
1097 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j RETURN
");
1099 my $accept = generate_nfqueue($options);
1100 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$accept");
1104 next if $rule->{type} ne $lc_direction;
1105 if ($direction eq 'OUT') {
1106 ruleset_generate_rule($ruleset, $chain, $rule,
1107 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1109 my $accept = generate_nfqueue($options);
1110 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject
" });
1116 sub generate_nfqueue {
1120 if($options->{ips}){
1121 $action = "NFQUEUE
";
1122 if($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1123 if(defined($3) && defined($1)) {
1124 $action .= " --queue-balance
$1:$3";
1125 }elsif (defined($1)) {
1126 $action .= " --queue-num
$1";
1129 $action .= " --queue-bypass
";
1137 sub ruleset_generate_vm_ipsrules {
1138 my ($ruleset, $options, $direction, $iface, $bridge) = @_;
1140 if ($options->{ips} && $direction eq 'IN') {
1141 my $nfqueue = generate_nfqueue($options);
1143 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1144 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1147 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1148 ruleset_create_chain($ruleset, "$bridge-IPS
");
1149 ruleset_insertrule($ruleset, "PVEFW-IPS
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-IPS
");
1152 ruleset_addrule($ruleset, "$bridge-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1156 sub generate_venet_rules_direction {
1157 my ($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
1159 parse_address_list($ip); # make sure we have a valid $ip list
1161 my $lc_direction = lc($direction);
1163 my $rules = $vmfw_conf->{rules};
1165 my $options = $vmfw_conf->{options};
1166 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1168 my $chain = "venet0-
$vmid-$direction";
1170 ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction);
1172 ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $chain, 'venet', $direction);
1177 if ($direction eq 'OUT') {
1178 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1180 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1183 my $accept = generate_nfqueue($options);
1184 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1185 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1187 # plug into FORWARD, INPUT and OUTPUT chain
1188 if ($direction eq 'OUT') {
1189 ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD
", {
1192 iface_in => 'venet0'});
1194 ruleset_generate_rule_insert($ruleset, "PVEFW-INPUT
", {
1197 iface_in => 'venet0'});
1199 ruleset_generate_rule($ruleset, "PVEFW-FORWARD
", {
1202 iface_out => 'venet0'});
1204 ruleset_generate_rule($ruleset, "PVEFW-OUTPUT
", {
1207 iface_out => 'venet0'});
1211 sub generate_tap_rules_direction {
1212 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_;
1214 my $lc_direction = lc($direction);
1216 my $rules = $vmfw_conf->{rules};
1218 my $options = $vmfw_conf->{options};
1219 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1221 my $tapchain = "$iface-$direction";
1223 ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction);
1225 ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $tapchain, $netid, $direction, $options);
1227 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface, $bridge);
1232 if ($direction eq 'OUT') {
1233 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1235 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1238 my $accept = generate_nfqueue($options);
1239 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1240 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
1242 # plug the tap chain to bridge chain
1243 if ($direction eq 'IN') {
1244 ruleset_insertrule($ruleset, "$bridge-IN
",
1245 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
1247 ruleset_insertrule($ruleset, "$bridge-OUT
",
1248 "-m physdev
--physdev-in
$iface -j
$tapchain");
1252 sub enable_host_firewall {
1253 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
1255 # fixme: allow security groups
1257 my $options = $hostfw_conf->{options};
1258 my $rules = $hostfw_conf->{rules};
1260 # host inbound firewall
1261 my $chain = "PVEFW-HOST-IN
";
1262 ruleset_create_chain($ruleset, $chain);
1264 my $loglevel = get_option_log_level($options, "log_level_in
");
1266 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1267 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1270 if ($options->{tcpflags}) {
1271 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1274 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1275 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1276 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
1277 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1278 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1279 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1281 # we use RETURN because we need to check also tap rules
1282 my $accept_action = 'RETURN';
1284 foreach my $rule (@$rules) {
1285 next if $rule->{type} ne 'in';
1286 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1289 # implement input policy
1290 my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1291 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1293 # host outbound firewall
1294 $chain = "PVEFW-HOST-OUT
";
1295 ruleset_create_chain($ruleset, $chain);
1297 $loglevel = get_option_log_level($options, "log_level_out
");
1299 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1300 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1301 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
1302 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1303 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1304 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1306 # we use RETURN because we may want to check other thigs later
1307 $accept_action = 'RETURN';
1309 foreach my $rule (@$rules) {
1310 next if $rule->{type} ne 'out';
1311 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1314 # implement output policy
1315 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1316 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1318 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
1319 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
1322 sub generate_group_rules {
1323 my ($ruleset, $groups_conf, $group) = @_;
1324 die "no such security group
'$group'\n" if !$groups_conf->{rules}->{$group};
1326 my $rules = $groups_conf->{rules}->{$group};
1328 my $chain = "GROUP-
${group
}-IN
";
1330 ruleset_create_chain($ruleset, $chain);
1331 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1333 foreach my $rule (@$rules) {
1334 next if $rule->{type} ne 'in';
1335 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1338 $chain = "GROUP-
${group
}-OUT
";
1340 ruleset_create_chain($ruleset, $chain);
1341 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1343 foreach my $rule (@$rules) {
1344 next if $rule->{type} ne 'out';
1345 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1346 # check also other tap rules later
1347 ruleset_generate_rule($ruleset, $chain, $rule,
1348 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" });
1353 my $valid_netdev_names = {};
1354 for (my $i = 0; $i < $MAX_NETS; $i++) {
1355 $valid_netdev_names->{"net
$i"} = 1;
1359 my ($line, $need_iface, $allow_groups) = @_;
1361 my $macros = get_firewall_macros();
1362 my $protocols = get_etc_protocols();
1364 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1366 # we can add single line comments to the end of the rule
1367 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
1369 # we can disable a rule when prefixed with '|'
1372 $enable = 0 if $line =~ s/^\|//;
1374 my @data = split(/\s+/, $line);
1375 my $expected_elements = $need_iface ? 8 : 7;
1377 die "wrong number of rule elements
\n" if scalar(@data) > $expected_elements;
1380 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1382 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1385 die "incomplete rule
\n" if ! ($type && $action);
1392 if ($type eq 'in' || $type eq 'out') {
1393 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1395 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1396 ($macro_name, $action) = ($1, $2);
1397 my $lc_macro_name = lc($macro_name);
1398 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1399 $macro_name = $preferred_name if $preferred_name;
1400 $macro = $macros->{$lc_macro_name};
1401 die "unknown
macro '$macro_name'\n" if !$macro;
1403 die "unknown action
'$action'\n";
1405 } elsif ($type eq 'group') {
1406 die "wrong number of rule elements
\n" if scalar(@data) != 3;
1407 die "groups disabled
\n" if !$allow_groups;
1409 die "invalid characters
in group name
\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1411 die "unknown rule type
'$type'\n";
1415 $iface = undef if $iface && $iface eq '-';
1418 $proto = undef if $proto && $proto eq '-';
1419 die "unknown protokol
'$proto'\n" if $proto &&
1420 !(defined($protocols->{byname}->{$proto}) ||
1421 defined($protocols->{byid}->{$proto}));
1423 $source = undef if $source && $source eq '-';
1424 $dest = undef if $dest && $dest eq '-';
1426 $dport = undef if $dport && $dport eq '-';
1427 $sport = undef if $sport && $sport eq '-';
1429 my $nbsource = undef;
1432 $nbsource = parse_address_list($source) if $source;
1433 $nbdest = parse_address_list($dest) if $dest;
1440 comment => $comment,
1445 nbsource => $nbsource,
1453 foreach my $templ (@$macro) {
1455 my $param_used = {};
1456 foreach my $k (keys %$templ) {
1457 my $v = $templ->{$k};
1458 if ($v eq 'PARAM') {
1460 $param_used->{$k} = 1;
1461 } elsif ($v eq 'DEST') {
1462 $v = $param->{dest};
1463 $param_used->{dest} = 1;
1464 } elsif ($v eq 'SOURCE') {
1465 $v = $param->{source};
1466 $param_used->{source} = 1;
1469 die "missing parameter
'$k' in macro '$macro_name'\n" if !defined($v);
1472 foreach my $k (keys %$param) {
1473 next if !defined($param->{$k});
1474 next if $param_used->{$k};
1475 if (defined($rule->{$k})) {
1476 die "parameter
'$k' already define
in macro (value
= '$rule->{$k}')\n"
1477 if $rule->{$k} ne $param->{$k};
1479 $rule->{$k} = $param->{$k};
1482 push @$rules, $rule;
1485 push @$rules, $param;
1488 foreach my $rule (@$rules) {
1489 $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport})
1490 if defined($rule->{dport});
1491 $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport})
1492 if defined($rule->{sport});
1498 sub parse_vmfw_option {
1503 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
1505 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags|ips):\s*(0|1)\s*$/i) {
1508 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1510 $value = $2 ? lc($3) : '';
1511 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1514 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
1519 die "can
't parse option '$line'\n"
1522 return ($opt, $value);
1525 sub parse_hostfw_option {
1530 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1532 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags|allow_bridge_route|optimize):\s*(0|1)\s*$/i) {
1535 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1537 $value = $2 ? lc($3) : '';
1538 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1541 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1546 die "can't parse option
'$line'\n"
1549 return ($opt, $value);
1552 sub parse_vm_fw_rules {
1553 my ($filename, $fh) = @_;
1555 my $res = { rules => [], options => {}};
1559 my $digest = Digest::SHA->new('sha1');
1561 while (defined(my $line = <$fh>)) {
1562 $digest->add($line);
1564 next if $line =~ m/^#/;
1565 next if $line =~ m/^\s*$/;
1567 my $linenr = $fh->input_line_number();
1568 my $prefix = "$filename (line
$linenr)";
1570 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1572 warn "$prefix: ignore unknown section
'$section'\n" if !$res->{$section};
1576 warn "$prefix: skip line
- no section
";
1580 next if !$res->{$section}; # skip undefined section
1582 if ($section eq 'options') {
1584 my ($opt, $value) = parse_vmfw_option($line);
1585 $res->{options}->{$opt} = $value;
1587 warn "$prefix: $@" if $@;
1592 eval { $rules = parse_fw_rule($line, 1, 1); };
1594 warn "$prefix: $err";
1598 push @{$res->{$section}}, @$rules;
1601 $res->{digest} = $digest->b64digest;
1606 sub parse_host_fw_rules {
1607 my ($filename, $fh) = @_;
1609 my $res = { rules => [], options => {}};
1613 my $digest = Digest::SHA->new('sha1');
1615 while (defined(my $line = <$fh>)) {
1616 $digest->add($line);
1618 next if $line =~ m/^#/;
1619 next if $line =~ m/^\s*$/;
1621 my $linenr = $fh->input_line_number();
1622 my $prefix = "$filename (line
$linenr)";
1624 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1626 warn "$prefix: ignore unknown section
'$section'\n" if !$res->{$section};
1630 warn "$prefix: skip line
- no section
";
1634 next if !$res->{$section}; # skip undefined section
1636 if ($section eq 'options') {
1638 my ($opt, $value) = parse_hostfw_option($line);
1639 $res->{options}->{$opt} = $value;
1641 warn "$prefix: $@" if $@;
1646 eval { $rules = parse_fw_rule($line, 1, 1); };
1648 warn "$prefix: $err";
1652 push @{$res->{$section}}, @$rules;
1655 $res->{digest} = $digest->b64digest;
1660 sub parse_group_fw_rules {
1661 my ($filename, $fh) = @_;
1666 my $res = { rules => {} };
1668 my $digest = Digest::SHA->new('sha1');
1670 while (defined(my $line = <$fh>)) {
1671 $digest->add($line);
1673 next if $line =~ m/^#/;
1674 next if $line =~ m/^\s*$/;
1676 my $linenr = $fh->input_line_number();
1677 my $prefix = "$filename (line
$linenr)";
1679 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1684 if (!$section || !$group) {
1685 warn "$prefix: skip line
- no section
";
1690 eval { $rules = parse_fw_rule($line, 0, 0); };
1692 warn "$prefix: $err";
1696 push @{$res->{$section}->{$group}}, @$rules;
1699 $res->{digest} = $digest->b64digest;
1705 my ($code, @param) = @_;
1709 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1716 sub read_local_vm_config {
1721 my $vmdata = { openvz => $openvz, qemu => $qemu };
1723 my $vmlist = PVE::Cluster::get_vmlist();
1724 return $vmdata if !$vmlist || !$vmlist->{ids};
1725 my $ids = $vmlist->{ids};
1727 foreach my $vmid (keys %$ids) {
1728 next if !$vmid; # skip VE0
1729 my $d = $ids->{$vmid};
1730 next if !$d->{node} || $d->{node} ne $nodename;
1731 next if !$d->{type};
1732 if ($d->{type} eq 'openvz') {
1733 if ($have_pve_manager) {
1734 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
1735 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1736 $openvz->{$vmid} = $conf;
1739 } elsif ($d->{type} eq 'qemu') {
1740 if ($have_qemu_server) {
1741 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1742 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1743 $qemu->{$vmid} = $conf;
1752 sub load_vmfw_conf {
1757 my $filename = "/etc/pve
/firewall/$vmid.fw
";
1758 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1759 $vmfw_conf = parse_vm_fw_rules($filename, $fh);
1765 sub read_vm_firewall_configs {
1767 my $vmfw_configs = {};
1769 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1770 my $vmfw_conf = load_vmfw_conf($vmid);
1771 next if !$vmfw_conf->{options}; # skip if file does not exists
1772 $vmfw_configs->{$vmid} = $vmfw_conf;
1775 return $vmfw_configs;
1778 sub get_option_log_level {
1779 my ($options, $k) = @_;
1781 my $v = $options->{$k};
1782 $v = $default_log_level if !defined($v);
1784 return undef if $v eq '' || $v eq 'nolog';
1786 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1788 return $v if ($v >= 0) && ($v <= 7);
1790 warn "unknown
log level
($k = '$v')\n";
1795 sub generate_std_chains {
1796 my ($ruleset, $options) = @_;
1798 my $loglevel = get_option_log_level($options, 'smurf_log_level');
1800 # same as shorewall smurflog.
1801 my $chain = 'PVEFW-smurflog';
1803 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP
: ", $loglevel) if $loglevel;
1804 push @{$pve_std_chains->{$chain}}, "-j DROP
";
1806 # same as shorewall logflags action.
1807 $loglevel = get_option_log_level($options, 'tcp_flags_log_level');
1808 $chain = 'PVEFW-logflags';
1809 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
1810 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP
: ", $loglevel) if $loglevel;
1811 push @{$pve_std_chains->{$chain}}, "-j DROP
";
1813 foreach my $chain (keys %$pve_std_chains) {
1814 ruleset_create_chain($ruleset, $chain);
1815 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1817 ruleset_generate_rule($ruleset, $chain, $rule);
1819 ruleset_addrule($ruleset, $chain, $rule);
1825 sub save_pvefw_status {
1828 die "unknown status
'$status' - internal error
"
1829 if $status !~ m/^(stopped|active)$/;
1831 mkdir dirname($pve_fw_status_filename);
1832 PVE::Tools::file_set_contents($pve_fw_status_filename, $status);
1835 sub read_pvefw_status {
1837 my $status = 'unknown';
1839 return 'stopped' if ! -f $pve_fw_status_filename;
1842 $status = PVE::Tools::file_get_contents($pve_fw_status_filename);
1849 # fixme: move to pve-common PVE::ProcFSTools
1850 sub read_proc_net_route {
1851 my $filename = "/proc/net
/route
";
1855 my $fh = IO::File->new ($filename, "r
");
1856 return $res if !$fh;
1858 my $int_to_quad = sub {
1859 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
1862 while (defined(my $line = <$fh>)) {
1863 next if $line =~/^Iface\s+Destination/; # skip head
1864 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
1865 push @{$res->{$iface}}, {
1866 dest => &$int_to_quad(hex($dest)),
1867 gateway => &$int_to_quad(hex($gateway)),
1868 mask => &$int_to_quad(hex($mask)),
1877 sub load_security_groups {
1879 my $groups_conf = {};
1880 my $filename = "/etc/pve
/firewall/groups
.fw
";
1881 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1882 $groups_conf = parse_group_fw_rules($filename, $fh);
1885 return $groups_conf;
1888 sub save_security_groups {
1889 my ($groups_conf) = @_;
1892 my $filename = "/etc/pve
/firewall/groups
.fw
";
1894 foreach my $group (sort keys %{$groups_conf->{rules}}) {
1895 my $rules = $groups_conf->{rules}->{$group};
1896 $raw .= "[group
$group]\n\n";
1898 foreach my $rule (@$rules) {
1899 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
1900 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
1901 $raw .= uc($rule->{type});
1902 $raw .= " " . $rule->{action};
1903 $raw .= " " . ($rule->{source} || '-');
1904 $raw .= " " . ($rule->{dest} || '-');
1905 $raw .= " " . ($rule->{proto} || '-');
1906 $raw .= " " . ($rule->{dport} || '-');
1907 $raw .= " " . ($rule->{sport} || '-');
1908 $raw .= " # " . encode('utf8', $rule->{comment})
1909 if $rule->{comment
} && $rule->{comment
} !~ m/^\s*$/;
1912 die "implement me '$rule->{type}'";
1919 PVE
::Tools
::file_set_contents
($filename, $raw);
1922 sub load_hostfw_conf
{
1924 my $hostfw_conf = {};
1925 my $filename = "/etc/pve/local/host.fw";
1926 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1927 $hostfw_conf = parse_host_fw_rules
($filename, $fh);
1929 return $hostfw_conf;
1933 my $vmdata = read_local_vm_config
();
1934 my $vmfw_configs = read_vm_firewall_configs
($vmdata);
1936 my $routing_table = read_proc_net_route
();
1938 my $groups_conf = load_security_groups
();
1942 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1943 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1945 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1947 my $hostfw_conf = load_hostfw_conf
();
1948 my $hostfw_options = $hostfw_conf->{options
} || {};
1950 generate_std_chains
($ruleset, $hostfw_options);
1952 my $hostfw_enable = !(defined($hostfw_options->{enable
}) && ($hostfw_options->{enable
} == 0));
1954 enable_host_firewall
($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
1956 my $ips_enable = undef;
1958 # generate firewall rules for QEMU VMs
1959 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
1960 my $conf = $vmdata->{qemu
}->{$vmid};
1961 my $vmfw_conf = $vmfw_configs->{$vmid};
1962 next if !$vmfw_conf;
1963 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1965 $ips_enable = 1 if $vmfw_conf->{options
}->{ips
};
1967 foreach my $netid (keys %$conf) {
1968 next if $netid !~ m/^net(\d+)$/;
1969 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
1971 my $iface = "tap${vmid}i$1";
1973 my $bridge = $net->{bridge
};
1974 next if !$bridge; # fixme: ?
1976 $bridge .= "v$net->{tag}" if $net->{tag
};
1978 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge, $routing_table);
1980 my $macaddr = $net->{macaddr
};
1981 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr,
1982 $vmfw_conf, $vmid, $bridge, 'IN');
1983 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr,
1984 $vmfw_conf, $vmid, $bridge, 'OUT');
1988 # generate firewall rules for OpenVZ containers
1989 foreach my $vmid (keys %{$vmdata->{openvz
}}) {
1990 my $conf = $vmdata->{openvz
}->{$vmid};
1992 my $vmfw_conf = $vmfw_configs->{$vmid};
1993 next if !$vmfw_conf;
1994 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1996 $ips_enable = 1 if $vmfw_conf->{options
}->{ips
};
1998 if ($conf->{ip_address
} && $conf->{ip_address
}->{value
}) {
1999 my $ip = $conf->{ip_address
}->{value
};
2000 generate_venet_rules_direction
($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'IN');
2001 generate_venet_rules_direction
($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'OUT');
2004 if ($conf->{netif
} && $conf->{netif
}->{value
}) {
2005 my $netif = PVE
::OpenVZ
::parse_netif
($conf->{netif
}->{value
});
2006 foreach my $netid (keys %$netif) {
2007 my $d = $netif->{$netid};
2008 my $bridge = $d->{bridge
};
2010 warn "no bridge device for CT $vmid iface '$netid'\n";
2014 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge, $routing_table);
2016 my $macaddr = $d->{mac
};
2017 my $iface = $d->{host_ifname
};
2018 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr,
2019 $vmfw_conf, $vmid, $bridge, 'IN');
2020 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr,
2021 $vmfw_conf, $vmid, $bridge, 'OUT');
2026 if($hostfw_options->{optimize
}){
2028 my $accept = $ips_enable ?
"PVEFW-IPS" : "ACCEPT";
2029 ruleset_insertrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept");
2030 ruleset_insertrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate INVALID -j DROP");
2033 # fixme: what log level should we use here?
2034 my $loglevel = get_option_log_level
($hostfw_options, "log_level_out");
2036 # fixme: should we really block inter-bridge traffic?
2038 # always allow traffic from containers?
2039 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN");
2041 # disable interbridge routing
2042 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
2043 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
2044 ruleset_addlog
($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+");
2045 ruleset_addlog
($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+");
2046 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
2047 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
2049 return wantarray ?
($ruleset, $hostfw_conf) : $ruleset;
2052 sub get_ruleset_status
{
2053 my ($ruleset, $verbose) = @_;
2055 my $active_chains = iptables_get_chains
();
2057 my $statushash = {};
2059 foreach my $chain (sort keys %$ruleset) {
2060 my $digest = Digest
::SHA-
>new('sha1');
2061 foreach my $cmd (@{$ruleset->{$chain}}) {
2062 $digest->add("$cmd\n");
2064 my $sig = $digest->b64digest;
2065 $statushash->{$chain}->{sig
} = $sig;
2067 my $oldsig = $active_chains->{$chain};
2068 if (!defined($oldsig)) {
2069 $statushash->{$chain}->{action
} = 'create';
2071 if ($oldsig eq $sig) {
2072 $statushash->{$chain}->{action
} = 'exists';
2074 $statushash->{$chain}->{action
} = 'update';
2077 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
2078 foreach my $cmd (@{$ruleset->{$chain}}) {
2079 print "\t$cmd\n" if $verbose;
2083 foreach my $chain (sort keys %$active_chains) {
2084 if (!defined($ruleset->{$chain})) {
2085 my $sig = $active_chains->{$chain};
2086 $statushash->{$chain}->{action
} = 'delete';
2087 $statushash->{$chain}->{sig
} = $sig;
2088 print "delete $chain ($sig)\n" if $verbose;
2098 get_ruleset_status
($ruleset, 1);
2101 sub print_sig_rule
{
2102 my ($chain, $sig) = @_;
2104 # We just use this to store a SHA1 checksum used to detect changes
2105 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
2108 sub get_rulset_cmdlist {
2109 my ($ruleset, $verbose) = @_;
2111 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
2113 my $statushash = get_ruleset_status($ruleset, $verbose);
2115 # create missing chains first
2116 foreach my $chain (sort keys %$ruleset) {
2117 my $stat = $statushash->{$chain};
2118 die "internal error
" if !$stat;
2119 next if $stat->{action} ne 'create';
2121 $cmdlist .= ":$chain - [0:0]\n";
2124 my $rule = "INPUT
-j PVEFW-INPUT
";
2125 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2126 $cmdlist .= "-A
$rule\n";
2128 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
2129 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2130 $cmdlist .= "-A
$rule\n";
2133 $rule = "FORWARD
-j PVEFW-FORWARD
";
2134 if (!PVE::Firewall::iptables_rule_exist($rule)) {
2135 $cmdlist .= "-A
$rule\n";
2138 foreach my $chain (sort keys %$ruleset) {
2139 my $stat = $statushash->{$chain};
2140 die "internal error
" if !$stat;
2142 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
2143 $cmdlist .= "-F
$chain\n";
2144 foreach my $cmd (@{$ruleset->{$chain}}) {
2145 $cmdlist .= "$cmd\n";
2147 $cmdlist .= print_sig_rule($chain, $stat->{sig});
2148 } elsif ($stat->{action} eq 'delete') {
2149 die "internal error
"; # this should not happen
2150 } elsif ($stat->{action} eq 'exists') {
2153 die "internal error
- unknown status
'$stat->{action}'";
2157 foreach my $chain (keys %$statushash) {
2158 next if $statushash->{$chain}->{action} ne 'delete';
2159 $cmdlist .= "-F
$chain\n";
2161 foreach my $chain (keys %$statushash) {
2162 next if $statushash->{$chain}->{action} ne 'delete';
2163 next if $chain eq 'PVEFW-INPUT';
2164 next if $chain eq 'PVEFW-OUTPUT';
2165 next if $chain eq 'PVEFW-FORWARD';
2166 $cmdlist .= "-X
$chain\n";
2169 $cmdlist .= "COMMIT
\n";
2175 my ($ruleset, $hostfw_conf, $verbose) = @_;
2177 enable_bridge_firewall();
2179 update_nf_conntrack_max($hostfw_conf);
2181 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
2183 print $cmdlist if $verbose;
2185 iptables_restore_cmdlist($cmdlist);
2187 # test: re-read status and check if everything is up to date
2188 my $statushash = get_ruleset_status($ruleset);
2191 foreach my $chain (sort keys %$ruleset) {
2192 my $stat = $statushash->{$chain};
2193 if ($stat->{action} ne 'exists') {
2194 warn "unable to update chain
'$chain'\n";
2199 die "unable to apply firewall changes
\n" if $errors;
2202 sub update_nf_conntrack_max {
2203 my ($hostfw_conf) = @_;
2205 my $max = 65536; # reasonable default
2207 my $options = $hostfw_conf->{options} || {};
2209 if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) {
2210 $max = $options->{nf_conntrack_max};
2211 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
2214 my $filename_nf_conntrack_max = "/proc/sys
/net/nf_conntrack_max
";
2215 my $filename_hashsize = "/sys/module
/nf_conntrack/parameters
/hashsize
";
2217 my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max);
2219 if ($current != $max) {
2220 my $hashsize = int($max/4);
2221 PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize);
2222 PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max);
2227 my ($start, $verbose) = @_;
2230 my $status = read_pvefw_status();
2232 my ($ruleset, $hostfw_conf) = PVE::Firewall::compile();
2234 if ($start || $status eq 'active') {
2236 save_pvefw_status('active') if ($status ne 'active');
2238 apply_ruleset($ruleset, $hostfw_conf, $verbose);
2240 print "Firewall
not active
(status
= $status)\n" if $verbose;