Skip to content

Commit bf59028

Browse files
oscar-maesPaolo Abeni
authored andcommitted
selftests: net: add test for destination in broadcast packets
Add test to check the broadcast ethernet destination field is set correctly. This test sends a broadcast ping, captures it using tcpdump and ensures that all bits of the 6 octet ethernet destination address are correctly set by examining the output capture file. Co-developed-by: Brett A C Sheffield <bacs@librecast.net> Signed-off-by: Brett A C Sheffield <bacs@librecast.net> Signed-off-by: Oscar Maes <oscmaes92@gmail.com> Link: https://patch.msgid.link/20250902150240.4272-1-oscmaes92@gmail.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent d3b2861 commit bf59028

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

tools/testing/selftests/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ TEST_PROGS += skf_net_off.sh
116116
TEST_GEN_FILES += skf_net_off
117117
TEST_GEN_FILES += tfo
118118
TEST_PROGS += tfo_passive.sh
119+
TEST_PROGS += broadcast_ether_dst.sh
119120
TEST_PROGS += broadcast_pmtu.sh
120121
TEST_PROGS += ipv6_force_forwarding.sh
121122

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Author: Brett A C Sheffield <bacs@librecast.net>
5+
# Author: Oscar Maes <oscmaes92@gmail.com>
6+
#
7+
# Ensure destination ethernet field is correctly set for
8+
# broadcast packets
9+
10+
source lib.sh
11+
12+
CLIENT_IP4="192.168.0.1"
13+
GW_IP4="192.168.0.2"
14+
15+
setup() {
16+
setup_ns CLIENT_NS SERVER_NS
17+
18+
ip -net "${SERVER_NS}" link add link1 type veth \
19+
peer name link0 netns "${CLIENT_NS}"
20+
21+
ip -net "${CLIENT_NS}" link set link0 up
22+
ip -net "${CLIENT_NS}" addr add "${CLIENT_IP4}"/24 dev link0
23+
24+
ip -net "${SERVER_NS}" link set link1 up
25+
26+
ip -net "${CLIENT_NS}" route add default via "${GW_IP4}"
27+
ip netns exec "${CLIENT_NS}" arp -s "${GW_IP4}" 00:11:22:33:44:55
28+
}
29+
30+
cleanup() {
31+
rm -f "${CAPFILE}" "${OUTPUT}"
32+
ip -net "${SERVER_NS}" link del link1
33+
cleanup_ns "${CLIENT_NS}" "${SERVER_NS}"
34+
}
35+
36+
test_broadcast_ether_dst() {
37+
local rc=0
38+
CAPFILE=$(mktemp -u cap.XXXXXXXXXX)
39+
OUTPUT=$(mktemp -u out.XXXXXXXXXX)
40+
41+
echo "Testing ethernet broadcast destination"
42+
43+
# start tcpdump listening for icmp
44+
# tcpdump will exit after receiving a single packet
45+
# timeout will kill tcpdump if it is still running after 2s
46+
timeout 2s ip netns exec "${CLIENT_NS}" \
47+
tcpdump -i link0 -c 1 -w "${CAPFILE}" icmp &> "${OUTPUT}" &
48+
pid=$!
49+
slowwait 1 grep -qs "listening" "${OUTPUT}"
50+
51+
# send broadcast ping
52+
ip netns exec "${CLIENT_NS}" \
53+
ping -W0.01 -c1 -b 255.255.255.255 &> /dev/null
54+
55+
# wait for tcpdump for exit after receiving packet
56+
wait "${pid}"
57+
58+
# compare ethernet destination field to ff:ff:ff:ff:ff:ff
59+
ether_dst=$(tcpdump -r "${CAPFILE}" -tnne 2>/dev/null | \
60+
awk '{sub(/,/,"",$3); print $3}')
61+
if [[ "${ether_dst}" == "ff:ff:ff:ff:ff:ff" ]]; then
62+
echo "[ OK ]"
63+
rc="${ksft_pass}"
64+
else
65+
echo "[FAIL] expected dst ether addr to be ff:ff:ff:ff:ff:ff," \
66+
"got ${ether_dst}"
67+
rc="${ksft_fail}"
68+
fi
69+
70+
return "${rc}"
71+
}
72+
73+
if [ ! -x "$(command -v tcpdump)" ]; then
74+
echo "SKIP: Could not run test without tcpdump tool"
75+
exit "${ksft_skip}"
76+
fi
77+
78+
trap cleanup EXIT
79+
80+
setup
81+
test_broadcast_ether_dst
82+
83+
exit $?

0 commit comments

Comments
 (0)