8 use Socket
qw(AF_INET6 inet_ntop inet_pton);
10 use PVE
::Exception
qw(raise raise_param_exc);
11 use PVE
::JSONSchema
qw(register_standard_option get_standard_option);
14 use PVE
::Tools
qw($IPV4RE $IPV6RE);
19 use PVE
::Tools
qw(run_command lock_file dir_glob_foreach);
22 my $hostfw_conf_filename = "/etc/pve/local/host.fw";
23 my $pvefw_conf_dir = "/etc/pve/firewall";
24 my $clusterfw_conf_filename = "$pvefw_conf_dir/cluster.fw";
26 # dynamically include PVE::QemuServer and PVE::OpenVZ
27 # to avoid dependency problems
30 require PVE
::QemuServer
;
31 $have_qemu_server = 1;
37 $have_pve_manager = 1;
40 my $pve_fw_status_dir = "/var/lib/pve-firewall";
42 mkdir $pve_fw_status_dir; # make sure this exists
44 my $security_group_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
45 my $ipset_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
46 our $ip_alias_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
48 my $max_alias_name_length = 64;
49 my $max_ipset_name_length = 64;
50 my $max_group_name_length = 20;
52 PVE
::JSONSchema
::register_format
('IPorCIDR', \
&pve_verify_ip_or_cidr
);
53 sub pve_verify_ip_or_cidr
{
54 my ($cidr, $noerr) = @_;
56 if ($cidr =~ m!^(?:$IPV6RE|$IPV4RE)(/(\d+))?$!) {
57 return $cidr if Net
::IP-
>new($cidr);
58 return undef if $noerr;
59 die Net
::IP
::Error
() . "\n";
61 return undef if $noerr;
62 die "value does not look like a valid IP address or CIDR network\n";
65 PVE
::JSONSchema
::register_format
('IPorCIDRorAlias', \
&pve_verify_ip_or_cidr_or_alias
);
66 sub pve_verify_ip_or_cidr_or_alias
{
67 my ($cidr, $noerr) = @_;
69 return if $cidr =~ m/^(?:$ip_alias_pattern)$/;
71 return pve_verify_ip_or_cidr
($cidr, $noerr);
74 PVE
::JSONSchema
::register_standard_option
('ipset-name', {
75 description
=> "IP set name.",
77 pattern
=> $ipset_name_pattern,
79 maxLength
=> $max_ipset_name_length,
82 PVE
::JSONSchema
::register_standard_option
('pve-fw-alias', {
83 description
=> "Alias name.",
85 pattern
=> $ip_alias_pattern,
87 maxLength
=> $max_alias_name_length,
90 PVE
::JSONSchema
::register_standard_option
('pve-fw-loglevel' => {
91 description
=> "Log level.",
93 enum
=> ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'nolog'],
97 PVE
::JSONSchema
::register_standard_option
('pve-security-group-name', {
98 description
=> "Security Group name.",
100 pattern
=> $security_group_name_pattern,
102 maxLength
=> $max_group_name_length,
105 my $feature_ipset_nomatch = 0;
107 my (undef, undef, $release) = POSIX
::uname
();
108 if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) {
109 my ($major, $minor) = ($1, $2);
110 $feature_ipset_nomatch = 1 if ($major > 3) ||
111 ($major == 3 && $minor >= 7);
118 my $nodename = PVE
::INotify
::nodename
();
120 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
122 my $default_log_level = 'nolog'; # avoid logs by default
124 my $log_level_hash = {
135 # we need to overwrite some macros for ipv6
136 my $pve_ipv6fw_macros = {
138 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'echo-request' },
140 'NeighborDiscovery' => [
141 "IPv6 neighbor solicitation, neighbor and router advertisement",
142 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'router-advertisement' },
143 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'neighbor-solicitation' },
144 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'neighbor-advertisement' },
147 { action
=> 'PARAM', proto
=> 'udp', dport
=> '546:547', sport
=> '546:547' },
150 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
151 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'echo-request' },
155 # imported/converted from: /usr/share/shorewall/macro.*
156 my $pve_fw_macros = {
159 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
160 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
163 "Auth (identd) traffic",
164 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
167 "Border Gateway Protocol traffic",
168 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
171 "BitTorrent traffic for BitTorrent 3.1 and earlier",
172 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
173 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
176 "BitTorrent traffic for BitTorrent 3.2 and later",
177 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
178 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
181 "Concurrent Versions System pserver traffic",
182 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
185 "Citrix/ICA traffic (ICA, ICA Browser, CGP)",
186 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
187 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
188 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
191 "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)",
192 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
193 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
196 "Distributed Checksum Clearinghouse spam filtering mechanism",
197 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
200 "Forwarded DHCP traffic",
201 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
204 "Domain Name System traffic (upd and tcp)",
205 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
206 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
209 "Distributed Compiler service",
210 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
213 "File Transfer Protocol",
214 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
217 "Finger protocol (RFC 742)",
218 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
221 "GNUnet secure peer-to-peer networking traffic",
222 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
223 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
224 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
225 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
228 "Generic Routing Encapsulation tunneling protocol",
229 { action
=> 'PARAM', proto
=> '47' },
232 "Git distributed revision control traffic",
233 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
236 "OpenPGP HTTP keyserver protocol traffic",
237 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
240 "Hypertext Transfer Protocol (WWW)",
241 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
244 "Hypertext Transfer Protocol (WWW) over SSL",
245 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
248 "Internet Cache Protocol V2 (Squid) traffic",
249 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
252 "AOL Instant Messenger traffic",
253 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
256 "Internet Message Access Protocol",
257 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
260 "Internet Message Access Protocol over SSL",
261 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
264 "IPIP capsulation traffic",
265 { action
=> 'PARAM', proto
=> '94' },
269 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
270 { action
=> 'PARAM', proto
=> '50' },
273 "IPsec authentication (AH) traffic",
274 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
275 { action
=> 'PARAM', proto
=> '51' },
278 "IPsec traffic and Nat-Traversal",
279 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
280 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
281 { action
=> 'PARAM', proto
=> '50' },
284 "Internet Relay Chat traffic",
285 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
288 "HP Jetdirect printing",
289 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
292 "Layer 2 Tunneling Protocol traffic",
293 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
296 "Lightweight Directory Access Protocol traffic",
297 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
300 "Secure Lightweight Directory Access Protocol traffic",
301 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
304 "Microsoft Notification Protocol",
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
308 "Microsoft SQL Server",
309 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
312 "Mail traffic (SMTP, SMTPS, Submission)",
313 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
314 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
315 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
318 "Munin networked resource monitoring traffic",
319 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
323 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
326 "NNTP traffic (Usenet).",
327 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
330 "Encrypted NNTP traffic (Usenet)",
331 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
334 "Network Time Protocol (ntpd)",
335 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
338 "OSPF multicast traffic",
339 { action
=> 'PARAM', proto
=> '89' },
343 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
346 "Symantec PCAnywere (tm)",
347 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
348 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
352 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
355 "Encrypted POP3 traffic",
356 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
359 "Point-to-Point Tunneling Protocol",
360 { action
=> 'PARAM', proto
=> '47' },
361 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
365 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
369 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
372 "Line Printer protocol printing",
373 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
376 "Microsoft Remote Desktop Protocol traffic",
377 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
380 "Routing Information Protocol (bidirectional)",
381 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
384 "BIND remote management protocol",
385 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
388 "Razor Antispam System",
389 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
392 "Remote time retrieval (rdate)",
393 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
397 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
400 "SANE network scanning",
401 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
404 "Microsoft SMB traffic",
405 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
406 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
407 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
408 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
411 "Samba Web Administration Tool",
412 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
415 "Simple Mail Transfer Protocol",
416 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
419 "Encrypted Simple Mail Transfer Protocol",
420 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
423 "Simple Network Management Protocol",
424 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
425 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
428 "Spam Assassin SPAMD traffic",
429 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
432 "Secure shell traffic",
433 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
436 "Subversion server (svnserve)",
437 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
440 "SixXS IPv6 Deployment and Tunnel Broker",
441 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
442 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
443 { action
=> 'PARAM', proto
=> '41' },
444 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
447 "Squid web proxy traffic",
448 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
451 "Mail message submission traffic",
452 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
455 "Syslog protocol (RFC 5424) traffic",
456 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
457 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
460 "Trivial File Transfer Protocol traffic",
461 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
465 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
469 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
472 "RFC 868 Time protocol",
473 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
476 "Traceroute (for up to 30 hops) traffic",
477 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
478 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
481 "VNC traffic for VNC display's 0 - 99",
482 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5999' },
485 "VNC traffic from Vncservers to Vncviewers in listen mode",
486 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
489 "WWW traffic (HTTP and HTTPS)",
490 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
491 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
494 "Web Cache/Proxy traffic (port 8080)",
495 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
499 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
502 "Whois (nicname, RFC 3912) traffic",
503 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
507 my $pve_fw_parsed_macros;
508 my $pve_fw_macro_descr;
509 my $pve_fw_preferred_macro_names = {};
511 my $pve_std_chains = {};
512 $pve_std_chains->{4} = {
513 'PVEFW-SET-ACCEPT-MARK' => [
514 "-j MARK --set-mark 1",
516 'PVEFW-DropBroadcast' => [
517 # same as shorewall 'Broadcast'
518 # simply DROP BROADCAST/MULTICAST/ANYCAST
519 # we can use this to reduce logging
520 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
521 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
522 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
523 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
526 # same as shorewall 'reject'
527 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
528 { action
=> 'DROP', source
=> '224.0.0.0/4' },
529 { action
=> 'DROP', proto
=> 'icmp' },
530 "-p tcp -j REJECT --reject-with tcp-reset",
531 "-p udp -j REJECT --reject-with icmp-port-unreachable",
532 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
533 "-j REJECT --reject-with icmp-host-prohibited",
536 # same as shorewall 'Drop', which is equal to DROP,
537 # but REJECT/DROP some packages to reduce logging,
538 # and ACCEPT critical ICMP types
539 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
540 # we are not interested in BROADCAST/MULTICAST/ANYCAST
541 { action
=> 'PVEFW-DropBroadcast' },
542 # ACCEPT critical ICMP types
543 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
544 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
545 # Drop packets with INVALID state
546 "-m conntrack --ctstate INVALID -j DROP",
547 # Drop Microsoft SMB noise
548 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
549 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
550 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
551 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
552 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
553 # Drop new/NotSyn traffic so that it doesn't get logged
554 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
556 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
559 # same as shorewall 'Reject', which is equal to Reject,
560 # but REJECT/DROP some packages to reduce logging,
561 # and ACCEPT critical ICMP types
562 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
563 # we are not interested in BROADCAST/MULTICAST/ANYCAST
564 { action
=> 'PVEFW-DropBroadcast' },
565 # ACCEPT critical ICMP types
566 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
567 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
568 # Drop packets with INVALID state
569 "-m conntrack --ctstate INVALID -j DROP",
570 # Drop Microsoft SMB noise
571 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
572 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
573 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
574 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
575 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
576 # Drop new/NotSyn traffic so that it doesn't get logged
577 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
579 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
581 'PVEFW-tcpflags' => [
582 # same as shorewall tcpflags action.
583 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
584 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
585 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
586 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
587 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
588 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
591 # same as shorewall smurfs action
592 # Filter packets for smurfs (packets with a broadcast address as the source).
593 "-s 0.0.0.0/32 -j RETURN",
594 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
595 "-s 224.0.0.0/4 -g PVEFW-smurflog",
599 $pve_std_chains->{6} = {
600 'PVEFW-SET-ACCEPT-MARK' => [
601 "-j MARK --set-mark 1",
603 'PVEFW-DropBroadcast' => [
604 # same as shorewall 'Broadcast'
605 # simply DROP BROADCAST/MULTICAST/ANYCAST
606 # we can use this to reduce logging
607 #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6
608 # ipv6 addrtype does not work with kernel 2.6.32
609 #{ action => 'DROP', dsttype => 'MULTICAST' },
610 #{ action => 'DROP', dsttype => 'ANYCAST' },
611 { action
=> 'DROP', dest
=> 'ff00::/8' },
612 #{ action => 'DROP', dest => '224.0.0.0/4' },
615 # same as shorewall 'reject'
616 #{ action => 'DROP', dsttype => 'BROADCAST' },
617 #{ action => 'DROP', source => '224.0.0.0/4' },
618 { action
=> 'DROP', proto
=> 'icmpv6' },
619 "-p tcp -j REJECT --reject-with tcp-reset",
620 #"-p udp -j REJECT --reject-with icmp-port-unreachable",
621 #"-p icmp -j REJECT --reject-with icmp-host-unreachable",
622 #"-j REJECT --reject-with icmp-host-prohibited",
625 # same as shorewall 'Drop', which is equal to DROP,
626 # but REJECT/DROP some packages to reduce logging,
627 # and ACCEPT critical ICMP types
628 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
629 # we are not interested in BROADCAST/MULTICAST/ANYCAST
630 { action
=> 'PVEFW-DropBroadcast' },
631 # ACCEPT critical ICMP types
632 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
633 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
634 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
636 # Drop packets with INVALID state
637 "-m conntrack --ctstate INVALID -j DROP",
638 # Drop Microsoft SMB noise
639 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
640 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
641 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
642 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
643 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
644 # Drop new/NotSyn traffic so that it doesn't get logged
645 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
647 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
650 # same as shorewall 'Reject', which is equal to Reject,
651 # but REJECT/DROP some packages to reduce logging,
652 # and ACCEPT critical ICMP types
653 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
654 # we are not interested in BROADCAST/MULTICAST/ANYCAST
655 { action
=> 'PVEFW-DropBroadcast' },
656 # ACCEPT critical ICMP types
657 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
658 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
659 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
661 # Drop packets with INVALID state
662 "-m conntrack --ctstate INVALID -j DROP",
663 # Drop Microsoft SMB noise
664 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
665 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
666 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
667 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
668 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
669 # Drop new/NotSyn traffic so that it doesn't get logged
670 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
672 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
674 'PVEFW-tcpflags' => [
675 # same as shorewall tcpflags action.
676 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
677 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
678 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
679 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
680 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
681 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
685 # iptables -p icmp -h
686 my $icmp_type_names = {
689 'destination-unreachable' => 1,
690 'network-unreachable' => 1,
691 'host-unreachable' => 1,
692 'protocol-unreachable' => 1,
693 'port-unreachable' => 1,
694 'fragmentation-needed' => 1,
695 'source-route-failed' => 1,
696 'network-unknown' => 1,
698 'network-prohibited' => 1,
699 'host-prohibited' => 1,
700 'TOS-network-unreachable' => 1,
701 'TOS-host-unreachable' => 1,
702 'communication-prohibited' => 1,
703 'host-precedence-violation' => 1,
704 'precedence-cutoff' => 1,
705 'source-quench' => 1,
707 'network-redirect' => 1,
708 'host-redirect' => 1,
709 'TOS-network-redirect' => 1,
710 'TOS-host-redirect' => 1,
712 'router-advertisement' => 1,
713 'router-solicitation' => 1,
714 'time-exceeded' => 1,
715 'ttl-zero-during-transit' => 1,
716 'ttl-zero-during-reassembly' => 1,
717 'parameter-problem' => 1,
718 'ip-header-bad' => 1,
719 'required-option-missing' => 1,
720 'timestamp-request' => 1,
721 'timestamp-reply' => 1,
722 'address-mask-request' => 1,
723 'address-mask-reply' => 1,
726 # ip6tables -p icmpv6 -h
728 my $icmpv6_type_names = {
730 'destination-unreachable' => 1,
732 'communication-prohibited' => 1,
733 'address-unreachable' => 1,
734 'port-unreachable' => 1,
735 'packet-too-big' => 1,
736 'time-exceeded' => 1,
737 'ttl-zero-during-transit' => 1,
738 'ttl-zero-during-reassembly' => 1,
739 'parameter-problem' => 1,
741 'unknown-header-type' => 1,
742 'unknown-option' => 1,
745 'router-solicitation' => 1,
746 'router-advertisement' => 1,
747 'neighbor-solicitation' => 1,
748 'neighbour-solicitation' => 1,
749 'neighbor-advertisement' => 1,
750 'neighbour-advertisement' => 1,
754 sub init_firewall_macros
{
756 $pve_fw_parsed_macros = {};
758 foreach my $k (keys %$pve_fw_macros) {
759 my $lc_name = lc($k);
760 my $macro = $pve_fw_macros->{$k};
761 if (!ref($macro->[0])) {
762 $pve_fw_macro_descr->{$k} = shift @$macro;
764 $pve_fw_preferred_macro_names->{$lc_name} = $k;
765 $pve_fw_parsed_macros->{$k} = $macro;
769 init_firewall_macros
();
772 return wantarray ?
($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
777 sub get_etc_services
{
779 return $etc_services if $etc_services;
781 my $filename = "/etc/services";
783 my $fh = IO
::File-
>new($filename, O_RDONLY
);
785 warn "unable to read '$filename' - $!\n";
791 while (my $line = <$fh>) {
793 next if $line =~m/^#/;
794 next if ($line =~m/^\s*$/);
796 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
797 $services->{byid
}->{$2}->{name
} = $1;
798 $services->{byid
}->{$2}->{port
} = $2;
799 $services->{byid
}->{$2}->{$3} = 1;
800 $services->{byname
}->{$1} = $services->{byid
}->{$2};
806 $etc_services = $services;
809 return $etc_services;
814 sub get_etc_protocols
{
815 return $etc_protocols if $etc_protocols;
817 my $filename = "/etc/protocols";
819 my $fh = IO
::File-
>new($filename, O_RDONLY
);
821 warn "unable to read '$filename' - $!\n";
827 while (my $line = <$fh>) {
829 next if $line =~m/^#/;
830 next if ($line =~m/^\s*$/);
832 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
833 $protocols->{byid
}->{$2}->{name
} = $1;
834 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
840 # add special case for ICMP v6
841 $protocols->{byid
}->{icmpv6
}->{name
} = "icmpv6";
842 $protocols->{byname
}->{icmpv6
} = $protocols->{byid
}->{icmpv6
};
844 $etc_protocols = $protocols;
846 return $etc_protocols;
849 my $ipv4_mask_hash_localnet = {
851 '255.255.128.0' => 17,
852 '255.255.192.0' => 18,
853 '255.255.224.0' => 19,
854 '255.255.240.0' => 20,
855 '255.255.248.0' => 21,
856 '255.255.252.0' => 22,
857 '255.255.254.0' => 23,
858 '255.255.255.0' => 24,
859 '255.255.255.128' => 25,
860 '255.255.255.192' => 26,
861 '255.255.255.224' => 27,
862 '255.255.255.240' => 28,
863 '255.255.255.248' => 29,
864 '255.255.255.252' => 30,
870 my ($new_value) = @_;
872 $__local_network = $new_value if defined($new_value);
874 return $__local_network if defined($__local_network);
877 my $nodename = PVE
::INotify
::nodename
();
879 my $ip = PVE
::Cluster
::remote_node_ip
($nodename);
881 my $testip = Net
::IP-
>new($ip);
883 my $routes = PVE
::ProcFSTools
::read_proc_net_route
();
884 foreach my $entry (@$routes) {
885 my $mask = $ipv4_mask_hash_localnet->{$entry->{mask
}};
886 next if !defined($mask);
887 return if $mask eq '0.0.0.0';
888 my $cidr = "$entry->{dest}/$mask";
889 my $testnet = Net
::IP-
>new($cidr);
890 if ($testnet->overlaps($testip)) {
891 $__local_network = $cidr;
898 return $__local_network;
901 # ipset names are limited to 31 characters,
902 # and we use '-v4' or '-v6' to indicate IP versions,
903 # and we use '_swap' suffix for atomic update,
904 # for example PVEFW-${VMID}-${ipset_name}_swap
906 my $max_iptables_ipset_name_length = 31 - length("PVEFW-") - length("_swap");
908 sub compute_ipset_chain_name
{
909 my ($vmid, $ipset_name, $ipversion) = @_;
911 $vmid = 0 if !defined($vmid);
913 my $id = "$vmid-${ipset_name}-v$ipversion";
915 if (length($id) > $max_iptables_ipset_name_length) {
916 $id = PVE
::Tools
::fnv31a_hex
($id);
922 sub compute_ipfilter_ipset_name
{
925 return "ipfilter-$iface";
928 sub parse_address_list
{
931 if ($str =~ m/^(\+)(\S+)$/) { # ipset ref
932 die "ipset name too long\n" if length($str) > ($max_ipset_name_length + 1);
936 if ($str =~ m/^${ip_alias_pattern}$/) {
937 die "alias name too long\n" if length($str) > $max_alias_name_length;
945 foreach my $elem (split(/,/, $str)) {
947 my $ip = Net
::IP-
>new($elem);
949 my $err = Net
::IP
::Error
();
950 die "invalid IP address: $err\n";
952 $iprange = 1 if $elem =~ m/-/;
954 my $new_ipversion = Net
::IP
::ip_is_ipv6
($ip->ip()) ?
6 : 4;
956 die "detected mixed ipv4/ipv6 addresses in address list '$str'\n"
957 if $ipversion && ($new_ipversion != $ipversion);
959 $ipversion = $new_ipversion;
962 die "you can't use a range in a list\n" if $iprange && $count > 1;
967 sub parse_port_name_number_or_range
{
970 my $services = PVE
::Firewall
::get_etc_services
();
974 foreach my $item (split(/,/, $str)) {
976 if ($item =~ m/^(\d+):(\d+)$/) {
977 my ($port1, $port2) = ($1, $2);
978 die "invalid port '$port1'\n" if $port1 > 65535;
979 die "invalid port '$port2'\n" if $port2 > 65535;
980 } elsif ($item =~ m/^(\d+)$/) {
982 die "invalid port '$port'\n" if $port > 65535;
984 if ($icmp_type_names->{$item}) {
986 } elsif ($icmpv6_type_names->{$item}) {
989 die "invalid port '$item'\n" if !$services->{byname
}->{$item};
994 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
999 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
1000 sub pve_fw_verify_port_spec
{
1003 parse_port_name_number_or_range
($portstr);
1008 PVE
::JSONSchema
::register_format
('pve-fw-addr-spec', \
&pve_fw_verify_addr_spec
);
1009 sub pve_fw_verify_addr_spec
{
1012 parse_address_list
($list);
1017 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
1018 sub pve_fw_verify_protocol_spec
{
1021 my $protocols = get_etc_protocols
();
1023 die "unknown protocol '$proto'\n" if $proto &&
1024 !(defined($protocols->{byname
}->{$proto}) ||
1025 defined($protocols->{byid
}->{$proto}));
1031 # helper function for API
1033 sub copy_opject_with_digest
{
1036 my $sha = Digest
::SHA-
>new('sha1');
1039 foreach my $k (sort keys %$object) {
1040 my $v = $object->{$k};
1041 next if !defined($v);
1043 $sha->add($k, ':', $v, "\n");
1046 my $digest = $sha->hexdigest;
1048 $res->{digest
} = $digest;
1050 return wantarray ?
($res, $digest) : $res;
1053 sub copy_list_with_digest
{
1056 my $sha = Digest
::SHA-
>new('sha1');
1059 foreach my $entry (@$list) {
1061 foreach my $k (sort keys %$entry) {
1062 my $v = $entry->{$k};
1063 next if !defined($v);
1065 # Note: digest ignores refs ($rule->{errors})
1066 $sha->add($k, ':', $v, "\n") if !ref($v); ;
1071 my $digest = $sha->hexdigest;
1073 foreach my $entry (@$res) {
1074 $entry->{digest
} = $digest;
1077 return wantarray ?
($res, $digest) : $res;
1080 my $rule_properties = {
1082 description
=> "Update rule at position <pos>.",
1087 digest
=> get_standard_option
('pve-config-digest'),
1091 enum
=> ['in', 'out', 'group'],
1094 description
=> "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
1097 pattern
=> $security_group_name_pattern,
1106 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
1108 type
=> 'string', format
=> 'pve-fw-addr-spec',
1112 type
=> 'string', format
=> 'pve-fw-addr-spec',
1116 type
=> 'string', format
=> 'pve-fw-protocol-spec',
1124 type
=> 'string', format
=> 'pve-fw-port-spec',
1128 type
=> 'string', format
=> 'pve-fw-port-spec',
1137 sub add_rule_properties
{
1138 my ($properties) = @_;
1140 foreach my $k (keys %$rule_properties) {
1141 my $h = $rule_properties->{$k};
1142 # copy data, so that we can modify later without side effects
1143 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
1149 sub delete_rule_properties
{
1150 my ($rule, $delete_str) = @_;
1152 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
1153 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
1154 if !defined($rule_properties->{$opt});
1155 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
1156 if $opt eq 'type' || $opt eq 'action';
1157 delete $rule->{$opt};
1163 my $apply_macro = sub {
1164 my ($macro_name, $param, $verify, $ipversion) = @_;
1166 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1167 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1169 if ($ipversion && ($ipversion == 6) && $pve_ipv6fw_macros->{$macro_name}) {
1170 $macro_rules = $pve_ipv6fw_macros->{$macro_name};
1175 foreach my $templ (@$macro_rules) {
1177 my $param_used = {};
1178 foreach my $k (keys %$templ) {
1179 my $v = $templ->{$k};
1180 if ($v eq 'PARAM') {
1182 $param_used->{$k} = 1;
1183 } elsif ($v eq 'DEST') {
1184 $v = $param->{dest
};
1185 $param_used->{dest
} = 1;
1186 } elsif ($v eq 'SOURCE') {
1187 $v = $param->{source
};
1188 $param_used->{source
} = 1;
1192 my $msg = "missing parameter '$k' in macro '$macro_name'";
1193 raise_param_exc
({ macro => $msg }) if $verify;
1198 foreach my $k (keys %$param) {
1199 next if $k eq 'macro';
1200 next if !defined($param->{$k});
1201 next if $param_used->{$k};
1202 if (defined($rule->{$k})) {
1203 if ($rule->{$k} ne $param->{$k}) {
1204 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
1205 raise_param_exc
({ $k => $msg }) if $verify;
1209 $rule->{$k} = $param->{$k};
1212 push @$rules, $rule;
1218 my $rule_env_iface_lookup = {
1227 my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_;
1229 my $allow_groups = $rule_env eq 'group' ?
0 : 1;
1231 my $allow_iface = $rule_env_iface_lookup->{$rule_env};
1232 die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen
1234 my $errors = $rule->{errors
} || {};
1236 my $error_count = 0;
1238 my $add_error = sub {
1239 my ($param, $msg) = @_;
1241 raise_param_exc
({ $param => $msg }) if !$noerr;
1243 $errors->{$param} = $msg if !$errors->{$param};
1247 my $set_ip_version = sub {
1250 die "detected mixed ipv4/ipv6 adresses in rule\n"
1251 if $ipversion && ($vers != $ipversion);
1256 my $check_ipset_or_alias_property = sub {
1257 my ($name, $expected_ipversion) = @_;
1259 if (my $value = $rule->{$name}) {
1260 if ($value =~ m/^\+/) {
1261 if ($value =~ m/^\+(${ipset_name_pattern})$/) {
1262 &$add_error($name, "no such ipset '$1'")
1263 if !($cluster_conf->{ipset
}->{$1} || ($fw_conf && $fw_conf->{ipset
}->{$1}));
1266 &$add_error($name, "invalid ipset name '$value'");
1268 } elsif ($value =~ m/^${ip_alias_pattern}$/){
1269 my $alias = lc($value);
1270 &$add_error($name, "no such alias '$value'")
1271 if !($cluster_conf->{aliases
}->{$alias} || ($fw_conf && $fw_conf->{aliases
}->{$alias}));
1272 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1273 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1275 &$set_ip_version($e->{ipversion
});
1280 my $type = $rule->{type
};
1281 my $action = $rule->{action
};
1283 &$add_error('type', "missing property") if !$type;
1284 &$add_error('action', "missing property") if !$action;
1287 if ($type eq 'in' || $type eq 'out') {
1288 &$add_error('action', "unknown action '$action'")
1289 if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/);
1290 } elsif ($type eq 'group') {
1291 &$add_error('type', "security groups not allowed")
1293 &$add_error('action', "invalid characters in security group name")
1294 if $action && ($action !~ m/^${security_group_name_pattern}$/);
1296 &$add_error('type', "unknown rule type '$type'");
1300 if ($rule->{iface
}) {
1301 &$add_error('type', "parameter -i not allowed for this rule type")
1303 eval { PVE
::JSONSchema
::pve_verify_iface
($rule->{iface
}); };
1304 &$add_error('iface', $@) if $@;
1305 if ($rule_env eq 'vm') {
1306 &$add_error('iface', "value does not match the regex pattern 'net\\d+'")
1307 if $rule->{iface
} !~ m/^net(\d+)$/;
1308 } elsif ($rule_env eq 'ct') {
1309 &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'")
1310 if $rule->{iface
} !~ m/^(venet|eth(\d+))$/;
1314 if ($rule->{macro}) {
1315 if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) {
1316 $rule->{macro} = $preferred_name;
1318 &$add_error('macro', "unknown macro '$rule->{macro}'");
1322 if ($rule->{proto
}) {
1323 eval { pve_fw_verify_protocol_spec
($rule->{proto
}); };
1324 &$add_error('proto', $@) if $@;
1325 &$set_ip_version(4) if $rule->{proto
} eq 'icmp';
1326 &$set_ip_version(6) if $rule->{proto
} eq 'icmpv6';
1329 if ($rule->{dport
}) {
1330 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1331 &$add_error('dport', $@) if $@;
1332 &$add_error('proto', "missing property - 'dport' requires this property")
1336 if ($rule->{sport
}) {
1337 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1338 &$add_error('sport', $@) if $@;
1339 &$add_error('proto', "missing property - 'sport' requires this property")
1343 if ($rule->{source
}) {
1345 my $source_ipversion = parse_address_list
($rule->{source
});
1346 &$set_ip_version($source_ipversion);
1348 &$add_error('source', $@) if $@;
1349 &$check_ipset_or_alias_property('source', $ipversion);
1352 if ($rule->{dest
}) {
1354 my $dest_ipversion = parse_address_list
($rule->{dest
});
1355 &$set_ip_version($dest_ipversion);
1357 &$add_error('dest', $@) if $@;
1358 &$check_ipset_or_alias_property('dest', $ipversion);
1361 $rule->{ipversion
} = $ipversion if $ipversion;
1363 if ($rule->{macro} && !$error_count) {
1364 eval { &$apply_macro($rule->{macro}, $rule, 1, $ipversion); };
1366 if (ref($err) eq "PVE::Exception" && $err->{errors
}) {
1367 my $eh = $err->{errors
};
1368 foreach my $p (keys %$eh) {
1369 &$add_error($p, $eh->{$p});
1372 &$add_error('macro', "$err");
1377 $rule->{errors
} = $errors if $error_count;
1382 sub copy_rule_data
{
1383 my ($rule, $param) = @_;
1385 foreach my $k (keys %$rule_properties) {
1386 if (defined(my $v = $param->{$k})) {
1387 if ($v eq '' || $v eq '-') {
1398 sub rules_modify_permissions
{
1399 my ($rule_env) = @_;
1401 if ($rule_env eq 'host') {
1403 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
1405 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1407 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1409 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1411 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
1418 sub rules_audit_permissions
{
1419 my ($rule_env) = @_;
1421 if ($rule_env eq 'host') {
1423 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
1425 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1427 check
=> ['perm', '/', [ 'Sys.Audit' ]],
1429 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1431 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1439 my $bridge_firewall_enabled = 0;
1441 sub enable_bridge_firewall
{
1443 return if $bridge_firewall_enabled; # only once
1445 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1446 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1448 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1449 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1451 $bridge_firewall_enabled = 1;
1454 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1456 sub iptables_restore_cmdlist
{
1459 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1462 sub ip6tables_restore_cmdlist
{
1465 run_command
("/sbin/ip6tables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1468 sub ipset_restore_cmdlist
{
1471 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist, errmsg
=> "ipset_restore_cmdlist");
1474 sub iptables_get_chains
{
1475 my ($iptablescmd) = @_;
1477 $iptablescmd = "iptables" if !$iptablescmd;
1481 # check what chains we want to track
1482 my $is_pvefw_chain = sub {
1485 return 1 if $name =~ m/^PVEFW-\S+$/;
1487 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
1489 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
1491 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
1493 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/;
1494 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
1506 return if $line =~ m/^#/;
1507 return if $line =~ m/^\s*$/;
1509 if ($line =~ m/^\*(\S+)$/) {
1514 return if $table ne 'filter';
1516 if ($line =~ m/^:(\S+)\s/) {
1518 return if !&$is_pvefw_chain($chain);
1519 $res->{$chain} = "unknown";
1520 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1521 my ($chain, $sig) = ($1, $2);
1522 return if !&$is_pvefw_chain($chain);
1523 $res->{$chain} = $sig;
1524 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1527 # simply ignore the rest
1532 run_command
("/sbin/$iptablescmd-save", outfunc
=> $parser);
1534 return wantarray ?
($res, $hooks) : $res;
1537 sub iptables_chain_digest
{
1539 my $digest = Digest
::SHA-
>new('sha1');
1540 foreach my $rule (@$rules) { # order is important
1541 $digest->add($rule);
1543 return $digest->b64digest;
1546 sub ipset_chain_digest
{
1549 my $digest = Digest
::SHA-
>new('sha1');
1550 foreach my $rule (sort @$rules) { # note: sorted
1551 $digest->add($rule);
1553 return $digest->b64digest;
1556 sub ipset_get_chains
{
1564 return if $line =~ m/^#/;
1565 return if $line =~ m/^\s*$/;
1566 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1568 $line =~ s/\s+$//; # delete trailing white space
1569 push @{$chains->{$chain}}, $line;
1571 # simply ignore the rest
1576 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
1578 # compute digest for each chain
1579 foreach my $chain (keys %$chains) {
1580 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1586 sub ruleset_generate_cmdstr
{
1587 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1589 return if defined($rule->{enable
}) && !$rule->{enable
};
1590 return if $rule->{errors
};
1592 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1594 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1595 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1599 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1600 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1602 my $source = $rule->{source
};
1603 my $dest = $rule->{dest
};
1606 if ($source =~ m/^\+/) {
1607 if ($source =~ m/^\+(${ipset_name_pattern})$/) {
1609 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1610 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1611 push @cmd, "-m set --match-set ${ipset_chain} src";
1612 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1613 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1614 push @cmd, "-m set --match-set ${ipset_chain} src";
1616 die "no such ipset '$name'\n";
1619 die "invalid security group name '$source'\n";
1621 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1622 my $alias = lc($source);
1623 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1624 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1625 die "no such alias '$source'\n" if !$e;
1626 push @cmd, "-s $e->{cidr}";
1627 } elsif ($source =~ m/\-/){
1628 push @cmd, "-m iprange --src-range $source";
1630 push @cmd, "-s $source";
1635 if ($dest =~ m/^\+/) {
1636 if ($dest =~ m/^\+(${ipset_name_pattern})$/) {
1638 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1639 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1640 push @cmd, "-m set --match-set ${ipset_chain} dst";
1641 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1642 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1643 push @cmd, "-m set --match-set ${ipset_chain} dst";
1645 die "no such ipset '$name'\n";
1648 die "invalid security group name '$dest'\n";
1650 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1651 my $alias = lc($dest);
1652 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1653 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1654 die "no such alias '$dest'\n" if !$e;
1655 push @cmd, "-d $e->{cidr}";
1656 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1657 push @cmd, "-m iprange --dst-range $dest";
1659 push @cmd, "-d $dest";
1663 if ($rule->{proto
}) {
1664 push @cmd, "-p $rule->{proto}";
1667 $multiport++ if $nbdport > 1;
1668 $multiport++ if $nbsport > 1;
1670 push @cmd, "--match multiport" if $multiport;
1672 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1673 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1675 if ($rule->{dport
}) {
1676 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1677 # Note: we use dport to store --icmp-type
1678 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
1679 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1680 } elsif ($rule->{proto
} && $rule->{proto
} eq 'icmpv6') {
1681 # Note: we use dport to store --icmpv6-type
1682 die "unknown icmpv6-type '$rule->{dport}'\n" if !defined($icmpv6_type_names->{$rule->{dport
}});
1683 push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}";
1686 if ($multiport == 2) {
1687 push @cmd, "--ports $rule->{dport}";
1689 push @cmd, "--dports $rule->{dport}";
1692 push @cmd, "--dport $rule->{dport}";
1697 if ($rule->{sport
}) {
1699 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1701 push @cmd, "--sport $rule->{sport}";
1704 } elsif ($rule->{dport
} || $rule->{sport
}) {
1705 die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport
};
1706 die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport
};
1709 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1711 if (my $action = $rule->{action
}) {
1712 $action = $actions->{$action} if defined($actions->{$action});
1713 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1714 push @cmd, $goto ?
"-g $action" : "-j $action";
1717 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1720 sub ruleset_generate_rule
{
1721 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1725 if ($rule->{macro}) {
1726 $rules = &$apply_macro($rule->{macro}, $rule, 0, $ipversion);
1731 # update all or nothing
1734 foreach my $tmp (@$rules) {
1735 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) {
1736 push @cmds, $cmdstr;
1740 foreach my $cmdstr (@cmds) {
1741 ruleset_addrule
($ruleset, $chain, $cmdstr);
1745 sub ruleset_generate_rule_insert
{
1746 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto) = @_;
1748 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1750 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $rule, $actions, $goto)) {
1751 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1755 sub ruleset_create_chain
{
1756 my ($ruleset, $chain) = @_;
1758 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1759 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1761 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1763 $ruleset->{$chain} = [];
1766 sub ruleset_chain_exist
{
1767 my ($ruleset, $chain) = @_;
1769 return $ruleset->{$chain} ?
1 : undef;
1772 sub ruleset_addrule
{
1773 my ($ruleset, $chain, $rule) = @_;
1775 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1777 push @{$ruleset->{$chain}}, "-A $chain $rule";
1780 sub ruleset_insertrule
{
1781 my ($ruleset, $chain, $rule) = @_;
1783 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1785 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1788 sub get_log_rule_base
{
1789 my ($chain, $vmid, $msg, $loglevel) = @_;
1791 die "internal error - no log level" if !defined($loglevel);
1793 $vmid = 0 if !defined($vmid);
1795 # Note: we use special format for prefix to pass further
1796 # info to log daemon (VMID, LOGVELEL and CHAIN)
1798 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1801 sub ruleset_addlog {
1802 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1804 return if !defined($loglevel);
1806 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1808 $logrule = "$rule $logrule" if defined($rule);
1810 ruleset_addrule($ruleset, $chain, $logrule);
1813 sub ruleset_add_chain_policy {
1814 my ($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action) = @_;
1816 if ($policy eq 'ACCEPT') {
1818 ruleset_generate_rule($ruleset, $chain, $ipversion, { action => 'ACCEPT' },
1819 { ACCEPT => $accept_action});
1821 } elsif ($policy eq 'DROP') {
1823 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1825 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1827 ruleset_addrule($ruleset, $chain, "-j DROP
");
1828 } elsif ($policy eq 'REJECT') {
1829 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1831 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1833 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1836 die "internal error
: unknown policy
'$policy'";
1840 sub ruleset_chain_add_conn_filters {
1841 my ($ruleset, $chain, $accept) = @_;
1843 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1844 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1847 sub ruleset_chain_add_input_filters {
1848 my ($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel) = @_;
1850 if ($cluster_conf->{ipset}->{blacklist}){
1851 if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist
")) {
1852 ruleset_create_chain($ruleset, "PVEFW-blacklist
");
1853 ruleset_addlog($ruleset, "PVEFW-blacklist
", 0, "DROP
: ", $loglevel) if $loglevel;
1854 ruleset_addrule($ruleset, "PVEFW-blacklist
", "-j DROP
");
1856 my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion);
1857 ruleset_addrule($ruleset, $chain, "-m set
--match-set
${ipset_chain
} src
-j PVEFW-blacklist
");
1860 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1861 if ($ipversion == 4) {
1862 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1866 if ($options->{tcpflags}) {
1867 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1871 sub ruleset_create_vm_chain {
1872 my ($ruleset, $chain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction) = @_;
1874 ruleset_create_chain($ruleset, $chain);
1875 my $accept = generate_nfqueue($options);
1877 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1878 if ($direction eq 'OUT') {
1879 ruleset_generate_rule($ruleset, $chain, $ipversion,
1880 { action => 'PVEFW-SET-ACCEPT-MARK',
1881 proto => 'udp', sport => 68, dport => 67 });
1883 ruleset_generate_rule($ruleset, $chain, $ipversion,
1884 { action => 'ACCEPT',
1885 proto => 'udp', sport => 67, dport => 68 });
1889 if ($direction eq 'OUT') {
1890 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1891 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1893 if ($ipfilter_ipset) {
1894 ruleset_addrule($ruleset, $chain, "-m set
! --match-set
$ipfilter_ipset src
-j DROP
");
1896 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1900 sub ruleset_add_group_rule {
1901 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_;
1903 my $group = $rule->{action};
1904 my $group_chain = "GROUP-
$group-$direction";
1905 if(!ruleset_chain_exist($ruleset, $group_chain)){
1906 generate_group_rules($ruleset, $cluster_conf, $group, $ipversion);
1909 if ($direction eq 'OUT' && $rule->{iface_out}) {
1910 ruleset_addrule($ruleset, $chain, "-o
$rule->{iface_out
} -j
$group_chain");
1911 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1912 ruleset_addrule($ruleset, $chain, "-i
$rule->{iface_in
} -j
$group_chain");
1914 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1917 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$action");
1920 sub ruleset_generate_vm_rules {
1921 my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_;
1923 my $lc_direction = lc($direction);
1925 my $in_accept = generate_nfqueue($options);
1927 foreach my $rule (@$rules) {
1928 next if $rule->{iface} && $rule->{iface} ne $netid;
1929 next if !$rule->{enable} || $rule->{errors};
1930 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
1932 if ($rule->{type} eq 'group') {
1933 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
1934 $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion);
1936 next if $rule->{type} ne $lc_direction;
1938 if ($direction eq 'OUT') {
1939 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1940 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
1941 undef, $cluster_conf, $vmfw_conf);
1943 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1944 { ACCEPT => $in_accept , REJECT => "PVEFW-reject
" },
1945 undef, $cluster_conf, $vmfw_conf);
1953 sub generate_nfqueue {
1956 if ($options->{ips}) {
1957 my $action = "NFQUEUE
";
1958 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1959 if (defined($3) && defined($1)) {
1960 $action .= " --queue-balance
$1:$3";
1961 } elsif (defined($1)) {
1962 $action .= " --queue-num
$1";
1965 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
1972 sub ruleset_generate_vm_ipsrules {
1973 my ($ruleset, $options, $direction, $iface) = @_;
1975 if ($options->{ips} && $direction eq 'IN') {
1976 my $nfqueue = generate_nfqueue($options);
1978 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS
")) {
1979 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1982 ruleset_addrule($ruleset, "PVEFW-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1986 sub generate_venet_rules_direction {
1987 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction, $ipversion) = @_;
1989 my $lc_direction = lc($direction);
1991 my $rules = $vmfw_conf->{rules};
1993 my $options = $vmfw_conf->{options};
1994 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1996 my $chain = "venet0-
$vmid-$direction";
1998 ruleset_create_vm_chain($ruleset, $chain, $ipversion, $options, undef, undef, $direction);
2000 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction, undef, $ipversion);
2005 if ($direction eq 'OUT') {
2006 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2008 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2011 my $accept = generate_nfqueue($options);
2012 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2013 ruleset_add_chain_policy($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2015 if ($direction eq 'OUT') {
2016 ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT
", $ipversion, {
2019 iface_in => 'venet0'});
2021 ruleset_generate_rule($ruleset, "PVEFW-VENET-IN
", $ipversion, {
2024 iface_out => 'venet0'});
2028 sub generate_tap_rules_direction {
2029 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_;
2031 my $lc_direction = lc($direction);
2033 my $rules = $vmfw_conf->{rules};
2035 my $options = $vmfw_conf->{options};
2036 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
2038 my $tapchain = "$iface-$direction";
2040 my $ipfilter_name = compute_ipfilter_ipset_name($netid);
2041 my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
2042 if $vmfw_conf->{ipset}->{$ipfilter_name};
2044 # create chain with mac and ip filter
2045 ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction);
2047 if ($options->{enable}) {
2048 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion);
2050 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
2055 if ($direction eq 'OUT') {
2056 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2058 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2061 my $accept = generate_nfqueue($options);
2062 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2063 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2065 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : 'ACCEPT';
2066 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, 'ACCEPT', $loglevel, $accept_action);
2069 # plug the tap chain to bridge chain
2070 if ($direction eq 'IN') {
2071 ruleset_addrule($ruleset, "PVEFW-FWBR-IN
",
2072 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
2074 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT
",
2075 "-m physdev
--physdev-is-bridged
--physdev-in
$iface -j
$tapchain");
2079 sub enable_host_firewall {
2080 my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
2082 my $options = $hostfw_conf->{options};
2083 my $cluster_options = $cluster_conf->{options};
2084 my $rules = $hostfw_conf->{rules};
2085 my $cluster_rules = $cluster_conf->{rules};
2087 # host inbound firewall
2088 my $chain = "PVEFW-HOST-IN
";
2089 ruleset_create_chain($ruleset, $chain);
2091 my $loglevel = get_option_log_level($options, "log_level_in
");
2093 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
2095 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2096 ruleset_chain_add_input_filters($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel);
2098 # we use RETURN because we need to check also tap rules
2099 my $accept_action = 'RETURN';
2101 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2103 # add host rules first, so that cluster wide rules can be overwritten
2104 foreach my $rule (@$rules, @$cluster_rules) {
2105 next if !$rule->{enable} || $rule->{errors};
2106 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2108 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
2111 if ($rule->{type} eq 'group') {
2112 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
2113 } elsif ($rule->{type} eq 'in') {
2114 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2115 { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2116 undef, $cluster_conf, $hostfw_conf);
2120 delete $rule->{iface_in};
2123 # allow standard traffic for management ipset (includes cluster network)
2124 my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management
", $ipversion);
2125 my $mngmntsrc = "-m set
--match-set
${mngmnt_ipset_chain
} src
";
2126 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2127 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2128 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2129 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
22 -j
$accept_action"); # SSH
2131 my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
2132 my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion};
2135 if ($localnet && ($ipversion == $localnet_ver)) {
2136 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2137 ruleset_addrule($ruleset, $chain, "-s
$localnet -d
$localnet $corosync_rule");
2138 ruleset_addrule($ruleset, $chain, "-s
$localnet -m addrtype
--dst-type MULTICAST
$corosync_rule");
2141 # implement input policy
2142 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
2143 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2145 # host outbound firewall
2146 $chain = "PVEFW-HOST-OUT
";
2147 ruleset_create_chain($ruleset, $chain);
2149 $loglevel = get_option_log_level($options, "log_level_out
");
2151 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
2153 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2155 # we use RETURN because we may want to check other thigs later
2156 $accept_action = 'RETURN';
2158 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2160 # add host rules first, so that cluster wide rules can be overwritten
2161 foreach my $rule (@$rules, @$cluster_rules) {
2162 next if !$rule->{enable} || $rule->{errors};
2163 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2165 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
2167 if ($rule->{type} eq 'group') {
2168 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
2169 } elsif ($rule->{type} eq 'out') {
2170 ruleset_generate_rule($ruleset, $chain, $ipversion,
2171 $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2172 undef, $cluster_conf, $hostfw_conf);
2176 delete $rule->{iface_out};
2179 # allow standard traffic on cluster network
2180 if ($localnet && ($ipversion == $localnet_ver)) {
2181 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2182 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
22 -j
$accept_action"); # SSH
2183 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2184 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2186 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2187 ruleset_addrule($ruleset, $chain, "-d
$localnet $corosync_rule");
2188 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
$corosync_rule");
2191 # implement output policy
2192 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
2193 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2195 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
2196 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
2199 sub generate_group_rules {
2200 my ($ruleset, $cluster_conf, $group, $ipversion) = @_;
2202 my $rules = $cluster_conf->{groups}->{$group};
2205 warn "no such security group
'$group'\n";
2206 $rules = []; # create empty chain
2209 my $chain = "GROUP-
${group
}-IN
";
2211 ruleset_create_chain($ruleset, $chain);
2212 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2214 foreach my $rule (@$rules) {
2215 next if $rule->{type} ne 'in';
2216 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2217 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2218 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
2219 undef, $cluster_conf);
2222 $chain = "GROUP-
${group
}-OUT
";
2224 ruleset_create_chain($ruleset, $chain);
2225 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2227 foreach my $rule (@$rules) {
2228 next if $rule->{type} ne 'out';
2229 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2230 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
2231 # check also other tap rules later
2232 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2233 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" },
2234 undef, $cluster_conf);
2239 my $valid_netdev_names = {};
2240 for (my $i = 0; $i < $MAX_NETS; $i++) {
2241 $valid_netdev_names->{"net
$i"} = 1;
2245 my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_;
2247 my $orig_line = $line;
2251 # we can add single line comments to the end of the rule
2252 if ($line =~ s/#\s*(.*?)\s*$//) {
2253 $rule->{comment} = decode('utf8', $1);
2256 # we can disable a rule when prefixed with '|'
2258 $rule->{enable} = $line =~ s/^\|// ? 0 : 1;
2260 $line =~ s/^(\S+)\s+(\S+)\s*// ||
2261 die "unable to parse rule
: $line\n";
2263 $rule->{type} = lc($1);
2264 $rule->{action} = $2;
2266 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
2267 if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
2268 $rule->{macro} = $1;
2269 $rule->{action} = $2;
2273 while (length($line)) {
2274 if ($line =~ s/^-i (\S+)\s*//) {
2275 $rule->{iface} = $1;
2279 last if $rule->{type} eq 'group';
2281 if ($line =~ s/^-p (\S+)\s*//) {
2282 $rule->{proto} = $1;
2286 if ($line =~ s/^-dport (\S+)\s*//) {
2287 $rule->{dport} = $1;
2291 if ($line =~ s/^-sport (\S+)\s*//) {
2292 $rule->{sport} = $1;
2295 if ($line =~ s/^-source (\S+)\s*//) {
2296 $rule->{source} = $1;
2299 if ($line =~ s/^-dest (\S+)\s*//) {
2307 die "unable to parse rule parameters
: $line\n" if length($line);
2309 $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1);
2310 if ($verbose && $rule->{errors}) {
2311 warn "$prefix - errors
in rule parameters
: $orig_line\n";
2312 foreach my $p (keys %{$rule->{errors}}) {
2313 warn " $p: $rule->{errors
}->{$p}\n";
2320 sub parse_vmfw_option {
2325 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
2327 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
2330 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
2332 $value = $2 ? lc($3) : '';
2333 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2336 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
2340 die "can
't parse option '$line'\n"
2343 return ($opt, $value);
2346 sub parse_hostfw_option {
2351 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2353 if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
2356 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
2358 $value = $2 ? lc($3) : '';
2359 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
2363 die "can't parse option
'$line'\n"
2366 return ($opt, $value);
2369 sub parse_clusterfw_option {
2374 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
2377 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2381 die "can
't parse option '$line'\n"
2384 return ($opt, $value);
2388 my ($clusterfw_conf, $fw_conf, $cidr) = @_;
2390 my $alias = lc($cidr);
2391 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
2392 $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
2394 die "no such alias '$cidr'\n" if !$e;;
2396 return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr};
2399 sub parse_ip_or_cidr {
2404 if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
2407 } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
2411 die "value does not look like a valid IP address or CIDR network\n";
2414 return wantarray ? ($cidr, $ipversion) : $cidr;
2420 # we can add single line comments to the end of the line
2421 my $comment = decode('utf8
', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
2423 if ($line =~ m/^(\S+)\s(\S+)$/) {
2424 my ($name, $cidr) = ($1, $2);
2427 ($cidr, $ipversion) = parse_ip_or_cidr($cidr);
2432 ipversion => $ipversion,
2434 $data->{comment} = $comment if $comment;
2441 sub generic_fw_config_parser {
2442 my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_;
2447 my $res = $empty_conf;
2449 while (defined(my $line = <$fh>)) {
2450 next if $line =~ m/^#/;
2451 next if $line =~ m/^\s*$/;
2455 my $linenr = $fh->input_line_number();
2456 my $prefix = "$filename (line $linenr)";
2458 if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
2459 $section = 'options
';
2463 if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) {
2464 $section = 'aliases
';
2468 if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2469 $section = 'groups
';
2473 die "security group name too long\n" if length($group) > $max_group_name_length;
2474 die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/;
2477 ($section, $group, $comment) = undef;
2478 warn "$prefix: $err";
2482 $res->{$section}->{$group} = [];
2483 $res->{group_comments}->{$group} = decode('utf8
', $comment)
2488 if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) {
2493 if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2498 die "ipset name too long\n" if length($group) > $max_ipset_name_length;
2499 die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
2502 ($section, $group, $comment) = undef;
2503 warn "$prefix: $err";
2507 $res->{$section}->{$group} = [];
2508 $res->{ipset_comments}->{$group} = decode('utf8
', $comment)
2514 warn "$prefix: skip line - no section\n";
2518 if ($section eq 'options
') {
2521 if ($rule_env eq 'cluster
') {
2522 ($opt, $value) = parse_clusterfw_option($line);
2523 } elsif ($rule_env eq 'host
') {
2524 ($opt, $value) = parse_hostfw_option($line);
2526 ($opt, $value) = parse_vmfw_option($line);
2528 $res->{options}->{$opt} = $value;
2530 warn "$prefix: $@" if $@;
2531 } elsif ($section eq 'aliases
') {
2533 my $data = parse_alias($line);
2534 $res->{aliases}->{lc($data->{name})} = $data;
2536 warn "$prefix: $@" if $@;
2537 } elsif ($section eq 'rules
') {
2539 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); };
2541 warn "$prefix: $err";
2544 push @{$res->{$section}}, $rule;
2545 } elsif ($section eq 'groups
') {
2547 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group
', $verbose); };
2549 warn "$prefix: $err";
2552 push @{$res->{$section}->{$group}}, $rule;
2553 } elsif ($section eq 'ipset
') {
2554 # we can add single line comments to the end of the rule
2555 my $comment = decode('utf8
', $1) if $line =~ s/#\s*(.*?)\s*$//;
2557 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2562 if ($nomatch && !$feature_ipset_nomatch) {
2563 $errors->{nomatch} = "nomatch not supported by kernel";
2567 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2568 resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
2570 $cidr = parse_ip_or_cidr($cidr);
2575 $errors->{cidr} = $err;
2578 my $entry = { cidr => $cidr };
2579 $entry->{nomatch} = 1 if $nomatch;
2580 $entry->{comment} = $comment if $comment;
2581 $entry->{errors} = $errors if $errors;
2583 if ($verbose && $errors) {
2584 warn "$prefix - errors in ipset '$group': $line\n";
2585 foreach my $p (keys %{$errors}) {
2586 warn " $p: $errors->{$p}\n";
2590 push @{$res->{$section}->{$group}}, $entry;
2592 warn "$prefix: skip line - unknown section\n";
2600 sub parse_hostfw_config {
2601 my ($filename, $fh, $cluster_conf, $verbose) = @_;
2603 my $empty_conf = { rules => [], options => {}};
2605 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host
');
2608 sub parse_vmfw_config {
2609 my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_;
2616 ipset_comments => {},
2619 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env);
2622 sub parse_clusterfw_config {
2623 my ($filename, $fh, $verbose) = @_;
2633 group_comments => {},
2635 ipset_comments => {},
2638 return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster
');
2642 my ($code, @param) = @_;
2646 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2653 sub read_local_vm_config {
2658 my $vmdata = { openvz => $openvz, qemu => $qemu };
2660 my $vmlist = PVE::Cluster::get_vmlist();
2661 return $vmdata if !$vmlist || !$vmlist->{ids};
2662 my $ids = $vmlist->{ids};
2664 foreach my $vmid (keys %$ids) {
2665 next if !$vmid; # skip VE0
2666 my $d = $ids->{$vmid};
2667 next if !$d->{node} || $d->{node} ne $nodename;
2668 next if !$d->{type};
2669 if ($d->{type} eq 'openvz
') {
2670 if ($have_pve_manager) {
2671 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
2672 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2673 $openvz->{$vmid} = $conf;
2676 } elsif ($d->{type} eq 'qemu
') {
2677 if ($have_qemu_server) {
2678 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2679 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2680 $qemu->{$vmid} = $conf;
2689 sub load_vmfw_conf {
2690 my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_;
2694 $dir = $pvefw_conf_dir if !defined($dir);
2696 my $filename = "$dir/$vmid.fw";
2697 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2698 $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose);
2699 $vmfw_conf->{vmid} = $vmid;
2705 my $format_rules = sub {
2706 my ($rules, $allow_iface) = @_;
2710 foreach my $rule (@$rules) {
2711 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
' || $rule->{type} eq 'group
') {
2712 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2713 $raw .= uc($rule->{type});
2714 if ($rule->{macro}) {
2715 $raw .= " $rule->{macro}($rule->{action})";
2717 $raw .= " " . $rule->{action};
2719 if ($allow_iface && $rule->{iface}) {
2720 $raw .= " -i $rule->{iface}";
2723 if ($rule->{type} ne 'group
') {
2724 $raw .= " -source $rule->{source}" if $rule->{source};
2725 $raw .= " -dest $rule->{dest}" if $rule->{dest};
2726 $raw .= " -p $rule->{proto}" if $rule->{proto};
2727 $raw .= " -dport $rule->{dport}" if $rule->{dport};
2728 $raw .= " -sport $rule->{sport}" if $rule->{sport};
2731 $raw .= " # " . encode('utf8
', $rule->{comment})
2732 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2735 die "unknown rule type '$rule->{type
}'";
2742 my $format_options = sub {
2747 $raw .= "[OPTIONS]\n\n";
2748 foreach my $opt (keys %$options) {
2749 $raw .= "$opt: $options->{$opt}\n";
2756 my $format_aliases = sub {
2761 $raw .= "[ALIASES]\n\n";
2762 foreach my $k (keys %$aliases) {
2763 my $e = $aliases->{$k};
2764 $raw .= "$e->{name} $e->{cidr}";
2765 $raw .= " # " . encode('utf8
', $e->{comment})
2766 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
2774 my $format_ipsets = sub {
2779 foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
2780 if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) {
2781 my $utf8comment = encode('utf8
', $comment);
2782 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2784 $raw .= "[IPSET $ipset]\n\n";
2786 my $options = $fw_conf->{ipset}->{$ipset};
2789 foreach my $entry (@$options) {
2790 $nethash->{$entry->{cidr}} = $entry;
2793 foreach my $cidr (sort keys %$nethash) {
2794 my $entry = $nethash->{$cidr};
2795 my $line = $entry->{nomatch} ? '!' : '';
2796 $line .= $entry->{cidr};
2797 $line .= " # " . encode('utf8
', $entry->{comment})
2798 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
2808 sub save_vmfw_conf {
2809 my ($vmid, $vmfw_conf) = @_;
2813 my $options = $vmfw_conf->{options};
2814 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2816 my $aliases = $vmfw_conf->{aliases};
2817 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2819 $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset};
2821 my $rules = $vmfw_conf->{rules} || [];
2822 if ($rules && scalar(@$rules)) {
2823 $raw .= "[RULES]\n\n";
2824 $raw .= &$format_rules($rules, 1);
2828 mkdir $pvefw_conf_dir;
2830 my $filename = "$pvefw_conf_dir/$vmid.fw";
2831 PVE::Tools::file_set_contents($filename, $raw);
2834 sub read_vm_firewall_configs {
2835 my ($cluster_conf, $vmdata, $dir, $verbose) = @_;
2837 my $vmfw_configs = {};
2839 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2840 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm
', $vmid, $dir, $verbose);
2841 next if !$vmfw_conf->{options}; # skip if file does not exists
2842 $vmfw_configs->{$vmid} = $vmfw_conf;
2844 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2845 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct
', $vmid, $dir, $verbose);
2846 next if !$vmfw_conf->{options}; # skip if file does not exists
2847 $vmfw_configs->{$vmid} = $vmfw_conf;
2850 return $vmfw_configs;
2853 sub get_option_log_level {
2854 my ($options, $k) = @_;
2856 my $v = $options->{$k};
2857 $v = $default_log_level if !defined($v);
2859 return undef if $v eq '' || $v eq 'nolog
';
2861 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2863 return $v if ($v >= 0) && ($v <= 7);
2865 warn "unknown log level ($k = '$v')\n";
2870 sub generate_std_chains {
2871 my ($ruleset, $options, $ipversion) = @_;
2873 my $std_chains = $pve_std_chains->{$ipversion} || die "internal error";
2875 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2879 if ($ipversion == 4) {
2880 # same as shorewall smurflog.
2881 $chain = 'PVEFW-smurflog
';
2882 $std_chains->{$chain} = [];
2884 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2885 push @{$std_chains->{$chain}}, "-j DROP";
2888 # same as shorewall logflags action.
2889 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2890 $chain = 'PVEFW-logflags
';
2891 $std_chains->{$chain} = [];
2893 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2894 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2895 push @{$std_chains->{$chain}}, "-j DROP";
2897 foreach my $chain (keys %$std_chains) {
2898 ruleset_create_chain($ruleset, $chain);
2899 foreach my $rule (@{$std_chains->{$chain}}) {
2901 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule);
2903 ruleset_addrule($ruleset, $chain, $rule);
2909 sub generate_ipset_chains {
2910 my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; #fixme
2912 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2914 my $options = $fw_conf->{ipset}->{$ipset};
2918 foreach my $entry (@$options) {
2919 next if $entry->{errors}; # skip entries with errors
2922 if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
2923 ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
2925 ($cidr, $ver) = parse_ip_or_cidr($entry->{cidr});
2927 #http://backreference.org/2013/03/01/ipv6-address-normalization/
2929 my $ipv6 = inet_pton(AF_INET6, lc($cidr));
2930 $cidr = inet_ntop(AF_INET6, $ipv6);
2936 $nethash->{$ver}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} };
2941 foreach my $ipversion (4, 6) {
2942 my $data = $nethash->{$ipversion};
2944 my $name = compute_ipset_chain_name($fw_conf->{vmid}, $ipset, $ipversion);
2946 my $hashsize = scalar(@$options);
2947 if ($hashsize <= 64) {
2950 $hashsize = round_powerof2($hashsize);
2953 my $family = $ipversion == "6" ? "inet6" : "inet";
2955 $ipset_ruleset->{$name} = ["create $name hash:net family $family hashsize $hashsize maxelem $hashsize"];
2957 foreach my $cidr (sort keys %$data) {
2958 my $entry = $data->{$cidr};
2960 my $cmd = "add $name $cidr";
2961 if ($entry->{nomatch}) {
2962 if ($feature_ipset_nomatch) {
2963 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
2965 warn "ignore !$cidr - nomatch not supported by kernel\n";
2968 push @{$ipset_ruleset->{$name}}, $cmd;
2975 sub round_powerof2 {
2979 $int |= $int >> $_ foreach (1,2,4,8,16);
2983 sub load_clusterfw_conf {
2984 my ($filename, $verbose) = @_;
2986 $filename = $clusterfw_conf_filename if !defined($filename);
2988 my $cluster_conf = {};
2989 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2990 $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose);
2993 return $cluster_conf;
2996 sub save_clusterfw_conf {
2997 my ($cluster_conf) = @_;
3001 my $options = $cluster_conf->{options};
3002 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3004 my $aliases = $cluster_conf->{aliases};
3005 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
3007 $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
3009 my $rules = $cluster_conf->{rules};
3010 if ($rules && scalar(@$rules)) {
3011 $raw .= "[RULES]\n\n";
3012 $raw .= &$format_rules($rules, 1);
3016 if ($cluster_conf->{groups}) {
3017 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
3018 my $rules = $cluster_conf->{groups}->{$group};
3019 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
3020 my $utf8comment = encode('utf8
', $comment);
3021 $raw .= "[group $group] # $utf8comment\n\n";
3023 $raw .= "[group $group]\n\n";
3026 $raw .= &$format_rules($rules, 0);
3031 mkdir $pvefw_conf_dir;
3032 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
3035 sub load_hostfw_conf {
3036 my ($cluster_conf, $filename, $verbose) = @_;
3038 $filename = $hostfw_conf_filename if !defined($filename);
3040 my $hostfw_conf = {};
3041 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3042 $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose);
3044 return $hostfw_conf;
3047 sub save_hostfw_conf {
3048 my ($hostfw_conf) = @_;
3052 my $options = $hostfw_conf->{options};
3053 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3055 my $rules = $hostfw_conf->{rules};
3056 if ($rules && scalar(@$rules)) {
3057 $raw .= "[RULES]\n\n";
3058 $raw .= &$format_rules($rules, 1);
3062 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
3066 my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_;
3070 if ($vmdata) { # test mode
3071 my $testdir = $vmdata->{testdir} || die "no test directory specified";
3072 my $filename = "$testdir/cluster.fw";
3073 $cluster_conf = load_clusterfw_conf($filename, $verbose);
3075 $filename = "$testdir/host.fw";
3076 $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose);
3078 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose);
3079 } else { # normal operation
3080 $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf;
3082 $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf;
3084 $vmdata = read_local_vm_config();
3085 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose);
3088 my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose);
3089 my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose);
3091 return ($ruleset, $ipset_ruleset, $rulesetv6);
3094 sub compile_iptables_filter {
3095 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_;
3097 $cluster_conf->{ipset}->{venet0} = [];
3098 my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0
', $ipversion);
3101 if ($cluster_conf->{aliases}->{local_network}) {
3102 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3105 ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
3107 $cluster_conf->{aliases}->{local_network} = {
3108 name => 'local_network
', cidr => $localnet, ipversion => $localnet_ver };
3111 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
3113 return ({}, {}) if !$cluster_conf->{options}->{enable};
3117 ruleset_create_chain($ruleset, "PVEFW-INPUT");
3118 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
3120 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3122 my $hostfw_options = $hostfw_conf->{options} || {};
3124 # fixme: what log level should we use here?
3125 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
3127 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT");
3130 ruleset_create_chain($ruleset, "PVEFW-VENET-OUT");
3131 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3132 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3134 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
3135 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3137 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN");
3139 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
3140 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT");
3142 ruleset_create_chain($ruleset, "PVEFW-VENET-IN");
3143 ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3145 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3147 generate_std_chains($ruleset, $hostfw_options, $ipversion);
3149 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
3151 my $ipset_ruleset = {};
3153 if ($hostfw_enable) {
3154 eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
3155 warn $@ if $@; # just to be sure - should not happen
3158 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3160 # generate firewall rules for QEMU VMs
3161 foreach my $vmid (keys %{$vmdata->{qemu}}) {
3163 my $conf = $vmdata->{qemu}->{$vmid};
3164 my $vmfw_conf = $vmfw_configs->{$vmid};
3165 return if !$vmfw_conf;
3167 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3169 foreach my $netid (keys %$conf) {
3170 next if $netid !~ m/^net(\d+)$/;
3171 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3172 next if !$net->{firewall};
3173 my $iface = "tap${vmid}i$1";
3175 my $macaddr = $net->{macaddr};
3176 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3177 $vmfw_conf, $vmid, 'IN
', $ipversion);
3178 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3179 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3182 warn $@ if $@; # just to be sure - should not happen
3185 # generate firewall rules for OpenVZ containers
3186 foreach my $vmid (keys %{$vmdata->{openvz}}) {
3188 my $conf = $vmdata->{openvz}->{$vmid};
3190 my $vmfw_conf = $vmfw_configs->{$vmid};
3191 return if !$vmfw_conf;
3193 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3195 if ($vmfw_conf->{options}->{enable}) {
3196 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
3197 my $ip = $conf->{ip_address}->{value};
3202 foreach my $singleip (split(',', $ip)) {
3203 my $singleip_ver = parse_address_list($singleip); # make sure we have a valid $ip list
3204 push @{$cluster_conf->{ipset}->{venet0}}, { cidr => $singleip };
3205 push @ips, $singleip if $singleip_ver == $ipversion;
3209 my $ip_list = join(',', @ips);
3210 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'IN
', $ipversion);
3211 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'OUT
', $ipversion);
3216 if ($conf->{netif} && $conf->{netif}->{value}) {
3217 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
3218 foreach my $netid (keys %$netif) {
3219 my $d = $netif->{$netid};
3220 my $bridge = $d->{bridge};
3221 next if !$bridge || $bridge !~ m/^vmbr\d+(v(\d+))?f$/; # firewall enabled ?
3222 my $macaddr = $d->{mac};
3223 my $iface = $d->{host_ifname};
3224 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3225 $vmfw_conf, $vmid, 'IN
', $ipversion);
3226 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3227 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3231 warn $@ if $@; # just to be sure - should not happen
3234 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
3235 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS");
3238 generate_ipset_chains($ipset_ruleset, undef, $cluster_conf);
3240 return ($ruleset, $ipset_ruleset);
3243 sub get_ruleset_status {
3244 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
3246 my $statushash = {};
3248 foreach my $chain (sort keys %$ruleset) {
3249 my $sig = &$digest_fn($ruleset->{$chain});
3251 $statushash->{$chain}->{sig} = $sig;
3253 my $oldsig = $active_chains->{$chain};
3254 if (!defined($oldsig)) {
3255 $statushash->{$chain}->{action} = 'create
';
3257 if ($oldsig eq $sig) {
3258 $statushash->{$chain}->{action} = 'exists';
3260 $statushash->{$chain}->{action} = 'update
';
3263 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
3264 foreach my $cmd (@{$ruleset->{$chain}}) {
3265 print "\t$cmd\n" if $verbose;
3269 foreach my $chain (sort keys %$active_chains) {
3270 if (!defined($ruleset->{$chain})) {
3271 my $sig = $active_chains->{$chain};
3272 $statushash->{$chain}->{action} = 'delete';
3273 $statushash->{$chain}->{sig} = $sig;
3274 print "delete $chain ($sig)\n" if $verbose;
3281 sub print_sig_rule {
3282 my ($chain, $sig) = @_;
3284 # We just use this to store a SHA1 checksum used to detect changes
3285 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
3288 sub get_ruleset_cmdlist {
3289 my ($ruleset, $verbose, $iptablescmd) = @_;
3291 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
3293 my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
3294 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
3296 # create missing chains first
3297 foreach my $chain (sort keys %$ruleset) {
3298 my $stat = $statushash->{$chain};
3299 die "internal error" if !$stat;
3300 next if $stat->{action} ne 'create
';
3302 $cmdlist .= ":$chain - [0:0]\n";
3305 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3306 my $chain = "PVEFW-$h";
3307 if ($ruleset->{$chain} && !$hooks->{$h}) {
3308 $cmdlist .= "-A $h -j $chain\n";
3312 foreach my $chain (sort keys %$ruleset) {
3313 my $stat = $statushash->{$chain};
3314 die "internal error" if !$stat;
3316 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
3317 $cmdlist .= "-F $chain\n";
3318 foreach my $cmd (@{$ruleset->{$chain}}) {
3319 $cmdlist .= "$cmd\n";
3321 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
3322 } elsif ($stat->{action
} eq 'delete') {
3323 die "internal error"; # this should not happen
3324 } elsif ($stat->{action
} eq 'exists') {
3327 die "internal error - unknown status '$stat->{action}'";
3331 foreach my $chain (keys %$statushash) {
3332 next if $statushash->{$chain}->{action
} ne 'delete';
3333 $cmdlist .= "-F $chain\n";
3335 foreach my $chain (keys %$statushash) {
3336 next if $statushash->{$chain}->{action
} ne 'delete';
3337 next if $chain eq 'PVEFW-INPUT';
3338 next if $chain eq 'PVEFW-OUTPUT';
3339 next if $chain eq 'PVEFW-FORWARD';
3340 $cmdlist .= "-X $chain\n";
3343 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
3345 $cmdlist .= "COMMIT\n";
3347 return wantarray ?
($cmdlist, $changes) : $cmdlist;
3350 sub get_ipset_cmdlist
{
3351 my ($ruleset, $verbose) = @_;
3355 my $delete_cmdlist = "";
3357 my $active_chains = ipset_get_chains
();
3358 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
3360 # remove stale _swap chains
3361 foreach my $chain (keys %$active_chains) {
3362 if ($chain =~ m/^PVEFW-\S+_swap$/) {
3363 $cmdlist .= "destroy $chain\n";
3367 foreach my $chain (keys %$ruleset) {
3368 my $stat = $statushash->{$chain};
3369 die "internal error" if !$stat;
3371 if ($stat->{action
} eq 'create') {
3372 foreach my $cmd (@{$ruleset->{$chain}}) {
3373 $cmdlist .= "$cmd\n";
3378 foreach my $chain (keys %$ruleset) {
3379 my $stat = $statushash->{$chain};
3380 die "internal error" if !$stat;
3382 if ($stat->{action
} eq 'update') {
3383 my $chain_swap = $chain."_swap";
3385 foreach my $cmd (@{$ruleset->{$chain}}) {
3386 $cmd =~ s/$chain/$chain_swap/;
3387 $cmdlist .= "$cmd\n";
3389 $cmdlist .= "swap $chain_swap $chain\n";
3390 $cmdlist .= "flush $chain_swap\n";
3391 $cmdlist .= "destroy $chain_swap\n";
3395 # the remove unused chains
3396 foreach my $chain (keys %$statushash) {
3397 next if $statushash->{$chain}->{action
} ne 'delete';
3399 $delete_cmdlist .= "flush $chain\n";
3400 $delete_cmdlist .= "destroy $chain\n";
3403 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
3405 return ($cmdlist, $delete_cmdlist, $changes);
3409 my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_;
3411 enable_bridge_firewall
();
3413 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
3414 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
3416 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
3417 my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist
($rulesetv6, $verbose, "ip6tables");
3420 if ($ipset_changes) {
3421 print "ipset changes:\n";
3422 print $ipset_create_cmdlist if $ipset_create_cmdlist;
3423 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
3427 print "iptables changes:\n";
3432 print "ip6tables changes:\n";
3437 my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1";
3438 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_create_cmdlist || '');
3440 ipset_restore_cmdlist
($ipset_create_cmdlist);
3442 $tmpfile = "$pve_fw_status_dir/ip4cmdlist";
3443 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlist || '');
3445 iptables_restore_cmdlist
($cmdlist);
3447 $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
3448 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlistv6 || '');
3450 ip6tables_restore_cmdlist
($cmdlistv6);
3452 $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
3453 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_delete_cmdlist || '');
3455 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
3457 # test: re-read status and check if everything is up to date
3458 my $active_chains = iptables_get_chains
();
3459 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
3462 foreach my $chain (sort keys %$ruleset) {
3463 my $stat = $statushash->{$chain};
3464 if ($stat->{action
} ne 'exists') {
3465 warn "unable to update chain '$chain'\n";
3470 my $active_chainsv6 = iptables_get_chains
("ip6tables");
3471 my $statushashv6 = get_ruleset_status
($rulesetv6, $active_chainsv6, \
&iptables_chain_digest
, 0);
3473 foreach my $chain (sort keys %$rulesetv6) {
3474 my $stat = $statushashv6->{$chain};
3475 if ($stat->{action
} ne 'exists') {
3476 warn "unable to update chain '$chain'\n";
3481 die "unable to apply firewall changes\n" if $errors;
3483 update_nf_conntrack_max
($hostfw_conf);
3485 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
3489 sub update_nf_conntrack_max
{
3490 my ($hostfw_conf) = @_;
3492 my $max = 65536; # reasonable default
3494 my $options = $hostfw_conf->{options
} || {};
3496 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
3497 $max = $options->{nf_conntrack_max
};
3498 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
3501 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
3502 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
3504 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
3506 if ($current != $max) {
3507 my $hashsize = int($max/4);
3508 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
3509 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
3513 sub update_nf_conntrack_tcp_timeout_established
{
3514 my ($hostfw_conf) = @_;
3516 my $options = $hostfw_conf->{options
} || {};
3518 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
3520 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
3523 sub remove_pvefw_chains
{
3525 PVE
::Firewall
::remove_pvefw_chains_iptables
("iptables");
3526 PVE
::Firewall
::remove_pvefw_chains_iptables
("ip6tables");
3527 PVE
::Firewall
::remove_pvefw_chains_ipset
();
3531 sub remove_pvefw_chains_iptables
{
3532 my ($iptablescmd) = @_;
3534 my ($chash, $hooks) = iptables_get_chains
($iptablescmd);
3535 my $cmdlist = "*filter\n";
3537 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3539 $cmdlist .= "-D $h -j PVEFW-$h\n";
3543 foreach my $chain (keys %$chash) {
3544 $cmdlist .= "-F $chain\n";
3547 foreach my $chain (keys %$chash) {
3548 $cmdlist .= "-X $chain\n";
3550 $cmdlist .= "COMMIT\n";
3552 if($iptablescmd eq "ip6tables") {
3553 ip6tables_restore_cmdlist
($cmdlist);
3555 iptables_restore_cmdlist
($cmdlist);
3559 sub remove_pvefw_chains_ipset
{
3561 my $ipset_chains = ipset_get_chains
();
3565 foreach my $chain (keys %$ipset_chains) {
3566 $cmdlist .= "flush $chain\n";
3567 $cmdlist .= "destroy $chain\n";
3570 ipset_restore_cmdlist
($cmdlist) if $cmdlist;
3574 my $cluster_conf = load_clusterfw_conf
();
3575 my $cluster_options = $cluster_conf->{options
};
3576 my $enable = $cluster_options->{enable
};
3580 # load required modules here
3586 my $cluster_conf = load_clusterfw_conf
();
3587 my $cluster_options = $cluster_conf->{options
};
3589 if (!$cluster_options->{enable
}) {
3590 PVE
::Firewall
::remove_pvefw_chains
();
3594 my $hostfw_conf = load_hostfw_conf
($cluster_conf);
3596 my ($ruleset, $ipset_ruleset, $rulesetv6) = compile
($cluster_conf, $hostfw_conf);
3598 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6);