]>
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::INotify; | |
8 | use PVE::Cluster; | |
9 | use PVE::ProcFSTools; | |
10 | use PVE::Tools; | |
11 | use File::Basename; | |
12 | use File::Path; | |
13 | use IO::File; | |
14 | use Net::IP; | |
15 | use PVE::Tools qw(run_command lock_file); | |
16 | use Encode; | |
17 | ||
18 | # dynamically include PVE::QemuServer and PVE::OpenVZ | |
19 | # to avoid dependency problems | |
20 | my $have_qemu_server; | |
21 | eval { | |
22 | require PVE::QemuServer; | |
23 | $have_qemu_server = 1; | |
24 | }; | |
25 | ||
26 | my $have_pve_manager; | |
27 | eval { | |
28 | require PVE::OpenVZ; | |
29 | $have_pve_manager = 1; | |
30 | }; | |
31 | ||
32 | use Data::Dumper; | |
33 | ||
34 | my $nodename = PVE::INotify::nodename(); | |
35 | ||
36 | my $pve_fw_lock_filename = "/var/lock/pvefw.lck"; | |
37 | my $pve_fw_status_filename = "/var/lib/pve-firewall/pvefw.status"; | |
38 | ||
39 | my $default_log_level = 'info'; | |
40 | ||
41 | my $log_level_hash = { | |
42 | debug => 7, | |
43 | info => 6, | |
44 | notice => 5, | |
45 | warning => 4, | |
46 | err => 3, | |
47 | crit => 2, | |
48 | alert => 1, | |
49 | emerg => 0, | |
50 | }; | |
51 | ||
52 | # imported/converted from: /usr/share/shorewall/macro.* | |
53 | my $pve_fw_macros = { | |
54 | 'Amanda' => [ | |
55 | { action => 'PARAM', proto => 'udp', dport => '10080' }, | |
56 | { action => 'PARAM', proto => 'tcp', dport => '10080' }, | |
57 | ], | |
58 | 'Auth' => [ | |
59 | { action => 'PARAM', proto => 'tcp', dport => '113' }, | |
60 | ], | |
61 | 'BGP' => [ | |
62 | { action => 'PARAM', proto => 'tcp', dport => '179' }, | |
63 | ], | |
64 | 'BitTorrent' => [ | |
65 | { action => 'PARAM', proto => 'tcp', dport => '6881:6889' }, | |
66 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
67 | ], | |
68 | 'BitTorrent32' => [ | |
69 | { action => 'PARAM', proto => 'tcp', dport => '6881:6999' }, | |
70 | { action => 'PARAM', proto => 'udp', dport => '6881' }, | |
71 | ], | |
72 | 'CVS' => [ | |
73 | { action => 'PARAM', proto => 'tcp', dport => '2401' }, | |
74 | ], | |
75 | 'Citrix' => [ | |
76 | { action => 'PARAM', proto => 'tcp', dport => '1494' }, | |
77 | { action => 'PARAM', proto => 'udp', dport => '1604' }, | |
78 | { action => 'PARAM', proto => 'tcp', dport => '2598' }, | |
79 | ], | |
80 | 'DAAP' => [ | |
81 | { action => 'PARAM', proto => 'tcp', dport => '3689' }, | |
82 | { action => 'PARAM', proto => 'udp', dport => '3689' }, | |
83 | ], | |
84 | 'DCC' => [ | |
85 | { action => 'PARAM', proto => 'tcp', dport => '6277' }, | |
86 | ], | |
87 | 'DHCPfwd' => [ | |
88 | { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
89 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' }, | |
90 | ], | |
91 | 'DNS' => [ | |
92 | { action => 'PARAM', proto => 'udp', dport => '53' }, | |
93 | { action => 'PARAM', proto => 'tcp', dport => '53' }, | |
94 | ], | |
95 | 'Distcc' => [ | |
96 | { action => 'PARAM', proto => 'tcp', dport => '3632' }, | |
97 | ], | |
98 | 'Edonkey' => [ | |
99 | { action => 'PARAM', proto => 'tcp', dport => '4662' }, | |
100 | { action => 'PARAM', proto => 'udp', dport => '4665' }, | |
101 | ], | |
102 | 'FTP' => [ | |
103 | { action => 'PARAM', proto => 'tcp', dport => '21' }, | |
104 | ], | |
105 | 'Finger' => [ | |
106 | { action => 'PARAM', proto => 'tcp', dport => '79' }, | |
107 | ], | |
108 | 'GNUnet' => [ | |
109 | { action => 'PARAM', proto => 'tcp', dport => '2086' }, | |
110 | { action => 'PARAM', proto => 'udp', dport => '2086' }, | |
111 | { action => 'PARAM', proto => 'tcp', dport => '1080' }, | |
112 | { action => 'PARAM', proto => 'udp', dport => '1080' }, | |
113 | ], | |
114 | 'GRE' => [ | |
115 | { action => 'PARAM', proto => '47' }, | |
116 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
117 | ], | |
118 | 'Git' => [ | |
119 | { action => 'PARAM', proto => 'tcp', dport => '9418' }, | |
120 | ], | |
121 | 'Gnutella' => [ | |
122 | { action => 'PARAM', proto => 'tcp', dport => '6346' }, | |
123 | { action => 'PARAM', proto => 'udp', dport => '6346' }, | |
124 | ], | |
125 | 'HKP' => [ | |
126 | { action => 'PARAM', proto => 'tcp', dport => '11371' }, | |
127 | ], | |
128 | 'HTTP' => [ | |
129 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
130 | ], | |
131 | 'HTTPS' => [ | |
132 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
133 | ], | |
134 | 'ICPV2' => [ | |
135 | { action => 'PARAM', proto => 'udp', dport => '3130' }, | |
136 | ], | |
137 | 'ICQ' => [ | |
138 | { action => 'PARAM', proto => 'tcp', dport => '5190' }, | |
139 | ], | |
140 | 'IMAP' => [ | |
141 | { action => 'PARAM', proto => 'tcp', dport => '143' }, | |
142 | ], | |
143 | 'IMAPS' => [ | |
144 | { action => 'PARAM', proto => 'tcp', dport => '993' }, | |
145 | ], | |
146 | 'IPIP' => [ | |
147 | { action => 'PARAM', proto => '94' }, | |
148 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' }, | |
149 | ], | |
150 | 'IPP' => [ | |
151 | { action => 'PARAM', proto => 'tcp', dport => '631' }, | |
152 | ], | |
153 | 'IPPbrd' => [ | |
154 | { action => 'PARAM', proto => 'udp', dport => '631' }, | |
155 | ], | |
156 | 'IPPserver' => [ | |
157 | { action => 'PARAM', source => 'SOURCE', dest => 'DEST', proto => 'tcp', dport => '631' }, | |
158 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '631' }, | |
159 | ], | |
160 | 'IPsec' => [ | |
161 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
162 | { action => 'PARAM', proto => '50' }, | |
163 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
164 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
165 | ], | |
166 | 'IPsecah' => [ | |
167 | { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' }, | |
168 | { action => 'PARAM', proto => '51' }, | |
169 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' }, | |
170 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' }, | |
171 | ], | |
172 | 'IPsecnat' => [ | |
173 | { action => 'PARAM', proto => 'udp', dport => '500' }, | |
174 | { action => 'PARAM', proto => 'udp', dport => '4500' }, | |
175 | { action => 'PARAM', proto => '50' }, | |
176 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' }, | |
177 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' }, | |
178 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' }, | |
179 | ], | |
180 | 'IRC' => [ | |
181 | { action => 'PARAM', proto => 'tcp', dport => '6667' }, | |
182 | ], | |
183 | 'JabberPlain' => [ | |
184 | { action => 'PARAM', proto => 'tcp', dport => '5222' }, | |
185 | ], | |
186 | 'JabberSecure' => [ | |
187 | { action => 'PARAM', proto => 'tcp', dport => '5223' }, | |
188 | ], | |
189 | 'Jabberd' => [ | |
190 | { action => 'PARAM', proto => 'tcp', dport => '5269' }, | |
191 | ], | |
192 | 'Jetdirect' => [ | |
193 | { action => 'PARAM', proto => 'tcp', dport => '9100' }, | |
194 | ], | |
195 | 'L2TP' => [ | |
196 | { action => 'PARAM', proto => 'udp', dport => '1701' }, | |
197 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' }, | |
198 | ], | |
199 | 'LDAP' => [ | |
200 | { action => 'PARAM', proto => 'tcp', dport => '389' }, | |
201 | ], | |
202 | 'LDAPS' => [ | |
203 | { action => 'PARAM', proto => 'tcp', dport => '636' }, | |
204 | ], | |
205 | 'MSNP' => [ | |
206 | { action => 'PARAM', proto => 'tcp', dport => '1863' }, | |
207 | ], | |
208 | 'MSSQL' => [ | |
209 | { action => 'PARAM', proto => 'tcp', dport => '1433' }, | |
210 | ], | |
211 | 'Mail' => [ | |
212 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
213 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
214 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
215 | ], | |
216 | 'Munin' => [ | |
217 | { action => 'PARAM', proto => 'tcp', dport => '4949' }, | |
218 | ], | |
219 | 'MySQL' => [ | |
220 | { action => 'PARAM', proto => 'tcp', dport => '3306' }, | |
221 | ], | |
222 | 'NNTP' => [ | |
223 | { action => 'PARAM', proto => 'tcp', dport => '119' }, | |
224 | ], | |
225 | 'NNTPS' => [ | |
226 | { action => 'PARAM', proto => 'tcp', dport => '563' }, | |
227 | ], | |
228 | 'NTP' => [ | |
229 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
230 | ], | |
231 | 'NTPbi' => [ | |
232 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
233 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' }, | |
234 | ], | |
235 | 'NTPbrd' => [ | |
236 | { action => 'PARAM', proto => 'udp', dport => '123' }, | |
237 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '123' }, | |
238 | ], | |
239 | 'OSPF' => [ | |
240 | { action => 'PARAM', proto => '89' }, | |
241 | ], | |
242 | 'OpenVPN' => [ | |
243 | { action => 'PARAM', proto => 'udp', dport => '1194' }, | |
244 | ], | |
245 | 'PCA' => [ | |
246 | { action => 'PARAM', proto => 'udp', dport => '5632' }, | |
247 | { action => 'PARAM', proto => 'tcp', dport => '5631' }, | |
248 | ], | |
249 | 'POP3' => [ | |
250 | { action => 'PARAM', proto => 'tcp', dport => '110' }, | |
251 | ], | |
252 | 'POP3S' => [ | |
253 | { action => 'PARAM', proto => 'tcp', dport => '995' }, | |
254 | ], | |
255 | 'PPtP' => [ | |
256 | { action => 'PARAM', proto => '47' }, | |
257 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' }, | |
258 | { action => 'PARAM', proto => 'tcp', dport => '1723' }, | |
259 | ], | |
260 | 'Ping' => [ | |
261 | { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, | |
262 | ], | |
263 | 'PostgreSQL' => [ | |
264 | { action => 'PARAM', proto => 'tcp', dport => '5432' }, | |
265 | ], | |
266 | 'Printer' => [ | |
267 | { action => 'PARAM', proto => 'tcp', dport => '515' }, | |
268 | ], | |
269 | 'RDP' => [ | |
270 | { action => 'PARAM', proto => 'tcp', dport => '3389' }, | |
271 | ], | |
272 | 'RIPbi' => [ | |
273 | { action => 'PARAM', proto => 'udp', dport => '520' }, | |
274 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' }, | |
275 | ], | |
276 | 'RNDC' => [ | |
277 | { action => 'PARAM', proto => 'tcp', dport => '953' }, | |
278 | ], | |
279 | 'Razor' => [ | |
280 | { action => 'ACCEPT', proto => 'tcp', dport => '2703' }, | |
281 | ], | |
282 | 'Rdate' => [ | |
283 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
284 | ], | |
285 | 'Rsync' => [ | |
286 | { action => 'PARAM', proto => 'tcp', dport => '873' }, | |
287 | ], | |
288 | 'SANE' => [ | |
289 | { action => 'PARAM', proto => 'tcp', dport => '6566' }, | |
290 | ], | |
291 | 'SMB' => [ | |
292 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
293 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
294 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
295 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
296 | ], | |
297 | 'SMBBI' => [ | |
298 | { action => 'PARAM', proto => 'udp', dport => '135,445' }, | |
299 | { action => 'PARAM', proto => 'udp', dport => '137:139' }, | |
300 | { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
301 | { action => 'PARAM', proto => 'tcp', dport => '135,139,445' }, | |
302 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' }, | |
303 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' }, | |
304 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' }, | |
305 | { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' }, | |
306 | ], | |
307 | 'SMBswat' => [ | |
308 | { action => 'PARAM', proto => 'tcp', dport => '901' }, | |
309 | ], | |
310 | 'SMTP' => [ | |
311 | { action => 'PARAM', proto => 'tcp', dport => '25' }, | |
312 | ], | |
313 | 'SMTPS' => [ | |
314 | { action => 'PARAM', proto => 'tcp', dport => '465' }, | |
315 | ], | |
316 | 'SNMP' => [ | |
317 | { action => 'PARAM', proto => 'udp', dport => '161:162' }, | |
318 | { action => 'PARAM', proto => 'tcp', dport => '161' }, | |
319 | ], | |
320 | 'SPAMD' => [ | |
321 | { action => 'PARAM', proto => 'tcp', dport => '783' }, | |
322 | ], | |
323 | 'SSH' => [ | |
324 | { action => 'PARAM', proto => 'tcp', dport => '22' }, | |
325 | ], | |
326 | 'SVN' => [ | |
327 | { action => 'PARAM', proto => 'tcp', dport => '3690' }, | |
328 | ], | |
329 | 'SixXS' => [ | |
330 | { action => 'PARAM', proto => 'tcp', dport => '3874' }, | |
331 | { action => 'PARAM', proto => 'udp', dport => '3740' }, | |
332 | { action => 'PARAM', proto => '41' }, | |
333 | { action => 'PARAM', proto => 'udp', dport => '5072,8374' }, | |
334 | ], | |
335 | 'Squid' => [ | |
336 | { action => 'PARAM', proto => 'tcp', dport => '3128' }, | |
337 | ], | |
338 | 'Submission' => [ | |
339 | { action => 'PARAM', proto => 'tcp', dport => '587' }, | |
340 | ], | |
341 | 'Syslog' => [ | |
342 | { action => 'PARAM', proto => 'udp', dport => '514' }, | |
343 | { action => 'PARAM', proto => 'tcp', dport => '514' }, | |
344 | ], | |
345 | 'TFTP' => [ | |
346 | { action => 'PARAM', proto => 'udp', dport => '69' }, | |
347 | ], | |
348 | 'Telnet' => [ | |
349 | { action => 'PARAM', proto => 'tcp', dport => '23' }, | |
350 | ], | |
351 | 'Telnets' => [ | |
352 | { action => 'PARAM', proto => 'tcp', dport => '992' }, | |
353 | ], | |
354 | 'Time' => [ | |
355 | { action => 'PARAM', proto => 'tcp', dport => '37' }, | |
356 | ], | |
357 | 'Trcrt' => [ | |
358 | { action => 'PARAM', proto => 'udp', dport => '33434:33524' }, | |
359 | { action => 'PARAM', proto => 'icmp', dport => 'echo-request' }, | |
360 | ], | |
361 | 'VNC' => [ | |
362 | { action => 'PARAM', proto => 'tcp', dport => '5900:5909' }, | |
363 | ], | |
364 | 'VNCL' => [ | |
365 | { action => 'PARAM', proto => 'tcp', dport => '5500' }, | |
366 | ], | |
367 | 'Web' => [ | |
368 | { action => 'PARAM', proto => 'tcp', dport => '80' }, | |
369 | { action => 'PARAM', proto => 'tcp', dport => '443' }, | |
370 | ], | |
371 | 'Webcache' => [ | |
372 | { action => 'PARAM', proto => 'tcp', dport => '8080' }, | |
373 | ], | |
374 | 'Webmin' => [ | |
375 | { action => 'PARAM', proto => 'tcp', dport => '10000' }, | |
376 | ], | |
377 | 'Whois' => [ | |
378 | { action => 'PARAM', proto => 'tcp', dport => '43' }, | |
379 | ], | |
380 | }; | |
381 | ||
382 | my $pve_fw_parsed_macros; | |
383 | my $pve_fw_preferred_macro_names = {}; | |
384 | ||
385 | my $pve_std_chains = { | |
386 | 'PVEFW-SET-ACCEPT-MARK' => [ | |
387 | "-j MARK --set-mark 1", | |
388 | ], | |
389 | 'PVEFW-DropBroadcast' => [ | |
390 | # same as shorewall 'Broadcast' | |
391 | # simply DROP BROADCAST/MULTICAST/ANYCAST | |
392 | # we can use this to reduce logging | |
393 | { action => 'DROP', dsttype => 'BROADCAST' }, | |
394 | { action => 'DROP', dsttype => 'MULTICAST' }, | |
395 | { action => 'DROP', dsttype => 'ANYCAST' }, | |
396 | { action => 'DROP', dest => '224.0.0.0/4' }, | |
397 | ], | |
398 | 'PVEFW-reject' => [ | |
399 | # same as shorewall 'reject' | |
400 | { action => 'DROP', dsttype => 'BROADCAST' }, | |
401 | { action => 'DROP', source => '224.0.0.0/4' }, | |
402 | { action => 'DROP', proto => 'icmp' }, | |
403 | "-p tcp -j REJECT --reject-with tcp-reset", | |
404 | "-p udp -j REJECT --reject-with icmp-port-unreachable", | |
405 | "-p icmp -j REJECT --reject-with icmp-host-unreachable", | |
406 | "-j REJECT --reject-with icmp-host-prohibited", | |
407 | ], | |
408 | 'PVEFW-Drop' => [ | |
409 | # same as shorewall 'Drop', which is equal to DROP, | |
410 | # but REJECT/DROP some packages to reduce logging, | |
411 | # and ACCEPT critical ICMP types | |
412 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
413 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
414 | { action => 'PVEFW-DropBroadcast' }, | |
415 | # ACCEPT critical ICMP types | |
416 | { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, | |
417 | { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, | |
418 | # Drop packets with INVALID state | |
419 | "-m conntrack --ctstate INVALID -j DROP", | |
420 | # Drop Microsoft SMB noise | |
421 | { action => 'DROP', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
422 | { action => 'DROP', proto => 'udp', dport => '137:139'}, | |
423 | { action => 'DROP', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
424 | { action => 'DROP', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
425 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
426 | # Drop new/NotSyn traffic so that it doesn't get logged | |
427 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
428 | # Drop DNS replies | |
429 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
430 | ], | |
431 | 'PVEFW-Reject' => [ | |
432 | # same as shorewall 'Reject', which is equal to Reject, | |
433 | # but REJECT/DROP some packages to reduce logging, | |
434 | # and ACCEPT critical ICMP types | |
435 | { action => 'PVEFW-reject', proto => 'tcp', dport => '43' }, # REJECT 'auth' | |
436 | # we are not interested in BROADCAST/MULTICAST/ANYCAST | |
437 | { action => 'PVEFW-DropBroadcast' }, | |
438 | # ACCEPT critical ICMP types | |
439 | { action => 'ACCEPT', proto => 'icmp', dport => 'fragmentation-needed' }, | |
440 | { action => 'ACCEPT', proto => 'icmp', dport => 'time-exceeded' }, | |
441 | # Drop packets with INVALID state | |
442 | "-m conntrack --ctstate INVALID -j DROP", | |
443 | # Drop Microsoft SMB noise | |
444 | { action => 'PVEFW-reject', proto => 'udp', dport => '135,445', nbdport => 2 }, | |
445 | { action => 'PVEFW-reject', proto => 'udp', dport => '137:139'}, | |
446 | { action => 'PVEFW-reject', proto => 'udp', dport => '1024:65535', sport => 137 }, | |
447 | { action => 'PVEFW-reject', proto => 'tcp', dport => '135,139,445', nbdport => 3 }, | |
448 | { action => 'DROP', proto => 'udp', dport => 1900 }, # UPnP | |
449 | # Drop new/NotSyn traffic so that it doesn't get logged | |
450 | "-p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP", | |
451 | # Drop DNS replies | |
452 | { action => 'DROP', proto => 'udp', sport => 53 }, | |
453 | ], | |
454 | 'PVEFW-tcpflags' => [ | |
455 | # same as shorewall tcpflags action. | |
456 | # Packets arriving on this interface are checked for som illegal combinations of TCP flags | |
457 | "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g PVEFW-logflags", | |
458 | "-p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g PVEFW-logflags", | |
459 | "-p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g PVEFW-logflags", | |
460 | "-p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g PVEFW-logflags", | |
461 | "-p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g PVEFW-logflags", | |
462 | ], | |
463 | 'PVEFW-smurfs' => [ | |
464 | # same as shorewall smurfs action | |
465 | # Filter packets for smurfs (packets with a broadcast address as the source). | |
466 | "-s 0.0.0.0/32 -j RETURN", | |
467 | "-m addrtype --src-type BROADCAST -g PVEFW-smurflog", | |
468 | "-s 224.0.0.0/4 -g PVEFW-smurflog", | |
469 | ], | |
470 | }; | |
471 | ||
472 | # iptables -p icmp -h | |
473 | my $icmp_type_names = { | |
474 | any => 1, | |
475 | 'echo-reply' => 1, | |
476 | 'destination-unreachable' => 1, | |
477 | 'network-unreachable' => 1, | |
478 | 'host-unreachable' => 1, | |
479 | 'protocol-unreachable' => 1, | |
480 | 'port-unreachable' => 1, | |
481 | 'fragmentation-needed' => 1, | |
482 | 'source-route-failed' => 1, | |
483 | 'network-unknown' => 1, | |
484 | 'host-unknown' => 1, | |
485 | 'network-prohibited' => 1, | |
486 | 'host-prohibited' => 1, | |
487 | 'TOS-network-unreachable' => 1, | |
488 | 'TOS-host-unreachable' => 1, | |
489 | 'communication-prohibited' => 1, | |
490 | 'host-precedence-violation' => 1, | |
491 | 'precedence-cutoff' => 1, | |
492 | 'source-quench' => 1, | |
493 | 'redirect' => 1, | |
494 | 'network-redirect' => 1, | |
495 | 'host-redirect' => 1, | |
496 | 'TOS-network-redirect' => 1, | |
497 | 'TOS-host-redirect' => 1, | |
498 | 'echo-request' => 1, | |
499 | 'router-advertisement' => 1, | |
500 | 'router-solicitation' => 1, | |
501 | 'time-exceeded' => 1, | |
502 | 'ttl-zero-during-transit' => 1, | |
503 | 'ttl-zero-during-reassembly' => 1, | |
504 | 'parameter-problem' => 1, | |
505 | 'ip-header-bad' => 1, | |
506 | 'required-option-missing' => 1, | |
507 | 'timestamp-request' => 1, | |
508 | 'timestamp-reply' => 1, | |
509 | 'address-mask-request' => 1, | |
510 | 'address-mask-reply' => 1, | |
511 | }; | |
512 | ||
513 | sub get_firewall_macros { | |
514 | ||
515 | return $pve_fw_parsed_macros if $pve_fw_parsed_macros; | |
516 | ||
517 | $pve_fw_parsed_macros = {}; | |
518 | ||
519 | foreach my $k (keys %$pve_fw_macros) { | |
520 | my $lc_name = lc($k); | |
521 | my $macro = $pve_fw_macros->{$k}; | |
522 | $pve_fw_preferred_macro_names->{$lc_name} = $k; | |
523 | $pve_fw_parsed_macros->{$k} = $macro; | |
524 | } | |
525 | ||
526 | return $pve_fw_parsed_macros; | |
527 | } | |
528 | ||
529 | my $etc_services; | |
530 | ||
531 | sub get_etc_services { | |
532 | ||
533 | return $etc_services if $etc_services; | |
534 | ||
535 | my $filename = "/etc/services"; | |
536 | ||
537 | my $fh = IO::File->new($filename, O_RDONLY); | |
538 | if (!$fh) { | |
539 | warn "unable to read '$filename' - $!\n"; | |
540 | return {}; | |
541 | } | |
542 | ||
543 | my $services = {}; | |
544 | ||
545 | while (my $line = <$fh>) { | |
546 | chomp ($line); | |
547 | next if $line =~m/^#/; | |
548 | next if ($line =~m/^\s*$/); | |
549 | ||
550 | if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) { | |
551 | $services->{byid}->{$2}->{name} = $1; | |
552 | $services->{byid}->{$2}->{port} = $2; | |
553 | $services->{byid}->{$2}->{$3} = 1; | |
554 | $services->{byname}->{$1} = $services->{byid}->{$2}; | |
555 | } | |
556 | } | |
557 | ||
558 | close($fh); | |
559 | ||
560 | $etc_services = $services; | |
561 | ||
562 | ||
563 | return $etc_services; | |
564 | } | |
565 | ||
566 | my $etc_protocols; | |
567 | ||
568 | sub get_etc_protocols { | |
569 | return $etc_protocols if $etc_protocols; | |
570 | ||
571 | my $filename = "/etc/protocols"; | |
572 | ||
573 | my $fh = IO::File->new($filename, O_RDONLY); | |
574 | if (!$fh) { | |
575 | warn "unable to read '$filename' - $!\n"; | |
576 | return {}; | |
577 | } | |
578 | ||
579 | my $protocols = {}; | |
580 | ||
581 | while (my $line = <$fh>) { | |
582 | chomp ($line); | |
583 | next if $line =~m/^#/; | |
584 | next if ($line =~m/^\s*$/); | |
585 | ||
586 | if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) { | |
587 | $protocols->{byid}->{$2}->{name} = $1; | |
588 | $protocols->{byname}->{$1} = $protocols->{byid}->{$2}; | |
589 | } | |
590 | } | |
591 | ||
592 | close($fh); | |
593 | ||
594 | $etc_protocols = $protocols; | |
595 | ||
596 | return $etc_protocols; | |
597 | } | |
598 | ||
599 | sub parse_address_list { | |
600 | my ($str) = @_; | |
601 | ||
602 | my $nbaor = 0; | |
603 | foreach my $aor (split(/,/, $str)) { | |
604 | if (!Net::IP->new($aor)) { | |
605 | my $err = Net::IP::Error(); | |
606 | die "invalid IP address: $err\n"; | |
607 | }else{ | |
608 | $nbaor++; | |
609 | } | |
610 | } | |
611 | return $nbaor; | |
612 | } | |
613 | ||
614 | sub parse_port_name_number_or_range { | |
615 | my ($str) = @_; | |
616 | ||
617 | my $services = PVE::Firewall::get_etc_services(); | |
618 | my $nbports = 0; | |
619 | foreach my $item (split(/,/, $str)) { | |
620 | my $portlist = ""; | |
621 | my $oldpon = undef; | |
622 | $nbports++; | |
623 | foreach my $pon (split(':', $item, 2)) { | |
624 | $pon = $services->{byname}->{$pon}->{port} if $services->{byname}->{$pon}->{port}; | |
625 | if ($pon =~ m/^\d+$/){ | |
626 | die "invalid port '$pon'\n" if $pon < 0 && $pon > 65535; | |
627 | die "port '$pon' must be bigger than port '$oldpon' \n" if $oldpon && ($pon < $oldpon); | |
628 | $oldpon = $pon; | |
629 | }else{ | |
630 | die "invalid port $services->{byname}->{$pon}\n" if !$services->{byname}->{$pon}; | |
631 | } | |
632 | } | |
633 | } | |
634 | ||
635 | return ($nbports); | |
636 | } | |
637 | ||
638 | # helper function for API | |
639 | ||
640 | my $rule_properties = { | |
641 | pos => { | |
642 | description => "Update rule at position <pos>.", | |
643 | type => 'integer', | |
644 | minimum => 0, | |
645 | optional => 1, | |
646 | }, | |
647 | digest => { | |
648 | type => 'string', | |
649 | optional => 1, | |
650 | }, | |
651 | type => { | |
652 | type => 'string', | |
653 | optional => 1, | |
654 | enum => ['in', 'out', 'group'], | |
655 | }, | |
656 | action => { | |
657 | type => 'string', | |
658 | optional => 1, | |
659 | }, | |
660 | macro => { | |
661 | type => 'string', | |
662 | optional => 1, | |
663 | }, | |
664 | source => { | |
665 | type => 'string', | |
666 | optional => 1, | |
667 | }, | |
668 | dest => { | |
669 | type => 'string', | |
670 | optional => 1, | |
671 | }, | |
672 | proto => { | |
673 | type => 'string', | |
674 | optional => 1, | |
675 | }, | |
676 | enable => { | |
677 | type => 'boolean', | |
678 | optional => 1, | |
679 | }, | |
680 | sport => { | |
681 | type => 'string', | |
682 | optional => 1, | |
683 | }, | |
684 | dport => { | |
685 | type => 'string', | |
686 | optional => 1, | |
687 | }, | |
688 | comment => { | |
689 | type => 'string', | |
690 | optional => 1, | |
691 | }, | |
692 | }; | |
693 | ||
694 | sub cleanup_fw_rule { | |
695 | my ($rule, $digest, $pos) = @_; | |
696 | ||
697 | my $r = {}; | |
698 | ||
699 | foreach my $k (keys %$rule) { | |
700 | next if !$rule_properties->{$k}; | |
701 | my $v = $rule->{$k}; | |
702 | next if !defined($v); | |
703 | $r->{$k} = $v; | |
704 | $r->{digest} = $digest; | |
705 | $r->{pos} = $pos; | |
706 | } | |
707 | ||
708 | return $r; | |
709 | } | |
710 | ||
711 | sub add_rule_properties { | |
712 | my ($properties) = @_; | |
713 | ||
714 | foreach my $k (keys %$rule_properties) { | |
715 | $properties->{$k} = $rule_properties->{$k}; | |
716 | } | |
717 | ||
718 | return $properties; | |
719 | } | |
720 | ||
721 | sub copy_rule_data { | |
722 | my ($rule, $param) = @_; | |
723 | ||
724 | foreach my $k (keys %$rule_properties) { | |
725 | if (defined(my $v = $param->{$k})) { | |
726 | if ($v eq '' || $v eq '-') { | |
727 | delete $rule->{$k}; | |
728 | } else { | |
729 | $rule->{$k} = $v; | |
730 | } | |
731 | } else { | |
732 | delete $rule->{$k}; | |
733 | } | |
734 | } | |
735 | return $rule; | |
736 | } | |
737 | ||
738 | # core functions | |
739 | my $bridge_firewall_enabled = 0; | |
740 | ||
741 | sub enable_bridge_firewall { | |
742 | ||
743 | return if $bridge_firewall_enabled; # only once | |
744 | ||
745 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-iptables", "1"); | |
746 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1"); | |
747 | ||
748 | # make sure syncookies are enabled (which is default on newer 3.X kernels anyways) | |
749 | PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/tcp_syncookies", "1"); | |
750 | ||
751 | $bridge_firewall_enabled = 1; | |
752 | } | |
753 | ||
754 | my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n"; | |
755 | ||
756 | sub iptables { | |
757 | my ($cmd) = @_; | |
758 | ||
759 | run_command("/sbin/iptables $cmd", outfunc => sub {}, errfunc => sub {}); | |
760 | } | |
761 | ||
762 | sub iptables_restore_cmdlist { | |
763 | my ($cmdlist) = @_; | |
764 | ||
765 | run_command("/sbin/iptables-restore -n", input => $cmdlist); | |
766 | } | |
767 | ||
768 | sub iptables_get_chains { | |
769 | ||
770 | my $res = {}; | |
771 | ||
772 | # check what chains we want to track | |
773 | my $is_pvefw_chain = sub { | |
774 | my $name = shift; | |
775 | ||
776 | return 1 if $name =~ m/^PVEFW-\S+$/; | |
777 | ||
778 | return 1 if $name =~ m/^tap\d+i\d+-(:?IN|OUT)$/; | |
779 | ||
780 | return 1 if $name =~ m/^veth\d+.\d+-(:?IN|OUT)$/; # fixme: dev name is configurable | |
781 | ||
782 | return 1 if $name =~ m/^venet0-\d+-(:?IN|OUT)$/; | |
783 | ||
784 | return 1 if $name =~ m/^vmbr\d+-(:?FW|IN|OUT|IPS)$/; | |
785 | return 1 if $name =~ m/^GROUP-(:?[^\s\-]+)-(:?IN|OUT)$/; | |
786 | ||
787 | return undef; | |
788 | }; | |
789 | ||
790 | my $table = ''; | |
791 | ||
792 | my $parser = sub { | |
793 | my $line = shift; | |
794 | ||
795 | return if $line =~ m/^#/; | |
796 | return if $line =~ m/^\s*$/; | |
797 | ||
798 | if ($line =~ m/^\*(\S+)$/) { | |
799 | $table = $1; | |
800 | return; | |
801 | } | |
802 | ||
803 | return if $table ne 'filter'; | |
804 | ||
805 | if ($line =~ m/^:(\S+)\s/) { | |
806 | my $chain = $1; | |
807 | return if !&$is_pvefw_chain($chain); | |
808 | $res->{$chain} = "unknown"; | |
809 | } elsif ($line =~ m/^-A\s+(\S+)\s.*--comment\s+\"PVESIG:(\S+)\"/) { | |
810 | my ($chain, $sig) = ($1, $2); | |
811 | return if !&$is_pvefw_chain($chain); | |
812 | $res->{$chain} = $sig; | |
813 | } else { | |
814 | # simply ignore the rest | |
815 | return; | |
816 | } | |
817 | }; | |
818 | ||
819 | run_command("/sbin/iptables-save", outfunc => $parser); | |
820 | ||
821 | return $res; | |
822 | } | |
823 | ||
824 | sub iptables_chain_exist { | |
825 | my ($chain) = @_; | |
826 | ||
827 | eval{ | |
828 | iptables("-n --list $chain"); | |
829 | }; | |
830 | return undef if $@; | |
831 | ||
832 | return 1; | |
833 | } | |
834 | ||
835 | sub iptables_rule_exist { | |
836 | my ($rule) = @_; | |
837 | ||
838 | eval{ | |
839 | iptables("-C $rule"); | |
840 | }; | |
841 | return undef if $@; | |
842 | ||
843 | return 1; | |
844 | } | |
845 | ||
846 | sub ruleset_generate_cmdstr { | |
847 | my ($ruleset, $chain, $rule, $actions, $goto) = @_; | |
848 | ||
849 | return if defined($rule->{enable}) && !$rule->{enable}; | |
850 | ||
851 | die "unable to emit macro - internal error" if $rule->{macro}; # should not happen | |
852 | ||
853 | my $nbdport = defined($rule->{dport}) ? parse_port_name_number_or_range($rule->{dport}) : 0; | |
854 | my $nbsport = defined($rule->{sport}) ? parse_port_name_number_or_range($rule->{sport}) : 0; | |
855 | my $nbsource = $rule->{source} ? parse_address_list( $rule->{source}) : 0; | |
856 | my $nbdest = $rule->{dest} ? parse_address_list($rule->{dest}) : 0; | |
857 | ||
858 | my @cmd = (); | |
859 | ||
860 | push @cmd, "-i $rule->{iface_in}" if $rule->{iface_in}; | |
861 | push @cmd, "-o $rule->{iface_out}" if $rule->{iface_out}; | |
862 | ||
863 | push @cmd, "-m iprange --src-range" if $nbsource > 1; | |
864 | push @cmd, "-s $rule->{source}" if $rule->{source}; | |
865 | push @cmd, "-m iprange --dst-range" if $nbdest > 1; | |
866 | push @cmd, "-d $rule->{dest}" if $rule->{dest}; | |
867 | ||
868 | if ($rule->{proto}) { | |
869 | push @cmd, "-p $rule->{proto}"; | |
870 | ||
871 | my $multiport = 0; | |
872 | $multiport++ if $nbdport > 1; | |
873 | $multiport++ if $nbsport > 1; | |
874 | ||
875 | push @cmd, "--match multiport" if $multiport; | |
876 | ||
877 | die "multiport: option '--sports' cannot be used together with '--dports'\n" | |
878 | if ($multiport == 2) && ($rule->{dport} ne $rule->{sport}); | |
879 | ||
880 | if ($rule->{dport}) { | |
881 | if ($rule->{proto} && $rule->{proto} eq 'icmp') { | |
882 | # Note: we use dport to store --icmp-type | |
883 | die "unknown icmp-type '$rule->{dport}'\n" if !defined($icmp_type_names->{$rule->{dport}}); | |
884 | push @cmd, "-m icmp --icmp-type $rule->{dport}"; | |
885 | } else { | |
886 | if ($nbdport > 1) { | |
887 | if ($multiport == 2) { | |
888 | push @cmd, "--ports $rule->{dport}"; | |
889 | } else { | |
890 | push @cmd, "--dports $rule->{dport}"; | |
891 | } | |
892 | } else { | |
893 | push @cmd, "--dport $rule->{dport}"; | |
894 | } | |
895 | } | |
896 | } | |
897 | ||
898 | if ($rule->{sport}) { | |
899 | if ($nbsport > 1) { | |
900 | push @cmd, "--sports $rule->{sport}" if $multiport != 2; | |
901 | } else { | |
902 | push @cmd, "--sport $rule->{sport}"; | |
903 | } | |
904 | } | |
905 | } elsif ($rule->{dport} || $rule->{sport}) { | |
906 | warn "ignoring destination port '$rule->{dport}' - no protocol specified\n" if $rule->{dport}; | |
907 | warn "ignoring source port '$rule->{sport}' - no protocol specified\n" if $rule->{sport}; | |
908 | } | |
909 | ||
910 | push @cmd, "-m addrtype --dst-type $rule->{dsttype}" if $rule->{dsttype}; | |
911 | ||
912 | if (my $action = $rule->{action}) { | |
913 | $action = $actions->{$action} if defined($actions->{$action}); | |
914 | $goto = 1 if !defined($goto) && $action eq 'PVEFW-SET-ACCEPT-MARK'; | |
915 | push @cmd, $goto ? "-g $action" : "-j $action"; | |
916 | } | |
917 | ||
918 | return scalar(@cmd) ? join(' ', @cmd) : undef; | |
919 | } | |
920 | ||
921 | my $apply_macro = sub { | |
922 | my ($macro_name, $param) = @_; | |
923 | ||
924 | my $macro_rules = $pve_fw_parsed_macros->{$macro_name}; | |
925 | die "unknown macro '$macro_name'\n" if !$macro_rules; # should not happen | |
926 | ||
927 | my $rules = []; | |
928 | ||
929 | foreach my $templ (@$macro_rules) { | |
930 | my $rule = {}; | |
931 | my $param_used = {}; | |
932 | foreach my $k (keys %$templ) { | |
933 | my $v = $templ->{$k}; | |
934 | if ($v eq 'PARAM') { | |
935 | $v = $param->{$k}; | |
936 | $param_used->{$k} = 1; | |
937 | } elsif ($v eq 'DEST') { | |
938 | $v = $param->{dest}; | |
939 | $param_used->{dest} = 1; | |
940 | } elsif ($v eq 'SOURCE') { | |
941 | $v = $param->{source}; | |
942 | $param_used->{source} = 1; | |
943 | } | |
944 | ||
945 | die "missing parameter '$k' in macro '$macro_name'\n" if !defined($v); | |
946 | $rule->{$k} = $v; | |
947 | } | |
948 | foreach my $k (keys %$param) { | |
949 | next if $k eq 'macro'; | |
950 | next if !defined($param->{$k}); | |
951 | next if $param_used->{$k}; | |
952 | if (defined($rule->{$k})) { | |
953 | die "parameter '$k' already define in macro (value = '$rule->{$k}')\n" | |
954 | if $rule->{$k} ne $param->{$k}; | |
955 | } else { | |
956 | $rule->{$k} = $param->{$k}; | |
957 | } | |
958 | } | |
959 | push @$rules, $rule; | |
960 | } | |
961 | ||
962 | return $rules; | |
963 | }; | |
964 | ||
965 | sub ruleset_generate_rule { | |
966 | my ($ruleset, $chain, $rule, $actions, $goto) = @_; | |
967 | ||
968 | my $rules; | |
969 | ||
970 | if ($rule->{macro}) { | |
971 | $rules = &$apply_macro($rule->{macro}, $rule); | |
972 | } else { | |
973 | $rules = [ $rule ]; | |
974 | } | |
975 | ||
976 | foreach my $tmp (@$rules) { | |
977 | if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $tmp, $actions, $goto)) { | |
978 | ruleset_addrule($ruleset, $chain, $cmdstr); | |
979 | } | |
980 | } | |
981 | } | |
982 | ||
983 | sub ruleset_generate_rule_insert { | |
984 | my ($ruleset, $chain, $rule, $actions, $goto) = @_; | |
985 | ||
986 | die "implement me" if $rule->{macro}; # not implemented, because not needed so far | |
987 | ||
988 | if (my $cmdstr = ruleset_generate_cmdstr($ruleset, $chain, $rule, $actions, $goto)) { | |
989 | ruleset_insertrule($ruleset, $chain, $cmdstr); | |
990 | } | |
991 | } | |
992 | ||
993 | sub ruleset_create_chain { | |
994 | my ($ruleset, $chain) = @_; | |
995 | ||
996 | die "Invalid chain name '$chain' (28 char max)\n" if length($chain) > 28; | |
997 | die "chain name may not contain collons\n" if $chain =~ m/:/; # because of log format | |
998 | ||
999 | die "chain '$chain' already exists\n" if $ruleset->{$chain}; | |
1000 | ||
1001 | $ruleset->{$chain} = []; | |
1002 | } | |
1003 | ||
1004 | sub ruleset_chain_exist { | |
1005 | my ($ruleset, $chain) = @_; | |
1006 | ||
1007 | return $ruleset->{$chain} ? 1 : undef; | |
1008 | } | |
1009 | ||
1010 | sub ruleset_addrule { | |
1011 | my ($ruleset, $chain, $rule) = @_; | |
1012 | ||
1013 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; | |
1014 | ||
1015 | push @{$ruleset->{$chain}}, "-A $chain $rule"; | |
1016 | } | |
1017 | ||
1018 | sub ruleset_insertrule { | |
1019 | my ($ruleset, $chain, $rule) = @_; | |
1020 | ||
1021 | die "no such chain '$chain'\n" if !$ruleset->{$chain}; | |
1022 | ||
1023 | unshift @{$ruleset->{$chain}}, "-A $chain $rule"; | |
1024 | } | |
1025 | ||
1026 | sub get_log_rule_base { | |
1027 | my ($chain, $vmid, $msg, $loglevel) = @_; | |
1028 | ||
1029 | die "internal error - no log level" if !defined($loglevel); | |
1030 | ||
1031 | $vmid = 0 if !defined($vmid); | |
1032 | ||
1033 | # Note: we use special format for prefix to pass further | |
1034 | # info to log daemon (VMID, LOGVELEL and CHAIN) | |
1035 | ||
1036 | return "-j NFLOG --nflog-prefix \":$vmid:$loglevel:$chain: $msg\""; | |
1037 | } | |
1038 | ||
1039 | sub ruleset_addlog { | |
1040 | my ($ruleset, $chain, $vmid, $msg, $loglevel, $rule) = @_; | |
1041 | ||
1042 | return if !defined($loglevel); | |
1043 | ||
1044 | my $logrule = get_log_rule_base($chain, $vmid, $msg, $loglevel); | |
1045 | ||
1046 | $logrule = "$rule $logrule" if defined($rule); | |
1047 | ||
1048 | ruleset_addrule($ruleset, $chain, $logrule) | |
1049 | } | |
1050 | ||
1051 | sub generate_bridge_chains { | |
1052 | my ($ruleset, $hostfw_conf, $bridge, $routing_table) = @_; | |
1053 | ||
1054 | my $options = $hostfw_conf->{options} || {}; | |
1055 | ||
1056 | die "error: detected direct route to bridge '$bridge'\n" | |
1057 | if !$options->{allow_bridge_route} && $routing_table->{$bridge}; | |
1058 | ||
1059 | if (!ruleset_chain_exist($ruleset, "$bridge-FW")) { | |
1060 | ruleset_create_chain($ruleset, "$bridge-FW"); | |
1061 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o $bridge -m physdev --physdev-is-out -j $bridge-FW"); | |
1062 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i $bridge -m physdev --physdev-is-in -j $bridge-FW"); | |
1063 | } | |
1064 | ||
1065 | if (!ruleset_chain_exist($ruleset, "$bridge-OUT")) { | |
1066 | ruleset_create_chain($ruleset, "$bridge-OUT"); | |
1067 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-in -j $bridge-OUT"); | |
1068 | ruleset_insertrule($ruleset, "PVEFW-INPUT", "-i $bridge -m physdev --physdev-is-in -j $bridge-OUT"); | |
1069 | } | |
1070 | ||
1071 | if (!ruleset_chain_exist($ruleset, "$bridge-IN")) { | |
1072 | ruleset_create_chain($ruleset, "$bridge-IN"); | |
1073 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-out -j $bridge-IN"); | |
1074 | ruleset_addrule($ruleset, "$bridge-FW", "-m mark --mark 1 -j ACCEPT"); | |
1075 | # accept traffic to unmanaged bridge ports | |
1076 | ruleset_addrule($ruleset, "$bridge-FW", "-m physdev --physdev-is-out -j ACCEPT "); | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | sub ruleset_add_chain_policy { | |
1081 | my ($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action) = @_; | |
1082 | ||
1083 | if ($policy eq 'ACCEPT') { | |
1084 | ||
1085 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT' }, | |
1086 | { ACCEPT => $accept_action}); | |
1087 | ||
1088 | } elsif ($policy eq 'DROP') { | |
1089 | ||
1090 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Drop"); | |
1091 | ||
1092 | ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); | |
1093 | ||
1094 | ruleset_addrule($ruleset, $chain, "-j DROP"); | |
1095 | } elsif ($policy eq 'REJECT') { | |
1096 | ruleset_addrule($ruleset, $chain, "-j PVEFW-Reject"); | |
1097 | ||
1098 | ruleset_addlog($ruleset, $chain, $vmid, "policy $policy: ", $loglevel); | |
1099 | ||
1100 | ruleset_addrule($ruleset, $chain, "-g PVEFW-reject"); | |
1101 | } else { | |
1102 | # should not happen | |
1103 | die "internal error: unknown policy '$policy'"; | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | sub ruleset_create_vm_chain { | |
1108 | my ($ruleset, $chain, $options, $macaddr, $direction) = @_; | |
1109 | ||
1110 | ruleset_create_chain($ruleset, $chain); | |
1111 | my $accept = generate_nfqueue($options); | |
1112 | ||
1113 | if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) { | |
1114 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs"); | |
1115 | } | |
1116 | ||
1117 | if (!(defined($options->{dhcp}) && $options->{dhcp} == 0)) { | |
1118 | if ($direction eq 'OUT') { | |
1119 | ruleset_generate_rule($ruleset, $chain, { action => 'PVEFW-SET-ACCEPT-MARK', | |
1120 | proto => 'udp', sport => 68, dport => 67 }); | |
1121 | } else { | |
1122 | ruleset_generate_rule($ruleset, $chain, { action => 'ACCEPT', | |
1123 | proto => 'udp', sport => 67, dport => 68 }); | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | if ($options->{tcpflags}) { | |
1128 | ruleset_addrule($ruleset, $chain, "-p tcp -j PVEFW-tcpflags"); | |
1129 | } | |
1130 | ||
1131 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); | |
1132 | if($direction eq 'OUT'){ | |
1133 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -g PVEFW-SET-ACCEPT-MARK"); | |
1134 | ||
1135 | }else{ | |
1136 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept"); | |
1137 | } | |
1138 | if ($direction eq 'OUT') { | |
1139 | if (defined($macaddr) && !(defined($options->{macfilter}) && $options->{macfilter} == 0)) { | |
1140 | ruleset_addrule($ruleset, $chain, "-m mac ! --mac-source $macaddr -j DROP"); | |
1141 | } | |
1142 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
1143 | } | |
1144 | ||
1145 | ||
1146 | } | |
1147 | ||
1148 | sub ruleset_generate_vm_rules { | |
1149 | my ($ruleset, $rules, $groups_conf, $chain, $netid, $direction, $options) = @_; | |
1150 | ||
1151 | my $lc_direction = lc($direction); | |
1152 | ||
1153 | foreach my $rule (@$rules) { | |
1154 | next if $rule->{iface} && $rule->{iface} ne $netid; | |
1155 | next if !$rule->{enable}; | |
1156 | if ($rule->{type} eq 'group') { | |
1157 | my $group_chain = "GROUP-$rule->{action}-$direction"; | |
1158 | if(!ruleset_chain_exist($ruleset, $group_chain)){ | |
1159 | generate_group_rules($ruleset, $groups_conf, $rule->{action}); | |
1160 | } | |
1161 | ruleset_addrule($ruleset, $chain, "-j $group_chain"); | |
1162 | if ($direction eq 'OUT'){ | |
1163 | ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j RETURN"); | |
1164 | }else{ | |
1165 | my $accept = generate_nfqueue($options); | |
1166 | ruleset_addrule($ruleset, $chain, "-m mark --mark 1 -j $accept"); | |
1167 | } | |
1168 | ||
1169 | } else { | |
1170 | next if $rule->{type} ne $lc_direction; | |
1171 | if ($direction eq 'OUT') { | |
1172 | ruleset_generate_rule($ruleset, $chain, $rule, | |
1173 | { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); | |
1174 | } else { | |
1175 | my $accept = generate_nfqueue($options); | |
1176 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept , REJECT => "PVEFW-reject" }); | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | sub generate_nfqueue { | |
1183 | my ($options) = @_; | |
1184 | ||
1185 | my $action = ""; | |
1186 | if($options->{ips}){ | |
1187 | $action = "NFQUEUE"; | |
1188 | if($options->{ips_queues} && $options->{ips_queues} =~ m/^(\d+)(:(\d+))?$/) { | |
1189 | if(defined($3) && defined($1)) { | |
1190 | $action .= " --queue-balance $1:$3"; | |
1191 | }elsif (defined($1)) { | |
1192 | $action .= " --queue-num $1"; | |
1193 | } | |
1194 | } | |
1195 | $action .= " --queue-bypass"; | |
1196 | }else{ | |
1197 | $action = "ACCEPT"; | |
1198 | } | |
1199 | ||
1200 | return $action; | |
1201 | } | |
1202 | ||
1203 | sub ruleset_generate_vm_ipsrules { | |
1204 | my ($ruleset, $options, $direction, $iface, $bridge) = @_; | |
1205 | ||
1206 | if ($options->{ips} && $direction eq 'IN') { | |
1207 | my $nfqueue = generate_nfqueue($options); | |
1208 | ||
1209 | if (!ruleset_chain_exist($ruleset, "$bridge-IPS")) { | |
1210 | ruleset_create_chain($ruleset, "PVEFW-IPS"); | |
1211 | } | |
1212 | ||
1213 | if (!ruleset_chain_exist($ruleset, "$bridge-IPS")) { | |
1214 | ruleset_create_chain($ruleset, "$bridge-IPS"); | |
1215 | ruleset_insertrule($ruleset, "PVEFW-IPS", "-o $bridge -m physdev --physdev-is-out -j $bridge-IPS"); | |
1216 | } | |
1217 | ||
1218 | ruleset_addrule($ruleset, "$bridge-IPS", "-m physdev --physdev-out $iface --physdev-is-bridged -j $nfqueue"); | |
1219 | } | |
1220 | } | |
1221 | ||
1222 | sub generate_venet_rules_direction { | |
1223 | my ($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, $direction) = @_; | |
1224 | ||
1225 | parse_address_list($ip); # make sure we have a valid $ip list | |
1226 | ||
1227 | my $lc_direction = lc($direction); | |
1228 | ||
1229 | my $rules = $vmfw_conf->{rules}; | |
1230 | ||
1231 | my $options = $vmfw_conf->{options}; | |
1232 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
1233 | ||
1234 | my $chain = "venet0-$vmid-$direction"; | |
1235 | ||
1236 | ruleset_create_vm_chain($ruleset, $chain, $options, undef, $direction); | |
1237 | ||
1238 | ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $chain, 'venet', $direction); | |
1239 | ||
1240 | # implement policy | |
1241 | my $policy; | |
1242 | ||
1243 | if ($direction eq 'OUT') { | |
1244 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default | |
1245 | } else { | |
1246 | $policy = $options->{policy_in} || 'DROP'; # allow nothing by default | |
1247 | } | |
1248 | ||
1249 | my $accept = generate_nfqueue($options); | |
1250 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; | |
1251 | ruleset_add_chain_policy($ruleset, $chain, $vmid, $policy, $loglevel, $accept_action); | |
1252 | ||
1253 | # plug into FORWARD, INPUT and OUTPUT chain | |
1254 | if ($direction eq 'OUT') { | |
1255 | ruleset_generate_rule_insert($ruleset, "PVEFW-FORWARD", { | |
1256 | action => $chain, | |
1257 | source => $ip, | |
1258 | iface_in => 'venet0'}); | |
1259 | ||
1260 | ruleset_generate_rule_insert($ruleset, "PVEFW-INPUT", { | |
1261 | action => $chain, | |
1262 | source => $ip, | |
1263 | iface_in => 'venet0'}); | |
1264 | } else { | |
1265 | ruleset_generate_rule($ruleset, "PVEFW-FORWARD", { | |
1266 | action => $chain, | |
1267 | dest => $ip, | |
1268 | iface_out => 'venet0'}); | |
1269 | ||
1270 | ruleset_generate_rule($ruleset, "PVEFW-OUTPUT", { | |
1271 | action => $chain, | |
1272 | dest => $ip, | |
1273 | iface_out => 'venet0'}); | |
1274 | } | |
1275 | } | |
1276 | ||
1277 | sub generate_tap_rules_direction { | |
1278 | my ($ruleset, $groups_conf, $iface, $netid, $macaddr, $vmfw_conf, $vmid, $bridge, $direction) = @_; | |
1279 | ||
1280 | my $lc_direction = lc($direction); | |
1281 | ||
1282 | my $rules = $vmfw_conf->{rules}; | |
1283 | ||
1284 | my $options = $vmfw_conf->{options}; | |
1285 | my $loglevel = get_option_log_level($options, "log_level_${lc_direction}"); | |
1286 | ||
1287 | my $tapchain = "$iface-$direction"; | |
1288 | ||
1289 | ruleset_create_vm_chain($ruleset, $tapchain, $options, $macaddr, $direction); | |
1290 | ||
1291 | ruleset_generate_vm_rules($ruleset, $rules, $groups_conf, $tapchain, $netid, $direction, $options); | |
1292 | ||
1293 | ruleset_generate_vm_ipsrules($ruleset, $options, $direction, $iface, $bridge); | |
1294 | ||
1295 | # implement policy | |
1296 | my $policy; | |
1297 | ||
1298 | if ($direction eq 'OUT') { | |
1299 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default | |
1300 | } else { | |
1301 | $policy = $options->{policy_in} || 'DROP'; # allow nothing by default | |
1302 | } | |
1303 | ||
1304 | my $accept = generate_nfqueue($options); | |
1305 | my $accept_action = $direction eq 'OUT' ? "PVEFW-SET-ACCEPT-MARK" : $accept; | |
1306 | ruleset_add_chain_policy($ruleset, $tapchain, $vmid, $policy, $loglevel, $accept_action); | |
1307 | ||
1308 | # plug the tap chain to bridge chain | |
1309 | if ($direction eq 'IN') { | |
1310 | ruleset_insertrule($ruleset, "$bridge-IN", | |
1311 | "-m physdev --physdev-is-bridged --physdev-out $iface -j $tapchain"); | |
1312 | } else { | |
1313 | ruleset_insertrule($ruleset, "$bridge-OUT", | |
1314 | "-m physdev --physdev-in $iface -j $tapchain"); | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | sub enable_host_firewall { | |
1319 | my ($ruleset, $hostfw_conf, $groups_conf) = @_; | |
1320 | ||
1321 | # fixme: allow security groups | |
1322 | ||
1323 | my $options = $hostfw_conf->{options}; | |
1324 | my $rules = $hostfw_conf->{rules}; | |
1325 | ||
1326 | # host inbound firewall | |
1327 | my $chain = "PVEFW-HOST-IN"; | |
1328 | ruleset_create_chain($ruleset, $chain); | |
1329 | ||
1330 | my $loglevel = get_option_log_level($options, "log_level_in"); | |
1331 | ||
1332 | if (!(defined($options->{nosmurfs}) && $options->{nosmurfs} == 0)) { | |
1333 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID,NEW -j PVEFW-smurfs"); | |
1334 | } | |
1335 | ||
1336 | if ($options->{tcpflags}) { | |
1337 | ruleset_addrule($ruleset, $chain, "-p tcp -j PVEFW-tcpflags"); | |
1338 | } | |
1339 | ||
1340 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); | |
1341 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
1342 | ruleset_addrule($ruleset, $chain, "-i lo -j ACCEPT"); | |
1343 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
1344 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW --dport 5404:5405 -j ACCEPT"); | |
1345 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync | |
1346 | ||
1347 | # we use RETURN because we need to check also tap rules | |
1348 | my $accept_action = 'RETURN'; | |
1349 | ||
1350 | foreach my $rule (@$rules) { | |
1351 | next if $rule->{type} ne 'in'; | |
1352 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); | |
1353 | } | |
1354 | ||
1355 | # implement input policy | |
1356 | my $policy = $options->{policy_in} || 'DROP'; # allow nothing by default | |
1357 | ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action); | |
1358 | ||
1359 | # host outbound firewall | |
1360 | $chain = "PVEFW-HOST-OUT"; | |
1361 | ruleset_create_chain($ruleset, $chain); | |
1362 | ||
1363 | $loglevel = get_option_log_level($options, "log_level_out"); | |
1364 | ||
1365 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate INVALID -j DROP"); | |
1366 | ruleset_addrule($ruleset, $chain, "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"); | |
1367 | ruleset_addrule($ruleset, $chain, "-o lo -j ACCEPT"); | |
1368 | ruleset_addrule($ruleset, $chain, "-m addrtype --dst-type MULTICAST -j ACCEPT"); | |
1369 | ruleset_addrule($ruleset, $chain, "-p udp -m conntrack --ctstate NEW --dport 5404:5405 -j ACCEPT"); | |
1370 | ruleset_addrule($ruleset, $chain, "-p udp -m udp --dport 9000 -j ACCEPT"); #corosync | |
1371 | ||
1372 | # we use RETURN because we may want to check other thigs later | |
1373 | $accept_action = 'RETURN'; | |
1374 | ||
1375 | foreach my $rule (@$rules) { | |
1376 | next if $rule->{type} ne 'out'; | |
1377 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => $accept_action, REJECT => "PVEFW-reject" }); | |
1378 | } | |
1379 | ||
1380 | # implement output policy | |
1381 | $policy = $options->{policy_out} || 'ACCEPT'; # allow everything by default | |
1382 | ruleset_add_chain_policy($ruleset, $chain, 0, $policy, $loglevel, $accept_action); | |
1383 | ||
1384 | ruleset_addrule($ruleset, "PVEFW-OUTPUT", "-j PVEFW-HOST-OUT"); | |
1385 | ruleset_addrule($ruleset, "PVEFW-INPUT", "-j PVEFW-HOST-IN"); | |
1386 | } | |
1387 | ||
1388 | sub generate_group_rules { | |
1389 | my ($ruleset, $groups_conf, $group) = @_; | |
1390 | die "no such security group '$group'\n" if !$groups_conf->{rules}->{$group}; | |
1391 | ||
1392 | my $rules = $groups_conf->{rules}->{$group}; | |
1393 | ||
1394 | my $chain = "GROUP-${group}-IN"; | |
1395 | ||
1396 | ruleset_create_chain($ruleset, $chain); | |
1397 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
1398 | ||
1399 | foreach my $rule (@$rules) { | |
1400 | next if $rule->{type} ne 'in'; | |
1401 | ruleset_generate_rule($ruleset, $chain, $rule, { ACCEPT => "PVEFW-SET-ACCEPT-MARK", REJECT => "PVEFW-reject" }); | |
1402 | } | |
1403 | ||
1404 | $chain = "GROUP-${group}-OUT"; | |
1405 | ||
1406 | ruleset_create_chain($ruleset, $chain); | |
1407 | ruleset_addrule($ruleset, $chain, "-j MARK --set-mark 0"); # clear mark | |
1408 | ||
1409 | foreach my $rule (@$rules) { | |
1410 | next if $rule->{type} ne 'out'; | |
1411 | # we use PVEFW-SET-ACCEPT-MARK (Instead of ACCEPT) because we need to | |
1412 | # check also other tap rules later | |
1413 | ruleset_generate_rule($ruleset, $chain, $rule, | |
1414 | { ACCEPT => 'PVEFW-SET-ACCEPT-MARK', REJECT => "PVEFW-reject" }); | |
1415 | } | |
1416 | } | |
1417 | ||
1418 | my $MAX_NETS = 32; | |
1419 | my $valid_netdev_names = {}; | |
1420 | for (my $i = 0; $i < $MAX_NETS; $i++) { | |
1421 | $valid_netdev_names->{"net$i"} = 1; | |
1422 | } | |
1423 | ||
1424 | sub parse_fw_rule { | |
1425 | my ($line, $need_iface, $allow_groups) = @_; | |
1426 | ||
1427 | my $macros = get_firewall_macros(); | |
1428 | my $protocols = get_etc_protocols(); | |
1429 | ||
1430 | my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport); | |
1431 | ||
1432 | # we can add single line comments to the end of the rule | |
1433 | my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//; | |
1434 | ||
1435 | # we can disable a rule when prefixed with '|' | |
1436 | my $enable = 1; | |
1437 | ||
1438 | $enable = 0 if $line =~ s/^\|//; | |
1439 | ||
1440 | my @data = split(/\s+/, $line); | |
1441 | my $expected_elements = $need_iface ? 8 : 7; | |
1442 | ||
1443 | die "wrong number of rule elements\n" if scalar(@data) > $expected_elements; | |
1444 | ||
1445 | if ($need_iface) { | |
1446 | ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data | |
1447 | } else { | |
1448 | ($type, $action, $source, $dest, $proto, $dport, $sport) = @data; | |
1449 | } | |
1450 | ||
1451 | die "incomplete rule\n" if ! ($type && $action); | |
1452 | ||
1453 | my $macro; | |
1454 | ||
1455 | $type = lc($type); | |
1456 | ||
1457 | if ($type eq 'in' || $type eq 'out') { | |
1458 | if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) { | |
1459 | # OK | |
1460 | } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) { | |
1461 | $action = $2; | |
1462 | my $preferred_name = $pve_fw_preferred_macro_names->{lc($1)}; | |
1463 | die "unknown macro '$1'\n" if !$preferred_name; | |
1464 | $macro = $preferred_name; | |
1465 | } else { | |
1466 | die "unknown action '$action'\n"; | |
1467 | } | |
1468 | } elsif ($type eq 'group') { | |
1469 | die "wrong number of rule elements\n" if scalar(@data) != 3; | |
1470 | die "groups disabled\n" if !$allow_groups; | |
1471 | ||
1472 | die "invalid characters in group name\n" if $action !~ m/^[A-Za-z0-9_\-]+$/; | |
1473 | } else { | |
1474 | die "unknown rule type '$type'\n"; | |
1475 | } | |
1476 | ||
1477 | if ($need_iface) { | |
1478 | $iface = undef if $iface && $iface eq '-'; | |
1479 | } | |
1480 | ||
1481 | $proto = undef if $proto && $proto eq '-'; | |
1482 | die "unknown protokol '$proto'\n" if $proto && | |
1483 | !(defined($protocols->{byname}->{$proto}) || | |
1484 | defined($protocols->{byid}->{$proto})); | |
1485 | ||
1486 | $source = undef if $source && $source eq '-'; | |
1487 | $dest = undef if $dest && $dest eq '-'; | |
1488 | ||
1489 | $dport = undef if $dport && $dport eq '-'; | |
1490 | $sport = undef if $sport && $sport eq '-'; | |
1491 | ||
1492 | parse_port_name_number_or_range($dport) if defined($dport); | |
1493 | parse_port_name_number_or_range($sport) if defined($sport); | |
1494 | ||
1495 | parse_address_list($source) if $source; | |
1496 | parse_address_list($dest) if $dest; | |
1497 | ||
1498 | return { | |
1499 | type => $type, | |
1500 | enable => $enable, | |
1501 | comment => $comment, | |
1502 | action => $action, | |
1503 | macro => $macro, | |
1504 | iface => $iface, | |
1505 | source => $source, | |
1506 | dest => $dest, | |
1507 | proto => $proto, | |
1508 | dport => $dport, | |
1509 | sport => $sport, | |
1510 | }; | |
1511 | } | |
1512 | ||
1513 | sub parse_vmfw_option { | |
1514 | my ($line) = @_; | |
1515 | ||
1516 | my ($opt, $value); | |
1517 | ||
1518 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
1519 | ||
1520 | if ($line =~ m/^(enable|dhcp|macfilter|nosmurfs|tcpflags|ips):\s*(0|1)\s*$/i) { | |
1521 | $opt = lc($1); | |
1522 | $value = int($2); | |
1523 | } elsif ($line =~ m/^(log_level_in|log_level_out):\s*(($loglevels)\s*)?$/i) { | |
1524 | $opt = lc($1); | |
1525 | $value = $2 ? lc($3) : ''; | |
1526 | } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
1527 | $opt = lc($1); | |
1528 | $value = uc($3); | |
1529 | } elsif ($line =~ m/^(ips_queues):\s*((\d+)(:(\d+))?)\s*$/i) { | |
1530 | $opt = lc($1); | |
1531 | $value = $2; | |
1532 | } else { | |
1533 | chomp $line; | |
1534 | die "can't parse option '$line'\n" | |
1535 | } | |
1536 | ||
1537 | return ($opt, $value); | |
1538 | } | |
1539 | ||
1540 | sub parse_hostfw_option { | |
1541 | my ($line) = @_; | |
1542 | ||
1543 | my ($opt, $value); | |
1544 | ||
1545 | my $loglevels = "emerg|alert|crit|err|warning|notice|info|debug|nolog"; | |
1546 | ||
1547 | if ($line =~ m/^(enable|dhcp|nosmurfs|tcpflags|allow_bridge_route|optimize):\s*(0|1)\s*$/i) { | |
1548 | $opt = lc($1); | |
1549 | $value = int($2); | |
1550 | } elsif ($line =~ m/^(log_level_in|log_level_out|tcp_flags_log_level|smurf_log_level):\s*(($loglevels)\s*)?$/i) { | |
1551 | $opt = lc($1); | |
1552 | $value = $2 ? lc($3) : ''; | |
1553 | } elsif ($line =~ m/^(policy_(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) { | |
1554 | $opt = lc($1); | |
1555 | $value = uc($3); | |
1556 | } elsif ($line =~ m/^(nf_conntrack_max):\s*(\d+)\s*$/i) { | |
1557 | $opt = lc($1); | |
1558 | $value = int($2); | |
1559 | } else { | |
1560 | chomp $line; | |
1561 | die "can't parse option '$line'\n" | |
1562 | } | |
1563 | ||
1564 | return ($opt, $value); | |
1565 | } | |
1566 | ||
1567 | sub parse_vm_fw_rules { | |
1568 | my ($filename, $fh) = @_; | |
1569 | ||
1570 | my $res = { rules => [], options => {}}; | |
1571 | ||
1572 | my $section; | |
1573 | ||
1574 | my $digest = Digest::SHA->new('sha1'); | |
1575 | ||
1576 | while (defined(my $line = <$fh>)) { | |
1577 | $digest->add($line); | |
1578 | ||
1579 | next if $line =~ m/^#/; | |
1580 | next if $line =~ m/^\s*$/; | |
1581 | ||
1582 | my $linenr = $fh->input_line_number(); | |
1583 | my $prefix = "$filename (line $linenr)"; | |
1584 | ||
1585 | if ($line =~ m/^\[(\S+)\]\s*$/i) { | |
1586 | $section = lc($1); | |
1587 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; | |
1588 | next; | |
1589 | } | |
1590 | if (!$section) { | |
1591 | warn "$prefix: skip line - no section"; | |
1592 | next; | |
1593 | } | |
1594 | ||
1595 | next if !$res->{$section}; # skip undefined section | |
1596 | ||
1597 | if ($section eq 'options') { | |
1598 | eval { | |
1599 | my ($opt, $value) = parse_vmfw_option($line); | |
1600 | $res->{options}->{$opt} = $value; | |
1601 | }; | |
1602 | warn "$prefix: $@" if $@; | |
1603 | next; | |
1604 | } | |
1605 | ||
1606 | my $rule; | |
1607 | eval { $rule = parse_fw_rule($line, 1, 1); }; | |
1608 | if (my $err = $@) { | |
1609 | warn "$prefix: $err"; | |
1610 | next; | |
1611 | } | |
1612 | ||
1613 | push @{$res->{$section}}, $rule; | |
1614 | } | |
1615 | ||
1616 | $res->{digest} = $digest->b64digest; | |
1617 | ||
1618 | return $res; | |
1619 | } | |
1620 | ||
1621 | sub parse_host_fw_rules { | |
1622 | my ($filename, $fh) = @_; | |
1623 | ||
1624 | my $res = { rules => [], options => {}}; | |
1625 | ||
1626 | my $section; | |
1627 | ||
1628 | my $digest = Digest::SHA->new('sha1'); | |
1629 | ||
1630 | while (defined(my $line = <$fh>)) { | |
1631 | $digest->add($line); | |
1632 | ||
1633 | next if $line =~ m/^#/; | |
1634 | next if $line =~ m/^\s*$/; | |
1635 | ||
1636 | my $linenr = $fh->input_line_number(); | |
1637 | my $prefix = "$filename (line $linenr)"; | |
1638 | ||
1639 | if ($line =~ m/^\[(\S+)\]\s*$/i) { | |
1640 | $section = lc($1); | |
1641 | warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section}; | |
1642 | next; | |
1643 | } | |
1644 | if (!$section) { | |
1645 | warn "$prefix: skip line - no section"; | |
1646 | next; | |
1647 | } | |
1648 | ||
1649 | next if !$res->{$section}; # skip undefined section | |
1650 | ||
1651 | if ($section eq 'options') { | |
1652 | eval { | |
1653 | my ($opt, $value) = parse_hostfw_option($line); | |
1654 | $res->{options}->{$opt} = $value; | |
1655 | }; | |
1656 | warn "$prefix: $@" if $@; | |
1657 | next; | |
1658 | } | |
1659 | ||
1660 | my $rule; | |
1661 | eval { $rule = parse_fw_rule($line, 1, 1); }; | |
1662 | if (my $err = $@) { | |
1663 | warn "$prefix: $err"; | |
1664 | next; | |
1665 | } | |
1666 | ||
1667 | push @{$res->{$section}}, $rule; | |
1668 | } | |
1669 | ||
1670 | $res->{digest} = $digest->b64digest; | |
1671 | ||
1672 | return $res; | |
1673 | } | |
1674 | ||
1675 | sub parse_group_fw_rules { | |
1676 | my ($filename, $fh) = @_; | |
1677 | ||
1678 | my $section; | |
1679 | my $group; | |
1680 | ||
1681 | my $res = { rules => {} }; | |
1682 | ||
1683 | my $digest = Digest::SHA->new('sha1'); | |
1684 | ||
1685 | while (defined(my $line = <$fh>)) { | |
1686 | $digest->add($line); | |
1687 | ||
1688 | next if $line =~ m/^#/; | |
1689 | next if $line =~ m/^\s*$/; | |
1690 | ||
1691 | my $linenr = $fh->input_line_number(); | |
1692 | my $prefix = "$filename (line $linenr)"; | |
1693 | ||
1694 | if ($line =~ m/^\[group\s+(\S+)\]\s*$/i) { | |
1695 | $section = 'rules'; | |
1696 | $group = lc($1); | |
1697 | next; | |
1698 | } | |
1699 | if (!$section || !$group) { | |
1700 | warn "$prefix: skip line - no section"; | |
1701 | next; | |
1702 | } | |
1703 | ||
1704 | my $rule; | |
1705 | eval { $rule = parse_fw_rule($line, 0, 0); }; | |
1706 | if (my $err = $@) { | |
1707 | warn "$prefix: $err"; | |
1708 | next; | |
1709 | } | |
1710 | ||
1711 | push @{$res->{$section}->{$group}}, $rule; | |
1712 | } | |
1713 | ||
1714 | $res->{digest} = $digest->b64digest; | |
1715 | ||
1716 | return $res; | |
1717 | } | |
1718 | ||
1719 | sub run_locked { | |
1720 | my ($code, @param) = @_; | |
1721 | ||
1722 | my $timeout = 10; | |
1723 | ||
1724 | my $res = lock_file($pve_fw_lock_filename, $timeout, $code, @param); | |
1725 | ||
1726 | die $@ if $@; | |
1727 | ||
1728 | return $res; | |
1729 | } | |
1730 | ||
1731 | sub read_local_vm_config { | |
1732 | ||
1733 | my $openvz = {}; | |
1734 | my $qemu = {}; | |
1735 | ||
1736 | my $vmdata = { openvz => $openvz, qemu => $qemu }; | |
1737 | ||
1738 | my $vmlist = PVE::Cluster::get_vmlist(); | |
1739 | return $vmdata if !$vmlist || !$vmlist->{ids}; | |
1740 | my $ids = $vmlist->{ids}; | |
1741 | ||
1742 | foreach my $vmid (keys %$ids) { | |
1743 | next if !$vmid; # skip VE0 | |
1744 | my $d = $ids->{$vmid}; | |
1745 | next if !$d->{node} || $d->{node} ne $nodename; | |
1746 | next if !$d->{type}; | |
1747 | if ($d->{type} eq 'openvz') { | |
1748 | if ($have_pve_manager) { | |
1749 | my $cfspath = PVE::OpenVZ::cfs_config_path($vmid); | |
1750 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
1751 | $openvz->{$vmid} = $conf; | |
1752 | } | |
1753 | } | |
1754 | } elsif ($d->{type} eq 'qemu') { | |
1755 | if ($have_qemu_server) { | |
1756 | my $cfspath = PVE::QemuServer::cfs_config_path($vmid); | |
1757 | if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) { | |
1758 | $qemu->{$vmid} = $conf; | |
1759 | } | |
1760 | } | |
1761 | } | |
1762 | } | |
1763 | ||
1764 | return $vmdata; | |
1765 | }; | |
1766 | ||
1767 | sub load_vmfw_conf { | |
1768 | my ($vmid) = @_; | |
1769 | ||
1770 | my $vmfw_conf = {}; | |
1771 | ||
1772 | my $filename = "/etc/pve/firewall/$vmid.fw"; | |
1773 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1774 | $vmfw_conf = parse_vm_fw_rules($filename, $fh); | |
1775 | } | |
1776 | ||
1777 | return $vmfw_conf; | |
1778 | } | |
1779 | ||
1780 | sub read_vm_firewall_configs { | |
1781 | my ($vmdata) = @_; | |
1782 | my $vmfw_configs = {}; | |
1783 | ||
1784 | foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) { | |
1785 | my $vmfw_conf = load_vmfw_conf($vmid); | |
1786 | next if !$vmfw_conf->{options}; # skip if file does not exists | |
1787 | $vmfw_configs->{$vmid} = $vmfw_conf; | |
1788 | } | |
1789 | ||
1790 | return $vmfw_configs; | |
1791 | } | |
1792 | ||
1793 | sub get_option_log_level { | |
1794 | my ($options, $k) = @_; | |
1795 | ||
1796 | my $v = $options->{$k}; | |
1797 | $v = $default_log_level if !defined($v); | |
1798 | ||
1799 | return undef if $v eq '' || $v eq 'nolog'; | |
1800 | ||
1801 | $v = $log_level_hash->{$v} if defined($log_level_hash->{$v}); | |
1802 | ||
1803 | return $v if ($v >= 0) && ($v <= 7); | |
1804 | ||
1805 | warn "unknown log level ($k = '$v')\n"; | |
1806 | ||
1807 | return undef; | |
1808 | } | |
1809 | ||
1810 | sub generate_std_chains { | |
1811 | my ($ruleset, $options) = @_; | |
1812 | ||
1813 | my $loglevel = get_option_log_level($options, 'smurf_log_level'); | |
1814 | ||
1815 | # same as shorewall smurflog. | |
1816 | my $chain = 'PVEFW-smurflog'; | |
1817 | ||
1818 | push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; | |
1819 | push @{$pve_std_chains->{$chain}}, "-j DROP"; | |
1820 | ||
1821 | # same as shorewall logflags action. | |
1822 | $loglevel = get_option_log_level($options, 'tcp_flags_log_level'); | |
1823 | $chain = 'PVEFW-logflags'; | |
1824 | # fixme: is this correctly logged by pvewf-logger? (ther is no --log-ip-options for NFLOG) | |
1825 | push @{$pve_std_chains->{$chain}}, get_log_rule_base($chain, 0, "DROP: ", $loglevel) if $loglevel; | |
1826 | push @{$pve_std_chains->{$chain}}, "-j DROP"; | |
1827 | ||
1828 | foreach my $chain (keys %$pve_std_chains) { | |
1829 | ruleset_create_chain($ruleset, $chain); | |
1830 | foreach my $rule (@{$pve_std_chains->{$chain}}) { | |
1831 | if (ref($rule)) { | |
1832 | ruleset_generate_rule($ruleset, $chain, $rule); | |
1833 | } else { | |
1834 | ruleset_addrule($ruleset, $chain, $rule); | |
1835 | } | |
1836 | } | |
1837 | } | |
1838 | } | |
1839 | ||
1840 | sub save_pvefw_status { | |
1841 | my ($status) = @_; | |
1842 | ||
1843 | die "unknown status '$status' - internal error" | |
1844 | if $status !~ m/^(stopped|active)$/; | |
1845 | ||
1846 | mkdir dirname($pve_fw_status_filename); | |
1847 | PVE::Tools::file_set_contents($pve_fw_status_filename, $status); | |
1848 | } | |
1849 | ||
1850 | sub read_pvefw_status { | |
1851 | ||
1852 | my $status = 'unknown'; | |
1853 | ||
1854 | return 'stopped' if ! -f $pve_fw_status_filename; | |
1855 | ||
1856 | eval { | |
1857 | $status = PVE::Tools::file_get_contents($pve_fw_status_filename); | |
1858 | }; | |
1859 | warn $@ if $@; | |
1860 | ||
1861 | return $status; | |
1862 | } | |
1863 | ||
1864 | # fixme: move to pve-common PVE::ProcFSTools | |
1865 | sub read_proc_net_route { | |
1866 | my $filename = "/proc/net/route"; | |
1867 | ||
1868 | my $res = {}; | |
1869 | ||
1870 | my $fh = IO::File->new ($filename, "r"); | |
1871 | return $res if !$fh; | |
1872 | ||
1873 | my $int_to_quad = sub { | |
1874 | return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0); | |
1875 | }; | |
1876 | ||
1877 | while (defined(my $line = <$fh>)) { | |
1878 | next if $line =~/^Iface\s+Destination/; # skip head | |
1879 | my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8]; | |
1880 | push @{$res->{$iface}}, { | |
1881 | dest => &$int_to_quad(hex($dest)), | |
1882 | gateway => &$int_to_quad(hex($gateway)), | |
1883 | mask => &$int_to_quad(hex($mask)), | |
1884 | metric => $metric, | |
1885 | mtu => $mtu, | |
1886 | }; | |
1887 | } | |
1888 | ||
1889 | return $res; | |
1890 | } | |
1891 | ||
1892 | sub load_security_groups { | |
1893 | ||
1894 | my $groups_conf = {}; | |
1895 | my $filename = "/etc/pve/firewall/groups.fw"; | |
1896 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1897 | $groups_conf = parse_group_fw_rules($filename, $fh); | |
1898 | } | |
1899 | ||
1900 | return $groups_conf; | |
1901 | } | |
1902 | ||
1903 | sub save_security_groups { | |
1904 | my ($groups_conf) = @_; | |
1905 | ||
1906 | my $raw = ''; | |
1907 | my $filename = "/etc/pve/firewall/groups.fw"; | |
1908 | ||
1909 | foreach my $group (sort keys %{$groups_conf->{rules}}) { | |
1910 | my $rules = $groups_conf->{rules}->{$group}; | |
1911 | $raw .= "[group $group]\n\n"; | |
1912 | ||
1913 | foreach my $rule (@$rules) { | |
1914 | if ($rule->{type} eq 'in' || $rule->{type} eq 'out') { | |
1915 | $raw .= '|' if defined($rule->{enable}) && !$rule->{enable}; | |
1916 | $raw .= uc($rule->{type}); | |
1917 | $raw .= " " . $rule->{action}; | |
1918 | $raw .= " " . ($rule->{source} || '-'); | |
1919 | $raw .= " " . ($rule->{dest} || '-'); | |
1920 | $raw .= " " . ($rule->{proto} || '-'); | |
1921 | $raw .= " " . ($rule->{dport} || '-'); | |
1922 | $raw .= " " . ($rule->{sport} || '-'); | |
1923 | $raw .= " # " . encode('utf8', $rule->{comment}) | |
1924 | if $rule->{comment} && $rule->{comment} !~ m/^\s*$/; | |
1925 | $raw .= "\n"; | |
1926 | } else { | |
1927 | die "implement me '$rule->{type}'"; | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | $raw .= "\n"; | |
1932 | } | |
1933 | ||
1934 | PVE::Tools::file_set_contents($filename, $raw); | |
1935 | } | |
1936 | ||
1937 | sub load_hostfw_conf { | |
1938 | ||
1939 | my $hostfw_conf = {}; | |
1940 | my $filename = "/etc/pve/local/host.fw"; | |
1941 | if (my $fh = IO::File->new($filename, O_RDONLY)) { | |
1942 | $hostfw_conf = parse_host_fw_rules($filename, $fh); | |
1943 | } | |
1944 | return $hostfw_conf; | |
1945 | } | |
1946 | ||
1947 | sub compile { | |
1948 | my $vmdata = read_local_vm_config(); | |
1949 | my $vmfw_configs = read_vm_firewall_configs($vmdata); | |
1950 | ||
1951 | my $routing_table = read_proc_net_route(); | |
1952 | ||
1953 | my $groups_conf = load_security_groups(); | |
1954 | ||
1955 | my $ruleset = {}; | |
1956 | ||
1957 | ruleset_create_chain($ruleset, "PVEFW-INPUT"); | |
1958 | ruleset_create_chain($ruleset, "PVEFW-OUTPUT"); | |
1959 | ||
1960 | ruleset_create_chain($ruleset, "PVEFW-FORWARD"); | |
1961 | ||
1962 | my $hostfw_conf = load_hostfw_conf(); | |
1963 | my $hostfw_options = $hostfw_conf->{options} || {}; | |
1964 | ||
1965 | generate_std_chains($ruleset, $hostfw_options); | |
1966 | ||
1967 | my $hostfw_enable = !(defined($hostfw_options->{enable}) && ($hostfw_options->{enable} == 0)); | |
1968 | ||
1969 | enable_host_firewall($ruleset, $hostfw_conf, $groups_conf) if $hostfw_enable; | |
1970 | ||
1971 | my $ips_enable = undef; | |
1972 | ||
1973 | # generate firewall rules for QEMU VMs | |
1974 | foreach my $vmid (keys %{$vmdata->{qemu}}) { | |
1975 | my $conf = $vmdata->{qemu}->{$vmid}; | |
1976 | my $vmfw_conf = $vmfw_configs->{$vmid}; | |
1977 | next if !$vmfw_conf; | |
1978 | next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); | |
1979 | ||
1980 | $ips_enable = 1 if $vmfw_conf->{options}->{ips}; | |
1981 | ||
1982 | foreach my $netid (keys %$conf) { | |
1983 | next if $netid !~ m/^net(\d+)$/; | |
1984 | my $net = PVE::QemuServer::parse_net($conf->{$netid}); | |
1985 | next if !$net; | |
1986 | my $iface = "tap${vmid}i$1"; | |
1987 | ||
1988 | my $bridge = $net->{bridge}; | |
1989 | next if !$bridge; # fixme: ? | |
1990 | ||
1991 | $bridge .= "v$net->{tag}" if $net->{tag}; | |
1992 | ||
1993 | generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table); | |
1994 | ||
1995 | my $macaddr = $net->{macaddr}; | |
1996 | generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, | |
1997 | $vmfw_conf, $vmid, $bridge, 'IN'); | |
1998 | generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, | |
1999 | $vmfw_conf, $vmid, $bridge, 'OUT'); | |
2000 | } | |
2001 | } | |
2002 | ||
2003 | # generate firewall rules for OpenVZ containers | |
2004 | foreach my $vmid (keys %{$vmdata->{openvz}}) { | |
2005 | my $conf = $vmdata->{openvz}->{$vmid}; | |
2006 | ||
2007 | my $vmfw_conf = $vmfw_configs->{$vmid}; | |
2008 | next if !$vmfw_conf; | |
2009 | next if defined($vmfw_conf->{options}->{enable}) && ($vmfw_conf->{options}->{enable} == 0); | |
2010 | ||
2011 | $ips_enable = 1 if $vmfw_conf->{options}->{ips}; | |
2012 | ||
2013 | if ($conf->{ip_address} && $conf->{ip_address}->{value}) { | |
2014 | my $ip = $conf->{ip_address}->{value}; | |
2015 | generate_venet_rules_direction($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'IN'); | |
2016 | generate_venet_rules_direction($ruleset, $groups_conf, $vmfw_conf, $vmid, $ip, 'OUT'); | |
2017 | } | |
2018 | ||
2019 | if ($conf->{netif} && $conf->{netif}->{value}) { | |
2020 | my $netif = PVE::OpenVZ::parse_netif($conf->{netif}->{value}); | |
2021 | foreach my $netid (keys %$netif) { | |
2022 | my $d = $netif->{$netid}; | |
2023 | my $bridge = $d->{bridge}; | |
2024 | if (!$bridge) { | |
2025 | warn "no bridge device for CT $vmid iface '$netid'\n"; | |
2026 | next; # fixme? | |
2027 | } | |
2028 | ||
2029 | generate_bridge_chains($ruleset, $hostfw_conf, $bridge, $routing_table); | |
2030 | ||
2031 | my $macaddr = $d->{mac}; | |
2032 | my $iface = $d->{host_ifname}; | |
2033 | generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, | |
2034 | $vmfw_conf, $vmid, $bridge, 'IN'); | |
2035 | generate_tap_rules_direction($ruleset, $groups_conf, $iface, $netid, $macaddr, | |
2036 | $vmfw_conf, $vmid, $bridge, 'OUT'); | |
2037 | } | |
2038 | } | |
2039 | } | |
2040 | ||
2041 | if($hostfw_options->{optimize}){ | |
2042 | ||
2043 | my $accept = $ips_enable ? "PVEFW-IPS" : "ACCEPT"; | |
2044 | ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate RELATED,ESTABLISHED -j $accept"); | |
2045 | ruleset_insertrule($ruleset, "PVEFW-FORWARD", "-m conntrack --ctstate INVALID -j DROP"); | |
2046 | } | |
2047 | ||
2048 | # fixme: what log level should we use here? | |
2049 | my $loglevel = get_option_log_level($hostfw_options, "log_level_out"); | |
2050 | ||
2051 | # fixme: should we really block inter-bridge traffic? | |
2052 | ||
2053 | # always allow traffic from containers? | |
2054 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i venet0 -j RETURN"); | |
2055 | ||
2056 | # disable interbridge routing | |
2057 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j PVEFW-Drop"); | |
2058 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j PVEFW-Drop"); | |
2059 | ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-o vmbr+"); | |
2060 | ruleset_addlog($ruleset, "PVEFW-FORWARD", 0, "DROP: ", $loglevel, "-i vmbr+"); | |
2061 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-o vmbr+ -j DROP"); | |
2062 | ruleset_addrule($ruleset, "PVEFW-FORWARD", "-i vmbr+ -j DROP"); | |
2063 | ||
2064 | return wantarray ? ($ruleset, $hostfw_conf) : $ruleset; | |
2065 | } | |
2066 | ||
2067 | sub get_ruleset_status { | |
2068 | my ($ruleset, $verbose) = @_; | |
2069 | ||
2070 | my $active_chains = iptables_get_chains(); | |
2071 | ||
2072 | my $statushash = {}; | |
2073 | ||
2074 | foreach my $chain (sort keys %$ruleset) { | |
2075 | my $digest = Digest::SHA->new('sha1'); | |
2076 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
2077 | $digest->add("$cmd\n"); | |
2078 | } | |
2079 | my $sig = $digest->b64digest; | |
2080 | $statushash->{$chain}->{sig} = $sig; | |
2081 | ||
2082 | my $oldsig = $active_chains->{$chain}; | |
2083 | if (!defined($oldsig)) { | |
2084 | $statushash->{$chain}->{action} = 'create'; | |
2085 | } else { | |
2086 | if ($oldsig eq $sig) { | |
2087 | $statushash->{$chain}->{action} = 'exists'; | |
2088 | } else { | |
2089 | $statushash->{$chain}->{action} = 'update'; | |
2090 | } | |
2091 | } | |
2092 | print "$statushash->{$chain}->{action} $chain ($sig)\n" if $verbose; | |
2093 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
2094 | print "\t$cmd\n" if $verbose; | |
2095 | } | |
2096 | } | |
2097 | ||
2098 | foreach my $chain (sort keys %$active_chains) { | |
2099 | if (!defined($ruleset->{$chain})) { | |
2100 | my $sig = $active_chains->{$chain}; | |
2101 | $statushash->{$chain}->{action} = 'delete'; | |
2102 | $statushash->{$chain}->{sig} = $sig; | |
2103 | print "delete $chain ($sig)\n" if $verbose; | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | return $statushash; | |
2108 | } | |
2109 | ||
2110 | sub print_ruleset { | |
2111 | my ($ruleset) = @_; | |
2112 | ||
2113 | get_ruleset_status($ruleset, 1); | |
2114 | } | |
2115 | ||
2116 | sub print_sig_rule { | |
2117 | my ($chain, $sig) = @_; | |
2118 | ||
2119 | # We just use this to store a SHA1 checksum used to detect changes | |
2120 | return "-A $chain -m comment --comment \"PVESIG:$sig\"\n"; | |
2121 | } | |
2122 | ||
2123 | sub get_rulset_cmdlist { | |
2124 | my ($ruleset, $verbose) = @_; | |
2125 | ||
2126 | my $cmdlist = "*filter\n"; # we pass this to iptables-restore; | |
2127 | ||
2128 | my $statushash = get_ruleset_status($ruleset, $verbose); | |
2129 | ||
2130 | # create missing chains first | |
2131 | foreach my $chain (sort keys %$ruleset) { | |
2132 | my $stat = $statushash->{$chain}; | |
2133 | die "internal error" if !$stat; | |
2134 | next if $stat->{action} ne 'create'; | |
2135 | ||
2136 | $cmdlist .= ":$chain - [0:0]\n"; | |
2137 | } | |
2138 | ||
2139 | my $rule = "INPUT -j PVEFW-INPUT"; | |
2140 | if (!PVE::Firewall::iptables_rule_exist($rule)) { | |
2141 | $cmdlist .= "-A $rule\n"; | |
2142 | } | |
2143 | $rule = "OUTPUT -j PVEFW-OUTPUT"; | |
2144 | if (!PVE::Firewall::iptables_rule_exist($rule)) { | |
2145 | $cmdlist .= "-A $rule\n"; | |
2146 | } | |
2147 | ||
2148 | $rule = "FORWARD -j PVEFW-FORWARD"; | |
2149 | if (!PVE::Firewall::iptables_rule_exist($rule)) { | |
2150 | $cmdlist .= "-A $rule\n"; | |
2151 | } | |
2152 | ||
2153 | foreach my $chain (sort keys %$ruleset) { | |
2154 | my $stat = $statushash->{$chain}; | |
2155 | die "internal error" if !$stat; | |
2156 | ||
2157 | if ($stat->{action} eq 'update' || $stat->{action} eq 'create') { | |
2158 | $cmdlist .= "-F $chain\n"; | |
2159 | foreach my $cmd (@{$ruleset->{$chain}}) { | |
2160 | $cmdlist .= "$cmd\n"; | |
2161 | } | |
2162 | $cmdlist .= print_sig_rule($chain, $stat->{sig}); | |
2163 | } elsif ($stat->{action} eq 'delete') { | |
2164 | die "internal error"; # this should not happen | |
2165 | } elsif ($stat->{action} eq 'exists') { | |
2166 | # do nothing | |
2167 | } else { | |
2168 | die "internal error - unknown status '$stat->{action}'"; | |
2169 | } | |
2170 | } | |
2171 | ||
2172 | foreach my $chain (keys %$statushash) { | |
2173 | next if $statushash->{$chain}->{action} ne 'delete'; | |
2174 | $cmdlist .= "-F $chain\n"; | |
2175 | } | |
2176 | foreach my $chain (keys %$statushash) { | |
2177 | next if $statushash->{$chain}->{action} ne 'delete'; | |
2178 | next if $chain eq 'PVEFW-INPUT'; | |
2179 | next if $chain eq 'PVEFW-OUTPUT'; | |
2180 | next if $chain eq 'PVEFW-FORWARD'; | |
2181 | $cmdlist .= "-X $chain\n"; | |
2182 | } | |
2183 | ||
2184 | $cmdlist .= "COMMIT\n"; | |
2185 | ||
2186 | return $cmdlist; | |
2187 | } | |
2188 | ||
2189 | sub apply_ruleset { | |
2190 | my ($ruleset, $hostfw_conf, $verbose) = @_; | |
2191 | ||
2192 | enable_bridge_firewall(); | |
2193 | ||
2194 | update_nf_conntrack_max($hostfw_conf); | |
2195 | ||
2196 | my $cmdlist = get_rulset_cmdlist($ruleset, $verbose); | |
2197 | ||
2198 | print $cmdlist if $verbose; | |
2199 | ||
2200 | iptables_restore_cmdlist($cmdlist); | |
2201 | ||
2202 | # test: re-read status and check if everything is up to date | |
2203 | my $statushash = get_ruleset_status($ruleset); | |
2204 | ||
2205 | my $errors; | |
2206 | foreach my $chain (sort keys %$ruleset) { | |
2207 | my $stat = $statushash->{$chain}; | |
2208 | if ($stat->{action} ne 'exists') { | |
2209 | warn "unable to update chain '$chain'\n"; | |
2210 | $errors = 1; | |
2211 | } | |
2212 | } | |
2213 | ||
2214 | die "unable to apply firewall changes\n" if $errors; | |
2215 | } | |
2216 | ||
2217 | sub update_nf_conntrack_max { | |
2218 | my ($hostfw_conf) = @_; | |
2219 | ||
2220 | my $max = 65536; # reasonable default | |
2221 | ||
2222 | my $options = $hostfw_conf->{options} || {}; | |
2223 | ||
2224 | if (defined($options->{nf_conntrack_max}) && ($options->{nf_conntrack_max} > $max)) { | |
2225 | $max = $options->{nf_conntrack_max}; | |
2226 | $max = int(($max+ 8191)/8192)*8192; # round to multiples of 8192 | |
2227 | } | |
2228 | ||
2229 | my $filename_nf_conntrack_max = "/proc/sys/net/nf_conntrack_max"; | |
2230 | my $filename_hashsize = "/sys/module/nf_conntrack/parameters/hashsize"; | |
2231 | ||
2232 | my $current = int(PVE::Tools::file_read_firstline($filename_nf_conntrack_max) || $max); | |
2233 | ||
2234 | if ($current != $max) { | |
2235 | my $hashsize = int($max/4); | |
2236 | PVE::ProcFSTools::write_proc_entry($filename_hashsize, $hashsize); | |
2237 | PVE::ProcFSTools::write_proc_entry($filename_nf_conntrack_max, $max); | |
2238 | } | |
2239 | } | |
2240 | ||
2241 | sub update { | |
2242 | my ($start, $verbose) = @_; | |
2243 | ||
2244 | my $code = sub { | |
2245 | my $status = read_pvefw_status(); | |
2246 | ||
2247 | my ($ruleset, $hostfw_conf) = PVE::Firewall::compile(); | |
2248 | ||
2249 | if ($start || $status eq 'active') { | |
2250 | ||
2251 | save_pvefw_status('active') if ($status ne 'active'); | |
2252 | ||
2253 | apply_ruleset($ruleset, $hostfw_conf, $verbose); | |
2254 | } else { | |
2255 | print "Firewall not active (status = $status)\n" if $verbose; | |
2256 | } | |
2257 | }; | |
2258 | ||
2259 | run_locked($code); | |
2260 | } | |
2261 | ||
2262 | ||
2263 | 1; |