Magellan Linux

Diff of /trunk/magellan-initscripts/etc/rc.d/init.d/network

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

revision 270 by niro, Tue Oct 11 11:19:58 2005 UTC revision 1090 by niro, Wed Jul 14 07:25:22 2010 UTC
# Line 1  Line 1 
1  #!/bin/bash  #!/bin/bash
2  # $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/network,v 1.10 2005-10-11 11:19:58 niro Exp $  # $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 10  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  # read values from files  # read values from files
# Line 24  read_value() Line 24  read_value()
24   # global   # global
25   local ONBOOT   local ONBOOT
26   local NETWORKING   local NETWORKING
27    
28   # static   # static
29   local IP   local IP
30   local NETMASK   local NETMASK
# Line 60  read_value() Line 60  read_value()
60   local WIRELESS_NICK   local WIRELESS_NICK
61   local WIRELESS_NWID   local WIRELESS_NWID
62   local WIRELESS_POWER   local WIRELESS_POWER
63     local WIRELESS_WPA_DRIVER
64    
65     local BRIDGE_INTERFACES
66     local BRIDGE_STP
67    
68   source ${file}   source ${file}
69   eval value=\$$(echo ${var})   eval value=\$$(echo ${var})
# Line 68  read_value() Line 72  read_value()
72    
73  checkconfig()  checkconfig()
74  {  {
75   if [ -z "${NETWORKING}" ]   if [[ -z ${NETWORKING} ]]
76   then   then
77   echo "NETWORKING missing in net.${interface}, aborted"   echo "NETWORKING missing in net.${interface}, aborted"
78   exit 1   exit 1
# Line 76  checkconfig() Line 80  checkconfig()
80    
81   case "${NETWORKING}" in   case "${NETWORKING}" in
82   static)   static)
83   if [ -z "${IP}" ]   if [[ -z ${IP} ]]
84   then   then
85   echo "IP missing in net.${interface}, aborted"   echo "IP missing in net.${interface}, aborted"
86   exit 1   exit 1
87   fi   fi
88    
89   if [ -z "${NETMASK}" ]   if [[ -z ${NETMASK} ]]
90   then   then
91   echo -n "NETMASK missing in net.${interface}, "   echo -n "NETMASK missing in net.${interface}, "
92   echo "using 255.255.255.0"   echo "using 255.255.255.0"
93   NETMASK=255.255.255.0   NETMASK=255.255.255.0
94   fi   fi
95    
96   if [ -z "${BROADCAST}" ]   if [[ -z ${BROADCAST} ]]
97   then   then
98   echo -n "BROADCAST missing in net.${interface}, "   echo -n "BROADCAST missing in net.${interface}, "
99   echo "using default address"   echo "using default address"
# Line 97  checkconfig() Line 101  checkconfig()
101   ;;   ;;
102    
103   dhcp)   dhcp)
104   if [ -z "${DHCP_PROG}" ]   if [[ -z ${DHCP_PROG} ]]
105   then   then
106   echo "DHCP_PROG missing in net.${interface}, aborted"   echo -n "DHCP_PROG missing in net.${interface},"
107   exit 1   echo "using default programm"
108     DHCP_PROG="/sbin/dhcpcd"
109   fi   fi
110     [[ -z ${DHCP_START} ]] && DHCP_START="-t 10"
111     [[ -z ${DHCP_STOP} ]] && DHCP_STOP="-k"
112   ;;   ;;
113    
114   esac   esac
# Line 120  onboot_interface_list() Line 127  onboot_interface_list()
127   if [[ $(read_value ONBOOT ${file}) = yes ]]   if [[ $(read_value ONBOOT ${file}) = yes ]]
128   then   then
129   iface="$(basename ${file} | sed s/net.//)"   iface="$(basename ${file} | sed s/net.//)"
130   # exclude backup files   # exclude backup files and exclude net.routes too
131   case "${iface}" in   case "${iface}" in
132   *~) ;;   *~) ;;
133     */net.routes) ;;
134   *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;   *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;
135   esac   esac
136   fi   fi
# Line 177  config_wireless_wpa() Line 185  config_wireless_wpa()
185   # check the configuration   # check the configuration
186   [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto   [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto
187   [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel   [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel
188   if [[ -z ${WIRELESS_WPA_DRIVER} ]]  
189   then   # use wext as default driver, do not abort here anymore
190   echo "WPA: WIRELESS_WPA_DRIVER given. Aborting setup."   [[ -z ${WIRELESS_WPA_DRIVER} ]] && WIRELESS_WPA_DRIVER=wext
  return 1  
  fi  
191    
192   # write a config with the settings from net.${iface}   # write a config with the settings from net.${iface}
193   # only wpa-psk ! all other needs manual setup   # only wpa-psk ! all other needs manual setup
# Line 190  config_wireless_wpa() Line 196  config_wireless_wpa()
196   # write default cfg from skeleton   # write default cfg from skeleton
197   cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG}   cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG}
198    
199     local wpa_proto
200     case ${WIRELESS_AUTH_MODE} in
201     wpa) wpa_proto="WPA" ;;
202     wpa2) wpa_proto="WPA2" ;;
203     esac
204    
205   # setup the network entry   # setup the network entry
206   sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \   sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
207   -e "s:@WIRELESS_KEY@:${WIRELESS_KEY}:g" \   -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
208     -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
209   ${WIRELESS_WPA_CONFIG}   ${WIRELESS_WPA_CONFIG}
210   fi   fi
211    
212     # remove old state dir
213     [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant
214    
215   # now run the wpa_supplicant dameon   # now run the wpa_supplicant dameon
216   wpa_supplicant -B \   wpa_supplicant -B \
217   -D"${WIRELESS_WPA_DRIVER}" \   -D"${WIRELESS_WPA_DRIVER}" \
# Line 218  setup_wireless_extensions() Line 234  setup_wireless_extensions()
234   return 1   return 1
235   fi   fi
236    
237   echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "   if [[ -n ${WIRELESS_BITRATE} ]] ||
238     [[ -n ${WIRELESS_CHANNEL} ]] ||
239     [[ -n ${WIRELESS_ESSID} ]] ||
240     [[ -n ${WIRELESS_FREQUENCY} ]] ||
241     [[ -n ${WIRELESS_MODE} ]] ||
242     [[ -n ${WIRELESS_NICK} ]] ||
243     [[ -n ${WIRELESS_AUTH_MODE} ]]
244     then
245     echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
246     fi
247    
248   [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"   [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"
249   [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"   [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"
# Line 228  setup_wireless_extensions() Line 253  setup_wireless_extensions()
253   [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"   [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"
254    
255   case "${WIRELESS_AUTH_MODE}" in   case "${WIRELESS_AUTH_MODE}" in
256   wpa) config_wireless_wpa "${iface}" ;;   wpa|wpa2) config_wireless_wpa "${iface}" ;;
257   wep|on) config_wireless_wep "${iface}" ;;   wep|on) config_wireless_wep "${iface}" ;;
258   off) iwconfig "${iface}" enc off ;;   off) iwconfig "${iface}" enc off ;;
259     esac
260    }
261    
262    config_bridge_devices()
263    {
264     local iface="$1"
265     local method="$2"
266     local bport
267    
268     if [[ -z ${iface} ]]
269     then
270     echo "BRIDGE: no \$iface given. Aborting setup."
271     return 1
272     fi
273    
274     if [[ -z ${method} ]]
275     then
276     echo "BRIDGE: no \$method given. Aborting setup."
277     return 1
278     fi
279    
280     # first check for brctl
281     if [[ -z $(which brctl) ]]
282     then
283     echo "brctl not found! Please install 'net-misc/bridge-utils'."
284     return 1
285     fi
286    
287     # check the config
288     if [[ -z ${BRIDGE_INTERFACES} ]]
289     then
290     echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
291     return 1
292     fi
293    
294     case ${method} in
295     add)
296     # setup the bridge device
297     brctl addbr ${iface}
298     for bport in ${BRIDGE_INTERFACES}
299     do
300     # enter promiscous mode
301     ifconfig ${bport} 0.0.0.0 promisc
302     # now setup the bridge
303     brctl addif ${iface} ${bport}
304     done
305     # enable spanning-tree protocol
306     case ${BRIDGE_STP} in
307     on|off) brctl stp ${iface} ${BRIDGE_STP} ;;
308     *) echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
309     esac
310     ;;
311    
312     remove)
313     for bport in ${BRIDGE_INTERFACE}
314     do
315     # bring the interface down
316     ifconfig ${bport} down
317     # remove the interface from the bridge
318     brctl delif ${iface} ${bport}
319     done
320     # bring the bridge down
321     brctl delbr ${iface}
322     ;;
323   esac   esac
324    
325     # unset the bridge variable to be safe
326     unset BRIDGE_INTERFACES
327     # continue to setup generic networking
328    }
329    
330    config_routes()
331    {
332     local method="$1"
333     local message
334    
335     # only add and del are allowed
336     case ${method} in
337     add) message="Adding route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
338     del) message="Removing route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
339     *)
340     echo "config_routes: unsupported \$method '${method}'."
341     exit 1
342     ;;
343     esac
344    
345     # adds/delete user routes
346     if [[ -f /etc/conf.d/net.routes ]]
347     then
348     ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end
349     while read route
350     do
351     case "${route}" in
352     \#*|"") continue ;;
353     esac
354     echo -e ${COLOREDSTAR}"${message}"
355     route ${method} ${route}
356     evaluate_retval
357     done
358     fi
359  }  }
360    
361  networking_start()  networking_start()
362  {  {
363   local iface dns   local iface dns routes ALL_INTERFACES
364    
365     if [[ -z $1 ]]
366     then
367     ALL_INTERFACES=$(onboot_interface_list ${network_settings}/net.*)
368     else
369     if [[ -e ${network_settings}/net.$1 ]]
370     then
371     ALL_INTERFACES="$1"
372     else
373     ${FAILURE}
374     echo "Interface $1 does not exist. Aborting"
375     ${NORMAL}
376     exit 1
377     fi
378     fi
379    
380   # get list of all devices   # get list of all devices
381   for iface in $(onboot_interface_list ${network_settings}/net.*)   for iface in ${ALL_INTERFACES}
382   do   do
383   # checkconfig   # checkconfig
384   source ${network_settings}/net.${iface} || exit 1   source ${network_settings}/net.${iface} || exit 1
385   checkconfig   checkconfig
386    
387   # setup mac   # setup mac
388   if [ -n "${FORCE_MAC_TO}" ]   if [[ -n ${FORCE_MAC_TO} ]]
389   then   then
390   echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "   echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
391   ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"   ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
392   evaluate_retval   evaluate_retval
393   fi   fi
394    
395   # activate the interface   # setup bridges
396   ifconfig "${iface}" up   if [[ ${iface} = br[0-9]* ]]
397     then
398     config_bridge_devices ${iface} add
399     fi
400    
401   # now configure wireless_extensions   # now configure wireless_extensions
402   [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"   [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
403    
404   echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."   echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
405    
406     # activate the interface
407     ifconfig "${iface}" up
408    
409   # setup static or dhcp   # setup static or dhcp
410   case ${NETWORKING} in   case ${NETWORKING} in
411   dhcp|DHCP)   dhcp|DHCP)
412   ${CURS_UP}   ${CURS_UP}
413   ${SET_WWCOL}   ${SET_WWCOL}
414   echo "[DHCP]"   echo "[DHCP]"
415   loadproc ${DHCP_PROG} ${DHCP_START}   loadproc "${DHCP_PROG}" "${DHCP_START}" "${iface}"
416   ;;   ;;
417   static|STATIC)   static|STATIC)
418   ${CURS_UP}   ${CURS_UP}
# Line 284  networking_start() Line 429  networking_start()
429   echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."   echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
430   route add default gateway ${GATEWAY} metric 1 dev ${iface}   route add default gateway ${GATEWAY} metric 1 dev ${iface}
431   evaluate_retval   evaluate_retval
432    
433     unset GATEWAY
434   fi   fi
435    
436   # setup /etc/resolv.conf   # setup /etc/resolv.conf
437     # whipe out the old one
438     echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
439     # include head
440     if [ -f /etc/resolv.conf.head ]
441     then
442     cat /etc/resolv.conf.head >> /etc/resolv.conf
443     else
444     echo "# /etc/resolv.conf.head can replace this line" >> /etc/resolv.conf
445     fi
446     # add given nameserver
447   if [[ -n ${NAMESERVER} ]]   if [[ -n ${NAMESERVER} ]]
448   then   then
449   echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."   echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
450    
  # whipe out the old one  
  echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf  
