2015-04-01 09:42:53 -06:00
|
|
|
#!/bin/sh /etc/rc.common
|
|
|
|
# Copyright (C) 2006-2011 OpenWrt.org
|
|
|
|
|
|
|
|
START=84
|
|
|
|
STOP=85
|
|
|
|
|
2015-04-15 16:39:45 -06:00
|
|
|
CLIENT_CONF=/tmp/vtun/vtund.conf
|
2015-04-01 09:42:53 -06:00
|
|
|
|
|
|
|
# Starting tun interface is tun50
|
2022-03-13 21:19:04 -06:00
|
|
|
TUNNUM=$((`uci get aredn.@tunnel[0].maxclients 2> /dev/null` + 50))
|
|
|
|
MAXTUNNUM=$((`uci get aredn.@tunnel[0].maxservers 2> /dev/null` + $TUNNUM - 1))
|
2015-04-01 09:42:53 -06:00
|
|
|
|
|
|
|
######### UTILITY FUNCTIONS ###########
|
|
|
|
# return the number of "type" sections
|
|
|
|
uci_type_count() {
|
|
|
|
local t="$1"
|
|
|
|
local rc="$2"
|
|
|
|
local count=`uci show vtun|egrep "@$t\[.\]=$t"|wc -l`
|
|
|
|
eval "$rc=$count"
|
|
|
|
}
|
|
|
|
|
|
|
|
######### UTILITY FUNCTIONS END ###########
|
2017-04-26 11:06:24 -06:00
|
|
|
default_config() {
|
|
|
|
local cfg="$1"
|
|
|
|
local new_file="$2"
|
|
|
|
local persist
|
|
|
|
|
|
|
|
config_get persist "$persist" persist "yes"
|
|
|
|
|
|
|
|
echo "default {" >> $new_file
|
|
|
|
echo " persist $persist;" >> $new_file
|
|
|
|
echo "}" >> $new_file
|
|
|
|
echo " " >> $new_file
|
|
|
|
}
|
2015-04-01 09:42:53 -06:00
|
|
|
|
|
|
|
options_config() {
|
|
|
|
local cfg="$1"
|
|
|
|
local new_file="$2"
|
|
|
|
local port timeout syslog ppp ifconfig route firewall ip
|
|
|
|
|
|
|
|
config_get port "$cfg" port "5525"
|
|
|
|
config_get timeout "$cfg" timeout "60"
|
|
|
|
config_get syslog "$cfg" syslog "daemon"
|
|
|
|
config_get ppp "$cfg" ppp "/usr/sbin/pppd"
|
|
|
|
config_get ifconfig "$cfg" ifconfig "/sbin/ifconfig"
|
|
|
|
config_get route "$cfg" route "/sbin/route"
|
2022-12-22 13:22:49 -07:00
|
|
|
config_get firewall "$cfg" firewall "/usr/sbin/nft"
|
2015-04-01 09:42:53 -06:00
|
|
|
config_get ip "$cfg" ip "/sbin/ip"
|
|
|
|
|
|
|
|
echo "options {" >> $new_file
|
|
|
|
echo " port $port;" >> $new_file
|
|
|
|
echo " timeout $timeout;" >> $new_file
|
|
|
|
echo " syslog $syslog;" >> $new_file
|
|
|
|
echo " ppp $ppp;" >> $new_file
|
|
|
|
echo " ifconfig $ifconfig;" >> $new_file
|
|
|
|
echo " route $route;" >> $new_file
|
|
|
|
echo " firewall $firewall;" >> $new_file
|
|
|
|
echo " ip $ip;" >> $new_file
|
|
|
|
echo "}" >> $new_file
|
|
|
|
echo " " >> $new_file
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# CONNECT THIS CLIENT TO A SERVER
|
|
|
|
to_server_config() {
|
|
|
|
local cfg="$1"
|
|
|
|
local new_file="$2"
|
|
|
|
local enabled host pwd net node netip clientip serverip
|
|
|
|
|
2021-03-21 18:13:34 -06:00
|
|
|
if [ $TUNNUM -le $MAXTUNNUM ]
|
2015-04-01 09:42:53 -06:00
|
|
|
then
|
|
|
|
config_get_bool enabled "$cfg" enabled
|
|
|
|
config_get node "$cfg" node
|
|
|
|
config_get host "$cfg" host
|
|
|
|
config_get passwd "$cfg" passwd
|
|
|
|
config_get netip "$cfg" netip
|
|
|
|
config_get clientip "$cfg" clientip
|
|
|
|
config_get serverip "$cfg" serverip
|
|
|
|
|
|
|
|
if [ $enabled -eq 1 ]
|
|
|
|
then
|
|
|
|
echo "$node {" >> $new_file
|
|
|
|
echo " passwd $passwd;" >> $new_file
|
|
|
|
echo " device tun${TUNNUM};" >> $new_file
|
|
|
|
echo " up {" >> $new_file
|
|
|
|
echo -e " ifconfig \"%% ${clientip} netmask 255.255.255.252 pointopoint ${serverip} mtu 1450\";" >> $new_file
|
|
|
|
echo -e " route \"add -net ${netip}/30 gw ${serverip}\";" >> $new_file
|
|
|
|
echo " };" >> $new_file
|
|
|
|
echo " down {" >> $new_file
|
|
|
|
echo " };" >> $new_file
|
|
|
|
echo "}" >> $new_file
|
|
|
|
echo " " >> $new_file
|
|
|
|
TUNNUM=$(( TUNNUM + 1 ))
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "## TOO MANY TUNNEL CONNECTIONS (${TUNNUM}) ##" >> $new_file
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
start_clients() {
|
|
|
|
local cfg="$1"
|
|
|
|
local new_file="$2"
|
2015-04-15 16:39:45 -06:00
|
|
|
local enabled host passwd net node netip clientip serverip
|
2015-04-01 09:42:53 -06:00
|
|
|
config_get_bool enabled "$cfg" enabled
|
|
|
|
|
|
|
|
if [ $enabled -eq 1 ]
|
|
|
|
then
|
|
|
|
config_get node "$cfg" node
|
|
|
|
config_get host "$cfg" host
|
|
|
|
/usr/sbin/vtund -p -f $CLIENT_CONF ${node} $host
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
apply_uci_config() {
|
2015-04-15 16:39:45 -06:00
|
|
|
# sh -c '. /lib/functions.sh; include /lib/config; uci_apply_defaults'
|
|
|
|
sh -c '. /lib/functions.sh; include /lib/config'
|
2015-04-01 09:42:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2015-05-24 13:59:11 -06:00
|
|
|
# only if vtun is installed and ind mesh mode
|
|
|
|
configmode=$(uci -q -c /etc/local/uci/ get hsmmmesh.settings.config)
|
|
|
|
if [ -x "/usr/sbin/vtund" -a "$configmode" = "mesh" ]
|
2015-04-01 09:42:53 -06:00
|
|
|
then
|
|
|
|
apply_uci_config
|
|
|
|
config_load vtun
|
2017-04-26 11:06:24 -06:00
|
|
|
|
2015-04-26 22:21:42 -06:00
|
|
|
#if options section doesn't exist, add it
|
|
|
|
j=`uci get vtun.@options[0]`
|
2017-04-26 11:06:24 -06:00
|
|
|
if [ $? -ne 0 ]
|
2015-04-26 22:21:42 -06:00
|
|
|
then
|
|
|
|
j=`uci add vtun options`
|
|
|
|
j='uci commit vtun'
|
|
|
|
config_load vtun
|
|
|
|
fi
|
2017-04-26 11:06:24 -06:00
|
|
|
|
|
|
|
#if default section doesn't exist, add it
|
|
|
|
j=`uci get vtun.@default[0]`
|
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
j=`uci add vtun default`
|
|
|
|
j='uci commit vtun'
|
|
|
|
config_load vtun
|
|
|
|
fi
|
|
|
|
|
2015-04-01 09:42:53 -06:00
|
|
|
# Remove the current vtun config file
|
2015-04-28 23:37:33 -06:00
|
|
|
mkdir -p /tmp/vtun
|
2015-04-01 09:42:53 -06:00
|
|
|
rm -f $CLIENT_CONF
|
|
|
|
|
|
|
|
# config_foreach network_config network
|
|
|
|
config_foreach options_config options "$CLIENT_CONF"
|
2017-04-26 11:06:24 -06:00
|
|
|
config_foreach default_config default "$CLIENT_CONF"
|
2015-04-01 09:42:53 -06:00
|
|
|
config_foreach to_server_config server "$CLIENT_CONF"
|
|
|
|
|
|
|
|
# STARTUP CLIENT TO SERVER CONNECTIONS
|
|
|
|
config_foreach start_clients server "$CLIENT_CONF"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
# only if vtun is installed
|
|
|
|
if [[ -x "/usr/sbin/vtund" ]]
|
|
|
|
then
|
|
|
|
# find all the vtund CLIENT processess...
|
2015-04-20 22:53:57 -06:00
|
|
|
for x in `ps -w|grep -F 'vtund[c]:'|grep -v grep|awk '{print $1}'`
|
|
|
|
do
|
|
|
|
s=`echo $s $x`
|
|
|
|
done
|
|
|
|
kill $s
|
2015-04-01 09:42:53 -06:00
|
|
|
fi
|
|
|
|
}
|