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_macro_ipversion = {};
510 my $pve_fw_preferred_macro_names = {};
512 my $pve_std_chains = {};
513 $pve_std_chains->{4} = {
514 'PVEFW-SET-ACCEPT-MARK' => [
515 "-j MARK --set-mark 1",
517 'PVEFW-DropBroadcast' => [
518 # same as shorewall 'Broadcast'
519 # simply DROP BROADCAST/MULTICAST/ANYCAST
520 # we can use this to reduce logging
521 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
522 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
523 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
524 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
527 # same as shorewall 'reject'
528 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
529 { action
=> 'DROP', source
=> '224.0.0.0/4' },
530 { action
=> 'DROP', proto
=> 'icmp' },
531 "-p tcp -j REJECT --reject-with tcp-reset",
532 "-p udp -j REJECT --reject-with icmp-port-unreachable",
533 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
534 "-j REJECT --reject-with icmp-host-prohibited",
537 # same as shorewall 'Drop', which is equal to DROP,
538 # but REJECT/DROP some packages to reduce logging,
539 # and ACCEPT critical ICMP types
540 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
541 # we are not interested in BROADCAST/MULTICAST/ANYCAST
542 { action
=> 'PVEFW-DropBroadcast' },
543 # ACCEPT critical ICMP types
544 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
545 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
546 # Drop packets with INVALID state
547 "-m conntrack --ctstate INVALID -j DROP",
548 # Drop Microsoft SMB noise
549 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
550 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
551 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
552 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
553 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
554 # Drop new/NotSyn traffic so that it doesn't get logged
555 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
557 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
560 # same as shorewall 'Reject', which is equal to Reject,
561 # but REJECT/DROP some packages to reduce logging,
562 # and ACCEPT critical ICMP types
563 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
564 # we are not interested in BROADCAST/MULTICAST/ANYCAST
565 { action
=> 'PVEFW-DropBroadcast' },
566 # ACCEPT critical ICMP types
567 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
568 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
569 # Drop packets with INVALID state
570 "-m conntrack --ctstate INVALID -j DROP",
571 # Drop Microsoft SMB noise
572 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
573 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
574 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
575 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
576 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
577 # Drop new/NotSyn traffic so that it doesn't get logged
578 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
580 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
582 'PVEFW-tcpflags' => [
583 # same as shorewall tcpflags action.
584 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
585 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
586 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
587 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
588 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
589 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
592 # same as shorewall smurfs action
593 # Filter packets for smurfs (packets with a broadcast address as the source).
594 "-s 0.0.0.0/32 -j RETURN",
595 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
596 "-s 224.0.0.0/4 -g PVEFW-smurflog",
600 $pve_std_chains->{6} = {
601 'PVEFW-SET-ACCEPT-MARK' => [
602 "-j MARK --set-mark 1",
604 'PVEFW-DropBroadcast' => [
605 # same as shorewall 'Broadcast'
606 # simply DROP BROADCAST/MULTICAST/ANYCAST
607 # we can use this to reduce logging
608 #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6
609 # ipv6 addrtype does not work with kernel 2.6.32
610 #{ action => 'DROP', dsttype => 'MULTICAST' },
611 #{ action => 'DROP', dsttype => 'ANYCAST' },
612 { action
=> 'DROP', dest
=> 'ff00::/8' },
613 #{ action => 'DROP', dest => '224.0.0.0/4' },
616 # same as shorewall 'reject'
617 #{ action => 'DROP', dsttype => 'BROADCAST' },
618 #{ action => 'DROP', source => '224.0.0.0/4' },
619 { action
=> 'DROP', proto
=> 'icmpv6' },
620 "-p tcp -j REJECT --reject-with tcp-reset",
621 #"-p udp -j REJECT --reject-with icmp-port-unreachable",
622 #"-p icmp -j REJECT --reject-with icmp-host-unreachable",
623 #"-j REJECT --reject-with icmp-host-prohibited",
626 # same as shorewall 'Drop', which is equal to DROP,
627 # but REJECT/DROP some packages to reduce logging,
628 # and ACCEPT critical ICMP types
629 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
630 # we are not interested in BROADCAST/MULTICAST/ANYCAST
631 { action
=> 'PVEFW-DropBroadcast' },
632 # ACCEPT critical ICMP types
633 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
634 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
635 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
637 # Drop packets with INVALID state
638 "-m conntrack --ctstate INVALID -j DROP",
639 # Drop Microsoft SMB noise
640 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
641 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
642 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
643 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
644 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
645 # Drop new/NotSyn traffic so that it doesn't get logged
646 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
648 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
651 # same as shorewall 'Reject', which is equal to Reject,
652 # but REJECT/DROP some packages to reduce logging,
653 # and ACCEPT critical ICMP types
654 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
655 # we are not interested in BROADCAST/MULTICAST/ANYCAST
656 { action
=> 'PVEFW-DropBroadcast' },
657 # ACCEPT critical ICMP types
658 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
659 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
660 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
662 # Drop packets with INVALID state
663 "-m conntrack --ctstate INVALID -j DROP",
664 # Drop Microsoft SMB noise
665 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
666 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
667 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
668 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
669 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
670 # Drop new/NotSyn traffic so that it doesn't get logged
671 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
673 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
675 'PVEFW-tcpflags' => [
676 # same as shorewall tcpflags action.
677 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
678 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
679 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
680 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
681 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
682 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
686 # iptables -p icmp -h
687 my $icmp_type_names = {
690 'destination-unreachable' => 1,
691 'network-unreachable' => 1,
692 'host-unreachable' => 1,
693 'protocol-unreachable' => 1,
694 'port-unreachable' => 1,
695 'fragmentation-needed' => 1,
696 'source-route-failed' => 1,
697 'network-unknown' => 1,
699 'network-prohibited' => 1,
700 'host-prohibited' => 1,
701 'TOS-network-unreachable' => 1,
702 'TOS-host-unreachable' => 1,
703 'communication-prohibited' => 1,
704 'host-precedence-violation' => 1,
705 'precedence-cutoff' => 1,
706 'source-quench' => 1,
708 'network-redirect' => 1,
709 'host-redirect' => 1,
710 'TOS-network-redirect' => 1,
711 'TOS-host-redirect' => 1,
713 'router-advertisement' => 1,
714 'router-solicitation' => 1,
715 'time-exceeded' => 1,
716 'ttl-zero-during-transit' => 1,
717 'ttl-zero-during-reassembly' => 1,
718 'parameter-problem' => 1,
719 'ip-header-bad' => 1,
720 'required-option-missing' => 1,
721 'timestamp-request' => 1,
722 'timestamp-reply' => 1,
723 'address-mask-request' => 1,
724 'address-mask-reply' => 1,
727 # ip6tables -p icmpv6 -h
729 my $icmpv6_type_names = {
731 'destination-unreachable' => 1,
733 'communication-prohibited' => 1,
734 'address-unreachable' => 1,
735 'port-unreachable' => 1,
736 'packet-too-big' => 1,
737 'time-exceeded' => 1,
738 'ttl-zero-during-transit' => 1,
739 'ttl-zero-during-reassembly' => 1,
740 'parameter-problem' => 1,
742 'unknown-header-type' => 1,
743 'unknown-option' => 1,
746 'router-solicitation' => 1,
747 'router-advertisement' => 1,
748 'neighbor-solicitation' => 1,
749 'neighbour-solicitation' => 1,
750 'neighbor-advertisement' => 1,
751 'neighbour-advertisement' => 1,
755 sub init_firewall_macros
{
757 $pve_fw_parsed_macros = {};
760 my ($k, $macro) = @_;
761 my $lc_name = lc($k);
762 $pve_fw_macro_ipversion->{$k} = 0;
763 while (!ref($macro->[0])) {
764 my $desc = shift @$macro;
765 if ($desc eq 'ipv4only') {
766 $pve_fw_macro_ipversion->{$k} = 4;
767 } elsif ($desc eq 'ipv6only') {
768 $pve_fw_macro_ipversion->{$k} = 6;
770 $pve_fw_macro_descr->{$k} = $desc;
773 $pve_fw_preferred_macro_names->{$lc_name} = $k;
774 $pve_fw_parsed_macros->{$k} = $macro;
777 foreach my $k (keys %$pve_fw_macros) {
778 &$parse($k, $pve_fw_macros->{$k});
781 foreach my $k (keys %$pve_ipv6fw_macros) {
782 next if $pve_fw_parsed_macros->{$k};
783 &$parse($k, $pve_ipv6fw_macros->{$k});
784 $pve_fw_macro_ipversion->{$k} = 6;
788 init_firewall_macros
();
791 return wantarray ?
($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
796 sub get_etc_services
{
798 return $etc_services if $etc_services;
800 my $filename = "/etc/services";
802 my $fh = IO
::File-
>new($filename, O_RDONLY
);
804 warn "unable to read '$filename' - $!\n";
810 while (my $line = <$fh>) {
812 next if $line =~m/^#/;
813 next if ($line =~m/^\s*$/);
815 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
816 $services->{byid
}->{$2}->{name
} = $1;
817 $services->{byid
}->{$2}->{port
} = $2;
818 $services->{byid
}->{$2}->{$3} = 1;
819 $services->{byname
}->{$1} = $services->{byid
}->{$2};
825 $etc_services = $services;
828 return $etc_services;
833 sub get_etc_protocols
{
834 return $etc_protocols if $etc_protocols;
836 my $filename = "/etc/protocols";
838 my $fh = IO
::File-
>new($filename, O_RDONLY
);
840 warn "unable to read '$filename' - $!\n";
846 while (my $line = <$fh>) {
848 next if $line =~m/^#/;
849 next if ($line =~m/^\s*$/);
851 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
852 $protocols->{byid
}->{$2}->{name
} = $1;
853 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
859 # add special case for ICMP v6
860 $protocols->{byid
}->{icmpv6
}->{name
} = "icmpv6";
861 $protocols->{byname
}->{icmpv6
} = $protocols->{byid
}->{icmpv6
};
863 $etc_protocols = $protocols;
865 return $etc_protocols;
868 my $ipv4_mask_hash_localnet = {
870 '255.255.128.0' => 17,
871 '255.255.192.0' => 18,
872 '255.255.224.0' => 19,
873 '255.255.240.0' => 20,
874 '255.255.248.0' => 21,
875 '255.255.252.0' => 22,
876 '255.255.254.0' => 23,
877 '255.255.255.0' => 24,
878 '255.255.255.128' => 25,
879 '255.255.255.192' => 26,
880 '255.255.255.224' => 27,
881 '255.255.255.240' => 28,
882 '255.255.255.248' => 29,
883 '255.255.255.252' => 30,
889 my ($new_value) = @_;
891 $__local_network = $new_value if defined($new_value);
893 return $__local_network if defined($__local_network);
896 my $nodename = PVE
::INotify
::nodename
();
898 my $ip = PVE
::Cluster
::remote_node_ip
($nodename);
900 my $testip = Net
::IP-
>new($ip);
902 my $routes = PVE
::ProcFSTools
::read_proc_net_route
();
903 foreach my $entry (@$routes) {
904 my $mask = $ipv4_mask_hash_localnet->{$entry->{mask
}};
905 next if !defined($mask);
906 return if $mask eq '0.0.0.0';
907 my $cidr = "$entry->{dest}/$mask";
908 my $testnet = Net
::IP-
>new($cidr);
909 if ($testnet->overlaps($testip)) {
910 $__local_network = $cidr;
917 return $__local_network;
920 # ipset names are limited to 31 characters,
921 # and we use '-v4' or '-v6' to indicate IP versions,
922 # and we use '_swap' suffix for atomic update,
923 # for example PVEFW-${VMID}-${ipset_name}_swap
925 my $max_iptables_ipset_name_length = 31 - length("PVEFW-") - length("_swap");
927 sub compute_ipset_chain_name
{
928 my ($vmid, $ipset_name, $ipversion) = @_;
930 $vmid = 0 if !defined($vmid);
932 my $id = "$vmid-${ipset_name}-v$ipversion";
934 if (length($id) > $max_iptables_ipset_name_length) {
935 $id = PVE
::Tools
::fnv31a_hex
($id);
941 sub compute_ipfilter_ipset_name
{
944 return "ipfilter-$iface";
947 sub parse_address_list
{
950 if ($str =~ m/^(\+)(\S+)$/) { # ipset ref
951 die "ipset name too long\n" if length($str) > ($max_ipset_name_length + 1);
955 if ($str =~ m/^${ip_alias_pattern}$/) {
956 die "alias name too long\n" if length($str) > $max_alias_name_length;
964 foreach my $elem (split(/,/, $str)) {
966 my $ip = Net
::IP-
>new($elem);
968 my $err = Net
::IP
::Error
();
969 die "invalid IP address: $err\n";
971 $iprange = 1 if $elem =~ m/-/;
973 my $new_ipversion = Net
::IP
::ip_is_ipv6
($ip->ip()) ?
6 : 4;
975 die "detected mixed ipv4/ipv6 addresses in address list '$str'\n"
976 if $ipversion && ($new_ipversion != $ipversion);
978 $ipversion = $new_ipversion;
981 die "you can't use a range in a list\n" if $iprange && $count > 1;
986 sub parse_port_name_number_or_range
{
989 my $services = PVE
::Firewall
::get_etc_services
();
993 foreach my $item (split(/,/, $str)) {
995 if ($item =~ m/^(\d+):(\d+)$/) {
996 my ($port1, $port2) = ($1, $2);
997 die "invalid port '$port1'\n" if $port1 > 65535;
998 die "invalid port '$port2'\n" if $port2 > 65535;
999 } elsif ($item =~ m/^(\d+)$/) {
1001 die "invalid port '$port'\n" if $port > 65535;
1003 if ($icmp_type_names->{$item}) {
1005 } elsif ($icmpv6_type_names->{$item}) {
1008 die "invalid port '$item'\n" if !$services->{byname
}->{$item};
1013 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
1018 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
1019 sub pve_fw_verify_port_spec
{
1022 parse_port_name_number_or_range
($portstr);
1027 PVE
::JSONSchema
::register_format
('pve-fw-addr-spec', \
&pve_fw_verify_addr_spec
);
1028 sub pve_fw_verify_addr_spec
{
1031 parse_address_list
($list);
1036 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
1037 sub pve_fw_verify_protocol_spec
{
1040 my $protocols = get_etc_protocols
();
1042 die "unknown protocol '$proto'\n" if $proto &&
1043 !(defined($protocols->{byname
}->{$proto}) ||
1044 defined($protocols->{byid
}->{$proto}));
1050 # helper function for API
1052 sub copy_opject_with_digest
{
1055 my $sha = Digest
::SHA-
>new('sha1');
1058 foreach my $k (sort keys %$object) {
1059 my $v = $object->{$k};
1060 next if !defined($v);
1062 $sha->add($k, ':', $v, "\n");
1065 my $digest = $sha->hexdigest;
1067 $res->{digest
} = $digest;
1069 return wantarray ?
($res, $digest) : $res;
1072 sub copy_list_with_digest
{
1075 my $sha = Digest
::SHA-
>new('sha1');
1078 foreach my $entry (@$list) {
1080 foreach my $k (sort keys %$entry) {
1081 my $v = $entry->{$k};
1082 next if !defined($v);
1084 # Note: digest ignores refs ($rule->{errors})
1085 $sha->add($k, ':', $v, "\n") if !ref($v); ;
1090 my $digest = $sha->hexdigest;
1092 foreach my $entry (@$res) {
1093 $entry->{digest
} = $digest;
1096 return wantarray ?
($res, $digest) : $res;
1099 my $rule_properties = {
1101 description
=> "Update rule at position <pos>.",
1106 digest
=> get_standard_option
('pve-config-digest'),
1110 enum
=> ['in', 'out', 'group'],
1113 description
=> "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
1116 pattern
=> $security_group_name_pattern,
1125 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
1127 type
=> 'string', format
=> 'pve-fw-addr-spec',
1131 type
=> 'string', format
=> 'pve-fw-addr-spec',
1135 type
=> 'string', format
=> 'pve-fw-protocol-spec',
1143 type
=> 'string', format
=> 'pve-fw-port-spec',
1147 type
=> 'string', format
=> 'pve-fw-port-spec',
1156 sub add_rule_properties
{
1157 my ($properties) = @_;
1159 foreach my $k (keys %$rule_properties) {
1160 my $h = $rule_properties->{$k};
1161 # copy data, so that we can modify later without side effects
1162 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
1168 sub delete_rule_properties
{
1169 my ($rule, $delete_str) = @_;
1171 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
1172 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
1173 if !defined($rule_properties->{$opt});
1174 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
1175 if $opt eq 'type' || $opt eq 'action';
1176 delete $rule->{$opt};
1182 my $apply_macro = sub {
1183 my ($macro_name, $param, $verify, $ipversion) = @_;
1185 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1186 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1188 if ($ipversion && ($ipversion == 6) && $pve_ipv6fw_macros->{$macro_name}) {
1189 $macro_rules = $pve_ipv6fw_macros->{$macro_name};
1192 # skip macros which are specific to another ipversion
1193 return if ($ipversion//0) != ($pve_fw_macro_ipversion->{$macro_name}//0);
1197 foreach my $templ (@$macro_rules) {
1199 my $param_used = {};
1200 foreach my $k (keys %$templ) {
1201 my $v = $templ->{$k};
1202 if ($v eq 'PARAM') {
1204 $param_used->{$k} = 1;
1205 } elsif ($v eq 'DEST') {
1206 $v = $param->{dest
};
1207 $param_used->{dest
} = 1;
1208 } elsif ($v eq 'SOURCE') {
1209 $v = $param->{source
};
1210 $param_used->{source
} = 1;
1214 my $msg = "missing parameter '$k' in macro '$macro_name'";
1215 raise_param_exc
({ macro => $msg }) if $verify;
1220 foreach my $k (keys %$param) {
1221 next if $k eq 'macro';
1222 next if !defined($param->{$k});
1223 next if $param_used->{$k};
1224 if (defined($rule->{$k})) {
1225 if ($rule->{$k} ne $param->{$k}) {
1226 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
1227 raise_param_exc
({ $k => $msg }) if $verify;
1231 $rule->{$k} = $param->{$k};
1234 push @$rules, $rule;
1240 my $rule_env_iface_lookup = {
1249 my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_;
1251 my $allow_groups = $rule_env eq 'group' ?
0 : 1;
1253 my $allow_iface = $rule_env_iface_lookup->{$rule_env};
1254 die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen
1256 my $errors = $rule->{errors
} || {};
1258 my $error_count = 0;
1260 my $add_error = sub {
1261 my ($param, $msg) = @_;
1263 raise_param_exc
({ $param => $msg }) if !$noerr;
1265 $errors->{$param} = $msg if !$errors->{$param};
1269 my $set_ip_version = sub {
1272 die "detected mixed ipv4/ipv6 adresses in rule\n"
1273 if $ipversion && ($vers != $ipversion);
1278 my $check_ipset_or_alias_property = sub {
1279 my ($name, $expected_ipversion) = @_;
1281 if (my $value = $rule->{$name}) {
1282 if ($value =~ m/^\+/) {
1283 if ($value =~ m/^\+(${ipset_name_pattern})$/) {
1284 &$add_error($name, "no such ipset '$1'")
1285 if !($cluster_conf->{ipset
}->{$1} || ($fw_conf && $fw_conf->{ipset
}->{$1}));
1288 &$add_error($name, "invalid ipset name '$value'");
1290 } elsif ($value =~ m/^${ip_alias_pattern}$/){
1291 my $alias = lc($value);
1292 &$add_error($name, "no such alias '$value'")
1293 if !($cluster_conf->{aliases
}->{$alias} || ($fw_conf && $fw_conf->{aliases
}->{$alias}));
1294 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1295 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1297 &$set_ip_version($e->{ipversion
});
1302 my $type = $rule->{type
};
1303 my $action = $rule->{action
};
1305 &$add_error('type', "missing property") if !$type;
1306 &$add_error('action', "missing property") if !$action;
1309 if ($type eq 'in' || $type eq 'out') {
1310 &$add_error('action', "unknown action '$action'")
1311 if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/);
1312 } elsif ($type eq 'group') {
1313 &$add_error('type', "security groups not allowed")
1315 &$add_error('action', "invalid characters in security group name")
1316 if $action && ($action !~ m/^${security_group_name_pattern}$/);
1318 &$add_error('type', "unknown rule type '$type'");
1322 if ($rule->{iface
}) {
1323 &$add_error('type', "parameter -i not allowed for this rule type")
1325 eval { PVE
::JSONSchema
::pve_verify_iface
($rule->{iface
}); };
1326 &$add_error('iface', $@) if $@;
1327 if ($rule_env eq 'vm') {
1328 &$add_error('iface', "value does not match the regex pattern 'net\\d+'")
1329 if $rule->{iface
} !~ m/^net(\d+)$/;
1330 } elsif ($rule_env eq 'ct') {
1331 &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'")
1332 if $rule->{iface
} !~ m/^(venet|eth(\d+))$/;
1336 if ($rule->{macro}) {
1337 if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) {
1338 $rule->{macro} = $preferred_name;
1340 &$add_error('macro', "unknown macro '$rule->{macro}'");
1344 if ($rule->{proto
}) {
1345 eval { pve_fw_verify_protocol_spec
($rule->{proto
}); };
1346 &$add_error('proto', $@) if $@;
1347 &$set_ip_version(4) if $rule->{proto
} eq 'icmp';
1348 &$set_ip_version(6) if $rule->{proto
} eq 'icmpv6';
1351 if ($rule->{dport
}) {
1352 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1353 &$add_error('dport', $@) if $@;
1354 &$add_error('proto', "missing property - 'dport' requires this property")
1358 if ($rule->{sport
}) {
1359 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1360 &$add_error('sport', $@) if $@;
1361 &$add_error('proto', "missing property - 'sport' requires this property")
1365 if ($rule->{source
}) {
1367 my $source_ipversion = parse_address_list
($rule->{source
});
1368 &$set_ip_version($source_ipversion);
1370 &$add_error('source', $@) if $@;
1371 &$check_ipset_or_alias_property('source', $ipversion);
1374 if ($rule->{dest
}) {
1376 my $dest_ipversion = parse_address_list
($rule->{dest
});
1377 &$set_ip_version($dest_ipversion);
1379 &$add_error('dest', $@) if $@;
1380 &$check_ipset_or_alias_property('dest', $ipversion);
1383 $rule->{ipversion
} = $ipversion if $ipversion;
1385 if ($rule->{macro} && !$error_count) {
1386 eval { &$apply_macro($rule->{macro}, $rule, 1, $ipversion); };
1388 if (ref($err) eq "PVE::Exception" && $err->{errors
}) {
1389 my $eh = $err->{errors
};
1390 foreach my $p (keys %$eh) {
1391 &$add_error($p, $eh->{$p});
1394 &$add_error('macro', "$err");
1399 $rule->{errors
} = $errors if $error_count;
1404 sub copy_rule_data
{
1405 my ($rule, $param) = @_;
1407 foreach my $k (keys %$rule_properties) {
1408 if (defined(my $v = $param->{$k})) {
1409 if ($v eq '' || $v eq '-') {
1420 sub rules_modify_permissions
{
1421 my ($rule_env) = @_;
1423 if ($rule_env eq 'host') {
1425 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
1427 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1429 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1431 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1433 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
1440 sub rules_audit_permissions
{
1441 my ($rule_env) = @_;
1443 if ($rule_env eq 'host') {
1445 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
1447 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1449 check
=> ['perm', '/', [ 'Sys.Audit' ]],
1451 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1453 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1461 my $bridge_firewall_enabled = 0;
1463 sub enable_bridge_firewall
{
1465 return if $bridge_firewall_enabled; # only once
1467 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1468 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1470 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1471 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1473 $bridge_firewall_enabled = 1;
1476 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1478 sub iptables_restore_cmdlist
{
1481 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1484 sub ip6tables_restore_cmdlist
{
1487 run_command
("/sbin/ip6tables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1490 sub ipset_restore_cmdlist
{
1493 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist, errmsg
=> "ipset_restore_cmdlist");
1496 sub iptables_get_chains
{
1497 my ($iptablescmd) = @_;
1499 $iptablescmd = "iptables" if !$iptablescmd;
1503 # check what chains we want to track
1504 my $is_pvefw_chain = sub {
1507 return 1 if $name =~ m/^PVEFW-\S+$/;
1509 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
1511 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
1513 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
1515 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/;
1516 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
1528 return if $line =~ m/^#/;
1529 return if $line =~ m/^\s*$/;
1531 if ($line =~ m/^\*(\S+)$/) {
1536 return if $table ne 'filter';
1538 if ($line =~ m/^:(\S+)\s/) {
1540 return if !&$is_pvefw_chain($chain);
1541 $res->{$chain} = "unknown";
1542 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1543 my ($chain, $sig) = ($1, $2);
1544 return if !&$is_pvefw_chain($chain);
1545 $res->{$chain} = $sig;
1546 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1549 # simply ignore the rest
1554 run_command
("/sbin/$iptablescmd-save", outfunc
=> $parser);
1556 return wantarray ?
($res, $hooks) : $res;
1559 sub iptables_chain_digest
{
1561 my $digest = Digest
::SHA-
>new('sha1');
1562 foreach my $rule (@$rules) { # order is important
1563 $digest->add($rule);
1565 return $digest->b64digest;
1568 sub ipset_chain_digest
{
1571 my $digest = Digest
::SHA-
>new('sha1');
1572 foreach my $rule (sort @$rules) { # note: sorted
1573 $digest->add($rule);
1575 return $digest->b64digest;
1578 sub ipset_get_chains
{
1586 return if $line =~ m/^#/;
1587 return if $line =~ m/^\s*$/;
1588 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1590 $line =~ s/\s+$//; # delete trailing white space
1591 push @{$chains->{$chain}}, $line;
1593 # simply ignore the rest
1598 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
1600 # compute digest for each chain
1601 foreach my $chain (keys %$chains) {
1602 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1608 sub ruleset_generate_cmdstr
{
1609 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1611 return if defined($rule->{enable
}) && !$rule->{enable
};
1612 return if $rule->{errors
};
1614 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1616 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1617 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1621 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1622 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1624 my $source = $rule->{source
};
1625 my $dest = $rule->{dest
};
1628 if ($source =~ m/^\+/) {
1629 if ($source =~ m/^\+(${ipset_name_pattern})$/) {
1631 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1632 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1633 push @cmd, "-m set --match-set ${ipset_chain} src";
1634 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1635 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1636 push @cmd, "-m set --match-set ${ipset_chain} src";
1638 die "no such ipset '$name'\n";
1641 die "invalid security group name '$source'\n";
1643 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1644 my $alias = lc($source);
1645 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1646 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1647 die "no such alias '$source'\n" if !$e;
1648 push @cmd, "-s $e->{cidr}";
1649 } elsif ($source =~ m/\-/){
1650 push @cmd, "-m iprange --src-range $source";
1652 push @cmd, "-s $source";
1657 if ($dest =~ m/^\+/) {
1658 if ($dest =~ m/^\+(${ipset_name_pattern})$/) {
1660 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1661 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1662 push @cmd, "-m set --match-set ${ipset_chain} dst";
1663 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1664 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1665 push @cmd, "-m set --match-set ${ipset_chain} dst";
1667 die "no such ipset '$name'\n";
1670 die "invalid security group name '$dest'\n";
1672 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1673 my $alias = lc($dest);
1674 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1675 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1676 die "no such alias '$dest'\n" if !$e;
1677 push @cmd, "-d $e->{cidr}";
1678 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1679 push @cmd, "-m iprange --dst-range $dest";
1681 push @cmd, "-d $dest";
1685 if ($rule->{proto
}) {
1686 push @cmd, "-p $rule->{proto}";
1689 $multiport++ if $nbdport > 1;
1690 $multiport++ if $nbsport > 1;
1692 push @cmd, "--match multiport" if $multiport;
1694 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1695 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1697 if ($rule->{dport
}) {
1698 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1699 # Note: we use dport to store --icmp-type
1700 die "unknown icmp-type '$rule->{dport}'\n"
1701 if $rule->{dport
} !~ /^\d+$/ && !defined($icmp_type_names->{$rule->{dport
}});
1702 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1703 } elsif ($rule->{proto
} && $rule->{proto
} eq 'icmpv6') {
1704 # Note: we use dport to store --icmpv6-type
1705 die "unknown icmpv6-type '$rule->{dport}'\n"
1706 if $rule->{dport
} !~ /^\d+$/ && !defined($icmpv6_type_names->{$rule->{dport
}});
1707 push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}";
1710 if ($multiport == 2) {
1711 push @cmd, "--ports $rule->{dport}";
1713 push @cmd, "--dports $rule->{dport}";
1716 push @cmd, "--dport $rule->{dport}";
1721 if ($rule->{sport
}) {
1723 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1725 push @cmd, "--sport $rule->{sport}";
1728 } elsif ($rule->{dport
} || $rule->{sport
}) {
1729 die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport
};
1730 die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport
};
1733 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1735 if (my $action = $rule->{action
}) {
1736 $action = $actions->{$action} if defined($actions->{$action});
1737 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1738 push @cmd, $goto ?
"-g $action" : "-j $action";
1741 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1744 sub ruleset_generate_rule
{
1745 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1749 if ($rule->{macro}) {
1750 $rules = &$apply_macro($rule->{macro}, $rule, 0, $ipversion);
1755 # update all or nothing
1758 foreach my $tmp (@$rules) {
1759 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) {
1760 push @cmds, $cmdstr;
1764 foreach my $cmdstr (@cmds) {
1765 ruleset_addrule
($ruleset, $chain, $cmdstr);
1769 sub ruleset_generate_rule_insert
{
1770 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto) = @_;
1772 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1774 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $rule, $actions, $goto)) {
1775 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1779 sub ruleset_create_chain
{
1780 my ($ruleset, $chain) = @_;
1782 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1783 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1785 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1787 $ruleset->{$chain} = [];
1790 sub ruleset_chain_exist
{
1791 my ($ruleset, $chain) = @_;
1793 return $ruleset->{$chain} ?
1 : undef;
1796 sub ruleset_addrule
{
1797 my ($ruleset, $chain, $rule) = @_;
1799 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1801 push @{$ruleset->{$chain}}, "-A $chain $rule";
1804 sub ruleset_insertrule
{
1805 my ($ruleset, $chain, $rule) = @_;
1807 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1809 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1812 sub get_log_rule_base
{
1813 my ($chain, $vmid, $msg, $loglevel) = @_;
1815 die "internal error - no log level" if !defined($loglevel);
1817 $vmid = 0 if !defined($vmid);
1819 # Note: we use special format for prefix to pass further
1820 # info to log daemon (VMID, LOGVELEL and CHAIN)
1822 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1825 sub ruleset_addlog {
1826 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1828 return if !defined($loglevel);
1830 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1832 $logrule = "$rule $logrule" if defined($rule);
1834 ruleset_addrule($ruleset, $chain, $logrule);
1837 sub ruleset_add_chain_policy {
1838 my ($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action) = @_;
1840 if ($policy eq 'ACCEPT') {
1842 ruleset_generate_rule($ruleset, $chain, $ipversion, { action => 'ACCEPT' },
1843 { ACCEPT => $accept_action});
1845 } elsif ($policy eq 'DROP') {
1847 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1849 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1851 ruleset_addrule($ruleset, $chain, "-j DROP
");
1852 } elsif ($policy eq 'REJECT') {
1853 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1855 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1857 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1860 die "internal error
: unknown policy
'$policy'";
1864 sub ruleset_chain_add_conn_filters {
1865 my ($ruleset, $chain, $accept) = @_;
1867 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1868 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1871 sub ruleset_chain_add_input_filters {
1872 my ($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel) = @_;
1874 if ($cluster_conf->{ipset}->{blacklist}){
1875 if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist
")) {
1876 ruleset_create_chain($ruleset, "PVEFW-blacklist
");
1877 ruleset_addlog($ruleset, "PVEFW-blacklist
", 0, "DROP
: ", $loglevel) if $loglevel;
1878 ruleset_addrule($ruleset, "PVEFW-blacklist
", "-j DROP
");
1880 my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion);
1881 ruleset_addrule($ruleset, $chain, "-m set
--match-set
${ipset_chain
} src
-j PVEFW-blacklist
");
1884 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1885 if ($ipversion == 4) {
1886 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1890 if ($options->{tcpflags}) {
1891 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1895 sub ruleset_create_vm_chain {
1896 my ($ruleset, $chain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction) = @_;
1898 ruleset_create_chain($ruleset, $chain);
1899 my $accept = generate_nfqueue($options);
1901 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1902 if ($direction eq 'OUT') {
1903 ruleset_generate_rule($ruleset, $chain, $ipversion,
1904 { action => 'PVEFW-SET-ACCEPT-MARK',
1905 proto => 'udp', sport => 68, dport => 67 });
1907 ruleset_generate_rule($ruleset, $chain, $ipversion,
1908 { action => 'ACCEPT',
1909 proto => 'udp', sport => 67, dport => 68 });
1913 if ($direction eq 'OUT') {
1914 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1915 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1917 if ($ipfilter_ipset) {
1918 ruleset_addrule($ruleset, $chain, "-m set
! --match-set
$ipfilter_ipset src
-j DROP
");
1920 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1924 sub ruleset_add_group_rule {
1925 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_;
1927 my $group = $rule->{action};
1928 my $group_chain = "GROUP-
$group-$direction";
1929 if(!ruleset_chain_exist($ruleset, $group_chain)){
1930 generate_group_rules($ruleset, $cluster_conf, $group, $ipversion);
1933 if ($direction eq 'OUT' && $rule->{iface_out}) {
1934 ruleset_addrule($ruleset, $chain, "-o
$rule->{iface_out
} -j
$group_chain");
1935 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1936 ruleset_addrule($ruleset, $chain, "-i
$rule->{iface_in
} -j
$group_chain");
1938 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1941 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$action");
1944 sub ruleset_generate_vm_rules {
1945 my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_;
1947 my $lc_direction = lc($direction);
1949 my $in_accept = generate_nfqueue($options);
1951 foreach my $rule (@$rules) {
1952 next if $rule->{iface} && $rule->{iface} ne $netid;
1953 next if !$rule->{enable} || $rule->{errors};
1954 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
1956 if ($rule->{type} eq 'group') {
1957 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
1958 $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion);
1960 next if $rule->{type} ne $lc_direction;
1962 if ($direction eq 'OUT') {
1963 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1964 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
1965 undef, $cluster_conf, $vmfw_conf);
1967 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1968 { ACCEPT => $in_accept , REJECT => "PVEFW-reject
" },
1969 undef, $cluster_conf, $vmfw_conf);
1977 sub generate_nfqueue {
1980 if ($options->{ips}) {
1981 my $action = "NFQUEUE
";
1982 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1983 if (defined($3) && defined($1)) {
1984 $action .= " --queue-balance
$1:$3";
1985 } elsif (defined($1)) {
1986 $action .= " --queue-num
$1";
1989 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
1996 sub ruleset_generate_vm_ipsrules {
1997 my ($ruleset, $options, $direction, $iface) = @_;
1999 if ($options->{ips} && $direction eq 'IN') {
2000 my $nfqueue = generate_nfqueue($options);
2002 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS
")) {
2003 ruleset_create_chain($ruleset, "PVEFW-IPS
");
2006 ruleset_addrule($ruleset, "PVEFW-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
2010 sub generate_venet_rules_direction {
2011 my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction, $ipversion) = @_;
2013 my $lc_direction = lc($direction);
2015 my $rules = $vmfw_conf->{rules};
2017 my $options = $vmfw_conf->{options};
2018 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
2020 my $chain = "venet0-
$vmid-$direction";
2022 ruleset_create_vm_chain($ruleset, $chain, $ipversion, $options, undef, undef, $direction);
2024 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction, undef, $ipversion);
2029 if ($direction eq 'OUT') {
2030 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2032 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2035 my $accept = generate_nfqueue($options);
2036 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2037 ruleset_add_chain_policy($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2039 if ($direction eq 'OUT') {
2040 ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT
", $ipversion, {
2043 iface_in => 'venet0'});
2045 ruleset_generate_rule($ruleset, "PVEFW-VENET-IN
", $ipversion, {
2048 iface_out => 'venet0'});
2052 sub generate_tap_rules_direction {
2053 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_;
2055 my $lc_direction = lc($direction);
2057 my $rules = $vmfw_conf->{rules};
2059 my $options = $vmfw_conf->{options};
2060 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
2062 my $tapchain = "$iface-$direction";
2064 my $ipfilter_name = compute_ipfilter_ipset_name($netid);
2065 my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
2066 if $vmfw_conf->{ipset}->{$ipfilter_name};
2068 # create chain with mac and ip filter
2069 ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction);
2071 if ($options->{enable}) {
2072 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion);
2074 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
2079 if ($direction eq 'OUT') {
2080 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2082 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2085 my $accept = generate_nfqueue($options);
2086 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2087 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2089 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : 'ACCEPT';
2090 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, 'ACCEPT', $loglevel, $accept_action);
2093 # plug the tap chain to bridge chain
2094 if ($direction eq 'IN') {
2095 ruleset_addrule($ruleset, "PVEFW-FWBR-IN
",
2096 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
2098 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT
",
2099 "-m physdev
--physdev-is-bridged
--physdev-in
$iface -j
$tapchain");
2103 sub enable_host_firewall {
2104 my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
2106 my $options = $hostfw_conf->{options};
2107 my $cluster_options = $cluster_conf->{options};
2108 my $rules = $hostfw_conf->{rules};
2109 my $cluster_rules = $cluster_conf->{rules};
2111 # host inbound firewall
2112 my $chain = "PVEFW-HOST-IN
";
2113 ruleset_create_chain($ruleset, $chain);
2115 my $loglevel = get_option_log_level($options, "log_level_in
");
2117 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
2119 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2120 ruleset_chain_add_input_filters($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel);
2122 # we use RETURN because we need to check also tap rules
2123 my $accept_action = 'RETURN';
2125 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2127 # add host rules first, so that cluster wide rules can be overwritten
2128 foreach my $rule (@$rules, @$cluster_rules) {
2129 next if !$rule->{enable} || $rule->{errors};
2130 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2132 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
2135 if ($rule->{type} eq 'group') {
2136 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
2137 } elsif ($rule->{type} eq 'in') {
2138 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2139 { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2140 undef, $cluster_conf, $hostfw_conf);
2144 delete $rule->{iface_in};
2147 # allow standard traffic for management ipset (includes cluster network)
2148 my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management
", $ipversion);
2149 my $mngmntsrc = "-m set
--match-set
${mngmnt_ipset_chain
} src
";
2150 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2151 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2152 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2153 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
22 -j
$accept_action"); # SSH
2155 my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
2156 my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion};
2159 if ($localnet && ($ipversion == $localnet_ver)) {
2160 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2161 ruleset_addrule($ruleset, $chain, "-s
$localnet -d
$localnet $corosync_rule");
2162 ruleset_addrule($ruleset, $chain, "-s
$localnet -m addrtype
--dst-type MULTICAST
$corosync_rule");
2165 # implement input policy
2166 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
2167 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2169 # host outbound firewall
2170 $chain = "PVEFW-HOST-OUT
";
2171 ruleset_create_chain($ruleset, $chain);
2173 $loglevel = get_option_log_level($options, "log_level_out
");
2175 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
2177 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2179 # we use RETURN because we may want to check other thigs later
2180 $accept_action = 'RETURN';
2182 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2184 # add host rules first, so that cluster wide rules can be overwritten
2185 foreach my $rule (@$rules, @$cluster_rules) {
2186 next if !$rule->{enable} || $rule->{errors};
2187 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2189 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
2191 if ($rule->{type} eq 'group') {
2192 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
2193 } elsif ($rule->{type} eq 'out') {
2194 ruleset_generate_rule($ruleset, $chain, $ipversion,
2195 $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2196 undef, $cluster_conf, $hostfw_conf);
2200 delete $rule->{iface_out};
2203 # allow standard traffic on cluster network
2204 if ($localnet && ($ipversion == $localnet_ver)) {
2205 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2206 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
22 -j
$accept_action"); # SSH
2207 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2208 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2210 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2211 ruleset_addrule($ruleset, $chain, "-d
$localnet $corosync_rule");
2212 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
$corosync_rule");
2215 # implement output policy
2216 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
2217 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2219 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
2220 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
2223 sub generate_group_rules {
2224 my ($ruleset, $cluster_conf, $group, $ipversion) = @_;
2226 my $rules = $cluster_conf->{groups}->{$group};
2229 warn "no such security group
'$group'\n";
2230 $rules = []; # create empty chain
2233 my $chain = "GROUP-
${group
}-IN
";
2235 ruleset_create_chain($ruleset, $chain);
2236 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2238 foreach my $rule (@$rules) {
2239 next if $rule->{type} ne 'in';
2240 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2241 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2242 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
2243 undef, $cluster_conf);
2246 $chain = "GROUP-
${group
}-OUT
";
2248 ruleset_create_chain($ruleset, $chain);
2249 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2251 foreach my $rule (@$rules) {
2252 next if $rule->{type} ne 'out';
2253 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2254 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
2255 # check also other tap rules later
2256 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2257 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" },
2258 undef, $cluster_conf);
2263 my $valid_netdev_names = {};
2264 for (my $i = 0; $i < $MAX_NETS; $i++) {
2265 $valid_netdev_names->{"net
$i"} = 1;
2269 my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_;
2271 my $orig_line = $line;
2275 # we can add single line comments to the end of the rule
2276 if ($line =~ s/#\s*(.*?)\s*$//) {
2277 $rule->{comment} = decode('utf8', $1);
2280 # we can disable a rule when prefixed with '|'
2282 $rule->{enable} = $line =~ s/^\|// ? 0 : 1;
2284 $line =~ s/^(\S+)\s+(\S+)\s*// ||
2285 die "unable to parse rule
: $line\n";
2287 $rule->{type} = lc($1);
2288 $rule->{action} = $2;
2290 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
2291 if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
2292 $rule->{macro} = $1;
2293 $rule->{action} = $2;
2297 while (length($line)) {
2298 if ($line =~ s/^-i (\S+)\s*//) {
2299 $rule->{iface} = $1;
2303 last if $rule->{type} eq 'group';
2305 if ($line =~ s/^-p (\S+)\s*//) {
2306 $rule->{proto} = $1;
2310 if ($line =~ s/^-dport (\S+)\s*//) {
2311 $rule->{dport} = $1;
2315 if ($line =~ s/^-sport (\S+)\s*//) {
2316 $rule->{sport} = $1;
2319 if ($line =~ s/^-source (\S+)\s*//) {
2320 $rule->{source} = $1;
2323 if ($line =~ s/^-dest (\S+)\s*//) {
2331 die "unable to parse rule parameters
: $line\n" if length($line);
2333 $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1);
2334 if ($verbose && $rule->{errors}) {
2335 warn "$prefix - errors
in rule parameters
: $orig_line\n";
2336 foreach my $p (keys %{$rule->{errors}}) {
2337 warn " $p: $rule->{errors
}->{$p}\n";
2344 sub parse_vmfw_option {
2349 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
2351 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
2354 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
2356 $value = $2 ? lc($3) : '';
2357 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2360 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
2364 die "can
't parse option '$line'\n"
2367 return ($opt, $value);
2370 sub parse_hostfw_option {
2375 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2377 if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
2380 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
2382 $value = $2 ? lc($3) : '';
2383 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
2387 die "can't parse option
'$line'\n"
2390 return ($opt, $value);
2393 sub parse_clusterfw_option {
2398 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
2401 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2405 die "can
't parse option '$line'\n"
2408 return ($opt, $value);
2412 my ($clusterfw_conf, $fw_conf, $cidr) = @_;
2414 my $alias = lc($cidr);
2415 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
2416 $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
2418 die "no such alias '$cidr'\n" if !$e;;
2420 return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr};
2423 sub parse_ip_or_cidr {
2428 if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
2431 } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
2435 die "value does not look like a valid IP address or CIDR network\n";
2438 return wantarray ? ($cidr, $ipversion) : $cidr;
2444 # we can add single line comments to the end of the line
2445 my $comment = decode('utf8
', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
2447 if ($line =~ m/^(\S+)\s(\S+)$/) {
2448 my ($name, $cidr) = ($1, $2);
2451 ($cidr, $ipversion) = parse_ip_or_cidr($cidr);
2456 ipversion => $ipversion,
2458 $data->{comment} = $comment if $comment;
2465 sub generic_fw_config_parser {
2466 my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_;
2471 my $res = $empty_conf;
2473 while (defined(my $line = <$fh>)) {
2474 next if $line =~ m/^#/;
2475 next if $line =~ m/^\s*$/;
2479 my $linenr = $fh->input_line_number();
2480 my $prefix = "$filename (line $linenr)";
2482 if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
2483 $section = 'options
';
2487 if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) {
2488 $section = 'aliases
';
2492 if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2493 $section = 'groups
';
2497 die "security group name too long\n" if length($group) > $max_group_name_length;
2498 die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/;
2501 ($section, $group, $comment) = undef;
2502 warn "$prefix: $err";
2506 $res->{$section}->{$group} = [];
2507 $res->{group_comments}->{$group} = decode('utf8
', $comment)
2512 if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) {
2517 if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2522 die "ipset name too long\n" if length($group) > $max_ipset_name_length;
2523 die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
2526 ($section, $group, $comment) = undef;
2527 warn "$prefix: $err";
2531 $res->{$section}->{$group} = [];
2532 $res->{ipset_comments}->{$group} = decode('utf8
', $comment)
2538 warn "$prefix: skip line - no section\n";
2542 if ($section eq 'options
') {
2545 if ($rule_env eq 'cluster
') {
2546 ($opt, $value) = parse_clusterfw_option($line);
2547 } elsif ($rule_env eq 'host
') {
2548 ($opt, $value) = parse_hostfw_option($line);
2550 ($opt, $value) = parse_vmfw_option($line);
2552 $res->{options}->{$opt} = $value;
2554 warn "$prefix: $@" if $@;
2555 } elsif ($section eq 'aliases
') {
2557 my $data = parse_alias($line);
2558 $res->{aliases}->{lc($data->{name})} = $data;
2560 warn "$prefix: $@" if $@;
2561 } elsif ($section eq 'rules
') {
2563 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); };
2565 warn "$prefix: $err";
2568 push @{$res->{$section}}, $rule;
2569 } elsif ($section eq 'groups
') {
2571 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group
', $verbose); };
2573 warn "$prefix: $err";
2576 push @{$res->{$section}->{$group}}, $rule;
2577 } elsif ($section eq 'ipset
') {
2578 # we can add single line comments to the end of the rule
2579 my $comment = decode('utf8
', $1) if $line =~ s/#\s*(.*?)\s*$//;
2581 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2586 if ($nomatch && !$feature_ipset_nomatch) {
2587 $errors->{nomatch} = "nomatch not supported by kernel";
2591 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2592 resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
2594 $cidr = parse_ip_or_cidr($cidr);
2599 $errors->{cidr} = $err;
2602 my $entry = { cidr => $cidr };
2603 $entry->{nomatch} = 1 if $nomatch;
2604 $entry->{comment} = $comment if $comment;
2605 $entry->{errors} = $errors if $errors;
2607 if ($verbose && $errors) {
2608 warn "$prefix - errors in ipset '$group': $line\n";
2609 foreach my $p (keys %{$errors}) {
2610 warn " $p: $errors->{$p}\n";
2614 push @{$res->{$section}->{$group}}, $entry;
2616 warn "$prefix: skip line - unknown section\n";
2624 sub parse_hostfw_config {
2625 my ($filename, $fh, $cluster_conf, $verbose) = @_;
2627 my $empty_conf = { rules => [], options => {}};
2629 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host
');
2632 sub parse_vmfw_config {
2633 my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_;
2640 ipset_comments => {},
2643 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env);
2646 sub parse_clusterfw_config {
2647 my ($filename, $fh, $verbose) = @_;
2657 group_comments => {},
2659 ipset_comments => {},
2662 return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster
');
2666 my ($code, @param) = @_;
2670 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2677 sub read_local_vm_config {
2682 my $vmdata = { openvz => $openvz, qemu => $qemu };
2684 my $vmlist = PVE::Cluster::get_vmlist();
2685 return $vmdata if !$vmlist || !$vmlist->{ids};
2686 my $ids = $vmlist->{ids};
2688 foreach my $vmid (keys %$ids) {
2689 next if !$vmid; # skip VE0
2690 my $d = $ids->{$vmid};
2691 next if !$d->{node} || $d->{node} ne $nodename;
2692 next if !$d->{type};
2693 if ($d->{type} eq 'openvz
') {
2694 if ($have_pve_manager) {
2695 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
2696 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2697 $openvz->{$vmid} = $conf;
2700 } elsif ($d->{type} eq 'qemu
') {
2701 if ($have_qemu_server) {
2702 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2703 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2704 $qemu->{$vmid} = $conf;
2713 sub load_vmfw_conf {
2714 my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_;
2718 $dir = $pvefw_conf_dir if !defined($dir);
2720 my $filename = "$dir/$vmid.fw";
2721 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2722 $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose);
2723 $vmfw_conf->{vmid} = $vmid;
2729 my $format_rules = sub {
2730 my ($rules, $allow_iface) = @_;
2734 foreach my $rule (@$rules) {
2735 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
' || $rule->{type} eq 'group
') {
2736 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2737 $raw .= uc($rule->{type});
2738 if ($rule->{macro}) {
2739 $raw .= " $rule->{macro}($rule->{action})";
2741 $raw .= " " . $rule->{action};
2743 if ($allow_iface && $rule->{iface}) {
2744 $raw .= " -i $rule->{iface}";
2747 if ($rule->{type} ne 'group
') {
2748 $raw .= " -source $rule->{source}" if $rule->{source};
2749 $raw .= " -dest $rule->{dest}" if $rule->{dest};
2750 $raw .= " -p $rule->{proto}" if $rule->{proto};
2751 $raw .= " -dport $rule->{dport}" if $rule->{dport};
2752 $raw .= " -sport $rule->{sport}" if $rule->{sport};
2755 $raw .= " # " . encode('utf8
', $rule->{comment})
2756 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2759 die "unknown rule type '$rule->{type
}'";
2766 my $format_options = sub {
2771 $raw .= "[OPTIONS]\n\n";
2772 foreach my $opt (keys %$options) {
2773 $raw .= "$opt: $options->{$opt}\n";
2780 my $format_aliases = sub {
2785 $raw .= "[ALIASES]\n\n";
2786 foreach my $k (keys %$aliases) {
2787 my $e = $aliases->{$k};
2788 $raw .= "$e->{name} $e->{cidr}";
2789 $raw .= " # " . encode('utf8
', $e->{comment})
2790 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
2798 my $format_ipsets = sub {
2803 foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
2804 if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) {
2805 my $utf8comment = encode('utf8
', $comment);
2806 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2808 $raw .= "[IPSET $ipset]\n\n";
2810 my $options = $fw_conf->{ipset}->{$ipset};
2813 foreach my $entry (@$options) {
2814 $nethash->{$entry->{cidr}} = $entry;
2817 foreach my $cidr (sort keys %$nethash) {
2818 my $entry = $nethash->{$cidr};
2819 my $line = $entry->{nomatch} ? '!' : '';
2820 $line .= $entry->{cidr};
2821 $line .= " # " . encode('utf8
', $entry->{comment})
2822 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
2832 sub save_vmfw_conf {
2833 my ($vmid, $vmfw_conf) = @_;
2837 my $options = $vmfw_conf->{options};
2838 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2840 my $aliases = $vmfw_conf->{aliases};
2841 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2843 $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset};
2845 my $rules = $vmfw_conf->{rules} || [];
2846 if ($rules && scalar(@$rules)) {
2847 $raw .= "[RULES]\n\n";
2848 $raw .= &$format_rules($rules, 1);
2852 mkdir $pvefw_conf_dir;
2854 my $filename = "$pvefw_conf_dir/$vmid.fw";
2855 PVE::Tools::file_set_contents($filename, $raw);
2858 sub read_vm_firewall_configs {
2859 my ($cluster_conf, $vmdata, $dir, $verbose) = @_;
2861 my $vmfw_configs = {};
2863 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2864 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm
', $vmid, $dir, $verbose);
2865 next if !$vmfw_conf->{options}; # skip if file does not exists
2866 $vmfw_configs->{$vmid} = $vmfw_conf;
2868 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2869 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct
', $vmid, $dir, $verbose);
2870 next if !$vmfw_conf->{options}; # skip if file does not exists
2871 $vmfw_configs->{$vmid} = $vmfw_conf;
2874 return $vmfw_configs;
2877 sub get_option_log_level {
2878 my ($options, $k) = @_;
2880 my $v = $options->{$k};
2881 $v = $default_log_level if !defined($v);
2883 return undef if $v eq '' || $v eq 'nolog
';
2885 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2887 return $v if ($v >= 0) && ($v <= 7);
2889 warn "unknown log level ($k = '$v')\n";
2894 sub generate_std_chains {
2895 my ($ruleset, $options, $ipversion) = @_;
2897 my $std_chains = $pve_std_chains->{$ipversion} || die "internal error";
2899 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2903 if ($ipversion == 4) {
2904 # same as shorewall smurflog.
2905 $chain = 'PVEFW-smurflog
';
2906 $std_chains->{$chain} = [];
2908 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2909 push @{$std_chains->{$chain}}, "-j DROP";
2912 # same as shorewall logflags action.
2913 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2914 $chain = 'PVEFW-logflags
';
2915 $std_chains->{$chain} = [];
2917 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2918 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2919 push @{$std_chains->{$chain}}, "-j DROP";
2921 foreach my $chain (keys %$std_chains) {
2922 ruleset_create_chain($ruleset, $chain);
2923 foreach my $rule (@{$std_chains->{$chain}}) {
2925 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule);
2927 ruleset_addrule($ruleset, $chain, $rule);
2933 sub generate_ipset_chains {
2934 my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; #fixme
2936 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2938 my $options = $fw_conf->{ipset}->{$ipset};
2942 foreach my $entry (@$options) {
2943 next if $entry->{errors}; # skip entries with errors
2946 if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
2947 ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
2949 ($cidr, $ver) = parse_ip_or_cidr($entry->{cidr});
2951 #http://backreference.org/2013/03/01/ipv6-address-normalization/
2953 my $ipv6 = inet_pton(AF_INET6, lc($cidr));
2954 $cidr = inet_ntop(AF_INET6, $ipv6);
2960 $nethash->{$ver}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} };
2965 foreach my $ipversion (4, 6) {
2966 my $data = $nethash->{$ipversion};
2968 my $name = compute_ipset_chain_name($fw_conf->{vmid}, $ipset, $ipversion);
2970 my $hashsize = scalar(@$options);
2971 if ($hashsize <= 64) {
2974 $hashsize = round_powerof2($hashsize);
2977 my $family = $ipversion == "6" ? "inet6" : "inet";
2979 $ipset_ruleset->{$name} = ["create $name hash:net family $family hashsize $hashsize maxelem $hashsize"];
2981 foreach my $cidr (sort keys %$data) {
2982 my $entry = $data->{$cidr};
2984 my $cmd = "add $name $cidr";
2985 if ($entry->{nomatch}) {
2986 if ($feature_ipset_nomatch) {
2987 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
2989 warn "ignore !$cidr - nomatch not supported by kernel\n";
2992 push @{$ipset_ruleset->{$name}}, $cmd;
2999 sub round_powerof2 {
3003 $int |= $int >> $_ foreach (1,2,4,8,16);
3007 sub load_clusterfw_conf {
3008 my ($filename, $verbose) = @_;
3010 $filename = $clusterfw_conf_filename if !defined($filename);
3012 my $cluster_conf = {};
3013 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3014 $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose);
3017 return $cluster_conf;
3020 sub save_clusterfw_conf {
3021 my ($cluster_conf) = @_;
3025 my $options = $cluster_conf->{options};
3026 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3028 my $aliases = $cluster_conf->{aliases};
3029 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
3031 $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
3033 my $rules = $cluster_conf->{rules};
3034 if ($rules && scalar(@$rules)) {
3035 $raw .= "[RULES]\n\n";
3036 $raw .= &$format_rules($rules, 1);
3040 if ($cluster_conf->{groups}) {
3041 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
3042 my $rules = $cluster_conf->{groups}->{$group};
3043 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
3044 my $utf8comment = encode('utf8
', $comment);
3045 $raw .= "[group $group] # $utf8comment\n\n";
3047 $raw .= "[group $group]\n\n";
3050 $raw .= &$format_rules($rules, 0);
3055 mkdir $pvefw_conf_dir;
3056 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
3059 sub load_hostfw_conf {
3060 my ($cluster_conf, $filename, $verbose) = @_;
3062 $filename = $hostfw_conf_filename if !defined($filename);
3064 my $hostfw_conf = {};
3065 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3066 $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose);
3068 return $hostfw_conf;
3071 sub save_hostfw_conf {
3072 my ($hostfw_conf) = @_;
3076 my $options = $hostfw_conf->{options};
3077 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3079 my $rules = $hostfw_conf->{rules};
3080 if ($rules && scalar(@$rules)) {
3081 $raw .= "[RULES]\n\n";
3082 $raw .= &$format_rules($rules, 1);
3086 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
3090 my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_;
3094 if ($vmdata) { # test mode
3095 my $testdir = $vmdata->{testdir} || die "no test directory specified";
3096 my $filename = "$testdir/cluster.fw";
3097 $cluster_conf = load_clusterfw_conf($filename, $verbose);
3099 $filename = "$testdir/host.fw";
3100 $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose);
3102 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose);
3103 } else { # normal operation
3104 $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf;
3106 $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf;
3108 $vmdata = read_local_vm_config();
3109 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose);
3112 my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose);
3113 my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose);
3115 return ($ruleset, $ipset_ruleset, $rulesetv6);
3118 sub compile_iptables_filter {
3119 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_;
3121 $cluster_conf->{ipset}->{venet0} = [];
3122 my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0
', $ipversion);
3125 if ($cluster_conf->{aliases}->{local_network}) {
3126 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3129 ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
3131 $cluster_conf->{aliases}->{local_network} = {
3132 name => 'local_network
', cidr => $localnet, ipversion => $localnet_ver };
3135 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
3137 return ({}, {}) if !$cluster_conf->{options}->{enable};
3141 ruleset_create_chain($ruleset, "PVEFW-INPUT");
3142 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
3144 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3146 my $hostfw_options = $hostfw_conf->{options} || {};
3148 # fixme: what log level should we use here?
3149 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
3151 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT");
3154 ruleset_create_chain($ruleset, "PVEFW-VENET-OUT");
3155 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3156 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT");
3158 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
3159 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3161 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN");
3163 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
3164 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT");
3166 ruleset_create_chain($ruleset, "PVEFW-VENET-IN");
3167 ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3169 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3171 generate_std_chains($ruleset, $hostfw_options, $ipversion);
3173 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
3175 my $ipset_ruleset = {};
3177 if ($hostfw_enable) {
3178 eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
3179 warn $@ if $@; # just to be sure - should not happen
3182 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN");
3184 # generate firewall rules for QEMU VMs
3185 foreach my $vmid (keys %{$vmdata->{qemu}}) {
3187 my $conf = $vmdata->{qemu}->{$vmid};
3188 my $vmfw_conf = $vmfw_configs->{$vmid};
3189 return if !$vmfw_conf;
3191 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3193 foreach my $netid (keys %$conf) {
3194 next if $netid !~ m/^net(\d+)$/;
3195 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3196 next if !$net->{firewall};
3197 my $iface = "tap${vmid}i$1";
3199 my $macaddr = $net->{macaddr};
3200 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3201 $vmfw_conf, $vmid, 'IN
', $ipversion);
3202 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3203 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3206 warn $@ if $@; # just to be sure - should not happen
3209 # generate firewall rules for OpenVZ containers
3210 foreach my $vmid (keys %{$vmdata->{openvz}}) {
3212 my $conf = $vmdata->{openvz}->{$vmid};
3214 my $vmfw_conf = $vmfw_configs->{$vmid};
3215 return if !$vmfw_conf;
3217 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3219 if ($vmfw_conf->{options}->{enable}) {
3220 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
3221 my $ip = $conf->{ip_address}->{value};
3226 foreach my $singleip (split(',', $ip)) {
3227 my $singleip_ver = parse_address_list($singleip); # make sure we have a valid $ip list
3228 push @{$cluster_conf->{ipset}->{venet0}}, { cidr => $singleip };
3229 push @ips, $singleip if $singleip_ver == $ipversion;
3233 my $ip_list = join(',', @ips);
3234 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'IN
', $ipversion);
3235 generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'OUT
', $ipversion);
3240 if ($conf->{netif} && $conf->{netif}->{value}) {
3241 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
3242 foreach my $netid (keys %$netif) {
3243 my $d = $netif->{$netid};
3244 my $bridge = $d->{bridge};
3245 next if !$bridge || $bridge !~ m/^vmbr\d+(v(\d+))?f$/; # firewall enabled ?
3246 my $macaddr = $d->{mac};
3247 my $iface = $d->{host_ifname};
3248 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3249 $vmfw_conf, $vmid, 'IN
', $ipversion);
3250 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3251 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3255 warn $@ if $@; # just to be sure - should not happen
3258 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
3259 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS");
3262 generate_ipset_chains($ipset_ruleset, undef, $cluster_conf);
3264 return ($ruleset, $ipset_ruleset);
3267 sub get_ruleset_status {
3268 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
3270 my $statushash = {};
3272 foreach my $chain (sort keys %$ruleset) {
3273 my $sig = &$digest_fn($ruleset->{$chain});
3275 $statushash->{$chain}->{sig} = $sig;
3277 my $oldsig = $active_chains->{$chain};
3278 if (!defined($oldsig)) {
3279 $statushash->{$chain}->{action} = 'create
';
3281 if ($oldsig eq $sig) {
3282 $statushash->{$chain}->{action} = 'exists';
3284 $statushash->{$chain}->{action} = 'update
';
3287 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
3288 foreach my $cmd (@{$ruleset->{$chain}}) {
3289 print "\t$cmd\n" if $verbose;
3293 foreach my $chain (sort keys %$active_chains) {
3294 if (!defined($ruleset->{$chain})) {
3295 my $sig = $active_chains->{$chain};
3296 $statushash->{$chain}->{action} = 'delete';
3297 $statushash->{$chain}->{sig} = $sig;
3298 print "delete $chain ($sig)\n" if $verbose;
3305 sub print_sig_rule {
3306 my ($chain, $sig) = @_;
3308 # We just use this to store a SHA1 checksum used to detect changes
3309 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
3312 sub get_ruleset_cmdlist {
3313 my ($ruleset, $verbose, $iptablescmd) = @_;
3315 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
3317 my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
3318 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
3320 # create missing chains first
3321 foreach my $chain (sort keys %$ruleset) {
3322 my $stat = $statushash->{$chain};
3323 die "internal error" if !$stat;
3324 next if $stat->{action} ne 'create
';
3326 $cmdlist .= ":$chain - [0:0]\n";
3329 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3330 my $chain = "PVEFW-$h";
3331 if ($ruleset->{$chain} && !$hooks->{$h}) {
3332 $cmdlist .= "-A $h -j $chain\n";
3336 foreach my $chain (sort keys %$ruleset) {
3337 my $stat = $statushash->{$chain};
3338 die "internal error" if !$stat;
3340 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
3341 $cmdlist .= "-F $chain\n";
3342 foreach my $cmd (@{$ruleset->{$chain}}) {
3343 $cmdlist .= "$cmd\n";
3345 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
3346 } elsif ($stat->{action
} eq 'delete') {
3347 die "internal error"; # this should not happen
3348 } elsif ($stat->{action
} eq 'exists') {
3351 die "internal error - unknown status '$stat->{action}'";
3355 foreach my $chain (keys %$statushash) {
3356 next if $statushash->{$chain}->{action
} ne 'delete';
3357 $cmdlist .= "-F $chain\n";
3359 foreach my $chain (keys %$statushash) {
3360 next if $statushash->{$chain}->{action
} ne 'delete';
3361 next if $chain eq 'PVEFW-INPUT';
3362 next if $chain eq 'PVEFW-OUTPUT';
3363 next if $chain eq 'PVEFW-FORWARD';
3364 $cmdlist .= "-X $chain\n";
3367 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
3369 $cmdlist .= "COMMIT\n";
3371 return wantarray ?
($cmdlist, $changes) : $cmdlist;
3374 sub get_ipset_cmdlist
{
3375 my ($ruleset, $verbose) = @_;
3379 my $delete_cmdlist = "";
3381 my $active_chains = ipset_get_chains
();
3382 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
3384 # remove stale _swap chains
3385 foreach my $chain (keys %$active_chains) {
3386 if ($chain =~ m/^PVEFW-\S+_swap$/) {
3387 $cmdlist .= "destroy $chain\n";
3391 foreach my $chain (keys %$ruleset) {
3392 my $stat = $statushash->{$chain};
3393 die "internal error" if !$stat;
3395 if ($stat->{action
} eq 'create') {
3396 foreach my $cmd (@{$ruleset->{$chain}}) {
3397 $cmdlist .= "$cmd\n";
3402 foreach my $chain (keys %$ruleset) {
3403 my $stat = $statushash->{$chain};
3404 die "internal error" if !$stat;
3406 if ($stat->{action
} eq 'update') {
3407 my $chain_swap = $chain."_swap";
3409 foreach my $cmd (@{$ruleset->{$chain}}) {
3410 $cmd =~ s/$chain/$chain_swap/;
3411 $cmdlist .= "$cmd\n";
3413 $cmdlist .= "swap $chain_swap $chain\n";
3414 $cmdlist .= "flush $chain_swap\n";
3415 $cmdlist .= "destroy $chain_swap\n";
3419 # the remove unused chains
3420 foreach my $chain (keys %$statushash) {
3421 next if $statushash->{$chain}->{action
} ne 'delete';
3423 $delete_cmdlist .= "flush $chain\n";
3424 $delete_cmdlist .= "destroy $chain\n";
3427 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
3429 return ($cmdlist, $delete_cmdlist, $changes);
3433 my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_;
3435 enable_bridge_firewall
();
3437 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
3438 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
3440 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
3441 my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist
($rulesetv6, $verbose, "ip6tables");
3444 if ($ipset_changes) {
3445 print "ipset changes:\n";
3446 print $ipset_create_cmdlist if $ipset_create_cmdlist;
3447 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
3451 print "iptables changes:\n";
3456 print "ip6tables changes:\n";
3461 my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1";
3462 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_create_cmdlist || '');
3464 ipset_restore_cmdlist
($ipset_create_cmdlist);
3466 $tmpfile = "$pve_fw_status_dir/ip4cmdlist";
3467 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlist || '');
3469 iptables_restore_cmdlist
($cmdlist);
3471 $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
3472 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlistv6 || '');
3474 ip6tables_restore_cmdlist
($cmdlistv6);
3476 $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
3477 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_delete_cmdlist || '');
3479 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
3481 # test: re-read status and check if everything is up to date
3482 my $active_chains = iptables_get_chains
();
3483 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
3486 foreach my $chain (sort keys %$ruleset) {
3487 my $stat = $statushash->{$chain};
3488 if ($stat->{action
} ne 'exists') {
3489 warn "unable to update chain '$chain'\n";
3494 my $active_chainsv6 = iptables_get_chains
("ip6tables");
3495 my $statushashv6 = get_ruleset_status
($rulesetv6, $active_chainsv6, \
&iptables_chain_digest
, 0);
3497 foreach my $chain (sort keys %$rulesetv6) {
3498 my $stat = $statushashv6->{$chain};
3499 if ($stat->{action
} ne 'exists') {
3500 warn "unable to update chain '$chain'\n";
3505 die "unable to apply firewall changes\n" if $errors;
3507 update_nf_conntrack_max
($hostfw_conf);
3509 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
3513 sub update_nf_conntrack_max
{
3514 my ($hostfw_conf) = @_;
3516 my $max = 65536; # reasonable default
3518 my $options = $hostfw_conf->{options
} || {};
3520 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
3521 $max = $options->{nf_conntrack_max
};
3522 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
3525 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
3526 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
3528 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
3530 if ($current != $max) {
3531 my $hashsize = int($max/4);
3532 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
3533 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
3537 sub update_nf_conntrack_tcp_timeout_established
{
3538 my ($hostfw_conf) = @_;
3540 my $options = $hostfw_conf->{options
} || {};
3542 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
3544 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
3547 sub remove_pvefw_chains
{
3549 PVE
::Firewall
::remove_pvefw_chains_iptables
("iptables");
3550 PVE
::Firewall
::remove_pvefw_chains_iptables
("ip6tables");
3551 PVE
::Firewall
::remove_pvefw_chains_ipset
();
3555 sub remove_pvefw_chains_iptables
{
3556 my ($iptablescmd) = @_;
3558 my ($chash, $hooks) = iptables_get_chains
($iptablescmd);
3559 my $cmdlist = "*filter\n";
3561 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3563 $cmdlist .= "-D $h -j PVEFW-$h\n";
3567 foreach my $chain (keys %$chash) {
3568 $cmdlist .= "-F $chain\n";
3571 foreach my $chain (keys %$chash) {
3572 $cmdlist .= "-X $chain\n";
3574 $cmdlist .= "COMMIT\n";
3576 if($iptablescmd eq "ip6tables") {
3577 ip6tables_restore_cmdlist
($cmdlist);
3579 iptables_restore_cmdlist
($cmdlist);
3583 sub remove_pvefw_chains_ipset
{
3585 my $ipset_chains = ipset_get_chains
();
3589 foreach my $chain (keys %$ipset_chains) {
3590 $cmdlist .= "flush $chain\n";
3591 $cmdlist .= "destroy $chain\n";
3594 ipset_restore_cmdlist
($cmdlist) if $cmdlist;
3598 my $cluster_conf = load_clusterfw_conf
();
3599 my $cluster_options = $cluster_conf->{options
};
3600 my $enable = $cluster_options->{enable
};
3604 # load required modules here
3610 my $cluster_conf = load_clusterfw_conf
();
3611 my $cluster_options = $cluster_conf->{options
};
3613 if (!$cluster_options->{enable
}) {
3614 PVE
::Firewall
::remove_pvefw_chains
();
3618 my $hostfw_conf = load_hostfw_conf
($cluster_conf);
3620 my ($ruleset, $ipset_ruleset, $rulesetv6) = compile
($cluster_conf, $hostfw_conf);
3622 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6);