]>
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 | |
4b586518 DM |
351 | # iptables -p icmp -h |
352 | my $icmp_type_names = { | |
353 | any => 1, | |
354 | 'echo-reply' => 1, | |
355 | 'destination-unreachable' => 1, | |
356 | 'network-unreachable' => 1, | |
357 | 'host-unreachable' => 1, | |
358 | 'protocol-unreachable' => 1, | |
359 | 'port-unreachable' => 1, | |
360 | 'fragmentation-needed' => 1, | |
361 | 'source-route-failed' => 1, | |
362 | 'network-unknown' => 1, | |
363 | 'host-unknown' => 1, | |
364 | 'network-prohibited' => 1, | |
365 | 'host-prohibited' => 1, | |
366 | 'TOS-network-unreachable' => 1, | |
367 | 'TOS-host-unreachable' => 1, | |
368 | 'communication-prohibited' => 1, | |
369 | 'host-precedence-violation' => 1, | |
370 | 'precedence-cutoff' => 1, | |
371 | 'source-quench' => 1, | |
372 | 'redirect' => 1, | |
373 | 'network-redirect' => 1, | |
374 | 'host-redirect' => 1, | |
375 | 'TOS-network-redirect' => 1, | |
376 | 'TOS-host-redirect' => 1, | |
377 | 'echo-request' => 1, | |
378 | 'router-advertisement' => 1, | |
379 | 'router-solicitation' => 1, | |
380 | 'time-exceeded' => 1, | |
381 | 'ttl-zero-during-transit' => 1, | |
382 | 'ttl-zero-during-reassembly' => 1, | |
383 | 'parameter-problem' => 1, | |
384 | 'ip-header-bad' => 1, | |
385 | 'required-option-missing' => 1, | |
386 | 'timestamp-request' => 1, | |
387 | 'timestamp-reply' => 1, | |
388 | 'address-mask-request' => 1, | |
389 | 'address-mask-reply' => 1, | |
390 | }; | |
391 | ||
a332200b | 392 | sub get_firewall_macros { |
9aab3127 | 393 | |
961b4928 | 394 | return $pve_fw_parsed_macros if $pve_fw_parsed_macros; |
6158271d | 395 | |
961b4928 | 396 | $pve_fw_parsed_macros = {}; |
9aab3127 | 397 | |
961b4928 DM |
398 | foreach my $k (keys %$pve_fw_macros) { |
399 | my $name = lc($k); | |
a332200b | 400 | |
961b4928 DM |
401 | my $macro = $pve_fw_macros->{$k}; |
402 | $pve_fw_preferred_macro_names->{$name} = $k; | |
403 | $pve_fw_parsed_macros->{$name} = $macro; | |
404 | } | |
a332200b | 405 | |
961b4928 | 406 | return $pve_fw_parsed_macros; |
9aab3127 DM |
407 | } |
408 | ||
fcba0beb DM |
409 | my $etc_services; |
410 | ||
411 | sub get_etc_services { | |
412 | ||
413 | return $etc_services if $etc_services; | |
414 | ||
415 | my $filename = "/etc/services"; | |
416 | ||
417 | my $fh = IO::File->new($filename, O_RDONLY); | |
418 | if (!$fh) { | |
419 | warn "unable to read '$filename' - $!\n"; | |
420 | return {}; | |
421 | } | |
422 | ||
423 | my $services = {}; | |
424 | ||
425 | while (my $line = <$fh>) { | |
426 | chomp ($line); | |
427 | next if $line =~m/^#/; | |
428 | next if ($line =~m/^\s*$/); | |
429 | ||
430 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
431 | $services->{byid}->{$2}->{name} = $1; | |
35b66e9d | 432 | $services->{byid}->{$2}->{port} = $2; |
fcba0beb DM |
433 | $services->{byid}->{$2}->{$3} = 1; |
434 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
435 | } | |
436 | } | |
437 | ||
438 | close($fh); | |
439 | ||
6158271d DM |
440 | $etc_services = $services; |
441 | ||
3a616aa0 | 442 | |
fcba0beb DM |
443 | return $etc_services; |
444 | } | |
445 | ||
446 | my $etc_protocols; | |
447 | ||
448 | sub get_etc_protocols { | |
449 | return $etc_protocols if $etc_protocols; | |
450 | ||
451 | my $filename = "/etc/protocols"; | |
452 | ||
453 | my $fh = IO::File->new($filename, O_RDONLY); | |
454 | if (!$fh) { | |
455 | warn "unable to read '$filename' - $!\n"; | |
456 | return {}; | |
457 | } | |
458 | ||
459 | my $protocols = {}; | |
460 | ||
461 | while (my $line = <$fh>) { | |
462 | chomp ($line); | |
463 | next if $line =~m/^#/; | |
464 | next if ($line =~m/^\s*$/); | |
465 | ||
466 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
467 | $protocols->{byid}->{$2}->{name} = $1; | |
468 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
469 | } | |
470 | } | |
471 | ||
472 | close($fh); | |
473 | ||
474 | $etc_protocols = $protocols; | |
475 | ||
476 | return $etc_protocols; | |
477 | } | |
478 | ||
ecbea048 DM |
479 | sub parse_address_list { |
480 | my ($str) = @_; | |
481 | ||
d6de1dc2 | 482 | my $nbaor = 0; |
ecbea048 DM |
483 | foreach my $aor (split(/,/, $str)) { |
484 | if (!Net::IP->new($aor)) { | |
485 | my $err = Net::IP::Error(); | |
486 | die "invalid IP address: $err\n"; | |
d6de1dc2 AD |
487 | }else{ |
488 | $nbaor++; | |
ecbea048 DM |
489 | } |
490 | } | |
d6de1dc2 | 491 | return $nbaor; |
ecbea048 | 492 | } |
dddd9413 | 493 | |
fcba0beb DM |
494 | sub parse_port_name_number_or_range { |
495 | my ($str) = @_; | |
496 | ||
497 | my $services = PVE::Firewall::get_etc_services(); | |
4cdbb3b7 | 498 | my $nbports = 0; |
fcba0beb | 499 | foreach my $item (split(/,/, $str)) { |
4cdbb3b7 | 500 | my $portlist = ""; |
35b66e9d | 501 | my $oldpon = undef; |
4b586518 | 502 | $nbports++; |
fcba0beb | 503 | foreach my $pon (split(':', $item, 2)) { |
35b66e9d | 504 | $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port}; |
4cdbb3b7 | 505 | if ($pon =~ m/^\d+$/){ |
857f62c8 | 506 | die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535; |
35b66e9d AD |
507 | die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon); |
508 | $oldpon = $pon; | |
4cdbb3b7 AD |
509 | }else{ |
510 | die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; | |
511 | } | |
fcba0beb DM |
512 | } |
513 | } | |
514 | ||
4cdbb3b7 | 515 | return ($nbports); |
fcba0beb DM |
516 | } |
517 | ||
780bcc0f DM |
518 | my $bridge_firewall_enabled = 0; |
519 | ||
520 | sub enable_bridge_firewall { | |
521 | ||
522 | return if $bridge_firewall_enabled; # only once | |
523 | ||
524 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables"); | |
525 | system("echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables"); | |
526 | ||
527 | $bridge_firewall_enabled = 1; | |
528 | } | |
529 | ||
8cebfa6f | 530 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; |
dddd9413 | 531 | |
3a616aa0 AD |
532 | sub iptables { |
533 | my ($cmd) = @_; | |
534 | ||
535 | run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); | |
536 | } | |
537 | ||
b16e818e DM |
538 | sub iptables_restore_cmdlist { |
539 | my ($cmdlist) = @_; | |
3a616aa0 | 540 | |
3fa83edf | 541 | run_command("/sbin/iptables-restore -n", input => $cmdlist); |
3a616aa0 AD |
542 | } |
543 | ||
de2a57cd DM |
544 | sub iptables_get_chains { |
545 | ||
546 | my $res = {}; | |
547 | ||
548 | # check what chains we want to track | |
549 | my $is_pvefw_chain = sub { | |
550 | my $name = shift; | |
551 | ||
dec84fcd DM |
552 | return 1 if $name =~ m/^PVEFW-\S+$/; |
553 | ||
de2a57cd | 554 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; |
5927497d | 555 | return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/; |
f50656c2 | 556 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; |
de2a57cd DM |
557 | |
558 | return undef; | |
559 | }; | |
560 | ||
561 | my $table = ''; | |
562 | ||
de2a57cd DM |
563 | my $parser = sub { |
564 | my $line = shift; | |
565 | ||
566 | return if $line =~ m/^#/; | |
567 | return if $line =~ m/^\s*$/; | |
568 | ||
569 | if ($line =~ m/^\*(\S+)$/) { | |
570 | $table = $1; | |
571 | return; | |
572 | } | |
573 | ||
574 | return if $table ne 'filter'; | |
575 | ||
576 | if ($line =~ m/^:(\S+)\s/) { | |
577 | my $chain = $1; | |
578 | return if !&$is_pvefw_chain($chain); | |
3fa83edf | 579 | $res->{$chain} = "unknown"; |
09d5f68e | 580 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { |
3fa83edf | 581 | my ($chain, $sig) = ($1, $2); |
de2a57cd | 582 | return if !&$is_pvefw_chain($chain); |
3fa83edf | 583 | $res->{$chain} = $sig; |
de2a57cd DM |
584 | } else { |
585 | # simply ignore the rest | |
586 | return; | |
587 | } | |
588 | }; | |
589 | ||
590 | run_command("/sbin/iptables-save", outfunc => $parser); | |
591 | ||
de2a57cd DM |
592 | return $res; |
593 | } | |
594 | ||
3a616aa0 AD |
595 | sub iptables_chain_exist { |
596 | my ($chain) = @_; | |
597 | ||
598 | eval{ | |
599 | iptables("-n --list $chain"); | |
600 | }; | |
601 | return undef if $@; | |
602 | ||
603 | return 1; | |
604 | } | |
605 | ||
606 | sub iptables_rule_exist { | |
607 | my ($rule) = @_; | |
608 | ||
609 | eval{ | |
610 | iptables("-C $rule"); | |
611 | }; | |
612 | return undef if $@; | |
613 | ||
614 | return 1; | |
615 | } | |
616 | ||
3fa83edf | 617 | sub ruleset_generate_rule { |
4e6112f9 | 618 | my ($ruleset, $chain, $rule, $goto) = @_; |
3a616aa0 | 619 | |
3fa83edf | 620 | my $cmd = ''; |
3a616aa0 | 621 | |
d6de1dc2 | 622 | $cmd .= " -m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1; |
3a616aa0 | 623 | $cmd .= " -s $rule->{source}" if $rule->{source}; |
d6de1dc2 | 624 | $cmd .= " -m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1; |
8c32a215 | 625 | $cmd .= " -d $rule->{dest}" if $rule->{dest}; |
3a616aa0 | 626 | $cmd .= " -p $rule->{proto}" if $rule->{proto}; |
4b586518 DM |
627 | |
628 | if (($rule->{nbdport} && $rule->{nbdport} > 1) || | |
629 | ($rule->{nbsport} && $rule->{nbsport} > 1)) { | |
630 | $cmd .= " --match multiport" | |
631 | } | |
632 | ||
633 | if ($rule->{dport}) { | |
634 | if ($rule->{proto} && $rule->{proto} eq 'icmp') { | |
635 | # Note: we use dport to store --icmp-type | |
636 | die "unknown icmp-type\n" if !$icmp_type_names->{$rule->{dport}}; | |
637 | $cmd .= " -m icmp --icmp-type $rule->{dport}"; | |
638 | } else { | |
639 | if ($rule->{nbdport} && $rule->{nbdport} > 1) { | |
640 | $cmd .= " --dports $rule->{dport}"; | |
641 | } else { | |
642 | $cmd .= " --dport $rule->{dport}"; | |
643 | } | |
644 | } | |
645 | } | |
646 | ||
647 | if ($rule->{sport}) { | |
648 | if ($rule->{nbsport} && $rule->{nbsport} > 1) { | |
649 | $cmd .= " --sports $rule->{sport}"; | |
650 | } else { | |
651 | $cmd .= " --sport $rule->{sport}"; | |
652 | } | |
653 | } | |
654 | ||
655 | $cmd .= " -m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; | |
4e6112f9 DM |
656 | |
657 | if (my $action = $rule->{action}) { | |
658 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
659 | $cmd .= $goto ? " -g $action" : " -j $action"; | |
660 | }; | |
3a616aa0 | 661 | |
3fa83edf DM |
662 | ruleset_addrule($ruleset, $chain, $cmd) if $cmd; |
663 | } | |
664 | ||
665 | sub ruleset_create_chain { | |
666 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 667 | |
d050c724 DM |
668 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; |
669 | ||
3fa83edf DM |
670 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; |
671 | ||
672 | $ruleset->{$chain} = []; | |
3a616aa0 AD |
673 | } |
674 | ||
3fa83edf DM |
675 | sub ruleset_chain_exist { |
676 | my ($ruleset, $chain) = @_; | |
3a616aa0 | 677 | |
3fa83edf DM |
678 | return $ruleset->{$chain} ? 1 : undef; |
679 | } | |
3a616aa0 | 680 | |
3fa83edf DM |
681 | sub ruleset_addrule { |
682 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 683 | |
3fa83edf | 684 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 685 | |
3fa83edf DM |
686 | push @{$ruleset->{$chain}}, "-A $chain $rule"; |
687 | } | |
3a616aa0 | 688 | |
3fa83edf DM |
689 | sub ruleset_insertrule { |
690 | my ($ruleset, $chain, $rule) = @_; | |
3a616aa0 | 691 | |
3fa83edf | 692 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; |
3a616aa0 | 693 | |
3fa83edf DM |
694 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; |
695 | } | |
3a616aa0 | 696 | |
3fa83edf DM |
697 | sub generate_bridge_chains { |
698 | my ($ruleset, $bridge) = @_; | |
3a616aa0 | 699 | |
dec84fcd DM |
700 | if (!ruleset_chain_exist($ruleset, "PVEFW-FORWARD")){ |
701 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
7fab95bc | 702 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); |
3a616aa0 AD |
703 | } |
704 | ||
5927497d DM |
705 | if (!ruleset_chain_exist($ruleset, "$bridge-FW")) { |
706 | ruleset_create_chain($ruleset, "$bridge-FW"); | |
707 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW"); | |
708 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW"); | |
9fcad984 AD |
709 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing |
710 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing | |
3fa83edf | 711 | } |
3a616aa0 | 712 | |
3fa83edf DM |
713 | if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) { |
714 | ruleset_create_chain($ruleset, "$bridge-OUT"); | |
5927497d | 715 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT"); |
9fcad984 AD |
716 | } |
717 | ||
718 | if (!ruleset_chain_exist($ruleset, "$bridge-IN")) { | |
719 | ruleset_create_chain($ruleset, "$bridge-IN"); | |
5927497d | 720 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN"); |
fdb0bf20 | 721 | ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT"); |
3fa83edf DM |
722 | } |
723 | } | |
3a616aa0 AD |
724 | |
725 | sub generate_tap_rules_direction { | |
fa9c4a6f DM |
726 | my ($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_; |
727 | ||
728 | my $rules = $vmfw_conf->{lc($direction)}; | |
729 | my $options = $vmfw_conf->{options}; | |
3a616aa0 AD |
730 | |
731 | my $tapchain = "$iface-$direction"; | |
732 | ||
3fa83edf | 733 | ruleset_create_chain($ruleset, $tapchain); |
3a616aa0 | 734 | |
7fab95bc DM |
735 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP"); |
736 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
3a616aa0 | 737 | |
c29f55c9 DM |
738 | if ($direction eq 'OUT' && defined($macaddr)) { |
739 | ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP"); | |
740 | } | |
741 | ||
3fa83edf | 742 | if ($rules) { |
3a616aa0 AD |
743 | foreach my $rule (@$rules) { |
744 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
bfbfa8b6 | 745 | # we go to $bridge-IN if accept in out rules |
3a616aa0 | 746 | if($rule->{action} =~ m/^(GROUP-(\S+))$/){ |
3fa83edf DM |
747 | $rule->{action} .= "-$direction"; |
748 | # generate empty group rule if don't exist | |
749 | if(!ruleset_chain_exist($ruleset, $rule->{action})){ | |
51bae274 | 750 | generate_group_rules($ruleset, $group_rules, $2); |
3fa83edf | 751 | } |
4e6112f9 | 752 | ruleset_generate_rule($ruleset, $tapchain, $rule); |
7b291cab | 753 | ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -j RETURN") |
31ff3ef6 | 754 | if $direction eq 'OUT'; |
4e6112f9 | 755 | } else { |
fdb0bf20 | 756 | $rule->{action} = "PVEFW-SET-ACCEPT-MARK" if $rule->{action} eq 'ACCEPT' && $direction eq 'OUT'; |
4e6112f9 | 757 | ruleset_generate_rule($ruleset, $tapchain, $rule); |
3a616aa0 | 758 | } |
4e6112f9 | 759 | } |
3a616aa0 AD |
760 | } |
761 | ||
ccae0b50 DM |
762 | # implement policy |
763 | my $policy; | |
764 | ||
765 | if ($direction eq 'OUT') { | |
766 | $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default | |
767 | } else { | |
768 | $policy = $options->{'policy-in'} || 'DROP'; # allow everything by default | |
769 | } | |
770 | ||
771 | if ($policy eq 'ACCEPT') { | |
772 | if ($direction eq 'OUT') { | |
fdb0bf20 | 773 | ruleset_addrule($ruleset, $tapchain, "-g PVEFW-SET-ACCEPT-MARK"); |
ccae0b50 DM |
774 | } else { |
775 | ruleset_addrule($ruleset, $tapchain, "-j ACCEPT"); | |
776 | } | |
777 | } elsif ($policy eq 'DROP') { | |
778 | ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-dropped: \" --log-level 4"); | |
779 | ruleset_addrule($ruleset, $tapchain, "-j DROP"); | |
780 | } elsif ($policy eq 'REJECT') { | |
781 | ruleset_addrule($ruleset, $tapchain, "-j LOG --log-prefix \"$tapchain-reject: \" --log-level 4"); | |
782 | ruleset_addrule($ruleset, $tapchain, "-j REJECT"); | |
783 | } else { | |
784 | # should not happen | |
785 | die "internal error: unknown policy '$policy'"; | |
786 | } | |
3a616aa0 | 787 | |
3fa83edf DM |
788 | # plug the tap chain to bridge chain |
789 | my $physdevdirection = $direction eq 'IN' ? "out" : "in"; | |
790 | my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain"; | |
791 | ruleset_insertrule($ruleset, "$bridge-$direction", $rule); | |
3a616aa0 | 792 | |
3fa83edf DM |
793 | if ($direction eq 'OUT'){ |
794 | # add tap->host rules | |
795 | my $rule = "-m physdev --physdev-$physdevdirection $iface -j $tapchain"; | |
dec84fcd | 796 | ruleset_addrule($ruleset, "PVEFW-INPUT", $rule); |
3a616aa0 AD |
797 | } |
798 | } | |
799 | ||
0bd5f137 | 800 | sub enablehostfw { |
51bae274 | 801 | my ($ruleset, $rules, $group_rules) = @_; |
0bd5f137 | 802 | |
51bae274 | 803 | # fixme: allow security groups |
0bd5f137 | 804 | |
3fa83edf | 805 | # host inbound firewall |
dec84fcd DM |
806 | my $chain = "PVEFW-HOST-IN"; |
807 | ruleset_create_chain($ruleset, $chain); | |
0bd5f137 | 808 | |
7fab95bc DM |
809 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
810 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
811 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); |
812 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 813 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 814 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 815 | |
3fa83edf DM |
816 | if ($rules->{in}) { |
817 | foreach my $rule (@{$rules->{in}}) { | |
818 | # we use RETURN because we need to check also tap rules | |
819 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 820 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 821 | } |
0bd5f137 AD |
822 | } |
823 | ||
dec84fcd DM |
824 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-IN dropped: \" --log-level 4"); |
825 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
0bd5f137 | 826 | |
3fa83edf | 827 | # host outbound firewall |
aadd745e | 828 | $chain = "PVEFW-HOST-OUT"; |
dec84fcd DM |
829 | ruleset_create_chain($ruleset, $chain); |
830 | ||
7fab95bc DM |
831 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); |
832 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
dec84fcd DM |
833 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); |
834 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
7fab95bc | 835 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); |
dec84fcd | 836 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync |
0bd5f137 | 837 | |
3fa83edf DM |
838 | if ($rules->{out}) { |
839 | foreach my $rule (@{$rules->{out}}) { | |
840 | # we use RETURN because we need to check also tap rules | |
841 | $rule->{action} = 'RETURN' if $rule->{action} eq 'ACCEPT'; | |
dec84fcd | 842 | ruleset_generate_rule($ruleset, $chain, $rule); |
3fa83edf | 843 | } |
0bd5f137 AD |
844 | } |
845 | ||
dec84fcd DM |
846 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"kvmhost-OUT dropped: \" --log-level 4"); |
847 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
6158271d | 848 | |
dec84fcd DM |
849 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); |
850 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
9d31b418 AD |
851 | } |
852 | ||
853 | sub generate_group_rules { | |
51bae274 | 854 | my ($ruleset, $group_rules, $group) = @_; |
9d31b418 | 855 | |
51bae274 | 856 | my $rules = $group_rules->{$group}; |
9d31b418 | 857 | |
51bae274 | 858 | die "no such security group '$group'\n" if !$rules; |
6158271d | 859 | |
3fa83edf | 860 | my $chain = "GROUP-${group}-IN"; |
9d31b418 | 861 | |
3fa83edf | 862 | ruleset_create_chain($ruleset, $chain); |
9d31b418 | 863 | |
3fa83edf DM |
864 | if ($rules->{in}) { |
865 | foreach my $rule (@{$rules->{in}}) { | |
866 | ruleset_generate_rule($ruleset, $chain, $rule); | |
9d31b418 AD |
867 | } |
868 | } | |
869 | ||
3fa83edf | 870 | $chain = "GROUP-${group}-OUT"; |
9d31b418 | 871 | |
3fa83edf | 872 | ruleset_create_chain($ruleset, $chain); |
4e6112f9 | 873 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark |
9d31b418 | 874 | |
3fa83edf DM |
875 | if ($rules->{out}) { |
876 | foreach my $rule (@{$rules->{out}}) { | |
6158271d DM |
877 | # we go the PVEFW-SET-ACCEPT-MARK Instead of ACCEPT) because we need to |
878 | # check also other tap rules (and group rules can be set on any bridge, | |
4e6112f9 DM |
879 | # so we can't go to VMBRXX-IN) |
880 | $rule->{action} = 'PVEFW-SET-ACCEPT-MARK' if $rule->{action} eq 'ACCEPT'; | |
6a6c7af7 | 881 | ruleset_generate_rule($ruleset, $chain, $rule); |
9d31b418 AD |
882 | } |
883 | } | |
9d31b418 AD |
884 | } |
885 | ||
51bae274 DM |
886 | my $MAX_NETS = 32; |
887 | my $valid_netdev_names = {}; | |
888 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
889 | $valid_netdev_names->{"net$i"} = 1; | |
890 | } | |
5e1267a5 | 891 | |
51bae274 DM |
892 | sub parse_fw_rule { |
893 | my ($line, $need_iface, $allow_groups) = @_; | |
5e1267a5 | 894 | |
a332200b | 895 | my $macros = get_firewall_macros(); |
fcba0beb | 896 | my $protocols = get_etc_protocols(); |
51bae274 DM |
897 | |
898 | my ($action, $iface, $source, $dest, $proto, $dport, $sport); | |
899 | ||
900 | $line =~ s/#.*$//; | |
901 | ||
902 | my @data = split(/\s+/, $line); | |
903 | my $expected_elements = $need_iface ? 7 : 6; | |
904 | ||
905 | die "wrong number of rule elements\n" if scalar(@data) > $expected_elements; | |
906 | ||
907 | if ($need_iface) { | |
908 | ($action, $iface, $source, $dest, $proto, $dport, $sport) = @data | |
909 | } else { | |
910 | ($action, $source, $dest, $proto, $dport, $sport) = @data; | |
911 | } | |
912 | ||
913 | die "incomplete rule\n" if !$action; | |
914 | ||
961b4928 DM |
915 | my $macro; |
916 | my $macro_name; | |
917 | ||
51bae274 DM |
918 | if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) { |
919 | # OK | |
920 | } elsif ($allow_groups && $action =~ m/^GROUP-(:?\S+)$/) { | |
921 | # OK | |
922 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
961b4928 DM |
923 | ($macro_name, $action) = ($1, $2); |
924 | my $lc_macro_name = lc($macro_name); | |
925 | my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name}; | |
926 | $macro_name = $preferred_name if $preferred_name; | |
927 | $macro = $macros->{$lc_macro_name}; | |
928 | die "unknown macro '$macro_name'\n" if !$macro; | |
51bae274 DM |
929 | } else { |
930 | die "unknown action '$action'\n"; | |
931 | } | |
932 | ||
933 | if ($need_iface) { | |
934 | $iface = undef if $iface && $iface eq '-'; | |
6158271d | 935 | die "unknown interface '$iface'\n" |
51bae274 DM |
936 | if defined($iface) && !$valid_netdev_names->{$iface}; |
937 | } | |
938 | ||
939 | $proto = undef if $proto && $proto eq '-'; | |
6158271d | 940 | die "unknown protokol '$proto'\n" if $proto && |
51bae274 DM |
941 | !(defined($protocols->{byname}->{$proto}) || |
942 | defined($protocols->{byid}->{$proto})); | |
943 | ||
944 | $source = undef if $source && $source eq '-'; | |
945 | $dest = undef if $dest && $dest eq '-'; | |
946 | ||
947 | $dport = undef if $dport && $dport eq '-'; | |
948 | $sport = undef if $sport && $sport eq '-'; | |
949 | ||
51bae274 DM |
950 | my $nbsource = undef; |
951 | my $nbdest = undef; | |
952 | ||
953 | $nbsource = parse_address_list($source) if $source; | |
954 | $nbdest = parse_address_list($dest) if $dest; | |
6158271d | 955 | |
961b4928 | 956 | my $rules = []; |
51bae274 | 957 | |
961b4928 | 958 | my $param = { |
51bae274 | 959 | action => $action, |
51bae274 DM |
960 | iface => $iface, |
961 | source => $source, | |
962 | dest => $dest, | |
963 | nbsource => $nbsource, | |
964 | nbdest => $nbdest, | |
965 | proto => $proto, | |
966 | dport => $dport, | |
967 | sport => $sport, | |
51bae274 | 968 | }; |
961b4928 DM |
969 | |
970 | if ($macro) { | |
971 | foreach my $templ (@$macro) { | |
972 | my $rule = {}; | |
973 | foreach my $k (keys %$templ) { | |
974 | my $v = $templ->{$k}; | |
857f62c8 DM |
975 | if ($v eq 'PARAM') { |
976 | $v = $param->{$k}; | |
977 | } elsif ($v eq 'DEST') { | |
978 | $v = $param->{dest}; | |
979 | } elsif ($v eq 'SOURCE') { | |
980 | $v = $param->{source}; | |
981 | } | |
982 | ||
961b4928 DM |
983 | die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v); |
984 | $rule->{$k} = $v; | |
985 | } | |
986 | push @$rules, $rule; | |
987 | } | |
988 | } else { | |
989 | push @$rules, $param; | |
990 | } | |
991 | ||
992 | foreach my $rule (@$rules) { | |
6158271d | 993 | $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport}) |
961b4928 | 994 | if defined($rule->{dport}); |
6158271d | 995 | $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport}) |
961b4928 DM |
996 | if defined($rule->{sport}); |
997 | } | |
998 | ||
999 | return $rules; | |
51bae274 DM |
1000 | } |
1001 | ||
85c6eaed DM |
1002 | sub parse_fw_option { |
1003 | my ($line) = @_; | |
1004 | ||
1005 | my ($opt, $value); | |
1006 | ||
1007 | if ($line =~ m/^enable:\s*(0|1)\s*$/i) { | |
1008 | $opt = 'enable'; | |
1009 | $value = int($1); | |
1010 | } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
1011 | $opt = lc($1); | |
1012 | $value = uc($3); | |
1013 | } else { | |
1014 | chomp $line; | |
1015 | die "can't parse option '$line'\n" | |
1016 | } | |
1017 | ||
1018 | return ($opt, $value); | |
1019 | } | |
1020 | ||
51bae274 DM |
1021 | sub parse_vm_fw_rules { |
1022 | my ($filename, $fh) = @_; | |
1023 | ||
85c6eaed | 1024 | my $res = { in => [], out => [], options => {}}; |
51bae274 DM |
1025 | |
1026 | my $section; | |
1027 | ||
5e1267a5 DM |
1028 | while (defined(my $line = <$fh>)) { |
1029 | next if $line =~ m/^#/; | |
1030 | next if $line =~ m/^\s*$/; | |
1031 | ||
961b4928 DM |
1032 | my $linenr = $fh->input_line_number(); |
1033 | my $prefix = "$filename (line $linenr)"; | |
1034 | ||
85c6eaed | 1035 | if ($line =~ m/^\[(\S+)\]\s*$/i) { |
5e1267a5 | 1036 | $section = lc($1); |
85c6eaed | 1037 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; |
5e1267a5 DM |
1038 | next; |
1039 | } | |
51bae274 | 1040 | if (!$section) { |
961b4928 | 1041 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
1042 | next; |
1043 | } | |
1044 | ||
85c6eaed DM |
1045 | next if !$res->{$section}; # skip undefined section |
1046 | ||
1047 | if ($section eq 'options') { | |
6158271d DM |
1048 | eval { |
1049 | my ($opt, $value) = parse_fw_option($line); | |
85c6eaed DM |
1050 | $res->{options}->{$opt} = $value; |
1051 | }; | |
1052 | warn "$prefix: $@" if $@; | |
1053 | next; | |
1054 | } | |
1055 | ||
961b4928 DM |
1056 | my $rules; |
1057 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
51bae274 | 1058 | if (my $err = $@) { |
961b4928 | 1059 | warn "$prefix: $err"; |
5e1267a5 DM |
1060 | next; |
1061 | } | |
1062 | ||
961b4928 | 1063 | push @{$res->{$section}}, @$rules; |
51bae274 DM |
1064 | } |
1065 | ||
1066 | return $res; | |
1067 | } | |
1068 | ||
1069 | sub parse_host_fw_rules { | |
1070 | my ($filename, $fh) = @_; | |
1071 | ||
1072 | my $res = { in => [], out => [] }; | |
1073 | ||
1074 | my $section; | |
1075 | ||
1076 | while (defined(my $line = <$fh>)) { | |
1077 | next if $line =~ m/^#/; | |
1078 | next if $line =~ m/^\s*$/; | |
1079 | ||
961b4928 DM |
1080 | my $linenr = $fh->input_line_number(); |
1081 | my $prefix = "$filename (line $linenr)"; | |
1082 | ||
51bae274 DM |
1083 | if ($line =~ m/^\[(in|out)\]\s*$/i) { |
1084 | $section = lc($1); | |
5e1267a5 DM |
1085 | next; |
1086 | } | |
51bae274 | 1087 | if (!$section) { |
961b4928 | 1088 | warn "$prefix: skip line - no section"; |
5e1267a5 DM |
1089 | next; |
1090 | } | |
1091 | ||
961b4928 DM |
1092 | my $rules; |
1093 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
ecbea048 | 1094 | if (my $err = $@) { |
961b4928 | 1095 | warn "$prefix: $err"; |
ecbea048 | 1096 | next; |
ecbea048 DM |
1097 | } |
1098 | ||
961b4928 | 1099 | push @{$res->{$section}}, @$rules; |
51bae274 | 1100 | } |
ecbea048 | 1101 | |
51bae274 DM |
1102 | return $res; |
1103 | } | |
4cdbb3b7 | 1104 | |
51bae274 DM |
1105 | sub parse_group_fw_rules { |
1106 | my ($filename, $fh) = @_; | |
5e1267a5 | 1107 | |
51bae274 DM |
1108 | my $section; |
1109 | my $group; | |
1110 | ||
1111 | my $res = { in => [], out => [] }; | |
6158271d | 1112 | |
51bae274 DM |
1113 | while (defined(my $line = <$fh>)) { |
1114 | next if $line =~ m/^#/; | |
1115 | next if $line =~ m/^\s*$/; | |
1116 | ||
961b4928 DM |
1117 | my $linenr = $fh->input_line_number(); |
1118 | my $prefix = "$filename (line $linenr)"; | |
1119 | ||
51bae274 DM |
1120 | if ($line =~ m/^\[(in|out):(\S+)\]\s*$/i) { |
1121 | $section = lc($1); | |
1122 | $group = lc($2); | |
1123 | next; | |
1124 | } | |
1125 | if (!$section || !$group) { | |
961b4928 | 1126 | warn "$prefix: skip line - no section"; |
51bae274 DM |
1127 | next; |
1128 | } | |
1129 | ||
961b4928 DM |
1130 | my $rules; |
1131 | eval { $rules = parse_fw_rule($line, 0, 0); }; | |
51bae274 | 1132 | if (my $err = $@) { |
961b4928 | 1133 | warn "$prefix: $err"; |
51bae274 DM |
1134 | next; |
1135 | } | |
1136 | ||
961b4928 | 1137 | push @{$res->{$group}->{$section}}, @$rules; |
5e1267a5 DM |
1138 | } |
1139 | ||
1140 | return $res; | |
1141 | } | |
1142 | ||
06320eb0 DM |
1143 | sub run_locked { |
1144 | my ($code, @param) = @_; | |
1145 | ||
1146 | my $timeout = 10; | |
1147 | ||
1148 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
1149 | ||
1150 | die $@ if $@; | |
1151 | ||
1152 | return $res; | |
1153 | } | |
1154 | ||
5e1267a5 DM |
1155 | sub read_local_vm_config { |
1156 | ||
1157 | my $openvz = {}; | |
1158 | ||
1159 | my $qemu = {}; | |
1160 | ||
1161 | my $list = PVE::QemuServer::config_list(); | |
1162 | ||
1163 | foreach my $vmid (keys %$list) { | |
5e1267a5 DM |
1164 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); |
1165 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
1166 | $qemu->{$vmid} = $conf; | |
1167 | } | |
1168 | } | |
1169 | ||
1170 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
1171 | ||
1172 | return $vmdata; | |
1173 | }; | |
1174 | ||
1175 | sub read_vm_firewall_rules { | |
1176 | my ($vmdata) = @_; | |
1177 | my $rules = {}; | |
1178 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
1179 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
1180 | my $fh = IO::File->new($filename, O_RDONLY); | |
1181 | next if !$fh; | |
1182 | ||
51bae274 | 1183 | $rules->{$vmid} = parse_vm_fw_rules($filename, $fh); |
5e1267a5 DM |
1184 | } |
1185 | ||
1186 | return $rules; | |
1187 | } | |
1188 | ||
1189 | sub compile { | |
5e1267a5 DM |
1190 | my $vmdata = read_local_vm_config(); |
1191 | my $rules = read_vm_firewall_rules($vmdata); | |
6158271d | 1192 | |
51bae274 DM |
1193 | my $group_rules = {}; |
1194 | my $filename = "/etc/pve/firewall/groups.fw"; | |
1195 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1196 | $group_rules = parse_group_fw_rules($filename, $fh); | |
1197 | } | |
5e1267a5 | 1198 | |
3fa83edf DM |
1199 | #print Dumper($rules); |
1200 | ||
1201 | my $ruleset = {}; | |
1202 | ||
dec84fcd DM |
1203 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); |
1204 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
fadb13dd | 1205 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); |
3fa83edf | 1206 | |
4e6112f9 DM |
1207 | ruleset_create_chain($ruleset, "PVEFW-SET-ACCEPT-MARK"); |
1208 | ruleset_addrule($ruleset, "PVEFW-SET-ACCEPT-MARK", "-j MARK --set-mark 1"); | |
1209 | ||
fadb13dd | 1210 | my $enable_hostfw = 0; |
51bae274 DM |
1211 | $filename = "/etc/pve/local/host.fw"; |
1212 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1213 | my $host_rules = parse_host_fw_rules($filename, $fh); | |
fadb13dd DM |
1214 | |
1215 | $enable_hostfw = 1; | |
1216 | ||
51bae274 DM |
1217 | enablehostfw($ruleset, $host_rules, $group_rules); |
1218 | } | |
3fa83edf | 1219 | |
6158271d | 1220 | # generate firewall rules for QEMU VMs |
3fa83edf DM |
1221 | foreach my $vmid (keys %{$vmdata->{qemu}}) { |
1222 | my $conf = $vmdata->{qemu}->{$vmid}; | |
fa9c4a6f DM |
1223 | my $vmfw_conf = $rules->{$vmid}; |
1224 | next if !$vmfw_conf; | |
1225 | next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); | |
3fa83edf DM |
1226 | |
1227 | foreach my $netid (keys %$conf) { | |
1228 | next if $netid !~ m/^net(\d+)$/; | |
1229 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
1230 | next if !$net; | |
1231 | my $iface = "tap${vmid}i$1"; | |
5e1267a5 | 1232 | |
3fa83edf DM |
1233 | my $bridge = $net->{bridge}; |
1234 | next if !$bridge; # fixme: ? | |
1235 | ||
1236 | $bridge .= "v$net->{tag}" if $net->{tag}; | |
1237 | ||
fadb13dd | 1238 | |
3fa83edf DM |
1239 | generate_bridge_chains($ruleset, $bridge); |
1240 | ||
c29f55c9 | 1241 | my $macaddr = $net->{macaddr}; |
fa9c4a6f DM |
1242 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN'); |
1243 | generate_tap_rules_direction($ruleset, $group_rules, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT'); | |
3fa83edf DM |
1244 | } |
1245 | } | |
c0413e35 | 1246 | |
fadb13dd DM |
1247 | if ($enable_hostfw) { |
1248 | # allow traffic from lo (ourself) | |
1249 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i lo -j ACCEPT"); | |
1250 | } | |
c0413e35 | 1251 | |
3fa83edf DM |
1252 | return $ruleset; |
1253 | } | |
1254 | ||
1255 | sub get_ruleset_status { | |
1256 | my ($ruleset, $verbose) = @_; | |
1257 | ||
1258 | my $active_chains = iptables_get_chains(); | |
1259 | ||
1260 | my $statushash = {}; | |
1261 | ||
1262 | foreach my $chain (sort keys %$ruleset) { | |
09d5f68e | 1263 | my $digest = Digest::SHA->new('sha1'); |
3fa83edf DM |
1264 | foreach my $cmd (@{$ruleset->{$chain}}) { |
1265 | $digest->add("$cmd\n"); | |
1266 | } | |
1267 | my $sig = $digest->b64digest; | |
1268 | $statushash->{$chain}->{sig} = $sig; | |
1269 | ||
1270 | my $oldsig = $active_chains->{$chain}; | |
1271 | if (!defined($oldsig)) { | |
1272 | $statushash->{$chain}->{action} = 'create'; | |
1273 | } else { | |
1274 | if ($oldsig eq $sig) { | |
1275 | $statushash->{$chain}->{action} = 'exists'; | |
1276 | } else { | |
1277 | $statushash->{$chain}->{action} = 'update'; | |
1278 | } | |
1279 | } | |
1280 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
1281 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1282 | print "\t$cmd\n" if $verbose; | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | foreach my $chain (sort keys %$active_chains) { | |
1287 | if (!defined($ruleset->{$chain})) { | |
1288 | my $sig = $active_chains->{$chain}; | |
1289 | $statushash->{$chain}->{action} = 'delete'; | |
1290 | $statushash->{$chain}->{sig} = $sig; | |
1291 | print "delete $chain ($sig)\n" if $verbose; | |
1292 | } | |
6158271d | 1293 | } |
3fa83edf DM |
1294 | |
1295 | return $statushash; | |
1296 | } | |
1297 | ||
1298 | sub print_ruleset { | |
1299 | my ($ruleset) = @_; | |
1300 | ||
1301 | get_ruleset_status($ruleset, 1); | |
1302 | } | |
1303 | ||
1304 | sub print_sig_rule { | |
1305 | my ($chain, $sig) = @_; | |
1306 | ||
09d5f68e DM |
1307 | # We just use this to store a SHA1 checksum used to detect changes |
1308 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
b6360c3f DM |
1309 | } |
1310 | ||
a84f4d96 DM |
1311 | sub apply_ruleset { |
1312 | my ($ruleset, $verbose) = @_; | |
3fa83edf | 1313 | |
a84f4d96 | 1314 | enable_bridge_firewall(); |
3fa83edf DM |
1315 | |
1316 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
1317 | ||
1318 | my $statushash = get_ruleset_status($ruleset, $verbose); | |
1319 | ||
1320 | # create missing chains first | |
1321 | foreach my $chain (sort keys %$ruleset) { | |
1322 | my $stat = $statushash->{$chain}; | |
1323 | die "internal error" if !$stat; | |
1324 | next if $stat->{action} ne 'create'; | |
886aba9c | 1325 | |
3fa83edf DM |
1326 | $cmdlist .= ":$chain - [0:0]\n"; |
1327 | } | |
1328 | ||
dec84fcd | 1329 | my $rule = "INPUT -j PVEFW-INPUT"; |
3fa83edf DM |
1330 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1331 | $cmdlist .= "-A $rule\n"; | |
1332 | } | |
dec84fcd | 1333 | $rule = "OUTPUT -j PVEFW-OUTPUT"; |
3fa83edf DM |
1334 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1335 | $cmdlist .= "-A $rule\n"; | |
1336 | } | |
1337 | ||
dec84fcd | 1338 | $rule = "FORWARD -j PVEFW-FORWARD"; |
3fa83edf DM |
1339 | if (!PVE::Firewall::iptables_rule_exist($rule)) { |
1340 | $cmdlist .= "-A $rule\n"; | |
1341 | } | |
1342 | ||
1343 | foreach my $chain (sort keys %$ruleset) { | |
1344 | my $stat = $statushash->{$chain}; | |
1345 | die "internal error" if !$stat; | |
1346 | ||
1347 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
1348 | $cmdlist .= "-F $chain\n"; | |
1349 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1350 | $cmdlist .= "$cmd\n"; | |
1351 | } | |
1352 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
1353 | } elsif ($stat->{action} eq 'delete') { | |
f5d28682 | 1354 | die "internal error"; # this should not happen |
3fa83edf DM |
1355 | } elsif ($stat->{action} eq 'exists') { |
1356 | # do nothing | |
1357 | } else { | |
1358 | die "internal error - unknown status '$stat->{action}'"; | |
1359 | } | |
1360 | } | |
1361 | ||
f5d28682 DM |
1362 | foreach my $chain (keys %$statushash) { |
1363 | next if $statushash->{$chain}->{action} ne 'delete'; | |
1364 | $cmdlist .= "-F $chain\n"; | |
1365 | } | |
1366 | foreach my $chain (keys %$statushash) { | |
1367 | next if $statushash->{$chain}->{action} ne 'delete'; | |
fadb13dd DM |
1368 | next if $chain eq 'PVEFW-INPUT'; |
1369 | next if $chain eq 'PVEFW-OUTPUT'; | |
1370 | next if $chain eq 'PVEFW-FORWARD'; | |
f5d28682 DM |
1371 | $cmdlist .= "-X $chain\n"; |
1372 | } | |
1373 | ||
3fa83edf DM |
1374 | $cmdlist .= "COMMIT\n"; |
1375 | ||
1376 | print $cmdlist if $verbose; | |
1377 | ||
1378 | iptables_restore_cmdlist($cmdlist); | |
1379 | ||
6158271d | 1380 | # test: re-read status and check if everything is up to date |
3fa83edf DM |
1381 | $statushash = get_ruleset_status($ruleset); |
1382 | ||
1383 | my $errors; | |
1384 | foreach my $chain (sort keys %$ruleset) { | |
1385 | my $stat = $statushash->{$chain}; | |
1386 | if ($stat->{action} ne 'exists') { | |
1387 | warn "unable to update chain '$chain'\n"; | |
1388 | $errors = 1; | |
1389 | } | |
1390 | } | |
b6360c3f | 1391 | |
3fa83edf | 1392 | die "unable to apply firewall changes\n" if $errors; |
b6360c3f DM |
1393 | } |
1394 | ||
b6360c3f | 1395 | 1; |