selftests: net: bridge: igmp: check for specific udp ip protocol
[linux-2.6-microblaze.git] / tools / testing / selftests / net / forwarding / bridge_igmp.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3
4 ALL_TESTS="v2reportleave_test"
5 NUM_NETIFS=4
6 CHECK_TC="yes"
7 TEST_GROUP="239.10.10.10"
8 TEST_GROUP_MAC="01:00:5e:0a:0a:0a"
9 source lib.sh
10
11 h1_create()
12 {
13         simple_if_init $h1 192.0.2.1/24 2001:db8:1::1/64
14 }
15
16 h1_destroy()
17 {
18         simple_if_fini $h1 192.0.2.1/24 2001:db8:1::1/64
19 }
20
21 h2_create()
22 {
23         simple_if_init $h2 192.0.2.2/24 2001:db8:1::2/64
24 }
25
26 h2_destroy()
27 {
28         simple_if_fini $h2 192.0.2.2/24 2001:db8:1::2/64
29 }
30
31 switch_create()
32 {
33         ip link add dev br0 type bridge mcast_snooping 1 mcast_querier 1
34
35         ip link set dev $swp1 master br0
36         ip link set dev $swp2 master br0
37
38         ip link set dev br0 up
39         ip link set dev $swp1 up
40         ip link set dev $swp2 up
41 }
42
43 switch_destroy()
44 {
45         ip link set dev $swp2 down
46         ip link set dev $swp1 down
47
48         ip link del dev br0
49 }
50
51 setup_prepare()
52 {
53         h1=${NETIFS[p1]}
54         swp1=${NETIFS[p2]}
55
56         swp2=${NETIFS[p3]}
57         h2=${NETIFS[p4]}
58
59         vrf_prepare
60
61         h1_create
62         h2_create
63
64         switch_create
65 }
66
67 cleanup()
68 {
69         pre_cleanup
70
71         switch_destroy
72
73         # Always cleanup the mcast group
74         ip address del dev $h2 $TEST_GROUP/32 2>&1 1>/dev/null
75
76         h2_destroy
77         h1_destroy
78
79         vrf_cleanup
80 }
81
82 # return 0 if the packet wasn't seen on host2_if or 1 if it was
83 mcast_packet_test()
84 {
85         local mac=$1
86         local src_ip=$2
87         local ip=$3
88         local host1_if=$4
89         local host2_if=$5
90         local seen=0
91
92         # Add an ACL on `host2_if` which will tell us whether the packet
93         # was received by it or not.
94         tc qdisc add dev $host2_if ingress
95         tc filter add dev $host2_if ingress protocol ip pref 1 handle 101 \
96                 flower ip_proto udp dst_mac $mac action drop
97
98         $MZ $host1_if -c 1 -p 64 -b $mac -A $src_ip -B $ip -t udp "dp=4096,sp=2048" -q
99         sleep 1
100
101         tc -j -s filter show dev $host2_if ingress \
102                 | jq -e ".[] | select(.options.handle == 101) \
103                 | select(.options.actions[0].stats.packets == 1)" &> /dev/null
104         if [[ $? -eq 0 ]]; then
105                 seen=1
106         fi
107
108         tc filter del dev $host2_if ingress protocol ip pref 1 handle 101 flower
109         tc qdisc del dev $host2_if ingress
110
111         return $seen
112 }
113
114 v2reportleave_test()
115 {
116         RET=0
117         ip address add dev $h2 $TEST_GROUP/32 autojoin
118         check_err $? "Could not join $TEST_GROUP"
119
120         sleep 5
121         bridge mdb show dev br0 | grep $TEST_GROUP 1>/dev/null
122         check_err $? "IGMPv2 report didn't create mdb entry for $TEST_GROUP"
123
124         mcast_packet_test $TEST_GROUP_MAC 192.0.2.1 $TEST_GROUP $h1 $h2
125         check_fail $? "Traffic to $TEST_GROUP wasn't forwarded"
126
127         log_test "IGMPv2 report $TEST_GROUP"
128
129         RET=0
130         bridge mdb show dev br0 | grep $TEST_GROUP 1>/dev/null
131         check_err $? "mdb entry for $TEST_GROUP is missing"
132
133         ip address del dev $h2 $TEST_GROUP/32
134         check_err $? "Could not leave $TEST_GROUP"
135
136         sleep 5
137         bridge mdb show dev br0 | grep $TEST_GROUP 1>/dev/null
138         check_fail $? "Leave didn't delete mdb entry for $TEST_GROUP"
139
140         mcast_packet_test $TEST_GROUP_MAC 192.0.2.1 $TEST_GROUP $h1 $h2
141         check_err $? "Traffic to $TEST_GROUP was forwarded without mdb entry"
142
143         log_test "IGMPv2 leave $TEST_GROUP"
144 }
145
146 trap cleanup EXIT
147
148 setup_prepare
149 setup_wait
150
151 tests_run
152
153 exit $EXIT_STATUS