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