base-files-network: configure vlan interface name type immediately before the vconfig...
[openwrt/staging/chunkeey.git] / package / base-files-network / files / lib / network / config.sh
1 #!/bin/sh
2 # Copyright (C) 2006-2011 OpenWrt.org
3
4 # DEBUG="echo"
5
6 do_sysctl() {
7 [ -n "$2" ] && \
8 sysctl -n -e -w "$1=$2" >/dev/null || \
9 sysctl -n -e "$1"
10 }
11
12 map_sysctls() {
13 local cfg="$1"
14 local ifn="$2"
15
16 local fam
17 for fam in ipv4 ipv6; do
18 if [ -d /proc/sys/net/$fam ]; then
19 local key
20 for key in /proc/sys/net/$fam/*/$ifn/*; do
21 local val
22 config_get val "$cfg" "${fam}_${key##*/}"
23 [ -n "$val" ] && echo -n "$val" > "$key"
24 done
25 fi
26 done
27 }
28
29 find_config() {
30 local iftype device iface ifaces ifn
31 for ifn in $interfaces; do
32 config_get iftype "$ifn" type
33 config_get iface "$ifn" ifname
34 case "$iftype" in
35 bridge) config_get ifaces "$ifn" ifnames;;
36 esac
37 config_get device "$ifn" device
38 for ifc in $device $iface $ifaces; do
39 [ ."$ifc" = ."$1" ] && {
40 echo "$ifn"
41 return 0
42 }
43 done
44 done
45
46 return 1;
47 }
48
49 scan_interfaces() {
50 local cfgfile="${1:-network}"
51 interfaces=
52 config_cb() {
53 case "$1" in
54 interface)
55 config_set "$2" auto 1
56 ;;
57 esac
58 local iftype ifname device proto
59 config_get iftype "$CONFIG_SECTION" TYPE
60 case "$iftype" in
61 interface)
62 append interfaces "$CONFIG_SECTION"
63 config_get proto "$CONFIG_SECTION" proto
64 config_get iftype "$CONFIG_SECTION" type
65 config_get ifname "$CONFIG_SECTION" ifname
66 config_get device "$CONFIG_SECTION" device "$ifname"
67 config_set "$CONFIG_SECTION" device "$device"
68 case "$iftype" in
69 bridge)
70 config_set "$CONFIG_SECTION" ifnames "$device"
71 config_set "$CONFIG_SECTION" ifname br-"$CONFIG_SECTION"
72 ;;
73 esac
74 ( type "scan_$proto" ) >/dev/null 2>/dev/null && eval "scan_$proto '$CONFIG_SECTION'"
75 ;;
76 esac
77 }
78 config_load "${cfgfile}"
79 }
80
81 add_vlan() {
82 local vif="${1%\.*}"
83
84 [ "$1" = "$vif" ] || ifconfig "$1" >/dev/null 2>/dev/null || {
85 ifconfig "$vif" up 2>/dev/null >/dev/null || add_vlan "$vif"
86 $DEBUG vconfig set_name_type DEV_PLUS_VID_NO_PAD
87 $DEBUG vconfig add "$vif" "${1##*\.}"
88 return 0
89 }
90 return 1
91 }
92
93 # add dns entries if they are not in resolv.conf yet
94 add_dns() {
95 local cfg="$1"; shift
96
97 remove_dns "$cfg"
98
99 # We may be called by pppd's ip-up which has a nonstandard umask set.
100 # Create an empty file here and force its permission to 0644, otherwise
101 # dnsmasq will not be able to re-read the resolv.conf.auto .
102 [ ! -f /tmp/resolv.conf.auto ] && {
103 touch /tmp/resolv.conf.auto
104 chmod 0644 /tmp/resolv.conf.auto
105 }
106
107 local dns
108 local add
109 for dns in "$@"; do
110 grep -qsE "^nameserver ${dns//./\\.}$" /tmp/resolv.conf.auto || {
111 add="${add:+$add }$dns"
112 echo "nameserver $dns" >> /tmp/resolv.conf.auto
113 }
114 done
115
116 [ -n "$cfg" ] && {
117 uci_toggle_state network "$cfg" dns "$add"
118 uci_toggle_state network "$cfg" resolv_dns "$add"
119 }
120 }
121
122 # remove dns entries of the given iface
123 remove_dns() {
124 local cfg="$1"
125
126 [ -n "$cfg" ] && {
127 [ -f /tmp/resolv.conf.auto ] && {
128 local dns=$(uci_get_state network "$cfg" resolv_dns)
129 for dns in $dns; do
130 sed -i -e "/^nameserver ${dns//./\\.}$/d" /tmp/resolv.conf.auto
131 done
132 }
133
134 uci_revert_state network "$cfg" dns
135 uci_revert_state network "$cfg" resolv_dns
136 }
137 }
138
139 # sort the device list, drop duplicates
140 sort_list() {
141 local arg="$*"
142 (
143 for item in $arg; do
144 echo "$item"
145 done
146 ) | sort -u
147 }
148
149 prepare_interface_bridge() {
150 return 0
151 }
152
153 # Create the interface, if necessary.
154 # Return status 0 indicates that the setup_interface() call should continue
155 # Return status 1 means that everything is set up already.
156
157 prepare_interface() {
158 local iface="$1"
159 local config="$2"
160 local macaddr="$3"
161
162 # if we're called for the bridge interface itself, don't bother trying
163 # to create any interfaces here. The scripts have already done that, otherwise
164 # the bridge interface wouldn't exist.
165 [ "br-$config" = "$iface" -o -e "$iface" ] && return 0;
166
167 ifconfig "$iface" 2>/dev/null >/dev/null && {
168 local proto
169 config_get proto "$config" proto
170
171 # make sure the interface is removed from any existing bridge and deconfigured,
172 # (deconfigured only if the interface is not set to proto=none)
173 unbridge "$iface"
174
175 local mtu macaddr txqueuelen
176 config_get mtu "$config" mtu
177 [ -n "$macaddr" ] || config_get macaddr "$config" macaddr
178 config_get txqueuelen "$config" txqueuelen
179 [ -n "$macaddr" ] && $DEBUG ifconfig "$iface" down
180 $DEBUG ifconfig "$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} ${txqueuelen:+txqueuelen $txqueuelen} up
181
182 [ "$proto" = none ] || ifconfig "$iface" 0.0.0.0
183
184 # Apply sysctl settings
185 map_sysctls "$config" "$iface"
186 }
187
188 # Setup VLAN interfaces
189 add_vlan "$iface" && return 1
190 ifconfig "$iface" 2>/dev/null >/dev/null || return 0
191
192 # Setup bridging
193 local iftype
194 config_get iftype "$config" type
195 case "$iftype" in
196 bridge)
197 local macaddr
198 config_get macaddr "$config" macaddr
199 [ -x /usr/sbin/brctl ] && {
200 ifconfig "br-$config" 2>/dev/null >/dev/null && {
201 local newdevs devices
202 config_get devices "$config" device
203 for dev in $(sort_list "$devices" "$iface"); do
204 append newdevs "$dev"
205 done
206 uci_toggle_state network "$config" device "$newdevs"
207 $DEBUG ifconfig "$iface" 0.0.0.0
208 $DEBUG do_sysctl "net.ipv6.conf.$iface.disable_ipv6" 1
209 $DEBUG brctl addif "br-$config" "$iface"
210 # Bridge existed already. No further processing necesary
211 } || {
212 local stp igmp_snooping
213 config_get_bool stp "$config" stp 0
214 config_get_bool igmp_snooping "$config" igmp_snooping 1
215 $DEBUG brctl addbr "br-$config"
216 $DEBUG brctl setfd "br-$config" 0
217 $DEBUG ifconfig "$iface" 0.0.0.0
218 $DEBUG do_sysctl "net.ipv6.conf.$iface.disable_ipv6" 1
219 $DEBUG brctl addif "br-$config" "$iface"
220 $DEBUG brctl stp "br-$config" $stp
221 [ -z "$macaddr" ] && macaddr="$(cat /sys/class/net/$iface/address)"
222 [ -e /sys/devices/virtual/net/br-$config/bridge/multicast_snooping ] && \
223 echo $igmp_snooping > /sys/devices/virtual/net/br-$config/bridge/multicast_snooping
224 $DEBUG ifconfig "br-$config" hw ether $macaddr up
225 # Creating the bridge here will have triggered a hotplug event, which will
226 # result in another setup_interface() call, so we simply stop processing
227 # the current event at this point.
228 }
229 ifconfig "$iface" ${macaddr:+hw ether "${macaddr}"} up 2>/dev/null >/dev/null
230 return 1
231 }
232 ;;
233 esac
234 return 0
235 }
236
237 set_interface_ifname() {
238 local config="$1"
239 local ifname="$2"
240
241 local device
242 config_get device "$1" device
243 uci_toggle_state network "$config" ifname "$ifname"
244 uci_toggle_state network "$config" device "$device"
245 }
246
247 setup_interface_none() {
248 env -i ACTION="ifup" INTERFACE="$2" DEVICE="$1" PROTO=none /sbin/hotplug-call "iface" &
249 }
250
251 setup_interface_static() {
252 local iface="$1"
253 local config="$2"
254
255 local ipaddr netmask ip6addr
256 config_get ipaddr "$config" ipaddr
257 config_get netmask "$config" netmask
258 config_get ip6addr "$config" ip6addr
259 [ -z "$ipaddr" -o -z "$netmask" ] && [ -z "$ip6addr" ] && return 1
260
261 local gateway ip6gw dns bcast metric
262 config_get gateway "$config" gateway
263 config_get ip6gw "$config" ip6gw
264 config_get dns "$config" dns
265 config_get bcast "$config" broadcast
266 config_get metric "$config" metric
267
268 case "$ip6addr" in
269 */*) ;;
270 *:*) ip6addr="$ip6addr/64" ;;
271 esac
272
273 [ -z "$ipaddr" ] || $DEBUG ifconfig "$iface" "$ipaddr" netmask "$netmask" broadcast "${bcast:-+}"
274 [ -z "$ip6addr" ] || $DEBUG ifconfig "${iface%:*}" add "$ip6addr"
275 [ -z "$gateway" ] || $DEBUG route add default gw "$gateway" ${metric:+metric $metric} dev "$iface"
276 [ -z "$ip6gw" ] || $DEBUG route -A inet6 add default gw "$ip6gw" ${metric:+metric $metric} dev "${iface%:*}"
277 [ -z "$dns" ] || add_dns "$config" $dns
278
279 config_get type "$config" TYPE
280 [ "$type" = "alias" ] && return 0
281
282 env -i ACTION="ifup" INTERFACE="$config" DEVICE="$iface" PROTO=static /sbin/hotplug-call "iface" &
283 }
284
285 setup_interface_alias() {
286 local config="$1"
287 local parent="$2"
288 local iface="$3"
289
290 local cfg
291 config_get cfg "$config" interface
292 [ "$parent" == "$cfg" ] || return 0
293
294 # parent device and ifname
295 local p_device p_type
296 config_get p_device "$cfg" device
297 config_get p_type "$cfg" type
298
299 # select alias ifname
300 local layer use_iface
301 config_get layer "$config" layer 2
302 case "$layer:$p_type" in
303 # layer 3: e.g. pppoe-wan or pptp-vpn
304 3:*) use_iface="$iface" ;;
305
306 # layer 2 and parent is bridge: e.g. br-wan
307 2:bridge) use_iface="br-$cfg" ;;
308
309 # layer 1: e.g. eth0 or ath0
310 *) use_iface="$p_device" ;;
311 esac
312
313 # alias counter
314 local ctr
315 config_get ctr "$parent" alias_count 0
316 ctr="$(($ctr + 1))"
317 config_set "$parent" alias_count "$ctr"
318
319 # alias list
320 local list
321 config_get list "$parent" aliases
322 append list "$config"
323 config_set "$parent" aliases "$list"
324
325 use_iface="$use_iface:$ctr"
326 set_interface_ifname "$config" "$use_iface"
327
328 local proto
329 config_get proto "$config" proto "static"
330 case "${proto}" in
331 static)
332 setup_interface_static "$use_iface" "$config"
333 ;;
334 *)
335 echo "Unsupported type '$proto' for alias config '$config'"
336 return 1
337 ;;
338 esac
339 }
340
341 setup_interface() {
342 local iface="$1"
343 local config="$2"
344 local proto="$3"
345 local vifmac="$4"
346
347 [ -n "$config" ] || {
348 config=$(find_config "$iface")
349 [ "$?" = 0 ] || return 1
350 }
351
352 prepare_interface "$iface" "$config" "$vifmac" || return 0
353
354 [ "$iface" = "br-$config" ] && {
355 # need to bring up the bridge and wait a second for
356 # it to switch to the 'forwarding' state, otherwise
357 # it will lose its routes...
358 ifconfig "$iface" up
359 sleep 1
360 }
361
362 # Interface settings
363 set_interface_ifname "$config" "$iface"
364
365 [ -n "$proto" ] || config_get proto "$config" proto
366 case "$proto" in
367 static)
368 setup_interface_static "$iface" "$config"
369 ;;
370 dhcp)
371 # kill running udhcpc instance
372 local pidfile="/var/run/dhcp-${iface}.pid"
373
374 SERVICE_PID_FILE="$pidfile" \
375 service_stop /sbin/udhcpc
376
377 local ipaddr netmask hostname proto1 clientid vendorid broadcast reqopts
378 config_get ipaddr "$config" ipaddr
379 config_get netmask "$config" netmask
380 config_get hostname "$config" hostname
381 config_get proto1 "$config" proto
382 config_get clientid "$config" clientid
383 config_get vendorid "$config" vendorid
384 config_get_bool broadcast "$config" broadcast 0
385 config_get reqopts "$config" reqopts
386
387 [ -z "$ipaddr" ] || \
388 $DEBUG ifconfig "$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
389
390 # additional request options
391 local opt dhcpopts daemonize
392 for opt in $reqopts; do
393 append dhcpopts "-O $opt"
394 done
395
396 # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
397 [ "$proto1" != "$proto" ] && {
398 append dhcpopts "-n -q"
399 } || {
400 append dhcpopts "-O rootpath -R"
401 daemonize=1
402 }
403 [ "$broadcast" = 1 ] && broadcast="-O broadcast" || broadcast=
404
405 SERVICE_DAEMONIZE=$daemonize \
406 SERVICE_PID_FILE="$pidfile" \
407 service_start /sbin/udhcpc -t 0 -i "$iface" \
408 ${ipaddr:+-r $ipaddr} \
409 ${hostname:+-H $hostname} \
410 ${clientid:+-c $clientid} \
411 ${vendorid:+-V $vendorid} \
412 -b -p "$pidfile" $broadcast \
413 ${dhcpopts}
414 ;;
415 none)
416 setup_interface_none "$iface" "$config"
417 ;;
418 *)
419 if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then
420 eval "setup_interface_$proto '$iface' '$config' '$proto'"
421 else
422 echo "Interface type $proto not supported."
423 return 1
424 fi
425 ;;
426 esac
427 }
428
429 stop_interface_dhcp() {
430 local config="$1"
431
432 local ifname
433 config_get ifname "$config" ifname
434
435 local lock="/var/lock/dhcp-${ifname}"
436 [ -f "$lock" ] && lock -u "$lock"
437
438 remove_dns "$config"
439
440 SERVICE_PID_FILE="/var/run/dhcp-${ifname}.pid" \
441 service_stop /sbin/udhcpc
442
443 uci -P /var/state revert "network.$config"
444 }
445
446 unbridge() {
447 local dev="$1"
448 local brdev
449
450 [ -x /usr/sbin/brctl ] || return 0
451 brctl show 2>/dev/null | grep "$dev" >/dev/null && {
452 # interface is still part of a bridge, correct that
453
454 for brdev in $(brctl show | awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
455 brctl delif "$brdev" "$dev" 2>/dev/null >/dev/null
456 do_sysctl "net.ipv6.conf.$dev.disable_ipv6" 0
457 done
458 }
459 }