6in4.sh 3.8 KB

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