]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Firewall; | |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | use POSIX; | |
6 | use Data::Dumper; | |
7 | use Digest::SHA; | |
8 | use PVE::INotify; | |
9 | use PVE::Exception qw(raise raise_param_exc); | |
10 | use PVE::JSONSchema qw(register_standard_option get_standard_option); | |
11 | use PVE::Cluster; | |
12 | use PVE::ProcFSTools; | |
13 | use PVE::Tools qw($IPV4RE); | |
14 | use File::Basename; | |
15 | use File::Path; | |
16 | use IO::File; | |
17 | use Net::IP; | |
18 | use PVE::Tools qw(run_command lock_file dir_glob_foreach); | |
19 | use Encode; | |
20 | ||
21 | my $hostfw_conf_filename = "/etc/pve/local/host.fw"; | |
22 | my $clusterfw_conf_filename = "/etc/pve/firewall/cluster.fw"; | |
23 | ||
24 | # dynamically include PVE::QemuServer and PVE::OpenVZ | |
25 | # to avoid dependency problems | |
26 | my $have_qemu_server; | |
27 | eval { | |
28 | require PVE::QemuServer; | |
29 | $have_qemu_server = 1; | |
30 | }; | |
31 | ||
32 | my $have_pve_manager; | |
33 | eval { | |
34 | require PVE::OpenVZ; | |
35 | $have_pve_manager = 1; | |
36 | }; | |
37 | ||
38 | PVE::JSONSchema::register_format('IPv4orCIDR', \&pve_verify_ipv4_or_cidr); | |
39 | sub pve_verify_ipv4_or_cidr { | |
40 | my ($cidr, $noerr) = @_; | |
41 | ||
42 | if ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) { | |
43 | return $cidr if Net::IP->new($cidr); | |
44 | return undef if $noerr; | |
45 | die Net::IP::Error() . "\n"; | |
46 | } | |
47 | return undef if $noerr; | |
48 | die "value does not look like a valid IP address or CIDR network\n"; | |
49 | } | |
50 | ||
51 | PVE::JSONSchema::register_standard_option('ipset-name', { | |
52 | description => "IP set name.", | |
53 | type => 'string', | |
54 | pattern => '[A-Za-z][A-Za-z0-9\-\_]+', | |
55 | minLength => 2, | |
56 | maxLength => 20, | |
57 | }); | |
58 | ||
59 | PVE::JSONSchema::register_standard_option('pve-fw-alias', { | |
60 | description => "Alias name.", | |
61 | type => 'string', | |
62 | pattern => '[A-Za-z][A-Za-z0-9\-\_]+', | |
63 | minLength => 2, | |
64 | maxLength => 20, | |
65 | }); | |
66 | ||
67 | PVE::JSONSchema::register_standard_option('pve-fw-loglevel' => { | |
68 | description => "Log level.", | |
69 | type => 'string', | |
70 | enum => ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'nolog'], | |
71 | optional => 1, | |
72 | }); | |
73 | ||
74 | my $security_group_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+'; | |
75 | my $ip_alias_pattern = '[A-Za-z][A-Za-z0-9\-\_]+'; | |
76 | ||
77 | PVE::JSONSchema::register_standard_option('pve-security-group-name', { | |
78 | description => "Security Group name.", | |
79 | type => 'string', | |
80 | pattern => $security_group_name_pattern, | |
81 | minLength => 2, | |
82 | maxLength => 20, | |
83 | }); | |
84 | ||
85 | my $feature_ipset_nomatch = 0; | |
86 | eval { | |
87 | my (undef, undef, $release) = POSIX::uname(); | |
88 | if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) { | |
89 | my ($major, $minor) = ($1, $2); | |
90 | $feature_ipset_nomatch = 1 if ($major > 3) || | |
91 | ($major == 3 && $minor >= 7); | |
92 | } | |
93 | ||
94 | }; | |
95 | ||
96 | use Data::Dumper; | |
97 | ||
98 | my $nodename = PVE::INotify::nodename(); | |
99 | ||
100 | my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; | |
101 | ||
102 | my $default_log_level = 'nolog'; # avoid logs by default | |
103 | ||
104 | my $log_level_hash = { | |
105 | debug => 7, | |
106 | info => 6, | |
107 | notice => 5, | |
108 | warning => 4, | |
109 | err => 3, | |
110 | crit => 2, | |
111 | alert => 1, | |
112 | emerg => 0, | |
113 | }; | |
114 | ||
115 | # imported/converted from: /usr/share/shorewall/macro.* | |
116 | my $pve_fw_macros = { | |
117 | 'Amanda' => [ | |
118 | "Amanda Backup", | |
119 | { action => 'PARAM', proto => 'udp', dport => '10080' }, | |
120 | { action => 'PARAM', proto => 'tcp', dport => '10080' }, | |
121 | ], | |
122 | 'Auth' => [ | |
123 | "Auth (identd) traffic", | |
124 | { action => 'PARAM', proto => 'tcp', dport => '113' }, | |
125 | ], | |
126 | 'BGP' => [ | |
127 | "Border Gateway Protocol traffic", | |
128 | { action => 'PARAM', proto => 'tcp', dport => '179' }, | |
129 | ], | |
130 | 'BitTorrent' => [ | |
131 | "BitTorrent traffic for BitTorrent 3.1 and earlier", | |
132 | { action => 'PARAM', proto => 'tcp', dport => '6881:6889' }, | |
133 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
134 | ], | |
135 | 'BitTorrent32' => [ | |
136 | "BitTorrent traffic for BitTorrent 3.2 and later", | |
137 | { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, | |
138 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
139 | ], | |
140 | 'CVS' => [ | |
141 | "Concurrent Versions System pserver traffic", | |
142 | { action => 'PARAM', proto => 'tcp', dport => '2401' }, | |
143 | ], | |
144 | 'Citrix' => [ | |
145 | "Citrix/ICA traffic (ICA, ICA Browser, CGP)", | |
146 | { action => 'PARAM', proto => 'tcp', dport => '1494' }, | |
147 | { action => 'PARAM', proto => 'udp', dport => '1604' }, | |
148 | { action => 'PARAM', proto => 'tcp', dport => '2598' }, | |
149 | ], | |
150 | 'DAAP' => [ | |
151 | "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)", | |
152 | { action => 'PARAM', proto => 'tcp', dport => '3689' }, | |
153 | { action => 'PARAM', proto => 'udp', dport => '3689' }, | |
154 | ], | |
155 | 'DCC' => [ | |
156 | "Distributed Checksum Clearinghouse spam filtering mechanism", | |
157 | { action => 'PARAM', proto => 'tcp', dport => '6277' }, | |
158 | ], | |
159 | 'DHCPfwd' => [ | |
160 | "Forwarded DHCP traffic", | |
161 | { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
162 | ], | |
163 | 'DNS' => [ | |
164 | "Domain Name System traffic (upd and tcp)", | |
165 | { action => 'PARAM', proto => 'udp', dport => '53' }, | |
166 | { action => 'PARAM', proto => 'tcp', dport => '53' }, | |
167 | ], | |
168 | 'Distcc' => [ | |
169 | "Distributed Compiler service", | |
170 | { action => 'PARAM', proto => 'tcp', dport => '3632' }, | |
171 | ], | |
172 | 'FTP' => [ | |
173 | "File Transfer Protocol", | |
174 | { action => 'PARAM', proto => 'tcp', dport => '21' }, | |
175 | ], | |
176 | 'Finger' => [ | |
177 | "Finger protocol (RFC 742)", | |
178 | { action => 'PARAM', proto => 'tcp', dport => '79' }, | |
179 | ], | |
180 | 'GNUnet' => [ | |
181 | "GNUnet secure peer-to-peer networking traffic", | |
182 | { action => 'PARAM', proto => 'tcp', dport => '2086' }, | |
183 | { action => 'PARAM', proto => 'udp', dport => '2086' }, | |
184 | { action => 'PARAM', proto => 'tcp', dport => '1080' }, | |
185 | { action => 'PARAM', proto => 'udp', dport => '1080' }, | |
186 | ], | |
187 | 'GRE' => [ | |
188 | "Generic Routing Encapsulation tunneling protocol", | |
189 | { action => 'PARAM', proto => '47' }, | |
190 | ], | |
191 | 'Git' => [ | |
192 | "Git distributed revision control traffic", | |
193 | { action => 'PARAM', proto => 'tcp', dport => '9418' }, | |
194 | ], | |
195 | 'HKP' => [ | |
196 | "OpenPGP HTTP keyserver protocol traffic", | |
197 | { action => 'PARAM', proto => 'tcp', dport => '11371' }, | |
198 | ], | |
199 | 'HTTP' => [ | |
200 | "Hypertext Transfer Protocol (WWW)", | |
201 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
202 | ], | |
203 | 'HTTPS' => [ | |
204 | "Hypertext Transfer Protocol (WWW) over SSL", | |
205 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
206 | ], | |
207 | 'ICPV2' => [ | |
208 | "Internet Cache Protocol V2 (Squid) traffic", | |
209 | { action => 'PARAM', proto => 'udp', dport => '3130' }, | |
210 | ], | |
211 | 'ICQ' => [ | |
212 | "AOL Instant Messenger traffic", | |
213 | { action => 'PARAM', proto => 'tcp', dport => '5190' }, | |
214 | ], | |
215 | 'IMAP' => [ | |
216 | "Internet Message Access Protocol", | |
217 | { action => 'PARAM', proto => 'tcp', dport => '143' }, | |
218 | ], | |
219 | 'IMAPS' => [ | |
220 | "Internet Message Access Protocol over SSL", | |
221 | { action => 'PARAM', proto => 'tcp', dport => '993' }, | |
222 | ], | |
223 | 'IPIP' => [ | |
224 | "IPIP capsulation traffic", | |
225 | { action => 'PARAM', proto => '94' }, | |
226 | ], | |
227 | 'IPsec' => [ | |
228 | "IPsec traffic", | |
229 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
230 | { action => 'PARAM', proto => '50' }, | |
231 | ], | |
232 | 'IPsecah' => [ | |
233 | "IPsec authentication (AH) traffic", | |
234 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
235 | { action => 'PARAM', proto => '51' }, | |
236 | ], | |
237 | 'IPsecnat' => [ | |
238 | "IPsec traffic and Nat-Traversal", | |
239 | { action => 'PARAM', proto => 'udp', dport => '500' }, | |
240 | { action => 'PARAM', proto => 'udp', dport => '4500' }, | |
241 | { action => 'PARAM', proto => '50' }, | |
242 | ], | |
243 | 'IRC' => [ | |
244 | "Internet Relay Chat traffic", | |
245 | { action => 'PARAM', proto => 'tcp', dport => '6667' }, | |
246 | ], | |
247 | 'Jetdirect' => [ | |
248 | "HP Jetdirect printing", | |
249 | { action => 'PARAM', proto => 'tcp', dport => '9100' }, | |
250 | ], | |
251 | 'L2TP' => [ | |
252 | "Layer 2 Tunneling Protocol traffic", | |
253 | { action => 'PARAM', proto => 'udp', dport => '1701' }, | |
254 | ], | |
255 | 'LDAP' => [ | |
256 | "Lightweight Directory Access Protocol traffic", | |
257 | { action => 'PARAM', proto => 'tcp', dport => '389' }, | |
258 | ], | |
259 | 'LDAPS' => [ | |
260 | "Secure Lightweight Directory Access Protocol traffic", | |
261 | { action => 'PARAM', proto => 'tcp', dport => '636' }, | |
262 | ], | |
263 | 'MSNP' => [ | |
264 | "Microsoft Notification Protocol", | |
265 | { action => 'PARAM', proto => 'tcp', dport => '1863' }, | |
266 | ], | |
267 | 'MSSQL' => [ | |
268 | "Microsoft SQL Server", | |
269 | { action => 'PARAM', proto => 'tcp', dport => '1433' }, | |
270 | ], | |
271 | 'Mail' => [ | |
272 | "Mail traffic (SMTP, SMTPS, Submission)", | |
273 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
274 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
275 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
276 | ], | |
277 | 'Munin' => [ | |
278 | "Munin networked resource monitoring traffic", | |
279 | { action => 'PARAM', proto => 'tcp', dport => '4949' }, | |
280 | ], | |
281 | 'MySQL' => [ | |
282 | "MySQL server", | |
283 | { action => 'PARAM', proto => 'tcp', dport => '3306' }, | |
284 | ], | |
285 | 'NNTP' => [ | |
286 | "NNTP traffic (Usenet).", | |
287 | { action => 'PARAM', proto => 'tcp', dport => '119' }, | |
288 | ], | |
289 | 'NNTPS' => [ | |
290 | "Encrypted NNTP traffic (Usenet)", | |
291 | { action => 'PARAM', proto => 'tcp', dport => '563' }, | |
292 | ], | |
293 | 'NTP' => [ | |
294 | "Network Time Protocol (ntpd)", | |
295 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
296 | ], | |
297 | 'OSPF' => [ | |
298 | "OSPF multicast traffic", | |
299 | { action => 'PARAM', proto => '89' }, | |
300 | ], | |
301 | 'OpenVPN' => [ | |
302 | "OpenVPN traffic", | |
303 | { action => 'PARAM', proto => 'udp', dport => '1194' }, | |
304 | ], | |
305 | 'PCA' => [ | |
306 | "Symantec PCAnywere (tm)", | |
307 | { action => 'PARAM', proto => 'udp', dport => '5632' }, | |
308 | { action => 'PARAM', proto => 'tcp', dport => '5631' }, | |
309 | ], | |
310 | 'POP3' => [ | |
311 | "POP3 traffic", | |
312 | { action => 'PARAM', proto => 'tcp', dport => '110' }, | |
313 | ], | |
314 | 'POP3S' => [ | |
315 | "Encrypted POP3 traffic", | |
316 | { action => 'PARAM', proto => 'tcp', dport => '995' }, | |
317 | ], | |
318 | 'PPtP' => [ | |
319 | "Point-to-Point Tunneling Protocol", | |
320 | { action => 'PARAM', proto => '47' }, | |
321 | { action => 'PARAM', proto => 'tcp', dport => '1723' }, | |
322 | ], | |
323 | 'Ping' => [ | |
324 | "ICMP echo request", | |
325 | { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, | |
326 | ], | |
327 | 'PostgreSQL' => [ | |
328 | "PostgreSQL server", | |
329 | { action => 'PARAM', proto => 'tcp', dport => '5432' }, | |
330 | ], | |
331 | 'Printer' => [ | |
332 | "Line Printer protocol printing", | |
333 | { action => 'PARAM', proto => 'tcp', dport => '515' }, | |
334 | ], | |
335 | 'RDP' => [ | |
336 | "Microsoft Remote Desktop Protocol traffic", | |
337 | { action => 'PARAM', proto => 'tcp', dport => '3389' }, | |
338 | ], | |
339 | 'RIP' => [ | |
340 | "Routing Information Protocol (bidirectional)", | |
341 | { action => 'PARAM', proto => 'udp', dport => '520' }, | |
342 | ], | |
343 | 'RNDC' => [ | |
344 | "BIND remote management protocol", | |
345 | { action => 'PARAM', proto => 'tcp', dport => '953' }, | |
346 | ], | |
347 | 'Razor' => [ | |
348 | "Razor Antispam System", | |
349 | { action => 'ACCEPT', proto => 'tcp', dport => '2703' }, | |
350 | ], | |
351 | 'Rdate' => [ | |
352 | "Remote time retrieval (rdate)", | |
353 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
354 | ], | |
355 | 'Rsync' => [ | |
356 | "Rsync server", | |
357 | { action => 'PARAM', proto => 'tcp', dport => '873' }, | |
358 | ], | |
359 | 'SANE' => [ | |
360 | "SANE network scanning", | |
361 | { action => 'PARAM', proto => 'tcp', dport => '6566' }, | |
362 | ], | |
363 | 'SMB' => [ | |
364 | "Microsoft SMB traffic", | |
365 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
366 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
367 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
368 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
369 | ], | |
370 | 'SMBswat' => [ | |
371 | "Samba Web Administration Tool", | |
372 | { action => 'PARAM', proto => 'tcp', dport => '901' }, | |
373 | ], | |
374 | 'SMTP' => [ | |
375 | "Simple Mail Transfer Protocol", | |
376 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
377 | ], | |
378 | 'SMTPS' => [ | |
379 | "Encrypted Simple Mail Transfer Protocol", | |
380 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
381 | ], | |
382 | 'SNMP' => [ | |
383 | "Simple Network Management Protocol", | |
384 | { action => 'PARAM', proto => 'udp', dport => '161:162' }, | |
385 | { action => 'PARAM', proto => 'tcp', dport => '161' }, | |
386 | ], | |
387 | 'SPAMD' => [ | |
388 | "Spam Assassin SPAMD traffic", | |
389 | { action => 'PARAM', proto => 'tcp', dport => '783' }, | |
390 | ], | |
391 | 'SSH' => [ | |
392 | "Secure shell traffic", | |
393 | { action => 'PARAM', proto => 'tcp', dport => '22' }, | |
394 | ], | |
395 | 'SVN' => [ | |
396 | "Subversion server (svnserve)", | |
397 | { action => 'PARAM', proto => 'tcp', dport => '3690' }, | |
398 | ], | |
399 | 'SixXS' => [ | |
400 | "SixXS IPv6 Deployment and Tunnel Broker", | |
401 | { action => 'PARAM', proto => 'tcp', dport => '3874' }, | |
402 | { action => 'PARAM', proto => 'udp', dport => '3740' }, | |
403 | { action => 'PARAM', proto => '41' }, | |
404 | { action => 'PARAM', proto => 'udp', dport => '5072,8374' }, | |
405 | ], | |
406 | 'Squid' => [ | |
407 | "Squid web proxy traffic", | |
408 | { action => 'PARAM', proto => 'tcp', dport => '3128' }, | |
409 | ], | |
410 | 'Submission' => [ | |
411 | "Mail message submission traffic", | |
412 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
413 | ], | |
414 | 'Syslog' => [ | |
415 | "Syslog protocol (RFC 5424) traffic", | |
416 | { action => 'PARAM', proto => 'udp', dport => '514' }, | |
417 | { action => 'PARAM', proto => 'tcp', dport => '514' }, | |
418 | ], | |
419 | 'TFTP' => [ | |
420 | "Trivial File Transfer Protocol traffic", | |
421 | { action => 'PARAM', proto => 'udp', dport => '69' }, | |
422 | ], | |
423 | 'Telnet' => [ | |
424 | "Telnet traffic", | |
425 | { action => 'PARAM', proto => 'tcp', dport => '23' }, | |
426 | ], | |
427 | 'Telnets' => [ | |
428 | "Telnet over SSL", | |
429 | { action => 'PARAM', proto => 'tcp', dport => '992' }, | |
430 | ], | |
431 | 'Time' => [ | |
432 | "RFC 868 Time protocol", | |
433 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
434 | ], | |
435 | 'Trcrt' => [ | |
436 | "Traceroute (for up to 30 hops) traffic", | |
437 | { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, | |
438 | { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, | |
439 | ], | |
440 | 'VNC' => [ | |
441 | "VNC traffic for VNC display's 0 - 99", | |
442 | { action => 'PARAM', proto => 'tcp', dport => '5900:5999' }, | |
443 | ], | |
444 | 'VNCL' => [ | |
445 | "VNC traffic from Vncservers to Vncviewers in listen mode", | |
446 | { action => 'PARAM', proto => 'tcp', dport => '5500' }, | |
447 | ], | |
448 | 'Web' => [ | |
449 | "WWW traffic (HTTP and HTTPS)", | |
450 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
451 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
452 | ], | |
453 | 'Webcache' => [ | |
454 | "Web Cache/Proxy traffic (port 8080)", | |
455 | { action => 'PARAM', proto => 'tcp', dport => '8080' }, | |
456 | ], | |
457 | 'Webmin' => [ | |
458 | "Webmin traffic", | |
459 | { action => 'PARAM', proto => 'tcp', dport => '10000' }, | |
460 | ], | |
461 | 'Whois' => [ | |
462 | "Whois (nicname, RFC 3912) traffic", | |
463 | { action => 'PARAM', proto => 'tcp', dport => '43' }, | |
464 | ], | |
465 | }; | |
466 | ||
467 | my $pve_fw_parsed_macros; | |
468 | my $pve_fw_macro_descr; | |
469 | my $pve_fw_preferred_macro_names = {}; | |
470 | ||
471 | my $pve_std_chains = { | |
472 | 'PVEFW-SET-ACCEPT-MARK' => [ | |
473 | "-j MARK --set-mark 1", | |
474 | ], | |
475 | 'PVEFW-DropBroadcast' => [ | |
476 | # same as shorewall 'Broadcast' | |
477 | # simply DROP BROADCAST/MULTICAST/ANYCAST | |
478 | # we can use this to reduce logging | |
479 | { action => 'DROP', dsttype => 'BROADCAST' }, | |
480 | { action => 'DROP', dsttype => 'MULTICAST' }, | |
481 | { action => 'DROP', dsttype => 'ANYCAST' }, | |
482 | { action => 'DROP', dest => '224.0.0.0/4' }, | |
483 | ], | |
484 | 'PVEFW-reject' => [ | |
485 | # same as shorewall 'reject' | |
486 | { action => 'DROP', dsttype => 'BROADCAST' }, | |
487 | { action => 'DROP', source => '224.0.0.0/4' }, | |
488 | { action => 'DROP', proto => 'icmp' }, | |
489 | "-p tcp -j REJECT --reject-with tcp-reset", | |
490 | "-p udp -j REJECT --reject-with icmp-port-unreachable", | |
491 | "-p icmp -j REJECT --reject-with icmp-host-unreachable", | |
492 | "-j REJECT --reject-with icmp-host-prohibited", | |
493 | ], | |
494 | 'PVEFW-Drop' => [ | |
495 | # same as shorewall 'Drop', which is equal to DROP, | |
496 | # but REJECT/DROP some packages to reduce logging, | |
497 | # and ACCEPT critical ICMP types | |
498 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
499 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
500 | { action => 'PVEFW-DropBroadcast' }, | |
501 | # ACCEPT critical ICMP types | |
502 | { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, | |
503 | { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, | |
504 | # Drop packets with INVALID state | |
505 | "-m conntrack --ctstate INVALID -j DROP", | |
506 | # Drop Microsoft SMB noise | |
507 | { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
508 | { action => 'DROP', proto => 'udp', dport => '137:139'}, | |
509 | { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
510 | { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
511 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
512 | # Drop new/NotSyn traffic so that it doesn't get logged | |
513 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
514 | # Drop DNS replies | |
515 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
516 | ], | |
517 | 'PVEFW-Reject' => [ | |
518 | # same as shorewall 'Reject', which is equal to Reject, | |
519 | # but REJECT/DROP some packages to reduce logging, | |
520 | # and ACCEPT critical ICMP types | |
521 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
522 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
523 | { action => 'PVEFW-DropBroadcast' }, | |
524 | # ACCEPT critical ICMP types | |
525 | { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, | |
526 | { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, | |
527 | # Drop packets with INVALID state | |
528 | "-m conntrack --ctstate INVALID -j DROP", | |
529 | # Drop Microsoft SMB noise | |
530 | { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
531 | { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, | |
532 | { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
533 | { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
534 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
535 | # Drop new/NotSyn traffic so that it doesn't get logged | |
536 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
537 | # Drop DNS replies | |
538 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
539 | ], | |
540 | 'PVEFW-tcpflags' => [ | |
541 | # same as shorewall tcpflags action. | |
542 | # Packets arriving on this interface are checked for som illegal combinations of TCP flags | |
543 | "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags", | |
544 | "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags", | |
545 | "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags", | |
546 | "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags", | |
547 | "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags", | |
548 | ], | |
549 | 'PVEFW-smurfs' => [ | |
550 | # same as shorewall smurfs action | |
551 | # Filter packets for smurfs (packets with a broadcast address as the source). | |
552 | "-s 0.0.0.0/32 -j RETURN", | |
553 | "-m addrtype --src-type BROADCAST -g PVEFW-smurflog", | |
554 | "-s 224.0.0.0/4 -g PVEFW-smurflog", | |
555 | ], | |
556 | }; | |
557 | ||
558 | # iptables -p icmp -h | |
559 | my $icmp_type_names = { | |
560 | any => 1, | |
561 | 'echo-reply' => 1, | |
562 | 'destination-unreachable' => 1, | |
563 | 'network-unreachable' => 1, | |
564 | 'host-unreachable' => 1, | |
565 | 'protocol-unreachable' => 1, | |
566 | 'port-unreachable' => 1, | |
567 | 'fragmentation-needed' => 1, | |
568 | 'source-route-failed' => 1, | |
569 | 'network-unknown' => 1, | |
570 | 'host-unknown' => 1, | |
571 | 'network-prohibited' => 1, | |
572 | 'host-prohibited' => 1, | |
573 | 'TOS-network-unreachable' => 1, | |
574 | 'TOS-host-unreachable' => 1, | |
575 | 'communication-prohibited' => 1, | |
576 | 'host-precedence-violation' => 1, | |
577 | 'precedence-cutoff' => 1, | |
578 | 'source-quench' => 1, | |
579 | 'redirect' => 1, | |
580 | 'network-redirect' => 1, | |
581 | 'host-redirect' => 1, | |
582 | 'TOS-network-redirect' => 1, | |
583 | 'TOS-host-redirect' => 1, | |
584 | 'echo-request' => 1, | |
585 | 'router-advertisement' => 1, | |
586 | 'router-solicitation' => 1, | |
587 | 'time-exceeded' => 1, | |
588 | 'ttl-zero-during-transit' => 1, | |
589 | 'ttl-zero-during-reassembly' => 1, | |
590 | 'parameter-problem' => 1, | |
591 | 'ip-header-bad' => 1, | |
592 | 'required-option-missing' => 1, | |
593 | 'timestamp-request' => 1, | |
594 | 'timestamp-reply' => 1, | |
595 | 'address-mask-request' => 1, | |
596 | 'address-mask-reply' => 1, | |
597 | }; | |
598 | ||
599 | sub init_firewall_macros { | |
600 | ||
601 | $pve_fw_parsed_macros = {}; | |
602 | ||
603 | foreach my $k (keys %$pve_fw_macros) { | |
604 | my $lc_name = lc($k); | |
605 | my $macro = $pve_fw_macros->{$k}; | |
606 | if (!ref($macro->[0])) { | |
607 | $pve_fw_macro_descr->{$k} = shift @$macro; | |
608 | } | |
609 | $pve_fw_preferred_macro_names->{$lc_name} = $k; | |
610 | $pve_fw_parsed_macros->{$k} = $macro; | |
611 | } | |
612 | } | |
613 | ||
614 | init_firewall_macros(); | |
615 | ||
616 | sub get_macros { | |
617 | return wantarray ? ($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros; | |
618 | } | |
619 | ||
620 | my $etc_services; | |
621 | ||
622 | sub get_etc_services { | |
623 | ||
624 | return $etc_services if $etc_services; | |
625 | ||
626 | my $filename = "/etc/services"; | |
627 | ||
628 | my $fh = IO::File->new($filename, O_RDONLY); | |
629 | if (!$fh) { | |
630 | warn "unable to read '$filename' - $!\n"; | |
631 | return {}; | |
632 | } | |
633 | ||
634 | my $services = {}; | |
635 | ||
636 | while (my $line = <$fh>) { | |
637 | chomp ($line); | |
638 | next if $line =~m/^#/; | |
639 | next if ($line =~m/^\s*$/); | |
640 | ||
641 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
642 | $services->{byid}->{$2}->{name} = $1; | |
643 | $services->{byid}->{$2}->{port} = $2; | |
644 | $services->{byid}->{$2}->{$3} = 1; | |
645 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
646 | } | |
647 | } | |
648 | ||
649 | close($fh); | |
650 | ||
651 | $etc_services = $services; | |
652 | ||
653 | ||
654 | return $etc_services; | |
655 | } | |
656 | ||
657 | my $etc_protocols; | |
658 | ||
659 | sub get_etc_protocols { | |
660 | return $etc_protocols if $etc_protocols; | |
661 | ||
662 | my $filename = "/etc/protocols"; | |
663 | ||
664 | my $fh = IO::File->new($filename, O_RDONLY); | |
665 | if (!$fh) { | |
666 | warn "unable to read '$filename' - $!\n"; | |
667 | return {}; | |
668 | } | |
669 | ||
670 | my $protocols = {}; | |
671 | ||
672 | while (my $line = <$fh>) { | |
673 | chomp ($line); | |
674 | next if $line =~m/^#/; | |
675 | next if ($line =~m/^\s*$/); | |
676 | ||
677 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
678 | $protocols->{byid}->{$2}->{name} = $1; | |
679 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
680 | } | |
681 | } | |
682 | ||
683 | close($fh); | |
684 | ||
685 | $etc_protocols = $protocols; | |
686 | ||
687 | return $etc_protocols; | |
688 | } | |
689 | ||
690 | my $ipv4_mask_hash_localnet = { | |
691 | '255.255.0.0' => 16, | |
692 | '255.255.128.0' => 17, | |
693 | '255.255.192.0' => 18, | |
694 | '255.255.224.0' => 19, | |
695 | '255.255.240.0' => 20, | |
696 | '255.255.248.0' => 21, | |
697 | '255.255.252.0' => 22, | |
698 | '255.255.254.0' => 23, | |
699 | '255.255.255.0' => 24, | |
700 | '255.255.255.128' => 25, | |
701 | '255.255.255.192' => 26, | |
702 | '255.255.255.224' => 27, | |
703 | '255.255.255.240' => 28, | |
704 | '255.255.255.248' => 29, | |
705 | '255.255.255.252' => 30, | |
706 | }; | |
707 | ||
708 | my $__local_network; | |
709 | ||
710 | sub local_network { | |
711 | my ($new_value) = @_; | |
712 | ||
713 | $__local_network = $new_value if defined($new_value); | |
714 | ||
715 | return $__local_network if defined($__local_network); | |
716 | ||
717 | eval { | |
718 | my $nodename = PVE::INotify::nodename(); | |
719 | ||
720 | my $ip = PVE::Cluster::remote_node_ip($nodename); | |
721 | ||
722 | my $testip = Net::IP->new($ip); | |
723 | ||
724 | my $routes = PVE::ProcFSTools::read_proc_net_route(); | |
725 | foreach my $entry (@$routes) { | |
726 | my $mask = $ipv4_mask_hash_localnet->{$entry->{mask}}; | |
727 | next if !defined($mask); | |
728 | return if $mask eq '0.0.0.0'; | |
729 | my $cidr = "$entry->{dest}/$mask"; | |
730 | my $testnet = Net::IP->new($cidr); | |
731 | if ($testnet->overlaps($testip)) { | |
732 | $__local_network = $cidr; | |
733 | return; | |
734 | } | |
735 | } | |
736 | }; | |
737 | warn $@ if $@; | |
738 | ||
739 | return $__local_network; | |
740 | } | |
741 | ||
742 | sub parse_address_list { | |
743 | my ($str) = @_; | |
744 | ||
745 | return if $str =~ m/^(\+)(\S+)$/; # ipset ref | |
746 | return if $str =~ m/^${ip_alias_pattern}$/; | |
747 | ||
748 | my $count = 0; | |
749 | my $iprange = 0; | |
750 | foreach my $elem (split(/,/, $str)) { | |
751 | $count++; | |
752 | if (!Net::IP->new($elem)) { | |
753 | my $err = Net::IP::Error(); | |
754 | die "invalid IP address: $err\n"; | |
755 | } | |
756 | $iprange = 1 if $elem =~ m/-/; | |
757 | } | |
758 | ||
759 | die "you can use a range in a list\n" if $iprange && $count > 1; | |
760 | } | |
761 | ||
762 | sub parse_port_name_number_or_range { | |
763 | my ($str) = @_; | |
764 | ||
765 | my $services = PVE::Firewall::get_etc_services(); | |
766 | my $count = 0; | |
767 | my $icmp_port = 0; | |
768 | ||
769 | foreach my $item (split(/,/, $str)) { | |
770 | $count++; | |
771 | if ($item =~ m/^(\d+):(\d+)$/) { | |
772 | my ($port1, $port2) = ($1, $2); | |
773 | die "invalid port '$port1'\n" if $port1 > 65535; | |
774 | die "invalid port '$port2'\n" if $port2 > 65535; | |
775 | } elsif ($item =~ m/^(\d+)$/) { | |
776 | my $port = $1; | |
777 | die "invalid port '$port'\n" if $port > 65535; | |
778 | } else { | |
779 | if ($icmp_type_names->{$item}) { | |
780 | $icmp_port = 1; | |
781 | } else { | |
782 | die "invalid port '$item'\n" if !$services->{byname}->{$item}; | |
783 | } | |
784 | } | |
785 | } | |
786 | ||
787 | die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1; | |
788 | ||
789 | return $count; | |
790 | } | |
791 | ||
792 | PVE::JSONSchema::register_format('pve-fw-port-spec', \&pve_fw_verify_port_spec); | |
793 | sub pve_fw_verify_port_spec { | |
794 | my ($portstr) = @_; | |
795 | ||
796 | parse_port_name_number_or_range($portstr); | |
797 | ||
798 | return $portstr; | |
799 | } | |
800 | ||
801 | PVE::JSONSchema::register_format('pve-fw-v4addr-spec', \&pve_fw_verify_v4addr_spec); | |
802 | sub pve_fw_verify_v4addr_spec { | |
803 | my ($list) = @_; | |
804 | ||
805 | parse_address_list($list); | |
806 | ||
807 | return $list; | |
808 | } | |
809 | ||
810 | PVE::JSONSchema::register_format('pve-fw-protocol-spec', \&pve_fw_verify_protocol_spec); | |
811 | sub pve_fw_verify_protocol_spec { | |
812 | my ($proto) = @_; | |
813 | ||
814 | my $protocols = get_etc_protocols(); | |
815 | ||
816 | die "unknown protocol '$proto'\n" if $proto && | |
817 | !(defined($protocols->{byname}->{$proto}) || | |
818 | defined($protocols->{byid}->{$proto})); | |
819 | ||
820 | return $proto; | |
821 | } | |
822 | ||
823 | ||
824 | # helper function for API | |
825 | ||
826 | sub copy_opject_with_digest { | |
827 | my ($object) = @_; | |
828 | ||
829 | my $sha = Digest::SHA->new('sha1'); | |
830 | ||
831 | my $res = {}; | |
832 | foreach my $k (sort keys %$object) { | |
833 | my $v = $object->{$k}; | |
834 | next if !defined($v); | |
835 | $res->{$k} = $v; | |
836 | $sha->add($k, ':', $v, "\n"); | |
837 | } | |
838 | ||
839 | my $digest = $sha->hexdigest; | |
840 | ||
841 | $res->{digest} = $digest; | |
842 | ||
843 | return wantarray ? ($res, $digest) : $res; | |
844 | } | |
845 | ||
846 | sub copy_list_with_digest { | |
847 | my ($list) = @_; | |
848 | ||
849 | my $sha = Digest::SHA->new('sha1'); | |
850 | ||
851 | my $res = []; | |
852 | foreach my $entry (@$list) { | |
853 | my $data = {}; | |
854 | foreach my $k (sort keys %$entry) { | |
855 | my $v = $entry->{$k}; | |
856 | next if !defined($v); | |
857 | $data->{$k} = $v; | |
858 | # Note: digest ignores refs ($rule->{errors}) | |
859 | $sha->add($k, ':', $v, "\n") if !ref($v); ; | |
860 | } | |
861 | push @$res, $data; | |
862 | } | |
863 | ||
864 | my $digest = $sha->hexdigest; | |
865 | ||
866 | foreach my $entry (@$res) { | |
867 | $entry->{digest} = $digest; | |
868 | } | |
869 | ||
870 | return wantarray ? ($res, $digest) : $res; | |
871 | } | |
872 | ||
873 | my $rule_properties = { | |
874 | pos => { | |
875 | description => "Update rule at position <pos>.", | |
876 | type => 'integer', | |
877 | minimum => 0, | |
878 | optional => 1, | |
879 | }, | |
880 | digest => get_standard_option('pve-config-digest'), | |
881 | type => { | |
882 | type => 'string', | |
883 | optional => 1, | |
884 | enum => ['in', 'out', 'group'], | |
885 | }, | |
886 | action => { | |
887 | description => "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.", | |
888 | type => 'string', | |
889 | optional => 1, | |
890 | pattern => $security_group_name_pattern, | |
891 | maxLength => 20, | |
892 | minLength => 2, | |
893 | }, | |
894 | macro => { | |
895 | type => 'string', | |
896 | optional => 1, | |
897 | maxLength => 128, | |
898 | }, | |
899 | iface => get_standard_option('pve-iface', { optional => 1 }), | |
900 | source => { | |
901 | type => 'string', format => 'pve-fw-v4addr-spec', | |
902 | optional => 1, | |
903 | }, | |
904 | dest => { | |
905 | type => 'string', format => 'pve-fw-v4addr-spec', | |
906 | optional => 1, | |
907 | }, | |
908 | proto => { | |
909 | type => 'string', format => 'pve-fw-protocol-spec', | |
910 | optional => 1, | |
911 | }, | |
912 | enable => { | |
913 | type => 'boolean', | |
914 | optional => 1, | |
915 | }, | |
916 | sport => { | |
917 | type => 'string', format => 'pve-fw-port-spec', | |
918 | optional => 1, | |
919 | }, | |
920 | dport => { | |
921 | type => 'string', format => 'pve-fw-port-spec', | |
922 | optional => 1, | |
923 | }, | |
924 | comment => { | |
925 | type => 'string', | |
926 | optional => 1, | |
927 | }, | |
928 | }; | |
929 | ||
930 | sub add_rule_properties { | |
931 | my ($properties) = @_; | |
932 | ||
933 | foreach my $k (keys %$rule_properties) { | |
934 | my $h = $rule_properties->{$k}; | |
935 | # copy data, so that we can modify later without side effects | |
936 | foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; } | |
937 | } | |
938 | ||
939 | return $properties; | |
940 | } | |
941 | ||
942 | sub delete_rule_properties { | |
943 | my ($rule, $delete_str) = @_; | |
944 | ||
945 | foreach my $opt (PVE::Tools::split_list($delete_str)) { | |
946 | raise_param_exc({ 'delete' => "no such property ('$opt')"}) | |
947 | if !defined($rule_properties->{$opt}); | |
948 | raise_param_exc({ 'delete' => "unable to delete required property '$opt'"}) | |
949 | if $opt eq 'type' || $opt eq 'action'; | |
950 | delete $rule->{$opt}; | |
951 | } | |
952 | ||
953 | return $rule; | |
954 | } | |
955 | ||
956 | my $apply_macro = sub { | |
957 | my ($macro_name, $param, $verify) = @_; | |
958 | ||
959 | my $macro_rules = $pve_fw_parsed_macros->{$macro_name}; | |
960 | die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen | |
961 | ||
962 | my $rules = []; | |
963 | ||
964 | foreach my $templ (@$macro_rules) { | |
965 | my $rule = {}; | |
966 | my $param_used = {}; | |
967 | foreach my $k (keys %$templ) { | |
968 | my $v = $templ->{$k}; | |
969 | if ($v eq 'PARAM') { | |
970 | $v = $param->{$k}; | |
971 | $param_used->{$k} = 1; | |
972 | } elsif ($v eq 'DEST') { | |
973 | $v = $param->{dest}; | |
974 | $param_used->{dest} = 1; | |
975 | } elsif ($v eq 'SOURCE') { | |
976 | $v = $param->{source}; | |
977 | $param_used->{source} = 1; | |
978 | } | |
979 | ||
980 | if (!defined($v)) { | |
981 | my $msg = "missing parameter '$k' in macro '$macro_name'"; | |
982 | raise_param_exc({ macro => $msg }) if $verify; | |
983 | die "$msg\n"; | |
984 | } | |
985 | $rule->{$k} = $v; | |
986 | } | |
987 | foreach my $k (keys %$param) { | |
988 | next if $k eq 'macro'; | |
989 | next if !defined($param->{$k}); | |
990 | next if $param_used->{$k}; | |
991 | if (defined($rule->{$k})) { | |
992 | if ($rule->{$k} ne $param->{$k}) { | |
993 | my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')"; | |
994 | raise_param_exc({ $k => $msg }) if $verify; | |
995 | die "$msg\n"; | |
996 | } | |
997 | } else { | |
998 | $rule->{$k} = $param->{$k}; | |
999 | } | |
1000 | } | |
1001 | push @$rules, $rule; | |
1002 | } | |
1003 | ||
1004 | return $rules; | |
1005 | }; | |
1006 | ||
1007 | my $rule_env_iface_lookup = { | |
1008 | 'ct' => 1, | |
1009 | 'vm' => 1, | |
1010 | 'group' => 0, | |
1011 | 'cluster' => 1, | |
1012 | 'host' => 1, | |
1013 | }; | |
1014 | ||
1015 | sub verify_rule { | |
1016 | my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_; | |
1017 | ||
1018 | my $allow_groups = $rule_env eq 'group' ? 0 : 1; | |
1019 | ||
1020 | my $allow_iface = $rule_env_iface_lookup->{$rule_env}; | |
1021 | die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen | |
1022 | ||
1023 | my $errors = $rule->{errors} || {}; | |
1024 | ||
1025 | my $error_count = 0; | |
1026 | ||
1027 | my $add_error = sub { | |
1028 | my ($param, $msg) = @_; | |
1029 | chomp $msg; | |
1030 | raise_param_exc({ $param => $msg }) if !$noerr; | |
1031 | $error_count++; | |
1032 | $errors->{$param} = $msg if !$errors->{$param}; | |
1033 | }; | |
1034 | ||
1035 | my $check_ipset_or_alias_property = sub { | |
1036 | my ($name) = @_; | |
1037 | ||
1038 | if (my $value = $rule->{$name}) { | |
1039 | if ($value =~ m/^\+/) { | |
1040 | if ($value =~ m/^\+(${security_group_name_pattern})$/) { | |
1041 | &$add_error($name, "no such ipset '$1'") | |
1042 | if !($cluster_conf->{ipset}->{$1} || ($fw_conf && $fw_conf->{ipset}->{$1})); | |
1043 | ||
1044 | } else { | |
1045 | &$add_error($name, "invalid security group name '$value'"); | |
1046 | } | |
1047 | } elsif ($value =~ m/^${ip_alias_pattern}$/){ | |
1048 | my $alias = lc($value); | |
1049 | &$add_error($name, "no such alias '$value'") | |
1050 | if !($cluster_conf->{aliases}->{$alias} || ($fw_conf && $fw_conf->{aliases}->{$alias})) | |
1051 | } | |
1052 | } | |
1053 | }; | |
1054 | ||
1055 | my $type = $rule->{type}; | |
1056 | my $action = $rule->{action}; | |
1057 | ||
1058 | &$add_error('type', "missing property") if !$type; | |
1059 | &$add_error('action', "missing property") if !$action; | |
1060 | ||
1061 | if ($type) { | |
1062 | if ($type eq 'in' || $type eq 'out') { | |
1063 | &$add_error('action', "unknown action '$action'") | |
1064 | if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/); | |
1065 | } elsif ($type eq 'group') { | |
1066 | &$add_error('type', "security groups not allowed") | |
1067 | if !$allow_groups; | |
1068 | &$add_error('action', "invalid characters in security group name") | |
1069 | if $action && ($action !~ m/^${security_group_name_pattern}$/); | |
1070 | } else { | |
1071 | &$add_error('type', "unknown rule type '$type'"); | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | if ($rule->{iface}) { | |
1076 | &$add_error('type', "parameter -i not allowed for this rule type") | |
1077 | if !$allow_iface; | |
1078 | eval { PVE::JSONSchema::pve_verify_iface($rule->{iface}); }; | |
1079 | &$add_error('iface', $@) if $@; | |
1080 | if ($rule_env eq 'vm') { | |
1081 | &$add_error('iface', "value does not match the regex pattern 'net\\d+'") | |
1082 | if $rule->{iface} !~ m/^net(\d+)$/; | |
1083 | } elsif ($rule_env eq 'ct') { | |
1084 | &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'") | |
1085 | if $rule->{iface} !~ m/^(venet|eth(\d+))$/; | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | if ($rule->{macro}) { | |
1090 | if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) { | |
1091 | $rule->{macro} = $preferred_name; | |
1092 | } else { | |
1093 | &$add_error('macro', "unknown macro '$rule->{macro}'"); | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | if ($rule->{proto}) { | |
1098 | eval { pve_fw_verify_protocol_spec($rule->{proto}); }; | |
1099 | &$add_error('proto', $@) if $@; | |
1100 | } | |
1101 | ||
1102 | if ($rule->{dport}) { | |
1103 | eval { parse_port_name_number_or_range($rule->{dport}); }; | |
1104 | &$add_error('dport', $@) if $@; | |
1105 | &$add_error('proto', "missing property - 'dport' requires this property") | |
1106 | if !$rule->{proto}; | |
1107 | } | |
1108 | ||
1109 | if ($rule->{sport}) { | |
1110 | eval { parse_port_name_number_or_range($rule->{sport}); }; | |
1111 | &$add_error('sport', $@) if $@; | |
1112 | &$add_error('proto', "missing property - 'sport' requires this property") | |
1113 | if !$rule->{proto}; | |
1114 | } | |
1115 | ||
1116 | if ($rule->{source}) { | |
1117 | eval { parse_address_list($rule->{source}); }; | |
1118 | &$add_error('source', $@) if $@; | |
1119 | &$check_ipset_or_alias_property('source'); | |
1120 | } | |
1121 | ||
1122 | if ($rule->{dest}) { | |
1123 | eval { parse_address_list($rule->{dest}); }; | |
1124 | &$add_error('dest', $@) if $@; | |
1125 | &$check_ipset_or_alias_property('dest'); | |
1126 | } | |
1127 | ||
1128 | if ($rule->{macro} && !$error_count) { | |
1129 | eval { &$apply_macro($rule->{macro}, $rule, 1); }; | |
1130 | if (my $err = $@) { | |
1131 | if (ref($err) eq "PVE::Exception" && $err->{errors}) { | |
1132 | my $eh = $err->{errors}; | |
1133 | foreach my $p (keys %$eh) { | |
1134 | &$add_error($p, $eh->{$p}); | |
1135 | } | |
1136 | } else { | |
1137 | &$add_error('macro', "$err"); | |
1138 | } | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | $rule->{errors} = $errors if $error_count; | |
1143 | ||
1144 | return $rule; | |
1145 | } | |
1146 | ||
1147 | sub copy_rule_data { | |
1148 | my ($rule, $param) = @_; | |
1149 | ||
1150 | foreach my $k (keys %$rule_properties) { | |
1151 | if (defined(my $v = $param->{$k})) { | |
1152 | if ($v eq '' || $v eq '-') { | |
1153 | delete $rule->{$k}; | |
1154 | } else { | |
1155 | $rule->{$k} = $v; | |
1156 | } | |
1157 | } | |
1158 | } | |
1159 | ||
1160 | return $rule; | |
1161 | } | |
1162 | ||
1163 | # core functions | |
1164 | my $bridge_firewall_enabled = 0; | |
1165 | ||
1166 | sub enable_bridge_firewall { | |
1167 | ||
1168 | return if $bridge_firewall_enabled; # only once | |
1169 | ||
1170 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1"); | |
1171 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1"); | |
1172 | ||
1173 | # make sure syncookies are enabled (which is default on newer 3.X kernels anyways) | |
1174 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1"); | |
1175 | ||
1176 | $bridge_firewall_enabled = 1; | |
1177 | } | |
1178 | ||
1179 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; | |
1180 | ||
1181 | sub iptables_restore_cmdlist { | |
1182 | my ($cmdlist) = @_; | |
1183 | ||
1184 | run_command("/sbin/iptables-restore -n", input => $cmdlist); | |
1185 | } | |
1186 | ||
1187 | sub ipset_restore_cmdlist { | |
1188 | my ($cmdlist) = @_; | |
1189 | ||
1190 | run_command("/usr/sbin/ipset restore", input => $cmdlist); | |
1191 | } | |
1192 | ||
1193 | sub iptables_get_chains { | |
1194 | ||
1195 | my $res = {}; | |
1196 | ||
1197 | # check what chains we want to track | |
1198 | my $is_pvefw_chain = sub { | |
1199 | my $name = shift; | |
1200 | ||
1201 | return 1 if $name =~ m/^PVEFW-\S+$/; | |
1202 | ||
1203 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; | |
1204 | ||
1205 | return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable | |
1206 | ||
1207 | return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/; | |
1208 | ||
1209 | return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/; | |
1210 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; | |
1211 | ||
1212 | return undef; | |
1213 | }; | |
1214 | ||
1215 | my $table = ''; | |
1216 | ||
1217 | my $hooks = {}; | |
1218 | ||
1219 | my $parser = sub { | |
1220 | my $line = shift; | |
1221 | ||
1222 | return if $line =~ m/^#/; | |
1223 | return if $line =~ m/^\s*$/; | |
1224 | ||
1225 | if ($line =~ m/^\*(\S+)$/) { | |
1226 | $table = $1; | |
1227 | return; | |
1228 | } | |
1229 | ||
1230 | return if $table ne 'filter'; | |
1231 | ||
1232 | if ($line =~ m/^:(\S+)\s/) { | |
1233 | my $chain = $1; | |
1234 | return if !&$is_pvefw_chain($chain); | |
1235 | $res->{$chain} = "unknown"; | |
1236 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { | |
1237 | my ($chain, $sig) = ($1, $2); | |
1238 | return if !&$is_pvefw_chain($chain); | |
1239 | $res->{$chain} = $sig; | |
1240 | } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) { | |
1241 | $hooks->{$1} = 1; | |
1242 | } else { | |
1243 | # simply ignore the rest | |
1244 | return; | |
1245 | } | |
1246 | }; | |
1247 | ||
1248 | run_command("/sbin/iptables-save", outfunc => $parser); | |
1249 | ||
1250 | return wantarray ? ($res, $hooks) : $res; | |
1251 | } | |
1252 | ||
1253 | sub iptables_chain_digest { | |
1254 | my ($rules) = @_; | |
1255 | my $digest = Digest::SHA->new('sha1'); | |
1256 | foreach my $rule (@$rules) { # order is important | |
1257 | $digest->add($rule); | |
1258 | } | |
1259 | return $digest->b64digest; | |
1260 | } | |
1261 | ||
1262 | sub ipset_chain_digest { | |
1263 | my ($rules) = @_; | |
1264 | ||
1265 | my $digest = Digest::SHA->new('sha1'); | |
1266 | foreach my $rule (sort @$rules) { # note: sorted | |
1267 | $digest->add($rule); | |
1268 | } | |
1269 | return $digest->b64digest; | |
1270 | } | |
1271 | ||
1272 | sub ipset_get_chains { | |
1273 | ||
1274 | my $res = {}; | |
1275 | my $chains = {}; | |
1276 | ||
1277 | my $parser = sub { | |
1278 | my $line = shift; | |
1279 | ||
1280 | return if $line =~ m/^#/; | |
1281 | return if $line =~ m/^\s*$/; | |
1282 | if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) { | |
1283 | my $chain = $1; | |
1284 | $line =~ s/\s+$//; # delete trailing white space | |
1285 | push @{$chains->{$chain}}, $line; | |
1286 | } else { | |
1287 | # simply ignore the rest | |
1288 | return; | |
1289 | } | |
1290 | }; | |
1291 | ||
1292 | run_command("/usr/sbin/ipset save", outfunc => $parser); | |
1293 | ||
1294 | # compute digest for each chain | |
1295 | foreach my $chain (keys %$chains) { | |
1296 | $res->{$chain} = ipset_chain_digest($chains->{$chain}); | |
1297 | } | |
1298 | ||
1299 | return $res; | |
1300 | } | |
1301 | ||
1302 | sub ruleset_generate_cmdstr { | |
1303 | my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; | |
1304 | ||
1305 | return if defined($rule->{enable}) && !$rule->{enable}; | |
1306 | return if $rule->{errors}; | |
1307 | ||
1308 | die "unable to emit macro - internal error" if $rule->{macro}; # should not happen | |
1309 | ||
1310 | my $nbdport = defined($rule->{dport}) ? parse_port_name_number_or_range($rule->{dport}) : 0; | |
1311 | my $nbsport = defined($rule->{sport}) ? parse_port_name_number_or_range($rule->{sport}) : 0; | |
1312 | ||
1313 | my @cmd = (); | |
1314 | ||
1315 | push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in}; | |
1316 | push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out}; | |
1317 | ||
1318 | my $source = $rule->{source}; | |
1319 | my $dest = $rule->{dest}; | |
1320 | ||
1321 | if ($source) { | |
1322 | if ($source =~ m/^\+/) { | |
1323 | if ($source =~ m/^\+(${security_group_name_pattern})$/) { | |
1324 | my $name = $1; | |
1325 | if ($fw_conf && $fw_conf->{ipset}->{$name}) { | |
1326 | die "implement me"; | |
1327 | } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { | |
1328 | push @cmd, "-m set --match-set PVEFW-$1 src"; | |
1329 | } else { | |
1330 | die "no such ipset '$name'\n"; | |
1331 | } | |
1332 | } else { | |
1333 | die "invalid security group name '$source'\n"; | |
1334 | } | |
1335 | } elsif ($source =~ m/^${ip_alias_pattern}$/){ | |
1336 | my $alias = lc($source); | |
1337 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; | |
1338 | $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; | |
1339 | die "no such alias '$source'\n" if !$e; | |
1340 | push @cmd, "-s $e->{cidr}"; | |
1341 | } elsif ($source =~ m/\-/){ | |
1342 | push @cmd, "-m iprange --src-range $source"; | |
1343 | } else { | |
1344 | push @cmd, "-s $source"; | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | if ($dest) { | |
1349 | if ($dest =~ m/^\+/) { | |
1350 | if ($dest =~ m/^\+(${security_group_name_pattern})$/) { | |
1351 | my $name = $1; | |
1352 | if ($fw_conf && $fw_conf->{ipset}->{$name}) { | |
1353 | die "implement me"; | |
1354 | } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { | |
1355 | push @cmd, "-m set --match-set PVEFW-$1 dst"; | |
1356 | } else { | |
1357 | die "no such ipset '$name'\n"; | |
1358 | } | |
1359 | } else { | |
1360 | die "invalid security group name '$dest'\n"; | |
1361 | } | |
1362 | } elsif ($dest =~ m/^${ip_alias_pattern}$/){ | |
1363 | my $alias = lc($dest); | |
1364 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; | |
1365 | $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; | |
1366 | die "no such alias '$dest'\n" if !$e; | |
1367 | push @cmd, "-d $e->{cidr}"; | |
1368 | } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){ | |
1369 | push @cmd, "-m iprange --dst-range $dest"; | |
1370 | } else { | |
1371 | push @cmd, "-d $dest"; | |
1372 | } | |
1373 | } | |
1374 | ||
1375 | if ($rule->{proto}) { | |
1376 | push @cmd, "-p $rule->{proto}"; | |
1377 | ||
1378 | my $multiport = 0; | |
1379 | $multiport++ if $nbdport > 1; | |
1380 | $multiport++ if $nbsport > 1; | |
1381 | ||
1382 | push @cmd, "--match multiport" if $multiport; | |
1383 | ||
1384 | die "multiport: option '--sports' cannot be used together with '--dports'\n" | |
1385 | if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); | |
1386 | ||
1387 | if ($rule->{dport}) { | |
1388 | if ($rule->{proto} && $rule->{proto} eq 'icmp') { | |
1389 | # Note: we use dport to store --icmp-type | |
1390 | die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}}); | |
1391 | push @cmd, "-m icmp --icmp-type $rule->{dport}"; | |
1392 | } else { | |
1393 | if ($nbdport > 1) { | |
1394 | if ($multiport == 2) { | |
1395 | push @cmd, "--ports $rule->{dport}"; | |
1396 | } else { | |
1397 | push @cmd, "--dports $rule->{dport}"; | |
1398 | } | |
1399 | } else { | |
1400 | push @cmd, "--dport $rule->{dport}"; | |
1401 | } | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | if ($rule->{sport}) { | |
1406 | if ($nbsport > 1) { | |
1407 | push @cmd, "--sports $rule->{sport}" if $multiport != 2; | |
1408 | } else { | |
1409 | push @cmd, "--sport $rule->{sport}"; | |
1410 | } | |
1411 | } | |
1412 | } elsif ($rule->{dport} || $rule->{sport}) { | |
1413 | die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport}; | |
1414 | die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport}; | |
1415 | } | |
1416 | ||
1417 | push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; | |
1418 | ||
1419 | if (my $action = $rule->{action}) { | |
1420 | $action = $actions->{$action} if defined($actions->{$action}); | |
1421 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
1422 | push @cmd, $goto ? "-g $action" : "-j $action"; | |
1423 | } | |
1424 | ||
1425 | return scalar(@cmd) ? join(' ', @cmd) : undef; | |
1426 | } | |
1427 | ||
1428 | sub ruleset_generate_rule { | |
1429 | my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; | |
1430 | ||
1431 | my $rules; | |
1432 | ||
1433 | if ($rule->{macro}) { | |
1434 | $rules = &$apply_macro($rule->{macro}, $rule); | |
1435 | } else { | |
1436 | $rules = [ $rule ]; | |
1437 | } | |
1438 | ||
1439 | # update all or nothing | |
1440 | ||
1441 | my @cmds = (); | |
1442 | foreach my $tmp (@$rules) { | |
1443 | if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) { | |
1444 | push @cmds, $cmdstr; | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | foreach my $cmdstr (@cmds) { | |
1449 | ruleset_addrule($ruleset, $chain, $cmdstr); | |
1450 | } | |
1451 | } | |
1452 | ||
1453 | sub ruleset_generate_rule_insert { | |
1454 | my ($ruleset, $chain, $rule, $actions, $goto) = @_; | |
1455 | ||
1456 | die "implement me" if $rule->{macro}; # not implemented, because not needed so far | |
1457 | ||
1458 | if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $rule, $actions, $goto)) { | |
1459 | ruleset_insertrule($ruleset, $chain, $cmdstr); | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | sub ruleset_create_chain { | |
1464 | my ($ruleset, $chain) = @_; | |
1465 | ||
1466 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; | |
1467 | die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format | |
1468 | ||
1469 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; | |
1470 | ||
1471 | $ruleset->{$chain} = []; | |
1472 | } | |
1473 | ||
1474 | sub ruleset_chain_exist { | |
1475 | my ($ruleset, $chain) = @_; | |
1476 | ||
1477 | return $ruleset->{$chain} ? 1 : undef; | |
1478 | } | |
1479 | ||
1480 | sub ruleset_addrule { | |
1481 | my ($ruleset, $chain, $rule) = @_; | |
1482 | ||
1483 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; | |
1484 | ||
1485 | push @{$ruleset->{$chain}}, "-A $chain $rule"; | |
1486 | } | |
1487 | ||
1488 | sub ruleset_insertrule { | |
1489 | my ($ruleset, $chain, $rule) = @_; | |
1490 | ||
1491 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; | |
1492 | ||
1493 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; | |
1494 | } | |
1495 | ||
1496 | sub get_log_rule_base { | |
1497 | my ($chain, $vmid, $msg, $loglevel) = @_; | |
1498 | ||
1499 | die "internal error - no log level" if !defined($loglevel); | |
1500 | ||
1501 | $vmid = 0 if !defined($vmid); | |
1502 | ||
1503 | # Note: we use special format for prefix to pass further | |
1504 | # info to log daemon (VMID, LOGVELEL and CHAIN) | |
1505 | ||
1506 | return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\""; | |
1507 | } | |
1508 | ||
1509 | sub ruleset_addlog { | |
1510 | my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_; | |
1511 | ||
1512 | return if !defined($loglevel); | |
1513 | ||
1514 | my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel); | |
1515 | ||
1516 | $logrule = "$rule $logrule" if defined($rule); | |
1517 | ||
1518 | ruleset_addrule($ruleset, $chain, $logrule); | |
1519 | } | |
1520 | ||
1521 | sub ruleset_add_chain_policy { | |
1522 | my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_; | |
1523 | ||
1524 | if ($policy eq 'ACCEPT') { | |
1525 | ||
1526 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' }, | |
1527 | { ACCEPT => $accept_action}); | |
1528 | ||
1529 | } elsif ($policy eq 'DROP') { | |
1530 | ||
1531 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop"); | |
1532 | ||
1533 | ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); | |
1534 | ||
1535 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
1536 | } elsif ($policy eq 'REJECT') { | |
1537 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject"); | |
1538 | ||
1539 | ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); | |
1540 | ||
1541 | ruleset_addrule($ruleset, $chain, "-g PVEFW-reject"); | |
1542 | } else { | |
1543 | # should not happen | |
1544 | die "internal error: unknown policy '$policy'"; | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | sub ruleset_chain_add_conn_filters { | |
1549 | my ($ruleset, $chain, $accept) = @_; | |
1550 | ||
1551 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); | |
1552 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept"); | |
1553 | } | |
1554 | ||
1555 | sub ruleset_chain_add_input_filters { | |
1556 | my ($ruleset, $chain, $options, $cluster_conf, $loglevel) = @_; | |
1557 | ||
1558 | if ($cluster_conf->{ipset}->{blacklist}){ | |
1559 | if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist")) { | |
1560 | ruleset_create_chain($ruleset, "PVEFW-blacklist"); | |
1561 | ruleset_addlog($ruleset, "PVEFW-blacklist", 0, "DROP: ", $loglevel) if $loglevel; | |
1562 | ruleset_addrule($ruleset, "PVEFW-blacklist", "-j DROP"); | |
1563 | } | |
1564 | ruleset_addrule($ruleset, $chain, "-m set --match-set PVEFW-blacklist src -j PVEFW-blacklist"); | |
1565 | } | |
1566 | ||
1567 | if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) { | |
1568 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs"); | |
1569 | } | |
1570 | ||
1571 | if ($options->{tcpflags}) { | |
1572 | ruleset_addrule($ruleset, $chain, "-p tcp -j PVEFW-tcpflags"); | |
1573 | } | |
1574 | } | |
1575 | ||
1576 | sub ruleset_create_vm_chain { | |
1577 | my ($ruleset, $chain, $options, $macaddr, $direction) = @_; | |
1578 | ||
1579 | ruleset_create_chain($ruleset, $chain); | |
1580 | my $accept = generate_nfqueue($options); | |
1581 | ||
1582 | if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) { | |
1583 | if ($direction eq 'OUT') { | |
1584 | ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK', | |
1585 | proto => 'udp', sport => 68, dport => 67 }); | |
1586 | } else { | |
1587 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT', | |
1588 | proto => 'udp', sport => 67, dport => 68 }); | |
1589 | } | |
1590 | } | |
1591 | ||
1592 | if ($direction eq 'OUT') { | |
1593 | if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) { | |
1594 | ruleset_addrule($ruleset, $chain, "-m mac ! --mac-source $macaddr -j DROP"); | |
1595 | } | |
1596 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
1597 | } | |
1598 | } | |
1599 | ||
1600 | sub ruleset_add_group_rule { | |
1601 | my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action) = @_; | |
1602 | ||
1603 | my $group = $rule->{action}; | |
1604 | my $group_chain = "GROUP-$group-$direction"; | |
1605 | if(!ruleset_chain_exist($ruleset, $group_chain)){ | |
1606 | generate_group_rules($ruleset, $cluster_conf, $group); | |
1607 | } | |
1608 | ||
1609 | if ($direction eq 'OUT' && $rule->{iface_out}) { | |
1610 | ruleset_addrule($ruleset, $chain, "-o $rule->{iface_out} -j $group_chain"); | |
1611 | } elsif ($direction eq 'IN' && $rule->{iface_in}) { | |
1612 | ruleset_addrule($ruleset, $chain, "-i $rule->{iface_in} -j $group_chain"); | |
1613 | } else { | |
1614 | ruleset_addrule($ruleset, $chain, "-j $group_chain"); | |
1615 | } | |
1616 | ||
1617 | ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j $action"); | |
1618 | } | |
1619 | ||
1620 | sub ruleset_generate_vm_rules { | |
1621 | my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options) = @_; | |
1622 | ||
1623 | my $lc_direction = lc($direction); | |
1624 | ||
1625 | my $in_accept = generate_nfqueue($options); | |
1626 | ||
1627 | foreach my $rule (@$rules) { | |
1628 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
1629 | next if !$rule->{enable} || $rule->{errors}; | |
1630 | if ($rule->{type} eq 'group') { | |
1631 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction, | |
1632 | $direction eq 'OUT' ? 'RETURN' : $in_accept); | |
1633 | } else { | |
1634 | next if $rule->{type} ne $lc_direction; | |
1635 | eval { | |
1636 | if ($direction eq 'OUT') { | |
1637 | ruleset_generate_rule($ruleset, $chain, $rule, | |
1638 | { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, | |
1639 | undef, $cluster_conf, $vmfw_conf); | |
1640 | } else { | |
1641 | ruleset_generate_rule($ruleset, $chain, $rule, | |
1642 | { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }, | |
1643 | undef, $cluster_conf, $vmfw_conf); | |
1644 | } | |
1645 | }; | |
1646 | warn $@ if $@; | |
1647 | } | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | sub generate_nfqueue { | |
1652 | my ($options) = @_; | |
1653 | ||
1654 | if ($options->{ips}) { | |
1655 | my $action = "NFQUEUE"; | |
1656 | if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) { | |
1657 | if (defined($3) && defined($1)) { | |
1658 | $action .= " --queue-balance $1:$3"; | |
1659 | } elsif (defined($1)) { | |
1660 | $action .= " --queue-num $1"; | |
1661 | } | |
1662 | } | |
1663 | $action .= " --queue-bypass" if $feature_ipset_nomatch; #need kernel 3.10 | |
1664 | return $action; | |
1665 | } else { | |
1666 | return "ACCEPT"; | |
1667 | } | |
1668 | } | |
1669 | ||
1670 | sub ruleset_generate_vm_ipsrules { | |
1671 | my ($ruleset, $options, $direction, $iface) = @_; | |
1672 | ||
1673 | if ($options->{ips} && $direction eq 'IN') { | |
1674 | my $nfqueue = generate_nfqueue($options); | |
1675 | ||
1676 | if (!ruleset_chain_exist($ruleset, "PVEFW-IPS")) { | |
1677 | ruleset_create_chain($ruleset, "PVEFW-IPS"); | |
1678 | } | |
1679 | ||
1680 | ruleset_addrule($ruleset, "PVEFW-IPS", "-m physdev --physdev-out $iface --physdev-is-bridged -j $nfqueue"); | |
1681 | } | |
1682 | } | |
1683 | ||
1684 | sub generate_venet_rules_direction { | |
1685 | my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction) = @_; | |
1686 | ||
1687 | my $lc_direction = lc($direction); | |
1688 | ||
1689 | my $rules = $vmfw_conf->{rules}; | |
1690 | ||
1691 | my $options = $vmfw_conf->{options}; | |
1692 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
1693 | ||
1694 | my $chain = "venet0-$vmid-$direction"; | |
1695 | ||
1696 | ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction); | |
1697 | ||
1698 | ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction); | |
1699 | ||
1700 | # implement policy | |
1701 | my $policy; | |
1702 | ||
1703 | if ($direction eq 'OUT') { | |
1704 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default | |
1705 | } else { | |
1706 | $policy = $options->{policy_in} || 'DROP'; # allow nothing by default | |
1707 | } | |
1708 | ||
1709 | my $accept = generate_nfqueue($options); | |
1710 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; | |
1711 | ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action); | |
1712 | ||
1713 | if ($direction eq 'OUT') { | |
1714 | ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT", { | |
1715 | action => $chain, | |
1716 | source => $ip, | |
1717 | iface_in => 'venet0'}); | |
1718 | } else { | |
1719 | ruleset_generate_rule($ruleset, "PVEFW-VENET-IN", { | |
1720 | action => $chain, | |
1721 | dest => $ip, | |
1722 | iface_out => 'venet0'}); | |
1723 | } | |
1724 | } | |
1725 | ||
1726 | sub generate_tap_rules_direction { | |
1727 | my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction) = @_; | |
1728 | ||
1729 | my $lc_direction = lc($direction); | |
1730 | ||
1731 | my $rules = $vmfw_conf->{rules}; | |
1732 | ||
1733 | my $options = $vmfw_conf->{options}; | |
1734 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
1735 | ||
1736 | my $tapchain = "$iface-$direction"; | |
1737 | ||
1738 | ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction); | |
1739 | ||
1740 | ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options); | |
1741 | ||
1742 | ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface); | |
1743 | ||
1744 | # implement policy | |
1745 | my $policy; | |
1746 | ||
1747 | if ($direction eq 'OUT') { | |
1748 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default | |
1749 | } else { | |
1750 | $policy = $options->{policy_in} || 'DROP'; # allow nothing by default | |
1751 | } | |
1752 | ||
1753 | my $accept = generate_nfqueue($options); | |
1754 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; | |
1755 | ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action); | |
1756 | ||
1757 | # plug the tap chain to bridge chain | |
1758 | if ($direction eq 'IN') { | |
1759 | ruleset_addrule($ruleset, "PVEFW-FWBR-IN", | |
1760 | "-m physdev --physdev-is-bridged --physdev-out $iface -j $tapchain"); | |
1761 | } else { | |
1762 | ruleset_addrule($ruleset, "PVEFW-FWBR-OUT", | |
1763 | "-m physdev --physdev-is-bridged --physdev-in $iface -j $tapchain"); | |
1764 | } | |
1765 | } | |
1766 | ||
1767 | sub enable_host_firewall { | |
1768 | my ($ruleset, $hostfw_conf, $cluster_conf) = @_; | |
1769 | ||
1770 | my $options = $hostfw_conf->{options}; | |
1771 | my $cluster_options = $cluster_conf->{options}; | |
1772 | my $rules = $hostfw_conf->{rules}; | |
1773 | my $cluster_rules = $cluster_conf->{rules}; | |
1774 | ||
1775 | # host inbound firewall | |
1776 | my $chain = "PVEFW-HOST-IN"; | |
1777 | ruleset_create_chain($ruleset, $chain); | |
1778 | ||
1779 | my $loglevel = get_option_log_level($options, "log_level_in"); | |
1780 | ||
1781 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); | |
1782 | ||
1783 | ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); | |
1784 | ruleset_chain_add_input_filters($ruleset, $chain, $options, $cluster_conf, $loglevel); | |
1785 | ||
1786 | # we use RETURN because we need to check also tap rules | |
1787 | my $accept_action = 'RETURN'; | |
1788 | ||
1789 | ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast | |
1790 | ||
1791 | # add host rules first, so that cluster wide rules can be overwritten | |
1792 | foreach my $rule (@$rules, @$cluster_rules) { | |
1793 | next if !$rule->{enable} || $rule->{errors}; | |
1794 | ||
1795 | $rule->{iface_in} = $rule->{iface} if $rule->{iface}; | |
1796 | ||
1797 | eval { | |
1798 | if ($rule->{type} eq 'group') { | |
1799 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action); | |
1800 | } elsif ($rule->{type} eq 'in') { | |
1801 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, | |
1802 | undef, $cluster_conf, $hostfw_conf); | |
1803 | } | |
1804 | }; | |
1805 | warn $@ if $@; | |
1806 | delete $rule->{iface_in}; | |
1807 | } | |
1808 | ||
1809 | # allow standard traffic for management ipset (includes cluster network) | |
1810 | my $mngmntsrc = "-m set --match-set PVEFW-management src"; | |
1811 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 8006 -j $accept_action"); # PVE API | |
1812 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 5900:5999 -j $accept_action"); # PVE VNC Console | |
1813 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 3128 -j $accept_action"); # SPICE Proxy | |
1814 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 22 -j $accept_action"); # SSH | |
1815 | ||
1816 | my $localnet = local_network(); | |
1817 | ||
1818 | # corosync | |
1819 | if ($localnet) { | |
1820 | my $corosync_rule = "-p udp --dport 5404:5405 -j $accept_action"; | |
1821 | ruleset_addrule($ruleset, $chain, "-s $localnet -d $localnet $corosync_rule"); | |
1822 | ruleset_addrule($ruleset, $chain, "-s $localnet -m addrtype --dst-type MULTICAST $corosync_rule"); | |
1823 | } | |
1824 | ||
1825 | # implement input policy | |
1826 | my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default | |
1827 | ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action); | |
1828 | ||
1829 | # host outbound firewall | |
1830 | $chain = "PVEFW-HOST-OUT"; | |
1831 | ruleset_create_chain($ruleset, $chain); | |
1832 | ||
1833 | $loglevel = get_option_log_level($options, "log_level_out"); | |
1834 | ||
1835 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); | |
1836 | ||
1837 | ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); | |
1838 | ||
1839 | # we use RETURN because we may want to check other thigs later | |
1840 | $accept_action = 'RETURN'; | |
1841 | ||
1842 | ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast | |
1843 | ||
1844 | # add host rules first, so that cluster wide rules can be overwritten | |
1845 | foreach my $rule (@$rules, @$cluster_rules) { | |
1846 | next if !$rule->{enable} || $rule->{errors}; | |
1847 | ||
1848 | $rule->{iface_out} = $rule->{iface} if $rule->{iface}; | |
1849 | eval { | |
1850 | if ($rule->{type} eq 'group') { | |
1851 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action); | |
1852 | } elsif ($rule->{type} eq 'out') { | |
1853 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, | |
1854 | undef, $cluster_conf, $hostfw_conf); | |
1855 | } | |
1856 | }; | |
1857 | warn $@ if $@; | |
1858 | delete $rule->{iface_out}; | |
1859 | } | |
1860 | ||
1861 | # allow standard traffic on cluster network | |
1862 | if ($localnet) { | |
1863 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 8006 -j $accept_action"); # PVE API | |
1864 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 22 -j $accept_action"); # SSH | |
1865 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 5900:5999 -j $accept_action"); # PVE VNC Console | |
1866 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 3128 -j $accept_action"); # SPICE Proxy | |
1867 | ||
1868 | my $corosync_rule = "-p udp --dport 5404:5405 -j $accept_action"; | |
1869 | ruleset_addrule($ruleset, $chain, "-d $localnet $corosync_rule"); | |
1870 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST $corosync_rule"); | |
1871 | } | |
1872 | ||
1873 | # implement output policy | |
1874 | $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default | |
1875 | ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action); | |
1876 | ||
1877 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); | |
1878 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
1879 | } | |
1880 | ||
1881 | sub generate_group_rules { | |
1882 | my ($ruleset, $cluster_conf, $group) = @_; | |
1883 | ||
1884 | my $rules = $cluster_conf->{groups}->{$group}; | |
1885 | ||
1886 | if (!$rules) { | |
1887 | warn "no such security group '$group'\n"; | |
1888 | $rules = []; # create empty chain | |
1889 | } | |
1890 | ||
1891 | my $chain = "GROUP-${group}-IN"; | |
1892 | ||
1893 | ruleset_create_chain($ruleset, $chain); | |
1894 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
1895 | ||
1896 | foreach my $rule (@$rules) { | |
1897 | next if $rule->{type} ne 'in'; | |
1898 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, undef, $cluster_conf); | |
1899 | } | |
1900 | ||
1901 | $chain = "GROUP-${group}-OUT"; | |
1902 | ||
1903 | ruleset_create_chain($ruleset, $chain); | |
1904 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
1905 | ||
1906 | foreach my $rule (@$rules) { | |
1907 | next if $rule->{type} ne 'out'; | |
1908 | # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to | |
1909 | # check also other tap rules later | |
1910 | ruleset_generate_rule($ruleset, $chain, $rule, | |
1911 | { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }, undef, $cluster_conf); | |
1912 | } | |
1913 | } | |
1914 | ||
1915 | my $MAX_NETS = 32; | |
1916 | my $valid_netdev_names = {}; | |
1917 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
1918 | $valid_netdev_names->{"net$i"} = 1; | |
1919 | } | |
1920 | ||
1921 | sub parse_fw_rule { | |
1922 | my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_; | |
1923 | ||
1924 | chomp $line; | |
1925 | ||
1926 | my $orig_line = $line; | |
1927 | ||
1928 | my $rule = {}; | |
1929 | ||
1930 | # we can add single line comments to the end of the rule | |
1931 | if ($line =~ s/#\s*(.*?)\s*$//) { | |
1932 | $rule->{comment} = decode('utf8', $1); | |
1933 | } | |
1934 | ||
1935 | # we can disable a rule when prefixed with '|' | |
1936 | ||
1937 | $rule->{enable} = $line =~ s/^\|// ? 0 : 1; | |
1938 | ||
1939 | $line =~ s/^(\S+)\s+(\S+)\s*// || | |
1940 | die "unable to parse rule: $line\n"; | |
1941 | ||
1942 | $rule->{type} = lc($1); | |
1943 | $rule->{action} = $2; | |
1944 | ||
1945 | if ($rule->{type} eq 'in' || $rule->{type} eq 'out') { | |
1946 | if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
1947 | $rule->{macro} = $1; | |
1948 | $rule->{action} = $2; | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | while (length($line)) { | |
1953 | if ($line =~ s/^-i (\S+)\s*//) { | |
1954 | $rule->{iface} = $1; | |
1955 | next; | |
1956 | } | |
1957 | ||
1958 | last if $rule->{type} eq 'group'; | |
1959 | ||
1960 | if ($line =~ s/^-p (\S+)\s*//) { | |
1961 | $rule->{proto} = $1; | |
1962 | next; | |
1963 | } | |
1964 | ||
1965 | if ($line =~ s/^-dport (\S+)\s*//) { | |
1966 | $rule->{dport} = $1; | |
1967 | next; | |
1968 | } | |
1969 | ||
1970 | if ($line =~ s/^-sport (\S+)\s*//) { | |
1971 | $rule->{sport} = $1; | |
1972 | next; | |
1973 | } | |
1974 | if ($line =~ s/^-source (\S+)\s*//) { | |
1975 | $rule->{source} = $1; | |
1976 | next; | |
1977 | } | |
1978 | if ($line =~ s/^-dest (\S+)\s*//) { | |
1979 | $rule->{dest} = $1; | |
1980 | next; | |
1981 | } | |
1982 | ||
1983 | last; | |
1984 | } | |
1985 | ||
1986 | die "unable to parse rule parameters: $line\n" if length($line); | |
1987 | ||
1988 | $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1); | |
1989 | if ($verbose && $rule->{errors}) { | |
1990 | warn "$prefix - errors in rule parameters: $orig_line\n"; | |
1991 | foreach my $p (keys %{$rule->{errors}}) { | |
1992 | warn " $p: $rule->{errors}->{$p}\n"; | |
1993 | } | |
1994 | } | |
1995 | ||
1996 | return $rule; | |
1997 | } | |
1998 | ||
1999 | sub parse_vmfw_option { | |
2000 | my ($line) = @_; | |
2001 | ||
2002 | my ($opt, $value); | |
2003 | ||
2004 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
2005 | ||
2006 | if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) { | |
2007 | $opt = lc($1); | |
2008 | $value = int($2); | |
2009 | } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) { | |
2010 | $opt = lc($1); | |
2011 | $value = $2 ? lc($3) : ''; | |
2012 | } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
2013 | $opt = lc($1); | |
2014 | $value = uc($3); | |
2015 | } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) { | |
2016 | $opt = lc($1); | |
2017 | $value = $2; | |
2018 | } else { | |
2019 | chomp $line; | |
2020 | die "can't parse option '$line'\n" | |
2021 | } | |
2022 | ||
2023 | return ($opt, $value); | |
2024 | } | |
2025 | ||
2026 | sub parse_hostfw_option { | |
2027 | my ($line) = @_; | |
2028 | ||
2029 | my ($opt, $value); | |
2030 | ||
2031 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
2032 | ||
2033 | if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) { | |
2034 | $opt = lc($1); | |
2035 | $value = int($2); | |
2036 | } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) { | |
2037 | $opt = lc($1); | |
2038 | $value = $2 ? lc($3) : ''; | |
2039 | } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) { | |
2040 | $opt = lc($1); | |
2041 | $value = int($2); | |
2042 | } else { | |
2043 | chomp $line; | |
2044 | die "can't parse option '$line'\n" | |
2045 | } | |
2046 | ||
2047 | return ($opt, $value); | |
2048 | } | |
2049 | ||
2050 | sub parse_clusterfw_option { | |
2051 | my ($line) = @_; | |
2052 | ||
2053 | my ($opt, $value); | |
2054 | ||
2055 | if ($line =~ m/^(enable):\s*(0|1)\s*$/i) { | |
2056 | $opt = lc($1); | |
2057 | $value = int($2); | |
2058 | } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
2059 | $opt = lc($1); | |
2060 | $value = uc($3); | |
2061 | } else { | |
2062 | chomp $line; | |
2063 | die "can't parse option '$line'\n" | |
2064 | } | |
2065 | ||
2066 | return ($opt, $value); | |
2067 | } | |
2068 | ||
2069 | sub parse_alias { | |
2070 | my ($line) = @_; | |
2071 | ||
2072 | # we can add single line comments to the end of the line | |
2073 | my $comment = decode('utf8', $1) if $line =~ s/\s*#\s*(.*?)\s*$//; | |
2074 | ||
2075 | if ($line =~ m/^(\S+)\s(\S+)$/) { | |
2076 | my ($name, $cidr) = ($1, $2); | |
2077 | $cidr =~ s|/32$||; | |
2078 | pve_verify_ipv4_or_cidr($cidr); | |
2079 | my $data = { | |
2080 | name => $name, | |
2081 | cidr => $cidr, | |
2082 | }; | |
2083 | $data->{comment} = $comment if $comment; | |
2084 | return $data; | |
2085 | } | |
2086 | ||
2087 | return undef; | |
2088 | } | |
2089 | ||
2090 | sub parse_vm_fw_rules { | |
2091 | my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_; | |
2092 | ||
2093 | my $res = { | |
2094 | rules => [], | |
2095 | options => {}, | |
2096 | aliases => {}, | |
2097 | }; | |
2098 | ||
2099 | my $section; | |
2100 | ||
2101 | while (defined(my $line = <$fh>)) { | |
2102 | next if $line =~ m/^#/; | |
2103 | next if $line =~ m/^\s*$/; | |
2104 | ||
2105 | my $linenr = $fh->input_line_number(); | |
2106 | my $prefix = "$filename (line $linenr)"; | |
2107 | ||
2108 | if ($line =~ m/^\[(\S+)\]\s*$/i) { | |
2109 | $section = lc($1); | |
2110 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; | |
2111 | next; | |
2112 | } | |
2113 | if (!$section) { | |
2114 | warn "$prefix: skip line - no section"; | |
2115 | next; | |
2116 | } | |
2117 | ||
2118 | next if !$res->{$section}; # skip undefined section | |
2119 | ||
2120 | if ($section eq 'options') { | |
2121 | eval { | |
2122 | my ($opt, $value) = parse_vmfw_option($line); | |
2123 | $res->{options}->{$opt} = $value; | |
2124 | }; | |
2125 | warn "$prefix: $@" if $@; | |
2126 | next; | |
2127 | } | |
2128 | ||
2129 | if ($section eq 'aliases') { | |
2130 | eval { | |
2131 | my $data = parse_alias($line); | |
2132 | $res->{aliases}->{lc($data->{name})} = $data; | |
2133 | }; | |
2134 | warn "$prefix: $@" if $@; | |
2135 | next; | |
2136 | } | |
2137 | ||
2138 | my $rule; | |
2139 | eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); }; | |
2140 | if (my $err = $@) { | |
2141 | warn "$prefix: $err"; | |
2142 | next; | |
2143 | } | |
2144 | ||
2145 | push @{$res->{$section}}, $rule; | |
2146 | } | |
2147 | ||
2148 | return $res; | |
2149 | } | |
2150 | ||
2151 | sub parse_host_fw_rules { | |
2152 | my ($filename, $fh, $cluster_conf, $verbose) = @_; | |
2153 | ||
2154 | my $res = { rules => [], options => {}}; | |
2155 | ||
2156 | my $section; | |
2157 | ||
2158 | while (defined(my $line = <$fh>)) { | |
2159 | next if $line =~ m/^#/; | |
2160 | next if $line =~ m/^\s*$/; | |
2161 | ||
2162 | my $linenr = $fh->input_line_number(); | |
2163 | my $prefix = "$filename (line $linenr)"; | |
2164 | ||
2165 | if ($line =~ m/^\[(\S+)\]\s*$/i) { | |
2166 | $section = lc($1); | |
2167 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; | |
2168 | next; | |
2169 | } | |
2170 | if (!$section) { | |
2171 | warn "$prefix: skip line - no section"; | |
2172 | next; | |
2173 | } | |
2174 | ||
2175 | next if !$res->{$section}; # skip undefined section | |
2176 | ||
2177 | if ($section eq 'options') { | |
2178 | eval { | |
2179 | my ($opt, $value) = parse_hostfw_option($line); | |
2180 | $res->{options}->{$opt} = $value; | |
2181 | }; | |
2182 | warn "$prefix: $@" if $@; | |
2183 | next; | |
2184 | } | |
2185 | ||
2186 | my $rule; | |
2187 | eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, 'host', $verbose); }; | |
2188 | if (my $err = $@) { | |
2189 | warn "$prefix: $err"; | |
2190 | next; | |
2191 | } | |
2192 | ||
2193 | push @{$res->{$section}}, $rule; | |
2194 | } | |
2195 | ||
2196 | return $res; | |
2197 | } | |
2198 | ||
2199 | sub parse_cluster_fw_rules { | |
2200 | my ($filename, $fh, $verbose) = @_; | |
2201 | ||
2202 | my $section; | |
2203 | my $group; | |
2204 | ||
2205 | my $res = { | |
2206 | rules => [], | |
2207 | options => {}, | |
2208 | aliases => {}, | |
2209 | groups => {}, | |
2210 | group_comments => {}, | |
2211 | ipset => {} , | |
2212 | ipset_comments => {}, | |
2213 | }; | |
2214 | ||
2215 | while (defined(my $line = <$fh>)) { | |
2216 | next if $line =~ m/^#/; | |
2217 | next if $line =~ m/^\s*$/; | |
2218 | ||
2219 | my $linenr = $fh->input_line_number(); | |
2220 | my $prefix = "$filename (line $linenr)"; | |
2221 | ||
2222 | if ($line =~ m/^\[options\]$/i) { | |
2223 | $section = 'options'; | |
2224 | next; | |
2225 | } | |
2226 | ||
2227 | if ($line =~ m/^\[aliases\]$/i) { | |
2228 | $section = 'aliases'; | |
2229 | next; | |
2230 | } | |
2231 | ||
2232 | if ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i) { | |
2233 | $section = 'groups'; | |
2234 | $group = lc($1); | |
2235 | my $comment = $2; | |
2236 | $res->{$section}->{$group} = []; | |
2237 | $res->{group_comments}->{$group} = decode('utf8', $comment) | |
2238 | if $comment; | |
2239 | next; | |
2240 | } | |
2241 | ||
2242 | if ($line =~ m/^\[rules\]$/i) { | |
2243 | $section = 'rules'; | |
2244 | next; | |
2245 | } | |
2246 | ||
2247 | if ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i) { | |
2248 | $section = 'ipset'; | |
2249 | $group = lc($1); | |
2250 | my $comment = $2; | |
2251 | $res->{$section}->{$group} = []; | |
2252 | $res->{ipset_comments}->{$group} = decode('utf8', $comment) | |
2253 | if $comment; | |
2254 | next; | |
2255 | } | |
2256 | ||
2257 | if (!$section) { | |
2258 | warn "$prefix: skip line - no section\n"; | |
2259 | next; | |
2260 | } | |
2261 | ||
2262 | if ($section eq 'options') { | |
2263 | eval { | |
2264 | my ($opt, $value) = parse_clusterfw_option($line); | |
2265 | $res->{options}->{$opt} = $value; | |
2266 | }; | |
2267 | warn "$prefix: $@" if $@; | |
2268 | } elsif ($section eq 'aliases') { | |
2269 | eval { | |
2270 | my $data = parse_alias($line); | |
2271 | $res->{aliases}->{lc($data->{name})} = $data; | |
2272 | }; | |
2273 | warn "$prefix: $@" if $@; | |
2274 | } elsif ($section eq 'rules') { | |
2275 | my $rule; | |
2276 | eval { $rule = parse_fw_rule($prefix, $line, $res, undef, 'cluster', $verbose); }; | |
2277 | if (my $err = $@) { | |
2278 | warn "$prefix: $err"; | |
2279 | next; | |
2280 | } | |
2281 | push @{$res->{$section}}, $rule; | |
2282 | } elsif ($section eq 'groups') { | |
2283 | my $rule; | |
2284 | eval { $rule = parse_fw_rule($prefix, $line, $res, undef, 'group', $verbose); }; | |
2285 | if (my $err = $@) { | |
2286 | warn "$prefix: $err"; | |
2287 | next; | |
2288 | } | |
2289 | push @{$res->{$section}->{$group}}, $rule; | |
2290 | } elsif ($section eq 'ipset') { | |
2291 | # we can add single line comments to the end of the rule | |
2292 | my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//; | |
2293 | ||
2294 | $line =~ m/^(\!)?\s*(\S+)\s*$/; | |
2295 | my $nomatch = $1; | |
2296 | my $cidr = $2; | |
2297 | ||
2298 | if($cidr !~ m/^${ip_alias_pattern}$/) { | |
2299 | $cidr =~ s|/32$||; | |
2300 | ||
2301 | eval { pve_verify_ipv4_or_cidr($cidr); }; | |
2302 | if (my $err = $@) { | |
2303 | warn "$prefix: $cidr - $err"; | |
2304 | next; | |
2305 | } | |
2306 | } | |
2307 | ||
2308 | my $entry = { cidr => $cidr }; | |
2309 | $entry->{nomatch} = 1 if $nomatch; | |
2310 | $entry->{comment} = $comment if $comment; | |
2311 | ||
2312 | push @{$res->{$section}->{$group}}, $entry; | |
2313 | } | |
2314 | } | |
2315 | ||
2316 | return $res; | |
2317 | } | |
2318 | ||
2319 | sub run_locked { | |
2320 | my ($code, @param) = @_; | |
2321 | ||
2322 | my $timeout = 10; | |
2323 | ||
2324 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
2325 | ||
2326 | die $@ if $@; | |
2327 | ||
2328 | return $res; | |
2329 | } | |
2330 | ||
2331 | sub read_local_vm_config { | |
2332 | ||
2333 | my $openvz = {}; | |
2334 | my $qemu = {}; | |
2335 | ||
2336 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
2337 | ||
2338 | my $vmlist = PVE::Cluster::get_vmlist(); | |
2339 | return $vmdata if !$vmlist || !$vmlist->{ids}; | |
2340 | my $ids = $vmlist->{ids}; | |
2341 | ||
2342 | foreach my $vmid (keys %$ids) { | |
2343 | next if !$vmid; # skip VE0 | |
2344 | my $d = $ids->{$vmid}; | |
2345 | next if !$d->{node} || $d->{node} ne $nodename; | |
2346 | next if !$d->{type}; | |
2347 | if ($d->{type} eq 'openvz') { | |
2348 | if ($have_pve_manager) { | |
2349 | my $cfspath = PVE::OpenVZ::cfs_config_path($vmid); | |
2350 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
2351 | $openvz->{$vmid} = $conf; | |
2352 | } | |
2353 | } | |
2354 | } elsif ($d->{type} eq 'qemu') { | |
2355 | if ($have_qemu_server) { | |
2356 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); | |
2357 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
2358 | $qemu->{$vmid} = $conf; | |
2359 | } | |
2360 | } | |
2361 | } | |
2362 | } | |
2363 | ||
2364 | return $vmdata; | |
2365 | }; | |
2366 | ||
2367 | sub load_vmfw_conf { | |
2368 | my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_; | |
2369 | ||
2370 | my $vmfw_conf = {}; | |
2371 | ||
2372 | $dir = "/etc/pve/firewall" if !defined($dir); | |
2373 | ||
2374 | my $filename = "$dir/$vmid.fw"; | |
2375 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
2376 | $vmfw_conf = parse_vm_fw_rules($filename, $fh, $cluster_conf, $rule_env, $verbose); | |
2377 | } | |
2378 | ||
2379 | return $vmfw_conf; | |
2380 | } | |
2381 | ||
2382 | my $format_rules = sub { | |
2383 | my ($rules, $allow_iface) = @_; | |
2384 | ||
2385 | my $raw = ''; | |
2386 | ||
2387 | foreach my $rule (@$rules) { | |
2388 | if ($rule->{type} eq 'in' || $rule->{type} eq 'out' || $rule->{type} eq 'group') { | |
2389 | $raw .= '|' if defined($rule->{enable}) && !$rule->{enable}; | |
2390 | $raw .= uc($rule->{type}); | |
2391 | if ($rule->{macro}) { | |
2392 | $raw .= " $rule->{macro}($rule->{action})"; | |
2393 | } else { | |
2394 | $raw .= " " . $rule->{action}; | |
2395 | } | |
2396 | if ($allow_iface && $rule->{iface}) { | |
2397 | $raw .= " -i $rule->{iface}"; | |
2398 | } | |
2399 | ||
2400 | if ($rule->{type} ne 'group') { | |
2401 | $raw .= " -source $rule->{source}" if $rule->{source}; | |
2402 | $raw .= " -dest $rule->{dest}" if $rule->{dest}; | |
2403 | $raw .= " -p $rule->{proto}" if $rule->{proto}; | |
2404 | $raw .= " -dport $rule->{dport}" if $rule->{dport}; | |
2405 | $raw .= " -sport $rule->{sport}" if $rule->{sport}; | |
2406 | } | |
2407 | ||
2408 | $raw .= " # " . encode('utf8', $rule->{comment}) | |
2409 | if $rule->{comment} && $rule->{comment} !~ m/^\s*$/; | |
2410 | $raw .= "\n"; | |
2411 | } else { | |
2412 | die "unknown rule type '$rule->{type}'"; | |
2413 | } | |
2414 | } | |
2415 | ||
2416 | return $raw; | |
2417 | }; | |
2418 | ||
2419 | my $format_options = sub { | |
2420 | my ($options) = @_; | |
2421 | ||
2422 | my $raw = ''; | |
2423 | ||
2424 | $raw .= "[OPTIONS]\n\n"; | |
2425 | foreach my $opt (keys %$options) { | |
2426 | $raw .= "$opt: $options->{$opt}\n"; | |
2427 | } | |
2428 | $raw .= "\n"; | |
2429 | ||
2430 | return $raw; | |
2431 | }; | |
2432 | ||
2433 | my $format_aliases = sub { | |
2434 | my ($aliases) = @_; | |
2435 | ||
2436 | my $raw = ''; | |
2437 | ||
2438 | $raw .= "[ALIASES]\n\n"; | |
2439 | foreach my $k (keys %$aliases) { | |
2440 | my $e = $aliases->{$k}; | |
2441 | $raw .= "$e->{name} $e->{cidr}"; | |
2442 | $raw .= " # " . encode('utf8', $e->{comment}) | |
2443 | if $e->{comment} && $e->{comment} !~ m/^\s*$/; | |
2444 | $raw .= "\n"; | |
2445 | } | |
2446 | $raw .= "\n"; | |
2447 | ||
2448 | return $raw; | |
2449 | }; | |
2450 | ||
2451 | my $format_ipset = sub { | |
2452 | my ($options) = @_; | |
2453 | ||
2454 | my $raw = ''; | |
2455 | ||
2456 | my $nethash = {}; | |
2457 | foreach my $entry (@$options) { | |
2458 | $nethash->{$entry->{cidr}} = $entry; | |
2459 | } | |
2460 | ||
2461 | foreach my $cidr (sort keys %$nethash) { | |
2462 | my $entry = $nethash->{$cidr}; | |
2463 | my $line = $entry->{nomatch} ? '!' : ''; | |
2464 | $line .= $entry->{cidr}; | |
2465 | $line .= " # " . encode('utf8', $entry->{comment}) | |
2466 | if $entry->{comment} && $entry->{comment} !~ m/^\s*$/; | |
2467 | $raw .= "$line\n"; | |
2468 | } | |
2469 | ||
2470 | return $raw; | |
2471 | }; | |
2472 | ||
2473 | sub save_vmfw_conf { | |
2474 | my ($vmid, $vmfw_conf) = @_; | |
2475 | ||
2476 | my $raw = ''; | |
2477 | ||
2478 | my $options = $vmfw_conf->{options}; | |
2479 | $raw .= &$format_options($options) if scalar(keys %$options); | |
2480 | ||
2481 | my $aliases = $vmfw_conf->{aliases}; | |
2482 | $raw .= &$format_aliases($aliases) if scalar(keys %$aliases); | |
2483 | ||
2484 | my $rules = $vmfw_conf->{rules} || []; | |
2485 | if (scalar(@$rules)) { | |
2486 | $raw .= "[RULES]\n\n"; | |
2487 | $raw .= &$format_rules($rules, 1); | |
2488 | $raw .= "\n"; | |
2489 | } | |
2490 | ||
2491 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
2492 | PVE::Tools::file_set_contents($filename, $raw); | |
2493 | } | |
2494 | ||
2495 | sub read_vm_firewall_configs { | |
2496 | my ($cluster_conf, $vmdata, $dir, $verbose) = @_; | |
2497 | ||
2498 | my $vmfw_configs = {}; | |
2499 | ||
2500 | foreach my $vmid (keys %{$vmdata->{qemu}}) { | |
2501 | my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm', $vmid, $dir, $verbose); | |
2502 | next if !$vmfw_conf->{options}; # skip if file does not exists | |
2503 | $vmfw_configs->{$vmid} = $vmfw_conf; | |
2504 | } | |
2505 | foreach my $vmid (keys %{$vmdata->{openvz}}) { | |
2506 | my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct', $vmid, $dir, $verbose); | |
2507 | next if !$vmfw_conf->{options}; # skip if file does not exists | |
2508 | $vmfw_configs->{$vmid} = $vmfw_conf; | |
2509 | } | |
2510 | ||
2511 | return $vmfw_configs; | |
2512 | } | |
2513 | ||
2514 | sub get_option_log_level { | |
2515 | my ($options, $k) = @_; | |
2516 | ||
2517 | my $v = $options->{$k}; | |
2518 | $v = $default_log_level if !defined($v); | |
2519 | ||
2520 | return undef if $v eq '' || $v eq 'nolog'; | |
2521 | ||
2522 | $v = $log_level_hash->{$v} if defined($log_level_hash->{$v}); | |
2523 | ||
2524 | return $v if ($v >= 0) && ($v <= 7); | |
2525 | ||
2526 | warn "unknown log level ($k = '$v')\n"; | |
2527 | ||
2528 | return undef; | |
2529 | } | |
2530 | ||
2531 | sub generate_std_chains { | |
2532 | my ($ruleset, $options) = @_; | |
2533 | ||
2534 | my $loglevel = get_option_log_level($options, 'smurf_log_level'); | |
2535 | ||
2536 | # same as shorewall smurflog. | |
2537 | my $chain = 'PVEFW-smurflog'; | |
2538 | $pve_std_chains->{$chain} = []; | |
2539 | ||
2540 | push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; | |
2541 | push @{$pve_std_chains->{$chain}}, "-j DROP"; | |
2542 | ||
2543 | # same as shorewall logflags action. | |
2544 | $loglevel = get_option_log_level($options, 'tcp_flags_log_level'); | |
2545 | $chain = 'PVEFW-logflags'; | |
2546 | $pve_std_chains->{$chain} = []; | |
2547 | ||
2548 | # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG) | |
2549 | push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; | |
2550 | push @{$pve_std_chains->{$chain}}, "-j DROP"; | |
2551 | ||
2552 | foreach my $chain (keys %$pve_std_chains) { | |
2553 | ruleset_create_chain($ruleset, $chain); | |
2554 | foreach my $rule (@{$pve_std_chains->{$chain}}) { | |
2555 | if (ref($rule)) { | |
2556 | ruleset_generate_rule($ruleset, $chain, $rule); | |
2557 | } else { | |
2558 | ruleset_addrule($ruleset, $chain, $rule); | |
2559 | } | |
2560 | } | |
2561 | } | |
2562 | } | |
2563 | ||
2564 | sub generate_ipset_chains { | |
2565 | my ($ipset_ruleset, $fw_conf) = @_; | |
2566 | ||
2567 | foreach my $ipset (keys %{$fw_conf->{ipset}}) { | |
2568 | generate_ipset($ipset_ruleset, "PVEFW-$ipset", $fw_conf->{ipset}->{$ipset}, $fw_conf->{aliases}); | |
2569 | } | |
2570 | } | |
2571 | ||
2572 | sub generate_ipset { | |
2573 | my ($ipset_ruleset, $name, $options, $aliases) = @_; | |
2574 | ||
2575 | my $hashsize = scalar(@$options); | |
2576 | if ($hashsize <= 64) { | |
2577 | $hashsize = 64; | |
2578 | } else { | |
2579 | $hashsize = round_powerof2($hashsize); | |
2580 | } | |
2581 | ||
2582 | push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize"; | |
2583 | ||
2584 | # remove duplicates | |
2585 | my $nethash = {}; | |
2586 | foreach my $entry (@$options) { | |
2587 | my $cidr = $entry->{cidr}; | |
2588 | if ($cidr =~ m/^${ip_alias_pattern}$/) { | |
2589 | my $alias = lc($cidr); | |
2590 | if ($aliases->{$alias}) { | |
2591 | $entry->{cidr} = $aliases->{$alias}->{cidr}; | |
2592 | $nethash->{$entry->{cidr}} = $entry; | |
2593 | } else { | |
2594 | warn "no such alias '$cidr'\n" if !$aliases->{$alias}; | |
2595 | } | |
2596 | } else { | |
2597 | $nethash->{$entry->{cidr}} = $entry; | |
2598 | } | |
2599 | } | |
2600 | ||
2601 | foreach my $cidr (sort keys %$nethash) { | |
2602 | my $entry = $nethash->{$cidr}; | |
2603 | ||
2604 | my $cmd = "add $name $cidr"; | |
2605 | if ($entry->{nomatch}) { | |
2606 | if ($feature_ipset_nomatch) { | |
2607 | push @{$ipset_ruleset->{$name}}, "$cmd nomatch"; | |
2608 | } else { | |
2609 | warn "ignore !$cidr - nomatch not supported by kernel\n"; | |
2610 | } | |
2611 | } else { | |
2612 | push @{$ipset_ruleset->{$name}}, $cmd; | |
2613 | } | |
2614 | } | |
2615 | } | |
2616 | ||
2617 | sub round_powerof2 { | |
2618 | my ($int) = @_; | |
2619 | ||
2620 | $int--; | |
2621 | $int |= $int >> $_ foreach (1,2,4,8,16); | |
2622 | return ++$int; | |
2623 | } | |
2624 | ||
2625 | sub load_clusterfw_conf { | |
2626 | my ($filename, $verbose) = @_; | |
2627 | ||
2628 | $filename = $clusterfw_conf_filename if !defined($filename); | |
2629 | ||
2630 | my $cluster_conf = {}; | |
2631 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
2632 | $cluster_conf = parse_cluster_fw_rules($filename, $fh, $verbose); | |
2633 | } | |
2634 | ||
2635 | return $cluster_conf; | |
2636 | } | |
2637 | ||
2638 | sub save_clusterfw_conf { | |
2639 | my ($cluster_conf) = @_; | |
2640 | ||
2641 | my $raw = ''; | |
2642 | ||
2643 | my $options = $cluster_conf->{options}; | |
2644 | $raw .= &$format_options($options) if scalar(keys %$options); | |
2645 | ||
2646 | my $aliases = $cluster_conf->{aliases}; | |
2647 | $raw .= &$format_aliases($aliases) if scalar(keys %$aliases); | |
2648 | ||
2649 | foreach my $ipset (sort keys %{$cluster_conf->{ipset}}) { | |
2650 | if (my $comment = $cluster_conf->{ipset_comments}->{$ipset}) { | |
2651 | my $utf8comment = encode('utf8', $comment); | |
2652 | $raw .= "[IPSET $ipset] # $utf8comment\n\n"; | |
2653 | } else { | |
2654 | $raw .= "[IPSET $ipset]\n\n"; | |
2655 | } | |
2656 | my $options = $cluster_conf->{ipset}->{$ipset}; | |
2657 | $raw .= &$format_ipset($options); | |
2658 | $raw .= "\n"; | |
2659 | } | |
2660 | ||
2661 | my $rules = $cluster_conf->{rules}; | |
2662 | if (scalar(@$rules)) { | |
2663 | $raw .= "[RULES]\n\n"; | |
2664 | $raw .= &$format_rules($rules, 1); | |
2665 | $raw .= "\n"; | |
2666 | } | |
2667 | ||
2668 | foreach my $group (sort keys %{$cluster_conf->{groups}}) { | |
2669 | my $rules = $cluster_conf->{groups}->{$group}; | |
2670 | if (my $comment = $cluster_conf->{group_comments}->{$group}) { | |
2671 | my $utf8comment = encode('utf8', $comment); | |
2672 | $raw .= "[group $group] # $utf8comment\n\n"; | |
2673 | } else { | |
2674 | $raw .= "[group $group]\n\n"; | |
2675 | } | |
2676 | ||
2677 | $raw .= &$format_rules($rules, 0); | |
2678 | $raw .= "\n"; | |
2679 | } | |
2680 | ||
2681 | PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw); | |
2682 | } | |
2683 | ||
2684 | sub load_hostfw_conf { | |
2685 | my ($cluster_conf, $filename, $verbose) = @_; | |
2686 | ||
2687 | $filename = $hostfw_conf_filename if !defined($filename); | |
2688 | ||
2689 | my $hostfw_conf = {}; | |
2690 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
2691 | $hostfw_conf = parse_host_fw_rules($filename, $fh, $cluster_conf, $verbose); | |
2692 | } | |
2693 | return $hostfw_conf; | |
2694 | } | |
2695 | ||
2696 | sub save_hostfw_conf { | |
2697 | my ($hostfw_conf) = @_; | |
2698 | ||
2699 | my $raw = ''; | |
2700 | ||
2701 | my $options = $hostfw_conf->{options}; | |
2702 | $raw .= &$format_options($options) if scalar(keys %$options); | |
2703 | ||
2704 | my $rules = $hostfw_conf->{rules}; | |
2705 | if (scalar(@$rules)) { | |
2706 | $raw .= "[RULES]\n\n"; | |
2707 | $raw .= &$format_rules($rules, 1); | |
2708 | $raw .= "\n"; | |
2709 | } | |
2710 | ||
2711 | PVE::Tools::file_set_contents($hostfw_conf_filename, $raw); | |
2712 | } | |
2713 | ||
2714 | sub compile { | |
2715 | my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_; | |
2716 | ||
2717 | my $vmfw_configs; | |
2718 | ||
2719 | if ($vmdata) { # test mode | |
2720 | my $testdir = $vmdata->{testdir} || die "no test directory specified"; | |
2721 | my $filename = "$testdir/cluster.fw"; | |
2722 | $cluster_conf = load_clusterfw_conf($filename, $verbose); | |
2723 | ||
2724 | $filename = "$testdir/host.fw"; | |
2725 | $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose); | |
2726 | ||
2727 | $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose); | |
2728 | } else { # normal operation | |
2729 | $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf; | |
2730 | ||
2731 | $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf; | |
2732 | ||
2733 | $vmdata = read_local_vm_config(); | |
2734 | $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose); | |
2735 | } | |
2736 | ||
2737 | $cluster_conf->{ipset}->{venet0} = []; | |
2738 | ||
2739 | my $localnet; | |
2740 | if ($cluster_conf->{aliases}->{local_network}) { | |
2741 | $localnet = $cluster_conf->{aliases}->{local_network}->{cidr}; | |
2742 | } else { | |
2743 | $localnet = local_network() || '127.0.0.0/8'; | |
2744 | $cluster_conf->{aliases}->{local_network} = { cidr => $localnet }; | |
2745 | } | |
2746 | ||
2747 | push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet }; | |
2748 | ||
2749 | my $ruleset = {}; | |
2750 | ||
2751 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); | |
2752 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
2753 | ||
2754 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
2755 | ||
2756 | my $hostfw_options = $hostfw_conf->{options} || {}; | |
2757 | ||
2758 | # fixme: what log level should we use here? | |
2759 | my $loglevel = get_option_log_level($hostfw_options, "log_level_out"); | |
2760 | ||
2761 | ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT"); | |
2762 | ||
2763 | ruleset_create_chain($ruleset, "PVEFW-VENET-OUT"); | |
2764 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set PVEFW-venet0 src -j PVEFW-VENET-OUT"); | |
2765 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set PVEFW-venet0 src -j PVEFW-VENET-OUT"); | |
2766 | ||
2767 | ruleset_create_chain($ruleset, "PVEFW-FWBR-IN"); | |
2768 | ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $hostfw_options, $cluster_conf, $loglevel); | |
2769 | ||
2770 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN"); | |
2771 | ||
2772 | ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT"); | |
2773 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT"); | |
2774 | ||
2775 | ruleset_create_chain($ruleset, "PVEFW-VENET-IN"); | |
2776 | ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $hostfw_options, $cluster_conf, $loglevel); | |
2777 | ||
2778 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set PVEFW-venet0 dst -j PVEFW-VENET-IN"); | |
2779 | ||
2780 | generate_std_chains($ruleset, $hostfw_options); | |
2781 | ||
2782 | my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0)); | |
2783 | ||
2784 | if ($hostfw_enable) { | |
2785 | eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf); }; | |
2786 | warn $@ if $@; # just to be sure - should not happen | |
2787 | } | |
2788 | ||
2789 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set PVEFW-venet0 dst -j PVEFW-VENET-IN"); | |
2790 | ||
2791 | # generate firewall rules for QEMU VMs | |
2792 | foreach my $vmid (keys %{$vmdata->{qemu}}) { | |
2793 | eval { | |
2794 | my $conf = $vmdata->{qemu}->{$vmid}; | |
2795 | my $vmfw_conf = $vmfw_configs->{$vmid}; | |
2796 | return if !$vmfw_conf; | |
2797 | return if !$vmfw_conf->{options}->{enable}; | |
2798 | ||
2799 | foreach my $netid (keys %$conf) { | |
2800 | next if $netid !~ m/^net(\d+)$/; | |
2801 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
2802 | next if !$net->{firewall}; | |
2803 | my $iface = "tap${vmid}i$1"; | |
2804 | ||
2805 | my $macaddr = $net->{macaddr}; | |
2806 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
2807 | $vmfw_conf, $vmid, 'IN'); | |
2808 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
2809 | $vmfw_conf, $vmid, 'OUT'); | |
2810 | } | |
2811 | }; | |
2812 | warn $@ if $@; # just to be sure - should not happen | |
2813 | } | |
2814 | ||
2815 | # generate firewall rules for OpenVZ containers | |
2816 | foreach my $vmid (keys %{$vmdata->{openvz}}) { | |
2817 | eval { | |
2818 | my $conf = $vmdata->{openvz}->{$vmid}; | |
2819 | ||
2820 | my $vmfw_conf = $vmfw_configs->{$vmid}; | |
2821 | return if !$vmfw_conf; | |
2822 | return if !$vmfw_conf->{options}->{enable}; | |
2823 | ||
2824 | if ($conf->{ip_address} && $conf->{ip_address}->{value}) { | |
2825 | my $ip = $conf->{ip_address}->{value}; | |
2826 | $ip =~ s/\s+/,/g; | |
2827 | parse_address_list($ip); # make sure we have a valid $ip list | |
2828 | ||
2829 | my @ips = split(',', $ip); | |
2830 | ||
2831 | foreach my $singleip (@ips) { | |
2832 | my $venet0ipset = {}; | |
2833 | $venet0ipset->{cidr} = $singleip; | |
2834 | push @{$cluster_conf->{ipset}->{venet0}}, $venet0ipset; | |
2835 | } | |
2836 | ||
2837 | generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'IN'); | |
2838 | generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'OUT'); | |
2839 | } | |
2840 | ||
2841 | if ($conf->{netif} && $conf->{netif}->{value}) { | |
2842 | my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value}); | |
2843 | foreach my $netid (keys %$netif) { | |
2844 | my $d = $netif->{$netid}; | |
2845 | ||
2846 | my $macaddr = $d->{mac}; | |
2847 | my $iface = $d->{host_ifname}; | |
2848 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
2849 | $vmfw_conf, $vmid, 'IN'); | |
2850 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
2851 | $vmfw_conf, $vmid, 'OUT'); | |
2852 | } | |
2853 | } | |
2854 | }; | |
2855 | warn $@ if $@; # just to be sure - should not happen | |
2856 | } | |
2857 | ||
2858 | if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){ | |
2859 | ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS"); | |
2860 | } | |
2861 | ||
2862 | my $ipset_ruleset = {}; | |
2863 | generate_ipset_chains($ipset_ruleset, $cluster_conf); | |
2864 | ||
2865 | return ($ruleset, $ipset_ruleset); | |
2866 | } | |
2867 | ||
2868 | sub get_ruleset_status { | |
2869 | my ($ruleset, $active_chains, $digest_fn, $verbose) = @_; | |
2870 | ||
2871 | my $statushash = {}; | |
2872 | ||
2873 | foreach my $chain (sort keys %$ruleset) { | |
2874 | my $sig = &$digest_fn($ruleset->{$chain}); | |
2875 | ||
2876 | $statushash->{$chain}->{sig} = $sig; | |
2877 | ||
2878 | my $oldsig = $active_chains->{$chain}; | |
2879 | if (!defined($oldsig)) { | |
2880 | $statushash->{$chain}->{action} = 'create'; | |
2881 | } else { | |
2882 | if ($oldsig eq $sig) { | |
2883 | $statushash->{$chain}->{action} = 'exists'; | |
2884 | } else { | |
2885 | $statushash->{$chain}->{action} = 'update'; | |
2886 | } | |
2887 | } | |
2888 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
2889 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
2890 | print "\t$cmd\n" if $verbose; | |
2891 | } | |
2892 | } | |
2893 | ||
2894 | foreach my $chain (sort keys %$active_chains) { | |
2895 | if (!defined($ruleset->{$chain})) { | |
2896 | my $sig = $active_chains->{$chain}; | |
2897 | $statushash->{$chain}->{action} = 'delete'; | |
2898 | $statushash->{$chain}->{sig} = $sig; | |
2899 | print "delete $chain ($sig)\n" if $verbose; | |
2900 | } | |
2901 | } | |
2902 | ||
2903 | return $statushash; | |
2904 | } | |
2905 | ||
2906 | sub print_sig_rule { | |
2907 | my ($chain, $sig) = @_; | |
2908 | ||
2909 | # We just use this to store a SHA1 checksum used to detect changes | |
2910 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
2911 | } | |
2912 | ||
2913 | sub get_ruleset_cmdlist { | |
2914 | my ($ruleset, $verbose) = @_; | |
2915 | ||
2916 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
2917 | ||
2918 | my ($active_chains, $hooks) = iptables_get_chains(); | |
2919 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose); | |
2920 | ||
2921 | # create missing chains first | |
2922 | foreach my $chain (sort keys %$ruleset) { | |
2923 | my $stat = $statushash->{$chain}; | |
2924 | die "internal error" if !$stat; | |
2925 | next if $stat->{action} ne 'create'; | |
2926 | ||
2927 | $cmdlist .= ":$chain - [0:0]\n"; | |
2928 | } | |
2929 | ||
2930 | foreach my $h (qw(INPUT OUTPUT FORWARD)) { | |
2931 | if (!$hooks->{$h}) { | |
2932 | $cmdlist .= "-A $h -j PVEFW-$h\n"; | |
2933 | } | |
2934 | } | |
2935 | ||
2936 | foreach my $chain (sort keys %$ruleset) { | |
2937 | my $stat = $statushash->{$chain}; | |
2938 | die "internal error" if !$stat; | |
2939 | ||
2940 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
2941 | $cmdlist .= "-F $chain\n"; | |
2942 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
2943 | $cmdlist .= "$cmd\n"; | |
2944 | } | |
2945 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
2946 | } elsif ($stat->{action} eq 'delete') { | |
2947 | die "internal error"; # this should not happen | |
2948 | } elsif ($stat->{action} eq 'exists') { | |
2949 | # do nothing | |
2950 | } else { | |
2951 | die "internal error - unknown status '$stat->{action}'"; | |
2952 | } | |
2953 | } | |
2954 | ||
2955 | foreach my $chain (keys %$statushash) { | |
2956 | next if $statushash->{$chain}->{action} ne 'delete'; | |
2957 | $cmdlist .= "-F $chain\n"; | |
2958 | } | |
2959 | foreach my $chain (keys %$statushash) { | |
2960 | next if $statushash->{$chain}->{action} ne 'delete'; | |
2961 | next if $chain eq 'PVEFW-INPUT'; | |
2962 | next if $chain eq 'PVEFW-OUTPUT'; | |
2963 | next if $chain eq 'PVEFW-FORWARD'; | |
2964 | $cmdlist .= "-X $chain\n"; | |
2965 | } | |
2966 | ||
2967 | my $changes = $cmdlist ne "*filter\n" ? 1 : 0; | |
2968 | ||
2969 | $cmdlist .= "COMMIT\n"; | |
2970 | ||
2971 | return wantarray ? ($cmdlist, $changes) : $cmdlist; | |
2972 | } | |
2973 | ||
2974 | sub get_ipset_cmdlist { | |
2975 | my ($ruleset, $verbose) = @_; | |
2976 | ||
2977 | my $cmdlist = ""; | |
2978 | ||
2979 | my $delete_cmdlist = ""; | |
2980 | ||
2981 | my $active_chains = ipset_get_chains(); | |
2982 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest, $verbose); | |
2983 | ||
2984 | # remove stale _swap chains | |
2985 | foreach my $chain (keys %$active_chains) { | |
2986 | if ($chain =~ m/^PVEFW-\S+_swap$/) { | |
2987 | $cmdlist .= "destroy $chain\n"; | |
2988 | } | |
2989 | } | |
2990 | ||
2991 | foreach my $chain (sort keys %$ruleset) { | |
2992 | my $stat = $statushash->{$chain}; | |
2993 | die "internal error" if !$stat; | |
2994 | ||
2995 | if ($stat->{action} eq 'create') { | |
2996 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
2997 | $cmdlist .= "$cmd\n"; | |
2998 | } | |
2999 | } | |
3000 | ||
3001 | if ($stat->{action} eq 'update') { | |
3002 | my $chain_swap = $chain."_swap"; | |
3003 | ||
3004 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
3005 | $cmd =~ s/$chain/$chain_swap/; | |
3006 | $cmdlist .= "$cmd\n"; | |
3007 | } | |
3008 | $cmdlist .= "swap $chain_swap $chain\n"; | |
3009 | $cmdlist .= "flush $chain_swap\n"; | |
3010 | $cmdlist .= "destroy $chain_swap\n"; | |
3011 | } | |
3012 | ||
3013 | } | |
3014 | ||
3015 | foreach my $chain (keys %$statushash) { | |
3016 | next if $statushash->{$chain}->{action} ne 'delete'; | |
3017 | ||
3018 | $delete_cmdlist .= "flush $chain\n"; | |
3019 | $delete_cmdlist .= "destroy $chain\n"; | |
3020 | } | |
3021 | ||
3022 | my $changes = ($cmdlist || $delete_cmdlist) ? 1 : 0; | |
3023 | ||
3024 | return ($cmdlist, $delete_cmdlist, $changes); | |
3025 | } | |
3026 | ||
3027 | sub apply_ruleset { | |
3028 | my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_; | |
3029 | ||
3030 | enable_bridge_firewall(); | |
3031 | ||
3032 | my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) = | |
3033 | get_ipset_cmdlist($ipset_ruleset, undef, $verbose); | |
3034 | ||
3035 | my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset, $verbose); | |
3036 | ||
3037 | if ($verbose) { | |
3038 | if ($ipset_changes) { | |
3039 | print "ipset changes:\n"; | |
3040 | print $ipset_create_cmdlist if $ipset_create_cmdlist; | |
3041 | print $ipset_delete_cmdlist if $ipset_delete_cmdlist; | |
3042 | } | |
3043 | ||
3044 | if ($changes) { | |
3045 | print "iptables changes:\n"; | |
3046 | print $cmdlist; | |
3047 | } | |
3048 | } | |
3049 | ||
3050 | ipset_restore_cmdlist($ipset_create_cmdlist); | |
3051 | ||
3052 | iptables_restore_cmdlist($cmdlist); | |
3053 | ||
3054 | ipset_restore_cmdlist($ipset_delete_cmdlist) if $ipset_delete_cmdlist; | |
3055 | ||
3056 | # test: re-read status and check if everything is up to date | |
3057 | my $active_chains = iptables_get_chains(); | |
3058 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0); | |
3059 | ||
3060 | my $errors; | |
3061 | foreach my $chain (sort keys %$ruleset) { | |
3062 | my $stat = $statushash->{$chain}; | |
3063 | if ($stat->{action} ne 'exists') { | |
3064 | warn "unable to update chain '$chain'\n"; | |
3065 | $errors = 1; | |
3066 | } | |
3067 | } | |
3068 | ||
3069 | die "unable to apply firewall changes\n" if $errors; | |
3070 | ||
3071 | update_nf_conntrack_max($hostfw_conf); | |
3072 | ||
3073 | update_nf_conntrack_tcp_timeout_established($hostfw_conf); | |
3074 | ||
3075 | } | |
3076 | ||
3077 | sub update_nf_conntrack_max { | |
3078 | my ($hostfw_conf) = @_; | |
3079 | ||
3080 | my $max = 65536; # reasonable default | |
3081 | ||
3082 | my $options = $hostfw_conf->{options} || {}; | |
3083 | ||
3084 | if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) { | |
3085 | $max = $options->{nf_conntrack_max}; | |
3086 | $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192 | |
3087 | } | |
3088 | ||
3089 | my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max"; | |
3090 | my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize"; | |
3091 | ||
3092 | my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max); | |
3093 | ||
3094 | if ($current != $max) { | |
3095 | my $hashsize = int($max/4); | |
3096 | PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize); | |
3097 | PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max); | |
3098 | } | |
3099 | } | |
3100 | ||
3101 | sub update_nf_conntrack_tcp_timeout_established { | |
3102 | my ($hostfw_conf) = @_; | |
3103 | ||
3104 | my $options = $hostfw_conf->{options} || {}; | |
3105 | ||
3106 | my $value = defined($options->{nf_conntrack_tcp_timeout_established}) ? $options->{nf_conntrack_tcp_timeout_established} : 432000; | |
3107 | ||
3108 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value); | |
3109 | } | |
3110 | ||
3111 | sub remove_pvefw_chains { | |
3112 | ||
3113 | my ($chash, $hooks) = iptables_get_chains(); | |
3114 | my $cmdlist = "*filter\n"; | |
3115 | ||
3116 | foreach my $h (qw(INPUT OUTPUT FORWARD)) { | |
3117 | if ($hooks->{$h}) { | |
3118 | $cmdlist .= "-D $h -j PVEFW-$h\n"; | |
3119 | } | |
3120 | } | |
3121 | ||
3122 | foreach my $chain (keys %$chash) { | |
3123 | $cmdlist .= "-F $chain\n"; | |
3124 | } | |
3125 | ||
3126 | foreach my $chain (keys %$chash) { | |
3127 | $cmdlist .= "-X $chain\n"; | |
3128 | } | |
3129 | $cmdlist .= "COMMIT\n"; | |
3130 | ||
3131 | iptables_restore_cmdlist($cmdlist); | |
3132 | } | |
3133 | ||
3134 | sub init { | |
3135 | my $cluster_conf = load_clusterfw_conf(); | |
3136 | my $cluster_options = $cluster_conf->{options}; | |
3137 | my $enable = $cluster_options->{enable}; | |
3138 | ||
3139 | return if !$enable; | |
3140 | ||
3141 | # load required modules here | |
3142 | } | |
3143 | ||
3144 | sub update { | |
3145 | my $code = sub { | |
3146 | ||
3147 | my $cluster_conf = load_clusterfw_conf(); | |
3148 | my $cluster_options = $cluster_conf->{options}; | |
3149 | ||
3150 | my $enable = $cluster_options->{enable}; | |
3151 | ||
3152 | die "Firewall is disabled - cannot start\n" if !$enable; | |
3153 | ||
3154 | if (!$enable) { | |
3155 | PVE::Firewall::remove_pvefw_chains(); | |
3156 | return; | |
3157 | } | |
3158 | ||
3159 | my $hostfw_conf = load_hostfw_conf(); | |
3160 | ||
3161 | my ($ruleset, $ipset_ruleset) = compile($cluster_conf, $hostfw_conf); | |
3162 | ||
3163 | apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset); | |
3164 | }; | |
3165 | ||
3166 | run_locked($code); | |
3167 | } | |
3168 | ||
3169 | 1; |