describe the problem
[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;
10use Data::Dumper;
b6360c3f 11
9aab3127
DM
12my $macros;
13sub get_shorewall_macros {
14
15 return $macros if $macros;
16
17 foreach my $path (</usr/share/shorewall/macro.*>) {
18 if ($path =~ m|/macro\.(\S+)$|) {
19 $macros->{$1} = 1;
20 }
21 }
22 return $macros;
23}
24
dddd9413 25
8cebfa6f 26my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
dddd9413
DM
27
28my $generate_input_rule = sub {
29 my ($zoneinfo, $rule, $net, $netid) = @_;
30
dddd9413 31 my $zone = $net->{zone} || die "internal error";
a8195f4f 32 my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
dddd9413 33 my $tap = $net->{tap} || die "internal error";
9aab3127 34
8cebfa6f
DM
35 my $dest = "$zid:$tap";
36
37 if ($rule->{dest}) {
38 $dest .= ":$rule->{dest}";
39 }
40
9aab3127
DM
41 my $action = $rule->{service} ?
42 "$rule->{service}($rule->{action})" : $rule->{action};
43
8cebfa6f
DM
44 my $source;
45
46 if ($zoneinfo->{$zone}->{type} eq 'bport') {
47 my $bridge_zone = $zoneinfo->{$zone}->{bridge_zone} || die "internal error";
48 my $bridge_ext_zone = $zoneinfo->{$bridge_zone}->{bridge_ext_zone} || die "internal error";
49 my $zoneref = $zoneinfo->{$bridge_ext_zone}->{zoneref} || die "internal error";
50 if (!$rule->{source}) {
8fb53d8c
DM
51 # $source = "${zoneref}";
52 $source = 'all';
8cebfa6f 53 } else {
8fb53d8c 54 # 'all' does not work
8cebfa6f
DM
55 $source = "${zoneref}:$rule->{source}";
56 }
57 } else {
9a4644fa 58 $source = "all:$rule->{source}";
8cebfa6f
DM
59 }
60
61 return sprintf($rule_format, $action, $source, $dest, $rule->{proto} || '-',
62 $rule->{dport} || '-', $rule->{sport} || '-');
dddd9413
DM
63};
64
65my $generate_output_rule = sub {
66 my ($zoneinfo, $rule, $net, $netid) = @_;
67
dddd9413 68 my $zone = $net->{zone} || die "internal error";
a8195f4f 69 my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
dddd9413 70 my $tap = $net->{tap} || die "internal error";
9aab3127
DM
71
72 my $action = $rule->{service} ?
73 "$rule->{service}($rule->{action})" : $rule->{action};
dddd9413 74
8cebfa6f
DM
75 my $dest;
76
77 if (!$rule->{dest}) {
9a4644fa 78 $dest = 'all';
8cebfa6f 79 } else {
9a4644fa 80 $dest = "all:$rule->{dest}";
8cebfa6f
DM
81 }
82
83 return sprintf($rule_format, $action, "$zid:$tap", $dest,
dddd9413
DM
84 $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-');
85};
86
b6360c3f
DM
87# we need complete VM configuration of all VMs (openvz/qemu)
88# in vmdata
89
5e1267a5 90my $compile_shorewall = sub {
dddd9413 91 my ($targetdir, $vmdata, $rules) = @_;
b6360c3f 92
80bfe1ff 93 # remove existing data ?
a8195f4f 94 foreach my $file (qw(params zones rules interfaces maclist policy)) {
80bfe1ff
DM
95 unlink "$targetdir/$file";
96 }
97
b6360c3f
DM
98 my $netinfo;
99
886aba9c
DM
100 my $zoneinfo = {
101 fw => { type => 'firewall' },
102 };
103
026a6466
DM
104 my $maclist = {};
105
35081236
DM
106 my $register_bridge;
107
108 $register_bridge = sub {
109 my ($bridge, $vlan) = @_;
f789653a
DM
110
111 my $zone = 'z' . $bridge;
112
113 return $zone if $zoneinfo->{$zone};
114
8cebfa6f
DM
115 my $ext_zone = "z${bridge}ext";
116
f789653a
DM
117 $zoneinfo->{$zone} = {
118 type => 'bridge',
119 bridge => $bridge,
8cebfa6f
DM
120 bridge_ext_zone => $ext_zone,
121 };
122
123 # physical input devices
124 my $dir = "/sys/class/net/$bridge/brif";
125 my $physical = {};
126 PVE::Tools::dir_glob_foreach($dir, '((eth|bond).+)', sub {
127 my ($slave) = @_;
128 $physical->{$slave} = 1;
129 });
130
131 $zoneinfo->{$ext_zone} = {
132 type => 'bport',
133 bridge_zone => $zone,
134 ifaces => $physical,
f789653a 135 };
35081236
DM
136
137 return &$register_bridge("${bridge}v${vlan}") if defined($vlan);
138
139 return $zone;
140 };
141
142 my $register_bridge_port = sub {
143 my ($bridge, $vlan, $vmzone, $tap) = @_;
144
145 my $bridge_zone = &$register_bridge($bridge, $vlan);
146 my $zone = $bridge_zone . $vmzone;
147
148 if (!$zoneinfo->{$zone}) {
149 $zoneinfo->{$zone} = {
150 type => 'bport',
151 bridge_zone => $bridge_zone,
152 ifaces => {},
153 };
154 }
155
156 $zoneinfo->{$zone}->{ifaces}->{$tap} = 1;
f789653a
DM
157
158 return $zone;
159 };
160
b6360c3f
DM
161 foreach my $vmid (keys %{$vmdata->{qemu}}) {
162 $netinfo->{$vmid} = {};
163 my $conf = $vmdata->{qemu}->{$vmid};
164 foreach my $opt (keys %$conf) {
165 next if $opt !~ m/^net(\d+)$/;
dddd9413 166 my $netnum = $1;
b6360c3f
DM
167 my $net = PVE::QemuServer::parse_net($conf->{$opt});
168 next if !$net;
886aba9c 169 die "implement me" if !$net->{bridge};
886aba9c 170
f789653a 171 my $vmzone = $conf->{zone} || "vm$vmid";
dddd9413 172 $net->{tap} = "tap${vmid}i${netnum}";
026a6466 173 $maclist->{$net->{tap}} = $net->{macaddr} || die "internal error";
dddd9413
DM
174 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap});
175 $netinfo->{$vmid}->{$opt} = $net;
886aba9c
DM
176 }
177 }
178
179 #print Dumper($netinfo);
180
f789653a 181 # NOTE: zone names have length limit, so we need to
886aba9c
DM
182 # translate them into shorter names
183
f789653a
DM
184 my $zoneid = 0;
185 my $zonemap = { fw => 'fw' };
186
187 my $lookup_zonename = sub {
188 my ($zone) = @_;
189
190 return $zonemap->{$zone} if defined($zonemap->{$zone});
191 $zonemap->{$zone} = 'z' . $zoneid++;
192
193 return $zonemap->{$zone};
194 };
195
dddd9413
DM
196 foreach my $z (sort keys %$zoneinfo) {
197 $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
a8195f4f
DM
198 $zoneinfo->{$z}->{zonevar} = uc($z);
199 $zoneinfo->{$z}->{zoneref} = '$' . $zoneinfo->{$z}->{zonevar};
dddd9413
DM
200 }
201
f789653a 202 my $out;
886aba9c 203
a8195f4f
DM
204 # dump params file
205 $out = "# PVE zones\n";
206 foreach my $z (sort keys %$zoneinfo) {
207 $out .= "$zoneinfo->{$z}->{zonevar}=$zoneinfo->{$z}->{id}\n";
208 }
209 PVE::Tools::file_set_contents("$targetdir/params", $out);
210
211 # dump zone file
212
213 my $format = "%-30s %-10s %-15s\n";
214 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
f789653a 215
886aba9c 216 foreach my $z (sort keys %$zoneinfo) {
a8195f4f 217 my $zid = $zoneinfo->{$z}->{zoneref};
f789653a 218 if ($zoneinfo->{$z}->{type} eq 'firewall') {
a8195f4f 219 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '');
f789653a 220 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
a8195f4f 221 $out .= sprintf($format, $zid, 'ipv4', '');
f789653a
DM
222 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
223 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
a8195f4f
DM
224 my $bzid = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
225 $out .= sprintf($format, "$zid:$bzid", 'bport', '');
f789653a
DM
226 } else {
227 die "internal error";
228 }
886aba9c
DM
229 }
230
f789653a
DM
231 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
232
233 PVE::Tools::file_set_contents("$targetdir/zones", $out);
886aba9c 234
f789653a 235 # dump interfaces
886aba9c 236
a8195f4f
DM
237 $format = "%-25s %-20s %-10s %-15s\n";
238 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
f789653a 239
026a6466
DM
240 my $maclist_format = "%-15s %-15s %-15s\n";
241 my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
242
886aba9c 243 foreach my $z (sort keys %$zoneinfo) {
a8195f4f 244 my $zid = $zoneinfo->{$z}->{zoneref};
f789653a
DM
245 if ($zoneinfo->{$z}->{type} eq 'firewall') {
246 # do nothing;
247 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
248 my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
a8195f4f 249 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional');
f789653a
DM
250 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
251 my $ifaces = $zoneinfo->{$z}->{ifaces};
252 foreach my $iface (sort keys %$ifaces) {
253 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
254 my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
255 my $iftxt = "$bridge:$iface";
8cebfa6f
DM
256
257 if ($maclist->{$iface}) {
258 $out .= sprintf($format, $zid, $iftxt, '-', 'maclist');
259 $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
260 } else {
261 $out .= sprintf($format, $zid, $iftxt, '-', '');
262 }
f789653a
DM
263 }
264 } else {
265 die "internal error";
b6360c3f
DM
266 }
267 }
268
f789653a
DM
269 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
270
271 PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
272
026a6466
DM
273 # dump maclist
274 PVE::Tools::file_set_contents("$targetdir/maclist", $macs);
275
f789653a
DM
276 # dump policy
277
278 $format = "%-15s %-15s %-15s %s\n";
279 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
8cebfa6f
DM
280 $out .= sprintf($format, 'fw', 'all', 'ACCEPT', '');
281
282 # we need to disable intra-zone traffic on bridges. Else traffic
283 # from untracked interfaces simply pass the firewall
284 foreach my $z (sort keys %$zoneinfo) {
285 my $zid = $zoneinfo->{$z}->{zoneref};
286 if ($zoneinfo->{$z}->{type} eq 'bridge') {
287 $out .= sprintf($format, $zid, $zid, 'REJECT', 'info');
288 }
289 }
f789653a 290 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
886aba9c 291
f789653a 292 PVE::Tools::file_set_contents("$targetdir/policy", $out);
886aba9c 293
dddd9413
DM
294 # dump rules
295 $out = '';
296
297 $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
298 foreach my $vmid (sort keys %$rules) {
78db5872
DM
299 my $inrules = $rules->{$vmid}->{in};
300 my $outrules = $rules->{$vmid}->{out};
301
302 if (scalar(@$inrules)) {
303 $out .= "# IN to VM $vmid\n";
dddd9413
DM
304 foreach my $rule (@$inrules) {
305 foreach my $netid (keys %{$netinfo->{$vmid}}) {
306 my $net = $netinfo->{$vmid}->{$netid};
9a4644fa 307 next if $rule->{iface} && $rule->{iface} ne $netid;
dddd9413
DM
308 $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
309 }
310 }
311 }
312
78db5872
DM
313 if (scalar(@$outrules)) {
314 $out .= "# OUT from VM $vmid\n";
dddd9413
DM
315 foreach my $rule (@$outrules) {
316 foreach my $netid (keys %{$netinfo->{$vmid}}) {
317 my $net = $netinfo->{$vmid}->{$netid};
9a4644fa 318 next if $rule->{iface} && $rule->{iface} ne $netid;
dddd9413
DM
319 $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
320 }
321 }
322 }
323 }
324
325 PVE::Tools::file_set_contents("$targetdir/rules", $out);
5e1267a5
DM
326};
327
328
329sub parse_fw_rules {
330 my ($filename, $fh) = @_;
331
332 my $section;
333
334 my $res = { in => [], out => [] };
335
336 my $macros = PVE::Firewall::get_shorewall_macros();
337
338 while (defined(my $line = <$fh>)) {
339 next if $line =~ m/^#/;
340 next if $line =~ m/^\s*$/;
341
342 if ($line =~ m/^\[(in|out)\]\s*$/i) {
343 $section = lc($1);
344 next;
345 }
346 next if !$section;
347
348 my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
349 split(/\s+/, $line);
350
351 if (!$action) {
352 warn "skip incomplete line\n";
353 next;
354 }
355
356 my $service;
357 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
358 # OK
359 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
360 ($service, $action) = ($1, $2);
361 if (!$macros->{$service}) {
362 warn "unknown service '$service'\n";
363 next;
364 }
365 } else {
366 warn "unknown action '$action'\n";
367 next;
368 }
369
370 $iface = undef if $iface && $iface eq '-';
371 if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) {
372 warn "unknown interface '$iface'\n";
373 next;
374 }
375
376 $proto = undef if $proto && $proto eq '-';
377 if ($proto && $proto !~ m/^(icmp|tcp|udp)$/) {
378 warn "unknown protokol '$proto'\n";
379 next;
380 }
381
382 $source = undef if $source && $source eq '-';
383
384# if ($source !~ m/^(XYZ)$/) {
385# warn "unknown source '$source'\n";
386# next;
387# }
388
389 $dest = undef if $dest && $dest eq '-';
390# if ($dest !~ m/^XYZ)$/) {
391# warn "unknown destination '$dest'\n";
392# next;
393# }
394
395 $dport = undef if $dport && $dport eq '-';
396 $sport = undef if $sport && $sport eq '-';
397
398 my $rule = {
399 action => $action,
400 service => $service,
401 iface => $iface,
402 source => $source,
403 dest => $dest,
404 proto => $proto,
405 dport => $dport,
406 sport => $sport,
407 };
408
409 push @{$res->{$section}}, $rule;
410 }
411
412 return $res;
413}
414
415sub read_local_vm_config {
416
417 my $openvz = {};
418
419 my $qemu = {};
420
421 my $list = PVE::QemuServer::config_list();
422
423 foreach my $vmid (keys %$list) {
424 # next if $vmid ne '100';
425 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
426 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
427 $qemu->{$vmid} = $conf;
428 }
429 }
430
431 my $vmdata = { openvz => $openvz, qemu => $qemu };
432
433 return $vmdata;
434};
435
436sub read_vm_firewall_rules {
437 my ($vmdata) = @_;
438 my $rules = {};
439 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
440 my $filename = "/etc/pve/firewall/$vmid.fw";
441 my $fh = IO::File->new($filename, O_RDONLY);
442 next if !$fh;
443
444 $rules->{$vmid} = parse_fw_rules($filename, $fh);
445 }
446
447 return $rules;
448}
449
450sub compile {
451
452 my $vmdata = read_local_vm_config();
453 my $rules = read_vm_firewall_rules($vmdata);
454
455 # print Dumper($vmdata);
456
457 my $swdir = '/etc/shorewall';
458 mkdir $swdir;
459
460 &$compile_shorewall($swdir, $vmdata, $rules);
b6360c3f 461
5e1267a5 462 PVE::Tools::run_command(['shorewall', 'compile']);
b6360c3f
DM
463}
464
5e1267a5
DM
465sub compile_and_start {
466 my ($restart) = @_;
886aba9c 467
5e1267a5 468 compile();
b6360c3f 469
5e1267a5 470 PVE::Tools::run_command(['shorewall', $restart ? 'restart' : 'start']);
b6360c3f
DM
471}
472
473
4741;