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.

214 lines
5.9 KiB

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