]> git.proxmox.com Git - pve-firewall.git/blame - src/PVE/Firewall.pm
bump version to 4.0-1
[pve-firewall.git] / src / PVE / Firewall.pm
CommitLineData
b6360c3f
DM
1package PVE::Firewall;
2
3use warnings;
4use strict;
585ede43 5
09d5f68e 6use Digest::SHA;
0fd61594
TL
7use Encode;
8use File::Basename;
9use File::Path;
10use IO::File;
11use Net::IP;
12use POSIX;
6d959e3f 13use Socket qw(AF_INET6 inet_ntop inet_pton);
0fd61594
TL
14use Storable qw(dclone);
15
16use PVE::Cluster;
7ca36671 17use PVE::Exception qw(raise raise_param_exc);
0fd61594 18use PVE::INotify;
e74a87f5 19use PVE::JSONSchema qw(register_standard_option get_standard_option);
2e1ae6b9 20use PVE::Network;
0fd61594 21use PVE::ProcFSTools;
eedcb564 22use PVE::SafeSyslog;
0fd61594 23use PVE::Tools qw($IPV4RE $IPV6RE);
1a301a8d 24use PVE::Tools qw(run_command lock_file dir_glob_foreach);
ecbea048 25
21053409
DM
26my $pvefw_conf_dir = "/etc/pve/firewall";
27my $clusterfw_conf_filename = "$pvefw_conf_dir/cluster.fw";
fca39c2c 28
fdefeeab 29# dynamically include PVE::QemuServer and PVE::LXC
d9fee004
DM
30# to avoid dependency problems
31my $have_qemu_server;
32eval {
33 require PVE::QemuServer;
b5a16dd3 34 require PVE::QemuConfig;
d9fee004
DM
35 $have_qemu_server = 1;
36};
37
3b4882dc
AG
38my $have_lxc;
39eval {
40 require PVE::LXC;
41 $have_lxc = 1;
42};
43
259db1e6
DM
44my $pve_fw_status_dir = "/var/lib/pve-firewall";
45
46mkdir $pve_fw_status_dir; # make sure this exists
47
351052d1
DM
48my $security_group_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
49my $ipset_name_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
f9792937 50our $ip_alias_pattern = '[A-Za-z][A-Za-z0-9\-\_]+';
351052d1
DM
51
52my $max_alias_name_length = 64;
53my $max_ipset_name_length = 64;
5c53cde4 54my $max_group_name_length = 18;
351052d1 55
48e3963e
WB
56my $PROTOCOLS_WITH_PORTS = {
57 udp => 1, 17 => 1,
58 udplite => 1, 136 => 1,
59 tcp => 1, 6 => 1,
60 dccp => 1, 33 => 1,
61 sctp => 1, 132 => 1,
62};
63
ae029a88
DM
64PVE::JSONSchema::register_format('IPorCIDR', \&pve_verify_ip_or_cidr);
65sub pve_verify_ip_or_cidr {
009ee3ac
DM
66 my ($cidr, $noerr) = @_;
67
70e524eb 68 if ($cidr =~ m!^(?:$IPV6RE|$IPV4RE)(/(\d+))?$!) {
e34d0e58 69 return $cidr if Net::IP->new($cidr);
7c619bbb
DM
70 return undef if $noerr;
71 die Net::IP::Error() . "\n";
72 }
73 return undef if $noerr;
74 die "value does not look like a valid IP address or CIDR network\n";
75}
76
ae029a88
DM
77PVE::JSONSchema::register_format('IPorCIDRorAlias', \&pve_verify_ip_or_cidr_or_alias);
78sub pve_verify_ip_or_cidr_or_alias {
7c619bbb
DM
79 my ($cidr, $noerr) = @_;
80
81 return if $cidr =~ m/^(?:$ip_alias_pattern)$/;
82
ae029a88 83 return pve_verify_ip_or_cidr($cidr, $noerr);
009ee3ac
DM
84}
85
e74a87f5
DM
86PVE::JSONSchema::register_standard_option('ipset-name', {
87 description => "IP set name.",
88 type => 'string',
351052d1 89 pattern => $ipset_name_pattern,
387d0ffc 90 minLength => 2,
351052d1 91 maxLength => $max_ipset_name_length,
387d0ffc
DM
92});
93
81d574a7
DM
94PVE::JSONSchema::register_standard_option('pve-fw-alias', {
95 description => "Alias name.",
96 type => 'string',
351052d1 97 pattern => $ip_alias_pattern,
81d574a7 98 minLength => 2,
351052d1 99 maxLength => $max_alias_name_length,
81d574a7
DM
100});
101
6302c41f
DM
102PVE::JSONSchema::register_standard_option('pve-fw-loglevel' => {
103 description => "Log level.",
e34d0e58 104 type => 'string',
6302c41f
DM
105 enum => ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'nolog'],
106 optional => 1,
107});
108
387d0ffc
DM
109PVE::JSONSchema::register_standard_option('pve-security-group-name', {
110 description => "Security Group name.",
111 type => 'string',
44be8ceb 112 pattern => $security_group_name_pattern,
387d0ffc 113 minLength => 2,
351052d1 114 maxLength => $max_group_name_length,
e74a87f5 115});
009ee3ac 116
cbb5d6f3
DM
117my $feature_ipset_nomatch = 0;
118eval {
119 my (undef, undef, $release) = POSIX::uname();
120 if ($release =~ m/^(\d+)\.(\d+)\.\d+-/) {
121 my ($major, $minor) = ($1, $2);
122 $feature_ipset_nomatch = 1 if ($major > 3) ||
123 ($major == 3 && $minor >= 7);
124 }
125
126};
127
c8301d63 128my $nodename = PVE::INotify::nodename();
c1f17287 129my $hostfw_conf_filename = "/etc/pve/nodes/$nodename/host.fw";
c8301d63 130
06320eb0
DM
131my $pve_fw_lock_filename = "/var/lock/pvefw.lck";
132
56f313f7 133my $default_log_level = 'nolog'; # avoid logs by default
cc37e000
TL
134my $global_log_ratelimit = '--limit 1/sec';
135
2d404ffc
DM
136my $log_level_hash = {
137 debug => 7,
138 info => 6,
139 notice => 5,
140 warning => 4,
141 err => 3,
142 crit => 2,
143 alert => 1,
144 emerg => 0,
145};
146
40af93c4
TL
147my $verbose = 0;
148sub set_verbose {
149 $verbose = shift;
150}
151
044409e5
TW
152# %rule
153#
154# name => optional
30c390f9 155# enable => [0|1]
044409e5
TW
156# action =>
157# proto =>
30c390f9
TW
158# sport => port[,port[,port]].. or port:port
159# dport => port[,port[,port]].. or port:port
044409e5
TW
160# log => optional, loglevel
161# logmsg => optional, logmsg - overwrites default
30c390f9
TW
162# iface_in => incomin interface
163# iface_out => outgoing interface
044409e5
TW
164# match => optional, overwrites generation of match
165# target => optional, overwrites action
166
35d1d6da
DM
167# we need to overwrite some macros for ipv6
168my $pve_ipv6fw_macros = {
169 'Ping' => [
170 { action => 'PARAM', proto => 'icmpv6', dport => 'echo-request' },
171 ],
b3d75afb
WB
172 'NeighborDiscovery' => [
173 "IPv6 neighbor solicitation, neighbor and router advertisement",
c71cdd44 174 { action => 'PARAM', proto => 'icmpv6', dport => 'router-solicitation' },
b3d75afb
WB
175 { action => 'PARAM', proto => 'icmpv6', dport => 'router-advertisement' },
176 { action => 'PARAM', proto => 'icmpv6', dport => 'neighbor-solicitation' },
177 { action => 'PARAM', proto => 'icmpv6', dport => 'neighbor-advertisement' },
178 ],
a42e9763 179 'DHCPv6' => [
24dd51c2 180 "DHCPv6 traffic",
a42e9763
WB
181 { action => 'PARAM', proto => 'udp', dport => '546:547', sport => '546:547' },
182 ],
35d1d6da
DM
183 'Trcrt' => [
184 { action => 'PARAM', proto => 'udp', dport => '33434:33524' },
185 { action => 'PARAM', proto => 'icmpv6', dport => 'echo-request' },
186 ],
187 };
188
857f62c8 189# imported/converted from: /usr/share/shorewall/macro.*
961b4928 190my $pve_fw_macros = {
857f62c8 191 'Amanda' => [
ebd54ae9 192 "Amanda Backup",
857f62c8
DM
193 { action => 'PARAM', proto => 'udp', dport => '10080' },
194 { action => 'PARAM', proto => 'tcp', dport => '10080' },
195 ],
196 'Auth' => [
ebd54ae9 197 "Auth (identd) traffic",
857f62c8
DM
198 { action => 'PARAM', proto => 'tcp', dport => '113' },
199 ],
200 'BGP' => [
ebd54ae9 201 "Border Gateway Protocol traffic",
857f62c8
DM
202 { action => 'PARAM', proto => 'tcp', dport => '179' },
203 ],
204 'BitTorrent' => [
ebd54ae9 205 "BitTorrent traffic for BitTorrent 3.1 and earlier",
961b4928 206 { action => 'PARAM', proto => 'tcp', dport => '6881:6889' },
857f62c8
DM
207 { action => 'PARAM', proto => 'udp', dport => '6881' },
208 ],
209 'BitTorrent32' => [
ebd54ae9 210 "BitTorrent traffic for BitTorrent 3.2 and later",
857f62c8
DM
211 { action => 'PARAM', proto => 'tcp', dport => '6881:6999' },
212 { action => 'PARAM', proto => 'udp', dport => '6881' },
213 ],
06ba9c44
AG
214 'Ceph' => [
215 "Ceph Storage Cluster traffic (Ceph Monitors, OSD & MDS Deamons)",
216 { action => 'PARAM', proto => 'tcp', dport => '6789' },
217 { action => 'PARAM', proto => 'tcp', dport => '6800:7300' },
218 ],
857f62c8 219 'CVS' => [
ebd54ae9 220 "Concurrent Versions System pserver traffic",
857f62c8
DM
221 { action => 'PARAM', proto => 'tcp', dport => '2401' },
222 ],
223 'Citrix' => [
ebd54ae9 224 "Citrix/ICA traffic (ICA, ICA Browser, CGP)",
857f62c8
DM
225 { action => 'PARAM', proto => 'tcp', dport => '1494' },
226 { action => 'PARAM', proto => 'udp', dport => '1604' },
227 { action => 'PARAM', proto => 'tcp', dport => '2598' },
228 ],
229 'DAAP' => [
ebd54ae9 230 "Digital Audio Access Protocol traffic (iTunes, Rythmbox daemons)",
857f62c8
DM
231 { action => 'PARAM', proto => 'tcp', dport => '3689' },
232 { action => 'PARAM', proto => 'udp', dport => '3689' },
233 ],
234 'DCC' => [
ebd54ae9 235 "Distributed Checksum Clearinghouse spam filtering mechanism",
857f62c8
DM
236 { action => 'PARAM', proto => 'tcp', dport => '6277' },
237 ],
238 'DHCPfwd' => [
d2c3266d 239 "Forwarded DHCP traffic",
857f62c8 240 { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' },
857f62c8
DM
241 ],
242 'DNS' => [
ebd54ae9 243 "Domain Name System traffic (upd and tcp)",
857f62c8
DM
244 { action => 'PARAM', proto => 'udp', dport => '53' },
245 { action => 'PARAM', proto => 'tcp', dport => '53' },
246 ],
247 'Distcc' => [
ebd54ae9 248 "Distributed Compiler service",
857f62c8
DM
249 { action => 'PARAM', proto => 'tcp', dport => '3632' },
250 ],
857f62c8 251 'FTP' => [
ebd54ae9 252 "File Transfer Protocol",
857f62c8
DM
253 { action => 'PARAM', proto => 'tcp', dport => '21' },
254 ],
255 'Finger' => [
ebd54ae9 256 "Finger protocol (RFC 742)",
857f62c8
DM
257 { action => 'PARAM', proto => 'tcp', dport => '79' },
258 ],
259 'GNUnet' => [
ebd54ae9 260 "GNUnet secure peer-to-peer networking traffic",
857f62c8
DM
261 { action => 'PARAM', proto => 'tcp', dport => '2086' },
262 { action => 'PARAM', proto => 'udp', dport => '2086' },
263 { action => 'PARAM', proto => 'tcp', dport => '1080' },
264 { action => 'PARAM', proto => 'udp', dport => '1080' },
265 ],
266 'GRE' => [
d2c3266d 267 "Generic Routing Encapsulation tunneling protocol",
857f62c8 268 { action => 'PARAM', proto => '47' },
857f62c8
DM
269 ],
270 'Git' => [
ebd54ae9 271 "Git distributed revision control traffic",
857f62c8
DM
272 { action => 'PARAM', proto => 'tcp', dport => '9418' },
273 ],
857f62c8 274 'HKP' => [
ebd54ae9 275 "OpenPGP HTTP keyserver protocol traffic",
857f62c8
DM
276 { action => 'PARAM', proto => 'tcp', dport => '11371' },
277 ],
278 'HTTP' => [
ebd54ae9 279 "Hypertext Transfer Protocol (WWW)",
961b4928
DM
280 { action => 'PARAM', proto => 'tcp', dport => '80' },
281 ],
857f62c8 282 'HTTPS' => [
ebd54ae9 283 "Hypertext Transfer Protocol (WWW) over SSL",
857f62c8
DM
284 { action => 'PARAM', proto => 'tcp', dport => '443' },
285 ],
286 'ICPV2' => [
ebd54ae9 287 "Internet Cache Protocol V2 (Squid) traffic",
857f62c8
DM
288 { action => 'PARAM', proto => 'udp', dport => '3130' },
289 ],
290 'ICQ' => [
ebd54ae9 291 "AOL Instant Messenger traffic",
857f62c8
DM
292 { action => 'PARAM', proto => 'tcp', dport => '5190' },
293 ],
294 'IMAP' => [
ebd54ae9 295 "Internet Message Access Protocol",
857f62c8
DM
296 { action => 'PARAM', proto => 'tcp', dport => '143' },
297 ],
298 'IMAPS' => [
ebd54ae9 299 "Internet Message Access Protocol over SSL",
857f62c8
DM
300 { action => 'PARAM', proto => 'tcp', dport => '993' },
301 ],
302 'IPIP' => [
d2c3266d 303 "IPIP capsulation traffic",
857f62c8 304 { action => 'PARAM', proto => '94' },
857f62c8 305 ],
857f62c8 306 'IPsec' => [
d2c3266d 307 "IPsec traffic",
857f62c8
DM
308 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
309 { action => 'PARAM', proto => '50' },
857f62c8
DM
310 ],
311 'IPsecah' => [
d2c3266d 312 "IPsec authentication (AH) traffic",
857f62c8
DM
313 { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
314 { action => 'PARAM', proto => '51' },
857f62c8
DM
315 ],
316 'IPsecnat' => [
d2c3266d 317 "IPsec traffic and Nat-Traversal",
857f62c8
DM
318 { action => 'PARAM', proto => 'udp', dport => '500' },
319 { action => 'PARAM', proto => 'udp', dport => '4500' },
320 { action => 'PARAM', proto => '50' },
857f62c8
DM
321 ],
322 'IRC' => [
ebd54ae9 323 "Internet Relay Chat traffic",
857f62c8
DM
324 { action => 'PARAM', proto => 'tcp', dport => '6667' },
325 ],
857f62c8 326 'Jetdirect' => [
ebd54ae9 327 "HP Jetdirect printing",
857f62c8
DM
328 { action => 'PARAM', proto => 'tcp', dport => '9100' },
329 ],
330 'L2TP' => [
ebd54ae9 331 "Layer 2 Tunneling Protocol traffic",
857f62c8 332 { action => 'PARAM', proto => 'udp', dport => '1701' },
857f62c8
DM
333 ],
334 'LDAP' => [
ebd54ae9 335 "Lightweight Directory Access Protocol traffic",
857f62c8
DM
336 { action => 'PARAM', proto => 'tcp', dport => '389' },
337 ],
338 'LDAPS' => [
ebd54ae9 339 "Secure Lightweight Directory Access Protocol traffic",
857f62c8
DM
340 { action => 'PARAM', proto => 'tcp', dport => '636' },
341 ],
342 'MSNP' => [
ebd54ae9 343 "Microsoft Notification Protocol",
857f62c8
DM
344 { action => 'PARAM', proto => 'tcp', dport => '1863' },
345 ],
346 'MSSQL' => [
ebd54ae9 347 "Microsoft SQL Server",
857f62c8
DM
348 { action => 'PARAM', proto => 'tcp', dport => '1433' },
349 ],
350 'Mail' => [
ebd54ae9 351 "Mail traffic (SMTP, SMTPS, Submission)",
857f62c8
DM
352 { action => 'PARAM', proto => 'tcp', dport => '25' },
353 { action => 'PARAM', proto => 'tcp', dport => '465' },
354 { action => 'PARAM', proto => 'tcp', dport => '587' },
355 ],
7831e1e3
EK
356 'MDNS' => [
357 "Multicast DNS",
358 { action => 'PARAM', proto => 'udp', dport => '5353' },
359 ],
857f62c8 360 'Munin' => [
ebd54ae9 361 "Munin networked resource monitoring traffic",
857f62c8
DM
362 { action => 'PARAM', proto => 'tcp', dport => '4949' },
363 ],
364 'MySQL' => [
ebd54ae9 365 "MySQL server",
857f62c8
DM
366 { action => 'PARAM', proto => 'tcp', dport => '3306' },
367 ],
368 'NNTP' => [
ebd54ae9 369 "NNTP traffic (Usenet).",
857f62c8
DM
370 { action => 'PARAM', proto => 'tcp', dport => '119' },
371 ],
372 'NNTPS' => [
ebd54ae9 373 "Encrypted NNTP traffic (Usenet)",
857f62c8
DM
374 { action => 'PARAM', proto => 'tcp', dport => '563' },
375 ],
376 'NTP' => [
ebd54ae9 377 "Network Time Protocol (ntpd)",
857f62c8
DM
378 { action => 'PARAM', proto => 'udp', dport => '123' },
379 ],
857f62c8 380 'OSPF' => [
ebd54ae9 381 "OSPF multicast traffic",
857f62c8
DM
382 { action => 'PARAM', proto => '89' },
383 ],
384 'OpenVPN' => [
ebd54ae9 385 "OpenVPN traffic",
857f62c8
DM
386 { action => 'PARAM', proto => 'udp', dport => '1194' },
387 ],
388 'PCA' => [
ebd54ae9 389 "Symantec PCAnywere (tm)",
857f62c8
DM
390 { action => 'PARAM', proto => 'udp', dport => '5632' },
391 { action => 'PARAM', proto => 'tcp', dport => '5631' },
392 ],
393 'POP3' => [
ebd54ae9 394 "POP3 traffic",
857f62c8
DM
395 { action => 'PARAM', proto => 'tcp', dport => '110' },
396 ],
397 'POP3S' => [
ebd54ae9 398 "Encrypted POP3 traffic",
857f62c8
DM
399 { action => 'PARAM', proto => 'tcp', dport => '995' },
400 ],
401 'PPtP' => [
ebd54ae9 402 "Point-to-Point Tunneling Protocol",
857f62c8 403 { action => 'PARAM', proto => '47' },
857f62c8
DM
404 { action => 'PARAM', proto => 'tcp', dport => '1723' },
405 ],
406 'Ping' => [
ebd54ae9 407 "ICMP echo request",
269d4f13 408 { action => 'PARAM', proto => 'icmp', dport => 'echo-request' },
857f62c8
DM
409 ],
410 'PostgreSQL' => [
ebd54ae9 411 "PostgreSQL server",
857f62c8
DM
412 { action => 'PARAM', proto => 'tcp', dport => '5432' },
413 ],
414 'Printer' => [
ebd54ae9 415 "Line Printer protocol printing",
857f62c8
DM
416 { action => 'PARAM', proto => 'tcp', dport => '515' },
417 ],
418 'RDP' => [
ebd54ae9 419 "Microsoft Remote Desktop Protocol traffic",
857f62c8
DM
420 { action => 'PARAM', proto => 'tcp', dport => '3389' },
421 ],
d2c3266d 422 'RIP' => [
ebd54ae9 423 "Routing Information Protocol (bidirectional)",
857f62c8 424 { action => 'PARAM', proto => 'udp', dport => '520' },
857f62c8
DM
425 ],
426 'RNDC' => [
ebd54ae9 427 "BIND remote management protocol",
857f62c8
DM
428 { action => 'PARAM', proto => 'tcp', dport => '953' },
429 ],
430 'Razor' => [
ebd54ae9 431 "Razor Antispam System",
056f9211 432 { action => 'PARAM', proto => 'tcp', dport => '2703' },
857f62c8
DM
433 ],
434 'Rdate' => [
ebd54ae9 435 "Remote time retrieval (rdate)",
857f62c8
DM
436 { action => 'PARAM', proto => 'tcp', dport => '37' },
437 ],
438 'Rsync' => [
ebd54ae9 439 "Rsync server",
857f62c8
DM
440 { action => 'PARAM', proto => 'tcp', dport => '873' },
441 ],
442 'SANE' => [
ebd54ae9 443 "SANE network scanning",
857f62c8
DM
444 { action => 'PARAM', proto => 'tcp', dport => '6566' },
445 ],
446 'SMB' => [
ebd54ae9 447 "Microsoft SMB traffic",
857f62c8
DM
448 { action => 'PARAM', proto => 'udp', dport => '135,445' },
449 { action => 'PARAM', proto => 'udp', dport => '137:139' },
450 { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
451 { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
452 ],
857f62c8 453 'SMBswat' => [
ebd54ae9 454 "Samba Web Administration Tool",
857f62c8
DM
455 { action => 'PARAM', proto => 'tcp', dport => '901' },
456 ],
457 'SMTP' => [
ebd54ae9 458 "Simple Mail Transfer Protocol",
857f62c8
DM
459 { action => 'PARAM', proto => 'tcp', dport => '25' },
460 ],
461 'SMTPS' => [
ebd54ae9 462 "Encrypted Simple Mail Transfer Protocol",
857f62c8
DM
463 { action => 'PARAM', proto => 'tcp', dport => '465' },
464 ],
465 'SNMP' => [
ebd54ae9 466 "Simple Network Management Protocol",
857f62c8
DM
467 { action => 'PARAM', proto => 'udp', dport => '161:162' },
468 { action => 'PARAM', proto => 'tcp', dport => '161' },
469 ],
470 'SPAMD' => [
ebd54ae9 471 "Spam Assassin SPAMD traffic",
857f62c8
DM
472 { action => 'PARAM', proto => 'tcp', dport => '783' },
473 ],
474 'SSH' => [
ebd54ae9 475 "Secure shell traffic",
857f62c8
DM
476 { action => 'PARAM', proto => 'tcp', dport => '22' },
477 ],
478 'SVN' => [
ebd54ae9 479 "Subversion server (svnserve)",
857f62c8
DM
480 { action => 'PARAM', proto => 'tcp', dport => '3690' },
481 ],
482 'SixXS' => [
ebd54ae9 483 "SixXS IPv6 Deployment and Tunnel Broker",
857f62c8
DM
484 { action => 'PARAM', proto => 'tcp', dport => '3874' },
485 { action => 'PARAM', proto => 'udp', dport => '3740' },
486 { action => 'PARAM', proto => '41' },
487 { action => 'PARAM', proto => 'udp', dport => '5072,8374' },
488 ],
489 'Squid' => [
ebd54ae9 490 "Squid web proxy traffic",
857f62c8
DM
491 { action => 'PARAM', proto => 'tcp', dport => '3128' },
492 ],
493 'Submission' => [
ebd54ae9 494 "Mail message submission traffic",
857f62c8
DM
495 { action => 'PARAM', proto => 'tcp', dport => '587' },
496 ],
497 'Syslog' => [
ebd54ae9 498 "Syslog protocol (RFC 5424) traffic",
857f62c8
DM
499 { action => 'PARAM', proto => 'udp', dport => '514' },
500 { action => 'PARAM', proto => 'tcp', dport => '514' },
501 ],
502 'TFTP' => [
ebd54ae9 503 "Trivial File Transfer Protocol traffic",
857f62c8
DM
504 { action => 'PARAM', proto => 'udp', dport => '69' },
505 ],
506 'Telnet' => [
ebd54ae9 507 "Telnet traffic",
857f62c8
DM
508 { action => 'PARAM', proto => 'tcp', dport => '23' },
509 ],
510 'Telnets' => [
ebd54ae9 511 "Telnet over SSL",
857f62c8
DM
512 { action => 'PARAM', proto => 'tcp', dport => '992' },
513 ],
514 'Time' => [
ebd54ae9 515 "RFC 868 Time protocol",
857f62c8
DM
516 { action => 'PARAM', proto => 'tcp', dport => '37' },
517 ],
518 'Trcrt' => [
ebd54ae9 519 "Traceroute (for up to 30 hops) traffic",
857f62c8 520 { action => 'PARAM', proto => 'udp', dport => '33434:33524' },
269d4f13 521 { action => 'PARAM', proto => 'icmp', dport => 'echo-request' },
857f62c8
DM
522 ],
523 'VNC' => [
93be4333
DM
524 "VNC traffic for VNC display's 0 - 99",
525 { action => 'PARAM', proto => 'tcp', dport => '5900:5999' },
857f62c8
DM
526 ],
527 'VNCL' => [
ebd54ae9 528 "VNC traffic from Vncservers to Vncviewers in listen mode",
857f62c8
DM
529 { action => 'PARAM', proto => 'tcp', dport => '5500' },
530 ],
531 'Web' => [
ebd54ae9 532 "WWW traffic (HTTP and HTTPS)",
857f62c8 533 { action => 'PARAM', proto => 'tcp', dport => '80' },
961b4928
DM
534 { action => 'PARAM', proto => 'tcp', dport => '443' },
535 ],
857f62c8 536 'Webcache' => [
ebd54ae9 537 "Web Cache/Proxy traffic (port 8080)",
857f62c8
DM
538 { action => 'PARAM', proto => 'tcp', dport => '8080' },
539 ],
540 'Webmin' => [
ebd54ae9 541 "Webmin traffic",
857f62c8
DM
542 { action => 'PARAM', proto => 'tcp', dport => '10000' },
543 ],
544 'Whois' => [
ebd54ae9 545 "Whois (nicname, RFC 3912) traffic",
857f62c8
DM
546 { action => 'PARAM', proto => 'tcp', dport => '43' },
547 ],
961b4928
DM
548};
549
550my $pve_fw_parsed_macros;
ebd54ae9 551my $pve_fw_macro_descr;
21a18e53 552my $pve_fw_macro_ipversion = {};
961b4928 553my $pve_fw_preferred_macro_names = {};
3a616aa0 554
fe3d79b4
WB
555my $FWACCEPTMARK_ON = "0x80000000/0x80000000";
556my $FWACCEPTMARK_OFF = "0x00000000/0x80000000";
557
5547adf7 558my $pve_std_chains = {};
cfd7cd9c
TW
559my $pve_std_chains_conf = {};
560$pve_std_chains_conf->{4} = {
d8f2505e 561 'PVEFW-SET-ACCEPT-MARK' => [
044409e5 562 { target => "-j MARK --set-mark $FWACCEPTMARK_ON" },
d8f2505e 563 ],
79929d9c
DM
564 'PVEFW-DropBroadcast' => [
565 # same as shorewall 'Broadcast'
566 # simply DROP BROADCAST/MULTICAST/ANYCAST
567 # we can use this to reduce logging
568 { action => 'DROP', dsttype => 'BROADCAST' },
569 { action => 'DROP', dsttype => 'MULTICAST' },
570 { action => 'DROP', dsttype => 'ANYCAST' },
cbb5d6f3 571 { action => 'DROP', dest => '224.0.0.0/4' },
79929d9c
DM
572 ],
573 'PVEFW-reject' => [
574 # same as shorewall 'reject'
575 { action => 'DROP', dsttype => 'BROADCAST' },
cbb5d6f3 576 { action => 'DROP', source => '224.0.0.0/4' },
79929d9c 577 { action => 'DROP', proto => 'icmp' },
044409e5
TW
578 { match => '-p tcp', target => '-j REJECT --reject-with tcp-reset' },
579 { match => '-p udp', target => '-j REJECT --reject-with icmp-port-unreachable' },
580 { match => '-p icmp', target => '-j REJECT --reject-with icmp-host-unreachable' },
581 { target => '-j REJECT --reject-with icmp-host-prohibited' },
79929d9c
DM
582 ],
583 'PVEFW-Drop' => [
cbb5d6f3 584 # same as shorewall 'Drop', which is equal to DROP,
79929d9c
DM
585 # but REJECT/DROP some packages to reduce logging,
586 # and ACCEPT critical ICMP types
587 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
588 # we are not interested in BROADCAST/MULTICAST/ANYCAST
589 { action => 'PVEFW-DropBroadcast' },
590 # ACCEPT critical ICMP types
591 { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' },
592 { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' },
593 # Drop packets with INVALID state
044409e5 594 { action => 'DROP', match => '-m conntrack --ctstate INVALID', },
79929d9c 595 # Drop Microsoft SMB noise
e4882cff
TW
596 { action => 'DROP', proto => 'udp', dport => '135,445' },
597 { action => 'DROP', proto => 'udp', dport => '137:139' },
79929d9c 598 { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 },
e4882cff 599 { action => 'DROP', proto => 'tcp', dport => '135,139,445' },
79929d9c
DM
600 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
601 # Drop new/NotSyn traffic so that it doesn't get logged
044409e5 602 { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' },
79929d9c
DM
603 # Drop DNS replies
604 { action => 'DROP', proto => 'udp', sport => 53 },
605 ],
606 'PVEFW-Reject' => [
cbb5d6f3 607 # same as shorewall 'Reject', which is equal to Reject,
79929d9c
DM
608 # but REJECT/DROP some packages to reduce logging,
609 # and ACCEPT critical ICMP types
610 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
611 # we are not interested in BROADCAST/MULTICAST/ANYCAST
612 { action => 'PVEFW-DropBroadcast' },
613 # ACCEPT critical ICMP types
614 { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' },
615 { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' },
616 # Drop packets with INVALID state
044409e5 617 { action => 'DROP', match => '-m conntrack --ctstate INVALID', },
79929d9c 618 # Drop Microsoft SMB noise
e4882cff 619 { action => 'PVEFW-reject', proto => 'udp', dport => '135,445' },
79929d9c
DM
620 { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'},
621 { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 },
e4882cff 622 { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445' },
79929d9c
DM
623 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
624 # Drop new/NotSyn traffic so that it doesn't get logged
044409e5 625 { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' },
79929d9c
DM
626 # Drop DNS replies
627 { action => 'DROP', proto => 'udp', sport => 53 },
628 ],
d86659ef
DM
629 'PVEFW-tcpflags' => [
630 # same as shorewall tcpflags action.
631 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
044409e5
TW
632 { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG', target => '-g PVEFW-logflags' },
633 { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE', target => '-g PVEFW-logflags' },
634 { match => '-p tcp -m tcp --tcp-flags SYN,RST SYN,RST', target => '-g PVEFW-logflags' },
635 { match => '-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN', target => '-g PVEFW-logflags' },
636 { match => '-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN', target => '-g PVEFW-logflags' },
d86659ef 637 ],
bee633ab
DM
638 'PVEFW-smurfs' => [
639 # same as shorewall smurfs action
640 # Filter packets for smurfs (packets with a broadcast address as the source).
044409e5
TW
641 { match => '-s 0.0.0.0/32', target => '-j RETURN' }, # allow DHCP
642 { match => '-m addrtype --src-type BROADCAST', target => '-g PVEFW-smurflog' },
643 { match => '-s 224.0.0.0/4', target => '-g PVEFW-smurflog' },
644 ],
645 'PVEFW-smurflog' => [
646 { action => 'DROP', logmsg => 'DROP: ' },
647 ],
648 'PVEFW-logflags' => [
649 { action => 'DROP', logmsg => 'DROP: ' },
bee633ab 650 ],
d8f2505e
DM
651};
652
cfd7cd9c 653$pve_std_chains_conf->{6} = {
47a79ff2 654 'PVEFW-SET-ACCEPT-MARK' => [
044409e5 655 { target => "-j MARK --set-mark $FWACCEPTMARK_ON" },
47a79ff2
AD
656 ],
657 'PVEFW-DropBroadcast' => [
044409e5
TW
658 # same as shorewall 'Broadcast'
659 # simply DROP BROADCAST/MULTICAST/ANYCAST
660 # we can use this to reduce logging
661 #{ action => 'DROP', dsttype => 'BROADCAST' }, #no broadcast in ipv6
649cd835
DM
662 # ipv6 addrtype does not work with kernel 2.6.32
663 #{ action => 'DROP', dsttype => 'MULTICAST' },
044409e5
TW
664 #{ action => 'DROP', dsttype => 'ANYCAST' },
665 { action => 'DROP', dest => 'ff00::/8' },
666 #{ action => 'DROP', dest => '224.0.0.0/4' },
47a79ff2
AD
667 ],
668 'PVEFW-reject' => [
649cd835 669 { action => 'DROP', proto => 'icmpv6' },
044409e5 670 { match => '-p tcp', target => '-j REJECT --reject-with tcp-reset' },
58ca8ec0
AD
671 { match => '-p udp', target => '-j REJECT --reject-with icmp6-port-unreachable' },
672 { target => '-j REJECT --reject-with icmp6-adm-prohibited' },
47a79ff2
AD
673 ],
674 'PVEFW-Drop' => [
044409e5
TW
675 # same as shorewall 'Drop', which is equal to DROP,
676 # but REJECT/DROP some packages to reduce logging,
677 # and ACCEPT critical ICMP types
649cd835 678 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
044409e5
TW
679 # we are not interested in BROADCAST/MULTICAST/ANYCAST
680 { action => 'PVEFW-DropBroadcast' },
681 # ACCEPT critical ICMP types
682 { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' },
683 { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' },
684 { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' },
685 # Drop packets with INVALID state
686 { action => 'DROP', match => '-m conntrack --ctstate INVALID', },
687 # Drop Microsoft SMB noise
e4882cff 688 { action => 'DROP', proto => 'udp', dport => '135,445' },
649cd835
DM
689 { action => 'DROP', proto => 'udp', dport => '137:139'},
690 { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 },
e4882cff 691 { action => 'DROP', proto => 'tcp', dport => '135,139,445' },
649cd835 692 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
044409e5
TW
693 # Drop new/NotSyn traffic so that it doesn't get logged
694 { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' },
695 # Drop DNS replies
649cd835 696 { action => 'DROP', proto => 'udp', sport => 53 },
47a79ff2
AD
697 ],
698 'PVEFW-Reject' => [
044409e5
TW
699 # same as shorewall 'Reject', which is equal to Reject,
700 # but REJECT/DROP some packages to reduce logging,
701 # and ACCEPT critical ICMP types
702 { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth'
703 # we are not interested in BROADCAST/MULTICAST/ANYCAST
704 { action => 'PVEFW-DropBroadcast' },
705 # ACCEPT critical ICMP types
706 { action => 'ACCEPT', proto => 'icmpv6', dport => 'destination-unreachable' },
707 { action => 'ACCEPT', proto => 'icmpv6', dport => 'time-exceeded' },
708 { action => 'ACCEPT', proto => 'icmpv6', dport => 'packet-too-big' },
709 # Drop packets with INVALID state
710 { action => 'DROP', match => '-m conntrack --ctstate INVALID', },
711 # Drop Microsoft SMB noise
e4882cff
TW
712 { action => 'PVEFW-reject', proto => 'udp', dport => '135,445' },
713 { action => 'PVEFW-reject', proto => 'udp', dport => '137:139' },
044409e5 714 { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 },
e4882cff 715 { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445' },
044409e5
TW
716 { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP
717 # Drop new/NotSyn traffic so that it doesn't get logged
718 { action => 'DROP', match => '-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN' },
719 # Drop DNS replies
720 { action => 'DROP', proto => 'udp', sport => 53 },
47a79ff2
AD
721 ],
722 'PVEFW-tcpflags' => [
044409e5
TW
723 # same as shorewall tcpflags action.
724 # Packets arriving on this interface are checked for som illegal combinations of TCP flags
725 { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG', target => '-g PVEFW-logflags' },
726 { match => '-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE', target => '-g PVEFW-logflags' },
727 { match => '-p tcp -m tcp --tcp-flags SYN,RST SYN,RST', target => '-g PVEFW-logflags' },
728 { match => '-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN', target => '-g PVEFW-logflags' },
729 { match => '-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN', target => '-g PVEFW-logflags' },
730 ],
731 'PVEFW-logflags' => [
732 { action => 'DROP', logmsg => 'DROP: ' },
47a79ff2 733 ],
47a79ff2
AD
734};
735
4b586518
DM
736# iptables -p icmp -h
737my $icmp_type_names = {
738 any => 1,
739 'echo-reply' => 1,
740 'destination-unreachable' => 1,
741 'network-unreachable' => 1,
742 'host-unreachable' => 1,
743 'protocol-unreachable' => 1,
744 'port-unreachable' => 1,
745 'fragmentation-needed' => 1,
746 'source-route-failed' => 1,
747 'network-unknown' => 1,
748 'host-unknown' => 1,
749 'network-prohibited' => 1,
750 'host-prohibited' => 1,
751 'TOS-network-unreachable' => 1,
752 'TOS-host-unreachable' => 1,
753 'communication-prohibited' => 1,
754 'host-precedence-violation' => 1,
755 'precedence-cutoff' => 1,
756 'source-quench' => 1,
757 'redirect' => 1,
758 'network-redirect' => 1,
759 'host-redirect' => 1,
760 'TOS-network-redirect' => 1,
761 'TOS-host-redirect' => 1,
762 'echo-request' => 1,
763 'router-advertisement' => 1,
764 'router-solicitation' => 1,
765 'time-exceeded' => 1,
766 'ttl-zero-during-transit' => 1,
767 'ttl-zero-during-reassembly' => 1,
768 'parameter-problem' => 1,
769 'ip-header-bad' => 1,
770 'required-option-missing' => 1,
771 'timestamp-request' => 1,
772 'timestamp-reply' => 1,
773 'address-mask-request' => 1,
774 'address-mask-reply' => 1,
775};
776
041b9277
DM
777# ip6tables -p icmpv6 -h
778
779my $icmpv6_type_names = {
780 'any' => 1,
781 'destination-unreachable' => 1,
782 'no-route' => 1,
783 'communication-prohibited' => 1,
784 'address-unreachable' => 1,
785 'port-unreachable' => 1,
786 'packet-too-big' => 1,
787 'time-exceeded' => 1,
788 'ttl-zero-during-transit' => 1,
789 'ttl-zero-during-reassembly' => 1,
790 'parameter-problem' => 1,
791 'bad-header' => 1,
792 'unknown-header-type' => 1,
793 'unknown-option' => 1,
794 'echo-request' => 1,
795 'echo-reply' => 1,
796 'router-solicitation' => 1,
797 'router-advertisement' => 1,
593604cc 798 'neighbor-solicitation' => 1,
041b9277 799 'neighbour-solicitation' => 1,
593604cc 800 'neighbor-advertisement' => 1,
041b9277
DM
801 'neighbour-advertisement' => 1,
802 'redirect' => 1,
803};
804
54cd19a8 805sub init_firewall_macros {
6158271d 806
961b4928 807 $pve_fw_parsed_macros = {};
9aab3127 808
21a18e53
WB
809 my $parse = sub {
810 my ($k, $macro) = @_;
e5076eee 811 my $lc_name = lc($k);
21a18e53
WB
812 $pve_fw_macro_ipversion->{$k} = 0;
813 while (!ref($macro->[0])) {
814 my $desc = shift @$macro;
815 if ($desc eq 'ipv4only') {
816 $pve_fw_macro_ipversion->{$k} = 4;
817 } elsif ($desc eq 'ipv6only') {
818 $pve_fw_macro_ipversion->{$k} = 6;
819 } else {
820 $pve_fw_macro_descr->{$k} = $desc;
821 }
ebd54ae9 822 }
e5076eee
DM
823 $pve_fw_preferred_macro_names->{$lc_name} = $k;
824 $pve_fw_parsed_macros->{$k} = $macro;
21a18e53
WB
825 };
826
827 foreach my $k (keys %$pve_fw_macros) {
828 &$parse($k, $pve_fw_macros->{$k});
829 }
830
831 foreach my $k (keys %$pve_ipv6fw_macros) {
832 next if $pve_fw_parsed_macros->{$k};
833 &$parse($k, $pve_ipv6fw_macros->{$k});
834 $pve_fw_macro_ipversion->{$k} = 6;
961b4928 835 }
9aab3127
DM
836}
837
54cd19a8
DM
838init_firewall_macros();
839
ebd54ae9
DM
840sub get_macros {
841 return wantarray ? ($pve_fw_parsed_macros, $pve_fw_macro_descr): $pve_fw_parsed_macros;
842}
843
fcba0beb
DM
844my $etc_services;
845
846sub get_etc_services {
847
848 return $etc_services if $etc_services;
849
850 my $filename = "/etc/services";
851
852 my $fh = IO::File->new($filename, O_RDONLY);
853 if (!$fh) {
854 warn "unable to read '$filename' - $!\n";
855 return {};
856 }
857
858 my $services = {};
859
860 while (my $line = <$fh>) {
861 chomp ($line);
862 next if $line =~m/^#/;
863 next if ($line =~m/^\s*$/);
864
d50f24ea 865 if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp|sctp).*$!) {
fcba0beb 866 $services->{byid}->{$2}->{name} = $1;
35b66e9d 867 $services->{byid}->{$2}->{port} = $2;
fcba0beb
DM
868 $services->{byid}->{$2}->{$3} = 1;
869 $services->{byname}->{$1} = $services->{byid}->{$2};
870 }
871 }
872
873 close($fh);
874
6158271d
DM
875 $etc_services = $services;
876
3a616aa0 877
fcba0beb
DM
878 return $etc_services;
879}
880
58bc569d
WB
881sub parse_protocol_file {
882 my ($filename) = @_;
fcba0beb
DM
883
884 my $fh = IO::File->new($filename, O_RDONLY);
885 if (!$fh) {
886 warn "unable to read '$filename' - $!\n";
887 return {};
888 }
889
890 my $protocols = {};
891
892 while (my $line = <$fh>) {
893 chomp ($line);
894 next if $line =~m/^#/;
895 next if ($line =~m/^\s*$/);
896
d6dd6e96 897 if ($line =~ m!^(\S+)\s+(\d+)(?:\s+.*)?$!) {
fcba0beb
DM
898 $protocols->{byid}->{$2}->{name} = $1;
899 $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
900 }
901 }
902
903 close($fh);
904
58bc569d
WB
905 return $protocols;
906}
907
908my $etc_protocols;
909
910sub get_etc_protocols {
911 return $etc_protocols if $etc_protocols;
912
913 my $protocols = parse_protocol_file('/etc/protocols');
914
041b9277
DM
915 # add special case for ICMP v6
916 $protocols->{byid}->{icmpv6}->{name} = "icmpv6";
917 $protocols->{byname}->{icmpv6} = $protocols->{byid}->{icmpv6};
918
fcba0beb
DM
919 $etc_protocols = $protocols;
920
921 return $etc_protocols;
922}
923
b452ea10
WB
924my $etc_ethertypes;
925
926sub get_etc_ethertypes {
927 $etc_ethertypes = parse_protocol_file('/etc/ethertypes')
928 if !$etc_ethertypes;
929 return $etc_ethertypes;
930}
931
525778d7 932my $__local_network;
93be4333 933
525778d7 934sub local_network {
ac633d30 935 my ($new_value) = @_;
93be4333 936
525778d7 937 $__local_network = $new_value if defined($new_value);
ac633d30 938
525778d7 939 return $__local_network if defined($__local_network);
93be4333
DM
940
941 eval {
942 my $nodename = PVE::INotify::nodename();
943
944 my $ip = PVE::Cluster::remote_node_ip($nodename);
945
946 my $testip = Net::IP->new($ip);
bfc488f6 947
5dc356af
WB
948 my $isv6 = $testip->version == 6;
949 my $routes = $isv6 ? PVE::ProcFSTools::read_proc_net_ipv6_route()
950 : PVE::ProcFSTools::read_proc_net_route();
93be4333 951 foreach my $entry (@$routes) {
5dc356af
WB
952 my $mask;
953 if ($isv6) {
954 $mask = $entry->{prefix};
15c80000 955 next if !$mask; # skip the default route...
5dc356af 956 } else {
2e1ae6b9 957 $mask = $PVE::Network::ipv4_mask_hash_localnet->{$entry->{mask}};
5dc356af
WB
958 next if !defined($mask);
959 }
93be4333
DM
960 my $cidr = "$entry->{dest}/$mask";
961 my $testnet = Net::IP->new($cidr);
15c80000
WB
962 my $overlap = $testnet->overlaps($testip);
963 if ($overlap == $Net::IP::IP_B_IN_A_OVERLAP ||
964 $overlap == $Net::IP::IP_IDENTICAL)
965 {
525778d7 966 $__local_network = $cidr;
93be4333
DM
967 return;
968 }
969 }
970 };
971 warn $@ if $@;
972
525778d7 973 return $__local_network;
93be4333
DM
974}
975
6d959e3f
DM
976# ipset names are limited to 31 characters,
977# and we use '-v4' or '-v6' to indicate IP versions,
978# and we use '_swap' suffix for atomic update,
979# for example PVEFW-${VMID}-${ipset_name}_swap
30150dca 980
88c26d5e 981my $max_iptables_ipset_name_length = 31 - length("PVEFW-") - length("_swap");
708ba714
DM
982
983sub compute_ipset_chain_name {
88c26d5e 984 my ($vmid, $ipset_name, $ipversion) = @_;
708ba714
DM
985
986 $vmid = 0 if !defined($vmid);
987
88c26d5e 988 my $id = "$vmid-${ipset_name}-v$ipversion";
708ba714 989
88c26d5e 990 if (length($id) > $max_iptables_ipset_name_length) {
708ba714
DM
991 $id = PVE::Tools::fnv31a_hex($id);
992 }
993
994 return "PVEFW-$id";
995}
996
b692f42c
DM
997sub compute_ipfilter_ipset_name {
998 my ($iface) = @_;
999
1000 return "ipfilter-$iface";
1001}
1002
ecbea048
DM
1003sub parse_address_list {
1004 my ($str) = @_;
1005
e2c62733
DM
1006 if ($str =~ m/^(\+)(\S+)$/) { # ipset ref
1007 die "ipset name too long\n" if length($str) > ($max_ipset_name_length + 1);
1008 return;
1009 }
1010
1011 if ($str =~ m/^${ip_alias_pattern}$/) {
1012 die "alias name too long\n" if length($str) > $max_alias_name_length;
1013 return;
1014 }
d72beb8e 1015
3162af6b
DM
1016 my $count = 0;
1017 my $iprange = 0;
a589b6ac 1018 my $ipversion;
7697c041 1019
55b47371
WB
1020 my @elements = split(/,/, $str);
1021 die "extraneous commas in list\n" if $str ne join(',', @elements);
1022 foreach my $elem (@elements) {
3162af6b 1023 $count++;
c344e509
DM
1024 my $ip = Net::IP->new($elem);
1025 if (!$ip) {
ecbea048
DM
1026 my $err = Net::IP::Error();
1027 die "invalid IP address: $err\n";
1028 }
3162af6b 1029 $iprange = 1 if $elem =~ m/-/;
a589b6ac 1030
c344e509 1031 my $new_ipversion = Net::IP::ip_is_ipv6($ip->ip()) ? 6 : 4;
a589b6ac
DM
1032
1033 die "detected mixed ipv4/ipv6 addresses in address list '$str'\n"
006490cb 1034 if $ipversion && ($new_ipversion != $ipversion);
a589b6ac
DM
1035
1036 $ipversion = $new_ipversion;
ecbea048 1037 }
e34d0e58 1038
5163367b 1039 die "you can't use a range in a list\n" if $iprange && $count > 1;
a589b6ac 1040
7697c041 1041 return $ipversion;
ecbea048 1042}
dddd9413 1043
fcba0beb 1044sub parse_port_name_number_or_range {
a1c04f71 1045 my ($str, $dport) = @_;
fcba0beb
DM
1046
1047 my $services = PVE::Firewall::get_etc_services();
7ca36671 1048 my $count = 0;
041b7e8e
DM
1049 my $icmp_port = 0;
1050
55b47371
WB
1051 my @elements = split(/,/, $str);
1052 die "extraneous commas in list\n" if $str ne join(',', @elements);
1053 foreach my $item (@elements) {
aced7e7d 1054 if ($item =~ m/^([0-9]+):([0-9]+)$/) {
6a241ca7 1055 $count += 2;
7ca36671
DM
1056 my ($port1, $port2) = ($1, $2);
1057 die "invalid port '$port1'\n" if $port1 > 65535;
1058 die "invalid port '$port2'\n" if $port2 > 65535;
12be0dfe 1059 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
aced7e7d 1060 } elsif ($item =~ m/^([0-9]+)$/) {
6a241ca7 1061 $count += 1;
7ca36671
DM
1062 my $port = $1;
1063 die "invalid port '$port'\n" if $port > 65535;
1064 } else {
a1c04f71 1065 if ($dport && $icmp_type_names->{$item}) {
041b7e8e 1066 $icmp_port = 1;
a1c04f71 1067 } elsif ($dport && $icmpv6_type_names->{$item}) {
041b9277 1068 $icmp_port = 1;
041b7e8e 1069 } else {
e34d0e58 1070 die "invalid port '$item'\n" if !$services->{byname}->{$item};
041b7e8e 1071 }
fcba0beb
DM
1072 }
1073 }
1074
6a241ca7
WB
1075 die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 0;
1076
1077 # I really don't like to use the word number here, but it's the only thing
1078 # that makes sense in a literal way. The range 1:100 counts as 2, not as
1079 # one and not as 100...
1080 die "too many entries in port list (> 15 numbers)\n"
1081 if $count > 15;
041b7e8e 1082
f776d6de 1083 return (scalar(@elements) > 1);
fcba0beb
DM
1084}
1085
a1c04f71
WB
1086PVE::JSONSchema::register_format('pve-fw-sport-spec', \&pve_fw_verify_sport_spec);
1087sub pve_fw_verify_sport_spec {
1088 my ($portstr) = @_;
1089
1090 parse_port_name_number_or_range($portstr, 0);
1091
1092 return $portstr;
1093}
1094
1095PVE::JSONSchema::register_format('pve-fw-dport-spec', \&pve_fw_verify_dport_spec);
1096sub pve_fw_verify_dport_spec {
54cd19a8
DM
1097 my ($portstr) = @_;
1098
a1c04f71 1099 parse_port_name_number_or_range($portstr, 1);
54cd19a8
DM
1100
1101 return $portstr;
1102}
1103
d31689ee
DM
1104PVE::JSONSchema::register_format('pve-fw-addr-spec', \&pve_fw_verify_addr_spec);
1105sub pve_fw_verify_addr_spec {
54cd19a8
DM
1106 my ($list) = @_;
1107
1108 parse_address_list($list);
1109
1110 return $list;
1111}
1112
1113PVE::JSONSchema::register_format('pve-fw-protocol-spec', \&pve_fw_verify_protocol_spec);
1114sub pve_fw_verify_protocol_spec {
1115 my ($proto) = @_;
1116
1117 my $protocols = get_etc_protocols();
1118
1119 die "unknown protocol '$proto'\n" if $proto &&
1120 !(defined($protocols->{byname}->{$proto}) ||
1121 defined($protocols->{byid}->{$proto}));
1122
1123 return $proto;
1124}
1125
1126
d1c53b3e 1127# helper function for API
d1c53b3e 1128
5d38d64f
DM
1129sub copy_opject_with_digest {
1130 my ($object) = @_;
1131
1132 my $sha = Digest::SHA->new('sha1');
1133
1134 my $res = {};
1135 foreach my $k (sort keys %$object) {
1136 my $v = $object->{$k};
0365eb68 1137 next if !defined($v);
5d38d64f
DM
1138 $res->{$k} = $v;
1139 $sha->add($k, ':', $v, "\n");
1140 }
1141
55e686c7 1142 my $digest = $sha->hexdigest;
5d38d64f
DM
1143
1144 $res->{digest} = $digest;
1145
1146 return wantarray ? ($res, $digest) : $res;
1147}
1148
1149sub copy_list_with_digest {
1150 my ($list) = @_;
1151
1152 my $sha = Digest::SHA->new('sha1');
1153
1154 my $res = [];
1155 foreach my $entry (@$list) {
1156 my $data = {};
1157 foreach my $k (sort keys %$entry) {
1158 my $v = $entry->{$k};
0365eb68 1159 next if !defined($v);
5d38d64f 1160 $data->{$k} = $v;
6d9246e7 1161 # Note: digest ignores refs ($rule->{errors})
e83f0d00
DC
1162 # since Digest::SHA expects a series of bytes,
1163 # we have to encode the value here to prevent errors when
1164 # using utf8 characters (eg. in comments)
1165 $sha->add($k, ':', encode_utf8($v), "\n") if !ref($v); ;
5d38d64f
DM
1166 }
1167 push @$res, $data;
1168 }
1169
55e686c7 1170 my $digest = $sha->hexdigest;
5d38d64f
DM
1171
1172 foreach my $entry (@$res) {
1173 $entry->{digest} = $digest;
1174 }
1175
1176 return wantarray ? ($res, $digest) : $res;
1177}
1178
e313afe0
DM
1179our $cluster_option_properties = {
1180 enable => {
1181 description => "Enable or disable the firewall cluster wide.",
1182 type => 'integer',
1183 minimum => 0,
1184 optional => 1,
1185 },
2549e7ef
SI
1186 ebtables => {
1187 description => "Enable ebtables rules cluster wide.",
1188 type => 'boolean',
1189 default => 1,
1190 optional => 1,
1191 },
e313afe0
DM
1192 policy_in => {
1193 description => "Input policy.",
1194 type => 'string',
1195 optional => 1,
1196 enum => ['ACCEPT', 'REJECT', 'DROP'],
1197 },
1198 policy_out => {
1199 description => "Output policy.",
1200 type => 'string',
1201 optional => 1,
1202 enum => ['ACCEPT', 'REJECT', 'DROP'],
1203 },
cc37e000
TL
1204 log_ratelimit => {
1205 description => "Log ratelimiting settings",
1206 type => 'string', format => {
1207 enable => {
1208 default_key => 1,
1209 description => 'Enable or disable log rate limiting',
1210 type => 'boolean',
1211 default => '1',
1212 },
1213 rate => {
1214 type => 'string',
1215 description => 'Frequency with which the burst bucket gets refilled',
1216 optional => 1,
1217 pattern => '[1-9][0-9]*\/(second|minute|hour|day)',
1218 format_description => 'rate',
1219 default => '1/second',
1220 },
1221 burst => {
1222 type => 'integer',
1223 minimum => 0,
1224 optional => 1,
1225 description => 'Inital burst of packages which will get logged',
1226 default => 5,
1227 },
1228 },
1229 optional => 1,
1230 },
e313afe0
DM
1231};
1232
1233our $host_option_properties = {
1234 enable => {
1235 description => "Enable host firewall rules.",
1236 type => 'boolean',
1237 optional => 1,
1238 },
1239 log_level_in => get_standard_option('pve-fw-loglevel', {
1240 description => "Log level for incoming traffic." }),
1241 log_level_out => get_standard_option('pve-fw-loglevel', {
1242 description => "Log level for outgoing traffic." }),
1243 tcp_flags_log_level => get_standard_option('pve-fw-loglevel', {
1244 description => "Log level for illegal tcp flags filter." }),
1245 smurf_log_level => get_standard_option('pve-fw-loglevel', {
1246 description => "Log level for SMURFS filter." }),
1247 nosmurfs => {
1248 description => "Enable SMURFS filter.",
1249 type => 'boolean',
1250 optional => 1,
1251 },
1252 tcpflags => {
1253 description => "Filter illegal combinations of TCP flags.",
1254 type => 'boolean',
1255 optional => 1,
1256 },
1257 nf_conntrack_max => {
1258 description => "Maximum number of tracked connections.",
1259 type => 'integer',
1260 optional => 1,
1261 minimum => 32768,
1262 },
1263 nf_conntrack_tcp_timeout_established => {
1264 description => "Conntrack established timeout.",
1265 type => 'integer',
1266 optional => 1,
1267 minimum => 7875,
1268 },
1269 ndp => {
1270 description => "Enable NDP.",
1271 type => 'boolean',
1272 optional => 1,
1273 },
27c49e25
CE
1274 nf_conntrack_allow_invalid => {
1275 description => "Allow invalid packets on connection tracking.",
1276 type => 'boolean',
1277 default => 0,
1278 optional => 1,
1279 },
e17d3eec
ML
1280 log_nf_conntrack => {
1281 description => "Enable logging of conntrack information.",
1282 type => 'boolean',
1283 default => 0,
1284 optional => 1
1285 },
e313afe0
DM
1286};
1287
1288our $vm_option_properties = {
1289 enable => {
1290 description => "Enable/disable firewall rules.",
1291 type => 'boolean',
1292 optional => 1,
1293 },
1294 macfilter => {
1295 description => "Enable/disable MAC address filter.",
1296 type => 'boolean',
1297 optional => 1,
1298 },
1299 dhcp => {
1300 description => "Enable DHCP.",
1301 type => 'boolean',
1302 optional => 1,
1303 },
1304 ndp => {
1305 description => "Enable NDP.",
1306 type => 'boolean',
1307 optional => 1,
1308 },
1309 radv => {
1310 description => "Allow sending Router Advertisement.",
1311 type => 'boolean',
1312 optional => 1,
1313 },
1314 ipfilter => {
1315 description => "Enable default IP filters. " .
1316 "This is equivalent to adding an empty ipfilter-net<id> ipset " .
1317 "for every interface. Such ipsets implicitly contain sane default " .
1318 "restrictions such as restricting IPv6 link local addresses to " .
1319 "the one derived from the interface's MAC address. For containers " .
1320 "the configured IP addresses will be implicitly added.",
1321 type => 'boolean',
1322 optional => 1,
1323 },
1324 policy_in => {
1325 description => "Input policy.",
1326 type => 'string',
1327 optional => 1,
1328 enum => ['ACCEPT', 'REJECT', 'DROP'],
1329 },
1330 policy_out => {
1331 description => "Output policy.",
1332 type => 'string',
1333 optional => 1,
1334 enum => ['ACCEPT', 'REJECT', 'DROP'],
1335 },
1336 log_level_in => get_standard_option('pve-fw-loglevel', {
1337 description => "Log level for incoming traffic." }),
1338 log_level_out => get_standard_option('pve-fw-loglevel', {
1339 description => "Log level for outgoing traffic." }),
1340
1341};
1342
fb060a52 1343
7a332151 1344my $addr_list_descr = "This can refer to a single IP address, an IP set ('+ipsetname') or an IP alias definition. You can also specify an address range like '20.34.101.207-201.3.9.99', or a list of IP addresses and networks (entries are separated by comma). Please do not mix IPv4 and IPv6 addresses inside such lists.";
fb060a52 1345
7a332151 1346my $port_descr = "You can use service names or simple numbers (0-65535), as defined in '/etc/services'. Port ranges can be specified with '\\d+:\\d+', for example '80:85', and you can use comma separated list to match several ports or ranges.";
fb060a52 1347
9c7e0858
DM
1348my $rule_properties = {
1349 pos => {
1350 description => "Update rule at position <pos>.",
1351 type => 'integer',
1352 minimum => 0,
1353 optional => 1,
1354 },
c71e785f 1355 digest => get_standard_option('pve-config-digest'),
9c7e0858 1356 type => {
e50429af 1357 description => "Rule type.",
9c7e0858
DM
1358 type => 'string',
1359 optional => 1,
1360 enum => ['in', 'out', 'group'],
1361 },
1362 action => {
c14dacdf 1363 description => "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.",
9c7e0858
DM
1364 type => 'string',
1365 optional => 1,
44be8ceb 1366 pattern => $security_group_name_pattern,
c14dacdf
DM
1367 maxLength => 20,
1368 minLength => 2,
9c7e0858 1369 },
e5076eee 1370 macro => {
e50429af 1371 description => "Use predefined standard macro.",
e5076eee
DM
1372 type => 'string',
1373 optional => 1,
54cd19a8 1374 maxLength => 128,
e5076eee 1375 },
fb060a52 1376 iface => get_standard_option('pve-iface', {
7ccaa5f1 1377 description => "Network interface name. You have to use network configuration key names for VMs and containers ('net\\d+'). Host related rules can use arbitrary strings.",
fb060a52
DM
1378 optional => 1
1379 }),
9c7e0858 1380 source => {
fb060a52 1381 description => "Restrict packet source address. $addr_list_descr",
d31689ee 1382 type => 'string', format => 'pve-fw-addr-spec',
9c7e0858
DM
1383 optional => 1,
1384 },
1385 dest => {
fb060a52 1386 description => "Restrict packet destination address. $addr_list_descr",
d31689ee 1387 type => 'string', format => 'pve-fw-addr-spec',
9c7e0858
DM
1388 optional => 1,
1389 },
1390 proto => {
fb060a52 1391 description => "IP protocol. You can use protocol names ('tcp'/'udp') or simple numbers, as defined in '/etc/protocols'.",
54cd19a8 1392 type => 'string', format => 'pve-fw-protocol-spec',
9c7e0858
DM
1393 optional => 1,
1394 },
1395 enable => {
e50429af 1396 description => "Flag to enable/disable a rule.",
72d055fc
AG
1397 type => 'integer',
1398 minimum => 0,
9c7e0858
DM
1399 optional => 1,
1400 },
3489f8a2
CE
1401 log => get_standard_option('pve-fw-loglevel', {
1402 description => "Log level for firewall rule.",
1403 }),
9c7e0858 1404 sport => {
fb060a52 1405 description => "Restrict TCP/UDP source port. $port_descr",
a1c04f71 1406 type => 'string', format => 'pve-fw-sport-spec',
9c7e0858
DM
1407 optional => 1,
1408 },
1409 dport => {
fb060a52 1410 description => "Restrict TCP/UDP destination port. $port_descr",
a1c04f71 1411 type => 'string', format => 'pve-fw-dport-spec',
9c7e0858
DM
1412 optional => 1,
1413 },
1414 comment => {
e50429af 1415 description => "Descriptive comment.",
9c7e0858
DM
1416 type => 'string',
1417 optional => 1,
1418 },
1419};
1420
1421sub add_rule_properties {
1422 my ($properties) = @_;
1423
1424 foreach my $k (keys %$rule_properties) {
3655b01f
DM
1425 my $h = $rule_properties->{$k};
1426 # copy data, so that we can modify later without side effects
1427 foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; }
9c7e0858 1428 }
cbb5d6f3 1429
9c7e0858
DM
1430 return $properties;
1431}
1432
5b7974df
DM
1433sub delete_rule_properties {
1434 my ($rule, $delete_str) = @_;
e34d0e58 1435
5b7974df
DM
1436 foreach my $opt (PVE::Tools::split_list($delete_str)) {
1437 raise_param_exc({ 'delete' => "no such property ('$opt')"})
1438 if !defined($rule_properties->{$opt});
1439 raise_param_exc({ 'delete' => "unable to delete required property '$opt'"})
1440 if $opt eq 'type' || $opt eq 'action';
1441 delete $rule->{$opt};
1442 }
1443
1444 return $rule;
1445}
1446
9a2745a0 1447my $apply_macro = sub {
35d1d6da 1448 my ($macro_name, $param, $verify, $ipversion) = @_;
9a2745a0
DM
1449
1450 my $macro_rules = $pve_fw_parsed_macros->{$macro_name};
1451 die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen
1452
35d1d6da
DM
1453 if ($ipversion && ($ipversion == 6) && $pve_ipv6fw_macros->{$macro_name}) {
1454 $macro_rules = $pve_ipv6fw_macros->{$macro_name};
1455 }
1456
21a18e53 1457 # skip macros which are specific to another ipversion
ff5d050e
AG
1458 if ($ipversion && (my $required = $pve_fw_macro_ipversion->{$macro_name})) {
1459 return if $ipversion != $required;
1460 }
21a18e53 1461
9a2745a0
DM
1462 my $rules = [];
1463
1464 foreach my $templ (@$macro_rules) {
1465 my $rule = {};
1466 my $param_used = {};
1467 foreach my $k (keys %$templ) {
1468 my $v = $templ->{$k};
1469 if ($v eq 'PARAM') {
1470 $v = $param->{$k};
1471 $param_used->{$k} = 1;
1472 } elsif ($v eq 'DEST') {
1473 $v = $param->{dest};
1474 $param_used->{dest} = 1;
1475 } elsif ($v eq 'SOURCE') {
1476 $v = $param->{source};
1477 $param_used->{source} = 1;
1478 }
1479
1480 if (!defined($v)) {
1481 my $msg = "missing parameter '$k' in macro '$macro_name'";
e34d0e58 1482 raise_param_exc({ macro => $msg }) if $verify;
9a2745a0
DM
1483 die "$msg\n";
1484 }
1485 $rule->{$k} = $v;
1486 }
1487 foreach my $k (keys %$param) {
1488 next if $k eq 'macro';
1489 next if !defined($param->{$k});
1490 next if $param_used->{$k};
1491 if (defined($rule->{$k})) {
1492 if ($rule->{$k} ne $param->{$k}) {
1493 my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')";
e34d0e58 1494 raise_param_exc({ $k => $msg }) if $verify;
9a2745a0
DM
1495 die "$msg\n";
1496 }
1497 } else {
1498 $rule->{$k} = $param->{$k};
1499 }
1500 }
1501 push @$rules, $rule;
1502 }
1503
1504 return $rules;
1505};
1506
b6b8e6ad
DM
1507my $rule_env_iface_lookup = {
1508 'ct' => 1,
1509 'vm' => 1,
1510 'group' => 0,
1511 'cluster' => 1,
1512 'host' => 1,
1513};
1514
7ca36671 1515sub verify_rule {
a523e057 1516 my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_;
b6b8e6ad
DM
1517
1518 my $allow_groups = $rule_env eq 'group' ? 0 : 1;
bfc488f6 1519
b6b8e6ad
DM
1520 my $allow_iface = $rule_env_iface_lookup->{$rule_env};
1521 die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen
7ca36671 1522
a523e057
DM
1523 my $errors = $rule->{errors} || {};
1524
6d9246e7 1525 my $error_count = 0;
7ca36671 1526
6d9246e7
DM
1527 my $add_error = sub {
1528 my ($param, $msg) = @_;
d4cda423 1529 chomp $msg;
6d9246e7
DM
1530 raise_param_exc({ $param => $msg }) if !$noerr;
1531 $error_count++;
1532 $errors->{$param} = $msg if !$errors->{$param};
1533 };
1534
eea9d2a1
DM
1535 my $ipversion;
1536 my $set_ip_version = sub {
1537 my $vers = shift;
1538 if ($vers) {
1539 die "detected mixed ipv4/ipv6 adresses in rule\n"
1540 if $ipversion && ($vers != $ipversion);
1541 $ipversion = $vers;
1542 }
1543 };
1544
a523e057 1545 my $check_ipset_or_alias_property = sub {
ae029a88 1546 my ($name, $expected_ipversion) = @_;
a523e057
DM
1547
1548 if (my $value = $rule->{$name}) {
1549 if ($value =~ m/^\+/) {
4dfe04e6 1550 if ($value =~ m/^\+(${ipset_name_pattern})$/) {
bfc488f6 1551 &$add_error($name, "no such ipset '$1'")
a523e057 1552 if !($cluster_conf->{ipset}->{$1} || ($fw_conf && $fw_conf->{ipset}->{$1}));
bfc488f6 1553
a523e057 1554 } else {
351052d1 1555 &$add_error($name, "invalid ipset name '$value'");
a523e057
DM
1556 }
1557 } elsif ($value =~ m/^${ip_alias_pattern}$/){
1558 my $alias = lc($value);
bfc488f6 1559 &$add_error($name, "no such alias '$value'")
70e524eb 1560 if !($cluster_conf->{aliases}->{$alias} || ($fw_conf && $fw_conf->{aliases}->{$alias}));
04f5088f 1561 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
70e524eb
AD
1562 $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf;
1563
eea9d2a1 1564 &$set_ip_version($e->{ipversion});
a523e057
DM
1565 }
1566 }
1567 };
1568
6d9246e7
DM
1569 my $type = $rule->{type};
1570 my $action = $rule->{action};
bfc488f6 1571
6d9246e7
DM
1572 &$add_error('type', "missing property") if !$type;
1573 &$add_error('action', "missing property") if !$action;
1574
1575 if ($type) {
1576 if ($type eq 'in' || $type eq 'out') {
1577 &$add_error('action', "unknown action '$action'")
1578 if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/);
1579 } elsif ($type eq 'group') {
1580 &$add_error('type', "security groups not allowed")
1581 if !$allow_groups;
1582 &$add_error('action', "invalid characters in security group name")
1583 if $action && ($action !~ m/^${security_group_name_pattern}$/);
1584 } else {
1585 &$add_error('type', "unknown rule type '$type'");
1586 }
7ca36671 1587 }
e34d0e58 1588
dba740a9 1589 if ($rule->{iface}) {
bfc488f6 1590 &$add_error('type', "parameter -i not allowed for this rule type")
b6b8e6ad 1591 if !$allow_iface;
dba740a9 1592 eval { PVE::JSONSchema::pve_verify_iface($rule->{iface}); };
6d9246e7 1593 &$add_error('iface', $@) if $@;
fdefeeab 1594 if ($rule_env eq 'vm' || $rule_env eq 'ct') {
b6b8e6ad
DM
1595 &$add_error('iface', "value does not match the regex pattern 'net\\d+'")
1596 if $rule->{iface} !~ m/^net(\d+)$/;
b6b8e6ad
DM
1597 }
1598 }
7ca36671
DM
1599
1600 if ($rule->{macro}) {
6d9246e7
DM
1601 if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) {
1602 $rule->{macro} = $preferred_name;
1603 } else {
1604 &$add_error('macro', "unknown macro '$rule->{macro}'");
1605 }
1606 }
1607
1608 if ($rule->{proto}) {
1609 eval { pve_fw_verify_protocol_spec($rule->{proto}); };
1610 &$add_error('proto', $@) if $@;
041b9277
DM
1611 &$set_ip_version(4) if $rule->{proto} eq 'icmp';
1612 &$set_ip_version(6) if $rule->{proto} eq 'icmpv6';
6d9246e7 1613 }
7ca36671
DM
1614
1615 if ($rule->{dport}) {
a1c04f71 1616 eval { parse_port_name_number_or_range($rule->{dport}, 1); };
6d9246e7 1617 &$add_error('dport', $@) if $@;
48e3963e 1618 my $proto = $rule->{proto};
6d9246e7 1619 &$add_error('proto', "missing property - 'dport' requires this property")
48e3963e
WB
1620 if !$proto;
1621 &$add_error('dport', "protocol '$proto' does not support ports")
1622 if !$PROTOCOLS_WITH_PORTS->{$proto} &&
1623 $proto ne 'icmp' && $proto ne 'icmpv6'; # special cases
6d9246e7 1624 }
7ca36671
DM
1625
1626 if ($rule->{sport}) {
a1c04f71 1627 eval { parse_port_name_number_or_range($rule->{sport}, 0); };
6d9246e7 1628 &$add_error('sport', $@) if $@;
48e3963e 1629 my $proto = $rule->{proto};
6d9246e7 1630 &$add_error('proto', "missing property - 'sport' requires this property")
48e3963e
WB
1631 if !$proto;
1632 &$add_error('sport', "protocol '$proto' does not support ports")
1633 if !$PROTOCOLS_WITH_PORTS->{$proto};
7ca36671
DM
1634 }
1635
1636 if ($rule->{source}) {
041b9277
DM
1637 eval {
1638 my $source_ipversion = parse_address_list($rule->{source});
1639 &$set_ip_version($source_ipversion);
1640 };
6d9246e7 1641 &$add_error('source', $@) if $@;
ae029a88 1642 &$check_ipset_or_alias_property('source', $ipversion);
7ca36671
DM
1643 }
1644
1645 if ($rule->{dest}) {
9e2205e5
DM
1646 eval {
1647 my $dest_ipversion = parse_address_list($rule->{dest});
041b9277 1648 &$set_ip_version($dest_ipversion);
9e2205e5 1649 };
6d9246e7 1650 &$add_error('dest', $@) if $@;
ae029a88 1651 &$check_ipset_or_alias_property('dest', $ipversion);
7ca36671
DM
1652 }
1653
35d1d6da
DM
1654 $rule->{ipversion} = $ipversion if $ipversion;
1655
a523e057 1656 if ($rule->{macro} && !$error_count) {
35d1d6da 1657 eval { &$apply_macro($rule->{macro}, $rule, 1, $ipversion); };
6d9246e7
DM
1658 if (my $err = $@) {
1659 if (ref($err) eq "PVE::Exception" && $err->{errors}) {
1660 my $eh = $err->{errors};
1661 foreach my $p (keys %$eh) {
1662 &$add_error($p, $eh->{$p});
1663 }
1664 } else {
1665 &$add_error('macro', "$err");
1666 }
1667 }
9a2745a0
DM
1668 }
1669
6d9246e7
DM
1670 $rule->{errors} = $errors if $error_count;
1671
7ca36671
DM
1672 return $rule;
1673}
1674
9c7e0858
DM
1675sub copy_rule_data {
1676 my ($rule, $param) = @_;
1677
1678 foreach my $k (keys %$rule_properties) {
1679 if (defined(my $v = $param->{$k})) {
1680 if ($v eq '' || $v eq '-') {
1681 delete $rule->{$k};
1682 } else {
1683 $rule->{$k} = $v;
1684 }
9c7e0858
DM
1685 }
1686 }
7ca36671 1687
9c7e0858
DM
1688 return $rule;
1689}
1690
9f6845cf
DM
1691sub rules_modify_permissions {
1692 my ($rule_env) = @_;
1693
1694 if ($rule_env eq 'host') {
1695 return {
1696 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
1697 };
1698 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1699 return {
1700 check => ['perm', '/', [ 'Sys.Modify' ]],
1701 };
3b4882dc 1702 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
9f6845cf
DM
1703 return {
1704 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
1705 }
1706 }
1707
1708 return undef;
1709}
1710
1711sub rules_audit_permissions {
1712 my ($rule_env) = @_;
1713
1714 if ($rule_env eq 'host') {
1715 return {
1716 check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
1717 };
1718 } elsif ($rule_env eq 'cluster' || $rule_env eq 'group') {
1719 return {
1720 check => ['perm', '/', [ 'Sys.Audit' ]],
1721 };
3b4882dc 1722 } elsif ($rule_env eq 'vm' || $rule_env eq 'ct') {
9f6845cf
DM
1723 return {
1724 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1725 }
1726 }
1727
1728 return undef;
1729}
1730
9c7e0858 1731# core functions
780bcc0f
DM
1732my $bridge_firewall_enabled = 0;
1733
1734sub enable_bridge_firewall {
1735
1736 return if $bridge_firewall_enabled; # only once
1737
5f0a912c
DM
1738 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1");
1739 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1");
780bcc0f 1740
6a8a75db
DM
1741 # make sure syncookies are enabled (which is default on newer 3.X kernels anyways)
1742 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1");
1743
780bcc0f
DM
1744 $bridge_firewall_enabled = 1;
1745}
1746
b16e818e
DM
1747sub iptables_restore_cmdlist {
1748 my ($cmdlist) = @_;
3a616aa0 1749
59f9b456 1750 run_command("/sbin/iptables-restore -n", input => $cmdlist, errmsg => "iptables_restore_cmdlist");
3a616aa0
AD
1751}
1752
17da5c0f
AD
1753sub ip6tables_restore_cmdlist {
1754 my ($cmdlist) = @_;
1755
59f9b456 1756 run_command("/sbin/ip6tables-restore -n", input => $cmdlist, errmsg => "iptables_restore_cmdlist");
17da5c0f
AD
1757}
1758
34cdedfa
AD
1759sub ipset_restore_cmdlist {
1760 my ($cmdlist) = @_;
1761
ff5363da 1762 run_command("/sbin/ipset restore", input => $cmdlist, errmsg => "ipset_restore_cmdlist");
34cdedfa
AD
1763}
1764
151c209e
AD
1765sub ebtables_restore_cmdlist {
1766 my ($cmdlist) = @_;
1767
1768 run_command("/sbin/ebtables-restore", input => $cmdlist, errmsg => "ebtables_restore_cmdlist");
1769}
1770
de2a57cd 1771sub iptables_get_chains {
17da5c0f
AD
1772 my ($iptablescmd) = @_;
1773
1774 $iptablescmd = "iptables" if !$iptablescmd;
de2a57cd
DM
1775
1776 my $res = {};
1777
1778 # check what chains we want to track
1779 my $is_pvefw_chain = sub {
1780 my $name = shift;
1781
dec84fcd
DM
1782 return 1 if $name =~ m/^PVEFW-\S+$/;
1783
a3ded5cd 1784 return 1 if $name =~ m/^tap\d+i\d+-(?:IN|OUT)$/;
954f24b1 1785
a3ded5cd 1786 return 1 if $name =~ m/^veth\d+i\d+-(?:IN|OUT)$/;
954f24b1 1787
a3ded5cd 1788 return 1 if $name =~ m/^fwbr\d+(v\d+)?-(?:FW|IN|OUT|IPS)$/;
a89dfcc6 1789 return 1 if $name =~ m/^GROUP-(?:$security_group_name_pattern)-(?:IN|OUT)$/;
de2a57cd
DM
1790
1791 return undef;
1792 };
1793
1794 my $table = '';
1795
c4a2e5ae
DM
1796 my $hooks = {};
1797
de2a57cd
DM
1798 my $parser = sub {
1799 my $line = shift;
1800
1801 return if $line =~ m/^#/;
1802 return if $line =~ m/^\s*$/;
1803
1804 if ($line =~ m/^\*(\S+)$/) {
1805 $table = $1;
1806 return;
1807 }
1808
1809 return if $table ne 'filter';
1810
1811 if ($line =~ m/^:(\S+)\s/) {
1812 my $chain = $1;
1813 return if !&$is_pvefw_chain($chain);
3fa83edf 1814 $res->{$chain} = "unknown";
09d5f68e 1815 } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) {
3fa83edf 1816 my ($chain, $sig) = ($1, $2);
de2a57cd 1817 return if !&$is_pvefw_chain($chain);
3fa83edf 1818 $res->{$chain} = $sig;
c4a2e5ae
DM
1819 } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) {
1820 $hooks->{$1} = 1;
de2a57cd
DM
1821 } else {
1822 # simply ignore the rest
1823 return;
1824 }
1825 };
1826
17da5c0f 1827 run_command("/sbin/$iptablescmd-save", outfunc => $parser);
de2a57cd 1828
c4a2e5ae 1829 return wantarray ? ($res, $hooks) : $res;
de2a57cd
DM
1830}
1831
9bf7d929
DM
1832sub iptables_chain_digest {
1833 my ($rules) = @_;
1834 my $digest = Digest::SHA->new('sha1');
1835 foreach my $rule (@$rules) { # order is important
1836 $digest->add($rule);
1837 }
1838 return $digest->b64digest;
1839}
1840
3f95d14a
DM
1841sub ipset_chain_digest {
1842 my ($rules) = @_;
4bd0a9c4 1843
3f95d14a
DM
1844 my $digest = Digest::SHA->new('sha1');
1845 foreach my $rule (sort @$rules) { # note: sorted
1846 $digest->add($rule);
1847 }
1848 return $digest->b64digest;
1849}
1850
34cdedfa
AD
1851sub ipset_get_chains {
1852
1853 my $res = {};
1854 my $chains = {};
1855
1856 my $parser = sub {
1857 my $line = shift;
1858
1859 return if $line =~ m/^#/;
1860 return if $line =~ m/^\s*$/;
4b96e877 1861 if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) {
81a0bf43
DM
1862 my $chain = $1;
1863 $line =~ s/\s+$//; # delete trailing white space
1864 push @{$chains->{$chain}}, $line;
34cdedfa
AD
1865 } else {
1866 # simply ignore the rest
1867 return;
1868 }
1869 };
1870
ff5363da 1871 run_command("/sbin/ipset save", outfunc => $parser);
34cdedfa 1872
3f95d14a
DM
1873 # compute digest for each chain
1874 foreach my $chain (keys %$chains) {
1875 $res->{$chain} = ipset_chain_digest($chains->{$chain});
34cdedfa
AD
1876 }
1877
1878 return $res;
1879}
1880
151c209e
AD
1881sub ebtables_get_chains {
1882
1883 my $res = {};
1884 my $chains = {};
151c209e
AD
1885 my $parser = sub {
1886 my $line = shift;
1887 return if $line =~ m/^#/;
1888 return if $line =~ m/^\s*$/;
fc1f1de9
WB
1889 if ($line =~ m/^:(\S+)\s\S+$/) {
1890 # Make sure we know chains exist even if they're empty.
1891 $chains->{$1} //= [];
84025e99 1892 } elsif ($line =~ m/^(?:\S+)\s(\S+)\s(?:\S+).*/) {
151c209e
AD
1893 my $chain = $1;
1894 $line =~ s/\s+$//;
1895 push @{$chains->{$chain}}, $line;
1896 } else {
1897 # simply ignore the rest
1898 return;
1899 }
1900 };
1901
1902 run_command("/sbin/ebtables-save", outfunc => $parser);
84025e99 1903 # compute digest for each chain and store rules as well
151c209e 1904 foreach my $chain (keys %$chains) {
84025e99
SI
1905 $res->{$chain}->{rules} = $chains->{$chain};
1906 $res->{$chain}->{sig} = iptables_chain_digest($chains->{$chain});
151c209e
AD
1907 }
1908 return $res;
1909}
1910
180da76c
TW
1911# substitude action of rule according to action hash
1912sub rule_substitude_action {
1913 my ($rule, $actions) = @_;
1914
1915 if (my $action = $rule->{action}) {
1916 $rule->{action} = $actions->{$action} if defined($actions->{$action});
1917 }
1918}
1919
a44cb745
TW
1920# generate a src or dst match
1921# $dir(ection) is either d or s
1922sub ipt_gen_src_or_dst_match {
1923 my ($adr, $dir, $ipversion, $cluster_conf, $fw_conf) = @_;
1924
1925 my $srcdst;
1926 if ($dir eq 's') {
1927 $srcdst = "src";
1928 } elsif ($dir eq 'd') {
1929 $srcdst = "dst";
1930 } else {
1931 die "ipt_gen_src_or_dst_match: invalid direction $dir \n";
1932 }
1933
1934 my $match;
1935 if ($adr =~ m/^\+/) {
1936 if ($adr =~ m/^\+(${ipset_name_pattern})$/) {
1937 my $name = $1;
1938 my $ipset_chain;
1939 if ($fw_conf && $fw_conf->{ipset}->{$name}) {
1940 $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name, $ipversion);
1941 } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) {
1942 $ipset_chain = compute_ipset_chain_name(0, $name, $ipversion);
1943 } else {
1944 die "no such ipset '$name'\n";
1945 }
1946 $match = "-m set --match-set ${ipset_chain} ${srcdst}";
1947 } else {
1948 die "invalid security group name '$adr'\n";
1949 }
1950 } elsif ($adr =~ m/^${ip_alias_pattern}$/){
1951 my $alias = lc($adr);
1952 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
1953 $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf;
1954 die "no such alias '$adr'\n" if !$e;
1955 $match = "-${dir} $e->{cidr}";
1956 } elsif ($adr =~ m/\-/){
1957 $match = "-m iprange --${srcdst}-range $adr";
1958 } else {
1959 $match = "-${dir} $adr";
1960 }
1961
1962 return $match;
1963}
1964
30c390f9
TW
1965# convert a %rule to an array of iptables commands
1966sub ipt_rule_to_cmds {
1967 my ($rule, $chain, $ipversion, $cluster_conf, $fw_conf, $vmid) = @_;
1968
1969 die "ipt_rule_to_cmds unable to handle macro" if $rule->{macro}; #should not happen
1970
1971 my @match = ();
1972
1973 if (defined $rule->{match}) {
1974 push @match, $rule->{match};
1975 } else {
1976 push @match, "-i $rule->{iface_in}" if $rule->{iface_in};
1977 push @match, "-o $rule->{iface_out}" if $rule->{iface_out};
1978
1979 if ($rule->{source}) {
1980 push @match, ipt_gen_src_or_dst_match($rule->{source}, 's', $ipversion, $cluster_conf, $fw_conf);
1981 }
1982 if ($rule->{dest}) {
1983 push @match, ipt_gen_src_or_dst_match($rule->{dest}, 'd', $ipversion, $cluster_conf, $fw_conf);
1984 }
1985
1986 if (my $proto = $rule->{proto}) {
1987 push @match, "-p $proto";
1988
f776d6de
WB
1989 my $multidport = defined($rule->{dport}) && parse_port_name_number_or_range($rule->{dport}, 1);
1990 my $multisport = defined($rule->{sport}) && parse_port_name_number_or_range($rule->{sport}, 0);
30c390f9 1991
f776d6de
WB
1992 my $add_dport = sub {
1993 return if !$rule->{dport};
3dffed4c 1994
30c390f9
TW
1995 if ($proto eq 'icmp') {
1996 # Note: we use dport to store --icmp-type
1997 die "unknown icmp-type '$rule->{dport}'\n"
1998 if $rule->{dport} !~ /^\d+$/ && !defined($icmp_type_names->{$rule->{dport}});
1999 push @match, "-m icmp --icmp-type $rule->{dport}";
2000 } elsif ($proto eq 'icmpv6') {
2001 # Note: we use dport to store --icmpv6-type
2002 die "unknown icmpv6-type '$rule->{dport}'\n"
2003 if $rule->{dport} !~ /^\d+$/ && !defined($icmpv6_type_names->{$rule->{dport}});
2004 push @match, "-m icmpv6 --icmpv6-type $rule->{dport}";
2005 } elsif (!$PROTOCOLS_WITH_PORTS->{$proto}) {
2006 die "protocol $proto does not have ports\n";
f776d6de
WB
2007 } elsif ($multidport) {
2008 push @match, "--match multiport", "--dports $rule->{dport}";
30c390f9 2009 } else {
f776d6de 2010 push @match, "--dport $rule->{dport}";
30c390f9 2011 }
f776d6de 2012 };
30c390f9 2013
f776d6de
WB
2014 my $add_sport = sub {
2015 return if !$rule->{sport};
3dffed4c 2016
30c390f9
TW
2017 die "protocol $proto does not have ports\n"
2018 if !$PROTOCOLS_WITH_PORTS->{$proto};
f776d6de
WB
2019 if ($multisport) {
2020 push @match, "--match multiport", "--sports $rule->{sport}";
30c390f9
TW
2021 } else {
2022 push @match, "--sport $rule->{sport}";
2023 }
f776d6de
WB
2024 };
2025
3dffed4c 2026 # order matters - single port before multiport!
f776d6de
WB
2027 $add_dport->() if $multisport;
2028 $add_sport->();
2029 $add_dport->() if !$multisport;
30c390f9
TW
2030 } elsif ($rule->{dport} || $rule->{sport}) {
2031 die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport};
2032 die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport};
2033 }
2034
2035 push @match, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype};
2036 }
2037 my $matchstr = scalar(@match) ? join(' ', @match) : "";
2038
2039 my $targetstr;
2040 if (defined $rule->{target}) {
2041 $targetstr = $rule->{target};
2042 } else {
2043 my $action = (defined $rule->{action}) ? $rule->{action} : "";
2044 my $goto = 1 if $action eq 'PVEFW-SET-ACCEPT-MARK';
2045 $targetstr = ($goto) ? "-g $action" : "-j $action";
2046 }
2047
2048 my @iptcmds;
2db2b6aa
TL
2049 my $log = $rule->{log};
2050 if (defined($log) && $log ne 'nolog') {
3489f8a2
CE
2051 my $loglevel = $log_level_hash->{$log};
2052 my $logaction = get_log_rule_base($chain, $vmid, $rule->{logmsg}, $loglevel);
30c390f9
TW
2053 push @iptcmds, "-A $chain $matchstr $logaction";
2054 }
2055 push @iptcmds, "-A $chain $matchstr $targetstr";
2056 return @iptcmds;
2057}
2058
eba0fb64 2059sub ruleset_generate_rule {
3489f8a2 2060 my ($ruleset, $chain, $ipversion, $rule, $cluster_conf, $fw_conf, $vmid) = @_;
eba0fb64 2061
e5076eee
DM
2062 my $rules;
2063
2064 if ($rule->{macro}) {
35d1d6da 2065 $rules = &$apply_macro($rule->{macro}, $rule, 0, $ipversion);
e5076eee
DM
2066 } else {
2067 $rules = [ $rule ];
2068 }
2069
30c390f9
TW
2070 # update all or nothing
2071 my @ipt_rule_cmds;
2072 foreach my $r (@$rules) {
3489f8a2 2073 push @ipt_rule_cmds, ipt_rule_to_cmds($r, $chain, $ipversion, $cluster_conf, $fw_conf, $vmid);
30c390f9
TW
2074 }
2075 foreach my $c (@ipt_rule_cmds) {
2076 ruleset_add_ipt_cmd($ruleset, $chain, $c);
2077 }
2078}
2079
3fa83edf
DM
2080sub ruleset_create_chain {
2081 my ($ruleset, $chain) = @_;
3a616aa0 2082
d050c724 2083 die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28;
782c4cde 2084 die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format
d050c724 2085
3fa83edf
DM
2086 die "chain '$chain' already exists\n" if $ruleset->{$chain};
2087
2088 $ruleset->{$chain} = [];
3a616aa0
AD
2089}
2090
3fa83edf
DM
2091sub ruleset_chain_exist {
2092 my ($ruleset, $chain) = @_;
3a616aa0 2093
3fa83edf
DM
2094 return $ruleset->{$chain} ? 1 : undef;
2095}
3a616aa0 2096
30c390f9
TW
2097# add an iptables command (like generated by ipt_rule_to_cmds) to a chain
2098sub ruleset_add_ipt_cmd {
2099 my ($ruleset, $chain, $iptcmd) = @_;
2100
2101 die "no such chain '$chain'\n" if !$ruleset->{$chain};
2102
2103 push @{$ruleset->{$chain}}, $iptcmd;
2104}
2105
1e9c5070 2106sub ruleset_addrule {
3489f8a2 2107 my ($ruleset, $chain, $match, $action, $log, $logmsg, $vmid) = @_;
1e9c5070 2108
3489f8a2 2109 die "no such chain '$chain'\n" if !$ruleset->{$chain};
1e9c5070 2110
3489f8a2
CE
2111 if ($log) {
2112 my $loglevel = $log_level_hash->{$log};
2113 my $logaction = get_log_rule_base($chain, $vmid, $logmsg, $loglevel);
7f7930f8 2114 push @{$ruleset->{$chain}}, "-A $chain $match $logaction";
3489f8a2
CE
2115 }
2116 # for stable ebtables digests avoid double-spaces to match ebtables-save output
2117 $match .= ' ' if length($match);
2118 push @{$ruleset->{$chain}}, "-A $chain ${match}$action";
1e9c5070
TW
2119}
2120
3fa83edf 2121sub ruleset_insertrule {
1e9c5070 2122 my ($ruleset, $chain, $match, $action, $log) = @_;
3a616aa0 2123
3fa83edf 2124 die "no such chain '$chain'\n" if !$ruleset->{$chain};
3a616aa0 2125
1e9c5070 2126 unshift @{$ruleset->{$chain}}, "-A $chain $match $action";
3fa83edf 2127}
3a616aa0 2128
782c4cde
DM
2129sub get_log_rule_base {
2130 my ($chain, $vmid, $msg, $loglevel) = @_;
cbb5d6f3 2131
782c4cde 2132 $vmid = 0 if !defined($vmid);
7f7930f8 2133 $msg = "" if !defined($msg);
782c4cde 2134
cc37e000
TL
2135 my $rlimit = '';
2136 if (defined($global_log_ratelimit)) {
2137 $rlimit = "-m limit $global_log_ratelimit ";
2138 }
2139
cbb5d6f3 2140 # Note: we use special format for prefix to pass further
7f7930f8 2141 # info to log daemon (VMID, LOGLEVEL and CHAIN)
cc37e000 2142 return "${rlimit}-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\"";
782c4cde
DM
2143}
2144
ead850e8 2145sub ruleset_add_chain_policy {
88c26d5e 2146 my ($ruleset, $chain, $ipversion, $vmid, $policy, $loglevel, $accept_action) = @_;
ead850e8
DM
2147
2148 if ($policy eq 'ACCEPT') {
2149
180da76c
TW
2150 my $rule = { action => 'ACCEPT' };
2151 rule_substitude_action($rule, { ACCEPT => $accept_action});
2152 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule);
ead850e8
DM
2153
2154 } elsif ($policy eq 'DROP') {
2155
1e9c5070 2156 ruleset_addrule($ruleset, $chain, "", "-j PVEFW-Drop");
ead850e8 2157
7f7930f8 2158 ruleset_addrule($ruleset, $chain, "", "-j DROP", $loglevel, "policy $policy: ", $vmid);
ead850e8 2159 } elsif ($policy eq 'REJECT') {
1e9c5070 2160 ruleset_addrule($ruleset, $chain, "", "-j PVEFW-Reject");
ead850e8 2161
d8d4dd67 2162 ruleset_addrule($ruleset, $chain, "", "-g PVEFW-reject", $loglevel, "policy $policy: ", $vmid);
ead850e8
DM
2163 } else {
2164 # should not happen
2165 die "internal error: unknown policy '$policy'";
2166 }
2167}
2168
a83abe93 2169sub ruleset_chain_add_ndp {
e8415920 2170 my ($ruleset, $chain, $ipversion, $options, $direction, $accept) = @_;
a83abe93
WB
2171 return if $ipversion != 6 || (defined($options->{ndp}) && !$options->{ndp});
2172
1e9c5070 2173 ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type router-solicitation", $accept);
d7aa51ac 2174 if ($direction ne 'OUT' || $options->{radv}) {
1e9c5070 2175 ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type router-advertisement", $accept);
d7aa51ac 2176 }
1e9c5070
TW
2177 ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type neighbor-solicitation", $accept);
2178 ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type neighbor-advertisement", $accept);
a83abe93
WB
2179}
2180
e2943485 2181sub ruleset_chain_add_conn_filters {
27c49e25 2182 my ($ruleset, $chain, $allow_invalid, $accept) = @_;
e2943485 2183
27c49e25
CE
2184 if (!$allow_invalid) {
2185 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID", "-j DROP");
2186 }
1e9c5070 2187 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED", "-j $accept");
e2943485
DM
2188}
2189
2190sub ruleset_chain_add_input_filters {
88c26d5e 2191 my ($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel) = @_;
2428e394
AD
2192
2193 if ($cluster_conf->{ipset}->{blacklist}){
b5831a0d
AD
2194 if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist")) {
2195 ruleset_create_chain($ruleset, "PVEFW-blacklist");
3489f8a2 2196 ruleset_addrule($ruleset, "PVEFW-blacklist", "", "-j DROP", $loglevel, "DROP: ", 0);
b5831a0d 2197 }
88c26d5e 2198 my $ipset_chain = compute_ipset_chain_name(0, 'blacklist', $ipversion);
1e9c5070 2199 ruleset_addrule($ruleset, $chain, "-m set --match-set ${ipset_chain} src", "-j PVEFW-blacklist");
2428e394 2200 }
e2943485
DM
2201
2202 if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) {
5b5c42b1 2203 if ($ipversion == 4) {
1e9c5070 2204 ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW", "-j PVEFW-smurfs");
5b5c42b1 2205 }
e2943485
DM
2206 }
2207
2208 if ($options->{tcpflags}) {
1e9c5070 2209 ruleset_addrule($ruleset, $chain, "-p tcp", "-j PVEFW-tcpflags");
e2943485
DM
2210 }
2211}
2212
9e15114a 2213sub ruleset_create_vm_chain {
88c26d5e 2214 my ($ruleset, $chain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction) = @_;
3a616aa0 2215
9e15114a 2216 ruleset_create_chain($ruleset, $chain);
b47ecc88 2217 my $accept = generate_nfqueue($options);
3a616aa0 2218
ce15d90b 2219 if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) {
dcafc5fb
WB
2220 if ($ipversion == 4) {
2221 if ($direction eq 'OUT') {
2222 ruleset_generate_rule($ruleset, $chain, $ipversion,
2223 { action => 'PVEFW-SET-ACCEPT-MARK',
2224 proto => 'udp', sport => 68, dport => 67 });
2225 } else {
2226 ruleset_generate_rule($ruleset, $chain, $ipversion,
2227 { action => 'ACCEPT',
2228 proto => 'udp', sport => 67, dport => 68 });
2229 }
2230 } elsif ($ipversion == 6) {
2231 if ($direction eq 'OUT') {
2232 ruleset_generate_rule($ruleset, $chain, $ipversion,
2233 { action => 'PVEFW-SET-ACCEPT-MARK',
2234 proto => 'udp', sport => 546, dport => 547 });
2235 } else {
2236 ruleset_generate_rule($ruleset, $chain, $ipversion,
2237 { action => 'ACCEPT',
2238 proto => 'udp', sport => 547, dport => 546 });
2239 }
76a2d1e7 2240 }
dcafc5fb 2241
ce15d90b
DM
2242 }
2243
b21aca2c
DM
2244 if ($direction eq 'OUT') {
2245 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
1e9c5070 2246 ruleset_addrule($ruleset, $chain, "-m mac ! --mac-source $macaddr", "-j DROP");
b21aca2c 2247 }
d7aa51ac 2248 if ($ipversion == 6 && !$options->{radv}) {
1e9c5070 2249 ruleset_addrule($ruleset, $chain, "-p icmpv6 --icmpv6-type router-advertisement", "-j DROP");
d7aa51ac 2250 }
808d711d 2251 if ($ipfilter_ipset) {
1e9c5070 2252 ruleset_addrule($ruleset, $chain, "-m set ! --match-set $ipfilter_ipset src", "-j DROP");
808d711d 2253 }
1e9c5070 2254 ruleset_addrule($ruleset, $chain, "", "-j MARK --set-mark $FWACCEPTMARK_OFF"); # clear mark
c29f55c9 2255 }
e8415920
WB
2256
2257 my $accept_action = $direction eq 'OUT' ? '-g PVEFW-SET-ACCEPT-MARK' : "-j $accept";
2258 ruleset_chain_add_ndp($ruleset, $chain, $ipversion, $options, $direction, $accept_action);
9e15114a
DM
2259}
2260
4bc6b5ac 2261sub ruleset_add_group_rule {
aedde2c2 2262 my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action, $ipversion) = @_;
4bc6b5ac
DM
2263
2264 my $group = $rule->{action};
2265 my $group_chain = "GROUP-$group-$direction";
2266 if(!ruleset_chain_exist($ruleset, $group_chain)){
aedde2c2 2267 generate_group_rules($ruleset, $cluster_conf, $group, $ipversion);
4bc6b5ac 2268 }
bfc488f6 2269
f8b12fff 2270 if ($direction eq 'OUT' && $rule->{iface_out}) {
1e9c5070 2271 ruleset_addrule($ruleset, $chain, "-o $rule->{iface_out}", "-j $group_chain");
f8b12fff 2272 } elsif ($direction eq 'IN' && $rule->{iface_in}) {
1e9c5070 2273 ruleset_addrule($ruleset, $chain, "-i $rule->{iface_in}", "-j $group_chain");
4bc6b5ac 2274 } else {
1e9c5070 2275 ruleset_addrule($ruleset, $chain, "", "-j $group_chain");
4bc6b5ac
DM
2276 }
2277
1e9c5070 2278 ruleset_addrule($ruleset, $chain, "-m mark --mark $FWACCEPTMARK_ON", "-j $action");
4bc6b5ac
DM
2279}
2280
9e15114a 2281sub ruleset_generate_vm_rules {
3489f8a2 2282 my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options, $ipversion, $vmid) = @_;
9e15114a
DM
2283
2284 my $lc_direction = lc($direction);
c29f55c9 2285
6b8ca015
DM
2286 my $in_accept = generate_nfqueue($options);
2287
92e976b3
DM
2288 foreach my $rule (@$rules) {
2289 next if $rule->{iface} && $rule->{iface} ne $netid;
b7ab6989 2290 next if !$rule->{enable} || $rule->{errors};
006490cb 2291 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
84870b1a 2292
92e976b3 2293 if ($rule->{type} eq 'group') {
4bc6b5ac 2294 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction,
aedde2c2 2295 $direction eq 'OUT' ? 'RETURN' : $in_accept, $ipversion);
92e976b3
DM
2296 } else {
2297 next if $rule->{type} ne $lc_direction;
921dfb33 2298 eval {
3489f8a2 2299 $rule->{logmsg} = "$rule->{action}: ";
921dfb33 2300 if ($direction eq 'OUT') {
180da76c 2301 rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" });
3489f8a2 2302 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf, $vmid);
921dfb33 2303 } else {
180da76c 2304 rule_substitude_action($rule, { ACCEPT => $in_accept , REJECT => "PVEFW-reject" });
3489f8a2 2305 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $vmfw_conf, $vmid);
921dfb33
DM
2306 }
2307 };
2308 warn $@ if $@;
4e6112f9 2309 }
3a616aa0 2310 }
9e15114a
DM
2311}
2312
b47ecc88
AD
2313sub generate_nfqueue {
2314 my ($options) = @_;
2315
73089769
DM
2316 if ($options->{ips}) {
2317 my $action = "NFQUEUE";
2318 if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) {
2319 if (defined($3) && defined($1)) {
b47ecc88 2320 $action .= " --queue-balance $1:$3";
73089769 2321 } elsif (defined($1)) {
b47ecc88
AD
2322 $action .= " --queue-num $1";
2323 }
2324 }
8dc92812 2325 $action .= " --queue-bypass" if $feature_ipset_nomatch; #need kernel 3.10
73089769
DM
2326 return $action;
2327 } else {
2328 return "ACCEPT";
b47ecc88 2329 }
b47ecc88
AD
2330}
2331
da6fc60b 2332sub ruleset_generate_vm_ipsrules {
a01c32c7 2333 my ($ruleset, $options, $direction, $iface) = @_;
da6fc60b
AD
2334
2335 if ($options->{ips} && $direction eq 'IN') {
2336 my $nfqueue = generate_nfqueue($options);
2337
a01c32c7 2338 if (!ruleset_chain_exist($ruleset, "PVEFW-IPS")) {
da6fc60b
AD
2339 ruleset_create_chain($ruleset, "PVEFW-IPS");
2340 }
2341
1e9c5070 2342 ruleset_addrule($ruleset, "PVEFW-IPS", "-m physdev --physdev-out $iface --physdev-is-bridged", "-j $nfqueue");
da6fc60b
AD
2343 }
2344}
2345
9e15114a 2346sub generate_tap_rules_direction {
84870b1a 2347 my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction, $ipversion) = @_;
9e15114a
DM
2348
2349 my $lc_direction = lc($direction);
2350
2351 my $rules = $vmfw_conf->{rules};
2352
2353 my $options = $vmfw_conf->{options};
2354 my $loglevel = get_option_log_level($options, "log_level_${lc_direction}");
2355
2356 my $tapchain = "$iface-$direction";
2357
b692f42c 2358 my $ipfilter_name = compute_ipfilter_ipset_name($netid);
88c26d5e 2359 my $ipfilter_ipset = compute_ipset_chain_name($vmid, $ipfilter_name, $ipversion)
74601077 2360 if $options->{ipfilter} || $vmfw_conf->{ipset}->{$ipfilter_name};
808d711d 2361
a34cfdd0 2362 # create chain with mac and ip filter
88c26d5e 2363 ruleset_create_vm_chain($ruleset, $tapchain, $ipversion, $options, $macaddr, $ipfilter_ipset, $direction);
9e15114a 2364
a34cfdd0 2365 if ($options->{enable}) {
3489f8a2 2366 ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options, $ipversion, $vmid);
3a616aa0 2367
a34cfdd0 2368 ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface);
da6fc60b 2369
a34cfdd0
DM
2370 # implement policy
2371 my $policy;
ccae0b50 2372
a34cfdd0
DM
2373 if ($direction eq 'OUT') {
2374 $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default
2375 } else {
76448f08 2376 $policy = $options->{policy_in} || 'DROP'; # allow nothing by default
a34cfdd0 2377 }
ccae0b50 2378
a34cfdd0
DM
2379 my $accept = generate_nfqueue($options);
2380 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept;
88c26d5e 2381 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, $policy, $loglevel, $accept_action);
a34cfdd0
DM
2382 } else {
2383 my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : 'ACCEPT';
88c26d5e 2384 ruleset_add_chain_policy($ruleset, $tapchain, $ipversion, $vmid, 'ACCEPT', $loglevel, $accept_action);
a34cfdd0 2385 }
3a616aa0 2386
3fa83edf 2387 # plug the tap chain to bridge chain
3cc81077 2388 if ($direction eq 'IN') {
a01c32c7 2389 ruleset_addrule($ruleset, "PVEFW-FWBR-IN",
8f3aa9cc 2390 "-m physdev --physdev-is-bridged --physdev-out $iface", "-j $tapchain");
3cc81077 2391 } else {
a01c32c7 2392 ruleset_addrule($ruleset, "PVEFW-FWBR-OUT",
8f3aa9cc 2393 "-m physdev --physdev-is-bridged --physdev-in $iface", "-j $tapchain");
3cc81077 2394 }
3a616aa0
AD
2395}
2396
d18c1e2b 2397sub enable_host_firewall {
aedde2c2 2398 my ($ruleset, $hostfw_conf, $cluster_conf, $ipversion) = @_;
0bd5f137 2399
92e976b3 2400 my $options = $hostfw_conf->{options};
63324b09 2401 my $cluster_options = $cluster_conf->{options};
92e976b3 2402 my $rules = $hostfw_conf->{rules};
35f0c37e 2403 my $cluster_rules = $cluster_conf->{rules};
178a63be 2404
3fa83edf 2405 # host inbound firewall
dec84fcd
DM
2406 my $chain = "PVEFW-HOST-IN";
2407 ruleset_create_chain($ruleset, $chain);
0bd5f137 2408
178a63be
DM
2409 my $loglevel = get_option_log_level($options, "log_level_in");
2410
1e9c5070 2411 ruleset_addrule($ruleset, $chain, "-i lo", "-j ACCEPT");
4ac863a6 2412
27c49e25 2413 ruleset_chain_add_conn_filters($ruleset, $chain, 0, 'ACCEPT');
e8415920 2414 ruleset_chain_add_ndp($ruleset, $chain, $ipversion, $options, 'IN', '-j RETURN');
88c26d5e 2415 ruleset_chain_add_input_filters($ruleset, $chain, $ipversion, $options, $cluster_conf, $loglevel);
fb424a00 2416
23e888f8
DM
2417 # we use RETURN because we need to check also tap rules
2418 my $accept_action = 'RETURN';
2419
1e9c5070 2420 ruleset_addrule($ruleset, $chain, "-p igmp", "-j $accept_action"); # important for multicast
cc8dc02f 2421
35f0c37e
DM
2422 # add host rules first, so that cluster wide rules can be overwritten
2423 foreach my $rule (@$rules, @$cluster_rules) {
5383df39 2424 next if !$rule->{enable} || $rule->{errors};
35d1d6da 2425 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
bfc488f6 2426
f8b12fff 2427 $rule->{iface_in} = $rule->{iface} if $rule->{iface};
5383df39 2428
1a9978ed
DM
2429 eval {
2430 if ($rule->{type} eq 'group') {
aedde2c2 2431 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action, $ipversion);
1a9978ed 2432 } elsif ($rule->{type} eq 'in') {
180da76c 2433 rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" });
3489f8a2 2434 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf, 0);
1a9978ed
DM
2435 }
2436 };
2437 warn $@ if $@;
f8b12fff 2438 delete $rule->{iface_in};
0bd5f137 2439 }
eb399cef
DM
2440
2441 # allow standard traffic for management ipset (includes cluster network)
88c26d5e 2442 my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management", $ipversion);
708ba714 2443 my $mngmntsrc = "-m set --match-set ${mngmnt_ipset_chain} src";
1e9c5070
TW
2444 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 8006", "-j $accept_action"); # PVE API
2445 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 5900:5999", "-j $accept_action"); # PVE VNC Console
2446 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 3128", "-j $accept_action"); # SPICE Proxy
2447 ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 22", "-j $accept_action"); # SSH
bfc488f6 2448
afcd29b3
DM
2449 my $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
2450 my $localnet_ver = $cluster_conf->{aliases}->{local_network}->{ipversion};
3bc79f87 2451
eb399cef 2452 # corosync
35d1d6da 2453 if ($localnet && ($ipversion == $localnet_ver)) {
1e9c5070
TW
2454 my $corosync_rule = "-p udp --dport 5404:5405";
2455 ruleset_addrule($ruleset, $chain, "-s $localnet -d $localnet $corosync_rule", "-j $accept_action");
2456 ruleset_addrule($ruleset, $chain, "-s $localnet -m addrtype --dst-type MULTICAST $corosync_rule", "-j $accept_action");
3bc79f87 2457 }
0bd5f137 2458
23e888f8 2459 # implement input policy
63324b09 2460 my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default
88c26d5e 2461 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
0bd5f137 2462
3fa83edf 2463 # host outbound firewall
aadd745e 2464 $chain = "PVEFW-HOST-OUT";
dec84fcd
DM
2465 ruleset_create_chain($ruleset, $chain);
2466
178a63be
DM
2467 $loglevel = get_option_log_level($options, "log_level_out");
2468
1e9c5070 2469 ruleset_addrule($ruleset, $chain, "-o lo", "-j ACCEPT");
e2943485 2470
27c49e25 2471 ruleset_chain_add_conn_filters($ruleset, $chain, 0, 'ACCEPT');
e2943485 2472
23e888f8
DM
2473 # we use RETURN because we may want to check other thigs later
2474 $accept_action = 'RETURN';
e8415920 2475 ruleset_chain_add_ndp($ruleset, $chain, $ipversion, $options, 'OUT', "-j $accept_action");
23e888f8 2476
1e9c5070 2477 ruleset_addrule($ruleset, $chain, "-p igmp", "-j $accept_action"); # important for multicast
cc8dc02f 2478
35f0c37e
DM
2479 # add host rules first, so that cluster wide rules can be overwritten
2480 foreach my $rule (@$rules, @$cluster_rules) {
5383df39 2481 next if !$rule->{enable} || $rule->{errors};
35d1d6da 2482 next if $rule->{ipversion} && ($rule->{ipversion} != $ipversion);
5383df39 2483
f8b12fff 2484 $rule->{iface_out} = $rule->{iface} if $rule->{iface};
1a9978ed 2485 eval {
3489f8a2 2486 $rule->{logmsg} = "$rule->{action}: ";
1a9978ed 2487 if ($rule->{type} eq 'group') {
aedde2c2 2488 ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action, $ipversion);
1a9978ed 2489 } elsif ($rule->{type} eq 'out') {
180da76c 2490 rule_substitude_action($rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" });
3489f8a2 2491 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf, $hostfw_conf, 0);
1a9978ed
DM
2492 }
2493 };
2494 warn $@ if $@;
f8b12fff 2495 delete $rule->{iface_out};
0bd5f137
AD
2496 }
2497
3bc79f87 2498 # allow standard traffic on cluster network
35d1d6da 2499 if ($localnet && ($ipversion == $localnet_ver)) {
1e9c5070
TW
2500 ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 8006", "-j $accept_action"); # PVE API
2501 ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 22", "-j $accept_action"); # SSH
2502 ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 5900:5999", "-j $accept_action"); # PVE VNC Console
2503 ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 3128", "-j $accept_action"); # SPICE Proxy
bfc488f6 2504
1e9c5070
TW
2505 my $corosync_rule = "-p udp --dport 5404:5405";
2506 ruleset_addrule($ruleset, $chain, "-d $localnet $corosync_rule", "-j $accept_action");
2507 ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST $corosync_rule", "-j $accept_action");
3bc79f87
DM
2508 }
2509
23e888f8 2510 # implement output policy
63324b09 2511 $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default
88c26d5e 2512 ruleset_add_chain_policy($ruleset, $chain, $ipversion, 0, $policy, $loglevel, $accept_action);
6158271d 2513
1e9c5070
TW
2514 ruleset_addrule($ruleset, "PVEFW-OUTPUT", "", "-j PVEFW-HOST-OUT");
2515 ruleset_addrule($ruleset, "PVEFW-INPUT", "", "-j PVEFW-HOST-IN");
9d31b418
AD
2516}
2517
2518sub generate_group_rules {
aedde2c2 2519 my ($ruleset, $cluster_conf, $group, $ipversion) = @_;
9d31b418 2520
c6f5cc88 2521 my $rules = $cluster_conf->{groups}->{$group};
6158271d 2522
b4deedab
DM
2523 if (!$rules) {
2524 warn "no such security group '$group'\n";
2525 $rules = []; # create empty chain
2526 }
2527
3fa83edf 2528 my $chain = "GROUP-${group}-IN";
9d31b418 2529
3fa83edf 2530 ruleset_create_chain($ruleset, $chain);
1e9c5070 2531 ruleset_addrule($ruleset, $chain, "", "-j MARK --set-mark $FWACCEPTMARK_OFF"); # clear mark
9d31b418 2532
92e976b3
DM
2533 foreach my $rule (@$rules) {
2534 next if $rule->{type} ne 'in';
7a5a402b 2535 next if !$rule->{enable} || $rule->{errors};
aedde2c2 2536 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
180da76c 2537 rule_substitude_action($rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" });
2dc0a26e 2538 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf);
9d31b418
AD
2539 }
2540
3fa83edf 2541 $chain = "GROUP-${group}-OUT";
9d31b418 2542
3fa83edf 2543 ruleset_create_chain($ruleset, $chain);
1e9c5070 2544 ruleset_addrule($ruleset, $chain, "", "-j MARK --set-mark $FWACCEPTMARK_OFF"); # clear mark
9d31b418 2545
92e976b3
DM
2546 foreach my $rule (@$rules) {
2547 next if $rule->{type} ne 'out';
7a5a402b 2548 next if !$rule->{enable} || $rule->{errors};
aedde2c2 2549 next if $rule->{ipversion} && $rule->{ipversion} ne $ipversion;
92e976b3
DM
2550 # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to
2551 # check also other tap rules later
180da76c 2552 rule_substitude_action($rule, { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" });
2dc0a26e 2553 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, $cluster_conf);
9d31b418 2554 }
9d31b418
AD
2555}
2556
51bae274
DM
2557my $MAX_NETS = 32;
2558my $valid_netdev_names = {};
2559for (my $i = 0; $i < $MAX_NETS; $i++) {
2560 $valid_netdev_names->{"net$i"} = 1;
2561}
5e1267a5 2562
fe3d79b4
WB
2563sub get_mark_values {
2564 my ($value, $mask) = @_;
2565 $value = hex($value) if $value =~ /^0x/;
2566 $mask = hex($mask) if defined($mask) && $mask =~ /^0x/;
2567 $mask = 0xffffffff if !defined($mask);
2568 return ($value, $mask);
2569}
2570
51bae274 2571sub parse_fw_rule {
40af93c4 2572 my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env) = @_;
5e1267a5 2573
d4cda423
DM
2574 my $orig_line = $line;
2575
6d9246e7
DM
2576 my $rule = {};
2577
11bac5c2 2578 # we can add single line comments to the end of the rule
6d9246e7
DM
2579 if ($line =~ s/#\s*(.*?)\s*$//) {
2580 $rule->{comment} = decode('utf8', $1);
2581 }
11bac5c2
DM
2582
2583 # we can disable a rule when prefixed with '|'
ea9e5116 2584
6d9246e7 2585 $rule->{enable} = $line =~ s/^\|// ? 0 : 1;
51bae274 2586
dba740a9
DM
2587 $line =~ s/^(\S+)\s+(\S+)\s*// ||
2588 die "unable to parse rule: $line\n";
6d9246e7
DM
2589
2590 $rule->{type} = lc($1);
2591 $rule->{action} = $2;
2592
2593 if ($rule->{type} eq 'in' || $rule->{type} eq 'out') {
2594 if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
2595 $rule->{macro} = $1;
2596 $rule->{action} = $2;
92e976b3 2597 }
51bae274
DM
2598 }
2599
dba740a9
DM
2600 while (length($line)) {
2601 if ($line =~ s/^-i (\S+)\s*//) {
6d9246e7 2602 $rule->{iface} = $1;
dba740a9
DM
2603 next;
2604 }
51bae274 2605
6d9246e7 2606 last if $rule->{type} eq 'group';
51bae274 2607
dba740a9 2608 if ($line =~ s/^-p (\S+)\s*//) {
6d9246e7 2609 $rule->{proto} = $1;
dba740a9
DM
2610 next;
2611 }
6d9246e7 2612
dba740a9 2613 if ($line =~ s/^-dport (\S+)\s*//) {
6d9246e7 2614 $rule->{dport} = $1;
dba740a9
DM
2615 next;
2616 }
6d9246e7 2617
dba740a9 2618 if ($line =~ s/^-sport (\S+)\s*//) {
6d9246e7 2619 $rule->{sport} = $1;
dba740a9
DM
2620 next;
2621 }
2622 if ($line =~ s/^-source (\S+)\s*//) {
6d9246e7 2623 $rule->{source} = $1;
dba740a9
DM
2624 next;
2625 }
2626 if ($line =~ s/^-dest (\S+)\s*//) {
6d9246e7 2627 $rule->{dest} = $1;
dba740a9
DM
2628 next;
2629 }
3489f8a2
CE
2630 if ($line =~ s/^-log (emerg|alert|crit|err|warning|notice|info|debug|nolog)\s*//) {
2631 $rule->{log} = $1;
2632 next;
2633 }
51bae274 2634
dba740a9
DM
2635 last;
2636 }
ba791b1f 2637
dba740a9 2638 die "unable to parse rule parameters: $line\n" if length($line);
51bae274 2639
a523e057 2640 $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1);
eedcb564
WB
2641 if ($rule->{errors}) {
2642 # The verbose flag really means we're running from the CLI and want
2643 # output on the console - in the other case we really want such errors
2644 # to go into the syslog instead.
2645 my $log = $verbose ? sub { warn @_ } : sub { syslog(err => @_) };
2646 $log->("$prefix - errors in rule parameters: $orig_line\n");
d4cda423 2647 foreach my $p (keys %{$rule->{errors}}) {
eedcb564 2648 $log->(" $p: $rule->{errors}->{$p}\n");
d4cda423
DM
2649 }
2650 }
6d9246e7
DM
2651
2652 return $rule;
51bae274
DM
2653}
2654
c5e8b008
AD
2655sub verify_ethertype {
2656 my ($value) = @_;
2657 my $types = get_etc_ethertypes();
2658 die "unknown ethernet protocol type: $value\n"
2659 if !defined($types->{byname}->{$value}) &&
2660 !defined($types->{byid}->{$value});
2661}
2662
2d404ffc 2663sub parse_vmfw_option {
85c6eaed
DM
2664 my ($line) = @_;
2665
2666 my ($opt, $value);
2667
178a63be
DM
2668 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2669
74601077 2670 if ($line =~ m/^(enable|dhcp|ndp|radv|macfilter|ipfilter|ips):\s*(0|1)\s*$/i) {
9c6b6efd
DM
2671 $opt = lc($1);
2672 $value = int($2);
178a63be
DM
2673 } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) {
2674 $opt = lc($1);
2675 $value = $2 ? lc($3) : '';
72f63fde 2676 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
85c6eaed
DM
2677 $opt = lc($1);
2678 $value = uc($3);
b47ecc88
AD
2679 } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) {
2680 $opt = lc($1);
2681 $value = $2;
c5e8b008
AD
2682 } elsif ($line =~ m/^(layer2_protocols):\s*(((\S+)[,]?)+)\s*$/i) {
2683 $opt = lc($1);
2684 $value = $2;
2685 verify_ethertype($_) foreach split(/\s*,\s*/, $value);
9c6b6efd 2686 } else {
85c6eaed
DM
2687 die "can't parse option '$line'\n"
2688 }
2689
2690 return ($opt, $value);
2691}
2692
2d404ffc
DM
2693sub parse_hostfw_option {
2694 my ($line) = @_;
2695
2696 my ($opt, $value);
2697
2698 my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog";
2699
27c49e25 2700 if ($line =~ m/^(enable|nosmurfs|tcpflags|ndp|log_nf_conntrack|nf_conntrack_allow_invalid):\s*(0|1)\s*$/i) {
2d404ffc
DM
2701 $opt = lc($1);
2702 $value = int($2);
178a63be 2703 } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) {
2d404ffc
DM
2704 $opt = lc($1);
2705 $value = $2 ? lc($3) : '';
28c082a1 2706 } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) {
490cdead
DM
2707 $opt = lc($1);
2708 $value = int($2);
2d404ffc 2709 } else {
2d404ffc
DM
2710 die "can't parse option '$line'\n"
2711 }
2712
2713 return ($opt, $value);
2714}
2715
c6f5cc88
DM
2716sub parse_clusterfw_option {
2717 my ($line) = @_;
2718
2719 my ($opt, $value);
2720
72d055fc 2721 if ($line =~ m/^(enable):\s*(\d+)\s*$/i) {
c6f5cc88
DM
2722 $opt = lc($1);
2723 $value = int($2);
72d055fc
AG
2724 if (($value > 1) && ((time() - $value) > 60)) {
2725 $value = 0
2726 }
2549e7ef 2727 } elsif ($line =~ m/^(ebtables):\s*(0|1)\s*$/i) {
84025e99
SI
2728 $opt = lc($1);
2729 $value = int($2);
63324b09
DM
2730 } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
2731 $opt = lc($1);
2732 $value = uc($3);
cc37e000
TL
2733 } elsif ($line =~ m/^(log_ratelimit):\s*(\S+)\s*$/) {
2734 $opt = lc($1);
2735 $value = $2;
c6f5cc88 2736 } else {
c6f5cc88
DM
2737 die "can't parse option '$line'\n"
2738 }
2739
2740 return ($opt, $value);
2741}
2742
6c221576
DM
2743sub resolve_alias {
2744 my ($clusterfw_conf, $fw_conf, $cidr) = @_;
2745
6d959e3f 2746 my $alias = lc($cidr);
04f5088f 2747 my $e = $fw_conf ? $fw_conf->{aliases}->{$alias} : undef;
6d959e3f 2748 $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf;
6c221576 2749
6d959e3f
DM
2750 die "no such alias '$cidr'\n" if !$e;;
2751
2752 return wantarray ? ($e->{cidr}, $e->{ipversion}) : $e->{cidr};
6c221576
DM
2753}
2754
ae029a88
DM
2755sub parse_ip_or_cidr {
2756 my ($cidr) = @_;
2757
2758 my $ipversion;
2759
2760 if ($cidr =~ m!^(?:$IPV6RE)(/(\d+))?$!) {
2761 $cidr =~ s|/128$||;
2762 $ipversion = 6;
2763 } elsif ($cidr =~ m!^(?:$IPV4RE)(/(\d+))?$!) {
2764 $cidr =~ s|/32$||;
2765 $ipversion = 4;
2766 } else {
2767 die "value does not look like a valid IP address or CIDR network\n";
2768 }
2769
2770 return wantarray ? ($cidr, $ipversion) : $cidr;
2771}
2772
e76a9f53 2773sub parse_alias {
92e1209b
AD
2774 my ($line) = @_;
2775
81d574a7
DM
2776 # we can add single line comments to the end of the line
2777 my $comment = decode('utf8', $1) if $line =~ s/\s*#\s*(.*?)\s*$//;
2778
92e1209b 2779 if ($line =~ m/^(\S+)\s(\S+)$/) {
81d574a7 2780 my ($name, $cidr) = ($1, $2);
ae029a88
DM
2781 my $ipversion;
2782
2783 ($cidr, $ipversion) = parse_ip_or_cidr($cidr);
2784
81d574a7
DM
2785 my $data = {
2786 name => $name,
2787 cidr => $cidr,
70e524eb 2788 ipversion => $ipversion,
81d574a7
DM
2789 };
2790 $data->{comment} = $comment if $comment;
2791 return $data;
92e1209b
AD
2792 }
2793
81d574a7 2794 return undef;
92e1209b
AD
2795}
2796
e5cd1ee0 2797sub generic_fw_config_parser {
585ede43
TL
2798 my ($filename, $cluster_conf, $empty_conf, $rule_env) = @_;
2799
51bae274
DM
2800 my $section;
2801 my $group;
2802
1210ae94 2803 my $res = $empty_conf;
6158271d 2804
0dbef530
TL
2805 my $raw;
2806 if ($filename =~ m!^/etc/pve/(.*)$!) {
2807 $raw = PVE::Cluster::get_config($1);
2808 } else {
2809 $raw = eval { PVE::Tools::file_get_contents($filename) }; # ignore errors
2810 }
2811 return {} if !$raw;
2812
2813 my $linenr = 0;
2814 while ($raw =~ /^\h*(.*?)\h*$/gm) {
2815 my $line = $1;
2816 $linenr++;
51bae274
DM
2817 next if $line =~ m/^#/;
2818 next if $line =~ m/^\s*$/;
c8c534f7
DM
2819 chomp $line;
2820
961b4928
DM
2821 my $prefix = "$filename (line $linenr)";
2822
1210ae94 2823 if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) {
c6f5cc88
DM
2824 $section = 'options';
2825 next;
2826 }
2827
1210ae94 2828 if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) {
92e1209b
AD
2829 $section = 'aliases';
2830 next;
2831 }
2832
1210ae94 2833 if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
c6f5cc88 2834 $section = 'groups';
92e976b3 2835 $group = lc($1);
0d22acb3 2836 my $comment = $2;
351052d1
DM
2837 eval {
2838 die "security group name too long\n" if length($group) > $max_group_name_length;
2839 die "invalid security group name '$group'\n" if $group !~ m/^${security_group_name_pattern}$/;
2840 };
2841 if (my $err = $@) {
2842 ($section, $group, $comment) = undef;
2843 warn "$prefix: $err";
2844 next;
2845 }
2846
c85c87f9 2847 $res->{$section}->{$group} = [];
649e4d57
DM
2848 $res->{group_comments}->{$group} = decode('utf8', $comment)
2849 if $comment;
51bae274
DM
2850 next;
2851 }
34cdedfa 2852
1210ae94 2853 if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) {
c6f5cc88
DM
2854 $section = 'rules';
2855 next;
2856 }
cbb5d6f3 2857
1210ae94 2858 if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) {
34cdedfa
AD
2859 $section = 'ipset';
2860 $group = lc($1);
d72c631c 2861 my $comment = $2;
351052d1
DM
2862 eval {
2863 die "ipset name too long\n" if length($group) > $max_ipset_name_length;
2864 die "invalid ipset name '$group'\n" if $group !~ m/^${ipset_name_pattern}$/;
2865 };
2866 if (my $err = $@) {
2867 ($section, $group, $comment) = undef;
2868 warn "$prefix: $err";
2869 next;
2870 }
2871
c85c87f9 2872 $res->{$section}->{$group} = [];
e34d0e58 2873 $res->{ipset_comments}->{$group} = decode('utf8', $comment)
649e4d57 2874 if $comment;
34cdedfa
AD
2875 next;
2876 }
2877
c6f5cc88 2878 if (!$section) {
cbb5d6f3 2879 warn "$prefix: skip line - no section\n";
51bae274
DM
2880 next;
2881 }
2882
c6f5cc88
DM
2883 if ($section eq 'options') {
2884 eval {
1210ae94
DM
2885 my ($opt, $value);
2886 if ($rule_env eq 'cluster') {
2887 ($opt, $value) = parse_clusterfw_option($line);
2888 } elsif ($rule_env eq 'host') {
2889 ($opt, $value) = parse_hostfw_option($line);
2890 } else {
2891 ($opt, $value) = parse_vmfw_option($line);
2892 }
c6f5cc88
DM
2893 $res->{options}->{$opt} = $value;
2894 };
2895 warn "$prefix: $@" if $@;
92e1209b
AD
2896 } elsif ($section eq 'aliases') {
2897 eval {
e76a9f53 2898 my $data = parse_alias($line);
81d574a7 2899 $res->{aliases}->{lc($data->{name})} = $data;
92e1209b
AD
2900 };
2901 warn "$prefix: $@" if $@;
c6f5cc88
DM
2902 } elsif ($section eq 'rules') {
2903 my $rule;
40af93c4 2904 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env); };
c6f5cc88
DM
2905 if (my $err = $@) {
2906 warn "$prefix: $err";
2907 next;
2908 }
2909 push @{$res->{$section}}, $rule;
2910 } elsif ($section eq 'groups') {
34cdedfa 2911 my $rule;
40af93c4 2912 eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group'); };
34cdedfa
AD
2913 if (my $err = $@) {
2914 warn "$prefix: $err";
2915 next;
2916 }
3f95d14a 2917 push @{$res->{$section}->{$group}}, $rule;
3f95d14a 2918 } elsif ($section eq 'ipset') {
9d6f90e6
DM
2919 # we can add single line comments to the end of the rule
2920 my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
2921
30f1b100 2922 $line =~ m/^(\!)?\s*(\S+)\s*$/;
2a052ee3 2923 my $nomatch = $1;
9d6f90e6 2924 my $cidr = $2;
d46b1ef6
DM
2925 my $errors;
2926
2927 if ($nomatch && !$feature_ipset_nomatch) {
2928 $errors->{nomatch} = "nomatch not supported by kernel";
2929 }
2a052ee3 2930
4803b296
DM
2931 eval {
2932 if ($cidr =~ m/^${ip_alias_pattern}$/) {
2933 resolve_alias($cluster_conf, $res, $cidr); # make sure alias exists
2934 } else {
ae029a88 2935 $cidr = parse_ip_or_cidr($cidr);
92e1209b 2936 }
4803b296
DM
2937 };
2938 if (my $err = $@) {
c8c534f7 2939 chomp $err;
d46b1ef6 2940 $errors->{cidr} = $err;
2a052ee3 2941 }
2a052ee3 2942
b14db52f
WB
2943 if ($cidr =~ m!/0+$!) {
2944 $errors->{cidr} = "a zero prefix is not allowed in ipset entries\n";
2945 }
2946
e34d0e58 2947 my $entry = { cidr => $cidr };
9d6f90e6
DM
2948 $entry->{nomatch} = 1 if $nomatch;
2949 $entry->{comment} = $comment if $comment;
d46b1ef6 2950 $entry->{errors} = $errors if $errors;
e34d0e58 2951
c8c534f7 2952 if ($verbose && $errors) {
9a3061c7 2953 warn "$prefix - errors in ipset '$group': $line\n";
c8c534f7
DM
2954 foreach my $p (keys %{$errors}) {
2955 warn " $p: $errors->{$p}\n";
2956 }
2957 }
2958
9d6f90e6 2959 push @{$res->{$section}->{$group}}, $entry;
1210ae94
DM
2960 } else {
2961 warn "$prefix: skip line - unknown section\n";
2962 next;
51bae274 2963 }
5e1267a5
DM
2964 }
2965
2966 return $res;
2967}
2968
06320eb0
DM
2969sub run_locked {
2970 my ($code, @param) = @_;
2971
2972 my $timeout = 10;
2973
2974 my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param);
2975
2976 die $@ if $@;
2977
2978 return $res;
2979}
2980
5e1267a5
DM
2981sub read_local_vm_config {
2982
5e1267a5 2983 my $qemu = {};
3b4882dc 2984 my $lxc = {};
5e1267a5 2985
fdefeeab 2986 my $vmdata = { qemu => $qemu, lxc => $lxc };
5e1267a5 2987
c8301d63
DM
2988 my $vmlist = PVE::Cluster::get_vmlist();
2989 return $vmdata if !$vmlist || !$vmlist->{ids};
2990 my $ids = $vmlist->{ids};
2991
2992 foreach my $vmid (keys %$ids) {
2993 next if !$vmid; # skip VE0
2994 my $d = $ids->{$vmid};
2995 next if !$d->{node} || $d->{node} ne $nodename;
2996 next if !$d->{type};
fdefeeab 2997 if ($d->{type} eq 'qemu') {
d9fee004 2998 if ($have_qemu_server) {
b5a16dd3 2999 my $cfspath = PVE::QemuConfig->cfs_config_path($vmid);
d9fee004
DM
3000 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
3001 $qemu->{$vmid} = $conf;
3002 }
c8301d63 3003 }
3b4882dc
AG
3004 } elsif ($d->{type} eq 'lxc') {
3005 if ($have_lxc) {
a7c85d56 3006 my $cfspath = PVE::LXC::Config->cfs_config_path($vmid);
3b4882dc
AG
3007 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
3008 $lxc->{$vmid} = $conf;
3009 }
3010 }
3011 }
5e1267a5 3012 }
d9fee004 3013
5e1267a5
DM
3014 return $vmdata;
3015};
3016
e7b35711 3017sub load_vmfw_conf {
40af93c4 3018 my ($cluster_conf, $rule_env, $vmid, $dir) = @_;
e7b35711 3019
21053409 3020 $dir = $pvefw_conf_dir if !defined($dir);
8aef5177 3021 my $filename = "$dir/$vmid.fw";
585ede43
TL
3022
3023 my $empty_conf = {
3024 rules => [],
3025 options => {},
3026 aliases => {},
3027 ipset => {} ,
3028 ipset_comments => {},
3029 };
3030
3031 my $vmfw_conf = generic_fw_config_parser($filename, $cluster_conf, $empty_conf, $rule_env);
3032 $vmfw_conf->{vmid} = $vmid;
e7b35711
DM
3033
3034 return $vmfw_conf;
3035}
3036
464f933e 3037my $format_rules = sub {
dba740a9 3038 my ($rules, $allow_iface) = @_;
464f933e
DM
3039
3040 my $raw = '';
3041
3042 foreach my $rule (@$rules) {
c14dacdf 3043 if ($rule->{type} eq 'in' || $rule->{type} eq 'out' || $rule->{type} eq 'group') {
464f933e
DM
3044 $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
3045 $raw .= uc($rule->{type});
7ca36671
DM
3046 if ($rule->{macro}) {
3047 $raw .= " $rule->{macro}($rule->{action})";
3048 } else {
3049 $raw .= " " . $rule->{action};
3050 }
dba740a9
DM
3051 if ($allow_iface && $rule->{iface}) {
3052 $raw .= " -i $rule->{iface}";
3053 }
c14dacdf
DM
3054
3055 if ($rule->{type} ne 'group') {
dba740a9
DM
3056 $raw .= " -source $rule->{source}" if $rule->{source};
3057 $raw .= " -dest $rule->{dest}" if $rule->{dest};
3058 $raw .= " -p $rule->{proto}" if $rule->{proto};
3059 $raw .= " -dport $rule->{dport}" if $rule->{dport};
3060 $raw .= " -sport $rule->{sport}" if $rule->{sport};
3489f8a2 3061 $raw .= " -log $rule->{log}" if $rule->{log};
c14dacdf
DM
3062 }
3063
cbb5d6f3 3064 $raw .= " # " . encode('utf8', $rule->{comment})
464f933e
DM
3065 if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
3066 $raw .= "\n";
3067 } else {
c14dacdf 3068 die "unknown rule type '$rule->{type}'";
464f933e
DM
3069 }
3070 }
3071
3072 return $raw;
3073};
3074
3075my $format_options = sub {
68c90e21
DM
3076 my ($options) = @_;
3077
3078 my $raw = '';
464f933e
DM
3079
3080 $raw .= "[OPTIONS]\n\n";
3081 foreach my $opt (keys %$options) {
3082 $raw .= "$opt: $options->{$opt}\n";
3083 }
3084 $raw .= "\n";
68c90e21
DM
3085
3086 return $raw;
464f933e
DM
3087};
3088
0d5f0a0f
DM
3089my $format_aliases = sub {
3090 my ($aliases) = @_;
3091
3092 my $raw = '';
3093
3094 $raw .= "[ALIASES]\n\n";
3095 foreach my $k (keys %$aliases) {
81d574a7
DM
3096 my $e = $aliases->{$k};
3097 $raw .= "$e->{name} $e->{cidr}";
3098 $raw .= " # " . encode('utf8', $e->{comment})
3099 if $e->{comment} && $e->{comment} !~ m/^\s*$/;
3100 $raw .= "\n";
0d5f0a0f
DM
3101 }
3102 $raw .= "\n";
3103
3104 return $raw;
3105};
3106
1210ae94
DM
3107my $format_ipsets = sub {
3108 my ($fw_conf) = @_;
3109
9d6f90e6
DM
3110 my $raw = '';
3111
1210ae94
DM
3112 foreach my $ipset (sort keys %{$fw_conf->{ipset}}) {
3113 if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) {
3114 my $utf8comment = encode('utf8', $comment);
3115 $raw .= "[IPSET $ipset] # $utf8comment\n\n";
3116 } else {
3117 $raw .= "[IPSET $ipset]\n\n";
3118 }
3119 my $options = $fw_conf->{ipset}->{$ipset};
6e299ae3 3120
1210ae94
DM
3121 my $nethash = {};
3122 foreach my $entry (@$options) {
3123 $nethash->{$entry->{cidr}} = $entry;
3124 }
3125
3126 foreach my $cidr (sort keys %$nethash) {
3127 my $entry = $nethash->{$cidr};
3128 my $line = $entry->{nomatch} ? '!' : '';
3129 $line .= $entry->{cidr};
3130 $line .= " # " . encode('utf8', $entry->{comment})
3131 if $entry->{comment} && $entry->{comment} !~ m/^\s*$/;
3132 $raw .= "$line\n";
3133 }
3134
3135 $raw .= "\n";
9d6f90e6 3136 }
e34d0e58 3137
9d6f90e6
DM
3138 return $raw;
3139};
3140
464f933e
DM
3141sub save_vmfw_conf {
3142 my ($vmid, $vmfw_conf) = @_;
3143
3144 my $raw = '';
3145
3146 my $options = $vmfw_conf->{options};
89ea63c8 3147 $raw .= &$format_options($options) if $options && scalar(keys %$options);
cbb5d6f3 3148
e76a9f53 3149 my $aliases = $vmfw_conf->{aliases};
89ea63c8 3150 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
e76a9f53 3151
89ea63c8 3152 $raw .= &$format_ipsets($vmfw_conf) if $vmfw_conf->{ipset};
1210ae94 3153
8824b2f0 3154 my $rules = $vmfw_conf->{rules} || [];
89ea63c8 3155 if ($rules && scalar(@$rules)) {
464f933e
DM
3156 $raw .= "[RULES]\n\n";
3157 $raw .= &$format_rules($rules, 1);
3158 $raw .= "\n";
3159 }
3160
21053409 3161 my $filename = "$pvefw_conf_dir/$vmid.fw";
c32e04e6
FG
3162 if ($raw) {
3163 mkdir $pvefw_conf_dir;
3164 PVE::Tools::file_set_contents($filename, $raw);
3165 } else {
3166 unlink $filename;
3167 }
464f933e
DM
3168}
3169
edee9035
AG
3170sub remove_vmfw_conf {
3171 my ($vmid) = @_;
3172
3173 my $vmfw_conffile = "$pvefw_conf_dir/$vmid.fw";
3174
3175 unlink $vmfw_conffile;
3176}
3177
5471ff7c
AG
3178sub clone_vmfw_conf {
3179 my ($vmid, $newid) = @_;
3180
3181 my $sourcevm_conffile = "$pvefw_conf_dir/$vmid.fw";
3182 my $clonevm_conffile = "$pvefw_conf_dir/$newid.fw";
3183
3184 if (-f $clonevm_conffile) {
3185 unlink $clonevm_conffile;
3186 }
3187 if (-f $sourcevm_conffile) {
3188 my $data = PVE::Tools::file_get_contents($sourcevm_conffile);
3189 PVE::Tools::file_set_contents($clonevm_conffile, $data);
3190 }
3191}
3192
6fc63ffd 3193sub read_vm_firewall_configs {
40af93c4 3194 my ($cluster_conf, $vmdata, $dir) = @_;
8aef5177 3195
6fc63ffd 3196 my $vmfw_configs = {};
c8301d63 3197
b6b8e6ad 3198 foreach my $vmid (keys %{$vmdata->{qemu}}) {
40af93c4 3199 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm', $vmid, $dir);
b6b8e6ad
DM
3200 next if !$vmfw_conf->{options}; # skip if file does not exists
3201 $vmfw_configs->{$vmid} = $vmfw_conf;
3202 }
3b4882dc 3203 foreach my $vmid (keys %{$vmdata->{lxc}}) {
40af93c4 3204 my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct', $vmid, $dir);
3b4882dc
AG
3205 next if !$vmfw_conf->{options}; # skip if file does not exists
3206 $vmfw_configs->{$vmid} = $vmfw_conf;
3207 }
5e1267a5 3208
6fc63ffd 3209 return $vmfw_configs;
5e1267a5
DM
3210}
3211
2d404ffc
DM
3212sub get_option_log_level {
3213 my ($options, $k) = @_;
3214
3215 my $v = $options->{$k};
178a63be 3216 $v = $default_log_level if !defined($v);
2d404ffc
DM
3217
3218 return undef if $v eq '' || $v eq 'nolog';
3219
3489f8a2 3220 return $v if defined($log_level_hash->{$v});
2d404ffc
DM
3221
3222 warn "unknown log level ($k = '$v')\n";
3223
3224 return undef;
3225}
3226
d8f2505e 3227sub generate_std_chains {
db8a955f
DM
3228 my ($ruleset, $options, $ipversion) = @_;
3229
3230 my $std_chains = $pve_std_chains->{$ipversion} || die "internal error";
cbb5d6f3 3231
2d404ffc 3232 my $loglevel = get_option_log_level($options, 'smurf_log_level');
044409e5
TW
3233 my $chain = 'PVEFW-smurflog';
3234 if ( $std_chains->{$chain} ) {
3235 foreach my $r (@{$std_chains->{$chain}}) {
3236 $r->{log} = $loglevel;
3237 }
db8a955f 3238 }
2d404ffc
DM
3239
3240 # same as shorewall logflags action.
3241 $loglevel = get_option_log_level($options, 'tcp_flags_log_level');
782c4cde 3242 $chain = 'PVEFW-logflags';
044409e5
TW
3243 if ( $std_chains->{$chain} ) {
3244 foreach my $r (@{$std_chains->{$chain}}) {
3245 $r->{log} = $loglevel;
3246 }
3247 }
d8f2505e 3248
db8a955f 3249 foreach my $chain (keys %$std_chains) {
d8f2505e 3250 ruleset_create_chain($ruleset, $chain);
db8a955f 3251 foreach my $rule (@{$std_chains->{$chain}}) {
d8f2505e 3252 if (ref($rule)) {
3489f8a2 3253 ruleset_generate_rule($ruleset, $chain, $ipversion, $rule, 0);
d8f2505e 3254 } else {
044409e5 3255 die "rule $rule as string - should not happen";
d8f2505e
DM
3256 }
3257 }
3258 }
3259}
3260
34cdedfa 3261sub generate_ipset_chains {
74601077 3262 my ($ipset_ruleset, $clusterfw_conf, $fw_conf, $device_ips, $ipsets) = @_;
34cdedfa 3263
74601077 3264 foreach my $ipset (keys %{$ipsets}) {
34cdedfa 3265
74601077 3266 my $options = $ipsets->{$ipset};
34cdedfa 3267
ebf72e49
WB
3268 if ($device_ips && $ipset =~ /^ipfilter-(net\d+)$/) {
3269 if (my $ips = $device_ips->{$1}) {
3270 $options = [@$options, @$ips];
3271 }
3272 }
3273
88c26d5e
DM
3274 # remove duplicates
3275 my $nethash = {};
3276 foreach my $entry (@$options) {
3277 next if $entry->{errors}; # skip entries with errors
3278 eval {
3279 my ($cidr, $ver);
3280 if ($entry->{cidr} =~ m/^${ip_alias_pattern}$/) {
3281 ($cidr, $ver) = resolve_alias($clusterfw_conf, $fw_conf, $entry->{cidr});
3282 } else {
3283 ($cidr, $ver) = parse_ip_or_cidr($entry->{cidr});
3284 }
3285 #http://backreference.org/2013/03/01/ipv6-address-normalization/
3286 if ($ver == 6) {
e0a9139f
WB
3287 # ip_compress_address takes an address only, no CIDR
3288 my ($addr, $prefix_len) = ($cidr =~ m@^([^/]*)(/.*)?$@);
3289 $cidr = lc(Net::IP::ip_compress_address($addr, 6));
3290 $cidr .= $prefix_len if defined($prefix_len);
88c26d5e
DM
3291 $cidr =~ s|/128$||;
3292 } else {
3293 $cidr =~ s|/32$||;
3294 }
34cdedfa 3295
88c26d5e
DM
3296 $nethash->{$ver}->{$cidr} = { cidr => $cidr, nomatch => $entry->{nomatch} };
3297 };
3298 warn $@ if $@;
3299 }
6d959e3f 3300
88c26d5e
DM
3301 foreach my $ipversion (4, 6) {
3302 my $data = $nethash->{$ipversion};
30f1b100 3303
88c26d5e 3304 my $name = compute_ipset_chain_name($fw_conf->{vmid}, $ipset, $ipversion);
6d959e3f 3305
88c26d5e
DM
3306 my $hashsize = scalar(@$options);
3307 if ($hashsize <= 64) {
3308 $hashsize = 64;
3309 } else {
3310 $hashsize = round_powerof2($hashsize);
3311 }
6d959e3f 3312
88c26d5e 3313 my $family = $ipversion == "6" ? "inet6" : "inet";
30f1b100 3314
88c26d5e 3315 $ipset_ruleset->{$name} = ["create $name hash:net family $family hashsize $hashsize maxelem $hashsize"];
6d959e3f 3316
88c26d5e
DM
3317 foreach my $cidr (sort keys %$data) {
3318 my $entry = $data->{$cidr};
6d959e3f 3319
88c26d5e
DM
3320 my $cmd = "add $name $cidr";
3321 if ($entry->{nomatch}) {
3322 if ($feature_ipset_nomatch) {
3323 push @{$ipset_ruleset->{$name}}, "$cmd nomatch";
3324 } else {
3325 warn "ignore !$cidr - nomatch not supported by kernel\n";
3326 }
6d959e3f 3327 } else {
88c26d5e 3328 push @{$ipset_ruleset->{$name}}, $cmd;
6d959e3f 3329 }
9d6f90e6 3330 }
9d6f90e6 3331 }
34cdedfa 3332 }
2a052ee3
AD
3333}
3334
3335sub round_powerof2 {
dd7a13fd 3336 my ($int) = @_;
2a052ee3 3337
dd7a13fd
DM
3338 $int--;
3339 $int |= $int >> $_ foreach (1,2,4,8,16);
3340 return ++$int;
34cdedfa
AD
3341}
3342
cc37e000
TL
3343my $set_global_log_ratelimit = sub {
3344 my $cluster_opts = shift;
3345
3346 $global_log_ratelimit = '--limit 1/sec';
3347 if (defined(my $log_rlimit = $cluster_opts->{log_ratelimit})) {
3348 my $ll_format = $cluster_option_properties->{log_ratelimit}->{format};
3349 my $limit = PVE::JSONSchema::parse_property_string($ll_format, $log_rlimit);
3350
3351 if ($limit->{enable}) {
3352 if (my $rate = $limit->{rate}) {
3353 $global_log_ratelimit = "--limit $rate";
3354 }
3355 if (my $burst = $limit->{burst}) {
3356 $global_log_ratelimit .= " --limit-burst $burst";
3357 }
3358 } else {
3359 $global_log_ratelimit = undef;
3360 }
3361 }
3362};
3363
fca39c2c 3364sub load_clusterfw_conf {
40af93c4 3365 my ($filename) = @_;
8aef5177
DM
3366
3367 $filename = $clusterfw_conf_filename if !defined($filename);
585ede43
TL
3368 my $empty_conf = {
3369 rules => [],
3370 options => {},
3371 aliases => {},
3372 groups => {},
3373 group_comments => {},
3374 ipset => {} ,
3375 ipset_comments => {},
3376 };
530c005e 3377
585ede43
TL
3378 my $cluster_conf = generic_fw_config_parser($filename, $empty_conf, $empty_conf, 'cluster');
3379 $set_global_log_ratelimit->($cluster_conf->{options});
5e1267a5 3380
fca39c2c 3381 return $cluster_conf;
e5d76bde
DM
3382}
3383
fca39c2c
DM
3384sub save_clusterfw_conf {
3385 my ($cluster_conf) = @_;
9c7e0858
DM
3386
3387 my $raw = '';
9c7e0858 3388
c6f5cc88 3389 my $options = $cluster_conf->{options};
89ea63c8 3390 $raw .= &$format_options($options) if $options && scalar(keys %$options);
9c7e0858 3391
0d5f0a0f 3392 my $aliases = $cluster_conf->{aliases};
89ea63c8 3393 $raw .= &$format_aliases($aliases) if $aliases && scalar(keys %$aliases);
e34d0e58 3394
89ea63c8 3395 $raw .= &$format_ipsets($cluster_conf) if $cluster_conf->{ipset};
1210ae94 3396
c6f5cc88 3397 my $rules = $cluster_conf->{rules};
89ea63c8 3398 if ($rules && scalar(@$rules)) {
c6f5cc88 3399 $raw .= "[RULES]\n\n";
63c91681 3400 $raw .= &$format_rules($rules, 1);
c6f5cc88
DM
3401 $raw .= "\n";
3402 }
3403
89ea63c8
DM
3404 if ($cluster_conf->{groups}) {
3405 foreach my $group (sort keys %{$cluster_conf->{groups}}) {
3406 my $rules = $cluster_conf->{groups}->{$group};
3407 if (my $comment = $cluster_conf->{group_comments}->{$group}) {
3408 my $utf8comment = encode('utf8', $comment);
3409 $raw .= "[group $group] # $utf8comment\n\n";
3410 } else {
3411 $raw .= "[group $group]\n\n";
3412 }
0d22acb3 3413
89ea63c8
DM
3414 $raw .= &$format_rules($rules, 0);
3415 $raw .= "\n";
3416 }
9c7e0858
DM
3417 }
3418
c32e04e6
FG
3419 if ($raw) {
3420 mkdir $pvefw_conf_dir;
3421 PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw);
3422 } else {
3423 unlink $clusterfw_conf_filename;
3424 }
9c7e0858
DM
3425}
3426
8b27beb9 3427sub load_hostfw_conf {
40af93c4 3428 my ($cluster_conf, $filename) = @_;
8aef5177
DM
3429
3430 $filename = $hostfw_conf_filename if !defined($filename);
8b27beb9 3431
585ede43
TL
3432 my $empty_conf = { rules => [], options => {}};
3433 return generic_fw_config_parser($filename, $cluster_conf, $empty_conf, 'host');
8b27beb9
DM
3434}
3435
63c91681
DM
3436sub save_hostfw_conf {
3437 my ($hostfw_conf) = @_;
3438
3439 my $raw = '';
3440
3441 my $options = $hostfw_conf->{options};
89ea63c8 3442 $raw .= &$format_options($options) if $options && scalar(keys %$options);
cbb5d6f3 3443
63c91681 3444 my $rules = $hostfw_conf->{rules};
89ea63c8 3445 if ($rules && scalar(@$rules)) {
63c91681
DM
3446 $raw .= "[RULES]\n\n";
3447 $raw .= &$format_rules($rules, 1);
3448 $raw .= "\n";
3449 }
3450
c32e04e6
FG
3451 if ($raw) {
3452 PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
3453 } else {
3454 unlink $hostfw_conf_filename;
3455 }
63c91681
DM
3456}
3457
e5d76bde 3458sub compile {
40af93c4 3459 my ($cluster_conf, $hostfw_conf, $vmdata) = @_;
3dfa8a7f 3460
8aef5177
DM
3461 my $vmfw_configs;
3462
cfd7cd9c
TW
3463 # fixme: once we read standard chains from config this needs to be put in test/standard cases below
3464 $pve_std_chains = dclone($pve_std_chains_conf);
3465
8aef5177
DM
3466 if ($vmdata) { # test mode
3467 my $testdir = $vmdata->{testdir} || die "no test directory specified";
3468 my $filename = "$testdir/cluster.fw";
40af93c4 3469 $cluster_conf = load_clusterfw_conf($filename);
8aef5177
DM
3470
3471 $filename = "$testdir/host.fw";
40af93c4 3472 $hostfw_conf = load_hostfw_conf($cluster_conf, $filename);
8aef5177 3473
40af93c4 3474 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir);
8aef5177 3475 } else { # normal operation
40af93c4 3476 $cluster_conf = load_clusterfw_conf(undef) if !$cluster_conf;
8aef5177 3477
40af93c4 3478 $hostfw_conf = load_hostfw_conf($cluster_conf, undef) if !$hostfw_conf;
8aef5177
DM
3479
3480 $vmdata = read_local_vm_config();
40af93c4 3481 $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef);
8aef5177 3482 }
3dfa8a7f 3483
84025e99 3484 return ({},{},{},{}) if !$cluster_conf->{options}->{enable};
9268573a 3485
525778d7
DM
3486 my $localnet;
3487 if ($cluster_conf->{aliases}->{local_network}) {
3488 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3489 } else {
afcd29b3
DM
3490 my $localnet_ver;
3491 ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
3492
84025e99 3493 $cluster_conf->{aliases}->{local_network} = {
afcd29b3 3494 name => 'local_network', cidr => $localnet, ipversion => $localnet_ver };
525778d7
DM
3495 }
3496
3497 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
bfc488f6 3498
40af93c4
TL
3499 my $ruleset = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 4);
3500 my $rulesetv6 = compile_iptables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, 6);
3501 my $ebtables_ruleset = compile_ebtables_filter($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata);
147dd882
WB
3502 my $ipset_ruleset = compile_ipsets($cluster_conf, $vmfw_configs, $vmdata);
3503
c5e8b008 3504 return ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset);
147dd882
WB
3505}
3506
3507sub compile_iptables_filter {
40af93c4 3508 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata, $ipversion) = @_;
085fd492 3509
3fa83edf
DM
3510 my $ruleset = {};
3511
dec84fcd
DM
3512 ruleset_create_chain($ruleset, "PVEFW-INPUT");
3513 ruleset_create_chain($ruleset, "PVEFW-OUTPUT");
5b1df9a0 3514
fadb13dd 3515 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
e34d0e58 3516
8b27beb9 3517 my $hostfw_options = $hostfw_conf->{options} || {};
fadb13dd 3518
88733a74
AD
3519 # fixme: what log level should we use here?
3520 my $loglevel = get_option_log_level($hostfw_options, "log_level_out");
3521
b409c8a8
TL
3522 my $conn_allow_invalid = $hostfw_options->{nf_conntrack_allow_invalid} // 0;
3523 ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", $conn_allow_invalid, "ACCEPT");
88733a74 3524
e2943485 3525 ruleset_create_chain($ruleset, "PVEFW-FWBR-IN");
88c26d5e 3526 ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $ipversion, $hostfw_options, $cluster_conf, $loglevel);
e2943485 3527
1e9c5070 3528 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+", "-j PVEFW-FWBR-IN");
e2943485
DM
3529
3530 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
1e9c5070 3531 ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+", "-j PVEFW-FWBR-OUT");
e2943485 3532
db8a955f 3533 generate_std_chains($ruleset, $hostfw_options, $ipversion);
fadb13dd 3534
6d2ab017 3535 my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0));
2d404ffc 3536
35d1d6da 3537 if ($hostfw_enable) {
aedde2c2 3538 eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf, $ipversion); };
1a9978ed
DM
3539 warn $@ if $@; # just to be sure - should not happen
3540 }
3fa83edf 3541
6158271d 3542 # generate firewall rules for QEMU VMs
0a0ba19e 3543 foreach my $vmid (sort keys %{$vmdata->{qemu}}) {
1a9978ed
DM
3544 eval {
3545 my $conf = $vmdata->{qemu}->{$vmid};
3546 my $vmfw_conf = $vmfw_configs->{$vmid};
3547 return if !$vmfw_conf;
1a9978ed 3548
0a0ba19e 3549 foreach my $netid (sort keys %$conf) {
1a9978ed
DM
3550 next if $netid !~ m/^net(\d+)$/;
3551 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3552 next if !$net->{firewall};
3553 my $iface = "tap${vmid}i$1";
3554
3555 my $macaddr = $net->{macaddr};
3556 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
84870b1a 3557 $vmfw_conf, $vmid, 'IN', $ipversion);
1a9978ed 3558 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
84870b1a 3559 $vmfw_conf, $vmid, 'OUT', $ipversion);
1a9978ed
DM
3560 }
3561 };
3562 warn $@ if $@; # just to be sure - should not happen
3fa83edf 3563 }
c8301d63 3564
3b4882dc 3565 # generate firewall rules for LXC containers
0a0ba19e 3566 foreach my $vmid (sort keys %{$vmdata->{lxc}}) {
3b4882dc
AG
3567 eval {
3568 my $conf = $vmdata->{lxc}->{$vmid};
3569 my $vmfw_conf = $vmfw_configs->{$vmid};
3570 return if !$vmfw_conf;
3571
3b4882dc 3572 if ($vmfw_conf->{options}->{enable}) {
0a0ba19e 3573 foreach my $netid (sort keys %$conf) {
3b4882dc 3574 next if $netid !~ m/^net(\d+)$/;
a7c85d56 3575 my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid});
3b4882dc
AG
3576 next if !$net->{firewall};
3577 my $iface = "veth${vmid}i$1";
3578 my $macaddr = $net->{hwaddr};
3579 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3580 $vmfw_conf, $vmid, 'IN', $ipversion);
3581 generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr,
3582 $vmfw_conf, $vmid, 'OUT', $ipversion);
3583 }
3584 }
3585 };
3586 warn $@ if $@; # just to be sure - should not happen
3587 }
3588
097820b0 3589 if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){
1e9c5070 3590 ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED", "-j PVEFW-IPS");
097820b0
AD
3591 }
3592
147dd882
WB
3593 return $ruleset;
3594}
3595
ebf72e49
WB
3596sub mac_to_linklocal {
3597 my ($macaddr) = @_;
3598 my @parts = split(/:/, $macaddr);
3599 # The standard link local address uses the fe80::/64 prefix with the
3600 # modified EUI-64 identifier derived from the MAC address by flipping the
3601 # universal/local bit and inserting FF:FE in the middle.
3602 # See RFC 4291.
3603 $parts[0] = sprintf("%02x", hex($parts[0]) ^ 0x02);
3604 my @meui64 = (@parts[0,1,2], 'ff', 'fe', @parts[3,4,5]);
3605 return "fe80::$parts[0]$parts[1]:$parts[2]FF:FE$parts[3]:$parts[4]$parts[5]";
3606}
3607
147dd882
WB
3608sub compile_ipsets {
3609 my ($cluster_conf, $vmfw_configs, $vmdata) = @_;
3610
3611 my $localnet;
3612 if ($cluster_conf->{aliases}->{local_network}) {
3613 $localnet = $cluster_conf->{aliases}->{local_network}->{cidr};
3614 } else {
3615 my $localnet_ver;
3616 ($localnet, $localnet_ver) = parse_ip_or_cidr(local_network() || '127.0.0.0/8');
3617
3618 $cluster_conf->{aliases}->{local_network} = {
3619 name => 'local_network', cidr => $localnet, ipversion => $localnet_ver };
3620 }
3621
3622 push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet };
3623
3624
3625 my $ipset_ruleset = {};
3626
3627 # generate ipsets for QEMU VMs
3628 foreach my $vmid (keys %{$vmdata->{qemu}}) {
3629 eval {
3630 my $conf = $vmdata->{qemu}->{$vmid};
3631 my $vmfw_conf = $vmfw_configs->{$vmid};
3632 return if !$vmfw_conf;
3633
74601077
WB
3634 # When the 'ipfilter' option is enabled every device for which there
3635 # is no 'ipfilter-netX' ipset defiend gets an implicit empty default
3636 # ipset.
3637 # The reason is that ipfilter ipsets are always filled with standard
3638 # IPv6 link-local filters.
3639 my $ipsets = $vmfw_conf->{ipset};
3640 my $implicit_sets = {};
3641
ebf72e49
WB
3642 my $device_ips = {};
3643 foreach my $netid (keys %$conf) {
3644 next if $netid !~ m/^net(\d+)$/;
3645 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3646 next if !$net->{firewall};
3647
74601077
WB
3648 if ($vmfw_conf->{options}->{ipfilter} && !$ipsets->{"ipfilter-$netid"}) {
3649 $implicit_sets->{"ipfilter-$netid"} = [];
3650 }
3651
ebf72e49
WB
3652 my $macaddr = $net->{macaddr};
3653 my $linklocal = mac_to_linklocal($macaddr);
3654 $device_ips->{$netid} = [
3655 { cidr => $linklocal },
3656 { cidr => 'fe80::/10', nomatch => 1 }
3657 ];
3658 }
3659
74601077
WB
3660 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf, $device_ips, $ipsets);
3661 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf, $device_ips, $implicit_sets);
147dd882
WB
3662 };
3663 warn $@ if $@; # just to be sure - should not happen
3664 }
3665
3666 # generate firewall rules for LXC containers
3667 foreach my $vmid (keys %{$vmdata->{lxc}}) {
aa229652
WB
3668 eval {
3669 my $conf = $vmdata->{lxc}->{$vmid};
3670 my $vmfw_conf = $vmfw_configs->{$vmid};
3671 return if !$vmfw_conf;
147dd882 3672
74601077
WB
3673 # When the 'ipfilter' option is enabled every device for which there
3674 # is no 'ipfilter-netX' ipset defiend gets an implicit empty default
3675 # ipset.
3676 # The reason is that ipfilter ipsets are always filled with standard
383fe679
WB
3677 # IPv6 link-local filters, as well as the IP addresses configured
3678 # for the container.
74601077
WB
3679 my $ipsets = $vmfw_conf->{ipset};
3680 my $implicit_sets = {};
3681
ebf72e49
WB
3682 my $device_ips = {};
3683 foreach my $netid (keys %$conf) {
3684 next if $netid !~ m/^net(\d+)$/;
a7c85d56 3685 my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid});
ebf72e49
WB
3686 next if !$net->{firewall};
3687
74601077
WB
3688 if ($vmfw_conf->{options}->{ipfilter} && !$ipsets->{"ipfilter-$netid"}) {
3689 $implicit_sets->{"ipfilter-$netid"} = [];
3690 }
3691
ebf72e49
WB
3692 my $macaddr = $net->{hwaddr};
3693 my $linklocal = mac_to_linklocal($macaddr);
383fe679 3694 my $set = $device_ips->{$netid} = [
ebf72e49
WB
3695 { cidr => $linklocal },
3696 { cidr => 'fe80::/10', nomatch => 1 }
3697 ];
37ef1ce1 3698 if (defined($net->{ip}) && $net->{ip} =~ m!^($IPV4RE)(?:/\d+)?$!) {
383fe679
WB
3699 push @$set, { cidr => $1 };
3700 }
37ef1ce1 3701 if (defined($net->{ip6}) && $net->{ip6} =~ m!^($IPV6RE)(?:/\d+)?$!) {
383fe679
WB
3702 push @$set, { cidr => $1 };
3703 }
ebf72e49
WB
3704 }
3705
74601077
WB
3706 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf, $device_ips, $ipsets);
3707 generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf, $device_ips, $implicit_sets);
aa229652
WB
3708 };
3709 warn $@ if $@; # just to be sure - should not happen
147dd882
WB
3710 }
3711
74601077 3712 generate_ipset_chains($ipset_ruleset, undef, $cluster_conf, undef, $cluster_conf->{ipset});
27083984 3713
147dd882 3714 return $ipset_ruleset;
3fa83edf
DM
3715}
3716
c5e8b008 3717sub compile_ebtables_filter {
40af93c4 3718 my ($cluster_conf, $hostfw_conf, $vmfw_configs, $vmdata) = @_;
c5e8b008 3719
2549e7ef 3720 if (!($cluster_conf->{options}->{ebtables} // 1)) {
84025e99
SI
3721 return {};
3722 }
c5e8b008
AD
3723
3724 my $ruleset = {};
3725
3726 ruleset_create_chain($ruleset, "PVEFW-FORWARD");
3727
c5e8b008
AD
3728 ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT");
3729 #for ipv4 and ipv6, check macaddress in iptables, so we use conntrack 'ESTABLISHED', to speedup rules
3730 ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-p IPv4', '-j ACCEPT');
3731 ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-p IPv6', '-j ACCEPT');
3732 ruleset_addrule($ruleset, 'PVEFW-FORWARD', '-o fwln+', '-j PVEFW-FWBR-OUT');
3733
3734 # generate firewall rules for QEMU VMs
2e30c5c7 3735 foreach my $vmid (sort keys %{$vmdata->{qemu}}) {
c5e8b008
AD
3736 eval {
3737 my $conf = $vmdata->{qemu}->{$vmid};
3738 my $vmfw_conf = $vmfw_configs->{$vmid};
3739 return if !$vmfw_conf;
401c141b 3740 my $ipsets = $vmfw_conf->{ipset};
c5e8b008 3741
4a1072dd 3742 foreach my $netid (sort keys %$conf) {
c5e8b008
AD
3743 next if $netid !~ m/^net(\d+)$/;
3744 my $net = PVE::QemuServer::parse_net($conf->{$netid});
3745 next if !$net->{firewall};
3746 my $iface = "tap${vmid}i$1";
3747 my $macaddr = $net->{macaddr};
401c141b
AD
3748 my $arpfilter = [];
3749 if (defined(my $ipset = $ipsets->{"ipfilter-$netid"})) {
3750 foreach my $ipaddr (@$ipset) {
3751 my($ip, $version) = parse_ip_or_cidr($ipaddr->{cidr});
3752 next if !$ip || ($version && $version != 4);
3753 push(@$arpfilter, $ip);
3754 }
3755 }
3756 generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter);
c5e8b008
AD
3757 }
3758 };
3759 warn $@ if $@; # just to be sure - should not happen
3760 }
3761
3762 # generate firewall rules for LXC containers
2e30c5c7 3763 foreach my $vmid (sort keys %{$vmdata->{lxc}}) {
c5e8b008
AD
3764 eval {
3765 my $conf = $vmdata->{lxc}->{$vmid};
3766
3767 my $vmfw_conf = $vmfw_configs->{$vmid};
3768 return if !$vmfw_conf || !$vmfw_conf->{options}->{enable};
401c141b 3769 my $ipsets = $vmfw_conf->{ipset};
c5e8b008 3770
4a1072dd 3771 foreach my $netid (sort keys %$conf) {
c5e8b008
AD
3772 next if $netid !~ m/^net(\d+)$/;
3773 my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid});
3774 next if !$net->{firewall};
3775 my $iface = "veth${vmid}i$1";
3776 my $macaddr = $net->{hwaddr};
401c141b
AD
3777 my $arpfilter = [];
3778 if (defined(my $ipset = $ipsets->{"ipfilter-$netid"})) {
3779 foreach my $ipaddr (@$ipset) {
3780 my($ip, $version) = parse_ip_or_cidr($ipaddr->{cidr});
3781 next if !$ip || ($version && $version != 4);
3782 push(@$arpfilter, $ip);
3783 }
3784 }
255698f6 3785 if (my $ip = $net->{ip} && $vmfw_conf->{options}->{ipfilter}) {
a9068b2d
TL
3786 # ebtables changes this to a .0/MASK network but we just
3787 # want the address here, no network - see #2193
255698f6
TL
3788 $ip =~ s|/(\d+)$||;
3789 push @$arpfilter, $ip;
556ae5c1 3790 }
401c141b 3791 generate_tap_layer2filter($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter);
c5e8b008
AD
3792 }
3793 };
3794 warn $@ if $@; # just to be sure - should not happen
3795 }
3796
3797 return $ruleset;
3798}
3799
3800sub generate_tap_layer2filter {
401c141b 3801 my ($ruleset, $iface, $macaddr, $vmfw_conf, $vmid, $arpfilter) = @_;
c5e8b008
AD
3802 my $options = $vmfw_conf->{options};
3803
3804 my $tapchain = $iface."-OUT";
3805
3806 # ebtables remove zeros from mac pairs
3807 $macaddr =~ s/0([0-9a-f])/$1/ig;
3808 $macaddr = lc($macaddr);
3809
3810 ruleset_create_chain($ruleset, $tapchain);
3811
3812 if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) {
3813 ruleset_addrule($ruleset, $tapchain, "-s ! $macaddr", '-j DROP');
3814 }
3815
401c141b
AD
3816 if (@$arpfilter){
3817 my $arpchain = $tapchain."-ARP";
3818 ruleset_addrule($ruleset, $tapchain, "-p ARP", "-j $arpchain");
3819 ruleset_create_chain($ruleset, $arpchain);
3820
3821 foreach my $ip (@{$arpfilter}) {
3822 ruleset_addrule($ruleset, $arpchain, "-p ARP --arp-ip-src $ip", '-j RETURN');
3823 }
3824 ruleset_addrule($ruleset, $arpchain, '', '-j DROP');
3825 }
3826
c5e8b008 3827 if (defined($options->{layer2_protocols})){
33efd363
AD
3828 my $protochain = $tapchain."-PROTO";
3829 ruleset_addrule($ruleset, $tapchain, '', "-j $protochain");
3830 ruleset_create_chain($ruleset, $protochain);
3831
c5e8b008 3832 foreach my $proto (split(/,/, $options->{layer2_protocols})) {
33efd363 3833 ruleset_addrule($ruleset, $protochain, "-p $proto", '-j RETURN');
c5e8b008 3834 }
33efd363 3835 ruleset_addrule($ruleset, $protochain, '', '-j DROP');
c5e8b008
AD
3836 }
3837
33efd363
AD
3838 ruleset_addrule($ruleset, $tapchain, '', '-j ACCEPT');
3839
c5e8b008
AD
3840 ruleset_addrule($ruleset, 'PVEFW-FWBR-OUT', "-i $iface", "-j $tapchain");
3841}
3842
84025e99
SI
3843# the parameter $change_only_regex changes two things if defined:
3844# * all chains not matching it will be left intact
3845# * both the $active_chains hash and the returned status_hash have different
3846# structure (they contain a key named 'rules').
3fa83edf 3847sub get_ruleset_status {
40af93c4 3848 my ($ruleset, $active_chains, $digest_fn, $change_only_regex) = @_;
3fa83edf
DM
3849
3850 my $statushash = {};
3851
3852 foreach my $chain (sort keys %$ruleset) {
84025e99
SI
3853 my $rules = $ruleset->{$chain};
3854 my $sig = &$digest_fn($rules);
3855 my $oldsig;
9bf7d929 3856
3fa83edf 3857 $statushash->{$chain}->{sig} = $sig;
84025e99
SI
3858 if (defined($change_only_regex)) {
3859 $oldsig = $active_chains->{$chain}->{sig};
3860 $statushash->{$chain}->{rules} = $rules;
3861 } else {
3862 $oldsig = $active_chains->{$chain};
3863 }
3fa83edf
DM
3864 if (!defined($oldsig)) {
3865 $statushash->{$chain}->{action} = 'create';
3866 } else {
3867 if ($oldsig eq $sig) {
3868 $statushash->{$chain}->{action} = 'exists';
3869 } else {
3870 $statushash->{$chain}->{action} = 'update';
3871 }
3872 }
84025e99
SI
3873 if ($verbose) {
3874 print "$statushash->{$chain}->{action} $chain ($sig)\n";
3875 foreach my $cmd (@{$rules}) {
3876 print "\t$cmd\n";
3877 }
3fa83edf
DM
3878 }
3879 }
3880
3881 foreach my $chain (sort keys %$active_chains) {
84025e99
SI
3882 next if defined($ruleset->{$chain});
3883 my $action = 'delete';
d9551052 3884 my $sig = $active_chains->{$chain};
84025e99
SI
3885 if (defined($change_only_regex)) {
3886 $action = 'ignore' if ($chain !~ m/$change_only_regex/);
3887 $statushash->{$chain}->{rules} = $active_chains->{$chain}->{rules};
d9551052 3888 $sig = $sig->{sig};
84025e99 3889 }
84025e99
SI
3890 $statushash->{$chain}->{action} = $action;
3891 $statushash->{$chain}->{sig} = $sig;
3892 print "$action $chain ($sig)\n" if $verbose;
6158271d 3893 }
2a052ee3 3894
3fa83edf
DM
3895 return $statushash;
3896}
3897
3fa83edf
DM
3898sub print_sig_rule {
3899 my ($chain, $sig) = @_;
3900
09d5f68e
DM
3901 # We just use this to store a SHA1 checksum used to detect changes
3902 return "-A $chain -m comment --comment \"PVESIG:$sig\"\n";
b6360c3f
DM
3903}
3904
df7cb349 3905sub get_ruleset_cmdlist {
40af93c4 3906 my ($ruleset, $iptablescmd) = @_;
3fa83edf 3907
3fa83edf 3908 my $cmdlist = "*filter\n"; # we pass this to iptables-restore;
cbb5d6f3 3909
17da5c0f 3910 my ($active_chains, $hooks) = iptables_get_chains($iptablescmd);
40af93c4 3911 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest);
3fa83edf
DM
3912
3913 # create missing chains first
3914 foreach my $chain (sort keys %$ruleset) {
3915 my $stat = $statushash->{$chain};
3916 die "internal error" if !$stat;
3917 next if $stat->{action} ne 'create';
886aba9c 3918
3fa83edf
DM
3919 $cmdlist .= ":$chain - [0:0]\n";
3920 }
3921
4bd0a9c4 3922 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
55fad3b7
DM
3923 my $chain = "PVEFW-$h";
3924 if ($ruleset->{$chain} && !$hooks->{$h}) {
3925 $cmdlist .= "-A $h -j $chain\n";
4bd0a9c4 3926 }
3fa83edf
DM
3927 }
3928
3929 foreach my $chain (sort keys %$ruleset) {
3930 my $stat = $statushash->{$chain};
3931 die "internal error" if !$stat;
3932
3933 if ($stat->{action} eq 'update' || $stat->{action} eq 'create') {
3934 $cmdlist .= "-F $chain\n";
3935 foreach my $cmd (@{$ruleset->{$chain}}) {
3936 $cmdlist .= "$cmd\n";
3937 }
3938 $cmdlist .= print_sig_rule($chain, $stat->{sig});
3939 } elsif ($stat->{action} eq 'delete') {
f5d28682 3940 die "internal error"; # this should not happen
3fa83edf
DM
3941 } elsif ($stat->{action} eq 'exists') {
3942 # do nothing
3943 } else {
3944 die "internal error - unknown status '$stat->{action}'";
3945 }
3946 }
3947
f5d28682
DM
3948 foreach my $chain (keys %$statushash) {
3949 next if $statushash->{$chain}->{action} ne 'delete';
3950 $cmdlist .= "-F $chain\n";
3951 }
3952 foreach my $chain (keys %$statushash) {
3953 next if $statushash->{$chain}->{action} ne 'delete';
fadb13dd
DM
3954 next if $chain eq 'PVEFW-INPUT';
3955 next if $chain eq 'PVEFW-OUTPUT';
3956 next if $chain eq 'PVEFW-FORWARD';
f5d28682
DM
3957 $cmdlist .= "-X $chain\n";
3958 }
3959
3f95d14a 3960 my $changes = $cmdlist ne "*filter\n" ? 1 : 0;
4bd0a9c4 3961
3fa83edf
DM
3962 $cmdlist .= "COMMIT\n";
3963
3f95d14a 3964 return wantarray ? ($cmdlist, $changes) : $cmdlist;
6b9f68a2
DM
3965}
3966
d4a23c88 3967my $pve_ebtables_chainname_regex = qr/PVEFW-\S+|(?:tap|veth)\d+i\d+-(?:IN|OUT)/;
84025e99 3968
151c209e 3969sub get_ebtables_cmdlist {
40af93c4 3970 my ($ruleset) = @_;
151c209e
AD
3971
3972 my $changes = 0;
3973 my $cmdlist = "*filter\n";
3974
84025e99
SI
3975 my $active_chains = ebtables_get_chains();
3976 my $statushash = get_ruleset_status($ruleset, $active_chains,
40af93c4 3977 \&iptables_chain_digest,
84025e99 3978 $pve_ebtables_chainname_regex);
151c209e 3979
84025e99
SI
3980 # create chains first and make sure PVE rules are evaluated if active
3981 my $append_pve_to_forward = '-A FORWARD -j PVEFW-FORWARD';
3982 my $pve_include = 0;
3983 foreach my $chain (sort keys %$statushash) {
3984 next if ($statushash->{$chain}->{action} eq 'delete');
151c209e 3985 $cmdlist .= ":$chain ACCEPT\n";
84025e99 3986 $pve_include = 1 if ($chain eq 'PVEFW-FORWARD');
151c209e
AD
3987 }
3988
84025e99 3989 foreach my $chain (sort keys %$statushash) {
151c209e 3990 my $stat = $statushash->{$chain};
84025e99
SI
3991 next if ($stat->{action} eq 'delete');
3992 $changes = 1 if ($stat->{action} !~ 'ignore|exists');
151c209e 3993
84025e99
SI
3994 foreach my $cmd (@{$statushash->{$chain}->{'rules'}}) {
3995 if ($chain eq 'FORWARD' && $cmd eq $append_pve_to_forward) {
3996 next if ! $pve_include;
3997 $pve_include = 0;
3998 }
151c209e
AD
3999 $cmdlist .= "$cmd\n";
4000 }
4001 }
84025e99 4002 $cmdlist .= "$append_pve_to_forward\n" if $pve_include;
151c209e
AD
4003
4004 return wantarray ? ($cmdlist, $changes) : $cmdlist;
4005}
4006
34cdedfa 4007sub get_ipset_cmdlist {
40af93c4 4008 my ($ruleset) = @_;
34cdedfa
AD
4009
4010 my $cmdlist = "";
4011
dd7a13fd
DM
4012 my $delete_cmdlist = "";
4013
4bd0a9c4 4014 my $active_chains = ipset_get_chains();
40af93c4 4015 my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest);
34cdedfa 4016
e34d0e58 4017 # remove stale _swap chains
30f1b100
DM
4018 foreach my $chain (keys %$active_chains) {
4019 if ($chain =~ m/^PVEFW-\S+_swap$/) {
4020 $cmdlist .= "destroy $chain\n";
4021 }
4022 }
4023
c69cf614 4024 foreach my $chain (keys %$ruleset) {
c69cf614
DM
4025 my $stat = $statushash->{$chain};
4026 die "internal error" if !$stat;
4027
4028 if ($stat->{action} eq 'create') {
4029 foreach my $cmd (@{$ruleset->{$chain}}) {
4030 $cmdlist .= "$cmd\n";
4031 }
4032 }
4033 }
4034
4035 foreach my $chain (keys %$ruleset) {
6d959e3f
DM
4036 my $stat = $statushash->{$chain};
4037 die "internal error" if !$stat;
34cdedfa 4038
dd7a13fd
DM
4039 if ($stat->{action} eq 'update') {
4040 my $chain_swap = $chain."_swap";
cbb5d6f3 4041
dd7a13fd
DM
4042 foreach my $cmd (@{$ruleset->{$chain}}) {
4043 $cmd =~ s/$chain/$chain_swap/;
4044 $cmdlist .= "$cmd\n";
34cdedfa 4045 }
dd7a13fd
DM
4046 $cmdlist .= "swap $chain_swap $chain\n";
4047 $cmdlist .= "flush $chain_swap\n";
4048 $cmdlist .= "destroy $chain_swap\n";
2a052ee3 4049 }
dd7a13fd 4050 }
3f95d14a 4051
88c26d5e 4052 # the remove unused chains
c69cf614 4053 foreach my $chain (keys %$statushash) {
dd7a13fd 4054 next if $statushash->{$chain}->{action} ne 'delete';
2a052ee3 4055
dd7a13fd
DM
4056 $delete_cmdlist .= "flush $chain\n";
4057 $delete_cmdlist .= "destroy $chain\n";
34cdedfa
AD
4058 }
4059
dd7a13fd 4060 my $changes = ($cmdlist || $delete_cmdlist) ? 1 : 0;
cbb5d6f3 4061
dd7a13fd 4062 return ($cmdlist, $delete_cmdlist, $changes);
34cdedfa
AD
4063}
4064
6b9f68a2 4065sub apply_ruleset {
40af93c4 4066 my ($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = @_;
6b9f68a2
DM
4067
4068 enable_bridge_firewall();
4069
cbb5d6f3 4070 my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) =
40af93c4 4071 get_ipset_cmdlist($ipset_ruleset);
6b9f68a2 4072
40af93c4
TL
4073 my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset);
4074 my ($cmdlistv6, $changesv6) = get_ruleset_cmdlist($rulesetv6, "ip6tables");
4075 my ($ebtables_cmdlist, $ebtables_changes) = get_ebtables_cmdlist($ebtables_ruleset);
2a052ee3 4076
81a0bf43
DM
4077 if ($verbose) {
4078 if ($ipset_changes) {
4079 print "ipset changes:\n";
4080 print $ipset_create_cmdlist if $ipset_create_cmdlist;
4081 print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
4082 }
3f95d14a 4083
81a0bf43
DM
4084 if ($changes) {
4085 print "iptables changes:\n";
4086 print $cmdlist;
4087 }
17da5c0f
AD
4088
4089 if ($changesv6) {
4090 print "ip6tables changes:\n";
4091 print $cmdlistv6;
4092 }
151c209e
AD
4093
4094 if ($ebtables_changes) {
4095 print "ebtables changes:\n";
4096 print $ebtables_cmdlist;
4097 }
81a0bf43 4098 }
3fa83edf 4099
259db1e6
DM
4100 my $tmpfile = "$pve_fw_status_dir/ipsetcmdlist1";
4101 PVE::Tools::file_set_contents($tmpfile, $ipset_create_cmdlist || '');
4102
2a052ee3 4103 ipset_restore_cmdlist($ipset_create_cmdlist);
34cdedfa 4104
259db1e6
DM
4105 $tmpfile = "$pve_fw_status_dir/ip4cmdlist";
4106 PVE::Tools::file_set_contents($tmpfile, $cmdlist || '');
4107
3fa83edf 4108 iptables_restore_cmdlist($cmdlist);
259db1e6
DM
4109
4110 $tmpfile = "$pve_fw_status_dir/ip6cmdlist";
4111 PVE::Tools::file_set_contents($tmpfile, $cmdlistv6 || '');
4112
17da5c0f 4113 ip6tables_restore_cmdlist($cmdlistv6);
3fa83edf 4114
259db1e6
DM
4115 $tmpfile = "$pve_fw_status_dir/ipsetcmdlist2";
4116 PVE::Tools::file_set_contents($tmpfile, $ipset_delete_cmdlist || '');
4117
dd7a13fd 4118 ipset_restore_cmdlist($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
2a052ee3 4119
151c209e
AD
4120 ebtables_restore_cmdlist($ebtables_cmdlist);
4121
4122 $tmpfile = "$pve_fw_status_dir/ebtablescmdlist";
4123 PVE::Tools::file_set_contents($tmpfile, $ebtables_cmdlist || '');
4124
6158271d 4125 # test: re-read status and check if everything is up to date
4bd0a9c4 4126 my $active_chains = iptables_get_chains();
40af93c4 4127 my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest);
3fa83edf
DM
4128
4129 my $errors;
4130 foreach my $chain (sort keys %$ruleset) {
4131 my $stat = $statushash->{$chain};
4132 if ($stat->{action} ne 'exists') {
4133 warn "unable to update chain '$chain'\n";
4134 $errors = 1;
4135 }
4136 }
b6360c3f 4137
17da5c0f 4138 my $active_chainsv6 = iptables_get_chains("ip6tables");
40af93c4 4139 my $statushashv6 = get_ruleset_status($rulesetv6, $active_chainsv6, \&iptables_chain_digest);
17da5c0f
AD
4140
4141 foreach my $chain (sort keys %$rulesetv6) {
4142 my $stat = $statushashv6->{$chain};
4143 if ($stat->{action} ne 'exists') {
4144 warn "unable to update chain '$chain'\n";
4145 $errors = 1;
4146 }
4147 }
4148
151c209e 4149 my $active_ebtables_chains = ebtables_get_chains();
84025e99
SI
4150 my $ebtables_statushash = get_ruleset_status($ebtables_ruleset,
4151 $active_ebtables_chains, \&iptables_chain_digest,
40af93c4 4152 $pve_ebtables_chainname_regex);
151c209e
AD
4153
4154 foreach my $chain (sort keys %$ebtables_ruleset) {
4155 my $stat = $ebtables_statushash->{$chain};
4156 if ($stat->{action} ne 'exists') {
4157 warn "ebtables : unable to update chain '$chain'\n";
4158 $errors = 1;
4159 }
4160 }
4161
3fa83edf 4162 die "unable to apply firewall changes\n" if $errors;
46a2ac1f
AD
4163
4164 update_nf_conntrack_max($hostfw_conf);
4165
4166 update_nf_conntrack_tcp_timeout_established($hostfw_conf);
4167
6c27f4f3 4168 update_nf_conntrack_logging($hostfw_conf);
b6360c3f
DM
4169}
4170
490cdead
DM
4171sub update_nf_conntrack_max {
4172 my ($hostfw_conf) = @_;
4173
4174 my $max = 65536; # reasonable default
4175
4176 my $options = $hostfw_conf->{options} || {};
4177
4178 if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) {
4179 $max = $options->{nf_conntrack_max};
4180 $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192
4181 }
4182
4183 my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max";
4184 my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize";
4185
4186 my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max);
4187
4188 if ($current != $max) {
4189 my $hashsize = int($max/4);
4190 PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize);
4191 PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max);
4192 }
4193}
4194
28c082a1
AD
4195sub update_nf_conntrack_tcp_timeout_established {
4196 my ($hostfw_conf) = @_;
4197
4198 my $options = $hostfw_conf->{options} || {};
4199
4200 my $value = defined($options->{nf_conntrack_tcp_timeout_established}) ? $options->{nf_conntrack_tcp_timeout_established} : 432000;
4201
4202 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value);
4203}
4204
6c27f4f3
DL
4205my $log_nf_conntrack_enabled = undef;
4206sub update_nf_conntrack_logging {
4207 my ($hostfw_conf) = @_;
4208
4209 my $options = $hostfw_conf->{options} || {};
4210 my $value = $options->{log_nf_conntrack} || 0;
4211 if (!defined($log_nf_conntrack_enabled)
4212 || $value != $log_nf_conntrack_enabled)
4213 {
4214 my $tmpfile = "$pve_fw_status_dir/log_nf_conntrack";
4215 PVE::Tools::file_set_contents($tmpfile, $value);
4216
4217 PVE::Tools::run_command([qw(systemctl try-reload-or-restart pvefw-logger.service)]);
4218 $log_nf_conntrack_enabled = $value;
4219 }
4220}
4221
c4a2e5ae
DM
4222sub remove_pvefw_chains {
4223
7b7b2654
AD
4224 PVE::Firewall::remove_pvefw_chains_iptables("iptables");
4225 PVE::Firewall::remove_pvefw_chains_iptables("ip6tables");
4226 PVE::Firewall::remove_pvefw_chains_ipset();
4227
4228}
4229
4230sub remove_pvefw_chains_iptables {
4231 my ($iptablescmd) = @_;
4232
4233 my ($chash, $hooks) = iptables_get_chains($iptablescmd);
c4a2e5ae
DM
4234 my $cmdlist = "*filter\n";
4235
4236 foreach my $h (qw(INPUT OUTPUT FORWARD)) {
4237 if ($hooks->{$h}) {
4238 $cmdlist .= "-D $h -j PVEFW-$h\n";
4239 }
4240 }
cbb5d6f3 4241
c4a2e5ae
DM
4242 foreach my $chain (keys %$chash) {
4243 $cmdlist .= "-F $chain\n";
4244 }
4245
4246 foreach my $chain (keys %$chash) {
4247 $cmdlist .= "-X $chain\n";
4248 }
4249 $cmdlist .= "COMMIT\n";
4250
7b7b2654
AD
4251 if($iptablescmd eq "ip6tables") {
4252 ip6tables_restore_cmdlist($cmdlist);
4253 } else {
4254 iptables_restore_cmdlist($cmdlist);
4255 }
4256}
4257
4258sub remove_pvefw_chains_ipset {
55fad3b7
DM
4259
4260 my $ipset_chains = ipset_get_chains();
4261
7b7b2654 4262 my $cmdlist = "";
55fad3b7
DM
4263
4264 foreach my $chain (keys %$ipset_chains) {
88c26d5e
DM
4265 $cmdlist .= "flush $chain\n";
4266 $cmdlist .= "destroy $chain\n";
55fad3b7
DM
4267 }
4268
7b7b2654 4269 ipset_restore_cmdlist($cmdlist) if $cmdlist;
c4a2e5ae
DM
4270}
4271
8b453a09
DM
4272sub init {
4273 my $cluster_conf = load_clusterfw_conf();
4274 my $cluster_options = $cluster_conf->{options};
4275 my $enable = $cluster_options->{enable};
4276
4277 return if !$enable;
4278
4279 # load required modules here
4280}
4281
6b9f68a2 4282sub update {
6b9f68a2 4283 my $code = sub {
3dfa8a7f
DM
4284
4285 my $cluster_conf = load_clusterfw_conf();
4286 my $cluster_options = $cluster_conf->{options};
4287
55fad3b7 4288 if (!$cluster_options->{enable}) {
b22130d3 4289 PVE::Firewall::remove_pvefw_chains();
3dfa8a7f
DM
4290 return;
4291 }
4292
50f9a28d 4293 my $hostfw_conf = load_hostfw_conf($cluster_conf);
3dfa8a7f 4294
c5e8b008 4295 my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = compile($cluster_conf, $hostfw_conf);
490cdead 4296
151c209e 4297 apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset, $rulesetv6, $ebtables_ruleset);
6b9f68a2
DM
4298 };
4299
4300 run_locked($code);
4301}
4302
b6360c3f 43031;