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