ff_check_gateway 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/sh
  2. #
  3. # Check if this system (still) should be an active B.A.T.M.A.N. gateway
  4. # and update gw_mode accordingly if necessary (Salt managed)
  5. #
  6. # Rewritten by: Maximilian Wilhelm <max@sdn.clinic>
  7. # -- Sat, 07 Mar 2015 23:21:53 +0100
  8. #
  9. node_status_file="/etc/freifunk/status"
  10. # Default to "off"
  11. new_state="off"
  12. force_offline="false"
  13. #
  14. # If the status of this machine is not 'active', we should not
  15. # be an active gateway.
  16. if [ -f "${node_status_file}" ]; then
  17. status=$(cat "${node_status_file}")
  18. if [ "${status}" != "active" ]; then
  19. force_offline="true"
  20. fi
  21. fi
  22. #
  23. # If there is no DHCP server running here, we should not be an
  24. # active gateway.
  25. if ! systemctl status isc-dhcp-server >/dev/null 2>&1; then
  26. force_offline="true"
  27. fi
  28. #
  29. # If there is a default route via an IP from our address space
  30. # we assume that this gateway has an internet connection. As a
  31. # default route would be propagated in via iBGP and/or OSPF it
  32. # will only be present if at least one border router has an up
  33. # and running connection to AS201701.
  34. if ip route show | grep "^default via 10.132." -q; then
  35. new_state="server"
  36. fi
  37. #
  38. # Forcefully offline?
  39. if [ "${force_offline}" = "true" ]; then
  40. new_state="off"
  41. fi
  42. #
  43. # Compatiblity glue: Newer versions of batctl print a deprecation
  44. # warning when called with -m <batif>. Avoid spamming the log and
  45. # producting SPAM by silently handling this here.
  46. mesh_if_param="-m"
  47. if batctl -h 2>&1 | grep -q "meshif"; then
  48. mesh_if_param="meshif"
  49. fi
  50. #
  51. # Let's check the interfaces
  52. for iface in $(ip -br l | awk '/^bat-/ { print $1 }'); do
  53. # Ignore any external BATMAN instance, if present
  54. if echo "${iface}" | grep -q '-ext$'; then
  55. continue
  56. fi
  57. old_state="$(batctl "${mesh_if_param}" "${iface}" gw_mode | awk '{ print $1 }')"
  58. if [ "$old_state" = "$new_state" ]; then
  59. # Nothing to do here, carry on
  60. continue
  61. fi
  62. # Set new values
  63. batctl "${mesh_if_param}" "${iface}" gw_mode "${new_state}"
  64. logger "B.A.T.M.A.N. gateway mode changed from ${old_state} to ${new_state} on ${iface}"
  65. done