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' },
140 'NeighborDiscovery' => [
141 "IPv6 neighbor solicitation, neighbor and router advertisement",
142 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'router-advertisement' },
143 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'neighbor-solicitation' },
144 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'neighbor-advertisement' },
147 { action
=> 'PARAM', proto
=> 'udp', dport
=> '546:547', sport
=> '546:547' },
150 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
151 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'echo-request' },
155 # imported/converted from: /usr/share/shorewall/macro.*
156 my $pve_fw_macros = {
159 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
160 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
163 "Auth (identd) traffic",
164 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
167 "Border Gateway Protocol traffic",
168 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
171 "BitTorrent traffic for BitTorrent 3.1 and earlier",
172 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
173 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
176 "BitTorrent traffic for BitTorrent 3.2 and later",
177 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
178 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
181 "Concurrent Versions System pserver traffic",
182 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
185 "Citrix/ICA traffic (ICA, ICA Browser, CGP)",
186 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
187 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
188 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
191 "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)",
192 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
193 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
196 "Distributed Checksum Clearinghouse spam filtering mechanism",
197 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
200 "Forwarded DHCP traffic",
201 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
204 "Domain Name System traffic (upd and tcp)",
205 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
206 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
209 "Distributed Compiler service",
210 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
213 "File Transfer Protocol",
214 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
217 "Finger protocol (RFC 742)",
218 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
221 "GNUnet secure peer-to-peer networking traffic",
222 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
223 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
224 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
225 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
228 "Generic Routing Encapsulation tunneling protocol",
229 { action
=> 'PARAM', proto
=> '47' },
232 "Git distributed revision control traffic",
233 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
236 "OpenPGP HTTP keyserver protocol traffic",
237 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
240 "Hypertext Transfer Protocol (WWW)",
241 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
244 "Hypertext Transfer Protocol (WWW) over SSL",
245 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
248 "Internet Cache Protocol V2 (Squid) traffic",
249 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
252 "AOL Instant Messenger traffic",
253 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
256 "Internet Message Access Protocol",
257 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
260 "Internet Message Access Protocol over SSL",
261 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
264 "IPIP capsulation traffic",
265 { action
=> 'PARAM', proto
=> '94' },
269 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
270 { action
=> 'PARAM', proto
=> '50' },
273 "IPsec authentication (AH) traffic",
274 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
275 { action
=> 'PARAM', proto
=> '51' },
278 "IPsec traffic and Nat-Traversal",
279 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
280 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
281 { action
=> 'PARAM', proto
=> '50' },
284 "Internet Relay Chat traffic",
285 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
288 "HP Jetdirect printing",
289 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
292 "Layer 2 Tunneling Protocol traffic",
293 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
296 "Lightweight Directory Access Protocol traffic",
297 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
300 "Secure Lightweight Directory Access Protocol traffic",
301 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
304 "Microsoft Notification Protocol",
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
308 "Microsoft SQL Server",
309 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
312 "Mail traffic (SMTP, SMTPS, Submission)",
313 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
314 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
315 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
318 "Munin networked resource monitoring traffic",
319 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
323 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
326 "NNTP traffic (Usenet).",
327 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
330 "Encrypted NNTP traffic (Usenet)",
331 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
334 "Network Time Protocol (ntpd)",
335 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
338 "OSPF multicast traffic",
339 { action
=> 'PARAM', proto
=> '89' },
343 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
346 "Symantec PCAnywere (tm)",
347 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
348 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
352 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
355 "Encrypted POP3 traffic",
356 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
359 "Point-to-Point Tunneling Protocol",
360 { action
=> 'PARAM', proto
=> '47' },
361 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
365 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
369 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
372 "Line Printer protocol printing",
373 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
376 "Microsoft Remote Desktop Protocol traffic",
377 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
380 "Routing Information Protocol (bidirectional)",
381 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
384 "BIND remote management protocol",
385 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
388 "Razor Antispam System",
389 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
392 "Remote time retrieval (rdate)",
393 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
397 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
400 "SANE network scanning",
401 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
404 "Microsoft SMB traffic",
405 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
406 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
407 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
408 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
411 "Samba Web Administration Tool",
412 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
415 "Simple Mail Transfer Protocol",
416 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
419 "Encrypted Simple Mail Transfer Protocol",
420 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
423 "Simple Network Management Protocol",
424 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
425 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
428 "Spam Assassin SPAMD traffic",
429 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
432 "Secure shell traffic",
433 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
436 "Subversion server (svnserve)",
437 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
440 "SixXS IPv6 Deployment and Tunnel Broker",
441 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
442 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
443 { action
=> 'PARAM', proto
=> '41' },
444 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
447 "Squid web proxy traffic",
448 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
451 "Mail message submission traffic",
452 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
455 "Syslog protocol (RFC 5424) traffic",
456 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
457 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
460 "Trivial File Transfer Protocol traffic",
461 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
465 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
469 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
472 "RFC 868 Time protocol",
473 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
476 "Traceroute (for up to 30 hops) traffic",
477 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
478 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
481 "VNC traffic for VNC display's 0 - 99",
482 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5999' },
485 "VNC traffic from Vncservers to Vncviewers in listen mode",
486 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
489 "WWW traffic (HTTP and HTTPS)",
490 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
491 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
494 "Web Cache/Proxy traffic (port 8080)",
495 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
499 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
502 "Whois (nicname, RFC 3912) traffic",
503 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
507 my $pve_fw_parsed_macros;
508 my $pve_fw_macro_descr;
509 my $pve_fw_preferred_macro_names = {};
511 my $pve_std_chains = {};
512 $pve_std_chains->{4} = {
513 'PVEFW-SET-ACCEPT-MARK' => [
514 "-j MARK --set-mark 1",
516 'PVEFW-DropBroadcast' => [
517 # same as shorewall 'Broadcast'
518 # simply DROP BROADCAST/MULTICAST/ANYCAST
519 # we can use this to reduce logging
520 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
521 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
522 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
523 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
526 # same as shorewall 'reject'
527 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
528 { action
=> 'DROP', source
=> '224.0.0.0/4' },
529 { action
=> 'DROP', proto
=> 'icmp' },
530 "-p tcp -j REJECT --reject-with tcp-reset",
531 "-p udp -j REJECT --reject-with icmp-port-unreachable",
532 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
533 "-j REJECT --reject-with icmp-host-prohibited",
536 # same as shorewall 'Drop', which is equal to DROP,
537 # but REJECT/DROP some packages to reduce logging,
538 # and ACCEPT critical ICMP types
539 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
540 # we are not interested in BROADCAST/MULTICAST/ANYCAST
541 { action
=> 'PVEFW-DropBroadcast' },
542 # ACCEPT critical ICMP types
543 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
544 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
545 # Drop packets with INVALID state
546 "-m conntrack --ctstate INVALID -j DROP",
547 # Drop Microsoft SMB noise
548 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
549 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
550 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
551 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
552 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
553 # Drop new/NotSyn traffic so that it doesn't get logged
554 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
556 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
559 # same as shorewall 'Reject', which is equal to Reject,
560 # but REJECT/DROP some packages to reduce logging,
561 # and ACCEPT critical ICMP types
562 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
563 # we are not interested in BROADCAST/MULTICAST/ANYCAST
564 { action
=> 'PVEFW-DropBroadcast' },
565 # ACCEPT critical ICMP types
566 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
567 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
568 # Drop packets with INVALID state
569 "-m conntrack --ctstate INVALID -j DROP",
570 # Drop Microsoft SMB noise
571 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
572 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
573 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
574 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
575 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
576 # Drop new/NotSyn traffic so that it doesn't get logged
577 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
579 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
581 'PVEFW-tcpflags' => [
582 # same as shorewall tcpflags action.
583 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
584 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
585 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
586 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
587 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
588 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
591 # same as shorewall smurfs action
592 # Filter packets for smurfs (packets with a broadcast address as the source).
593 "-s 0.0.0.0/32 -j RETURN",
594 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
595 "-s 224.0.0.0/4 -g PVEFW-smurflog",
599 $pve_std_chains->{6} = {
600 'PVEFW-SET-ACCEPT-MARK' => [
601 "-j MARK --set-mark 1",
603 'PVEFW-DropBroadcast' => [
604 # same as shorewall 'Broadcast'
605 # simply DROP BROADCAST/MULTICAST/ANYCAST
606 # we can use this to reduce logging
607 #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6
608 # ipv6 addrtype does not work with kernel 2.6.32
609 #{ action => 'DROP', dsttype => 'MULTICAST' },
610 #{ action => 'DROP', dsttype => 'ANYCAST' },
611 { action
=> 'DROP', dest
=> 'ff00::/8' },
612 #{ action => 'DROP', dest => '224.0.0.0/4' },
615 # same as shorewall 'reject'
616 #{ action => 'DROP', dsttype => 'BROADCAST' },
617 #{ action => 'DROP', source => '224.0.0.0/4' },
618 { action
=> 'DROP', proto
=> 'icmpv6' },
619 "-p tcp -j REJECT --reject-with tcp-reset",
620 #"-p udp -j REJECT --reject-with icmp-port-unreachable",
621 #"-p icmp -j REJECT --reject-with icmp-host-unreachable",
622 #"-j REJECT --reject-with icmp-host-prohibited",
625 # same as shorewall 'Drop', which is equal to DROP,
626 # but REJECT/DROP some packages to reduce logging,
627 # and ACCEPT critical ICMP types
628 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
629 # we are not interested in BROADCAST/MULTICAST/ANYCAST
630 { action
=> 'PVEFW-DropBroadcast' },
631 # ACCEPT critical ICMP types
632 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
633 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
634 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
636 # Drop packets with INVALID state
637 "-m conntrack --ctstate INVALID -j DROP",
638 # Drop Microsoft SMB noise
639 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
640 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
641 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
642 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
643 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
644 # Drop new/NotSyn traffic so that it doesn't get logged
645 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
647 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
650 # same as shorewall 'Reject', which is equal to Reject,
651 # but REJECT/DROP some packages to reduce logging,
652 # and ACCEPT critical ICMP types
653 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
654 # we are not interested in BROADCAST/MULTICAST/ANYCAST
655 { action
=> 'PVEFW-DropBroadcast' },
656 # ACCEPT critical ICMP types
657 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
658 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
659 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
661 # Drop packets with INVALID state
662 "-m conntrack --ctstate INVALID -j DROP",
663 # Drop Microsoft SMB noise
664 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
665 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
666 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
667 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
668 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
669 # Drop new/NotSyn traffic so that it doesn't get logged
670 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
672 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
674 'PVEFW-tcpflags' => [
675 # same as shorewall tcpflags action.
676 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
677 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
678 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
679 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
680 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
681 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
685 # iptables -p icmp -h
686 my $icmp_type_names = {
689 'destination-unreachable' => 1,
690 'network-unreachable' => 1,
691 'host-unreachable' => 1,
692 'protocol-unreachable' => 1,
693 'port-unreachable' => 1,
694 'fragmentation-needed' => 1,
695 'source-route-failed' => 1,
696 'network-unknown' => 1,
698 'network-prohibited' => 1,
699 'host-prohibited' => 1,
700 'TOS-network-unreachable' => 1,
701 'TOS-host-unreachable' => 1,
702 'communication-prohibited' => 1,
703 'host-precedence-violation' => 1,
704 'precedence-cutoff' => 1,
705 'source-quench' => 1,
707 'network-redirect' => 1,
708 'host-redirect' => 1,
709 'TOS-network-redirect' => 1,
710 'TOS-host-redirect' => 1,
712 'router-advertisement' => 1,
713 'router-solicitation' => 1,
714 'time-exceeded' => 1,
715 'ttl-zero-during-transit' => 1,
716 'ttl-zero-during-reassembly' => 1,
717 'parameter-problem' => 1,
718 'ip-header-bad' => 1,
719 'required-option-missing' => 1,
720 'timestamp-request' => 1,
721 'timestamp-reply' => 1,
722 'address-mask-request' => 1,
723 'address-mask-reply' => 1,
726 # ip6tables -p icmpv6 -h
728 my $icmpv6_type_names = {
730 'destination-unreachable' => 1,
732 'communication-prohibited' => 1,
733 'address-unreachable' => 1,
734 'port-unreachable' => 1,
735 'packet-too-big' => 1,
736 'time-exceeded' => 1,
737 'ttl-zero-during-transit' => 1,
738 'ttl-zero-during-reassembly' => 1,
739 'parameter-problem' => 1,
741 'unknown-header-type' => 1,
742 'unknown-option' => 1,
745 'router-solicitation' => 1,
746 'router-advertisement' => 1,
747 'neighbour-solicitation' => 1,
748 'neighbour-advertisement' => 1,
752 sub init_firewall_macros
{
754 $pve_fw_parsed_macros = {};
756 foreach my $k (keys %$pve_fw_macros) {
757 my $lc_name = lc($k);
758 my $macro = $pve_fw_macros->{$k};
759 if (!ref($macro->[0])) {
760 $pve_fw_macro_descr->{$k} = shift @$macro;
762 $pve_fw_preferred_macro_names->{$lc_name} = $k;
763 $pve_fw_parsed_macros->{$k} = $macro;
767 init_firewall_macros
();
770 return wantarray ?
($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
775 sub get_etc_services
{
777 return $etc_services if $etc_services;
779 my $filename = "/etc/services";
781 my $fh = IO
::File-
>new($filename, O_RDONLY
);
783 warn "unable to read '$filename' - $!\n";
789 while (my $line = <$fh>) {
791 next if $line =~m/^#/;
792 next if ($line =~m/^\s*$/);
794 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
795 $services->{byid
}->{$2}->{name
} = $1;
796 $services->{byid
}->{$2}->{port
} = $2;
797 $services->{byid
}->{$2}->{$3} = 1;
798 $services->{byname
}->{$1} = $services->{byid
}->{$2};
804 $etc_services = $services;
807 return $etc_services;
812 sub get_etc_protocols
{
813 return $etc_protocols if $etc_protocols;
815 my $filename = "/etc/protocols";
817 my $fh = IO
::File-
>new($filename, O_RDONLY
);
819 warn "unable to read '$filename' - $!\n";
825 while (my $line = <$fh>) {
827 next if $line =~m/^#/;
828 next if ($line =~m/^\s*$/);
830 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
831 $protocols->{byid
}->{$2}->{name
} = $1;
832 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
838 # add special case for ICMP v6
839 $protocols->{byid
}->{icmpv6
}->{name
} = "icmpv6";
840 $protocols->{byname
}->{icmpv6
} = $protocols->{byid
}->{icmpv6
};
842 $etc_protocols = $protocols;
844 return $etc_protocols;
847 my $ipv4_mask_hash_localnet = {
849 '255.255.128.0' => 17,
850 '255.255.192.0' => 18,
851 '255.255.224.0' => 19,
852 '255.255.240.0' => 20,
853 '255.255.248.0' => 21,
854 '255.255.252.0' => 22,
855 '255.255.254.0' => 23,
856 '255.255.255.0' => 24,
857 '255.255.255.128' => 25,
858 '255.255.255.192' => 26,
859 '255.255.255.224' => 27,
860 '255.255.255.240' => 28,
861 '255.255.255.248' => 29,
862 '255.255.255.252' => 30,
868 my ($new_value) = @_;
870 $__local_network = $new_value if defined($new_value);
872 return $__local_network if defined($__local_network);
875 my $nodename = PVE
::INotify
::nodename
();
877 my $ip = PVE
::Cluster
::remote_node_ip
($nodename);
879 my $testip = Net
::IP-
>new($ip);
881 my $routes = PVE
::ProcFSTools
::read_proc_net_route
();
882 foreach my $entry (@$routes) {
883 my $mask = $ipv4_mask_hash_localnet->{$entry->{mask
}};
884 next if !defined($mask);
885 return if $mask eq '0.0.0.0';
886 my $cidr = "$entry->{dest}/$mask";
887 my $testnet = Net
::IP-
>new($cidr);
888 if ($testnet->overlaps($testip)) {
889 $__local_network = $cidr;
896 return $__local_network;
899 # ipset names are limited to 31 characters,
900 # and we use '-v4' or '-v6' to indicate IP versions,
901 # and we use '_swap' suffix for atomic update,
902 # for example PVEFW-${VMID}-${ipset_name}_swap
904 my $max_iptables_ipset_name_length = 31 - length("PVEFW-") - length("_swap");
906 sub compute_ipset_chain_name
{
907 my ($vmid, $ipset_name, $ipversion) = @_;
909 $vmid = 0 if !defined($vmid);
911 my $id = "$vmid-${ipset_name}-v$ipversion";
913 if (length($id) > $max_iptables_ipset_name_length) {
914 $id = PVE
::Tools
::fnv31a_hex
($id);
920 sub compute_ipfilter_ipset_name
{
923 return "ipfilter-$iface";
926 sub parse_address_list
{
929 if ($str =~ m/^(\+)(\S+)$/) { # ipset ref
930 die "ipset name too long\n" if length($str) > ($max_ipset_name_length + 1);
934 if ($str =~ m/^${ip_alias_pattern}$/) {
935 die "alias name too long\n" if length($str) > $max_alias_name_length;
943 foreach my $elem (split(/,/, $str)) {
945 my $ip = Net
::IP-
>new($elem);
947 my $err = Net
::IP
::Error
();
948 die "invalid IP address: $err\n";
950 $iprange = 1 if $elem =~ m/-/;
952 my $new_ipversion = Net
::IP
::ip_is_ipv6
($ip->ip()) ?
6 : 4;
954 die "detected mixed ipv4/ipv6 addresses in address list '$str'\n"
955 if $ipversion && ($new_ipversion != $ipversion);
957 $ipversion = $new_ipversion;
960 die "you can't use a range in a list\n" if $iprange && $count > 1;
965 sub parse_port_name_number_or_range
{
968 my $services = PVE
::Firewall
::get_etc_services
();
972 foreach my $item (split(/,/, $str)) {
974 if ($item =~ m/^(\d+):(\d+)$/) {
975 my ($port1, $port2) = ($1, $2);
976 die "invalid port '$port1'\n" if $port1 > 65535;
977 die "invalid port '$port2'\n" if $port2 > 65535;
978 } elsif ($item =~ m/^(\d+)$/) {
980 die "invalid port '$port'\n" if $port > 65535;
982 if ($icmp_type_names->{$item}) {
984 } elsif ($icmpv6_type_names->{$item}) {
987 die "invalid port '$item'\n" if !$services->{byname
}->{$item};
992 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
997 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
998 sub pve_fw_verify_port_spec
{
1001 parse_port_name_number_or_range
($portstr);
1006 PVE
::JSONSchema
::register_format
('pve-fw-addr-spec', \
&pve_fw_verify_addr_spec
);
1007 sub pve_fw_verify_addr_spec
{
1010 parse_address_list
($list);
1015 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
1016 sub pve_fw_verify_protocol_spec
{
1019 my $protocols = get_etc_protocols
();
1021 die "unknown protocol '$proto'\n" if $proto &&
1022 !(defined($protocols->{byname
}->{$proto}) ||
1023 defined($protocols->{byid
}->{$proto}));
1029 # helper function for API
1031 sub copy_opject_with_digest
{
1034 my $sha = Digest
::SHA-
>new('sha1');
1037 foreach my $k (sort keys %$object) {
1038 my $v = $object->{$k};
1039 next if !defined($v);
1041 $sha->add($k, ':', $v, "\n");
1044 my $digest = $sha->hexdigest;
1046 $res->{digest
} = $digest;
1048 return wantarray ?
($res, $digest) : $res;
1051 sub copy_list_with_digest
{
1054 my $sha = Digest
::SHA-
>new('sha1');
1057 foreach my $entry (@$list) {
1059 foreach my $k (sort keys %$entry) {
1060 my $v = $entry->{$k};
1061 next if !defined($v);
1063 # Note: digest ignores refs ($rule->{errors})
1064 $sha->add($k, ':', $v, "\n") if !ref($v); ;
1069 my $digest = $sha->hexdigest;
1071 foreach my $entry (@$res) {
1072 $entry->{digest
} = $digest;
1075 return wantarray ?
($res, $digest) : $res;
1078 my $rule_properties = {
1080 description
=> "Update rule at position <pos>.",
1085 digest
=> get_standard_option
('pve-config-digest'),
1089 enum
=> ['in', 'out', 'group'],
1092 description
=> "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
1095 pattern
=> $security_group_name_pattern,
1104 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
1106 type
=> 'string', format
=> 'pve-fw-addr-spec',
1110 type
=> 'string', format
=> 'pve-fw-addr-spec',
1114 type
=> 'string', format
=> 'pve-fw-protocol-spec',
1122 type
=> 'string', format
=> 'pve-fw-port-spec',
1126 type
=> 'string', format
=> 'pve-fw-port-spec',
1135 sub add_rule_properties
{
1136 my ($properties) = @_;
1138 foreach my $k (keys %$rule_properties) {
1139 my $h = $rule_properties->{$k};
1140 # copy data, so that we can modify later without side effects
1141 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
1147 sub delete_rule_properties
{
1148 my ($rule, $delete_str) = @_;
1150 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
1151 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
1152 if !defined($rule_properties->{$opt});
1153 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
1154 if $opt eq 'type' || $opt eq 'action';
1155 delete $rule->{$opt};
1161 my $apply_macro = sub {
1162 my ($macro_name, $param, $verify, $ipversion) = @_;
1164 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1165 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1167 if ($ipversion && ($ipversion == 6) && $pve_ipv6fw_macros->{$macro_name}) {
1168 $macro_rules = $pve_ipv6fw_macros->{$macro_name};
1173 foreach my $templ (@$macro_rules) {
1175 my $param_used = {};
1176 foreach my $k (keys %$templ) {
1177 my $v = $templ->{$k};
1178 if ($v eq 'PARAM') {
1180 $param_used->{$k} = 1;
1181 } elsif ($v eq 'DEST') {
1182 $v = $param->{dest
};
1183 $param_used->{dest
} = 1;
1184 } elsif ($v eq 'SOURCE') {
1185 $v = $param->{source
};
1186 $param_used->{source
} = 1;
1190 my $msg = "missing parameter '$k' in macro '$macro_name'";
1191 raise_param_exc
({ macro => $msg }) if $verify;
1196 foreach my $k (keys %$param) {
1197 next if $k eq 'macro';
1198 next if !defined($param->{$k});
1199 next if $param_used->{$k};
1200 if (defined($rule->{$k})) {
1201 if ($rule->{$k} ne $param->{$k}) {
1202 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
1203 raise_param_exc
({ $k => $msg }) if $verify;
1207 $rule->{$k} = $param->{$k};
1210 push @$rules, $rule;
1216 my $rule_env_iface_lookup = {
1225 my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_;
1227 my $allow_groups = $rule_env eq 'group' ?
0 : 1;
1229 my $allow_iface = $rule_env_iface_lookup->{$rule_env};
1230 die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen
1232 my $errors = $rule->{errors
} || {};
1234 my $error_count = 0;
1236 my $add_error = sub {
1237 my ($param, $msg) = @_;
1239 raise_param_exc
({ $param => $msg }) if !$noerr;
1241 $errors->{$param} = $msg if !$errors->{$param};
1245 my $set_ip_version = sub {
1248 die "detected mixed ipv4/ipv6 adresses in rule\n"
1249 if $ipversion && ($vers != $ipversion);
1254 my $check_ipset_or_alias_property = sub {
1255 my ($name, $expected_ipversion) = @_;
1257 if (my $value = $rule->{$name}) {
1258 if ($value =~ m/^\+/) {
1259 if ($value =~ m/^\+(${ipset_name_pattern})$/) {
1260 &$add_error($name, "no such ipset '$1'")
1261 if !($cluster_conf->{ipset
}->{$1} || ($fw_conf && $fw_conf->{ipset
}->{$1}));
1264 &$add_error($name, "invalid ipset name '$value'");
1266 } elsif ($value =~ m/^${ip_alias_pattern}$/){
1267 my $alias = lc($value);
1268 &$add_error($name, "no such alias '$value'")
1269 if !($cluster_conf->{aliases
}->{$alias} || ($fw_conf && $fw_conf->{aliases
}->{$alias}));
1270 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1271 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1273 &$set_ip_version($e->{ipversion
});
1278 my $type = $rule->{type
};
1279 my $action = $rule->{action
};
1281 &$add_error('type', "missing property") if !$type;
1282 &$add_error('action', "missing property") if !$action;
1285 if ($type eq 'in' || $type eq 'out') {
1286 &$add_error('action', "unknown action '$action'")
1287 if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/);
1288 } elsif ($type eq 'group') {
1289 &$add_error('type', "security groups not allowed")
1291 &$add_error('action', "invalid characters in security group name")
1292 if $action && ($action !~ m/^${security_group_name_pattern}$/);
1294 &$add_error('type', "unknown rule type '$type'");
1298 if ($rule->{iface
}) {
1299 &$add_error('type', "parameter -i not allowed for this rule type")
1301 eval { PVE
::JSONSchema
::pve_verify_iface
($rule->{iface
}); };
1302 &$add_error('iface', $@) if $@;
1303 if ($rule_env eq 'vm') {
1304 &$add_error('iface', "value does not match the regex pattern 'net\\d+'")
1305 if $rule->{iface
} !~ m/^net(\d+)$/;
1306 } elsif ($rule_env eq 'ct') {
1307 &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'")
1308 if $rule->{iface
} !~ m/^(venet|eth(\d+))$/;
1312 if ($rule->{macro}) {
1313 if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) {
1314 $rule->{macro} = $preferred_name;
1316 &$add_error('macro', "unknown macro '$rule->{macro}'");
1320 if ($rule->{proto
}) {
1321 eval { pve_fw_verify_protocol_spec
($rule->{proto
}); };
1322 &$add_error('proto', $@) if $@;
1323 &$set_ip_version(4) if $rule->{proto
} eq 'icmp';
1324 &$set_ip_version(6) if $rule->{proto
} eq 'icmpv6';
1327 if ($rule->{dport
}) {
1328 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1329 &$add_error('dport', $@) if $@;
1330 &$add_error('proto', "missing property - 'dport' requires this property")
1334 if ($rule->{sport
}) {
1335 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1336 &$add_error('sport', $@) if $@;
1337 &$add_error('proto', "missing property - 'sport' requires this property")
1341 if ($rule->{source
}) {
1343 my $source_ipversion = parse_address_list
($rule->{source
});
1344 &$set_ip_version($source_ipversion);
1346 &$add_error('source', $@) if $@;
1347 &$check_ipset_or_alias_property('source', $ipversion);
1350 if ($rule->{dest
}) {
1352 my $dest_ipversion = parse_address_list
($rule->{dest
});
1353 &$set_ip_version($dest_ipversion);
1355 &$add_error('dest', $@) if $@;
1356 &$check_ipset_or_alias_property('dest', $ipversion);
1359 $rule->{ipversion
} = $ipversion if $ipversion;
1361 if ($rule->{macro} && !$error_count) {
1362 eval { &$apply_macro($rule->{macro}, $rule, 1, $ipversion); };
1364 if (ref($err) eq "PVE::Exception" && $err->{errors
}) {
1365 my $eh = $err->{errors
};
1366 foreach my $p (keys %$eh) {
1367 &$add_error($p, $eh->{$p});
1370 &$add_error('macro', "$err");
1375 $rule->{errors
} = $errors if $error_count;
1380 sub copy_rule_data
{
1381 my ($rule, $param) = @_;
1383 foreach my $k (keys %$rule_properties) {
1384 if (defined(my $v = $param->{$k})) {
1385 if ($v eq '' || $v eq '-') {
1396 sub rules_modify_permissions
{
1397 my ($rule_env) = @_;
1399 if ($rule_env eq 'host') {
1401 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
1403 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1405 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1407 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1409 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
1416 sub rules_audit_permissions
{
1417 my ($rule_env) = @_;
1419 if ($rule_env eq 'host') {
1421 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
1423 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1425 check
=> ['perm', '/', [ 'Sys.Audit' ]],
1427 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1429 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1437 my $bridge_firewall_enabled = 0;
1439 sub enable_bridge_firewall
{
1441 return if $bridge_firewall_enabled; # only once
1443 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1444 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1446 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1447 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1449 $bridge_firewall_enabled = 1;
1452 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1454 sub iptables_restore_cmdlist
{
1457 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1460 sub ip6tables_restore_cmdlist
{
1463 run_command
("/sbin/ip6tables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1466 sub ipset_restore_cmdlist
{
1469 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist, errmsg
=> "ipset_restore_cmdlist");
1472 sub iptables_get_chains
{
1473 my ($iptablescmd) = @_;
1475 $iptablescmd = "iptables" if !$iptablescmd;
1479 # check what chains we want to track
1480 my $is_pvefw_chain = sub {
1483 return 1 if $name =~ m/^PVEFW-\S+$/;
1485 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
1487 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
1489 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
1491 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/;
1492 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
1504 return if $line =~ m/^#/;
1505 return if $line =~ m/^\s*$/;
1507 if ($line =~ m/^\*(\S+)$/) {
1512 return if $table ne 'filter';
1514 if ($line =~ m/^:(\S+)\s/) {
1516 return if !&$is_pvefw_chain($chain);
1517 $res->{$chain} = "unknown";
1518 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1519 my ($chain, $sig) = ($1, $2);
1520 return if !&$is_pvefw_chain($chain);
1521 $res->{$chain} = $sig;
1522 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1525 # simply ignore the rest
1530 run_command
("/sbin/$iptablescmd-save", outfunc
=> $parser);
1532 return wantarray ?
($res, $hooks) : $res;
1535 sub iptables_chain_digest
{
1537 my $digest = Digest
::SHA-
>new('sha1');
1538 foreach my $rule (@$rules) { # order is important
1539 $digest->add($rule);
1541 return $digest->b64digest;
1544 sub ipset_chain_digest
{
1547 my $digest = Digest
::SHA-
>new('sha1');
1548 foreach my $rule (sort @$rules) { # note: sorted
1549 $digest->add($rule);
1551 return $digest->b64digest;
1554 sub ipset_get_chains
{
1562 return if $line =~ m/^#/;
1563 return if $line =~ m/^\s*$/;
1564 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1566 $line =~ s/\s+$//; # delete trailing white space
1567 push @{$chains->{$chain}}, $line;
1569 # simply ignore the rest
1574 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
1576 # compute digest for each chain
1577 foreach my $chain (keys %$chains) {
1578 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1584 sub ruleset_generate_cmdstr
{
1585 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1587 return if defined($rule->{enable
}) && !$rule->{enable
};
1588 return if $rule->{errors
};
1590 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1592 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1593 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1597 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1598 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1600 my $source = $rule->{source
};
1601 my $dest = $rule->{dest
};
1604 if ($source =~ m/^\+/) {
1605 if ($source =~ m/^\+(${ipset_name_pattern})$/) {
1607 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1608 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1609 push @cmd, "-m set --match-set ${ipset_chain} src";
1610 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1611 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1612 push @cmd, "-m set --match-set ${ipset_chain} src";
1614 die "no such ipset '$name'\n";
1617 die "invalid security group name '$source'\n";
1619 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1620 my $alias = lc($source);
1621 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1622 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1623 die "no such alias '$source'\n" if !$e;
1624 push @cmd, "-s $e->{cidr}";
1625 } elsif ($source =~ m/\-/){
1626 push @cmd, "-m iprange --src-range $source";
1628 push @cmd, "-s $source";
1633 if ($dest =~ m/^\+/) {
1634 if ($dest =~ m/^\+(${ipset_name_pattern})$/) {
1636 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1637 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1638 push @cmd, "-m set --match-set ${ipset_chain} dst";
1639 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1640 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1641 push @cmd, "-m set --match-set ${ipset_chain} dst";
1643 die "no such ipset '$name'\n";
1646 die "invalid security group name '$dest'\n";
1648 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1649 my $alias = lc($dest);
1650 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1651 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1652 die "no such alias '$dest'\n" if !$e;
1653 push @cmd, "-d $e->{cidr}";
1654 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1655 push @cmd, "-m iprange --dst-range $dest";
1657 push @cmd, "-d $dest";
1661 if ($rule->{proto
}) {
1662 push @cmd, "-p $rule->{proto}";
1665 $multiport++ if $nbdport > 1;
1666 $multiport++ if $nbsport > 1;
1668 push @cmd, "--match multiport" if $multiport;
1670 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1671 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1673 if ($rule->{dport
}) {
1674 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1675 # Note: we use dport to store --icmp-type
1676 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
1677 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1678 } elsif ($rule->{proto
} && $rule->{proto
} eq 'icmpv6') {
1679 # Note: we use dport to store --icmpv6-type
1680 die "unknown icmpv6-type '$rule->{dport}'\n" if !defined($icmpv6_type_names->{$rule->{dport
}});
1681 push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}";
1684 if ($multiport == 2) {
1685 push @cmd, "--ports $rule->{dport}";
1687 push @cmd, "--dports $rule->{dport}";
1690 push @cmd, "--dport $rule->{dport}";
1695 if ($rule->{sport
}) {
1697 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1699 push @cmd, "--sport $rule->{sport}";
1702 } elsif ($rule->{dport
} || $rule->{sport
}) {
1703 die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport
};
1704 die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport
};
1707 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1709 if (my $action = $rule->{action
}) {
1710 $action = $actions->{$action} if defined($actions->{$action});
1711 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1712 push @cmd, $goto ?
"-g $action" : "-j $action";
1715 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1718 sub ruleset_generate_rule
{
1719 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1723 if ($rule->{macro}) {
1724 $rules = &$apply_macro($rule->{macro}, $rule, 0, $ipversion);
1729 # update all or nothing
1732 foreach my $tmp (@$rules) {
1733 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) {
1734 push @cmds, $cmdstr;
1738 foreach my $cmdstr (@cmds) {
1739 ruleset_addrule
($ruleset, $chain, $cmdstr);
1743 sub ruleset_generate_rule_insert
{
1744 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto) = @_;
1746 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1748 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $rule, $actions, $goto)) {
1749 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1753 sub ruleset_create_chain
{
1754 my ($ruleset, $chain) = @_;
1756 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1757 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1759 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1761 $ruleset->{$chain} = [];
1764 sub ruleset_chain_exist
{
1765 my ($ruleset, $chain) = @_;
1767 return $ruleset->{$chain} ?
1 : undef;
1770 sub ruleset_addrule
{
1771 my ($ruleset, $chain, $rule) = @_;
1773 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1775 push @{$ruleset->{$chain}}, "-A $chain $rule";
1778 sub ruleset_insertrule
{
1779 my ($ruleset, $chain, $rule) = @_;
1781 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1783 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1786 sub get_log_rule_base
{
1787 my ($chain, $vmid, $msg, $loglevel) = @_;
1789 die "internal error - no log level" if !defined($loglevel);
1791 $vmid = 0 if !defined($vmid);
1793 # Note: we use special format for prefix to pass further
1794 # info to log daemon (VMID, LOGVELEL and CHAIN)
1796 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1799 sub ruleset_addlog {
1800 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1802 return if !defined($loglevel);
1804 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1806 $logrule = "$rule $logrule" if defined($rule);
1808 ruleset_addrule($ruleset, $chain, $logrule);
1811 sub ruleset_add_chain_policy {
1812 my ($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action) = @_;
1814 if ($policy eq 'ACCEPT') {
1816 ruleset_generate_rule($ruleset, $chain, $ipversion, { action => 'ACCEPT' },
1817 { ACCEPT => $accept_action});
1819 } elsif ($policy eq 'DROP') {
1821 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1823 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1825 ruleset_addrule($ruleset, $chain, "-j DROP
");
1826 } elsif ($policy eq 'REJECT') {
1827 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1829 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1831 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1834 die "internal error
: unknown policy
'$policy'";
1838 sub ruleset_chain_add_conn_filters {
1839 my ($ruleset, $chain, $accept) = @_;
1841 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1842 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1845 sub ruleset_chain_add_input_filters {
1846 my ($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel) = @_;
1848 if ($cluster_conf->{ipset}->{blacklist}){
1849 if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist
")) {
1850 ruleset_create_chain($ruleset, "PVEFW-blacklist
");
1851 ruleset_addlog($ruleset, "PVEFW-blacklist
", 0, "DROP
: ", $loglevel) if $loglevel;
1852 ruleset_addrule($ruleset, "PVEFW-blacklist
", "-j DROP
");
1854 my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion);
1855 ruleset_addrule($ruleset, $chain, "-m set
--match-set
${ipset_chain
} src
-j PVEFW-blacklist
");
1858 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1859 if ($ipversion == 4) {
1860 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1864 if ($options->{tcpflags}) {
1865 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1869 sub ruleset_create_vm_chain {
1870 my ($ruleset, $chain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction) = @_;
1872 ruleset_create_chain($ruleset, $chain);
1873 my $accept = generate_nfqueue($options);
1875 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1876 if ($direction eq 'OUT') {
1877 ruleset_generate_rule($ruleset, $chain, $ipversion,
1878 { action => 'PVEFW-SET-ACCEPT-MARK',
1879 proto => 'udp', sport => 68, dport => 67 });
1881 ruleset_generate_rule($ruleset, $chain, $ipversion,
1882 { action => 'ACCEPT',
1883 proto => 'udp', sport => 67, dport => 68 });
1887 if ($direction eq 'OUT') {
1888 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1889 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1891 if ($ipfilter_ipset) {
1892 ruleset_addrule($ruleset, $chain, "-m set
! --match-set
$ipfilter_ipset src
-j DROP
");
1894 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1898 sub ruleset_add_group_rule {
1899 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_;
1901 my $group = $rule->{action};
1902 my $group_chain = "GROUP-
$group-$direction";
1903 if(!ruleset_chain_exist($ruleset, $group_chain)){
1904 generate_group_rules($ruleset, $cluster_conf, $group, $ipversion);
1907 if ($direction eq 'OUT' && $rule->{iface_out}) {
1908 ruleset_addrule($ruleset, $chain, "-o
$rule->{iface_out
} -j
$group_chain");
1909 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1910 ruleset_addrule($ruleset, $chain, "-i
$rule->{iface_in
} -j
$group_chain");
1912 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1915 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$action");
1918 sub ruleset_generate_vm_rules {
1919 my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_;
1921 my $lc_direction = lc($direction);
1923 my $in_accept = generate_nfqueue($options);
1925 foreach my $rule (@$rules) {
1926 next if $rule->{iface} && $rule->{iface} ne $netid;
1927 next if !$rule->{enable} || $rule->{errors};
1928 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
1930 if ($rule->{type} eq 'group') {
1931 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
1932 $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion);
1934 next if $rule->{type} ne $lc_direction;
1936 if ($direction eq 'OUT') {
1937 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1938 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
1939 undef, $cluster_conf, $vmfw_conf);
1941 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1942 { ACCEPT => $in_accept , REJECT => "PVEFW-reject
" },
1943 undef, $cluster_conf, $vmfw_conf);
1951 sub generate_nfqueue {
1954 if ($options->{ips}) {
1955 my $action = "NFQUEUE
";
1956 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1957 if (defined($3) && defined($1)) {
1958 $action .= " --queue-balance
$1:$3";
1959 } elsif (defined($1)) {
1960 $action .= " --queue-num
$1";
1963 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
1970 sub ruleset_generate_vm_ipsrules {
1971 my ($ruleset, $options, $direction, $iface) = @_;
1973 if ($options->{ips} && $direction eq 'IN') {
1974 my $nfqueue = generate_nfqueue($options);
1976 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS
")) {
1977 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1980 ruleset_addrule($ruleset, "PVEFW-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1984 sub generate_venet_rules_direction {
1985 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction, $ipversion) = @_;
1987 my $lc_direction = lc($direction);
1989 my $rules = $vmfw_conf->{rules};
1991 my $options = $vmfw_conf->{options};
1992 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1994 my $chain = "venet0-
$vmid-$direction";
1996 ruleset_create_vm_chain($ruleset, $chain, $ipversion, $options, undef, undef, $direction);
1998 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction, undef, $ipversion);
2003 if ($direction eq 'OUT') {
2004 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2006 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2009 my $accept = generate_nfqueue($options);
2010 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2011 ruleset_add_chain_policy($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2013 if ($direction eq 'OUT') {
2014 ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT
", $ipversion, {
2017 iface_in => 'venet0'});
2019 ruleset_generate_rule($ruleset, "PVEFW-VENET-IN
", $ipversion, {
2022 iface_out => 'venet0'});
2026 sub generate_tap_rules_direction {
2027 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_;
2029 my $lc_direction = lc($direction);
2031 my $rules = $vmfw_conf->{rules};
2033 my $options = $vmfw_conf->{options};
2034 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
2036 my $tapchain = "$iface-$direction";
2038 my $ipfilter_name = compute_ipfilter_ipset_name($netid);
2039 my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
2040 if $vmfw_conf->{ipset}->{$ipfilter_name};
2042 # create chain with mac and ip filter
2043 ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction);
2045 if ($options->{enable}) {
2046 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion);
2048 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
2053 if ($direction eq 'OUT') {
2054 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2056 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2059 my $accept = generate_nfqueue($options);
2060 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2061 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2063 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : 'ACCEPT';
2064 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, 'ACCEPT', $loglevel, $accept_action);
2067 # plug the tap chain to bridge chain
2068 if ($direction eq 'IN') {
2069 ruleset_addrule($ruleset, "PVEFW-FWBR-IN
",
2070 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
2072 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT
",
2073 "-m physdev
--physdev-is-bridged
--physdev-in
$iface -j
$tapchain");
2077 sub enable_host_firewall {
2078 my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
2080 my $options = $hostfw_conf->{options};
2081 my $cluster_options = $cluster_conf->{options};
2082 my $rules = $hostfw_conf->{rules};
2083 my $cluster_rules = $cluster_conf->{rules};
2085 # host inbound firewall
2086 my $chain = "PVEFW-HOST-IN
";
2087 ruleset_create_chain($ruleset, $chain);
2089 my $loglevel = get_option_log_level($options, "log_level_in
");
2091 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
2093 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2094 ruleset_chain_add_input_filters($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel);
2096 # we use RETURN because we need to check also tap rules
2097 my $accept_action = 'RETURN';
2099 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2101 # add host rules first, so that cluster wide rules can be overwritten
2102 foreach my $rule (@$rules, @$cluster_rules) {
2103 next if !$rule->{enable} || $rule->{errors};
2104 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2106 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
2109 if ($rule->{type} eq 'group') {
2110 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
2111 } elsif ($rule->{type} eq 'in') {
2112 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2113 { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2114 undef, $cluster_conf, $hostfw_conf);
2118 delete $rule->{iface_in};
2121 # allow standard traffic for management ipset (includes cluster network)
2122 my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management
", $ipversion);
2123 my $mngmntsrc = "-m set
--match-set
${mngmnt_ipset_chain
} src
";
2124 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2125 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2126 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2127 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
22 -j
$accept_action"); # SSH
2129 my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
2130 my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion};
2133 if ($localnet && ($ipversion == $localnet_ver)) {
2134 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2135 ruleset_addrule($ruleset, $chain, "-s
$localnet -d
$localnet $corosync_rule");
2136 ruleset_addrule($ruleset, $chain, "-s
$localnet -m addrtype
--dst-type MULTICAST
$corosync_rule");
2139 # implement input policy
2140 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
2141 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2143 # host outbound firewall
2144 $chain = "PVEFW-HOST-OUT
";
2145 ruleset_create_chain($ruleset, $chain);
2147 $loglevel = get_option_log_level($options, "log_level_out
");
2149 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
2151 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2153 # we use RETURN because we may want to check other thigs later
2154 $accept_action = 'RETURN';
2156 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2158 # add host rules first, so that cluster wide rules can be overwritten
2159 foreach my $rule (@$rules, @$cluster_rules) {
2160 next if !$rule->{enable} || $rule->{errors};
2161 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2163 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
2165 if ($rule->{type} eq 'group') {
2166 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
2167 } elsif ($rule->{type} eq 'out') {
2168 ruleset_generate_rule($ruleset, $chain, $ipversion,
2169 $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2170 undef, $cluster_conf, $hostfw_conf);
2174 delete $rule->{iface_out};
2177 # allow standard traffic on cluster network
2178 if ($localnet && ($ipversion == $localnet_ver)) {
2179 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2180 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
22 -j
$accept_action"); # SSH
2181 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2182 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2184 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2185 ruleset_addrule($ruleset, $chain, "-d
$localnet $corosync_rule");
2186 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
$corosync_rule");
2189 # implement output policy
2190 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
2191 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2193 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
2194 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
2197 sub generate_group_rules {
2198 my ($ruleset, $cluster_conf, $group, $ipversion) = @_;
2200 my $rules = $cluster_conf->{groups}->{$group};
2203 warn "no such security group
'$group'\n";
2204 $rules = []; # create empty chain
2207 my $chain = "GROUP-
${group
}-IN
";
2209 ruleset_create_chain($ruleset, $chain);
2210 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2212 foreach my $rule (@$rules) {
2213 next if $rule->{type} ne 'in';
2214 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2215 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2216 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
2217 undef, $cluster_conf);
2220 $chain = "GROUP-
${group
}-OUT
";
2222 ruleset_create_chain($ruleset, $chain);
2223 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2225 foreach my $rule (@$rules) {
2226 next if $rule->{type} ne 'out';
2227 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2228 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
2229 # check also other tap rules later
2230 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2231 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" },
2232 undef, $cluster_conf);
2237 my $valid_netdev_names = {};
2238 for (my $i = 0; $i < $MAX_NETS; $i++) {
2239 $valid_netdev_names->{"net
$i"} = 1;
2243 my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_;
2245 my $orig_line = $line;
2249 # we can add single line comments to the end of the rule
2250 if ($line =~ s/#\s*(.*?)\s*$//) {
2251 $rule->{comment} = decode('utf8', $1);
2254 # we can disable a rule when prefixed with '|'
2256 $rule->{enable} = $line =~ s/^\|// ? 0 : 1;
2258 $line =~ s/^(\S+)\s+(\S+)\s*// ||
2259 die "unable to parse rule
: $line\n";
2261 $rule->{type} = lc($1);
2262 $rule->{action} = $2;
2264 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
2265 if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
2266 $rule->{macro} = $1;
2267 $rule->{action} = $2;
2271 while (length($line)) {
2272 if ($line =~ s/^-i (\S+)\s*//) {
2273 $rule->{iface} = $1;
2277 last if $rule->{type} eq 'group';
2279 if ($line =~ s/^-p (\S+)\s*//) {
2280 $rule->{proto} = $1;
2284 if ($line =~ s/^-dport (\S+)\s*//) {
2285 $rule->{dport} = $1;
2289 if ($line =~ s/^-sport (\S+)\s*//) {
2290 $rule->{sport} = $1;
2293 if ($line =~ s/^-source (\S+)\s*//) {
2294 $rule->{source} = $1;
2297 if ($line =~ s/^-dest (\S+)\s*//) {
2305 die "unable to parse rule parameters
: $line\n" if length($line);
2307 $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1);
2308 if ($verbose && $rule->{errors}) {
2309 warn "$prefix - errors
in rule parameters
: $orig_line\n";
2310 foreach my $p (keys %{$rule->{errors}}) {
2311 warn " $p: $rule->{errors
}->{$p}\n";
2318 sub parse_vmfw_option {
2323 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
2325 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
2328 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
2330 $value = $2 ? lc($3) : '';
2331 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2334 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
2338 die "can
't parse option '$line'\n"
2341 return ($opt, $value);
2344 sub parse_hostfw_option {
2349 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2351 if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
2354 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
2356 $value = $2 ? lc($3) : '';
2357 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
2361 die "can't parse option
'$line'\n"
2364 return ($opt, $value);
2367 sub parse_clusterfw_option {
2372 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
2375 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2379 die "can
't parse option '$line'\n"
2382 return ($opt, $value);
2386 my ($clusterfw_conf, $fw_conf, $cidr) = @_;
2388 my $alias = lc($cidr);
2389 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
2390 $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
2392 die "no such alias '$cidr'\n" if !$e;;
2394 return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr};
2397 sub parse_ip_or_cidr {
2402 if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
2405 } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
2409 die "value does not look like a valid IP address or CIDR network\n";
2412 return wantarray ? ($cidr, $ipversion) : $cidr;
2418 # we can add single line comments to the end of the line
2419 my $comment = decode('utf8
', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
2421 if ($line =~ m/^(\S+)\s(\S+)$/) {
2422 my ($name, $cidr) = ($1, $2);
2425 ($cidr, $ipversion) = parse_ip_or_cidr($cidr);
2430 ipversion => $ipversion,
2432 $data->{comment} = $comment if $comment;
2439 sub generic_fw_config_parser {
2440 my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_;
2445 my $res = $empty_conf;
2447 while (defined(my $line = <$fh>)) {
2448 next if $line =~ m/^#/;
2449 next if $line =~ m/^\s*$/;
2453 my $linenr = $fh->input_line_number();
2454 my $prefix = "$filename (line $linenr)";
2456 if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
2457 $section = 'options
';
2461 if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) {
2462 $section = 'aliases
';
2466 if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2467 $section = 'groups
';
2471 die "security group name too long\n" if length($group) > $max_group_name_length;
2472 die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/;
2475 ($section, $group, $comment) = undef;
2476 warn "$prefix: $err";
2480 $res->{$section}->{$group} = [];
2481 $res->{group_comments}->{$group} = decode('utf8
', $comment)
2486 if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) {
2491 if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2496 die "ipset name too long\n" if length($group) > $max_ipset_name_length;
2497 die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
2500 ($section, $group, $comment) = undef;
2501 warn "$prefix: $err";
2505 $res->{$section}->{$group} = [];
2506 $res->{ipset_comments}->{$group} = decode('utf8
', $comment)
2512 warn "$prefix: skip line - no section\n";
2516 if ($section eq 'options
') {
2519 if ($rule_env eq 'cluster
') {
2520 ($opt, $value) = parse_clusterfw_option($line);
2521 } elsif ($rule_env eq 'host
') {
2522 ($opt, $value) = parse_hostfw_option($line);
2524 ($opt, $value) = parse_vmfw_option($line);
2526 $res->{options}->{$opt} = $value;
2528 warn "$prefix: $@" if $@;
2529 } elsif ($section eq 'aliases
') {
2531 my $data = parse_alias($line);
2532 $res->{aliases}->{lc($data->{name})} = $data;
2534 warn "$prefix: $@" if $@;
2535 } elsif ($section eq 'rules
') {
2537 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); };
2539 warn "$prefix: $err";
2542 push @{$res->{$section}}, $rule;
2543 } elsif ($section eq 'groups
') {
2545 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group
', $verbose); };
2547 warn "$prefix: $err";
2550 push @{$res->{$section}->{$group}}, $rule;
2551 } elsif ($section eq 'ipset
') {
2552 # we can add single line comments to the end of the rule
2553 my $comment = decode('utf8
', $1) if $line =~ s/#\s*(.*?)\s*$//;
2555 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2560 if ($nomatch && !$feature_ipset_nomatch) {
2561 $errors->{nomatch} = "nomatch not supported by kernel";
2565 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2566 resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
2568 $cidr = parse_ip_or_cidr($cidr);
2573 $errors->{cidr} = $err;
2576 my $entry = { cidr => $cidr };
2577 $entry->{nomatch} = 1 if $nomatch;
2578 $entry->{comment} = $comment if $comment;
2579 $entry->{errors} = $errors if $errors;
2581 if ($verbose && $errors) {
2582 warn "$prefix - errors in ipset '$group': $line\n";
2583 foreach my $p (keys %{$errors}) {
2584 warn " $p: $errors->{$p}\n";
2588 push @{$res->{$section}->{$group}}, $entry;
2590 warn "$prefix: skip line - unknown section\n";
2598 sub parse_hostfw_config {
2599 my ($filename, $fh, $cluster_conf, $verbose) = @_;
2601 my $empty_conf = { rules => [], options => {}};
2603 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host
');
2606 sub parse_vmfw_config {
2607 my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_;
2614 ipset_comments => {},
2617 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env);
2620 sub parse_clusterfw_config {
2621 my ($filename, $fh, $verbose) = @_;
2631 group_comments => {},
2633 ipset_comments => {},
2636 return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster
');
2640 my ($code, @param) = @_;
2644 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2651 sub read_local_vm_config {
2656 my $vmdata = { openvz => $openvz, qemu => $qemu };
2658 my $vmlist = PVE::Cluster::get_vmlist();
2659 return $vmdata if !$vmlist || !$vmlist->{ids};
2660 my $ids = $vmlist->{ids};
2662 foreach my $vmid (keys %$ids) {
2663 next if !$vmid; # skip VE0
2664 my $d = $ids->{$vmid};
2665 next if !$d->{node} || $d->{node} ne $nodename;
2666 next if !$d->{type};
2667 if ($d->{type} eq 'openvz
') {
2668 if ($have_pve_manager) {
2669 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
2670 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2671 $openvz->{$vmid} = $conf;
2674 } elsif ($d->{type} eq 'qemu
') {
2675 if ($have_qemu_server) {
2676 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2677 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2678 $qemu->{$vmid} = $conf;
2687 sub load_vmfw_conf {
2688 my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_;
2692 $dir = $pvefw_conf_dir if !defined($dir);
2694 my $filename = "$dir/$vmid.fw";
2695 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2696 $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose);
2697 $vmfw_conf->{vmid} = $vmid;
2703 my $format_rules = sub {
2704 my ($rules, $allow_iface) = @_;
2708 foreach my $rule (@$rules) {
2709 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
' || $rule->{type} eq 'group
') {
2710 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2711 $raw .= uc($rule->{type});
2712 if ($rule->{macro}) {
2713 $raw .= " $rule->{macro}($rule->{action})";
2715 $raw .= " " . $rule->{action};
2717 if ($allow_iface && $rule->{iface}) {
2718 $raw .= " -i $rule->{iface}";
2721 if ($rule->{type} ne 'group
') {
2722 $raw .= " -source $rule->{source}" if $rule->{source};
2723 $raw .= " -dest $rule->{dest}" if $rule->{dest};
2724 $raw .= " -p $rule->{proto}" if $rule->{proto};
2725 $raw .= " -dport $rule->{dport}" if $rule->{dport};
2726 $raw .= " -sport $rule->{sport}" if $rule->{sport};
2729 $raw .= " # " . encode('utf8
', $rule->{comment})
2730 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2733 die "unknown rule type '$rule->{type
}'";
2740 my $format_options = sub {
2745 $raw .= "[OPTIONS]\n\n";
2746 foreach my $opt (keys %$options) {
2747 $raw .= "$opt: $options->{$opt}\n";
2754 my $format_aliases = sub {
2759 $raw .= "[ALIASES]\n\n";
2760 foreach my $k (keys %$aliases) {
2761 my $e = $aliases->{$k};
2762 $raw .= "$e->{name} $e->{cidr}";
2763 $raw .= " # " . encode('utf8
', $e->{comment})
2764 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
2772 my $format_ipsets = sub {
2777 foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
2778 if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) {
2779 my $utf8comment = encode('utf8
', $comment);
2780 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2782 $raw .= "[IPSET $ipset]\n\n";
2784 my $options = $fw_conf->{ipset}->{$ipset};
2787 foreach my $entry (@$options) {
2788 $nethash->{$entry->{cidr}} = $entry;
2791 foreach my $cidr (sort keys %$nethash) {
2792 my $entry = $nethash->{$cidr};
2793 my $line = $entry->{nomatch} ? '!' : '';
2794 $line .= $entry->{cidr};
2795 $line .= " # " . encode('utf8
', $entry->{comment})
2796 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
2806 sub save_vmfw_conf {
2807 my ($vmid, $vmfw_conf) = @_;
2811 my $options = $vmfw_conf->{options};
2812 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2814 my $aliases = $vmfw_conf->{aliases};
2815 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2817 $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset};
2819 my $rules = $vmfw_conf->{rules} || [];
2820 if ($rules && scalar(@$rules)) {
2821 $raw .= "[RULES]\n\n";
2822 $raw .= &$format_rules($rules, 1);
2826 mkdir $pvefw_conf_dir;
2828 my $filename = "$pvefw_conf_dir/$vmid.fw";
2829 PVE::Tools::file_set_contents($filename, $raw);
2832 sub read_vm_firewall_configs {
2833 my ($cluster_conf, $vmdata, $dir, $verbose) = @_;
2835 my $vmfw_configs = {};
2837 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2838 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm
', $vmid, $dir, $verbose);
2839 next if !$vmfw_conf->{options}; # skip if file does not exists
2840 $vmfw_configs->{$vmid} = $vmfw_conf;
2842 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2843 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct
', $vmid, $dir, $verbose);
2844 next if !$vmfw_conf->{options}; # skip if file does not exists
2845 $vmfw_configs->{$vmid} = $vmfw_conf;
2848 return $vmfw_configs;
2851 sub get_option_log_level {
2852 my ($options, $k) = @_;
2854 my $v = $options->{$k};
2855 $v = $default_log_level if !defined($v);
2857 return undef if $v eq '' || $v eq 'nolog
';
2859 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2861 return $v if ($v >= 0) && ($v <= 7);
2863 warn "unknown log level ($k = '$v')\n";
2868 sub generate_std_chains {
2869 my ($ruleset, $options, $ipversion) = @_;
2871 my $std_chains = $pve_std_chains->{$ipversion} || die "internal error";
2873 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2877 if ($ipversion == 4) {
2878 # same as shorewall smurflog.
2879 $chain = 'PVEFW-smurflog
';
2880 $std_chains->{$chain} = [];
2882 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2883 push @{$std_chains->{$chain}}, "-j DROP";
2886 # same as shorewall logflags action.
2887 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2888 $chain = 'PVEFW-logflags
';
2889 $std_chains->{$chain} = [];
2891 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2892 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2893 push @{$std_chains->{$chain}}, "-j DROP";
2895 foreach my $chain (keys %$std_chains) {
2896 ruleset_create_chain($ruleset, $chain);
2897 foreach my $rule (@{$std_chains->{$chain}}) {
2899 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule);
2901 ruleset_addrule($ruleset, $chain, $rule);
2907 sub generate_ipset_chains {
2908 my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; #fixme
2910 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2912 my $options = $fw_conf->{ipset}->{$ipset};
2916 foreach my $entry (@$options) {
2917 next if $entry->{errors}; # skip entries with errors
2920 if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
2921 ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
2923 ($cidr, $ver) = parse_ip_or_cidr($entry->{cidr});
2925 #http://backreference.org/2013/03/01/ipv6-address-normalization/
2927 my $ipv6 = inet_pton(AF_INET6, lc($cidr));
2928 $cidr = inet_ntop(AF_INET6, $ipv6);
2934 $nethash->{$ver}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} };
2939 foreach my $ipversion (4, 6) {
2940 my $data = $nethash->{$ipversion};
2942 my $name = compute_ipset_chain_name($fw_conf->{vmid}, $ipset, $ipversion);
2944 my $hashsize = scalar(@$options);
2945 if ($hashsize <= 64) {
2948 $hashsize = round_powerof2($hashsize);
2951 my $family = $ipversion == "6" ? "inet6" : "inet";
2953 $ipset_ruleset->{$name} = ["create $name hash:net family $family hashsize $hashsize maxelem $hashsize"];
2955 foreach my $cidr (sort keys %$data) {
2956 my $entry = $data->{$cidr};
2958 my $cmd = "add $name $cidr";
2959 if ($entry->{nomatch}) {
2960 if ($feature_ipset_nomatch) {
2961 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
2963 warn "ignore !$cidr - nomatch not supported by kernel\n";
2966 push @{$ipset_ruleset->{$name}}, $cmd;
2973 sub round_powerof2 {
2977 $int |= $int >> $_ foreach (1,2,4,8,16);
2981 sub load_clusterfw_conf {
2982 my ($filename, $verbose) = @_;
2984 $filename = $clusterfw_conf_filename if !defined($filename);
2986 my $cluster_conf = {};
2987 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2988 $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose);
2991 return $cluster_conf;
2994 sub save_clusterfw_conf {
2995 my ($cluster_conf) = @_;
2999 my $options = $cluster_conf->{options};
3000 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3002 my $aliases = $cluster_conf->{aliases};
3003 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
3005 $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
3007 my $rules = $cluster_conf->{rules};
3008 if ($rules && scalar(@$rules)) {
3009 $raw .= "[RULES]\n\n";
3010 $raw .= &$format_rules($rules, 1);
3014 if ($cluster_conf->{groups}) {
3015 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
3016 my $rules = $cluster_conf->{groups}->{$group};
3017 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
3018 my $utf8comment = encode('utf8
', $comment);
3019 $raw .= "[group $group] # $utf8comment\n\n";
3021 $raw .= "[group $group]\n\n";
3024 $raw .= &$format_rules($rules, 0);
3029 mkdir $pvefw_conf_dir;
3030 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
3033 sub load_hostfw_conf {
3034 my ($cluster_conf, $filename, $verbose) = @_;
3036 $filename = $hostfw_conf_filename if !defined($filename);
3038 my $hostfw_conf = {};
3039 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3040 $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose);
3042 return $hostfw_conf;
3045 sub save_hostfw_conf {
3046 my ($hostfw_conf) = @_;
3050 my $options = $hostfw_conf->{options};
3051 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3053 my $rules = $hostfw_conf->{rules};
3054 if ($rules && scalar(@$rules)) {
3055 $raw .= "[RULES]\n\n";
3056 $raw .= &$format_rules($rules, 1);
3060 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
3064 my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_;
3068 if ($vmdata) { # test mode
3069 my $testdir = $vmdata->{testdir} || die "no test directory specified";
3070 my $filename = "$testdir/cluster.fw";
3071 $cluster_conf = load_clusterfw_conf($filename, $verbose);
3073 $filename = "$testdir/host.fw";
3074 $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose);
3076 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose);
3077 } else { # normal operation
3078 $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf;
3080 $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf;
3082 $vmdata = read_local_vm_config();
3083 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose);
3086 my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose);
3087 my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose);
3089 return ($ruleset, $ipset_ruleset, $rulesetv6);
3092 sub compile_iptables_filter {
3093 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_;
3095 $cluster_conf->{ipset}->{venet0} = [];
3096 my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0
', $ipversion);
3099 if ($cluster_conf->{aliases}->{local_network}) {
3100 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3103 ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
3105 $cluster_conf->{aliases}->{local_network} = {
3106 name => 'local_network
', cidr => $localnet, ipversion => $localnet_ver };
3109 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
3111 return ({}, {}) if !$cluster_conf->{options}->{enable};
3115 ruleset_create_chain($ruleset, "PVEFW-INPUT");
3116 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
3118 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3120 my $hostfw_options = $hostfw_conf->{options} || {};
3122 # fixme: what log level should we use here?
3123 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
3125 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT");
3128 ruleset_create_chain($ruleset, "PVEFW-VENET-OUT");
3129 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3130 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3132 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
3133 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3135 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN");
3137 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
3138 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT");
3140 ruleset_create_chain($ruleset, "PVEFW-VENET-IN");
3141 ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3143 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3145 generate_std_chains($ruleset, $hostfw_options, $ipversion);
3147 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
3149 my $ipset_ruleset = {};
3151 if ($hostfw_enable) {
3152 eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
3153 warn $@ if $@; # just to be sure - should not happen
3156 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3158 # generate firewall rules for QEMU VMs
3159 foreach my $vmid (keys %{$vmdata->{qemu}}) {
3161 my $conf = $vmdata->{qemu}->{$vmid};
3162 my $vmfw_conf = $vmfw_configs->{$vmid};
3163 return if !$vmfw_conf;
3165 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3167 foreach my $netid (keys %$conf) {
3168 next if $netid !~ m/^net(\d+)$/;
3169 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3170 next if !$net->{firewall};
3171 my $iface = "tap${vmid}i$1";
3173 my $macaddr = $net->{macaddr};
3174 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3175 $vmfw_conf, $vmid, 'IN
', $ipversion);
3176 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3177 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3180 warn $@ if $@; # just to be sure - should not happen
3183 # generate firewall rules for OpenVZ containers
3184 foreach my $vmid (keys %{$vmdata->{openvz}}) {
3186 my $conf = $vmdata->{openvz}->{$vmid};
3188 my $vmfw_conf = $vmfw_configs->{$vmid};
3189 return if !$vmfw_conf;
3191 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3193 if ($vmfw_conf->{options}->{enable}) {
3194 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
3195 my $ip = $conf->{ip_address}->{value};
3200 foreach my $singleip (split(',', $ip)) {
3201 my $singleip_ver = parse_address_list($singleip); # make sure we have a valid $ip list
3202 push @{$cluster_conf->{ipset}->{venet0}}, { cidr => $singleip };
3203 push @ips, $singleip if $singleip_ver == $ipversion;
3207 my $ip_list = join(',', @ips);
3208 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'IN
', $ipversion);
3209 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'OUT
', $ipversion);
3214 if ($conf->{netif} && $conf->{netif}->{value}) {
3215 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
3216 foreach my $netid (keys %$netif) {
3217 my $d = $netif->{$netid};
3218 my $bridge = $d->{bridge};
3219 next if !$bridge || $bridge !~ m/^vmbr\d+(v(\d+))?f$/; # firewall enabled ?
3220 my $macaddr = $d->{mac};
3221 my $iface = $d->{host_ifname};
3222 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3223 $vmfw_conf, $vmid, 'IN
', $ipversion);
3224 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3225 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3229 warn $@ if $@; # just to be sure - should not happen
3232 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
3233 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS");
3236 generate_ipset_chains($ipset_ruleset, undef, $cluster_conf);
3238 return ($ruleset, $ipset_ruleset);
3241 sub get_ruleset_status {
3242 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
3244 my $statushash = {};
3246 foreach my $chain (sort keys %$ruleset) {
3247 my $sig = &$digest_fn($ruleset->{$chain});
3249 $statushash->{$chain}->{sig} = $sig;
3251 my $oldsig = $active_chains->{$chain};
3252 if (!defined($oldsig)) {
3253 $statushash->{$chain}->{action} = 'create
';
3255 if ($oldsig eq $sig) {
3256 $statushash->{$chain}->{action} = 'exists';
3258 $statushash->{$chain}->{action} = 'update
';
3261 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
3262 foreach my $cmd (@{$ruleset->{$chain}}) {
3263 print "\t$cmd\n" if $verbose;
3267 foreach my $chain (sort keys %$active_chains) {
3268 if (!defined($ruleset->{$chain})) {
3269 my $sig = $active_chains->{$chain};
3270 $statushash->{$chain}->{action} = 'delete';
3271 $statushash->{$chain}->{sig} = $sig;
3272 print "delete $chain ($sig)\n" if $verbose;
3279 sub print_sig_rule {
3280 my ($chain, $sig) = @_;
3282 # We just use this to store a SHA1 checksum used to detect changes
3283 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
3286 sub get_ruleset_cmdlist {
3287 my ($ruleset, $verbose, $iptablescmd) = @_;
3289 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
3291 my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
3292 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
3294 # create missing chains first
3295 foreach my $chain (sort keys %$ruleset) {
3296 my $stat = $statushash->{$chain};
3297 die "internal error" if !$stat;
3298 next if $stat->{action} ne 'create
';
3300 $cmdlist .= ":$chain - [0:0]\n";
3303 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3304 my $chain = "PVEFW-$h";
3305 if ($ruleset->{$chain} && !$hooks->{$h}) {
3306 $cmdlist .= "-A $h -j $chain\n";
3310 foreach my $chain (sort keys %$ruleset) {
3311 my $stat = $statushash->{$chain};
3312 die "internal error" if !$stat;
3314 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
3315 $cmdlist .= "-F $chain\n";
3316 foreach my $cmd (@{$ruleset->{$chain}}) {
3317 $cmdlist .= "$cmd\n";
3319 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
3320 } elsif ($stat->{action
} eq 'delete') {
3321 die "internal error"; # this should not happen
3322 } elsif ($stat->{action
} eq 'exists') {
3325 die "internal error - unknown status '$stat->{action}'";
3329 foreach my $chain (keys %$statushash) {
3330 next if $statushash->{$chain}->{action
} ne 'delete';
3331 $cmdlist .= "-F $chain\n";
3333 foreach my $chain (keys %$statushash) {
3334 next if $statushash->{$chain}->{action
} ne 'delete';
3335 next if $chain eq 'PVEFW-INPUT';
3336 next if $chain eq 'PVEFW-OUTPUT';
3337 next if $chain eq 'PVEFW-FORWARD';
3338 $cmdlist .= "-X $chain\n";
3341 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
3343 $cmdlist .= "COMMIT\n";
3345 return wantarray ?
($cmdlist, $changes) : $cmdlist;
3348 sub get_ipset_cmdlist
{
3349 my ($ruleset, $verbose) = @_;
3353 my $delete_cmdlist = "";
3355 my $active_chains = ipset_get_chains
();
3356 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
3358 # remove stale _swap chains
3359 foreach my $chain (keys %$active_chains) {
3360 if ($chain =~ m/^PVEFW-\S+_swap$/) {
3361 $cmdlist .= "destroy $chain\n";
3365 foreach my $chain (keys %$ruleset) {
3366 my $stat = $statushash->{$chain};
3367 die "internal error" if !$stat;
3369 if ($stat->{action
} eq 'create') {
3370 foreach my $cmd (@{$ruleset->{$chain}}) {
3371 $cmdlist .= "$cmd\n";
3376 foreach my $chain (keys %$ruleset) {
3377 my $stat = $statushash->{$chain};
3378 die "internal error" if !$stat;
3380 if ($stat->{action
} eq 'update') {
3381 my $chain_swap = $chain."_swap";
3383 foreach my $cmd (@{$ruleset->{$chain}}) {
3384 $cmd =~ s/$chain/$chain_swap/;
3385 $cmdlist .= "$cmd\n";
3387 $cmdlist .= "swap $chain_swap $chain\n";
3388 $cmdlist .= "flush $chain_swap\n";
3389 $cmdlist .= "destroy $chain_swap\n";
3393 # the remove unused chains
3394 foreach my $chain (keys %$statushash) {
3395 next if $statushash->{$chain}->{action
} ne 'delete';
3397 $delete_cmdlist .= "flush $chain\n";
3398 $delete_cmdlist .= "destroy $chain\n";
3401 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
3403 return ($cmdlist, $delete_cmdlist, $changes);
3407 my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_;
3409 enable_bridge_firewall
();
3411 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
3412 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
3414 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
3415 my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist
($rulesetv6, $verbose, "ip6tables");
3418 if ($ipset_changes) {
3419 print "ipset changes:\n";
3420 print $ipset_create_cmdlist if $ipset_create_cmdlist;
3421 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
3425 print "iptables changes:\n";
3430 print "ip6tables changes:\n";
3435 my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1";
3436 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_create_cmdlist || '');
3438 ipset_restore_cmdlist
($ipset_create_cmdlist);
3440 $tmpfile = "$pve_fw_status_dir/ip4cmdlist";
3441 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlist || '');
3443 iptables_restore_cmdlist
($cmdlist);
3445 $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
3446 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlistv6 || '');
3448 ip6tables_restore_cmdlist
($cmdlistv6);
3450 $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
3451 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_delete_cmdlist || '');
3453 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
3455 # test: re-read status and check if everything is up to date
3456 my $active_chains = iptables_get_chains
();
3457 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
3460 foreach my $chain (sort keys %$ruleset) {
3461 my $stat = $statushash->{$chain};
3462 if ($stat->{action
} ne 'exists') {
3463 warn "unable to update chain '$chain'\n";
3468 my $active_chainsv6 = iptables_get_chains
("ip6tables");
3469 my $statushashv6 = get_ruleset_status
($rulesetv6, $active_chainsv6, \
&iptables_chain_digest
, 0);
3471 foreach my $chain (sort keys %$rulesetv6) {
3472 my $stat = $statushashv6->{$chain};
3473 if ($stat->{action
} ne 'exists') {
3474 warn "unable to update chain '$chain'\n";
3479 die "unable to apply firewall changes\n" if $errors;
3481 update_nf_conntrack_max
($hostfw_conf);
3483 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
3487 sub update_nf_conntrack_max
{
3488 my ($hostfw_conf) = @_;
3490 my $max = 65536; # reasonable default
3492 my $options = $hostfw_conf->{options
} || {};
3494 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
3495 $max = $options->{nf_conntrack_max
};
3496 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
3499 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
3500 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
3502 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
3504 if ($current != $max) {
3505 my $hashsize = int($max/4);
3506 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
3507 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
3511 sub update_nf_conntrack_tcp_timeout_established
{
3512 my ($hostfw_conf) = @_;
3514 my $options = $hostfw_conf->{options
} || {};
3516 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
3518 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
3521 sub remove_pvefw_chains
{
3523 PVE
::Firewall
::remove_pvefw_chains_iptables
("iptables");
3524 PVE
::Firewall
::remove_pvefw_chains_iptables
("ip6tables");
3525 PVE
::Firewall
::remove_pvefw_chains_ipset
();
3529 sub remove_pvefw_chains_iptables
{
3530 my ($iptablescmd) = @_;
3532 my ($chash, $hooks) = iptables_get_chains
($iptablescmd);
3533 my $cmdlist = "*filter\n";
3535 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3537 $cmdlist .= "-D $h -j PVEFW-$h\n";
3541 foreach my $chain (keys %$chash) {
3542 $cmdlist .= "-F $chain\n";
3545 foreach my $chain (keys %$chash) {
3546 $cmdlist .= "-X $chain\n";
3548 $cmdlist .= "COMMIT\n";
3550 if($iptablescmd eq "ip6tables") {
3551 ip6tables_restore_cmdlist
($cmdlist);
3553 iptables_restore_cmdlist
($cmdlist);
3557 sub remove_pvefw_chains_ipset
{
3559 my $ipset_chains = ipset_get_chains
();
3563 foreach my $chain (keys %$ipset_chains) {
3564 $cmdlist .= "flush $chain\n";
3565 $cmdlist .= "destroy $chain\n";
3568 ipset_restore_cmdlist
($cmdlist) if $cmdlist;
3572 my $cluster_conf = load_clusterfw_conf
();
3573 my $cluster_options = $cluster_conf->{options
};
3574 my $enable = $cluster_options->{enable
};
3578 # load required modules here
3584 my $cluster_conf = load_clusterfw_conf
();
3585 my $cluster_options = $cluster_conf->{options
};
3587 if (!$cluster_options->{enable
}) {
3588 PVE
::Firewall
::remove_pvefw_chains
();
3592 my $hostfw_conf = load_hostfw_conf
($cluster_conf);
3594 my ($ruleset, $ipset_ruleset, $rulesetv6) = compile
($cluster_conf, $hostfw_conf);
3596 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6);