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