Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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