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