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