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