451   for dns in ${NAMESERVER}   for dns in ${NAMESERVER}
452   do   do
453   echo "nameserver ${dns}" >> /etc/resolv.conf   echo "nameserver ${dns}" >> /etc/resolv.conf
454   done   done
455    
456     unset NAMESERVER
457     fi
458     # include tail
459     if [ -f /etc/resolv.conf.tail ]
460     then
461     cat /etc/resolv.conf.tail >> /etc/resolv.conf
462     else
463     echo "# /etc/resolv.conf.tail can replace this line" >> /etc/resolv.conf
464   fi   fi
465   done   done
466    
467     # setup user routes
468     config_routes add
469  }  }
470    
471  networking_stop()  networking_stop()
472  {  {
473     if [[ -z $1 ]]
474     then
475     ALL_INTERFACES=$(onboot_interface_list ${network_settings}/net.*)
476     else
477     if [[ -e ${network_settings}/net.$1 ]]
478     then
479     ALL_INTERFACES="$1"
480     else
481     ${FAILURE}
482     echo "Interface $1 does not exist. Aborting"
483     ${NORMAL}
484     exit 1
485     fi
486     fi
487    
488   # get list of all devices   # get list of all devices
489   for iface in $(onboot_interface_list ${network_settings}/net.*)   for iface in ${ALL_INTERFACES}
490   do   do
491   source ${network_settings}/net.${iface} || exit 1   source ${network_settings}/net.${iface} || exit 1
492   checkconfig   checkconfig
# Line 320  networking_stop() Line 502  networking_stop()
502   ifconfig ${iface} down   ifconfig ${iface} down
503   evaluate_retval   evaluate_retval
504    
505     # remove bridges
506     if [[ ${iface} = br[0-9]* ]]
507     then
508     config_bridge_devices ${iface} remove
509     fi
510    
511   # shutdown dhcp-daemon   # shutdown dhcp-daemon
512   if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]   if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
513   then   then
# Line 337  networking_stop() Line 525  networking_stop()
525   killall wpa_supplicant   killall wpa_supplicant
526   fi   fi
527   done   done
528    
529   # remove state dir   # remove state dir
530   if [ -d /var/run/wpa_supplicant ]   if [ -d /var/run/wpa_supplicant ]
531   then   then
532   rm -rf /var/run/wpa_supplicant   rm -rf /var/run/wpa_supplicant
533   fi   fi
534    
535     # delete user routes
536     config_routes del
537  }  }
538    
539  case $1 in  case $1 in
540   start)   start)
541   networking_start   networking_start $2
542   update_svcstatus $1   update_svcstatus $1
543   splash svc_started "$(basename $0)" 0   splash svc_started "$(basename $0)" 0
544   ;;   ;;
545    
546   stop)   stop)
547   networking_stop   networking_stop $2
548   update_svcstatus $1   update_svcstatus $1
549   splash svc_stopped "$(basename $0)" 0   splash svc_stopped "$(basename $0)" 0
550   ;;   ;;
# Line 365  case $1 in Line 556  case $1 in
556   ;;   ;;
557    
558   *)   *)
559   echo "Usage: $0 {start|stop|restart}"   echo "Usage: $0 {start|stop|restart} [interface]"
560   exit 1   exit 1
561   ;;   ;;
562  esac  esac

Legend:
Removed from v.270  
changed lines
  Added in v.1090