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.

198 lines
3.7 KiB

mwan3: fix interface-bound traffic when interface is offline This commit fixed what 6d99b602 was supposed to fix without affecting interface-bound traffic. Before 6d99b602 interface-bound traffic was working normally as long as at least one interface was online. However when the last interface went offline, it was impossible to ping and such state was unrecoverable. Commit 6d99b602 fixed unrecoverable offline state problem (it was possible to ping -I iface) but messed inteface-bound traffic. Traffic with interface source address was not working if the interface was in "offline" state, even if another interface was online. The problem was caused by an inconsistent "offline" interface state: iptables-related rules were kept while routing table and policy were deleted. The idea behind this commit is to: 1. Keep all the rules for each interface (iptables, routing table, policy) regardless of its state. This ensures consistency, 2. Make interface state hotplug events affect only iptables' mwan3_policy_* rules. Interface-related iptables, routing table and policy is removed only when mwan3 is manually stopped. To make such changes possible, it's necessary to change the way mwan3_policy_* rule generator keeps track of interface state hotplug events. Until now, it checked for the existence of custom interface-related routing table (table id 1, 2, 3, ...). Clearly we can no longer rely on that so each interface state is stored explicitly in file. Signed-off-by: Marcin Jurkowski <marcin1j@gmail.com>
7 years ago
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/network.sh
  4. . /lib/mwan3/mwan3.sh
  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 currently active policy
  17. connected Show directly connected networks
  18. rules Show active rules
  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. ACTION=ifdown INTERFACE=$1 /sbin/hotplug-call iface
  31. kill $(pgrep -f "mwan3track $1 $2") &> /dev/null
  32. mwan3_track_clean $1
  33. }
  34. ifup()
  35. {
  36. local device enabled
  37. config_load mwan3
  38. if [ -z "$1" ]; then
  39. echo "Expecting interface. Usage: mwan3 ifup <interface>" && exit 0
  40. fi
  41. if [ -n "$2" ]; then
  42. echo "Too many arguments. Usage: mwan3 ifup <interface>" && exit 0
  43. fi
  44. config_get_bool enabled globals 'enabled' 0
  45. [ ${enabled} -gt 0 ] || {
  46. echo "Warning: mwan3 is global disabled. Usage: /etc/init.d/mwan3 start"
  47. exit 0
  48. }
  49. config_get enabled "$1" enabled 0
  50. device=$(uci -p /var/state get network.$1.ifname) &> /dev/null
  51. if [ -n "$device" ] ; then
  52. [ "$enabled" -eq 1 ] && ACTION=ifup INTERFACE=$1 DEVICE=$device /sbin/hotplug-call iface
  53. fi
  54. }
  55. interfaces()
  56. {
  57. config_load mwan3
  58. echo "Interface status:"
  59. config_foreach mwan3_report_iface_status interface
  60. echo -e
  61. }
  62. policies()
  63. {
  64. echo "Current ipv4 policies:"
  65. mwan3_report_policies_v4
  66. echo -e
  67. echo "Current ipv6 policies:"
  68. mwan3_report_policies_v6
  69. echo -e
  70. }
  71. connected()
  72. {
  73. echo "Directly connected ipv4 networks:"
  74. mwan3_report_connected_v4
  75. echo -e
  76. echo "Directly connected ipv6 networks:"
  77. mwan3_report_connected_v6
  78. echo -e
  79. }
  80. rules()
  81. {
  82. echo "Active ipv4 user rules:"
  83. mwan3_report_rules_v4
  84. echo -e
  85. echo "Active ipv6 user rules:"
  86. mwan3_report_rules_v6
  87. echo -e
  88. }
  89. status()
  90. {
  91. interfaces
  92. policies
  93. connected
  94. rules
  95. }
  96. start()
  97. {
  98. local enabled
  99. config_load mwan3
  100. config_get_bool enabled globals 'enabled' 0
  101. [ ${enabled} -gt 0 ] || {
  102. echo "Warning: mwan3 is global disabled. Usage: /etc/init.d/mwan3 start"
  103. exit 0
  104. }
  105. config_foreach ifup interface
  106. }
  107. stop()
  108. {
  109. local ipset route rule table IP IPT pid
  110. for pid in $(pgrep -f "mwan3track"); do
  111. kill -TERM "$pid" > /dev/null 2>&1
  112. sleep 1
  113. kill -KILL "$pid" > /dev/null 2>&1
  114. done
  115. config_load mwan3
  116. config_foreach mwan3_track_clean interface
  117. for IP in "$IP4" "$IP6"; do
  118. for route in $(seq 1 $MWAN3_INTERFACE_MAX); do
  119. $IP route flush table $route &> /dev/null
  120. done
  121. for rule in $($IP rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do
  122. $IP rule del pref $rule &> /dev/null
  123. done
  124. done
  125. for IPT in "$IPT4" "$IPT6"; do
  126. $IPT -D PREROUTING -j mwan3_hook &> /dev/null
  127. $IPT -D OUTPUT -j mwan3_hook &> /dev/null
  128. for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
  129. $IPT -F $table &> /dev/null
  130. done
  131. for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
  132. $IPT -X $table &> /dev/null
  133. done
  134. done
  135. for ipset in $($IPS -n list | grep mwan3_); do
  136. $IPS -q destroy $ipset
  137. done
  138. for ipset in $($IPS -n list | grep mwan3 | grep -E '_v4|_v6'); do
  139. $IPS -q destroy $ipset
  140. done
  141. mwan3_lock_clean
  142. rm -rf $MWAN3_STATUS_DIR $MWAN3TRACK_STATUS_DIR
  143. }
  144. restart() {
  145. stop
  146. start
  147. }
  148. case "$1" in
  149. ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart)
  150. mwan3_init
  151. $*
  152. ;;
  153. *)
  154. help
  155. ;;
  156. esac
  157. exit 0