786f37fc14c81d8f58d0da32626eb91082192c15
[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 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="uclient-fetch"
86 local urlget_opts="-qO-"
87 local ca_path="${SSL_CERT_DIR:-/etc/ssl/certs}"
88
89 [ -f /lib/libustream-ssl.so ] && http=https
90 [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
91 urlget_opts="$urlget_opts --no-check-certificate"
92 }
93
94 local url="$http://ipv4.tunnelbroker.net/nic/update?hostname=$tunnelid"
95 local try=0
96 local max=3
97
98 (
99 set -o pipefail
100 while [ $((++try)) -le $max ]; do
101 if proto_6in4_update $urlget $urlget_opts --user="$username" --password="$password" "$url" 2>&1 | \
102 sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
103 logger -t "$link";
104 then
105 logger -t "$link" "updated"
106 return 0
107 fi
108 sleep 5
109 done
110 logger -t "$link" "update failed"
111 )
112 }
113 }
114
115 proto_6in4_teardown() {
116 local cfg="$1"
117 }
118
119 proto_6in4_init_config() {
120 no_device=1
121 available=1
122
123 proto_config_add_string "ipaddr"
124 proto_config_add_string "ip6addr"
125 proto_config_add_string "ip6prefix"
126 proto_config_add_string "peeraddr"
127 proto_config_add_string "tunlink"
128 proto_config_add_string "tunnelid"
129 proto_config_add_string "username"
130 proto_config_add_string "password"
131 proto_config_add_string "updatekey"
132 proto_config_add_int "mtu"
133 proto_config_add_int "ttl"
134 proto_config_add_string "tos"
135 }
136
137 [ -n "$INCLUDE_ONLY" ] || {
138 add_protocol 6in4
139 }