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