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