2 # 6in4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2015 OpenWrt.org
5 [ -n "$INCLUDE_ONLY" ] || {
7 . /lib/functions/network.sh
16 (while [ $((timeout--)) -gt 0 ]; do
19 done; kill -9 $$) 2>/dev/null &
28 local link="6in4-$cfg"
30 local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunlink tunnelid username password updatekey
31 json_get_vars mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunlink tunnelid username password updatekey
33 [ -z "$peeraddr" ] && {
34 proto_notify_error "$cfg" "MISSING_ADDRESS"
35 proto_block_restart "$cfg"
39 ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
42 local wanif="$tunlink"
43 if [ -z "$wanif" ] && ! network_find_wan wanif; then
44 proto_notify_error "$cfg" "NO_WAN_LINK"
48 if ! network_get_ipaddr ipaddr "$wanif"; then
49 proto_notify_error "$cfg" "NO_WAN_LINK"
54 proto_init_update "$link" 1
56 [ -n "$ip6addr" ] && {
57 local local6="${ip6addr%%/*}"
58 local mask6="${ip6addr##*/}"
59 [[ "$local6" = "$mask6" ]] && mask6=
60 proto_add_ipv6_address "$local6" "$mask6"
61 proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
64 [ -n "$ip6prefix" ] && {
65 proto_add_ipv6_prefix "$ip6prefix"
66 proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
70 json_add_string mode sit
71 json_add_int mtu "${mtu:-1280}"
72 json_add_int ttl "${ttl:-64}"
73 [ -n "$tos" ] && json_add_string tos "$tos"
74 json_add_string local "$ipaddr"
75 json_add_string remote "$peeraddr"
76 [ -n "$tunlink" ] && json_add_string link "$tunlink"
79 proto_send_update "$cfg"
81 [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
82 [ -n "$updatekey" ] && password="$updatekey"
86 local urlget_opts="-qO-"
87 local ca_path="${SSL_CERT_DIR-/etc/ssl/certs}"
89 if [ -n "$(which curl)" ]; then
92 if curl -V | grep "Protocols:" | grep -qF "https"; then
94 urlget_opts="$urlget_opts --capath $ca_path"
97 if [ "$http" = "http" ] &&
98 wget --version 2>&1 | grep -qF "+https"; then
100 urlget_opts="-qO- --ca-directory=$ca_path"
103 [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
104 if [ "$urlget" = "curl" ]; then
105 urlget_opts="$urlget_opts -k"
107 urlget_opts="$urlget_opts --no-check-certificate"
111 local url="$http://ipv4.tunnelbroker.net/nic/update?username=$username&password=$password&hostname=$tunnelid"
117 while [ $((++try)) -le $max ]; do
118 if proto_6in4_update $urlget $urlget_opts "$url" 2>&1 | \
119 sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
122 logger -t "$link" "updated"
127 logger -t "$link" "update failed"
132 proto_6in4_teardown() {
136 proto_6in4_init_config() {
140 proto_config_add_string "ipaddr"
141 proto_config_add_string "ip6addr"
142 proto_config_add_string "ip6prefix"
143 proto_config_add_string "peeraddr"
144 proto_config_add_string "tunlink"
145 proto_config_add_string "tunnelid"
146 proto_config_add_string "username"
147 proto_config_add_string "password"
148 proto_config_add_string "updatekey"
149 proto_config_add_int "mtu"
150 proto_config_add_int "ttl"
151 proto_config_add_string "tos"
154 [ -n "$INCLUDE_ONLY" ] || {