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 $bridge_firewall_enabled = 0;
660 sub enable_bridge_firewall
{
662 return if $bridge_firewall_enabled; # only once
664 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
665 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
667 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
668 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
670 $bridge_firewall_enabled = 1;
673 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
678 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
681 sub iptables_restore_cmdlist
{
684 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
687 sub iptables_get_chains
{
691 # check what chains we want to track
692 my $is_pvefw_chain = sub {
695 return 1 if $name =~ m/^PVEFW-\S+$/;
697 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
699 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
701 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
703 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
704 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
714 return if $line =~ m/^#/;
715 return if $line =~ m/^\s*$/;
717 if ($line =~ m/^\*(\S+)$/) {
722 return if $table ne 'filter';
724 if ($line =~ m/^:(\S+)\s/) {
726 return if !&$is_pvefw_chain($chain);
727 $res->{$chain} = "unknown";
728 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
729 my ($chain, $sig) = ($1, $2);
730 return if !&$is_pvefw_chain($chain);
731 $res->{$chain} = $sig;
733 # simply ignore the rest
738 run_command
("/sbin/iptables-save", outfunc
=> $parser);
743 sub iptables_chain_exist
{
747 iptables
("-n --list $chain");
754 sub iptables_rule_exist
{
758 iptables
("-C $rule");
765 sub ruleset_generate_cmdstr
{
766 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
768 return if $rule->{disable
};
772 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
773 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
775 push @cmd, "-m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
776 push @cmd, "-s $rule->{source}" if $rule->{source
};
777 push @cmd, "-m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
778 push @cmd, "-d $rule->{dest}" if $rule->{dest
};
780 if ($rule->{proto
}) {
781 push @cmd, "-p $rule->{proto}";
784 $multiport++ if $rule->{nbdport
} && ($rule->{nbdport
} > 1);
785 $multiport++ if $rule->{nbsport
} && ($rule->{nbsport
} > 1);
787 push @cmd, "--match multiport" if $multiport;
789 die "multiport: option '--sports' cannot be used together with '--dports'\n"
790 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
792 if ($rule->{dport
}) {
793 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
794 # Note: we use dport to store --icmp-type
795 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
796 push @cmd, "-m icmp --icmp-type $rule->{dport}";
798 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
799 if ($multiport == 2) {
800 push @cmd, "--ports $rule->{dport}";
802 push @cmd, "--dports $rule->{dport}";
805 push @cmd, "--dport $rule->{dport}";
810 if ($rule->{sport
}) {
811 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
812 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
814 push @cmd, "--sport $rule->{sport}";
817 } elsif ($rule->{dport
} || $rule->{sport
}) {
818 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
819 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
822 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
824 if (my $action = $rule->{action
}) {
825 $action = $actions->{$action} if defined($actions->{$action});
826 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
827 push @cmd, $goto ?
"-g $action" : "-j $action";
830 return scalar(@cmd) ?
join(' ', @cmd) : undef;
833 sub ruleset_generate_rule
{
834 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
836 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
837 ruleset_addrule
($ruleset, $chain, $cmdstr);
840 sub ruleset_generate_rule_insert
{
841 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
843 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
844 ruleset_insertrule
($ruleset, $chain, $cmdstr);
848 sub ruleset_create_chain
{
849 my ($ruleset, $chain) = @_;
851 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
852 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
854 die "chain '$chain' already exists\n" if $ruleset->{$chain};
856 $ruleset->{$chain} = [];
859 sub ruleset_chain_exist
{
860 my ($ruleset, $chain) = @_;
862 return $ruleset->{$chain} ?
1 : undef;
865 sub ruleset_addrule
{
866 my ($ruleset, $chain, $rule) = @_;
868 die "no such chain '$chain'\n" if !$ruleset->{$chain};
870 push @{$ruleset->{$chain}}, "-A $chain $rule";
873 sub ruleset_insertrule
{
874 my ($ruleset, $chain, $rule) = @_;
876 die "no such chain '$chain'\n" if !$ruleset->{$chain};
878 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
881 sub get_log_rule_base
{
882 my ($chain, $vmid, $msg, $loglevel) = @_;
884 die "internal error - no log level" if !defined($loglevel);
886 $vmid = 0 if !defined($vmid);
888 # Note: we use special format for prefix to pass further
889 # info to log daemon (VMID, LOGVELEL and CHAIN)
891 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
895 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
897 return if !defined($loglevel);
899 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
901 $logrule = "$rule $logrule" if defined($rule);
903 ruleset_addrule($ruleset, $chain, $logrule)
906 sub generate_bridge_chains {
907 my ($ruleset, $hostfw_conf, $bridge, $routing_table) = @_;
909 my $options = $hostfw_conf->{options} || {};
911 die "error
: detected direct route to bridge
'$bridge'\n"
912 if !$options->{allow_bridge_route} && $routing_table->{$bridge};
914 if (!ruleset_chain_exist($ruleset, "$bridge-FW
")) {
915 ruleset_create_chain($ruleset, "$bridge-FW
");
916 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-FW
");
917 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-FW
");
920 if (!ruleset_chain_exist($ruleset, "$bridge-OUT
")) {
921 ruleset_create_chain($ruleset, "$bridge-OUT
");
922 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-in
-j
$bridge-OUT
");
923 ruleset_insertrule($ruleset, "PVEFW-INPUT
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-OUT
");
926 if (!ruleset_chain_exist($ruleset, "$bridge-IN
")) {
927 ruleset_create_chain($ruleset, "$bridge-IN
");
928 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j
$bridge-IN
");
929 ruleset_addrule($ruleset, "$bridge-FW
", "-m mark
--mark
1 -j ACCEPT
");
930 # accept traffic to unmanaged bridge ports
931 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j ACCEPT
");
935 sub ruleset_add_chain_policy {
936 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
938 if ($policy eq 'ACCEPT') {
940 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
941 { ACCEPT => $accept_action});
943 } elsif ($policy eq 'DROP') {
945 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
947 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
949 ruleset_addrule($ruleset, $chain, "-j DROP
");
950 } elsif ($policy eq 'REJECT') {
951 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
953 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
955 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
958 die "internal error
: unknown policy
'$policy'";
962 sub ruleset_create_vm_chain {
963 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
965 ruleset_create_chain($ruleset, $chain);
967 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
968 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
971 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
972 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
67:68 -j ACCEPT
");
975 if ($options->{tcpflags}) {
976 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
979 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
980 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
982 if ($direction eq 'OUT') {
983 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
984 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
986 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
990 sub ruleset_generate_vm_rules {
991 my ($ruleset, $rules, $groups_conf, $chain, $netid, $direction) = @_;
993 my $lc_direction = lc($direction);
995 foreach my $rule (@$rules) {
996 next if $rule->{iface} && $rule->{iface} ne $netid;
997 next if $rule->{disable};
998 if ($rule->{type} eq 'group') {
999 my $group_chain = "GROUP-
$rule->{action
}-$direction";
1000 if(!ruleset_chain_exist($ruleset, $group_chain)){
1001 generate_group_rules($ruleset, $groups_conf, $rule->{action});
1003 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1004 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j RETURN
")
1005 if $direction eq 'OUT';
1007 next if $rule->{type} ne $lc_direction;
1008 if ($direction eq 'OUT') {
1009 ruleset_generate_rule($ruleset, $chain, $rule,
1010 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" });
1012 ruleset_generate_rule($ruleset, $chain, $rule, { REJECT => "PVEFW-reject
" });
1018 sub generate_venet_rules_direction {
1019 my ($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
1021 parse_address_list($ip); # make sure we have a valid $ip list
1023 my $lc_direction = lc($direction);
1025 my $rules = $vmfw_conf->{rules};
1027 my $options = $vmfw_conf->{options};
1028 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1030 my $chain = "venet0-
$vmid-$direction";
1032 ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction);
1034 ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $chain, 'venet', $direction);
1039 if ($direction eq 'OUT') {
1040 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1042 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1045 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : "ACCEPT
";
1046 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1048 # plug into FORWARD, INPUT and OUTPUT chain
1049 if ($direction eq 'OUT') {
1050 ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD
", {
1053 iface_in => 'venet0'});
1055 ruleset_generate_rule_insert($ruleset, "PVEFW-INPUT
", {
1058 iface_in => 'venet0'});
1060 ruleset_generate_rule($ruleset, "PVEFW-FORWARD
", {
1063 iface_out => 'venet0'});
1065 ruleset_generate_rule($ruleset, "PVEFW-OUTPUT
", {
1068 iface_out => 'venet0'});
1072 sub generate_tap_rules_direction {
1073 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_;
1075 my $lc_direction = lc($direction);
1077 my $rules = $vmfw_conf->{rules};
1079 my $options = $vmfw_conf->{options};
1080 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1082 my $tapchain = "$iface-$direction";
1084 ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction);
1086 ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $tapchain, $netid, $direction);
1091 if ($direction eq 'OUT') {
1092 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1094 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1097 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : "ACCEPT
";
1098 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
1100 # plug the tap chain to bridge chain
1101 if ($direction eq 'IN') {
1102 ruleset_insertrule($ruleset, "$bridge-IN
",
1103 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
1105 ruleset_insertrule($ruleset, "$bridge-OUT
",
1106 "-m physdev
--physdev-in
$iface -j
$tapchain");
1110 sub enable_host_firewall {
1111 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
1113 # fixme: allow security groups
1115 my $options = $hostfw_conf->{options};
1116 my $rules = $hostfw_conf->{rules};
1118 # host inbound firewall
1119 my $chain = "PVEFW-HOST-IN
";
1120 ruleset_create_chain($ruleset, $chain);
1122 my $loglevel = get_option_log_level($options, "log_level_in
");
1124 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1125 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1126 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
1127 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1128 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1129 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1131 # we use RETURN because we need to check also tap rules
1132 my $accept_action = 'RETURN';
1134 foreach my $rule (@$rules) {
1135 next if $rule->{type} ne 'in';
1136 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1139 # implement input policy
1140 my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1141 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1143 # host outbound firewall
1144 $chain = "PVEFW-HOST-OUT
";
1145 ruleset_create_chain($ruleset, $chain);
1147 $loglevel = get_option_log_level($options, "log_level_out
");
1149 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1150 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1151 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
1152 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1153 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1154 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1156 # we use RETURN because we may want to check other thigs later
1157 $accept_action = 'RETURN';
1159 foreach my $rule (@$rules) {
1160 next if $rule->{type} ne 'out';
1161 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" });
1164 # implement output policy
1165 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1166 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1168 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
1169 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
1172 sub generate_group_rules {
1173 my ($ruleset, $groups_conf, $group) = @_;
1175 die "no such security group
'$group'\n" if !$groups_conf->{$group};
1177 my $rules = $groups_conf->{rules}->{$group};
1179 my $chain = "GROUP-
${group
}-IN
";
1181 ruleset_create_chain($ruleset, $chain);
1183 foreach my $rule (@$rules) {
1184 next if $rule->{type} ne 'in';
1185 ruleset_generate_rule($ruleset, $chain, $rule, { REJECT => "PVEFW-reject
" });
1188 $chain = "GROUP-
${group
}-OUT
";
1190 ruleset_create_chain($ruleset, $chain);
1191 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1193 foreach my $rule (@$rules) {
1194 next if $rule->{type} ne 'out';
1195 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1196 # check also other tap rules later
1197 ruleset_generate_rule($ruleset, $chain, $rule,
1198 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" });
1203 my $valid_netdev_names = {};
1204 for (my $i = 0; $i < $MAX_NETS; $i++) {
1205 $valid_netdev_names->{"net
$i"} = 1;
1209 my ($line, $need_iface, $allow_groups) = @_;
1211 my $macros = get_firewall_macros();
1212 my $protocols = get_etc_protocols();
1214 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1216 # we can add single line comments to the end of the rule
1217 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
1219 # we can disable a rule when prefixed with '|'
1220 my $disable = 1 if $line =~ s/^\|//;
1222 my @data = split(/\s+/, $line);
1223 my $expected_elements = $need_iface ? 8 : 7;
1225 die "wrong number of rule elements
\n" if scalar(@data) > $expected_elements;
1228 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1230 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1233 die "incomplete rule
\n" if ! ($type && $action);
1240 if ($type eq 'in' || $type eq 'out') {
1241 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1243 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1244 ($macro_name, $action) = ($1, $2);
1245 my $lc_macro_name = lc($macro_name);
1246 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1247 $macro_name = $preferred_name if $preferred_name;
1248 $macro = $macros->{$lc_macro_name};
1249 die "unknown
macro '$macro_name'\n" if !$macro;
1251 die "unknown action
'$action'\n";
1253 } elsif ($type eq 'group') {
1254 die "wrong number of rule elements
\n" if scalar(@data) != 3;
1255 die "groups disabled
\n" if !$allow_groups;
1257 die "invalid characters
in group name
\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1259 die "unknown rule type
'$type'\n";
1263 $iface = undef if $iface && $iface eq '-';
1266 $proto = undef if $proto && $proto eq '-';
1267 die "unknown protokol
'$proto'\n" if $proto &&
1268 !(defined($protocols->{byname}->{$proto}) ||
1269 defined($protocols->{byid}->{$proto}));
1271 $source = undef if $source && $source eq '-';
1272 $dest = undef if $dest && $dest eq '-';
1274 $dport = undef if $dport && $dport eq '-';
1275 $sport = undef if $sport && $sport eq '-';
1277 my $nbsource = undef;
1280 $nbsource = parse_address_list($source) if $source;
1281 $nbdest = parse_address_list($dest) if $dest;
1287 disable => $disable,
1288 comment => $comment,
1293 nbsource => $nbsource,
1301 foreach my $templ (@$macro) {
1303 my $param_used = {};
1304 foreach my $k (keys %$templ) {
1305 my $v = $templ->{$k};
1306 if ($v eq 'PARAM') {
1308 $param_used->{$k} = 1;
1309 } elsif ($v eq 'DEST') {
1310 $v = $param->{dest};
1311 $param_used->{dest} = 1;
1312 } elsif ($v eq 'SOURCE') {
1313 $v = $param->{source};
1314 $param_used->{source} = 1;
1317 die "missing parameter
'$k' in macro '$macro_name'\n" if !defined($v);
1320 foreach my $k (keys %$param) {
1321 next if !defined($param->{$k});
1322 next if $param_used->{$k};
1323 if (defined($rule->{$k})) {
1324 die "parameter
'$k' already define
in macro (value
= '$rule->{$k}')\n"
1325 if $rule->{$k} ne $param->{$k};
1327 $rule->{$k} = $param->{$k};
1330 push @$rules, $rule;
1333 push @$rules, $param;
1336 foreach my $rule (@$rules) {
1337 $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport})
1338 if defined($rule->{dport});
1339 $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport})
1340 if defined($rule->{sport});
1346 sub parse_vmfw_option {
1351 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
1353 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1356 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1358 $value = $2 ? lc($3) : '';
1359 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1364 die "can
't parse option '$line'\n"
1367 return ($opt, $value);
1370 sub parse_hostfw_option {
1375 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1377 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags|allow_bridge_route):\s*(0|1)\s*$/i) {
1380 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1382 $value = $2 ? lc($3) : '';
1383 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1386 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1391 die "can't parse option
'$line'\n"
1394 return ($opt, $value);
1397 sub parse_vm_fw_rules {
1398 my ($filename, $fh) = @_;
1400 my $res = { rules => [], options => {}};
1404 my $digest = Digest::SHA->new('sha1');
1406 while (defined(my $line = <$fh>)) {
1407 $digest->add($line);
1409 next if $line =~ m/^#/;
1410 next if $line =~ m/^\s*$/;
1412 my $linenr = $fh->input_line_number();
1413 my $prefix = "$filename (line
$linenr)";
1415 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1417 warn "$prefix: ignore unknown section
'$section'\n" if !$res->{$section};
1421 warn "$prefix: skip line
- no section
";
1425 next if !$res->{$section}; # skip undefined section
1427 if ($section eq 'options') {
1429 my ($opt, $value) = parse_vmfw_option($line);
1430 $res->{options}->{$opt} = $value;
1432 warn "$prefix: $@" if $@;
1437 eval { $rules = parse_fw_rule($line, 1, 1); };
1439 warn "$prefix: $err";
1443 push @{$res->{$section}}, @$rules;
1446 $res->{digest} = $digest->b64digest;
1451 sub parse_host_fw_rules {
1452 my ($filename, $fh) = @_;
1454 my $res = { rules => [], options => {}};
1458 my $digest = Digest::SHA->new('sha1');
1460 while (defined(my $line = <$fh>)) {
1461 $digest->add($line);
1463 next if $line =~ m/^#/;
1464 next if $line =~ m/^\s*$/;
1466 my $linenr = $fh->input_line_number();
1467 my $prefix = "$filename (line
$linenr)";
1469 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1471 warn "$prefix: ignore unknown section
'$section'\n" if !$res->{$section};
1475 warn "$prefix: skip line
- no section
";
1479 next if !$res->{$section}; # skip undefined section
1481 if ($section eq 'options') {
1483 my ($opt, $value) = parse_hostfw_option($line);
1484 $res->{options}->{$opt} = $value;
1486 warn "$prefix: $@" if $@;
1491 eval { $rules = parse_fw_rule($line, 1, 1); };
1493 warn "$prefix: $err";
1497 push @{$res->{$section}}, @$rules;
1500 $res->{digest} = $digest->b64digest;
1505 sub parse_group_fw_rules {
1506 my ($filename, $fh) = @_;
1511 my $res = { rules => {} };
1513 my $digest = Digest::SHA->new('sha1');
1515 while (defined(my $line = <$fh>)) {
1516 $digest->add($line);
1518 next if $line =~ m/^#/;
1519 next if $line =~ m/^\s*$/;
1521 my $linenr = $fh->input_line_number();
1522 my $prefix = "$filename (line
$linenr)";
1524 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1529 if (!$section || !$group) {
1530 warn "$prefix: skip line
- no section
";
1535 eval { $rules = parse_fw_rule($line, 0, 0); };
1537 warn "$prefix: $err";
1541 push @{$res->{$section}->{$group}}, @$rules;
1544 $res->{digest} = $digest->b64digest;
1550 my ($code, @param) = @_;
1554 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1561 sub read_local_vm_config {
1566 my $vmdata = { openvz => $openvz, qemu => $qemu };
1568 my $vmlist = PVE::Cluster::get_vmlist();
1569 return $vmdata if !$vmlist || !$vmlist->{ids};
1570 my $ids = $vmlist->{ids};
1572 foreach my $vmid (keys %$ids) {
1573 next if !$vmid; # skip VE0
1574 my $d = $ids->{$vmid};
1575 next if !$d->{node} || $d->{node} ne $nodename;
1576 next if !$d->{type};
1577 if ($d->{type} eq 'openvz') {
1578 if ($have_pve_manager) {
1579 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
1580 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1581 $openvz->{$vmid} = $conf;
1584 } elsif ($d->{type} eq 'qemu') {
1585 if ($have_qemu_server) {
1586 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1587 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1588 $qemu->{$vmid} = $conf;
1597 sub load_vmfw_conf {
1602 my $filename = "/etc/pve
/firewall/$vmid.fw
";
1603 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1604 $vmfw_conf = parse_vm_fw_rules($filename, $fh);
1610 sub read_vm_firewall_configs {
1612 my $vmfw_configs = {};
1614 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1615 my $vmfw_conf = load_vmfw_conf($vmid);
1616 next if !$vmfw_conf->{options}; # skip if file does not exists
1617 $vmfw_configs->{$vmid} = $vmfw_conf;
1620 return $vmfw_configs;
1623 sub get_option_log_level {
1624 my ($options, $k) = @_;
1626 my $v = $options->{$k};
1627 $v = $default_log_level if !defined($v);
1629 return undef if $v eq '' || $v eq 'nolog';
1631 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1633 return $v if ($v >= 0) && ($v <= 7);
1635 warn "unknown
log level
($k = '$v')\n";
1640 sub generate_std_chains {
1641 my ($ruleset, $options) = @_;
1643 my $loglevel = get_option_log_level($options, 'smurf_log_level');
1645 # same as shorewall smurflog.
1646 my $chain = 'PVEFW-smurflog';
1648 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP
: ", $loglevel) if $loglevel;
1649 push @{$pve_std_chains->{$chain}}, "-j DROP
";
1651 # same as shorewall logflags action.
1652 $loglevel = get_option_log_level($options, 'tcp_flags_log_level');
1653 $chain = 'PVEFW-logflags';
1654 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
1655 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP
: ", $loglevel) if $loglevel;
1656 push @{$pve_std_chains->{$chain}}, "-j DROP
";
1658 foreach my $chain (keys %$pve_std_chains) {
1659 ruleset_create_chain($ruleset, $chain);
1660 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1662 ruleset_generate_rule($ruleset, $chain, $rule);
1664 ruleset_addrule($ruleset, $chain, $rule);
1670 sub save_pvefw_status {
1673 die "unknown status
'$status' - internal error
"
1674 if $status !~ m/^(stopped|active)$/;
1676 mkdir dirname($pve_fw_status_filename);
1677 PVE::Tools::file_set_contents($pve_fw_status_filename, $status);
1680 sub read_pvefw_status {
1682 my $status = 'unknown';
1684 return 'stopped' if ! -f $pve_fw_status_filename;
1687 $status = PVE::Tools::file_get_contents($pve_fw_status_filename);
1694 # fixme: move to pve-common PVE::ProcFSTools
1695 sub read_proc_net_route {
1696 my $filename = "/proc/net
/route
";
1700 my $fh = IO::File->new ($filename, "r
");
1701 return $res if !$fh;
1703 my $int_to_quad = sub {
1704 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
1707 while (defined(my $line = <$fh>)) {
1708 next if $line =~/^Iface\s+Destination/; # skip head
1709 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
1710 push @{$res->{$iface}}, {
1711 dest => &$int_to_quad(hex($dest)),
1712 gateway => &$int_to_quad(hex($gateway)),
1713 mask => &$int_to_quad(hex($mask)),
1722 sub load_security_groups {
1724 my $groups_conf = {};
1725 my $filename = "/etc/pve
/firewall/groups
.fw
";
1726 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1727 $groups_conf = parse_group_fw_rules($filename, $fh);
1730 return $groups_conf;
1733 sub load_hostfw_conf {
1735 my $hostfw_conf = {};
1736 my $filename = "/etc/pve
/local/host
.fw
";
1737 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1738 $hostfw_conf = parse_host_fw_rules($filename, $fh);
1740 return $hostfw_conf;
1744 my $vmdata = read_local_vm_config();
1745 my $vmfw_configs = read_vm_firewall_configs($vmdata);
1747 my $routing_table = read_proc_net_route();
1749 my $groups_conf = load_security_groups();
1753 ruleset_create_chain($ruleset, "PVEFW-INPUT
");
1754 ruleset_create_chain($ruleset, "PVEFW-OUTPUT
");
1756 ruleset_create_chain($ruleset, "PVEFW-FORWARD
");
1758 my $hostfw_conf = load_hostfw_conf();
1759 my $hostfw_options = $hostfw_conf->{options} || {};
1761 generate_std_chains($ruleset, $hostfw_options);
1763 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
1765 enable_host_firewall($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
1767 # generate firewall rules for QEMU VMs
1768 foreach my $vmid (keys %{$vmdata->{qemu}}) {
1769 my $conf = $vmdata->{qemu}->{$vmid};
1770 my $vmfw_conf = $vmfw_configs->{$vmid};
1771 next if !$vmfw_conf;
1772 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
1774 foreach my $netid (keys %$conf) {
1775 next if $netid !~ m/^net(\d+)$/;
1776 my $net = PVE::QemuServer::parse_net($conf->{$netid});
1778 my $iface = "tap
${vmid
}i
$1";
1780 my $bridge = $net->{bridge};
1781 next if !$bridge; # fixme: ?
1783 $bridge .= "v
$net->{tag
}" if $net->{tag};
1785 generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table);
1787 my $macaddr = $net->{macaddr};
1788 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr,
1789 $vmfw_conf, $vmid, $bridge, 'IN');
1790 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr,
1791 $vmfw_conf, $vmid, $bridge, 'OUT');
1795 # generate firewall rules for OpenVZ containers
1796 foreach my $vmid (keys %{$vmdata->{openvz}}) {
1797 my $conf = $vmdata->{openvz}->{$vmid};
1799 my $vmfw_conf = $vmfw_configs->{$vmid};
1800 next if !$vmfw_conf;
1801 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
1803 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
1804 my $ip = $conf->{ip_address}->{value};
1805 generate_venet_rules_direction($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'IN');
1806 generate_venet_rules_direction($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'OUT');
1809 if ($conf->{netif} && $conf->{netif}->{value}) {
1810 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
1811 foreach my $netid (keys %$netif) {
1812 my $d = $netif->{$netid};
1813 my $bridge = $d->{bridge};
1815 warn "no bridge device
for CT
$vmid iface
'$netid'\n";
1819 generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table);
1821 my $macaddr = $d->{mac};
1822 my $iface = $d->{host_ifname};
1823 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr,
1824 $vmfw_conf, $vmid, $bridge, 'IN');
1825 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr,
1826 $vmfw_conf, $vmid, $bridge, 'OUT');
1831 # fixme: what log level should we use here?
1832 my $loglevel = get_option_log_level($hostfw_options, "log_level_out
");
1834 # fixme: should we really block inter-bridge traffic?
1836 # always allow traffic from containers?
1837 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i venet0
-j RETURN
");
1839 # disable interbridge routing
1840 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o vmbr
+ -j PVEFW-Drop
");
1841 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i vmbr
+ -j PVEFW-Drop
");
1842 ruleset_addlog($ruleset, "PVEFW-FORWARD
", 0, "DROP
: ", $loglevel, "-o vmbr
+");
1843 ruleset_addlog($ruleset, "PVEFW-FORWARD
", 0, "DROP
: ", $loglevel, "-i vmbr
+");
1844 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o vmbr
+ -j DROP
");
1845 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i vmbr
+ -j DROP
");
1847 return wantarray ? ($ruleset, $hostfw_conf) : $ruleset;
1850 sub get_ruleset_status {
1851 my ($ruleset, $verbose) = @_;
1853 my $active_chains = iptables_get_chains();
1855 my $statushash = {};
1857 foreach my $chain (sort keys %$ruleset) {
1858 my $digest = Digest::SHA->new('sha1');
1859 foreach my $cmd (@{$ruleset->{$chain}}) {
1860 $digest->add("$cmd\n");
1862 my $sig = $digest->b64digest;
1863 $statushash->{$chain}->{sig} = $sig;
1865 my $oldsig = $active_chains->{$chain};
1866 if (!defined($oldsig)) {
1867 $statushash->{$chain}->{action} = 'create';
1869 if ($oldsig eq $sig) {
1870 $statushash->{$chain}->{action} = 'exists';
1872 $statushash->{$chain}->{action} = 'update';
1875 print "$statushash->{$chain}->{action
} $chain ($sig)\n" if $verbose;
1876 foreach my $cmd (@{$ruleset->{$chain}}) {
1877 print "\t$cmd\n" if $verbose;
1881 foreach my $chain (sort keys %$active_chains) {
1882 if (!defined($ruleset->{$chain})) {
1883 my $sig = $active_chains->{$chain};
1884 $statushash->{$chain}->{action} = 'delete';
1885 $statushash->{$chain}->{sig} = $sig;
1886 print "delete $chain ($sig)\n" if $verbose;
1896 get_ruleset_status($ruleset, 1);
1899 sub print_sig_rule {
1900 my ($chain, $sig) = @_;
1902 # We just use this to store a SHA1 checksum used to detect changes
1903 return "-A
$chain -m comment
--comment
\"PVESIG
:$sig\"\n";
1906 sub get_rulset_cmdlist
{
1907 my ($ruleset, $verbose) = @_;
1909 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
1911 my $statushash = get_ruleset_status
($ruleset, $verbose);
1913 # create missing chains first
1914 foreach my $chain (sort keys %$ruleset) {
1915 my $stat = $statushash->{$chain};
1916 die "internal error" if !$stat;
1917 next if $stat->{action
} ne 'create';
1919 $cmdlist .= ":$chain - [0:0]\n";
1922 my $rule = "INPUT -j PVEFW-INPUT";
1923 if (!PVE
::Firewall
::iptables_rule_exist
($rule)) {
1924 $cmdlist .= "-A $rule\n";
1926 $rule = "OUTPUT -j PVEFW-OUTPUT";
1927 if (!PVE
::Firewall
::iptables_rule_exist
($rule)) {
1928 $cmdlist .= "-A $rule\n";
1931 $rule = "FORWARD -j PVEFW-FORWARD";
1932 if (!PVE
::Firewall
::iptables_rule_exist
($rule)) {
1933 $cmdlist .= "-A $rule\n";
1936 foreach my $chain (sort keys %$ruleset) {
1937 my $stat = $statushash->{$chain};
1938 die "internal error" if !$stat;
1940 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
1941 $cmdlist .= "-F $chain\n";
1942 foreach my $cmd (@{$ruleset->{$chain}}) {
1943 $cmdlist .= "$cmd\n";
1945 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
1946 } elsif ($stat->{action
} eq 'delete') {
1947 die "internal error"; # this should not happen
1948 } elsif ($stat->{action
} eq 'exists') {
1951 die "internal error - unknown status '$stat->{action}'";
1955 foreach my $chain (keys %$statushash) {
1956 next if $statushash->{$chain}->{action
} ne 'delete';
1957 $cmdlist .= "-F $chain\n";
1959 foreach my $chain (keys %$statushash) {
1960 next if $statushash->{$chain}->{action
} ne 'delete';
1961 next if $chain eq 'PVEFW-INPUT';
1962 next if $chain eq 'PVEFW-OUTPUT';
1963 next if $chain eq 'PVEFW-FORWARD';
1964 $cmdlist .= "-X $chain\n";
1967 $cmdlist .= "COMMIT\n";
1973 my ($ruleset, $hostfw_conf, $verbose) = @_;
1975 enable_bridge_firewall
();
1977 update_nf_conntrack_max
($hostfw_conf);
1979 my $cmdlist = get_rulset_cmdlist
($ruleset, $verbose);
1981 print $cmdlist if $verbose;
1983 iptables_restore_cmdlist
($cmdlist);
1985 # test: re-read status and check if everything is up to date
1986 my $statushash = get_ruleset_status
($ruleset);
1989 foreach my $chain (sort keys %$ruleset) {
1990 my $stat = $statushash->{$chain};
1991 if ($stat->{action
} ne 'exists') {
1992 warn "unable to update chain '$chain'\n";
1997 die "unable to apply firewall changes\n" if $errors;
2000 sub update_nf_conntrack_max
{
2001 my ($hostfw_conf) = @_;
2003 my $max = 65536; # reasonable default
2005 my $options = $hostfw_conf->{options
} || {};
2007 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
2008 $max = $options->{nf_conntrack_max
};
2009 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
2012 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
2013 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
2015 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
2017 if ($current != $max) {
2018 my $hashsize = int($max/4);
2019 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
2020 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
2025 my ($start, $verbose) = @_;
2028 my $status = read_pvefw_status
();
2030 my ($ruleset, $hostfw_conf) = PVE
::Firewall
::compile
();
2032 if ($start || $status eq 'active') {
2034 save_pvefw_status
('active') if ($status ne 'active');
2036 apply_ruleset
($ruleset, $hostfw_conf, $verbose);
2038 print "Firewall not active (status = $status)\n" if $verbose;