implement 'enable' option
[pve-firewall.git] / PVE / Firewall.pm
1 package PVE::Firewall;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use Digest::SHA;
7 use PVE::Tools;
8 use PVE::QemuServer;
9 use File::Path;
10 use IO::File;
11 use Net::IP;
12 use PVE::Tools qw(run_command lock_file);
13
14 use Data::Dumper;
15
16 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
17
18 # todo: define more MACROS
19 # imported/converted from: /usr/share/shorewall/macro.*
20 my $pve_fw_macros = {
21 'Amanda' => [
22 { action => 'PARAM', proto => 'udp', dport => '10080' },
23 { action => 'PARAM', proto => 'tcp', dport => '10080' },
24 ],
25 'Auth' => [
26 { action => 'PARAM', proto => 'tcp', dport => '113' },
27 ],
28 'BGP' => [
29 { action => 'PARAM', proto => 'tcp', dport => '179' },
30 ],
31 'BitTorrent' => [
32 { action => 'PARAM', proto => 'tcp', dport => '6881:6889' },
33 { action => 'PARAM', proto => 'udp', dport => '6881' },
34 ],
35 'BitTorrent32' => [
36 { action => 'PARAM', proto => 'tcp', dport => '6881:6999' },
37 { action => 'PARAM', proto => 'udp', dport => '6881' },
38 ],
39 'CVS' => [
40 { action => 'PARAM', proto => 'tcp', dport => '2401' },
41 ],
42 'Citrix' => [
43 { action => 'PARAM', proto => 'tcp', dport => '1494' },
44 { action => 'PARAM', proto => 'udp', dport => '1604' },
45 { action => 'PARAM', proto => 'tcp', dport => '2598' },
46 ],
47 'DAAP' => [
48 { action => 'PARAM', proto => 'tcp', dport => '3689' },
49 { action => 'PARAM', proto => 'udp', dport => '3689' },
50 ],
51 'DCC' => [
52 { action => 'PARAM', proto => 'tcp', dport => '6277' },
53 ],
54 'DHCPfwd' => [
55 { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' },
56 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' },
57 ],
58 'DNS' => [
59 { action => 'PARAM', proto => 'udp', dport => '53' },
60 { action => 'PARAM', proto => 'tcp', dport => '53' },
61 ],
62 'Distcc' => [
63 { action => 'PARAM', proto => 'tcp', dport => '3632' },
64 ],
65 'Edonkey' => [
66 { action => 'PARAM', proto => 'tcp', dport => '4662' },
67 { action => 'PARAM', proto => 'udp', dport => '4665' },
68 ],
69 'FTP' => [
70 { action => 'PARAM', proto => 'tcp', dport => '21' },
71 ],
72 'Finger' => [
73 { action => 'PARAM', proto => 'tcp', dport => '79' },
74 ],
75 'GNUnet' => [
76 { action => 'PARAM', proto => 'tcp', dport => '2086' },
77 { action => 'PARAM', proto => 'udp', dport => '2086' },
78 { action => 'PARAM', proto => 'tcp', dport => '1080' },
79 { action => 'PARAM', proto => 'udp', dport => '1080' },
80 ],
81 'GRE' => [
82 { action => 'PARAM', proto => '47' },
83 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
84 ],
85 'Git' => [
86 { action => 'PARAM', proto => 'tcp', dport => '9418' },
87 ],
88 'Gnutella' => [
89 { action => 'PARAM', proto => 'tcp', dport => '6346' },
90 { action => 'PARAM', proto => 'udp', dport => '6346' },
91 ],
92 'HKP' => [
93 { action => 'PARAM', proto => 'tcp', dport => '11371' },
94 ],
95 'HTTP' => [
96 { action => 'PARAM', proto => 'tcp', dport => '80' },
97 ],
98 'HTTPS' => [
99 { action => 'PARAM', proto => 'tcp', dport => '443' },
100 ],
101 'ICPV2' => [
102 { action => 'PARAM', proto => 'udp', dport => '3130' },
103 ],
104 'ICQ' => [
105 { action => 'PARAM', proto => 'tcp', dport => '5190' },
106 ],
107 'IMAP' => [
108 { action => 'PARAM', proto => 'tcp', dport => '143' },
109 ],
110 'IMAPS' => [
111 { action => 'PARAM', proto => 'tcp', dport => '993' },
112 ],
113 'IPIP' => [
114 { action => 'PARAM', proto => '94' },
115 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' },
116 ],
117 'IPP' => [
118 { action => 'PARAM', proto => 'tcp', dport => '631' },
119 ],
120 'IPPbrd' => [
121 { action => 'PARAM', proto => 'udp', dport => '631' },
122 ],
123 'IPPserver' => [
124 { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' },
125 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' },
126 ],
127 'IPsec' => [
128 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
129 { action => 'PARAM', proto => '50' },
130 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
131 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
132 ],
133 'IPsecah' => [
134 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
135 { action => 'PARAM', proto => '51' },
136 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
137 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' },
138 ],
139 'IPsecnat' => [
140 { action => 'PARAM', proto => 'udp', dport => '500' },
141 { action => 'PARAM', proto => 'udp', dport => '4500' },
142 { action => 'PARAM', proto => '50' },
143 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' },
144 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' },
145 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
146 ],
147 'IRC' => [
148 { action => 'PARAM', proto => 'tcp', dport => '6667' },
149 ],
150 'JabberPlain' => [
151 { action => 'PARAM', proto => 'tcp', dport => '5222' },
152 ],
153 'JabberSecure' => [
154 { action => 'PARAM', proto => 'tcp', dport => '5223' },
155 ],
156 'Jabberd' => [
157 { action => 'PARAM', proto => 'tcp', dport => '5269' },
158 ],
159 'Jetdirect' => [
160 { action => 'PARAM', proto => 'tcp', dport => '9100' },
161 ],
162 'L2TP' => [
163 { action => 'PARAM', proto => 'udp', dport => '1701' },
164 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' },
165 ],
166 'LDAP' => [
167 { action => 'PARAM', proto => 'tcp', dport => '389' },
168 ],
169 'LDAPS' => [
170 { action => 'PARAM', proto => 'tcp', dport => '636' },
171 ],
172 'MSNP' => [
173 { action => 'PARAM', proto => 'tcp', dport => '1863' },
174 ],
175 'MSSQL' => [
176 { action => 'PARAM', proto => 'tcp', dport => '1433' },
177 ],
178 'Mail' => [
179 { action => 'PARAM', proto => 'tcp', dport => '25' },
180 { action => 'PARAM', proto => 'tcp', dport => '465' },
181 { action => 'PARAM', proto => 'tcp', dport => '587' },
182 ],
183 'Munin' => [
184 { action => 'PARAM', proto => 'tcp', dport => '4949' },
185 ],
186 'MySQL' => [
187 { action => 'PARAM', proto => 'tcp', dport => '3306' },
188 ],
189 'NNTP' => [
190 { action => 'PARAM', proto => 'tcp', dport => '119' },
191 ],
192 'NNTPS' => [
193 { action => 'PARAM', proto => 'tcp', dport => '563' },
194 ],
195 'NTP' => [
196 { action => 'PARAM', proto => 'udp', dport => '123' },
197 ],
198 'NTPbi' => [
199 { action => 'PARAM', proto => 'udp', dport => '123' },
200 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' },
201 ],
202 'NTPbrd' => [
203 { action => 'PARAM', proto => 'udp', dport => '123' },
204 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' },
205 ],
206 'OSPF' => [
207 { action => 'PARAM', proto => '89' },
208 ],
209 'OpenVPN' => [
210 { action => 'PARAM', proto => 'udp', dport => '1194' },
211 ],
212 'PCA' => [
213 { action => 'PARAM', proto => 'udp', dport => '5632' },
214 { action => 'PARAM', proto => 'tcp', dport => '5631' },
215 ],
216 'POP3' => [
217 { action => 'PARAM', proto => 'tcp', dport => '110' },
218 ],
219 'POP3S' => [
220 { action => 'PARAM', proto => 'tcp', dport => '995' },
221 ],
222 'PPtP' => [
223 { action => 'PARAM', proto => '47' },
224 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
225 { action => 'PARAM', proto => 'tcp', dport => '1723' },
226 ],
227 'Ping' => [
228 { action => 'PARAM', proto => 'icmp', dport => '8' },
229 ],
230 'PostgreSQL' => [
231 { action => 'PARAM', proto => 'tcp', dport => '5432' },
232 ],
233 'Printer' => [
234 { action => 'PARAM', proto => 'tcp', dport => '515' },
235 ],
236 'RDP' => [
237 { action => 'PARAM', proto => 'tcp', dport => '3389' },
238 ],
239 'RIPbi' => [
240 { action => 'PARAM', proto => 'udp', dport => '520' },
241 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' },
242 ],
243 'RNDC' => [
244 { action => 'PARAM', proto => 'tcp', dport => '953' },
245 ],
246 'Razor' => [
247 { action => 'ACCEPT', proto => 'tcp', dport => '2703' },
248 ],
249 'Rdate' => [
250 { action => 'PARAM', proto => 'tcp', dport => '37' },
251 ],
252 'Rsync' => [
253 { action => 'PARAM', proto => 'tcp', dport => '873' },
254 ],
255 'SANE' => [
256 { action => 'PARAM', proto => 'tcp', dport => '6566' },
257 ],
258 'SMB' => [
259 { action => 'PARAM', proto => 'udp', dport => '135,445' },
260 { action => 'PARAM', proto => 'udp', dport => '137:139' },
261 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
262 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
263 ],
264 'SMBBI' => [
265 { action => 'PARAM', proto => 'udp', dport => '135,445' },
266 { action => 'PARAM', proto => 'udp', dport => '137:139' },
267 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
268 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
269 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' },
270 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' },
271 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' },
272 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' },
273 ],
274 'SMBswat' => [
275 { action => 'PARAM', proto => 'tcp', dport => '901' },
276 ],
277 'SMTP' => [
278 { action => 'PARAM', proto => 'tcp', dport => '25' },
279 ],
280 'SMTPS' => [
281 { action => 'PARAM', proto => 'tcp', dport => '465' },
282 ],
283 'SNMP' => [
284 { action => 'PARAM', proto => 'udp', dport => '161:162' },
285 { action => 'PARAM', proto => 'tcp', dport => '161' },
286 ],
287 'SPAMD' => [
288 { action => 'PARAM', proto => 'tcp', dport => '783' },
289 ],
290 'SSH' => [
291 { action => 'PARAM', proto => 'tcp', dport => '22' },
292 ],
293 'SVN' => [
294 { action => 'PARAM', proto => 'tcp', dport => '3690' },
295 ],
296 'SixXS' => [
297 { action => 'PARAM', proto => 'tcp', dport => '3874' },
298 { action => 'PARAM', proto => 'udp', dport => '3740' },
299 { action => 'PARAM', proto => '41' },
300 { action => 'PARAM', proto => 'udp', dport => '5072,8374' },
301 ],
302 'Squid' => [
303 { action => 'PARAM', proto => 'tcp', dport => '3128' },
304 ],
305 'Submission' => [
306 { action => 'PARAM', proto => 'tcp', dport => '587' },
307 ],
308 'Syslog' => [
309 { action => 'PARAM', proto => 'udp', dport => '514' },
310 { action => 'PARAM', proto => 'tcp', dport => '514' },
311 ],
312 'TFTP' => [
313 { action => 'PARAM', proto => 'udp', dport => '69' },
314 ],
315 'Telnet' => [
316 { action => 'PARAM', proto => 'tcp', dport => '23' },
317 ],
318 'Telnets' => [
319 { action => 'PARAM', proto => 'tcp', dport => '992' },
320 ],
321 'Time' => [
322 { action => 'PARAM', proto => 'tcp', dport => '37' },
323 ],
324 'Trcrt' => [
325 { action => 'PARAM', proto => 'udp', dport => '33434:33524' },
326 { action => 'PARAM', proto => 'icmp', dport => '8' },
327 ],
328 'VNC' => [
329 { action => 'PARAM', proto => 'tcp', dport => '5900:5909' },
330 ],
331 'VNCL' => [
332 { action => 'PARAM', proto => 'tcp', dport => '5500' },
333 ],
334 'Web' => [
335 { action => 'PARAM', proto => 'tcp', dport => '80' },
336 { action => 'PARAM', proto => 'tcp', dport => '443' },
337 ],
338 'Webcache' => [
339 { action => 'PARAM', proto => 'tcp', dport => '8080' },
340 ],
341 'Webmin' => [
342 { action => 'PARAM', proto => 'tcp', dport => '10000' },
343 ],
344 'Whois' => [
345 { action => 'PARAM', proto => 'tcp', dport => '43' },
346 ],
347 };
348
349 my $pve_fw_parsed_macros;
350 my $pve_fw_preferred_macro_names = {};
351
352 sub get_firewall_macros {
353
354 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
355
356 $pve_fw_parsed_macros = {};
357
358 foreach my $k (keys %$pve_fw_macros) {
359 my $name = lc($k);
360
361 my $macro = $pve_fw_macros->{$k};
362 $pve_fw_preferred_macro_names->{$name} = $k;
363 $pve_fw_parsed_macros->{$name} = $macro;
364 }
365
366 return $pve_fw_parsed_macros;
367 }
368
369 my $etc_services;
370
371 sub get_etc_services {
372
373 return $etc_services if $etc_services;
374
375 my $filename = "/etc/services";
376
377 my $fh = IO::File->new($filename, O_RDONLY);
378 if (!$fh) {
379 warn "unable to read '$filename' - $!\n";
380 return {};
381 }
382
383 my $services = {};
384
385 while (my $line = <$fh>) {
386 chomp ($line);
387 next if $line =~m/^#/;
388 next if ($line =~m/^\s*$/);
389
390 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
391 $services->{byid}->{$2}->{name} = $1;
392 $services->{byid}->{$2}->{port} = $2;
393 $services->{byid}->{$2}->{$3} = 1;
394 $services->{byname}->{$1} = $services->{byid}->{$2};
395 }
396 }
397
398 close($fh);
399
400 $etc_services = $services;
401
402
403 return $etc_services;
404 }
405
406 my $etc_protocols;
407
408 sub get_etc_protocols {
409 return $etc_protocols if $etc_protocols;
410
411 my $filename = "/etc/protocols";
412
413 my $fh = IO::File->new($filename, O_RDONLY);
414 if (!$fh) {
415 warn "unable to read '$filename' - $!\n";
416 return {};
417 }
418
419 my $protocols = {};
420
421 while (my $line = <$fh>) {
422 chomp ($line);
423 next if $line =~m/^#/;
424 next if ($line =~m/^\s*$/);
425
426 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
427 $protocols->{byid}->{$2}->{name} = $1;
428 $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
429 }
430 }
431
432 close($fh);
433
434 $etc_protocols = $protocols;
435
436 return $etc_protocols;
437 }
438
439 sub parse_address_list {
440 my ($str) = @_;
441
442 my $nbaor = 0;
443 foreach my $aor (split(/,/, $str)) {
444 if (!Net::IP->new($aor)) {
445 my $err = Net::IP::Error();
446 die "invalid IP address: $err\n";
447 }else{
448 $nbaor++;
449 }
450 }
451 return $nbaor;
452 }
453
454 sub parse_port_name_number_or_range {
455 my ($str) = @_;
456
457 my $services = PVE::Firewall::get_etc_services();
458 my $nbports = 0;
459 foreach my $item (split(/,/, $str)) {
460 my $portlist = "";
461 my $oldpon = undef;
462 foreach my $pon (split(':', $item, 2)) {
463 $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port};
464 if ($pon =~ m/^\d+$/){
465 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
466 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
467 $oldpon = $pon;
468 }else{
469 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon};
470 }
471 $nbports++;
472 }
473 }
474
475 return ($nbports);
476 }
477
478 my $bridge_firewall_enabled = 0;
479
480 sub enable_bridge_firewall {
481
482 return if $bridge_firewall_enabled; # only once
483
484 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables");
485 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables");
486
487 $bridge_firewall_enabled = 1;
488 }
489
490 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
491
492 sub iptables {
493 my ($cmd) = @_;
494
495 run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
496 }
497
498 sub iptables_restore_cmdlist {
499 my ($cmdlist) = @_;
500
501 run_command("/sbin/iptables-restore -n", input => $cmdlist);
502 }
503
504 sub iptables_get_chains {
505
506 my $res = {};
507
508 # check what chains we want to track
509 my $is_pvefw_chain = sub {
510 my $name = shift;
511
512 return 1 if $name =~ m/^PVEFW-\S+$/;
513
514 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
515 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
516 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
517
518 return undef;
519 };
520
521 my $table = '';
522
523 my $parser = sub {
524 my $line = shift;
525
526 return if $line =~ m/^#/;
527 return if $line =~ m/^\s*$/;
528
529 if ($line =~ m/^\*(\S+)$/) {
530 $table = $1;
531 return;
532 }
533
534 return if $table ne 'filter';
535
536 if ($line =~ m/^:(\S+)\s/) {
537 my $chain = $1;
538 return if !&$is_pvefw_chain($chain);
539 $res->{$chain} = "unknown";
540 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
541 my ($chain, $sig) = ($1, $2);
542 return if !&$is_pvefw_chain($chain);
543 $res->{$chain} = $sig;
544 } else {
545 # simply ignore the rest
546 return;
547 }
548 };
549
550 run_command("/sbin/iptables-save", outfunc => $parser);
551
552 return $res;
553 }
554
555 sub iptables_chain_exist {
556 my ($chain) = @_;
557
558 eval{
559 iptables("-n --list $chain");
560 };
561 return undef if $@;
562
563 return 1;
564 }
565
566 sub iptables_rule_exist {
567 my ($rule) = @_;
568
569 eval{
570 iptables("-C $rule");
571 };
572 return undef if $@;
573
574 return 1;
575 }
576
577 sub ruleset_generate_rule {
578 my ($ruleset, $chain, $rule, $goto) = @_;
579
580 my $cmd = '';
581
582 $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
583 $cmd .= " -s $rule->{source}" if $rule->{source};
584 $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
585 $cmd .= " -d $rule->{dest}" if $rule->{dest};
586 $cmd .= " -p $rule->{proto}" if $rule->{proto};
587 $cmd .= " --match multiport" if $rule->{nbdport} && $rule->{nbdport} > 1;
588 $cmd .= " --dport $rule->{dport}" if $rule->{dport};
589 $cmd .= " --match multiport" if $rule->{nbsport} && $rule->{nbsport} > 1;
590 $cmd .= " --sport $rule->{sport}" if $rule->{sport};
591
592 if (my $action = $rule->{action}) {
593 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
594 $cmd .= $goto ? " -g $action" : " -j $action";
595 };
596
597 ruleset_addrule($ruleset, $chain, $cmd) if $cmd;
598 }
599
600 sub ruleset_create_chain {
601 my ($ruleset, $chain) = @_;
602
603 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
604
605 die "chain '$chain' already exists\n" if $ruleset->{$chain};
606
607 $ruleset->{$chain} = [];
608 }
609
610 sub ruleset_chain_exist {
611 my ($ruleset, $chain) = @_;
612
613 return $ruleset->{$chain} ? 1 : undef;
614 }
615
616 sub ruleset_addrule {
617 my ($ruleset, $chain, $rule) = @_;
618
619 die "no such chain '$chain'\n" if !$ruleset->{$chain};
620
621 push @{$ruleset->{$chain}}, "-A $chain $rule";
622 }
623
624 sub ruleset_insertrule {
625 my ($ruleset, $chain, $rule) = @_;
626
627 die "no such chain '$chain'\n" if !$ruleset->{$chain};
628
629 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
630 }
631
632 sub generate_bridge_chains {
633 my ($ruleset, $bridge) = @_;
634
635 if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){
636 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
637 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
638 }
639
640 if (!ruleset_chain_exist($ruleset, "$bridge-FW")) {
641 ruleset_create_chain($ruleset, "$bridge-FW");
642 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
643 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
644 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing
645 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing
646 }
647
648 if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) {
649 ruleset_create_chain($ruleset, "$bridge-OUT");
650 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
651 }
652
653 if (!ruleset_chain_exist($ruleset, "$bridge-IN")) {
654 ruleset_create_chain($ruleset, "$bridge-IN");
655 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
656 }
657 }
658
659 sub generate_tap_rules_direction {
660 my ($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
661
662 my $rules = $vmfw_conf->{lc($direction)};
663 my $options = $vmfw_conf->{options};
664
665 my $tapchain = "$iface-$direction";
666
667 ruleset_create_chain($ruleset, $tapchain);
668
669 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
670 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
671
672 if ($direction eq 'OUT' && defined($macaddr)) {
673 ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
674 }
675
676 if ($rules) {
677 foreach my $rule (@$rules) {
678 next if $rule->{iface} && $rule->{iface} ne $netid;
679 # we go to $bridge-IN if accept in out rules
680 if($rule->{action} =~ m/^(GROUP-(\S+))$/){
681 $rule->{action} .= "-$direction";
682 # generate empty group rule if don't exist
683 if(!ruleset_chain_exist($ruleset, $rule->{action})){
684 generate_group_rules($ruleset, $group_rules, $2);
685 }
686 ruleset_generate_rule($ruleset, $tapchain, $rule);
687 ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -g $bridge-IN")
688 if $direction eq 'OUT';
689 } else {
690 $rule->{action} = "$bridge-IN" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT';
691 ruleset_generate_rule($ruleset, $tapchain, $rule);
692 }
693 }
694 }
695
696 ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
697 ruleset_addrule($ruleset, $tapchain, "-j DROP");
698
699 # plug the tap chain to bridge chain
700 my $physdevdirection = $direction eq 'IN' ? "out" : "in";
701 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
702 ruleset_insertrule($ruleset, "$bridge-$direction", $rule);
703
704 if ($direction eq 'OUT'){
705 # add tap->host rules
706 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
707 ruleset_addrule($ruleset, "PVEFW-INPUT", $rule);
708 }
709 }
710
711 sub enablehostfw {
712 my ($ruleset, $rules, $group_rules) = @_;
713
714 # fixme: allow security groups
715
716 # host inbound firewall
717 my $chain = "PVEFW-HOST-IN";
718 ruleset_create_chain($ruleset, $chain);
719
720 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
721 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
722 ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT");
723 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
724 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
725 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
726
727 if ($rules->{in}) {
728 foreach my $rule (@{$rules->{in}}) {
729 # we use RETURN because we need to check also tap rules
730 $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
731 ruleset_generate_rule($ruleset, $chain, $rule);
732 }
733 }
734
735 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
736 ruleset_addrule($ruleset, $chain, "-j DROP");
737
738 # host outbound firewall
739 $chain = "PVEFW-HOST-OUT";
740 ruleset_create_chain($ruleset, $chain);
741
742 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
743 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
744 ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT");
745 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
746 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
747 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
748
749 if ($rules->{out}) {
750 foreach my $rule (@{$rules->{out}}) {
751 # we use RETURN because we need to check also tap rules
752 $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
753 ruleset_generate_rule($ruleset, $chain, $rule);
754 }
755 }
756
757 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
758 ruleset_addrule($ruleset, $chain, "-j DROP");
759
760 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
761 ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
762 }
763
764 sub generate_group_rules {
765 my ($ruleset, $group_rules, $group) = @_;
766
767 my $rules = $group_rules->{$group};
768
769 die "no such security group '$group'\n" if !$rules;
770
771 my $chain = "GROUP-${group}-IN";
772
773 ruleset_create_chain($ruleset, $chain);
774
775 if ($rules->{in}) {
776 foreach my $rule (@{$rules->{in}}) {
777 ruleset_generate_rule($ruleset, $chain, $rule);
778 }
779 }
780
781 $chain = "GROUP-${group}-OUT";
782
783 ruleset_create_chain($ruleset, $chain);
784 ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
785
786 if ($rules->{out}) {
787 foreach my $rule (@{$rules->{out}}) {
788 # we go the PVEFW-SET-ACCEPT-MARK Instead of ACCEPT) because we need to
789 # check also other tap rules (and group rules can be set on any bridge,
790 # so we can't go to VMBRXX-IN)
791 $rule->{action} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action} eq 'ACCEPT';
792 ruleset_generate_rule($ruleset, $chain, $rule);
793 }
794 }
795 }
796
797 my $MAX_NETS = 32;
798 my $valid_netdev_names = {};
799 for (my $i = 0; $i < $MAX_NETS; $i++) {
800 $valid_netdev_names->{"net$i"} = 1;
801 }
802
803 sub parse_fw_rule {
804 my ($line, $need_iface, $allow_groups) = @_;
805
806 my $macros = get_firewall_macros();
807 my $protocols = get_etc_protocols();
808
809 my ($action, $iface, $source, $dest, $proto, $dport, $sport);
810
811 $line =~ s/#.*$//;
812
813 my @data = split(/\s+/, $line);
814 my $expected_elements = $need_iface ? 7 : 6;
815
816 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
817
818 if ($need_iface) {
819 ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data
820 } else {
821 ($action, $source, $dest, $proto, $dport, $sport) = @data;
822 }
823
824 die "incomplete rule\n" if !$action;
825
826 my $macro;
827 my $macro_name;
828
829 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
830 # OK
831 } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) {
832 # OK
833 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
834 ($macro_name, $action) = ($1, $2);
835 my $lc_macro_name = lc($macro_name);
836 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
837 $macro_name = $preferred_name if $preferred_name;
838 $macro = $macros->{$lc_macro_name};
839 die "unknown macro '$macro_name'\n" if !$macro;
840 } else {
841 die "unknown action '$action'\n";
842 }
843
844 if ($need_iface) {
845 $iface = undef if $iface && $iface eq '-';
846 die "unknown interface '$iface'\n"
847 if defined($iface) && !$valid_netdev_names->{$iface};
848 }
849
850 $proto = undef if $proto && $proto eq '-';
851 die "unknown protokol '$proto'\n" if $proto &&
852 !(defined($protocols->{byname}->{$proto}) ||
853 defined($protocols->{byid}->{$proto}));
854
855 $source = undef if $source && $source eq '-';
856 $dest = undef if $dest && $dest eq '-';
857
858 $dport = undef if $dport && $dport eq '-';
859 $sport = undef if $sport && $sport eq '-';
860
861 my $nbsource = undef;
862 my $nbdest = undef;
863
864 $nbsource = parse_address_list($source) if $source;
865 $nbdest = parse_address_list($dest) if $dest;
866
867 my $rules = [];
868
869 my $param = {
870 action => $action,
871 iface => $iface,
872 source => $source,
873 dest => $dest,
874 nbsource => $nbsource,
875 nbdest => $nbdest,
876 proto => $proto,
877 dport => $dport,
878 sport => $sport,
879 };
880
881 if ($macro) {
882 foreach my $templ (@$macro) {
883 my $rule = {};
884 foreach my $k (keys %$templ) {
885 my $v = $templ->{$k};
886 if ($v eq 'PARAM') {
887 $v = $param->{$k};
888 } elsif ($v eq 'DEST') {
889 $v = $param->{dest};
890 } elsif ($v eq 'SOURCE') {
891 $v = $param->{source};
892 }
893
894 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
895 $rule->{$k} = $v;
896 }
897 push @$rules, $rule;
898 }
899 } else {
900 push @$rules, $param;
901 }
902
903 foreach my $rule (@$rules) {
904 $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport})
905 if defined($rule->{dport});
906 $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport})
907 if defined($rule->{sport});
908 }
909
910 return $rules;
911 }
912
913 sub parse_fw_option {
914 my ($line) = @_;
915
916 my ($opt, $value);
917
918 if ($line =~ m/^enable:\s*(0|1)\s*$/i) {
919 $opt = 'enable';
920 $value = int($1);
921 } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
922 $opt = lc($1);
923 $value = uc($3);
924 } else {
925 chomp $line;
926 die "can't parse option '$line'\n"
927 }
928
929 return ($opt, $value);
930 }
931
932 sub parse_vm_fw_rules {
933 my ($filename, $fh) = @_;
934
935 my $res = { in => [], out => [], options => {}};
936
937 my $section;
938
939 while (defined(my $line = <$fh>)) {
940 next if $line =~ m/^#/;
941 next if $line =~ m/^\s*$/;
942
943 my $linenr = $fh->input_line_number();
944 my $prefix = "$filename (line $linenr)";
945
946 if ($line =~ m/^\[(\S+)\]\s*$/i) {
947 $section = lc($1);
948 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
949 next;
950 }
951 if (!$section) {
952 warn "$prefix: skip line - no section";
953 next;
954 }
955
956 next if !$res->{$section}; # skip undefined section
957
958 if ($section eq 'options') {
959 eval {
960 my ($opt, $value) = parse_fw_option($line);
961 $res->{options}->{$opt} = $value;
962 };
963 warn "$prefix: $@" if $@;
964 next;
965 }
966
967 my $rules;
968 eval { $rules = parse_fw_rule($line, 1, 1); };
969 if (my $err = $@) {
970 warn "$prefix: $err";
971 next;
972 }
973
974 push @{$res->{$section}}, @$rules;
975 }
976
977 return $res;
978 }
979
980 sub parse_host_fw_rules {
981 my ($filename, $fh) = @_;
982
983 my $res = { in => [], out => [] };
984
985 my $section;
986
987 while (defined(my $line = <$fh>)) {
988 next if $line =~ m/^#/;
989 next if $line =~ m/^\s*$/;
990
991 my $linenr = $fh->input_line_number();
992 my $prefix = "$filename (line $linenr)";
993
994 if ($line =~ m/^\[(in|out)\]\s*$/i) {
995 $section = lc($1);
996 next;
997 }
998 if (!$section) {
999 warn "$prefix: skip line - no section";
1000 next;
1001 }
1002
1003 my $rules;
1004 eval { $rules = parse_fw_rule($line, 1, 1); };
1005 if (my $err = $@) {
1006 warn "$prefix: $err";
1007 next;
1008 }
1009
1010 push @{$res->{$section}}, @$rules;
1011 }
1012
1013 return $res;
1014 }
1015
1016 sub parse_group_fw_rules {
1017 my ($filename, $fh) = @_;
1018
1019 my $section;
1020 my $group;
1021
1022 my $res = { in => [], out => [] };
1023
1024 while (defined(my $line = <$fh>)) {
1025 next if $line =~ m/^#/;
1026 next if $line =~ m/^\s*$/;
1027
1028 my $linenr = $fh->input_line_number();
1029 my $prefix = "$filename (line $linenr)";
1030
1031 if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) {
1032 $section = lc($1);
1033 $group = lc($2);
1034 next;
1035 }
1036 if (!$section || !$group) {
1037 warn "$prefix: skip line - no section";
1038 next;
1039 }
1040
1041 my $rules;
1042 eval { $rules = parse_fw_rule($line, 0, 0); };
1043 if (my $err = $@) {
1044 warn "$prefix: $err";
1045 next;
1046 }
1047
1048 push @{$res->{$group}->{$section}}, @$rules;
1049 }
1050
1051 return $res;
1052 }
1053
1054 sub run_locked {
1055 my ($code, @param) = @_;
1056
1057 my $timeout = 10;
1058
1059 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1060
1061 die $@ if $@;
1062
1063 return $res;
1064 }
1065
1066 sub read_local_vm_config {
1067
1068 my $openvz = {};
1069
1070 my $qemu = {};
1071
1072 my $list = PVE::QemuServer::config_list();
1073
1074 foreach my $vmid (keys %$list) {
1075 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1076 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1077 $qemu->{$vmid} = $conf;
1078 }
1079 }
1080
1081 my $vmdata = { openvz => $openvz, qemu => $qemu };
1082
1083 return $vmdata;
1084 };
1085
1086 sub read_vm_firewall_rules {
1087 my ($vmdata) = @_;
1088 my $rules = {};
1089 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1090 my $filename = "/etc/pve/firewall/$vmid.fw";
1091 my $fh = IO::File->new($filename, O_RDONLY);
1092 next if !$fh;
1093
1094 $rules->{$vmid} = parse_vm_fw_rules($filename, $fh);
1095 }
1096
1097 return $rules;
1098 }
1099
1100 sub compile {
1101 my $vmdata = read_local_vm_config();
1102 my $rules = read_vm_firewall_rules($vmdata);
1103
1104 my $group_rules = {};
1105 my $filename = "/etc/pve/firewall/groups.fw";
1106 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1107 $group_rules = parse_group_fw_rules($filename, $fh);
1108 }
1109
1110 #print Dumper($rules);
1111
1112 my $ruleset = {};
1113
1114 ruleset_create_chain($ruleset, "PVEFW-INPUT");
1115 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
1116 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
1117
1118 ruleset_create_chain($ruleset, "PVEFW-SET-ACCEPT-MARK");
1119 ruleset_addrule($ruleset, "PVEFW-SET-ACCEPT-MARK", "-j MARK --set-mark 1");
1120
1121 my $enable_hostfw = 0;
1122 $filename = "/etc/pve/local/host.fw";
1123 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1124 my $host_rules = parse_host_fw_rules($filename, $fh);
1125
1126 $enable_hostfw = 1;
1127
1128 enablehostfw($ruleset, $host_rules, $group_rules);
1129 }
1130
1131 # generate firewall rules for QEMU VMs
1132 foreach my $vmid (keys %{$vmdata->{qemu}}) {
1133 my $conf = $vmdata->{qemu}->{$vmid};
1134 my $vmfw_conf = $rules->{$vmid};
1135 next if !$vmfw_conf;
1136 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
1137
1138 foreach my $netid (keys %$conf) {
1139 next if $netid !~ m/^net(\d+)$/;
1140 my $net = PVE::QemuServer::parse_net($conf->{$netid});
1141 next if !$net;
1142 my $iface = "tap${vmid}i$1";
1143
1144 my $bridge = $net->{bridge};
1145 next if !$bridge; # fixme: ?
1146
1147 $bridge .= "v$net->{tag}" if $net->{tag};
1148
1149
1150 generate_bridge_chains($ruleset, $bridge);
1151
1152 my $macaddr = $net->{macaddr};
1153 generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1154 generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1155 }
1156 }
1157
1158 if ($enable_hostfw) {
1159 # allow traffic from lo (ourself)
1160 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT");
1161 }
1162
1163 return $ruleset;
1164 }
1165
1166 sub get_ruleset_status {
1167 my ($ruleset, $verbose) = @_;
1168
1169 my $active_chains = iptables_get_chains();
1170
1171 my $statushash = {};
1172
1173 foreach my $chain (sort keys %$ruleset) {
1174 my $digest = Digest::SHA->new('sha1');
1175 foreach my $cmd (@{$ruleset->{$chain}}) {
1176 $digest->add("$cmd\n");
1177 }
1178 my $sig = $digest->b64digest;
1179 $statushash->{$chain}->{sig} = $sig;
1180
1181 my $oldsig = $active_chains->{$chain};
1182 if (!defined($oldsig)) {
1183 $statushash->{$chain}->{action} = 'create';
1184 } else {
1185 if ($oldsig eq $sig) {
1186 $statushash->{$chain}->{action} = 'exists';
1187 } else {
1188 $statushash->{$chain}->{action} = 'update';
1189 }
1190 }
1191 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1192 foreach my $cmd (@{$ruleset->{$chain}}) {
1193 print "\t$cmd\n" if $verbose;
1194 }
1195 }
1196
1197 foreach my $chain (sort keys %$active_chains) {
1198 if (!defined($ruleset->{$chain})) {
1199 my $sig = $active_chains->{$chain};
1200 $statushash->{$chain}->{action} = 'delete';
1201 $statushash->{$chain}->{sig} = $sig;
1202 print "delete $chain ($sig)\n" if $verbose;
1203 }
1204 }
1205
1206 return $statushash;
1207 }
1208
1209 sub print_ruleset {
1210 my ($ruleset) = @_;
1211
1212 get_ruleset_status($ruleset, 1);
1213 }
1214
1215 sub print_sig_rule {
1216 my ($chain, $sig) = @_;
1217
1218 # We just use this to store a SHA1 checksum used to detect changes
1219 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1220 }
1221
1222 sub apply_ruleset {
1223 my ($ruleset, $verbose) = @_;
1224
1225 enable_bridge_firewall();
1226
1227 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
1228
1229 my $statushash = get_ruleset_status($ruleset, $verbose);
1230
1231 # create missing chains first
1232 foreach my $chain (sort keys %$ruleset) {
1233 my $stat = $statushash->{$chain};
1234 die "internal error" if !$stat;
1235 next if $stat->{action} ne 'create';
1236
1237 $cmdlist .= ":$chain - [0:0]\n";
1238 }
1239
1240 my $rule = "INPUT -j PVEFW-INPUT";
1241 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1242 $cmdlist .= "-A $rule\n";
1243 }
1244 $rule = "OUTPUT -j PVEFW-OUTPUT";
1245 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1246 $cmdlist .= "-A $rule\n";
1247 }
1248
1249 $rule = "FORWARD -j PVEFW-FORWARD";
1250 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1251 $cmdlist .= "-A $rule\n";
1252 }
1253
1254 foreach my $chain (sort keys %$ruleset) {
1255 my $stat = $statushash->{$chain};
1256 die "internal error" if !$stat;
1257
1258 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1259 $cmdlist .= "-F $chain\n";
1260 foreach my $cmd (@{$ruleset->{$chain}}) {
1261 $cmdlist .= "$cmd\n";
1262 }
1263 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1264 } elsif ($stat->{action} eq 'delete') {
1265 die "internal error"; # this should not happen
1266 } elsif ($stat->{action} eq 'exists') {
1267 # do nothing
1268 } else {
1269 die "internal error - unknown status '$stat->{action}'";
1270 }
1271 }
1272
1273 foreach my $chain (keys %$statushash) {
1274 next if $statushash->{$chain}->{action} ne 'delete';
1275 $cmdlist .= "-F $chain\n";
1276 }
1277 foreach my $chain (keys %$statushash) {
1278 next if $statushash->{$chain}->{action} ne 'delete';
1279 next if $chain eq 'PVEFW-INPUT';
1280 next if $chain eq 'PVEFW-OUTPUT';
1281 next if $chain eq 'PVEFW-FORWARD';
1282 $cmdlist .= "-X $chain\n";
1283 }
1284
1285 $cmdlist .= "COMMIT\n";
1286
1287 print $cmdlist if $verbose;
1288
1289 iptables_restore_cmdlist($cmdlist);
1290
1291 # test: re-read status and check if everything is up to date
1292 $statushash = get_ruleset_status($ruleset);
1293
1294 my $errors;
1295 foreach my $chain (sort keys %$ruleset) {
1296 my $stat = $statushash->{$chain};
1297 if ($stat->{action} ne 'exists') {
1298 warn "unable to update chain '$chain'\n";
1299 $errors = 1;
1300 }
1301 }
1302
1303 die "unable to apply firewall changes\n" if $errors;
1304 }
1305
1306 1;