6in4.sh 3.2 KB

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