1 # 6to4.sh - IPv6-in-IPv4 tunnel backend
2 # Copyright (c) 2010-2011 OpenWrt.org
5 local if=$(ip -4 r l e 0.0.0.0/0); if="${if#default* dev }"; if="${if%% *}"
6 [ -n "$if" ] && grep -qs "^ *$if:" /proc/net/dev && echo "$if"
10 local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
11 echo "${ip%%[^0-9.]*}"
16 local oIFS="$IFS"; IFS="."; set -- $ip4; IFS="$oIFS"
18 printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
23 local oIFS="$IFS"; IFS="."; set -- $1; IFS="$oIFS"
24 [ $1 -eq 10 ] && return 0
25 [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 0
26 [ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 0
30 set_6to4_radvd_interface() {
32 local lanif="${2:-lan}"
33 local ifmtu="${3:-1280}"
39 config_get net "$cfg" interface
41 [ "$net" = "$lanif" ] && {
47 config_foreach find_ifsection interface
49 [ -z "$ifsection" ] && {
50 ifsection="iface_$sid"
51 uci_set_state radvd "$ifsection" "" interface
52 uci_set_state radvd "$ifsection" interface "$lanif"
55 uci_set_state radvd "$ifsection" ignore 0
56 uci_set_state radvd "$ifsection" IgnoreIfMissing 1
57 uci_set_state radvd "$ifsection" AdvSendAdvert 1
58 uci_set_state radvd "$ifsection" MaxRtrAdvInterval 30
59 uci_set_state radvd "$ifsection" AdvLinkMTU "$ifmtu"
62 set_6to4_radvd_prefix() {
64 local lanif="${2:-lan}"
65 local wanif="${3:-wan}"
66 local prefix="${4:-0:0:0:1::/64}"
72 config_get net "$cfg" interface
73 config_get base "$cfg" Base6to4Interface
75 [ "$net" = "$lanif" ] && [ "$base" = "$wanif" ] && {
81 config_foreach find_pfxsection prefix
83 [ -z "$pfxsection" ] && {
84 pfxsection="prefix_${sid}_${lanif}"
85 uci_set_state radvd "$pfxsection" "" prefix
86 uci_set_state radvd "$pfxsection" ignore 0
87 uci_set_state radvd "$pfxsection" interface "$lanif"
88 uci_set_state radvd "$pfxsection" prefix "$prefix"
89 uci_set_state radvd "$pfxsection" AdvOnLink 1
90 uci_set_state radvd "$pfxsection" AdvAutonomous 1
91 uci_set_state radvd "$pfxsection" AdvValidLifetime 300
92 uci_set_state radvd "$pfxsection" AdvPreferredLifetime 120
93 uci_set_state radvd "$pfxsection" Base6to4Interface "$wanif"
98 # Hook into scan_interfaces() to synthesize a .device option
99 # This is needed for /sbin/ifup to properly dispatch control
100 # to setup_interface_6to4() even if no .ifname is set in
103 config_set "$1" device "6to4-$1"
106 coldplug_interface_6to4() {
107 setup_interface_6to4 "6to4-$1" "$1"
110 setup_interface_6to4() {
113 local link="6to4-$cfg"
115 local local4=$(uci_get network "$cfg" ipaddr)
118 config_get mtu "$cfg" mtu
121 config_get ttl "$cfg" ttl
124 config_get metric "$cfg" metric
127 config_get_bool defaultroute "$cfg" defaultroute 1
129 local wanif=$(find_6to4_wanif)
131 logger -t "$link" "Cannot find wan interface - aborting"
135 local wancfg=$(find_config "$wanif")
136 [ -z "$wancfg" ] && {
137 logger -t "$link" "Cannot find wan network - aborting"
141 # If local4 is unset, guess local IPv4 address from the
142 # interface used by the default route.
143 [ -z "$local4" ] && {
145 local4=$(find_6to4_wanip "$wanif")
146 uci_set_state network "$cfg" wan_device "$wanif"
150 test_6to4_rfc1918 "$local4" && {
151 logger -t "$link" "Local wan ip $local4 is private - aborting"
155 [ -n "$local4" ] && {
156 logger -t "$link" "Starting ..."
158 # creating the tunnel below will trigger a net subsystem event
159 # prevent it from touching or iface by disabling .auto here
160 uci_set_state network "$cfg" ifname $link
161 uci_set_state network "$cfg" auto 0
163 # find our local prefix
164 local prefix6=$(find_6to4_prefix "$local4")
165 local local6="$prefix6::1/16"
167 logger -t "$link" " * IPv4 address is $local4"
168 logger -t "$link" " * IPv6 address is $local6"
169 ip tunnel add $link mode sit remote any local $local4 ttl ${ttl:-64}
171 ip link set mtu ${mtu:-1280} dev $link
172 ip addr add $local6 dev $link
174 uci_set_state network "$cfg" ipaddr $local4
175 uci_set_state network "$cfg" ip6addr $local6
177 [ "$defaultroute" = 1 ] && {
178 logger -t "$link" " * Adding default route"
179 ip -6 route add ::/0 via ::192.88.99.1 metric ${metric:-1} dev $link
180 uci_set_state network "$cfg" defaultroute 1
183 [ -f /etc/config/radvd ] && /etc/init.d/radvd enabled && {
184 local sid="6to4_$cfg"
186 uci_revert_state radvd
189 # find delegation target
191 config_get adv_interface "$cfg" adv_interface
193 local adv_subnet=$(uci_get network "$cfg" adv_subnet)
194 adv_subnet=$((0x${adv_subnet:-1}))
198 for adv_interface in ${adv_interface:-lan}; do
200 config_get adv_ifname "${adv_interface:-lan}" ifname
202 grep -qs "^ *$adv_ifname:" /proc/net/dev && {
203 local subnet6="$(printf "%s:%x::1/64" "$prefix6" $adv_subnet)"
205 logger -t "$link" " * Advertising IPv6 subnet $subnet6 on ${adv_interface:-lan} ($adv_ifname)"
206 ip -6 addr add $subnet6 dev $adv_ifname
208 set_6to4_radvd_interface "$sid" "$adv_interface" "$mtu"
209 set_6to4_radvd_prefix "$sid" "$adv_interface" \
210 "$wancfg" "$(printf "0:0:0:%x::/64" $adv_subnet)"
212 adv_subnets="${adv_subnets:+$adv_subnets }$adv_ifname:$subnet6"
213 adv_subnet=$(($adv_subnet + 1))
217 uci_set_state network "$cfg" adv_subnets "$adv_subnets"
219 /etc/init.d/radvd restart
222 logger -t "$link" "... started"
224 env -i ACTION="ifup" INTERFACE="$cfg" DEVICE="$link" PROTO=6to4 /sbin/hotplug-call "iface" &
226 echo "Cannot determine local IPv4 address for 6to4 tunnel $cfg - skipping"
230 stop_interface_6to4() {
232 local link="6to4-$cfg"
234 local local6=$(uci_get_state network "$cfg" ip6addr)
235 local defaultroute=$(uci_get_state network "$cfg" defaultroute)
237 local adv_subnets=$(uci_get_state network "$cfg" adv_subnets)
239 grep -qs "^ *$link:" /proc/net/dev && {
240 logger -t "$link" "Shutting down ..."
241 env -i ACTION="ifdown" INTERFACE="$cfg" DEVICE="$link" PROTO=6to4 /sbin/hotplug-call "iface" &
243 [ -n "$adv_subnets" ] && {
244 uci_revert_state radvd
245 /etc/init.d/radvd enabled && /etc/init.d/radvd restart
248 for adv_subnet in $adv_subnets; do
249 local ifname="${adv_subnet%%:*}"
250 local subnet="${adv_subnet#*:}"
252 logger -t "$link" " * Removing IPv6 subnet $subnet from interface $ifname"
253 ip -6 addr del $subnet dev $ifname
257 [ "$defaultroute" = "1" ] && \
258 ip -6 route del ::/0 via ::192.88.99.1 dev $link
260 ip addr del $local6 dev $link
261 ip link set $link down
264 logger -t "$link" "... stopped"