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