1
0

wireguard.sh 5.1 KB

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