better documentation
[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
DM
7use PVE::QemuServer;
8
9aab3127
DM
9my $macros;
10sub get_shorewall_macros {
11
12 return $macros if $macros;
13
14 foreach my $path (</usr/share/shorewall/macro.*>) {
15 if ($path =~ m|/macro\.(\S+)$|) {
16 $macros->{$1} = 1;
17 }
18 }
19 return $macros;
20}
21
dddd9413 22
8cebfa6f 23my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
dddd9413
DM
24
25my $generate_input_rule = sub {
26 my ($zoneinfo, $rule, $net, $netid) = @_;
27
dddd9413 28 my $zone = $net->{zone} || die "internal error";
a8195f4f 29 my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
dddd9413 30 my $tap = $net->{tap} || die "internal error";
9aab3127 31
8cebfa6f
DM
32 my $dest = "$zid:$tap";
33
34 if ($rule->{dest}) {
35 $dest .= ":$rule->{dest}";
36 }
37
9aab3127
DM
38 my $action = $rule->{service} ?
39 "$rule->{service}($rule->{action})" : $rule->{action};
40
8cebfa6f
DM
41 my $source;
42
43 if ($zoneinfo->{$zone}->{type} eq 'bport') {
44 my $bridge_zone = $zoneinfo->{$zone}->{bridge_zone} || die "internal error";
45 my $bridge_ext_zone = $zoneinfo->{$bridge_zone}->{bridge_ext_zone} || die "internal error";
46 my $zoneref = $zoneinfo->{$bridge_ext_zone}->{zoneref} || die "internal error";
47 if (!$rule->{source}) {
48 $source = "${zoneref}";
49 } else {
50 $source = "${zoneref}:$rule->{source}";
51 }
52 } else {
9a4644fa 53 $source = "all:$rule->{source}";
8cebfa6f
DM
54 }
55
56 return sprintf($rule_format, $action, $source, $dest, $rule->{proto} || '-',
57 $rule->{dport} || '-', $rule->{sport} || '-');
dddd9413
DM
58};
59
60my $generate_output_rule = sub {
61 my ($zoneinfo, $rule, $net, $netid) = @_;
62
dddd9413 63 my $zone = $net->{zone} || die "internal error";
a8195f4f 64 my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
dddd9413 65 my $tap = $net->{tap} || die "internal error";
9aab3127
DM
66
67 my $action = $rule->{service} ?
68 "$rule->{service}($rule->{action})" : $rule->{action};
dddd9413 69
8cebfa6f
DM
70 my $dest;
71
72 if (!$rule->{dest}) {
9a4644fa 73 $dest = 'all';
8cebfa6f 74 } else {
9a4644fa 75 $dest = "all:$rule->{dest}";
8cebfa6f
DM
76 }
77
78 return sprintf($rule_format, $action, "$zid:$tap", $dest,
dddd9413
DM
79 $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-');
80};
81
b6360c3f
DM
82# we need complete VM configuration of all VMs (openvz/qemu)
83# in vmdata
84
85sub compile {
dddd9413 86 my ($targetdir, $vmdata, $rules) = @_;
b6360c3f 87
80bfe1ff 88 # remove existing data ?
a8195f4f 89 foreach my $file (qw(params zones rules interfaces maclist policy)) {
80bfe1ff
DM
90 unlink "$targetdir/$file";
91 }
92
b6360c3f
DM
93 my $netinfo;
94
886aba9c
DM
95 my $zoneinfo = {
96 fw => { type => 'firewall' },
97 };
98
026a6466
DM
99 my $maclist = {};
100
35081236
DM
101 my $register_bridge;
102
103 $register_bridge = sub {
104 my ($bridge, $vlan) = @_;
f789653a
DM
105
106 my $zone = 'z' . $bridge;
107
108 return $zone if $zoneinfo->{$zone};
109
8cebfa6f
DM
110 my $ext_zone = "z${bridge}ext";
111
f789653a
DM
112 $zoneinfo->{$zone} = {
113 type => 'bridge',
114 bridge => $bridge,
8cebfa6f
DM
115 bridge_ext_zone => $ext_zone,
116 };
117
118 # physical input devices
119 my $dir = "/sys/class/net/$bridge/brif";
120 my $physical = {};
121 PVE::Tools::dir_glob_foreach($dir, '((eth|bond).+)', sub {
122 my ($slave) = @_;
123 $physical->{$slave} = 1;
124 });
125
126 $zoneinfo->{$ext_zone} = {
127 type => 'bport',
128 bridge_zone => $zone,
129 ifaces => $physical,
f789653a 130 };
35081236
DM
131
132 return &$register_bridge("${bridge}v${vlan}") if defined($vlan);
133
134 return $zone;
135 };
136
137 my $register_bridge_port = sub {
138 my ($bridge, $vlan, $vmzone, $tap) = @_;
139
140 my $bridge_zone = &$register_bridge($bridge, $vlan);
141 my $zone = $bridge_zone . $vmzone;
142
143 if (!$zoneinfo->{$zone}) {
144 $zoneinfo->{$zone} = {
145 type => 'bport',
146 bridge_zone => $bridge_zone,
147 ifaces => {},
148 };
149 }
150
151 $zoneinfo->{$zone}->{ifaces}->{$tap} = 1;
f789653a
DM
152
153 return $zone;
154 };
155
b6360c3f
DM
156 foreach my $vmid (keys %{$vmdata->{qemu}}) {
157 $netinfo->{$vmid} = {};
158 my $conf = $vmdata->{qemu}->{$vmid};
159 foreach my $opt (keys %$conf) {
160 next if $opt !~ m/^net(\d+)$/;
dddd9413 161 my $netnum = $1;
b6360c3f
DM
162 my $net = PVE::QemuServer::parse_net($conf->{$opt});
163 next if !$net;
886aba9c 164 die "implement me" if !$net->{bridge};
886aba9c 165
f789653a 166 my $vmzone = $conf->{zone} || "vm$vmid";
dddd9413 167 $net->{tap} = "tap${vmid}i${netnum}";
026a6466 168 $maclist->{$net->{tap}} = $net->{macaddr} || die "internal error";
dddd9413
DM
169 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap});
170 $netinfo->{$vmid}->{$opt} = $net;
886aba9c
DM
171 }
172 }
173
174 #print Dumper($netinfo);
175
f789653a 176 # NOTE: zone names have length limit, so we need to
886aba9c
DM
177 # translate them into shorter names
178
f789653a
DM
179 my $zoneid = 0;
180 my $zonemap = { fw => 'fw' };
181
182 my $lookup_zonename = sub {
183 my ($zone) = @_;
184
185 return $zonemap->{$zone} if defined($zonemap->{$zone});
186 $zonemap->{$zone} = 'z' . $zoneid++;
187
188 return $zonemap->{$zone};
189 };
190
dddd9413
DM
191 foreach my $z (sort keys %$zoneinfo) {
192 $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
a8195f4f
DM
193 $zoneinfo->{$z}->{zonevar} = uc($z);
194 $zoneinfo->{$z}->{zoneref} = '$' . $zoneinfo->{$z}->{zonevar};
dddd9413
DM
195 }
196
f789653a 197 my $out;
886aba9c 198
a8195f4f
DM
199 # dump params file
200 $out = "# PVE zones\n";
201 foreach my $z (sort keys %$zoneinfo) {
202 $out .= "$zoneinfo->{$z}->{zonevar}=$zoneinfo->{$z}->{id}\n";
203 }
204 PVE::Tools::file_set_contents("$targetdir/params", $out);
205
206 # dump zone file
207
208 my $format = "%-30s %-10s %-15s\n";
209 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
f789653a 210
886aba9c 211 foreach my $z (sort keys %$zoneinfo) {
a8195f4f 212 my $zid = $zoneinfo->{$z}->{zoneref};
f789653a 213 if ($zoneinfo->{$z}->{type} eq 'firewall') {
a8195f4f 214 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '');
f789653a 215 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
a8195f4f 216 $out .= sprintf($format, $zid, 'ipv4', '');
f789653a
DM
217 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
218 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
a8195f4f
DM
219 my $bzid = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
220 $out .= sprintf($format, "$zid:$bzid", 'bport', '');
f789653a
DM
221 } else {
222 die "internal error";
223 }
886aba9c
DM
224 }
225
f789653a
DM
226 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
227
228 PVE::Tools::file_set_contents("$targetdir/zones", $out);
886aba9c 229
f789653a 230 # dump interfaces
886aba9c 231
a8195f4f
DM
232 $format = "%-25s %-20s %-10s %-15s\n";
233 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
f789653a 234
026a6466
DM
235 my $maclist_format = "%-15s %-15s %-15s\n";
236 my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
237
886aba9c 238 foreach my $z (sort keys %$zoneinfo) {
a8195f4f 239 my $zid = $zoneinfo->{$z}->{zoneref};
f789653a
DM
240 if ($zoneinfo->{$z}->{type} eq 'firewall') {
241 # do nothing;
242 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
243 my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
a8195f4f 244 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional');
f789653a
DM
245 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
246 my $ifaces = $zoneinfo->{$z}->{ifaces};
247 foreach my $iface (sort keys %$ifaces) {
248 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
249 my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
250 my $iftxt = "$bridge:$iface";
8cebfa6f
DM
251
252 if ($maclist->{$iface}) {
253 $out .= sprintf($format, $zid, $iftxt, '-', 'maclist');
254 $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
255 } else {
256 $out .= sprintf($format, $zid, $iftxt, '-', '');
257 }
f789653a
DM
258 }
259 } else {
260 die "internal error";
b6360c3f
DM
261 }
262 }
263
f789653a
DM
264 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
265
266 PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
267
026a6466
DM
268 # dump maclist
269 PVE::Tools::file_set_contents("$targetdir/maclist", $macs);
270
f789653a
DM
271 # dump policy
272
273 $format = "%-15s %-15s %-15s %s\n";
274 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
8cebfa6f
DM
275 $out .= sprintf($format, 'fw', 'all', 'ACCEPT', '');
276
277 # we need to disable intra-zone traffic on bridges. Else traffic
278 # from untracked interfaces simply pass the firewall
279 foreach my $z (sort keys %$zoneinfo) {
280 my $zid = $zoneinfo->{$z}->{zoneref};
281 if ($zoneinfo->{$z}->{type} eq 'bridge') {
282 $out .= sprintf($format, $zid, $zid, 'REJECT', 'info');
283 }
284 }
f789653a 285 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
886aba9c 286
f789653a 287 PVE::Tools::file_set_contents("$targetdir/policy", $out);
886aba9c 288
dddd9413
DM
289 # dump rules
290 $out = '';
291
292 $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
293 foreach my $vmid (sort keys %$rules) {
78db5872
DM
294 my $inrules = $rules->{$vmid}->{in};
295 my $outrules = $rules->{$vmid}->{out};
296
297 if (scalar(@$inrules)) {
298 $out .= "# IN to VM $vmid\n";
dddd9413
DM
299 foreach my $rule (@$inrules) {
300 foreach my $netid (keys %{$netinfo->{$vmid}}) {
301 my $net = $netinfo->{$vmid}->{$netid};
9a4644fa 302 next if $rule->{iface} && $rule->{iface} ne $netid;
dddd9413
DM
303 $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
304 }
305 }
306 }
307
78db5872
DM
308 if (scalar(@$outrules)) {
309 $out .= "# OUT from VM $vmid\n";
dddd9413
DM
310 foreach my $rule (@$outrules) {
311 foreach my $netid (keys %{$netinfo->{$vmid}}) {
312 my $net = $netinfo->{$vmid}->{$netid};
9a4644fa 313 next if $rule->{iface} && $rule->{iface} ne $netid;
dddd9413
DM
314 $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
315 }
316 }
317 }
318 }
319
320 PVE::Tools::file_set_contents("$targetdir/rules", $out);
b6360c3f
DM
321
322}
323
886aba9c 324
b6360c3f
DM
325sub activate {
326
327}
328
329
3301;