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