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-loglevel' => {
60 description
=> "Log level.",
62 enum
=> ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'nolog'],
66 my $security_group_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
67 my $ip_alias_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
69 PVE
::JSONSchema
::register_standard_option
('pve-security-group-name', {
70 description
=> "Security Group name.",
72 pattern
=> $security_group_name_pattern,
77 my $feature_ipset_nomatch = 0;
79 my (undef, undef, $release) = POSIX
::uname
();
80 if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) {
81 my ($major, $minor) = ($1, $2);
82 $feature_ipset_nomatch = 1 if ($major > 3) ||
83 ($major == 3 && $minor >= 7);
90 my $nodename = PVE
::INotify
::nodename
();
92 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
93 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
95 my $default_log_level = 'info';
97 my $log_level_hash = {
108 # imported/converted from: /usr/share/shorewall/macro.*
109 my $pve_fw_macros = {
112 { action
=> 'PARAM', proto
=> 'udp', dport
=> '10080' },
113 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10080' },
116 "Auth (identd) traffic",
117 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '113' },
120 "Border Gateway Protocol traffic",
121 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '179' },
124 "BitTorrent traffic for BitTorrent 3.1 and earlier",
125 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6889' },
126 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
129 "BitTorrent traffic for BitTorrent 3.2 and later",
130 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6881:6999' },
131 { action
=> 'PARAM', proto
=> 'udp', dport
=> '6881' },
134 "Concurrent Versions System pserver traffic",
135 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2401' },
138 "Citrix/ICA traffic (ICA, ICA Browser, CGP)",
139 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1494' },
140 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1604' },
141 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2598' },
144 "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)",
145 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3689' },
146 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3689' },
149 "Distributed Checksum Clearinghouse spam filtering mechanism",
150 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6277' },
153 "Forwarded DHCP traffic (bidirectional)",
154 { action
=> 'PARAM', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
155 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '67:68', sport
=> '67:68' },
158 "Domain Name System traffic (upd and tcp)",
159 { action
=> 'PARAM', proto
=> 'udp', dport
=> '53' },
160 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '53' },
163 "Distributed Compiler service",
164 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3632' },
167 "File Transfer Protocol",
168 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '21' },
171 "Finger protocol (RFC 742)",
172 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '79' },
175 "GNUnet secure peer-to-peer networking traffic",
176 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '2086' },
177 { action
=> 'PARAM', proto
=> 'udp', dport
=> '2086' },
178 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1080' },
179 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1080' },
182 "Generic Routing Encapsulation tunneling protocol (bidirectional)",
183 { action
=> 'PARAM', proto
=> '47' },
184 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
187 "Git distributed revision control traffic",
188 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9418' },
191 "OpenPGP HTTP keyserver protocol traffic",
192 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '11371' },
195 "Hypertext Transfer Protocol (WWW)",
196 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
199 "Hypertext Transfer Protocol (WWW) over SSL",
200 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
203 "Internet Cache Protocol V2 (Squid) traffic",
204 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3130' },
207 "AOL Instant Messenger traffic",
208 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5190' },
211 "Internet Message Access Protocol",
212 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '143' },
215 "Internet Message Access Protocol over SSL",
216 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '993' },
219 "IPIP capsulation traffic (bidirectional)",
220 { action
=> 'PARAM', proto
=> '94' },
221 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '94' },
224 "IPsec traffic (bidirectional)",
225 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
226 { action
=> 'PARAM', proto
=> '50' },
227 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
228 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
231 "IPsec authentication (AH) traffic (bidirectional)",
232 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500', sport
=> '500' },
233 { action
=> 'PARAM', proto
=> '51' },
234 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500', sport
=> '500' },
235 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '51' },
238 "IPsec traffic and Nat-Traversal (bidirectional)",
239 { action
=> 'PARAM', proto
=> 'udp', dport
=> '500' },
240 { action
=> 'PARAM', proto
=> 'udp', dport
=> '4500' },
241 { action
=> 'PARAM', proto
=> '50' },
242 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '500' },
243 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '4500' },
244 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '50' },
247 "Internet Relay Chat traffic",
248 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6667' },
251 "HP Jetdirect printing",
252 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '9100' },
255 "Layer 2 Tunneling Protocol traffic",
256 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1701' },
257 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1701' },
260 "Lightweight Directory Access Protocol traffic",
261 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '389' },
264 "Secure Lightweight Directory Access Protocol traffic",
265 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '636' },
268 "Microsoft Notification Protocol",
269 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1863' },
272 "Microsoft SQL Server",
273 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1433' },
276 "Mail traffic (SMTP, SMTPS, Submission)",
277 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
278 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
279 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
282 "Munin networked resource monitoring traffic",
283 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '4949' },
287 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3306' },
290 "NNTP traffic (Usenet).",
291 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '119' },
294 "Encrypted NNTP traffic (Usenet)",
295 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '563' },
298 "Network Time Protocol (ntpd)",
299 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
302 "Bi-directional NTP (for NTP peers)",
303 { action
=> 'PARAM', proto
=> 'udp', dport
=> '123' },
304 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '123' },
307 "OSPF multicast traffic",
308 { action
=> 'PARAM', proto
=> '89' },
312 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1194' },
315 "Symantec PCAnywere (tm)",
316 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5632' },
317 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5631' },
321 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '110' },
324 "Encrypted POP3 traffic",
325 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '995' },
328 "Point-to-Point Tunneling Protocol",
329 { action
=> 'PARAM', proto
=> '47' },
330 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> '47' },
331 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '1723' },
335 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
339 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5432' },
342 "Line Printer protocol printing",
343 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '515' },
346 "Microsoft Remote Desktop Protocol traffic",
347 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3389' },
350 "Routing Information Protocol (bidirectional)",
351 { action
=> 'PARAM', proto
=> 'udp', dport
=> '520' },
352 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '520' },
355 "BIND remote management protocol",
356 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '953' },
359 "Razor Antispam System",
360 { action
=> 'ACCEPT', proto
=> 'tcp', dport
=> '2703' },
363 "Remote time retrieval (rdate)",
364 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
368 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '873' },
371 "SANE network scanning",
372 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '6566' },
375 "Microsoft SMB traffic",
376 { action
=> 'PARAM', proto
=> 'udp', dport
=> '135,445' },
377 { action
=> 'PARAM', proto
=> 'udp', dport
=> '137:139' },
378 { action
=> 'PARAM', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
379 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '135,139,445' },
382 "Microsoft SMB traffic (bidirectional)",
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' },
387 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '135,445' },
388 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '137:139' },
389 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'udp', dport
=> '1024:65535', sport
=> '137' },
390 { action
=> 'PARAM', source
=> 'DEST', dest
=> 'SOURCE', proto
=> 'tcp', dport
=> '135,139,445' },
393 "Samba Web Administration Tool",
394 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '901' },
397 "Simple Mail Transfer Protocol",
398 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '25' },
401 "Encrypted Simple Mail Transfer Protocol",
402 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '465' },
405 "Simple Network Management Protocol",
406 { action
=> 'PARAM', proto
=> 'udp', dport
=> '161:162' },
407 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '161' },
410 "Spam Assassin SPAMD traffic",
411 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '783' },
414 "Secure shell traffic",
415 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '22' },
418 "Subversion server (svnserve)",
419 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3690' },
422 "SixXS IPv6 Deployment and Tunnel Broker",
423 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3874' },
424 { action
=> 'PARAM', proto
=> 'udp', dport
=> '3740' },
425 { action
=> 'PARAM', proto
=> '41' },
426 { action
=> 'PARAM', proto
=> 'udp', dport
=> '5072,8374' },
429 "Squid web proxy traffic",
430 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '3128' },
433 "Mail message submission traffic",
434 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '587' },
437 "Syslog protocol (RFC 5424) traffic",
438 { action
=> 'PARAM', proto
=> 'udp', dport
=> '514' },
439 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '514' },
442 "Trivial File Transfer Protocol traffic",
443 { action
=> 'PARAM', proto
=> 'udp', dport
=> '69' },
447 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '23' },
451 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '992' },
454 "RFC 868 Time protocol",
455 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '37' },
458 "Traceroute (for up to 30 hops) traffic",
459 { action
=> 'PARAM', proto
=> 'udp', dport
=> '33434:33524' },
460 { action
=> 'PARAM', proto
=> 'icmp', dport
=> 'echo-request' },
463 "VNC traffic for VNC display's 0 - 9",
464 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5900:5909' },
467 "VNC traffic from Vncservers to Vncviewers in listen mode",
468 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '5500' },
471 "WWW traffic (HTTP and HTTPS)",
472 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '80' },
473 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '443' },
476 "Web Cache/Proxy traffic (port 8080)",
477 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '8080' },
481 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '10000' },
484 "Whois (nicname, RFC 3912) traffic",
485 { action
=> 'PARAM', proto
=> 'tcp', dport
=> '43' },
489 my $pve_fw_parsed_macros;
490 my $pve_fw_macro_descr;
491 my $pve_fw_preferred_macro_names = {};
493 my $pve_std_chains = {
494 'PVEFW-SET-ACCEPT-MARK' => [
495 "-j MARK --set-mark 1",
497 'PVEFW-DropBroadcast' => [
498 # same as shorewall 'Broadcast'
499 # simply DROP BROADCAST/MULTICAST/ANYCAST
500 # we can use this to reduce logging
501 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
502 { action
=> 'DROP', dsttype
=> 'MULTICAST' },
503 { action
=> 'DROP', dsttype
=> 'ANYCAST' },
504 { action
=> 'DROP', dest
=> '224.0.0.0/4' },
507 # same as shorewall 'reject'
508 { action
=> 'DROP', dsttype
=> 'BROADCAST' },
509 { action
=> 'DROP', source
=> '224.0.0.0/4' },
510 { action
=> 'DROP', proto
=> 'icmp' },
511 "-p tcp -j REJECT --reject-with tcp-reset",
512 "-p udp -j REJECT --reject-with icmp-port-unreachable",
513 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
514 "-j REJECT --reject-with icmp-host-prohibited",
517 # same as shorewall 'Drop', which is equal to DROP,
518 # but REJECT/DROP some packages to reduce logging,
519 # and ACCEPT critical ICMP types
520 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
521 # we are not interested in BROADCAST/MULTICAST/ANYCAST
522 { action
=> 'PVEFW-DropBroadcast' },
523 # ACCEPT critical ICMP types
524 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
525 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
526 # Drop packets with INVALID state
527 "-m conntrack --ctstate INVALID -j DROP",
528 # Drop Microsoft SMB noise
529 { action
=> 'DROP', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
530 { action
=> 'DROP', proto
=> 'udp', dport
=> '137:139'},
531 { action
=> 'DROP', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
532 { action
=> 'DROP', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
533 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
534 # Drop new/NotSyn traffic so that it doesn't get logged
535 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
537 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
540 # same as shorewall 'Reject', which is equal to Reject,
541 # but REJECT/DROP some packages to reduce logging,
542 # and ACCEPT critical ICMP types
543 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '43' }, # REJECT 'auth'
544 # we are not interested in BROADCAST/MULTICAST/ANYCAST
545 { action
=> 'PVEFW-DropBroadcast' },
546 # ACCEPT critical ICMP types
547 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'fragmentation-needed' },
548 { action
=> 'ACCEPT', proto
=> 'icmp', dport
=> 'time-exceeded' },
549 # Drop packets with INVALID state
550 "-m conntrack --ctstate INVALID -j DROP",
551 # Drop Microsoft SMB noise
552 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '135,445', nbdport
=> 2 },
553 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '137:139'},
554 { action
=> 'PVEFW-reject', proto
=> 'udp', dport
=> '1024:65535', sport
=> 137 },
555 { action
=> 'PVEFW-reject', proto
=> 'tcp', dport
=> '135,139,445', nbdport
=> 3 },
556 { action
=> 'DROP', proto
=> 'udp', dport
=> 1900 }, # UPnP
557 # Drop new/NotSyn traffic so that it doesn't get logged
558 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
560 { action
=> 'DROP', proto
=> 'udp', sport
=> 53 },
562 'PVEFW-tcpflags' => [
563 # same as shorewall tcpflags action.
564 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
565 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
566 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
567 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
568 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
569 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
572 # same as shorewall smurfs action
573 # Filter packets for smurfs (packets with a broadcast address as the source).
574 "-s 0.0.0.0/32 -j RETURN",
575 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
576 "-s 224.0.0.0/4 -g PVEFW-smurflog",
580 # iptables -p icmp -h
581 my $icmp_type_names = {
584 'destination-unreachable' => 1,
585 'network-unreachable' => 1,
586 'host-unreachable' => 1,
587 'protocol-unreachable' => 1,
588 'port-unreachable' => 1,
589 'fragmentation-needed' => 1,
590 'source-route-failed' => 1,
591 'network-unknown' => 1,
593 'network-prohibited' => 1,
594 'host-prohibited' => 1,
595 'TOS-network-unreachable' => 1,
596 'TOS-host-unreachable' => 1,
597 'communication-prohibited' => 1,
598 'host-precedence-violation' => 1,
599 'precedence-cutoff' => 1,
600 'source-quench' => 1,
602 'network-redirect' => 1,
603 'host-redirect' => 1,
604 'TOS-network-redirect' => 1,
605 'TOS-host-redirect' => 1,
607 'router-advertisement' => 1,
608 'router-solicitation' => 1,
609 'time-exceeded' => 1,
610 'ttl-zero-during-transit' => 1,
611 'ttl-zero-during-reassembly' => 1,
612 'parameter-problem' => 1,
613 'ip-header-bad' => 1,
614 'required-option-missing' => 1,
615 'timestamp-request' => 1,
616 'timestamp-reply' => 1,
617 'address-mask-request' => 1,
618 'address-mask-reply' => 1,
621 sub init_firewall_macros
{
623 $pve_fw_parsed_macros = {};
625 foreach my $k (keys %$pve_fw_macros) {
626 my $lc_name = lc($k);
627 my $macro = $pve_fw_macros->{$k};
628 if (!ref($macro->[0])) {
629 $pve_fw_macro_descr->{$k} = shift @$macro;
631 $pve_fw_preferred_macro_names->{$lc_name} = $k;
632 $pve_fw_parsed_macros->{$k} = $macro;
636 init_firewall_macros
();
639 return wantarray ?
($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
644 sub get_etc_services
{
646 return $etc_services if $etc_services;
648 my $filename = "/etc/services";
650 my $fh = IO
::File-
>new($filename, O_RDONLY
);
652 warn "unable to read '$filename' - $!\n";
658 while (my $line = <$fh>) {
660 next if $line =~m/^#/;
661 next if ($line =~m/^\s*$/);
663 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
664 $services->{byid
}->{$2}->{name
} = $1;
665 $services->{byid
}->{$2}->{port
} = $2;
666 $services->{byid
}->{$2}->{$3} = 1;
667 $services->{byname
}->{$1} = $services->{byid
}->{$2};
673 $etc_services = $services;
676 return $etc_services;
681 sub get_etc_protocols
{
682 return $etc_protocols if $etc_protocols;
684 my $filename = "/etc/protocols";
686 my $fh = IO
::File-
>new($filename, O_RDONLY
);
688 warn "unable to read '$filename' - $!\n";
694 while (my $line = <$fh>) {
696 next if $line =~m/^#/;
697 next if ($line =~m/^\s*$/);
699 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
700 $protocols->{byid
}->{$2}->{name
} = $1;
701 $protocols->{byname
}->{$1} = $protocols->{byid
}->{$2};
707 $etc_protocols = $protocols;
709 return $etc_protocols;
712 sub parse_address_list
{
715 return if $str =~ m/^(\+)(\S+)$/; # ipset ref
716 return if $str =~ m/^${ip_alias_pattern}$/;
720 foreach my $elem (split(/,/, $str)) {
722 if (!Net
::IP-
>new($elem)) {
723 my $err = Net
::IP
::Error
();
724 die "invalid IP address: $err\n";
726 $iprange = 1 if $elem =~ m/-/;
729 die "you can use a range in a list\n" if $iprange && $count > 1;
732 sub parse_port_name_number_or_range
{
735 my $services = PVE
::Firewall
::get_etc_services
();
739 foreach my $item (split(/,/, $str)) {
741 if ($item =~ m/^(\d+):(\d+)$/) {
742 my ($port1, $port2) = ($1, $2);
743 die "invalid port '$port1'\n" if $port1 > 65535;
744 die "invalid port '$port2'\n" if $port2 > 65535;
745 } elsif ($item =~ m/^(\d+)$/) {
747 die "invalid port '$port'\n" if $port > 65535;
749 if ($icmp_type_names->{$item}) {
752 die "invalid port '$item'\n" if !$services->{byname
}->{$item};
757 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1;
762 PVE
::JSONSchema
::register_format
('pve-fw-port-spec', \
&pve_fw_verify_port_spec
);
763 sub pve_fw_verify_port_spec
{
766 parse_port_name_number_or_range
($portstr);
771 PVE
::JSONSchema
::register_format
('pve-fw-v4addr-spec', \
&pve_fw_verify_v4addr_spec
);
772 sub pve_fw_verify_v4addr_spec
{
775 parse_address_list
($list);
780 PVE
::JSONSchema
::register_format
('pve-fw-protocol-spec', \
&pve_fw_verify_protocol_spec
);
781 sub pve_fw_verify_protocol_spec
{
784 my $protocols = get_etc_protocols
();
786 die "unknown protocol '$proto'\n" if $proto &&
787 !(defined($protocols->{byname
}->{$proto}) ||
788 defined($protocols->{byid
}->{$proto}));
794 # helper function for API
796 sub copy_opject_with_digest
{
799 my $sha = Digest
::SHA-
>new('sha1');
802 foreach my $k (sort keys %$object) {
803 my $v = $object->{$k};
804 next if !defined($v);
806 $sha->add($k, ':', $v, "\n");
809 my $digest = $sha->b64digest;
811 $res->{digest
} = $digest;
813 return wantarray ?
($res, $digest) : $res;
816 sub copy_list_with_digest
{
819 my $sha = Digest
::SHA-
>new('sha1');
822 foreach my $entry (@$list) {
824 foreach my $k (sort keys %$entry) {
825 my $v = $entry->{$k};
826 next if !defined($v);
828 $sha->add($k, ':', $v, "\n");
833 my $digest = $sha->b64digest;
835 foreach my $entry (@$res) {
836 $entry->{digest
} = $digest;
839 return wantarray ?
($res, $digest) : $res;
842 my $rule_properties = {
844 description
=> "Update rule at position <pos>.",
849 digest
=> get_standard_option
('pve-config-digest'),
853 enum
=> ['in', 'out', 'group'],
856 description
=> "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
859 pattern
=> $security_group_name_pattern,
868 iface
=> get_standard_option
('pve-iface', { optional
=> 1 }),
870 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
874 type
=> 'string', format
=> 'pve-fw-v4addr-spec',
878 type
=> 'string', format
=> 'pve-fw-protocol-spec',
886 type
=> 'string', format
=> 'pve-fw-port-spec',
890 type
=> 'string', format
=> 'pve-fw-port-spec',
899 sub add_rule_properties
{
900 my ($properties) = @_;
902 foreach my $k (keys %$rule_properties) {
903 my $h = $rule_properties->{$k};
904 # copy data, so that we can modify later without side effects
905 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
911 sub delete_rule_properties
{
912 my ($rule, $delete_str) = @_;
914 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
915 raise_param_exc
({ 'delete' => "no such property ('$opt')"})
916 if !defined($rule_properties->{$opt});
917 raise_param_exc
({ 'delete' => "unable to delete required property '$opt'"})
918 if $opt eq 'type' || $opt eq 'action';
919 delete $rule->{$opt};
925 my $apply_macro = sub {
926 my ($macro_name, $param, $verify) = @_;
928 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
929 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
933 foreach my $templ (@$macro_rules) {
936 foreach my $k (keys %$templ) {
937 my $v = $templ->{$k};
940 $param_used->{$k} = 1;
941 } elsif ($v eq 'DEST') {
943 $param_used->{dest
} = 1;
944 } elsif ($v eq 'SOURCE') {
945 $v = $param->{source
};
946 $param_used->{source
} = 1;
950 my $msg = "missing parameter '$k' in macro '$macro_name'";
951 raise_param_exc
({ macro => $msg }) if $verify;
956 foreach my $k (keys %$param) {
957 next if $k eq 'macro';
958 next if !defined($param->{$k});
959 next if $param_used->{$k};
960 if (defined($rule->{$k})) {
961 if ($rule->{$k} ne $param->{$k}) {
962 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
963 raise_param_exc
({ $k => $msg }) if $verify;
967 $rule->{$k} = $param->{$k};
977 my ($rule, $allow_groups) = @_;
979 my $type = $rule->{type
};
981 raise_param_exc
({ type
=> "missing property"}) if !$type;
982 raise_param_exc
({ action
=> "missing property"}) if !$rule->{action
};
984 if ($type eq 'in' || $type eq 'out') {
985 raise_param_exc
({ action
=> "unknown action '$rule->{action}'"})
986 if $rule->{action
} !~ m/^(ACCEPT|DROP|REJECT)$/;
987 } elsif ($type eq 'group') {
988 raise_param_exc
({ type
=> "security groups not allowed"})
990 raise_param_exc
({ action
=> "invalid characters in security group name"})
991 if $rule->{action
} !~ m/^${security_group_name_pattern}$/;
993 raise_param_exc
({ type
=> "unknown rule type '$type'"});
996 # fixme: verify $rule->{iface}?
998 if ($rule->{macro}) {
999 my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})};
1000 raise_param_exc
({ macro => "unknown macro '$rule->{macro}'"}) if !$preferred_name;
1001 $rule->{macro} = $preferred_name;
1004 if ($rule->{dport
}) {
1005 eval { parse_port_name_number_or_range
($rule->{dport
}); };
1006 raise_param_exc
({ dport
=> $@ }) if $@;
1009 if ($rule->{sport
}) {
1010 eval { parse_port_name_number_or_range
($rule->{sport
}); };
1011 raise_param_exc
({ sport
=> $@ }) if $@;
1014 if ($rule->{source
}) {
1015 eval { parse_address_list
($rule->{source
}); };
1016 raise_param_exc
({ source
=> $@ }) if $@;
1019 if ($rule->{dest
}) {
1020 eval { parse_address_list
($rule->{dest
}); };
1021 raise_param_exc
({ dest
=> $@ }) if $@;
1024 if ($rule->{macro}) {
1025 &$apply_macro($rule->{macro}, $rule, 1);
1031 sub copy_rule_data
{
1032 my ($rule, $param) = @_;
1034 foreach my $k (keys %$rule_properties) {
1035 if (defined(my $v = $param->{$k})) {
1036 if ($v eq '' || $v eq '-') {
1052 my $bridge_firewall_enabled = 0;
1054 sub enable_bridge_firewall
{
1056 return if $bridge_firewall_enabled; # only once
1058 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1059 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
1061 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1062 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/ipv4/tcp_syncookies", "1");
1064 $bridge_firewall_enabled = 1;
1067 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
1069 sub iptables_restore_cmdlist
{
1072 run_command
("/sbin/iptables-restore -n", input
=> $cmdlist);
1075 sub ipset_restore_cmdlist
{
1078 run_command
("/usr/sbin/ipset restore", input
=> $cmdlist);
1081 sub iptables_get_chains
{
1085 # check what chains we want to track
1086 my $is_pvefw_chain = sub {
1089 return 1 if $name =~ m/^PVEFW-\S+$/;
1091 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
1093 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
1095 return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/;
1097 return 1 if $name =~ m/^vmbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/;
1098 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
1110 return if $line =~ m/^#/;
1111 return if $line =~ m/^\s*$/;
1113 if ($line =~ m/^\*(\S+)$/) {
1118 return if $table ne 'filter';
1120 if ($line =~ m/^:(\S+)\s/) {
1122 return if !&$is_pvefw_chain($chain);
1123 $res->{$chain} = "unknown";
1124 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
1125 my ($chain, $sig) = ($1, $2);
1126 return if !&$is_pvefw_chain($chain);
1127 $res->{$chain} = $sig;
1128 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1131 # simply ignore the rest
1136 run_command
("/sbin/iptables-save", outfunc
=> $parser);
1138 return wantarray ?
($res, $hooks) : $res;
1141 sub iptables_chain_digest
{
1143 my $digest = Digest
::SHA-
>new('sha1');
1144 foreach my $rule (@$rules) { # order is important
1145 $digest->add($rule);
1147 return $digest->b64digest;
1150 sub ipset_chain_digest
{
1153 my $digest = Digest
::SHA-
>new('sha1');
1154 foreach my $rule (sort @$rules) { # note: sorted
1155 $digest->add($rule);
1157 return $digest->b64digest;
1160 sub ipset_get_chains
{
1168 return if $line =~ m/^#/;
1169 return if $line =~ m/^\s*$/;
1170 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
1172 $line =~ s/\s+$//; # delete trailing white space
1173 push @{$chains->{$chain}}, $line;
1175 # simply ignore the rest
1180 run_command
("/usr/sbin/ipset save", outfunc
=> $parser);
1182 # compute digest for each chain
1183 foreach my $chain (keys %$chains) {
1184 $res->{$chain} = ipset_chain_digest
($chains->{$chain});
1190 sub ruleset_generate_cmdstr
{
1191 my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf) = @_;
1193 return if defined($rule->{enable
}) && !$rule->{enable
};
1195 die "unable to emit macro - internal error" if $rule->{macro}; # should not happen
1197 my $nbdport = defined($rule->{dport
}) ? parse_port_name_number_or_range
($rule->{dport
}) : 0;
1198 my $nbsport = defined($rule->{sport
}) ? parse_port_name_number_or_range
($rule->{sport
}) : 0;
1202 push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in
};
1203 push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out
};
1205 my $source = $rule->{source
};
1206 my $dest = $rule->{dest
};
1209 if ($source =~ m/^\+/) {
1210 if ($source =~ m/^\+(${security_group_name_pattern})$/) {
1211 die "no such ipset '$1'\n" if !$cluster_conf->{ipset
}->{$1};
1212 push @cmd, "-m set --match-set PVEFW-$1 src";
1214 die "invalid security group name '$source'\n";
1216 } elsif ($source =~ m/^${ip_alias_pattern}$/){
1217 die "no such alias $source\n" if !$cluster_conf->{aliases
}->{$source};
1218 push @cmd, "-s $cluster_conf->{aliases}->{$source}";
1220 } elsif ($source =~ m/\-/){
1221 push @cmd, "-m iprange --src-range $source";
1224 push @cmd, "-s $source";
1229 if ($dest =~ m/^\+/) {
1230 if ($dest =~ m/^\+(${security_group_name_pattern})$/) {
1231 die "no such ipset '$1'\n" if !$cluster_conf->{ipset
}->{$1};
1232 push @cmd, "-m set --match-set PVEFW-$1 dst";
1234 die "invalid security group name '$dest'\n";
1236 } elsif ($dest =~ m/^${ip_alias_pattern}$/){
1237 die "no such alias $dest" if !$cluster_conf->{aliases
}->{$dest};
1238 push @cmd, "-d $cluster_conf->{aliases}->{$dest}";
1240 } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){
1241 push @cmd, "-m iprange --dst-range $dest";
1244 push @cmd, "-d $dest";
1248 if ($rule->{proto
}) {
1249 push @cmd, "-p $rule->{proto}";
1252 $multiport++ if $nbdport > 1;
1253 $multiport++ if $nbsport > 1;
1255 push @cmd, "--match multiport" if $multiport;
1257 die "multiport: option '--sports' cannot be used together with '--dports'\n"
1258 if ($multiport == 2) && ($rule->{dport
} ne $rule->{sport
});
1260 if ($rule->{dport
}) {
1261 if ($rule->{proto
} && $rule->{proto
} eq 'icmp') {
1262 # Note: we use dport to store --icmp-type
1263 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport
}});
1264 push @cmd, "-m icmp --icmp-type $rule->{dport}";
1267 if ($multiport == 2) {
1268 push @cmd, "--ports $rule->{dport}";
1270 push @cmd, "--dports $rule->{dport}";
1273 push @cmd, "--dport $rule->{dport}";
1278 if ($rule->{sport
}) {
1280 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
1282 push @cmd, "--sport $rule->{sport}";
1285 } elsif ($rule->{dport
} || $rule->{sport
}) {
1286 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport
};
1287 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport
};
1290 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype
};
1292 if (my $action = $rule->{action
}) {
1293 $action = $actions->{$action} if defined($actions->{$action});
1294 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
1295 push @cmd, $goto ?
"-g $action" : "-j $action";
1298 return scalar(@cmd) ?
join(' ', @cmd) : undef;
1301 sub ruleset_generate_rule
{
1302 my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf) = @_;
1306 if ($rule->{macro}) {
1307 $rules = &$apply_macro($rule->{macro}, $rule);
1312 # update all or nothing
1315 foreach my $tmp (@$rules) {
1316 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $tmp, $actions, $goto, $cluster_conf)) {
1317 push @cmds, $cmdstr;
1321 foreach my $cmdstr (@cmds) {
1322 ruleset_addrule
($ruleset, $chain, $cmdstr);
1326 sub ruleset_generate_rule_insert
{
1327 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
1329 die "implement me" if $rule->{macro}; # not implemented, because not needed so far
1331 if (my $cmdstr = ruleset_generate_cmdstr
($ruleset, $chain, $rule, $actions, $goto)) {
1332 ruleset_insertrule
($ruleset, $chain, $cmdstr);
1336 sub ruleset_create_chain
{
1337 my ($ruleset, $chain) = @_;
1339 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
1340 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
1342 die "chain '$chain' already exists\n" if $ruleset->{$chain};
1344 $ruleset->{$chain} = [];
1347 sub ruleset_chain_exist
{
1348 my ($ruleset, $chain) = @_;
1350 return $ruleset->{$chain} ?
1 : undef;
1353 sub ruleset_addrule
{
1354 my ($ruleset, $chain, $rule) = @_;
1356 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1358 push @{$ruleset->{$chain}}, "-A $chain $rule";
1361 sub ruleset_insertrule
{
1362 my ($ruleset, $chain, $rule) = @_;
1364 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1366 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
1369 sub get_log_rule_base
{
1370 my ($chain, $vmid, $msg, $loglevel) = @_;
1372 die "internal error - no log level" if !defined($loglevel);
1374 $vmid = 0 if !defined($vmid);
1376 # Note: we use special format for prefix to pass further
1377 # info to log daemon (VMID, LOGVELEL and CHAIN)
1379 return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
1382 sub ruleset_addlog {
1383 my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_;
1385 return if !defined($loglevel);
1387 my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel);
1389 $logrule = "$rule $logrule" if defined($rule);
1391 ruleset_addrule($ruleset, $chain, $logrule)
1394 sub generate_bridge_chains {
1395 my ($ruleset, $hostfw_conf, $bridge, $routing_table, $bridges_config) = @_;
1397 my $options = $hostfw_conf->{options} || {};
1399 die "error
: detected direct route to bridge
'$bridge'\n"
1400 if !$options->{allow_bridge_route} && $routing_table->{$bridge};
1402 if (!ruleset_chain_exist($ruleset, "$bridge-FW
")) {
1403 ruleset_create_chain($ruleset, "$bridge-FW
");
1404 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-FW
");
1405 ruleset_addrule($ruleset, "PVEFW-FORWARD
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-FW
");
1408 if (!ruleset_chain_exist($ruleset, "$bridge-OUT
")) {
1409 ruleset_create_chain($ruleset, "$bridge-OUT
");
1411 if($options->{optimize}){
1412 foreach my $interface (@{$bridges_config->{$bridge}}) {
1413 ruleset_addrule($ruleset, "$bridge-OUT
", "-m physdev
--physdev-is-bridged
--physdev-in
$interface -g PVEFW-SET-ACCEPT-MARK
");
1417 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-in
-j
$bridge-OUT
");
1418 ruleset_insertrule($ruleset, "PVEFW-INPUT
", "-i
$bridge -m physdev
--physdev-is-in
-j
$bridge-OUT
");
1421 if (!ruleset_chain_exist($ruleset, "$bridge-IN
")) {
1422 ruleset_create_chain($ruleset, "$bridge-IN
");
1424 if($options->{optimize}){
1425 foreach my $interface (@{$bridges_config->{$bridge}}) {
1426 ruleset_addrule($ruleset, "$bridge-IN
", "-m physdev
--physdev-is-bridged
--physdev-out
$interface -j ACCEPT
");
1430 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j
$bridge-IN
");
1431 ruleset_addrule($ruleset, "$bridge-FW
", "-m mark
--mark
1 -j ACCEPT
");
1432 # accept traffic to unmanaged bridge ports
1433 ruleset_addrule($ruleset, "$bridge-FW
", "-m physdev
--physdev-is-out
-j ACCEPT
");
1437 sub ruleset_add_chain_policy {
1438 my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_;
1440 if ($policy eq 'ACCEPT') {
1442 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
1443 { ACCEPT => $accept_action});
1445 } elsif ($policy eq 'DROP') {
1447 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop
");
1449 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1451 ruleset_addrule($ruleset, $chain, "-j DROP
");
1452 } elsif ($policy eq 'REJECT') {
1453 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject
");
1455 ruleset_addlog($ruleset, $chain, $vmid, "policy
$policy: ", $loglevel);
1457 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject
");
1460 die "internal error
: unknown policy
'$policy'";
1464 sub ruleset_create_vm_chain {
1465 my ($ruleset, $chain, $options, $host_options, $macaddr, $direction) = @_;
1467 ruleset_create_chain($ruleset, $chain);
1468 my $accept = generate_nfqueue($options);
1470 if (!(defined($host_options->{nosmurfs}) && $host_options->{nosmurfs} == 0)) {
1471 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1474 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
1475 if ($direction eq 'OUT') {
1476 ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK',
1477 proto => 'udp', sport => 68, dport => 67 });
1479 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT',
1480 proto => 'udp', sport => 67, dport => 68 });
1484 if ($host_options->{tcpflags}) {
1485 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1488 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1489 if ($direction eq 'OUT') {
1490 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-g PVEFW-SET-ACCEPT-MARK
");
1492 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j
$accept");
1495 if ($direction eq 'OUT') {
1496 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1497 ruleset_addrule($ruleset, $chain, "-m mac
! --mac-source
$macaddr -j DROP
");
1499 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1503 sub ruleset_generate_vm_rules {
1504 my ($ruleset, $rules, $cluster_conf, $chain, $netid, $direction, $options) = @_;
1506 my $lc_direction = lc($direction);
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 my $group_chain = "GROUP-
$rule->{action
}-$direction";
1513 if(!ruleset_chain_exist($ruleset, $group_chain)){
1514 generate_group_rules($ruleset, $cluster_conf, $rule->{action});
1516 ruleset_addrule($ruleset, $chain, "-j
$group_chain");
1517 if ($direction eq 'OUT'){
1518 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j RETURN
");
1520 my $accept = generate_nfqueue($options);
1521 ruleset_addrule($ruleset, $chain, "-m mark
--mark
1 -j
$accept");
1525 next if $rule->{type} ne $lc_direction;
1526 if ($direction eq 'OUT') {
1527 ruleset_generate_rule($ruleset, $chain, $rule,
1528 { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1530 my $accept = generate_nfqueue($options);
1531 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1537 sub generate_nfqueue {
1540 if ($options->{ips}) {
1541 my $action = "NFQUEUE
";
1542 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
1543 if (defined($3) && defined($1)) {
1544 $action .= " --queue-balance
$1:$3";
1545 } elsif (defined($1)) {
1546 $action .= " --queue-num
$1";
1549 $action .= " --queue-bypass
" if $feature_ipset_nomatch; #need kernel 3.10
1556 sub ruleset_generate_vm_ipsrules {
1557 my ($ruleset, $options, $direction, $iface, $bridge) = @_;
1559 if ($options->{ips} && $direction eq 'IN') {
1560 my $nfqueue = generate_nfqueue($options);
1562 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1563 ruleset_create_chain($ruleset, "PVEFW-IPS
");
1566 if (!ruleset_chain_exist($ruleset, "$bridge-IPS
")) {
1567 ruleset_create_chain($ruleset, "$bridge-IPS
");
1568 ruleset_insertrule($ruleset, "PVEFW-IPS
", "-o
$bridge -m physdev
--physdev-is-out
-j
$bridge-IPS
");
1571 ruleset_addrule($ruleset, "$bridge-IPS
", "-m physdev
--physdev-out
$iface --physdev-is-bridged
-j
$nfqueue");
1575 sub generate_venet_rules_direction {
1576 my ($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, $direction) = @_;
1578 parse_address_list($ip); # make sure we have a valid $ip list
1580 my $lc_direction = lc($direction);
1582 my $rules = $vmfw_conf->{rules};
1584 my $options = $vmfw_conf->{options};
1585 my $hostfw_options = $vmfw_conf->{options};
1586 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1588 my $chain = "venet0-
$vmid-$direction";
1590 ruleset_create_vm_chain($ruleset, $chain, $options, $hostfw_options, undef, $direction);
1592 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $chain, 'venet', $direction);
1597 if ($direction eq 'OUT') {
1598 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1600 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1603 my $accept = generate_nfqueue($options);
1604 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1605 ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action);
1607 # plug into FORWARD, INPUT and OUTPUT chain
1608 if ($direction eq 'OUT') {
1609 ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD
", {
1612 iface_in => 'venet0'});
1614 ruleset_generate_rule_insert($ruleset, "PVEFW-INPUT
", {
1617 iface_in => 'venet0'});
1619 ruleset_generate_rule($ruleset, "PVEFW-FORWARD
", {
1622 iface_out => 'venet0'});
1624 ruleset_generate_rule($ruleset, "PVEFW-OUTPUT
", {
1627 iface_out => 'venet0'});
1631 sub generate_tap_rules_direction {
1632 my ($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_;
1634 my $lc_direction = lc($direction);
1636 my $rules = $vmfw_conf->{rules};
1638 my $options = $vmfw_conf->{options};
1639 my $hostfw_options = $hostfw_conf->{options};
1640 my $loglevel = get_option_log_level($options, "log_level_
${lc_direction
}");
1642 my $tapchain = "$iface-$direction";
1644 ruleset_create_vm_chain($ruleset, $tapchain, $options, $hostfw_options, $macaddr, $direction);
1646 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $tapchain, $netid, $direction, $options);
1648 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface, $bridge);
1653 if ($direction eq 'OUT') {
1654 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1656 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1659 my $accept = generate_nfqueue($options);
1660 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK
" : $accept;
1661 ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action);
1663 # plug the tap chain to bridge chain
1664 if ($direction eq 'IN') {
1665 ruleset_addrule($ruleset, "$bridge-IN
",
1666 "-m physdev
--physdev-is-bridged
--physdev-out
$iface -j
$tapchain");
1668 ruleset_addrule($ruleset, "$bridge-OUT
",
1669 "-m physdev
--physdev-in
$iface -j
$tapchain");
1673 sub enable_host_firewall {
1674 my ($ruleset, $hostfw_conf, $cluster_conf) = @_;
1676 my $options = $hostfw_conf->{options};
1677 my $cluster_options = $cluster_conf->{options};
1678 my $rules = $hostfw_conf->{rules};
1679 my $cluster_rules = $cluster_conf->{rules};
1681 # host inbound firewall
1682 my $chain = "PVEFW-HOST-IN
";
1683 ruleset_create_chain($ruleset, $chain);
1685 my $loglevel = get_option_log_level($options, "log_level_in
");
1687 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
1688 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
,NEW
-j PVEFW-smurfs
");
1691 if ($options->{tcpflags}) {
1692 ruleset_addrule($ruleset, $chain, "-p tcp
-j PVEFW-tcpflags
");
1695 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1696 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1697 ruleset_addrule($ruleset, $chain, "-i lo
-j ACCEPT
");
1698 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1699 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1700 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1702 # we use RETURN because we need to check also tap rules
1703 my $accept_action = 'RETURN';
1705 # add host rules first, so that cluster wide rules can be overwritten
1706 foreach my $rule (@$rules, @$cluster_rules) {
1707 next if $rule->{type} ne 'in';
1708 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1711 # implement input policy
1712 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
1713 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1715 # host outbound firewall
1716 $chain = "PVEFW-HOST-OUT
";
1717 ruleset_create_chain($ruleset, $chain);
1719 $loglevel = get_option_log_level($options, "log_level_out
");
1721 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate INVALID
-j DROP
");
1722 ruleset_addrule($ruleset, $chain, "-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
");
1723 ruleset_addrule($ruleset, $chain, "-o lo
-j ACCEPT
");
1724 ruleset_addrule($ruleset, $chain, "-m addrtype
--dst-type MULTICAST
-j ACCEPT
");
1725 ruleset_addrule($ruleset, $chain, "-p udp
-m conntrack
--ctstate NEW
--dport
5404:5405 -j ACCEPT
");
1726 ruleset_addrule($ruleset, $chain, "-p udp
-m udp
--dport
9000 -j ACCEPT
"); #corosync
1728 # we use RETURN because we may want to check other thigs later
1729 $accept_action = 'RETURN';
1731 # add host rules first, so that cluster wide rules can be overwritten
1732 foreach my $rule (@$rules, @$cluster_rules) {
1733 next if $rule->{type} ne 'out';
1734 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1737 # implement output policy
1738 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
1739 ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action);
1741 ruleset_addrule($ruleset, "PVEFW-OUTPUT
", "-j PVEFW-HOST-OUT
");
1742 ruleset_addrule($ruleset, "PVEFW-INPUT
", "-j PVEFW-HOST-IN
");
1745 sub generate_group_rules {
1746 my ($ruleset, $cluster_conf, $group) = @_;
1747 die "no such security group
'$group'\n" if !$cluster_conf->{groups}->{$group};
1749 my $rules = $cluster_conf->{groups}->{$group};
1751 my $chain = "GROUP-
${group
}-IN
";
1753 ruleset_create_chain($ruleset, $chain);
1754 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1756 foreach my $rule (@$rules) {
1757 next if $rule->{type} ne 'in';
1758 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK
", REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1761 $chain = "GROUP-
${group
}-OUT
";
1763 ruleset_create_chain($ruleset, $chain);
1764 ruleset_addrule($ruleset, $chain, "-j MARK
--set-mark
0"); # clear mark
1766 foreach my $rule (@$rules) {
1767 next if $rule->{type} ne 'out';
1768 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1769 # check also other tap rules later
1770 ruleset_generate_rule($ruleset, $chain, $rule,
1771 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject
" }, undef, $cluster_conf);
1776 my $valid_netdev_names = {};
1777 for (my $i = 0; $i < $MAX_NETS; $i++) {
1778 $valid_netdev_names->{"net
$i"} = 1;
1782 my ($line, $need_iface, $allow_groups) = @_;
1784 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1786 # we can add single line comments to the end of the rule
1787 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
1789 # we can disable a rule when prefixed with '|'
1792 $enable = 0 if $line =~ s/^\|//;
1794 my @data = split(/\s+/, $line);
1795 my $expected_elements = $need_iface ? 8 : 7;
1797 die "wrong number of rule elements
\n" if scalar(@data) > $expected_elements;
1800 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1802 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1805 die "incomplete rule
\n" if ! ($type && $action);
1811 if ($type eq 'in' || $type eq 'out') {
1812 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1814 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1816 my $preferred_name = $pve_fw_preferred_macro_names->{lc($1)};
1817 die "unknown
macro '$1'\n" if !$preferred_name;
1818 $macro = $preferred_name;
1820 die "unknown action
'$action'\n";
1822 } elsif ($type eq 'group') {
1823 die "wrong number of rule elements
\n" if scalar(@data) != 3;
1824 die "groups disabled
\n" if !$allow_groups;
1826 die "invalid characters
in group name
\n" if $action !~ m/^${security_group_name_pattern}$/;
1828 die "unknown rule type
'$type'\n";
1832 $iface = undef if $iface && $iface eq '-';
1835 $proto = undef if $proto && $proto eq '-';
1836 pve_fw_verify_protocol_spec($proto) if $proto;
1838 $source = undef if $source && $source eq '-';
1839 $dest = undef if $dest && $dest eq '-';
1841 $dport = undef if $dport && $dport eq '-';
1842 $sport = undef if $sport && $sport eq '-';
1844 parse_port_name_number_or_range($dport) if defined($dport);
1845 parse_port_name_number_or_range($sport) if defined($sport);
1847 parse_address_list($source) if $source;
1848 parse_address_list($dest) if $dest;
1853 comment => $comment,
1865 sub parse_vmfw_option {
1870 my $loglevels = "emerg
|alert
|crit
|err
|warning
|notice
|info
|debug
|nolog
";
1872 if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) {
1875 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1877 $value = $2 ? lc($3) : '';
1878 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1881 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
1886 die "can
't parse option '$line'\n"
1889 return ($opt, $value);
1892 sub parse_hostfw_option {
1897 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1899 if ($line =~ m/^(enable|nosmurfs|tcpflags|allow_bridge_route|optimize):\s*(0|1)\s*$/i) {
1902 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1904 $value = $2 ? lc($3) : '';
1905 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
1910 die "can't parse option
'$line'\n"
1913 return ($opt, $value);
1916 sub parse_clusterfw_option {
1921 if ($line =~ m/^(enable):\s*(0|1)\s*$/i) {
1924 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1929 die "can
't parse option '$line'\n"
1932 return ($opt, $value);
1935 sub parse_clusterfw_alias {
1939 if ($line =~ m/^(\S+)\s(\S+)$/) {
1943 pve_verify_ipv4_or_cidr($2) if $2;
1948 die "can't parse option
'$line'\n";
1951 return ($opt, $value);
1954 sub parse_vm_fw_rules {
1955 my ($filename, $fh) = @_;
1957 my $res = { rules => [], options => {}};
1961 while (defined(my $line = <$fh>)) {
1962 next if $line =~ m/^#/;
1963 next if $line =~ m/^\s*$/;
1965 my $linenr = $fh->input_line_number();
1966 my $prefix = "$filename (line
$linenr)";
1968 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1970 warn "$prefix: ignore unknown section
'$section'\n" if !$res->{$section};
1974 warn "$prefix: skip line
- no section
";
1978 next if !$res->{$section}; # skip undefined section
1980 if ($section eq 'options') {
1982 my ($opt, $value) = parse_vmfw_option($line);
1983 $res->{options}->{$opt} = $value;
1985 warn "$prefix: $@" if $@;
1990 eval { $rule = parse_fw_rule($line, 1, 1); };
1992 warn "$prefix: $err";
1996 push @{$res->{$section}}, $rule;
2002 sub parse_host_fw_rules {
2003 my ($filename, $fh) = @_;
2005 my $res = { rules => [], options => {}};
2009 while (defined(my $line = <$fh>)) {
2010 next if $line =~ m/^#/;
2011 next if $line =~ m/^\s*$/;
2013 my $linenr = $fh->input_line_number();
2014 my $prefix = "$filename (line
$linenr)";
2016 if ($line =~ m/^\[(\S+)\]\s*$/i) {
2018 warn "$prefix: ignore unknown section
'$section'\n" if !$res->{$section};
2022 warn "$prefix: skip line
- no section
";
2026 next if !$res->{$section}; # skip undefined section
2028 if ($section eq 'options') {
2030 my ($opt, $value) = parse_hostfw_option($line);
2031 $res->{options}->{$opt} = $value;
2033 warn "$prefix: $@" if $@;
2038 eval { $rule = parse_fw_rule($line, 1, 1); };
2040 warn "$prefix: $err";
2044 push @{$res->{$section}}, $rule;
2050 sub parse_cluster_fw_rules {
2051 my ($filename, $fh) = @_;
2060 group_comments => {},
2062 ipset_comments => {},
2065 while (defined(my $line = <$fh>)) {
2066 next if $line =~ m/^#/;
2067 next if $line =~ m/^\s*$/;
2069 my $linenr = $fh->input_line_number();
2070 my $prefix = "$filename (line
$linenr)";
2072 if ($line =~ m/^\[options\]$/i) {
2073 $section = 'options';
2077 if ($line =~ m/^\[aliases\]$/i) {
2078 $section = 'aliases';
2082 if ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i) {
2083 $section = 'groups';
2086 $res->{$section}->{$group} = [];
2087 $res->{group_comments}->{$group} = decode('utf8', $comment)
2092 if ($line =~ m/^\[rules\]$/i) {
2097 if ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i) {
2101 $res->{$section}->{$group} = [];
2102 $res->{ipset_comments}->{$group} = decode('utf8', $comment)
2108 warn "$prefix: skip line
- no section
\n";
2112 if ($section eq 'options') {
2114 my ($opt, $value) = parse_clusterfw_option($line);
2115 $res->{options}->{$opt} = $value;
2117 warn "$prefix: $@" if $@;
2118 } elsif ($section eq 'aliases') {
2120 my ($opt, $value) = parse_clusterfw_alias($line);
2121 $res->{aliases}->{$opt} = $value;
2123 warn "$prefix: $@" if $@;
2124 } elsif ($section eq 'rules') {
2126 eval { $rule = parse_fw_rule($line, 1, 1); };
2128 warn "$prefix: $err";
2131 push @{$res->{$section}}, $rule;
2132 } elsif ($section eq 'groups') {
2134 eval { $rule = parse_fw_rule($line, 0, 0); };
2136 warn "$prefix: $err";
2139 push @{$res->{$section}->{$group}}, $rule;
2140 } elsif ($section eq 'ipset') {
2141 # we can add single line comments to the end of the rule
2142 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
2144 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2148 if($cidr !~ m/^${ip_alias_pattern}$/) {
2151 eval { pve_verify_ipv4_or_cidr($cidr); };
2153 warn "$prefix: $cidr - $err";
2158 my $entry = { cidr => $cidr };
2159 $entry->{nomatch} = 1 if $nomatch;
2160 $entry->{comment} = $comment if $comment;
2162 push @{$res->{$section}->{$group}}, $entry;
2170 my ($code, @param) = @_;
2174 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2181 sub read_local_vm_config {
2186 my $vmdata = { openvz => $openvz, qemu => $qemu };
2188 my $vmlist = PVE::Cluster::get_vmlist();
2189 return $vmdata if !$vmlist || !$vmlist->{ids};
2190 my $ids = $vmlist->{ids};
2192 foreach my $vmid (keys %$ids) {
2193 next if !$vmid; # skip VE0
2194 my $d = $ids->{$vmid};
2195 next if !$d->{node} || $d->{node} ne $nodename;
2196 next if !$d->{type};
2197 if ($d->{type} eq 'openvz') {
2198 if ($have_pve_manager) {
2199 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
2200 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2201 $openvz->{$vmid} = $conf;
2204 } elsif ($d->{type} eq 'qemu') {
2205 if ($have_qemu_server) {
2206 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
2207 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
2208 $qemu->{$vmid} = $conf;
2217 sub read_bridges_config {
2219 my $bridgehash = {};
2221 dir_glob_foreach('/sys/class/net', 'vmbr(\d+)', sub {
2224 dir_glob_foreach("/sys/class/net/$bridge/brif", '((eth|bond)(\d+)(\.(\d+))?)', sub {
2225 my ($interface) = @_;
2226 push @{$bridgehash->{$bridge}}, $interface;
2233 sub load_vmfw_conf {
2238 my $filename = "/etc/pve
/firewall/$vmid.fw
";
2239 if (my $fh = IO::File->new($filename, O_RDONLY)) {
2240 $vmfw_conf = parse_vm_fw_rules($filename, $fh);
2246 my $format_rules = sub {
2247 my ($rules, $need_iface) = @_;
2251 foreach my $rule (@$rules) {
2252 if ($rule->{type} eq 'in' || $rule->{type} eq 'out' || $rule->{type} eq 'group') {
2253 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
2254 $raw .= uc($rule->{type});
2255 if ($rule->{macro}) {
2256 $raw .= " $rule->{macro}($rule->{action
})";
2258 $raw .= " " . $rule->{action};
2260 $raw .= " " . ($rule->{iface} || '-') if $need_iface;
2262 if ($rule->{type} ne 'group') {
2263 $raw .= " " . ($rule->{source} || '-');
2264 $raw .= " " . ($rule->{dest} || '-');
2265 $raw .= " " . ($rule->{proto} || '-');
2266 $raw .= " " . ($rule->{dport} || '-');
2267 $raw .= " " . ($rule->{sport} || '-');
2270 $raw .= " # " . encode('utf8', $rule->{comment})
2271 if $rule->{comment
} && $rule->{comment
} !~ m/^\s*$/;
2274 die "unknown rule type '$rule->{type}'";
2281 my $format_options = sub {
2286 $raw .= "[OPTIONS]\n\n";
2287 foreach my $opt (keys %$options) {
2288 $raw .= "$opt: $options->{$opt}\n";
2295 my $format_ipset = sub {
2301 foreach my $entry (@$options) {
2302 $nethash->{$entry->{cidr
}} = $entry;
2305 foreach my $cidr (sort keys %$nethash) {
2306 my $entry = $nethash->{$cidr};
2307 my $line = $entry->{nomatch
} ?
'!' : '';
2308 $line .= $entry->{cidr
};
2309 $line .= " # " . encode
('utf8', $entry->{comment
})
2310 if $entry->{comment
} && $entry->{comment
} !~ m/^\s*$/;
2317 sub save_vmfw_conf
{
2318 my ($vmid, $vmfw_conf) = @_;
2322 my $options = $vmfw_conf->{options
};
2323 $raw .= &$format_options($options) if scalar(keys %$options);
2325 my $rules = $vmfw_conf->{rules
};
2326 if (scalar(@$rules)) {
2327 $raw .= "[RULES]\n\n";
2328 $raw .= &$format_rules($rules, 1);
2332 my $filename = "/etc/pve/firewall/$vmid.fw";
2333 PVE
::Tools
::file_set_contents
($filename, $raw);
2336 sub read_vm_firewall_configs
{
2338 my $vmfw_configs = {};
2340 foreach my $vmid (keys %{$vmdata->{qemu
}}, keys %{$vmdata->{openvz
}}) {
2341 my $vmfw_conf = load_vmfw_conf
($vmid);
2342 next if !$vmfw_conf->{options
}; # skip if file does not exists
2343 $vmfw_configs->{$vmid} = $vmfw_conf;
2346 return $vmfw_configs;
2349 sub get_option_log_level
{
2350 my ($options, $k) = @_;
2352 my $v = $options->{$k};
2353 $v = $default_log_level if !defined($v);
2355 return undef if $v eq '' || $v eq 'nolog';
2357 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
2359 return $v if ($v >= 0) && ($v <= 7);
2361 warn "unknown log level ($k = '$v')\n";
2366 sub generate_std_chains
{
2367 my ($ruleset, $options) = @_;
2369 my $loglevel = get_option_log_level
($options, 'smurf_log_level');
2371 # same as shorewall smurflog.
2372 my $chain = 'PVEFW-smurflog';
2373 $pve_std_chains->{$chain} = [];
2375 push @{$pve_std_chains->{$chain}}, get_log_rule_base
($chain, 0, "DROP: ", $loglevel) if $loglevel;
2376 push @{$pve_std_chains->{$chain}}, "-j DROP";
2378 # same as shorewall logflags action.
2379 $loglevel = get_option_log_level
($options, 'tcp_flags_log_level');
2380 $chain = 'PVEFW-logflags';
2381 $pve_std_chains->{$chain} = [];
2383 # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG)
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 foreach my $chain (keys %$pve_std_chains) {
2388 ruleset_create_chain
($ruleset, $chain);
2389 foreach my $rule (@{$pve_std_chains->{$chain}}) {
2391 ruleset_generate_rule
($ruleset, $chain, $rule);
2393 ruleset_addrule
($ruleset, $chain, $rule);
2399 sub generate_ipset_chains
{
2400 my ($ipset_ruleset, $fw_conf) = @_;
2402 foreach my $ipset (keys %{$fw_conf->{ipset
}}) {
2403 generate_ipset
($ipset_ruleset, "PVEFW-$ipset", $fw_conf->{ipset
}->{$ipset}, $fw_conf->{aliases
});
2407 sub generate_ipset
{
2408 my ($ipset_ruleset, $name, $options, $aliases) = @_;
2410 my $hashsize = scalar(@$options);
2411 if ($hashsize <= 64) {
2414 $hashsize = round_powerof2
($hashsize);
2417 push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize";
2421 foreach my $entry (@$options) {
2422 my $cidr = $entry->{cidr
};
2423 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2424 if ($aliases->{$cidr}) {
2425 $entry->{cidr
} = $aliases->{$cidr};
2427 warn "no such alias '$cidr'\n" if !$aliases->{$cidr};
2430 $nethash->{$entry->{cidr
}} = $entry;
2433 foreach my $cidr (sort keys %$nethash) {
2434 my $entry = $nethash->{$cidr};
2436 my $cmd = "add $name $cidr";
2437 if ($entry->{nomatch
}) {
2438 if ($feature_ipset_nomatch) {
2439 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
2441 warn "ignore !$cidr - nomatch not supported by kernel\n";
2444 push @{$ipset_ruleset->{$name}}, $cmd;
2449 sub round_powerof2
{
2453 $int |= $int >> $_ foreach (1,2,4,8,16);
2457 sub save_pvefw_status
{
2460 die "unknown status '$status' - internal error"
2461 if $status !~ m/^(stopped|active)$/;
2463 mkdir dirname
($pve_fw_status_filename);
2464 PVE
::Tools
::file_set_contents
($pve_fw_status_filename, $status);
2467 sub read_pvefw_status
{
2469 my $status = 'unknown';
2471 return 'stopped' if ! -f
$pve_fw_status_filename;
2474 $status = PVE
::Tools
::file_get_contents
($pve_fw_status_filename);
2481 # fixme: move to pve-common PVE::ProcFSTools
2482 sub read_proc_net_route
{
2483 my $filename = "/proc/net/route";
2487 my $fh = IO
::File-
>new ($filename, "r");
2488 return $res if !$fh;
2490 my $int_to_quad = sub {
2491 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
2494 while (defined(my $line = <$fh>)) {
2495 next if $line =~/^Iface\s+Destination/; # skip head
2496 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
2497 push @{$res->{$iface}}, {
2498 dest
=> &$int_to_quad(hex($dest)),
2499 gateway
=> &$int_to_quad(hex($gateway)),
2500 mask
=> &$int_to_quad(hex($mask)),
2509 sub load_clusterfw_conf
{
2511 my $cluster_conf = {};
2512 if (my $fh = IO
::File-
>new($clusterfw_conf_filename, O_RDONLY
)) {
2513 $cluster_conf = parse_cluster_fw_rules
($clusterfw_conf_filename, $fh);
2516 return $cluster_conf;
2519 sub save_clusterfw_conf
{
2520 my ($cluster_conf) = @_;
2524 my $options = $cluster_conf->{options
};
2525 $raw .= &$format_options($options) if scalar(keys %$options);
2527 foreach my $ipset (sort keys %{$cluster_conf->{ipset
}}) {
2528 if (my $comment = $cluster_conf->{ipset_comments
}->{$ipset}) {
2529 my $utf8comment = encode
('utf8', $comment);
2530 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
2532 $raw .= "[IPSET $ipset]\n\n";
2534 my $options = $cluster_conf->{ipset
}->{$ipset};
2535 $raw .= &$format_ipset($options);
2539 my $rules = $cluster_conf->{rules
};
2540 if (scalar(@$rules)) {
2541 $raw .= "[RULES]\n\n";
2542 $raw .= &$format_rules($rules, 1);
2546 foreach my $group (sort keys %{$cluster_conf->{groups
}}) {
2547 my $rules = $cluster_conf->{groups
}->{$group};
2548 if (my $comment = $cluster_conf->{group_comments
}->{$group}) {
2549 my $utf8comment = encode
('utf8', $comment);
2550 $raw .= "[group $group] # $utf8comment\n\n";
2552 $raw .= "[group $group]\n\n";
2555 $raw .= &$format_rules($rules, 0);
2559 PVE
::Tools
::file_set_contents
($clusterfw_conf_filename, $raw);
2562 sub load_hostfw_conf
{
2564 my $hostfw_conf = {};
2565 if (my $fh = IO
::File-
>new($hostfw_conf_filename, O_RDONLY
)) {
2566 $hostfw_conf = parse_host_fw_rules
($hostfw_conf_filename, $fh);
2568 return $hostfw_conf;
2571 sub save_hostfw_conf
{
2572 my ($hostfw_conf) = @_;
2576 my $options = $hostfw_conf->{options
};
2577 $raw .= &$format_options($options) if scalar(keys %$options);
2579 my $rules = $hostfw_conf->{rules
};
2580 if (scalar(@$rules)) {
2581 $raw .= "[RULES]\n\n";
2582 $raw .= &$format_rules($rules, 1);
2586 PVE
::Tools
::file_set_contents
($hostfw_conf_filename, $raw);
2590 my ($cluster_conf, $hostfw_conf) = @_;
2592 $cluster_conf = load_clusterfw_conf
() if !$cluster_conf;
2593 $hostfw_conf = load_hostfw_conf
() if !$hostfw_conf;
2595 my $vmdata = read_local_vm_config
();
2596 my $vmfw_configs = read_vm_firewall_configs
($vmdata);
2598 my $routing_table = read_proc_net_route
();
2600 my $bridges_config = read_bridges_config
();
2602 my $ipset_ruleset = {};
2603 generate_ipset_chains
($ipset_ruleset, $cluster_conf);
2607 ruleset_create_chain
($ruleset, "PVEFW-INPUT");
2608 ruleset_create_chain
($ruleset, "PVEFW-OUTPUT");
2610 ruleset_create_chain
($ruleset, "PVEFW-FORWARD");
2612 my $hostfw_options = $hostfw_conf->{options
} || {};
2614 generate_std_chains
($ruleset, $hostfw_options);
2616 my $hostfw_enable = !(defined($hostfw_options->{enable
}) && ($hostfw_options->{enable
} == 0));
2618 enable_host_firewall
($ruleset, $hostfw_conf, $cluster_conf) if $hostfw_enable;
2620 # generate firewall rules for QEMU VMs
2621 foreach my $vmid (keys %{$vmdata->{qemu
}}) {
2622 my $conf = $vmdata->{qemu
}->{$vmid};
2623 my $vmfw_conf = $vmfw_configs->{$vmid};
2624 next if !$vmfw_conf;
2625 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
2627 foreach my $netid (keys %$conf) {
2628 next if $netid !~ m/^net(\d+)$/;
2629 my $net = PVE
::QemuServer
::parse_net
($conf->{$netid});
2631 my $iface = "tap${vmid}i$1";
2633 my $bridge = $net->{bridge
};
2634 next if !$bridge; # fixme: ?
2636 $bridge .= "v$net->{tag}" if $net->{tag
};
2638 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge, $routing_table, $bridges_config);
2640 my $macaddr = $net->{macaddr
};
2641 generate_tap_rules_direction
($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr,
2642 $vmfw_conf, $vmid, $bridge, 'IN');
2643 generate_tap_rules_direction
($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr,
2644 $vmfw_conf, $vmid, $bridge, 'OUT');
2648 # generate firewall rules for OpenVZ containers
2649 foreach my $vmid (keys %{$vmdata->{openvz
}}) {
2650 my $conf = $vmdata->{openvz
}->{$vmid};
2652 my $vmfw_conf = $vmfw_configs->{$vmid};
2653 next if !$vmfw_conf;
2654 next if defined($vmfw_conf->{options
}->{enable
}) && ($vmfw_conf->{options
}->{enable
} == 0);
2656 if ($conf->{ip_address
} && $conf->{ip_address
}->{value
}) {
2657 my $ip = $conf->{ip_address
}->{value
};
2658 generate_venet_rules_direction
($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, 'IN');
2659 generate_venet_rules_direction
($ruleset, $cluster_conf, $hostfw_conf, $vmfw_conf, $vmid, $ip, 'OUT');
2662 if ($conf->{netif
} && $conf->{netif
}->{value
}) {
2663 my $netif = PVE
::OpenVZ
::parse_netif
($conf->{netif
}->{value
});
2664 foreach my $netid (keys %$netif) {
2665 my $d = $netif->{$netid};
2666 my $bridge = $d->{bridge
};
2668 warn "no bridge device for CT $vmid iface '$netid'\n";
2672 generate_bridge_chains
($ruleset, $hostfw_conf, $bridge, $routing_table, $bridges_config);
2674 my $macaddr = $d->{mac
};
2675 my $iface = $d->{host_ifname
};
2676 generate_tap_rules_direction
($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr,
2677 $vmfw_conf, $vmid, $bridge, 'IN');
2678 generate_tap_rules_direction
($ruleset, $cluster_conf, $hostfw_conf, $iface, $netid, $macaddr,
2679 $vmfw_conf, $vmid, $bridge, 'OUT');
2684 if($hostfw_options->{optimize
}){
2686 my $accept = ruleset_chain_exist
($ruleset, "PVEFW-IPS") ?
"PVEFW-IPS" : "ACCEPT";
2687 ruleset_insertrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept");
2688 ruleset_insertrule
($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate INVALID -j DROP");
2691 # fixme: what log level should we use here?
2692 my $loglevel = get_option_log_level
($hostfw_options, "log_level_out");
2694 # fixme: should we really block inter-bridge traffic?
2696 # always allow traffic from containers?
2697 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN");
2699 # disable interbridge routing
2700 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop");
2701 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop");
2702 ruleset_addlog
($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+");
2703 ruleset_addlog
($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+");
2704 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP");
2705 ruleset_addrule
($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP");
2707 return ($ruleset, $ipset_ruleset);
2710 sub get_ruleset_status
{
2711 my ($ruleset, $active_chains, $digest_fn, $verbose) = @_;
2713 my $statushash = {};
2715 foreach my $chain (sort keys %$ruleset) {
2716 my $sig = &$digest_fn($ruleset->{$chain});
2718 $statushash->{$chain}->{sig
} = $sig;
2720 my $oldsig = $active_chains->{$chain};
2721 if (!defined($oldsig)) {
2722 $statushash->{$chain}->{action
} = 'create';
2724 if ($oldsig eq $sig) {
2725 $statushash->{$chain}->{action
} = 'exists';
2727 $statushash->{$chain}->{action
} = 'update';
2730 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
2731 foreach my $cmd (@{$ruleset->{$chain}}) {
2732 print "\t$cmd\n" if $verbose;
2736 foreach my $chain (sort keys %$active_chains) {
2737 if (!defined($ruleset->{$chain})) {
2738 my $sig = $active_chains->{$chain};
2739 $statushash->{$chain}->{action
} = 'delete';
2740 $statushash->{$chain}->{sig
} = $sig;
2741 print "delete $chain ($sig)\n" if $verbose;
2748 sub print_sig_rule
{
2749 my ($chain, $sig) = @_;
2751 # We just use this to store a SHA1 checksum used to detect changes
2752 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
2755 sub get_ruleset_cmdlist {
2756 my ($ruleset, $verbose) = @_;
2758 my $cmdlist = "*filter
\n"; # we pass this to iptables-restore;
2760 my ($active_chains, $hooks) = iptables_get_chains();
2761 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
2763 # create missing chains first
2764 foreach my $chain (sort keys %$ruleset) {
2765 my $stat = $statushash->{$chain};
2766 die "internal error
" if !$stat;
2767 next if $stat->{action} ne 'create';
2769 $cmdlist .= ":$chain - [0:0]\n";
2772 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2773 if (!$hooks->{$h}) {
2774 $cmdlist .= "-A $h -j PVEFW-$h\n";
2778 foreach my $chain (sort keys %$ruleset) {
2779 my $stat = $statushash->{$chain};
2780 die "internal error" if !$stat;
2782 if ($stat->{action
} eq 'update' || $stat->{action
} eq 'create') {
2783 $cmdlist .= "-F $chain\n";
2784 foreach my $cmd (@{$ruleset->{$chain}}) {
2785 $cmdlist .= "$cmd\n";
2787 $cmdlist .= print_sig_rule
($chain, $stat->{sig
});
2788 } elsif ($stat->{action
} eq 'delete') {
2789 die "internal error"; # this should not happen
2790 } elsif ($stat->{action
} eq 'exists') {
2793 die "internal error - unknown status '$stat->{action}'";
2797 foreach my $chain (keys %$statushash) {
2798 next if $statushash->{$chain}->{action
} ne 'delete';
2799 $cmdlist .= "-F $chain\n";
2801 foreach my $chain (keys %$statushash) {
2802 next if $statushash->{$chain}->{action
} ne 'delete';
2803 next if $chain eq 'PVEFW-INPUT';
2804 next if $chain eq 'PVEFW-OUTPUT';
2805 next if $chain eq 'PVEFW-FORWARD';
2806 $cmdlist .= "-X $chain\n";
2809 my $changes = $cmdlist ne "*filter\n" ?
1 : 0;
2811 $cmdlist .= "COMMIT\n";
2813 return wantarray ?
($cmdlist, $changes) : $cmdlist;
2816 sub get_ipset_cmdlist
{
2817 my ($ruleset, $verbose) = @_;
2821 my $delete_cmdlist = "";
2823 my $active_chains = ipset_get_chains
();
2824 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&ipset_chain_digest
, $verbose);
2826 # remove stale _swap chains
2827 foreach my $chain (keys %$active_chains) {
2828 if ($chain =~ m/^PVEFW-\S+_swap$/) {
2829 $cmdlist .= "destroy $chain\n";
2833 foreach my $chain (sort keys %$ruleset) {
2834 my $stat = $statushash->{$chain};
2835 die "internal error" if !$stat;
2837 if ($stat->{action
} eq 'create') {
2838 foreach my $cmd (@{$ruleset->{$chain}}) {
2839 $cmdlist .= "$cmd\n";
2843 if ($stat->{action
} eq 'update') {
2844 my $chain_swap = $chain."_swap";
2846 foreach my $cmd (@{$ruleset->{$chain}}) {
2847 $cmd =~ s/$chain/$chain_swap/;
2848 $cmdlist .= "$cmd\n";
2850 $cmdlist .= "swap $chain_swap $chain\n";
2851 $cmdlist .= "flush $chain_swap\n";
2852 $cmdlist .= "destroy $chain_swap\n";
2857 foreach my $chain (keys %$statushash) {
2858 next if $statushash->{$chain}->{action
} ne 'delete';
2860 $delete_cmdlist .= "flush $chain\n";
2861 $delete_cmdlist .= "destroy $chain\n";
2864 my $changes = ($cmdlist || $delete_cmdlist) ?
1 : 0;
2866 return ($cmdlist, $delete_cmdlist, $changes);
2870 my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_;
2872 enable_bridge_firewall
();
2874 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
2875 get_ipset_cmdlist
($ipset_ruleset, undef, $verbose);
2877 my ($cmdlist, $changes) = get_ruleset_cmdlist
($ruleset, $verbose);
2880 if ($ipset_changes) {
2881 print "ipset changes:\n";
2882 print $ipset_create_cmdlist if $ipset_create_cmdlist;
2883 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
2887 print "iptables changes:\n";
2892 ipset_restore_cmdlist
($ipset_create_cmdlist);
2894 iptables_restore_cmdlist
($cmdlist);
2896 ipset_restore_cmdlist
($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
2898 # test: re-read status and check if everything is up to date
2899 my $active_chains = iptables_get_chains
();
2900 my $statushash = get_ruleset_status
($ruleset, $active_chains, \
&iptables_chain_digest
, 0);
2903 foreach my $chain (sort keys %$ruleset) {
2904 my $stat = $statushash->{$chain};
2905 if ($stat->{action
} ne 'exists') {
2906 warn "unable to update chain '$chain'\n";
2911 die "unable to apply firewall changes\n" if $errors;
2913 update_nf_conntrack_max
($hostfw_conf);
2915 update_nf_conntrack_tcp_timeout_established
($hostfw_conf);
2919 sub update_nf_conntrack_max
{
2920 my ($hostfw_conf) = @_;
2922 my $max = 65536; # reasonable default
2924 my $options = $hostfw_conf->{options
} || {};
2926 if (defined($options->{nf_conntrack_max
}) && ($options->{nf_conntrack_max
} > $max)) {
2927 $max = $options->{nf_conntrack_max
};
2928 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
2931 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
2932 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
2934 my $current = int(PVE
::Tools
::file_read_firstline
($filename_nf_conntrack_max) || $max);
2936 if ($current != $max) {
2937 my $hashsize = int($max/4);
2938 PVE
::ProcFSTools
::write_proc_entry
($filename_hashsize, $hashsize);
2939 PVE
::ProcFSTools
::write_proc_entry
($filename_nf_conntrack_max, $max);
2943 sub update_nf_conntrack_tcp_timeout_established
{
2944 my ($hostfw_conf) = @_;
2946 my $options = $hostfw_conf->{options
} || {};
2948 my $value = defined($options->{nf_conntrack_tcp_timeout_established
}) ?
$options->{nf_conntrack_tcp_timeout_established
} : 432000;
2950 PVE
::ProcFSTools
::write_proc_entry
("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
2953 sub remove_pvefw_chains
{
2955 my ($chash, $hooks) = iptables_get_chains
();
2956 my $cmdlist = "*filter\n";
2958 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
2960 $cmdlist .= "-D $h -j PVEFW-$h\n";
2964 foreach my $chain (keys %$chash) {
2965 $cmdlist .= "-F $chain\n";
2968 foreach my $chain (keys %$chash) {
2969 $cmdlist .= "-X $chain\n";
2971 $cmdlist .= "COMMIT\n";
2973 iptables_restore_cmdlist
($cmdlist);
2977 my ($start, $verbose) = @_;
2981 my $cluster_conf = load_clusterfw_conf
();
2982 my $cluster_options = $cluster_conf->{options
};
2984 my $enable = $cluster_options->{enable
};
2986 my $status = read_pvefw_status
();
2988 die "Firewall is disabled - cannot start\n" if !$enable && $start;
2991 PVE
::Firewall
::remove_pvefw_chains
();
2992 print "Firewall disabled\n" if $verbose;
2996 my $hostfw_conf = load_hostfw_conf
();
2998 my ($ruleset, $ipset_ruleset) = compile
($cluster_conf, $hostfw_conf);
3000 if ($start || $status eq 'active') {
3002 save_pvefw_status
('active') if ($status ne 'active');
3004 apply_ruleset
($ruleset, $hostfw_conf, $ipset_ruleset, $verbose);
3006 print "Firewall not active (status = $status)\n" if $verbose;