Merge branch 'asoc-4.18' into asoc-4.19 for amd dep
[linux-2.6-microblaze.git] / tools / testing / selftests / net / fib_rule_tests.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3
4 # This test is for checking IPv4 and IPv6 FIB rules API
5
6 ret=0
7
8 PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}
9 IP="ip -netns testns"
10
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
16
17 DEV_ADDR=192.51.100.1
18 DEV=dummy0
19
20 log_test()
21 {
22         local rc=$1
23         local expected=$2
24         local msg="$3"
25
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
37                 fi
38         fi
39 }
40
41 log_section()
42 {
43         echo
44         echo "######################################################################"
45         echo "TEST SECTION: $*"
46         echo "######################################################################"
47 }
48
49 setup()
50 {
51         set -e
52         ip netns add testns
53         $IP link set dev lo up
54
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
59
60         set +e
61 }
62
63 cleanup()
64 {
65         $IP link del dev dummy0 &> /dev/null
66         ip netns del testns
67 }
68
69 fib_check_iproute_support()
70 {
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
75         fi
76
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
81         fi
82
83         return 0
84 }
85
86 fib_rule6_del()
87 {
88         $IP -6 rule del $1
89         log_test $? 0 "rule6 del $1"
90 }
91
92 fib_rule6_del_by_pref()
93 {
94         pref=$($IP -6 rule show | grep "$1 lookup $TABLE" | cut -d ":" -f 1)
95         $IP -6 rule del pref $pref
96 }
97
98 fib_rule6_test_match_n_redirect()
99 {
100         local match="$1"
101         local getmatch="$2"
102
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"
106
107         fib_rule6_del_by_pref "$match"
108         log_test $? 0 "rule6 del by pref: $match"
109 }
110
111 fib_rule6_test()
112 {
113         # setup the fib rule redirect route
114         $IP -6 route add table $RTABLE default via $GW_IP6 dev $DEV onlink
115
116         match="oif $DEV"
117         fib_rule6_test_match_n_redirect "$match" "$match" "oif redirect to table"
118
119         match="from $SRC_IP6 iif $DEV"
120         fib_rule6_test_match_n_redirect "$match" "$match" "iif redirect to table"
121
122         match="tos 0x10"
123         fib_rule6_test_match_n_redirect "$match" "$match" "tos redirect to table"
124
125         match="fwmark 0x64"
126         getmatch="mark 0x64"
127         fib_rule6_test_match_n_redirect "$match" "$getmatch" "fwmark redirect to table"
128
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"
134         fi
135
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"
140         fi
141
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"
146         fi
147
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"
152         fi
153 }
154
155 fib_rule4_del()
156 {
157         $IP rule del $1
158         log_test $? 0 "del $1"
159 }
160
161 fib_rule4_del_by_pref()
162 {
163         pref=$($IP rule show | grep "$1 lookup $TABLE" | cut -d ":" -f 1)
164         $IP rule del pref $pref
165 }
166
167 fib_rule4_test_match_n_redirect()
168 {
169         local match="$1"
170         local getmatch="$2"
171
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"
175
176         fib_rule4_del_by_pref "$match"
177         log_test $? 0 "rule4 del by pref: $match"
178 }
179
180 fib_rule4_test()
181 {
182         # setup the fib rule redirect route
183         $IP route add table $RTABLE default via $GW_IP4 dev $DEV onlink
184
185         match="oif $DEV"
186         fib_rule4_test_match_n_redirect "$match" "$match" "oif redirect to table"
187
188         match="from $SRC_IP iif $DEV"
189         fib_rule4_test_match_n_redirect "$match" "$match" "iif redirect to table"
190
191         match="tos 0x10"
192         fib_rule4_test_match_n_redirect "$match" "$match" "tos redirect to table"
193
194         match="fwmark 0x64"
195         getmatch="mark 0x64"
196         fib_rule4_test_match_n_redirect "$match" "$getmatch" "fwmark redirect to table"
197
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"
203         fi
204
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"
209         fi
210
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"
215         fi
216
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"
221         fi
222 }
223
224 run_fibrule_tests()
225 {
226         log_section "IPv4 fib rule"
227         fib_rule4_test
228         log_section "IPv6 fib rule"
229         fib_rule6_test
230 }
231
232 if [ "$(id -u)" -ne 0 ];then
233         echo "SKIP: Need root privileges"
234         exit 0
235 fi
236
237 if [ ! -x "$(command -v ip)" ]; then
238         echo "SKIP: Could not run test without ip tool"
239         exit 0
240 fi
241
242 # start clean
243 cleanup &> /dev/null
244 setup
245 run_fibrule_tests
246 cleanup
247
248 exit $ret