]>
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); | |
684 | ||
685 | found: | |
686 | mod_timer(&p->timer, now + br->multicast_membership_interval); | |
687 | out: | |
688 | err = 0; | |
689 | ||
690 | err: | |
691 | spin_unlock(&br->multicast_lock); | |
692 | return err; | |
693 | } | |
694 | ||
8ef2a9a5 YH |
695 | static int br_ip4_multicast_add_group(struct net_bridge *br, |
696 | struct net_bridge_port *port, | |
697 | __be32 group) | |
698 | { | |
699 | struct br_ip br_group; | |
700 | ||
701 | if (ipv4_is_local_multicast(group)) | |
702 | return 0; | |
703 | ||
704 | br_group.u.ip4 = group; | |
705 | br_group.proto = htons(ETH_P_IP); | |
706 | ||
707 | return br_multicast_add_group(br, port, &br_group); | |
708 | } | |
709 | ||
dfd56b8b | 710 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
711 | static int br_ip6_multicast_add_group(struct net_bridge *br, |
712 | struct net_bridge_port *port, | |
713 | const struct in6_addr *group) | |
714 | { | |
715 | struct br_ip br_group; | |
716 | ||
e4de9f9e | 717 | if (!ipv6_is_transient_multicast(group)) |
08b202b6 YH |
718 | return 0; |
719 | ||
4e3fd7a0 | 720 | br_group.u.ip6 = *group; |
9cc6e0c4 | 721 | br_group.proto = htons(ETH_P_IPV6); |
08b202b6 YH |
722 | |
723 | return br_multicast_add_group(br, port, &br_group); | |
724 | } | |
725 | #endif | |
726 | ||
eb1d1641 HX |
727 | static void br_multicast_router_expired(unsigned long data) |
728 | { | |
729 | struct net_bridge_port *port = (void *)data; | |
730 | struct net_bridge *br = port->br; | |
731 | ||
732 | spin_lock(&br->multicast_lock); | |
733 | if (port->multicast_router != 1 || | |
734 | timer_pending(&port->multicast_router_timer) || | |
735 | hlist_unhashed(&port->rlist)) | |
736 | goto out; | |
737 | ||
738 | hlist_del_init_rcu(&port->rlist); | |
739 | ||
740 | out: | |
741 | spin_unlock(&br->multicast_lock); | |
742 | } | |
743 | ||
744 | static void br_multicast_local_router_expired(unsigned long data) | |
745 | { | |
746 | } | |
747 | ||
c83b8fab HX |
748 | static void br_multicast_querier_expired(unsigned long data) |
749 | { | |
bb63f1f8 | 750 | struct net_bridge *br = (void *)data; |
c83b8fab HX |
751 | |
752 | spin_lock(&br->multicast_lock); | |
753 | if (!netif_running(br->dev) || br->multicast_disabled) | |
754 | goto out; | |
755 | ||
756 | br_multicast_start_querier(br); | |
757 | ||
758 | out: | |
759 | spin_unlock(&br->multicast_lock); | |
760 | } | |
761 | ||
8ef2a9a5 YH |
762 | static void __br_multicast_send_query(struct net_bridge *br, |
763 | struct net_bridge_port *port, | |
764 | struct br_ip *ip) | |
eb1d1641 | 765 | { |
eb1d1641 HX |
766 | struct sk_buff *skb; |
767 | ||
8ef2a9a5 | 768 | skb = br_multicast_alloc_query(br, ip); |
eb1d1641 | 769 | if (!skb) |
8ef2a9a5 | 770 | return; |
eb1d1641 HX |
771 | |
772 | if (port) { | |
773 | __skb_push(skb, sizeof(struct ethhdr)); | |
774 | skb->dev = port->dev; | |
713aefa3 | 775 | NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, |
eb1d1641 HX |
776 | dev_queue_xmit); |
777 | } else | |
778 | netif_rx(skb); | |
8ef2a9a5 YH |
779 | } |
780 | ||
781 | static void br_multicast_send_query(struct net_bridge *br, | |
782 | struct net_bridge_port *port, u32 sent) | |
783 | { | |
784 | unsigned long time; | |
785 | struct br_ip br_group; | |
786 | ||
787 | if (!netif_running(br->dev) || br->multicast_disabled || | |
c5c23260 | 788 | !br->multicast_querier || |
8ef2a9a5 YH |
789 | timer_pending(&br->multicast_querier_timer)) |
790 | return; | |
791 | ||
08b202b6 YH |
792 | memset(&br_group.u, 0, sizeof(br_group.u)); |
793 | ||
8ef2a9a5 | 794 | br_group.proto = htons(ETH_P_IP); |
08b202b6 | 795 | __br_multicast_send_query(br, port, &br_group); |
8ef2a9a5 | 796 | |
dfd56b8b | 797 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 | 798 | br_group.proto = htons(ETH_P_IPV6); |
8ef2a9a5 | 799 | __br_multicast_send_query(br, port, &br_group); |
08b202b6 | 800 | #endif |
eb1d1641 | 801 | |
eb1d1641 HX |
802 | time = jiffies; |
803 | time += sent < br->multicast_startup_query_count ? | |
804 | br->multicast_startup_query_interval : | |
805 | br->multicast_query_interval; | |
806 | mod_timer(port ? &port->multicast_query_timer : | |
807 | &br->multicast_query_timer, time); | |
808 | } | |
809 | ||
810 | static void br_multicast_port_query_expired(unsigned long data) | |
811 | { | |
812 | struct net_bridge_port *port = (void *)data; | |
813 | struct net_bridge *br = port->br; | |
814 | ||
815 | spin_lock(&br->multicast_lock); | |
02a780c0 DC |
816 | if (port->state == BR_STATE_DISABLED || |
817 | port->state == BR_STATE_BLOCKING) | |
eb1d1641 HX |
818 | goto out; |
819 | ||
820 | if (port->multicast_startup_queries_sent < | |
821 | br->multicast_startup_query_count) | |
822 | port->multicast_startup_queries_sent++; | |
823 | ||
824 | br_multicast_send_query(port->br, port, | |
825 | port->multicast_startup_queries_sent); | |
826 | ||
827 | out: | |
828 | spin_unlock(&br->multicast_lock); | |
829 | } | |
830 | ||
831 | void br_multicast_add_port(struct net_bridge_port *port) | |
832 | { | |
833 | port->multicast_router = 1; | |
834 | ||
835 | setup_timer(&port->multicast_router_timer, br_multicast_router_expired, | |
836 | (unsigned long)port); | |
837 | setup_timer(&port->multicast_query_timer, | |
838 | br_multicast_port_query_expired, (unsigned long)port); | |
839 | } | |
840 | ||
841 | void br_multicast_del_port(struct net_bridge_port *port) | |
842 | { | |
843 | del_timer_sync(&port->multicast_router_timer); | |
844 | } | |
845 | ||
561f1103 HX |
846 | static void __br_multicast_enable_port(struct net_bridge_port *port) |
847 | { | |
848 | port->multicast_startup_queries_sent = 0; | |
849 | ||
850 | if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || | |
851 | del_timer(&port->multicast_query_timer)) | |
852 | mod_timer(&port->multicast_query_timer, jiffies); | |
853 | } | |
854 | ||
eb1d1641 HX |
855 | void br_multicast_enable_port(struct net_bridge_port *port) |
856 | { | |
857 | struct net_bridge *br = port->br; | |
858 | ||
859 | spin_lock(&br->multicast_lock); | |
860 | if (br->multicast_disabled || !netif_running(br->dev)) | |
861 | goto out; | |
862 | ||
561f1103 | 863 | __br_multicast_enable_port(port); |
eb1d1641 HX |
864 | |
865 | out: | |
866 | spin_unlock(&br->multicast_lock); | |
867 | } | |
868 | ||
869 | void br_multicast_disable_port(struct net_bridge_port *port) | |
870 | { | |
871 | struct net_bridge *br = port->br; | |
872 | struct net_bridge_port_group *pg; | |
873 | struct hlist_node *p, *n; | |
874 | ||
875 | spin_lock(&br->multicast_lock); | |
876 | hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) | |
877 | br_multicast_del_pg(br, pg); | |
878 | ||
879 | if (!hlist_unhashed(&port->rlist)) | |
880 | hlist_del_init_rcu(&port->rlist); | |
881 | del_timer(&port->multicast_router_timer); | |
882 | del_timer(&port->multicast_query_timer); | |
883 | spin_unlock(&br->multicast_lock); | |
884 | } | |
885 | ||
8ef2a9a5 YH |
886 | static int br_ip4_multicast_igmp3_report(struct net_bridge *br, |
887 | struct net_bridge_port *port, | |
888 | struct sk_buff *skb) | |
eb1d1641 HX |
889 | { |
890 | struct igmpv3_report *ih; | |
891 | struct igmpv3_grec *grec; | |
892 | int i; | |
893 | int len; | |
894 | int num; | |
895 | int type; | |
896 | int err = 0; | |
897 | __be32 group; | |
898 | ||
899 | if (!pskb_may_pull(skb, sizeof(*ih))) | |
900 | return -EINVAL; | |
901 | ||
902 | ih = igmpv3_report_hdr(skb); | |
903 | num = ntohs(ih->ngrec); | |
904 | len = sizeof(*ih); | |
905 | ||
906 | for (i = 0; i < num; i++) { | |
907 | len += sizeof(*grec); | |
908 | if (!pskb_may_pull(skb, len)) | |
909 | return -EINVAL; | |
910 | ||
fd218cf9 | 911 | grec = (void *)(skb->data + len - sizeof(*grec)); |
eb1d1641 HX |
912 | group = grec->grec_mca; |
913 | type = grec->grec_type; | |
914 | ||
8eabf95c | 915 | len += ntohs(grec->grec_nsrcs) * 4; |
eb1d1641 HX |
916 | if (!pskb_may_pull(skb, len)) |
917 | return -EINVAL; | |
918 | ||
919 | /* We treat this as an IGMPv2 report for now. */ | |
920 | switch (type) { | |
921 | case IGMPV3_MODE_IS_INCLUDE: | |
922 | case IGMPV3_MODE_IS_EXCLUDE: | |
923 | case IGMPV3_CHANGE_TO_INCLUDE: | |
924 | case IGMPV3_CHANGE_TO_EXCLUDE: | |
925 | case IGMPV3_ALLOW_NEW_SOURCES: | |
926 | case IGMPV3_BLOCK_OLD_SOURCES: | |
927 | break; | |
928 | ||
929 | default: | |
930 | continue; | |
931 | } | |
932 | ||
8ef2a9a5 | 933 | err = br_ip4_multicast_add_group(br, port, group); |
eb1d1641 HX |
934 | if (err) |
935 | break; | |
936 | } | |
937 | ||
938 | return err; | |
939 | } | |
940 | ||
dfd56b8b | 941 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
942 | static int br_ip6_multicast_mld2_report(struct net_bridge *br, |
943 | struct net_bridge_port *port, | |
944 | struct sk_buff *skb) | |
945 | { | |
946 | struct icmp6hdr *icmp6h; | |
947 | struct mld2_grec *grec; | |
948 | int i; | |
949 | int len; | |
950 | int num; | |
951 | int err = 0; | |
952 | ||
953 | if (!pskb_may_pull(skb, sizeof(*icmp6h))) | |
954 | return -EINVAL; | |
955 | ||
956 | icmp6h = icmp6_hdr(skb); | |
957 | num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); | |
958 | len = sizeof(*icmp6h); | |
959 | ||
960 | for (i = 0; i < num; i++) { | |
961 | __be16 *nsrcs, _nsrcs; | |
962 | ||
963 | nsrcs = skb_header_pointer(skb, | |
964 | len + offsetof(struct mld2_grec, | |
649e984d | 965 | grec_nsrcs), |
08b202b6 YH |
966 | sizeof(_nsrcs), &_nsrcs); |
967 | if (!nsrcs) | |
968 | return -EINVAL; | |
969 | ||
970 | if (!pskb_may_pull(skb, | |
971 | len + sizeof(*grec) + | |
d41db9f3 | 972 | sizeof(struct in6_addr) * ntohs(*nsrcs))) |
08b202b6 YH |
973 | return -EINVAL; |
974 | ||
975 | grec = (struct mld2_grec *)(skb->data + len); | |
d41db9f3 LL |
976 | len += sizeof(*grec) + |
977 | sizeof(struct in6_addr) * ntohs(*nsrcs); | |
08b202b6 YH |
978 | |
979 | /* We treat these as MLDv1 reports for now. */ | |
980 | switch (grec->grec_type) { | |
981 | case MLD2_MODE_IS_INCLUDE: | |
982 | case MLD2_MODE_IS_EXCLUDE: | |
983 | case MLD2_CHANGE_TO_INCLUDE: | |
984 | case MLD2_CHANGE_TO_EXCLUDE: | |
985 | case MLD2_ALLOW_NEW_SOURCES: | |
986 | case MLD2_BLOCK_OLD_SOURCES: | |
987 | break; | |
988 | ||
989 | default: | |
990 | continue; | |
991 | } | |
992 | ||
993 | err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); | |
994 | if (!err) | |
995 | break; | |
996 | } | |
997 | ||
998 | return err; | |
999 | } | |
1000 | #endif | |
1001 | ||
7e80c124 | 1002 | /* |
1003 | * Add port to rotuer_list | |
1004 | * list is maintained ordered by pointer value | |
1005 | * and locked by br->multicast_lock and RCU | |
1006 | */ | |
0909e117 HX |
1007 | static void br_multicast_add_router(struct net_bridge *br, |
1008 | struct net_bridge_port *port) | |
1009 | { | |
dcdca2c4 | 1010 | struct net_bridge_port *p; |
7e80c124 | 1011 | struct hlist_node *n, *slot = NULL; |
dcdca2c4 | 1012 | |
709b9326 | 1013 | hlist_for_each_entry(p, n, &br->router_list, rlist) { |
7e80c124 | 1014 | if ((unsigned long) port >= (unsigned long) p) |
1015 | break; | |
1016 | slot = n; | |
dcdca2c4 | 1017 | } |
1018 | ||
7e80c124 | 1019 | if (slot) |
1020 | hlist_add_after_rcu(slot, &port->rlist); | |
dcdca2c4 | 1021 | else |
1022 | hlist_add_head_rcu(&port->rlist, &br->router_list); | |
0909e117 HX |
1023 | } |
1024 | ||
eb1d1641 HX |
1025 | static void br_multicast_mark_router(struct net_bridge *br, |
1026 | struct net_bridge_port *port) | |
1027 | { | |
1028 | unsigned long now = jiffies; | |
eb1d1641 HX |
1029 | |
1030 | if (!port) { | |
1031 | if (br->multicast_router == 1) | |
1032 | mod_timer(&br->multicast_router_timer, | |
1033 | now + br->multicast_querier_interval); | |
1034 | return; | |
1035 | } | |
1036 | ||
1037 | if (port->multicast_router != 1) | |
1038 | return; | |
1039 | ||
1040 | if (!hlist_unhashed(&port->rlist)) | |
1041 | goto timer; | |
1042 | ||
0909e117 | 1043 | br_multicast_add_router(br, port); |
eb1d1641 HX |
1044 | |
1045 | timer: | |
1046 | mod_timer(&port->multicast_router_timer, | |
1047 | now + br->multicast_querier_interval); | |
1048 | } | |
1049 | ||
1050 | static void br_multicast_query_received(struct net_bridge *br, | |
1051 | struct net_bridge_port *port, | |
8ef2a9a5 | 1052 | int saddr) |
eb1d1641 HX |
1053 | { |
1054 | if (saddr) | |
1055 | mod_timer(&br->multicast_querier_timer, | |
1056 | jiffies + br->multicast_querier_interval); | |
1057 | else if (timer_pending(&br->multicast_querier_timer)) | |
1058 | return; | |
1059 | ||
1060 | br_multicast_mark_router(br, port); | |
1061 | } | |
1062 | ||
8ef2a9a5 YH |
1063 | static int br_ip4_multicast_query(struct net_bridge *br, |
1064 | struct net_bridge_port *port, | |
1065 | struct sk_buff *skb) | |
eb1d1641 | 1066 | { |
b71d1d42 | 1067 | const struct iphdr *iph = ip_hdr(skb); |
eb1d1641 HX |
1068 | struct igmphdr *ih = igmp_hdr(skb); |
1069 | struct net_bridge_mdb_entry *mp; | |
1070 | struct igmpv3_query *ih3; | |
1071 | struct net_bridge_port_group *p; | |
e8051688 | 1072 | struct net_bridge_port_group __rcu **pp; |
eb1d1641 HX |
1073 | unsigned long max_delay; |
1074 | unsigned long now = jiffies; | |
1075 | __be32 group; | |
bec68ff1 | 1076 | int err = 0; |
eb1d1641 HX |
1077 | |
1078 | spin_lock(&br->multicast_lock); | |
1079 | if (!netif_running(br->dev) || | |
1080 | (port && port->state == BR_STATE_DISABLED)) | |
1081 | goto out; | |
1082 | ||
8ef2a9a5 | 1083 | br_multicast_query_received(br, port, !!iph->saddr); |
eb1d1641 HX |
1084 | |
1085 | group = ih->group; | |
1086 | ||
1087 | if (skb->len == sizeof(*ih)) { | |
1088 | max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); | |
1089 | ||
1090 | if (!max_delay) { | |
1091 | max_delay = 10 * HZ; | |
1092 | group = 0; | |
1093 | } | |
1094 | } else { | |
bec68ff1 YH |
1095 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { |
1096 | err = -EINVAL; | |
1097 | goto out; | |
1098 | } | |
eb1d1641 HX |
1099 | |
1100 | ih3 = igmpv3_query_hdr(skb); | |
1101 | if (ih3->nsrcs) | |
bec68ff1 | 1102 | goto out; |
eb1d1641 | 1103 | |
0ba8c9ec YH |
1104 | max_delay = ih3->code ? |
1105 | IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; | |
eb1d1641 HX |
1106 | } |
1107 | ||
1108 | if (!group) | |
1109 | goto out; | |
1110 | ||
e8051688 | 1111 | mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group); |
eb1d1641 HX |
1112 | if (!mp) |
1113 | goto out; | |
1114 | ||
1115 | max_delay *= br->multicast_last_member_count; | |
1116 | ||
8a870178 | 1117 | if (mp->mglist && |
eb1d1641 HX |
1118 | (timer_pending(&mp->timer) ? |
1119 | time_after(mp->timer.expires, now + max_delay) : | |
1120 | try_to_del_timer_sync(&mp->timer) >= 0)) | |
1121 | mod_timer(&mp->timer, now + max_delay); | |
1122 | ||
e8051688 ED |
1123 | for (pp = &mp->ports; |
1124 | (p = mlock_dereference(*pp, br)) != NULL; | |
1125 | pp = &p->next) { | |
eb1d1641 HX |
1126 | if (timer_pending(&p->timer) ? |
1127 | time_after(p->timer.expires, now + max_delay) : | |
1128 | try_to_del_timer_sync(&p->timer) >= 0) | |
24f9cdcb | 1129 | mod_timer(&p->timer, now + max_delay); |
eb1d1641 HX |
1130 | } |
1131 | ||
1132 | out: | |
1133 | spin_unlock(&br->multicast_lock); | |
bec68ff1 | 1134 | return err; |
eb1d1641 HX |
1135 | } |
1136 | ||
dfd56b8b | 1137 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
1138 | static int br_ip6_multicast_query(struct net_bridge *br, |
1139 | struct net_bridge_port *port, | |
1140 | struct sk_buff *skb) | |
1141 | { | |
b71d1d42 | 1142 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
08b202b6 YH |
1143 | struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); |
1144 | struct net_bridge_mdb_entry *mp; | |
1145 | struct mld2_query *mld2q; | |
e8051688 ED |
1146 | struct net_bridge_port_group *p; |
1147 | struct net_bridge_port_group __rcu **pp; | |
08b202b6 YH |
1148 | unsigned long max_delay; |
1149 | unsigned long now = jiffies; | |
b71d1d42 | 1150 | const struct in6_addr *group = NULL; |
08b202b6 YH |
1151 | int err = 0; |
1152 | ||
1153 | spin_lock(&br->multicast_lock); | |
1154 | if (!netif_running(br->dev) || | |
1155 | (port && port->state == BR_STATE_DISABLED)) | |
1156 | goto out; | |
1157 | ||
1158 | br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); | |
1159 | ||
1160 | if (skb->len == sizeof(*mld)) { | |
1161 | if (!pskb_may_pull(skb, sizeof(*mld))) { | |
1162 | err = -EINVAL; | |
1163 | goto out; | |
1164 | } | |
1165 | mld = (struct mld_msg *) icmp6_hdr(skb); | |
4715213d | 1166 | max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); |
08b202b6 YH |
1167 | if (max_delay) |
1168 | group = &mld->mld_mca; | |
1169 | } else if (skb->len >= sizeof(*mld2q)) { | |
1170 | if (!pskb_may_pull(skb, sizeof(*mld2q))) { | |
1171 | err = -EINVAL; | |
1172 | goto out; | |
1173 | } | |
1174 | mld2q = (struct mld2_query *)icmp6_hdr(skb); | |
1175 | if (!mld2q->mld2q_nsrcs) | |
1176 | group = &mld2q->mld2q_mca; | |
1177 | max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; | |
1178 | } | |
1179 | ||
1180 | if (!group) | |
1181 | goto out; | |
1182 | ||
e8051688 | 1183 | mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group); |
08b202b6 YH |
1184 | if (!mp) |
1185 | goto out; | |
1186 | ||
1187 | max_delay *= br->multicast_last_member_count; | |
8a870178 | 1188 | if (mp->mglist && |
08b202b6 YH |
1189 | (timer_pending(&mp->timer) ? |
1190 | time_after(mp->timer.expires, now + max_delay) : | |
1191 | try_to_del_timer_sync(&mp->timer) >= 0)) | |
1192 | mod_timer(&mp->timer, now + max_delay); | |
1193 | ||
e8051688 ED |
1194 | for (pp = &mp->ports; |
1195 | (p = mlock_dereference(*pp, br)) != NULL; | |
1196 | pp = &p->next) { | |
08b202b6 YH |
1197 | if (timer_pending(&p->timer) ? |
1198 | time_after(p->timer.expires, now + max_delay) : | |
1199 | try_to_del_timer_sync(&p->timer) >= 0) | |
24f9cdcb | 1200 | mod_timer(&p->timer, now + max_delay); |
08b202b6 YH |
1201 | } |
1202 | ||
1203 | out: | |
1204 | spin_unlock(&br->multicast_lock); | |
1205 | return err; | |
1206 | } | |
1207 | #endif | |
1208 | ||
eb1d1641 HX |
1209 | static void br_multicast_leave_group(struct net_bridge *br, |
1210 | struct net_bridge_port *port, | |
8ef2a9a5 | 1211 | struct br_ip *group) |
eb1d1641 HX |
1212 | { |
1213 | struct net_bridge_mdb_htable *mdb; | |
1214 | struct net_bridge_mdb_entry *mp; | |
1215 | struct net_bridge_port_group *p; | |
1216 | unsigned long now; | |
1217 | unsigned long time; | |
1218 | ||
eb1d1641 HX |
1219 | spin_lock(&br->multicast_lock); |
1220 | if (!netif_running(br->dev) || | |
1221 | (port && port->state == BR_STATE_DISABLED) || | |
1222 | timer_pending(&br->multicast_querier_timer)) | |
1223 | goto out; | |
1224 | ||
e8051688 | 1225 | mdb = mlock_dereference(br->mdb, br); |
eb1d1641 HX |
1226 | mp = br_mdb_ip_get(mdb, group); |
1227 | if (!mp) | |
1228 | goto out; | |
1229 | ||
c2d3babf | 1230 | if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { |
50426b59 AW |
1231 | struct net_bridge_port_group __rcu **pp; |
1232 | ||
1233 | for (pp = &mp->ports; | |
1234 | (p = mlock_dereference(*pp, br)) != NULL; | |
1235 | pp = &p->next) { | |
1236 | if (p->port != port) | |
1237 | continue; | |
1238 | ||
1239 | rcu_assign_pointer(*pp, p->next); | |
1240 | hlist_del_init(&p->mglist); | |
1241 | del_timer(&p->timer); | |
1242 | call_rcu_bh(&p->rcu, br_multicast_free_pg); | |
1243 | ||
1244 | if (!mp->ports && !mp->mglist && | |
1245 | netif_running(br->dev)) | |
1246 | mod_timer(&mp->timer, jiffies); | |
1247 | } | |
1248 | goto out; | |
1249 | } | |
1250 | ||
eb1d1641 HX |
1251 | now = jiffies; |
1252 | time = now + br->multicast_last_member_count * | |
1253 | br->multicast_last_member_interval; | |
1254 | ||
1255 | if (!port) { | |
8a870178 | 1256 | if (mp->mglist && |
eb1d1641 HX |
1257 | (timer_pending(&mp->timer) ? |
1258 | time_after(mp->timer.expires, time) : | |
1259 | try_to_del_timer_sync(&mp->timer) >= 0)) { | |
1260 | mod_timer(&mp->timer, time); | |
eb1d1641 HX |
1261 | } |
1262 | ||
1263 | goto out; | |
1264 | } | |
1265 | ||
e8051688 ED |
1266 | for (p = mlock_dereference(mp->ports, br); |
1267 | p != NULL; | |
1268 | p = mlock_dereference(p->next, br)) { | |
eb1d1641 HX |
1269 | if (p->port != port) |
1270 | continue; | |
1271 | ||
1272 | if (!hlist_unhashed(&p->mglist) && | |
1273 | (timer_pending(&p->timer) ? | |
1274 | time_after(p->timer.expires, time) : | |
1275 | try_to_del_timer_sync(&p->timer) >= 0)) { | |
1276 | mod_timer(&p->timer, time); | |
eb1d1641 HX |
1277 | } |
1278 | ||
1279 | break; | |
1280 | } | |
1281 | ||
1282 | out: | |
1283 | spin_unlock(&br->multicast_lock); | |
1284 | } | |
1285 | ||
8ef2a9a5 YH |
1286 | static void br_ip4_multicast_leave_group(struct net_bridge *br, |
1287 | struct net_bridge_port *port, | |
1288 | __be32 group) | |
1289 | { | |
1290 | struct br_ip br_group; | |
1291 | ||
1292 | if (ipv4_is_local_multicast(group)) | |
1293 | return; | |
1294 | ||
1295 | br_group.u.ip4 = group; | |
1296 | br_group.proto = htons(ETH_P_IP); | |
1297 | ||
1298 | br_multicast_leave_group(br, port, &br_group); | |
1299 | } | |
1300 | ||
dfd56b8b | 1301 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
1302 | static void br_ip6_multicast_leave_group(struct net_bridge *br, |
1303 | struct net_bridge_port *port, | |
1304 | const struct in6_addr *group) | |
1305 | { | |
1306 | struct br_ip br_group; | |
1307 | ||
e4de9f9e | 1308 | if (!ipv6_is_transient_multicast(group)) |
08b202b6 YH |
1309 | return; |
1310 | ||
4e3fd7a0 | 1311 | br_group.u.ip6 = *group; |
08b202b6 YH |
1312 | br_group.proto = htons(ETH_P_IPV6); |
1313 | ||
1314 | br_multicast_leave_group(br, port, &br_group); | |
1315 | } | |
1316 | #endif | |
8ef2a9a5 | 1317 | |
eb1d1641 HX |
1318 | static int br_multicast_ipv4_rcv(struct net_bridge *br, |
1319 | struct net_bridge_port *port, | |
1320 | struct sk_buff *skb) | |
1321 | { | |
1322 | struct sk_buff *skb2 = skb; | |
b71d1d42 | 1323 | const struct iphdr *iph; |
eb1d1641 | 1324 | struct igmphdr *ih; |
95c96174 ED |
1325 | unsigned int len; |
1326 | unsigned int offset; | |
eb1d1641 HX |
1327 | int err; |
1328 | ||
eb1d1641 HX |
1329 | /* We treat OOM as packet loss for now. */ |
1330 | if (!pskb_may_pull(skb, sizeof(*iph))) | |
1331 | return -EINVAL; | |
1332 | ||
1333 | iph = ip_hdr(skb); | |
1334 | ||
1335 | if (iph->ihl < 5 || iph->version != 4) | |
1336 | return -EINVAL; | |
1337 | ||
1338 | if (!pskb_may_pull(skb, ip_hdrlen(skb))) | |
1339 | return -EINVAL; | |
1340 | ||
1341 | iph = ip_hdr(skb); | |
1342 | ||
1343 | if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) | |
1344 | return -EINVAL; | |
1345 | ||
bd4265fe HX |
1346 | if (iph->protocol != IPPROTO_IGMP) { |
1347 | if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP) | |
1348 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; | |
eb1d1641 | 1349 | return 0; |
bd4265fe | 1350 | } |
eb1d1641 HX |
1351 | |
1352 | len = ntohs(iph->tot_len); | |
1353 | if (skb->len < len || len < ip_hdrlen(skb)) | |
1354 | return -EINVAL; | |
1355 | ||
1356 | if (skb->len > len) { | |
1357 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
1358 | if (!skb2) | |
1359 | return -ENOMEM; | |
1360 | ||
1361 | err = pskb_trim_rcsum(skb2, len); | |
1362 | if (err) | |
8440853b | 1363 | goto err_out; |
eb1d1641 HX |
1364 | } |
1365 | ||
1366 | len -= ip_hdrlen(skb2); | |
1367 | offset = skb_network_offset(skb2) + ip_hdrlen(skb2); | |
1368 | __skb_pull(skb2, offset); | |
1369 | skb_reset_transport_header(skb2); | |
1370 | ||
1371 | err = -EINVAL; | |
1372 | if (!pskb_may_pull(skb2, sizeof(*ih))) | |
1373 | goto out; | |
1374 | ||
eb1d1641 HX |
1375 | switch (skb2->ip_summed) { |
1376 | case CHECKSUM_COMPLETE: | |
1377 | if (!csum_fold(skb2->csum)) | |
1378 | break; | |
1379 | /* fall through */ | |
1380 | case CHECKSUM_NONE: | |
1381 | skb2->csum = 0; | |
1382 | if (skb_checksum_complete(skb2)) | |
8440853b | 1383 | goto out; |
eb1d1641 HX |
1384 | } |
1385 | ||
1386 | err = 0; | |
1387 | ||
1388 | BR_INPUT_SKB_CB(skb)->igmp = 1; | |
1389 | ih = igmp_hdr(skb2); | |
1390 | ||
1391 | switch (ih->type) { | |
1392 | case IGMP_HOST_MEMBERSHIP_REPORT: | |
1393 | case IGMPV2_HOST_MEMBERSHIP_REPORT: | |
62b2bcb4 | 1394 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
8ef2a9a5 | 1395 | err = br_ip4_multicast_add_group(br, port, ih->group); |
eb1d1641 HX |
1396 | break; |
1397 | case IGMPV3_HOST_MEMBERSHIP_REPORT: | |
8ef2a9a5 | 1398 | err = br_ip4_multicast_igmp3_report(br, port, skb2); |
eb1d1641 HX |
1399 | break; |
1400 | case IGMP_HOST_MEMBERSHIP_QUERY: | |
8ef2a9a5 | 1401 | err = br_ip4_multicast_query(br, port, skb2); |
eb1d1641 HX |
1402 | break; |
1403 | case IGMP_HOST_LEAVE_MESSAGE: | |
8ef2a9a5 | 1404 | br_ip4_multicast_leave_group(br, port, ih->group); |
eb1d1641 HX |
1405 | break; |
1406 | } | |
1407 | ||
1408 | out: | |
1409 | __skb_push(skb2, offset); | |
8440853b | 1410 | err_out: |
eb1d1641 HX |
1411 | if (skb2 != skb) |
1412 | kfree_skb(skb2); | |
1413 | return err; | |
1414 | } | |
1415 | ||
dfd56b8b | 1416 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
1417 | static int br_multicast_ipv6_rcv(struct net_bridge *br, |
1418 | struct net_bridge_port *port, | |
1419 | struct sk_buff *skb) | |
1420 | { | |
9d89081d | 1421 | struct sk_buff *skb2; |
b71d1d42 | 1422 | const struct ipv6hdr *ip6h; |
22df1331 | 1423 | u8 icmp6_type; |
08b202b6 | 1424 | u8 nexthdr; |
75f2811c | 1425 | __be16 frag_off; |
95c96174 | 1426 | unsigned int len; |
bb7a0bd6 | 1427 | int offset; |
08b202b6 YH |
1428 | int err; |
1429 | ||
08b202b6 YH |
1430 | if (!pskb_may_pull(skb, sizeof(*ip6h))) |
1431 | return -EINVAL; | |
1432 | ||
1433 | ip6h = ipv6_hdr(skb); | |
1434 | ||
1435 | /* | |
1436 | * We're interested in MLD messages only. | |
1437 | * - Version is 6 | |
1438 | * - MLD has always Router Alert hop-by-hop option | |
1439 | * - But we do not support jumbrograms. | |
1440 | */ | |
1441 | if (ip6h->version != 6 || | |
1442 | ip6h->nexthdr != IPPROTO_HOPOPTS || | |
1443 | ip6h->payload_len == 0) | |
1444 | return 0; | |
1445 | ||
ff9a57a6 | 1446 | len = ntohs(ip6h->payload_len) + sizeof(*ip6h); |
08b202b6 YH |
1447 | if (skb->len < len) |
1448 | return -EINVAL; | |
1449 | ||
1450 | nexthdr = ip6h->nexthdr; | |
75f2811c | 1451 | offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); |
08b202b6 YH |
1452 | |
1453 | if (offset < 0 || nexthdr != IPPROTO_ICMPV6) | |
1454 | return 0; | |
1455 | ||
1456 | /* Okay, we found ICMPv6 header */ | |
1457 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
1458 | if (!skb2) | |
1459 | return -ENOMEM; | |
1460 | ||
9d89081d TW |
1461 | err = -EINVAL; |
1462 | if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) | |
1463 | goto out; | |
1464 | ||
08b202b6 YH |
1465 | len -= offset - skb_network_offset(skb2); |
1466 | ||
1467 | __skb_pull(skb2, offset); | |
1468 | skb_reset_transport_header(skb2); | |
fa2da8cd | 1469 | skb_postpull_rcsum(skb2, skb_network_header(skb2), |
1470 | skb_network_header_len(skb2)); | |
08b202b6 | 1471 | |
22df1331 | 1472 | icmp6_type = icmp6_hdr(skb2)->icmp6_type; |
08b202b6 | 1473 | |
22df1331 | 1474 | switch (icmp6_type) { |
08b202b6 YH |
1475 | case ICMPV6_MGM_QUERY: |
1476 | case ICMPV6_MGM_REPORT: | |
1477 | case ICMPV6_MGM_REDUCTION: | |
1478 | case ICMPV6_MLD2_REPORT: | |
1479 | break; | |
1480 | default: | |
1481 | err = 0; | |
1482 | goto out; | |
1483 | } | |
1484 | ||
1485 | /* Okay, we found MLD message. Check further. */ | |
1486 | if (skb2->len > len) { | |
1487 | err = pskb_trim_rcsum(skb2, len); | |
1488 | if (err) | |
1489 | goto out; | |
4b275d7e | 1490 | err = -EINVAL; |
08b202b6 YH |
1491 | } |
1492 | ||
4b275d7e YZ |
1493 | ip6h = ipv6_hdr(skb2); |
1494 | ||
08b202b6 YH |
1495 | switch (skb2->ip_summed) { |
1496 | case CHECKSUM_COMPLETE: | |
4b275d7e YZ |
1497 | if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, |
1498 | IPPROTO_ICMPV6, skb2->csum)) | |
08b202b6 YH |
1499 | break; |
1500 | /*FALLTHROUGH*/ | |
1501 | case CHECKSUM_NONE: | |
4b275d7e YZ |
1502 | skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, |
1503 | &ip6h->daddr, | |
1504 | skb2->len, | |
1505 | IPPROTO_ICMPV6, 0)); | |
1506 | if (__skb_checksum_complete(skb2)) | |
08b202b6 YH |
1507 | goto out; |
1508 | } | |
1509 | ||
1510 | err = 0; | |
1511 | ||
1512 | BR_INPUT_SKB_CB(skb)->igmp = 1; | |
1513 | ||
22df1331 | 1514 | switch (icmp6_type) { |
08b202b6 YH |
1515 | case ICMPV6_MGM_REPORT: |
1516 | { | |
9d89081d TW |
1517 | struct mld_msg *mld; |
1518 | if (!pskb_may_pull(skb2, sizeof(*mld))) { | |
1519 | err = -EINVAL; | |
1520 | goto out; | |
1521 | } | |
1522 | mld = (struct mld_msg *)skb_transport_header(skb2); | |
fc2af6c7 | 1523 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
08b202b6 YH |
1524 | err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); |
1525 | break; | |
1526 | } | |
1527 | case ICMPV6_MLD2_REPORT: | |
1528 | err = br_ip6_multicast_mld2_report(br, port, skb2); | |
1529 | break; | |
1530 | case ICMPV6_MGM_QUERY: | |
1531 | err = br_ip6_multicast_query(br, port, skb2); | |
1532 | break; | |
1533 | case ICMPV6_MGM_REDUCTION: | |
1534 | { | |
9d89081d TW |
1535 | struct mld_msg *mld; |
1536 | if (!pskb_may_pull(skb2, sizeof(*mld))) { | |
1537 | err = -EINVAL; | |
1538 | goto out; | |
1539 | } | |
1540 | mld = (struct mld_msg *)skb_transport_header(skb2); | |
08b202b6 YH |
1541 | br_ip6_multicast_leave_group(br, port, &mld->mld_mca); |
1542 | } | |
1543 | } | |
1544 | ||
1545 | out: | |
9d89081d | 1546 | kfree_skb(skb2); |
08b202b6 YH |
1547 | return err; |
1548 | } | |
1549 | #endif | |
1550 | ||
eb1d1641 HX |
1551 | int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, |
1552 | struct sk_buff *skb) | |
1553 | { | |
1fafc7a9 YH |
1554 | BR_INPUT_SKB_CB(skb)->igmp = 0; |
1555 | BR_INPUT_SKB_CB(skb)->mrouters_only = 0; | |
1556 | ||
eb1d1641 HX |
1557 | if (br->multicast_disabled) |
1558 | return 0; | |
1559 | ||
1560 | switch (skb->protocol) { | |
1561 | case htons(ETH_P_IP): | |
1562 | return br_multicast_ipv4_rcv(br, port, skb); | |
dfd56b8b | 1563 | #if IS_ENABLED(CONFIG_IPV6) |
08b202b6 YH |
1564 | case htons(ETH_P_IPV6): |
1565 | return br_multicast_ipv6_rcv(br, port, skb); | |
1566 | #endif | |
eb1d1641 HX |
1567 | } |
1568 | ||
1569 | return 0; | |
1570 | } | |
1571 | ||
1572 | static void br_multicast_query_expired(unsigned long data) | |
1573 | { | |
1574 | struct net_bridge *br = (void *)data; | |
1575 | ||
1576 | spin_lock(&br->multicast_lock); | |
1577 | if (br->multicast_startup_queries_sent < | |
1578 | br->multicast_startup_query_count) | |
1579 | br->multicast_startup_queries_sent++; | |
1580 | ||
1581 | br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); | |
1582 | ||
1583 | spin_unlock(&br->multicast_lock); | |
1584 | } | |
1585 | ||
1586 | void br_multicast_init(struct net_bridge *br) | |
1587 | { | |
1588 | br->hash_elasticity = 4; | |
1589 | br->hash_max = 512; | |
1590 | ||
1591 | br->multicast_router = 1; | |
c5c23260 | 1592 | br->multicast_querier = 0; |
eb1d1641 HX |
1593 | br->multicast_last_member_count = 2; |
1594 | br->multicast_startup_query_count = 2; | |
1595 | ||
1596 | br->multicast_last_member_interval = HZ; | |
1597 | br->multicast_query_response_interval = 10 * HZ; | |
1598 | br->multicast_startup_query_interval = 125 * HZ / 4; | |
1599 | br->multicast_query_interval = 125 * HZ; | |
1600 | br->multicast_querier_interval = 255 * HZ; | |
1601 | br->multicast_membership_interval = 260 * HZ; | |
1602 | ||
1603 | spin_lock_init(&br->multicast_lock); | |
1604 | setup_timer(&br->multicast_router_timer, | |
1605 | br_multicast_local_router_expired, 0); | |
1606 | setup_timer(&br->multicast_querier_timer, | |
bb63f1f8 | 1607 | br_multicast_querier_expired, (unsigned long)br); |
eb1d1641 HX |
1608 | setup_timer(&br->multicast_query_timer, br_multicast_query_expired, |
1609 | (unsigned long)br); | |
ee07c6e7 | 1610 | br_mdb_init(); |
eb1d1641 HX |
1611 | } |
1612 | ||
1613 | void br_multicast_open(struct net_bridge *br) | |
1614 | { | |
1615 | br->multicast_startup_queries_sent = 0; | |
1616 | ||
1617 | if (br->multicast_disabled) | |
1618 | return; | |
1619 | ||
1620 | mod_timer(&br->multicast_query_timer, jiffies); | |
1621 | } | |
1622 | ||
1623 | void br_multicast_stop(struct net_bridge *br) | |
1624 | { | |
1625 | struct net_bridge_mdb_htable *mdb; | |
1626 | struct net_bridge_mdb_entry *mp; | |
1627 | struct hlist_node *p, *n; | |
1628 | u32 ver; | |
1629 | int i; | |
1630 | ||
1631 | del_timer_sync(&br->multicast_router_timer); | |
1632 | del_timer_sync(&br->multicast_querier_timer); | |
1633 | del_timer_sync(&br->multicast_query_timer); | |
1634 | ||
1635 | spin_lock_bh(&br->multicast_lock); | |
e8051688 | 1636 | mdb = mlock_dereference(br->mdb, br); |
eb1d1641 HX |
1637 | if (!mdb) |
1638 | goto out; | |
1639 | ||
1640 | br->mdb = NULL; | |
1641 | ||
1642 | ver = mdb->ver; | |
1643 | for (i = 0; i < mdb->max; i++) { | |
1644 | hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], | |
1645 | hlist[ver]) { | |
1646 | del_timer(&mp->timer); | |
eb1d1641 HX |
1647 | call_rcu_bh(&mp->rcu, br_multicast_free_group); |
1648 | } | |
1649 | } | |
1650 | ||
1651 | if (mdb->old) { | |
1652 | spin_unlock_bh(&br->multicast_lock); | |
10cc2b50 | 1653 | rcu_barrier_bh(); |
eb1d1641 HX |
1654 | spin_lock_bh(&br->multicast_lock); |
1655 | WARN_ON(mdb->old); | |
1656 | } | |
1657 | ||
1658 | mdb->old = mdb; | |
1659 | call_rcu_bh(&mdb->rcu, br_mdb_free); | |
1660 | ||
1661 | out: | |
1662 | spin_unlock_bh(&br->multicast_lock); | |
1663 | } | |
0909e117 HX |
1664 | |
1665 | int br_multicast_set_router(struct net_bridge *br, unsigned long val) | |
1666 | { | |
1667 | int err = -ENOENT; | |
1668 | ||
1669 | spin_lock_bh(&br->multicast_lock); | |
1670 | if (!netif_running(br->dev)) | |
1671 | goto unlock; | |
1672 | ||
1673 | switch (val) { | |
1674 | case 0: | |
1675 | case 2: | |
1676 | del_timer(&br->multicast_router_timer); | |
1677 | /* fall through */ | |
1678 | case 1: | |
1679 | br->multicast_router = val; | |
1680 | err = 0; | |
1681 | break; | |
1682 | ||
1683 | default: | |
1684 | err = -EINVAL; | |
1685 | break; | |
1686 | } | |
1687 | ||
1688 | unlock: | |
1689 | spin_unlock_bh(&br->multicast_lock); | |
1690 | ||
1691 | return err; | |
1692 | } | |
1693 | ||
1694 | int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) | |
1695 | { | |
1696 | struct net_bridge *br = p->br; | |
1697 | int err = -ENOENT; | |
1698 | ||
1699 | spin_lock(&br->multicast_lock); | |
1700 | if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) | |
1701 | goto unlock; | |
1702 | ||
1703 | switch (val) { | |
1704 | case 0: | |
1705 | case 1: | |
1706 | case 2: | |
1707 | p->multicast_router = val; | |
1708 | err = 0; | |
1709 | ||
1710 | if (val < 2 && !hlist_unhashed(&p->rlist)) | |
1711 | hlist_del_init_rcu(&p->rlist); | |
1712 | ||
1713 | if (val == 1) | |
1714 | break; | |
1715 | ||
1716 | del_timer(&p->multicast_router_timer); | |
1717 | ||
1718 | if (val == 0) | |
1719 | break; | |
1720 | ||
1721 | br_multicast_add_router(br, p); | |
1722 | break; | |
1723 | ||
1724 | default: | |
1725 | err = -EINVAL; | |
1726 | break; | |
1727 | } | |
1728 | ||
1729 | unlock: | |
1730 | spin_unlock(&br->multicast_lock); | |
1731 | ||
1732 | return err; | |
1733 | } | |
561f1103 | 1734 | |
74857216 | 1735 | static void br_multicast_start_querier(struct net_bridge *br) |
561f1103 HX |
1736 | { |
1737 | struct net_bridge_port *port; | |
74857216 HX |
1738 | |
1739 | br_multicast_open(br); | |
1740 | ||
1741 | list_for_each_entry(port, &br->port_list, list) { | |
1742 | if (port->state == BR_STATE_DISABLED || | |
1743 | port->state == BR_STATE_BLOCKING) | |
1744 | continue; | |
1745 | ||
1746 | __br_multicast_enable_port(port); | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | int br_multicast_toggle(struct net_bridge *br, unsigned long val) | |
1751 | { | |
3a7fda06 | 1752 | int err = 0; |
e8051688 | 1753 | struct net_bridge_mdb_htable *mdb; |
561f1103 | 1754 | |
ef5e0d82 | 1755 | spin_lock_bh(&br->multicast_lock); |
561f1103 HX |
1756 | if (br->multicast_disabled == !val) |
1757 | goto unlock; | |
1758 | ||
1759 | br->multicast_disabled = !val; | |
1760 | if (br->multicast_disabled) | |
1761 | goto unlock; | |
1762 | ||
3a7fda06 HX |
1763 | if (!netif_running(br->dev)) |
1764 | goto unlock; | |
1765 | ||
e8051688 ED |
1766 | mdb = mlock_dereference(br->mdb, br); |
1767 | if (mdb) { | |
1768 | if (mdb->old) { | |
561f1103 HX |
1769 | err = -EEXIST; |
1770 | rollback: | |
1771 | br->multicast_disabled = !!val; | |
1772 | goto unlock; | |
1773 | } | |
1774 | ||
e8051688 | 1775 | err = br_mdb_rehash(&br->mdb, mdb->max, |
561f1103 HX |
1776 | br->hash_elasticity); |
1777 | if (err) | |
1778 | goto rollback; | |
1779 | } | |
1780 | ||
74857216 | 1781 | br_multicast_start_querier(br); |
561f1103 HX |
1782 | |
1783 | unlock: | |
ef5e0d82 | 1784 | spin_unlock_bh(&br->multicast_lock); |
561f1103 HX |
1785 | |
1786 | return err; | |
1787 | } | |
b195167f | 1788 | |
c5c23260 HX |
1789 | int br_multicast_set_querier(struct net_bridge *br, unsigned long val) |
1790 | { | |
1791 | val = !!val; | |
1792 | ||
1793 | spin_lock_bh(&br->multicast_lock); | |
1794 | if (br->multicast_querier == val) | |
1795 | goto unlock; | |
1796 | ||
1797 | br->multicast_querier = val; | |
1798 | if (val) | |
1799 | br_multicast_start_querier(br); | |
1800 | ||
1801 | unlock: | |
1802 | spin_unlock_bh(&br->multicast_lock); | |
1803 | ||
1804 | return 0; | |
1805 | } | |
1806 | ||
b195167f HX |
1807 | int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) |
1808 | { | |
1809 | int err = -ENOENT; | |
1810 | u32 old; | |
e8051688 | 1811 | struct net_bridge_mdb_htable *mdb; |
b195167f HX |
1812 | |
1813 | spin_lock(&br->multicast_lock); | |
1814 | if (!netif_running(br->dev)) | |
1815 | goto unlock; | |
1816 | ||
1817 | err = -EINVAL; | |
1818 | if (!is_power_of_2(val)) | |
1819 | goto unlock; | |
e8051688 ED |
1820 | |
1821 | mdb = mlock_dereference(br->mdb, br); | |
1822 | if (mdb && val < mdb->size) | |
b195167f HX |
1823 | goto unlock; |
1824 | ||
1825 | err = 0; | |
1826 | ||
1827 | old = br->hash_max; | |
1828 | br->hash_max = val; | |
1829 | ||
e8051688 ED |
1830 | if (mdb) { |
1831 | if (mdb->old) { | |
b195167f HX |
1832 | err = -EEXIST; |
1833 | rollback: | |
1834 | br->hash_max = old; | |
1835 | goto unlock; | |
1836 | } | |
1837 | ||
1838 | err = br_mdb_rehash(&br->mdb, br->hash_max, | |
1839 | br->hash_elasticity); | |
1840 | if (err) | |
1841 | goto rollback; | |
1842 | } | |
1843 | ||
1844 | unlock: | |
1845 | spin_unlock(&br->multicast_lock); | |
1846 | ||
1847 | return err; | |
1848 | } |