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