You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
5.2 KiB

  1. #!/bin/sh
  2. . /lib/functions.sh
  3. IP="/usr/sbin/ip -4"
  4. IPS="/usr/sbin/ipset"
  5. IPT="/usr/sbin/iptables -t mangle -w"
  6. help()
  7. {
  8. cat <<EOF
  9. Syntax: mwan3 [command]
  10. Available commands:
  11. start Load iptables rules, ip rules and ip routes
  12. stop Unload iptables rules, ip rules and ip routes
  13. restart Reload iptables rules, ip rules and ip routes
  14. ifup <iface> Load rules and routes for specific interface
  15. ifdown <iface> Unload rules and routes for specific interface
  16. interfaces Show interfaces status
  17. policies Show policies status
  18. rules Show rules status
  19. status Show all status
  20. EOF
  21. }
  22. ifdown()
  23. {
  24. if [ -z "$1" ]; then
  25. echo "Error: Expecting interface. Usage: mwan3 ifdown <interface>" && exit 0
  26. fi
  27. if [ -n "$2" ]; then
  28. echo "Error: Too many arguments. Usage: mwan3 ifdown <interface>" && exit 0
  29. fi
  30. if [ -e /var/run/mwan3track-$1.pid ] ; then
  31. kill $(cat /var/run/mwan3track-$1.pid)
  32. rm /var/run/mwan3track-$1.pid
  33. fi
  34. ACTION=ifdown INTERFACE=$1 /sbin/hotplug-call iface
  35. }
  36. ifup()
  37. {
  38. local device enabled
  39. config_load mwan3
  40. if [ -z "$1" ]; then
  41. echo "Expecting interface. Usage: mwan3 ifup <interface>" && exit 0
  42. fi
  43. if [ -n "$2" ]; then
  44. echo "Too many arguments. Usage: mwan3 ifup <interface>" && exit 0
  45. fi
  46. config_get enabled "$1" enabled 0
  47. device=$(uci -p /var/state get network.$1.ifname) &> /dev/null
  48. if [ -n "$device" ] ; then
  49. [ "$enabled" -eq 1 ] && ACTION=ifup INTERFACE=$1 DEVICE=$device /sbin/hotplug-call iface
  50. fi
  51. }
  52. interfaces()
  53. {
  54. local device enabled iface_id tracking
  55. config_load mwan3
  56. echo "Interface status:"
  57. check_iface_status()
  58. {
  59. let iface_id++
  60. device=$(uci -p /var/state get network.$1.ifname) &> /dev/null
  61. if [ -z "$device" ]; then
  62. echo " interface $1 is unknown"
  63. return 0
  64. fi
  65. config_get enabled "$1" enabled 0
  66. if [ -n "$(ps -w | grep mwan3track | grep -v grep | sed '/.*\/usr\/sbin\/mwan3track \([^ ]*\) .*$/!d;s//\1/' | awk '$1 == "'$1'"')" ]; then
  67. tracking="active"
  68. else
  69. tracking="down"
  70. fi
  71. if [ -n "$($IP rule | awk '$5 == "'$device'"')" -a -n "$($IPT -S mwan3_iface_$1 2> /dev/null)" -a -n "$($IP route list table $iface_id default dev $device 2> /dev/null)" ]; then
  72. if [ -n "$(uci -p /var/state get mwan3.$1.track_ip 2> /dev/null)" ]; then
  73. echo " interface $1 is online (tracking $tracking)"
  74. else
  75. echo " interface $1 is online"
  76. fi
  77. elif [ -n "$($IP rule | awk '$5 == "'$device'"')" -o -n "$($IPT -S mwan3_iface_$1 2> /dev/null)" -o -n "$($IP route list table $iface_id default dev $device 2> /dev/null)" ]; then
  78. echo " interface $1 error"
  79. else
  80. if [ "$enabled" -eq 1 ]; then
  81. if [ -n "$(uci -p /var/state get mwan3.$1.track_ip 2> /dev/null)" ]; then
  82. echo " interface $1 is offline (tracking $tracking)"
  83. else
  84. echo " interface $1 is offline"
  85. fi
  86. else
  87. echo " interface $1 is disabled"
  88. fi
  89. fi
  90. }
  91. config_foreach check_iface_status interface
  92. echo -e
  93. }
  94. policies()
  95. {
  96. local percent policy share total_weight weight iface
  97. for policy in $($IPT -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
  98. echo "Policy $policy:" | sed 's/mwan3_policy_//'
  99. [ -n "$total_weight" ] || total_weight=$($IPT -S $policy | cut -s -d'"' -f2 | head -1 | awk '{print $3}')
  100. if [ ! -z "${total_weight##*[!0-9]*}" ]; then
  101. for iface in $($IPT -S $policy | cut -s -d'"' -f2 | awk '{print $1}'); do
  102. weight=$($IPT -S $policy | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}')
  103. percent=$(($weight*100/$total_weight))
  104. echo " $iface ($percent%)"
  105. done
  106. else
  107. echo " $($IPT -S $policy | sed '/.*--comment \([^ ]*\) .*$/!d;s//\1/;q')"
  108. fi
  109. echo -e
  110. unset iface
  111. unset total_weight
  112. done
  113. }
  114. rules()
  115. {
  116. local address
  117. if [ -n "$($IPT -S mwan3_connected 2> /dev/null)" ]; then
  118. echo "Known networks:"
  119. for address in $($IPS list mwan3_connected | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}'); do
  120. echo " $address"
  121. done
  122. echo -e
  123. fi
  124. if [ -n "$($IPT -S mwan3_rules 2> /dev/null)" ]; then
  125. echo "Active rules:"
  126. $IPT -L mwan3_rules -n -v 2> /dev/null | tail -n+3 | sed 's/mark.*//' | sed 's/mwan3_policy_/- /' | sed 's/mwan3_rule_/S /'
  127. echo -e
  128. fi
  129. }
  130. status()
  131. {
  132. interfaces
  133. policies
  134. rules
  135. }
  136. start()
  137. {
  138. config_load mwan3
  139. config_foreach ifup interface
  140. }
  141. stop()
  142. {
  143. local ipset route rule table
  144. killall mwan3track &> /dev/null
  145. rm /var/run/mwan3track-* &> /dev/null
  146. for route in $($IP route list table all | sed 's/.*table \([^ ]*\) .*/\1/' | awk '{print $1}' | awk '{for(i=1;i<=NF;i++) if($i+0>0) if($i+0<255) {print;break}}'); do
  147. $IP route flush table $route &> /dev/null
  148. done
  149. for rule in $($IP rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do
  150. $IP rule del pref $rule &> /dev/null
  151. done
  152. $IPT -D PREROUTING -j mwan3_hook &> /dev/null
  153. $IPT -D OUTPUT -j mwan3_hook &> /dev/null
  154. for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
  155. $IPT -F $table &> /dev/null
  156. done
  157. for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
  158. $IPT -X $table &> /dev/null
  159. done
  160. for ipset in $(ipset -n list | grep mwan3); do
  161. $IPS destroy $ipset
  162. done
  163. }
  164. restart() {
  165. stop
  166. start
  167. }
  168. case "$1" in
  169. ifup|ifdown|interfaces|policies|rules|status|start|stop|restart)
  170. $*
  171. ;;
  172. *)
  173. help
  174. ;;
  175. esac
  176. exit 0