-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.sh
470 lines (394 loc) · 8.15 KB
/
lib.sh
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
# shellcheck disable=SC1004 disable=SC2154
tcpdump="${BRIST_TCPDUMP:-tcpdump -qU}"
capread="${BRIST_CAPREAD:-tcpdump}"
socat="${BRIST_SOCAT:-socat}"
PATH=$PATH:/usr/sbin
die()
{
echo "error: $*" >&2
exit 1
}
warn()
{
echo "warn: $*" >&2
}
alias fail='{ t_status=2; return 0; }'
alias skip='{ t_status=1; return 0; }'
alias pass='{ t_status=0; return 0; }'
alias require2loops='{ \
if [ $(echo $loops | wc -w) -lt 2 ]; then \
step "Require at least 2 loops"; \
skip; \
fi \
}'
alias require3loops='{ \
if [ $(echo $loops | wc -w) -lt 3 ]; then \
step "Require at least 3 loops"; \
skip; \
fi \
}'
alias require4loops='{ \
if [ $(echo $loops | wc -w) -lt 4 ]; then \
step "Require at least 4 loops"; \
skip; \
fi \
}'
genshuf()
{
local loop
: >$work/shufdata
for loop in $(shuf -e $loops); do
echo $loop $(shuf -en1 0 1) >>$work/shufdata
done
}
setshuf()
{
local to
local from
local invert
set $(cat $work/shufdata)
for to in $loops; do
from=$1;
invert=$2
shift 2
if [ $invert -eq 0 ]; then
eval 'sb'$to'=$b'$from
eval 'sh'$to'=$h'$from
else
eval 'sb'$to'=$h'$from
eval 'sh'$to'=$b'$from
fi
done
for to in $(shuf -e $loops); do
eval 'b'$to'=$sb'$to
eval 'h'$to'=$sh'$to
done
}
printtopology()
{
[ $V -gt 0 ] || return
local l="$loops"
local bp="$bports"
local hp="$hports"
while [ $(car $l) ]; do
printf "(b%u) %s <--> %s (h%u)\n" \
$(car $l) $(car $bp) $(car $hp) $(car $l)
l="$(cdr $l)"
bp="$(cdr $bp)"
hp="$(cdr $hp)"
done
}
step()
{
t_step="$*"
printf "\e[1m%s:\e[0m %s\n" "$t_current" "$t_step"
[ -n "$single_step" ] && read -p "Single-stepping, press enter to continue"
}
depcheck()
{
"$@" -h >/dev/null 2>&1 && return
die "\"$*\" is missing"
}
# shellcheck disable=SC2086
phys()
{
eval echo '$'${1}${2}
}
car()
{
echo "$1"
}
cdr()
{
shift
echo "$@"
}
u8()
{
printf '\\%3.3o' $(($1))
}
be16()
{
printf '\\%3.3o\\%3.3o' $(($1 >> 8)) $(($1 & 0xff))
}
# shellcheck disable=SC2183 disable=SC2086
addrfmt()
{
bytes=$(echo 0x$* | sed -e 's/:/ 0x/g')
printf '\\%3.3o\\%3.3o\\%3.3o\\%3.3o\\%3.3o\\%3.3o' $bytes
}
# shellcheck disable=SC2183 disable=SC2046
rndaddr()
{
printf '%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x' \
$((($(shuf -i 0-255 -n 1) & 0xfe) | 2)) $(shuf -i 0-255 -n 5)
}
ifaddr()
{
ip -br link show dev "$1" | awk '{ print($3); }'
}
ipaddr()
{
ip -br addr show dev "$1" | awk '{ split($3,a,"/"); print a[1]; }'
}
# Generate low-level Ethernet frames for basic layer-2 tests.
# For layer-3 and upwards, use ping, nemesis or other tools.
#
# Legend:
# b: broadcast dest addr
# d: local dest addr from arg
# g: global dest addr from arg (multicast)
# i: source addr from interface arg
# I: dest addr from interface arg
# q: 802.1Q VLAN tag with VID arg
# s: local source addr from arg
# t: ether type/len from arg
#
# shellcheck disable=SC2046 disable=SC2059 disable=SC2086
eth()
{
da="$(addrfmt $(rndaddr))"
sa="$(addrfmt $(rndaddr))"
qtag=""
et="$(be16 0xbbbb)"
while getopts "bd:g:i:I:q:s:t:" opt; do
case $opt in
b)
da="\377\377\377\377\377\377"
;;
d)
da="\002\000\000\000\000$(u8 $OPTARG)"
;;
g)
da="\001\000\000\000\000$(u8 $OPTARG)"
;;
i)
sa="$(addrfmt $(ifaddr $OPTARG))"
;;
I)
da="$(addrfmt $(ifaddr $OPTARG))"
;;
q)
qtag="$(be16 0x8100)$(be16 $OPTARG)"
;;
s)
sa="\002\000\000\000\000$(u8 $OPTARG)"
;;
t)
et="$(be16 $OPTARG)"
;;
*)
exit 1
;;
esac
done
shift $((OPTIND - 1))
printf "${da}${sa}${qtag}${et}"
}
_kill_capture()
{
local pid=$(eval echo '$'"${1}_capture")
if [ -n "$pid" ]; then
kill -1 "$pid" 2>/dev/null && wait "$pid"
eval "unset ${1}_capture"
fi
}
_capture()
{
local pcap="${t_work}/${1}.pcap"
local filter="${2}"
rm -f "$pcap"
# Note: arguments must be compatible with tshark as well!
$tcpdump -q -lnp -i "$1" -w "$pcap" -s 128 "$filter" 2>/dev/null &
eval "${1}_capture=$!"
#shellcheck disable=SC2034
for i in $(seq 50); do
[ -f "$pcap" ] && return 0
sleep 0.1
done
die "unable to start capture on $1"
}
capture()
{
local filter="ether proto 0xbbbb"
while getopts "f:" opt; do
case $opt in
f)
filter="$OPTARG"
;;
*)
exit 1
;;
esac
done
shift $((OPTIND - 1))
step "$*: capturing $filter ..."
while [ $# -gt 0 ]; do
_capture "$1" "$filter"
shift
done
[ "$conf_capture_delay" ] && sleep "$conf_capture_delay"
}
# shellcheck disable=SC2086
report()
{
opts="-n -A"
while getopts "o:" opt; do
case $opt in
o)
opts="$OPTARG"
;;
*)
exit 1
;;
esac
done
shift $((OPTIND - 1))
_kill_capture "${1}"
$capread $opts -r "$t_work/${1}.pcap" 2>/dev/null
}
# Inject IGMP v2 query frames on interface $1
mcast_query_start()
{
local count=100
local ival=10
while getopts "c:i:" opt; do
case $opt in
c)
count=$OPTARG
;;
i)
ival=$OPTARG
;;
*)
exit 1
;;
esac
done
shift $((OPTIND - 1))
nemesis igmp -d "$1" -p 0x11 -r 100 -c $count -D 224.0.0.1 -i $ival &
eval "${1}_query=$!"
}
mcast_query_stop()
{
local pid=$(eval echo '$'"${1}_query")
if [ "$pid" ]; then
kill -1 "$pid" && wait "$pid"
eval "unset ${1}_query"
fi
}
# Joins group 225.1.2.3 on interface $1
# NOTE: the interface must have an IPv4 address
mcast_join()
{
local group=225.1.2.3
local addr=$(ipaddr "$1")
[ -z "$addr" ] && die "Interface $1 has no address"
step "$1: joining $group from $addr"
socat UDP4-RECVFROM:6666,ip-add-membership=$group:$addr,fork EXEC:hostname &
eval "${1}_join=$!"
}
mcast_leave()
{
local pid=$(eval echo '$'"${1}_join")
if [ "$pid" ]; then
kill -1 "$pid" && wait "$pid"
eval "unset ${1}_join"
fi
}
# Blocking multicast generator, sends 3 ICMP messages on interface $1
# to group 225.1.2.3 by default. Override with -c NUM and -g GROUP.
mcast_gen()
{
local group="225.1.2.3"
local cnt=3
while getopts "c:g:" opt; do
case $opt in
c)
cnt="$OPTARG"
;;
g)
group="$OPTARG"
;;
*)
exit 1
;;
esac
done
shift $((OPTIND - 1))
step "$h1: sending multicast to $group ($cnt sec) ..."
ping -qc "$cnt" -W 1 -I "$1" "$group" >/dev/null
}
# Find gaps in ICMP multicast streams
mcast_analyze_gaps()
{
step "$h1: analyzing, should be uninterrupted from first to last packet ..."
_kill_capture "${1}"
$capread -nr "$t_work/${1}.pcap" icmp 2>/dev/null >"$t_work/${1}.text"
# Find first and last ICMP sequence number (works with tshark & tcpdump)
sed 's/.*seq \([0-9]*\),.*/\1/g' <"$t_work/${1}.text" >"$t_work/${1}.seqnos"
#first=1
local first=$(head -1 "$t_work/${1}.seqnos")
local last=$(tail -1 "$t_work/${1}.seqnos")
seq "$first" "$last" >"$t_work/${1}.seq"
cmp "$t_work/${1}.seq" "$t_work/${1}.seqnos" || fail
}
inject()
{
# Collect all data to make sure that we hand it to socat in a
# single write(1).
cat >"$t_work/last.bin"
$socat -u open:"$t_work/last.bin" "interface:$1"
[ "$conf_inject_delay" ] && sleep "$conf_inject_delay"
}
create_br()
{
local br="$1"
local opts="$2"
shift 2
# shellcheck disable=SC2086
ip link add dev "$br" type bridge $opts
ip link set dev "$br" up
while [ $# -gt 0 ]; do
ip link set dev "$1" master "$br"
shift
done
}
br_has()
{
ip link add type bridge help 2>&1 | grep -q $1
}
brport_has()
{
bridge link help 2>&1 | grep -q $1
}
br_test()
{
local port="$1"
local setting="$2"
local val="$3"
local cmd=bridge
local altval=
# There are some inconsistencies in the output from iproute2 with
# regards to boolean values - we try to paper over those here.
case $val in
on)
altval=1
;;
off)
altval=0
;;
1)
altval=on
;;
0)
altval=off
;;
esac
if stat /sys/class/net/$port/bridge >/dev/null 2>&1; then
cmd=ip
fi
$cmd -d link show dev $port | grep -q "$setting $val" && return 0
[ "$altval" ] && \
$cmd -d link show dev $port | grep -q "$setting $altval"
}