add comments to generated rules file
[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";
32 my $zid = $zoneinfo->{$zone}->{id} || die "internal error";
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";
49 my $zid = $zoneinfo->{$zone}->{id} || die "internal error";
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
DM
65 # remove existing data ?
66 foreach my $file (qw(zones rules interfaces maclist policy)) {
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);
153 }
154
886aba9c
DM
155 # dump zone file
156
f789653a 157 my $out;
886aba9c 158
f6846fbf
DM
159 my $format = "%-15s %-10s %-15s %s\n";
160 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS', '');
f789653a 161
886aba9c 162 foreach my $z (sort keys %$zoneinfo) {
dddd9413 163 my $zid = $zoneinfo->{$z}->{id};
f789653a 164 if ($zoneinfo->{$z}->{type} eq 'firewall') {
f6846fbf 165 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '' , "# $z");
f789653a 166 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
f6846fbf 167 $out .= sprintf($format, $zid, 'ipv4', '', "# $z");
f789653a
DM
168 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
169 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
dddd9413 170 my $bzid = $zoneinfo->{$bridge_zone}->{id} || die "internal error";
f6846fbf 171 $out .= sprintf($format, "$zid:$bzid", 'bport', '', "# $z");
f789653a
DM
172 } else {
173 die "internal error";
174 }
886aba9c
DM
175 }
176
f789653a
DM
177 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
178
179 PVE::Tools::file_set_contents("$targetdir/zones", $out);
886aba9c 180
f789653a 181 # dump interfaces
886aba9c 182
f6846fbf
DM
183 $format = "%-15s %-20s %-10s %-15s %s\n";
184 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS', '');
f789653a 185
026a6466
DM
186 my $maclist_format = "%-15s %-15s %-15s\n";
187 my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
188
886aba9c 189 foreach my $z (sort keys %$zoneinfo) {
dddd9413 190 my $zid = $zoneinfo->{$z}->{id};
f789653a
DM
191 if ($zoneinfo->{$z}->{type} eq 'firewall') {
192 # do nothing;
193 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
194 my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
80bfe1ff 195 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional', "# $z");
f789653a
DM
196
197 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
198 my $ifaces = $zoneinfo->{$z}->{ifaces};
199 foreach my $iface (sort keys %$ifaces) {
200 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
201 my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
202 my $iftxt = "$bridge:$iface";
026a6466
DM
203 $out .= sprintf($format, $zid, $iftxt, '-', 'maclist', "# $z");
204 $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
f789653a
DM
205 }
206 } else {
207 die "internal error";
b6360c3f
DM
208 }
209 }
210
f789653a
DM
211 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
212
213 PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
214
026a6466
DM
215 # dump maclist
216 PVE::Tools::file_set_contents("$targetdir/maclist", $macs);
217
f789653a
DM
218 # dump policy
219
220 $format = "%-15s %-15s %-15s %s\n";
221 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
80bfe1ff 222 #$out .= sprintf($format, 'fw', 'all', 'ACCEPT', '');
f789653a 223 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
886aba9c 224
f789653a 225 PVE::Tools::file_set_contents("$targetdir/policy", $out);
886aba9c 226
dddd9413
DM
227 # dump rules
228 $out = '';
229
230 $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
231 foreach my $vmid (sort keys %$rules) {
78db5872
DM
232 my $inrules = $rules->{$vmid}->{in};
233 my $outrules = $rules->{$vmid}->{out};
234
235 if (scalar(@$inrules)) {
236 $out .= "# IN to VM $vmid\n";
dddd9413
DM
237 foreach my $rule (@$inrules) {
238 foreach my $netid (keys %{$netinfo->{$vmid}}) {
239 my $net = $netinfo->{$vmid}->{$netid};
240 next if !($rule->{iface} eq 'any' || $rule->{iface} eq $netid);
241 $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
242 }
243 }
244 }
245
78db5872
DM
246 if (scalar(@$outrules)) {
247 $out .= "# OUT from VM $vmid\n";
dddd9413
DM
248 foreach my $rule (@$outrules) {
249 foreach my $netid (keys %{$netinfo->{$vmid}}) {
250 my $net = $netinfo->{$vmid}->{$netid};
251 next if !($rule->{iface} eq 'any' || $rule->{iface} eq $netid);
252 $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
253 }
254 }
255 }
256 }
257
258 PVE::Tools::file_set_contents("$targetdir/rules", $out);
b6360c3f
DM
259
260}
261
886aba9c 262
b6360c3f
DM
263sub activate {
264
265}
266
267
2681;