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