Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1227 - (hide annotations) (download)
Thu Feb 17 22:24:59 2011 UTC (13 years, 2 months ago) by niro
Original Path: trunk/busybox-initscripts/rc/network
File size: 13330 byte(s)
-fixed a escaping issues with the routes
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 niro 1193 local BRIDGE_AGEING_TIME
68     local BRIDGE_PRIORITY
69     local BRIDGE_FORWARD_DELAY
70     local BRIDGE_HELLO_TIME
71     local BRIDGE_MAX_MESSAGE_AGE
72     local BRIDGE_PATH_COST
73     local BRIDGE_PORT_PRIORITY
74 niro 724
75     source ${file}
76     eval value=\$$(echo ${var})
77     echo "${value}"
78     }
79    
80     checkconfig()
81     {
82     if [[ -z ${NETWORKING} ]]
83     then
84 niro 1193 rc_echo "NETWORKING missing in net.${iface}, aborted"
85 niro 724 exit 1
86     fi
87    
88     case "${NETWORKING}" in
89     static)
90     if [[ -z ${IP} ]]
91     then
92 niro 1193 rc_echo "IP missing in net.${iface}, aborted"
93 niro 724 exit 1
94     fi
95    
96     if [[ -z ${NETMASK} ]]
97     then
98 niro 1193 rc_echo -n "NETMASK missing in net.${iface}, "
99 niro 1168 rc_echo "using 255.255.255.0"
100 niro 724 NETMASK=255.255.255.0
101     fi
102    
103     if [[ -z ${BROADCAST} ]]
104     then
105 niro 1193 rc_echo -n "BROADCAST missing in net.${iface}, "
106 niro 1168 rc_echo "using default address"
107 niro 724 fi
108     ;;
109    
110     dhcp)
111     if [[ -z ${DHCP_PROG} ]]
112     then
113 niro 1193 rc_echo -n "DHCP_PROG missing in net.${iface},"
114     rc_echo "using default programm /sbin/udhcpc"
115     DHCP_PROG="/sbin/udhcpc"
116     fi
117 niro 1203 [[ -z ${DHCP_START} ]] && DHCP_START="-T10"
118 niro 1193 [[ -z ${DHCP_STOP} ]] && DHCP_STOP="-k"
119 niro 724 ;;
120    
121     esac
122     }
123    
124     # onboot_interface_list /path/to/files*
125     onboot_interface_list()
126     {
127     local file
128     local devices
129     local iface
130    
131     # get list of all devices
132     for file in $@
133     do
134     if [[ $(read_value ONBOOT ${file}) = yes ]]
135     then
136     iface="$(basename ${file} | sed s/net.//)"
137     # exclude backup files and exclude net.routes too
138     case "${iface}" in
139     *~) ;;
140     */net.routes) ;;
141     *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;
142     esac
143     fi
144     done
145    
146     echo "${devices}"
147     }
148    
149     config_wireless_wep()
150     {
151     local iface="$1"
152    
153     if [[ -z ${iface} ]]
154     then
155 niro 1168 rc_echo "WEP: no \$iface given. Aborting setup."
156 niro 724 return 1
157     fi
158    
159     ${CURS_UP}
160     ${SET_WWCOL}
161 niro 1168 rc_echo "[AUTH: WEP]"
162 niro 724
163     iwconfig "${iface}" enc on
164     [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}"
165     [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}"
166     [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}"
167     }
168    
169     config_wireless_wpa()
170     {
171     local iface="$1"
172    
173     if [[ -z ${iface} ]]
174     then
175 niro 1168 rc_echo "WPA: no \$iface given. Aborting setup."
176 niro 724 return 1
177     fi
178    
179     if [ ! -x /sbin/wpa_supplicant ]
180     then
181 niro 1168 rc_echo "WPA: wpa_supplicant not installed. Aborting setup."
182 niro 724 return 1
183     fi
184    
185     ${CURS_UP}
186     ${SET_WWCOL}
187 niro 1168 rc_echo "[AUTH: WPA]"
188 niro 724
189     # get default settings
190     [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant
191    
192     # check the configuration
193     [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto
194     [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel
195    
196 niro 1193 # use wext as default driver, do not abort here anymore
197     [[ -z ${WIRELESS_WPA_DRIVER} ]] && WIRELESS_WPA_DRIVER=wext
198    
199 niro 724 # write a config with the settings from net.${iface}
200     # only wpa-psk ! all other needs manual setup
201     if [[ ${WIRELESS_WPA_AUTOCONF} = true ]]
202     then
203     # write default cfg from skeleton
204     cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG}
205    
206 niro 1193 local wpa_proto
207     case ${WIRELESS_AUTH_MODE} in
208     wpa) wpa_proto="WPA" ;;
209     wpa2) wpa_proto="WPA2" ;;
210     esac
211    
212 niro 724 # setup the network entry
213     sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
214 niro 1193 -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
215     -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
216 niro 724 ${WIRELESS_WPA_CONFIG}
217     fi
218    
219     # remove old state dir
220     [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant
221    
222     # now run the wpa_supplicant dameon
223     wpa_supplicant -B \
224     -D"${WIRELESS_WPA_DRIVER}" \
225     -c"${WIRELESS_WPA_CONFIG}" \
226     -i"${iface}" \
227     ${WIRELESS_WPA_OPTS}
228    
229     # echo wait 5 seconds
230 niro 1168 rc_echo " Waiting 5 seconds to retrieve authentification reply ... "
231 niro 724 sleep 5
232     }
233    
234     setup_wireless_extensions()
235     {
236     local iface="$1"
237    
238     if [[ -z ${iface} ]]
239     then
240 niro 1168 rc_echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
241 niro 724 return 1
242     fi
243    
244     if [[ -n ${WIRELESS_BITRATE} ]] ||
245     [[ -n ${WIRELESS_CHANNEL} ]] ||
246     [[ -n ${WIRELESS_ESSID} ]] ||
247     [[ -n ${WIRELESS_FREQUENCY} ]] ||
248     [[ -n ${WIRELESS_MODE} ]] ||
249     [[ -n ${WIRELESS_NICK} ]] ||
250     [[ -n ${WIRELESS_AUTH_MODE} ]]
251     then
252 niro 1168 rc_print "Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
253 niro 724 fi
254    
255     [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"
256     [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"
257     [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}"
258     [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}"
259     [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}"
260     [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"
261    
262     case "${WIRELESS_AUTH_MODE}" in
263 niro 1193 wpa|wpa2) config_wireless_wpa "${iface}" ;;
264     wep|on) config_wireless_wep "${iface}" ;;
265     off) iwconfig "${iface}" enc off ;;
266 niro 724 esac
267     }
268    
269 niro 1193 config_bridge_options()
270     {
271     local iface="$1"
272     local i
273     local port
274     local cost
275     local prio
276    
277     # enable spanning-tree protocol
278     case ${BRIDGE_STP} in
279     on|off) brctl stp "${iface}" "${BRIDGE_STP}" ;;
280     *) rc_echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
281     esac
282    
283     # configure ageing time
284     if [[ ! -z ${BRIDGE_AGEING_TIME} ]]
285     then
286     brctl setageing "${iface}" "${BRIDGE_AGEING_TIME}"
287     fi
288    
289     # configure bridge priority
290     if [[ ! -z ${BRIDGE_PRIORITY} ]]
291     then
292     brctl setbridgeprio "${iface}" "${BRIDGE_PRIORITY}"
293     fi
294    
295     # configure forward delay
296     if [[ ! -z ${BRIDGE_FORWARD_DELAY} ]]
297     then
298     brctl setfd "${iface}" "${BRIDGE_FORWARD_DELAY}"
299     fi
300    
301     # configure hello time
302     if [[ ! -z ${BRIDGE_HELLO_TIME} ]]
303     then
304     brctl sethello "${iface}" "${BRIDGE_HELLO_TIME}"
305     fi
306    
307     # configure maximal message age
308     if [[ ! -z ${BRIDGE_MAX_MESSAGE_AGE} ]]
309     then
310     brctl setmaxage "${iface}" "${BRIDGE_MAX_MESSAGE_AGE}"
311     fi
312    
313     # configure path cost for every port
314     if [[ ! -z ${BRIDGE_PATH_COST} ]]
315     then
316     for i in ${BRIDGE_PATH_COST}
317     do
318     port="${i%=*}"
319     cost="${i#*=}"
320     [[ ! -z ${port} ]] && brctl pathcost "${iface}" "${port}" "${cost}"
321     done
322     fi
323    
324     # configure port priority for every port
325     if [[ ! -z ${BRIDGE_PORT_PRIORITY} ]]
326     then
327     for i in ${BRIDGE_PORT_PRIORITY}
328     do
329     port="${i%=*}"
330     prio="${i#*=}"
331     [[ ! -z ${port} ]] && brctl setportprio "${iface}" "${port}" "${prio}"
332     done
333     fi
334     }
335    
336 niro 724 config_bridge_devices()
337     {
338     local iface="$1"
339     local method="$2"
340     local bport
341    
342     if [[ -z ${iface} ]]
343     then
344 niro 1168 rc_echo "BRIDGE: no \$iface given. Aborting setup."
345 niro 724 return 1
346     fi
347    
348     if [[ -z ${method} ]]
349     then
350 niro 1168 rc_echo "BRIDGE: no \$method given. Aborting setup."
351 niro 724 return 1
352     fi
353    
354     # first check for brctl
355     if [[ -z $(which brctl) ]]
356     then
357 niro 1168 rc_echo "brctl not found! Please install 'net-misc/bridge-utils'."
358 niro 724 return 1
359     fi
360    
361     # check the config
362     if [[ -z ${BRIDGE_INTERFACES} ]]
363     then
364 niro 1168 rc_echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
365 niro 724 return 1
366     fi
367    
368     case ${method} in
369     add)
370     # setup the bridge device
371 niro 1193 brctl addbr "${iface}"
372 niro 724 for bport in ${BRIDGE_INTERFACES}
373     do
374     # enter promiscous mode
375 niro 1193 ifconfig "${bport}" 0.0.0.0 promisc
376 niro 724 # now setup the bridge
377 niro 1193 brctl addif "${iface}" "${bport}"
378 niro 724 done
379 niro 1193 # configure all other options
380     config_bridge_options "${iface}"
381 niro 724 ;;
382    
383     remove)
384     for bport in ${BRIDGE_INTERFACE}
385     do
386     # bring the interface down
387 niro 1193 ifconfig "${bport}" down
388 niro 724 # remove the interface from the bridge
389 niro 1193 brctl delif "${iface}" "${bport}"
390 niro 724 done
391     # bring the bridge down
392 niro 1193 brctl delbr "${iface}"
393 niro 724 ;;
394     esac
395    
396     # unset the bridge variable to be safe
397     unset BRIDGE_INTERFACES
398     # continue to setup generic networking
399     }
400    
401     config_routes()
402     {
403     local method="$1"
404     local message
405    
406     # only add and del are allowed
407     case ${method} in
408 niro 1227 add) message="Adding" ;;
409     del) message="Removing" ;;
410 niro 724 *)
411 niro 1168 rc_echo "config_routes: unsupported \$method '${method}'."
412 niro 724 exit 1
413     ;;
414     esac
415    
416     # adds/delete user routes
417     if [[ -f /etc/conf.d/net.routes ]]
418     then
419     ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end
420     while read route
421     do
422     case "${route}" in
423     \#*|"") continue ;;
424     esac
425 niro 1227 rc_print "${message} route ${COLBLUE}${route}${COLDEFAULT} ..."
426     # do not esacpe ${route} or it breaks!
427     route "${method}" ${route}
428 niro 724 evaluate_retval
429     done
430     fi
431     }
432    
433     networking_start()
434     {
435     local iface dns routes ALL_INTERFACES
436    
437     if [[ -z $1 ]]
438     then
439 niro 1193 ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
440 niro 724 else
441 niro 1193 if [[ -e ${rc_network_settings}/net.$1 ]]
442 niro 724 then
443     ALL_INTERFACES="$1"
444     else
445     ${FAILURE}
446 niro 1168 rc_echo "Interface $1 does not exist. Aborting"
447 niro 724 ${NORMAL}
448     exit 1
449     fi
450     fi
451    
452     # get list of all devices
453     for iface in ${ALL_INTERFACES}
454     do
455     # checkconfig
456 niro 1193 source ${rc_network_settings}/net.${iface} || exit 1
457 niro 724 checkconfig
458    
459     # setup mac
460 niro 1193 if [[ -n ${FORCE_MAC_TO} ]]
461 niro 724 then
462 niro 1168 rc_print "Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
463 niro 724 ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
464     evaluate_retval
465     fi
466    
467     # setup bridges
468     if [[ ${iface} = br[0-9]* ]]
469     then
470 niro 1193 config_bridge_devices "${iface}" add
471 niro 724 fi
472    
473     # now configure wireless_extensions
474     [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
475    
476 niro 1168 rc_print "Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
477 niro 724
478 niro 1193 # activate the interface
479     ifconfig "${iface}" up
480    
481 niro 724 # setup static or dhcp
482     case ${NETWORKING} in
483     dhcp|DHCP)
484     ${CURS_UP}
485     ${SET_WWCOL}
486 niro 1168 rc_echo "[DHCP]"
487 niro 1193 loadproc "${DHCP_PROG}" "${DHCP_START}" "${iface}"
488 niro 724 ;;
489     static|STATIC)
490     ${CURS_UP}
491     ${SET_WWCOL}
492 niro 1168 rc_echo "[STATIC]"
493 niro 724 ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}"
494     evaluate_retval
495     ;;
496     esac
497    
498     # setup def gw
499     if [[ -n ${GATEWAY} ]]
500     then
501 niro 1168 rc_print "Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
502 niro 1193 route add default gateway "${GATEWAY}" metric 1 dev "${iface}"
503 niro 724 evaluate_retval
504    
505     unset GATEWAY
506     fi
507    
508     # setup /etc/resolv.conf
509 niro 1193 # add given nameserver
510 niro 724 if [[ -n ${NAMESERVER} ]]
511     then
512 niro 1168 rc_print "Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
513    
514 niro 724 # whipe out the old one
515     echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
516 niro 1193 # include head
517     if [ -f /etc/resolv.conf.head ]
518     then
519     cat /etc/resolv.conf.head >> /etc/resolv.conf
520     else
521     echo "# /etc/resolv.conf.head can replace this line" >> /etc/resolv.conf
522     fi
523    
524 niro 724 for dns in ${NAMESERVER}
525     do
526     echo "nameserver ${dns}" >> /etc/resolv.conf
527     done
528    
529 niro 1193 # include tail
530     if [ -f /etc/resolv.conf.tail ]
531     then
532     cat /etc/resolv.conf.tail >> /etc/resolv.conf
533     else
534     echo "# /etc/resolv.conf.tail can replace this line" >> /etc/resolv.conf
535     fi
536    
537 niro 724 unset NAMESERVER
538     fi
539     done
540    
541     # setup user routes
542     config_routes add
543     }
544    
545     networking_stop()
546     {
547     if [[ -z $1 ]]
548     then
549 niro 1193 ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
550 niro 724 else
551 niro 1193 if [[ -e ${rc_network_settings}/net.$1 ]]
552 niro 724 then
553     ALL_INTERFACES="$1"
554     else
555     ${FAILURE}
556 niro 1168 rc_echo "Interface $1 does not exist. Aborting"
557 niro 724 ${NORMAL}
558     exit 1
559     fi
560     fi
561    
562     # get list of all devices
563     for iface in ${ALL_INTERFACES}
564     do
565 niro 1193 source ${rc_network_settings}/net.${iface} || exit 1
566 niro 724 checkconfig
567    
568     if [[ -n ${GATEWAY} ]]
569     then
570 niro 1168 rc_print "Removing default gateway ..."
571 niro 724 route del -net default
572     evaluate_retval
573     fi
574    
575 niro 1168 rc_print "Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
576 niro 1193 ifconfig "${iface}" down
577 niro 724 evaluate_retval
578    
579     # remove bridges
580     if [[ ${iface} = br[0-9]* ]]
581     then
582 niro 1193 config_bridge_devices "${iface}" remove
583 niro 724 fi
584    
585     # shutdown dhcp-daemon
586     if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
587     then
588 niro 1168 rc_print "Stopping the dhcp-daemon ..."
589 niro 724 ${CURS_UP}
590     ${SET_WWCOL}
591 niro 1168 rc_echo "[$(basename ${DHCP_PROG})]"
592 niro 724 ${DHCP_PROG} ${DHCP_STOP} "${iface}"
593     evaluate_retval
594     fi
595    
596     # shutdown wpa_supplicant daemon
597     if [[ -n $(pidof wpa_supplicant) ]]
598     then
599     killall wpa_supplicant
600     fi
601     done
602    
603     # remove state dir
604     if [ -d /var/run/wpa_supplicant ]
605     then
606     rm -rf /var/run/wpa_supplicant
607     fi
608    
609     # delete user routes
610     config_routes del
611     }
612    
613     case $1 in
614     start)
615     networking_start $2
616     update_svcstatus $1
617     splash svc_started "$(basename $0)" 0
618     ;;
619    
620     stop)
621     networking_stop $2
622     update_svcstatus $1
623     splash svc_stopped "$(basename $0)" 0
624     ;;
625    
626     restart)
627     $0 stop
628     sleep 1
629     $0 start
630     ;;
631    
632     *)
633 niro 1193 rc_echo "Usage: $0 {start|stop|restart} [interface]"
634 niro 724 exit 1
635     ;;
636     esac