broadcast_ether_dst.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. source lib.sh
  10. CLIENT_IP4="192.168.0.1"
  11. GW_IP4="192.168.0.2"
  12. setup() {
  13. setup_ns CLIENT_NS SERVER_NS
  14. ip -net "${SERVER_NS}" link add link1 type veth \
  15. peer name link0 netns "${CLIENT_NS}"
  16. ip -net "${CLIENT_NS}" link set link0 up
  17. ip -net "${CLIENT_NS}" addr add "${CLIENT_IP4}"/24 dev link0
  18. ip -net "${SERVER_NS}" link set link1 up
  19. ip -net "${CLIENT_NS}" route add default via "${GW_IP4}"
  20. ip netns exec "${CLIENT_NS}" arp -s "${GW_IP4}" 00:11:22:33:44:55
  21. }
  22. cleanup() {
  23. rm -f "${CAPFILE}" "${OUTPUT}"
  24. ip -net "${SERVER_NS}" link del link1
  25. cleanup_ns "${CLIENT_NS}" "${SERVER_NS}"
  26. }
  27. test_broadcast_ether_dst() {
  28. local rc=0
  29. CAPFILE=$(mktemp -u cap.XXXXXXXXXX)
  30. OUTPUT=$(mktemp -u out.XXXXXXXXXX)
  31. echo "Testing ethernet broadcast destination"
  32. # start tcpdump listening for icmp
  33. # tcpdump will exit after receiving a single packet
  34. # timeout will kill tcpdump if it is still running after 2s
  35. timeout 2s ip netns exec "${CLIENT_NS}" \
  36. tcpdump -i link0 -c 1 -w "${CAPFILE}" icmp &> "${OUTPUT}" &
  37. pid=$!
  38. slowwait 1 grep -qs "listening" "${OUTPUT}"
  39. # send broadcast ping
  40. ip netns exec "${CLIENT_NS}" \
  41. ping -W0.01 -c1 -b 255.255.255.255 &> /dev/null
  42. # wait for tcpdump for exit after receiving packet
  43. wait "${pid}"
  44. # compare ethernet destination field to ff:ff:ff:ff:ff:ff
  45. ether_dst=$(tcpdump -r "${CAPFILE}" -tnne 2>/dev/null | \
  46. awk '{sub(/,/,"",$3); print $3}')
  47. if [[ "${ether_dst}" == "ff:ff:ff:ff:ff:ff" ]]; then
  48. echo "[ OK ]"
  49. rc="${ksft_pass}"
  50. else
  51. echo "[FAIL] expected dst ether addr to be ff:ff:ff:ff:ff:ff," \
  52. "got ${ether_dst}"
  53. rc="${ksft_fail}"
  54. fi
  55. return "${rc}"
  56. }
  57. if [ ! -x "$(command -v tcpdump)" ]; then
  58. echo "SKIP: Could not run test without tcpdump tool"
  59. exit "${ksft_skip}"
  60. fi
  61. trap cleanup EXIT
  62. setup
  63. test_broadcast_ether_dst
  64. exit $?