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