Harald Welte | 7c29536 | 2020-12-11 15:30:56 +0100 | [diff] [blame] | 1 | #!/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 | |
| 7 | PIDFILE_PCAP=/tmp/pcap.pid |
| 8 | DUMPCAP=/usr/bin/dumpcap |
| 9 | |
| 10 | PIDFILE_NETCAT=/tmp/netcat.pid |
| 11 | NETCAT=/bin/nc |
| 12 | GSMTAP_PORT=4729 |
| 13 | |
| 14 | TESTCASE=$1 |
| 15 | |
| 16 | kill_rm_pidfile() { |
| 17 | if [ -e $1 ]; then |
| 18 | kill "$(cat "$1")" |
| 19 | rm $1 |
| 20 | fi |
| 21 | } |
| 22 | |
| 23 | echo "------ $TESTCASE ------" |
| 24 | date |
| 25 | |
| 26 | if [ "z$TTCN3_PCAP_PATH" = "z" ]; then |
| 27 | TTCN3_PCAP_PATH=/tmp |
| 28 | fi |
| 29 | |
| 30 | kill_rm_pidfile $PIDFILE_NETCAT |
| 31 | kill_rm_pidfile $PIDFILE_PCAP |
| 32 | |
| 33 | if [ -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 |
| 46 | fi |
| 47 | |
| 48 | # Create a dummy sink for GSMTAP packets |
| 49 | $NETCAT -l -u -k -p $GSMTAP_PORT >/dev/null 2>$TESTCASE.netcat.stderr & |
| 50 | PID=$! |
| 51 | echo $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 |
| 58 | ADDL_ARGS="" |
| 59 | for 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 |
| 74 | done |
| 75 | |
| 76 | $CMD -s 1500 -n ${ADDL_ARGS} -w "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" >$TTCN3_PCAP_PATH/$TESTCASE.pcapng.stdout 2>&1 & |
| 77 | PID=$! |
| 78 | echo $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. |
| 83 | ping 127.0.0.1 >/dev/null 2>&1 & |
| 84 | PID=$! |
| 85 | i=0 |
| 86 | while [ ! -f "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" ] || |
| 87 | [ "$(stat -c '%s' "$TTCN3_PCAP_PATH/$TESTCASE.pcapng")" -eq 32 ] |
| 88 | do |
| 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 |
| 95 | done |
| 96 | kill $PID |