packages: sort network related packages into package/network/
[oweals/openwrt.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 tunnelid username password
18         json_get_vars mtu ttl ipaddr peeraddr ip6addr tunnelid username password
19
20         [ -z "$ip6addr" -o -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         local local6="${ip6addr%%/*}"
37         local mask6="${ip6addr##*/}"
38         [[ "$local6" = "$mask6" ]] && mask6=
39
40         proto_init_update "$link" 1
41         proto_add_ipv6_address "$local6" "$mask6"
42         proto_add_ipv6_route "::" 0
43
44         proto_add_tunnel
45         json_add_string mode sit
46         json_add_int mtu "${mtu:-1280}"
47         json_add_int ttl "${ttl:-64}"
48         json_add_string local "$ipaddr"
49         json_add_string remote "$peeraddr"
50         proto_close_tunnel
51
52         proto_send_update "$cfg"
53
54         [ -n "$tunnelid" -a -n "$username" -a -n "$password" ] && {
55                 [ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
56                         password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
57                 }
58
59                 local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=$username&pass=$password&tid=$tunnelid"
60                 local try=0
61                 local max=3
62
63                 while [ $((++try)) -le $max ]; do
64                         wget -qO/dev/null "$url" 2>/dev/null && break
65                         sleep 1
66                 done
67         }
68 }
69
70 proto_6in4_teardown() {
71         local cfg="$1"
72 }
73
74 proto_6in4_init_config() {
75         no_device=1             
76         available=1
77
78         proto_config_add_string "ipaddr"
79         proto_config_add_string "ip6addr"
80         proto_config_add_string "peeraddr"
81         proto_config_add_string "tunnelid"
82         proto_config_add_string "username"
83         proto_config_add_string "password"
84         proto_config_add_int "mtu"
85         proto_config_add_int "ttl"
86 }
87
88 [ -n "$INCLUDE_ONLY" ] || {
89         add_protocol 6in4
90 }