1
0

6in4.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 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_add_prefix() {
  21. append "$3" "$1"
  22. }
  23. proto_6in4_setup() {
  24. local cfg="$1"
  25. local iface="$2"
  26. local link="6in4-$cfg"
  27. local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix ip6prefixes tunlink tunnelid username password updatekey
  28. json_get_vars mtu ttl tos ipaddr peeraddr ip6addr tunlink tunnelid username password updatekey
  29. json_for_each_item proto_6in4_add_prefix ip6prefix ip6prefixes
  30. [ -z "$peeraddr" ] && {
  31. proto_notify_error "$cfg" "MISSING_ADDRESS"
  32. proto_block_restart "$cfg"
  33. return
  34. }
  35. ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
  36. [ -z "$ipaddr" ] && {
  37. local wanif="$tunlink"
  38. if [ -z "$wanif" ] && ! network_find_wan wanif; then
  39. proto_notify_error "$cfg" "NO_WAN_LINK"
  40. return
  41. fi
  42. if ! network_get_ipaddr ipaddr "$wanif"; then
  43. proto_notify_error "$cfg" "NO_WAN_LINK"
  44. return
  45. fi
  46. }
  47. proto_init_update "$link" 1
  48. [ -n "$ip6addr" ] && {
  49. local local6="${ip6addr%%/*}"
  50. local mask6="${ip6addr##*/}"
  51. [[ "$local6" = "$mask6" ]] && mask6=
  52. proto_add_ipv6_address "$local6" "$mask6"
  53. proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
  54. }
  55. for ip6prefix in $ip6prefixes; do
  56. proto_add_ipv6_prefix "$ip6prefix"
  57. proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
  58. done
  59. proto_add_tunnel
  60. json_add_string mode sit
  61. json_add_int mtu "${mtu:-1280}"
  62. json_add_int ttl "${ttl:-64}"
  63. [ -n "$tos" ] && json_add_string tos "$tos"
  64. json_add_string local "$ipaddr"
  65. json_add_string remote "$peeraddr"
  66. [ -n "$tunlink" ] && json_add_string link "$tunlink"
  67. proto_close_tunnel
  68. proto_send_update "$cfg"
  69. [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
  70. [ -n "$updatekey" ] && password="$updatekey"
  71. local http="http"
  72. local urlget="uclient-fetch"
  73. local urlget_opts="-qO-"
  74. local ca_path="${SSL_CERT_DIR:-/etc/ssl/certs}"
  75. [ -f /lib/libustream-ssl.so ] && http=https
  76. [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
  77. urlget_opts="$urlget_opts --no-check-certificate"
  78. }
  79. local url="$http://ipv4.tunnelbroker.net/nic/update?hostname=$tunnelid"
  80. local try=0
  81. local max=3
  82. (
  83. set -o pipefail
  84. while [ $((++try)) -le $max ]; do
  85. if proto_6in4_update $urlget $urlget_opts --user="$username" --password="$password" "$url" 2>&1 | \
  86. sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
  87. logger -t "$link";
  88. then
  89. logger -t "$link" "updated"
  90. return 0
  91. fi
  92. sleep 5
  93. done
  94. logger -t "$link" "update failed"
  95. )
  96. }
  97. }
  98. proto_6in4_teardown() {
  99. local cfg="$1"
  100. }
  101. proto_6in4_init_config() {
  102. no_device=1
  103. available=1
  104. proto_config_add_string "ipaddr"
  105. proto_config_add_string "ip6addr"
  106. proto_config_add_array "ip6prefix"
  107. proto_config_add_string "peeraddr"
  108. proto_config_add_string "tunlink"
  109. proto_config_add_string "tunnelid"
  110. proto_config_add_string "username"
  111. proto_config_add_string "password"
  112. proto_config_add_string "updatekey"
  113. proto_config_add_int "mtu"
  114. proto_config_add_int "ttl"
  115. proto_config_add_string "tos"
  116. }
  117. [ -n "$INCLUDE_ONLY" ] || {
  118. add_protocol 6in4
  119. }