1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
|
#!/sbin/runscript
# Copyright (c) 2004-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Contributed by Roy Marples (uberlord@gentoo.org)
# Many thanks to Aron Griffis (agriffis@gentoo.org)
# for help, ideas and patches
#NB: Config is in /etc/conf.d/net
# For pcmcia users. note that pcmcia must be added to the same
# runlevel as the net.* script that needs it.
depend() {
need localmount
use coldplug pcmcia usb isdn wlan
# Load any custom depend functions for the given interface
# For example, br0 may need eth0 and eth1
local iface="${myservice##*.}"
[[ $(type -t "depend_${iface}") == "function" ]] && depend_${iface}
return 0
}
# Define where our modules are
MODULES_DIR="/lib/rcscripts/net.modules.d"
# Some defaults
background="${background:-no}"
# Load some functions shared between ourselves and our dhcp helpers
source "${MODULES_DIR}/helpers.d/functions"
# Make some wrappers to fudge after/before/need/use depend flags.
# These are callbacks so MODULE will be set.
after() {
eval "${MODULE}_after() { echo \"$*\"; }"
}
before() {
eval "${MODULE}_before() { echo \"$*\"; }"
}
need() {
eval "${MODULE}_need() { echo \"$*\"; }"
}
installed() {
# We deliberately misspell this as _installed will probably be used
# at some point
eval "${MODULE}_instlled() { echo \"$*\"; }"
}
provide() {
eval "${MODULE}_provide() { echo \"$*\"; }"
}
functions() {
eval "${MODULE}_functions() { echo \"$*\"; }"
}
variables() {
eval "${MODULE}_variables() { echo \"$*\"; }"
}
# void go_background(void)
#
# Setup for backgrounding network script
go_background() {
einfo "Backgrounding ..."
mark_service_inactive "net.${iface}"
exit 0
}
# bool module_load_minimum(char *module)
#
# Does the minimum checking on a module - even when forcing
module_load_minimum() {
local f="$1" MODULE="${1##*/}"
if [[ ! -f ${f} ]]; then
eerror "${f} does not exist"
return 1
fi
if ! source "${f}" ; then
eerror "${MODULE} failed a sanity check"
return 1
fi
for f in depend; do
is_function "${MODULE}_${f}" && continue
eerror "${MODULE} does not support the required function ${f}"
return 1
done
return 0
}
# bool modules_load_auto()
#
# Load and check each module for sanity
# If the module is not installed, the functions are to be removed
modules_load_auto() {
local i j inst
# Populate the MODULES array
# Basically we treat evey file in ${MODULES_DIR} as a module
MODULES=( $( cd "${MODULES_DIR}" ; ls ) )
j="${#MODULES[@]}"
for (( i=0; i<j; i++ )); do
MODULES[i]="${MODULES_DIR}/${MODULES[i]}"
[[ ! -f ${MODULES[i]} ]] && unset MODULES[i]
done
MODULES=( "${MODULES[@]}" )
# Each of these sources into the global namespace, so it's
# important that module functions and variables are prefixed with
# the module name, for example iproute2_
j="${#MODULES[@]}"
loaded_interface=false
for (( i=0; i<j; i++ )); do
if [[ ${MODULES[i]##*/} == "interface" ]]; then
eerror "interface is a reserved name - cannot load a module called interface"
return 1
fi
(
u=0;
module_load_minimum "${MODULES[i]}" || u=1;
if [[ ${u} == 0 ]]; then
inst="${MODULES[i]##*/}_check_installed";
if is_function "${inst}" ; then
${inst} false || u=1;
fi
fi
exit "${u}";
)
if [[ $? == 0 ]]; then
source "${MODULES[i]}"
MODULES[i]="${MODULES[i]##*/}"
else
unset MODULES[i]
fi
done
MODULES=( "${MODULES[@]}" )
return 0
}
# bool modules_check_installed(void)
#
# Ensure that all modules have the required modules loaded
# This enables us to remove modules from the MODULES array
# Whilst other modules can still explicitly call them
# One example of this is essidnet which configures network
# settings for the specific ESSID connected to as the user
# may be using a daemon to configure wireless instead of our
# iwconfig module
modules_check_installed() {
local i j missingdeps nmods="${#MODULES[@]}"
for (( i=0; i<nmods; i++ )); do
is_function "${MODULES[i]}_instlled" || continue
for j in $( ${MODULES[i]}_instlled ); do
missingdeps=true
if is_function "${j}_check_installed" ; then
${j}_check_installed && missingdeps=false
elif is_function "${j}_depend" ; then
missingdeps=false
fi
${missingdeps} && unset MODULES[i] && unset PROVIDES[i] && break
done
done
MODULES=( "${MODULES[@]}" )
PROVIDES=( "${PROVIDES[@]}" )
}
# bool modules_check_user(void)
modules_check_user() {
local -a umods
local i j k l nmods="${#MODULES[@]}"
# Has the interface got any specific modules?
eval umods=( \"\$\{modules_${iface}\[@\]\}\" )
# Global setting follows interface-specific setting
umods=( "${umods[@]}" "${modules[@]}" )
# Add our preferred modules
local -a pmods=( "iproute2" "dhcpcd" "iwconfig" "netplugd" )
umods=( "${umods[@]}" "${pmods[@]}" )
# First we strip any modules that conflict from user settings
# So if the user specifies pump then we don't use dhcpcd
for (( i=0; i<${#umods[@]}; i++ )); do
# Some users will inevitably put "dhcp" in their modules
# list. To keep users from screwing up their system this
# way, ignore this setting so that the default dhcp
# module will be used.
[[ ${umods[i]} == "dhcp" ]] && continue
# We remove any modules we explicitly don't want
if [[ ${umods[i]} == !* ]]; then
for (( j=0; j<nmods; j++ )); do
[[ -z ${MODULES[j]} ]] && continue
if [[ ${umods[i]:1} == "${MODULES[j]}" \
|| ${umods[i]:1} == "${PROVIDES[j]}" ]]; then
# We may need to setup a class wrapper for it even though
# we don't use it directly
# However, we put it into an array and wrap later as
# another module may provide the same thing
${MODULES[j]}_check_installed \
&& WRAP_MODULES=(
"${WRAP_MODULES[@]}"
"${MODULES[j]} ${PROVIDES[j]}"
)
unset MODULES[j]
unset PROVIDES[j]
fi
done
continue
fi
if ! is_function "${umods[i]}_depend" ; then
# If the module is one of our preferred modules, then
# ignore this error; whatever is available will be
# used instead.
(( i < ${#umods[@]} - ${#pmods[@]} )) || continue
# The function may not exist because the modules software is
# not installed. Load the module and report its error
if [[ -e "${MODULES_DIR}/${umods[i]}" ]]; then
source "${MODULES_DIR}/${umods[i]}"
is_function "${umods[i]}_check_installed" \
&& ${umods[i]}_check_installed true
else
eerror "The module \"${umods[i]}\" does not exist"
fi
return 1
fi
if is_function "${umods[i]}_provide" ; then
mod=$( ${umods[i]}_provide )
else
mod="${umods[i]}"
fi
for (( j=0; j<nmods; j++ )); do
[[ -z ${MODULES[j]} ]] && continue
if [[ ${PROVIDES[j]} == "${mod}" && ${umods[i]} != "${MODULES[j]}" ]]; then
# We don't have a match - now ensure that we still provide an
# alternative. This is to handle our preferred modules.
for (( l=0; l<nmods; l++ )); do
[[ ${l} == "${j}" || -z ${MODULES[l]} ]] && continue
if [[ ${PROVIDES[l]} == "${mod}" ]]; then
unset MODULES[j]
unset PROVIDES[j]
break
fi
done
fi
done
done
# Then we strip conflicting modules.
# We only need to do this for 3rd party modules that conflict with
# our own modules and the preferred list AND the user modules
# list doesn't specify a preference.
for (( i=0; i<nmods-1; i++ )); do
[[ -z ${MODULES[i]} ]] && continue
for (( j=i+1; j<nmods; j++)); do
[[ -z ${MODULES[j]} ]] && continue
[[ ${PROVIDES[i]} == "${PROVIDES[j]}" ]] \
&& unset MODULES[j] && unset PROVIDES[j]
done
done
MODULES=( "${MODULES[@]}" )
PROVIDES=( "${PROVIDES[@]}" )
return 0
}
# void modules_sort(void)
#
# Sort our modules
modules_sort() {
local i j nmods=${#MODULES[@]} m
local -a provide=() provide_list=() after=() dead=() sorted=() sortedp=()
# Make our provide list
for ((i=0; i<nmods; i++)); do
dead[i]="false"
if [[ ${MODULES[i]} != "${PROVIDES[i]}" ]] ; then
local provided=false
for ((j=0; j<${#provide[@]}; j++)); do
if [[ ${provide[j]} == "${PROVIDES[i]}" ]]; then
provide_list[j]="${provide_list[j]} ${MODULES[i]}"
provided=true
fi
done
if ! ${provided}; then
provide[j]="${PROVIDES[i]}"
provide_list[j]="${MODULES[i]}"
fi
fi
done
# Create an after array, which holds which modules the module at
# index i must be after
for ((i=0; i<nmods; i++)); do
if is_function "${MODULES[i]}_after" ; then
after[i]=" ${after[i]} $(${MODULES[i]}_after) "
fi
if is_function "${MODULES[i]}_before" ; then
for m in $(${MODULES[i]}_before); do
for ((j=0; j<nmods; j++)) ; do
if [[ ${PROVIDES[j]} == "${m}" ]]; then
after[j]=" ${after[j]} ${MODULES[i]} "
break
fi
done
done
fi
done
# Replace the after list modules with real modules
for ((i=0; i<nmods; i++)); do
if [[ -n ${after[i]} ]]; then
for ((j=0; j<${#provide[@]}; j++)); do
after[i]="${after[i]// ${provide[j]} / ${provide_list[j]} }"
done
fi
done
# We then use the below code to provide a topologial sort
module_after_visit() {
local name=$1 i x
for ((i=0; i<nmods; i++)); do
[[ ${MODULES[i]} == "$1" ]] && break
done
${dead[i]} && return
dead[i]="true"
for x in ${after[i]} ; do
module_after_visit "${x}"
done
sorted=( "${sorted[@]}" "${MODULES[i]}" )
sortedp=( "${sortedp[@]}" "${PROVIDES[i]}" )
}
for x in ${MODULES[@]}; do
module_after_visit "${x}"
done
MODULES=( "${sorted[@]}" )
PROVIDES=( "${sortedp[@]}" )
}
# bool modules_check_depends(bool showprovides)
modules_check_depends() {
local showprovides="${1:-false}" nmods="${#MODULES[@]}" i j needmod
local missingdeps p interface=false
for (( i=0; i<nmods; i++ )); do
if is_function "${MODULES[i]}_need" ; then
for needmod in $( ${MODULES[i]}_need ); do
missingdeps=true
for (( j=0; j<nmods; j++ )); do
if [[ ${needmod} == "${MODULES[j]}" \
|| ${needmod} == "${PROVIDES[j]}" ]]; then
missingdeps=false
break
fi
done
if ${missingdeps} ; then
eerror "${MODULES[i]} needs ${needmod} (dependency failure)"
return 1
fi
done
fi
if is_function "${MODULES[i]}_functions" ; then
for f in $( ${MODULES[i]}_functions ); do
if ! is_function "${f}" ; then
eerror "${MODULES[i]}: missing required function \"${f}\""
return 1
fi
done
fi
[[ ${PROVIDES[i]} == "interface" ]] && interface=true
if ${showprovides} ; then
[[ ${PROVIDES[i]} != "${MODULES[i]}" ]] \
&& veinfo "${MODULES[i]} provides ${PROVIDES[i]}"
fi
done
if ! ${interface} ; then
eerror "no interface module has been loaded"
return 1
fi
return 0
}
# bool modules_load(char *iface, bool starting)
#
# Loads the defined handler and modules for the interface
# Returns 0 on success, otherwise 1
modules_load() {
local iface="$1" starting="${2:-true}" MODULE p=false i j k
local -a x
local RC_INDENTATION="${RC_INDENTATION}"
local -a PROVIDES WRAP_MODULES
if [[ ${iface} != "lo" ]]; then
eval x=( \"\$\{modules_force_${iface}\[@\]\}\" )
[[ -n ${x} ]] && modules_force=( "${x[@]}" )
if [[ -n ${modules_force} ]]; then
ewarn "WARNING: You are forcing modules!"
ewarn "Do not complain or file bugs if things start breaking"
report=true
fi
fi
veinfo "Loading networking modules for ${iface}"
eindent
if [[ -z ${modules_force} ]]; then
modules_load_auto || return 1
else
j="${#modules_force[@]}"
for (( i=0; i<j; i++ )); do
module_load_minimum "${MODULES_DIR}/${modules_force[i]}" || return 1
if is_function "${modules_force[i]}_check_installed" ; then
${modules_force[i]}_check_installed || unset modules_force[i]
fi
done
MODULES=( "${modules_force[@]}" )
fi
j="${#MODULES[@]}"
for (( i=0; i<j; i++ )); do
# Now load our dependencies - we need to use the MODULE variable
# here as the after/before/need functions use it
MODULE="${MODULES[i]}"
${MODULE}_depend
# If no provide is given, assume module name
if is_function "${MODULES[i]}_provide" ; then
PROVIDES[i]=$( ${MODULES[i]}_provide )
else
PROVIDES[i]="${MODULES[i]}"
fi
done
if [[ -n ${modules_force[@]} ]]; then
# Strip any duplicate modules providing the same thing
j="${#MODULES[@]}"
for (( i=0; i<j-1; i++ )); do
[[ -z ${MODULES[i]} ]] && continue
for (( k=i+1; k<j; k++ )); do
if [[ ${PROVIDES[i]} == ${PROVIDES[k]} ]]; then
unset MODULES[k]
unset PROVIDES[k]
fi
done
done
MODULES=( "${MODULES[@]}" )
PROVIDES=( "${PROVIDES[@]}" )
else
if ${starting}; then
modules_check_user || return 1
else
# Always prefer iproute2 for taking down interfaces
if is_function iproute2_provide ; then
function_wrap iproute2 $(iproute2_provide)
fi
fi
fi
# Wrap our modules
j="${#MODULES[@]}"
for (( i=0; i<j; i++ )); do
function_wrap "${MODULES[i]}" "${PROVIDES[i]}"
done
j="${#WRAP_MODULES[@]}"
for (( i=0; i<j; i++ )); do
function_wrap ${WRAP_MODULES[i]}
done
if [[ -z ${modules_force[@]} ]]; then
modules_check_installed || return 1
modules_sort || return 1
fi
veinfo "modules: ${MODULES[@]}"
eindent
${starting} && p=true
modules_check_depends "${p}" || return 1
return 0
}
# bool iface_start(char *interface)
#
# iface_start is called from start. It's expected to start the base
# interface (for example "eth0"), aliases (for example "eth0:1") and to start
# VLAN interfaces (for example eth0.0, eth0.1). VLAN setup is accomplished by
# calling itself recursively.
iface_start() {
local iface="$1" mod config_counter="-1" x config_worked=false
local RC_INDENTATION="${RC_INDENTATION}"
local -a config fallback fallback_route conf a
local ifvar=$( bash_variable "$1" ) i j
# Try and work out a metric for the interface if we're on auto
eval x=\"\$\{metric_${ifvar}\}\"
if [[ -z ${x} ]]; then
if [[ ${RC_AUTO_INTERFACE} == "yes" ]]; then
eval "metric_${ifvar}=\""$( calculate_metric ${iface} )"\""
else
eval "metric_${ifvar}=0"
fi
fi
# pre Start any modules with
for mod in ${MODULES[@]}; do
if is_function "${mod}_pre_start" ; then
${mod}_pre_start "${iface}" || { eend 1; return 1; }
fi
done
# We now expand the configuration parameters and pray that the
# fallbacks expand to the same number as config or there will be
# trouble!
eval a=( \"\$\{config_${ifvar}\[@\]\}\" )
for (( i=0; i<${#a[@]}; i++ )); do
local -a b=( $( expand_parameters "${a[i]}" ) )
config=( "${config[@]}" "${b[@]}" )
done
eval a=( \"\$\{fallback_${ifvar}\[@\]\}\" )
for (( i=0; i<${#a[@]}; i++ )); do
local -a b=( $( expand_parameters "${a[i]}" ) )
fallback=( "${fallback[@]}" "${b[@]}" )
done
# We don't expand routes
eval fallback_route=( \"\$\{fallback_route_${ifvar}\[@\]\}\" )
# We must support old configs
if [[ -z ${config} ]]; then
interface_get_old_config "${iface}" || return 1
if [[ -n ${config} ]]; then
ewarn "You are using a depreciated configuration syntax for ${iface}"
ewarn "You are advised to read /etc/conf.d/net.example and upgrade it accordingly"
fi
fi
# Handle "noop" correctly
if [[ ${config[0]} == "noop" ]]; then
if interface_is_up "${iface}" true ; then
einfo "Keeping current configuration for ${iface}"
eend 0
return 0
fi
# Remove noop from the config var
config=( "${config[@]:1}" )
fi
# Provide a default of DHCP if no configuration is set and we're auto
# Otherwise a default of NULL
if [[ -z ${config} ]]; then
# if [[ ${RC_AUTO_INTERFACE} == "yes" ]]; then
if is_function "dhcp_start" ; then
config=( "dhcp" )
ewarn "Configuration not set for ${iface} - assuming dhcp"
else
eerror "Cannot default to dhcp as there is no dhcp module loaded"
eerror "No configuration for ${iface}"
return 1
fi
# else
# config=( "null" )
# ewarn "Configuration not set for ${iface} - assuming null"
# fi
fi
einfo "Bringing up ${iface}"
eindent
for (( config_counter=0; config_counter<${#config[@]}; config_counter++ )); do
# Handle null and noop correctly
if [[ ${config[config_counter]} == "null" \
|| ${config[config_counter]} == "noop" ]]; then
eend 0
config_worked=true
continue
fi
# We convert it to an array - this has the added
# bonus of trimming spaces!
conf=( ${config[config_counter]} )
einfo "${conf[0]}"
# Do we have a function for our config?
if is_function "${conf[0]}_start" ; then
eindent
${conf[0]}_start "${iface}" ; x=$?
eoutdent
[[ ${x} == 0 ]] && config_worked=true && continue
# We need to test to see if it's an IP address or a function
# We do this by testing if the 1st character is a digit
elif [[ ${conf[0]:0:1} == [[:digit:]] || ${conf[0]} == *:* ]]; then
x="0"
if [[ ${RC_AUTO_INTERFACE} == "yes" ]] \
&& is_function arping_address_exists ; then
if arping_address_exists "${iface}" "${conf[0]}" ; then
eerror "${conf[0]%%/*} already taken on ${iface}"
x="1"
fi
fi
[[ ${x} == "0" ]] && interface_add_address "${iface}" ${conf[@]}; x="$?"
eend "${x}" && config_worked=true && continue
else
eerror "No loaded modules provide \"${conf[0]}\" (${conf[0]}_start)"
fi
if [[ -n ${fallback[config_counter]} ]]; then
einfo "Trying fallback configuration"
config[config_counter]="${fallback[config_counter]}"
fallback[config_counter]=""
# Do we have a fallback route?
if [[ -n ${fallback_route[config_counter]} ]]; then
eval "routes_${ifvar}=( "\"\$\{fallback_route\[${config_counter}\]\[@\]\}\"" )"
fallback_route[config_counter]=""
fi
(( config_counter-- )) # since the loop will increment it
continue
fi
done
eoutdent
# We return failure if no configuration parameters worked
${config_worked} || return 1
# Start any modules with _post_start
for mod in ${MODULES[@]}; do
if is_function "${mod}_post_start" ; then
${mod}_post_start "${iface}" || return 1
fi
done
return 0
}
# bool iface_stop(char *interface)
#
# iface_stop: bring down an interface. Don't trust information in
# /etc/conf.d/net since the configuration might have changed since
# iface_start ran. Instead query for current configuration and bring
# down the interface.
iface_stop() {
local iface="$1" i aliases need_begin=false mod
local RC_INDENTATION="${RC_INDENTATION}"
# pre Stop any modules
for mod in ${MODULES[@]}; do
is_function "${mod}_pre_stop" && ${mod}_pre_stop "${iface}"
done
einfo "Bringing down ${iface}"
eindent
# Collect list of aliases for this interface.
# List will be in reverse order.
aliases=$( interface_get_aliases_rev "${iface}" )
# Stop aliases before primary interface.
# Note this must be done in reverse order, since ifconfig eth0:1
# will remove eth0:2, etc. It might be sufficient to simply remove
# the base interface but we're being safe here.
for i in ${aliases} ${iface}; do
# Stop all our modules
for mod in ${MODULES[@]}; do
is_function "${mod}_stop" && ${mod}_stop "${i}"
done
# A module may have removed the interface
if ! interface_exists "${iface}" ; then
eend 0
continue
fi
# Delete all the addresses for this alias
interface_del_addresses "${i}"
# Do final shut down of this alias
if ! ${IN_BACKGROUND}; then
ebegin "Shutting down ${i}"
interface_iface_stop "${i}"
eend "$?"
fi
done
# post Stop any modules
for mod in ${MODULES[@]}; do
# We have already taken down the interface, so no need to error
is_function "${mod}_post_stop" && ${mod}_post_stop "${iface}"
done
return 0
}
# bool run_start(char *iface)
#
# Brings up ${IFACE}. Calls preup, iface_start, then postup.
# Returns 0 (success) unless preup or iface_start returns 1 (failure).
# Ignores the return value from postup.
# We cannot check that the device exists ourselves as modules like
# tuntap make create it.
run_start() {
local iface="$1" IFVAR=$( bash_variable "$1" )
# We do this so users can specify additional addresses for lo if they
# need too - additional routes too
# However, no extra modules are loaded as they are just not needed
if [[ ${iface} == "lo" ]]; then
metric_lo="0"
config_lo=( "127.0.0.1/8 brd 127.255.255.255" "${config_lo[@]}" )
routes_lo=( "127.0.0.0/8" "${routes_lo[@]}" )
fi
# We may not have a loaded module for ${iface}
# Some users may have "alias natsemi eth0" in /etc/modules.d/foo
# so we can work with this
# However, if they do the same with eth1 and try to start it
# but eth0 has not been loaded then the module gets loaded as
# eth0.
# Not much we can do about this :(
# Also, we cannot error here as some modules - such as bridge
# create interfaces
if ! interface_exists "${iface}" ; then
/sbin/modprobe "${iface}" &>/dev/null
fi
# Call user-defined preup function if it exists
if is_function preup ; then
einfo "Running preup function"
eindent
( preup "${iface}" )
eend "$?" "preup ${iface} failed" || return 1
eoutdent
fi
# If config is set to noop and the interface is up with an address
# then we don't start it
local config
eval config=( \"\$\{config_${IFVAR}\[@\]\}\" )
if [[ ${config[0]} == "noop" ]] && interface_is_up "${iface}" true ; then
einfo "Keeping current configuration for ${iface}"
eend 0
else
# Remove noop from the config var
[[ ${config[0]} == "noop" ]] \
&& eval "config_${IFVAR}=( "\"\$\{config\[@\]:1\}\"" )"
# There may be existing ip address info - so we strip it
interface_del_addresses "${iface}"
# Start the interface
if ! iface_start "${iface}" ; then
interface_exists "${iface}" && interface_down "${iface}"
eend 1
return 1
fi
fi
# Call user-defined postup function if it exists
if is_function postup ; then
einfo "Running postup function"
eindent
( postup "${iface}" )
eoutdent
fi
return 0
}
# bool run_stop(char *iface) {
#
# Brings down ${iface}. If predown call returns non-zero, then
# stop returns non-zero to indicate failure bringing down device.
# In all other cases stop returns 0 to indicate success.
run_stop() {
local iface="$1" IFVAR=$( bash_variable "$1" ) x
# Load our ESSID variable so users can use it in predown() instead
# of having to write code.
local ESSID=$( get_options "ESSID" ) ESSIDVAR
[[ -n ${ESSID} ]] && ESSIDVAR=$( bash_variable "${ESSID}" )
# Call user-defined predown function if it exists
if is_function predown ; then
einfo "Running predown function"
eindent
( predown "${iface}" )
eend $? "predown ${iface} failed" || return 1
eoutdent
elif is_net_fs /; then
eerror "root filesystem is network mounted -- can't stop ${iface}"
return 1
fi
iface_stop "${iface}" || return 1 # always succeeds, btw
# Call user-defined postdown function if it exists
if is_function postdown ; then
einfo "Running postdown function"
eindent
( postdown "${iface}" )
eoutdent
fi
return 0
}
# bool run(char *iface, char *cmd)
#
# Main start/stop entry point
# We load modules here and remove any functions that they
# added as we may be called inside the same shell scope for another interface
run() {
local iface="$1" cmd="$2" r=1 RC_INDENTATION="${RC_INDENTATION}"
local starting=true
local -a MODULES mods
local IN_BACKGROUND="${IN_BACKGROUND}"
# No point in backgrounding if we're already there ...
# This is also required so we can select the "best" interface
if [[ ${IN_BACKGROUND} == "true" || ${IN_BACKGROUND} == "1" ]]; then
IN_BACKGROUND=true
background=false
else
IN_BACKGROUND=false
fi
# We need to override the exit function as runscript.sh now checks
# for it. We need it so we can mark the service as inactive ourselves.
unset -f exit
eindent
[[ ${cmd} == "stop" ]] && starting=false
# We force lo to only use these modules for a major speed boost
[[ ${iface} == "lo" ]] && modules_force=( "iproute2" "ifconfig" "system" )
if modules_load "${iface}" "${starting}" ; then
if [[ ${cmd} == "stop" ]]; then
# Reverse the module list for stopping
mods=( "${MODULES[@]}" )
for ((i = 0; i < ${#mods[@]}; i++)); do
MODULES[i]=${mods[((${#mods[@]} - i - 1))]}
done
run_stop "${iface}" && r=0
remove_state "${iface}"
else
run_start "${iface}" && r=0
fi
fi
# Only apply best state if we're on auto
if [[ ${r} == "0" ]]; then
local siface=""
if [[ ${RC_AUTO_INTERFACE} == "yes" ]]; then
siface=$( select_best_interface )
if [[ -n ${siface} ]]; then
einfo "Selecting best interface: ${siface}"
fi
elif [[ ${cmd} == "start" ]]; then
siface="${iface}"
fi
[[ -n ${siface} ]] && apply_state "${siface}"
else
if [[ ${cmd} == "start" ]]; then
# Call user-defined failup if it exists
if is_function failup ; then
einfo "Running failup function"
eindent
( failup "${iface}" )
eoutdent
fi
else
# Call user-defined faildown if it exists
if is_function faildown ; then
einfo "Running faildown function"
eindent
( faildown "${iface}" )
eoutdent
fi
fi
fi
return "${r}"
}
# void link_file(char *file)
#
# Move a config file from /etc to ${netdir} and creates a link if needed.
# This enables net-scripts to control the config file for interface management
# and allow /etc to become read only.
link_file() {
local file="$1"
local link=$( readlink "/etc/${file}" 2>/dev/null )
if [[ ${link} != "${netdir}/${file}" ]]; then
if [[ -f "/etc/${file}" ]]; then
vewarn "Moving /etc/${file} to ${netdir}/${file} and creating link"
mv "/etc/${file}" "${netdir}"
ln -snf "${netdir}/${file}" "/etc/${file}"
fi
fi
}
# bool start(void)
#
# Start entry point so that we only have one function
# which localises variables and unsets functions
start() {
if [[ ${IN_HOTPLUG} == "1" ]]; then
# If we've been called by hotplug, check if we have
# a policy for the interface for not starting
local x ifvar=$( bash_variable "${IFACE}" )
eval x=\"\$\{hotplug_${ifvar}\}\"
if [[ ${x} == "no" || ${x} == "false" ]]; then
eerror "Not starting interface ${IFACE} due to hotplug policy"
unset -f exit
mark_service_stopped "net.${IFACE}"
exit 1
fi
fi
if [[ ! -d "${statedir}/${IFACE}" ]]; then
if ! mkdir -m 0755 -p "${statedir}/${IFACE}" ; then
eerror "Unable to create state directory!"
return 1
fi
fi
if [[ ${RC_AUTO_INTERFACE} == "yes" ]]; then
link_file "resolv.conf"
link_file "ntp.conf"
link_file "yp.conf"
fi
einfo "Starting ${IFACE}"
run "${IFACE}" start
}
# bool stop(void)
#
# Stop entry point so that we only have one function
# which localises variables and unsets functions
stop() {
einfo "Stopping ${IFACE}"
run "${IFACE}" stop
}
# vim:ts=4
|