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 # imported/converted from: /usr/share/shorewall/macro.*
136 my $pve_fw_macros = {
139 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
140 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
143 "Auth (identd) traffic",
144 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
147 "Border Gateway Protocol traffic",
148 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
151 "BitTorrent traffic for BitTorrent 3.1 and earlier",
152 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
153 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
156 "BitTorrent traffic for BitTorrent 3.2 and later",
157 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
158 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
161 "Concurrent Versions System pserver traffic",
162 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
165 "Citrix/ICA traffic (ICA, ICA Browser, CGP)",
166 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
167 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
168 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
171 "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)",
172 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
173 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
176 "Distributed Checksum Clearinghouse spam filtering mechanism",
177 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
180 "Forwarded DHCP traffic",
181 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
184 "Domain Name System traffic (upd and tcp)",
185 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
186 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
189 "Distributed Compiler service",
190 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
193 "File Transfer Protocol",
194 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
197 "Finger protocol (RFC 742)",
198 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
201 "GNUnet secure peer-to-peer networking traffic",
202 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
203 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
204 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
205 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
208 "Generic Routing Encapsulation tunneling protocol",
209 { action
=> 'PARAM', proto
=> '47' },
212 "Git distributed revision control traffic",
213 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
216 "OpenPGP HTTP keyserver protocol traffic",
217 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
220 "Hypertext Transfer Protocol (WWW)",
221 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
224 "Hypertext Transfer Protocol (WWW) over SSL",
225 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
228 "Internet Cache Protocol V2 (Squid) traffic",
229 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
232 "AOL Instant Messenger traffic",
233 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
236 "Internet Message Access Protocol",
237 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
240 "Internet Message Access Protocol over SSL",
241 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
244 "IPIP capsulation traffic",
245 { action
=> 'PARAM', proto
=> '94' },
249 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
250 { action
=> 'PARAM', proto
=> '50' },
253 "IPsec authentication (AH) traffic",
254 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
255 { action
=> 'PARAM', proto
=> '51' },
258 "IPsec traffic and Nat-Traversal",
259 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
260 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
261 { action
=> 'PARAM', proto
=> '50' },
264 "Internet Relay Chat traffic",
265 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
268 "HP Jetdirect printing",
269 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
272 "Layer 2 Tunneling Protocol traffic",
273 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
276 "Lightweight Directory Access Protocol traffic",
277 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
280 "Secure Lightweight Directory Access Protocol traffic",
281 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
284 "Microsoft Notification Protocol",
285 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
288 "Microsoft SQL Server",
289 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
292 "Mail traffic (SMTP, SMTPS, Submission)",
293 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
294 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
295 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
298 "Munin networked resource monitoring traffic",
299 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
303 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
306 "NNTP traffic (Usenet).",
307 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
310 "Encrypted NNTP traffic (Usenet)",
311 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
314 "Network Time Protocol (ntpd)",
315 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
318 "OSPF multicast traffic",
319 { action
=> 'PARAM', proto
=> '89' },
323 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
326 "Symantec PCAnywere (tm)",
327 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
328 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
332 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
335 "Encrypted POP3 traffic",
336 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
339 "Point-to-Point Tunneling Protocol",
340 { action
=> 'PARAM', proto
=> '47' },
341 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
345 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
349 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
352 "Line Printer protocol printing",
353 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
356 "Microsoft Remote Desktop Protocol traffic",
357 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
360 "Routing Information Protocol (bidirectional)",
361 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
364 "BIND remote management protocol",
365 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
368 "Razor Antispam System",
369 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
372 "Remote time retrieval (rdate)",
373 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
377 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
380 "SANE network scanning",
381 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
384 "Microsoft SMB traffic",
385 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
386 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
387 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
388 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
391 "Samba Web Administration Tool",
392 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
395 "Simple Mail Transfer Protocol",
396 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
399 "Encrypted Simple Mail Transfer Protocol",
400 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
403 "Simple Network Management Protocol",
404 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
405 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
408 "Spam Assassin SPAMD traffic",
409 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
412 "Secure shell traffic",
413 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
416 "Subversion server (svnserve)",
417 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
420 "SixXS IPv6 Deployment and Tunnel Broker",
421 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
422 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
423 { action
=> 'PARAM', proto
=> '41' },
424 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
427 "Squid web proxy traffic",
428 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
431 "Mail message submission traffic",
432 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
435 "Syslog protocol (RFC 5424) traffic",
436 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
437 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
440 "Trivial File Transfer Protocol traffic",
441 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
445 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
449 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
452 "RFC 868 Time protocol",
453 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
456 "Traceroute (for up to 30 hops) traffic",
457 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
458 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
461 "VNC traffic for VNC display's 0 - 99",
462 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5999' },
465 "VNC traffic from Vncservers to Vncviewers in listen mode",
466 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
469 "WWW traffic (HTTP and HTTPS)",
470 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
471 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
474 "Web Cache/Proxy traffic (port 8080)",
475 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
479 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
482 "Whois (nicname, RFC 3912) traffic",
483 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
487 my $pve_fw_parsed_macros;
488 my $pve_fw_macro_descr;
489 my $pve_fw_preferred_macro_names = {};
491 my $pve_std_chains = {};
492 $pve_std_chains->{4} = {
493 'PVEFW-SET-ACCEPT-MARK' => [
494 "-j MARK --set-mark 1",
496 'PVEFW-DropBroadcast' => [
497 # same as shorewall 'Broadcast'
498 # simply DROP BROADCAST/MULTICAST/ANYCAST
499 # we can use this to reduce logging
500 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
501 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
502 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
503 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
506 # same as shorewall 'reject'
507 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
508 { action
=> 'DROP', source
=> '224.0.0.0/4' },
509 { action
=> 'DROP', proto
=> 'icmp' },
510 "-p tcp -j REJECT --reject-with tcp-reset",
511 "-p udp -j REJECT --reject-with icmp-port-unreachable",
512 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
513 "-j REJECT --reject-with icmp-host-prohibited",
516 # same as shorewall 'Drop', which is equal to DROP,
517 # but REJECT/DROP some packages to reduce logging,
518 # and ACCEPT critical ICMP types
519 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
520 # we are not interested in BROADCAST/MULTICAST/ANYCAST
521 { action
=> 'PVEFW-DropBroadcast' },
522 # ACCEPT critical ICMP types
523 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
524 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
525 # Drop packets with INVALID state
526 "-m conntrack --ctstate INVALID -j DROP",
527 # Drop Microsoft SMB noise
528 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
529 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
530 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
531 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
532 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
533 # Drop new/NotSyn traffic so that it doesn't get logged
534 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
536 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
539 # same as shorewall 'Reject', which is equal to Reject,
540 # but REJECT/DROP some packages to reduce logging,
541 # and ACCEPT critical ICMP types
542 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
543 # we are not interested in BROADCAST/MULTICAST/ANYCAST
544 { action
=> 'PVEFW-DropBroadcast' },
545 # ACCEPT critical ICMP types
546 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
547 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
548 # Drop packets with INVALID state
549 "-m conntrack --ctstate INVALID -j DROP",
550 # Drop Microsoft SMB noise
551 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
552 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
553 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
554 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
555 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
556 # Drop new/NotSyn traffic so that it doesn't get logged
557 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
559 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
561 'PVEFW-tcpflags' => [
562 # same as shorewall tcpflags action.
563 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
564 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
565 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
566 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
567 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
568 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
571 # same as shorewall smurfs action
572 # Filter packets for smurfs (packets with a broadcast address as the source).
573 "-s 0.0.0.0/32 -j RETURN",
574 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
575 "-s 224.0.0.0/4 -g PVEFW-smurflog",
579 $pve_std_chains->{6} = {
580 'PVEFW-SET-ACCEPT-MARK' => [
581 "-j MARK --set-mark 1",
583 'PVEFW-DropBroadcast' => [
584 # same as shorewall 'Broadcast'
585 # simply DROP BROADCAST/MULTICAST/ANYCAST
586 # we can use this to reduce logging
587 #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6
588 # ipv6 addrtype does not work with kernel 2.6.32
589 #{ action => 'DROP', dsttype => 'MULTICAST' },
590 #{ action => 'DROP', dsttype => 'ANYCAST' },
591 { action
=> 'DROP', dest
=> 'ff00::/8' },
592 #{ action => 'DROP', dest => '224.0.0.0/4' },
595 # same as shorewall 'reject'
596 #{ action => 'DROP', dsttype => 'BROADCAST' },
597 #{ action => 'DROP', source => '224.0.0.0/4' },
598 { action
=> 'DROP', proto
=> 'icmpv6' },
599 "-p tcp -j REJECT --reject-with tcp-reset",
600 #"-p udp -j REJECT --reject-with icmp-port-unreachable",
601 #"-p icmp -j REJECT --reject-with icmp-host-unreachable",
602 #"-j REJECT --reject-with icmp-host-prohibited",
605 # same as shorewall 'Drop', which is equal to DROP,
606 # but REJECT/DROP some packages to reduce logging,
607 # and ACCEPT critical ICMP types
608 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
609 # we are not interested in BROADCAST/MULTICAST/ANYCAST
610 { action
=> 'PVEFW-DropBroadcast' },
611 # ACCEPT critical ICMP types
612 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
613 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
614 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
616 # Drop packets with INVALID state
617 "-m conntrack --ctstate INVALID -j DROP",
618 # Drop Microsoft SMB noise
619 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
620 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
621 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
622 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
623 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
624 # Drop new/NotSyn traffic so that it doesn't get logged
625 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
627 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
630 # same as shorewall 'Reject', which is equal to Reject,
631 # but REJECT/DROP some packages to reduce logging,
632 # and ACCEPT critical ICMP types
633 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
634 # we are not interested in BROADCAST/MULTICAST/ANYCAST
635 { action
=> 'PVEFW-DropBroadcast' },
636 # ACCEPT critical ICMP types
637 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
638 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
639 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
641 # Drop packets with INVALID state
642 "-m conntrack --ctstate INVALID -j DROP",
643 # Drop Microsoft SMB noise
644 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
645 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
646 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
647 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
648 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
649 # Drop new/NotSyn traffic so that it doesn't get logged
650 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
652 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
654 'PVEFW-tcpflags' => [
655 # same as shorewall tcpflags action.
656 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
657 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
658 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
659 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
660 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
661 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
665 # iptables -p icmp -h
666 my $icmp_type_names = {
669 'destination-unreachable' => 1,
670 'network-unreachable' => 1,
671 'host-unreachable' => 1,
672 'protocol-unreachable' => 1,
673 'port-unreachable' => 1,
674 'fragmentation-needed' => 1,
675 'source-route-failed' => 1,
676 'network-unknown' => 1,
678 'network-prohibited' => 1,
679 'host-prohibited' => 1,
680 'TOS-network-unreachable' => 1,
681 'TOS-host-unreachable' => 1,
682 'communication-prohibited' => 1,
683 'host-precedence-violation' => 1,
684 'precedence-cutoff' => 1,
685 'source-quench' => 1,
687 'network-redirect' => 1,
688 'host-redirect' => 1,
689 'TOS-network-redirect' => 1,
690 'TOS-host-redirect' => 1,
692 'router-advertisement' => 1,
693 'router-solicitation' => 1,
694 'time-exceeded' => 1,
695 'ttl-zero-during-transit' => 1,
696 'ttl-zero-during-reassembly' => 1,
697 'parameter-problem' => 1,
698 'ip-header-bad' => 1,
699 'required-option-missing' => 1,
700 'timestamp-request' => 1,
701 'timestamp-reply' => 1,
702 'address-mask-request' => 1,
703 'address-mask-reply' => 1,
706 # ip6tables -p icmpv6 -h
708 my $icmpv6_type_names = {
710 'destination-unreachable' => 1,
712 'communication-prohibited' => 1,
713 'address-unreachable' => 1,
714 'port-unreachable' => 1,
715 'packet-too-big' => 1,
716 'time-exceeded' => 1,
717 'ttl-zero-during-transit' => 1,
718 'ttl-zero-during-reassembly' => 1,
719 'parameter-problem' => 1,
721 'unknown-header-type' => 1,
722 'unknown-option' => 1,
725 'router-solicitation' => 1,
726 'router-advertisement' => 1,
727 'neighbour-solicitation' => 1,
728 'neighbour-advertisement' => 1,
732 sub init_firewall_macros
{
734 $pve_fw_parsed_macros = {};
736 foreach my $k (keys %$pve_fw_macros) {
737 my $lc_name = lc($k);
738 my $macro = $pve_fw_macros->{$k};
739 if (!ref($macro->[0])) {
740 $pve_fw_macro_descr->{$k} = shift @$macro;
742 $pve_fw_preferred_macro_names->{$lc_name} = $k;
743 $pve_fw_parsed_macros->{$k} = $macro;
747 init_firewall_macros
();
750 return wantarray ?
($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
755 sub get_etc_services
{
757 return $etc_services if $etc_services;
759 my $filename = "/etc/services";
761 my $fh = IO
::File-
>new($filename, O_RDONLY
);
763 warn "unable to read '$filename' - $!\n";
769 while (my $line = <$fh>) {
771 next if $line =~m/^#/;
772 next if ($line =~m/^\s*$/);
774 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
775 $services->{byid
}->{$2}->{name
} = $1;
776 $services->{byid
}->{$2}->{port
} = $2;
777 $services->{byid
}->{$2}->{$3} = 1;
778 $services->{byname
}->{$1} = $services->{byid
}->{$2};
784 $etc_services = $services;
787 return $etc_services;
792 sub get_etc_protocols
{
793 return $etc_protocols if $etc_protocols;
795 my $filename = "/etc/protocols";
797 my $fh = IO
::File-
>new($filename, O_RDONLY
);
799 warn "unable to read '$filename' - $!\n";
805 while (my $line = <$fh>) {
807 next if $line =~m/^#/;
808 next if ($line =~m/^\s*$/);
810 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
811 $protocols->{byid
}->{$2}->{name
} = $1;
812 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
818 # add special case for ICMP v6
819 $protocols->{byid
}->{icmpv6
}->{name
} = "icmpv6";
820 $protocols->{byname
}->{icmpv6
} = $protocols->{byid
}->{icmpv6
};
822 $etc_protocols = $protocols;
824 return $etc_protocols;
827 my $ipv4_mask_hash_localnet = {
829 '255.255.128.0' => 17,
830 '255.255.192.0' => 18,
831 '255.255.224.0' => 19,
832 '255.255.240.0' => 20,
833 '255.255.248.0' => 21,
834 '255.255.252.0' => 22,
835 '255.255.254.0' => 23,
836 '255.255.255.0' => 24,
837 '255.255.255.128' => 25,
838 '255.255.255.192' => 26,
839 '255.255.255.224' => 27,
840 '255.255.255.240' => 28,
841 '255.255.255.248' => 29,
842 '255.255.255.252' => 30,
848 my ($new_value) = @_;
850 $__local_network = $new_value if defined($new_value);
852 return $__local_network if defined($__local_network);
855 my $nodename = PVE
::INotify
::nodename
();
857 my $ip = PVE
::Cluster
::remote_node_ip
($nodename);
859 my $testip = Net
::IP-
>new($ip);
861 my $routes = PVE
::ProcFSTools
::read_proc_net_route
();
862 foreach my $entry (@$routes) {
863 my $mask = $ipv4_mask_hash_localnet->{$entry->{mask
}};
864 next if !defined($mask);
865 return if $mask eq '0.0.0.0';
866 my $cidr = "$entry->{dest}/$mask";
867 my $testnet = Net
::IP-
>new($cidr);
868 if ($testnet->overlaps($testip)) {
869 $__local_network = $cidr;
876 return $__local_network;
879 # ipset names are limited to 31 characters,
880 # and we use '-v4' or '-v6' to indicate IP versions,
881 # and we use '_swap' suffix for atomic update,
882 # for example PVEFW-${VMID}-${ipset_name}_swap
884 my $max_iptables_ipset_name_length = 31 - length("_swap") - length("-v4");
886 sub compute_ipset_chain_name
{
887 my ($vmid, $ipset_name) = @_;
889 $vmid = 0 if !defined($vmid);
891 my $id = "$vmid-${ipset_name}";
893 if ((length($id) + 6) > $max_iptables_ipset_name_length) {
894 $id = PVE
::Tools
::fnv31a_hex
($id);
900 sub compute_ipfilter_ipset_name
{
903 return "ipfilter-$iface";
906 sub parse_address_list
{
909 if ($str =~ m/^(\+)(\S+)$/) { # ipset ref
910 die "ipset name too long\n" if length($str) > ($max_ipset_name_length + 1);
914 if ($str =~ m/^${ip_alias_pattern}$/) {
915 die "alias name too long\n" if length($str) > $max_alias_name_length;
923 foreach my $elem (split(/,/, $str)) {
925 my $ip = Net
::IP-
>new($elem);
927 my $err = Net
::IP
::Error
();
928 die "invalid IP address: $err\n";
930 $iprange = 1 if $elem =~ m/-/;
932 my $new_ipversion = Net
::IP
::ip_is_ipv6
($ip->ip()) ?
6 : 4;
934 die "detected mixed ipv4/ipv6 addresses in address list '$str'\n"
935 if $ipversion && ($new_ipversion != $ipversion);
937 $ipversion = $new_ipversion;
940 die "you can't use a range in a list\n" if $iprange && $count > 1;
945 sub parse_port_name_number_or_range
{
948 my $services = PVE
::Firewall
::get_etc_services
();
952 foreach my $item (split(/,/, $str)) {
954 if ($item =~ m/^(\d+):(\d+)$/) {
955 my ($port1, $port2) = ($1, $2);
956 die "invalid port '$port1'\n" if $port1 > 65535;
957 die "invalid port '$port2'\n" if $port2 > 65535;
958 } elsif ($item =~ m/^(\d+)$/) {
960 die "invalid port '$port'\n" if $port > 65535;
962 if ($icmp_type_names->{$item}) {
964 } elsif ($icmpv6_type_names->{$item}) {
967 die "invalid port '$item'\n" if !$services->{byname
}->{$item};
972 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
977 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
978 sub pve_fw_verify_port_spec
{
981 parse_port_name_number_or_range
($portstr);
986 PVE
::JSONSchema
::register_format
('pve-fw-addr-spec', \
&pve_fw_verify_addr_spec
);
987 sub pve_fw_verify_addr_spec
{
990 parse_address_list
($list);
995 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
996 sub pve_fw_verify_protocol_spec
{
999 my $protocols = get_etc_protocols
();
1001 die "unknown protocol '$proto'\n" if $proto &&
1002 !(defined($protocols->{byname
}->{$proto}) ||
1003 defined($protocols->{byid
}->{$proto}));
1009 # helper function for API
1011 sub copy_opject_with_digest
{
1014 my $sha = Digest
::SHA-
>new('sha1');
1017 foreach my $k (sort keys %$object) {
1018 my $v = $object->{$k};
1019 next if !defined($v);
1021 $sha->add($k, ':', $v, "\n");
1024 my $digest = $sha->hexdigest;
1026 $res->{digest
} = $digest;
1028 return wantarray ?
($res, $digest) : $res;
1031 sub copy_list_with_digest
{
1034 my $sha = Digest
::SHA-
>new('sha1');
1037 foreach my $entry (@$list) {
1039 foreach my $k (sort keys %$entry) {
1040 my $v = $entry->{$k};
1041 next if !defined($v);
1043 # Note: digest ignores refs ($rule->{errors})
1044 $sha->add($k, ':', $v, "\n") if !ref($v); ;
1049 my $digest = $sha->hexdigest;
1051 foreach my $entry (@$res) {
1052 $entry->{digest
} = $digest;
1055 return wantarray ?
($res, $digest) : $res;
1058 my $rule_properties = {
1060 description
=> "Update rule at position <pos>.",
1065 digest
=> get_standard_option
('pve-config-digest'),
1069 enum
=> ['in', 'out', 'group'],
1072 description
=> "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
1075 pattern
=> $security_group_name_pattern,
1084 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
1086 type
=> 'string', format
=> 'pve-fw-addr-spec',
1090 type
=> 'string', format
=> 'pve-fw-addr-spec',
1094 type
=> 'string', format
=> 'pve-fw-protocol-spec',
1102 type
=> 'string', format
=> 'pve-fw-port-spec',
1106 type
=> 'string', format
=> 'pve-fw-port-spec',
1115 sub add_rule_properties
{
1116 my ($properties) = @_;
1118 foreach my $k (keys %$rule_properties) {
1119 my $h = $rule_properties->{$k};
1120 # copy data, so that we can modify later without side effects
1121 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
1127 sub delete_rule_properties
{
1128 my ($rule, $delete_str) = @_;
1130 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
1131 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
1132 if !defined($rule_properties->{$opt});
1133 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
1134 if $opt eq 'type' || $opt eq 'action';
1135 delete $rule->{$opt};
1141 my $apply_macro = sub {
1142 my ($macro_name, $param, $verify) = @_;
1144 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1145 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1149 foreach my $templ (@$macro_rules) {
1151 my $param_used = {};
1152 foreach my $k (keys %$templ) {
1153 my $v = $templ->{$k};
1154 if ($v eq 'PARAM') {
1156 $param_used->{$k} = 1;
1157 } elsif ($v eq 'DEST') {
1158 $v = $param->{dest
};
1159 $param_used->{dest
} = 1;
1160 } elsif ($v eq 'SOURCE') {
1161 $v = $param->{source
};
1162 $param_used->{source
} = 1;
1166 my $msg = "missing parameter '$k' in macro '$macro_name'";
1167 raise_param_exc
({ macro => $msg }) if $verify;
1172 foreach my $k (keys %$param) {
1173 next if $k eq 'macro';
1174 next if !defined($param->{$k});
1175 next if $param_used->{$k};
1176 if (defined($rule->{$k})) {
1177 if ($rule->{$k} ne $param->{$k}) {
1178 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
1179 raise_param_exc
({ $k => $msg }) if $verify;
1183 $rule->{$k} = $param->{$k};
1186 push @$rules, $rule;
1192 my $rule_env_iface_lookup = {
1201 my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_;
1203 my $allow_groups = $rule_env eq 'group' ?
0 : 1;
1205 my $allow_iface = $rule_env_iface_lookup->{$rule_env};
1206 die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen
1208 my $errors = $rule->{errors
} || {};
1210 my $error_count = 0;
1212 my $add_error = sub {
1213 my ($param, $msg) = @_;
1215 raise_param_exc
({ $param => $msg }) if !$noerr;
1217 $errors->{$param} = $msg if !$errors->{$param};
1220 my $check_ipset_or_alias_property = sub {
1221 my ($name, $expected_ipversion) = @_;
1223 if (my $value = $rule->{$name}) {
1224 if ($value =~ m/^\+/) {
1225 if ($value =~ m/^\+(${ipset_name_pattern})$/) {
1226 &$add_error($name, "no such ipset '$1'")
1227 if !($cluster_conf->{ipset
}->{$1} || ($fw_conf && $fw_conf->{ipset
}->{$1}));
1230 &$add_error($name, "invalid ipset name '$value'");
1232 } elsif ($value =~ m/^${ip_alias_pattern}$/){
1233 my $alias = lc($value);
1234 &$add_error($name, "no such alias '$value'")
1235 if !($cluster_conf->{aliases
}->{$alias} || ($fw_conf && $fw_conf->{aliases
}->{$alias}));
1237 my $e = $fw_conf->{aliases
}->{$alias} if $fw_conf;
1238 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1240 die "detected mixed ipv4/ipv6 adresses in rule\n"
1241 if $expected_ipversion && ($expected_ipversion != $e->{ipversion
});
1246 my $type = $rule->{type
};
1247 my $action = $rule->{action
};
1249 &$add_error('type', "missing property") if !$type;
1250 &$add_error('action', "missing property") if !$action;
1253 if ($type eq 'in' || $type eq 'out') {
1254 &$add_error('action', "unknown action '$action'")
1255 if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/);
1256 } elsif ($type eq 'group') {
1257 &$add_error('type', "security groups not allowed")
1259 &$add_error('action', "invalid characters in security group name")
1260 if $action && ($action !~ m/^${security_group_name_pattern}$/);
1262 &$add_error('type', "unknown rule type '$type'");
1266 if ($rule->{iface
}) {
1267 &$add_error('type', "parameter -i not allowed for this rule type")
1269 eval { PVE
::JSONSchema
::pve_verify_iface
($rule->{iface
}); };
1270 &$add_error('iface', $@) if $@;
1271 if ($rule_env eq 'vm') {
1272 &$add_error('iface', "value does not match the regex pattern 'net\\d+'")
1273 if $rule->{iface
} !~ m/^net(\d+)$/;
1274 } elsif ($rule_env eq 'ct') {
1275 &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'")
1276 if $rule->{iface
} !~ m/^(venet|eth(\d+))$/;
1280 if ($rule->{macro}) {
1281 if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) {
1282 $rule->{macro} = $preferred_name;
1284 &$add_error('macro', "unknown macro '$rule->{macro}'");
1289 my $set_ip_version = sub {
1292 die "detected mixed ipv4/ipv6 adresses in rule\n"
1293 if $ipversion && ($vers != $ipversion);
1298 if ($rule->{proto
}) {
1299 eval { pve_fw_verify_protocol_spec
($rule->{proto
}); };
1300 &$add_error('proto', $@) if $@;
1301 &$set_ip_version(4) if $rule->{proto
} eq 'icmp';
1302 &$set_ip_version(6) if $rule->{proto
} eq 'icmpv6';
1305 if ($rule->{dport
}) {
1306 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1307 &$add_error('dport', $@) if $@;
1308 &$add_error('proto', "missing property - 'dport' requires this property")
1312 if ($rule->{sport
}) {
1313 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1314 &$add_error('sport', $@) if $@;
1315 &$add_error('proto', "missing property - 'sport' requires this property")
1319 if ($rule->{source
}) {
1321 my $source_ipversion = parse_address_list
($rule->{source
});
1322 &$set_ip_version($source_ipversion);
1324 &$add_error('source', $@) if $@;
1325 &$check_ipset_or_alias_property('source', $ipversion);
1328 if ($rule->{dest
}) {
1330 my $dest_ipversion = parse_address_list
($rule->{dest
});
1331 &$set_ip_version($dest_ipversion);
1333 &$add_error('dest', $@) if $@;
1334 &$check_ipset_or_alias_property('dest', $ipversion);
1337 if ($rule->{macro} && !$error_count) {
1338 eval { &$apply_macro($rule->{macro}, $rule, 1); };
1340 if (ref($err) eq "PVE::Exception" && $err->{errors
}) {
1341 my $eh = $err->{errors
};
1342 foreach my $p (keys %$eh) {
1343 &$add_error($p, $eh->{$p});
1346 &$add_error('macro', "$err");
1351 $rule->{errors
} = $errors if $error_count;
1352 $rule->{ipversion
} = $ipversion if $ipversion;
1357 sub copy_rule_data
{
1358 my ($rule, $param) = @_;
1360 foreach my $k (keys %$rule_properties) {
1361 if (defined(my $v = $param->{$k})) {
1362 if ($v eq '' || $v eq '-') {
1373 sub rules_modify_permissions
{
1374 my ($rule_env) = @_;
1376 if ($rule_env eq 'host') {
1378 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
1380 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1382 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1384 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1386 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
1393 sub rules_audit_permissions
{
1394 my ($rule_env) = @_;
1396 if ($rule_env eq 'host') {
1398 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
1400 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1402 check
=> ['perm', '/', [ 'Sys.Audit' ]],
1404 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1406 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1414 my $bridge_firewall_enabled = 0;
1416 sub enable_bridge_firewall
{
1418 return if $bridge_firewall_enabled; # only once
1420 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1421 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1423 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1424 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1426 $bridge_firewall_enabled = 1;
1429 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1431 sub iptables_restore_cmdlist
{
1434 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1437 sub ip6tables_restore_cmdlist
{
1440 run_command
("/sbin/ip6tables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1443 sub ipset_restore_cmdlist
{
1446 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist, errmsg
=> "ipset_restore_cmdlist");
1449 sub iptables_get_chains
{
1450 my ($iptablescmd) = @_;
1452 $iptablescmd = "iptables" if !$iptablescmd;
1456 # check what chains we want to track
1457 my $is_pvefw_chain = sub {
1460 return 1 if $name =~ m/^PVEFW-\S+$/;
1462 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
1464 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
1466 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
1468 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/;
1469 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
1481 return if $line =~ m/^#/;
1482 return if $line =~ m/^\s*$/;
1484 if ($line =~ m/^\*(\S+)$/) {
1489 return if $table ne 'filter';
1491 if ($line =~ m/^:(\S+)\s/) {
1493 return if !&$is_pvefw_chain($chain);
1494 $res->{$chain} = "unknown";
1495 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1496 my ($chain, $sig) = ($1, $2);
1497 return if !&$is_pvefw_chain($chain);
1498 $res->{$chain} = $sig;
1499 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1502 # simply ignore the rest
1507 run_command
("/sbin/$iptablescmd-save", outfunc
=> $parser);
1509 return wantarray ?
($res, $hooks) : $res;
1512 sub iptables_chain_digest
{
1514 my $digest = Digest
::SHA-
>new('sha1');
1515 foreach my $rule (@$rules) { # order is important
1516 $digest->add($rule);
1518 return $digest->b64digest;
1521 sub ipset_chain_digest
{
1524 my $digest = Digest
::SHA-
>new('sha1');
1525 foreach my $rule (sort @$rules) { # note: sorted
1526 $digest->add($rule);
1528 return $digest->b64digest;
1531 sub ipset_get_chains
{
1539 return if $line =~ m/^#/;
1540 return if $line =~ m/^\s*$/;
1541 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1543 $line =~ s/\s+$//; # delete trailing white space
1544 push @{$chains->{$chain}}, $line;
1546 # simply ignore the rest
1551 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
1553 # compute digest for each chain
1554 foreach my $chain (keys %$chains) {
1555 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1561 sub ruleset_generate_cmdstr
{
1562 my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1564 return if defined($rule->{enable
}) && !$rule->{enable
};
1565 return if $rule->{errors
};
1567 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1569 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1570 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1574 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1575 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1577 my $source = $rule->{source
};
1578 my $dest = $rule->{dest
};
1581 if ($source =~ m/^\+/) {
1582 if ($source =~ m/^\+(${ipset_name_pattern})$/) {
1584 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1585 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name);
1586 push @cmd, "-m set --match-set ${ipset_chain} src";
1587 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1588 my $ipset_chain = compute_ipset_chain_name
(0, $name);
1589 push @cmd, "-m set --match-set ${ipset_chain} src";
1591 die "no such ipset '$name'\n";
1594 die "invalid security group name '$source'\n";
1596 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1597 my $alias = lc($source);
1598 my $e = $fw_conf->{aliases
}->{$alias} if $fw_conf;
1599 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1600 die "no such alias '$source'\n" if !$e;
1601 push @cmd, "-s $e->{cidr}";
1602 } elsif ($source =~ m/\-/){
1603 push @cmd, "-m iprange --src-range $source";
1605 push @cmd, "-s $source";
1610 if ($dest =~ m/^\+/) {
1611 if ($dest =~ m/^\+(${ipset_name_pattern})$/) {
1613 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1614 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name);
1615 push @cmd, "-m set --match-set ${ipset_chain} dst";
1616 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1617 my $ipset_chain = compute_ipset_chain_name
(0, $name);
1618 push @cmd, "-m set --match-set ${ipset_chain} dst";
1620 die "no such ipset '$name'\n";
1623 die "invalid security group name '$dest'\n";
1625 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1626 my $alias = lc($dest);
1627 my $e = $fw_conf->{aliases
}->{$alias} if $fw_conf;
1628 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1629 die "no such alias '$dest'\n" if !$e;
1630 push @cmd, "-d $e->{cidr}";
1631 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1632 push @cmd, "-m iprange --dst-range $dest";
1634 push @cmd, "-d $dest";
1638 if ($rule->{proto
}) {
1639 push @cmd, "-p $rule->{proto}";
1642 $multiport++ if $nbdport > 1;
1643 $multiport++ if $nbsport > 1;
1645 push @cmd, "--match multiport" if $multiport;
1647 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1648 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1650 if ($rule->{dport
}) {
1651 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1652 # Note: we use dport to store --icmp-type
1653 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
1654 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1655 } elsif ($rule->{proto
} && $rule->{proto
} eq 'icmpv6') {
1656 # Note: we use dport to store --icmpv6-type
1657 die "unknown icmpv6-type '$rule->{dport}'\n" if !defined($icmpv6_type_names->{$rule->{dport
}});
1658 push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}";
1661 if ($multiport == 2) {
1662 push @cmd, "--ports $rule->{dport}";
1664 push @cmd, "--dports $rule->{dport}";
1667 push @cmd, "--dport $rule->{dport}";
1672 if ($rule->{sport
}) {
1674 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1676 push @cmd, "--sport $rule->{sport}";
1679 } elsif ($rule->{dport
} || $rule->{sport
}) {
1680 die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport
};
1681 die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport
};
1684 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1686 if (my $action = $rule->{action
}) {
1687 $action = $actions->{$action} if defined($actions->{$action});
1688 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1689 push @cmd, $goto ?
"-g $action" : "-j $action";
1692 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1695 sub ruleset_generate_rule
{
1696 my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1700 if ($rule->{macro}) {
1701 $rules = &$apply_macro($rule->{macro}, $rule);
1706 # update all or nothing
1709 foreach my $tmp (@$rules) {
1710 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) {
1711 push @cmds, $cmdstr;
1715 foreach my $cmdstr (@cmds) {
1716 ruleset_addrule
($ruleset, $chain, $cmdstr);
1720 sub ruleset_generate_rule_insert
{
1721 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1723 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1725 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
1726 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1730 sub ruleset_create_chain
{
1731 my ($ruleset, $chain) = @_;
1733 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1734 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1736 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1738 $ruleset->{$chain} = [];
1741 sub ruleset_chain_exist
{
1742 my ($ruleset, $chain) = @_;
1744 return $ruleset->{$chain} ?
1 : undef;
1747 sub ruleset_addrule
{
1748 my ($ruleset, $chain, $rule) = @_;
1750 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1752 push @{$ruleset->{$chain}}, "-A $chain $rule";
1755 sub ruleset_insertrule
{
1756 my ($ruleset, $chain, $rule) = @_;
1758 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1760 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1763 sub get_log_rule_base
{
1764 my ($chain, $vmid, $msg, $loglevel) = @_;
1766 die "internal error - no log level" if !defined($loglevel);
1768 $vmid = 0 if !defined($vmid);
1770 # Note: we use special format for prefix to pass further
1771 # info to log daemon (VMID, LOGVELEL and CHAIN)
1773 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1776 sub ruleset_addlog {
1777 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1779 return if !defined($loglevel);
1781 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1783 $logrule = "$rule $logrule" if defined($rule);
1785 ruleset_addrule($ruleset, $chain, $logrule);
1788 sub ruleset_add_chain_policy {
1789 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
1791 if ($policy eq 'ACCEPT') {
1793 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
1794 { ACCEPT => $accept_action});
1796 } elsif ($policy eq 'DROP') {
1798 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1800 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1802 ruleset_addrule($ruleset, $chain, "-j DROP
");
1803 } elsif ($policy eq 'REJECT') {
1804 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1806 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1808 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1811 die "internal error
: unknown policy
'$policy'";
1815 sub ruleset_chain_add_conn_filters {
1816 my ($ruleset, $chain, $accept) = @_;
1818 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1819 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1822 sub ruleset_chain_add_input_filters {
1823 my ($ruleset, $chain, $options, $cluster_conf, $ipversion, $loglevel) = @_;
1825 if ($cluster_conf->{ipset}->{blacklist}){
1826 if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist
")) {
1827 ruleset_create_chain($ruleset, "PVEFW-blacklist
");
1828 ruleset_addlog($ruleset, "PVEFW-blacklist
", 0, "DROP
: ", $loglevel) if $loglevel;
1829 ruleset_addrule($ruleset, "PVEFW-blacklist
", "-j DROP
");
1831 my $ipset_chain = compute_ipset_chain_name(0, 'blacklist');
1832 ruleset_addrule($ruleset, $chain, "-m set
--match-set
${ipset_chain
} src
-j PVEFW-blacklist
");
1835 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1836 if ($ipversion == 4) {
1837 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1841 if ($options->{tcpflags}) {
1842 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1846 sub ruleset_create_vm_chain {
1847 my ($ruleset, $chain, $options, $macaddr, $ipfilter_ipset, $direction) = @_;
1849 ruleset_create_chain($ruleset, $chain);
1850 my $accept = generate_nfqueue($options);
1852 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1853 if ($direction eq 'OUT') {
1854 ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK',
1855 proto => 'udp', sport => 68, dport => 67 });
1857 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT',
1858 proto => 'udp', sport => 67, dport => 68 });
1862 if ($direction eq 'OUT') {
1863 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1864 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1866 if ($ipfilter_ipset) {
1867 ruleset_addrule($ruleset, $chain, "-m set
! --match-set
$ipfilter_ipset src
-j DROP
");
1869 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1873 sub ruleset_add_group_rule {
1874 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_;
1876 my $group = $rule->{action};
1877 my $group_chain = "GROUP-
$group-$direction";
1878 if(!ruleset_chain_exist($ruleset, $group_chain)){
1879 generate_group_rules($ruleset, $cluster_conf, $group, $ipversion);
1882 if ($direction eq 'OUT' && $rule->{iface_out}) {
1883 ruleset_addrule($ruleset, $chain, "-o
$rule->{iface_out
} -j
$group_chain");
1884 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1885 ruleset_addrule($ruleset, $chain, "-i
$rule->{iface_in
} -j
$group_chain");
1887 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1890 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$action");
1893 sub ruleset_generate_vm_rules {
1894 my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_;
1896 my $lc_direction = lc($direction);
1898 my $in_accept = generate_nfqueue($options);
1900 foreach my $rule (@$rules) {
1901 next if $rule->{iface} && $rule->{iface} ne $netid;
1902 next if !$rule->{enable} || $rule->{errors};
1903 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
1905 if ($rule->{type} eq 'group') {
1906 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
1907 $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion);
1909 next if $rule->{type} ne $lc_direction;
1911 if ($direction eq 'OUT') {
1912 ruleset_generate_rule($ruleset, $chain, $rule,
1913 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
1914 undef, $cluster_conf, $vmfw_conf);
1916 ruleset_generate_rule($ruleset, $chain, $rule,
1917 { ACCEPT => $in_accept , REJECT => "PVEFW-reject
" },
1918 undef, $cluster_conf, $vmfw_conf);
1926 sub generate_nfqueue {
1929 if ($options->{ips}) {
1930 my $action = "NFQUEUE
";
1931 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1932 if (defined($3) && defined($1)) {
1933 $action .= " --queue-balance
$1:$3";
1934 } elsif (defined($1)) {
1935 $action .= " --queue-num
$1";
1938 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
1945 sub ruleset_generate_vm_ipsrules {
1946 my ($ruleset, $options, $direction, $iface) = @_;
1948 if ($options->{ips} && $direction eq 'IN') {
1949 my $nfqueue = generate_nfqueue($options);
1951 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS
")) {
1952 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1955 ruleset_addrule($ruleset, "PVEFW-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1959 sub generate_venet_rules_direction {
1960 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction, $ipversion) = @_;
1962 my $lc_direction = lc($direction);
1964 my $rules = $vmfw_conf->{rules};
1966 my $options = $vmfw_conf->{options};
1967 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1969 my $chain = "venet0-
$vmid-$direction";
1971 ruleset_create_vm_chain($ruleset, $chain, $options, undef, undef, $direction);
1973 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction, undef, $ipversion);
1978 if ($direction eq 'OUT') {
1979 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1981 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1984 my $accept = generate_nfqueue($options);
1985 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1986 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1988 if ($direction eq 'OUT') {
1989 ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT
", {
1992 iface_in => 'venet0'});
1994 ruleset_generate_rule($ruleset, "PVEFW-VENET-IN
", {
1997 iface_out => 'venet0'});
2001 sub generate_tap_rules_direction {
2002 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_;
2004 my $lc_direction = lc($direction);
2006 my $rules = $vmfw_conf->{rules};
2008 my $options = $vmfw_conf->{options};
2009 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
2011 my $tapchain = "$iface-$direction";
2013 my $ipfilter_name = compute_ipfilter_ipset_name($netid);
2014 my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name)
2015 if $vmfw_conf->{ipset}->{$ipfilter_name};
2017 # create chain with mac and ip filter
2018 ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $ipfilter_ipset, $direction);
2020 if ($options->{enable}) {
2021 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion);
2023 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
2028 if ($direction eq 'OUT') {
2029 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2031 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2034 my $accept = generate_nfqueue($options);
2035 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2036 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
2038 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : 'ACCEPT';
2039 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, 'ACCEPT', $loglevel, $accept_action);
2042 # plug the tap chain to bridge chain
2043 if ($direction eq 'IN') {
2044 ruleset_addrule($ruleset, "PVEFW-FWBR-IN
",
2045 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
2047 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT
",
2048 "-m physdev
--physdev-is-bridged
--physdev-in
$iface -j
$tapchain");
2052 sub enable_host_firewall {
2053 my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
2055 my $options = $hostfw_conf->{options};
2056 my $cluster_options = $cluster_conf->{options};
2057 my $rules = $hostfw_conf->{rules};
2058 my $cluster_rules = $cluster_conf->{rules};
2060 # host inbound firewall
2061 my $chain = "PVEFW-HOST-IN
";
2062 ruleset_create_chain($ruleset, $chain);
2064 my $loglevel = get_option_log_level($options, "log_level_in
");
2066 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
2068 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2069 ruleset_chain_add_input_filters($ruleset, $chain, $options, $cluster_conf, $ipversion, $loglevel);
2071 # we use RETURN because we need to check also tap rules
2072 my $accept_action = 'RETURN';
2074 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2076 # add host rules first, so that cluster wide rules can be overwritten
2077 foreach my $rule (@$rules, @$cluster_rules) {
2078 next if !$rule->{enable} || $rule->{errors};
2080 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
2083 if ($rule->{type} eq 'group') {
2084 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
2085 } elsif ($rule->{type} eq 'in') {
2086 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2087 undef, $cluster_conf, $hostfw_conf);
2091 delete $rule->{iface_in};
2094 # allow standard traffic for management ipset (includes cluster network)
2095 my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management
");
2096 my $mngmntsrc = "-m set
--match-set
${mngmnt_ipset_chain
} src
";
2097 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2098 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2099 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2100 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
22 -j
$accept_action"); # SSH
2102 my $localnet = local_network();
2106 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2107 ruleset_addrule($ruleset, $chain, "-s
$localnet -d
$localnet $corosync_rule");
2108 ruleset_addrule($ruleset, $chain, "-s
$localnet -m addrtype
--dst-type MULTICAST
$corosync_rule");
2111 # implement input policy
2112 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
2113 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
2115 # host outbound firewall
2116 $chain = "PVEFW-HOST-OUT
";
2117 ruleset_create_chain($ruleset, $chain);
2119 $loglevel = get_option_log_level($options, "log_level_out
");
2121 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
2123 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2125 # we use RETURN because we may want to check other thigs later
2126 $accept_action = 'RETURN';
2128 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2130 # add host rules first, so that cluster wide rules can be overwritten
2131 foreach my $rule (@$rules, @$cluster_rules) {
2132 next if !$rule->{enable} || $rule->{errors};
2134 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
2136 if ($rule->{type} eq 'group') {
2137 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
2138 } elsif ($rule->{type} eq 'out') {
2139 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2140 undef, $cluster_conf, $hostfw_conf);
2144 delete $rule->{iface_out};
2147 # allow standard traffic on cluster network
2149 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2150 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
22 -j
$accept_action"); # SSH
2151 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2152 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2154 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2155 ruleset_addrule($ruleset, $chain, "-d
$localnet $corosync_rule");
2156 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
$corosync_rule");
2159 # implement output policy
2160 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
2161 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
2163 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
2164 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
2167 sub generate_group_rules {
2168 my ($ruleset, $cluster_conf, $group, $ipversion) = @_;
2170 my $rules = $cluster_conf->{groups}->{$group};
2173 warn "no such security group
'$group'\n";
2174 $rules = []; # create empty chain
2177 my $chain = "GROUP-
${group
}-IN
";
2179 ruleset_create_chain($ruleset, $chain);
2180 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2182 foreach my $rule (@$rules) {
2183 next if $rule->{type} ne 'in';
2184 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2185 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
2188 $chain = "GROUP-
${group
}-OUT
";
2190 ruleset_create_chain($ruleset, $chain);
2191 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2193 foreach my $rule (@$rules) {
2194 next if $rule->{type} ne 'out';
2195 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2196 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
2197 # check also other tap rules later
2198 ruleset_generate_rule($ruleset, $chain, $rule,
2199 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
2204 my $valid_netdev_names = {};
2205 for (my $i = 0; $i < $MAX_NETS; $i++) {
2206 $valid_netdev_names->{"net
$i"} = 1;
2210 my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_;
2212 my $orig_line = $line;
2216 # we can add single line comments to the end of the rule
2217 if ($line =~ s/#\s*(.*?)\s*$//) {
2218 $rule->{comment} = decode('utf8', $1);
2221 # we can disable a rule when prefixed with '|'
2223 $rule->{enable} = $line =~ s/^\|// ? 0 : 1;
2225 $line =~ s/^(\S+)\s+(\S+)\s*// ||
2226 die "unable to parse rule
: $line\n";
2228 $rule->{type} = lc($1);
2229 $rule->{action} = $2;
2231 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
2232 if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
2233 $rule->{macro} = $1;
2234 $rule->{action} = $2;
2238 while (length($line)) {
2239 if ($line =~ s/^-i (\S+)\s*//) {
2240 $rule->{iface} = $1;
2244 last if $rule->{type} eq 'group';
2246 if ($line =~ s/^-p (\S+)\s*//) {
2247 $rule->{proto} = $1;
2251 if ($line =~ s/^-dport (\S+)\s*//) {
2252 $rule->{dport} = $1;
2256 if ($line =~ s/^-sport (\S+)\s*//) {
2257 $rule->{sport} = $1;
2260 if ($line =~ s/^-source (\S+)\s*//) {
2261 $rule->{source} = $1;
2264 if ($line =~ s/^-dest (\S+)\s*//) {
2272 die "unable to parse rule parameters
: $line\n" if length($line);
2274 $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1);
2275 if ($verbose && $rule->{errors}) {
2276 warn "$prefix - errors
in rule parameters
: $orig_line\n";
2277 foreach my $p (keys %{$rule->{errors}}) {
2278 warn " $p: $rule->{errors
}->{$p}\n";
2285 sub parse_vmfw_option {
2290 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
2292 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
2295 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
2297 $value = $2 ? lc($3) : '';
2298 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2301 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
2305 die "can
't parse option '$line'\n"
2308 return ($opt, $value);
2311 sub parse_hostfw_option {
2316 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2318 if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
2321 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
2323 $value = $2 ? lc($3) : '';
2324 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
2328 die "can't parse option
'$line'\n"
2331 return ($opt, $value);
2334 sub parse_clusterfw_option {
2339 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
2342 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2346 die "can
't parse option '$line'\n"
2349 return ($opt, $value);
2353 my ($clusterfw_conf, $fw_conf, $cidr) = @_;
2355 my $alias = lc($cidr);
2356 my $e = $fw_conf->{aliases}->{$alias} if $fw_conf;
2357 $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
2359 die "no such alias '$cidr'\n" if !$e;;
2361 return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr};
2364 sub parse_ip_or_cidr {
2369 if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
2372 } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
2376 die "value does not look like a valid IP address or CIDR network\n";
2379 return wantarray ? ($cidr, $ipversion) : $cidr;
2385 # we can add single line comments to the end of the line
2386 my $comment = decode('utf8
', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
2388 if ($line =~ m/^(\S+)\s(\S+)$/) {
2389 my ($name, $cidr) = ($1, $2);
2392 ($cidr, $ipversion) = parse_ip_or_cidr($cidr);
2397 ipversion => $ipversion,
2399 $data->{comment} = $comment if $comment;
2406 sub generic_fw_config_parser {
2407 my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_;
2412 my $res = $empty_conf;
2414 while (defined(my $line = <$fh>)) {
2415 next if $line =~ m/^#/;
2416 next if $line =~ m/^\s*$/;
2420 my $linenr = $fh->input_line_number();
2421 my $prefix = "$filename (line $linenr)";
2423 if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
2424 $section = 'options
';
2428 if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) {
2429 $section = 'aliases
';
2433 if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2434 $section = 'groups
';
2438 die "security group name too long\n" if length($group) > $max_group_name_length;
2439 die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/;
2442 ($section, $group, $comment) = undef;
2443 warn "$prefix: $err";
2447 $res->{$section}->{$group} = [];
2448 $res->{group_comments}->{$group} = decode('utf8
', $comment)
2453 if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) {
2458 if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2463 die "ipset name too long\n" if length($group) > $max_ipset_name_length;
2464 die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
2467 ($section, $group, $comment) = undef;
2468 warn "$prefix: $err";
2472 $res->{$section}->{$group} = [];
2473 $res->{ipset_comments}->{$group} = decode('utf8
', $comment)
2479 warn "$prefix: skip line - no section\n";
2483 if ($section eq 'options
') {
2486 if ($rule_env eq 'cluster
') {
2487 ($opt, $value) = parse_clusterfw_option($line);
2488 } elsif ($rule_env eq 'host
') {
2489 ($opt, $value) = parse_hostfw_option($line);
2491 ($opt, $value) = parse_vmfw_option($line);
2493 $res->{options}->{$opt} = $value;
2495 warn "$prefix: $@" if $@;
2496 } elsif ($section eq 'aliases
') {
2498 my $data = parse_alias($line);
2499 $res->{aliases}->{lc($data->{name})} = $data;
2501 warn "$prefix: $@" if $@;
2502 } elsif ($section eq 'rules
') {
2504 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); };
2506 warn "$prefix: $err";
2509 push @{$res->{$section}}, $rule;
2510 } elsif ($section eq 'groups
') {
2512 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group
', $verbose); };
2514 warn "$prefix: $err";
2517 push @{$res->{$section}->{$group}}, $rule;
2518 } elsif ($section eq 'ipset
') {
2519 # we can add single line comments to the end of the rule
2520 my $comment = decode('utf8
', $1) if $line =~ s/#\s*(.*?)\s*$//;
2522 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2527 if ($nomatch && !$feature_ipset_nomatch) {
2528 $errors->{nomatch} = "nomatch not supported by kernel";
2532 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2533 resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
2535 $cidr = parse_ip_or_cidr($cidr);
2540 $errors->{cidr} = $err;
2543 my $entry = { cidr => $cidr };
2544 $entry->{nomatch} = 1 if $nomatch;
2545 $entry->{comment} = $comment if $comment;
2546 $entry->{errors} = $errors if $errors;
2548 if ($verbose && $errors) {
2549 warn "$prefix - errors in ipset '$group': $line\n";
2550 foreach my $p (keys %{$errors}) {
2551 warn " $p: $errors->{$p}\n";
2555 push @{$res->{$section}->{$group}}, $entry;
2557 warn "$prefix: skip line - unknown section\n";
2565 sub parse_hostfw_config {
2566 my ($filename, $fh, $cluster_conf, $verbose) = @_;
2568 my $empty_conf = { rules => [], options => {}};
2570 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host
');
2573 sub parse_vmfw_config {
2574 my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_;
2581 ipset_comments => {},
2584 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env);
2587 sub parse_clusterfw_config {
2588 my ($filename, $fh, $verbose) = @_;
2598 group_comments => {},
2600 ipset_comments => {},
2603 return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster
');
2607 my ($code, @param) = @_;
2611 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2618 sub read_local_vm_config {
2623 my $vmdata = { openvz => $openvz, qemu => $qemu };
2625 my $vmlist = PVE::Cluster::get_vmlist();
2626 return $vmdata if !$vmlist || !$vmlist->{ids};
2627 my $ids = $vmlist->{ids};
2629 foreach my $vmid (keys %$ids) {
2630 next if !$vmid; # skip VE0
2631 my $d = $ids->{$vmid};
2632 next if !$d->{node} || $d->{node} ne $nodename;
2633 next if !$d->{type};
2634 if ($d->{type} eq 'openvz
') {
2635 if ($have_pve_manager) {
2636 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
2637 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2638 $openvz->{$vmid} = $conf;
2641 } elsif ($d->{type} eq 'qemu
') {
2642 if ($have_qemu_server) {
2643 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2644 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2645 $qemu->{$vmid} = $conf;
2654 sub load_vmfw_conf {
2655 my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_;
2659 $dir = $pvefw_conf_dir if !defined($dir);
2661 my $filename = "$dir/$vmid.fw";
2662 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2663 $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose);
2664 $vmfw_conf->{vmid} = $vmid;
2670 my $format_rules = sub {
2671 my ($rules, $allow_iface) = @_;
2675 foreach my $rule (@$rules) {
2676 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
' || $rule->{type} eq 'group
') {
2677 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2678 $raw .= uc($rule->{type});
2679 if ($rule->{macro}) {
2680 $raw .= " $rule->{macro}($rule->{action})";
2682 $raw .= " " . $rule->{action};
2684 if ($allow_iface && $rule->{iface}) {
2685 $raw .= " -i $rule->{iface}";
2688 if ($rule->{type} ne 'group
') {
2689 $raw .= " -source $rule->{source}" if $rule->{source};
2690 $raw .= " -dest $rule->{dest}" if $rule->{dest};
2691 $raw .= " -p $rule->{proto}" if $rule->{proto};
2692 $raw .= " -dport $rule->{dport}" if $rule->{dport};
2693 $raw .= " -sport $rule->{sport}" if $rule->{sport};
2696 $raw .= " # " . encode('utf8
', $rule->{comment})
2697 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2700 die "unknown rule type '$rule->{type
}'";
2707 my $format_options = sub {
2712 $raw .= "[OPTIONS]\n\n";
2713 foreach my $opt (keys %$options) {
2714 $raw .= "$opt: $options->{$opt}\n";
2721 my $format_aliases = sub {
2726 $raw .= "[ALIASES]\n\n";
2727 foreach my $k (keys %$aliases) {
2728 my $e = $aliases->{$k};
2729 $raw .= "$e->{name} $e->{cidr}";
2730 $raw .= " # " . encode('utf8
', $e->{comment})
2731 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
2739 my $format_ipsets = sub {
2744 foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
2745 if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) {
2746 my $utf8comment = encode('utf8
', $comment);
2747 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2749 $raw .= "[IPSET $ipset]\n\n";
2751 my $options = $fw_conf->{ipset}->{$ipset};
2754 foreach my $entry (@$options) {
2755 $nethash->{$entry->{cidr}} = $entry;
2758 foreach my $cidr (sort keys %$nethash) {
2759 my $entry = $nethash->{$cidr};
2760 my $line = $entry->{nomatch} ? '!' : '';
2761 $line .= $entry->{cidr};
2762 $line .= " # " . encode('utf8
', $entry->{comment})
2763 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
2773 sub save_vmfw_conf {
2774 my ($vmid, $vmfw_conf) = @_;
2778 my $options = $vmfw_conf->{options};
2779 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2781 my $aliases = $vmfw_conf->{aliases};
2782 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2784 $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset};
2786 my $rules = $vmfw_conf->{rules} || [];
2787 if ($rules && scalar(@$rules)) {
2788 $raw .= "[RULES]\n\n";
2789 $raw .= &$format_rules($rules, 1);
2793 mkdir $pvefw_conf_dir;
2795 my $filename = "$pvefw_conf_dir/$vmid.fw";
2796 PVE::Tools::file_set_contents($filename, $raw);
2799 sub read_vm_firewall_configs {
2800 my ($cluster_conf, $vmdata, $dir, $verbose) = @_;
2802 my $vmfw_configs = {};
2804 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2805 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm
', $vmid, $dir, $verbose);
2806 next if !$vmfw_conf->{options}; # skip if file does not exists
2807 $vmfw_configs->{$vmid} = $vmfw_conf;
2809 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2810 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct
', $vmid, $dir, $verbose);
2811 next if !$vmfw_conf->{options}; # skip if file does not exists
2812 $vmfw_configs->{$vmid} = $vmfw_conf;
2815 return $vmfw_configs;
2818 sub get_option_log_level {
2819 my ($options, $k) = @_;
2821 my $v = $options->{$k};
2822 $v = $default_log_level if !defined($v);
2824 return undef if $v eq '' || $v eq 'nolog
';
2826 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2828 return $v if ($v >= 0) && ($v <= 7);
2830 warn "unknown log level ($k = '$v')\n";
2835 sub generate_std_chains {
2836 my ($ruleset, $options, $ipversion) = @_;
2838 my $std_chains = $pve_std_chains->{$ipversion} || die "internal error";
2840 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2844 if ($ipversion == 4) {
2845 # same as shorewall smurflog.
2846 $chain = 'PVEFW-smurflog
';
2847 $std_chains->{$chain} = [];
2849 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2850 push @{$std_chains->{$chain}}, "-j DROP";
2853 # same as shorewall logflags action.
2854 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2855 $chain = 'PVEFW-logflags
';
2856 $std_chains->{$chain} = [];
2858 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2859 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2860 push @{$std_chains->{$chain}}, "-j DROP";
2862 foreach my $chain (keys %$std_chains) {
2863 ruleset_create_chain($ruleset, $chain);
2864 foreach my $rule (@{$std_chains->{$chain}}) {
2866 ruleset_generate_rule($ruleset, $chain, $rule);
2868 ruleset_addrule($ruleset, $chain, $rule);
2874 sub generate_ipset_chains {
2875 my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_;
2877 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2878 my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $ipset);
2879 generate_ipset($ipset_ruleset, $ipset_chain, $fw_conf->{ipset}->{$ipset}, $clusterfw_conf, $fw_conf);
2883 sub generate_ipset {
2884 my ($ipset_ruleset, $name, $options, $clusterfw_conf, $fw_conf) = @_;
2886 die "duplicate ipset chain '$name'\n" if defined($ipset_ruleset->{$name});
2888 $ipset_ruleset->{$name} = ["create $name list:set size 4"];
2892 foreach my $entry (@$options) {
2893 next if $entry->{errors}; # skip entries with errors
2895 my ($cidr, $ipversion);
2896 if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
2897 ($cidr, $ipversion) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
2899 ($cidr, $ipversion) = parse_ip_or_cidr($entry->{cidr});
2901 #http://backreference.org/2013/03/01/ipv6-address-normalization/
2902 if ($ipversion == 6) {
2903 my $ipv6 = inet_pton(AF_INET6, lc($cidr));
2904 $cidr = inet_ntop(AF_INET6, $ipv6);
2910 $nethash->{$ipversion}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} };
2915 foreach my $ipversion (sort keys %$nethash) {
2916 my $data = $nethash->{$ipversion};
2917 my $subname = "$name-v$ipversion";
2919 my $hashsize = scalar(@$options);
2920 if ($hashsize <= 64) {
2923 $hashsize = round_powerof2($hashsize);
2926 my $family = $ipversion == "6" ? "inet6" : "inet";
2928 $ipset_ruleset->{$subname} = ["create $subname hash:net family $family hashsize $hashsize maxelem $hashsize"];
2930 foreach my $cidr (sort keys %$data) {
2931 my $entry = $data->{$cidr};
2933 my $cmd = "add $subname $cidr";
2934 if ($entry->{nomatch}) {
2935 if ($feature_ipset_nomatch) {
2936 push @{$ipset_ruleset->{$subname}}, "$cmd nomatch";
2938 warn "ignore !$cidr - nomatch not supported by kernel\n";
2941 push @{$ipset_ruleset->{$subname}}, $cmd;
2945 push @{$ipset_ruleset->{$name}}, "add $name $subname";
2949 sub round_powerof2 {
2953 $int |= $int >> $_ foreach (1,2,4,8,16);
2957 sub load_clusterfw_conf {
2958 my ($filename, $verbose) = @_;
2960 $filename = $clusterfw_conf_filename if !defined($filename);
2962 my $cluster_conf = {};
2963 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2964 $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose);
2967 return $cluster_conf;
2970 sub save_clusterfw_conf {
2971 my ($cluster_conf) = @_;
2975 my $options = $cluster_conf->{options};
2976 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2978 my $aliases = $cluster_conf->{aliases};
2979 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2981 $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
2983 my $rules = $cluster_conf->{rules};
2984 if ($rules && scalar(@$rules)) {
2985 $raw .= "[RULES]\n\n";
2986 $raw .= &$format_rules($rules, 1);
2990 if ($cluster_conf->{groups}) {
2991 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
2992 my $rules = $cluster_conf->{groups}->{$group};
2993 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
2994 my $utf8comment = encode('utf8
', $comment);
2995 $raw .= "[group $group] # $utf8comment\n\n";
2997 $raw .= "[group $group]\n\n";
3000 $raw .= &$format_rules($rules, 0);
3005 mkdir $pvefw_conf_dir;
3006 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
3009 sub load_hostfw_conf {
3010 my ($cluster_conf, $filename, $verbose) = @_;
3012 $filename = $hostfw_conf_filename if !defined($filename);
3014 my $hostfw_conf = {};
3015 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3016 $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose);
3018 return $hostfw_conf;
3021 sub save_hostfw_conf {
3022 my ($hostfw_conf) = @_;
3026 my $options = $hostfw_conf->{options};
3027 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3029 my $rules = $hostfw_conf->{rules};
3030 if ($rules && scalar(@$rules)) {
3031 $raw .= "[RULES]\n\n";
3032 $raw .= &$format_rules($rules, 1);
3036 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
3040 my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_;
3044 if ($vmdata) { # test mode
3045 my $testdir = $vmdata->{testdir} || die "no test directory specified";
3046 my $filename = "$testdir/cluster.fw";
3047 $cluster_conf = load_clusterfw_conf($filename, $verbose);
3049 $filename = "$testdir/host.fw";
3050 $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose);
3052 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose);
3053 } else { # normal operation
3054 $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf;
3056 $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf;
3058 $vmdata = read_local_vm_config();
3059 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose);
3062 my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose);
3063 my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose);
3065 return ($ruleset, $ipset_ruleset, $rulesetv6);
3068 sub compile_iptables_filter {
3069 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_;
3071 $cluster_conf->{ipset}->{venet0} = [];
3072 my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0
');
3075 if ($cluster_conf->{aliases}->{local_network}) {
3076 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3078 $localnet = local_network() || '127.0.0.0/8';
3079 $cluster_conf->{aliases}->{local_network} = { cidr => $localnet };
3082 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
3084 return ({}, {}) if !$cluster_conf->{options}->{enable};
3088 ruleset_create_chain($ruleset, "PVEFW-INPUT");
3089 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
3091 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3093 my $hostfw_options = $hostfw_conf->{options} || {};
3095 # fixme: what log level should we use here?
3096 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
3098 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT");
3101 ruleset_create_chain($ruleset, "PVEFW-VENET-OUT");
3102 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3103 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3105 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
3106 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $hostfw_options, $cluster_conf, $ipversion, $loglevel);
3108 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN");
3110 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
3111 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT");
3113 ruleset_create_chain($ruleset, "PVEFW-VENET-IN");
3114 ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $hostfw_options, $cluster_conf, $ipversion, $loglevel);
3116 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3118 generate_std_chains($ruleset, $hostfw_options, $ipversion);
3120 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
3122 my $ipset_ruleset = {};
3124 # currently pveproxy don't works with ipv6
, so let
's generate host fw ipv4 only for the moment
3125 if ($hostfw_enable && ($ipversion == 4)) {
3126 eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
3127 warn $@ if $@; # just to be sure - should not happen
3130 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3132 # generate firewall rules for QEMU VMs
3133 foreach my $vmid (keys %{$vmdata->{qemu}}) {
3135 my $conf = $vmdata->{qemu}->{$vmid};
3136 my $vmfw_conf = $vmfw_configs->{$vmid};
3137 return if !$vmfw_conf;
3139 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3141 foreach my $netid (keys %$conf) {
3142 next if $netid !~ m/^net(\d+)$/;
3143 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3144 next if !$net->{firewall};
3145 my $iface = "tap${vmid}i$1";
3147 my $macaddr = $net->{macaddr};
3148 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3149 $vmfw_conf, $vmid, 'IN
', $ipversion);
3150 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3151 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3154 warn $@ if $@; # just to be sure - should not happen
3157 # generate firewall rules for OpenVZ containers
3158 foreach my $vmid (keys %{$vmdata->{openvz}}) {
3160 my $conf = $vmdata->{openvz}->{$vmid};
3162 my $vmfw_conf = $vmfw_configs->{$vmid};
3163 return if !$vmfw_conf;
3165 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3167 if ($vmfw_conf->{options}->{enable}) {
3168 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
3169 my $ip = $conf->{ip_address}->{value};
3174 foreach my $singleip (split(',', $ip)) {
3175 my $singleip_ver = parse_address_list($singleip); # make sure we have a valid $ip list
3176 push @{$cluster_conf->{ipset}->{venet0}}, { cidr => $singleip };
3177 push @ips, $singleip if $singleip_ver == $ipversion;
3181 my $ip_list = join(',', @ips);
3182 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'IN
', $ipversion);
3183 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'OUT
', $ipversion);
3188 if ($conf->{netif} && $conf->{netif}->{value}) {
3189 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
3190 foreach my $netid (keys %$netif) {
3191 my $d = $netif->{$netid};
3192 my $bridge = $d->{bridge};
3193 next if !$bridge || $bridge !~ m/^vmbr\d+(v(\d+))?f$/; # firewall enabled ?
3194 my $macaddr = $d->{mac};
3195 my $iface = $d->{host_ifname};
3196 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3197 $vmfw_conf, $vmid, 'IN
', $ipversion);
3198 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3199 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3203 warn $@ if $@; # just to be sure - should not happen
3206 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
3207 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS");
3210 generate_ipset_chains($ipset_ruleset, undef, $cluster_conf);
3212 return ($ruleset, $ipset_ruleset);
3215 sub get_ruleset_status {
3216 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
3218 my $statushash = {};
3220 foreach my $chain (sort keys %$ruleset) {
3221 my $sig = &$digest_fn($ruleset->{$chain});
3223 $statushash->{$chain}->{sig} = $sig;
3225 my $oldsig = $active_chains->{$chain};
3226 if (!defined($oldsig)) {
3227 $statushash->{$chain}->{action} = 'create
';
3229 if ($oldsig eq $sig) {
3230 $statushash->{$chain}->{action} = 'exists';
3232 $statushash->{$chain}->{action} = 'update
';
3235 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
3236 foreach my $cmd (@{$ruleset->{$chain}}) {
3237 print "\t$cmd\n" if $verbose;
3241 foreach my $chain (sort keys %$active_chains) {
3242 if (!defined($ruleset->{$chain})) {
3243 my $sig = $active_chains->{$chain};
3244 $statushash->{$chain}->{action} = 'delete';
3245 $statushash->{$chain}->{sig} = $sig;
3246 print "delete $chain ($sig)\n" if $verbose;
3253 sub print_sig_rule {
3254 my ($chain, $sig) = @_;
3256 # We just use this to store a SHA1 checksum used to detect changes
3257 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
3260 sub get_ruleset_cmdlist {
3261 my ($ruleset, $verbose, $iptablescmd) = @_;
3263 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
3265 my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
3266 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
3268 # create missing chains first
3269 foreach my $chain (sort keys %$ruleset) {
3270 my $stat = $statushash->{$chain};
3271 die "internal error" if !$stat;
3272 next if $stat->{action} ne 'create
';
3274 $cmdlist .= ":$chain - [0:0]\n";
3277 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3278 my $chain = "PVEFW-$h";
3279 if ($ruleset->{$chain} && !$hooks->{$h}) {
3280 $cmdlist .= "-A $h -j $chain\n";
3284 foreach my $chain (sort keys %$ruleset) {
3285 my $stat = $statushash->{$chain};
3286 die "internal error" if !$stat;
3288 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
3289 $cmdlist .= "-F $chain\n";
3290 foreach my $cmd (@{$ruleset->{$chain}}) {
3291 $cmdlist .= "$cmd\n";
3293 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
3294 } elsif ($stat->{action
} eq 'delete') {
3295 die "internal error"; # this should not happen
3296 } elsif ($stat->{action
} eq 'exists') {
3299 die "internal error - unknown status '$stat->{action}'";
3303 foreach my $chain (keys %$statushash) {
3304 next if $statushash->{$chain}->{action
} ne 'delete';
3305 $cmdlist .= "-F $chain\n";
3307 foreach my $chain (keys %$statushash) {
3308 next if $statushash->{$chain}->{action
} ne 'delete';
3309 next if $chain eq 'PVEFW-INPUT';
3310 next if $chain eq 'PVEFW-OUTPUT';
3311 next if $chain eq 'PVEFW-FORWARD';
3312 $cmdlist .= "-X $chain\n";
3315 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
3317 $cmdlist .= "COMMIT\n";
3319 return wantarray ?
($cmdlist, $changes) : $cmdlist;
3322 sub get_ipset_cmdlist
{
3323 my ($ruleset, $verbose) = @_;
3327 my $delete_cmdlist = "";
3329 my $active_chains = ipset_get_chains
();
3330 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
3332 # remove stale _swap chains
3333 foreach my $chain (keys %$active_chains) {
3334 if ($chain =~ m/^PVEFW-\S+_swap$/) {
3335 $cmdlist .= "destroy $chain\n";
3339 # create -v4 and -v6 chains first
3340 foreach my $chain (keys %$ruleset) {
3341 next if $chain !~ m/-v[46]$/;
3342 my $stat = $statushash->{$chain};
3343 die "internal error" if !$stat;
3345 if ($stat->{action
} eq 'create') {
3346 foreach my $cmd (@{$ruleset->{$chain}}) {
3347 $cmdlist .= "$cmd\n";
3352 # then create list chains which use above -v4 and -v6 chains
3353 foreach my $chain (keys %$ruleset) {
3354 next if $chain =~ m/-v[46]$/;
3355 my $stat = $statushash->{$chain};
3356 die "internal error" if !$stat;
3358 if ($stat->{action
} eq 'create') {
3359 foreach my $cmd (@{$ruleset->{$chain}}) {
3360 $cmdlist .= "$cmd\n";
3365 foreach my $chain (keys %$ruleset) {
3366 my $stat = $statushash->{$chain};
3367 die "internal error" if !$stat;
3369 if ($stat->{action
} eq 'update') {
3370 my $chain_swap = $chain."_swap";
3372 foreach my $cmd (@{$ruleset->{$chain}}) {
3373 $cmd =~ s/$chain/$chain_swap/;
3374 $cmdlist .= "$cmd\n";
3376 $cmdlist .= "swap $chain_swap $chain\n";
3377 $cmdlist .= "flush $chain_swap\n";
3378 $cmdlist .= "destroy $chain_swap\n";
3382 # remove unused list chains first
3383 foreach my $chain (keys %$statushash) {
3384 next if $statushash->{$chain}->{action
} ne 'delete';
3385 next if $chain !~ m/-v[46]$/;
3387 $delete_cmdlist .= "flush $chain\n";
3388 $delete_cmdlist .= "destroy $chain\n";
3391 # the remove unused -v4 -v6 chains
3392 foreach my $chain (keys %$statushash) {
3393 next if $statushash->{$chain}->{action
} ne 'delete';
3394 next if $chain =~ m/-v[46]$/;
3396 $delete_cmdlist .= "flush $chain\n";
3397 $delete_cmdlist .= "destroy $chain\n";
3400 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
3402 return ($cmdlist, $delete_cmdlist, $changes);
3406 my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_;
3408 enable_bridge_firewall
();
3410 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
3411 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
3413 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
3414 my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist
($rulesetv6, $verbose, "ip6tables");
3417 if ($ipset_changes) {
3418 print "ipset changes:\n";
3419 print $ipset_create_cmdlist if $ipset_create_cmdlist;
3420 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
3424 print "iptables changes:\n";
3429 print "ip6tables changes:\n";
3434 my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1";
3435 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_create_cmdlist || '');
3437 ipset_restore_cmdlist
($ipset_create_cmdlist);
3439 $tmpfile = "$pve_fw_status_dir/ip4cmdlist";
3440 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlist || '');
3442 iptables_restore_cmdlist
($cmdlist);
3444 $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
3445 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlistv6 || '');
3447 ip6tables_restore_cmdlist
($cmdlistv6);
3449 $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
3450 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_delete_cmdlist || '');
3452 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
3454 # test: re-read status and check if everything is up to date
3455 my $active_chains = iptables_get_chains
();
3456 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
3459 foreach my $chain (sort keys %$ruleset) {
3460 my $stat = $statushash->{$chain};
3461 if ($stat->{action
} ne 'exists') {
3462 warn "unable to update chain '$chain'\n";
3467 my $active_chainsv6 = iptables_get_chains
("ip6tables");
3468 my $statushashv6 = get_ruleset_status
($rulesetv6, $active_chainsv6, \
&iptables_chain_digest
, 0);
3470 foreach my $chain (sort keys %$rulesetv6) {
3471 my $stat = $statushashv6->{$chain};
3472 if ($stat->{action
} ne 'exists') {
3473 warn "unable to update chain '$chain'\n";
3478 die "unable to apply firewall changes\n" if $errors;
3480 update_nf_conntrack_max
($hostfw_conf);
3482 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
3486 sub update_nf_conntrack_max
{
3487 my ($hostfw_conf) = @_;
3489 my $max = 65536; # reasonable default
3491 my $options = $hostfw_conf->{options
} || {};
3493 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
3494 $max = $options->{nf_conntrack_max
};
3495 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
3498 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
3499 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
3501 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
3503 if ($current != $max) {
3504 my $hashsize = int($max/4);
3505 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
3506 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
3510 sub update_nf_conntrack_tcp_timeout_established
{
3511 my ($hostfw_conf) = @_;
3513 my $options = $hostfw_conf->{options
} || {};
3515 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
3517 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
3520 sub remove_pvefw_chains
{
3522 PVE
::Firewall
::remove_pvefw_chains_iptables
("iptables");
3523 PVE
::Firewall
::remove_pvefw_chains_iptables
("ip6tables");
3524 PVE
::Firewall
::remove_pvefw_chains_ipset
();
3528 sub remove_pvefw_chains_iptables
{
3529 my ($iptablescmd) = @_;
3531 my ($chash, $hooks) = iptables_get_chains
($iptablescmd);
3532 my $cmdlist = "*filter\n";
3534 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3536 $cmdlist .= "-D $h -j PVEFW-$h\n";
3540 foreach my $chain (keys %$chash) {
3541 $cmdlist .= "-F $chain\n";
3544 foreach my $chain (keys %$chash) {
3545 $cmdlist .= "-X $chain\n";
3547 $cmdlist .= "COMMIT\n";
3549 if($iptablescmd eq "ip6tables") {
3550 ip6tables_restore_cmdlist
($cmdlist);
3552 iptables_restore_cmdlist
($cmdlist);
3556 sub remove_pvefw_chains_ipset
{
3558 my $ipset_chains = ipset_get_chains
();
3560 my $sub_cmdlist = "";
3563 foreach my $chain (keys %$ipset_chains) {
3564 if ($chain =~ m/^PVEFW-\S+\-(v4|v6)$/) {
3565 $sub_cmdlist .= "flush $chain\n";
3566 $sub_cmdlist .= "destroy $chain\n";
3568 $cmdlist .= "flush $chain\n";
3569 $cmdlist .= "destroy $chain\n";
3573 $cmdlist .= $sub_cmdlist;
3575 ipset_restore_cmdlist
($cmdlist) if $cmdlist;
3579 my $cluster_conf = load_clusterfw_conf
();
3580 my $cluster_options = $cluster_conf->{options
};
3581 my $enable = $cluster_options->{enable
};
3585 # load required modules here
3591 my $cluster_conf = load_clusterfw_conf
();
3592 my $cluster_options = $cluster_conf->{options
};
3594 if (!$cluster_options->{enable
}) {
3595 PVE
::Firewall
::remove_pvefw_chains
();
3599 my $hostfw_conf = load_hostfw_conf
();
3601 my ($ruleset, $ipset_ruleset, $rulesetv6) = compile
($cluster_conf, $hostfw_conf);
3603 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6);