1 package PVE
::Service
::pve_firewall
;
7 use Time
::HiRes qw
(gettimeofday usleep
);
10 use PVE
::Cluster
qw(cfs_read_file);
15 use PVE
::RPCEnvironment
;
17 use PVE
::Tools
qw(dir_glob_foreach file_read_firstline);
20 use PVE
::FirewallSimulator
;
22 use base
qw(PVE::Daemon);
24 my $cmdline = [$0, @ARGV];
26 my %daemon_options = (restart_on_error
=> 5, stop_wait_time
=> 5);
28 my $daemon = __PACKAGE__-
>new('pve-firewall', $cmdline, %daemon_options);
30 my $nodename = PVE
::INotify
::nodename
();
33 PVE
::Cluster
::cfs_update
();
35 PVE
::Firewall
::init
();
38 my $restart_request = 0;
44 my $initial_memory_usage;
49 syslog
('info' , "server closing");
52 1 while (waitpid(-1, POSIX
::WNOHANG
()) > 0);
54 syslog
('info' , "clear firewall rules");
56 eval { PVE
::Firewall
::remove_pvefw_chains
(); };
59 $self->exit_daemon(0);
71 local $SIG{'__WARN__'} = 'IGNORE'; # do not fill up logs
74 $next_update = time() + $updatetime;
76 my ($ccsec, $cusec) = gettimeofday
();
78 PVE
::Cluster
::cfs_update
();
79 PVE
::Firewall
::update
();
84 syslog
('err', "status update error: $err");
87 my ($ccsec_end, $cusec_end) = gettimeofday
();
88 my $cptime = ($ccsec_end-$ccsec) + ($cusec_end - $cusec)/1000000;
90 syslog
('info', sprintf("firewall update time (%.3f seconds)", $cptime))
95 my $mem = PVE
::ProcFSTools
::read_memory_usage
();
97 if (!defined($initial_memory_usage) || ($cycle < 10)) {
98 $initial_memory_usage = $mem->{resident
};
100 my $diff = $mem->{resident
} - $initial_memory_usage;
101 if ($diff > 5*1024*1024) {
102 syslog
('info', "restarting server after $cycle cycles to " .
103 "reduce memory usage (free $mem->{resident} ($diff) bytes)");
104 $self->restart_daemon();
109 while ((time() < $next_update) &&
110 ($wcount < $updatetime) && # protect against time wrap
111 !$restart_request) { $wcount++; sleep (1); };
113 $self->restart_daemon() if $restart_request;
117 $daemon->register_start_command("Start the Proxmox VE firewall service.");
118 $daemon->register_restart_command(1, "Restart the Proxmox VE firewall service.");
119 $daemon->register_stop_command(
120 "Stop the Proxmox VE firewall service. Note, stopping actively removes all Proxmox VE related"
121 ." iptable rules rendering the host potentially unprotected."
124 __PACKAGE__-
>register_method ({
128 description
=> "Get firewall status.",
130 additionalProperties
=> 0,
135 additionalProperties
=> 0,
139 enum
=> ['unknown', 'stopped', 'running'],
142 description
=> "Firewall is enabled (in 'cluster.fw')",
146 description
=> "Set when there are pending changes.",
155 local $SIG{'__WARN__'} = 'DEFAULT'; # do not fill up syslog
159 my $status = $daemon->running() ?
'running' : 'stopped';
161 my $res = { status
=> $status };
163 PVE
::Firewall
::set_verbose
(1); # show syntax errors
165 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
(undef);
166 $res->{enable
} = $cluster_conf->{options
}->{enable
} ?
1 : 0;
168 if ($status eq 'running') {
170 my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = PVE
::Firewall
::compile
($cluster_conf, undef, undef);
172 PVE
::Firewall
::set_verbose
(0); # do not show iptables details
173 my (undef, undef, $ipset_changes) = PVE
::Firewall
::get_ipset_cmdlist
($ipset_ruleset);
174 my ($test, $ruleset_changes) = PVE
::Firewall
::get_ruleset_cmdlist
($ruleset->{filter
});
175 my (undef, $ruleset_changesv6) = PVE
::Firewall
::get_ruleset_cmdlist
($rulesetv6->{filter
}, "ip6tables");
176 my (undef, $ruleset_changes_raw) = PVE
::Firewall
::get_ruleset_cmdlist
($ruleset->{raw
}, undef, 'raw');
177 my (undef, $ruleset_changesv6_raw) = PVE
::Firewall
::get_ruleset_cmdlist
($rulesetv6->{raw
}, "ip6tables", 'raw');
178 my (undef, $ebtables_changes) = PVE
::Firewall
::get_ebtables_cmdlist
($ebtables_ruleset);
180 $res->{changes
} = ($ipset_changes || $ruleset_changes || $ruleset_changesv6 || $ebtables_changes || $ruleset_changes_raw || $ruleset_changesv6_raw) ?
1 : 0;
186 return PVE
::Firewall
::run_locked
($code);
189 __PACKAGE__-
>register_method ({
193 description
=> "Compile and print firewall rules. This is useful for testing.",
195 additionalProperties
=> 0,
198 returns
=> { type
=> 'null' },
203 local $SIG{'__WARN__'} = 'DEFAULT'; # do not fill up syslog
207 PVE
::Firewall
::set_verbose
(1);
209 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
(undef);
210 my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = PVE
::Firewall
::compile
($cluster_conf, undef, undef);
212 print "ipset cmdlist:\n";
213 my (undef, undef, $ipset_changes) = PVE
::Firewall
::get_ipset_cmdlist
($ipset_ruleset);
215 print "\niptables cmdlist:\n";
216 my (undef, $ruleset_changes) = PVE
::Firewall
::get_ruleset_cmdlist
($ruleset->{filter
});
218 print "\nip6tables cmdlist:\n";
219 my (undef, $ruleset_changesv6) = PVE
::Firewall
::get_ruleset_cmdlist
($rulesetv6->{filter
}, "ip6tables");
221 print "\nebtables cmdlist:\n";
222 my (undef, $ebtables_changes) = PVE
::Firewall
::get_ebtables_cmdlist
($ebtables_ruleset);
224 print "\niptables table raw cmdlist:\n";
225 my (undef, $ruleset_changes_raw) = PVE
::Firewall
::get_ruleset_cmdlist
($ruleset->{raw
}, undef, 'raw');
227 print "\nip6tables table raw cmdlist:\n";
228 my (undef, $ruleset_changesv6_raw) = PVE
::Firewall
::get_ruleset_cmdlist
($rulesetv6->{raw
}, "ip6tables", 'raw');
231 if ($ipset_changes || $ruleset_changes || $ruleset_changesv6 || $ebtables_changes || $ruleset_changes_raw || $ruleset_changesv6_raw) {
232 print "detected changes\n";
234 print "no changes\n";
236 if (!$cluster_conf->{options
}->{enable
}) {
237 print "firewall disabled\n";
241 PVE
::Firewall
::run_locked
($code);
246 __PACKAGE__-
>register_method ({
250 description
=> "Print information about local network.",
252 additionalProperties
=> 0,
255 returns
=> { type
=> 'null' },
259 local $SIG{'__WARN__'} = 'DEFAULT'; # do not fill up syslog
261 my $nodename = PVE
::INotify
::nodename
();
262 print "local hostname: $nodename\n";
264 my $ip = PVE
::Cluster
::remote_node_ip
($nodename);
265 print "local IP address: $ip\n";
267 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
269 my $localnet = PVE
::Firewall
::local_network
() || '127.0.0.0/8';
270 print "network auto detect: $localnet\n";
271 if ($cluster_conf->{aliases
}->{local_network
}) {
272 print "using user defined local_network: $cluster_conf->{aliases}->{local_network}->{cidr}\n";
274 print "using detected local_network: $localnet\n";
277 if (PVE
::Corosync
::check_conf_exists
(1)) {
278 my $corosync_conf = PVE
::Cluster
::cfs_read_file
("corosync.conf");
279 my $corosync_node_found = 0;
281 print "\naccepting corosync traffic from/to:\n";
283 PVE
::Corosync
::for_all_corosync_addresses
($corosync_conf, undef, sub {
284 my ($curr_node_name, $curr_node_ip, undef, $key) = @_;
286 return if $curr_node_name eq $nodename;
288 $corosync_node_found = 1;
290 $key =~ m/(?:ring|link)(\d+)_addr/;
291 print " - $curr_node_name: $curr_node_ip (link: $1)\n";
294 if (!$corosync_node_found) {
295 print " - no nodes found\n";
302 __PACKAGE__-
>register_method ({
306 description
=> "Simulate firewall rules. This does not simulates the kernel 'routing' table,"
307 ." but simply assumes that routing from source zone to destination zone is possible.",
309 additionalProperties
=> 0,
312 description
=> "Verbose output.",
318 description
=> "Source zone.",
320 pattern
=> '(host|outside|vm\d+|ct\d+|vmbr\d+/\S+)',
322 default => 'outside',
325 description
=> "Destination zone.",
327 pattern
=> '(host|outside|vm\d+|ct\d+|vmbr\d+/\S+)',
332 description
=> "Protocol.",
334 pattern
=> '(tcp|udp)',
339 description
=> "Destination port.",
346 description
=> "Source port.",
353 description
=> "Source IP address.",
354 type
=> 'string', format
=> 'ipv4',
358 description
=> "Destination IP address.",
359 type
=> 'string', format
=> 'ipv4',
364 returns
=> { type
=> 'null' },
368 local $SIG{'__WARN__'} = 'DEFAULT'; # do not fill up syslog
370 PVE
::Firewall
::set_verbose
($param->{verbose
});
372 my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = PVE
::Firewall
::compile
();
374 PVE
::FirewallSimulator
::debug
();
376 my $host_ip = PVE
::Cluster
::remote_node_ip
($nodename);
378 PVE
::FirewallSimulator
::reset_trace
();
379 print Dumper
($ruleset->{filter
}) if $param->{verbose
};
380 print Dumper
($ruleset->{raw
}) if $param->{verbose
};
383 from
=> $param->{from
},
385 proto
=> $param->{protocol
} || 'tcp',
386 source
=> $param->{source
},
387 dest
=> $param->{dest
},
388 dport
=> $param->{dport
},
389 sport
=> $param->{sport
},
392 if (!defined($test->{to
})) {
393 $test->{to
} = 'host';
394 PVE
::FirewallSimulator
::add_trace
("Set Zone: to => '$test->{to}'\n");
396 if (!defined($test->{from
})) {
397 $test->{from
} = 'outside',
398 PVE
::FirewallSimulator
::add_trace
("Set Zone: from => '$test->{from}'\n");
401 my $vmdata = PVE
::Firewall
::read_local_vm_config
();
403 print "Test packet:\n";
405 foreach my $k (qw(from to proto source dest dport sport)) {
406 printf(" %-8s: %s\n", $k, $test->{$k}) if defined($test->{$k});
409 $test->{action
} = 'QUERY';
411 my $res = PVE
::FirewallSimulator
::simulate_firewall
(
412 $ruleset->{filter
}, $ipset_ruleset, $host_ip, $vmdata, $test);
414 print "ACTION: $res\n";
420 start
=> [ __PACKAGE__
, 'start', []],
421 restart
=> [ __PACKAGE__
, 'restart', []],
422 stop
=> [ __PACKAGE__
, 'stop', []],
423 compile
=> [ __PACKAGE__
, 'compile', []],
424 simulate
=> [ __PACKAGE__
, 'simulate', []],
425 localnet
=> [ __PACKAGE__
, 'localnet', []],
426 status
=> [ __PACKAGE__
, 'status', [], undef, sub {
428 my $status = ($res->{enable
} ?
"enabled" : "disabled") . '/' . $res->{status
};
430 if ($res->{changes
}) {
431 print "Status: $status (pending changes)\n";
433 print "Status: $status\n";