165056cdb0d50541dc2d752eaccb2ba24cd5b888
[12.09/openwrt.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 proto_6in4_setup() {
13         local cfg="$1"
14         local iface="$2"
15         local link="6in4-$cfg"
16
17         local mtu ttl ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey
18         json_get_vars mtu ttl ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey
19
20         [ -z "$peeraddr" ] && {
21                 proto_notify_error "$cfg" "MISSING_ADDRESS"
22                 proto_block_restart "$cfg"
23                 return
24         }
25
26         ( proto_add_host_dependency "$cfg" 0.0.0.0 )
27
28         [ -z "$ipaddr" ] && {
29                 local wanif
30                 if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
31                         proto_notify_error "$cfg" "NO_WAN_LINK"
32                         return
33                 fi
34         }
35
36         proto_init_update "$link" 1
37         proto_add_ipv6_route "::" 0
38
39         [ -n "$ip6addr" ] && {
40                 local local6="${ip6addr%%/*}"
41                 local mask6="${ip6addr##*/}"
42                 [[ "$local6" = "$mask6" ]] && mask6=
43                 proto_add_ipv6_address "$local6" "$mask6"
44         }
45
46         [ -n "$ip6prefix" ] && proto_add_ipv6_prefix "$ip6prefix"
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 "$ipaddr"
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" -o -n "$updatekey" \) ] && {
59                 [ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
60                         password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
61                 }
62
63                 [ -n "$updatekey" ] && password="$updatekey"
64
65                 local url="http://ipv4.tunnelbroker.net/nic/update?username=$username&password=$password&hostname=$tunnelid"
66                 local try=0
67                 local max=3
68
69                 while [ $((++try)) -le $max ]; do
70                         ( exec wget -qO/dev/null "$url" 2>/dev/null ) &
71                         local pid=$!
72                         ( sleep 5; kill $pid 2>/dev/null ) &
73                         wait $pid && break
74                 done
75         }
76 }
77
78 proto_6in4_teardown() {
79         local cfg="$1"
80 }
81
82 proto_6in4_init_config() {
83         no_device=1             
84         available=1
85
86         proto_config_add_string "ipaddr"
87         proto_config_add_string "ip6addr"
88         proto_config_add_string "ip6prefix"
89         proto_config_add_string "peeraddr"
90         proto_config_add_string "tunnelid"
91         proto_config_add_string "username"
92         proto_config_add_string "password"
93         proto_config_add_string "updatekey"
94         proto_config_add_int "mtu"
95         proto_config_add_int "ttl"
96 }
97
98 [ -n "$INCLUDE_ONLY" ] || {
99         add_protocol 6in4
100 }