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