wireguard.sh 5.0 KB

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