Magellan Linux

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

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

revision 243 by niro, Tue Sep 20 20:22:05 2005 UTC revision 867 by niro, Sat May 9 16:08:38 2009 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.5 2005-09-20 20:22:05 niro Exp $  # $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    
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 "DHCP_PROG missing in net.${interface}, aborted"
107   exit 1   exit 1
# Line 120  onboot_interface_list() Line 124  onboot_interface_list()
124   if [[ $(read_value ONBOOT ${file}) = yes ]]   if [[ $(read_value ONBOOT ${file}) = yes ]]
125   then   then
126   iface="$(basename ${file} | sed s/net.//)"   iface="$(basename ${file} | sed s/net.//)"
127   # exclude backup files   # exclude backup files and exclude net.routes too
128   case "${iface}" in   case "${iface}" in
129   *~) ;;   *~) ;;
130     */net.routes) ;;
131   *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;   *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;
132   esac   esac
133   fi   fi
# Line 131  onboot_interface_list() Line 136  onboot_interface_list()
136   echo "${devices}"   echo "${devices}"
137  }  }
138    
139    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     ${CURS_UP}
150     ${SET_WWCOL}
151     echo "[AUTH: WEP]"
152    
153     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     ${CURS_UP}
176     ${SET_WWCOL}
177     echo "[AUTH: WPA]"
178    
179     # 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     local wpa_proto
199     case ${WIRELESS_AUTH_MODE} in
200     wpa) wpa_proto="WPA" ;;
201     wpa2) wpa_proto="WPA2" ;;
202     esac
203    
204     # setup the network entry
205     sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
206     -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
207     -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
208     ${WIRELESS_WPA_CONFIG}
209     fi
210    
211     # remove old state dir
212     [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant
213    
214     # 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    
221     # echo wait 5 seconds
222     echo "    Waiting 5 seconds to retrieve authentification reply ... "
223     sleep 5
224    }
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     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    
247     [[ -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     wpa|wpa2) config_wireless_wpa "${iface}" ;;
256     wep|on) config_wireless_wep "${iface}" ;;
257     off) iwconfig "${iface}" enc off ;;
258     esac
259    }
260    
261    config_bridge_devices()
262    {
263     local iface="$1"
264     local method="$2"
265     local bport
266    
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     if [[ -z ${BRIDGE_INTERFACES} ]]
288     then
289     echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
290     return 1
291     fi
292    
293     case ${method} in
294     add)
295     # setup the bridge device
296     brctl addbr ${iface}
297     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     case ${BRIDGE_STP} in
306     on|off) brctl stp ${iface} ${BRIDGE_STP} ;;
307     *) echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
308     esac
309     ;;
310    
311     remove)
312     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     # bring the bridge down
320     brctl delbr ${iface}
321     ;;
322     esac
323    
324     # unset the bridge variable to be safe
325     unset BRIDGE_INTERFACES
326     # 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  networking_start()  networking_start()
361  {  {
362   local iface   local iface dns routes ALL_INTERFACES
363    
364     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   # get list of all devices   # get list of all devices
380   for iface in $(onboot_interface_list ${network_settings}/net.*)   for iface in ${ALL_INTERFACES}
381   do   do
382   # checkconfig   # checkconfig
383   source ${network_settings}/net.${iface} || exit 1   source ${network_settings}/net.${iface} || exit 1
384   checkconfig   checkconfig
385    
  echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."  
   
386   # setup mac   # setup mac
387   [ -n "${FORCE_MAC_TO}" ] && ifconfig ${iface} hw ether "${FORCE_MAC_TO}"   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     # setup bridges
395     if [[ ${iface} = br[0-9]* ]]
396     then
397     config_bridge_devices ${iface} add
398     fi
399    
400     # activate the interface
401     ifconfig "${iface}" up
402    
403     # now configure wireless_extensions
404     [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
405    
406     echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
407    
408   # setup static or dhcp   # setup static or dhcp
409   case ${NETWORKING} in   case ${NETWORKING} in
# Line 153  networking_start() Line 411  networking_start()
411   ${CURS_UP}   ${CURS_UP}
412   ${SET_WWCOL}   ${SET_WWCOL}
413   echo "[DHCP]"   echo "[DHCP]"
414   loadproc ${DHCP_PROG} ${DHCP_START}   loadproc ${DHCP_PROG} ${DHCP_START} "${iface}"
415   ;;   ;;
416   static|STATIC)   static|STATIC)
417   ${CURS_UP}   ${CURS_UP}
# Line 167  networking_start() Line 425  networking_start()
425   # setup def gw   # setup def gw
426   if [[ -n ${GATEWAY} ]]   if [[ -n ${GATEWAY} ]]
427   then   then
428   echo -e ${COLOREDSTAR}"Setting up default gateway ..."   echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
429   route add default gateway ${GATEWAY} metric 1 dev ${iface}   route add default gateway ${GATEWAY} metric 1 dev ${iface}
430   evaluate_retval   evaluate_retval
431    
432     unset GATEWAY
433   fi   fi
434    
435   # setup wlan extensions   # setup /etc/resolv.conf
436   if [ -x /usr/sbin/iwconfig ]   if [[ -n ${NAMESERVER} ]]
437   then   then
438   [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"   echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
439   [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"  
440   [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}"   # whipe out the old one
441   [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}"   echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
442   [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}"   for dns in ${NAMESERVER}
443   [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"   do
444   [[ -n ${WIRELESS_AUTH_MODE} ]] && iwconfig "${iface}" enc "${WIRELESS_AUTH_MODE}"   echo "nameserver ${dns}" >> /etc/resolv.conf
445   [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}"   done
446   [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}"  
447   [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}"   unset NAMESERVER
448   fi   fi
449   done   done
450    
451     # setup user routes
452     config_routes add
453  }  }
454    
455  networking_stop()  networking_stop()
456  {  {
457     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   # get list of all devices   # get list of all devices
473   for iface in $(onboot_interface_list ${network_settings}/net.*)   for iface in ${ALL_INTERFACES}
474   do   do
475   source ${network_settings}/net.${iface} || exit 1   source ${network_settings}/net.${iface} || exit 1
476   checkconfig   checkconfig
# Line 208  networking_stop() Line 486  networking_stop()
486   ifconfig ${iface} down   ifconfig ${iface} down
487   evaluate_retval   evaluate_retval
488    
489     # remove bridges
490     if [[ ${iface} = br[0-9]* ]]
491     then
492     config_bridge_devices ${iface} remove
493     fi
494    
495   # shutdown dhcp-daemon   # shutdown dhcp-daemon
496   if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]   if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
497   then   then
# Line 215  networking_stop() Line 499  networking_stop()
499   ${CURS_UP}   ${CURS_UP}
500   ${SET_WWCOL}   ${SET_WWCOL}
501   echo "[$(basename ${DHCP_PROG})]"   echo "[$(basename ${DHCP_PROG})]"
502   ${DHCP_PROG} ${DHCP_STOP}   ${DHCP_PROG} ${DHCP_STOP} "${iface}"
503   evaluate_retval   evaluate_retval
504   fi   fi
505    
506     # shutdown wpa_supplicant daemon
507     if [[ -n $(pidof wpa_supplicant) ]]
508     then
509     killall wpa_supplicant
510     fi
511   done   done
512    
513     # remove state dir
514     if [ -d /var/run/wpa_supplicant ]
515     then
516     rm -rf /var/run/wpa_supplicant
517     fi
518    
519     # delete user routes
520     config_routes del
521  }  }
522    
523  case $1 in  case $1 in
524   start)   start)
525   networking_start   networking_start $2
526   update_svcstatus $1   update_svcstatus $1
527   splash svc_started "$(basename $0)" 0   splash svc_started "$(basename $0)" 0
528   ;;   ;;
529    
530   stop)   stop)
531   networking_stop   networking_stop $2
532   update_svcstatus $1   update_svcstatus $1
533   splash svc_stopped "$(basename $0)" 0   splash svc_stopped "$(basename $0)" 0
534   ;;   ;;
# Line 241  case $1 in Line 540  case $1 in
540   ;;   ;;
541    
542   *)   *)
543   echo "Usage: $0 {start|stop|restart}"   echo "Usage: $0 {start|stop|restart} [interface]"
544   exit 1   exit 1
545   ;;   ;;
546  esac  esac

Legend:
Removed from v.243  
changed lines
  Added in v.867