Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2101 - (show annotations) (download)
Tue Apr 2 08:27:01 2013 UTC (11 years ago) by niro
File size: 13458 byte(s)
-do not use hardcoded pathes to iwconfig
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 # 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
29 # 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 local WIRELESS_WPA_DRIVER
65
66 local BRIDGE_INTERFACES
67 local BRIDGE_STP
68 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
76 source ${file}
77 eval value=\$$(echo ${var})
78 echo "${value}"
79 }
80
81 checkconfig()
82 {
83 if [[ -z ${NETWORKING} ]]
84 then
85 rc_echo "NETWORKING missing in net.${iface}, aborted"
86 exit 1
87 fi
88
89 case "${NETWORKING}" in
90 static)
91 if [[ -z ${IP} ]]
92 then
93 rc_echo "IP missing in net.${iface}, aborted"
94 exit 1
95 fi
96
97 if [[ -z ${NETMASK} ]]
98 then
99 rc_echo -n "NETMASK missing in net.${iface}, "
100 rc_echo "using ${DEFAULT_NETMASK}"
101 NETMASK="${DEFAULT_NETMASK}"
102 fi
103
104 if [[ -z ${BROADCAST} ]]
105 then
106 rc_echo -n "BROADCAST missing in net.${iface}, "
107 rc_echo "using default address"
108 fi
109 ;;
110
111 dhcp)
112 if [[ -z ${DHCP_PROG} ]]
113 then
114 rc_echo -n "DHCP_PROG missing in net.${iface},"
115 rc_echo "using default programm ${DEFAULT_DHCP_PROG}"
116 DHCP_PROG="${DEFAULT_DHCP_PROG}"
117 fi
118 [[ -z ${DHCP_START} ]] && DHCP_START="${DEFAULT_DHCP_START}"
119 [[ -z ${DHCP_STOP} ]] && DHCP_STOP="${DEFAULT_DHCP_STOP}"
120 ;;
121
122 esac
123 }
124
125 # onboot_interface_list /path/to/files*
126 onboot_interface_list()
127 {
128 local file
129 local devices
130 local iface
131
132 # get list of all devices
133 for file in $@
134 do
135 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 if [[ $(read_value ONBOOT ${file}) = yes ]]
145 then
146 devices="${devices} ${iface}"
147 fi
148 done
149
150 echo "${devices}"
151 }
152
153 config_wireless_wep()
154 {
155 local iface="$1"
156
157 if [[ -z ${iface} ]]
158 then
159 rc_echo "WEP: no \$iface given. Aborting setup."
160 return 1
161 fi
162
163 ${CURS_UP}
164 ${SET_WWCOL}
165 rc_echo "[AUTH: WEP]"
166
167 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 rc_echo "WPA: no \$iface given. Aborting setup."
180 return 1
181 fi
182
183 if [ ! -x $(type -P wpa_supplicant) ]
184 then
185 rc_echo "WPA: wpa_supplicant not installed. Aborting setup."
186 return 1
187 fi
188
189 ${CURS_UP}
190 ${SET_WWCOL}
191 rc_echo "[AUTH: WPA]"
192
193 # 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/wpa_supplicant.auto
198 [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel
199
200 # use wext as default driver, do not abort here anymore
201 [[ -z ${WIRELESS_WPA_DRIVER} ]] && WIRELESS_WPA_DRIVER=wext
202
203 # 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 local wpa_proto
211 case ${WIRELESS_AUTH_MODE} in
212 wpa) wpa_proto="WPA" ;;
213 wpa2) wpa_proto="WPA2" ;;
214 esac
215
216 # setup the network entry
217 sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
218 -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \
219 -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \
220 ${WIRELESS_WPA_CONFIG}
221 fi
222
223 # remove old state dir
224 [ -d /run/wpa_supplicant ] && rm -rf /run/wpa_supplicant
225
226 # 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
233 # echo wait 5 seconds
234 rc_echo " Waiting 5 seconds to retrieve authentification reply ... "
235 sleep 5
236 }
237
238 setup_wireless_extensions()
239 {
240 local iface="$1"
241
242 if [[ -z ${iface} ]]
243 then
244 rc_echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
245 return 1
246 fi
247
248 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 rc_print "Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
257 fi
258
259 [[ -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 wpa|wpa2) config_wireless_wpa "${iface}" ;;
268 wep|on) config_wireless_wep "${iface}" ;;
269 off) iwconfig "${iface}" enc off ;;
270 esac
271 }
272
273 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 *) rc_echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
285 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 config_bridge_devices()
341 {
342 local iface="$1"
343 local method="$2"
344 local bport
345
346 if [[ -z ${iface} ]]
347 then
348 rc_echo "BRIDGE: no \$iface given. Aborting setup."
349 return 1
350 fi
351
352 if [[ -z ${method} ]]
353 then
354 rc_echo "BRIDGE: no \$method given. Aborting setup."
355 return 1
356 fi
357
358 # first check for brctl
359 if [[ -z $(type -P brctl) ]]
360 then
361 rc_echo "brctl not found! Please install 'net-misc/bridge-utils'."
362 return 1
363 fi
364
365 # check the config
366 if [[ -z ${BRIDGE_INTERFACES} ]]
367 then
368 rc_echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
369 return 1
370 fi
371
372 case ${method} in
373 add)
374 # setup the bridge device
375 brctl addbr "${iface}"
376 for bport in ${BRIDGE_INTERFACES}
377 do
378 # enter promiscous mode
379 ifconfig "${bport}" 0.0.0.0 promisc
380 # now setup the bridge
381 brctl addif "${iface}" "${bport}"
382 done
383 # configure all other options
384 config_bridge_options "${iface}"
385 ;;
386
387 remove)
388 for bport in ${BRIDGE_INTERFACE}
389 do
390 # bring the interface down
391 ifconfig "${bport}" down
392 # remove the interface from the bridge
393 brctl delif "${iface}" "${bport}"
394 done
395 # bring the bridge down
396 brctl delbr "${iface}"
397 ;;
398 esac
399
400 # unset the bridge variable to be safe
401 unset BRIDGE_INTERFACES
402 # 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 add) message="Adding" ;;
413 del) message="Removing" ;;
414 *)
415 rc_echo "config_routes: unsupported \$method '${method}'."
416 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 rc_print "${message} route ${COLBLUE}${route}${COLDEFAULT} ..."
430 # do not esacpe ${route} or it breaks!
431 route "${method}" ${route}
432 evaluate_retval
433 done
434 fi
435 }
436
437 networking_start()
438 {
439 local iface dns routes ALL_INTERFACES
440
441 if [[ -z $1 ]]
442 then
443 ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
444 else
445 if [[ -e ${rc_network_settings}/net.$1 ]]
446 then
447 ALL_INTERFACES="$1"
448 else
449 ${FAILURE}
450 rc_echo "Interface $1 does not exist. Aborting"
451 ${NORMAL}
452 exit 1
453 fi
454 fi
455
456 # get list of all devices
457 for iface in ${ALL_INTERFACES}
458 do
459 # checkconfig
460 source ${rc_network_settings}/net.${iface} || exit 1
461 checkconfig
462
463 # setup mac
464 if [[ -n ${FORCE_MAC_TO} ]]
465 then
466 rc_print "Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
467 ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
468 evaluate_retval
469 fi
470
471 # setup bridges
472 if [[ ${iface} = br[0-9]* ]]
473 then
474 config_bridge_devices "${iface}" add
475 fi
476
477 # now configure wireless_extensions
478 [ -x $(type -P iwconfig) ] && setup_wireless_extensions "${iface}"
479
480 rc_print "Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
481
482 # activate the interface
483 ifconfig "${iface}" up
484
485 # setup static or dhcp
486 case ${NETWORKING} in
487 dhcp|DHCP)
488 ${CURS_UP}
489 ${SET_WWCOL}
490 rc_echo "[DHCP]"
491 loadproc ${DHCP_PROG} ${DHCP_START} "${iface}"
492 ;;
493 static|STATIC)
494 ${CURS_UP}
495 ${SET_WWCOL}
496 rc_echo "[STATIC]"
497 ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}"
498 evaluate_retval
499 ;;
500 esac
501
502 # setup def gw
503 if [[ -n ${GATEWAY} ]]
504 then
505 rc_print "Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
506 route add default gateway "${GATEWAY}" metric 1 dev "${iface}"
507 evaluate_retval
508
509 unset GATEWAY
510 fi
511
512 # setup /etc/resolv.conf
513 # add given nameserver
514 if [[ -n ${NAMESERVER} ]]
515 then
516 rc_print "Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
517
518 # 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 for dns in ${NAMESERVER}
529 do
530 echo "nameserver ${dns}" >> /etc/resolv.conf
531 done
532
533 # 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 unset NAMESERVER
542 fi
543 done
544
545 # setup user routes
546 config_routes add
547 }
548
549 networking_stop()
550 {
551 if [[ -z $1 ]]
552 then
553 ALL_INTERFACES=$(onboot_interface_list ${rc_network_settings}/net.*)
554 else
555 if [[ -e ${rc_network_settings}/net.$1 ]]
556 then
557 ALL_INTERFACES="$1"
558 else
559 ${FAILURE}
560 rc_echo "Interface $1 does not exist. Aborting"
561 ${NORMAL}
562 exit 1
563 fi
564 fi
565
566 # get list of all devices
567 for iface in ${ALL_INTERFACES}
568 do
569 source ${rc_network_settings}/net.${iface} || exit 1
570 checkconfig
571
572 if [[ -n ${GATEWAY} ]]
573 then
574 rc_print "Removing default gateway ..."
575 route del -net default
576 evaluate_retval
577 fi
578
579 rc_print "Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
580 ifconfig "${iface}" down
581 evaluate_retval
582
583 # remove bridges
584 if [[ ${iface} = br[0-9]* ]]
585 then
586 config_bridge_devices "${iface}" remove
587 fi
588
589 # shutdown dhcp-daemon
590 if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof $(basename ${DHCP_PROG})) ]]
591 then
592 rc_print "Stopping the dhcp-daemon ..."
593 ${CURS_UP}
594 ${SET_WWCOL}
595 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 fi
605
606 # shutdown wpa_supplicant daemon
607 if [[ -n $(pidof wpa_supplicant) ]]
608 then
609 killall wpa_supplicant
610 fi
611 done
612
613 # remove state dir
614 if [ -d /run/wpa_supplicant ]
615 then
616 rm -rf /run/wpa_supplicant
617 fi
618
619 # delete user routes
620 config_routes del
621 }
622
623 case $1 in
624 start)
625 networking_start $2
626 update_svcstatus $1
627 splash svc_started "$(basename $0)" 0
628 ;;
629
630 stop)
631 networking_stop $2
632 update_svcstatus $1
633 splash svc_stopped "$(basename $0)" 0
634 ;;
635
636 restart)
637 $0 stop
638 sleep 1
639 $0 start
640 ;;
641
642 *)
643 rc_echo "Usage: $0 {start|stop|restart} [interface]"
644 exit 1
645 ;;
646 esac

Properties

Name Value
svn:executable *