6in4.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. # Function taken from 6to4 package (6to4.sh), flipped returns
  11. test_6in4_rfc1918()
  12. {
  13. local oIFS="$IFS"; IFS="."; set -- $1; IFS="$oIFS"
  14. [ $1 -eq 10 ] && return 1
  15. [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 1
  16. [ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 1
  17. # RFC 6598
  18. [ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 1
  19. return 0
  20. }
  21. proto_6in4_update() {
  22. sh -c '
  23. timeout=5
  24. (while [ $((timeout--)) -gt 0 ]; do
  25. sleep 1
  26. kill -0 $$ || exit 0
  27. done; kill -9 $$) 2>/dev/null &
  28. exec "$@"
  29. ' "$1" "$@"
  30. }
  31. proto_6in4_add_prefix() {
  32. append "$3" "$1"
  33. }
  34. proto_6in4_setup() {
  35. local cfg="$1"
  36. local iface="$2"
  37. local link="6in4-$cfg"
  38. local remoteip
  39. local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix ip6prefixes tunlink tunnelid username password updatekey device
  40. json_get_vars mtu ttl tos ipaddr peeraddr ip6addr tunlink tunnelid username password updatekey device
  41. json_for_each_item proto_6in4_add_prefix ip6prefix ip6prefixes
  42. [ -n "$device" ] && link="$device"
  43. [ -z "$peeraddr" ] && {
  44. proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
  45. proto_block_restart "$cfg"
  46. return
  47. }
  48. remoteip=$(resolveip -t 10 -4 "$peeraddr")
  49. if [ -z "$remoteip" ]; then
  50. proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
  51. return
  52. fi
  53. for ip in $remoteip; do
  54. peeraddr=$ip
  55. break
  56. done
  57. ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
  58. [ -z "$ipaddr" ] && {
  59. local wanif="$tunlink"
  60. if [ -z "$wanif" ] && ! network_find_wan wanif; then
  61. proto_notify_error "$cfg" "NO_WAN_LINK"
  62. return
  63. fi
  64. if ! network_get_ipaddr ipaddr "$wanif"; then
  65. proto_notify_error "$cfg" "NO_WAN_LINK"
  66. return
  67. fi
  68. }
  69. proto_init_update "$link" 1
  70. [ -n "$ip6addr" ] && {
  71. local local6="${ip6addr%%/*}"
  72. local mask6="${ip6addr##*/}"
  73. [ "$local6" = "$mask6" ] && mask6=
  74. proto_add_ipv6_address "$local6" "$mask6"
  75. proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
  76. }
  77. for ip6prefix in $ip6prefixes; do
  78. proto_add_ipv6_prefix "$ip6prefix"
  79. proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
  80. done
  81. proto_add_tunnel
  82. json_add_string mode sit
  83. json_add_int mtu "${mtu:-1280}"
  84. json_add_int ttl "${ttl:-64}"
  85. [ -n "$tos" ] && json_add_string tos "$tos"
  86. json_add_string local "$ipaddr"
  87. json_add_string remote "$peeraddr"
  88. [ -n "$tunlink" ] && json_add_string link "$tunlink"
  89. proto_close_tunnel
  90. proto_send_update "$cfg"
  91. [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
  92. [ -n "$updatekey" ] && password="$updatekey"
  93. local http="http"
  94. local urlget="uclient-fetch"
  95. local urlget_opts="-qO-"
  96. local ca_path="${SSL_CERT_DIR:-/etc/ssl/certs}"
  97. [ -f /lib/libustream-ssl.so ] && http=https
  98. [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
  99. urlget_opts="$urlget_opts --no-check-certificate"
  100. }
  101. local url="$http://ipv4.tunnelbroker.net/nic/update?hostname=$tunnelid"
  102. test_6in4_rfc1918 "$ipaddr" && {
  103. local url="${url}&myip=${ipaddr}"
  104. }
  105. local try=0
  106. local max=3
  107. (
  108. set -o pipefail
  109. while [ $((++try)) -le $max ]; do
  110. if proto_6in4_update $urlget $urlget_opts --user="$username" --password="$password" "$url" 2>&1 | \
  111. sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
  112. logger -t "$link";
  113. then
  114. logger -t "$link" "updated"
  115. return 0
  116. fi
  117. sleep 5
  118. done
  119. logger -t "$link" "update failed"
  120. )
  121. }
  122. }
  123. proto_6in4_teardown() {
  124. local cfg="$1"
  125. }
  126. proto_6in4_init_config() {
  127. no_device=1
  128. available=1
  129. proto_config_add_string "ipaddr"
  130. proto_config_add_string "ip6addr"
  131. proto_config_add_array "ip6prefix"
  132. proto_config_add_string "peeraddr"
  133. proto_config_add_string "tunlink"
  134. proto_config_add_string "tunnelid"
  135. proto_config_add_string "username"
  136. proto_config_add_string "password"
  137. proto_config_add_string "updatekey"
  138. proto_config_add_int "mtu"
  139. proto_config_add_int "ttl"
  140. proto_config_add_string "tos"
  141. proto_config_add_string "device"
  142. }
  143. [ -n "$INCLUDE_ONLY" ] || {
  144. add_protocol 6in4
  145. }