]> git.proxmox.com Git - pve-firewall.git/blob - src/PVE/Firewall.pm
add veth chain to is_pvefw_chain()
[pve-firewall.git] / src / PVE / Firewall.pm
1 package PVE::Firewall;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use Digest::SHA;
7 use PVE::INotify;
8 use PVE::Cluster;
9 use PVE::ProcFSTools;
10 use PVE::Tools;
11 use PVE::QemuServer;
12 use PVE::OpenVZ; # dependeny problem?!
13 use File::Basename;
14 use File::Path;
15 use IO::File;
16 use Net::IP;
17 use PVE::Tools qw(run_command lock_file);
18
19 use Data::Dumper;
20
21 my $nodename = PVE::INotify::nodename();
22
23 my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
24 my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status";
25
26 my $default_log_level = 'info';
27
28 my $log_level_hash = {
29 debug => 7,
30 info => 6,
31 notice => 5,
32 warning => 4,
33 err => 3,
34 crit => 2,
35 alert => 1,
36 emerg => 0,
37 };
38
39 # imported/converted from: /usr/share/shorewall/macro.*
40 my $pve_fw_macros = {
41 'Amanda' => [
42 { action => 'PARAM', proto => 'udp', dport => '10080' },
43 { action => 'PARAM', proto => 'tcp', dport => '10080' },
44 ],
45 'Auth' => [
46 { action => 'PARAM', proto => 'tcp', dport => '113' },
47 ],
48 'BGP' => [
49 { action => 'PARAM', proto => 'tcp', dport => '179' },
50 ],
51 'BitTorrent' => [
52 { action => 'PARAM', proto => 'tcp', dport => '6881:6889' },
53 { action => 'PARAM', proto => 'udp', dport => '6881' },
54 ],
55 'BitTorrent32' => [
56 { action => 'PARAM', proto => 'tcp', dport => '6881:6999' },
57 { action => 'PARAM', proto => 'udp', dport => '6881' },
58 ],
59 'CVS' => [
60 { action => 'PARAM', proto => 'tcp', dport => '2401' },
61 ],
62 'Citrix' => [
63 { action => 'PARAM', proto => 'tcp', dport => '1494' },
64 { action => 'PARAM', proto => 'udp', dport => '1604' },
65 { action => 'PARAM', proto => 'tcp', dport => '2598' },
66 ],
67 'DAAP' => [
68 { action => 'PARAM', proto => 'tcp', dport => '3689' },
69 { action => 'PARAM', proto => 'udp', dport => '3689' },
70 ],
71 'DCC' => [
72 { action => 'PARAM', proto => 'tcp', dport => '6277' },
73 ],
74 'DHCPfwd' => [
75 { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' },
76 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' },
77 ],
78 'DNS' => [
79 { action => 'PARAM', proto => 'udp', dport => '53' },
80 { action => 'PARAM', proto => 'tcp', dport => '53' },
81 ],
82 'Distcc' => [
83 { action => 'PARAM', proto => 'tcp', dport => '3632' },
84 ],
85 'Edonkey' => [
86 { action => 'PARAM', proto => 'tcp', dport => '4662' },
87 { action => 'PARAM', proto => 'udp', dport => '4665' },
88 ],
89 'FTP' => [
90 { action => 'PARAM', proto => 'tcp', dport => '21' },
91 ],
92 'Finger' => [
93 { action => 'PARAM', proto => 'tcp', dport => '79' },
94 ],
95 'GNUnet' => [
96 { action => 'PARAM', proto => 'tcp', dport => '2086' },
97 { action => 'PARAM', proto => 'udp', dport => '2086' },
98 { action => 'PARAM', proto => 'tcp', dport => '1080' },
99 { action => 'PARAM', proto => 'udp', dport => '1080' },
100 ],
101 'GRE' => [
102 { action => 'PARAM', proto => '47' },
103 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
104 ],
105 'Git' => [
106 { action => 'PARAM', proto => 'tcp', dport => '9418' },
107 ],
108 'Gnutella' => [
109 { action => 'PARAM', proto => 'tcp', dport => '6346' },
110 { action => 'PARAM', proto => 'udp', dport => '6346' },
111 ],
112 'HKP' => [
113 { action => 'PARAM', proto => 'tcp', dport => '11371' },
114 ],
115 'HTTP' => [
116 { action => 'PARAM', proto => 'tcp', dport => '80' },
117 ],
118 'HTTPS' => [
119 { action => 'PARAM', proto => 'tcp', dport => '443' },
120 ],
121 'ICPV2' => [
122 { action => 'PARAM', proto => 'udp', dport => '3130' },
123 ],
124 'ICQ' => [
125 { action => 'PARAM', proto => 'tcp', dport => '5190' },
126 ],
127 'IMAP' => [
128 { action => 'PARAM', proto => 'tcp', dport => '143' },
129 ],
130 'IMAPS' => [
131 { action => 'PARAM', proto => 'tcp', dport => '993' },
132 ],
133 'IPIP' => [
134 { action => 'PARAM', proto => '94' },
135 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' },
136 ],
137 'IPP' => [
138 { action => 'PARAM', proto => 'tcp', dport => '631' },
139 ],
140 'IPPbrd' => [
141 { action => 'PARAM', proto => 'udp', dport => '631' },
142 ],
143 'IPPserver' => [
144 { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' },
145 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' },
146 ],
147 'IPsec' => [
148 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
149 { action => 'PARAM', proto => '50' },
150 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
151 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
152 ],
153 'IPsecah' => [
154 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
155 { action => 'PARAM', proto => '51' },
156 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
157 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' },
158 ],
159 'IPsecnat' => [
160 { action => 'PARAM', proto => 'udp', dport => '500' },
161 { action => 'PARAM', proto => 'udp', dport => '4500' },
162 { action => 'PARAM', proto => '50' },
163 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' },
164 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' },
165 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
166 ],
167 'IRC' => [
168 { action => 'PARAM', proto => 'tcp', dport => '6667' },
169 ],
170 'JabberPlain' => [
171 { action => 'PARAM', proto => 'tcp', dport => '5222' },
172 ],
173 'JabberSecure' => [
174 { action => 'PARAM', proto => 'tcp', dport => '5223' },
175 ],
176 'Jabberd' => [
177 { action => 'PARAM', proto => 'tcp', dport => '5269' },
178 ],
179 'Jetdirect' => [
180 { action => 'PARAM', proto => 'tcp', dport => '9100' },
181 ],
182 'L2TP' => [
183 { action => 'PARAM', proto => 'udp', dport => '1701' },
184 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' },
185 ],
186 'LDAP' => [
187 { action => 'PARAM', proto => 'tcp', dport => '389' },
188 ],
189 'LDAPS' => [
190 { action => 'PARAM', proto => 'tcp', dport => '636' },
191 ],
192 'MSNP' => [
193 { action => 'PARAM', proto => 'tcp', dport => '1863' },
194 ],
195 'MSSQL' => [
196 { action => 'PARAM', proto => 'tcp', dport => '1433' },
197 ],
198 'Mail' => [
199 { action => 'PARAM', proto => 'tcp', dport => '25' },
200 { action => 'PARAM', proto => 'tcp', dport => '465' },
201 { action => 'PARAM', proto => 'tcp', dport => '587' },
202 ],
203 'Munin' => [
204 { action => 'PARAM', proto => 'tcp', dport => '4949' },
205 ],
206 'MySQL' => [
207 { action => 'PARAM', proto => 'tcp', dport => '3306' },
208 ],
209 'NNTP' => [
210 { action => 'PARAM', proto => 'tcp', dport => '119' },
211 ],
212 'NNTPS' => [
213 { action => 'PARAM', proto => 'tcp', dport => '563' },
214 ],
215 'NTP' => [
216 { action => 'PARAM', proto => 'udp', dport => '123' },
217 ],
218 'NTPbi' => [
219 { action => 'PARAM', proto => 'udp', dport => '123' },
220 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' },
221 ],
222 'NTPbrd' => [
223 { action => 'PARAM', proto => 'udp', dport => '123' },
224 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' },
225 ],
226 'OSPF' => [
227 { action => 'PARAM', proto => '89' },
228 ],
229 'OpenVPN' => [
230 { action => 'PARAM', proto => 'udp', dport => '1194' },
231 ],
232 'PCA' => [
233 { action => 'PARAM', proto => 'udp', dport => '5632' },
234 { action => 'PARAM', proto => 'tcp', dport => '5631' },
235 ],
236 'POP3' => [
237 { action => 'PARAM', proto => 'tcp', dport => '110' },
238 ],
239 'POP3S' => [
240 { action => 'PARAM', proto => 'tcp', dport => '995' },
241 ],
242 'PPtP' => [
243 { action => 'PARAM', proto => '47' },
244 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
245 { action => 'PARAM', proto => 'tcp', dport => '1723' },
246 ],
247 'Ping' => [
248 { action => 'PARAM', proto => 'icmp', dport => 'echo-request' },
249 ],
250 'PostgreSQL' => [
251 { action => 'PARAM', proto => 'tcp', dport => '5432' },
252 ],
253 'Printer' => [
254 { action => 'PARAM', proto => 'tcp', dport => '515' },
255 ],
256 'RDP' => [
257 { action => 'PARAM', proto => 'tcp', dport => '3389' },
258 ],
259 'RIPbi' => [
260 { action => 'PARAM', proto => 'udp', dport => '520' },
261 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' },
262 ],
263 'RNDC' => [
264 { action => 'PARAM', proto => 'tcp', dport => '953' },
265 ],
266 'Razor' => [
267 { action => 'ACCEPT', proto => 'tcp', dport => '2703' },
268 ],
269 'Rdate' => [
270 { action => 'PARAM', proto => 'tcp', dport => '37' },
271 ],
272 'Rsync' => [
273 { action => 'PARAM', proto => 'tcp', dport => '873' },
274 ],
275 'SANE' => [
276 { action => 'PARAM', proto => 'tcp', dport => '6566' },
277 ],
278 'SMB' => [
279 { action => 'PARAM', proto => 'udp', dport => '135,445' },
280 { action => 'PARAM', proto => 'udp', dport => '137:139' },
281 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
282 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
283 ],
284 'SMBBI' => [
285 { action => 'PARAM', proto => 'udp', dport => '135,445' },
286 { action => 'PARAM', proto => 'udp', dport => '137:139' },
287 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
288 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
289 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' },
290 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' },
291 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' },
292 { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' },
293 ],
294 'SMBswat' => [
295 { action => 'PARAM', proto => 'tcp', dport => '901' },
296 ],
297 'SMTP' => [
298 { action => 'PARAM', proto => 'tcp', dport => '25' },
299 ],
300 'SMTPS' => [
301 { action => 'PARAM', proto => 'tcp', dport => '465' },
302 ],
303 'SNMP' => [
304 { action => 'PARAM', proto => 'udp', dport => '161:162' },
305 { action => 'PARAM', proto => 'tcp', dport => '161' },
306 ],
307 'SPAMD' => [
308 { action => 'PARAM', proto => 'tcp', dport => '783' },
309 ],
310 'SSH' => [
311 { action => 'PARAM', proto => 'tcp', dport => '22' },
312 ],
313 'SVN' => [
314 { action => 'PARAM', proto => 'tcp', dport => '3690' },
315 ],
316 'SixXS' => [
317 { action => 'PARAM', proto => 'tcp', dport => '3874' },
318 { action => 'PARAM', proto => 'udp', dport => '3740' },
319 { action => 'PARAM', proto => '41' },
320 { action => 'PARAM', proto => 'udp', dport => '5072,8374' },
321 ],
322 'Squid' => [
323 { action => 'PARAM', proto => 'tcp', dport => '3128' },
324 ],
325 'Submission' => [
326 { action => 'PARAM', proto => 'tcp', dport => '587' },
327 ],
328 'Syslog' => [
329 { action => 'PARAM', proto => 'udp', dport => '514' },
330 { action => 'PARAM', proto => 'tcp', dport => '514' },
331 ],
332 'TFTP' => [
333 { action => 'PARAM', proto => 'udp', dport => '69' },
334 ],
335 'Telnet' => [
336 { action => 'PARAM', proto => 'tcp', dport => '23' },
337 ],
338 'Telnets' => [
339 { action => 'PARAM', proto => 'tcp', dport => '992' },
340 ],
341 'Time' => [
342 { action => 'PARAM', proto => 'tcp', dport => '37' },
343 ],
344 'Trcrt' => [
345 { action => 'PARAM', proto => 'udp', dport => '33434:33524' },
346 { action => 'PARAM', proto => 'icmp', dport => 'echo-request' },
347 ],
348 'VNC' => [
349 { action => 'PARAM', proto => 'tcp', dport => '5900:5909' },
350 ],
351 'VNCL' => [
352 { action => 'PARAM', proto => 'tcp', dport => '5500' },
353 ],
354 'Web' => [
355 { action => 'PARAM', proto => 'tcp', dport => '80' },
356 { action => 'PARAM', proto => 'tcp', dport => '443' },
357 ],
358 'Webcache' => [
359 { action => 'PARAM', proto => 'tcp', dport => '8080' },
360 ],
361 'Webmin' => [
362 { action => 'PARAM', proto => 'tcp', dport => '10000' },
363 ],
364 'Whois' => [
365 { action => 'PARAM', proto => 'tcp', dport => '43' },
366 ],
367 };
368
369 my $pve_fw_parsed_macros;
370 my $pve_fw_preferred_macro_names = {};
371
372 my $pve_std_chains = {
373 'PVEFW-SET-ACCEPT-MARK' => [
374 "-j MARK --set-mark 1",
375 ],
376 'PVEFW-DropBroadcast' => [
377 # same as shorewall 'Broadcast'
378 # simply DROP BROADCAST/MULTICAST/ANYCAST
379 # we can use this to reduce logging
380 { action => 'DROP', dsttype => 'BROADCAST' },
381 { action => 'DROP', dsttype => 'MULTICAST' },
382 { action => 'DROP', dsttype => 'ANYCAST' },
383 { action => 'DROP', dest => '224.0.0.0/4' },
384 ],
385 'PVEFW-reject' => [
386 # same as shorewall 'reject'
387 { action => 'DROP', dsttype => 'BROADCAST' },
388 { action => 'DROP', source => '224.0.0.0/4' },
389 { action => 'DROP', proto => 'icmp' },
390 "-p tcp -j REJECT --reject-with tcp-reset",
391 "-p udp -j REJECT --reject-with icmp-port-unreachable",
392 "-p icmp -j REJECT --reject-with icmp-host-unreachable",
393 "-j REJECT --reject-with icmp-host-prohibited",
394 ],
395 'PVEFW-Drop' => [
396 # same as shorewall 'Drop', which is equal to DROP,
397 # but REJECT/DROP some packages to reduce logging,
398 # and ACCEPT critical ICMP types
399 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
400 # we are not interested in BROADCAST/MULTICAST/ANYCAST
401 { action => 'PVEFW-DropBroadcast' },
402 # ACCEPT critical ICMP types
403 { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' },
404 { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' },
405 # Drop packets with INVALID state
406 "-m conntrack --ctstate INVALID -j DROP",
407 # Drop Microsoft SMB noise
408 { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 },
409 { action => 'DROP', proto => 'udp', dport => '137:139'},
410 { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 },
411 { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 },
412 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
413 # Drop new/NotSyn traffic so that it doesn't get logged
414 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
415 # Drop DNS replies
416 { action => 'DROP', proto => 'udp', sport => 53 },
417 ],
418 'PVEFW-Reject' => [
419 # same as shorewall 'Reject', which is equal to Reject,
420 # but REJECT/DROP some packages to reduce logging,
421 # and ACCEPT critical ICMP types
422 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
423 # we are not interested in BROADCAST/MULTICAST/ANYCAST
424 { action => 'PVEFW-DropBroadcast' },
425 # ACCEPT critical ICMP types
426 { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' },
427 { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' },
428 # Drop packets with INVALID state
429 "-m conntrack --ctstate INVALID -j DROP",
430 # Drop Microsoft SMB noise
431 { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 },
432 { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'},
433 { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 },
434 { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 },
435 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
436 # Drop new/NotSyn traffic so that it doesn't get logged
437 "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP",
438 # Drop DNS replies
439 { action => 'DROP', proto => 'udp', sport => 53 },
440 ],
441 'PVEFW-tcpflags' => [
442 # same as shorewall tcpflags action.
443 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
444 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags",
445 "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags",
446 "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags",
447 "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags",
448 "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags",
449 ],
450 'PVEFW-smurfs' => [
451 # same as shorewall smurfs action
452 # Filter packets for smurfs (packets with a broadcast address as the source).
453 "-s 0.0.0.0/32 -j RETURN",
454 "-m addrtype --src-type BROADCAST -g PVEFW-smurflog",
455 "-s 224.0.0.0/4 -g PVEFW-smurflog",
456 ],
457 };
458
459 # iptables -p icmp -h
460 my $icmp_type_names = {
461 any => 1,
462 'echo-reply' => 1,
463 'destination-unreachable' => 1,
464 'network-unreachable' => 1,
465 'host-unreachable' => 1,
466 'protocol-unreachable' => 1,
467 'port-unreachable' => 1,
468 'fragmentation-needed' => 1,
469 'source-route-failed' => 1,
470 'network-unknown' => 1,
471 'host-unknown' => 1,
472 'network-prohibited' => 1,
473 'host-prohibited' => 1,
474 'TOS-network-unreachable' => 1,
475 'TOS-host-unreachable' => 1,
476 'communication-prohibited' => 1,
477 'host-precedence-violation' => 1,
478 'precedence-cutoff' => 1,
479 'source-quench' => 1,
480 'redirect' => 1,
481 'network-redirect' => 1,
482 'host-redirect' => 1,
483 'TOS-network-redirect' => 1,
484 'TOS-host-redirect' => 1,
485 'echo-request' => 1,
486 'router-advertisement' => 1,
487 'router-solicitation' => 1,
488 'time-exceeded' => 1,
489 'ttl-zero-during-transit' => 1,
490 'ttl-zero-during-reassembly' => 1,
491 'parameter-problem' => 1,
492 'ip-header-bad' => 1,
493 'required-option-missing' => 1,
494 'timestamp-request' => 1,
495 'timestamp-reply' => 1,
496 'address-mask-request' => 1,
497 'address-mask-reply' => 1,
498 };
499
500 sub get_firewall_macros {
501
502 return $pve_fw_parsed_macros if $pve_fw_parsed_macros;
503
504 $pve_fw_parsed_macros = {};
505
506 foreach my $k (keys %$pve_fw_macros) {
507 my $name = lc($k);
508
509 my $macro = $pve_fw_macros->{$k};
510 $pve_fw_preferred_macro_names->{$name} = $k;
511 $pve_fw_parsed_macros->{$name} = $macro;
512 }
513
514 return $pve_fw_parsed_macros;
515 }
516
517 my $etc_services;
518
519 sub get_etc_services {
520
521 return $etc_services if $etc_services;
522
523 my $filename = "/etc/services";
524
525 my $fh = IO::File->new($filename, O_RDONLY);
526 if (!$fh) {
527 warn "unable to read '$filename' - $!\n";
528 return {};
529 }
530
531 my $services = {};
532
533 while (my $line = <$fh>) {
534 chomp ($line);
535 next if $line =~m/^#/;
536 next if ($line =~m/^\s*$/);
537
538 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
539 $services->{byid}->{$2}->{name} = $1;
540 $services->{byid}->{$2}->{port} = $2;
541 $services->{byid}->{$2}->{$3} = 1;
542 $services->{byname}->{$1} = $services->{byid}->{$2};
543 }
544 }
545
546 close($fh);
547
548 $etc_services = $services;
549
550
551 return $etc_services;
552 }
553
554 my $etc_protocols;
555
556 sub get_etc_protocols {
557 return $etc_protocols if $etc_protocols;
558
559 my $filename = "/etc/protocols";
560
561 my $fh = IO::File->new($filename, O_RDONLY);
562 if (!$fh) {
563 warn "unable to read '$filename' - $!\n";
564 return {};
565 }
566
567 my $protocols = {};
568
569 while (my $line = <$fh>) {
570 chomp ($line);
571 next if $line =~m/^#/;
572 next if ($line =~m/^\s*$/);
573
574 if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
575 $protocols->{byid}->{$2}->{name} = $1;
576 $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
577 }
578 }
579
580 close($fh);
581
582 $etc_protocols = $protocols;
583
584 return $etc_protocols;
585 }
586
587 sub parse_address_list {
588 my ($str) = @_;
589
590 my $nbaor = 0;
591 foreach my $aor (split(/,/, $str)) {
592 if (!Net::IP->new($aor)) {
593 my $err = Net::IP::Error();
594 die "invalid IP address: $err\n";
595 }else{
596 $nbaor++;
597 }
598 }
599 return $nbaor;
600 }
601
602 sub parse_port_name_number_or_range {
603 my ($str) = @_;
604
605 my $services = PVE::Firewall::get_etc_services();
606 my $nbports = 0;
607 foreach my $item (split(/,/, $str)) {
608 my $portlist = "";
609 my $oldpon = undef;
610 $nbports++;
611 foreach my $pon (split(':', $item, 2)) {
612 $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port};
613 if ($pon =~ m/^\d+$/){
614 die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535;
615 die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon);
616 $oldpon = $pon;
617 }else{
618 die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon};
619 }
620 }
621 }
622
623 return ($nbports);
624 }
625
626 my $bridge_firewall_enabled = 0;
627
628 sub enable_bridge_firewall {
629
630 return if $bridge_firewall_enabled; # only once
631
632 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
633 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
634
635 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
636 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1");
637
638 $bridge_firewall_enabled = 1;
639 }
640
641 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
642
643 sub iptables {
644 my ($cmd) = @_;
645
646 run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {});
647 }
648
649 sub iptables_restore_cmdlist {
650 my ($cmdlist) = @_;
651
652 run_command("/sbin/iptables-restore -n", input => $cmdlist);
653 }
654
655 sub iptables_get_chains {
656
657 my $res = {};
658
659 # check what chains we want to track
660 my $is_pvefw_chain = sub {
661 my $name = shift;
662
663 return 1 if $name =~ m/^PVEFW-\S+$/;
664
665 return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/;
666
667 return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable
668
669 return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/;
670 return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/;
671
672 return undef;
673 };
674
675 my $table = '';
676
677 my $parser = sub {
678 my $line = shift;
679
680 return if $line =~ m/^#/;
681 return if $line =~ m/^\s*$/;
682
683 if ($line =~ m/^\*(\S+)$/) {
684 $table = $1;
685 return;
686 }
687
688 return if $table ne 'filter';
689
690 if ($line =~ m/^:(\S+)\s/) {
691 my $chain = $1;
692 return if !&$is_pvefw_chain($chain);
693 $res->{$chain} = "unknown";
694 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
695 my ($chain, $sig) = ($1, $2);
696 return if !&$is_pvefw_chain($chain);
697 $res->{$chain} = $sig;
698 } else {
699 # simply ignore the rest
700 return;
701 }
702 };
703
704 run_command("/sbin/iptables-save", outfunc => $parser);
705
706 return $res;
707 }
708
709 sub iptables_chain_exist {
710 my ($chain) = @_;
711
712 eval{
713 iptables("-n --list $chain");
714 };
715 return undef if $@;
716
717 return 1;
718 }
719
720 sub iptables_rule_exist {
721 my ($rule) = @_;
722
723 eval{
724 iptables("-C $rule");
725 };
726 return undef if $@;
727
728 return 1;
729 }
730
731 sub ruleset_generate_rule {
732 my ($ruleset, $chain, $rule, $actions, $goto) = @_;
733
734 return if $rule->{disable};
735
736 my @cmd = ();
737
738 push @cmd, "-m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1;
739 push @cmd, "-s $rule->{source}" if $rule->{source};
740 push @cmd, "-m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1;
741 push @cmd, "-d $rule->{dest}" if $rule->{dest};
742
743 if ($rule->{proto}) {
744 push @cmd, "-p $rule->{proto}";
745
746 my $multiport = 0;
747 $multiport++ if $rule->{nbdport} && ($rule->{nbdport} > 1);
748 $multiport++ if $rule->{nbsport} && ($rule->{nbsport} > 1);
749
750 push @cmd, "--match multiport" if $multiport;
751
752 die "multiport: option '--sports' cannot be used together with '--dports'\n"
753 if ($multiport == 2) && ($rule->{dport} ne $rule->{sport});
754
755 if ($rule->{dport}) {
756 if ($rule->{proto} && $rule->{proto} eq 'icmp') {
757 # Note: we use dport to store --icmp-type
758 die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}});
759 push @cmd, "-m icmp --icmp-type $rule->{dport}";
760 } else {
761 if ($rule->{nbdport} && $rule->{nbdport} > 1) {
762 if ($multiport == 2) {
763 push @cmd, "--ports $rule->{dport}";
764 } else {
765 push @cmd, "--dports $rule->{dport}";
766 }
767 } else {
768 push @cmd, "--dport $rule->{dport}";
769 }
770 }
771 }
772
773 if ($rule->{sport}) {
774 if ($rule->{nbsport} && $rule->{nbsport} > 1) {
775 push @cmd, "--sports $rule->{sport}" if $multiport != 2;
776 } else {
777 push @cmd, "--sport $rule->{sport}";
778 }
779 }
780 } elsif ($rule->{dport} || $rule->{sport}) {
781 warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport};
782 warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport};
783 }
784
785 push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype};
786
787 if (my $action = $rule->{action}) {
788 $action = $actions->{$action} if defined($actions->{$action});
789 $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK';
790 push @cmd, $goto ? "-g $action" : "-j $action";
791 }
792
793 if (scalar(@cmd)) {
794 my $cmdstr = join(' ', @cmd);
795 ruleset_addrule($ruleset, $chain, $cmdstr);
796 }
797 }
798
799 sub ruleset_create_chain {
800 my ($ruleset, $chain) = @_;
801
802 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
803
804 die "chain '$chain' already exists\n" if $ruleset->{$chain};
805
806 $ruleset->{$chain} = [];
807 }
808
809 sub ruleset_chain_exist {
810 my ($ruleset, $chain) = @_;
811
812 return $ruleset->{$chain} ? 1 : undef;
813 }
814
815 sub ruleset_addrule {
816 my ($ruleset, $chain, $rule) = @_;
817
818 die "no such chain '$chain'\n" if !$ruleset->{$chain};
819
820 push @{$ruleset->{$chain}}, "-A $chain $rule";
821 }
822
823 sub ruleset_insertrule {
824 my ($ruleset, $chain, $rule) = @_;
825
826 die "no such chain '$chain'\n" if !$ruleset->{$chain};
827
828 unshift @{$ruleset->{$chain}}, "-A $chain $rule";
829 }
830
831 sub generate_bridge_chains {
832 my ($ruleset, $hostfw_conf, $bridge) = @_;
833
834 my $options = $hostfw_conf->{options} || {};
835
836 # fixme: what log level should we use here?
837 my $loglevel = get_option_log_level($options, "log_level_out");
838
839 if (!ruleset_chain_exist($ruleset, "$bridge-FW")) {
840 ruleset_create_chain($ruleset, "$bridge-FW");
841 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
842 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW");
843 # disable interbridge routing
844 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j PVEFW-Drop");
845 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j PVEFW-Drop");
846 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j LOG --log-prefix \"PVEFW-FORWARD-dropped \" --log-level $loglevel");
847 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j LOG --log-prefix \"PVEFW-FORWARD-dropped \" --log-level $loglevel");
848 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP");
849 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP");
850 }
851
852 if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) {
853 ruleset_create_chain($ruleset, "$bridge-OUT");
854 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
855 ruleset_addrule($ruleset, "PVEFW-INPUT", "-i $bridge -m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT");
856 }
857
858 if (!ruleset_chain_exist($ruleset, "$bridge-IN")) {
859 ruleset_create_chain($ruleset, "$bridge-IN");
860 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN");
861 ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT");
862 # accept traffic to unmanaged bridge ports
863 ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j ACCEPT ");
864 }
865 }
866
867 sub ruleset_add_chain_policy {
868 my ($ruleset, $chain, $policy, $loglevel, $accept_action) = @_;
869
870 if ($policy eq 'ACCEPT') {
871
872 ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' },
873 { ACCEPT => $accept_action});
874
875 } elsif ($policy eq 'DROP') {
876
877 ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop");
878
879 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"$chain-dropped: \" --log-level $loglevel")
880 if defined($loglevel);
881
882 ruleset_addrule($ruleset, $chain, "-j DROP");
883 } elsif ($policy eq 'REJECT') {
884 ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject");
885
886 ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"$chain-reject: \" --log-level $loglevel")
887 if defined($loglevel);
888
889 ruleset_addrule($ruleset, $chain, "-g PVEFW-reject");
890 } else {
891 # should not happen
892 die "internal error: unknown policy '$policy'";
893 }
894 }
895
896 sub generate_tap_rules_direction {
897 my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_;
898
899 my $lc_direction = lc($direction);
900
901 my $rules = $vmfw_conf->{rules};
902
903 my $options = $vmfw_conf->{options};
904 my $loglevel = get_option_log_level($options, "log_level_${lc_direction}");
905
906 my $tapchain = "$iface-$direction";
907
908 ruleset_create_chain($ruleset, $tapchain);
909
910 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
911 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs");
912 }
913
914 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
915 ruleset_addrule($ruleset, $tapchain, "-p udp -m udp --dport 67:68 -j ACCEPT");
916 }
917
918 if ($options->{tcpflags}) {
919 ruleset_addrule($ruleset, $tapchain, "-p tcp -j PVEFW-tcpflags");
920 }
921
922 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP");
923 ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
924
925 if ($direction eq 'OUT') {
926 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
927 ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP");
928 }
929 ruleset_addrule($ruleset, $tapchain, "-j MARK --set-mark 0"); # clear mark
930 }
931
932 foreach my $rule (@$rules) {
933 next if $rule->{iface} && $rule->{iface} ne $netid;
934 next if $rule->{disable};
935 if ($rule->{type} eq 'group') {
936 my $group_chain = "GROUP-$rule->{action}-$direction";
937 if(!ruleset_chain_exist($ruleset, $group_chain)){
938 generate_group_rules($ruleset, $groups_conf, $rule->{action});
939 }
940 ruleset_addrule($ruleset, $tapchain, "-j $group_chain");
941 ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -j RETURN")
942 if $direction eq 'OUT';
943 } else {
944 next if $rule->{type} ne $lc_direction;
945 if ($direction eq 'OUT') {
946 ruleset_generate_rule($ruleset, $tapchain, $rule,
947 { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" });
948 } else {
949 ruleset_generate_rule($ruleset, $tapchain, $rule, { REJECT => "PVEFW-reject" });
950 }
951 }
952 }
953
954 # implement policy
955 my $policy;
956
957 if ($direction eq 'OUT') {
958 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
959 } else {
960 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
961 }
962
963 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : "ACCEPT";
964 ruleset_add_chain_policy($ruleset, $tapchain, $policy, $loglevel, $accept_action);
965
966 # plug the tap chain to bridge chain
967 my $physdevdirection = $direction eq 'IN' ? "out" : "in";
968 my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain";
969 ruleset_insertrule($ruleset, "$bridge-$direction", $rule);
970 }
971
972 sub enable_host_firewall {
973 my ($ruleset, $hostfw_conf, $groups_conf) = @_;
974
975 # fixme: allow security groups
976
977 my $options = $hostfw_conf->{options};
978 my $rules = $hostfw_conf->{rules};
979
980 # host inbound firewall
981 my $chain = "PVEFW-HOST-IN";
982 ruleset_create_chain($ruleset, $chain);
983
984 my $loglevel = get_option_log_level($options, "log_level_in");
985
986 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
987 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
988 ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT");
989 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
990 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
991 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
992
993 # we use RETURN because we need to check also tap rules
994 my $accept_action = 'RETURN';
995
996 foreach my $rule (@$rules) {
997 next if $rule->{type} ne 'in';
998 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" });
999 }
1000
1001 # implement input policy
1002 my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
1003 ruleset_add_chain_policy($ruleset, $chain, $policy, $loglevel, $accept_action);
1004
1005 # host outbound firewall
1006 $chain = "PVEFW-HOST-OUT";
1007 ruleset_create_chain($ruleset, $chain);
1008
1009 $loglevel = get_option_log_level($options, "log_level_out");
1010
1011 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP");
1012 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1013 ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT");
1014 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT");
1015 ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT");
1016 ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync
1017
1018 # we use RETURN because we may want to check other thigs later
1019 $accept_action = 'RETURN';
1020
1021 foreach my $rule (@$rules) {
1022 next if $rule->{type} ne 'out';
1023 ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" });
1024 }
1025
1026 # implement output policy
1027 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
1028 ruleset_add_chain_policy($ruleset, $chain, $policy, $loglevel, $accept_action);
1029
1030 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT");
1031 ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN");
1032 }
1033
1034 sub generate_group_rules {
1035 my ($ruleset, $groups_conf, $group) = @_;
1036
1037 die "no such security group '$group'\n" if !$groups_conf->{$group};
1038
1039 my $rules = $groups_conf->{$group}->{rules};
1040
1041 my $chain = "GROUP-${group}-IN";
1042
1043 ruleset_create_chain($ruleset, $chain);
1044
1045 foreach my $rule (@$rules) {
1046 next if $rule->{type} ne 'in';
1047 ruleset_generate_rule($ruleset, $chain, $rule, { REJECT => "PVEFW-reject" });
1048 }
1049
1050 $chain = "GROUP-${group}-OUT";
1051
1052 ruleset_create_chain($ruleset, $chain);
1053 ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark
1054
1055 foreach my $rule (@$rules) {
1056 next if $rule->{type} ne 'out';
1057 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
1058 # check also other tap rules later
1059 ruleset_generate_rule($ruleset, $chain, $rule,
1060 { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" });
1061 }
1062 }
1063
1064 my $MAX_NETS = 32;
1065 my $valid_netdev_names = {};
1066 for (my $i = 0; $i < $MAX_NETS; $i++) {
1067 $valid_netdev_names->{"net$i"} = 1;
1068 }
1069
1070 sub parse_fw_rule {
1071 my ($line, $need_iface, $allow_groups) = @_;
1072
1073 my $macros = get_firewall_macros();
1074 my $protocols = get_etc_protocols();
1075
1076 my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
1077
1078 # we can add single line comments to the end of the rule
1079 my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//;
1080
1081 # we can disable a rule when prefixed with '|'
1082 my $disable = 1 if $line =~ s/^\|//;
1083
1084 my @data = split(/\s+/, $line);
1085 my $expected_elements = $need_iface ? 8 : 7;
1086
1087 die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
1088
1089 if ($need_iface) {
1090 ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
1091 } else {
1092 ($type, $action, $source, $dest, $proto, $dport, $sport) = @data;
1093 }
1094
1095 die "incomplete rule\n" if ! ($type && $action);
1096
1097 my $macro;
1098 my $macro_name;
1099
1100 $type = lc($type);
1101
1102 if ($type eq 'in' || $type eq 'out') {
1103 if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
1104 # OK
1105 } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
1106 ($macro_name, $action) = ($1, $2);
1107 my $lc_macro_name = lc($macro_name);
1108 my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name};
1109 $macro_name = $preferred_name if $preferred_name;
1110 $macro = $macros->{$lc_macro_name};
1111 die "unknown macro '$macro_name'\n" if !$macro;
1112 } else {
1113 die "unknown action '$action'\n";
1114 }
1115 } elsif ($type eq 'group') {
1116 die "wrong number of rule elements\n" if scalar(@data) != 3;
1117 die "groups disabled\n" if !$allow_groups;
1118
1119 die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/;
1120 } else {
1121 die "unknown rule type '$type'\n";
1122 }
1123
1124 if ($need_iface) {
1125 $iface = undef if $iface && $iface eq '-';
1126 die "unknown interface '$iface'\n"
1127 if defined($iface) && !$valid_netdev_names->{$iface};
1128 }
1129
1130 $proto = undef if $proto && $proto eq '-';
1131 die "unknown protokol '$proto'\n" if $proto &&
1132 !(defined($protocols->{byname}->{$proto}) ||
1133 defined($protocols->{byid}->{$proto}));
1134
1135 $source = undef if $source && $source eq '-';
1136 $dest = undef if $dest && $dest eq '-';
1137
1138 $dport = undef if $dport && $dport eq '-';
1139 $sport = undef if $sport && $sport eq '-';
1140
1141 my $nbsource = undef;
1142 my $nbdest = undef;
1143
1144 $nbsource = parse_address_list($source) if $source;
1145 $nbdest = parse_address_list($dest) if $dest;
1146
1147 my $rules = [];
1148
1149 my $param = {
1150 type => $type,
1151 disable => $disable,
1152 comment => $comment,
1153 action => $action,
1154 iface => $iface,
1155 source => $source,
1156 dest => $dest,
1157 nbsource => $nbsource,
1158 nbdest => $nbdest,
1159 proto => $proto,
1160 dport => $dport,
1161 sport => $sport,
1162 };
1163
1164 if ($macro) {
1165 foreach my $templ (@$macro) {
1166 my $rule = {};
1167 my $param_used = {};
1168 foreach my $k (keys %$templ) {
1169 my $v = $templ->{$k};
1170 if ($v eq 'PARAM') {
1171 $v = $param->{$k};
1172 $param_used->{$k} = 1;
1173 } elsif ($v eq 'DEST') {
1174 $v = $param->{dest};
1175 $param_used->{dest} = 1;
1176 } elsif ($v eq 'SOURCE') {
1177 $v = $param->{source};
1178 $param_used->{source} = 1;
1179 }
1180
1181 die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v);
1182 $rule->{$k} = $v;
1183 }
1184 foreach my $k (keys %$param) {
1185 next if !defined($param->{$k});
1186 next if $param_used->{$k};
1187 if (defined($rule->{$k})) {
1188 die "parameter '$k' already define in macro (value = '$rule->{$k}')\n"
1189 if $rule->{$k} ne $param->{$k};
1190 } else {
1191 $rule->{$k} = $param->{$k};
1192 }
1193 }
1194 push @$rules, $rule;
1195 }
1196 } else {
1197 push @$rules, $param;
1198 }
1199
1200 foreach my $rule (@$rules) {
1201 $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport})
1202 if defined($rule->{dport});
1203 $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport})
1204 if defined($rule->{sport});
1205 }
1206
1207 return $rules;
1208 }
1209
1210 sub parse_vmfw_option {
1211 my ($line) = @_;
1212
1213 my ($opt, $value);
1214
1215 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1216
1217 if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1218 $opt = lc($1);
1219 $value = int($2);
1220 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
1221 $opt = lc($1);
1222 $value = $2 ? lc($3) : '';
1223 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1224 $opt = lc($1);
1225 $value = uc($3);
1226 } else {
1227 chomp $line;
1228 die "can't parse option '$line'\n"
1229 }
1230
1231 return ($opt, $value);
1232 }
1233
1234 sub parse_hostfw_option {
1235 my ($line) = @_;
1236
1237 my ($opt, $value);
1238
1239 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
1240
1241 if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags):\s*(0|1)\s*$/i) {
1242 $opt = lc($1);
1243 $value = int($2);
1244 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
1245 $opt = lc($1);
1246 $value = $2 ? lc($3) : '';
1247 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
1248 $opt = lc($1);
1249 $value = uc($3);
1250 } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) {
1251 $opt = lc($1);
1252 $value = int($2);
1253 } else {
1254 chomp $line;
1255 die "can't parse option '$line'\n"
1256 }
1257
1258 return ($opt, $value);
1259 }
1260
1261 sub parse_vm_fw_rules {
1262 my ($filename, $fh) = @_;
1263
1264 my $res = { rules => [], options => {}};
1265
1266 my $section;
1267
1268 while (defined(my $line = <$fh>)) {
1269 next if $line =~ m/^#/;
1270 next if $line =~ m/^\s*$/;
1271
1272 my $linenr = $fh->input_line_number();
1273 my $prefix = "$filename (line $linenr)";
1274
1275 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1276 $section = lc($1);
1277 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1278 next;
1279 }
1280 if (!$section) {
1281 warn "$prefix: skip line - no section";
1282 next;
1283 }
1284
1285 next if !$res->{$section}; # skip undefined section
1286
1287 if ($section eq 'options') {
1288 eval {
1289 my ($opt, $value) = parse_vmfw_option($line);
1290 $res->{options}->{$opt} = $value;
1291 };
1292 warn "$prefix: $@" if $@;
1293 next;
1294 }
1295
1296 my $rules;
1297 eval { $rules = parse_fw_rule($line, 1, 1); };
1298 if (my $err = $@) {
1299 warn "$prefix: $err";
1300 next;
1301 }
1302
1303 push @{$res->{$section}}, @$rules;
1304 }
1305
1306 return $res;
1307 }
1308
1309 sub parse_host_fw_rules {
1310 my ($filename, $fh) = @_;
1311
1312 my $res = { rules => [], options => {}};
1313
1314 my $section;
1315
1316 while (defined(my $line = <$fh>)) {
1317 next if $line =~ m/^#/;
1318 next if $line =~ m/^\s*$/;
1319
1320 my $linenr = $fh->input_line_number();
1321 my $prefix = "$filename (line $linenr)";
1322
1323 if ($line =~ m/^\[(\S+)\]\s*$/i) {
1324 $section = lc($1);
1325 warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
1326 next;
1327 }
1328 if (!$section) {
1329 warn "$prefix: skip line - no section";
1330 next;
1331 }
1332
1333 next if !$res->{$section}; # skip undefined section
1334
1335 if ($section eq 'options') {
1336 eval {
1337 my ($opt, $value) = parse_hostfw_option($line);
1338 $res->{options}->{$opt} = $value;
1339 };
1340 warn "$prefix: $@" if $@;
1341 next;
1342 }
1343
1344 my $rules;
1345 eval { $rules = parse_fw_rule($line, 1, 1); };
1346 if (my $err = $@) {
1347 warn "$prefix: $err";
1348 next;
1349 }
1350
1351 push @{$res->{$section}}, @$rules;
1352 }
1353
1354 return $res;
1355 }
1356
1357 sub parse_group_fw_rules {
1358 my ($filename, $fh) = @_;
1359
1360 my $section;
1361 my $group;
1362
1363 my $res = { rules => [] };
1364
1365 while (defined(my $line = <$fh>)) {
1366 next if $line =~ m/^#/;
1367 next if $line =~ m/^\s*$/;
1368
1369 my $linenr = $fh->input_line_number();
1370 my $prefix = "$filename (line $linenr)";
1371
1372 if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) {
1373 $section = 'rules';
1374 $group = lc($1);
1375 next;
1376 }
1377 if (!$section || !$group) {
1378 warn "$prefix: skip line - no section";
1379 next;
1380 }
1381
1382 my $rules;
1383 eval { $rules = parse_fw_rule($line, 0, 0); };
1384 if (my $err = $@) {
1385 warn "$prefix: $err";
1386 next;
1387 }
1388
1389 push @{$res->{$group}->{$section}}, @$rules;
1390 }
1391
1392 return $res;
1393 }
1394
1395 sub run_locked {
1396 my ($code, @param) = @_;
1397
1398 my $timeout = 10;
1399
1400 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
1401
1402 die $@ if $@;
1403
1404 return $res;
1405 }
1406
1407 sub read_local_vm_config {
1408
1409 my $openvz = {};
1410 my $qemu = {};
1411
1412 my $vmdata = { openvz => $openvz, qemu => $qemu };
1413
1414 my $vmlist = PVE::Cluster::get_vmlist();
1415 return $vmdata if !$vmlist || !$vmlist->{ids};
1416 my $ids = $vmlist->{ids};
1417
1418 foreach my $vmid (keys %$ids) {
1419 next if !$vmid; # skip VE0
1420 my $d = $ids->{$vmid};
1421 next if !$d->{node} || $d->{node} ne $nodename;
1422 next if !$d->{type};
1423 if ($d->{type} eq 'openvz') {
1424 my $cfspath = PVE::OpenVZ::cfs_config_path($vmid);
1425 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1426 $openvz->{$vmid} = $conf;
1427 }
1428 } elsif ($d->{type} eq 'qemu') {
1429 my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
1430 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
1431 $qemu->{$vmid} = $conf;
1432 }
1433 }
1434 }
1435
1436 return $vmdata;
1437 };
1438
1439 sub read_vm_firewall_configs {
1440 my ($vmdata) = @_;
1441 my $vmfw_configs = {};
1442
1443 foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
1444 my $filename = "/etc/pve/firewall/$vmid.fw";
1445 my $fh = IO::File->new($filename, O_RDONLY);
1446 next if !$fh;
1447
1448 $vmfw_configs->{$vmid} = parse_vm_fw_rules($filename, $fh);
1449 }
1450
1451 return $vmfw_configs;
1452 }
1453
1454 sub get_option_log_level {
1455 my ($options, $k) = @_;
1456
1457 my $v = $options->{$k};
1458 $v = $default_log_level if !defined($v);
1459
1460 return undef if $v eq '' || $v eq 'nolog';
1461
1462 $v = $log_level_hash->{$v} if defined($log_level_hash->{$v});
1463
1464 return $v if ($v >= 0) && ($v <= 7);
1465
1466 warn "unknown log level ($k = '$v')\n";
1467
1468 return undef;
1469 }
1470
1471 sub generate_std_chains {
1472 my ($ruleset, $options) = @_;
1473
1474 my $loglevel = get_option_log_level($options, 'smurf_log_level');
1475
1476 # same as shorewall smurflog.
1477 if (defined($loglevel)) {
1478 $pve_std_chains-> {'PVEFW-smurflog'} = [
1479 "-j LOG --log-prefix \"smurfs-dropped: \" --log-level $loglevel",
1480 "-j DROP",
1481 ];
1482 } else {
1483 $pve_std_chains-> {'PVEFW-smurflog'} = [ "-j DROP" ];
1484 }
1485
1486 # same as shorewall logflags action.
1487 $loglevel = get_option_log_level($options, 'tcp_flags_log_level');
1488 if (defined($loglevel)) {
1489 $pve_std_chains-> {'PVEFW-logflags'} = [
1490 "-j LOG --log-prefix \"logflags-dropped: \" --log-level $loglevel --log-ip-options",
1491 "-j DROP",
1492 ];
1493 } else {
1494 $pve_std_chains-> {'PVEFW-logflags'} = [ "-j DROP" ];
1495 }
1496
1497 foreach my $chain (keys %$pve_std_chains) {
1498 ruleset_create_chain($ruleset, $chain);
1499 foreach my $rule (@{$pve_std_chains->{$chain}}) {
1500 if (ref($rule)) {
1501 ruleset_generate_rule($ruleset, $chain, $rule);
1502 } else {
1503 ruleset_addrule($ruleset, $chain, $rule);
1504 }
1505 }
1506 }
1507 }
1508
1509 sub save_pvefw_status {
1510 my ($status) = @_;
1511
1512 die "unknown status '$status' - internal error"
1513 if $status !~ m/^(stopped|active)$/;
1514
1515 mkdir dirname($pve_fw_status_filename);
1516 PVE::Tools::file_set_contents($pve_fw_status_filename, $status);
1517 }
1518
1519 sub read_pvefw_status {
1520
1521 my $status = 'unknown';
1522
1523 return 'stopped' if ! -f $pve_fw_status_filename;
1524
1525 eval {
1526 $status = PVE::Tools::file_get_contents($pve_fw_status_filename);
1527 };
1528 warn $@ if $@;
1529
1530 return $status;
1531 }
1532
1533 sub compile {
1534 my $vmdata = read_local_vm_config();
1535 my $vmfw_configs = read_vm_firewall_configs($vmdata);
1536
1537 my $groups_conf = {};
1538 my $filename = "/etc/pve/firewall/groups.fw";
1539 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1540 $groups_conf = parse_group_fw_rules($filename, $fh);
1541 }
1542
1543 my $ruleset = {};
1544
1545 ruleset_create_chain($ruleset, "PVEFW-INPUT");
1546 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
1547
1548 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
1549 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
1550
1551 my $hostfw_options = {};
1552 my $hostfw_conf = {};
1553
1554 $filename = "/etc/pve/local/host.fw";
1555 if (my $fh = IO::File->new($filename, O_RDONLY)) {
1556 $hostfw_conf = parse_host_fw_rules($filename, $fh);
1557 $hostfw_options = $hostfw_conf->{options};
1558 }
1559
1560 generate_std_chains($ruleset, $hostfw_options);
1561
1562 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
1563
1564 enable_host_firewall($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable;
1565
1566 # generate firewall rules for QEMU VMs
1567 foreach my $vmid (keys %{$vmdata->{qemu}}) {
1568 my $conf = $vmdata->{qemu}->{$vmid};
1569 my $vmfw_conf = $vmfw_configs->{$vmid};
1570 next if !$vmfw_conf;
1571 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
1572
1573 foreach my $netid (keys %$conf) {
1574 next if $netid !~ m/^net(\d+)$/;
1575 my $net = PVE::QemuServer::parse_net($conf->{$netid});
1576 next if !$net;
1577 my $iface = "tap${vmid}i$1";
1578
1579 my $bridge = $net->{bridge};
1580 next if !$bridge; # fixme: ?
1581
1582 $bridge .= "v$net->{tag}" if $net->{tag};
1583
1584 generate_bridge_chains($ruleset, $hostfw_conf, $bridge);
1585
1586 my $macaddr = $net->{macaddr};
1587 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1588 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1589 }
1590 }
1591
1592 # generate firewall rules for OpenVZ containers
1593 foreach my $vmid (keys %{$vmdata->{openvz}}) {
1594 my $conf = $vmdata->{openvz}->{$vmid};
1595
1596 my $vmfw_conf = $vmfw_configs->{$vmid};
1597 next if !$vmfw_conf;
1598 next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0);
1599
1600 if ($conf->{ip_address} && $conf->{ip_address}->{value}) {
1601 my $ip = $conf->{ip_address}->{value};
1602 die "implement me";
1603 }
1604
1605 if ($conf->{netif} && $conf->{netif}->{value}) {
1606 my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value});
1607 print Dumper($netif);
1608 foreach my $netid (keys %$netif) {
1609 my $d = $netif->{$netid};
1610 my $bridge = $d->{bridge};
1611 if (!$bridge) {
1612 warn "no bridge device for CT $vmid iface '$netid'\n";
1613 next; # fixme?
1614 }
1615 my $macaddr = $d->{host_mac};
1616 my $iface = $d->{host_ifname};
1617 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN');
1618 generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT');
1619 }
1620 }
1621 }
1622
1623 return wantarray ? ($ruleset, $hostfw_conf) : $ruleset;
1624 }
1625
1626 sub get_ruleset_status {
1627 my ($ruleset, $verbose) = @_;
1628
1629 my $active_chains = iptables_get_chains();
1630
1631 my $statushash = {};
1632
1633 foreach my $chain (sort keys %$ruleset) {
1634 my $digest = Digest::SHA->new('sha1');
1635 foreach my $cmd (@{$ruleset->{$chain}}) {
1636 $digest->add("$cmd\n");
1637 }
1638 my $sig = $digest->b64digest;
1639 $statushash->{$chain}->{sig} = $sig;
1640
1641 my $oldsig = $active_chains->{$chain};
1642 if (!defined($oldsig)) {
1643 $statushash->{$chain}->{action} = 'create';
1644 } else {
1645 if ($oldsig eq $sig) {
1646 $statushash->{$chain}->{action} = 'exists';
1647 } else {
1648 $statushash->{$chain}->{action} = 'update';
1649 }
1650 }
1651 print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose;
1652 foreach my $cmd (@{$ruleset->{$chain}}) {
1653 print "\t$cmd\n" if $verbose;
1654 }
1655 }
1656
1657 foreach my $chain (sort keys %$active_chains) {
1658 if (!defined($ruleset->{$chain})) {
1659 my $sig = $active_chains->{$chain};
1660 $statushash->{$chain}->{action} = 'delete';
1661 $statushash->{$chain}->{sig} = $sig;
1662 print "delete $chain ($sig)\n" if $verbose;
1663 }
1664 }
1665
1666 return $statushash;
1667 }
1668
1669 sub print_ruleset {
1670 my ($ruleset) = @_;
1671
1672 get_ruleset_status($ruleset, 1);
1673 }
1674
1675 sub print_sig_rule {
1676 my ($chain, $sig) = @_;
1677
1678 # We just use this to store a SHA1 checksum used to detect changes
1679 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
1680 }
1681
1682 sub get_rulset_cmdlist {
1683 my ($ruleset, $verbose) = @_;
1684
1685 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
1686
1687 my $statushash = get_ruleset_status($ruleset, $verbose);
1688
1689 # create missing chains first
1690 foreach my $chain (sort keys %$ruleset) {
1691 my $stat = $statushash->{$chain};
1692 die "internal error" if !$stat;
1693 next if $stat->{action} ne 'create';
1694
1695 $cmdlist .= ":$chain - [0:0]\n";
1696 }
1697
1698 my $rule = "INPUT -j PVEFW-INPUT";
1699 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1700 $cmdlist .= "-A $rule\n";
1701 }
1702 $rule = "OUTPUT -j PVEFW-OUTPUT";
1703 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1704 $cmdlist .= "-A $rule\n";
1705 }
1706
1707 $rule = "FORWARD -j PVEFW-FORWARD";
1708 if (!PVE::Firewall::iptables_rule_exist($rule)) {
1709 $cmdlist .= "-A $rule\n";
1710 }
1711
1712 foreach my $chain (sort keys %$ruleset) {
1713 my $stat = $statushash->{$chain};
1714 die "internal error" if !$stat;
1715
1716 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
1717 $cmdlist .= "-F $chain\n";
1718 foreach my $cmd (@{$ruleset->{$chain}}) {
1719 $cmdlist .= "$cmd\n";
1720 }
1721 $cmdlist .= print_sig_rule($chain, $stat->{sig});
1722 } elsif ($stat->{action} eq 'delete') {
1723 die "internal error"; # this should not happen
1724 } elsif ($stat->{action} eq 'exists') {
1725 # do nothing
1726 } else {
1727 die "internal error - unknown status '$stat->{action}'";
1728 }
1729 }
1730
1731 foreach my $chain (keys %$statushash) {
1732 next if $statushash->{$chain}->{action} ne 'delete';
1733 $cmdlist .= "-F $chain\n";
1734 }
1735 foreach my $chain (keys %$statushash) {
1736 next if $statushash->{$chain}->{action} ne 'delete';
1737 next if $chain eq 'PVEFW-INPUT';
1738 next if $chain eq 'PVEFW-OUTPUT';
1739 next if $chain eq 'PVEFW-FORWARD';
1740 $cmdlist .= "-X $chain\n";
1741 }
1742
1743 $cmdlist .= "COMMIT\n";
1744
1745 return $cmdlist;
1746 }
1747
1748 sub apply_ruleset {
1749 my ($ruleset, $verbose) = @_;
1750
1751 enable_bridge_firewall();
1752
1753 my $cmdlist = get_rulset_cmdlist($ruleset, $verbose);
1754
1755 print $cmdlist if $verbose;
1756
1757 iptables_restore_cmdlist($cmdlist);
1758
1759 # test: re-read status and check if everything is up to date
1760 my $statushash = get_ruleset_status($ruleset);
1761
1762 my $errors;
1763 foreach my $chain (sort keys %$ruleset) {
1764 my $stat = $statushash->{$chain};
1765 if ($stat->{action} ne 'exists') {
1766 warn "unable to update chain '$chain'\n";
1767 $errors = 1;
1768 }
1769 }
1770
1771 die "unable to apply firewall changes\n" if $errors;
1772 }
1773
1774 sub update_nf_conntrack_max {
1775 my ($hostfw_conf) = @_;
1776
1777 my $max = 65536; # reasonable default
1778
1779 my $options = $hostfw_conf->{options} || {};
1780
1781 if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) {
1782 $max = $options->{nf_conntrack_max};
1783 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
1784 }
1785
1786 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
1787 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
1788
1789 my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max);
1790
1791 if ($current != $max) {
1792 my $hashsize = int($max/4);
1793 PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize);
1794 PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max);
1795 }
1796 }
1797
1798 sub update {
1799 my ($start, $verbose) = @_;
1800
1801 my $code = sub {
1802 my $status = read_pvefw_status();
1803
1804 my ($ruleset, $hostfw_conf) = PVE::Firewall::compile();
1805
1806 update_nf_conntrack_max($hostfw_conf);
1807
1808 if ($start || $status eq 'active') {
1809
1810 save_pvefw_status('active') if ($status ne 'active');
1811
1812 PVE::Firewall::apply_ruleset($ruleset, $verbose);
1813 } else {
1814 print "Firewall not active (status = $status)\n" if $verbose;
1815 }
1816 };
1817
1818 run_locked($code);
1819 }
1820
1821
1822 1;