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