xref: /linux/tools/testing/selftests/net/forwarding/local_termination.sh (revision 643e2e259c2b25a2af0ae4c23c6e16586d9fd19c)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="standalone vlan_unaware_bridge vlan_aware_bridge test_vlan \
5	   vlan_over_vlan_unaware_bridged_port vlan_over_vlan_aware_bridged_port \
6	   vlan_over_vlan_unaware_bridge vlan_over_vlan_aware_bridge"
7NUM_NETIFS=2
8PING_COUNT=1
9REQUIRE_MTOOLS=yes
10
11source lib.sh
12
13H1_IPV4="192.0.2.1"
14H2_IPV4="192.0.2.2"
15H1_IPV6="2001:db8:1::1"
16H2_IPV6="2001:db8:1::2"
17
18BRIDGE_ADDR="00:00:de:ad:be:ee"
19MACVLAN_ADDR="00:00:de:ad:be:ef"
20UNKNOWN_UC_ADDR1="de:ad:be:ef:ee:03"
21UNKNOWN_UC_ADDR2="de:ad:be:ef:ee:04"
22UNKNOWN_UC_ADDR3="de:ad:be:ef:ee:05"
23JOINED_IPV4_MC_ADDR="225.1.2.3"
24UNKNOWN_IPV4_MC_ADDR1="225.1.2.4"
25UNKNOWN_IPV4_MC_ADDR2="225.1.2.5"
26UNKNOWN_IPV4_MC_ADDR3="225.1.2.6"
27JOINED_IPV6_MC_ADDR="ff2e::0102:0304"
28UNKNOWN_IPV6_MC_ADDR1="ff2e::0102:0305"
29UNKNOWN_IPV6_MC_ADDR2="ff2e::0102:0306"
30UNKNOWN_IPV6_MC_ADDR3="ff2e::0102:0307"
31
32JOINED_MACV4_MC_ADDR="01:00:5e:01:02:03"
33UNKNOWN_MACV4_MC_ADDR1="01:00:5e:01:02:04"
34UNKNOWN_MACV4_MC_ADDR2="01:00:5e:01:02:05"
35UNKNOWN_MACV4_MC_ADDR3="01:00:5e:01:02:06"
36JOINED_MACV6_MC_ADDR="33:33:01:02:03:04"
37UNKNOWN_MACV6_MC_ADDR1="33:33:01:02:03:05"
38UNKNOWN_MACV6_MC_ADDR2="33:33:01:02:03:06"
39UNKNOWN_MACV6_MC_ADDR3="33:33:01:02:03:07"
40
41PTP_1588_L2_SYNC=" \
4201:1b:19:00:00:00 00:00:de:ad:be:ef 88:f7 00 02 \
4300 2c 00 00 02 00 00 00 00 00 00 00 00 00 00 00 \
4400 00 3e 37 63 ff fe cf 17 0e 00 01 00 00 00 00 \
4500 00 00 00 00 00 00 00 00 00"
46PTP_1588_L2_FOLLOW_UP=" \
4701:1b:19:00:00:00 00:00:de:ad:be:ef 88:f7 08 02 \
4800 2c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
4900 00 3e 37 63 ff fe cf 17 0e 00 01 00 00 02 00 \
5000 00 66 83 c5 f1 17 97 ed f0"
51PTP_1588_L2_PDELAY_REQ=" \
5201:80:c2:00:00:0e 00:00:de:ad:be:ef 88:f7 02 02 \
5300 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
5400 00 3e 37 63 ff fe cf 17 0e 00 01 00 06 05 7f \
5500 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
5600 00 00 00"
57PTP_1588_IPV4_SYNC=" \
5801:00:5e:00:01:81 00:00:de:ad:be:ef 08:00 45 00 \
5900 48 0a 9a 40 00 01 11 cb 88 c0 00 02 01 e0 00 \
6001 81 01 3f 01 3f 00 34 a3 c8 00 02 00 2c 00 00 \
6102 00 00 00 00 00 00 00 00 00 00 00 00 00 3e 37 \
6263 ff fe cf 17 0e 00 01 00 00 00 00 00 00 00 00 \
6300 00 00 00 00 00"
64PTP_1588_IPV4_FOLLOW_UP="
6501:00:5e:00:01:81 00:00:de:ad:be:ef 08:00 45 00 \
6600 48 0a 9b 40 00 01 11 cb 87 c0 00 02 01 e0 00 \
6701 81 01 40 01 40 00 34 a3 c8 08 02 00 2c 00 00 \
6800 00 00 00 00 00 00 00 00 00 00 00 00 00 3e 37 \
6963 ff fe cf 17 0e 00 01 00 00 02 00 00 00 66 83 \
70c6 0f 1d 9a 61 87"
71PTP_1588_IPV4_PDELAY_REQ=" \
7201:00:5e:00:00:6b 00:00:de:ad:be:ef 08:00 45 00 \
7300 52 35 a9 40 00 01 11 a1 85 c0 00 02 01 e0 00 \
7400 6b 01 3f 01 3f 00 3e a2 bc 02 02 00 36 00 00 \
7500 00 00 00 00 00 00 00 00 00 00 00 00 00 3e 37 \
7663 ff fe cf 17 0e 00 01 00 01 05 7f 00 00 00 00 \
7700 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
78PTP_1588_IPV6_SYNC=" \
7933:33:00:00:01:81 00:00:de:ad:be:ef 86:dd 60 06 \
807c 2f 00 36 11 01 20 01 0d b8 00 01 00 00 00 00 \
8100 00 00 00 00 01 ff 0e 00 00 00 00 00 00 00 00 \
8200 00 00 00 01 81 01 3f 01 3f 00 36 2e 92 00 02 \
8300 2c 00 00 02 00 00 00 00 00 00 00 00 00 00 00 \
8400 00 3e 37 63 ff fe cf 17 0e 00 01 00 00 00 00 \
8500 00 00 00 00 00 00 00 00 00 00 00"
86PTP_1588_IPV6_FOLLOW_UP=" \
8733:33:00:00:01:81 00:00:de:ad:be:ef 86:dd 60 0a \
8800 bc 00 36 11 01 20 01 0d b8 00 01 00 00 00 00 \
8900 00 00 00 00 01 ff 0e 00 00 00 00 00 00 00 00 \
9000 00 00 00 01 81 01 40 01 40 00 36 2e 92 08 02 \
9100 2c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
9200 00 3e 37 63 ff fe cf 17 0e 00 01 00 00 02 00 \
9300 00 66 83 c6 2a 32 09 bd 74 00 00"
94PTP_1588_IPV6_PDELAY_REQ=" \
9533:33:00:00:00:6b 00:00:de:ad:be:ef 86:dd 60 0c \
965c fd 00 40 11 01 fe 80 00 00 00 00 00 00 3c 37 \
9763 ff fe cf 17 0e ff 02 00 00 00 00 00 00 00 00 \
9800 00 00 00 00 6b 01 3f 01 3f 00 40 b4 54 02 02 \
9900 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
10000 00 3e 37 63 ff fe cf 17 0e 00 01 00 01 05 7f \
10100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
10200 00 00 00 00 00"
103
104# Disable promisc to ensure we don't receive unknown MAC DA packets
105export TCPDUMP_EXTRA_FLAGS="-pl"
106
107h1=${NETIFS[p1]}
108h2=${NETIFS[p2]}
109
110send_raw()
111{
112	local if_name=$1; shift
113	local pkt="$1"; shift
114	local smac=$(mac_get $if_name)
115
116	pkt="${pkt/00:00:de:ad:be:ef/$smac}"
117
118	$MZ -q $if_name "$pkt"
119}
120
121send_uc_ipv4()
122{
123	local if_name=$1
124	local dmac=$2
125
126	ip neigh add $H2_IPV4 lladdr $dmac dev $if_name
127	ping_do $if_name $H2_IPV4
128	ip neigh del $H2_IPV4 dev $if_name
129}
130
131check_rcv()
132{
133	local if_name=$1; shift
134	local type=$1; shift
135	local pattern=$1; shift
136	local should_receive=$1; shift
137	local test_name="$1"; shift
138	local should_fail=
139
140	[ $should_receive = true ] && should_fail=0 || should_fail=1
141	RET=0
142
143	tcpdump_show $if_name | grep -q "$pattern"
144
145	check_err_fail "$should_fail" "$?" "reception"
146
147	log_test "$test_name: $type"
148}
149
150mc_route_prepare()
151{
152	local if_name=$1
153	local vrf_name=$(master_name_get $if_name)
154
155	ip route add 225.100.1.0/24 dev $if_name vrf $vrf_name
156	ip -6 route add ff2e::/64 dev $if_name vrf $vrf_name
157}
158
159mc_route_destroy()
160{
161	local if_name=$1
162	local vrf_name=$(master_name_get $if_name)
163
164	ip route del 225.100.1.0/24 dev $if_name vrf $vrf_name
165	ip -6 route del ff2e::/64 dev $if_name vrf $vrf_name
166}
167
168run_test()
169{
170	local send_if_name=$1; shift
171	local rcv_if_name=$1; shift
172	local skip_ptp=$1; shift
173	local no_unicast_flt=$1; shift
174	local test_name="$1"; shift
175	local smac=$(mac_get $send_if_name)
176	local rcv_dmac=$(mac_get $rcv_if_name)
177	local should_receive
178
179	tcpdump_start $rcv_if_name
180
181	mc_route_prepare $send_if_name
182	mc_route_prepare $rcv_if_name
183
184	send_uc_ipv4 $send_if_name $rcv_dmac
185	send_uc_ipv4 $send_if_name $MACVLAN_ADDR
186	send_uc_ipv4 $send_if_name $UNKNOWN_UC_ADDR1
187
188	ip link set dev $rcv_if_name promisc on
189	send_uc_ipv4 $send_if_name $UNKNOWN_UC_ADDR2
190	mc_send $send_if_name $UNKNOWN_IPV4_MC_ADDR2
191	mc_send $send_if_name $UNKNOWN_IPV6_MC_ADDR2
192	ip link set dev $rcv_if_name promisc off
193
194	mc_join $rcv_if_name $JOINED_IPV4_MC_ADDR
195	mc_send $send_if_name $JOINED_IPV4_MC_ADDR
196	mc_leave
197
198	mc_join $rcv_if_name $JOINED_IPV6_MC_ADDR
199	mc_send $send_if_name $JOINED_IPV6_MC_ADDR
200	mc_leave
201
202	mc_send $send_if_name $UNKNOWN_IPV4_MC_ADDR1
203	mc_send $send_if_name $UNKNOWN_IPV6_MC_ADDR1
204
205	ip link set dev $rcv_if_name allmulticast on
206	send_uc_ipv4 $send_if_name $UNKNOWN_UC_ADDR3
207	mc_send $send_if_name $UNKNOWN_IPV4_MC_ADDR3
208	mc_send $send_if_name $UNKNOWN_IPV6_MC_ADDR3
209	ip link set dev $rcv_if_name allmulticast off
210
211	mc_route_destroy $rcv_if_name
212	mc_route_destroy $send_if_name
213
214	if [ $skip_ptp = false ]; then
215		ip maddress add 01:1b:19:00:00:00 dev $rcv_if_name
216		send_raw $send_if_name "$PTP_1588_L2_SYNC"
217		send_raw $send_if_name "$PTP_1588_L2_FOLLOW_UP"
218		ip maddress del 01:1b:19:00:00:00 dev $rcv_if_name
219
220		ip maddress add 01:80:c2:00:00:0e dev $rcv_if_name
221		send_raw $send_if_name "$PTP_1588_L2_PDELAY_REQ"
222		ip maddress del 01:80:c2:00:00:0e dev $rcv_if_name
223
224		mc_join $rcv_if_name 224.0.1.129
225		send_raw $send_if_name "$PTP_1588_IPV4_SYNC"
226		send_raw $send_if_name "$PTP_1588_IPV4_FOLLOW_UP"
227		mc_leave
228
229		mc_join $rcv_if_name 224.0.0.107
230		send_raw $send_if_name "$PTP_1588_IPV4_PDELAY_REQ"
231		mc_leave
232
233		mc_join $rcv_if_name ff0e::181
234		send_raw $send_if_name "$PTP_1588_IPV6_SYNC"
235		send_raw $send_if_name "$PTP_1588_IPV6_FOLLOW_UP"
236		mc_leave
237
238		mc_join $rcv_if_name ff02::6b
239		send_raw $send_if_name "$PTP_1588_IPV6_PDELAY_REQ"
240		mc_leave
241	fi
242
243	sleep 1
244
245	tcpdump_stop $rcv_if_name
246
247	check_rcv $rcv_if_name "Unicast IPv4 to primary MAC address" \
248		"$smac > $rcv_dmac, ethertype IPv4 (0x0800)" \
249		true "$test_name"
250
251	check_rcv $rcv_if_name "Unicast IPv4 to macvlan MAC address" \
252		"$smac > $MACVLAN_ADDR, ethertype IPv4 (0x0800)" \
253		true "$test_name"
254
255	[ $no_unicast_flt = true ] && should_receive=true || should_receive=false
256	check_rcv $rcv_if_name "Unicast IPv4 to unknown MAC address" \
257		"$smac > $UNKNOWN_UC_ADDR1, ethertype IPv4 (0x0800)" \
258		$should_receive "$test_name"
259
260	check_rcv $rcv_if_name "Unicast IPv4 to unknown MAC address, promisc" \
261		"$smac > $UNKNOWN_UC_ADDR2, ethertype IPv4 (0x0800)" \
262		true "$test_name"
263
264	[ $no_unicast_flt = true ] && should_receive=true || should_receive=false
265	check_rcv $rcv_if_name \
266		"Unicast IPv4 to unknown MAC address, allmulti" \
267		"$smac > $UNKNOWN_UC_ADDR3, ethertype IPv4 (0x0800)" \
268		$should_receive "$test_name"
269
270	check_rcv $rcv_if_name "Multicast IPv4 to joined group" \
271		"$smac > $JOINED_MACV4_MC_ADDR, ethertype IPv4 (0x0800)" \
272		true "$test_name"
273
274	xfail \
275		check_rcv $rcv_if_name \
276			"Multicast IPv4 to unknown group" \
277			"$smac > $UNKNOWN_MACV4_MC_ADDR1, ethertype IPv4 (0x0800)" \
278			false "$test_name"
279
280	check_rcv $rcv_if_name "Multicast IPv4 to unknown group, promisc" \
281		"$smac > $UNKNOWN_MACV4_MC_ADDR2, ethertype IPv4 (0x0800)" \
282		true "$test_name"
283
284	check_rcv $rcv_if_name "Multicast IPv4 to unknown group, allmulti" \
285		"$smac > $UNKNOWN_MACV4_MC_ADDR3, ethertype IPv4 (0x0800)" \
286		true "$test_name"
287
288	check_rcv $rcv_if_name "Multicast IPv6 to joined group" \
289		"$smac > $JOINED_MACV6_MC_ADDR, ethertype IPv6 (0x86dd)" \
290		true "$test_name"
291
292	xfail \
293		check_rcv $rcv_if_name "Multicast IPv6 to unknown group" \
294			"$smac > $UNKNOWN_MACV6_MC_ADDR1, ethertype IPv6 (0x86dd)" \
295			false "$test_name"
296
297	check_rcv $rcv_if_name "Multicast IPv6 to unknown group, promisc" \
298		"$smac > $UNKNOWN_MACV6_MC_ADDR2, ethertype IPv6 (0x86dd)" \
299		true "$test_name"
300
301	check_rcv $rcv_if_name "Multicast IPv6 to unknown group, allmulti" \
302		"$smac > $UNKNOWN_MACV6_MC_ADDR3, ethertype IPv6 (0x86dd)" \
303		true "$test_name"
304
305	if [ $skip_ptp = false ]; then
306		check_rcv $rcv_if_name "1588v2 over L2 transport, Sync" \
307			"ethertype PTP (0x88f7).* PTPv2.* msg type : sync msg" \
308			true "$test_name"
309
310		check_rcv $rcv_if_name "1588v2 over L2 transport, Follow-Up" \
311			"ethertype PTP (0x88f7).* PTPv2.* msg type : follow up msg" \
312			true "$test_name"
313
314		check_rcv $rcv_if_name "1588v2 over L2 transport, Peer Delay Request" \
315			"ethertype PTP (0x88f7).* PTPv2.* msg type : peer delay req msg" \
316			true "$test_name"
317
318		check_rcv $rcv_if_name "1588v2 over IPv4, Sync" \
319			"ethertype IPv4 (0x0800).* PTPv2.* msg type : sync msg" \
320			true "$test_name"
321
322		check_rcv $rcv_if_name "1588v2 over IPv4, Follow-Up" \
323			"ethertype IPv4 (0x0800).* PTPv2.* msg type : follow up msg" \
324			true "$test_name"
325
326		check_rcv $rcv_if_name "1588v2 over IPv4, Peer Delay Request" \
327			"ethertype IPv4 (0x0800).* PTPv2.* msg type : peer delay req msg" \
328			true "$test_name"
329
330		check_rcv $rcv_if_name "1588v2 over IPv6, Sync" \
331			"ethertype IPv6 (0x86dd).* PTPv2.* msg type : sync msg" \
332			true "$test_name"
333
334		check_rcv $rcv_if_name "1588v2 over IPv6, Follow-Up" \
335			"ethertype IPv6 (0x86dd).* PTPv2.* msg type : follow up msg" \
336			true "$test_name"
337
338		check_rcv $rcv_if_name "1588v2 over IPv6, Peer Delay Request" \
339			"ethertype IPv6 (0x86dd).* PTPv2.* msg type : peer delay req msg" \
340			true "$test_name"
341	fi
342
343	tcpdump_cleanup $rcv_if_name
344}
345
346h1_create()
347{
348	simple_if_init $h1 $H1_IPV4/24 $H1_IPV6/64
349}
350
351h1_destroy()
352{
353	simple_if_fini $h1 $H1_IPV4/24 $H1_IPV6/64
354}
355
356h2_create()
357{
358	simple_if_init $h2 $H2_IPV4/24 $H2_IPV6/64
359}
360
361h2_destroy()
362{
363	simple_if_fini $h2 $H2_IPV4/24 $H2_IPV6/64
364}
365
366h1_vlan_create()
367{
368	simple_if_init $h1
369	vlan_create $h1 100 v$h1 $H1_IPV4/24 $H1_IPV6/64
370}
371
372h1_vlan_destroy()
373{
374	vlan_destroy $h1 100
375	simple_if_fini $h1
376}
377
378h2_vlan_create()
379{
380	simple_if_init $h2
381	vlan_create $h2 100 v$h2 $H2_IPV4/24 $H2_IPV6/64
382}
383
384h2_vlan_destroy()
385{
386	vlan_destroy $h2 100
387	simple_if_fini $h2
388}
389
390bridge_create()
391{
392	local vlan_filtering=$1
393
394	ip link add br0 type bridge vlan_filtering $vlan_filtering
395	ip link set br0 address $BRIDGE_ADDR
396	ip link set br0 up
397
398	ip link set $h2 master br0
399	ip link set $h2 up
400}
401
402bridge_destroy()
403{
404	ip link del br0
405}
406
407macvlan_create()
408{
409	local lower=$1
410
411	ip link add link $lower name macvlan0 type macvlan mode private
412	ip link set macvlan0 address $MACVLAN_ADDR
413	ip link set macvlan0 up
414}
415
416macvlan_destroy()
417{
418	ip link del macvlan0
419}
420
421standalone()
422{
423	local no_unicast_flt=true
424	local skip_ptp=false
425
426	if [ $(has_unicast_flt $h2) = yes ]; then
427		no_unicast_flt=false
428	fi
429
430	h1_create
431	h2_create
432	macvlan_create $h2
433
434	run_test $h1 $h2 $skip_ptp $no_unicast_flt "$h2"
435
436	macvlan_destroy
437	h2_destroy
438	h1_destroy
439}
440
441test_bridge()
442{
443	local no_unicast_flt=true
444	local vlan_filtering=$1
445	local skip_ptp=true
446
447	h1_create
448	bridge_create $vlan_filtering
449	simple_if_init br0 $H2_IPV4/24 $H2_IPV6/64
450	macvlan_create br0
451
452	run_test $h1 br0 $skip_ptp $no_unicast_flt \
453		"vlan_filtering=$vlan_filtering bridge"
454
455	macvlan_destroy
456	simple_if_fini br0 $H2_IPV4/24 $H2_IPV6/64
457	bridge_destroy
458	h1_destroy
459}
460
461vlan_unaware_bridge()
462{
463	test_bridge 0
464}
465
466vlan_aware_bridge()
467{
468	test_bridge 1
469}
470
471test_vlan()
472{
473	local no_unicast_flt=true
474	local skip_ptp=false
475
476	if [ $(has_unicast_flt $h2) = yes ]; then
477		no_unicast_flt=false
478	fi
479
480	h1_vlan_create
481	h2_vlan_create
482	macvlan_create $h2.100
483
484	run_test $h1.100 $h2.100 $skip_ptp $no_unicast_flt "VLAN upper"
485
486	macvlan_destroy
487	h2_vlan_destroy
488	h1_vlan_destroy
489}
490
491vlan_over_bridged_port()
492{
493	local no_unicast_flt=true
494	local vlan_filtering=$1
495	local skip_ptp=false
496
497	# br_manage_promisc() will not force a single vlan_filtering port to
498	# promiscuous mode, so we should still expect unicast filtering to take
499	# place if the device can do it.
500	if [ $(has_unicast_flt $h2) = yes ] && [ $vlan_filtering = 1 ]; then
501		no_unicast_flt=false
502	fi
503
504	h1_vlan_create
505	h2_vlan_create
506	bridge_create $vlan_filtering
507	macvlan_create $h2.100
508
509	run_test $h1.100 $h2.100 $skip_ptp $no_unicast_flt \
510		"VLAN over vlan_filtering=$vlan_filtering bridged port"
511
512	macvlan_destroy
513	bridge_destroy
514	h2_vlan_destroy
515	h1_vlan_destroy
516}
517
518vlan_over_vlan_unaware_bridged_port()
519{
520	vlan_over_bridged_port 0
521}
522
523vlan_over_vlan_aware_bridged_port()
524{
525	vlan_over_bridged_port 1
526}
527
528vlan_over_bridge()
529{
530	local no_unicast_flt=true
531	local vlan_filtering=$1
532	local skip_ptp=true
533
534	h1_vlan_create
535	bridge_create $vlan_filtering
536	simple_if_init br0
537	vlan_create br0 100 vbr0 $H2_IPV4/24 $H2_IPV6/64
538	macvlan_create br0.100
539
540	if [ $vlan_filtering = 1 ]; then
541		bridge vlan add dev $h2 vid 100 master
542		bridge vlan add dev br0 vid 100 self
543	fi
544
545	run_test $h1.100 br0.100 $skip_ptp $no_unicast_flt \
546		"VLAN over vlan_filtering=$vlan_filtering bridge"
547
548	if [ $vlan_filtering = 1 ]; then
549		bridge vlan del dev br0 vid 100 self
550		bridge vlan del dev $h2 vid 100 master
551	fi
552
553	macvlan_destroy
554	vlan_destroy br0 100
555	simple_if_fini br0
556	bridge_destroy
557	h1_vlan_destroy
558}
559
560vlan_over_vlan_unaware_bridge()
561{
562	vlan_over_bridge 0
563}
564
565vlan_over_vlan_aware_bridge()
566{
567	vlan_over_bridge 1
568}
569
570cleanup()
571{
572	pre_cleanup
573
574	ip link set $h2 down
575	ip link set $h1 down
576
577	vrf_cleanup
578}
579
580setup_prepare()
581{
582	vrf_prepare
583	# setup_wait() needs this
584	ip link set $h1 up
585	ip link set $h2 up
586}
587
588trap cleanup EXIT
589
590setup_prepare
591setup_wait
592
593tests_run
594
595exit $EXIT_STATUS
596