[package] /etc/functions.sh => /lib/functions.sh
[openwrt.git] / package / 6in4 / files / 6in4.sh
1 #!/bin/sh
2 # 6in4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010 OpenWrt.org
4
5 [ -n "$INCLUDE_ONLY" ] || {
6         . /lib/functions.sh
7         . ../netifd-proto.sh
8         init_proto "$@"
9 }
10
11 find_6in4_wanif() {
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"
14 }
15
16 find_6in4_wanip() {
17         local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
18         echo "${ip%%[^0-9.]*}"
19 }
20
21 tun_error() {
22         local cfg="$1"; shift;
23
24         [ -n "$1" ] && proto_notify_error "$cfg" "$@"
25         proto_block_restart "$cfg"
26 }
27
28 proto_6in4_setup() {
29         local cfg="$1"
30         local iface="$2"
31         local link="6in4-$cfg"
32
33         json_get_var mtu mtu
34         json_get_var ttl ttl
35         json_get_var local4 ipaddr
36         json_get_var remote4 peeraddr
37         json_get_var ip6addr ip6addr
38         json_get_var tunnelid tunnelid
39         json_get_var username username
40         json_get_var password password
41
42         [ -z "$ip6addr" -o -z "$remote4" ] && {
43                 tun_error "$cfg" "MISSING_ADDRESS"
44                 return
45         }
46
47         [ -z "$local4" ] && {
48                 local wanif=$(find_6in4_wanif)
49                 [ -z "$wanif" ] && {
50                         tun_error "$cfg" "NO_WAN_LINK"
51                         return
52                 }
53
54                 . /lib/network/config.sh
55                 local wancfg="$(find_config "$wanif")"
56                 [ -z "$wancfg" ] && {
57                         tun_error "$cfg" "NO_WAN_LINK"
58                         return
59                 }
60
61                 # If local4 is unset, guess local IPv4 address from the
62                 # interface used by the default route.
63                 [ -n "$wanif" ] && local4=$(find_6in4_wanip "$wanif")
64
65                 [ -z "$local4" ] && {
66                         tun_error "$cfg" "NO_WAN_LINK"
67                         return
68                 }
69         }
70
71         local local6="${ip6addr%%/*}"
72         local mask6="${ip6addr##*/}"
73         [[ "$local6" = "$mask6" ]] && mask6=
74
75         proto_init_update "$link" 1
76         proto_add_ipv6_address "$local6" "$mask6"
77         proto_add_ipv6_route "::" 0
78
79         proto_add_tunnel
80         json_add_string mode sit
81         json_add_int mtu "${mtu:-1280}"
82         json_add_int ttl "${ttl:-64}"
83         json_add_string local "$local4"
84         json_add_string remote "$remote4"
85         proto_close_tunnel
86
87         proto_send_update "$cfg"
88
89         [ -n "$tunnelid" -a -n "$username" -a -n "$password" ] && {
90                 [ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
91                         password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
92                 }
93
94                 local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=$username&pass=$password&tid=$tunnelid"
95                 local try=0
96                 local max=3
97
98                 while [ $((++try)) -le $max ]; do
99                         wget -qO/dev/null "$url" 2>/dev/null && break
100                         sleep 1
101                 done
102         }
103 }
104
105 proto_6in4_teardown() {
106         local cfg="$1"
107 }
108
109 proto_6in4_init_config() {
110         no_device=1             
111         available=1
112
113         proto_config_add_string "ipaddr"
114         proto_config_add_string "ip6addr"
115         proto_config_add_string "peeraddr"
116         proto_config_add_string "tunnelid"
117         proto_config_add_string "username"
118         proto_config_add_string "password"
119         proto_config_add_int "mtu"
120         proto_config_add_int "ttl"
121 }
122
123 [ -n "$INCLUDE_ONLY" ] || {
124         add_protocol 6in4
125 }