Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # By Seth Schoen (c) 2021, for the IPv4 Unicast Extensions Project
0005 # Thanks to David Ahern for help and advice on nettest modifications.
0006 #
0007 # Self-tests for IPv4 address extensions: the kernel's ability to accept
0008 # certain traditionally unused or unallocated IPv4 addresses. For each kind
0009 # of address, we test for interface assignment, ping, TCP, and forwarding.
0010 # Must be run as root (to manipulate network namespaces and virtual
0011 # interfaces).
0012 #
0013 # Things we test for here:
0014 #
0015 # * Currently the kernel accepts addresses in 0/8 and 240/4 as valid.
0016 #
0017 # * Notwithstanding that, 0.0.0.0 and 255.255.255.255 cannot be assigned.
0018 #
0019 # * Currently the kernel DOES NOT accept unicast use of the lowest
0020 #   address in an IPv4 subnet (e.g. 192.168.100.0/32 in 192.168.100.0/24).
0021 #   This is treated as a second broadcast address, for compatibility
0022 #   with 4.2BSD (!).
0023 #
0024 # * Currently the kernel DOES NOT accept unicast use of any of 127/8.
0025 #
0026 # * Currently the kernel DOES NOT accept unicast use of any of 224/4.
0027 #
0028 # These tests provide an easy way to flip the expected result of any
0029 # of these behaviors for testing kernel patches that change them.
0030 
0031 # Kselftest framework requirement - SKIP code is 4.
0032 ksft_skip=4
0033 
0034 # nettest can be run from PATH or from same directory as this selftest
0035 if ! which nettest >/dev/null; then
0036         PATH=$PWD:$PATH
0037         if ! which nettest >/dev/null; then
0038                 echo "'nettest' command not found; skipping tests"
0039                 exit $ksft_skip
0040         fi
0041 fi
0042 
0043 result=0
0044 
0045 hide_output(){ exec 3>&1 4>&2 >/dev/null 2>/dev/null; }
0046 show_output(){ exec >&3 2>&4; }
0047 
0048 show_result(){
0049         if [ $1 -eq 0 ]; then
0050                 printf "TEST: %-60s  [ OK ]\n" "${2}"
0051         else
0052                 printf "TEST: %-60s  [FAIL]\n" "${2}"
0053                 result=1
0054         fi
0055 }
0056 
0057 _do_segmenttest(){
0058         # Perform a simple set of link tests between a pair of
0059         # IP addresses on a shared (virtual) segment, using
0060         # ping and nettest.
0061         # foo --- bar
0062         # Arguments: ip_a ip_b prefix_length test_description
0063         #
0064         # Caller must set up foo-ns and bar-ns namespaces
0065         # containing linked veth devices foo and bar,
0066         # respectively.
0067 
0068         ip -n foo-ns address add $1/$3 dev foo || return 1
0069         ip -n foo-ns link set foo up || return 1
0070         ip -n bar-ns address add $2/$3 dev bar || return 1
0071         ip -n bar-ns link set bar up || return 1
0072 
0073         ip netns exec foo-ns timeout 2 ping -c 1 $2 || return 1
0074         ip netns exec bar-ns timeout 2 ping -c 1 $1 || return 1
0075 
0076         nettest -B -N bar-ns -O foo-ns -r $1 || return 1
0077         nettest -B -N foo-ns -O bar-ns -r $2 || return 1
0078 
0079         return 0
0080 }
0081 
0082 _do_route_test(){
0083         # Perform a simple set of gateway tests.
0084         #
0085         # [foo] <---> [foo1]-[bar1] <---> [bar]   /prefix
0086         #  host          gateway          host
0087         #
0088         # Arguments: foo_ip foo1_ip bar1_ip bar_ip prefix_len test_description
0089         # Displays test result and returns success or failure.
0090 
0091         # Caller must set up foo-ns, bar-ns, and router-ns
0092         # containing linked veth devices foo-foo1, bar1-bar
0093         # (foo in foo-ns, foo1 and bar1 in router-ns, and
0094         # bar in bar-ns).
0095 
0096         ip -n foo-ns address add $1/$5 dev foo || return 1
0097         ip -n foo-ns link set foo up || return 1
0098         ip -n foo-ns route add default via $2 || return 1
0099         ip -n bar-ns address add $4/$5 dev bar || return 1
0100         ip -n bar-ns link set bar up || return 1
0101         ip -n bar-ns route add default via $3 || return 1
0102         ip -n router-ns address add $2/$5 dev foo1 || return 1
0103         ip -n router-ns link set foo1 up || return 1
0104         ip -n router-ns address add $3/$5 dev bar1 || return 1
0105         ip -n router-ns link set bar1 up || return 1
0106 
0107         echo 1 | ip netns exec router-ns tee /proc/sys/net/ipv4/ip_forward
0108 
0109         ip netns exec foo-ns timeout 2 ping -c 1 $2 || return 1
0110         ip netns exec foo-ns timeout 2 ping -c 1 $4 || return 1
0111         ip netns exec bar-ns timeout 2 ping -c 1 $3 || return 1
0112         ip netns exec bar-ns timeout 2 ping -c 1 $1 || return 1
0113 
0114         nettest -B -N bar-ns -O foo-ns -r $1 || return 1
0115         nettest -B -N foo-ns -O bar-ns -r $4 || return 1
0116 
0117         return 0
0118 }
0119 
0120 segmenttest(){
0121         # Sets up veth link and tries to connect over it.
0122         # Arguments: ip_a ip_b prefix_len test_description
0123         hide_output
0124         ip netns add foo-ns
0125         ip netns add bar-ns
0126         ip link add foo netns foo-ns type veth peer name bar netns bar-ns
0127 
0128         test_result=0
0129         _do_segmenttest "$@" || test_result=1
0130 
0131         ip netns pids foo-ns | xargs -r kill -9
0132         ip netns pids bar-ns | xargs -r kill -9
0133         ip netns del foo-ns
0134         ip netns del bar-ns
0135         show_output
0136 
0137         # inverted tests will expect failure instead of success
0138         [ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
0139 
0140         show_result $test_result "$4"
0141 }
0142 
0143 route_test(){
0144         # Sets up a simple gateway and tries to connect through it.
0145         # [foo] <---> [foo1]-[bar1] <---> [bar]   /prefix
0146         # Arguments: foo_ip foo1_ip bar1_ip bar_ip prefix_len test_description
0147         # Returns success or failure.
0148 
0149         hide_output
0150         ip netns add foo-ns
0151         ip netns add bar-ns
0152         ip netns add router-ns
0153         ip link add foo netns foo-ns type veth peer name foo1 netns router-ns
0154         ip link add bar netns bar-ns type veth peer name bar1 netns router-ns
0155 
0156         test_result=0
0157         _do_route_test "$@" || test_result=1
0158 
0159         ip netns pids foo-ns | xargs -r kill -9
0160         ip netns pids bar-ns | xargs -r kill -9
0161         ip netns pids router-ns | xargs -r kill -9
0162         ip netns del foo-ns
0163         ip netns del bar-ns
0164         ip netns del router-ns
0165 
0166         show_output
0167 
0168         # inverted tests will expect failure instead of success
0169         [ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
0170         show_result $test_result "$6"
0171 }
0172 
0173 echo "###########################################################################"
0174 echo "Unicast address extensions tests (behavior of reserved IPv4 addresses)"
0175 echo "###########################################################################"
0176 #
0177 # Test support for 240/4
0178 segmenttest 240.1.2.1   240.1.2.4    24 "assign and ping within 240/4 (1 of 2) (is allowed)"
0179 segmenttest 250.100.2.1 250.100.30.4 16 "assign and ping within 240/4 (2 of 2) (is allowed)"
0180 #
0181 # Test support for 0/8
0182 segmenttest 0.1.2.17    0.1.2.23  24 "assign and ping within 0/8 (1 of 2) (is allowed)"
0183 segmenttest 0.77.240.17 0.77.2.23 16 "assign and ping within 0/8 (2 of 2) (is allowed)"
0184 #
0185 # Even 255.255/16 is OK!
0186 segmenttest 255.255.3.1 255.255.50.77 16 "assign and ping inside 255.255/16 (is allowed)"
0187 #
0188 # Or 255.255.255/24
0189 segmenttest 255.255.255.1 255.255.255.254 24 "assign and ping inside 255.255.255/24 (is allowed)"
0190 #
0191 # Routing between different networks
0192 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)"
0193 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)"
0194 #
0195 # Test support for lowest address ending in .0
0196 segmenttest 5.10.15.20 5.10.15.0 24 "assign and ping lowest address (/24)"
0197 #
0198 # Test support for lowest address not ending in .0
0199 segmenttest 192.168.101.192 192.168.101.193 26 "assign and ping lowest address (/26)"
0200 #
0201 # Routing using lowest address as a gateway/endpoint
0202 route_test 192.168.42.1 192.168.42.0 9.8.7.6 9.8.7.0 24 "routing using lowest address"
0203 #
0204 # ==============================================
0205 # ==== TESTS THAT CURRENTLY EXPECT FAILURE =====
0206 # ==============================================
0207 expect_failure=true
0208 # It should still not be possible to use 0.0.0.0 or 255.255.255.255
0209 # as a unicast address.  Thus, these tests expect failure.
0210 segmenttest 0.0.1.5       0.0.0.0         16 "assigning 0.0.0.0 (is forbidden)"
0211 segmenttest 255.255.255.1 255.255.255.255 16 "assigning 255.255.255.255 (is forbidden)"
0212 #
0213 # Test support for not having all of 127 be loopback
0214 # Currently Linux does not allow this, so this should fail too
0215 segmenttest 127.99.4.5 127.99.4.6 16 "assign and ping inside 127/8 (is forbidden)"
0216 #
0217 # Test support for unicast use of class D
0218 # Currently Linux does not allow this, so this should fail too
0219 segmenttest 225.1.2.3 225.1.2.200 24 "assign and ping class D address (is forbidden)"
0220 #
0221 # Routing using class D as a gateway
0222 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)"
0223 #
0224 # Routing using 127/8
0225 # Currently Linux does not allow this, so this should fail too
0226 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)"
0227 #
0228 unset expect_failure
0229 # =====================================================
0230 # ==== END OF TESTS THAT CURRENTLY EXPECT FAILURE =====
0231 # =====================================================
0232 exit ${result}