Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 867 - (hide annotations) (download)
Sat May 9 16:08:38 2009 UTC (15 years ago) by niro
Original Path: trunk/magellan-initscripts/etc/rc.d/init.d/network
File size: 11324 byte(s)
added support for WPA2 encryption
1 niro 2 #!/bin/bash
2 niro 781 # $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 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     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 niro 867 local wpa_proto
199     case ${WIRELESS_AUTH_MODE} in
200     wpa) wpa_proto="WPA" ;;
201     wpa2) wpa_proto="WPA2" ;;
202     esac
203    
204 niro 268 # setup the network entry
205     sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
206 niro 867 -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
207     -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
208 niro 268 ${WIRELESS_WPA_CONFIG}
209     fi
210    
211 niro 275 # remove old state dir
212     [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant
213    
214 niro 268 # 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 niro 270
221     # echo wait 5 seconds
222     echo " Waiting 5 seconds to retrieve authentification reply ... "
223     sleep 5
224 niro 268 }
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 niro 275 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 niro 270
247 niro 268 [[ -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 niro 867 wpa|wpa2) config_wireless_wpa "${iface}" ;;
256     wep|on) config_wireless_wep "${iface}" ;;
257     off) iwconfig "${iface}" enc off ;;
258 niro 268 esac
259     }
260    
261 niro 506 config_bridge_devices()
262     {
263     local iface="$1"
264     local method="$2"
265 niro 636 local bport
266 niro 506
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 niro 638 if [[ -z ${BRIDGE_INTERFACES} ]]
288 niro 506 then
289 niro 638 echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
290 niro 506 return 1
291     fi
292    
293     case ${method} in
294     add)
295     # setup the bridge device
296     brctl addbr ${iface}
297 niro 636 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 niro 640 case ${BRIDGE_STP} in
306 niro 636 on|off) brctl stp ${iface} ${BRIDGE_STP} ;;
307     *) echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
308     esac
309 niro 506 ;;
310 niro 636
311 niro 506 remove)
312 niro 636 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 niro 506 # bring the bridge down
320     brctl delbr ${iface}
321     ;;
322     esac
323 niro 636
324 niro 506 # unset the bridge variable to be safe
325 niro 636 unset BRIDGE_INTERFACES
326 niro 506 # 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 niro 243 networking_start()
361     {
362 niro 522 local iface dns routes ALL_INTERFACES
363 niro 243
364 niro 522 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 niro 243 # get list of all devices
380 niro 522 for iface in ${ALL_INTERFACES}
381 niro 243 do
382     # checkconfig
383     source ${network_settings}/net.${iface} || exit 1
384     checkconfig
385    
386 niro 270 # 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 niro 506 # setup bridges
395     if [[ ${iface} = br[0-9]* ]]
396     then
397     config_bridge_devices ${iface} add
398     fi
399    
400 niro 270 # activate the interface
401     ifconfig "${iface}" up
402    
403     # now configure wireless_extensions
404     [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
405    
406 niro 243 echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
407    
408     # setup static or dhcp
409     case ${NETWORKING} in
410     dhcp|DHCP)
411     ${CURS_UP}
412     ${SET_WWCOL}
413     echo "[DHCP]"
414 niro 275 loadproc ${DHCP_PROG} ${DHCP_START} "${iface}"
415 niro 2 ;;
416 niro 243 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 niro 2
425 niro 243 # setup def gw
426     if [[ -n ${GATEWAY} ]]
427 niro 2 then
428 niro 245 echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
429 niro 243 route add default gateway ${GATEWAY} metric 1 dev ${iface}
430 niro 2 evaluate_retval
431 niro 684
432     unset GATEWAY
433 niro 2 fi
434    
435 niro 245 # 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 niro 684
447     unset NAMESERVER
448 niro 245 fi
449 niro 243 done
450 niro 506
451     # setup user routes
452     config_routes add
453 niro 243 }
454 niro 2
455 niro 243 networking_stop()
456     {
457 niro 522 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 niro 243 # get list of all devices
473 niro 522 for iface in ${ALL_INTERFACES}
474 niro 243 do
475     source ${network_settings}/net.${iface} || exit 1
476     checkconfig
477    
478     if [[ -n ${GATEWAY} ]]
479 niro 2 then
480     echo -e ${COLOREDSTAR}"Removing default gateway ..."
481     route del -net default
482     evaluate_retval
483     fi
484    
485 niro 243 echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
486     ifconfig ${iface} down
487     evaluate_retval
488 niro 2
489 niro 506 # remove bridges
490     if [[ ${iface} = br[0-9]* ]]
491     then
492     config_bridge_devices ${iface} remove
493     fi
494    
495 niro 243 # 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 niro 270 ${DHCP_PROG} ${DHCP_STOP} "${iface}"
503 niro 243 evaluate_retval
504     fi
505 niro 268
506     # shutdown wpa_supplicant daemon
507     if [[ -n $(pidof wpa_supplicant) ]]
508     then
509     killall wpa_supplicant
510     fi
511 niro 243 done
512 niro 275
513 niro 270 # remove state dir
514     if [ -d /var/run/wpa_supplicant ]
515     then
516     rm -rf /var/run/wpa_supplicant
517     fi
518 niro 506
519     # delete user routes
520     config_routes del
521 niro 243 }
522 niro 2
523 niro 243 case $1 in
524     start)
525 niro 522 networking_start $2
526 niro 2 update_svcstatus $1
527 niro 243 splash svc_started "$(basename $0)" 0
528     ;;
529    
530     stop)
531 niro 522 networking_stop $2
532 niro 243 update_svcstatus $1
533 niro 2 splash svc_stopped "$(basename $0)" 0
534     ;;
535    
536     restart)
537     $0 stop
538     sleep 1
539     $0 start
540     ;;
541    
542     *)
543 niro 522 echo "Usage: $0 {start|stop|restart} [interface]"
544 niro 2 exit 1
545     ;;
546     esac

Properties

Name Value
svn:executable *