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