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