Magellan Linux

Annotation of /trunk/initscripts/sysvinit/rc/network

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *