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.

110 lines
2.8 KiB

  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/network.sh
  4. . /lib/mwan3/mwan3.sh
  5. . /lib/mwan3/common.sh
  6. mwan3_rtmon_route_handle()
  7. {
  8. config_load mwan3
  9. local section action route_line family tbl device metric tos dst line
  10. local route_device tid
  11. route_line=${1##"Deleted "}
  12. route_family=$2
  13. if [ "$route_family" = "ipv4" ]; then
  14. IP="$IP4"
  15. elif [ "$route_family" = "ipv6" ] && [ $NO_IPV6 -eq 0 ]; then
  16. IP="$IP6"
  17. else
  18. return
  19. fi
  20. if [ "$route_line" == "$1" ]; then
  21. action="add"
  22. else
  23. action="del"
  24. fi
  25. # never add default route lines, since this is handled elsewhere
  26. [ -z "${route_line##default*}" ] && return
  27. [ -z "${route_line##::/0*}" ] && return
  28. route_line=${route_line%% linkdown*}
  29. route_line=${route_line%% unreachable*}
  30. mwan3_update_dev_to_table
  31. mwan3_route_line_dev "tid" "$route_line" "$route_family"
  32. handle_route() {
  33. tbl=$($IP route list table $tid)
  34. if [ $action = "add" ]; then
  35. echo "$tbl" | grep -q "^default\|^::/0" || return
  36. else
  37. [ -z "$tbl" ] && return
  38. fi
  39. # check that action needs to be performed. May not need to take action if:
  40. # Got a route update on ipv6 where route is already in the table
  41. # Got a delete event, but table was already flushed
  42. [ $action = "add" ] && [ -z "${tbl##*$route_line*}" ] && return
  43. [ $action = "del" ] && [ -n "${tbl##*$route_line*}" ] && return
  44. network_get_device device "$section"
  45. LOG debug "adjusting route $device: $IP route "$action" table $tid $route_line"
  46. $IP route "$action" table $tid $route_line ||
  47. LOG warn "failed: $IP route $action table $tid $route_line"
  48. }
  49. handle_route_cb(){
  50. let tid++
  51. config_get family "$section" family ipv4
  52. [ "$family" != "$route_family" ] && return
  53. handle_route
  54. }
  55. if [ $action = "add" ]; then
  56. ## handle old routes from 'change' or 'replace'
  57. metric=${route_line##*metric }
  58. [ "$metric" = "$route_line" ] && unset metric || metric=${metric%% *}
  59. tos=${route_line##*tos }
  60. [ "$tos" = "$route_line" ] && unset tos || tos=${tos%% *}
  61. dst=${route_line%% *}
  62. grep_line="$dst ${tos:+tos $tos}.*table [0-9].*${metric:+metric $metric}"
  63. $IP route list table all | grep "$grep_line" | while read line; do
  64. tbl=${line##*table }
  65. tbl=${tbl%% *}
  66. [ $tbl -gt $MWAN3_INTERFACE_MAX ] && continue
  67. LOG debug "removing route on ip route change/replace: $line"
  68. $IP route del $line
  69. done
  70. fi
  71. if [ -n "$tid" ]; then
  72. handle_route
  73. else
  74. config_foreach handle_route_cb interface
  75. fi
  76. }
  77. main()
  78. {
  79. local IP family
  80. config_load mwan3
  81. family=$1
  82. [ -z $family ] && family=ipv4
  83. if [ "$family" = ipv6 ]; then
  84. IP="$IP6"
  85. else
  86. IP="$IP4"
  87. fi
  88. mwan3_init
  89. $IP monitor route | while read line; do
  90. [ -z "${line##*table*}" ] && continue
  91. LOG debug "handling route update $family $line"
  92. mwan3_lock "service" "mwan3rtmon"
  93. mwan3_rtmon_route_handle "$line" "$family"
  94. mwan3_unlock "service" "mwan3rtmon"
  95. done
  96. }
  97. main "$@"