Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1092 - (hide annotations) (download)
Wed Jul 14 11:02:11 2010 UTC (13 years, 10 months ago) by niro
Original Path: trunk/magellan-initscripts/etc/rc.d/init.d/network
File size: 13268 byte(s)
-improved bridge support
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 niro 1092 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 niro 636
75 niro 243 source ${file}
76     eval value=\$$(echo ${var})
77     echo "${value}"
78     }
79    
80     checkconfig()
81     {
82 niro 636 if [[ -z ${NETWORKING} ]]
83 niro 2 then
84     echo "NETWORKING missing in net.${interface}, aborted"
85     exit 1
86     fi
87    
88 niro 20 case "${NETWORKING}" in
89 niro 2 static)
90 niro 636 if [[ -z ${IP} ]]
91 niro 2 then
92     echo "IP missing in net.${interface}, aborted"
93     exit 1
94     fi
95    
96 niro 636 if [[ -z ${NETMASK} ]]
97 niro 2 then
98     echo -n "NETMASK missing in net.${interface}, "
99     echo "using 255.255.255.0"
100     NETMASK=255.255.255.0
101     fi
102    
103 niro 636 if [[ -z ${BROADCAST} ]]
104 niro 2 then
105     echo -n "BROADCAST missing in net.${interface}, "
106     echo "using default address"
107     fi
108     ;;
109 niro 20
110 niro 2 dhcp)
111 niro 636 if [[ -z ${DHCP_PROG} ]]
112 niro 2 then
113 niro 955 echo -n "DHCP_PROG missing in net.${interface},"
114     echo "using default programm"
115     DHCP_PROG="/sbin/dhcpcd"
116 niro 2 fi
117 niro 955 [[ -z ${DHCP_START} ]] && DHCP_START="-t 10"
118     [[ -z ${DHCP_STOP} ]] && DHCP_STOP="-k"
119 niro 2 ;;
120 niro 20
121 niro 2 esac
122     }
123    
124 niro 243 # onboot_interface_list /path/to/files*
125     onboot_interface_list()
126     {
127     local file
128     local devices
129     local iface
130 niro 2
131 niro 243 # 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 niro 506 # exclude backup files and exclude net.routes too
138 niro 243 case "${iface}" in
139 niro 2 *~) ;;
140 niro 506 */net.routes) ;;
141 niro 243 *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;
142     esac
143     fi
144     done
145 niro 181
146 niro 243 echo "${devices}"
147     }
148 niro 181
149 niro 268 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 niro 270 ${CURS_UP}
160     ${SET_WWCOL}
161     echo "[AUTH: WEP]"
162    
163 niro 268 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 niro 270 ${CURS_UP}
186     ${SET_WWCOL}
187     echo "[AUTH: WPA]"
188    
189 niro 268 # 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 niro 872 # use wext as default driver, do not abort here anymore
197     [[ -z ${WIRELESS_WPA_DRIVER} ]] && WIRELESS_WPA_DRIVER=wext
198    
199 niro 268 # 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 niro 867 local wpa_proto
207     case ${WIRELESS_AUTH_MODE} in
208     wpa) wpa_proto="WPA" ;;
209     wpa2) wpa_proto="WPA2" ;;
210     esac
211    
212 niro 268 # setup the network entry
213     sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
214 niro 867 -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
215     -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
216 niro 268 ${WIRELESS_WPA_CONFIG}
217     fi
218    
219 niro 275 # remove old state dir
220     [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant
221    
222 niro 268 # 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 niro 270
229     # echo wait 5 seconds
230     echo " Waiting 5 seconds to retrieve authentification reply ... "
231     sleep 5
232 niro 268 }
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 niro 275 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 niro 270
255 niro 268 [[ -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 niro 867 wpa|wpa2) config_wireless_wpa "${iface}" ;;
264     wep|on) config_wireless_wep "${iface}" ;;
265     off) iwconfig "${iface}" enc off ;;
266 niro 268 esac
267     }
268    
269 niro 1092 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
318     port="${i%=*}"
319     cost="${i#*=}"
320     [[ ! -z ${port} ]] && brctl pathcost "${iface}" "${port}" "${cost}"
321     done
322     fi
323    
324     # configure port priority for every port
325     if [[ ! -z ${BRIDGE_PORT_PRIORITY} ]]
326     then
327     for i in ${BRIDGE_PORT_PRIORITY}
328     do
329     port="${i%=*}"
330     prio="${i#*=}"
331     [[ ! -z ${port} ]] && brctl setportprio "${iface}" "${port}" "${prio}"
332     done
333     fi
334     }
335    
336 niro 506 config_bridge_devices()
337     {
338     local iface="$1"
339     local method="$2"
340 niro 636 local bport
341 niro 506
342     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 niro 638 if [[ -z ${BRIDGE_INTERFACES} ]]
363 niro 506 then
364 niro 638 echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
365 niro 506 return 1
366     fi
367    
368     case ${method} in
369     add)
370     # setup the bridge device
371 niro 1092 brctl addbr "${iface}"
372 niro 636 for bport in ${BRIDGE_INTERFACES}
373     do
374     # enter promiscous mode
375 niro 1092 ifconfig "${bport}" 0.0.0.0 promisc
376 niro 636 # now setup the bridge
377 niro 1092 brctl addif "${iface}" "${bport}"
378 niro 636 done
379 niro 1092 # configure all other options
380     config_bridge_options "${iface}"
381 niro 506 ;;
382 niro 636
383 niro 506 remove)
384 niro 636 for bport in ${BRIDGE_INTERFACE}
385     do
386     # bring the interface down
387 niro 1092 ifconfig "${bport}" down
388 niro 636 # remove the interface from the bridge
389 niro 1092 brctl delif "${iface}" "${bport}"
390 niro 636 done
391 niro 506 # bring the bridge down
392 niro 1092 brctl delbr "${iface}"
393 niro 506 ;;
394     esac
395 niro 636
396 niro 506 # unset the bridge variable to be safe
397 niro 636 unset BRIDGE_INTERFACES
398 niro 506 # 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
425     echo -e ${COLOREDSTAR}"${message}"
426 niro 1092 route "${method}" "${route}"
427 niro 506 evaluate_retval
428     done
429     fi
430     }
431    
432 niro 243 networking_start()
433     {
434 niro 522 local iface dns routes ALL_INTERFACES
435 niro 243
436 niro 522 if [[ -z $1 ]]
437     then
438 niro 1091 ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
439 niro 522 else
440 niro 1091 if [[ -e ${rc_network_settings}/net.$1 ]]
441 niro 522 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 niro 243 # get list of all devices
452 niro 522 for iface in ${ALL_INTERFACES}
453 niro 243 do
454     # checkconfig
455 niro 1091 source ${rc_network_settings}/net.${iface} || exit 1
456 niro 243 checkconfig
457    
458 niro 270 # setup mac
459 niro 955 if [[ -n ${FORCE_MAC_TO} ]]
460 niro 270 then
461     echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
462     ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
463     evaluate_retval
464     fi
465    
466 niro 506 # setup bridges
467     if [[ ${iface} = br[0-9]* ]]
468     then
469 niro 1092 config_bridge_devices "${iface}" add
470 niro 506 fi
471    
472 niro 270 # now configure wireless_extensions
473     [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
474    
475 niro 243 echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
476    
477 niro 868 # activate the interface
478     ifconfig "${iface}" up
479    
480 niro 243 # setup static or dhcp
481     case ${NETWORKING} in
482     dhcp|DHCP)
483     ${CURS_UP}
484     ${SET_WWCOL}
485     echo "[DHCP]"
486 niro 955 loadproc "${DHCP_PROG}" "${DHCP_START}" "${iface}"
487 niro 2 ;;
488 niro 243 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 niro 2
497 niro 243 # setup def gw
498     if [[ -n ${GATEWAY} ]]
499 niro 2 then
500 niro 245 echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
501 niro 1092 route add default gateway "${GATEWAY}" metric 1 dev "${iface}"
502 niro 2 evaluate_retval
503 niro 684
504     unset GATEWAY
505 niro 2 fi
506    
507 niro 245 # setup /etc/resolv.conf
508 niro 1090 # whipe out the old one
509     echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
510     # include head
511     if [ -f /etc/resolv.conf.head ]
512     then
513     cat /etc/resolv.conf.head >> /etc/resolv.conf
514     else
515     echo "# /etc/resolv.conf.head can replace this line" >> /etc/resolv.conf
516     fi
517     # add given nameserver
518 niro 245 if [[ -n ${NAMESERVER} ]]
519     then
520     echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
521 niro 1090
522 niro 245 for dns in ${NAMESERVER}
523     do
524     echo "nameserver ${dns}" >> /etc/resolv.conf
525     done
526 niro 684
527     unset NAMESERVER
528 niro 245 fi
529 niro 1090 # include tail
530     if [ -f /etc/resolv.conf.tail ]
531     then
532     cat /etc/resolv.conf.tail >> /etc/resolv.conf
533     else
534     echo "# /etc/resolv.conf.tail can replace this line" >> /etc/resolv.conf
535     fi
536 niro 243 done
537 niro 506
538     # setup user routes
539     config_routes add
540 niro 243 }
541 niro 2
542 niro 243 networking_stop()
543     {
544 niro 522 if [[ -z $1 ]]
545     then
546 niro 1091 ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
547 niro 522 else
548 niro 1091 if [[ -e ${rc_network_settings}/net.$1 ]]
549 niro 522 then
550     ALL_INTERFACES="$1"
551     else
552     ${FAILURE}
553     echo "Interface $1 does not exist. Aborting"
554     ${NORMAL}
555     exit 1
556     fi
557     fi
558    
559 niro 243 # get list of all devices
560 niro 522 for iface in ${ALL_INTERFACES}
561 niro 243 do
562 niro 1091 source ${rc_network_settings}/net.${iface} || exit 1
563 niro 243 checkconfig
564    
565     if [[ -n ${GATEWAY} ]]
566 niro 2 then
567     echo -e ${COLOREDSTAR}"Removing default gateway ..."
568     route del -net default
569     evaluate_retval
570     fi
571    
572 niro 243 echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
573     ifconfig ${iface} down
574     evaluate_retval
575 niro 2
576 niro 506 # remove bridges
577     if [[ ${iface} = br[0-9]* ]]
578     then
579 niro 1092 config_bridge_devices "${iface}" remove
580 niro 506 fi
581    
582 niro 243 # shutdown dhcp-daemon
583     if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
584     then
585     echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..."
586     ${CURS_UP}
587     ${SET_WWCOL}
588     echo "[$(basename ${DHCP_PROG})]"
589 niro 270 ${DHCP_PROG} ${DHCP_STOP} "${iface}"
590 niro 243 evaluate_retval
591     fi
592 niro 268
593     # shutdown wpa_supplicant daemon
594     if [[ -n $(pidof wpa_supplicant) ]]
595     then
596     killall wpa_supplicant
597     fi
598 niro 243 done
599 niro 275
600 niro 270 # remove state dir
601     if [ -d /var/run/wpa_supplicant ]
602     then
603     rm -rf /var/run/wpa_supplicant
604     fi
605 niro 506
606     # delete user routes
607     config_routes del
608 niro 243 }
609 niro 2
610 niro 243 case $1 in
611     start)
612 niro 522 networking_start $2
613 niro 2 update_svcstatus $1
614 niro 243 splash svc_started "$(basename $0)" 0
615     ;;
616    
617     stop)
618 niro 522 networking_stop $2
619 niro 243 update_svcstatus $1
620 niro 2 splash svc_stopped "$(basename $0)" 0
621     ;;
622    
623     restart)
624     $0 stop
625     sleep 1
626     $0 start
627     ;;
628    
629     *)
630 niro 522 echo "Usage: $0 {start|stop|restart} [interface]"
631 niro 2 exit 1
632     ;;
633     esac

Properties

Name Value
svn:executable *