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