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