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