Files
op-packages/dsvpn/files/init
T

132 lines
3.3 KiB
Bash
Executable File

#!/bin/sh /etc/rc.common
# Copyright (C) 2019 Ycarus (Yannick Chabanois) <ycarus@zugaina.org> for OpenMPTCProuter project
# network.globals.multipath='disable' means the kernel is running with
# net.mptcp.enabled=0. Asking for an MPTCP socket then fails outright with
# ENOPROTOOPT ("Protocol not available") -- the kernel has no implicit
# fallback to plain TCP, every caller has to do it itself, and most don't.
# Only an explicit "disable" counts, so this package keeps its stock
# behaviour wherever network.globals.multipath doesn't exist at all.
_omr_mptcp_globally_disabled() {
[ "$(uci -q get network.globals.multipath)" = "disable" ]
}
START=90
STOP=10
USE_PROCD=1
PROG_NAME=dsvpn
PROG=/usr/sbin/${PROG_NAME}
_log() {
logger -p daemon.info -t ${PROG_NAME} "$@"
}
_err() {
logger -p daemon.err -t ${PROG_NAME} "$@"
}
validate_section() {
uci_validate_section dsvpn dsvpn "${1}" \
'enable:bool:0' \
'key:string' \
'host:host' \
'port:port' \
'dev:string' \
'localip:host' \
'remoteip:host' \
'mode:string:client' \
'externalip:string:auto'
}
version_over_5_4() {
MAJOR_VERSION=$(uname -r | awk -F '.' '{print $1}')
MINOR_VERSION=$(uname -r | awk -F '.' '{print $2}' | awk -F '-' '{print $1}')
if [ $MAJOR_VERSION -ge 5 ] && [ $MINOR_VERSION -gt 13 ] || [ $MAJOR_VERSION -gt 5 ] ; then
return 0
else
return 1
fi
}
start_instance() {
local enable key host port dev
local key_dir="/var/run/${PROG_NAME}"
local key_file="${key_dir}/${1}.key"
validate_section "${1}" || {
_err "validation failed"
return 1
}
[ "${enable}" = "1" ] || return 1
[ -n "${key}" ] || return 1
[ "${key}" != "secretkey" ] || return 1
[ -z "${host}" ] && host="auto"
[ -z "${port}" ] && port="auto"
[ -n "${dev}" ] || return 1
mkdir -p "$key_dir"
chmod 0700 "$key_dir"
( umask 077; printf '%s\n' "$key" > "$key_file" ) || {
_err "can't create key file"
return 1
}
key=""
# "uci get network.omrvpn" is the section type, never the device: this
# ran on every start, and a bare "uci commit" flushed every package's
# pending changes, not only this one
if [ -n "$(uci -q get network.omrvpn)" ] && [ "$(uci -q get network.omrvpn.device)" != "$dev" ]; then
uci -q set network.omrvpn.device=$dev
uci -q commit network
fi
ip addr flush $dev
sleep 2
_log "starting ${PROG_NAME} ${1} instance $*"
# mptcpize LD_PRELOADs a socket() that always asks for IPPROTO_MPTCP and
# never falls back, so with MPTCP globally off dsvpn cannot create its
# socket at all ("Couldn't create connection: Protocol not available")
# and procd just respawns it forever. Run it unwrapped instead.
if version_over_5_4 && ! _omr_mptcp_globally_disabled; then
PROG="mptcpize run ${PROG}"
fi
procd_open_instance
procd_set_param command ${PROG} ${mode} \
"$key_file" \
$host \
$port \
$dev \
${localip:+$localip} \
${remoteip:+$remoteip}
[ "$mode" = "client" ] && procd_append_param command $(ip r get $host | awk '{print $3}' | tr -d "\n")
[ "$mode" = "server" ] && procd_append_param command externalip
procd_set_param respawn 0 30 5
procd_set_param nice 10
procd_set_param file "$key_file"
procd_set_param stdout 1
procd_set_param stderr 1
procd_close_instance
}
start_service() {
config_load dsvpn
config_foreach start_instance dsvpn
}
service_triggers() {
procd_add_reload_trigger dsvpn network
}