test_flow_dissector.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Load BPF flow dissector and verify it correctly dissects traffic
  5. BPF_FILE="bpf_flow.bpf.o"
  6. export TESTNAME=test_flow_dissector
  7. unmount=0
  8. # Kselftest framework requirement - SKIP code is 4.
  9. ksft_skip=4
  10. msg="skip all tests:"
  11. if [ $UID != 0 ]; then
  12. echo $msg please run this as root >&2
  13. exit $ksft_skip
  14. fi
  15. # This test needs to be run in a network namespace with in_netns.sh. Check if
  16. # this is the case and run it with in_netns.sh if it is being run in the root
  17. # namespace.
  18. if [[ -z $(ip netns identify $$) ]]; then
  19. err=0
  20. if bpftool="$(which bpftool)"; then
  21. echo "Testing global flow dissector..."
  22. $bpftool prog loadall $BPF_FILE /sys/fs/bpf/flow \
  23. type flow_dissector
  24. if ! unshare --net $bpftool prog attach pinned \
  25. /sys/fs/bpf/flow/_dissect flow_dissector; then
  26. echo "Unexpected unsuccessful attach in namespace" >&2
  27. err=1
  28. fi
  29. $bpftool prog attach pinned /sys/fs/bpf/flow/_dissect \
  30. flow_dissector
  31. if unshare --net $bpftool prog attach pinned \
  32. /sys/fs/bpf/flow/_dissect flow_dissector; then
  33. echo "Unexpected successful attach in namespace" >&2
  34. err=1
  35. fi
  36. if ! $bpftool prog detach pinned \
  37. /sys/fs/bpf/flow/_dissect flow_dissector; then
  38. echo "Failed to detach flow dissector" >&2
  39. err=1
  40. fi
  41. rm -rf /sys/fs/bpf/flow
  42. else
  43. echo "Skipping root flow dissector test, bpftool not found" >&2
  44. fi
  45. # Run the rest of the tests in a net namespace.
  46. ../net/in_netns.sh "$0" "$@"
  47. err=$(( $err + $? ))
  48. if (( $err == 0 )); then
  49. echo "selftests: $TESTNAME [PASS]";
  50. else
  51. echo "selftests: $TESTNAME [FAILED]";
  52. fi
  53. exit $err
  54. fi
  55. # Determine selftest success via shell exit code
  56. exit_handler()
  57. {
  58. set +e
  59. # Cleanup
  60. tc filter del dev lo ingress pref 1337 2> /dev/null
  61. tc qdisc del dev lo ingress 2> /dev/null
  62. ./flow_dissector_load -d 2> /dev/null
  63. if [ $unmount -ne 0 ]; then
  64. umount bpffs 2> /dev/null
  65. fi
  66. }
  67. # Exit script immediately (well catched by trap handler) if any
  68. # program/thing exits with a non-zero status.
  69. set -e
  70. # (Use 'trap -l' to list meaning of numbers)
  71. trap exit_handler 0 2 3 6 9
  72. # Mount BPF file system
  73. if /bin/mount | grep /sys/fs/bpf > /dev/null; then
  74. echo "bpffs already mounted"
  75. else
  76. echo "bpffs not mounted. Mounting..."
  77. unmount=1
  78. /bin/mount bpffs /sys/fs/bpf -t bpf
  79. fi
  80. # Attach BPF program
  81. ./flow_dissector_load -p $BPF_FILE -s _dissect
  82. # Setup
  83. tc qdisc add dev lo ingress
  84. echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter
  85. echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
  86. echo 0 > /proc/sys/net/ipv4/conf/lo/rp_filter
  87. echo "Testing IPv4..."
  88. # Drops all IP/UDP packets coming from port 9
  89. tc filter add dev lo parent ffff: protocol ip pref 1337 flower ip_proto \
  90. udp src_port 9 action drop
  91. # Send 10 IPv4/UDP packets from port 8. Filter should not drop any.
  92. ./test_flow_dissector -i 4 -f 8
  93. # Send 10 IPv4/UDP packets from port 9. Filter should drop all.
  94. ./test_flow_dissector -i 4 -f 9 -F
  95. # Send 10 IPv4/UDP packets from port 10. Filter should not drop any.
  96. ./test_flow_dissector -i 4 -f 10
  97. echo "Testing IPv4 from 127.0.0.127 (fallback to generic dissector)..."
  98. # Send 10 IPv4/UDP packets from port 8. Filter should not drop any.
  99. ./test_flow_dissector -i 4 -S 127.0.0.127 -f 8
  100. # Send 10 IPv4/UDP packets from port 9. Filter should drop all.
  101. ./test_flow_dissector -i 4 -S 127.0.0.127 -f 9 -F
  102. # Send 10 IPv4/UDP packets from port 10. Filter should not drop any.
  103. ./test_flow_dissector -i 4 -S 127.0.0.127 -f 10
  104. echo "Testing IPIP..."
  105. # Send 10 IPv4/IPv4/UDP packets from port 8. Filter should not drop any.
  106. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
  107. -D 192.168.0.1 -S 1.1.1.1 -f 8
  108. # Send 10 IPv4/IPv4/UDP packets from port 9. Filter should drop all.
  109. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
  110. -D 192.168.0.1 -S 1.1.1.1 -f 9 -F
  111. # Send 10 IPv4/IPv4/UDP packets from port 10. Filter should not drop any.
  112. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
  113. -D 192.168.0.1 -S 1.1.1.1 -f 10
  114. echo "Testing IPv4 + GRE..."
  115. # Send 10 IPv4/GRE/IPv4/UDP packets from port 8. Filter should not drop any.
  116. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
  117. -D 192.168.0.1 -S 1.1.1.1 -f 8
  118. # Send 10 IPv4/GRE/IPv4/UDP packets from port 9. Filter should drop all.
  119. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
  120. -D 192.168.0.1 -S 1.1.1.1 -f 9 -F
  121. # Send 10 IPv4/GRE/IPv4/UDP packets from port 10. Filter should not drop any.
  122. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
  123. -D 192.168.0.1 -S 1.1.1.1 -f 10
  124. tc filter del dev lo ingress pref 1337
  125. echo "Testing port range..."
  126. # Drops all IP/UDP packets coming from port 8-10
  127. tc filter add dev lo parent ffff: protocol ip pref 1337 flower ip_proto \
  128. udp src_port 8-10 action drop
  129. # Send 10 IPv4/UDP packets from port 7. Filter should not drop any.
  130. ./test_flow_dissector -i 4 -f 7
  131. # Send 10 IPv4/UDP packets from port 9. Filter should drop all.
  132. ./test_flow_dissector -i 4 -f 9 -F
  133. # Send 10 IPv4/UDP packets from port 11. Filter should not drop any.
  134. ./test_flow_dissector -i 4 -f 11
  135. tc filter del dev lo ingress pref 1337
  136. echo "Testing IPv6..."
  137. # Drops all IPv6/UDP packets coming from port 9
  138. tc filter add dev lo parent ffff: protocol ipv6 pref 1337 flower ip_proto \
  139. udp src_port 9 action drop
  140. # Send 10 IPv6/UDP packets from port 8. Filter should not drop any.
  141. ./test_flow_dissector -i 6 -f 8
  142. # Send 10 IPv6/UDP packets from port 9. Filter should drop all.
  143. ./test_flow_dissector -i 6 -f 9 -F
  144. # Send 10 IPv6/UDP packets from port 10. Filter should not drop any.
  145. ./test_flow_dissector -i 6 -f 10
  146. exit 0