unicast_extensions.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/bin/bash
  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. source lib.sh
  31. check_gen_prog "nettest"
  32. result=0
  33. hide_output(){ exec 3>&1 4>&2 >/dev/null 2>/dev/null; }
  34. show_output(){ exec >&3 2>&4; }
  35. show_result(){
  36. if [ $1 -eq 0 ]; then
  37. printf "TEST: %-60s [ OK ]\n" "${2}"
  38. else
  39. printf "TEST: %-60s [FAIL]\n" "${2}"
  40. result=1
  41. fi
  42. }
  43. _do_segmenttest(){
  44. # Perform a simple set of link tests between a pair of
  45. # IP addresses on a shared (virtual) segment, using
  46. # ping and nettest.
  47. # foo --- bar
  48. # Arguments: ip_a ip_b prefix_length test_description
  49. #
  50. # Caller must set up $foo_ns and $bar_ns namespaces
  51. # containing linked veth devices foo and bar,
  52. # respectively.
  53. ip -n $foo_ns address add $1/$3 dev foo || return 1
  54. ip -n $foo_ns link set foo up || return 1
  55. ip -n $bar_ns address add $2/$3 dev bar || return 1
  56. ip -n $bar_ns link set bar up || return 1
  57. ip netns exec $foo_ns timeout 2 ping -c 1 $2 || return 1
  58. ip netns exec $bar_ns timeout 2 ping -c 1 $1 || return 1
  59. nettest -B -N $bar_ns -O $foo_ns -r $1 || return 1
  60. nettest -B -N $foo_ns -O $bar_ns -r $2 || return 1
  61. return 0
  62. }
  63. _do_route_test(){
  64. # Perform a simple set of gateway tests.
  65. #
  66. # [foo] <---> [foo1]-[bar1] <---> [bar] /prefix
  67. # host gateway host
  68. #
  69. # Arguments: foo_ip foo1_ip bar1_ip bar_ip prefix_len test_description
  70. # Displays test result and returns success or failure.
  71. # Caller must set up $foo_ns, $bar_ns, and $router_ns
  72. # containing linked veth devices foo-foo1, bar1-bar
  73. # (foo in $foo_ns, foo1 and bar1 in $router_ns, and
  74. # bar in $bar_ns).
  75. ip -n $foo_ns address add $1/$5 dev foo || return 1
  76. ip -n $foo_ns link set foo up || return 1
  77. ip -n $foo_ns route add default via $2 || return 1
  78. ip -n $bar_ns address add $4/$5 dev bar || return 1
  79. ip -n $bar_ns link set bar up || return 1
  80. ip -n $bar_ns route add default via $3 || return 1
  81. ip -n $router_ns address add $2/$5 dev foo1 || return 1
  82. ip -n $router_ns link set foo1 up || return 1
  83. ip -n $router_ns address add $3/$5 dev bar1 || return 1
  84. ip -n $router_ns link set bar1 up || return 1
  85. echo 1 | ip netns exec $router_ns tee /proc/sys/net/ipv4/ip_forward
  86. ip netns exec $foo_ns timeout 2 ping -c 1 $2 || return 1
  87. ip netns exec $foo_ns timeout 2 ping -c 1 $4 || return 1
  88. ip netns exec $bar_ns timeout 2 ping -c 1 $3 || return 1
  89. ip netns exec $bar_ns timeout 2 ping -c 1 $1 || return 1
  90. nettest -B -N $bar_ns -O $foo_ns -r $1 || return 1
  91. nettest -B -N $foo_ns -O $bar_ns -r $4 || return 1
  92. return 0
  93. }
  94. segmenttest(){
  95. # Sets up veth link and tries to connect over it.
  96. # Arguments: ip_a ip_b prefix_len test_description
  97. hide_output
  98. setup_ns foo_ns bar_ns
  99. ip link add foo netns $foo_ns type veth peer name bar netns $bar_ns
  100. test_result=0
  101. _do_segmenttest "$@" || test_result=1
  102. ip netns pids $foo_ns | xargs -r kill -9
  103. ip netns pids $bar_ns | xargs -r kill -9
  104. cleanup_ns $foo_ns $bar_ns
  105. show_output
  106. # inverted tests will expect failure instead of success
  107. [ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
  108. show_result $test_result "$4"
  109. }
  110. route_test(){
  111. # Sets up a simple gateway and tries to connect through it.
  112. # [foo] <---> [foo1]-[bar1] <---> [bar] /prefix
  113. # Arguments: foo_ip foo1_ip bar1_ip bar_ip prefix_len test_description
  114. # Returns success or failure.
  115. hide_output
  116. setup_ns foo_ns bar_ns router_ns
  117. ip link add foo netns $foo_ns type veth peer name foo1 netns $router_ns
  118. ip link add bar netns $bar_ns type veth peer name bar1 netns $router_ns
  119. test_result=0
  120. _do_route_test "$@" || test_result=1
  121. ip netns pids $foo_ns | xargs -r kill -9
  122. ip netns pids $bar_ns | xargs -r kill -9
  123. ip netns pids $router_ns | xargs -r kill -9
  124. cleanup_ns $foo_ns $bar_ns $router_ns
  125. show_output
  126. # inverted tests will expect failure instead of success
  127. [ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
  128. show_result $test_result "$6"
  129. }
  130. echo "###########################################################################"
  131. echo "Unicast address extensions tests (behavior of reserved IPv4 addresses)"
  132. echo "###########################################################################"
  133. #
  134. # Test support for 240/4
  135. segmenttest 240.1.2.1 240.1.2.4 24 "assign and ping within 240/4 (1 of 2) (is allowed)"
  136. segmenttest 250.100.2.1 250.100.30.4 16 "assign and ping within 240/4 (2 of 2) (is allowed)"
  137. #
  138. # Test support for 0/8
  139. segmenttest 0.1.2.17 0.1.2.23 24 "assign and ping within 0/8 (1 of 2) (is allowed)"
  140. segmenttest 0.77.240.17 0.77.2.23 16 "assign and ping within 0/8 (2 of 2) (is allowed)"
  141. #
  142. # Even 255.255/16 is OK!
  143. segmenttest 255.255.3.1 255.255.50.77 16 "assign and ping inside 255.255/16 (is allowed)"
  144. #
  145. # Or 255.255.255/24
  146. segmenttest 255.255.255.1 255.255.255.254 24 "assign and ping inside 255.255.255/24 (is allowed)"
  147. #
  148. # Routing between different networks
  149. 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)"
  150. 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)"
  151. #
  152. # Test support for lowest address ending in .0
  153. segmenttest 5.10.15.20 5.10.15.0 24 "assign and ping lowest address (/24)"
  154. #
  155. # Test support for lowest address not ending in .0
  156. segmenttest 192.168.101.192 192.168.101.193 26 "assign and ping lowest address (/26)"
  157. #
  158. # Routing using lowest address as a gateway/endpoint
  159. route_test 192.168.42.1 192.168.42.0 9.8.7.6 9.8.7.0 24 "routing using lowest address"
  160. #
  161. # ==============================================
  162. # ==== TESTS THAT CURRENTLY EXPECT FAILURE =====
  163. # ==============================================
  164. expect_failure=true
  165. # It should still not be possible to use 0.0.0.0 or 255.255.255.255
  166. # as a unicast address. Thus, these tests expect failure.
  167. segmenttest 0.0.1.5 0.0.0.0 16 "assigning 0.0.0.0 (is forbidden)"
  168. segmenttest 255.255.255.1 255.255.255.255 16 "assigning 255.255.255.255 (is forbidden)"
  169. #
  170. # Test support for not having all of 127 be loopback
  171. # Currently Linux does not allow this, so this should fail too
  172. segmenttest 127.99.4.5 127.99.4.6 16 "assign and ping inside 127/8 (is forbidden)"
  173. #
  174. # Test support for unicast use of class D
  175. # Currently Linux does not allow this, so this should fail too
  176. segmenttest 225.1.2.3 225.1.2.200 24 "assign and ping class D address (is forbidden)"
  177. #
  178. # Routing using class D as a gateway
  179. 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)"
  180. #
  181. # Routing using 127/8
  182. # Currently Linux does not allow this, so this should fail too
  183. 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)"
  184. #
  185. unset expect_failure
  186. # =====================================================
  187. # ==== END OF TESTS THAT CURRENTLY EXPECT FAILURE =====
  188. # =====================================================
  189. exit ${result}