2 # 6to4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2012 OpenWrt.org
5 [ -n "$INCLUDE_ONLY" ] || {
7 . /lib/functions/network.sh
14 local oIFS="$IFS"; IFS="."; set -- $ip4; IFS="$oIFS"
16 printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
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
27 [ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 0
32 set_6to4_radvd_interface() {
34 local lanif="${2:-lan}"
35 local ifmtu="${3:-1280}"
41 config_get net "$cfg" interface
43 [ "$net" = "$lanif" ] && {
49 config_foreach find_ifsection interface
51 [ -z "$ifsection" ] && {
52 ifsection="iface_$sid"
53 uci_set_state radvd "$ifsection" "" interface
54 uci_set_state radvd "$ifsection" interface "$lanif"
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"
64 set_6to4_radvd_prefix() {
66 local lanif="${2:-lan}"
67 local wanif="${3:-wan}"
68 local prefix="${4:-0:0:0:1::/64}"
76 config_get net "$cfg" interface
77 config_get base "$cfg" Base6to4Interface
79 [ "$net" = "$lanif" ] && [ "$base" = "$wanif" ] && {
85 config_foreach find_pfxsection prefix
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"
104 local link="6to4-$cfg"
106 local mtu ttl ipaddr adv_subnet adv_interface adv_valid_lifetime adv_preferred_lifetime
107 json_get_vars mtu ttl ipaddr adv_subnet adv_interface adv_valid_lifetime adv_preferred_lifetime
109 ( proto_add_host_dependency "$cfg" 0.0.0.0 )
112 if ! network_find_wan wanif; then
113 proto_notify_error "$cfg" "NO_WAN_LINK"
117 [ -z "$ipaddr" ] && {
118 if ! network_get_ipaddr ipaddr "$wanif"; then
119 proto_notify_error "$cfg" "NO_WAN_ADDRESS"
124 test_6to4_rfc1918 "$ipaddr" && {
125 proto_notify_error "$cfg" "INVALID_LOCAL_ADDRESS"
129 # find our local prefix
130 local prefix6=$(find_6to4_prefix "$ipaddr")
131 local local6="$prefix6::1"
133 proto_init_update "$link" 1
134 proto_add_ipv6_address "$local6" 16
135 proto_add_ipv6_route "::" 0 "::192.88.99.1"
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"
144 proto_send_update "$cfg"
146 [ -f /etc/config/radvd ] && /etc/init.d/radvd enabled && {
147 local sid="6to4_$cfg"
149 uci_revert_state radvd
152 adv_subnet=$((0x${adv_subnet:-1}))
156 for adv_interface in ${adv_interface:-lan}; do
158 network_get_device adv_ifname "${adv_interface:-lan}" || continue
160 local subnet6="$(printf "%s:%x::1/64" "$prefix6" $adv_subnet)"
162 logger -t "$link" " * Advertising IPv6 subnet $subnet6 on ${adv_interface:-lan} ($adv_ifname)"
163 ip -6 addr add $subnet6 dev $adv_ifname
165 set_6to4_radvd_interface "$sid" "$adv_interface" "$mtu"
166 set_6to4_radvd_prefix "$sid" "$adv_interface" \
167 "$wanif" "$(printf "0:0:0:%x::/64" $adv_subnet)" \
168 "$adv_valid_lifetime" "$adv_preferred_lifetime"
170 adv_subnets="${adv_subnets:+$adv_subnets }$adv_ifname:$subnet6"
171 adv_subnet=$(($adv_subnet + 1))
174 uci_set_state network "$cfg" adv_subnets "$adv_subnets"
176 /etc/init.d/radvd restart
180 proto_6to4_teardown() {
182 local link="6to4-$cfg"
184 local adv_subnets=$(uci_get_state network "$cfg" adv_subnets)
186 grep -qs "^ *$link:" /proc/net/dev && {
187 [ -n "$adv_subnets" ] && {
188 uci_revert_state radvd
189 /etc/init.d/radvd enabled && /etc/init.d/radvd restart
194 proto_6to4_init_config() {
198 proto_config_add_string "ipaddr"
199 proto_config_add_int "mtu"
200 proto_config_add_int "ttl"
201 proto_config_add_string "adv_interface"
202 proto_config_add_string "adv_subnet"
203 proto_config_add_int "adv_valid_lifetime"
204 proto_config_add_int "adv_preferred_lifetime"
207 [ -n "$INCLUDE_ONLY" ] || {