Line 0
Link Here
|
|
|
1 |
#!/bin/bash |
2 |
|
3 |
for optionsfile in /etc/net/options /etc/net/options.d/*; do |
4 |
[ -s "$optionsfile" ] && . $optionsfile |
5 |
done |
6 |
|
7 |
DENOISE="egrep ^[^#]" |
8 |
|
9 |
# Stolen from /etc/init.d/functions and improved. |
10 |
is_yes() |
11 |
{ |
12 |
case "$1" in |
13 |
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|[Yy]|1) |
14 |
# true returns zero |
15 |
return 0 |
16 |
;; |
17 |
*) |
18 |
# false returns one |
19 |
return 1 |
20 |
;; |
21 |
esac |
22 |
} |
23 |
|
24 |
is_no() |
25 |
{ |
26 |
case "$1" in |
27 |
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|[Nn]|0) |
28 |
# true returns zero |
29 |
return 0 |
30 |
;; |
31 |
*) |
32 |
# false returns one |
33 |
return 1 |
34 |
;; |
35 |
esac |
36 |
} |
37 |
|
38 |
SourceIfNotEmpty() |
39 |
{ |
40 |
local f |
41 |
f="$1" |
42 |
shift |
43 |
[ -s "$f" ] && . "$f" "$@" |
44 |
} |
45 |
|
46 |
ExecIfExecutable() |
47 |
{ |
48 |
local f |
49 |
f="$1" |
50 |
shift |
51 |
[ -x "$f" ] && "$f" "$@" |
52 |
} |
53 |
|
54 |
supported_type() |
55 |
{ |
56 |
local TYPE=${1:?missing 1st arg to $FUNCNAME} |
57 |
[ -x $SCRIPTDIR/create-$TYPE -o -x $SCRIPTDIR/destroy-$TYPE ] && return 0 |
58 |
return 1 |
59 |
} |
60 |
|
61 |
# try to translate iface name into corresponding type |
62 |
name2type() |
63 |
{ |
64 |
local NAME=${1:?missing 1st arg to $FUNCNAME} |
65 |
local CAND=${NAME%%[0-9]*} |
66 |
supported_type $CAND && echo $CAND |
67 |
} |
68 |
|
69 |
# Read options hierarchy. Should work for unknown interfaces too. |
70 |
pickup_options() |
71 |
{ |
72 |
# to determine iface type we must read it's config, but |
73 |
# we should read global config and type config first. |
74 |
# A workaround here. |
75 |
SourceIfNotEmpty `profiled_filename $MYIFACEDIR/options` |
76 |
is_yes "$DISABLED" && return |
77 |
[ -z "$TYPE" ] && TYPE=`name2type $NAME` |
78 |
[ -z "$TYPE" ] && { |
79 |
print_error "No TYPE is specified for iface '$NAME' and can't guess automatically. Please fix." |
80 |
return 1 |
81 |
} |
82 |
if ! supported_type $TYPE; then |
83 |
print_error "iface type '$TYPE' is not supported" |
84 |
fi |
85 |
# source default options |
86 |
SourceIfNotEmpty `profiled_filename $IFACEDIR/default/options` |
87 |
# then source default options for our iface type |
88 |
SourceIfNotEmpty `profiled_filename $IFACEDIR/default/options-$TYPE` |
89 |
# and finally source iface options |
90 |
SourceIfNotEmpty `profiled_filename $MYIFACEDIR/options` |
91 |
# Load type-specific functions |
92 |
SourceIfNotEmpty $SCRIPTDIR/functions-$TYPE |
93 |
} |
94 |
|
95 |
print_error() |
96 |
{ |
97 |
echo "ERROR: $0: $@" >&2 |
98 |
# don't hang if logger hangs |
99 |
logger -p daemon.info -t /etc/net -- "ERROR: $0: $@" & |
100 |
} |
101 |
|
102 |
# Don't display progress if not verbose. |
103 |
if is_yes "$VERBOSE"; then |
104 |
print_message() |
105 |
{ |
106 |
echo "$@" |
107 |
return 0 |
108 |
} |
109 |
if is_yes "$PROGRESS"; then |
110 |
print_progress() |
111 |
{ |
112 |
echo -n ${1:-.} |
113 |
return 0 |
114 |
} |
115 |
print_nack() |
116 |
{ |
117 |
echo -n ${1:-!} |
118 |
return 0 |
119 |
} |
120 |
fi |
121 |
else |
122 |
print_message() { return 0; } |
123 |
print_progress() { return 0; } |
124 |
print_nack() { return 0; } |
125 |
fi |
126 |
|
127 |
iface_is_up() |
128 |
{ |
129 |
local NAME=${1:?missing 1st argument to $FUNCNAME} |
130 |
$IP -o link show dev $NAME 2>/dev/null | cut -d' ' -f3 | grep -qs '[<,]UP[,>]' |
131 |
} |
132 |
|
133 |
try_rmmod() |
134 |
{ |
135 |
local MODNAME=${1:?missing 1st argument to $FUNCNAME} |
136 |
# Note: usage counter is treated in a different way by 2.6 kernels. |
137 |
# This may cause bugs :( |
138 |
$LSMOD | egrep -qs "^$MODNAME +[0-9]+ +0 " && rmmod $MODNAME |
139 |
} |
140 |
|
141 |
# Look if iface name will change after ifrename call. |
142 |
get_mapped_ifname() |
143 |
{ |
144 |
local OLDNAME=${1:?missing 1st argument to $FUNCNAME} |
145 |
local PROF_IFTAB=`profiled_filename $IFTAB` |
146 |
if [ -s "$PROF_IFTAB" ]; then |
147 |
[ -x $IFRENAME ] || { |
148 |
print_error "$IFRENAME is unavailable, but $PROF_IFTAB exists" |
149 |
exit 1 |
150 |
} |
151 |
NEW_NAME=`$IFRENAME -c $PROF_IFTAB -i $OLDNAME -D 2>/dev/null | head -1 | cut -d' ' -f7 | sed 's/.$//'` |
152 |
[ $? = 0 -a -n "$NEW_NAME" ] && echo $NEW_NAME |
153 |
fi |
154 |
} |
155 |
|
156 |
profiled_filename() |
157 |
{ |
158 |
local BASE=${1:?missing 1st arg to $FUNCNAME} |
159 |
local CAND1=$BASE${NETPROFILE:+#$NETPROFILE}${NETHOST:+@$NETHOST} |
160 |
local CAND2=$BASE${NETHOST:+@$NETHOST} |
161 |
local CAND3=$BASE${NETPROFILE:+#$NETPROFILE} |
162 |
local CAND4=$BASE |
163 |
if [ -e $CAND1 ]; then |
164 |
echo $CAND1 |
165 |
elif [ -e $CAND2 ]; then |
166 |
echo $CAND2 |
167 |
elif [ -e $CAND3 ]; then |
168 |
echo $CAND3 |
169 |
else |
170 |
echo $CAND4 |
171 |
fi |
172 |
return 0 |
173 |
} |
174 |
|
175 |
# This function updates part of current config file namespace, consisting of |
176 |
# profile name and host name. Profile name can be overriden, but host |
177 |
# name can't be. |
178 |
init_netprofile() |
179 |
{ |
180 |
if [ -n "$1" ]; then |
181 |
NETPROFILE="$1" |
182 |
elif [ -n "$MYIFACEDIR" -a -s "$MYIFACEDIR/selectprofile" -a -x "$MYIFACEDIR/selectprofile" ]; then |
183 |
NETPROFILE=`$MYIFACEDIR/selectprofile $0 | head -1 | cut -d' ' -f1` |
184 |
elif [ -z "$NETPROFILE" ]; then |
185 |
if [ -s "$PROFILE_FILE" -a -r "$PROFILE_FILE" ]; then |
186 |
NETPROFILE=`$DENOISE -m 1 "$PROFILE_FILE" | cut -d' ' -f1` |
187 |
elif [ -r "$PROC_CMDLINE" ]; then |
188 |
# try to fetch profile name from kernel options |
189 |
NETPROFILE=`cat "$PROC_CMDLINE" | sed 's/ / /g' | \ |
190 |
sed 's/ /\n/g' | egrep '^netprofile=' | cut -d'=' -f2` |
191 |
fi |
192 |
fi |
193 |
return 0 |
194 |
} |
195 |
|
196 |
# Network hostname init. Should be called by network.init during startup. |
197 |
init_nethost() |
198 |
{ |
199 |
if [ -n "$1" ]; then |
200 |
NETHOST="$1" |
201 |
elif [ -s "$HOSTTAB" ] && grep "^$HOSTNAME " $HOSTTAB; then |
202 |
NETHOST=`$DENOISE $HOSTTAB | grep -m 1 "^$HOSTNAME " | cut -d' ' -f2` |
203 |
else |
204 |
NETHOST=$HOSTNAME |
205 |
fi |
206 |
return 0 |
207 |
} |
208 |
|
209 |
# iterator |
210 |
foreach_child () |
211 |
{ |
212 |
local WHAT=${1:?missing 1st argument to $FUNCNAME} |
213 |
local CHILDREN=`$SCRIPTDIR/childfinder $NAME` |
214 |
local ret=0 |
215 |
for child in $CHILDREN; do |
216 |
$SCRIPTDIR/$WHAT $child || : $((ret++)) |
217 |
done |
218 |
return $ret |
219 |
} |
220 |
|
221 |
# get down all dependant ifaces |
222 |
ifdown_children () |
223 |
{ |
224 |
foreach_child ifdown |
225 |
} |
226 |
|
227 |
# same for ifdown |
228 |
ifup_children () |
229 |
{ |
230 |
foreach_child ifup |
231 |
} |
232 |
|
233 |
# check if parent iface(s) (REQUIRES/HOST) is up and ifup if needed |
234 |
ifup_parents () |
235 |
{ |
236 |
[ -z "$REQUIRES" -a -z "$HOST" ] && return 0 |
237 |
local parent ret=0 |
238 |
for parent in $REQUIRES $HOST; do |
239 |
if ! iface_is_up $parent; then |
240 |
$SCRIPTDIR/ifup $parent || { |
241 |
ret=$? |
242 |
break |
243 |
} |
244 |
fi |
245 |
done |
246 |
return $ret |
247 |
} |
248 |
|
249 |
ifdown_parents () |
250 |
{ |
251 |
[ -z "$REQUIRES" -a -z "$HOST" ] && return 0 |
252 |
local parent ret=0 |
253 |
for parent in $REQUIRES $HOST; do |
254 |
if iface_is_up $parent; then |
255 |
$SCRIPTDIR/ifdown $parent || { |
256 |
ret=$? |
257 |
break |
258 |
} |
259 |
fi |
260 |
done |
261 |
return $ret |
262 |
} |
263 |
|
264 |
iface_exists () |
265 |
{ |
266 |
local NAME=${1:?missing 1st argument to $FUNCNAME} |
267 |
$IP li sh dev $NAME >/dev/null 2>&1 |
268 |
} |
269 |
|
270 |
seen_iface () |
271 |
{ |
272 |
local needle=${1:?missing 1st arg to $FUNCNAME} |
273 |
for cand in $SEEN_IFACES; do |
274 |
[ "$cand" = "$needle" ] && return 0 |
275 |
done |
276 |
return 1 |
277 |
} |
278 |
|
279 |
add_seen_iface () |
280 |
{ |
281 |
local newname=${1:?missing 1st arg to $FUNCNAME} |
282 |
SEEN_IFACES="${SEEN_IFACES:+$SEEN_IFACES }$newname" |
283 |
} |
284 |
|
285 |
# Universal configuration processor. Automatically does profiling, |
286 |
# comments filtering and progress reporting. |
287 |
xargise_file() |
288 |
{ |
289 |
local BASEFILE="${1:?missing 1st arg to $FUNCNAME}" |
290 |
local PROCESSOR="${2:?missing 2nd arg to $FUNCNAME}" |
291 |
local REALFILE=`profiled_filename $BASEFILE` |
292 |
if [ -s $REALFILE ]; then |
293 |
$DENOISE $REALFILE | xargs --max-lines=1 $PROCESSOR && print_progress |
294 |
fi |
295 |
return 0 |
296 |
} |
297 |
|
298 |
# operation\first word | add X | del X | Y X |
299 |
#----------------------+-------+-------+------ |
300 |
# add | add X | del X | add X |
301 |
#----------------------+-------+-------+------ |
302 |
# del | del X | del X | del X |
303 |
#----------------------+-------+-------+------ |
304 |
process_ipv4rules() |
305 |
{ |
306 |
local OP=${1:?missing 1st arg to $FUNCNAME} |
307 |
SRCFILE=`profiled_filename $MYIFACEDIR/ipv4rule` |
308 |
[ -s "$SRCFILE" ] && $DENOISE "$SRCFILE" | \ |
309 |
while read FIRST REST; do |
310 |
case "$FIRST" in |
311 |
add) |
312 |
[ $OP = "del" ] && FIRST=del |
313 |
;; |
314 |
del) |
315 |
# should we restore deleted rules on iface shutdown? |
316 |
# [ $OP = "del" ] && FIRST=add |
317 |
;; |
318 |
*) |
319 |
FIRST="$OP $FIRST" |
320 |
;; |
321 |
esac |
322 |
$IP -4 rule $FIRST $REST |
323 |
print_progress |
324 |
done |
325 |
} |
326 |
|
327 |
flush_addresses() |
328 |
{ |
329 |
local NAME=${1:?missing 1st arg to $FUNCNAME} |
330 |
# Delete ip rules before addresses (and thus routes), see comment in |
331 |
# config-ipv4:config_routes_rules() for exact reason. |
332 |
process_ipv4rules del |
333 |
$IP -4 address flush dev $NAME >/dev/null 2>&1 |
334 |
$IP -6 address flush dev $NAME scope host >/dev/null 2>&1 |
335 |
$IP -6 address flush dev $NAME scope site >/dev/null 2>&1 |
336 |
$IP -6 address flush dev $NAME scope global >/dev/null 2>&1 |
337 |
} |
338 |
|
339 |
have_ifplugd() |
340 |
{ |
341 |
if [ -x "${IFPLUGD:=$DEFAULT_IFPLUGD}" ]; then |
342 |
echo yes |
343 |
else |
344 |
echo no |
345 |
fi |
346 |
} |