add original zone names as comments
[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
10 my $rule_format = "%-15s %-15s %-15s %-15s %-15s %-15s\n";
11
12 my $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
26 my $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
40 # we need complete VM configuration of all VMs (openvz/qemu)
41 # in vmdata
42
43 sub compile {
44 my ($targetdir, $vmdata, $rules) = @_;
45
46 my $netinfo;
47
48 my $zoneinfo = {
49 fw => { type => 'firewall' },
50 };
51
52 my $register_bridge;
53
54 $register_bridge = sub {
55 my ($bridge, $vlan) = @_;
56
57 my $zone = 'z' . $bridge;
58
59 return $zone if $zoneinfo->{$zone};
60
61 $zoneinfo->{$zone} = {
62 type => 'bridge',
63 bridge => $bridge,
64 };
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;
86
87 return $zone;
88 };
89
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+)$/;
95 my $netnum = $1;
96 my $net = PVE::QemuServer::parse_net($conf->{$opt});
97 next if !$net;
98 die "implement me" if !$net->{bridge};
99
100 my $vmzone = $conf->{zone} || "vm$vmid";
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;
104 }
105 }
106
107 #print Dumper($netinfo);
108
109 # NOTE: zone names have length limit, so we need to
110 # translate them into shorter names
111
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
124 foreach my $z (sort keys %$zoneinfo) {
125 $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
126 }
127
128 # dump zone file
129
130 my $out;
131
132 my $format = "%-15s %-10s %-15s %s\n";
133 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS', '');
134
135 foreach my $z (sort keys %$zoneinfo) {
136 my $zid = $zoneinfo->{$z}->{id};
137 if ($zoneinfo->{$z}->{type} eq 'firewall') {
138 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '' , "# $z");
139 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
140 $out .= sprintf($format, $zid, 'ipv4', '', "# $z");
141 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
142 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
143 my $bzid = $zoneinfo->{$bridge_zone}->{id} || die "internal error";
144 $out .= sprintf($format, "$zid:$bzid", 'bport', '', "# $z");
145 } else {
146 die "internal error";
147 }
148 }
149
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);
153
154 # dump interfaces
155
156 $format = "%-15s %-20s %-10s %-15s %s\n";
157 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS', '');
158
159 foreach my $z (sort keys %$zoneinfo) {
160 my $zid = $zoneinfo->{$z}->{id};
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";
165 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge', "# $z");
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";
173 $out .= sprintf($format, $zid, $iftxt, '', '', "# $z");
174 }
175 } else {
176 die "internal error";
177 }
178 }
179
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');
189
190 PVE::Tools::file_set_contents("$targetdir/policy", $out);
191
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);
219
220 }
221
222
223 sub activate {
224
225 }
226
227
228 1;