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