check chain name length (max 28 chars)
[pve-firewall.git] / PVE / Firewall.pm
CommitLineData
b6360c3f
DM
1package PVE::Firewall;
2
3use warnings;
4use strict;
5use Data::Dumper;
09d5f68e 6use Digest::SHA;
f789653a 7use PVE::Tools;
b6360c3f 8use PVE::QemuServer;
5e1267a5
DM
9use File::Path;
10use IO::File;
ecbea048 11use Net::IP;
06320eb0 12use PVE::Tools qw(run_command lock_file);
ecbea048 13
5e1267a5 14use Data::Dumper;
b6360c3f 15
06320eb0
DM
16my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
17
9aab3127 18my $macros;
3a616aa0 19
a332200b
DM
20# todo: implement some kind of MACROS, like shorewall /usr/share/shorewall/macro.*
21sub get_firewall_macros {
9aab3127
DM
22
23 return $macros if $macros;
24
a332200b
DM
25 #foreach my $path (</usr/share/shorewall/macro.*>) {
26 # if ($path =~ m|/macro\.(\S+)$|) {
27 # $macros->{$1} = 1;
28 # }
29 #}
30
31 $macros = {}; # fixme: implemet me
32
9aab3127
DM
33 return $macros;
34}
35
fcba0beb
DM
36my $etc_services;
37
38sub get_etc_services {
39
40 return $etc_services if $etc_services;
41
42 my $filename = "/etc/services";
43
44 my $fh = IO::File->new($filename, O_RDONLY);
45 if (!$fh) {
46 warn "unable to read '$filename' - $!\n";
47 return {};
48 }
49
50 my $services = {};
51
52 while (my $line = <$fh>) {
53 chomp ($line);
54 next if $line =~m/^#/;
55 next if ($line =~m/^\s*$/);
56
57 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
58 $services->{byid}->{$2}->{name} = $1;
59 $services->{byid}->{$2}->{$3} = 1;
60 $services->{byname}->{$1} = $services->{byid}->{$2};
61 }
62 }
63
64 close($fh);
65
66 $etc_services = $services;
67
3a616aa0 68
fcba0beb
DM
69 return $etc_services;
70}
71
72my $etc_protocols;
73
74sub get_etc_protocols {
75 return $etc_protocols if $etc_protocols;
76
77 my $filename = "/etc/protocols";
78
79 my $fh = IO::File->new($filename, O_RDONLY);
80 if (!$fh) {
81 warn "unable to read '$filename' - $!\n";
82 return {};
83 }
84
85 my $protocols = {};
86
87 while (my $line = <$fh>) {
88 chomp ($line);
89 next if $line =~m/^#/;
90 next if ($line =~m/^\s*$/);
91
92 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
93 $protocols->{byid}->{$2}->{name} = $1;
94 $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
95 }
96 }
97
98 close($fh);
99
100 $etc_protocols = $protocols;
101
102 return $etc_protocols;
103}
104
ecbea048
DM
105sub parse_address_list {
106 my ($str) = @_;
107
d6de1dc2 108 my $nbaor = 0;
ecbea048
DM
109 foreach my $aor (split(/,/, $str)) {
110 if (!Net::IP->new($aor)) {
111 my $err = Net::IP::Error();
112 die "invalid IP address: $err\n";
d6de1dc2
AD
113 }else{
114 $nbaor++;
ecbea048
DM
115 }
116 }
d6de1dc2 117 return $nbaor;
ecbea048 118}
dddd9413 119
fcba0beb
DM
120sub parse_port_name_number_or_range {
121 my ($str) = @_;
122
123 my $services = PVE::Firewall::get_etc_services();
4cdbb3b7 124 my $nbports = 0;
fcba0beb 125 foreach my $item (split(/,/, $str)) {
4cdbb3b7 126 my $portlist = "";
fcba0beb 127 foreach my $pon (split(':', $item, 2)) {
4cdbb3b7
AD
128 if ($pon =~ m/^\d+$/){
129 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65536;
130 }else{
131 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon};
132 }
133 $nbports++;
fcba0beb
DM
134 }
135 }
136
4cdbb3b7 137 return ($nbports);
fcba0beb
DM
138}
139
780bcc0f
DM
140my $bridge_firewall_enabled = 0;
141
142sub enable_bridge_firewall {
143
144 return if $bridge_firewall_enabled; # only once
145
146 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables");
147 system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables");
148
149 $bridge_firewall_enabled = 1;
150}
151
8cebfa6f 152my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
dddd9413 153
3a616aa0
AD
154sub iptables {
155 my ($cmd) = @_;
156
157 run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
158}
159
b16e818e
DM
160sub iptables_restore_cmdlist {
161 my ($cmdlist) = @_;
3a616aa0 162
3fa83edf 163 run_command("/sbin/iptables-restore -n", input => $cmdlist);
3a616aa0
AD
164}
165
de2a57cd
DM
166sub iptables_get_chains {
167
168 my $res = {};
169
170 # check what chains we want to track
171 my $is_pvefw_chain = sub {
172 my $name = shift;
173
dec84fcd
DM
174 return 1 if $name =~ m/^PVEFW-\S+$/;
175
de2a57cd 176 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
b16e818e 177 return 1 if $name =~ m/^vmbr\d+-(:?IN|OUT)$/;
f50656c2 178 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
de2a57cd
DM
179
180 return undef;
181 };
182
183 my $table = '';
184
de2a57cd
DM
185 my $parser = sub {
186 my $line = shift;
187
188 return if $line =~ m/^#/;
189 return if $line =~ m/^\s*$/;
190
191 if ($line =~ m/^\*(\S+)$/) {
192 $table = $1;
193 return;
194 }
195
196 return if $table ne 'filter';
197
198 if ($line =~ m/^:(\S+)\s/) {
199 my $chain = $1;
200 return if !&$is_pvefw_chain($chain);
3fa83edf 201 $res->{$chain} = "unknown";
09d5f68e 202 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
3fa83edf 203 my ($chain, $sig) = ($1, $2);
de2a57cd 204 return if !&$is_pvefw_chain($chain);
3fa83edf 205 $res->{$chain} = $sig;
de2a57cd
DM
206 } else {
207 # simply ignore the rest
208 return;
209 }
210 };
211
212 run_command("/sbin/iptables-save", outfunc => $parser);
213
de2a57cd
DM
214 return $res;
215}
216
3a616aa0
AD
217sub iptables_chain_exist {
218 my ($chain) = @_;
219
220 eval{
221 iptables("-n --list $chain");
222 };
223 return undef if $@;
224
225 return 1;
226}
227
228sub iptables_rule_exist {
229 my ($rule) = @_;
230
231 eval{
232 iptables("-C $rule");
233 };
234 return undef if $@;
235
236 return 1;
237}
238
3fa83edf
DM
239sub ruleset_generate_rule {
240 my ($ruleset, $chain, $rule) = @_;
3a616aa0 241
3fa83edf 242 my $cmd = '';
3a616aa0 243
d6de1dc2 244 $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
3a616aa0 245 $cmd .= " -s $rule->{source}" if $rule->{source};
d6de1dc2 246 $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
3a616aa0
AD
247 $cmd .= " -d $rule->{dest}" if $rule->{destination};
248 $cmd .= " -p $rule->{proto}" if $rule->{proto};
4cdbb3b7 249 $cmd .= " --match multiport" if $rule->{nbdport} && $rule->{nbdport} > 1;
3a616aa0 250 $cmd .= " --dport $rule->{dport}" if $rule->{dport};
4cdbb3b7 251 $cmd .= " --match multiport" if $rule->{nbsport} && $rule->{nbsport} > 1;
3a616aa0
AD
252 $cmd .= " --sport $rule->{sport}" if $rule->{sport};
253 $cmd .= " -j $rule->{action}" if $rule->{action};
254
3fa83edf
DM
255 ruleset_addrule($ruleset, $chain, $cmd) if $cmd;
256}
257
258sub ruleset_create_chain {
259 my ($ruleset, $chain) = @_;
3a616aa0 260
d050c724
DM
261 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
262
3fa83edf
DM
263 die "chain '$chain' already exists\n" if $ruleset->{$chain};
264
265 $ruleset->{$chain} = [];
3a616aa0
AD
266}
267
3fa83edf
DM
268sub ruleset_chain_exist {
269 my ($ruleset, $chain) = @_;
3a616aa0 270
3fa83edf
DM
271 return $ruleset->{$chain} ? 1 : undef;
272}
3a616aa0 273
3fa83edf
DM
274sub ruleset_addrule {
275 my ($ruleset, $chain, $rule) = @_;
3a616aa0 276
3fa83edf 277 die "no such chain '$chain'\n" if !$ruleset->{$chain};
3a616aa0 278
3fa83edf
DM
279 push @{$ruleset->{$chain}}, "-A $chain $rule";
280}
3a616aa0 281
3fa83edf
DM
282sub ruleset_insertrule {
283 my ($ruleset, $chain, $rule) = @_;
3a616aa0 284
3fa83edf 285 die "no such chain '$chain'\n" if !$ruleset->{$chain};
3a616aa0 286
3fa83edf
DM
287 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
288}
3a616aa0 289
3fa83edf
DM
290sub generate_bridge_chains {
291 my ($ruleset, $bridge) = @_;
3a616aa0 292
dec84fcd
DM
293 if (!ruleset_chain_exist($ruleset, "PVEFW-BRIDGE-IN")){
294 ruleset_create_chain($ruleset, "PVEFW-BRIDGE-IN");
d4d0fd1d
AD
295 }
296
dec84fcd
DM
297 if (!ruleset_chain_exist($ruleset, "PVEFW-BRIDGE-OUT")){
298 ruleset_create_chain($ruleset, "PVEFW-BRIDGE-OUT");
d4d0fd1d 299 }
3fa83edf 300
dec84fcd
DM
301 if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){
302 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3fa83edf 303
dec84fcd
DM
304 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m state --state RELATED,ESTABLISHED -j ACCEPT");
305 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-in --physdev-is-bridged -j PVEFW-BRIDGE-OUT");
306 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-out --physdev-is-bridged -j PVEFW-BRIDGE-IN");
3a616aa0
AD
307 }
308
3fa83edf
DM
309 if (!ruleset_chain_exist($ruleset, "$bridge-IN")) {
310 ruleset_create_chain($ruleset, "$bridge-IN");
dec84fcd
DM
311 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing
312 ruleset_addrule($ruleset, "PVEFW-BRIDGE-IN", "-j $bridge-IN");
3fa83edf
DM
313 ruleset_addrule($ruleset, "$bridge-IN", "-j ACCEPT");
314 }
3a616aa0 315
3fa83edf
DM
316 if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) {
317 ruleset_create_chain($ruleset, "$bridge-OUT");
dec84fcd
DM
318 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing
319 ruleset_addrule($ruleset, "PVEFW-BRIDGE-OUT", "-j $bridge-OUT");
3fa83edf
DM
320 }
321}
3a616aa0
AD
322
323sub generate_tap_rules_direction {
c29f55c9 324 my ($ruleset, $iface, $netid, $macaddr, $rules, $bridge, $direction) = @_;
3a616aa0
AD
325
326 my $tapchain = "$iface-$direction";
327
3fa83edf 328 ruleset_create_chain($ruleset, $tapchain);
3a616aa0 329
3fa83edf
DM
330 ruleset_addrule($ruleset, $tapchain, "-m state --state INVALID -j DROP");
331 ruleset_addrule($ruleset, $tapchain, "-m state --state RELATED,ESTABLISHED -j ACCEPT");
3a616aa0 332
c29f55c9
DM
333 if ($direction eq 'OUT' && defined($macaddr)) {
334 ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
335 }
336
3fa83edf 337 if ($rules) {
3a616aa0
AD
338 foreach my $rule (@$rules) {
339 next if $rule->{iface} && $rule->{iface} ne $netid;
340 if($rule->{action} =~ m/^(GROUP-(\S+))$/){
3fa83edf
DM
341 $rule->{action} .= "-$direction";
342 # generate empty group rule if don't exist
343 if(!ruleset_chain_exist($ruleset, $rule->{action})){
344 generate_group_rules($ruleset, $2);
345 }
3a616aa0 346 }
3fa83edf 347 # we go to vmbr-IN if accept in out rules
3a616aa0 348 $rule->{action} = "$bridge-IN" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT';
3fa83edf 349 ruleset_generate_rule($ruleset, $tapchain, $rule);
3a616aa0
AD
350 }
351 }
352
3fa83edf
DM
353 ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
354 ruleset_addrule($ruleset, $tapchain, "-j DROP");
3a616aa0 355
3fa83edf
DM
356 # plug the tap chain to bridge chain
357 my $physdevdirection = $direction eq 'IN' ? "out" : "in";
358 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
359 ruleset_insertrule($ruleset, "$bridge-$direction", $rule);
3a616aa0 360
3fa83edf
DM
361 if ($direction eq 'OUT'){
362 # add tap->host rules
363 my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain";
dec84fcd 364 ruleset_addrule($ruleset, "PVEFW-INPUT", $rule);
3a616aa0
AD
365 }
366}
367
0bd5f137 368sub enablehostfw {
3fa83edf 369 my ($ruleset) = @_;
0bd5f137
AD
370
371 my $filename = "/etc/pve/local/host.fw";
372 my $fh = IO::File->new($filename, O_RDONLY);
373 return if !$fh;
374
375 my $rules = parse_fw_rules($filename, $fh);
0bd5f137 376
3fa83edf 377 # host inbound firewall
dec84fcd
DM
378 my $chain = "PVEFW-HOST-IN";
379 ruleset_create_chain($ruleset, $chain);
0bd5f137 380
dec84fcd
DM
381 ruleset_addrule($ruleset, $chain, "-m state --state INVALID -j DROP");
382 ruleset_addrule($ruleset, $chain, "-m state --state RELATED,ESTABLISHED -j ACCEPT");
383 ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT");
384 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
385 ruleset_addrule($ruleset, $chain, "-p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
386 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
0bd5f137 387
3fa83edf
DM
388 if ($rules->{in}) {
389 foreach my $rule (@{$rules->{in}}) {
390 # we use RETURN because we need to check also tap rules
391 $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
dec84fcd 392 ruleset_generate_rule($ruleset, $chain, $rule);
3fa83edf 393 }
0bd5f137
AD
394 }
395
dec84fcd
DM
396 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
397 ruleset_addrule($ruleset, $chain, "-j DROP");
0bd5f137 398
3fa83edf 399 # host outbound firewall
aadd745e 400 $chain = "PVEFW-HOST-OUT";
dec84fcd
DM
401 ruleset_create_chain($ruleset, $chain);
402
403 ruleset_addrule($ruleset, $chain, "-m state --state INVALID -j DROP");
404 ruleset_addrule($ruleset, $chain, "-m state --state RELATED,ESTABLISHED -j ACCEPT");
405 ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT");
406 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
407 ruleset_addrule($ruleset, $chain, "-p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
408 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
0bd5f137 409
3fa83edf
DM
410 if ($rules->{out}) {
411 foreach my $rule (@{$rules->{out}}) {
412 # we use RETURN because we need to check also tap rules
413 $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
dec84fcd 414 ruleset_generate_rule($ruleset, $chain, $rule);
3fa83edf 415 }
0bd5f137
AD
416 }
417
dec84fcd
DM
418 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
419 ruleset_addrule($ruleset, $chain, "-j DROP");
9d31b418 420
dec84fcd
DM
421 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
422 ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
9d31b418
AD
423}
424
425sub generate_group_rules {
3fa83edf 426 my ($ruleset, $group) = @_;
9d31b418
AD
427
428 my $filename = "/etc/pve/firewall/groups.fw";
429 my $fh = IO::File->new($filename, O_RDONLY);
430 return if !$fh;
431
432 my $rules = parse_fw_rules($filename, $fh, $group);
9d31b418 433
3fa83edf 434 my $chain = "GROUP-${group}-IN";
9d31b418 435
3fa83edf 436 ruleset_create_chain($ruleset, $chain);
9d31b418 437
3fa83edf
DM
438 if ($rules->{in}) {
439 foreach my $rule (@{$rules->{in}}) {
440 ruleset_generate_rule($ruleset, $chain, $rule);
9d31b418
AD
441 }
442 }
443
3fa83edf 444 $chain = "GROUP-${group}-OUT";
9d31b418 445
3fa83edf 446 ruleset_create_chain($ruleset, $chain);
9d31b418 447
3fa83edf
DM
448 if ($rules->{out}) {
449 foreach my $rule (@{$rules->{out}}) {
dec84fcd 450 # we go the PVEFW-BRIDGE-IN because we need to check also other tap rules
3fa83edf 451 # (and group rules can be set on any bridge, so we can't go to VMBRXX-IN)
dec84fcd 452 $rule->{action} = 'PVEFW-BRIDGE-IN' if $rule->{action} eq 'ACCEPT';
3fa83edf 453 ruleset_generate_rule($rule, $chain, $rule);
9d31b418
AD
454 }
455 }
9d31b418
AD
456}
457
5e1267a5 458sub parse_fw_rules {
9d31b418 459 my ($filename, $fh, $group) = @_;
5e1267a5
DM
460
461 my $section;
9d31b418
AD
462 my $securitygroup;
463 my $securitygroupexist;
5e1267a5
DM
464
465 my $res = { in => [], out => [] };
466
a332200b 467 my $macros = get_firewall_macros();
fcba0beb
DM
468 my $protocols = get_etc_protocols();
469
5e1267a5
DM
470 while (defined(my $line = <$fh>)) {
471 next if $line =~ m/^#/;
472 next if $line =~ m/^\s*$/;
473
9d31b418 474 if ($line =~ m/^\[(in|out)(:(\S+))?\]\s*$/i) {
5e1267a5 475 $section = lc($1);
9d31b418
AD
476 $securitygroup = lc($3) if $3;
477 $securitygroupexist = 1 if $securitygroup && $securitygroup eq $group;
5e1267a5
DM
478 next;
479 }
480 next if !$section;
9d31b418 481 next if $group && $securitygroup ne $group;
5e1267a5
DM
482
483 my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
484 split(/\s+/, $line);
485
486 if (!$action) {
487 warn "skip incomplete line\n";
488 next;
489 }
490
491 my $service;
3a616aa0 492 if ($action =~ m/^(ACCEPT|DROP|REJECT|GROUP-(\S+))$/) {
5e1267a5
DM
493 # OK
494 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
495 ($service, $action) = ($1, $2);
496 if (!$macros->{$service}) {
497 warn "unknown service '$service'\n";
498 next;
499 }
500 } else {
501 warn "unknown action '$action'\n";
502 next;
503 }
504
505 $iface = undef if $iface && $iface eq '-';
506 if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) {
507 warn "unknown interface '$iface'\n";
508 next;
509 }
510
511 $proto = undef if $proto && $proto eq '-';
fcba0beb
DM
512 if ($proto && !(defined($protocols->{byname}->{$proto}) ||
513 defined($protocols->{byid}->{$proto}))) {
5e1267a5
DM
514 warn "unknown protokol '$proto'\n";
515 next;
516 }
517
518 $source = undef if $source && $source eq '-';
5e1267a5 519 $dest = undef if $dest && $dest eq '-';
5e1267a5
DM
520
521 $dport = undef if $dport && $dport eq '-';
522 $sport = undef if $sport && $sport eq '-';
4cdbb3b7
AD
523 my $nbdport = undef;
524 my $nbsport = undef;
d6de1dc2
AD
525 my $nbsource = undef;
526 my $nbdest = undef;
5e1267a5 527
ecbea048 528 eval {
d6de1dc2
AD
529 $nbsource = parse_address_list($source) if $source;
530 $nbdest = parse_address_list($dest) if $dest;
4cdbb3b7
AD
531 $nbdport = parse_port_name_number_or_range($dport) if $dport;
532 $nbsport = parse_port_name_number_or_range($sport) if $sport;
ecbea048
DM
533 };
534 if (my $err = $@) {
535 warn $err;
536 next;
537
538 }
539
540
5e1267a5
DM
541 my $rule = {
542 action => $action,
543 service => $service,
544 iface => $iface,
545 source => $source,
546 dest => $dest,
d6de1dc2
AD
547 nbsource => $nbsource,
548 nbdest => $nbdest,
5e1267a5
DM
549 proto => $proto,
550 dport => $dport,
551 sport => $sport,
4cdbb3b7
AD
552 nbdport => $nbdport,
553 nbsport => $nbsport,
554
5e1267a5
DM
555 };
556
557 push @{$res->{$section}}, $rule;
558 }
559
9d31b418 560 die "security group $group don't exist" if $group && !$securitygroupexist;
5e1267a5
DM
561 return $res;
562}
563
06320eb0
DM
564sub run_locked {
565 my ($code, @param) = @_;
566
567 my $timeout = 10;
568
569 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
570
571 die $@ if $@;
572
573 return $res;
574}
575
5e1267a5
DM
576sub read_local_vm_config {
577
578 my $openvz = {};
579
580 my $qemu = {};
581
582 my $list = PVE::QemuServer::config_list();
583
584 foreach my $vmid (keys %$list) {
5e1267a5
DM
585 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
586 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
587 $qemu->{$vmid} = $conf;
588 }
589 }
590
591 my $vmdata = { openvz => $openvz, qemu => $qemu };
592
593 return $vmdata;
594};
595
596sub read_vm_firewall_rules {
597 my ($vmdata) = @_;
598 my $rules = {};
599 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
600 my $filename = "/etc/pve/firewall/$vmid.fw";
601 my $fh = IO::File->new($filename, O_RDONLY);
602 next if !$fh;
603
604 $rules->{$vmid} = parse_fw_rules($filename, $fh);
605 }
606
607 return $rules;
608}
609
610sub compile {
5e1267a5
DM
611 my $vmdata = read_local_vm_config();
612 my $rules = read_vm_firewall_rules($vmdata);
613
3fa83edf
DM
614 #print Dumper($rules);
615
616 my $ruleset = {};
617
618 # setup host firewall rules
dec84fcd
DM
619 ruleset_create_chain($ruleset, "PVEFW-INPUT");
620 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
3fa83edf
DM
621
622 enablehostfw($ruleset);
623
624 # generate firewall rules for QEMU VMs
625 foreach my $vmid (keys %{$vmdata->{qemu}}) {
626 my $conf = $vmdata->{qemu}->{$vmid};
627 next if !$rules->{$vmid};
628
629 foreach my $netid (keys %$conf) {
630 next if $netid !~ m/^net(\d+)$/;
631 my $net = PVE::QemuServer::parse_net($conf->{$netid});
632 next if !$net;
633 my $iface = "tap${vmid}i$1";
5e1267a5 634
3fa83edf
DM
635 my $bridge = $net->{bridge};
636 next if !$bridge; # fixme: ?
637
638 $bridge .= "v$net->{tag}" if $net->{tag};
639
640 generate_bridge_chains($ruleset, $bridge);
641
c29f55c9
DM
642 my $macaddr = $net->{macaddr};
643 generate_tap_rules_direction($ruleset, $iface, $netid, $macaddr, $rules->{$vmid}->{in}, $bridge, 'IN');
644 generate_tap_rules_direction($ruleset, $iface, $netid, $macaddr, $rules->{$vmid}->{out}, $bridge, 'OUT');
3fa83edf
DM
645 }
646 }
3fa83edf
DM
647 return $ruleset;
648}
649
650sub get_ruleset_status {
651 my ($ruleset, $verbose) = @_;
652
653 my $active_chains = iptables_get_chains();
654
655 my $statushash = {};
656
657 foreach my $chain (sort keys %$ruleset) {
09d5f68e 658 my $digest = Digest::SHA->new('sha1');
3fa83edf
DM
659 foreach my $cmd (@{$ruleset->{$chain}}) {
660 $digest->add("$cmd\n");
661 }
662 my $sig = $digest->b64digest;
663 $statushash->{$chain}->{sig} = $sig;
664
665 my $oldsig = $active_chains->{$chain};
666 if (!defined($oldsig)) {
667 $statushash->{$chain}->{action} = 'create';
668 } else {
669 if ($oldsig eq $sig) {
670 $statushash->{$chain}->{action} = 'exists';
671 } else {
672 $statushash->{$chain}->{action} = 'update';
673 }
674 }
675 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
676 foreach my $cmd (@{$ruleset->{$chain}}) {
677 print "\t$cmd\n" if $verbose;
678 }
679 }
680
681 foreach my $chain (sort keys %$active_chains) {
682 if (!defined($ruleset->{$chain})) {
683 my $sig = $active_chains->{$chain};
684 $statushash->{$chain}->{action} = 'delete';
685 $statushash->{$chain}->{sig} = $sig;
686 print "delete $chain ($sig)\n" if $verbose;
687 }
688 }
689
690 return $statushash;
691}
692
693sub print_ruleset {
694 my ($ruleset) = @_;
695
696 get_ruleset_status($ruleset, 1);
697}
698
699sub print_sig_rule {
700 my ($chain, $sig) = @_;
701
09d5f68e
DM
702 # We just use this to store a SHA1 checksum used to detect changes
703 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
b6360c3f
DM
704}
705
a84f4d96
DM
706sub apply_ruleset {
707 my ($ruleset, $verbose) = @_;
3fa83edf 708
a84f4d96 709 enable_bridge_firewall();
3fa83edf
DM
710
711 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
712
713 my $statushash = get_ruleset_status($ruleset, $verbose);
714
715 # create missing chains first
716 foreach my $chain (sort keys %$ruleset) {
717 my $stat = $statushash->{$chain};
718 die "internal error" if !$stat;
719 next if $stat->{action} ne 'create';
886aba9c 720
3fa83edf
DM
721 $cmdlist .= ":$chain - [0:0]\n";
722 }
723
dec84fcd 724 my $rule = "INPUT -j PVEFW-INPUT";
3fa83edf
DM
725 if (!PVE::Firewall::iptables_rule_exist($rule)) {
726 $cmdlist .= "-A $rule\n";
727 }
dec84fcd 728 $rule = "OUTPUT -j PVEFW-OUTPUT";
3fa83edf
DM
729 if (!PVE::Firewall::iptables_rule_exist($rule)) {
730 $cmdlist .= "-A $rule\n";
731 }
732
dec84fcd 733 $rule = "FORWARD -j PVEFW-FORWARD";
3fa83edf
DM
734 if (!PVE::Firewall::iptables_rule_exist($rule)) {
735 $cmdlist .= "-A $rule\n";
736 }
737
738 foreach my $chain (sort keys %$ruleset) {
739 my $stat = $statushash->{$chain};
740 die "internal error" if !$stat;
741
742 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
743 $cmdlist .= "-F $chain\n";
744 foreach my $cmd (@{$ruleset->{$chain}}) {
745 $cmdlist .= "$cmd\n";
746 }
747 $cmdlist .= print_sig_rule($chain, $stat->{sig});
748 } elsif ($stat->{action} eq 'delete') {
749 $cmdlist .= "-F $chain\n";
750 $cmdlist .= "-X $chain\n";
751 } elsif ($stat->{action} eq 'exists') {
752 # do nothing
753 } else {
754 die "internal error - unknown status '$stat->{action}'";
755 }
756 }
757
758 $cmdlist .= "COMMIT\n";
759
760 print $cmdlist if $verbose;
761
762 iptables_restore_cmdlist($cmdlist);
763
764 # test: re-read status and check if everything is up to date
765 $statushash = get_ruleset_status($ruleset);
766
767 my $errors;
768 foreach my $chain (sort keys %$ruleset) {
769 my $stat = $statushash->{$chain};
770 if ($stat->{action} ne 'exists') {
771 warn "unable to update chain '$chain'\n";
772 $errors = 1;
773 }
774 }
b6360c3f 775
3fa83edf 776 die "unable to apply firewall changes\n" if $errors;
b6360c3f
DM
777}
778
b6360c3f 7791;