]>
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' => [ | |
93be4333 DM |
441 | "VNC traffic for VNC display's 0 - 99", |
442 | { action => 'PARAM', proto => 'tcp', dport => '5900:5999' }, | |
857f62c8 DM |
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 | ||
525778d7 | 690 | my $ipv4_mask_hash_localnet = { |
93be4333 DM |
691 | '255.255.0.0' => 16, |
692 | '255.255.128.0' => 17, | |
693 | '255.255.192.0' => 18, | |
694 | '255.255.224.0' => 19, | |
695 | '255.255.240.0' => 20, | |
696 | '255.255.248.0' => 21, | |
697 | '255.255.252.0' => 22, | |
698 | '255.255.254.0' => 23, | |
699 | '255.255.255.0' => 24, | |
700 | '255.255.255.128' => 25, | |
701 | '255.255.255.192' => 26, | |
702 | '255.255.255.224' => 27, | |
703 | '255.255.255.240' => 28, | |
704 | '255.255.255.248' => 29, | |
705 | '255.255.255.252' => 30, | |
706 | }; | |
707 | ||
525778d7 | 708 | my $__local_network; |
93be4333 | 709 | |
525778d7 | 710 | sub local_network { |
ac633d30 | 711 | my ($new_value) = @_; |
93be4333 | 712 | |
525778d7 | 713 | $__local_network = $new_value if defined($new_value); |
ac633d30 | 714 | |
525778d7 | 715 | return $__local_network if defined($__local_network); |
93be4333 DM |
716 | |
717 | eval { | |
718 | my $nodename = PVE::INotify::nodename(); | |
719 | ||
720 | my $ip = PVE::Cluster::remote_node_ip($nodename); | |
721 | ||
722 | my $testip = Net::IP->new($ip); | |
bfc488f6 | 723 | |
93be4333 DM |
724 | my $routes = PVE::ProcFSTools::read_proc_net_route(); |
725 | foreach my $entry (@$routes) { | |
525778d7 | 726 | my $mask = $ipv4_mask_hash_localnet->{$entry->{mask}}; |
93be4333 DM |
727 | next if !defined($mask); |
728 | return if $mask eq '0.0.0.0'; | |
729 | my $cidr = "$entry->{dest}/$mask"; | |
730 | my $testnet = Net::IP->new($cidr); | |
731 | if ($testnet->overlaps($testip)) { | |
525778d7 | 732 | $__local_network = $cidr; |
93be4333 DM |
733 | return; |
734 | } | |
735 | } | |
736 | }; | |
737 | warn $@ if $@; | |
738 | ||
525778d7 | 739 | return $__local_network; |
93be4333 DM |
740 | } |
741 | ||
708ba714 DM |
742 | my $max_ipset_name_length = 27; |
743 | ||
744 | sub compute_ipset_chain_name { | |
745 | my ($vmid, $ipset_name) = @_; | |
746 | ||
747 | $vmid = 0 if !defined($vmid); | |
748 | ||
749 | my $id = "$vmid-${ipset_name}"; | |
750 | ||
751 | ||
752 | if ((length($id) + 6) > $max_ipset_name_length) { | |
753 | $id = PVE::Tools::fnv31a_hex($id); | |
754 | } | |
755 | ||
756 | return "PVEFW-$id"; | |
757 | } | |
758 | ||
ecbea048 DM |
759 | sub parse_address_list { |
760 | my ($str) = @_; | |
761 | ||
28311400 | 762 | return if $str =~ m/^(\+)(\S+)$/; # ipset ref |
44be8ceb | 763 | return if $str =~ m/^${ip_alias_pattern}$/; |
d72beb8e | 764 | |
3162af6b DM |
765 | my $count = 0; |
766 | my $iprange = 0; | |
767 | foreach my $elem (split(/,/, $str)) { | |
768 | $count++; | |
769 | if (!Net::IP->new($elem)) { | |
ecbea048 DM |
770 | my $err = Net::IP::Error(); |
771 | die "invalid IP address: $err\n"; | |
772 | } | |
3162af6b | 773 | $iprange = 1 if $elem =~ m/-/; |
ecbea048 | 774 | } |
e34d0e58 | 775 | |
3162af6b | 776 | die "you can use a range in a list\n" if $iprange && $count > 1; |
ecbea048 | 777 | } |
dddd9413 | 778 | |
fcba0beb DM |
779 | sub parse_port_name_number_or_range { |
780 | my ($str) = @_; | |
781 | ||
782 | my $services = PVE::Firewall::get_etc_services(); | |
7ca36671 | 783 | my $count = 0; |
041b7e8e DM |
784 | my $icmp_port = 0; |
785 | ||
fcba0beb | 786 | foreach my $item (split(/,/, $str)) { |
7ca36671 DM |
787 | $count++; |
788 | if ($item =~ m/^(\d+):(\d+)$/) { | |
789 | my ($port1, $port2) = ($1, $2); | |
790 | die "invalid port '$port1'\n" if $port1 > 65535; | |
791 | die "invalid port '$port2'\n" if $port2 > 65535; | |
792 | } elsif ($item =~ m/^(\d+)$/) { | |
793 | my $port = $1; | |
794 | die "invalid port '$port'\n" if $port > 65535; | |
795 | } else { | |
041b7e8e DM |
796 | if ($icmp_type_names->{$item}) { |
797 | $icmp_port = 1; | |
798 | } else { | |
e34d0e58 | 799 | die "invalid port '$item'\n" if !$services->{byname}->{$item}; |
041b7e8e | 800 | } |
fcba0beb DM |
801 | } |
802 | } | |
803 | ||
041b7e8e DM |
804 | die "ICPM ports not allowed in port range\n" if $icmp_port && $count > 1; |
805 | ||
7ca36671 | 806 | return $count; |
fcba0beb DM |
807 | } |
808 | ||
54cd19a8 DM |
809 | PVE::JSONSchema::register_format('pve-fw-port-spec', \&pve_fw_verify_port_spec); |
810 | sub pve_fw_verify_port_spec { | |
811 | my ($portstr) = @_; | |
812 | ||
7ca36671 | 813 | parse_port_name_number_or_range($portstr); |
54cd19a8 DM |
814 | |
815 | return $portstr; | |
816 | } | |
817 | ||
818 | PVE::JSONSchema::register_format('pve-fw-v4addr-spec', \&pve_fw_verify_v4addr_spec); | |
819 | sub pve_fw_verify_v4addr_spec { | |
820 | my ($list) = @_; | |
821 | ||
822 | parse_address_list($list); | |
823 | ||
824 | return $list; | |
825 | } | |
826 | ||
827 | PVE::JSONSchema::register_format('pve-fw-protocol-spec', \&pve_fw_verify_protocol_spec); | |
828 | sub pve_fw_verify_protocol_spec { | |
829 | my ($proto) = @_; | |
830 | ||
831 | my $protocols = get_etc_protocols(); | |
832 | ||
833 | die "unknown protocol '$proto'\n" if $proto && | |
834 | !(defined($protocols->{byname}->{$proto}) || | |
835 | defined($protocols->{byid}->{$proto})); | |
836 | ||
837 | return $proto; | |
838 | } | |
839 | ||
840 | ||
d1c53b3e | 841 | # helper function for API |
d1c53b3e | 842 | |
5d38d64f DM |
843 | sub copy_opject_with_digest { |
844 | my ($object) = @_; | |
845 | ||
846 | my $sha = Digest::SHA->new('sha1'); | |
847 | ||
848 | my $res = {}; | |
849 | foreach my $k (sort keys %$object) { | |
850 | my $v = $object->{$k}; | |
0365eb68 | 851 | next if !defined($v); |
5d38d64f DM |
852 | $res->{$k} = $v; |
853 | $sha->add($k, ':', $v, "\n"); | |
854 | } | |
855 | ||
55e686c7 | 856 | my $digest = $sha->hexdigest; |
5d38d64f DM |
857 | |
858 | $res->{digest} = $digest; | |
859 | ||
860 | return wantarray ? ($res, $digest) : $res; | |
861 | } | |
862 | ||
863 | sub copy_list_with_digest { | |
864 | my ($list) = @_; | |
865 | ||
866 | my $sha = Digest::SHA->new('sha1'); | |
867 | ||
868 | my $res = []; | |
869 | foreach my $entry (@$list) { | |
870 | my $data = {}; | |
871 | foreach my $k (sort keys %$entry) { | |
872 | my $v = $entry->{$k}; | |
0365eb68 | 873 | next if !defined($v); |
5d38d64f | 874 | $data->{$k} = $v; |
6d9246e7 DM |
875 | # Note: digest ignores refs ($rule->{errors}) |
876 | $sha->add($k, ':', $v, "\n") if !ref($v); ; | |
5d38d64f DM |
877 | } |
878 | push @$res, $data; | |
879 | } | |
880 | ||
55e686c7 | 881 | my $digest = $sha->hexdigest; |
5d38d64f DM |
882 | |
883 | foreach my $entry (@$res) { | |
884 | $entry->{digest} = $digest; | |
885 | } | |
886 | ||
887 | return wantarray ? ($res, $digest) : $res; | |
888 | } | |
889 | ||
9c7e0858 DM |
890 | my $rule_properties = { |
891 | pos => { | |
892 | description => "Update rule at position <pos>.", | |
893 | type => 'integer', | |
894 | minimum => 0, | |
895 | optional => 1, | |
896 | }, | |
c71e785f | 897 | digest => get_standard_option('pve-config-digest'), |
9c7e0858 DM |
898 | type => { |
899 | type => 'string', | |
900 | optional => 1, | |
901 | enum => ['in', 'out', 'group'], | |
902 | }, | |
903 | action => { | |
c14dacdf | 904 | description => "Rule action ('ACCEPT', 'DROP', 'REJECT') or security group name.", |
9c7e0858 DM |
905 | type => 'string', |
906 | optional => 1, | |
44be8ceb | 907 | pattern => $security_group_name_pattern, |
c14dacdf DM |
908 | maxLength => 20, |
909 | minLength => 2, | |
9c7e0858 | 910 | }, |
e5076eee DM |
911 | macro => { |
912 | type => 'string', | |
913 | optional => 1, | |
54cd19a8 | 914 | maxLength => 128, |
e5076eee | 915 | }, |
54cd19a8 | 916 | iface => get_standard_option('pve-iface', { optional => 1 }), |
9c7e0858 | 917 | source => { |
54cd19a8 | 918 | type => 'string', format => 'pve-fw-v4addr-spec', |
9c7e0858 DM |
919 | optional => 1, |
920 | }, | |
921 | dest => { | |
54cd19a8 | 922 | type => 'string', format => 'pve-fw-v4addr-spec', |
9c7e0858 DM |
923 | optional => 1, |
924 | }, | |
925 | proto => { | |
54cd19a8 | 926 | type => 'string', format => 'pve-fw-protocol-spec', |
9c7e0858 DM |
927 | optional => 1, |
928 | }, | |
929 | enable => { | |
930 | type => 'boolean', | |
931 | optional => 1, | |
932 | }, | |
933 | sport => { | |
54cd19a8 | 934 | type => 'string', format => 'pve-fw-port-spec', |
9c7e0858 DM |
935 | optional => 1, |
936 | }, | |
937 | dport => { | |
54cd19a8 | 938 | type => 'string', format => 'pve-fw-port-spec', |
9c7e0858 DM |
939 | optional => 1, |
940 | }, | |
941 | comment => { | |
942 | type => 'string', | |
943 | optional => 1, | |
944 | }, | |
945 | }; | |
946 | ||
947 | sub add_rule_properties { | |
948 | my ($properties) = @_; | |
949 | ||
950 | foreach my $k (keys %$rule_properties) { | |
3655b01f DM |
951 | my $h = $rule_properties->{$k}; |
952 | # copy data, so that we can modify later without side effects | |
953 | foreach my $opt (keys %$h) { $properties->{$k}->{$opt} = $h->{$opt}; } | |
9c7e0858 | 954 | } |
cbb5d6f3 | 955 | |
9c7e0858 DM |
956 | return $properties; |
957 | } | |
958 | ||
5b7974df DM |
959 | sub delete_rule_properties { |
960 | my ($rule, $delete_str) = @_; | |
e34d0e58 | 961 | |
5b7974df DM |
962 | foreach my $opt (PVE::Tools::split_list($delete_str)) { |
963 | raise_param_exc({ 'delete' => "no such property ('$opt')"}) | |
964 | if !defined($rule_properties->{$opt}); | |
965 | raise_param_exc({ 'delete' => "unable to delete required property '$opt'"}) | |
966 | if $opt eq 'type' || $opt eq 'action'; | |
967 | delete $rule->{$opt}; | |
968 | } | |
969 | ||
970 | return $rule; | |
971 | } | |
972 | ||
9a2745a0 DM |
973 | my $apply_macro = sub { |
974 | my ($macro_name, $param, $verify) = @_; | |
975 | ||
976 | my $macro_rules = $pve_fw_parsed_macros->{$macro_name}; | |
977 | die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen | |
978 | ||
979 | my $rules = []; | |
980 | ||
981 | foreach my $templ (@$macro_rules) { | |
982 | my $rule = {}; | |
983 | my $param_used = {}; | |
984 | foreach my $k (keys %$templ) { | |
985 | my $v = $templ->{$k}; | |
986 | if ($v eq 'PARAM') { | |
987 | $v = $param->{$k}; | |
988 | $param_used->{$k} = 1; | |
989 | } elsif ($v eq 'DEST') { | |
990 | $v = $param->{dest}; | |
991 | $param_used->{dest} = 1; | |
992 | } elsif ($v eq 'SOURCE') { | |
993 | $v = $param->{source}; | |
994 | $param_used->{source} = 1; | |
995 | } | |
996 | ||
997 | if (!defined($v)) { | |
998 | my $msg = "missing parameter '$k' in macro '$macro_name'"; | |
e34d0e58 | 999 | raise_param_exc({ macro => $msg }) if $verify; |
9a2745a0 DM |
1000 | die "$msg\n"; |
1001 | } | |
1002 | $rule->{$k} = $v; | |
1003 | } | |
1004 | foreach my $k (keys %$param) { | |
1005 | next if $k eq 'macro'; | |
1006 | next if !defined($param->{$k}); | |
1007 | next if $param_used->{$k}; | |
1008 | if (defined($rule->{$k})) { | |
1009 | if ($rule->{$k} ne $param->{$k}) { | |
1010 | my $msg = "parameter '$k' already define in macro (value = '$rule->{$k}')"; | |
e34d0e58 | 1011 | raise_param_exc({ $k => $msg }) if $verify; |
9a2745a0 DM |
1012 | die "$msg\n"; |
1013 | } | |
1014 | } else { | |
1015 | $rule->{$k} = $param->{$k}; | |
1016 | } | |
1017 | } | |
1018 | push @$rules, $rule; | |
1019 | } | |
1020 | ||
1021 | return $rules; | |
1022 | }; | |
1023 | ||
b6b8e6ad DM |
1024 | my $rule_env_iface_lookup = { |
1025 | 'ct' => 1, | |
1026 | 'vm' => 1, | |
1027 | 'group' => 0, | |
1028 | 'cluster' => 1, | |
1029 | 'host' => 1, | |
1030 | }; | |
1031 | ||
7ca36671 | 1032 | sub verify_rule { |
a523e057 | 1033 | my ($rule, $cluster_conf, $fw_conf, $rule_env, $noerr) = @_; |
b6b8e6ad DM |
1034 | |
1035 | my $allow_groups = $rule_env eq 'group' ? 0 : 1; | |
bfc488f6 | 1036 | |
b6b8e6ad DM |
1037 | my $allow_iface = $rule_env_iface_lookup->{$rule_env}; |
1038 | die "unknown rule_env '$rule_env'\n" if !defined($allow_iface); # should not happen | |
7ca36671 | 1039 | |
a523e057 DM |
1040 | my $errors = $rule->{errors} || {}; |
1041 | ||
6d9246e7 | 1042 | my $error_count = 0; |
7ca36671 | 1043 | |
6d9246e7 DM |
1044 | my $add_error = sub { |
1045 | my ($param, $msg) = @_; | |
d4cda423 | 1046 | chomp $msg; |
6d9246e7 DM |
1047 | raise_param_exc({ $param => $msg }) if !$noerr; |
1048 | $error_count++; | |
1049 | $errors->{$param} = $msg if !$errors->{$param}; | |
1050 | }; | |
1051 | ||
a523e057 DM |
1052 | my $check_ipset_or_alias_property = sub { |
1053 | my ($name) = @_; | |
1054 | ||
1055 | if (my $value = $rule->{$name}) { | |
1056 | if ($value =~ m/^\+/) { | |
1057 | if ($value =~ m/^\+(${security_group_name_pattern})$/) { | |
bfc488f6 | 1058 | &$add_error($name, "no such ipset '$1'") |
a523e057 | 1059 | if !($cluster_conf->{ipset}->{$1} || ($fw_conf && $fw_conf->{ipset}->{$1})); |
bfc488f6 | 1060 | |
a523e057 DM |
1061 | } else { |
1062 | &$add_error($name, "invalid security group name '$value'"); | |
1063 | } | |
1064 | } elsif ($value =~ m/^${ip_alias_pattern}$/){ | |
1065 | my $alias = lc($value); | |
bfc488f6 | 1066 | &$add_error($name, "no such alias '$value'") |
a523e057 DM |
1067 | if !($cluster_conf->{aliases}->{$alias} || ($fw_conf && $fw_conf->{aliases}->{$alias})) |
1068 | } | |
1069 | } | |
1070 | }; | |
1071 | ||
6d9246e7 DM |
1072 | my $type = $rule->{type}; |
1073 | my $action = $rule->{action}; | |
bfc488f6 | 1074 | |
6d9246e7 DM |
1075 | &$add_error('type', "missing property") if !$type; |
1076 | &$add_error('action', "missing property") if !$action; | |
1077 | ||
1078 | if ($type) { | |
1079 | if ($type eq 'in' || $type eq 'out') { | |
1080 | &$add_error('action', "unknown action '$action'") | |
1081 | if $action && ($action !~ m/^(ACCEPT|DROP|REJECT)$/); | |
1082 | } elsif ($type eq 'group') { | |
1083 | &$add_error('type', "security groups not allowed") | |
1084 | if !$allow_groups; | |
1085 | &$add_error('action', "invalid characters in security group name") | |
1086 | if $action && ($action !~ m/^${security_group_name_pattern}$/); | |
1087 | } else { | |
1088 | &$add_error('type', "unknown rule type '$type'"); | |
1089 | } | |
7ca36671 | 1090 | } |
e34d0e58 | 1091 | |
dba740a9 | 1092 | if ($rule->{iface}) { |
bfc488f6 | 1093 | &$add_error('type', "parameter -i not allowed for this rule type") |
b6b8e6ad | 1094 | if !$allow_iface; |
dba740a9 | 1095 | eval { PVE::JSONSchema::pve_verify_iface($rule->{iface}); }; |
6d9246e7 | 1096 | &$add_error('iface', $@) if $@; |
b6b8e6ad DM |
1097 | if ($rule_env eq 'vm') { |
1098 | &$add_error('iface', "value does not match the regex pattern 'net\\d+'") | |
1099 | if $rule->{iface} !~ m/^net(\d+)$/; | |
1100 | } elsif ($rule_env eq 'ct') { | |
1101 | &$add_error('iface', "value does not match the regex pattern '(venet|eth\\d+)'") | |
1102 | if $rule->{iface} !~ m/^(venet|eth(\d+))$/; | |
1103 | } | |
1104 | } | |
7ca36671 DM |
1105 | |
1106 | if ($rule->{macro}) { | |
6d9246e7 DM |
1107 | if (my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})}) { |
1108 | $rule->{macro} = $preferred_name; | |
1109 | } else { | |
1110 | &$add_error('macro', "unknown macro '$rule->{macro}'"); | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | if ($rule->{proto}) { | |
1115 | eval { pve_fw_verify_protocol_spec($rule->{proto}); }; | |
1116 | &$add_error('proto', $@) if $@; | |
1117 | } | |
7ca36671 DM |
1118 | |
1119 | if ($rule->{dport}) { | |
1120 | eval { parse_port_name_number_or_range($rule->{dport}); }; | |
6d9246e7 DM |
1121 | &$add_error('dport', $@) if $@; |
1122 | &$add_error('proto', "missing property - 'dport' requires this property") | |
914f9a50 | 1123 | if !$rule->{proto}; |
6d9246e7 | 1124 | } |
7ca36671 DM |
1125 | |
1126 | if ($rule->{sport}) { | |
1127 | eval { parse_port_name_number_or_range($rule->{sport}); }; | |
6d9246e7 DM |
1128 | &$add_error('sport', $@) if $@; |
1129 | &$add_error('proto', "missing property - 'sport' requires this property") | |
914f9a50 | 1130 | if !$rule->{proto}; |
7ca36671 DM |
1131 | } |
1132 | ||
1133 | if ($rule->{source}) { | |
1134 | eval { parse_address_list($rule->{source}); }; | |
6d9246e7 | 1135 | &$add_error('source', $@) if $@; |
a523e057 | 1136 | &$check_ipset_or_alias_property('source'); |
7ca36671 DM |
1137 | } |
1138 | ||
1139 | if ($rule->{dest}) { | |
1140 | eval { parse_address_list($rule->{dest}); }; | |
6d9246e7 | 1141 | &$add_error('dest', $@) if $@; |
a523e057 | 1142 | &$check_ipset_or_alias_property('dest'); |
7ca36671 DM |
1143 | } |
1144 | ||
a523e057 | 1145 | if ($rule->{macro} && !$error_count) { |
6d9246e7 DM |
1146 | eval { &$apply_macro($rule->{macro}, $rule, 1); }; |
1147 | if (my $err = $@) { | |
1148 | if (ref($err) eq "PVE::Exception" && $err->{errors}) { | |
1149 | my $eh = $err->{errors}; | |
1150 | foreach my $p (keys %$eh) { | |
1151 | &$add_error($p, $eh->{$p}); | |
1152 | } | |
1153 | } else { | |
1154 | &$add_error('macro', "$err"); | |
1155 | } | |
1156 | } | |
9a2745a0 DM |
1157 | } |
1158 | ||
6d9246e7 DM |
1159 | $rule->{errors} = $errors if $error_count; |
1160 | ||
7ca36671 DM |
1161 | return $rule; |
1162 | } | |
1163 | ||
9c7e0858 DM |
1164 | sub copy_rule_data { |
1165 | my ($rule, $param) = @_; | |
1166 | ||
1167 | foreach my $k (keys %$rule_properties) { | |
1168 | if (defined(my $v = $param->{$k})) { | |
1169 | if ($v eq '' || $v eq '-') { | |
1170 | delete $rule->{$k}; | |
1171 | } else { | |
1172 | $rule->{$k} = $v; | |
1173 | } | |
9c7e0858 DM |
1174 | } |
1175 | } | |
7ca36671 | 1176 | |
9c7e0858 DM |
1177 | return $rule; |
1178 | } | |
1179 | ||
1180 | # core functions | |
780bcc0f DM |
1181 | my $bridge_firewall_enabled = 0; |
1182 | ||
1183 | sub enable_bridge_firewall { | |
1184 | ||
1185 | return if $bridge_firewall_enabled; # only once | |
1186 | ||
5f0a912c DM |
1187 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1"); |
1188 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1"); | |
780bcc0f | 1189 | |
6a8a75db DM |
1190 | # make sure syncookies are enabled (which is default on newer 3.X kernels anyways) |
1191 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1"); | |
1192 | ||
780bcc0f DM |
1193 | $bridge_firewall_enabled = 1; |
1194 | } | |
1195 | ||
8cebfa6f | 1196 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; |
dddd9413 | 1197 | |
b16e818e DM |
1198 | sub iptables_restore_cmdlist { |
1199 | my ($cmdlist) = @_; | |
3a616aa0 | 1200 | |
3fa83edf | 1201 | run_command("/sbin/iptables-restore -n", input => $cmdlist); |
3a616aa0 AD |
1202 | } |
1203 | ||
34cdedfa AD |
1204 | sub ipset_restore_cmdlist { |
1205 | my ($cmdlist) = @_; | |
1206 | ||
dd7a13fd | 1207 | run_command("/usr/sbin/ipset restore", input => $cmdlist); |
34cdedfa AD |
1208 | } |
1209 | ||
de2a57cd DM |
1210 | sub iptables_get_chains { |
1211 | ||
1212 | my $res = {}; | |
1213 | ||
1214 | # check what chains we want to track | |
1215 | my $is_pvefw_chain = sub { | |
1216 | my $name = shift; | |
1217 | ||
dec84fcd DM |
1218 | return 1 if $name =~ m/^PVEFW-\S+$/; |
1219 | ||
de2a57cd | 1220 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; |
954f24b1 DM |
1221 | |
1222 | return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable | |
1223 | ||
9e15114a DM |
1224 | return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/; |
1225 | ||
a01c32c7 | 1226 | return 1 if $name =~ m/^fwbr\d+(v\d+)?-(:?FW|IN|OUT|IPS)$/; |
f50656c2 | 1227 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; |
de2a57cd DM |
1228 | |
1229 | return undef; | |
1230 | }; | |
1231 | ||
1232 | my $table = ''; | |
1233 | ||
c4a2e5ae DM |
1234 | my $hooks = {}; |
1235 | ||
de2a57cd DM |
1236 | my $parser = sub { |
1237 | my $line = shift; | |
1238 | ||
1239 | return if $line =~ m/^#/; | |
1240 | return if $line =~ m/^\s*$/; | |
1241 | ||
1242 | if ($line =~ m/^\*(\S+)$/) { | |
1243 | $table = $1; | |
1244 | return; | |
1245 | } | |
1246 | ||
1247 | return if $table ne 'filter'; | |
1248 | ||
1249 | if ($line =~ m/^:(\S+)\s/) { | |
1250 | my $chain = $1; | |
1251 | return if !&$is_pvefw_chain($chain); | |
3fa83edf | 1252 | $res->{$chain} = "unknown"; |
09d5f68e | 1253 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { |
3fa83edf | 1254 | my ($chain, $sig) = ($1, $2); |
de2a57cd | 1255 | return if !&$is_pvefw_chain($chain); |
3fa83edf | 1256 | $res->{$chain} = $sig; |
c4a2e5ae DM |
1257 | } elsif ($line =~ m/^-A\s+(INPUT|OUTPUT|FORWARD)\s+-j\s+PVEFW-\1$/) { |
1258 | $hooks->{$1} = 1; | |
de2a57cd DM |
1259 | } else { |
1260 | # simply ignore the rest | |
1261 | return; | |
1262 | } | |
1263 | }; | |
1264 | ||
1265 | run_command("/sbin/iptables-save", outfunc => $parser); | |
1266 | ||
c4a2e5ae | 1267 | return wantarray ? ($res, $hooks) : $res; |
de2a57cd DM |
1268 | } |
1269 | ||
9bf7d929 DM |
1270 | sub iptables_chain_digest { |
1271 | my ($rules) = @_; | |
1272 | my $digest = Digest::SHA->new('sha1'); | |
1273 | foreach my $rule (@$rules) { # order is important | |
1274 | $digest->add($rule); | |
1275 | } | |
1276 | return $digest->b64digest; | |
1277 | } | |
1278 | ||
3f95d14a DM |
1279 | sub ipset_chain_digest { |
1280 | my ($rules) = @_; | |
4bd0a9c4 | 1281 | |
3f95d14a DM |
1282 | my $digest = Digest::SHA->new('sha1'); |
1283 | foreach my $rule (sort @$rules) { # note: sorted | |
1284 | $digest->add($rule); | |
1285 | } | |
1286 | return $digest->b64digest; | |
1287 | } | |
1288 | ||
34cdedfa AD |
1289 | sub ipset_get_chains { |
1290 | ||
1291 | my $res = {}; | |
1292 | my $chains = {}; | |
1293 | ||
1294 | my $parser = sub { | |
1295 | my $line = shift; | |
1296 | ||
1297 | return if $line =~ m/^#/; | |
1298 | return if $line =~ m/^\s*$/; | |
4b96e877 | 1299 | if ($line =~ m/^(?:\S+)\s(PVEFW-\S+)\s(?:\S+).*/) { |
81a0bf43 DM |
1300 | my $chain = $1; |
1301 | $line =~ s/\s+$//; # delete trailing white space | |
1302 | push @{$chains->{$chain}}, $line; | |
34cdedfa AD |
1303 | } else { |
1304 | # simply ignore the rest | |
1305 | return; | |
1306 | } | |
1307 | }; | |
1308 | ||
3f95d14a | 1309 | run_command("/usr/sbin/ipset save", outfunc => $parser); |
34cdedfa | 1310 | |
3f95d14a DM |
1311 | # compute digest for each chain |
1312 | foreach my $chain (keys %$chains) { | |
1313 | $res->{$chain} = ipset_chain_digest($chains->{$chain}); | |
34cdedfa AD |
1314 | } |
1315 | ||
1316 | return $res; | |
1317 | } | |
1318 | ||
eba0fb64 | 1319 | sub ruleset_generate_cmdstr { |
e523d2bb | 1320 | my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; |
3a616aa0 | 1321 | |
00bb4391 | 1322 | return if defined($rule->{enable}) && !$rule->{enable}; |
6d9246e7 | 1323 | return if $rule->{errors}; |
11bac5c2 | 1324 | |
e5076eee DM |
1325 | die "unable to emit macro - internal error" if $rule->{macro}; # should not happen |
1326 | ||
1327 | my $nbdport = defined($rule->{dport}) ? parse_port_name_number_or_range($rule->{dport}) : 0; | |
1328 | my $nbsport = defined($rule->{sport}) ? parse_port_name_number_or_range($rule->{sport}) : 0; | |
e5076eee | 1329 | |
41524a58 | 1330 | my @cmd = (); |
3a616aa0 | 1331 | |
eba0fb64 DM |
1332 | push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in}; |
1333 | push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out}; | |
1334 | ||
ba791b1f AD |
1335 | my $source = $rule->{source}; |
1336 | my $dest = $rule->{dest}; | |
1337 | ||
cbb5d6f3 | 1338 | if ($source) { |
44be8ceb DM |
1339 | if ($source =~ m/^\+/) { |
1340 | if ($source =~ m/^\+(${security_group_name_pattern})$/) { | |
e523d2bb DM |
1341 | my $name = $1; |
1342 | if ($fw_conf && $fw_conf->{ipset}->{$name}) { | |
708ba714 DM |
1343 | my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name); |
1344 | push @cmd, "-m set --match-set ${ipset_chain} src"; | |
e523d2bb | 1345 | } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { |
708ba714 DM |
1346 | my $ipset_chain = compute_ipset_chain_name(0, $name); |
1347 | push @cmd, "-m set --match-set ${ipset_chain} src"; | |
e523d2bb DM |
1348 | } else { |
1349 | die "no such ipset '$name'\n"; | |
1350 | } | |
44be8ceb DM |
1351 | } else { |
1352 | die "invalid security group name '$source'\n"; | |
1353 | } | |
1354 | } elsif ($source =~ m/^${ip_alias_pattern}$/){ | |
81d574a7 | 1355 | my $alias = lc($source); |
bfc488f6 | 1356 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; |
e523d2bb DM |
1357 | $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; |
1358 | die "no such alias '$source'\n" if !$e; | |
81d574a7 | 1359 | push @cmd, "-s $e->{cidr}"; |
ac8242cc | 1360 | } elsif ($source =~ m/\-/){ |
ba791b1f | 1361 | push @cmd, "-m iprange --src-range $source"; |
cbb5d6f3 | 1362 | } else { |
ba791b1f AD |
1363 | push @cmd, "-s $source"; |
1364 | } | |
1365 | } | |
1366 | ||
cbb5d6f3 | 1367 | if ($dest) { |
44be8ceb DM |
1368 | if ($dest =~ m/^\+/) { |
1369 | if ($dest =~ m/^\+(${security_group_name_pattern})$/) { | |
e523d2bb DM |
1370 | my $name = $1; |
1371 | if ($fw_conf && $fw_conf->{ipset}->{$name}) { | |
708ba714 DM |
1372 | my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $name); |
1373 | push @cmd, "-m set --match-set ${ipset_chain} src"; | |
e523d2bb | 1374 | } elsif ($cluster_conf && $cluster_conf->{ipset}->{$name}) { |
708ba714 DM |
1375 | my $ipset_chain = compute_ipset_chain_name(0, $name); |
1376 | push @cmd, "-m set --match-set ${ipset_chain} dst"; | |
e523d2bb DM |
1377 | } else { |
1378 | die "no such ipset '$name'\n"; | |
1379 | } | |
44be8ceb DM |
1380 | } else { |
1381 | die "invalid security group name '$dest'\n"; | |
1382 | } | |
1383 | } elsif ($dest =~ m/^${ip_alias_pattern}$/){ | |
51b40846 | 1384 | my $alias = lc($dest); |
bfc488f6 | 1385 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; |
e523d2bb DM |
1386 | $e = $cluster_conf->{aliases}->{$alias} if !$e && $cluster_conf; |
1387 | die "no such alias '$dest'\n" if !$e; | |
81d574a7 | 1388 | push @cmd, "-d $e->{cidr}"; |
cbb5d6f3 | 1389 | } elsif ($dest =~ m/^(\d+)\.(\d+).(\d+).(\d+)\-(\d+)\.(\d+).(\d+).(\d+)$/){ |
ba791b1f | 1390 | push @cmd, "-m iprange --dst-range $dest"; |
cbb5d6f3 | 1391 | } else { |
d6d2a385 | 1392 | push @cmd, "-d $dest"; |
ba791b1f AD |
1393 | } |
1394 | } | |
4b586518 | 1395 | |
181390b0 | 1396 | if ($rule->{proto}) { |
41524a58 | 1397 | push @cmd, "-p $rule->{proto}"; |
e3a1d391 | 1398 | |
181390b0 | 1399 | my $multiport = 0; |
e5076eee DM |
1400 | $multiport++ if $nbdport > 1; |
1401 | $multiport++ if $nbsport > 1; | |
e3a1d391 | 1402 | |
41524a58 | 1403 | push @cmd, "--match multiport" if $multiport; |
4b586518 | 1404 | |
cbb5d6f3 | 1405 | die "multiport: option '--sports' cannot be used together with '--dports'\n" |
181390b0 DM |
1406 | if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); |
1407 | ||
1408 | if ($rule->{dport}) { | |
1409 | if ($rule->{proto} && $rule->{proto} eq 'icmp') { | |
1410 | # Note: we use dport to store --icmp-type | |
1411 | die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}}); | |
41524a58 | 1412 | push @cmd, "-m icmp --icmp-type $rule->{dport}"; |
181390b0 | 1413 | } else { |
e5076eee | 1414 | if ($nbdport > 1) { |
181390b0 | 1415 | if ($multiport == 2) { |
41524a58 | 1416 | push @cmd, "--ports $rule->{dport}"; |
181390b0 | 1417 | } else { |
41524a58 | 1418 | push @cmd, "--dports $rule->{dport}"; |
181390b0 | 1419 | } |
e3a1d391 | 1420 | } else { |
41524a58 | 1421 | push @cmd, "--dport $rule->{dport}"; |
e3a1d391 | 1422 | } |
4b586518 DM |
1423 | } |
1424 | } | |
4b586518 | 1425 | |
181390b0 | 1426 | if ($rule->{sport}) { |
e5076eee | 1427 | if ($nbsport > 1) { |
41524a58 | 1428 | push @cmd, "--sports $rule->{sport}" if $multiport != 2; |
181390b0 | 1429 | } else { |
41524a58 | 1430 | push @cmd, "--sport $rule->{sport}"; |
181390b0 | 1431 | } |
4b586518 | 1432 | } |
181390b0 | 1433 | } elsif ($rule->{dport} || $rule->{sport}) { |
93d96f83 DM |
1434 | die "destination port '$rule->{dport}', but no protocol specified\n" if $rule->{dport}; |
1435 | die "source port '$rule->{sport}', but no protocol specified\n" if $rule->{sport}; | |
4b586518 DM |
1436 | } |
1437 | ||
41524a58 | 1438 | push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; |
4e6112f9 DM |
1439 | |
1440 | if (my $action = $rule->{action}) { | |
cbb5d6f3 | 1441 | $action = $actions->{$action} if defined($actions->{$action}); |
4e6112f9 | 1442 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; |
41524a58 | 1443 | push @cmd, $goto ? "-g $action" : "-j $action"; |
181390b0 | 1444 | } |
3a616aa0 | 1445 | |
eba0fb64 DM |
1446 | return scalar(@cmd) ? join(' ', @cmd) : undef; |
1447 | } | |
1448 | ||
1449 | sub ruleset_generate_rule { | |
e523d2bb | 1450 | my ($ruleset, $chain, $rule, $actions, $goto, $cluster_conf, $fw_conf) = @_; |
eba0fb64 | 1451 | |
e5076eee DM |
1452 | my $rules; |
1453 | ||
1454 | if ($rule->{macro}) { | |
1455 | $rules = &$apply_macro($rule->{macro}, $rule); | |
1456 | } else { | |
1457 | $rules = [ $rule ]; | |
1458 | } | |
1459 | ||
d4091b82 DM |
1460 | # update all or nothing |
1461 | ||
1462 | my @cmds = (); | |
cbb5d6f3 | 1463 | foreach my $tmp (@$rules) { |
e523d2bb | 1464 | if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $tmp, $actions, $goto, $cluster_conf, $fw_conf)) { |
d4091b82 | 1465 | push @cmds, $cmdstr; |
e5076eee | 1466 | } |
41524a58 | 1467 | } |
d4091b82 DM |
1468 | |
1469 | foreach my $cmdstr (@cmds) { | |
1470 | ruleset_addrule($ruleset, $chain, $cmdstr); | |
1471 | } | |
3fa83edf | 1472 | } |
0f168d7b | 1473 | |
eba0fb64 DM |
1474 | sub ruleset_generate_rule_insert { |
1475 | my ($ruleset, $chain, $rule, $actions, $goto) = @_; | |
1476 | ||
e5076eee DM |
1477 | die "implement me" if $rule->{macro}; # not implemented, because not needed so far |
1478 | ||
eba0fb64 DM |
1479 | if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $rule, $actions, $goto)) { |
1480 | ruleset_insertrule($ruleset, $chain, $cmdstr); | |
1481 | } | |
1482 | } | |
3fa83edf DM |
1483 | |
1484 | sub ruleset_create_chain { | |
1485 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 1486 | |
d050c724 | 1487 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; |
782c4cde | 1488 | die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format |
d050c724 | 1489 | |
3fa83edf DM |
1490 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; |
1491 | ||
1492 | $ruleset->{$chain} = []; | |
3a616aa0 AD |
1493 | } |
1494 | ||
3fa83edf DM |
1495 | sub ruleset_chain_exist { |
1496 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 1497 | |
3fa83edf DM |
1498 | return $ruleset->{$chain} ? 1 : undef; |
1499 | } | |
3a616aa0 | 1500 | |
3fa83edf DM |
1501 | sub ruleset_addrule { |
1502 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 1503 | |
3fa83edf | 1504 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 1505 | |
3fa83edf DM |
1506 | push @{$ruleset->{$chain}}, "-A $chain $rule"; |
1507 | } | |
3a616aa0 | 1508 | |
3fa83edf DM |
1509 | sub ruleset_insertrule { |
1510 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 1511 | |
3fa83edf | 1512 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 1513 | |
3fa83edf DM |
1514 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; |
1515 | } | |
3a616aa0 | 1516 | |
782c4cde DM |
1517 | sub get_log_rule_base { |
1518 | my ($chain, $vmid, $msg, $loglevel) = @_; | |
cbb5d6f3 | 1519 | |
782c4cde DM |
1520 | die "internal error - no log level" if !defined($loglevel); |
1521 | ||
1522 | $vmid = 0 if !defined($vmid); | |
1523 | ||
cbb5d6f3 | 1524 | # Note: we use special format for prefix to pass further |
782c4cde DM |
1525 | # info to log daemon (VMID, LOGVELEL and CHAIN) |
1526 | ||
1527 | return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\""; | |
1528 | } | |
1529 | ||
1530 | sub ruleset_addlog { | |
1531 | my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_; | |
1532 | ||
1533 | return if !defined($loglevel); | |
1534 | ||
1535 | my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel); | |
1536 | ||
1537 | $logrule = "$rule $logrule" if defined($rule); | |
1538 | ||
88733a74 | 1539 | ruleset_addrule($ruleset, $chain, $logrule); |
782c4cde DM |
1540 | } |
1541 | ||
ead850e8 | 1542 | sub ruleset_add_chain_policy { |
782c4cde | 1543 | my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_; |
ead850e8 DM |
1544 | |
1545 | if ($policy eq 'ACCEPT') { | |
1546 | ||
1547 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' }, | |
1548 | { ACCEPT => $accept_action}); | |
1549 | ||
1550 | } elsif ($policy eq 'DROP') { | |
1551 | ||
1552 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop"); | |
1553 | ||
782c4cde | 1554 | ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); |
ead850e8 DM |
1555 | |
1556 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
1557 | } elsif ($policy eq 'REJECT') { | |
1558 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject"); | |
1559 | ||
782c4cde | 1560 | ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); |
ead850e8 DM |
1561 | |
1562 | ruleset_addrule($ruleset, $chain, "-g PVEFW-reject"); | |
1563 | } else { | |
1564 | # should not happen | |
1565 | die "internal error: unknown policy '$policy'"; | |
1566 | } | |
1567 | } | |
1568 | ||
e2943485 DM |
1569 | sub ruleset_chain_add_conn_filters { |
1570 | my ($ruleset, $chain, $accept) = @_; | |
1571 | ||
1572 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); | |
1573 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept"); | |
1574 | } | |
1575 | ||
1576 | sub ruleset_chain_add_input_filters { | |
2428e394 AD |
1577 | my ($ruleset, $chain, $options, $cluster_conf, $loglevel) = @_; |
1578 | ||
1579 | if ($cluster_conf->{ipset}->{blacklist}){ | |
b5831a0d AD |
1580 | if (!ruleset_chain_exist($ruleset, "PVEFW-blacklist")) { |
1581 | ruleset_create_chain($ruleset, "PVEFW-blacklist"); | |
1582 | ruleset_addlog($ruleset, "PVEFW-blacklist", 0, "DROP: ", $loglevel) if $loglevel; | |
1583 | ruleset_addrule($ruleset, "PVEFW-blacklist", "-j DROP"); | |
1584 | } | |
708ba714 DM |
1585 | my $ipset_chain = compute_ipset_chain_name(0, 'blacklist'); |
1586 | ruleset_addrule($ruleset, $chain, "-m set --match-set ${ipset_chain} src -j PVEFW-blacklist"); | |
2428e394 | 1587 | } |
e2943485 DM |
1588 | |
1589 | if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) { | |
1590 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs"); | |
1591 | } | |
1592 | ||
1593 | if ($options->{tcpflags}) { | |
1594 | ruleset_addrule($ruleset, $chain, "-p tcp -j PVEFW-tcpflags"); | |
1595 | } | |
1596 | } | |
1597 | ||
9e15114a | 1598 | sub ruleset_create_vm_chain { |
58638f2c | 1599 | my ($ruleset, $chain, $options, $macaddr, $direction) = @_; |
3a616aa0 | 1600 | |
9e15114a | 1601 | ruleset_create_chain($ruleset, $chain); |
b47ecc88 | 1602 | my $accept = generate_nfqueue($options); |
3a616aa0 | 1603 | |
ce15d90b | 1604 | if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) { |
76a2d1e7 | 1605 | if ($direction eq 'OUT') { |
cbb5d6f3 | 1606 | ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK', |
0f168d7b | 1607 | proto => 'udp', sport => 68, dport => 67 }); |
76a2d1e7 | 1608 | } else { |
cbb5d6f3 | 1609 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT', |
0f168d7b | 1610 | proto => 'udp', sport => 67, dport => 68 }); |
76a2d1e7 | 1611 | } |
ce15d90b DM |
1612 | } |
1613 | ||
b21aca2c DM |
1614 | if ($direction eq 'OUT') { |
1615 | if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) { | |
9e15114a | 1616 | ruleset_addrule($ruleset, $chain, "-m mac ! --mac-source $macaddr -j DROP"); |
b21aca2c | 1617 | } |
9e15114a | 1618 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark |
c29f55c9 | 1619 | } |
9e15114a DM |
1620 | } |
1621 | ||
4bc6b5ac DM |
1622 | sub ruleset_add_group_rule { |
1623 | my ($ruleset, $cluster_conf, $chain, $rule, $direction, $action) = @_; | |
1624 | ||
1625 | my $group = $rule->{action}; | |
1626 | my $group_chain = "GROUP-$group-$direction"; | |
1627 | if(!ruleset_chain_exist($ruleset, $group_chain)){ | |
1628 | generate_group_rules($ruleset, $cluster_conf, $group); | |
1629 | } | |
bfc488f6 | 1630 | |
f8b12fff DM |
1631 | if ($direction eq 'OUT' && $rule->{iface_out}) { |
1632 | ruleset_addrule($ruleset, $chain, "-o $rule->{iface_out} -j $group_chain"); | |
1633 | } elsif ($direction eq 'IN' && $rule->{iface_in}) { | |
1634 | ruleset_addrule($ruleset, $chain, "-i $rule->{iface_in} -j $group_chain"); | |
4bc6b5ac DM |
1635 | } else { |
1636 | ruleset_addrule($ruleset, $chain, "-j $group_chain"); | |
1637 | } | |
1638 | ||
1639 | ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j $action"); | |
1640 | } | |
1641 | ||
9e15114a | 1642 | sub ruleset_generate_vm_rules { |
e523d2bb | 1643 | my ($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, $netid, $direction, $options) = @_; |
9e15114a DM |
1644 | |
1645 | my $lc_direction = lc($direction); | |
c29f55c9 | 1646 | |
6b8ca015 DM |
1647 | my $in_accept = generate_nfqueue($options); |
1648 | ||
92e976b3 DM |
1649 | foreach my $rule (@$rules) { |
1650 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
b7ab6989 | 1651 | next if !$rule->{enable} || $rule->{errors}; |
92e976b3 | 1652 | if ($rule->{type} eq 'group') { |
4bc6b5ac DM |
1653 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, $direction, |
1654 | $direction eq 'OUT' ? 'RETURN' : $in_accept); | |
92e976b3 DM |
1655 | } else { |
1656 | next if $rule->{type} ne $lc_direction; | |
921dfb33 DM |
1657 | eval { |
1658 | if ($direction eq 'OUT') { | |
1659 | ruleset_generate_rule($ruleset, $chain, $rule, | |
e34d0e58 | 1660 | { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, |
e523d2bb | 1661 | undef, $cluster_conf, $vmfw_conf); |
921dfb33 | 1662 | } else { |
e34d0e58 DM |
1663 | ruleset_generate_rule($ruleset, $chain, $rule, |
1664 | { ACCEPT => $in_accept , REJECT => "PVEFW-reject" }, | |
e523d2bb | 1665 | undef, $cluster_conf, $vmfw_conf); |
921dfb33 DM |
1666 | } |
1667 | }; | |
1668 | warn $@ if $@; | |
4e6112f9 | 1669 | } |
3a616aa0 | 1670 | } |
9e15114a DM |
1671 | } |
1672 | ||
b47ecc88 AD |
1673 | sub generate_nfqueue { |
1674 | my ($options) = @_; | |
1675 | ||
73089769 DM |
1676 | if ($options->{ips}) { |
1677 | my $action = "NFQUEUE"; | |
1678 | if ($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) { | |
1679 | if (defined($3) && defined($1)) { | |
b47ecc88 | 1680 | $action .= " --queue-balance $1:$3"; |
73089769 | 1681 | } elsif (defined($1)) { |
b47ecc88 AD |
1682 | $action .= " --queue-num $1"; |
1683 | } | |
1684 | } | |
8dc92812 | 1685 | $action .= " --queue-bypass" if $feature_ipset_nomatch; #need kernel 3.10 |
73089769 DM |
1686 | return $action; |
1687 | } else { | |
1688 | return "ACCEPT"; | |
b47ecc88 | 1689 | } |
b47ecc88 AD |
1690 | } |
1691 | ||
da6fc60b | 1692 | sub ruleset_generate_vm_ipsrules { |
a01c32c7 | 1693 | my ($ruleset, $options, $direction, $iface) = @_; |
da6fc60b AD |
1694 | |
1695 | if ($options->{ips} && $direction eq 'IN') { | |
1696 | my $nfqueue = generate_nfqueue($options); | |
1697 | ||
a01c32c7 | 1698 | if (!ruleset_chain_exist($ruleset, "PVEFW-IPS")) { |
da6fc60b AD |
1699 | ruleset_create_chain($ruleset, "PVEFW-IPS"); |
1700 | } | |
1701 | ||
a01c32c7 | 1702 | ruleset_addrule($ruleset, "PVEFW-IPS", "-m physdev --physdev-out $iface --physdev-is-bridged -j $nfqueue"); |
da6fc60b AD |
1703 | } |
1704 | } | |
1705 | ||
9e15114a | 1706 | sub generate_venet_rules_direction { |
58638f2c | 1707 | my ($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, $direction) = @_; |
9e15114a | 1708 | |
9e15114a DM |
1709 | my $lc_direction = lc($direction); |
1710 | ||
1711 | my $rules = $vmfw_conf->{rules}; | |
1712 | ||
1713 | my $options = $vmfw_conf->{options}; | |
1714 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
1715 | ||
1716 | my $chain = "venet0-$vmid-$direction"; | |
1717 | ||
58638f2c | 1718 | ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction); |
9e15114a | 1719 | |
e523d2bb | 1720 | ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $chain, 'venet', $direction); |
9e15114a DM |
1721 | |
1722 | # implement policy | |
1723 | my $policy; | |
1724 | ||
1725 | if ($direction eq 'OUT') { | |
1726 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default | |
1727 | } else { | |
1728 | $policy = $options->{policy_in} || 'DROP'; # allow nothing by default | |
1729 | } | |
1730 | ||
b47ecc88 AD |
1731 | my $accept = generate_nfqueue($options); |
1732 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; | |
782c4cde | 1733 | ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action); |
9e15114a | 1734 | |
eba0fb64 | 1735 | if ($direction eq 'OUT') { |
41389aed | 1736 | ruleset_generate_rule_insert($ruleset, "PVEFW-VENET-OUT", { |
eba0fb64 DM |
1737 | action => $chain, |
1738 | source => $ip, | |
1739 | iface_in => 'venet0'}); | |
1740 | } else { | |
41389aed | 1741 | ruleset_generate_rule($ruleset, "PVEFW-VENET-IN", { |
eba0fb64 DM |
1742 | action => $chain, |
1743 | dest => $ip, | |
1744 | iface_out => 'venet0'}); | |
1745 | } | |
9e15114a DM |
1746 | } |
1747 | ||
1748 | sub generate_tap_rules_direction { | |
58638f2c | 1749 | my ($ruleset, $cluster_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $direction) = @_; |
9e15114a DM |
1750 | |
1751 | my $lc_direction = lc($direction); | |
1752 | ||
1753 | my $rules = $vmfw_conf->{rules}; | |
1754 | ||
1755 | my $options = $vmfw_conf->{options}; | |
1756 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
1757 | ||
1758 | my $tapchain = "$iface-$direction"; | |
1759 | ||
58638f2c | 1760 | ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction); |
9e15114a | 1761 | |
e523d2bb | 1762 | ruleset_generate_vm_rules($ruleset, $rules, $cluster_conf, $vmfw_conf, $tapchain, $netid, $direction, $options); |
3a616aa0 | 1763 | |
a01c32c7 | 1764 | ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface); |
da6fc60b | 1765 | |
ccae0b50 DM |
1766 | # implement policy |
1767 | my $policy; | |
1768 | ||
1769 | if ($direction eq 'OUT') { | |
72f63fde | 1770 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default |
ccae0b50 | 1771 | } else { |
72f63fde | 1772 | $policy = $options->{policy_in} || 'DROP'; # allow nothing by default |
ccae0b50 DM |
1773 | } |
1774 | ||
b47ecc88 AD |
1775 | my $accept = generate_nfqueue($options); |
1776 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; | |
782c4cde | 1777 | ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action); |
3a616aa0 | 1778 | |
3fa83edf | 1779 | # plug the tap chain to bridge chain |
3cc81077 | 1780 | if ($direction eq 'IN') { |
a01c32c7 DM |
1781 | ruleset_addrule($ruleset, "PVEFW-FWBR-IN", |
1782 | "-m physdev --physdev-is-bridged --physdev-out $iface -j $tapchain"); | |
3cc81077 | 1783 | } else { |
a01c32c7 DM |
1784 | ruleset_addrule($ruleset, "PVEFW-FWBR-OUT", |
1785 | "-m physdev --physdev-is-bridged --physdev-in $iface -j $tapchain"); | |
3cc81077 | 1786 | } |
3a616aa0 AD |
1787 | } |
1788 | ||
d18c1e2b | 1789 | sub enable_host_firewall { |
fca39c2c | 1790 | my ($ruleset, $hostfw_conf, $cluster_conf) = @_; |
0bd5f137 | 1791 | |
92e976b3 | 1792 | my $options = $hostfw_conf->{options}; |
63324b09 | 1793 | my $cluster_options = $cluster_conf->{options}; |
92e976b3 | 1794 | my $rules = $hostfw_conf->{rules}; |
35f0c37e | 1795 | my $cluster_rules = $cluster_conf->{rules}; |
178a63be | 1796 | |
3fa83edf | 1797 | # host inbound firewall |
dec84fcd DM |
1798 | my $chain = "PVEFW-HOST-IN"; |
1799 | ruleset_create_chain($ruleset, $chain); | |
0bd5f137 | 1800 | |
178a63be DM |
1801 | my $loglevel = get_option_log_level($options, "log_level_in"); |
1802 | ||
e2943485 | 1803 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); |
4ac863a6 | 1804 | |
e2943485 | 1805 | ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); |
2428e394 | 1806 | ruleset_chain_add_input_filters($ruleset, $chain, $options, $cluster_conf, $loglevel); |
fb424a00 | 1807 | |
23e888f8 DM |
1808 | # we use RETURN because we need to check also tap rules |
1809 | my $accept_action = 'RETURN'; | |
1810 | ||
cc8dc02f DM |
1811 | ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast |
1812 | ||
35f0c37e DM |
1813 | # add host rules first, so that cluster wide rules can be overwritten |
1814 | foreach my $rule (@$rules, @$cluster_rules) { | |
5383df39 | 1815 | next if !$rule->{enable} || $rule->{errors}; |
bfc488f6 | 1816 | |
f8b12fff | 1817 | $rule->{iface_in} = $rule->{iface} if $rule->{iface}; |
5383df39 | 1818 | |
1a9978ed DM |
1819 | eval { |
1820 | if ($rule->{type} eq 'group') { | |
1821 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'IN', $accept_action); | |
1822 | } elsif ($rule->{type} eq 'in') { | |
bfc488f6 | 1823 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, |
e523d2bb | 1824 | undef, $cluster_conf, $hostfw_conf); |
1a9978ed DM |
1825 | } |
1826 | }; | |
1827 | warn $@ if $@; | |
f8b12fff | 1828 | delete $rule->{iface_in}; |
0bd5f137 | 1829 | } |
eb399cef DM |
1830 | |
1831 | # allow standard traffic for management ipset (includes cluster network) | |
708ba714 DM |
1832 | my $mngmnt_ipset_chain = compute_ipset_chain_name(0, "management"); |
1833 | my $mngmntsrc = "-m set --match-set ${mngmnt_ipset_chain} src"; | |
eb399cef | 1834 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 8006 -j $accept_action"); # PVE API |
bfc488f6 | 1835 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 5900:5999 -j $accept_action"); # PVE VNC Console |
eb399cef DM |
1836 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 3128 -j $accept_action"); # SPICE Proxy |
1837 | ruleset_addrule($ruleset, $chain, "$mngmntsrc -p tcp --dport 22 -j $accept_action"); # SSH | |
bfc488f6 | 1838 | |
525778d7 | 1839 | my $localnet = local_network(); |
3bc79f87 | 1840 | |
eb399cef | 1841 | # corosync |
525778d7 | 1842 | if ($localnet) { |
c5191f57 | 1843 | my $corosync_rule = "-p udp --dport 5404:5405 -j $accept_action"; |
525778d7 DM |
1844 | ruleset_addrule($ruleset, $chain, "-s $localnet -d $localnet $corosync_rule"); |
1845 | ruleset_addrule($ruleset, $chain, "-s $localnet -m addrtype --dst-type MULTICAST $corosync_rule"); | |
3bc79f87 | 1846 | } |
0bd5f137 | 1847 | |
23e888f8 | 1848 | # implement input policy |
63324b09 | 1849 | my $policy = $cluster_options->{policy_in} || 'DROP'; # allow nothing by default |
782c4cde | 1850 | ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action); |
0bd5f137 | 1851 | |
3fa83edf | 1852 | # host outbound firewall |
aadd745e | 1853 | $chain = "PVEFW-HOST-OUT"; |
dec84fcd DM |
1854 | ruleset_create_chain($ruleset, $chain); |
1855 | ||
178a63be DM |
1856 | $loglevel = get_option_log_level($options, "log_level_out"); |
1857 | ||
dec84fcd | 1858 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); |
e2943485 DM |
1859 | |
1860 | ruleset_chain_add_conn_filters($ruleset, $chain, 'ACCEPT'); | |
1861 | ||
23e888f8 DM |
1862 | # we use RETURN because we may want to check other thigs later |
1863 | $accept_action = 'RETURN'; | |
1864 | ||
cc8dc02f DM |
1865 | ruleset_addrule($ruleset, $chain, "-p igmp -j $accept_action"); # important for multicast |
1866 | ||
35f0c37e DM |
1867 | # add host rules first, so that cluster wide rules can be overwritten |
1868 | foreach my $rule (@$rules, @$cluster_rules) { | |
5383df39 DM |
1869 | next if !$rule->{enable} || $rule->{errors}; |
1870 | ||
f8b12fff | 1871 | $rule->{iface_out} = $rule->{iface} if $rule->{iface}; |
1a9978ed DM |
1872 | eval { |
1873 | if ($rule->{type} eq 'group') { | |
1874 | ruleset_add_group_rule($ruleset, $cluster_conf, $chain, $rule, 'OUT', $accept_action); | |
1875 | } elsif ($rule->{type} eq 'out') { | |
bfc488f6 | 1876 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }, |
e523d2bb | 1877 | undef, $cluster_conf, $hostfw_conf); |
1a9978ed DM |
1878 | } |
1879 | }; | |
1880 | warn $@ if $@; | |
f8b12fff | 1881 | delete $rule->{iface_out}; |
0bd5f137 AD |
1882 | } |
1883 | ||
3bc79f87 | 1884 | # allow standard traffic on cluster network |
525778d7 DM |
1885 | if ($localnet) { |
1886 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 8006 -j $accept_action"); # PVE API | |
1887 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 22 -j $accept_action"); # SSH | |
bfc488f6 | 1888 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 5900:5999 -j $accept_action"); # PVE VNC Console |
525778d7 | 1889 | ruleset_addrule($ruleset, $chain, "-d $localnet -p tcp --dport 3128 -j $accept_action"); # SPICE Proxy |
bfc488f6 | 1890 | |
c5191f57 | 1891 | my $corosync_rule = "-p udp --dport 5404:5405 -j $accept_action"; |
525778d7 | 1892 | ruleset_addrule($ruleset, $chain, "-d $localnet $corosync_rule"); |
f573ae2c | 1893 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST $corosync_rule"); |
3bc79f87 DM |
1894 | } |
1895 | ||
23e888f8 | 1896 | # implement output policy |
63324b09 | 1897 | $policy = $cluster_options->{policy_out} || 'ACCEPT'; # allow everything by default |
782c4cde | 1898 | ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action); |
6158271d | 1899 | |
dec84fcd DM |
1900 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); |
1901 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
9d31b418 AD |
1902 | } |
1903 | ||
1904 | sub generate_group_rules { | |
fca39c2c | 1905 | my ($ruleset, $cluster_conf, $group) = @_; |
9d31b418 | 1906 | |
c6f5cc88 | 1907 | my $rules = $cluster_conf->{groups}->{$group}; |
6158271d | 1908 | |
b4deedab DM |
1909 | if (!$rules) { |
1910 | warn "no such security group '$group'\n"; | |
1911 | $rules = []; # create empty chain | |
1912 | } | |
1913 | ||
3fa83edf | 1914 | my $chain = "GROUP-${group}-IN"; |
9d31b418 | 1915 | |
3fa83edf | 1916 | ruleset_create_chain($ruleset, $chain); |
b47ecc88 | 1917 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark |
9d31b418 | 1918 | |
92e976b3 DM |
1919 | foreach my $rule (@$rules) { |
1920 | next if $rule->{type} ne 'in'; | |
ba791b1f | 1921 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }, undef, $cluster_conf); |
9d31b418 AD |
1922 | } |
1923 | ||
3fa83edf | 1924 | $chain = "GROUP-${group}-OUT"; |
9d31b418 | 1925 | |
3fa83edf | 1926 | ruleset_create_chain($ruleset, $chain); |
4e6112f9 | 1927 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark |
9d31b418 | 1928 | |
92e976b3 DM |
1929 | foreach my $rule (@$rules) { |
1930 | next if $rule->{type} ne 'out'; | |
1931 | # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to | |
1932 | # check also other tap rules later | |
cbb5d6f3 | 1933 | ruleset_generate_rule($ruleset, $chain, $rule, |
ba791b1f | 1934 | { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }, undef, $cluster_conf); |
9d31b418 | 1935 | } |
9d31b418 AD |
1936 | } |
1937 | ||
51bae274 DM |
1938 | my $MAX_NETS = 32; |
1939 | my $valid_netdev_names = {}; | |
1940 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
1941 | $valid_netdev_names->{"net$i"} = 1; | |
1942 | } | |
5e1267a5 | 1943 | |
51bae274 | 1944 | sub parse_fw_rule { |
a523e057 | 1945 | my ($prefix, $line, $cluster_conf, $fw_conf, $rule_env, $verbose) = @_; |
5e1267a5 | 1946 | |
dba740a9 | 1947 | chomp $line; |
51bae274 | 1948 | |
d4cda423 DM |
1949 | my $orig_line = $line; |
1950 | ||
6d9246e7 DM |
1951 | my $rule = {}; |
1952 | ||
11bac5c2 | 1953 | # we can add single line comments to the end of the rule |
6d9246e7 DM |
1954 | if ($line =~ s/#\s*(.*?)\s*$//) { |
1955 | $rule->{comment} = decode('utf8', $1); | |
1956 | } | |
11bac5c2 DM |
1957 | |
1958 | # we can disable a rule when prefixed with '|' | |
ea9e5116 | 1959 | |
6d9246e7 | 1960 | $rule->{enable} = $line =~ s/^\|// ? 0 : 1; |
51bae274 | 1961 | |
dba740a9 DM |
1962 | $line =~ s/^(\S+)\s+(\S+)\s*// || |
1963 | die "unable to parse rule: $line\n"; | |
6d9246e7 DM |
1964 | |
1965 | $rule->{type} = lc($1); | |
1966 | $rule->{action} = $2; | |
1967 | ||
1968 | if ($rule->{type} eq 'in' || $rule->{type} eq 'out') { | |
1969 | if ($rule->{action} =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
1970 | $rule->{macro} = $1; | |
1971 | $rule->{action} = $2; | |
92e976b3 | 1972 | } |
51bae274 DM |
1973 | } |
1974 | ||
dba740a9 DM |
1975 | while (length($line)) { |
1976 | if ($line =~ s/^-i (\S+)\s*//) { | |
6d9246e7 | 1977 | $rule->{iface} = $1; |
dba740a9 DM |
1978 | next; |
1979 | } | |
51bae274 | 1980 | |
6d9246e7 | 1981 | last if $rule->{type} eq 'group'; |
51bae274 | 1982 | |
dba740a9 | 1983 | if ($line =~ s/^-p (\S+)\s*//) { |
6d9246e7 | 1984 | $rule->{proto} = $1; |
dba740a9 DM |
1985 | next; |
1986 | } | |
6d9246e7 | 1987 | |
dba740a9 | 1988 | if ($line =~ s/^-dport (\S+)\s*//) { |
6d9246e7 | 1989 | $rule->{dport} = $1; |
dba740a9 DM |
1990 | next; |
1991 | } | |
6d9246e7 | 1992 | |
dba740a9 | 1993 | if ($line =~ s/^-sport (\S+)\s*//) { |
6d9246e7 | 1994 | $rule->{sport} = $1; |
dba740a9 DM |
1995 | next; |
1996 | } | |
1997 | if ($line =~ s/^-source (\S+)\s*//) { | |
6d9246e7 | 1998 | $rule->{source} = $1; |
dba740a9 DM |
1999 | next; |
2000 | } | |
2001 | if ($line =~ s/^-dest (\S+)\s*//) { | |
6d9246e7 | 2002 | $rule->{dest} = $1; |
dba740a9 DM |
2003 | next; |
2004 | } | |
51bae274 | 2005 | |
dba740a9 DM |
2006 | last; |
2007 | } | |
ba791b1f | 2008 | |
dba740a9 | 2009 | die "unable to parse rule parameters: $line\n" if length($line); |
51bae274 | 2010 | |
a523e057 | 2011 | $rule = verify_rule($rule, $cluster_conf, $fw_conf, $rule_env, 1); |
d4cda423 DM |
2012 | if ($verbose && $rule->{errors}) { |
2013 | warn "$prefix - errors in rule parameters: $orig_line\n"; | |
2014 | foreach my $p (keys %{$rule->{errors}}) { | |
2015 | warn " $p: $rule->{errors}->{$p}\n"; | |
2016 | } | |
2017 | } | |
6d9246e7 DM |
2018 | |
2019 | return $rule; | |
51bae274 DM |
2020 | } |
2021 | ||
2d404ffc | 2022 | sub parse_vmfw_option { |
85c6eaed DM |
2023 | my ($line) = @_; |
2024 | ||
2025 | my ($opt, $value); | |
2026 | ||
178a63be DM |
2027 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; |
2028 | ||
6bafd113 | 2029 | if ($line =~ m/^(enable|dhcp|macfilter|ips):\s*(0|1)\s*$/i) { |
9c6b6efd DM |
2030 | $opt = lc($1); |
2031 | $value = int($2); | |
178a63be DM |
2032 | } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) { |
2033 | $opt = lc($1); | |
2034 | $value = $2 ? lc($3) : ''; | |
72f63fde | 2035 | } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { |
85c6eaed DM |
2036 | $opt = lc($1); |
2037 | $value = uc($3); | |
b47ecc88 AD |
2038 | } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) { |
2039 | $opt = lc($1); | |
2040 | $value = $2; | |
9c6b6efd | 2041 | } else { |
85c6eaed DM |
2042 | chomp $line; |
2043 | die "can't parse option '$line'\n" | |
2044 | } | |
2045 | ||
2046 | return ($opt, $value); | |
2047 | } | |
2048 | ||
2d404ffc DM |
2049 | sub parse_hostfw_option { |
2050 | my ($line) = @_; | |
2051 | ||
2052 | my ($opt, $value); | |
2053 | ||
2054 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
2055 | ||
c50a5a68 | 2056 | if ($line =~ m/^(enable|nosmurfs|tcpflags):\s*(0|1)\s*$/i) { |
2d404ffc DM |
2057 | $opt = lc($1); |
2058 | $value = int($2); | |
178a63be | 2059 | } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) { |
2d404ffc DM |
2060 | $opt = lc($1); |
2061 | $value = $2 ? lc($3) : ''; | |
28c082a1 | 2062 | } elsif ($line =~ m/^(nf_conntrack_max|nf_conntrack_tcp_timeout_established):\s*(\d+)\s*$/i) { |
490cdead DM |
2063 | $opt = lc($1); |
2064 | $value = int($2); | |
2d404ffc DM |
2065 | } else { |
2066 | chomp $line; | |
2067 | die "can't parse option '$line'\n" | |
2068 | } | |
2069 | ||
2070 | return ($opt, $value); | |
2071 | } | |
2072 | ||
c6f5cc88 DM |
2073 | sub parse_clusterfw_option { |
2074 | my ($line) = @_; | |
2075 | ||
2076 | my ($opt, $value); | |
2077 | ||
2078 | if ($line =~ m/^(enable):\s*(0|1)\s*$/i) { | |
2079 | $opt = lc($1); | |
2080 | $value = int($2); | |
63324b09 DM |
2081 | } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { |
2082 | $opt = lc($1); | |
2083 | $value = uc($3); | |
c6f5cc88 DM |
2084 | } else { |
2085 | chomp $line; | |
2086 | die "can't parse option '$line'\n" | |
2087 | } | |
2088 | ||
2089 | return ($opt, $value); | |
2090 | } | |
2091 | ||
e76a9f53 | 2092 | sub parse_alias { |
92e1209b AD |
2093 | my ($line) = @_; |
2094 | ||
81d574a7 DM |
2095 | # we can add single line comments to the end of the line |
2096 | my $comment = decode('utf8', $1) if $line =~ s/\s*#\s*(.*?)\s*$//; | |
2097 | ||
92e1209b | 2098 | if ($line =~ m/^(\S+)\s(\S+)$/) { |
81d574a7 DM |
2099 | my ($name, $cidr) = ($1, $2); |
2100 | $cidr =~ s|/32$||; | |
2101 | pve_verify_ipv4_or_cidr($cidr); | |
2102 | my $data = { | |
2103 | name => $name, | |
2104 | cidr => $cidr, | |
2105 | }; | |
2106 | $data->{comment} = $comment if $comment; | |
2107 | return $data; | |
92e1209b AD |
2108 | } |
2109 | ||
81d574a7 | 2110 | return undef; |
92e1209b AD |
2111 | } |
2112 | ||
1210ae94 DM |
2113 | sub generic_fw_rules_parser { |
2114 | my ($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env) = @_; | |
5e1267a5 | 2115 | |
51bae274 DM |
2116 | my $section; |
2117 | my $group; | |
2118 | ||
1210ae94 | 2119 | my $res = $empty_conf; |
6158271d | 2120 | |
51bae274 DM |
2121 | while (defined(my $line = <$fh>)) { |
2122 | next if $line =~ m/^#/; | |
2123 | next if $line =~ m/^\s*$/; | |
2124 | ||
961b4928 DM |
2125 | my $linenr = $fh->input_line_number(); |
2126 | my $prefix = "$filename (line $linenr)"; | |
2127 | ||
1210ae94 | 2128 | if ($empty_conf->{options} && ($line =~ m/^\[options\]$/i)) { |
c6f5cc88 DM |
2129 | $section = 'options'; |
2130 | next; | |
2131 | } | |
2132 | ||
1210ae94 | 2133 | if ($empty_conf->{aliases} && ($line =~ m/^\[aliases\]$/i)) { |
92e1209b AD |
2134 | $section = 'aliases'; |
2135 | next; | |
2136 | } | |
2137 | ||
1210ae94 | 2138 | if ($empty_conf->{groups} && ($line =~ m/^\[group\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) { |
c6f5cc88 | 2139 | $section = 'groups'; |
92e976b3 | 2140 | $group = lc($1); |
0d22acb3 | 2141 | my $comment = $2; |
c85c87f9 | 2142 | $res->{$section}->{$group} = []; |
649e4d57 DM |
2143 | $res->{group_comments}->{$group} = decode('utf8', $comment) |
2144 | if $comment; | |
51bae274 DM |
2145 | next; |
2146 | } | |
34cdedfa | 2147 | |
1210ae94 | 2148 | if ($empty_conf->{rules} && ($line =~ m/^\[rules\]$/i)) { |
c6f5cc88 DM |
2149 | $section = 'rules'; |
2150 | next; | |
2151 | } | |
cbb5d6f3 | 2152 | |
1210ae94 | 2153 | if ($empty_conf->{ipset} && ($line =~ m/^\[ipset\s+(\S+)\]\s*(?:#\s*(.*?)\s*)?$/i)) { |
34cdedfa AD |
2154 | $section = 'ipset'; |
2155 | $group = lc($1); | |
d72c631c | 2156 | my $comment = $2; |
c85c87f9 | 2157 | $res->{$section}->{$group} = []; |
e34d0e58 | 2158 | $res->{ipset_comments}->{$group} = decode('utf8', $comment) |
649e4d57 | 2159 | if $comment; |
34cdedfa AD |
2160 | next; |
2161 | } | |
2162 | ||
c6f5cc88 | 2163 | if (!$section) { |
cbb5d6f3 | 2164 | warn "$prefix: skip line - no section\n"; |
51bae274 DM |
2165 | next; |
2166 | } | |
2167 | ||
c6f5cc88 DM |
2168 | if ($section eq 'options') { |
2169 | eval { | |
1210ae94 DM |
2170 | my ($opt, $value); |
2171 | if ($rule_env eq 'cluster') { | |
2172 | ($opt, $value) = parse_clusterfw_option($line); | |
2173 | } elsif ($rule_env eq 'host') { | |
2174 | ($opt, $value) = parse_hostfw_option($line); | |
2175 | } else { | |
2176 | ($opt, $value) = parse_vmfw_option($line); | |
2177 | } | |
c6f5cc88 DM |
2178 | $res->{options}->{$opt} = $value; |
2179 | }; | |
2180 | warn "$prefix: $@" if $@; | |
92e1209b AD |
2181 | } elsif ($section eq 'aliases') { |
2182 | eval { | |
e76a9f53 | 2183 | my $data = parse_alias($line); |
81d574a7 | 2184 | $res->{aliases}->{lc($data->{name})} = $data; |
92e1209b AD |
2185 | }; |
2186 | warn "$prefix: $@" if $@; | |
c6f5cc88 DM |
2187 | } elsif ($section eq 'rules') { |
2188 | my $rule; | |
1210ae94 | 2189 | eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, $res, $rule_env, $verbose); }; |
c6f5cc88 DM |
2190 | if (my $err = $@) { |
2191 | warn "$prefix: $err"; | |
2192 | next; | |
2193 | } | |
2194 | push @{$res->{$section}}, $rule; | |
2195 | } elsif ($section eq 'groups') { | |
34cdedfa | 2196 | my $rule; |
1210ae94 | 2197 | eval { $rule = parse_fw_rule($prefix, $line, $cluster_conf, undef, 'group', $verbose); }; |
34cdedfa AD |
2198 | if (my $err = $@) { |
2199 | warn "$prefix: $err"; | |
2200 | next; | |
2201 | } | |
3f95d14a | 2202 | push @{$res->{$section}->{$group}}, $rule; |
3f95d14a | 2203 | } elsif ($section eq 'ipset') { |
9d6f90e6 DM |
2204 | # we can add single line comments to the end of the rule |
2205 | my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//; | |
2206 | ||
30f1b100 | 2207 | $line =~ m/^(\!)?\s*(\S+)\s*$/; |
2a052ee3 | 2208 | my $nomatch = $1; |
9d6f90e6 | 2209 | my $cidr = $2; |
2a052ee3 | 2210 | |
44be8ceb | 2211 | if($cidr !~ m/^${ip_alias_pattern}$/) { |
92e1209b | 2212 | $cidr =~ s|/32$||; |
e34d0e58 | 2213 | |
92e1209b AD |
2214 | eval { pve_verify_ipv4_or_cidr($cidr); }; |
2215 | if (my $err = $@) { | |
2216 | warn "$prefix: $cidr - $err"; | |
2217 | next; | |
2218 | } | |
2a052ee3 | 2219 | } |
2a052ee3 | 2220 | |
e34d0e58 | 2221 | my $entry = { cidr => $cidr }; |
9d6f90e6 DM |
2222 | $entry->{nomatch} = 1 if $nomatch; |
2223 | $entry->{comment} = $comment if $comment; | |
e34d0e58 | 2224 | |
9d6f90e6 | 2225 | push @{$res->{$section}->{$group}}, $entry; |
1210ae94 DM |
2226 | } else { |
2227 | warn "$prefix: skip line - unknown section\n"; | |
2228 | next; | |
51bae274 | 2229 | } |
5e1267a5 DM |
2230 | } |
2231 | ||
2232 | return $res; | |
2233 | } | |
2234 | ||
1210ae94 DM |
2235 | sub parse_host_fw_rules { |
2236 | my ($filename, $fh, $cluster_conf, $verbose) = @_; | |
2237 | ||
2238 | my $empty_conf = { rules => [], options => {}}; | |
2239 | ||
2240 | return generic_fw_rules_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, 'host'); | |
2241 | } | |
2242 | ||
2243 | sub parse_vm_fw_rules { | |
2244 | my ($filename, $fh, $cluster_conf, $rule_env, $verbose) = @_; | |
2245 | ||
2246 | my $empty_conf = { | |
2247 | rules => [], | |
2248 | options => {}, | |
2249 | aliases => {}, | |
2250 | ipset => {} , | |
2251 | ipset_comments => {}, | |
2252 | }; | |
2253 | ||
2254 | return generic_fw_rules_parser($filename, $fh, $verbose, $cluster_conf, $empty_conf, $rule_env); | |
2255 | } | |
2256 | ||
2257 | sub parse_cluster_fw_rules { | |
2258 | my ($filename, $fh, $verbose) = @_; | |
2259 | ||
2260 | my $section; | |
2261 | my $group; | |
2262 | ||
2263 | my $empty_conf = { | |
2264 | rules => [], | |
2265 | options => {}, | |
2266 | aliases => {}, | |
2267 | groups => {}, | |
2268 | group_comments => {}, | |
2269 | ipset => {} , | |
2270 | ipset_comments => {}, | |
2271 | }; | |
2272 | ||
2273 | return generic_fw_rules_parser($filename, $fh, $verbose, $empty_conf, $empty_conf, 'cluster'); | |
2274 | } | |
2275 | ||
06320eb0 DM |
2276 | sub run_locked { |
2277 | my ($code, @param) = @_; | |
2278 | ||
2279 | my $timeout = 10; | |
2280 | ||
2281 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
2282 | ||
2283 | die $@ if $@; | |
2284 | ||
2285 | return $res; | |
2286 | } | |
2287 | ||
5e1267a5 DM |
2288 | sub read_local_vm_config { |
2289 | ||
2290 | my $openvz = {}; | |
5e1267a5 DM |
2291 | my $qemu = {}; |
2292 | ||
c8301d63 | 2293 | my $vmdata = { openvz => $openvz, qemu => $qemu }; |
5e1267a5 | 2294 | |
c8301d63 DM |
2295 | my $vmlist = PVE::Cluster::get_vmlist(); |
2296 | return $vmdata if !$vmlist || !$vmlist->{ids}; | |
2297 | my $ids = $vmlist->{ids}; | |
2298 | ||
2299 | foreach my $vmid (keys %$ids) { | |
2300 | next if !$vmid; # skip VE0 | |
2301 | my $d = $ids->{$vmid}; | |
2302 | next if !$d->{node} || $d->{node} ne $nodename; | |
2303 | next if !$d->{type}; | |
2304 | if ($d->{type} eq 'openvz') { | |
d9fee004 DM |
2305 | if ($have_pve_manager) { |
2306 | my $cfspath = PVE::OpenVZ::cfs_config_path($vmid); | |
2307 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
2308 | $openvz->{$vmid} = $conf; | |
2309 | } | |
c8301d63 DM |
2310 | } |
2311 | } elsif ($d->{type} eq 'qemu') { | |
d9fee004 DM |
2312 | if ($have_qemu_server) { |
2313 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); | |
2314 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
2315 | $qemu->{$vmid} = $conf; | |
2316 | } | |
c8301d63 | 2317 | } |
5e1267a5 DM |
2318 | } |
2319 | } | |
d9fee004 | 2320 | |
5e1267a5 DM |
2321 | return $vmdata; |
2322 | }; | |
2323 | ||
e7b35711 | 2324 | sub load_vmfw_conf { |
a523e057 | 2325 | my ($cluster_conf, $rule_env, $vmid, $dir, $verbose) = @_; |
e7b35711 DM |
2326 | |
2327 | my $vmfw_conf = {}; | |
2328 | ||
8aef5177 DM |
2329 | $dir = "/etc/pve/firewall" if !defined($dir); |
2330 | ||
2331 | my $filename = "$dir/$vmid.fw"; | |
e7b35711 | 2332 | if (my $fh = IO::File->new($filename, O_RDONLY)) { |
a523e057 | 2333 | $vmfw_conf = parse_vm_fw_rules($filename, $fh, $cluster_conf, $rule_env, $verbose); |
708ba714 | 2334 | $vmfw_conf->{vmid} = $vmid; |
e7b35711 DM |
2335 | } |
2336 | ||
2337 | return $vmfw_conf; | |
2338 | } | |
2339 | ||
464f933e | 2340 | my $format_rules = sub { |
dba740a9 | 2341 | my ($rules, $allow_iface) = @_; |
464f933e DM |
2342 | |
2343 | my $raw = ''; | |
2344 | ||
2345 | foreach my $rule (@$rules) { | |
c14dacdf | 2346 | if ($rule->{type} eq 'in' || $rule->{type} eq 'out' || $rule->{type} eq 'group') { |
464f933e DM |
2347 | $raw .= '|' if defined($rule->{enable}) && !$rule->{enable}; |
2348 | $raw .= uc($rule->{type}); | |
7ca36671 DM |
2349 | if ($rule->{macro}) { |
2350 | $raw .= " $rule->{macro}($rule->{action})"; | |
2351 | } else { | |
2352 | $raw .= " " . $rule->{action}; | |
2353 | } | |
dba740a9 DM |
2354 | if ($allow_iface && $rule->{iface}) { |
2355 | $raw .= " -i $rule->{iface}"; | |
2356 | } | |
c14dacdf DM |
2357 | |
2358 | if ($rule->{type} ne 'group') { | |
dba740a9 DM |
2359 | $raw .= " -source $rule->{source}" if $rule->{source}; |
2360 | $raw .= " -dest $rule->{dest}" if $rule->{dest}; | |
2361 | $raw .= " -p $rule->{proto}" if $rule->{proto}; | |
2362 | $raw .= " -dport $rule->{dport}" if $rule->{dport}; | |
2363 | $raw .= " -sport $rule->{sport}" if $rule->{sport}; | |
c14dacdf DM |
2364 | } |
2365 | ||
cbb5d6f3 | 2366 | $raw .= " # " . encode('utf8', $rule->{comment}) |
464f933e DM |
2367 | if $rule->{comment} && $rule->{comment} !~ m/^\s*$/; |
2368 | $raw .= "\n"; | |
2369 | } else { | |
c14dacdf | 2370 | die "unknown rule type '$rule->{type}'"; |
464f933e DM |
2371 | } |
2372 | } | |
2373 | ||
2374 | return $raw; | |
2375 | }; | |
2376 | ||
2377 | my $format_options = sub { | |
68c90e21 DM |
2378 | my ($options) = @_; |
2379 | ||
2380 | my $raw = ''; | |
464f933e DM |
2381 | |
2382 | $raw .= "[OPTIONS]\n\n"; | |
2383 | foreach my $opt (keys %$options) { | |
2384 | $raw .= "$opt: $options->{$opt}\n"; | |
2385 | } | |
2386 | $raw .= "\n"; | |
68c90e21 DM |
2387 | |
2388 | return $raw; | |
464f933e DM |
2389 | }; |
2390 | ||
0d5f0a0f DM |
2391 | my $format_aliases = sub { |
2392 | my ($aliases) = @_; | |
2393 | ||
2394 | my $raw = ''; | |
2395 | ||
2396 | $raw .= "[ALIASES]\n\n"; | |
2397 | foreach my $k (keys %$aliases) { | |
81d574a7 DM |
2398 | my $e = $aliases->{$k}; |
2399 | $raw .= "$e->{name} $e->{cidr}"; | |
2400 | $raw .= " # " . encode('utf8', $e->{comment}) | |
2401 | if $e->{comment} && $e->{comment} !~ m/^\s*$/; | |
2402 | $raw .= "\n"; | |
0d5f0a0f DM |
2403 | } |
2404 | $raw .= "\n"; | |
2405 | ||
2406 | return $raw; | |
2407 | }; | |
2408 | ||
1210ae94 DM |
2409 | my $format_ipsets = sub { |
2410 | my ($fw_conf) = @_; | |
2411 | ||
9d6f90e6 DM |
2412 | my $raw = ''; |
2413 | ||
1210ae94 DM |
2414 | foreach my $ipset (sort keys %{$fw_conf->{ipset}}) { |
2415 | if (my $comment = $fw_conf->{ipset_comments}->{$ipset}) { | |
2416 | my $utf8comment = encode('utf8', $comment); | |
2417 | $raw .= "[IPSET $ipset] # $utf8comment\n\n"; | |
2418 | } else { | |
2419 | $raw .= "[IPSET $ipset]\n\n"; | |
2420 | } | |
2421 | my $options = $fw_conf->{ipset}->{$ipset}; | |
6e299ae3 | 2422 | |
1210ae94 DM |
2423 | my $nethash = {}; |
2424 | foreach my $entry (@$options) { | |
2425 | $nethash->{$entry->{cidr}} = $entry; | |
2426 | } | |
2427 | ||
2428 | foreach my $cidr (sort keys %$nethash) { | |
2429 | my $entry = $nethash->{$cidr}; | |
2430 | my $line = $entry->{nomatch} ? '!' : ''; | |
2431 | $line .= $entry->{cidr}; | |
2432 | $line .= " # " . encode('utf8', $entry->{comment}) | |
2433 | if $entry->{comment} && $entry->{comment} !~ m/^\s*$/; | |
2434 | $raw .= "$line\n"; | |
2435 | } | |
2436 | ||
2437 | $raw .= "\n"; | |
9d6f90e6 | 2438 | } |
e34d0e58 | 2439 | |
9d6f90e6 DM |
2440 | return $raw; |
2441 | }; | |
2442 | ||
464f933e DM |
2443 | sub save_vmfw_conf { |
2444 | my ($vmid, $vmfw_conf) = @_; | |
2445 | ||
2446 | my $raw = ''; | |
2447 | ||
2448 | my $options = $vmfw_conf->{options}; | |
68c90e21 | 2449 | $raw .= &$format_options($options) if scalar(keys %$options); |
cbb5d6f3 | 2450 | |
e76a9f53 DM |
2451 | my $aliases = $vmfw_conf->{aliases}; |
2452 | $raw .= &$format_aliases($aliases) if scalar(keys %$aliases); | |
2453 | ||
1210ae94 DM |
2454 | $raw .= &$format_ipsets($vmfw_conf); |
2455 | ||
8824b2f0 | 2456 | my $rules = $vmfw_conf->{rules} || []; |
464f933e DM |
2457 | if (scalar(@$rules)) { |
2458 | $raw .= "[RULES]\n\n"; | |
2459 | $raw .= &$format_rules($rules, 1); | |
2460 | $raw .= "\n"; | |
2461 | } | |
2462 | ||
2463 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
2464 | PVE::Tools::file_set_contents($filename, $raw); | |
2465 | } | |
2466 | ||
6fc63ffd | 2467 | sub read_vm_firewall_configs { |
a523e057 | 2468 | my ($cluster_conf, $vmdata, $dir, $verbose) = @_; |
8aef5177 | 2469 | |
6fc63ffd | 2470 | my $vmfw_configs = {}; |
c8301d63 | 2471 | |
b6b8e6ad | 2472 | foreach my $vmid (keys %{$vmdata->{qemu}}) { |
a523e057 | 2473 | my $vmfw_conf = load_vmfw_conf($cluster_conf, 'vm', $vmid, $dir, $verbose); |
b6b8e6ad DM |
2474 | next if !$vmfw_conf->{options}; # skip if file does not exists |
2475 | $vmfw_configs->{$vmid} = $vmfw_conf; | |
2476 | } | |
2477 | foreach my $vmid (keys %{$vmdata->{openvz}}) { | |
a523e057 | 2478 | my $vmfw_conf = load_vmfw_conf($cluster_conf, 'ct', $vmid, $dir, $verbose); |
e7b35711 DM |
2479 | next if !$vmfw_conf->{options}; # skip if file does not exists |
2480 | $vmfw_configs->{$vmid} = $vmfw_conf; | |
5e1267a5 DM |
2481 | } |
2482 | ||
6fc63ffd | 2483 | return $vmfw_configs; |
5e1267a5 DM |
2484 | } |
2485 | ||
2d404ffc DM |
2486 | sub get_option_log_level { |
2487 | my ($options, $k) = @_; | |
2488 | ||
2489 | my $v = $options->{$k}; | |
178a63be | 2490 | $v = $default_log_level if !defined($v); |
2d404ffc DM |
2491 | |
2492 | return undef if $v eq '' || $v eq 'nolog'; | |
2493 | ||
2494 | $v = $log_level_hash->{$v} if defined($log_level_hash->{$v}); | |
2495 | ||
2496 | return $v if ($v >= 0) && ($v <= 7); | |
2497 | ||
2498 | warn "unknown log level ($k = '$v')\n"; | |
2499 | ||
2500 | return undef; | |
2501 | } | |
2502 | ||
d8f2505e | 2503 | sub generate_std_chains { |
2d404ffc | 2504 | my ($ruleset, $options) = @_; |
cbb5d6f3 | 2505 | |
2d404ffc DM |
2506 | my $loglevel = get_option_log_level($options, 'smurf_log_level'); |
2507 | ||
2508 | # same as shorewall smurflog. | |
782c4cde | 2509 | my $chain = 'PVEFW-smurflog'; |
12f3796e | 2510 | $pve_std_chains->{$chain} = []; |
782c4cde DM |
2511 | |
2512 | push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; | |
2513 | push @{$pve_std_chains->{$chain}}, "-j DROP"; | |
2d404ffc DM |
2514 | |
2515 | # same as shorewall logflags action. | |
2516 | $loglevel = get_option_log_level($options, 'tcp_flags_log_level'); | |
782c4cde | 2517 | $chain = 'PVEFW-logflags'; |
12f3796e DM |
2518 | $pve_std_chains->{$chain} = []; |
2519 | ||
782c4cde DM |
2520 | # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG) |
2521 | push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; | |
2522 | push @{$pve_std_chains->{$chain}}, "-j DROP"; | |
d8f2505e DM |
2523 | |
2524 | foreach my $chain (keys %$pve_std_chains) { | |
2525 | ruleset_create_chain($ruleset, $chain); | |
2526 | foreach my $rule (@{$pve_std_chains->{$chain}}) { | |
2527 | if (ref($rule)) { | |
2528 | ruleset_generate_rule($ruleset, $chain, $rule); | |
2529 | } else { | |
2530 | ruleset_addrule($ruleset, $chain, $rule); | |
2531 | } | |
2532 | } | |
2533 | } | |
2534 | } | |
2535 | ||
34cdedfa | 2536 | sub generate_ipset_chains { |
708ba714 | 2537 | my ($ipset_ruleset, $clusterfw_conf, $fw_conf) = @_; |
34cdedfa | 2538 | |
dd7a13fd | 2539 | foreach my $ipset (keys %{$fw_conf->{ipset}}) { |
708ba714 DM |
2540 | my $ipset_chain = compute_ipset_chain_name($fw_conf->{vmid}, $ipset); |
2541 | generate_ipset($ipset_ruleset, $ipset_chain, $fw_conf->{ipset}->{$ipset}, $clusterfw_conf, $fw_conf); | |
34cdedfa AD |
2542 | } |
2543 | } | |
2544 | ||
2545 | sub generate_ipset { | |
708ba714 DM |
2546 | my ($ipset_ruleset, $name, $options, $clusterfw_conf, $fw_conf) = @_; |
2547 | ||
2548 | die "duplicate ipset chain '$name'\n" if defined($ipset_ruleset->{$name}); | |
34cdedfa | 2549 | |
dd7a13fd DM |
2550 | my $hashsize = scalar(@$options); |
2551 | if ($hashsize <= 64) { | |
2a052ee3 | 2552 | $hashsize = 64; |
dd7a13fd | 2553 | } else { |
2a052ee3 AD |
2554 | $hashsize = round_powerof2($hashsize); |
2555 | } | |
2556 | ||
708ba714 | 2557 | $ipset_ruleset->{$name} = ["create $name hash:net family inet hashsize $hashsize maxelem $hashsize"]; |
34cdedfa | 2558 | |
30f1b100 DM |
2559 | # remove duplicates |
2560 | my $nethash = {}; | |
9d6f90e6 | 2561 | foreach my $entry (@$options) { |
92e1209b | 2562 | my $cidr = $entry->{cidr}; |
d4091b82 | 2563 | if ($cidr =~ m/^${ip_alias_pattern}$/) { |
81d574a7 | 2564 | my $alias = lc($cidr); |
708ba714 DM |
2565 | my $e = $fw_conf->{aliases}->{$alias} if $fw_conf; |
2566 | $e = $clusterfw_conf->{aliases}->{$alias} if !$e && $clusterfw_conf; | |
2567 | if ($e) { | |
2568 | $entry->{cidr} = $e->{cidr}; | |
7dd13259 | 2569 | $nethash->{$entry->{cidr}} = $entry; |
d4091b82 | 2570 | } else { |
708ba714 | 2571 | warn "no such alias '$cidr'\n"; |
d4091b82 | 2572 | } |
7dd13259 DM |
2573 | } else { |
2574 | $nethash->{$entry->{cidr}} = $entry; | |
92e1209b | 2575 | } |
30f1b100 DM |
2576 | } |
2577 | ||
2578 | foreach my $cidr (sort keys %$nethash) { | |
2579 | my $entry = $nethash->{$cidr}; | |
2580 | ||
9d6f90e6 DM |
2581 | my $cmd = "add $name $cidr"; |
2582 | if ($entry->{nomatch}) { | |
2583 | if ($feature_ipset_nomatch) { | |
2584 | push @{$ipset_ruleset->{$name}}, "$cmd nomatch"; | |
2585 | } else { | |
2586 | warn "ignore !$cidr - nomatch not supported by kernel\n"; | |
2587 | } | |
2588 | } else { | |
2589 | push @{$ipset_ruleset->{$name}}, $cmd; | |
2590 | } | |
34cdedfa | 2591 | } |
2a052ee3 AD |
2592 | } |
2593 | ||
2594 | sub round_powerof2 { | |
dd7a13fd | 2595 | my ($int) = @_; |
2a052ee3 | 2596 | |
dd7a13fd DM |
2597 | $int--; |
2598 | $int |= $int >> $_ foreach (1,2,4,8,16); | |
2599 | return ++$int; | |
34cdedfa AD |
2600 | } |
2601 | ||
fca39c2c | 2602 | sub load_clusterfw_conf { |
d4cda423 | 2603 | my ($filename, $verbose) = @_; |
8aef5177 DM |
2604 | |
2605 | $filename = $clusterfw_conf_filename if !defined($filename); | |
530c005e | 2606 | |
fca39c2c | 2607 | my $cluster_conf = {}; |
8aef5177 | 2608 | if (my $fh = IO::File->new($filename, O_RDONLY)) { |
d4cda423 | 2609 | $cluster_conf = parse_cluster_fw_rules($filename, $fh, $verbose); |
51bae274 | 2610 | } |
5e1267a5 | 2611 | |
fca39c2c | 2612 | return $cluster_conf; |
e5d76bde DM |
2613 | } |
2614 | ||
fca39c2c DM |
2615 | sub save_clusterfw_conf { |
2616 | my ($cluster_conf) = @_; | |
9c7e0858 DM |
2617 | |
2618 | my $raw = ''; | |
9c7e0858 | 2619 | |
c6f5cc88 | 2620 | my $options = $cluster_conf->{options}; |
68c90e21 | 2621 | $raw .= &$format_options($options) if scalar(keys %$options); |
9c7e0858 | 2622 | |
0d5f0a0f DM |
2623 | my $aliases = $cluster_conf->{aliases}; |
2624 | $raw .= &$format_aliases($aliases) if scalar(keys %$aliases); | |
e34d0e58 | 2625 | |
1210ae94 DM |
2626 | $raw .= &$format_ipsets($cluster_conf); |
2627 | ||
c6f5cc88 DM |
2628 | my $rules = $cluster_conf->{rules}; |
2629 | if (scalar(@$rules)) { | |
2630 | $raw .= "[RULES]\n\n"; | |
63c91681 | 2631 | $raw .= &$format_rules($rules, 1); |
c6f5cc88 DM |
2632 | $raw .= "\n"; |
2633 | } | |
2634 | ||
2635 | foreach my $group (sort keys %{$cluster_conf->{groups}}) { | |
2636 | my $rules = $cluster_conf->{groups}->{$group}; | |
0d22acb3 | 2637 | if (my $comment = $cluster_conf->{group_comments}->{$group}) { |
649e4d57 DM |
2638 | my $utf8comment = encode('utf8', $comment); |
2639 | $raw .= "[group $group] # $utf8comment\n\n"; | |
0d22acb3 DM |
2640 | } else { |
2641 | $raw .= "[group $group]\n\n"; | |
2642 | } | |
2643 | ||
63c91681 | 2644 | $raw .= &$format_rules($rules, 0); |
9c7e0858 DM |
2645 | $raw .= "\n"; |
2646 | } | |
2647 | ||
fca39c2c | 2648 | PVE::Tools::file_set_contents($clusterfw_conf_filename, $raw); |
9c7e0858 DM |
2649 | } |
2650 | ||
8b27beb9 | 2651 | sub load_hostfw_conf { |
a523e057 | 2652 | my ($cluster_conf, $filename, $verbose) = @_; |
8aef5177 DM |
2653 | |
2654 | $filename = $hostfw_conf_filename if !defined($filename); | |
8b27beb9 DM |
2655 | |
2656 | my $hostfw_conf = {}; | |
8aef5177 | 2657 | if (my $fh = IO::File->new($filename, O_RDONLY)) { |
a523e057 | 2658 | $hostfw_conf = parse_host_fw_rules($filename, $fh, $cluster_conf, $verbose); |
8b27beb9 DM |
2659 | } |
2660 | return $hostfw_conf; | |
2661 | } | |
2662 | ||
63c91681 DM |
2663 | sub save_hostfw_conf { |
2664 | my ($hostfw_conf) = @_; | |
2665 | ||
2666 | my $raw = ''; | |
2667 | ||
2668 | my $options = $hostfw_conf->{options}; | |
68c90e21 | 2669 | $raw .= &$format_options($options) if scalar(keys %$options); |
cbb5d6f3 | 2670 | |
63c91681 DM |
2671 | my $rules = $hostfw_conf->{rules}; |
2672 | if (scalar(@$rules)) { | |
2673 | $raw .= "[RULES]\n\n"; | |
2674 | $raw .= &$format_rules($rules, 1); | |
2675 | $raw .= "\n"; | |
2676 | } | |
2677 | ||
2678 | PVE::Tools::file_set_contents($hostfw_conf_filename, $raw); | |
2679 | } | |
2680 | ||
e5d76bde | 2681 | sub compile { |
d4cda423 | 2682 | my ($cluster_conf, $hostfw_conf, $vmdata, $verbose) = @_; |
3dfa8a7f | 2683 | |
8aef5177 DM |
2684 | my $vmfw_configs; |
2685 | ||
2686 | if ($vmdata) { # test mode | |
2687 | my $testdir = $vmdata->{testdir} || die "no test directory specified"; | |
2688 | my $filename = "$testdir/cluster.fw"; | |
d4cda423 | 2689 | $cluster_conf = load_clusterfw_conf($filename, $verbose); |
8aef5177 DM |
2690 | |
2691 | $filename = "$testdir/host.fw"; | |
a523e057 | 2692 | $hostfw_conf = load_hostfw_conf($cluster_conf, $filename, $verbose); |
8aef5177 | 2693 | |
a523e057 | 2694 | $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, $testdir, $verbose); |
8aef5177 | 2695 | } else { # normal operation |
d4cda423 | 2696 | $cluster_conf = load_clusterfw_conf(undef, $verbose) if !$cluster_conf; |
8aef5177 | 2697 | |
a523e057 | 2698 | $hostfw_conf = load_hostfw_conf($cluster_conf, undef, $verbose) if !$hostfw_conf; |
8aef5177 DM |
2699 | |
2700 | $vmdata = read_local_vm_config(); | |
a523e057 | 2701 | $vmfw_configs = read_vm_firewall_configs($cluster_conf, $vmdata, undef, $verbose); |
8aef5177 | 2702 | } |
3dfa8a7f | 2703 | |
27083984 | 2704 | $cluster_conf->{ipset}->{venet0} = []; |
708ba714 | 2705 | my $venet0_ipset_chain = compute_ipset_chain_name(0, 'venet0'); |
bfc488f6 | 2706 | |
525778d7 DM |
2707 | my $localnet; |
2708 | if ($cluster_conf->{aliases}->{local_network}) { | |
2709 | $localnet = $cluster_conf->{aliases}->{local_network}->{cidr}; | |
2710 | } else { | |
2711 | $localnet = local_network() || '127.0.0.0/8'; | |
2712 | $cluster_conf->{aliases}->{local_network} = { cidr => $localnet }; | |
2713 | } | |
2714 | ||
2715 | push @{$cluster_conf->{ipset}->{management}}, { cidr => $localnet }; | |
bfc488f6 | 2716 | |
3fa83edf DM |
2717 | my $ruleset = {}; |
2718 | ||
dec84fcd DM |
2719 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); |
2720 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
5b1df9a0 | 2721 | |
fadb13dd | 2722 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); |
e34d0e58 | 2723 | |
8b27beb9 | 2724 | my $hostfw_options = $hostfw_conf->{options} || {}; |
fadb13dd | 2725 | |
88733a74 AD |
2726 | # fixme: what log level should we use here? |
2727 | my $loglevel = get_option_log_level($hostfw_options, "log_level_out"); | |
2728 | ||
097820b0 | 2729 | ruleset_chain_add_conn_filters($ruleset, "PVEFW-FORWARD", "ACCEPT"); |
88733a74 | 2730 | |
708ba714 | 2731 | |
e2943485 | 2732 | ruleset_create_chain($ruleset, "PVEFW-VENET-OUT"); |
708ba714 DM |
2733 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT"); |
2734 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i venet0 -m set --match-set ${venet0_ipset_chain} src -j PVEFW-VENET-OUT"); | |
e2943485 DM |
2735 | |
2736 | ruleset_create_chain($ruleset, "PVEFW-FWBR-IN"); | |
2428e394 | 2737 | ruleset_chain_add_input_filters($ruleset, "PVEFW-FWBR-IN", $hostfw_options, $cluster_conf, $loglevel); |
e2943485 | 2738 | |
d1b41c08 | 2739 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-in fwln+ -j PVEFW-FWBR-IN"); |
e2943485 DM |
2740 | |
2741 | ruleset_create_chain($ruleset, "PVEFW-FWBR-OUT"); | |
d1b41c08 | 2742 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m physdev --physdev-is-bridged --physdev-out fwln+ -j PVEFW-FWBR-OUT"); |
e2943485 DM |
2743 | |
2744 | ruleset_create_chain($ruleset, "PVEFW-VENET-IN"); | |
2428e394 | 2745 | ruleset_chain_add_input_filters($ruleset, "PVEFW-VENET-IN", $hostfw_options, $cluster_conf, $loglevel); |
e2943485 | 2746 | |
708ba714 | 2747 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN"); |
e2943485 | 2748 | |
92e976b3 | 2749 | generate_std_chains($ruleset, $hostfw_options); |
fadb13dd | 2750 | |
6d2ab017 | 2751 | my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0)); |
2d404ffc | 2752 | |
708ba714 DM |
2753 | my $ipset_ruleset = {}; |
2754 | ||
1a9978ed DM |
2755 | if ($hostfw_enable) { |
2756 | eval { enable_host_firewall($ruleset, $hostfw_conf, $cluster_conf); }; | |
2757 | warn $@ if $@; # just to be sure - should not happen | |
2758 | } | |
3fa83edf | 2759 | |
708ba714 | 2760 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-o venet0 -m set --match-set ${venet0_ipset_chain} dst -j PVEFW-VENET-IN"); |
62689a1e | 2761 | |
6158271d | 2762 | # generate firewall rules for QEMU VMs |
3fa83edf | 2763 | foreach my $vmid (keys %{$vmdata->{qemu}}) { |
1a9978ed DM |
2764 | eval { |
2765 | my $conf = $vmdata->{qemu}->{$vmid}; | |
2766 | my $vmfw_conf = $vmfw_configs->{$vmid}; | |
2767 | return if !$vmfw_conf; | |
2768 | return if !$vmfw_conf->{options}->{enable}; | |
2769 | ||
708ba714 DM |
2770 | generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf); |
2771 | ||
1a9978ed DM |
2772 | foreach my $netid (keys %$conf) { |
2773 | next if $netid !~ m/^net(\d+)$/; | |
2774 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
2775 | next if !$net->{firewall}; | |
2776 | my $iface = "tap${vmid}i$1"; | |
2777 | ||
2778 | my $macaddr = $net->{macaddr}; | |
2779 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
2780 | $vmfw_conf, $vmid, 'IN'); | |
2781 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
2782 | $vmfw_conf, $vmid, 'OUT'); | |
2783 | } | |
2784 | }; | |
2785 | warn $@ if $@; # just to be sure - should not happen | |
3fa83edf | 2786 | } |
c8301d63 DM |
2787 | |
2788 | # generate firewall rules for OpenVZ containers | |
2789 | foreach my $vmid (keys %{$vmdata->{openvz}}) { | |
1a9978ed DM |
2790 | eval { |
2791 | my $conf = $vmdata->{openvz}->{$vmid}; | |
c8301d63 | 2792 | |
1a9978ed DM |
2793 | my $vmfw_conf = $vmfw_configs->{$vmid}; |
2794 | return if !$vmfw_conf; | |
2795 | return if !$vmfw_conf->{options}->{enable}; | |
c8301d63 | 2796 | |
708ba714 DM |
2797 | generate_ipset_chains($ipset_ruleset, $cluster_conf, $vmfw_conf); |
2798 | ||
1a9978ed DM |
2799 | if ($conf->{ip_address} && $conf->{ip_address}->{value}) { |
2800 | my $ip = $conf->{ip_address}->{value}; | |
2801 | $ip =~ s/\s+/,/g; | |
2802 | parse_address_list($ip); # make sure we have a valid $ip list | |
27083984 | 2803 | |
1a9978ed | 2804 | my @ips = split(',', $ip); |
27083984 | 2805 | |
1a9978ed DM |
2806 | foreach my $singleip (@ips) { |
2807 | my $venet0ipset = {}; | |
2808 | $venet0ipset->{cidr} = $singleip; | |
2809 | push @{$cluster_conf->{ipset}->{venet0}}, $venet0ipset; | |
2810 | } | |
c8301d63 | 2811 | |
1a9978ed DM |
2812 | generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'IN'); |
2813 | generate_venet_rules_direction($ruleset, $cluster_conf, $vmfw_conf, $vmid, $ip, 'OUT'); | |
2814 | } | |
530c005e | 2815 | |
1a9978ed DM |
2816 | if ($conf->{netif} && $conf->{netif}->{value}) { |
2817 | my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value}); | |
2818 | foreach my $netid (keys %$netif) { | |
2819 | my $d = $netif->{$netid}; | |
2820 | ||
2821 | my $macaddr = $d->{mac}; | |
2822 | my $iface = $d->{host_ifname}; | |
2823 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
2824 | $vmfw_conf, $vmid, 'IN'); | |
2825 | generate_tap_rules_direction($ruleset, $cluster_conf, $iface, $netid, $macaddr, | |
2826 | $vmfw_conf, $vmid, 'OUT'); | |
2827 | } | |
c8301d63 | 2828 | } |
1a9978ed DM |
2829 | }; |
2830 | warn $@ if $@; # just to be sure - should not happen | |
c8301d63 | 2831 | } |
c0413e35 | 2832 | |
097820b0 AD |
2833 | if(ruleset_chain_exist($ruleset, "PVEFW-IPS")){ |
2834 | ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j PVEFW-IPS"); | |
2835 | } | |
2836 | ||
708ba714 | 2837 | generate_ipset_chains($ipset_ruleset, undef, $cluster_conf); |
27083984 | 2838 | |
3dfa8a7f | 2839 | return ($ruleset, $ipset_ruleset); |
3fa83edf DM |
2840 | } |
2841 | ||
2842 | sub get_ruleset_status { | |
4bd0a9c4 | 2843 | my ($ruleset, $active_chains, $digest_fn, $verbose) = @_; |
3fa83edf DM |
2844 | |
2845 | my $statushash = {}; | |
2846 | ||
2847 | foreach my $chain (sort keys %$ruleset) { | |
4bd0a9c4 | 2848 | my $sig = &$digest_fn($ruleset->{$chain}); |
9bf7d929 | 2849 | |
3fa83edf DM |
2850 | $statushash->{$chain}->{sig} = $sig; |
2851 | ||
2852 | my $oldsig = $active_chains->{$chain}; | |
2853 | if (!defined($oldsig)) { | |
2854 | $statushash->{$chain}->{action} = 'create'; | |
2855 | } else { | |
2856 | if ($oldsig eq $sig) { | |
2857 | $statushash->{$chain}->{action} = 'exists'; | |
2858 | } else { | |
2859 | $statushash->{$chain}->{action} = 'update'; | |
2860 | } | |
2861 | } | |
2862 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
2863 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
2864 | print "\t$cmd\n" if $verbose; | |
2865 | } | |
2866 | } | |
2867 | ||
2868 | foreach my $chain (sort keys %$active_chains) { | |
2869 | if (!defined($ruleset->{$chain})) { | |
2870 | my $sig = $active_chains->{$chain}; | |
2871 | $statushash->{$chain}->{action} = 'delete'; | |
2872 | $statushash->{$chain}->{sig} = $sig; | |
2873 | print "delete $chain ($sig)\n" if $verbose; | |
2874 | } | |
6158271d | 2875 | } |
2a052ee3 | 2876 | |
3fa83edf DM |
2877 | return $statushash; |
2878 | } | |
2879 | ||
3fa83edf DM |
2880 | sub print_sig_rule { |
2881 | my ($chain, $sig) = @_; | |
2882 | ||
09d5f68e DM |
2883 | # We just use this to store a SHA1 checksum used to detect changes |
2884 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
b6360c3f DM |
2885 | } |
2886 | ||
df7cb349 | 2887 | sub get_ruleset_cmdlist { |
a84f4d96 | 2888 | my ($ruleset, $verbose) = @_; |
3fa83edf | 2889 | |
3fa83edf | 2890 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; |
cbb5d6f3 | 2891 | |
4bd0a9c4 DM |
2892 | my ($active_chains, $hooks) = iptables_get_chains(); |
2893 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose); | |
3fa83edf DM |
2894 | |
2895 | # create missing chains first | |
2896 | foreach my $chain (sort keys %$ruleset) { | |
2897 | my $stat = $statushash->{$chain}; | |
2898 | die "internal error" if !$stat; | |
2899 | next if $stat->{action} ne 'create'; | |
886aba9c | 2900 | |
3fa83edf DM |
2901 | $cmdlist .= ":$chain - [0:0]\n"; |
2902 | } | |
2903 | ||
4bd0a9c4 DM |
2904 | foreach my $h (qw(INPUT OUTPUT FORWARD)) { |
2905 | if (!$hooks->{$h}) { | |
2906 | $cmdlist .= "-A $h -j PVEFW-$h\n"; | |
2907 | } | |
3fa83edf DM |
2908 | } |
2909 | ||
2910 | foreach my $chain (sort keys %$ruleset) { | |
2911 | my $stat = $statushash->{$chain}; | |
2912 | die "internal error" if !$stat; | |
2913 | ||
2914 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
2915 | $cmdlist .= "-F $chain\n"; | |
2916 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
2917 | $cmdlist .= "$cmd\n"; | |
2918 | } | |
2919 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
2920 | } elsif ($stat->{action} eq 'delete') { | |
f5d28682 | 2921 | die "internal error"; # this should not happen |
3fa83edf DM |
2922 | } elsif ($stat->{action} eq 'exists') { |
2923 | # do nothing | |
2924 | } else { | |
2925 | die "internal error - unknown status '$stat->{action}'"; | |
2926 | } | |
2927 | } | |
2928 | ||
f5d28682 DM |
2929 | foreach my $chain (keys %$statushash) { |
2930 | next if $statushash->{$chain}->{action} ne 'delete'; | |
2931 | $cmdlist .= "-F $chain\n"; | |
2932 | } | |
2933 | foreach my $chain (keys %$statushash) { | |
2934 | next if $statushash->{$chain}->{action} ne 'delete'; | |
fadb13dd DM |
2935 | next if $chain eq 'PVEFW-INPUT'; |
2936 | next if $chain eq 'PVEFW-OUTPUT'; | |
2937 | next if $chain eq 'PVEFW-FORWARD'; | |
f5d28682 DM |
2938 | $cmdlist .= "-X $chain\n"; |
2939 | } | |
2940 | ||
3f95d14a | 2941 | my $changes = $cmdlist ne "*filter\n" ? 1 : 0; |
4bd0a9c4 | 2942 | |
3fa83edf DM |
2943 | $cmdlist .= "COMMIT\n"; |
2944 | ||
3f95d14a | 2945 | return wantarray ? ($cmdlist, $changes) : $cmdlist; |
6b9f68a2 DM |
2946 | } |
2947 | ||
34cdedfa | 2948 | sub get_ipset_cmdlist { |
dd7a13fd | 2949 | my ($ruleset, $verbose) = @_; |
34cdedfa AD |
2950 | |
2951 | my $cmdlist = ""; | |
2952 | ||
dd7a13fd DM |
2953 | my $delete_cmdlist = ""; |
2954 | ||
4bd0a9c4 DM |
2955 | my $active_chains = ipset_get_chains(); |
2956 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest, $verbose); | |
34cdedfa | 2957 | |
e34d0e58 | 2958 | # remove stale _swap chains |
30f1b100 DM |
2959 | foreach my $chain (keys %$active_chains) { |
2960 | if ($chain =~ m/^PVEFW-\S+_swap$/) { | |
2961 | $cmdlist .= "destroy $chain\n"; | |
2962 | } | |
2963 | } | |
2964 | ||
dd7a13fd DM |
2965 | foreach my $chain (sort keys %$ruleset) { |
2966 | my $stat = $statushash->{$chain}; | |
2967 | die "internal error" if !$stat; | |
2a052ee3 | 2968 | |
dd7a13fd DM |
2969 | if ($stat->{action} eq 'create') { |
2970 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
34cdedfa AD |
2971 | $cmdlist .= "$cmd\n"; |
2972 | } | |
dd7a13fd | 2973 | } |
34cdedfa | 2974 | |
dd7a13fd DM |
2975 | if ($stat->{action} eq 'update') { |
2976 | my $chain_swap = $chain."_swap"; | |
cbb5d6f3 | 2977 | |
dd7a13fd DM |
2978 | foreach my $cmd (@{$ruleset->{$chain}}) { |
2979 | $cmd =~ s/$chain/$chain_swap/; | |
2980 | $cmdlist .= "$cmd\n"; | |
34cdedfa | 2981 | } |
dd7a13fd DM |
2982 | $cmdlist .= "swap $chain_swap $chain\n"; |
2983 | $cmdlist .= "flush $chain_swap\n"; | |
2984 | $cmdlist .= "destroy $chain_swap\n"; | |
2a052ee3 | 2985 | } |
34cdedfa | 2986 | |
dd7a13fd | 2987 | } |
3f95d14a | 2988 | |
dd7a13fd DM |
2989 | foreach my $chain (keys %$statushash) { |
2990 | next if $statushash->{$chain}->{action} ne 'delete'; | |
2a052ee3 | 2991 | |
dd7a13fd DM |
2992 | $delete_cmdlist .= "flush $chain\n"; |
2993 | $delete_cmdlist .= "destroy $chain\n"; | |
34cdedfa AD |
2994 | } |
2995 | ||
dd7a13fd | 2996 | my $changes = ($cmdlist || $delete_cmdlist) ? 1 : 0; |
cbb5d6f3 | 2997 | |
dd7a13fd | 2998 | return ($cmdlist, $delete_cmdlist, $changes); |
34cdedfa AD |
2999 | } |
3000 | ||
6b9f68a2 | 3001 | sub apply_ruleset { |
34cdedfa | 3002 | my ($ruleset, $hostfw_conf, $ipset_ruleset, $verbose) = @_; |
6b9f68a2 DM |
3003 | |
3004 | enable_bridge_firewall(); | |
3005 | ||
cbb5d6f3 | 3006 | my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) = |
81a0bf43 | 3007 | get_ipset_cmdlist($ipset_ruleset, undef, $verbose); |
6b9f68a2 | 3008 | |
81a0bf43 | 3009 | my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset, $verbose); |
2a052ee3 | 3010 | |
81a0bf43 DM |
3011 | if ($verbose) { |
3012 | if ($ipset_changes) { | |
3013 | print "ipset changes:\n"; | |
3014 | print $ipset_create_cmdlist if $ipset_create_cmdlist; | |
3015 | print $ipset_delete_cmdlist if $ipset_delete_cmdlist; | |
3016 | } | |
3f95d14a | 3017 | |
81a0bf43 DM |
3018 | if ($changes) { |
3019 | print "iptables changes:\n"; | |
3020 | print $cmdlist; | |
3021 | } | |
3022 | } | |
3fa83edf | 3023 | |
2a052ee3 | 3024 | ipset_restore_cmdlist($ipset_create_cmdlist); |
34cdedfa | 3025 | |
3fa83edf DM |
3026 | iptables_restore_cmdlist($cmdlist); |
3027 | ||
dd7a13fd | 3028 | ipset_restore_cmdlist($ipset_delete_cmdlist) if $ipset_delete_cmdlist; |
2a052ee3 | 3029 | |
6158271d | 3030 | # test: re-read status and check if everything is up to date |
4bd0a9c4 | 3031 | my $active_chains = iptables_get_chains(); |
81a0bf43 | 3032 | my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0); |
3fa83edf DM |
3033 | |
3034 | my $errors; | |
3035 | foreach my $chain (sort keys %$ruleset) { | |
3036 | my $stat = $statushash->{$chain}; | |
3037 | if ($stat->{action} ne 'exists') { | |
3038 | warn "unable to update chain '$chain'\n"; | |
3039 | $errors = 1; | |
3040 | } | |
3041 | } | |
b6360c3f | 3042 | |
3fa83edf | 3043 | die "unable to apply firewall changes\n" if $errors; |
46a2ac1f AD |
3044 | |
3045 | update_nf_conntrack_max($hostfw_conf); | |
3046 | ||
3047 | update_nf_conntrack_tcp_timeout_established($hostfw_conf); | |
3048 | ||
b6360c3f DM |
3049 | } |
3050 | ||
490cdead DM |
3051 | sub update_nf_conntrack_max { |
3052 | my ($hostfw_conf) = @_; | |
3053 | ||
3054 | my $max = 65536; # reasonable default | |
3055 | ||
3056 | my $options = $hostfw_conf->{options} || {}; | |
3057 | ||
3058 | if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) { | |
3059 | $max = $options->{nf_conntrack_max}; | |
3060 | $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192 | |
3061 | } | |
3062 | ||
3063 | my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max"; | |
3064 | my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize"; | |
3065 | ||
3066 | my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max); | |
3067 | ||
3068 | if ($current != $max) { | |
3069 | my $hashsize = int($max/4); | |
3070 | PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize); | |
3071 | PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max); | |
3072 | } | |
3073 | } | |
3074 | ||
28c082a1 AD |
3075 | sub update_nf_conntrack_tcp_timeout_established { |
3076 | my ($hostfw_conf) = @_; | |
3077 | ||
3078 | my $options = $hostfw_conf->{options} || {}; | |
3079 | ||
3080 | my $value = defined($options->{nf_conntrack_tcp_timeout_established}) ? $options->{nf_conntrack_tcp_timeout_established} : 432000; | |
3081 | ||
3082 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established", $value); | |
3083 | } | |
3084 | ||
c4a2e5ae DM |
3085 | sub remove_pvefw_chains { |
3086 | ||
3087 | my ($chash, $hooks) = iptables_get_chains(); | |
3088 | my $cmdlist = "*filter\n"; | |
3089 | ||
3090 | foreach my $h (qw(INPUT OUTPUT FORWARD)) { | |
3091 | if ($hooks->{$h}) { | |
3092 | $cmdlist .= "-D $h -j PVEFW-$h\n"; | |
3093 | } | |
3094 | } | |
cbb5d6f3 | 3095 | |
c4a2e5ae DM |
3096 | foreach my $chain (keys %$chash) { |
3097 | $cmdlist .= "-F $chain\n"; | |
3098 | } | |
3099 | ||
3100 | foreach my $chain (keys %$chash) { | |
3101 | $cmdlist .= "-X $chain\n"; | |
3102 | } | |
3103 | $cmdlist .= "COMMIT\n"; | |
3104 | ||
3105 | iptables_restore_cmdlist($cmdlist); | |
3106 | } | |
3107 | ||
8b453a09 DM |
3108 | sub init { |
3109 | my $cluster_conf = load_clusterfw_conf(); | |
3110 | my $cluster_options = $cluster_conf->{options}; | |
3111 | my $enable = $cluster_options->{enable}; | |
3112 | ||
3113 | return if !$enable; | |
3114 | ||
3115 | # load required modules here | |
3116 | } | |
3117 | ||
6b9f68a2 | 3118 | sub update { |
6b9f68a2 | 3119 | my $code = sub { |
3dfa8a7f DM |
3120 | |
3121 | my $cluster_conf = load_clusterfw_conf(); | |
3122 | my $cluster_options = $cluster_conf->{options}; | |
3123 | ||
51e57fee | 3124 | my $enable = $cluster_options->{enable}; |
3dfa8a7f | 3125 | |
e2beb7aa | 3126 | die "Firewall is disabled - cannot start\n" if !$enable; |
3dfa8a7f DM |
3127 | |
3128 | if (!$enable) { | |
b22130d3 | 3129 | PVE::Firewall::remove_pvefw_chains(); |
3dfa8a7f DM |
3130 | return; |
3131 | } | |
3132 | ||
3133 | my $hostfw_conf = load_hostfw_conf(); | |
3134 | ||
3135 | my ($ruleset, $ipset_ruleset) = compile($cluster_conf, $hostfw_conf); | |
490cdead | 3136 | |
d4cda423 | 3137 | apply_ruleset($ruleset, $hostfw_conf, $ipset_ruleset); |
6b9f68a2 DM |
3138 | }; |
3139 | ||
3140 | run_locked($code); | |
3141 | } | |
3142 | ||
b6360c3f | 3143 | 1; |