source: trunk/package/6in4/files.old/6in4.sh @ 31036

Last change on this file since 31036 was 31036, checked in by nbd, 5 years ago

6in4: add netifd support

  • Property svn:executable set to *
File size: 2.6 KB
Line 
1# 6in4.sh - IPv6-in-IPv4 tunnel backend
2# Copyright (c) 2010 OpenWrt.org
3
4find_6in4_wanif() {
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"
7}
8
9find_6in4_wanip() {
10        local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
11        echo "${ip%%[^0-9.]*}"
12}
13
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
17# the configuration.
18scan_6in4() {
19        config_set "$1" device "6in4-$1"
20}
21
22coldplug_interface_6in4() {
23        setup_interface_6in4 "6in4-$1" "$1"
24}
25
26setup_interface_6in4() {
27        local iface="$1"
28        local cfg="$2"
29        local link="6in4-$cfg"
30
31        local local4=$(uci_get network "$cfg" ipaddr)
32
33        local remote4
34        config_get remote4 "$cfg" peeraddr
35
36        local local6
37        config_get local6 "$cfg" ip6addr
38
39        local mtu
40        config_get mtu "$cfg" mtu
41
42        local ttl
43        config_get ttl "$cfg" ttl
44
45        local metric
46        config_get metric "$cfg" metric
47
48        local defaultroute
49        config_get_bool defaultroute "$cfg" defaultroute 1
50
51        # If local4 is unset, guess local IPv4 address from the
52        # interface used by the default route.
53        [ -z "$local4" ] && {
54                local wanif=$(find_6in4_wanif)
55                [ -n "$wanif" ] && {
56                        local4=$(find_6in4_wanip "$wanif")
57                        uci_set_state network "$cfg" wan_device "$wanif"
58                }
59        }
60
61        [ -n "$local4" ] && {
62                # creating the tunnel below will trigger a net subsystem event
63                # prevent it from touching or iface by disabling .auto here
64                uci_set_state network "$cfg" ifname $link
65                uci_set_state network "$cfg" auto 0
66
67                ip tunnel add $link mode sit remote $remote4 local $local4 ttl ${ttl:-64}
68                ip link set $link up
69                ip link set mtu ${mtu:-1280} dev $link
70                ip addr add $local6 dev $link
71
72                uci_set_state network "$cfg" ipaddr $local4
73                uci_set_state network "$cfg" ip6addr $local6
74
75                [ "$defaultroute" = 1 ] && {
76                        ip -6 route add ::/0 ${metric:+metric $metric} dev $link
77                        uci_set_state network "$cfg" defaultroute 1
78                }
79
80                env -i ACTION="ifup" INTERFACE="$cfg" DEVICE="$link" PROTO=6in4 /sbin/hotplug-call "iface" &
81        } || {
82                echo "Cannot determine local IPv4 address for 6in4 tunnel $cfg - skipping"
83        }
84}
85
86stop_interface_6in4() {
87        local cfg="$1"
88        local link="6in4-$cfg"
89
90        local local6=$(uci_get_state network "$cfg" ip6addr)
91        local defaultroute=$(uci_get_state network "$cfg" defaultroute)
92
93        grep -qs "^ *$link:" /proc/net/dev && {
94                env -i ACTION="ifdown" INTERFACE="$cfg" DEVICE="$link" PROTO=6in4 /sbin/hotplug-call "iface" &
95
96                [ "$defaultroute" = "1" ] && {
97                        ip -6 route del ::/0 dev $link
98                }
99
100                ip addr del $local6 dev $link
101                ip link set $link down
102                ip tunnel del $link
103        }
104}
Note: See TracBrowser for help on using the repository browser.