Magellan Linux

Annotation of /trunk/busybox-initscripts/rc/network

Parent Directory Parent Directory | Revision Log Revision Log


Revision 724 - (hide annotations) (download)
Mon Jun 2 22:14:35 2008 UTC (15 years, 11 months ago) by niro
File size: 11114 byte(s)
-busybox port of magellan-initscript

1 niro 724 #!/bin/sh
2     # $Header: /home/cvsd/magellan-cvs/magellan-src/busybox-initscripts/rc/network,v 1.1 2008-06-02 22:14:35 niro Exp $
3    
4     #%rlevels: 0:k 3:s
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     # setup the network entry
199     sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
200     -e "s:@WIRELESS_KEY@:${WIRELESS_KEY}:g" \
201     ${WIRELESS_WPA_CONFIG}
202     fi
203    
204     # remove old state dir
205     [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant
206    
207     # now run the wpa_supplicant dameon
208     wpa_supplicant -B \
209     -D"${WIRELESS_WPA_DRIVER}" \
210     -c"${WIRELESS_WPA_CONFIG}" \
211     -i"${iface}" \
212     ${WIRELESS_WPA_OPTS}
213    
214     # echo wait 5 seconds
215     echo " Waiting 5 seconds to retrieve authentification reply ... "
216     sleep 5
217     }
218    
219     setup_wireless_extensions()
220     {
221     local iface="$1"
222    
223     if [[ -z ${iface} ]]
224     then
225     echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
226     return 1
227     fi
228    
229     if [[ -n ${WIRELESS_BITRATE} ]] ||
230     [[ -n ${WIRELESS_CHANNEL} ]] ||
231     [[ -n ${WIRELESS_ESSID} ]] ||
232     [[ -n ${WIRELESS_FREQUENCY} ]] ||
233     [[ -n ${WIRELESS_MODE} ]] ||
234     [[ -n ${WIRELESS_NICK} ]] ||
235     [[ -n ${WIRELESS_AUTH_MODE} ]]
236     then
237     echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
238     fi
239    
240     [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"
241     [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"
242     [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}"
243     [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}"
244     [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}"
245     [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"
246    
247     case "${WIRELESS_AUTH_MODE}" in
248     wpa) config_wireless_wpa "${iface}" ;;
249     wep|on) config_wireless_wep "${iface}" ;;
250     off) iwconfig "${iface}" enc off ;;
251     esac
252     }
253    
254     config_bridge_devices()
255     {
256     local iface="$1"
257     local method="$2"
258     local bport
259    
260     if [[ -z ${iface} ]]
261     then
262     echo "BRIDGE: no \$iface given. Aborting setup."
263     return 1
264     fi
265    
266     if [[ -z ${method} ]]
267     then
268     echo "BRIDGE: no \$method given. Aborting setup."
269     return 1
270     fi
271    
272     # first check for brctl
273     if [[ -z $(which brctl) ]]
274     then
275     echo "brctl not found! Please install 'net-misc/bridge-utils'."
276     return 1
277     fi
278    
279     # check the config
280     if [[ -z ${BRIDGE_INTERFACES} ]]
281     then
282     echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
283     return 1
284     fi
285    
286     case ${method} in
287     add)
288     # setup the bridge device
289     brctl addbr ${iface}
290     for bport in ${BRIDGE_INTERFACES}
291     do
292     # enter promiscous mode
293     ifconfig ${bport} 0.0.0.0 promisc
294     # now setup the bridge
295     brctl addif ${iface} ${bport}
296     done
297     # enable spanning-tree protocol
298     case ${BRIDGE_STP} in
299     on|off) brctl stp ${iface} ${BRIDGE_STP} ;;
300     *) echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
301     esac
302     ;;
303    
304     remove)
305     for bport in ${BRIDGE_INTERFACE}
306     do
307     # bring the interface down
308     ifconfig ${bport} down
309     # remove the interface from the bridge
310     brctl delif ${iface} ${bport}
311     done
312     # bring the bridge down
313     brctl delbr ${iface}
314     ;;
315     esac
316    
317     # unset the bridge variable to be safe
318     unset BRIDGE_INTERFACES
319     # continue to setup generic networking
320     }
321    
322     config_routes()
323     {
324     local method="$1"
325     local message
326    
327     # only add and del are allowed
328     case ${method} in
329     add) message="Adding route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
330     del) message="Removing route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
331     *)
332     echo "config_routes: unsupported \$method '${method}'."
333     exit 1
334     ;;
335     esac
336    
337     # adds/delete user routes
338     if [[ -f /etc/conf.d/net.routes ]]
339     then
340     ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end
341     while read route
342     do
343     case "${route}" in
344     \#*|"") continue ;;
345     esac
346     echo -e ${COLOREDSTAR}"${message}"
347     route ${method} ${route}
348     evaluate_retval
349     done
350     fi
351     }
352    
353     networking_start()
354     {
355     local iface dns routes ALL_INTERFACES
356    
357     if [[ -z $1 ]]
358     then
359     ALL_INTERFACES=$(onboot_interface_list ${network_settings}/net.*)
360     else
361     if [[ -e ${network_settings}/net.$1 ]]
362     then
363     ALL_INTERFACES="$1"
364     else
365     ${FAILURE}
366     echo "Interface $1 does not exist. Aborting"
367     ${NORMAL}
368     exit 1
369     fi
370     fi
371    
372     # get list of all devices
373     for iface in ${ALL_INTERFACES}
374     do
375     # checkconfig
376     source ${network_settings}/net.${iface} || exit 1
377     checkconfig
378    
379     # setup mac
380     if [ -n "${FORCE_MAC_TO}" ]
381     then
382     echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
383     ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
384     evaluate_retval
385     fi
386    
387     # setup bridges
388     if [[ ${iface} = br[0-9]* ]]
389     then
390     config_bridge_devices ${iface} add
391     fi
392    
393     # activate the interface
394     ifconfig "${iface}" up
395    
396     # now configure wireless_extensions
397     [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
398    
399     echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
400    
401     # setup static or dhcp
402     case ${NETWORKING} in
403     dhcp|DHCP)
404     ${CURS_UP}
405     ${SET_WWCOL}
406     echo "[DHCP]"
407     loadproc ${DHCP_PROG} ${DHCP_START} "${iface}"
408     ;;
409     static|STATIC)
410     ${CURS_UP}
411     ${SET_WWCOL}
412     echo "[STATIC]"
413     ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}"
414     evaluate_retval
415     ;;
416     esac
417    
418     # setup def gw
419     if [[ -n ${GATEWAY} ]]
420     then
421     echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
422     route add default gateway ${GATEWAY} metric 1 dev ${iface}
423     evaluate_retval
424    
425     unset GATEWAY
426     fi
427    
428     # setup /etc/resolv.conf
429     if [[ -n ${NAMESERVER} ]]
430     then
431     echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
432    
433     # whipe out the old one
434     echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
435     for dns in ${NAMESERVER}
436     do
437     echo "nameserver ${dns}" >> /etc/resolv.conf
438     done
439    
440     unset NAMESERVER
441     fi
442     done
443    
444     # setup user routes
445     config_routes add
446     }
447    
448     networking_stop()
449     {
450     if [[ -z $1 ]]
451     then
452     ALL_INTERFACES=$(onboot_interface_list ${network_settings}/net.*)
453     else
454     if [[ -e ${network_settings}/net.$1 ]]
455     then
456     ALL_INTERFACES="$1"
457     else
458     ${FAILURE}
459     echo "Interface $1 does not exist. Aborting"
460     ${NORMAL}
461     exit 1
462     fi
463     fi
464    
465     # get list of all devices
466     for iface in ${ALL_INTERFACES}
467     do
468     source ${network_settings}/net.${iface} || exit 1
469     checkconfig
470    
471     if [[ -n ${GATEWAY} ]]
472     then
473     echo -e ${COLOREDSTAR}"Removing default gateway ..."
474     route del -net default
475     evaluate_retval
476     fi
477    
478     echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
479     ifconfig ${iface} down
480     evaluate_retval
481    
482     # remove bridges
483     if [[ ${iface} = br[0-9]* ]]
484     then
485     config_bridge_devices ${iface} remove
486     fi
487    
488     # shutdown dhcp-daemon
489     if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
490     then
491     echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..."
492     ${CURS_UP}
493     ${SET_WWCOL}
494     echo "[$(basename ${DHCP_PROG})]"
495     ${DHCP_PROG} ${DHCP_STOP} "${iface}"
496     evaluate_retval
497     fi
498    
499     # shutdown wpa_supplicant daemon
500     if [[ -n $(pidof wpa_supplicant) ]]
501     then
502     killall wpa_supplicant
503     fi
504     done
505    
506     # remove state dir
507     if [ -d /var/run/wpa_supplicant ]
508     then
509     rm -rf /var/run/wpa_supplicant
510     fi
511    
512     # delete user routes
513     config_routes del
514     }
515    
516     case $1 in
517     start)
518     networking_start $2
519     update_svcstatus $1
520     splash svc_started "$(basename $0)" 0
521     ;;
522    
523     stop)
524     networking_stop $2
525     update_svcstatus $1
526     splash svc_stopped "$(basename $0)" 0
527     ;;
528    
529     restart)
530     $0 stop
531     sleep 1
532     $0 start
533     ;;
534    
535     *)
536     echo "Usage: $0 {start|stop|restart} [interface]"
537     exit 1
538     ;;
539     esac