proc: test /proc/thread-self symlink
[linux/fpc-iii.git] / tools / testing / selftests / net / fib_rule_tests.sh
blobd4cfb6a7a086d57eda4e0854b8e8ea1fe38d4527
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # This test is for checking IPv4 and IPv6 FIB rules API
6 ret=0
8 PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}
9 IP="ip -netns testns"
11 RTABLE=100
12 GW_IP4=192.51.100.2
13 SRC_IP=192.51.100.3
14 GW_IP6=2001:db8:1::2
15 SRC_IP6=2001:db8:1::3
17 DEV_ADDR=192.51.100.1
18 DEV=dummy0
20 log_test()
22 local rc=$1
23 local expected=$2
24 local msg="$3"
26 if [ ${rc} -eq ${expected} ]; then
27 nsuccess=$((nsuccess+1))
28 printf "\n TEST: %-50s [ OK ]\n" "${msg}"
29 else
30 nfail=$((nfail+1))
31 printf "\n TEST: %-50s [FAIL]\n" "${msg}"
32 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
33 echo
34 echo "hit enter to continue, 'q' to quit"
35 read a
36 [ "$a" = "q" ] && exit 1
41 log_section()
43 echo
44 echo "######################################################################"
45 echo "TEST SECTION: $*"
46 echo "######################################################################"
49 setup()
51 set -e
52 ip netns add testns
53 $IP link set dev lo up
55 $IP link add dummy0 type dummy
56 $IP link set dev dummy0 up
57 $IP address add 198.51.100.1/24 dev dummy0
58 $IP -6 address add 2001:db8:1::1/64 dev dummy0
60 set +e
63 cleanup()
65 $IP link del dev dummy0 &> /dev/null
66 ip netns del testns
69 fib_check_iproute_support()
71 ip rule help 2>&1 | grep -q $1
72 if [ $? -ne 0 ]; then
73 echo "SKIP: iproute2 iprule too old, missing $1 match"
74 return 1
77 ip route get help 2>&1 | grep -q $2
78 if [ $? -ne 0 ]; then
79 echo "SKIP: iproute2 get route too old, missing $2 match"
80 return 1
83 return 0
86 fib_rule6_del()
88 $IP -6 rule del $1
89 log_test $? 0 "rule6 del $1"
92 fib_rule6_del_by_pref()
94 pref=$($IP -6 rule show | grep "$1 lookup $TABLE" | cut -d ":" -f 1)
95 $IP -6 rule del pref $pref
98 fib_rule6_test_match_n_redirect()
100 local match="$1"
101 local getmatch="$2"
103 $IP -6 rule add $match table $RTABLE
104 $IP -6 route get $GW_IP6 $getmatch | grep -q "table $RTABLE"
105 log_test $? 0 "rule6 check: $1"
107 fib_rule6_del_by_pref "$match"
108 log_test $? 0 "rule6 del by pref: $match"
111 fib_rule6_test()
113 # setup the fib rule redirect route
114 $IP -6 route add table $RTABLE default via $GW_IP6 dev $DEV onlink
116 match="oif $DEV"
117 fib_rule6_test_match_n_redirect "$match" "$match" "oif redirect to table"
119 match="from $SRC_IP6 iif $DEV"
120 fib_rule6_test_match_n_redirect "$match" "$match" "iif redirect to table"
122 match="tos 0x10"
123 fib_rule6_test_match_n_redirect "$match" "$match" "tos redirect to table"
125 match="fwmark 0x64"
126 getmatch="mark 0x64"
127 fib_rule6_test_match_n_redirect "$match" "$getmatch" "fwmark redirect to table"
129 fib_check_iproute_support "uidrange" "uid"
130 if [ $? -eq 0 ]; then
131 match="uidrange 100-100"
132 getmatch="uid 100"
133 fib_rule6_test_match_n_redirect "$match" "$getmatch" "uid redirect to table"
136 fib_check_iproute_support "sport" "sport"
137 if [ $? -eq 0 ]; then
138 match="sport 666 dport 777"
139 fib_rule6_test_match_n_redirect "$match" "$match" "sport and dport redirect to table"
142 fib_check_iproute_support "ipproto" "ipproto"
143 if [ $? -eq 0 ]; then
144 match="ipproto tcp"
145 fib_rule6_test_match_n_redirect "$match" "$match" "ipproto match"
148 fib_check_iproute_support "ipproto" "ipproto"
149 if [ $? -eq 0 ]; then
150 match="ipproto icmp"
151 fib_rule6_test_match_n_redirect "$match" "$match" "ipproto icmp match"
155 fib_rule4_del()
157 $IP rule del $1
158 log_test $? 0 "del $1"
161 fib_rule4_del_by_pref()
163 pref=$($IP rule show | grep "$1 lookup $TABLE" | cut -d ":" -f 1)
164 $IP rule del pref $pref
167 fib_rule4_test_match_n_redirect()
169 local match="$1"
170 local getmatch="$2"
172 $IP rule add $match table $RTABLE
173 $IP route get $GW_IP4 $getmatch | grep -q "table $RTABLE"
174 log_test $? 0 "rule4 check: $1"
176 fib_rule4_del_by_pref "$match"
177 log_test $? 0 "rule4 del by pref: $match"
180 fib_rule4_test()
182 # setup the fib rule redirect route
183 $IP route add table $RTABLE default via $GW_IP4 dev $DEV onlink
185 match="oif $DEV"
186 fib_rule4_test_match_n_redirect "$match" "$match" "oif redirect to table"
188 match="from $SRC_IP iif $DEV"
189 fib_rule4_test_match_n_redirect "$match" "$match" "iif redirect to table"
191 match="tos 0x10"
192 fib_rule4_test_match_n_redirect "$match" "$match" "tos redirect to table"
194 match="fwmark 0x64"
195 getmatch="mark 0x64"
196 fib_rule4_test_match_n_redirect "$match" "$getmatch" "fwmark redirect to table"
198 fib_check_iproute_support "uidrange" "uid"
199 if [ $? -eq 0 ]; then
200 match="uidrange 100-100"
201 getmatch="uid 100"
202 fib_rule4_test_match_n_redirect "$match" "$getmatch" "uid redirect to table"
205 fib_check_iproute_support "sport" "sport"
206 if [ $? -eq 0 ]; then
207 match="sport 666 dport 777"
208 fib_rule4_test_match_n_redirect "$match" "$match" "sport and dport redirect to table"
211 fib_check_iproute_support "ipproto" "ipproto"
212 if [ $? -eq 0 ]; then
213 match="ipproto tcp"
214 fib_rule4_test_match_n_redirect "$match" "$match" "ipproto tcp match"
217 fib_check_iproute_support "ipproto" "ipproto"
218 if [ $? -eq 0 ]; then
219 match="ipproto icmp"
220 fib_rule4_test_match_n_redirect "$match" "$match" "ipproto icmp match"
224 run_fibrule_tests()
226 log_section "IPv4 fib rule"
227 fib_rule4_test
228 log_section "IPv6 fib rule"
229 fib_rule6_test
232 if [ "$(id -u)" -ne 0 ];then
233 echo "SKIP: Need root privileges"
234 exit 0
237 if [ ! -x "$(command -v ip)" ]; then
238 echo "SKIP: Could not run test without ip tool"
239 exit 0
242 # start clean
243 cleanup &> /dev/null
244 setup
245 run_fibrule_tests
246 cleanup
248 exit $ret