af2ddca1d7959a56e0cc6d71e3e769acab3cabc2
[openwrt/staging/wigyori.git] / package / network / ipv6 / 6in4 / files / 6in4.sh
1 #!/bin/sh
2 # 6in4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2015 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_update() {
13 sh -c '
14 local timeout=5
15
16 (while [ $((timeout--)) -gt 0 ]; do
17 sleep 1
18 kill -0 $$ || exit 0
19 done; kill -9 $$) 2>/dev/null &
20
21 exec "$@"
22 ' "$1" "$@"
23 }
24
25 proto_6in4_setup() {
26 local cfg="$1"
27 local iface="$2"
28 local link="6in4-$cfg"
29
30 local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey
31 json_get_vars mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey
32
33 [ -z "$peeraddr" ] && {
34 proto_notify_error "$cfg" "MISSING_ADDRESS"
35 proto_block_restart "$cfg"
36 return
37 }
38
39 ( proto_add_host_dependency "$cfg" "$peeraddr" )
40
41 [ -z "$ipaddr" ] && {
42 local wanif
43 if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
44 proto_notify_error "$cfg" "NO_WAN_LINK"
45 return
46 fi
47 }
48
49 proto_init_update "$link" 1
50
51 [ -n "$ip6addr" ] && {
52 local local6="${ip6addr%%/*}"
53 local mask6="${ip6addr##*/}"
54 [[ "$local6" = "$mask6" ]] && mask6=
55 proto_add_ipv6_address "$local6" "$mask6"
56 proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
57 }
58
59 [ -n "$ip6prefix" ] && {
60 proto_add_ipv6_prefix "$ip6prefix"
61 proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
62 }
63
64 proto_add_tunnel
65 json_add_string mode sit
66 json_add_int mtu "${mtu:-1280}"
67 json_add_int ttl "${ttl:-64}"
68 [ -n "$tos" ] && json_add_string tos "$tos"
69 json_add_string local "$ipaddr"
70 json_add_string remote "$peeraddr"
71 proto_close_tunnel
72
73 proto_send_update "$cfg"
74
75 [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
76 [ -n "$updatekey" ] && password="$updatekey"
77
78 local http="http"
79 local urlget="wget"
80 local urlget_opts="-qO-"
81 local ca_path="${SSL_CERT_DIR-/etc/ssl/certs}"
82
83 if [ -n "$(which curl)" ]; then
84 urlget="curl"
85 urlget_opts="-s -S"
86 if curl -V | grep "Protocols:" | grep -qF "https"; then
87 http="https"
88 urlget_opts="$urlget_opts --capath $ca_path"
89 fi
90 fi
91 if [ "$http" = "http" ] &&
92 wget --version 2>&1 | grep -qF "+https"; then
93 urlget="wget"
94 urlget_opts="-qO- --ca-directory=$ca_path"
95 http="https"
96 fi
97 [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
98 if [ "$urlget" = "curl" ]; then
99 urlget_opts="$urlget_opts -k"
100 else
101 urlget_opts="$urlget_opts --no-check-certificate"
102 fi
103 }
104
105 local url="$http://ipv4.tunnelbroker.net/nic/update?username=$username&password=$password&hostname=$tunnelid"
106 local try=0
107 local max=3
108
109 (
110 set -o pipefail
111 while [ $((++try)) -le $max ]; do
112 if proto_6in4_update $urlget $urlget_opts "$url" 2>&1 | \
113 sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
114 logger -t "$link";
115 then
116 logger -t "$link" "updated"
117 return 0
118 fi
119 sleep 5
120 done
121 logger -t "$link" "update failed"
122 )
123 }
124 }
125
126 proto_6in4_teardown() {
127 local cfg="$1"
128 }
129
130 proto_6in4_init_config() {
131 no_device=1
132 available=1
133
134 proto_config_add_string "ipaddr"
135 proto_config_add_string "ip6addr"
136 proto_config_add_string "ip6prefix"
137 proto_config_add_string "peeraddr"
138 proto_config_add_string "tunnelid"
139 proto_config_add_string "username"
140 proto_config_add_string "password"
141 proto_config_add_string "updatekey"
142 proto_config_add_int "mtu"
143 proto_config_add_int "ttl"
144 proto_config_add_string "tos"
145 }
146
147 [ -n "$INCLUDE_ONLY" ] || {
148 add_protocol 6in4
149 }