2 # 6in4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010 OpenWrt.org
5 [ -n "$INCLUDE_ONLY" ] || {
12 local if=$(ip -4 r l e 0.0.0.0/0); if="${if#default* dev }"; if="${if%% *}"
13 [ -n "$if" ] && grep -qs "^ *$if:" /proc/net/dev && echo "$if"
17 local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
18 echo "${ip%%[^0-9.]*}"
21 # Hook into scan_interfaces() to synthesize a .device option
22 # This is needed for /sbin/ifup to properly dispatch control
23 # to setup_interface_6in4() even if no .ifname is set in
26 config_set "$1" device "6in4-$1"
29 coldplug_interface_6in4() {
30 setup_interface_6in4 "6in4-$1" "$1"
35 local cfg="$1"; shift;
37 [ -n "$1" ] && proto_notify_error "$cfg" "$@"
38 proto_block_restart "$cfg"
44 local link="6in4-$cfg"
48 json_get_var local4 ipaddr
49 json_get_var remote4 peeraddr
50 json_get_var ip6addr ip6addr
51 json_get_var tunnelid tunnelid
52 json_get_var username username
53 json_get_var password password
55 [ -z "$ip6addr" -o -z "$remote4" ] && {
56 tun_error "$cfg" "MISSING_ADDRESS"
61 local wanif=$(find_6in4_wanif)
63 tun_error "$cfg" "NO_WAN_LINK"
67 . /lib/network/config.sh
68 local wancfg="$(find_config "$wanif")"
70 tun_error "$cfg" "NO_WAN_LINK"
74 # If local4 is unset, guess local IPv4 address from the
75 # interface used by the default route.
76 [ -n "$wanif" ] && local4=$(find_6in4_wanip "$wanif")
79 tun_error "$cfg" "NO_WAN_LINK"
84 local local6="${ip6addr%%/*}"
85 local mask6="${ip6addr##*/}"
86 [[ "$local6" = "$mask6" ]] && mask6=
88 proto_init_update "$link" 1
89 proto_add_ipv6_address "$local6" "$mask6"
90 proto_add_ipv6_route "::" 0
93 json_add_string mode sit
94 json_add_int mtu "${mtu:-1280}"
95 json_add_int ttl "${ttl:-64}"
96 json_add_string local "$local4"
97 json_add_string remote "$remote4"
100 proto_send_update "$cfg"
102 [ -n "$tunnelid" -a -n "$username" -a -n "$password" ] && {
103 [ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
104 password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
107 local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=$username&pass=$password&tid=$tunnelid"
111 while [ $((++try)) -le $max ]; do
112 wget -qO/dev/null "$url" 2>/dev/null && break
118 proto_6in4_teardown() {
122 proto_6in4_init_config() {
126 proto_config_add_string "ipaddr"
127 proto_config_add_string "ip6addr"
128 proto_config_add_string "peeraddr"
129 proto_config_add_string "tunnelid"
130 proto_config_add_string "username"
131 proto_config_add_string "password"
132 proto_config_add_int "mtu"
133 proto_config_add_int "ttl"
136 [ -n "$INCLUDE_ONLY" ] || {