30-vpnclient-set-server-ipv6-route 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/bash
  2. is_serverip6route_set() {
  3. local server_ip6=${1}
  4. if [[ -z "${server_ip6}" ]]; then
  5. return 0
  6. fi
  7. if ! ip -6 route | grep -q "^${server_ip6}"; then
  8. return 1
  9. fi
  10. }
  11. set_serverip6route() {
  12. local server_ip6=${1}
  13. local ip6_gw=${2}
  14. local wired_device=${3}
  15. ip route add "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  16. }
  17. wired_device=$(ip route | awk '/default via/ { print $5; }')
  18. # See https://openvpn.net/community-resources/reference-manual-for-openvpn-2-4/#environmental-variables
  19. # to have a list of variables provided by OpenVPN, i.e:
  20. # - ifconfig_ipv6_remote
  21. # - net_gateway_ipv6
  22. echo "[INFO] Autodetected internet interface: ${wired_device}"
  23. if [[ -n "${ifconfig_ipv6_remote}" ]]; then
  24. echo "[INFO] Autodetected IPv6 address for the VPN server: ${ifconfig_ipv6_remote}"
  25. else
  26. echo "[INFO] No IPv6 address for the VPN server detected"
  27. echo "[INFO] No IPv6 route set"
  28. exit 0
  29. fi
  30. # Set the new server ipv6 route
  31. if [[ -n "${net_gateway_ipv6}" ]]; then
  32. if ! is_serverip6route_set "${ifconfig_ipv6_remote}"; then
  33. set_serverip6route "${ifconfig_ipv6_remote}" "${net_gateway_ipv6}" "${wired_device}"
  34. fi
  35. echo "[INFO] Native IPv6 detected"
  36. echo "[INFO] Autodetected native IPv6 gateway: ${net_gateway_ipv6}"
  37. if is_serverip6route_set "${ifconfig_ipv6_remote}"; then
  38. echo "[ OK ] IPv6 server route correctly set"
  39. else
  40. echo "[FAIL] No IPv6 server route set" >&2
  41. exit 1
  42. fi
  43. else
  44. echo "[INFO] No native IPv6 detected"
  45. echo "[INFO] No IPv6 server route to set"
  46. fi