wireguard.sh 4.3 KB

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