0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 ksft_skip=4
0033
0034
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
0059
0060
0061
0062
0063
0064
0065
0066
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
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
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
0122
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
0138 [ -n "$expect_failure" ] && test_result=`expr 1 - $test_result`
0139
0140 show_result $test_result "$4"
0141 }
0142
0143 route_test(){
0144
0145
0146
0147
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
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
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
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
0186 segmenttest 255.255.3.1 255.255.50.77 16 "assign and ping inside 255.255/16 (is allowed)"
0187
0188
0189 segmenttest 255.255.255.1 255.255.255.254 24 "assign and ping inside 255.255.255/24 (is allowed)"
0190
0191
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
0196 segmenttest 5.10.15.20 5.10.15.0 24 "assign and ping lowest address (/24)"
0197
0198
0199 segmenttest 192.168.101.192 192.168.101.193 26 "assign and ping lowest address (/26)"
0200
0201
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
0206
0207 expect_failure=true
0208
0209
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
0214
0215 segmenttest 127.99.4.5 127.99.4.6 16 "assign and ping inside 127/8 (is forbidden)"
0216
0217
0218
0219 segmenttest 225.1.2.3 225.1.2.200 24 "assign and ping class D address (is forbidden)"
0220
0221
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
0225
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
0231
0232 exit ${result}