]>
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 | $pve_std_chains->{6} = { | |
576 | 'PVEFW-SET-ACCEPT-MARK' => [ | |
577 | "-j MARK --set-mark 1", | |
578 | ], | |
579 | 'PVEFW-DropBroadcast' => [ | |
580 | # same as shorewall 'Broadcast' | |
581 | # simply DROP BROADCAST/MULTICAST/ANYCAST | |
582 | # we can use this to reduce logging | |
583 | #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6 | |
584 | { action => 'DROP', dsttype => 'MULTICAST' }, | |
585 | { action => 'DROP', dsttype => 'ANYCAST' }, | |
586 | #{ action => 'DROP', dest => '224.0.0.0/4' }, | |
587 | ], | |
588 | 'PVEFW-reject' => [ | |
589 | # same as shorewall 'reject' | |
590 | #{ action => 'DROP', dsttype => 'BROADCAST' }, | |
591 | #{ action => 'DROP', source => '224.0.0.0/4' }, | |
592 | { action => 'DROP', proto => 'icmpv6' }, | |
593 | "-p tcp -j REJECT --reject-with tcp-reset", | |
594 | #"-p udp -j REJECT --reject-with icmp-port-unreachable", | |
595 | #"-p icmp -j REJECT --reject-with icmp-host-unreachable", | |
596 | #"-j REJECT --reject-with icmp-host-prohibited", | |
597 | ], | |
598 | 'PVEFW-Drop' => [ | |
599 | # same as shorewall 'Drop', which is equal to DROP, | |
600 | # but REJECT/DROP some packages to reduce logging, | |
601 | # and ACCEPT critical ICMP types | |
602 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
603 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
604 | { action => 'PVEFW-DropBroadcast' }, | |
605 | # ACCEPT critical ICMP types | |
606 | { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, | |
607 | { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, | |
608 | { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, | |
609 | ||
610 | # Drop packets with INVALID state | |
611 | "-m conntrack --ctstate INVALID -j DROP", | |
612 | # Drop Microsoft SMB noise | |
613 | { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
614 | { action => 'DROP', proto => 'udp', dport => '137:139'}, | |
615 | { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
616 | { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
617 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
618 | # Drop new/NotSyn traffic so that it doesn't get logged | |
619 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
620 | # Drop DNS replies | |
621 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
622 | ], | |
623 | 'PVEFW-Reject' => [ | |
624 | # same as shorewall 'Reject', which is equal to Reject, | |
625 | # but REJECT/DROP some packages to reduce logging, | |
626 | # and ACCEPT critical ICMP types | |
627 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
628 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
629 | { action => 'PVEFW-DropBroadcast' }, | |
630 | # ACCEPT critical ICMP types | |
631 | { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' }, | |
632 | { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' }, | |
633 | { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' }, | |
634 | ||
635 | # Drop packets with INVALID state | |
636 | "-m conntrack --ctstate INVALID -j DROP", | |
637 | # Drop Microsoft SMB noise | |
638 | { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
639 | { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, | |
640 | { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
641 | { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
642 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
643 | # Drop new/NotSyn traffic so that it doesn't get logged | |
644 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
645 | # Drop DNS replies | |
646 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
647 | ], | |
648 | 'PVEFW-tcpflags' => [ | |
649 | # same as shorewall tcpflags action. | |
650 | # Packets arriving on this interface are checked for som illegal combinations of TCP flags | |
651 | "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags", | |
652 | "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags", | |
653 | "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags", | |
654 | "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags", | |
655 | "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags", | |
656 | ], | |
657 | 'PVEFW-smurfs' => [ | |
658 | #does smurf attack works with ipv6, as broadcast not exist ??? | |
659 | ||
660 | # same as shorewall smurfs action | |
661 | # Filter packets for smurfs (packets with a broadcast address as the source). | |
662 | #"-s 0.0.0.0/32 -j RETURN", | |
663 | #"-m addrtype --src-type BROADCAST -g PVEFW-smurflog", | |
664 | #"-s 224.0.0.0/4 -g PVEFW-smurflog", | |
665 | ], | |
666 | }; | |
667 | ||
668 | # iptables -p icmp -h | |
669 | my $icmp_type_names = { | |
670 | any => 1, | |
671 | 'echo-reply' => 1, | |
672 | 'destination-unreachable' => 1, | |
673 | 'network-unreachable' => 1, | |
674 | 'host-unreachable' => 1, | |
675 | 'protocol-unreachable' => 1, | |
676 | 'port-unreachable' => 1, | |
677 | 'fragmentation-needed' => 1, | |
678 | 'source-route-failed' => 1, | |
679 | 'network-unknown' => 1, | |
680 | 'host-unknown' => 1, | |
681 | 'network-prohibited' => 1, | |
682 | 'host-prohibited' => 1, | |
683 | 'TOS-network-unreachable' => 1, | |
684 | 'TOS-host-unreachable' => 1, | |
685 | 'communication-prohibited' => 1, | |
686 | 'host-precedence-violation' => 1, | |
687 | 'precedence-cutoff' => 1, | |
688 | 'source-quench' => 1, | |
689 | 'redirect' => 1, | |
690 | 'network-redirect' => 1, | |
691 | 'host-redirect' => 1, | |
692 | 'TOS-network-redirect' => 1, | |
693 | 'TOS-host-redirect' => 1, | |
694 | 'echo-request' => 1, | |
695 | 'router-advertisement' => 1, | |
696 | 'router-solicitation' => 1, | |
697 | 'time-exceeded' => 1, | |
698 | 'ttl-zero-during-transit' => 1, | |
699 | 'ttl-zero-during-reassembly' => 1, | |
700 | 'parameter-problem' => 1, | |
701 | 'ip-header-bad' => 1, | |
702 | 'required-option-missing' => 1, | |
703 | 'timestamp-request' => 1, | |
704 | 'timestamp-reply' => 1, | |
705 | 'address-mask-request' => 1, | |
706 | 'address-mask-reply' => 1, | |
707 | }; | |
708 | ||
709 | # ip6tables -p icmpv6 -h | |
710 | ||
711 | my $icmpv6_type_names = { | |
712 | 'any' => 1, | |
713 | 'destination-unreachable' => 1, | |
714 | 'no-route' => 1, | |
715 | 'communication-prohibited' => 1, | |
716 | 'address-unreachable' => 1, | |
717 | 'port-unreachable' => 1, | |
718 | 'packet-too-big' => 1, | |
719 | 'time-exceeded' => 1, | |
720 | 'ttl-zero-during-transit' => 1, | |
721 | 'ttl-zero-during-reassembly' => 1, | |
722 | 'parameter-problem' => 1, | |
723 | 'bad-header' => 1, | |
724 | 'unknown-header-type' => 1, | |
725 | 'unknown-option' => 1, | |
726 | 'echo-request' => 1, | |
727 | 'echo-reply' => 1, | |
728 | 'router-solicitation' => 1, | |
729 | 'router-advertisement' => 1, | |
730 | 'neighbour-solicitation' => 1, | |
731 | 'neighbour-advertisement' => 1, | |
732 | 'redirect' => 1, | |
733 | }; | |
734 | ||
735 | sub init_firewall_macros { | |
736 | ||
737 | $pve_fw_parsed_macros = {}; | |
738 | ||
739 | foreach my $k (keys %$pve_fw_macros) { | |
740 | my $lc_name = lc($k); | |
741 | my $macro = $pve_fw_macros->{$k}; | |
742 | if (!ref($macro->[0])) { | |
743 | $pve_fw_macro_descr->{$k} = shift @$macro; | |
744 | } | |
745 | $pve_fw_preferred_macro_names->{$lc_name} = $k; | |
746 | $pve_fw_parsed_macros->{$k} = $macro; | |
747 | } | |
748 | } | |
749 | ||
750 | init_firewall_macros(); | |
751 | ||
752 | sub get_macros { | |
753 | return wantarray ? ($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros; | |
754 | } | |
755 | ||
756 | my $etc_services; | |
757 | ||
758 | sub get_etc_services { | |
759 | ||
760 | return $etc_services if $etc_services; | |
761 | ||
762 | my $filename = "/etc/services"; | |
763 | ||
764 | my $fh = IO::File->new($filename, O_RDONLY); | |
765 | if (!$fh) { | |
766 | warn "unable to read '$filename' - $!\n"; | |
767 | return {}; | |
768 | } | |
769 | ||
770 | my $services = {}; | |
771 | ||
772 | while (my $line = <$fh>) { | |
773 | chomp ($line); | |
774 | next if $line =~m/^#/; | |
775 | next if ($line =~m/^\s*$/); | |
776 | ||
777 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
778 | $services->{byid}->{$2}->{name} = $1; | |
779 | $services->{byid}->{$2}->{port} = $2; | |
780 | $services->{byid}->{$2}->{$3} = 1; | |
781 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
782 | } | |
783 | } | |
784 | ||
785 | close($fh); | |
786 | ||
787 | $etc_services = $services; | |
788 | ||
789 | ||
790 | return $etc_services; | |
791 | } | |
792 | ||
793 | my $etc_protocols; | |
794 | ||
795 | sub get_etc_protocols { | |
796 | return $etc_protocols if $etc_protocols; | |
797 | ||
798 | my $filename = "/etc/protocols"; | |
799 | ||
800 | my $fh = IO::File->new($filename, O_RDONLY); | |
801 | if (!$fh) { | |
802 | warn "unable to read '$filename' - $!\n"; | |
803 | return {}; | |
804 | } | |
805 | ||
806 | my $protocols = {}; | |
807 | ||
808 | while (my $line = <$fh>) { | |
809 | chomp ($line); | |
810 | next if $line =~m/^#/; | |
811 | next if ($line =~m/^\s*$/); | |
812 | ||
813 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
814 | $protocols->{byid}->{$2}->{name} = $1; | |
815 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
816 | } | |
817 | } | |
818 | ||
819 | close($fh); | |
820 | ||
821 | # add special case for ICMP v6 | |
822 | $protocols->{byid}->{icmpv6}->{name} = "icmpv6"; | |
823 | $protocols->{byname}->{icmpv6} = $protocols->{byid}->{icmpv6}; | |
824 | ||
825 | $etc_protocols = $protocols; | |
826 | ||
827 | return $etc_protocols; | |
828 | } | |
829 | ||
830 | my $ipv4_mask_hash_localnet = { | |
831 | '255.255.0.0' => 16, | |
832 | '255.255.128.0' => 17, | |
833 | '255.255.192.0' => 18, | |
834 | '255.255.224.0' => 19, | |
835 | '255.255.240.0' => 20, | |
836 | '255.255.248.0' => 21, | |
837 | '255.255.252.0' => 22, | |
838 | '255.255.254.0' => 23, | |
839 | '255.255.255.0' => 24, | |
840 | '255.255.255.128' => 25, | |
841 | '255.255.255.192' => 26, | |
842 | '255.255.255.224' => 27, | |
843 | '255.255.255.240' => 28, | |
844 | '255.255.255.248' => 29, | |
845 | '255.255.255.252' => 30, | |
846 | }; | |
847 | ||
848 | my $__local_network; | |
849 | ||
850 | sub local_network { | |
851 | my ($new_value) = @_; | |
852 | ||
853 | $__local_network = $new_value if defined($new_value); | |
854 | ||
855 | return $__local_network if defined($__local_network); | |
856 | ||
857 | eval { | |
858 | my $nodename = PVE::INotify::nodename(); | |
859 | ||
860 | my $ip = PVE::Cluster::remote_node_ip($nodename); | |
861 | ||
862 | my $testip = Net::IP->new($ip); | |
863 | ||
864 | my $routes = PVE::ProcFSTools::read_proc_net_route(); | |
865 | foreach my $entry (@$routes) { | |
866 | my $mask = $ipv4_mask_hash_localnet->{$entry->{mask}}; | |
867 | next if !defined($mask); | |
868 | return if $mask eq '0.0.0.0'; | |
869 | my $cidr = "$entry->{dest}/$mask"; | |
870 | my $testnet = Net::IP->new($cidr); | |
871 | if ($testnet->overlaps($testip)) { | |
872 | $__local_network = $cidr; | |
873 | return; | |
874 | } | |
875 | } | |
876 | }; | |
877 | warn $@ if $@; | |
878 | ||
879 | return $__local_network; | |
880 | } | |
881 | ||
882 | # ipset names are limited to 31 characters, | |
883 | # and we use '-v4' or '-v6' to indicate IP versions, | |
884 | # and we use '_swap' suffix for atomic update, | |
885 | # for example PVEFW-${VMID}-${ipset_name}_swap | |
886 | ||
887 | my $max_iptables_ipset_name_length = 31 - length("_swap") - length("-v4"); | |
888 | ||
889 | sub compute_ipset_chain_name { | |
890 | my ($vmid, $ipset_name) = @_; | |
891 | ||
892 | $vmid = 0 if !defined($vmid); | |
893 | ||
894 | my $id = "$vmid-${ipset_name}"; | |
895 | ||
896 | if ((length($id) + 6) > $max_iptables_ipset_name_length) { | |
897 | $id = PVE::Tools::fnv31a_hex($id); | |
898 | } | |
899 | ||
900 | return "PVEFW-$id"; | |
901 | } | |
902 | ||
903 | sub compute_ipfilter_ipset_name { | |
904 | my ($iface) = @_; | |
905 | ||
906 | return "ipfilter-$iface"; | |
907 | } | |
908 | ||
909 | sub parse_address_list { | |
910 | my ($str) = @_; | |
911 | ||
912 | if ($str =~ m/^(\+)(\S+)$/) { # ipset ref | |
913 | die "ipset name too long\n" if length($str) > ($max_ipset_name_length + 1); | |
914 | return; | |
915 | } | |
916 | ||
917 | if ($str =~ m/^${ip_alias_pattern}$/) { | |
918 | die "alias name too long\n" if length($str) > $max_alias_name_length; | |
919 | return; | |
920 | } | |
921 | ||
922 | my $count = 0; | |
923 | my $iprange = 0; | |
924 | my $ipversion; | |
925 | ||
926 | foreach my $elem (split(/,/, $str)) { | |
927 | $count++; | |
928 | my $ip = Net::IP->new($elem); | |
929 | if (!$ip) { | |
930 | my $err = Net::IP::Error(); | |
931 | die "invalid IP address: $err\n"; | |
932 | } | |
933 | $iprange = 1 if $elem =~ m/-/; | |
934 | ||
935 | my $new_ipversion = Net::IP::ip_is_ipv6($ip->ip()) ? 6 : 4; | |
936 | ||
937 | die "detected mixed ipv4/ipv6 addresses in address list '$str'\n" | |
938 | if $ipversion && ($new_ipversion != $ipversion); | |
939 | ||
940 | $ipversion = $new_ipversion; | |
941 | } | |
942 | ||
943 | die "you can't use a range in a list\n" if $iprange && $count > 1; | |
944 | ||
945 | return $ipversion; | |
946 | } | |
947 | ||
948 | sub parse_port_name_number_or_range { | |
949 | my ($str) = @_; | |
950 | ||
951 | my $services = PVE::Firewall::get_etc_services(); | |
952 | my $count = 0; | |
953 | my $icmp_port = 0; | |
954 | ||
955 | foreach my $item (split(/,/, $str)) { | |
956 | $count++; | |
957 | if ($item =~ m/^(\d+):(\d+)$/) { | |
958 | my ($port1, $port2) = ($1, $2); | |
959 | die "invalid port '$port1'\n" if $port1 > 65535; | |
960 | die "invalid port '$port2'\n" if $port2 > 65535; | |
961 | } elsif ($item =~ m/^(\d+)$/) { | |
962 | my $port = $1; | |
963 | die "invalid port '$port'\n" if $port > 65535; | |
964 | } else { | |
965 | if ($icmp_type_names->{$item}) { | |
966 | $icmp_port = 1; | |
967 | } elsif ($icmpv6_type_names->{$item}) { | |
968 | $icmp_port = 1; | |
969 | } else { | |
970 | die "invalid port '$item'\n" if !$services->{byname}->{$item}; | |
971 | } | |
972 | } | |
973 | } | |
974 | ||
975 | die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1; | |
976 | ||
977 | return $count; | |
978 | } | |
979 | ||
980 | PVE::JSONSchema::register_format('pve-fw-port-spec', \&pve_fw_verify_port_spec); | |
981 | sub pve_fw_verify_port_spec { | |
982 | my ($portstr) = @_; | |
983 | ||
984 | parse_port_name_number_or_range($portstr); | |
985 | ||
986 | return $portstr; | |
987 | } | |
988 | ||
989 | PVE::JSONSchema::register_format('pve-fw-addr-spec', \&pve_fw_verify_addr_spec); | |
990 | sub pve_fw_verify_addr_spec { | |
991 | my ($list) = @_; | |
992 | ||
993 | parse_address_list($list); | |
994 | ||
995 | return $list; | |
996 | } | |
997 | ||
998 | PVE::JSONSchema::register_format('pve-fw-protocol-spec', \&pve_fw_verify_protocol_spec); | |
999 | sub pve_fw_verify_protocol_spec { | |
1000 | my ($proto) = @_; | |
1001 | ||
1002 | my $protocols = get_etc_protocols(); | |
1003 | ||
1004 | die "unknown protocol '$proto'\n" if $proto && | |
1005 | !(defined($protocols->{byname}->{$proto}) || | |
1006 | defined($protocols->{byid}->{$proto})); | |
1007 | ||
1008 | return $proto; | |
1009 | } | |
1010 | ||
1011 | ||
1012 | # helper function for API | |
1013 | ||
1014 | sub copy_opject_with_digest { | |
1015 | my ($object) = @_; | |
1016 | ||
1017 | my $sha = Digest::SHA->new('sha1'); | |
1018 | ||
1019 | my $res = {}; | |
1020 | foreach my $k (sort keys %$object) { | |
1021 | my $v = $object->{$k}; | |
1022 | next if !defined($v); | |
1023 | $res->{$k} = $v; | |
1024 | $sha->add($k, ':', $v, "\n"); | |
1025 | } | |
1026 | ||
1027 | my $digest = $sha->hexdigest; | |
1028 | ||
1029 | $res->{digest} = $digest; | |
1030 | ||
1031 | return wantarray ? ($res, $digest) : $res; | |
1032 | } | |
1033 | ||
1034 | sub copy_list_with_digest { | |
1035 | my ($list) = @_; | |
1036 | ||
1037 | my $sha = Digest::SHA->new('sha1'); | |
1038 | ||
1039 | my $res = []; | |
1040 | foreach my $entry (@$list) { | |
1041 | my $data = {}; | |
1042 | foreach my $k (sort keys %$entry) { | |
1043 | my $v = $entry->{$k}; | |
1044 | next if !defined($v); | |
1045 | $data->{$k} = $v; | |
1046 | # Note: digest ignores refs ($rule->{errors}) | |
1047 | $sha->add($k, ':', $v, "\n") if !ref($v); ; | |
1048 | } | |
1049 | push @$res, $data; | |
1050 | } | |
1051 | ||
1052 | my $digest = $sha->hexdigest; | |
1053 | ||
1054 | foreach my $entry (@$res) { | |
1055 | $entry->{digest} = $digest; | |
1056 | } | |
1057 | ||
1058 | return wantarray ? ($res, $digest) : $res; | |
1059 | } | |
1060 | ||
1061 | my $rule_properties = { | |
1062 | pos => { | |
1063 | description => "Update rule at position <pos>.", | |
1064 | type => 'integer', | |
1065 | minimum => 0, | |
1066 | optional => 1, | |
1067 | }, | |
1068 | digest => get_standard_option('pve-config-digest'), | |
1069 | type => { | |
1070 | type => 'string', | |
1071 | optional => 1, | |
1072 | enum => ['in', 'out', 'group'], | |
1073 | }, | |
1074 | action => { | |
1075 | description => "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.", | |
1076 | type => 'string', | |
1077 | optional => 1, | |
1078 | pattern => $security_group_name_pattern, | |
1079 | maxLength => 20, | |
1080 | minLength => 2, | |
1081 | }, | |
1082 | macro => { | |
1083 | type => 'string', | |
1084 | optional => 1, | |
1085 | maxLength => 128, | |
1086 | }, | |
1087 | iface => get_standard_option('pve-iface', { optional => 1 }), | |
1088 | source => { | |
1089 | type => 'string', format => 'pve-fw-addr-spec', | |
1090 | optional => 1, | |
1091 | }, | |
1092 | dest => { | |
1093 | type => 'string', format => 'pve-fw-addr-spec', | |
1094 | optional => 1, | |
1095 | }, | |
1096 | proto => { | |
1097 | type => 'string', format => 'pve-fw-protocol-spec', | |
1098 | optional => 1, | |
1099 | }, | |
1100 | enable => { | |
1101 | type => 'boolean', | |
1102 | optional => 1, | |
1103 | }, | |
1104 | sport => { | |
1105 | type => 'string', format => 'pve-fw-port-spec', | |
1106 | optional => 1, | |
1107 | }, | |
1108 | dport => { | |
1109 | type => 'string', format => 'pve-fw-port-spec', | |
1110 | optional => 1, | |
1111 | }, | |
1112 | comment => { | |
1113 | type => 'string', | |
1114 | optional => 1, | |
1115 | }, | |
1116 | }; | |
1117 | ||
1118 | sub add_rule_properties { | |
1119 | my ($properties) = @_; | |
1120 | ||
1121 | foreach my $k (keys %$rule_properties) { | |
1122 | my $h = $rule_properties->{$k}; | |
1123 | # copy data, so that we can modify later without side effects | |
1124 | foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; } | |
1125 | } | |
1126 | ||
1127 | return $properties; | |
1128 | } | |
1129 | ||
1130 | sub delete_rule_properties { | |
1131 | my ($rule, $delete_str) = @_; | |
1132 | ||
1133 | foreach my $opt (PVE::Tools::split_list($delete_str)) { | |
1134 | raise_param_exc({ 'delete' => "no such property ('$opt')"}) | |
1135 | if !defined($rule_properties->{$opt}); | |
1136 | raise_param_exc({ 'delete' => "unable to delete required property '$opt'"}) | |
1137 | if $opt eq 'type' || $opt eq 'action'; | |
1138 | delete $rule->{$opt}; | |
1139 | } | |
1140 | ||
1141 | return $rule; | |
1142 | } | |
1143 | ||
1144 | my $apply_macro = sub { | |
1145 | my ($macro_name, $param, $verify) = @_; | |
1146 | ||
1147 | my $macro_rules = $pve_fw_parsed_macros->{$macro_name}; | |
1148 | die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen | |
1149 | ||
1150 | my $rules = []; | |
1151 | ||
1152 | foreach my $templ (@$macro_rules) { | |
1153 | my $rule = {}; | |
1154 | my $param_used = {}; | |
1155 | foreach my $k (keys %$templ) { | |
1156 | my $v = $templ->{$k}; | |
1157 | if ($v eq 'PARAM') { | |
1158 | $v = $param->{$k}; | |
1159 | $param_used->{$k} = 1; | |
1160 | } elsif ($v eq 'DEST') { | |
1161 | $v = $param->{dest}; | |
1162 | $param_used->{dest} = 1; | |
1163 | } elsif ($v eq 'SOURCE') { | |
1164 | $v = $param->{source}; | |
1165 | $param_used->{source} = 1; | |
1166 | } | |
1167 | ||
1168 | if (!defined($v)) { | |
1169 | my $msg = "missing parameter '$k' in macro '$macro_name'"; | |
1170 | raise_param_exc({ macro => $msg }) if $verify; | |
1171 | die "$msg\n"; | |
1172 | } | |
1173 | $rule->{$k} = $v; | |
1174 | } | |
1175 | foreach my $k (keys %$param) { | |
1176 | next if $k eq 'macro'; | |
1177 | next if !defined($param->{$k}); | |
1178 | next if $param_used->{$k}; | |
1179 | if (defined($rule->{$k})) { | |
1180 | if ($rule->{$k} ne $param->{$k}) { | |
1181 | my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')"; | |
1182 | raise_param_exc({ $k => $msg }) if $verify; | |
1183 | die "$msg\n"; | |
1184 | } | |
1185 | } else { | |
1186 | $rule->{$k} = $param->{$k}; | |
1187 | } | |
1188 | } | |
1189 | push @$rules, $rule; | |
1190 | } | |
1191 | ||
1192 | return $rules; | |
1193 | }; | |
1194 | ||
1195 | my $rule_env_iface_lookup = { | |
1196 | 'ct' => 1, | |
1197 | 'vm' => 1, | |
1198 | 'group' => 0, | |
1199 | 'cluster' => 1, | |
1200 | 'host' => 1, | |
1201 | }; | |
1202 | ||
1203 | sub verify_rule { | |
1204 | my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_; | |
1205 | ||
1206 | my $allow_groups = $rule_env eq 'group' ? 0 : 1; | |
1207 | ||
1208 | my $allow_iface = $rule_env_iface_lookup->{$rule_env}; | |
1209 | die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen | |
1210 | ||
1211 | my $errors = $rule->{errors} || {}; | |
1212 | ||
1213 | my $error_count = 0; | |
1214 | ||
1215 | my $add_error = sub { | |
1216 | my ($param, $msg) = @_; | |
1217 | chomp $msg; | |
1218 | raise_param_exc({ $param => $msg }) if !$noerr; | |
1219 | $error_count++; | |
1220 | $errors->{$param} = $msg if !$errors->{$param}; | |
1221 | }; | |
1222 | ||
1223 | my $check_ipset_or_alias_property = sub { | |
1224 | my ($name, $expected_ipversion) = @_; | |
1225 | ||
1226 | if (my $value = $rule->{$name}) { | |
1227 | if ($value =~ m/^\+/) { | |
1228 | if ($value =~ m/^\+(${ipset_name_pattern})$/) { | |
1229 | &$add_error($name, "no such ipset '$1'") | |
1230 | if !($cluster_conf->{ipset}->{$1} || ($fw_conf && $fw_conf->{ipset}->{$1})); | |
1231 | ||
1232 | } else { | |
1233 | &$add_error($name, "invalid ipset name '$value'"); | |
1234 | } | |
1235 | } elsif ($value =~ m/^${ip_alias_pattern}$/){ | |
1236 | my $alias = lc($value); | |
1237 | &$add_error($name, "no such alias '$value'") | |
1238 | if !($cluster_conf->{aliases}->{$alias} || ($fw_conf && $fw_conf->{aliases}->{$alias})); | |
1239 | ||
1240 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; | |
1241 | $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; | |
1242 | ||
1243 | die "detected mixed ipv4/ipv6 adresses in rule\n" | |
1244 | if $expected_ipversion && ($expected_ipversion != $e->{ipversion}); | |
1245 | } | |
1246 | } | |
1247 | }; | |
1248 | ||
1249 | my $type = $rule->{type}; | |
1250 | my $action = $rule->{action}; | |
1251 | ||
1252 | &$add_error('type', "missing property") if !$type; | |
1253 | &$add_error('action', "missing property") if !$action; | |
1254 | ||
1255 | if ($type) { | |
1256 | if ($type eq 'in' || $type eq 'out') { | |
1257 | &$add_error('action', "unknown action '$action'") | |
1258 | if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/); | |
1259 | } elsif ($type eq 'group') { | |
1260 | &$add_error('type', "security groups not allowed") | |
1261 | if !$allow_groups; | |
1262 | &$add_error('action', "invalid characters in security group name") | |
1263 | if $action && ($action !~ m/^${security_group_name_pattern}$/); | |
1264 | } else { | |
1265 | &$add_error('type', "unknown rule type '$type'"); | |
1266 | } | |
1267 | } | |
1268 | ||
1269 | if ($rule->{iface}) { | |
1270 | &$add_error('type', "parameter -i not allowed for this rule type") | |
1271 | if !$allow_iface; | |
1272 | eval { PVE::JSONSchema::pve_verify_iface($rule->{iface}); }; | |
1273 | &$add_error('iface', $@) if $@; | |
1274 | if ($rule_env eq 'vm') { | |
1275 | &$add_error('iface', "value does not match the regex pattern 'net\\d+'") | |
1276 | if $rule->{iface} !~ m/^net(\d+)$/; | |
1277 | } elsif ($rule_env eq 'ct') { | |
1278 | &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'") | |
1279 | if $rule->{iface} !~ m/^(venet|eth(\d+))$/; | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | if ($rule->{macro}) { | |
1284 | if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) { | |
1285 | $rule->{macro} = $preferred_name; | |
1286 | } else { | |
1287 | &$add_error('macro', "unknown macro '$rule->{macro}'"); | |
1288 | } | |
1289 | } | |
1290 | ||
1291 | my $ipversion; | |
1292 | my $set_ip_version = sub { | |
1293 | my $vers = shift; | |
1294 | if ($vers) { | |
1295 | die "detected mixed ipv4/ipv6 adresses in rule\n" | |
1296 | if $ipversion && ($vers != $ipversion); | |
1297 | $ipversion = $vers; | |
1298 | } | |
1299 | }; | |
1300 | ||
1301 | if ($rule->{proto}) { | |
1302 | eval { pve_fw_verify_protocol_spec($rule->{proto}); }; | |
1303 | &$add_error('proto', $@) if $@; | |
1304 | &$set_ip_version(4) if $rule->{proto} eq 'icmp'; | |
1305 | &$set_ip_version(6) if $rule->{proto} eq 'icmpv6'; | |
1306 | } | |
1307 | ||
1308 | if ($rule->{dport}) { | |
1309 | eval { parse_port_name_number_or_range($rule->{dport}); }; | |
1310 | &$add_error('dport', $@) if $@; | |
1311 | &$add_error('proto', "missing property - 'dport' requires this property") | |
1312 | if !$rule->{proto}; | |
1313 | } | |
1314 | ||
1315 | if ($rule->{sport}) { | |
1316 | eval { parse_port_name_number_or_range($rule->{sport}); }; | |
1317 | &$add_error('sport', $@) if $@; | |
1318 | &$add_error('proto', "missing property - 'sport' requires this property") | |
1319 | if !$rule->{proto}; | |
1320 | } | |
1321 | ||
1322 | if ($rule->{source}) { | |
1323 | eval { | |
1324 | my $source_ipversion = parse_address_list($rule->{source}); | |
1325 | &$set_ip_version($source_ipversion); | |
1326 | }; | |
1327 | &$add_error('source', $@) if $@; | |
1328 | &$check_ipset_or_alias_property('source', $ipversion); | |
1329 | } | |
1330 | ||
1331 | if ($rule->{dest}) { | |
1332 | eval { | |
1333 | my $dest_ipversion = parse_address_list($rule->{dest}); | |
1334 | &$set_ip_version($dest_ipversion); | |
1335 | }; | |
1336 | &$add_error('dest', $@) if $@; | |
1337 | &$check_ipset_or_alias_property('dest', $ipversion); | |
1338 | } | |
1339 | ||
1340 | if ($rule->{macro} && !$error_count) { | |
1341 | eval { &$apply_macro($rule->{macro}, $rule, 1); }; | |
1342 | if (my $err = $@) { | |
1343 | if (ref($err) eq "PVE::Exception" && $err->{errors}) { | |
1344 | my $eh = $err->{errors}; | |
1345 | foreach my $p (keys %$eh) { | |
1346 | &$add_error($p, $eh->{$p}); | |
1347 | } | |
1348 | } else { | |
1349 | &$add_error('macro', "$err"); | |
1350 | } | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | $rule->{errors} = $errors if $error_count; | |
1355 | $rule->{ipversion} = $ipversion if $ipversion; | |
1356 | ||
1357 | return $rule; | |
1358 | } | |
1359 | ||
1360 | sub copy_rule_data { | |
1361 | my ($rule, $param) = @_; | |
1362 | ||
1363 | foreach my $k (keys %$rule_properties) { | |
1364 | if (defined(my $v = $param->{$k})) { | |
1365 | if ($v eq '' || $v eq '-') { | |
1366 | delete $rule->{$k}; | |
1367 | } else { | |
1368 | $rule->{$k} = $v; | |
1369 | } | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | return $rule; | |
1374 | } | |
1375 | ||
1376 | sub rules_modify_permissions { | |
1377 | my ($rule_env) = @_; | |
1378 | ||
1379 | if ($rule_env eq 'host') { | |
1380 | return { | |
1381 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], | |
1382 | }; | |
1383 | } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') { | |
1384 | return { | |
1385 | check => ['perm', '/', [ 'Sys.Modify' ]], | |
1386 | }; | |
1387 | } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') { | |
1388 | return { | |
1389 | check => ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]], | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | return undef; | |
1394 | } | |
1395 | ||
1396 | sub rules_audit_permissions { | |
1397 | my ($rule_env) = @_; | |
1398 | ||
1399 | if ($rule_env eq 'host') { | |
1400 | return { | |
1401 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], | |
1402 | }; | |
1403 | } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') { | |
1404 | return { | |
1405 | check => ['perm', '/', [ 'Sys.Audit' ]], | |
1406 | }; | |
1407 | } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') { | |
1408 | return { | |
1409 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | return undef; | |
1414 | } | |
1415 | ||
1416 | # core functions | |
1417 | my $bridge_firewall_enabled = 0; | |
1418 | ||
1419 | sub enable_bridge_firewall { | |
1420 | ||
1421 | return if $bridge_firewall_enabled; # only once | |
1422 | ||
1423 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1"); | |
1424 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1"); | |
1425 | ||
1426 | # make sure syncookies are enabled (which is default on newer 3.X kernels anyways) | |
1427 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1"); | |
1428 | ||
1429 | $bridge_firewall_enabled = 1; | |
1430 | } | |
1431 | ||
1432 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; | |
1433 | ||
1434 | sub iptables_restore_cmdlist { | |
1435 | my ($cmdlist) = @_; | |
1436 | ||
1437 | run_command("/sbin/iptables-restore -n", input => $cmdlist); | |
1438 | } | |
1439 | ||
1440 | sub ipset_restore_cmdlist { | |
1441 | my ($cmdlist) = @_; | |
1442 | ||
1443 | run_command("/usr/sbin/ipset restore", input => $cmdlist); | |
1444 | } | |
1445 | ||
1446 | sub iptables_get_chains { | |
1447 | ||
1448 | my $res = {}; | |
1449 | ||
1450 | # check what chains we want to track | |
1451 | my $is_pvefw_chain = sub { | |
1452 | my $name = shift; | |
1453 | ||
1454 | return 1 if $name =~ m/^PVEFW-\S+$/; | |
1455 | ||
1456 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; | |
1457 | ||
1458 | return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable | |
1459 | ||
1460 | return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/; | |
1461 | ||
1462 | return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/; | |
1463 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; | |
1464 | ||
1465 | return undef; | |
1466 | }; | |
1467 | ||
1468 | my $table = ''; | |
1469 | ||
1470 | my $hooks = {}; | |
1471 | ||
1472 | my $parser = sub { | |
1473 | my $line = shift; | |
1474 | ||
1475 | return if $line =~ m/^#/; | |
1476 | return if $line =~ m/^\s*$/; | |
1477 | ||
1478 | if ($line =~ m/^\*(\S+)$/) { | |
1479 | $table = $1; | |
1480 | return; | |
1481 | } | |
1482 | ||
1483 | return if $table ne 'filter'; | |
1484 | ||
1485 | if ($line =~ m/^:(\S+)\s/) { | |
1486 | my $chain = $1; | |
1487 | return if !&$is_pvefw_chain($chain); | |
1488 | $res->{$chain} = "unknown"; | |
1489 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { | |
1490 | my ($chain, $sig) = ($1, $2); | |
1491 | return if !&$is_pvefw_chain($chain); | |
1492 | $res->{$chain} = $sig; | |
1493 | } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) { | |
1494 | $hooks->{$1} = 1; | |
1495 | } else { | |
1496 | # simply ignore the rest | |
1497 | return; | |
1498 | } | |
1499 | }; | |
1500 | ||
1501 | run_command("/sbin/iptables-save", outfunc => $parser); | |
1502 | ||
1503 | return wantarray ? ($res, $hooks) : $res; | |
1504 | } | |
1505 | ||
1506 | sub iptables_chain_digest { | |
1507 | my ($rules) = @_; | |
1508 | my $digest = Digest::SHA->new('sha1'); | |
1509 | foreach my $rule (@$rules) { # order is important | |
1510 | $digest->add($rule); | |
1511 | } | |
1512 | return $digest->b64digest; | |
1513 | } | |
1514 | ||
1515 | sub ipset_chain_digest { | |
1516 | my ($rules) = @_; | |
1517 | ||
1518 | my $digest = Digest::SHA->new('sha1'); | |
1519 | foreach my $rule (sort @$rules) { # note: sorted | |
1520 | $digest->add($rule); | |
1521 | } | |
1522 | return $digest->b64digest; | |
1523 | } | |
1524 | ||
1525 | sub ipset_get_chains { | |
1526 | ||
1527 | my $res = {}; | |
1528 | my $chains = {}; | |
1529 | ||
1530 | my $parser = sub { | |
1531 | my $line = shift; | |
1532 | ||
1533 | return if $line =~ m/^#/; | |
1534 | return if $line =~ m/^\s*$/; | |
1535 | if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) { | |
1536 | my $chain = $1; | |
1537 | $line =~ s/\s+$//; # delete trailing white space | |
1538 | push @{$chains->{$chain}}, $line; | |
1539 | } else { | |
1540 | # simply ignore the rest | |
1541 | return; | |
1542 | } | |
1543 | }; | |
1544 | ||
1545 | run_command("/usr/sbin/ipset save", outfunc => $parser); | |
1546 | ||
1547 | # compute digest for each chain | |
1548 | foreach my $chain (keys %$chains) { | |
1549 | $res->{$chain} = ipset_chain_digest($chains->{$chain}); | |
1550 | } | |
1551 | ||
1552 | return $res; | |
1553 | } | |
1554 | ||
1555 | sub ruleset_generate_cmdstr { | |
1556 | my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; | |
1557 | ||
1558 | return if defined($rule->{enable}) && !$rule->{enable}; | |
1559 | return if $rule->{errors}; | |
1560 | ||
1561 | die "unable to emit macro - internal error" if $rule->{macro}; # should not happen | |
1562 | ||
1563 | my $nbdport = defined($rule->{dport}) ? parse_port_name_number_or_range($rule->{dport}) : 0; | |
1564 | my $nbsport = defined($rule->{sport}) ? parse_port_name_number_or_range($rule->{sport}) : 0; | |
1565 | ||
1566 | my @cmd = (); | |
1567 | ||
1568 | push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in}; | |
1569 | push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out}; | |
1570 | ||
1571 | my $source = $rule->{source}; | |
1572 | my $dest = $rule->{dest}; | |
1573 | ||
1574 | if ($source) { | |
1575 | if ($source =~ m/^\+/) { | |
1576 | if ($source =~ m/^\+(${ipset_name_pattern})$/) { | |
1577 | my $name = $1; | |
1578 | if ($fw_conf && $fw_conf->{ipset}->{$name}) { | |
1579 | my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name); | |
1580 | push @cmd, "-m set --match-set ${ipset_chain} src"; | |
1581 | } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { | |
1582 | my $ipset_chain = compute_ipset_chain_name(0, $name); | |
1583 | push @cmd, "-m set --match-set ${ipset_chain} src"; | |
1584 | } else { | |
1585 | die "no such ipset '$name'\n"; | |
1586 | } | |
1587 | } else { | |
1588 | die "invalid security group name '$source'\n"; | |
1589 | } | |
1590 | } elsif ($source =~ m/^${ip_alias_pattern}$/){ | |
1591 | my $alias = lc($source); | |
1592 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; | |
1593 | $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; | |
1594 | die "no such alias '$source'\n" if !$e; | |
1595 | push @cmd, "-s $e->{cidr}"; | |
1596 | } elsif ($source =~ m/\-/){ | |
1597 | push @cmd, "-m iprange --src-range $source"; | |
1598 | } else { | |
1599 | push @cmd, "-s $source"; | |
1600 | } | |
1601 | } | |
1602 | ||
1603 | if ($dest) { | |
1604 | if ($dest =~ m/^\+/) { | |
1605 | if ($dest =~ m/^\+(${ipset_name_pattern})$/) { | |
1606 | my $name = $1; | |
1607 | if ($fw_conf && $fw_conf->{ipset}->{$name}) { | |
1608 | my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name); | |
1609 | push @cmd, "-m set --match-set ${ipset_chain} dst"; | |
1610 | } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { | |
1611 | my $ipset_chain = compute_ipset_chain_name(0, $name); | |
1612 | push @cmd, "-m set --match-set ${ipset_chain} dst"; | |
1613 | } else { | |
1614 | die "no such ipset '$name'\n"; | |
1615 | } | |
1616 | } else { | |
1617 | die "invalid security group name '$dest'\n"; | |
1618 | } | |
1619 | } elsif ($dest =~ m/^${ip_alias_pattern}$/){ | |
1620 | my $alias = lc($dest); | |
1621 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; | |
1622 | $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; | |
1623 | die "no such alias '$dest'\n" if !$e; | |
1624 | push @cmd, "-d $e->{cidr}"; | |
1625 | } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){ | |
1626 | push @cmd, "-m iprange --dst-range $dest"; | |
1627 | } else { | |
1628 | push @cmd, "-d $dest"; | |
1629 | } | |
1630 | } | |
1631 | ||
1632 | if ($rule->{proto}) { | |
1633 | push @cmd, "-p $rule->{proto}"; | |
1634 | ||
1635 | my $multiport = 0; | |
1636 | $multiport++ if $nbdport > 1; | |
1637 | $multiport++ if $nbsport > 1; | |
1638 | ||
1639 | push @cmd, "--match multiport" if $multiport; | |
1640 | ||
1641 | die "multiport: option '--sports' cannot be used together with '--dports'\n" | |
1642 | if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); | |
1643 | ||
1644 | if ($rule->{dport}) { | |
1645 | if ($rule->{proto} && $rule->{proto} eq 'icmp') { | |
1646 | # Note: we use dport to store --icmp-type | |
1647 | die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}}); | |
1648 | push @cmd, "-m icmp --icmp-type $rule->{dport}"; | |
1649 | } elsif ($rule->{proto} && $rule->{proto} eq 'icmpv6') { | |
1650 | # Note: we use dport to store --icmpv6-type | |
1651 | die "unknown icmpv6-type '$rule->{dport}'\n" if !defined($icmpv6_type_names->{$rule->{dport}}); | |
1652 | push @cmd, "-m icmpv6 --icmpv6-type $rule->{dport}"; | |
1653 | } else { | |
1654 | if ($nbdport > 1) { | |
1655 | if ($multiport == 2) { | |
1656 | push @cmd, "--ports $rule->{dport}"; | |
1657 | } else { | |
1658 | push @cmd, "--dports $rule->{dport}"; | |
1659 | } | |
1660 | } else { | |
1661 | push @cmd, "--dport $rule->{dport}"; | |
1662 | } | |
1663 | } | |
1664 | } | |
1665 | ||
1666 | if ($rule->{sport}) { | |
1667 | if ($nbsport > 1) { | |
1668 | push @cmd, "--sports $rule->{sport}" if $multiport != 2; | |
1669 | } else { | |
1670 | push @cmd, "--sport $rule->{sport}"; | |
1671 | } | |
1672 | } | |
1673 | } elsif ($rule->{dport} || $rule->{sport}) { | |
1674 | die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport}; | |
1675 | die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport}; | |
1676 | } | |
1677 | ||
1678 | push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; | |
1679 | ||
1680 | if (my $action = $rule->{action}) { | |
1681 | $action = $actions->{$action} if defined($actions->{$action}); | |
1682 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
1683 | push @cmd, $goto ? "-g $action" : "-j $action"; | |
1684 | } | |
1685 | ||
1686 | return scalar(@cmd) ? join(' ', @cmd) : undef; | |
1687 | } | |
1688 | ||
1689 | sub ruleset_generate_rule { | |
1690 | my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; | |
1691 | ||
1692 | my $rules; | |
1693 | ||
1694 | if ($rule->{macro}) { | |
1695 | $rules = &$apply_macro($rule->{macro}, $rule); | |
1696 | } else { | |
1697 | $rules = [ $rule ]; | |
1698 | } | |
1699 | ||
1700 | # update all or nothing | |
1701 | ||
1702 | my @cmds = (); | |
1703 | foreach my $tmp (@$rules) { | |
1704 | if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) { | |
1705 | push @cmds, $cmdstr; | |
1706 | } | |
1707 | } | |
1708 | ||
1709 | foreach my $cmdstr (@cmds) { | |
1710 | ruleset_addrule($ruleset, $chain, $cmdstr); | |
1711 | } | |
1712 | } | |
1713 | ||
1714 | sub ruleset_generate_rule_insert { | |
1715 | my ($ruleset, $chain, $rule, $actions, $goto) = @_; | |
1716 | ||
1717 | die "implement me" if $rule->{macro}; # not implemented, because not needed so far | |
1718 | ||
1719 | if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $rule, $actions, $goto)) { | |
1720 | ruleset_insertrule($ruleset, $chain, $cmdstr); | |
1721 | } | |
1722 | } | |
1723 | ||
1724 | sub ruleset_create_chain { | |
1725 | my ($ruleset, $chain) = @_; | |
1726 | ||
1727 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; | |
1728 | die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format | |
1729 | ||
1730 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; | |
1731 | ||
1732 | $ruleset->{$chain} = []; | |
1733 | } | |
1734 | ||
1735 | sub ruleset_chain_exist { | |
1736 | my ($ruleset, $chain) = @_; | |
1737 | ||
1738 | return $ruleset->{$chain} ? 1 : undef; | |
1739 | } | |
1740 | ||
1741 | sub ruleset_addrule { | |
1742 | my ($ruleset, $chain, $rule) = @_; | |
1743 | ||
1744 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; | |
1745 | ||
1746 | push @{$ruleset->{$chain}}, "-A $chain $rule"; | |
1747 | } | |
1748 | ||
1749 | sub ruleset_insertrule { | |
1750 | my ($ruleset, $chain, $rule) = @_; | |
1751 | ||
1752 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; | |
1753 | ||
1754 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; | |
1755 | } | |
1756 | ||
1757 | sub get_log_rule_base { | |
1758 | my ($chain, $vmid, $msg, $loglevel) = @_; | |
1759 | ||
1760 | die "internal error - no log level" if !defined($loglevel); | |
1761 | ||
1762 | $vmid = 0 if !defined($vmid); | |
1763 | ||
1764 | # Note: we use special format for prefix to pass further | |
1765 | # info to log daemon (VMID, LOGVELEL and CHAIN) | |
1766 | ||
1767 | return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\""; | |
1768 | } | |
1769 | ||
1770 | sub ruleset_addlog { | |
1771 | my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_; | |
1772 | ||
1773 | return if !defined($loglevel); | |
1774 | ||
1775 | my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel); | |
1776 | ||
1777 | $logrule = "$rule $logrule" if defined($rule); | |
1778 | ||
1779 | ruleset_addrule($ruleset, $chain, $logrule); | |
1780 | } | |
1781 | ||
1782 | sub ruleset_add_chain_policy { | |
1783 | my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_; | |
1784 | ||
1785 | if ($policy eq 'ACCEPT') { | |
1786 | ||
1787 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' }, | |
1788 | { ACCEPT => $accept_action}); | |
1789 | ||
1790 | } elsif ($policy eq 'DROP') { | |
1791 | ||
1792 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop"); | |
1793 | ||
1794 | ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); | |
1795 | ||
1796 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
1797 | } elsif ($policy eq 'REJECT') { | |
1798 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject"); | |
1799 | ||
1800 | ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); | |
1801 | ||
1802 | ruleset_addrule($ruleset, $chain, "-g PVEFW-reject"); | |
1803 | } else { | |
1804 | # should not happen | |
1805 | die "internal error: unknown policy '$policy'"; | |
1806 | } | |
1807 | } | |
1808 | ||
1809 | sub ruleset_chain_add_conn_filters { | |
1810 | my ($ruleset, $chain, $accept) = @_; | |
1811 | ||
1812 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); | |
1813 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept"); | |
1814 | } | |
1815 | ||
1816 | sub ruleset_chain_add_input_filters { | |
1817 | my ($ruleset, $chain, $options, $cluster_conf, $loglevel) = @_; | |
1818 | ||
1819 | if ($cluster_conf->{ipset}->{blacklist}){ | |
1820 | if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist")) { | |
1821 | ruleset_create_chain($ruleset, "PVEFW-blacklist"); | |
1822 | ruleset_addlog($ruleset, "PVEFW-blacklist", 0, "DROP: ", $loglevel) if $loglevel; | |
1823 | ruleset_addrule($ruleset, "PVEFW-blacklist", "-j DROP"); | |
1824 | } | |
1825 | my $ipset_chain = compute_ipset_chain_name(0, 'blacklist'); | |
1826 | ruleset_addrule($ruleset, $chain, "-m set --match-set ${ipset_chain} src -j PVEFW-blacklist"); | |
1827 | } | |
1828 | ||
1829 | if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) { | |
1830 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs"); | |
1831 | } | |
1832 | ||
1833 | if ($options->{tcpflags}) { | |
1834 | ruleset_addrule($ruleset, $chain, "-p tcp -j PVEFW-tcpflags"); | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | sub ruleset_create_vm_chain { | |
1839 | my ($ruleset, $chain, $options, $macaddr, $ipfilter_ipset, $direction) = @_; | |
1840 | ||
1841 | ruleset_create_chain($ruleset, $chain); | |
1842 | my $accept = generate_nfqueue($options); | |
1843 | ||
1844 | if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) { | |
1845 | if ($direction eq 'OUT') { | |
1846 | ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK', | |
1847 | proto => 'udp', sport => 68, dport => 67 }); | |
1848 | } else { | |
1849 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT', | |
1850 | proto => 'udp', sport => 67, dport => 68 }); | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | if ($direction eq 'OUT') { | |
1855 | if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) { | |
1856 | ruleset_addrule($ruleset, $chain, "-m mac ! --mac-source $macaddr -j DROP"); | |
1857 | } | |
1858 | if ($ipfilter_ipset) { | |
1859 | ruleset_addrule($ruleset, $chain, "-m set ! --match-set $ipfilter_ipset src -j DROP"); | |
1860 | } | |
1861 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | sub ruleset_add_group_rule { | |
1866 | my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_; | |
1867 | ||
1868 | my $group = $rule->{action}; | |
1869 | my $group_chain = "GROUP-$group-$direction"; | |
1870 | if(!ruleset_chain_exist($ruleset, $group_chain)){ | |
1871 | generate_group_rules($ruleset, $cluster_conf, $group, $ipversion); | |
1872 | } | |
1873 | ||
1874 | if ($direction eq 'OUT' && $rule->{iface_out}) { | |
1875 | ruleset_addrule($ruleset, $chain, "-o $rule->{iface_out} -j $group_chain"); | |
1876 | } elsif ($direction eq 'IN' && $rule->{iface_in}) { | |
1877 | ruleset_addrule($ruleset, $chain, "-i $rule->{iface_in} -j $group_chain"); | |
1878 | } else { | |
1879 | ruleset_addrule($ruleset, $chain, "-j $group_chain"); | |
1880 | } | |
1881 | ||
1882 | ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j $action"); | |
1883 | } | |
1884 | ||
1885 | sub ruleset_generate_vm_rules { | |
1886 | my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion) = @_; | |
1887 | ||
1888 | my $lc_direction = lc($direction); | |
1889 | ||
1890 | my $in_accept = generate_nfqueue($options); | |
1891 | ||
1892 | foreach my $rule (@$rules) { | |
1893 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
1894 | next if !$rule->{enable} || $rule->{errors}; | |
1895 | next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion); | |
1896 | ||
1897 | if ($rule->{type} eq 'group') { | |
1898 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction, | |
1899 | $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion); | |
1900 | } else { | |
1901 | next if $rule->{type} ne $lc_direction; | |
1902 | eval { | |
1903 | if ($direction eq 'OUT') { | |
1904 | ruleset_generate_rule($ruleset, $chain, $rule, | |
1905 | { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, | |
1906 | undef, $cluster_conf, $vmfw_conf); | |
1907 | } else { | |
1908 | ruleset_generate_rule($ruleset, $chain, $rule, | |
1909 | { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }, | |
1910 | undef, $cluster_conf, $vmfw_conf); | |
1911 | } | |
1912 | }; | |
1913 | warn $@ if $@; | |
1914 | } | |
1915 | } | |
1916 | } | |
1917 | ||
1918 | sub generate_nfqueue { | |
1919 | my ($options) = @_; | |
1920 | ||
1921 | if ($options->{ips}) { | |
1922 | my $action = "NFQUEUE"; | |
1923 | if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) { | |
1924 | if (defined($3) && defined($1)) { | |
1925 | $action .= " --queue-balance $1:$3"; | |
1926 | } elsif (defined($1)) { | |
1927 | $action .= " --queue-num $1"; | |
1928 | } | |
1929 | } | |
1930 | $action .= " --queue-bypass" if $feature_ipset_nomatch; #need kernel 3.10 | |
1931 | return $action; | |
1932 | } else { | |
1933 | return "ACCEPT"; | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | sub ruleset_generate_vm_ipsrules { | |
1938 | my ($ruleset, $options, $direction, $iface) = @_; | |
1939 | ||
1940 | if ($options->{ips} && $direction eq 'IN') { | |
1941 | my $nfqueue = generate_nfqueue($options); | |
1942 | ||
1943 | if (!ruleset_chain_exist($ruleset, "PVEFW-IPS")) { | |
1944 | ruleset_create_chain($ruleset, "PVEFW-IPS"); | |
1945 | } | |
1946 | ||
1947 | ruleset_addrule($ruleset, "PVEFW-IPS", "-m physdev --physdev-out $iface --physdev-is-bridged -j $nfqueue"); | |
1948 | } | |
1949 | } | |
1950 | ||
1951 | sub generate_venet_rules_direction { | |
1952 | my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction, $ipversion) = @_; | |
1953 | ||
1954 | my $lc_direction = lc($direction); | |
1955 | ||
1956 | my $rules = $vmfw_conf->{rules}; | |
1957 | ||
1958 | my $options = $vmfw_conf->{options}; | |
1959 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
1960 | ||
1961 | my $chain = "venet0-$vmid-$direction"; | |
1962 | ||
1963 | ruleset_create_vm_chain($ruleset, $chain, $options, undef, undef, $direction); | |
1964 | ||
1965 | ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction, undef, $ipversion); | |
1966 | ||
1967 | # implement policy | |
1968 | my $policy; | |
1969 | ||
1970 | if ($direction eq 'OUT') { | |
1971 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default | |
1972 | } else { | |
1973 | $policy = $options->{policy_in} || 'DROP'; # allow nothing by default | |
1974 | } | |
1975 | ||
1976 | my $accept = generate_nfqueue($options); | |
1977 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; | |
1978 | ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action); | |
1979 | ||
1980 | if ($direction eq 'OUT') { | |
1981 | ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT", { | |
1982 | action => $chain, | |
1983 | source => $ip, | |
1984 | iface_in => 'venet0'}); | |
1985 | } else { | |
1986 | ruleset_generate_rule($ruleset, "PVEFW-VENET-IN", { | |
1987 | action => $chain, | |
1988 | dest => $ip, | |
1989 | iface_out => 'venet0'}); | |
1990 | } | |
1991 | } | |
1992 | ||
1993 | sub generate_tap_rules_direction { | |
1994 | my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_; | |
1995 | ||
1996 | my $lc_direction = lc($direction); | |
1997 | ||
1998 | my $rules = $vmfw_conf->{rules}; | |
1999 | ||
2000 | my $options = $vmfw_conf->{options}; | |
2001 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
2002 | ||
2003 | my $tapchain = "$iface-$direction"; | |
2004 | ||
2005 | my $ipfilter_name = compute_ipfilter_ipset_name($netid); | |
2006 | my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name) | |
2007 | if $vmfw_conf->{ipset}->{$ipfilter_name}; | |
2008 | ||
2009 | # create chain with mac and ip filter | |
2010 | ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $ipfilter_ipset, $direction); | |
2011 | ||
2012 | if ($options->{enable}) { | |
2013 | ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion); | |
2014 | ||
2015 | ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface); | |
2016 | ||
2017 | # implement policy | |
2018 | my $policy; | |
2019 | ||
2020 | if ($direction eq 'OUT') { | |
2021 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default | |
2022 | } else { | |
2023 | $policy = $options->{policy_in} || 'DROP'; # allow nothing by default | |
2024 | } | |
2025 | ||
2026 | my $accept = generate_nfqueue($options); | |
2027 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; | |
2028 | ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action); | |
2029 | } else { | |
2030 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : 'ACCEPT'; | |
2031 | ruleset_add_chain_policy($ruleset, $tapchain, $vmid, 'ACCEPT', $loglevel, $accept_action); | |
2032 | } | |
2033 | ||
2034 | # plug the tap chain to bridge chain | |
2035 | if ($direction eq 'IN') { | |
2036 | ruleset_addrule($ruleset, "PVEFW-FWBR-IN", | |
2037 | "-m physdev --physdev-is-bridged --physdev-out $iface -j $tapchain"); | |
2038 | } else { | |
2039 | ruleset_addrule($ruleset, "PVEFW-FWBR-OUT", | |
2040 | "-m physdev --physdev-is-bridged --physdev-in $iface -j $tapchain"); | |
2041 | } | |
2042 | } | |
2043 | ||
2044 | sub enable_host_firewall { | |
2045 | my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_; | |
2046 | ||
2047 | my $options = $hostfw_conf->{options}; | |
2048 | my $cluster_options = $cluster_conf->{options}; | |
2049 | my $rules = $hostfw_conf->{rules}; | |
2050 | my $cluster_rules = $cluster_conf->{rules}; | |
2051 | ||
2052 | # host inbound firewall | |
2053 | my $chain = "PVEFW-HOST-IN"; | |
2054 | ruleset_create_chain($ruleset, $chain); | |
2055 | ||
2056 | my $loglevel = get_option_log_level($options, "log_level_in"); | |
2057 | ||
2058 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); | |
2059 | ||
2060 | ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); | |
2061 | ruleset_chain_add_input_filters($ruleset, $chain, $options, $cluster_conf, $loglevel); | |
2062 | ||
2063 | # we use RETURN because we need to check also tap rules | |
2064 | my $accept_action = 'RETURN'; | |
2065 | ||
2066 | ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast | |
2067 | ||
2068 | # add host rules first, so that cluster wide rules can be overwritten | |
2069 | foreach my $rule (@$rules, @$cluster_rules) { | |
2070 | next if !$rule->{enable} || $rule->{errors}; | |
2071 | ||
2072 | $rule->{iface_in} = $rule->{iface} if $rule->{iface}; | |
2073 | ||
2074 | eval { | |
2075 | if ($rule->{type} eq 'group') { | |
2076 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion); | |
2077 | } elsif ($rule->{type} eq 'in') { | |
2078 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, | |
2079 | undef, $cluster_conf, $hostfw_conf); | |
2080 | } | |
2081 | }; | |
2082 | warn $@ if $@; | |
2083 | delete $rule->{iface_in}; | |
2084 | } | |
2085 | ||
2086 | # allow standard traffic for management ipset (includes cluster network) | |
2087 | my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management"); | |
2088 | my $mngmntsrc = "-m set --match-set ${mngmnt_ipset_chain} src"; | |
2089 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 8006 -j $accept_action"); # PVE API | |
2090 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 5900:5999 -j $accept_action"); # PVE VNC Console | |
2091 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 3128 -j $accept_action"); # SPICE Proxy | |
2092 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 22 -j $accept_action"); # SSH | |
2093 | ||
2094 | my $localnet = local_network(); | |
2095 | ||
2096 | # corosync | |
2097 | if ($localnet) { | |
2098 | my $corosync_rule = "-p udp --dport 5404:5405 -j $accept_action"; | |
2099 | ruleset_addrule($ruleset, $chain, "-s $localnet -d $localnet $corosync_rule"); | |
2100 | ruleset_addrule($ruleset, $chain, "-s $localnet -m addrtype --dst-type MULTICAST $corosync_rule"); | |
2101 | } | |
2102 | ||
2103 | # implement input policy | |
2104 | my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default | |
2105 | ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action); | |
2106 | ||
2107 | # host outbound firewall | |
2108 | $chain = "PVEFW-HOST-OUT"; | |
2109 | ruleset_create_chain($ruleset, $chain); | |
2110 | ||
2111 | $loglevel = get_option_log_level($options, "log_level_out"); | |
2112 | ||
2113 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); | |
2114 | ||
2115 | ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); | |
2116 | ||
2117 | # we use RETURN because we may want to check other thigs later | |
2118 | $accept_action = 'RETURN'; | |
2119 | ||
2120 | ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast | |
2121 | ||
2122 | # add host rules first, so that cluster wide rules can be overwritten | |
2123 | foreach my $rule (@$rules, @$cluster_rules) { | |
2124 | next if !$rule->{enable} || $rule->{errors}; | |
2125 | ||
2126 | $rule->{iface_out} = $rule->{iface} if $rule->{iface}; | |
2127 | eval { | |
2128 | if ($rule->{type} eq 'group') { | |
2129 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion); | |
2130 | } elsif ($rule->{type} eq 'out') { | |
2131 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, | |
2132 | undef, $cluster_conf, $hostfw_conf); | |
2133 | } | |
2134 | }; | |
2135 | warn $@ if $@; | |
2136 | delete $rule->{iface_out}; | |
2137 | } | |
2138 | ||
2139 | # allow standard traffic on cluster network | |
2140 | if ($localnet) { | |
2141 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 8006 -j $accept_action"); # PVE API | |
2142 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 22 -j $accept_action"); # SSH | |
2143 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 5900:5999 -j $accept_action"); # PVE VNC Console | |
2144 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 3128 -j $accept_action"); # SPICE Proxy | |
2145 | ||
2146 | my $corosync_rule = "-p udp --dport 5404:5405 -j $accept_action"; | |
2147 | ruleset_addrule($ruleset, $chain, "-d $localnet $corosync_rule"); | |
2148 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST $corosync_rule"); | |
2149 | } | |
2150 | ||
2151 | # implement output policy | |
2152 | $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default | |
2153 | ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action); | |
2154 | ||
2155 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); | |
2156 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
2157 | } | |
2158 | ||
2159 | sub generate_group_rules { | |
2160 | my ($ruleset, $cluster_conf, $group, $ipversion) = @_; | |
2161 | ||
2162 | my $rules = $cluster_conf->{groups}->{$group}; | |
2163 | ||
2164 | if (!$rules) { | |
2165 | warn "no such security group '$group'\n"; | |
2166 | $rules = []; # create empty chain | |
2167 | } | |
2168 | ||
2169 | my $chain = "GROUP-${group}-IN"; | |
2170 | ||
2171 | ruleset_create_chain($ruleset, $chain); | |
2172 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
2173 | ||
2174 | foreach my $rule (@$rules) { | |
2175 | next if $rule->{type} ne 'in'; | |
2176 | next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion; | |
2177 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, undef, $cluster_conf); | |
2178 | } | |
2179 | ||
2180 | $chain = "GROUP-${group}-OUT"; | |
2181 | ||
2182 | ruleset_create_chain($ruleset, $chain); | |
2183 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
2184 | ||
2185 | foreach my $rule (@$rules) { | |
2186 | next if $rule->{type} ne 'out'; | |
2187 | next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion; | |
2188 | # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to | |
2189 | # check also other tap rules later | |
2190 | ruleset_generate_rule($ruleset, $chain, $rule, | |
2191 | { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }, undef, $cluster_conf); | |
2192 | } | |
2193 | } | |
2194 | ||
2195 | my $MAX_NETS = 32; | |
2196 | my $valid_netdev_names = {}; | |
2197 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
2198 | $valid_netdev_names->{"net$i"} = 1; | |
2199 | } | |
2200 | ||
2201 | sub parse_fw_rule { | |
2202 | my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_; | |
2203 | ||
2204 | my $orig_line = $line; | |
2205 | ||
2206 | my $rule = {}; | |
2207 | ||
2208 | # we can add single line comments to the end of the rule | |
2209 | if ($line =~ s/#\s*(.*?)\s*$//) { | |
2210 | $rule->{comment} = decode('utf8', $1); | |
2211 | } | |
2212 | ||
2213 | # we can disable a rule when prefixed with '|' | |
2214 | ||
2215 | $rule->{enable} = $line =~ s/^\|// ? 0 : 1; | |
2216 | ||
2217 | $line =~ s/^(\S+)\s+(\S+)\s*// || | |
2218 | die "unable to parse rule: $line\n"; | |
2219 | ||
2220 | $rule->{type} = lc($1); | |
2221 | $rule->{action} = $2; | |
2222 | ||
2223 | if ($rule->{type} eq 'in' || $rule->{type} eq 'out') { | |
2224 | if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
2225 | $rule->{macro} = $1; | |
2226 | $rule->{action} = $2; | |
2227 | } | |
2228 | } | |
2229 | ||
2230 | while (length($line)) { | |
2231 | if ($line =~ s/^-i (\S+)\s*//) { | |
2232 | $rule->{iface} = $1; | |
2233 | next; | |
2234 | } | |
2235 | ||
2236 | last if $rule->{type} eq 'group'; | |
2237 | ||
2238 | if ($line =~ s/^-p (\S+)\s*//) { | |
2239 | $rule->{proto} = $1; | |
2240 | next; | |
2241 | } | |
2242 | ||
2243 | if ($line =~ s/^-dport (\S+)\s*//) { | |
2244 | $rule->{dport} = $1; | |
2245 | next; | |
2246 | } | |
2247 | ||
2248 | if ($line =~ s/^-sport (\S+)\s*//) { | |
2249 | $rule->{sport} = $1; | |
2250 | next; | |
2251 | } | |
2252 | if ($line =~ s/^-source (\S+)\s*//) { | |
2253 | $rule->{source} = $1; | |
2254 | next; | |
2255 | } | |
2256 | if ($line =~ s/^-dest (\S+)\s*//) { | |
2257 | $rule->{dest} = $1; | |
2258 | next; | |
2259 | } | |
2260 | ||
2261 | last; | |
2262 | } | |
2263 | ||
2264 | die "unable to parse rule parameters: $line\n" if length($line); | |
2265 | ||
2266 | $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1); | |
2267 | if ($verbose && $rule->{errors}) { | |
2268 | warn "$prefix - errors in rule parameters: $orig_line\n"; | |
2269 | foreach my $p (keys %{$rule->{errors}}) { | |
2270 | warn " $p: $rule->{errors}->{$p}\n"; | |
2271 | } | |
2272 | } | |
2273 | ||
2274 | return $rule; | |
2275 | } | |
2276 | ||
2277 | sub parse_vmfw_option { | |
2278 | my ($line) = @_; | |
2279 | ||
2280 | my ($opt, $value); | |
2281 | ||
2282 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
2283 | ||
2284 | if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) { | |
2285 | $opt = lc($1); | |
2286 | $value = int($2); | |
2287 | } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) { | |
2288 | $opt = lc($1); | |
2289 | $value = $2 ? lc($3) : ''; | |
2290 | } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
2291 | $opt = lc($1); | |
2292 | $value = uc($3); | |
2293 | } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) { | |
2294 | $opt = lc($1); | |
2295 | $value = $2; | |
2296 | } else { | |
2297 | die "can't parse option '$line'\n" | |
2298 | } | |
2299 | ||
2300 | return ($opt, $value); | |
2301 | } | |
2302 | ||
2303 | sub parse_hostfw_option { | |
2304 | my ($line) = @_; | |
2305 | ||
2306 | my ($opt, $value); | |
2307 | ||
2308 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
2309 | ||
2310 | if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) { | |
2311 | $opt = lc($1); | |
2312 | $value = int($2); | |
2313 | } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) { | |
2314 | $opt = lc($1); | |
2315 | $value = $2 ? lc($3) : ''; | |
2316 | } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) { | |
2317 | $opt = lc($1); | |
2318 | $value = int($2); | |
2319 | } else { | |
2320 | die "can't parse option '$line'\n" | |
2321 | } | |
2322 | ||
2323 | return ($opt, $value); | |
2324 | } | |
2325 | ||
2326 | sub parse_clusterfw_option { | |
2327 | my ($line) = @_; | |
2328 | ||
2329 | my ($opt, $value); | |
2330 | ||
2331 | if ($line =~ m/^(enable):\s*(0|1)\s*$/i) { | |
2332 | $opt = lc($1); | |
2333 | $value = int($2); | |
2334 | } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
2335 | $opt = lc($1); | |
2336 | $value = uc($3); | |
2337 | } else { | |
2338 | die "can't parse option '$line'\n" | |
2339 | } | |
2340 | ||
2341 | return ($opt, $value); | |
2342 | } | |
2343 | ||
2344 | sub resolve_alias { | |
2345 | my ($clusterfw_conf, $fw_conf, $cidr) = @_; | |
2346 | ||
2347 | my $alias = lc($cidr); | |
2348 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; | |
2349 | $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf; | |
2350 | ||
2351 | die "no such alias '$cidr'\n" if !$e;; | |
2352 | ||
2353 | return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr}; | |
2354 | } | |
2355 | ||
2356 | sub parse_ip_or_cidr { | |
2357 | my ($cidr) = @_; | |
2358 | ||
2359 | my $ipversion; | |
2360 | ||
2361 | if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) { | |
2362 | $cidr =~ s|/128$||; | |
2363 | $ipversion = 6; | |
2364 | } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) { | |
2365 | $cidr =~ s|/32$||; | |
2366 | $ipversion = 4; | |
2367 | } else { | |
2368 | die "value does not look like a valid IP address or CIDR network\n"; | |
2369 | } | |
2370 | ||
2371 | return wantarray ? ($cidr, $ipversion) : $cidr; | |
2372 | } | |
2373 | ||
2374 | sub parse_alias { | |
2375 | my ($line) = @_; | |
2376 | ||
2377 | # we can add single line comments to the end of the line | |
2378 | my $comment = decode('utf8', $1) if $line =~ s/\s*#\s*(.*?)\s*$//; | |
2379 | ||
2380 | if ($line =~ m/^(\S+)\s(\S+)$/) { | |
2381 | my ($name, $cidr) = ($1, $2); | |
2382 | my $ipversion; | |
2383 | ||
2384 | ($cidr, $ipversion) = parse_ip_or_cidr($cidr); | |
2385 | ||
2386 | my $data = { | |
2387 | name => $name, | |
2388 | cidr => $cidr, | |
2389 | ipversion => $ipversion, | |
2390 | }; | |
2391 | $data->{comment} = $comment if $comment; | |
2392 | return $data; | |
2393 | } | |
2394 | ||
2395 | return undef; | |
2396 | } | |
2397 | ||
2398 | sub generic_fw_config_parser { | |
2399 | my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_; | |
2400 | ||
2401 | my $section; | |
2402 | my $group; | |
2403 | ||
2404 | my $res = $empty_conf; | |
2405 | ||
2406 | while (defined(my $line = <$fh>)) { | |
2407 | next if $line =~ m/^#/; | |
2408 | next if $line =~ m/^\s*$/; | |
2409 | ||
2410 | chomp $line; | |
2411 | ||
2412 | my $linenr = $fh->input_line_number(); | |
2413 | my $prefix = "$filename (line $linenr)"; | |
2414 | ||
2415 | if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) { | |
2416 | $section = 'options'; | |
2417 | next; | |
2418 | } | |
2419 | ||
2420 | if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) { | |
2421 | $section = 'aliases'; | |
2422 | next; | |
2423 | } | |
2424 | ||
2425 | if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) { | |
2426 | $section = 'groups'; | |
2427 | $group = lc($1); | |
2428 | my $comment = $2; | |
2429 | eval { | |
2430 | die "security group name too long\n" if length($group) > $max_group_name_length; | |
2431 | die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/; | |
2432 | }; | |
2433 | if (my $err = $@) { | |
2434 | ($section, $group, $comment) = undef; | |
2435 | warn "$prefix: $err"; | |
2436 | next; | |
2437 | } | |
2438 | ||
2439 | $res->{$section}->{$group} = []; | |
2440 | $res->{group_comments}->{$group} = decode('utf8', $comment) | |
2441 | if $comment; | |
2442 | next; | |
2443 | } | |
2444 | ||
2445 | if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) { | |
2446 | $section = 'rules'; | |
2447 | next; | |
2448 | } | |
2449 | ||
2450 | if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) { | |
2451 | $section = 'ipset'; | |
2452 | $group = lc($1); | |
2453 | my $comment = $2; | |
2454 | eval { | |
2455 | die "ipset name too long\n" if length($group) > $max_ipset_name_length; | |
2456 | die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/; | |
2457 | }; | |
2458 | if (my $err = $@) { | |
2459 | ($section, $group, $comment) = undef; | |
2460 | warn "$prefix: $err"; | |
2461 | next; | |
2462 | } | |
2463 | ||
2464 | $res->{$section}->{$group} = []; | |
2465 | $res->{ipset_comments}->{$group} = decode('utf8', $comment) | |
2466 | if $comment; | |
2467 | next; | |
2468 | } | |
2469 | ||
2470 | if (!$section) { | |
2471 | warn "$prefix: skip line - no section\n"; | |
2472 | next; | |
2473 | } | |
2474 | ||
2475 | if ($section eq 'options') { | |
2476 | eval { | |
2477 | my ($opt, $value); | |
2478 | if ($rule_env eq 'cluster') { | |
2479 | ($opt, $value) = parse_clusterfw_option($line); | |
2480 | } elsif ($rule_env eq 'host') { | |
2481 | ($opt, $value) = parse_hostfw_option($line); | |
2482 | } else { | |
2483 | ($opt, $value) = parse_vmfw_option($line); | |
2484 | } | |
2485 | $res->{options}->{$opt} = $value; | |
2486 | }; | |
2487 | warn "$prefix: $@" if $@; | |
2488 | } elsif ($section eq 'aliases') { | |
2489 | eval { | |
2490 | my $data = parse_alias($line); | |
2491 | $res->{aliases}->{lc($data->{name})} = $data; | |
2492 | }; | |
2493 | warn "$prefix: $@" if $@; | |
2494 | } elsif ($section eq 'rules') { | |
2495 | my $rule; | |
2496 | eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); }; | |
2497 | if (my $err = $@) { | |
2498 | warn "$prefix: $err"; | |
2499 | next; | |
2500 | } | |
2501 | push @{$res->{$section}}, $rule; | |
2502 | } elsif ($section eq 'groups') { | |
2503 | my $rule; | |
2504 | eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group', $verbose); }; | |
2505 | if (my $err = $@) { | |
2506 | warn "$prefix: $err"; | |
2507 | next; | |
2508 | } | |
2509 | push @{$res->{$section}->{$group}}, $rule; | |
2510 | } elsif ($section eq 'ipset') { | |
2511 | # we can add single line comments to the end of the rule | |
2512 | my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//; | |
2513 | ||
2514 | $line =~ m/^(\!)?\s*(\S+)\s*$/; | |
2515 | my $nomatch = $1; | |
2516 | my $cidr = $2; | |
2517 | my $errors; | |
2518 | ||
2519 | if ($nomatch && !$feature_ipset_nomatch) { | |
2520 | $errors->{nomatch} = "nomatch not supported by kernel"; | |
2521 | } | |
2522 | ||
2523 | eval { | |
2524 | if ($cidr =~ m/^${ip_alias_pattern}$/) { | |
2525 | resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists | |
2526 | } else { | |
2527 | $cidr = parse_ip_or_cidr($cidr); | |
2528 | } | |
2529 | }; | |
2530 | if (my $err = $@) { | |
2531 | chomp $err; | |
2532 | $errors->{cidr} = $err; | |
2533 | } | |
2534 | ||
2535 | my $entry = { cidr => $cidr }; | |
2536 | $entry->{nomatch} = 1 if $nomatch; | |
2537 | $entry->{comment} = $comment if $comment; | |
2538 | $entry->{errors} = $errors if $errors; | |
2539 | ||
2540 | if ($verbose && $errors) { | |
2541 | warn "$prefix - errors in ipset '$group': $line\n"; | |
2542 | foreach my $p (keys %{$errors}) { | |
2543 | warn " $p: $errors->{$p}\n"; | |
2544 | } | |
2545 | } | |
2546 | ||
2547 | push @{$res->{$section}->{$group}}, $entry; | |
2548 | } else { | |
2549 | warn "$prefix: skip line - unknown section\n"; | |
2550 | next; | |
2551 | } | |
2552 | } | |
2553 | ||
2554 | return $res; | |
2555 | } | |
2556 | ||
2557 | sub parse_hostfw_config { | |
2558 | my ($filename, $fh, $cluster_conf, $verbose) = @_; | |
2559 | ||
2560 | my $empty_conf = { rules => [], options => {}}; | |
2561 | ||
2562 | return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host'); | |
2563 | } | |
2564 | ||
2565 | sub parse_vmfw_config { | |
2566 | my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_; | |
2567 | ||
2568 | my $empty_conf = { | |
2569 | rules => [], | |
2570 | options => {}, | |
2571 | aliases => {}, | |
2572 | ipset => {} , | |
2573 | ipset_comments => {}, | |
2574 | }; | |
2575 | ||
2576 | return generic_fw_config_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env); | |
2577 | } | |
2578 | ||
2579 | sub parse_clusterfw_config { | |
2580 | my ($filename, $fh, $verbose) = @_; | |
2581 | ||
2582 | my $section; | |
2583 | my $group; | |
2584 | ||
2585 | my $empty_conf = { | |
2586 | rules => [], | |
2587 | options => {}, | |
2588 | aliases => {}, | |
2589 | groups => {}, | |
2590 | group_comments => {}, | |
2591 | ipset => {} , | |
2592 | ipset_comments => {}, | |
2593 | }; | |
2594 | ||
2595 | return generic_fw_config_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster'); | |
2596 | } | |
2597 | ||
2598 | sub run_locked { | |
2599 | my ($code, @param) = @_; | |
2600 | ||
2601 | my $timeout = 10; | |
2602 | ||
2603 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
2604 | ||
2605 | die $@ if $@; | |
2606 | ||
2607 | return $res; | |
2608 | } | |
2609 | ||
2610 | sub read_local_vm_config { | |
2611 | ||
2612 | my $openvz = {}; | |
2613 | my $qemu = {}; | |
2614 | ||
2615 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
2616 | ||
2617 | my $vmlist = PVE::Cluster::get_vmlist(); | |
2618 | return $vmdata if !$vmlist || !$vmlist->{ids}; | |
2619 | my $ids = $vmlist->{ids}; | |
2620 | ||
2621 | foreach my $vmid (keys %$ids) { | |
2622 | next if !$vmid; # skip VE0 | |
2623 | my $d = $ids->{$vmid}; | |
2624 | next if !$d->{node} || $d->{node} ne $nodename; | |
2625 | next if !$d->{type}; | |
2626 | if ($d->{type} eq 'openvz') { | |
2627 | if ($have_pve_manager) { | |
2628 | my $cfspath = PVE::OpenVZ::cfs_config_path($vmid); | |
2629 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
2630 | $openvz->{$vmid} = $conf; | |
2631 | } | |
2632 | } | |
2633 | } elsif ($d->{type} eq 'qemu') { | |
2634 | if ($have_qemu_server) { | |
2635 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); | |
2636 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
2637 | $qemu->{$vmid} = $conf; | |
2638 | } | |
2639 | } | |
2640 | } | |
2641 | } | |
2642 | ||
2643 | return $vmdata; | |
2644 | }; | |
2645 | ||
2646 | sub load_vmfw_conf { | |
2647 | my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_; | |
2648 | ||
2649 | my $vmfw_conf = {}; | |
2650 | ||
2651 | $dir = $pvefw_conf_dir if !defined($dir); | |
2652 | ||
2653 | my $filename = "$dir/$vmid.fw"; | |
2654 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
2655 | $vmfw_conf = parse_vmfw_config($filename, $fh, $cluster_conf, $rule_env, $verbose); | |
2656 | $vmfw_conf->{vmid} = $vmid; | |
2657 | } | |
2658 | ||
2659 | return $vmfw_conf; | |
2660 | } | |
2661 | ||
2662 | my $format_rules = sub { | |
2663 | my ($rules, $allow_iface) = @_; | |
2664 | ||
2665 | my $raw = ''; | |
2666 | ||
2667 | foreach my $rule (@$rules) { | |
2668 | if ($rule->{type} eq 'in' || $rule->{type} eq 'out' || $rule->{type} eq 'group') { | |
2669 | $raw .= '|' if defined($rule->{enable}) && !$rule->{enable}; | |
2670 | $raw .= uc($rule->{type}); | |
2671 | if ($rule->{macro}) { | |
2672 | $raw .= " $rule->{macro}($rule->{action})"; | |
2673 | } else { | |
2674 | $raw .= " " . $rule->{action}; | |
2675 | } | |
2676 | if ($allow_iface && $rule->{iface}) { | |
2677 | $raw .= " -i $rule->{iface}"; | |
2678 | } | |
2679 | ||
2680 | if ($rule->{type} ne 'group') { | |
2681 | $raw .= " -source $rule->{source}" if $rule->{source}; | |
2682 | $raw .= " -dest $rule->{dest}" if $rule->{dest}; | |
2683 | $raw .= " -p $rule->{proto}" if $rule->{proto}; | |
2684 | $raw .= " -dport $rule->{dport}" if $rule->{dport}; | |
2685 | $raw .= " -sport $rule->{sport}" if $rule->{sport}; | |
2686 | } | |
2687 | ||
2688 | $raw .= " # " . encode('utf8', $rule->{comment}) | |
2689 | if $rule->{comment} && $rule->{comment} !~ m/^\s*$/; | |
2690 | $raw .= "\n"; | |
2691 | } else { | |
2692 | die "unknown rule type '$rule->{type}'"; | |
2693 | } | |
2694 | } | |
2695 | ||
2696 | return $raw; | |
2697 | }; | |
2698 | ||
2699 | my $format_options = sub { | |
2700 | my ($options) = @_; | |
2701 | ||
2702 | my $raw = ''; | |
2703 | ||
2704 | $raw .= "[OPTIONS]\n\n"; | |
2705 | foreach my $opt (keys %$options) { | |
2706 | $raw .= "$opt: $options->{$opt}\n"; | |
2707 | } | |
2708 | $raw .= "\n"; | |
2709 | ||
2710 | return $raw; | |
2711 | }; | |
2712 | ||
2713 | my $format_aliases = sub { | |
2714 | my ($aliases) = @_; | |
2715 | ||
2716 | my $raw = ''; | |
2717 | ||
2718 | $raw .= "[ALIASES]\n\n"; | |
2719 | foreach my $k (keys %$aliases) { | |
2720 | my $e = $aliases->{$k}; | |
2721 | $raw .= "$e->{name} $e->{cidr}"; | |
2722 | $raw .= " # " . encode('utf8', $e->{comment}) | |
2723 | if $e->{comment} && $e->{comment} !~ m/^\s*$/; | |
2724 | $raw .= "\n"; | |
2725 | } | |
2726 | $raw .= "\n"; | |
2727 | ||
2728 | return $raw; | |
2729 | }; | |
2730 | ||
2731 | my $format_ipsets = sub { | |
2732 | my ($fw_conf) = @_; | |
2733 | ||
2734 | my $raw = ''; | |
2735 | ||
2736 | foreach my $ipset (sort keys %{$fw_conf->{ipset}}) { | |
2737 | if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) { | |
2738 | my $utf8comment = encode('utf8', $comment); | |
2739 | $raw .= "[IPSET $ipset] # $utf8comment\n\n"; | |
2740 | } else { | |
2741 | $raw .= "[IPSET $ipset]\n\n"; | |
2742 | } | |
2743 | my $options = $fw_conf->{ipset}->{$ipset}; | |
2744 | ||
2745 | my $nethash = {}; | |
2746 | foreach my $entry (@$options) { | |
2747 | $nethash->{$entry->{cidr}} = $entry; | |
2748 | } | |
2749 | ||
2750 | foreach my $cidr (sort keys %$nethash) { | |
2751 | my $entry = $nethash->{$cidr}; | |
2752 | my $line = $entry->{nomatch} ? '!' : ''; | |
2753 | $line .= $entry->{cidr}; | |
2754 | $line .= " # " . encode('utf8', $entry->{comment}) | |
2755 | if $entry->{comment} && $entry->{comment} !~ m/^\s*$/; | |
2756 | $raw .= "$line\n"; | |
2757 | } | |
2758 | ||
2759 | $raw .= "\n"; | |
2760 | } | |
2761 | ||
2762 | return $raw; | |
2763 | }; | |
2764 | ||
2765 | sub save_vmfw_conf { | |
2766 | my ($vmid, $vmfw_conf) = @_; | |
2767 | ||
2768 | my $raw = ''; | |
2769 | ||
2770 | my $options = $vmfw_conf->{options}; | |
2771 | $raw .= &$format_options($options) if $options && scalar(keys %$options); | |
2772 | ||
2773 | my $aliases = $vmfw_conf->{aliases}; | |
2774 | $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases); | |
2775 | ||
2776 | $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset}; | |
2777 | ||
2778 | my $rules = $vmfw_conf->{rules} || []; | |
2779 | if ($rules && scalar(@$rules)) { | |
2780 | $raw .= "[RULES]\n\n"; | |
2781 | $raw .= &$format_rules($rules, 1); | |
2782 | $raw .= "\n"; | |
2783 | } | |
2784 | ||
2785 | mkdir $pvefw_conf_dir; | |
2786 | ||
2787 | my $filename = "$pvefw_conf_dir/$vmid.fw"; | |
2788 | PVE::Tools::file_set_contents($filename, $raw); | |
2789 | } | |
2790 | ||
2791 | sub read_vm_firewall_configs { | |
2792 | my ($cluster_conf, $vmdata, $dir, $verbose) = @_; | |
2793 | ||
2794 | my $vmfw_configs = {}; | |
2795 | ||
2796 | foreach my $vmid (keys %{$vmdata->{qemu}}) { | |
2797 | my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm', $vmid, $dir, $verbose); | |
2798 | next if !$vmfw_conf->{options}; # skip if file does not exists | |
2799 | $vmfw_configs->{$vmid} = $vmfw_conf; | |
2800 | } | |
2801 | foreach my $vmid (keys %{$vmdata->{openvz}}) { | |
2802 | my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct', $vmid, $dir, $verbose); | |
2803 | next if !$vmfw_conf->{options}; # skip if file does not exists | |
2804 | $vmfw_configs->{$vmid} = $vmfw_conf; | |
2805 | } | |
2806 | ||
2807 | return $vmfw_configs; | |
2808 | } | |
2809 | ||
2810 | sub get_option_log_level { | |
2811 | my ($options, $k) = @_; | |
2812 | ||
2813 | my $v = $options->{$k}; | |
2814 | $v = $default_log_level if !defined($v); | |
2815 | ||
2816 | return undef if $v eq '' || $v eq 'nolog'; | |
2817 | ||
2818 | $v = $log_level_hash->{$v} if defined($log_level_hash->{$v}); | |
2819 | ||
2820 | return $v if ($v >= 0) && ($v <= 7); | |
2821 | ||
2822 | warn "unknown log level ($k = '$v')\n"; | |
2823 | ||
2824 | return undef; | |
2825 | } | |
2826 | ||
2827 | sub generate_std_chains { | |
2828 | my ($ruleset, $options, $ipversion) = @_; | |
2829 | ||
2830 | my $std_chains = $pve_std_chains->{$ipversion} || die "internal error"; | |
2831 | ||
2832 | my $loglevel = get_option_log_level($options, 'smurf_log_level'); | |
2833 | ||
2834 | my $chain; | |
2835 | ||
2836 | if ($ipversion == 4) { | |
2837 | # same as shorewall smurflog. | |
2838 | $chain = 'PVEFW-smurflog'; | |
2839 | $std_chains->{$chain} = []; | |
2840 | ||
2841 | push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; | |
2842 | push @{$std_chains->{$chain}}, "-j DROP"; | |
2843 | } | |
2844 | ||
2845 | # same as shorewall logflags action. | |
2846 | $loglevel = get_option_log_level($options, 'tcp_flags_log_level'); | |
2847 | $chain = 'PVEFW-logflags'; | |
2848 | $std_chains->{$chain} = []; | |
2849 | ||
2850 | # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG) | |
2851 | push @{$std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; | |
2852 | push @{$std_chains->{$chain}}, "-j DROP"; | |
2853 | ||
2854 | foreach my $chain (keys %$std_chains) { | |
2855 | ruleset_create_chain($ruleset, $chain); | |
2856 | foreach my $rule (@{$std_chains->{$chain}}) { | |
2857 | if (ref($rule)) { | |
2858 | ruleset_generate_rule($ruleset, $chain, $rule); | |
2859 | } else { | |
2860 | ruleset_addrule($ruleset, $chain, $rule); | |
2861 | } | |
2862 | } | |
2863 | } | |
2864 | } | |
2865 | ||
2866 | sub generate_ipset_chains { | |
2867 | my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; | |
2868 | ||
2869 | foreach my $ipset (keys %{$fw_conf->{ipset}}) { | |
2870 | my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $ipset); | |
2871 | generate_ipset($ipset_ruleset, $ipset_chain, $fw_conf->{ipset}->{$ipset}, $clusterfw_conf, $fw_conf); | |
2872 | } | |
2873 | } | |
2874 | ||
2875 | sub generate_ipset { | |
2876 | my ($ipset_ruleset, $name, $options, $clusterfw_conf, $fw_conf) = @_; | |
2877 | ||
2878 | die "duplicate ipset chain '$name'\n" if defined($ipset_ruleset->{$name}); | |
2879 | ||
2880 | $ipset_ruleset->{$name} = ["create $name list:set size 4"]; | |
2881 | ||
2882 | # remove duplicates | |
2883 | my $nethash = {}; | |
2884 | foreach my $entry (@$options) { | |
2885 | next if $entry->{errors}; # skip entries with errors | |
2886 | eval { | |
2887 | my ($cidr, $ipversion); | |
2888 | if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) { | |
2889 | ($cidr, $ipversion) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr}); | |
2890 | } else { | |
2891 | ($cidr, $ipversion) = parse_ip_or_cidr($entry->{cidr}); | |
2892 | } | |
2893 | #http://backreference.org/2013/03/01/ipv6-address-normalization/ | |
2894 | if ($ipversion == 6) { | |
2895 | my $ipv6 = inet_pton(AF_INET6, lc($cidr)); | |
2896 | $cidr = inet_ntop(AF_INET6, $ipv6); | |
2897 | $cidr =~ s|/128$||; | |
2898 | } else { | |
2899 | $cidr =~ s|/32$||; | |
2900 | } | |
2901 | ||
2902 | $nethash->{$ipversion}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} }; | |
2903 | }; | |
2904 | warn $@ if $@; | |
2905 | } | |
2906 | ||
2907 | foreach my $ipversion (sort keys %$nethash) { | |
2908 | my $data = $nethash->{$ipversion}; | |
2909 | my $subname = "$name-v$ipversion"; | |
2910 | ||
2911 | my $hashsize = scalar(@$options); | |
2912 | if ($hashsize <= 64) { | |
2913 | $hashsize = 64; | |
2914 | } else { | |
2915 | $hashsize = round_powerof2($hashsize); | |
2916 | } | |
2917 | ||
2918 | my $family = $ipversion == "6" ? "inet6" : "inet"; | |
2919 | ||
2920 | $ipset_ruleset->{$subname} = ["create $subname hash:net family $family hashsize $hashsize maxelem $hashsize"]; | |
2921 | ||
2922 | foreach my $cidr (sort keys %$data) { | |
2923 | my $entry = $data->{$cidr}; | |
2924 | ||
2925 | my $cmd = "add $subname $cidr"; | |
2926 | if ($entry->{nomatch}) { | |
2927 | if ($feature_ipset_nomatch) { | |
2928 | push @{$ipset_ruleset->{$subname}}, "$cmd nomatch"; | |
2929 | } else { | |
2930 | warn "ignore !$cidr - nomatch not supported by kernel\n"; | |
2931 | } | |
2932 | } else { | |
2933 | push @{$ipset_ruleset->{$subname}}, $cmd; | |
2934 | } | |
2935 | } | |
2936 | ||
2937 | push @{$ipset_ruleset->{$name}}, "add $name $subname"; | |
2938 | } | |
2939 | } | |
2940 | ||
2941 | sub round_powerof2 { | |
2942 | my ($int) = @_; | |
2943 | ||
2944 | $int--; | |
2945 | $int |= $int >> $_ foreach (1,2,4,8,16); | |
2946 | return ++$int; | |
2947 | } | |
2948 | ||
2949 | sub load_clusterfw_conf { | |
2950 | my ($filename, $verbose) = @_; | |
2951 | ||
2952 | $filename = $clusterfw_conf_filename if !defined($filename); | |
2953 | ||
2954 | my $cluster_conf = {}; | |
2955 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
2956 | $cluster_conf = parse_clusterfw_config($filename, $fh, $verbose); | |
2957 | } | |
2958 | ||
2959 | return $cluster_conf; | |
2960 | } | |
2961 | ||
2962 | sub save_clusterfw_conf { | |
2963 | my ($cluster_conf) = @_; | |
2964 | ||
2965 | my $raw = ''; | |
2966 | ||
2967 | my $options = $cluster_conf->{options}; | |
2968 | $raw .= &$format_options($options) if $options && scalar(keys %$options); | |
2969 | ||
2970 | my $aliases = $cluster_conf->{aliases}; | |
2971 | $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases); | |
2972 | ||
2973 | $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset}; | |
2974 | ||
2975 | my $rules = $cluster_conf->{rules}; | |
2976 | if ($rules && scalar(@$rules)) { | |
2977 | $raw .= "[RULES]\n\n"; | |
2978 | $raw .= &$format_rules($rules, 1); | |
2979 | $raw .= "\n"; | |
2980 | } | |
2981 | ||
2982 | if ($cluster_conf->{groups}) { | |
2983 | foreach my $group (sort keys %{$cluster_conf->{groups}}) { | |
2984 | my $rules = $cluster_conf->{groups}->{$group}; | |
2985 | if (my $comment = $cluster_conf->{group_comments}->{$group}) { | |
2986 | my $utf8comment = encode('utf8', $comment); | |
2987 | $raw .= "[group $group] # $utf8comment\n\n"; | |
2988 | } else { | |
2989 | $raw .= "[group $group]\n\n"; | |
2990 | } | |
2991 | ||
2992 | $raw .= &$format_rules($rules, 0); | |
2993 | $raw .= "\n"; | |
2994 | } | |
2995 | } | |
2996 | ||
2997 | mkdir $pvefw_conf_dir; | |
2998 | PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw); | |
2999 | } | |
3000 | ||
3001 | sub load_hostfw_conf { | |
3002 | my ($cluster_conf, $filename, $verbose) = @_; | |
3003 | ||
3004 | $filename = $hostfw_conf_filename if !defined($filename); | |
3005 | ||
3006 | my $hostfw_conf = {}; | |
3007 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
3008 | $hostfw_conf = parse_hostfw_config($filename, $fh, $cluster_conf, $verbose); | |
3009 | } | |
3010 | return $hostfw_conf; | |
3011 | } | |
3012 | ||
3013 | sub save_hostfw_conf { | |
3014 | my ($hostfw_conf) = @_; | |
3015 | ||
3016 | my $raw = ''; | |
3017 | ||
3018 | my $options = $hostfw_conf->{options}; | |
3019 | $raw .= &$format_options($options) if $options && scalar(keys %$options); | |
3020 | ||
3021 | my $rules = $hostfw_conf->{rules}; | |
3022 | if ($rules && scalar(@$rules)) { | |
3023 | $raw .= "[RULES]\n\n"; | |
3024 | $raw .= &$format_rules($rules, 1); | |
3025 | $raw .= "\n"; | |
3026 | } | |
3027 | ||
3028 | PVE::Tools::file_set_contents($hostfw_conf_filename, $raw); | |
3029 | } | |
3030 | ||
3031 | sub compile { | |
3032 | my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_; | |
3033 | ||
3034 | my $vmfw_configs; | |
3035 | ||
3036 | if ($vmdata) { # test mode | |
3037 | my $testdir = $vmdata->{testdir} || die "no test directory specified"; | |
3038 | my $filename = "$testdir/cluster.fw"; | |
3039 | $cluster_conf = load_clusterfw_conf($filename, $verbose); | |
3040 | ||
3041 | $filename = "$testdir/host.fw"; | |
3042 | $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose); | |
3043 | ||
3044 | $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose); | |
3045 | } else { # normal operation | |
3046 | $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf; | |
3047 | ||
3048 | $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf; | |
3049 | ||
3050 | $vmdata = read_local_vm_config(); | |
3051 | $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose); | |
3052 | } | |
3053 | ||
3054 | my ($ruleset, $ipset_ruleset) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4, $verbose); | |
3055 | my ($rulesetv6) = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6, $verbose); | |
3056 | ||
3057 | return ($ruleset, $ipset_ruleset, $rulesetv6); | |
3058 | } | |
3059 | ||
3060 | sub compile_iptables_filter { | |
3061 | my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion, $verbose) = @_; | |
3062 | ||
3063 | $cluster_conf->{ipset}->{venet0} = []; | |
3064 | my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0'); | |
3065 | ||
3066 | my $localnet; | |
3067 | if ($cluster_conf->{aliases}->{local_network}) { | |
3068 | $localnet = $cluster_conf->{aliases}->{local_network}->{cidr}; | |
3069 | } else { | |
3070 | $localnet = local_network() || '127.0.0.0/8'; | |
3071 | $cluster_conf->{aliases}->{local_network} = { cidr => $localnet }; | |
3072 | } | |
3073 | ||
3074 | push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet }; | |
3075 | ||
3076 | return ({}, {}) if !$cluster_conf->{options}->{enable}; | |
3077 | ||
3078 | my $ruleset = {}; | |
3079 | ||
3080 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); | |
3081 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
3082 | ||
3083 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
3084 | ||
3085 | my $hostfw_options = $hostfw_conf->{options} || {}; | |
3086 | ||
3087 | # fixme: what log level should we use here? | |
3088 | my $loglevel = get_option_log_level($hostfw_options, "log_level_out"); | |
3089 | ||
3090 | ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT"); | |
3091 | ||
3092 | ||
3093 | ruleset_create_chain($ruleset, "PVEFW-VENET-OUT"); | |
3094 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT"); | |
3095 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT"); | |
3096 | ||
3097 | ruleset_create_chain($ruleset, "PVEFW-FWBR-IN"); | |
3098 | ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $hostfw_options, $cluster_conf, $loglevel); | |
3099 | ||
3100 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN"); | |
3101 | ||
3102 | ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT"); | |
3103 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT"); | |
3104 | ||
3105 | ruleset_create_chain($ruleset, "PVEFW-VENET-IN"); | |
3106 | ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $hostfw_options, $cluster_conf, $loglevel); | |
3107 | ||
3108 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN"); | |
3109 | ||
3110 | generate_std_chains($ruleset, $hostfw_options, $ipversion); | |
3111 | ||
3112 | my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0)); | |
3113 | ||
3114 | my $ipset_ruleset = {}; | |
3115 | ||
3116 | # currently pveproxy don't works with ipv6, so let's generate host fw ipv4 only for the moment | |
3117 | if ($hostfw_enable && ($ipversion == 4)) { | |
3118 | eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); }; | |
3119 | warn $@ if $@; # just to be sure - should not happen | |
3120 | } | |
3121 | ||
3122 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN"); | |
3123 | ||
3124 | # generate firewall rules for QEMU VMs | |
3125 | foreach my $vmid (keys %{$vmdata->{qemu}}) { | |
3126 | eval { | |
3127 | my $conf = $vmdata->{qemu}->{$vmid}; | |
3128 | my $vmfw_conf = $vmfw_configs->{$vmid}; | |
3129 | return if !$vmfw_conf; | |
3130 | ||
3131 | generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf); | |
3132 | ||
3133 | foreach my $netid (keys %$conf) { | |
3134 | next if $netid !~ m/^net(\d+)$/; | |
3135 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
3136 | next if !$net->{firewall}; | |
3137 | my $iface = "tap${vmid}i$1"; | |
3138 | ||
3139 | my $macaddr = $net->{macaddr}; | |
3140 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
3141 | $vmfw_conf, $vmid, 'IN', $ipversion); | |
3142 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
3143 | $vmfw_conf, $vmid, 'OUT', $ipversion); | |
3144 | } | |
3145 | }; | |
3146 | warn $@ if $@; # just to be sure - should not happen | |
3147 | } | |
3148 | ||
3149 | # generate firewall rules for OpenVZ containers | |
3150 | foreach my $vmid (keys %{$vmdata->{openvz}}) { | |
3151 | eval { | |
3152 | my $conf = $vmdata->{openvz}->{$vmid}; | |
3153 | ||
3154 | my $vmfw_conf = $vmfw_configs->{$vmid}; | |
3155 | return if !$vmfw_conf; | |
3156 | ||
3157 | generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf); | |
3158 | ||
3159 | if ($vmfw_conf->{options}->{enable}) { | |
3160 | if ($conf->{ip_address} && $conf->{ip_address}->{value}) { | |
3161 | my $ip = $conf->{ip_address}->{value}; | |
3162 | $ip =~ s/\s+/,/g; | |
3163 | ||
3164 | my @ips = (); | |
3165 | ||
3166 | foreach my $singleip (split(',', $ip)) { | |
3167 | my $singleip_ver = parse_address_list($singleip); # make sure we have a valid $ip list | |
3168 | push @{$cluster_conf->{ipset}->{venet0}}, { cidr => $singleip }; | |
3169 | push @ips, $singleip if $singleip_ver == $ipversion; | |
3170 | } | |
3171 | ||
3172 | if (scalar(@ips)) { | |
3173 | my $ip_list = join(',', @ips); | |
3174 | generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'IN', $ipversion); | |
3175 | generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip_list, 'OUT', $ipversion); | |
3176 | } | |
3177 | } | |
3178 | } | |
3179 | ||
3180 | if ($conf->{netif} && $conf->{netif}->{value}) { | |
3181 | my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value}); | |
3182 | foreach my $netid (keys %$netif) { | |
3183 | my $d = $netif->{$netid}; | |
3184 | my $bridge = $d->{bridge}; | |
3185 | next if !$bridge || $bridge !~ m/^vmbr\d+(v(\d+))?f$/; # firewall enabled ? | |
3186 | my $macaddr = $d->{mac}; | |
3187 | my $iface = $d->{host_ifname}; | |
3188 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
3189 | $vmfw_conf, $vmid, 'IN', $ipversion); | |
3190 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
3191 | $vmfw_conf, $vmid, 'OUT', $ipversion); | |
3192 | } | |
3193 | } | |
3194 | }; | |
3195 | warn $@ if $@; # just to be sure - should not happen | |
3196 | } | |
3197 | ||
3198 | if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){ | |
3199 | ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS"); | |
3200 | } | |
3201 | ||
3202 | generate_ipset_chains($ipset_ruleset, undef, $cluster_conf); | |
3203 | ||
3204 | return ($ruleset, $ipset_ruleset); | |
3205 | } | |
3206 | ||
3207 | sub get_ruleset_status { | |
3208 | my ($ruleset, $active_chains, $digest_fn, $verbose) = @_; | |
3209 | ||
3210 | my $statushash = {}; | |
3211 | ||
3212 | foreach my $chain (sort keys %$ruleset) { | |
3213 | my $sig = &$digest_fn($ruleset->{$chain}); | |
3214 | ||
3215 | $statushash->{$chain}->{sig} = $sig; | |
3216 | ||
3217 | my $oldsig = $active_chains->{$chain}; | |
3218 | if (!defined($oldsig)) { | |
3219 | $statushash->{$chain}->{action} = 'create'; | |
3220 | } else { | |
3221 | if ($oldsig eq $sig) { | |
3222 | $statushash->{$chain}->{action} = 'exists'; | |
3223 | } else { | |
3224 | $statushash->{$chain}->{action} = 'update'; | |
3225 | } | |
3226 | } | |
3227 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
3228 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
3229 | print "\t$cmd\n" if $verbose; | |
3230 | } | |
3231 | } | |
3232 | ||
3233 | foreach my $chain (sort keys %$active_chains) { | |
3234 | if (!defined($ruleset->{$chain})) { | |
3235 | my $sig = $active_chains->{$chain}; | |
3236 | $statushash->{$chain}->{action} = 'delete'; | |
3237 | $statushash->{$chain}->{sig} = $sig; | |
3238 | print "delete $chain ($sig)\n" if $verbose; | |
3239 | } | |
3240 | } | |
3241 | ||
3242 | return $statushash; | |
3243 | } | |
3244 | ||
3245 | sub print_sig_rule { | |
3246 | my ($chain, $sig) = @_; | |
3247 | ||
3248 | # We just use this to store a SHA1 checksum used to detect changes | |
3249 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
3250 | } | |
3251 | ||
3252 | sub get_ruleset_cmdlist { | |
3253 | my ($ruleset, $verbose) = @_; | |
3254 | ||
3255 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
3256 | ||
3257 | my ($active_chains, $hooks) = iptables_get_chains(); | |
3258 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose); | |
3259 | ||
3260 | # create missing chains first | |
3261 | foreach my $chain (sort keys %$ruleset) { | |
3262 | my $stat = $statushash->{$chain}; | |
3263 | die "internal error" if !$stat; | |
3264 | next if $stat->{action} ne 'create'; | |
3265 | ||
3266 | $cmdlist .= ":$chain - [0:0]\n"; | |
3267 | } | |
3268 | ||
3269 | foreach my $h (qw(INPUT OUTPUT FORWARD)) { | |
3270 | my $chain = "PVEFW-$h"; | |
3271 | if ($ruleset->{$chain} && !$hooks->{$h}) { | |
3272 | $cmdlist .= "-A $h -j $chain\n"; | |
3273 | } | |
3274 | } | |
3275 | ||
3276 | foreach my $chain (sort keys %$ruleset) { | |
3277 | my $stat = $statushash->{$chain}; | |
3278 | die "internal error" if !$stat; | |
3279 | ||
3280 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
3281 | $cmdlist .= "-F $chain\n"; | |
3282 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
3283 | $cmdlist .= "$cmd\n"; | |
3284 | } | |
3285 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
3286 | } elsif ($stat->{action} eq 'delete') { | |
3287 | die "internal error"; # this should not happen | |
3288 | } elsif ($stat->{action} eq 'exists') { | |
3289 | # do nothing | |
3290 | } else { | |
3291 | die "internal error - unknown status '$stat->{action}'"; | |
3292 | } | |
3293 | } | |
3294 | ||
3295 | foreach my $chain (keys %$statushash) { | |
3296 | next if $statushash->{$chain}->{action} ne 'delete'; | |
3297 | $cmdlist .= "-F $chain\n"; | |
3298 | } | |
3299 | foreach my $chain (keys %$statushash) { | |
3300 | next if $statushash->{$chain}->{action} ne 'delete'; | |
3301 | next if $chain eq 'PVEFW-INPUT'; | |
3302 | next if $chain eq 'PVEFW-OUTPUT'; | |
3303 | next if $chain eq 'PVEFW-FORWARD'; | |
3304 | $cmdlist .= "-X $chain\n"; | |
3305 | } | |
3306 | ||
3307 | my $changes = $cmdlist ne "*filter\n" ? 1 : 0; | |
3308 | ||
3309 | $cmdlist .= "COMMIT\n"; | |
3310 | ||
3311 | return wantarray ? ($cmdlist, $changes) : $cmdlist; | |
3312 | } | |
3313 | ||
3314 | sub get_ipset_cmdlist { | |
3315 | my ($ruleset, $verbose) = @_; | |
3316 | ||
3317 | my $cmdlist = ""; | |
3318 | ||
3319 | my $delete_cmdlist = ""; | |
3320 | ||
3321 | my $active_chains = ipset_get_chains(); | |
3322 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest, $verbose); | |
3323 | ||
3324 | # remove stale _swap chains | |
3325 | foreach my $chain (keys %$active_chains) { | |
3326 | if ($chain =~ m/^PVEFW-\S+_swap$/) { | |
3327 | $cmdlist .= "destroy $chain\n"; | |
3328 | } | |
3329 | } | |
3330 | ||
3331 | foreach my $chain (sort keys %$ruleset) { | |
3332 | my $stat = $statushash->{$chain}; | |
3333 | die "internal error" if !$stat; | |
3334 | ||
3335 | if ($stat->{action} eq 'create') { | |
3336 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
3337 | $cmdlist .= "$cmd\n"; | |
3338 | } | |
3339 | } | |
3340 | } | |
3341 | ||
3342 | foreach my $chain (sort keys %$ruleset) { | |
3343 | my $stat = $statushash->{$chain}; | |
3344 | die "internal error" if !$stat; | |
3345 | ||
3346 | if ($stat->{action} eq 'update') { | |
3347 | my $chain_swap = $chain."_swap"; | |
3348 | ||
3349 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
3350 | $cmd =~ s/$chain/$chain_swap/; | |
3351 | $cmdlist .= "$cmd\n"; | |
3352 | } | |
3353 | $cmdlist .= "swap $chain_swap $chain\n"; | |
3354 | $cmdlist .= "flush $chain_swap\n"; | |
3355 | $cmdlist .= "destroy $chain_swap\n"; | |
3356 | } | |
3357 | } | |
3358 | ||
3359 | foreach my $chain (sort keys %$statushash) { | |
3360 | next if $statushash->{$chain}->{action} ne 'delete'; | |
3361 | ||
3362 | $delete_cmdlist .= "flush $chain\n"; | |
3363 | $delete_cmdlist .= "destroy $chain\n"; | |
3364 | } | |
3365 | ||
3366 | my $changes = ($cmdlist || $delete_cmdlist) ? 1 : 0; | |
3367 | ||
3368 | return ($cmdlist, $delete_cmdlist, $changes); | |
3369 | } | |
3370 | ||
3371 | sub apply_ruleset { | |
3372 | my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_; | |
3373 | ||
3374 | enable_bridge_firewall(); | |
3375 | ||
3376 | my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) = | |
3377 | get_ipset_cmdlist($ipset_ruleset, undef, $verbose); | |
3378 | ||
3379 | my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset, $verbose); | |
3380 | ||
3381 | if ($verbose) { | |
3382 | if ($ipset_changes) { | |
3383 | print "ipset changes:\n"; | |
3384 | print $ipset_create_cmdlist if $ipset_create_cmdlist; | |
3385 | print $ipset_delete_cmdlist if $ipset_delete_cmdlist; | |
3386 | } | |
3387 | ||
3388 | if ($changes) { | |
3389 | print "iptables changes:\n"; | |
3390 | print $cmdlist; | |
3391 | } | |
3392 | } | |
3393 | ||
3394 | ipset_restore_cmdlist($ipset_create_cmdlist); | |
3395 | ||
3396 | iptables_restore_cmdlist($cmdlist); | |
3397 | ||
3398 | ipset_restore_cmdlist($ipset_delete_cmdlist) if $ipset_delete_cmdlist; | |
3399 | ||
3400 | # test: re-read status and check if everything is up to date | |
3401 | my $active_chains = iptables_get_chains(); | |
3402 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0); | |
3403 | ||
3404 | my $errors; | |
3405 | foreach my $chain (sort keys %$ruleset) { | |
3406 | my $stat = $statushash->{$chain}; | |
3407 | if ($stat->{action} ne 'exists') { | |
3408 | warn "unable to update chain '$chain'\n"; | |
3409 | $errors = 1; | |
3410 | } | |
3411 | } | |
3412 | ||
3413 | die "unable to apply firewall changes\n" if $errors; | |
3414 | ||
3415 | update_nf_conntrack_max($hostfw_conf); | |
3416 | ||
3417 | update_nf_conntrack_tcp_timeout_established($hostfw_conf); | |
3418 | ||
3419 | } | |
3420 | ||
3421 | sub update_nf_conntrack_max { | |
3422 | my ($hostfw_conf) = @_; | |
3423 | ||
3424 | my $max = 65536; # reasonable default | |
3425 | ||
3426 | my $options = $hostfw_conf->{options} || {}; | |
3427 | ||
3428 | if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) { | |
3429 | $max = $options->{nf_conntrack_max}; | |
3430 | $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192 | |
3431 | } | |
3432 | ||
3433 | my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max"; | |
3434 | my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize"; | |
3435 | ||
3436 | my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max); | |
3437 | ||
3438 | if ($current != $max) { | |
3439 | my $hashsize = int($max/4); | |
3440 | PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize); | |
3441 | PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max); | |
3442 | } | |
3443 | } | |
3444 | ||
3445 | sub update_nf_conntrack_tcp_timeout_established { | |
3446 | my ($hostfw_conf) = @_; | |
3447 | ||
3448 | my $options = $hostfw_conf->{options} || {}; | |
3449 | ||
3450 | my $value = defined($options->{nf_conntrack_tcp_timeout_established}) ? $options->{nf_conntrack_tcp_timeout_established} : 432000; | |
3451 | ||
3452 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value); | |
3453 | } | |
3454 | ||
3455 | sub remove_pvefw_chains { | |
3456 | ||
3457 | my ($chash, $hooks) = iptables_get_chains(); | |
3458 | my $cmdlist = "*filter\n"; | |
3459 | ||
3460 | foreach my $h (qw(INPUT OUTPUT FORWARD)) { | |
3461 | if ($hooks->{$h}) { | |
3462 | $cmdlist .= "-D $h -j PVEFW-$h\n"; | |
3463 | } | |
3464 | } | |
3465 | ||
3466 | foreach my $chain (keys %$chash) { | |
3467 | $cmdlist .= "-F $chain\n"; | |
3468 | } | |
3469 | ||
3470 | foreach my $chain (keys %$chash) { | |
3471 | $cmdlist .= "-X $chain\n"; | |
3472 | } | |
3473 | $cmdlist .= "COMMIT\n"; | |
3474 | ||
3475 | iptables_restore_cmdlist($cmdlist); | |
3476 | ||
3477 | my $ipset_chains = ipset_get_chains(); | |
3478 | ||
3479 | $cmdlist = ""; | |
3480 | ||
3481 | foreach my $chain (keys %$ipset_chains) { | |
3482 | $cmdlist .= "flush $chain\n"; | |
3483 | $cmdlist .= "destroy $chain\n"; | |
3484 | } | |
3485 | ||
3486 | ipset_restore_cmdlist($cmdlist) if $cmdlist; | |
3487 | } | |
3488 | ||
3489 | sub init { | |
3490 | my $cluster_conf = load_clusterfw_conf(); | |
3491 | my $cluster_options = $cluster_conf->{options}; | |
3492 | my $enable = $cluster_options->{enable}; | |
3493 | ||
3494 | return if !$enable; | |
3495 | ||
3496 | # load required modules here | |
3497 | } | |
3498 | ||
3499 | sub update { | |
3500 | my $code = sub { | |
3501 | ||
3502 | my $cluster_conf = load_clusterfw_conf(); | |
3503 | my $cluster_options = $cluster_conf->{options}; | |
3504 | ||
3505 | if (!$cluster_options->{enable}) { | |
3506 | PVE::Firewall::remove_pvefw_chains(); | |
3507 | return; | |
3508 | } | |
3509 | ||
3510 | my $hostfw_conf = load_hostfw_conf(); | |
3511 | ||
3512 | my ($ruleset, $ipset_ruleset, $rulesetv6) = compile($cluster_conf, $hostfw_conf); | |
3513 | ||
3514 | apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset); | |
3515 | }; | |
3516 | ||
3517 | run_locked($code); | |
3518 | } | |
3519 | ||
3520 | 1; |