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 | |
Harald Welte | ccee3a5 | 2020-12-11 17:19:58 +0100 | [diff] [blame] | 33 | if [ ! -x $DUMPCAP ]; then |
| 34 | echo "Missing required dumpcap binary at ${DUMPCAP}" |
| 35 | exit 31 |
| 36 | fi |
| 37 | |
| 38 | if [ "$(id -u)" = "0" ]; then |
| 39 | CMD="$DUMPCAP -q" |
| 40 | else |
Harald Welte | 7c29536 | 2020-12-11 15:30:56 +0100 | [diff] [blame] | 41 | 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 |
| 53 | fi |
| 54 | |
| 55 | # Create a dummy sink for GSMTAP packets |
| 56 | $NETCAT -l -u -k -p $GSMTAP_PORT >/dev/null 2>$TESTCASE.netcat.stderr & |
| 57 | PID=$! |
| 58 | echo $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 Welte | 8132008 | 2020-12-12 16:26:58 +0100 | [diff] [blame] | 65 | ADDL_ARGS="-i lo" |
Harald Welte | 7c29536 | 2020-12-11 15:30:56 +0100 | [diff] [blame] | 66 | for 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 |
| 81 | done |
| 82 | |
| 83 | $CMD -s 1500 -n ${ADDL_ARGS} -w "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" >$TTCN3_PCAP_PATH/$TESTCASE.pcapng.stdout 2>&1 & |
| 84 | PID=$! |
| 85 | echo $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. |
| 90 | ping 127.0.0.1 >/dev/null 2>&1 & |
| 91 | PID=$! |
| 92 | i=0 |
| 93 | while [ ! -f "$TTCN3_PCAP_PATH/$TESTCASE.pcapng" ] || |
| 94 | [ "$(stat -c '%s' "$TTCN3_PCAP_PATH/$TESTCASE.pcapng")" -eq 32 ] |
| 95 | do |
| 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 |
| 102 | done |
| 103 | kill $PID |