6in4.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/sh
  2. # 6in4.sh - IPv6-in-IPv4 tunnel backend
  3. # Copyright (c) 2010-2015 OpenWrt.org
  4. [ -n "$INCLUDE_ONLY" ] || {
  5. . /lib/functions.sh
  6. . /lib/functions/network.sh
  7. . ../netifd-proto.sh
  8. init_proto "$@"
  9. }
  10. proto_6in4_update() {
  11. sh -c '
  12. local timeout=5
  13. (while [ $((timeout--)) -gt 0 ]; do
  14. sleep 1
  15. kill -0 $$ || exit 0
  16. done; kill -9 $$) 2>/dev/null &
  17. exec "$@"
  18. ' "$1" "$@"
  19. }
  20. proto_6in4_setup() {
  21. local cfg="$1"
  22. local iface="$2"
  23. local link="6in4-$cfg"
  24. local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey
  25. json_get_vars mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey
  26. [ -z "$peeraddr" ] && {
  27. proto_notify_error "$cfg" "MISSING_ADDRESS"
  28. proto_block_restart "$cfg"
  29. return
  30. }
  31. ( proto_add_host_dependency "$cfg" "$peeraddr" )
  32. [ -z "$ipaddr" ] && {
  33. local wanif
  34. if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
  35. proto_notify_error "$cfg" "NO_WAN_LINK"
  36. return
  37. fi
  38. }
  39. proto_init_update "$link" 1
  40. [ -n "$ip6addr" ] && {
  41. local local6="${ip6addr%%/*}"
  42. local mask6="${ip6addr##*/}"
  43. [[ "$local6" = "$mask6" ]] && mask6=
  44. proto_add_ipv6_address "$local6" "$mask6"
  45. proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
  46. }
  47. [ -n "$ip6prefix" ] && {
  48. proto_add_ipv6_prefix "$ip6prefix"
  49. proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
  50. }
  51. proto_add_tunnel
  52. json_add_string mode sit
  53. json_add_int mtu "${mtu:-1280}"
  54. json_add_int ttl "${ttl:-64}"
  55. [ -n "$tos" ] && json_add_string tos "$tos"
  56. json_add_string local "$ipaddr"
  57. json_add_string remote "$peeraddr"
  58. proto_close_tunnel
  59. proto_send_update "$cfg"
  60. [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
  61. [ -n "$updatekey" ] && password="$updatekey"
  62. local http="http"
  63. local urlget="wget"
  64. local urlget_opts="-qO-"
  65. local ca_path="${SSL_CERT_DIR-/etc/ssl/certs}"
  66. if [ -n "$(which curl)" ]; then
  67. urlget="curl"
  68. urlget_opts="-s -S"
  69. if curl -V | grep "Protocols:" | grep -qF "https"; then
  70. http="https"
  71. urlget_opts="$urlget_opts --capath $ca_path"
  72. fi
  73. fi
  74. if [ "$http" = "http" ] &&
  75. wget --version 2>&1 | grep -qF "+https"; then
  76. urlget="wget"
  77. urlget_opts="-qO- --ca-directory=$ca_path"
  78. http="https"
  79. fi
  80. [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
  81. if [ "$urlget" = "curl" ]; then
  82. urlget_opts="$urlget_opts -k"
  83. else
  84. urlget_opts="$urlget_opts --no-check-certificate"
  85. fi
  86. }
  87. local url="$http://ipv4.tunnelbroker.net/nic/update?username=$username&password=$password&hostname=$tunnelid"
  88. local try=0
  89. local max=3
  90. (
  91. set -o pipefail
  92. while [ $((++try)) -le $max ]; do
  93. if proto_6in4_update $urlget $urlget_opts "$url" 2>&1 | \
  94. sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
  95. logger -t "$link";
  96. then
  97. logger -t "$link" "updated"
  98. return 0
  99. fi
  100. sleep 5
  101. done
  102. logger -t "$link" "update failed"
  103. )
  104. }
  105. }
  106. proto_6in4_teardown() {
  107. local cfg="$1"
  108. }
  109. proto_6in4_init_config() {
  110. no_device=1
  111. available=1
  112. proto_config_add_string "ipaddr"
  113. proto_config_add_string "ip6addr"
  114. proto_config_add_string "ip6prefix"
  115. proto_config_add_string "peeraddr"
  116. proto_config_add_string "tunnelid"
  117. proto_config_add_string "username"
  118. proto_config_add_string "password"
  119. proto_config_add_string "updatekey"
  120. proto_config_add_int "mtu"
  121. proto_config_add_int "ttl"
  122. proto_config_add_string "tos"
  123. }
  124. [ -n "$INCLUDE_ONLY" ] || {
  125. add_protocol 6in4
  126. }