]>
git.proxmox.com Git - pve-firewall.git/blob - PVE/Firewall.pm
3f0adf0cb531d0314a97135f5b3fceef79d7cf79
13 sub get_shorewall_macros
{
15 return $macros if $macros;
17 foreach my $path (</usr/share
/shorewall/macro
.*>) {
18 if ($path =~ m
|/macro\
.(\S
+)$|) {
26 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
28 my $generate_input_rule = sub {
29 my ($zoneinfo, $rule, $net, $netid) = @_;
31 my $zone = $net->{zone
} || die "internal error";
32 my $zid = $zoneinfo->{$zone}->{zoneref
} || die "internal error";
33 my $tap = $net->{tap
} || die "internal error";
35 my $dest = "$zid:$tap";
38 $dest .= ":$rule->{dest}";
41 my $action = $rule->{service
} ?
42 "$rule->{service}($rule->{action})" : $rule->{action
};
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
}) {
51 # $source = "${zoneref}";
55 $source = "${zoneref}:$rule->{source}";
58 $source = "all:$rule->{source}";
61 return sprintf($rule_format, $action, $source, $dest, $rule->{proto
} || '-',
62 $rule->{dport
} || '-', $rule->{sport
} || '-');
65 my $generate_output_rule = sub {
66 my ($zoneinfo, $rule, $net, $netid) = @_;
68 my $zone = $net->{zone
} || die "internal error";
69 my $zid = $zoneinfo->{$zone}->{zoneref
} || die "internal error";
70 my $tap = $net->{tap
} || die "internal error";
72 my $action = $rule->{service
} ?
73 "$rule->{service}($rule->{action})" : $rule->{action
};
80 $dest = "all:$rule->{dest}";
83 return sprintf($rule_format, $action, "$zid:$tap", $dest,
84 $rule->{proto
} || '-', $rule->{dport
} || '-', $rule->{sport
} || '-');
87 # we need complete VM configuration of all VMs (openvz/qemu)
90 my $compile_shorewall = sub {
91 my ($targetdir, $vmdata, $rules) = @_;
93 # remove existing data ?
94 foreach my $file (qw(params zones rules interfaces maclist policy)) {
95 unlink "$targetdir/$file";
101 fw
=> { type
=> 'firewall' },
108 $register_bridge = sub {
109 my ($bridge, $vlan) = @_;
111 my $zone = 'z' . $bridge;
113 return $zone if $zoneinfo->{$zone};
115 my $ext_zone = "z${bridge}ext";
117 $zoneinfo->{$zone} = {
120 bridge_ext_zone
=> $ext_zone,
123 # physical input devices
124 my $dir = "/sys/class/net/$bridge/brif";
126 PVE
::Tools
::dir_glob_foreach
($dir, '((eth|bond).+)', sub {
128 $physical->{$slave} = 1;
131 $zoneinfo->{$ext_zone} = {
133 bridge_zone
=> $zone,
137 return &$register_bridge("${bridge}v${vlan}") if defined($vlan);
142 my $register_bridge_port = sub {
143 my ($bridge, $vlan, $vmzone, $tap) = @_;
145 my $bridge_zone = &$register_bridge($bridge, $vlan);
146 my $zone = $bridge_zone . $vmzone;
148 if (!$zoneinfo->{$zone}) {
149 $zoneinfo->{$zone} = {
151 bridge_zone
=> $bridge_zone,
156 $zoneinfo->{$zone}->{ifaces
}->{$tap} = 1;
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+)$/;
167 my $net = PVE
::QemuServer
::parse_net
($conf->{$opt});
169 die "implement me" if !$net->{bridge
};
171 my $vmzone = $conf->{zone
} || "vm$vmid";
172 $net->{tap
} = "tap${vmid}i${netnum}";
173 $maclist->{$net->{tap
}} = $net->{macaddr
} || die "internal error";
174 $net->{zone
} = &$register_bridge_port($net->{bridge
}, $net->{tag
}, $vmzone, $net->{tap
});
175 $netinfo->{$vmid}->{$opt} = $net;
179 #print Dumper($netinfo);
181 # NOTE: zone names have length limit, so we need to
182 # translate them into shorter names
185 my $zonemap = { fw
=> 'fw' };
187 my $lookup_zonename = sub {
190 return $zonemap->{$zone} if defined($zonemap->{$zone});
191 $zonemap->{$zone} = 'z' . $zoneid++;
193 return $zonemap->{$zone};
196 foreach my $z (sort keys %$zoneinfo) {
197 $zoneinfo->{$z}->{id
} = &$lookup_zonename($z);
198 $zoneinfo->{$z}->{zonevar
} = uc($z);
199 $zoneinfo->{$z}->{zoneref
} = '$' . $zoneinfo->{$z}->{zonevar
};
205 $out = "# PVE zones\n";
206 foreach my $z (sort keys %$zoneinfo) {
207 $out .= "$zoneinfo->{$z}->{zonevar}=$zoneinfo->{$z}->{id}\n";
209 PVE
::Tools
::file_set_contents
("$targetdir/params", $out);
213 my $format = "%-30s %-10s %-15s\n";
214 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
216 foreach my $z (sort keys %$zoneinfo) {
217 my $zid = $zoneinfo->{$z}->{zoneref
};
218 if ($zoneinfo->{$z}->{type
} eq 'firewall') {
219 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type
}, '');
220 } elsif ($zoneinfo->{$z}->{type
} eq 'bridge') {
221 $out .= sprintf($format, $zid, 'ipv4', '');
222 } elsif ($zoneinfo->{$z}->{type
} eq 'bport') {
223 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone
} || die "internal error";
224 my $bzid = $zoneinfo->{$bridge_zone}->{zoneref
} || die "internal error";
225 $out .= sprintf($format, "$zid:$bzid", 'bport', '');
227 die "internal error";
231 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
233 PVE
::Tools
::file_set_contents
("$targetdir/zones", $out);
237 $format = "%-25s %-20s %-10s %-15s\n";
238 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
240 my $maclist_format = "%-15s %-15s %-15s\n";
241 my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
243 foreach my $z (sort keys %$zoneinfo) {
244 my $zid = $zoneinfo->{$z}->{zoneref
};
245 if ($zoneinfo->{$z}->{type
} eq 'firewall') {
247 } elsif ($zoneinfo->{$z}->{type
} eq 'bridge') {
248 my $bridge = $zoneinfo->{$z}->{bridge
} || die "internal error";
249 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional');
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";
257 if ($maclist->{$iface}) {
258 $out .= sprintf($format, $zid, $iftxt, '-', 'maclist');
259 $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
261 $out .= sprintf($format, $zid, $iftxt, '-', '');
265 die "internal error";
269 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
271 PVE
::Tools
::file_set_contents
("$targetdir/interfaces", $out);
274 PVE
::Tools
::file_set_contents
("$targetdir/maclist", $macs);
278 $format = "%-15s %-15s %-15s %s\n";
279 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
280 $out .= sprintf($format, 'fw', 'all', 'ACCEPT', '');
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');
290 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
292 PVE
::Tools
::file_set_contents
("$targetdir/policy", $out);
297 $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
298 foreach my $vmid (sort keys %$rules) {
299 my $inrules = $rules->{$vmid}->{in};
300 my $outrules = $rules->{$vmid}->{out
};
302 if (scalar(@$inrules)) {
303 $out .= "# IN to VM $vmid\n";
304 foreach my $rule (@$inrules) {
305 foreach my $netid (keys %{$netinfo->{$vmid}}) {
306 my $net = $netinfo->{$vmid}->{$netid};
307 next if $rule->{iface
} && $rule->{iface
} ne $netid;
308 $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
313 if (scalar(@$outrules)) {
314 $out .= "# OUT from VM $vmid\n";
315 foreach my $rule (@$outrules) {
316 foreach my $netid (keys %{$netinfo->{$vmid}}) {
317 my $net = $netinfo->{$vmid}->{$netid};
318 next if $rule->{iface
} && $rule->{iface
} ne $netid;
319 $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
325 PVE
::Tools
::file_set_contents
("$targetdir/rules", $out);
330 my ($filename, $fh) = @_;
334 my $res = { in => [], out
=> [] };
336 my $macros = PVE
::Firewall
::get_shorewall_macros
();
338 while (defined(my $line = <$fh>)) {
339 next if $line =~ m/^#/;
340 next if $line =~ m/^\s*$/;
342 if ($line =~ m/^\[(in|out)\]\s*$/i) {
348 my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
352 warn "skip incomplete line\n";
357 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
359 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
360 ($service, $action) = ($1, $2);
361 if (!$macros->{$service}) {
362 warn "unknown service '$service'\n";
366 warn "unknown action '$action'\n";
370 $iface = undef if $iface && $iface eq '-';
371 if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) {
372 warn "unknown interface '$iface'\n";
376 $proto = undef if $proto && $proto eq '-';
377 if ($proto && $proto !~ m/^(icmp|tcp|udp)$/) {
378 warn "unknown protokol '$proto'\n";
382 $source = undef if $source && $source eq '-';
384 # if ($source !~ m/^(XYZ)$/) {
385 # warn "unknown source '$source'\n";
389 $dest = undef if $dest && $dest eq '-';
390 # if ($dest !~ m/^XYZ)$/) {
391 # warn "unknown destination '$dest'\n";
395 $dport = undef if $dport && $dport eq '-';
396 $sport = undef if $sport && $sport eq '-';
409 push @{$res->{$section}}, $rule;
415 sub read_local_vm_config
{
421 my $list = PVE
::QemuServer
::config_list
();
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;
431 my $vmdata = { openvz
=> $openvz, qemu
=> $qemu };
436 sub read_vm_firewall_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
);
444 $rules->{$vmid} = parse_fw_rules
($filename, $fh);
452 my $vmdata = read_local_vm_config
();
453 my $rules = read_vm_firewall_rules
($vmdata);
455 # print Dumper($vmdata);
457 my $swdir = '/etc/shorewall';
460 &$compile_shorewall($swdir, $vmdata, $rules);
462 PVE
::Tools
::run_command
(['shorewall', 'compile']);
465 sub compile_and_start
{
470 PVE
::Tools
::run_command
(['shorewall', $restart ?
'restart' : 'start']);