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