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