add original zone names as comments
[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
DM
45
46 my $netinfo;
47
886aba9c
DM
48 my $zoneinfo = {
49 fw => { type => 'firewall' },
50 };
51
35081236
DM
52 my $register_bridge;
53
54 $register_bridge = sub {
55 my ($bridge, $vlan) = @_;
f789653a
DM
56
57 my $zone = 'z' . $bridge;
58
59 return $zone if $zoneinfo->{$zone};
60
61 $zoneinfo->{$zone} = {
62 type => 'bridge',
63 bridge => $bridge,
64 };
35081236
DM
65
66 return &$register_bridge("${bridge}v${vlan}") if defined($vlan);
67
68 return $zone;
69 };
70
71 my $register_bridge_port = sub {
72 my ($bridge, $vlan, $vmzone, $tap) = @_;
73
74 my $bridge_zone = &$register_bridge($bridge, $vlan);
75 my $zone = $bridge_zone . $vmzone;
76
77 if (!$zoneinfo->{$zone}) {
78 $zoneinfo->{$zone} = {
79 type => 'bport',
80 bridge_zone => $bridge_zone,
81 ifaces => {},
82 };
83 }
84
85 $zoneinfo->{$zone}->{ifaces}->{$tap} = 1;
f789653a
DM
86
87 return $zone;
88 };
89
b6360c3f
DM
90 foreach my $vmid (keys %{$vmdata->{qemu}}) {
91 $netinfo->{$vmid} = {};
92 my $conf = $vmdata->{qemu}->{$vmid};
93 foreach my $opt (keys %$conf) {
94 next if $opt !~ m/^net(\d+)$/;
dddd9413 95 my $netnum = $1;
b6360c3f
DM
96 my $net = PVE::QemuServer::parse_net($conf->{$opt});
97 next if !$net;
886aba9c 98 die "implement me" if !$net->{bridge};
886aba9c 99
f789653a 100 my $vmzone = $conf->{zone} || "vm$vmid";
dddd9413
DM
101 $net->{tap} = "tap${vmid}i${netnum}";
102 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap});
103 $netinfo->{$vmid}->{$opt} = $net;
886aba9c
DM
104 }
105 }
106
107 #print Dumper($netinfo);
108
f789653a 109 # NOTE: zone names have length limit, so we need to
886aba9c
DM
110 # translate them into shorter names
111
f789653a
DM
112 my $zoneid = 0;
113 my $zonemap = { fw => 'fw' };
114
115 my $lookup_zonename = sub {
116 my ($zone) = @_;
117
118 return $zonemap->{$zone} if defined($zonemap->{$zone});
119 $zonemap->{$zone} = 'z' . $zoneid++;
120
121 return $zonemap->{$zone};
122 };
123
dddd9413
DM
124 foreach my $z (sort keys %$zoneinfo) {
125 $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
126 }
127
886aba9c
DM
128 # dump zone file
129
f789653a 130 my $out;
886aba9c 131
f6846fbf
DM
132 my $format = "%-15s %-10s %-15s %s\n";
133 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS', '');
f789653a 134
886aba9c 135 foreach my $z (sort keys %$zoneinfo) {
dddd9413 136 my $zid = $zoneinfo->{$z}->{id};
f789653a 137 if ($zoneinfo->{$z}->{type} eq 'firewall') {
f6846fbf 138 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '' , "# $z");
f789653a 139 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
f6846fbf 140 $out .= sprintf($format, $zid, 'ipv4', '', "# $z");
f789653a
DM
141 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
142 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
dddd9413 143 my $bzid = $zoneinfo->{$bridge_zone}->{id} || die "internal error";
f6846fbf 144 $out .= sprintf($format, "$zid:$bzid", 'bport', '', "# $z");
f789653a
DM
145 } else {
146 die "internal error";
147 }
886aba9c
DM
148 }
149
f789653a
DM
150 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
151
152 PVE::Tools::file_set_contents("$targetdir/zones", $out);
886aba9c 153
f789653a 154 # dump interfaces
886aba9c 155
f6846fbf
DM
156 $format = "%-15s %-20s %-10s %-15s %s\n";
157 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS', '');
f789653a 158
886aba9c 159 foreach my $z (sort keys %$zoneinfo) {
dddd9413 160 my $zid = $zoneinfo->{$z}->{id};
f789653a
DM
161 if ($zoneinfo->{$z}->{type} eq 'firewall') {
162 # do nothing;
163 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
164 my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
f6846fbf 165 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge', "# $z");
f789653a
DM
166
167 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
168 my $ifaces = $zoneinfo->{$z}->{ifaces};
169 foreach my $iface (sort keys %$ifaces) {
170 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
171 my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
172 my $iftxt = "$bridge:$iface";
f6846fbf 173 $out .= sprintf($format, $zid, $iftxt, '', '', "# $z");
f789653a
DM
174 }
175 } else {
176 die "internal error";
b6360c3f
DM
177 }
178 }
179
f789653a
DM
180 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
181
182 PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
183
184 # dump policy
185
186 $format = "%-15s %-15s %-15s %s\n";
187 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
188 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
886aba9c 189
f789653a 190 PVE::Tools::file_set_contents("$targetdir/policy", $out);
886aba9c 191
dddd9413
DM
192 # dump rules
193 $out = '';
194
195 $out = sprintf($rule_format, '#ACTION', 'SOURCE', 'DEST', 'PROTO', 'DPORT', 'SPORT');
196 foreach my $vmid (sort keys %$rules) {
197 if (my $inrules = $rules->{$vmid}->{in}) {
198 foreach my $rule (@$inrules) {
199 foreach my $netid (keys %{$netinfo->{$vmid}}) {
200 my $net = $netinfo->{$vmid}->{$netid};
201 next if !($rule->{iface} eq 'any' || $rule->{iface} eq $netid);
202 $out .= &$generate_input_rule($zoneinfo, $rule, $net, $netid);
203 }
204 }
205 }
206
207 if (my $outrules = $rules->{$vmid}->{out}) {
208 foreach my $rule (@$outrules) {
209 foreach my $netid (keys %{$netinfo->{$vmid}}) {
210 my $net = $netinfo->{$vmid}->{$netid};
211 next if !($rule->{iface} eq 'any' || $rule->{iface} eq $netid);
212 $out .= &$generate_output_rule($zoneinfo, $rule, $net, $netid);
213 }
214 }
215 }
216 }
217
218 PVE::Tools::file_set_contents("$targetdir/rules", $out);
b6360c3f
DM
219
220}
221
886aba9c 222
b6360c3f
DM
223sub activate {
224
225}
226
227
2281;