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.

188 lines
4.7 KiB

  1. #!/bin/sh
  2. # Copyright 2016-2017 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. proto_config_add_string "fwmark"
  20. available=1
  21. no_proto_task=1
  22. }
  23. proto_wireguard_setup_peer() {
  24. local peer_config="$1"
  25. local public_key
  26. local allowed_ips
  27. local route_allowed_ips
  28. local endpoint_host
  29. local endpoint_port
  30. local persistent_keepalive
  31. config_get public_key "${peer_config}" "public_key"
  32. config_get allowed_ips "${peer_config}" "allowed_ips"
  33. config_get_bool route_allowed_ips "${peer_config}" "route_allowed_ips" 0
  34. config_get endpoint_host "${peer_config}" "endpoint_host"
  35. config_get endpoint_port "${peer_config}" "endpoint_port"
  36. config_get persistent_keepalive "${peer_config}" "persistent_keepalive"
  37. # peer configuration
  38. echo "[Peer]" >> "${wg_cfg}"
  39. echo "PublicKey=${public_key}" >> "${wg_cfg}"
  40. for allowed_ip in $allowed_ips; do
  41. echo "AllowedIPs=${allowed_ip}" >> "${wg_cfg}"
  42. done
  43. if [ "${endpoint_host}" ]; then
  44. case "${endpoint_host}" in
  45. *:*)
  46. endpoint="[${endpoint_host}]"
  47. ;;
  48. *)
  49. endpoint="${endpoint_host}"
  50. ;;
  51. esac
  52. if [ "${endpoint_port}" ]; then
  53. endpoint="${endpoint}:${endpoint_port}"
  54. else
  55. endpoint="${endpoint}:51820"
  56. fi
  57. echo "Endpoint=${endpoint}" >> "${wg_cfg}"
  58. fi
  59. if [ "${persistent_keepalive}" ]; then
  60. echo "PersistentKeepalive=${persistent_keepalive}" >> "${wg_cfg}"
  61. fi
  62. # add routes for allowed ips
  63. if [ ${route_allowed_ips} -ne 0 ]; then
  64. for allowed_ip in ${allowed_ips}; do
  65. case "${allowed_ip}" in
  66. *:*/*)
  67. proto_add_ipv6_route "${allowed_ip%%/*}" "${allowed_ip##*/}"
  68. ;;
  69. */*)
  70. proto_add_ipv4_route "${allowed_ip%%/*}" "${allowed_ip##*/}"
  71. ;;
  72. esac
  73. done
  74. fi
  75. }
  76. proto_wireguard_setup() {
  77. local config="$1"
  78. local wg_dir="/tmp/wireguard"
  79. local wg_cfg="${wg_dir}/${config}"
  80. local private_key
  81. local listen_port
  82. local mtu
  83. local preshared_key
  84. # load configuration
  85. config_load network
  86. config_get private_key "${config}" "private_key"
  87. config_get listen_port "${config}" "listen_port"
  88. config_get addresses "${config}" "addresses"
  89. config_get mtu "${config}" "mtu"
  90. config_get preshared_key "${config}" "preshared_key"
  91. config_get fwmark "${config}" "fwmark"
  92. # create interface
  93. ip link del dev "${config}" 2>/dev/null
  94. ip link add dev "${config}" type wireguard
  95. if [ "${mtu}" ]; then
  96. ip link set mtu "${mtu}" dev "${config}"
  97. fi
  98. proto_init_update "${config}" 1
  99. # generate configuration file
  100. umask 077
  101. mkdir -p "${wg_dir}"
  102. echo "[Interface]" > "${wg_cfg}"
  103. echo "PrivateKey=${private_key}" >> "${wg_cfg}"
  104. if [ "${listen_port}" ]; then
  105. echo "ListenPort=${listen_port}" >> "${wg_cfg}"
  106. fi
  107. if [ "${preshared_key}" ]; then
  108. echo "PresharedKey=${preshared_key}" >> "${wg_cfg}"
  109. fi
  110. if [ "${fwmark}" ]; then
  111. echo "FwMark=${fwmark}" >> "${wg_cfg}"
  112. fi
  113. config_foreach proto_wireguard_setup_peer "wireguard_${config}"
  114. # apply configuration file
  115. ${WG} setconf ${config} "${wg_cfg}"
  116. WG_RETURN=$?
  117. # delete configuration file
  118. rm -f "${wg_cfg}"
  119. # check status
  120. if [ ${WG_RETURN} -ne 0 ]; then
  121. sleep 5
  122. proto_setup_failed "${config}"
  123. exit 1
  124. fi
  125. # add ip addresses
  126. for address in ${addresses}; do
  127. case "${address}" in
  128. *:*/*)
  129. proto_add_ipv6_address "${address%%/*}" "${address##*/}"
  130. ;;
  131. *.*/*)
  132. proto_add_ipv4_address "${address%%/*}" "${address##*/}"
  133. ;;
  134. *:*)
  135. proto_add_ipv6_address "${address%%/*}" "128"
  136. ;;
  137. *.*)
  138. proto_add_ipv4_address "${address%%/*}" "32"
  139. ;;
  140. esac
  141. done
  142. # endpoint dependency
  143. wg show "${config}" endpoints | \
  144. sed -E 's/\[?([0-9.:a-f]+)\]?:([0-9]+)/\1 \2/' | \
  145. while IFS=$'\t ' read -r key address port; do
  146. [ -n "${port}" ] || continue
  147. echo "adding host depedency for ${address} at ${config}"
  148. proto_add_host_dependency "${config}" "${address}"
  149. done
  150. proto_send_update "${config}"
  151. }
  152. proto_wireguard_teardown() {
  153. local config="$1"
  154. ip link del dev "${config}" >/dev/null 2>&1
  155. }
  156. [ -n "$INCLUDE_ONLY" ] || {
  157. add_protocol wireguard
  158. }