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