5a1ad475949039a8020bb609d908f4a21a226e17
[openwrt.git] / package / 6to4 / files / 6to4.sh
1 #!/bin/sh
2 # 6to4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2012 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 find_6to4_prefix() {
13         local ip4="$1"
14         local oIFS="$IFS"; IFS="."; set -- $ip4; IFS="$oIFS"
15
16         printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
17 }
18
19 test_6to4_rfc1918()
20 {
21         local oIFS="$IFS"; IFS="."; set -- $1; IFS="$oIFS"
22         [ $1 -eq  10 ] && return 0
23         [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 0
24         [ $1 -eq 172 ] && [ $2 -ge  16 ] && [ $2 -le  31 ] && return 0
25
26         # RFC 6598
27         [ $1 -eq 100 ] && [ $2 -ge  64 ] && [ $2 -le 127 ] && return 0
28
29         return 1
30 }
31
32 set_6to4_radvd_interface() {
33         local cfgid="$1"
34         local lanif="${2:-lan}"
35         local ifmtu="${3:-1280}"
36         local ifsection=""
37
38         find_ifsection() {
39                 local net
40                 local cfg="$1"
41                 config_get net "$cfg" interface
42
43                 [ "$net" = "$lanif" ] && {
44                         ifsection="$cfg"
45                         return 1
46                 }
47         }
48
49         config_foreach find_ifsection interface
50
51         [ -z "$ifsection" ] && {
52                 ifsection="iface_$sid"
53                 uci_set_state radvd "$ifsection" "" interface
54                 uci_set_state radvd "$ifsection" interface "$lanif"
55         }
56
57         uci_set_state radvd "$ifsection" ignore            0
58         uci_set_state radvd "$ifsection" IgnoreIfMissing   1
59         uci_set_state radvd "$ifsection" AdvSendAdvert     1
60         uci_set_state radvd "$ifsection" MaxRtrAdvInterval 30
61         uci_set_state radvd "$ifsection" AdvLinkMTU        "$ifmtu"
62 }
63
64 set_6to4_radvd_prefix() {
65         local cfgid="$1"
66         local lanif="${2:-lan}"
67         local wanif="${3:-wan}"
68         local prefix="${4:-0:0:0:1::/64}"
69         local vlt="${5:-300}"
70         local plt="${6:-120}"
71         local pfxsection=""
72
73         find_pfxsection() {
74                 local net base
75                 local cfg="$1"
76                 config_get net  "$cfg" interface
77                 config_get base "$cfg" Base6to4Interface
78
79                 [ "$net" = "$lanif" ] && [ "$base" = "$wanif" ] && {
80                         pfxsection="$cfg"
81                         return 1
82                 }
83         }
84
85         config_foreach find_pfxsection prefix
86
87         [ -z "$pfxsection" ] && {
88                 pfxsection="prefix_${sid}_${lanif}"
89                 uci_set_state radvd "$pfxsection" ""                   prefix
90                 uci_set_state radvd "$pfxsection" ignore               0
91                 uci_set_state radvd "$pfxsection" interface            "$lanif"
92                 uci_set_state radvd "$pfxsection" prefix               "$prefix"
93                 uci_set_state radvd "$pfxsection" AdvOnLink            1
94                 uci_set_state radvd "$pfxsection" AdvAutonomous        1
95                 uci_set_state radvd "$pfxsection" AdvValidLifetime     "$vlt"
96                 uci_set_state radvd "$pfxsection" AdvPreferredLifetime "$plt"
97                 uci_set_state radvd "$pfxsection" Base6to4Interface    "$wanif"
98         }
99 }
100
101 tun_error() {
102         local cfg="$1"; shift;
103
104         [ -n "$1" ] && proto_notify_error "$cfg" "$@"
105         proto_block_restart "$cfg"
106 }
107
108 proto_6to4_setup() {
109         local cfg="$1"
110         local iface="$2"
111         local link="6to4-$cfg"
112
113         local mtu ttl ipaddr adv_subnet adv_interface adv_valid_lifetime adv_preferred_lifetime
114         json_get_vars mtu ttl ipaddr adv_subnet adv_interface adv_valid_lifetime adv_preferred_lifetime
115
116         [ -z "$ipaddr" ] && {
117                 local wanif
118                 if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
119                         tun_error "$cfg" "NO_WAN_LINK"
120                         return
121                 fi
122         }
123
124         test_6to4_rfc1918 "$ipaddr" && {
125                 tun_error "$cfg" "INVALID_LOCAL_ADDRESS"
126                 return
127         }
128
129         # find our local prefix
130         local prefix6=$(find_6to4_prefix "$ipaddr")
131         local local6="$prefix6::1"
132
133         proto_init_update "$link" 1
134         proto_add_ipv6_address "$local6" 16
135         proto_add_ipv6_route "::" 0 "::192.88.99.1"
136
137         proto_add_tunnel
138         json_add_string mode sit
139         json_add_int mtu "${mtu:-1280}"
140         json_add_int ttl "${ttl:-64}"
141         json_add_string local "$ipaddr"
142         proto_close_tunnel
143
144         proto_send_update "$cfg"
145
146         [ -f /etc/config/radvd ] && /etc/init.d/radvd enabled && {
147                 local sid="6to4_$cfg"
148
149                 uci_revert_state radvd
150                 config_load radvd
151                 config_load network
152
153                 adv_subnet=$((0x${adv_subnet:-1}))
154
155                 local adv_subnets=""
156
157                 for adv_interface in ${adv_interface:-lan}; do
158                         local adv_ifname
159                         config_get adv_ifname "${adv_interface:-lan}" ifname
160
161                         grep -qs "^ *$adv_ifname:" /proc/net/dev && {
162                                 local subnet6="$(printf "%s:%x::1/64" "$prefix6" $adv_subnet)"
163
164                                 logger -t "$link" " * Advertising IPv6 subnet $subnet6 on ${adv_interface:-lan} ($adv_ifname)"
165                                 ip -6 addr add $subnet6 dev $adv_ifname
166
167                                 set_6to4_radvd_interface "$sid" "$adv_interface" "$mtu"
168                                 set_6to4_radvd_prefix    "$sid" "$adv_interface" \
169                                         "$wancfg" "$(printf "0:0:0:%x::/64" $adv_subnet)" \
170                                         "$adv_valid_lifetime" "$adv_preferred_lifetime"
171
172                                 adv_subnets="${adv_subnets:+$adv_subnets }$adv_ifname:$subnet6"
173                                 adv_subnet=$(($adv_subnet + 1))
174                         }
175                 done
176
177                 uci_set_state network "$cfg" adv_subnets "$adv_subnets"
178
179                 /etc/init.d/radvd restart
180         }
181 }
182
183 proto_6to4_teardown() {
184         local cfg="$1"
185         local link="6to4-$cfg"
186
187         local adv_subnets=$(uci_get_state network "$cfg" adv_subnets)
188
189         grep -qs "^ *$link:" /proc/net/dev && {
190                 [ -n "$adv_subnets" ] && {
191                         uci_revert_state radvd
192                         /etc/init.d/radvd enabled && /etc/init.d/radvd restart
193                 }
194         }
195 }
196
197 proto_6to4_init_config() {
198         no_device=1
199         available=1
200
201         proto_config_add_string "ipaddr"
202         proto_config_add_int "mtu"
203         proto_config_add_int "ttl"
204         proto_config_add_string "adv_interface"
205         proto_config_add_string "adv_subnet"
206         proto_config_add_int "adv_valid_lifetime"
207         proto_config_add_int "adv_preferred_lifetime"
208 }
209
210 [ -n "$INCLUDE_ONLY" ] || {
211         add_protocol 6to4
212 }