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