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