2014-04-04 20:34:15 -06:00
#!/bin/sh
2018-11-27 22:37:59 -07:00
configmode=$(uci -q -c /etc/local/uci/ get hsmmmesh.settings.config)
2014-04-04 20:34:15 -06:00
if [ "$configmode" != "mesh" ] ; then exit 0; fi
2022-10-04 18:00:16 -06:00
xlink=$(uci -q -c /etc/config.mesh/ show xlink | grep "ifname='${DEVICE}'")
2014-04-04 20:34:15 -06:00
# This section will generate rtnetlink errors when the rule doesn't exist.
# This will be most common in the case of ifup.
# On initial boot no route will be present, and on most interface restarts
# the route should already be gone from ifdown but some scripts do not call
# ifup/ifdown to control the interface so we will want to make sure to
# run a clean before hand to avoid duplicate rules.
if [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "ifup" ] ; then
echo "Deleting specific routing rules that may exist."
2022-10-04 18:00:16 -06:00
if [ "$INTERFACE" == "wifi" ] || [ "$INTERFACE" == "dtdlink" ] || [ "${INTERFACE:0:3}" == "tun" ] || [ "$xlink" != "" ] ; then
2014-04-07 21:27:50 -06:00
ip rule del pref 20010 iif $DEVICE lookup 29
ip rule del pref 20020 iif $DEVICE lookup 30
ip rule del pref 20080 iif $DEVICE lookup 31
ip rule del pref 20090 iif $DEVICE lookup main
2014-04-04 20:34:15 -06:00
ip rule del pref 20099 iif $DEVICE unreachable
fi
if [ "$INTERFACE" == "lan" ] ; then
2014-04-07 21:27:50 -06:00
ip rule del pref 30010 iif $DEVICE lookup 29
ip rule del pref 30020 iif $DEVICE lookup 30
2014-12-12 00:42:27 -07:00
ip rule del pref 30090 iif $DEVICE lookup main
ip rule del pref 30099 iif $DEVICE lookup 31
2014-04-04 20:34:15 -06:00
fi
if [ "$INTERFACE" == "loopback" ] ; then
2014-04-07 21:27:50 -06:00
ip rule del pref 30210 lookup 29
ip rule del pref 30220 lookup 30
ip rule del pref 30290 lookup main
2014-04-04 20:34:15 -06:00
ip rule del pref 30299 lookup 31
fi
fi
if [ "$ACTION" = "ifup" ] ; then
2022-08-11 01:19:33 -06:00
is_olsrgw=$(/sbin/uci -q get aredn.@wan[0].olsrd_gw)
2014-04-04 20:34:15 -06:00
# Set routes for wifi or device to device linking
# unreachable rule is needed to ensure packets do not traverse a rule later that considers routing to another network.
echo "Setting routing rules."
2022-10-04 18:00:16 -06:00
if [ "$INTERFACE" == "wifi" ] || [ "$INTERFACE" == "dtdlink" ] || [ "${INTERFACE:0:3}" = "tun" ] || [ "$xlink" != "" ]; then
2021-11-19 14:48:45 -07:00
if [ "$(/sbin/uci -q get aredn.@dmz[0].mode)" != "0" ] ; then
2014-04-07 21:27:50 -06:00
ip rule add pref 20010 iif $DEVICE lookup 29
fi
ip rule add pref 20020 iif $DEVICE lookup 30
ip rule add pref 20080 iif $DEVICE lookup 31
2014-04-04 20:34:15 -06:00
if [ $is_olsrgw -eq 1 ] ; then
2014-04-07 21:27:50 -06:00
ip rule add pref 20090 iif $DEVICE lookup main
2014-04-04 20:34:15 -06:00
fi
ip rule add pref 20099 iif $DEVICE unreachable
fi
2014-04-07 21:27:50 -06:00
# Makes sure the lan interface is allowed to hit the local interfaces(29) mesh(30), meshgw(31), and local routes (main) Local ethernet is trusted to use additonal rules.
2014-04-04 20:34:15 -06:00
if [ "$INTERFACE" == "lan" ] ; then
2014-04-07 21:27:50 -06:00
ip rule add pref 30010 iif $DEVICE lookup 29
ip rule add pref 30020 iif $DEVICE lookup 30
2014-12-12 00:42:27 -07:00
ip rule add pref 30090 iif $DEVICE lookup main
ip rule add pref 30099 iif $DEVICE lookup 31
2014-04-07 21:27:50 -06:00
# Lets go ahead and set the route to the local network here since we only need to be able to route to it after the interface comes up.
2018-11-27 22:37:59 -07:00
lan_ipaddr=$(uci -q get network.lan.ipaddr)
lan_netmask=$(uci -q get network.lan.netmask)
bridge=$(uci -q get network.lan.type)
if [ $bridge = "bridge" ] ; then
lan_ifname="br-lan"
else
2022-12-22 13:22:49 -07:00
lan_ifname=$(uci -q get network.lan.device | cut -f1)
2018-11-27 22:37:59 -07:00
fi
2014-04-07 21:27:50 -06:00
if [ "$lan_ifname" != "" ] && [ "$lan_ipaddr" != "" ] && [ "$lan_netmask" != "" ] ; then
2018-11-27 22:37:59 -07:00
lan_networkip=$(ipcalc.sh $lan_ipaddr $lan_netmask|grep NETWORK|cut -d'=' -f2)
2014-04-07 21:27:50 -06:00
ip route add $lan_networkip/$lan_netmask table 29 dev $lan_ifname
fi
2014-04-04 20:34:15 -06:00
fi
# Handles setting up rules for local resolution of routes. MeshGW should be last for localhost otherwise ping test of olsrd will break
if [ "$INTERFACE" == "loopback" ] ; then
2014-04-07 21:27:50 -06:00
ip rule add pref 30210 lookup 29
ip rule add pref 30220 lookup 30
ip rule add pref 30290 lookup main
2014-04-04 20:34:15 -06:00
ip rule add pref 30299 lookup 31
fi
fi
# Ensure we send a good exit code
exit 0;