Magellan Linux

Diff of /trunk/initscripts/sysvinit/rc/network

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/unlabeled-1.1.1/magellan-initscripts/etc/rc.d/init.d/network revision 2 by niro, Mon Dec 13 22:52:07 2004 UTC trunk/magellan-initscripts/etc/rc.d/init.d/network revision 1108 by niro, Wed Jul 14 14:51:55 2010 UTC
# Line 1  Line 1 
1  #!/bin/bash  #!/bin/bash
2    # $Id$
3    
4  #%rlevels: 0:k 1:k 2:k 3:s 4:s 5:s 6:k  #%rlevels: 0:k 1:k 2:k 3:s 4:s 5:s 6:k
5  #%start: 20  #%start: 20
# Line 9  Line 10 
10  #%before:  #%before:
11  #%after:  #%after:
12    
13  source /etc/sysconfig/rc  source /etc/conf.d/rc
14  source $rc_functions  source ${rc_functions}
15    
16  checkconfig() {  # read values from files
17   if [ -z $NETWORKING ]  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     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    
75     source ${file}
76     eval value=\$$(echo ${var})
77     echo "${value}"
78    }
79    
80    checkconfig()
81    {
82     if [[ -z ${NETWORKING} ]]
83   then   then
84   echo "NETWORKING missing in net.${interface}, aborted"   echo "NETWORKING missing in net.${interface}, aborted"
85   exit 1   exit 1
86   fi   fi
87    
88   case $NETWORKING in   case "${NETWORKING}" in
89   static)   static)
90   if [ -z $IP ]   if [[ -z ${IP} ]]
91   then   then
92   echo "IP missing in net.${interface}, aborted"   echo "IP missing in net.${interface}, aborted"
93   exit 1   exit 1
94   fi   fi
95    
96   if [ -z $NETMASK ]   if [[ -z ${NETMASK} ]]
97   then   then
98   echo -n "NETMASK missing in net.${interface}, "   echo -n "NETMASK missing in net.${interface}, "
99   echo "using 255.255.255.0"   echo "using 255.255.255.0"
100   NETMASK=255.255.255.0   NETMASK=255.255.255.0
101   fi   fi
102    
103   if [ -z $BROADCAST ]   if [[ -z ${BROADCAST} ]]
104   then   then
105   echo -n "BROADCAST missing in net.${interface}, "   echo -n "BROADCAST missing in net.${interface}, "
106   echo "using default address"   echo "using default address"
107   fi   fi
108   ;;   ;;
109    
110   dhcp)   dhcp)
111   if [ -z $DHCP_PROG ]   if [[ -z ${DHCP_PROG} ]]
112   then   then
113   echo "DHCP_PROG missing in net.${interface}, aborted"   echo -n "DHCP_PROG missing in net.${interface},"
114   exit 1   echo "using default programm"
115     DHCP_PROG="/sbin/dhcpcd"
116   fi   fi
117     [[ -z ${DHCP_START} ]] && DHCP_START="-t 10"
118     [[ -z ${DHCP_STOP} ]] && DHCP_STOP="-k"
119   ;;   ;;
120    
121   esac   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  case "$1" in   echo "${devices}"
147   start)  }
148   for file in $(grep -il "ONBOOT=\"yes\"" $network_settings/net.*)  
149    config_wireless_wep()
150    {
151     local iface="$1"
152    
153     if [[ -z ${iface} ]]
154     then
155     echo "WEP: no \$iface given. Aborting setup."
156     return 1
157     fi
158    
159     ${CURS_UP}
160     ${SET_WWCOL}
161     echo "[AUTH: WEP]"
162    
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     echo "WPA: no \$iface given. Aborting setup."
176     return 1
177     fi
178    
179     if [ ! -x /sbin/wpa_supplicant ]
180     then
181     echo "WPA: wpa_supplicant not installed. Aborting setup."
182     return 1
183     fi
184    
185     ${CURS_UP}
186     ${SET_WWCOL}
187     echo "[AUTH: WPA]"
188    
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     # use wext as default driver, do not abort here anymore
197     [[ -z ${WIRELESS_WPA_DRIVER} ]] && WIRELESS_WPA_DRIVER=wext
198    
199     # 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     local wpa_proto
207     case ${WIRELESS_AUTH_MODE} in
208     wpa) wpa_proto="WPA" ;;
209     wpa2) wpa_proto="WPA2" ;;
210     esac
211    
212     # setup the network entry
213     sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
214     -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
215     -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
216     ${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     echo "    Waiting 5 seconds to retrieve authentification reply ... "
231     sleep 5
232    }
233    
234    setup_wireless_extensions()
235    {
236     local iface="$1"
237    
238     if [[ -z ${iface} ]]
239     then
240     echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
241     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     echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
253     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     wpa|wpa2) config_wireless_wpa "${iface}" ;;
264     wep|on) config_wireless_wep "${iface}" ;;
265     off) iwconfig "${iface}" enc off ;;
266     esac
267    }
268    
269    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     *) 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   do
318   interface=$(basename $file | sed s/net.//)   port="${i%=*}"
319   case "$interface" in   cost="${i#*=}"
320   *~) ;;   [[ ! -z ${port} ]] && brctl pathcost "${iface}" "${port}" "${cost}"
321   *)   done
322   #$network_devices/ifup $interface   fi
323   source $network_settings/net.${interface} || exit 1  
324   checkconfig   # configure port priority for every port
325   case $NETWORKING in   if [[ ! -z ${BRIDGE_PORT_PRIORITY} ]]
326   dhcp)   then
327   echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${interface}${COLDEFAULT} ..."   for i in ${BRIDGE_PORT_PRIORITY}
328   $CURS_UP   do
329   $SET_WWCOL   port="${i%=*}"
330   echo "[DHCP]"   prio="${i#*=}"
331   #modprobe $interface   [[ ! -z ${port} ]] && brctl setportprio "${iface}" "${port}" "${prio}"
332   loadproc $DHCP_PROG $DHCP_START   done
333   ;;   fi
334   static)  }
335   echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${interface}${COLDEFAULT} ..."  
336   $CURS_UP  config_bridge_devices()
337   $SET_WWCOL  {
338   echo "[STATIC]"   local iface="$1"
339   ifconfig $interface $IP netmask $NETMASK broadcast $BROADCAST   local method="$2"
340   evaluate_retval   local bport
341   ;;  
342   esac   if [[ -z ${iface} ]]
343   ;;   then
344     echo "BRIDGE: no \$iface given. Aborting setup."
345     return 1
346     fi
347    
348     if [[ -z ${method} ]]
349     then
350     echo "BRIDGE: no \$method given. Aborting setup."
351     return 1
352     fi
353    
354     # first check for brctl
355     if [[ -z $(which brctl) ]]
356     then
357     echo "brctl not found! Please install 'net-misc/bridge-utils'."
358     return 1
359     fi
360    
361     # check the config
362     if [[ -z ${BRIDGE_INTERFACES} ]]
363     then
364     echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
365     return 1
366     fi
367    
368     case ${method} in
369     add)
370     # setup the bridge device
371     brctl addbr "${iface}"
372     for bport in ${BRIDGE_INTERFACES}
373     do
374     # enter promiscous mode
375     ifconfig "${bport}" 0.0.0.0 promisc
376     # now setup the bridge
377     brctl addif "${iface}" "${bport}"
378     done
379     # configure all other options
380     config_bridge_options "${iface}"
381     ;;
382    
383     remove)
384     for bport in ${BRIDGE_INTERFACE}
385     do
386     # bring the interface down
387     ifconfig "${bport}" down
388     # remove the interface from the bridge
389     brctl delif "${iface}" "${bport}"
390     done
391     # bring the bridge down
392     brctl delbr "${iface}"
393     ;;
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     add) message="Adding route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
409     del) message="Removing route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
410     *)
411     echo "config_routes: unsupported \$method '${method}'."
412     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   esac
425     echo -e ${COLOREDSTAR}"${message}"
426     route "${method}" "${route}"
427     evaluate_retval
428   done   done
429     fi
430    }
431    
432    networking_start()
433    {
434     local iface dns routes ALL_INTERFACES
435    
436     if [[ -z $1 ]]
437     then
438     ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
439     else
440     if [[ -e ${rc_network_settings}/net.$1 ]]
441     then
442     ALL_INTERFACES="$1"
443     else
444     ${FAILURE}
445     echo "Interface $1 does not exist. Aborting"
446     ${NORMAL}
447     exit 1
448     fi
449     fi
450    
451   if [ "$GATEWAY" != "" ]   # get list of all devices
452     for iface in ${ALL_INTERFACES}
453     do
454     # checkconfig
455     source ${rc_network_settings}/net.${iface} || exit 1
456     checkconfig
457    
458     # setup mac
459     if [[ -n ${FORCE_MAC_TO} ]]
460   then   then
461   echo -e ${COLOREDSTAR}"Setting up default gateway ..."   echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
462   route add default gateway $GATEWAY metric 1 \   ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
  dev $GATEWAY_IF  
463   evaluate_retval   evaluate_retval
464   fi   fi
465    
466   update_svcstatus $1   # setup bridges
467   splash svc_started "$(basename $0)" 0   if [[ ${iface} = br[0-9]* ]]
468   ;;   then
469     config_bridge_devices "${iface}" add
470     fi
471    
472   stop)   # now configure wireless_extensions
473   if [ "$GATEWAY" != "" ]   [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
474    
475     echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
476    
477     # activate the interface
478     ifconfig "${iface}" up
479    
480     # setup static or dhcp
481     case ${NETWORKING} in
482     dhcp|DHCP)
483     ${CURS_UP}
484     ${SET_WWCOL}
485     echo "[DHCP]"
486     loadproc "${DHCP_PROG}" "${DHCP_START}" "${iface}"
487     ;;
488     static|STATIC)
489     ${CURS_UP}
490     ${SET_WWCOL}
491     echo "[STATIC]"
492     ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}"
493     evaluate_retval
494     ;;
495     esac
496    
497     # setup def gw
498     if [[ -n ${GATEWAY} ]]
499     then
500     echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
501     route add default gateway "${GATEWAY}" metric 1 dev "${iface}"
502     evaluate_retval
503    
504     unset GATEWAY
505     fi
506    
507     # setup /etc/resolv.conf
508     # add given nameserver
509     if [[ -n ${NAMESERVER} ]]
510     then
511     echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
512    
513     # whipe out the old one
514     echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
515     # include head
516     if [ -f /etc/resolv.conf.head ]
517     then
518     cat /etc/resolv.conf.head >> /etc/resolv.conf
519     else
520     echo "# /etc/resolv.conf.head can replace this line" >> /etc/resolv.conf
521     fi
522    
523     for dns in ${NAMESERVER}
524     do
525     echo "nameserver ${dns}" >> /etc/resolv.conf
526     done
527    
528     # include tail
529     if [ -f /etc/resolv.conf.tail ]
530     then
531     cat /etc/resolv.conf.tail >> /etc/resolv.conf
532     else
533     echo "# /etc/resolv.conf.tail can replace this line" >> /etc/resolv.conf
534     fi
535    
536     unset NAMESERVER
537     fi
538     done
539    
540     # setup user routes
541     config_routes add
542    }
543    
544    networking_stop()
545    {
546     if [[ -z $1 ]]
547     then
548     ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
549     else
550     if [[ -e ${rc_network_settings}/net.$1 ]]
551     then
552     ALL_INTERFACES="$1"
553     else
554     ${FAILURE}
555     echo "Interface $1 does not exist. Aborting"
556     ${NORMAL}
557     exit 1
558     fi
559     fi
560    
561     # get list of all devices
562     for iface in ${ALL_INTERFACES}
563     do
564     source ${rc_network_settings}/net.${iface} || exit 1
565     checkconfig
566    
567     if [[ -n ${GATEWAY} ]]
568   then   then
569   echo -e ${COLOREDSTAR}"Removing default gateway ..."   echo -e ${COLOREDSTAR}"Removing default gateway ..."
570   route del -net default   route del -net default
571   evaluate_retval   evaluate_retval
572   fi   fi
573    
574   for file in $(grep -il "ONBOOT=\"yes\"" $network_settings/net.*)   echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
575   do   ifconfig ${iface} down
576   interface=$(basename $file | sed s/net.//)   evaluate_retval
577   case "$interface" in  
578   *~) ;;   # remove bridges
579   *)   if [[ ${iface} = br[0-9]* ]]
580   #$network_devices/ifdown $interface   then
581   source $network_settings/net.${interface} || exit 1   config_bridge_devices "${iface}" remove
582   checkconfig   fi
583   echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${interface}${COLDEFAULT} ..."  
584   ifconfig eth0 down   # shutdown dhcp-daemon
585   evaluate_retval   if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
586     then
587   #shutdown dhcp-daemon   echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..."
588   if [ $NETWORKING == dhcp ]   ${CURS_UP}
589   then   ${SET_WWCOL}
590   my_runlevel="`runlevel | cut -d ' ' -f2`"   echo "[$(basename ${DHCP_PROG})]"
591   if [ "$my_runlevel" -ne "0" -a "$my_runlevel" -ne "6" ]   ${DHCP_PROG} ${DHCP_STOP} "${iface}"
592   then   evaluate_retval
593   echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..."   fi
594   $CURS_UP  
595   $SET_WWCOL   # shutdown wpa_supplicant daemon
596   echo "[${DHCP_PROG}]"   if [[ -n $(pidof wpa_supplicant) ]]
597   $DHCP_PROG $DHCP_STOP   then
598   evaluate_retval   killall wpa_supplicant
599   fi   fi
600   fi   done
601   ;;  
602   esac   # remove state dir
603   done   if [ -d /var/run/wpa_supplicant ]
604     then
605     rm -rf /var/run/wpa_supplicant
606     fi
607    
608     # delete user routes
609     config_routes del
610    }
611    
612    case $1 in
613     start)
614     networking_start $2
615     update_svcstatus $1
616     splash svc_started "$(basename $0)" 0
617     ;;
618    
619     stop)
620     networking_stop $2
621   update_svcstatus $1   update_svcstatus $1
622   splash svc_stopped "$(basename $0)" 0   splash svc_stopped "$(basename $0)" 0
623   ;;   ;;
# Line 146  case "$1" in Line 629  case "$1" in
629   ;;   ;;
630    
631   *)   *)
632   echo "Usage: $0 {start|stop|restart}"   echo "Usage: $0 {start|stop|restart} [interface]"
633   exit 1   exit 1
634   ;;   ;;
635  esac  esac

Legend:
Removed from v.2  
changed lines
  Added in v.1108