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.

104 lines
3.2 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
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. . /usr/share/libubox/jshn.sh
  6. . /lib/mwan3/common.sh
  7. SCRIPTNAME="mwan3-hotplug"
  8. [ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "connected" ] || [ "$ACTION" = "disconnected" ] || exit 1
  9. [ -n "$INTERFACE" ] || exit 2
  10. if ( [ "$ACTION" = "ifup" ] || [ "$ACTION" = "connected" ] ) && [ -z "$DEVICE" ]; then
  11. LOG notice "$ACTION called on $INTERFACE with no device set"
  12. exit 3
  13. fi
  14. [ "$MWAN3_STARTUP" = 1 ] || mwan3_lock "$ACTION" "$INTERFACE"
  15. config_load mwan3
  16. config_get_bool enabled globals 'enabled' '0'
  17. [ "${enabled}" -gt 0 ] || {
  18. [ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE"
  19. LOG notice "mwan3 hotplug on $INTERFACE not called because globally disabled"
  20. mwan3_flush_conntrack "$INTERFACE" "$ACTION"
  21. exit 0
  22. }
  23. $IPT4 -S mwan3_hook &>/dev/null || {
  24. mwan3_unlock "$ACTION" "$INTERFACE"
  25. LOG warn "hotplug called on $INTERFACE before mwan3 has been set up"
  26. exit 0
  27. }
  28. mwan3_init
  29. [ "$MWAN3_STARTUP" = 1 ] || {
  30. mwan3_set_connected_iptables
  31. mwan3_set_custom_ipset
  32. }
  33. if [ "$MWAN3_STARTUP" != 1 ]; then
  34. mwan3_set_user_iface_rules $INTERFACE $DEVICE
  35. fi
  36. config_get initial_state $INTERFACE initial_state "online"
  37. config_get_bool enabled $INTERFACE 'enabled' '0'
  38. [ "${enabled}" -eq 1 ] || {
  39. [ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE"
  40. LOG notice "mwan3 hotplug on $INTERFACE not called because interface disabled"
  41. exit 0
  42. }
  43. trackpid=$(pgrep -f "mwan3track $INTERFACE ")
  44. if [ "$initial_state" = "offline" ]; then
  45. status=$(cat $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS 2>/dev/null || echo unknown)
  46. else
  47. status=online
  48. fi
  49. [ -z "$TRUE_INTERFACE" ] && mwan3_get_true_iface TRUE_INTERFACE $INTERFACE
  50. binary_status=$status
  51. [ "$binary_status" = "online" ] || binary_status=offline
  52. LOG notice "Execute "$ACTION" event on interface $INTERFACE (${DEVICE:-unknown})"
  53. case "$ACTION" in
  54. ifup|connected)
  55. mwan3_create_iface_iptables $INTERFACE $DEVICE
  56. mwan3_create_iface_rules $INTERFACE $DEVICE
  57. mwan3_create_iface_route $INTERFACE $DEVICE
  58. [ "$MWAN3_STARTUP" != 1 ] && mwan3_add_non_default_iface_route $INTERFACE $DEVICE
  59. mwan3_set_iface_hotplug_state $INTERFACE "$binary_status"
  60. mwan3_get_src_ip src_ip "$TRUE_INTERFACE"
  61. if [ -n "${trackpid}" ]; then
  62. device_pid=$(pgrep -f "mwan3track $INTERFACE $DEVICE ")
  63. if [ "$device_pid" = "$trackpid" ]; then
  64. [ "$ACTION" = ifup ] && kill -USR2 "$trackpid"
  65. else
  66. mwan3_track $INTERFACE $DEVICE "$binary_status" "$src_ip"
  67. LOG notice "Restarted tracker [$!] on interface $INTERFACE (${DEVICE:-unknown})"
  68. fi
  69. else
  70. mwan3_track $INTERFACE $DEVICE "$binary_status" "$src_ip"
  71. LOG notice "Started tracker [$!] on interface $INTERFACE (${DEVICE:-unknown})"
  72. fi
  73. [ "$MWAN3_STARTUP" != 1 ] && [ "$binary_status" == "online" ] && mwan3_set_policies_iptables
  74. ;;
  75. ifdown|disconnected)
  76. mwan3_set_iface_hotplug_state $INTERFACE "offline"
  77. mwan3_delete_iface_ipset_entries $INTERFACE
  78. mwan3_delete_iface_rules $INTERFACE
  79. mwan3_delete_iface_route $INTERFACE
  80. mwan3_delete_iface_iptables $INTERFACE
  81. if [ "$ACTION" = "ifdown" ]; then
  82. [ -n "$trackpid" ] && kill -USR1 "$trackpid"
  83. fi
  84. mwan3_set_policies_iptables
  85. ;;
  86. esac
  87. [ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE"
  88. exit 0