Commit | Line | Data |
---|---|---|
b6360c3f DM |
1 | package PVE::Firewall; |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | use Data::Dumper; | |
09d5f68e | 6 | use Digest::SHA; |
f789653a | 7 | use PVE::Tools; |
b6360c3f | 8 | use PVE::QemuServer; |
5e1267a5 DM |
9 | use File::Path; |
10 | use IO::File; | |
ecbea048 | 11 | use Net::IP; |
06320eb0 | 12 | use PVE::Tools qw(run_command lock_file); |
ecbea048 | 13 | |
5e1267a5 | 14 | use Data::Dumper; |
b6360c3f | 15 | |
06320eb0 DM |
16 | my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; |
17 | ||
857f62c8 | 18 | # imported/converted from: /usr/share/shorewall/macro.* |
961b4928 | 19 | my $pve_fw_macros = { |
857f62c8 DM |
20 | 'Amanda' => [ |
21 | { action => 'PARAM', proto => 'udp', dport => '10080' }, | |
22 | { action => 'PARAM', proto => 'tcp', dport => '10080' }, | |
23 | ], | |
24 | 'Auth' => [ | |
25 | { action => 'PARAM', proto => 'tcp', dport => '113' }, | |
26 | ], | |
27 | 'BGP' => [ | |
28 | { action => 'PARAM', proto => 'tcp', dport => '179' }, | |
29 | ], | |
30 | 'BitTorrent' => [ | |
961b4928 | 31 | { action => 'PARAM', proto => 'tcp', dport => '6881:6889' }, |
857f62c8 DM |
32 | { action => 'PARAM', proto => 'udp', dport => '6881' }, |
33 | ], | |
34 | 'BitTorrent32' => [ | |
35 | { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, | |
36 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
37 | ], | |
38 | 'CVS' => [ | |
39 | { action => 'PARAM', proto => 'tcp', dport => '2401' }, | |
40 | ], | |
41 | 'Citrix' => [ | |
42 | { action => 'PARAM', proto => 'tcp', dport => '1494' }, | |
43 | { action => 'PARAM', proto => 'udp', dport => '1604' }, | |
44 | { action => 'PARAM', proto => 'tcp', dport => '2598' }, | |
45 | ], | |
46 | 'DAAP' => [ | |
47 | { action => 'PARAM', proto => 'tcp', dport => '3689' }, | |
48 | { action => 'PARAM', proto => 'udp', dport => '3689' }, | |
49 | ], | |
50 | 'DCC' => [ | |
51 | { action => 'PARAM', proto => 'tcp', dport => '6277' }, | |
52 | ], | |
53 | 'DHCPfwd' => [ | |
54 | { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
55 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
56 | ], | |
57 | 'DNS' => [ | |
58 | { action => 'PARAM', proto => 'udp', dport => '53' }, | |
59 | { action => 'PARAM', proto => 'tcp', dport => '53' }, | |
60 | ], | |
61 | 'Distcc' => [ | |
62 | { action => 'PARAM', proto => 'tcp', dport => '3632' }, | |
63 | ], | |
64 | 'Edonkey' => [ | |
65 | { action => 'PARAM', proto => 'tcp', dport => '4662' }, | |
66 | { action => 'PARAM', proto => 'udp', dport => '4665' }, | |
961b4928 | 67 | ], |
857f62c8 DM |
68 | 'FTP' => [ |
69 | { action => 'PARAM', proto => 'tcp', dport => '21' }, | |
70 | ], | |
71 | 'Finger' => [ | |
72 | { action => 'PARAM', proto => 'tcp', dport => '79' }, | |
73 | ], | |
74 | 'GNUnet' => [ | |
75 | { action => 'PARAM', proto => 'tcp', dport => '2086' }, | |
76 | { action => 'PARAM', proto => 'udp', dport => '2086' }, | |
77 | { action => 'PARAM', proto => 'tcp', dport => '1080' }, | |
78 | { action => 'PARAM', proto => 'udp', dport => '1080' }, | |
79 | ], | |
80 | 'GRE' => [ | |
81 | { action => 'PARAM', proto => '47' }, | |
82 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
83 | ], | |
84 | 'Git' => [ | |
85 | { action => 'PARAM', proto => 'tcp', dport => '9418' }, | |
86 | ], | |
87 | 'Gnutella' => [ | |
88 | { action => 'PARAM', proto => 'tcp', dport => '6346' }, | |
89 | { action => 'PARAM', proto => 'udp', dport => '6346' }, | |
90 | ], | |
91 | 'HKP' => [ | |
92 | { action => 'PARAM', proto => 'tcp', dport => '11371' }, | |
93 | ], | |
94 | 'HTTP' => [ | |
961b4928 DM |
95 | { action => 'PARAM', proto => 'tcp', dport => '80' }, |
96 | ], | |
857f62c8 DM |
97 | 'HTTPS' => [ |
98 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
99 | ], | |
100 | 'ICPV2' => [ | |
101 | { action => 'PARAM', proto => 'udp', dport => '3130' }, | |
102 | ], | |
103 | 'ICQ' => [ | |
104 | { action => 'PARAM', proto => 'tcp', dport => '5190' }, | |
105 | ], | |
106 | 'IMAP' => [ | |
107 | { action => 'PARAM', proto => 'tcp', dport => '143' }, | |
108 | ], | |
109 | 'IMAPS' => [ | |
110 | { action => 'PARAM', proto => 'tcp', dport => '993' }, | |
111 | ], | |
112 | 'IPIP' => [ | |
113 | { action => 'PARAM', proto => '94' }, | |
114 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' }, | |
115 | ], | |
116 | 'IPP' => [ | |
117 | { action => 'PARAM', proto => 'tcp', dport => '631' }, | |
118 | ], | |
119 | 'IPPbrd' => [ | |
120 | { action => 'PARAM', proto => 'udp', dport => '631' }, | |
121 | ], | |
122 | 'IPPserver' => [ | |
123 | { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' }, | |
124 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' }, | |
125 | ], | |
126 | 'IPsec' => [ | |
127 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
128 | { action => 'PARAM', proto => '50' }, | |
129 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
130 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
131 | ], | |
132 | 'IPsecah' => [ | |
133 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
134 | { action => 'PARAM', proto => '51' }, | |
135 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
136 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' }, | |
137 | ], | |
138 | 'IPsecnat' => [ | |
139 | { action => 'PARAM', proto => 'udp', dport => '500' }, | |
140 | { action => 'PARAM', proto => 'udp', dport => '4500' }, | |
141 | { action => 'PARAM', proto => '50' }, | |
142 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' }, | |
143 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' }, | |
144 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
145 | ], | |
146 | 'IRC' => [ | |
147 | { action => 'PARAM', proto => 'tcp', dport => '6667' }, | |
148 | ], | |
149 | 'JabberPlain' => [ | |
150 | { action => 'PARAM', proto => 'tcp', dport => '5222' }, | |
151 | ], | |
152 | 'JabberSecure' => [ | |
153 | { action => 'PARAM', proto => 'tcp', dport => '5223' }, | |
154 | ], | |
155 | 'Jabberd' => [ | |
156 | { action => 'PARAM', proto => 'tcp', dport => '5269' }, | |
157 | ], | |
158 | 'Jetdirect' => [ | |
159 | { action => 'PARAM', proto => 'tcp', dport => '9100' }, | |
160 | ], | |
161 | 'L2TP' => [ | |
162 | { action => 'PARAM', proto => 'udp', dport => '1701' }, | |
163 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' }, | |
164 | ], | |
165 | 'LDAP' => [ | |
166 | { action => 'PARAM', proto => 'tcp', dport => '389' }, | |
167 | ], | |
168 | 'LDAPS' => [ | |
169 | { action => 'PARAM', proto => 'tcp', dport => '636' }, | |
170 | ], | |
171 | 'MSNP' => [ | |
172 | { action => 'PARAM', proto => 'tcp', dport => '1863' }, | |
173 | ], | |
174 | 'MSSQL' => [ | |
175 | { action => 'PARAM', proto => 'tcp', dport => '1433' }, | |
176 | ], | |
177 | 'Mail' => [ | |
178 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
179 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
180 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
181 | ], | |
182 | 'Munin' => [ | |
183 | { action => 'PARAM', proto => 'tcp', dport => '4949' }, | |
184 | ], | |
185 | 'MySQL' => [ | |
186 | { action => 'PARAM', proto => 'tcp', dport => '3306' }, | |
187 | ], | |
188 | 'NNTP' => [ | |
189 | { action => 'PARAM', proto => 'tcp', dport => '119' }, | |
190 | ], | |
191 | 'NNTPS' => [ | |
192 | { action => 'PARAM', proto => 'tcp', dport => '563' }, | |
193 | ], | |
194 | 'NTP' => [ | |
195 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
196 | ], | |
197 | 'NTPbi' => [ | |
198 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
199 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' }, | |
200 | ], | |
201 | 'NTPbrd' => [ | |
202 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
203 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' }, | |
204 | ], | |
205 | 'OSPF' => [ | |
206 | { action => 'PARAM', proto => '89' }, | |
207 | ], | |
208 | 'OpenVPN' => [ | |
209 | { action => 'PARAM', proto => 'udp', dport => '1194' }, | |
210 | ], | |
211 | 'PCA' => [ | |
212 | { action => 'PARAM', proto => 'udp', dport => '5632' }, | |
213 | { action => 'PARAM', proto => 'tcp', dport => '5631' }, | |
214 | ], | |
215 | 'POP3' => [ | |
216 | { action => 'PARAM', proto => 'tcp', dport => '110' }, | |
217 | ], | |
218 | 'POP3S' => [ | |
219 | { action => 'PARAM', proto => 'tcp', dport => '995' }, | |
220 | ], | |
221 | 'PPtP' => [ | |
222 | { action => 'PARAM', proto => '47' }, | |
223 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
224 | { action => 'PARAM', proto => 'tcp', dport => '1723' }, | |
225 | ], | |
226 | 'Ping' => [ | |
227 | { action => 'PARAM', proto => 'icmp', dport => '8' }, | |
228 | ], | |
229 | 'PostgreSQL' => [ | |
230 | { action => 'PARAM', proto => 'tcp', dport => '5432' }, | |
231 | ], | |
232 | 'Printer' => [ | |
233 | { action => 'PARAM', proto => 'tcp', dport => '515' }, | |
234 | ], | |
235 | 'RDP' => [ | |
236 | { action => 'PARAM', proto => 'tcp', dport => '3389' }, | |
237 | ], | |
238 | 'RIPbi' => [ | |
239 | { action => 'PARAM', proto => 'udp', dport => '520' }, | |
240 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' }, | |
241 | ], | |
242 | 'RNDC' => [ | |
243 | { action => 'PARAM', proto => 'tcp', dport => '953' }, | |
244 | ], | |
245 | 'Razor' => [ | |
246 | { action => 'ACCEPT', proto => 'tcp', dport => '2703' }, | |
247 | ], | |
248 | 'Rdate' => [ | |
249 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
250 | ], | |
251 | 'Rsync' => [ | |
252 | { action => 'PARAM', proto => 'tcp', dport => '873' }, | |
253 | ], | |
254 | 'SANE' => [ | |
255 | { action => 'PARAM', proto => 'tcp', dport => '6566' }, | |
256 | ], | |
257 | 'SMB' => [ | |
258 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
259 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
260 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
261 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
262 | ], | |
263 | 'SMBBI' => [ | |
264 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
265 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
266 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
267 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
268 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' }, | |
269 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' }, | |
270 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
271 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' }, | |
272 | ], | |
273 | 'SMBswat' => [ | |
274 | { action => 'PARAM', proto => 'tcp', dport => '901' }, | |
275 | ], | |
276 | 'SMTP' => [ | |
277 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
278 | ], | |
279 | 'SMTPS' => [ | |
280 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
281 | ], | |
282 | 'SNMP' => [ | |
283 | { action => 'PARAM', proto => 'udp', dport => '161:162' }, | |
284 | { action => 'PARAM', proto => 'tcp', dport => '161' }, | |
285 | ], | |
286 | 'SPAMD' => [ | |
287 | { action => 'PARAM', proto => 'tcp', dport => '783' }, | |
288 | ], | |
289 | 'SSH' => [ | |
290 | { action => 'PARAM', proto => 'tcp', dport => '22' }, | |
291 | ], | |
292 | 'SVN' => [ | |
293 | { action => 'PARAM', proto => 'tcp', dport => '3690' }, | |
294 | ], | |
295 | 'SixXS' => [ | |
296 | { action => 'PARAM', proto => 'tcp', dport => '3874' }, | |
297 | { action => 'PARAM', proto => 'udp', dport => '3740' }, | |
298 | { action => 'PARAM', proto => '41' }, | |
299 | { action => 'PARAM', proto => 'udp', dport => '5072,8374' }, | |
300 | ], | |
301 | 'Squid' => [ | |
302 | { action => 'PARAM', proto => 'tcp', dport => '3128' }, | |
303 | ], | |
304 | 'Submission' => [ | |
305 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
306 | ], | |
307 | 'Syslog' => [ | |
308 | { action => 'PARAM', proto => 'udp', dport => '514' }, | |
309 | { action => 'PARAM', proto => 'tcp', dport => '514' }, | |
310 | ], | |
311 | 'TFTP' => [ | |
312 | { action => 'PARAM', proto => 'udp', dport => '69' }, | |
313 | ], | |
314 | 'Telnet' => [ | |
315 | { action => 'PARAM', proto => 'tcp', dport => '23' }, | |
316 | ], | |
317 | 'Telnets' => [ | |
318 | { action => 'PARAM', proto => 'tcp', dport => '992' }, | |
319 | ], | |
320 | 'Time' => [ | |
321 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
322 | ], | |
323 | 'Trcrt' => [ | |
324 | { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, | |
325 | { action => 'PARAM', proto => 'icmp', dport => '8' }, | |
326 | ], | |
327 | 'VNC' => [ | |
328 | { action => 'PARAM', proto => 'tcp', dport => '5900:5909' }, | |
329 | ], | |
330 | 'VNCL' => [ | |
331 | { action => 'PARAM', proto => 'tcp', dport => '5500' }, | |
332 | ], | |
333 | 'Web' => [ | |
334 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
961b4928 DM |
335 | { action => 'PARAM', proto => 'tcp', dport => '443' }, |
336 | ], | |
857f62c8 DM |
337 | 'Webcache' => [ |
338 | { action => 'PARAM', proto => 'tcp', dport => '8080' }, | |
339 | ], | |
340 | 'Webmin' => [ | |
341 | { action => 'PARAM', proto => 'tcp', dport => '10000' }, | |
342 | ], | |
343 | 'Whois' => [ | |
344 | { action => 'PARAM', proto => 'tcp', dport => '43' }, | |
345 | ], | |
961b4928 DM |
346 | }; |
347 | ||
348 | my $pve_fw_parsed_macros; | |
349 | my $pve_fw_preferred_macro_names = {}; | |
3a616aa0 | 350 | |
d8f2505e DM |
351 | my $pve_std_chains = { |
352 | 'PVEFW-SET-ACCEPT-MARK' => [ | |
353 | "-j MARK --set-mark 1", | |
354 | ], | |
355 | }; | |
356 | ||
4b586518 DM |
357 | # iptables -p icmp -h |
358 | my $icmp_type_names = { | |
359 | any => 1, | |
360 | 'echo-reply' => 1, | |
361 | 'destination-unreachable' => 1, | |
362 | 'network-unreachable' => 1, | |
363 | 'host-unreachable' => 1, | |
364 | 'protocol-unreachable' => 1, | |
365 | 'port-unreachable' => 1, | |
366 | 'fragmentation-needed' => 1, | |
367 | 'source-route-failed' => 1, | |
368 | 'network-unknown' => 1, | |
369 | 'host-unknown' => 1, | |
370 | 'network-prohibited' => 1, | |
371 | 'host-prohibited' => 1, | |
372 | 'TOS-network-unreachable' => 1, | |
373 | 'TOS-host-unreachable' => 1, | |
374 | 'communication-prohibited' => 1, | |
375 | 'host-precedence-violation' => 1, | |
376 | 'precedence-cutoff' => 1, | |
377 | 'source-quench' => 1, | |
378 | 'redirect' => 1, | |
379 | 'network-redirect' => 1, | |
380 | 'host-redirect' => 1, | |
381 | 'TOS-network-redirect' => 1, | |
382 | 'TOS-host-redirect' => 1, | |
383 | 'echo-request' => 1, | |
384 | 'router-advertisement' => 1, | |
385 | 'router-solicitation' => 1, | |
386 | 'time-exceeded' => 1, | |
387 | 'ttl-zero-during-transit' => 1, | |
388 | 'ttl-zero-during-reassembly' => 1, | |
389 | 'parameter-problem' => 1, | |
390 | 'ip-header-bad' => 1, | |
391 | 'required-option-missing' => 1, | |
392 | 'timestamp-request' => 1, | |
393 | 'timestamp-reply' => 1, | |
394 | 'address-mask-request' => 1, | |
395 | 'address-mask-reply' => 1, | |
396 | }; | |
397 | ||
a332200b | 398 | sub get_firewall_macros { |
9aab3127 | 399 | |
961b4928 | 400 | return $pve_fw_parsed_macros if $pve_fw_parsed_macros; |
6158271d | 401 | |
961b4928 | 402 | $pve_fw_parsed_macros = {}; |
9aab3127 | 403 | |
961b4928 DM |
404 | foreach my $k (keys %$pve_fw_macros) { |
405 | my $name = lc($k); | |
a332200b | 406 | |
961b4928 DM |
407 | my $macro = $pve_fw_macros->{$k}; |
408 | $pve_fw_preferred_macro_names->{$name} = $k; | |
409 | $pve_fw_parsed_macros->{$name} = $macro; | |
410 | } | |
a332200b | 411 | |
961b4928 | 412 | return $pve_fw_parsed_macros; |
9aab3127 DM |
413 | } |
414 | ||
fcba0beb DM |
415 | my $etc_services; |
416 | ||
417 | sub get_etc_services { | |
418 | ||
419 | return $etc_services if $etc_services; | |
420 | ||
421 | my $filename = "/etc/services"; | |
422 | ||
423 | my $fh = IO::File->new($filename, O_RDONLY); | |
424 | if (!$fh) { | |
425 | warn "unable to read '$filename' - $!\n"; | |
426 | return {}; | |
427 | } | |
428 | ||
429 | my $services = {}; | |
430 | ||
431 | while (my $line = <$fh>) { | |
432 | chomp ($line); | |
433 | next if $line =~m/^#/; | |
434 | next if ($line =~m/^\s*$/); | |
435 | ||
436 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
437 | $services->{byid}->{$2}->{name} = $1; | |
35b66e9d | 438 | $services->{byid}->{$2}->{port} = $2; |
fcba0beb DM |
439 | $services->{byid}->{$2}->{$3} = 1; |
440 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
441 | } | |
442 | } | |
443 | ||
444 | close($fh); | |
445 | ||
6158271d DM |
446 | $etc_services = $services; |
447 | ||
3a616aa0 | 448 | |
fcba0beb DM |
449 | return $etc_services; |
450 | } | |
451 | ||
452 | my $etc_protocols; | |
453 | ||
454 | sub get_etc_protocols { | |
455 | return $etc_protocols if $etc_protocols; | |
456 | ||
457 | my $filename = "/etc/protocols"; | |
458 | ||
459 | my $fh = IO::File->new($filename, O_RDONLY); | |
460 | if (!$fh) { | |
461 | warn "unable to read '$filename' - $!\n"; | |
462 | return {}; | |
463 | } | |
464 | ||
465 | my $protocols = {}; | |
466 | ||
467 | while (my $line = <$fh>) { | |
468 | chomp ($line); | |
469 | next if $line =~m/^#/; | |
470 | next if ($line =~m/^\s*$/); | |
471 | ||
472 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
473 | $protocols->{byid}->{$2}->{name} = $1; | |
474 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
475 | } | |
476 | } | |
477 | ||
478 | close($fh); | |
479 | ||
480 | $etc_protocols = $protocols; | |
481 | ||
482 | return $etc_protocols; | |
483 | } | |
484 | ||
ecbea048 DM |
485 | sub parse_address_list { |
486 | my ($str) = @_; | |
487 | ||
d6de1dc2 | 488 | my $nbaor = 0; |
ecbea048 DM |
489 | foreach my $aor (split(/,/, $str)) { |
490 | if (!Net::IP->new($aor)) { | |
491 | my $err = Net::IP::Error(); | |
492 | die "invalid IP address: $err\n"; | |
d6de1dc2 AD |
493 | }else{ |
494 | $nbaor++; | |
ecbea048 DM |
495 | } |
496 | } | |
d6de1dc2 | 497 | return $nbaor; |
ecbea048 | 498 | } |
dddd9413 | 499 | |
fcba0beb DM |
500 | sub parse_port_name_number_or_range { |
501 | my ($str) = @_; | |
502 | ||
503 | my $services = PVE::Firewall::get_etc_services(); | |
4cdbb3b7 | 504 | my $nbports = 0; |
fcba0beb | 505 | foreach my $item (split(/,/, $str)) { |
4cdbb3b7 | 506 | my $portlist = ""; |
35b66e9d | 507 | my $oldpon = undef; |
4b586518 | 508 | $nbports++; |
fcba0beb | 509 | foreach my $pon (split(':', $item, 2)) { |
35b66e9d | 510 | $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port}; |
4cdbb3b7 | 511 | if ($pon =~ m/^\d+$/){ |
857f62c8 | 512 | die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535; |
35b66e9d AD |
513 | die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon); |
514 | $oldpon = $pon; | |
4cdbb3b7 AD |
515 | }else{ |
516 | die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; | |
517 | } | |
fcba0beb DM |
518 | } |
519 | } | |
520 | ||
4cdbb3b7 | 521 | return ($nbports); |
fcba0beb DM |
522 | } |
523 | ||
780bcc0f DM |
524 | my $bridge_firewall_enabled = 0; |
525 | ||
526 | sub enable_bridge_firewall { | |
527 | ||
528 | return if $bridge_firewall_enabled; # only once | |
529 | ||
530 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables"); | |
531 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables"); | |
532 | ||
533 | $bridge_firewall_enabled = 1; | |
534 | } | |
535 | ||
8cebfa6f | 536 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; |
dddd9413 | 537 | |
3a616aa0 AD |
538 | sub iptables { |
539 | my ($cmd) = @_; | |
540 | ||
541 | run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); | |
542 | } | |
543 | ||
b16e818e DM |
544 | sub iptables_restore_cmdlist { |
545 | my ($cmdlist) = @_; | |
3a616aa0 | 546 | |
3fa83edf | 547 | run_command("/sbin/iptables-restore -n", input => $cmdlist); |
3a616aa0 AD |
548 | } |
549 | ||
de2a57cd DM |
550 | sub iptables_get_chains { |
551 | ||
552 | my $res = {}; | |
553 | ||
554 | # check what chains we want to track | |
555 | my $is_pvefw_chain = sub { | |
556 | my $name = shift; | |
557 | ||
dec84fcd DM |
558 | return 1 if $name =~ m/^PVEFW-\S+$/; |
559 | ||
de2a57cd | 560 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; |
5927497d | 561 | return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/; |
f50656c2 | 562 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; |
de2a57cd DM |
563 | |
564 | return undef; | |
565 | }; | |
566 | ||
567 | my $table = ''; | |
568 | ||
de2a57cd DM |
569 | my $parser = sub { |
570 | my $line = shift; | |
571 | ||
572 | return if $line =~ m/^#/; | |
573 | return if $line =~ m/^\s*$/; | |
574 | ||
575 | if ($line =~ m/^\*(\S+)$/) { | |
576 | $table = $1; | |
577 | return; | |
578 | } | |
579 | ||
580 | return if $table ne 'filter'; | |
581 | ||
582 | if ($line =~ m/^:(\S+)\s/) { | |
583 | my $chain = $1; | |
584 | return if !&$is_pvefw_chain($chain); | |
3fa83edf | 585 | $res->{$chain} = "unknown"; |
09d5f68e | 586 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { |
3fa83edf | 587 | my ($chain, $sig) = ($1, $2); |
de2a57cd | 588 | return if !&$is_pvefw_chain($chain); |
3fa83edf | 589 | $res->{$chain} = $sig; |
de2a57cd DM |
590 | } else { |
591 | # simply ignore the rest | |
592 | return; | |
593 | } | |
594 | }; | |
595 | ||
596 | run_command("/sbin/iptables-save", outfunc => $parser); | |
597 | ||
de2a57cd DM |
598 | return $res; |
599 | } | |
600 | ||
3a616aa0 AD |
601 | sub iptables_chain_exist { |
602 | my ($chain) = @_; | |
603 | ||
604 | eval{ | |
605 | iptables("-n --list $chain"); | |
606 | }; | |
607 | return undef if $@; | |
608 | ||
609 | return 1; | |
610 | } | |
611 | ||
612 | sub iptables_rule_exist { | |
613 | my ($rule) = @_; | |
614 | ||
615 | eval{ | |
616 | iptables("-C $rule"); | |
617 | }; | |
618 | return undef if $@; | |
619 | ||
620 | return 1; | |
621 | } | |
622 | ||
3fa83edf | 623 | sub ruleset_generate_rule { |
4e6112f9 | 624 | my ($ruleset, $chain, $rule, $goto) = @_; |
3a616aa0 | 625 | |
3fa83edf | 626 | my $cmd = ''; |
3a616aa0 | 627 | |
d6de1dc2 | 628 | $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1; |
3a616aa0 | 629 | $cmd .= " -s $rule->{source}" if $rule->{source}; |
d6de1dc2 | 630 | $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1; |
8c32a215 | 631 | $cmd .= " -d $rule->{dest}" if $rule->{dest}; |
3a616aa0 | 632 | $cmd .= " -p $rule->{proto}" if $rule->{proto}; |
4b586518 DM |
633 | |
634 | if (($rule->{nbdport} && $rule->{nbdport} > 1) || | |
635 | ($rule->{nbsport} && $rule->{nbsport} > 1)) { | |
636 | $cmd .= " --match multiport" | |
637 | } | |
638 | ||
639 | if ($rule->{dport}) { | |
640 | if ($rule->{proto} && $rule->{proto} eq 'icmp') { | |
641 | # Note: we use dport to store --icmp-type | |
642 | die "unknown icmp-type\n" if !$icmp_type_names->{$rule->{dport}}; | |
643 | $cmd .= " -m icmp --icmp-type $rule->{dport}"; | |
644 | } else { | |
645 | if ($rule->{nbdport} && $rule->{nbdport} > 1) { | |
646 | $cmd .= " --dports $rule->{dport}"; | |
647 | } else { | |
648 | $cmd .= " --dport $rule->{dport}"; | |
649 | } | |
650 | } | |
651 | } | |
652 | ||
653 | if ($rule->{sport}) { | |
654 | if ($rule->{nbsport} && $rule->{nbsport} > 1) { | |
655 | $cmd .= " --sports $rule->{sport}"; | |
656 | } else { | |
657 | $cmd .= " --sport $rule->{sport}"; | |
658 | } | |
659 | } | |
660 | ||
661 | $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; | |
4e6112f9 DM |
662 | |
663 | if (my $action = $rule->{action}) { | |
664 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
665 | $cmd .= $goto ? " -g $action" : " -j $action"; | |
666 | }; | |
3a616aa0 | 667 | |
3fa83edf DM |
668 | ruleset_addrule($ruleset, $chain, $cmd) if $cmd; |
669 | } | |
670 | ||
671 | sub ruleset_create_chain { | |
672 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 673 | |
d050c724 DM |
674 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; |
675 | ||
3fa83edf DM |
676 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; |
677 | ||
678 | $ruleset->{$chain} = []; | |
3a616aa0 AD |
679 | } |
680 | ||
3fa83edf DM |
681 | sub ruleset_chain_exist { |
682 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 683 | |
3fa83edf DM |
684 | return $ruleset->{$chain} ? 1 : undef; |
685 | } | |
3a616aa0 | 686 | |
3fa83edf DM |
687 | sub ruleset_addrule { |
688 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 689 | |
3fa83edf | 690 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 691 | |
3fa83edf DM |
692 | push @{$ruleset->{$chain}}, "-A $chain $rule"; |
693 | } | |
3a616aa0 | 694 | |
3fa83edf DM |
695 | sub ruleset_insertrule { |
696 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 697 | |
3fa83edf | 698 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 699 | |
3fa83edf DM |
700 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; |
701 | } | |
3a616aa0 | 702 | |
3fa83edf DM |
703 | sub generate_bridge_chains { |
704 | my ($ruleset, $bridge) = @_; | |
3a616aa0 | 705 | |
dec84fcd DM |
706 | if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){ |
707 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
7fab95bc | 708 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); |
3a616aa0 AD |
709 | } |
710 | ||
5927497d DM |
711 | if (!ruleset_chain_exist($ruleset, "$bridge-FW")) { |
712 | ruleset_create_chain($ruleset, "$bridge-FW"); | |
713 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW"); | |
714 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW"); | |
9fcad984 AD |
715 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing |
716 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing | |
3fa83edf | 717 | } |
3a616aa0 | 718 | |
3fa83edf DM |
719 | if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) { |
720 | ruleset_create_chain($ruleset, "$bridge-OUT"); | |
5927497d | 721 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT"); |
9fcad984 AD |
722 | } |
723 | ||
724 | if (!ruleset_chain_exist($ruleset, "$bridge-IN")) { | |
725 | ruleset_create_chain($ruleset, "$bridge-IN"); | |
5927497d | 726 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN"); |
fdb0bf20 | 727 | ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT"); |
3fa83edf DM |
728 | } |
729 | } | |
3a616aa0 AD |
730 | |
731 | sub generate_tap_rules_direction { | |
fa9c4a6f DM |
732 | my ($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_; |
733 | ||
734 | my $rules = $vmfw_conf->{lc($direction)}; | |
735 | my $options = $vmfw_conf->{options}; | |
3a616aa0 AD |
736 | |
737 | my $tapchain = "$iface-$direction"; | |
738 | ||
3fa83edf | 739 | ruleset_create_chain($ruleset, $tapchain); |
3a616aa0 | 740 | |
7fab95bc DM |
741 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP"); |
742 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
3a616aa0 | 743 | |
c29f55c9 DM |
744 | if ($direction eq 'OUT' && defined($macaddr)) { |
745 | ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP"); | |
746 | } | |
747 | ||
3fa83edf | 748 | if ($rules) { |
3a616aa0 AD |
749 | foreach my $rule (@$rules) { |
750 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
bfbfa8b6 | 751 | # we go to $bridge-IN if accept in out rules |
3a616aa0 | 752 | if($rule->{action} =~ m/^(GROUP-(\S+))$/){ |
3fa83edf DM |
753 | $rule->{action} .= "-$direction"; |
754 | # generate empty group rule if don't exist | |
755 | if(!ruleset_chain_exist($ruleset, $rule->{action})){ | |
51bae274 | 756 | generate_group_rules($ruleset, $group_rules, $2); |
3fa83edf | 757 | } |
4e6112f9 | 758 | ruleset_generate_rule($ruleset, $tapchain, $rule); |
7b291cab | 759 | ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -j RETURN") |
31ff3ef6 | 760 | if $direction eq 'OUT'; |
4e6112f9 | 761 | } else { |
fdb0bf20 | 762 | $rule->{action} = "PVEFW-SET-ACCEPT-MARK" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT'; |
4e6112f9 | 763 | ruleset_generate_rule($ruleset, $tapchain, $rule); |
3a616aa0 | 764 | } |
4e6112f9 | 765 | } |
3a616aa0 AD |
766 | } |
767 | ||
ccae0b50 DM |
768 | # implement policy |
769 | my $policy; | |
770 | ||
771 | if ($direction eq 'OUT') { | |
772 | $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default | |
773 | } else { | |
774 | $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default | |
775 | } | |
776 | ||
777 | if ($policy eq 'ACCEPT') { | |
778 | if ($direction eq 'OUT') { | |
fdb0bf20 | 779 | ruleset_addrule($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK"); |
ccae0b50 DM |
780 | } else { |
781 | ruleset_addrule($ruleset, $tapchain, "-j ACCEPT"); | |
782 | } | |
783 | } elsif ($policy eq 'DROP') { | |
784 | ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4"); | |
785 | ruleset_addrule($ruleset, $tapchain, "-j DROP"); | |
786 | } elsif ($policy eq 'REJECT') { | |
787 | ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level 4"); | |
788 | ruleset_addrule($ruleset, $tapchain, "-j REJECT"); | |
789 | } else { | |
790 | # should not happen | |
791 | die "internal error: unknown policy '$policy'"; | |
792 | } | |
3a616aa0 | 793 | |
3fa83edf DM |
794 | # plug the tap chain to bridge chain |
795 | my $physdevdirection = $direction eq 'IN' ? "out" : "in"; | |
796 | my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain"; | |
797 | ruleset_insertrule($ruleset, "$bridge-$direction", $rule); | |
3a616aa0 | 798 | |
3fa83edf DM |
799 | if ($direction eq 'OUT'){ |
800 | # add tap->host rules | |
801 | my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain"; | |
dec84fcd | 802 | ruleset_addrule($ruleset, "PVEFW-INPUT", $rule); |
3a616aa0 AD |
803 | } |
804 | } | |
805 | ||
0bd5f137 | 806 | sub enablehostfw { |
51bae274 | 807 | my ($ruleset, $rules, $group_rules) = @_; |
0bd5f137 | 808 | |
51bae274 | 809 | # fixme: allow security groups |
0bd5f137 | 810 | |
3fa83edf | 811 | # host inbound firewall |
dec84fcd DM |
812 | my $chain = "PVEFW-HOST-IN"; |
813 | ruleset_create_chain($ruleset, $chain); | |
0bd5f137 | 814 | |
7fab95bc DM |
815 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
816 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
817 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); |
818 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 819 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 820 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 821 | |
3fa83edf DM |
822 | if ($rules->{in}) { |
823 | foreach my $rule (@{$rules->{in}}) { | |
824 | # we use RETURN because we need to check also tap rules | |
825 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 826 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 827 | } |
0bd5f137 AD |
828 | } |
829 | ||
dec84fcd DM |
830 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4"); |
831 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
0bd5f137 | 832 | |
3fa83edf | 833 | # host outbound firewall |
aadd745e | 834 | $chain = "PVEFW-HOST-OUT"; |
dec84fcd DM |
835 | ruleset_create_chain($ruleset, $chain); |
836 | ||
7fab95bc DM |
837 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
838 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
839 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); |
840 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 841 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 842 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 843 | |
3fa83edf DM |
844 | if ($rules->{out}) { |
845 | foreach my $rule (@{$rules->{out}}) { | |
846 | # we use RETURN because we need to check also tap rules | |
847 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 848 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 849 | } |
0bd5f137 AD |
850 | } |
851 | ||
dec84fcd DM |
852 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4"); |
853 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
6158271d | 854 | |
dec84fcd DM |
855 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); |
856 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
9d31b418 AD |
857 | } |
858 | ||
859 | sub generate_group_rules { | |
51bae274 | 860 | my ($ruleset, $group_rules, $group) = @_; |
9d31b418 | 861 | |
51bae274 | 862 | my $rules = $group_rules->{$group}; |
9d31b418 | 863 | |
51bae274 | 864 | die "no such security group '$group'\n" if !$rules; |
6158271d | 865 | |
3fa83edf | 866 | my $chain = "GROUP-${group}-IN"; |
9d31b418 | 867 | |
3fa83edf | 868 | ruleset_create_chain($ruleset, $chain); |
9d31b418 | 869 | |
3fa83edf DM |
870 | if ($rules->{in}) { |
871 | foreach my $rule (@{$rules->{in}}) { | |
872 | ruleset_generate_rule($ruleset, $chain, $rule); | |
9d31b418 AD |
873 | } |
874 | } | |
875 | ||
3fa83edf | 876 | $chain = "GROUP-${group}-OUT"; |
9d31b418 | 877 | |
3fa83edf | 878 | ruleset_create_chain($ruleset, $chain); |
4e6112f9 | 879 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark |
9d31b418 | 880 | |
3fa83edf DM |
881 | if ($rules->{out}) { |
882 | foreach my $rule (@{$rules->{out}}) { | |
6158271d DM |
883 | # we go the PVEFW-SET-ACCEPT-MARK Instead of ACCEPT) because we need to |
884 | # check also other tap rules (and group rules can be set on any bridge, | |
4e6112f9 DM |
885 | # so we can't go to VMBRXX-IN) |
886 | $rule->{action} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action} eq 'ACCEPT'; | |
6a6c7af7 | 887 | ruleset_generate_rule($ruleset, $chain, $rule); |
9d31b418 AD |
888 | } |
889 | } | |
9d31b418 AD |
890 | } |
891 | ||
51bae274 DM |
892 | my $MAX_NETS = 32; |
893 | my $valid_netdev_names = {}; | |
894 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
895 | $valid_netdev_names->{"net$i"} = 1; | |
896 | } | |
5e1267a5 | 897 | |
51bae274 DM |
898 | sub parse_fw_rule { |
899 | my ($line, $need_iface, $allow_groups) = @_; | |
5e1267a5 | 900 | |
a332200b | 901 | my $macros = get_firewall_macros(); |
fcba0beb | 902 | my $protocols = get_etc_protocols(); |
51bae274 DM |
903 | |
904 | my ($action, $iface, $source, $dest, $proto, $dport, $sport); | |
905 | ||
906 | $line =~ s/#.*$//; | |
907 | ||
908 | my @data = split(/\s+/, $line); | |
909 | my $expected_elements = $need_iface ? 7 : 6; | |
910 | ||
911 | die "wrong number of rule elements\n" if scalar(@data) > $expected_elements; | |
912 | ||
913 | if ($need_iface) { | |
914 | ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data | |
915 | } else { | |
916 | ($action, $source, $dest, $proto, $dport, $sport) = @data; | |
917 | } | |
918 | ||
919 | die "incomplete rule\n" if !$action; | |
920 | ||
961b4928 DM |
921 | my $macro; |
922 | my $macro_name; | |
923 | ||
51bae274 DM |
924 | if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) { |
925 | # OK | |
926 | } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) { | |
927 | # OK | |
928 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
961b4928 DM |
929 | ($macro_name, $action) = ($1, $2); |
930 | my $lc_macro_name = lc($macro_name); | |
931 | my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name}; | |
932 | $macro_name = $preferred_name if $preferred_name; | |
933 | $macro = $macros->{$lc_macro_name}; | |
934 | die "unknown macro '$macro_name'\n" if !$macro; | |
51bae274 DM |
935 | } else { |
936 | die "unknown action '$action'\n"; | |
937 | } | |
938 | ||
939 | if ($need_iface) { | |
940 | $iface = undef if $iface && $iface eq '-'; | |
6158271d | 941 | die "unknown interface '$iface'\n" |
51bae274 DM |
942 | if defined($iface) && !$valid_netdev_names->{$iface}; |
943 | } | |
944 | ||
945 | $proto = undef if $proto && $proto eq '-'; | |
6158271d | 946 | die "unknown protokol '$proto'\n" if $proto && |
51bae274 DM |
947 | !(defined($protocols->{byname}->{$proto}) || |
948 | defined($protocols->{byid}->{$proto})); | |
949 | ||
950 | $source = undef if $source && $source eq '-'; | |
951 | $dest = undef if $dest && $dest eq '-'; | |
952 | ||
953 | $dport = undef if $dport && $dport eq '-'; | |
954 | $sport = undef if $sport && $sport eq '-'; | |
955 | ||
51bae274 DM |
956 | my $nbsource = undef; |
957 | my $nbdest = undef; | |
958 | ||
959 | $nbsource = parse_address_list($source) if $source; | |
960 | $nbdest = parse_address_list($dest) if $dest; | |
6158271d | 961 | |
961b4928 | 962 | my $rules = []; |
51bae274 | 963 | |
961b4928 | 964 | my $param = { |
51bae274 | 965 | action => $action, |
51bae274 DM |
966 | iface => $iface, |
967 | source => $source, | |
968 | dest => $dest, | |
969 | nbsource => $nbsource, | |
970 | nbdest => $nbdest, | |
971 | proto => $proto, | |
972 | dport => $dport, | |
973 | sport => $sport, | |
51bae274 | 974 | }; |
961b4928 DM |
975 | |
976 | if ($macro) { | |
977 | foreach my $templ (@$macro) { | |
978 | my $rule = {}; | |
979 | foreach my $k (keys %$templ) { | |
980 | my $v = $templ->{$k}; | |
857f62c8 DM |
981 | if ($v eq 'PARAM') { |
982 | $v = $param->{$k}; | |
983 | } elsif ($v eq 'DEST') { | |
984 | $v = $param->{dest}; | |
985 | } elsif ($v eq 'SOURCE') { | |
986 | $v = $param->{source}; | |
987 | } | |
988 | ||
961b4928 DM |
989 | die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v); |
990 | $rule->{$k} = $v; | |
991 | } | |
992 | push @$rules, $rule; | |
993 | } | |
994 | } else { | |
995 | push @$rules, $param; | |
996 | } | |
997 | ||
998 | foreach my $rule (@$rules) { | |
6158271d | 999 | $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport}) |
961b4928 | 1000 | if defined($rule->{dport}); |
6158271d | 1001 | $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport}) |
961b4928 DM |
1002 | if defined($rule->{sport}); |
1003 | } | |
1004 | ||
1005 | return $rules; | |
51bae274 DM |
1006 | } |
1007 | ||
85c6eaed DM |
1008 | sub parse_fw_option { |
1009 | my ($line) = @_; | |
1010 | ||
1011 | my ($opt, $value); | |
1012 | ||
1013 | if ($line =~ m/^enable:\s*(0|1)\s*$/i) { | |
1014 | $opt = 'enable'; | |
1015 | $value = int($1); | |
1016 | } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
1017 | $opt = lc($1); | |
1018 | $value = uc($3); | |
1019 | } else { | |
1020 | chomp $line; | |
1021 | die "can't parse option '$line'\n" | |
1022 | } | |
1023 | ||
1024 | return ($opt, $value); | |
1025 | } | |
1026 | ||
51bae274 DM |
1027 | sub parse_vm_fw_rules { |
1028 | my ($filename, $fh) = @_; | |
1029 | ||
85c6eaed | 1030 | my $res = { in => [], out => [], options => {}}; |
51bae274 DM |
1031 | |
1032 | my $section; | |
1033 | ||
5e1267a5 DM |
1034 | while (defined(my $line = <$fh>)) { |
1035 | next if $line =~ m/^#/; | |
1036 | next if $line =~ m/^\s*$/; | |
1037 | ||
961b4928 DM |
1038 | my $linenr = $fh->input_line_number(); |
1039 | my $prefix = "$filename (line $linenr)"; | |
1040 | ||
85c6eaed | 1041 | if ($line =~ m/^\[(\S+)\]\s*$/i) { |
5e1267a5 | 1042 | $section = lc($1); |
85c6eaed | 1043 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; |
5e1267a5 DM |
1044 | next; |
1045 | } | |
51bae274 | 1046 | if (!$section) { |
961b4928 | 1047 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
1048 | next; |
1049 | } | |
1050 | ||
85c6eaed DM |
1051 | next if !$res->{$section}; # skip undefined section |
1052 | ||
1053 | if ($section eq 'options') { | |
6158271d DM |
1054 | eval { |
1055 | my ($opt, $value) = parse_fw_option($line); | |
85c6eaed DM |
1056 | $res->{options}->{$opt} = $value; |
1057 | }; | |
1058 | warn "$prefix: $@" if $@; | |
1059 | next; | |
1060 | } | |
1061 | ||
961b4928 DM |
1062 | my $rules; |
1063 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
51bae274 | 1064 | if (my $err = $@) { |
961b4928 | 1065 | warn "$prefix: $err"; |
5e1267a5 DM |
1066 | next; |
1067 | } | |
1068 | ||
961b4928 | 1069 | push @{$res->{$section}}, @$rules; |
51bae274 DM |
1070 | } |
1071 | ||
1072 | return $res; | |
1073 | } | |
1074 | ||
1075 | sub parse_host_fw_rules { | |
1076 | my ($filename, $fh) = @_; | |
1077 | ||
1078 | my $res = { in => [], out => [] }; | |
1079 | ||
1080 | my $section; | |
1081 | ||
1082 | while (defined(my $line = <$fh>)) { | |
1083 | next if $line =~ m/^#/; | |
1084 | next if $line =~ m/^\s*$/; | |
1085 | ||
961b4928 DM |
1086 | my $linenr = $fh->input_line_number(); |
1087 | my $prefix = "$filename (line $linenr)"; | |
1088 | ||
51bae274 DM |
1089 | if ($line =~ m/^\[(in|out)\]\s*$/i) { |
1090 | $section = lc($1); | |
5e1267a5 DM |
1091 | next; |
1092 | } | |
51bae274 | 1093 | if (!$section) { |
961b4928 | 1094 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
1095 | next; |
1096 | } | |
1097 | ||
961b4928 DM |
1098 | my $rules; |
1099 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
ecbea048 | 1100 | if (my $err = $@) { |
961b4928 | 1101 | warn "$prefix: $err"; |
ecbea048 | 1102 | next; |
ecbea048 DM |
1103 | } |
1104 | ||
961b4928 | 1105 | push @{$res->{$section}}, @$rules; |
51bae274 | 1106 | } |
ecbea048 | 1107 | |
51bae274 DM |
1108 | return $res; |
1109 | } | |
4cdbb3b7 | 1110 | |
51bae274 DM |
1111 | sub parse_group_fw_rules { |
1112 | my ($filename, $fh) = @_; | |
5e1267a5 | 1113 | |
51bae274 DM |
1114 | my $section; |
1115 | my $group; | |
1116 | ||
1117 | my $res = { in => [], out => [] }; | |
6158271d | 1118 | |
51bae274 DM |
1119 | while (defined(my $line = <$fh>)) { |
1120 | next if $line =~ m/^#/; | |
1121 | next if $line =~ m/^\s*$/; | |
1122 | ||
961b4928 DM |
1123 | my $linenr = $fh->input_line_number(); |
1124 | my $prefix = "$filename (line $linenr)"; | |
1125 | ||
51bae274 DM |
1126 | if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) { |
1127 | $section = lc($1); | |
1128 | $group = lc($2); | |
1129 | next; | |
1130 | } | |
1131 | if (!$section || !$group) { | |
961b4928 | 1132 | warn "$prefix: skip line - no section"; |
51bae274 DM |
1133 | next; |
1134 | } | |
1135 | ||
961b4928 DM |
1136 | my $rules; |
1137 | eval { $rules = parse_fw_rule($line, 0, 0); }; | |
51bae274 | 1138 | if (my $err = $@) { |
961b4928 | 1139 | warn "$prefix: $err"; |
51bae274 DM |
1140 | next; |
1141 | } | |
1142 | ||
961b4928 | 1143 | push @{$res->{$group}->{$section}}, @$rules; |
5e1267a5 DM |
1144 | } |
1145 | ||
1146 | return $res; | |
1147 | } | |
1148 | ||
06320eb0 DM |
1149 | sub run_locked { |
1150 | my ($code, @param) = @_; | |
1151 | ||
1152 | my $timeout = 10; | |
1153 | ||
1154 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
1155 | ||
1156 | die $@ if $@; | |
1157 | ||
1158 | return $res; | |
1159 | } | |
1160 | ||
5e1267a5 DM |
1161 | sub read_local_vm_config { |
1162 | ||
1163 | my $openvz = {}; | |
1164 | ||
1165 | my $qemu = {}; | |
1166 | ||
1167 | my $list = PVE::QemuServer::config_list(); | |
1168 | ||
1169 | foreach my $vmid (keys %$list) { | |
5e1267a5 DM |
1170 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); |
1171 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
1172 | $qemu->{$vmid} = $conf; | |
1173 | } | |
1174 | } | |
1175 | ||
1176 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
1177 | ||
1178 | return $vmdata; | |
1179 | }; | |
1180 | ||
1181 | sub read_vm_firewall_rules { | |
1182 | my ($vmdata) = @_; | |
1183 | my $rules = {}; | |
1184 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
1185 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
1186 | my $fh = IO::File->new($filename, O_RDONLY); | |
1187 | next if !$fh; | |
1188 | ||
51bae274 | 1189 | $rules->{$vmid} = parse_vm_fw_rules($filename, $fh); |
5e1267a5 DM |
1190 | } |
1191 | ||
1192 | return $rules; | |
1193 | } | |
1194 | ||
d8f2505e DM |
1195 | sub generate_std_chains { |
1196 | my ($ruleset) = @_; | |
1197 | ||
1198 | foreach my $chain (keys %$pve_std_chains) { | |
1199 | ruleset_create_chain($ruleset, $chain); | |
1200 | foreach my $rule (@{$pve_std_chains->{$chain}}) { | |
1201 | if (ref($rule)) { | |
1202 | ruleset_generate_rule($ruleset, $chain, $rule); | |
1203 | } else { | |
1204 | ruleset_addrule($ruleset, $chain, $rule); | |
1205 | } | |
1206 | } | |
1207 | } | |
1208 | } | |
1209 | ||
5e1267a5 | 1210 | sub compile { |
5e1267a5 DM |
1211 | my $vmdata = read_local_vm_config(); |
1212 | my $rules = read_vm_firewall_rules($vmdata); | |
6158271d | 1213 | |
51bae274 DM |
1214 | my $group_rules = {}; |
1215 | my $filename = "/etc/pve/firewall/groups.fw"; | |
1216 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1217 | $group_rules = parse_group_fw_rules($filename, $fh); | |
1218 | } | |
5e1267a5 | 1219 | |
3fa83edf DM |
1220 | #print Dumper($rules); |
1221 | ||
1222 | my $ruleset = {}; | |
1223 | ||
dec84fcd DM |
1224 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); |
1225 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
fadb13dd | 1226 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); |
3fa83edf | 1227 | |
d8f2505e | 1228 | generate_std_chains($ruleset); |
4e6112f9 | 1229 | |
fadb13dd | 1230 | my $enable_hostfw = 0; |
51bae274 DM |
1231 | $filename = "/etc/pve/local/host.fw"; |
1232 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1233 | my $host_rules = parse_host_fw_rules($filename, $fh); | |
fadb13dd DM |
1234 | |
1235 | $enable_hostfw = 1; | |
1236 | ||
51bae274 DM |
1237 | enablehostfw($ruleset, $host_rules, $group_rules); |
1238 | } | |
3fa83edf | 1239 | |
6158271d | 1240 | # generate firewall rules for QEMU VMs |
3fa83edf DM |
1241 | foreach my $vmid (keys %{$vmdata->{qemu}}) { |
1242 | my $conf = $vmdata->{qemu}->{$vmid}; | |
fa9c4a6f DM |
1243 | my $vmfw_conf = $rules->{$vmid}; |
1244 | next if !$vmfw_conf; | |
1245 | next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); | |
3fa83edf DM |
1246 | |
1247 | foreach my $netid (keys %$conf) { | |
1248 | next if $netid !~ m/^net(\d+)$/; | |
1249 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
1250 | next if !$net; | |
1251 | my $iface = "tap${vmid}i$1"; | |
5e1267a5 | 1252 | |
3fa83edf DM |
1253 | my $bridge = $net->{bridge}; |
1254 | next if !$bridge; # fixme: ? | |
1255 | ||
1256 | $bridge .= "v$net->{tag}" if $net->{tag}; | |
1257 | ||
fadb13dd | 1258 | |
3fa83edf DM |
1259 | generate_bridge_chains($ruleset, $bridge); |
1260 | ||
c29f55c9 | 1261 | my $macaddr = $net->{macaddr}; |
fa9c4a6f DM |
1262 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN'); |
1263 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT'); | |
3fa83edf DM |
1264 | } |
1265 | } | |
c0413e35 | 1266 | |
fadb13dd DM |
1267 | if ($enable_hostfw) { |
1268 | # allow traffic from lo (ourself) | |
1269 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT"); | |
1270 | } | |
c0413e35 | 1271 | |
3fa83edf DM |
1272 | return $ruleset; |
1273 | } | |
1274 | ||
1275 | sub get_ruleset_status { | |
1276 | my ($ruleset, $verbose) = @_; | |
1277 | ||
1278 | my $active_chains = iptables_get_chains(); | |
1279 | ||
1280 | my $statushash = {}; | |
1281 | ||
1282 | foreach my $chain (sort keys %$ruleset) { | |
09d5f68e | 1283 | my $digest = Digest::SHA->new('sha1'); |
3fa83edf DM |
1284 | foreach my $cmd (@{$ruleset->{$chain}}) { |
1285 | $digest->add("$cmd\n"); | |
1286 | } | |
1287 | my $sig = $digest->b64digest; | |
1288 | $statushash->{$chain}->{sig} = $sig; | |
1289 | ||
1290 | my $oldsig = $active_chains->{$chain}; | |
1291 | if (!defined($oldsig)) { | |
1292 | $statushash->{$chain}->{action} = 'create'; | |
1293 | } else { | |
1294 | if ($oldsig eq $sig) { | |
1295 | $statushash->{$chain}->{action} = 'exists'; | |
1296 | } else { | |
1297 | $statushash->{$chain}->{action} = 'update'; | |
1298 | } | |
1299 | } | |
1300 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
1301 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1302 | print "\t$cmd\n" if $verbose; | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | foreach my $chain (sort keys %$active_chains) { | |
1307 | if (!defined($ruleset->{$chain})) { | |
1308 | my $sig = $active_chains->{$chain}; | |
1309 | $statushash->{$chain}->{action} = 'delete'; | |
1310 | $statushash->{$chain}->{sig} = $sig; | |
1311 | print "delete $chain ($sig)\n" if $verbose; | |
1312 | } | |
6158271d | 1313 | } |
3fa83edf DM |
1314 | |
1315 | return $statushash; | |
1316 | } | |
1317 | ||
1318 | sub print_ruleset { | |
1319 | my ($ruleset) = @_; | |
1320 | ||
1321 | get_ruleset_status($ruleset, 1); | |
1322 | } | |
1323 | ||
1324 | sub print_sig_rule { | |
1325 | my ($chain, $sig) = @_; | |
1326 | ||
09d5f68e DM |
1327 | # We just use this to store a SHA1 checksum used to detect changes |
1328 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
b6360c3f DM |
1329 | } |
1330 | ||
a84f4d96 DM |
1331 | sub apply_ruleset { |
1332 | my ($ruleset, $verbose) = @_; | |
3fa83edf | 1333 | |
a84f4d96 | 1334 | enable_bridge_firewall(); |
3fa83edf DM |
1335 | |
1336 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
1337 | ||
1338 | my $statushash = get_ruleset_status($ruleset, $verbose); | |
1339 | ||
1340 | # create missing chains first | |
1341 | foreach my $chain (sort keys %$ruleset) { | |
1342 | my $stat = $statushash->{$chain}; | |
1343 | die "internal error" if !$stat; | |
1344 | next if $stat->{action} ne 'create'; | |
886aba9c | 1345 | |
3fa83edf DM |
1346 | $cmdlist .= ":$chain - [0:0]\n"; |
1347 | } | |
1348 | ||
dec84fcd | 1349 | my $rule = "INPUT -j PVEFW-INPUT"; |
3fa83edf DM |
1350 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1351 | $cmdlist .= "-A $rule\n"; | |
1352 | } | |
dec84fcd | 1353 | $rule = "OUTPUT -j PVEFW-OUTPUT"; |
3fa83edf DM |
1354 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1355 | $cmdlist .= "-A $rule\n"; | |
1356 | } | |
1357 | ||
dec84fcd | 1358 | $rule = "FORWARD -j PVEFW-FORWARD"; |
3fa83edf DM |
1359 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1360 | $cmdlist .= "-A $rule\n"; | |
1361 | } | |
1362 | ||
1363 | foreach my $chain (sort keys %$ruleset) { | |
1364 | my $stat = $statushash->{$chain}; | |
1365 | die "internal error" if !$stat; | |
1366 | ||
1367 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
1368 | $cmdlist .= "-F $chain\n"; | |
1369 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1370 | $cmdlist .= "$cmd\n"; | |
1371 | } | |
1372 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
1373 | } elsif ($stat->{action} eq 'delete') { | |
f5d28682 | 1374 | die "internal error"; # this should not happen |
3fa83edf DM |
1375 | } elsif ($stat->{action} eq 'exists') { |
1376 | # do nothing | |
1377 | } else { | |
1378 | die "internal error - unknown status '$stat->{action}'"; | |
1379 | } | |
1380 | } | |
1381 | ||
f5d28682 DM |
1382 | foreach my $chain (keys %$statushash) { |
1383 | next if $statushash->{$chain}->{action} ne 'delete'; | |
1384 | $cmdlist .= "-F $chain\n"; | |
1385 | } | |
1386 | foreach my $chain (keys %$statushash) { | |
1387 | next if $statushash->{$chain}->{action} ne 'delete'; | |
fadb13dd DM |
1388 | next if $chain eq 'PVEFW-INPUT'; |
1389 | next if $chain eq 'PVEFW-OUTPUT'; | |
1390 | next if $chain eq 'PVEFW-FORWARD'; | |
f5d28682 DM |
1391 | $cmdlist .= "-X $chain\n"; |
1392 | } | |
1393 | ||
3fa83edf DM |
1394 | $cmdlist .= "COMMIT\n"; |
1395 | ||
1396 | print $cmdlist if $verbose; | |
1397 | ||
1398 | iptables_restore_cmdlist($cmdlist); | |
1399 | ||
6158271d | 1400 | # test: re-read status and check if everything is up to date |
3fa83edf DM |
1401 | $statushash = get_ruleset_status($ruleset); |
1402 | ||
1403 | my $errors; | |
1404 | foreach my $chain (sort keys %$ruleset) { | |
1405 | my $stat = $statushash->{$chain}; | |
1406 | if ($stat->{action} ne 'exists') { | |
1407 | warn "unable to update chain '$chain'\n"; | |
1408 | $errors = 1; | |
1409 | } | |
1410 | } | |
b6360c3f | 1411 | |
3fa83edf | 1412 | die "unable to apply firewall changes\n" if $errors; |
b6360c3f DM |
1413 | } |
1414 | ||
b6360c3f | 1415 | 1; |