]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Firewall; | |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | use Data::Dumper; | |
6 | use Digest::SHA; | |
7 | use PVE::ProcFSTools; | |
8 | use PVE::Tools; | |
9 | use PVE::QemuServer; | |
10 | use File::Basename; | |
11 | use File::Path; | |
12 | use IO::File; | |
13 | use Net::IP; | |
14 | use PVE::Tools qw(run_command lock_file); | |
15 | ||
16 | use Data::Dumper; | |
17 | ||
18 | my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; | |
19 | my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status"; | |
20 | ||
21 | my $default_log_level = 'info'; | |
22 | ||
23 | my $log_level_hash = { | |
24 | debug => 7, | |
25 | info => 6, | |
26 | notice => 5, | |
27 | warning => 4, | |
28 | err => 3, | |
29 | crit => 2, | |
30 | alert => 1, | |
31 | emerg => 0, | |
32 | }; | |
33 | ||
34 | # imported/converted from: /usr/share/shorewall/macro.* | |
35 | my $pve_fw_macros = { | |
36 | 'Amanda' => [ | |
37 | { action => 'PARAM', proto => 'udp', dport => '10080' }, | |
38 | { action => 'PARAM', proto => 'tcp', dport => '10080' }, | |
39 | ], | |
40 | 'Auth' => [ | |
41 | { action => 'PARAM', proto => 'tcp', dport => '113' }, | |
42 | ], | |
43 | 'BGP' => [ | |
44 | { action => 'PARAM', proto => 'tcp', dport => '179' }, | |
45 | ], | |
46 | 'BitTorrent' => [ | |
47 | { action => 'PARAM', proto => 'tcp', dport => '6881:6889' }, | |
48 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
49 | ], | |
50 | 'BitTorrent32' => [ | |
51 | { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, | |
52 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
53 | ], | |
54 | 'CVS' => [ | |
55 | { action => 'PARAM', proto => 'tcp', dport => '2401' }, | |
56 | ], | |
57 | 'Citrix' => [ | |
58 | { action => 'PARAM', proto => 'tcp', dport => '1494' }, | |
59 | { action => 'PARAM', proto => 'udp', dport => '1604' }, | |
60 | { action => 'PARAM', proto => 'tcp', dport => '2598' }, | |
61 | ], | |
62 | 'DAAP' => [ | |
63 | { action => 'PARAM', proto => 'tcp', dport => '3689' }, | |
64 | { action => 'PARAM', proto => 'udp', dport => '3689' }, | |
65 | ], | |
66 | 'DCC' => [ | |
67 | { action => 'PARAM', proto => 'tcp', dport => '6277' }, | |
68 | ], | |
69 | 'DHCPfwd' => [ | |
70 | { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
71 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
72 | ], | |
73 | 'DNS' => [ | |
74 | { action => 'PARAM', proto => 'udp', dport => '53' }, | |
75 | { action => 'PARAM', proto => 'tcp', dport => '53' }, | |
76 | ], | |
77 | 'Distcc' => [ | |
78 | { action => 'PARAM', proto => 'tcp', dport => '3632' }, | |
79 | ], | |
80 | 'Edonkey' => [ | |
81 | { action => 'PARAM', proto => 'tcp', dport => '4662' }, | |
82 | { action => 'PARAM', proto => 'udp', dport => '4665' }, | |
83 | ], | |
84 | 'FTP' => [ | |
85 | { action => 'PARAM', proto => 'tcp', dport => '21' }, | |
86 | ], | |
87 | 'Finger' => [ | |
88 | { action => 'PARAM', proto => 'tcp', dport => '79' }, | |
89 | ], | |
90 | 'GNUnet' => [ | |
91 | { action => 'PARAM', proto => 'tcp', dport => '2086' }, | |
92 | { action => 'PARAM', proto => 'udp', dport => '2086' }, | |
93 | { action => 'PARAM', proto => 'tcp', dport => '1080' }, | |
94 | { action => 'PARAM', proto => 'udp', dport => '1080' }, | |
95 | ], | |
96 | 'GRE' => [ | |
97 | { action => 'PARAM', proto => '47' }, | |
98 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
99 | ], | |
100 | 'Git' => [ | |
101 | { action => 'PARAM', proto => 'tcp', dport => '9418' }, | |
102 | ], | |
103 | 'Gnutella' => [ | |
104 | { action => 'PARAM', proto => 'tcp', dport => '6346' }, | |
105 | { action => 'PARAM', proto => 'udp', dport => '6346' }, | |
106 | ], | |
107 | 'HKP' => [ | |
108 | { action => 'PARAM', proto => 'tcp', dport => '11371' }, | |
109 | ], | |
110 | 'HTTP' => [ | |
111 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
112 | ], | |
113 | 'HTTPS' => [ | |
114 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
115 | ], | |
116 | 'ICPV2' => [ | |
117 | { action => 'PARAM', proto => 'udp', dport => '3130' }, | |
118 | ], | |
119 | 'ICQ' => [ | |
120 | { action => 'PARAM', proto => 'tcp', dport => '5190' }, | |
121 | ], | |
122 | 'IMAP' => [ | |
123 | { action => 'PARAM', proto => 'tcp', dport => '143' }, | |
124 | ], | |
125 | 'IMAPS' => [ | |
126 | { action => 'PARAM', proto => 'tcp', dport => '993' }, | |
127 | ], | |
128 | 'IPIP' => [ | |
129 | { action => 'PARAM', proto => '94' }, | |
130 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' }, | |
131 | ], | |
132 | 'IPP' => [ | |
133 | { action => 'PARAM', proto => 'tcp', dport => '631' }, | |
134 | ], | |
135 | 'IPPbrd' => [ | |
136 | { action => 'PARAM', proto => 'udp', dport => '631' }, | |
137 | ], | |
138 | 'IPPserver' => [ | |
139 | { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' }, | |
140 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' }, | |
141 | ], | |
142 | 'IPsec' => [ | |
143 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
144 | { action => 'PARAM', proto => '50' }, | |
145 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
146 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
147 | ], | |
148 | 'IPsecah' => [ | |
149 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
150 | { action => 'PARAM', proto => '51' }, | |
151 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
152 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' }, | |
153 | ], | |
154 | 'IPsecnat' => [ | |
155 | { action => 'PARAM', proto => 'udp', dport => '500' }, | |
156 | { action => 'PARAM', proto => 'udp', dport => '4500' }, | |
157 | { action => 'PARAM', proto => '50' }, | |
158 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' }, | |
159 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' }, | |
160 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
161 | ], | |
162 | 'IRC' => [ | |
163 | { action => 'PARAM', proto => 'tcp', dport => '6667' }, | |
164 | ], | |
165 | 'JabberPlain' => [ | |
166 | { action => 'PARAM', proto => 'tcp', dport => '5222' }, | |
167 | ], | |
168 | 'JabberSecure' => [ | |
169 | { action => 'PARAM', proto => 'tcp', dport => '5223' }, | |
170 | ], | |
171 | 'Jabberd' => [ | |
172 | { action => 'PARAM', proto => 'tcp', dport => '5269' }, | |
173 | ], | |
174 | 'Jetdirect' => [ | |
175 | { action => 'PARAM', proto => 'tcp', dport => '9100' }, | |
176 | ], | |
177 | 'L2TP' => [ | |
178 | { action => 'PARAM', proto => 'udp', dport => '1701' }, | |
179 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' }, | |
180 | ], | |
181 | 'LDAP' => [ | |
182 | { action => 'PARAM', proto => 'tcp', dport => '389' }, | |
183 | ], | |
184 | 'LDAPS' => [ | |
185 | { action => 'PARAM', proto => 'tcp', dport => '636' }, | |
186 | ], | |
187 | 'MSNP' => [ | |
188 | { action => 'PARAM', proto => 'tcp', dport => '1863' }, | |
189 | ], | |
190 | 'MSSQL' => [ | |
191 | { action => 'PARAM', proto => 'tcp', dport => '1433' }, | |
192 | ], | |
193 | 'Mail' => [ | |
194 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
195 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
196 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
197 | ], | |
198 | 'Munin' => [ | |
199 | { action => 'PARAM', proto => 'tcp', dport => '4949' }, | |
200 | ], | |
201 | 'MySQL' => [ | |
202 | { action => 'PARAM', proto => 'tcp', dport => '3306' }, | |
203 | ], | |
204 | 'NNTP' => [ | |
205 | { action => 'PARAM', proto => 'tcp', dport => '119' }, | |
206 | ], | |
207 | 'NNTPS' => [ | |
208 | { action => 'PARAM', proto => 'tcp', dport => '563' }, | |
209 | ], | |
210 | 'NTP' => [ | |
211 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
212 | ], | |
213 | 'NTPbi' => [ | |
214 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
215 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' }, | |
216 | ], | |
217 | 'NTPbrd' => [ | |
218 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
219 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' }, | |
220 | ], | |
221 | 'OSPF' => [ | |
222 | { action => 'PARAM', proto => '89' }, | |
223 | ], | |
224 | 'OpenVPN' => [ | |
225 | { action => 'PARAM', proto => 'udp', dport => '1194' }, | |
226 | ], | |
227 | 'PCA' => [ | |
228 | { action => 'PARAM', proto => 'udp', dport => '5632' }, | |
229 | { action => 'PARAM', proto => 'tcp', dport => '5631' }, | |
230 | ], | |
231 | 'POP3' => [ | |
232 | { action => 'PARAM', proto => 'tcp', dport => '110' }, | |
233 | ], | |
234 | 'POP3S' => [ | |
235 | { action => 'PARAM', proto => 'tcp', dport => '995' }, | |
236 | ], | |
237 | 'PPtP' => [ | |
238 | { action => 'PARAM', proto => '47' }, | |
239 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
240 | { action => 'PARAM', proto => 'tcp', dport => '1723' }, | |
241 | ], | |
242 | 'Ping' => [ | |
243 | { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, | |
244 | ], | |
245 | 'PostgreSQL' => [ | |
246 | { action => 'PARAM', proto => 'tcp', dport => '5432' }, | |
247 | ], | |
248 | 'Printer' => [ | |
249 | { action => 'PARAM', proto => 'tcp', dport => '515' }, | |
250 | ], | |
251 | 'RDP' => [ | |
252 | { action => 'PARAM', proto => 'tcp', dport => '3389' }, | |
253 | ], | |
254 | 'RIPbi' => [ | |
255 | { action => 'PARAM', proto => 'udp', dport => '520' }, | |
256 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' }, | |
257 | ], | |
258 | 'RNDC' => [ | |
259 | { action => 'PARAM', proto => 'tcp', dport => '953' }, | |
260 | ], | |
261 | 'Razor' => [ | |
262 | { action => 'ACCEPT', proto => 'tcp', dport => '2703' }, | |
263 | ], | |
264 | 'Rdate' => [ | |
265 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
266 | ], | |
267 | 'Rsync' => [ | |
268 | { action => 'PARAM', proto => 'tcp', dport => '873' }, | |
269 | ], | |
270 | 'SANE' => [ | |
271 | { action => 'PARAM', proto => 'tcp', dport => '6566' }, | |
272 | ], | |
273 | 'SMB' => [ | |
274 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
275 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
276 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
277 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
278 | ], | |
279 | 'SMBBI' => [ | |
280 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
281 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
282 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
283 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
284 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' }, | |
285 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' }, | |
286 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
287 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' }, | |
288 | ], | |
289 | 'SMBswat' => [ | |
290 | { action => 'PARAM', proto => 'tcp', dport => '901' }, | |
291 | ], | |
292 | 'SMTP' => [ | |
293 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
294 | ], | |
295 | 'SMTPS' => [ | |
296 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
297 | ], | |
298 | 'SNMP' => [ | |
299 | { action => 'PARAM', proto => 'udp', dport => '161:162' }, | |
300 | { action => 'PARAM', proto => 'tcp', dport => '161' }, | |
301 | ], | |
302 | 'SPAMD' => [ | |
303 | { action => 'PARAM', proto => 'tcp', dport => '783' }, | |
304 | ], | |
305 | 'SSH' => [ | |
306 | { action => 'PARAM', proto => 'tcp', dport => '22' }, | |
307 | ], | |
308 | 'SVN' => [ | |
309 | { action => 'PARAM', proto => 'tcp', dport => '3690' }, | |
310 | ], | |
311 | 'SixXS' => [ | |
312 | { action => 'PARAM', proto => 'tcp', dport => '3874' }, | |
313 | { action => 'PARAM', proto => 'udp', dport => '3740' }, | |
314 | { action => 'PARAM', proto => '41' }, | |
315 | { action => 'PARAM', proto => 'udp', dport => '5072,8374' }, | |
316 | ], | |
317 | 'Squid' => [ | |
318 | { action => 'PARAM', proto => 'tcp', dport => '3128' }, | |
319 | ], | |
320 | 'Submission' => [ | |
321 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
322 | ], | |
323 | 'Syslog' => [ | |
324 | { action => 'PARAM', proto => 'udp', dport => '514' }, | |
325 | { action => 'PARAM', proto => 'tcp', dport => '514' }, | |
326 | ], | |
327 | 'TFTP' => [ | |
328 | { action => 'PARAM', proto => 'udp', dport => '69' }, | |
329 | ], | |
330 | 'Telnet' => [ | |
331 | { action => 'PARAM', proto => 'tcp', dport => '23' }, | |
332 | ], | |
333 | 'Telnets' => [ | |
334 | { action => 'PARAM', proto => 'tcp', dport => '992' }, | |
335 | ], | |
336 | 'Time' => [ | |
337 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
338 | ], | |
339 | 'Trcrt' => [ | |
340 | { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, | |
341 | { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, | |
342 | ], | |
343 | 'VNC' => [ | |
344 | { action => 'PARAM', proto => 'tcp', dport => '5900:5909' }, | |
345 | ], | |
346 | 'VNCL' => [ | |
347 | { action => 'PARAM', proto => 'tcp', dport => '5500' }, | |
348 | ], | |
349 | 'Web' => [ | |
350 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
351 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
352 | ], | |
353 | 'Webcache' => [ | |
354 | { action => 'PARAM', proto => 'tcp', dport => '8080' }, | |
355 | ], | |
356 | 'Webmin' => [ | |
357 | { action => 'PARAM', proto => 'tcp', dport => '10000' }, | |
358 | ], | |
359 | 'Whois' => [ | |
360 | { action => 'PARAM', proto => 'tcp', dport => '43' }, | |
361 | ], | |
362 | }; | |
363 | ||
364 | my $pve_fw_parsed_macros; | |
365 | my $pve_fw_preferred_macro_names = {}; | |
366 | ||
367 | my $pve_std_chains = { | |
368 | 'PVEFW-SET-ACCEPT-MARK' => [ | |
369 | "-j MARK --set-mark 1", | |
370 | ], | |
371 | 'PVEFW-DropBroadcast' => [ | |
372 | # same as shorewall 'Broadcast' | |
373 | # simply DROP BROADCAST/MULTICAST/ANYCAST | |
374 | # we can use this to reduce logging | |
375 | { action => 'DROP', dsttype => 'BROADCAST' }, | |
376 | { action => 'DROP', dsttype => 'MULTICAST' }, | |
377 | { action => 'DROP', dsttype => 'ANYCAST' }, | |
378 | { action => 'DROP', dest => '224.0.0.0/4' }, | |
379 | ], | |
380 | 'PVEFW-reject' => [ | |
381 | # same as shorewall 'reject' | |
382 | { action => 'DROP', dsttype => 'BROADCAST' }, | |
383 | { action => 'DROP', source => '224.0.0.0/4' }, | |
384 | { action => 'DROP', proto => 'icmp' }, | |
385 | "-p tcp -j REJECT --reject-with tcp-reset", | |
386 | "-p udp -j REJECT --reject-with icmp-port-unreachable", | |
387 | "-p icmp -j REJECT --reject-with icmp-host-unreachable", | |
388 | "-j REJECT --reject-with icmp-host-prohibited", | |
389 | ], | |
390 | 'PVEFW-Drop' => [ | |
391 | # same as shorewall 'Drop', which is equal to DROP, | |
392 | # but REJECT/DROP some packages to reduce logging, | |
393 | # and ACCEPT critical ICMP types | |
394 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
395 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
396 | { action => 'PVEFW-DropBroadcast' }, | |
397 | # ACCEPT critical ICMP types | |
398 | { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, | |
399 | { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, | |
400 | # Drop packets with INVALID state | |
401 | "-m conntrack --ctstate INVALID -j DROP", | |
402 | # Drop Microsoft SMB noise | |
403 | { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
404 | { action => 'DROP', proto => 'udp', dport => '137:139'}, | |
405 | { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
406 | { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
407 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
408 | # Drop new/NotSyn traffic so that it doesn't get logged | |
409 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
410 | # Drop DNS replies | |
411 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
412 | ], | |
413 | 'PVEFW-Reject' => [ | |
414 | # same as shorewall 'Reject', which is equal to Reject, | |
415 | # but REJECT/DROP some packages to reduce logging, | |
416 | # and ACCEPT critical ICMP types | |
417 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
418 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
419 | { action => 'PVEFW-DropBroadcast' }, | |
420 | # ACCEPT critical ICMP types | |
421 | { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, | |
422 | { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, | |
423 | # Drop packets with INVALID state | |
424 | "-m conntrack --ctstate INVALID -j DROP", | |
425 | # Drop Microsoft SMB noise | |
426 | { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
427 | { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, | |
428 | { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
429 | { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
430 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
431 | # Drop new/NotSyn traffic so that it doesn't get logged | |
432 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
433 | # Drop DNS replies | |
434 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
435 | ], | |
436 | 'PVEFW-tcpflags' => [ | |
437 | # same as shorewall tcpflags action. | |
438 | # Packets arriving on this interface are checked for som illegal combinations of TCP flags | |
439 | "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags", | |
440 | "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags", | |
441 | "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags", | |
442 | "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags", | |
443 | "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags", | |
444 | ], | |
445 | 'PVEFW-smurfs' => [ | |
446 | # same as shorewall smurfs action | |
447 | # Filter packets for smurfs (packets with a broadcast address as the source). | |
448 | "-s 0.0.0.0/32 -j RETURN", | |
449 | "-m addrtype --src-type BROADCAST -g PVEFW-smurflog", | |
450 | "-s 224.0.0.0/4 -g PVEFW-smurflog", | |
451 | ], | |
452 | }; | |
453 | ||
454 | # iptables -p icmp -h | |
455 | my $icmp_type_names = { | |
456 | any => 1, | |
457 | 'echo-reply' => 1, | |
458 | 'destination-unreachable' => 1, | |
459 | 'network-unreachable' => 1, | |
460 | 'host-unreachable' => 1, | |
461 | 'protocol-unreachable' => 1, | |
462 | 'port-unreachable' => 1, | |
463 | 'fragmentation-needed' => 1, | |
464 | 'source-route-failed' => 1, | |
465 | 'network-unknown' => 1, | |
466 | 'host-unknown' => 1, | |
467 | 'network-prohibited' => 1, | |
468 | 'host-prohibited' => 1, | |
469 | 'TOS-network-unreachable' => 1, | |
470 | 'TOS-host-unreachable' => 1, | |
471 | 'communication-prohibited' => 1, | |
472 | 'host-precedence-violation' => 1, | |
473 | 'precedence-cutoff' => 1, | |
474 | 'source-quench' => 1, | |
475 | 'redirect' => 1, | |
476 | 'network-redirect' => 1, | |
477 | 'host-redirect' => 1, | |
478 | 'TOS-network-redirect' => 1, | |
479 | 'TOS-host-redirect' => 1, | |
480 | 'echo-request' => 1, | |
481 | 'router-advertisement' => 1, | |
482 | 'router-solicitation' => 1, | |
483 | 'time-exceeded' => 1, | |
484 | 'ttl-zero-during-transit' => 1, | |
485 | 'ttl-zero-during-reassembly' => 1, | |
486 | 'parameter-problem' => 1, | |
487 | 'ip-header-bad' => 1, | |
488 | 'required-option-missing' => 1, | |
489 | 'timestamp-request' => 1, | |
490 | 'timestamp-reply' => 1, | |
491 | 'address-mask-request' => 1, | |
492 | 'address-mask-reply' => 1, | |
493 | }; | |
494 | ||
495 | sub get_firewall_macros { | |
496 | ||
497 | return $pve_fw_parsed_macros if $pve_fw_parsed_macros; | |
498 | ||
499 | $pve_fw_parsed_macros = {}; | |
500 | ||
501 | foreach my $k (keys %$pve_fw_macros) { | |
502 | my $name = lc($k); | |
503 | ||
504 | my $macro = $pve_fw_macros->{$k}; | |
505 | $pve_fw_preferred_macro_names->{$name} = $k; | |
506 | $pve_fw_parsed_macros->{$name} = $macro; | |
507 | } | |
508 | ||
509 | return $pve_fw_parsed_macros; | |
510 | } | |
511 | ||
512 | my $etc_services; | |
513 | ||
514 | sub get_etc_services { | |
515 | ||
516 | return $etc_services if $etc_services; | |
517 | ||
518 | my $filename = "/etc/services"; | |
519 | ||
520 | my $fh = IO::File->new($filename, O_RDONLY); | |
521 | if (!$fh) { | |
522 | warn "unable to read '$filename' - $!\n"; | |
523 | return {}; | |
524 | } | |
525 | ||
526 | my $services = {}; | |
527 | ||
528 | while (my $line = <$fh>) { | |
529 | chomp ($line); | |
530 | next if $line =~m/^#/; | |
531 | next if ($line =~m/^\s*$/); | |
532 | ||
533 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
534 | $services->{byid}->{$2}->{name} = $1; | |
535 | $services->{byid}->{$2}->{port} = $2; | |
536 | $services->{byid}->{$2}->{$3} = 1; | |
537 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
538 | } | |
539 | } | |
540 | ||
541 | close($fh); | |
542 | ||
543 | $etc_services = $services; | |
544 | ||
545 | ||
546 | return $etc_services; | |
547 | } | |
548 | ||
549 | my $etc_protocols; | |
550 | ||
551 | sub get_etc_protocols { | |
552 | return $etc_protocols if $etc_protocols; | |
553 | ||
554 | my $filename = "/etc/protocols"; | |
555 | ||
556 | my $fh = IO::File->new($filename, O_RDONLY); | |
557 | if (!$fh) { | |
558 | warn "unable to read '$filename' - $!\n"; | |
559 | return {}; | |
560 | } | |
561 | ||
562 | my $protocols = {}; | |
563 | ||
564 | while (my $line = <$fh>) { | |
565 | chomp ($line); | |
566 | next if $line =~m/^#/; | |
567 | next if ($line =~m/^\s*$/); | |
568 | ||
569 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
570 | $protocols->{byid}->{$2}->{name} = $1; | |
571 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
572 | } | |
573 | } | |
574 | ||
575 | close($fh); | |
576 | ||
577 | $etc_protocols = $protocols; | |
578 | ||
579 | return $etc_protocols; | |
580 | } | |
581 | ||
582 | sub parse_address_list { | |
583 | my ($str) = @_; | |
584 | ||
585 | my $nbaor = 0; | |
586 | foreach my $aor (split(/,/, $str)) { | |
587 | if (!Net::IP->new($aor)) { | |
588 | my $err = Net::IP::Error(); | |
589 | die "invalid IP address: $err\n"; | |
590 | }else{ | |
591 | $nbaor++; | |
592 | } | |
593 | } | |
594 | return $nbaor; | |
595 | } | |
596 | ||
597 | sub parse_port_name_number_or_range { | |
598 | my ($str) = @_; | |
599 | ||
600 | my $services = PVE::Firewall::get_etc_services(); | |
601 | my $nbports = 0; | |
602 | foreach my $item (split(/,/, $str)) { | |
603 | my $portlist = ""; | |
604 | my $oldpon = undef; | |
605 | $nbports++; | |
606 | foreach my $pon (split(':', $item, 2)) { | |
607 | $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port}; | |
608 | if ($pon =~ m/^\d+$/){ | |
609 | die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535; | |
610 | die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon); | |
611 | $oldpon = $pon; | |
612 | }else{ | |
613 | die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; | |
614 | } | |
615 | } | |
616 | } | |
617 | ||
618 | return ($nbports); | |
619 | } | |
620 | ||
621 | my $bridge_firewall_enabled = 0; | |
622 | ||
623 | sub enable_bridge_firewall { | |
624 | ||
625 | return if $bridge_firewall_enabled; # only once | |
626 | ||
627 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1"); | |
628 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1"); | |
629 | ||
630 | # make sure syncookies are enabled (which is default on newer 3.X kernels anyways) | |
631 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1"); | |
632 | ||
633 | $bridge_firewall_enabled = 1; | |
634 | } | |
635 | ||
636 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; | |
637 | ||
638 | sub iptables { | |
639 | my ($cmd) = @_; | |
640 | ||
641 | run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); | |
642 | } | |
643 | ||
644 | sub iptables_restore_cmdlist { | |
645 | my ($cmdlist) = @_; | |
646 | ||
647 | run_command("/sbin/iptables-restore -n", input => $cmdlist); | |
648 | } | |
649 | ||
650 | sub iptables_get_chains { | |
651 | ||
652 | my $res = {}; | |
653 | ||
654 | # check what chains we want to track | |
655 | my $is_pvefw_chain = sub { | |
656 | my $name = shift; | |
657 | ||
658 | return 1 if $name =~ m/^PVEFW-\S+$/; | |
659 | ||
660 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; | |
661 | return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT)$/; | |
662 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; | |
663 | ||
664 | return undef; | |
665 | }; | |
666 | ||
667 | my $table = ''; | |
668 | ||
669 | my $parser = sub { | |
670 | my $line = shift; | |
671 | ||
672 | return if $line =~ m/^#/; | |
673 | return if $line =~ m/^\s*$/; | |
674 | ||
675 | if ($line =~ m/^\*(\S+)$/) { | |
676 | $table = $1; | |
677 | return; | |
678 | } | |
679 | ||
680 | return if $table ne 'filter'; | |
681 | ||
682 | if ($line =~ m/^:(\S+)\s/) { | |
683 | my $chain = $1; | |
684 | return if !&$is_pvefw_chain($chain); | |
685 | $res->{$chain} = "unknown"; | |
686 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { | |
687 | my ($chain, $sig) = ($1, $2); | |
688 | return if !&$is_pvefw_chain($chain); | |
689 | $res->{$chain} = $sig; | |
690 | } else { | |
691 | # simply ignore the rest | |
692 | return; | |
693 | } | |
694 | }; | |
695 | ||
696 | run_command("/sbin/iptables-save", outfunc => $parser); | |
697 | ||
698 | return $res; | |
699 | } | |
700 | ||
701 | sub iptables_chain_exist { | |
702 | my ($chain) = @_; | |
703 | ||
704 | eval{ | |
705 | iptables("-n --list $chain"); | |
706 | }; | |
707 | return undef if $@; | |
708 | ||
709 | return 1; | |
710 | } | |
711 | ||
712 | sub iptables_rule_exist { | |
713 | my ($rule) = @_; | |
714 | ||
715 | eval{ | |
716 | iptables("-C $rule"); | |
717 | }; | |
718 | return undef if $@; | |
719 | ||
720 | return 1; | |
721 | } | |
722 | ||
723 | sub ruleset_generate_rule { | |
724 | my ($ruleset, $chain, $rule, $actions, $goto) = @_; | |
725 | ||
726 | return if $rule->{disable}; | |
727 | ||
728 | my @cmd = (); | |
729 | ||
730 | push @cmd, "-m iprange --src-range" if $rule->{nbsource} && $rule->{nbsource} > 1; | |
731 | push @cmd, "-s $rule->{source}" if $rule->{source}; | |
732 | push @cmd, "-m iprange --dst-range" if $rule->{nbdest} && $rule->{nbdest} > 1; | |
733 | push @cmd, "-d $rule->{dest}" if $rule->{dest}; | |
734 | ||
735 | if ($rule->{proto}) { | |
736 | push @cmd, "-p $rule->{proto}"; | |
737 | ||
738 | my $multiport = 0; | |
739 | $multiport++ if $rule->{nbdport} && ($rule->{nbdport} > 1); | |
740 | $multiport++ if $rule->{nbsport} && ($rule->{nbsport} > 1); | |
741 | ||
742 | push @cmd, "--match multiport" if $multiport; | |
743 | ||
744 | die "multiport: option '--sports' cannot be used together with '--dports'\n" | |
745 | if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); | |
746 | ||
747 | if ($rule->{dport}) { | |
748 | if ($rule->{proto} && $rule->{proto} eq 'icmp') { | |
749 | # Note: we use dport to store --icmp-type | |
750 | die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}}); | |
751 | push @cmd, "-m icmp --icmp-type $rule->{dport}"; | |
752 | } else { | |
753 | if ($rule->{nbdport} && $rule->{nbdport} > 1) { | |
754 | if ($multiport == 2) { | |
755 | push @cmd, "--ports $rule->{dport}"; | |
756 | } else { | |
757 | push @cmd, "--dports $rule->{dport}"; | |
758 | } | |
759 | } else { | |
760 | push @cmd, "--dport $rule->{dport}"; | |
761 | } | |
762 | } | |
763 | } | |
764 | ||
765 | if ($rule->{sport}) { | |
766 | if ($rule->{nbsport} && $rule->{nbsport} > 1) { | |
767 | push @cmd, "--sports $rule->{sport}" if $multiport != 2; | |
768 | } else { | |
769 | push @cmd, "--sport $rule->{sport}"; | |
770 | } | |
771 | } | |
772 | } elsif ($rule->{dport} || $rule->{sport}) { | |
773 | warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport}; | |
774 | warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport}; | |
775 | } | |
776 | ||
777 | push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; | |
778 | ||
779 | if (my $action = $rule->{action}) { | |
780 | $action = $actions->{$action} if defined($actions->{$action}); | |
781 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
782 | push @cmd, $goto ? "-g $action" : "-j $action"; | |
783 | } | |
784 | ||
785 | if (scalar(@cmd)) { | |
786 | my $cmdstr = join(' ', @cmd); | |
787 | ruleset_addrule($ruleset, $chain, $cmdstr); | |
788 | } | |
789 | } | |
790 | ||
791 | sub ruleset_create_chain { | |
792 | my ($ruleset, $chain) = @_; | |
793 | ||
794 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; | |
795 | ||
796 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; | |
797 | ||
798 | $ruleset->{$chain} = []; | |
799 | } | |
800 | ||
801 | sub ruleset_chain_exist { | |
802 | my ($ruleset, $chain) = @_; | |
803 | ||
804 | return $ruleset->{$chain} ? 1 : undef; | |
805 | } | |
806 | ||
807 | sub ruleset_addrule { | |
808 | my ($ruleset, $chain, $rule) = @_; | |
809 | ||
810 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; | |
811 | ||
812 | push @{$ruleset->{$chain}}, "-A $chain $rule"; | |
813 | } | |
814 | ||
815 | sub ruleset_insertrule { | |
816 | my ($ruleset, $chain, $rule) = @_; | |
817 | ||
818 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; | |
819 | ||
820 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; | |
821 | } | |
822 | ||
823 | sub generate_bridge_chains { | |
824 | my ($ruleset, $bridge) = @_; | |
825 | ||
826 | if (!ruleset_chain_exist($ruleset, "$bridge-FW")) { | |
827 | ruleset_create_chain($ruleset, "$bridge-FW"); | |
828 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-bridged -j $bridge-FW"); | |
829 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-bridged -j $bridge-FW"); | |
830 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -j DROP"); # disable interbridge routing | |
831 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -j DROP"); # disable interbridge routing | |
832 | } | |
833 | ||
834 | if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) { | |
835 | ruleset_create_chain($ruleset, "$bridge-OUT"); | |
836 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT"); | |
837 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-i $bridge -m physdev --physdev-is-bridged --physdev-is-in -j $bridge-OUT"); | |
838 | } | |
839 | ||
840 | if (!ruleset_chain_exist($ruleset, "$bridge-IN")) { | |
841 | ruleset_create_chain($ruleset, "$bridge-IN"); | |
842 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j $bridge-IN"); | |
843 | ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT"); | |
844 | # accept traffic to unmanaged bridge ports | |
845 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-bridged --physdev-is-out -j ACCEPT "); | |
846 | } | |
847 | } | |
848 | ||
849 | sub ruleset_add_chain_policy { | |
850 | my ($ruleset, $chain, $policy, $loglevel, $accept_action) = @_; | |
851 | ||
852 | if ($policy eq 'ACCEPT') { | |
853 | ||
854 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' }, | |
855 | { ACCEPT => $accept_action}); | |
856 | ||
857 | } elsif ($policy eq 'DROP') { | |
858 | ||
859 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop"); | |
860 | ||
861 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"$chain-dropped: \" --log-level $loglevel") | |
862 | if defined($loglevel); | |
863 | ||
864 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
865 | } elsif ($policy eq 'REJECT') { | |
866 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject"); | |
867 | ||
868 | ruleset_addrule($ruleset, $chain, "-j LOG --log-prefix \"$chain-reject: \" --log-level $loglevel") | |
869 | if defined($loglevel); | |
870 | ||
871 | ruleset_addrule($ruleset, $chain, "-g PVEFW-reject"); | |
872 | } else { | |
873 | # should not happen | |
874 | die "internal error: unknown policy '$policy'"; | |
875 | } | |
876 | } | |
877 | ||
878 | sub generate_tap_rules_direction { | |
879 | my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, $direction) = @_; | |
880 | ||
881 | my $lc_direction = lc($direction); | |
882 | ||
883 | my $rules = $vmfw_conf->{rules}; | |
884 | ||
885 | my $options = $vmfw_conf->{options}; | |
886 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
887 | ||
888 | my $tapchain = "$iface-$direction"; | |
889 | ||
890 | ruleset_create_chain($ruleset, $tapchain); | |
891 | ||
892 | if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) { | |
893 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs"); | |
894 | } | |
895 | ||
896 | if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) { | |
897 | ruleset_addrule($ruleset, $tapchain, "-p udp -m udp --dport 67:68 -j ACCEPT"); | |
898 | } | |
899 | ||
900 | if ($options->{tcpflags}) { | |
901 | ruleset_addrule($ruleset, $tapchain, "-p tcp -j PVEFW-tcpflags"); | |
902 | } | |
903 | ||
904 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate INVALID -j DROP"); | |
905 | ruleset_addrule($ruleset, $tapchain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
906 | ||
907 | if ($direction eq 'OUT') { | |
908 | if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) { | |
909 | ruleset_addrule($ruleset, $tapchain, "-m mac ! --mac-source $macaddr -j DROP"); | |
910 | } | |
911 | ruleset_addrule($ruleset, $tapchain, "-j MARK --set-mark 0"); # clear mark | |
912 | } | |
913 | ||
914 | foreach my $rule (@$rules) { | |
915 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
916 | next if $rule->{disable}; | |
917 | if ($rule->{type} eq 'group') { | |
918 | my $group_chain = "GROUP-$rule->{action}-$direction"; | |
919 | if(!ruleset_chain_exist($ruleset, $group_chain)){ | |
920 | generate_group_rules($ruleset, $groups_conf, $rule->{action}); | |
921 | } | |
922 | ruleset_addrule($ruleset, $tapchain, "-j $group_chain"); | |
923 | ruleset_addrule($ruleset, $tapchain, "-m mark --mark 1 -j RETURN") | |
924 | if $direction eq 'OUT'; | |
925 | } else { | |
926 | next if $rule->{type} ne $lc_direction; | |
927 | if ($direction eq 'OUT') { | |
928 | ruleset_generate_rule($ruleset, $tapchain, $rule, | |
929 | { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); | |
930 | } else { | |
931 | ruleset_generate_rule($ruleset, $tapchain, $rule, { REJECT => "PVEFW-reject" }); | |
932 | } | |
933 | } | |
934 | } | |
935 | ||
936 | # implement policy | |
937 | my $policy; | |
938 | ||
939 | if ($direction eq 'OUT') { | |
940 | $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default | |
941 | } else { | |
942 | $policy = $options->{'policy-in'} || 'DROP'; # allow nothing by default | |
943 | } | |
944 | ||
945 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : "ACCEPT"; | |
946 | ruleset_add_chain_policy($ruleset, $tapchain, $policy, $loglevel, $accept_action); | |
947 | ||
948 | # plug the tap chain to bridge chain | |
949 | my $physdevdirection = $direction eq 'IN' ? "out" : "in"; | |
950 | my $rule = "-m physdev --physdev-$physdevdirection $iface --physdev-is-bridged -j $tapchain"; | |
951 | ruleset_insertrule($ruleset, "$bridge-$direction", $rule); | |
952 | } | |
953 | ||
954 | sub enable_host_firewall { | |
955 | my ($ruleset, $hostfw_conf, $groups_conf) = @_; | |
956 | ||
957 | # fixme: allow security groups | |
958 | ||
959 | my $options = $hostfw_conf->{options}; | |
960 | my $rules = $hostfw_conf->{rules}; | |
961 | ||
962 | # host inbound firewall | |
963 | my $chain = "PVEFW-HOST-IN"; | |
964 | ruleset_create_chain($ruleset, $chain); | |
965 | ||
966 | my $loglevel = get_option_log_level($options, "log_level_in"); | |
967 | ||
968 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); | |
969 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
970 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); | |
971 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
972 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); | |
973 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync | |
974 | ||
975 | # we use RETURN because we need to check also tap rules | |
976 | my $accept_action = 'RETURN'; | |
977 | ||
978 | foreach my $rule (@$rules) { | |
979 | next if $rule->{type} ne 'in'; | |
980 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); | |
981 | } | |
982 | ||
983 | # implement input policy | |
984 | my $policy = $options->{'policy-in'} || 'DROP'; # allow nothing by default | |
985 | ruleset_add_chain_policy($ruleset, $chain, $policy, $loglevel, $accept_action); | |
986 | ||
987 | # host outbound firewall | |
988 | $chain = "PVEFW-HOST-OUT"; | |
989 | ruleset_create_chain($ruleset, $chain); | |
990 | ||
991 | $loglevel = get_option_log_level($options, "log_level_out"); | |
992 | ||
993 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); | |
994 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
995 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); | |
996 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
997 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW -m multiport --dports 5404,5405 -j ACCEPT"); | |
998 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync | |
999 | ||
1000 | # we use RETURN because we may want to check other thigs later | |
1001 | $accept_action = 'RETURN'; | |
1002 | ||
1003 | foreach my $rule (@$rules) { | |
1004 | next if $rule->{type} ne 'out'; | |
1005 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); | |
1006 | } | |
1007 | ||
1008 | # implement output policy | |
1009 | $policy = $options->{'policy-out'} || 'ACCEPT'; # allow everything by default | |
1010 | ruleset_add_chain_policy($ruleset, $chain, $policy, $loglevel, $accept_action); | |
1011 | ||
1012 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); | |
1013 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
1014 | } | |
1015 | ||
1016 | sub generate_group_rules { | |
1017 | my ($ruleset, $groups_conf, $group) = @_; | |
1018 | ||
1019 | die "no such security group '$group'\n" if !$groups_conf->{$group}; | |
1020 | ||
1021 | my $rules = $groups_conf->{$group}->{rules}; | |
1022 | ||
1023 | my $chain = "GROUP-${group}-IN"; | |
1024 | ||
1025 | ruleset_create_chain($ruleset, $chain); | |
1026 | ||
1027 | foreach my $rule (@$rules) { | |
1028 | next if $rule->{type} ne 'in'; | |
1029 | ruleset_generate_rule($ruleset, $chain, $rule, { REJECT => "PVEFW-reject" }); | |
1030 | } | |
1031 | ||
1032 | $chain = "GROUP-${group}-OUT"; | |
1033 | ||
1034 | ruleset_create_chain($ruleset, $chain); | |
1035 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
1036 | ||
1037 | foreach my $rule (@$rules) { | |
1038 | next if $rule->{type} ne 'out'; | |
1039 | # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to | |
1040 | # check also other tap rules later | |
1041 | ruleset_generate_rule($ruleset, $chain, $rule, | |
1042 | { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }); | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | my $MAX_NETS = 32; | |
1047 | my $valid_netdev_names = {}; | |
1048 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
1049 | $valid_netdev_names->{"net$i"} = 1; | |
1050 | } | |
1051 | ||
1052 | sub parse_fw_rule { | |
1053 | my ($line, $need_iface, $allow_groups) = @_; | |
1054 | ||
1055 | my $macros = get_firewall_macros(); | |
1056 | my $protocols = get_etc_protocols(); | |
1057 | ||
1058 | my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport); | |
1059 | ||
1060 | # we can add single line comments to the end of the rule | |
1061 | my $comment = $1 if $line =~ s/#\s*(.*?)\s*$//; | |
1062 | ||
1063 | # we can disable a rule when prefixed with '|' | |
1064 | my $disable = 1 if $line =~ s/^\|//; | |
1065 | ||
1066 | my @data = split(/\s+/, $line); | |
1067 | my $expected_elements = $need_iface ? 8 : 7; | |
1068 | ||
1069 | die "wrong number of rule elements\n" if scalar(@data) > $expected_elements; | |
1070 | ||
1071 | if ($need_iface) { | |
1072 | ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data | |
1073 | } else { | |
1074 | ($type, $action, $source, $dest, $proto, $dport, $sport) = @data; | |
1075 | } | |
1076 | ||
1077 | die "incomplete rule\n" if ! ($type && $action); | |
1078 | ||
1079 | my $macro; | |
1080 | my $macro_name; | |
1081 | ||
1082 | $type = lc($type); | |
1083 | ||
1084 | if ($type eq 'in' || $type eq 'out') { | |
1085 | if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) { | |
1086 | # OK | |
1087 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
1088 | ($macro_name, $action) = ($1, $2); | |
1089 | my $lc_macro_name = lc($macro_name); | |
1090 | my $preferred_name = $pve_fw_preferred_macro_names->{$lc_macro_name}; | |
1091 | $macro_name = $preferred_name if $preferred_name; | |
1092 | $macro = $macros->{$lc_macro_name}; | |
1093 | die "unknown macro '$macro_name'\n" if !$macro; | |
1094 | } else { | |
1095 | die "unknown action '$action'\n"; | |
1096 | } | |
1097 | } elsif ($type eq 'group') { | |
1098 | die "wrong number of rule elements\n" if scalar(@data) != 3; | |
1099 | die "groups disabled\n" if !$allow_groups; | |
1100 | ||
1101 | die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/; | |
1102 | } else { | |
1103 | die "unknown rule type '$type'\n"; | |
1104 | } | |
1105 | ||
1106 | if ($need_iface) { | |
1107 | $iface = undef if $iface && $iface eq '-'; | |
1108 | die "unknown interface '$iface'\n" | |
1109 | if defined($iface) && !$valid_netdev_names->{$iface}; | |
1110 | } | |
1111 | ||
1112 | $proto = undef if $proto && $proto eq '-'; | |
1113 | die "unknown protokol '$proto'\n" if $proto && | |
1114 | !(defined($protocols->{byname}->{$proto}) || | |
1115 | defined($protocols->{byid}->{$proto})); | |
1116 | ||
1117 | $source = undef if $source && $source eq '-'; | |
1118 | $dest = undef if $dest && $dest eq '-'; | |
1119 | ||
1120 | $dport = undef if $dport && $dport eq '-'; | |
1121 | $sport = undef if $sport && $sport eq '-'; | |
1122 | ||
1123 | my $nbsource = undef; | |
1124 | my $nbdest = undef; | |
1125 | ||
1126 | $nbsource = parse_address_list($source) if $source; | |
1127 | $nbdest = parse_address_list($dest) if $dest; | |
1128 | ||
1129 | my $rules = []; | |
1130 | ||
1131 | my $param = { | |
1132 | type => $type, | |
1133 | disable => $disable, | |
1134 | comment => $comment, | |
1135 | action => $action, | |
1136 | iface => $iface, | |
1137 | source => $source, | |
1138 | dest => $dest, | |
1139 | nbsource => $nbsource, | |
1140 | nbdest => $nbdest, | |
1141 | proto => $proto, | |
1142 | dport => $dport, | |
1143 | sport => $sport, | |
1144 | }; | |
1145 | ||
1146 | if ($macro) { | |
1147 | foreach my $templ (@$macro) { | |
1148 | my $rule = {}; | |
1149 | my $param_used = {}; | |
1150 | foreach my $k (keys %$templ) { | |
1151 | my $v = $templ->{$k}; | |
1152 | if ($v eq 'PARAM') { | |
1153 | $v = $param->{$k}; | |
1154 | $param_used->{$k} = 1; | |
1155 | } elsif ($v eq 'DEST') { | |
1156 | $v = $param->{dest}; | |
1157 | $param_used->{dest} = 1; | |
1158 | } elsif ($v eq 'SOURCE') { | |
1159 | $v = $param->{source}; | |
1160 | $param_used->{source} = 1; | |
1161 | } | |
1162 | ||
1163 | die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v); | |
1164 | $rule->{$k} = $v; | |
1165 | } | |
1166 | foreach my $k (keys %$param) { | |
1167 | next if !defined($param->{$k}); | |
1168 | next if $param_used->{$k}; | |
1169 | if (defined($rule->{$k})) { | |
1170 | die "parameter '$k' already define in macro (value = '$rule->{$k}')\n" | |
1171 | if $rule->{$k} ne $param->{$k}; | |
1172 | } else { | |
1173 | $rule->{$k} = $param->{$k}; | |
1174 | } | |
1175 | } | |
1176 | push @$rules, $rule; | |
1177 | } | |
1178 | } else { | |
1179 | push @$rules, $param; | |
1180 | } | |
1181 | ||
1182 | foreach my $rule (@$rules) { | |
1183 | $rule->{nbdport} = parse_port_name_number_or_range($rule->{dport}) | |
1184 | if defined($rule->{dport}); | |
1185 | $rule->{nbsport} = parse_port_name_number_or_range($rule->{sport}) | |
1186 | if defined($rule->{sport}); | |
1187 | } | |
1188 | ||
1189 | return $rules; | |
1190 | } | |
1191 | ||
1192 | sub parse_vmfw_option { | |
1193 | my ($line) = @_; | |
1194 | ||
1195 | my ($opt, $value); | |
1196 | ||
1197 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
1198 | ||
1199 | if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags):\s*(0|1)\s*$/i) { | |
1200 | $opt = lc($1); | |
1201 | $value = int($2); | |
1202 | } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) { | |
1203 | $opt = lc($1); | |
1204 | $value = $2 ? lc($3) : ''; | |
1205 | } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
1206 | $opt = lc($1); | |
1207 | $value = uc($3); | |
1208 | } else { | |
1209 | chomp $line; | |
1210 | die "can't parse option '$line'\n" | |
1211 | } | |
1212 | ||
1213 | return ($opt, $value); | |
1214 | } | |
1215 | ||
1216 | sub parse_hostfw_option { | |
1217 | my ($line) = @_; | |
1218 | ||
1219 | my ($opt, $value); | |
1220 | ||
1221 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
1222 | ||
1223 | if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags):\s*(0|1)\s*$/i) { | |
1224 | $opt = lc($1); | |
1225 | $value = int($2); | |
1226 | } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) { | |
1227 | $opt = lc($1); | |
1228 | $value = $2 ? lc($3) : ''; | |
1229 | } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
1230 | $opt = lc($1); | |
1231 | $value = uc($3); | |
1232 | } else { | |
1233 | chomp $line; | |
1234 | die "can't parse option '$line'\n" | |
1235 | } | |
1236 | ||
1237 | return ($opt, $value); | |
1238 | } | |
1239 | ||
1240 | sub parse_vm_fw_rules { | |
1241 | my ($filename, $fh) = @_; | |
1242 | ||
1243 | my $res = { rules => [], options => {}}; | |
1244 | ||
1245 | my $section; | |
1246 | ||
1247 | while (defined(my $line = <$fh>)) { | |
1248 | next if $line =~ m/^#/; | |
1249 | next if $line =~ m/^\s*$/; | |
1250 | ||
1251 | my $linenr = $fh->input_line_number(); | |
1252 | my $prefix = "$filename (line $linenr)"; | |
1253 | ||
1254 | if ($line =~ m/^\[(\S+)\]\s*$/i) { | |
1255 | $section = lc($1); | |
1256 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; | |
1257 | next; | |
1258 | } | |
1259 | if (!$section) { | |
1260 | warn "$prefix: skip line - no section"; | |
1261 | next; | |
1262 | } | |
1263 | ||
1264 | next if !$res->{$section}; # skip undefined section | |
1265 | ||
1266 | if ($section eq 'options') { | |
1267 | eval { | |
1268 | my ($opt, $value) = parse_vmfw_option($line); | |
1269 | $res->{options}->{$opt} = $value; | |
1270 | }; | |
1271 | warn "$prefix: $@" if $@; | |
1272 | next; | |
1273 | } | |
1274 | ||
1275 | my $rules; | |
1276 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
1277 | if (my $err = $@) { | |
1278 | warn "$prefix: $err"; | |
1279 | next; | |
1280 | } | |
1281 | ||
1282 | push @{$res->{$section}}, @$rules; | |
1283 | } | |
1284 | ||
1285 | return $res; | |
1286 | } | |
1287 | ||
1288 | sub parse_host_fw_rules { | |
1289 | my ($filename, $fh) = @_; | |
1290 | ||
1291 | my $res = { rules => [], options => {}}; | |
1292 | ||
1293 | my $section; | |
1294 | ||
1295 | while (defined(my $line = <$fh>)) { | |
1296 | next if $line =~ m/^#/; | |
1297 | next if $line =~ m/^\s*$/; | |
1298 | ||
1299 | my $linenr = $fh->input_line_number(); | |
1300 | my $prefix = "$filename (line $linenr)"; | |
1301 | ||
1302 | if ($line =~ m/^\[(\S+)\]\s*$/i) { | |
1303 | $section = lc($1); | |
1304 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; | |
1305 | next; | |
1306 | } | |
1307 | if (!$section) { | |
1308 | warn "$prefix: skip line - no section"; | |
1309 | next; | |
1310 | } | |
1311 | ||
1312 | next if !$res->{$section}; # skip undefined section | |
1313 | ||
1314 | if ($section eq 'options') { | |
1315 | eval { | |
1316 | my ($opt, $value) = parse_hostfw_option($line); | |
1317 | $res->{options}->{$opt} = $value; | |
1318 | }; | |
1319 | warn "$prefix: $@" if $@; | |
1320 | next; | |
1321 | } | |
1322 | ||
1323 | my $rules; | |
1324 | eval { $rules = parse_fw_rule($line, 1, 1); }; | |
1325 | if (my $err = $@) { | |
1326 | warn "$prefix: $err"; | |
1327 | next; | |
1328 | } | |
1329 | ||
1330 | push @{$res->{$section}}, @$rules; | |
1331 | } | |
1332 | ||
1333 | return $res; | |
1334 | } | |
1335 | ||
1336 | sub parse_group_fw_rules { | |
1337 | my ($filename, $fh) = @_; | |
1338 | ||
1339 | my $section; | |
1340 | my $group; | |
1341 | ||
1342 | my $res = { rules => [] }; | |
1343 | ||
1344 | while (defined(my $line = <$fh>)) { | |
1345 | next if $line =~ m/^#/; | |
1346 | next if $line =~ m/^\s*$/; | |
1347 | ||
1348 | my $linenr = $fh->input_line_number(); | |
1349 | my $prefix = "$filename (line $linenr)"; | |
1350 | ||
1351 | if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) { | |
1352 | $section = 'rules'; | |
1353 | $group = lc($1); | |
1354 | next; | |
1355 | } | |
1356 | if (!$section || !$group) { | |
1357 | warn "$prefix: skip line - no section"; | |
1358 | next; | |
1359 | } | |
1360 | ||
1361 | my $rules; | |
1362 | eval { $rules = parse_fw_rule($line, 0, 0); }; | |
1363 | if (my $err = $@) { | |
1364 | warn "$prefix: $err"; | |
1365 | next; | |
1366 | } | |
1367 | ||
1368 | push @{$res->{$group}->{$section}}, @$rules; | |
1369 | } | |
1370 | ||
1371 | return $res; | |
1372 | } | |
1373 | ||
1374 | sub run_locked { | |
1375 | my ($code, @param) = @_; | |
1376 | ||
1377 | my $timeout = 10; | |
1378 | ||
1379 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
1380 | ||
1381 | die $@ if $@; | |
1382 | ||
1383 | return $res; | |
1384 | } | |
1385 | ||
1386 | sub read_local_vm_config { | |
1387 | ||
1388 | my $openvz = {}; | |
1389 | ||
1390 | my $qemu = {}; | |
1391 | ||
1392 | my $list = PVE::QemuServer::config_list(); | |
1393 | ||
1394 | foreach my $vmid (keys %$list) { | |
1395 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); | |
1396 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
1397 | $qemu->{$vmid} = $conf; | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
1402 | ||
1403 | return $vmdata; | |
1404 | }; | |
1405 | ||
1406 | sub read_vm_firewall_rules { | |
1407 | my ($vmdata) = @_; | |
1408 | my $rules = {}; | |
1409 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
1410 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
1411 | my $fh = IO::File->new($filename, O_RDONLY); | |
1412 | next if !$fh; | |
1413 | ||
1414 | $rules->{$vmid} = parse_vm_fw_rules($filename, $fh); | |
1415 | } | |
1416 | ||
1417 | return $rules; | |
1418 | } | |
1419 | ||
1420 | sub get_option_log_level { | |
1421 | my ($options, $k) = @_; | |
1422 | ||
1423 | my $v = $options->{$k}; | |
1424 | $v = $default_log_level if !defined($v); | |
1425 | ||
1426 | return undef if $v eq '' || $v eq 'nolog'; | |
1427 | ||
1428 | $v = $log_level_hash->{$v} if defined($log_level_hash->{$v}); | |
1429 | ||
1430 | return $v if ($v >= 0) && ($v <= 7); | |
1431 | ||
1432 | warn "unknown log level ($k = '$v')\n"; | |
1433 | ||
1434 | return undef; | |
1435 | } | |
1436 | ||
1437 | sub generate_std_chains { | |
1438 | my ($ruleset, $options) = @_; | |
1439 | ||
1440 | my $loglevel = get_option_log_level($options, 'smurf_log_level'); | |
1441 | ||
1442 | # same as shorewall smurflog. | |
1443 | if (defined($loglevel)) { | |
1444 | $pve_std_chains-> {'PVEFW-smurflog'} = [ | |
1445 | "-j LOG --log-prefix \"smurfs-dropped\" --log-level $loglevel", | |
1446 | "-j DROP", | |
1447 | ]; | |
1448 | } else { | |
1449 | $pve_std_chains-> {'PVEFW-smurflog'} = [ "-j DROP" ]; | |
1450 | } | |
1451 | ||
1452 | # same as shorewall logflags action. | |
1453 | $loglevel = get_option_log_level($options, 'tcp_flags_log_level'); | |
1454 | if (defined($loglevel)) { | |
1455 | $pve_std_chains-> {'PVEFW-logflags'} = [ | |
1456 | "-j LOG --log-prefix \"logflags-dropped:\" --log-level $loglevel --log-ip-options", | |
1457 | "-j DROP", | |
1458 | ]; | |
1459 | } else { | |
1460 | $pve_std_chains-> {'PVEFW-logflags'} = [ "-j DROP" ]; | |
1461 | } | |
1462 | ||
1463 | foreach my $chain (keys %$pve_std_chains) { | |
1464 | ruleset_create_chain($ruleset, $chain); | |
1465 | foreach my $rule (@{$pve_std_chains->{$chain}}) { | |
1466 | if (ref($rule)) { | |
1467 | ruleset_generate_rule($ruleset, $chain, $rule); | |
1468 | } else { | |
1469 | ruleset_addrule($ruleset, $chain, $rule); | |
1470 | } | |
1471 | } | |
1472 | } | |
1473 | } | |
1474 | ||
1475 | sub save_pvefw_status { | |
1476 | my ($status) = @_; | |
1477 | ||
1478 | die "unknown status '$status' - internal error" | |
1479 | if $status !~ m/^(stopped|active)$/; | |
1480 | ||
1481 | mkdir dirname($pve_fw_status_filename); | |
1482 | PVE::Tools::file_set_contents($pve_fw_status_filename, $status); | |
1483 | } | |
1484 | ||
1485 | sub read_pvefw_status { | |
1486 | ||
1487 | my $status = 'unknown'; | |
1488 | ||
1489 | return 'stopped' if ! -f $pve_fw_status_filename; | |
1490 | ||
1491 | eval { | |
1492 | $status = PVE::Tools::file_get_contents($pve_fw_status_filename); | |
1493 | }; | |
1494 | warn $@ if $@; | |
1495 | ||
1496 | return $status; | |
1497 | } | |
1498 | ||
1499 | sub compile { | |
1500 | my $vmdata = read_local_vm_config(); | |
1501 | my $rules = read_vm_firewall_rules($vmdata); | |
1502 | ||
1503 | my $groups_conf = {}; | |
1504 | my $filename = "/etc/pve/firewall/groups.fw"; | |
1505 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1506 | $groups_conf = parse_group_fw_rules($filename, $fh); | |
1507 | } | |
1508 | ||
1509 | #print Dumper($rules); | |
1510 | ||
1511 | my $ruleset = {}; | |
1512 | ||
1513 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); | |
1514 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
1515 | ||
1516 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
1517 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
1518 | ||
1519 | my $hostfw_options = {}; | |
1520 | my $hostfw_conf; | |
1521 | ||
1522 | $filename = "/etc/pve/local/host.fw"; | |
1523 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1524 | $hostfw_conf = parse_host_fw_rules($filename, $fh); | |
1525 | $hostfw_options = $hostfw_conf->{options}; | |
1526 | } | |
1527 | ||
1528 | generate_std_chains($ruleset, $hostfw_options); | |
1529 | ||
1530 | my $hostfw_enable = $hostfw_conf && | |
1531 | !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0)); | |
1532 | ||
1533 | enable_host_firewall($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable; | |
1534 | ||
1535 | # generate firewall rules for QEMU VMs | |
1536 | foreach my $vmid (keys %{$vmdata->{qemu}}) { | |
1537 | my $conf = $vmdata->{qemu}->{$vmid}; | |
1538 | my $vmfw_conf = $rules->{$vmid}; | |
1539 | next if !$vmfw_conf; | |
1540 | next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); | |
1541 | ||
1542 | foreach my $netid (keys %$conf) { | |
1543 | next if $netid !~ m/^net(\d+)$/; | |
1544 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
1545 | next if !$net; | |
1546 | my $iface = "tap${vmid}i$1"; | |
1547 | ||
1548 | my $bridge = $net->{bridge}; | |
1549 | next if !$bridge; # fixme: ? | |
1550 | ||
1551 | $bridge .= "v$net->{tag}" if $net->{tag}; | |
1552 | ||
1553 | generate_bridge_chains($ruleset, $bridge); | |
1554 | ||
1555 | my $macaddr = $net->{macaddr}; | |
1556 | generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'IN'); | |
1557 | generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $bridge, 'OUT'); | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | return $ruleset; | |
1562 | } | |
1563 | ||
1564 | sub get_ruleset_status { | |
1565 | my ($ruleset, $verbose) = @_; | |
1566 | ||
1567 | my $active_chains = iptables_get_chains(); | |
1568 | ||
1569 | my $statushash = {}; | |
1570 | ||
1571 | foreach my $chain (sort keys %$ruleset) { | |
1572 | my $digest = Digest::SHA->new('sha1'); | |
1573 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1574 | $digest->add("$cmd\n"); | |
1575 | } | |
1576 | my $sig = $digest->b64digest; | |
1577 | $statushash->{$chain}->{sig} = $sig; | |
1578 | ||
1579 | my $oldsig = $active_chains->{$chain}; | |
1580 | if (!defined($oldsig)) { | |
1581 | $statushash->{$chain}->{action} = 'create'; | |
1582 | } else { | |
1583 | if ($oldsig eq $sig) { | |
1584 | $statushash->{$chain}->{action} = 'exists'; | |
1585 | } else { | |
1586 | $statushash->{$chain}->{action} = 'update'; | |
1587 | } | |
1588 | } | |
1589 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
1590 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1591 | print "\t$cmd\n" if $verbose; | |
1592 | } | |
1593 | } | |
1594 | ||
1595 | foreach my $chain (sort keys %$active_chains) { | |
1596 | if (!defined($ruleset->{$chain})) { | |
1597 | my $sig = $active_chains->{$chain}; | |
1598 | $statushash->{$chain}->{action} = 'delete'; | |
1599 | $statushash->{$chain}->{sig} = $sig; | |
1600 | print "delete $chain ($sig)\n" if $verbose; | |
1601 | } | |
1602 | } | |
1603 | ||
1604 | return $statushash; | |
1605 | } | |
1606 | ||
1607 | sub print_ruleset { | |
1608 | my ($ruleset) = @_; | |
1609 | ||
1610 | get_ruleset_status($ruleset, 1); | |
1611 | } | |
1612 | ||
1613 | sub print_sig_rule { | |
1614 | my ($chain, $sig) = @_; | |
1615 | ||
1616 | # We just use this to store a SHA1 checksum used to detect changes | |
1617 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
1618 | } | |
1619 | ||
1620 | sub get_rulset_cmdlist { | |
1621 | my ($ruleset, $verbose) = @_; | |
1622 | ||
1623 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
1624 | ||
1625 | my $statushash = get_ruleset_status($ruleset, $verbose); | |
1626 | ||
1627 | # create missing chains first | |
1628 | foreach my $chain (sort keys %$ruleset) { | |
1629 | my $stat = $statushash->{$chain}; | |
1630 | die "internal error" if !$stat; | |
1631 | next if $stat->{action} ne 'create'; | |
1632 | ||
1633 | $cmdlist .= ":$chain - [0:0]\n"; | |
1634 | } | |
1635 | ||
1636 | my $rule = "INPUT -j PVEFW-INPUT"; | |
1637 | if (!PVE::Firewall::iptables_rule_exist($rule)) { | |
1638 | $cmdlist .= "-A $rule\n"; | |
1639 | } | |
1640 | $rule = "OUTPUT -j PVEFW-OUTPUT"; | |
1641 | if (!PVE::Firewall::iptables_rule_exist($rule)) { | |
1642 | $cmdlist .= "-A $rule\n"; | |
1643 | } | |
1644 | ||
1645 | $rule = "FORWARD -j PVEFW-FORWARD"; | |
1646 | if (!PVE::Firewall::iptables_rule_exist($rule)) { | |
1647 | $cmdlist .= "-A $rule\n"; | |
1648 | } | |
1649 | ||
1650 | foreach my $chain (sort keys %$ruleset) { | |
1651 | my $stat = $statushash->{$chain}; | |
1652 | die "internal error" if !$stat; | |
1653 | ||
1654 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
1655 | $cmdlist .= "-F $chain\n"; | |
1656 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
1657 | $cmdlist .= "$cmd\n"; | |
1658 | } | |
1659 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
1660 | } elsif ($stat->{action} eq 'delete') { | |
1661 | die "internal error"; # this should not happen | |
1662 | } elsif ($stat->{action} eq 'exists') { | |
1663 | # do nothing | |
1664 | } else { | |
1665 | die "internal error - unknown status '$stat->{action}'"; | |
1666 | } | |
1667 | } | |
1668 | ||
1669 | foreach my $chain (keys %$statushash) { | |
1670 | next if $statushash->{$chain}->{action} ne 'delete'; | |
1671 | $cmdlist .= "-F $chain\n"; | |
1672 | } | |
1673 | foreach my $chain (keys %$statushash) { | |
1674 | next if $statushash->{$chain}->{action} ne 'delete'; | |
1675 | next if $chain eq 'PVEFW-INPUT'; | |
1676 | next if $chain eq 'PVEFW-OUTPUT'; | |
1677 | next if $chain eq 'PVEFW-FORWARD'; | |
1678 | $cmdlist .= "-X $chain\n"; | |
1679 | } | |
1680 | ||
1681 | $cmdlist .= "COMMIT\n"; | |
1682 | ||
1683 | return $cmdlist; | |
1684 | } | |
1685 | ||
1686 | sub apply_ruleset { | |
1687 | my ($ruleset, $verbose) = @_; | |
1688 | ||
1689 | enable_bridge_firewall(); | |
1690 | ||
1691 | my $cmdlist = get_rulset_cmdlist($ruleset, $verbose); | |
1692 | ||
1693 | print $cmdlist if $verbose; | |
1694 | ||
1695 | iptables_restore_cmdlist($cmdlist); | |
1696 | ||
1697 | # test: re-read status and check if everything is up to date | |
1698 | my $statushash = get_ruleset_status($ruleset); | |
1699 | ||
1700 | my $errors; | |
1701 | foreach my $chain (sort keys %$ruleset) { | |
1702 | my $stat = $statushash->{$chain}; | |
1703 | if ($stat->{action} ne 'exists') { | |
1704 | warn "unable to update chain '$chain'\n"; | |
1705 | $errors = 1; | |
1706 | } | |
1707 | } | |
1708 | ||
1709 | die "unable to apply firewall changes\n" if $errors; | |
1710 | } | |
1711 | ||
1712 | sub update { | |
1713 | my ($start, $verbose) = @_; | |
1714 | ||
1715 | my $code = sub { | |
1716 | my $status = read_pvefw_status(); | |
1717 | ||
1718 | my $ruleset = PVE::Firewall::compile(); | |
1719 | ||
1720 | if ($start || $status eq 'active') { | |
1721 | ||
1722 | save_pvefw_status('active') if ($status ne 'active'); | |
1723 | ||
1724 | PVE::Firewall::apply_ruleset($ruleset, $verbose); | |
1725 | } else { | |
1726 | print "Firewall not active (status = $status)\n" if $verbose; | |
1727 | } | |
1728 | }; | |
1729 | ||
1730 | run_locked($code); | |
1731 | } | |
1732 | ||
1733 | ||
1734 | 1; |