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