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.

182 lines
3.5 KiB

  1. #!/bin/sh
  2. [ -x /usr/bin/ip ] || exit 4
  3. [ -x /usr/sbin/ipset ] || exit 5
  4. [ -x /usr/sbin/iptables ] || exit 6
  5. [ -x /usr/sbin/ip6tables ] || exit 7
  6. [ -x /usr/bin/logger ] || exit 8
  7. . /lib/functions.sh
  8. . /lib/functions/network.sh
  9. . /lib/mwan3/mwan3.sh
  10. help()
  11. {
  12. cat <<EOF
  13. Syntax: mwan3 [command]
  14. Available commands:
  15. start Load iptables rules, ip rules and ip routes
  16. stop Unload iptables rules, ip rules and ip routes
  17. restart Reload iptables rules, ip rules and ip routes
  18. ifup <iface> Load rules and routes for specific interface
  19. ifdown <iface> Unload rules and routes for specific interface
  20. interfaces Show interfaces status
  21. policies Show currently active policy
  22. connected Show directly connected networks
  23. rules Show active rules
  24. status Show all status
  25. EOF
  26. }
  27. ifdown()
  28. {
  29. if [ -z "$1" ]; then
  30. echo "Error: Expecting interface. Usage: mwan3 ifdown <interface>" && exit 0
  31. fi
  32. if [ -n "$2" ]; then
  33. echo "Error: Too many arguments. Usage: mwan3 ifdown <interface>" && exit 0
  34. fi
  35. ACTION=ifdown INTERFACE=$1 /sbin/hotplug-call iface
  36. if [ -e /var/run/mwan3track-$1.pid ] ; then
  37. kill $(cat /var/run/mwan3track-$1.pid)
  38. rm /var/run/mwan3track-$1.pid
  39. fi
  40. }
  41. ifup()
  42. {
  43. local device enabled
  44. config_load mwan3
  45. if [ -z "$1" ]; then
  46. echo "Expecting interface. Usage: mwan3 ifup <interface>" && exit 0
  47. fi
  48. if [ -n "$2" ]; then
  49. echo "Too many arguments. Usage: mwan3 ifup <interface>" && exit 0
  50. fi
  51. config_get enabled "$1" enabled 0
  52. device=$(uci -p /var/state get network.$1.ifname) &> /dev/null
  53. if [ -n "$device" ] ; then
  54. [ "$enabled" -eq 1 ] && ACTION=ifup INTERFACE=$1 DEVICE=$device /sbin/hotplug-call iface
  55. fi
  56. }
  57. interfaces()
  58. {
  59. config_load mwan3
  60. echo "Interface status:"
  61. config_foreach mwan3_report_iface_status interface
  62. echo -e
  63. }
  64. policies()
  65. {
  66. echo "Current ipv4 policies:"
  67. mwan3_report_policies_v4
  68. echo -e
  69. echo "Current ipv6 policies:"
  70. mwan3_report_policies_v6
  71. echo -e
  72. }
  73. connected()
  74. {
  75. echo "Directly connected ipv4 networks:"
  76. mwan3_report_connected_v4
  77. echo -e
  78. echo "Directly connected ipv6 networks:"
  79. mwan3_report_connected_v6
  80. echo -e
  81. }
  82. rules()
  83. {
  84. echo "Active ipv4 user rules:"
  85. mwan3_report_rules_v4
  86. echo -e
  87. echo "Active ipv6 user rules:"
  88. mwan3_report_rules_v6
  89. echo -e
  90. }
  91. status()
  92. {
  93. interfaces
  94. policies
  95. connected
  96. rules
  97. }
  98. start()
  99. {
  100. config_load mwan3
  101. config_foreach ifup interface
  102. }
  103. stop()
  104. {
  105. local ipset route rule table IP IPT
  106. killall mwan3track &> /dev/null
  107. rm /var/run/mwan3track-* &> /dev/null
  108. for IP in "$IP4" "$IP6"; do
  109. 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
  110. $IP route flush table $route &> /dev/null
  111. done
  112. for rule in $($IP rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do
  113. $IP rule del pref $rule &> /dev/null
  114. done
  115. done
  116. for IPT in "$IPT4" "$IPT6"; do
  117. $IPT -D PREROUTING -j mwan3_hook &> /dev/null
  118. $IPT -D OUTPUT -j mwan3_hook &> /dev/null
  119. for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
  120. $IPT -F $table &> /dev/null
  121. done
  122. for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
  123. $IPT -X $table &> /dev/null
  124. done
  125. done
  126. for ipset in $($IPS -n list | grep mwan3_); do
  127. $IPS -q destroy $ipset
  128. done
  129. for ipset in $($IPS -n list | grep mwan3 | grep -E '_v4|_v6'); do
  130. $IPS -q destroy $ipset
  131. done
  132. }
  133. restart() {
  134. stop
  135. start
  136. }
  137. case "$1" in
  138. ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart)
  139. $*
  140. ;;
  141. *)
  142. help
  143. ;;
  144. esac
  145. exit 0