Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *