add 6in4 - support for static IPv6-in-IPv4 tunnels through /etc/config/network
[librecmc/librecmc.git] / package / 6in4 / files / 6in4.sh
1 # 6in4.sh - IPv6-in-IPv4 tunnel backend
2 # Copyright (c) 2010 OpenWrt.org
3
4 # Hook into scan_interfaces() to synthesize a .device option
5 # This is needed for /sbin/ifup to properly dispatch control
6 # to setup_interface_6in4() even if no .ifname is set in
7 # the configuration.
8 scan_6in4() {
9         config_set "$1" device "6in4-$1"
10 }
11
12 coldplug_interface_6in4() {
13         setup_interface_6in4 "6in4-$1" "$1"
14 }
15
16 setup_interface_6in4() {
17         local iface="$1"
18         local cfg="$2"
19         local link="6in4-$cfg"
20
21         local local4
22         config_get local4 "$cfg" ipaddr
23
24         local remote4
25         config_get remote4 "$cfg" peeraddr
26
27         local local6
28         config_get local6 "$cfg" ip6addr
29
30         local mtu
31         config_get mtu "$cfg" mtu
32
33         local ttl
34         config_get ttl "$cfg" ttl
35
36         local defaultroute
37         config_get_bool defaultroute "$cfg" defaultroute 1
38
39
40         # creating the tunnel below will trigger a net subsystem event
41         # prevent it from touching or iface by disabling .auto here
42         uci_set_state network "$cfg" ifname $link
43         uci_set_state network "$cfg" auto 0
44
45         ip tunnel add $link mode sit remote $remote4 local $local4 ttl 255
46         ip link set $link up
47         ip link set mtu ${mtu:-1280} dev $link
48         ip tunnel change $link ttl ${ttl:-64}
49         ip addr add $local6 dev $link
50
51         uci_set_state network "$cfg" ip6addr $local6
52
53         [ "$defaultroute" = 1 ] && {
54                 ip -6 route add ::/0 dev $link
55                 uci_set_state network "$cfg" defaultroute 1
56         }
57
58         env -i ACTION="ifup" INTERFACE="$cfg" DEVICE="$link" PROTO=6in4 /sbin/hotplug-call "iface" &
59 }
60
61 stop_interface_6in4() {
62         local cfg="$1"
63         local link="6in4-$cfg"
64
65         local local6=$(uci_get_state network "$cfg" ip6addr)
66         local defaultroute=$(uci_get_state network "$cfg" defaultroute)
67
68         grep -qs "^ *$link:" /proc/net/dev && {
69                 env -i ACTION="ifdown" INTERFACE="$cfg" DEVICE="$link" PROTO=6in4 /sbin/hotplug-call "iface" &
70
71                 [ "$defaultroute" = "1" ] && {
72                         ip -6 route del ::/0 dev $link
73                 }
74
75                 ip addr del $local6 dev $link
76                 ip link set $link down
77                 ip tunnel del $link
78         }
79 }