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