6in4: don't use /dev/stdout for wget calls
[lede.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_setup() {
13         local cfg="$1"
14         local iface="$2"
15         local link="6in4-$cfg"
16
17         local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey sourcerouting
18         json_get_vars mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey sourcerouting
19
20         [ -z "$peeraddr" ] && {
21                 proto_notify_error "$cfg" "MISSING_ADDRESS"
22                 proto_block_restart "$cfg"
23                 return
24         }
25
26         ( proto_add_host_dependency "$cfg" 0.0.0.0 )
27
28         [ -z "$ipaddr" ] && {
29                 local wanif
30                 if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
31                         proto_notify_error "$cfg" "NO_WAN_LINK"
32                         return
33                 fi
34         }
35
36         proto_init_update "$link" 1
37
38         local source=""
39         [ "$sourcerouting" != "0" ] && source="::/128"
40         proto_add_ipv6_route "::" 0 "" "" "" "$source"
41
42         [ -n "$ip6addr" ] && {
43                 local local6="${ip6addr%%/*}"
44                 local mask6="${ip6addr##*/}"
45                 [[ "$local6" = "$mask6" ]] && mask6=
46                 proto_add_ipv6_address "$local6" "$mask6"
47                 [ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
48         }
49
50         [ -n "$ip6prefix" ] && {
51                 proto_add_ipv6_prefix "$ip6prefix"
52                 [ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
53         }
54
55         proto_add_tunnel
56         json_add_string mode sit
57         json_add_int mtu "${mtu:-1280}"
58         json_add_int ttl "${ttl:-64}"
59         [ -n "$tos" ] && json_add_string tos "$tos"
60         json_add_string local "$ipaddr"
61         json_add_string remote "$peeraddr"
62         proto_close_tunnel
63
64         proto_send_update "$cfg"
65
66         [ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
67                 [ -n "$updatekey" ] && password="$updatekey"
68
69                 local http="http"
70                 local urlget="wget"
71                 local urlget_opts="-qO-"
72                 local ca_path="${SSL_CERT_DIR-/etc/ssl/certs}"
73
74                 if [ -n "$(which curl)" ]; then
75                         urlget="curl"
76                         urlget_opts="-s -S"
77                         if curl -V | grep "Protocols:" | grep -qF "https"; then
78                                 http="https"
79                                 urlget_opts="$urlget_opts --capath $ca_path"
80                         fi
81                 fi
82                 if [ "$http" = "http" ] &&
83                         wget --version 2>&1 | grep -qF "+https"; then
84                         urlget="wget"
85                         urlget_opts="-qO- --ca-directory=$ca_path"
86                         http="https"
87                 fi
88                 [ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
89                         if [ "$urlget" = "curl" ]; then
90                                 urlget_opts="$urlget_opts -k"
91                         else
92                                 urlget_opts="$urlget_opts --no-check-certificate"
93                         fi
94                 }
95
96                 local url="$http://ipv4.tunnelbroker.net/nic/update?username=$username&password=$password&hostname=$tunnelid"
97                 local try=0
98                 local max=3
99
100                 while [ $((++try)) -le $max ]; do
101                         ( exec $urlget $urlget_opts "$url" | logger -t "$link" ) &
102                         local pid=$!
103                         ( sleep 20; kill $pid 2>/dev/null ) &
104                         wait $pid && break
105                         sleep 20;
106                 done
107         }
108 }
109
110 proto_6in4_teardown() {
111         local cfg="$1"
112 }
113
114 proto_6in4_init_config() {
115         no_device=1
116         available=1
117
118         proto_config_add_string "ipaddr"
119         proto_config_add_string "ip6addr"
120         proto_config_add_string "ip6prefix"
121         proto_config_add_string "peeraddr"
122         proto_config_add_string "tunnelid"
123         proto_config_add_string "username"
124         proto_config_add_string "password"
125         proto_config_add_string "updatekey"
126         proto_config_add_int "mtu"
127         proto_config_add_int "ttl"
128         proto_config_add_string "tos"
129         proto_config_add_boolean "sourcerouting"
130 }
131
132 [ -n "$INCLUDE_ONLY" ] || {
133         add_protocol 6in4
134 }