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