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',
1114 type
=> 'string', format
=> 'pve-fw-port-spec',
1118 type
=> 'string', format
=> 'pve-fw-port-spec',
1127 sub add_rule_properties
{
1128 my ($properties) = @_;
1130 foreach my $k (keys %$rule_properties) {
1131 my $h = $rule_properties->{$k};
1132 # copy data, so that we can modify later without side effects
1133 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
1139 sub delete_rule_properties
{
1140 my ($rule, $delete_str) = @_;
1142 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
1143 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
1144 if !defined($rule_properties->{$opt});
1145 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
1146 if $opt eq 'type' || $opt eq 'action';
1147 delete $rule->{$opt};
1153 my $apply_macro = sub {
1154 my ($macro_name, $param, $verify, $ipversion) = @_;
1156 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1157 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1159 if ($ipversion && ($ipversion == 6) && $pve_ipv6fw_macros->{$macro_name}) {
1160 $macro_rules = $pve_ipv6fw_macros->{$macro_name};
1165 foreach my $templ (@$macro_rules) {
1167 my $param_used = {};
1168 foreach my $k (keys %$templ) {
1169 my $v = $templ->{$k};
1170 if ($v eq 'PARAM') {
1172 $param_used->{$k} = 1;
1173 } elsif ($v eq 'DEST') {
1174 $v = $param->{dest
};
1175 $param_used->{dest
} = 1;
1176 } elsif ($v eq 'SOURCE') {
1177 $v = $param->{source
};
1178 $param_used->{source
} = 1;
1182 my $msg = "missing parameter '$k' in macro '$macro_name'";
1183 raise_param_exc
({ macro => $msg }) if $verify;
1188 foreach my $k (keys %$param) {
1189 next if $k eq 'macro';
1190 next if !defined($param->{$k});
1191 next if $param_used->{$k};
1192 if (defined($rule->{$k})) {
1193 if ($rule->{$k} ne $param->{$k}) {
1194 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
1195 raise_param_exc
({ $k => $msg }) if $verify;
1199 $rule->{$k} = $param->{$k};
1202 push @$rules, $rule;
1208 my $rule_env_iface_lookup = {
1217 my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_;
1219 my $allow_groups = $rule_env eq 'group' ?
0 : 1;
1221 my $allow_iface = $rule_env_iface_lookup->{$rule_env};
1222 die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen
1224 my $errors = $rule->{errors
} || {};
1226 my $error_count = 0;
1228 my $add_error = sub {
1229 my ($param, $msg) = @_;
1231 raise_param_exc
({ $param => $msg }) if !$noerr;
1233 $errors->{$param} = $msg if !$errors->{$param};
1237 my $set_ip_version = sub {
1240 die "detected mixed ipv4/ipv6 adresses in rule\n"
1241 if $ipversion && ($vers != $ipversion);
1246 my $check_ipset_or_alias_property = sub {
1247 my ($name, $expected_ipversion) = @_;
1249 if (my $value = $rule->{$name}) {
1250 if ($value =~ m/^\+/) {
1251 if ($value =~ m/^\+(${ipset_name_pattern})$/) {
1252 &$add_error($name, "no such ipset '$1'")
1253 if !($cluster_conf->{ipset
}->{$1} || ($fw_conf && $fw_conf->{ipset
}->{$1}));
1256 &$add_error($name, "invalid ipset name '$value'");
1258 } elsif ($value =~ m/^${ip_alias_pattern}$/){
1259 my $alias = lc($value);
1260 &$add_error($name, "no such alias '$value'")
1261 if !($cluster_conf->{aliases
}->{$alias} || ($fw_conf && $fw_conf->{aliases
}->{$alias}));
1262 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1263 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1265 &$set_ip_version($e->{ipversion
});
1270 my $type = $rule->{type
};
1271 my $action = $rule->{action
};
1273 &$add_error('type', "missing property") if !$type;
1274 &$add_error('action', "missing property") if !$action;
1277 if ($type eq 'in' || $type eq 'out') {
1278 &$add_error('action', "unknown action '$action'")
1279 if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/);
1280 } elsif ($type eq 'group') {
1281 &$add_error('type', "security groups not allowed")
1283 &$add_error('action', "invalid characters in security group name")
1284 if $action && ($action !~ m/^${security_group_name_pattern}$/);
1286 &$add_error('type', "unknown rule type '$type'");
1290 if ($rule->{iface
}) {
1291 &$add_error('type', "parameter -i not allowed for this rule type")
1293 eval { PVE
::JSONSchema
::pve_verify_iface
($rule->{iface
}); };
1294 &$add_error('iface', $@) if $@;
1295 if ($rule_env eq 'vm') {
1296 &$add_error('iface', "value does not match the regex pattern 'net\\d+'")
1297 if $rule->{iface
} !~ m/^net(\d+)$/;
1298 } elsif ($rule_env eq 'ct') {
1299 &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'")
1300 if $rule->{iface
} !~ m/^(venet|eth(\d+))$/;
1304 if ($rule->{macro}) {
1305 if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) {
1306 $rule->{macro} = $preferred_name;
1308 &$add_error('macro', "unknown macro '$rule->{macro}'");
1312 if ($rule->{proto
}) {
1313 eval { pve_fw_verify_protocol_spec
($rule->{proto
}); };
1314 &$add_error('proto', $@) if $@;
1315 &$set_ip_version(4) if $rule->{proto
} eq 'icmp';
1316 &$set_ip_version(6) if $rule->{proto
} eq 'icmpv6';
1319 if ($rule->{dport
}) {
1320 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1321 &$add_error('dport', $@) if $@;
1322 &$add_error('proto', "missing property - 'dport' requires this property")
1326 if ($rule->{sport
}) {
1327 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1328 &$add_error('sport', $@) if $@;
1329 &$add_error('proto', "missing property - 'sport' requires this property")
1333 if ($rule->{source
}) {
1335 my $source_ipversion = parse_address_list
($rule->{source
});
1336 &$set_ip_version($source_ipversion);
1338 &$add_error('source', $@) if $@;
1339 &$check_ipset_or_alias_property('source', $ipversion);
1342 if ($rule->{dest
}) {
1344 my $dest_ipversion = parse_address_list
($rule->{dest
});
1345 &$set_ip_version($dest_ipversion);
1347 &$add_error('dest', $@) if $@;
1348 &$check_ipset_or_alias_property('dest', $ipversion);
1351 $rule->{ipversion
} = $ipversion if $ipversion;
1353 if ($rule->{macro} && !$error_count) {
1354 eval { &$apply_macro($rule->{macro}, $rule, 1, $ipversion); };
1356 if (ref($err) eq "PVE::Exception" && $err->{errors
}) {
1357 my $eh = $err->{errors
};
1358 foreach my $p (keys %$eh) {
1359 &$add_error($p, $eh->{$p});
1362 &$add_error('macro', "$err");
1367 $rule->{errors
} = $errors if $error_count;
1372 sub copy_rule_data
{
1373 my ($rule, $param) = @_;
1375 foreach my $k (keys %$rule_properties) {
1376 if (defined(my $v = $param->{$k})) {
1377 if ($v eq '' || $v eq '-') {
1388 sub rules_modify_permissions
{
1389 my ($rule_env) = @_;
1391 if ($rule_env eq 'host') {
1393 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
1395 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1397 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1399 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1401 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
1408 sub rules_audit_permissions
{
1409 my ($rule_env) = @_;
1411 if ($rule_env eq 'host') {
1413 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
1415 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1417 check
=> ['perm', '/', [ 'Sys.Audit' ]],
1419 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1421 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1429 my $bridge_firewall_enabled = 0;
1431 sub enable_bridge_firewall
{
1433 return if $bridge_firewall_enabled; # only once
1435 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1436 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1438 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1439 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1441 $bridge_firewall_enabled = 1;
1444 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1446 sub iptables_restore_cmdlist
{
1449 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1452 sub ip6tables_restore_cmdlist
{
1455 run_command
("/sbin/ip6tables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1458 sub ipset_restore_cmdlist
{
1461 run_command
("/sbin/ipset restore", input
=> $cmdlist, errmsg
=> "ipset_restore_cmdlist");
1464 sub iptables_get_chains
{
1465 my ($iptablescmd) = @_;
1467 $iptablescmd = "iptables" if !$iptablescmd;
1471 # check what chains we want to track
1472 my $is_pvefw_chain = sub {
1475 return 1 if $name =~ m/^PVEFW-\S+$/;
1477 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
1479 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
1481 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
1483 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/;
1484 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
1496 return if $line =~ m/^#/;
1497 return if $line =~ m/^\s*$/;
1499 if ($line =~ m/^\*(\S+)$/) {
1504 return if $table ne 'filter';
1506 if ($line =~ m/^:(\S+)\s/) {
1508 return if !&$is_pvefw_chain($chain);
1509 $res->{$chain} = "unknown";
1510 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1511 my ($chain, $sig) = ($1, $2);
1512 return if !&$is_pvefw_chain($chain);
1513 $res->{$chain} = $sig;
1514 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1517 # simply ignore the rest
1522 run_command
("/sbin/$iptablescmd-save", outfunc
=> $parser);
1524 return wantarray ?
($res, $hooks) : $res;
1527 sub iptables_chain_digest
{
1529 my $digest = Digest
::SHA-
>new('sha1');
1530 foreach my $rule (@$rules) { # order is important
1531 $digest->add($rule);
1533 return $digest->b64digest;
1536 sub ipset_chain_digest
{
1539 my $digest = Digest
::SHA-
>new('sha1');
1540 foreach my $rule (sort @$rules) { # note: sorted
1541 $digest->add($rule);
1543 return $digest->b64digest;
1546 sub ipset_get_chains
{
1554 return if $line =~ m/^#/;
1555 return if $line =~ m/^\s*$/;
1556 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1558 $line =~ s/\s+$//; # delete trailing white space
1559 push @{$chains->{$chain}}, $line;
1561 # simply ignore the rest
1566 run_command
("/sbin/ipset save", outfunc
=> $parser);
1568 # compute digest for each chain
1569 foreach my $chain (keys %$chains) {
1570 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1576 sub ruleset_generate_cmdstr
{
1577 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1579 return if defined($rule->{enable
}) && !$rule->{enable
};
1580 return if $rule->{errors
};
1582 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1584 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1585 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1589 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1590 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1592 my $source = $rule->{source
};
1593 my $dest = $rule->{dest
};
1596 if ($source =~ m/^\+/) {
1597 if ($source =~ m/^\+(${ipset_name_pattern})$/) {
1599 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1600 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1601 push @cmd, "-m set --match-set ${ipset_chain} src";
1602 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1603 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1604 push @cmd, "-m set --match-set ${ipset_chain} src";
1606 die "no such ipset '$name'\n";
1609 die "invalid security group name '$source'\n";
1611 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1612 my $alias = lc($source);
1613 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1614 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1615 die "no such alias '$source'\n" if !$e;
1616 push @cmd, "-s $e->{cidr}";
1617 } elsif ($source =~ m/\-/){
1618 push @cmd, "-m iprange --src-range $source";
1620 push @cmd, "-s $source";
1625 if ($dest =~ m/^\+/) {
1626 if ($dest =~ m/^\+(${ipset_name_pattern})$/) {
1628 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1629 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1630 push @cmd, "-m set --match-set ${ipset_chain} dst";
1631 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1632 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1633 push @cmd, "-m set --match-set ${ipset_chain} dst";
1635 die "no such ipset '$name'\n";
1638 die "invalid security group name '$dest'\n";
1640 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1641 my $alias = lc($dest);
1642 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1643 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1644 die "no such alias '$dest'\n" if !$e;
1645 push @cmd, "-d $e->{cidr}";
1646 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1647 push @cmd, "-m iprange --dst-range $dest";
1649 push @cmd, "-d $dest";
1653 if ($rule->{proto
}) {
1654 push @cmd, "-p $rule->{proto}";
1657 $multiport++ if $nbdport > 1;
1658 $multiport++ if $nbsport > 1;
1660 push @cmd, "--match multiport" if $multiport;
1662 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1663 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1665 if ($rule->{dport
}) {
1666 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1667 # Note: we use dport to store --icmp-type
1668 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
1669 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1670 } elsif ($rule->{proto
} && $rule->{proto
} eq 'icmpv6') {
1671 # Note: we use dport to store --icmpv6-type
1672 die "unknown icmpv6-type '$rule->{dport}'\n" if !defined($icmpv6_type_names->{$rule->{dport
}});
1673 push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}";
1676 if ($multiport == 2) {
1677 push @cmd, "--ports $rule->{dport}";
1679 push @cmd, "--dports $rule->{dport}";
1682 push @cmd, "--dport $rule->{dport}";
1687 if ($rule->{sport
}) {
1689 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1691 push @cmd, "--sport $rule->{sport}";
1694 } elsif ($rule->{dport
} || $rule->{sport
}) {
1695 die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport
};
1696 die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport
};
1699 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1701 if (my $action = $rule->{action
}) {
1702 $action = $actions->{$action} if defined($actions->{$action});
1703 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1704 push @cmd, $goto ?
"-g $action" : "-j $action";
1707 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1710 sub ruleset_generate_rule
{
1711 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1715 if ($rule->{macro}) {
1716 $rules = &$apply_macro($rule->{macro}, $rule, 0, $ipversion);
1721 # update all or nothing
1724 foreach my $tmp (@$rules) {
1725 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) {
1726 push @cmds, $cmdstr;
1730 foreach my $cmdstr (@cmds) {
1731 ruleset_addrule
($ruleset, $chain, $cmdstr);
1735 sub ruleset_generate_rule_insert
{
1736 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto) = @_;
1738 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1740 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $rule, $actions, $goto)) {
1741 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1745 sub ruleset_create_chain
{
1746 my ($ruleset, $chain) = @_;
1748 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1749 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1751 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1753 $ruleset->{$chain} = [];
1756 sub ruleset_chain_exist
{
1757 my ($ruleset, $chain) = @_;
1759 return $ruleset->{$chain} ?
1 : undef;
1762 sub ruleset_addrule
{
1763 my ($ruleset, $chain, $rule) = @_;
1765 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1767 push @{$ruleset->{$chain}}, "-A $chain $rule";
1770 sub ruleset_insertrule
{
1771 my ($ruleset, $chain, $rule) = @_;
1773 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1775 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1778 sub get_log_rule_base
{
1779 my ($chain, $vmid, $msg, $loglevel) = @_;
1781 die "internal error - no log level" if !defined($loglevel);
1783 $vmid = 0 if !defined($vmid);
1785 # Note: we use special format for prefix to pass further
1786 # info to log daemon (VMID, LOGVELEL and CHAIN)
1788 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1791 sub ruleset_addlog {
1792 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1794 return if !defined($loglevel);
1796 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1798 $logrule = "$rule $logrule" if defined($rule);
1800 ruleset_addrule($ruleset, $chain, $logrule);
1803 sub ruleset_add_chain_policy {
1804 my ($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action) = @_;
1806 if ($policy eq 'ACCEPT') {
1808 ruleset_generate_rule($ruleset, $chain, $ipversion, { action => 'ACCEPT' },
1809 { ACCEPT => $accept_action});
1811 } elsif ($policy eq 'DROP') {
1813 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1815 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1817 ruleset_addrule($ruleset, $chain, "-j DROP
");
1818 } elsif ($policy eq 'REJECT') {
1819 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1821 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1823 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1826 die "internal error
: unknown policy
'$policy'";
1830 sub ruleset_chain_add_conn_filters {
1831 my ($ruleset, $chain, $accept) = @_;
1833 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1834 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1837 sub ruleset_chain_add_input_filters {
1838 my ($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel) = @_;
1840 if ($cluster_conf->{ipset}->{blacklist}){
1841 if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist
")) {
1842 ruleset_create_chain($ruleset, "PVEFW-blacklist
");
1843 ruleset_addlog($ruleset, "PVEFW-blacklist
", 0, "DROP
: ", $loglevel) if $loglevel;
1844 ruleset_addrule($ruleset, "PVEFW-blacklist
", "-j DROP
");
1846 my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion);
1847 ruleset_addrule($ruleset, $chain, "-m set
--match-set
${ipset_chain
} src
-j PVEFW-blacklist
");
1850 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1851 if ($ipversion == 4) {
1852 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1856 if ($options->{tcpflags}) {
1857 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1861 sub ruleset_create_vm_chain {
1862 my ($ruleset, $chain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction) = @_;
1864 ruleset_create_chain($ruleset, $chain);
1865 my $accept = generate_nfqueue($options);
1867 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1868 if ($direction eq 'OUT') {
1869 ruleset_generate_rule($ruleset, $chain, $ipversion,
1870 { action => 'PVEFW-SET-ACCEPT-MARK',
1871 proto => 'udp', sport => 68, dport => 67 });
1873 ruleset_generate_rule($ruleset, $chain, $ipversion,
1874 { action => 'ACCEPT',
1875 proto => 'udp', sport => 67, dport => 68 });
1879 if ($direction eq 'OUT') {
1880 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1881 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1883 if ($ipfilter_ipset) {
1884 ruleset_addrule($ruleset, $chain, "-m set
! --match-set
$ipfilter_ipset src
-j DROP
");
1886 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1890 sub ruleset_add_group_rule {
1891 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_;
1893 my $group = $rule->{action};
1894 my $group_chain = "GROUP-
$group-$direction";
1895 if(!ruleset_chain_exist($ruleset, $group_chain)){
1896 generate_group_rules($ruleset, $cluster_conf, $group, $ipversion);
1899 if ($direction eq 'OUT' && $rule->{iface_out}) {
1900 ruleset_addrule($ruleset, $chain, "-o
$rule->{iface_out
} -j
$group_chain");
1901 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1902 ruleset_addrule($ruleset, $chain, "-i
$rule->{iface_in
} -j
$group_chain");
1904 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1907 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$action");
1910 sub ruleset_generate_vm_rules {
1911 my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_;
1913 my $lc_direction = lc($direction);
1915 my $in_accept = generate_nfqueue($options);
1917 foreach my $rule (@$rules) {
1918 next if $rule->{iface} && $rule->{iface} ne $netid;
1919 next if !$rule->{enable} || $rule->{errors};
1920 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
1922 if ($rule->{type} eq 'group') {
1923 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
1924 $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion);
1926 next if $rule->{type} ne $lc_direction;
1928 if ($direction eq 'OUT') {
1929 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1930 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
1931 undef, $cluster_conf, $vmfw_conf);
1933 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1934 { ACCEPT => $in_accept , REJECT => "PVEFW-reject
" },
1935 undef, $cluster_conf, $vmfw_conf);
1943 sub generate_nfqueue {
1946 if ($options->{ips}) {
1947 my $action = "NFQUEUE
";
1948 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1949 if (defined($3) && defined($1)) {
1950 $action .= " --queue-balance
$1:$3";
1951 } elsif (defined($1)) {
1952 $action .= " --queue-num
$1";
1955 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
1962 sub ruleset_generate_vm_ipsrules {
1963 my ($ruleset, $options, $direction, $iface) = @_;
1965 if ($options->{ips} && $direction eq 'IN') {
1966 my $nfqueue = generate_nfqueue($options);
1968 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS
")) {
1969 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1972 ruleset_addrule($ruleset, "PVEFW-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1976 sub generate_venet_rules_direction {
1977 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction, $ipversion) = @_;
1979 my $lc_direction = lc($direction);
1981 my $rules = $vmfw_conf->{rules};
1983 my $options = $vmfw_conf->{options};
1984 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1986 my $chain = "venet0-
$vmid-$direction";
1988 ruleset_create_vm_chain($ruleset, $chain, $ipversion, $options, undef, undef, $direction);
1990 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction, undef, $ipversion);
1995 if ($direction eq 'OUT') {
1996 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1998 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2001 my $accept = generate_nfqueue($options);
2002 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2003 ruleset_add_chain_policy($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2005 if ($direction eq 'OUT') {
2006 ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT
", $ipversion, {
2009 iface_in => 'venet0'});
2011 ruleset_generate_rule($ruleset, "PVEFW-VENET-IN
", $ipversion, {
2014 iface_out => 'venet0'});
2018 sub generate_tap_rules_direction {
2019 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_;
2021 my $lc_direction = lc($direction);
2023 my $rules = $vmfw_conf->{rules};
2025 my $options = $vmfw_conf->{options};
2026 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
2028 my $tapchain = "$iface-$direction";
2030 my $ipfilter_name = compute_ipfilter_ipset_name($netid);
2031 my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
2032 if $vmfw_conf->{ipset}->{$ipfilter_name};
2034 # create chain with mac and ip filter
2035 ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction);
2037 if ($options->{enable}) {
2038 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion);
2040 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
2045 if ($direction eq 'OUT') {
2046 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2048 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2051 my $accept = generate_nfqueue($options);
2052 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2053 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2055 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : 'ACCEPT';
2056 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, 'ACCEPT', $loglevel, $accept_action);
2059 # plug the tap chain to bridge chain
2060 if ($direction eq 'IN') {
2061 ruleset_addrule($ruleset, "PVEFW-FWBR-IN
",
2062 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
2064 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT
",
2065 "-m physdev
--physdev-is-bridged
--physdev-in
$iface -j
$tapchain");
2069 sub enable_host_firewall {
2070 my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
2072 my $options = $hostfw_conf->{options};
2073 my $cluster_options = $cluster_conf->{options};
2074 my $rules = $hostfw_conf->{rules};
2075 my $cluster_rules = $cluster_conf->{rules};
2077 # host inbound firewall
2078 my $chain = "PVEFW-HOST-IN
";
2079 ruleset_create_chain($ruleset, $chain);
2081 my $loglevel = get_option_log_level($options, "log_level_in
");
2083 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
2085 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2086 ruleset_chain_add_input_filters($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel);
2088 # we use RETURN because we need to check also tap rules
2089 my $accept_action = 'RETURN';
2091 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2093 # add host rules first, so that cluster wide rules can be overwritten
2094 foreach my $rule (@$rules, @$cluster_rules) {
2095 next if !$rule->{enable} || $rule->{errors};
2096 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2098 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
2101 if ($rule->{type} eq 'group') {
2102 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
2103 } elsif ($rule->{type} eq 'in') {
2104 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2105 { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2106 undef, $cluster_conf, $hostfw_conf);
2110 delete $rule->{iface_in};
2113 # allow standard traffic for management ipset (includes cluster network)
2114 my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management
", $ipversion);
2115 my $mngmntsrc = "-m set
--match-set
${mngmnt_ipset_chain
} src
";
2116 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2117 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2118 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2119 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
22 -j
$accept_action"); # SSH
2121 my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
2122 my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion};
2125 if ($localnet && ($ipversion == $localnet_ver)) {
2126 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2127 ruleset_addrule($ruleset, $chain, "-s
$localnet -d
$localnet $corosync_rule");
2128 ruleset_addrule($ruleset, $chain, "-s
$localnet -m addrtype
--dst-type MULTICAST
$corosync_rule");
2131 # implement input policy
2132 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
2133 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2135 # host outbound firewall
2136 $chain = "PVEFW-HOST-OUT
";
2137 ruleset_create_chain($ruleset, $chain);
2139 $loglevel = get_option_log_level($options, "log_level_out
");
2141 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
2143 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2145 # we use RETURN because we may want to check other thigs later
2146 $accept_action = 'RETURN';
2148 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2150 # add host rules first, so that cluster wide rules can be overwritten
2151 foreach my $rule (@$rules, @$cluster_rules) {
2152 next if !$rule->{enable} || $rule->{errors};
2153 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2155 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
2157 if ($rule->{type} eq 'group') {
2158 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
2159 } elsif ($rule->{type} eq 'out') {
2160 ruleset_generate_rule($ruleset, $chain, $ipversion,
2161 $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2162 undef, $cluster_conf, $hostfw_conf);
2166 delete $rule->{iface_out};
2169 # allow standard traffic on cluster network
2170 if ($localnet && ($ipversion == $localnet_ver)) {
2171 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2172 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
22 -j
$accept_action"); # SSH
2173 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2174 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2176 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2177 ruleset_addrule($ruleset, $chain, "-d
$localnet $corosync_rule");
2178 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
$corosync_rule");
2181 # implement output policy
2182 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
2183 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2185 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
2186 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
2189 sub generate_group_rules {
2190 my ($ruleset, $cluster_conf, $group, $ipversion) = @_;
2192 my $rules = $cluster_conf->{groups}->{$group};
2195 warn "no such security group
'$group'\n";
2196 $rules = []; # create empty chain
2199 my $chain = "GROUP-
${group
}-IN
";
2201 ruleset_create_chain($ruleset, $chain);
2202 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2204 foreach my $rule (@$rules) {
2205 next if $rule->{type} ne 'in';
2206 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2207 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2208 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
2209 undef, $cluster_conf);
2212 $chain = "GROUP-
${group
}-OUT
";
2214 ruleset_create_chain($ruleset, $chain);
2215 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2217 foreach my $rule (@$rules) {
2218 next if $rule->{type} ne 'out';
2219 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2220 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
2221 # check also other tap rules later
2222 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2223 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" },
2224 undef, $cluster_conf);
2229 my $valid_netdev_names = {};
2230 for (my $i = 0; $i < $MAX_NETS; $i++) {
2231 $valid_netdev_names->{"net
$i"} = 1;
2235 my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_;
2237 my $orig_line = $line;
2241 # we can add single line comments to the end of the rule
2242 if ($line =~ s/#\s*(.*?)\s*$//) {
2243 $rule->{comment} = decode('utf8', $1);
2246 # we can disable a rule when prefixed with '|'
2248 $rule->{enable} = $line =~ s/^\|// ? 0 : 1;
2250 $line =~ s/^(\S+)\s+(\S+)\s*// ||
2251 die "unable to parse rule
: $line\n";
2253 $rule->{type} = lc($1);
2254 $rule->{action} = $2;
2256 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
2257 if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
2258 $rule->{macro} = $1;
2259 $rule->{action} = $2;
2263 while (length($line)) {
2264 if ($line =~ s/^-i (\S+)\s*//) {
2265 $rule->{iface} = $1;
2269 last if $rule->{type} eq 'group';
2271 if ($line =~ s/^-p (\S+)\s*//) {
2272 $rule->{proto} = $1;
2276 if ($line =~ s/^-dport (\S+)\s*//) {
2277 $rule->{dport} = $1;
2281 if ($line =~ s/^-sport (\S+)\s*//) {
2282 $rule->{sport} = $1;
2285 if ($line =~ s/^-source (\S+)\s*//) {
2286 $rule->{source} = $1;
2289 if ($line =~ s/^-dest (\S+)\s*//) {
2297 die "unable to parse rule parameters
: $line\n" if length($line);
2299 $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1);
2300 if ($verbose && $rule->{errors}) {
2301 warn "$prefix - errors
in rule parameters
: $orig_line\n";
2302 foreach my $p (keys %{$rule->{errors}}) {
2303 warn " $p: $rule->{errors
}->{$p}\n";
2310 sub parse_vmfw_option {
2315 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
2317 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
2320 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
2322 $value = $2 ? lc($3) : '';
2323 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2326 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
2330 die "can
't parse option '$line'\n"
2333 return ($opt, $value);
2336 sub parse_hostfw_option {
2341 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2343 if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
2346 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
2348 $value = $2 ? lc($3) : '';
2349 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
2353 die "can't parse option
'$line'\n"
2356 return ($opt, $value);
2359 sub parse_clusterfw_option {
2364 if ($line =~ m/^(enable):\s*(\d+)\s*$/i) {
2367 if (($value > 1) && ((time() - $value) > 60)) {
2370 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2374 die "can
't parse option '$line'\n"
2377 return ($opt, $value);
2381 my ($clusterfw_conf, $fw_conf, $cidr) = @_;
2383 my $alias = lc($cidr);
2384 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
2385 $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
2387 die "no such alias '$cidr'\n" if !$e;;
2389 return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr};
2392 sub parse_ip_or_cidr {
2397 if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
2400 } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
2404 die "value does not look like a valid IP address or CIDR network\n";
2407 return wantarray ? ($cidr, $ipversion) : $cidr;
2413 # we can add single line comments to the end of the line
2414 my $comment = decode('utf8
', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
2416 if ($line =~ m/^(\S+)\s(\S+)$/) {
2417 my ($name, $cidr) = ($1, $2);
2420 ($cidr, $ipversion) = parse_ip_or_cidr($cidr);
2425 ipversion => $ipversion,
2427 $data->{comment} = $comment if $comment;
2434 sub generic_fw_config_parser {
2435 my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_;
2440 my $res = $empty_conf;
2442 while (defined(my $line = <$fh>)) {
2443 next if $line =~ m/^#/;
2444 next if $line =~ m/^\s*$/;
2448 my $linenr = $fh->input_line_number();
2449 my $prefix = "$filename (line $linenr)";
2451 if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
2452 $section = 'options
';
2456 if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) {
2457 $section = 'aliases
';
2461 if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2462 $section = 'groups
';
2466 die "security group name too long\n" if length($group) > $max_group_name_length;
2467 die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/;
2470 ($section, $group, $comment) = undef;
2471 warn "$prefix: $err";
2475 $res->{$section}->{$group} = [];
2476 $res->{group_comments}->{$group} = decode('utf8
', $comment)
2481 if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) {
2486 if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2491 die "ipset name too long\n" if length($group) > $max_ipset_name_length;
2492 die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
2495 ($section, $group, $comment) = undef;
2496 warn "$prefix: $err";
2500 $res->{$section}->{$group} = [];
2501 $res->{ipset_comments}->{$group} = decode('utf8
', $comment)
2507 warn "$prefix: skip line - no section\n";
2511 if ($section eq 'options
') {
2514 if ($rule_env eq 'cluster
') {
2515 ($opt, $value) = parse_clusterfw_option($line);
2516 } elsif ($rule_env eq 'host
') {
2517 ($opt, $value) = parse_hostfw_option($line);
2519 ($opt, $value) = parse_vmfw_option($line);
2521 $res->{options}->{$opt} = $value;
2523 warn "$prefix: $@" if $@;
2524 } elsif ($section eq 'aliases
') {
2526 my $data = parse_alias($line);
2527 $res->{aliases}->{lc($data->{name})} = $data;
2529 warn "$prefix: $@" if $@;
2530 } elsif ($section eq 'rules
') {
2532 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); };
2534 warn "$prefix: $err";
2537 push @{$res->{$section}}, $rule;
2538 } elsif ($section eq 'groups
') {
2540 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group
', $verbose); };
2542 warn "$prefix: $err";
2545 push @{$res->{$section}->{$group}}, $rule;
2546 } elsif ($section eq 'ipset
') {
2547 # we can add single line comments to the end of the rule
2548 my $comment = decode('utf8
', $1) if $line =~ s/#\s*(.*?)\s*$//;
2550 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2555 if ($nomatch && !$feature_ipset_nomatch) {
2556 $errors->{nomatch} = "nomatch not supported by kernel";
2560 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2561 resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
2563 $cidr = parse_ip_or_cidr($cidr);
2568 $errors->{cidr} = $err;
2571 my $entry = { cidr => $cidr };
2572 $entry->{nomatch} = 1 if $nomatch;
2573 $entry->{comment} = $comment if $comment;
2574 $entry->{errors} = $errors if $errors;
2576 if ($verbose && $errors) {
2577 warn "$prefix - errors in ipset '$group': $line\n";
2578 foreach my $p (keys %{$errors}) {
2579 warn " $p: $errors->{$p}\n";
2583 push @{$res->{$section}->{$group}}, $entry;
2585 warn "$prefix: skip line - unknown section\n";
2593 sub parse_hostfw_config {
2594 my ($filename, $fh, $cluster_conf, $verbose) = @_;
2596 my $empty_conf = { rules => [], options => {}};
2598 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host
');
2601 sub parse_vmfw_config {
2602 my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_;
2609 ipset_comments => {},
2612 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env);
2615 sub parse_clusterfw_config {
2616 my ($filename, $fh, $verbose) = @_;
2626 group_comments => {},
2628 ipset_comments => {},
2631 return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster
');
2635 my ($code, @param) = @_;
2639 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2646 sub read_local_vm_config {
2651 my $vmdata = { openvz => $openvz, qemu => $qemu };
2653 my $vmlist = PVE::Cluster::get_vmlist();
2654 return $vmdata if !$vmlist || !$vmlist->{ids};
2655 my $ids = $vmlist->{ids};
2657 foreach my $vmid (keys %$ids) {
2658 next if !$vmid; # skip VE0
2659 my $d = $ids->{$vmid};
2660 next if !$d->{node} || $d->{node} ne $nodename;
2661 next if !$d->{type};
2662 if ($d->{type} eq 'openvz
') {
2663 if ($have_pve_manager) {
2664 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
2665 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2666 $openvz->{$vmid} = $conf;
2669 } elsif ($d->{type} eq 'qemu
') {
2670 if ($have_qemu_server) {
2671 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2672 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2673 $qemu->{$vmid} = $conf;
2682 sub load_vmfw_conf {
2683 my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_;
2687 $dir = $pvefw_conf_dir if !defined($dir);
2689 my $filename = "$dir/$vmid.fw";
2690 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2691 $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose);
2692 $vmfw_conf->{vmid} = $vmid;
2698 my $format_rules = sub {
2699 my ($rules, $allow_iface) = @_;
2703 foreach my $rule (@$rules) {
2704 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
' || $rule->{type} eq 'group
') {
2705 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2706 $raw .= uc($rule->{type});
2707 if ($rule->{macro}) {
2708 $raw .= " $rule->{macro}($rule->{action})";
2710 $raw .= " " . $rule->{action};
2712 if ($allow_iface && $rule->{iface}) {
2713 $raw .= " -i $rule->{iface}";
2716 if ($rule->{type} ne 'group
') {
2717 $raw .= " -source $rule->{source}" if $rule->{source};
2718 $raw .= " -dest $rule->{dest}" if $rule->{dest};
2719 $raw .= " -p $rule->{proto}" if $rule->{proto};
2720 $raw .= " -dport $rule->{dport}" if $rule->{dport};
2721 $raw .= " -sport $rule->{sport}" if $rule->{sport};
2724 $raw .= " # " . encode('utf8
', $rule->{comment})
2725 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2728 die "unknown rule type '$rule->{type
}'";
2735 my $format_options = sub {
2740 $raw .= "[OPTIONS]\n\n";
2741 foreach my $opt (keys %$options) {
2742 $raw .= "$opt: $options->{$opt}\n";
2749 my $format_aliases = sub {
2754 $raw .= "[ALIASES]\n\n";
2755 foreach my $k (keys %$aliases) {
2756 my $e = $aliases->{$k};
2757 $raw .= "$e->{name} $e->{cidr}";
2758 $raw .= " # " . encode('utf8
', $e->{comment})
2759 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
2767 my $format_ipsets = sub {
2772 foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
2773 if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) {
2774 my $utf8comment = encode('utf8
', $comment);
2775 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2777 $raw .= "[IPSET $ipset]\n\n";
2779 my $options = $fw_conf->{ipset}->{$ipset};
2782 foreach my $entry (@$options) {
2783 $nethash->{$entry->{cidr}} = $entry;
2786 foreach my $cidr (sort keys %$nethash) {
2787 my $entry = $nethash->{$cidr};
2788 my $line = $entry->{nomatch} ? '!' : '';
2789 $line .= $entry->{cidr};
2790 $line .= " # " . encode('utf8
', $entry->{comment})
2791 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
2801 sub save_vmfw_conf {
2802 my ($vmid, $vmfw_conf) = @_;
2806 my $options = $vmfw_conf->{options};
2807 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2809 my $aliases = $vmfw_conf->{aliases};
2810 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2812 $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset};
2814 my $rules = $vmfw_conf->{rules} || [];
2815 if ($rules && scalar(@$rules)) {
2816 $raw .= "[RULES]\n\n";
2817 $raw .= &$format_rules($rules, 1);
2821 mkdir $pvefw_conf_dir;
2823 my $filename = "$pvefw_conf_dir/$vmid.fw";
2824 PVE::Tools::file_set_contents($filename, $raw);
2827 sub read_vm_firewall_configs {
2828 my ($cluster_conf, $vmdata, $dir, $verbose) = @_;
2830 my $vmfw_configs = {};
2832 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2833 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm
', $vmid, $dir, $verbose);
2834 next if !$vmfw_conf->{options}; # skip if file does not exists
2835 $vmfw_configs->{$vmid} = $vmfw_conf;
2837 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2838 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct
', $vmid, $dir, $verbose);
2839 next if !$vmfw_conf->{options}; # skip if file does not exists
2840 $vmfw_configs->{$vmid} = $vmfw_conf;
2843 return $vmfw_configs;
2846 sub get_option_log_level {
2847 my ($options, $k) = @_;
2849 my $v = $options->{$k};
2850 $v = $default_log_level if !defined($v);
2852 return undef if $v eq '' || $v eq 'nolog
';
2854 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2856 return $v if ($v >= 0) && ($v <= 7);
2858 warn "unknown log level ($k = '$v')\n";
2863 sub generate_std_chains {
2864 my ($ruleset, $options, $ipversion) = @_;
2866 my $std_chains = $pve_std_chains->{$ipversion} || die "internal error";
2868 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2872 if ($ipversion == 4) {
2873 # same as shorewall smurflog.
2874 $chain = 'PVEFW-smurflog
';
2875 $std_chains->{$chain} = [];
2877 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2878 push @{$std_chains->{$chain}}, "-j DROP";
2881 # same as shorewall logflags action.
2882 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2883 $chain = 'PVEFW-logflags
';
2884 $std_chains->{$chain} = [];
2886 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2887 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2888 push @{$std_chains->{$chain}}, "-j DROP";
2890 foreach my $chain (keys %$std_chains) {
2891 ruleset_create_chain($ruleset, $chain);
2892 foreach my $rule (@{$std_chains->{$chain}}) {
2894 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule);
2896 ruleset_addrule($ruleset, $chain, $rule);
2902 sub generate_ipset_chains {
2903 my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; #fixme
2905 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2907 my $options = $fw_conf->{ipset}->{$ipset};
2911 foreach my $entry (@$options) {
2912 next if $entry->{errors}; # skip entries with errors
2915 if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
2916 ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
2918 ($cidr, $ver) = parse_ip_or_cidr($entry->{cidr});
2920 #http://backreference.org/2013/03/01/ipv6-address-normalization/
2922 my $ipv6 = inet_pton(AF_INET6, lc($cidr));
2923 $cidr = inet_ntop(AF_INET6, $ipv6);
2929 $nethash->{$ver}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} };
2934 foreach my $ipversion (4, 6) {
2935 my $data = $nethash->{$ipversion};
2937 my $name = compute_ipset_chain_name($fw_conf->{vmid}, $ipset, $ipversion);
2939 my $hashsize = scalar(@$options);
2940 if ($hashsize <= 64) {
2943 $hashsize = round_powerof2($hashsize);
2946 my $family = $ipversion == "6" ? "inet6" : "inet";
2948 $ipset_ruleset->{$name} = ["create $name hash:net family $family hashsize $hashsize maxelem $hashsize"];
2950 foreach my $cidr (sort keys %$data) {
2951 my $entry = $data->{$cidr};
2953 my $cmd = "add $name $cidr";
2954 if ($entry->{nomatch}) {
2955 if ($feature_ipset_nomatch) {
2956 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
2958 warn "ignore !$cidr - nomatch not supported by kernel\n";
2961 push @{$ipset_ruleset->{$name}}, $cmd;
2968 sub round_powerof2 {
2972 $int |= $int >> $_ foreach (1,2,4,8,16);
2976 sub load_clusterfw_conf {
2977 my ($filename, $verbose) = @_;
2979 $filename = $clusterfw_conf_filename if !defined($filename);
2981 my $cluster_conf = {};
2982 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2983 $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose);
2986 return $cluster_conf;
2989 sub save_clusterfw_conf {
2990 my ($cluster_conf) = @_;
2994 my $options = $cluster_conf->{options};
2995 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2997 my $aliases = $cluster_conf->{aliases};
2998 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
3000 $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
3002 my $rules = $cluster_conf->{rules};
3003 if ($rules && scalar(@$rules)) {
3004 $raw .= "[RULES]\n\n";
3005 $raw .= &$format_rules($rules, 1);
3009 if ($cluster_conf->{groups}) {
3010 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
3011 my $rules = $cluster_conf->{groups}->{$group};
3012 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
3013 my $utf8comment = encode('utf8
', $comment);
3014 $raw .= "[group $group] # $utf8comment\n\n";
3016 $raw .= "[group $group]\n\n";
3019 $raw .= &$format_rules($rules, 0);
3024 mkdir $pvefw_conf_dir;
3025 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
3028 sub load_hostfw_conf {
3029 my ($cluster_conf, $filename, $verbose) = @_;
3031 $filename = $hostfw_conf_filename if !defined($filename);
3033 my $hostfw_conf = {};
3034 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3035 $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose);
3037 return $hostfw_conf;
3040 sub save_hostfw_conf {
3041 my ($hostfw_conf) = @_;
3045 my $options = $hostfw_conf->{options};
3046 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3048 my $rules = $hostfw_conf->{rules};
3049 if ($rules && scalar(@$rules)) {
3050 $raw .= "[RULES]\n\n";
3051 $raw .= &$format_rules($rules, 1);
3055 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
3059 my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_;
3063 if ($vmdata) { # test mode
3064 my $testdir = $vmdata->{testdir} || die "no test directory specified";
3065 my $filename = "$testdir/cluster.fw";
3066 $cluster_conf = load_clusterfw_conf($filename, $verbose);
3068 $filename = "$testdir/host.fw";
3069 $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose);
3071 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose);
3072 } else { # normal operation
3073 $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf;
3075 $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf;
3077 $vmdata = read_local_vm_config();
3078 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose);
3081 my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose);
3082 my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose);
3084 return ($ruleset, $ipset_ruleset, $rulesetv6);
3087 sub compile_iptables_filter {
3088 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_;
3090 $cluster_conf->{ipset}->{venet0} = [];
3091 my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0
', $ipversion);
3094 if ($cluster_conf->{aliases}->{local_network}) {
3095 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3098 ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
3100 $cluster_conf->{aliases}->{local_network} = {
3101 name => 'local_network
', cidr => $localnet, ipversion => $localnet_ver };
3104 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
3106 return ({}, {}) if !$cluster_conf->{options}->{enable};
3110 ruleset_create_chain($ruleset, "PVEFW-INPUT");
3111 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
3113 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3115 my $hostfw_options = $hostfw_conf->{options} || {};
3117 # fixme: what log level should we use here?
3118 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
3120 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT");
3123 ruleset_create_chain($ruleset, "PVEFW-VENET-OUT");
3124 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3125 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3127 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
3128 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3130 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN");
3132 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
3133 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT");
3135 ruleset_create_chain($ruleset, "PVEFW-VENET-IN");
3136 ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3138 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3140 generate_std_chains($ruleset, $hostfw_options, $ipversion);
3142 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
3144 my $ipset_ruleset = {};
3146 if ($hostfw_enable) {
3147 eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
3148 warn $@ if $@; # just to be sure - should not happen
3151 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3153 # generate firewall rules for QEMU VMs
3154 foreach my $vmid (keys %{$vmdata->{qemu}}) {
3156 my $conf = $vmdata->{qemu}->{$vmid};
3157 my $vmfw_conf = $vmfw_configs->{$vmid};
3158 return if !$vmfw_conf;
3160 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3162 foreach my $netid (keys %$conf) {
3163 next if $netid !~ m/^net(\d+)$/;
3164 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3165 next if !$net->{firewall};
3166 my $iface = "tap${vmid}i$1";
3168 my $macaddr = $net->{macaddr};
3169 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3170 $vmfw_conf, $vmid, 'IN
', $ipversion);
3171 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3172 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3175 warn $@ if $@; # just to be sure - should not happen
3178 # generate firewall rules for OpenVZ containers
3179 foreach my $vmid (keys %{$vmdata->{openvz}}) {
3181 my $conf = $vmdata->{openvz}->{$vmid};
3183 my $vmfw_conf = $vmfw_configs->{$vmid};
3184 return if !$vmfw_conf;
3186 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3188 if ($vmfw_conf->{options}->{enable}) {
3189 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
3190 my $ip = $conf->{ip_address}->{value};
3195 foreach my $singleip (split(',', $ip)) {
3196 my $singleip_ver = parse_address_list($singleip); # make sure we have a valid $ip list
3197 push @{$cluster_conf->{ipset}->{venet0}}, { cidr => $singleip };
3198 push @ips, $singleip if $singleip_ver == $ipversion;
3202 my $ip_list = join(',', @ips);
3203 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'IN
', $ipversion);
3204 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'OUT
', $ipversion);
3209 if ($conf->{netif} && $conf->{netif}->{value}) {
3210 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
3211 foreach my $netid (keys %$netif) {
3212 my $d = $netif->{$netid};
3213 my $bridge = $d->{bridge};
3214 next if !$bridge || $bridge !~ m/^vmbr\d+(v(\d+))?f$/; # firewall enabled ?
3215 my $macaddr = $d->{mac};
3216 my $iface = $d->{host_ifname};
3217 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3218 $vmfw_conf, $vmid, 'IN
', $ipversion);
3219 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3220 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3224 warn $@ if $@; # just to be sure - should not happen
3227 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
3228 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS");
3231 generate_ipset_chains($ipset_ruleset, undef, $cluster_conf);
3233 return ($ruleset, $ipset_ruleset);
3236 sub get_ruleset_status {
3237 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
3239 my $statushash = {};
3241 foreach my $chain (sort keys %$ruleset) {
3242 my $sig = &$digest_fn($ruleset->{$chain});
3244 $statushash->{$chain}->{sig} = $sig;
3246 my $oldsig = $active_chains->{$chain};
3247 if (!defined($oldsig)) {
3248 $statushash->{$chain}->{action} = 'create
';
3250 if ($oldsig eq $sig) {
3251 $statushash->{$chain}->{action} = 'exists';
3253 $statushash->{$chain}->{action} = 'update
';
3256 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
3257 foreach my $cmd (@{$ruleset->{$chain}}) {
3258 print "\t$cmd\n" if $verbose;
3262 foreach my $chain (sort keys %$active_chains) {
3263 if (!defined($ruleset->{$chain})) {
3264 my $sig = $active_chains->{$chain};
3265 $statushash->{$chain}->{action} = 'delete';
3266 $statushash->{$chain}->{sig} = $sig;
3267 print "delete $chain ($sig)\n" if $verbose;
3274 sub print_sig_rule {
3275 my ($chain, $sig) = @_;
3277 # We just use this to store a SHA1 checksum used to detect changes
3278 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
3281 sub get_ruleset_cmdlist {
3282 my ($ruleset, $verbose, $iptablescmd) = @_;
3284 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
3286 my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
3287 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
3289 # create missing chains first
3290 foreach my $chain (sort keys %$ruleset) {
3291 my $stat = $statushash->{$chain};
3292 die "internal error" if !$stat;
3293 next if $stat->{action} ne 'create
';
3295 $cmdlist .= ":$chain - [0:0]\n";
3298 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3299 my $chain = "PVEFW-$h";
3300 if ($ruleset->{$chain} && !$hooks->{$h}) {
3301 $cmdlist .= "-A $h -j $chain\n";
3305 foreach my $chain (sort keys %$ruleset) {
3306 my $stat = $statushash->{$chain};
3307 die "internal error" if !$stat;
3309 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
3310 $cmdlist .= "-F $chain\n";
3311 foreach my $cmd (@{$ruleset->{$chain}}) {
3312 $cmdlist .= "$cmd\n";
3314 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
3315 } elsif ($stat->{action
} eq 'delete') {
3316 die "internal error"; # this should not happen
3317 } elsif ($stat->{action
} eq 'exists') {
3320 die "internal error - unknown status '$stat->{action}'";
3324 foreach my $chain (keys %$statushash) {
3325 next if $statushash->{$chain}->{action
} ne 'delete';
3326 $cmdlist .= "-F $chain\n";
3328 foreach my $chain (keys %$statushash) {
3329 next if $statushash->{$chain}->{action
} ne 'delete';
3330 next if $chain eq 'PVEFW-INPUT';
3331 next if $chain eq 'PVEFW-OUTPUT';
3332 next if $chain eq 'PVEFW-FORWARD';
3333 $cmdlist .= "-X $chain\n";
3336 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
3338 $cmdlist .= "COMMIT\n";
3340 return wantarray ?
($cmdlist, $changes) : $cmdlist;
3343 sub get_ipset_cmdlist
{
3344 my ($ruleset, $verbose) = @_;
3348 my $delete_cmdlist = "";
3350 my $active_chains = ipset_get_chains
();
3351 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
3353 # remove stale _swap chains
3354 foreach my $chain (keys %$active_chains) {
3355 if ($chain =~ m/^PVEFW-\S+_swap$/) {
3356 $cmdlist .= "destroy $chain\n";
3360 foreach my $chain (keys %$ruleset) {
3361 my $stat = $statushash->{$chain};
3362 die "internal error" if !$stat;
3364 if ($stat->{action
} eq 'create') {
3365 foreach my $cmd (@{$ruleset->{$chain}}) {
3366 $cmdlist .= "$cmd\n";
3371 foreach my $chain (keys %$ruleset) {
3372 my $stat = $statushash->{$chain};
3373 die "internal error" if !$stat;
3375 if ($stat->{action
} eq 'update') {
3376 my $chain_swap = $chain."_swap";
3378 foreach my $cmd (@{$ruleset->{$chain}}) {
3379 $cmd =~ s/$chain/$chain_swap/;
3380 $cmdlist .= "$cmd\n";
3382 $cmdlist .= "swap $chain_swap $chain\n";
3383 $cmdlist .= "flush $chain_swap\n";
3384 $cmdlist .= "destroy $chain_swap\n";
3388 # the remove unused chains
3389 foreach my $chain (keys %$statushash) {
3390 next if $statushash->{$chain}->{action
} ne 'delete';
3392 $delete_cmdlist .= "flush $chain\n";
3393 $delete_cmdlist .= "destroy $chain\n";
3396 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
3398 return ($cmdlist, $delete_cmdlist, $changes);
3402 my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_;
3404 enable_bridge_firewall
();
3406 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
3407 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
3409 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
3410 my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist
($rulesetv6, $verbose, "ip6tables");
3413 if ($ipset_changes) {
3414 print "ipset changes:\n";
3415 print $ipset_create_cmdlist if $ipset_create_cmdlist;
3416 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
3420 print "iptables changes:\n";
3425 print "ip6tables changes:\n";
3430 my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1";
3431 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_create_cmdlist || '');
3433 ipset_restore_cmdlist
($ipset_create_cmdlist);
3435 $tmpfile = "$pve_fw_status_dir/ip4cmdlist";
3436 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlist || '');
3438 iptables_restore_cmdlist
($cmdlist);
3440 $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
3441 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlistv6 || '');
3443 ip6tables_restore_cmdlist
($cmdlistv6);
3445 $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
3446 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_delete_cmdlist || '');
3448 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
3450 # test: re-read status and check if everything is up to date
3451 my $active_chains = iptables_get_chains
();
3452 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
3455 foreach my $chain (sort keys %$ruleset) {
3456 my $stat = $statushash->{$chain};
3457 if ($stat->{action
} ne 'exists') {
3458 warn "unable to update chain '$chain'\n";
3463 my $active_chainsv6 = iptables_get_chains
("ip6tables");
3464 my $statushashv6 = get_ruleset_status
($rulesetv6, $active_chainsv6, \
&iptables_chain_digest
, 0);
3466 foreach my $chain (sort keys %$rulesetv6) {
3467 my $stat = $statushashv6->{$chain};
3468 if ($stat->{action
} ne 'exists') {
3469 warn "unable to update chain '$chain'\n";
3474 die "unable to apply firewall changes\n" if $errors;
3476 update_nf_conntrack_max
($hostfw_conf);
3478 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
3482 sub update_nf_conntrack_max
{
3483 my ($hostfw_conf) = @_;
3485 my $max = 65536; # reasonable default
3487 my $options = $hostfw_conf->{options
} || {};
3489 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
3490 $max = $options->{nf_conntrack_max
};
3491 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
3494 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
3495 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
3497 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
3499 if ($current != $max) {
3500 my $hashsize = int($max/4);
3501 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
3502 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
3506 sub update_nf_conntrack_tcp_timeout_established
{
3507 my ($hostfw_conf) = @_;
3509 my $options = $hostfw_conf->{options
} || {};
3511 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
3513 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
3516 sub remove_pvefw_chains
{
3518 PVE
::Firewall
::remove_pvefw_chains_iptables
("iptables");
3519 PVE
::Firewall
::remove_pvefw_chains_iptables
("ip6tables");
3520 PVE
::Firewall
::remove_pvefw_chains_ipset
();
3524 sub remove_pvefw_chains_iptables
{
3525 my ($iptablescmd) = @_;
3527 my ($chash, $hooks) = iptables_get_chains
($iptablescmd);
3528 my $cmdlist = "*filter\n";
3530 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3532 $cmdlist .= "-D $h -j PVEFW-$h\n";
3536 foreach my $chain (keys %$chash) {
3537 $cmdlist .= "-F $chain\n";
3540 foreach my $chain (keys %$chash) {
3541 $cmdlist .= "-X $chain\n";
3543 $cmdlist .= "COMMIT\n";
3545 if($iptablescmd eq "ip6tables") {
3546 ip6tables_restore_cmdlist
($cmdlist);
3548 iptables_restore_cmdlist
($cmdlist);
3552 sub remove_pvefw_chains_ipset
{
3554 my $ipset_chains = ipset_get_chains
();
3558 foreach my $chain (keys %$ipset_chains) {
3559 $cmdlist .= "flush $chain\n";
3560 $cmdlist .= "destroy $chain\n";
3563 ipset_restore_cmdlist
($cmdlist) if $cmdlist;
3567 my $cluster_conf = load_clusterfw_conf
();
3568 my $cluster_options = $cluster_conf->{options
};
3569 my $enable = $cluster_options->{enable
};
3573 # load required modules here
3579 my $cluster_conf = load_clusterfw_conf
();
3580 my $cluster_options = $cluster_conf->{options
};
3582 if (!$cluster_options->{enable
}) {
3583 PVE
::Firewall
::remove_pvefw_chains
();
3587 my $hostfw_conf = load_hostfw_conf
($cluster_conf);
3589 my ($ruleset, $ipset_ruleset, $rulesetv6) = compile
($cluster_conf, $hostfw_conf);
3591 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6);