unicast_extensions.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # By Seth Schoen (c) 2021, for the IPv4 Unicast Extensions Project
  5. # Thanks to David Ahern for help and advice on nettest modifications.
  6. #
  7. # Self-tests for IPv4 address extensions: the kernel's ability to accept
  8. # certain traditionally unused or unallocated IPv4 addresses. For each kind
  9. # of address, we test for interface assignment, ping, TCP, and forwarding.
  10. # Must be run as root (to manipulate network namespaces and virtual
  11. # interfaces).
  12. #
  13. # Things we test for here:
  14. #
  15. # * Currently the kernel accepts addresses in 0/8 and 240/4 as valid.
  16. #
  17. # * Notwithstanding that, 0.0.0.0 and 255.255.255.255 cannot be assigned.
  18. #
  19. # * Currently the kernel DOES NOT accept unicast use of the lowest
  20. # address in an IPv4 subnet (e.g. 192.168.100.0/32 in 192.168.100.0/24).
  21. # This is treated as a second broadcast address, for compatibility
  22. # with 4.2BSD (!).
  23. #
  24. # * Currently the kernel DOES NOT accept unicast use of any of 127/8.
  25. #
  26. # * Currently the kernel DOES NOT accept unicast use of any of 224/4.
  27. #
  28. # These tests provide an easy way to flip the expected result of any
  29. # of these behaviors for testing kernel patches that change them.
  30. # Kselftest framework requirement - SKIP code is 4.
  31. ksft_skip=4
  32. # nettest can be run from PATH or from same directory as this selftest
  33. if ! which nettest >/dev/null; then
  34. PATH=$PWD:$PATH
  35. if ! which nettest >/dev/null; then
  36. echo "'nettest' command not found; skipping tests"
  37. exit $ksft_skip
  38. fi
  39. fi
  40. result=0
  41. hide_output(){ exec 3>&1 4>&2 >/dev/null 2>/dev/null; }
  42. show_output(){ exec >&3 2>&4; }
  43. show_result(){
  44. if [ $1 -eq 0 ]; then
  45. printf "TEST: %-60s [ OK ]\n" "${2}"
  46. else
  47. printf "TEST: %-60s [FAIL]\n" "${2}"
  48. result=1
  49. fi
  50. }
  51. _do_segmenttest(){
  52. # Perform a simple set of link tests between a pair of
  53. # IP addresses on a shared (virtual) segment, using
  54. # ping and nettest.
  55. # foo --- bar
  56. # Arguments: ip_a ip_b prefix_length test_description
  57. #
  58. # Caller must set up foo-ns and bar-ns namespaces
  59. # containing linked veth devices foo and bar,
  60. # respectively.
  61. ip -n foo-ns address add $1/$3 dev foo || return 1
  62. ip -n foo-ns link set foo up || return 1
  63. ip -n bar-ns address add $2/$3 dev bar || return 1
  64. ip -n bar-ns link set bar up || return 1
  65. ip netns exec foo-ns timeout 2 ping -c 1 $2 || return 1
  66. ip netns exec bar-ns timeout 2 ping -c 1 $1 || return 1
  67. nettest -B -N bar-ns -O foo-ns -r $1 || return 1
  68. nettest -B -N foo-ns -O bar-ns -r $2 || return 1
  69. return 0
  70. }
  71. _do_route_test(){
  72. # Perform a simple set of gateway tests.
  73. #
  74. # [foo] <---> [foo1]-[bar1] <---> [bar] /prefix
  75. # host gateway host
  76. #
  77. # Arguments: foo_ip foo1_ip bar1_ip bar_ip prefix_len test_description
  78. # Displays test result and returns success or failure.
  79. # Caller must set up foo-ns, bar-ns, and router-ns
  80. # containing linked veth devices foo-foo1, bar1-bar
  81. # (foo in foo-ns, foo1 and bar1 in router-ns, and
  82. # bar in bar-ns).
  83. ip -n foo-ns address add $1/$5 dev foo || return 1
  84. ip -n foo-ns link set foo up || return 1
  85. ip -n foo-ns route add default via $2 || return 1
  86. ip -n bar-ns address add $4/$5 dev bar || return 1
  87. ip -n bar-ns link set bar up || return 1
  88. ip -n bar-ns route add default via $3 || return 1
  89. ip -n router-ns address add $2/$5 dev foo1 || return 1
  90. ip -n router-ns link set foo1 up || return 1
  91. ip -n router-ns address add $3/$5 dev bar1 || return 1
  92. ip -n router-ns link set bar1 up || return 1
  93. echo 1 | ip netns exec router-ns tee /proc/sys/net/ipv4/ip_forward
  94. ip netns exec foo-ns timeout 2 ping -c 1 $2 || return 1
  95. ip netns exec foo-ns timeout 2 ping -c 1 $4 || return 1
  96. ip netns exec bar-ns timeout 2 ping -c 1 $3 || return 1
  97. ip netns exec bar-ns timeout 2 ping -c 1 $1 || return 1
  98. nettest -B -N bar-ns -O foo-ns -r $1 || return 1
  99. nettest -B -N foo-ns -O bar-ns -r $4 || return 1
  100. return 0
  101. }
  102. segmenttest(){
  103. # Sets up veth link and tries to connect over it.
  104. # Arguments: ip_a ip_b prefix_len test_description
  105. hide_output
  106. ip netns add foo-ns
  107. ip netns add bar-ns
  108. ip link add foo netns foo-ns type veth peer name bar netns bar-ns
  109. test_result=0
  110. _do_segmenttest "$@" || test_result=1
  111. ip netns pids foo-ns | xargs -r kill -9
  112. ip netns pids bar-ns | xargs -r kill -9
  113. ip netns del foo-ns
  114. ip netns del bar-ns
  115. show_output
  116. # inverted tests will expect failure instead of success
  117. [ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
  118. show_result $test_result "$4"
  119. }
  120. route_test(){
  121. # Sets up a simple gateway and tries to connect through it.
  122. # [foo] <---> [foo1]-[bar1] <---> [bar] /prefix
  123. # Arguments: foo_ip foo1_ip bar1_ip bar_ip prefix_len test_description
  124. # Returns success or failure.
  125. hide_output
  126. ip netns add foo-ns
  127. ip netns add bar-ns
  128. ip netns add router-ns
  129. ip link add foo netns foo-ns type veth peer name foo1 netns router-ns
  130. ip link add bar netns bar-ns type veth peer name bar1 netns router-ns
  131. test_result=0
  132. _do_route_test "$@" || test_result=1
  133. ip netns pids foo-ns | xargs -r kill -9
  134. ip netns pids bar-ns | xargs -r kill -9
  135. ip netns pids router-ns | xargs -r kill -9
  136. ip netns del foo-ns
  137. ip netns del bar-ns
  138. ip netns del router-ns
  139. show_output
  140. # inverted tests will expect failure instead of success
  141. [ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
  142. show_result $test_result "$6"
  143. }
  144. echo "###########################################################################"
  145. echo "Unicast address extensions tests (behavior of reserved IPv4 addresses)"
  146. echo "###########################################################################"
  147. #
  148. # Test support for 240/4
  149. segmenttest 240.1.2.1 240.1.2.4 24 "assign and ping within 240/4 (1 of 2) (is allowed)"
  150. segmenttest 250.100.2.1 250.100.30.4 16 "assign and ping within 240/4 (2 of 2) (is allowed)"
  151. #
  152. # Test support for 0/8
  153. segmenttest 0.1.2.17 0.1.2.23 24 "assign and ping within 0/8 (1 of 2) (is allowed)"
  154. segmenttest 0.77.240.17 0.77.2.23 16 "assign and ping within 0/8 (2 of 2) (is allowed)"
  155. #
  156. # Even 255.255/16 is OK!
  157. segmenttest 255.255.3.1 255.255.50.77 16 "assign and ping inside 255.255/16 (is allowed)"
  158. #
  159. # Or 255.255.255/24
  160. segmenttest 255.255.255.1 255.255.255.254 24 "assign and ping inside 255.255.255/24 (is allowed)"
  161. #
  162. # Routing between different networks
  163. route_test 240.5.6.7 240.5.6.1 255.1.2.1 255.1.2.3 24 "route between 240.5.6/24 and 255.1.2/24 (is allowed)"
  164. route_test 0.200.6.7 0.200.38.1 245.99.101.1 245.99.200.111 16 "route between 0.200/16 and 245.99/16 (is allowed)"
  165. #
  166. # Test support for lowest address ending in .0
  167. segmenttest 5.10.15.20 5.10.15.0 24 "assign and ping lowest address (/24)"
  168. #
  169. # Test support for lowest address not ending in .0
  170. segmenttest 192.168.101.192 192.168.101.193 26 "assign and ping lowest address (/26)"
  171. #
  172. # Routing using lowest address as a gateway/endpoint
  173. route_test 192.168.42.1 192.168.42.0 9.8.7.6 9.8.7.0 24 "routing using lowest address"
  174. #
  175. # ==============================================
  176. # ==== TESTS THAT CURRENTLY EXPECT FAILURE =====
  177. # ==============================================
  178. expect_failure=true
  179. # It should still not be possible to use 0.0.0.0 or 255.255.255.255
  180. # as a unicast address. Thus, these tests expect failure.
  181. segmenttest 0.0.1.5 0.0.0.0 16 "assigning 0.0.0.0 (is forbidden)"
  182. segmenttest 255.255.255.1 255.255.255.255 16 "assigning 255.255.255.255 (is forbidden)"
  183. #
  184. # Test support for not having all of 127 be loopback
  185. # Currently Linux does not allow this, so this should fail too
  186. segmenttest 127.99.4.5 127.99.4.6 16 "assign and ping inside 127/8 (is forbidden)"
  187. #
  188. # Test support for unicast use of class D
  189. # Currently Linux does not allow this, so this should fail too
  190. segmenttest 225.1.2.3 225.1.2.200 24 "assign and ping class D address (is forbidden)"
  191. #
  192. # Routing using class D as a gateway
  193. route_test 225.1.42.1 225.1.42.2 9.8.7.6 9.8.7.1 24 "routing using class D (is forbidden)"
  194. #
  195. # Routing using 127/8
  196. # Currently Linux does not allow this, so this should fail too
  197. route_test 127.99.2.3 127.99.2.4 200.1.2.3 200.1.2.4 24 "routing using 127/8 (is forbidden)"
  198. #
  199. unset expect_failure
  200. # =====================================================
  201. # ==== END OF TESTS THAT CURRENTLY EXPECT FAILURE =====
  202. # =====================================================
  203. exit ${result}