1f08c3bdfSopenharmony_ci#! /bin/sh
2f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
3f08c3bdfSopenharmony_ci# Copyright (c) 2014-2019 Oracle and/or its affiliates. All Rights Reserved.
4f08c3bdfSopenharmony_ci# Copyright (c) International Business Machines Corp., 2001
5f08c3bdfSopenharmony_ci# Author:       Manoj Iyer, manjo@mail.utexas.edu
6f08c3bdfSopenharmony_ci#
7f08c3bdfSopenharmony_ci# Description:  Test basic functionality of ip command in route2 package
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ciTST_CNT=6
10f08c3bdfSopenharmony_ciTST_SETUP="init"
11f08c3bdfSopenharmony_ciTST_TESTFUNC="test"
12f08c3bdfSopenharmony_ciTST_CLEANUP="cleanup"
13f08c3bdfSopenharmony_ciTST_NEEDS_TMPDIR=1
14f08c3bdfSopenharmony_ciTST_NEEDS_ROOT=1
15f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="cat awk diff"
16f08c3bdfSopenharmony_ciTST_NEEDS_DRIVERS="dummy"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cirm_dummy=
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ciinit()
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	tst_res TINFO "inititalizing tests"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	iface=ltp_dummy
26f08c3bdfSopenharmony_ci	lsmod | grep -q dummy || rm_dummy=1
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci	ROD ip link add $iface type dummy
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	ip4_addr=${IPV4_NET16_UNUSED}.6.6
31f08c3bdfSopenharmony_ci	ROD ip addr add ${ip4_addr}/24 dev $iface
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	cat > tst_ip02.exp <<-EOF
34f08c3bdfSopenharmony_ci	1:
35f08c3bdfSopenharmony_ci	link/loopback
36f08c3bdfSopenharmony_ci	2:
37f08c3bdfSopenharmony_ci	link/ether
38f08c3bdfSopenharmony_ci	3:
39f08c3bdfSopenharmony_ci	link/ether
40f08c3bdfSopenharmony_ci	EOF
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
43f08c3bdfSopenharmony_ci		tst_brk TBROK "can't create expected output for test02"
44f08c3bdfSopenharmony_ci	fi
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cicleanup()
48f08c3bdfSopenharmony_ci{
49f08c3bdfSopenharmony_ci	[ -n "$iface" -a -d /sys/class/net/$iface ] && ip link del $iface
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	[ "$rm_dummy" ] && modprobe -r dummy
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	# test #5
54f08c3bdfSopenharmony_ci	[ "$ip4_addr" ] && ip route show | grep -q $ip4_addr && ip route del $ip4_addr
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_citest1()
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	tst_res TINFO "test 'ip link set' command"
60f08c3bdfSopenharmony_ci	tst_res TINFO "changing mtu size of $iface device"
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	MTUSZ_BAK=$(cat /sys/class/net/${iface}/mtu)
63f08c3bdfSopenharmony_ci	ip link set ${iface} mtu 1281
64f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
65f08c3bdfSopenharmony_ci		tst_res TFAIL "ip command failed"
66f08c3bdfSopenharmony_ci		return
67f08c3bdfSopenharmony_ci	fi
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	MTUSZ=$(cat /sys/class/net/${iface}/mtu)
70f08c3bdfSopenharmony_ci	if [ $MTUSZ -eq 1281 ]; then
71f08c3bdfSopenharmony_ci		tst_res TPASS "successfully changed mtu size"
72f08c3bdfSopenharmony_ci		ip link set $iface mtu $MTUSZ_BAK
73f08c3bdfSopenharmony_ci	else
74f08c3bdfSopenharmony_ci		tst_res TFAIL "MTU value set to $MTUSZ, but expected 1281"
75f08c3bdfSopenharmony_ci	fi
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_citest2()
79f08c3bdfSopenharmony_ci{
80f08c3bdfSopenharmony_ci	tst_res TINFO "test 'ip link show' command (list device attributes)"
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	ip link show $iface | grep $iface > /dev/null
83f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
84f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip link show $iface' command failed"
85f08c3bdfSopenharmony_ci		return
86f08c3bdfSopenharmony_ci	fi
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	tst_res TPASS "$iface correctly listed"
89f08c3bdfSopenharmony_ci}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_citest3()
92f08c3bdfSopenharmony_ci{
93f08c3bdfSopenharmony_ci	tst_res TINFO "test 'ip addr' command with loopback dev"
94f08c3bdfSopenharmony_ci	tst_res TINFO "add a new protocol address to the device"
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	ip addr add 127.6.6.6/24 dev lo
97f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
98f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip addr add' command failed"
99f08c3bdfSopenharmony_ci		return
100f08c3bdfSopenharmony_ci	fi
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	tst_res TINFO "show protocol address"
103f08c3bdfSopenharmony_ci	ip addr show dev lo | grep 127.6.6.6 > /dev/null
104f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
105f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip addr show' command failed"
106f08c3bdfSopenharmony_ci		return
107f08c3bdfSopenharmony_ci	fi
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci	tst_res TINFO "delete protocol address"
110f08c3bdfSopenharmony_ci	ip addr del 127.6.6.6/24 dev lo
111f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
112f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip addr del' command failed"
113f08c3bdfSopenharmony_ci		return
114f08c3bdfSopenharmony_ci	fi
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	ip addr show dev lo | grep 127.6.6.6 > /dev/null
117f08c3bdfSopenharmony_ci	if [ $? -eq 0 ]; then
118f08c3bdfSopenharmony_ci		tst_res TFAIL "ip addr del command failed"
119f08c3bdfSopenharmony_ci		return
120f08c3bdfSopenharmony_ci	fi
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	tst_res TPASS "'ip addr' command successfully tested"
123f08c3bdfSopenharmony_ci}
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_citest4()
126f08c3bdfSopenharmony_ci{
127f08c3bdfSopenharmony_ci	local taddr="$(tst_ipaddr_un)"
128f08c3bdfSopenharmony_ci	local tdev="$(tst_iface)"
129f08c3bdfSopenharmony_ci	tst_res TINFO "test 'ip neigh' command"
130f08c3bdfSopenharmony_ci	tst_res TINFO "add a new neighbor (or replace existed)"
131f08c3bdfSopenharmony_ci	ip neigh replace $taddr lladdr 00:00:00:00:00:00 dev $tdev nud reachable
132f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
133f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip neigh replace' command failed"
134f08c3bdfSopenharmony_ci		return
135f08c3bdfSopenharmony_ci	fi
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	tst_res TINFO "show all neighbor entries in arp tables"
138f08c3bdfSopenharmony_ci	echo "$taddr dev $tdev lladdr 00:00:00:00:00:00 REACHABLE" > tst_ip.exp
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_ci	ip neigh show $taddr | head -n1 > tst_ip.out 2>&1
141f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
142f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip neigh show' command failed"
143f08c3bdfSopenharmony_ci		return
144f08c3bdfSopenharmony_ci	fi
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_ci	diff -iwB tst_ip.out tst_ip.exp
147f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
148f08c3bdfSopenharmony_ci		tst_res TFAIL "expected output differs from actual output"
149f08c3bdfSopenharmony_ci		return
150f08c3bdfSopenharmony_ci	fi
151f08c3bdfSopenharmony_ci
152f08c3bdfSopenharmony_ci	tst_res TINFO "delete neighbor from the arp table"
153f08c3bdfSopenharmony_ci
154f08c3bdfSopenharmony_ci	ip neigh del $taddr dev $tdev
155f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
156f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip neigh del' command failed"
157f08c3bdfSopenharmony_ci		return
158f08c3bdfSopenharmony_ci	fi
159f08c3bdfSopenharmony_ci
160f08c3bdfSopenharmony_ci	ip neigh show | grep $taddr | grep -v ' FAILED$' > /dev/null
161f08c3bdfSopenharmony_ci	if [ $? -eq 0 ]; then
162f08c3bdfSopenharmony_ci		tst_res TFAIL "$taddr still listed in arp"
163f08c3bdfSopenharmony_ci		return
164f08c3bdfSopenharmony_ci	fi
165f08c3bdfSopenharmony_ci
166f08c3bdfSopenharmony_ci	tst_res TPASS "'ip neigh' command successfully tested"
167f08c3bdfSopenharmony_ci}
168f08c3bdfSopenharmony_ci
169f08c3bdfSopenharmony_citest5()
170f08c3bdfSopenharmony_ci{
171f08c3bdfSopenharmony_ci	tst_res TINFO "test 'ip route add/del' commands"
172f08c3bdfSopenharmony_ci
173f08c3bdfSopenharmony_ci	ROD ip route add $ip4_addr via 127.0.0.1
174f08c3bdfSopenharmony_ci
175f08c3bdfSopenharmony_ci	tst_res TINFO "show all route entries in route table"
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_ci	# create expected output file.
178f08c3bdfSopenharmony_ci	cat > tst_ip.exp <<-EOF
179f08c3bdfSopenharmony_ci$ip4_addr via 127.0.0.1 dev lo
180f08c3bdfSopenharmony_ci	EOF
181f08c3bdfSopenharmony_ci
182f08c3bdfSopenharmony_ci	ip route show | grep "$ip4_addr via 127.0.0.1 dev lo" > tst_ip.out 2>&1
183f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
184f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip route show' command failed"
185f08c3bdfSopenharmony_ci		return
186f08c3bdfSopenharmony_ci	fi
187f08c3bdfSopenharmony_ci
188f08c3bdfSopenharmony_ci	diff -iwB tst_ip.out tst_ip.exp
189f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
190f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip route show' did not list new route"
191f08c3bdfSopenharmony_ci		return
192f08c3bdfSopenharmony_ci	fi
193f08c3bdfSopenharmony_ci
194f08c3bdfSopenharmony_ci	tst_res TINFO "delete route from the route table"
195f08c3bdfSopenharmony_ci
196f08c3bdfSopenharmony_ci	ROD ip route del $ip4_addr via 127.0.0.1
197f08c3bdfSopenharmony_ci
198f08c3bdfSopenharmony_ci	ip route show | grep 127.0.0.1 > /dev/null
199f08c3bdfSopenharmony_ci	if [ $? -eq 0 ]; then
200f08c3bdfSopenharmony_ci		tst_res TFAIL "route not deleted"
201f08c3bdfSopenharmony_ci		return
202f08c3bdfSopenharmony_ci	fi
203f08c3bdfSopenharmony_ci
204f08c3bdfSopenharmony_ci	tst_res TPASS "'ip route' command successfully tested"
205f08c3bdfSopenharmony_ci}
206f08c3bdfSopenharmony_ci
207f08c3bdfSopenharmony_citest6()
208f08c3bdfSopenharmony_ci{
209f08c3bdfSopenharmony_ci	tst_res TINFO "test 'ip maddr add/del' commands"
210f08c3bdfSopenharmony_ci	tst_res TINFO "adding a new multicast addr"
211f08c3bdfSopenharmony_ci
212f08c3bdfSopenharmony_ci	ip maddr add 66:66:00:00:00:66 dev $iface
213f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
214f08c3bdfSopenharmony_ci		tst_res TFAIL "ip maddr add command failed"
215f08c3bdfSopenharmony_ci		return
216f08c3bdfSopenharmony_ci	fi
217f08c3bdfSopenharmony_ci
218f08c3bdfSopenharmony_ci	tst_res TINFO "show all multicast addr entries"
219f08c3bdfSopenharmony_ci
220f08c3bdfSopenharmony_ci	cat > tst_ip.exp <<-EOF
221f08c3bdfSopenharmony_ci        link  66:66:00:00:00:66 static
222f08c3bdfSopenharmony_ci	EOF
223f08c3bdfSopenharmony_ci
224f08c3bdfSopenharmony_ci	ip maddr show | grep "66:66:00:00:00:66" > tst_ip.out 2>&1
225f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
226f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip maddr show' command failed"
227f08c3bdfSopenharmony_ci		return
228f08c3bdfSopenharmony_ci	fi
229f08c3bdfSopenharmony_ci
230f08c3bdfSopenharmony_ci	diff -iwB tst_ip.out tst_ip.exp
231f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
232f08c3bdfSopenharmony_ci		tst_res TFAIL "multicast addr not added to $iface"
233f08c3bdfSopenharmony_ci		return
234f08c3bdfSopenharmony_ci	fi
235f08c3bdfSopenharmony_ci
236f08c3bdfSopenharmony_ci	tst_res TINFO "delete multicast address"
237f08c3bdfSopenharmony_ci
238f08c3bdfSopenharmony_ci	ip maddr del 66:66:00:00:00:66 dev $iface
239f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
240f08c3bdfSopenharmony_ci		tst_res TFAIL "'ip maddr del' command failed"
241f08c3bdfSopenharmony_ci		return
242f08c3bdfSopenharmony_ci	fi
243f08c3bdfSopenharmony_ci
244f08c3bdfSopenharmony_ci	ip maddr show | grep "66:66:00:00:00:66" > /dev/null
245f08c3bdfSopenharmony_ci	if [ $? -eq 0 ]; then
246f08c3bdfSopenharmony_ci		tst_res TFAIL "66:66:00:00:00:66 is not deleted"
247f08c3bdfSopenharmony_ci		return
248f08c3bdfSopenharmony_ci	fi
249f08c3bdfSopenharmony_ci
250f08c3bdfSopenharmony_ci	tst_res TPASS "'ip maddr' command successfully tested"
251f08c3bdfSopenharmony_ci}
252f08c3bdfSopenharmony_ci
253f08c3bdfSopenharmony_ci. tst_net.sh
254f08c3bdfSopenharmony_citst_run
255