Home | History | Annotate | Line # | Download | only in rc.d
ipsec revision 1.11.14.2
      1        1.1   itojun #!/bin/sh
      2        1.1   itojun #
      3  1.11.14.1      tls # $NetBSD: ipsec,v 1.11.14.2 2014/08/19 23:45:51 tls Exp $
      4        1.1   itojun #
      5        1.1   itojun 
      6        1.1   itojun # PROVIDE: ipsec
      7       1.11  tsutsui # REQUIRE: root bootconf mountcritlocal tty
      8        1.7  thorpej # BEFORE:  DAEMON
      9        1.1   itojun 
     10        1.8  mycroft $_rc_subr_loaded . /etc/rc.subr
     11        1.1   itojun 
     12        1.1   itojun name="ipsec"
     13        1.6    lukem rcvar=$name
     14        1.4    lukem start_precmd="ipsec_prestart"
     15        1.1   itojun start_cmd="ipsec_start"
     16        1.6    lukem stop_precmd="test -f /etc/ipsec.conf"
     17        1.1   itojun stop_cmd="ipsec_stop"
     18        1.1   itojun reload_cmd="ipsec_reload"
     19        1.1   itojun extra_commands="reload"
     20        1.1   itojun 
     21        1.5    lukem ipsec_prestart()
     22        1.1   itojun {
     23        1.1   itojun 	if [ ! -f /etc/ipsec.conf ]; then
     24        1.4    lukem 		warn "/etc/ipsec.conf not readable; ipsec start aborted."
     25        1.9      apb 
     26        1.9      apb 		stop_boot
     27        1.4    lukem 		return 1
     28        1.1   itojun 	fi
     29        1.4    lukem 	return 0
     30        1.4    lukem }
     31        1.4    lukem 
     32  1.11.14.1      tls ipsec_getip() {
     33  1.11.14.1      tls 	ifconfig $1 | while read what address rest; do
     34  1.11.14.1      tls 		case "$what" in
     35  1.11.14.1      tls 		inet)	echo "$address";;
     36  1.11.14.1      tls 		esac
     37  1.11.14.1      tls 	done
     38  1.11.14.1      tls }
     39  1.11.14.1      tls 
     40  1.11.14.2      tls ipsec_load() {
     41  1.11.14.2      tls 	if [ -z "$1" ]; then
     42  1.11.14.2      tls 		/sbin/setkey -f /etc/ipsec.conf
     43  1.11.14.2      tls 	else
     44  1.11.14.2      tls 		sed -e "s/@LOCAL_ADDR@/$1/" < /etc/ipsec.conf | \
     45  1.11.14.2      tls 		    /sbin/setkey -f -
     46  1.11.14.2      tls 	fi
     47  1.11.14.2      tls }
     48  1.11.14.2      tls 
     49  1.11.14.2      tls ipsec_configure() {
     50  1.11.14.2      tls 	while true; do
     51  1.11.14.2      tls 		local addr="$(ipsec_getip "$ipsec_flags")"
     52  1.11.14.2      tls 		case "$addr" in
     53  1.11.14.2      tls 		'')		sleep 1;;
     54  1.11.14.2      tls 		"0.0.0.0")	sleep 1;;
     55  1.11.14.2      tls 		*)		ipsec_load "$addr"; return;;
     56  1.11.14.2      tls 		esac
     57  1.11.14.2      tls 	done &
     58  1.11.14.2      tls }
     59  1.11.14.2      tls 
     60        1.4    lukem ipsec_start()
     61        1.4    lukem {
     62        1.1   itojun 	echo "Installing ipsec manual keys/policies."
     63  1.11.14.1      tls 	if [ -n "$ipsec_flags" ]; then
     64  1.11.14.2      tls 		ipsec_configure
     65  1.11.14.1      tls 	else
     66  1.11.14.2      tls 		ipsec_load
     67  1.11.14.1      tls 	fi
     68        1.1   itojun }
     69        1.1   itojun 
     70        1.1   itojun ipsec_stop()
     71        1.1   itojun {
     72        1.3   itojun 	echo "Clearing ipsec manual keys/policies."
     73        1.1   itojun 
     74        1.1   itojun 	# still not 100% sure if we would like to do this.
     75        1.1   itojun 	# it is very questionable to do this during shutdown session, since
     76        1.1   itojun 	# it can hang any of remaining IPv4/v6 session.
     77        1.1   itojun 	#
     78        1.1   itojun 	/sbin/setkey -F
     79        1.1   itojun 	/sbin/setkey -FP
     80        1.1   itojun }
     81        1.1   itojun 
     82        1.1   itojun ipsec_reload()
     83        1.1   itojun {
     84        1.1   itojun 	echo "Reloading ipsec manual keys/policies."
     85  1.11.14.1      tls 	ipsec_stop
     86  1.11.14.1      tls 	ipsec_start
     87        1.1   itojun }
     88        1.1   itojun 
     89        1.1   itojun load_rc_config $name
     90        1.1   itojun run_rc_command "$1"
     91