]> git.proxmox.com Git - pve-firewall.git/blob - PVE/Firewall.pm
de25b045bd897b5ad9892180aa820b46c76b61b8
[pve-firewall.git] / PVE / Firewall.pm
1 package PVE::Firewall;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use PVE::Tools;
7 use PVE::QemuServer;
8 use File::Path;
9 use IO::File;
10 use Net::IP;
11 use PVE::Tools qw(run_command);
12
13 use Data::Dumper;
14
15 my $macros;
16 my @ruleset = ();
17
18 sub get_shorewall_macros {
19
20 return $macros if $macros;
21
22 foreach my $path (</usr/share/shorewall/macro.*>) {
23 if ($path =~ m|/macro\.(\S+)$|) {
24 $macros->{$1} = 1;
25 }
26 }
27 return $macros;
28 }
29
30 my $etc_services;
31
32 sub get_etc_services {
33
34 return $etc_services if $etc_services;
35
36 my $filename = "/etc/services";
37
38 my $fh = IO::File->new($filename, O_RDONLY);
39 if (!$fh) {
40 warn "unable to read '$filename' - $!\n";
41 return {};
42 }
43
44 my $services = {};
45
46 while (my $line = <$fh>) {
47 chomp ($line);
48 next if $line =~m/^#/;
49 next if ($line =~m/^\s*$/);
50
51 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
52 $services->{byid}->{$2}->{name} = $1;
53 $services->{byid}->{$2}->{$3} = 1;
54 $services->{byname}->{$1} = $services->{byid}->{$2};
55 }
56 }
57
58 close($fh);
59
60 $etc_services = $services;
61
62
63 return $etc_services;
64 }
65
66 my $etc_protocols;
67
68 sub get_etc_protocols {
69 return $etc_protocols if $etc_protocols;
70
71 my $filename = "/etc/protocols";
72
73 my $fh = IO::File->new($filename, O_RDONLY);
74 if (!$fh) {
75 warn "unable to read '$filename' - $!\n";
76 return {};
77 }
78
79 my $protocols = {};
80
81 while (my $line = <$fh>) {
82 chomp ($line);
83 next if $line =~m/^#/;
84 next if ($line =~m/^\s*$/);
85
86 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
87 $protocols->{byid}->{$2}->{name} = $1;
88 $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
89 }
90 }
91
92 close($fh);
93
94 $etc_protocols = $protocols;
95
96 return $etc_protocols;
97 }
98
99 sub parse_address_list {
100 my ($str) = @_;
101
102 my $nbaor = 0;
103 foreach my $aor (split(/,/, $str)) {
104 if (!Net::IP->new($aor)) {
105 my $err = Net::IP::Error();
106 die "invalid IP address: $err\n";
107 }else{
108 $nbaor++;
109 }
110 }
111 return $nbaor;
112 }
113
114 sub parse_port_name_number_or_range {
115 my ($str) = @_;
116
117 my $services = PVE::Firewall::get_etc_services();
118 my $nbports = 0;
119 foreach my $item (split(/,/, $str)) {
120 my $portlist = "";
121 foreach my $pon (split(':', $item, 2)) {
122 if ($pon =~ m/^\d+$/){
123 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65536;
124 }else{
125 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon};
126 }
127 $nbports++;
128 }
129 }
130
131 return ($nbports);
132 }
133
134 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
135
136 sub iptables {
137 my ($cmd) = @_;
138
139 run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
140 }
141
142 sub iptables_restore {
143
144 unshift (@ruleset, '*filter');
145 push (@ruleset, 'COMMIT');
146
147 my $cmdlist = join("\n", @ruleset);
148
149 run_command("echo '$cmdlist' | /sbin/iptables-restore -n", outfunc => sub {});
150 }
151
152 sub iptables_addrule {
153 my ($rule) = @_;
154
155 push (@ruleset, $rule);
156 }
157
158 sub iptables_chain_exist {
159 my ($chain) = @_;
160
161 eval{
162 iptables("-n --list $chain");
163 };
164 return undef if $@;
165
166 return 1;
167 }
168
169 sub iptables_rule_exist {
170 my ($rule) = @_;
171
172 eval{
173 iptables("-C $rule");
174 };
175 return undef if $@;
176
177 return 1;
178 }
179
180 sub iptables_generate_rule {
181 my ($chain, $rule) = @_;
182
183 my $cmd = "-A $chain";
184
185 $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
186 $cmd .= " -s $rule->{source}" if $rule->{source};
187 $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
188 $cmd .= " -d $rule->{dest}" if $rule->{destination};
189 $cmd .= " -p $rule->{proto}" if $rule->{proto};
190 $cmd .= " --match multiport" if $rule->{nbdport} && $rule->{nbdport} > 1;
191 $cmd .= " --dport $rule->{dport}" if $rule->{dport};
192 $cmd .= " --match multiport" if $rule->{nbsport} && $rule->{nbsport} > 1;
193 $cmd .= " --sport $rule->{sport}" if $rule->{sport};
194 $cmd .= " -j $rule->{action}" if $rule->{action};
195
196 iptables_addrule($cmd);
197
198 }
199
200 sub generate_bridge_rules {
201 my ($bridge) = @_;
202
203 if(!iptables_chain_exist("BRIDGEFW-OUT")){
204 iptables_addrule(":BRIDGEFW-OUT - [0:0]");
205 }
206
207 if(!iptables_chain_exist("BRIDGEFW-IN")){
208 iptables_addrule(":BRIDGEFW-IN - [0:0]");
209 }
210
211 if(!iptables_chain_exist("proxmoxfw-FORWARD")){
212 iptables_addrule(":proxmoxfw-FORWARD - [0:0]");
213 iptables_addrule("-I FORWARD -j proxmoxfw-FORWARD");
214 iptables_addrule("-A proxmoxfw-FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT");
215 iptables_addrule("-A proxmoxfw-FORWARD -m physdev --physdev-is-in --physdev-is-bridged -j BRIDGEFW-OUT");
216 iptables_addrule("-A proxmoxfw-FORWARD -m physdev --physdev-is-out --physdev-is-bridged -j BRIDGEFW-IN");
217
218 }
219
220 generate_proxmoxfwinput();
221
222 if(!iptables_chain_exist("$bridge-IN")){
223 iptables_addrule(":$bridge-IN - [0:0]");
224 iptables_addrule("-A proxmoxfw-FORWARD -i $bridge -j DROP"); #disable interbridge routing
225 iptables_addrule("-A BRIDGEFW-IN -j $bridge-IN");
226 iptables_addrule("-A $bridge-IN -j ACCEPT");
227
228 }
229
230 if(!iptables_chain_exist("$bridge-OUT")){
231 iptables_addrule(":$bridge-OUT - [0:0]");
232 iptables_addrule("-A proxmoxfw-FORWARD -o $bridge -j DROP"); # disable interbridge routing
233 iptables_addrule("-A BRIDGEFW-OUT -j $bridge-OUT");
234
235 }
236
237 }
238
239
240 sub generate_tap_rules_direction {
241 my ($iface, $netid, $rules, $bridge, $direction) = @_;
242
243 my $tapchain = "$iface-$direction";
244
245 iptables_addrule(":$tapchain - [0:0]");
246
247 iptables_addrule("-A $tapchain -m state --state INVALID -j DROP");
248 iptables_addrule("-A $tapchain -m state --state RELATED,ESTABLISHED -j ACCEPT");
249
250 if (scalar(@$rules)) {
251 foreach my $rule (@$rules) {
252 next if $rule->{iface} && $rule->{iface} ne $netid;
253 if($rule->{action} =~ m/^(GROUP-(\S+))$/){
254 $rule->{action} .= "-$direction";
255 #generate empty group rule if don't exist
256 if(!iptables_chain_exist($rule->{action})){
257 generate_group_rules($2);
258 }
259 }
260 #we go to vmbr-IN if accept in out rules
261 $rule->{action} = "$bridge-IN" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT';
262 iptables_generate_rule($tapchain, $rule);
263 }
264 }
265
266 iptables_addrule("-A $tapchain -j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
267 iptables_addrule("-A $tapchain -j DROP");
268
269 #plug the tap chain to bridge chain
270 my $physdevdirection = $direction eq 'IN' ? "out":"in";
271 my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
272
273 if(!iptables_rule_exist($rule)){
274 iptables_addrule("-I $rule");
275 }
276
277 if($direction eq 'OUT'){
278 #add tap->host rules
279 my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
280
281 if(!iptables_rule_exist($rule)){
282 iptables_addrule("-A $rule");
283 }
284 }
285 }
286
287 sub generate_tap_rules {
288 my ($net, $netid, $vmid) = @_;
289
290 my $filename = "/etc/pve/firewall/$vmid.fw";
291 my $fh = IO::File->new($filename, O_RDONLY);
292 return if !$fh;
293
294 #generate bridge rules
295 my $bridge = $net->{bridge};
296 my $tag = $net->{tag};
297 $bridge .= "v$tag" if $tag;
298
299 #generate tap chain
300 my $rules = parse_fw_rules($filename, $fh);
301
302 my $inrules = $rules->{in};
303 my $outrules = $rules->{out};
304
305 my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/;
306
307 generate_bridge_rules($bridge);
308 generate_tap_rules_direction($iface, $netid, $inrules, $bridge, 'IN');
309 generate_tap_rules_direction($iface, $netid, $outrules, $bridge, 'OUT');
310 iptables_restore();
311 }
312
313 sub flush_tap_rules {
314 my ($net, $netid, $vmid) = @_;
315
316 my $bridge = $net->{bridge};
317 my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/;
318
319 flush_tap_rules_direction($iface, $bridge, 'IN');
320 flush_tap_rules_direction($iface, $bridge, 'OUT');
321 iptables_restore();
322 }
323
324 sub flush_tap_rules_direction {
325 my ($iface, $bridge, $direction) = @_;
326
327 my $tapchain = "$iface-$direction";
328
329 if(iptables_chain_exist($tapchain)){
330 iptables_addrule("-F $tapchain");
331
332 my $physdevdirection = $direction eq 'IN' ? "out":"in";
333 my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
334 if(iptables_rule_exist($rule)){
335 iptables_addrule("-D $rule");
336 }
337
338 if($direction eq 'OUT'){
339 my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
340
341 if(!iptables_rule_exist($rule)){
342 iptables_addrule("-D $rule");
343 }
344 }
345
346 iptables_addrule("-X $tapchain");
347 }
348 }
349
350 sub enablehostfw {
351
352 generate_proxmoxfwinput();
353 generate_proxmoxfwoutput();
354
355 my $filename = "/etc/pve/local/host.fw";
356 my $fh = IO::File->new($filename, O_RDONLY);
357 return if !$fh;
358
359 my $rules = parse_fw_rules($filename, $fh);
360 my $inrules = $rules->{in};
361 my $outrules = $rules->{out};
362
363 #host inbound firewall
364 iptables_addrule(":host-IN - [0:0]");
365 iptables_addrule("-A host-IN -m state --state INVALID -j DROP");
366 iptables_addrule("-A host-IN -m state --state RELATED,ESTABLISHED -j ACCEPT");
367 iptables_addrule("-A host-IN -i lo -j ACCEPT");
368 iptables_addrule("-A host-IN -m addrtype --dst-type MULTICAST -j ACCEPT");
369 iptables_addrule("-A host-IN -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
370 iptables_addrule("-A host-IN -p udp -m udp --dport 9000 -j ACCEPT"); #corosync
371
372 if (scalar(@$inrules)) {
373 foreach my $rule (@$inrules) {
374 #we use RETURN because we need to check also tap rules
375 $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
376 iptables_generate_rule('host-IN', $rule);
377 }
378 }
379
380 iptables_addrule("-A host-IN -j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4");
381 iptables_addrule("-A host-IN -j DROP");
382
383 #host outbound firewall
384 iptables_addrule(":host-OUT - [0:0]");
385 iptables_addrule("-A host-OUT -m state --state INVALID -j DROP");
386 iptables_addrule("-A host-OUT -m state --state RELATED,ESTABLISHED -j ACCEPT");
387 iptables_addrule("-A host-OUT -o lo -j ACCEPT");
388 iptables_addrule("-A host-OUT -m addrtype --dst-type MULTICAST -j ACCEPT");
389 iptables_addrule("-A host-OUT -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT");
390 iptables_addrule("-A host-OUT -p udp -m udp --dport 9000 -j ACCEPT"); #corosync
391
392 if (scalar(@$outrules)) {
393 foreach my $rule (@$outrules) {
394 #we use RETURN because we need to check also tap rules
395 $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT';
396 iptables_generate_rule('host-OUT', $rule);
397 }
398 }
399
400 iptables_addrule("-A host-OUT -j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4");
401 iptables_addrule("-A host-OUT -j DROP");
402
403
404 my $rule = "proxmoxfw-INPUT -j host-IN";
405 if(!iptables_rule_exist($rule)){
406 iptables_addrule("-I $rule");
407 }
408
409 $rule = "proxmoxfw-OUTPUT -j host-OUT";
410 if(!iptables_rule_exist($rule)){
411 iptables_addrule("-I $rule");
412 }
413
414 iptables_restore();
415
416
417 }
418
419 sub disablehostfw {
420
421 my $chain = "host-IN";
422
423 my $rule = "proxmoxfw-INPUT -j $chain";
424 if(iptables_rule_exist($rule)){
425 iptables_addrule("-D $rule");
426 }
427
428 if(iptables_chain_exist($chain)){
429 iptables_addrule("-F $chain");
430 iptables_addrule("-X $chain");
431 }
432
433 $chain = "host-OUT";
434
435 $rule = "proxmoxfw-OUTPUT -j $chain";
436 if(iptables_rule_exist($rule)){
437 iptables_addrule("-D $rule");
438 }
439
440 if(iptables_chain_exist($chain)){
441 iptables_addrule("-F $chain");
442 iptables_addrule("-X $chain");
443 }
444
445 iptables_restore();
446 }
447
448 sub generate_proxmoxfwinput {
449
450 if(!iptables_chain_exist("proxmoxfw-INPUT")){
451 iptables_addrule(":proxmoxfw-INPUT - [0:0]");
452 iptables_addrule("-I INPUT -j proxmoxfw-INPUT");
453 iptables_addrule("-A INPUT -j ACCEPT");
454 }
455 }
456
457 sub generate_proxmoxfwoutput {
458
459 if(!iptables_chain_exist("proxmoxfw-OUTPUT")){
460 iptables_addrule(":proxmoxfw-OUTPUT - [0:0]");
461 iptables_addrule("-I OUTPUT -j proxmoxfw-OUTPUT");
462 iptables_addrule("-A OUTPUT -j ACCEPT");
463 }
464
465 }
466
467
468 my $generate_input_rule = sub {
469 my ($zoneinfo, $rule, $net, $netid) = @_;
470
471 my $zone = $net->{zone} || die "internal error";
472 my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
473 my $tap = $net->{tap} || die "internal error";
474
475 my $dest = "$zid:$tap";
476
477 if ($rule->{dest}) {
478 $dest .= ":$rule->{dest}";
479 }
480
481 my $action = $rule->{service} ?
482 "$rule->{service}($rule->{action})" : $rule->{action};
483
484 my $sources = [];
485
486 if (!$rule->{source}) {
487 push @$sources, 'all';
488 } elsif ($zoneinfo->{$zone}->{type} eq 'bport') {
489 my $bridge_zone = $zoneinfo->{$zone}->{bridge_zone} || die "internal error";
490 my $zoneref = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
491
492 # using 'all' does not work, so we create one rule for
493 # each related zone on the same bridge
494 push @$sources, "${zoneref}:$rule->{source}";
495 foreach my $z (keys %$zoneinfo) {
496 next if $z eq $zone;
497 next if !$zoneinfo->{$z}->{bridge_zone};
498 next if $zoneinfo->{$z}->{bridge_zone} ne $bridge_zone;
499 $zoneref = $zoneinfo->{$z}->{zoneref} || die "internal error";
500 push @$sources, "${zoneref}:$rule->{source}";
501 }
502 } else {
503 push @$sources, "all:$rule->{source}";
504 }
505
506 my $out = '';
507
508 foreach my $source (@$sources) {
509 $out .= sprintf($rule_format, $action, $source, $dest, $rule->{proto} || '-',
510 $rule->{dport} || '-', $rule->{sport} || '-');
511 }
512
513 return $out;
514 };
515
516 my $generate_output_rule = sub {
517 my ($zoneinfo, $rule, $net, $netid) = @_;
518
519 my $zone = $net->{zone} || die "internal error";
520 my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
521 my $tap = $net->{tap} || die "internal error";
522
523 my $action = $rule->{service} ?
524 "$rule->{service}($rule->{action})" : $rule->{action};
525
526 my $dest;
527
528 if (!$rule->{dest}) {
529 $dest = 'all';
530 } else {
531 $dest = "all:$rule->{dest}";
532 }
533
534 return sprintf($rule_format, $action, "$zid:$tap", $dest,
535 $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-');
536 };
537
538 # we need complete VM configuration of all VMs (openvz/qemu)
539 # in vmdata
540
541 my $compile_shorewall = sub {
542 my ($targetdir, $vmdata, $rules) = @_;
543
544 # remove existing data ?
545 foreach my $file (qw(params zones rules interfaces maclist policy)) {
546 unlink "$targetdir/$file";
547 }
548
549 my $netinfo;
550
551 my $zoneinfo = {
552 fw => { type => 'firewall' },
553 };
554
555 my $maclist = {};
556
557 my $register_bridge;
558
559 $register_bridge = sub {
560 my ($bridge, $vlan) = @_;
561
562 my $zone = $bridge;
563
564 return $zone if $zoneinfo->{$zone};
565
566 my $ext_zone = "${bridge}_ext";
567
568 $zoneinfo->{$zone} = {
569 type => 'bridge',
570 bridge => $bridge,
571 bridge_ext_zone => $ext_zone,
572 };
573
574 # physical input devices
575 my $dir = "/sys/class/net/$bridge/brif";
576 my $physical = {};
577 PVE::Tools::dir_glob_foreach($dir, '((eth|bond).+)', sub {
578 my ($slave) = @_;
579 $physical->{$slave} = 1;
580 });
581
582 $zoneinfo->{$ext_zone} = {
583 type => 'bport',
584 bridge_zone => $zone,
585 ifaces => $physical,
586 };
587
588 return &$register_bridge("${bridge}v${vlan}") if defined($vlan);
589
590 return $zone;
591 };
592
593 my $register_bridge_port = sub {
594 my ($bridge, $vlan, $vmzone, $tap) = @_;
595
596 my $bridge_zone = &$register_bridge($bridge, $vlan);
597 my $zone = $bridge_zone . '_' . $vmzone;
598
599 if (!$zoneinfo->{$zone}) {
600 $zoneinfo->{$zone} = {
601 type => 'bport',
602 bridge_zone => $bridge_zone,
603 ifaces => {},
604 };
605 }
606
607 $zoneinfo->{$zone}->{ifaces}->{$tap} = 1;
608
609 return $zone;
610 };
611
612 foreach my $vmid (keys %{$vmdata->{qemu}}) {
613 $netinfo->{$vmid} = {};
614 my $conf = $vmdata->{qemu}->{$vmid};
615 foreach my $opt (keys %$conf) {
616 next if $opt !~ m/^net(\d+)$/;
617 my $netnum = $1;
618 my $net = PVE::QemuServer::parse_net($conf->{$opt});
619 next if !$net;
620 die "implement me" if !$net->{bridge};
621
622 my $vmzone = $conf->{zone} || "vm$vmid";
623 $net->{tap} = "tap${vmid}i${netnum}";
624 $maclist->{$net->{tap}} = $net->{macaddr} || die "internal error";
625 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap});
626 $netinfo->{$vmid}->{$opt} = $net;
627 }
628 }
629
630 #print Dumper($netinfo);
631
632 # NOTE: zone names have length limit, so we need to
633 # translate them into shorter names
634
635 my $zoneid = 0;
636 my $zonemap = { fw => 'fw' };
637
638 my $lookup_zonename = sub {
639 my ($zone) = @_;
640
641 return $zonemap->{$zone} if defined($zonemap->{$zone});
642 $zonemap->{$zone} = 'z' . $zoneid++;
643
644 return $zonemap->{$zone};
645 };
646
647 foreach my $z (sort keys %$zoneinfo) {
648 $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
649 $zoneinfo->{$z}->{zonevar} = uc($z);
650 $zoneinfo->{$z}->{zoneref} = '$' . $zoneinfo->{$z}->{zonevar};
651 }
652
653 my $out;
654
655 # dump params file
656 $out = "# PVE zones\n";
657 foreach my $z (sort keys %$zoneinfo) {
658 $out .= "$zoneinfo->{$z}->{zonevar}=$zoneinfo->{$z}->{id}\n";
659 }
660 PVE::Tools::file_set_contents("$targetdir/params", $out);
661
662 # dump zone file
663
664 my $format = "%-30s %-10s %-15s\n";
665 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
666
667 foreach my $z (sort keys %$zoneinfo) {
668 my $zid = $zoneinfo->{$z}->{zoneref};
669 if ($zoneinfo->{$z}->{type} eq 'firewall') {
670 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '');
671 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
672 $out .= sprintf($format, $zid, 'ipv4', '');
673 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
674 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
675 my $bzid = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
676 $out .= sprintf($format, "$zid:$bzid", 'bport', '');
677 } else {
678 die "internal error";
679 }
680 }
681
682 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
683
684 PVE::Tools::file_set_contents("$targetdir/zones", $out);
685
686 # dump interfaces
687
688 $format = "%-25s %-20s %-10s %-15s\n";
689 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
690
691 my $maclist_format = "%-15s %-15s %-15s\n";
692 my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
693
694 foreach my $z (sort keys %$zoneinfo) {
695 my $zid = $zoneinfo->{$z}->{zoneref};
696 if ($zoneinfo->{$z}->{type} eq 'firewall') {
697 # do nothing;
698 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
699 my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
700 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional');
701 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
702 my $ifaces = $zoneinfo->{$z}->{ifaces};
703 foreach my $iface (sort keys %$ifaces) {
704 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
705 my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
706 my $iftxt = "$bridge:$iface";
707
708 if ($maclist->{$iface}) {
709 $out .= sprintf($format, $zid, $iftxt, '-', 'maclist');
710 $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
711 } else {
712 $out .= sprintf($format, $zid, $iftxt, '-', '');
713 }
714 }
715 } else {
716 die "internal error";
717 }
718 }
719
720 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
721
722 PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
723
724 # dump maclist
725 PVE::Tools::file_set_contents("$targetdir/maclist", $macs);
726
727 # dump policy
728
729 $format = "%-15s %-15s %-15s %s\n";
730 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
731 $out .= sprintf($format, 'fw', 'all', 'ACCEPT', '');
732
733 # we need to disable intra-zone traffic on bridges. Else traffic
734 # from untracked interfaces simply pass the firewall
735 foreach my $z (sort keys %$zoneinfo) {
736 my $zid = $zoneinfo->{$z}->{zoneref};
737 if ($zoneinfo->{$z}->{type} eq 'bridge') {
738 $out .= sprintf($format, $zid, $zid, 'REJECT', 'info');
739 }
740 }
741 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
742
743 PVE::Tools::file_set_contents("$targetdir/policy", $out);
744
745 # dump rules
746 $out = '';
747
748 $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
749 foreach my $vmid (sort keys %$rules) {
750 my $inrules = $rules->{$vmid}->{in};
751 my $outrules = $rules->{$vmid}->{out};
752
753 if (scalar(@$inrules)) {
754 $out .= "# IN to VM $vmid\n";
755 foreach my $rule (@$inrules) {
756 foreach my $netid (keys %{$netinfo->{$vmid}}) {
757 my $net = $netinfo->{$vmid}->{$netid};
758 next if $rule->{iface} && $rule->{iface} ne $netid;
759 $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
760 }
761 }
762 }
763
764 if (scalar(@$outrules)) {
765 $out .= "# OUT from VM $vmid\n";
766 foreach my $rule (@$outrules) {
767 foreach my $netid (keys %{$netinfo->{$vmid}}) {
768 my $net = $netinfo->{$vmid}->{$netid};
769 next if $rule->{iface} && $rule->{iface} ne $netid;
770 $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
771 }
772 }
773 }
774 }
775
776 PVE::Tools::file_set_contents("$targetdir/rules", $out);
777 };
778
779
780 sub parse_fw_rules {
781 my ($filename, $fh) = @_;
782
783 my $section;
784
785 my $res = { in => [], out => [] };
786
787 my $macros = get_shorewall_macros();
788 my $protocols = get_etc_protocols();
789
790 while (defined(my $line = <$fh>)) {
791 next if $line =~ m/^#/;
792 next if $line =~ m/^\s*$/;
793
794 if ($line =~ m/^\[(in|out)\]\s*$/i) {
795 $section = lc($1);
796 next;
797 }
798 next if !$section;
799
800 my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
801 split(/\s+/, $line);
802
803 if (!$action) {
804 warn "skip incomplete line\n";
805 next;
806 }
807
808 my $service;
809 if ($action =~ m/^(ACCEPT|DROP|REJECT|GROUP-(\S+))$/) {
810 # OK
811 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
812 ($service, $action) = ($1, $2);
813 if (!$macros->{$service}) {
814 warn "unknown service '$service'\n";
815 next;
816 }
817 } else {
818 warn "unknown action '$action'\n";
819 next;
820 }
821
822 $iface = undef if $iface && $iface eq '-';
823 if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) {
824 warn "unknown interface '$iface'\n";
825 next;
826 }
827
828 $proto = undef if $proto && $proto eq '-';
829 if ($proto && !(defined($protocols->{byname}->{$proto}) ||
830 defined($protocols->{byid}->{$proto}))) {
831 warn "unknown protokol '$proto'\n";
832 next;
833 }
834
835 $source = undef if $source && $source eq '-';
836 $dest = undef if $dest && $dest eq '-';
837
838 $dport = undef if $dport && $dport eq '-';
839 $sport = undef if $sport && $sport eq '-';
840 my $nbdport = undef;
841 my $nbsport = undef;
842 my $nbsource = undef;
843 my $nbdest = undef;
844
845 eval {
846 $nbsource = parse_address_list($source) if $source;
847 $nbdest = parse_address_list($dest) if $dest;
848 $nbdport = parse_port_name_number_or_range($dport) if $dport;
849 $nbsport = parse_port_name_number_or_range($sport) if $sport;
850 };
851 if (my $err = $@) {
852 warn $err;
853 next;
854
855 }
856
857
858 my $rule = {
859 action => $action,
860 service => $service,
861 iface => $iface,
862 source => $source,
863 dest => $dest,
864 nbsource => $nbsource,
865 nbdest => $nbdest,
866 proto => $proto,
867 dport => $dport,
868 sport => $sport,
869 nbdport => $nbdport,
870 nbsport => $nbsport,
871
872 };
873
874 push @{$res->{$section}}, $rule;
875 }
876
877 return $res;
878 }
879
880 sub read_local_vm_config {
881
882 my $openvz = {};
883
884 my $qemu = {};
885
886 my $list = PVE::QemuServer::config_list();
887
888 foreach my $vmid (keys %$list) {
889 #next if !($vmid eq '100' || $vmid eq '102');
890 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
891 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
892 $qemu->{$vmid} = $conf;
893 }
894 }
895
896 my $vmdata = { openvz => $openvz, qemu => $qemu };
897
898 return $vmdata;
899 };
900
901 sub read_vm_firewall_rules {
902 my ($vmdata) = @_;
903 my $rules = {};
904 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
905 my $filename = "/etc/pve/firewall/$vmid.fw";
906 my $fh = IO::File->new($filename, O_RDONLY);
907 next if !$fh;
908
909 $rules->{$vmid} = parse_fw_rules($filename, $fh);
910 }
911
912 return $rules;
913 }
914
915 sub compile {
916
917 my $vmdata = read_local_vm_config();
918 my $rules = read_vm_firewall_rules($vmdata);
919
920 # print Dumper($vmdata);
921
922 my $swdir = '/etc/shorewall';
923 mkdir $swdir;
924
925 &$compile_shorewall($swdir, $vmdata, $rules);
926
927 PVE::Tools::run_command(['shorewall', 'compile']);
928 }
929
930 sub compile_and_start {
931 my ($restart) = @_;
932
933 compile();
934
935 PVE::Tools::run_command(['shorewall', $restart ? 'restart' : 'start']);
936 }
937
938
939 1;