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