xref: /openbmc/linux/tools/testing/selftests/net/rtnetlink.sh (revision b240b419db5d624ce7a5a397d6f62a1a686009ec)
1#!/bin/sh
2#
3# This test is for checking rtnetlink callpaths, and get as much coverage as possible.
4#
5# set -e
6
7devdummy="test-dummy0"
8ret=0
9
10# set global exit status, but never reset nonzero one.
11check_err()
12{
13	if [ $ret -eq 0 ]; then
14		ret=$1
15	fi
16}
17
18# same but inverted -- used when command must fail for test to pass
19check_fail()
20{
21	if [ $1 -eq 0 ]; then
22		ret=1
23	fi
24}
25
26kci_add_dummy()
27{
28	ip link add name "$devdummy" type dummy
29	check_err $?
30	ip link set "$devdummy" up
31	check_err $?
32}
33
34kci_del_dummy()
35{
36	ip link del dev "$devdummy"
37	check_err $?
38}
39
40kci_test_netconf()
41{
42	dev="$1"
43	r=$ret
44
45	ip netconf show dev "$dev" > /dev/null
46	check_err $?
47
48	for f in 4 6; do
49		ip -$f netconf show dev "$dev" > /dev/null
50		check_err $?
51	done
52
53	if [ $ret -ne 0 ] ;then
54		echo "FAIL: ip netconf show $dev"
55		test $r -eq 0 && ret=0
56		return 1
57	fi
58}
59
60# add a bridge with vlans on top
61kci_test_bridge()
62{
63	devbr="test-br0"
64	vlandev="testbr-vlan1"
65
66	ret=0
67	ip link add name "$devbr" type bridge
68	check_err $?
69
70	ip link set dev "$devdummy" master "$devbr"
71	check_err $?
72
73	ip link set "$devbr" up
74	check_err $?
75
76	ip link add link "$devbr" name "$vlandev" type vlan id 1
77	check_err $?
78	ip addr add dev "$vlandev" 10.200.7.23/30
79	check_err $?
80	ip -6 addr add dev "$vlandev" dead:42::1234/64
81	check_err $?
82	ip -d link > /dev/null
83	check_err $?
84	ip r s t all > /dev/null
85	check_err $?
86
87	for name in "$devbr" "$vlandev" "$devdummy" ; do
88		kci_test_netconf "$name"
89	done
90
91	ip -6 addr del dev "$vlandev" dead:42::1234/64
92	check_err $?
93
94	ip link del dev "$vlandev"
95	check_err $?
96	ip link del dev "$devbr"
97	check_err $?
98
99	if [ $ret -ne 0 ];then
100		echo "FAIL: bridge setup"
101		return 1
102	fi
103	echo "PASS: bridge setup"
104
105}
106
107kci_test_gre()
108{
109	gredev=neta
110	rem=10.42.42.1
111	loc=10.0.0.1
112
113	ret=0
114	ip tunnel add $gredev mode gre remote $rem local $loc ttl 1
115	check_err $?
116	ip link set $gredev up
117	check_err $?
118	ip addr add 10.23.7.10 dev $gredev
119	check_err $?
120	ip route add 10.23.8.0/30 dev $gredev
121	check_err $?
122	ip addr add dev "$devdummy" 10.23.7.11/24
123	check_err $?
124	ip link > /dev/null
125	check_err $?
126	ip addr > /dev/null
127	check_err $?
128
129	kci_test_netconf "$gredev"
130
131	ip addr del dev "$devdummy" 10.23.7.11/24
132	check_err $?
133
134	ip link del $gredev
135	check_err $?
136
137	if [ $ret -ne 0 ];then
138		echo "FAIL: gre tunnel endpoint"
139		return 1
140	fi
141	echo "PASS: gre tunnel endpoint"
142}
143
144# tc uses rtnetlink too, for full tc testing
145# please see tools/testing/selftests/tc-testing.
146kci_test_tc()
147{
148	dev=lo
149	ret=0
150
151	tc qdisc add dev "$dev" root handle 1: htb
152	check_err $?
153	tc class add dev "$dev" parent 1: classid 1:10 htb rate 1mbit
154	check_err $?
155	tc filter add dev "$dev" parent 1:0 prio 5 handle ffe: protocol ip u32 divisor 256
156	check_err $?
157	tc filter add dev "$dev" parent 1:0 prio 5 handle ffd: protocol ip u32 divisor 256
158	check_err $?
159	tc filter add dev "$dev" parent 1:0 prio 5 handle ffc: protocol ip u32 divisor 256
160	check_err $?
161	tc filter add dev "$dev" protocol ip parent 1: prio 5 handle ffe:2:3 u32 ht ffe:2: match ip src 10.0.0.3 flowid 1:10
162	check_err $?
163	tc filter add dev "$dev" protocol ip parent 1: prio 5 handle ffe:2:2 u32 ht ffe:2: match ip src 10.0.0.2 flowid 1:10
164	check_err $?
165	tc filter show dev "$dev" parent  1:0 > /dev/null
166	check_err $?
167	tc filter del dev "$dev" protocol ip parent 1: prio 5 handle ffe:2:3 u32
168	check_err $?
169	tc filter show dev "$dev" parent  1:0 > /dev/null
170	check_err $?
171	tc qdisc del dev "$dev" root handle 1: htb
172	check_err $?
173
174	if [ $ret -ne 0 ];then
175		echo "FAIL: tc htb hierarchy"
176		return 1
177	fi
178	echo "PASS: tc htb hierarchy"
179
180}
181
182kci_test_polrouting()
183{
184	ret=0
185	ip rule add fwmark 1 lookup 100
186	check_err $?
187	ip route add local 0.0.0.0/0 dev lo table 100
188	check_err $?
189	ip r s t all > /dev/null
190	check_err $?
191	ip rule del fwmark 1 lookup 100
192	check_err $?
193	ip route del local 0.0.0.0/0 dev lo table 100
194	check_err $?
195
196	if [ $ret -ne 0 ];then
197		echo "FAIL: policy route test"
198		return 1
199	fi
200	echo "PASS: policy routing"
201}
202
203kci_test_route_get()
204{
205	ret=0
206
207	ip route get 127.0.0.1 > /dev/null
208	check_err $?
209	ip route get 127.0.0.1 dev "$devdummy" > /dev/null
210	check_err $?
211	ip route get ::1 > /dev/null
212	check_err $?
213	ip route get fe80::1 dev "$devdummy" > /dev/null
214	check_err $?
215	ip route get 127.0.0.1 from 127.0.0.1 oif lo tos 0x1 mark 0x1 > /dev/null
216	check_err $?
217	ip route get ::1 from ::1 iif lo oif lo tos 0x1 mark 0x1 > /dev/null
218	check_err $?
219	ip addr add dev "$devdummy" 10.23.7.11/24
220	check_err $?
221	ip route get 10.23.7.11 from 10.23.7.12 iif "$devdummy" > /dev/null
222	check_err $?
223	ip addr del dev "$devdummy" 10.23.7.11/24
224	check_err $?
225
226	if [ $ret -ne 0 ];then
227		echo "FAIL: route get"
228		return 1
229	fi
230
231	echo "PASS: route get"
232}
233
234kci_test_addrlabel()
235{
236	ret=0
237
238	ip addrlabel add prefix dead::/64 dev lo label 1
239	check_err $?
240
241	ip addrlabel list |grep -q "prefix dead::/64 dev lo label 1"
242	check_err $?
243
244	ip addrlabel del prefix dead::/64 dev lo label 1 2> /dev/null
245	check_err $?
246
247	ip addrlabel add prefix dead::/64 label 1 2> /dev/null
248	check_err $?
249
250	ip addrlabel del prefix dead::/64 label 1 2> /dev/null
251	check_err $?
252
253	# concurrent add/delete
254	for i in $(seq 1 1000); do
255		ip addrlabel add prefix 1c3::/64 label 12345 2>/dev/null
256	done &
257
258	for i in $(seq 1 1000); do
259		ip addrlabel del prefix 1c3::/64 label 12345 2>/dev/null
260	done
261
262	wait
263
264	ip addrlabel del prefix 1c3::/64 label 12345 2>/dev/null
265
266	if [ $ret -ne 0 ];then
267		echo "FAIL: ipv6 addrlabel"
268		return 1
269	fi
270
271	echo "PASS: ipv6 addrlabel"
272}
273
274kci_test_ifalias()
275{
276	ret=0
277	namewant=$(uuidgen)
278	syspathname="/sys/class/net/$devdummy/ifalias"
279
280	ip link set dev "$devdummy" alias "$namewant"
281	check_err $?
282
283	if [ $ret -ne 0 ]; then
284		echo "FAIL: cannot set interface alias of $devdummy to $namewant"
285		return 1
286	fi
287
288	ip link show "$devdummy" | grep -q "alias $namewant"
289	check_err $?
290
291	if [ -r "$syspathname" ] ; then
292		read namehave < "$syspathname"
293		if [ "$namewant" != "$namehave" ]; then
294			echo "FAIL: did set ifalias $namewant but got $namehave"
295			return 1
296		fi
297
298		namewant=$(uuidgen)
299		echo "$namewant" > "$syspathname"
300	        ip link show "$devdummy" | grep -q "alias $namewant"
301		check_err $?
302
303		# sysfs interface allows to delete alias again
304		echo "" > "$syspathname"
305
306	        ip link show "$devdummy" | grep -q "alias $namewant"
307		check_fail $?
308
309		for i in $(seq 1 100); do
310			uuidgen > "$syspathname" &
311		done
312
313		wait
314
315		# re-add the alias -- kernel should free mem when dummy dev is removed
316		ip link set dev "$devdummy" alias "$namewant"
317		check_err $?
318	fi
319
320	if [ $ret -ne 0 ]; then
321		echo "FAIL: set interface alias $devdummy to $namewant"
322		return 1
323	fi
324
325	echo "PASS: set ifalias $namewant for $devdummy"
326}
327
328kci_test_vrf()
329{
330	vrfname="test-vrf"
331	ret=0
332
333	ip link show type vrf 2>/dev/null
334	if [ $? -ne 0 ]; then
335		echo "SKIP: vrf: iproute2 too old"
336		return 0
337	fi
338
339	ip link add "$vrfname" type vrf table 10
340	check_err $?
341	if [ $ret -ne 0 ];then
342		echo "FAIL: can't add vrf interface, skipping test"
343		return 0
344	fi
345
346	ip -br link show type vrf | grep -q "$vrfname"
347	check_err $?
348	if [ $ret -ne 0 ];then
349		echo "FAIL: created vrf device not found"
350		return 1
351	fi
352
353	ip link set dev "$vrfname" up
354	check_err $?
355
356	ip link set dev "$devdummy" master "$vrfname"
357	check_err $?
358	ip link del dev "$vrfname"
359	check_err $?
360
361	if [ $ret -ne 0 ];then
362		echo "FAIL: vrf"
363		return 1
364	fi
365
366	echo "PASS: vrf"
367}
368
369kci_test_encap_vxlan()
370{
371	ret=0
372	vxlan="test-vxlan0"
373	vlan="test-vlan0"
374	testns="$1"
375
376	ip netns exec "$testns" ip link add "$vxlan" type vxlan id 42 group 239.1.1.1 \
377		dev "$devdummy" dstport 4789 2>/dev/null
378	if [ $? -ne 0 ]; then
379		echo "FAIL: can't add vxlan interface, skipping test"
380		return 0
381	fi
382	check_err $?
383
384	ip netns exec "$testns" ip addr add 10.2.11.49/24 dev "$vxlan"
385	check_err $?
386
387	ip netns exec "$testns" ip link set up dev "$vxlan"
388	check_err $?
389
390	ip netns exec "$testns" ip link add link "$vxlan" name "$vlan" type vlan id 1
391	check_err $?
392
393	ip netns exec "$testns" ip link del "$vxlan"
394	check_err $?
395
396	if [ $ret -ne 0 ]; then
397		echo "FAIL: vxlan"
398		return 1
399	fi
400	echo "PASS: vxlan"
401}
402
403kci_test_encap_fou()
404{
405	ret=0
406	name="test-fou"
407	testns="$1"
408
409	ip fou help 2>&1 |grep -q 'Usage: ip fou'
410	if [ $? -ne 0 ];then
411		echo "SKIP: fou: iproute2 too old"
412		return 1
413	fi
414
415	ip netns exec "$testns" ip fou add port 7777 ipproto 47 2>/dev/null
416	if [ $? -ne 0 ];then
417		echo "FAIL: can't add fou port 7777, skipping test"
418		return 1
419	fi
420
421	ip netns exec "$testns" ip fou add port 8888 ipproto 4
422	check_err $?
423
424	ip netns exec "$testns" ip fou del port 9999 2>/dev/null
425	check_fail $?
426
427	ip netns exec "$testns" ip fou del port 7777
428	check_err $?
429
430	if [ $ret -ne 0 ]; then
431		echo "FAIL: fou"
432		return 1
433	fi
434
435	echo "PASS: fou"
436}
437
438# test various encap methods, use netns to avoid unwanted interference
439kci_test_encap()
440{
441	testns="testns"
442	ret=0
443
444	ip netns add "$testns"
445	if [ $? -ne 0 ]; then
446		echo "SKIP encap tests: cannot add net namespace $testns"
447		return 1
448	fi
449
450	ip netns exec "$testns" ip link set lo up
451	check_err $?
452
453	ip netns exec "$testns" ip link add name "$devdummy" type dummy
454	check_err $?
455	ip netns exec "$testns" ip link set "$devdummy" up
456	check_err $?
457
458	kci_test_encap_vxlan "$testns"
459	kci_test_encap_fou "$testns"
460
461	ip netns del "$testns"
462}
463
464kci_test_macsec()
465{
466	msname="test_macsec0"
467	ret=0
468
469	ip macsec help 2>&1 | grep -q "^Usage: ip macsec"
470	if [ $? -ne 0 ]; then
471		echo "SKIP: macsec: iproute2 too old"
472		return 0
473	fi
474
475	ip link add link "$devdummy" "$msname" type macsec port 42 encrypt on
476	check_err $?
477	if [ $ret -ne 0 ];then
478		echo "FAIL: can't add macsec interface, skipping test"
479		return 1
480	fi
481
482	ip macsec add "$msname" tx sa 0 pn 1024 on key 01 12345678901234567890123456789012
483	check_err $?
484
485	ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef"
486	check_err $?
487
488	ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef" sa 0 pn 1 on key 00 0123456789abcdef0123456789abcdef
489	check_err $?
490
491	ip macsec show > /dev/null
492	check_err $?
493
494	ip link del dev "$msname"
495	check_err $?
496
497	if [ $ret -ne 0 ];then
498		echo "FAIL: macsec"
499		return 1
500	fi
501
502	echo "PASS: macsec"
503}
504
505kci_test_gretap()
506{
507	testns="testns"
508	DEV_NS=gretap00
509	ret=0
510
511	ip netns add "$testns"
512	if [ $? -ne 0 ]; then
513		echo "SKIP gretap tests: cannot add net namespace $testns"
514		return 1
515	fi
516
517	ip link help gretap 2>&1 | grep -q "^Usage:"
518	if [ $? -ne 0 ];then
519		echo "SKIP: gretap: iproute2 too old"
520		ip netns del "$testns"
521		return 1
522	fi
523
524	# test native tunnel
525	ip netns exec "$testns" ip link add dev "$DEV_NS" type gretap seq \
526		key 102 local 172.16.1.100 remote 172.16.1.200
527	check_err $?
528
529	ip netns exec "$testns" ip addr add dev "$DEV_NS" 10.1.1.100/24
530	check_err $?
531
532	ip netns exec "$testns" ip link set dev $DEV_NS up
533	check_err $?
534
535	ip netns exec "$testns" ip link del "$DEV_NS"
536	check_err $?
537
538	# test external mode
539	ip netns exec "$testns" ip link add dev "$DEV_NS" type gretap external
540	check_err $?
541
542	ip netns exec "$testns" ip link del "$DEV_NS"
543	check_err $?
544
545	if [ $ret -ne 0 ]; then
546		echo "FAIL: gretap"
547		ip netns del "$testns"
548		return 1
549	fi
550	echo "PASS: gretap"
551
552	ip netns del "$testns"
553}
554
555kci_test_ip6gretap()
556{
557	testns="testns"
558	DEV_NS=ip6gretap00
559	ret=0
560
561	ip netns add "$testns"
562	if [ $? -ne 0 ]; then
563		echo "SKIP ip6gretap tests: cannot add net namespace $testns"
564		return 1
565	fi
566
567	ip link help ip6gretap 2>&1 | grep -q "^Usage:"
568	if [ $? -ne 0 ];then
569		echo "SKIP: ip6gretap: iproute2 too old"
570		ip netns del "$testns"
571		return 1
572	fi
573
574	# test native tunnel
575	ip netns exec "$testns" ip link add dev "$DEV_NS" type ip6gretap seq \
576		key 102 local fc00:100::1 remote fc00:100::2
577	check_err $?
578
579	ip netns exec "$testns" ip addr add dev "$DEV_NS" fc00:200::1/96
580	check_err $?
581
582	ip netns exec "$testns" ip link set dev $DEV_NS up
583	check_err $?
584
585	ip netns exec "$testns" ip link del "$DEV_NS"
586	check_err $?
587
588	# test external mode
589	ip netns exec "$testns" ip link add dev "$DEV_NS" type ip6gretap external
590	check_err $?
591
592	ip netns exec "$testns" ip link del "$DEV_NS"
593	check_err $?
594
595	if [ $ret -ne 0 ]; then
596		echo "FAIL: ip6gretap"
597		ip netns del "$testns"
598		return 1
599	fi
600	echo "PASS: ip6gretap"
601
602	ip netns del "$testns"
603}
604
605kci_test_erspan()
606{
607	testns="testns"
608	DEV_NS=erspan00
609	ret=0
610
611	ip link help erspan 2>&1 | grep -q "^Usage:"
612	if [ $? -ne 0 ];then
613		echo "SKIP: erspan: iproute2 too old"
614		return 1
615	fi
616
617	ip netns add "$testns"
618	if [ $? -ne 0 ]; then
619		echo "SKIP erspan tests: cannot add net namespace $testns"
620		return 1
621	fi
622
623	# test native tunnel erspan v1
624	ip netns exec "$testns" ip link add dev "$DEV_NS" type erspan seq \
625		key 102 local 172.16.1.100 remote 172.16.1.200 \
626		erspan_ver 1 erspan 488
627	check_err $?
628
629	ip netns exec "$testns" ip addr add dev "$DEV_NS" 10.1.1.100/24
630	check_err $?
631
632	ip netns exec "$testns" ip link set dev $DEV_NS up
633	check_err $?
634
635	ip netns exec "$testns" ip link del "$DEV_NS"
636	check_err $?
637
638	# test native tunnel erspan v2
639	ip netns exec "$testns" ip link add dev "$DEV_NS" type erspan seq \
640		key 102 local 172.16.1.100 remote 172.16.1.200 \
641		erspan_ver 2 erspan_dir ingress erspan_hwid 7
642	check_err $?
643
644	ip netns exec "$testns" ip addr add dev "$DEV_NS" 10.1.1.100/24
645	check_err $?
646
647	ip netns exec "$testns" ip link set dev $DEV_NS up
648	check_err $?
649
650	ip netns exec "$testns" ip link del "$DEV_NS"
651	check_err $?
652
653	# test external mode
654	ip netns exec "$testns" ip link add dev "$DEV_NS" type erspan external
655	check_err $?
656
657	ip netns exec "$testns" ip link del "$DEV_NS"
658	check_err $?
659
660	if [ $ret -ne 0 ]; then
661		echo "FAIL: erspan"
662		ip netns del "$testns"
663		return 1
664	fi
665	echo "PASS: erspan"
666
667	ip netns del "$testns"
668}
669
670kci_test_ip6erspan()
671{
672	testns="testns"
673	DEV_NS=ip6erspan00
674	ret=0
675
676	ip link help ip6erspan 2>&1 | grep -q "^Usage:"
677	if [ $? -ne 0 ];then
678		echo "SKIP: ip6erspan: iproute2 too old"
679		return 1
680	fi
681
682	ip netns add "$testns"
683	if [ $? -ne 0 ]; then
684		echo "SKIP ip6erspan tests: cannot add net namespace $testns"
685		return 1
686	fi
687
688	# test native tunnel ip6erspan v1
689	ip netns exec "$testns" ip link add dev "$DEV_NS" type ip6erspan seq \
690		key 102 local fc00:100::1 remote fc00:100::2 \
691		erspan_ver 1 erspan 488
692	check_err $?
693
694	ip netns exec "$testns" ip addr add dev "$DEV_NS" 10.1.1.100/24
695	check_err $?
696
697	ip netns exec "$testns" ip link set dev $DEV_NS up
698	check_err $?
699
700	ip netns exec "$testns" ip link del "$DEV_NS"
701	check_err $?
702
703	# test native tunnel ip6erspan v2
704	ip netns exec "$testns" ip link add dev "$DEV_NS" type ip6erspan seq \
705		key 102 local fc00:100::1 remote fc00:100::2 \
706		erspan_ver 2 erspan_dir ingress erspan_hwid 7
707	check_err $?
708
709	ip netns exec "$testns" ip addr add dev "$DEV_NS" 10.1.1.100/24
710	check_err $?
711
712	ip netns exec "$testns" ip link set dev $DEV_NS up
713	check_err $?
714
715	ip netns exec "$testns" ip link del "$DEV_NS"
716	check_err $?
717
718	# test external mode
719	ip netns exec "$testns" ip link add dev "$DEV_NS" \
720		type ip6erspan external
721	check_err $?
722
723	ip netns exec "$testns" ip link del "$DEV_NS"
724	check_err $?
725
726	if [ $ret -ne 0 ]; then
727		echo "FAIL: ip6erspan"
728		ip netns del "$testns"
729		return 1
730	fi
731	echo "PASS: ip6erspan"
732
733	ip netns del "$testns"
734}
735
736kci_test_rtnl()
737{
738	kci_add_dummy
739	if [ $ret -ne 0 ];then
740		echo "FAIL: cannot add dummy interface"
741		return 1
742	fi
743
744	kci_test_polrouting
745	kci_test_route_get
746	kci_test_tc
747	kci_test_gre
748	kci_test_gretap
749	kci_test_ip6gretap
750	kci_test_erspan
751	kci_test_ip6erspan
752	kci_test_bridge
753	kci_test_addrlabel
754	kci_test_ifalias
755	kci_test_vrf
756	kci_test_encap
757	kci_test_macsec
758
759	kci_del_dummy
760}
761
762#check for needed privileges
763if [ "$(id -u)" -ne 0 ];then
764	echo "SKIP: Need root privileges"
765	exit 0
766fi
767
768for x in ip tc;do
769	$x -Version 2>/dev/null >/dev/null
770	if [ $? -ne 0 ];then
771		echo "SKIP: Could not run test without the $x tool"
772		exit 0
773	fi
774done
775
776kci_test_rtnl
777
778exit $ret
779