]> git.proxmox.com Git - pve-firewall.git/blob - PVE/Firewall.pm
4bf4284048f96b8686cef2df9ea2344da7b012a3
[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 foreach my $aor (split(/,/, $str)) {
103 if (!Net::IP->new($aor)) {
104 my $err = Net::IP::Error();
105 die "invalid IP address: $err\n";
106 }
107 }
108 }
109
110 sub parse_port_name_number_or_range {
111 my ($str) = @_;
112
113 my $services = PVE::Firewall::get_etc_services();
114
115 foreach my $item (split(/,/, $str)) {
116 foreach my $pon (split(':', $item, 2)) {
117 next if $pon =~ m/^\d+$/ && $pon > 0 && $pon < 65536;
118 next if defined($services->{byname}->{$pon});
119 die "invalid port '$pon'\n";
120 }
121 }
122
123 }
124
125 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
126
127 sub iptables {
128 my ($cmd) = @_;
129
130 run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
131 }
132
133 sub iptables_restore {
134
135 unshift (@ruleset, '*filter');
136 push (@ruleset, 'COMMIT');
137
138 my $cmdlist = join("\n", @ruleset);
139
140 run_command("echo '$cmdlist' | /sbin/iptables-restore -n", outfunc => sub {});
141 }
142
143 sub iptables_addrule {
144 my ($rule) = @_;
145
146 push (@ruleset, $rule);
147 }
148
149 sub iptables_chain_exist {
150 my ($chain) = @_;
151
152 eval{
153 iptables("-n --list $chain");
154 };
155 return undef if $@;
156
157 return 1;
158 }
159
160 sub iptables_rule_exist {
161 my ($rule) = @_;
162
163 eval{
164 iptables("-C $rule");
165 };
166 return undef if $@;
167
168 return 1;
169 }
170
171 sub iptables_generate_rule {
172 my ($chain, $rule) = @_;
173
174 my $cmd = "-A $chain";
175
176 $cmd .= " -s $rule->{source}" if $rule->{source};
177 $cmd .= " -d $rule->{dest}" if $rule->{destination};
178 $cmd .= " -p $rule->{proto}" if $rule->{proto};
179 $cmd .= " --dport $rule->{dport}" if $rule->{dport};
180 $cmd .= " --sport $rule->{sport}" if $rule->{sport};
181 $cmd .= " -j $rule->{action}" if $rule->{action};
182
183 iptables_addrule($cmd);
184
185 }
186
187 sub generate_bridge_rules {
188 my ($bridge) = @_;
189
190 if(!iptables_chain_exist("BRIDGEFW-OUT")){
191 iptables_addrule(":BRIDGEFW-OUT - [0:0]");
192 }
193
194 if(!iptables_chain_exist("BRIDGEFW-IN")){
195 iptables_addrule(":BRIDGEFW-IN - [0:0]");
196 }
197
198 if(!iptables_chain_exist("proxmoxfw-FORWARD")){
199 iptables_addrule(":proxmoxfw-FORWARD - [0:0]");
200 iptables_addrule("-I FORWARD -j proxmoxfw-FORWARD");
201 iptables_addrule("-A proxmoxfw-FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT");
202 iptables_addrule("-A proxmoxfw-FORWARD -m physdev --physdev-is-in --physdev-is-bridged -j BRIDGEFW-OUT");
203 iptables_addrule("-A proxmoxfw-FORWARD -m physdev --physdev-is-out --physdev-is-bridged -j BRIDGEFW-IN");
204
205 }
206
207 generate_proxmoxfwinput();
208
209 if(!iptables_chain_exist("$bridge-IN")){
210 iptables_addrule(":$bridge-IN - [0:0]");
211 iptables_addrule("-A proxmoxfw-FORWARD -i $bridge -j DROP"); #disable interbridge routing
212 iptables_addrule("-A BRIDGEFW-IN -j $bridge-IN");
213 iptables_addrule("-A $bridge-IN -j ACCEPT");
214
215 }
216
217 if(!iptables_chain_exist("$bridge-OUT")){
218 iptables_addrule(":$bridge-OUT - [0:0]");
219 iptables_addrule("-A proxmoxfw-FORWARD -o $bridge -j DROP"); # disable interbridge routing
220 iptables_addrule("-A BRIDGEFW-OUT -j $bridge-OUT");
221
222 }
223
224 }
225
226
227 sub generate_tap_rules_direction {
228 my ($iface, $netid, $rules, $bridge, $direction) = @_;
229
230 my $tapchain = "$iface-$direction";
231
232 iptables_addrule(":$tapchain - [0:0]");
233
234 iptables_addrule("-A $tapchain -m state --state INVALID -j DROP");
235 iptables_addrule("-A $tapchain -m state --state RELATED,ESTABLISHED -j ACCEPT");
236
237 if (scalar(@$rules)) {
238 foreach my $rule (@$rules) {
239 next if $rule->{iface} && $rule->{iface} ne $netid;
240 if($rule->{action} =~ m/^(GROUP-(\S+))$/){
241 $rule->{action} .= "-$direction";
242 #generate empty group rule if don't exist
243 if(!iptables_chain_exist($rule->{action})){
244 generate_group_rules($2);
245 }
246 }
247 #we go to vmbr-IN if accept in out rules
248 $rule->{action} = "$bridge-IN" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT';
249 iptables_generate_rule($tapchain, $rule);
250 }
251 }
252
253 iptables_addrule("-A $tapchain -j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4");
254 iptables_addrule("-A $tapchain -j DROP");
255
256 #plug the tap chain to bridge chain
257 my $physdevdirection = $direction eq 'IN' ? "out":"in";
258 my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
259
260 if(!iptables_rule_exist($rule)){
261 iptables_addrule("-I $rule");
262 }
263
264 if($direction eq 'OUT'){
265 #add tap->host rules
266 my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
267
268 if(!iptables_rule_exist($rule)){
269 iptables_addrule("-A $rule");
270 }
271 }
272 }
273
274 sub generate_tap_rules {
275 my ($net, $netid, $vmid) = @_;
276
277 my $filename = "/etc/pve/firewall/$vmid.fw";
278 my $fh = IO::File->new($filename, O_RDONLY);
279 return if !$fh;
280
281 #generate bridge rules
282 my $bridge = $net->{bridge};
283 my $tag = $net->{tag};
284 $bridge .= "v$tag" if $tag;
285
286 #generate tap chain
287 my $rules = parse_fw_rules($filename, $fh);
288
289 my $inrules = $rules->{in};
290 my $outrules = $rules->{out};
291
292 my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/;
293
294 generate_bridge_rules($bridge);
295 generate_tap_rules_direction($iface, $netid, $inrules, $bridge, 'IN');
296 generate_tap_rules_direction($iface, $netid, $outrules, $bridge, 'OUT');
297 iptables_restore();
298 }
299
300 sub flush_tap_rules {
301 my ($net, $netid, $vmid) = @_;
302
303 my $bridge = $net->{bridge};
304 my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/;
305
306 flush_tap_rules_direction($iface, $bridge, 'IN');
307 flush_tap_rules_direction($iface, $bridge, 'OUT');
308 iptables_restore();
309 }
310
311 sub flush_tap_rules_direction {
312 my ($iface, $bridge, $direction) = @_;
313
314 my $tapchain = "$iface-$direction";
315
316 if(iptables_chain_exist($tapchain)){
317 iptables_addrule("-F $tapchain");
318
319 my $physdevdirection = $direction eq 'IN' ? "out":"in";
320 my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
321 if(iptables_rule_exist($rule)){
322 iptables_addrule("-D $rule");
323 }
324
325 if($direction eq 'OUT'){
326 my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain";
327
328 if(!iptables_rule_exist($rule)){
329 iptables_addrule("-D $rule");
330 }
331 }
332
333 iptables_addrule("-X $tapchain");
334 }
335 }
336
337 my $generate_input_rule = sub {
338 my ($zoneinfo, $rule, $net, $netid) = @_;
339
340 my $zone = $net->{zone} || die "internal error";
341 my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
342 my $tap = $net->{tap} || die "internal error";
343
344 my $dest = "$zid:$tap";
345
346 if ($rule->{dest}) {
347 $dest .= ":$rule->{dest}";
348 }
349
350 my $action = $rule->{service} ?
351 "$rule->{service}($rule->{action})" : $rule->{action};
352
353 my $sources = [];
354
355 if (!$rule->{source}) {
356 push @$sources, 'all';
357 } elsif ($zoneinfo->{$zone}->{type} eq 'bport') {
358 my $bridge_zone = $zoneinfo->{$zone}->{bridge_zone} || die "internal error";
359 my $zoneref = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
360
361 # using 'all' does not work, so we create one rule for
362 # each related zone on the same bridge
363 push @$sources, "${zoneref}:$rule->{source}";
364 foreach my $z (keys %$zoneinfo) {
365 next if $z eq $zone;
366 next if !$zoneinfo->{$z}->{bridge_zone};
367 next if $zoneinfo->{$z}->{bridge_zone} ne $bridge_zone;
368 $zoneref = $zoneinfo->{$z}->{zoneref} || die "internal error";
369 push @$sources, "${zoneref}:$rule->{source}";
370 }
371 } else {
372 push @$sources, "all:$rule->{source}";
373 }
374
375 my $out = '';
376
377 foreach my $source (@$sources) {
378 $out .= sprintf($rule_format, $action, $source, $dest, $rule->{proto} || '-',
379 $rule->{dport} || '-', $rule->{sport} || '-');
380 }
381
382 return $out;
383 };
384
385 my $generate_output_rule = sub {
386 my ($zoneinfo, $rule, $net, $netid) = @_;
387
388 my $zone = $net->{zone} || die "internal error";
389 my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
390 my $tap = $net->{tap} || die "internal error";
391
392 my $action = $rule->{service} ?
393 "$rule->{service}($rule->{action})" : $rule->{action};
394
395 my $dest;
396
397 if (!$rule->{dest}) {
398 $dest = 'all';
399 } else {
400 $dest = "all:$rule->{dest}";
401 }
402
403 return sprintf($rule_format, $action, "$zid:$tap", $dest,
404 $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-');
405 };
406
407 # we need complete VM configuration of all VMs (openvz/qemu)
408 # in vmdata
409
410 my $compile_shorewall = sub {
411 my ($targetdir, $vmdata, $rules) = @_;
412
413 # remove existing data ?
414 foreach my $file (qw(params zones rules interfaces maclist policy)) {
415 unlink "$targetdir/$file";
416 }
417
418 my $netinfo;
419
420 my $zoneinfo = {
421 fw => { type => 'firewall' },
422 };
423
424 my $maclist = {};
425
426 my $register_bridge;
427
428 $register_bridge = sub {
429 my ($bridge, $vlan) = @_;
430
431 my $zone = $bridge;
432
433 return $zone if $zoneinfo->{$zone};
434
435 my $ext_zone = "${bridge}_ext";
436
437 $zoneinfo->{$zone} = {
438 type => 'bridge',
439 bridge => $bridge,
440 bridge_ext_zone => $ext_zone,
441 };
442
443 # physical input devices
444 my $dir = "/sys/class/net/$bridge/brif";
445 my $physical = {};
446 PVE::Tools::dir_glob_foreach($dir, '((eth|bond).+)', sub {
447 my ($slave) = @_;
448 $physical->{$slave} = 1;
449 });
450
451 $zoneinfo->{$ext_zone} = {
452 type => 'bport',
453 bridge_zone => $zone,
454 ifaces => $physical,
455 };
456
457 return &$register_bridge("${bridge}v${vlan}") if defined($vlan);
458
459 return $zone;
460 };
461
462 my $register_bridge_port = sub {
463 my ($bridge, $vlan, $vmzone, $tap) = @_;
464
465 my $bridge_zone = &$register_bridge($bridge, $vlan);
466 my $zone = $bridge_zone . '_' . $vmzone;
467
468 if (!$zoneinfo->{$zone}) {
469 $zoneinfo->{$zone} = {
470 type => 'bport',
471 bridge_zone => $bridge_zone,
472 ifaces => {},
473 };
474 }
475
476 $zoneinfo->{$zone}->{ifaces}->{$tap} = 1;
477
478 return $zone;
479 };
480
481 foreach my $vmid (keys %{$vmdata->{qemu}}) {
482 $netinfo->{$vmid} = {};
483 my $conf = $vmdata->{qemu}->{$vmid};
484 foreach my $opt (keys %$conf) {
485 next if $opt !~ m/^net(\d+)$/;
486 my $netnum = $1;
487 my $net = PVE::QemuServer::parse_net($conf->{$opt});
488 next if !$net;
489 die "implement me" if !$net->{bridge};
490
491 my $vmzone = $conf->{zone} || "vm$vmid";
492 $net->{tap} = "tap${vmid}i${netnum}";
493 $maclist->{$net->{tap}} = $net->{macaddr} || die "internal error";
494 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap});
495 $netinfo->{$vmid}->{$opt} = $net;
496 }
497 }
498
499 #print Dumper($netinfo);
500
501 # NOTE: zone names have length limit, so we need to
502 # translate them into shorter names
503
504 my $zoneid = 0;
505 my $zonemap = { fw => 'fw' };
506
507 my $lookup_zonename = sub {
508 my ($zone) = @_;
509
510 return $zonemap->{$zone} if defined($zonemap->{$zone});
511 $zonemap->{$zone} = 'z' . $zoneid++;
512
513 return $zonemap->{$zone};
514 };
515
516 foreach my $z (sort keys %$zoneinfo) {
517 $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
518 $zoneinfo->{$z}->{zonevar} = uc($z);
519 $zoneinfo->{$z}->{zoneref} = '$' . $zoneinfo->{$z}->{zonevar};
520 }
521
522 my $out;
523
524 # dump params file
525 $out = "# PVE zones\n";
526 foreach my $z (sort keys %$zoneinfo) {
527 $out .= "$zoneinfo->{$z}->{zonevar}=$zoneinfo->{$z}->{id}\n";
528 }
529 PVE::Tools::file_set_contents("$targetdir/params", $out);
530
531 # dump zone file
532
533 my $format = "%-30s %-10s %-15s\n";
534 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
535
536 foreach my $z (sort keys %$zoneinfo) {
537 my $zid = $zoneinfo->{$z}->{zoneref};
538 if ($zoneinfo->{$z}->{type} eq 'firewall') {
539 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '');
540 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
541 $out .= sprintf($format, $zid, 'ipv4', '');
542 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
543 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
544 my $bzid = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
545 $out .= sprintf($format, "$zid:$bzid", 'bport', '');
546 } else {
547 die "internal error";
548 }
549 }
550
551 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
552
553 PVE::Tools::file_set_contents("$targetdir/zones", $out);
554
555 # dump interfaces
556
557 $format = "%-25s %-20s %-10s %-15s\n";
558 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
559
560 my $maclist_format = "%-15s %-15s %-15s\n";
561 my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
562
563 foreach my $z (sort keys %$zoneinfo) {
564 my $zid = $zoneinfo->{$z}->{zoneref};
565 if ($zoneinfo->{$z}->{type} eq 'firewall') {
566 # do nothing;
567 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
568 my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
569 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional');
570 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
571 my $ifaces = $zoneinfo->{$z}->{ifaces};
572 foreach my $iface (sort keys %$ifaces) {
573 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
574 my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
575 my $iftxt = "$bridge:$iface";
576
577 if ($maclist->{$iface}) {
578 $out .= sprintf($format, $zid, $iftxt, '-', 'maclist');
579 $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
580 } else {
581 $out .= sprintf($format, $zid, $iftxt, '-', '');
582 }
583 }
584 } else {
585 die "internal error";
586 }
587 }
588
589 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
590
591 PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
592
593 # dump maclist
594 PVE::Tools::file_set_contents("$targetdir/maclist", $macs);
595
596 # dump policy
597
598 $format = "%-15s %-15s %-15s %s\n";
599 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
600 $out .= sprintf($format, 'fw', 'all', 'ACCEPT', '');
601
602 # we need to disable intra-zone traffic on bridges. Else traffic
603 # from untracked interfaces simply pass the firewall
604 foreach my $z (sort keys %$zoneinfo) {
605 my $zid = $zoneinfo->{$z}->{zoneref};
606 if ($zoneinfo->{$z}->{type} eq 'bridge') {
607 $out .= sprintf($format, $zid, $zid, 'REJECT', 'info');
608 }
609 }
610 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
611
612 PVE::Tools::file_set_contents("$targetdir/policy", $out);
613
614 # dump rules
615 $out = '';
616
617 $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
618 foreach my $vmid (sort keys %$rules) {
619 my $inrules = $rules->{$vmid}->{in};
620 my $outrules = $rules->{$vmid}->{out};
621
622 if (scalar(@$inrules)) {
623 $out .= "# IN to VM $vmid\n";
624 foreach my $rule (@$inrules) {
625 foreach my $netid (keys %{$netinfo->{$vmid}}) {
626 my $net = $netinfo->{$vmid}->{$netid};
627 next if $rule->{iface} && $rule->{iface} ne $netid;
628 $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
629 }
630 }
631 }
632
633 if (scalar(@$outrules)) {
634 $out .= "# OUT from VM $vmid\n";
635 foreach my $rule (@$outrules) {
636 foreach my $netid (keys %{$netinfo->{$vmid}}) {
637 my $net = $netinfo->{$vmid}->{$netid};
638 next if $rule->{iface} && $rule->{iface} ne $netid;
639 $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
640 }
641 }
642 }
643 }
644
645 PVE::Tools::file_set_contents("$targetdir/rules", $out);
646 };
647
648
649 sub parse_fw_rules {
650 my ($filename, $fh) = @_;
651
652 my $section;
653
654 my $res = { in => [], out => [] };
655
656 my $macros = get_shorewall_macros();
657 my $protocols = get_etc_protocols();
658
659 while (defined(my $line = <$fh>)) {
660 next if $line =~ m/^#/;
661 next if $line =~ m/^\s*$/;
662
663 if ($line =~ m/^\[(in|out)\]\s*$/i) {
664 $section = lc($1);
665 next;
666 }
667 next if !$section;
668
669 my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
670 split(/\s+/, $line);
671
672 if (!$action) {
673 warn "skip incomplete line\n";
674 next;
675 }
676
677 my $service;
678 if ($action =~ m/^(ACCEPT|DROP|REJECT|GROUP-(\S+))$/) {
679 # OK
680 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
681 ($service, $action) = ($1, $2);
682 if (!$macros->{$service}) {
683 warn "unknown service '$service'\n";
684 next;
685 }
686 } else {
687 warn "unknown action '$action'\n";
688 next;
689 }
690
691 $iface = undef if $iface && $iface eq '-';
692 if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) {
693 warn "unknown interface '$iface'\n";
694 next;
695 }
696
697 $proto = undef if $proto && $proto eq '-';
698 if ($proto && !(defined($protocols->{byname}->{$proto}) ||
699 defined($protocols->{byid}->{$proto}))) {
700 warn "unknown protokol '$proto'\n";
701 next;
702 }
703
704 $source = undef if $source && $source eq '-';
705 $dest = undef if $dest && $dest eq '-';
706
707 $dport = undef if $dport && $dport eq '-';
708 $sport = undef if $sport && $sport eq '-';
709
710 eval {
711 parse_address_list($source) if $source;
712 parse_address_list($dest) if $dest;
713 parse_port_name_number_or_range($dport) if $dport;
714 parse_port_name_number_or_range($sport) if $sport;
715 };
716 if (my $err = $@) {
717 warn $err;
718 next;
719
720 }
721
722
723 my $rule = {
724 action => $action,
725 service => $service,
726 iface => $iface,
727 source => $source,
728 dest => $dest,
729 proto => $proto,
730 dport => $dport,
731 sport => $sport,
732 };
733
734 push @{$res->{$section}}, $rule;
735 }
736
737 return $res;
738 }
739
740 sub read_local_vm_config {
741
742 my $openvz = {};
743
744 my $qemu = {};
745
746 my $list = PVE::QemuServer::config_list();
747
748 foreach my $vmid (keys %$list) {
749 #next if !($vmid eq '100' || $vmid eq '102');
750 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
751 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
752 $qemu->{$vmid} = $conf;
753 }
754 }
755
756 my $vmdata = { openvz => $openvz, qemu => $qemu };
757
758 return $vmdata;
759 };
760
761 sub read_vm_firewall_rules {
762 my ($vmdata) = @_;
763 my $rules = {};
764 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
765 my $filename = "/etc/pve/firewall/$vmid.fw";
766 my $fh = IO::File->new($filename, O_RDONLY);
767 next if !$fh;
768
769 $rules->{$vmid} = parse_fw_rules($filename, $fh);
770 }
771
772 return $rules;
773 }
774
775 sub compile {
776
777 my $vmdata = read_local_vm_config();
778 my $rules = read_vm_firewall_rules($vmdata);
779
780 # print Dumper($vmdata);
781
782 my $swdir = '/etc/shorewall';
783 mkdir $swdir;
784
785 &$compile_shorewall($swdir, $vmdata, $rules);
786
787 PVE::Tools::run_command(['shorewall', 'compile']);
788 }
789
790 sub compile_and_start {
791 my ($restart) = @_;
792
793 compile();
794
795 PVE::Tools::run_command(['shorewall', $restart ? 'restart' : 'start']);
796 }
797
798
799 1;