#!/bin/sh /etc/rc.common # Copyright (C) 2019 Ycarus (Yannick Chabanois) 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 mode localip remoteip externalip gateway local prog="$PROG" 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 # dsvpn adds the tun address once, on its first connection, and procd # keeps an unchanged instance across a reload: flushing on every reload # left the running tunnel without an address. Only the tun of an # instance that is going away (restart) needs it. procd_running would # reset the jshn message start_service is building, ask ubus directly. if [ -d "/sys/class/net/$dev" ] && [ "$(ubus call service list "{\"name\":\"${PROG_NAME}\"}" 2>/dev/null | jsonfilter -q -l 1 -e "@['${PROG_NAME}'].instances[*].running")" != "true" ]; then ip addr flush dev "$dev" sleep 2 fi _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 # The arguments are positional: an empty one would shift the next ones # into its place, dsvpn takes "auto" for each instead. The gateway is the # route's "via": an on-link route has none, and a name can't be routed gateway="$(ip route get "$host" 2>/dev/null | awk '{ for (i = 1; i < NF; i++) if ($i == "via") { print $(i + 1); exit } }')" procd_open_instance procd_set_param command ${prog} ${mode} \ "$key_file" \ $host \ $port \ $dev \ ${localip:-auto} \ ${remoteip:-auto} [ "$mode" = "client" ] && procd_append_param command ${gateway:-auto} [ "$mode" = "server" ] && procd_append_param command ${externalip:-auto} 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 }