]>
git.proxmox.com Git - pve-firewall.git/blob - src/PVE/Service/pve_firewall.pm
9f712e0d16a856b4c4b3c1ff2e0e801b6db7a8ad
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) ;
13 use PVE
:: RPCEnvironment
;
16 use PVE
:: FirewallSimulator
;
19 use base
qw(PVE::Daemon) ;
21 my $cmdline = [ $0, @ARGV ];
23 my %daemon_options = ( restart_on_error
=> 5 , stop_wait_time
=> 5 );
25 my $daemon = __PACKAGE__-
> new ( 'pve-firewall' , $cmdline, %daemon_options );
27 my $nodename = PVE
:: INotify
:: nodename
();
31 PVE
:: Cluster
:: cfs_update
();
33 PVE
:: Firewall
:: init
();
36 my $restart_request = 0 ;
42 my $initial_memory_usage ;
47 syslog
( 'info' , "server closing" );
50 1 while ( waitpid (- 1 , POSIX
:: WNOHANG
()) > 0 );
52 syslog
( 'info' , "clear firewall rules" );
54 eval { PVE
:: Firewall
:: remove_pvefw_chains
(); };
57 $self -> exit_daemon ( 0 );
69 local $SIG { '__WARN__' } = 'IGNORE' ; # do not fill up logs
73 $next_update = time () + $updatetime ;
75 my ( $ccsec, $cusec ) = gettimeofday
();
77 PVE
:: Cluster
:: cfs_update
();
78 PVE
:: Firewall
:: update
();
83 syslog
( 'err' , "status update error: $err " );
86 my ( $ccsec_end, $cusec_end ) = gettimeofday
();
87 my $cptime = ( $ccsec_end - $ccsec ) + ( $cusec_end - $cusec )/ 1000000 ;
89 syslog
( 'info' , sprintf ( "firewall update time (%.3f seconds)" , $cptime ))
94 my $mem = PVE
:: ProcFSTools
:: read_memory_usage
();
96 if (! defined ( $initial_memory_usage ) || ( $cycle < 10 )) {
97 $initial_memory_usage = $mem ->{ resident
};
99 my $diff = $mem ->{ resident
} - $initial_memory_usage ;
100 if ( $diff > 5 * 1024 * 1024 ) {
101 syslog
( 'info' , "restarting server after $cycle cycles to " .
102 "reduce memory usage (free $mem ->{resident} ( $diff ) bytes)" );
103 $self -> restart_daemon ();
108 while (( time () < $next_update ) &&
109 ( $wcount < $updatetime ) && # protect against time wrap
110 ! $restart_request ) { $wcount++ ; sleep ( 1 ); };
112 $self -> restart_daemon () if $restart_request ;
116 $daemon -> register_start_command ( "Start the Proxmox VE firewall service." );
117 $daemon -> register_restart_command ( 1 , "Restart the Proxmox VE firewall service." );
118 $daemon -> register_stop_command ( "Stop firewall. This removes all Proxmox VE " .
119 "related iptable rules. " .
120 "The host is unprotected afterwards." );
122 __PACKAGE__-
> register_method ({
126 description
=> "Get firewall status." ,
128 additionalProperties
=> 0 ,
133 additionalProperties
=> 0 ,
137 enum
=> [ 'unknown' , 'stopped' , 'running' ],
140 description
=> "Firewall is enabled (in 'cluster.fw')" ,
144 description
=> "Set when there are pending changes." ,
153 local $SIG { '__WARN__' } = 'DEFAULT' ; # do not fill up syslog
157 my $status = $daemon -> running () ?
'running' : 'stopped' ;
159 my $res = { status
=> $status };
161 my $verbose = 1 ; # show syntax errors
162 my $cluster_conf = PVE
:: Firewall
:: load_clusterfw_conf
( undef , $verbose );
163 $res ->{ enable
} = $cluster_conf ->{ options
}->{ enable
} ?
1 : 0 ;
165 if ( $status eq 'running' ) {
167 my ( $ruleset, $ipset_ruleset, $rulesetv6 ) = PVE
:: Firewall
:: compile
( $cluster_conf, undef , undef , $verbose );
169 $verbose = 0 ; # do not show iptables details
170 my ( undef , undef , $ipset_changes ) = PVE
:: Firewall
:: get_ipset_cmdlist
( $ipset_ruleset, $verbose );
171 my ( $test, $ruleset_changes ) = PVE
:: Firewall
:: get_ruleset_cmdlist
( $ruleset, $verbose );
172 my ( undef , $ruleset_changesv6 ) = PVE
:: Firewall
:: get_ruleset_cmdlist
( $rulesetv6, $verbose, "ip6tables" );
174 $res ->{ changes
} = ( $ipset_changes || $ruleset_changes || $ruleset_changesv6 ) ?
1 : 0 ;
180 return PVE
:: Firewall
:: run_locked
( $code );
183 __PACKAGE__-
> register_method ({
187 description
=> "Compile and print firewall rules. This is useful for testing." ,
189 additionalProperties
=> 0 ,
192 returns
=> { type
=> 'null' },
197 local $SIG { '__WARN__' } = 'DEFAULT' ; # do not fill up syslog
203 my $cluster_conf = PVE
:: Firewall
:: load_clusterfw_conf
( undef , $verbose );
204 my ( $ruleset, $ipset_ruleset, $rulesetv6 ) = PVE
:: Firewall
:: compile
( $cluster_conf, undef , undef , $verbose );
206 print "ipset cmdlist: \n " ;
207 my ( undef , undef , $ipset_changes ) = PVE
:: Firewall
:: get_ipset_cmdlist
( $ipset_ruleset, $verbose );
209 print " \n iptables cmdlist: \n " ;
210 my ( undef , $ruleset_changes ) = PVE
:: Firewall
:: get_ruleset_cmdlist
( $ruleset, $verbose );
212 print " \n ip6tables cmdlist: \n " ;
213 my ( undef , $ruleset_changesv6 ) = PVE
:: Firewall
:: get_ruleset_cmdlist
( $rulesetv6, $verbose, "ip6tables" );
215 if ( $ipset_changes || $ruleset_changes || $ruleset_changesv6 ) {
216 print "detected changes \n " ;
218 print "no changes \n " ;
220 if (! $cluster_conf ->{ options
}->{ enable
}) {
221 print "firewall disabled \n " ;
226 PVE
:: Firewall
:: run_locked
( $code );
231 __PACKAGE__-
> register_method ({
235 description
=> "Print information about local network." ,
237 additionalProperties
=> 0 ,
240 returns
=> { type
=> 'null' },
244 local $SIG { '__WARN__' } = 'DEFAULT' ; # do not fill up syslog
246 my $nodename = PVE
:: INotify
:: nodename
();
247 print "local hostname: $nodename\n " ;
249 my $ip = PVE
:: Cluster
:: remote_node_ip
( $nodename );
250 print "local IP address: $ip\n " ;
252 my $cluster_conf = PVE
:: Firewall
:: load_clusterfw_conf
();
254 my $localnet = PVE
:: Firewall
:: local_network
() || '127.0.0.0/8' ;
255 print "network auto detect: $localnet\n " ;
256 if ( $cluster_conf ->{ aliases
}->{ local_network
}) {
257 print "using user defined local_network: $cluster_conf ->{aliases}->{local_network}->{cidr} \n " ;
259 print "using detected local_network: $localnet\n " ;
265 __PACKAGE__-
> register_method ({
269 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." ,
271 additionalProperties
=> 0 ,
274 description
=> "Verbose output." ,
280 description
=> "Source zone." ,
282 pattern
=> '(host|outside|vm\d+|ct\d+|vmbr\d+/\S+)' ,
284 default => 'outside' ,
287 description
=> "Destination zone." ,
289 pattern
=> '(host|outside|vm\d+|ct\d+|vmbr\d+/\S+)' ,
294 description
=> "Protocol." ,
296 pattern
=> '(tcp|udp)' ,
301 description
=> "Destination port." ,
308 description
=> "Source port." ,
315 description
=> "Source IP address." ,
316 type
=> 'string' , format
=> 'ipv4' ,
320 description
=> "Destination IP address." ,
321 type
=> 'string' , format
=> 'ipv4' ,
326 returns
=> { type
=> 'null' },
330 local $SIG { '__WARN__' } = 'DEFAULT' ; # do not fill up syslog
332 my ( $ruleset, $ipset_ruleset, $rulesetv6 ) = PVE
:: Firewall
:: compile
( undef , undef , undef , $param ->{ verbose
});
334 PVE
:: FirewallSimulator
:: debug
( $param ->{ verbose
} || 0 );
336 my $host_ip = PVE
:: Cluster
:: remote_node_ip
( $nodename );
338 PVE
:: FirewallSimulator
:: reset_trace
();
339 print Dumper
( $ruleset ) if $param ->{ verbose
};
342 from
=> $param ->{ from
},
344 proto
=> $param ->{ protocol
} || 'tcp' ,
345 source
=> $param ->{ source
},
346 dest
=> $param ->{ dest
},
347 dport
=> $param ->{ dport
},
348 sport
=> $param ->{ sport
},
351 if (! defined ( $test ->{ to
})) {
352 $test ->{ to
} = 'host' ;
353 PVE
:: FirewallSimulator
:: add_trace
( "Set Zone: to => ' $test ->{to}' \n " );
355 if (! defined ( $test ->{ from
})) {
356 $test ->{ from
} = 'outside' ,
357 PVE
:: FirewallSimulator
:: add_trace
( "Set Zone: from => ' $test ->{from}' \n " );
360 my $vmdata = PVE
:: Firewall
:: read_local_vm_config
();
362 print "Test packet: \n " ;
364 foreach my $k ( qw(from to proto source dest dport sport) ) {
365 printf ( " %-8s: %s\n " , $k, $test ->{ $k }) if defined ( $test ->{ $k });
368 $test ->{ action
} = 'QUERY' ;
370 my $res = PVE
:: FirewallSimulator
:: simulate_firewall
( $ruleset, $ipset_ruleset,
371 $host_ip, $vmdata, $test );
373 print "ACTION: $res\n " ;
379 start
=> [ __PACKAGE__
, 'start' , []],
380 restart
=> [ __PACKAGE__
, 'restart' , []],
381 stop
=> [ __PACKAGE__
, 'stop' , []],
382 compile
=> [ __PACKAGE__
, 'compile' , []],
383 simulate
=> [ __PACKAGE__
, 'simulate' , []],
384 localnet
=> [ __PACKAGE__
, 'localnet' , []],
385 status
=> [ __PACKAGE__
, 'status' , [], undef , sub {
387 my $status = ( $res ->{ enable
} ?
"enabled" : "disabled" ) . '/' . $res ->{ status
};
389 if ( $res ->{ changes
}) {
390 print "Status: $status (pending changes) \n " ;
392 print "Status: $status\n " ;
403 pve-firewall - PVE Firewall Daemon
411 This service updates iptables rules periodically.
413 =include pve_copyright