read in shorewall macros
[pve-firewall.git] / PVE / Firewall.pm
1 package PVE::Firewall;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use PVE::Tools;
7 use PVE::QemuServer;
8
9 my $macros;
10 sub 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
22
23 my $rule_format = "%-15s %-15s %-15s %-15s %-15s %-15s\n";
24
25 my $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";
34
35 my $action = $rule->{service} ?
36 "$rule->{service}($rule->{action})" : $rule->{action};
37
38 return sprintf($rule_format, $action, $rule->{source}, "$zid:$tap",
39 $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-');
40 };
41
42 my $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";
51
52 my $action = $rule->{service} ?
53 "$rule->{service}($rule->{action})" : $rule->{action};
54
55 return sprintf($rule_format, $action, "$zid:$tap", $rule->{dest},
56 $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-');
57 };
58
59 # we need complete VM configuration of all VMs (openvz/qemu)
60 # in vmdata
61
62 sub compile {
63 my ($targetdir, $vmdata, $rules) = @_;
64
65 # remove existing data ?
66 foreach my $file (qw(zones rules interfaces maclist policy)) {
67 unlink "$targetdir/$file";
68 }
69
70 my $netinfo;
71
72 my $zoneinfo = {
73 fw => { type => 'firewall' },
74 };
75
76 my $maclist = {};
77
78 my $register_bridge;
79
80 $register_bridge = sub {
81 my ($bridge, $vlan) = @_;
82
83 my $zone = 'z' . $bridge;
84
85 return $zone if $zoneinfo->{$zone};
86
87 $zoneinfo->{$zone} = {
88 type => 'bridge',
89 bridge => $bridge,
90 };
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;
112
113 return $zone;
114 };
115
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+)$/;
121 my $netnum = $1;
122 my $net = PVE::QemuServer::parse_net($conf->{$opt});
123 next if !$net;
124 die "implement me" if !$net->{bridge};
125
126 my $vmzone = $conf->{zone} || "vm$vmid";
127 $net->{tap} = "tap${vmid}i${netnum}";
128 $maclist->{$net->{tap}} = $net->{macaddr} || die "internal error";
129 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap});
130 $netinfo->{$vmid}->{$opt} = $net;
131 }
132 }
133
134 #print Dumper($netinfo);
135
136 # NOTE: zone names have length limit, so we need to
137 # translate them into shorter names
138
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
151 foreach my $z (sort keys %$zoneinfo) {
152 $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
153 }
154
155 # dump zone file
156
157 my $out;
158
159 my $format = "%-15s %-10s %-15s %s\n";
160 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS', '');
161
162 foreach my $z (sort keys %$zoneinfo) {
163 my $zid = $zoneinfo->{$z}->{id};
164 if ($zoneinfo->{$z}->{type} eq 'firewall') {
165 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '' , "# $z");
166 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
167 $out .= sprintf($format, $zid, 'ipv4', '', "# $z");
168 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
169 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
170 my $bzid = $zoneinfo->{$bridge_zone}->{id} || die "internal error";
171 $out .= sprintf($format, "$zid:$bzid", 'bport', '', "# $z");
172 } else {
173 die "internal error";
174 }
175 }
176
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);
180
181 # dump interfaces
182
183 $format = "%-15s %-20s %-10s %-15s %s\n";
184 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS', '');
185
186 my $maclist_format = "%-15s %-15s %-15s\n";
187 my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
188
189 foreach my $z (sort keys %$zoneinfo) {
190 my $zid = $zoneinfo->{$z}->{id};
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";
195 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge,optional', "# $z");
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";
203 $out .= sprintf($format, $zid, $iftxt, '-', 'maclist', "# $z");
204 $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
205 }
206 } else {
207 die "internal error";
208 }
209 }
210
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
215 # dump maclist
216 PVE::Tools::file_set_contents("$targetdir/maclist", $macs);
217
218 # dump policy
219
220 $format = "%-15s %-15s %-15s %s\n";
221 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
222 #$out .= sprintf($format, 'fw', 'all', 'ACCEPT', '');
223 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
224
225 PVE::Tools::file_set_contents("$targetdir/policy", $out);
226
227 # dump rules
228 $out = '';
229
230 $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
231 foreach my $vmid (sort keys %$rules) {
232 if (my $inrules = $rules->{$vmid}->{in}) {
233 foreach my $rule (@$inrules) {
234 foreach my $netid (keys %{$netinfo->{$vmid}}) {
235 my $net = $netinfo->{$vmid}->{$netid};
236 next if !($rule->{iface} eq 'any' || $rule->{iface} eq $netid);
237 $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
238 }
239 }
240 }
241
242 if (my $outrules = $rules->{$vmid}->{out}) {
243 foreach my $rule (@$outrules) {
244 foreach my $netid (keys %{$netinfo->{$vmid}}) {
245 my $net = $netinfo->{$vmid}->{$netid};
246 next if !($rule->{iface} eq 'any' || $rule->{iface} eq $netid);
247 $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
248 }
249 }
250 }
251 }
252
253 PVE::Tools::file_set_contents("$targetdir/rules", $out);
254
255 }
256
257
258 sub activate {
259
260 }
261
262
263 1;