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::LXC
27 # to avoid dependency problems
30 require PVE
::QemuServer
;
31 $have_qemu_server = 1;
41 my $pve_fw_status_dir = "/var/lib/pve-firewall";
43 mkdir $pve_fw_status_dir; # make sure this exists
45 my $security_group_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
46 my $ipset_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
47 our $ip_alias_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
49 my $max_alias_name_length = 64;
50 my $max_ipset_name_length = 64;
51 my $max_group_name_length = 20;
53 PVE
::JSONSchema
::register_format
('IPorCIDR', \
&pve_verify_ip_or_cidr
);
54 sub pve_verify_ip_or_cidr
{
55 my ($cidr, $noerr) = @_;
57 if ($cidr =~ m!^(?:$IPV6RE|$IPV4RE)(/(\d+))?$!) {
58 return $cidr if Net
::IP-
>new($cidr);
59 return undef if $noerr;
60 die Net
::IP
::Error
() . "\n";
62 return undef if $noerr;
63 die "value does not look like a valid IP address or CIDR network\n";
66 PVE
::JSONSchema
::register_format
('IPorCIDRorAlias', \
&pve_verify_ip_or_cidr_or_alias
);
67 sub pve_verify_ip_or_cidr_or_alias
{
68 my ($cidr, $noerr) = @_;
70 return if $cidr =~ m/^(?:$ip_alias_pattern)$/;
72 return pve_verify_ip_or_cidr
($cidr, $noerr);
75 PVE
::JSONSchema
::register_standard_option
('ipset-name', {
76 description
=> "IP set name.",
78 pattern
=> $ipset_name_pattern,
80 maxLength
=> $max_ipset_name_length,
83 PVE
::JSONSchema
::register_standard_option
('pve-fw-alias', {
84 description
=> "Alias name.",
86 pattern
=> $ip_alias_pattern,
88 maxLength
=> $max_alias_name_length,
91 PVE
::JSONSchema
::register_standard_option
('pve-fw-loglevel' => {
92 description
=> "Log level.",
94 enum
=> ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'nolog'],
98 PVE
::JSONSchema
::register_standard_option
('pve-security-group-name', {
99 description
=> "Security Group name.",
101 pattern
=> $security_group_name_pattern,
103 maxLength
=> $max_group_name_length,
106 my $feature_ipset_nomatch = 0;
108 my (undef, undef, $release) = POSIX
::uname
();
109 if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) {
110 my ($major, $minor) = ($1, $2);
111 $feature_ipset_nomatch = 1 if ($major > 3) ||
112 ($major == 3 && $minor >= 7);
117 my $nodename = PVE
::INotify
::nodename
();
119 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
121 my $default_log_level = 'nolog'; # avoid logs by default
123 my $log_level_hash = {
134 # we need to overwrite some macros for ipv6
135 my $pve_ipv6fw_macros = {
137 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'echo-request' },
139 'NeighborDiscovery' => [
140 "IPv6 neighbor solicitation, neighbor and router advertisement",
141 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'router-advertisement' },
142 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'neighbor-solicitation' },
143 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'neighbor-advertisement' },
146 { action
=> 'PARAM', proto
=> 'udp', dport
=> '546:547', sport
=> '546:547' },
149 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
150 { action
=> 'PARAM', proto
=> 'icmpv6', dport
=> 'echo-request' },
154 # imported/converted from: /usr/share/shorewall/macro.*
155 my $pve_fw_macros = {
158 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
159 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
162 "Auth (identd) traffic",
163 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
166 "Border Gateway Protocol traffic",
167 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
170 "BitTorrent traffic for BitTorrent 3.1 and earlier",
171 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
172 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
175 "BitTorrent traffic for BitTorrent 3.2 and later",
176 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
177 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
180 "Ceph Storage Cluster traffic (Ceph Monitors, OSD & MDS Deamons)",
181 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6789' },
182 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6800:7300' },
185 "Concurrent Versions System pserver traffic",
186 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
189 "Citrix/ICA traffic (ICA, ICA Browser, CGP)",
190 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
191 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
192 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
195 "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)",
196 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
197 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
200 "Distributed Checksum Clearinghouse spam filtering mechanism",
201 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
204 "Forwarded DHCP traffic",
205 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
208 "Domain Name System traffic (upd and tcp)",
209 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
210 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
213 "Distributed Compiler service",
214 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
217 "File Transfer Protocol",
218 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
221 "Finger protocol (RFC 742)",
222 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
225 "GNUnet secure peer-to-peer networking traffic",
226 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
227 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
228 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
229 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
232 "Generic Routing Encapsulation tunneling protocol",
233 { action
=> 'PARAM', proto
=> '47' },
236 "Git distributed revision control traffic",
237 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
240 "OpenPGP HTTP keyserver protocol traffic",
241 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
244 "Hypertext Transfer Protocol (WWW)",
245 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
248 "Hypertext Transfer Protocol (WWW) over SSL",
249 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
252 "Internet Cache Protocol V2 (Squid) traffic",
253 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
256 "AOL Instant Messenger traffic",
257 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
260 "Internet Message Access Protocol",
261 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
264 "Internet Message Access Protocol over SSL",
265 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
268 "IPIP capsulation traffic",
269 { action
=> 'PARAM', proto
=> '94' },
273 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
274 { action
=> 'PARAM', proto
=> '50' },
277 "IPsec authentication (AH) traffic",
278 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
279 { action
=> 'PARAM', proto
=> '51' },
282 "IPsec traffic and Nat-Traversal",
283 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
284 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
285 { action
=> 'PARAM', proto
=> '50' },
288 "Internet Relay Chat traffic",
289 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
292 "HP Jetdirect printing",
293 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
296 "Layer 2 Tunneling Protocol traffic",
297 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
300 "Lightweight Directory Access Protocol traffic",
301 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
304 "Secure Lightweight Directory Access Protocol traffic",
305 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
308 "Microsoft Notification Protocol",
309 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
312 "Microsoft SQL Server",
313 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
316 "Mail traffic (SMTP, SMTPS, Submission)",
317 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
318 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
319 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
322 "Munin networked resource monitoring traffic",
323 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
327 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
330 "NNTP traffic (Usenet).",
331 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
334 "Encrypted NNTP traffic (Usenet)",
335 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
338 "Network Time Protocol (ntpd)",
339 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
342 "OSPF multicast traffic",
343 { action
=> 'PARAM', proto
=> '89' },
347 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
350 "Symantec PCAnywere (tm)",
351 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
352 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
356 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
359 "Encrypted POP3 traffic",
360 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
363 "Point-to-Point Tunneling Protocol",
364 { action
=> 'PARAM', proto
=> '47' },
365 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
369 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
373 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
376 "Line Printer protocol printing",
377 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
380 "Microsoft Remote Desktop Protocol traffic",
381 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
384 "Routing Information Protocol (bidirectional)",
385 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
388 "BIND remote management protocol",
389 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
392 "Razor Antispam System",
393 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
396 "Remote time retrieval (rdate)",
397 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
401 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
404 "SANE network scanning",
405 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
408 "Microsoft SMB traffic",
409 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
410 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
411 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
412 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
415 "Samba Web Administration Tool",
416 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
419 "Simple Mail Transfer Protocol",
420 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
423 "Encrypted Simple Mail Transfer Protocol",
424 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
427 "Simple Network Management Protocol",
428 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
429 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
432 "Spam Assassin SPAMD traffic",
433 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
436 "Secure shell traffic",
437 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
440 "Subversion server (svnserve)",
441 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
444 "SixXS IPv6 Deployment and Tunnel Broker",
445 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
446 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
447 { action
=> 'PARAM', proto
=> '41' },
448 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
451 "Squid web proxy traffic",
452 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
455 "Mail message submission traffic",
456 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
459 "Syslog protocol (RFC 5424) traffic",
460 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
461 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
464 "Trivial File Transfer Protocol traffic",
465 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
469 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
473 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
476 "RFC 868 Time protocol",
477 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
480 "Traceroute (for up to 30 hops) traffic",
481 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
482 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
485 "VNC traffic for VNC display's 0 - 99",
486 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5999' },
489 "VNC traffic from Vncservers to Vncviewers in listen mode",
490 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
493 "WWW traffic (HTTP and HTTPS)",
494 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
495 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
498 "Web Cache/Proxy traffic (port 8080)",
499 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
503 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
506 "Whois (nicname, RFC 3912) traffic",
507 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
511 my $pve_fw_parsed_macros;
512 my $pve_fw_macro_descr;
513 my $pve_fw_macro_ipversion = {};
514 my $pve_fw_preferred_macro_names = {};
516 my $pve_std_chains = {};
517 $pve_std_chains->{4} = {
518 'PVEFW-SET-ACCEPT-MARK' => [
519 "-j MARK --set-mark 1",
521 'PVEFW-DropBroadcast' => [
522 # same as shorewall 'Broadcast'
523 # simply DROP BROADCAST/MULTICAST/ANYCAST
524 # we can use this to reduce logging
525 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
526 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
527 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
528 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
531 # same as shorewall 'reject'
532 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
533 { action
=> 'DROP', source
=> '224.0.0.0/4' },
534 { action
=> 'DROP', proto
=> 'icmp' },
535 "-p tcp -j REJECT --reject-with tcp-reset",
536 "-p udp -j REJECT --reject-with icmp-port-unreachable",
537 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
538 "-j REJECT --reject-with icmp-host-prohibited",
541 # same as shorewall 'Drop', which is equal to DROP,
542 # but REJECT/DROP some packages to reduce logging,
543 # and ACCEPT critical ICMP types
544 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
545 # we are not interested in BROADCAST/MULTICAST/ANYCAST
546 { action
=> 'PVEFW-DropBroadcast' },
547 # ACCEPT critical ICMP types
548 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
549 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
550 # Drop packets with INVALID state
551 "-m conntrack --ctstate INVALID -j DROP",
552 # Drop Microsoft SMB noise
553 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
554 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
555 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
556 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
557 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
558 # Drop new/NotSyn traffic so that it doesn't get logged
559 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
561 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
564 # same as shorewall 'Reject', which is equal to Reject,
565 # but REJECT/DROP some packages to reduce logging,
566 # and ACCEPT critical ICMP types
567 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
568 # we are not interested in BROADCAST/MULTICAST/ANYCAST
569 { action
=> 'PVEFW-DropBroadcast' },
570 # ACCEPT critical ICMP types
571 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
572 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
573 # Drop packets with INVALID state
574 "-m conntrack --ctstate INVALID -j DROP",
575 # Drop Microsoft SMB noise
576 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
577 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
578 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
579 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
580 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
581 # Drop new/NotSyn traffic so that it doesn't get logged
582 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
584 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
586 'PVEFW-tcpflags' => [
587 # same as shorewall tcpflags action.
588 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
589 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
590 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
591 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
592 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
593 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
596 # same as shorewall smurfs action
597 # Filter packets for smurfs (packets with a broadcast address as the source).
598 "-s 0.0.0.0/32 -j RETURN", # allow DHCP
599 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
600 "-s 224.0.0.0/4 -g PVEFW-smurflog",
604 $pve_std_chains->{6} = {
605 'PVEFW-SET-ACCEPT-MARK' => [
606 "-j MARK --set-mark 1",
608 'PVEFW-DropBroadcast' => [
609 # same as shorewall 'Broadcast'
610 # simply DROP BROADCAST/MULTICAST/ANYCAST
611 # we can use this to reduce logging
612 #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6
613 # ipv6 addrtype does not work with kernel 2.6.32
614 #{ action => 'DROP', dsttype => 'MULTICAST' },
615 #{ action => 'DROP', dsttype => 'ANYCAST' },
616 { action
=> 'DROP', dest
=> 'ff00::/8' },
617 #{ action => 'DROP', dest => '224.0.0.0/4' },
620 # same as shorewall 'reject'
621 #{ action => 'DROP', dsttype => 'BROADCAST' },
622 #{ action => 'DROP', source => '224.0.0.0/4' },
623 { action
=> 'DROP', proto
=> 'icmpv6' },
624 "-p tcp -j REJECT --reject-with tcp-reset",
625 #"-p udp -j REJECT --reject-with icmp-port-unreachable",
626 #"-p icmp -j REJECT --reject-with icmp-host-unreachable",
627 #"-j REJECT --reject-with icmp-host-prohibited",
630 # same as shorewall 'Drop', which is equal to DROP,
631 # but REJECT/DROP some packages to reduce logging,
632 # and ACCEPT critical ICMP types
633 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
634 # we are not interested in BROADCAST/MULTICAST/ANYCAST
635 { action
=> 'PVEFW-DropBroadcast' },
636 # ACCEPT critical ICMP types
637 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
638 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
639 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
641 # Drop packets with INVALID state
642 "-m conntrack --ctstate INVALID -j DROP",
643 # Drop Microsoft SMB noise
644 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
645 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
646 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
647 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
648 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
649 # Drop new/NotSyn traffic so that it doesn't get logged
650 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
652 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
655 # same as shorewall 'Reject', which is equal to Reject,
656 # but REJECT/DROP some packages to reduce logging,
657 # and ACCEPT critical ICMP types
658 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
659 # we are not interested in BROADCAST/MULTICAST/ANYCAST
660 { action
=> 'PVEFW-DropBroadcast' },
661 # ACCEPT critical ICMP types
662 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'destination-unreachable' },
663 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'time-exceeded' },
664 { action
=> 'ACCEPT', proto
=> 'icmpv6', dport
=> 'packet-too-big' },
666 # Drop packets with INVALID state
667 "-m conntrack --ctstate INVALID -j DROP",
668 # Drop Microsoft SMB noise
669 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
670 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
671 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
672 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
673 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
674 # Drop new/NotSyn traffic so that it doesn't get logged
675 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
677 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
679 'PVEFW-tcpflags' => [
680 # same as shorewall tcpflags action.
681 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
682 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
683 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
684 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
685 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
686 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
690 # iptables -p icmp -h
691 my $icmp_type_names = {
694 'destination-unreachable' => 1,
695 'network-unreachable' => 1,
696 'host-unreachable' => 1,
697 'protocol-unreachable' => 1,
698 'port-unreachable' => 1,
699 'fragmentation-needed' => 1,
700 'source-route-failed' => 1,
701 'network-unknown' => 1,
703 'network-prohibited' => 1,
704 'host-prohibited' => 1,
705 'TOS-network-unreachable' => 1,
706 'TOS-host-unreachable' => 1,
707 'communication-prohibited' => 1,
708 'host-precedence-violation' => 1,
709 'precedence-cutoff' => 1,
710 'source-quench' => 1,
712 'network-redirect' => 1,
713 'host-redirect' => 1,
714 'TOS-network-redirect' => 1,
715 'TOS-host-redirect' => 1,
717 'router-advertisement' => 1,
718 'router-solicitation' => 1,
719 'time-exceeded' => 1,
720 'ttl-zero-during-transit' => 1,
721 'ttl-zero-during-reassembly' => 1,
722 'parameter-problem' => 1,
723 'ip-header-bad' => 1,
724 'required-option-missing' => 1,
725 'timestamp-request' => 1,
726 'timestamp-reply' => 1,
727 'address-mask-request' => 1,
728 'address-mask-reply' => 1,
731 # ip6tables -p icmpv6 -h
733 my $icmpv6_type_names = {
735 'destination-unreachable' => 1,
737 'communication-prohibited' => 1,
738 'address-unreachable' => 1,
739 'port-unreachable' => 1,
740 'packet-too-big' => 1,
741 'time-exceeded' => 1,
742 'ttl-zero-during-transit' => 1,
743 'ttl-zero-during-reassembly' => 1,
744 'parameter-problem' => 1,
746 'unknown-header-type' => 1,
747 'unknown-option' => 1,
750 'router-solicitation' => 1,
751 'router-advertisement' => 1,
752 'neighbor-solicitation' => 1,
753 'neighbour-solicitation' => 1,
754 'neighbor-advertisement' => 1,
755 'neighbour-advertisement' => 1,
759 sub init_firewall_macros
{
761 $pve_fw_parsed_macros = {};
764 my ($k, $macro) = @_;
765 my $lc_name = lc($k);
766 $pve_fw_macro_ipversion->{$k} = 0;
767 while (!ref($macro->[0])) {
768 my $desc = shift @$macro;
769 if ($desc eq 'ipv4only') {
770 $pve_fw_macro_ipversion->{$k} = 4;
771 } elsif ($desc eq 'ipv6only') {
772 $pve_fw_macro_ipversion->{$k} = 6;
774 $pve_fw_macro_descr->{$k} = $desc;
777 $pve_fw_preferred_macro_names->{$lc_name} = $k;
778 $pve_fw_parsed_macros->{$k} = $macro;
781 foreach my $k (keys %$pve_fw_macros) {
782 &$parse($k, $pve_fw_macros->{$k});
785 foreach my $k (keys %$pve_ipv6fw_macros) {
786 next if $pve_fw_parsed_macros->{$k};
787 &$parse($k, $pve_ipv6fw_macros->{$k});
788 $pve_fw_macro_ipversion->{$k} = 6;
792 init_firewall_macros
();
795 return wantarray ?
($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
800 sub get_etc_services
{
802 return $etc_services if $etc_services;
804 my $filename = "/etc/services";
806 my $fh = IO
::File-
>new($filename, O_RDONLY
);
808 warn "unable to read '$filename' - $!\n";
814 while (my $line = <$fh>) {
816 next if $line =~m/^#/;
817 next if ($line =~m/^\s*$/);
819 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
820 $services->{byid
}->{$2}->{name
} = $1;
821 $services->{byid
}->{$2}->{port
} = $2;
822 $services->{byid
}->{$2}->{$3} = 1;
823 $services->{byname
}->{$1} = $services->{byid
}->{$2};
829 $etc_services = $services;
832 return $etc_services;
837 sub get_etc_protocols
{
838 return $etc_protocols if $etc_protocols;
840 my $filename = "/etc/protocols";
842 my $fh = IO
::File-
>new($filename, O_RDONLY
);
844 warn "unable to read '$filename' - $!\n";
850 while (my $line = <$fh>) {
852 next if $line =~m/^#/;
853 next if ($line =~m/^\s*$/);
855 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
856 $protocols->{byid
}->{$2}->{name
} = $1;
857 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
863 # add special case for ICMP v6
864 $protocols->{byid
}->{icmpv6
}->{name
} = "icmpv6";
865 $protocols->{byname
}->{icmpv6
} = $protocols->{byid
}->{icmpv6
};
867 $etc_protocols = $protocols;
869 return $etc_protocols;
872 my $ipv4_mask_hash_localnet = {
874 '255.255.128.0' => 17,
875 '255.255.192.0' => 18,
876 '255.255.224.0' => 19,
877 '255.255.240.0' => 20,
878 '255.255.248.0' => 21,
879 '255.255.252.0' => 22,
880 '255.255.254.0' => 23,
881 '255.255.255.0' => 24,
882 '255.255.255.128' => 25,
883 '255.255.255.192' => 26,
884 '255.255.255.224' => 27,
885 '255.255.255.240' => 28,
886 '255.255.255.248' => 29,
887 '255.255.255.252' => 30,
893 my ($new_value) = @_;
895 $__local_network = $new_value if defined($new_value);
897 return $__local_network if defined($__local_network);
900 my $nodename = PVE
::INotify
::nodename
();
902 my $ip = PVE
::Cluster
::remote_node_ip
($nodename);
904 my $testip = Net
::IP-
>new($ip);
906 my $isv6 = $testip->version == 6;
907 my $routes = $isv6 ? PVE
::ProcFSTools
::read_proc_net_ipv6_route
()
908 : PVE
::ProcFSTools
::read_proc_net_route
();
909 foreach my $entry (@$routes) {
912 $mask = $entry->{prefix
};
914 $mask = $ipv4_mask_hash_localnet->{$entry->{mask
}};
915 next if !defined($mask);
917 my $cidr = "$entry->{dest}/$mask";
918 my $testnet = Net
::IP-
>new($cidr);
919 if ($testnet->overlaps($testip) == $Net::IP
::IP_B_IN_A_OVERLAP
) {
920 $__local_network = $cidr;
927 return $__local_network;
930 # ipset names are limited to 31 characters,
931 # and we use '-v4' or '-v6' to indicate IP versions,
932 # and we use '_swap' suffix for atomic update,
933 # for example PVEFW-${VMID}-${ipset_name}_swap
935 my $max_iptables_ipset_name_length = 31 - length("PVEFW-") - length("_swap");
937 sub compute_ipset_chain_name
{
938 my ($vmid, $ipset_name, $ipversion) = @_;
940 $vmid = 0 if !defined($vmid);
942 my $id = "$vmid-${ipset_name}-v$ipversion";
944 if (length($id) > $max_iptables_ipset_name_length) {
945 $id = PVE
::Tools
::fnv31a_hex
($id);
951 sub compute_ipfilter_ipset_name
{
954 return "ipfilter-$iface";
957 sub parse_address_list
{
960 if ($str =~ m/^(\+)(\S+)$/) { # ipset ref
961 die "ipset name too long\n" if length($str) > ($max_ipset_name_length + 1);
965 if ($str =~ m/^${ip_alias_pattern}$/) {
966 die "alias name too long\n" if length($str) > $max_alias_name_length;
974 foreach my $elem (split(/,/, $str)) {
976 my $ip = Net
::IP-
>new($elem);
978 my $err = Net
::IP
::Error
();
979 die "invalid IP address: $err\n";
981 $iprange = 1 if $elem =~ m/-/;
983 my $new_ipversion = Net
::IP
::ip_is_ipv6
($ip->ip()) ?
6 : 4;
985 die "detected mixed ipv4/ipv6 addresses in address list '$str'\n"
986 if $ipversion && ($new_ipversion != $ipversion);
988 $ipversion = $new_ipversion;
991 die "you can't use a range in a list\n" if $iprange && $count > 1;
996 sub parse_port_name_number_or_range
{
999 my $services = PVE
::Firewall
::get_etc_services
();
1003 foreach my $item (split(/,/, $str)) {
1005 if ($item =~ m/^(\d+):(\d+)$/) {
1006 my ($port1, $port2) = ($1, $2);
1007 die "invalid port '$port1'\n" if $port1 > 65535;
1008 die "invalid port '$port2'\n" if $port2 > 65535;
1009 } elsif ($item =~ m/^(\d+)$/) {
1011 die "invalid port '$port'\n" if $port > 65535;
1013 if ($icmp_type_names->{$item}) {
1015 } elsif ($icmpv6_type_names->{$item}) {
1018 die "invalid port '$item'\n" if !$services->{byname
}->{$item};
1023 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
1028 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
1029 sub pve_fw_verify_port_spec
{
1032 parse_port_name_number_or_range
($portstr);
1037 PVE
::JSONSchema
::register_format
('pve-fw-addr-spec', \
&pve_fw_verify_addr_spec
);
1038 sub pve_fw_verify_addr_spec
{
1041 parse_address_list
($list);
1046 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
1047 sub pve_fw_verify_protocol_spec
{
1050 my $protocols = get_etc_protocols
();
1052 die "unknown protocol '$proto'\n" if $proto &&
1053 !(defined($protocols->{byname
}->{$proto}) ||
1054 defined($protocols->{byid
}->{$proto}));
1060 # helper function for API
1062 sub copy_opject_with_digest
{
1065 my $sha = Digest
::SHA-
>new('sha1');
1068 foreach my $k (sort keys %$object) {
1069 my $v = $object->{$k};
1070 next if !defined($v);
1072 $sha->add($k, ':', $v, "\n");
1075 my $digest = $sha->hexdigest;
1077 $res->{digest
} = $digest;
1079 return wantarray ?
($res, $digest) : $res;
1082 sub copy_list_with_digest
{
1085 my $sha = Digest
::SHA-
>new('sha1');
1088 foreach my $entry (@$list) {
1090 foreach my $k (sort keys %$entry) {
1091 my $v = $entry->{$k};
1092 next if !defined($v);
1094 # Note: digest ignores refs ($rule->{errors})
1095 $sha->add($k, ':', $v, "\n") if !ref($v); ;
1100 my $digest = $sha->hexdigest;
1102 foreach my $entry (@$res) {
1103 $entry->{digest
} = $digest;
1106 return wantarray ?
($res, $digest) : $res;
1109 my $rule_properties = {
1111 description
=> "Update rule at position <pos>.",
1116 digest
=> get_standard_option
('pve-config-digest'),
1120 enum
=> ['in', 'out', 'group'],
1123 description
=> "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
1126 pattern
=> $security_group_name_pattern,
1135 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
1137 type
=> 'string', format
=> 'pve-fw-addr-spec',
1141 type
=> 'string', format
=> 'pve-fw-addr-spec',
1145 type
=> 'string', format
=> 'pve-fw-protocol-spec',
1154 type
=> 'string', format
=> 'pve-fw-port-spec',
1158 type
=> 'string', format
=> 'pve-fw-port-spec',
1167 sub add_rule_properties
{
1168 my ($properties) = @_;
1170 foreach my $k (keys %$rule_properties) {
1171 my $h = $rule_properties->{$k};
1172 # copy data, so that we can modify later without side effects
1173 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
1179 sub delete_rule_properties
{
1180 my ($rule, $delete_str) = @_;
1182 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
1183 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
1184 if !defined($rule_properties->{$opt});
1185 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
1186 if $opt eq 'type' || $opt eq 'action';
1187 delete $rule->{$opt};
1193 my $apply_macro = sub {
1194 my ($macro_name, $param, $verify, $ipversion) = @_;
1196 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1197 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1199 if ($ipversion && ($ipversion == 6) && $pve_ipv6fw_macros->{$macro_name}) {
1200 $macro_rules = $pve_ipv6fw_macros->{$macro_name};
1203 # skip macros which are specific to another ipversion
1204 if ($ipversion && (my $required = $pve_fw_macro_ipversion->{$macro_name})) {
1205 return if $ipversion != $required;
1210 foreach my $templ (@$macro_rules) {
1212 my $param_used = {};
1213 foreach my $k (keys %$templ) {
1214 my $v = $templ->{$k};
1215 if ($v eq 'PARAM') {
1217 $param_used->{$k} = 1;
1218 } elsif ($v eq 'DEST') {
1219 $v = $param->{dest
};
1220 $param_used->{dest
} = 1;
1221 } elsif ($v eq 'SOURCE') {
1222 $v = $param->{source
};
1223 $param_used->{source
} = 1;
1227 my $msg = "missing parameter '$k' in macro '$macro_name'";
1228 raise_param_exc
({ macro => $msg }) if $verify;
1233 foreach my $k (keys %$param) {
1234 next if $k eq 'macro';
1235 next if !defined($param->{$k});
1236 next if $param_used->{$k};
1237 if (defined($rule->{$k})) {
1238 if ($rule->{$k} ne $param->{$k}) {
1239 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
1240 raise_param_exc
({ $k => $msg }) if $verify;
1244 $rule->{$k} = $param->{$k};
1247 push @$rules, $rule;
1253 my $rule_env_iface_lookup = {
1262 my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_;
1264 my $allow_groups = $rule_env eq 'group' ?
0 : 1;
1266 my $allow_iface = $rule_env_iface_lookup->{$rule_env};
1267 die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen
1269 my $errors = $rule->{errors
} || {};
1271 my $error_count = 0;
1273 my $add_error = sub {
1274 my ($param, $msg) = @_;
1276 raise_param_exc
({ $param => $msg }) if !$noerr;
1278 $errors->{$param} = $msg if !$errors->{$param};
1282 my $set_ip_version = sub {
1285 die "detected mixed ipv4/ipv6 adresses in rule\n"
1286 if $ipversion && ($vers != $ipversion);
1291 my $check_ipset_or_alias_property = sub {
1292 my ($name, $expected_ipversion) = @_;
1294 if (my $value = $rule->{$name}) {
1295 if ($value =~ m/^\+/) {
1296 if ($value =~ m/^\+(${ipset_name_pattern})$/) {
1297 &$add_error($name, "no such ipset '$1'")
1298 if !($cluster_conf->{ipset
}->{$1} || ($fw_conf && $fw_conf->{ipset
}->{$1}));
1301 &$add_error($name, "invalid ipset name '$value'");
1303 } elsif ($value =~ m/^${ip_alias_pattern}$/){
1304 my $alias = lc($value);
1305 &$add_error($name, "no such alias '$value'")
1306 if !($cluster_conf->{aliases
}->{$alias} || ($fw_conf && $fw_conf->{aliases
}->{$alias}));
1307 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1308 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1310 &$set_ip_version($e->{ipversion
});
1315 my $type = $rule->{type
};
1316 my $action = $rule->{action
};
1318 &$add_error('type', "missing property") if !$type;
1319 &$add_error('action', "missing property") if !$action;
1322 if ($type eq 'in' || $type eq 'out') {
1323 &$add_error('action', "unknown action '$action'")
1324 if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/);
1325 } elsif ($type eq 'group') {
1326 &$add_error('type', "security groups not allowed")
1328 &$add_error('action', "invalid characters in security group name")
1329 if $action && ($action !~ m/^${security_group_name_pattern}$/);
1331 &$add_error('type', "unknown rule type '$type'");
1335 if ($rule->{iface
}) {
1336 &$add_error('type', "parameter -i not allowed for this rule type")
1338 eval { PVE
::JSONSchema
::pve_verify_iface
($rule->{iface
}); };
1339 &$add_error('iface', $@) if $@;
1340 if ($rule_env eq 'vm' || $rule_env eq 'ct') {
1341 &$add_error('iface', "value does not match the regex pattern 'net\\d+'")
1342 if $rule->{iface
} !~ m/^net(\d+)$/;
1346 if ($rule->{macro}) {
1347 if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) {
1348 $rule->{macro} = $preferred_name;
1350 &$add_error('macro', "unknown macro '$rule->{macro}'");
1354 if ($rule->{proto
}) {
1355 eval { pve_fw_verify_protocol_spec
($rule->{proto
}); };
1356 &$add_error('proto', $@) if $@;
1357 &$set_ip_version(4) if $rule->{proto
} eq 'icmp';
1358 &$set_ip_version(6) if $rule->{proto
} eq 'icmpv6';
1361 if ($rule->{dport
}) {
1362 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1363 &$add_error('dport', $@) if $@;
1364 &$add_error('proto', "missing property - 'dport' requires this property")
1368 if ($rule->{sport
}) {
1369 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1370 &$add_error('sport', $@) if $@;
1371 &$add_error('proto', "missing property - 'sport' requires this property")
1375 if ($rule->{source
}) {
1377 my $source_ipversion = parse_address_list
($rule->{source
});
1378 &$set_ip_version($source_ipversion);
1380 &$add_error('source', $@) if $@;
1381 &$check_ipset_or_alias_property('source', $ipversion);
1384 if ($rule->{dest
}) {
1386 my $dest_ipversion = parse_address_list
($rule->{dest
});
1387 &$set_ip_version($dest_ipversion);
1389 &$add_error('dest', $@) if $@;
1390 &$check_ipset_or_alias_property('dest', $ipversion);
1393 $rule->{ipversion
} = $ipversion if $ipversion;
1395 if ($rule->{macro} && !$error_count) {
1396 eval { &$apply_macro($rule->{macro}, $rule, 1, $ipversion); };
1398 if (ref($err) eq "PVE::Exception" && $err->{errors
}) {
1399 my $eh = $err->{errors
};
1400 foreach my $p (keys %$eh) {
1401 &$add_error($p, $eh->{$p});
1404 &$add_error('macro', "$err");
1409 $rule->{errors
} = $errors if $error_count;
1414 sub copy_rule_data
{
1415 my ($rule, $param) = @_;
1417 foreach my $k (keys %$rule_properties) {
1418 if (defined(my $v = $param->{$k})) {
1419 if ($v eq '' || $v eq '-') {
1430 sub rules_modify_permissions
{
1431 my ($rule_env) = @_;
1433 if ($rule_env eq 'host') {
1435 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
1437 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1439 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1441 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1443 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
1450 sub rules_audit_permissions
{
1451 my ($rule_env) = @_;
1453 if ($rule_env eq 'host') {
1455 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
1457 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1459 check
=> ['perm', '/', [ 'Sys.Audit' ]],
1461 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
1463 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1471 my $bridge_firewall_enabled = 0;
1473 sub enable_bridge_firewall
{
1475 return if $bridge_firewall_enabled; # only once
1477 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1478 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1480 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1481 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1483 $bridge_firewall_enabled = 1;
1486 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1488 sub iptables_restore_cmdlist
{
1491 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1494 sub ip6tables_restore_cmdlist
{
1497 run_command
("/sbin/ip6tables-restore -n", input
=> $cmdlist, errmsg
=> "iptables_restore_cmdlist");
1500 sub ipset_restore_cmdlist
{
1503 run_command
("/sbin/ipset restore", input
=> $cmdlist, errmsg
=> "ipset_restore_cmdlist");
1506 sub iptables_get_chains
{
1507 my ($iptablescmd) = @_;
1509 $iptablescmd = "iptables" if !$iptablescmd;
1513 # check what chains we want to track
1514 my $is_pvefw_chain = sub {
1517 return 1 if $name =~ m/^PVEFW-\S+$/;
1519 return 1 if $name =~ m/^tap\d+i\d+-(?:IN|OUT)$/;
1521 return 1 if $name =~ m/^veth\d+i\d+-(?:IN|OUT)$/;
1523 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(?:FW|IN|OUT|IPS)$/;
1524 return 1 if $name =~ m/^GROUP-(?:$security_group_name_pattern)-(?:IN|OUT)$/;
1536 return if $line =~ m/^#/;
1537 return if $line =~ m/^\s*$/;
1539 if ($line =~ m/^\*(\S+)$/) {
1544 return if $table ne 'filter';
1546 if ($line =~ m/^:(\S+)\s/) {
1548 return if !&$is_pvefw_chain($chain);
1549 $res->{$chain} = "unknown";
1550 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1551 my ($chain, $sig) = ($1, $2);
1552 return if !&$is_pvefw_chain($chain);
1553 $res->{$chain} = $sig;
1554 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1557 # simply ignore the rest
1562 run_command
("/sbin/$iptablescmd-save", outfunc
=> $parser);
1564 return wantarray ?
($res, $hooks) : $res;
1567 sub iptables_chain_digest
{
1569 my $digest = Digest
::SHA-
>new('sha1');
1570 foreach my $rule (@$rules) { # order is important
1571 $digest->add($rule);
1573 return $digest->b64digest;
1576 sub ipset_chain_digest
{
1579 my $digest = Digest
::SHA-
>new('sha1');
1580 foreach my $rule (sort @$rules) { # note: sorted
1581 $digest->add($rule);
1583 return $digest->b64digest;
1586 sub ipset_get_chains
{
1594 return if $line =~ m/^#/;
1595 return if $line =~ m/^\s*$/;
1596 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1598 $line =~ s/\s+$//; # delete trailing white space
1599 push @{$chains->{$chain}}, $line;
1601 # simply ignore the rest
1606 run_command
("/sbin/ipset save", outfunc
=> $parser);
1608 # compute digest for each chain
1609 foreach my $chain (keys %$chains) {
1610 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1616 sub ruleset_generate_cmdstr
{
1617 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1619 return if defined($rule->{enable
}) && !$rule->{enable
};
1620 return if $rule->{errors
};
1622 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1624 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1625 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1629 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1630 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1632 my $source = $rule->{source
};
1633 my $dest = $rule->{dest
};
1636 if ($source =~ m/^\+/) {
1637 if ($source =~ m/^\+(${ipset_name_pattern})$/) {
1639 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1640 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1641 push @cmd, "-m set --match-set ${ipset_chain} src";
1642 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1643 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1644 push @cmd, "-m set --match-set ${ipset_chain} src";
1646 die "no such ipset '$name'\n";
1649 die "invalid security group name '$source'\n";
1651 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1652 my $alias = lc($source);
1653 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1654 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1655 die "no such alias '$source'\n" if !$e;
1656 push @cmd, "-s $e->{cidr}";
1657 } elsif ($source =~ m/\-/){
1658 push @cmd, "-m iprange --src-range $source";
1660 push @cmd, "-s $source";
1665 if ($dest =~ m/^\+/) {
1666 if ($dest =~ m/^\+(${ipset_name_pattern})$/) {
1668 if ($fw_conf && $fw_conf->{ipset
}->{$name}) {
1669 my $ipset_chain = compute_ipset_chain_name
($fw_conf->{vmid
}, $name, $ipversion);
1670 push @cmd, "-m set --match-set ${ipset_chain} dst";
1671 } elsif ($cluster_conf && $cluster_conf->{ipset
}->{$name}) {
1672 my $ipset_chain = compute_ipset_chain_name
(0, $name, $ipversion);
1673 push @cmd, "-m set --match-set ${ipset_chain} dst";
1675 die "no such ipset '$name'\n";
1678 die "invalid security group name '$dest'\n";
1680 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1681 my $alias = lc($dest);
1682 my $e = $fw_conf ?
$fw_conf->{aliases
}->{$alias} : undef;
1683 $e = $cluster_conf->{aliases
}->{$alias} if !$e && $cluster_conf;
1684 die "no such alias '$dest'\n" if !$e;
1685 push @cmd, "-d $e->{cidr}";
1686 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1687 push @cmd, "-m iprange --dst-range $dest";
1689 push @cmd, "-d $dest";
1693 if ($rule->{proto
}) {
1694 push @cmd, "-p $rule->{proto}";
1697 $multiport++ if $nbdport > 1;
1698 $multiport++ if $nbsport > 1;
1700 push @cmd, "--match multiport" if $multiport;
1702 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1703 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1705 if ($rule->{dport
}) {
1706 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1707 # Note: we use dport to store --icmp-type
1708 die "unknown icmp-type '$rule->{dport}'\n"
1709 if $rule->{dport
} !~ /^\d+$/ && !defined($icmp_type_names->{$rule->{dport
}});
1710 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1711 } elsif ($rule->{proto
} && $rule->{proto
} eq 'icmpv6') {
1712 # Note: we use dport to store --icmpv6-type
1713 die "unknown icmpv6-type '$rule->{dport}'\n"
1714 if $rule->{dport
} !~ /^\d+$/ && !defined($icmpv6_type_names->{$rule->{dport
}});
1715 push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}";
1718 if ($multiport == 2) {
1719 push @cmd, "--ports $rule->{dport}";
1721 push @cmd, "--dports $rule->{dport}";
1724 push @cmd, "--dport $rule->{dport}";
1729 if ($rule->{sport
}) {
1731 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1733 push @cmd, "--sport $rule->{sport}";
1736 } elsif ($rule->{dport
} || $rule->{sport
}) {
1737 die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport
};
1738 die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport
};
1741 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1743 if (my $action = $rule->{action
}) {
1744 $action = $actions->{$action} if defined($actions->{$action});
1745 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1746 push @cmd, $goto ?
"-g $action" : "-j $action";
1749 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1752 sub ruleset_generate_rule
{
1753 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_;
1757 if ($rule->{macro}) {
1758 $rules = &$apply_macro($rule->{macro}, $rule, 0, $ipversion);
1763 # update all or nothing
1766 foreach my $tmp (@$rules) {
1767 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) {
1768 push @cmds, $cmdstr;
1772 foreach my $cmdstr (@cmds) {
1773 ruleset_addrule
($ruleset, $chain, $cmdstr);
1777 sub ruleset_generate_rule_insert
{
1778 my ($ruleset, $chain, $ipversion, $rule, $actions, $goto) = @_;
1780 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1782 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $ipversion, $rule, $actions, $goto)) {
1783 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1787 sub ruleset_create_chain
{
1788 my ($ruleset, $chain) = @_;
1790 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1791 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1793 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1795 $ruleset->{$chain} = [];
1798 sub ruleset_chain_exist
{
1799 my ($ruleset, $chain) = @_;
1801 return $ruleset->{$chain} ?
1 : undef;
1804 sub ruleset_addrule
{
1805 my ($ruleset, $chain, $rule) = @_;
1807 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1809 push @{$ruleset->{$chain}}, "-A $chain $rule";
1812 sub ruleset_insertrule
{
1813 my ($ruleset, $chain, $rule) = @_;
1815 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1817 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1820 sub get_log_rule_base
{
1821 my ($chain, $vmid, $msg, $loglevel) = @_;
1823 die "internal error - no log level" if !defined($loglevel);
1825 $vmid = 0 if !defined($vmid);
1827 # Note: we use special format for prefix to pass further
1828 # info to log daemon (VMID, LOGVELEL and CHAIN)
1830 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1833 sub ruleset_addlog {
1834 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1836 return if !defined($loglevel);
1838 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1840 $logrule = "$rule $logrule" if defined($rule);
1842 ruleset_addrule($ruleset, $chain, $logrule);
1845 sub ruleset_add_chain_policy {
1846 my ($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action) = @_;
1848 if ($policy eq 'ACCEPT') {
1850 ruleset_generate_rule($ruleset, $chain, $ipversion, { action => 'ACCEPT' },
1851 { ACCEPT => $accept_action});
1853 } elsif ($policy eq 'DROP') {
1855 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1857 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1859 ruleset_addrule($ruleset, $chain, "-j DROP
");
1860 } elsif ($policy eq 'REJECT') {
1861 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1863 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1865 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1868 die "internal error
: unknown policy
'$policy'";
1872 sub ruleset_chain_add_conn_filters {
1873 my ($ruleset, $chain, $accept) = @_;
1875 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1876 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1879 sub ruleset_chain_add_input_filters {
1880 my ($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel) = @_;
1882 if ($cluster_conf->{ipset}->{blacklist}){
1883 if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist
")) {
1884 ruleset_create_chain($ruleset, "PVEFW-blacklist
");
1885 ruleset_addlog($ruleset, "PVEFW-blacklist
", 0, "DROP
: ", $loglevel) if $loglevel;
1886 ruleset_addrule($ruleset, "PVEFW-blacklist
", "-j DROP
");
1888 my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion);
1889 ruleset_addrule($ruleset, $chain, "-m set
--match-set
${ipset_chain
} src
-j PVEFW-blacklist
");
1892 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1893 if ($ipversion == 4) {
1894 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1898 if ($options->{tcpflags}) {
1899 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1903 sub ruleset_create_vm_chain {
1904 my ($ruleset, $chain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction) = @_;
1906 ruleset_create_chain($ruleset, $chain);
1907 my $accept = generate_nfqueue($options);
1909 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1910 if ($ipversion == 4) {
1911 if ($direction eq 'OUT') {
1912 ruleset_generate_rule($ruleset, $chain, $ipversion,
1913 { action => 'PVEFW-SET-ACCEPT-MARK',
1914 proto => 'udp', sport => 68, dport => 67 });
1916 ruleset_generate_rule($ruleset, $chain, $ipversion,
1917 { action => 'ACCEPT',
1918 proto => 'udp', sport => 67, dport => 68 });
1920 } elsif ($ipversion == 6) {
1921 if ($direction eq 'OUT') {
1922 ruleset_generate_rule($ruleset, $chain, $ipversion,
1923 { action => 'PVEFW-SET-ACCEPT-MARK',
1924 proto => 'udp', sport => 546, dport => 547 });
1926 ruleset_generate_rule($ruleset, $chain, $ipversion,
1927 { action => 'ACCEPT',
1928 proto => 'udp', sport => 547, dport => 546 });
1934 if ($direction eq 'OUT') {
1935 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1936 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1938 if ($ipfilter_ipset) {
1939 ruleset_addrule($ruleset, $chain, "-m set
! --match-set
$ipfilter_ipset src
-j DROP
");
1941 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1945 sub ruleset_add_group_rule {
1946 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_;
1948 my $group = $rule->{action};
1949 my $group_chain = "GROUP-
$group-$direction";
1950 if(!ruleset_chain_exist($ruleset, $group_chain)){
1951 generate_group_rules($ruleset, $cluster_conf, $group, $ipversion);
1954 if ($direction eq 'OUT' && $rule->{iface_out}) {
1955 ruleset_addrule($ruleset, $chain, "-o
$rule->{iface_out
} -j
$group_chain");
1956 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1957 ruleset_addrule($ruleset, $chain, "-i
$rule->{iface_in
} -j
$group_chain");
1959 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1962 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$action");
1965 sub ruleset_generate_vm_rules {
1966 my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_;
1968 my $lc_direction = lc($direction);
1970 my $in_accept = generate_nfqueue($options);
1972 foreach my $rule (@$rules) {
1973 next if $rule->{iface} && $rule->{iface} ne $netid;
1974 next if !$rule->{enable} || $rule->{errors};
1975 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
1977 if ($rule->{type} eq 'group') {
1978 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
1979 $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion);
1981 next if $rule->{type} ne $lc_direction;
1983 if ($direction eq 'OUT') {
1984 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1985 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
1986 undef, $cluster_conf, $vmfw_conf);
1988 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
1989 { ACCEPT => $in_accept , REJECT => "PVEFW-reject
" },
1990 undef, $cluster_conf, $vmfw_conf);
1998 sub generate_nfqueue {
2001 if ($options->{ips}) {
2002 my $action = "NFQUEUE
";
2003 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
2004 if (defined($3) && defined($1)) {
2005 $action .= " --queue-balance
$1:$3";
2006 } elsif (defined($1)) {
2007 $action .= " --queue-num
$1";
2010 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
2017 sub ruleset_generate_vm_ipsrules {
2018 my ($ruleset, $options, $direction, $iface) = @_;
2020 if ($options->{ips} && $direction eq 'IN') {
2021 my $nfqueue = generate_nfqueue($options);
2023 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS
")) {
2024 ruleset_create_chain($ruleset, "PVEFW-IPS
");
2027 ruleset_addrule($ruleset, "PVEFW-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
2031 sub generate_tap_rules_direction {
2032 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_;
2034 my $lc_direction = lc($direction);
2036 my $rules = $vmfw_conf->{rules};
2038 my $options = $vmfw_conf->{options};
2039 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
2041 my $tapchain = "$iface-$direction";
2043 my $ipfilter_name = compute_ipfilter_ipset_name($netid);
2044 my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
2045 if $vmfw_conf->{ipset}->{$ipfilter_name};
2047 # create chain with mac and ip filter
2048 ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction);
2050 if ($options->{enable}) {
2051 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion);
2053 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
2058 if ($direction eq 'OUT') {
2059 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2061 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
2064 my $accept = generate_nfqueue($options);
2065 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
2066 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
2068 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : 'ACCEPT';
2069 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, 'ACCEPT', $loglevel, $accept_action);
2072 # plug the tap chain to bridge chain
2073 if ($direction eq 'IN') {
2074 ruleset_addrule($ruleset, "PVEFW-FWBR-IN
",
2075 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
2077 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT
",
2078 "-m physdev
--physdev-is-bridged
--physdev-in
$iface -j
$tapchain");
2082 sub enable_host_firewall {
2083 my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
2085 my $options = $hostfw_conf->{options};
2086 my $cluster_options = $cluster_conf->{options};
2087 my $rules = $hostfw_conf->{rules};
2088 my $cluster_rules = $cluster_conf->{rules};
2090 # host inbound firewall
2091 my $chain = "PVEFW-HOST-IN
";
2092 ruleset_create_chain($ruleset, $chain);
2094 my $loglevel = get_option_log_level($options, "log_level_in
");
2096 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
2098 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2099 ruleset_chain_add_input_filters($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel);
2101 # we use RETURN because we need to check also tap rules
2102 my $accept_action = 'RETURN';
2104 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2106 # add host rules first, so that cluster wide rules can be overwritten
2107 foreach my $rule (@$rules, @$cluster_rules) {
2108 next if !$rule->{enable} || $rule->{errors};
2109 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2111 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
2114 if ($rule->{type} eq 'group') {
2115 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
2116 } elsif ($rule->{type} eq 'in') {
2117 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2118 { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2119 undef, $cluster_conf, $hostfw_conf);
2123 delete $rule->{iface_in};
2126 # allow standard traffic for management ipset (includes cluster network)
2127 my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management
", $ipversion);
2128 my $mngmntsrc = "-m set
--match-set
${mngmnt_ipset_chain
} src
";
2129 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2130 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2131 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2132 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp
--dport
22 -j
$accept_action"); # SSH
2134 my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
2135 my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion};
2138 if ($localnet && ($ipversion == $localnet_ver)) {
2139 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2140 ruleset_addrule($ruleset, $chain, "-s
$localnet -d
$localnet $corosync_rule");
2141 ruleset_addrule($ruleset, $chain, "-s
$localnet -m addrtype
--dst-type MULTICAST
$corosync_rule");
2144 # implement input policy
2145 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
2146 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2148 # host outbound firewall
2149 $chain = "PVEFW-HOST-OUT
";
2150 ruleset_create_chain($ruleset, $chain);
2152 $loglevel = get_option_log_level($options, "log_level_out
");
2154 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
2156 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
2158 # we use RETURN because we may want to check other thigs later
2159 $accept_action = 'RETURN';
2161 ruleset_addrule($ruleset, $chain, "-p igmp
-j
$accept_action"); # important for multicast
2163 # add host rules first, so that cluster wide rules can be overwritten
2164 foreach my $rule (@$rules, @$cluster_rules) {
2165 next if !$rule->{enable} || $rule->{errors};
2166 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
2168 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
2170 if ($rule->{type} eq 'group') {
2171 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
2172 } elsif ($rule->{type} eq 'out') {
2173 ruleset_generate_rule($ruleset, $chain, $ipversion,
2174 $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
2175 undef, $cluster_conf, $hostfw_conf);
2179 delete $rule->{iface_out};
2182 # allow standard traffic on cluster network
2183 if ($localnet && ($ipversion == $localnet_ver)) {
2184 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
8006 -j
$accept_action"); # PVE API
2185 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
22 -j
$accept_action"); # SSH
2186 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
5900:5999 -j
$accept_action"); # PVE VNC Console
2187 ruleset_addrule($ruleset, $chain, "-d
$localnet -p tcp
--dport
3128 -j
$accept_action"); # SPICE Proxy
2189 my $corosync_rule = "-p udp
--dport
5404:5405 -j
$accept_action";
2190 ruleset_addrule($ruleset, $chain, "-d
$localnet $corosync_rule");
2191 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
$corosync_rule");
2194 # implement output policy
2195 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
2196 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
2198 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
2199 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
2202 sub generate_group_rules {
2203 my ($ruleset, $cluster_conf, $group, $ipversion) = @_;
2205 my $rules = $cluster_conf->{groups}->{$group};
2208 warn "no such security group
'$group'\n";
2209 $rules = []; # create empty chain
2212 my $chain = "GROUP-
${group
}-IN
";
2214 ruleset_create_chain($ruleset, $chain);
2215 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2217 foreach my $rule (@$rules) {
2218 next if $rule->{type} ne 'in';
2219 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2220 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2221 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
2222 undef, $cluster_conf);
2225 $chain = "GROUP-
${group
}-OUT
";
2227 ruleset_create_chain($ruleset, $chain);
2228 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
2230 foreach my $rule (@$rules) {
2231 next if $rule->{type} ne 'out';
2232 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
2233 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
2234 # check also other tap rules later
2235 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule,
2236 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" },
2237 undef, $cluster_conf);
2242 my $valid_netdev_names = {};
2243 for (my $i = 0; $i < $MAX_NETS; $i++) {
2244 $valid_netdev_names->{"net
$i"} = 1;
2248 my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_;
2250 my $orig_line = $line;
2254 # we can add single line comments to the end of the rule
2255 if ($line =~ s/#\s*(.*?)\s*$//) {
2256 $rule->{comment} = decode('utf8', $1);
2259 # we can disable a rule when prefixed with '|'
2261 $rule->{enable} = $line =~ s/^\|// ? 0 : 1;
2263 $line =~ s/^(\S+)\s+(\S+)\s*// ||
2264 die "unable to parse rule
: $line\n";
2266 $rule->{type} = lc($1);
2267 $rule->{action} = $2;
2269 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
2270 if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
2271 $rule->{macro} = $1;
2272 $rule->{action} = $2;
2276 while (length($line)) {
2277 if ($line =~ s/^-i (\S+)\s*//) {
2278 $rule->{iface} = $1;
2282 last if $rule->{type} eq 'group';
2284 if ($line =~ s/^-p (\S+)\s*//) {
2285 $rule->{proto} = $1;
2289 if ($line =~ s/^-dport (\S+)\s*//) {
2290 $rule->{dport} = $1;
2294 if ($line =~ s/^-sport (\S+)\s*//) {
2295 $rule->{sport} = $1;
2298 if ($line =~ s/^-source (\S+)\s*//) {
2299 $rule->{source} = $1;
2302 if ($line =~ s/^-dest (\S+)\s*//) {
2310 die "unable to parse rule parameters
: $line\n" if length($line);
2312 $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1);
2313 if ($verbose && $rule->{errors}) {
2314 warn "$prefix - errors
in rule parameters
: $orig_line\n";
2315 foreach my $p (keys %{$rule->{errors}}) {
2316 warn " $p: $rule->{errors
}->{$p}\n";
2323 sub parse_vmfw_option {
2328 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
2330 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
2333 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
2335 $value = $2 ? lc($3) : '';
2336 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2339 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
2343 die "can
't parse option '$line'\n"
2346 return ($opt, $value);
2349 sub parse_hostfw_option {
2354 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2356 if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
2359 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
2361 $value = $2 ? lc($3) : '';
2362 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
2366 die "can't parse option
'$line'\n"
2369 return ($opt, $value);
2372 sub parse_clusterfw_option {
2377 if ($line =~ m/^(enable):\s*(\d+)\s*$/i) {
2380 if (($value > 1) && ((time() - $value) > 60)) {
2383 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2387 die "can
't parse option '$line'\n"
2390 return ($opt, $value);
2394 my ($clusterfw_conf, $fw_conf, $cidr) = @_;
2396 my $alias = lc($cidr);
2397 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
2398 $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
2400 die "no such alias '$cidr'\n" if !$e;;
2402 return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr};
2405 sub parse_ip_or_cidr {
2410 if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
2413 } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
2417 die "value does not look like a valid IP address or CIDR network\n";
2420 return wantarray ? ($cidr, $ipversion) : $cidr;
2426 # we can add single line comments to the end of the line
2427 my $comment = decode('utf8
', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
2429 if ($line =~ m/^(\S+)\s(\S+)$/) {
2430 my ($name, $cidr) = ($1, $2);
2433 ($cidr, $ipversion) = parse_ip_or_cidr($cidr);
2438 ipversion => $ipversion,
2440 $data->{comment} = $comment if $comment;
2447 sub generic_fw_config_parser {
2448 my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_;
2453 my $res = $empty_conf;
2455 while (defined(my $line = <$fh>)) {
2456 next if $line =~ m/^#/;
2457 next if $line =~ m/^\s*$/;
2461 my $linenr = $fh->input_line_number();
2462 my $prefix = "$filename (line $linenr)";
2464 if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
2465 $section = 'options
';
2469 if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) {
2470 $section = 'aliases
';
2474 if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2475 $section = 'groups
';
2479 die "security group name too long\n" if length($group) > $max_group_name_length;
2480 die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/;
2483 ($section, $group, $comment) = undef;
2484 warn "$prefix: $err";
2488 $res->{$section}->{$group} = [];
2489 $res->{group_comments}->{$group} = decode('utf8
', $comment)
2494 if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) {
2499 if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
2504 die "ipset name too long\n" if length($group) > $max_ipset_name_length;
2505 die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
2508 ($section, $group, $comment) = undef;
2509 warn "$prefix: $err";
2513 $res->{$section}->{$group} = [];
2514 $res->{ipset_comments}->{$group} = decode('utf8
', $comment)
2520 warn "$prefix: skip line - no section\n";
2524 if ($section eq 'options
') {
2527 if ($rule_env eq 'cluster
') {
2528 ($opt, $value) = parse_clusterfw_option($line);
2529 } elsif ($rule_env eq 'host
') {
2530 ($opt, $value) = parse_hostfw_option($line);
2532 ($opt, $value) = parse_vmfw_option($line);
2534 $res->{options}->{$opt} = $value;
2536 warn "$prefix: $@" if $@;
2537 } elsif ($section eq 'aliases
') {
2539 my $data = parse_alias($line);
2540 $res->{aliases}->{lc($data->{name})} = $data;
2542 warn "$prefix: $@" if $@;
2543 } elsif ($section eq 'rules
') {
2545 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); };
2547 warn "$prefix: $err";
2550 push @{$res->{$section}}, $rule;
2551 } elsif ($section eq 'groups
') {
2553 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group
', $verbose); };
2555 warn "$prefix: $err";
2558 push @{$res->{$section}->{$group}}, $rule;
2559 } elsif ($section eq 'ipset
') {
2560 # we can add single line comments to the end of the rule
2561 my $comment = decode('utf8
', $1) if $line =~ s/#\s*(.*?)\s*$//;
2563 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2568 if ($nomatch && !$feature_ipset_nomatch) {
2569 $errors->{nomatch} = "nomatch not supported by kernel";
2573 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2574 resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
2576 $cidr = parse_ip_or_cidr($cidr);
2581 $errors->{cidr} = $err;
2584 my $entry = { cidr => $cidr };
2585 $entry->{nomatch} = 1 if $nomatch;
2586 $entry->{comment} = $comment if $comment;
2587 $entry->{errors} = $errors if $errors;
2589 if ($verbose && $errors) {
2590 warn "$prefix - errors in ipset '$group': $line\n";
2591 foreach my $p (keys %{$errors}) {
2592 warn " $p: $errors->{$p}\n";
2596 push @{$res->{$section}->{$group}}, $entry;
2598 warn "$prefix: skip line - unknown section\n";
2606 sub parse_hostfw_config {
2607 my ($filename, $fh, $cluster_conf, $verbose) = @_;
2609 my $empty_conf = { rules => [], options => {}};
2611 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host
');
2614 sub parse_vmfw_config {
2615 my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_;
2622 ipset_comments => {},
2625 return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env);
2628 sub parse_clusterfw_config {
2629 my ($filename, $fh, $verbose) = @_;
2639 group_comments => {},
2641 ipset_comments => {},
2644 return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster
');
2648 my ($code, @param) = @_;
2652 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2659 sub read_local_vm_config {
2664 my $vmdata = { qemu => $qemu, lxc => $lxc };
2666 my $vmlist = PVE::Cluster::get_vmlist();
2667 return $vmdata if !$vmlist || !$vmlist->{ids};
2668 my $ids = $vmlist->{ids};
2670 foreach my $vmid (keys %$ids) {
2671 next if !$vmid; # skip VE0
2672 my $d = $ids->{$vmid};
2673 next if !$d->{node} || $d->{node} ne $nodename;
2674 next if !$d->{type};
2675 if ($d->{type} eq 'qemu
') {
2676 if ($have_qemu_server) {
2677 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2678 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2679 $qemu->{$vmid} = $conf;
2682 } elsif ($d->{type} eq 'lxc
') {
2684 my $cfspath = PVE::LXC::cfs_config_path($vmid);
2685 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2686 $lxc->{$vmid} = $conf;
2695 sub load_vmfw_conf {
2696 my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_;
2700 $dir = $pvefw_conf_dir if !defined($dir);
2702 my $filename = "$dir/$vmid.fw";
2703 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2704 $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose);
2705 $vmfw_conf->{vmid} = $vmid;
2711 my $format_rules = sub {
2712 my ($rules, $allow_iface) = @_;
2716 foreach my $rule (@$rules) {
2717 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
' || $rule->{type} eq 'group
') {
2718 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2719 $raw .= uc($rule->{type});
2720 if ($rule->{macro}) {
2721 $raw .= " $rule->{macro}($rule->{action})";
2723 $raw .= " " . $rule->{action};
2725 if ($allow_iface && $rule->{iface}) {
2726 $raw .= " -i $rule->{iface}";
2729 if ($rule->{type} ne 'group
') {
2730 $raw .= " -source $rule->{source}" if $rule->{source};
2731 $raw .= " -dest $rule->{dest}" if $rule->{dest};
2732 $raw .= " -p $rule->{proto}" if $rule->{proto};
2733 $raw .= " -dport $rule->{dport}" if $rule->{dport};
2734 $raw .= " -sport $rule->{sport}" if $rule->{sport};
2737 $raw .= " # " . encode('utf8
', $rule->{comment})
2738 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2741 die "unknown rule type '$rule->{type
}'";
2748 my $format_options = sub {
2753 $raw .= "[OPTIONS]\n\n";
2754 foreach my $opt (keys %$options) {
2755 $raw .= "$opt: $options->{$opt}\n";
2762 my $format_aliases = sub {
2767 $raw .= "[ALIASES]\n\n";
2768 foreach my $k (keys %$aliases) {
2769 my $e = $aliases->{$k};
2770 $raw .= "$e->{name} $e->{cidr}";
2771 $raw .= " # " . encode('utf8
', $e->{comment})
2772 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
2780 my $format_ipsets = sub {
2785 foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
2786 if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) {
2787 my $utf8comment = encode('utf8
', $comment);
2788 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2790 $raw .= "[IPSET $ipset]\n\n";
2792 my $options = $fw_conf->{ipset}->{$ipset};
2795 foreach my $entry (@$options) {
2796 $nethash->{$entry->{cidr}} = $entry;
2799 foreach my $cidr (sort keys %$nethash) {
2800 my $entry = $nethash->{$cidr};
2801 my $line = $entry->{nomatch} ? '!' : '';
2802 $line .= $entry->{cidr};
2803 $line .= " # " . encode('utf8
', $entry->{comment})
2804 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
2814 sub save_vmfw_conf {
2815 my ($vmid, $vmfw_conf) = @_;
2819 my $options = $vmfw_conf->{options};
2820 $raw .= &$format_options($options) if $options && scalar(keys %$options);
2822 my $aliases = $vmfw_conf->{aliases};
2823 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
2825 $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset};
2827 my $rules = $vmfw_conf->{rules} || [];
2828 if ($rules && scalar(@$rules)) {
2829 $raw .= "[RULES]\n\n";
2830 $raw .= &$format_rules($rules, 1);
2834 mkdir $pvefw_conf_dir;
2836 my $filename = "$pvefw_conf_dir/$vmid.fw";
2837 PVE::Tools::file_set_contents($filename, $raw);
2840 sub remove_vmfw_conf {
2843 my $vmfw_conffile = "$pvefw_conf_dir/$vmid.fw";
2845 unlink $vmfw_conffile;
2848 sub clone_vmfw_conf {
2849 my ($vmid, $newid) = @_;
2851 my $sourcevm_conffile = "$pvefw_conf_dir/$vmid.fw";
2852 my $clonevm_conffile = "$pvefw_conf_dir/$newid.fw";
2854 if (-f $clonevm_conffile) {
2855 unlink $clonevm_conffile;
2857 if (-f $sourcevm_conffile) {
2858 my $data = PVE::Tools::file_get_contents($sourcevm_conffile);
2859 PVE::Tools::file_set_contents($clonevm_conffile, $data);
2863 sub read_vm_firewall_configs {
2864 my ($cluster_conf, $vmdata, $dir, $verbose) = @_;
2866 my $vmfw_configs = {};
2868 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2869 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm
', $vmid, $dir, $verbose);
2870 next if !$vmfw_conf->{options}; # skip if file does not exists
2871 $vmfw_configs->{$vmid} = $vmfw_conf;
2873 foreach my $vmid (keys %{$vmdata->{lxc}}) {
2874 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct
', $vmid, $dir, $verbose);
2875 next if !$vmfw_conf->{options}; # skip if file does not exists
2876 $vmfw_configs->{$vmid} = $vmfw_conf;
2879 return $vmfw_configs;
2882 sub get_option_log_level {
2883 my ($options, $k) = @_;
2885 my $v = $options->{$k};
2886 $v = $default_log_level if !defined($v);
2888 return undef if $v eq '' || $v eq 'nolog
';
2890 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2892 return $v if ($v >= 0) && ($v <= 7);
2894 warn "unknown log level ($k = '$v')\n";
2899 sub generate_std_chains {
2900 my ($ruleset, $options, $ipversion) = @_;
2902 my $std_chains = $pve_std_chains->{$ipversion} || die "internal error";
2904 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2908 if ($ipversion == 4) {
2909 # same as shorewall smurflog.
2910 $chain = 'PVEFW-smurflog
';
2911 $std_chains->{$chain} = [];
2913 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2914 push @{$std_chains->{$chain}}, "-j DROP";
2917 # same as shorewall logflags action.
2918 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2919 $chain = 'PVEFW-logflags
';
2920 $std_chains->{$chain} = [];
2922 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2923 push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2924 push @{$std_chains->{$chain}}, "-j DROP";
2926 foreach my $chain (keys %$std_chains) {
2927 ruleset_create_chain($ruleset, $chain);
2928 foreach my $rule (@{$std_chains->{$chain}}) {
2930 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule);
2932 ruleset_addrule($ruleset, $chain, $rule);
2938 sub generate_ipset_chains {
2939 my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; #fixme
2941 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2943 my $options = $fw_conf->{ipset}->{$ipset};
2947 foreach my $entry (@$options) {
2948 next if $entry->{errors}; # skip entries with errors
2951 if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
2952 ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
2954 ($cidr, $ver) = parse_ip_or_cidr($entry->{cidr});
2956 #http://backreference.org/2013/03/01/ipv6-address-normalization/
2958 $cidr = lc(Net::IP::ip_compress_address($cidr, 6));
2964 $nethash->{$ver}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} };
2969 foreach my $ipversion (4, 6) {
2970 my $data = $nethash->{$ipversion};
2972 my $name = compute_ipset_chain_name($fw_conf->{vmid}, $ipset, $ipversion);
2974 my $hashsize = scalar(@$options);
2975 if ($hashsize <= 64) {
2978 $hashsize = round_powerof2($hashsize);
2981 my $family = $ipversion == "6" ? "inet6" : "inet";
2983 $ipset_ruleset->{$name} = ["create $name hash:net family $family hashsize $hashsize maxelem $hashsize"];
2985 foreach my $cidr (sort keys %$data) {
2986 my $entry = $data->{$cidr};
2988 my $cmd = "add $name $cidr";
2989 if ($entry->{nomatch}) {
2990 if ($feature_ipset_nomatch) {
2991 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
2993 warn "ignore !$cidr - nomatch not supported by kernel\n";
2996 push @{$ipset_ruleset->{$name}}, $cmd;
3003 sub round_powerof2 {
3007 $int |= $int >> $_ foreach (1,2,4,8,16);
3011 sub load_clusterfw_conf {
3012 my ($filename, $verbose) = @_;
3014 $filename = $clusterfw_conf_filename if !defined($filename);
3016 my $cluster_conf = {};
3017 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3018 $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose);
3021 return $cluster_conf;
3024 sub save_clusterfw_conf {
3025 my ($cluster_conf) = @_;
3029 my $options = $cluster_conf->{options};
3030 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3032 my $aliases = $cluster_conf->{aliases};
3033 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
3035 $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
3037 my $rules = $cluster_conf->{rules};
3038 if ($rules && scalar(@$rules)) {
3039 $raw .= "[RULES]\n\n";
3040 $raw .= &$format_rules($rules, 1);
3044 if ($cluster_conf->{groups}) {
3045 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
3046 my $rules = $cluster_conf->{groups}->{$group};
3047 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
3048 my $utf8comment = encode('utf8
', $comment);
3049 $raw .= "[group $group] # $utf8comment\n\n";
3051 $raw .= "[group $group]\n\n";
3054 $raw .= &$format_rules($rules, 0);
3059 mkdir $pvefw_conf_dir;
3060 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
3063 sub load_hostfw_conf {
3064 my ($cluster_conf, $filename, $verbose) = @_;
3066 $filename = $hostfw_conf_filename if !defined($filename);
3068 my $hostfw_conf = {};
3069 if (my $fh = IO::File->new($filename, O_RDONLY)) {
3070 $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose);
3072 return $hostfw_conf;
3075 sub save_hostfw_conf {
3076 my ($hostfw_conf) = @_;
3080 my $options = $hostfw_conf->{options};
3081 $raw .= &$format_options($options) if $options && scalar(keys %$options);
3083 my $rules = $hostfw_conf->{rules};
3084 if ($rules && scalar(@$rules)) {
3085 $raw .= "[RULES]\n\n";
3086 $raw .= &$format_rules($rules, 1);
3090 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
3094 my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_;
3098 if ($vmdata) { # test mode
3099 my $testdir = $vmdata->{testdir} || die "no test directory specified";
3100 my $filename = "$testdir/cluster.fw";
3101 $cluster_conf = load_clusterfw_conf($filename, $verbose);
3103 $filename = "$testdir/host.fw";
3104 $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose);
3106 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose);
3107 } else { # normal operation
3108 $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf;
3110 $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf;
3112 $vmdata = read_local_vm_config();
3113 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose);
3116 my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose);
3117 my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose);
3119 return ($ruleset, $ipset_ruleset, $rulesetv6);
3122 sub compile_iptables_filter {
3123 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_;
3126 if ($cluster_conf->{aliases}->{local_network}) {
3127 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3130 ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
3132 $cluster_conf->{aliases}->{local_network} = {
3133 name => 'local_network
', cidr => $localnet, ipversion => $localnet_ver };
3136 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
3138 return ({}, {}) if !$cluster_conf->{options}->{enable};
3142 ruleset_create_chain($ruleset, "PVEFW-INPUT");
3143 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
3145 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3147 my $hostfw_options = $hostfw_conf->{options} || {};
3149 # fixme: what log level should we use here?
3150 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
3152 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT");
3154 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
3155 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
3157 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN");
3159 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
3160 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT");
3162 generate_std_chains($ruleset, $hostfw_options, $ipversion);
3164 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
3166 my $ipset_ruleset = {};
3168 if ($hostfw_enable) {
3169 eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
3170 warn $@ if $@; # just to be sure - should not happen
3173 # generate firewall rules for QEMU VMs
3174 foreach my $vmid (keys %{$vmdata->{qemu}}) {
3176 my $conf = $vmdata->{qemu}->{$vmid};
3177 my $vmfw_conf = $vmfw_configs->{$vmid};
3178 return if !$vmfw_conf;
3180 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3182 foreach my $netid (keys %$conf) {
3183 next if $netid !~ m/^net(\d+)$/;
3184 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3185 next if !$net->{firewall};
3186 my $iface = "tap${vmid}i$1";
3188 my $macaddr = $net->{macaddr};
3189 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3190 $vmfw_conf, $vmid, 'IN
', $ipversion);
3191 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3192 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3195 warn $@ if $@; # just to be sure - should not happen
3198 # generate firewall rules for LXC containers
3199 foreach my $vmid (keys %{$vmdata->{lxc}}) {
3201 my $conf = $vmdata->{lxc}->{$vmid};
3202 my $vmfw_conf = $vmfw_configs->{$vmid};
3203 return if !$vmfw_conf;
3205 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf);
3207 if ($vmfw_conf->{options}->{enable}) {
3208 foreach my $netid (keys %$conf) {
3209 next if $netid !~ m/^net(\d+)$/;
3210 my $net = PVE::LXC::parse_lxc_network($conf->{$netid});
3211 next if !$net->{firewall};
3212 my $iface = "veth${vmid}i$1";
3213 my $macaddr = $net->{hwaddr};
3214 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3215 $vmfw_conf, $vmid, 'IN
', $ipversion);
3216 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3217 $vmfw_conf, $vmid, 'OUT
', $ipversion);
3221 warn $@ if $@; # just to be sure - should not happen
3224 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
3225 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS");
3228 generate_ipset_chains($ipset_ruleset, undef, $cluster_conf);
3230 return ($ruleset, $ipset_ruleset);
3233 sub get_ruleset_status {
3234 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
3236 my $statushash = {};
3238 foreach my $chain (sort keys %$ruleset) {
3239 my $sig = &$digest_fn($ruleset->{$chain});
3241 $statushash->{$chain}->{sig} = $sig;
3243 my $oldsig = $active_chains->{$chain};
3244 if (!defined($oldsig)) {
3245 $statushash->{$chain}->{action} = 'create
';
3247 if ($oldsig eq $sig) {
3248 $statushash->{$chain}->{action} = 'exists';
3250 $statushash->{$chain}->{action} = 'update
';
3253 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
3254 foreach my $cmd (@{$ruleset->{$chain}}) {
3255 print "\t$cmd\n" if $verbose;
3259 foreach my $chain (sort keys %$active_chains) {
3260 if (!defined($ruleset->{$chain})) {
3261 my $sig = $active_chains->{$chain};
3262 $statushash->{$chain}->{action} = 'delete';
3263 $statushash->{$chain}->{sig} = $sig;
3264 print "delete $chain ($sig)\n" if $verbose;
3271 sub print_sig_rule {
3272 my ($chain, $sig) = @_;
3274 # We just use this to store a SHA1 checksum used to detect changes
3275 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
3278 sub get_ruleset_cmdlist {
3279 my ($ruleset, $verbose, $iptablescmd) = @_;
3281 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
3283 my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
3284 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
3286 # create missing chains first
3287 foreach my $chain (sort keys %$ruleset) {
3288 my $stat = $statushash->{$chain};
3289 die "internal error" if !$stat;
3290 next if $stat->{action} ne 'create
';
3292 $cmdlist .= ":$chain - [0:0]\n";
3295 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3296 my $chain = "PVEFW-$h";
3297 if ($ruleset->{$chain} && !$hooks->{$h}) {
3298 $cmdlist .= "-A $h -j $chain\n";
3302 foreach my $chain (sort keys %$ruleset) {
3303 my $stat = $statushash->{$chain};
3304 die "internal error" if !$stat;
3306 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
3307 $cmdlist .= "-F $chain\n";
3308 foreach my $cmd (@{$ruleset->{$chain}}) {
3309 $cmdlist .= "$cmd\n";
3311 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
3312 } elsif ($stat->{action
} eq 'delete') {
3313 die "internal error"; # this should not happen
3314 } elsif ($stat->{action
} eq 'exists') {
3317 die "internal error - unknown status '$stat->{action}'";
3321 foreach my $chain (keys %$statushash) {
3322 next if $statushash->{$chain}->{action
} ne 'delete';
3323 $cmdlist .= "-F $chain\n";
3325 foreach my $chain (keys %$statushash) {
3326 next if $statushash->{$chain}->{action
} ne 'delete';
3327 next if $chain eq 'PVEFW-INPUT';
3328 next if $chain eq 'PVEFW-OUTPUT';
3329 next if $chain eq 'PVEFW-FORWARD';
3330 $cmdlist .= "-X $chain\n";
3333 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
3335 $cmdlist .= "COMMIT\n";
3337 return wantarray ?
($cmdlist, $changes) : $cmdlist;
3340 sub get_ipset_cmdlist
{
3341 my ($ruleset, $verbose) = @_;
3345 my $delete_cmdlist = "";
3347 my $active_chains = ipset_get_chains
();
3348 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
3350 # remove stale _swap chains
3351 foreach my $chain (keys %$active_chains) {
3352 if ($chain =~ m/^PVEFW-\S+_swap$/) {
3353 $cmdlist .= "destroy $chain\n";
3357 foreach my $chain (keys %$ruleset) {
3358 my $stat = $statushash->{$chain};
3359 die "internal error" if !$stat;
3361 if ($stat->{action
} eq 'create') {
3362 foreach my $cmd (@{$ruleset->{$chain}}) {
3363 $cmdlist .= "$cmd\n";
3368 foreach my $chain (keys %$ruleset) {
3369 my $stat = $statushash->{$chain};
3370 die "internal error" if !$stat;
3372 if ($stat->{action
} eq 'update') {
3373 my $chain_swap = $chain."_swap";
3375 foreach my $cmd (@{$ruleset->{$chain}}) {
3376 $cmd =~ s/$chain/$chain_swap/;
3377 $cmdlist .= "$cmd\n";
3379 $cmdlist .= "swap $chain_swap $chain\n";
3380 $cmdlist .= "flush $chain_swap\n";
3381 $cmdlist .= "destroy $chain_swap\n";
3385 # the remove unused chains
3386 foreach my $chain (keys %$statushash) {
3387 next if $statushash->{$chain}->{action
} ne 'delete';
3389 $delete_cmdlist .= "flush $chain\n";
3390 $delete_cmdlist .= "destroy $chain\n";
3393 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
3395 return ($cmdlist, $delete_cmdlist, $changes);
3399 my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $verbose) = @_;
3401 enable_bridge_firewall
();
3403 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
3404 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
3406 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
3407 my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist
($rulesetv6, $verbose, "ip6tables");
3410 if ($ipset_changes) {
3411 print "ipset changes:\n";
3412 print $ipset_create_cmdlist if $ipset_create_cmdlist;
3413 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
3417 print "iptables changes:\n";
3422 print "ip6tables changes:\n";
3427 my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1";
3428 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_create_cmdlist || '');
3430 ipset_restore_cmdlist
($ipset_create_cmdlist);
3432 $tmpfile = "$pve_fw_status_dir/ip4cmdlist";
3433 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlist || '');
3435 iptables_restore_cmdlist
($cmdlist);
3437 $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
3438 PVE
::Tools
::file_set_contents
($tmpfile, $cmdlistv6 || '');
3440 ip6tables_restore_cmdlist
($cmdlistv6);
3442 $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
3443 PVE
::Tools
::file_set_contents
($tmpfile, $ipset_delete_cmdlist || '');
3445 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
3447 # test: re-read status and check if everything is up to date
3448 my $active_chains = iptables_get_chains
();
3449 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
3452 foreach my $chain (sort keys %$ruleset) {
3453 my $stat = $statushash->{$chain};
3454 if ($stat->{action
} ne 'exists') {
3455 warn "unable to update chain '$chain'\n";
3460 my $active_chainsv6 = iptables_get_chains
("ip6tables");
3461 my $statushashv6 = get_ruleset_status
($rulesetv6, $active_chainsv6, \
&iptables_chain_digest
, 0);
3463 foreach my $chain (sort keys %$rulesetv6) {
3464 my $stat = $statushashv6->{$chain};
3465 if ($stat->{action
} ne 'exists') {
3466 warn "unable to update chain '$chain'\n";
3471 die "unable to apply firewall changes\n" if $errors;
3473 update_nf_conntrack_max
($hostfw_conf);
3475 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
3479 sub update_nf_conntrack_max
{
3480 my ($hostfw_conf) = @_;
3482 my $max = 65536; # reasonable default
3484 my $options = $hostfw_conf->{options
} || {};
3486 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
3487 $max = $options->{nf_conntrack_max
};
3488 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
3491 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
3492 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
3494 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
3496 if ($current != $max) {
3497 my $hashsize = int($max/4);
3498 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
3499 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
3503 sub update_nf_conntrack_tcp_timeout_established
{
3504 my ($hostfw_conf) = @_;
3506 my $options = $hostfw_conf->{options
} || {};
3508 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
3510 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
3513 sub remove_pvefw_chains
{
3515 PVE
::Firewall
::remove_pvefw_chains_iptables
("iptables");
3516 PVE
::Firewall
::remove_pvefw_chains_iptables
("ip6tables");
3517 PVE
::Firewall
::remove_pvefw_chains_ipset
();
3521 sub remove_pvefw_chains_iptables
{
3522 my ($iptablescmd) = @_;
3524 my ($chash, $hooks) = iptables_get_chains
($iptablescmd);
3525 my $cmdlist = "*filter\n";
3527 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
3529 $cmdlist .= "-D $h -j PVEFW-$h\n";
3533 foreach my $chain (keys %$chash) {
3534 $cmdlist .= "-F $chain\n";
3537 foreach my $chain (keys %$chash) {
3538 $cmdlist .= "-X $chain\n";
3540 $cmdlist .= "COMMIT\n";
3542 if($iptablescmd eq "ip6tables") {
3543 ip6tables_restore_cmdlist
($cmdlist);
3545 iptables_restore_cmdlist
($cmdlist);
3549 sub remove_pvefw_chains_ipset
{
3551 my $ipset_chains = ipset_get_chains
();
3555 foreach my $chain (keys %$ipset_chains) {
3556 $cmdlist .= "flush $chain\n";
3557 $cmdlist .= "destroy $chain\n";
3560 ipset_restore_cmdlist
($cmdlist) if $cmdlist;
3564 my $cluster_conf = load_clusterfw_conf
();
3565 my $cluster_options = $cluster_conf->{options
};
3566 my $enable = $cluster_options->{enable
};
3570 # load required modules here
3576 my $cluster_conf = load_clusterfw_conf
();
3577 my $cluster_options = $cluster_conf->{options
};
3579 if (!$cluster_options->{enable
}) {
3580 PVE
::Firewall
::remove_pvefw_chains
();
3584 my $hostfw_conf = load_hostfw_conf
($cluster_conf);
3586 my ($ruleset, $ipset_ruleset, $rulesetv6) = compile
($cluster_conf, $hostfw_conf);
3588 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6);