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