]>
Commit | Line | Data |
---|---|---|
b6360c3f DM |
1 | package PVE::Firewall; |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | use Data::Dumper; | |
de2a57cd | 6 | use Digest::SHA; |
f789653a | 7 | use PVE::Tools; |
b6360c3f | 8 | use PVE::QemuServer; |
5e1267a5 DM |
9 | use File::Path; |
10 | use IO::File; | |
ecbea048 | 11 | use Net::IP; |
06320eb0 | 12 | use PVE::Tools qw(run_command lock_file); |
ecbea048 | 13 | |
5e1267a5 | 14 | use Data::Dumper; |
b6360c3f | 15 | |
06320eb0 DM |
16 | my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; |
17 | ||
9aab3127 | 18 | my $macros; |
3a616aa0 AD |
19 | my @ruleset = (); |
20 | ||
a332200b DM |
21 | # todo: implement some kind of MACROS, like shorewall /usr/share/shorewall/macro.* |
22 | sub get_firewall_macros { | |
9aab3127 DM |
23 | |
24 | return $macros if $macros; | |
25 | ||
a332200b DM |
26 | #foreach my $path (</usr/share/shorewall/macro.*>) { |
27 | # if ($path =~ m|/macro\.(\S+)$|) { | |
28 | # $macros->{$1} = 1; | |
29 | # } | |
30 | #} | |
31 | ||
32 | $macros = {}; # fixme: implemet me | |
33 | ||
9aab3127 DM |
34 | return $macros; |
35 | } | |
36 | ||
fcba0beb DM |
37 | my $etc_services; |
38 | ||
39 | sub get_etc_services { | |
40 | ||
41 | return $etc_services if $etc_services; | |
42 | ||
43 | my $filename = "/etc/services"; | |
44 | ||
45 | my $fh = IO::File->new($filename, O_RDONLY); | |
46 | if (!$fh) { | |
47 | warn "unable to read '$filename' - $!\n"; | |
48 | return {}; | |
49 | } | |
50 | ||
51 | my $services = {}; | |
52 | ||
53 | while (my $line = <$fh>) { | |
54 | chomp ($line); | |
55 | next if $line =~m/^#/; | |
56 | next if ($line =~m/^\s*$/); | |
57 | ||
58 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
59 | $services->{byid}->{$2}->{name} = $1; | |
60 | $services->{byid}->{$2}->{$3} = 1; | |
61 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
62 | } | |
63 | } | |
64 | ||
65 | close($fh); | |
66 | ||
67 | $etc_services = $services; | |
68 | ||
3a616aa0 | 69 | |
fcba0beb DM |
70 | return $etc_services; |
71 | } | |
72 | ||
73 | my $etc_protocols; | |
74 | ||
75 | sub get_etc_protocols { | |
76 | return $etc_protocols if $etc_protocols; | |
77 | ||
78 | my $filename = "/etc/protocols"; | |
79 | ||
80 | my $fh = IO::File->new($filename, O_RDONLY); | |
81 | if (!$fh) { | |
82 | warn "unable to read '$filename' - $!\n"; | |
83 | return {}; | |
84 | } | |
85 | ||
86 | my $protocols = {}; | |
87 | ||
88 | while (my $line = <$fh>) { | |
89 | chomp ($line); | |
90 | next if $line =~m/^#/; | |
91 | next if ($line =~m/^\s*$/); | |
92 | ||
93 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
94 | $protocols->{byid}->{$2}->{name} = $1; | |
95 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
96 | } | |
97 | } | |
98 | ||
99 | close($fh); | |
100 | ||
101 | $etc_protocols = $protocols; | |
102 | ||
103 | return $etc_protocols; | |
104 | } | |
105 | ||
ecbea048 DM |
106 | sub parse_address_list { |
107 | my ($str) = @_; | |
108 | ||
d6de1dc2 | 109 | my $nbaor = 0; |
ecbea048 DM |
110 | foreach my $aor (split(/,/, $str)) { |
111 | if (!Net::IP->new($aor)) { | |
112 | my $err = Net::IP::Error(); | |
113 | die "invalid IP address: $err\n"; | |
d6de1dc2 AD |
114 | }else{ |
115 | $nbaor++; | |
ecbea048 DM |
116 | } |
117 | } | |
d6de1dc2 | 118 | return $nbaor; |
ecbea048 | 119 | } |
dddd9413 | 120 | |
fcba0beb DM |
121 | sub parse_port_name_number_or_range { |
122 | my ($str) = @_; | |
123 | ||
124 | my $services = PVE::Firewall::get_etc_services(); | |
4cdbb3b7 | 125 | my $nbports = 0; |
fcba0beb | 126 | foreach my $item (split(/,/, $str)) { |
4cdbb3b7 | 127 | my $portlist = ""; |
fcba0beb | 128 | foreach my $pon (split(':', $item, 2)) { |
4cdbb3b7 AD |
129 | if ($pon =~ m/^\d+$/){ |
130 | die "invalid port '$pon'\n" if $pon < 0 && $pon > 65536; | |
131 | }else{ | |
132 | die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; | |
133 | } | |
134 | $nbports++; | |
fcba0beb DM |
135 | } |
136 | } | |
137 | ||
4cdbb3b7 | 138 | return ($nbports); |
fcba0beb DM |
139 | } |
140 | ||
8cebfa6f | 141 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; |
dddd9413 | 142 | |
3a616aa0 AD |
143 | sub iptables { |
144 | my ($cmd) = @_; | |
145 | ||
146 | run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); | |
147 | } | |
148 | ||
149 | sub iptables_restore { | |
150 | ||
151 | unshift (@ruleset, '*filter'); | |
152 | push (@ruleset, 'COMMIT'); | |
153 | ||
64568ce0 | 154 | my $cmdlist = join("\n", @ruleset) . "\n"; |
3a616aa0 | 155 | |
64568ce0 DM |
156 | my $verbose = 1; # fixme: how/when do we set this |
157 | ||
158 | #run_command("echo '$cmdlist' | /sbin/iptables-restore -n"); | |
159 | eval { run_command("/sbin/iptables-restore -n ", input => $cmdlist); }; | |
160 | if (my $err = $@) { | |
161 | print STDERR $cmdlist if $verbose; | |
162 | die $err; | |
163 | } | |
3a616aa0 AD |
164 | } |
165 | ||
de2a57cd DM |
166 | # experimental code to read existing chains and compute SHA1 checksum |
167 | # for each chain. | |
168 | sub iptables_get_chains { | |
169 | ||
170 | my $res = {}; | |
171 | ||
172 | # check what chains we want to track | |
173 | my $is_pvefw_chain = sub { | |
174 | my $name = shift; | |
175 | ||
176 | return 1 if $name =~ m/^BRIDGEFW-(:?IN|OUT)$/; | |
177 | return 1 if $name =~ m/^proxmoxfw-\S+$/; | |
178 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; | |
179 | ||
180 | return undef; | |
181 | }; | |
182 | ||
183 | my $table = ''; | |
184 | ||
185 | my $dhash = {}; | |
186 | ||
187 | my $parser = sub { | |
188 | my $line = shift; | |
189 | ||
190 | return if $line =~ m/^#/; | |
191 | return if $line =~ m/^\s*$/; | |
192 | ||
193 | if ($line =~ m/^\*(\S+)$/) { | |
194 | $table = $1; | |
195 | return; | |
196 | } | |
197 | ||
198 | return if $table ne 'filter'; | |
199 | ||
200 | if ($line =~ m/^:(\S+)\s/) { | |
201 | my $chain = $1; | |
202 | return if !&$is_pvefw_chain($chain); | |
203 | $dhash->{$chain} = Digest::SHA->new('sha1'); | |
204 | } elsif ($line =~ m/^-([A-Z]) (\S+)\s/) { | |
205 | my $chain = $2; | |
206 | return if !&$is_pvefw_chain($chain); | |
207 | my $sha = $dhash->{$chain} || die "undefined chain '$chain'"; | |
208 | $sha->add_bits("$line\n"); | |
209 | } else { | |
210 | # simply ignore the rest | |
211 | return; | |
212 | } | |
213 | }; | |
214 | ||
215 | run_command("/sbin/iptables-save", outfunc => $parser); | |
216 | ||
217 | foreach my $chain (keys %$dhash) { | |
218 | my $sha = $dhash->{$chain}; | |
219 | $res->{$chain} = $sha->b64digest; | |
220 | } | |
221 | ||
222 | return $res; | |
223 | } | |
224 | ||
3a616aa0 AD |
225 | sub iptables_addrule { |
226 | my ($rule) = @_; | |
227 | ||
228 | push (@ruleset, $rule); | |
229 | } | |
230 | ||
231 | sub iptables_chain_exist { | |
232 | my ($chain) = @_; | |
233 | ||
234 | eval{ | |
235 | iptables("-n --list $chain"); | |
236 | }; | |
237 | return undef if $@; | |
238 | ||
239 | return 1; | |
240 | } | |
241 | ||
242 | sub iptables_rule_exist { | |
243 | my ($rule) = @_; | |
244 | ||
245 | eval{ | |
246 | iptables("-C $rule"); | |
247 | }; | |
248 | return undef if $@; | |
249 | ||
250 | return 1; | |
251 | } | |
252 | ||
253 | sub iptables_generate_rule { | |
254 | my ($chain, $rule) = @_; | |
255 | ||
256 | my $cmd = "-A $chain"; | |
257 | ||
d6de1dc2 | 258 | $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1; |
3a616aa0 | 259 | $cmd .= " -s $rule->{source}" if $rule->{source}; |
d6de1dc2 | 260 | $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1; |
3a616aa0 AD |
261 | $cmd .= " -d $rule->{dest}" if $rule->{destination}; |
262 | $cmd .= " -p $rule->{proto}" if $rule->{proto}; | |
4cdbb3b7 | 263 | $cmd .= " --match multiport" if $rule->{nbdport} && $rule->{nbdport} > 1; |
3a616aa0 | 264 | $cmd .= " --dport $rule->{dport}" if $rule->{dport}; |
4cdbb3b7 | 265 | $cmd .= " --match multiport" if $rule->{nbsport} && $rule->{nbsport} > 1; |
3a616aa0 AD |
266 | $cmd .= " --sport $rule->{sport}" if $rule->{sport}; |
267 | $cmd .= " -j $rule->{action}" if $rule->{action}; | |
268 | ||
269 | iptables_addrule($cmd); | |
270 | ||
271 | } | |
272 | ||
273 | sub generate_bridge_rules { | |
274 | my ($bridge) = @_; | |
275 | ||
276 | if(!iptables_chain_exist("BRIDGEFW-OUT")){ | |
277 | iptables_addrule(":BRIDGEFW-OUT - [0:0]"); | |
278 | } | |
279 | ||
280 | if(!iptables_chain_exist("BRIDGEFW-IN")){ | |
281 | iptables_addrule(":BRIDGEFW-IN - [0:0]"); | |
282 | } | |
283 | ||
284 | if(!iptables_chain_exist("proxmoxfw-FORWARD")){ | |
285 | iptables_addrule(":proxmoxfw-FORWARD - [0:0]"); | |
286 | iptables_addrule("-I FORWARD -j proxmoxfw-FORWARD"); | |
287 | iptables_addrule("-A proxmoxfw-FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT"); | |
288 | iptables_addrule("-A proxmoxfw-FORWARD -m physdev --physdev-is-in --physdev-is-bridged -j BRIDGEFW-OUT"); | |
289 | iptables_addrule("-A proxmoxfw-FORWARD -m physdev --physdev-is-out --physdev-is-bridged -j BRIDGEFW-IN"); | |
290 | ||
291 | } | |
292 | ||
293 | generate_proxmoxfwinput(); | |
294 | ||
295 | if(!iptables_chain_exist("$bridge-IN")){ | |
296 | iptables_addrule(":$bridge-IN - [0:0]"); | |
297 | iptables_addrule("-A proxmoxfw-FORWARD -i $bridge -j DROP"); #disable interbridge routing | |
298 | iptables_addrule("-A BRIDGEFW-IN -j $bridge-IN"); | |
299 | iptables_addrule("-A $bridge-IN -j ACCEPT"); | |
300 | ||
301 | } | |
302 | ||
303 | if(!iptables_chain_exist("$bridge-OUT")){ | |
304 | iptables_addrule(":$bridge-OUT - [0:0]"); | |
305 | iptables_addrule("-A proxmoxfw-FORWARD -o $bridge -j DROP"); # disable interbridge routing | |
306 | iptables_addrule("-A BRIDGEFW-OUT -j $bridge-OUT"); | |
307 | ||
308 | } | |
309 | ||
310 | } | |
311 | ||
312 | ||
313 | sub generate_tap_rules_direction { | |
314 | my ($iface, $netid, $rules, $bridge, $direction) = @_; | |
315 | ||
316 | my $tapchain = "$iface-$direction"; | |
317 | ||
318 | iptables_addrule(":$tapchain - [0:0]"); | |
319 | ||
320 | iptables_addrule("-A $tapchain -m state --state INVALID -j DROP"); | |
321 | iptables_addrule("-A $tapchain -m state --state RELATED,ESTABLISHED -j ACCEPT"); | |
322 | ||
323 | if (scalar(@$rules)) { | |
324 | foreach my $rule (@$rules) { | |
325 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
326 | if($rule->{action} =~ m/^(GROUP-(\S+))$/){ | |
327 | $rule->{action} .= "-$direction"; | |
328 | #generate empty group rule if don't exist | |
329 | if(!iptables_chain_exist($rule->{action})){ | |
330 | generate_group_rules($2); | |
331 | } | |
332 | } | |
333 | #we go to vmbr-IN if accept in out rules | |
334 | $rule->{action} = "$bridge-IN" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT'; | |
335 | iptables_generate_rule($tapchain, $rule); | |
336 | } | |
337 | } | |
338 | ||
339 | iptables_addrule("-A $tapchain -j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4"); | |
340 | iptables_addrule("-A $tapchain -j DROP"); | |
341 | ||
342 | #plug the tap chain to bridge chain | |
343 | my $physdevdirection = $direction eq 'IN' ? "out":"in"; | |
344 | my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain"; | |
345 | ||
346 | if(!iptables_rule_exist($rule)){ | |
347 | iptables_addrule("-I $rule"); | |
348 | } | |
349 | ||
350 | if($direction eq 'OUT'){ | |
351 | #add tap->host rules | |
352 | my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain"; | |
353 | ||
354 | if(!iptables_rule_exist($rule)){ | |
355 | iptables_addrule("-A $rule"); | |
356 | } | |
357 | } | |
358 | } | |
359 | ||
360 | sub generate_tap_rules { | |
361 | my ($net, $netid, $vmid) = @_; | |
362 | ||
363 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
364 | my $fh = IO::File->new($filename, O_RDONLY); | |
365 | return if !$fh; | |
366 | ||
367 | #generate bridge rules | |
368 | my $bridge = $net->{bridge}; | |
369 | my $tag = $net->{tag}; | |
370 | $bridge .= "v$tag" if $tag; | |
371 | ||
372 | #generate tap chain | |
373 | my $rules = parse_fw_rules($filename, $fh); | |
374 | ||
375 | my $inrules = $rules->{in}; | |
376 | my $outrules = $rules->{out}; | |
377 | ||
378 | my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/; | |
379 | ||
380 | generate_bridge_rules($bridge); | |
381 | generate_tap_rules_direction($iface, $netid, $inrules, $bridge, 'IN'); | |
382 | generate_tap_rules_direction($iface, $netid, $outrules, $bridge, 'OUT'); | |
383 | iptables_restore(); | |
384 | } | |
385 | ||
386 | sub flush_tap_rules { | |
387 | my ($net, $netid, $vmid) = @_; | |
388 | ||
389 | my $bridge = $net->{bridge}; | |
390 | my $iface = "tap".$vmid."i".$1 if $netid =~ m/net(\d+)/; | |
391 | ||
392 | flush_tap_rules_direction($iface, $bridge, 'IN'); | |
393 | flush_tap_rules_direction($iface, $bridge, 'OUT'); | |
394 | iptables_restore(); | |
395 | } | |
396 | ||
397 | sub flush_tap_rules_direction { | |
398 | my ($iface, $bridge, $direction) = @_; | |
399 | ||
400 | my $tapchain = "$iface-$direction"; | |
401 | ||
402 | if(iptables_chain_exist($tapchain)){ | |
403 | iptables_addrule("-F $tapchain"); | |
404 | ||
405 | my $physdevdirection = $direction eq 'IN' ? "out":"in"; | |
406 | my $rule = "$bridge-$direction -m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain"; | |
407 | if(iptables_rule_exist($rule)){ | |
408 | iptables_addrule("-D $rule"); | |
409 | } | |
410 | ||
411 | if($direction eq 'OUT'){ | |
412 | my $rule = "proxmoxfw-INPUT -m physdev --physdev-$physdevdirection $iface -j $tapchain"; | |
462a6553 | 413 | if(iptables_rule_exist($rule)){ |
3a616aa0 AD |
414 | iptables_addrule("-D $rule"); |
415 | } | |
416 | } | |
417 | ||
418 | iptables_addrule("-X $tapchain"); | |
419 | } | |
420 | } | |
421 | ||
0bd5f137 AD |
422 | sub enablehostfw { |
423 | ||
424 | generate_proxmoxfwinput(); | |
425 | generate_proxmoxfwoutput(); | |
426 | ||
427 | my $filename = "/etc/pve/local/host.fw"; | |
428 | my $fh = IO::File->new($filename, O_RDONLY); | |
429 | return if !$fh; | |
430 | ||
431 | my $rules = parse_fw_rules($filename, $fh); | |
432 | my $inrules = $rules->{in}; | |
433 | my $outrules = $rules->{out}; | |
434 | ||
435 | #host inbound firewall | |
436 | iptables_addrule(":host-IN - [0:0]"); | |
437 | iptables_addrule("-A host-IN -m state --state INVALID -j DROP"); | |
438 | iptables_addrule("-A host-IN -m state --state RELATED,ESTABLISHED -j ACCEPT"); | |
439 | iptables_addrule("-A host-IN -i lo -j ACCEPT"); | |
440 | iptables_addrule("-A host-IN -m addrtype --dst-type MULTICAST -j ACCEPT"); | |
441 | iptables_addrule("-A host-IN -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT"); | |
442 | iptables_addrule("-A host-IN -p udp -m udp --dport 9000 -j ACCEPT"); #corosync | |
443 | ||
444 | if (scalar(@$inrules)) { | |
445 | foreach my $rule (@$inrules) { | |
446 | #we use RETURN because we need to check also tap rules | |
447 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
448 | iptables_generate_rule('host-IN', $rule); | |
449 | } | |
450 | } | |
451 | ||
452 | iptables_addrule("-A host-IN -j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4"); | |
453 | iptables_addrule("-A host-IN -j DROP"); | |
454 | ||
455 | #host outbound firewall | |
456 | iptables_addrule(":host-OUT - [0:0]"); | |
457 | iptables_addrule("-A host-OUT -m state --state INVALID -j DROP"); | |
458 | iptables_addrule("-A host-OUT -m state --state RELATED,ESTABLISHED -j ACCEPT"); | |
459 | iptables_addrule("-A host-OUT -o lo -j ACCEPT"); | |
460 | iptables_addrule("-A host-OUT -m addrtype --dst-type MULTICAST -j ACCEPT"); | |
461 | iptables_addrule("-A host-OUT -p udp -m state --state NEW -m multiport --dports 5404,5405 -j ACCEPT"); | |
462 | iptables_addrule("-A host-OUT -p udp -m udp --dport 9000 -j ACCEPT"); #corosync | |
463 | ||
464 | if (scalar(@$outrules)) { | |
465 | foreach my $rule (@$outrules) { | |
466 | #we use RETURN because we need to check also tap rules | |
467 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
468 | iptables_generate_rule('host-OUT', $rule); | |
469 | } | |
470 | } | |
471 | ||
472 | iptables_addrule("-A host-OUT -j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4"); | |
473 | iptables_addrule("-A host-OUT -j DROP"); | |
474 | ||
475 | ||
476 | my $rule = "proxmoxfw-INPUT -j host-IN"; | |
477 | if(!iptables_rule_exist($rule)){ | |
478 | iptables_addrule("-I $rule"); | |
479 | } | |
480 | ||
481 | $rule = "proxmoxfw-OUTPUT -j host-OUT"; | |
482 | if(!iptables_rule_exist($rule)){ | |
483 | iptables_addrule("-I $rule"); | |
484 | } | |
485 | ||
486 | iptables_restore(); | |
487 | ||
488 | ||
489 | } | |
490 | ||
491 | sub disablehostfw { | |
492 | ||
493 | my $chain = "host-IN"; | |
494 | ||
495 | my $rule = "proxmoxfw-INPUT -j $chain"; | |
496 | if(iptables_rule_exist($rule)){ | |
497 | iptables_addrule("-D $rule"); | |
498 | } | |
499 | ||
500 | if(iptables_chain_exist($chain)){ | |
501 | iptables_addrule("-F $chain"); | |
502 | iptables_addrule("-X $chain"); | |
503 | } | |
504 | ||
505 | $chain = "host-OUT"; | |
506 | ||
507 | $rule = "proxmoxfw-OUTPUT -j $chain"; | |
508 | if(iptables_rule_exist($rule)){ | |
509 | iptables_addrule("-D $rule"); | |
510 | } | |
511 | ||
512 | if(iptables_chain_exist($chain)){ | |
513 | iptables_addrule("-F $chain"); | |
514 | iptables_addrule("-X $chain"); | |
515 | } | |
516 | ||
517 | iptables_restore(); | |
518 | } | |
519 | ||
520 | sub generate_proxmoxfwinput { | |
521 | ||
522 | if(!iptables_chain_exist("proxmoxfw-INPUT")){ | |
523 | iptables_addrule(":proxmoxfw-INPUT - [0:0]"); | |
524 | iptables_addrule("-I INPUT -j proxmoxfw-INPUT"); | |
525 | iptables_addrule("-A INPUT -j ACCEPT"); | |
526 | } | |
527 | } | |
528 | ||
529 | sub generate_proxmoxfwoutput { | |
530 | ||
531 | if(!iptables_chain_exist("proxmoxfw-OUTPUT")){ | |
532 | iptables_addrule(":proxmoxfw-OUTPUT - [0:0]"); | |
533 | iptables_addrule("-I OUTPUT -j proxmoxfw-OUTPUT"); | |
534 | iptables_addrule("-A OUTPUT -j ACCEPT"); | |
535 | } | |
536 | ||
537 | } | |
538 | ||
9d31b418 AD |
539 | sub enable_group_rules { |
540 | my ($group) = @_; | |
541 | ||
542 | generate_group_rules($group); | |
543 | iptables_restore(); | |
544 | } | |
545 | ||
546 | sub generate_group_rules { | |
547 | my ($group) = @_; | |
548 | ||
549 | my $filename = "/etc/pve/firewall/groups.fw"; | |
550 | my $fh = IO::File->new($filename, O_RDONLY); | |
551 | return if !$fh; | |
552 | ||
553 | my $rules = parse_fw_rules($filename, $fh, $group); | |
554 | my $inrules = $rules->{in}; | |
555 | my $outrules = $rules->{out}; | |
556 | ||
557 | my $chain = "GROUP-".$group."-IN"; | |
558 | ||
559 | iptables_addrule(":$chain - [0:0]"); | |
560 | ||
561 | if (scalar(@$inrules)) { | |
562 | foreach my $rule (@$inrules) { | |
563 | iptables_generate_rule($chain, $rule); | |
564 | } | |
565 | } | |
566 | ||
567 | $chain = "GROUP-".$group."-OUT"; | |
568 | ||
569 | iptables_addrule(":$chain - [0:0]"); | |
570 | ||
571 | if(!iptables_chain_exist("BRIDGEFW-OUT")){ | |
572 | iptables_addrule(":BRIDGEFW-OUT - [0:0]"); | |
573 | } | |
574 | ||
575 | if(!iptables_chain_exist("BRIDGEFW-IN")){ | |
576 | iptables_addrule(":BRIDGEFW-IN - [0:0]"); | |
577 | } | |
578 | ||
579 | if (scalar(@$outrules)) { | |
580 | foreach my $rule (@$outrules) { | |
581 | #we go the BRIDGEFW-IN because we need to check also other tap rules | |
582 | #(and group rules can be set on any bridge, so we can't go to VMBRXX-IN) | |
583 | $rule->{action} = 'BRIDGEFW-IN' if $rule->{action} eq 'ACCEPT'; | |
584 | iptables_generate_rule($chain, $rule); | |
585 | } | |
586 | } | |
587 | ||
588 | } | |
589 | ||
590 | sub disable_group_rules { | |
591 | my ($group) = @_; | |
592 | ||
593 | my $chain = "GROUP-".$group."-IN"; | |
594 | ||
595 | if(iptables_chain_exist($chain)){ | |
596 | iptables_addrule("-F $chain"); | |
597 | iptables_addrule("-X $chain"); | |
598 | } | |
599 | ||
600 | $chain = "GROUP-".$group."-OUT"; | |
601 | ||
602 | if(iptables_chain_exist($chain)){ | |
603 | iptables_addrule("-F $chain"); | |
604 | iptables_addrule("-X $chain"); | |
605 | } | |
606 | ||
607 | #iptables_restore will die if security group is linked in a tap chain | |
608 | #maybe can we improve that, parsing each vm config, or parsing iptables -S | |
609 | #to see if the security group is linked or not | |
610 | iptables_restore(); | |
611 | } | |
612 | ||
5e1267a5 | 613 | sub parse_fw_rules { |
9d31b418 | 614 | my ($filename, $fh, $group) = @_; |
5e1267a5 DM |
615 | |
616 | my $section; | |
9d31b418 AD |
617 | my $securitygroup; |
618 | my $securitygroupexist; | |
5e1267a5 DM |
619 | |
620 | my $res = { in => [], out => [] }; | |
621 | ||
a332200b | 622 | my $macros = get_firewall_macros(); |
fcba0beb DM |
623 | my $protocols = get_etc_protocols(); |
624 | ||
5e1267a5 DM |
625 | while (defined(my $line = <$fh>)) { |
626 | next if $line =~ m/^#/; | |
627 | next if $line =~ m/^\s*$/; | |
628 | ||
9d31b418 | 629 | if ($line =~ m/^\[(in|out)(:(\S+))?\]\s*$/i) { |
5e1267a5 | 630 | $section = lc($1); |
9d31b418 AD |
631 | $securitygroup = lc($3) if $3; |
632 | $securitygroupexist = 1 if $securitygroup && $securitygroup eq $group; | |
5e1267a5 DM |
633 | next; |
634 | } | |
635 | next if !$section; | |
9d31b418 | 636 | next if $group && $securitygroup ne $group; |
5e1267a5 DM |
637 | |
638 | my ($action, $iface, $source, $dest, $proto, $dport, $sport) = | |
639 | split(/\s+/, $line); | |
640 | ||
641 | if (!$action) { | |
642 | warn "skip incomplete line\n"; | |
643 | next; | |
644 | } | |
645 | ||
646 | my $service; | |
3a616aa0 | 647 | if ($action =~ m/^(ACCEPT|DROP|REJECT|GROUP-(\S+))$/) { |
5e1267a5 DM |
648 | # OK |
649 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
650 | ($service, $action) = ($1, $2); | |
651 | if (!$macros->{$service}) { | |
652 | warn "unknown service '$service'\n"; | |
653 | next; | |
654 | } | |
655 | } else { | |
656 | warn "unknown action '$action'\n"; | |
657 | next; | |
658 | } | |
659 | ||
660 | $iface = undef if $iface && $iface eq '-'; | |
661 | if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) { | |
662 | warn "unknown interface '$iface'\n"; | |
663 | next; | |
664 | } | |
665 | ||
666 | $proto = undef if $proto && $proto eq '-'; | |
fcba0beb DM |
667 | if ($proto && !(defined($protocols->{byname}->{$proto}) || |
668 | defined($protocols->{byid}->{$proto}))) { | |
5e1267a5 DM |
669 | warn "unknown protokol '$proto'\n"; |
670 | next; | |
671 | } | |
672 | ||
673 | $source = undef if $source && $source eq '-'; | |
5e1267a5 | 674 | $dest = undef if $dest && $dest eq '-'; |
5e1267a5 DM |
675 | |
676 | $dport = undef if $dport && $dport eq '-'; | |
677 | $sport = undef if $sport && $sport eq '-'; | |
4cdbb3b7 AD |
678 | my $nbdport = undef; |
679 | my $nbsport = undef; | |
d6de1dc2 AD |
680 | my $nbsource = undef; |
681 | my $nbdest = undef; | |
5e1267a5 | 682 | |
ecbea048 | 683 | eval { |
d6de1dc2 AD |
684 | $nbsource = parse_address_list($source) if $source; |
685 | $nbdest = parse_address_list($dest) if $dest; | |
4cdbb3b7 AD |
686 | $nbdport = parse_port_name_number_or_range($dport) if $dport; |
687 | $nbsport = parse_port_name_number_or_range($sport) if $sport; | |
ecbea048 DM |
688 | }; |
689 | if (my $err = $@) { | |
690 | warn $err; | |
691 | next; | |
692 | ||
693 | } | |
694 | ||
695 | ||
5e1267a5 DM |
696 | my $rule = { |
697 | action => $action, | |
698 | service => $service, | |
699 | iface => $iface, | |
700 | source => $source, | |
701 | dest => $dest, | |
d6de1dc2 AD |
702 | nbsource => $nbsource, |
703 | nbdest => $nbdest, | |
5e1267a5 DM |
704 | proto => $proto, |
705 | dport => $dport, | |
706 | sport => $sport, | |
4cdbb3b7 AD |
707 | nbdport => $nbdport, |
708 | nbsport => $nbsport, | |
709 | ||
5e1267a5 DM |
710 | }; |
711 | ||
712 | push @{$res->{$section}}, $rule; | |
713 | } | |
714 | ||
9d31b418 | 715 | die "security group $group don't exist" if $group && !$securitygroupexist; |
5e1267a5 DM |
716 | return $res; |
717 | } | |
718 | ||
06320eb0 DM |
719 | sub run_locked { |
720 | my ($code, @param) = @_; | |
721 | ||
722 | my $timeout = 10; | |
723 | ||
724 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
725 | ||
726 | die $@ if $@; | |
727 | ||
728 | return $res; | |
729 | } | |
730 | ||
5e1267a5 DM |
731 | sub read_local_vm_config { |
732 | ||
733 | my $openvz = {}; | |
734 | ||
735 | my $qemu = {}; | |
736 | ||
737 | my $list = PVE::QemuServer::config_list(); | |
738 | ||
739 | foreach my $vmid (keys %$list) { | |
b9b06789 | 740 | #next if !($vmid eq '100' || $vmid eq '102'); |
5e1267a5 DM |
741 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); |
742 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
743 | $qemu->{$vmid} = $conf; | |
744 | } | |
745 | } | |
746 | ||
747 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
748 | ||
749 | return $vmdata; | |
750 | }; | |
751 | ||
752 | sub read_vm_firewall_rules { | |
753 | my ($vmdata) = @_; | |
754 | my $rules = {}; | |
755 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
756 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
757 | my $fh = IO::File->new($filename, O_RDONLY); | |
758 | next if !$fh; | |
759 | ||
760 | $rules->{$vmid} = parse_fw_rules($filename, $fh); | |
761 | } | |
762 | ||
763 | return $rules; | |
764 | } | |
765 | ||
766 | sub compile { | |
5e1267a5 DM |
767 | my $vmdata = read_local_vm_config(); |
768 | my $rules = read_vm_firewall_rules($vmdata); | |
769 | ||
770 | # print Dumper($vmdata); | |
771 | ||
a332200b | 772 | die "implement me"; |
b6360c3f DM |
773 | } |
774 | ||
5e1267a5 DM |
775 | sub compile_and_start { |
776 | my ($restart) = @_; | |
886aba9c | 777 | |
5e1267a5 | 778 | compile(); |
b6360c3f | 779 | |
a332200b | 780 | die "implement me"; |
b6360c3f DM |
781 | } |
782 | ||
b6360c3f | 783 | 1; |