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