Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *