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.

88 lines
2.6 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. . /lib/mwan3/common.sh
  6. initscript=/etc/init.d/mwan3
  7. . /lib/functions/procd.sh
  8. SCRIPTNAME="mwan3-hotplug"
  9. [ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "connected" ] || [ "$ACTION" = "disconnected" ] || exit 1
  10. [ -n "$INTERFACE" ] || exit 2
  11. [ "$FIRSTCONNECT" = "1" ] || [ "$MWAN3_SHUTDOWN" = "1" ] && exit 0
  12. if { [ "$ACTION" = "ifup" ] || [ "$ACTION" = "connected" ] ; } && [ -z "$DEVICE" ]; then
  13. LOG notice "$ACTION called on $INTERFACE with no device set"
  14. exit 3
  15. fi
  16. [ "$MWAN3_STARTUP" = "init" ] || procd_lock
  17. mwan3_init
  18. /etc/init.d/mwan3 running || {
  19. [ "$MWAN3_STARTUP" = "init" ] || procd_lock
  20. LOG notice "mwan3 hotplug $ACTION on $INTERFACE not called because globally disabled"
  21. mwan3_flush_conntrack "$INTERFACE" "$ACTION"
  22. exit 0
  23. }
  24. $IPT4 -S mwan3_hook &>/dev/null || {
  25. LOG warn "hotplug called on $INTERFACE before mwan3 has been set up"
  26. exit 0
  27. }
  28. if [ "$MWAN3_STARTUP" != "init" ] && [ "$ACTION" = "ifup" ]; then
  29. mwan3_set_user_iface_rules $INTERFACE $DEVICE
  30. fi
  31. config_get_bool enabled $INTERFACE 'enabled' '0'
  32. [ "${enabled}" -eq 1 ] || {
  33. LOG notice "mwan3 hotplug on $INTERFACE not called because interface disabled"
  34. exit 0
  35. }
  36. config_get initial_state $INTERFACE initial_state "online"
  37. if [ "$initial_state" = "offline" ]; then
  38. status=$(cat $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS 2>/dev/null || echo unknown)
  39. [ "$status" = "online" ] || status=offline
  40. else
  41. status=online
  42. fi
  43. LOG notice "Execute $ACTION event on interface $INTERFACE (${DEVICE:-unknown})"
  44. case "$ACTION" in
  45. connected)
  46. mwan3_set_iface_hotplug_state $INTERFACE "online"
  47. mwan3_set_policies_iptables
  48. ;;
  49. ifup)
  50. mwan3_create_iface_iptables $INTERFACE $DEVICE
  51. mwan3_create_iface_rules $INTERFACE $DEVICE
  52. mwan3_set_iface_hotplug_state $INTERFACE "$status"
  53. if [ "$MWAN3_STARTUP" != "init" ]; then
  54. mwan3_create_iface_route $INTERFACE $DEVICE
  55. mwan3_set_general_rules
  56. [ "$status" = "online" ] && mwan3_set_policies_iptables
  57. fi
  58. [ "$ACTION" = ifup ] && procd_running mwan3 "track_$INTERFACE" && procd_send_signal mwan3 "track_$INTERFACE" USR2
  59. ;;
  60. disconnected)
  61. mwan3_set_iface_hotplug_state $INTERFACE "offline"
  62. mwan3_set_policies_iptables
  63. ;;
  64. ifdown)
  65. mwan3_set_iface_hotplug_state $INTERFACE "offline"
  66. mwan3_delete_iface_ipset_entries $INTERFACE
  67. mwan3_delete_iface_rules $INTERFACE
  68. mwan3_delete_iface_route $INTERFACE
  69. mwan3_delete_iface_iptables $INTERFACE
  70. procd_running mwan3 "track_$INTERFACE" && procd_send_signal mwan3 "track_$INTERFACE" USR1
  71. mwan3_set_policies_iptables
  72. ;;
  73. esac
  74. exit 0