1 package PVE
::Service
::pve_firewall
;
8 use Time
::HiRes qw
(gettimeofday
);
9 use PVE
::Tools
qw(dir_glob_foreach file_read_firstline);
12 use PVE
::Cluster
qw(cfs_read_file);
14 use PVE
::RPCEnvironment
;
17 use PVE
::FirewallSimulator
;
20 use base
qw(PVE::Daemon);
22 my $cmdline = [$0, @ARGV];
24 my %daemon_options = (restart_on_error
=> 5, stop_wait_time
=> 5);
26 my $daemon = __PACKAGE__-
>new('pve-firewall', $cmdline, %daemon_options);
28 my $nodename = PVE
::INotify
::nodename
();
32 PVE
::Cluster
::cfs_update
();
34 PVE
::Firewall
::init
();
37 my $restart_request = 0;
43 my $initial_memory_usage;
48 syslog
('info' , "server closing");
51 1 while (waitpid(-1, POSIX
::WNOHANG
()) > 0);
53 syslog
('info' , "clear firewall rules");
55 eval { PVE
::Firewall
::remove_pvefw_chains
(); };
58 $self->exit_daemon(0);
70 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("Stop firewall. This removes all Proxmox VE " .
120 "related iptable rules. " .
121 "The host is unprotected afterwards.");
123 __PACKAGE__-
>register_method ({
127 description
=> "Get firewall status.",
129 additionalProperties
=> 0,
134 additionalProperties
=> 0,
138 enum
=> ['unknown', 'stopped', 'running'],
141 description
=> "Firewall is enabled (in 'cluster.fw')",
145 description
=> "Set when there are pending changes.",
154 local $SIG{'__WARN__'} = 'DEFAULT'; # do not fill up syslog
158 my $status = $daemon->running() ?
'running' : 'stopped';
160 my $res = { status
=> $status };
162 PVE
::Firewall
::set_verbose
(1); # show syntax errors
164 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
(undef);
165 $res->{enable
} = $cluster_conf->{options
}->{enable
} ?
1 : 0;
167 if ($status eq 'running') {
169 my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = PVE
::Firewall
::compile
($cluster_conf, undef, undef);
171 PVE
::Firewall
::set_verbose
(0); # do not show iptables details
172 my (undef, undef, $ipset_changes) = PVE
::Firewall
::get_ipset_cmdlist
($ipset_ruleset);
173 my ($test, $ruleset_changes) = PVE
::Firewall
::get_ruleset_cmdlist
($ruleset);
174 my (undef, $ruleset_changesv6) = PVE
::Firewall
::get_ruleset_cmdlist
($rulesetv6, "ip6tables");
175 my (undef, $ebtables_changes) = PVE
::Firewall
::get_ebtables_cmdlist
($ebtables_ruleset);
177 $res->{changes
} = ($ipset_changes || $ruleset_changes || $ruleset_changesv6 || $ebtables_changes) ?
1 : 0;
183 return PVE
::Firewall
::run_locked
($code);
186 __PACKAGE__-
>register_method ({
190 description
=> "Compile and print firewall rules. This is useful for testing.",
192 additionalProperties
=> 0,
195 returns
=> { type
=> 'null' },
200 local $SIG{'__WARN__'} = 'DEFAULT'; # do not fill up syslog
204 PVE
::Firewall
::set_verbose
(1);
206 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
(undef);
207 my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = PVE
::Firewall
::compile
($cluster_conf, undef, undef);
209 print "ipset cmdlist:\n";
210 my (undef, undef, $ipset_changes) = PVE
::Firewall
::get_ipset_cmdlist
($ipset_ruleset);
212 print "\niptables cmdlist:\n";
213 my (undef, $ruleset_changes) = PVE
::Firewall
::get_ruleset_cmdlist
($ruleset);
215 print "\nip6tables cmdlist:\n";
216 my (undef, $ruleset_changesv6) = PVE
::Firewall
::get_ruleset_cmdlist
($rulesetv6, "ip6tables");
218 print "\nebtables cmdlist:\n";
219 my (undef, $ebtables_changes) = PVE
::Firewall
::get_ebtables_cmdlist
($ebtables_ruleset);
221 if ($ipset_changes || $ruleset_changes || $ruleset_changesv6 || $ebtables_changes) {
222 print "detected changes\n";
224 print "no changes\n";
226 if (!$cluster_conf->{options
}->{enable
}) {
227 print "firewall disabled\n";
232 PVE
::Firewall
::run_locked
($code);
237 __PACKAGE__-
>register_method ({
241 description
=> "Print information about local network.",
243 additionalProperties
=> 0,
246 returns
=> { type
=> 'null' },
250 local $SIG{'__WARN__'} = 'DEFAULT'; # do not fill up syslog
252 my $nodename = PVE
::INotify
::nodename
();
253 print "local hostname: $nodename\n";
255 my $ip = PVE
::Cluster
::remote_node_ip
($nodename);
256 print "local IP address: $ip\n";
258 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
260 my $localnet = PVE
::Firewall
::local_network
() || '127.0.0.0/8';
261 print "network auto detect: $localnet\n";
262 if ($cluster_conf->{aliases
}->{local_network
}) {
263 print "using user defined local_network: $cluster_conf->{aliases}->{local_network}->{cidr}\n";
265 print "using detected local_network: $localnet\n";
268 if (PVE
::Corosync
::check_conf_exists
(1)) {
269 my $corosync_conf = PVE
::Cluster
::cfs_read_file
("corosync.conf");
270 my $corosync_node_found = 0;
272 print "\naccepting corosync traffic from/to:\n";
274 PVE
::Corosync
::for_all_corosync_addresses
($corosync_conf, undef, sub {
275 my ($curr_node_name, $curr_node_ip, undef, $key) = @_;
277 return if $curr_node_name eq $nodename;
279 $corosync_node_found = 1;
281 $key =~ m/(?:ring|link)(\d+)_addr/;
282 print " - $curr_node_name: $curr_node_ip (link: $1)\n";
285 if (!$corosync_node_found) {
286 print " - no nodes found\n";
293 __PACKAGE__-
>register_method ({
297 description
=> "Simulate firewall rules. This does not simulate kernel 'routing' table. Instead, this simply assumes that routing from source zone to destination zone is possible.",
299 additionalProperties
=> 0,
302 description
=> "Verbose output.",
308 description
=> "Source zone.",
310 pattern
=> '(host|outside|vm\d+|ct\d+|vmbr\d+/\S+)',
312 default => 'outside',
315 description
=> "Destination zone.",
317 pattern
=> '(host|outside|vm\d+|ct\d+|vmbr\d+/\S+)',
322 description
=> "Protocol.",
324 pattern
=> '(tcp|udp)',
329 description
=> "Destination port.",
336 description
=> "Source port.",
343 description
=> "Source IP address.",
344 type
=> 'string', format
=> 'ipv4',
348 description
=> "Destination IP address.",
349 type
=> 'string', format
=> 'ipv4',
354 returns
=> { type
=> 'null' },
358 local $SIG{'__WARN__'} = 'DEFAULT'; # do not fill up syslog
360 PVE
::Firewall
::set_verbose
($param->{verbose
});
362 my ($ruleset, $ipset_ruleset, $rulesetv6, $ebtables_ruleset) = PVE
::Firewall
::compile
();
364 PVE
::FirewallSimulator
::debug
();
366 my $host_ip = PVE
::Cluster
::remote_node_ip
($nodename);
368 PVE
::FirewallSimulator
::reset_trace
();
369 print Dumper
($ruleset) if $param->{verbose
};
372 from
=> $param->{from
},
374 proto
=> $param->{protocol
} || 'tcp',
375 source
=> $param->{source
},
376 dest
=> $param->{dest
},
377 dport
=> $param->{dport
},
378 sport
=> $param->{sport
},
381 if (!defined($test->{to
})) {
382 $test->{to
} = 'host';
383 PVE
::FirewallSimulator
::add_trace
("Set Zone: to => '$test->{to}'\n");
385 if (!defined($test->{from
})) {
386 $test->{from
} = 'outside',
387 PVE
::FirewallSimulator
::add_trace
("Set Zone: from => '$test->{from}'\n");
390 my $vmdata = PVE
::Firewall
::read_local_vm_config
();
392 print "Test packet:\n";
394 foreach my $k (qw(from to proto source dest dport sport)) {
395 printf(" %-8s: %s\n", $k, $test->{$k}) if defined($test->{$k});
398 $test->{action
} = 'QUERY';
400 my $res = PVE
::FirewallSimulator
::simulate_firewall
($ruleset, $ipset_ruleset,
401 $host_ip, $vmdata, $test);
403 print "ACTION: $res\n";
409 start
=> [ __PACKAGE__
, 'start', []],
410 restart
=> [ __PACKAGE__
, 'restart', []],
411 stop
=> [ __PACKAGE__
, 'stop', []],
412 compile
=> [ __PACKAGE__
, 'compile', []],
413 simulate
=> [ __PACKAGE__
, 'simulate', []],
414 localnet
=> [ __PACKAGE__
, 'localnet', []],
415 status
=> [ __PACKAGE__
, 'status', [], undef, sub {
417 my $status = ($res->{enable
} ?
"enabled" : "disabled") . '/' . $res->{status
};
419 if ($res->{changes
}) {
420 print "Status: $status (pending changes)\n";
422 print "Status: $status\n";