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.

176 lines
4.5 KiB

  1. #!/bin/sh
  2. # Copyright 2016 Dan Luedtke <mail@danrl.com>
  3. # Licensed to the public under the Apache License 2.0.
  4. WG=/usr/bin/wg
  5. if [ ! -x $WG ]; then
  6. logger -t "wireguard" "error: missing wireguard-tools (${WG})"
  7. exit 0
  8. fi
  9. [ -n "$INCLUDE_ONLY" ] || {
  10. . /lib/functions.sh
  11. . ../netifd-proto.sh
  12. init_proto "$@"
  13. }
  14. proto_wireguard_init_config() {
  15. proto_config_add_string "private_key"
  16. proto_config_add_int "listen_port"
  17. proto_config_add_int "mtu"
  18. proto_config_add_string "preshared_key"
  19. available=1
  20. no_proto_task=1
  21. }
  22. proto_wireguard_setup_peer() {
  23. local peer_config="$1"
  24. local public_key
  25. local allowed_ips
  26. local route_allowed_ips
  27. local endpoint_host
  28. local endpoint_port
  29. local persistent_keepalive
  30. config_get public_key "${peer_config}" "public_key"
  31. config_get allowed_ips "${peer_config}" "allowed_ips"
  32. config_get_bool route_allowed_ips "${peer_config}" "route_allowed_ips" 0
  33. config_get endpoint_host "${peer_config}" "endpoint_host"
  34. config_get endpoint_port "${peer_config}" "endpoint_port"
  35. config_get persistent_keepalive "${peer_config}" "persistent_keepalive"
  36. # peer configuration
  37. echo "[Peer]" >> "${wg_cfg}"
  38. echo "PublicKey=${public_key}" >> "${wg_cfg}"
  39. for allowed_ip in $allowed_ips; do
  40. echo "AllowedIPs=${allowed_ip}" >> "${wg_cfg}"
  41. done
  42. if [ "${endpoint_host}" ]; then
  43. case "${endpoint_host}" in
  44. *:*)
  45. endpoint="[${endpoint_host}]"
  46. ;;
  47. *)
  48. endpoint="${endpoint_host}"
  49. ;;
  50. esac
  51. if [ "${endpoint_port}" ]; then
  52. endpoint="${endpoint}:${endpoint_port}"
  53. else
  54. endpoint="${endpoint}:51820"
  55. fi
  56. echo "Endpoint=${endpoint}" >> "${wg_cfg}"
  57. fi
  58. if [ "${persistent_keepalive}" ]; then
  59. echo "PersistentKeepalive=${persistent_keepalive}" >> "${wg_cfg}"
  60. fi
  61. # add routes for allowed ips
  62. if [ ${route_allowed_ips} -ne 0 ]; then
  63. for allowed_ip in ${allowed_ips}; do
  64. case "${allowed_ip}" in
  65. *:*/*)
  66. proto_add_ipv6_route "${allowed_ip%%/*}" "${allowed_ip##*/}"
  67. ;;
  68. */*)
  69. proto_add_ipv4_route "${allowed_ip%%/*}" "${allowed_ip##*/}"
  70. ;;
  71. esac
  72. done
  73. fi
  74. #### FEATURE DISABLED
  75. # proto_add_host_dependency() has failed with IPv6 addresses during tests.
  76. # Endpoint dependency feature is disabled until the issue is fixed.
  77. ####
  78. # # endpoint dependency
  79. # if [ "${endpoint_host}" ]; then
  80. # endpoint_dependency=0
  81. # for ip in $(resolveip -t 10 "${endpoint_host}"); do
  82. # echo "adding host depedency for ${ip} at ${config}"
  83. # proto_add_host_dependency "${config}" "${ip}"
  84. # endpoint_dependency=1
  85. # done
  86. # if [ ${endpoint_dependency} -eq 0 ]; then
  87. # echo "error resolving ${endpoint_host}!"
  88. # sleep 5
  89. # proto_setup_failed "${config}"
  90. # exit 1
  91. # fi
  92. # fi
  93. ####
  94. }
  95. proto_wireguard_setup() {
  96. local config="$1"
  97. local wg_dir="/tmp/wireguard"
  98. local wg_cfg="${wg_dir}/${config}"
  99. local private_key
  100. local listen_port
  101. local mtu
  102. local preshared_key
  103. # load configuration
  104. config_load network
  105. config_get private_key "${config}" "private_key"
  106. config_get listen_port "${config}" "listen_port"
  107. config_get mtu "${config}" "mtu"
  108. config_get preshared_key "${config}" "preshared_key"
  109. # create interface
  110. ip link del dev "${config}" 2>/dev/null
  111. ip link add dev "${config}" type wireguard
  112. if [ "${mtu}" ]; then
  113. ip link set mtu "${mtu}" dev "${config}"
  114. fi
  115. proto_init_update "${config}" 1
  116. # generate configuration file
  117. umask 077
  118. mkdir -p "${wg_dir}"
  119. echo "[Interface]" > "${wg_cfg}"
  120. echo "PrivateKey=${private_key}" >> "${wg_cfg}"
  121. if [ "${listen_port}" ]; then
  122. echo "ListenPort=${listen_port}" >> "${wg_cfg}"
  123. fi
  124. if [ "${preshared_key}" ]; then
  125. echo "PresharedKey=${preshared_key}" >> "${wg_cfg}"
  126. fi
  127. config_foreach proto_wireguard_setup_peer "wireguard_${config}"
  128. # apply configuration file
  129. ${WG} setconf ${config} "${wg_cfg}"
  130. WG_RETURN=$?
  131. # delete configuration file
  132. rm -f "${wg_cfg}"
  133. # check status
  134. if [ ${WG_RETURN} -ne 0 ]; then
  135. sleep 5
  136. proto_setup_failed "${config}"
  137. exit 1
  138. fi
  139. proto_send_update "${config}"
  140. }
  141. proto_wireguard_teardown() {
  142. local config="$1"
  143. ip link del dev "${config}" >/dev/null 2>&1
  144. }
  145. [ -n "$INCLUDE_ONLY" ] || {
  146. add_protocol wireguard
  147. }