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 tunnelid username password updatekey sourcerouting
31 json_get_vars mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey sourcerouting
33 [ -z "$peeraddr" ] && {
34 proto_notify_error "$cfg" "MISSING_ADDRESS"
35 proto_block_restart "$cfg"
39 ( proto_add_host_dependency "$cfg" 0.0.0.0 )
43 if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
44 proto_notify_error "$cfg" "NO_WAN_LINK"
49 proto_init_update "$link" 1
52 [ "$sourcerouting" != "0" ] && source="::/128"
53 proto_add_ipv6_route "::" 0 "" "" "" "$source"
55 [ -n "$ip6addr" ] && {
56 local local6="${ip6addr%%/*}"
57 local mask6="${ip6addr##*/}"
58 [[ "$local6" = "$mask6" ]] && mask6=
59 proto_add_ipv6_address "$local6" "$mask6"
60 [ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
63 [ -n "$ip6prefix" ] && {
64 proto_add_ipv6_prefix "$ip6prefix"
65 [ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
69 json_add_string mode sit
70 json_add_int mtu "${mtu:-1280}"
71 json_add_int ttl "${ttl:-64}"
72 [ -n "$tos" ] && json_add_string tos "$tos"
73 json_add_string local "$ipaddr"
74 json_add_string remote "$peeraddr"
77 proto_send_update "$cfg"
79 [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
80 [ -n "$updatekey" ] && password="$updatekey"
84 local urlget_opts="-qO-"
85 local ca_path="${SSL_CERT_DIR-/etc/ssl/certs}"
87 if [ -n "$(which curl)" ]; then
90 if curl -V | grep "Protocols:" | grep -qF "https"; then
92 urlget_opts="$urlget_opts --capath $ca_path"
95 if [ "$http" = "http" ] &&
96 wget --version 2>&1 | grep -qF "+https"; then
98 urlget_opts="-qO- --ca-directory=$ca_path"
101 [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
102 if [ "$urlget" = "curl" ]; then
103 urlget_opts="$urlget_opts -k"
105 urlget_opts="$urlget_opts --no-check-certificate"
109 local url="$http://ipv4.tunnelbroker.net/nic/update?username=$username&password=$password&hostname=$tunnelid"
115 while [ $((++try)) -le $max ]; do
116 if proto_6in4_update $urlget $urlget_opts "$url" 2>&1 | \
117 sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
120 logger -t "$link" "updated"
125 logger -t "$link" "update failed"
130 proto_6in4_teardown() {
134 proto_6in4_init_config() {
138 proto_config_add_string "ipaddr"
139 proto_config_add_string "ip6addr"
140 proto_config_add_string "ip6prefix"
141 proto_config_add_string "peeraddr"
142 proto_config_add_string "tunnelid"
143 proto_config_add_string "username"
144 proto_config_add_string "password"
145 proto_config_add_string "updatekey"
146 proto_config_add_int "mtu"
147 proto_config_add_int "ttl"
148 proto_config_add_string "tos"
149 proto_config_add_boolean "sourcerouting"
152 [ -n "$INCLUDE_ONLY" ] || {