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