Rebased from upstream / out of band repository.
[librecmc/librecmc.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_add_prefix() {
26         append "$3" "$1"
27 }
28
29 proto_6in4_setup() {
30         local cfg="$1"
31         local iface="$2"
32         local link="6in4-$cfg"
33
34         local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix ip6prefixes tunlink tunnelid username password updatekey
35         json_get_vars mtu ttl tos ipaddr peeraddr ip6addr tunlink tunnelid username password updatekey
36         json_for_each_item proto_6in4_add_prefix ip6prefix ip6prefixes
37
38         [ -z "$peeraddr" ] && {
39                 proto_notify_error "$cfg" "MISSING_ADDRESS"
40                 proto_block_restart "$cfg"
41                 return
42         }
43
44         ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
45
46         [ -z "$ipaddr" ] && {
47                 local wanif="$tunlink"
48                 if [ -z "$wanif" ] && ! network_find_wan wanif; then
49                         proto_notify_error "$cfg" "NO_WAN_LINK"
50                         return
51                 fi
52
53                 if ! network_get_ipaddr ipaddr "$wanif"; then
54                         proto_notify_error "$cfg" "NO_WAN_LINK"
55                         return
56                 fi
57         }
58
59         proto_init_update "$link" 1
60
61         [ -n "$ip6addr" ] && {
62                 local local6="${ip6addr%%/*}"
63                 local mask6="${ip6addr##*/}"
64                 [[ "$local6" = "$mask6" ]] && mask6=
65                 proto_add_ipv6_address "$local6" "$mask6"
66                 proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
67         }
68
69         for ip6prefix in $ip6prefixes; do
70                 proto_add_ipv6_prefix "$ip6prefix"
71                 proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
72         done
73
74         proto_add_tunnel
75         json_add_string mode sit
76         json_add_int mtu "${mtu:-1280}"
77         json_add_int ttl "${ttl:-64}"
78         [ -n "$tos" ] && json_add_string tos "$tos"
79         json_add_string local "$ipaddr"
80         json_add_string remote "$peeraddr"
81         [ -n "$tunlink" ] && json_add_string link "$tunlink"
82         proto_close_tunnel
83
84         proto_send_update "$cfg"
85
86         [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
87                 [ -n "$updatekey" ] && password="$updatekey"
88
89                 local http="http"
90                 local urlget="uclient-fetch"
91                 local urlget_opts="-qO-"
92                 local ca_path="${SSL_CERT_DIR:-/etc/ssl/certs}"
93
94                 [ -f /lib/libustream-ssl.so ] && http=https
95                 [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
96                         urlget_opts="$urlget_opts --no-check-certificate"
97                 }
98
99                 local url="$http://ipv4.tunnelbroker.net/nic/update?hostname=$tunnelid"
100                 local try=0
101                 local max=3
102
103                 (
104                         set -o pipefail
105                         while [ $((++try)) -le $max ]; do
106                                 if proto_6in4_update $urlget $urlget_opts --user="$username" --password="$password" "$url" 2>&1 | \
107                                         sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
108                                         logger -t "$link";
109                                 then
110                                         logger -t "$link" "updated"
111                                         return 0
112                                 fi
113                                 sleep 5
114                         done
115                         logger -t "$link" "update failed"
116                 )
117         }
118 }
119
120 proto_6in4_teardown() {
121         local cfg="$1"
122 }
123
124 proto_6in4_init_config() {
125         no_device=1
126         available=1
127
128         proto_config_add_string "ipaddr"
129         proto_config_add_string "ip6addr"
130         proto_config_add_array "ip6prefix"
131         proto_config_add_string "peeraddr"
132         proto_config_add_string "tunlink"
133         proto_config_add_string "tunnelid"
134         proto_config_add_string "username"
135         proto_config_add_string "password"
136         proto_config_add_string "updatekey"
137         proto_config_add_int "mtu"
138         proto_config_add_int "ttl"
139         proto_config_add_string "tos"
140 }
141
142 [ -n "$INCLUDE_ONLY" ] || {
143         add_protocol 6in4
144 }