xref: /linux/tools/testing/selftests/net/rtnetlink.sh (revision 6f47c7ae8c7afaf9ad291d39f0d3974f191a7946)
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	# we didn't create a peer, make sure we can Tx
805	ip neigh add $dstip dev $dev lladdr 00:11:22:33:44:55
806	# use ping to exercise the Tx path
807	ping -I $dev -c 3 -W 1 -i 0 $dstip >/dev/null
808
809	# does driver have correct offload info
810	run_cmd diff $sysfsf - << EOF
811SA count=2 tx=3
812sa[0] tx ipaddr=0x00000000 00000000 00000000 00000000
813sa[0]    spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1
814sa[0]    key=0x34333231 38373635 32313039 36353433
815sa[1] rx ipaddr=0x00000000 00000000 00000000 037ba8c0
816sa[1]    spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1
817sa[1]    key=0x34333231 38373635 32313039 36353433
818EOF
819	if [ $? -ne 0 ] ; then
820		end_test "FAIL: ipsec_offload incorrect driver data"
821		check_err 1
822	fi
823
824	# does offload get removed from driver
825	ip x s flush
826	ip x p flush
827	lines=`grep -c "SA count=0" $sysfsf`
828	if [ $lines -ne 1 ] ; then
829		check_err 1
830		end_test "FAIL: ipsec_offload SA not removed from driver"
831	fi
832
833	# clean up any leftovers
834	echo 0 > /sys/bus/netdevsim/del_device
835	$probed && rmmod netdevsim
836
837	if [ $ret -ne 0 ]; then
838		end_test "FAIL: ipsec_offload"
839		return 1
840	fi
841	end_test "PASS: ipsec_offload"
842}
843
844kci_test_gretap()
845{
846	DEV_NS=gretap00
847	local ret=0
848
849	setup_ns testns
850	if [ $? -ne 0 ]; then
851		end_test "SKIP gretap tests: cannot add net namespace $testns"
852		return $ksft_skip
853	fi
854
855	run_cmd_grep "^Usage:" ip link help gretap
856	if [ $? -ne 0 ];then
857		end_test "SKIP: gretap: iproute2 too old"
858		ip netns del "$testns"
859		return $ksft_skip
860	fi
861
862	# test native tunnel
863	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type gretap seq \
864		key 102 local 172.16.1.100 remote 172.16.1.200
865
866
867	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
868	run_cmd ip -netns "$testns" link set dev $DEV_NS up
869	run_cmd ip -netns "$testns" link del "$DEV_NS"
870
871	# test external mode
872	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type gretap external
873	run_cmd ip -netns "$testns" link del "$DEV_NS"
874
875	if [ $ret -ne 0 ]; then
876		end_test "FAIL: gretap"
877		ip netns del "$testns"
878		return 1
879	fi
880	end_test "PASS: gretap"
881
882	ip netns del "$testns"
883}
884
885kci_test_ip6gretap()
886{
887	DEV_NS=ip6gretap00
888	local ret=0
889
890	setup_ns testns
891	if [ $? -ne 0 ]; then
892		end_test "SKIP ip6gretap tests: cannot add net namespace $testns"
893		return $ksft_skip
894	fi
895
896	run_cmd_grep "^Usage:" ip link help ip6gretap
897	if [ $? -ne 0 ];then
898		end_test "SKIP: ip6gretap: iproute2 too old"
899		ip netns del "$testns"
900		return $ksft_skip
901	fi
902
903	# test native tunnel
904	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap seq \
905		key 102 local fc00:100::1 remote fc00:100::2
906
907
908	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" fc00:200::1/96
909	run_cmd ip -netns "$testns" link set dev $DEV_NS up
910	run_cmd ip -netns "$testns" link del "$DEV_NS"
911
912	# test external mode
913	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap external
914	run_cmd ip -netns "$testns" link del "$DEV_NS"
915
916	if [ $ret -ne 0 ]; then
917		end_test "FAIL: ip6gretap"
918		ip netns del "$testns"
919		return 1
920	fi
921	end_test "PASS: ip6gretap"
922
923	ip netns del "$testns"
924}
925
926kci_test_erspan()
927{
928	DEV_NS=erspan00
929	local ret=0
930	run_cmd_grep "^Usage:" ip link help erspan
931	if [ $? -ne 0 ];then
932		end_test "SKIP: erspan: iproute2 too old"
933		return $ksft_skip
934	fi
935	setup_ns testns
936	if [ $? -ne 0 ]; then
937		end_test "SKIP erspan tests: cannot add net namespace $testns"
938		return $ksft_skip
939	fi
940
941	# test native tunnel erspan v1
942	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \
943		key 102 local 172.16.1.100 remote 172.16.1.200 \
944		erspan_ver 1 erspan 488
945
946
947	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
948	run_cmd ip -netns "$testns" link set dev $DEV_NS up
949	run_cmd ip -netns "$testns" link del "$DEV_NS"
950
951	# test native tunnel erspan v2
952	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \
953		key 102 local 172.16.1.100 remote 172.16.1.200 \
954		erspan_ver 2 erspan_dir ingress erspan_hwid 7
955
956
957	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
958	run_cmd ip -netns "$testns" link set dev $DEV_NS up
959	run_cmd ip -netns "$testns" link del "$DEV_NS"
960
961	# test external mode
962	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan external
963	run_cmd ip -netns "$testns" link del "$DEV_NS"
964
965	if [ $ret -ne 0 ]; then
966		end_test "FAIL: erspan"
967		ip netns del "$testns"
968		return 1
969	fi
970	end_test "PASS: erspan"
971
972	ip netns del "$testns"
973}
974
975kci_test_ip6erspan()
976{
977	DEV_NS=ip6erspan00
978	local ret=0
979	run_cmd_grep "^Usage:" ip link help ip6erspan
980	if [ $? -ne 0 ];then
981		end_test "SKIP: ip6erspan: iproute2 too old"
982		return $ksft_skip
983	fi
984	setup_ns testns
985	if [ $? -ne 0 ]; then
986		end_test "SKIP ip6erspan tests: cannot add net namespace $testns"
987		return $ksft_skip
988	fi
989
990	# test native tunnel ip6erspan v1
991	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \
992		key 102 local fc00:100::1 remote fc00:100::2 \
993		erspan_ver 1 erspan 488
994
995
996	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
997	run_cmd ip -netns "$testns" link set dev $DEV_NS up
998	run_cmd ip -netns "$testns" link del "$DEV_NS"
999
1000	# test native tunnel ip6erspan v2
1001	run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \
1002		key 102 local fc00:100::1 remote fc00:100::2 \
1003		erspan_ver 2 erspan_dir ingress erspan_hwid 7
1004
1005
1006	run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24
1007	run_cmd ip -netns "$testns" link set dev $DEV_NS up
1008	run_cmd ip -netns "$testns" link del "$DEV_NS"
1009
1010	# test external mode
1011	run_cmd ip -netns "$testns" link add dev "$DEV_NS" \
1012		type ip6erspan external
1013
1014	run_cmd ip -netns "$testns" link del "$DEV_NS"
1015
1016	if [ $ret -ne 0 ]; then
1017		end_test "FAIL: ip6erspan"
1018		ip netns del "$testns"
1019		return 1
1020	fi
1021	end_test "PASS: ip6erspan"
1022
1023	ip netns del "$testns"
1024}
1025
1026kci_test_fdb_get()
1027{
1028	brdev="test-br0"
1029	vxlandev="vxlan10"
1030	test_mac=de:ad:be:ef:13:37
1031	localip="10.0.2.2"
1032	dstip="10.0.2.3"
1033	local ret=0
1034
1035	run_cmd_grep 'bridge fdb get' bridge fdb help
1036	if [ $? -ne 0 ];then
1037		end_test "SKIP: fdb get tests: iproute2 too old"
1038		return $ksft_skip
1039	fi
1040
1041	setup_ns testns
1042	if [ $? -ne 0 ]; then
1043		end_test "SKIP fdb get tests: cannot add net namespace $testns"
1044		return $ksft_skip
1045	fi
1046	IP="ip -netns $testns"
1047	BRIDGE="bridge -netns $testns"
1048	run_cmd $IP link add "$vxlandev" type vxlan id 10 local $localip \
1049                dstport 4789
1050	run_cmd $IP link add name "$brdev" type bridge
1051	run_cmd $IP link set dev "$vxlandev" master "$brdev"
1052	run_cmd $BRIDGE fdb add $test_mac dev "$vxlandev" master
1053	run_cmd $BRIDGE fdb add $test_mac dev "$vxlandev" dst $dstip self
1054	run_cmd_grep "dev $vxlandev master $brdev" $BRIDGE fdb get $test_mac brport "$vxlandev"
1055	run_cmd_grep "dev $vxlandev master $brdev" $BRIDGE fdb get $test_mac br "$brdev"
1056	run_cmd_grep "dev $vxlandev dst $dstip" $BRIDGE fdb get $test_mac dev "$vxlandev" self
1057
1058	ip netns del $testns &>/dev/null
1059
1060	if [ $ret -ne 0 ]; then
1061		end_test "FAIL: bridge fdb get"
1062		return 1
1063	fi
1064
1065	end_test "PASS: bridge fdb get"
1066}
1067
1068kci_test_neigh_get()
1069{
1070	dstmac=de:ad:be:ef:13:37
1071	dstip=10.0.2.4
1072	dstip6=dead::2
1073	local ret=0
1074
1075	run_cmd_grep 'ip neigh get' ip neigh help
1076	if [ $? -ne 0 ];then
1077		end_test "SKIP: fdb get tests: iproute2 too old"
1078		return $ksft_skip
1079	fi
1080
1081	# ipv4
1082	run_cmd ip neigh add $dstip lladdr $dstmac dev "$devdummy"
1083	run_cmd_grep "$dstmac" ip neigh get $dstip dev "$devdummy"
1084	run_cmd ip neigh del $dstip lladdr $dstmac dev "$devdummy"
1085
1086	# ipv4 proxy
1087	run_cmd ip neigh add proxy $dstip dev "$devdummy"
1088	run_cmd_grep "$dstip" ip neigh get proxy $dstip dev "$devdummy"
1089	run_cmd ip neigh del proxy $dstip dev "$devdummy"
1090
1091	# ipv6
1092	run_cmd ip neigh add $dstip6 lladdr $dstmac dev "$devdummy"
1093	run_cmd_grep "$dstmac" ip neigh get $dstip6 dev "$devdummy"
1094	run_cmd ip neigh del $dstip6 lladdr $dstmac dev "$devdummy"
1095
1096	# ipv6 proxy
1097	run_cmd ip neigh add proxy $dstip6 dev "$devdummy"
1098	run_cmd_grep "$dstip6" ip neigh get proxy $dstip6 dev "$devdummy"
1099	run_cmd ip neigh del proxy $dstip6 dev "$devdummy"
1100
1101	if [ $ret -ne 0 ];then
1102		end_test "FAIL: neigh get"
1103		return 1
1104	fi
1105
1106	end_test "PASS: neigh get"
1107}
1108
1109kci_test_bridge_parent_id()
1110{
1111	local ret=0
1112	sysfsnet=/sys/bus/netdevsim/devices/netdevsim
1113	probed=false
1114
1115	if [ ! -w /sys/bus/netdevsim/new_device ] ; then
1116		run_cmd modprobe -q netdevsim
1117		if [ $ret -ne 0 ]; then
1118			end_test "SKIP: bridge_parent_id can't load netdevsim"
1119			return $ksft_skip
1120		fi
1121		probed=true
1122	fi
1123
1124	echo "10 1" > /sys/bus/netdevsim/new_device
1125	while [ ! -d ${sysfsnet}10 ] ; do :; done
1126	echo "20 1" > /sys/bus/netdevsim/new_device
1127	while [ ! -d ${sysfsnet}20 ] ; do :; done
1128	udevadm settle
1129	dev10=`ls ${sysfsnet}10/net/`
1130	dev20=`ls ${sysfsnet}20/net/`
1131	run_cmd ip link add name test-bond0 type bond mode 802.3ad
1132	run_cmd ip link set dev $dev10 master test-bond0
1133	run_cmd ip link set dev $dev20 master test-bond0
1134	run_cmd ip link add name test-br0 type bridge
1135	run_cmd ip link set dev test-bond0 master test-br0
1136
1137	# clean up any leftovers
1138	ip link del dev test-br0
1139	ip link del dev test-bond0
1140	echo 20 > /sys/bus/netdevsim/del_device
1141	echo 10 > /sys/bus/netdevsim/del_device
1142	$probed && rmmod netdevsim
1143
1144	if [ $ret -ne 0 ]; then
1145		end_test "FAIL: bridge_parent_id"
1146		return 1
1147	fi
1148	end_test "PASS: bridge_parent_id"
1149}
1150
1151address_get_proto()
1152{
1153	local addr=$1; shift
1154
1155	ip -N -j address show dev "$devdummy" |
1156	    jq -e -r --arg addr "${addr%/*}" \
1157	       '.[].addr_info[] | select(.local == $addr) | .protocol'
1158}
1159
1160address_count()
1161{
1162	ip -N -j address show dev "$devdummy" "$@" |
1163	    jq -e -r '[.[].addr_info[] | .local | select(. != null)] | length'
1164}
1165
1166do_test_address_proto()
1167{
1168	local what=$1; shift
1169	local addr=$1; shift
1170	local addr2=${addr%/*}2/${addr#*/}
1171	local addr3=${addr%/*}3/${addr#*/}
1172	local proto
1173	local count
1174	local ret=0
1175	local err
1176
1177	ip address add dev "$devdummy" "$addr3"
1178	check_err $?
1179	proto=$(address_get_proto "$addr3")
1180	[[ "$proto" == null ]]
1181	check_err $?
1182
1183	ip address add dev "$devdummy" "$addr2" proto 0x99
1184	check_err $?
1185	proto=$(address_get_proto "$addr2")
1186	[[ "$proto" == 0x99 ]]
1187	check_err $?
1188
1189	ip address add dev "$devdummy" "$addr" proto 0xab
1190	check_err $?
1191	proto=$(address_get_proto "$addr")
1192	[[ "$proto" == 0xab ]]
1193	check_err $?
1194
1195	ip address replace dev "$devdummy" "$addr" proto 0x11
1196	proto=$(address_get_proto "$addr")
1197	check_err $?
1198	[[ "$proto" == 0x11 ]]
1199	check_err $?
1200
1201	count=$(address_count)
1202	check_err $?
1203	(( count >= 3 )) # $addr, $addr2 and $addr3 plus any kernel addresses
1204	check_err $?
1205
1206	count=$(address_count proto 0)
1207	check_err $?
1208	(( count == 1 )) # just $addr3
1209	check_err $?
1210
1211	count=$(address_count proto 0x11)
1212	check_err $?
1213	(( count == 2 )) # $addr and $addr3
1214	check_err $?
1215
1216	count=$(address_count proto 0xab)
1217	check_err $?
1218	(( count == 1 )) # just $addr3
1219	check_err $?
1220
1221	ip address del dev "$devdummy" "$addr"
1222	ip address del dev "$devdummy" "$addr2"
1223	ip address del dev "$devdummy" "$addr3"
1224
1225	if [ $ret -ne 0 ]; then
1226		end_test "FAIL: address proto $what"
1227		return 1
1228	fi
1229	end_test "PASS: address proto $what"
1230}
1231
1232kci_test_address_proto()
1233{
1234	local ret=0
1235
1236	do_test_address_proto IPv4 192.0.2.1/28
1237	check_err $?
1238
1239	do_test_address_proto IPv6 2001:db8:1::1/64
1240	check_err $?
1241
1242	return $ret
1243}
1244
1245kci_test_enslave_bonding()
1246{
1247	local bond="bond123"
1248	local ret=0
1249
1250	setup_ns testns
1251	if [ $? -ne 0 ]; then
1252		end_test "SKIP bonding tests: cannot add net namespace $testns"
1253		return $ksft_skip
1254	fi
1255
1256	run_cmd ip -netns $testns link add dev $bond type bond mode balance-rr
1257	run_cmd ip -netns $testns link add dev $devdummy type dummy
1258	run_cmd ip -netns $testns link set dev $devdummy up
1259	run_cmd ip -netns $testns link set dev $devdummy master $bond down
1260	if [ $ret -ne 0 ]; then
1261		end_test "FAIL: initially up interface added to a bond and set down"
1262		ip netns del "$testns"
1263		return 1
1264	fi
1265
1266	end_test "PASS: enslave interface in a bond"
1267	ip netns del "$testns"
1268}
1269
1270kci_test_rtnl()
1271{
1272	local current_test
1273	local ret=0
1274
1275	kci_add_dummy
1276	if [ $ret -ne 0 ];then
1277		end_test "FAIL: cannot add dummy interface"
1278		return 1
1279	fi
1280
1281	for current_test in ${TESTS:-$ALL_TESTS}; do
1282		$current_test
1283		check_err $?
1284	done
1285
1286	kci_del_dummy
1287	return $ret
1288}
1289
1290usage()
1291{
1292	cat <<EOF
1293usage: ${0##*/} OPTS
1294
1295        -t <test>   Test(s) to run (default: all)
1296                    (options: $(echo $ALL_TESTS))
1297        -v          Verbose mode (show commands and output)
1298        -P          Pause after every test
1299        -p          Pause after every failing test before cleanup (for debugging)
1300EOF
1301}
1302
1303#check for needed privileges
1304if [ "$(id -u)" -ne 0 ];then
1305	end_test "SKIP: Need root privileges"
1306	exit $ksft_skip
1307fi
1308
1309for x in ip tc;do
1310	$x -Version 2>/dev/null >/dev/null
1311	if [ $? -ne 0 ];then
1312		end_test "SKIP: Could not run test without the $x tool"
1313		exit $ksft_skip
1314	fi
1315done
1316
1317while getopts t:hvpP o; do
1318	case $o in
1319		t) TESTS=$OPTARG;;
1320		v) VERBOSE=1;;
1321		p) PAUSE_ON_FAIL=yes;;
1322		P) PAUSE=yes;;
1323		h) usage; exit 0;;
1324		*) usage; exit 1;;
1325	esac
1326done
1327
1328[ $PAUSE = "yes" ] && PAUSE_ON_FAIL="no"
1329
1330kci_test_rtnl
1331
1332exit $?
1333