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