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