1 # 6to4.sh - IPv6-in-IPv4 tunnel backend
2 # Copyright (c) 2010 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
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_6to4() even if no .ifname is set in
26 config_set
"$1" device
"6to4-$1"
29 coldplug_interface_6to4
() {
30 setup_interface_6to4
"6to4-$1" "$1"
33 setup_interface_6to4
() {
36 local link
="6to4-$cfg"
38 local local4
=$
(uci_get network
"$cfg" ipaddr
)
41 config_get mtu
"$cfg" mtu
44 config_get ttl
"$cfg" ttl
47 config_get metric
"$cfg" metric
50 config_get_bool defaultroute
"$cfg" defaultroute
1
52 local wanif
=$
(find_6to4_wanif
)
54 logger
-t "$link" "Cannot find wan interface - aborting"
58 local wancfg
=$
(find_config
"$wanif")
60 logger
-t "$link" "Cannot find wan network - aborting"
64 # If local4 is unset, guess local IPv4 address from the
65 # interface used by the default route.
68 local4
=$
(find_6to4_wanip
"$wanif")
69 uci_set_state network
"$cfg" wan_device
"$wanif"
74 logger
-t "$link" "Starting ..."
76 # creating the tunnel below will trigger a net subsystem event
77 # prevent it from touching or iface by disabling .auto here
78 uci_set_state network
"$cfg" ifname
$link
79 uci_set_state network
"$cfg" auto
0
81 # find our local prefix
82 local prefix6
=$
(find_6to4_prefix
"$local4")
83 local local6
="$prefix6::1/16"
85 logger
-t "$link" " * IPv4 address is $local4"
86 logger
-t "$link" " * IPv6 address is $local6"
87 ip tunnel add
$link mode sit remote any
local $local4 ttl
${ttl:-64}
89 ip link
set mtu
${mtu:-1280} dev
$link
90 ip addr add
$local6 dev
$link
92 uci_set_state network
"$cfg" ipaddr
$local4
93 uci_set_state network
"$cfg" ip6addr
$local6
95 [ "$defaultroute" = 1 ] && {
96 logger
-t "$link" " * Adding default route"
97 ip
-6 route add
2000::/3 via
::192.88.99.1 metric
${metric:-1} dev
$link
98 uci_set_state network
"$cfg" defaultroute
1
101 # find delegation target
103 config_get adv_interface
"$cfg" adv_interface
106 config_get adv_ifname
"${adv_interface:-lan}" ifname
108 local adv_subnet
=$
(uci_get network
"$cfg" adv_subnet
)
110 grep -qs "^ *$adv_ifname:" /proc
/net
/dev
&& {
111 local subnet6
="$prefix6:${adv_subnet:-1}::1/64"
113 logger
-t "$link" " * Advertising IPv6 subnet $subnet6 on ${adv_interface:-lan} ($adv_ifname)"
114 ip
-6 addr add
$subnet6 dev
$adv_ifname
115 uci_set_state network
"$cfg" adv_subnet
"$subnet6"
116 uci_set_state network
"$cfg" adv_ifname
"$adv_ifname"
118 [ -f /etc
/config
/radvd
] && /etc
/init.d
/radvd enabled
&& {
119 local sid
="6to4_$cfg"
122 revert radvd.iface_$sid
123 revert radvd.prefix_$sid
124 set radvd.iface_$sid=interface
125 set radvd.iface_$sid.ignore=0
126 set radvd.iface_$sid.interface=${adv_interface:-lan}
127 set radvd.iface_$sid.IgnoreIfMissing=1
128 set radvd.iface_$sid.AdvSendAdvert=1
129 set radvd.iface_$sid.MaxRtrAdvInterval=30
130 set radvd.prefix_$sid=prefix
131 set radvd.prefix_$sid.ignore=0
132 set radvd.prefix_$sid.interface=${adv_interface:-lan}
133 set radvd.prefix_$sid.prefix=0:0:0:${adv_subnet:-1}::/64
134 set radvd.prefix_$sid.AdvOnLink=1
135 set radvd.prefix_$sid.AdvAutonomous=1
136 set radvd.prefix_$sid.AdvValidLifetime=300
137 set radvd.prefix_$sid.AdvPreferredLifetime=120
138 set radvd.prefix_$sid.Base6to4Interface=$wancfg
141 /etc
/init.d
/radvd restart
145 logger
-t "$link" "... started"
147 env
-i ACTION
="ifup" INTERFACE
="$cfg" DEVICE
="$link" PROTO
=6to4
/sbin
/hotplug-call
"iface" &
149 echo "Cannot determine local IPv4 address for 6to4 tunnel $cfg - skipping"
153 stop_interface_6to4
() {
155 local link
="6to4-$cfg"
157 local local6
=$
(uci_get_state network
"$cfg" ip6addr
)
158 local defaultroute
=$
(uci_get_state network
"$cfg" defaultroute
)
160 local adv_subnet
=$
(uci_get_state network
"$cfg" adv_subnet
)
161 local adv_ifname
=$
(uci_get_state network
"$cfg" adv_ifname
)
163 grep -qs "^ *$link:" /proc
/net
/dev
&& {
164 logger
-t "$link" "Shutting down ..."
165 env
-i ACTION
="ifdown" INTERFACE
="$cfg" DEVICE
="$link" PROTO
=6to4
/sbin
/hotplug-call
"iface" &
167 [ -n "$adv_subnet" ] && [ -n "$adv_ifname" ] && {
168 local sid
="6to4_$cfg"
171 revert radvd.iface_$sid
172 revert radvd.prefix_$sid
175 /etc
/init.d
/radvd enabled
&& /etc
/init.d
/radvd restart
177 ip
-6 addr del
$adv_subnet dev
$adv_ifname
180 [ "$defaultroute" = "1" ] && {
181 ip
-6 route del
2000::/3 via
::192.88.99.1 dev
$link metric
1
184 ip addr del
$local6 dev
$link
185 ip link
set $link down
188 logger
-t "$link" "... stopped"