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