add static route fix from #1384
[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" = "0.0.0.0" ]; then
60 opt="-host"
61 fi
62 $DEBUG route add ${opt:-"-net"} $1 netmask $2 gw $3 metric $4
63 unset opt
64 } done
65 }
66
67 env -i ACTION="ifup" INTERFACE="${iftype}" PROTO=static /sbin/hotplug "iface" &
68 ;;
69 dhcp*)
70 DHCP_IP=$(nvram get ${iftype}_ipaddr)
71 DHCP_NETMASK=$(nvram get ${iftype}_netmask)
72 mtu=$(nvram get ${iftype}_mtu)
73 $DEBUG ifconfig $if $DHCP_IP ${DHCP_NETMASK:+netmask $DHCP_NETMASK} ${mtu:+mtu $(($mtu))} broadcast + up
74
75 DHCP_ARGS="-i $if ${DHCP_IP:+-r $DHCP_IP} -b -p $pidfile -t 0"
76 DHCP_HOSTNAME=$(nvram get ${iftype}_hostname)
77 DHCP_HOSTNAME=${DHCP_HOSTNAME%%.*}
78 [ -z $DHCP_HOSTNAME ] || DHCP_ARGS="$DHCP_ARGS -H $DHCP_HOSTNAME"
79 [ "$(nvram get ${iftype}_proto)" = "pptp" ] && DHCP_ARGS="$DHCP_ARGS -n -q" || DHCP_ARGS="$DHCP_ARGS -R &"
80 [ -r $pidfile ] && oldpid=$(cat $pidfile 2>&-)
81 ${DEBUG:-eval} "udhcpc $DHCP_ARGS"
82 [ -n "$oldpid" ] && pidof udhcpc | grep "$oldpid" >&- 2>&- && {
83 sleep 1
84 kill -9 $oldpid
85 }
86 # hotplug events are handled by /usr/share/udhcpc/default.script
87 ;;
88 none|"")
89 ;;
90 *)
91 [ -x "/sbin/ifup.$ifproto" ] && { $DEBUG /sbin/ifup.$ifproto ${iftype}; exit; }
92 echo "### ifup ${iftype}: ignored ${iftype}_proto=\"$ifproto\" (not supported)"
93 ;;
94 esac
95 }
96
97 # newline
98 N="
99 "
100
101 _C=0
102
103 hotplug_dev() {
104 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug net
105 }
106
107 append() {
108 local var="$1"
109 local value="$2"
110 local sep="${3:- }"
111
112 eval "export -n -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
113 }
114
115 reset_cb() {
116 config_cb() { return 0; }
117 option_cb() { return 0; }
118 }
119 reset_cb
120
121 config () {
122 local cfgtype="$1"
123 local name="$2"
124
125 CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
126 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
127 append CONFIG_SECTIONS "$name"
128 config_cb "$cfgtype" "$name"
129 CONFIG_SECTION="$name"
130 export -n "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
131 }
132
133 option () {
134 local varname="$1"; shift
135 local value="$*"
136
137 export -n "CONFIG_${CONFIG_SECTION}_${varname}=$value"
138 option_cb "$varname" "$*"
139 }
140
141 config_rename() {
142 local OLD="$1"
143 local NEW="$2"
144 local oldvar
145 local newvar
146
147 [ "$OLD" -a "$NEW" ] || return
148 for oldvar in `set | grep ^CONFIG_${OLD}_ | \
149 sed -e 's/\(.*\)=.*$/\1/'` ; do
150 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
151 eval "export -n \"$newvar=\${$oldvar}\""
152 unset "$oldvar"
153 done
154 CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
155
156 [ "$CONFIG_SECTION" = "$OLD" ] && CONFIG_SECTION="$NEW"
157 }
158
159 config_unset() {
160 config_set "$1" "$2" ""
161 }
162
163 config_clear() {
164 local SECTION="$1"
165 local oldvar
166
167 CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , ,")"
168 CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
169
170 for oldvar in `set | grep ^CONFIG_${SECTION:+$SECTION_} | \
171 sed -e 's/\(.*\)=.*$/\1/'` ; do
172 unset $oldvar
173 done
174 }
175
176 config_load() {
177 local file="/etc/config/$1"
178 _C=0
179 CONFIG_SECTIONS=
180 CONFIG_NUM_SECTIONS=0
181 CONFIG_SECTION=
182
183 [ -e "$file" ] && {
184 . $file
185 } || return 1
186
187 ${CONFIG_SECTION:+config_cb}
188 }
189
190 config_get() {
191 case "$3" in
192 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
193 *) eval "export -n -- \"$1=\${CONFIG_${2}_${3}}\"";;
194 esac
195 }
196
197 config_set() {
198 local section="$1"
199 local option="$2"
200 local value="$3"
201 export -n "CONFIG_${section}_${option}=$value"
202 }
203
204 config_foreach() {
205 local function="$1"
206 local section
207
208 [ -z "$CONFIG_SECTIONS" ] && return 0
209 for section in ${CONFIG_SECTIONS}; do
210 eval "$function \"\$section\""
211 done
212 }
213
214 load_modules() {
215 cd /etc/modules.d
216 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
217 }
218
219 include() {
220 local file
221
222 for file in $(ls $1/*.sh 2>/dev/null); do
223 . $file
224 done
225 }
226
227 find_mtd_part() {
228 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
229
230 PART="${PART##mtd}"
231 echo "${PART:+/dev/mtdblock/$PART}"
232 }
233
234 strtok() { # <string> { <variable> [<separator>] ... }
235 local tmp
236 local val="$1"
237 local count=0
238
239 shift
240
241 while [ $# -gt 1 ]; do
242 tmp="${val%%$2*}"
243
244 [ "$tmp" = "$val" ] && break
245
246 val="${val#$tmp$2}"
247
248 export -n "$1=$tmp"; count=$((count+1))
249 shift 2
250 done
251
252 if [ $# -gt 0 -a "$val" ]; then
253 export -n "$1=$val"; count=$((count+1))
254 fi
255
256 return $count
257 }
258
259 set_led() {
260 local led="$1"
261 local state="$2"
262 [ -f "/proc/diag/led/$1" ] && echo "$state" > "/proc/diag/led/$1"
263 }
264
265 set_state() {
266 case "$1" in
267 preinit)
268 set_led dmz 1
269 set_led diag 1
270 set_led power 0
271 ;;
272 failsafe)
273 set_led diag f
274 set_led power f
275 set_led dmz f
276 ;;
277 done)
278 set_led dmz 0
279 set_led diag 0
280 set_led power 1
281 ;;
282 esac
283 }