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