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