]>
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; | |
392 | $services->{byid}->{$2}->{$3} = 1; | |
393 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
394 | } | |
395 | } | |
396 | ||
397 | close($fh); | |
398 | ||
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 = ""; |
fcba0beb | 460 | foreach my $pon (split(':', $item, 2)) { |
4cdbb3b7 | 461 | if ($pon =~ m/^\d+$/){ |
857f62c8 | 462 | die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535; |
4cdbb3b7 AD |
463 | }else{ |
464 | die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; | |
465 | } | |
466 | $nbports++; | |
fcba0beb DM |
467 | } |
468 | } | |
469 | ||
4cdbb3b7 | 470 | return ($nbports); |
fcba0beb DM |
471 | } |
472 | ||
780bcc0f DM |
473 | my $bridge_firewall_enabled = 0; |
474 | ||
475 | sub enable_bridge_firewall { | |
476 | ||
477 | return if $bridge_firewall_enabled; # only once | |
478 | ||
479 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables"); | |
480 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables"); | |
481 | ||
482 | $bridge_firewall_enabled = 1; | |
483 | } | |
484 | ||
8cebfa6f | 485 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; |
dddd9413 | 486 | |
3a616aa0 AD |
487 | sub iptables { |
488 | my ($cmd) = @_; | |
489 | ||
490 | run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); | |
491 | } | |
492 | ||
b16e818e DM |
493 | sub iptables_restore_cmdlist { |
494 | my ($cmdlist) = @_; | |
3a616aa0 | 495 | |
3fa83edf | 496 | run_command("/sbin/iptables-restore -n", input => $cmdlist); |
3a616aa0 AD |
497 | } |
498 | ||
de2a57cd DM |
499 | sub iptables_get_chains { |
500 | ||
501 | my $res = {}; | |
502 | ||
503 | # check what chains we want to track | |
504 | my $is_pvefw_chain = sub { | |
505 | my $name = shift; | |
506 | ||
dec84fcd DM |
507 | return 1 if $name =~ m/^PVEFW-\S+$/; |
508 | ||
de2a57cd | 509 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; |
b16e818e | 510 | return 1 if $name =~ m/^vmbr\d+-(:?IN|OUT)$/; |
f50656c2 | 511 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; |
de2a57cd DM |
512 | |
513 | return undef; | |
514 | }; | |
515 | ||
516 | my $table = ''; | |
517 | ||
de2a57cd DM |
518 | my $parser = sub { |
519 | my $line = shift; | |
520 | ||
521 | return if $line =~ m/^#/; | |
522 | return if $line =~ m/^\s*$/; | |
523 | ||
524 | if ($line =~ m/^\*(\S+)$/) { | |
525 | $table = $1; | |
526 | return; | |
527 | } | |
528 | ||
529 | return if $table ne 'filter'; | |
530 | ||
531 | if ($line =~ m/^:(\S+)\s/) { | |
532 | my $chain = $1; | |
533 | return if !&$is_pvefw_chain($chain); | |
3fa83edf | 534 | $res->{$chain} = "unknown"; |
09d5f68e | 535 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { |
3fa83edf | 536 | my ($chain, $sig) = ($1, $2); |
de2a57cd | 537 | return if !&$is_pvefw_chain($chain); |
3fa83edf | 538 | $res->{$chain} = $sig; |
de2a57cd DM |
539 | } else { |
540 | # simply ignore the rest | |
541 | return; | |
542 | } | |
543 | }; | |
544 | ||
545 | run_command("/sbin/iptables-save", outfunc => $parser); | |
546 | ||
de2a57cd DM |
547 | return $res; |
548 | } | |
549 | ||
3a616aa0 AD |
550 | sub iptables_chain_exist { |
551 | my ($chain) = @_; | |
552 | ||
553 | eval{ | |
554 | iptables("-n --list $chain"); | |
555 | }; | |
556 | return undef if $@; | |
557 | ||
558 | return 1; | |
559 | } | |
560 | ||
561 | sub iptables_rule_exist { | |
562 | my ($rule) = @_; | |
563 | ||
564 | eval{ | |
565 | iptables("-C $rule"); | |
566 | }; | |
567 | return undef if $@; | |
568 | ||
569 | return 1; | |
570 | } | |
571 | ||
3fa83edf | 572 | sub ruleset_generate_rule { |
4e6112f9 | 573 | my ($ruleset, $chain, $rule, $goto) = @_; |
3a616aa0 | 574 | |
3fa83edf | 575 | my $cmd = ''; |
3a616aa0 | 576 | |
d6de1dc2 | 577 | $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1; |
3a616aa0 | 578 | $cmd .= " -s $rule->{source}" if $rule->{source}; |
d6de1dc2 | 579 | $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1; |
8c32a215 | 580 | $cmd .= " -d $rule->{dest}" if $rule->{dest}; |
3a616aa0 | 581 | $cmd .= " -p $rule->{proto}" if $rule->{proto}; |
4cdbb3b7 | 582 | $cmd .= " --match multiport" if $rule->{nbdport} && $rule->{nbdport} > 1; |
3a616aa0 | 583 | $cmd .= " --dport $rule->{dport}" if $rule->{dport}; |
4cdbb3b7 | 584 | $cmd .= " --match multiport" if $rule->{nbsport} && $rule->{nbsport} > 1; |
3a616aa0 | 585 | $cmd .= " --sport $rule->{sport}" if $rule->{sport}; |
4e6112f9 DM |
586 | |
587 | if (my $action = $rule->{action}) { | |
588 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
589 | $cmd .= $goto ? " -g $action" : " -j $action"; | |
590 | }; | |
3a616aa0 | 591 | |
3fa83edf DM |
592 | ruleset_addrule($ruleset, $chain, $cmd) if $cmd; |
593 | } | |
594 | ||
595 | sub ruleset_create_chain { | |
596 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 597 | |
d050c724 DM |
598 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; |
599 | ||
3fa83edf DM |
600 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; |
601 | ||
602 | $ruleset->{$chain} = []; | |
3a616aa0 AD |
603 | } |
604 | ||
3fa83edf DM |
605 | sub ruleset_chain_exist { |
606 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 607 | |
3fa83edf DM |
608 | return $ruleset->{$chain} ? 1 : undef; |
609 | } | |
3a616aa0 | 610 | |
3fa83edf DM |
611 | sub ruleset_addrule { |
612 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 613 | |
3fa83edf | 614 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 615 | |
3fa83edf DM |
616 | push @{$ruleset->{$chain}}, "-A $chain $rule"; |
617 | } | |
3a616aa0 | 618 | |
3fa83edf DM |
619 | sub ruleset_insertrule { |
620 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 621 | |
3fa83edf | 622 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 623 | |
3fa83edf DM |
624 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; |
625 | } | |
3a616aa0 | 626 | |
3fa83edf DM |
627 | sub generate_bridge_chains { |
628 | my ($ruleset, $bridge) = @_; | |
3a616aa0 | 629 | |
dec84fcd DM |
630 | if (!ruleset_chain_exist($ruleset, "PVEFW-BRIDGE-IN")){ |
631 | ruleset_create_chain($ruleset, "PVEFW-BRIDGE-IN"); | |
d4d0fd1d AD |
632 | } |
633 | ||
dec84fcd DM |
634 | if (!ruleset_chain_exist($ruleset, "PVEFW-BRIDGE-OUT")){ |
635 | ruleset_create_chain($ruleset, "PVEFW-BRIDGE-OUT"); | |
d4d0fd1d | 636 | } |
3fa83edf | 637 | |
dec84fcd DM |
638 | if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){ |
639 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
3fa83edf | 640 | |
7fab95bc | 641 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); |
dec84fcd DM |
642 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-in --physdev-is-bridged -j PVEFW-BRIDGE-OUT"); |
643 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-out --physdev-is-bridged -j PVEFW-BRIDGE-IN"); | |
3a616aa0 AD |
644 | } |
645 | ||
3fa83edf DM |
646 | if (!ruleset_chain_exist($ruleset, "$bridge-IN")) { |
647 | ruleset_create_chain($ruleset, "$bridge-IN"); | |
dec84fcd DM |
648 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing |
649 | ruleset_addrule($ruleset, "PVEFW-BRIDGE-IN", "-j $bridge-IN"); | |
3fa83edf DM |
650 | ruleset_addrule($ruleset, "$bridge-IN", "-j ACCEPT"); |
651 | } | |
3a616aa0 | 652 | |
3fa83edf DM |
653 | if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) { |
654 | ruleset_create_chain($ruleset, "$bridge-OUT"); | |
dec84fcd DM |
655 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing |
656 | ruleset_addrule($ruleset, "PVEFW-BRIDGE-OUT", "-j $bridge-OUT"); | |
3fa83edf DM |
657 | } |
658 | } | |
3a616aa0 AD |
659 | |
660 | sub generate_tap_rules_direction { | |
51bae274 | 661 | my ($ruleset, $group_rules, $iface, $netid, $macaddr, $rules, $bridge, $direction) = @_; |
3a616aa0 AD |
662 | |
663 | my $tapchain = "$iface-$direction"; | |
664 | ||
3fa83edf | 665 | ruleset_create_chain($ruleset, $tapchain); |
3a616aa0 | 666 | |
7fab95bc DM |
667 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP"); |
668 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
3a616aa0 | 669 | |
c29f55c9 DM |
670 | if ($direction eq 'OUT' && defined($macaddr)) { |
671 | ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP"); | |
672 | } | |
673 | ||
3fa83edf | 674 | if ($rules) { |
3a616aa0 AD |
675 | foreach my $rule (@$rules) { |
676 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
bfbfa8b6 | 677 | # we go to $bridge-IN if accept in out rules |
3a616aa0 | 678 | if($rule->{action} =~ m/^(GROUP-(\S+))$/){ |
3fa83edf DM |
679 | $rule->{action} .= "-$direction"; |
680 | # generate empty group rule if don't exist | |
681 | if(!ruleset_chain_exist($ruleset, $rule->{action})){ | |
51bae274 | 682 | generate_group_rules($ruleset, $group_rules, $2); |
3fa83edf | 683 | } |
4e6112f9 | 684 | ruleset_generate_rule($ruleset, $tapchain, $rule); |
31ff3ef6 DM |
685 | ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -g $bridge-IN") |
686 | if $direction eq 'OUT'; | |
4e6112f9 | 687 | } else { |
4e6112f9 DM |
688 | $rule->{action} = "$bridge-IN" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT'; |
689 | ruleset_generate_rule($ruleset, $tapchain, $rule); | |
3a616aa0 | 690 | } |
4e6112f9 | 691 | } |
3a616aa0 AD |
692 | } |
693 | ||
3fa83edf DM |
694 | ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4"); |
695 | ruleset_addrule($ruleset, $tapchain, "-j DROP"); | |
3a616aa0 | 696 | |
3fa83edf DM |
697 | # plug the tap chain to bridge chain |
698 | my $physdevdirection = $direction eq 'IN' ? "out" : "in"; | |
699 | my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain"; | |
700 | ruleset_insertrule($ruleset, "$bridge-$direction", $rule); | |
3a616aa0 | 701 | |
3fa83edf DM |
702 | if ($direction eq 'OUT'){ |
703 | # add tap->host rules | |
704 | my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain"; | |
dec84fcd | 705 | ruleset_addrule($ruleset, "PVEFW-INPUT", $rule); |
3a616aa0 AD |
706 | } |
707 | } | |
708 | ||
0bd5f137 | 709 | sub enablehostfw { |
51bae274 | 710 | my ($ruleset, $rules, $group_rules) = @_; |
0bd5f137 | 711 | |
51bae274 | 712 | # fixme: allow security groups |
0bd5f137 | 713 | |
3fa83edf | 714 | # host inbound firewall |
dec84fcd DM |
715 | my $chain = "PVEFW-HOST-IN"; |
716 | ruleset_create_chain($ruleset, $chain); | |
0bd5f137 | 717 | |
7fab95bc DM |
718 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
719 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
720 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); |
721 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 722 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 723 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 724 | |
3fa83edf DM |
725 | if ($rules->{in}) { |
726 | foreach my $rule (@{$rules->{in}}) { | |
727 | # we use RETURN because we need to check also tap rules | |
728 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 729 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 730 | } |
0bd5f137 AD |
731 | } |
732 | ||
dec84fcd DM |
733 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4"); |
734 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
0bd5f137 | 735 | |
3fa83edf | 736 | # host outbound firewall |
aadd745e | 737 | $chain = "PVEFW-HOST-OUT"; |
dec84fcd DM |
738 | ruleset_create_chain($ruleset, $chain); |
739 | ||
7fab95bc DM |
740 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
741 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
742 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); |
743 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 744 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 745 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 746 | |
3fa83edf DM |
747 | if ($rules->{out}) { |
748 | foreach my $rule (@{$rules->{out}}) { | |
749 | # we use RETURN because we need to check also tap rules | |
750 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 751 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 752 | } |
0bd5f137 AD |
753 | } |
754 | ||
dec84fcd DM |
755 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4"); |
756 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
9d31b418 | 757 | |
dec84fcd DM |
758 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); |
759 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
9d31b418 AD |
760 | } |
761 | ||
762 | sub generate_group_rules { | |
51bae274 | 763 | my ($ruleset, $group_rules, $group) = @_; |
9d31b418 | 764 | |
51bae274 | 765 | my $rules = $group_rules->{$group}; |
9d31b418 | 766 | |
51bae274 DM |
767 | die "no such security group '$group'\n" if !$rules; |
768 | ||
3fa83edf | 769 | my $chain = "GROUP-${group}-IN"; |
9d31b418 | 770 | |
3fa83edf | 771 | ruleset_create_chain($ruleset, $chain); |
9d31b418 | 772 | |
3fa83edf DM |
773 | if ($rules->{in}) { |
774 | foreach my $rule (@{$rules->{in}}) { | |
775 | ruleset_generate_rule($ruleset, $chain, $rule); | |
9d31b418 AD |
776 | } |
777 | } | |
778 | ||
3fa83edf | 779 | $chain = "GROUP-${group}-OUT"; |
9d31b418 | 780 | |
3fa83edf | 781 | ruleset_create_chain($ruleset, $chain); |
4e6112f9 | 782 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark |
9d31b418 | 783 | |
3fa83edf DM |
784 | if ($rules->{out}) { |
785 | foreach my $rule (@{$rules->{out}}) { | |
4e6112f9 DM |
786 | # we go the PVEFW-SET-ACCEPT-MARK Instead of ACCEPT) because we need to |
787 | # check also other tap rules (and group rules can be set on any bridge, | |
788 | # so we can't go to VMBRXX-IN) | |
789 | $rule->{action} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action} eq 'ACCEPT'; | |
6a6c7af7 | 790 | ruleset_generate_rule($ruleset, $chain, $rule); |
9d31b418 AD |
791 | } |
792 | } | |
9d31b418 AD |
793 | } |
794 | ||
51bae274 DM |
795 | my $MAX_NETS = 32; |
796 | my $valid_netdev_names = {}; | |
797 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
798 | $valid_netdev_names->{"net$i"} = 1; | |
799 | } | |
5e1267a5 | 800 | |
51bae274 DM |
801 | sub parse_fw_rule { |
802 | my ($line, $need_iface, $allow_groups) = @_; | |
5e1267a5 | 803 | |
a332200b | 804 | my $macros = get_firewall_macros(); |
fcba0beb | 805 | my $protocols = get_etc_protocols(); |
51bae274 DM |
806 | |
807 | my ($action, $iface, $source, $dest, $proto, $dport, $sport); | |
808 | ||
809 | $line =~ s/#.*$//; | |
810 | ||
811 | my @data = split(/\s+/, $line); | |
812 | my $expected_elements = $need_iface ? 7 : 6; | |
813 | ||
814 | die "wrong number of rule elements\n" if scalar(@data) > $expected_elements; | |
815 | ||
816 | if ($need_iface) { | |
817 | ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data | |
818 | } else { | |
819 | ($action, $source, $dest, $proto, $dport, $sport) = @data; | |
820 | } | |
821 | ||
822 | die "incomplete rule\n" if !$action; | |
823 | ||
961b4928 DM |
824 | my $macro; |
825 | my $macro_name; | |
826 | ||
51bae274 DM |
827 | if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) { |
828 | # OK | |
829 | } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) { | |
830 | # OK | |
831 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
961b4928 DM |
832 | ($macro_name, $action) = ($1, $2); |
833 | my $lc_macro_name = lc($macro_name); | |
834 | my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name}; | |
835 | $macro_name = $preferred_name if $preferred_name; | |
836 | $macro = $macros->{$lc_macro_name}; | |
837 | die "unknown macro '$macro_name'\n" if !$macro; | |
51bae274 DM |
838 | } else { |
839 | die "unknown action '$action'\n"; | |
840 | } | |
841 | ||
842 | if ($need_iface) { | |
843 | $iface = undef if $iface && $iface eq '-'; | |
844 | die "unknown interface '$iface'\n" | |
845 | if defined($iface) && !$valid_netdev_names->{$iface}; | |
846 | } | |
847 | ||
848 | $proto = undef if $proto && $proto eq '-'; | |
849 | die "unknown protokol '$proto'\n" if $proto && | |
850 | !(defined($protocols->{byname}->{$proto}) || | |
851 | defined($protocols->{byid}->{$proto})); | |
852 | ||
853 | $source = undef if $source && $source eq '-'; | |
854 | $dest = undef if $dest && $dest eq '-'; | |
855 | ||
856 | $dport = undef if $dport && $dport eq '-'; | |
857 | $sport = undef if $sport && $sport eq '-'; | |
858 | ||
51bae274 DM |
859 | my $nbsource = undef; |
860 | my $nbdest = undef; | |
861 | ||
862 | $nbsource = parse_address_list($source) if $source; | |
863 | $nbdest = parse_address_list($dest) if $dest; | |
961b4928 DM |
864 | |
865 | my $rules = []; | |
51bae274 | 866 | |
961b4928 | 867 | my $param = { |
51bae274 | 868 | action => $action, |
51bae274 DM |
869 | iface => $iface, |
870 | source => $source, | |
871 | dest => $dest, | |
872 | nbsource => $nbsource, | |
873 | nbdest => $nbdest, | |
874 | proto => $proto, | |
875 | dport => $dport, | |
876 | sport => $sport, | |
51bae274 | 877 | }; |
961b4928 DM |
878 | |
879 | if ($macro) { | |
880 | foreach my $templ (@$macro) { | |
881 | my $rule = {}; | |
882 | foreach my $k (keys %$templ) { | |
883 | my $v = $templ->{$k}; | |
857f62c8 DM |
884 | if ($v eq 'PARAM') { |
885 | $v = $param->{$k}; | |
886 | } elsif ($v eq 'DEST') { | |
887 | $v = $param->{dest}; | |
888 | } elsif ($v eq 'SOURCE') { | |
889 | $v = $param->{source}; | |
890 | } | |
891 | ||
961b4928 DM |
892 | die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v); |
893 | $rule->{$k} = $v; | |
894 | } | |
895 | push @$rules, $rule; | |
896 | } | |
897 | } else { | |
898 | push @$rules, $param; | |
899 | } | |
900 | ||
901 | foreach my $rule (@$rules) { | |
902 | $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport}) | |
903 | if defined($rule->{dport}); | |
904 | $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport}) | |
905 | if defined($rule->{sport}); | |
906 | } | |
907 | ||
908 | return $rules; | |
51bae274 DM |
909 | } |
910 | ||
85c6eaed DM |
911 | sub parse_fw_option { |
912 | my ($line) = @_; | |
913 | ||
914 | my ($opt, $value); | |
915 | ||
916 | if ($line =~ m/^enable:\s*(0|1)\s*$/i) { | |
917 | $opt = 'enable'; | |
918 | $value = int($1); | |
919 | } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
920 | $opt = lc($1); | |
921 | $value = uc($3); | |
922 | } else { | |
923 | chomp $line; | |
924 | die "can't parse option '$line'\n" | |
925 | } | |
926 | ||
927 | return ($opt, $value); | |
928 | } | |
929 | ||
51bae274 DM |
930 | sub parse_vm_fw_rules { |
931 | my ($filename, $fh) = @_; | |
932 | ||
85c6eaed | 933 | my $res = { in => [], out => [], options => {}}; |
51bae274 DM |
934 | |
935 | my $section; | |
936 | ||
5e1267a5 DM |
937 | while (defined(my $line = <$fh>)) { |
938 | next if $line =~ m/^#/; | |
939 | next if $line =~ m/^\s*$/; | |
940 | ||
961b4928 DM |
941 | my $linenr = $fh->input_line_number(); |
942 | my $prefix = "$filename (line $linenr)"; | |
943 | ||
85c6eaed | 944 | if ($line =~ m/^\[(\S+)\]\s*$/i) { |
5e1267a5 | 945 | $section = lc($1); |
85c6eaed | 946 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; |
5e1267a5 DM |
947 | next; |
948 | } | |
51bae274 | 949 | if (!$section) { |
961b4928 | 950 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
951 | next; |
952 | } | |
953 | ||
85c6eaed DM |
954 | next if !$res->{$section}; # skip undefined section |
955 | ||
956 | if ($section eq 'options') { | |
957 | eval { | |
958 | my ($opt, $value) = parse_fw_option($line); | |
959 | $res->{options}->{$opt} = $value; | |
960 | }; | |
961 | warn "$prefix: $@" if $@; | |
962 | next; | |
963 | } | |
964 | ||
961b4928 DM |
965 | my $rules; |
966 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
51bae274 | 967 | if (my $err = $@) { |
961b4928 | 968 | warn "$prefix: $err"; |
5e1267a5 DM |
969 | next; |
970 | } | |
971 | ||
961b4928 | 972 | push @{$res->{$section}}, @$rules; |
51bae274 DM |
973 | } |
974 | ||
975 | return $res; | |
976 | } | |
977 | ||
978 | sub parse_host_fw_rules { | |
979 | my ($filename, $fh) = @_; | |
980 | ||
981 | my $res = { in => [], out => [] }; | |
982 | ||
983 | my $section; | |
984 | ||
985 | while (defined(my $line = <$fh>)) { | |
986 | next if $line =~ m/^#/; | |
987 | next if $line =~ m/^\s*$/; | |
988 | ||
961b4928 DM |
989 | my $linenr = $fh->input_line_number(); |
990 | my $prefix = "$filename (line $linenr)"; | |
991 | ||
51bae274 DM |
992 | if ($line =~ m/^\[(in|out)\]\s*$/i) { |
993 | $section = lc($1); | |
5e1267a5 DM |
994 | next; |
995 | } | |
51bae274 | 996 | if (!$section) { |
961b4928 | 997 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
998 | next; |
999 | } | |
1000 | ||
961b4928 DM |
1001 | my $rules; |
1002 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
ecbea048 | 1003 | if (my $err = $@) { |
961b4928 | 1004 | warn "$prefix: $err"; |
ecbea048 | 1005 | next; |
ecbea048 DM |
1006 | } |
1007 | ||
961b4928 | 1008 | push @{$res->{$section}}, @$rules; |
51bae274 | 1009 | } |
ecbea048 | 1010 | |
51bae274 DM |
1011 | return $res; |
1012 | } | |
4cdbb3b7 | 1013 | |
51bae274 DM |
1014 | sub parse_group_fw_rules { |
1015 | my ($filename, $fh) = @_; | |
5e1267a5 | 1016 | |
51bae274 DM |
1017 | my $section; |
1018 | my $group; | |
1019 | ||
1020 | my $res = { in => [], out => [] }; | |
1021 | ||
1022 | while (defined(my $line = <$fh>)) { | |
1023 | next if $line =~ m/^#/; | |
1024 | next if $line =~ m/^\s*$/; | |
1025 | ||
961b4928 DM |
1026 | my $linenr = $fh->input_line_number(); |
1027 | my $prefix = "$filename (line $linenr)"; | |
1028 | ||
51bae274 DM |
1029 | if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) { |
1030 | $section = lc($1); | |
1031 | $group = lc($2); | |
1032 | next; | |
1033 | } | |
1034 | if (!$section || !$group) { | |
961b4928 | 1035 | warn "$prefix: skip line - no section"; |
51bae274 DM |
1036 | next; |
1037 | } | |
1038 | ||
961b4928 DM |
1039 | my $rules; |
1040 | eval { $rules = parse_fw_rule($line, 0, 0); }; | |
51bae274 | 1041 | if (my $err = $@) { |
961b4928 | 1042 | warn "$prefix: $err"; |
51bae274 DM |
1043 | next; |
1044 | } | |
1045 | ||
961b4928 | 1046 | push @{$res->{$group}->{$section}}, @$rules; |
5e1267a5 DM |
1047 | } |
1048 | ||
1049 | return $res; | |
1050 | } | |
1051 | ||
06320eb0 DM |
1052 | sub run_locked { |
1053 | my ($code, @param) = @_; | |
1054 | ||
1055 | my $timeout = 10; | |
1056 | ||
1057 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
1058 | ||
1059 | die $@ if $@; | |
1060 | ||
1061 | return $res; | |
1062 | } | |
1063 | ||
5e1267a5 DM |
1064 | sub read_local_vm_config { |
1065 | ||
1066 | my $openvz = {}; | |
1067 | ||
1068 | my $qemu = {}; | |
1069 | ||
1070 | my $list = PVE::QemuServer::config_list(); | |
1071 | ||
1072 | foreach my $vmid (keys %$list) { | |
5e1267a5 DM |
1073 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); |
1074 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
1075 | $qemu->{$vmid} = $conf; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
1080 | ||
1081 | return $vmdata; | |
1082 | }; | |
1083 | ||
1084 | sub read_vm_firewall_rules { | |
1085 | my ($vmdata) = @_; | |
1086 | my $rules = {}; | |
1087 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
1088 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
1089 | my $fh = IO::File->new($filename, O_RDONLY); | |
1090 | next if !$fh; | |
1091 | ||
51bae274 | 1092 | $rules->{$vmid} = parse_vm_fw_rules($filename, $fh); |
5e1267a5 DM |
1093 | } |
1094 | ||
1095 | return $rules; | |
1096 | } | |
1097 | ||
1098 | sub compile { | |
5e1267a5 DM |
1099 | my $vmdata = read_local_vm_config(); |
1100 | my $rules = read_vm_firewall_rules($vmdata); | |
51bae274 DM |
1101 | |
1102 | my $group_rules = {}; | |
1103 | my $filename = "/etc/pve/firewall/groups.fw"; | |
1104 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1105 | $group_rules = parse_group_fw_rules($filename, $fh); | |
1106 | } | |
5e1267a5 | 1107 | |
3fa83edf DM |
1108 | #print Dumper($rules); |
1109 | ||
1110 | my $ruleset = {}; | |
1111 | ||
dec84fcd DM |
1112 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); |
1113 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
fadb13dd | 1114 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); |
3fa83edf | 1115 | |
4e6112f9 DM |
1116 | ruleset_create_chain($ruleset, "PVEFW-SET-ACCEPT-MARK"); |
1117 | ruleset_addrule($ruleset, "PVEFW-SET-ACCEPT-MARK", "-j MARK --set-mark 1"); | |
1118 | ||
fadb13dd | 1119 | my $enable_hostfw = 0; |
51bae274 DM |
1120 | $filename = "/etc/pve/local/host.fw"; |
1121 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1122 | my $host_rules = parse_host_fw_rules($filename, $fh); | |
fadb13dd DM |
1123 | |
1124 | $enable_hostfw = 1; | |
1125 | ||
51bae274 DM |
1126 | enablehostfw($ruleset, $host_rules, $group_rules); |
1127 | } | |
3fa83edf DM |
1128 | |
1129 | # generate firewall rules for QEMU VMs | |
1130 | foreach my $vmid (keys %{$vmdata->{qemu}}) { | |
1131 | my $conf = $vmdata->{qemu}->{$vmid}; | |
fadb13dd | 1132 | |
3fa83edf | 1133 | next if !$rules->{$vmid}; |
fadb13dd DM |
1134 | my $options = $rules->{$vmid}->{options}; |
1135 | next if defined($options->{enable}) && ($options->{enable} == 0); | |
3fa83edf DM |
1136 | |
1137 | foreach my $netid (keys %$conf) { | |
1138 | next if $netid !~ m/^net(\d+)$/; | |
1139 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
1140 | next if !$net; | |
1141 | my $iface = "tap${vmid}i$1"; | |
5e1267a5 | 1142 | |
3fa83edf DM |
1143 | my $bridge = $net->{bridge}; |
1144 | next if !$bridge; # fixme: ? | |
1145 | ||
1146 | $bridge .= "v$net->{tag}" if $net->{tag}; | |
1147 | ||
fadb13dd | 1148 | |
3fa83edf DM |
1149 | generate_bridge_chains($ruleset, $bridge); |
1150 | ||
c29f55c9 | 1151 | my $macaddr = $net->{macaddr}; |
51bae274 DM |
1152 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $rules->{$vmid}->{in}, $bridge, 'IN'); |
1153 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $rules->{$vmid}->{out}, $bridge, 'OUT'); | |
3fa83edf DM |
1154 | } |
1155 | } | |
c0413e35 | 1156 | |
fadb13dd DM |
1157 | if ($enable_hostfw) { |
1158 | # allow traffic from lo (ourself) | |
1159 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT"); | |
1160 | } | |
c0413e35 | 1161 | |
3fa83edf DM |
1162 | return $ruleset; |
1163 | } | |
1164 | ||
1165 | sub get_ruleset_status { | |
1166 | my ($ruleset, $verbose) = @_; | |
1167 | ||
1168 | my $active_chains = iptables_get_chains(); | |
1169 | ||
1170 | my $statushash = {}; | |
1171 | ||
1172 | foreach my $chain (sort keys %$ruleset) { | |
09d5f68e | 1173 | my $digest = Digest::SHA->new('sha1'); |
3fa83edf DM |
1174 | foreach my $cmd (@{$ruleset->{$chain}}) { |
1175 | $digest->add("$cmd\n"); | |
1176 | } | |
1177 | my $sig = $digest->b64digest; | |
1178 | $statushash->{$chain}->{sig} = $sig; | |
1179 | ||
1180 | my $oldsig = $active_chains->{$chain}; | |
1181 | if (!defined($oldsig)) { | |
1182 | $statushash->{$chain}->{action} = 'create'; | |
1183 | } else { | |
1184 | if ($oldsig eq $sig) { | |
1185 | $statushash->{$chain}->{action} = 'exists'; | |
1186 | } else { | |
1187 | $statushash->{$chain}->{action} = 'update'; | |
1188 | } | |
1189 | } | |
1190 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
1191 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1192 | print "\t$cmd\n" if $verbose; | |
1193 | } | |
1194 | } | |
1195 | ||
1196 | foreach my $chain (sort keys %$active_chains) { | |
1197 | if (!defined($ruleset->{$chain})) { | |
1198 | my $sig = $active_chains->{$chain}; | |
1199 | $statushash->{$chain}->{action} = 'delete'; | |
1200 | $statushash->{$chain}->{sig} = $sig; | |
1201 | print "delete $chain ($sig)\n" if $verbose; | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | return $statushash; | |
1206 | } | |
1207 | ||
1208 | sub print_ruleset { | |
1209 | my ($ruleset) = @_; | |
1210 | ||
1211 | get_ruleset_status($ruleset, 1); | |
1212 | } | |
1213 | ||
1214 | sub print_sig_rule { | |
1215 | my ($chain, $sig) = @_; | |
1216 | ||
09d5f68e DM |
1217 | # We just use this to store a SHA1 checksum used to detect changes |
1218 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
b6360c3f DM |
1219 | } |
1220 | ||
a84f4d96 DM |
1221 | sub apply_ruleset { |
1222 | my ($ruleset, $verbose) = @_; | |
3fa83edf | 1223 | |
a84f4d96 | 1224 | enable_bridge_firewall(); |
3fa83edf DM |
1225 | |
1226 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
1227 | ||
1228 | my $statushash = get_ruleset_status($ruleset, $verbose); | |
1229 | ||
1230 | # create missing chains first | |
1231 | foreach my $chain (sort keys %$ruleset) { | |
1232 | my $stat = $statushash->{$chain}; | |
1233 | die "internal error" if !$stat; | |
1234 | next if $stat->{action} ne 'create'; | |
886aba9c | 1235 | |
3fa83edf DM |
1236 | $cmdlist .= ":$chain - [0:0]\n"; |
1237 | } | |
1238 | ||
dec84fcd | 1239 | my $rule = "INPUT -j PVEFW-INPUT"; |
3fa83edf DM |
1240 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1241 | $cmdlist .= "-A $rule\n"; | |
1242 | } | |
dec84fcd | 1243 | $rule = "OUTPUT -j PVEFW-OUTPUT"; |
3fa83edf DM |
1244 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1245 | $cmdlist .= "-A $rule\n"; | |
1246 | } | |
1247 | ||
dec84fcd | 1248 | $rule = "FORWARD -j PVEFW-FORWARD"; |
3fa83edf DM |
1249 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1250 | $cmdlist .= "-A $rule\n"; | |
1251 | } | |
1252 | ||
1253 | foreach my $chain (sort keys %$ruleset) { | |
1254 | my $stat = $statushash->{$chain}; | |
1255 | die "internal error" if !$stat; | |
1256 | ||
1257 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
1258 | $cmdlist .= "-F $chain\n"; | |
1259 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1260 | $cmdlist .= "$cmd\n"; | |
1261 | } | |
1262 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
1263 | } elsif ($stat->{action} eq 'delete') { | |
f5d28682 | 1264 | die "internal error"; # this should not happen |
3fa83edf DM |
1265 | } elsif ($stat->{action} eq 'exists') { |
1266 | # do nothing | |
1267 | } else { | |
1268 | die "internal error - unknown status '$stat->{action}'"; | |
1269 | } | |
1270 | } | |
1271 | ||
f5d28682 DM |
1272 | foreach my $chain (keys %$statushash) { |
1273 | next if $statushash->{$chain}->{action} ne 'delete'; | |
1274 | $cmdlist .= "-F $chain\n"; | |
1275 | } | |
1276 | foreach my $chain (keys %$statushash) { | |
1277 | next if $statushash->{$chain}->{action} ne 'delete'; | |
fadb13dd DM |
1278 | next if $chain eq 'PVEFW-INPUT'; |
1279 | next if $chain eq 'PVEFW-OUTPUT'; | |
1280 | next if $chain eq 'PVEFW-FORWARD'; | |
f5d28682 DM |
1281 | $cmdlist .= "-X $chain\n"; |
1282 | } | |
1283 | ||
3fa83edf DM |
1284 | $cmdlist .= "COMMIT\n"; |
1285 | ||
1286 | print $cmdlist if $verbose; | |
1287 | ||
1288 | iptables_restore_cmdlist($cmdlist); | |
1289 | ||
1290 | # test: re-read status and check if everything is up to date | |
1291 | $statushash = get_ruleset_status($ruleset); | |
1292 | ||
1293 | my $errors; | |
1294 | foreach my $chain (sort keys %$ruleset) { | |
1295 | my $stat = $statushash->{$chain}; | |
1296 | if ($stat->{action} ne 'exists') { | |
1297 | warn "unable to update chain '$chain'\n"; | |
1298 | $errors = 1; | |
1299 | } | |
1300 | } | |
b6360c3f | 1301 | |
3fa83edf | 1302 | die "unable to apply firewall changes\n" if $errors; |
b6360c3f DM |
1303 | } |
1304 | ||
b6360c3f | 1305 | 1; |