blob: eb390fddb0468f59a199b1deb5ecb093f25efba6 [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
arehbein579055b2022-11-02 00:48:32 +010033if [ ! -x "$DUMPCAP" ]; then
Harald Welteccee3a52020-12-11 17:19:58 +010034 echo "Missing required dumpcap binary at ${DUMPCAP}"
35 exit 31
36fi
37
38if [ "$(id -u)" = "0" ]; then
39 CMD="$DUMPCAP -q"
40else
Harald Welte7c295362020-12-11 15:30:56 +010041 CAP_ERR="1"
42 if [ -x /sbin/setcap ]; then
43 # N. B: this check requires libcap2-bin package
44 /sbin/setcap -q -v 'cap_net_admin,cap_net_raw=pie' $DUMPCAP
45 CAP_ERR="$?"
46 fi
47 if [ -u $DUMPCAP -o "$CAP_ERR" = "0" ]; then
48 CMD="$DUMPCAP -q"
49 else
50 echo "NOTE: unable to use dumpcap due to missing capabilities or suid bit"
51 exit 32
52 fi
53fi
54
55# Create a dummy sink for GSMTAP packets
56$NETCAT -l -u -k -p $GSMTAP_PORT >/dev/null 2>$TESTCASE.netcat.stderr &
57PID=$!
58echo $PID > $PIDFILE_NETCAT
59
60# generate the list of interface arguments. For capturing from
61# interfaces of different link-layer types, we cannot use "-i all"
62# but must use dumpcap with each individual interface name. We also
63# must write pcapng files, as only those can record the interface of
64# each packet
Harald Welte81320082020-12-12 16:26:58 +010065ADDL_ARGS="-i lo"
Harald Welte7c295362020-12-11 15:30:56 +010066for f in /sys/class/net/*; do
67 DEV=`basename $f`
68 if [[ "$DEV" == "hdlcnet"* ]]; then
69 # skip these as we only want the hdlcX devices, avoid capturing twice on both sides
70 continue
71 elif [[ "$DEV" == "hdlc"* ]]; then
72 # these are the user-side of the FR links, which is
73 # what we interface with from our test suite, emulating
74 # a BSS.
75 ADDL_ARGS="${ADDL_ARGS} -i ${DEV}"
76 elif [[ "$DEV" == "eth"* ]]; then
77 # we blindly assume that "normal" docker network devices
78 # are called ethXXX
79 ADDL_ARGS="${ADDL_ARGS} -i ${DEV}"
80 fi
81done
82
83$CMD -s 1500 -n ${ADDL_ARGS} -w "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" >$TTCN3_PCAP_PATH/$TESTCASE.pcapng.stdout 2>&1 &
84PID=$!
85echo $PID > $PIDFILE_PCAP
86
87# Wait until packet dumper creates the pcap file and starts recording.
88# We generate some traffic until we see packet dumper catches it.
89# Timeout is 10 seconds.
90ping 127.0.0.1 >/dev/null 2>&1 &
91PID=$!
92i=0
93while [ ! -f "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" ] ||
94 [ "$(stat -c '%s' "$TTCN3_PCAP_PATH/$TESTCASE.pcapng")" -eq 32 ]
95do
96 echo "Waiting for packet dumper to start... $i"
97 sleep 1
98 i=$((i+1))
99 if [ $i -eq 10 ]; then
100 break
101 fi
102done
103kill $PID