6e53de1e45e6926e5d4d0315002d92318a916c51
[openwrt/staging/lynxis/omap.git] / package / network / ipv6 / 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 sourcerouting
18 json_get_vars mtu ttl ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey sourcerouting
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
38 local source=""
39 [ "$sourcerouting" != "0" ] && source="::/128"
40 proto_add_ipv6_route "::" 0 "" "" "" "$source"
41
42 [ -n "$ip6addr" ] && {
43 local local6="${ip6addr%%/*}"
44 local mask6="${ip6addr##*/}"
45 [[ "$local6" = "$mask6" ]] && mask6=
46 proto_add_ipv6_address "$local6" "$mask6"
47 [ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
48 }
49
50 [ -n "$ip6prefix" ] && {
51 proto_add_ipv6_prefix "$ip6prefix"
52 [ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
53 }
54
55 proto_add_tunnel
56 json_add_string mode sit
57 json_add_int mtu "${mtu:-1280}"
58 json_add_int ttl "${ttl:-64}"
59 json_add_string local "$ipaddr"
60 json_add_string remote "$peeraddr"
61 proto_close_tunnel
62
63 proto_send_update "$cfg"
64
65 [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
66 [ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
67 password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
68 }
69
70 [ -n "$updatekey" ] && password="$updatekey"
71
72 local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=$username&pass=$password&tid=$tunnelid"
73 local try=0
74 local max=3
75
76 while [ $((++try)) -le $max ]; do
77 ( exec wget -qO/dev/null "$url" 2>/dev/null ) &
78 local pid=$!
79 ( sleep 5; kill $pid 2>/dev/null ) &
80 wait $pid && break
81 done
82 }
83 }
84
85 proto_6in4_teardown() {
86 local cfg="$1"
87 }
88
89 proto_6in4_init_config() {
90 no_device=1
91 available=1
92
93 proto_config_add_string "ipaddr"
94 proto_config_add_string "ip6addr"
95 proto_config_add_string "ip6prefix"
96 proto_config_add_string "peeraddr"
97 proto_config_add_string "tunnelid"
98 proto_config_add_string "username"
99 proto_config_add_string "password"
100 proto_config_add_string "updatekey"
101 proto_config_add_int "mtu"
102 proto_config_add_int "ttl"
103 proto_config_add_boolean "sourcerouting"
104 }
105
106 [ -n "$INCLUDE_ONLY" ] || {
107 add_protocol 6in4
108 }