12 use PVE
::OpenVZ
; # dependeny problem?!
17 use PVE
::Tools
qw(run_command lock_file);
21 my $nodename = PVE
::INotify
::nodename
();
23 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
24 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
26 my $default_log_level = 'info';
28 my $log_level_hash = {
39 # imported/converted from: /usr/share/shorewall/macro.*
42 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
43 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
46 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
49 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
52 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
53 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
56 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
57 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
60 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
63 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
64 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
65 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
68 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
69 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
72 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
75 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
76 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
79 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
80 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
83 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
86 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4662' },
87 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4665' },
90 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
93 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
96 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
97 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
98 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
99 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
102 { action
=> 'PARAM', proto
=> '47' },
103 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
106 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
109 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6346' },
110 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6346' },
113 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
116 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
119 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
122 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
125 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
128 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
131 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
134 { action
=> 'PARAM', proto
=> '94' },
135 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
138 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '631' },
141 { action
=> 'PARAM', proto
=> 'udp', dport
=> '631' },
144 { action
=> 'PARAM', source
=> 'SOURCE', dest
=> 'DEST', proto
=> 'tcp', dport
=> '631' },
145 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '631' },
148 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
149 { action
=> 'PARAM', proto
=> '50' },
150 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
151 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
154 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
155 { action
=> 'PARAM', proto
=> '51' },
156 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
157 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
160 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
161 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
162 { action
=> 'PARAM', proto
=> '50' },
163 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
164 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
165 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
168 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
171 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5222' },
174 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5223' },
177 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5269' },
180 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
183 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
184 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
187 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
190 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
193 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
196 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
199 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
200 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
201 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
204 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
207 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
210 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
213 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
216 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
219 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
220 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
223 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
224 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '123' },
227 { action
=> 'PARAM', proto
=> '89' },
230 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
233 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
234 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
237 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
240 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
243 { action
=> 'PARAM', proto
=> '47' },
244 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
245 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
248 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
251 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
254 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
257 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
260 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
261 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
264 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
267 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
270 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
273 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
276 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
279 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
280 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
281 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
282 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
285 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
286 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
287 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
288 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
289 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
290 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
291 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
292 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
295 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
298 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
301 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
304 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
308 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
311 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
314 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
317 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
318 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
319 { action
=> 'PARAM', proto
=> '41' },
320 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
323 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
326 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
329 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
330 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
333 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
336 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
339 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
342 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
345 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
346 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
349 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
352 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
355 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
356 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
359 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
362 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
365 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
369 my $pve_fw_parsed_macros;
370 my $pve_fw_preferred_macro_names = {};
372 my $pve_std_chains = {
373 'PVEFW-SET-ACCEPT-MARK' => [
374 "-j MARK --set-mark 1",
376 'PVEFW-DropBroadcast' => [
377 # same as shorewall 'Broadcast'
378 # simply DROP BROADCAST/MULTICAST/ANYCAST
379 # we can use this to reduce logging
380 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
381 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
382 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
383 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
386 # same as shorewall 'reject'
387 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
388 { action
=> 'DROP', source
=> '224.0.0.0/4' },
389 { action
=> 'DROP', proto
=> 'icmp' },
390 "-p tcp -j REJECT --reject-with tcp-reset",
391 "-p udp -j REJECT --reject-with icmp-port-unreachable",
392 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
393 "-j REJECT --reject-with icmp-host-prohibited",
396 # same as shorewall 'Drop', which is equal to DROP,
397 # but REJECT/DROP some packages to reduce logging,
398 # and ACCEPT critical ICMP types
399 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
400 # we are not interested in BROADCAST/MULTICAST/ANYCAST
401 { action
=> 'PVEFW-DropBroadcast' },
402 # ACCEPT critical ICMP types
403 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
404 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
405 # Drop packets with INVALID state
406 "-m conntrack --ctstate INVALID -j DROP",
407 # Drop Microsoft SMB noise
408 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
409 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
410 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
411 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
412 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
413 # Drop new/NotSyn traffic so that it doesn't get logged
414 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
416 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
419 # same as shorewall 'Reject', which is equal to Reject,
420 # but REJECT/DROP some packages to reduce logging,
421 # and ACCEPT critical ICMP types
422 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
423 # we are not interested in BROADCAST/MULTICAST/ANYCAST
424 { action
=> 'PVEFW-DropBroadcast' },
425 # ACCEPT critical ICMP types
426 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
427 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
428 # Drop packets with INVALID state
429 "-m conntrack --ctstate INVALID -j DROP",
430 # Drop Microsoft SMB noise
431 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
432 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
433 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
434 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
435 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
436 # Drop new/NotSyn traffic so that it doesn't get logged
437 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
439 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
441 'PVEFW-tcpflags' => [
442 # same as shorewall tcpflags action.
443 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
444 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
445 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
446 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
447 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
448 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
451 # same as shorewall smurfs action
452 # Filter packets for smurfs (packets with a broadcast address as the source).
453 "-s 0.0.0.0/32 -j RETURN",
454 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
455 "-s 224.0.0.0/4 -g PVEFW-smurflog",
459 # iptables -p icmp -h
460 my $icmp_type_names = {
463 'destination-unreachable' => 1,
464 'network-unreachable' => 1,
465 'host-unreachable' => 1,
466 'protocol-unreachable' => 1,
467 'port-unreachable' => 1,
468 'fragmentation-needed' => 1,
469 'source-route-failed' => 1,
470 'network-unknown' => 1,
472 'network-prohibited' => 1,
473 'host-prohibited' => 1,
474 'TOS-network-unreachable' => 1,
475 'TOS-host-unreachable' => 1,
476 'communication-prohibited' => 1,
477 'host-precedence-violation' => 1,
478 'precedence-cutoff' => 1,
479 'source-quench' => 1,
481 'network-redirect' => 1,
482 'host-redirect' => 1,
483 'TOS-network-redirect' => 1,
484 'TOS-host-redirect' => 1,
486 'router-advertisement' => 1,
487 'router-solicitation' => 1,
488 'time-exceeded' => 1,
489 'ttl-zero-during-transit' => 1,
490 'ttl-zero-during-reassembly' => 1,
491 'parameter-problem' => 1,
492 'ip-header-bad' => 1,
493 'required-option-missing' => 1,
494 'timestamp-request' => 1,
495 'timestamp-reply' => 1,
496 'address-mask-request' => 1,
497 'address-mask-reply' => 1,
500 sub get_firewall_macros
{
502 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
504 $pve_fw_parsed_macros = {};
506 foreach my $k (keys %$pve_fw_macros) {
509 my $macro = $pve_fw_macros->{$k};
510 $pve_fw_preferred_macro_names->{$name} = $k;
511 $pve_fw_parsed_macros->{$name} = $macro;
514 return $pve_fw_parsed_macros;
519 sub get_etc_services
{
521 return $etc_services if $etc_services;
523 my $filename = "/etc/services";
525 my $fh = IO
::File-
>new($filename, O_RDONLY
);
527 warn "unable to read '$filename' - $!\n";
533 while (my $line = <$fh>) {
535 next if $line =~m/^#/;
536 next if ($line =~m/^\s*$/);
538 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
539 $services->{byid
}->{$2}->{name
} = $1;
540 $services->{byid
}->{$2}->{port
} = $2;
541 $services->{byid
}->{$2}->{$3} = 1;
542 $services->{byname
}->{$1} = $services->{byid
}->{$2};
548 $etc_services = $services;
551 return $etc_services;
556 sub get_etc_protocols
{
557 return $etc_protocols if $etc_protocols;
559 my $filename = "/etc/protocols";
561 my $fh = IO
::File-
>new($filename, O_RDONLY
);
563 warn "unable to read '$filename' - $!\n";
569 while (my $line = <$fh>) {
571 next if $line =~m/^#/;
572 next if ($line =~m/^\s*$/);
574 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
575 $protocols->{byid
}->{$2}->{name
} = $1;
576 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
582 $etc_protocols = $protocols;
584 return $etc_protocols;
587 sub parse_address_list
{
591 foreach my $aor (split(/,/, $str)) {
592 if (!Net
::IP-
>new($aor)) {
593 my $err = Net
::IP
::Error
();
594 die "invalid IP address: $err\n";
602 sub parse_port_name_number_or_range
{
605 my $services = PVE
::Firewall
::get_etc_services
();
607 foreach my $item (split(/,/, $str)) {
611 foreach my $pon (split(':', $item, 2)) {
612 $pon = $services->{byname
}->{$pon}->{port
} if $services->{byname
}->{$pon}->{port
};
613 if ($pon =~ m/^\d+$/){
614 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
615 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
618 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname
}->{$pon};
626 my $bridge_firewall_enabled = 0;
628 sub enable_bridge_firewall
{
630 return if $bridge_firewall_enabled; # only once
632 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
633 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
635 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
636 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
638 $bridge_firewall_enabled = 1;
641 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
646 run_command
("/sbin/iptables $cmd", outfunc
=> sub {}, errfunc
=> sub {});
649 sub iptables_restore_cmdlist
{
652 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
655 sub iptables_get_chains
{
659 # check what chains we want to track
660 my $is_pvefw_chain = sub {
663 return 1 if $name =~ m/^PVEFW-\S+$/;
665 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
667 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
669 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
671 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
672 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
682 return if $line =~ m/^#/;
683 return if $line =~ m/^\s*$/;
685 if ($line =~ m/^\*(\S+)$/) {
690 return if $table ne 'filter';
692 if ($line =~ m/^:(\S+)\s/) {
694 return if !&$is_pvefw_chain($chain);
695 $res->{$chain} = "unknown";
696 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
697 my ($chain, $sig) = ($1, $2);
698 return if !&$is_pvefw_chain($chain);
699 $res->{$chain} = $sig;
701 # simply ignore the rest
706 run_command
("/sbin/iptables-save", outfunc
=> $parser);
711 sub iptables_chain_exist
{
715 iptables
("-n --list $chain");
722 sub iptables_rule_exist
{
726 iptables
("-C $rule");
733 sub ruleset_generate_cmdstr
{
734 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
736 return if $rule->{disable
};
740 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
741 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
743 push @cmd, "-m iprange --src-range" if $rule->{nbsource
} && $rule->{nbsource
} > 1;
744 push @cmd, "-s $rule->{source}" if $rule->{source
};
745 push @cmd, "-m iprange --dst-range" if $rule->{nbdest
} && $rule->{nbdest
} > 1;
746 push @cmd, "-d $rule->{dest}" if $rule->{dest
};
748 if ($rule->{proto
}) {
749 push @cmd, "-p $rule->{proto}";
752 $multiport++ if $rule->{nbdport
} && ($rule->{nbdport
} > 1);
753 $multiport++ if $rule->{nbsport
} && ($rule->{nbsport
} > 1);
755 push @cmd, "--match multiport" if $multiport;
757 die "multiport: option '--sports' cannot be used together with '--dports'\n"
758 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
760 if ($rule->{dport
}) {
761 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
762 # Note: we use dport to store --icmp-type
763 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
764 push @cmd, "-m icmp --icmp-type $rule->{dport}";
766 if ($rule->{nbdport
} && $rule->{nbdport
} > 1) {
767 if ($multiport == 2) {
768 push @cmd, "--ports $rule->{dport}";
770 push @cmd, "--dports $rule->{dport}";
773 push @cmd, "--dport $rule->{dport}";
778 if ($rule->{sport
}) {
779 if ($rule->{nbsport
} && $rule->{nbsport
} > 1) {
780 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
782 push @cmd, "--sport $rule->{sport}";
785 } elsif ($rule->{dport
} || $rule->{sport
}) {
786 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
787 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
790 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
792 if (my $action = $rule->{action
}) {
793 $action = $actions->{$action} if defined($actions->{$action});
794 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
795 push @cmd, $goto ?
"-g $action" : "-j $action";
798 return scalar(@cmd) ?
join(' ', @cmd) : undef;
801 sub ruleset_generate_rule
{
802 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
804 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
805 ruleset_addrule
($ruleset, $chain, $cmdstr);
808 sub ruleset_generate_rule_insert
{
809 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
811 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
812 ruleset_insertrule
($ruleset, $chain, $cmdstr);
816 sub ruleset_create_chain
{
817 my ($ruleset, $chain) = @_;
819 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
821 die "chain '$chain' already exists\n" if $ruleset->{$chain};
823 $ruleset->{$chain} = [];
826 sub ruleset_chain_exist
{
827 my ($ruleset, $chain) = @_;
829 return $ruleset->{$chain} ?
1 : undef;
832 sub ruleset_addrule
{
833 my ($ruleset, $chain, $rule) = @_;
835 die "no such chain '$chain'\n" if !$ruleset->{$chain};
837 push @{$ruleset->{$chain}}, "-A $chain $rule";
840 sub ruleset_insertrule
{
841 my ($ruleset, $chain, $rule) = @_;
843 die "no such chain '$chain'\n" if !$ruleset->{$chain};
845 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
848 sub generate_bridge_chains
{
849 my ($ruleset, $hostfw_conf, $bridge) = @_;
851 if (!ruleset_chain_exist
($ruleset, "$bridge-FW")) {
852 ruleset_create_chain
($ruleset, "$bridge-FW");
853 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
854 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
857 if (!ruleset_chain_exist
($ruleset, "$bridge-OUT")) {
858 ruleset_create_chain
($ruleset, "$bridge-OUT");
859 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
860 ruleset_insertrule
($ruleset, "PVEFW-INPUT", "-i $bridge -m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
863 if (!ruleset_chain_exist
($ruleset, "$bridge-IN")) {
864 ruleset_create_chain
($ruleset, "$bridge-IN");
865 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
866 ruleset_addrule
($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
867 # accept traffic to unmanaged bridge ports
868 ruleset_addrule
($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j ACCEPT ");
872 sub ruleset_add_chain_policy
{
873 my ($ruleset, $chain, $policy, $loglevel, $accept_action) = @_;
875 if ($policy eq 'ACCEPT') {
877 ruleset_generate_rule
($ruleset, $chain, { action
=> 'ACCEPT' },
878 { ACCEPT
=> $accept_action});
880 } elsif ($policy eq 'DROP') {
882 ruleset_addrule
($ruleset, $chain, "-j PVEFW-Drop");
884 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"$chain-dropped: \" --log-level $loglevel")
885 if defined($loglevel);
887 ruleset_addrule
($ruleset, $chain, "-j DROP");
888 } elsif ($policy eq 'REJECT') {
889 ruleset_addrule
($ruleset, $chain, "-j PVEFW-Reject");
891 ruleset_addrule
($ruleset, $chain, "-j LOG --log-prefix \"$chain-reject: \" --log-level $loglevel")
892 if defined($loglevel);
894 ruleset_addrule
($ruleset, $chain, "-g PVEFW-reject");
897 die "internal error: unknown policy '$policy'";
901 sub ruleset_create_vm_chain
{
902 my ($ruleset, $chain, $options, $macaddr, $direction) = @_;
904 ruleset_create_chain
($ruleset, $chain);
906 if (!(defined($options->{nosmurfs
}) && $options->{nosmurfs
} == 0)) {
907 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs");
910 if (!(defined($options->{dhcp
}) && $options->{dhcp
} == 0)) {
911 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 67:68 -j ACCEPT");
914 if ($options->{tcpflags
}) {
915 ruleset_addrule
($ruleset, $chain, "-p tcp -j PVEFW-tcpflags");
918 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
919 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
921 if ($direction eq 'OUT') {
922 if (defined($macaddr) && !(defined($options->{macfilter
}) && $options->{macfilter
} == 0)) {
923 ruleset_addrule
($ruleset, $chain, "-m mac ! --mac-source $macaddr -j DROP");
925 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
929 sub ruleset_generate_vm_rules
{
930 my ($ruleset, $rules, $groups_conf, $chain, $netid, $direction) = @_;
932 my $lc_direction = lc($direction);
934 foreach my $rule (@$rules) {
935 next if $rule->{iface
} && $rule->{iface
} ne $netid;
936 next if $rule->{disable
};
937 if ($rule->{type
} eq 'group') {
938 my $group_chain = "GROUP-$rule->{action}-$direction";
939 if(!ruleset_chain_exist
($ruleset, $group_chain)){
940 generate_group_rules
($ruleset, $groups_conf, $rule->{action
});
942 ruleset_addrule
($ruleset, $chain, "-j $group_chain");
943 ruleset_addrule
($ruleset, $chain, "-m mark --mark 1 -j RETURN")
944 if $direction eq 'OUT';
946 next if $rule->{type
} ne $lc_direction;
947 if ($direction eq 'OUT') {
948 ruleset_generate_rule
($ruleset, $chain, $rule,
949 { ACCEPT
=> "PVEFW-SET-ACCEPT-MARK", REJECT
=> "PVEFW-reject" });
951 ruleset_generate_rule
($ruleset, $chain, $rule, { REJECT
=> "PVEFW-reject" });
957 sub generate_venet_rules_direction
{
958 my ($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
960 parse_address_list
($ip); # make sure we have a valid $ip list
962 my $lc_direction = lc($direction);
964 my $rules = $vmfw_conf->{rules
};
966 my $options = $vmfw_conf->{options
};
967 my $loglevel = get_option_log_level
($options, "log_level_${lc_direction}");
969 my $chain = "venet0-$vmid-$direction";
971 ruleset_create_vm_chain
($ruleset, $chain, $options, undef, $direction);
973 ruleset_generate_vm_rules
($ruleset, $rules, $groups_conf, $chain, 'venet', $direction);
978 if ($direction eq 'OUT') {
979 $policy = $options->{policy_out
} || 'ACCEPT'; # allow everything by default
981 $policy = $options->{policy_in
} || 'DROP'; # allow nothing by default
984 my $accept_action = $direction eq 'OUT' ?
"PVEFW-SET-ACCEPT-MARK" : "ACCEPT";
985 ruleset_add_chain_policy
($ruleset, $chain, $policy, $loglevel, $accept_action);
987 # plug into FORWARD, INPUT and OUTPUT chain
988 if ($direction eq 'OUT') {
989 ruleset_generate_rule_insert
($ruleset, "PVEFW-FORWARD", {
992 iface_in
=> 'venet0'});
994 ruleset_generate_rule_insert
($ruleset, "PVEFW-INPUT", {
997 iface_in
=> 'venet0'});
999 ruleset_generate_rule
($ruleset, "PVEFW-FORWARD", {
1002 iface_out
=> 'venet0'});
1004 ruleset_generate_rule
($ruleset, "PVEFW-OUTPUT", {
1007 iface_out
=> 'venet0'});
1011 sub generate_tap_rules_direction
{
1012 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
1014 my $lc_direction = lc($direction);
1016 my $rules = $vmfw_conf->{rules
};
1018 my $options = $vmfw_conf->{options
};
1019 my $loglevel = get_option_log_level
($options, "log_level_${lc_direction}");
1021 my $tapchain = "$iface-$direction";
1023 ruleset_create_vm_chain
($ruleset, $tapchain, $options, $macaddr, $direction);
1025 ruleset_generate_vm_rules
($ruleset, $rules, $groups_conf, $tapchain, $netid, $direction);
1030 if ($direction eq 'OUT') {
1031 $policy = $options->{policy_out
} || 'ACCEPT'; # allow everything by default
1033 $policy = $options->{policy_in
} || 'DROP'; # allow nothing by default
1036 my $accept_action = $direction eq 'OUT' ?
"PVEFW-SET-ACCEPT-MARK" : "ACCEPT";
1037 ruleset_add_chain_policy
($ruleset, $tapchain, $policy, $loglevel, $accept_action);
1039 # plug the tap chain to bridge chain
1040 my $physdevdirection = $direction eq 'IN' ?
"out" : "in";
1041 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
1042 ruleset_insertrule
($ruleset, "$bridge-$direction", $rule);
1045 sub enable_host_firewall
{
1046 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
1048 # fixme: allow security groups
1050 my $options = $hostfw_conf->{options
};
1051 my $rules = $hostfw_conf->{rules
};
1053 # host inbound firewall
1054 my $chain = "PVEFW-HOST-IN";
1055 ruleset_create_chain
($ruleset, $chain);
1057 my $loglevel = get_option_log_level
($options, "log_level_in");
1059 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
1060 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1061 ruleset_addrule
($ruleset, $chain, "-i lo -j ACCEPT");
1062 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
1063 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
1064 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
1066 # we use RETURN because we need to check also tap rules
1067 my $accept_action = 'RETURN';
1069 foreach my $rule (@$rules) {
1070 next if $rule->{type
} ne 'in';
1071 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> $accept_action, REJECT
=> "PVEFW-reject" });
1074 # implement input policy
1075 my $policy = $options->{policy_in
} || 'DROP'; # allow nothing by default
1076 ruleset_add_chain_policy
($ruleset, $chain, $policy, $loglevel, $accept_action);
1078 # host outbound firewall
1079 $chain = "PVEFW-HOST-OUT";
1080 ruleset_create_chain
($ruleset, $chain);
1082 $loglevel = get_option_log_level
($options, "log_level_out");
1084 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
1085 ruleset_addrule
($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1086 ruleset_addrule
($ruleset, $chain, "-o lo -j ACCEPT");
1087 ruleset_addrule
($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
1088 ruleset_addrule
($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
1089 ruleset_addrule
($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
1091 # we use RETURN because we may want to check other thigs later
1092 $accept_action = 'RETURN';
1094 foreach my $rule (@$rules) {
1095 next if $rule->{type
} ne 'out';
1096 ruleset_generate_rule
($ruleset, $chain, $rule, { ACCEPT
=> $accept_action, REJECT
=> "PVEFW-reject" });
1099 # implement output policy
1100 $policy = $options->{policy_out
} || 'ACCEPT'; # allow everything by default
1101 ruleset_add_chain_policy
($ruleset, $chain, $policy, $loglevel, $accept_action);
1103 ruleset_addrule
($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
1104 ruleset_addrule
($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
1107 sub generate_group_rules
{
1108 my ($ruleset, $groups_conf, $group) = @_;
1110 die "no such security group '$group'\n" if !$groups_conf->{$group};
1112 my $rules = $groups_conf->{$group}->{rules
};
1114 my $chain = "GROUP-${group}-IN";
1116 ruleset_create_chain
($ruleset, $chain);
1118 foreach my $rule (@$rules) {
1119 next if $rule->{type
} ne 'in';
1120 ruleset_generate_rule
($ruleset, $chain, $rule, { REJECT
=> "PVEFW-reject" });
1123 $chain = "GROUP-${group}-OUT";
1125 ruleset_create_chain
($ruleset, $chain);
1126 ruleset_addrule
($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
1128 foreach my $rule (@$rules) {
1129 next if $rule->{type
} ne 'out';
1130 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1131 # check also other tap rules later
1132 ruleset_generate_rule
($ruleset, $chain, $rule,
1133 { ACCEPT
=> 'PVEFW-SET-ACCEPT-MARK', REJECT
=> "PVEFW-reject" });
1138 my $valid_netdev_names = {};
1139 for (my $i = 0; $i < $MAX_NETS; $i++) {
1140 $valid_netdev_names->{"net$i"} = 1;
1144 my ($line, $need_iface, $allow_groups) = @_;
1146 my $macros = get_firewall_macros
();
1147 my $protocols = get_etc_protocols
();
1149 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1151 # we can add single line comments to the end of the rule
1152 my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
1154 # we can disable a rule when prefixed with '|'
1155 my $disable = 1 if $line =~ s/^\|//;
1157 my @data = split(/\s+/, $line);
1158 my $expected_elements = $need_iface ?
8 : 7;
1160 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
1163 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1165 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1168 die "incomplete rule\n" if ! ($type && $action);
1175 if ($type eq 'in' || $type eq 'out') {
1176 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1178 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1179 ($macro_name, $action) = ($1, $2);
1180 my $lc_macro_name = lc($macro_name);
1181 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1182 $macro_name = $preferred_name if $preferred_name;
1183 $macro = $macros->{$lc_macro_name};
1184 die "unknown macro '$macro_name'\n" if !$macro;
1186 die "unknown action '$action'\n";
1188 } elsif ($type eq 'group') {
1189 die "wrong number of rule elements\n" if scalar(@data) != 3;
1190 die "groups disabled\n" if !$allow_groups;
1192 die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1194 die "unknown rule type '$type'\n";
1198 $iface = undef if $iface && $iface eq '-';
1201 $proto = undef if $proto && $proto eq '-';
1202 die "unknown protokol '$proto'\n" if $proto &&
1203 !(defined($protocols->{byname
}->{$proto}) ||
1204 defined($protocols->{byid
}->{$proto}));
1206 $source = undef if $source && $source eq '-';
1207 $dest = undef if $dest && $dest eq '-';
1209 $dport = undef if $dport && $dport eq '-';
1210 $sport = undef if $sport && $sport eq '-';
1212 my $nbsource = undef;
1215 $nbsource = parse_address_list
($source) if $source;
1216 $nbdest = parse_address_list
($dest) if $dest;
1222 disable
=> $disable,
1223 comment
=> $comment,
1228 nbsource
=> $nbsource,
1236 foreach my $templ (@$macro) {
1238 my $param_used = {};
1239 foreach my $k (keys %$templ) {
1240 my $v = $templ->{$k};
1241 if ($v eq 'PARAM') {
1243 $param_used->{$k} = 1;
1244 } elsif ($v eq 'DEST') {
1245 $v = $param->{dest
};
1246 $param_used->{dest
} = 1;
1247 } elsif ($v eq 'SOURCE') {
1248 $v = $param->{source
};
1249 $param_used->{source
} = 1;
1252 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1255 foreach my $k (keys %$param) {
1256 next if !defined($param->{$k});
1257 next if $param_used->{$k};
1258 if (defined($rule->{$k})) {
1259 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1260 if $rule->{$k} ne $param->{$k};
1262 $rule->{$k} = $param->{$k};
1265 push @$rules, $rule;
1268 push @$rules, $param;
1271 foreach my $rule (@$rules) {
1272 $rule->{nbdport
} = parse_port_name_number_or_range
($rule->{dport
})
1273 if defined($rule->{dport
});
1274 $rule->{nbsport
} = parse_port_name_number_or_range
($rule->{sport
})
1275 if defined($rule->{sport
});
1281 sub parse_vmfw_option
{
1286 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1288 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1291 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1293 $value = $2 ?
lc($3) : '';
1294 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1299 die "can't parse option '$line'\n"
1302 return ($opt, $value);
1305 sub parse_hostfw_option
{
1310 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1312 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1315 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1317 $value = $2 ?
lc($3) : '';
1318 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1321 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1326 die "can't parse option '$line'\n"
1329 return ($opt, $value);
1332 sub parse_vm_fw_rules
{
1333 my ($filename, $fh) = @_;
1335 my $res = { rules
=> [], options
=> {}};
1339 while (defined(my $line = <$fh>)) {
1340 next if $line =~ m/^#/;
1341 next if $line =~ m/^\s*$/;
1343 my $linenr = $fh->input_line_number();
1344 my $prefix = "$filename (line $linenr)";
1346 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1348 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1352 warn "$prefix: skip line - no section";
1356 next if !$res->{$section}; # skip undefined section
1358 if ($section eq 'options') {
1360 my ($opt, $value) = parse_vmfw_option
($line);
1361 $res->{options
}->{$opt} = $value;
1363 warn "$prefix: $@" if $@;
1368 eval { $rules = parse_fw_rule
($line, 1, 1); };
1370 warn "$prefix: $err";
1374 push @{$res->{$section}}, @$rules;
1380 sub parse_host_fw_rules
{
1381 my ($filename, $fh) = @_;
1383 my $res = { rules
=> [], options
=> {}};
1387 while (defined(my $line = <$fh>)) {
1388 next if $line =~ m/^#/;
1389 next if $line =~ m/^\s*$/;
1391 my $linenr = $fh->input_line_number();
1392 my $prefix = "$filename (line $linenr)";
1394 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1396 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1400 warn "$prefix: skip line - no section";
1404 next if !$res->{$section}; # skip undefined section
1406 if ($section eq 'options') {
1408 my ($opt, $value) = parse_hostfw_option
($line);
1409 $res->{options
}->{$opt} = $value;
1411 warn "$prefix: $@" if $@;
1416 eval { $rules = parse_fw_rule
($line, 1, 1); };
1418 warn "$prefix: $err";
1422 push @{$res->{$section}}, @$rules;
1428 sub parse_group_fw_rules
{
1429 my ($filename, $fh) = @_;
1434 my $res = { rules
=> [] };
1436 while (defined(my $line = <$fh>)) {
1437 next if $line =~ m/^#/;
1438 next if $line =~ m/^\s*$/;
1440 my $linenr = $fh->input_line_number();
1441 my $prefix = "$filename (line $linenr)";
1443 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1448 if (!$section || !$group) {
1449 warn "$prefix: skip line - no section";
1454 eval { $rules = parse_fw_rule
($line, 0, 0); };
1456 warn "$prefix: $err";
1460 push @{$res->{$group}->{$section}}, @$rules;
1467 my ($code, @param) = @_;
1471 my $res = lock_file
($pve_fw_lock_filename, $timeout, $code, @param);
1478 sub read_local_vm_config
{
1483 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
1485 my $vmlist = PVE
::Cluster
::get_vmlist
();
1486 return $vmdata if !$vmlist || !$vmlist->{ids
};
1487 my $ids = $vmlist->{ids
};
1489 foreach my $vmid (keys %$ids) {
1490 next if !$vmid; # skip VE0
1491 my $d = $ids->{$vmid};
1492 next if !$d->{node
} || $d->{node
} ne $nodename;
1493 next if !$d->{type
};
1494 if ($d->{type
} eq 'openvz') {
1495 my $cfspath = PVE
::OpenVZ
::cfs_config_path
($vmid);
1496 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1497 $openvz->{$vmid} = $conf;
1499 } elsif ($d->{type
} eq 'qemu') {
1500 my $cfspath = PVE
::QemuServer
::cfs_config_path
($vmid);
1501 if (my $conf = PVE
::Cluster
::cfs_read_file
($cfspath)) {
1502 $qemu->{$vmid} = $conf;
1510 sub read_vm_firewall_configs
{
1512 my $vmfw_configs = {};
1514 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
1515 my $filename = "/etc/pve/firewall/$vmid.fw";
1516 my $fh = IO
::File-
>new($filename, O_RDONLY
);
1519 $vmfw_configs->{$vmid} = parse_vm_fw_rules
($filename, $fh);
1522 return $vmfw_configs;
1525 sub get_option_log_level
{
1526 my ($options, $k) = @_;
1528 my $v = $options->{$k};
1529 $v = $default_log_level if !defined($v);
1531 return undef if $v eq '' || $v eq 'nolog';
1533 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1535 return $v if ($v >= 0) && ($v <= 7);
1537 warn "unknown log level ($k = '$v')\n";
1542 sub generate_std_chains
{
1543 my ($ruleset, $options) = @_;
1545 my $loglevel = get_option_log_level
($options, 'smurf_log_level');
1547 # same as shorewall smurflog.
1548 if (defined($loglevel)) {
1549 $pve_std_chains-> {'PVEFW-smurflog'} = [
1550 "-j LOG --log-prefix \"smurfs-dropped: \" --log-level $loglevel",
1554 $pve_std_chains-> {'PVEFW-smurflog'} = [ "-j DROP" ];
1557 # same as shorewall logflags action.
1558 $loglevel = get_option_log_level
($options, 'tcp_flags_log_level');
1559 if (defined($loglevel)) {
1560 $pve_std_chains-> {'PVEFW-logflags'} = [
1561 "-j LOG --log-prefix \"logflags-dropped: \" --log-level $loglevel --log-ip-options",
1565 $pve_std_chains-> {'PVEFW-logflags'} = [ "-j DROP" ];
1568 foreach my $chain (keys %$pve_std_chains) {
1569 ruleset_create_chain
($ruleset, $chain);
1570 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1572 ruleset_generate_rule
($ruleset, $chain, $rule);
1574 ruleset_addrule
($ruleset, $chain, $rule);
1580 sub save_pvefw_status
{
1583 die "unknown status '$status' - internal error"
1584 if $status !~ m/^(stopped|active)$/;
1586 mkdir dirname
($pve_fw_status_filename);
1587 PVE
::Tools
::file_set_contents
($pve_fw_status_filename, $status);
1590 sub read_pvefw_status
{
1592 my $status = 'unknown';
1594 return 'stopped' if ! -f
$pve_fw_status_filename;
1597 $status = PVE
::Tools
::file_get_contents
($pve_fw_status_filename);
1605 my $vmdata = read_local_vm_config
();
1606 my $vmfw_configs = read_vm_firewall_configs
($vmdata);
1608 my $groups_conf = {};
1609 my $filename = "/etc/pve/firewall/groups.fw";
1610 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1611 $groups_conf = parse_group_fw_rules
($filename, $fh);
1616 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
1617 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
1619 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
1621 my $hostfw_options = {};
1622 my $hostfw_conf = {};
1624 $filename = "/etc/pve/local/host.fw";
1625 if (my $fh = IO
::File-
>new($filename, O_RDONLY
)) {
1626 $hostfw_conf = parse_host_fw_rules
($filename, $fh);
1627 $hostfw_options = $hostfw_conf->{options
};
1630 generate_std_chains
($ruleset, $hostfw_options);
1632 my $hostfw_enable = !(defined($hostfw_options->{enable
}) && ($hostfw_options->{enable
} == 0));
1634 enable_host_firewall
($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
1636 # generate firewall rules for QEMU VMs
1637 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
1638 my $conf = $vmdata->{qemu
}->{$vmid};
1639 my $vmfw_conf = $vmfw_configs->{$vmid};
1640 next if !$vmfw_conf;
1641 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1643 foreach my $netid (keys %$conf) {
1644 next if $netid !~ m/^net(\d+)$/;
1645 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
1647 my $iface = "tap${vmid}i$1";
1649 my $bridge = $net->{bridge
};
1650 next if !$bridge; # fixme: ?
1652 $bridge .= "v$net->{tag}" if $net->{tag
};
1654 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge);
1656 my $macaddr = $net->{macaddr
};
1657 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1658 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1662 # generate firewall rules for OpenVZ containers
1663 foreach my $vmid (keys %{$vmdata->{openvz
}}) {
1664 my $conf = $vmdata->{openvz
}->{$vmid};
1666 my $vmfw_conf = $vmfw_configs->{$vmid};
1667 next if !$vmfw_conf;
1668 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
1670 if ($conf->{ip_address
} && $conf->{ip_address
}->{value
}) {
1671 my $ip = $conf->{ip_address
}->{value
};
1672 generate_venet_rules_direction
($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'IN');
1673 generate_venet_rules_direction
($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'OUT');
1676 if ($conf->{netif
} && $conf->{netif
}->{value
}) {
1677 my $netif = PVE
::OpenVZ
::parse_netif
($conf->{netif
}->{value
});
1678 foreach my $netid (keys %$netif) {
1679 my $d = $netif->{$netid};
1680 my $bridge = $d->{bridge
};
1682 warn "no bridge device for CT $vmid iface '$netid'\n";
1685 my $macaddr = $d->{host_mac
};
1686 my $iface = $d->{host_ifname
};
1687 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1688 generate_tap_rules_direction
($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1693 # fixme: this is an optimization? if so, we should also drop INVALID packages?
1694 ruleset_insertrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1696 # fixme: what log level should we use here?
1697 my $loglevel = get_option_log_level
($hostfw_options, "log_level_out");
1699 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i venet0 -j ACCEPT");
1700 # disable interbridge routing
1701 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
1702 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
1703 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j LOG --log-prefix \"PVEFW-FORWARD-dropped \" --log-level $loglevel");
1704 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j LOG --log-prefix \"PVEFW-FORWARD-dropped \" --log-level $loglevel");
1705 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
1706 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
1708 return wantarray ?
($ruleset, $hostfw_conf) : $ruleset;
1711 sub get_ruleset_status
{
1712 my ($ruleset, $verbose) = @_;
1714 my $active_chains = iptables_get_chains
();
1716 my $statushash = {};
1718 foreach my $chain (sort keys %$ruleset) {
1719 my $digest = Digest
::SHA-
>new('sha1');
1720 foreach my $cmd (@{$ruleset->{$chain}}) {
1721 $digest->add("$cmd\n");
1723 my $sig = $digest->b64digest;
1724 $statushash->{$chain}->{sig
} = $sig;
1726 my $oldsig = $active_chains->{$chain};
1727 if (!defined($oldsig)) {
1728 $statushash->{$chain}->{action
} = 'create';
1730 if ($oldsig eq $sig) {
1731 $statushash->{$chain}->{action
} = 'exists';
1733 $statushash->{$chain}->{action
} = 'update';
1736 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1737 foreach my $cmd (@{$ruleset->{$chain}}) {
1738 print "\t$cmd\n" if $verbose;
1742 foreach my $chain (sort keys %$active_chains) {
1743 if (!defined($ruleset->{$chain})) {
1744 my $sig = $active_chains->{$chain};
1745 $statushash->{$chain}->{action
} = 'delete';
1746 $statushash->{$chain}->{sig
} = $sig;
1747 print "delete $chain ($sig)\n" if $verbose;
1757 get_ruleset_status
($ruleset, 1);
1760 sub print_sig_rule
{
1761 my ($chain, $sig) = @_;
1763 # We just use this to store a SHA1 checksum used to detect changes
1764 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1767 sub get_rulset_cmdlist {
1768 my ($ruleset, $verbose) = @_;
1770 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
1772 my $statushash = get_ruleset_status($ruleset, $verbose);
1774 # create missing chains first
1775 foreach my $chain (sort keys %$ruleset) {
1776 my $stat = $statushash->{$chain};
1777 die "internal error
" if !$stat;
1778 next if $stat->{action} ne 'create';
1780 $cmdlist .= ":$chain - [0:0]\n";
1783 my $rule = "INPUT
-j PVEFW-INPUT
";
1784 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1785 $cmdlist .= "-A
$rule\n";
1787 $rule = "OUTPUT
-j PVEFW-OUTPUT
";
1788 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1789 $cmdlist .= "-A
$rule\n";
1792 $rule = "FORWARD
-j PVEFW-FORWARD
";
1793 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1794 $cmdlist .= "-A
$rule\n";
1797 foreach my $chain (sort keys %$ruleset) {
1798 my $stat = $statushash->{$chain};
1799 die "internal error
" if !$stat;
1801 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1802 $cmdlist .= "-F
$chain\n";
1803 foreach my $cmd (@{$ruleset->{$chain}}) {
1804 $cmdlist .= "$cmd\n";
1806 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1807 } elsif ($stat->{action} eq 'delete') {
1808 die "internal error
"; # this should not happen
1809 } elsif ($stat->{action} eq 'exists') {
1812 die "internal error
- unknown status
'$stat->{action}'";
1816 foreach my $chain (keys %$statushash) {
1817 next if $statushash->{$chain}->{action} ne 'delete';
1818 $cmdlist .= "-F
$chain\n";
1820 foreach my $chain (keys %$statushash) {
1821 next if $statushash->{$chain}->{action} ne 'delete';
1822 next if $chain eq 'PVEFW-INPUT';
1823 next if $chain eq 'PVEFW-OUTPUT';
1824 next if $chain eq 'PVEFW-FORWARD';
1825 $cmdlist .= "-X
$chain\n";
1828 $cmdlist .= "COMMIT
\n";
1834 my ($ruleset, $hostfw_conf, $verbose) = @_;
1836 enable_bridge_firewall();
1838 update_nf_conntrack_max($hostfw_conf);
1840 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
1842 print $cmdlist if $verbose;
1844 iptables_restore_cmdlist($cmdlist);
1846 # test: re-read status and check if everything is up to date
1847 my $statushash = get_ruleset_status($ruleset);
1850 foreach my $chain (sort keys %$ruleset) {
1851 my $stat = $statushash->{$chain};
1852 if ($stat->{action} ne 'exists') {
1853 warn "unable to update chain
'$chain'\n";
1858 die "unable to apply firewall changes
\n" if $errors;
1861 sub update_nf_conntrack_max {
1862 my ($hostfw_conf) = @_;
1864 my $max = 65536; # reasonable default
1866 my $options = $hostfw_conf->{options} || {};
1868 if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) {
1869 $max = $options->{nf_conntrack_max};
1870 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
1873 my $filename_nf_conntrack_max = "/proc/sys
/net/nf_conntrack_max
";
1874 my $filename_hashsize = "/sys/module
/nf_conntrack/parameters
/hashsize
";
1876 my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max);
1878 if ($current != $max) {
1879 my $hashsize = int($max/4);
1880 PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize);
1881 PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max);
1886 my ($start, $verbose) = @_;
1889 my $status = read_pvefw_status();
1891 my ($ruleset, $hostfw_conf) = PVE::Firewall::compile();
1893 if ($start || $status eq 'active') {
1895 save_pvefw_status('active') if ($status ne 'active');
1897 apply_ruleset($ruleset, $hostfw_conf, $verbose);
1899 print "Firewall
not active
(status
= $status)\n" if $verbose;