generate maclist
[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 $maclist = {};
53
54 my $register_bridge;
55
56 $register_bridge = sub {
57 my ($bridge, $vlan) = @_;
58
59 my $zone = 'z' . $bridge;
60
61 return $zone if $zoneinfo->{$zone};
62
63 $zoneinfo->{$zone} = {
64 type => 'bridge',
65 bridge => $bridge,
66 };
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;
88
89 return $zone;
90 };
91
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+)$/;
97 my $netnum = $1;
98 my $net = PVE::QemuServer::parse_net($conf->{$opt});
99 next if !$net;
100 die "implement me" if !$net->{bridge};
101
102 my $vmzone = $conf->{zone} || "vm$vmid";
103 $net->{tap} = "tap${vmid}i${netnum}";
104 $maclist->{$net->{tap}} = $net->{macaddr} || die "internal error";
105 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, $net->{tap});
106 $netinfo->{$vmid}->{$opt} = $net;
107 }
108 }
109
110 #print Dumper($netinfo);
111
112 # NOTE: zone names have length limit, so we need to
113 # translate them into shorter names
114
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
127 foreach my $z (sort keys %$zoneinfo) {
128 $zoneinfo->{$z}->{id} = &$lookup_zonename($z);
129 }
130
131 # dump zone file
132
133 my $out;
134
135 my $format = "%-15s %-10s %-15s %s\n";
136 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS', '');
137
138 foreach my $z (sort keys %$zoneinfo) {
139 my $zid = $zoneinfo->{$z}->{id};
140 if ($zoneinfo->{$z}->{type} eq 'firewall') {
141 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '' , "# $z");
142 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
143 $out .= sprintf($format, $zid, 'ipv4', '', "# $z");
144 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
145 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
146 my $bzid = $zoneinfo->{$bridge_zone}->{id} || die "internal error";
147 $out .= sprintf($format, "$zid:$bzid", 'bport', '', "# $z");
148 } else {
149 die "internal error";
150 }
151 }
152
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);
156
157 # dump interfaces
158
159 $format = "%-15s %-20s %-10s %-15s %s\n";
160 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS', '');
161
162 my $maclist_format = "%-15s %-15s %-15s\n";
163 my $macs = sprintf($maclist_format, '#DISPOSITION', 'INTERFACE', 'MACZONE');
164
165 foreach my $z (sort keys %$zoneinfo) {
166 my $zid = $zoneinfo->{$z}->{id};
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";
171 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge', "# $z");
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";
179 $out .= sprintf($format, $zid, $iftxt, '-', 'maclist', "# $z");
180 $macs .= sprintf($maclist_format, 'ACCEPT', $iface, $maclist->{$iface});
181 }
182 } else {
183 die "internal error";
184 }
185 }
186
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
191 # dump maclist
192 PVE::Tools::file_set_contents("$targetdir/maclist", $macs);
193
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');
199
200 PVE::Tools::file_set_contents("$targetdir/policy", $out);
201
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);
229
230 }
231
232
233 sub activate {
234
235 }
236
237
238 1;