1#!/bin/bash
2#
3# This test is for checking rtnetlink callpaths, and get as much coverage as possible.
4#
5# set -e
6
7ALL_TESTS="
8	kci_test_polrouting
9	kci_test_route_get
10	kci_test_addrlft
11	kci_test_promote_secondaries
12	kci_test_tc
13	kci_test_gre
14	kci_test_gretap
15	kci_test_ip6gretap
16	kci_test_erspan
17	kci_test_ip6erspan
18	kci_test_bridge
19	kci_test_addrlabel
20	kci_test_ifalias
21	kci_test_vrf
22	kci_test_encap
23	kci_test_macsec
24	kci_test_ipsec
25	kci_test_ipsec_offload
26	kci_test_fdb_get
27	kci_test_neigh_get
28	kci_test_bridge_parent_id
29	kci_test_address_proto
30"
31
32devdummy="test-dummy0"
33
34# Kselftest framework requirement - SKIP code is 4.
35ksft_skip=4
36
37# set global exit status, but never reset nonzero one.
38check_err()
39{
40	if [ $ret -eq 0 ]; then
41		ret=$1
42	fi
43}
44
45# same but inverted -- used when command must fail for test to pass
46check_fail()
47{
48	if [ $1 -eq 0 ]; then
49		ret=1
50	fi
51}
52
53kci_add_dummy()
54{
55	ip link add name "$devdummy" type dummy
56	check_err $?
57	ip link set "$devdummy" up
58	check_err $?
59}
60
61kci_del_dummy()
62{
63	ip link del dev "$devdummy"
64	check_err $?
65}
66
67kci_test_netconf()
68{
69	dev="$1"
70	r=$ret
71
72	ip netconf show dev "$dev" > /dev/null
73	check_err $?
74
75	for f in 4 6; do
76		ip -$f netconf show dev "$dev" > /dev/null
77		check_err $?
78	done
79
80	if [ $ret -ne 0 ] ;then
81		echo "FAIL: ip netconf show $dev"
82		test $r -eq 0 && ret=0
83		return 1
84	fi
85}
86
87# add a bridge with vlans on top
88kci_test_bridge()
89{
90	devbr="test-br0"
91	vlandev="testbr-vlan1"
92
93	local ret=0
94	ip link add name "$devbr" type bridge
95	check_err $?
96
97	ip link set dev "$devdummy" master "$devbr"
98	check_err $?
99
100	ip link set "$devbr" up
101	check_err $?
102
103	ip link add link "$devbr" name "$vlandev" type vlan id 1
104	check_err $?
105	ip addr add dev "$vlandev" 10.200.7.23/30
106	check_err $?
107	ip -6 addr add dev "$vlandev" dead:42::1234/64
108	check_err $?
109	ip -d link > /dev/null
110	check_err $?
111	ip r s t all > /dev/null
112	check_err $?
113
114	for name in "$devbr" "$vlandev" "$devdummy" ; do
115		kci_test_netconf "$name"
116	done
117
118	ip -6 addr del dev "$vlandev" dead:42::1234/64
119	check_err $?
120
121	ip link del dev "$vlandev"
122	check_err $?
123	ip link del dev "$devbr"
124	check_err $?
125
126	if [ $ret -ne 0 ];then
127		echo "FAIL: bridge setup"
128		return 1
129	fi
130	echo "PASS: bridge setup"
131
132}
133
134kci_test_gre()
135{
136	gredev=neta
137	rem=10.42.42.1
138	loc=10.0.0.1
139
140	local ret=0
141	ip tunnel add $gredev mode gre remote $rem local $loc ttl 1
142	check_err $?
143	ip link set $gredev up
144	check_err $?
145	ip addr add 10.23.7.10 dev $gredev
146	check_err $?
147	ip route add 10.23.8.0/30 dev $gredev
148	check_err $?
149	ip addr add dev "$devdummy" 10.23.7.11/24
150	check_err $?
151	ip link > /dev/null
152	check_err $?
153	ip addr > /dev/null
154	check_err $?
155
156	kci_test_netconf "$gredev"
157
158	ip addr del dev "$devdummy" 10.23.7.11/24
159	check_err $?
160
161	ip link del $gredev
162	check_err $?
163
164	if [ $ret -ne 0 ];then
165		echo "FAIL: gre tunnel endpoint"
166		return 1
167	fi
168	echo "PASS: gre tunnel endpoint"
169}
170
171# tc uses rtnetlink too, for full tc testing
172# please see tools/testing/selftests/tc-testing.
173kci_test_tc()
174{
175	dev=lo
176	local ret=0
177
178	tc qdisc add dev "$dev" root handle 1: htb
179	check_err $?
180	tc class add dev "$dev" parent 1: classid 1:10 htb rate 1mbit
181	check_err $?
182	tc filter add dev "$dev" parent 1:0 prio 5 handle ffe: protocol ip u32 divisor 256
183	check_err $?
184	tc filter add dev "$dev" parent 1:0 prio 5 handle ffd: protocol ip u32 divisor 256
185	check_err $?
186	tc filter add dev "$dev" parent 1:0 prio 5 handle ffc: protocol ip u32 divisor 256
187	check_err $?
188	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
189	check_err $?
190	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
191	check_err $?
192	tc filter show dev "$dev" parent  1:0 > /dev/null
193	check_err $?
194	tc filter del dev "$dev" protocol ip parent 1: prio 5 handle ffe:2:3 u32
195	check_err $?
196	tc filter show dev "$dev" parent  1:0 > /dev/null
197	check_err $?
198	tc qdisc del dev "$dev" root handle 1: htb
199	check_err $?
200
201	if [ $ret -ne 0 ];then
202		echo "FAIL: tc htb hierarchy"
203		return 1
204	fi
205	echo "PASS: tc htb hierarchy"
206
207}
208
209kci_test_polrouting()
210{
211	local ret=0
212	ip rule add fwmark 1 lookup 100
213	check_err $?
214	ip route add local 0.0.0.0/0 dev lo table 100
215	check_err $?
216	ip r s t all > /dev/null
217	check_err $?
218	ip rule del fwmark 1 lookup 100
219	check_err $?
220	ip route del local 0.0.0.0/0 dev lo table 100
221	check_err $?
222
223	if [ $ret -ne 0 ];then
224		echo "FAIL: policy route test"
225		return 1
226	fi
227	echo "PASS: policy routing"
228}
229
230kci_test_route_get()
231{
232	local hash_policy=$(sysctl -n net.ipv4.fib_multipath_hash_policy)
233
234	local ret=0
235
236	ip route get 127.0.0.1 > /dev/null
237	check_err $?
238	ip route get 127.0.0.1 dev "$devdummy" > /dev/null
239	check_err $?
240	ip route get ::1 > /dev/null
241	check_err $?
242	ip route get fe80::1 dev "$devdummy" > /dev/null
243	check_err $?
244	ip route get 127.0.0.1 from 127.0.0.1 oif lo tos 0x10 mark 0x1 > /dev/null
245	check_err $?
246	ip route get ::1 from ::1 iif lo oif lo tos 0x10 mark 0x1 > /dev/null
247	check_err $?
248	ip addr add dev "$devdummy" 10.23.7.11/24
249	check_err $?
250	ip route get 10.23.7.11 from 10.23.7.12 iif "$devdummy" > /dev/null
251	check_err $?
252	ip route add 10.23.8.0/24 \
253		nexthop via 10.23.7.13 dev "$devdummy" \
254		nexthop via 10.23.7.14 dev "$devdummy"
255	check_err $?
256	sysctl -wq net.ipv4.fib_multipath_hash_policy=0
257	ip route get 10.23.8.11 > /dev/null
258	check_err $?
259	sysctl -wq net.ipv4.fib_multipath_hash_policy=1
260	ip route get 10.23.8.11 > /dev/null
261	check_err $?
262	sysctl -wq net.ipv4.fib_multipath_hash_policy="$hash_policy"
263	ip route del 10.23.8.0/24
264	check_err $?
265	ip addr del dev "$devdummy" 10.23.7.11/24
266	check_err $?
267
268	if [ $ret -ne 0 ];then
269		echo "FAIL: route get"
270		return 1
271	fi
272
273	echo "PASS: route get"
274}
275
276kci_test_addrlft()
277{
278	for i in $(seq 10 100) ;do
279		lft=$(((RANDOM%3) + 1))
280		ip addr add 10.23.11.$i/32 dev "$devdummy" preferred_lft $lft valid_lft $((lft+1))
281		check_err $?
282	done
283
284	sleep 5
285
286	ip addr show dev "$devdummy" | grep "10.23.11."
287	if [ $? -eq 0 ]; then
288		echo "FAIL: preferred_lft addresses remaining"
289		check_err 1
290		return
291	fi
292
293	echo "PASS: preferred_lft addresses have expired"
294}
295
296kci_test_promote_secondaries()
297{
298	promote=$(sysctl -n net.ipv4.conf.$devdummy.promote_secondaries)
299
300	sysctl -q net.ipv4.conf.$devdummy.promote_secondaries=1
301
302	for i in $(seq 2 254);do
303		IP="10.23.11.$i"
304		ip -f inet addr add $IP/16 brd + dev "$devdummy"
305		ifconfig "$devdummy" $IP netmask 255.255.0.0
306	done
307
308	ip addr flush dev "$devdummy"
309
310	[ $promote -eq 0 ] && sysctl -q net.ipv4.conf.$devdummy.promote_secondaries=0
311
312	echo "PASS: promote_secondaries complete"
313}
314
315kci_test_addrlabel()
316{
317	local ret=0
318
319	ip addrlabel add prefix dead::/64 dev lo label 1
320	check_err $?
321
322	ip addrlabel list |grep -q "prefix dead::/64 dev lo label 1"
323	check_err $?
324
325	ip addrlabel del prefix dead::/64 dev lo label 1 2> /dev/null
326	check_err $?
327
328	ip addrlabel add prefix dead::/64 label 1 2> /dev/null
329	check_err $?
330
331	ip addrlabel del prefix dead::/64 label 1 2> /dev/null
332	check_err $?
333
334	# concurrent add/delete
335	for i in $(seq 1 1000); do
336		ip addrlabel add prefix 1c3::/64 label 12345 2>/dev/null
337	done &
338
339	for i in $(seq 1 1000); do
340		ip addrlabel del prefix 1c3::/64 label 12345 2>/dev/null
341	done
342
343	wait
344
345	ip addrlabel del prefix 1c3::/64 label 12345 2>/dev/null
346
347	if [ $ret -ne 0 ];then
348		echo "FAIL: ipv6 addrlabel"
349		return 1
350	fi
351
352	echo "PASS: ipv6 addrlabel"
353}
354
355kci_test_ifalias()
356{
357	local ret=0
358	namewant=$(uuidgen)
359	syspathname="/sys/class/net/$devdummy/ifalias"
360
361	ip link set dev "$devdummy" alias "$namewant"
362	check_err $?
363
364	if [ $ret -ne 0 ]; then
365		echo "FAIL: cannot set interface alias of $devdummy to $namewant"
366		return 1
367	fi
368
369	ip link show "$devdummy" | grep -q "alias $namewant"
370	check_err $?
371
372	if [ -r "$syspathname" ] ; then
373		read namehave < "$syspathname"
374		if [ "$namewant" != "$namehave" ]; then
375			echo "FAIL: did set ifalias $namewant but got $namehave"
376			return 1
377		fi
378
379		namewant=$(uuidgen)
380		echo "$namewant" > "$syspathname"
381	        ip link show "$devdummy" | grep -q "alias $namewant"
382		check_err $?
383
384		# sysfs interface allows to delete alias again
385		echo "" > "$syspathname"
386
387	        ip link show "$devdummy" | grep -q "alias $namewant"
388		check_fail $?
389
390		for i in $(seq 1 100); do
391			uuidgen > "$syspathname" &
392		done
393
394		wait
395
396		# re-add the alias -- kernel should free mem when dummy dev is removed
397		ip link set dev "$devdummy" alias "$namewant"
398		check_err $?
399	fi
400
401	if [ $ret -ne 0 ]; then
402		echo "FAIL: set interface alias $devdummy to $namewant"
403		return 1
404	fi
405
406	echo "PASS: set ifalias $namewant for $devdummy"
407}
408
409kci_test_vrf()
410{
411	vrfname="test-vrf"
412	local ret=0
413
414	ip link show type vrf 2>/dev/null
415	if [ $? -ne 0 ]; then
416		echo "SKIP: vrf: iproute2 too old"
417		return $ksft_skip
418	fi
419
420	ip link add "$vrfname" type vrf table 10
421	check_err $?
422	if [ $ret -ne 0 ];then
423		echo "FAIL: can't add vrf interface, skipping test"
424		return 0
425	fi
426
427	ip -br link show type vrf | grep -q "$vrfname"
428	check_err $?
429	if [ $ret -ne 0 ];then
430		echo "FAIL: created vrf device not found"
431		return 1
432	fi
433
434	ip link set dev "$vrfname" up
435	check_err $?
436
437	ip link set dev "$devdummy" master "$vrfname"
438	check_err $?
439	ip link del dev "$vrfname"
440	check_err $?
441
442	if [ $ret -ne 0 ];then
443		echo "FAIL: vrf"
444		return 1
445	fi
446
447	echo "PASS: vrf"
448}
449
450kci_test_encap_vxlan()
451{
452	local ret=0
453	vxlan="test-vxlan0"
454	vlan="test-vlan0"
455	testns="$1"
456
457	ip -netns "$testns" link add "$vxlan" type vxlan id 42 group 239.1.1.1 \
458		dev "$devdummy" dstport 4789 2>/dev/null
459	if [ $? -ne 0 ]; then
460		echo "FAIL: can't add vxlan interface, skipping test"
461		return 0
462	fi
463	check_err $?
464
465	ip -netns "$testns" addr add 10.2.11.49/24 dev "$vxlan"
466	check_err $?
467
468	ip -netns "$testns" link set up dev "$vxlan"
469	check_err $?
470
471	ip -netns "$testns" link add link "$vxlan" name "$vlan" type vlan id 1
472	check_err $?
473
474	# changelink testcases
475	ip -netns "$testns" link set dev "$vxlan" type vxlan vni 43 2>/dev/null
476	check_fail $?
477
478	ip -netns "$testns" link set dev "$vxlan" type vxlan group ffe5::5 dev "$devdummy" 2>/dev/null
479	check_fail $?
480
481	ip -netns "$testns" link set dev "$vxlan" type vxlan ttl inherit 2>/dev/null
482	check_fail $?
483
484	ip -netns "$testns" link set dev "$vxlan" type vxlan ttl 64
485	check_err $?
486
487	ip -netns "$testns" link set dev "$vxlan" type vxlan nolearning
488	check_err $?
489
490	ip -netns "$testns" link set dev "$vxlan" type vxlan proxy 2>/dev/null
491	check_fail $?
492
493	ip -netns "$testns" link set dev "$vxlan" type vxlan norsc 2>/dev/null
494	check_fail $?
495
496	ip -netns "$testns" link set dev "$vxlan" type vxlan l2miss 2>/dev/null
497	check_fail $?
498
499	ip -netns "$testns" link set dev "$vxlan" type vxlan l3miss 2>/dev/null
500	check_fail $?
501
502	ip -netns "$testns" link set dev "$vxlan" type vxlan external 2>/dev/null
503	check_fail $?
504
505	ip -netns "$testns" link set dev "$vxlan" type vxlan udpcsum 2>/dev/null
506	check_fail $?
507
508	ip -netns "$testns" link set dev "$vxlan" type vxlan udp6zerocsumtx 2>/dev/null
509	check_fail $?
510
511	ip -netns "$testns" link set dev "$vxlan" type vxlan udp6zerocsumrx 2>/dev/null
512	check_fail $?
513
514	ip -netns "$testns" link set dev "$vxlan" type vxlan remcsumtx 2>/dev/null
515	check_fail $?
516
517	ip -netns "$testns" link set dev "$vxlan" type vxlan remcsumrx 2>/dev/null
518	check_fail $?
519
520	ip -netns "$testns" link set dev "$vxlan" type vxlan gbp 2>/dev/null
521	check_fail $?
522
523	ip -netns "$testns" link set dev "$vxlan" type vxlan gpe 2>/dev/null
524	check_fail $?
525
526	ip -netns "$testns" link del "$vxlan"
527	check_err $?
528
529	if [ $ret -ne 0 ]; then
530		echo "FAIL: vxlan"
531		return 1
532	fi
533	echo "PASS: vxlan"
534}
535
536kci_test_encap_fou()
537{
538	local ret=0
539	name="test-fou"
540	testns="$1"
541
542	ip fou help 2>&1 |grep -q 'Usage: ip fou'
543	if [ $? -ne 0 ];then
544		echo "SKIP: fou: iproute2 too old"
545		return $ksft_skip
546	fi
547
548	if ! /sbin/modprobe -q -n fou; then
549		echo "SKIP: module fou is not found"
550		return $ksft_skip
551	fi
552	/sbin/modprobe -q fou
553	ip -netns "$testns" fou add port 7777 ipproto 47 2>/dev/null
554	if [ $? -ne 0 ];then
555		echo "FAIL: can't add fou port 7777, skipping test"
556		return 1
557	fi
558
559	ip -netns "$testns" fou add port 8888 ipproto 4
560	check_err $?
561
562	ip -netns "$testns" fou del port 9999 2>/dev/null
563	check_fail $?
564
565	ip -netns "$testns" fou del port 7777
566	check_err $?
567
568	if [ $ret -ne 0 ]; then
569		echo "FAIL: fou"
570		return 1
571	fi
572
573	echo "PASS: fou"
574}
575
576# test various encap methods, use netns to avoid unwanted interference
577kci_test_encap()
578{
579	testns="testns"
580	local ret=0
581
582	ip netns add "$testns"
583	if [ $? -ne 0 ]; then
584		echo "SKIP encap tests: cannot add net namespace $testns"
585		return $ksft_skip
586	fi
587
588	ip -netns "$testns" link set lo up
589	check_err $?
590
591	ip -netns "$testns" link add name "$devdummy" type dummy
592	check_err $?
593	ip -netns "$testns" link set "$devdummy" up
594	check_err $?
595
596	kci_test_encap_vxlan "$testns"
597	check_err $?
598	kci_test_encap_fou "$testns"
599	check_err $?
600
601	ip netns del "$testns"
602	return $ret
603}
604
605kci_test_macsec()
606{
607	msname="test_macsec0"
608	local ret=0
609
610	ip macsec help 2>&1 | grep -q "^Usage: ip macsec"
611	if [ $? -ne 0 ]; then
612		echo "SKIP: macsec: iproute2 too old"
613		return $ksft_skip
614	fi
615
616	ip link add link "$devdummy" "$msname" type macsec port 42 encrypt on
617	check_err $?
618	if [ $ret -ne 0 ];then
619		echo "FAIL: can't add macsec interface, skipping test"
620		return 1
621	fi
622
623	ip macsec add "$msname" tx sa 0 pn 1024 on key 01 12345678901234567890123456789012
624	check_err $?
625
626	ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef"
627	check_err $?
628
629	ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef" sa 0 pn 1 on key 00 0123456789abcdef0123456789abcdef
630	check_err $?
631
632	ip macsec show > /dev/null
633	check_err $?
634
635	ip link del dev "$msname"
636	check_err $?
637
638	if [ $ret -ne 0 ];then
639		echo "FAIL: macsec"
640		return 1
641	fi
642
643	echo "PASS: macsec"
644}
645
646#-------------------------------------------------------------------
647# Example commands
648#   ip x s add proto esp src 14.0.0.52 dst 14.0.0.70 \
649#            spi 0x07 mode transport reqid 0x07 replay-window 32 \
650#            aead 'rfc4106(gcm(aes))' 1234567890123456dcba 128 \
651#            sel src 14.0.0.52/24 dst 14.0.0.70/24
652#   ip x p add dir out src 14.0.0.52/24 dst 14.0.0.70/24 \
653#            tmpl proto esp src 14.0.0.52 dst 14.0.0.70 \
654#            spi 0x07 mode transport reqid 0x07
655#
656# Subcommands not tested
657#    ip x s update
658#    ip x s allocspi
659#    ip x s deleteall
660#    ip x p update
661#    ip x p deleteall
662#    ip x p set
663#-------------------------------------------------------------------
664kci_test_ipsec()
665{
666	local ret=0
667	algo="aead rfc4106(gcm(aes)) 0x3132333435363738393031323334353664636261 128"
668	srcip=192.168.123.1
669	dstip=192.168.123.2
670	spi=7
671
672	ip addr add $srcip dev $devdummy
673
674	# flush to be sure there's nothing configured
675	ip x s flush ; ip x p flush
676	check_err $?
677
678	# start the monitor in the background
679	tmpfile=`mktemp /var/run/ipsectestXXX`
680	mpid=`(ip x m > $tmpfile & echo $!) 2>/dev/null`
681	sleep 0.2
682
683	ipsecid="proto esp src $srcip dst $dstip spi 0x07"
684	ip x s add $ipsecid \
685            mode transport reqid 0x07 replay-window 32 \
686            $algo sel src $srcip/24 dst $dstip/24
687	check_err $?
688
689	lines=`ip x s list | grep $srcip | grep $dstip | wc -l`
690	test $lines -eq 2
691	check_err $?
692
693	ip x s count | grep -q "SAD count 1"
694	check_err $?
695
696	lines=`ip x s get $ipsecid | grep $srcip | grep $dstip | wc -l`
697	test $lines -eq 2
698	check_err $?
699
700	ip x s delete $ipsecid
701	check_err $?
702
703	lines=`ip x s list | wc -l`
704	test $lines -eq 0
705	check_err $?
706
707	ipsecsel="dir out src $srcip/24 dst $dstip/24"
708	ip x p add $ipsecsel \
709		    tmpl proto esp src $srcip dst $dstip \
710		    spi 0x07 mode transport reqid 0x07
711	check_err $?
712
713	lines=`ip x p list | grep $srcip | grep $dstip | wc -l`
714	test $lines -eq 2
715	check_err $?
716
717	ip x p count | grep -q "SPD IN  0 OUT 1 FWD 0"
718	check_err $?
719
720	lines=`ip x p get $ipsecsel | grep $srcip | grep $dstip | wc -l`
721	test $lines -eq 2
722	check_err $?
723
724	ip x p delete $ipsecsel
725	check_err $?
726
727	lines=`ip x p list | wc -l`
728	test $lines -eq 0
729	check_err $?
730
731	# check the monitor results
732	kill $mpid
733	lines=`wc -l $tmpfile | cut "-d " -f1`
734	test $lines -eq 20
735	check_err $?
736	rm -rf $tmpfile
737
738	# clean up any leftovers
739	ip x s flush
740	check_err $?
741	ip x p flush
742	check_err $?
743	ip addr del $srcip/32 dev $devdummy
744
745	if [ $ret -ne 0 ]; then
746		echo "FAIL: ipsec"
747		return 1
748	fi
749	echo "PASS: ipsec"
750}
751
752#-------------------------------------------------------------------
753# Example commands
754#   ip x s add proto esp src 14.0.0.52 dst 14.0.0.70 \
755#            spi 0x07 mode transport reqid 0x07 replay-window 32 \
756#            aead 'rfc4106(gcm(aes))' 1234567890123456dcba 128 \
757#            sel src 14.0.0.52/24 dst 14.0.0.70/24
758#            offload dev sim1 dir out
759#   ip x p add dir out src 14.0.0.52/24 dst 14.0.0.70/24 \
760#            tmpl proto esp src 14.0.0.52 dst 14.0.0.70 \
761#            spi 0x07 mode transport reqid 0x07
762#
763#-------------------------------------------------------------------
764kci_test_ipsec_offload()
765{
766	local ret=0
767	algo="aead rfc4106(gcm(aes)) 0x3132333435363738393031323334353664636261 128"
768	srcip=192.168.123.3
769	dstip=192.168.123.4
770	sysfsd=/sys/kernel/debug/netdevsim/netdevsim0/ports/0/
771	sysfsf=$sysfsd/ipsec
772	sysfsnet=/sys/bus/netdevsim/devices/netdevsim0/net/
773	probed=false
774
775	# setup netdevsim since dummydev doesn't have offload support
776	if [ ! -w /sys/bus/netdevsim/new_device ] ; then
777		modprobe -q netdevsim
778		check_err $?
779		if [ $ret -ne 0 ]; then
780			echo "SKIP: ipsec_offload can't load netdevsim"
781			return $ksft_skip
782		fi
783		probed=true
784	fi
785
786	echo "0" > /sys/bus/netdevsim/new_device
787	while [ ! -d $sysfsnet ] ; do :; done
788	udevadm settle
789	dev=`ls $sysfsnet`
790
791	ip addr add $srcip dev $dev
792	ip link set $dev up
793	if [ ! -d $sysfsd ] ; then
794		echo "FAIL: ipsec_offload can't create device $dev"
795		return 1
796	fi
797	if [ ! -f $sysfsf ] ; then
798		echo "FAIL: ipsec_offload netdevsim doesn't support IPsec offload"
799		return 1
800	fi
801
802	# flush to be sure there's nothing configured
803	ip x s flush ; ip x p flush
804
805	# create offloaded SAs, both in and out
806	ip x p add dir out src $srcip/24 dst $dstip/24 \
807	    tmpl proto esp src $srcip dst $dstip spi 9 \
808	    mode transport reqid 42
809	check_err $?
810	ip x p add dir in src $dstip/24 dst $srcip/24 \
811	    tmpl proto esp src $dstip dst $srcip spi 9 \
812	    mode transport reqid 42
813	check_err $?
814
815	ip x s add proto esp src $srcip dst $dstip spi 9 \
816	    mode transport reqid 42 $algo sel src $srcip/24 dst $dstip/24 \
817	    offload dev $dev dir out
818	check_err $?
819	ip x s add proto esp src $dstip dst $srcip spi 9 \
820	    mode transport reqid 42 $algo sel src $dstip/24 dst $srcip/24 \
821	    offload dev $dev dir in
822	check_err $?
823	if [ $ret -ne 0 ]; then
824		echo "FAIL: ipsec_offload can't create SA"
825		return 1
826	fi
827
828	# does offload show up in ip output
829	lines=`ip x s list | grep -c "crypto offload parameters: dev $dev dir"`
830	if [ $lines -ne 2 ] ; then
831		echo "FAIL: ipsec_offload SA offload missing from list output"
832		check_err 1
833	fi
834
835	# use ping to exercise the Tx path
836	ping -I $dev -c 3 -W 1 -i 0 $dstip >/dev/null
837
838	# does driver have correct offload info
839	diff $sysfsf - << EOF
840SA count=2 tx=3
841sa[0] tx ipaddr=0x00000000 00000000 00000000 00000000
842sa[0]    spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1
843sa[0]    key=0x34333231 38373635 32313039 36353433
844sa[1] rx ipaddr=0x00000000 00000000 00000000 037ba8c0
845sa[1]    spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1
846sa[1]    key=0x34333231 38373635 32313039 36353433
847EOF
848	if [ $? -ne 0 ] ; then
849		echo "FAIL: ipsec_offload incorrect driver data"
850		check_err 1
851	fi
852
853	# does offload get removed from driver
854	ip x s flush
855	ip x p flush
856	lines=`grep -c "SA count=0" $sysfsf`
857	if [ $lines -ne 1 ] ; then
858		echo "FAIL: ipsec_offload SA not removed from driver"
859		check_err 1
860	fi
861
862	# clean up any leftovers
863	$probed && rmmod netdevsim
864
865	if [ $ret -ne 0 ]; then
866		echo "FAIL: ipsec_offload"
867		return 1
868	fi
869	echo "PASS: ipsec_offload"
870}
871
872kci_test_gretap()
873{
874	testns="testns"
875	DEV_NS=gretap00
876	local ret=0
877
878	ip netns add "$testns"
879	if [ $? -ne 0 ]; then
880		echo "SKIP gretap tests: cannot add net namespace $testns"
881		return $ksft_skip
882	fi
883
884	ip link help gretap 2>&1 | grep -q "^Usage:"
885	if [ $? -ne 0 ];then
886		echo "SKIP: gretap: iproute2 too old"
887		ip netns del "$testns"
888		return $ksft_skip
889	fi
890
891	# test native tunnel
892	ip -netns "$testns" link add dev "$DEV_NS" type gretap seq \
893		key 102 local 172.16.1.100 remote 172.16.1.200
894	check_err $?
895
896	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
897	check_err $?
898
899	ip -netns "$testns" link set dev $DEV_NS up
900	check_err $?
901
902	ip -netns "$testns" link del "$DEV_NS"
903	check_err $?
904
905	# test external mode
906	ip -netns "$testns" link add dev "$DEV_NS" type gretap external
907	check_err $?
908
909	ip -netns "$testns" link del "$DEV_NS"
910	check_err $?
911
912	if [ $ret -ne 0 ]; then
913		echo "FAIL: gretap"
914		ip netns del "$testns"
915		return 1
916	fi
917	echo "PASS: gretap"
918
919	ip netns del "$testns"
920}
921
922kci_test_ip6gretap()
923{
924	testns="testns"
925	DEV_NS=ip6gretap00
926	local ret=0
927
928	ip netns add "$testns"
929	if [ $? -ne 0 ]; then
930		echo "SKIP ip6gretap tests: cannot add net namespace $testns"
931		return $ksft_skip
932	fi
933
934	ip link help ip6gretap 2>&1 | grep -q "^Usage:"
935	if [ $? -ne 0 ];then
936		echo "SKIP: ip6gretap: iproute2 too old"
937		ip netns del "$testns"
938		return $ksft_skip
939	fi
940
941	# test native tunnel
942	ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap seq \
943		key 102 local fc00:100::1 remote fc00:100::2
944	check_err $?
945
946	ip -netns "$testns" addr add dev "$DEV_NS" fc00:200::1/96
947	check_err $?
948
949	ip -netns "$testns" link set dev $DEV_NS up
950	check_err $?
951
952	ip -netns "$testns" link del "$DEV_NS"
953	check_err $?
954
955	# test external mode
956	ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap external
957	check_err $?
958
959	ip -netns "$testns" link del "$DEV_NS"
960	check_err $?
961
962	if [ $ret -ne 0 ]; then
963		echo "FAIL: ip6gretap"
964		ip netns del "$testns"
965		return 1
966	fi
967	echo "PASS: ip6gretap"
968
969	ip netns del "$testns"
970}
971
972kci_test_erspan()
973{
974	testns="testns"
975	DEV_NS=erspan00
976	local ret=0
977
978	ip link help erspan 2>&1 | grep -q "^Usage:"
979	if [ $? -ne 0 ];then
980		echo "SKIP: erspan: iproute2 too old"
981		return $ksft_skip
982	fi
983
984	ip netns add "$testns"
985	if [ $? -ne 0 ]; then
986		echo "SKIP erspan tests: cannot add net namespace $testns"
987		return $ksft_skip
988	fi
989
990	# test native tunnel erspan v1
991	ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \
992		key 102 local 172.16.1.100 remote 172.16.1.200 \
993		erspan_ver 1 erspan 488
994	check_err $?
995
996	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
997	check_err $?
998
999	ip -netns "$testns" link set dev $DEV_NS up
1000	check_err $?
1001
1002	ip -netns "$testns" link del "$DEV_NS"
1003	check_err $?
1004
1005	# test native tunnel erspan v2
1006	ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \
1007		key 102 local 172.16.1.100 remote 172.16.1.200 \
1008		erspan_ver 2 erspan_dir ingress erspan_hwid 7
1009	check_err $?
1010
1011	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
1012	check_err $?
1013
1014	ip -netns "$testns" link set dev $DEV_NS up
1015	check_err $?
1016
1017	ip -netns "$testns" link del "$DEV_NS"
1018	check_err $?
1019
1020	# test external mode
1021	ip -netns "$testns" link add dev "$DEV_NS" type erspan external
1022	check_err $?
1023
1024	ip -netns "$testns" link del "$DEV_NS"
1025	check_err $?
1026
1027	if [ $ret -ne 0 ]; then
1028		echo "FAIL: erspan"
1029		ip netns del "$testns"
1030		return 1
1031	fi
1032	echo "PASS: erspan"
1033
1034	ip netns del "$testns"
1035}
1036
1037kci_test_ip6erspan()
1038{
1039	testns="testns"
1040	DEV_NS=ip6erspan00
1041	local ret=0
1042
1043	ip link help ip6erspan 2>&1 | grep -q "^Usage:"
1044	if [ $? -ne 0 ];then
1045		echo "SKIP: ip6erspan: iproute2 too old"
1046		return $ksft_skip
1047	fi
1048
1049	ip netns add "$testns"
1050	if [ $? -ne 0 ]; then
1051		echo "SKIP ip6erspan tests: cannot add net namespace $testns"
1052		return $ksft_skip
1053	fi
1054
1055	# test native tunnel ip6erspan v1
1056	ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \
1057		key 102 local fc00:100::1 remote fc00:100::2 \
1058		erspan_ver 1 erspan 488
1059	check_err $?
1060
1061	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
1062	check_err $?
1063
1064	ip -netns "$testns" link set dev $DEV_NS up
1065	check_err $?
1066
1067	ip -netns "$testns" link del "$DEV_NS"
1068	check_err $?
1069
1070	# test native tunnel ip6erspan v2
1071	ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \
1072		key 102 local fc00:100::1 remote fc00:100::2 \
1073		erspan_ver 2 erspan_dir ingress erspan_hwid 7
1074	check_err $?
1075
1076	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
1077	check_err $?
1078
1079	ip -netns "$testns" link set dev $DEV_NS up
1080	check_err $?
1081
1082	ip -netns "$testns" link del "$DEV_NS"
1083	check_err $?
1084
1085	# test external mode
1086	ip -netns "$testns" link add dev "$DEV_NS" \
1087		type ip6erspan external
1088	check_err $?
1089
1090	ip -netns "$testns" link del "$DEV_NS"
1091	check_err $?
1092
1093	if [ $ret -ne 0 ]; then
1094		echo "FAIL: ip6erspan"
1095		ip netns del "$testns"
1096		return 1
1097	fi
1098	echo "PASS: ip6erspan"
1099
1100	ip netns del "$testns"
1101}
1102
1103kci_test_fdb_get()
1104{
1105	IP="ip -netns testns"
1106	BRIDGE="bridge -netns testns"
1107	brdev="test-br0"
1108	vxlandev="vxlan10"
1109	test_mac=de:ad:be:ef:13:37
1110	localip="10.0.2.2"
1111	dstip="10.0.2.3"
1112	local ret=0
1113
1114	bridge fdb help 2>&1 |grep -q 'bridge fdb get'
1115	if [ $? -ne 0 ];then
1116		echo "SKIP: fdb get tests: iproute2 too old"
1117		return $ksft_skip
1118	fi
1119
1120	ip netns add testns
1121	if [ $? -ne 0 ]; then
1122		echo "SKIP fdb get tests: cannot add net namespace $testns"
1123		return $ksft_skip
1124	fi
1125
1126	$IP link add "$vxlandev" type vxlan id 10 local $localip \
1127                dstport 4789 2>/dev/null
1128	check_err $?
1129	$IP link add name "$brdev" type bridge &>/dev/null
1130	check_err $?
1131	$IP link set dev "$vxlandev" master "$brdev" &>/dev/null
1132	check_err $?
1133	$BRIDGE fdb add $test_mac dev "$vxlandev" master &>/dev/null
1134	check_err $?
1135	$BRIDGE fdb add $test_mac dev "$vxlandev" dst $dstip self &>/dev/null
1136	check_err $?
1137
1138	$BRIDGE fdb get $test_mac brport "$vxlandev" 2>/dev/null | grep -q "dev $vxlandev master $brdev"
1139	check_err $?
1140	$BRIDGE fdb get $test_mac br "$brdev" 2>/dev/null | grep -q "dev $vxlandev master $brdev"
1141	check_err $?
1142	$BRIDGE fdb get $test_mac dev "$vxlandev" self 2>/dev/null | grep -q "dev $vxlandev dst $dstip"
1143	check_err $?
1144
1145	ip netns del testns &>/dev/null
1146
1147	if [ $ret -ne 0 ]; then
1148		echo "FAIL: bridge fdb get"
1149		return 1
1150	fi
1151
1152	echo "PASS: bridge fdb get"
1153}
1154
1155kci_test_neigh_get()
1156{
1157	dstmac=de:ad:be:ef:13:37
1158	dstip=10.0.2.4
1159	dstip6=dead::2
1160	local ret=0
1161
1162	ip neigh help 2>&1 |grep -q 'ip neigh get'
1163	if [ $? -ne 0 ];then
1164		echo "SKIP: fdb get tests: iproute2 too old"
1165		return $ksft_skip
1166	fi
1167
1168	# ipv4
1169	ip neigh add $dstip lladdr $dstmac dev "$devdummy"  > /dev/null
1170	check_err $?
1171	ip neigh get $dstip dev "$devdummy" 2> /dev/null | grep -q "$dstmac"
1172	check_err $?
1173	ip neigh del $dstip lladdr $dstmac dev "$devdummy"  > /dev/null
1174	check_err $?
1175
1176	# ipv4 proxy
1177	ip neigh add proxy $dstip dev "$devdummy" > /dev/null
1178	check_err $?
1179	ip neigh get proxy $dstip dev "$devdummy" 2>/dev/null | grep -q "$dstip"
1180	check_err $?
1181	ip neigh del proxy $dstip dev "$devdummy" > /dev/null
1182	check_err $?
1183
1184	# ipv6
1185	ip neigh add $dstip6 lladdr $dstmac dev "$devdummy"  > /dev/null
1186	check_err $?
1187	ip neigh get $dstip6 dev "$devdummy" 2> /dev/null | grep -q "$dstmac"
1188	check_err $?
1189	ip neigh del $dstip6 lladdr $dstmac dev "$devdummy"  > /dev/null
1190	check_err $?
1191
1192	# ipv6 proxy
1193	ip neigh add proxy $dstip6 dev "$devdummy" > /dev/null
1194	check_err $?
1195	ip neigh get proxy $dstip6 dev "$devdummy" 2>/dev/null | grep -q "$dstip6"
1196	check_err $?
1197	ip neigh del proxy $dstip6 dev "$devdummy" > /dev/null
1198	check_err $?
1199
1200	if [ $ret -ne 0 ];then
1201		echo "FAIL: neigh get"
1202		return 1
1203	fi
1204
1205	echo "PASS: neigh get"
1206}
1207
1208kci_test_bridge_parent_id()
1209{
1210	local ret=0
1211	sysfsnet=/sys/bus/netdevsim/devices/netdevsim
1212	probed=false
1213
1214	if [ ! -w /sys/bus/netdevsim/new_device ] ; then
1215		modprobe -q netdevsim
1216		check_err $?
1217		if [ $ret -ne 0 ]; then
1218			echo "SKIP: bridge_parent_id can't load netdevsim"
1219			return $ksft_skip
1220		fi
1221		probed=true
1222	fi
1223
1224	echo "10 1" > /sys/bus/netdevsim/new_device
1225	while [ ! -d ${sysfsnet}10 ] ; do :; done
1226	echo "20 1" > /sys/bus/netdevsim/new_device
1227	while [ ! -d ${sysfsnet}20 ] ; do :; done
1228	udevadm settle
1229	dev10=`ls ${sysfsnet}10/net/`
1230	dev20=`ls ${sysfsnet}20/net/`
1231
1232	ip link add name test-bond0 type bond mode 802.3ad
1233	ip link set dev $dev10 master test-bond0
1234	ip link set dev $dev20 master test-bond0
1235	ip link add name test-br0 type bridge
1236	ip link set dev test-bond0 master test-br0
1237	check_err $?
1238
1239	# clean up any leftovers
1240	ip link del dev test-br0
1241	ip link del dev test-bond0
1242	echo 20 > /sys/bus/netdevsim/del_device
1243	echo 10 > /sys/bus/netdevsim/del_device
1244	$probed && rmmod netdevsim
1245
1246	if [ $ret -ne 0 ]; then
1247		echo "FAIL: bridge_parent_id"
1248		return 1
1249	fi
1250	echo "PASS: bridge_parent_id"
1251}
1252
1253address_get_proto()
1254{
1255	local addr=$1; shift
1256
1257	ip -N -j address show dev "$devdummy" |
1258	    jq -e -r --arg addr "${addr%/*}" \
1259	       '.[].addr_info[] | select(.local == $addr) | .protocol'
1260}
1261
1262address_count()
1263{
1264	ip -N -j address show dev "$devdummy" "$@" |
1265	    jq -e -r '[.[].addr_info[] | .local | select(. != null)] | length'
1266}
1267
1268do_test_address_proto()
1269{
1270	local what=$1; shift
1271	local addr=$1; shift
1272	local addr2=${addr%/*}2/${addr#*/}
1273	local addr3=${addr%/*}3/${addr#*/}
1274	local proto
1275	local count
1276	local ret=0
1277	local err
1278
1279	ip address add dev "$devdummy" "$addr3"
1280	check_err $?
1281	proto=$(address_get_proto "$addr3")
1282	[[ "$proto" == null ]]
1283	check_err $?
1284
1285	ip address add dev "$devdummy" "$addr2" proto 0x99
1286	check_err $?
1287	proto=$(address_get_proto "$addr2")
1288	[[ "$proto" == 0x99 ]]
1289	check_err $?
1290
1291	ip address add dev "$devdummy" "$addr" proto 0xab
1292	check_err $?
1293	proto=$(address_get_proto "$addr")
1294	[[ "$proto" == 0xab ]]
1295	check_err $?
1296
1297	ip address replace dev "$devdummy" "$addr" proto 0x11
1298	proto=$(address_get_proto "$addr")
1299	check_err $?
1300	[[ "$proto" == 0x11 ]]
1301	check_err $?
1302
1303	count=$(address_count)
1304	check_err $?
1305	(( count >= 3 )) # $addr, $addr2 and $addr3 plus any kernel addresses
1306	check_err $?
1307
1308	count=$(address_count proto 0)
1309	check_err $?
1310	(( count == 1 )) # just $addr3
1311	check_err $?
1312
1313	count=$(address_count proto 0x11)
1314	check_err $?
1315	(( count == 2 )) # $addr and $addr3
1316	check_err $?
1317
1318	count=$(address_count proto 0xab)
1319	check_err $?
1320	(( count == 1 )) # just $addr3
1321	check_err $?
1322
1323	ip address del dev "$devdummy" "$addr"
1324	ip address del dev "$devdummy" "$addr2"
1325	ip address del dev "$devdummy" "$addr3"
1326
1327	if [ $ret -ne 0 ]; then
1328		echo "FAIL: address proto $what"
1329		return 1
1330	fi
1331	echo "PASS: address proto $what"
1332}
1333
1334kci_test_address_proto()
1335{
1336	local ret=0
1337
1338	do_test_address_proto IPv4 192.0.2.1/28
1339	check_err $?
1340
1341	do_test_address_proto IPv6 2001:db8:1::1/64
1342	check_err $?
1343
1344	return $ret
1345}
1346
1347kci_test_rtnl()
1348{
1349	local current_test
1350	local ret=0
1351
1352	kci_add_dummy
1353	if [ $ret -ne 0 ];then
1354		echo "FAIL: cannot add dummy interface"
1355		return 1
1356	fi
1357
1358	for current_test in ${TESTS:-$ALL_TESTS}; do
1359		$current_test
1360		check_err $?
1361	done
1362
1363	kci_del_dummy
1364	return $ret
1365}
1366
1367usage()
1368{
1369	cat <<EOF
1370usage: ${0##*/} OPTS
1371
1372        -t <test>   Test(s) to run (default: all)
1373                    (options: $(echo $ALL_TESTS))
1374EOF
1375}
1376
1377#check for needed privileges
1378if [ "$(id -u)" -ne 0 ];then
1379	echo "SKIP: Need root privileges"
1380	exit $ksft_skip
1381fi
1382
1383for x in ip tc;do
1384	$x -Version 2>/dev/null >/dev/null
1385	if [ $? -ne 0 ];then
1386		echo "SKIP: Could not run test without the $x tool"
1387		exit $ksft_skip
1388	fi
1389done
1390
1391while getopts t:h o; do
1392	case $o in
1393		t) TESTS=$OPTARG;;
1394		h) usage; exit 0;;
1395		*) usage; exit 1;;
1396	esac
1397done
1398
1399kci_test_rtnl
1400
1401exit $?
1402