6915599c6cdda245c7dea15c2acd643232d8d76a
[openwrt/openwrt.git] / target / linux / generic / patches-4.1 / 645-bridge_multicast_to_unicast.patch
1 --- a/include/linux/if_bridge.h
2 +++ b/include/linux/if_bridge.h
3 @@ -46,6 +46,7 @@ struct br_ip_list {
4 #define BR_LEARNING_SYNC BIT(9)
5 #define BR_PROXYARP_WIFI BIT(10)
6 #define BR_ISOLATE_MODE BIT(11)
7 +#define BR_MULTICAST_TO_UCAST BIT(12)
8
9 extern void brioctl_set(int (*ioctl_hook)(struct net *, unsigned int, void __user *));
10
11 --- a/net/bridge/br_multicast.c
12 +++ b/net/bridge/br_multicast.c
13 @@ -635,7 +635,8 @@ struct net_bridge_port_group *br_multica
14 struct net_bridge_port *port,
15 struct br_ip *group,
16 struct net_bridge_port_group __rcu *next,
17 - unsigned char state)
18 + unsigned char state,
19 + const unsigned char *src)
20 {
21 struct net_bridge_port_group *p;
22
23 @@ -650,12 +651,33 @@ struct net_bridge_port_group *br_multica
24 hlist_add_head(&p->mglist, &port->mglist);
25 setup_timer(&p->timer, br_multicast_port_group_expired,
26 (unsigned long)p);
27 + if ((port->flags & BR_MULTICAST_TO_UCAST) && src) {
28 + memcpy(p->eth_addr, src, ETH_ALEN);
29 + p->unicast = true;
30 + }
31 return p;
32 }
33
34 +static bool br_port_group_equal(struct net_bridge_port_group *p,
35 + struct net_bridge_port *port,
36 + const unsigned char *src)
37 +{
38 + if (p->port != port)
39 + return false;
40 +
41 + if (!p->unicast)
42 + return true;
43 +
44 + if (!src)
45 + return false;
46 +
47 + return ether_addr_equal(src, p->eth_addr);
48 +}
49 +
50 static int br_multicast_add_group(struct net_bridge *br,
51 struct net_bridge_port *port,
52 - struct br_ip *group)
53 + struct br_ip *group,
54 + const unsigned char *src)
55 {
56 struct net_bridge_mdb_entry *mp;
57 struct net_bridge_port_group *p;
58 @@ -682,13 +704,13 @@ static int br_multicast_add_group(struct
59 for (pp = &mp->ports;
60 (p = mlock_dereference(*pp, br)) != NULL;
61 pp = &p->next) {
62 - if (p->port == port)
63 + if (br_port_group_equal(p, port, src))
64 goto found;
65 if ((unsigned long)p->port < (unsigned long)port)
66 break;
67 }
68
69 - p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
70 + p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY, src);
71 if (unlikely(!p))
72 goto err;
73 rcu_assign_pointer(*pp, p);
74 @@ -707,7 +729,7 @@ err:
75 static int br_ip4_multicast_add_group(struct net_bridge *br,
76 struct net_bridge_port *port,
77 __be32 group,
78 - __u16 vid)
79 + __u16 vid, const unsigned char *src)
80 {
81 struct br_ip br_group;
82
83 @@ -718,14 +740,14 @@ static int br_ip4_multicast_add_group(st
84 br_group.proto = htons(ETH_P_IP);
85 br_group.vid = vid;
86
87 - return br_multicast_add_group(br, port, &br_group);
88 + return br_multicast_add_group(br, port, &br_group, src);
89 }
90
91 #if IS_ENABLED(CONFIG_IPV6)
92 static int br_ip6_multicast_add_group(struct net_bridge *br,
93 struct net_bridge_port *port,
94 const struct in6_addr *group,
95 - __u16 vid)
96 + __u16 vid, const unsigned char *src)
97 {
98 struct br_ip br_group;
99
100 @@ -736,7 +758,7 @@ static int br_ip6_multicast_add_group(st
101 br_group.proto = htons(ETH_P_IPV6);
102 br_group.vid = vid;
103
104 - return br_multicast_add_group(br, port, &br_group);
105 + return br_multicast_add_group(br, port, &br_group, src);
106 }
107 #endif
108
109 @@ -966,6 +988,7 @@ static int br_ip4_multicast_igmp3_report
110 struct sk_buff *skb,
111 u16 vid)
112 {
113 + const unsigned char *src;
114 struct igmpv3_report *ih;
115 struct igmpv3_grec *grec;
116 int i;
117 @@ -1009,7 +1032,8 @@ static int br_ip4_multicast_igmp3_report
118 continue;
119 }
120
121 - err = br_ip4_multicast_add_group(br, port, group, vid);
122 + src = eth_hdr(skb)->h_source;
123 + err = br_ip4_multicast_add_group(br, port, group, vid, src);
124 if (err)
125 break;
126 }
127 @@ -1023,6 +1047,7 @@ static int br_ip6_multicast_mld2_report(
128 struct sk_buff *skb,
129 u16 vid)
130 {
131 + const unsigned char *src = eth_hdr(skb)->h_source;
132 struct icmp6hdr *icmp6h;
133 struct mld2_grec *grec;
134 int i;
135 @@ -1071,7 +1096,7 @@ static int br_ip6_multicast_mld2_report(
136 }
137
138 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
139 - vid);
140 + vid, src);
141 if (err)
142 break;
143 }
144 @@ -1407,7 +1432,8 @@ br_multicast_leave_group(struct net_brid
145 struct net_bridge_port *port,
146 struct br_ip *group,
147 struct bridge_mcast_other_query *other_query,
148 - struct bridge_mcast_own_query *own_query)
149 + struct bridge_mcast_own_query *own_query,
150 + const unsigned char *src)
151 {
152 struct net_bridge_mdb_htable *mdb;
153 struct net_bridge_mdb_entry *mp;
154 @@ -1457,7 +1483,7 @@ br_multicast_leave_group(struct net_brid
155 for (pp = &mp->ports;
156 (p = mlock_dereference(*pp, br)) != NULL;
157 pp = &p->next) {
158 - if (p->port != port)
159 + if (!br_port_group_equal(p, port, src))
160 continue;
161
162 rcu_assign_pointer(*pp, p->next);
163 @@ -1491,7 +1517,7 @@ br_multicast_leave_group(struct net_brid
164 for (p = mlock_dereference(mp->ports, br);
165 p != NULL;
166 p = mlock_dereference(p->next, br)) {
167 - if (p->port != port)
168 + if (!br_port_group_equal(p, port, src))
169 continue;
170
171 if (!hlist_unhashed(&p->mglist) &&
172 @@ -1509,8 +1535,8 @@ out:
173
174 static void br_ip4_multicast_leave_group(struct net_bridge *br,
175 struct net_bridge_port *port,
176 - __be32 group,
177 - __u16 vid)
178 + __be32 group, __u16 vid,
179 + const unsigned char *src)
180 {
181 struct br_ip br_group;
182 struct bridge_mcast_own_query *own_query;
183 @@ -1525,14 +1551,14 @@ static void br_ip4_multicast_leave_group
184 br_group.vid = vid;
185
186 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
187 - own_query);
188 + own_query, src);
189 }
190
191 #if IS_ENABLED(CONFIG_IPV6)
192 static void br_ip6_multicast_leave_group(struct net_bridge *br,
193 struct net_bridge_port *port,
194 const struct in6_addr *group,
195 - __u16 vid)
196 + __u16 vid, const unsigned char *src)
197 {
198 struct br_ip br_group;
199 struct bridge_mcast_own_query *own_query;
200 @@ -1547,7 +1573,7 @@ static void br_ip6_multicast_leave_group
201 br_group.vid = vid;
202
203 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
204 - own_query);
205 + own_query, src);
206 }
207 #endif
208
209 @@ -1556,6 +1582,7 @@ static int br_multicast_ipv4_rcv(struct
210 struct sk_buff *skb,
211 u16 vid)
212 {
213 + const unsigned char *src;
214 struct sk_buff *skb2 = skb;
215 const struct iphdr *iph;
216 struct igmphdr *ih;
217 @@ -1624,12 +1651,13 @@ static int br_multicast_ipv4_rcv(struct
218
219 BR_INPUT_SKB_CB(skb)->igmp = 1;
220 ih = igmp_hdr(skb2);
221 + src = eth_hdr(skb)->h_source;
222
223 switch (ih->type) {
224 case IGMP_HOST_MEMBERSHIP_REPORT:
225 case IGMPV2_HOST_MEMBERSHIP_REPORT:
226 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
227 - err = br_ip4_multicast_add_group(br, port, ih->group, vid);
228 + err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
229 break;
230 case IGMPV3_HOST_MEMBERSHIP_REPORT:
231 err = br_ip4_multicast_igmp3_report(br, port, skb2, vid);
232 @@ -1638,7 +1666,7 @@ static int br_multicast_ipv4_rcv(struct
233 err = br_ip4_multicast_query(br, port, skb2, vid);
234 break;
235 case IGMP_HOST_LEAVE_MESSAGE:
236 - br_ip4_multicast_leave_group(br, port, ih->group, vid);
237 + br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
238 break;
239 }
240
241 @@ -1656,6 +1684,7 @@ static int br_multicast_ipv6_rcv(struct
242 struct sk_buff *skb,
243 u16 vid)
244 {
245 + const unsigned char *src;
246 struct sk_buff *skb2;
247 const struct ipv6hdr *ip6h;
248 u8 icmp6_type;
249 @@ -1763,9 +1792,11 @@ static int br_multicast_ipv6_rcv(struct
250 err = -EINVAL;
251 goto out;
252 }
253 + src = eth_hdr(skb)->h_source;
254 mld = (struct mld_msg *)skb_transport_header(skb2);
255 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
256 - err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
257 + err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
258 + src);
259 break;
260 }
261 case ICMPV6_MLD2_REPORT:
262 @@ -1781,8 +1812,9 @@ static int br_multicast_ipv6_rcv(struct
263 err = -EINVAL;
264 goto out;
265 }
266 + src = eth_hdr(skb)->h_source;
267 mld = (struct mld_msg *)skb_transport_header(skb2);
268 - br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
269 + br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
270 }
271 }
272
273 --- a/net/bridge/br_private.h
274 +++ b/net/bridge/br_private.h
275 @@ -113,6 +113,9 @@ struct net_bridge_port_group {
276 struct timer_list timer;
277 struct br_ip addr;
278 unsigned char state;
279 +
280 + unsigned char eth_addr[ETH_ALEN];
281 + bool unicast;
282 };
283
284 struct net_bridge_mdb_entry
285 @@ -481,7 +484,8 @@ void br_multicast_free_pg(struct rcu_hea
286 struct net_bridge_port_group *
287 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
288 struct net_bridge_port_group __rcu *next,
289 - unsigned char state);
290 + unsigned char state,
291 + const unsigned char *src);
292 void br_mdb_init(void);
293 void br_mdb_uninit(void);
294 void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
295 --- a/net/bridge/br_mdb.c
296 +++ b/net/bridge/br_mdb.c
297 @@ -343,7 +343,7 @@ static int br_mdb_add_group(struct net_b
298 break;
299 }
300
301 - p = br_multicast_new_port_group(port, group, *pp, state);
302 + p = br_multicast_new_port_group(port, group, *pp, state, NULL);
303 if (unlikely(!p))
304 return -ENOMEM;
305 rcu_assign_pointer(*pp, p);
306 --- a/net/bridge/br_forward.c
307 +++ b/net/bridge/br_forward.c
308 @@ -169,6 +169,29 @@ out:
309 return p;
310 }
311
312 +static struct net_bridge_port *maybe_deliver_addr(
313 + struct net_bridge_port *prev, struct net_bridge_port *p,
314 + struct sk_buff *skb, const unsigned char *addr,
315 + void (*__packet_hook)(const struct net_bridge_port *p,
316 + struct sk_buff *skb))
317 +{
318 + struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
319 +
320 + if (!should_deliver(p, skb))
321 + return prev;
322 +
323 + skb = skb_copy(skb, GFP_ATOMIC);
324 + if (!skb) {
325 + dev->stats.tx_dropped++;
326 + return prev;
327 + }
328 +
329 + memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN);
330 + __packet_hook(p, skb);
331 +
332 + return prev;
333 +}
334 +
335 /* called under bridge lock */
336 static void br_flood(struct net_bridge *br, struct sk_buff *skb,
337 struct sk_buff *skb0,
338 @@ -241,6 +264,7 @@ static void br_multicast_flood(struct ne
339 struct net_bridge_port *prev = NULL;
340 struct net_bridge_port_group *p;
341 struct hlist_node *rp;
342 + const unsigned char *addr;
343
344 rp = rcu_dereference(hlist_first_rcu(&br->router_list));
345 p = mdst ? rcu_dereference(mdst->ports) : NULL;
346 @@ -251,10 +275,19 @@ static void br_multicast_flood(struct ne
347 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
348 NULL;
349
350 - port = (unsigned long)lport > (unsigned long)rport ?
351 - lport : rport;
352 -
353 - prev = maybe_deliver(prev, port, skb, __packet_hook);
354 + if ((unsigned long)lport > (unsigned long)rport) {
355 + port = lport;
356 + addr = p->unicast ? p->eth_addr : NULL;
357 + } else {
358 + port = rport;
359 + addr = NULL;
360 + }
361 +
362 + if (addr)
363 + prev = maybe_deliver_addr(prev, port, skb, addr,
364 + __packet_hook);
365 + else
366 + prev = maybe_deliver(prev, port, skb, __packet_hook);
367 if (IS_ERR(prev))
368 goto out;
369
370 --- a/net/bridge/br_sysfs_if.c
371 +++ b/net/bridge/br_sysfs_if.c
372 @@ -204,6 +204,7 @@ static BRPORT_ATTR(multicast_router, S_I
373 store_multicast_router);
374
375 BRPORT_ATTR_FLAG(multicast_fast_leave, BR_MULTICAST_FAST_LEAVE);
376 +BRPORT_ATTR_FLAG(multicast_to_unicast, BR_MULTICAST_TO_UCAST);
377 #endif
378
379 static const struct brport_attribute *brport_attrs[] = {
380 @@ -230,6 +231,7 @@ static const struct brport_attribute *br
381 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
382 &brport_attr_multicast_router,
383 &brport_attr_multicast_fast_leave,
384 + &brport_attr_multicast_to_unicast,
385 #endif
386 &brport_attr_proxyarp,
387 &brport_attr_proxyarp_wifi,