8 use Socket
qw(AF_INET6 inet_ntop inet_pton);
10 use PVE
::Exception
qw(raise raise_param_exc);
11 use PVE
::JSONSchema
qw(register_standard_option get_standard_option);
14 use PVE
::Tools
qw($IPV4RE $IPV6RE);
19 use PVE
::Tools
qw(run_command lock_file dir_glob_foreach);
22 my $hostfw_conf_filename = "/etc/pve/local/host.fw";
23 my $pvefw_conf_dir = "/etc/pve/firewall";
24 my $clusterfw_conf_filename = "$pvefw_conf_dir/cluster.fw";
26 # dynamically include PVE::QemuServer and PVE::OpenVZ
27 # to avoid dependency problems
30 require PVE
::QemuServer
;
31 $have_qemu_server = 1;
37 $have_pve_manager = 1;
40 my $pve_fw_status_dir = "/var/lib/pve-firewall";
42 mkdir $pve_fw_status_dir; # make sure this exists
44 my $security_group_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
45 my $ipset_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
46 our $ip_alias_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
48 my $max_alias_name_length = 64;
49 my $max_ipset_name_length = 64;
50 my $max_group_name_length = 20;
52 PVE
::JSONSchema
::register_format
('IPorCIDR', \
&pve_verify_ip_or_cidr
);
53 sub pve_verify_ip_or_cidr
{
54 my ($cidr, $noerr) = @_;
56 if ($cidr =~ m!^(?:$IPV6RE|$IPV4RE)(/(\d+))?$!) {
57 return $cidr if Net
::IP-
>new($cidr);
58 return undef if $noerr;
59 die Net
::IP
::Error
() . "\n";
61 return undef if $noerr;
62 die "value does not look like a valid IP address or CIDR network\n";
65 PVE
::JSONSchema
::register_format
('IPorCIDRorAlias', \
&pve_verify_ip_or_cidr_or_alias
);
66 sub pve_verify_ip_or_cidr_or_alias
{
67 my ($cidr, $noerr) = @_;
69 return if $cidr =~ m/^(?:$ip_alias_pattern)$/;
71 return pve_verify_ip_or_cidr
($cidr, $noerr);
74 PVE
::JSONSchema
::register_standard_option
('ipset-name', {
75 description
=> "IP set name.",
77 pattern
=> $ipset_name_pattern,
79 maxLength
=> $max_ipset_name_length,
82 PVE
::JSONSchema
::register_standard_option
('pve-fw-alias', {
83 description
=> "Alias name.",
85 pattern
=> $ip_alias_pattern,
87 maxLength
=> $max_alias_name_length,
90 PVE
::JSONSchema
::register_standard_option
('pve-fw-loglevel' => {
91 description
=> "Log level.",
93 enum
=> ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'nolog'],
97 PVE
::JSONSchema
::register_standard_option
('pve-security-group-name', {
98 description
=> "Security Group name.",
100 pattern
=> $security_group_name_pattern,
102 maxLength
=> $max_group_name_length,
105 my $feature_ipset_nomatch = 0;
107 my (undef, undef, $release) = POSIX
::uname
();
108 if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) {
109 my ($major, $minor) = ($1, $2);
110 $feature_ipset_nomatch = 1 if ($major > 3) ||
111 ($major == 3 && $minor >= 7);
118 my $nodename = PVE
::INotify
::nodename
();
120 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
122 my $default_log_level = 'nolog'; # avoid logs by default
124 my $log_level_hash = {
135 # we need to overwrite some macros for ipv6
136 my $pve_ipv6fw_macros = {
138 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'echo-request' },
141 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
142 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'echo-request' },
146 # imported/converted from: /usr/share/shorewall/macro.*
147 my $pve_fw_macros = {
150 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
151 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
154 "Auth (identd) traffic",
155 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
158 "Border Gateway Protocol traffic",
159 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
162 "BitTorrent traffic for BitTorrent 3.1 and earlier",
163 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
164 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
167 "BitTorrent traffic for BitTorrent 3.2 and later",
168 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
169 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
172 "Concurrent Versions System pserver traffic",
173 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
176 "Citrix/ICA traffic (ICA, ICA Browser, CGP)",
177 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
178 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
179 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
182 "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)",
183 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
184 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
187 "Distributed Checksum Clearinghouse spam filtering mechanism",
188 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
191 "Forwarded DHCP traffic",
192 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
195 "Domain Name System traffic (upd and tcp)",
196 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
197 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
200 "Distributed Compiler service",
201 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
204 "File Transfer Protocol",
205 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
208 "Finger protocol (RFC 742)",
209 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
212 "GNUnet secure peer-to-peer networking traffic",
213 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
214 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
215 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
216 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
219 "Generic Routing Encapsulation tunneling protocol",
220 { action
=> 'PARAM', proto
=> '47' },
223 "Git distributed revision control traffic",
224 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
227 "OpenPGP HTTP keyserver protocol traffic",
228 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
231 "Hypertext Transfer Protocol (WWW)",
232 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
235 "Hypertext Transfer Protocol (WWW) over SSL",
236 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
239 "Internet Cache Protocol V2 (Squid) traffic",
240 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
243 "AOL Instant Messenger traffic",
244 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
247 "Internet Message Access Protocol",
248 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
251 "Internet Message Access Protocol over SSL",
252 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
255 "IPIP capsulation traffic",
256 { action
=> 'PARAM', proto
=> '94' },
260 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
261 { action
=> 'PARAM', proto
=> '50' },
264 "IPsec authentication (AH) traffic",
265 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
266 { action
=> 'PARAM', proto
=> '51' },
269 "IPsec traffic and Nat-Traversal",
270 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
271 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
272 { action
=> 'PARAM', proto
=> '50' },
275 "Internet Relay Chat traffic",
276 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
279 "HP Jetdirect printing",
280 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
283 "Layer 2 Tunneling Protocol traffic",
284 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
287 "Lightweight Directory Access Protocol traffic",
288 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
291 "Secure Lightweight Directory Access Protocol traffic",
292 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
295 "Microsoft Notification Protocol",
296 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
299 "Microsoft SQL Server",
300 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
303 "Mail traffic (SMTP, SMTPS, Submission)",
304 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
306 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
309 "Munin networked resource monitoring traffic",
310 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
314 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
317 "NNTP traffic (Usenet).",
318 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
321 "Encrypted NNTP traffic (Usenet)",
322 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
325 "Network Time Protocol (ntpd)",
326 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
329 "OSPF multicast traffic",
330 { action
=> 'PARAM', proto
=> '89' },
334 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
337 "Symantec PCAnywere (tm)",
338 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
339 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
343 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
346 "Encrypted POP3 traffic",
347 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
350 "Point-to-Point Tunneling Protocol",
351 { action
=> 'PARAM', proto
=> '47' },
352 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
356 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
360 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
363 "Line Printer protocol printing",
364 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
367 "Microsoft Remote Desktop Protocol traffic",
368 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
371 "Routing Information Protocol (bidirectional)",
372 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
375 "BIND remote management protocol",
376 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
379 "Razor Antispam System",
380 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
383 "Remote time retrieval (rdate)",
384 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
388 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
391 "SANE network scanning",
392 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
395 "Microsoft SMB traffic",
396 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
397 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
398 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
399 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
402 "Samba Web Administration Tool",
403 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
406 "Simple Mail Transfer Protocol",
407 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
410 "Encrypted Simple Mail Transfer Protocol",
411 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
414 "Simple Network Management Protocol",
415 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
416 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
419 "Spam Assassin SPAMD traffic",
420 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
423 "Secure shell traffic",
424 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
427 "Subversion server (svnserve)",
428 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
431 "SixXS IPv6 Deployment and Tunnel Broker",
432 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
433 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
434 { action
=> 'PARAM', proto
=> '41' },
435 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
438 "Squid web proxy traffic",
439 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
442 "Mail message submission traffic",
443 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
446 "Syslog protocol (RFC 5424) traffic",
447 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
448 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
451 "Trivial File Transfer Protocol traffic",
452 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
456 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
460 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
463 "RFC 868 Time protocol",
464 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
467 "Traceroute (for up to 30 hops) traffic",
468 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
469 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
472 "VNC traffic for VNC display's 0 - 99",
473 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5999' },
476 "VNC traffic from Vncservers to Vncviewers in listen mode",
477 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
480 "WWW traffic (HTTP and HTTPS)",
481 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
482 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
485 "Web Cache/Proxy traffic (port 8080)",
486 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
490 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
493 "Whois (nicname, RFC 3912) traffic",
494 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
498 my $pve_fw_parsed_macros;
499 my $pve_fw_macro_descr;
500 my $pve_fw_preferred_macro_names = {};
502 my $pve_std_chains = {};
503 $pve_std_chains->{4} = {
504 'PVEFW-SET-ACCEPT-MARK' => [
505 "-j MARK --set-mark 1",
507 'PVEFW-DropBroadcast' => [
508 # same as shorewall 'Broadcast'
509 # simply DROP BROADCAST/MULTICAST/ANYCAST
510 # we can use this to reduce logging
511 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
512 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
513 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
514 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
517 # same as shorewall 'reject'
518 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
519 { action
=> 'DROP', source
=> '224.0.0.0/4' },
520 { action
=> 'DROP', proto
=> 'icmp' },
521 "-p tcp -j REJECT --reject-with tcp-reset",
522 "-p udp -j REJECT --reject-with icmp-port-unreachable",
523 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
524 "-j REJECT --reject-with icmp-host-prohibited",
527 # same as shorewall 'Drop', which is equal to DROP,
528 # but REJECT/DROP some packages to reduce logging,
529 # and ACCEPT critical ICMP types
530 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
531 # we are not interested in BROADCAST/MULTICAST/ANYCAST
532 { action
=> 'PVEFW-DropBroadcast' },
533 # ACCEPT critical ICMP types
534 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
535 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
536 # Drop packets with INVALID state
537 "-m conntrack --ctstate INVALID -j DROP",
538 # Drop Microsoft SMB noise
539 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
540 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
541 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
542 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
543 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
544 # Drop new/NotSyn traffic so that it doesn't get logged
545 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
547 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
550 # same as shorewall 'Reject', which is equal to Reject,
551 # but REJECT/DROP some packages to reduce logging,
552 # and ACCEPT critical ICMP types
553 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
554 # we are not interested in BROADCAST/MULTICAST/ANYCAST
555 { action
=> 'PVEFW-DropBroadcast' },
556 # ACCEPT critical ICMP types
557 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
558 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
559 # Drop packets with INVALID state
560 "-m conntrack --ctstate INVALID -j DROP",
561 # Drop Microsoft SMB noise
562 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
563 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
564 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
565 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
566 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
567 # Drop new/NotSyn traffic so that it doesn't get logged
568 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
570 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
572 'PVEFW-tcpflags' => [
573 # same as shorewall tcpflags action.
574 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
575 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
576 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
577 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
578 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
579 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
582 # same as shorewall smurfs action
583 # Filter packets for smurfs (packets with a broadcast address as the source).
584 "-s 0.0.0.0/32 -j RETURN",
585 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
586 "-s 224.0.0.0/4 -g PVEFW-smurflog",
590 $pve_std_chains->{6} = {
591 'PVEFW-SET-ACCEPT-MARK' => [
592 "-j MARK --set-mark 1",
594 'PVEFW-DropBroadcast' => [
595 # same as shorewall 'Broadcast'
596 # simply DROP BROADCAST/MULTICAST/ANYCAST
597 # we can use this to reduce logging
598 #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6
599 # ipv6 addrtype does not work with kernel 2.6.32
600 #{ action => 'DROP', dsttype => 'MULTICAST' },
601 #{ action => 'DROP', dsttype => 'ANYCAST' },
602 { action
=> 'DROP', dest
=> 'ff00::/8' },
603 #{ action => 'DROP', dest => '224.0.0.0/4' },
606 # same as shorewall 'reject'
607 #{ action => 'DROP', dsttype => 'BROADCAST' },
608 #{ action => 'DROP', source => '224.0.0.0/4' },
609 { action
=> 'DROP', proto
=> 'icmpv6' },
610 "-p tcp -j REJECT --reject-with tcp-reset",
611 #"-p udp -j REJECT --reject-with icmp-port-unreachable",
612 #"-p icmp -j REJECT --reject-with icmp-host-unreachable",
613 #"-j REJECT --reject-with icmp-host-prohibited",
616 # same as shorewall 'Drop', which is equal to DROP,
617 # but REJECT/DROP some packages to reduce logging,
618 # and ACCEPT critical ICMP types
619 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
620 # we are not interested in BROADCAST/MULTICAST/ANYCAST
621 { action
=> 'PVEFW-DropBroadcast' },
622 # ACCEPT critical ICMP types
623 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
624 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
625 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
627 # Drop packets with INVALID state
628 "-m conntrack --ctstate INVALID -j DROP",
629 # Drop Microsoft SMB noise
630 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
631 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
632 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
633 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
634 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
635 # Drop new/NotSyn traffic so that it doesn't get logged
636 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
638 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
641 # same as shorewall 'Reject', which is equal to Reject,
642 # but REJECT/DROP some packages to reduce logging,
643 # and ACCEPT critical ICMP types
644 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
645 # we are not interested in BROADCAST/MULTICAST/ANYCAST
646 { action
=> 'PVEFW-DropBroadcast' },
647 # ACCEPT critical ICMP types
648 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
649 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
650 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
652 # Drop packets with INVALID state
653 "-m conntrack --ctstate INVALID -j DROP",
654 # Drop Microsoft SMB noise
655 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
656 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
657 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
658 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
659 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
660 # Drop new/NotSyn traffic so that it doesn't get logged
661 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
663 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
665 'PVEFW-tcpflags' => [
666 # same as shorewall tcpflags action.
667 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
668 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
669 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
670 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
671 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
672 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
676 # iptables -p icmp -h
677 my $icmp_type_names = {
680 'destination-unreachable' => 1,
681 'network-unreachable' => 1,
682 'host-unreachable' => 1,
683 'protocol-unreachable' => 1,
684 'port-unreachable' => 1,
685 'fragmentation-needed' => 1,
686 'source-route-failed' => 1,
687 'network-unknown' => 1,
689 'network-prohibited' => 1,
690 'host-prohibited' => 1,
691 'TOS-network-unreachable' => 1,
692 'TOS-host-unreachable' => 1,
693 'communication-prohibited' => 1,
694 'host-precedence-violation' => 1,
695 'precedence-cutoff' => 1,
696 'source-quench' => 1,
698 'network-redirect' => 1,
699 'host-redirect' => 1,
700 'TOS-network-redirect' => 1,
701 'TOS-host-redirect' => 1,
703 'router-advertisement' => 1,
704 'router-solicitation' => 1,
705 'time-exceeded' => 1,
706 'ttl-zero-during-transit' => 1,
707 'ttl-zero-during-reassembly' => 1,
708 'parameter-problem' => 1,
709 'ip-header-bad' => 1,
710 'required-option-missing' => 1,
711 'timestamp-request' => 1,
712 'timestamp-reply' => 1,
713 'address-mask-request' => 1,
714 'address-mask-reply' => 1,
717 # ip6tables -p icmpv6 -h
719 my $icmpv6_type_names = {
721 'destination-unreachable' => 1,
723 'communication-prohibited' => 1,
724 'address-unreachable' => 1,
725 'port-unreachable' => 1,
726 'packet-too-big' => 1,
727 'time-exceeded' => 1,
728 'ttl-zero-during-transit' => 1,
729 'ttl-zero-during-reassembly' => 1,
730 'parameter-problem' => 1,
732 'unknown-header-type' => 1,
733 'unknown-option' => 1,
736 'router-solicitation' => 1,
737 'router-advertisement' => 1,
738 'neighbour-solicitation' => 1,
739 'neighbour-advertisement' => 1,
743 sub init_firewall_macros
{
745 $pve_fw_parsed_macros = {};
747 foreach my $k (keys %$pve_fw_macros) {
748 my $lc_name = lc($k);
749 my $macro = $pve_fw_macros->{$k};
750 if (!ref($macro->[0])) {
751 $pve_fw_macro_descr->{$k} = shift @$macro;
753 $pve_fw_preferred_macro_names->{$lc_name} = $k;
754 $pve_fw_parsed_macros->{$k} = $macro;
758 init_firewall_macros
();
761 return wantarray ?
($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
766 sub get_etc_services
{
768 return $etc_services if $etc_services;
770 my $filename = "/etc/services";
772 my $fh = IO
::File-
>new($filename, O_RDONLY
);
774 warn "unable to read '$filename' - $!\n";
780 while (my $line = <$fh>) {
782 next if $line =~m/^#/;
783 next if ($line =~m/^\s*$/);
785 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
786 $services->{byid
}->{$2}->{name
} = $1;
787 $services->{byid
}->{$2}->{port
} = $2;
788 $services->{byid
}->{$2}->{$3} = 1;
789 $services->{byname
}->{$1} = $services->{byid
}->{$2};
795 $etc_services = $services;
798 return $etc_services;
803 sub get_etc_protocols
{
804 return $etc_protocols if $etc_protocols;
806 my $filename = "/etc/protocols";
808 my $fh = IO
::File-
>new($filename, O_RDONLY
);
810 warn "unable to read '$filename' - $!\n";
816 while (my $line = <$fh>) {
818 next if $line =~m/^#/;
819 next if ($line =~m/^\s*$/);
821 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
822 $protocols->{byid
}->{$2}->{name
} = $1;
823 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
829 # add special case for ICMP v6
830 $protocols->{byid
}->{icmpv6
}->{name
} = "icmpv6";
831 $protocols->{byname
}->{icmpv6
} = $protocols->{byid
}->{icmpv6
};
833 $etc_protocols = $protocols;
835 return $etc_protocols;
838 my $ipv4_mask_hash_localnet = {
840 '255.255.128.0' => 17,
841 '255.255.192.0' => 18,
842 '255.255.224.0' => 19,
843 '255.255.240.0' => 20,
844 '255.255.248.0' => 21,
845 '255.255.252.0' => 22,
846 '255.255.254.0' => 23,
847 '255.255.255.0' => 24,
848 '255.255.255.128' => 25,
849 '255.255.255.192' => 26,
850 '255.255.255.224' => 27,
851 '255.255.255.240' => 28,
852 '255.255.255.248' => 29,
853 '255.255.255.252' => 30,
859 my ($new_value) = @_;
861 $__local_network = $new_value if defined($new_value);
863 return $__local_network if defined($__local_network);
866 my $nodename = PVE
::INotify
::nodename
();
868 my $ip = PVE
::Cluster
::remote_node_ip
($nodename);
870 my $testip = Net
::IP-
>new($ip);
872 my $routes = PVE
::ProcFSTools
::read_proc_net_route
();
873 foreach my $entry (@$routes) {
874 my $mask = $ipv4_mask_hash_localnet->{$entry->{mask
}};
875 next if !defined($mask);
876 return if $mask eq '0.0.0.0';
877 my $cidr = "$entry->{dest}/$mask";
878 my $testnet = Net
::IP-
>new($cidr);
879 if ($testnet->overlaps($testip)) {
880 $__local_network = $cidr;
887 return $__local_network;
890 # ipset names are limited to 31 characters,
891 # and we use '-v4' or '-v6' to indicate IP versions,
892 # and we use '_swap' suffix for atomic update,
893 # for example PVEFW-${VMID}-${ipset_name}_swap
895 my $max_iptables_ipset_name_length = 31 - length("PVEFW-") - length("_swap");
897 sub compute_ipset_chain_name
{
898 my ($vmid, $ipset_name, $ipversion) = @_;
900 $vmid = 0 if !defined($vmid);
902 my $id = "$vmid-${ipset_name}-v$ipversion";
904 if (length($id) > $max_iptables_ipset_name_length) {
905 $id = PVE
::Tools
::fnv31a_hex
($id);
911 sub compute_ipfilter_ipset_name
{
914 return "ipfilter-$iface";
917 sub parse_address_list
{
920 if ($str =~ m/^(\+)(\S+)$/) { # ipset ref
921 die "ipset name too long\n" if length($str) > ($max_ipset_name_length + 1);
925 if ($str =~ m/^${ip_alias_pattern}$/) {
926 die "alias name too long\n" if length($str) > $max_alias_name_length;
934 foreach my $elem (split(/,/, $str)) {
936 my $ip = Net
::IP-
>new($elem);
938 my $err = Net
::IP
::Error
();
939 die "invalid IP address: $err\n";
941 $iprange = 1 if $elem =~ m/-/;
943 my $new_ipversion = Net
::IP
::ip_is_ipv6
($ip->ip()) ?
6 : 4;
945 die "detected mixed ipv4/ipv6 addresses in address list '$str'\n"
946 if $ipversion && ($new_ipversion != $ipversion);
948 $ipversion = $new_ipversion;
951 die "you can't use a range in a list\n" if $iprange && $count > 1;
956 sub parse_port_name_number_or_range
{
959 my $services = PVE
::Firewall
::get_etc_services
();
963 foreach my $item (split(/,/, $str)) {
965 if ($item =~ m/^(\d+):(\d+)$/) {
966 my ($port1, $port2) = ($1, $2);
967 die "invalid port '$port1'\n" if $port1 > 65535;
968 die "invalid port '$port2'\n" if $port2 > 65535;
969 } elsif ($item =~ m/^(\d+)$/) {
971 die "invalid port '$port'\n" if $port > 65535;
973 if ($icmp_type_names->{$item}) {
975 } elsif ($icmpv6_type_names->{$item}) {
978 die "invalid port '$item'\n" if !$services->{byname
}->{$item};
983 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
988 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
989 sub pve_fw_verify_port_spec
{
992 parse_port_name_number_or_range
($portstr);
997 PVE
::JSONSchema
::register_format
('pve-fw-addr-spec', \
&pve_fw_verify_addr_spec
);
998 sub pve_fw_verify_addr_spec
{
1001 parse_address_list
($list);
1006 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
1007 sub pve_fw_verify_protocol_spec
{
1010 my $protocols = get_etc_protocols
();
1012 die "unknown protocol '$proto'\n" if $proto &&
1013 !(defined($protocols->{byname
}->{$proto}) ||
1014 defined($protocols->{byid
}->{$proto}));
1020 # helper function for API
1022 sub copy_opject_with_digest
{
1025 my $sha = Digest
::SHA-
>new('sha1');
1028 foreach my $k (sort keys %$object) {
1029 my $v = $object->{$k};
1030 next if !defined($v);
1032 $sha->add($k, ':', $v, "\n");
1035 my $digest = $sha->hexdigest;
1037 $res->{digest
} = $digest;
1039 return wantarray ?
($res, $digest) : $res;
1042 sub copy_list_with_digest
{
1045 my $sha = Digest
::SHA-
>new('sha1');
1048 foreach my $entry (@$list) {
1050 foreach my $k (sort keys %$entry) {
1051 my $v = $entry->{$k};
1052 next if !defined($v);
1054 # Note: digest ignores refs ($rule->{errors})
1055 $sha->add($k, ':', $v, "\n") if !ref($v); ;
1060 my $digest = $sha->hexdigest;
1062 foreach my $entry (@$res) {
1063 $entry->{digest
} = $digest;
1066 return wantarray ?
($res, $digest) : $res;
1069 my $rule_properties = {
1071 description
=> "Update rule at position <pos>.",
1076 digest
=> get_standard_option
('pve-config-digest'),
1080 enum
=> ['in', 'out', 'group'],
1083 description
=> "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
1086 pattern
=> $security_group_name_pattern,
1095 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
1097 type
=> 'string', format
=> 'pve-fw-addr-spec',
1101 type
=> 'string', format
=> 'pve-fw-addr-spec',
1105 type
=> 'string', format
=> 'pve-fw-protocol-spec',
1113 type
=> 'string', format
=> 'pve-fw-port-spec',
1117 type
=> 'string', format
=> 'pve-fw-port-spec',
1126 sub add_rule_properties
{
1127 my ($properties) = @_;
1129 foreach my $k (keys %$rule_properties) {
1130 my $h = $rule_properties->{$k};
1131 # copy data, so that we can modify later without side effects
1132 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
1138 sub delete_rule_properties
{
1139 my ($rule, $delete_str) = @_;
1141 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
1142 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
1143 if !defined($rule_properties->{$opt});
1144 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
1145 if $opt eq 'type' || $opt eq 'action';
1146 delete $rule->{$opt};
1152 my $apply_macro = sub {
1153 my ($macro_name, $param, $verify, $ipversion) = @_;
1155 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1156 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1158 if ($ipversion && ($ipversion == 6) && $pve_ipv6fw_macros->{$macro_name}) {
1159 $macro_rules = $pve_ipv6fw_macros->{$macro_name};
1164 foreach my $templ (@$macro_rules) {
1166 my $param_used = {};
1167 foreach my $k (keys %$templ) {
1168 my $v = $templ->{$k};
1169 if ($v eq 'PARAM') {
1171 $param_used->{$k} = 1;
1172 } elsif ($v eq 'DEST') {
1173 $v = $param->{dest
};
1174 $param_used->{dest
} = 1;
1175 } elsif ($v eq 'SOURCE') {
1176 $v = $param->{source
};
1177 $param_used->{source
} = 1;
1181 my $msg = "missing parameter '$k' in macro '$macro_name'";
1182 raise_param_exc
({ macro => $msg }) if $verify;
1187 foreach my $k (keys %$param) {
1188 next if $k eq 'macro';
1189 next if !defined($param->{$k});
1190 next if $param_used->{$k};
1191 if (defined($rule->{$k})) {
1192 if ($rule->{$k} ne $param->{$k}) {
1193 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
1194 raise_param_exc
({ $k => $msg }) if $verify;
1198 $rule->{$k} = $param->{$k};
1201 push @$rules, $rule;
1207 my $rule_env_iface_lookup = {
1216 my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_;
1218 my $allow_groups = $rule_env eq 'group' ?
0 : 1;
1220 my $allow_iface = $rule_env_iface_lookup->{$rule_env};
1221 die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen
1223 my $errors = $rule->{errors
} || {};
1225 my $error_count = 0;
1227 my $add_error = sub {
1228 my ($param, $msg) = @_;
1230 raise_param_exc
({ $param => $msg }) if !$noerr;
1232 $errors->{$param} = $msg if !$errors->{$param};
1236 my $set_ip_version = sub {
1239 die "detected mixed ipv4/ipv6 adresses in rule\n"
1240 if $ipversion && ($vers != $ipversion);
1245 my $check_ipset_or_alias_property = sub {
1246 my ($name, $expected_ipversion) = @_;
1248 if (my $value = $rule->{$name}) {
1249 if ($value =~ m/^\+/) {
1250 if ($value =~ m/^\+(${ipset_name_pattern})$/) {
1251 &$add_error($name, "no such ipset '$1'")
1252 if !($cluster_conf->{ipset
}->{$1} || ($fw_conf && $fw_conf->{ipset
}->{$1}));
1255 &$add_error($name, "invalid ipset name '$value'");
1257 } elsif ($value =~ m/^${ip_alias_pattern}$/){
1258 my $alias = lc($value);
1259 &$add_error($name, "no such alias '$value'")
1260 if !($cluster_conf->{aliases
}->{$alias} || ($fw_conf && $fw_conf->{aliases
}->{$alias}));
1261 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1262 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1264 &$set_ip_version($e->{ipversion
});
1269 my $type = $rule->{type
};
1270 my $action = $rule->{action
};
1272 &$add_error('type', "missing property") if !$type;
1273 &$add_error('action', "missing property") if !$action;
1276 if ($type eq 'in' || $type eq 'out') {
1277 &$add_error('action', "unknown action '$action'")
1278 if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/);
1279 } elsif ($type eq 'group') {
1280 &$add_error('type', "security groups not allowed")
1282 &$add_error('action', "invalid characters in security group name")
1283 if $action && ($action !~ m/^${security_group_name_pattern}$/);
1285 &$add_error('type', "unknown rule type '$type'");
1289 if ($rule->{iface
}) {
1290 &$add_error('type', "parameter -i not allowed for this rule type")
1292 eval { PVE
::JSONSchema
::pve_verify_iface
($rule->{iface
}); };
1293 &$add_error('iface', $@) if $@;
1294 if ($rule_env eq 'vm') {
1295 &$add_error('iface', "value does not match the regex pattern 'net\\d+'")
1296 if $rule->{iface
} !~ m/^net(\d+)$/;
1297 } elsif ($rule_env eq 'ct') {
1298 &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'")
1299 if $rule->{iface
} !~ m/^(venet|eth(\d+))$/;
1303 if ($rule->{macro}) {
1304 if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) {
1305 $rule->{macro} = $preferred_name;
1307 &$add_error('macro', "unknown macro '$rule->{macro}'");
1311 if ($rule->{proto
}) {
1312 eval { pve_fw_verify_protocol_spec
($rule->{proto
}); };
1313 &$add_error('proto', $@) if $@;
1314 &$set_ip_version(4) if $rule->{proto
} eq 'icmp';
1315 &$set_ip_version(6) if $rule->{proto
} eq 'icmpv6';
1318 if ($rule->{dport
}) {
1319 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1320 &$add_error('dport', $@) if $@;
1321 &$add_error('proto', "missing property - 'dport' requires this property")
1325 if ($rule->{sport
}) {
1326 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1327 &$add_error('sport', $@) if $@;
1328 &$add_error('proto', "missing property - 'sport' requires this property")
1332 if ($rule->{source
}) {
1334 my $source_ipversion = parse_address_list
($rule->{source
});
1335 &$set_ip_version($source_ipversion);
1337 &$add_error('source', $@) if $@;
1338 &$check_ipset_or_alias_property('source', $ipversion);
1341 if ($rule->{dest
}) {
1343 my $dest_ipversion = parse_address_list
($rule->{dest
});
1344 &$set_ip_version($dest_ipversion);
1346 &$add_error('dest', $@) if $@;
1347 &$check_ipset_or_alias_property('dest', $ipversion);
1350 $rule->{ipversion
} = $ipversion if $ipversion;
1352 if ($rule->{macro} && !$error_count) {
1353 eval { &$apply_macro($rule->{macro}, $rule, 1, $ipversion); };
1355 if (ref($err) eq "PVE::Exception" && $err->{errors
}) {
1356 my $eh = $err->{errors
};
1357 foreach my $p (keys %$eh) {
1358 &$add_error($p, $eh->{$p});
1361 &$add_error('macro', "$err");
1366 $rule->{errors
} = $errors if $error_count;
1371 sub copy_rule_data
{
1372 my ($rule, $param) = @_;
1374 foreach my $k (keys %$rule_properties) {
1375 if (defined(my $v = $param->{$k})) {
1376 if ($v eq '' || $v eq '-') {
1387 sub rules_modify_permissions
{
1388 my ($rule_env) = @_;
1390 if ($rule_env eq 'host') {
1392 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
1394 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1396 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1398 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1400 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
1407 sub rules_audit_permissions
{
1408 my ($rule_env) = @_;
1410 if ($rule_env eq 'host') {
1412 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
1414 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1416 check
=> ['perm', '/', [ 'Sys.Audit' ]],
1418 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1420 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1428 my $bridge_firewall_enabled = 0;
1430 sub enable_bridge_firewall
{
1432 return if $bridge_firewall_enabled; # only once
1434 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1435 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1437 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1438 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1440 $bridge_firewall_enabled = 1;
1443 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1445 sub iptables_restore_cmdlist
{
1448 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1451 sub ip6tables_restore_cmdlist
{
1454 run_command
("/sbin/ip6tables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1457 sub ipset_restore_cmdlist
{
1460 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist, errmsg
=> "ipset_restore_cmdlist");
1463 sub iptables_get_chains
{
1464 my ($iptablescmd) = @_;
1466 $iptablescmd = "iptables" if !$iptablescmd;
1470 # check what chains we want to track
1471 my $is_pvefw_chain = sub {
1474 return 1 if $name =~ m/^PVEFW-\S+$/;
1476 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
1478 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
1480 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
1482 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/;
1483 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
1495 return if $line =~ m/^#/;
1496 return if $line =~ m/^\s*$/;
1498 if ($line =~ m/^\*(\S+)$/) {
1503 return if $table ne 'filter';
1505 if ($line =~ m/^:(\S+)\s/) {
1507 return if !&$is_pvefw_chain($chain);
1508 $res->{$chain} = "unknown";
1509 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1510 my ($chain, $sig) = ($1, $2);
1511 return if !&$is_pvefw_chain($chain);
1512 $res->{$chain} = $sig;
1513 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1516 # simply ignore the rest
1521 run_command
("/sbin/$iptablescmd-save", outfunc
=> $parser);
1523 return wantarray ?
($res, $hooks) : $res;
1526 sub iptables_chain_digest
{
1528 my $digest = Digest
::SHA-
>new('sha1');
1529 foreach my $rule (@$rules) { # order is important
1530 $digest->add($rule);
1532 return $digest->b64digest;
1535 sub ipset_chain_digest
{
1538 my $digest = Digest
::SHA-
>new('sha1');
1539 foreach my $rule (sort @$rules) { # note: sorted
1540 $digest->add($rule);
1542 return $digest->b64digest;
1545 sub ipset_get_chains
{
1553 return if $line =~ m/^#/;
1554 return if $line =~ m/^\s*$/;
1555 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1557 $line =~ s/\s+$//; # delete trailing white space
1558 push @{$chains->{$chain}}, $line;
1560 # simply ignore the rest
1565 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
1567 # compute digest for each chain
1568 foreach my $chain (keys %$chains) {
1569 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1575 sub ruleset_generate_cmdstr
{
1576 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1578 return if defined($rule->{enable
}) && !$rule->{enable
};
1579 return if $rule->{errors
};
1581 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1583 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1584 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1588 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1589 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1591 my $source = $rule->{source
};
1592 my $dest = $rule->{dest
};
1595 if ($source =~ m/^\+/) {
1596 if ($source =~ m/^\+(${ipset_name_pattern})$/) {
1598 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1599 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1600 push @cmd, "-m set --match-set ${ipset_chain} src";
1601 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1602 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1603 push @cmd, "-m set --match-set ${ipset_chain} src";
1605 die "no such ipset '$name'\n";
1608 die "invalid security group name '$source'\n";
1610 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1611 my $alias = lc($source);
1612 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1613 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1614 die "no such alias '$source'\n" if !$e;
1615 push @cmd, "-s $e->{cidr}";
1616 } elsif ($source =~ m/\-/){
1617 push @cmd, "-m iprange --src-range $source";
1619 push @cmd, "-s $source";
1624 if ($dest =~ m/^\+/) {
1625 if ($dest =~ m/^\+(${ipset_name_pattern})$/) {
1627 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1628 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1629 push @cmd, "-m set --match-set ${ipset_chain} dst";
1630 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1631 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1632 push @cmd, "-m set --match-set ${ipset_chain} dst";
1634 die "no such ipset '$name'\n";
1637 die "invalid security group name '$dest'\n";
1639 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1640 my $alias = lc($dest);
1641 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1642 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1643 die "no such alias '$dest'\n" if !$e;
1644 push @cmd, "-d $e->{cidr}";
1645 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1646 push @cmd, "-m iprange --dst-range $dest";
1648 push @cmd, "-d $dest";
1652 if ($rule->{proto
}) {
1653 push @cmd, "-p $rule->{proto}";
1656 $multiport++ if $nbdport > 1;
1657 $multiport++ if $nbsport > 1;
1659 push @cmd, "--match multiport" if $multiport;
1661 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1662 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1664 if ($rule->{dport
}) {
1665 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1666 # Note: we use dport to store --icmp-type
1667 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
1668 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1669 } elsif ($rule->{proto
} && $rule->{proto
} eq 'icmpv6') {
1670 # Note: we use dport to store --icmpv6-type
1671 die "unknown icmpv6-type '$rule->{dport}'\n" if !defined($icmpv6_type_names->{$rule->{dport
}});
1672 push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}";
1675 if ($multiport == 2) {
1676 push @cmd, "--ports $rule->{dport}";
1678 push @cmd, "--dports $rule->{dport}";
1681 push @cmd, "--dport $rule->{dport}";
1686 if ($rule->{sport
}) {
1688 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1690 push @cmd, "--sport $rule->{sport}";
1693 } elsif ($rule->{dport
} || $rule->{sport
}) {
1694 die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport
};
1695 die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport
};
1698 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1700 if (my $action = $rule->{action
}) {
1701 $action = $actions->{$action} if defined($actions->{$action});
1702 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1703 push @cmd, $goto ?
"-g $action" : "-j $action";
1706 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1709 sub ruleset_generate_rule
{
1710 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1714 if ($rule->{macro}) {
1715 $rules = &$apply_macro($rule->{macro}, $rule, 0, $ipversion);
1720 # update all or nothing
1723 foreach my $tmp (@$rules) {
1724 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) {
1725 push @cmds, $cmdstr;
1729 foreach my $cmdstr (@cmds) {
1730 ruleset_addrule
($ruleset, $chain, $cmdstr);
1734 sub ruleset_generate_rule_insert
{
1735 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto) = @_;
1737 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1739 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $rule, $actions, $goto)) {
1740 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1744 sub ruleset_create_chain
{
1745 my ($ruleset, $chain) = @_;
1747 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1748 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1750 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1752 $ruleset->{$chain} = [];
1755 sub ruleset_chain_exist
{
1756 my ($ruleset, $chain) = @_;
1758 return $ruleset->{$chain} ?
1 : undef;
1761 sub ruleset_addrule
{
1762 my ($ruleset, $chain, $rule) = @_;
1764 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1766 push @{$ruleset->{$chain}}, "-A $chain $rule";
1769 sub ruleset_insertrule
{
1770 my ($ruleset, $chain, $rule) = @_;
1772 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1774 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1777 sub get_log_rule_base
{
1778 my ($chain, $vmid, $msg, $loglevel) = @_;
1780 die "internal error - no log level" if !defined($loglevel);
1782 $vmid = 0 if !defined($vmid);
1784 # Note: we use special format for prefix to pass further
1785 # info to log daemon (VMID, LOGVELEL and CHAIN)
1787 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1790 sub ruleset_addlog {
1791 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1793 return if !defined($loglevel);
1795 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1797 $logrule = "$rule $logrule" if defined($rule);
1799 ruleset_addrule($ruleset, $chain, $logrule);
1802 sub ruleset_add_chain_policy {
1803 my ($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action) = @_;
1805 if ($policy eq 'ACCEPT') {
1807 ruleset_generate_rule($ruleset, $chain, $ipversion, { action => 'ACCEPT' },
1808 { ACCEPT => $accept_action});
1810 } elsif ($policy eq 'DROP') {
1812 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1814 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1816 ruleset_addrule($ruleset, $chain, "-j DROP
");
1817 } elsif ($policy eq 'REJECT') {
1818 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1820 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1822 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1825 die "internal error
: unknown policy
'$policy'";
1829 sub ruleset_chain_add_conn_filters {
1830 my ($ruleset, $chain, $accept) = @_;
1832 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1833 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1836 sub ruleset_chain_add_input_filters {
1837 my ($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel) = @_;
1839 if ($cluster_conf->{ipset}->{blacklist}){
1840 if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist
")) {
1841 ruleset_create_chain($ruleset, "PVEFW-blacklist
");
1842 ruleset_addlog($ruleset, "PVEFW-blacklist
", 0, "DROP
: ", $loglevel) if $loglevel;
1843 ruleset_addrule($ruleset, "PVEFW-blacklist
", "-j DROP
");
1845 my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion);
1846 ruleset_addrule($ruleset, $chain, "-m set
--match-set
${ipset_chain
} src
-j PVEFW-blacklist
");
1849 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1850 if ($ipversion == 4) {
1851 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1855 if ($options->{tcpflags}) {
1856 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1860 sub ruleset_create_vm_chain {
1861 my ($ruleset, $chain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction) = @_;
1863 ruleset_create_chain($ruleset, $chain);
1864 my $accept = generate_nfqueue($options);
1866 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1867 if ($direction eq 'OUT') {
1868 ruleset_generate_rule($ruleset, $chain, $ipversion,
1869 { action => 'PVEFW-SET-ACCEPT-MARK',
1870 proto => 'udp', sport => 68, dport => 67 });
1872 ruleset_generate_rule($ruleset, $chain, $ipversion,
1873 { action => 'ACCEPT',
1874 proto => 'udp', sport => 67, dport => 68 });
1878 if ($direction eq 'OUT') {
1879 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1880 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1882 if ($ipfilter_ipset) {
1883 ruleset_addrule($ruleset, $chain, "-m set
! --match-set
$ipfilter_ipset src
-j DROP
");
1885 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1889 sub ruleset_add_group_rule {
1890 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_;
1892 my $group = $rule->{action};
1893 my $group_chain = "GROUP-
$group-$direction";
1894 if(!ruleset_chain_exist($ruleset, $group_chain)){
1895 generate_group_rules($ruleset, $cluster_conf, $group, $ipversion);
1898 if ($direction eq 'OUT' && $rule->{iface_out}) {
1899 ruleset_addrule($ruleset, $chain, "-o
$rule->{iface_out
} -j
$group_chain");
1900 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1901 ruleset_addrule($ruleset, $chain, "-i
$rule->{iface_in
} -j
$group_chain");
1903 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1906 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$action");
1909 sub ruleset_generate_vm_rules {
1910 my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_;
1912 my $lc_direction = lc($direction);
1914 my $in_accept = generate_nfqueue($options);
1916 foreach my $rule (@$rules) {
1917 next if $rule->{iface} && $rule->{iface} ne $netid;
1918 next if !$rule->{enable} || $rule->{errors};
1919 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
1921 if ($rule->{type} eq 'group') {
1922 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
1923 $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion);
1925 next if $rule->{type} ne $lc_direction;
1927 if ($direction eq 'OUT') {
1928 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1929 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
1930 undef, $cluster_conf, $vmfw_conf);
1932 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1933 { ACCEPT => $in_accept , REJECT => "PVEFW-reject
" },
1934 undef, $cluster_conf, $vmfw_conf);
1942 sub generate_nfqueue {
1945 if ($options->{ips}) {
1946 my $action = "NFQUEUE
";
1947 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1948 if (defined($3) && defined($1)) {
1949 $action .= " --queue-balance
$1:$3";
1950 } elsif (defined($1)) {
1951 $action .= " --queue-num
$1";
1954 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
1961 sub ruleset_generate_vm_ipsrules {
1962 my ($ruleset, $options, $direction, $iface) = @_;
1964 if ($options->{ips} && $direction eq 'IN') {
1965 my $nfqueue = generate_nfqueue($options);
1967 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS
")) {
1968 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1971 ruleset_addrule($ruleset, "PVEFW-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1975 sub generate_venet_rules_direction {
1976 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction, $ipversion) = @_;
1978 my $lc_direction = lc($direction);
1980 my $rules = $vmfw_conf->{rules};
1982 my $options = $vmfw_conf->{options};
1983 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1985 my $chain = "venet0-
$vmid-$direction";
1987 ruleset_create_vm_chain($ruleset, $chain, $ipversion, $options, undef, undef, $direction);
1989 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction, undef, $ipversion);
1994 if ($direction eq 'OUT') {
1995 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1997 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2000 my $accept = generate_nfqueue($options);
2001 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2002 ruleset_add_chain_policy($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2004 if ($direction eq 'OUT') {
2005 ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT
", $ipversion, {
2008 iface_in => 'venet0'});
2010 ruleset_generate_rule($ruleset, "PVEFW-VENET-IN
", $ipversion, {
2013 iface_out => 'venet0'});
2017 sub generate_tap_rules_direction {
2018 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_;
2020 my $lc_direction = lc($direction);
2022 my $rules = $vmfw_conf->{rules};
2024 my $options = $vmfw_conf->{options};
2025 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
2027 my $tapchain = "$iface-$direction";
2029 my $ipfilter_name = compute_ipfilter_ipset_name($netid);
2030 my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
2031 if $vmfw_conf->{ipset}->{$ipfilter_name};
2033 # create chain with mac and ip filter
2034 ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction);
2036 if ($options->{enable}) {
2037 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion);
2039 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
2044 if ($direction eq 'OUT') {
2045 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2047 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2050 my $accept = generate_nfqueue($options);
2051 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2052 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2054 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : 'ACCEPT';
2055 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, 'ACCEPT', $loglevel, $accept_action);
2058 # plug the tap chain to bridge chain
2059 if ($direction eq 'IN') {
2060 ruleset_addrule($ruleset, "PVEFW-FWBR-IN
",
2061 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
2063 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT
",
2064 "-m physdev
--physdev-is-bridged
--physdev-in
$iface -j
$tapchain");
2068 sub enable_host_firewall {
2069 my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
2071 my $options = $hostfw_conf->{options};
2072 my $cluster_options = $cluster_conf->{options};
2073 my $rules = $hostfw_conf->{rules};
2074 my $cluster_rules = $cluster_conf->{rules};
2076 # host inbound firewall
2077 my $chain = "PVEFW-HOST-IN
";
2078 ruleset_create_chain($ruleset, $chain);
2080 my $loglevel = get_option_log_level($options, "log_level_in
");
2082 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
2084 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2085 ruleset_chain_add_input_filters($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel);
2087 # we use RETURN because we need to check also tap rules
2088 my $accept_action = 'RETURN';
2090 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2092 # add host rules first, so that cluster wide rules can be overwritten
2093 foreach my $rule (@$rules, @$cluster_rules) {
2094 next if !$rule->{enable} || $rule->{errors};
2095 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2097 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
2100 if ($rule->{type} eq 'group') {
2101 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
2102 } elsif ($rule->{type} eq 'in') {
2103 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2104 { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2105 undef, $cluster_conf, $hostfw_conf);
2109 delete $rule->{iface_in};
2112 # allow standard traffic for management ipset (includes cluster network)
2113 my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management
", $ipversion);
2114 my $mngmntsrc = "-m set
--match-set
${mngmnt_ipset_chain
} src
";
2115 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2116 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2117 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2118 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
22 -j
$accept_action"); # SSH
2120 my ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network());
2123 if ($localnet && ($ipversion == $localnet_ver)) {
2124 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2125 ruleset_addrule($ruleset, $chain, "-s
$localnet -d
$localnet $corosync_rule");
2126 ruleset_addrule($ruleset, $chain, "-s
$localnet -m addrtype
--dst-type MULTICAST
$corosync_rule");
2129 # implement input policy
2130 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
2131 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2133 # host outbound firewall
2134 $chain = "PVEFW-HOST-OUT
";
2135 ruleset_create_chain($ruleset, $chain);
2137 $loglevel = get_option_log_level($options, "log_level_out
");
2139 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
2141 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2143 # we use RETURN because we may want to check other thigs later
2144 $accept_action = 'RETURN';
2146 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2148 # add host rules first, so that cluster wide rules can be overwritten
2149 foreach my $rule (@$rules, @$cluster_rules) {
2150 next if !$rule->{enable} || $rule->{errors};
2151 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2153 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
2155 if ($rule->{type} eq 'group') {
2156 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
2157 } elsif ($rule->{type} eq 'out') {
2158 ruleset_generate_rule($ruleset, $chain, $ipversion,
2159 $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2160 undef, $cluster_conf, $hostfw_conf);
2164 delete $rule->{iface_out};
2167 # allow standard traffic on cluster network
2168 if ($localnet && ($ipversion == $localnet_ver)) {
2169 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2170 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
22 -j
$accept_action"); # SSH
2171 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2172 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2174 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2175 ruleset_addrule($ruleset, $chain, "-d
$localnet $corosync_rule");
2176 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
$corosync_rule");
2179 # implement output policy
2180 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
2181 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2183 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
2184 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
2187 sub generate_group_rules {
2188 my ($ruleset, $cluster_conf, $group, $ipversion) = @_;
2190 my $rules = $cluster_conf->{groups}->{$group};
2193 warn "no such security group
'$group'\n";
2194 $rules = []; # create empty chain
2197 my $chain = "GROUP-
${group
}-IN
";
2199 ruleset_create_chain($ruleset, $chain);
2200 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2202 foreach my $rule (@$rules) {
2203 next if $rule->{type} ne 'in';
2204 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2205 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2206 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
2207 undef, $cluster_conf);
2210 $chain = "GROUP-
${group
}-OUT
";
2212 ruleset_create_chain($ruleset, $chain);
2213 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2215 foreach my $rule (@$rules) {
2216 next if $rule->{type} ne 'out';
2217 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2218 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
2219 # check also other tap rules later
2220 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2221 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" },
2222 undef, $cluster_conf);
2227 my $valid_netdev_names = {};
2228 for (my $i = 0; $i < $MAX_NETS; $i++) {
2229 $valid_netdev_names->{"net
$i"} = 1;
2233 my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_;
2235 my $orig_line = $line;
2239 # we can add single line comments to the end of the rule
2240 if ($line =~ s/#\s*(.*?)\s*$//) {
2241 $rule->{comment} = decode('utf8', $1);
2244 # we can disable a rule when prefixed with '|'
2246 $rule->{enable} = $line =~ s/^\|// ? 0 : 1;
2248 $line =~ s/^(\S+)\s+(\S+)\s*// ||
2249 die "unable to parse rule
: $line\n";
2251 $rule->{type} = lc($1);
2252 $rule->{action} = $2;
2254 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
2255 if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
2256 $rule->{macro} = $1;
2257 $rule->{action} = $2;
2261 while (length($line)) {
2262 if ($line =~ s/^-i (\S+)\s*//) {
2263 $rule->{iface} = $1;
2267 last if $rule->{type} eq 'group';
2269 if ($line =~ s/^-p (\S+)\s*//) {
2270 $rule->{proto} = $1;
2274 if ($line =~ s/^-dport (\S+)\s*//) {
2275 $rule->{dport} = $1;
2279 if ($line =~ s/^-sport (\S+)\s*//) {
2280 $rule->{sport} = $1;
2283 if ($line =~ s/^-source (\S+)\s*//) {
2284 $rule->{source} = $1;
2287 if ($line =~ s/^-dest (\S+)\s*//) {
2295 die "unable to parse rule parameters
: $line\n" if length($line);
2297 $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1);
2298 if ($verbose && $rule->{errors}) {
2299 warn "$prefix - errors
in rule parameters
: $orig_line\n";
2300 foreach my $p (keys %{$rule->{errors}}) {
2301 warn " $p: $rule->{errors
}->{$p}\n";
2308 sub parse_vmfw_option {
2313 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
2315 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
2318 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
2320 $value = $2 ? lc($3) : '';
2321 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2324 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
2328 die "can
't parse option '$line'\n"
2331 return ($opt, $value);
2334 sub parse_hostfw_option {
2339 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2341 if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
2344 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
2346 $value = $2 ? lc($3) : '';
2347 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
2351 die "can't parse option
'$line'\n"
2354 return ($opt, $value);
2357 sub parse_clusterfw_option {
2362 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
2365 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2369 die "can
't parse option '$line'\n"
2372 return ($opt, $value);
2376 my ($clusterfw_conf, $fw_conf, $cidr) = @_;
2378 my $alias = lc($cidr);
2379 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
2380 $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
2382 die "no such alias '$cidr'\n" if !$e;;
2384 return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr};
2387 sub parse_ip_or_cidr {
2392 if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
2395 } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
2399 die "value does not look like a valid IP address or CIDR network\n";
2402 return wantarray ? ($cidr, $ipversion) : $cidr;
2408 # we can add single line comments to the end of the line
2409 my $comment = decode('utf8
', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
2411 if ($line =~ m/^(\S+)\s(\S+)$/) {
2412 my ($name, $cidr) = ($1, $2);
2415 ($cidr, $ipversion) = parse_ip_or_cidr($cidr);
2420 ipversion => $ipversion,
2422 $data->{comment} = $comment if $comment;
2429 sub generic_fw_config_parser {
2430 my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_;
2435 my $res = $empty_conf;
2437 while (defined(my $line = <$fh>)) {
2438 next if $line =~ m/^#/;
2439 next if $line =~ m/^\s*$/;
2443 my $linenr = $fh->input_line_number();
2444 my $prefix = "$filename (line $linenr)";
2446 if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
2447 $section = 'options
';
2451 if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) {
2452 $section = 'aliases
';
2456 if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2457 $section = 'groups
';
2461 die "security group name too long\n" if length($group) > $max_group_name_length;
2462 die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/;
2465 ($section, $group, $comment) = undef;
2466 warn "$prefix: $err";
2470 $res->{$section}->{$group} = [];
2471 $res->{group_comments}->{$group} = decode('utf8
', $comment)
2476 if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) {
2481 if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2486 die "ipset name too long\n" if length($group) > $max_ipset_name_length;
2487 die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
2490 ($section, $group, $comment) = undef;
2491 warn "$prefix: $err";
2495 $res->{$section}->{$group} = [];
2496 $res->{ipset_comments}->{$group} = decode('utf8
', $comment)
2502 warn "$prefix: skip line - no section\n";
2506 if ($section eq 'options
') {
2509 if ($rule_env eq 'cluster
') {
2510 ($opt, $value) = parse_clusterfw_option($line);
2511 } elsif ($rule_env eq 'host
') {
2512 ($opt, $value) = parse_hostfw_option($line);
2514 ($opt, $value) = parse_vmfw_option($line);
2516 $res->{options}->{$opt} = $value;
2518 warn "$prefix: $@" if $@;
2519 } elsif ($section eq 'aliases
') {
2521 my $data = parse_alias($line);
2522 $res->{aliases}->{lc($data->{name})} = $data;
2524 warn "$prefix: $@" if $@;
2525 } elsif ($section eq 'rules
') {
2527 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); };
2529 warn "$prefix: $err";
2532 push @{$res->{$section}}, $rule;
2533 } elsif ($section eq 'groups
') {
2535 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group
', $verbose); };
2537 warn "$prefix: $err";
2540 push @{$res->{$section}->{$group}}, $rule;
2541 } elsif ($section eq 'ipset
') {
2542 # we can add single line comments to the end of the rule
2543 my $comment = decode('utf8
', $1) if $line =~ s/#\s*(.*?)\s*$//;
2545 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2550 if ($nomatch && !$feature_ipset_nomatch) {
2551 $errors->{nomatch} = "nomatch not supported by kernel";
2555 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2556 resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
2558 $cidr = parse_ip_or_cidr($cidr);
2563 $errors->{cidr} = $err;
2566 my $entry = { cidr => $cidr };
2567 $entry->{nomatch} = 1 if $nomatch;
2568 $entry->{comment} = $comment if $comment;
2569 $entry->{errors} = $errors if $errors;
2571 if ($verbose && $errors) {
2572 warn "$prefix - errors in ipset '$group': $line\n";
2573 foreach my $p (keys %{$errors}) {
2574 warn " $p: $errors->{$p}\n";
2578 push @{$res->{$section}->{$group}}, $entry;
2580 warn "$prefix: skip line - unknown section\n";
2588 sub parse_hostfw_config {
2589 my ($filename, $fh, $cluster_conf, $verbose) = @_;
2591 my $empty_conf = { rules => [], options => {}};
2593 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host
');
2596 sub parse_vmfw_config {
2597 my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_;
2604 ipset_comments => {},
2607 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env);
2610 sub parse_clusterfw_config {
2611 my ($filename, $fh, $verbose) = @_;
2621 group_comments => {},
2623 ipset_comments => {},
2626 return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster
');
2630 my ($code, @param) = @_;
2634 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2641 sub read_local_vm_config {
2646 my $vmdata = { openvz => $openvz, qemu => $qemu };
2648 my $vmlist = PVE::Cluster::get_vmlist();
2649 return $vmdata if !$vmlist || !$vmlist->{ids};
2650 my $ids = $vmlist->{ids};
2652 foreach my $vmid (keys %$ids) {
2653 next if !$vmid; # skip VE0
2654 my $d = $ids->{$vmid};
2655 next if !$d->{node} || $d->{node} ne $nodename;
2656 next if !$d->{type};
2657 if ($d->{type} eq 'openvz
') {
2658 if ($have_pve_manager) {
2659 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
2660 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2661 $openvz->{$vmid} = $conf;
2664 } elsif ($d->{type} eq 'qemu
') {
2665 if ($have_qemu_server) {
2666 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2667 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2668 $qemu->{$vmid} = $conf;
2677 sub load_vmfw_conf {
2678 my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_;
2682 $dir = $pvefw_conf_dir if !defined($dir);
2684 my $filename = "$dir/$vmid.fw";
2685 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2686 $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose);
2687 $vmfw_conf->{vmid} = $vmid;
2693 my $format_rules = sub {
2694 my ($rules, $allow_iface) = @_;
2698 foreach my $rule (@$rules) {
2699 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
' || $rule->{type} eq 'group
') {
2700 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2701 $raw .= uc($rule->{type});
2702 if ($rule->{macro}) {
2703 $raw .= " $rule->{macro}($rule->{action})";
2705 $raw .= " " . $rule->{action};
2707 if ($allow_iface && $rule->{iface}) {
2708 $raw .= " -i $rule->{iface}";
2711 if ($rule->{type} ne 'group
') {
2712 $raw .= " -source $rule->{source}" if $rule->{source};
2713 $raw .= " -dest $rule->{dest}" if $rule->{dest};
2714 $raw .= " -p $rule->{proto}" if $rule->{proto};
2715 $raw .= " -dport $rule->{dport}" if $rule->{dport};
2716 $raw .= " -sport $rule->{sport}" if $rule->{sport};
2719 $raw .= " # " . encode('utf8
', $rule->{comment})
2720 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2723 die "unknown rule type '$rule->{type
}'";
2730 my $format_options = sub {
2735 $raw .= "[OPTIONS]\n\n";
2736 foreach my $opt (keys %$options) {
2737 $raw .= "$opt: $options->{$opt}\n";
2744 my $format_aliases = sub {
2749 $raw .= "[ALIASES]\n\n";
2750 foreach my $k (keys %$aliases) {
2751 my $e = $aliases->{$k};
2752 $raw .= "$e->{name} $e->{cidr}";
2753 $raw .= " # " . encode('utf8
', $e->{comment})
2754 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
2762 my $format_ipsets = sub {
2767 foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
2768 if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) {
2769 my $utf8comment = encode('utf8
', $comment);
2770 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2772 $raw .= "[IPSET $ipset]\n\n";
2774 my $options = $fw_conf->{ipset}->{$ipset};
2777 foreach my $entry (@$options) {
2778 $nethash->{$entry->{cidr}} = $entry;
2781 foreach my $cidr (sort keys %$nethash) {
2782 my $entry = $nethash->{$cidr};
2783 my $line = $entry->{nomatch} ? '!' : '';
2784 $line .= $entry->{cidr};
2785 $line .= " # " . encode('utf8
', $entry->{comment})
2786 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
2796 sub save_vmfw_conf {
2797 my ($vmid, $vmfw_conf) = @_;
2801 my $options = $vmfw_conf->{options};
2802 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2804 my $aliases = $vmfw_conf->{aliases};
2805 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2807 $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset};
2809 my $rules = $vmfw_conf->{rules} || [];
2810 if ($rules && scalar(@$rules)) {
2811 $raw .= "[RULES]\n\n";
2812 $raw .= &$format_rules($rules, 1);
2816 mkdir $pvefw_conf_dir;
2818 my $filename = "$pvefw_conf_dir/$vmid.fw";
2819 PVE::Tools::file_set_contents($filename, $raw);
2822 sub read_vm_firewall_configs {
2823 my ($cluster_conf, $vmdata, $dir, $verbose) = @_;
2825 my $vmfw_configs = {};
2827 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2828 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm
', $vmid, $dir, $verbose);
2829 next if !$vmfw_conf->{options}; # skip if file does not exists
2830 $vmfw_configs->{$vmid} = $vmfw_conf;
2832 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2833 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct
', $vmid, $dir, $verbose);
2834 next if !$vmfw_conf->{options}; # skip if file does not exists
2835 $vmfw_configs->{$vmid} = $vmfw_conf;
2838 return $vmfw_configs;
2841 sub get_option_log_level {
2842 my ($options, $k) = @_;
2844 my $v = $options->{$k};
2845 $v = $default_log_level if !defined($v);
2847 return undef if $v eq '' || $v eq 'nolog
';
2849 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2851 return $v if ($v >= 0) && ($v <= 7);
2853 warn "unknown log level ($k = '$v')\n";
2858 sub generate_std_chains {
2859 my ($ruleset, $options, $ipversion) = @_;
2861 my $std_chains = $pve_std_chains->{$ipversion} || die "internal error";
2863 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2867 if ($ipversion == 4) {
2868 # same as shorewall smurflog.
2869 $chain = 'PVEFW-smurflog
';
2870 $std_chains->{$chain} = [];
2872 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2873 push @{$std_chains->{$chain}}, "-j DROP";
2876 # same as shorewall logflags action.
2877 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2878 $chain = 'PVEFW-logflags
';
2879 $std_chains->{$chain} = [];
2881 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2882 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2883 push @{$std_chains->{$chain}}, "-j DROP";
2885 foreach my $chain (keys %$std_chains) {
2886 ruleset_create_chain($ruleset, $chain);
2887 foreach my $rule (@{$std_chains->{$chain}}) {
2889 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule);
2891 ruleset_addrule($ruleset, $chain, $rule);
2897 sub generate_ipset_chains {
2898 my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; #fixme
2900 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2902 my $options = $fw_conf->{ipset}->{$ipset};
2906 foreach my $entry (@$options) {
2907 next if $entry->{errors}; # skip entries with errors
2910 if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
2911 ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
2913 ($cidr, $ver) = parse_ip_or_cidr($entry->{cidr});
2915 #http://backreference.org/2013/03/01/ipv6-address-normalization/
2917 my $ipv6 = inet_pton(AF_INET6, lc($cidr));
2918 $cidr = inet_ntop(AF_INET6, $ipv6);
2924 $nethash->{$ver}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} };
2929 foreach my $ipversion (4, 6) {
2930 my $data = $nethash->{$ipversion};
2932 my $name = compute_ipset_chain_name($fw_conf->{vmid}, $ipset, $ipversion);
2934 my $hashsize = scalar(@$options);
2935 if ($hashsize <= 64) {
2938 $hashsize = round_powerof2($hashsize);
2941 my $family = $ipversion == "6" ? "inet6" : "inet";
2943 $ipset_ruleset->{$name} = ["create $name hash:net family $family hashsize $hashsize maxelem $hashsize"];
2945 foreach my $cidr (sort keys %$data) {
2946 my $entry = $data->{$cidr};
2948 my $cmd = "add $name $cidr";
2949 if ($entry->{nomatch}) {
2950 if ($feature_ipset_nomatch) {
2951 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
2953 warn "ignore !$cidr - nomatch not supported by kernel\n";
2956 push @{$ipset_ruleset->{$name}}, $cmd;
2963 sub round_powerof2 {
2967 $int |= $int >> $_ foreach (1,2,4,8,16);
2971 sub load_clusterfw_conf {
2972 my ($filename, $verbose) = @_;
2974 $filename = $clusterfw_conf_filename if !defined($filename);
2976 my $cluster_conf = {};
2977 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2978 $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose);
2981 return $cluster_conf;
2984 sub save_clusterfw_conf {
2985 my ($cluster_conf) = @_;
2989 my $options = $cluster_conf->{options};
2990 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2992 my $aliases = $cluster_conf->{aliases};
2993 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2995 $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
2997 my $rules = $cluster_conf->{rules};
2998 if ($rules && scalar(@$rules)) {
2999 $raw .= "[RULES]\n\n";
3000 $raw .= &$format_rules($rules, 1);
3004 if ($cluster_conf->{groups}) {
3005 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
3006 my $rules = $cluster_conf->{groups}->{$group};
3007 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
3008 my $utf8comment = encode('utf8
', $comment);
3009 $raw .= "[group $group] # $utf8comment\n\n";
3011 $raw .= "[group $group]\n\n";
3014 $raw .= &$format_rules($rules, 0);
3019 mkdir $pvefw_conf_dir;
3020 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
3023 sub load_hostfw_conf {
3024 my ($cluster_conf, $filename, $verbose) = @_;
3026 $filename = $hostfw_conf_filename if !defined($filename);
3028 my $hostfw_conf = {};
3029 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3030 $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose);
3032 return $hostfw_conf;
3035 sub save_hostfw_conf {
3036 my ($hostfw_conf) = @_;
3040 my $options = $hostfw_conf->{options};
3041 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3043 my $rules = $hostfw_conf->{rules};
3044 if ($rules && scalar(@$rules)) {
3045 $raw .= "[RULES]\n\n";
3046 $raw .= &$format_rules($rules, 1);
3050 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
3054 my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_;
3058 if ($vmdata) { # test mode
3059 my $testdir = $vmdata->{testdir} || die "no test directory specified";
3060 my $filename = "$testdir/cluster.fw";
3061 $cluster_conf = load_clusterfw_conf($filename, $verbose);
3063 $filename = "$testdir/host.fw";
3064 $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose);
3066 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose);
3067 } else { # normal operation
3068 $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf;
3070 $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf;
3072 $vmdata = read_local_vm_config();
3073 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose);
3076 my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose);
3077 my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose);
3079 return ($ruleset, $ipset_ruleset, $rulesetv6);
3082 sub compile_iptables_filter {
3083 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_;
3085 $cluster_conf->{ipset}->{venet0} = [];
3086 my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0
', $ipversion);
3089 if ($cluster_conf->{aliases}->{local_network}) {
3090 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3092 $localnet = local_network() || '127.0.0.0/8';
3093 $cluster_conf->{aliases}->{local_network} = { cidr => $localnet };
3096 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
3098 return ({}, {}) if !$cluster_conf->{options}->{enable};
3102 ruleset_create_chain($ruleset, "PVEFW-INPUT");
3103 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
3105 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3107 my $hostfw_options = $hostfw_conf->{options} || {};
3109 # fixme: what log level should we use here?
3110 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
3112 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT");
3115 ruleset_create_chain($ruleset, "PVEFW-VENET-OUT");
3116 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3117 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3119 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
3120 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3122 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN");
3124 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
3125 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT");
3127 ruleset_create_chain($ruleset, "PVEFW-VENET-IN");
3128 ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3130 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3132 generate_std_chains($ruleset, $hostfw_options, $ipversion);
3134 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
3136 my $ipset_ruleset = {};
3138 if ($hostfw_enable) {
3139 eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
3140 warn $@ if $@; # just to be sure - should not happen
3143 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3145 # generate firewall rules for QEMU VMs
3146 foreach my $vmid (keys %{$vmdata->{qemu}}) {
3148 my $conf = $vmdata->{qemu}->{$vmid};
3149 my $vmfw_conf = $vmfw_configs->{$vmid};
3150 return if !$vmfw_conf;
3152 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3154 foreach my $netid (keys %$conf) {
3155 next if $netid !~ m/^net(\d+)$/;
3156 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3157 next if !$net->{firewall};
3158 my $iface = "tap${vmid}i$1";
3160 my $macaddr = $net->{macaddr};
3161 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3162 $vmfw_conf, $vmid, 'IN
', $ipversion);
3163 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3164 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3167 warn $@ if $@; # just to be sure - should not happen
3170 # generate firewall rules for OpenVZ containers
3171 foreach my $vmid (keys %{$vmdata->{openvz}}) {
3173 my $conf = $vmdata->{openvz}->{$vmid};
3175 my $vmfw_conf = $vmfw_configs->{$vmid};
3176 return if !$vmfw_conf;
3178 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3180 if ($vmfw_conf->{options}->{enable}) {
3181 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
3182 my $ip = $conf->{ip_address}->{value};
3187 foreach my $singleip (split(',', $ip)) {
3188 my $singleip_ver = parse_address_list($singleip); # make sure we have a valid $ip list
3189 push @{$cluster_conf->{ipset}->{venet0}}, { cidr => $singleip };
3190 push @ips, $singleip if $singleip_ver == $ipversion;
3194 my $ip_list = join(',', @ips);
3195 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'IN
', $ipversion);
3196 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'OUT
', $ipversion);
3201 if ($conf->{netif} && $conf->{netif}->{value}) {
3202 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
3203 foreach my $netid (keys %$netif) {
3204 my $d = $netif->{$netid};
3205 my $bridge = $d->{bridge};
3206 next if !$bridge || $bridge !~ m/^vmbr\d+(v(\d+))?f$/; # firewall enabled ?
3207 my $macaddr = $d->{mac};
3208 my $iface = $d->{host_ifname};
3209 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3210 $vmfw_conf, $vmid, 'IN
', $ipversion);
3211 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3212 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3216 warn $@ if $@; # just to be sure - should not happen
3219 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
3220 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS");
3223 generate_ipset_chains($ipset_ruleset, undef, $cluster_conf);
3225 return ($ruleset, $ipset_ruleset);
3228 sub get_ruleset_status {
3229 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
3231 my $statushash = {};
3233 foreach my $chain (sort keys %$ruleset) {
3234 my $sig = &$digest_fn($ruleset->{$chain});
3236 $statushash->{$chain}->{sig} = $sig;
3238 my $oldsig = $active_chains->{$chain};
3239 if (!defined($oldsig)) {
3240 $statushash->{$chain}->{action} = 'create
';
3242 if ($oldsig eq $sig) {
3243 $statushash->{$chain}->{action} = 'exists';
3245 $statushash->{$chain}->{action} = 'update
';
3248 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
3249 foreach my $cmd (@{$ruleset->{$chain}}) {
3250 print "\t$cmd\n" if $verbose;
3254 foreach my $chain (sort keys %$active_chains) {
3255 if (!defined($ruleset->{$chain})) {
3256 my $sig = $active_chains->{$chain};
3257 $statushash->{$chain}->{action} = 'delete';
3258 $statushash->{$chain}->{sig} = $sig;
3259 print "delete $chain ($sig)\n" if $verbose;
3266 sub print_sig_rule {
3267 my ($chain, $sig) = @_;
3269 # We just use this to store a SHA1 checksum used to detect changes
3270 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
3273 sub get_ruleset_cmdlist {
3274 my ($ruleset, $verbose, $iptablescmd) = @_;
3276 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
3278 my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
3279 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
3281 # create missing chains first
3282 foreach my $chain (sort keys %$ruleset) {
3283 my $stat = $statushash->{$chain};
3284 die "internal error" if !$stat;
3285 next if $stat->{action} ne 'create
';
3287 $cmdlist .= ":$chain - [0:0]\n";
3290 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3291 my $chain = "PVEFW-$h";
3292 if ($ruleset->{$chain} && !$hooks->{$h}) {
3293 $cmdlist .= "-A $h -j $chain\n";
3297 foreach my $chain (sort keys %$ruleset) {
3298 my $stat = $statushash->{$chain};
3299 die "internal error" if !$stat;
3301 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
3302 $cmdlist .= "-F $chain\n";
3303 foreach my $cmd (@{$ruleset->{$chain}}) {
3304 $cmdlist .= "$cmd\n";
3306 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
3307 } elsif ($stat->{action
} eq 'delete') {
3308 die "internal error"; # this should not happen
3309 } elsif ($stat->{action
} eq 'exists') {
3312 die "internal error - unknown status '$stat->{action}'";
3316 foreach my $chain (keys %$statushash) {
3317 next if $statushash->{$chain}->{action
} ne 'delete';
3318 $cmdlist .= "-F $chain\n";
3320 foreach my $chain (keys %$statushash) {
3321 next if $statushash->{$chain}->{action
} ne 'delete';
3322 next if $chain eq 'PVEFW-INPUT';
3323 next if $chain eq 'PVEFW-OUTPUT';
3324 next if $chain eq 'PVEFW-FORWARD';
3325 $cmdlist .= "-X $chain\n";
3328 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
3330 $cmdlist .= "COMMIT\n";
3332 return wantarray ?
($cmdlist, $changes) : $cmdlist;
3335 sub get_ipset_cmdlist
{
3336 my ($ruleset, $verbose) = @_;
3340 my $delete_cmdlist = "";
3342 my $active_chains = ipset_get_chains
();
3343 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
3345 # remove stale _swap chains
3346 foreach my $chain (keys %$active_chains) {
3347 if ($chain =~ m/^PVEFW-\S+_swap$/) {
3348 $cmdlist .= "destroy $chain\n";
3352 foreach my $chain (keys %$ruleset) {
3353 my $stat = $statushash->{$chain};
3354 die "internal error" if !$stat;
3356 if ($stat->{action
} eq 'create') {
3357 foreach my $cmd (@{$ruleset->{$chain}}) {
3358 $cmdlist .= "$cmd\n";
3363 foreach my $chain (keys %$ruleset) {
3364 my $stat = $statushash->{$chain};
3365 die "internal error" if !$stat;
3367 if ($stat->{action
} eq 'update') {
3368 my $chain_swap = $chain."_swap";
3370 foreach my $cmd (@{$ruleset->{$chain}}) {
3371 $cmd =~ s/$chain/$chain_swap/;
3372 $cmdlist .= "$cmd\n";
3374 $cmdlist .= "swap $chain_swap $chain\n";
3375 $cmdlist .= "flush $chain_swap\n";
3376 $cmdlist .= "destroy $chain_swap\n";
3380 # the remove unused chains
3381 foreach my $chain (keys %$statushash) {
3382 next if $statushash->{$chain}->{action
} ne 'delete';
3384 $delete_cmdlist .= "flush $chain\n";
3385 $delete_cmdlist .= "destroy $chain\n";
3388 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
3390 return ($cmdlist, $delete_cmdlist, $changes);
3394 my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_;
3396 enable_bridge_firewall
();
3398 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
3399 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
3401 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
3402 my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist
($rulesetv6, $verbose, "ip6tables");
3405 if ($ipset_changes) {
3406 print "ipset changes:\n";
3407 print $ipset_create_cmdlist if $ipset_create_cmdlist;
3408 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
3412 print "iptables changes:\n";
3417 print "ip6tables changes:\n";
3422 my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1";
3423 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_create_cmdlist || '');
3425 ipset_restore_cmdlist
($ipset_create_cmdlist);
3427 $tmpfile = "$pve_fw_status_dir/ip4cmdlist";
3428 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlist || '');
3430 iptables_restore_cmdlist
($cmdlist);
3432 $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
3433 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlistv6 || '');
3435 ip6tables_restore_cmdlist
($cmdlistv6);
3437 $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
3438 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_delete_cmdlist || '');
3440 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
3442 # test: re-read status and check if everything is up to date
3443 my $active_chains = iptables_get_chains
();
3444 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
3447 foreach my $chain (sort keys %$ruleset) {
3448 my $stat = $statushash->{$chain};
3449 if ($stat->{action
} ne 'exists') {
3450 warn "unable to update chain '$chain'\n";
3455 my $active_chainsv6 = iptables_get_chains
("ip6tables");
3456 my $statushashv6 = get_ruleset_status
($rulesetv6, $active_chainsv6, \
&iptables_chain_digest
, 0);
3458 foreach my $chain (sort keys %$rulesetv6) {
3459 my $stat = $statushashv6->{$chain};
3460 if ($stat->{action
} ne 'exists') {
3461 warn "unable to update chain '$chain'\n";
3466 die "unable to apply firewall changes\n" if $errors;
3468 update_nf_conntrack_max
($hostfw_conf);
3470 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
3474 sub update_nf_conntrack_max
{
3475 my ($hostfw_conf) = @_;
3477 my $max = 65536; # reasonable default
3479 my $options = $hostfw_conf->{options
} || {};
3481 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
3482 $max = $options->{nf_conntrack_max
};
3483 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
3486 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
3487 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
3489 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
3491 if ($current != $max) {
3492 my $hashsize = int($max/4);
3493 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
3494 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
3498 sub update_nf_conntrack_tcp_timeout_established
{
3499 my ($hostfw_conf) = @_;
3501 my $options = $hostfw_conf->{options
} || {};
3503 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
3505 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
3508 sub remove_pvefw_chains
{
3510 PVE
::Firewall
::remove_pvefw_chains_iptables
("iptables");
3511 PVE
::Firewall
::remove_pvefw_chains_iptables
("ip6tables");
3512 PVE
::Firewall
::remove_pvefw_chains_ipset
();
3516 sub remove_pvefw_chains_iptables
{
3517 my ($iptablescmd) = @_;
3519 my ($chash, $hooks) = iptables_get_chains
($iptablescmd);
3520 my $cmdlist = "*filter\n";
3522 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3524 $cmdlist .= "-D $h -j PVEFW-$h\n";
3528 foreach my $chain (keys %$chash) {
3529 $cmdlist .= "-F $chain\n";
3532 foreach my $chain (keys %$chash) {
3533 $cmdlist .= "-X $chain\n";
3535 $cmdlist .= "COMMIT\n";
3537 if($iptablescmd eq "ip6tables") {
3538 ip6tables_restore_cmdlist
($cmdlist);
3540 iptables_restore_cmdlist
($cmdlist);
3544 sub remove_pvefw_chains_ipset
{
3546 my $ipset_chains = ipset_get_chains
();
3550 foreach my $chain (keys %$ipset_chains) {
3551 $cmdlist .= "flush $chain\n";
3552 $cmdlist .= "destroy $chain\n";
3555 ipset_restore_cmdlist
($cmdlist) if $cmdlist;
3559 my $cluster_conf = load_clusterfw_conf
();
3560 my $cluster_options = $cluster_conf->{options
};
3561 my $enable = $cluster_options->{enable
};
3565 # load required modules here
3571 my $cluster_conf = load_clusterfw_conf
();
3572 my $cluster_options = $cluster_conf->{options
};
3574 if (!$cluster_options->{enable
}) {
3575 PVE
::Firewall
::remove_pvefw_chains
();
3579 my $hostfw_conf = load_hostfw_conf
($cluster_conf);
3581 my ($ruleset, $ipset_ruleset, $rulesetv6) = compile
($cluster_conf, $hostfw_conf);
3583 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6);