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