]>
Commit | Line | Data |
---|---|---|
eb1d1641 HX |
1 | /* |
2 | * Bridge multicast support. | |
3 | * | |
4 | * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the Free | |
8 | * Software Foundation; either version 2 of the License, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <linux/err.h> | |
14 | #include <linux/if_ether.h> | |
15 | #include <linux/igmp.h> | |
16 | #include <linux/jhash.h> | |
17 | #include <linux/kernel.h> | |
b195167f | 18 | #include <linux/log2.h> |
eb1d1641 HX |
19 | #include <linux/netdevice.h> |
20 | #include <linux/netfilter_bridge.h> | |
21 | #include <linux/random.h> | |
22 | #include <linux/rculist.h> | |
23 | #include <linux/skbuff.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/timer.h> | |
26 | #include <net/ip.h> | |
dfd56b8b | 27 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
28 | #include <net/ipv6.h> |
29 | #include <net/mld.h> | |
30 | #include <net/addrconf.h> | |
d4c4f07d | 31 | #include <net/ip6_checksum.h> |
08b202b6 | 32 | #endif |
eb1d1641 HX |
33 | |
34 | #include "br_private.h" | |
35 | ||
e8051688 ED |
36 | #define mlock_dereference(X, br) \ |
37 | rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock)) | |
38 | ||
c83b8fab | 39 | static void br_multicast_start_querier(struct net_bridge *br); |
2ce297fc | 40 | unsigned int br_mdb_rehash_seq; |
c83b8fab | 41 | |
dfd56b8b | 42 | #if IS_ENABLED(CONFIG_IPV6) |
e4de9f9e | 43 | static inline int ipv6_is_transient_multicast(const struct in6_addr *addr) |
eb1d1641 | 44 | { |
e4de9f9e | 45 | if (ipv6_addr_is_multicast(addr) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr)) |
08b202b6 YH |
46 | return 1; |
47 | return 0; | |
48 | } | |
49 | #endif | |
50 | ||
8ef2a9a5 YH |
51 | static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) |
52 | { | |
53 | if (a->proto != b->proto) | |
54 | return 0; | |
55 | switch (a->proto) { | |
56 | case htons(ETH_P_IP): | |
57 | return a->u.ip4 == b->u.ip4; | |
dfd56b8b | 58 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
59 | case htons(ETH_P_IPV6): |
60 | return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); | |
61 | #endif | |
8ef2a9a5 YH |
62 | } |
63 | return 0; | |
64 | } | |
65 | ||
66 | static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) | |
eb1d1641 | 67 | { |
0eae88f3 | 68 | return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1); |
eb1d1641 HX |
69 | } |
70 | ||
dfd56b8b | 71 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
72 | static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, |
73 | const struct in6_addr *ip) | |
74 | { | |
75 | return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1); | |
76 | } | |
77 | #endif | |
78 | ||
8ef2a9a5 YH |
79 | static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, |
80 | struct br_ip *ip) | |
81 | { | |
82 | switch (ip->proto) { | |
83 | case htons(ETH_P_IP): | |
84 | return __br_ip4_hash(mdb, ip->u.ip4); | |
dfd56b8b | 85 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
86 | case htons(ETH_P_IPV6): |
87 | return __br_ip6_hash(mdb, &ip->u.ip6); | |
88 | #endif | |
8ef2a9a5 YH |
89 | } |
90 | return 0; | |
eb1d1641 HX |
91 | } |
92 | ||
93 | static struct net_bridge_mdb_entry *__br_mdb_ip_get( | |
8ef2a9a5 | 94 | struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) |
eb1d1641 HX |
95 | { |
96 | struct net_bridge_mdb_entry *mp; | |
97 | struct hlist_node *p; | |
98 | ||
49f5fcfd | 99 | hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { |
8ef2a9a5 | 100 | if (br_ip_equal(&mp->addr, dst)) |
eb1d1641 HX |
101 | return mp; |
102 | } | |
103 | ||
104 | return NULL; | |
105 | } | |
106 | ||
7f285fa7 HX |
107 | static struct net_bridge_mdb_entry *br_mdb_ip_get( |
108 | struct net_bridge_mdb_htable *mdb, struct br_ip *dst) | |
109 | { | |
110 | if (!mdb) | |
111 | return NULL; | |
112 | ||
113 | return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); | |
114 | } | |
115 | ||
8ef2a9a5 | 116 | static struct net_bridge_mdb_entry *br_mdb_ip4_get( |
eb1d1641 HX |
117 | struct net_bridge_mdb_htable *mdb, __be32 dst) |
118 | { | |
8ef2a9a5 YH |
119 | struct br_ip br_dst; |
120 | ||
121 | br_dst.u.ip4 = dst; | |
122 | br_dst.proto = htons(ETH_P_IP); | |
0821ec55 | 123 | |
7f285fa7 | 124 | return br_mdb_ip_get(mdb, &br_dst); |
8ef2a9a5 YH |
125 | } |
126 | ||
dfd56b8b | 127 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
128 | static struct net_bridge_mdb_entry *br_mdb_ip6_get( |
129 | struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst) | |
130 | { | |
131 | struct br_ip br_dst; | |
0821ec55 | 132 | |
4e3fd7a0 | 133 | br_dst.u.ip6 = *dst; |
08b202b6 YH |
134 | br_dst.proto = htons(ETH_P_IPV6); |
135 | ||
7f285fa7 | 136 | return br_mdb_ip_get(mdb, &br_dst); |
08b202b6 YH |
137 | } |
138 | #endif | |
139 | ||
eb1d1641 HX |
140 | struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, |
141 | struct sk_buff *skb) | |
142 | { | |
e8051688 | 143 | struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); |
8ef2a9a5 YH |
144 | struct br_ip ip; |
145 | ||
7f285fa7 | 146 | if (br->multicast_disabled) |
eb1d1641 HX |
147 | return NULL; |
148 | ||
8ef2a9a5 | 149 | if (BR_INPUT_SKB_CB(skb)->igmp) |
eb1d1641 HX |
150 | return NULL; |
151 | ||
8ef2a9a5 YH |
152 | ip.proto = skb->protocol; |
153 | ||
eb1d1641 HX |
154 | switch (skb->protocol) { |
155 | case htons(ETH_P_IP): | |
8ef2a9a5 YH |
156 | ip.u.ip4 = ip_hdr(skb)->daddr; |
157 | break; | |
dfd56b8b | 158 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 | 159 | case htons(ETH_P_IPV6): |
4e3fd7a0 | 160 | ip.u.ip6 = ipv6_hdr(skb)->daddr; |
08b202b6 YH |
161 | break; |
162 | #endif | |
8ef2a9a5 YH |
163 | default: |
164 | return NULL; | |
eb1d1641 HX |
165 | } |
166 | ||
8ef2a9a5 | 167 | return br_mdb_ip_get(mdb, &ip); |
eb1d1641 HX |
168 | } |
169 | ||
170 | static void br_mdb_free(struct rcu_head *head) | |
171 | { | |
172 | struct net_bridge_mdb_htable *mdb = | |
173 | container_of(head, struct net_bridge_mdb_htable, rcu); | |
174 | struct net_bridge_mdb_htable *old = mdb->old; | |
175 | ||
176 | mdb->old = NULL; | |
177 | kfree(old->mhash); | |
178 | kfree(old); | |
179 | } | |
180 | ||
181 | static int br_mdb_copy(struct net_bridge_mdb_htable *new, | |
182 | struct net_bridge_mdb_htable *old, | |
183 | int elasticity) | |
184 | { | |
185 | struct net_bridge_mdb_entry *mp; | |
186 | struct hlist_node *p; | |
187 | int maxlen; | |
188 | int len; | |
189 | int i; | |
190 | ||
191 | for (i = 0; i < old->max; i++) | |
192 | hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) | |
193 | hlist_add_head(&mp->hlist[new->ver], | |
8ef2a9a5 | 194 | &new->mhash[br_ip_hash(new, &mp->addr)]); |
eb1d1641 HX |
195 | |
196 | if (!elasticity) | |
197 | return 0; | |
198 | ||
199 | maxlen = 0; | |
200 | for (i = 0; i < new->max; i++) { | |
201 | len = 0; | |
202 | hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) | |
203 | len++; | |
204 | if (len > maxlen) | |
205 | maxlen = len; | |
206 | } | |
207 | ||
208 | return maxlen > elasticity ? -EINVAL : 0; | |
209 | } | |
210 | ||
211 | static void br_multicast_free_pg(struct rcu_head *head) | |
212 | { | |
213 | struct net_bridge_port_group *p = | |
214 | container_of(head, struct net_bridge_port_group, rcu); | |
215 | ||
216 | kfree(p); | |
217 | } | |
218 | ||
219 | static void br_multicast_free_group(struct rcu_head *head) | |
220 | { | |
221 | struct net_bridge_mdb_entry *mp = | |
222 | container_of(head, struct net_bridge_mdb_entry, rcu); | |
223 | ||
224 | kfree(mp); | |
225 | } | |
226 | ||
227 | static void br_multicast_group_expired(unsigned long data) | |
228 | { | |
229 | struct net_bridge_mdb_entry *mp = (void *)data; | |
230 | struct net_bridge *br = mp->br; | |
231 | struct net_bridge_mdb_htable *mdb; | |
232 | ||
233 | spin_lock(&br->multicast_lock); | |
234 | if (!netif_running(br->dev) || timer_pending(&mp->timer)) | |
235 | goto out; | |
236 | ||
8a870178 | 237 | mp->mglist = false; |
eb1d1641 HX |
238 | |
239 | if (mp->ports) | |
240 | goto out; | |
241 | ||
e8051688 ED |
242 | mdb = mlock_dereference(br->mdb, br); |
243 | ||
eb1d1641 HX |
244 | hlist_del_rcu(&mp->hlist[mdb->ver]); |
245 | mdb->size--; | |
246 | ||
eb1d1641 HX |
247 | call_rcu_bh(&mp->rcu, br_multicast_free_group); |
248 | ||
249 | out: | |
250 | spin_unlock(&br->multicast_lock); | |
251 | } | |
252 | ||
253 | static void br_multicast_del_pg(struct net_bridge *br, | |
254 | struct net_bridge_port_group *pg) | |
255 | { | |
e8051688 | 256 | struct net_bridge_mdb_htable *mdb; |
eb1d1641 HX |
257 | struct net_bridge_mdb_entry *mp; |
258 | struct net_bridge_port_group *p; | |
e8051688 ED |
259 | struct net_bridge_port_group __rcu **pp; |
260 | ||
261 | mdb = mlock_dereference(br->mdb, br); | |
eb1d1641 | 262 | |
8ef2a9a5 | 263 | mp = br_mdb_ip_get(mdb, &pg->addr); |
eb1d1641 HX |
264 | if (WARN_ON(!mp)) |
265 | return; | |
266 | ||
e8051688 ED |
267 | for (pp = &mp->ports; |
268 | (p = mlock_dereference(*pp, br)) != NULL; | |
269 | pp = &p->next) { | |
eb1d1641 HX |
270 | if (p != pg) |
271 | continue; | |
272 | ||
83f6a740 | 273 | rcu_assign_pointer(*pp, p->next); |
eb1d1641 HX |
274 | hlist_del_init(&p->mglist); |
275 | del_timer(&p->timer); | |
eb1d1641 HX |
276 | call_rcu_bh(&p->rcu, br_multicast_free_pg); |
277 | ||
8a870178 | 278 | if (!mp->ports && !mp->mglist && |
eb1d1641 HX |
279 | netif_running(br->dev)) |
280 | mod_timer(&mp->timer, jiffies); | |
281 | ||
282 | return; | |
283 | } | |
284 | ||
285 | WARN_ON(1); | |
286 | } | |
287 | ||
288 | static void br_multicast_port_group_expired(unsigned long data) | |
289 | { | |
290 | struct net_bridge_port_group *pg = (void *)data; | |
291 | struct net_bridge *br = pg->port->br; | |
292 | ||
293 | spin_lock(&br->multicast_lock); | |
294 | if (!netif_running(br->dev) || timer_pending(&pg->timer) || | |
295 | hlist_unhashed(&pg->mglist)) | |
296 | goto out; | |
297 | ||
298 | br_multicast_del_pg(br, pg); | |
299 | ||
300 | out: | |
301 | spin_unlock(&br->multicast_lock); | |
302 | } | |
303 | ||
e8051688 | 304 | static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, |
eb1d1641 HX |
305 | int elasticity) |
306 | { | |
e8051688 | 307 | struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); |
eb1d1641 HX |
308 | struct net_bridge_mdb_htable *mdb; |
309 | int err; | |
310 | ||
311 | mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); | |
312 | if (!mdb) | |
313 | return -ENOMEM; | |
314 | ||
315 | mdb->max = max; | |
316 | mdb->old = old; | |
317 | ||
318 | mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); | |
319 | if (!mdb->mhash) { | |
320 | kfree(mdb); | |
321 | return -ENOMEM; | |
322 | } | |
323 | ||
324 | mdb->size = old ? old->size : 0; | |
325 | mdb->ver = old ? old->ver ^ 1 : 0; | |
326 | ||
327 | if (!old || elasticity) | |
328 | get_random_bytes(&mdb->secret, sizeof(mdb->secret)); | |
329 | else | |
330 | mdb->secret = old->secret; | |
331 | ||
332 | if (!old) | |
333 | goto out; | |
334 | ||
335 | err = br_mdb_copy(mdb, old, elasticity); | |
336 | if (err) { | |
337 | kfree(mdb->mhash); | |
338 | kfree(mdb); | |
339 | return err; | |
340 | } | |
341 | ||
2ce297fc | 342 | br_mdb_rehash_seq++; |
eb1d1641 HX |
343 | call_rcu_bh(&mdb->rcu, br_mdb_free); |
344 | ||
345 | out: | |
346 | rcu_assign_pointer(*mdbp, mdb); | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
8ef2a9a5 YH |
351 | static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, |
352 | __be32 group) | |
eb1d1641 HX |
353 | { |
354 | struct sk_buff *skb; | |
355 | struct igmphdr *ih; | |
356 | struct ethhdr *eth; | |
357 | struct iphdr *iph; | |
358 | ||
359 | skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + | |
360 | sizeof(*ih) + 4); | |
361 | if (!skb) | |
362 | goto out; | |
363 | ||
364 | skb->protocol = htons(ETH_P_IP); | |
365 | ||
366 | skb_reset_mac_header(skb); | |
367 | eth = eth_hdr(skb); | |
368 | ||
369 | memcpy(eth->h_source, br->dev->dev_addr, 6); | |
370 | eth->h_dest[0] = 1; | |
371 | eth->h_dest[1] = 0; | |
372 | eth->h_dest[2] = 0x5e; | |
373 | eth->h_dest[3] = 0; | |
374 | eth->h_dest[4] = 0; | |
375 | eth->h_dest[5] = 1; | |
376 | eth->h_proto = htons(ETH_P_IP); | |
377 | skb_put(skb, sizeof(*eth)); | |
378 | ||
379 | skb_set_network_header(skb, skb->len); | |
380 | iph = ip_hdr(skb); | |
381 | ||
382 | iph->version = 4; | |
383 | iph->ihl = 6; | |
384 | iph->tos = 0xc0; | |
385 | iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); | |
386 | iph->id = 0; | |
387 | iph->frag_off = htons(IP_DF); | |
388 | iph->ttl = 1; | |
389 | iph->protocol = IPPROTO_IGMP; | |
390 | iph->saddr = 0; | |
391 | iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); | |
392 | ((u8 *)&iph[1])[0] = IPOPT_RA; | |
393 | ((u8 *)&iph[1])[1] = 4; | |
394 | ((u8 *)&iph[1])[2] = 0; | |
395 | ((u8 *)&iph[1])[3] = 0; | |
396 | ip_send_check(iph); | |
397 | skb_put(skb, 24); | |
398 | ||
399 | skb_set_transport_header(skb, skb->len); | |
400 | ih = igmp_hdr(skb); | |
401 | ih->type = IGMP_HOST_MEMBERSHIP_QUERY; | |
402 | ih->code = (group ? br->multicast_last_member_interval : | |
403 | br->multicast_query_response_interval) / | |
404 | (HZ / IGMP_TIMER_SCALE); | |
405 | ih->group = group; | |
406 | ih->csum = 0; | |
407 | ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); | |
408 | skb_put(skb, sizeof(*ih)); | |
409 | ||
410 | __skb_pull(skb, sizeof(*eth)); | |
411 | ||
412 | out: | |
413 | return skb; | |
414 | } | |
415 | ||
dfd56b8b | 416 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 | 417 | static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, |
b71d1d42 | 418 | const struct in6_addr *group) |
08b202b6 YH |
419 | { |
420 | struct sk_buff *skb; | |
421 | struct ipv6hdr *ip6h; | |
422 | struct mld_msg *mldq; | |
423 | struct ethhdr *eth; | |
424 | u8 *hopopt; | |
425 | unsigned long interval; | |
426 | ||
427 | skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + | |
428 | 8 + sizeof(*mldq)); | |
429 | if (!skb) | |
430 | goto out; | |
431 | ||
432 | skb->protocol = htons(ETH_P_IPV6); | |
433 | ||
434 | /* Ethernet header */ | |
435 | skb_reset_mac_header(skb); | |
436 | eth = eth_hdr(skb); | |
437 | ||
438 | memcpy(eth->h_source, br->dev->dev_addr, 6); | |
08b202b6 YH |
439 | eth->h_proto = htons(ETH_P_IPV6); |
440 | skb_put(skb, sizeof(*eth)); | |
441 | ||
442 | /* IPv6 header + HbH option */ | |
443 | skb_set_network_header(skb, skb->len); | |
444 | ip6h = ipv6_hdr(skb); | |
445 | ||
446 | *(__force __be32 *)ip6h = htonl(0x60000000); | |
76d66158 | 447 | ip6h->payload_len = htons(8 + sizeof(*mldq)); |
08b202b6 YH |
448 | ip6h->nexthdr = IPPROTO_HOPOPTS; |
449 | ip6h->hop_limit = 1; | |
a7bff75b | 450 | ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); |
d1d81d4c UW |
451 | if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, |
452 | &ip6h->saddr)) { | |
453 | kfree_skb(skb); | |
454 | return NULL; | |
455 | } | |
36cff5a1 | 456 | ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); |
08b202b6 YH |
457 | |
458 | hopopt = (u8 *)(ip6h + 1); | |
459 | hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ | |
460 | hopopt[1] = 0; /* length of HbH */ | |
461 | hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ | |
462 | hopopt[3] = 2; /* Length of RA Option */ | |
463 | hopopt[4] = 0; /* Type = 0x0000 (MLD) */ | |
464 | hopopt[5] = 0; | |
1de5a71c EZ |
465 | hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ |
466 | hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ | |
08b202b6 YH |
467 | |
468 | skb_put(skb, sizeof(*ip6h) + 8); | |
469 | ||
470 | /* ICMPv6 */ | |
471 | skb_set_transport_header(skb, skb->len); | |
472 | mldq = (struct mld_msg *) icmp6_hdr(skb); | |
473 | ||
474 | interval = ipv6_addr_any(group) ? br->multicast_last_member_interval : | |
475 | br->multicast_query_response_interval; | |
476 | ||
477 | mldq->mld_type = ICMPV6_MGM_QUERY; | |
478 | mldq->mld_code = 0; | |
479 | mldq->mld_cksum = 0; | |
480 | mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); | |
481 | mldq->mld_reserved = 0; | |
4e3fd7a0 | 482 | mldq->mld_mca = *group; |
08b202b6 YH |
483 | |
484 | /* checksum */ | |
485 | mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, | |
486 | sizeof(*mldq), IPPROTO_ICMPV6, | |
487 | csum_partial(mldq, | |
488 | sizeof(*mldq), 0)); | |
489 | skb_put(skb, sizeof(*mldq)); | |
490 | ||
491 | __skb_pull(skb, sizeof(*eth)); | |
492 | ||
493 | out: | |
494 | return skb; | |
495 | } | |
496 | #endif | |
497 | ||
8ef2a9a5 YH |
498 | static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, |
499 | struct br_ip *addr) | |
500 | { | |
501 | switch (addr->proto) { | |
502 | case htons(ETH_P_IP): | |
503 | return br_ip4_multicast_alloc_query(br, addr->u.ip4); | |
dfd56b8b | 504 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
505 | case htons(ETH_P_IPV6): |
506 | return br_ip6_multicast_alloc_query(br, &addr->u.ip6); | |
507 | #endif | |
8ef2a9a5 YH |
508 | } |
509 | return NULL; | |
510 | } | |
511 | ||
eb1d1641 | 512 | static struct net_bridge_mdb_entry *br_multicast_get_group( |
8ef2a9a5 YH |
513 | struct net_bridge *br, struct net_bridge_port *port, |
514 | struct br_ip *group, int hash) | |
eb1d1641 | 515 | { |
e8051688 | 516 | struct net_bridge_mdb_htable *mdb; |
eb1d1641 HX |
517 | struct net_bridge_mdb_entry *mp; |
518 | struct hlist_node *p; | |
95c96174 ED |
519 | unsigned int count = 0; |
520 | unsigned int max; | |
eb1d1641 HX |
521 | int elasticity; |
522 | int err; | |
523 | ||
e8051688 | 524 | mdb = rcu_dereference_protected(br->mdb, 1); |
eb1d1641 HX |
525 | hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { |
526 | count++; | |
8ef2a9a5 | 527 | if (unlikely(br_ip_equal(group, &mp->addr))) |
eb1d1641 | 528 | return mp; |
eb1d1641 HX |
529 | } |
530 | ||
531 | elasticity = 0; | |
532 | max = mdb->max; | |
533 | ||
534 | if (unlikely(count > br->hash_elasticity && count)) { | |
535 | if (net_ratelimit()) | |
28a16c97 | 536 | br_info(br, "Multicast hash table " |
537 | "chain limit reached: %s\n", | |
538 | port ? port->dev->name : br->dev->name); | |
eb1d1641 HX |
539 | |
540 | elasticity = br->hash_elasticity; | |
541 | } | |
542 | ||
543 | if (mdb->size >= max) { | |
544 | max *= 2; | |
036be6db TG |
545 | if (unlikely(max > br->hash_max)) { |
546 | br_warn(br, "Multicast hash table maximum of %d " | |
547 | "reached, disabling snooping: %s\n", | |
548 | br->hash_max, | |
549 | port ? port->dev->name : br->dev->name); | |
eb1d1641 HX |
550 | err = -E2BIG; |
551 | disable: | |
552 | br->multicast_disabled = 1; | |
553 | goto err; | |
554 | } | |
555 | } | |
556 | ||
557 | if (max > mdb->max || elasticity) { | |
558 | if (mdb->old) { | |
559 | if (net_ratelimit()) | |
28a16c97 | 560 | br_info(br, "Multicast hash table " |
561 | "on fire: %s\n", | |
562 | port ? port->dev->name : br->dev->name); | |
eb1d1641 HX |
563 | err = -EEXIST; |
564 | goto err; | |
565 | } | |
566 | ||
567 | err = br_mdb_rehash(&br->mdb, max, elasticity); | |
568 | if (err) { | |
28a16c97 | 569 | br_warn(br, "Cannot rehash multicast " |
570 | "hash table, disabling snooping: %s, %d, %d\n", | |
571 | port ? port->dev->name : br->dev->name, | |
572 | mdb->size, err); | |
eb1d1641 HX |
573 | goto disable; |
574 | } | |
575 | ||
576 | err = -EAGAIN; | |
577 | goto err; | |
578 | } | |
579 | ||
580 | return NULL; | |
581 | ||
582 | err: | |
583 | mp = ERR_PTR(err); | |
584 | return mp; | |
585 | } | |
586 | ||
587 | static struct net_bridge_mdb_entry *br_multicast_new_group( | |
8ef2a9a5 YH |
588 | struct net_bridge *br, struct net_bridge_port *port, |
589 | struct br_ip *group) | |
eb1d1641 | 590 | { |
e8051688 | 591 | struct net_bridge_mdb_htable *mdb; |
eb1d1641 HX |
592 | struct net_bridge_mdb_entry *mp; |
593 | int hash; | |
4c0833bc | 594 | int err; |
eb1d1641 | 595 | |
e8051688 | 596 | mdb = rcu_dereference_protected(br->mdb, 1); |
eb1d1641 | 597 | if (!mdb) { |
4c0833bc TK |
598 | err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); |
599 | if (err) | |
600 | return ERR_PTR(err); | |
eb1d1641 HX |
601 | goto rehash; |
602 | } | |
603 | ||
604 | hash = br_ip_hash(mdb, group); | |
605 | mp = br_multicast_get_group(br, port, group, hash); | |
606 | switch (PTR_ERR(mp)) { | |
607 | case 0: | |
608 | break; | |
609 | ||
610 | case -EAGAIN: | |
611 | rehash: | |
e8051688 | 612 | mdb = rcu_dereference_protected(br->mdb, 1); |
eb1d1641 HX |
613 | hash = br_ip_hash(mdb, group); |
614 | break; | |
615 | ||
616 | default: | |
617 | goto out; | |
618 | } | |
619 | ||
620 | mp = kzalloc(sizeof(*mp), GFP_ATOMIC); | |
621 | if (unlikely(!mp)) | |
4c0833bc | 622 | return ERR_PTR(-ENOMEM); |
eb1d1641 HX |
623 | |
624 | mp->br = br; | |
8ef2a9a5 | 625 | mp->addr = *group; |
eb1d1641 HX |
626 | setup_timer(&mp->timer, br_multicast_group_expired, |
627 | (unsigned long)mp); | |
eb1d1641 HX |
628 | |
629 | hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); | |
630 | mdb->size++; | |
631 | ||
632 | out: | |
633 | return mp; | |
634 | } | |
635 | ||
636 | static int br_multicast_add_group(struct net_bridge *br, | |
8ef2a9a5 YH |
637 | struct net_bridge_port *port, |
638 | struct br_ip *group) | |
eb1d1641 HX |
639 | { |
640 | struct net_bridge_mdb_entry *mp; | |
641 | struct net_bridge_port_group *p; | |
e8051688 | 642 | struct net_bridge_port_group __rcu **pp; |
eb1d1641 HX |
643 | unsigned long now = jiffies; |
644 | int err; | |
645 | ||
eb1d1641 HX |
646 | spin_lock(&br->multicast_lock); |
647 | if (!netif_running(br->dev) || | |
648 | (port && port->state == BR_STATE_DISABLED)) | |
649 | goto out; | |
650 | ||
651 | mp = br_multicast_new_group(br, port, group); | |
652 | err = PTR_ERR(mp); | |
4c0833bc | 653 | if (IS_ERR(mp)) |
eb1d1641 HX |
654 | goto err; |
655 | ||
656 | if (!port) { | |
8a870178 | 657 | mp->mglist = true; |
eb1d1641 HX |
658 | mod_timer(&mp->timer, now + br->multicast_membership_interval); |
659 | goto out; | |
660 | } | |
661 | ||
e8051688 ED |
662 | for (pp = &mp->ports; |
663 | (p = mlock_dereference(*pp, br)) != NULL; | |
664 | pp = &p->next) { | |
eb1d1641 HX |
665 | if (p->port == port) |
666 | goto found; | |
667 | if ((unsigned long)p->port < (unsigned long)port) | |
668 | break; | |
669 | } | |
670 | ||
671 | p = kzalloc(sizeof(*p), GFP_ATOMIC); | |
672 | err = -ENOMEM; | |
673 | if (unlikely(!p)) | |
674 | goto err; | |
675 | ||
8ef2a9a5 | 676 | p->addr = *group; |
eb1d1641 HX |
677 | p->port = port; |
678 | p->next = *pp; | |
679 | hlist_add_head(&p->mglist, &port->mglist); | |
680 | setup_timer(&p->timer, br_multicast_port_group_expired, | |
681 | (unsigned long)p); | |
eb1d1641 HX |
682 | |
683 | rcu_assign_pointer(*pp, p); | |
37a393bc | 684 | br_mdb_notify(br->dev, port, group, RTM_NEWMDB); |
eb1d1641 HX |
685 | |
686 | found: | |
687 | mod_timer(&p->timer, now + br->multicast_membership_interval); | |
688 | out: | |
689 | err = 0; | |
690 | ||
691 | err: | |
692 | spin_unlock(&br->multicast_lock); | |
693 | return err; | |
694 | } | |
695 | ||
8ef2a9a5 YH |
696 | static int br_ip4_multicast_add_group(struct net_bridge *br, |
697 | struct net_bridge_port *port, | |
698 | __be32 group) | |
699 | { | |
700 | struct br_ip br_group; | |
701 | ||
702 | if (ipv4_is_local_multicast(group)) | |
703 | return 0; | |
704 | ||
705 | br_group.u.ip4 = group; | |
706 | br_group.proto = htons(ETH_P_IP); | |
707 | ||
708 | return br_multicast_add_group(br, port, &br_group); | |
709 | } | |
710 | ||
dfd56b8b | 711 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
712 | static int br_ip6_multicast_add_group(struct net_bridge *br, |
713 | struct net_bridge_port *port, | |
714 | const struct in6_addr *group) | |
715 | { | |
716 | struct br_ip br_group; | |
717 | ||
e4de9f9e | 718 | if (!ipv6_is_transient_multicast(group)) |
08b202b6 YH |
719 | return 0; |
720 | ||
4e3fd7a0 | 721 | br_group.u.ip6 = *group; |
9cc6e0c4 | 722 | br_group.proto = htons(ETH_P_IPV6); |
08b202b6 YH |
723 | |
724 | return br_multicast_add_group(br, port, &br_group); | |
725 | } | |
726 | #endif | |
727 | ||
eb1d1641 HX |
728 | static void br_multicast_router_expired(unsigned long data) |
729 | { | |
730 | struct net_bridge_port *port = (void *)data; | |
731 | struct net_bridge *br = port->br; | |
732 | ||
733 | spin_lock(&br->multicast_lock); | |
734 | if (port->multicast_router != 1 || | |
735 | timer_pending(&port->multicast_router_timer) || | |
736 | hlist_unhashed(&port->rlist)) | |
737 | goto out; | |
738 | ||
739 | hlist_del_init_rcu(&port->rlist); | |
740 | ||
741 | out: | |
742 | spin_unlock(&br->multicast_lock); | |
743 | } | |
744 | ||
745 | static void br_multicast_local_router_expired(unsigned long data) | |
746 | { | |
747 | } | |
748 | ||
c83b8fab HX |
749 | static void br_multicast_querier_expired(unsigned long data) |
750 | { | |
bb63f1f8 | 751 | struct net_bridge *br = (void *)data; |
c83b8fab HX |
752 | |
753 | spin_lock(&br->multicast_lock); | |
754 | if (!netif_running(br->dev) || br->multicast_disabled) | |
755 | goto out; | |
756 | ||
757 | br_multicast_start_querier(br); | |
758 | ||
759 | out: | |
760 | spin_unlock(&br->multicast_lock); | |
761 | } | |
762 | ||
8ef2a9a5 YH |
763 | static void __br_multicast_send_query(struct net_bridge *br, |
764 | struct net_bridge_port *port, | |
765 | struct br_ip *ip) | |
eb1d1641 | 766 | { |
eb1d1641 HX |
767 | struct sk_buff *skb; |
768 | ||
8ef2a9a5 | 769 | skb = br_multicast_alloc_query(br, ip); |
eb1d1641 | 770 | if (!skb) |
8ef2a9a5 | 771 | return; |
eb1d1641 HX |
772 | |
773 | if (port) { | |
774 | __skb_push(skb, sizeof(struct ethhdr)); | |
775 | skb->dev = port->dev; | |
713aefa3 | 776 | NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, |
eb1d1641 HX |
777 | dev_queue_xmit); |
778 | } else | |
779 | netif_rx(skb); | |
8ef2a9a5 YH |
780 | } |
781 | ||
782 | static void br_multicast_send_query(struct net_bridge *br, | |
783 | struct net_bridge_port *port, u32 sent) | |
784 | { | |
785 | unsigned long time; | |
786 | struct br_ip br_group; | |
787 | ||
788 | if (!netif_running(br->dev) || br->multicast_disabled || | |
c5c23260 | 789 | !br->multicast_querier || |
8ef2a9a5 YH |
790 | timer_pending(&br->multicast_querier_timer)) |
791 | return; | |
792 | ||
08b202b6 YH |
793 | memset(&br_group.u, 0, sizeof(br_group.u)); |
794 | ||
8ef2a9a5 | 795 | br_group.proto = htons(ETH_P_IP); |
08b202b6 | 796 | __br_multicast_send_query(br, port, &br_group); |
8ef2a9a5 | 797 | |
dfd56b8b | 798 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 | 799 | br_group.proto = htons(ETH_P_IPV6); |
8ef2a9a5 | 800 | __br_multicast_send_query(br, port, &br_group); |
08b202b6 | 801 | #endif |
eb1d1641 | 802 | |
eb1d1641 HX |
803 | time = jiffies; |
804 | time += sent < br->multicast_startup_query_count ? | |
805 | br->multicast_startup_query_interval : | |
806 | br->multicast_query_interval; | |
807 | mod_timer(port ? &port->multicast_query_timer : | |
808 | &br->multicast_query_timer, time); | |
809 | } | |
810 | ||
811 | static void br_multicast_port_query_expired(unsigned long data) | |
812 | { | |
813 | struct net_bridge_port *port = (void *)data; | |
814 | struct net_bridge *br = port->br; | |
815 | ||
816 | spin_lock(&br->multicast_lock); | |
02a780c0 DC |
817 | if (port->state == BR_STATE_DISABLED || |
818 | port->state == BR_STATE_BLOCKING) | |
eb1d1641 HX |
819 | goto out; |
820 | ||
821 | if (port->multicast_startup_queries_sent < | |
822 | br->multicast_startup_query_count) | |
823 | port->multicast_startup_queries_sent++; | |
824 | ||
825 | br_multicast_send_query(port->br, port, | |
826 | port->multicast_startup_queries_sent); | |
827 | ||
828 | out: | |
829 | spin_unlock(&br->multicast_lock); | |
830 | } | |
831 | ||
832 | void br_multicast_add_port(struct net_bridge_port *port) | |
833 | { | |
834 | port->multicast_router = 1; | |
835 | ||
836 | setup_timer(&port->multicast_router_timer, br_multicast_router_expired, | |
837 | (unsigned long)port); | |
838 | setup_timer(&port->multicast_query_timer, | |
839 | br_multicast_port_query_expired, (unsigned long)port); | |
840 | } | |
841 | ||
842 | void br_multicast_del_port(struct net_bridge_port *port) | |
843 | { | |
844 | del_timer_sync(&port->multicast_router_timer); | |
845 | } | |
846 | ||
561f1103 HX |
847 | static void __br_multicast_enable_port(struct net_bridge_port *port) |
848 | { | |
849 | port->multicast_startup_queries_sent = 0; | |
850 | ||
851 | if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || | |
852 | del_timer(&port->multicast_query_timer)) | |
853 | mod_timer(&port->multicast_query_timer, jiffies); | |
854 | } | |
855 | ||
eb1d1641 HX |
856 | void br_multicast_enable_port(struct net_bridge_port *port) |
857 | { | |
858 | struct net_bridge *br = port->br; | |
859 | ||
860 | spin_lock(&br->multicast_lock); | |
861 | if (br->multicast_disabled || !netif_running(br->dev)) | |
862 | goto out; | |
863 | ||
561f1103 | 864 | __br_multicast_enable_port(port); |
eb1d1641 HX |
865 | |
866 | out: | |
867 | spin_unlock(&br->multicast_lock); | |
868 | } | |
869 | ||
870 | void br_multicast_disable_port(struct net_bridge_port *port) | |
871 | { | |
872 | struct net_bridge *br = port->br; | |
873 | struct net_bridge_port_group *pg; | |
874 | struct hlist_node *p, *n; | |
875 | ||
876 | spin_lock(&br->multicast_lock); | |
877 | hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) | |
878 | br_multicast_del_pg(br, pg); | |
879 | ||
880 | if (!hlist_unhashed(&port->rlist)) | |
881 | hlist_del_init_rcu(&port->rlist); | |
882 | del_timer(&port->multicast_router_timer); | |
883 | del_timer(&port->multicast_query_timer); | |
884 | spin_unlock(&br->multicast_lock); | |
885 | } | |
886 | ||
8ef2a9a5 YH |
887 | static int br_ip4_multicast_igmp3_report(struct net_bridge *br, |
888 | struct net_bridge_port *port, | |
889 | struct sk_buff *skb) | |
eb1d1641 HX |
890 | { |
891 | struct igmpv3_report *ih; | |
892 | struct igmpv3_grec *grec; | |
893 | int i; | |
894 | int len; | |
895 | int num; | |
896 | int type; | |
897 | int err = 0; | |
898 | __be32 group; | |
899 | ||
900 | if (!pskb_may_pull(skb, sizeof(*ih))) | |
901 | return -EINVAL; | |
902 | ||
903 | ih = igmpv3_report_hdr(skb); | |
904 | num = ntohs(ih->ngrec); | |
905 | len = sizeof(*ih); | |
906 | ||
907 | for (i = 0; i < num; i++) { | |
908 | len += sizeof(*grec); | |
909 | if (!pskb_may_pull(skb, len)) | |
910 | return -EINVAL; | |
911 | ||
fd218cf9 | 912 | grec = (void *)(skb->data + len - sizeof(*grec)); |
eb1d1641 HX |
913 | group = grec->grec_mca; |
914 | type = grec->grec_type; | |
915 | ||
8eabf95c | 916 | len += ntohs(grec->grec_nsrcs) * 4; |
eb1d1641 HX |
917 | if (!pskb_may_pull(skb, len)) |
918 | return -EINVAL; | |
919 | ||
920 | /* We treat this as an IGMPv2 report for now. */ | |
921 | switch (type) { | |
922 | case IGMPV3_MODE_IS_INCLUDE: | |
923 | case IGMPV3_MODE_IS_EXCLUDE: | |
924 | case IGMPV3_CHANGE_TO_INCLUDE: | |
925 | case IGMPV3_CHANGE_TO_EXCLUDE: | |
926 | case IGMPV3_ALLOW_NEW_SOURCES: | |
927 | case IGMPV3_BLOCK_OLD_SOURCES: | |
928 | break; | |
929 | ||
930 | default: | |
931 | continue; | |
932 | } | |
933 | ||
8ef2a9a5 | 934 | err = br_ip4_multicast_add_group(br, port, group); |
eb1d1641 HX |
935 | if (err) |
936 | break; | |
937 | } | |
938 | ||
939 | return err; | |
940 | } | |
941 | ||
dfd56b8b | 942 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
943 | static int br_ip6_multicast_mld2_report(struct net_bridge *br, |
944 | struct net_bridge_port *port, | |
945 | struct sk_buff *skb) | |
946 | { | |
947 | struct icmp6hdr *icmp6h; | |
948 | struct mld2_grec *grec; | |
949 | int i; | |
950 | int len; | |
951 | int num; | |
952 | int err = 0; | |
953 | ||
954 | if (!pskb_may_pull(skb, sizeof(*icmp6h))) | |
955 | return -EINVAL; | |
956 | ||
957 | icmp6h = icmp6_hdr(skb); | |
958 | num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); | |
959 | len = sizeof(*icmp6h); | |
960 | ||
961 | for (i = 0; i < num; i++) { | |
962 | __be16 *nsrcs, _nsrcs; | |
963 | ||
964 | nsrcs = skb_header_pointer(skb, | |
965 | len + offsetof(struct mld2_grec, | |
649e984d | 966 | grec_nsrcs), |
08b202b6 YH |
967 | sizeof(_nsrcs), &_nsrcs); |
968 | if (!nsrcs) | |
969 | return -EINVAL; | |
970 | ||
971 | if (!pskb_may_pull(skb, | |
972 | len + sizeof(*grec) + | |
d41db9f3 | 973 | sizeof(struct in6_addr) * ntohs(*nsrcs))) |
08b202b6 YH |
974 | return -EINVAL; |
975 | ||
976 | grec = (struct mld2_grec *)(skb->data + len); | |
d41db9f3 LL |
977 | len += sizeof(*grec) + |
978 | sizeof(struct in6_addr) * ntohs(*nsrcs); | |
08b202b6 YH |
979 | |
980 | /* We treat these as MLDv1 reports for now. */ | |
981 | switch (grec->grec_type) { | |
982 | case MLD2_MODE_IS_INCLUDE: | |
983 | case MLD2_MODE_IS_EXCLUDE: | |
984 | case MLD2_CHANGE_TO_INCLUDE: | |
985 | case MLD2_CHANGE_TO_EXCLUDE: | |
986 | case MLD2_ALLOW_NEW_SOURCES: | |
987 | case MLD2_BLOCK_OLD_SOURCES: | |
988 | break; | |
989 | ||
990 | default: | |
991 | continue; | |
992 | } | |
993 | ||
994 | err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); | |
995 | if (!err) | |
996 | break; | |
997 | } | |
998 | ||
999 | return err; | |
1000 | } | |
1001 | #endif | |
1002 | ||
7e80c124 | 1003 | /* |
1004 | * Add port to rotuer_list | |
1005 | * list is maintained ordered by pointer value | |
1006 | * and locked by br->multicast_lock and RCU | |
1007 | */ | |
0909e117 HX |
1008 | static void br_multicast_add_router(struct net_bridge *br, |
1009 | struct net_bridge_port *port) | |
1010 | { | |
dcdca2c4 | 1011 | struct net_bridge_port *p; |
7e80c124 | 1012 | struct hlist_node *n, *slot = NULL; |
dcdca2c4 | 1013 | |
709b9326 | 1014 | hlist_for_each_entry(p, n, &br->router_list, rlist) { |
7e80c124 | 1015 | if ((unsigned long) port >= (unsigned long) p) |
1016 | break; | |
1017 | slot = n; | |
dcdca2c4 | 1018 | } |
1019 | ||
7e80c124 | 1020 | if (slot) |
1021 | hlist_add_after_rcu(slot, &port->rlist); | |
dcdca2c4 | 1022 | else |
1023 | hlist_add_head_rcu(&port->rlist, &br->router_list); | |
0909e117 HX |
1024 | } |
1025 | ||
eb1d1641 HX |
1026 | static void br_multicast_mark_router(struct net_bridge *br, |
1027 | struct net_bridge_port *port) | |
1028 | { | |
1029 | unsigned long now = jiffies; | |
eb1d1641 HX |
1030 | |
1031 | if (!port) { | |
1032 | if (br->multicast_router == 1) | |
1033 | mod_timer(&br->multicast_router_timer, | |
1034 | now + br->multicast_querier_interval); | |
1035 | return; | |
1036 | } | |
1037 | ||
1038 | if (port->multicast_router != 1) | |
1039 | return; | |
1040 | ||
1041 | if (!hlist_unhashed(&port->rlist)) | |
1042 | goto timer; | |
1043 | ||
0909e117 | 1044 | br_multicast_add_router(br, port); |
eb1d1641 HX |
1045 | |
1046 | timer: | |
1047 | mod_timer(&port->multicast_router_timer, | |
1048 | now + br->multicast_querier_interval); | |
1049 | } | |
1050 | ||
1051 | static void br_multicast_query_received(struct net_bridge *br, | |
1052 | struct net_bridge_port *port, | |
8ef2a9a5 | 1053 | int saddr) |
eb1d1641 HX |
1054 | { |
1055 | if (saddr) | |
1056 | mod_timer(&br->multicast_querier_timer, | |
1057 | jiffies + br->multicast_querier_interval); | |
1058 | else if (timer_pending(&br->multicast_querier_timer)) | |
1059 | return; | |
1060 | ||
1061 | br_multicast_mark_router(br, port); | |
1062 | } | |
1063 | ||
8ef2a9a5 YH |
1064 | static int br_ip4_multicast_query(struct net_bridge *br, |
1065 | struct net_bridge_port *port, | |
1066 | struct sk_buff *skb) | |
eb1d1641 | 1067 | { |
b71d1d42 | 1068 | const struct iphdr *iph = ip_hdr(skb); |
eb1d1641 HX |
1069 | struct igmphdr *ih = igmp_hdr(skb); |
1070 | struct net_bridge_mdb_entry *mp; | |
1071 | struct igmpv3_query *ih3; | |
1072 | struct net_bridge_port_group *p; | |
e8051688 | 1073 | struct net_bridge_port_group __rcu **pp; |
eb1d1641 HX |
1074 | unsigned long max_delay; |
1075 | unsigned long now = jiffies; | |
1076 | __be32 group; | |
bec68ff1 | 1077 | int err = 0; |
eb1d1641 HX |
1078 | |
1079 | spin_lock(&br->multicast_lock); | |
1080 | if (!netif_running(br->dev) || | |
1081 | (port && port->state == BR_STATE_DISABLED)) | |
1082 | goto out; | |
1083 | ||
8ef2a9a5 | 1084 | br_multicast_query_received(br, port, !!iph->saddr); |
eb1d1641 HX |
1085 | |
1086 | group = ih->group; | |
1087 | ||
1088 | if (skb->len == sizeof(*ih)) { | |
1089 | max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); | |
1090 | ||
1091 | if (!max_delay) { | |
1092 | max_delay = 10 * HZ; | |
1093 | group = 0; | |
1094 | } | |
1095 | } else { | |
bec68ff1 YH |
1096 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { |
1097 | err = -EINVAL; | |
1098 | goto out; | |
1099 | } | |
eb1d1641 HX |
1100 | |
1101 | ih3 = igmpv3_query_hdr(skb); | |
1102 | if (ih3->nsrcs) | |
bec68ff1 | 1103 | goto out; |
eb1d1641 | 1104 | |
0ba8c9ec YH |
1105 | max_delay = ih3->code ? |
1106 | IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; | |
eb1d1641 HX |
1107 | } |
1108 | ||
1109 | if (!group) | |
1110 | goto out; | |
1111 | ||
e8051688 | 1112 | mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group); |
eb1d1641 HX |
1113 | if (!mp) |
1114 | goto out; | |
1115 | ||
1116 | max_delay *= br->multicast_last_member_count; | |
1117 | ||
8a870178 | 1118 | if (mp->mglist && |
eb1d1641 HX |
1119 | (timer_pending(&mp->timer) ? |
1120 | time_after(mp->timer.expires, now + max_delay) : | |
1121 | try_to_del_timer_sync(&mp->timer) >= 0)) | |
1122 | mod_timer(&mp->timer, now + max_delay); | |
1123 | ||
e8051688 ED |
1124 | for (pp = &mp->ports; |
1125 | (p = mlock_dereference(*pp, br)) != NULL; | |
1126 | pp = &p->next) { | |
eb1d1641 HX |
1127 | if (timer_pending(&p->timer) ? |
1128 | time_after(p->timer.expires, now + max_delay) : | |
1129 | try_to_del_timer_sync(&p->timer) >= 0) | |
24f9cdcb | 1130 | mod_timer(&p->timer, now + max_delay); |
eb1d1641 HX |
1131 | } |
1132 | ||
1133 | out: | |
1134 | spin_unlock(&br->multicast_lock); | |
bec68ff1 | 1135 | return err; |
eb1d1641 HX |
1136 | } |
1137 | ||
dfd56b8b | 1138 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
1139 | static int br_ip6_multicast_query(struct net_bridge *br, |
1140 | struct net_bridge_port *port, | |
1141 | struct sk_buff *skb) | |
1142 | { | |
b71d1d42 | 1143 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
08b202b6 YH |
1144 | struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); |
1145 | struct net_bridge_mdb_entry *mp; | |
1146 | struct mld2_query *mld2q; | |
e8051688 ED |
1147 | struct net_bridge_port_group *p; |
1148 | struct net_bridge_port_group __rcu **pp; | |
08b202b6 YH |
1149 | unsigned long max_delay; |
1150 | unsigned long now = jiffies; | |
b71d1d42 | 1151 | const struct in6_addr *group = NULL; |
08b202b6 YH |
1152 | int err = 0; |
1153 | ||
1154 | spin_lock(&br->multicast_lock); | |
1155 | if (!netif_running(br->dev) || | |
1156 | (port && port->state == BR_STATE_DISABLED)) | |
1157 | goto out; | |
1158 | ||
1159 | br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); | |
1160 | ||
1161 | if (skb->len == sizeof(*mld)) { | |
1162 | if (!pskb_may_pull(skb, sizeof(*mld))) { | |
1163 | err = -EINVAL; | |
1164 | goto out; | |
1165 | } | |
1166 | mld = (struct mld_msg *) icmp6_hdr(skb); | |
4715213d | 1167 | max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); |
08b202b6 YH |
1168 | if (max_delay) |
1169 | group = &mld->mld_mca; | |
1170 | } else if (skb->len >= sizeof(*mld2q)) { | |
1171 | if (!pskb_may_pull(skb, sizeof(*mld2q))) { | |
1172 | err = -EINVAL; | |
1173 | goto out; | |
1174 | } | |
1175 | mld2q = (struct mld2_query *)icmp6_hdr(skb); | |
1176 | if (!mld2q->mld2q_nsrcs) | |
1177 | group = &mld2q->mld2q_mca; | |
1178 | max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; | |
1179 | } | |
1180 | ||
1181 | if (!group) | |
1182 | goto out; | |
1183 | ||
e8051688 | 1184 | mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group); |
08b202b6 YH |
1185 | if (!mp) |
1186 | goto out; | |
1187 | ||
1188 | max_delay *= br->multicast_last_member_count; | |
8a870178 | 1189 | if (mp->mglist && |
08b202b6 YH |
1190 | (timer_pending(&mp->timer) ? |
1191 | time_after(mp->timer.expires, now + max_delay) : | |
1192 | try_to_del_timer_sync(&mp->timer) >= 0)) | |
1193 | mod_timer(&mp->timer, now + max_delay); | |
1194 | ||
e8051688 ED |
1195 | for (pp = &mp->ports; |
1196 | (p = mlock_dereference(*pp, br)) != NULL; | |
1197 | pp = &p->next) { | |
08b202b6 YH |
1198 | if (timer_pending(&p->timer) ? |
1199 | time_after(p->timer.expires, now + max_delay) : | |
1200 | try_to_del_timer_sync(&p->timer) >= 0) | |
24f9cdcb | 1201 | mod_timer(&p->timer, now + max_delay); |
08b202b6 YH |
1202 | } |
1203 | ||
1204 | out: | |
1205 | spin_unlock(&br->multicast_lock); | |
1206 | return err; | |
1207 | } | |
1208 | #endif | |
1209 | ||
eb1d1641 HX |
1210 | static void br_multicast_leave_group(struct net_bridge *br, |
1211 | struct net_bridge_port *port, | |
8ef2a9a5 | 1212 | struct br_ip *group) |
eb1d1641 HX |
1213 | { |
1214 | struct net_bridge_mdb_htable *mdb; | |
1215 | struct net_bridge_mdb_entry *mp; | |
1216 | struct net_bridge_port_group *p; | |
1217 | unsigned long now; | |
1218 | unsigned long time; | |
1219 | ||
eb1d1641 HX |
1220 | spin_lock(&br->multicast_lock); |
1221 | if (!netif_running(br->dev) || | |
1222 | (port && port->state == BR_STATE_DISABLED) || | |
1223 | timer_pending(&br->multicast_querier_timer)) | |
1224 | goto out; | |
1225 | ||
e8051688 | 1226 | mdb = mlock_dereference(br->mdb, br); |
eb1d1641 HX |
1227 | mp = br_mdb_ip_get(mdb, group); |
1228 | if (!mp) | |
1229 | goto out; | |
1230 | ||
c2d3babf | 1231 | if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { |
50426b59 AW |
1232 | struct net_bridge_port_group __rcu **pp; |
1233 | ||
1234 | for (pp = &mp->ports; | |
1235 | (p = mlock_dereference(*pp, br)) != NULL; | |
1236 | pp = &p->next) { | |
1237 | if (p->port != port) | |
1238 | continue; | |
1239 | ||
1240 | rcu_assign_pointer(*pp, p->next); | |
1241 | hlist_del_init(&p->mglist); | |
1242 | del_timer(&p->timer); | |
1243 | call_rcu_bh(&p->rcu, br_multicast_free_pg); | |
37a393bc | 1244 | br_mdb_notify(br->dev, port, group, RTM_DELMDB); |
50426b59 AW |
1245 | |
1246 | if (!mp->ports && !mp->mglist && | |
1247 | netif_running(br->dev)) | |
1248 | mod_timer(&mp->timer, jiffies); | |
1249 | } | |
1250 | goto out; | |
1251 | } | |
1252 | ||
eb1d1641 HX |
1253 | now = jiffies; |
1254 | time = now + br->multicast_last_member_count * | |
1255 | br->multicast_last_member_interval; | |
1256 | ||
1257 | if (!port) { | |
8a870178 | 1258 | if (mp->mglist && |
eb1d1641 HX |
1259 | (timer_pending(&mp->timer) ? |
1260 | time_after(mp->timer.expires, time) : | |
1261 | try_to_del_timer_sync(&mp->timer) >= 0)) { | |
1262 | mod_timer(&mp->timer, time); | |
eb1d1641 HX |
1263 | } |
1264 | ||
1265 | goto out; | |
1266 | } | |
1267 | ||
e8051688 ED |
1268 | for (p = mlock_dereference(mp->ports, br); |
1269 | p != NULL; | |
1270 | p = mlock_dereference(p->next, br)) { | |
eb1d1641 HX |
1271 | if (p->port != port) |
1272 | continue; | |
1273 | ||
1274 | if (!hlist_unhashed(&p->mglist) && | |
1275 | (timer_pending(&p->timer) ? | |
1276 | time_after(p->timer.expires, time) : | |
1277 | try_to_del_timer_sync(&p->timer) >= 0)) { | |
1278 | mod_timer(&p->timer, time); | |
eb1d1641 HX |
1279 | } |
1280 | ||
1281 | break; | |
1282 | } | |
1283 | ||
1284 | out: | |
1285 | spin_unlock(&br->multicast_lock); | |
1286 | } | |
1287 | ||
8ef2a9a5 YH |
1288 | static void br_ip4_multicast_leave_group(struct net_bridge *br, |
1289 | struct net_bridge_port *port, | |
1290 | __be32 group) | |
1291 | { | |
1292 | struct br_ip br_group; | |
1293 | ||
1294 | if (ipv4_is_local_multicast(group)) | |
1295 | return; | |
1296 | ||
1297 | br_group.u.ip4 = group; | |
1298 | br_group.proto = htons(ETH_P_IP); | |
1299 | ||
1300 | br_multicast_leave_group(br, port, &br_group); | |
1301 | } | |
1302 | ||
dfd56b8b | 1303 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
1304 | static void br_ip6_multicast_leave_group(struct net_bridge *br, |
1305 | struct net_bridge_port *port, | |
1306 | const struct in6_addr *group) | |
1307 | { | |
1308 | struct br_ip br_group; | |
1309 | ||
e4de9f9e | 1310 | if (!ipv6_is_transient_multicast(group)) |
08b202b6 YH |
1311 | return; |
1312 | ||
4e3fd7a0 | 1313 | br_group.u.ip6 = *group; |
08b202b6 YH |
1314 | br_group.proto = htons(ETH_P_IPV6); |
1315 | ||
1316 | br_multicast_leave_group(br, port, &br_group); | |
1317 | } | |
1318 | #endif | |
8ef2a9a5 | 1319 | |
eb1d1641 HX |
1320 | static int br_multicast_ipv4_rcv(struct net_bridge *br, |
1321 | struct net_bridge_port *port, | |
1322 | struct sk_buff *skb) | |
1323 | { | |
1324 | struct sk_buff *skb2 = skb; | |
b71d1d42 | 1325 | const struct iphdr *iph; |
eb1d1641 | 1326 | struct igmphdr *ih; |
95c96174 ED |
1327 | unsigned int len; |
1328 | unsigned int offset; | |
eb1d1641 HX |
1329 | int err; |
1330 | ||
eb1d1641 HX |
1331 | /* We treat OOM as packet loss for now. */ |
1332 | if (!pskb_may_pull(skb, sizeof(*iph))) | |
1333 | return -EINVAL; | |
1334 | ||
1335 | iph = ip_hdr(skb); | |
1336 | ||
1337 | if (iph->ihl < 5 || iph->version != 4) | |
1338 | return -EINVAL; | |
1339 | ||
1340 | if (!pskb_may_pull(skb, ip_hdrlen(skb))) | |
1341 | return -EINVAL; | |
1342 | ||
1343 | iph = ip_hdr(skb); | |
1344 | ||
1345 | if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) | |
1346 | return -EINVAL; | |
1347 | ||
bd4265fe HX |
1348 | if (iph->protocol != IPPROTO_IGMP) { |
1349 | if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP) | |
1350 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; | |
eb1d1641 | 1351 | return 0; |
bd4265fe | 1352 | } |
eb1d1641 HX |
1353 | |
1354 | len = ntohs(iph->tot_len); | |
1355 | if (skb->len < len || len < ip_hdrlen(skb)) | |
1356 | return -EINVAL; | |
1357 | ||
1358 | if (skb->len > len) { | |
1359 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
1360 | if (!skb2) | |
1361 | return -ENOMEM; | |
1362 | ||
1363 | err = pskb_trim_rcsum(skb2, len); | |
1364 | if (err) | |
8440853b | 1365 | goto err_out; |
eb1d1641 HX |
1366 | } |
1367 | ||
1368 | len -= ip_hdrlen(skb2); | |
1369 | offset = skb_network_offset(skb2) + ip_hdrlen(skb2); | |
1370 | __skb_pull(skb2, offset); | |
1371 | skb_reset_transport_header(skb2); | |
1372 | ||
1373 | err = -EINVAL; | |
1374 | if (!pskb_may_pull(skb2, sizeof(*ih))) | |
1375 | goto out; | |
1376 | ||
eb1d1641 HX |
1377 | switch (skb2->ip_summed) { |
1378 | case CHECKSUM_COMPLETE: | |
1379 | if (!csum_fold(skb2->csum)) | |
1380 | break; | |
1381 | /* fall through */ | |
1382 | case CHECKSUM_NONE: | |
1383 | skb2->csum = 0; | |
1384 | if (skb_checksum_complete(skb2)) | |
8440853b | 1385 | goto out; |
eb1d1641 HX |
1386 | } |
1387 | ||
1388 | err = 0; | |
1389 | ||
1390 | BR_INPUT_SKB_CB(skb)->igmp = 1; | |
1391 | ih = igmp_hdr(skb2); | |
1392 | ||
1393 | switch (ih->type) { | |
1394 | case IGMP_HOST_MEMBERSHIP_REPORT: | |
1395 | case IGMPV2_HOST_MEMBERSHIP_REPORT: | |
62b2bcb4 | 1396 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
8ef2a9a5 | 1397 | err = br_ip4_multicast_add_group(br, port, ih->group); |
eb1d1641 HX |
1398 | break; |
1399 | case IGMPV3_HOST_MEMBERSHIP_REPORT: | |
8ef2a9a5 | 1400 | err = br_ip4_multicast_igmp3_report(br, port, skb2); |
eb1d1641 HX |
1401 | break; |
1402 | case IGMP_HOST_MEMBERSHIP_QUERY: | |
8ef2a9a5 | 1403 | err = br_ip4_multicast_query(br, port, skb2); |
eb1d1641 HX |
1404 | break; |
1405 | case IGMP_HOST_LEAVE_MESSAGE: | |
8ef2a9a5 | 1406 | br_ip4_multicast_leave_group(br, port, ih->group); |
eb1d1641 HX |
1407 | break; |
1408 | } | |
1409 | ||
1410 | out: | |
1411 | __skb_push(skb2, offset); | |
8440853b | 1412 | err_out: |
eb1d1641 HX |
1413 | if (skb2 != skb) |
1414 | kfree_skb(skb2); | |
1415 | return err; | |
1416 | } | |
1417 | ||
dfd56b8b | 1418 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
1419 | static int br_multicast_ipv6_rcv(struct net_bridge *br, |
1420 | struct net_bridge_port *port, | |
1421 | struct sk_buff *skb) | |
1422 | { | |
9d89081d | 1423 | struct sk_buff *skb2; |
b71d1d42 | 1424 | const struct ipv6hdr *ip6h; |
22df1331 | 1425 | u8 icmp6_type; |
08b202b6 | 1426 | u8 nexthdr; |
75f2811c | 1427 | __be16 frag_off; |
95c96174 | 1428 | unsigned int len; |
bb7a0bd6 | 1429 | int offset; |
08b202b6 YH |
1430 | int err; |
1431 | ||
08b202b6 YH |
1432 | if (!pskb_may_pull(skb, sizeof(*ip6h))) |
1433 | return -EINVAL; | |
1434 | ||
1435 | ip6h = ipv6_hdr(skb); | |
1436 | ||
1437 | /* | |
1438 | * We're interested in MLD messages only. | |
1439 | * - Version is 6 | |
1440 | * - MLD has always Router Alert hop-by-hop option | |
1441 | * - But we do not support jumbrograms. | |
1442 | */ | |
1443 | if (ip6h->version != 6 || | |
1444 | ip6h->nexthdr != IPPROTO_HOPOPTS || | |
1445 | ip6h->payload_len == 0) | |
1446 | return 0; | |
1447 | ||
ff9a57a6 | 1448 | len = ntohs(ip6h->payload_len) + sizeof(*ip6h); |
08b202b6 YH |
1449 | if (skb->len < len) |
1450 | return -EINVAL; | |
1451 | ||
1452 | nexthdr = ip6h->nexthdr; | |
75f2811c | 1453 | offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); |
08b202b6 YH |
1454 | |
1455 | if (offset < 0 || nexthdr != IPPROTO_ICMPV6) | |
1456 | return 0; | |
1457 | ||
1458 | /* Okay, we found ICMPv6 header */ | |
1459 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
1460 | if (!skb2) | |
1461 | return -ENOMEM; | |
1462 | ||
9d89081d TW |
1463 | err = -EINVAL; |
1464 | if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) | |
1465 | goto out; | |
1466 | ||
08b202b6 YH |
1467 | len -= offset - skb_network_offset(skb2); |
1468 | ||
1469 | __skb_pull(skb2, offset); | |
1470 | skb_reset_transport_header(skb2); | |
fa2da8cd | 1471 | skb_postpull_rcsum(skb2, skb_network_header(skb2), |
1472 | skb_network_header_len(skb2)); | |
08b202b6 | 1473 | |
22df1331 | 1474 | icmp6_type = icmp6_hdr(skb2)->icmp6_type; |
08b202b6 | 1475 | |
22df1331 | 1476 | switch (icmp6_type) { |
08b202b6 YH |
1477 | case ICMPV6_MGM_QUERY: |
1478 | case ICMPV6_MGM_REPORT: | |
1479 | case ICMPV6_MGM_REDUCTION: | |
1480 | case ICMPV6_MLD2_REPORT: | |
1481 | break; | |
1482 | default: | |
1483 | err = 0; | |
1484 | goto out; | |
1485 | } | |
1486 | ||
1487 | /* Okay, we found MLD message. Check further. */ | |
1488 | if (skb2->len > len) { | |
1489 | err = pskb_trim_rcsum(skb2, len); | |
1490 | if (err) | |
1491 | goto out; | |
4b275d7e | 1492 | err = -EINVAL; |
08b202b6 YH |
1493 | } |
1494 | ||
4b275d7e YZ |
1495 | ip6h = ipv6_hdr(skb2); |
1496 | ||
08b202b6 YH |
1497 | switch (skb2->ip_summed) { |
1498 | case CHECKSUM_COMPLETE: | |
4b275d7e YZ |
1499 | if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, |
1500 | IPPROTO_ICMPV6, skb2->csum)) | |
08b202b6 YH |
1501 | break; |
1502 | /*FALLTHROUGH*/ | |
1503 | case CHECKSUM_NONE: | |
4b275d7e YZ |
1504 | skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, |
1505 | &ip6h->daddr, | |
1506 | skb2->len, | |
1507 | IPPROTO_ICMPV6, 0)); | |
1508 | if (__skb_checksum_complete(skb2)) | |
08b202b6 YH |
1509 | goto out; |
1510 | } | |
1511 | ||
1512 | err = 0; | |
1513 | ||
1514 | BR_INPUT_SKB_CB(skb)->igmp = 1; | |
1515 | ||
22df1331 | 1516 | switch (icmp6_type) { |
08b202b6 YH |
1517 | case ICMPV6_MGM_REPORT: |
1518 | { | |
9d89081d TW |
1519 | struct mld_msg *mld; |
1520 | if (!pskb_may_pull(skb2, sizeof(*mld))) { | |
1521 | err = -EINVAL; | |
1522 | goto out; | |
1523 | } | |
1524 | mld = (struct mld_msg *)skb_transport_header(skb2); | |
fc2af6c7 | 1525 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
08b202b6 YH |
1526 | err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); |
1527 | break; | |
1528 | } | |
1529 | case ICMPV6_MLD2_REPORT: | |
1530 | err = br_ip6_multicast_mld2_report(br, port, skb2); | |
1531 | break; | |
1532 | case ICMPV6_MGM_QUERY: | |
1533 | err = br_ip6_multicast_query(br, port, skb2); | |
1534 | break; | |
1535 | case ICMPV6_MGM_REDUCTION: | |
1536 | { | |
9d89081d TW |
1537 | struct mld_msg *mld; |
1538 | if (!pskb_may_pull(skb2, sizeof(*mld))) { | |
1539 | err = -EINVAL; | |
1540 | goto out; | |
1541 | } | |
1542 | mld = (struct mld_msg *)skb_transport_header(skb2); | |
08b202b6 YH |
1543 | br_ip6_multicast_leave_group(br, port, &mld->mld_mca); |
1544 | } | |
1545 | } | |
1546 | ||
1547 | out: | |
9d89081d | 1548 | kfree_skb(skb2); |
08b202b6 YH |
1549 | return err; |
1550 | } | |
1551 | #endif | |
1552 | ||
eb1d1641 HX |
1553 | int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, |
1554 | struct sk_buff *skb) | |
1555 | { | |
1fafc7a9 YH |
1556 | BR_INPUT_SKB_CB(skb)->igmp = 0; |
1557 | BR_INPUT_SKB_CB(skb)->mrouters_only = 0; | |
1558 | ||
eb1d1641 HX |
1559 | if (br->multicast_disabled) |
1560 | return 0; | |
1561 | ||
1562 | switch (skb->protocol) { | |
1563 | case htons(ETH_P_IP): | |
1564 | return br_multicast_ipv4_rcv(br, port, skb); | |
dfd56b8b | 1565 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
1566 | case htons(ETH_P_IPV6): |
1567 | return br_multicast_ipv6_rcv(br, port, skb); | |
1568 | #endif | |
eb1d1641 HX |
1569 | } |
1570 | ||
1571 | return 0; | |
1572 | } | |
1573 | ||
1574 | static void br_multicast_query_expired(unsigned long data) | |
1575 | { | |
1576 | struct net_bridge *br = (void *)data; | |
1577 | ||
1578 | spin_lock(&br->multicast_lock); | |
1579 | if (br->multicast_startup_queries_sent < | |
1580 | br->multicast_startup_query_count) | |
1581 | br->multicast_startup_queries_sent++; | |
1582 | ||
1583 | br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); | |
1584 | ||
1585 | spin_unlock(&br->multicast_lock); | |
1586 | } | |
1587 | ||
1588 | void br_multicast_init(struct net_bridge *br) | |
1589 | { | |
1590 | br->hash_elasticity = 4; | |
1591 | br->hash_max = 512; | |
1592 | ||
1593 | br->multicast_router = 1; | |
c5c23260 | 1594 | br->multicast_querier = 0; |
eb1d1641 HX |
1595 | br->multicast_last_member_count = 2; |
1596 | br->multicast_startup_query_count = 2; | |
1597 | ||
1598 | br->multicast_last_member_interval = HZ; | |
1599 | br->multicast_query_response_interval = 10 * HZ; | |
1600 | br->multicast_startup_query_interval = 125 * HZ / 4; | |
1601 | br->multicast_query_interval = 125 * HZ; | |
1602 | br->multicast_querier_interval = 255 * HZ; | |
1603 | br->multicast_membership_interval = 260 * HZ; | |
1604 | ||
1605 | spin_lock_init(&br->multicast_lock); | |
1606 | setup_timer(&br->multicast_router_timer, | |
1607 | br_multicast_local_router_expired, 0); | |
1608 | setup_timer(&br->multicast_querier_timer, | |
bb63f1f8 | 1609 | br_multicast_querier_expired, (unsigned long)br); |
eb1d1641 HX |
1610 | setup_timer(&br->multicast_query_timer, br_multicast_query_expired, |
1611 | (unsigned long)br); | |
ee07c6e7 | 1612 | br_mdb_init(); |
eb1d1641 HX |
1613 | } |
1614 | ||
1615 | void br_multicast_open(struct net_bridge *br) | |
1616 | { | |
1617 | br->multicast_startup_queries_sent = 0; | |
1618 | ||
1619 | if (br->multicast_disabled) | |
1620 | return; | |
1621 | ||
1622 | mod_timer(&br->multicast_query_timer, jiffies); | |
1623 | } | |
1624 | ||
1625 | void br_multicast_stop(struct net_bridge *br) | |
1626 | { | |
1627 | struct net_bridge_mdb_htable *mdb; | |
1628 | struct net_bridge_mdb_entry *mp; | |
1629 | struct hlist_node *p, *n; | |
1630 | u32 ver; | |
1631 | int i; | |
1632 | ||
1633 | del_timer_sync(&br->multicast_router_timer); | |
1634 | del_timer_sync(&br->multicast_querier_timer); | |
1635 | del_timer_sync(&br->multicast_query_timer); | |
1636 | ||
1637 | spin_lock_bh(&br->multicast_lock); | |
e8051688 | 1638 | mdb = mlock_dereference(br->mdb, br); |
eb1d1641 HX |
1639 | if (!mdb) |
1640 | goto out; | |
1641 | ||
1642 | br->mdb = NULL; | |
1643 | ||
1644 | ver = mdb->ver; | |
1645 | for (i = 0; i < mdb->max; i++) { | |
1646 | hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], | |
1647 | hlist[ver]) { | |
1648 | del_timer(&mp->timer); | |
eb1d1641 HX |
1649 | call_rcu_bh(&mp->rcu, br_multicast_free_group); |
1650 | } | |
1651 | } | |
1652 | ||
1653 | if (mdb->old) { | |
1654 | spin_unlock_bh(&br->multicast_lock); | |
10cc2b50 | 1655 | rcu_barrier_bh(); |
eb1d1641 HX |
1656 | spin_lock_bh(&br->multicast_lock); |
1657 | WARN_ON(mdb->old); | |
1658 | } | |
1659 | ||
1660 | mdb->old = mdb; | |
1661 | call_rcu_bh(&mdb->rcu, br_mdb_free); | |
1662 | ||
1663 | out: | |
1664 | spin_unlock_bh(&br->multicast_lock); | |
1665 | } | |
0909e117 HX |
1666 | |
1667 | int br_multicast_set_router(struct net_bridge *br, unsigned long val) | |
1668 | { | |
1669 | int err = -ENOENT; | |
1670 | ||
1671 | spin_lock_bh(&br->multicast_lock); | |
1672 | if (!netif_running(br->dev)) | |
1673 | goto unlock; | |
1674 | ||
1675 | switch (val) { | |
1676 | case 0: | |
1677 | case 2: | |
1678 | del_timer(&br->multicast_router_timer); | |
1679 | /* fall through */ | |
1680 | case 1: | |
1681 | br->multicast_router = val; | |
1682 | err = 0; | |
1683 | break; | |
1684 | ||
1685 | default: | |
1686 | err = -EINVAL; | |
1687 | break; | |
1688 | } | |
1689 | ||
1690 | unlock: | |
1691 | spin_unlock_bh(&br->multicast_lock); | |
1692 | ||
1693 | return err; | |
1694 | } | |
1695 | ||
1696 | int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) | |
1697 | { | |
1698 | struct net_bridge *br = p->br; | |
1699 | int err = -ENOENT; | |
1700 | ||
1701 | spin_lock(&br->multicast_lock); | |
1702 | if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) | |
1703 | goto unlock; | |
1704 | ||
1705 | switch (val) { | |
1706 | case 0: | |
1707 | case 1: | |
1708 | case 2: | |
1709 | p->multicast_router = val; | |
1710 | err = 0; | |
1711 | ||
1712 | if (val < 2 && !hlist_unhashed(&p->rlist)) | |
1713 | hlist_del_init_rcu(&p->rlist); | |
1714 | ||
1715 | if (val == 1) | |
1716 | break; | |
1717 | ||
1718 | del_timer(&p->multicast_router_timer); | |
1719 | ||
1720 | if (val == 0) | |
1721 | break; | |
1722 | ||
1723 | br_multicast_add_router(br, p); | |
1724 | break; | |
1725 | ||
1726 | default: | |
1727 | err = -EINVAL; | |
1728 | break; | |
1729 | } | |
1730 | ||
1731 | unlock: | |
1732 | spin_unlock(&br->multicast_lock); | |
1733 | ||
1734 | return err; | |
1735 | } | |
561f1103 | 1736 | |
74857216 | 1737 | static void br_multicast_start_querier(struct net_bridge *br) |
561f1103 HX |
1738 | { |
1739 | struct net_bridge_port *port; | |
74857216 HX |
1740 | |
1741 | br_multicast_open(br); | |
1742 | ||
1743 | list_for_each_entry(port, &br->port_list, list) { | |
1744 | if (port->state == BR_STATE_DISABLED || | |
1745 | port->state == BR_STATE_BLOCKING) | |
1746 | continue; | |
1747 | ||
1748 | __br_multicast_enable_port(port); | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | int br_multicast_toggle(struct net_bridge *br, unsigned long val) | |
1753 | { | |
3a7fda06 | 1754 | int err = 0; |
e8051688 | 1755 | struct net_bridge_mdb_htable *mdb; |
561f1103 | 1756 | |
ef5e0d82 | 1757 | spin_lock_bh(&br->multicast_lock); |
561f1103 HX |
1758 | if (br->multicast_disabled == !val) |
1759 | goto unlock; | |
1760 | ||
1761 | br->multicast_disabled = !val; | |
1762 | if (br->multicast_disabled) | |
1763 | goto unlock; | |
1764 | ||
3a7fda06 HX |
1765 | if (!netif_running(br->dev)) |
1766 | goto unlock; | |
1767 | ||
e8051688 ED |
1768 | mdb = mlock_dereference(br->mdb, br); |
1769 | if (mdb) { | |
1770 | if (mdb->old) { | |
561f1103 HX |
1771 | err = -EEXIST; |
1772 | rollback: | |
1773 | br->multicast_disabled = !!val; | |
1774 | goto unlock; | |
1775 | } | |
1776 | ||
e8051688 | 1777 | err = br_mdb_rehash(&br->mdb, mdb->max, |
561f1103 HX |
1778 | br->hash_elasticity); |
1779 | if (err) | |
1780 | goto rollback; | |
1781 | } | |
1782 | ||
74857216 | 1783 | br_multicast_start_querier(br); |
561f1103 HX |
1784 | |
1785 | unlock: | |
ef5e0d82 | 1786 | spin_unlock_bh(&br->multicast_lock); |
561f1103 HX |
1787 | |
1788 | return err; | |
1789 | } | |
b195167f | 1790 | |
c5c23260 HX |
1791 | int br_multicast_set_querier(struct net_bridge *br, unsigned long val) |
1792 | { | |
1793 | val = !!val; | |
1794 | ||
1795 | spin_lock_bh(&br->multicast_lock); | |
1796 | if (br->multicast_querier == val) | |
1797 | goto unlock; | |
1798 | ||
1799 | br->multicast_querier = val; | |
1800 | if (val) | |
1801 | br_multicast_start_querier(br); | |
1802 | ||
1803 | unlock: | |
1804 | spin_unlock_bh(&br->multicast_lock); | |
1805 | ||
1806 | return 0; | |
1807 | } | |
1808 | ||
b195167f HX |
1809 | int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) |
1810 | { | |
1811 | int err = -ENOENT; | |
1812 | u32 old; | |
e8051688 | 1813 | struct net_bridge_mdb_htable *mdb; |
b195167f HX |
1814 | |
1815 | spin_lock(&br->multicast_lock); | |
1816 | if (!netif_running(br->dev)) | |
1817 | goto unlock; | |
1818 | ||
1819 | err = -EINVAL; | |
1820 | if (!is_power_of_2(val)) | |
1821 | goto unlock; | |
e8051688 ED |
1822 | |
1823 | mdb = mlock_dereference(br->mdb, br); | |
1824 | if (mdb && val < mdb->size) | |
b195167f HX |
1825 | goto unlock; |
1826 | ||
1827 | err = 0; | |
1828 | ||
1829 | old = br->hash_max; | |
1830 | br->hash_max = val; | |
1831 | ||
e8051688 ED |
1832 | if (mdb) { |
1833 | if (mdb->old) { | |
b195167f HX |
1834 | err = -EEXIST; |
1835 | rollback: | |
1836 | br->hash_max = old; | |
1837 | goto unlock; | |
1838 | } | |
1839 | ||
1840 | err = br_mdb_rehash(&br->mdb, br->hash_max, | |
1841 | br->hash_elasticity); | |
1842 | if (err) | |
1843 | goto rollback; | |
1844 | } | |
1845 | ||
1846 | unlock: | |
1847 | spin_unlock(&br->multicast_lock); | |
1848 | ||
1849 | return err; | |
1850 | } |