when adding vlans, not only use vlan*hwaddr, but check vlan*hwname as well (finally...
[openwrt/svn-archive/archive.git] / openwrt / package / base-files / default / etc / functions.sh
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4
5 alias debug=${DEBUG:-:}
6
7 # valid interface?
8 if_valid () (
9 ifconfig "$1" >&- 2>&- ||
10 [ "${1%%[0-9]}" = "br" ] ||
11 {
12 [ "${1%%[0-9]*}" = "vlan" ] && (
13 i=${1#vlan}
14 hwname=$(nvram get vlan${i}hwname)
15 hwaddr=$(nvram get ${hwname}macaddr)
16 [ -z "$hwaddr" ] && return 1
17
18 vif=$(ifconfig -a | awk '/^eth.*'$hwaddr'/ {print $1; exit}' IGNORECASE=1)
19 debug "# vlan$i => $vif"
20 [ -n "$vif" ] || vif=$(ifconfig -a | awk '/^eth'${hw##et}'/ {print $1; exit}' IGNORECASE=1)
21
22 $DEBUG ifconfig $vif up
23 $DEBUG vconfig add $vif $i 2>&-
24 )
25 } ||
26 { debug "# missing interface '$1' ignored"; false; }
27 )
28
29 do_ifup() {
30 ifproto="$1"
31 iftype="$2"
32 if="$(nvram get ${iftype}_ifname)"
33 [ "${if%%[0-9]}" = "ppp" ] && if="$(nvram get ${iftype}_device)"
34
35 pidfile=/var/run/${if}.pid
36 [ -f $pidfile ] && $DEBUG kill $(cat $pidfile)
37
38 case "$ifproto" in
39 static)
40 ip=$(nvram get ${iftype}_ipaddr)
41 netmask=$(nvram get ${iftype}_netmask)
42 gateway=$(nvram get ${iftype}_gateway)
43 mtu=$(nvram get ${iftype}_mtu)
44 static_route=$(nvram get ${iftype}_static_route)
45
46 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} ${mtu:+mtu $(($mtu))} broadcast + up
47 ${gateway:+$DEBUG route add default gw $gateway}
48
49 [ -f /tmp/resolv.conf.auto ] || {
50 debug "# --- creating /tmp/resolv.conf.auto ---"
51 for dns in $(nvram get ${iftype}_dns); do
52 echo "nameserver $dns" >> /tmp/resolv.conf.auto
53 done
54 }
55
56 [ -n "$static_route" ] && {
57 for route in $static_route; do {
58 eval "set $(echo $route | sed 's/:/ /g')"
59 if [ "$2" = "255.255.255.255" ]; then
60 opt="-host"
61 fi
62 $DEBUG route add ${opt:-"-net"} $1 netmask $2 gw $3 metric $4
63 } done
64 }
65
66 env -i ACTION="ifup" INTERFACE="${iftype}" PROTO=static /sbin/hotplug "iface" &
67 ;;
68 dhcp*)
69 DHCP_IP=$(nvram get ${iftype}_ipaddr)
70 DHCP_NETMASK=$(nvram get ${iftype}_netmask)
71 mtu=$(nvram get ${iftype}_mtu)
72 $DEBUG ifconfig $if $DHCP_IP ${DHCP_NETMASK:+netmask $DHCP_NETMASK} ${mtu:+mtu $(($mtu))} broadcast + up
73
74 DHCP_ARGS="-i $if ${DHCP_IP:+-r $DHCP_IP} -b -p $pidfile -t 0"
75 DHCP_HOSTNAME=$(nvram get ${iftype}_hostname)
76 DHCP_HOSTNAME=${DHCP_HOSTNAME%%.*}
77 [ -z $DHCP_HOSTNAME ] || DHCP_ARGS="$DHCP_ARGS -H $DHCP_HOSTNAME"
78 [ "$(nvram get ${iftype}_proto)" = "pptp" ] && DHCP_ARGS="$DHCP_ARGS -n -q" || DHCP_ARGS="$DHCP_ARGS -R &"
79 [ -r $pidfile ] && oldpid=$(cat $pidfile 2>&-)
80 ${DEBUG:-eval} "udhcpc $DHCP_ARGS"
81 [ -n "$oldpid" ] && pidof udhcpc | grep "$oldpid" >&- 2>&- && {
82 sleep 1
83 kill -9 $oldpid
84 }
85 # hotplug events are handled by /usr/share/udhcpc/default.script
86 ;;
87 none|"")
88 ;;
89 *)
90 [ -x "/sbin/ifup.$ifproto" ] && { $DEBUG /sbin/ifup.$ifproto ${iftype}; exit; }
91 echo "### ifup ${iftype}: ignored ${iftype}_proto=\"$ifproto\" (not supported)"
92 ;;
93 esac
94 }
95
96 # newline
97 N="
98 "
99
100 _C=0
101
102 hotplug_dev() {
103 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug net
104 }
105
106 append() {
107 local var="$1"
108 local value="$2"
109 local sep="${3:- }"
110
111 eval "export -n -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
112 }
113
114 reset_cb() {
115 config_cb() { return 0; }
116 option_cb() { return 0; }
117 }
118 reset_cb
119
120 config () {
121 local cfgtype="$1"
122 local name="$2"
123
124 CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
125 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
126 append CONFIG_SECTIONS "$name"
127 config_cb "$cfgtype" "$name"
128 CONFIG_SECTION="$name"
129 export -n "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
130 }
131
132 option () {
133 local varname="$1"; shift
134 local value="$*"
135
136 export -n "CONFIG_${CONFIG_SECTION}_${varname}=$value"
137 option_cb "$varname" "$*"
138 }
139
140 config_rename() {
141 local OLD="$1"
142 local NEW="$2"
143 local oldvar
144 local newvar
145
146 [ "$OLD" -a "$NEW" ] || return
147 for oldvar in `set | grep ^CONFIG_${OLD}_ | \
148 sed -e 's/\(.*\)=.*$/\1/'` ; do
149 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
150 eval "export -n \"$newvar=\${$oldvar}\""
151 unset "$oldvar"
152 done
153 CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
154
155 [ "$CONFIG_SECTION" = "$OLD" ] && CONFIG_SECTION="$NEW"
156 }
157
158 config_unset() {
159 config_set "$1" "$2" ""
160 }
161
162 config_clear() {
163 local SECTION="$1"
164 local oldvar
165
166 CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , ,")"
167 CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
168
169 for oldvar in `set | grep ^CONFIG_${SECTION:+$SECTION_} | \
170 sed -e 's/\(.*\)=.*$/\1/'` ; do
171 unset $oldvar
172 done
173 }
174
175 config_load() {
176 local file="/etc/config/$1"
177 _C=0
178 CONFIG_SECTIONS=
179 CONFIG_NUM_SECTIONS=0
180 CONFIG_SECTION=
181
182 [ -e "$file" ] && {
183 . $file
184 } || return 1
185
186 ${CONFIG_SECTION:+config_cb}
187 }
188
189 config_get() {
190 case "$3" in
191 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
192 *) eval "export -n -- \"$1=\${CONFIG_${2}_${3}}\"";;
193 esac
194 }
195
196 config_set() {
197 local section="$1"
198 local option="$2"
199 local value="$3"
200 export -n "CONFIG_${section}_${option}=$value"
201 }
202
203 config_foreach() {
204 local function="$1"
205 local section
206
207 [ -z "$CONFIG_SECTIONS" ] && return 0
208 for section in ${CONFIG_SECTIONS}; do
209 eval "$function \"\$section\""
210 done
211 }
212
213 load_modules() {
214 cd /etc/modules.d
215 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
216 }
217
218 include() {
219 local file
220
221 for file in $(ls $1/*.sh 2>/dev/null); do
222 . $file
223 done
224 }
225
226 find_mtd_part() {
227 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
228
229 PART="${PART##mtd}"
230 echo "${PART:+/dev/mtdblock/$PART}"
231 }
232
233 strtok() { # <string> { <variable> [<separator>] ... }
234 local tmp
235 local val="$1"
236 local count=0
237
238 shift
239
240 while [ $# -gt 1 ]; do
241 tmp="${val%%$2*}"
242
243 [ "$tmp" = "$val" ] && break
244
245 val="${val#$tmp$2}"
246
247 export -n "$1=$tmp"; count=$((count+1))
248 shift 2
249 done
250
251 if [ $# -gt 0 -a "$val" ]; then
252 export -n "$1=$val"; count=$((count+1))
253 fi
254
255 return $count
256 }
257
258 set_led() {
259 local led="$1"
260 local state="$2"
261 [ -f "/proc/diag/led/$1" ] && echo "$state" > "/proc/diag/led/$1"
262 }
263
264 set_state() {
265 case "$1" in
266 preinit)
267 set_led dmz 1
268 set_led diag 1
269 set_led power 0
270 ;;
271 failsafe)
272 set_led diag f
273 set_led power f
274 set_led dmz f
275 ;;
276 done)
277 set_led dmz 0
278 set_led diag 0
279 set_led power 1
280 ;;
281 esac
282 }