diff --git a/source/scripts/init/c_registration/15_ssh_server.c b/source/scripts/init/c_registration/15_ssh_server.c index 6143ee73..42286e24 100644 --- a/source/scripts/init/c_registration/15_ssh_server.c +++ b/source/scripts/init/c_registration/15_ssh_server.c @@ -50,6 +50,9 @@ const char* SERVICE_CUSTOM_EVENTS[] = { #endif #if defined(_ARRIS_XB6_PRODUCT_REQ_) "wan-status|/etc/utopia/service.d/service_sshd.sh", +#endif +#if defined(_WNXL11BWL_PRODUCT_REQ_) + "mesh_wan_linkstatus|/etc/utopia/service.d/service_sshd.sh", #endif NULL }; diff --git a/source/scripts/init/service.d/service_sshd.sh b/source/scripts/init/service.d/service_sshd.sh index a2ba75cc..181ff812 100755 --- a/source/scripts/init/service.d/service_sshd.sh +++ b/source/scripts/init/service.d/service_sshd.sh @@ -134,6 +134,29 @@ get_listen_params() { fi } +# wait_for_iface_ip +# Blocks up to 300 seconds (150 x 2s) waiting for an IPv4 address on the +# given interface. Prints the IP to stdout on success; logs an error and +# returns 1 on timeout. +wait_for_iface_ip() { + local IFACE="$1" + local SLEEP_INTERVAL=2 + local RETRIES=0 + local MAX_RETRIES=150 # 150 x 2s = 300s max wait + while [ $RETRIES -lt $MAX_RETRIES ]; do + sleep $SLEEP_INTERVAL + WAITED_IP=`ip -4 addr show dev $IFACE scope global | awk '/inet/{print $2}' | cut -d '/' -f1` + if [ -n "$WAITED_IP" ]; then + echo_t "[utopia] $IFACE got IP $WAITED_IP after $((RETRIES * SLEEP_INTERVAL)) seconds" + echo "$WAITED_IP" + return 0 + fi + RETRIES=$((RETRIES + 1)) + done + echo_t "[utopia] ERROR: Timed out waiting for IP on $IFACE after $((MAX_RETRIES * SLEEP_INTERVAL)) seconds" + return 1 +} + do_start() { #DIR_NAME=/tmp/home/admin #if [ ! -d $DIR_NAME ] ; then @@ -208,18 +231,45 @@ do_start() { commandString="$commandString -p [$CM_IPV4]:22" fi elif [ "$BOX_TYPE" = "WNXL11BWL" ]; then - CM_IP=`ip -4 addr show dev $CMINTERFACE scope global | awk '/inet/{print $2}' | cut -d '/' -f1 | head -n1` - if [ ! -z $CM_IP ]; then - commandString="$commandString -p [$CM_IP]:22" - fi + echo_t "[utopia] devicemode `deviceinfo.sh -mode`" + echo_t "[utopia] route `route -n`" + echo_t "[utopia] CMINTERFACE $CMINTERFACE " + CM_IPv6=`ip -6 addr show dev wwan0 scope global | awk '/inet/{print $2}' | cut -d '/' -f1 | head -n1` - if [ ! -z $CM_IPv6 ]; then + if [ ! -z "$CM_IPv6" ]; then commandString="$commandString -p [$CM_IPv6]:22" fi CM_IPv4=`ip -4 addr show dev wwan0 scope global | awk '/inet/{print $2}' | cut -d '/' -f1 | head -n1` - if [ ! -z $CM_IPv4 ]; then + if [ ! -z "$CM_IPv4" ]; then commandString="$commandString -p [$CM_IPv4]:22" fi + if [ "$CMINTERFACE" != "wwan0" ]; then + CM_IP=`ip -4 addr show dev $CMINTERFACE scope global | awk '/inet/{print $2}' | cut -d '/' -f1 | head -n1` + if [ ! -z "$CM_IP" ]; then + commandString="$commandString -p [$CM_IP]:22" + else + DEVICE_MODE=`deviceinfo.sh -mode` + if [ "$DEVICE_MODE" = "Extender" ]; then + MESH_WAN_STATUS=`sysevent get mesh_wan_linkstatus` + if [ "$MESH_WAN_STATUS" = "up" ]; then + echo_t "[utopia] $CMINTERFACE has no IP (Extender mode, mesh WAN up), waiting up to 300s" + CM_IP=`wait_for_iface_ip "$CMINTERFACE"` + if [ -n "$CM_IP" ]; then + commandString="$commandString -p [$CM_IP]:22" + else + echo_t "[utopia] ERROR: $CMINTERFACE did not get an IP after 300s, dropbear will start without it" + fi + else + echo_t "[utopia] $CMINTERFACE has no IP and mesh_wan_linkstatus is not up, skipping wait" + fi + else + echo_t "[utopia] $CMINTERFACE has no IP and device is not in Extender mode, skipping wait" + fi + fi + echo_t "[utopia] CM_IP $CM_IP " + fi + + echo_t "[utopia] commandString $commandString" else CM_IP="" if ([ "$BOX_TYPE" = "rpi" ] || [ "$BOX_TYPE" = "bpi" ]) ;then @@ -452,6 +502,19 @@ case "$1" in service_stop service_start ;; + mesh_wan_linkstatus) + if [ "$BOX_TYPE" = "WNXL11BWL" ]; then + echo_t "mesh_wan_linkstatus_value $2" + echo_t "mesh_wan_linkstatus_sysevent `sysevent get mesh_wan_linkstatus`" + if [ "$2" = "up" ]; then + DEVICE_MODE=`deviceinfo.sh -mode` + if [ "$DEVICE_MODE" = "Extender" ]; then + service_stop + service_start + fi + fi + fi + ;; *) echo "Usage: $SELF_NAME [${SERVICE_NAME}-start|${SERVICE_NAME}-stop|${SERVICE_NAME}-restart|ssh_server_restart|lan-status|wan-status]" >&2