Magellan Linux

Annotation of /trunk/initscripts/systemd/units/scripts/network.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2312 - (hide annotations) (download) (as text)
Thu Jan 2 14:21:05 2014 UTC (10 years, 4 months ago) by niro
File MIME type: application/x-sh
File size: 12276 byte(s)
-fixed a typo
1 niro 1378 #!/bin/bash
2     # $Id$
3     # Magellan network configuration script for systemd
4    
5 niro 1382 # get default settings
6     source /etc/conf.d/network
7    
8 niro 2305 wait_online()
9     {
10 niro 2312 local timeout="$1"
11 niro 2305 local iface="$2"
12    
13     (( timeout *= 10 ))
14    
15     while [ ! -e /sys/class/net/${iface} ]
16     do
17     (( timeout-- > 0 )) || return 1
18 niro 2308 echo "waiting 0.1 seconds for device '${iface}' - timeout->'${timeout}'"
19 niro 2305 sleep 0.1
20     done
21    
22     return 0
23     }
24    
25 niro 1378 # read values from files
26     read_value()
27     {
28     local var="$1"
29     local file="$2"
30     local value
31    
32     # local all possible vars
33     # global
34     local ONBOOT
35     local NETWORKING
36    
37     # static
38     local IP
39     local NETMASK
40     local BROADCAST
41     local NETWORKING
42     local FORCE_MAC_TO
43    
44     # dhcp
45     local DHCP_PROG
46     local DHCP_START
47     local DHCP_STOP
48    
49     # default gw
50     local GATEWAY
51     local GATEWAY_IF
52    
53     # wireless extensions
54     local WIRELESS_AP
55     local WIRELESS_AUTH_MODE
56     local WIRELESS_BITRATE
57     local WIRELESS_CHANNEL
58     local WIRELESS_DEFAULT_KEY
59     local WIRELESS_ESSID
60     local WIRELESS_FREQUENCY
61     local WIRELESS_KEY
62     local WIRELESS_KEY_ASCII
63     local WIRELESS_KEY_0
64     local WIRELESS_KEY_1
65     local WIRELESS_KEY_2
66     local WIRELESS_KEY_3
67     local WIRELESS_KEY_LENGTH
68     local WIRELESS_MODE
69     local WIRELESS_NICK
70     local WIRELESS_NWID
71     local WIRELESS_POWER
72     local WIRELESS_WPA_DRIVER
73    
74     local BRIDGE_INTERFACES
75     local BRIDGE_STP
76     local BRIDGE_AGEING_TIME
77     local BRIDGE_PRIORITY
78     local BRIDGE_FORWARD_DELAY
79     local BRIDGE_HELLO_TIME
80     local BRIDGE_MAX_MESSAGE_AGE
81     local BRIDGE_PATH_COST
82     local BRIDGE_PORT_PRIORITY
83    
84     source ${file}
85     eval value=\$$(echo ${var})
86     echo "${value}"
87     }
88    
89     checkconfig()
90     {
91     if [[ -z ${NETWORKING} ]]
92     then
93     echo "NETWORKING missing in net.${iface}, aborted"
94     exit 1
95     fi
96    
97     case "${NETWORKING}" in
98     static)
99     if [[ -z ${IP} ]]
100     then
101     echo "IP missing in net.${iface}, aborted"
102     exit 1
103     fi
104    
105     if [[ -z ${NETMASK} ]]
106     then
107     echo -n "NETMASK missing in net.${iface}, "
108 niro 1382 echo "using ${DEFAULT_NETMASK}"
109     NETMASK="${DEFAULT_NETMASK}"
110 niro 1378 fi
111    
112     if [[ -z ${BROADCAST} ]]
113     then
114     echo -n "BROADCAST missing in net.${iface}, "
115     echo "using default address"
116     fi
117     ;;
118    
119     dhcp)
120     if [[ -z ${DHCP_PROG} ]]
121     then
122     echo -n "DHCP_PROG missing in net.${iface},"
123 niro 1382 echo "using default programm ${DEFAULT_DHCP_PROG}"
124     DHCP_PROG="${DEFAULT_DHCP_PROG}"
125 niro 1378 fi
126 niro 1382 [[ -z ${DHCP_START} ]] && DHCP_START="${DEFAULT_DHCP_START}"
127     [[ -z ${DHCP_STOP} ]] && DHCP_STOP="${DEFAULT_DHCP_STOP}"
128 niro 1378 ;;
129    
130     esac
131     }
132    
133     # onboot_interface_list /path/to/files*
134     onboot_interface_list()
135     {
136     local file
137     local devices
138     local iface
139    
140     # get list of all devices
141     for file in $@
142     do
143     iface="$(basename ${file} | sed s/net.//)"
144    
145     # exclude backup files and exclude net.routes and net.sample too
146     case "${iface}" in
147     *~) continue ;;
148     routes) continue ;;
149     sample) continue ;;
150     esac
151    
152     if [[ $(read_value ONBOOT ${file}) = yes ]]
153     then
154     devices="${devices} ${iface}"
155     fi
156     done
157    
158     echo "${devices}"
159     }
160    
161     config_wireless_wep()
162     {
163     local iface="$1"
164    
165     if [[ -z ${iface} ]]
166     then
167     echo "WEP: no \$iface given. Aborting setup."
168     return 1
169     fi
170    
171     iwconfig "${iface}" enc on
172     [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}"
173     [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}"
174     [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}"
175     }
176    
177     config_wireless_wpa()
178     {
179     local iface="$1"
180    
181     if [[ -z ${iface} ]]
182     then
183     echo "WPA: no \$iface given. Aborting setup."
184     return 1
185     fi
186    
187 niro 2063 if [ ! -x $(type -P wpa_supplicant) ]
188 niro 1378 then
189     echo "WPA: wpa_supplicant not installed. Aborting setup."
190     return 1
191     fi
192    
193     # get default settings
194     [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant
195    
196     # check the configuration
197 niro 2031 [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant/wpa_supplicant.auto
198 niro 1378 [[ -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 niro 1665 [ -d /run/wpa_supplicant ] && rm -rf /run/wpa_supplicant
225 niro 1378
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     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     echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
245     return 1
246     fi
247    
248     [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"
249     [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"
250     [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}"
251     [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}"
252     [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}"
253     [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"
254    
255     case "${WIRELESS_AUTH_MODE}" in
256     wpa|wpa2) config_wireless_wpa "${iface}" ;;
257     wep|on) config_wireless_wep "${iface}" ;;
258     off) iwconfig "${iface}" enc off ;;
259     esac
260     }
261    
262     config_bridge_options()
263     {
264     local iface="$1"
265     local i
266     local port
267     local cost
268     local prio
269    
270     # enable spanning-tree protocol
271     case ${BRIDGE_STP} in
272     on|off) brctl stp "${iface}" "${BRIDGE_STP}" ;;
273     *) echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;;
274     esac
275    
276     # configure ageing time
277     if [[ ! -z ${BRIDGE_AGEING_TIME} ]]
278     then
279     brctl setageing "${iface}" "${BRIDGE_AGEING_TIME}"
280     fi
281    
282     # configure bridge priority
283     if [[ ! -z ${BRIDGE_PRIORITY} ]]
284     then
285     brctl setbridgeprio "${iface}" "${BRIDGE_PRIORITY}"
286     fi
287    
288     # configure forward delay
289     if [[ ! -z ${BRIDGE_FORWARD_DELAY} ]]
290     then
291     brctl setfd "${iface}" "${BRIDGE_FORWARD_DELAY}"
292     fi
293    
294     # configure hello time
295     if [[ ! -z ${BRIDGE_HELLO_TIME} ]]
296     then
297     brctl sethello "${iface}" "${BRIDGE_HELLO_TIME}"
298     fi
299    
300     # configure maximal message age
301     if [[ ! -z ${BRIDGE_MAX_MESSAGE_AGE} ]]
302     then
303     brctl setmaxage "${iface}" "${BRIDGE_MAX_MESSAGE_AGE}"
304     fi
305    
306     # configure path cost for every port
307     if [[ ! -z ${BRIDGE_PATH_COST} ]]
308     then
309     for i in ${BRIDGE_PATH_COST}
310     do
311     port="${i%=*}"
312     cost="${i#*=}"
313     [[ ! -z ${port} ]] && brctl pathcost "${iface}" "${port}" "${cost}"
314     done
315     fi
316    
317     # configure port priority for every port
318     if [[ ! -z ${BRIDGE_PORT_PRIORITY} ]]
319     then
320     for i in ${BRIDGE_PORT_PRIORITY}
321     do
322     port="${i%=*}"
323     prio="${i#*=}"
324     [[ ! -z ${port} ]] && brctl setportprio "${iface}" "${port}" "${prio}"
325     done
326     fi
327     }
328    
329     config_bridge_devices()
330     {
331     local iface="$1"
332     local method="$2"
333     local bport
334    
335     if [[ -z ${iface} ]]
336     then
337     echo "BRIDGE: no \$iface given. Aborting setup."
338     return 1
339     fi
340    
341     if [[ -z ${method} ]]
342     then
343     echo "BRIDGE: no \$method given. Aborting setup."
344     return 1
345     fi
346    
347     # first check for brctl
348 niro 2030 if [[ -z $(type -P brctl) ]]
349 niro 1378 then
350     echo "brctl not found! Please install 'net-misc/bridge-utils'."
351     return 1
352     fi
353    
354     # check the config
355     if [[ -z ${BRIDGE_INTERFACES} ]]
356     then
357     echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup."
358     return 1
359     fi
360    
361     case ${method} in
362     add)
363     # setup the bridge device
364     brctl addbr "${iface}"
365     for bport in ${BRIDGE_INTERFACES}
366     do
367     # enter promiscous mode
368     ifconfig "${bport}" 0.0.0.0 promisc
369     # now setup the bridge
370     brctl addif "${iface}" "${bport}"
371     done
372     # configure all other options
373     config_bridge_options "${iface}"
374     ;;
375    
376     remove)
377     for bport in ${BRIDGE_INTERFACE}
378     do
379     # bring the interface down
380     ifconfig "${bport}" down
381     # remove the interface from the bridge
382     brctl delif "${iface}" "${bport}"
383     done
384     # bring the bridge down
385     brctl delbr "${iface}"
386     ;;
387     esac
388    
389     # unset the bridge variable to be safe
390     unset BRIDGE_INTERFACES
391     # continue to setup generic networking
392     }
393    
394     config_routes()
395     {
396     local method="$1"
397     local message
398    
399     # only add and del are allowed
400     case ${method} in
401     add) message="Adding" ;;
402     del) message="Removing" ;;
403     *)
404     echo "config_routes: unsupported \$method '${method}'."
405     exit 1
406     ;;
407     esac
408    
409     # adds/delete user routes
410     if [[ -f /etc/conf.d/net.routes ]]
411     then
412     ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end
413     while read route
414     do
415     case "${route}" in
416     \#*|"") continue ;;
417     esac
418     # do not esacpe ${route} or it breaks!
419     route "${method}" ${route}
420     done
421     fi
422     }
423    
424     networking_start()
425     {
426     local iface dns routes ALL_INTERFACES
427    
428     if [[ -z $1 ]]
429     then
430     ALL_INTERFACES=$(onboot_interface_list /etc/conf.d/net.*)
431     else
432     if [[ -e /etc/conf.d/net.$1 ]]
433     then
434     ALL_INTERFACES="$1"
435     else
436     echo "Interface $1 does not exist. Aborting"
437     exit 1
438     fi
439     fi
440    
441     # get list of all devices
442     for iface in ${ALL_INTERFACES}
443     do
444     # checkconfig
445     source /etc/conf.d/net.${iface} || exit 1
446     checkconfig
447    
448 niro 2305 # wait until the device is created
449     wait_online 5 "${iface}" || { echo "device '${iface}' does not exist"; continue; }
450    
451 niro 1378 # setup mac
452     if [[ -n ${FORCE_MAC_TO} ]]
453     then
454     ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
455     fi
456    
457     # setup bridges
458     if [[ ${iface} = br[0-9]* ]]
459     then
460     config_bridge_devices "${iface}" add
461     fi
462    
463     # now configure wireless_extensions
464 niro 2101 [ -x $(type -P iwconfig) ] && setup_wireless_extensions "${iface}"
465 niro 1378
466     # activate the interface
467     ifconfig "${iface}" up
468    
469     # setup static or dhcp
470     case ${NETWORKING} in
471     dhcp|DHCP)
472     ${DHCP_PROG} ${DHCP_START} "${iface}" ;;
473     static|STATIC)
474     ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}" ;;
475     esac
476    
477     # setup def gw
478     if [[ -n ${GATEWAY} ]]
479     then
480     route add default gateway "${GATEWAY}" metric 1 dev "${iface}"
481     unset GATEWAY
482     fi
483    
484     # setup /etc/resolv.conf
485     # add given nameserver
486     if [[ -n ${NAMESERVER} ]]
487     then
488     # whipe out the old one
489     echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
490     # include head
491     if [ -f /etc/resolv.conf.head ]
492     then
493     cat /etc/resolv.conf.head >> /etc/resolv.conf
494     else
495     echo "# /etc/resolv.conf.head can replace this line" >> /etc/resolv.conf
496     fi
497    
498     for dns in ${NAMESERVER}
499     do
500     echo "nameserver ${dns}" >> /etc/resolv.conf
501     done
502    
503     # include tail
504     if [ -f /etc/resolv.conf.tail ]
505     then
506     cat /etc/resolv.conf.tail >> /etc/resolv.conf
507     else
508     echo "# /etc/resolv.conf.tail can replace this line" >> /etc/resolv.conf
509     fi
510    
511     unset NAMESERVER
512     fi
513     done
514    
515     # setup user routes
516     config_routes add
517     }
518    
519     networking_stop()
520     {
521     if [[ -z $1 ]]
522     then
523     ALL_INTERFACES=$(onboot_interface_list /etc/conf.d/net.*)
524     else
525     if [[ -e /etc/conf.d/net.$1 ]]
526     then
527     ALL_INTERFACES="$1"
528     else
529     echo "Interface $1 does not exist. Aborting"
530     exit 1
531     fi
532     fi
533    
534     # get list of all devices
535     for iface in ${ALL_INTERFACES}
536     do
537     source /etc/conf.d/net.${iface} || exit 1
538     checkconfig
539    
540     if [[ -n ${GATEWAY} ]]
541     then
542     route del -net default
543     fi
544    
545     ifconfig "${iface}" down
546    
547     # remove bridges
548     if [[ ${iface} = br[0-9]* ]]
549     then
550     config_bridge_devices "${iface}" remove
551     fi
552    
553     # shutdown dhcp-daemon
554     if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof $(basename ${DHCP_PROG})) ]]
555     then
556     if [[ -z ${DHCP_STOP} ]]
557     then
558     killall -15 ${DHCP_PROG}
559     sleep 1
560     # try harder
561     if [[ -n $(pidof $(basename ${DHCP_PROG})) ]]
562     then
563     killall -9 ${DHCP_PROG}
564     fi
565     else
566     ${DHCP_PROG} ${DHCP_STOP} "${iface}"
567     fi
568     fi
569    
570     # shutdown wpa_supplicant daemon
571     if [[ -n $(pidof wpa_supplicant) ]]
572     then
573     killall wpa_supplicant
574     fi
575     done
576    
577     # remove state dir
578 niro 1665 if [ -d /run/wpa_supplicant ]
579 niro 1378 then
580 niro 1665 rm -rf /run/wpa_supplicant
581 niro 1378 fi
582    
583     # delete user routes
584     config_routes del
585     }
586    
587     case $1 in
588     start)
589     networking_start $2 ;;
590    
591     stop)
592     networking_stop $2 ;;
593    
594     restart)
595     $0 stop
596     sleep 1
597     $0 start
598     ;;
599    
600     *)
601     echo "Usage: $0 {start|stop|restart} [interface]"
602     exit 1
603     ;;
604     esac