#!/bin/sh . /lib/functions.sh . /lib/functions/network.sh proto="4" [ -f /proc/net/ipv6_route ] && proto="4 6" config_load freifunk-policyrouting config_get enable pr enable config_get fallback pr fallback config_get zones pr zones if [ "$ACTION" = "ifup" ] && [ "$enable" = "1" ]; then network_get_subnet net $INTERFACE network_get_subnet6 net6 $INTERFACE network_get_physdev dev $INTERFACE if [ "$net" != "" -a -n "$dev" ]; then eval $(/bin/ipcalc.sh $net) if [ "$PREFIX" != "0" ]; then if [ ! "$(ip r s t olsr-default |grep "throw $NETWORK/$PREFIX")" ]; then ip r a throw $NETWORK/$PREFIX table olsr-default if [ "$?" = 0 ]; then logger -s -t policyrouting "Add route: throw $NETWORK/$PREFIX table olsr-default" else logger -s -t policyrouting "Error! Could not add route: throw $NETWORK/$PREFIX table olsr-default" fi fi fi if [ -n "$net6" ]; then if [ ! "$(ip -6 r s t olsr-default |grep "throw $net6")" ]; then rule="throw $net6 table olsr-default dev $dev" ip -6 r a $rule if [ "$?" = 0 ]; then logger -s -t policyrouting "Add route: $rule (IPv6)" else logger -s -t policyrouting "Error! Could not add route: $rule (IPv6)" fi fi fi networks="" for z in $zones; do network_zone="$(uci -q get firewall.zone_${z}.network)" if [ -z "$network_zone" ]; then network_zone="$z" fi networks="$networks $network_zone" done for n in $networks; do if [ "$INTERFACE" = "$n" ]; then for p in $proto; do if [ ! "$(ip -$p ru s | grep "from all iif $dev lookup olsr-default")" ]; then ip -$p rule add dev "$dev" lookup olsr-default prio 20000 if [ "$?" = 0 ]; then logger -s -t policyrouting "Use mesh gateway for interface $dev (IPv$p)" if [ -z "$(uci -P /var/state get freifunk-policyrouting.${INTERFACE})" ]; then uci -P /var/state set freifunk-policyrouting.${INTERFACE}="state" fi uci -P /var/state set freifunk-policyrouting.${INTERFACE}.device="$dev" else logger -s -t policyrouting "Error: Could not add rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)" fi fi done fi done fi fi if [ "$ACTION" = "ifdown" ]; then dev="$(uci -q -P /var/state get freifunk-policyrouting.${INTERFACE}.device)" if [ -n "$dev" ]; then networks="" for z in $zones; do network_zone="$(uci -q get firewall.zone_${z}.network)" if [ -z "$network_zone" ]; then network_zone="$z" fi networks="$networks $network_zone" done for n in $networks; do if [ "$INTERFACE" = "$n" ]; then for p in $proto; do if [ "$(ip -$p ru s | grep "from all iif $dev lookup olsr-default")" ]; then ip -$p rule del dev "$dev" lookup olsr-default prio 20000 if [ "$?" = 0 ]; then logger -s -t policyrouting "Remove rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)" else logger -s -t policyrouting "Error! Could not remove rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)" fi fi done fi done fi fi