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