30-policyrouting 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/network.sh
  4. proto="4"
  5. [ -f /proc/net/ipv6_route ] && proto="4 6"
  6. config_load freifunk-policyrouting
  7. config_get enable pr enable
  8. config_get fallback pr fallback
  9. config_get strict pr strict
  10. config_get zones pr zones
  11. if [ "$ACTION" = "ifup" ] && [ "$enable" = "1" ]; then
  12. network_get_subnet net $INTERFACE
  13. network_get_subnet6 net6 $INTERFACE
  14. network_get_physdev dev $INTERFACE
  15. if [ "$net" != "" -a -n "$dev" ]; then
  16. eval $(/bin/ipcalc.sh $net)
  17. if [ "$PREFIX" != "0" -a "$NETWORK" != "127.0.0.0" ]; then
  18. if [ ! "$(ip r s t localnets |grep "$NETWORK/$PREFIX dev")" ]; then
  19. cmd="ip r a $NETWORK/$PREFIX dev $dev table localnets"
  20. $cmd
  21. if [ "$?" = 0 ]; then
  22. logger -s -t policyrouting "Add route: $cmd"
  23. else
  24. logger -s -t policyrouting "Error! Could not add route: $cmd"
  25. fi
  26. fi
  27. fi
  28. if [ -n "$net6" ]; then
  29. cmd="ip -6 r a $net6 dev $dev table localnets"
  30. $cmd 2>&1 > /dev/null
  31. if [ "$?" = 0 ]; then
  32. logger -s -t policyrouting "Add route: $cmd (IPv6)"
  33. fi
  34. fi
  35. networks=""
  36. for z in $zones; do
  37. network_zone="$(uci -q get firewall.zone_${z}.network)"
  38. if [ -z "$network_zone" ]; then
  39. network_zone="$z"
  40. fi
  41. networks="$networks $network_zone"
  42. done
  43. for n in $networks; do
  44. if [ "$INTERFACE" = "$n" ]; then
  45. for p in $proto; do
  46. if [ ! "$(ip -$p ru s | grep "from all iif $dev lookup olsr-default")" ]; then
  47. ip -$p rule add dev "$dev" lookup olsr-default prio 20000
  48. if [ "$strict" != 0 ]; then
  49. ip -$p rule add dev "$dev" unreachable prio 20001
  50. fi
  51. if [ "$?" = 0 ]; then
  52. logger -s -t policyrouting "Use mesh gateway for interface $dev (IPv$p)"
  53. if [ -z "$(uci -P /var/state get freifunk-policyrouting.${INTERFACE})" ]; then
  54. uci -P /var/state set freifunk-policyrouting.${INTERFACE}="state"
  55. fi
  56. uci -P /var/state set freifunk-policyrouting.${INTERFACE}.device="$dev"
  57. else
  58. logger -s -t policyrouting "Error: Could not add rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)"
  59. fi
  60. fi
  61. done
  62. fi
  63. done
  64. fi
  65. fi
  66. if [ "$ACTION" = "ifdown" ]; then
  67. dev="$(uci -q -P /var/state get freifunk-policyrouting.${INTERFACE}.device)"
  68. if [ -n "$dev" ]; then
  69. networks=""
  70. for z in $zones; do
  71. network_zone="$(uci -q get firewall.zone_${z}.network)"
  72. if [ -z "$network_zone" ]; then
  73. network_zone="$z"
  74. fi
  75. networks="$networks $network_zone"
  76. done
  77. for n in $networks; do
  78. if [ "$INTERFACE" = "$n" ]; then
  79. for p in $proto; do
  80. if [ "$(ip -$p ru s | grep "from all iif $dev lookup olsr-default")" ]; then
  81. ip -$p rule del dev "$dev" lookup olsr-default prio 20000
  82. ip -$p rule del dev "$dev" unreachable prio 20001
  83. if [ "$?" = 0 ]; then
  84. logger -s -t policyrouting "Remove rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)"
  85. else
  86. logger -s -t policyrouting "Error! Could not remove rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)"
  87. fi
  88. fi
  89. done
  90. fi
  91. done
  92. fi
  93. fi