Magellan Linux

Contents of /trunk/magellan-initscripts/etc/rc.d/init.d/network

Parent Directory Parent Directory | Revision Log Revision Log


Revision 868 - (show annotations) (download)
Wed May 13 18:57:55 2009 UTC (14 years, 11 months ago) by niro
File size: 11323 byte(s)
-fixed some warnings with wireless extensions enabled
- wireless modes (managed, ad-hoc) are working, as the device is not in use because we run "ifconfig $device up" later
1 #!/bin/bash
2 # $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/network,v 1.19 2008-12-22 22:01:15 niro Exp $
3
4 #%rlevels: 0:k 1:k 2:k 3:s 4:s 5:s 6:k
5 #%start: 20
6 #%stop: 80
7
8 #deps
9 #%needs:
10 #%before:
11 #%after:
12
13 source /etc/conf.d/rc
14 source ${rc_functions}
15
16 # read values from files
17 read_value()
18 {
19 local var="$1"
20 local file="$2"
21 local value
22
23 # local all possible vars
24 # global
25 local ONBOOT
26 local NETWORKING
27
28 # static
29 local IP
30 local NETMASK
31 local BROADCAST
32 local NETWORKING
33 local FORCE_MAC_TO
34
35 # dhcp
36 local DHCP_PROG
37 local DHCP_START
38 local DHCP_STOP
39
40 # default gw
41 local GATEWAY
42 local GATEWAY_IF
43
44 # wireless extensions
45 local WIRELESS_AP
46 local WIRELESS_AUTH_MODE
47 local WIRELESS_BITRATE
48 local WIRELESS_CHANNEL
49 local WIRELESS_DEFAULT_KEY
50 local WIRELESS_ESSID
51 local WIRELESS_FREQUENCY
52 local WIRELESS_KEY
53 local WIRELESS_KEY_ASCII
54 local WIRELESS_KEY_0
55 local WIRELESS_KEY_1
56 local WIRELESS_KEY_2
57 local WIRELESS_KEY_3
58 local WIRELESS_KEY_LENGTH
59 local WIRELESS_MODE
60 local WIRELESS_NICK
61 local WIRELESS_NWID
62 local WIRELESS_POWER
63 local WIRELESS_WPA_DRIVER
64
65 local BRIDGE_INTERFACES
66 local BRIDGE_STP
67
68 source ${file}
69 eval value=\$$(echo ${var})
70 echo "${value}"
71 }
72
73 checkconfig()
74 {
75 if [[ -z ${NETWORKING} ]]
76 then
77 echo "NETWORKING missing in net.${interface}, aborted"
78 exit 1
79 fi
80
81 case "${NETWORKING}" in
82 static)
83 if [[ -z ${IP} ]]
84 then
85 echo "IP missing in net.${interface}, aborted"
86 exit 1
87 fi
88
89 if [[ -z ${NETMASK} ]]
90 then
91 echo -n "NETMASK missing in net.${interface}, "
92 echo "using 255.255.255.0"
93 NETMASK=255.255.255.0
94 fi
95
96 if [[ -z ${BROADCAST} ]]
97 then
98 echo -n "BROADCAST missing in net.${interface}, "
99 echo "using default address"
100 fi
101 ;;
102
103 dhcp)
104 if [[ -z ${DHCP_PROG} ]]
105 then
106 echo "DHCP_PROG missing in net.${interface}, aborted"
107 exit 1
108 fi
109 ;;
110
111 esac
112 }
113
114 # onboot_interface_list /path/to/files*
115 onboot_interface_list()
116 {
117 local file
118 local devices
119 local iface
120
121 # get list of all devices
122 for file in $@
123 do
124 if [[ $(read_value ONBOOT ${file}) = yes ]]
125 then
126 iface="$(basename ${file} | sed s/net.//)"
127 # exclude backup files and exclude net.routes too
128 case "${iface}" in
129 *~) ;;
130 */net.routes) ;;
131 *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;
132 esac
133 fi
134 done
135
136 echo "${devices}"
137 }
138
139 config_wireless_wep()
140 {
141 local iface="$1"
142
143 if [[ -z ${iface} ]]
144 then
145 echo "WEP: no \$iface given. Aborting setup."
146 return 1
147 fi
148
149 ${CURS_UP}
150 ${SET_WWCOL}
151 echo "[AUTH: WEP]"
152
153 iwconfig "${iface}" enc on
154 [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}"
155 [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}"
156 [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}"
157 }
158
159 config_wireless_wpa()
160 {
161 local iface="$1"
162
163 if [[ -z ${iface} ]]
164 then
165 echo "WPA: no \$iface given. Aborting setup."
166 return 1
167 fi
168
169 if [ ! -x /sbin/wpa_supplicant ]
170 then
171 echo "WPA: wpa_supplicant not installed. Aborting setup."
172 return 1
173 fi
174
175 ${CURS_UP}
176 ${SET_WWCOL}
177 echo "[AUTH: WPA]"
178
179 # get default settings
180 [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant
181
182 # check the configuration
183 [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto
184 [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel
185 if [[ -z ${WIRELESS_WPA_DRIVER} ]]
186 then
187 echo "WPA: WIRELESS_WPA_DRIVER given. Aborting setup."
188 return 1
189 fi
190
191 # write a config with the settings from net.${iface}
192 # only wpa-psk ! all other needs manual setup
193 if [[ ${WIRELESS_WPA_AUTOCONF} = true ]]
194 then
195 # write default cfg from skeleton
196 cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG}
197
198 local wpa_proto
199 case ${WIRELESS_AUTH_MODE} in
200 wpa) wpa_proto="WPA" ;;
201 wpa2) wpa_proto="WPA2" ;;
202 esac
203
204 # setup the network entry
205 sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
206 -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
207 -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
208 ${WIRELESS_WPA_CONFIG}
209 fi
210
211 # remove old state dir
212 [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant
213
214 # now run the wpa_supplicant dameon
215 wpa_supplicant -B \
216 -D"${WIRELESS_WPA_DRIVER}" \
217 -c"${WIRELESS_WPA_CONFIG}" \
218 -i"${iface}" \
219 ${WIRELESS_WPA_OPTS}
220
221 # echo wait 5 seconds
222 echo " Waiting 5 seconds to retrieve authentification reply ... "
223 sleep 5
224 }
225
226 setup_wireless_extensions()
227 {
228 local iface="$1"
229
230 if [[ -z ${iface} ]]
231 then
232 echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
233 return 1
234 fi
235
236 if [[ -n ${WIRELESS_BITRATE} ]] ||
237 [[ -n ${WIRELESS_CHANNEL} ]] ||
238 [[ -n ${WIRELESS_ESSID} ]] ||
239 [[ -n ${WIRELESS_FREQUENCY} ]] ||
240 [[ -n ${WIRELESS_MODE} ]] ||
241 [[ -n ${WIRELESS_NICK} ]] ||
242 [[ -n ${WIRELESS_AUTH_MODE} ]]
243 then
244 echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
245 fi
246
247 [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"
248 [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"
249 [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}"
250 [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}"
251 [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}"
252 [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"
253
254 case "${WIRELESS_AUTH_MODE}" in
255 wpa|wpa2) config_wireless_wpa "${iface}" ;;
256 wep|on) config_wireless_wep "${iface}" ;;
257 off) iwconfig "${iface}" enc off ;;
258 esac
259 }
260
261 config_bridge_devices()
262 {
263 local iface="$1"
264 local method="$2"
265 local bport
266
267 if [[ -z ${iface} ]]
268 then
269 echo "BRIDGE: no \$iface given. Aborting setup."
270 return 1
271 fi
272
273 if [[ -z ${method} ]]
274 then
275 echo "BRIDGE: no \$method given. Aborting setup."
276 return 1
277 fi
278
279 # first check for brctl
280 if [[ -z $(which brctl) ]]
281 then
282 echo "brctl not found! Please install 'net-misc/bridge-utils'."
283 return 1
284 fi
285
286 # check the config
287 if [[ -z ${BRIDGE_INTERFACES} ]]
288 then
289 echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
290 return 1
291 fi
292
293 case ${method} in
294 add)
295 # setup the bridge device
296 brctl addbr ${iface}
297 for bport in ${BRIDGE_INTERFACES}
298 do
299 # enter promiscous mode
300 ifconfig ${bport} 0.0.0.0 promisc
301 # now setup the bridge
302 brctl addif ${iface} ${bport}
303 done
304 # enable spanning-tree protocol
305 case ${BRIDGE_STP} in
306 on|off) brctl stp ${iface} ${BRIDGE_STP} ;;
307 *) echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
308 esac
309 ;;
310
311 remove)
312 for bport in ${BRIDGE_INTERFACE}
313 do
314 # bring the interface down
315 ifconfig ${bport} down
316 # remove the interface from the bridge
317 brctl delif ${iface} ${bport}
318 done
319 # bring the bridge down
320 brctl delbr ${iface}
321 ;;
322 esac
323
324 # unset the bridge variable to be safe
325 unset BRIDGE_INTERFACES
326 # continue to setup generic networking
327 }
328
329 config_routes()
330 {
331 local method="$1"
332 local message
333
334 # only add and del are allowed
335 case ${method} in
336 add) message="Adding route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
337 del) message="Removing route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
338 *)
339 echo "config_routes: unsupported \$method '${method}'."
340 exit 1
341 ;;
342 esac
343
344 # adds/delete user routes
345 if [[ -f /etc/conf.d/net.routes ]]
346 then
347 ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end
348 while read route
349 do
350 case "${route}" in
351 \#*|"") continue ;;
352 esac
353 echo -e ${COLOREDSTAR}"${message}"
354 route ${method} ${route}
355 evaluate_retval
356 done
357 fi
358 }
359
360 networking_start()
361 {
362 local iface dns routes ALL_INTERFACES
363
364 if [[ -z $1 ]]
365 then
366 ALL_INTERFACES=$(onboot_interface_list ${network_settings}/net.*)
367 else
368 if [[ -e ${network_settings}/net.$1 ]]
369 then
370 ALL_INTERFACES="$1"
371 else
372 ${FAILURE}
373 echo "Interface $1 does not exist. Aborting"
374 ${NORMAL}
375 exit 1
376 fi
377 fi
378
379 # get list of all devices
380 for iface in ${ALL_INTERFACES}
381 do
382 # checkconfig
383 source ${network_settings}/net.${iface} || exit 1
384 checkconfig
385
386 # setup mac
387 if [ -n "${FORCE_MAC_TO}" ]
388 then
389 echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
390 ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
391 evaluate_retval
392 fi
393
394 # setup bridges
395 if [[ ${iface} = br[0-9]* ]]
396 then
397 config_bridge_devices ${iface} add
398 fi
399
400 # now configure wireless_extensions
401 [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
402
403 echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
404
405 # activate the interface
406 ifconfig "${iface}" up
407
408 # setup static or dhcp
409 case ${NETWORKING} in
410 dhcp|DHCP)
411 ${CURS_UP}
412 ${SET_WWCOL}
413 echo "[DHCP]"
414 loadproc ${DHCP_PROG} ${DHCP_START} "${iface}"
415 ;;
416 static|STATIC)
417 ${CURS_UP}
418 ${SET_WWCOL}
419 echo "[STATIC]"
420 ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}"
421 evaluate_retval
422 ;;
423 esac
424
425 # setup def gw
426 if [[ -n ${GATEWAY} ]]
427 then
428 echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
429 route add default gateway ${GATEWAY} metric 1 dev ${iface}
430 evaluate_retval
431
432 unset GATEWAY
433 fi
434
435 # setup /etc/resolv.conf
436 if [[ -n ${NAMESERVER} ]]
437 then
438 echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
439
440 # whipe out the old one
441 echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
442 for dns in ${NAMESERVER}
443 do
444 echo "nameserver ${dns}" >> /etc/resolv.conf
445 done
446
447 unset NAMESERVER
448 fi
449 done
450
451 # setup user routes
452 config_routes add
453 }
454
455 networking_stop()
456 {
457 if [[ -z $1 ]]
458 then
459 ALL_INTERFACES=$(onboot_interface_list ${network_settings}/net.*)
460 else
461 if [[ -e ${network_settings}/net.$1 ]]
462 then
463 ALL_INTERFACES="$1"
464 else
465 ${FAILURE}
466 echo "Interface $1 does not exist. Aborting"
467 ${NORMAL}
468 exit 1
469 fi
470 fi
471
472 # get list of all devices
473 for iface in ${ALL_INTERFACES}
474 do
475 source ${network_settings}/net.${iface} || exit 1
476 checkconfig
477
478 if [[ -n ${GATEWAY} ]]
479 then
480 echo -e ${COLOREDSTAR}"Removing default gateway ..."
481 route del -net default
482 evaluate_retval
483 fi
484
485 echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
486 ifconfig ${iface} down
487 evaluate_retval
488
489 # remove bridges
490 if [[ ${iface} = br[0-9]* ]]
491 then
492 config_bridge_devices ${iface} remove
493 fi
494
495 # shutdown dhcp-daemon
496 if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
497 then
498 echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..."
499 ${CURS_UP}
500 ${SET_WWCOL}
501 echo "[$(basename ${DHCP_PROG})]"
502 ${DHCP_PROG} ${DHCP_STOP} "${iface}"
503 evaluate_retval
504 fi
505
506 # shutdown wpa_supplicant daemon
507 if [[ -n $(pidof wpa_supplicant) ]]
508 then
509 killall wpa_supplicant
510 fi
511 done
512
513 # remove state dir
514 if [ -d /var/run/wpa_supplicant ]
515 then
516 rm -rf /var/run/wpa_supplicant
517 fi
518
519 # delete user routes
520 config_routes del
521 }
522
523 case $1 in
524 start)
525 networking_start $2
526 update_svcstatus $1
527 splash svc_started "$(basename $0)" 0
528 ;;
529
530 stop)
531 networking_stop $2
532 update_svcstatus $1
533 splash svc_stopped "$(basename $0)" 0
534 ;;
535
536 restart)
537 $0 stop
538 sleep 1
539 $0 start
540 ;;
541
542 *)
543 echo "Usage: $0 {start|stop|restart} [interface]"
544 exit 1
545 ;;
546 esac

Properties

Name Value
svn:executable *