]>
git.proxmox.com Git - pve-firewall.git/blob - src/pve-firewall
6 use POSIX
":sys_wait_h" ;
9 use Time
:: HiRes qw
( gettimeofday
);
10 use PVE
:: Tools
qw(dir_glob_foreach file_read_firstline) ;
12 use PVE
:: Cluster
qw(cfs_read_file) ;
13 use PVE
:: RPCEnvironment
;
16 use PVE
:: FirewallSimulator
;
19 use base
qw(PVE::CLIHandler) ;
21 my $pve_firewall_pidfile = "/var/run/pve-firewall.pid" ;
23 $SIG { '__WARN__' } = sub {
28 syslog
( 'warning' , "WARNING: %s " , $t );
32 initlog
( 'pve-firewall' );
34 $ENV { 'PATH' } = '/sbin:/bin:/usr/sbin:/usr/bin' ;
36 die "please run as root \n " if $> != 0 ;
38 PVE
:: INotify
:: inotify_init
();
40 my $rpcenv = PVE
:: RPCEnvironment-
> init ( 'cli' );
42 $rpcenv -> init_request ();
43 $rpcenv -> set_language ( $ENV { LANG
});
44 $rpcenv -> set_user ( 'root @pam ' );
46 my $nodename = PVE
:: INotify
:: nodename
();
48 my $commandline = [ $0, @ARGV ];
55 syslog
( 'info' , "server shutdown (restart)" );
57 $ENV { RESTART_PVE_FIREWALL
} = 1 ;
59 sleep ( $waittime ) if $waittime ; # avoid high server load due to restarts
61 PVE
:: INotify
:: inotify_close
();
64 exit (- 1 ); # never reached?
68 unlink " $pve_firewall_pidfile .lock" ;
69 unlink $pve_firewall_pidfile ;
74 my $lkfn = " $pidfile .lock" ;
76 if (! open ( FLCK
, ">> $lkfn " )) {
77 my $msg = "can't aquire lock on file ' $lkfn ' - $! " ;
82 if (! flock ( FLCK
, LOCK_EX
| LOCK_NB
)) {
84 my $msg = "can't aquire lock ' $lkfn ' - $! " ;
93 if (! open ( PIDFH
, "> $pidfile " )) {
94 my $msg = "can't open pid file ' $pidfile ' - $! " ;
102 my $restart_request = 0 ;
108 my $initial_memory_usage ;
113 # try to get the lock
114 lockpidfile
( $pve_firewall_pidfile );
119 my $restart = $ENV { RESTART_PVE_FIREWALL
};
121 delete $ENV { RESTART_PVE_FIREWALL
};
123 PVE
:: Cluster
:: cfs_update
();
125 PVE
:: Firewall
:: init
();
127 if (! $param ->{ debug
}) {
128 open STDIN
, '</dev/null' || die "can't read /dev/null" ;
129 open STDOUT
, '>/dev/null' || die "can't write /dev/null" ;
132 if (! $restart && ! $param ->{ debug
}) {
134 if (! defined ( $spid )) {
135 my $msg = "can't put server into background - fork failed" ;
138 } elsif ( $spid ) { # parent
143 writepidfile
( $pve_firewall_pidfile );
145 open STDERR
, '>&STDOUT' || die "can't close STDERR \n " ;
147 $SIG { INT
} = $SIG { TERM
} = $SIG { QUIT
} = sub {
148 syslog
( 'info' , "server closing" );
150 $SIG { INT
} = 'DEFAULT' ;
153 1 while ( waitpid (- 1 , POSIX
:: WNOHANG
()) > 0 );
155 syslog
( 'info' , "clear firewall rules" );
156 eval { PVE
:: Firewall
:: remove_pvefw_chains
(); die "STOP" ;};
165 # wake up process, so this forces an immediate firewall rules update
166 syslog
( 'info' , "received signal HUP (restart)" );
167 $restart_request = 1 ;
171 syslog
( 'info' , "restarting server" );
173 syslog
( 'info' , "starting server" );
180 local $SIG { '__WARN__' } = 'IGNORE' ; # do not fill up logs
182 $next_update = time () + $updatetime ;
184 my ( $ccsec, $cusec ) = gettimeofday
();
186 PVE
:: Cluster
:: cfs_update
();
187 PVE
:: Firewall
:: update
();
192 syslog
( 'err' , "status update error: $err " );
195 my ( $ccsec_end, $cusec_end ) = gettimeofday
();
196 my $cptime = ( $ccsec_end - $ccsec ) + ( $cusec_end - $cusec )/ 1000000 ;
198 syslog
( 'info' , sprintf ( "firewall update time (%.3f seconds)" , $cptime ))
203 my $mem = PVE
:: ProcFSTools
:: read_memory_usage
();
205 if (! defined ( $initial_memory_usage ) || ( $cycle < 10 )) {
206 $initial_memory_usage = $mem ->{ resident
};
208 my $diff = $mem ->{ resident
} - $initial_memory_usage ;
209 if ( $diff > 5 * 1024 * 1024 ) {
210 syslog
( 'info' , "restarting server after $cycle cycles to " .
211 "reduce memory usage (free $mem ->{resident} ( $diff ) bytes)" );
217 while (( time () < $next_update ) &&
218 ( $wcount < $updatetime ) && # protect against time wrap
219 ! $restart_request ) { $wcount++ ; sleep ( 1 ); };
221 restart_server
() if $restart_request ;
227 syslog
( 'err' , "ERROR: $err " );
234 __PACKAGE__-
> register_method ({
238 description
=> "Start the Proxmox VE firewall service." ,
240 additionalProperties
=> 0 ,
243 description
=> "Debug mode - stay in foreground" ,
250 returns
=> { type
=> 'null' },
260 __PACKAGE__-
> register_method ({
264 description
=> "Stop firewall. This removes all Proxmox VE related iptable rules. The host is unprotected afterwards." ,
266 additionalProperties
=> 0 ,
269 returns
=> { type
=> 'null' },
274 my $pid = int ( PVE
:: Tools
:: file_read_firstline
( $pve_firewall_pidfile ) || 0 );
277 if ( PVE
:: ProcFSTools
:: check_process_running
( $pid )) {
278 kill ( 15 , $pid ); # send TERM signal
279 # give max 5 seconds to shut down
280 for ( my $i = 0 ; $i < 5 ; $i++ ) {
281 last if ! PVE
:: ProcFSTools
:: check_process_running
( $pid );
289 if (- f
$pve_firewall_pidfile ) {
290 # try to get the lock
291 lockpidfile
( $pve_firewall_pidfile );
299 __PACKAGE__-
> register_method ({
303 description
=> "Get firewall status." ,
305 additionalProperties
=> 0 ,
310 additionalProperties
=> 0 ,
314 enum
=> [ 'unknown' , 'stopped' , 'active' ],
317 description
=> "Set when there are pending changes." ,
326 local $SIG { '__WARN__' } = 'DEFAULT' ; # do not fill up syslog
330 my $pid = int ( PVE
:: Tools
:: file_read_firstline
( $pve_firewall_pidfile ) || 0 );
331 my $running = PVE
:: ProcFSTools
:: check_process_running
( $pid );
333 my $status = $running ?
'active' : 'stopped' ;
335 my $res = { status
=> $status };
336 if ( $status eq 'active' ) {
337 my ( $ruleset, $ipset_ruleset ) = PVE
:: Firewall
:: compile
();
339 my ( undef , undef , $ipset_changes ) = PVE
:: Firewall
:: get_ipset_cmdlist
( $ipset_ruleset );
340 my ( undef , $ruleset_changes ) = PVE
:: Firewall
:: get_ruleset_cmdlist
( $ruleset );
342 $res ->{ changes
} = ( $ipset_changes || $ruleset_changes ) ?
1 : 0 ;
348 return PVE
:: Firewall
:: run_locked
( $code );
351 __PACKAGE__-
> register_method ({
355 description
=> "Compile and print firewall rules. This is useful for testing." ,
357 additionalProperties
=> 0 ,
360 returns
=> { type
=> 'null' },
365 local $SIG { '__WARN__' } = 'DEFAULT' ; # do not fill up syslog
368 my ( $ruleset, $ipset_ruleset ) = PVE
:: Firewall
:: compile
();
370 my ( undef , undef , $ipset_changes ) = PVE
:: Firewall
:: get_ipset_cmdlist
( $ipset_ruleset, 1 );
371 my ( undef , $ruleset_changes ) = PVE
:: Firewall
:: get_ruleset_cmdlist
( $ruleset, 1 );
372 if ( $ipset_changes || $ruleset_changes ) {
373 print "detected changes \n " ;
375 print "no changes \n " ;
379 PVE
:: Firewall
:: run_locked
( $code );
384 __PACKAGE__-
> register_method ({
388 description
=> "Print information about local network." ,
390 additionalProperties
=> 0 ,
393 returns
=> { type
=> 'null' },
397 local $SIG { '__WARN__' } = 'DEFAULT' ; # do not fill up syslog
399 my $nodename = PVE
:: INotify
:: nodename
();
400 print "local hostname: $nodename\n " ;
402 my $ip = PVE
:: Cluster
:: remote_node_ip
( $nodename );
403 print "local IP address: $ip\n " ;
405 my $cluster_conf = PVE
:: Firewall
:: load_clusterfw_conf
();
407 my $localnet = PVE
:: Firewall
:: local_network
() || '127.0.0.0/8' ;
408 print "network auto detect: $localnet\n " ;
409 if ( $cluster_conf ->{ aliases
}->{ local_network
}) {
410 print "using user defined local_network: $cluster_conf ->{aliases}->{local_network}->{cidr} \n " ;
412 print "using detected local_network: $localnet\n " ;
418 __PACKAGE__-
> register_method ({
422 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." ,
424 additionalProperties
=> 0 ,
427 description
=> "Verbose output." ,
433 description
=> "Source zone." ,
435 pattern
=> '(host|outside|vm\d+|ct\d+|vmbr\d+/\S+)' ,
437 default => 'outside' ,
440 description
=> "Destination zone." ,
442 pattern
=> '(host|outside|vm\d+|ct\d+|vmbr\d+/\S+)' ,
447 description
=> "Protocol." ,
449 pattern
=> '(tcp|udp)' ,
454 description
=> "Destination port." ,
461 description
=> "Source port." ,
468 description
=> "Source IP address." ,
469 type
=> 'string' , format
=> 'ipv4' ,
473 description
=> "Destination IP address." ,
474 type
=> 'string' , format
=> 'ipv4' ,
479 returns
=> { type
=> 'null' },
483 local $SIG { '__WARN__' } = 'DEFAULT' ; # do not fill up syslog
485 my ( $ruleset, $ipset_ruleset ) = PVE
:: Firewall
:: compile
();
487 PVE
:: FirewallSimulator
:: debug
( $param ->{ verbose
} || 0 );
489 my $host_ip = PVE
:: Cluster
:: remote_node_ip
( $nodename );
491 PVE
:: FirewallSimulator
:: reset_trace
();
492 print Dumper
( $ruleset ) if $param ->{ verbose
};
495 from
=> $param ->{ from
},
497 proto
=> $param ->{ protocol
} || 'tcp' ,
498 source
=> $param ->{ source
},
499 dest
=> $param ->{ dest
},
500 dport
=> $param ->{ dport
},
501 sport
=> $param ->{ sport
},
504 if (! defined ( $test ->{ to
})) {
505 $test ->{ to
} = 'host' ;
506 PVE
:: FirewallSimulator
:: add_trace
( "Set Zone: to => ' $test ->{to}' \n " );
508 if (! defined ( $test ->{ from
})) {
509 $test ->{ from
} = 'outside' ,
510 PVE
:: FirewallSimulator
:: add_trace
( "Set Zone: from => ' $test ->{from}' \n " );
513 my $vmdata = PVE
:: Firewall
:: read_local_vm_config
();
515 print "Test packet: \n " ;
517 foreach my $k ( qw(from to proto source dest dport sport) ) {
518 printf ( " %-8s: %s\n " , $k, $test ->{ $k }) if defined ( $test ->{ $k });
521 $test ->{ action
} = 'QUERY' ;
523 my $res = PVE
:: FirewallSimulator
:: simulate_firewall
( $ruleset, $ipset_ruleset,
524 $host_ip, $vmdata, $test );
526 print "ACTION: $res\n " ;
532 start
=> [ __PACKAGE__
, 'start' , []],
533 stop
=> [ __PACKAGE__
, 'stop' , []],
534 compile
=> [ __PACKAGE__
, 'compile' , []],
535 simulate
=> [ __PACKAGE__
, 'simulate' , []],
536 localnet
=> [ __PACKAGE__
, 'localnet' , []],
537 status
=> [ __PACKAGE__
, 'status' , [], undef , sub {
539 if ( $res ->{ changes
}) {
540 print "Status: $res ->{status} (pending changes) \n " ;
542 print "Status: $res ->{status} \n " ;
549 PVE
:: CLIHandler
:: handle_cmd
( $cmddef, $0, $cmd, \
@ARGV, undef , $0 );
557 pve-firewall - PVE Firewall Daemon
565 This service updates iptables rules periodically.
567 =include pve_copyright