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