Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2030 - (hide annotations) (download)
Thu Jan 24 14:19:08 2013 UTC (11 years, 3 months ago) by niro
File size: 13437 byte(s)
-replace which with type -P to reduce dependencies
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 1363 source /etc/conf.d/network
16 niro 2
17 niro 243 # read values from files
18     read_value()
19     {
20     local var="$1"
21     local file="$2"
22     local value
23    
24     # local all possible vars
25     # global
26     local ONBOOT
27     local NETWORKING
28 niro 691
29 niro 243 # static
30     local IP
31     local NETMASK
32     local BROADCAST
33     local NETWORKING
34     local FORCE_MAC_TO
35    
36     # dhcp
37     local DHCP_PROG
38     local DHCP_START
39     local DHCP_STOP
40    
41     # default gw
42     local GATEWAY
43     local GATEWAY_IF
44    
45     # wireless extensions
46     local WIRELESS_AP
47     local WIRELESS_AUTH_MODE
48     local WIRELESS_BITRATE
49     local WIRELESS_CHANNEL
50     local WIRELESS_DEFAULT_KEY
51     local WIRELESS_ESSID
52     local WIRELESS_FREQUENCY
53     local WIRELESS_KEY
54     local WIRELESS_KEY_ASCII
55     local WIRELESS_KEY_0
56     local WIRELESS_KEY_1
57     local WIRELESS_KEY_2
58     local WIRELESS_KEY_3
59     local WIRELESS_KEY_LENGTH
60     local WIRELESS_MODE
61     local WIRELESS_NICK
62     local WIRELESS_NWID
63     local WIRELESS_POWER
64 niro 636 local WIRELESS_WPA_DRIVER
65 niro 243
66 niro 636 local BRIDGE_INTERFACES
67     local BRIDGE_STP
68 niro 1092 local BRIDGE_AGEING_TIME
69     local BRIDGE_PRIORITY
70     local BRIDGE_FORWARD_DELAY
71     local BRIDGE_HELLO_TIME
72     local BRIDGE_MAX_MESSAGE_AGE
73     local BRIDGE_PATH_COST
74     local BRIDGE_PORT_PRIORITY
75 niro 636
76 niro 243 source ${file}
77     eval value=\$$(echo ${var})
78     echo "${value}"
79     }
80    
81     checkconfig()
82     {
83 niro 636 if [[ -z ${NETWORKING} ]]
84 niro 2 then
85 niro 1258 rc_echo "NETWORKING missing in net.${iface}, aborted"
86 niro 2 exit 1
87     fi
88    
89 niro 20 case "${NETWORKING}" in
90 niro 2 static)
91 niro 636 if [[ -z ${IP} ]]
92 niro 2 then
93 niro 1258 rc_echo "IP missing in net.${iface}, aborted"
94 niro 2 exit 1
95     fi
96    
97 niro 636 if [[ -z ${NETMASK} ]]
98 niro 2 then
99 niro 1258 rc_echo -n "NETMASK missing in net.${iface}, "
100 niro 1363 rc_echo "using ${DEFAULT_NETMASK}"
101     NETMASK="${DEFAULT_NETMASK}"
102 niro 2 fi
103    
104 niro 636 if [[ -z ${BROADCAST} ]]
105 niro 2 then
106 niro 1258 rc_echo -n "BROADCAST missing in net.${iface}, "
107     rc_echo "using default address"
108 niro 2 fi
109     ;;
110 niro 20
111 niro 2 dhcp)
112 niro 636 if [[ -z ${DHCP_PROG} ]]
113 niro 2 then
114 niro 1258 rc_echo -n "DHCP_PROG missing in net.${iface},"
115 niro 1363 rc_echo "using default programm ${DEFAULT_DHCP_PROG}"
116     DHCP_PROG="${DEFAULT_DHCP_PROG}"
117 niro 2 fi
118 niro 1363 [[ -z ${DHCP_START} ]] && DHCP_START="${DEFAULT_DHCP_START}"
119     [[ -z ${DHCP_STOP} ]] && DHCP_STOP="${DEFAULT_DHCP_STOP}"
120 niro 2 ;;
121 niro 20
122 niro 2 esac
123     }
124    
125 niro 243 # onboot_interface_list /path/to/files*
126     onboot_interface_list()
127     {
128     local file
129     local devices
130     local iface
131 niro 2
132 niro 243 # get list of all devices
133     for file in $@
134     do
135 niro 1258 iface="$(basename ${file} | sed s/net.//)"
136    
137     # exclude backup files and exclude net.routes and net.sample too
138     case "${iface}" in
139     *~) continue ;;
140     routes) continue ;;
141     sample) continue ;;
142     esac
143    
144 niro 243 if [[ $(read_value ONBOOT ${file}) = yes ]]
145     then
146 niro 1258 devices="${devices} ${iface}"
147 niro 243 fi
148     done
149 niro 181
150 niro 243 echo "${devices}"
151     }
152 niro 181
153 niro 268 config_wireless_wep()
154     {
155     local iface="$1"
156    
157     if [[ -z ${iface} ]]
158     then
159 niro 1258 rc_echo "WEP: no \$iface given. Aborting setup."
160 niro 268 return 1
161     fi
162    
163 niro 270 ${CURS_UP}
164     ${SET_WWCOL}
165 niro 1258 rc_echo "[AUTH: WEP]"
166 niro 270
167 niro 268 iwconfig "${iface}" enc on
168     [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}"
169     [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}"
170     [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}"
171     }
172    
173     config_wireless_wpa()
174     {
175     local iface="$1"
176    
177     if [[ -z ${iface} ]]
178     then
179 niro 1258 rc_echo "WPA: no \$iface given. Aborting setup."
180 niro 268 return 1
181     fi
182    
183     if [ ! -x /sbin/wpa_supplicant ]
184     then
185 niro 1258 rc_echo "WPA: wpa_supplicant not installed. Aborting setup."
186 niro 268 return 1
187     fi
188    
189 niro 270 ${CURS_UP}
190     ${SET_WWCOL}
191 niro 1258 rc_echo "[AUTH: WPA]"
192 niro 270
193 niro 268 # get default settings
194     [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant
195    
196     # check the configuration
197     [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto
198     [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel
199    
200 niro 872 # use wext as default driver, do not abort here anymore
201     [[ -z ${WIRELESS_WPA_DRIVER} ]] && WIRELESS_WPA_DRIVER=wext
202    
203 niro 268 # write a config with the settings from net.${iface}
204     # only wpa-psk ! all other needs manual setup
205     if [[ ${WIRELESS_WPA_AUTOCONF} = true ]]
206     then
207     # write default cfg from skeleton
208     cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG}
209    
210 niro 867 local wpa_proto
211     case ${WIRELESS_AUTH_MODE} in
212     wpa) wpa_proto="WPA" ;;
213     wpa2) wpa_proto="WPA2" ;;
214     esac
215    
216 niro 268 # setup the network entry
217     sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
218 niro 867 -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
219     -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
220 niro 268 ${WIRELESS_WPA_CONFIG}
221     fi
222    
223 niro 275 # remove old state dir
224 niro 1665 [ -d /run/wpa_supplicant ] && rm -rf /run/wpa_supplicant
225 niro 275
226 niro 268 # now run the wpa_supplicant dameon
227     wpa_supplicant -B \
228     -D"${WIRELESS_WPA_DRIVER}" \
229     -c"${WIRELESS_WPA_CONFIG}" \
230     -i"${iface}" \
231     ${WIRELESS_WPA_OPTS}
232 niro 270
233     # echo wait 5 seconds
234 niro 1258 rc_echo " Waiting 5 seconds to retrieve authentification reply ... "
235 niro 270 sleep 5
236 niro 268 }
237    
238     setup_wireless_extensions()
239     {
240     local iface="$1"
241    
242     if [[ -z ${iface} ]]
243     then
244 niro 1258 rc_echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
245 niro 268 return 1
246     fi
247    
248 niro 275 if [[ -n ${WIRELESS_BITRATE} ]] ||
249     [[ -n ${WIRELESS_CHANNEL} ]] ||
250     [[ -n ${WIRELESS_ESSID} ]] ||
251     [[ -n ${WIRELESS_FREQUENCY} ]] ||
252     [[ -n ${WIRELESS_MODE} ]] ||
253     [[ -n ${WIRELESS_NICK} ]] ||
254     [[ -n ${WIRELESS_AUTH_MODE} ]]
255     then
256 niro 1258 rc_print "Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
257 niro 275 fi
258 niro 270
259 niro 268 [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"
260     [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"
261     [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}"
262     [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}"
263     [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}"
264     [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"
265    
266     case "${WIRELESS_AUTH_MODE}" in
267 niro 867 wpa|wpa2) config_wireless_wpa "${iface}" ;;
268     wep|on) config_wireless_wep "${iface}" ;;
269     off) iwconfig "${iface}" enc off ;;
270 niro 268 esac
271     }
272    
273 niro 1092 config_bridge_options()
274     {
275     local iface="$1"
276     local i
277     local port
278     local cost
279     local prio
280    
281     # enable spanning-tree protocol
282     case ${BRIDGE_STP} in
283     on|off) brctl stp "${iface}" "${BRIDGE_STP}" ;;
284 niro 1258 *) rc_echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
285 niro 1092 esac
286    
287     # configure ageing time
288     if [[ ! -z ${BRIDGE_AGEING_TIME} ]]
289     then
290     brctl setageing "${iface}" "${BRIDGE_AGEING_TIME}"
291     fi
292    
293     # configure bridge priority
294     if [[ ! -z ${BRIDGE_PRIORITY} ]]
295     then
296     brctl setbridgeprio "${iface}" "${BRIDGE_PRIORITY}"
297     fi
298    
299     # configure forward delay
300     if [[ ! -z ${BRIDGE_FORWARD_DELAY} ]]
301     then
302     brctl setfd "${iface}" "${BRIDGE_FORWARD_DELAY}"
303     fi
304    
305     # configure hello time
306     if [[ ! -z ${BRIDGE_HELLO_TIME} ]]
307     then
308     brctl sethello "${iface}" "${BRIDGE_HELLO_TIME}"
309     fi
310    
311     # configure maximal message age
312     if [[ ! -z ${BRIDGE_MAX_MESSAGE_AGE} ]]
313     then
314     brctl setmaxage "${iface}" "${BRIDGE_MAX_MESSAGE_AGE}"
315     fi
316    
317     # configure path cost for every port
318     if [[ ! -z ${BRIDGE_PATH_COST} ]]
319     then
320     for i in ${BRIDGE_PATH_COST}
321     do
322     port="${i%=*}"
323     cost="${i#*=}"
324     [[ ! -z ${port} ]] && brctl pathcost "${iface}" "${port}" "${cost}"
325     done
326     fi
327    
328     # configure port priority for every port
329     if [[ ! -z ${BRIDGE_PORT_PRIORITY} ]]
330     then
331     for i in ${BRIDGE_PORT_PRIORITY}
332     do
333     port="${i%=*}"
334     prio="${i#*=}"
335     [[ ! -z ${port} ]] && brctl setportprio "${iface}" "${port}" "${prio}"
336     done
337     fi
338     }
339    
340 niro 506 config_bridge_devices()
341     {
342     local iface="$1"
343     local method="$2"
344 niro 636 local bport
345 niro 506
346     if [[ -z ${iface} ]]
347     then
348 niro 1258 rc_echo "BRIDGE: no \$iface given. Aborting setup."
349 niro 506 return 1
350     fi
351    
352     if [[ -z ${method} ]]
353     then
354 niro 1258 rc_echo "BRIDGE: no \$method given. Aborting setup."
355 niro 506 return 1
356     fi
357    
358     # first check for brctl
359 niro 2030 if [[ -z $(type -P brctl) ]]
360 niro 506 then
361 niro 1258 rc_echo "brctl not found! Please install 'net-misc/bridge-utils'."
362 niro 506 return 1
363     fi
364    
365     # check the config
366 niro 638 if [[ -z ${BRIDGE_INTERFACES} ]]
367 niro 506 then
368 niro 1258 rc_echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
369 niro 506 return 1
370     fi
371    
372     case ${method} in
373     add)
374     # setup the bridge device
375 niro 1092 brctl addbr "${iface}"
376 niro 636 for bport in ${BRIDGE_INTERFACES}
377     do
378     # enter promiscous mode
379 niro 1092 ifconfig "${bport}" 0.0.0.0 promisc
380 niro 636 # now setup the bridge
381 niro 1092 brctl addif "${iface}" "${bport}"
382 niro 636 done
383 niro 1092 # configure all other options
384     config_bridge_options "${iface}"
385 niro 506 ;;
386 niro 636
387 niro 506 remove)
388 niro 636 for bport in ${BRIDGE_INTERFACE}
389     do
390     # bring the interface down
391 niro 1092 ifconfig "${bport}" down
392 niro 636 # remove the interface from the bridge
393 niro 1092 brctl delif "${iface}" "${bport}"
394 niro 636 done
395 niro 506 # bring the bridge down
396 niro 1092 brctl delbr "${iface}"
397 niro 506 ;;
398     esac
399 niro 636
400 niro 506 # unset the bridge variable to be safe
401 niro 636 unset BRIDGE_INTERFACES
402 niro 506 # continue to setup generic networking
403     }
404    
405     config_routes()
406     {
407     local method="$1"
408     local message
409    
410     # only add and del are allowed
411     case ${method} in
412 niro 1258 add) message="Adding" ;;
413     del) message="Removing" ;;
414 niro 506 *)
415 niro 1258 rc_echo "config_routes: unsupported \$method '${method}'."
416 niro 506 exit 1
417     ;;
418     esac
419    
420     # adds/delete user routes
421     if [[ -f /etc/conf.d/net.routes ]]
422     then
423     ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end
424     while read route
425     do
426     case "${route}" in
427     \#*|"") continue ;;
428     esac
429 niro 1258 rc_print "${message} route ${COLBLUE}${route}${COLDEFAULT} ..."
430     # do not esacpe ${route} or it breaks!
431     route "${method}" ${route}
432 niro 506 evaluate_retval
433     done
434     fi
435     }
436    
437 niro 243 networking_start()
438     {
439 niro 522 local iface dns routes ALL_INTERFACES
440 niro 243
441 niro 522 if [[ -z $1 ]]
442     then
443 niro 1091 ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
444 niro 522 else
445 niro 1091 if [[ -e ${rc_network_settings}/net.$1 ]]
446 niro 522 then
447     ALL_INTERFACES="$1"
448     else
449     ${FAILURE}
450 niro 1258 rc_echo "Interface $1 does not exist. Aborting"
451 niro 522 ${NORMAL}
452     exit 1
453     fi
454     fi
455    
456 niro 243 # get list of all devices
457 niro 522 for iface in ${ALL_INTERFACES}
458 niro 243 do
459     # checkconfig
460 niro 1091 source ${rc_network_settings}/net.${iface} || exit 1
461 niro 243 checkconfig
462    
463 niro 270 # setup mac
464 niro 955 if [[ -n ${FORCE_MAC_TO} ]]
465 niro 270 then
466 niro 1258 rc_print "Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
467 niro 270 ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
468     evaluate_retval
469     fi
470    
471 niro 506 # setup bridges
472     if [[ ${iface} = br[0-9]* ]]
473     then
474 niro 1092 config_bridge_devices "${iface}" add
475 niro 506 fi
476    
477 niro 270 # now configure wireless_extensions
478     [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
479    
480 niro 1258 rc_print "Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
481 niro 243
482 niro 868 # activate the interface
483     ifconfig "${iface}" up
484    
485 niro 243 # setup static or dhcp
486     case ${NETWORKING} in
487     dhcp|DHCP)
488     ${CURS_UP}
489     ${SET_WWCOL}
490 niro 1258 rc_echo "[DHCP]"
491 niro 1276 loadproc ${DHCP_PROG} ${DHCP_START} "${iface}"
492 niro 2 ;;
493 niro 243 static|STATIC)
494     ${CURS_UP}
495     ${SET_WWCOL}
496 niro 1258 rc_echo "[STATIC]"
497 niro 243 ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}"
498     evaluate_retval
499     ;;
500     esac
501 niro 2
502 niro 243 # setup def gw
503     if [[ -n ${GATEWAY} ]]
504 niro 2 then
505 niro 1258 rc_print "Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
506 niro 1092 route add default gateway "${GATEWAY}" metric 1 dev "${iface}"
507 niro 2 evaluate_retval
508 niro 684
509     unset GATEWAY
510 niro 2 fi
511    
512 niro 245 # setup /etc/resolv.conf
513 niro 1090 # add given nameserver
514 niro 245 if [[ -n ${NAMESERVER} ]]
515     then
516 niro 1258 rc_print "Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
517 niro 1090
518 niro 1108 # whipe out the old one
519     echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
520     # include head
521     if [ -f /etc/resolv.conf.head ]
522     then
523     cat /etc/resolv.conf.head >> /etc/resolv.conf
524     else
525     echo "# /etc/resolv.conf.head can replace this line" >> /etc/resolv.conf
526     fi
527    
528 niro 245 for dns in ${NAMESERVER}
529     do
530     echo "nameserver ${dns}" >> /etc/resolv.conf
531     done
532 niro 684
533 niro 1108 # include tail
534     if [ -f /etc/resolv.conf.tail ]
535     then
536     cat /etc/resolv.conf.tail >> /etc/resolv.conf
537     else
538     echo "# /etc/resolv.conf.tail can replace this line" >> /etc/resolv.conf
539     fi
540    
541 niro 684 unset NAMESERVER
542 niro 245 fi
543 niro 243 done
544 niro 506
545     # setup user routes
546     config_routes add
547 niro 243 }
548 niro 2
549 niro 243 networking_stop()
550     {
551 niro 522 if [[ -z $1 ]]
552     then
553 niro 1091 ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
554 niro 522 else
555 niro 1091 if [[ -e ${rc_network_settings}/net.$1 ]]
556 niro 522 then
557     ALL_INTERFACES="$1"
558     else
559     ${FAILURE}
560 niro 1258 rc_echo "Interface $1 does not exist. Aborting"
561 niro 522 ${NORMAL}
562     exit 1
563     fi
564     fi
565    
566 niro 243 # get list of all devices
567 niro 522 for iface in ${ALL_INTERFACES}
568 niro 243 do
569 niro 1091 source ${rc_network_settings}/net.${iface} || exit 1
570 niro 243 checkconfig
571    
572     if [[ -n ${GATEWAY} ]]
573 niro 2 then
574 niro 1258 rc_print "Removing default gateway ..."
575 niro 2 route del -net default
576     evaluate_retval
577     fi
578    
579 niro 1258 rc_print "Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
580     ifconfig "${iface}" down
581 niro 243 evaluate_retval
582 niro 2
583 niro 506 # remove bridges
584     if [[ ${iface} = br[0-9]* ]]
585     then
586 niro 1092 config_bridge_devices "${iface}" remove
587 niro 506 fi
588    
589 niro 243 # shutdown dhcp-daemon
590 niro 1276 if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof $(basename ${DHCP_PROG})) ]]
591 niro 243 then
592 niro 1258 rc_print "Stopping the dhcp-daemon ..."
593 niro 243 ${CURS_UP}
594     ${SET_WWCOL}
595 niro 1258 rc_echo "[$(basename ${DHCP_PROG})]"
596     if [[ -z ${DHCP_STOP} ]]
597     then
598     killproc ${DHCP_PROG}
599     evaluate_retval
600     else
601     ${DHCP_PROG} ${DHCP_STOP} "${iface}"
602     evaluate_retval
603     fi
604 niro 243 fi
605 niro 268
606     # shutdown wpa_supplicant daemon
607     if [[ -n $(pidof wpa_supplicant) ]]
608     then
609     killall wpa_supplicant
610     fi
611 niro 243 done
612 niro 275
613 niro 270 # remove state dir
614 niro 1665 if [ -d /run/wpa_supplicant ]
615 niro 270 then
616 niro 1665 rm -rf /run/wpa_supplicant
617 niro 270 fi
618 niro 506
619     # delete user routes
620     config_routes del
621 niro 243 }
622 niro 2
623 niro 243 case $1 in
624     start)
625 niro 522 networking_start $2
626 niro 2 update_svcstatus $1
627 niro 243 splash svc_started "$(basename $0)" 0
628     ;;
629    
630     stop)
631 niro 522 networking_stop $2
632 niro 243 update_svcstatus $1
633 niro 2 splash svc_stopped "$(basename $0)" 0
634     ;;
635    
636     restart)
637     $0 stop
638     sleep 1
639     $0 start
640     ;;
641    
642     *)
643 niro 1258 rc_echo "Usage: $0 {start|stop|restart} [interface]"
644 niro 2 exit 1
645     ;;
646     esac

Properties

Name Value
svn:executable *