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