#!/bin/sh
# configure network gateway ip rules on wwan up
#
# we always want to route incoming WWAN packets out on the same interface. This
# is important for VLAN failover, otherwise the reply to incoming WWAN packets may
# get routed out on the VLAN interface. We use table 261 for this (# is arbitrary)
# ... delete existing rules first...
WWAN_ADDR=`cat /var/run/wwan_ipv4`
WWAN_GW=`cat /var/run/wwan_gw`

if [ "$WWAN_ADDR" != "" ] && [ "$WWAN_GW" != "" ]; then
    old_rules=`/sbin/ip rule list | grep 261 | awk '{print $3}'`
    for i in $old_rules
    do
            logger -p user.info -t network "deleting incoming WWAN routing rule: from $i, table 261"
            /sbin/ip rule del from $i table 261
    done
    /sbin/ip route flush table 261
    # create new rule
    logger -p user.info -t network "adding incoming WWAN default route"
    /sbin/ip rule add from $WWAN_ADDR table 261
    /sbin/ip route add default via $WWAN_GW table 261
fi