1 # 6in4.sh - IPv6-in-IPv4 tunnel backend
2 # Copyright (c) 2010 OpenWrt.org
5 local if=$(ip -4 r l e 0/0); if="${if#default via * 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]* brd *}"
14 # Hook into scan_interfaces() to synthesize a .device option
15 # This is needed for /sbin/ifup to properly dispatch control
16 # to setup_interface_6in4() even if no .ifname is set in
19 config_set "$1" device "6in4-$1"
22 coldplug_interface_6in4() {
23 setup_interface_6in4 "6in4-$1" "$1"
26 setup_interface_6in4() {
29 local link="6in4-$cfg"
32 config_get local4 "$cfg" ipaddr
35 config_get remote4 "$cfg" peeraddr
38 config_get local6 "$cfg" ip6addr
41 config_get mtu "$cfg" mtu
44 config_get ttl "$cfg" ttl
47 config_get_bool defaultroute "$cfg" defaultroute 1
49 # If local4 is unset, guess local IPv4 address from the
50 # interface used by the default route.
52 local wanif=$(find_6in4_wanif)
54 local4=$(find_6in4_wanip "$wanif")
55 uci_set_state network "$cfg" wan_device "$wanif"
60 # creating the tunnel below will trigger a net subsystem event
61 # prevent it from touching or iface by disabling .auto here
62 uci_set_state network "$cfg" ifname $link
63 uci_set_state network "$cfg" auto 0
65 ip tunnel add $link mode sit remote $remote4 local $local4 ttl 255
67 ip link set mtu ${mtu:-1280} dev $link
68 ip tunnel change $link ttl ${ttl:-64}
69 ip addr add $local6 dev $link
71 uci_set_state network "$cfg" ipaddr $local4
72 uci_set_state network "$cfg" ip6addr $local6
74 [ "$defaultroute" = 1 ] && {
75 ip -6 route add ::/0 dev $link
76 uci_set_state network "$cfg" defaultroute 1
79 env -i ACTION="ifup" INTERFACE="$cfg" DEVICE="$link" PROTO=6in4 /sbin/hotplug-call "iface" &
81 echo "Cannot determine local IPv4 address for 6in4 tunnel $cfg - skipping"
85 stop_interface_6in4() {
87 local link="6in4-$cfg"
89 local local6=$(uci_get_state network "$cfg" ip6addr)
90 local defaultroute=$(uci_get_state network "$cfg" defaultroute)
92 grep -qs "^ *$link:" /proc/net/dev && {
93 env -i ACTION="ifdown" INTERFACE="$cfg" DEVICE="$link" PROTO=6in4 /sbin/hotplug-call "iface" &
95 [ "$defaultroute" = "1" ] && {
96 ip -6 route del ::/0 dev $link
99 ip addr del $local6 dev $link
100 ip link set $link down