2 # 6in4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2015 OpenWrt.org
5 [ -n "$INCLUDE_ONLY" ] ||
{
7 .
/lib
/functions
/network.sh
12 # Function taken from 6to4 package (6to4.sh), flipped returns
15 local oIFS
="$IFS"; IFS
="."; set -- $1; IFS
="$oIFS"
16 [ $1 -eq 10 ] && return 1
17 [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 1
18 [ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 1
21 [ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 1
30 (while [ $((timeout--)) -gt 0 ]; do
33 done; kill -9 $$) 2>/dev/null &
39 proto_6in4_add_prefix
() {
46 local link
="6in4-$cfg"
48 local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix ip6prefixes tunlink tunnelid username password updatekey
49 json_get_vars mtu ttl tos ipaddr peeraddr ip6addr tunlink tunnelid username password updatekey
50 json_for_each_item proto_6in4_add_prefix ip6prefix ip6prefixes
52 [ -z "$peeraddr" ] && {
53 proto_notify_error
"$cfg" "MISSING_ADDRESS"
54 proto_block_restart
"$cfg"
58 ( proto_add_host_dependency
"$cfg" "$peeraddr" "$tunlink" )
61 local wanif
="$tunlink"
62 if [ -z "$wanif" ] && ! network_find_wan wanif
; then
63 proto_notify_error
"$cfg" "NO_WAN_LINK"
67 if ! network_get_ipaddr ipaddr
"$wanif"; then
68 proto_notify_error
"$cfg" "NO_WAN_LINK"
73 proto_init_update
"$link" 1
75 [ -n "$ip6addr" ] && {
76 local local6
="${ip6addr%%/*}"
77 local mask6
="${ip6addr##*/}"
78 [ "$local6" = "$mask6" ] && mask6
=
79 proto_add_ipv6_address
"$local6" "$mask6"
80 proto_add_ipv6_route
"::" 0 "" "" "" "$local6/$mask6"
83 for ip6prefix
in $ip6prefixes; do
84 proto_add_ipv6_prefix
"$ip6prefix"
85 proto_add_ipv6_route
"::" 0 "" "" "" "$ip6prefix"
89 json_add_string mode sit
90 json_add_int mtu
"${mtu:-1280}"
91 json_add_int ttl
"${ttl:-64}"
92 [ -n "$tos" ] && json_add_string tos
"$tos"
93 json_add_string
local "$ipaddr"
94 json_add_string remote
"$peeraddr"
95 [ -n "$tunlink" ] && json_add_string link
"$tunlink"
98 proto_send_update
"$cfg"
100 [ -n "$tunnelid" -a -n "$username" -a \
( -n "$password" -o -n "$updatekey" \
) ] && {
101 [ -n "$updatekey" ] && password
="$updatekey"
104 local urlget
="uclient-fetch"
105 local urlget_opts
="-qO-"
106 local ca_path
="${SSL_CERT_DIR:-/etc/ssl/certs}"
108 [ -f /lib
/libustream-ssl.so
] && http
=https
109 [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
110 urlget_opts
="$urlget_opts --no-check-certificate"
113 local url
="$http://ipv4.tunnelbroker.net/nic/update?hostname=$tunnelid"
115 test_6in4_rfc1918
"$ipaddr" && {
116 local url
="${url}&myip=${ipaddr}"
124 while [ $
((++try
)) -le $max ]; do
125 if proto_6in4_update
$urlget $urlget_opts --user="$username" --password="$password" "$url" 2>&1 | \
126 sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
129 logger
-t "$link" "updated"
134 logger
-t "$link" "update failed"
139 proto_6in4_teardown
() {
143 proto_6in4_init_config
() {
147 proto_config_add_string
"ipaddr"
148 proto_config_add_string
"ip6addr"
149 proto_config_add_array
"ip6prefix"
150 proto_config_add_string
"peeraddr"
151 proto_config_add_string
"tunlink"
152 proto_config_add_string
"tunnelid"
153 proto_config_add_string
"username"
154 proto_config_add_string
"password"
155 proto_config_add_string
"updatekey"
156 proto_config_add_int
"mtu"
157 proto_config_add_int
"ttl"
158 proto_config_add_string
"tos"
161 [ -n "$INCLUDE_ONLY" ] ||
{