9 use PVE
::Exception
qw(raise raise_param_exc);
10 use PVE
::JSONSchema
qw(register_standard_option get_standard_option);
13 use PVE
::Tools
qw($IPV4RE);
18 use PVE
::Tools
qw(run_command lock_file dir_glob_foreach);
21 my $hostfw_conf_filename = "/etc/pve/local/host.fw";
22 my $clusterfw_conf_filename = "/etc/pve/firewall/cluster.fw";
24 # dynamically include PVE::QemuServer and PVE::OpenVZ
25 # to avoid dependency problems
28 require PVE
::QemuServer
;
29 $have_qemu_server = 1;
35 $have_pve_manager = 1;
38 PVE
::JSONSchema
::register_format
('IPv4orCIDR', \
&pve_verify_ipv4_or_cidr
);
39 sub pve_verify_ipv4_or_cidr
{
40 my ($cidr, $noerr) = @_;
42 if ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
43 return $cidr if Net
::IP-
>new($cidr);
44 return undef if $noerr;
45 die Net
::IP
::Error
() . "\n";
47 return undef if $noerr;
48 die "value does not look like a valid IP address or CIDR network\n";
51 PVE
::JSONSchema
::register_standard_option
('ipset-name', {
52 description
=> "IP set name.",
54 pattern
=> '[A-Za-z][A-Za-z0-9\-\_]+',
59 PVE
::JSONSchema
::register_standard_option
('pve-fw-alias', {
60 description
=> "Alias name.",
62 pattern
=> '[A-Za-z][A-Za-z0-9\-\_]+',
67 PVE
::JSONSchema
::register_standard_option
('pve-fw-loglevel' => {
68 description
=> "Log level.",
70 enum
=> ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'nolog'],
74 my $security_group_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
75 my $ip_alias_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
77 PVE
::JSONSchema
::register_standard_option
('pve-security-group-name', {
78 description
=> "Security Group name.",
80 pattern
=> $security_group_name_pattern,
85 my $feature_ipset_nomatch = 0;
87 my (undef, undef, $release) = POSIX
::uname
();
88 if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) {
89 my ($major, $minor) = ($1, $2);
90 $feature_ipset_nomatch = 1 if ($major > 3) ||
91 ($major == 3 && $minor >= 7);
98 my $nodename = PVE
::INotify
::nodename
();
100 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
102 my $default_log_level = 'info';
104 my $log_level_hash = {
115 # imported/converted from: /usr/share/shorewall/macro.*
116 my $pve_fw_macros = {
119 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
120 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
123 "Auth (identd) traffic",
124 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
127 "Border Gateway Protocol traffic",
128 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
131 "BitTorrent traffic for BitTorrent 3.1 and earlier",
132 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
133 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
136 "BitTorrent traffic for BitTorrent 3.2 and later",
137 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
138 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
141 "Concurrent Versions System pserver traffic",
142 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
145 "Citrix/ICA traffic (ICA, ICA Browser, CGP)",
146 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
147 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
148 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
151 "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)",
152 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
153 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
156 "Distributed Checksum Clearinghouse spam filtering mechanism",
157 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
160 "Forwarded DHCP traffic (bidirectional)",
161 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
162 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
165 "Domain Name System traffic (upd and tcp)",
166 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
167 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
170 "Distributed Compiler service",
171 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
174 "File Transfer Protocol",
175 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
178 "Finger protocol (RFC 742)",
179 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
182 "GNUnet secure peer-to-peer networking traffic",
183 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
184 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
185 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
186 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
189 "Generic Routing Encapsulation tunneling protocol (bidirectional)",
190 { action
=> 'PARAM', proto
=> '47' },
191 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
194 "Git distributed revision control traffic",
195 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
198 "OpenPGP HTTP keyserver protocol traffic",
199 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
202 "Hypertext Transfer Protocol (WWW)",
203 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
206 "Hypertext Transfer Protocol (WWW) over SSL",
207 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
210 "Internet Cache Protocol V2 (Squid) traffic",
211 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
214 "AOL Instant Messenger traffic",
215 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
218 "Internet Message Access Protocol",
219 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
222 "Internet Message Access Protocol over SSL",
223 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
226 "IPIP capsulation traffic (bidirectional)",
227 { action
=> 'PARAM', proto
=> '94' },
228 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
231 "IPsec traffic (bidirectional)",
232 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
233 { action
=> 'PARAM', proto
=> '50' },
234 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
235 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
238 "IPsec authentication (AH) traffic (bidirectional)",
239 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
240 { action
=> 'PARAM', proto
=> '51' },
241 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
242 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
245 "IPsec traffic and Nat-Traversal (bidirectional)",
246 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
247 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
248 { action
=> 'PARAM', proto
=> '50' },
249 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
250 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
251 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
254 "Internet Relay Chat traffic",
255 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
258 "HP Jetdirect printing",
259 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
262 "Layer 2 Tunneling Protocol traffic",
263 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
264 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
267 "Lightweight Directory Access Protocol traffic",
268 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
271 "Secure Lightweight Directory Access Protocol traffic",
272 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
275 "Microsoft Notification Protocol",
276 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
279 "Microsoft SQL Server",
280 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
283 "Mail traffic (SMTP, SMTPS, Submission)",
284 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
285 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
286 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
289 "Munin networked resource monitoring traffic",
290 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
294 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
297 "NNTP traffic (Usenet).",
298 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
301 "Encrypted NNTP traffic (Usenet)",
302 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
305 "Network Time Protocol (ntpd)",
306 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
309 "Bi-directional NTP (for NTP peers)",
310 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
311 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
314 "OSPF multicast traffic",
315 { action
=> 'PARAM', proto
=> '89' },
319 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
322 "Symantec PCAnywere (tm)",
323 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
324 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
328 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
331 "Encrypted POP3 traffic",
332 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
335 "Point-to-Point Tunneling Protocol",
336 { action
=> 'PARAM', proto
=> '47' },
337 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
338 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
342 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
346 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
349 "Line Printer protocol printing",
350 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
353 "Microsoft Remote Desktop Protocol traffic",
354 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
357 "Routing Information Protocol (bidirectional)",
358 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
359 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
362 "BIND remote management protocol",
363 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
366 "Razor Antispam System",
367 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
370 "Remote time retrieval (rdate)",
371 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
375 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
378 "SANE network scanning",
379 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
382 "Microsoft SMB traffic",
383 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
384 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
385 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
386 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
389 "Microsoft SMB traffic (bidirectional)",
390 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
391 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
392 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
393 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
394 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
395 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
396 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
397 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
400 "Samba Web Administration Tool",
401 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
404 "Simple Mail Transfer Protocol",
405 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
408 "Encrypted Simple Mail Transfer Protocol",
409 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
412 "Simple Network Management Protocol",
413 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
414 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
417 "Spam Assassin SPAMD traffic",
418 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
421 "Secure shell traffic",
422 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
425 "Subversion server (svnserve)",
426 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
429 "SixXS IPv6 Deployment and Tunnel Broker",
430 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
431 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
432 { action
=> 'PARAM', proto
=> '41' },
433 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
436 "Squid web proxy traffic",
437 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
440 "Mail message submission traffic",
441 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
444 "Syslog protocol (RFC 5424) traffic",
445 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
446 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
449 "Trivial File Transfer Protocol traffic",
450 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
454 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
458 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
461 "RFC 868 Time protocol",
462 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
465 "Traceroute (for up to 30 hops) traffic",
466 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
467 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
470 "VNC traffic for VNC display's 0 - 9",
471 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
474 "VNC traffic from Vncservers to Vncviewers in listen mode",
475 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
478 "WWW traffic (HTTP and HTTPS)",
479 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
480 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
483 "Web Cache/Proxy traffic (port 8080)",
484 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
488 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
491 "Whois (nicname, RFC 3912) traffic",
492 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
496 my $pve_fw_parsed_macros;
497 my $pve_fw_macro_descr;
498 my $pve_fw_preferred_macro_names = {};
500 my $pve_std_chains = {
501 'PVEFW-SET-ACCEPT-MARK' => [
502 "-j MARK --set-mark 1",
504 'PVEFW-DropBroadcast' => [
505 # same as shorewall 'Broadcast'
506 # simply DROP BROADCAST/MULTICAST/ANYCAST
507 # we can use this to reduce logging
508 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
509 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
510 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
511 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
514 # same as shorewall 'reject'
515 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
516 { action
=> 'DROP', source
=> '224.0.0.0/4' },
517 { action
=> 'DROP', proto
=> 'icmp' },
518 "-p tcp -j REJECT --reject-with tcp-reset",
519 "-p udp -j REJECT --reject-with icmp-port-unreachable",
520 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
521 "-j REJECT --reject-with icmp-host-prohibited",
524 # same as shorewall 'Drop', which is equal to DROP,
525 # but REJECT/DROP some packages to reduce logging,
526 # and ACCEPT critical ICMP types
527 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
528 # we are not interested in BROADCAST/MULTICAST/ANYCAST
529 { action
=> 'PVEFW-DropBroadcast' },
530 # ACCEPT critical ICMP types
531 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
532 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
533 # Drop packets with INVALID state
534 "-m conntrack --ctstate INVALID -j DROP",
535 # Drop Microsoft SMB noise
536 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
537 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
538 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
539 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
540 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
541 # Drop new/NotSyn traffic so that it doesn't get logged
542 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
544 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
547 # same as shorewall 'Reject', which is equal to Reject,
548 # but REJECT/DROP some packages to reduce logging,
549 # and ACCEPT critical ICMP types
550 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
551 # we are not interested in BROADCAST/MULTICAST/ANYCAST
552 { action
=> 'PVEFW-DropBroadcast' },
553 # ACCEPT critical ICMP types
554 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
555 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
556 # Drop packets with INVALID state
557 "-m conntrack --ctstate INVALID -j DROP",
558 # Drop Microsoft SMB noise
559 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
560 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
561 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
562 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
563 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
564 # Drop new/NotSyn traffic so that it doesn't get logged
565 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
567 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
569 'PVEFW-tcpflags' => [
570 # same as shorewall tcpflags action.
571 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
572 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
573 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
574 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
575 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
576 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
579 # same as shorewall smurfs action
580 # Filter packets for smurfs (packets with a broadcast address as the source).
581 "-s 0.0.0.0/32 -j RETURN",
582 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
583 "-s 224.0.0.0/4 -g PVEFW-smurflog",
587 # iptables -p icmp -h
588 my $icmp_type_names = {
591 'destination-unreachable' => 1,
592 'network-unreachable' => 1,
593 'host-unreachable' => 1,
594 'protocol-unreachable' => 1,
595 'port-unreachable' => 1,
596 'fragmentation-needed' => 1,
597 'source-route-failed' => 1,
598 'network-unknown' => 1,
600 'network-prohibited' => 1,
601 'host-prohibited' => 1,
602 'TOS-network-unreachable' => 1,
603 'TOS-host-unreachable' => 1,
604 'communication-prohibited' => 1,
605 'host-precedence-violation' => 1,
606 'precedence-cutoff' => 1,
607 'source-quench' => 1,
609 'network-redirect' => 1,
610 'host-redirect' => 1,
611 'TOS-network-redirect' => 1,
612 'TOS-host-redirect' => 1,
614 'router-advertisement' => 1,
615 'router-solicitation' => 1,
616 'time-exceeded' => 1,
617 'ttl-zero-during-transit' => 1,
618 'ttl-zero-during-reassembly' => 1,
619 'parameter-problem' => 1,
620 'ip-header-bad' => 1,
621 'required-option-missing' => 1,
622 'timestamp-request' => 1,
623 'timestamp-reply' => 1,
624 'address-mask-request' => 1,
625 'address-mask-reply' => 1,
628 sub init_firewall_macros
{
630 $pve_fw_parsed_macros = {};
632 foreach my $k (keys %$pve_fw_macros) {
633 my $lc_name = lc($k);
634 my $macro = $pve_fw_macros->{$k};
635 if (!ref($macro->[0])) {
636 $pve_fw_macro_descr->{$k} = shift @$macro;
638 $pve_fw_preferred_macro_names->{$lc_name} = $k;
639 $pve_fw_parsed_macros->{$k} = $macro;
643 init_firewall_macros
();
646 return wantarray ?
($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
651 sub get_etc_services
{
653 return $etc_services if $etc_services;
655 my $filename = "/etc/services";
657 my $fh = IO
::File-
>new($filename, O_RDONLY
);
659 warn "unable to read '$filename' - $!\n";
665 while (my $line = <$fh>) {
667 next if $line =~m/^#/;
668 next if ($line =~m/^\s*$/);
670 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
671 $services->{byid
}->{$2}->{name
} = $1;
672 $services->{byid
}->{$2}->{port
} = $2;
673 $services->{byid
}->{$2}->{$3} = 1;
674 $services->{byname
}->{$1} = $services->{byid
}->{$2};
680 $etc_services = $services;
683 return $etc_services;
688 sub get_etc_protocols
{
689 return $etc_protocols if $etc_protocols;
691 my $filename = "/etc/protocols";
693 my $fh = IO
::File-
>new($filename, O_RDONLY
);
695 warn "unable to read '$filename' - $!\n";
701 while (my $line = <$fh>) {
703 next if $line =~m/^#/;
704 next if ($line =~m/^\s*$/);
706 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
707 $protocols->{byid
}->{$2}->{name
} = $1;
708 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
714 $etc_protocols = $protocols;
716 return $etc_protocols;
719 sub parse_address_list
{
722 return if $str =~ m/^(\+)(\S+)$/; # ipset ref
723 return if $str =~ m/^${ip_alias_pattern}$/;
727 foreach my $elem (split(/,/, $str)) {
729 if (!Net
::IP-
>new($elem)) {
730 my $err = Net
::IP
::Error
();
731 die "invalid IP address: $err\n";
733 $iprange = 1 if $elem =~ m/-/;
736 die "you can use a range in a list\n" if $iprange && $count > 1;
739 sub parse_port_name_number_or_range
{
742 my $services = PVE
::Firewall
::get_etc_services
();
746 foreach my $item (split(/,/, $str)) {
748 if ($item =~ m/^(\d+):(\d+)$/) {
749 my ($port1, $port2) = ($1, $2);
750 die "invalid port '$port1'\n" if $port1 > 65535;
751 die "invalid port '$port2'\n" if $port2 > 65535;
752 } elsif ($item =~ m/^(\d+)$/) {
754 die "invalid port '$port'\n" if $port > 65535;
756 if ($icmp_type_names->{$item}) {
759 die "invalid port '$item'\n" if !$services->{byname
}->{$item};
764 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
769 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
770 sub pve_fw_verify_port_spec
{
773 parse_port_name_number_or_range
($portstr);
778 PVE
::JSONSchema
::register_format
('pve-fw-v4addr-spec', \
&pve_fw_verify_v4addr_spec
);
779 sub pve_fw_verify_v4addr_spec
{
782 parse_address_list
($list);
787 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
788 sub pve_fw_verify_protocol_spec
{
791 my $protocols = get_etc_protocols
();
793 die "unknown protocol '$proto'\n" if $proto &&
794 !(defined($protocols->{byname
}->{$proto}) ||
795 defined($protocols->{byid
}->{$proto}));
801 # helper function for API
803 sub copy_opject_with_digest
{
806 my $sha = Digest
::SHA-
>new('sha1');
809 foreach my $k (sort keys %$object) {
810 my $v = $object->{$k};
811 next if !defined($v);
813 $sha->add($k, ':', $v, "\n");
816 my $digest = $sha->hexdigest;
818 $res->{digest
} = $digest;
820 return wantarray ?
($res, $digest) : $res;
823 sub copy_list_with_digest
{
826 my $sha = Digest
::SHA-
>new('sha1');
829 foreach my $entry (@$list) {
831 foreach my $k (sort keys %$entry) {
832 my $v = $entry->{$k};
833 next if !defined($v);
835 $sha->add($k, ':', $v, "\n");
840 my $digest = $sha->hexdigest;
842 foreach my $entry (@$res) {
843 $entry->{digest
} = $digest;
846 return wantarray ?
($res, $digest) : $res;
849 my $rule_properties = {
851 description
=> "Update rule at position <pos>.",
856 digest
=> get_standard_option
('pve-config-digest'),
860 enum
=> ['in', 'out', 'group'],
863 description
=> "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
866 pattern
=> $security_group_name_pattern,
875 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
877 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
881 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
885 type
=> 'string', format
=> 'pve-fw-protocol-spec',
893 type
=> 'string', format
=> 'pve-fw-port-spec',
897 type
=> 'string', format
=> 'pve-fw-port-spec',
906 sub add_rule_properties
{
907 my ($properties) = @_;
909 foreach my $k (keys %$rule_properties) {
910 my $h = $rule_properties->{$k};
911 # copy data, so that we can modify later without side effects
912 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
918 sub delete_rule_properties
{
919 my ($rule, $delete_str) = @_;
921 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
922 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
923 if !defined($rule_properties->{$opt});
924 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
925 if $opt eq 'type' || $opt eq 'action';
926 delete $rule->{$opt};
932 my $apply_macro = sub {
933 my ($macro_name, $param, $verify) = @_;
935 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
936 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
940 foreach my $templ (@$macro_rules) {
943 foreach my $k (keys %$templ) {
944 my $v = $templ->{$k};
947 $param_used->{$k} = 1;
948 } elsif ($v eq 'DEST') {
950 $param_used->{dest
} = 1;
951 } elsif ($v eq 'SOURCE') {
952 $v = $param->{source
};
953 $param_used->{source
} = 1;
957 my $msg = "missing parameter '$k' in macro '$macro_name'";
958 raise_param_exc
({ macro => $msg }) if $verify;
963 foreach my $k (keys %$param) {
964 next if $k eq 'macro';
965 next if !defined($param->{$k});
966 next if $param_used->{$k};
967 if (defined($rule->{$k})) {
968 if ($rule->{$k} ne $param->{$k}) {
969 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
970 raise_param_exc
({ $k => $msg }) if $verify;
974 $rule->{$k} = $param->{$k};
984 my ($rule, $allow_groups) = @_;
986 my $type = $rule->{type
};
988 raise_param_exc
({ type
=> "missing property"}) if !$type;
989 raise_param_exc
({ action
=> "missing property"}) if !$rule->{action
};
991 if ($type eq 'in' || $type eq 'out') {
992 raise_param_exc
({ action
=> "unknown action '$rule->{action}'"})
993 if $rule->{action
} !~ m/^(ACCEPT|DROP|REJECT)$/;
994 } elsif ($type eq 'group') {
995 raise_param_exc
({ type
=> "security groups not allowed"})
997 raise_param_exc
({ action
=> "invalid characters in security group name"})
998 if $rule->{action
} !~ m/^${security_group_name_pattern}$/;
1000 raise_param_exc
({ type
=> "unknown rule type '$type'"});
1003 if ($rule->{iface
}) {
1004 eval { PVE
::JSONSchema
::pve_verify_iface
($rule->{iface
}); };
1005 raise_param_exc
({ iface
=> $@ }) if $@;
1008 if ($rule->{macro}) {
1009 my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})};
1010 raise_param_exc
({ macro => "unknown macro '$rule->{macro}'"}) if !$preferred_name;
1011 $rule->{macro} = $preferred_name;
1014 if ($rule->{dport
}) {
1015 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1016 raise_param_exc
({ dport
=> $@ }) if $@;
1019 if ($rule->{sport
}) {
1020 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1021 raise_param_exc
({ sport
=> $@ }) if $@;
1024 if ($rule->{source
}) {
1025 eval { parse_address_list
($rule->{source
}); };
1026 raise_param_exc
({ source
=> $@ }) if $@;
1029 if ($rule->{dest
}) {
1030 eval { parse_address_list
($rule->{dest
}); };
1031 raise_param_exc
({ dest
=> $@ }) if $@;
1034 if ($rule->{macro}) {
1035 &$apply_macro($rule->{macro}, $rule, 1);
1041 sub copy_rule_data
{
1042 my ($rule, $param) = @_;
1044 foreach my $k (keys %$rule_properties) {
1045 if (defined(my $v = $param->{$k})) {
1046 if ($v eq '' || $v eq '-') {
1062 my $bridge_firewall_enabled = 0;
1064 sub enable_bridge_firewall
{
1066 return if $bridge_firewall_enabled; # only once
1068 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1069 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1071 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1072 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1074 $bridge_firewall_enabled = 1;
1077 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1079 sub iptables_restore_cmdlist
{
1082 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
1085 sub ipset_restore_cmdlist
{
1088 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist);
1091 sub iptables_get_chains
{
1095 # check what chains we want to track
1096 my $is_pvefw_chain = sub {
1099 return 1 if $name =~ m/^PVEFW-\S+$/;
1101 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
1103 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
1105 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
1107 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/;
1108 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
1120 return if $line =~ m/^#/;
1121 return if $line =~ m/^\s*$/;
1123 if ($line =~ m/^\*(\S+)$/) {
1128 return if $table ne 'filter';
1130 if ($line =~ m/^:(\S+)\s/) {
1132 return if !&$is_pvefw_chain($chain);
1133 $res->{$chain} = "unknown";
1134 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1135 my ($chain, $sig) = ($1, $2);
1136 return if !&$is_pvefw_chain($chain);
1137 $res->{$chain} = $sig;
1138 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1141 # simply ignore the rest
1146 run_command
("/sbin/iptables-save", outfunc
=> $parser);
1148 return wantarray ?
($res, $hooks) : $res;
1151 sub iptables_chain_digest
{
1153 my $digest = Digest
::SHA-
>new('sha1');
1154 foreach my $rule (@$rules) { # order is important
1155 $digest->add($rule);
1157 return $digest->b64digest;
1160 sub ipset_chain_digest
{
1163 my $digest = Digest
::SHA-
>new('sha1');
1164 foreach my $rule (sort @$rules) { # note: sorted
1165 $digest->add($rule);
1167 return $digest->b64digest;
1170 sub ipset_get_chains
{
1178 return if $line =~ m/^#/;
1179 return if $line =~ m/^\s*$/;
1180 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1182 $line =~ s/\s+$//; # delete trailing white space
1183 push @{$chains->{$chain}}, $line;
1185 # simply ignore the rest
1190 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
1192 # compute digest for each chain
1193 foreach my $chain (keys %$chains) {
1194 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1200 sub ruleset_generate_cmdstr
{
1201 my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf) = @_;
1203 return if defined($rule->{enable
}) && !$rule->{enable
};
1205 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1207 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1208 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1212 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1213 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1215 my $source = $rule->{source
};
1216 my $dest = $rule->{dest
};
1219 if ($source =~ m/^\+/) {
1220 if ($source =~ m/^\+(${security_group_name_pattern})$/) {
1221 die "no such ipset '$1'\n" if !$cluster_conf->{ipset
}->{$1};
1222 push @cmd, "-m set --match-set PVEFW-$1 src";
1224 die "invalid security group name '$source'\n";
1226 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1227 my $alias = lc($source);
1228 my $e = $cluster_conf->{aliases
}->{$alias};
1229 die "no such alias $source\n" if !$e;
1230 push @cmd, "-s $e->{cidr}";
1231 } elsif ($source =~ m/\-/){
1232 push @cmd, "-m iprange --src-range $source";
1235 push @cmd, "-s $source";
1240 if ($dest =~ m/^\+/) {
1241 if ($dest =~ m/^\+(${security_group_name_pattern})$/) {
1242 die "no such ipset '$1'\n" if !$cluster_conf->{ipset
}->{$1};
1243 push @cmd, "-m set --match-set PVEFW-$1 dst";
1245 die "invalid security group name '$dest'\n";
1247 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1248 my $alias = lc($dest);
1249 my $e = $cluster_conf->{aliases
}->{$alias};
1250 die "no such alias $dest" if !$e;
1251 push @cmd, "-d $e->{cidr}";
1252 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1253 push @cmd, "-m iprange --dst-range $dest";
1256 push @cmd, "-d $dest";
1260 if ($rule->{proto
}) {
1261 push @cmd, "-p $rule->{proto}";
1264 $multiport++ if $nbdport > 1;
1265 $multiport++ if $nbsport > 1;
1267 push @cmd, "--match multiport" if $multiport;
1269 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1270 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1272 if ($rule->{dport
}) {
1273 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1274 # Note: we use dport to store --icmp-type
1275 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
1276 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1279 if ($multiport == 2) {
1280 push @cmd, "--ports $rule->{dport}";
1282 push @cmd, "--dports $rule->{dport}";
1285 push @cmd, "--dport $rule->{dport}";
1290 if ($rule->{sport
}) {
1292 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1294 push @cmd, "--sport $rule->{sport}";
1297 } elsif ($rule->{dport
} || $rule->{sport
}) {
1298 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
1299 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
1302 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1304 if (my $action = $rule->{action
}) {
1305 $action = $actions->{$action} if defined($actions->{$action});
1306 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1307 push @cmd, $goto ?
"-g $action" : "-j $action";
1310 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1313 sub ruleset_generate_rule
{
1314 my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf) = @_;
1318 if ($rule->{macro}) {
1319 $rules = &$apply_macro($rule->{macro}, $rule);
1324 # update all or nothing
1327 foreach my $tmp (@$rules) {
1328 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $tmp, $actions, $goto, $cluster_conf)) {
1329 push @cmds, $cmdstr;
1333 foreach my $cmdstr (@cmds) {
1334 ruleset_addrule
($ruleset, $chain, $cmdstr);
1338 sub ruleset_generate_rule_insert
{
1339 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1341 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1343 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
1344 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1348 sub ruleset_create_chain
{
1349 my ($ruleset, $chain) = @_;
1351 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1352 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1354 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1356 $ruleset->{$chain} = [];
1359 sub ruleset_chain_exist
{
1360 my ($ruleset, $chain) = @_;
1362 return $ruleset->{$chain} ?
1 : undef;
1365 sub ruleset_addrule
{
1366 my ($ruleset, $chain, $rule) = @_;
1368 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1370 push @{$ruleset->{$chain}}, "-A $chain $rule";
1373 sub ruleset_insertrule
{
1374 my ($ruleset, $chain, $rule) = @_;
1376 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1378 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1381 sub get_log_rule_base
{
1382 my ($chain, $vmid, $msg, $loglevel) = @_;
1384 die "internal error - no log level" if !defined($loglevel);
1386 $vmid = 0 if !defined($vmid);
1388 # Note: we use special format for prefix to pass further
1389 # info to log daemon (VMID, LOGVELEL and CHAIN)
1391 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1394 sub ruleset_addlog {
1395 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1397 return if !defined($loglevel);
1399 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1401 $logrule = "$rule $logrule" if defined($rule);
1403 ruleset_addrule($ruleset, $chain, $logrule);
1406 sub ruleset_add_chain_policy {
1407 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
1409 if ($policy eq 'ACCEPT') {
1411 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
1412 { ACCEPT => $accept_action});
1414 } elsif ($policy eq 'DROP') {
1416 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1418 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1420 ruleset_addrule($ruleset, $chain, "-j DROP
");
1421 } elsif ($policy eq 'REJECT') {
1422 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1424 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1426 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1429 die "internal error
: unknown policy
'$policy'";
1433 sub ruleset_chain_add_conn_filters {
1434 my ($ruleset, $chain, $accept) = @_;
1436 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1437 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1440 sub ruleset_chain_add_input_filters {
1441 my ($ruleset, $chain, $options, $cluster_conf, $loglevel) = @_;
1443 if ($cluster_conf->{ipset}->{blacklist}){
1444 ruleset_addlog($ruleset, $chain, 0, "DROP
: ", $loglevel, "-m set
--match-set PVEFW-blacklist src
");
1445 ruleset_addrule($ruleset, $chain, "-m set
--match-set PVEFW-blacklist src
-j DROP
");
1448 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1449 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1452 if ($options->{tcpflags}) {
1453 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1457 sub ruleset_create_vm_chain {
1458 my ($ruleset, $chain, $options, $host_options, $macaddr, $direction) = @_;
1460 ruleset_create_chain($ruleset, $chain);
1461 my $accept = generate_nfqueue($options);
1463 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1464 if ($direction eq 'OUT') {
1465 ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK',
1466 proto => 'udp', sport => 68, dport => 67 });
1468 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT',
1469 proto => 'udp', sport => 67, dport => 68 });
1473 if ($direction eq 'OUT') {
1474 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1475 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1477 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1481 sub ruleset_add_group_rule {
1482 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action) = @_;
1484 my $group = $rule->{action};
1485 my $group_chain = "GROUP-
$group-$direction";
1486 if(!ruleset_chain_exist($ruleset, $group_chain)){
1487 generate_group_rules($ruleset, $cluster_conf, $group);
1490 if ($direction eq 'OUT' && $rule->{iface_out}) {
1491 ruleset_addrule($ruleset, $chain, "-o
$rule->{iface_out
} -j
$group_chain");
1492 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1493 ruleset_addrule($ruleset, $chain, "-i
$rule->{iface_in
} -j
$group_chain");
1495 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1498 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$action");
1501 sub ruleset_generate_vm_rules {
1502 my ($ruleset, $rules, $cluster_conf, $chain, $netid, $direction, $options) = @_;
1504 my $lc_direction = lc($direction);
1506 my $in_accept = generate_nfqueue($options);
1508 foreach my $rule (@$rules) {
1509 next if $rule->{iface} && $rule->{iface} ne $netid;
1510 next if !$rule->{enable};
1511 if ($rule->{type} eq 'group') {
1512 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
1513 $direction eq 'OUT' ? 'RETURN' : $in_accept);
1515 next if $rule->{type} ne $lc_direction;
1517 if ($direction eq 'OUT') {
1518 ruleset_generate_rule($ruleset, $chain, $rule,
1519 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" },
1520 undef, $cluster_conf);
1522 ruleset_generate_rule($ruleset, $chain, $rule,
1523 { ACCEPT => $in_accept , REJECT => "PVEFW-reject
" },
1524 undef, $cluster_conf);
1532 sub generate_nfqueue {
1535 if ($options->{ips}) {
1536 my $action = "NFQUEUE
";
1537 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1538 if (defined($3) && defined($1)) {
1539 $action .= " --queue-balance
$1:$3";
1540 } elsif (defined($1)) {
1541 $action .= " --queue-num
$1";
1544 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
1551 sub ruleset_generate_vm_ipsrules {
1552 my ($ruleset, $options, $direction, $iface) = @_;
1554 if ($options->{ips} && $direction eq 'IN') {
1555 my $nfqueue = generate_nfqueue($options);
1557 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS
")) {
1558 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1561 ruleset_addrule($ruleset, "PVEFW-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1565 sub generate_venet_rules_direction {
1566 my ($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
1568 my $lc_direction = lc($direction);
1570 my $rules = $vmfw_conf->{rules};
1572 my $options = $vmfw_conf->{options};
1573 my $hostfw_options = $vmfw_conf->{options};
1574 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1576 my $chain = "venet0-
$vmid-$direction";
1578 ruleset_create_vm_chain($ruleset, $chain, $options, $hostfw_options, undef, $direction);
1580 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $chain, 'venet', $direction);
1585 if ($direction eq 'OUT') {
1586 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1588 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1591 my $accept = generate_nfqueue($options);
1592 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1593 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1595 if ($direction eq 'OUT') {
1596 ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT
", {
1599 iface_in => 'venet0'});
1601 ruleset_generate_rule($ruleset, "PVEFW-VENET-IN
", {
1604 iface_out => 'venet0'});
1608 sub generate_tap_rules_direction {
1609 my ($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction) = @_;
1611 my $lc_direction = lc($direction);
1613 my $rules = $vmfw_conf->{rules};
1615 my $options = $vmfw_conf->{options};
1616 my $hostfw_options = $hostfw_conf->{options};
1617 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1619 my $tapchain = "$iface-$direction";
1621 ruleset_create_vm_chain($ruleset, $tapchain, $options, $hostfw_options, $macaddr, $direction);
1623 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $tapchain, $netid, $direction, $options);
1625 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
1630 if ($direction eq 'OUT') {
1631 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1633 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1636 my $accept = generate_nfqueue($options);
1637 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1638 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
1640 # plug the tap chain to bridge chain
1641 if ($direction eq 'IN') {
1642 ruleset_addrule($ruleset, "PVEFW-FWBR-IN
",
1643 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
1645 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT
",
1646 "-m physdev
--physdev-is-bridged
--physdev-in
$iface -j
$tapchain");
1650 sub enable_host_firewall {
1651 my ($ruleset, $hostfw_conf, $cluster_conf) = @_;
1653 my $options = $hostfw_conf->{options};
1654 my $cluster_options = $cluster_conf->{options};
1655 my $rules = $hostfw_conf->{rules};
1656 my $cluster_rules = $cluster_conf->{rules};
1658 # host inbound firewall
1659 my $chain = "PVEFW-HOST-IN
";
1660 ruleset_create_chain($ruleset, $chain);
1662 my $loglevel = get_option_log_level($options, "log_level_in
");
1664 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
1666 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
1667 ruleset_chain_add_input_filters($ruleset, $chain, $options, $cluster_conf, $loglevel);
1669 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1670 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1671 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1673 # we use RETURN because we need to check also tap rules
1674 my $accept_action = 'RETURN';
1676 # add host rules first, so that cluster wide rules can be overwritten
1677 foreach my $rule (@$rules, @$cluster_rules) {
1678 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
1679 if ($rule->{type} eq 'group') {
1680 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action);
1681 } elsif ($rule->{type} eq 'in') {
1682 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
1683 undef, $cluster_conf);
1685 delete $rule->{iface_in};
1688 # implement input policy
1689 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
1690 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1692 # host outbound firewall
1693 $chain = "PVEFW-HOST-OUT
";
1694 ruleset_create_chain($ruleset, $chain);
1696 $loglevel = get_option_log_level($options, "log_level_out
");
1698 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
1700 ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT');
1702 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1703 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1704 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1706 # we use RETURN because we may want to check other thigs later
1707 $accept_action = 'RETURN';
1709 # add host rules first, so that cluster wide rules can be overwritten
1710 foreach my $rule (@$rules, @$cluster_rules) {
1711 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
1712 if ($rule->{type} eq 'group') {
1713 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action);
1714 } elsif ($rule->{type} eq 'out') {
1715 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" },
1716 undef, $cluster_conf);
1718 delete $rule->{iface_out};
1721 # implement output policy
1722 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
1723 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1725 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
1726 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
1729 sub generate_group_rules {
1730 my ($ruleset, $cluster_conf, $group) = @_;
1732 my $rules = $cluster_conf->{groups}->{$group};
1735 warn "no such security group
'$group'\n";
1736 $rules = []; # create empty chain
1739 my $chain = "GROUP-
${group
}-IN
";
1741 ruleset_create_chain($ruleset, $chain);
1742 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1744 foreach my $rule (@$rules) {
1745 next if $rule->{type} ne 'in';
1746 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1749 $chain = "GROUP-
${group
}-OUT
";
1751 ruleset_create_chain($ruleset, $chain);
1752 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1754 foreach my $rule (@$rules) {
1755 next if $rule->{type} ne 'out';
1756 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1757 # check also other tap rules later
1758 ruleset_generate_rule($ruleset, $chain, $rule,
1759 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1764 my $valid_netdev_names = {};
1765 for (my $i = 0; $i < $MAX_NETS; $i++) {
1766 $valid_netdev_names->{"net
$i"} = 1;
1770 my ($line, $allow_iface, $allow_groups) = @_;
1772 my ($type, $action, $macro, $iface, $source, $dest, $proto, $dport, $sport);
1776 # we can add single line comments to the end of the rule
1777 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
1779 # we can disable a rule when prefixed with '|'
1782 $enable = 0 if $line =~ s/^\|//;
1784 $line =~ s/^(\S+)\s+(\S+)\s*// ||
1785 die "unable to parse rule
: $line\n";
1790 if ($type eq 'in' || $type eq 'out') {
1791 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1793 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1795 my $preferred_name = $pve_fw_preferred_macro_names->{lc($1)};
1796 die "unknown
macro '$1'\n" if !$preferred_name;
1797 $macro = $preferred_name;
1799 die "unknown action
'$action'\n";
1801 } elsif ($type eq 'group') {
1802 die "groups disabled
\n" if !$allow_groups;
1803 die "invalid characters
in group name
\n" if $action !~ m/^${security_group_name_pattern}$/;
1805 die "unknown rule type
'$type'\n";
1808 while (length($line)) {
1809 if ($line =~ s/^-i (\S+)\s*//) {
1810 die "parameter
-i
not allowed
\n" if !$allow_iface;
1812 PVE::JSONSchema::pve_verify_iface($iface);
1816 last if $type eq 'group';
1818 if ($line =~ s/^-p (\S+)\s*//) {
1820 pve_fw_verify_protocol_spec($proto);
1823 if ($line =~ s/^-dport (\S+)\s*//) {
1825 parse_port_name_number_or_range($dport);
1828 if ($line =~ s/^-sport (\S+)\s*//) {
1830 parse_port_name_number_or_range($sport);
1833 if ($line =~ s/^-source (\S+)\s*//) {
1835 parse_address_list($source);
1838 if ($line =~ s/^-dest (\S+)\s*//) {
1840 parse_address_list($dest);
1847 die "unable to parse rule parameters
: $line\n" if length($line);
1852 comment => $comment,
1864 sub parse_vmfw_option {
1869 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
1871 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
1874 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1876 $value = $2 ? lc($3) : '';
1877 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1880 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
1885 die "can
't parse option '$line'\n"
1888 return ($opt, $value);
1891 sub parse_hostfw_option {
1896 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1898 if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1901 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1903 $value = $2 ? lc($3) : '';
1904 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
1909 die "can't parse option
'$line'\n"
1912 return ($opt, $value);
1915 sub parse_clusterfw_option {
1920 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
1923 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1928 die "can
't parse option '$line'\n"
1931 return ($opt, $value);
1934 sub parse_clusterfw_alias {
1937 # we can add single line comments to the end of the line
1938 my $comment = decode('utf8
', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
1940 if ($line =~ m/^(\S+)\s(\S+)$/) {
1941 my ($name, $cidr) = ($1, $2);
1943 pve_verify_ipv4_or_cidr($cidr);
1948 $data->{comment} = $comment if $comment;
1955 sub parse_vm_fw_rules {
1956 my ($filename, $fh) = @_;
1958 my $res = { rules => [], options => {}};
1962 while (defined(my $line = <$fh>)) {
1963 next if $line =~ m/^#/;
1964 next if $line =~ m/^\s*$/;
1966 my $linenr = $fh->input_line_number();
1967 my $prefix = "$filename (line $linenr)";
1969 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1971 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1975 warn "$prefix: skip line - no section";
1979 next if !$res->{$section}; # skip undefined section
1981 if ($section eq 'options
') {
1983 my ($opt, $value) = parse_vmfw_option($line);
1984 $res->{options}->{$opt} = $value;
1986 warn "$prefix: $@" if $@;
1991 eval { $rule = parse_fw_rule($line, 1, 1); };
1993 warn "$prefix: $err";
1997 push @{$res->{$section}}, $rule;
2003 sub parse_host_fw_rules {
2004 my ($filename, $fh) = @_;
2006 my $res = { rules => [], options => {}};
2010 while (defined(my $line = <$fh>)) {
2011 next if $line =~ m/^#/;
2012 next if $line =~ m/^\s*$/;
2014 my $linenr = $fh->input_line_number();
2015 my $prefix = "$filename (line $linenr)";
2017 if ($line =~ m/^\[(\S+)\]\s*$/i) {
2019 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
2023 warn "$prefix: skip line - no section";
2027 next if !$res->{$section}; # skip undefined section
2029 if ($section eq 'options
') {
2031 my ($opt, $value) = parse_hostfw_option($line);
2032 $res->{options}->{$opt} = $value;
2034 warn "$prefix: $@" if $@;
2039 eval { $rule = parse_fw_rule($line, 1, 1); };
2041 warn "$prefix: $err";
2045 push @{$res->{$section}}, $rule;
2051 sub parse_cluster_fw_rules {
2052 my ($filename, $fh) = @_;
2062 group_comments => {},
2064 ipset_comments => {},
2067 while (defined(my $line = <$fh>)) {
2068 next if $line =~ m/^#/;
2069 next if $line =~ m/^\s*$/;
2071 my $linenr = $fh->input_line_number();
2072 my $prefix = "$filename (line $linenr)";
2074 if ($line =~ m/^\[options\]$/i) {
2075 $section = 'options
';
2079 if ($line =~ m/^\[aliases\]$/i) {
2080 $section = 'aliases
';
2084 if ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i) {
2085 $section = 'groups
';
2088 $res->{$section}->{$group} = [];
2089 $res->{group_comments}->{$group} = decode('utf8
', $comment)
2094 if ($line =~ m/^\[rules\]$/i) {
2099 if ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i) {
2103 $res->{$section}->{$group} = [];
2104 $res->{ipset_comments}->{$group} = decode('utf8
', $comment)
2110 warn "$prefix: skip line - no section\n";
2114 if ($section eq 'options
') {
2116 my ($opt, $value) = parse_clusterfw_option($line);
2117 $res->{options}->{$opt} = $value;
2119 warn "$prefix: $@" if $@;
2120 } elsif ($section eq 'aliases
') {
2122 my $data = parse_clusterfw_alias($line);
2123 $res->{aliases}->{lc($data->{name})} = $data;
2125 warn "$prefix: $@" if $@;
2126 } elsif ($section eq 'rules
') {
2128 eval { $rule = parse_fw_rule($line, 1, 1); };
2130 warn "$prefix: $err";
2133 push @{$res->{$section}}, $rule;
2134 } elsif ($section eq 'groups
') {
2136 eval { $rule = parse_fw_rule($line, 0, 0); };
2138 warn "$prefix: $err";
2141 push @{$res->{$section}->{$group}}, $rule;
2142 } elsif ($section eq 'ipset
') {
2143 # we can add single line comments to the end of the rule
2144 my $comment = decode('utf8
', $1) if $line =~ s/#\s*(.*?)\s*$//;
2146 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2150 if($cidr !~ m/^${ip_alias_pattern}$/) {
2153 eval { pve_verify_ipv4_or_cidr($cidr); };
2155 warn "$prefix: $cidr - $err";
2160 my $entry = { cidr => $cidr };
2161 $entry->{nomatch} = 1 if $nomatch;
2162 $entry->{comment} = $comment if $comment;
2164 push @{$res->{$section}->{$group}}, $entry;
2172 my ($code, @param) = @_;
2176 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2183 sub read_local_vm_config {
2188 my $vmdata = { openvz => $openvz, qemu => $qemu };
2190 my $vmlist = PVE::Cluster::get_vmlist();
2191 return $vmdata if !$vmlist || !$vmlist->{ids};
2192 my $ids = $vmlist->{ids};
2194 foreach my $vmid (keys %$ids) {
2195 next if !$vmid; # skip VE0
2196 my $d = $ids->{$vmid};
2197 next if !$d->{node} || $d->{node} ne $nodename;
2198 next if !$d->{type};
2199 if ($d->{type} eq 'openvz
') {
2200 if ($have_pve_manager) {
2201 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
2202 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2203 $openvz->{$vmid} = $conf;
2206 } elsif ($d->{type} eq 'qemu
') {
2207 if ($have_qemu_server) {
2208 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2209 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2210 $qemu->{$vmid} = $conf;
2219 sub load_vmfw_conf {
2220 my ($vmid, $dir) = @_;
2224 $dir = "/etc/pve/firewall" if !defined($dir);
2226 my $filename = "$dir/$vmid.fw";
2227 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2228 $vmfw_conf = parse_vm_fw_rules($filename, $fh);
2234 my $format_rules = sub {
2235 my ($rules, $allow_iface) = @_;
2239 foreach my $rule (@$rules) {
2240 if ($rule->{type} eq 'in' || $rule->{type} eq 'out
' || $rule->{type} eq 'group
') {
2241 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2242 $raw .= uc($rule->{type});
2243 if ($rule->{macro}) {
2244 $raw .= " $rule->{macro}($rule->{action})";
2246 $raw .= " " . $rule->{action};
2248 if ($allow_iface && $rule->{iface}) {
2249 $raw .= " -i $rule->{iface}";
2252 if ($rule->{type} ne 'group
') {
2253 $raw .= " -source $rule->{source}" if $rule->{source};
2254 $raw .= " -dest $rule->{dest}" if $rule->{dest};
2255 $raw .= " -p $rule->{proto}" if $rule->{proto};
2256 $raw .= " -dport $rule->{dport}" if $rule->{dport};
2257 $raw .= " -sport $rule->{sport}" if $rule->{sport};
2260 $raw .= " # " . encode('utf8
', $rule->{comment})
2261 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
2264 die "unknown rule type '$rule->{type
}'";
2271 my $format_options = sub {
2276 $raw .= "[OPTIONS]\n\n";
2277 foreach my $opt (keys %$options) {
2278 $raw .= "$opt: $options->{$opt}\n";
2285 my $format_aliases = sub {
2290 $raw .= "[ALIASES]\n\n";
2291 foreach my $k (keys %$aliases) {
2292 my $e = $aliases->{$k};
2293 $raw .= "$e->{name} $e->{cidr}";
2294 $raw .= " # " . encode('utf8
', $e->{comment})
2295 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
2303 my $format_ipset = sub {
2309 foreach my $entry (@$options) {
2310 $nethash->{$entry->{cidr}} = $entry;
2313 foreach my $cidr (sort keys %$nethash) {
2314 my $entry = $nethash->{$cidr};
2315 my $line = $entry->{nomatch} ? '!' : '';
2316 $line .= $entry->{cidr};
2317 $line .= " # " . encode('utf8
', $entry->{comment})
2318 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
2325 sub save_vmfw_conf {
2326 my ($vmid, $vmfw_conf) = @_;
2330 my $options = $vmfw_conf->{options};
2331 $raw .= &$format_options($options) if scalar(keys %$options);
2333 my $rules = $vmfw_conf->{rules} || [];
2334 if (scalar(@$rules)) {
2335 $raw .= "[RULES]\n\n";
2336 $raw .= &$format_rules($rules, 1);
2340 my $filename = "/etc/pve/firewall/$vmid.fw";
2341 PVE::Tools::file_set_contents($filename, $raw);
2344 sub read_vm_firewall_configs {
2345 my ($vmdata, $dir) = @_;
2347 my $vmfw_configs = {};
2349 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
2350 my $vmfw_conf = load_vmfw_conf($vmid, $dir);
2351 next if !$vmfw_conf->{options}; # skip if file does not exists
2352 $vmfw_configs->{$vmid} = $vmfw_conf;
2355 return $vmfw_configs;
2358 sub get_option_log_level {
2359 my ($options, $k) = @_;
2361 my $v = $options->{$k};
2362 $v = $default_log_level if !defined($v);
2364 return undef if $v eq '' || $v eq 'nolog
';
2366 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2368 return $v if ($v >= 0) && ($v <= 7);
2370 warn "unknown log level ($k = '$v')\n";
2375 sub generate_std_chains {
2376 my ($ruleset, $options) = @_;
2378 my $loglevel = get_option_log_level($options, 'smurf_log_level
');
2380 # same as shorewall smurflog.
2381 my $chain = 'PVEFW-smurflog
';
2382 $pve_std_chains->{$chain} = [];
2384 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2385 push @{$pve_std_chains->{$chain}}, "-j DROP";
2387 # same as shorewall logflags action.
2388 $loglevel = get_option_log_level($options, 'tcp_flags_log_level
');
2389 $chain = 'PVEFW-logflags
';
2390 $pve_std_chains->{$chain} = [];
2392 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
2393 push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel;
2394 push @{$pve_std_chains->{$chain}}, "-j DROP";
2396 foreach my $chain (keys %$pve_std_chains) {
2397 ruleset_create_chain($ruleset, $chain);
2398 foreach my $rule (@{$pve_std_chains->{$chain}}) {
2400 ruleset_generate_rule($ruleset, $chain, $rule);
2402 ruleset_addrule($ruleset, $chain, $rule);
2408 sub generate_ipset_chains {
2409 my ($ipset_ruleset, $fw_conf) = @_;
2411 foreach my $ipset (keys %{$fw_conf->{ipset}}) {
2412 generate_ipset($ipset_ruleset, "PVEFW-$ipset", $fw_conf->{ipset}->{$ipset}, $fw_conf->{aliases});
2416 sub generate_ipset {
2417 my ($ipset_ruleset, $name, $options, $aliases) = @_;
2419 my $hashsize = scalar(@$options);
2420 if ($hashsize <= 64) {
2423 $hashsize = round_powerof2($hashsize);
2426 push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize";
2430 foreach my $entry (@$options) {
2431 my $cidr = $entry->{cidr};
2432 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2433 my $alias = lc($cidr);
2434 if ($aliases->{$alias}) {
2435 $entry->{cidr} = $aliases->{$alias}->{cidr};
2436 $nethash->{$entry->{cidr}} = $entry;
2438 warn "no such alias '$cidr'\n" if !$aliases->{$alias};
2441 $nethash->{$entry->{cidr}} = $entry;
2445 foreach my $cidr (sort keys %$nethash) {
2446 my $entry = $nethash->{$cidr};
2448 my $cmd = "add $name $cidr";
2449 if ($entry->{nomatch}) {
2450 if ($feature_ipset_nomatch) {
2451 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
2453 warn "ignore !$cidr - nomatch not supported by kernel\n";
2456 push @{$ipset_ruleset->{$name}}, $cmd;
2461 sub round_powerof2 {
2465 $int |= $int >> $_ foreach (1,2,4,8,16);
2469 sub load_clusterfw_conf {
2470 my ($filename) = @_;
2472 $filename = $clusterfw_conf_filename if !defined($filename);
2474 my $cluster_conf = {};
2475 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2476 $cluster_conf = parse_cluster_fw_rules($filename, $fh);
2479 return $cluster_conf;
2482 sub save_clusterfw_conf {
2483 my ($cluster_conf) = @_;
2487 my $options = $cluster_conf->{options};
2488 $raw .= &$format_options($options) if scalar(keys %$options);
2490 my $aliases = $cluster_conf->{aliases};
2491 $raw .= &$format_aliases($aliases) if scalar(keys %$aliases);
2493 foreach my $ipset (sort keys %{$cluster_conf->{ipset}}) {
2494 if (my $comment = $cluster_conf->{ipset_comments}->{$ipset}) {
2495 my $utf8comment = encode('utf8
', $comment);
2496 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2498 $raw .= "[IPSET $ipset]\n\n";
2500 my $options = $cluster_conf->{ipset}->{$ipset};
2501 $raw .= &$format_ipset($options);
2505 my $rules = $cluster_conf->{rules};
2506 if (scalar(@$rules)) {
2507 $raw .= "[RULES]\n\n";
2508 $raw .= &$format_rules($rules, 1);
2512 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
2513 my $rules = $cluster_conf->{groups}->{$group};
2514 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
2515 my $utf8comment = encode('utf8
', $comment);
2516 $raw .= "[group $group] # $utf8comment\n\n";
2518 $raw .= "[group $group]\n\n";
2521 $raw .= &$format_rules($rules, 0);
2525 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
2528 sub load_hostfw_conf {
2529 my ($filename) = @_;
2531 $filename = $hostfw_conf_filename if !defined($filename);
2533 my $hostfw_conf = {};
2534 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2535 $hostfw_conf = parse_host_fw_rules($filename, $fh);
2537 return $hostfw_conf;
2540 sub save_hostfw_conf {
2541 my ($hostfw_conf) = @_;
2545 my $options = $hostfw_conf->{options};
2546 $raw .= &$format_options($options) if scalar(keys %$options);
2548 my $rules = $hostfw_conf->{rules};
2549 if (scalar(@$rules)) {
2550 $raw .= "[RULES]\n\n";
2551 $raw .= &$format_rules($rules, 1);
2555 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
2559 my ($cluster_conf, $hostfw_conf, $vmdata) = @_;
2563 if ($vmdata) { # test mode
2564 my $testdir = $vmdata->{testdir} || die "no test directory specified";
2565 my $filename = "$testdir/cluster.fw";
2566 die "missing test file '$filename'\n" if ! -f $filename;
2567 $cluster_conf = load_clusterfw_conf($filename);
2569 $filename = "$testdir/host.fw";
2570 die "missing test file '$filename'\n" if ! -f $filename;
2571 $hostfw_conf = load_hostfw_conf($filename);
2573 $vmfw_configs = read_vm_firewall_configs($vmdata, $testdir);
2574 } else { # normal operation
2575 $cluster_conf = load_clusterfw_conf() if !$cluster_conf;
2577 $hostfw_conf = load_hostfw_conf() if !$hostfw_conf;
2579 $vmdata = read_local_vm_config();
2580 $vmfw_configs = read_vm_firewall_configs($vmdata);
2584 $cluster_conf->{ipset}->{venet0} = [];
2588 ruleset_create_chain($ruleset, "PVEFW-INPUT");
2589 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
2591 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
2593 my $hostfw_options = $hostfw_conf->{options} || {};
2595 # fixme: what log level should we use here?
2596 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
2598 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT");
2600 ruleset_create_chain($ruleset, "PVEFW-VENET-OUT");
2601 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set PVEFW-venet0 src -j PVEFW-VENET-OUT");
2602 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set PVEFW-venet0 src -j PVEFW-VENET-OUT");
2604 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
2605 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $hostfw_options, $cluster_conf, $loglevel);
2607 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN");
2609 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
2610 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT");
2612 ruleset_create_chain($ruleset, "PVEFW-VENET-IN");
2613 ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $hostfw_options, $cluster_conf, $loglevel);
2615 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set PVEFW-venet0 dst -j PVEFW-VENET-IN");
2617 generate_std_chains($ruleset, $hostfw_options);
2619 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
2621 enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf) if $hostfw_enable;
2623 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set PVEFW-venet0 dst -j PVEFW-VENET-IN");
2625 # generate firewall rules for QEMU VMs
2626 foreach my $vmid (keys %{$vmdata->{qemu}}) {
2627 my $conf = $vmdata->{qemu}->{$vmid};
2628 my $vmfw_conf = $vmfw_configs->{$vmid};
2629 next if !$vmfw_conf;
2630 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
2632 foreach my $netid (keys %$conf) {
2633 next if $netid !~ m/^net(\d+)$/;
2634 my $net = PVE::QemuServer::parse_net($conf->{$netid});
2635 next if !$net->{firewall};
2636 my $iface = "tap${vmid}i$1";
2638 my $macaddr = $net->{macaddr};
2639 generate_tap_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr,
2640 $vmfw_conf, $vmid, 'IN
');
2641 generate_tap_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr,
2642 $vmfw_conf, $vmid, 'OUT
');
2646 # generate firewall rules for OpenVZ containers
2647 foreach my $vmid (keys %{$vmdata->{openvz}}) {
2648 my $conf = $vmdata->{openvz}->{$vmid};
2650 my $vmfw_conf = $vmfw_configs->{$vmid};
2651 next if !$vmfw_conf;
2652 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
2654 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
2655 my $ip = $conf->{ip_address}->{value};
2657 parse_address_list($ip); # make sure we have a valid $ip list
2659 my @ips = split(',', $ip);
2661 foreach my $singleip (@ips) {
2662 my $venet0ipset = {};
2663 $venet0ipset->{cidr} = $singleip;
2664 push @{$cluster_conf->{ipset}->{venet0}}, $venet0ipset;
2667 generate_venet_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, 'IN
');
2668 generate_venet_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, 'OUT
');
2671 if ($conf->{netif} && $conf->{netif}->{value}) {
2672 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
2673 foreach my $netid (keys %$netif) {
2674 my $d = $netif->{$netid};
2676 my $macaddr = $d->{mac};
2677 my $iface = $d->{host_ifname};
2678 generate_tap_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr,
2679 $vmfw_conf, $vmid, 'IN
');
2680 generate_tap_rules_direction($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr,
2681 $vmfw_conf, $vmid, 'OUT
');
2686 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
2687 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS");
2690 my $ipset_ruleset = {};
2691 generate_ipset_chains($ipset_ruleset, $cluster_conf);
2693 return ($ruleset, $ipset_ruleset);
2696 sub get_ruleset_status {
2697 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
2699 my $statushash = {};
2701 foreach my $chain (sort keys %$ruleset) {
2702 my $sig = &$digest_fn($ruleset->{$chain});
2704 $statushash->{$chain}->{sig} = $sig;
2706 my $oldsig = $active_chains->{$chain};
2707 if (!defined($oldsig)) {
2708 $statushash->{$chain}->{action} = 'create
';
2710 if ($oldsig eq $sig) {
2711 $statushash->{$chain}->{action} = 'exists';
2713 $statushash->{$chain}->{action} = 'update
';
2716 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
2717 foreach my $cmd (@{$ruleset->{$chain}}) {
2718 print "\t$cmd\n" if $verbose;
2722 foreach my $chain (sort keys %$active_chains) {
2723 if (!defined($ruleset->{$chain})) {
2724 my $sig = $active_chains->{$chain};
2725 $statushash->{$chain}->{action} = 'delete';
2726 $statushash->{$chain}->{sig} = $sig;
2727 print "delete $chain ($sig)\n" if $verbose;
2734 sub print_sig_rule {
2735 my ($chain, $sig) = @_;
2737 # We just use this to store a SHA1 checksum used to detect changes
2738 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
2741 sub get_ruleset_cmdlist {
2742 my ($ruleset, $verbose) = @_;
2744 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
2746 my ($active_chains, $hooks) = iptables_get_chains();
2747 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
2749 # create missing chains first
2750 foreach my $chain (sort keys %$ruleset) {
2751 my $stat = $statushash->{$chain};
2752 die "internal error" if !$stat;
2753 next if $stat->{action} ne 'create
';
2755 $cmdlist .= ":$chain - [0:0]\n";
2758 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2759 if (!$hooks->{$h}) {
2760 $cmdlist .= "-A $h -j PVEFW-$h\n";
2764 foreach my $chain (sort keys %$ruleset) {
2765 my $stat = $statushash->{$chain};
2766 die "internal error" if !$stat;
2768 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
2769 $cmdlist .= "-F $chain\n";
2770 foreach my $cmd (@{$ruleset->{$chain}}) {
2771 $cmdlist .= "$cmd\n";
2773 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
2774 } elsif ($stat->{action
} eq 'delete') {
2775 die "internal error"; # this should not happen
2776 } elsif ($stat->{action
} eq 'exists') {
2779 die "internal error - unknown status '$stat->{action}'";
2783 foreach my $chain (keys %$statushash) {
2784 next if $statushash->{$chain}->{action
} ne 'delete';
2785 $cmdlist .= "-F $chain\n";
2787 foreach my $chain (keys %$statushash) {
2788 next if $statushash->{$chain}->{action
} ne 'delete';
2789 next if $chain eq 'PVEFW-INPUT';
2790 next if $chain eq 'PVEFW-OUTPUT';
2791 next if $chain eq 'PVEFW-FORWARD';
2792 $cmdlist .= "-X $chain\n";
2795 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
2797 $cmdlist .= "COMMIT\n";
2799 return wantarray ?
($cmdlist, $changes) : $cmdlist;
2802 sub get_ipset_cmdlist
{
2803 my ($ruleset, $verbose) = @_;
2807 my $delete_cmdlist = "";
2809 my $active_chains = ipset_get_chains
();
2810 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
2812 # remove stale _swap chains
2813 foreach my $chain (keys %$active_chains) {
2814 if ($chain =~ m/^PVEFW-\S+_swap$/) {
2815 $cmdlist .= "destroy $chain\n";
2819 foreach my $chain (sort keys %$ruleset) {
2820 my $stat = $statushash->{$chain};
2821 die "internal error" if !$stat;
2823 if ($stat->{action
} eq 'create') {
2824 foreach my $cmd (@{$ruleset->{$chain}}) {
2825 $cmdlist .= "$cmd\n";
2829 if ($stat->{action
} eq 'update') {
2830 my $chain_swap = $chain."_swap";
2832 foreach my $cmd (@{$ruleset->{$chain}}) {
2833 $cmd =~ s/$chain/$chain_swap/;
2834 $cmdlist .= "$cmd\n";
2836 $cmdlist .= "swap $chain_swap $chain\n";
2837 $cmdlist .= "flush $chain_swap\n";
2838 $cmdlist .= "destroy $chain_swap\n";
2843 foreach my $chain (keys %$statushash) {
2844 next if $statushash->{$chain}->{action
} ne 'delete';
2846 $delete_cmdlist .= "flush $chain\n";
2847 $delete_cmdlist .= "destroy $chain\n";
2850 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
2852 return ($cmdlist, $delete_cmdlist, $changes);
2856 my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_;
2858 enable_bridge_firewall
();
2860 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
2861 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
2863 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
2866 if ($ipset_changes) {
2867 print "ipset changes:\n";
2868 print $ipset_create_cmdlist if $ipset_create_cmdlist;
2869 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
2873 print "iptables changes:\n";
2878 ipset_restore_cmdlist
($ipset_create_cmdlist);
2880 iptables_restore_cmdlist
($cmdlist);
2882 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
2884 # test: re-read status and check if everything is up to date
2885 my $active_chains = iptables_get_chains
();
2886 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
2889 foreach my $chain (sort keys %$ruleset) {
2890 my $stat = $statushash->{$chain};
2891 if ($stat->{action
} ne 'exists') {
2892 warn "unable to update chain '$chain'\n";
2897 die "unable to apply firewall changes\n" if $errors;
2899 update_nf_conntrack_max
($hostfw_conf);
2901 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
2905 sub update_nf_conntrack_max
{
2906 my ($hostfw_conf) = @_;
2908 my $max = 65536; # reasonable default
2910 my $options = $hostfw_conf->{options
} || {};
2912 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
2913 $max = $options->{nf_conntrack_max
};
2914 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
2917 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
2918 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
2920 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
2922 if ($current != $max) {
2923 my $hashsize = int($max/4);
2924 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
2925 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
2929 sub update_nf_conntrack_tcp_timeout_established
{
2930 my ($hostfw_conf) = @_;
2932 my $options = $hostfw_conf->{options
} || {};
2934 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
2936 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
2939 sub remove_pvefw_chains
{
2941 my ($chash, $hooks) = iptables_get_chains
();
2942 my $cmdlist = "*filter\n";
2944 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2946 $cmdlist .= "-D $h -j PVEFW-$h\n";
2950 foreach my $chain (keys %$chash) {
2951 $cmdlist .= "-F $chain\n";
2954 foreach my $chain (keys %$chash) {
2955 $cmdlist .= "-X $chain\n";
2957 $cmdlist .= "COMMIT\n";
2959 iptables_restore_cmdlist
($cmdlist);
2967 my $cluster_conf = load_clusterfw_conf
();
2968 my $cluster_options = $cluster_conf->{options
};
2970 my $enable = $cluster_options->{enable
};
2972 die "Firewall is disabled - cannot start\n" if !$enable;
2975 PVE
::Firewall
::remove_pvefw_chains
();
2976 print "Firewall disabled\n" if $verbose;
2980 my $hostfw_conf = load_hostfw_conf
();
2982 my ($ruleset, $ipset_ruleset) = compile
($cluster_conf, $hostfw_conf);
2984 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $verbose);