xref: /linux/tools/testing/selftests/net/forwarding/bridge_igmp.sh (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="v2reportleave_test v3include_test v3inc_allow_test v3inc_is_include_test \
5	   v3inc_is_exclude_test v3inc_to_exclude_test v3exc_allow_test v3exc_is_include_test \
6	   v3exc_is_exclude_test v3exc_to_exclude_test v3inc_block_test v3exc_block_test \
7	   v3exc_timeout_test v3star_ex_auto_add_test"
8NUM_NETIFS=4
9CHECK_TC="yes"
10TEST_GROUP="239.10.10.10"
11TEST_GROUP_MAC="01:00:5e:0a:0a:0a"
12
13ALL_GROUP="224.0.0.1"
14ALL_MAC="01:00:5e:00:00:01"
15
16# IGMPv3 is_in report: grp 239.10.10.10 is_include 192.0.2.1,192.0.2.2,192.0.2.3
17MZPKT_IS_INC="22:00:9d:de:00:00:00:01:01:00:00:03:ef:0a:0a:0a:c0:00:02:01:c0:00:02:02:c0:00:02:03"
18# IGMPv3 is_in report: grp 239.10.10.10 is_include 192.0.2.10,192.0.2.11,192.0.2.12
19MZPKT_IS_INC2="22:00:9d:c3:00:00:00:01:01:00:00:03:ef:0a:0a:0a:c0:00:02:0a:c0:00:02:0b:c0:00:02:0c"
20# IGMPv3 is_in report: grp 239.10.10.10 is_include 192.0.2.20,192.0.2.30
21MZPKT_IS_INC3="22:00:5f:b4:00:00:00:01:01:00:00:02:ef:0a:0a:0a:c0:00:02:14:c0:00:02:1e"
22# IGMPv3 allow report: grp 239.10.10.10 allow 192.0.2.10,192.0.2.11,192.0.2.12
23MZPKT_ALLOW="22:00:99:c3:00:00:00:01:05:00:00:03:ef:0a:0a:0a:c0:00:02:0a:c0:00:02:0b:c0:00:02:0c"
24# IGMPv3 allow report: grp 239.10.10.10 allow 192.0.2.20,192.0.2.30
25MZPKT_ALLOW2="22:00:5b:b4:00:00:00:01:05:00:00:02:ef:0a:0a:0a:c0:00:02:14:c0:00:02:1e"
26# IGMPv3 is_ex report: grp 239.10.10.10 is_exclude 192.0.2.1,192.0.2.2,192.0.2.20,192.0.2.21
27MZPKT_IS_EXC="22:00:da:b6:00:00:00:01:02:00:00:04:ef:0a:0a:0a:c0:00:02:01:c0:00:02:02:c0:00:02:14:c0:00:02:15"
28# IGMPv3 is_ex report: grp 239.10.10.10 is_exclude 192.0.2.20,192.0.2.30
29MZPKT_IS_EXC2="22:00:5e:b4:00:00:00:01:02:00:00:02:ef:0a:0a:0a:c0:00:02:14:c0:00:02:1e"
30# IGMPv3 to_ex report: grp 239.10.10.10 to_exclude 192.0.2.1,192.0.2.20,192.0.2.30
31MZPKT_TO_EXC="22:00:9a:b1:00:00:00:01:04:00:00:03:ef:0a:0a:0a:c0:00:02:01:c0:00:02:14:c0:00:02:1e"
32# IGMPv3 block report: grp 239.10.10.10 block 192.0.2.1,192.0.2.20,192.0.2.30
33MZPKT_BLOCK="22:00:98:b1:00:00:00:01:06:00:00:03:ef:0a:0a:0a:c0:00:02:01:c0:00:02:14:c0:00:02:1e"
34
35source lib.sh
36
37h1_create()
38{
39	simple_if_init $h1 192.0.2.1/24 2001:db8:1::1/64
40}
41
42h1_destroy()
43{
44	simple_if_fini $h1 192.0.2.1/24 2001:db8:1::1/64
45}
46
47h2_create()
48{
49	simple_if_init $h2 192.0.2.2/24 2001:db8:1::2/64
50}
51
52h2_destroy()
53{
54	simple_if_fini $h2 192.0.2.2/24 2001:db8:1::2/64
55}
56
57switch_create()
58{
59	ip link add dev br0 type bridge mcast_snooping 1 mcast_querier 1
60
61	ip link set dev $swp1 master br0
62	ip link set dev $swp2 master br0
63
64	ip link set dev br0 up
65	ip link set dev $swp1 up
66	ip link set dev $swp2 up
67}
68
69switch_destroy()
70{
71	ip link set dev $swp2 down
72	ip link set dev $swp1 down
73
74	ip link del dev br0
75}
76
77setup_prepare()
78{
79	h1=${NETIFS[p1]}
80	swp1=${NETIFS[p2]}
81
82	swp2=${NETIFS[p3]}
83	h2=${NETIFS[p4]}
84
85	vrf_prepare
86
87	h1_create
88	h2_create
89
90	switch_create
91}
92
93cleanup()
94{
95	pre_cleanup
96
97	switch_destroy
98
99	h2_destroy
100	h1_destroy
101
102	vrf_cleanup
103}
104
105v2reportleave_test()
106{
107	RET=0
108	ip address add dev $h2 $TEST_GROUP/32 autojoin
109	check_err $? "Could not join $TEST_GROUP"
110
111	sleep 5
112	bridge mdb show dev br0 | grep $TEST_GROUP 1>/dev/null
113	check_err $? "IGMPv2 report didn't create mdb entry for $TEST_GROUP"
114
115	mcast_packet_test $TEST_GROUP_MAC 192.0.2.1 $TEST_GROUP $h1 $h2
116	check_fail $? "Traffic to $TEST_GROUP wasn't forwarded"
117
118	log_test "IGMPv2 report $TEST_GROUP"
119
120	RET=0
121	bridge mdb show dev br0 | grep $TEST_GROUP 1>/dev/null
122	check_err $? "mdb entry for $TEST_GROUP is missing"
123
124	ip address del dev $h2 $TEST_GROUP/32
125	check_err $? "Could not leave $TEST_GROUP"
126
127	sleep 5
128	bridge mdb show dev br0 | grep $TEST_GROUP 1>/dev/null
129	check_fail $? "Leave didn't delete mdb entry for $TEST_GROUP"
130
131	mcast_packet_test $TEST_GROUP_MAC 192.0.2.1 $TEST_GROUP $h1 $h2
132	check_err $? "Traffic to $TEST_GROUP was forwarded without mdb entry"
133
134	log_test "IGMPv2 leave $TEST_GROUP"
135}
136
137v3include_prepare()
138{
139	local host1_if=$1
140	local mac=$2
141	local group=$3
142	local X=("192.0.2.1" "192.0.2.2" "192.0.2.3")
143
144	ip link set dev br0 type bridge mcast_igmp_version 3
145	check_err $? "Could not change bridge IGMP version to 3"
146
147	$MZ $host1_if -b $mac -c 1 -B $group -t ip "proto=2,p=$MZPKT_IS_INC" -q
148	sleep 1
149	bridge -j -d -s mdb show dev br0 \
150		| jq -e ".[].mdb[] | \
151			 select(.grp == \"$TEST_GROUP\" and .source_list != null)" &>/dev/null
152	check_err $? "Missing *,G entry with source list"
153	bridge -j -d -s mdb show dev br0 \
154		| jq -e ".[].mdb[] | \
155			 select(.grp == \"$TEST_GROUP\" and \
156				.source_list != null and .filter_mode == \"include\")" &>/dev/null
157	check_err $? "Wrong *,G entry filter mode"
158	brmcast_check_sg_entries "is_include" "${X[@]}"
159}
160
161v3exclude_prepare()
162{
163	local host1_if=$1
164	local mac=$2
165	local group=$3
166	local pkt=$4
167	local X=("192.0.2.1" "192.0.2.2")
168	local Y=("192.0.2.20" "192.0.2.21")
169
170	v3include_prepare $host1_if $mac $group
171
172	$MZ $host1_if -c 1 -b $mac -B $group -t ip "proto=2,p=$MZPKT_IS_EXC" -q
173	sleep 1
174	bridge -j -d -s mdb show dev br0 \
175		| jq -e ".[].mdb[] | \
176			 select(.grp == \"$TEST_GROUP\" and \
177				.source_list != null and .filter_mode == \"exclude\")" &>/dev/null
178	check_err $? "Wrong *,G entry filter mode"
179
180	brmcast_check_sg_entries "is_exclude" "${X[@]}" "${Y[@]}"
181
182	brmcast_check_sg_state 0 "${X[@]}"
183	brmcast_check_sg_state 1 "${Y[@]}"
184
185	bridge -j -d -s mdb show dev br0 \
186		| jq -e ".[].mdb[] | \
187			 select(.grp == \"$TEST_GROUP\" and \
188				.source_list != null and
189				.source_list[].address == \"192.0.2.3\")" &>/dev/null
190	check_fail $? "Wrong *,G entry source list, 192.0.2.3 entry still exists"
191}
192
193v3cleanup()
194{
195	local port=$1
196	local group=$2
197
198	bridge mdb del dev br0 port $port grp $group
199	ip link set dev br0 type bridge mcast_igmp_version 2
200}
201
202v3include_test()
203{
204	RET=0
205	local X=("192.0.2.1" "192.0.2.2" "192.0.2.3")
206
207	v3include_prepare $h1 $ALL_MAC $ALL_GROUP
208
209	brmcast_check_sg_state 0 "${X[@]}"
210
211	brmcast_check_sg_fwding 1 "${X[@]}"
212	brmcast_check_sg_fwding 0 "192.0.2.100"
213
214	log_test "IGMPv3 report $TEST_GROUP is_include"
215
216	v3cleanup $swp1 $TEST_GROUP
217}
218
219v3inc_allow_test()
220{
221	RET=0
222	local X=("192.0.2.10" "192.0.2.11" "192.0.2.12")
223
224	v3include_prepare $h1 $ALL_MAC $ALL_GROUP
225
226	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_ALLOW" -q
227	sleep 1
228	brmcast_check_sg_entries "allow" "${X[@]}"
229
230	brmcast_check_sg_state 0 "${X[@]}"
231
232	brmcast_check_sg_fwding 1 "${X[@]}"
233	brmcast_check_sg_fwding 0 "192.0.2.100"
234
235	log_test "IGMPv3 report $TEST_GROUP include -> allow"
236
237	v3cleanup $swp1 $TEST_GROUP
238}
239
240v3inc_is_include_test()
241{
242	RET=0
243	local X=("192.0.2.10" "192.0.2.11" "192.0.2.12")
244
245	v3include_prepare $h1 $ALL_MAC $ALL_GROUP
246
247	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_IS_INC2" -q
248	sleep 1
249	brmcast_check_sg_entries "is_include" "${X[@]}"
250
251	brmcast_check_sg_state 0 "${X[@]}"
252
253	brmcast_check_sg_fwding 1 "${X[@]}"
254	brmcast_check_sg_fwding 0 "192.0.2.100"
255
256	log_test "IGMPv3 report $TEST_GROUP include -> is_include"
257
258	v3cleanup $swp1 $TEST_GROUP
259}
260
261v3inc_is_exclude_test()
262{
263	RET=0
264
265	v3exclude_prepare $h1 $ALL_MAC $ALL_GROUP
266
267	brmcast_check_sg_fwding 1 "${X[@]}" 192.0.2.100
268	brmcast_check_sg_fwding 0 "${Y[@]}"
269
270	log_test "IGMPv3 report $TEST_GROUP include -> is_exclude"
271
272	v3cleanup $swp1 $TEST_GROUP
273}
274
275v3inc_to_exclude_test()
276{
277	RET=0
278	local X=("192.0.2.1")
279	local Y=("192.0.2.20" "192.0.2.30")
280
281	v3include_prepare $h1 $ALL_MAC $ALL_GROUP
282
283	ip link set dev br0 type bridge mcast_last_member_interval 500
284	check_err $? "Could not change mcast_last_member_interval to 5s"
285
286	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_TO_EXC" -q
287	sleep 1
288	bridge -j -d -s mdb show dev br0 \
289		| jq -e ".[].mdb[] | \
290			 select(.grp == \"$TEST_GROUP\" and \
291				.source_list != null and .filter_mode == \"exclude\")" &>/dev/null
292	check_err $? "Wrong *,G entry filter mode"
293
294	brmcast_check_sg_entries "to_exclude" "${X[@]}" "${Y[@]}"
295
296	brmcast_check_sg_state 0 "${X[@]}"
297	brmcast_check_sg_state 1 "${Y[@]}"
298
299	bridge -j -d -s mdb show dev br0 \
300		| jq -e ".[].mdb[] | \
301			 select(.grp == \"$TEST_GROUP\" and \
302				.source_list != null and
303				.source_list[].address == \"192.0.2.2\")" &>/dev/null
304	check_fail $? "Wrong *,G entry source list, 192.0.2.2 entry still exists"
305	bridge -j -d -s mdb show dev br0 \
306		| jq -e ".[].mdb[] | \
307			 select(.grp == \"$TEST_GROUP\" and \
308				.source_list != null and
309				.source_list[].address == \"192.0.2.21\")" &>/dev/null
310	check_fail $? "Wrong *,G entry source list, 192.0.2.21 entry still exists"
311
312	brmcast_check_sg_fwding 1 "${X[@]}" 192.0.2.100
313	brmcast_check_sg_fwding 0 "${Y[@]}"
314
315	log_test "IGMPv3 report $TEST_GROUP include -> to_exclude"
316
317	ip link set dev br0 type bridge mcast_last_member_interval 100
318
319	v3cleanup $swp1 $TEST_GROUP
320}
321
322v3exc_allow_test()
323{
324	RET=0
325	local X=("192.0.2.1" "192.0.2.2" "192.0.2.20" "192.0.2.30")
326	local Y=("192.0.2.21")
327
328	v3exclude_prepare $h1 $ALL_MAC $ALL_GROUP
329
330	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_ALLOW2" -q
331	sleep 1
332	brmcast_check_sg_entries "allow" "${X[@]}" "${Y[@]}"
333
334	brmcast_check_sg_state 0 "${X[@]}"
335	brmcast_check_sg_state 1 "${Y[@]}"
336
337	brmcast_check_sg_fwding 1 "${X[@]}" 192.0.2.100
338	brmcast_check_sg_fwding 0 "${Y[@]}"
339
340	log_test "IGMPv3 report $TEST_GROUP exclude -> allow"
341
342	v3cleanup $swp1 $TEST_GROUP
343}
344
345v3exc_is_include_test()
346{
347	RET=0
348	local X=("192.0.2.1" "192.0.2.2" "192.0.2.20" "192.0.2.30")
349	local Y=("192.0.2.21")
350
351	v3exclude_prepare $h1 $ALL_MAC $ALL_GROUP
352
353	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_IS_INC3" -q
354	sleep 1
355	brmcast_check_sg_entries "is_include" "${X[@]}" "${Y[@]}"
356
357	brmcast_check_sg_state 0 "${X[@]}"
358	brmcast_check_sg_state 1 "${Y[@]}"
359
360	brmcast_check_sg_fwding 1 "${X[@]}" 192.0.2.100
361	brmcast_check_sg_fwding 0 "${Y[@]}"
362
363	log_test "IGMPv3 report $TEST_GROUP exclude -> is_include"
364
365	v3cleanup $swp1 $TEST_GROUP
366}
367
368v3exc_is_exclude_test()
369{
370	RET=0
371	local X=("192.0.2.30")
372	local Y=("192.0.2.20")
373
374	v3exclude_prepare $h1 $ALL_MAC $ALL_GROUP
375
376	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_IS_EXC2" -q
377	sleep 1
378	brmcast_check_sg_entries "is_exclude" "${X[@]}" "${Y[@]}"
379
380	brmcast_check_sg_state 0 "${X[@]}"
381	brmcast_check_sg_state 1 "${Y[@]}"
382
383	brmcast_check_sg_fwding 1 "${X[@]}" 192.0.2.100
384	brmcast_check_sg_fwding 0 "${Y[@]}"
385
386	log_test "IGMPv3 report $TEST_GROUP exclude -> is_exclude"
387
388	v3cleanup $swp1 $TEST_GROUP
389}
390
391v3exc_to_exclude_test()
392{
393	RET=0
394	local X=("192.0.2.1" "192.0.2.30")
395	local Y=("192.0.2.20")
396
397	v3exclude_prepare $h1 $ALL_MAC $ALL_GROUP
398
399	ip link set dev br0 type bridge mcast_last_member_interval 500
400	check_err $? "Could not change mcast_last_member_interval to 5s"
401
402	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_TO_EXC" -q
403	sleep 1
404	brmcast_check_sg_entries "to_exclude" "${X[@]}" "${Y[@]}"
405
406	brmcast_check_sg_state 0 "${X[@]}"
407	brmcast_check_sg_state 1 "${Y[@]}"
408
409	brmcast_check_sg_fwding 1 "${X[@]}" 192.0.2.100
410	brmcast_check_sg_fwding 0 "${Y[@]}"
411
412	log_test "IGMPv3 report $TEST_GROUP exclude -> to_exclude"
413
414	ip link set dev br0 type bridge mcast_last_member_interval 100
415
416	v3cleanup $swp1 $TEST_GROUP
417}
418
419v3inc_block_test()
420{
421	RET=0
422	local X=("192.0.2.2" "192.0.2.3")
423
424	v3include_prepare $h1 $ALL_MAC $ALL_GROUP
425
426	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_BLOCK" -q
427	# make sure the lowered timers have expired (by default 2 seconds)
428	sleep 3
429	brmcast_check_sg_entries "block" "${X[@]}"
430
431	brmcast_check_sg_state 0 "${X[@]}"
432
433	bridge -j -d -s mdb show dev br0 \
434		| jq -e ".[].mdb[] | \
435			 select(.grp == \"$TEST_GROUP\" and \
436				.source_list != null and
437				.source_list[].address == \"192.0.2.1\")" &>/dev/null
438	check_fail $? "Wrong *,G entry source list, 192.0.2.1 entry still exists"
439
440	brmcast_check_sg_fwding 1 "${X[@]}"
441	brmcast_check_sg_fwding 0 "192.0.2.100"
442
443	log_test "IGMPv3 report $TEST_GROUP include -> block"
444
445	v3cleanup $swp1 $TEST_GROUP
446}
447
448v3exc_block_test()
449{
450	RET=0
451	local X=("192.0.2.1" "192.0.2.2" "192.0.2.30")
452	local Y=("192.0.2.20" "192.0.2.21")
453
454	v3exclude_prepare $h1 $ALL_MAC $ALL_GROUP
455
456	ip link set dev br0 type bridge mcast_last_member_interval 500
457	check_err $? "Could not change mcast_last_member_interval to 5s"
458
459	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_BLOCK" -q
460	sleep 1
461	brmcast_check_sg_entries "block" "${X[@]}" "${Y[@]}"
462
463	brmcast_check_sg_state 0 "${X[@]}"
464	brmcast_check_sg_state 1 "${Y[@]}"
465
466	brmcast_check_sg_fwding 1 "${X[@]}" 192.0.2.100
467	brmcast_check_sg_fwding 0 "${Y[@]}"
468
469	log_test "IGMPv3 report $TEST_GROUP exclude -> block"
470
471	ip link set dev br0 type bridge mcast_last_member_interval 100
472
473	v3cleanup $swp1 $TEST_GROUP
474}
475
476v3exc_timeout_test()
477{
478	RET=0
479	local X=("192.0.2.20" "192.0.2.30")
480
481	# GMI should be 5 seconds
482	ip link set dev br0 type bridge mcast_query_interval 100 \
483					mcast_query_response_interval 100 \
484					mcast_membership_interval 500
485
486	v3exclude_prepare $h1 $ALL_MAC $ALL_GROUP
487	ip link set dev br0 type bridge mcast_query_interval 500 \
488					mcast_query_response_interval 500 \
489					mcast_membership_interval 1500
490
491	$MZ $h1 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_ALLOW2" -q
492	sleep 5
493	bridge -j -d -s mdb show dev br0 \
494		| jq -e ".[].mdb[] | \
495			 select(.grp == \"$TEST_GROUP\" and \
496				.source_list != null and .filter_mode == \"include\")" &>/dev/null
497	check_err $? "Wrong *,G entry filter mode"
498
499	bridge -j -d -s mdb show dev br0 \
500		| jq -e ".[].mdb[] | \
501			 select(.grp == \"$TEST_GROUP\" and \
502				.source_list != null and
503				.source_list[].address == \"192.0.2.1\")" &>/dev/null
504	check_fail $? "Wrong *,G entry source list, 192.0.2.1 entry still exists"
505	bridge -j -d -s mdb show dev br0 \
506		| jq -e ".[].mdb[] | \
507			 select(.grp == \"$TEST_GROUP\" and \
508				.source_list != null and
509				.source_list[].address == \"192.0.2.2\")" &>/dev/null
510	check_fail $? "Wrong *,G entry source list, 192.0.2.2 entry still exists"
511
512	brmcast_check_sg_entries "allow" "${X[@]}"
513
514	brmcast_check_sg_state 0 "${X[@]}"
515
516	brmcast_check_sg_fwding 1 "${X[@]}"
517	brmcast_check_sg_fwding 0 192.0.2.100
518
519	log_test "IGMPv3 group $TEST_GROUP exclude timeout"
520
521	ip link set dev br0 type bridge mcast_query_interval 12500 \
522					mcast_query_response_interval 1000 \
523					mcast_membership_interval 26000
524
525	v3cleanup $swp1 $TEST_GROUP
526}
527
528v3star_ex_auto_add_test()
529{
530	RET=0
531
532	v3exclude_prepare $h1 $ALL_MAC $ALL_GROUP
533
534	$MZ $h2 -c 1 -b $ALL_MAC -B $ALL_GROUP -t ip "proto=2,p=$MZPKT_IS_INC" -q
535	sleep 1
536	bridge -j -d -s mdb show dev br0 \
537		| jq -e ".[].mdb[] | \
538			 select(.grp == \"$TEST_GROUP\" and .src == \"192.0.2.3\" and \
539				.port == \"$swp1\")" &>/dev/null
540	check_err $? "S,G entry for *,G port doesn't exist"
541
542	bridge -j -d -s mdb show dev br0 \
543		| jq -e ".[].mdb[] | \
544			 select(.grp == \"$TEST_GROUP\" and .src == \"192.0.2.3\" and \
545				.port == \"$swp1\" and \
546				.flags[] == \"added_by_star_ex\")" &>/dev/null
547	check_err $? "Auto-added S,G entry doesn't have added_by_star_ex flag"
548
549	brmcast_check_sg_fwding 1 192.0.2.3
550
551	log_test "IGMPv3 S,G port entry automatic add to a *,G port"
552
553	v3cleanup $swp1 $TEST_GROUP
554	v3cleanup $swp2 $TEST_GROUP
555}
556
557trap cleanup EXIT
558
559setup_prepare
560setup_wait
561
562tests_run
563
564exit $EXIT_STATUS
565