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