Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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