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