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