xref: /linux/tools/testing/selftests/net/broadcast_ether_dst.sh (revision db87bd2ad1f736c2f7ab231f9b40c885934f6b2c)
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
10source lib.sh
11
12CLIENT_IP4="192.168.0.1"
13GW_IP4="192.168.0.2"
14
15setup() {
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
30cleanup() {
31	rm -f "${CAPFILE}" "${OUTPUT}"
32	ip -net "${SERVER_NS}" link del link1
33	cleanup_ns "${CLIENT_NS}" "${SERVER_NS}"
34}
35
36test_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
73if [ ! -x "$(command -v tcpdump)" ]; then
74	echo "SKIP: Could not run test without tcpdump tool"
75	exit "${ksft_skip}"
76fi
77
78trap cleanup EXIT
79
80setup
81test_broadcast_ether_dst
82
83exit $?
84