fib_rule_tests.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # This test is for checking IPv4 and IPv6 FIB rules API
  4. ret=0
  5. PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}
  6. IP="ip -netns testns"
  7. RTABLE=100
  8. GW_IP4=192.51.100.2
  9. SRC_IP=192.51.100.3
  10. GW_IP6=2001:db8:1::2
  11. SRC_IP6=2001:db8:1::3
  12. DEV_ADDR=192.51.100.1
  13. DEV_ADDR6=2001:db8:1::1
  14. DEV=dummy0
  15. log_test()
  16. {
  17. local rc=$1
  18. local expected=$2
  19. local msg="$3"
  20. if [ ${rc} -eq ${expected} ]; then
  21. nsuccess=$((nsuccess+1))
  22. printf "\n TEST: %-50s [ OK ]\n" "${msg}"
  23. else
  24. ret=1
  25. nfail=$((nfail+1))
  26. printf "\n TEST: %-50s [FAIL]\n" "${msg}"
  27. if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
  28. echo
  29. echo "hit enter to continue, 'q' to quit"
  30. read a
  31. [ "$a" = "q" ] && exit 1
  32. fi
  33. fi
  34. }
  35. log_section()
  36. {
  37. echo
  38. echo "######################################################################"
  39. echo "TEST SECTION: $*"
  40. echo "######################################################################"
  41. }
  42. setup()
  43. {
  44. set -e
  45. ip netns add testns
  46. $IP link set dev lo up
  47. $IP link add dummy0 type dummy
  48. $IP link set dev dummy0 up
  49. $IP address add $DEV_ADDR/24 dev dummy0
  50. $IP -6 address add $DEV_ADDR6/64 dev dummy0
  51. set +e
  52. }
  53. cleanup()
  54. {
  55. $IP link del dev dummy0 &> /dev/null
  56. ip netns del testns
  57. }
  58. fib_check_iproute_support()
  59. {
  60. ip rule help 2>&1 | grep -q $1
  61. if [ $? -ne 0 ]; then
  62. echo "SKIP: iproute2 iprule too old, missing $1 match"
  63. return 1
  64. fi
  65. ip route get help 2>&1 | grep -q $2
  66. if [ $? -ne 0 ]; then
  67. echo "SKIP: iproute2 get route too old, missing $2 match"
  68. return 1
  69. fi
  70. return 0
  71. }
  72. fib_rule6_del()
  73. {
  74. $IP -6 rule del $1
  75. log_test $? 0 "rule6 del $1"
  76. }
  77. fib_rule6_del_by_pref()
  78. {
  79. pref=$($IP -6 rule show | grep "$1 lookup $TABLE" | cut -d ":" -f 1)
  80. $IP -6 rule del pref $pref
  81. }
  82. fib_rule6_test_match_n_redirect()
  83. {
  84. local match="$1"
  85. local getmatch="$2"
  86. $IP -6 rule add $match table $RTABLE
  87. $IP -6 route get $GW_IP6 $getmatch | grep -q "table $RTABLE"
  88. log_test $? 0 "rule6 check: $1"
  89. fib_rule6_del_by_pref "$match"
  90. log_test $? 0 "rule6 del by pref: $match"
  91. }
  92. fib_rule6_test()
  93. {
  94. # setup the fib rule redirect route
  95. $IP -6 route add table $RTABLE default via $GW_IP6 dev $DEV onlink
  96. match="oif $DEV"
  97. fib_rule6_test_match_n_redirect "$match" "$match" "oif redirect to table"
  98. match="from $SRC_IP6 iif $DEV"
  99. fib_rule6_test_match_n_redirect "$match" "$match" "iif redirect to table"
  100. match="tos 0x10"
  101. fib_rule6_test_match_n_redirect "$match" "$match" "tos redirect to table"
  102. match="fwmark 0x64"
  103. getmatch="mark 0x64"
  104. fib_rule6_test_match_n_redirect "$match" "$getmatch" "fwmark redirect to table"
  105. fib_check_iproute_support "uidrange" "uid"
  106. if [ $? -eq 0 ]; then
  107. match="uidrange 100-100"
  108. getmatch="uid 100"
  109. fib_rule6_test_match_n_redirect "$match" "$getmatch" "uid redirect to table"
  110. fi
  111. fib_check_iproute_support "sport" "sport"
  112. if [ $? -eq 0 ]; then
  113. match="sport 666 dport 777"
  114. fib_rule6_test_match_n_redirect "$match" "$match" "sport and dport redirect to table"
  115. fi
  116. fib_check_iproute_support "ipproto" "ipproto"
  117. if [ $? -eq 0 ]; then
  118. match="ipproto tcp"
  119. fib_rule6_test_match_n_redirect "$match" "$match" "ipproto match"
  120. fi
  121. fib_check_iproute_support "ipproto" "ipproto"
  122. if [ $? -eq 0 ]; then
  123. match="ipproto ipv6-icmp"
  124. fib_rule6_test_match_n_redirect "$match" "$match" "ipproto ipv6-icmp match"
  125. fi
  126. }
  127. fib_rule4_del()
  128. {
  129. $IP rule del $1
  130. log_test $? 0 "del $1"
  131. }
  132. fib_rule4_del_by_pref()
  133. {
  134. pref=$($IP rule show | grep "$1 lookup $TABLE" | cut -d ":" -f 1)
  135. $IP rule del pref $pref
  136. }
  137. fib_rule4_test_match_n_redirect()
  138. {
  139. local match="$1"
  140. local getmatch="$2"
  141. $IP rule add $match table $RTABLE
  142. $IP route get $GW_IP4 $getmatch | grep -q "table $RTABLE"
  143. log_test $? 0 "rule4 check: $1"
  144. fib_rule4_del_by_pref "$match"
  145. log_test $? 0 "rule4 del by pref: $match"
  146. }
  147. fib_rule4_test()
  148. {
  149. # setup the fib rule redirect route
  150. $IP route add table $RTABLE default via $GW_IP4 dev $DEV onlink
  151. match="oif $DEV"
  152. fib_rule4_test_match_n_redirect "$match" "$match" "oif redirect to table"
  153. match="from $SRC_IP iif $DEV"
  154. fib_rule4_test_match_n_redirect "$match" "$match" "iif redirect to table"
  155. match="tos 0x10"
  156. fib_rule4_test_match_n_redirect "$match" "$match" "tos redirect to table"
  157. match="fwmark 0x64"
  158. getmatch="mark 0x64"
  159. fib_rule4_test_match_n_redirect "$match" "$getmatch" "fwmark redirect to table"
  160. fib_check_iproute_support "uidrange" "uid"
  161. if [ $? -eq 0 ]; then
  162. match="uidrange 100-100"
  163. getmatch="uid 100"
  164. fib_rule4_test_match_n_redirect "$match" "$getmatch" "uid redirect to table"
  165. fi
  166. fib_check_iproute_support "sport" "sport"
  167. if [ $? -eq 0 ]; then
  168. match="sport 666 dport 777"
  169. fib_rule4_test_match_n_redirect "$match" "$match" "sport and dport redirect to table"
  170. fi
  171. fib_check_iproute_support "ipproto" "ipproto"
  172. if [ $? -eq 0 ]; then
  173. match="ipproto tcp"
  174. fib_rule4_test_match_n_redirect "$match" "$match" "ipproto tcp match"
  175. fi
  176. fib_check_iproute_support "ipproto" "ipproto"
  177. if [ $? -eq 0 ]; then
  178. match="ipproto icmp"
  179. fib_rule4_test_match_n_redirect "$match" "$match" "ipproto icmp match"
  180. fi
  181. }
  182. run_fibrule_tests()
  183. {
  184. log_section "IPv4 fib rule"
  185. fib_rule4_test
  186. log_section "IPv6 fib rule"
  187. fib_rule6_test
  188. }
  189. if [ "$(id -u)" -ne 0 ];then
  190. echo "SKIP: Need root privileges"
  191. exit 0
  192. fi
  193. if [ ! -x "$(command -v ip)" ]; then
  194. echo "SKIP: Could not run test without ip tool"
  195. exit 0
  196. fi
  197. # start clean
  198. cleanup &> /dev/null
  199. setup
  200. run_fibrule_tests
  201. cleanup
  202. if [ "$TESTS" != "none" ]; then
  203. printf "\nTests passed: %3d\n" ${nsuccess}
  204. printf "Tests failed: %3d\n" ${nfail}
  205. fi
  206. exit $ret