[package] 6in4: use network_find_wan() and network_get_iapddr() to find local ip...
[openwrt/svn-archive/archive.git] / package / 6in4 / files / 6in4.sh
1 #!/bin/sh
2 # 6in4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2012 OpenWrt.org
4
5 [ -n "$INCLUDE_ONLY" ] || {
6 . /lib/functions.sh
7 . /lib/functions/network.sh
8 . ../netifd-proto.sh
9 init_proto "$@"
10 }
11
12 tun_error() {
13 local cfg="$1"; shift;
14
15 [ -n "$1" ] && proto_notify_error "$cfg" "$@"
16 proto_block_restart "$cfg"
17 }
18
19 proto_6in4_setup() {
20 local cfg="$1"
21 local iface="$2"
22 local link="6in4-$cfg"
23
24 local mtu ttl local4 peeraddr ip6addr tunnelid username password
25 json_get_vars mtu ttl local4 peeraddr ip6addr tunnelid username password
26
27 [ -z "$ip6addr" -o -z "$peeraddr" ] && {
28 tun_error "$cfg" "MISSING_ADDRESS"
29 return
30 }
31
32 [ -z "$local4" ] && {
33 local wanif
34 if ! network_find_wan wanif || ! network_get_ipaddr local4 "$wanif"; then
35 tun_error "$cfg" "NO_WAN_LINK"
36 return
37 fi
38 }
39
40 local local6="${ip6addr%%/*}"
41 local mask6="${ip6addr##*/}"
42 [[ "$local6" = "$mask6" ]] && mask6=
43
44 proto_init_update "$link" 1
45 proto_add_ipv6_address "$local6" "$mask6"
46 proto_add_ipv6_route "::" 0
47
48 proto_add_tunnel
49 json_add_string mode sit
50 json_add_int mtu "${mtu:-1280}"
51 json_add_int ttl "${ttl:-64}"
52 json_add_string local "$local4"
53 json_add_string remote "$peeraddr"
54 proto_close_tunnel
55
56 proto_send_update "$cfg"
57
58 [ -n "$tunnelid" -a -n "$username" -a -n "$password" ] && {
59 [ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
60 password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
61 }
62
63 local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=$username&pass=$password&tid=$tunnelid"
64 local try=0
65 local max=3
66
67 while [ $((++try)) -le $max ]; do
68 wget -qO/dev/null "$url" 2>/dev/null && break
69 sleep 1
70 done
71 }
72 }
73
74 proto_6in4_teardown() {
75 local cfg="$1"
76 }
77
78 proto_6in4_init_config() {
79 no_device=1
80 available=1
81
82 proto_config_add_string "ipaddr"
83 proto_config_add_string "ip6addr"
84 proto_config_add_string "peeraddr"
85 proto_config_add_string "tunnelid"
86 proto_config_add_string "username"
87 proto_config_add_string "password"
88 proto_config_add_int "mtu"
89 proto_config_add_int "ttl"
90 }
91
92 [ -n "$INCLUDE_ONLY" ] || {
93 add_protocol 6in4
94 }