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