Magellan Linux

Contents of /trunk/initscripts/sysvinit/rc/network

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *