xref: /linux/tools/testing/selftests/net/rtnetlink.sh (revision eed4edda910fe34dfae8c6bfbcf57f4593a54295)
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_macsec_offload
25	kci_test_ipsec
26	kci_test_ipsec_offload
27	kci_test_fdb_get
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
562kci_test_macsec_offload()
563{
564	sysfsd=/sys/kernel/debug/netdevsim/netdevsim0/ports/0/
565	sysfsnet=/sys/bus/netdevsim/devices/netdevsim0/net/
566	probed=false
567	local ret=0
568	run_cmd_grep "^Usage: ip macsec" ip macsec help
569	if [ $? -ne 0 ]; then
570		end_test "SKIP: macsec: iproute2 too old"
571		return $ksft_skip
572	fi
573
574	if ! mount | grep -q debugfs; then
575		mount -t debugfs none /sys/kernel/debug/ &> /dev/null
576	fi
577
578	# setup netdevsim since dummydev doesn't have offload support
579	if [ ! -w /sys/bus/netdevsim/new_device ] ; then
580		run_cmd modprobe -q netdevsim
581
582		if [ $ret -ne 0 ]; then
583			end_test "SKIP: macsec_offload can't load netdevsim"
584			return $ksft_skip
585		fi
586		probed=true
587	fi
588
589	echo "0" > /sys/bus/netdevsim/new_device
590	while [ ! -d $sysfsnet ] ; do :; done
591	udevadm settle
592	dev=`ls $sysfsnet`
593
594	ip link set $dev up
595	if [ ! -d $sysfsd ] ; then
596		end_test "FAIL: macsec_offload can't create device $dev"
597		return 1
598	fi
599	run_cmd_grep 'macsec-hw-offload: on' ethtool -k $dev
600	if [ $? -eq 1 ] ; then
601		end_test "FAIL: macsec_offload netdevsim doesn't support MACsec offload"
602		return 1
603	fi
604	run_cmd ip link add link $dev kci_macsec1 type macsec port 4 offload mac
605	run_cmd ip link add link $dev kci_macsec2 type macsec address "aa:bb:cc:dd:ee:ff" port 5 offload mac
606	run_cmd ip link add link $dev kci_macsec3 type macsec sci abbacdde01020304 offload mac
607	run_cmd_fail ip link add link $dev kci_macsec4 type macsec port 8 offload mac
608
609	msname=kci_macsec1
610	run_cmd ip macsec add "$msname" tx sa 0 pn 1024 on key 01 12345678901234567890123456789012
611	run_cmd ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef"
612	run_cmd ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef" sa 0 pn 1 on \
613		key 00 0123456789abcdef0123456789abcdef
614	run_cmd_fail ip macsec add "$msname" rx port 1235 address "1c:ed:de:ad:be:ef"
615	# clean up any leftovers
616	for msdev in kci_macsec{1,2,3,4} ; do
617	    ip link del $msdev 2> /dev/null
618	done
619	echo 0 > /sys/bus/netdevsim/del_device
620	$probed && rmmod netdevsim
621
622	if [ $ret -ne 0 ]; then
623		end_test "FAIL: macsec_offload"
624		return 1
625	fi
626	end_test "PASS: macsec_offload"
627}
628
629#-------------------------------------------------------------------
630# Example commands
631#   ip x s add proto esp src 14.0.0.52 dst 14.0.0.70 \
632#            spi 0x07 mode transport reqid 0x07 replay-window 32 \
633#            aead 'rfc4106(gcm(aes))' 1234567890123456dcba 128 \
634#            sel src 14.0.0.52/24 dst 14.0.0.70/24
635#   ip x p add dir out src 14.0.0.52/24 dst 14.0.0.70/24 \
636#            tmpl proto esp src 14.0.0.52 dst 14.0.0.70 \
637#            spi 0x07 mode transport reqid 0x07
638#
639# Subcommands not tested
640#    ip x s update
641#    ip x s allocspi
642#    ip x s deleteall
643#    ip x p update
644#    ip x p deleteall
645#    ip x p set
646#-------------------------------------------------------------------
647kci_test_ipsec()
648{
649	local ret=0
650	algo="aead rfc4106(gcm(aes)) 0x3132333435363738393031323334353664636261 128"
651	srcip=192.168.123.1
652	dstip=192.168.123.2
653	spi=7
654
655	ip addr add $srcip dev $devdummy
656
657	# flush to be sure there's nothing configured
658	run_cmd ip x s flush ; ip x p flush
659
660	# start the monitor in the background
661	tmpfile=`mktemp /var/run/ipsectestXXX`
662	mpid=`(ip x m > $tmpfile & echo $!) 2>/dev/null`
663	sleep 0.2
664
665	ipsecid="proto esp src $srcip dst $dstip spi 0x07"
666	run_cmd ip x s add $ipsecid \
667            mode transport reqid 0x07 replay-window 32 \
668            $algo sel src $srcip/24 dst $dstip/24
669
670
671	lines=`ip x s list | grep $srcip | grep $dstip | wc -l`
672	run_cmd test $lines -eq 2
673	run_cmd_grep "SAD count 1" ip x s count
674
675	lines=`ip x s get $ipsecid | grep $srcip | grep $dstip | wc -l`
676	run_cmd test $lines -eq 2
677	run_cmd ip x s delete $ipsecid
678
679	lines=`ip x s list | wc -l`
680	run_cmd test $lines -eq 0
681
682	ipsecsel="dir out src $srcip/24 dst $dstip/24"
683	run_cmd ip x p add $ipsecsel \
684		    tmpl proto esp src $srcip dst $dstip \
685		    spi 0x07 mode transport reqid 0x07
686
687
688	lines=`ip x p list | grep $srcip | grep $dstip | wc -l`
689	run_cmd test $lines -eq 2
690
691	run_cmd_grep "SPD IN  0 OUT 1 FWD 0" ip x p count
692
693	lines=`ip x p get $ipsecsel | grep $srcip | grep $dstip | wc -l`
694	run_cmd test $lines -eq 2
695
696	run_cmd ip x p delete $ipsecsel
697
698	lines=`ip x p list | wc -l`
699	run_cmd test $lines -eq 0
700
701	# check the monitor results
702	kill $mpid
703	lines=`wc -l $tmpfile | cut "-d " -f1`
704	run_cmd test $lines -eq 20
705	rm -rf $tmpfile
706
707	# clean up any leftovers
708	run_cmd ip x s flush
709	run_cmd ip x p flush
710	ip addr del $srcip/32 dev $devdummy
711
712	if [ $ret -ne 0 ]; then
713		end_test "FAIL: ipsec"
714		return 1
715	fi
716	end_test "PASS: ipsec"
717}
718
719#-------------------------------------------------------------------
720# Example commands
721#   ip x s add proto esp src 14.0.0.52 dst 14.0.0.70 \
722#            spi 0x07 mode transport reqid 0x07 replay-window 32 \
723#            aead 'rfc4106(gcm(aes))' 1234567890123456dcba 128 \
724#            sel src 14.0.0.52/24 dst 14.0.0.70/24
725#            offload dev sim1 dir out
726#   ip x p add dir out src 14.0.0.52/24 dst 14.0.0.70/24 \
727#            tmpl proto esp src 14.0.0.52 dst 14.0.0.70 \
728#            spi 0x07 mode transport reqid 0x07
729#
730#-------------------------------------------------------------------
731kci_test_ipsec_offload()
732{
733	local ret=0
734	algo="aead rfc4106(gcm(aes)) 0x3132333435363738393031323334353664636261 128"
735	srcip=192.168.123.3
736	dstip=192.168.123.4
737	sysfsd=/sys/kernel/debug/netdevsim/netdevsim0/ports/0/
738	sysfsf=$sysfsd/ipsec
739	sysfsnet=/sys/bus/netdevsim/devices/netdevsim0/net/
740	probed=false
741
742	if ! mount | grep -q debugfs; then
743		mount -t debugfs none /sys/kernel/debug/ &> /dev/null
744	fi
745
746	# setup netdevsim since dummydev doesn't have offload support
747	if [ ! -w /sys/bus/netdevsim/new_device ] ; then
748		run_cmd modprobe -q netdevsim
749		if [ $ret -ne 0 ]; then
750			end_test "SKIP: ipsec_offload can't load netdevsim"
751			return $ksft_skip
752		fi
753		probed=true
754	fi
755
756	echo "0" > /sys/bus/netdevsim/new_device
757	while [ ! -d $sysfsnet ] ; do :; done
758	udevadm settle
759	dev=`ls $sysfsnet`
760
761	ip addr add $srcip dev $dev
762	ip link set $dev up
763	if [ ! -d $sysfsd ] ; then
764		end_test "FAIL: ipsec_offload can't create device $dev"
765		return 1
766	fi
767	if [ ! -f $sysfsf ] ; then
768		end_test "FAIL: ipsec_offload netdevsim doesn't support IPsec offload"
769		return 1
770	fi
771
772	# flush to be sure there's nothing configured
773	ip x s flush ; ip x p flush
774
775	# create offloaded SAs, both in and out
776	run_cmd ip x p add dir out src $srcip/24 dst $dstip/24 \
777	    tmpl proto esp src $srcip dst $dstip spi 9 \
778	    mode transport reqid 42
779
780	run_cmd ip x p add dir in src $dstip/24 dst $srcip/24 \
781	    tmpl proto esp src $dstip dst $srcip spi 9 \
782	    mode transport reqid 42
783
784	run_cmd ip x s add proto esp src $srcip dst $dstip spi 9 \
785	    mode transport reqid 42 $algo sel src $srcip/24 dst $dstip/24 \
786	    offload dev $dev dir out
787
788	run_cmd ip x s add proto esp src $dstip dst $srcip spi 9 \
789	    mode transport reqid 42 $algo sel src $dstip/24 dst $srcip/24 \
790	    offload dev $dev dir in
791
792	if [ $ret -ne 0 ]; then
793		end_test "FAIL: ipsec_offload can't create SA"
794		return 1
795	fi
796
797	# does offload show up in ip output
798	lines=`ip x s list | grep -c "crypto offload parameters: dev $dev dir"`
799	if [ $lines -ne 2 ] ; then
800		check_err 1
801		end_test "FAIL: ipsec_offload SA offload missing from list output"
802	fi
803
804	# use ping to exercise the Tx path
805	ping -I $dev -c 3 -W 1 -i 0 $dstip >/dev/null
806
807	# does driver have correct offload info
808	run_cmd diff $sysfsf - << EOF
809SA count=2 tx=3
810sa[0] tx ipaddr=0x00000000 00000000 00000000 00000000
811sa[0]    spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1
812sa[0]    key=0x34333231 38373635 32313039 36353433
813sa[1] rx ipaddr=0x00000000 00000000 00000000 037ba8c0
814sa[1]    spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1
815sa[1]    key=0x34333231 38373635 32313039 36353433
816EOF
817	if [ $? -ne 0 ] ; then
818		end_test "FAIL: ipsec_offload incorrect driver data"
819		check_err 1
820	fi
821
822	# does offload get removed from driver
823	ip x s flush
824	ip x p flush
825	lines=`grep -c "SA count=0" $sysfsf`
826	if [ $lines -ne 1 ] ; then
827		check_err 1
828		end_test "FAIL: ipsec_offload SA not removed from driver"
829	fi
830
831	# clean up any leftovers
832	echo 0 > /sys/bus/netdevsim/del_device
833	$probed && rmmod netdevsim
834
835	if [ $ret -ne 0 ]; then
836		end_test "FAIL: ipsec_offload"
837		return 1
838	fi
839	end_test "PASS: ipsec_offload"
840}
841
842kci_test_gretap()
843{
844	DEV_NS=gretap00
845	local ret=0
846
847	setup_ns testns
848	if [ $? -ne 0 ]; then
849		end_test "SKIP gretap tests: cannot add net namespace $testns"
850		return $ksft_skip
851	fi
852
853	run_cmd_grep "^Usage:" ip link help gretap
854	if [ $? -ne 0 ];then
855		end_test "SKIP: gretap: iproute2 too old"
856		ip netns del "$testns"
857		return $ksft_skip
858	fi
859
860	# test native tunnel
861	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type gretap seq \
862		key 102 local 172.16.1.100 remote 172.16.1.200
863
864
865	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
866	run_cmd ip -netns "$testns" link set dev $DEV_NS up
867	run_cmd ip -netns "$testns" link del "$DEV_NS"
868
869	# test external mode
870	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type gretap external
871	run_cmd ip -netns "$testns" link del "$DEV_NS"
872
873	if [ $ret -ne 0 ]; then
874		end_test "FAIL: gretap"
875		ip netns del "$testns"
876		return 1
877	fi
878	end_test "PASS: gretap"
879
880	ip netns del "$testns"
881}
882
883kci_test_ip6gretap()
884{
885	DEV_NS=ip6gretap00
886	local ret=0
887
888	setup_ns testns
889	if [ $? -ne 0 ]; then
890		end_test "SKIP ip6gretap tests: cannot add net namespace $testns"
891		return $ksft_skip
892	fi
893
894	run_cmd_grep "^Usage:" ip link help ip6gretap
895	if [ $? -ne 0 ];then
896		end_test "SKIP: ip6gretap: iproute2 too old"
897		ip netns del "$testns"
898		return $ksft_skip
899	fi
900
901	# test native tunnel
902	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap seq \
903		key 102 local fc00:100::1 remote fc00:100::2
904
905
906	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" fc00:200::1/96
907	run_cmd ip -netns "$testns" link set dev $DEV_NS up
908	run_cmd ip -netns "$testns" link del "$DEV_NS"
909
910	# test external mode
911	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap external
912	run_cmd ip -netns "$testns" link del "$DEV_NS"
913
914	if [ $ret -ne 0 ]; then
915		end_test "FAIL: ip6gretap"
916		ip netns del "$testns"
917		return 1
918	fi
919	end_test "PASS: ip6gretap"
920
921	ip netns del "$testns"
922}
923
924kci_test_erspan()
925{
926	DEV_NS=erspan00
927	local ret=0
928	run_cmd_grep "^Usage:" ip link help erspan
929	if [ $? -ne 0 ];then
930		end_test "SKIP: erspan: iproute2 too old"
931		return $ksft_skip
932	fi
933	setup_ns testns
934	if [ $? -ne 0 ]; then
935		end_test "SKIP erspan tests: cannot add net namespace $testns"
936		return $ksft_skip
937	fi
938
939	# test native tunnel erspan v1
940	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \
941		key 102 local 172.16.1.100 remote 172.16.1.200 \
942		erspan_ver 1 erspan 488
943
944
945	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
946	run_cmd ip -netns "$testns" link set dev $DEV_NS up
947	run_cmd ip -netns "$testns" link del "$DEV_NS"
948
949	# test native tunnel erspan v2
950	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \
951		key 102 local 172.16.1.100 remote 172.16.1.200 \
952		erspan_ver 2 erspan_dir ingress erspan_hwid 7
953
954
955	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
956	run_cmd ip -netns "$testns" link set dev $DEV_NS up
957	run_cmd ip -netns "$testns" link del "$DEV_NS"
958
959	# test external mode
960	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan external
961	run_cmd ip -netns "$testns" link del "$DEV_NS"
962
963	if [ $ret -ne 0 ]; then
964		end_test "FAIL: erspan"
965		ip netns del "$testns"
966		return 1
967	fi
968	end_test "PASS: erspan"
969
970	ip netns del "$testns"
971}
972
973kci_test_ip6erspan()
974{
975	DEV_NS=ip6erspan00
976	local ret=0
977	run_cmd_grep "^Usage:" ip link help ip6erspan
978	if [ $? -ne 0 ];then
979		end_test "SKIP: ip6erspan: iproute2 too old"
980		return $ksft_skip
981	fi
982	setup_ns testns
983	if [ $? -ne 0 ]; then
984		end_test "SKIP ip6erspan tests: cannot add net namespace $testns"
985		return $ksft_skip
986	fi
987
988	# test native tunnel ip6erspan v1
989	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \
990		key 102 local fc00:100::1 remote fc00:100::2 \
991		erspan_ver 1 erspan 488
992
993
994	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
995	run_cmd ip -netns "$testns" link set dev $DEV_NS up
996	run_cmd ip -netns "$testns" link del "$DEV_NS"
997
998	# test native tunnel ip6erspan v2
999	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \
1000		key 102 local fc00:100::1 remote fc00:100::2 \
1001		erspan_ver 2 erspan_dir ingress erspan_hwid 7
1002
1003
1004	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
1005	run_cmd ip -netns "$testns" link set dev $DEV_NS up
1006	run_cmd ip -netns "$testns" link del "$DEV_NS"
1007
1008	# test external mode
1009	run_cmd ip -netns "$testns" link add dev "$DEV_NS" \
1010		type ip6erspan external
1011
1012	run_cmd ip -netns "$testns" link del "$DEV_NS"
1013
1014	if [ $ret -ne 0 ]; then
1015		end_test "FAIL: ip6erspan"
1016		ip netns del "$testns"
1017		return 1
1018	fi
1019	end_test "PASS: ip6erspan"
1020
1021	ip netns del "$testns"
1022}
1023
1024kci_test_fdb_get()
1025{
1026	brdev="test-br0"
1027	vxlandev="vxlan10"
1028	test_mac=de:ad:be:ef:13:37
1029	localip="10.0.2.2"
1030	dstip="10.0.2.3"
1031	local ret=0
1032
1033	run_cmd_grep 'bridge fdb get' bridge fdb help
1034	if [ $? -ne 0 ];then
1035		end_test "SKIP: fdb get tests: iproute2 too old"
1036		return $ksft_skip
1037	fi
1038
1039	setup_ns testns
1040	if [ $? -ne 0 ]; then
1041		end_test "SKIP fdb get tests: cannot add net namespace $testns"
1042		return $ksft_skip
1043	fi
1044	IP="ip -netns $testns"
1045	BRIDGE="bridge -netns $testns"
1046	run_cmd $IP link add "$vxlandev" type vxlan id 10 local $localip \
1047                dstport 4789
1048	run_cmd $IP link add name "$brdev" type bridge
1049	run_cmd $IP link set dev "$vxlandev" master "$brdev"
1050	run_cmd $BRIDGE fdb add $test_mac dev "$vxlandev" master
1051	run_cmd $BRIDGE fdb add $test_mac dev "$vxlandev" dst $dstip self
1052	run_cmd_grep "dev $vxlandev master $brdev" $BRIDGE fdb get $test_mac brport "$vxlandev"
1053	run_cmd_grep "dev $vxlandev master $brdev" $BRIDGE fdb get $test_mac br "$brdev"
1054	run_cmd_grep "dev $vxlandev dst $dstip" $BRIDGE fdb get $test_mac dev "$vxlandev" self
1055
1056	ip netns del $testns &>/dev/null
1057
1058	if [ $ret -ne 0 ]; then
1059		end_test "FAIL: bridge fdb get"
1060		return 1
1061	fi
1062
1063	end_test "PASS: bridge fdb get"
1064}
1065
1066kci_test_neigh_get()
1067{
1068	dstmac=de:ad:be:ef:13:37
1069	dstip=10.0.2.4
1070	dstip6=dead::2
1071	local ret=0
1072
1073	run_cmd_grep 'ip neigh get' ip neigh help
1074	if [ $? -ne 0 ];then
1075		end_test "SKIP: fdb get tests: iproute2 too old"
1076		return $ksft_skip
1077	fi
1078
1079	# ipv4
1080	run_cmd ip neigh add $dstip lladdr $dstmac dev "$devdummy"
1081	run_cmd_grep "$dstmac" ip neigh get $dstip dev "$devdummy"
1082	run_cmd ip neigh del $dstip lladdr $dstmac dev "$devdummy"
1083
1084	# ipv4 proxy
1085	run_cmd ip neigh add proxy $dstip dev "$devdummy"
1086	run_cmd_grep "$dstip" ip neigh get proxy $dstip dev "$devdummy"
1087	run_cmd ip neigh del proxy $dstip dev "$devdummy"
1088
1089	# ipv6
1090	run_cmd ip neigh add $dstip6 lladdr $dstmac dev "$devdummy"
1091	run_cmd_grep "$dstmac" ip neigh get $dstip6 dev "$devdummy"
1092	run_cmd ip neigh del $dstip6 lladdr $dstmac dev "$devdummy"
1093
1094	# ipv6 proxy
1095	run_cmd ip neigh add proxy $dstip6 dev "$devdummy"
1096	run_cmd_grep "$dstip6" ip neigh get proxy $dstip6 dev "$devdummy"
1097	run_cmd ip neigh del proxy $dstip6 dev "$devdummy"
1098
1099	if [ $ret -ne 0 ];then
1100		end_test "FAIL: neigh get"
1101		return 1
1102	fi
1103
1104	end_test "PASS: neigh get"
1105}
1106
1107kci_test_bridge_parent_id()
1108{
1109	local ret=0
1110	sysfsnet=/sys/bus/netdevsim/devices/netdevsim
1111	probed=false
1112
1113	if [ ! -w /sys/bus/netdevsim/new_device ] ; then
1114		run_cmd modprobe -q netdevsim
1115		if [ $ret -ne 0 ]; then
1116			end_test "SKIP: bridge_parent_id can't load netdevsim"
1117			return $ksft_skip
1118		fi
1119		probed=true
1120	fi
1121
1122	echo "10 1" > /sys/bus/netdevsim/new_device
1123	while [ ! -d ${sysfsnet}10 ] ; do :; done
1124	echo "20 1" > /sys/bus/netdevsim/new_device
1125	while [ ! -d ${sysfsnet}20 ] ; do :; done
1126	udevadm settle
1127	dev10=`ls ${sysfsnet}10/net/`
1128	dev20=`ls ${sysfsnet}20/net/`
1129	run_cmd ip link add name test-bond0 type bond mode 802.3ad
1130	run_cmd ip link set dev $dev10 master test-bond0
1131	run_cmd ip link set dev $dev20 master test-bond0
1132	run_cmd ip link add name test-br0 type bridge
1133	run_cmd ip link set dev test-bond0 master test-br0
1134
1135	# clean up any leftovers
1136	ip link del dev test-br0
1137	ip link del dev test-bond0
1138	echo 20 > /sys/bus/netdevsim/del_device
1139	echo 10 > /sys/bus/netdevsim/del_device
1140	$probed && rmmod netdevsim
1141
1142	if [ $ret -ne 0 ]; then
1143		end_test "FAIL: bridge_parent_id"
1144		return 1
1145	fi
1146	end_test "PASS: bridge_parent_id"
1147}
1148
1149address_get_proto()
1150{
1151	local addr=$1; shift
1152
1153	ip -N -j address show dev "$devdummy" |
1154	    jq -e -r --arg addr "${addr%/*}" \
1155	       '.[].addr_info[] | select(.local == $addr) | .protocol'
1156}
1157
1158address_count()
1159{
1160	ip -N -j address show dev "$devdummy" "$@" |
1161	    jq -e -r '[.[].addr_info[] | .local | select(. != null)] | length'
1162}
1163
1164do_test_address_proto()
1165{
1166	local what=$1; shift
1167	local addr=$1; shift
1168	local addr2=${addr%/*}2/${addr#*/}
1169	local addr3=${addr%/*}3/${addr#*/}
1170	local proto
1171	local count
1172	local ret=0
1173	local err
1174
1175	ip address add dev "$devdummy" "$addr3"
1176	check_err $?
1177	proto=$(address_get_proto "$addr3")
1178	[[ "$proto" == null ]]
1179	check_err $?
1180
1181	ip address add dev "$devdummy" "$addr2" proto 0x99
1182	check_err $?
1183	proto=$(address_get_proto "$addr2")
1184	[[ "$proto" == 0x99 ]]
1185	check_err $?
1186
1187	ip address add dev "$devdummy" "$addr" proto 0xab
1188	check_err $?
1189	proto=$(address_get_proto "$addr")
1190	[[ "$proto" == 0xab ]]
1191	check_err $?
1192
1193	ip address replace dev "$devdummy" "$addr" proto 0x11
1194	proto=$(address_get_proto "$addr")
1195	check_err $?
1196	[[ "$proto" == 0x11 ]]
1197	check_err $?
1198
1199	count=$(address_count)
1200	check_err $?
1201	(( count >= 3 )) # $addr, $addr2 and $addr3 plus any kernel addresses
1202	check_err $?
1203
1204	count=$(address_count proto 0)
1205	check_err $?
1206	(( count == 1 )) # just $addr3
1207	check_err $?
1208
1209	count=$(address_count proto 0x11)
1210	check_err $?
1211	(( count == 2 )) # $addr and $addr3
1212	check_err $?
1213
1214	count=$(address_count proto 0xab)
1215	check_err $?
1216	(( count == 1 )) # just $addr3
1217	check_err $?
1218
1219	ip address del dev "$devdummy" "$addr"
1220	ip address del dev "$devdummy" "$addr2"
1221	ip address del dev "$devdummy" "$addr3"
1222
1223	if [ $ret -ne 0 ]; then
1224		end_test "FAIL: address proto $what"
1225		return 1
1226	fi
1227	end_test "PASS: address proto $what"
1228}
1229
1230kci_test_address_proto()
1231{
1232	local ret=0
1233
1234	do_test_address_proto IPv4 192.0.2.1/28
1235	check_err $?
1236
1237	do_test_address_proto IPv6 2001:db8:1::1/64
1238	check_err $?
1239
1240	return $ret
1241}
1242
1243kci_test_enslave_bonding()
1244{
1245	local bond="bond123"
1246	local ret=0
1247
1248	setup_ns testns
1249	if [ $? -ne 0 ]; then
1250		end_test "SKIP bonding tests: cannot add net namespace $testns"
1251		return $ksft_skip
1252	fi
1253
1254	run_cmd ip -netns $testns link add dev $bond type bond mode balance-rr
1255	run_cmd ip -netns $testns link add dev $devdummy type dummy
1256	run_cmd ip -netns $testns link set dev $devdummy up
1257	run_cmd ip -netns $testns link set dev $devdummy master $bond down
1258	if [ $ret -ne 0 ]; then
1259		end_test "FAIL: initially up interface added to a bond and set down"
1260		ip netns del "$testns"
1261		return 1
1262	fi
1263
1264	end_test "PASS: enslave interface in a bond"
1265	ip netns del "$testns"
1266}
1267
1268kci_test_rtnl()
1269{
1270	local current_test
1271	local ret=0
1272
1273	kci_add_dummy
1274	if [ $ret -ne 0 ];then
1275		end_test "FAIL: cannot add dummy interface"
1276		return 1
1277	fi
1278
1279	for current_test in ${TESTS:-$ALL_TESTS}; do
1280		$current_test
1281		check_err $?
1282	done
1283
1284	kci_del_dummy
1285	return $ret
1286}
1287
1288usage()
1289{
1290	cat <<EOF
1291usage: ${0##*/} OPTS
1292
1293        -t <test>   Test(s) to run (default: all)
1294                    (options: $(echo $ALL_TESTS))
1295        -v          Verbose mode (show commands and output)
1296        -P          Pause after every test
1297        -p          Pause after every failing test before cleanup (for debugging)
1298EOF
1299}
1300
1301#check for needed privileges
1302if [ "$(id -u)" -ne 0 ];then
1303	end_test "SKIP: Need root privileges"
1304	exit $ksft_skip
1305fi
1306
1307for x in ip tc;do
1308	$x -Version 2>/dev/null >/dev/null
1309	if [ $? -ne 0 ];then
1310		end_test "SKIP: Could not run test without the $x tool"
1311		exit $ksft_skip
1312	fi
1313done
1314
1315while getopts t:hvpP o; do
1316	case $o in
1317		t) TESTS=$OPTARG;;
1318		v) VERBOSE=1;;
1319		p) PAUSE_ON_FAIL=yes;;
1320		P) PAUSE=yes;;
1321		h) usage; exit 0;;
1322		*) usage; exit 1;;
1323	esac
1324done
1325
1326[ $PAUSE = "yes" ] && PAUSE_ON_FAIL="no"
1327
1328kci_test_rtnl
1329
1330exit $?
1331