]>
Commit | Line | Data |
---|---|---|
b6360c3f DM |
1 | package PVE::Firewall; |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | use Data::Dumper; | |
f789653a | 6 | use PVE::Tools; |
b6360c3f | 7 | use PVE::QemuServer; |
5e1267a5 DM |
8 | use File::Path; |
9 | use IO::File; | |
ecbea048 | 10 | use Net::IP; |
3a616aa0 | 11 | use PVE::Tools qw(run_command); |
ecbea048 | 12 | |
5e1267a5 | 13 | use Data::Dumper; |
b6360c3f | 14 | |
9aab3127 | 15 | my $macros; |
3a616aa0 AD |
16 | my @ruleset = (); |
17 | ||
9aab3127 DM |
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 | ||
fcba0beb DM |
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 | ||
3a616aa0 | 62 | |
fcba0beb DM |
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 | ||
ecbea048 DM |
99 | sub parse_address_list { |
100 | my ($str) = @_; | |
101 | ||
d6de1dc2 | 102 | my $nbaor = 0; |
ecbea048 DM |
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"; | |
d6de1dc2 AD |
107 | }else{ |
108 | $nbaor++; | |
ecbea048 DM |
109 | } |
110 | } | |
d6de1dc2 | 111 | return $nbaor; |
ecbea048 | 112 | } |
dddd9413 | 113 | |
fcba0beb DM |
114 | sub parse_port_name_number_or_range { |
115 | my ($str) = @_; | |
116 | ||
117 | my $services = PVE::Firewall::get_etc_services(); | |
4cdbb3b7 | 118 | my $nbports = 0; |
fcba0beb | 119 | foreach my $item (split(/,/, $str)) { |
4cdbb3b7 | 120 | my $portlist = ""; |
fcba0beb | 121 | foreach my $pon (split(':', $item, 2)) { |
4cdbb3b7 AD |
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++; | |
fcba0beb DM |
128 | } |
129 | } | |
130 | ||
4cdbb3b7 | 131 | return ($nbports); |
fcba0beb DM |
132 | } |
133 | ||
8cebfa6f | 134 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; |
dddd9413 | 135 | |
3a616aa0 AD |
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 | ||
d6de1dc2 | 185 | $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1; |
3a616aa0 | 186 | $cmd .= " -s $rule->{source}" if $rule->{source}; |
d6de1dc2 | 187 | $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1; |
3a616aa0 AD |
188 | $cmd .= " -d $rule->{dest}" if $rule->{destination}; |
189 | $cmd .= " -p $rule->{proto}" if $rule->{proto}; | |
4cdbb3b7 | 190 | $cmd .= " --match multiport" if $rule->{nbdport} && $rule->{nbdport} > 1; |
3a616aa0 | 191 | $cmd .= " --dport $rule->{dport}" if $rule->{dport}; |
4cdbb3b7 | 192 | $cmd .= " --match multiport" if $rule->{nbsport} && $rule->{nbsport} > 1; |
3a616aa0 AD |
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 | ||
dddd9413 DM |
350 | my $generate_input_rule = sub { |
351 | my ($zoneinfo, $rule, $net, $netid) = @_; | |
352 | ||
dddd9413 | 353 | my $zone = $net->{zone} || die "internal error"; |
a8195f4f | 354 | my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error"; |
dddd9413 | 355 | my $tap = $net->{tap} || die "internal error"; |
9aab3127 | 356 | |
8cebfa6f DM |
357 | my $dest = "$zid:$tap"; |
358 | ||
359 | if ($rule->{dest}) { | |
360 | $dest .= ":$rule->{dest}"; | |
361 | } | |
362 | ||
9aab3127 DM |
363 | my $action = $rule->{service} ? |
364 | "$rule->{service}($rule->{action})" : $rule->{action}; | |
365 | ||
b9b06789 | 366 | my $sources = []; |
8cebfa6f | 367 | |
b9b06789 DM |
368 | if (!$rule->{source}) { |
369 | push @$sources, 'all'; | |
370 | } elsif ($zoneinfo->{$zone}->{type} eq 'bport') { | |
8cebfa6f | 371 | my $bridge_zone = $zoneinfo->{$zone}->{bridge_zone} || die "internal error"; |
b9b06789 DM |
372 | my $zoneref = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error"; |
373 | ||
374 | # using 'all' does not work, so we create one rule for | |
375 | # each related zone on the same bridge | |
376 | push @$sources, "${zoneref}:$rule->{source}"; | |
377 | foreach my $z (keys %$zoneinfo) { | |
378 | next if $z eq $zone; | |
379 | next if !$zoneinfo->{$z}->{bridge_zone}; | |
380 | next if $zoneinfo->{$z}->{bridge_zone} ne $bridge_zone; | |
381 | $zoneref = $zoneinfo->{$z}->{zoneref} || die "internal error"; | |
382 | push @$sources, "${zoneref}:$rule->{source}"; | |
8cebfa6f DM |
383 | } |
384 | } else { | |
b9b06789 DM |
385 | push @$sources, "all:$rule->{source}"; |
386 | } | |
387 | ||
388 | my $out = ''; | |
389 | ||
390 | foreach my $source (@$sources) { | |
391 | $out .= sprintf($rule_format, $action, $source, $dest, $rule->{proto} || '-', | |
392 | $rule->{dport} || '-', $rule->{sport} || '-'); | |
8cebfa6f DM |
393 | } |
394 | ||
b9b06789 | 395 | return $out; |
dddd9413 DM |
396 | }; |
397 | ||
398 | my $generate_output_rule = sub { | |
399 | my ($zoneinfo, $rule, $net, $netid) = @_; | |
400 | ||
dddd9413 | 401 | my $zone = $net->{zone} || die "internal error"; |
a8195f4f | 402 | my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error"; |
dddd9413 | 403 | my $tap = $net->{tap} || die "internal error"; |
9aab3127 DM |
404 | |
405 | my $action = $rule->{service} ? | |
406 | "$rule->{service}($rule->{action})" : $rule->{action}; | |
dddd9413 | 407 | |
8cebfa6f DM |
408 | my $dest; |
409 | ||
410 | if (!$rule->{dest}) { | |
9a4644fa | 411 | $dest = 'all'; |
8cebfa6f | 412 | } else { |
9a4644fa | 413 | $dest = "all:$rule->{dest}"; |
8cebfa6f DM |
414 | } |
415 | ||
416 | return sprintf($rule_format, $action, "$zid:$tap", $dest, | |
dddd9413 DM |
417 | $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-'); |
418 | }; | |
419 | ||
b6360c3f DM |
420 | # we need complete VM configuration of all VMs (openvz/qemu) |
421 | # in vmdata | |
422 | ||
5e1267a5 | 423 | my $compile_shorewall = sub { |
dddd9413 | 424 | my ($targetdir, $vmdata, $rules) = @_; |
b6360c3f | 425 | |
80bfe1ff | 426 | # remove existing data ? |
a8195f4f | 427 | foreach my $file (qw(params zones rules interfaces maclist policy)) { |
80bfe1ff DM |
428 | unlink "$targetdir/$file"; |
429 | } | |
430 | ||
b6360c3f DM |
431 | my $netinfo; |
432 | ||
886aba9c DM |
433 | my $zoneinfo = { |
434 | fw => { type => 'firewall' }, | |
435 | }; | |
436 | ||
026a6466 DM |
437 | my $maclist = {}; |
438 | ||
35081236 DM |
439 | my $register_bridge; |
440 | ||
441 | $register_bridge = sub { | |
442 | my ($bridge, $vlan) = @_; | |
f789653a | 443 | |
1b6a0a59 | 444 | my $zone = $bridge; |
f789653a DM |
445 | |
446 | return $zone if $zoneinfo->{$zone}; | |
447 | ||
1b6a0a59 | 448 | my $ext_zone = "${bridge}_ext"; |
8cebfa6f | 449 | |
f789653a DM |
450 | $zoneinfo->{$zone} = { |
451 | type => 'bridge', | |
452 | bridge => $bridge, | |
8cebfa6f DM |
453 | bridge_ext_zone => $ext_zone, |
454 | }; | |
455 | ||
456 | # physical input devices | |
457 | my $dir = "/sys/class/net/$bridge/brif"; | |
458 | my $physical = {}; | |
459 | PVE::Tools::dir_glob_foreach($dir, '((eth|bond).+)', sub { | |
460 | my ($slave) = @_; | |
461 | $physical->{$slave} = 1; | |
462 | }); | |
463 | ||
464 | $zoneinfo->{$ext_zone} = { | |
465 | type => 'bport', | |
466 | bridge_zone => $zone, | |
467 | ifaces => $physical, | |
f789653a | 468 | }; |
35081236 DM |
469 | |
470 | return &$register_bridge("${bridge}v${vlan}") if defined($vlan); | |
471 | ||
472 | return $zone; | |
473 | }; | |
474 | ||
475 | my $register_bridge_port = sub { | |
476 | my ($bridge, $vlan, $vmzone, $tap) = @_; | |
477 | ||
478 | my $bridge_zone = &$register_bridge($bridge, $vlan); | |
1b6a0a59 | 479 | my $zone = $bridge_zone . '_' . $vmzone; |
35081236 DM |
480 | |
481 | if (!$zoneinfo->{$zone}) { | |
482 | $zoneinfo->{$zone} = { | |
483 | type => 'bport', | |
484 | bridge_zone => $bridge_zone, | |
485 | ifaces => {}, | |
486 | }; | |
487 | } | |
488 | ||
489 | $zoneinfo->{$zone}->{ifaces}->{$tap} = 1; | |
f789653a DM |
490 | |
491 | return $zone; | |
492 | }; | |
493 | ||
b6360c3f DM |
494 | foreach my $vmid (keys %{$vmdata->{qemu}}) { |
495 | $netinfo->{$vmid} = {}; | |
496 | my $conf = $vmdata->{qemu}->{$vmid}; | |
497 | foreach my $opt (keys %$conf) { | |
498 | next if $opt !~ m/^net(\d+)$/; | |
dddd9413 | 499 | my $netnum = $1; |
b6360c3f DM |
500 | my $net = PVE::QemuServer::parse_net($conf->{$opt}); |
501 | next if !$net; | |
886aba9c | 502 | die "implement me" if !$net->{bridge}; |
886aba9c | 503 | |
f789653a | 504 | my $vmzone = $conf->{zone} || "vm$vmid"; |
dddd9413 | 505 | $net->{tap} = "tap${vmid}i${netnum}"; |
026a6466 | 506 | $maclist->{$net->{tap}} = $net->{macaddr} || die "internal error"; |
dddd9413 DM |
507 | $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap}); |
508 | $netinfo->{$vmid}->{$opt} = $net; | |
886aba9c DM |
509 | } |
510 | } | |
511 | ||
512 | #print Dumper($netinfo); | |
513 | ||
f789653a | 514 | # NOTE: zone names have length limit, so we need to |
886aba9c DM |
515 | # translate them into shorter names |
516 | ||
f789653a DM |
517 | my $zoneid = 0; |
518 | my $zonemap = { fw => 'fw' }; | |
519 | ||
520 | my $lookup_zonename = sub { | |
521 | my ($zone) = @_; | |
522 | ||
523 | return $zonemap->{$zone} if defined($zonemap->{$zone}); | |
524 | $zonemap->{$zone} = 'z' . $zoneid++; | |
525 | ||
526 | return $zonemap->{$zone}; | |
527 | }; | |
528 | ||
dddd9413 DM |
529 | foreach my $z (sort keys %$zoneinfo) { |
530 | $zoneinfo->{$z}->{id} = &$lookup_zonename($z); | |
a8195f4f DM |
531 | $zoneinfo->{$z}->{zonevar} = uc($z); |
532 | $zoneinfo->{$z}->{zoneref} = '$' . $zoneinfo->{$z}->{zonevar}; | |
dddd9413 DM |
533 | } |
534 | ||
f789653a | 535 | my $out; |
886aba9c | 536 | |
a8195f4f DM |
537 | # dump params file |
538 | $out = "# PVE zones\n"; | |
539 | foreach my $z (sort keys %$zoneinfo) { | |
540 | $out .= "$zoneinfo->{$z}->{zonevar}=$zoneinfo->{$z}->{id}\n"; | |
541 | } | |
542 | PVE::Tools::file_set_contents("$targetdir/params", $out); | |
543 | ||
544 | # dump zone file | |
545 | ||
546 | my $format = "%-30s %-10s %-15s\n"; | |
547 | $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS'); | |
f789653a | 548 | |
886aba9c | 549 | foreach my $z (sort keys %$zoneinfo) { |
a8195f4f | 550 | my $zid = $zoneinfo->{$z}->{zoneref}; |
f789653a | 551 | if ($zoneinfo->{$z}->{type} eq 'firewall') { |
a8195f4f | 552 | $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, ''); |
f789653a | 553 | } elsif ($zoneinfo->{$z}->{type} eq 'bridge') { |
a8195f4f | 554 | $out .= sprintf($format, $zid, 'ipv4', ''); |
f789653a DM |
555 | } elsif ($zoneinfo->{$z}->{type} eq 'bport') { |
556 | my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error"; | |
a8195f4f DM |
557 | my $bzid = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error"; |
558 | $out .= sprintf($format, "$zid:$bzid", 'bport', ''); | |
f789653a DM |
559 | } else { |
560 | die "internal error"; | |
561 | } | |
886aba9c DM |
562 | } |
563 | ||
f789653a DM |
564 | $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n"); |
565 | ||
566 | PVE::Tools::file_set_contents("$targetdir/zones", $out); | |
886aba9c | 567 | |
f789653a | 568 | # dump interfaces |
886aba9c | 569 | |
a8195f4f DM |
570 | $format = "%-25s %-20s %-10s %-15s\n"; |
571 | $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS'); | |
f789653a | 572 | |
026a6466 DM |
573 | my $maclist_format = "%-15s %-15s %-15s\n"; |
574 | my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE'); | |
575 | ||
886aba9c | 576 | foreach my $z (sort keys %$zoneinfo) { |
a8195f4f | 577 | my $zid = $zoneinfo->{$z}->{zoneref}; |
f789653a DM |
578 | if ($zoneinfo->{$z}->{type} eq 'firewall') { |
579 | # do nothing; | |
580 | } elsif ($zoneinfo->{$z}->{type} eq 'bridge') { | |
581 | my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error"; | |
a8195f4f | 582 | $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional'); |
f789653a DM |
583 | } elsif ($zoneinfo->{$z}->{type} eq 'bport') { |
584 | my $ifaces = $zoneinfo->{$z}->{ifaces}; | |
585 | foreach my $iface (sort keys %$ifaces) { | |
586 | my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error"; | |
587 | my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error"; | |
588 | my $iftxt = "$bridge:$iface"; | |
8cebfa6f DM |
589 | |
590 | if ($maclist->{$iface}) { | |
591 | $out .= sprintf($format, $zid, $iftxt, '-', 'maclist'); | |
592 | $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface}); | |
593 | } else { | |
594 | $out .= sprintf($format, $zid, $iftxt, '-', ''); | |
595 | } | |
f789653a DM |
596 | } |
597 | } else { | |
598 | die "internal error"; | |
b6360c3f DM |
599 | } |
600 | } | |
601 | ||
f789653a DM |
602 | $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n"); |
603 | ||
604 | PVE::Tools::file_set_contents("$targetdir/interfaces", $out); | |
605 | ||
026a6466 DM |
606 | # dump maclist |
607 | PVE::Tools::file_set_contents("$targetdir/maclist", $macs); | |
608 | ||
f789653a DM |
609 | # dump policy |
610 | ||
611 | $format = "%-15s %-15s %-15s %s\n"; | |
612 | $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG'); | |
8cebfa6f DM |
613 | $out .= sprintf($format, 'fw', 'all', 'ACCEPT', ''); |
614 | ||
615 | # we need to disable intra-zone traffic on bridges. Else traffic | |
616 | # from untracked interfaces simply pass the firewall | |
617 | foreach my $z (sort keys %$zoneinfo) { | |
618 | my $zid = $zoneinfo->{$z}->{zoneref}; | |
619 | if ($zoneinfo->{$z}->{type} eq 'bridge') { | |
620 | $out .= sprintf($format, $zid, $zid, 'REJECT', 'info'); | |
621 | } | |
622 | } | |
f789653a | 623 | $out .= sprintf($format, 'all', 'all', 'REJECT', 'info'); |
886aba9c | 624 | |
f789653a | 625 | PVE::Tools::file_set_contents("$targetdir/policy", $out); |
886aba9c | 626 | |
dddd9413 DM |
627 | # dump rules |
628 | $out = ''; | |
629 | ||
630 | $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT'); | |
631 | foreach my $vmid (sort keys %$rules) { | |
78db5872 DM |
632 | my $inrules = $rules->{$vmid}->{in}; |
633 | my $outrules = $rules->{$vmid}->{out}; | |
634 | ||
635 | if (scalar(@$inrules)) { | |
636 | $out .= "# IN to VM $vmid\n"; | |
dddd9413 DM |
637 | foreach my $rule (@$inrules) { |
638 | foreach my $netid (keys %{$netinfo->{$vmid}}) { | |
639 | my $net = $netinfo->{$vmid}->{$netid}; | |
9a4644fa | 640 | next if $rule->{iface} && $rule->{iface} ne $netid; |
dddd9413 DM |
641 | $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid); |
642 | } | |
643 | } | |
644 | } | |
645 | ||
78db5872 DM |
646 | if (scalar(@$outrules)) { |
647 | $out .= "# OUT from VM $vmid\n"; | |
dddd9413 DM |
648 | foreach my $rule (@$outrules) { |
649 | foreach my $netid (keys %{$netinfo->{$vmid}}) { | |
650 | my $net = $netinfo->{$vmid}->{$netid}; | |
9a4644fa | 651 | next if $rule->{iface} && $rule->{iface} ne $netid; |
dddd9413 DM |
652 | $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid); |
653 | } | |
654 | } | |
655 | } | |
656 | } | |
657 | ||
658 | PVE::Tools::file_set_contents("$targetdir/rules", $out); | |
5e1267a5 DM |
659 | }; |
660 | ||
661 | ||
662 | sub parse_fw_rules { | |
663 | my ($filename, $fh) = @_; | |
664 | ||
665 | my $section; | |
666 | ||
667 | my $res = { in => [], out => [] }; | |
668 | ||
fcba0beb DM |
669 | my $macros = get_shorewall_macros(); |
670 | my $protocols = get_etc_protocols(); | |
671 | ||
5e1267a5 DM |
672 | while (defined(my $line = <$fh>)) { |
673 | next if $line =~ m/^#/; | |
674 | next if $line =~ m/^\s*$/; | |
675 | ||
676 | if ($line =~ m/^\[(in|out)\]\s*$/i) { | |
677 | $section = lc($1); | |
678 | next; | |
679 | } | |
680 | next if !$section; | |
681 | ||
682 | my ($action, $iface, $source, $dest, $proto, $dport, $sport) = | |
683 | split(/\s+/, $line); | |
684 | ||
685 | if (!$action) { | |
686 | warn "skip incomplete line\n"; | |
687 | next; | |
688 | } | |
689 | ||
690 | my $service; | |
3a616aa0 | 691 | if ($action =~ m/^(ACCEPT|DROP|REJECT|GROUP-(\S+))$/) { |
5e1267a5 DM |
692 | # OK |
693 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
694 | ($service, $action) = ($1, $2); | |
695 | if (!$macros->{$service}) { | |
696 | warn "unknown service '$service'\n"; | |
697 | next; | |
698 | } | |
699 | } else { | |
700 | warn "unknown action '$action'\n"; | |
701 | next; | |
702 | } | |
703 | ||
704 | $iface = undef if $iface && $iface eq '-'; | |
705 | if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) { | |
706 | warn "unknown interface '$iface'\n"; | |
707 | next; | |
708 | } | |
709 | ||
710 | $proto = undef if $proto && $proto eq '-'; | |
fcba0beb DM |
711 | if ($proto && !(defined($protocols->{byname}->{$proto}) || |
712 | defined($protocols->{byid}->{$proto}))) { | |
5e1267a5 DM |
713 | warn "unknown protokol '$proto'\n"; |
714 | next; | |
715 | } | |
716 | ||
717 | $source = undef if $source && $source eq '-'; | |
5e1267a5 | 718 | $dest = undef if $dest && $dest eq '-'; |
5e1267a5 DM |
719 | |
720 | $dport = undef if $dport && $dport eq '-'; | |
721 | $sport = undef if $sport && $sport eq '-'; | |
4cdbb3b7 AD |
722 | my $nbdport = undef; |
723 | my $nbsport = undef; | |
d6de1dc2 AD |
724 | my $nbsource = undef; |
725 | my $nbdest = undef; | |
5e1267a5 | 726 | |
ecbea048 | 727 | eval { |
d6de1dc2 AD |
728 | $nbsource = parse_address_list($source) if $source; |
729 | $nbdest = parse_address_list($dest) if $dest; | |
4cdbb3b7 AD |
730 | $nbdport = parse_port_name_number_or_range($dport) if $dport; |
731 | $nbsport = parse_port_name_number_or_range($sport) if $sport; | |
ecbea048 DM |
732 | }; |
733 | if (my $err = $@) { | |
734 | warn $err; | |
735 | next; | |
736 | ||
737 | } | |
738 | ||
739 | ||
5e1267a5 DM |
740 | my $rule = { |
741 | action => $action, | |
742 | service => $service, | |
743 | iface => $iface, | |
744 | source => $source, | |
745 | dest => $dest, | |
d6de1dc2 AD |
746 | nbsource => $nbsource, |
747 | nbdest => $nbdest, | |
5e1267a5 DM |
748 | proto => $proto, |
749 | dport => $dport, | |
750 | sport => $sport, | |
4cdbb3b7 AD |
751 | nbdport => $nbdport, |
752 | nbsport => $nbsport, | |
753 | ||
5e1267a5 DM |
754 | }; |
755 | ||
756 | push @{$res->{$section}}, $rule; | |
757 | } | |
758 | ||
759 | return $res; | |
760 | } | |
761 | ||
762 | sub read_local_vm_config { | |
763 | ||
764 | my $openvz = {}; | |
765 | ||
766 | my $qemu = {}; | |
767 | ||
768 | my $list = PVE::QemuServer::config_list(); | |
769 | ||
770 | foreach my $vmid (keys %$list) { | |
b9b06789 | 771 | #next if !($vmid eq '100' || $vmid eq '102'); |
5e1267a5 DM |
772 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); |
773 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
774 | $qemu->{$vmid} = $conf; | |
775 | } | |
776 | } | |
777 | ||
778 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
779 | ||
780 | return $vmdata; | |
781 | }; | |
782 | ||
783 | sub read_vm_firewall_rules { | |
784 | my ($vmdata) = @_; | |
785 | my $rules = {}; | |
786 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
787 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
788 | my $fh = IO::File->new($filename, O_RDONLY); | |
789 | next if !$fh; | |
790 | ||
791 | $rules->{$vmid} = parse_fw_rules($filename, $fh); | |
792 | } | |
793 | ||
794 | return $rules; | |
795 | } | |
796 | ||
797 | sub compile { | |
798 | ||
799 | my $vmdata = read_local_vm_config(); | |
800 | my $rules = read_vm_firewall_rules($vmdata); | |
801 | ||
802 | # print Dumper($vmdata); | |
803 | ||
804 | my $swdir = '/etc/shorewall'; | |
805 | mkdir $swdir; | |
806 | ||
807 | &$compile_shorewall($swdir, $vmdata, $rules); | |
b6360c3f | 808 | |
5e1267a5 | 809 | PVE::Tools::run_command(['shorewall', 'compile']); |
b6360c3f DM |
810 | } |
811 | ||
5e1267a5 DM |
812 | sub compile_and_start { |
813 | my ($restart) = @_; | |
886aba9c | 814 | |
5e1267a5 | 815 | compile(); |
b6360c3f | 816 | |
5e1267a5 | 817 | PVE::Tools::run_command(['shorewall', $restart ? 'restart' : 'start']); |
b6360c3f DM |
818 | } |
819 | ||
820 | ||
821 | 1; |