blob: aa3814a9b20d5b2e1b27905e8bab00b2b4df5125 [file] [log] [blame]
Harald Welte7c295362020-12-11 15:30:56 +01001#!/bin/bash
2#
3# contrary to ttcn3-tcpdump-start.sh, this version is dumpcap-only and
4# needed when we want to capture from interfaces of different link
5# types. It will also store the results as pcap-ng, not plain old pcap.
6
7PIDFILE_PCAP=/tmp/pcap.pid
8DUMPCAP=/usr/bin/dumpcap
9
10PIDFILE_NETCAT=/tmp/netcat.pid
11NETCAT=/bin/nc
12GSMTAP_PORT=4729
13
14TESTCASE=$1
15
16kill_rm_pidfile() {
17 if [ -e $1 ]; then
18 kill "$(cat "$1")"
19 rm $1
20 fi
21}
22
23echo "------ $TESTCASE ------"
24date
25
26if [ "z$TTCN3_PCAP_PATH" = "z" ]; then
27 TTCN3_PCAP_PATH=/tmp
28fi
29
30kill_rm_pidfile $PIDFILE_NETCAT
31kill_rm_pidfile $PIDFILE_PCAP
32
33if [ -x $DUMPCAP ]; then
34 CAP_ERR="1"
35 if [ -x /sbin/setcap ]; then
36 # N. B: this check requires libcap2-bin package
37 /sbin/setcap -q -v 'cap_net_admin,cap_net_raw=pie' $DUMPCAP
38 CAP_ERR="$?"
39 fi
40 if [ -u $DUMPCAP -o "$CAP_ERR" = "0" ]; then
41 CMD="$DUMPCAP -q"
42 else
43 echo "NOTE: unable to use dumpcap due to missing capabilities or suid bit"
44 exit 32
45 fi
46fi
47
48# Create a dummy sink for GSMTAP packets
49$NETCAT -l -u -k -p $GSMTAP_PORT >/dev/null 2>$TESTCASE.netcat.stderr &
50PID=$!
51echo $PID > $PIDFILE_NETCAT
52
53# generate the list of interface arguments. For capturing from
54# interfaces of different link-layer types, we cannot use "-i all"
55# but must use dumpcap with each individual interface name. We also
56# must write pcapng files, as only those can record the interface of
57# each packet
58ADDL_ARGS=""
59for f in /sys/class/net/*; do
60 DEV=`basename $f`
61 if [[ "$DEV" == "hdlcnet"* ]]; then
62 # skip these as we only want the hdlcX devices, avoid capturing twice on both sides
63 continue
64 elif [[ "$DEV" == "hdlc"* ]]; then
65 # these are the user-side of the FR links, which is
66 # what we interface with from our test suite, emulating
67 # a BSS.
68 ADDL_ARGS="${ADDL_ARGS} -i ${DEV}"
69 elif [[ "$DEV" == "eth"* ]]; then
70 # we blindly assume that "normal" docker network devices
71 # are called ethXXX
72 ADDL_ARGS="${ADDL_ARGS} -i ${DEV}"
73 fi
74done
75
76$CMD -s 1500 -n ${ADDL_ARGS} -w "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" >$TTCN3_PCAP_PATH/$TESTCASE.pcapng.stdout 2>&1 &
77PID=$!
78echo $PID > $PIDFILE_PCAP
79
80# Wait until packet dumper creates the pcap file and starts recording.
81# We generate some traffic until we see packet dumper catches it.
82# Timeout is 10 seconds.
83ping 127.0.0.1 >/dev/null 2>&1 &
84PID=$!
85i=0
86while [ ! -f "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" ] ||
87 [ "$(stat -c '%s' "$TTCN3_PCAP_PATH/$TESTCASE.pcapng")" -eq 32 ]
88do
89 echo "Waiting for packet dumper to start... $i"
90 sleep 1
91 i=$((i+1))
92 if [ $i -eq 10 ]; then
93 break
94 fi
95done
96kill $PID