]>
Commit | Line | Data |
---|---|---|
b6360c3f DM |
1 | package PVE::Firewall; |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | use Data::Dumper; | |
09d5f68e | 6 | use Digest::SHA; |
f789653a | 7 | use PVE::Tools; |
b6360c3f | 8 | use PVE::QemuServer; |
5e1267a5 DM |
9 | use File::Path; |
10 | use IO::File; | |
ecbea048 | 11 | use Net::IP; |
06320eb0 | 12 | use PVE::Tools qw(run_command lock_file); |
ecbea048 | 13 | |
5e1267a5 | 14 | use Data::Dumper; |
b6360c3f | 15 | |
06320eb0 DM |
16 | my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; |
17 | ||
961b4928 | 18 | # todo: define more MACROS |
857f62c8 | 19 | # imported/converted from: /usr/share/shorewall/macro.* |
961b4928 | 20 | my $pve_fw_macros = { |
857f62c8 DM |
21 | 'Amanda' => [ |
22 | { action => 'PARAM', proto => 'udp', dport => '10080' }, | |
23 | { action => 'PARAM', proto => 'tcp', dport => '10080' }, | |
24 | ], | |
25 | 'Auth' => [ | |
26 | { action => 'PARAM', proto => 'tcp', dport => '113' }, | |
27 | ], | |
28 | 'BGP' => [ | |
29 | { action => 'PARAM', proto => 'tcp', dport => '179' }, | |
30 | ], | |
31 | 'BitTorrent' => [ | |
961b4928 | 32 | { action => 'PARAM', proto => 'tcp', dport => '6881:6889' }, |
857f62c8 DM |
33 | { action => 'PARAM', proto => 'udp', dport => '6881' }, |
34 | ], | |
35 | 'BitTorrent32' => [ | |
36 | { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, | |
37 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
38 | ], | |
39 | 'CVS' => [ | |
40 | { action => 'PARAM', proto => 'tcp', dport => '2401' }, | |
41 | ], | |
42 | 'Citrix' => [ | |
43 | { action => 'PARAM', proto => 'tcp', dport => '1494' }, | |
44 | { action => 'PARAM', proto => 'udp', dport => '1604' }, | |
45 | { action => 'PARAM', proto => 'tcp', dport => '2598' }, | |
46 | ], | |
47 | 'DAAP' => [ | |
48 | { action => 'PARAM', proto => 'tcp', dport => '3689' }, | |
49 | { action => 'PARAM', proto => 'udp', dport => '3689' }, | |
50 | ], | |
51 | 'DCC' => [ | |
52 | { action => 'PARAM', proto => 'tcp', dport => '6277' }, | |
53 | ], | |
54 | 'DHCPfwd' => [ | |
55 | { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
56 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
57 | ], | |
58 | 'DNS' => [ | |
59 | { action => 'PARAM', proto => 'udp', dport => '53' }, | |
60 | { action => 'PARAM', proto => 'tcp', dport => '53' }, | |
61 | ], | |
62 | 'Distcc' => [ | |
63 | { action => 'PARAM', proto => 'tcp', dport => '3632' }, | |
64 | ], | |
65 | 'Edonkey' => [ | |
66 | { action => 'PARAM', proto => 'tcp', dport => '4662' }, | |
67 | { action => 'PARAM', proto => 'udp', dport => '4665' }, | |
961b4928 | 68 | ], |
857f62c8 DM |
69 | 'FTP' => [ |
70 | { action => 'PARAM', proto => 'tcp', dport => '21' }, | |
71 | ], | |
72 | 'Finger' => [ | |
73 | { action => 'PARAM', proto => 'tcp', dport => '79' }, | |
74 | ], | |
75 | 'GNUnet' => [ | |
76 | { action => 'PARAM', proto => 'tcp', dport => '2086' }, | |
77 | { action => 'PARAM', proto => 'udp', dport => '2086' }, | |
78 | { action => 'PARAM', proto => 'tcp', dport => '1080' }, | |
79 | { action => 'PARAM', proto => 'udp', dport => '1080' }, | |
80 | ], | |
81 | 'GRE' => [ | |
82 | { action => 'PARAM', proto => '47' }, | |
83 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
84 | ], | |
85 | 'Git' => [ | |
86 | { action => 'PARAM', proto => 'tcp', dport => '9418' }, | |
87 | ], | |
88 | 'Gnutella' => [ | |
89 | { action => 'PARAM', proto => 'tcp', dport => '6346' }, | |
90 | { action => 'PARAM', proto => 'udp', dport => '6346' }, | |
91 | ], | |
92 | 'HKP' => [ | |
93 | { action => 'PARAM', proto => 'tcp', dport => '11371' }, | |
94 | ], | |
95 | 'HTTP' => [ | |
961b4928 DM |
96 | { action => 'PARAM', proto => 'tcp', dport => '80' }, |
97 | ], | |
857f62c8 DM |
98 | 'HTTPS' => [ |
99 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
100 | ], | |
101 | 'ICPV2' => [ | |
102 | { action => 'PARAM', proto => 'udp', dport => '3130' }, | |
103 | ], | |
104 | 'ICQ' => [ | |
105 | { action => 'PARAM', proto => 'tcp', dport => '5190' }, | |
106 | ], | |
107 | 'IMAP' => [ | |
108 | { action => 'PARAM', proto => 'tcp', dport => '143' }, | |
109 | ], | |
110 | 'IMAPS' => [ | |
111 | { action => 'PARAM', proto => 'tcp', dport => '993' }, | |
112 | ], | |
113 | 'IPIP' => [ | |
114 | { action => 'PARAM', proto => '94' }, | |
115 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' }, | |
116 | ], | |
117 | 'IPP' => [ | |
118 | { action => 'PARAM', proto => 'tcp', dport => '631' }, | |
119 | ], | |
120 | 'IPPbrd' => [ | |
121 | { action => 'PARAM', proto => 'udp', dport => '631' }, | |
122 | ], | |
123 | 'IPPserver' => [ | |
124 | { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' }, | |
125 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' }, | |
126 | ], | |
127 | 'IPsec' => [ | |
128 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
129 | { action => 'PARAM', proto => '50' }, | |
130 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
131 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
132 | ], | |
133 | 'IPsecah' => [ | |
134 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
135 | { action => 'PARAM', proto => '51' }, | |
136 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
137 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' }, | |
138 | ], | |
139 | 'IPsecnat' => [ | |
140 | { action => 'PARAM', proto => 'udp', dport => '500' }, | |
141 | { action => 'PARAM', proto => 'udp', dport => '4500' }, | |
142 | { action => 'PARAM', proto => '50' }, | |
143 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' }, | |
144 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' }, | |
145 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
146 | ], | |
147 | 'IRC' => [ | |
148 | { action => 'PARAM', proto => 'tcp', dport => '6667' }, | |
149 | ], | |
150 | 'JabberPlain' => [ | |
151 | { action => 'PARAM', proto => 'tcp', dport => '5222' }, | |
152 | ], | |
153 | 'JabberSecure' => [ | |
154 | { action => 'PARAM', proto => 'tcp', dport => '5223' }, | |
155 | ], | |
156 | 'Jabberd' => [ | |
157 | { action => 'PARAM', proto => 'tcp', dport => '5269' }, | |
158 | ], | |
159 | 'Jetdirect' => [ | |
160 | { action => 'PARAM', proto => 'tcp', dport => '9100' }, | |
161 | ], | |
162 | 'L2TP' => [ | |
163 | { action => 'PARAM', proto => 'udp', dport => '1701' }, | |
164 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' }, | |
165 | ], | |
166 | 'LDAP' => [ | |
167 | { action => 'PARAM', proto => 'tcp', dport => '389' }, | |
168 | ], | |
169 | 'LDAPS' => [ | |
170 | { action => 'PARAM', proto => 'tcp', dport => '636' }, | |
171 | ], | |
172 | 'MSNP' => [ | |
173 | { action => 'PARAM', proto => 'tcp', dport => '1863' }, | |
174 | ], | |
175 | 'MSSQL' => [ | |
176 | { action => 'PARAM', proto => 'tcp', dport => '1433' }, | |
177 | ], | |
178 | 'Mail' => [ | |
179 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
180 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
181 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
182 | ], | |
183 | 'Munin' => [ | |
184 | { action => 'PARAM', proto => 'tcp', dport => '4949' }, | |
185 | ], | |
186 | 'MySQL' => [ | |
187 | { action => 'PARAM', proto => 'tcp', dport => '3306' }, | |
188 | ], | |
189 | 'NNTP' => [ | |
190 | { action => 'PARAM', proto => 'tcp', dport => '119' }, | |
191 | ], | |
192 | 'NNTPS' => [ | |
193 | { action => 'PARAM', proto => 'tcp', dport => '563' }, | |
194 | ], | |
195 | 'NTP' => [ | |
196 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
197 | ], | |
198 | 'NTPbi' => [ | |
199 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
200 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' }, | |
201 | ], | |
202 | 'NTPbrd' => [ | |
203 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
204 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' }, | |
205 | ], | |
206 | 'OSPF' => [ | |
207 | { action => 'PARAM', proto => '89' }, | |
208 | ], | |
209 | 'OpenVPN' => [ | |
210 | { action => 'PARAM', proto => 'udp', dport => '1194' }, | |
211 | ], | |
212 | 'PCA' => [ | |
213 | { action => 'PARAM', proto => 'udp', dport => '5632' }, | |
214 | { action => 'PARAM', proto => 'tcp', dport => '5631' }, | |
215 | ], | |
216 | 'POP3' => [ | |
217 | { action => 'PARAM', proto => 'tcp', dport => '110' }, | |
218 | ], | |
219 | 'POP3S' => [ | |
220 | { action => 'PARAM', proto => 'tcp', dport => '995' }, | |
221 | ], | |
222 | 'PPtP' => [ | |
223 | { action => 'PARAM', proto => '47' }, | |
224 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
225 | { action => 'PARAM', proto => 'tcp', dport => '1723' }, | |
226 | ], | |
227 | 'Ping' => [ | |
228 | { action => 'PARAM', proto => 'icmp', dport => '8' }, | |
229 | ], | |
230 | 'PostgreSQL' => [ | |
231 | { action => 'PARAM', proto => 'tcp', dport => '5432' }, | |
232 | ], | |
233 | 'Printer' => [ | |
234 | { action => 'PARAM', proto => 'tcp', dport => '515' }, | |
235 | ], | |
236 | 'RDP' => [ | |
237 | { action => 'PARAM', proto => 'tcp', dport => '3389' }, | |
238 | ], | |
239 | 'RIPbi' => [ | |
240 | { action => 'PARAM', proto => 'udp', dport => '520' }, | |
241 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' }, | |
242 | ], | |
243 | 'RNDC' => [ | |
244 | { action => 'PARAM', proto => 'tcp', dport => '953' }, | |
245 | ], | |
246 | 'Razor' => [ | |
247 | { action => 'ACCEPT', proto => 'tcp', dport => '2703' }, | |
248 | ], | |
249 | 'Rdate' => [ | |
250 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
251 | ], | |
252 | 'Rsync' => [ | |
253 | { action => 'PARAM', proto => 'tcp', dport => '873' }, | |
254 | ], | |
255 | 'SANE' => [ | |
256 | { action => 'PARAM', proto => 'tcp', dport => '6566' }, | |
257 | ], | |
258 | 'SMB' => [ | |
259 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
260 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
261 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
262 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
263 | ], | |
264 | 'SMBBI' => [ | |
265 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
266 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
267 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
268 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
269 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' }, | |
270 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' }, | |
271 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
272 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' }, | |
273 | ], | |
274 | 'SMBswat' => [ | |
275 | { action => 'PARAM', proto => 'tcp', dport => '901' }, | |
276 | ], | |
277 | 'SMTP' => [ | |
278 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
279 | ], | |
280 | 'SMTPS' => [ | |
281 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
282 | ], | |
283 | 'SNMP' => [ | |
284 | { action => 'PARAM', proto => 'udp', dport => '161:162' }, | |
285 | { action => 'PARAM', proto => 'tcp', dport => '161' }, | |
286 | ], | |
287 | 'SPAMD' => [ | |
288 | { action => 'PARAM', proto => 'tcp', dport => '783' }, | |
289 | ], | |
290 | 'SSH' => [ | |
291 | { action => 'PARAM', proto => 'tcp', dport => '22' }, | |
292 | ], | |
293 | 'SVN' => [ | |
294 | { action => 'PARAM', proto => 'tcp', dport => '3690' }, | |
295 | ], | |
296 | 'SixXS' => [ | |
297 | { action => 'PARAM', proto => 'tcp', dport => '3874' }, | |
298 | { action => 'PARAM', proto => 'udp', dport => '3740' }, | |
299 | { action => 'PARAM', proto => '41' }, | |
300 | { action => 'PARAM', proto => 'udp', dport => '5072,8374' }, | |
301 | ], | |
302 | 'Squid' => [ | |
303 | { action => 'PARAM', proto => 'tcp', dport => '3128' }, | |
304 | ], | |
305 | 'Submission' => [ | |
306 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
307 | ], | |
308 | 'Syslog' => [ | |
309 | { action => 'PARAM', proto => 'udp', dport => '514' }, | |
310 | { action => 'PARAM', proto => 'tcp', dport => '514' }, | |
311 | ], | |
312 | 'TFTP' => [ | |
313 | { action => 'PARAM', proto => 'udp', dport => '69' }, | |
314 | ], | |
315 | 'Telnet' => [ | |
316 | { action => 'PARAM', proto => 'tcp', dport => '23' }, | |
317 | ], | |
318 | 'Telnets' => [ | |
319 | { action => 'PARAM', proto => 'tcp', dport => '992' }, | |
320 | ], | |
321 | 'Time' => [ | |
322 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
323 | ], | |
324 | 'Trcrt' => [ | |
325 | { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, | |
326 | { action => 'PARAM', proto => 'icmp', dport => '8' }, | |
327 | ], | |
328 | 'VNC' => [ | |
329 | { action => 'PARAM', proto => 'tcp', dport => '5900:5909' }, | |
330 | ], | |
331 | 'VNCL' => [ | |
332 | { action => 'PARAM', proto => 'tcp', dport => '5500' }, | |
333 | ], | |
334 | 'Web' => [ | |
335 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
961b4928 DM |
336 | { action => 'PARAM', proto => 'tcp', dport => '443' }, |
337 | ], | |
857f62c8 DM |
338 | 'Webcache' => [ |
339 | { action => 'PARAM', proto => 'tcp', dport => '8080' }, | |
340 | ], | |
341 | 'Webmin' => [ | |
342 | { action => 'PARAM', proto => 'tcp', dport => '10000' }, | |
343 | ], | |
344 | 'Whois' => [ | |
345 | { action => 'PARAM', proto => 'tcp', dport => '43' }, | |
346 | ], | |
961b4928 DM |
347 | }; |
348 | ||
349 | my $pve_fw_parsed_macros; | |
350 | my $pve_fw_preferred_macro_names = {}; | |
3a616aa0 | 351 | |
a332200b | 352 | sub get_firewall_macros { |
9aab3127 | 353 | |
961b4928 DM |
354 | return $pve_fw_parsed_macros if $pve_fw_parsed_macros; |
355 | ||
356 | $pve_fw_parsed_macros = {}; | |
9aab3127 | 357 | |
961b4928 DM |
358 | foreach my $k (keys %$pve_fw_macros) { |
359 | my $name = lc($k); | |
a332200b | 360 | |
961b4928 DM |
361 | my $macro = $pve_fw_macros->{$k}; |
362 | $pve_fw_preferred_macro_names->{$name} = $k; | |
363 | $pve_fw_parsed_macros->{$name} = $macro; | |
364 | } | |
a332200b | 365 | |
961b4928 | 366 | return $pve_fw_parsed_macros; |
9aab3127 DM |
367 | } |
368 | ||
fcba0beb DM |
369 | my $etc_services; |
370 | ||
371 | sub get_etc_services { | |
372 | ||
373 | return $etc_services if $etc_services; | |
374 | ||
375 | my $filename = "/etc/services"; | |
376 | ||
377 | my $fh = IO::File->new($filename, O_RDONLY); | |
378 | if (!$fh) { | |
379 | warn "unable to read '$filename' - $!\n"; | |
380 | return {}; | |
381 | } | |
382 | ||
383 | my $services = {}; | |
384 | ||
385 | while (my $line = <$fh>) { | |
386 | chomp ($line); | |
387 | next if $line =~m/^#/; | |
388 | next if ($line =~m/^\s*$/); | |
389 | ||
390 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
391 | $services->{byid}->{$2}->{name} = $1; | |
35b66e9d | 392 | $services->{byid}->{$2}->{port} = $2; |
fcba0beb DM |
393 | $services->{byid}->{$2}->{$3} = 1; |
394 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
395 | } | |
396 | } | |
397 | ||
398 | close($fh); | |
399 | ||
400 | $etc_services = $services; | |
401 | ||
3a616aa0 | 402 | |
fcba0beb DM |
403 | return $etc_services; |
404 | } | |
405 | ||
406 | my $etc_protocols; | |
407 | ||
408 | sub get_etc_protocols { | |
409 | return $etc_protocols if $etc_protocols; | |
410 | ||
411 | my $filename = "/etc/protocols"; | |
412 | ||
413 | my $fh = IO::File->new($filename, O_RDONLY); | |
414 | if (!$fh) { | |
415 | warn "unable to read '$filename' - $!\n"; | |
416 | return {}; | |
417 | } | |
418 | ||
419 | my $protocols = {}; | |
420 | ||
421 | while (my $line = <$fh>) { | |
422 | chomp ($line); | |
423 | next if $line =~m/^#/; | |
424 | next if ($line =~m/^\s*$/); | |
425 | ||
426 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
427 | $protocols->{byid}->{$2}->{name} = $1; | |
428 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
429 | } | |
430 | } | |
431 | ||
432 | close($fh); | |
433 | ||
434 | $etc_protocols = $protocols; | |
435 | ||
436 | return $etc_protocols; | |
437 | } | |
438 | ||
ecbea048 DM |
439 | sub parse_address_list { |
440 | my ($str) = @_; | |
441 | ||
d6de1dc2 | 442 | my $nbaor = 0; |
ecbea048 DM |
443 | foreach my $aor (split(/,/, $str)) { |
444 | if (!Net::IP->new($aor)) { | |
445 | my $err = Net::IP::Error(); | |
446 | die "invalid IP address: $err\n"; | |
d6de1dc2 AD |
447 | }else{ |
448 | $nbaor++; | |
ecbea048 DM |
449 | } |
450 | } | |
d6de1dc2 | 451 | return $nbaor; |
ecbea048 | 452 | } |
dddd9413 | 453 | |
fcba0beb DM |
454 | sub parse_port_name_number_or_range { |
455 | my ($str) = @_; | |
456 | ||
457 | my $services = PVE::Firewall::get_etc_services(); | |
4cdbb3b7 | 458 | my $nbports = 0; |
fcba0beb | 459 | foreach my $item (split(/,/, $str)) { |
4cdbb3b7 | 460 | my $portlist = ""; |
35b66e9d | 461 | my $oldpon = undef; |
fcba0beb | 462 | foreach my $pon (split(':', $item, 2)) { |
35b66e9d | 463 | $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port}; |
4cdbb3b7 | 464 | if ($pon =~ m/^\d+$/){ |
857f62c8 | 465 | die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535; |
35b66e9d AD |
466 | die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon); |
467 | $oldpon = $pon; | |
4cdbb3b7 AD |
468 | }else{ |
469 | die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; | |
470 | } | |
471 | $nbports++; | |
fcba0beb DM |
472 | } |
473 | } | |
474 | ||
4cdbb3b7 | 475 | return ($nbports); |
fcba0beb DM |
476 | } |
477 | ||
780bcc0f DM |
478 | my $bridge_firewall_enabled = 0; |
479 | ||
480 | sub enable_bridge_firewall { | |
481 | ||
482 | return if $bridge_firewall_enabled; # only once | |
483 | ||
484 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables"); | |
485 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables"); | |
486 | ||
487 | $bridge_firewall_enabled = 1; | |
488 | } | |
489 | ||
8cebfa6f | 490 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; |
dddd9413 | 491 | |
3a616aa0 AD |
492 | sub iptables { |
493 | my ($cmd) = @_; | |
494 | ||
495 | run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); | |
496 | } | |
497 | ||
b16e818e DM |
498 | sub iptables_restore_cmdlist { |
499 | my ($cmdlist) = @_; | |
3a616aa0 | 500 | |
3fa83edf | 501 | run_command("/sbin/iptables-restore -n", input => $cmdlist); |
3a616aa0 AD |
502 | } |
503 | ||
de2a57cd DM |
504 | sub iptables_get_chains { |
505 | ||
506 | my $res = {}; | |
507 | ||
508 | # check what chains we want to track | |
509 | my $is_pvefw_chain = sub { | |
510 | my $name = shift; | |
511 | ||
dec84fcd DM |
512 | return 1 if $name =~ m/^PVEFW-\S+$/; |
513 | ||
de2a57cd | 514 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; |
b16e818e | 515 | return 1 if $name =~ m/^vmbr\d+-(:?IN|OUT)$/; |
f50656c2 | 516 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; |
de2a57cd DM |
517 | |
518 | return undef; | |
519 | }; | |
520 | ||
521 | my $table = ''; | |
522 | ||
de2a57cd DM |
523 | my $parser = sub { |
524 | my $line = shift; | |
525 | ||
526 | return if $line =~ m/^#/; | |
527 | return if $line =~ m/^\s*$/; | |
528 | ||
529 | if ($line =~ m/^\*(\S+)$/) { | |
530 | $table = $1; | |
531 | return; | |
532 | } | |
533 | ||
534 | return if $table ne 'filter'; | |
535 | ||
536 | if ($line =~ m/^:(\S+)\s/) { | |
537 | my $chain = $1; | |
538 | return if !&$is_pvefw_chain($chain); | |
3fa83edf | 539 | $res->{$chain} = "unknown"; |
09d5f68e | 540 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { |
3fa83edf | 541 | my ($chain, $sig) = ($1, $2); |
de2a57cd | 542 | return if !&$is_pvefw_chain($chain); |
3fa83edf | 543 | $res->{$chain} = $sig; |
de2a57cd DM |
544 | } else { |
545 | # simply ignore the rest | |
546 | return; | |
547 | } | |
548 | }; | |
549 | ||
550 | run_command("/sbin/iptables-save", outfunc => $parser); | |
551 | ||
de2a57cd DM |
552 | return $res; |
553 | } | |
554 | ||
3a616aa0 AD |
555 | sub iptables_chain_exist { |
556 | my ($chain) = @_; | |
557 | ||
558 | eval{ | |
559 | iptables("-n --list $chain"); | |
560 | }; | |
561 | return undef if $@; | |
562 | ||
563 | return 1; | |
564 | } | |
565 | ||
566 | sub iptables_rule_exist { | |
567 | my ($rule) = @_; | |
568 | ||
569 | eval{ | |
570 | iptables("-C $rule"); | |
571 | }; | |
572 | return undef if $@; | |
573 | ||
574 | return 1; | |
575 | } | |
576 | ||
3fa83edf | 577 | sub ruleset_generate_rule { |
4e6112f9 | 578 | my ($ruleset, $chain, $rule, $goto) = @_; |
3a616aa0 | 579 | |
3fa83edf | 580 | my $cmd = ''; |
3a616aa0 | 581 | |
d6de1dc2 | 582 | $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1; |
3a616aa0 | 583 | $cmd .= " -s $rule->{source}" if $rule->{source}; |
d6de1dc2 | 584 | $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1; |
8c32a215 | 585 | $cmd .= " -d $rule->{dest}" if $rule->{dest}; |
3a616aa0 | 586 | $cmd .= " -p $rule->{proto}" if $rule->{proto}; |
4cdbb3b7 | 587 | $cmd .= " --match multiport" if $rule->{nbdport} && $rule->{nbdport} > 1; |
3a616aa0 | 588 | $cmd .= " --dport $rule->{dport}" if $rule->{dport}; |
4cdbb3b7 | 589 | $cmd .= " --match multiport" if $rule->{nbsport} && $rule->{nbsport} > 1; |
3a616aa0 | 590 | $cmd .= " --sport $rule->{sport}" if $rule->{sport}; |
4e6112f9 DM |
591 | |
592 | if (my $action = $rule->{action}) { | |
593 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
594 | $cmd .= $goto ? " -g $action" : " -j $action"; | |
595 | }; | |
3a616aa0 | 596 | |
3fa83edf DM |
597 | ruleset_addrule($ruleset, $chain, $cmd) if $cmd; |
598 | } | |
599 | ||
600 | sub ruleset_create_chain { | |
601 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 602 | |
d050c724 DM |
603 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; |
604 | ||
3fa83edf DM |
605 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; |
606 | ||
607 | $ruleset->{$chain} = []; | |
3a616aa0 AD |
608 | } |
609 | ||
3fa83edf DM |
610 | sub ruleset_chain_exist { |
611 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 612 | |
3fa83edf DM |
613 | return $ruleset->{$chain} ? 1 : undef; |
614 | } | |
3a616aa0 | 615 | |
3fa83edf DM |
616 | sub ruleset_addrule { |
617 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 618 | |
3fa83edf | 619 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 620 | |
3fa83edf DM |
621 | push @{$ruleset->{$chain}}, "-A $chain $rule"; |
622 | } | |
3a616aa0 | 623 | |
3fa83edf DM |
624 | sub ruleset_insertrule { |
625 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 626 | |
3fa83edf | 627 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 628 | |
3fa83edf DM |
629 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; |
630 | } | |
3a616aa0 | 631 | |
3fa83edf DM |
632 | sub generate_bridge_chains { |
633 | my ($ruleset, $bridge) = @_; | |
3a616aa0 | 634 | |
dec84fcd DM |
635 | if (!ruleset_chain_exist($ruleset, "PVEFW-BRIDGE-IN")){ |
636 | ruleset_create_chain($ruleset, "PVEFW-BRIDGE-IN"); | |
d4d0fd1d AD |
637 | } |
638 | ||
dec84fcd DM |
639 | if (!ruleset_chain_exist($ruleset, "PVEFW-BRIDGE-OUT")){ |
640 | ruleset_create_chain($ruleset, "PVEFW-BRIDGE-OUT"); | |
d4d0fd1d | 641 | } |
3fa83edf | 642 | |
dec84fcd DM |
643 | if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){ |
644 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
3fa83edf | 645 | |
7fab95bc | 646 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); |
dec84fcd DM |
647 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-in --physdev-is-bridged -j PVEFW-BRIDGE-OUT"); |
648 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-out --physdev-is-bridged -j PVEFW-BRIDGE-IN"); | |
3a616aa0 AD |
649 | } |
650 | ||
3fa83edf DM |
651 | if (!ruleset_chain_exist($ruleset, "$bridge-IN")) { |
652 | ruleset_create_chain($ruleset, "$bridge-IN"); | |
dec84fcd DM |
653 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing |
654 | ruleset_addrule($ruleset, "PVEFW-BRIDGE-IN", "-j $bridge-IN"); | |
3fa83edf DM |
655 | ruleset_addrule($ruleset, "$bridge-IN", "-j ACCEPT"); |
656 | } | |
3a616aa0 | 657 | |
3fa83edf DM |
658 | if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) { |
659 | ruleset_create_chain($ruleset, "$bridge-OUT"); | |
dec84fcd DM |
660 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing |
661 | ruleset_addrule($ruleset, "PVEFW-BRIDGE-OUT", "-j $bridge-OUT"); | |
3fa83edf DM |
662 | } |
663 | } | |
3a616aa0 AD |
664 | |
665 | sub generate_tap_rules_direction { | |
51bae274 | 666 | my ($ruleset, $group_rules, $iface, $netid, $macaddr, $rules, $bridge, $direction) = @_; |
3a616aa0 AD |
667 | |
668 | my $tapchain = "$iface-$direction"; | |
669 | ||
3fa83edf | 670 | ruleset_create_chain($ruleset, $tapchain); |
3a616aa0 | 671 | |
7fab95bc DM |
672 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP"); |
673 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
3a616aa0 | 674 | |
c29f55c9 DM |
675 | if ($direction eq 'OUT' && defined($macaddr)) { |
676 | ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP"); | |
677 | } | |
678 | ||
3fa83edf | 679 | if ($rules) { |
3a616aa0 AD |
680 | foreach my $rule (@$rules) { |
681 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
bfbfa8b6 | 682 | # we go to $bridge-IN if accept in out rules |
3a616aa0 | 683 | if($rule->{action} =~ m/^(GROUP-(\S+))$/){ |
3fa83edf DM |
684 | $rule->{action} .= "-$direction"; |
685 | # generate empty group rule if don't exist | |
686 | if(!ruleset_chain_exist($ruleset, $rule->{action})){ | |
51bae274 | 687 | generate_group_rules($ruleset, $group_rules, $2); |
3fa83edf | 688 | } |
4e6112f9 | 689 | ruleset_generate_rule($ruleset, $tapchain, $rule); |
31ff3ef6 DM |
690 | ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -g $bridge-IN") |
691 | if $direction eq 'OUT'; | |
4e6112f9 | 692 | } else { |
4e6112f9 DM |
693 | $rule->{action} = "$bridge-IN" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT'; |
694 | ruleset_generate_rule($ruleset, $tapchain, $rule); | |
3a616aa0 | 695 | } |
4e6112f9 | 696 | } |
3a616aa0 AD |
697 | } |
698 | ||
3fa83edf DM |
699 | ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4"); |
700 | ruleset_addrule($ruleset, $tapchain, "-j DROP"); | |
3a616aa0 | 701 | |
3fa83edf DM |
702 | # plug the tap chain to bridge chain |
703 | my $physdevdirection = $direction eq 'IN' ? "out" : "in"; | |
704 | my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain"; | |
705 | ruleset_insertrule($ruleset, "$bridge-$direction", $rule); | |
3a616aa0 | 706 | |
3fa83edf DM |
707 | if ($direction eq 'OUT'){ |
708 | # add tap->host rules | |
709 | my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain"; | |
dec84fcd | 710 | ruleset_addrule($ruleset, "PVEFW-INPUT", $rule); |
3a616aa0 AD |
711 | } |
712 | } | |
713 | ||
0bd5f137 | 714 | sub enablehostfw { |
51bae274 | 715 | my ($ruleset, $rules, $group_rules) = @_; |
0bd5f137 | 716 | |
51bae274 | 717 | # fixme: allow security groups |
0bd5f137 | 718 | |
3fa83edf | 719 | # host inbound firewall |
dec84fcd DM |
720 | my $chain = "PVEFW-HOST-IN"; |
721 | ruleset_create_chain($ruleset, $chain); | |
0bd5f137 | 722 | |
7fab95bc DM |
723 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
724 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
725 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); |
726 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 727 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 728 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 729 | |
3fa83edf DM |
730 | if ($rules->{in}) { |
731 | foreach my $rule (@{$rules->{in}}) { | |
732 | # we use RETURN because we need to check also tap rules | |
733 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 734 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 735 | } |
0bd5f137 AD |
736 | } |
737 | ||
dec84fcd DM |
738 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4"); |
739 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
0bd5f137 | 740 | |
3fa83edf | 741 | # host outbound firewall |
aadd745e | 742 | $chain = "PVEFW-HOST-OUT"; |
dec84fcd DM |
743 | ruleset_create_chain($ruleset, $chain); |
744 | ||
7fab95bc DM |
745 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
746 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
747 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); |
748 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 749 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 750 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 751 | |
3fa83edf DM |
752 | if ($rules->{out}) { |
753 | foreach my $rule (@{$rules->{out}}) { | |
754 | # we use RETURN because we need to check also tap rules | |
755 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 756 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 757 | } |
0bd5f137 AD |
758 | } |
759 | ||
dec84fcd DM |
760 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4"); |
761 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
9d31b418 | 762 | |
dec84fcd DM |
763 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); |
764 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
9d31b418 AD |
765 | } |
766 | ||
767 | sub generate_group_rules { | |
51bae274 | 768 | my ($ruleset, $group_rules, $group) = @_; |
9d31b418 | 769 | |
51bae274 | 770 | my $rules = $group_rules->{$group}; |
9d31b418 | 771 | |
51bae274 DM |
772 | die "no such security group '$group'\n" if !$rules; |
773 | ||
3fa83edf | 774 | my $chain = "GROUP-${group}-IN"; |
9d31b418 | 775 | |
3fa83edf | 776 | ruleset_create_chain($ruleset, $chain); |
9d31b418 | 777 | |
3fa83edf DM |
778 | if ($rules->{in}) { |
779 | foreach my $rule (@{$rules->{in}}) { | |
780 | ruleset_generate_rule($ruleset, $chain, $rule); | |
9d31b418 AD |
781 | } |
782 | } | |
783 | ||
3fa83edf | 784 | $chain = "GROUP-${group}-OUT"; |
9d31b418 | 785 | |
3fa83edf | 786 | ruleset_create_chain($ruleset, $chain); |
4e6112f9 | 787 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark |
9d31b418 | 788 | |
3fa83edf DM |
789 | if ($rules->{out}) { |
790 | foreach my $rule (@{$rules->{out}}) { | |
4e6112f9 DM |
791 | # we go the PVEFW-SET-ACCEPT-MARK Instead of ACCEPT) because we need to |
792 | # check also other tap rules (and group rules can be set on any bridge, | |
793 | # so we can't go to VMBRXX-IN) | |
794 | $rule->{action} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action} eq 'ACCEPT'; | |
6a6c7af7 | 795 | ruleset_generate_rule($ruleset, $chain, $rule); |
9d31b418 AD |
796 | } |
797 | } | |
9d31b418 AD |
798 | } |
799 | ||
51bae274 DM |
800 | my $MAX_NETS = 32; |
801 | my $valid_netdev_names = {}; | |
802 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
803 | $valid_netdev_names->{"net$i"} = 1; | |
804 | } | |
5e1267a5 | 805 | |
51bae274 DM |
806 | sub parse_fw_rule { |
807 | my ($line, $need_iface, $allow_groups) = @_; | |
5e1267a5 | 808 | |
a332200b | 809 | my $macros = get_firewall_macros(); |
fcba0beb | 810 | my $protocols = get_etc_protocols(); |
51bae274 DM |
811 | |
812 | my ($action, $iface, $source, $dest, $proto, $dport, $sport); | |
813 | ||
814 | $line =~ s/#.*$//; | |
815 | ||
816 | my @data = split(/\s+/, $line); | |
817 | my $expected_elements = $need_iface ? 7 : 6; | |
818 | ||
819 | die "wrong number of rule elements\n" if scalar(@data) > $expected_elements; | |
820 | ||
821 | if ($need_iface) { | |
822 | ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data | |
823 | } else { | |
824 | ($action, $source, $dest, $proto, $dport, $sport) = @data; | |
825 | } | |
826 | ||
827 | die "incomplete rule\n" if !$action; | |
828 | ||
961b4928 DM |
829 | my $macro; |
830 | my $macro_name; | |
831 | ||
51bae274 DM |
832 | if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) { |
833 | # OK | |
834 | } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) { | |
835 | # OK | |
836 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
961b4928 DM |
837 | ($macro_name, $action) = ($1, $2); |
838 | my $lc_macro_name = lc($macro_name); | |
839 | my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name}; | |
840 | $macro_name = $preferred_name if $preferred_name; | |
841 | $macro = $macros->{$lc_macro_name}; | |
842 | die "unknown macro '$macro_name'\n" if !$macro; | |
51bae274 DM |
843 | } else { |
844 | die "unknown action '$action'\n"; | |
845 | } | |
846 | ||
847 | if ($need_iface) { | |
848 | $iface = undef if $iface && $iface eq '-'; | |
849 | die "unknown interface '$iface'\n" | |
850 | if defined($iface) && !$valid_netdev_names->{$iface}; | |
851 | } | |
852 | ||
853 | $proto = undef if $proto && $proto eq '-'; | |
854 | die "unknown protokol '$proto'\n" if $proto && | |
855 | !(defined($protocols->{byname}->{$proto}) || | |
856 | defined($protocols->{byid}->{$proto})); | |
857 | ||
858 | $source = undef if $source && $source eq '-'; | |
859 | $dest = undef if $dest && $dest eq '-'; | |
860 | ||
861 | $dport = undef if $dport && $dport eq '-'; | |
862 | $sport = undef if $sport && $sport eq '-'; | |
863 | ||
51bae274 DM |
864 | my $nbsource = undef; |
865 | my $nbdest = undef; | |
866 | ||
867 | $nbsource = parse_address_list($source) if $source; | |
868 | $nbdest = parse_address_list($dest) if $dest; | |
961b4928 DM |
869 | |
870 | my $rules = []; | |
51bae274 | 871 | |
961b4928 | 872 | my $param = { |
51bae274 | 873 | action => $action, |
51bae274 DM |
874 | iface => $iface, |
875 | source => $source, | |
876 | dest => $dest, | |
877 | nbsource => $nbsource, | |
878 | nbdest => $nbdest, | |
879 | proto => $proto, | |
880 | dport => $dport, | |
881 | sport => $sport, | |
51bae274 | 882 | }; |
961b4928 DM |
883 | |
884 | if ($macro) { | |
885 | foreach my $templ (@$macro) { | |
886 | my $rule = {}; | |
887 | foreach my $k (keys %$templ) { | |
888 | my $v = $templ->{$k}; | |
857f62c8 DM |
889 | if ($v eq 'PARAM') { |
890 | $v = $param->{$k}; | |
891 | } elsif ($v eq 'DEST') { | |
892 | $v = $param->{dest}; | |
893 | } elsif ($v eq 'SOURCE') { | |
894 | $v = $param->{source}; | |
895 | } | |
896 | ||
961b4928 DM |
897 | die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v); |
898 | $rule->{$k} = $v; | |
899 | } | |
900 | push @$rules, $rule; | |
901 | } | |
902 | } else { | |
903 | push @$rules, $param; | |
904 | } | |
905 | ||
906 | foreach my $rule (@$rules) { | |
907 | $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport}) | |
908 | if defined($rule->{dport}); | |
909 | $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport}) | |
910 | if defined($rule->{sport}); | |
911 | } | |
912 | ||
913 | return $rules; | |
51bae274 DM |
914 | } |
915 | ||
85c6eaed DM |
916 | sub parse_fw_option { |
917 | my ($line) = @_; | |
918 | ||
919 | my ($opt, $value); | |
920 | ||
921 | if ($line =~ m/^enable:\s*(0|1)\s*$/i) { | |
922 | $opt = 'enable'; | |
923 | $value = int($1); | |
924 | } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
925 | $opt = lc($1); | |
926 | $value = uc($3); | |
927 | } else { | |
928 | chomp $line; | |
929 | die "can't parse option '$line'\n" | |
930 | } | |
931 | ||
932 | return ($opt, $value); | |
933 | } | |
934 | ||
51bae274 DM |
935 | sub parse_vm_fw_rules { |
936 | my ($filename, $fh) = @_; | |
937 | ||
85c6eaed | 938 | my $res = { in => [], out => [], options => {}}; |
51bae274 DM |
939 | |
940 | my $section; | |
941 | ||
5e1267a5 DM |
942 | while (defined(my $line = <$fh>)) { |
943 | next if $line =~ m/^#/; | |
944 | next if $line =~ m/^\s*$/; | |
945 | ||
961b4928 DM |
946 | my $linenr = $fh->input_line_number(); |
947 | my $prefix = "$filename (line $linenr)"; | |
948 | ||
85c6eaed | 949 | if ($line =~ m/^\[(\S+)\]\s*$/i) { |
5e1267a5 | 950 | $section = lc($1); |
85c6eaed | 951 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; |
5e1267a5 DM |
952 | next; |
953 | } | |
51bae274 | 954 | if (!$section) { |
961b4928 | 955 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
956 | next; |
957 | } | |
958 | ||
85c6eaed DM |
959 | next if !$res->{$section}; # skip undefined section |
960 | ||
961 | if ($section eq 'options') { | |
962 | eval { | |
963 | my ($opt, $value) = parse_fw_option($line); | |
964 | $res->{options}->{$opt} = $value; | |
965 | }; | |
966 | warn "$prefix: $@" if $@; | |
967 | next; | |
968 | } | |
969 | ||
961b4928 DM |
970 | my $rules; |
971 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
51bae274 | 972 | if (my $err = $@) { |
961b4928 | 973 | warn "$prefix: $err"; |
5e1267a5 DM |
974 | next; |
975 | } | |
976 | ||
961b4928 | 977 | push @{$res->{$section}}, @$rules; |
51bae274 DM |
978 | } |
979 | ||
980 | return $res; | |
981 | } | |
982 | ||
983 | sub parse_host_fw_rules { | |
984 | my ($filename, $fh) = @_; | |
985 | ||
986 | my $res = { in => [], out => [] }; | |
987 | ||
988 | my $section; | |
989 | ||
990 | while (defined(my $line = <$fh>)) { | |
991 | next if $line =~ m/^#/; | |
992 | next if $line =~ m/^\s*$/; | |
993 | ||
961b4928 DM |
994 | my $linenr = $fh->input_line_number(); |
995 | my $prefix = "$filename (line $linenr)"; | |
996 | ||
51bae274 DM |
997 | if ($line =~ m/^\[(in|out)\]\s*$/i) { |
998 | $section = lc($1); | |
5e1267a5 DM |
999 | next; |
1000 | } | |
51bae274 | 1001 | if (!$section) { |
961b4928 | 1002 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
1003 | next; |
1004 | } | |
1005 | ||
961b4928 DM |
1006 | my $rules; |
1007 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
ecbea048 | 1008 | if (my $err = $@) { |
961b4928 | 1009 | warn "$prefix: $err"; |
ecbea048 | 1010 | next; |
ecbea048 DM |
1011 | } |
1012 | ||
961b4928 | 1013 | push @{$res->{$section}}, @$rules; |
51bae274 | 1014 | } |
ecbea048 | 1015 | |
51bae274 DM |
1016 | return $res; |
1017 | } | |
4cdbb3b7 | 1018 | |
51bae274 DM |
1019 | sub parse_group_fw_rules { |
1020 | my ($filename, $fh) = @_; | |
5e1267a5 | 1021 | |
51bae274 DM |
1022 | my $section; |
1023 | my $group; | |
1024 | ||
1025 | my $res = { in => [], out => [] }; | |
1026 | ||
1027 | while (defined(my $line = <$fh>)) { | |
1028 | next if $line =~ m/^#/; | |
1029 | next if $line =~ m/^\s*$/; | |
1030 | ||
961b4928 DM |
1031 | my $linenr = $fh->input_line_number(); |
1032 | my $prefix = "$filename (line $linenr)"; | |
1033 | ||
51bae274 DM |
1034 | if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) { |
1035 | $section = lc($1); | |
1036 | $group = lc($2); | |
1037 | next; | |
1038 | } | |
1039 | if (!$section || !$group) { | |
961b4928 | 1040 | warn "$prefix: skip line - no section"; |
51bae274 DM |
1041 | next; |
1042 | } | |
1043 | ||
961b4928 DM |
1044 | my $rules; |
1045 | eval { $rules = parse_fw_rule($line, 0, 0); }; | |
51bae274 | 1046 | if (my $err = $@) { |
961b4928 | 1047 | warn "$prefix: $err"; |
51bae274 DM |
1048 | next; |
1049 | } | |
1050 | ||
961b4928 | 1051 | push @{$res->{$group}->{$section}}, @$rules; |
5e1267a5 DM |
1052 | } |
1053 | ||
1054 | return $res; | |
1055 | } | |
1056 | ||
06320eb0 DM |
1057 | sub run_locked { |
1058 | my ($code, @param) = @_; | |
1059 | ||
1060 | my $timeout = 10; | |
1061 | ||
1062 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
1063 | ||
1064 | die $@ if $@; | |
1065 | ||
1066 | return $res; | |
1067 | } | |
1068 | ||
5e1267a5 DM |
1069 | sub read_local_vm_config { |
1070 | ||
1071 | my $openvz = {}; | |
1072 | ||
1073 | my $qemu = {}; | |
1074 | ||
1075 | my $list = PVE::QemuServer::config_list(); | |
1076 | ||
1077 | foreach my $vmid (keys %$list) { | |
5e1267a5 DM |
1078 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); |
1079 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
1080 | $qemu->{$vmid} = $conf; | |
1081 | } | |
1082 | } | |
1083 | ||
1084 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
1085 | ||
1086 | return $vmdata; | |
1087 | }; | |
1088 | ||
1089 | sub read_vm_firewall_rules { | |
1090 | my ($vmdata) = @_; | |
1091 | my $rules = {}; | |
1092 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
1093 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
1094 | my $fh = IO::File->new($filename, O_RDONLY); | |
1095 | next if !$fh; | |
1096 | ||
51bae274 | 1097 | $rules->{$vmid} = parse_vm_fw_rules($filename, $fh); |
5e1267a5 DM |
1098 | } |
1099 | ||
1100 | return $rules; | |
1101 | } | |
1102 | ||
1103 | sub compile { | |
5e1267a5 DM |
1104 | my $vmdata = read_local_vm_config(); |
1105 | my $rules = read_vm_firewall_rules($vmdata); | |
51bae274 DM |
1106 | |
1107 | my $group_rules = {}; | |
1108 | my $filename = "/etc/pve/firewall/groups.fw"; | |
1109 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1110 | $group_rules = parse_group_fw_rules($filename, $fh); | |
1111 | } | |
5e1267a5 | 1112 | |
3fa83edf DM |
1113 | #print Dumper($rules); |
1114 | ||
1115 | my $ruleset = {}; | |
1116 | ||
dec84fcd DM |
1117 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); |
1118 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
fadb13dd | 1119 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); |
3fa83edf | 1120 | |
4e6112f9 DM |
1121 | ruleset_create_chain($ruleset, "PVEFW-SET-ACCEPT-MARK"); |
1122 | ruleset_addrule($ruleset, "PVEFW-SET-ACCEPT-MARK", "-j MARK --set-mark 1"); | |
1123 | ||
fadb13dd | 1124 | my $enable_hostfw = 0; |
51bae274 DM |
1125 | $filename = "/etc/pve/local/host.fw"; |
1126 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1127 | my $host_rules = parse_host_fw_rules($filename, $fh); | |
fadb13dd DM |
1128 | |
1129 | $enable_hostfw = 1; | |
1130 | ||
51bae274 DM |
1131 | enablehostfw($ruleset, $host_rules, $group_rules); |
1132 | } | |
3fa83edf DM |
1133 | |
1134 | # generate firewall rules for QEMU VMs | |
1135 | foreach my $vmid (keys %{$vmdata->{qemu}}) { | |
1136 | my $conf = $vmdata->{qemu}->{$vmid}; | |
fadb13dd | 1137 | |
3fa83edf | 1138 | next if !$rules->{$vmid}; |
fadb13dd DM |
1139 | my $options = $rules->{$vmid}->{options}; |
1140 | next if defined($options->{enable}) && ($options->{enable} == 0); | |
3fa83edf DM |
1141 | |
1142 | foreach my $netid (keys %$conf) { | |
1143 | next if $netid !~ m/^net(\d+)$/; | |
1144 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
1145 | next if !$net; | |
1146 | my $iface = "tap${vmid}i$1"; | |
5e1267a5 | 1147 | |
3fa83edf DM |
1148 | my $bridge = $net->{bridge}; |
1149 | next if !$bridge; # fixme: ? | |
1150 | ||
1151 | $bridge .= "v$net->{tag}" if $net->{tag}; | |
1152 | ||
fadb13dd | 1153 | |
3fa83edf DM |
1154 | generate_bridge_chains($ruleset, $bridge); |
1155 | ||
c29f55c9 | 1156 | my $macaddr = $net->{macaddr}; |
51bae274 DM |
1157 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $rules->{$vmid}->{in}, $bridge, 'IN'); |
1158 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $rules->{$vmid}->{out}, $bridge, 'OUT'); | |
3fa83edf DM |
1159 | } |
1160 | } | |
c0413e35 | 1161 | |
fadb13dd DM |
1162 | if ($enable_hostfw) { |
1163 | # allow traffic from lo (ourself) | |
1164 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT"); | |
1165 | } | |
c0413e35 | 1166 | |
3fa83edf DM |
1167 | return $ruleset; |
1168 | } | |
1169 | ||
1170 | sub get_ruleset_status { | |
1171 | my ($ruleset, $verbose) = @_; | |
1172 | ||
1173 | my $active_chains = iptables_get_chains(); | |
1174 | ||
1175 | my $statushash = {}; | |
1176 | ||
1177 | foreach my $chain (sort keys %$ruleset) { | |
09d5f68e | 1178 | my $digest = Digest::SHA->new('sha1'); |
3fa83edf DM |
1179 | foreach my $cmd (@{$ruleset->{$chain}}) { |
1180 | $digest->add("$cmd\n"); | |
1181 | } | |
1182 | my $sig = $digest->b64digest; | |
1183 | $statushash->{$chain}->{sig} = $sig; | |
1184 | ||
1185 | my $oldsig = $active_chains->{$chain}; | |
1186 | if (!defined($oldsig)) { | |
1187 | $statushash->{$chain}->{action} = 'create'; | |
1188 | } else { | |
1189 | if ($oldsig eq $sig) { | |
1190 | $statushash->{$chain}->{action} = 'exists'; | |
1191 | } else { | |
1192 | $statushash->{$chain}->{action} = 'update'; | |
1193 | } | |
1194 | } | |
1195 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
1196 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1197 | print "\t$cmd\n" if $verbose; | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | foreach my $chain (sort keys %$active_chains) { | |
1202 | if (!defined($ruleset->{$chain})) { | |
1203 | my $sig = $active_chains->{$chain}; | |
1204 | $statushash->{$chain}->{action} = 'delete'; | |
1205 | $statushash->{$chain}->{sig} = $sig; | |
1206 | print "delete $chain ($sig)\n" if $verbose; | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | return $statushash; | |
1211 | } | |
1212 | ||
1213 | sub print_ruleset { | |
1214 | my ($ruleset) = @_; | |
1215 | ||
1216 | get_ruleset_status($ruleset, 1); | |
1217 | } | |
1218 | ||
1219 | sub print_sig_rule { | |
1220 | my ($chain, $sig) = @_; | |
1221 | ||
09d5f68e DM |
1222 | # We just use this to store a SHA1 checksum used to detect changes |
1223 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
b6360c3f DM |
1224 | } |
1225 | ||
a84f4d96 DM |
1226 | sub apply_ruleset { |
1227 | my ($ruleset, $verbose) = @_; | |
3fa83edf | 1228 | |
a84f4d96 | 1229 | enable_bridge_firewall(); |
3fa83edf DM |
1230 | |
1231 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
1232 | ||
1233 | my $statushash = get_ruleset_status($ruleset, $verbose); | |
1234 | ||
1235 | # create missing chains first | |
1236 | foreach my $chain (sort keys %$ruleset) { | |
1237 | my $stat = $statushash->{$chain}; | |
1238 | die "internal error" if !$stat; | |
1239 | next if $stat->{action} ne 'create'; | |
886aba9c | 1240 | |
3fa83edf DM |
1241 | $cmdlist .= ":$chain - [0:0]\n"; |
1242 | } | |
1243 | ||
dec84fcd | 1244 | my $rule = "INPUT -j PVEFW-INPUT"; |
3fa83edf DM |
1245 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1246 | $cmdlist .= "-A $rule\n"; | |
1247 | } | |
dec84fcd | 1248 | $rule = "OUTPUT -j PVEFW-OUTPUT"; |
3fa83edf DM |
1249 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1250 | $cmdlist .= "-A $rule\n"; | |
1251 | } | |
1252 | ||
dec84fcd | 1253 | $rule = "FORWARD -j PVEFW-FORWARD"; |
3fa83edf DM |
1254 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1255 | $cmdlist .= "-A $rule\n"; | |
1256 | } | |
1257 | ||
1258 | foreach my $chain (sort keys %$ruleset) { | |
1259 | my $stat = $statushash->{$chain}; | |
1260 | die "internal error" if !$stat; | |
1261 | ||
1262 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
1263 | $cmdlist .= "-F $chain\n"; | |
1264 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1265 | $cmdlist .= "$cmd\n"; | |
1266 | } | |
1267 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
1268 | } elsif ($stat->{action} eq 'delete') { | |
f5d28682 | 1269 | die "internal error"; # this should not happen |
3fa83edf DM |
1270 | } elsif ($stat->{action} eq 'exists') { |
1271 | # do nothing | |
1272 | } else { | |
1273 | die "internal error - unknown status '$stat->{action}'"; | |
1274 | } | |
1275 | } | |
1276 | ||
f5d28682 DM |
1277 | foreach my $chain (keys %$statushash) { |
1278 | next if $statushash->{$chain}->{action} ne 'delete'; | |
1279 | $cmdlist .= "-F $chain\n"; | |
1280 | } | |
1281 | foreach my $chain (keys %$statushash) { | |
1282 | next if $statushash->{$chain}->{action} ne 'delete'; | |
fadb13dd DM |
1283 | next if $chain eq 'PVEFW-INPUT'; |
1284 | next if $chain eq 'PVEFW-OUTPUT'; | |
1285 | next if $chain eq 'PVEFW-FORWARD'; | |
f5d28682 DM |
1286 | $cmdlist .= "-X $chain\n"; |
1287 | } | |
1288 | ||
3fa83edf DM |
1289 | $cmdlist .= "COMMIT\n"; |
1290 | ||
1291 | print $cmdlist if $verbose; | |
1292 | ||
1293 | iptables_restore_cmdlist($cmdlist); | |
1294 | ||
1295 | # test: re-read status and check if everything is up to date | |
1296 | $statushash = get_ruleset_status($ruleset); | |
1297 | ||
1298 | my $errors; | |
1299 | foreach my $chain (sort keys %$ruleset) { | |
1300 | my $stat = $statushash->{$chain}; | |
1301 | if ($stat->{action} ne 'exists') { | |
1302 | warn "unable to update chain '$chain'\n"; | |
1303 | $errors = 1; | |
1304 | } | |
1305 | } | |
b6360c3f | 1306 | |
3fa83edf | 1307 | die "unable to apply firewall changes\n" if $errors; |
b6360c3f DM |
1308 | } |
1309 | ||
b6360c3f | 1310 | 1; |