arp_ndisc_evict_nocarrier.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Tests sysctl options {arp,ndisc}_evict_nocarrier={0,1}
  5. #
  6. # Create a veth pair and set IPs/routes on both. Then ping to establish
  7. # an entry in the ARP/ND table. Depending on the test set sysctl option to
  8. # 1 or 0. Set remote veth down which will cause local veth to go into a no
  9. # carrier state. Depending on the test check the ARP/ND table:
  10. #
  11. # {arp,ndisc}_evict_nocarrier=1 should contain no ARP/ND after no carrier
  12. # {arp,ndisc}_evict_nocarrer=0 should still contain the single ARP/ND entry
  13. #
  14. source lib.sh
  15. readonly V4_ADDR0=10.0.10.1
  16. readonly V4_ADDR1=10.0.10.2
  17. readonly V6_ADDR0=2001:db8:91::1
  18. readonly V6_ADDR1=2001:db8:91::2
  19. nsid=100
  20. ret=0
  21. cleanup_v6()
  22. {
  23. cleanup_ns ${me} ${peer}
  24. sysctl -w net.ipv6.conf.veth1.ndisc_evict_nocarrier=1 >/dev/null 2>&1
  25. sysctl -w net.ipv6.conf.all.ndisc_evict_nocarrier=1 >/dev/null 2>&1
  26. }
  27. setup_v6() {
  28. setup_ns me peer
  29. IP="ip -netns ${me}"
  30. $IP li add veth1 type veth peer name veth2
  31. $IP li set veth1 up
  32. $IP -6 addr add $V6_ADDR0/64 dev veth1 nodad
  33. $IP li set veth2 netns ${peer} up
  34. ip -netns ${peer} -6 addr add $V6_ADDR1/64 dev veth2 nodad
  35. ip netns exec ${me} sysctl -w $1 >/dev/null 2>&1
  36. # Establish an ND cache entry
  37. ip netns exec ${me} ping -6 -c1 -Iveth1 $V6_ADDR1 >/dev/null 2>&1
  38. # Should have the veth1 entry in ND table
  39. ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
  40. if [ $? -ne 0 ]; then
  41. cleanup_v6
  42. echo "failed"
  43. exit 1
  44. fi
  45. # Set veth2 down, which will put veth1 in NOCARRIER state
  46. ip netns exec ${peer} ip link set veth2 down
  47. }
  48. setup_v4() {
  49. setup_ns PEER_NS
  50. ip link add name veth0 type veth peer name veth1
  51. ip link set dev veth0 up
  52. ip link set dev veth1 netns "${PEER_NS}"
  53. ip netns exec "${PEER_NS}" ip link set dev veth1 up
  54. ip addr add $V4_ADDR0/24 dev veth0
  55. ip netns exec "${PEER_NS}" ip addr add $V4_ADDR1/24 dev veth1
  56. ip netns exec ${PEER_NS} ip route add default via $V4_ADDR1 dev veth1
  57. ip route add default via $V4_ADDR0 dev veth0
  58. sysctl -w "$1" >/dev/null 2>&1
  59. # Establish an ARP cache entry
  60. ping -c1 -I veth0 $V4_ADDR1 -q >/dev/null 2>&1
  61. # Should have the veth1 entry in ARP table
  62. ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
  63. if [ $? -ne 0 ]; then
  64. cleanup_v4
  65. echo "failed; is the system using MACAddressPolicy=persistent ?"
  66. exit 1
  67. fi
  68. # Set veth1 down, which will put veth0 in NOCARRIER state
  69. ip netns exec "${PEER_NS}" ip link set veth1 down
  70. }
  71. cleanup_v4() {
  72. ip neigh flush dev veth0
  73. ip link del veth0
  74. cleanup_ns $PEER_NS
  75. sysctl -w net.ipv4.conf.veth0.arp_evict_nocarrier=1 >/dev/null 2>&1
  76. sysctl -w net.ipv4.conf.all.arp_evict_nocarrier=1 >/dev/null 2>&1
  77. }
  78. # Run test when arp_evict_nocarrier = 1 (default).
  79. run_arp_evict_nocarrier_enabled() {
  80. echo "run arp_evict_nocarrier=1 test"
  81. setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=1"
  82. # ARP table should be empty
  83. ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
  84. if [ $? -eq 0 ];then
  85. echo "failed"
  86. ret=1
  87. else
  88. echo "ok"
  89. fi
  90. cleanup_v4
  91. }
  92. # Run test when arp_evict_nocarrier = 0
  93. run_arp_evict_nocarrier_disabled() {
  94. echo "run arp_evict_nocarrier=0 test"
  95. setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=0"
  96. # ARP table should still contain the entry
  97. ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
  98. if [ $? -eq 0 ];then
  99. echo "ok"
  100. else
  101. echo "failed"
  102. ret=1
  103. fi
  104. cleanup_v4
  105. }
  106. run_arp_evict_nocarrier_disabled_all() {
  107. echo "run all.arp_evict_nocarrier=0 test"
  108. setup_v4 "net.ipv4.conf.all.arp_evict_nocarrier=0"
  109. # ARP table should still contain the entry
  110. ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
  111. if [ $? -eq 0 ];then
  112. echo "ok"
  113. else
  114. echo "failed"
  115. fi
  116. cleanup_v4
  117. }
  118. run_ndisc_evict_nocarrier_enabled() {
  119. echo "run ndisc_evict_nocarrier=1 test"
  120. setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=1"
  121. ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
  122. if [ $? -eq 0 ];then
  123. echo "failed"
  124. ret=1
  125. else
  126. echo "ok"
  127. fi
  128. cleanup_v6
  129. }
  130. run_ndisc_evict_nocarrier_disabled() {
  131. echo "run ndisc_evict_nocarrier=0 test"
  132. setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=0"
  133. ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
  134. if [ $? -eq 0 ];then
  135. echo "ok"
  136. else
  137. echo "failed"
  138. ret=1
  139. fi
  140. cleanup_v6
  141. }
  142. run_ndisc_evict_nocarrier_disabled_all() {
  143. echo "run all.ndisc_evict_nocarrier=0 test"
  144. setup_v6 "net.ipv6.conf.all.ndisc_evict_nocarrier=0"
  145. ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
  146. if [ $? -eq 0 ];then
  147. echo "ok"
  148. else
  149. echo "failed"
  150. ret=1
  151. fi
  152. cleanup_v6
  153. }
  154. run_all_tests() {
  155. run_arp_evict_nocarrier_enabled
  156. run_arp_evict_nocarrier_disabled
  157. run_arp_evict_nocarrier_disabled_all
  158. run_ndisc_evict_nocarrier_enabled
  159. run_ndisc_evict_nocarrier_disabled
  160. run_ndisc_evict_nocarrier_disabled_all
  161. }
  162. if [ "$(id -u)" -ne 0 ];then
  163. echo "SKIP: Need root privileges"
  164. exit $ksft_skip;
  165. fi
  166. run_all_tests
  167. exit $ret