Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *