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	echo 0 > /sys/bus/netdevsim/del_device
864	$probed && rmmod netdevsim
865
866	if [ $ret -ne 0 ]; then
867		echo "FAIL: ipsec_offload"
868		return 1
869	fi
870	echo "PASS: ipsec_offload"
871}
872
873kci_test_gretap()
874{
875	testns="testns"
876	DEV_NS=gretap00
877	local ret=0
878
879	ip netns add "$testns"
880	if [ $? -ne 0 ]; then
881		echo "SKIP gretap tests: cannot add net namespace $testns"
882		return $ksft_skip
883	fi
884
885	ip link help gretap 2>&1 | grep -q "^Usage:"
886	if [ $? -ne 0 ];then
887		echo "SKIP: gretap: iproute2 too old"
888		ip netns del "$testns"
889		return $ksft_skip
890	fi
891
892	# test native tunnel
893	ip -netns "$testns" link add dev "$DEV_NS" type gretap seq \
894		key 102 local 172.16.1.100 remote 172.16.1.200
895	check_err $?
896
897	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
898	check_err $?
899
900	ip -netns "$testns" link set dev $DEV_NS up
901	check_err $?
902
903	ip -netns "$testns" link del "$DEV_NS"
904	check_err $?
905
906	# test external mode
907	ip -netns "$testns" link add dev "$DEV_NS" type gretap external
908	check_err $?
909
910	ip -netns "$testns" link del "$DEV_NS"
911	check_err $?
912
913	if [ $ret -ne 0 ]; then
914		echo "FAIL: gretap"
915		ip netns del "$testns"
916		return 1
917	fi
918	echo "PASS: gretap"
919
920	ip netns del "$testns"
921}
922
923kci_test_ip6gretap()
924{
925	testns="testns"
926	DEV_NS=ip6gretap00
927	local ret=0
928
929	ip netns add "$testns"
930	if [ $? -ne 0 ]; then
931		echo "SKIP ip6gretap tests: cannot add net namespace $testns"
932		return $ksft_skip
933	fi
934
935	ip link help ip6gretap 2>&1 | grep -q "^Usage:"
936	if [ $? -ne 0 ];then
937		echo "SKIP: ip6gretap: iproute2 too old"
938		ip netns del "$testns"
939		return $ksft_skip
940	fi
941
942	# test native tunnel
943	ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap seq \
944		key 102 local fc00:100::1 remote fc00:100::2
945	check_err $?
946
947	ip -netns "$testns" addr add dev "$DEV_NS" fc00:200::1/96
948	check_err $?
949
950	ip -netns "$testns" link set dev $DEV_NS up
951	check_err $?
952
953	ip -netns "$testns" link del "$DEV_NS"
954	check_err $?
955
956	# test external mode
957	ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap external
958	check_err $?
959
960	ip -netns "$testns" link del "$DEV_NS"
961	check_err $?
962
963	if [ $ret -ne 0 ]; then
964		echo "FAIL: ip6gretap"
965		ip netns del "$testns"
966		return 1
967	fi
968	echo "PASS: ip6gretap"
969
970	ip netns del "$testns"
971}
972
973kci_test_erspan()
974{
975	testns="testns"
976	DEV_NS=erspan00
977	local ret=0
978
979	ip link help erspan 2>&1 | grep -q "^Usage:"
980	if [ $? -ne 0 ];then
981		echo "SKIP: erspan: iproute2 too old"
982		return $ksft_skip
983	fi
984
985	ip netns add "$testns"
986	if [ $? -ne 0 ]; then
987		echo "SKIP erspan tests: cannot add net namespace $testns"
988		return $ksft_skip
989	fi
990
991	# test native tunnel erspan v1
992	ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \
993		key 102 local 172.16.1.100 remote 172.16.1.200 \
994		erspan_ver 1 erspan 488
995	check_err $?
996
997	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
998	check_err $?
999
1000	ip -netns "$testns" link set dev $DEV_NS up
1001	check_err $?
1002
1003	ip -netns "$testns" link del "$DEV_NS"
1004	check_err $?
1005
1006	# test native tunnel erspan v2
1007	ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \
1008		key 102 local 172.16.1.100 remote 172.16.1.200 \
1009		erspan_ver 2 erspan_dir ingress erspan_hwid 7
1010	check_err $?
1011
1012	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
1013	check_err $?
1014
1015	ip -netns "$testns" link set dev $DEV_NS up
1016	check_err $?
1017
1018	ip -netns "$testns" link del "$DEV_NS"
1019	check_err $?
1020
1021	# test external mode
1022	ip -netns "$testns" link add dev "$DEV_NS" type erspan external
1023	check_err $?
1024
1025	ip -netns "$testns" link del "$DEV_NS"
1026	check_err $?
1027
1028	if [ $ret -ne 0 ]; then
1029		echo "FAIL: erspan"
1030		ip netns del "$testns"
1031		return 1
1032	fi
1033	echo "PASS: erspan"
1034
1035	ip netns del "$testns"
1036}
1037
1038kci_test_ip6erspan()
1039{
1040	testns="testns"
1041	DEV_NS=ip6erspan00
1042	local ret=0
1043
1044	ip link help ip6erspan 2>&1 | grep -q "^Usage:"
1045	if [ $? -ne 0 ];then
1046		echo "SKIP: ip6erspan: iproute2 too old"
1047		return $ksft_skip
1048	fi
1049
1050	ip netns add "$testns"
1051	if [ $? -ne 0 ]; then
1052		echo "SKIP ip6erspan tests: cannot add net namespace $testns"
1053		return $ksft_skip
1054	fi
1055
1056	# test native tunnel ip6erspan v1
1057	ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \
1058		key 102 local fc00:100::1 remote fc00:100::2 \
1059		erspan_ver 1 erspan 488
1060	check_err $?
1061
1062	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
1063	check_err $?
1064
1065	ip -netns "$testns" link set dev $DEV_NS up
1066	check_err $?
1067
1068	ip -netns "$testns" link del "$DEV_NS"
1069	check_err $?
1070
1071	# test native tunnel ip6erspan v2
1072	ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \
1073		key 102 local fc00:100::1 remote fc00:100::2 \
1074		erspan_ver 2 erspan_dir ingress erspan_hwid 7
1075	check_err $?
1076
1077	ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
1078	check_err $?
1079
1080	ip -netns "$testns" link set dev $DEV_NS up
1081	check_err $?
1082
1083	ip -netns "$testns" link del "$DEV_NS"
1084	check_err $?
1085
1086	# test external mode
1087	ip -netns "$testns" link add dev "$DEV_NS" \
1088		type ip6erspan external
1089	check_err $?
1090
1091	ip -netns "$testns" link del "$DEV_NS"
1092	check_err $?
1093
1094	if [ $ret -ne 0 ]; then
1095		echo "FAIL: ip6erspan"
1096		ip netns del "$testns"
1097		return 1
1098	fi
1099	echo "PASS: ip6erspan"
1100
1101	ip netns del "$testns"
1102}
1103
1104kci_test_fdb_get()
1105{
1106	IP="ip -netns testns"
1107	BRIDGE="bridge -netns testns"
1108	brdev="test-br0"
1109	vxlandev="vxlan10"
1110	test_mac=de:ad:be:ef:13:37
1111	localip="10.0.2.2"
1112	dstip="10.0.2.3"
1113	local ret=0
1114
1115	bridge fdb help 2>&1 |grep -q 'bridge fdb get'
1116	if [ $? -ne 0 ];then
1117		echo "SKIP: fdb get tests: iproute2 too old"
1118		return $ksft_skip
1119	fi
1120
1121	ip netns add testns
1122	if [ $? -ne 0 ]; then
1123		echo "SKIP fdb get tests: cannot add net namespace $testns"
1124		return $ksft_skip
1125	fi
1126
1127	$IP link add "$vxlandev" type vxlan id 10 local $localip \
1128                dstport 4789 2>/dev/null
1129	check_err $?
1130	$IP link add name "$brdev" type bridge &>/dev/null
1131	check_err $?
1132	$IP link set dev "$vxlandev" master "$brdev" &>/dev/null
1133	check_err $?
1134	$BRIDGE fdb add $test_mac dev "$vxlandev" master &>/dev/null
1135	check_err $?
1136	$BRIDGE fdb add $test_mac dev "$vxlandev" dst $dstip self &>/dev/null
1137	check_err $?
1138
1139	$BRIDGE fdb get $test_mac brport "$vxlandev" 2>/dev/null | grep -q "dev $vxlandev master $brdev"
1140	check_err $?
1141	$BRIDGE fdb get $test_mac br "$brdev" 2>/dev/null | grep -q "dev $vxlandev master $brdev"
1142	check_err $?
1143	$BRIDGE fdb get $test_mac dev "$vxlandev" self 2>/dev/null | grep -q "dev $vxlandev dst $dstip"
1144	check_err $?
1145
1146	ip netns del testns &>/dev/null
1147
1148	if [ $ret -ne 0 ]; then
1149		echo "FAIL: bridge fdb get"
1150		return 1
1151	fi
1152
1153	echo "PASS: bridge fdb get"
1154}
1155
1156kci_test_neigh_get()
1157{
1158	dstmac=de:ad:be:ef:13:37
1159	dstip=10.0.2.4
1160	dstip6=dead::2
1161	local ret=0
1162
1163	ip neigh help 2>&1 |grep -q 'ip neigh get'
1164	if [ $? -ne 0 ];then
1165		echo "SKIP: fdb get tests: iproute2 too old"
1166		return $ksft_skip
1167	fi
1168
1169	# ipv4
1170	ip neigh add $dstip lladdr $dstmac dev "$devdummy"  > /dev/null
1171	check_err $?
1172	ip neigh get $dstip dev "$devdummy" 2> /dev/null | grep -q "$dstmac"
1173	check_err $?
1174	ip neigh del $dstip lladdr $dstmac dev "$devdummy"  > /dev/null
1175	check_err $?
1176
1177	# ipv4 proxy
1178	ip neigh add proxy $dstip dev "$devdummy" > /dev/null
1179	check_err $?
1180	ip neigh get proxy $dstip dev "$devdummy" 2>/dev/null | grep -q "$dstip"
1181	check_err $?
1182	ip neigh del proxy $dstip dev "$devdummy" > /dev/null
1183	check_err $?
1184
1185	# ipv6
1186	ip neigh add $dstip6 lladdr $dstmac dev "$devdummy"  > /dev/null
1187	check_err $?
1188	ip neigh get $dstip6 dev "$devdummy" 2> /dev/null | grep -q "$dstmac"
1189	check_err $?
1190	ip neigh del $dstip6 lladdr $dstmac dev "$devdummy"  > /dev/null
1191	check_err $?
1192
1193	# ipv6 proxy
1194	ip neigh add proxy $dstip6 dev "$devdummy" > /dev/null
1195	check_err $?
1196	ip neigh get proxy $dstip6 dev "$devdummy" 2>/dev/null | grep -q "$dstip6"
1197	check_err $?
1198	ip neigh del proxy $dstip6 dev "$devdummy" > /dev/null
1199	check_err $?
1200
1201	if [ $ret -ne 0 ];then
1202		echo "FAIL: neigh get"
1203		return 1
1204	fi
1205
1206	echo "PASS: neigh get"
1207}
1208
1209kci_test_bridge_parent_id()
1210{
1211	local ret=0
1212	sysfsnet=/sys/bus/netdevsim/devices/netdevsim
1213	probed=false
1214
1215	if [ ! -w /sys/bus/netdevsim/new_device ] ; then
1216		modprobe -q netdevsim
1217		check_err $?
1218		if [ $ret -ne 0 ]; then
1219			echo "SKIP: bridge_parent_id can't load netdevsim"
1220			return $ksft_skip
1221		fi
1222		probed=true
1223	fi
1224
1225	echo "10 1" > /sys/bus/netdevsim/new_device
1226	while [ ! -d ${sysfsnet}10 ] ; do :; done
1227	echo "20 1" > /sys/bus/netdevsim/new_device
1228	while [ ! -d ${sysfsnet}20 ] ; do :; done
1229	udevadm settle
1230	dev10=`ls ${sysfsnet}10/net/`
1231	dev20=`ls ${sysfsnet}20/net/`
1232
1233	ip link add name test-bond0 type bond mode 802.3ad
1234	ip link set dev $dev10 master test-bond0
1235	ip link set dev $dev20 master test-bond0
1236	ip link add name test-br0 type bridge
1237	ip link set dev test-bond0 master test-br0
1238	check_err $?
1239
1240	# clean up any leftovers
1241	ip link del dev test-br0
1242	ip link del dev test-bond0
1243	echo 20 > /sys/bus/netdevsim/del_device
1244	echo 10 > /sys/bus/netdevsim/del_device
1245	$probed && rmmod netdevsim
1246
1247	if [ $ret -ne 0 ]; then
1248		echo "FAIL: bridge_parent_id"
1249		return 1
1250	fi
1251	echo "PASS: bridge_parent_id"
1252}
1253
1254address_get_proto()
1255{
1256	local addr=$1; shift
1257
1258	ip -N -j address show dev "$devdummy" |
1259	    jq -e -r --arg addr "${addr%/*}" \
1260	       '.[].addr_info[] | select(.local == $addr) | .protocol'
1261}
1262
1263address_count()
1264{
1265	ip -N -j address show dev "$devdummy" "$@" |
1266	    jq -e -r '[.[].addr_info[] | .local | select(. != null)] | length'
1267}
1268
1269do_test_address_proto()
1270{
1271	local what=$1; shift
1272	local addr=$1; shift
1273	local addr2=${addr%/*}2/${addr#*/}
1274	local addr3=${addr%/*}3/${addr#*/}
1275	local proto
1276	local count
1277	local ret=0
1278	local err
1279
1280	ip address add dev "$devdummy" "$addr3"
1281	check_err $?
1282	proto=$(address_get_proto "$addr3")
1283	[[ "$proto" == null ]]
1284	check_err $?
1285
1286	ip address add dev "$devdummy" "$addr2" proto 0x99
1287	check_err $?
1288	proto=$(address_get_proto "$addr2")
1289	[[ "$proto" == 0x99 ]]
1290	check_err $?
1291
1292	ip address add dev "$devdummy" "$addr" proto 0xab
1293	check_err $?
1294	proto=$(address_get_proto "$addr")
1295	[[ "$proto" == 0xab ]]
1296	check_err $?
1297
1298	ip address replace dev "$devdummy" "$addr" proto 0x11
1299	proto=$(address_get_proto "$addr")
1300	check_err $?
1301	[[ "$proto" == 0x11 ]]
1302	check_err $?
1303
1304	count=$(address_count)
1305	check_err $?
1306	(( count >= 3 )) # $addr, $addr2 and $addr3 plus any kernel addresses
1307	check_err $?
1308
1309	count=$(address_count proto 0)
1310	check_err $?
1311	(( count == 1 )) # just $addr3
1312	check_err $?
1313
1314	count=$(address_count proto 0x11)
1315	check_err $?
1316	(( count == 2 )) # $addr and $addr3
1317	check_err $?
1318
1319	count=$(address_count proto 0xab)
1320	check_err $?
1321	(( count == 1 )) # just $addr3
1322	check_err $?
1323
1324	ip address del dev "$devdummy" "$addr"
1325	ip address del dev "$devdummy" "$addr2"
1326	ip address del dev "$devdummy" "$addr3"
1327
1328	if [ $ret -ne 0 ]; then
1329		echo "FAIL: address proto $what"
1330		return 1
1331	fi
1332	echo "PASS: address proto $what"
1333}
1334
1335kci_test_address_proto()
1336{
1337	local ret=0
1338
1339	do_test_address_proto IPv4 192.0.2.1/28
1340	check_err $?
1341
1342	do_test_address_proto IPv6 2001:db8:1::1/64
1343	check_err $?
1344
1345	return $ret
1346}
1347
1348kci_test_rtnl()
1349{
1350	local current_test
1351	local ret=0
1352
1353	kci_add_dummy
1354	if [ $ret -ne 0 ];then
1355		echo "FAIL: cannot add dummy interface"
1356		return 1
1357	fi
1358
1359	for current_test in ${TESTS:-$ALL_TESTS}; do
1360		$current_test
1361		check_err $?
1362	done
1363
1364	kci_del_dummy
1365	return $ret
1366}
1367
1368usage()
1369{
1370	cat <<EOF
1371usage: ${0##*/} OPTS
1372
1373        -t <test>   Test(s) to run (default: all)
1374                    (options: $(echo $ALL_TESTS))
1375EOF
1376}
1377
1378#check for needed privileges
1379if [ "$(id -u)" -ne 0 ];then
1380	echo "SKIP: Need root privileges"
1381	exit $ksft_skip
1382fi
1383
1384for x in ip tc;do
1385	$x -Version 2>/dev/null >/dev/null
1386	if [ $? -ne 0 ];then
1387		echo "SKIP: Could not run test without the $x tool"
1388		exit $ksft_skip
1389	fi
1390done
1391
1392while getopts t:h o; do
1393	case $o in
1394		t) TESTS=$OPTARG;;
1395		h) usage; exit 0;;
1396		*) usage; exit 1;;
1397	esac
1398done
1399
1400kci_test_rtnl
1401
1402exit $?
1403