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