Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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