6in4: add netifd support
[openwrt.git] / package / 6in4 / files / 6in4.sh
1 #!/bin/sh
2 # 6in4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010 OpenWrt.org
4
5 [ -n "$INCLUDE_ONLY" ] || {
6         . /etc/functions.sh
7         . ../netifd-proto.sh
8         init_proto "$@"
9 }
10
11 find_6in4_wanif() {
12         local if=$(ip -4 r l e 0.0.0.0/0); if="${if#default* dev }"; if="${if%% *}"
13         [ -n "$if" ] && grep -qs "^ *$if:" /proc/net/dev && echo "$if"
14 }
15
16 find_6in4_wanip() {
17         local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
18         echo "${ip%%[^0-9.]*}"
19 }
20
21 # Hook into scan_interfaces() to synthesize a .device option
22 # This is needed for /sbin/ifup to properly dispatch control
23 # to setup_interface_6in4() even if no .ifname is set in
24 # the configuration.
25 scan_6in4() {
26         config_set "$1" device "6in4-$1"
27 }
28
29 coldplug_interface_6in4() {
30         setup_interface_6in4 "6in4-$1" "$1"
31 }
32
33
34 tun_error() {
35         local cfg="$1"; shift;
36
37         [ -n "$1" ] && proto_notify_error "$cfg" "$@"
38         proto_block_restart "$cfg"
39 }
40
41 proto_6in4_setup() {
42         local cfg="$1"
43         local iface="$2"
44         local link="6in4-$cfg"
45
46         json_get_var mtu mtu
47         json_get_var ttl ttl
48         json_get_var local4 ipaddr
49         json_get_var remote4 peeraddr
50         json_get_var ip6addr ip6addr
51         json_get_var tunnelid tunnelid
52         json_get_var username username
53         json_get_var password password
54
55         [ -z "$ip6addr" -o -z "$remote4" ] && {
56                 tun_error "$cfg" "MISSING_ADDRESS"
57                 return
58         }
59
60         [ -z "$local4" ] && {
61                 local wanif=$(find_6in4_wanif)
62                 [ -z "$wanif" ] && {
63                         tun_error "$cfg" "NO_WAN_LINK"
64                         return
65                 }
66
67                 . /lib/network/config.sh
68                 local wancfg="$(find_config "$wanif")"
69                 [ -z "$wancfg" ] && {
70                         tun_error "$cfg" "NO_WAN_LINK"
71                         return
72                 }
73
74                 # If local4 is unset, guess local IPv4 address from the
75                 # interface used by the default route.
76                 [ -n "$wanif" ] && local4=$(find_6in4_wanip "$wanif")
77
78                 [ -z "$local4" ] && {
79                         tun_error "$cfg" "NO_WAN_LINK"
80                         return
81                 }
82         }
83
84         local local6="${ip6addr%%/*}"
85         local mask6="${ip6addr##*/}"
86         [[ "$local6" = "$mask6" ]] && mask6=
87
88         proto_init_update "$link" 1
89         proto_add_ipv6_address "$local6" "$mask6"
90         proto_add_ipv6_route "::" 0
91
92         proto_add_tunnel
93         json_add_string mode sit
94         json_add_int mtu "${mtu:-1280}"
95         json_add_int ttl "${ttl:-64}"
96         json_add_string local "$local4"
97         json_add_string remote "$remote4"
98         proto_close_tunnel
99
100         proto_send_update "$cfg"
101
102         [ -n "$tunnelid" -a -n "$username" -a -n "$password" ] && {
103                 [ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
104                         password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
105                 }
106
107                 local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=$username&pass=$password&tid=$tunnelid"
108                 local try=0
109                 local max=3
110
111                 while [ $((++try)) -le $max ]; do
112                         wget -qO/dev/null "$url" 2>/dev/null && break
113                         sleep 1
114                 done
115         }
116 }
117
118 proto_6in4_teardown() {
119         local cfg="$1"
120 }
121
122 proto_6in4_init_config() {
123         no_device=1             
124         available=1
125
126         proto_config_add_string "ipaddr"
127         proto_config_add_string "ip6addr"
128         proto_config_add_string "peeraddr"
129         proto_config_add_string "tunnelid"
130         proto_config_add_string "username"
131         proto_config_add_string "password"
132         proto_config_add_int "mtu"
133         proto_config_add_int "ttl"
134 }
135
136 [ -n "$INCLUDE_ONLY" ] || {
137         add_protocol 6in4
138 }