[package] 6to4: implement metric option
[openwrt.git] / package / 6to4 / files / 6to4.sh
1 # 6to4.sh - IPv6-in-IPv4 tunnel backend
2 # Copyright (c) 2010 OpenWrt.org
3
4 find_6to4_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
9 find_6to4_wanip() {
10 local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
11 echo "${ip%%[^0-9.]*}"
12 }
13
14 find_6to4_prefix() {
15 local ip4="$1"
16 local oIFS="$IFS"; IFS="."; set -- $ip4; IFS="$oIFS"
17
18 printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
19 }
20
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
24 # the configuration.
25 scan_6to4() {
26 config_set "$1" device "6to4-$1"
27 }
28
29 coldplug_interface_6to4() {
30 setup_interface_6to4 "6to4-$1" "$1"
31 }
32
33 setup_interface_6to4() {
34 local iface="$1"
35 local cfg="$2"
36 local link="6to4-$cfg"
37
38 local local4=$(uci_get network "$cfg" ipaddr)
39
40 local mtu
41 config_get mtu "$cfg" mtu
42
43 local ttl
44 config_get ttl "$cfg" ttl
45
46 local metric
47 config_get metric "$cfg" metric
48
49 local defaultroute
50 config_get_bool defaultroute "$cfg" defaultroute 1
51
52 local wanif=$(find_6to4_wanif)
53 [ -z "$wanif" ] && {
54 logger -t "$link" "Cannot find wan interface - aborting"
55 return
56 }
57
58 local wancfg=$(find_config "$wanif")
59 [ -z "$wancfg" ] && {
60 logger -t "$link" "Cannot find wan network - aborting"
61 return
62 }
63
64 # If local4 is unset, guess local IPv4 address from the
65 # interface used by the default route.
66 [ -z "$local4" ] && {
67 [ -n "$wanif" ] && {
68 local4=$(find_6to4_wanip "$wanif")
69 uci_set_state network "$cfg" wan_device "$wanif"
70 }
71 }
72
73 [ -n "$local4" ] && {
74 logger -t "$link" "Starting ..."
75
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
80
81 # find our local prefix
82 local prefix6=$(find_6to4_prefix "$local4")
83 local local6="$prefix6::1/16"
84
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}
88 ip link set $link up
89 ip link set mtu ${mtu:-1280} dev $link
90 ip addr add $local6 dev $link
91
92 uci_set_state network "$cfg" ipaddr $local4
93 uci_set_state network "$cfg" ip6addr $local6
94
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
99 }
100
101 # find delegation target
102 local adv_interface
103 config_get adv_interface "$cfg" adv_interface
104
105 local adv_ifname
106 config_get adv_ifname "${adv_interface:-lan}" ifname
107
108 local adv_subnet=$(uci_get network "$cfg" adv_subnet)
109
110 grep -qs "^ *$adv_ifname:" /proc/net/dev && {
111 local subnet6="$prefix6:${adv_subnet:-1}::1/64"
112
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"
117
118 [ -f /etc/config/radvd ] && /etc/init.d/radvd enabled && {
119 local sid="6to4_$cfg"
120
121 uci -q batch <<-EOF
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
139 EOF
140
141 /etc/init.d/radvd restart
142 }
143 }
144
145 logger -t "$link" "... started"
146
147 env -i ACTION="ifup" INTERFACE="$cfg" DEVICE="$link" PROTO=6to4 /sbin/hotplug-call "iface" &
148 } || {
149 echo "Cannot determine local IPv4 address for 6to4 tunnel $cfg - skipping"
150 }
151 }
152
153 stop_interface_6to4() {
154 local cfg="$1"
155 local link="6to4-$cfg"
156
157 local local6=$(uci_get_state network "$cfg" ip6addr)
158 local defaultroute=$(uci_get_state network "$cfg" defaultroute)
159
160 local adv_subnet=$(uci_get_state network "$cfg" adv_subnet)
161 local adv_ifname=$(uci_get_state network "$cfg" adv_ifname)
162
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" &
166
167 [ -n "$adv_subnet" ] && [ -n "$adv_ifname" ] && {
168 local sid="6to4_$cfg"
169
170 uci -q batch <<-EOF
171 revert radvd.iface_$sid
172 revert radvd.prefix_$sid
173 EOF
174
175 /etc/init.d/radvd enabled && /etc/init.d/radvd restart
176
177 ip -6 addr del $adv_subnet dev $adv_ifname
178 }
179
180 [ "$defaultroute" = "1" ] && {
181 ip -6 route del 2000::/3 via ::192.88.99.1 dev $link metric 1
182 }
183
184 ip addr del $local6 dev $link
185 ip link set $link down
186 ip tunnel del $link
187
188 logger -t "$link" "... stopped"
189 }
190 }
This page took 0.05331 seconds and 5 git commands to generate.