perf tools: Streamline bpf examples and headers installation
[linux/fpc-iii.git] / net / ipv4 / igmp.c
blob75151be21413fb60161087c23aefeb6c31093509
1 /*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
11 * Authors:
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
19 * Fixes:
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
23 * functions.
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
32 * points.
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
53 * message (960131).
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
58 * true (960216).
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
70 * Vinay Kulkarni
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <linux/uaccess.h>
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/jiffies.h>
79 #include <linux/string.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
82 #include <linux/in.h>
83 #include <linux/inet.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/if_arp.h>
89 #include <linux/rtnetlink.h>
90 #include <linux/times.h>
91 #include <linux/pkt_sched.h>
92 #include <linux/byteorder/generic.h>
94 #include <net/net_namespace.h>
95 #include <net/arp.h>
96 #include <net/ip.h>
97 #include <net/protocol.h>
98 #include <net/route.h>
99 #include <net/sock.h>
100 #include <net/checksum.h>
101 #include <net/inet_common.h>
102 #include <linux/netfilter_ipv4.h>
103 #ifdef CONFIG_IP_MROUTE
104 #include <linux/mroute.h>
105 #endif
106 #ifdef CONFIG_PROC_FS
107 #include <linux/proc_fs.h>
108 #include <linux/seq_file.h>
109 #endif
111 #ifdef CONFIG_IP_MULTICAST
112 /* Parameter names and values are taken from igmp-v2-06 draft */
114 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
115 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
116 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
117 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
118 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
119 #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
122 #define IGMP_INITIAL_REPORT_DELAY (1)
124 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
131 #define IGMP_V1_SEEN(in_dev) \
132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
142 static int unsolicited_report_interval(struct in_device *in_dev)
144 int interval_ms, interval_jiffies;
146 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
147 interval_ms = IN_DEV_CONF_GET(
148 in_dev,
149 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
150 else /* v3 */
151 interval_ms = IN_DEV_CONF_GET(
152 in_dev,
153 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
155 interval_jiffies = msecs_to_jiffies(interval_ms);
157 /* _timer functions can't handle a delay of 0 jiffies so ensure
158 * we always return a positive value.
160 if (interval_jiffies <= 0)
161 interval_jiffies = 1;
162 return interval_jiffies;
165 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
166 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
167 static void igmpv3_clear_delrec(struct in_device *in_dev);
168 static int sf_setstate(struct ip_mc_list *pmc);
169 static void sf_markstate(struct ip_mc_list *pmc);
170 #endif
171 static void ip_mc_clear_src(struct ip_mc_list *pmc);
172 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
173 int sfcount, __be32 *psfsrc, int delta);
175 static void ip_ma_put(struct ip_mc_list *im)
177 if (refcount_dec_and_test(&im->refcnt)) {
178 in_dev_put(im->interface);
179 kfree_rcu(im, rcu);
183 #define for_each_pmc_rcu(in_dev, pmc) \
184 for (pmc = rcu_dereference(in_dev->mc_list); \
185 pmc != NULL; \
186 pmc = rcu_dereference(pmc->next_rcu))
188 #define for_each_pmc_rtnl(in_dev, pmc) \
189 for (pmc = rtnl_dereference(in_dev->mc_list); \
190 pmc != NULL; \
191 pmc = rtnl_dereference(pmc->next_rcu))
193 #ifdef CONFIG_IP_MULTICAST
196 * Timer management
199 static void igmp_stop_timer(struct ip_mc_list *im)
201 spin_lock_bh(&im->lock);
202 if (del_timer(&im->timer))
203 refcount_dec(&im->refcnt);
204 im->tm_running = 0;
205 im->reporter = 0;
206 im->unsolicit_count = 0;
207 spin_unlock_bh(&im->lock);
210 /* It must be called with locked im->lock */
211 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
213 int tv = prandom_u32() % max_delay;
215 im->tm_running = 1;
216 if (!mod_timer(&im->timer, jiffies+tv+2))
217 refcount_inc(&im->refcnt);
220 static void igmp_gq_start_timer(struct in_device *in_dev)
222 int tv = prandom_u32() % in_dev->mr_maxdelay;
223 unsigned long exp = jiffies + tv + 2;
225 if (in_dev->mr_gq_running &&
226 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
227 return;
229 in_dev->mr_gq_running = 1;
230 if (!mod_timer(&in_dev->mr_gq_timer, exp))
231 in_dev_hold(in_dev);
234 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
236 int tv = prandom_u32() % delay;
238 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
239 in_dev_hold(in_dev);
242 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
244 spin_lock_bh(&im->lock);
245 im->unsolicit_count = 0;
246 if (del_timer(&im->timer)) {
247 if ((long)(im->timer.expires-jiffies) < max_delay) {
248 add_timer(&im->timer);
249 im->tm_running = 1;
250 spin_unlock_bh(&im->lock);
251 return;
253 refcount_dec(&im->refcnt);
255 igmp_start_timer(im, max_delay);
256 spin_unlock_bh(&im->lock);
261 * Send an IGMP report.
264 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
267 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
268 int gdeleted, int sdeleted)
270 switch (type) {
271 case IGMPV3_MODE_IS_INCLUDE:
272 case IGMPV3_MODE_IS_EXCLUDE:
273 if (gdeleted || sdeleted)
274 return 0;
275 if (!(pmc->gsquery && !psf->sf_gsresp)) {
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return 1;
278 /* don't include if this source is excluded
279 * in all filters
281 if (psf->sf_count[MCAST_INCLUDE])
282 return type == IGMPV3_MODE_IS_INCLUDE;
283 return pmc->sfcount[MCAST_EXCLUDE] ==
284 psf->sf_count[MCAST_EXCLUDE];
286 return 0;
287 case IGMPV3_CHANGE_TO_INCLUDE:
288 if (gdeleted || sdeleted)
289 return 0;
290 return psf->sf_count[MCAST_INCLUDE] != 0;
291 case IGMPV3_CHANGE_TO_EXCLUDE:
292 if (gdeleted || sdeleted)
293 return 0;
294 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
295 psf->sf_count[MCAST_INCLUDE])
296 return 0;
297 return pmc->sfcount[MCAST_EXCLUDE] ==
298 psf->sf_count[MCAST_EXCLUDE];
299 case IGMPV3_ALLOW_NEW_SOURCES:
300 if (gdeleted || !psf->sf_crcount)
301 return 0;
302 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
303 case IGMPV3_BLOCK_OLD_SOURCES:
304 if (pmc->sfmode == MCAST_INCLUDE)
305 return gdeleted || (psf->sf_crcount && sdeleted);
306 return psf->sf_crcount && !gdeleted && !sdeleted;
308 return 0;
311 static int
312 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
314 struct ip_sf_list *psf;
315 int scount = 0;
317 for (psf = pmc->sources; psf; psf = psf->sf_next) {
318 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
319 continue;
320 scount++;
322 return scount;
325 /* source address selection per RFC 3376 section 4.2.13 */
326 static __be32 igmpv3_get_srcaddr(struct net_device *dev,
327 const struct flowi4 *fl4)
329 struct in_device *in_dev = __in_dev_get_rcu(dev);
331 if (!in_dev)
332 return htonl(INADDR_ANY);
334 for_ifa(in_dev) {
335 if (fl4->saddr == ifa->ifa_local)
336 return fl4->saddr;
337 } endfor_ifa(in_dev);
339 return htonl(INADDR_ANY);
342 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
344 struct sk_buff *skb;
345 struct rtable *rt;
346 struct iphdr *pip;
347 struct igmpv3_report *pig;
348 struct net *net = dev_net(dev);
349 struct flowi4 fl4;
350 int hlen = LL_RESERVED_SPACE(dev);
351 int tlen = dev->needed_tailroom;
352 unsigned int size = mtu;
354 while (1) {
355 skb = alloc_skb(size + hlen + tlen,
356 GFP_ATOMIC | __GFP_NOWARN);
357 if (skb)
358 break;
359 size >>= 1;
360 if (size < 256)
361 return NULL;
363 skb->priority = TC_PRIO_CONTROL;
365 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
366 0, 0,
367 IPPROTO_IGMP, 0, dev->ifindex);
368 if (IS_ERR(rt)) {
369 kfree_skb(skb);
370 return NULL;
373 skb_dst_set(skb, &rt->dst);
374 skb->dev = dev;
376 skb_reserve(skb, hlen);
377 skb_tailroom_reserve(skb, mtu, tlen);
379 skb_reset_network_header(skb);
380 pip = ip_hdr(skb);
381 skb_put(skb, sizeof(struct iphdr) + 4);
383 pip->version = 4;
384 pip->ihl = (sizeof(struct iphdr)+4)>>2;
385 pip->tos = 0xc0;
386 pip->frag_off = htons(IP_DF);
387 pip->ttl = 1;
388 pip->daddr = fl4.daddr;
390 rcu_read_lock();
391 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
392 rcu_read_unlock();
394 pip->protocol = IPPROTO_IGMP;
395 pip->tot_len = 0; /* filled in later */
396 ip_select_ident(net, skb, NULL);
397 ((u8 *)&pip[1])[0] = IPOPT_RA;
398 ((u8 *)&pip[1])[1] = 4;
399 ((u8 *)&pip[1])[2] = 0;
400 ((u8 *)&pip[1])[3] = 0;
402 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
403 skb_put(skb, sizeof(*pig));
404 pig = igmpv3_report_hdr(skb);
405 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
406 pig->resv1 = 0;
407 pig->csum = 0;
408 pig->resv2 = 0;
409 pig->ngrec = 0;
410 return skb;
413 static int igmpv3_sendpack(struct sk_buff *skb)
415 struct igmphdr *pig = igmp_hdr(skb);
416 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
418 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
420 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
423 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
425 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
428 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
429 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
431 struct net_device *dev = pmc->interface->dev;
432 struct igmpv3_report *pih;
433 struct igmpv3_grec *pgr;
435 if (!skb) {
436 skb = igmpv3_newpack(dev, mtu);
437 if (!skb)
438 return NULL;
440 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
441 pgr->grec_type = type;
442 pgr->grec_auxwords = 0;
443 pgr->grec_nsrcs = 0;
444 pgr->grec_mca = pmc->multiaddr;
445 pih = igmpv3_report_hdr(skb);
446 pih->ngrec = htons(ntohs(pih->ngrec)+1);
447 *ppgr = pgr;
448 return skb;
451 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
453 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
454 int type, int gdeleted, int sdeleted)
456 struct net_device *dev = pmc->interface->dev;
457 struct net *net = dev_net(dev);
458 struct igmpv3_report *pih;
459 struct igmpv3_grec *pgr = NULL;
460 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
461 int scount, stotal, first, isquery, truncate;
462 unsigned int mtu;
464 if (pmc->multiaddr == IGMP_ALL_HOSTS)
465 return skb;
466 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
467 return skb;
469 mtu = READ_ONCE(dev->mtu);
470 if (mtu < IPV4_MIN_MTU)
471 return skb;
473 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
474 type == IGMPV3_MODE_IS_EXCLUDE;
475 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
476 type == IGMPV3_CHANGE_TO_EXCLUDE;
478 stotal = scount = 0;
480 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
482 if (!*psf_list)
483 goto empty_source;
485 pih = skb ? igmpv3_report_hdr(skb) : NULL;
487 /* EX and TO_EX get a fresh packet, if needed */
488 if (truncate) {
489 if (pih && pih->ngrec &&
490 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
491 if (skb)
492 igmpv3_sendpack(skb);
493 skb = igmpv3_newpack(dev, mtu);
496 first = 1;
497 psf_prev = NULL;
498 for (psf = *psf_list; psf; psf = psf_next) {
499 __be32 *psrc;
501 psf_next = psf->sf_next;
503 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
504 psf_prev = psf;
505 continue;
508 /* Based on RFC3376 5.1. Should not send source-list change
509 * records when there is a filter mode change.
511 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
512 (!gdeleted && pmc->crcount)) &&
513 (type == IGMPV3_ALLOW_NEW_SOURCES ||
514 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
515 goto decrease_sf_crcount;
517 /* clear marks on query responses */
518 if (isquery)
519 psf->sf_gsresp = 0;
521 if (AVAILABLE(skb) < sizeof(__be32) +
522 first*sizeof(struct igmpv3_grec)) {
523 if (truncate && !first)
524 break; /* truncate these */
525 if (pgr)
526 pgr->grec_nsrcs = htons(scount);
527 if (skb)
528 igmpv3_sendpack(skb);
529 skb = igmpv3_newpack(dev, mtu);
530 first = 1;
531 scount = 0;
533 if (first) {
534 skb = add_grhead(skb, pmc, type, &pgr, mtu);
535 first = 0;
537 if (!skb)
538 return NULL;
539 psrc = skb_put(skb, sizeof(__be32));
540 *psrc = psf->sf_inaddr;
541 scount++; stotal++;
542 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
543 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
544 decrease_sf_crcount:
545 psf->sf_crcount--;
546 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
547 if (psf_prev)
548 psf_prev->sf_next = psf->sf_next;
549 else
550 *psf_list = psf->sf_next;
551 kfree(psf);
552 continue;
555 psf_prev = psf;
558 empty_source:
559 if (!stotal) {
560 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
561 type == IGMPV3_BLOCK_OLD_SOURCES)
562 return skb;
563 if (pmc->crcount || isquery) {
564 /* make sure we have room for group header */
565 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
566 igmpv3_sendpack(skb);
567 skb = NULL; /* add_grhead will get a new one */
569 skb = add_grhead(skb, pmc, type, &pgr, mtu);
572 if (pgr)
573 pgr->grec_nsrcs = htons(scount);
575 if (isquery)
576 pmc->gsquery = 0; /* clear query state on report */
577 return skb;
580 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
582 struct sk_buff *skb = NULL;
583 struct net *net = dev_net(in_dev->dev);
584 int type;
586 if (!pmc) {
587 rcu_read_lock();
588 for_each_pmc_rcu(in_dev, pmc) {
589 if (pmc->multiaddr == IGMP_ALL_HOSTS)
590 continue;
591 if (ipv4_is_local_multicast(pmc->multiaddr) &&
592 !net->ipv4.sysctl_igmp_llm_reports)
593 continue;
594 spin_lock_bh(&pmc->lock);
595 if (pmc->sfcount[MCAST_EXCLUDE])
596 type = IGMPV3_MODE_IS_EXCLUDE;
597 else
598 type = IGMPV3_MODE_IS_INCLUDE;
599 skb = add_grec(skb, pmc, type, 0, 0);
600 spin_unlock_bh(&pmc->lock);
602 rcu_read_unlock();
603 } else {
604 spin_lock_bh(&pmc->lock);
605 if (pmc->sfcount[MCAST_EXCLUDE])
606 type = IGMPV3_MODE_IS_EXCLUDE;
607 else
608 type = IGMPV3_MODE_IS_INCLUDE;
609 skb = add_grec(skb, pmc, type, 0, 0);
610 spin_unlock_bh(&pmc->lock);
612 if (!skb)
613 return 0;
614 return igmpv3_sendpack(skb);
618 * remove zero-count source records from a source filter list
620 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
622 struct ip_sf_list *psf_prev, *psf_next, *psf;
624 psf_prev = NULL;
625 for (psf = *ppsf; psf; psf = psf_next) {
626 psf_next = psf->sf_next;
627 if (psf->sf_crcount == 0) {
628 if (psf_prev)
629 psf_prev->sf_next = psf->sf_next;
630 else
631 *ppsf = psf->sf_next;
632 kfree(psf);
633 } else
634 psf_prev = psf;
638 static void igmpv3_send_cr(struct in_device *in_dev)
640 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
641 struct sk_buff *skb = NULL;
642 int type, dtype;
644 rcu_read_lock();
645 spin_lock_bh(&in_dev->mc_tomb_lock);
647 /* deleted MCA's */
648 pmc_prev = NULL;
649 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
650 pmc_next = pmc->next;
651 if (pmc->sfmode == MCAST_INCLUDE) {
652 type = IGMPV3_BLOCK_OLD_SOURCES;
653 dtype = IGMPV3_BLOCK_OLD_SOURCES;
654 skb = add_grec(skb, pmc, type, 1, 0);
655 skb = add_grec(skb, pmc, dtype, 1, 1);
657 if (pmc->crcount) {
658 if (pmc->sfmode == MCAST_EXCLUDE) {
659 type = IGMPV3_CHANGE_TO_INCLUDE;
660 skb = add_grec(skb, pmc, type, 1, 0);
662 pmc->crcount--;
663 if (pmc->crcount == 0) {
664 igmpv3_clear_zeros(&pmc->tomb);
665 igmpv3_clear_zeros(&pmc->sources);
668 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
669 if (pmc_prev)
670 pmc_prev->next = pmc_next;
671 else
672 in_dev->mc_tomb = pmc_next;
673 in_dev_put(pmc->interface);
674 kfree(pmc);
675 } else
676 pmc_prev = pmc;
678 spin_unlock_bh(&in_dev->mc_tomb_lock);
680 /* change recs */
681 for_each_pmc_rcu(in_dev, pmc) {
682 spin_lock_bh(&pmc->lock);
683 if (pmc->sfcount[MCAST_EXCLUDE]) {
684 type = IGMPV3_BLOCK_OLD_SOURCES;
685 dtype = IGMPV3_ALLOW_NEW_SOURCES;
686 } else {
687 type = IGMPV3_ALLOW_NEW_SOURCES;
688 dtype = IGMPV3_BLOCK_OLD_SOURCES;
690 skb = add_grec(skb, pmc, type, 0, 0);
691 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
693 /* filter mode changes */
694 if (pmc->crcount) {
695 if (pmc->sfmode == MCAST_EXCLUDE)
696 type = IGMPV3_CHANGE_TO_EXCLUDE;
697 else
698 type = IGMPV3_CHANGE_TO_INCLUDE;
699 skb = add_grec(skb, pmc, type, 0, 0);
700 pmc->crcount--;
702 spin_unlock_bh(&pmc->lock);
704 rcu_read_unlock();
706 if (!skb)
707 return;
708 (void) igmpv3_sendpack(skb);
711 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
712 int type)
714 struct sk_buff *skb;
715 struct iphdr *iph;
716 struct igmphdr *ih;
717 struct rtable *rt;
718 struct net_device *dev = in_dev->dev;
719 struct net *net = dev_net(dev);
720 __be32 group = pmc ? pmc->multiaddr : 0;
721 struct flowi4 fl4;
722 __be32 dst;
723 int hlen, tlen;
725 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
726 return igmpv3_send_report(in_dev, pmc);
728 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
729 return 0;
731 if (type == IGMP_HOST_LEAVE_MESSAGE)
732 dst = IGMP_ALL_ROUTER;
733 else
734 dst = group;
736 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
737 0, 0,
738 IPPROTO_IGMP, 0, dev->ifindex);
739 if (IS_ERR(rt))
740 return -1;
742 hlen = LL_RESERVED_SPACE(dev);
743 tlen = dev->needed_tailroom;
744 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
745 if (!skb) {
746 ip_rt_put(rt);
747 return -1;
749 skb->priority = TC_PRIO_CONTROL;
751 skb_dst_set(skb, &rt->dst);
753 skb_reserve(skb, hlen);
755 skb_reset_network_header(skb);
756 iph = ip_hdr(skb);
757 skb_put(skb, sizeof(struct iphdr) + 4);
759 iph->version = 4;
760 iph->ihl = (sizeof(struct iphdr)+4)>>2;
761 iph->tos = 0xc0;
762 iph->frag_off = htons(IP_DF);
763 iph->ttl = 1;
764 iph->daddr = dst;
765 iph->saddr = fl4.saddr;
766 iph->protocol = IPPROTO_IGMP;
767 ip_select_ident(net, skb, NULL);
768 ((u8 *)&iph[1])[0] = IPOPT_RA;
769 ((u8 *)&iph[1])[1] = 4;
770 ((u8 *)&iph[1])[2] = 0;
771 ((u8 *)&iph[1])[3] = 0;
773 ih = skb_put(skb, sizeof(struct igmphdr));
774 ih->type = type;
775 ih->code = 0;
776 ih->csum = 0;
777 ih->group = group;
778 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
780 return ip_local_out(net, skb->sk, skb);
783 static void igmp_gq_timer_expire(struct timer_list *t)
785 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
787 in_dev->mr_gq_running = 0;
788 igmpv3_send_report(in_dev, NULL);
789 in_dev_put(in_dev);
792 static void igmp_ifc_timer_expire(struct timer_list *t)
794 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
796 igmpv3_send_cr(in_dev);
797 if (in_dev->mr_ifc_count) {
798 in_dev->mr_ifc_count--;
799 igmp_ifc_start_timer(in_dev,
800 unsolicited_report_interval(in_dev));
802 in_dev_put(in_dev);
805 static void igmp_ifc_event(struct in_device *in_dev)
807 struct net *net = dev_net(in_dev->dev);
808 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
809 return;
810 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
811 igmp_ifc_start_timer(in_dev, 1);
815 static void igmp_timer_expire(struct timer_list *t)
817 struct ip_mc_list *im = from_timer(im, t, timer);
818 struct in_device *in_dev = im->interface;
820 spin_lock(&im->lock);
821 im->tm_running = 0;
823 if (im->unsolicit_count) {
824 im->unsolicit_count--;
825 igmp_start_timer(im, unsolicited_report_interval(in_dev));
827 im->reporter = 1;
828 spin_unlock(&im->lock);
830 if (IGMP_V1_SEEN(in_dev))
831 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
832 else if (IGMP_V2_SEEN(in_dev))
833 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
834 else
835 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
837 ip_ma_put(im);
840 /* mark EXCLUDE-mode sources */
841 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
843 struct ip_sf_list *psf;
844 int i, scount;
846 scount = 0;
847 for (psf = pmc->sources; psf; psf = psf->sf_next) {
848 if (scount == nsrcs)
849 break;
850 for (i = 0; i < nsrcs; i++) {
851 /* skip inactive filters */
852 if (psf->sf_count[MCAST_INCLUDE] ||
853 pmc->sfcount[MCAST_EXCLUDE] !=
854 psf->sf_count[MCAST_EXCLUDE])
855 break;
856 if (srcs[i] == psf->sf_inaddr) {
857 scount++;
858 break;
862 pmc->gsquery = 0;
863 if (scount == nsrcs) /* all sources excluded */
864 return 0;
865 return 1;
868 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
870 struct ip_sf_list *psf;
871 int i, scount;
873 if (pmc->sfmode == MCAST_EXCLUDE)
874 return igmp_xmarksources(pmc, nsrcs, srcs);
876 /* mark INCLUDE-mode sources */
877 scount = 0;
878 for (psf = pmc->sources; psf; psf = psf->sf_next) {
879 if (scount == nsrcs)
880 break;
881 for (i = 0; i < nsrcs; i++)
882 if (srcs[i] == psf->sf_inaddr) {
883 psf->sf_gsresp = 1;
884 scount++;
885 break;
888 if (!scount) {
889 pmc->gsquery = 0;
890 return 0;
892 pmc->gsquery = 1;
893 return 1;
896 /* return true if packet was dropped */
897 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
899 struct ip_mc_list *im;
900 struct net *net = dev_net(in_dev->dev);
902 /* Timers are only set for non-local groups */
904 if (group == IGMP_ALL_HOSTS)
905 return false;
906 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
907 return false;
909 rcu_read_lock();
910 for_each_pmc_rcu(in_dev, im) {
911 if (im->multiaddr == group) {
912 igmp_stop_timer(im);
913 break;
916 rcu_read_unlock();
917 return false;
920 /* return true if packet was dropped */
921 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
922 int len)
924 struct igmphdr *ih = igmp_hdr(skb);
925 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
926 struct ip_mc_list *im;
927 __be32 group = ih->group;
928 int max_delay;
929 int mark = 0;
930 struct net *net = dev_net(in_dev->dev);
933 if (len == 8) {
934 if (ih->code == 0) {
935 /* Alas, old v1 router presents here. */
937 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
938 in_dev->mr_v1_seen = jiffies +
939 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
940 group = 0;
941 } else {
942 /* v2 router present */
943 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
944 in_dev->mr_v2_seen = jiffies +
945 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
947 /* cancel the interface change timer */
948 in_dev->mr_ifc_count = 0;
949 if (del_timer(&in_dev->mr_ifc_timer))
950 __in_dev_put(in_dev);
951 /* clear deleted report items */
952 igmpv3_clear_delrec(in_dev);
953 } else if (len < 12) {
954 return true; /* ignore bogus packet; freed by caller */
955 } else if (IGMP_V1_SEEN(in_dev)) {
956 /* This is a v3 query with v1 queriers present */
957 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
958 group = 0;
959 } else if (IGMP_V2_SEEN(in_dev)) {
960 /* this is a v3 query with v2 queriers present;
961 * Interpretation of the max_delay code is problematic here.
962 * A real v2 host would use ih_code directly, while v3 has a
963 * different encoding. We use the v3 encoding as more likely
964 * to be intended in a v3 query.
966 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
967 if (!max_delay)
968 max_delay = 1; /* can't mod w/ 0 */
969 } else { /* v3 */
970 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
971 return true;
973 ih3 = igmpv3_query_hdr(skb);
974 if (ih3->nsrcs) {
975 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
976 + ntohs(ih3->nsrcs)*sizeof(__be32)))
977 return true;
978 ih3 = igmpv3_query_hdr(skb);
981 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
982 if (!max_delay)
983 max_delay = 1; /* can't mod w/ 0 */
984 in_dev->mr_maxdelay = max_delay;
985 if (ih3->qrv)
986 in_dev->mr_qrv = ih3->qrv;
987 if (!group) { /* general query */
988 if (ih3->nsrcs)
989 return true; /* no sources allowed */
990 igmp_gq_start_timer(in_dev);
991 return false;
993 /* mark sources to include, if group & source-specific */
994 mark = ih3->nsrcs != 0;
998 * - Start the timers in all of our membership records
999 * that the query applies to for the interface on
1000 * which the query arrived excl. those that belong
1001 * to a "local" group (224.0.0.X)
1002 * - For timers already running check if they need to
1003 * be reset.
1004 * - Use the igmp->igmp_code field as the maximum
1005 * delay possible
1007 rcu_read_lock();
1008 for_each_pmc_rcu(in_dev, im) {
1009 int changed;
1011 if (group && group != im->multiaddr)
1012 continue;
1013 if (im->multiaddr == IGMP_ALL_HOSTS)
1014 continue;
1015 if (ipv4_is_local_multicast(im->multiaddr) &&
1016 !net->ipv4.sysctl_igmp_llm_reports)
1017 continue;
1018 spin_lock_bh(&im->lock);
1019 if (im->tm_running)
1020 im->gsquery = im->gsquery && mark;
1021 else
1022 im->gsquery = mark;
1023 changed = !im->gsquery ||
1024 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1025 spin_unlock_bh(&im->lock);
1026 if (changed)
1027 igmp_mod_timer(im, max_delay);
1029 rcu_read_unlock();
1030 return false;
1033 /* called in rcu_read_lock() section */
1034 int igmp_rcv(struct sk_buff *skb)
1036 /* This basically follows the spec line by line -- see RFC1112 */
1037 struct igmphdr *ih;
1038 struct net_device *dev = skb->dev;
1039 struct in_device *in_dev;
1040 int len = skb->len;
1041 bool dropped = true;
1043 if (netif_is_l3_master(dev)) {
1044 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1045 if (!dev)
1046 goto drop;
1049 in_dev = __in_dev_get_rcu(dev);
1050 if (!in_dev)
1051 goto drop;
1053 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1054 goto drop;
1056 if (skb_checksum_simple_validate(skb))
1057 goto drop;
1059 ih = igmp_hdr(skb);
1060 switch (ih->type) {
1061 case IGMP_HOST_MEMBERSHIP_QUERY:
1062 dropped = igmp_heard_query(in_dev, skb, len);
1063 break;
1064 case IGMP_HOST_MEMBERSHIP_REPORT:
1065 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1066 /* Is it our report looped back? */
1067 if (rt_is_output_route(skb_rtable(skb)))
1068 break;
1069 /* don't rely on MC router hearing unicast reports */
1070 if (skb->pkt_type == PACKET_MULTICAST ||
1071 skb->pkt_type == PACKET_BROADCAST)
1072 dropped = igmp_heard_report(in_dev, ih->group);
1073 break;
1074 case IGMP_PIM:
1075 #ifdef CONFIG_IP_PIMSM_V1
1076 return pim_rcv_v1(skb);
1077 #endif
1078 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1079 case IGMP_DVMRP:
1080 case IGMP_TRACE:
1081 case IGMP_HOST_LEAVE_MESSAGE:
1082 case IGMP_MTRACE:
1083 case IGMP_MTRACE_RESP:
1084 break;
1085 default:
1086 break;
1089 drop:
1090 if (dropped)
1091 kfree_skb(skb);
1092 else
1093 consume_skb(skb);
1094 return 0;
1097 #endif
1101 * Add a filter to a device
1104 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1106 char buf[MAX_ADDR_LEN];
1107 struct net_device *dev = in_dev->dev;
1109 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1110 We will get multicast token leakage, when IFF_MULTICAST
1111 is changed. This check should be done in ndo_set_rx_mode
1112 routine. Something sort of:
1113 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1114 --ANK
1116 if (arp_mc_map(addr, buf, dev, 0) == 0)
1117 dev_mc_add(dev, buf);
1121 * Remove a filter from a device
1124 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1126 char buf[MAX_ADDR_LEN];
1127 struct net_device *dev = in_dev->dev;
1129 if (arp_mc_map(addr, buf, dev, 0) == 0)
1130 dev_mc_del(dev, buf);
1133 #ifdef CONFIG_IP_MULTICAST
1135 * deleted ip_mc_list manipulation
1137 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1139 struct ip_mc_list *pmc;
1140 struct net *net = dev_net(in_dev->dev);
1142 /* this is an "ip_mc_list" for convenience; only the fields below
1143 * are actually used. In particular, the refcnt and users are not
1144 * used for management of the delete list. Using the same structure
1145 * for deleted items allows change reports to use common code with
1146 * non-deleted or query-response MCA's.
1148 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1149 if (!pmc)
1150 return;
1151 spin_lock_init(&pmc->lock);
1152 spin_lock_bh(&im->lock);
1153 pmc->interface = im->interface;
1154 in_dev_hold(in_dev);
1155 pmc->multiaddr = im->multiaddr;
1156 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1157 pmc->sfmode = im->sfmode;
1158 if (pmc->sfmode == MCAST_INCLUDE) {
1159 struct ip_sf_list *psf;
1161 pmc->tomb = im->tomb;
1162 pmc->sources = im->sources;
1163 im->tomb = im->sources = NULL;
1164 for (psf = pmc->sources; psf; psf = psf->sf_next)
1165 psf->sf_crcount = pmc->crcount;
1167 spin_unlock_bh(&im->lock);
1169 spin_lock_bh(&in_dev->mc_tomb_lock);
1170 pmc->next = in_dev->mc_tomb;
1171 in_dev->mc_tomb = pmc;
1172 spin_unlock_bh(&in_dev->mc_tomb_lock);
1176 * restore ip_mc_list deleted records
1178 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1180 struct ip_mc_list *pmc, *pmc_prev;
1181 struct ip_sf_list *psf;
1182 struct net *net = dev_net(in_dev->dev);
1183 __be32 multiaddr = im->multiaddr;
1185 spin_lock_bh(&in_dev->mc_tomb_lock);
1186 pmc_prev = NULL;
1187 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1188 if (pmc->multiaddr == multiaddr)
1189 break;
1190 pmc_prev = pmc;
1192 if (pmc) {
1193 if (pmc_prev)
1194 pmc_prev->next = pmc->next;
1195 else
1196 in_dev->mc_tomb = pmc->next;
1198 spin_unlock_bh(&in_dev->mc_tomb_lock);
1200 spin_lock_bh(&im->lock);
1201 if (pmc) {
1202 im->interface = pmc->interface;
1203 if (im->sfmode == MCAST_INCLUDE) {
1204 im->tomb = pmc->tomb;
1205 im->sources = pmc->sources;
1206 for (psf = im->sources; psf; psf = psf->sf_next)
1207 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1208 } else {
1209 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1211 in_dev_put(pmc->interface);
1212 kfree(pmc);
1214 spin_unlock_bh(&im->lock);
1218 * flush ip_mc_list deleted records
1220 static void igmpv3_clear_delrec(struct in_device *in_dev)
1222 struct ip_mc_list *pmc, *nextpmc;
1224 spin_lock_bh(&in_dev->mc_tomb_lock);
1225 pmc = in_dev->mc_tomb;
1226 in_dev->mc_tomb = NULL;
1227 spin_unlock_bh(&in_dev->mc_tomb_lock);
1229 for (; pmc; pmc = nextpmc) {
1230 nextpmc = pmc->next;
1231 ip_mc_clear_src(pmc);
1232 in_dev_put(pmc->interface);
1233 kfree(pmc);
1235 /* clear dead sources, too */
1236 rcu_read_lock();
1237 for_each_pmc_rcu(in_dev, pmc) {
1238 struct ip_sf_list *psf, *psf_next;
1240 spin_lock_bh(&pmc->lock);
1241 psf = pmc->tomb;
1242 pmc->tomb = NULL;
1243 spin_unlock_bh(&pmc->lock);
1244 for (; psf; psf = psf_next) {
1245 psf_next = psf->sf_next;
1246 kfree(psf);
1249 rcu_read_unlock();
1251 #endif
1253 static void igmp_group_dropped(struct ip_mc_list *im)
1255 struct in_device *in_dev = im->interface;
1256 #ifdef CONFIG_IP_MULTICAST
1257 struct net *net = dev_net(in_dev->dev);
1258 int reporter;
1259 #endif
1261 if (im->loaded) {
1262 im->loaded = 0;
1263 ip_mc_filter_del(in_dev, im->multiaddr);
1266 #ifdef CONFIG_IP_MULTICAST
1267 if (im->multiaddr == IGMP_ALL_HOSTS)
1268 return;
1269 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1270 return;
1272 reporter = im->reporter;
1273 igmp_stop_timer(im);
1275 if (!in_dev->dead) {
1276 if (IGMP_V1_SEEN(in_dev))
1277 return;
1278 if (IGMP_V2_SEEN(in_dev)) {
1279 if (reporter)
1280 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1281 return;
1283 /* IGMPv3 */
1284 igmpv3_add_delrec(in_dev, im);
1286 igmp_ifc_event(in_dev);
1288 #endif
1291 static void igmp_group_added(struct ip_mc_list *im, unsigned int mode)
1293 struct in_device *in_dev = im->interface;
1294 #ifdef CONFIG_IP_MULTICAST
1295 struct net *net = dev_net(in_dev->dev);
1296 #endif
1298 if (im->loaded == 0) {
1299 im->loaded = 1;
1300 ip_mc_filter_add(in_dev, im->multiaddr);
1303 #ifdef CONFIG_IP_MULTICAST
1304 if (im->multiaddr == IGMP_ALL_HOSTS)
1305 return;
1306 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1307 return;
1309 if (in_dev->dead)
1310 return;
1311 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1312 spin_lock_bh(&im->lock);
1313 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1314 spin_unlock_bh(&im->lock);
1315 return;
1317 /* else, v3 */
1319 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1320 * not send filter-mode change record as the mode should be from
1321 * IN() to IN(A).
1323 if (mode == MCAST_EXCLUDE)
1324 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1326 igmp_ifc_event(in_dev);
1327 #endif
1332 * Multicast list managers
1335 static u32 ip_mc_hash(const struct ip_mc_list *im)
1337 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1340 static void ip_mc_hash_add(struct in_device *in_dev,
1341 struct ip_mc_list *im)
1343 struct ip_mc_list __rcu **mc_hash;
1344 u32 hash;
1346 mc_hash = rtnl_dereference(in_dev->mc_hash);
1347 if (mc_hash) {
1348 hash = ip_mc_hash(im);
1349 im->next_hash = mc_hash[hash];
1350 rcu_assign_pointer(mc_hash[hash], im);
1351 return;
1354 /* do not use a hash table for small number of items */
1355 if (in_dev->mc_count < 4)
1356 return;
1358 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1359 GFP_KERNEL);
1360 if (!mc_hash)
1361 return;
1363 for_each_pmc_rtnl(in_dev, im) {
1364 hash = ip_mc_hash(im);
1365 im->next_hash = mc_hash[hash];
1366 RCU_INIT_POINTER(mc_hash[hash], im);
1369 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1372 static void ip_mc_hash_remove(struct in_device *in_dev,
1373 struct ip_mc_list *im)
1375 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1376 struct ip_mc_list *aux;
1378 if (!mc_hash)
1379 return;
1380 mc_hash += ip_mc_hash(im);
1381 while ((aux = rtnl_dereference(*mc_hash)) != im)
1382 mc_hash = &aux->next_hash;
1383 *mc_hash = im->next_hash;
1388 * A socket has joined a multicast group on device dev.
1390 static void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1391 unsigned int mode)
1393 struct ip_mc_list *im;
1394 #ifdef CONFIG_IP_MULTICAST
1395 struct net *net = dev_net(in_dev->dev);
1396 #endif
1398 ASSERT_RTNL();
1400 for_each_pmc_rtnl(in_dev, im) {
1401 if (im->multiaddr == addr) {
1402 im->users++;
1403 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
1404 goto out;
1408 im = kzalloc(sizeof(*im), GFP_KERNEL);
1409 if (!im)
1410 goto out;
1412 im->users = 1;
1413 im->interface = in_dev;
1414 in_dev_hold(in_dev);
1415 im->multiaddr = addr;
1416 /* initial mode is (EX, empty) */
1417 im->sfmode = mode;
1418 im->sfcount[mode] = 1;
1419 refcount_set(&im->refcnt, 1);
1420 spin_lock_init(&im->lock);
1421 #ifdef CONFIG_IP_MULTICAST
1422 timer_setup(&im->timer, igmp_timer_expire, 0);
1423 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1424 #endif
1426 im->next_rcu = in_dev->mc_list;
1427 in_dev->mc_count++;
1428 rcu_assign_pointer(in_dev->mc_list, im);
1430 ip_mc_hash_add(in_dev, im);
1432 #ifdef CONFIG_IP_MULTICAST
1433 igmpv3_del_delrec(in_dev, im);
1434 #endif
1435 igmp_group_added(im, mode);
1436 if (!in_dev->dead)
1437 ip_rt_multicast_event(in_dev);
1438 out:
1439 return;
1442 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1444 __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE);
1446 EXPORT_SYMBOL(ip_mc_inc_group);
1448 static int ip_mc_check_iphdr(struct sk_buff *skb)
1450 const struct iphdr *iph;
1451 unsigned int len;
1452 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1454 if (!pskb_may_pull(skb, offset))
1455 return -EINVAL;
1457 iph = ip_hdr(skb);
1459 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1460 return -EINVAL;
1462 offset += ip_hdrlen(skb) - sizeof(*iph);
1464 if (!pskb_may_pull(skb, offset))
1465 return -EINVAL;
1467 iph = ip_hdr(skb);
1469 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1470 return -EINVAL;
1472 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1473 if (skb->len < len || len < offset)
1474 return -EINVAL;
1476 skb_set_transport_header(skb, offset);
1478 return 0;
1481 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1483 unsigned int len = skb_transport_offset(skb);
1485 len += sizeof(struct igmpv3_report);
1487 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1490 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1492 unsigned int len = skb_transport_offset(skb);
1494 len += sizeof(struct igmphdr);
1495 if (skb->len < len)
1496 return -EINVAL;
1498 /* IGMPv{1,2}? */
1499 if (skb->len != len) {
1500 /* or IGMPv3? */
1501 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1502 if (skb->len < len || !pskb_may_pull(skb, len))
1503 return -EINVAL;
1506 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1507 * all-systems destination addresses (224.0.0.1) for general queries
1509 if (!igmp_hdr(skb)->group &&
1510 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1511 return -EINVAL;
1513 return 0;
1516 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1518 switch (igmp_hdr(skb)->type) {
1519 case IGMP_HOST_LEAVE_MESSAGE:
1520 case IGMP_HOST_MEMBERSHIP_REPORT:
1521 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1522 /* fall through */
1523 return 0;
1524 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1525 return ip_mc_check_igmp_reportv3(skb);
1526 case IGMP_HOST_MEMBERSHIP_QUERY:
1527 return ip_mc_check_igmp_query(skb);
1528 default:
1529 return -ENOMSG;
1533 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1535 return skb_checksum_simple_validate(skb);
1538 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1541 struct sk_buff *skb_chk;
1542 unsigned int transport_len;
1543 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1544 int ret = -EINVAL;
1546 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1548 skb_chk = skb_checksum_trimmed(skb, transport_len,
1549 ip_mc_validate_checksum);
1550 if (!skb_chk)
1551 goto err;
1553 if (!pskb_may_pull(skb_chk, len))
1554 goto err;
1556 ret = ip_mc_check_igmp_msg(skb_chk);
1557 if (ret)
1558 goto err;
1560 if (skb_trimmed)
1561 *skb_trimmed = skb_chk;
1562 /* free now unneeded clone */
1563 else if (skb_chk != skb)
1564 kfree_skb(skb_chk);
1566 ret = 0;
1568 err:
1569 if (ret && skb_chk && skb_chk != skb)
1570 kfree_skb(skb_chk);
1572 return ret;
1576 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1577 * @skb: the skb to validate
1578 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1580 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1581 * skb transport header accordingly and returns zero.
1583 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1584 * standard
1585 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1586 * -ENOMEM: A memory allocation failure happened.
1588 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1589 * to NULL): After parsing an IGMP packet successfully it will point to
1590 * an skb which has its tail aligned to the IP packet end. This might
1591 * either be the originally provided skb or a trimmed, cloned version if
1592 * the skb frame had data beyond the IP packet. A cloned skb allows us
1593 * to leave the original skb and its full frame unchanged (which might be
1594 * desirable for layer 2 frame jugglers).
1596 * Caller needs to set the skb network header and free any returned skb if it
1597 * differs from the provided skb.
1599 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1601 int ret = ip_mc_check_iphdr(skb);
1603 if (ret < 0)
1604 return ret;
1606 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1607 return -ENOMSG;
1609 return __ip_mc_check_igmp(skb, skb_trimmed);
1611 EXPORT_SYMBOL(ip_mc_check_igmp);
1614 * Resend IGMP JOIN report; used by netdev notifier.
1616 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1618 #ifdef CONFIG_IP_MULTICAST
1619 struct ip_mc_list *im;
1620 int type;
1621 struct net *net = dev_net(in_dev->dev);
1623 ASSERT_RTNL();
1625 for_each_pmc_rtnl(in_dev, im) {
1626 if (im->multiaddr == IGMP_ALL_HOSTS)
1627 continue;
1628 if (ipv4_is_local_multicast(im->multiaddr) &&
1629 !net->ipv4.sysctl_igmp_llm_reports)
1630 continue;
1632 /* a failover is happening and switches
1633 * must be notified immediately
1635 if (IGMP_V1_SEEN(in_dev))
1636 type = IGMP_HOST_MEMBERSHIP_REPORT;
1637 else if (IGMP_V2_SEEN(in_dev))
1638 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1639 else
1640 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1641 igmp_send_report(in_dev, im, type);
1643 #endif
1647 * A socket has left a multicast group on device dev
1650 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1652 struct ip_mc_list *i;
1653 struct ip_mc_list __rcu **ip;
1655 ASSERT_RTNL();
1657 for (ip = &in_dev->mc_list;
1658 (i = rtnl_dereference(*ip)) != NULL;
1659 ip = &i->next_rcu) {
1660 if (i->multiaddr == addr) {
1661 if (--i->users == 0) {
1662 ip_mc_hash_remove(in_dev, i);
1663 *ip = i->next_rcu;
1664 in_dev->mc_count--;
1665 igmp_group_dropped(i);
1666 ip_mc_clear_src(i);
1668 if (!in_dev->dead)
1669 ip_rt_multicast_event(in_dev);
1671 ip_ma_put(i);
1672 return;
1674 break;
1678 EXPORT_SYMBOL(ip_mc_dec_group);
1680 /* Device changing type */
1682 void ip_mc_unmap(struct in_device *in_dev)
1684 struct ip_mc_list *pmc;
1686 ASSERT_RTNL();
1688 for_each_pmc_rtnl(in_dev, pmc)
1689 igmp_group_dropped(pmc);
1692 void ip_mc_remap(struct in_device *in_dev)
1694 struct ip_mc_list *pmc;
1696 ASSERT_RTNL();
1698 for_each_pmc_rtnl(in_dev, pmc) {
1699 #ifdef CONFIG_IP_MULTICAST
1700 igmpv3_del_delrec(in_dev, pmc);
1701 #endif
1702 igmp_group_added(pmc, pmc->sfmode);
1706 /* Device going down */
1708 void ip_mc_down(struct in_device *in_dev)
1710 struct ip_mc_list *pmc;
1712 ASSERT_RTNL();
1714 for_each_pmc_rtnl(in_dev, pmc)
1715 igmp_group_dropped(pmc);
1717 #ifdef CONFIG_IP_MULTICAST
1718 in_dev->mr_ifc_count = 0;
1719 if (del_timer(&in_dev->mr_ifc_timer))
1720 __in_dev_put(in_dev);
1721 in_dev->mr_gq_running = 0;
1722 if (del_timer(&in_dev->mr_gq_timer))
1723 __in_dev_put(in_dev);
1724 #endif
1726 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1729 void ip_mc_init_dev(struct in_device *in_dev)
1731 #ifdef CONFIG_IP_MULTICAST
1732 struct net *net = dev_net(in_dev->dev);
1733 #endif
1734 ASSERT_RTNL();
1736 #ifdef CONFIG_IP_MULTICAST
1737 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1738 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
1739 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1740 #endif
1742 spin_lock_init(&in_dev->mc_tomb_lock);
1745 /* Device going up */
1747 void ip_mc_up(struct in_device *in_dev)
1749 struct ip_mc_list *pmc;
1750 #ifdef CONFIG_IP_MULTICAST
1751 struct net *net = dev_net(in_dev->dev);
1752 #endif
1754 ASSERT_RTNL();
1756 #ifdef CONFIG_IP_MULTICAST
1757 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1758 #endif
1759 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1761 for_each_pmc_rtnl(in_dev, pmc) {
1762 #ifdef CONFIG_IP_MULTICAST
1763 igmpv3_del_delrec(in_dev, pmc);
1764 #endif
1765 igmp_group_added(pmc, pmc->sfmode);
1770 * Device is about to be destroyed: clean up.
1773 void ip_mc_destroy_dev(struct in_device *in_dev)
1775 struct ip_mc_list *i;
1777 ASSERT_RTNL();
1779 /* Deactivate timers */
1780 ip_mc_down(in_dev);
1781 #ifdef CONFIG_IP_MULTICAST
1782 igmpv3_clear_delrec(in_dev);
1783 #endif
1785 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1786 in_dev->mc_list = i->next_rcu;
1787 in_dev->mc_count--;
1788 ip_ma_put(i);
1792 /* RTNL is locked */
1793 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1795 struct net_device *dev = NULL;
1796 struct in_device *idev = NULL;
1798 if (imr->imr_ifindex) {
1799 idev = inetdev_by_index(net, imr->imr_ifindex);
1800 return idev;
1802 if (imr->imr_address.s_addr) {
1803 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1804 if (!dev)
1805 return NULL;
1808 if (!dev) {
1809 struct rtable *rt = ip_route_output(net,
1810 imr->imr_multiaddr.s_addr,
1811 0, 0, 0);
1812 if (!IS_ERR(rt)) {
1813 dev = rt->dst.dev;
1814 ip_rt_put(rt);
1817 if (dev) {
1818 imr->imr_ifindex = dev->ifindex;
1819 idev = __in_dev_get_rtnl(dev);
1821 return idev;
1825 * Join a socket to a group
1828 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1829 __be32 *psfsrc)
1831 struct ip_sf_list *psf, *psf_prev;
1832 int rv = 0;
1834 psf_prev = NULL;
1835 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1836 if (psf->sf_inaddr == *psfsrc)
1837 break;
1838 psf_prev = psf;
1840 if (!psf || psf->sf_count[sfmode] == 0) {
1841 /* source filter not found, or count wrong => bug */
1842 return -ESRCH;
1844 psf->sf_count[sfmode]--;
1845 if (psf->sf_count[sfmode] == 0) {
1846 ip_rt_multicast_event(pmc->interface);
1848 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1849 #ifdef CONFIG_IP_MULTICAST
1850 struct in_device *in_dev = pmc->interface;
1851 struct net *net = dev_net(in_dev->dev);
1852 #endif
1854 /* no more filters for this source */
1855 if (psf_prev)
1856 psf_prev->sf_next = psf->sf_next;
1857 else
1858 pmc->sources = psf->sf_next;
1859 #ifdef CONFIG_IP_MULTICAST
1860 if (psf->sf_oldin &&
1861 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1862 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1863 psf->sf_next = pmc->tomb;
1864 pmc->tomb = psf;
1865 rv = 1;
1866 } else
1867 #endif
1868 kfree(psf);
1870 return rv;
1873 #ifndef CONFIG_IP_MULTICAST
1874 #define igmp_ifc_event(x) do { } while (0)
1875 #endif
1877 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1878 int sfcount, __be32 *psfsrc, int delta)
1880 struct ip_mc_list *pmc;
1881 int changerec = 0;
1882 int i, err;
1884 if (!in_dev)
1885 return -ENODEV;
1886 rcu_read_lock();
1887 for_each_pmc_rcu(in_dev, pmc) {
1888 if (*pmca == pmc->multiaddr)
1889 break;
1891 if (!pmc) {
1892 /* MCA not found?? bug */
1893 rcu_read_unlock();
1894 return -ESRCH;
1896 spin_lock_bh(&pmc->lock);
1897 rcu_read_unlock();
1898 #ifdef CONFIG_IP_MULTICAST
1899 sf_markstate(pmc);
1900 #endif
1901 if (!delta) {
1902 err = -EINVAL;
1903 if (!pmc->sfcount[sfmode])
1904 goto out_unlock;
1905 pmc->sfcount[sfmode]--;
1907 err = 0;
1908 for (i = 0; i < sfcount; i++) {
1909 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1911 changerec |= rv > 0;
1912 if (!err && rv < 0)
1913 err = rv;
1915 if (pmc->sfmode == MCAST_EXCLUDE &&
1916 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1917 pmc->sfcount[MCAST_INCLUDE]) {
1918 #ifdef CONFIG_IP_MULTICAST
1919 struct ip_sf_list *psf;
1920 struct net *net = dev_net(in_dev->dev);
1921 #endif
1923 /* filter mode change */
1924 pmc->sfmode = MCAST_INCLUDE;
1925 #ifdef CONFIG_IP_MULTICAST
1926 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1927 in_dev->mr_ifc_count = pmc->crcount;
1928 for (psf = pmc->sources; psf; psf = psf->sf_next)
1929 psf->sf_crcount = 0;
1930 igmp_ifc_event(pmc->interface);
1931 } else if (sf_setstate(pmc) || changerec) {
1932 igmp_ifc_event(pmc->interface);
1933 #endif
1935 out_unlock:
1936 spin_unlock_bh(&pmc->lock);
1937 return err;
1941 * Add multicast single-source filter to the interface list
1943 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1944 __be32 *psfsrc)
1946 struct ip_sf_list *psf, *psf_prev;
1948 psf_prev = NULL;
1949 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1950 if (psf->sf_inaddr == *psfsrc)
1951 break;
1952 psf_prev = psf;
1954 if (!psf) {
1955 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1956 if (!psf)
1957 return -ENOBUFS;
1958 psf->sf_inaddr = *psfsrc;
1959 if (psf_prev) {
1960 psf_prev->sf_next = psf;
1961 } else
1962 pmc->sources = psf;
1964 psf->sf_count[sfmode]++;
1965 if (psf->sf_count[sfmode] == 1) {
1966 ip_rt_multicast_event(pmc->interface);
1968 return 0;
1971 #ifdef CONFIG_IP_MULTICAST
1972 static void sf_markstate(struct ip_mc_list *pmc)
1974 struct ip_sf_list *psf;
1975 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1977 for (psf = pmc->sources; psf; psf = psf->sf_next)
1978 if (pmc->sfcount[MCAST_EXCLUDE]) {
1979 psf->sf_oldin = mca_xcount ==
1980 psf->sf_count[MCAST_EXCLUDE] &&
1981 !psf->sf_count[MCAST_INCLUDE];
1982 } else
1983 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1986 static int sf_setstate(struct ip_mc_list *pmc)
1988 struct ip_sf_list *psf, *dpsf;
1989 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1990 int qrv = pmc->interface->mr_qrv;
1991 int new_in, rv;
1993 rv = 0;
1994 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1995 if (pmc->sfcount[MCAST_EXCLUDE]) {
1996 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1997 !psf->sf_count[MCAST_INCLUDE];
1998 } else
1999 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2000 if (new_in) {
2001 if (!psf->sf_oldin) {
2002 struct ip_sf_list *prev = NULL;
2004 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2005 if (dpsf->sf_inaddr == psf->sf_inaddr)
2006 break;
2007 prev = dpsf;
2009 if (dpsf) {
2010 if (prev)
2011 prev->sf_next = dpsf->sf_next;
2012 else
2013 pmc->tomb = dpsf->sf_next;
2014 kfree(dpsf);
2016 psf->sf_crcount = qrv;
2017 rv++;
2019 } else if (psf->sf_oldin) {
2021 psf->sf_crcount = 0;
2023 * add or update "delete" records if an active filter
2024 * is now inactive
2026 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2027 if (dpsf->sf_inaddr == psf->sf_inaddr)
2028 break;
2029 if (!dpsf) {
2030 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2031 if (!dpsf)
2032 continue;
2033 *dpsf = *psf;
2034 /* pmc->lock held by callers */
2035 dpsf->sf_next = pmc->tomb;
2036 pmc->tomb = dpsf;
2038 dpsf->sf_crcount = qrv;
2039 rv++;
2042 return rv;
2044 #endif
2047 * Add multicast source filter list to the interface list
2049 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2050 int sfcount, __be32 *psfsrc, int delta)
2052 struct ip_mc_list *pmc;
2053 int isexclude;
2054 int i, err;
2056 if (!in_dev)
2057 return -ENODEV;
2058 rcu_read_lock();
2059 for_each_pmc_rcu(in_dev, pmc) {
2060 if (*pmca == pmc->multiaddr)
2061 break;
2063 if (!pmc) {
2064 /* MCA not found?? bug */
2065 rcu_read_unlock();
2066 return -ESRCH;
2068 spin_lock_bh(&pmc->lock);
2069 rcu_read_unlock();
2071 #ifdef CONFIG_IP_MULTICAST
2072 sf_markstate(pmc);
2073 #endif
2074 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2075 if (!delta)
2076 pmc->sfcount[sfmode]++;
2077 err = 0;
2078 for (i = 0; i < sfcount; i++) {
2079 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2080 if (err)
2081 break;
2083 if (err) {
2084 int j;
2086 if (!delta)
2087 pmc->sfcount[sfmode]--;
2088 for (j = 0; j < i; j++)
2089 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2090 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2091 #ifdef CONFIG_IP_MULTICAST
2092 struct ip_sf_list *psf;
2093 struct net *net = dev_net(pmc->interface->dev);
2094 in_dev = pmc->interface;
2095 #endif
2097 /* filter mode change */
2098 if (pmc->sfcount[MCAST_EXCLUDE])
2099 pmc->sfmode = MCAST_EXCLUDE;
2100 else if (pmc->sfcount[MCAST_INCLUDE])
2101 pmc->sfmode = MCAST_INCLUDE;
2102 #ifdef CONFIG_IP_MULTICAST
2103 /* else no filters; keep old mode for reports */
2105 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2106 in_dev->mr_ifc_count = pmc->crcount;
2107 for (psf = pmc->sources; psf; psf = psf->sf_next)
2108 psf->sf_crcount = 0;
2109 igmp_ifc_event(in_dev);
2110 } else if (sf_setstate(pmc)) {
2111 igmp_ifc_event(in_dev);
2112 #endif
2114 spin_unlock_bh(&pmc->lock);
2115 return err;
2118 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2120 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
2122 spin_lock_bh(&pmc->lock);
2123 tomb = pmc->tomb;
2124 pmc->tomb = NULL;
2125 sources = pmc->sources;
2126 pmc->sources = NULL;
2127 pmc->sfmode = MCAST_EXCLUDE;
2128 pmc->sfcount[MCAST_INCLUDE] = 0;
2129 pmc->sfcount[MCAST_EXCLUDE] = 1;
2130 spin_unlock_bh(&pmc->lock);
2132 for (psf = tomb; psf; psf = nextpsf) {
2133 nextpsf = psf->sf_next;
2134 kfree(psf);
2136 for (psf = sources; psf; psf = nextpsf) {
2137 nextpsf = psf->sf_next;
2138 kfree(psf);
2142 /* Join a multicast group
2144 static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2145 unsigned int mode)
2147 __be32 addr = imr->imr_multiaddr.s_addr;
2148 struct ip_mc_socklist *iml, *i;
2149 struct in_device *in_dev;
2150 struct inet_sock *inet = inet_sk(sk);
2151 struct net *net = sock_net(sk);
2152 int ifindex;
2153 int count = 0;
2154 int err;
2156 ASSERT_RTNL();
2158 if (!ipv4_is_multicast(addr))
2159 return -EINVAL;
2161 in_dev = ip_mc_find_dev(net, imr);
2163 if (!in_dev) {
2164 err = -ENODEV;
2165 goto done;
2168 err = -EADDRINUSE;
2169 ifindex = imr->imr_ifindex;
2170 for_each_pmc_rtnl(inet, i) {
2171 if (i->multi.imr_multiaddr.s_addr == addr &&
2172 i->multi.imr_ifindex == ifindex)
2173 goto done;
2174 count++;
2176 err = -ENOBUFS;
2177 if (count >= net->ipv4.sysctl_igmp_max_memberships)
2178 goto done;
2179 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2180 if (!iml)
2181 goto done;
2183 memcpy(&iml->multi, imr, sizeof(*imr));
2184 iml->next_rcu = inet->mc_list;
2185 iml->sflist = NULL;
2186 iml->sfmode = mode;
2187 rcu_assign_pointer(inet->mc_list, iml);
2188 __ip_mc_inc_group(in_dev, addr, mode);
2189 err = 0;
2190 done:
2191 return err;
2194 /* Join ASM (Any-Source Multicast) group
2196 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2198 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2200 EXPORT_SYMBOL(ip_mc_join_group);
2202 /* Join SSM (Source-Specific Multicast) group
2204 int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2205 unsigned int mode)
2207 return __ip_mc_join_group(sk, imr, mode);
2210 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2211 struct in_device *in_dev)
2213 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2214 int err;
2216 if (!psf) {
2217 /* any-source empty exclude case */
2218 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2219 iml->sfmode, 0, NULL, 0);
2221 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2222 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2223 RCU_INIT_POINTER(iml->sflist, NULL);
2224 /* decrease mem now to avoid the memleak warning */
2225 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2226 kfree_rcu(psf, rcu);
2227 return err;
2230 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2232 struct inet_sock *inet = inet_sk(sk);
2233 struct ip_mc_socklist *iml;
2234 struct ip_mc_socklist __rcu **imlp;
2235 struct in_device *in_dev;
2236 struct net *net = sock_net(sk);
2237 __be32 group = imr->imr_multiaddr.s_addr;
2238 u32 ifindex;
2239 int ret = -EADDRNOTAVAIL;
2241 ASSERT_RTNL();
2243 in_dev = ip_mc_find_dev(net, imr);
2244 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2245 ret = -ENODEV;
2246 goto out;
2248 ifindex = imr->imr_ifindex;
2249 for (imlp = &inet->mc_list;
2250 (iml = rtnl_dereference(*imlp)) != NULL;
2251 imlp = &iml->next_rcu) {
2252 if (iml->multi.imr_multiaddr.s_addr != group)
2253 continue;
2254 if (ifindex) {
2255 if (iml->multi.imr_ifindex != ifindex)
2256 continue;
2257 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2258 iml->multi.imr_address.s_addr)
2259 continue;
2261 (void) ip_mc_leave_src(sk, iml, in_dev);
2263 *imlp = iml->next_rcu;
2265 if (in_dev)
2266 ip_mc_dec_group(in_dev, group);
2268 /* decrease mem now to avoid the memleak warning */
2269 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2270 kfree_rcu(iml, rcu);
2271 return 0;
2273 out:
2274 return ret;
2276 EXPORT_SYMBOL(ip_mc_leave_group);
2278 int ip_mc_source(int add, int omode, struct sock *sk, struct
2279 ip_mreq_source *mreqs, int ifindex)
2281 int err;
2282 struct ip_mreqn imr;
2283 __be32 addr = mreqs->imr_multiaddr;
2284 struct ip_mc_socklist *pmc;
2285 struct in_device *in_dev = NULL;
2286 struct inet_sock *inet = inet_sk(sk);
2287 struct ip_sf_socklist *psl;
2288 struct net *net = sock_net(sk);
2289 int leavegroup = 0;
2290 int i, j, rv;
2292 if (!ipv4_is_multicast(addr))
2293 return -EINVAL;
2295 ASSERT_RTNL();
2297 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2298 imr.imr_address.s_addr = mreqs->imr_interface;
2299 imr.imr_ifindex = ifindex;
2300 in_dev = ip_mc_find_dev(net, &imr);
2302 if (!in_dev) {
2303 err = -ENODEV;
2304 goto done;
2306 err = -EADDRNOTAVAIL;
2308 for_each_pmc_rtnl(inet, pmc) {
2309 if ((pmc->multi.imr_multiaddr.s_addr ==
2310 imr.imr_multiaddr.s_addr) &&
2311 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2312 break;
2314 if (!pmc) { /* must have a prior join */
2315 err = -EINVAL;
2316 goto done;
2318 /* if a source filter was set, must be the same mode as before */
2319 if (pmc->sflist) {
2320 if (pmc->sfmode != omode) {
2321 err = -EINVAL;
2322 goto done;
2324 } else if (pmc->sfmode != omode) {
2325 /* allow mode switches for empty-set filters */
2326 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2327 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2328 NULL, 0);
2329 pmc->sfmode = omode;
2332 psl = rtnl_dereference(pmc->sflist);
2333 if (!add) {
2334 if (!psl)
2335 goto done; /* err = -EADDRNOTAVAIL */
2336 rv = !0;
2337 for (i = 0; i < psl->sl_count; i++) {
2338 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2339 sizeof(__be32));
2340 if (rv == 0)
2341 break;
2343 if (rv) /* source not found */
2344 goto done; /* err = -EADDRNOTAVAIL */
2346 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2347 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2348 leavegroup = 1;
2349 goto done;
2352 /* update the interface filter */
2353 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2354 &mreqs->imr_sourceaddr, 1);
2356 for (j = i+1; j < psl->sl_count; j++)
2357 psl->sl_addr[j-1] = psl->sl_addr[j];
2358 psl->sl_count--;
2359 err = 0;
2360 goto done;
2362 /* else, add a new source to the filter */
2364 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2365 err = -ENOBUFS;
2366 goto done;
2368 if (!psl || psl->sl_count == psl->sl_max) {
2369 struct ip_sf_socklist *newpsl;
2370 int count = IP_SFBLOCK;
2372 if (psl)
2373 count += psl->sl_max;
2374 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2375 if (!newpsl) {
2376 err = -ENOBUFS;
2377 goto done;
2379 newpsl->sl_max = count;
2380 newpsl->sl_count = count - IP_SFBLOCK;
2381 if (psl) {
2382 for (i = 0; i < psl->sl_count; i++)
2383 newpsl->sl_addr[i] = psl->sl_addr[i];
2384 /* decrease mem now to avoid the memleak warning */
2385 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2386 kfree_rcu(psl, rcu);
2388 rcu_assign_pointer(pmc->sflist, newpsl);
2389 psl = newpsl;
2391 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2392 for (i = 0; i < psl->sl_count; i++) {
2393 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2394 sizeof(__be32));
2395 if (rv == 0)
2396 break;
2398 if (rv == 0) /* address already there is an error */
2399 goto done;
2400 for (j = psl->sl_count-1; j >= i; j--)
2401 psl->sl_addr[j+1] = psl->sl_addr[j];
2402 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2403 psl->sl_count++;
2404 err = 0;
2405 /* update the interface list */
2406 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2407 &mreqs->imr_sourceaddr, 1);
2408 done:
2409 if (leavegroup)
2410 err = ip_mc_leave_group(sk, &imr);
2411 return err;
2414 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2416 int err = 0;
2417 struct ip_mreqn imr;
2418 __be32 addr = msf->imsf_multiaddr;
2419 struct ip_mc_socklist *pmc;
2420 struct in_device *in_dev;
2421 struct inet_sock *inet = inet_sk(sk);
2422 struct ip_sf_socklist *newpsl, *psl;
2423 struct net *net = sock_net(sk);
2424 int leavegroup = 0;
2426 if (!ipv4_is_multicast(addr))
2427 return -EINVAL;
2428 if (msf->imsf_fmode != MCAST_INCLUDE &&
2429 msf->imsf_fmode != MCAST_EXCLUDE)
2430 return -EINVAL;
2432 ASSERT_RTNL();
2434 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2435 imr.imr_address.s_addr = msf->imsf_interface;
2436 imr.imr_ifindex = ifindex;
2437 in_dev = ip_mc_find_dev(net, &imr);
2439 if (!in_dev) {
2440 err = -ENODEV;
2441 goto done;
2444 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2445 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2446 leavegroup = 1;
2447 goto done;
2450 for_each_pmc_rtnl(inet, pmc) {
2451 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2452 pmc->multi.imr_ifindex == imr.imr_ifindex)
2453 break;
2455 if (!pmc) { /* must have a prior join */
2456 err = -EINVAL;
2457 goto done;
2459 if (msf->imsf_numsrc) {
2460 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2461 GFP_KERNEL);
2462 if (!newpsl) {
2463 err = -ENOBUFS;
2464 goto done;
2466 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2467 memcpy(newpsl->sl_addr, msf->imsf_slist,
2468 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2469 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2470 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2471 if (err) {
2472 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2473 goto done;
2475 } else {
2476 newpsl = NULL;
2477 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2478 msf->imsf_fmode, 0, NULL, 0);
2480 psl = rtnl_dereference(pmc->sflist);
2481 if (psl) {
2482 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2483 psl->sl_count, psl->sl_addr, 0);
2484 /* decrease mem now to avoid the memleak warning */
2485 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2486 kfree_rcu(psl, rcu);
2487 } else
2488 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2489 0, NULL, 0);
2490 rcu_assign_pointer(pmc->sflist, newpsl);
2491 pmc->sfmode = msf->imsf_fmode;
2492 err = 0;
2493 done:
2494 if (leavegroup)
2495 err = ip_mc_leave_group(sk, &imr);
2496 return err;
2499 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2500 struct ip_msfilter __user *optval, int __user *optlen)
2502 int err, len, count, copycount;
2503 struct ip_mreqn imr;
2504 __be32 addr = msf->imsf_multiaddr;
2505 struct ip_mc_socklist *pmc;
2506 struct in_device *in_dev;
2507 struct inet_sock *inet = inet_sk(sk);
2508 struct ip_sf_socklist *psl;
2509 struct net *net = sock_net(sk);
2511 ASSERT_RTNL();
2513 if (!ipv4_is_multicast(addr))
2514 return -EINVAL;
2516 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2517 imr.imr_address.s_addr = msf->imsf_interface;
2518 imr.imr_ifindex = 0;
2519 in_dev = ip_mc_find_dev(net, &imr);
2521 if (!in_dev) {
2522 err = -ENODEV;
2523 goto done;
2525 err = -EADDRNOTAVAIL;
2527 for_each_pmc_rtnl(inet, pmc) {
2528 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2529 pmc->multi.imr_ifindex == imr.imr_ifindex)
2530 break;
2532 if (!pmc) /* must have a prior join */
2533 goto done;
2534 msf->imsf_fmode = pmc->sfmode;
2535 psl = rtnl_dereference(pmc->sflist);
2536 if (!psl) {
2537 len = 0;
2538 count = 0;
2539 } else {
2540 count = psl->sl_count;
2542 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2543 len = copycount * sizeof(psl->sl_addr[0]);
2544 msf->imsf_numsrc = count;
2545 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2546 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2547 return -EFAULT;
2549 if (len &&
2550 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2551 return -EFAULT;
2552 return 0;
2553 done:
2554 return err;
2557 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2558 struct group_filter __user *optval, int __user *optlen)
2560 int err, i, count, copycount;
2561 struct sockaddr_in *psin;
2562 __be32 addr;
2563 struct ip_mc_socklist *pmc;
2564 struct inet_sock *inet = inet_sk(sk);
2565 struct ip_sf_socklist *psl;
2567 ASSERT_RTNL();
2569 psin = (struct sockaddr_in *)&gsf->gf_group;
2570 if (psin->sin_family != AF_INET)
2571 return -EINVAL;
2572 addr = psin->sin_addr.s_addr;
2573 if (!ipv4_is_multicast(addr))
2574 return -EINVAL;
2576 err = -EADDRNOTAVAIL;
2578 for_each_pmc_rtnl(inet, pmc) {
2579 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2580 pmc->multi.imr_ifindex == gsf->gf_interface)
2581 break;
2583 if (!pmc) /* must have a prior join */
2584 goto done;
2585 gsf->gf_fmode = pmc->sfmode;
2586 psl = rtnl_dereference(pmc->sflist);
2587 count = psl ? psl->sl_count : 0;
2588 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2589 gsf->gf_numsrc = count;
2590 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2591 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2592 return -EFAULT;
2594 for (i = 0; i < copycount; i++) {
2595 struct sockaddr_storage ss;
2597 psin = (struct sockaddr_in *)&ss;
2598 memset(&ss, 0, sizeof(ss));
2599 psin->sin_family = AF_INET;
2600 psin->sin_addr.s_addr = psl->sl_addr[i];
2601 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2602 return -EFAULT;
2604 return 0;
2605 done:
2606 return err;
2610 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2612 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2613 int dif, int sdif)
2615 struct inet_sock *inet = inet_sk(sk);
2616 struct ip_mc_socklist *pmc;
2617 struct ip_sf_socklist *psl;
2618 int i;
2619 int ret;
2621 ret = 1;
2622 if (!ipv4_is_multicast(loc_addr))
2623 goto out;
2625 rcu_read_lock();
2626 for_each_pmc_rcu(inet, pmc) {
2627 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2628 (pmc->multi.imr_ifindex == dif ||
2629 (sdif && pmc->multi.imr_ifindex == sdif)))
2630 break;
2632 ret = inet->mc_all;
2633 if (!pmc)
2634 goto unlock;
2635 psl = rcu_dereference(pmc->sflist);
2636 ret = (pmc->sfmode == MCAST_EXCLUDE);
2637 if (!psl)
2638 goto unlock;
2640 for (i = 0; i < psl->sl_count; i++) {
2641 if (psl->sl_addr[i] == rmt_addr)
2642 break;
2644 ret = 0;
2645 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2646 goto unlock;
2647 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2648 goto unlock;
2649 ret = 1;
2650 unlock:
2651 rcu_read_unlock();
2652 out:
2653 return ret;
2657 * A socket is closing.
2660 void ip_mc_drop_socket(struct sock *sk)
2662 struct inet_sock *inet = inet_sk(sk);
2663 struct ip_mc_socklist *iml;
2664 struct net *net = sock_net(sk);
2666 if (!inet->mc_list)
2667 return;
2669 rtnl_lock();
2670 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2671 struct in_device *in_dev;
2673 inet->mc_list = iml->next_rcu;
2674 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2675 (void) ip_mc_leave_src(sk, iml, in_dev);
2676 if (in_dev)
2677 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2678 /* decrease mem now to avoid the memleak warning */
2679 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2680 kfree_rcu(iml, rcu);
2682 rtnl_unlock();
2685 /* called with rcu_read_lock() */
2686 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2688 struct ip_mc_list *im;
2689 struct ip_mc_list __rcu **mc_hash;
2690 struct ip_sf_list *psf;
2691 int rv = 0;
2693 mc_hash = rcu_dereference(in_dev->mc_hash);
2694 if (mc_hash) {
2695 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2697 for (im = rcu_dereference(mc_hash[hash]);
2698 im != NULL;
2699 im = rcu_dereference(im->next_hash)) {
2700 if (im->multiaddr == mc_addr)
2701 break;
2703 } else {
2704 for_each_pmc_rcu(in_dev, im) {
2705 if (im->multiaddr == mc_addr)
2706 break;
2709 if (im && proto == IPPROTO_IGMP) {
2710 rv = 1;
2711 } else if (im) {
2712 if (src_addr) {
2713 for (psf = im->sources; psf; psf = psf->sf_next) {
2714 if (psf->sf_inaddr == src_addr)
2715 break;
2717 if (psf)
2718 rv = psf->sf_count[MCAST_INCLUDE] ||
2719 psf->sf_count[MCAST_EXCLUDE] !=
2720 im->sfcount[MCAST_EXCLUDE];
2721 else
2722 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2723 } else
2724 rv = 1; /* unspecified source; tentatively allow */
2726 return rv;
2729 #if defined(CONFIG_PROC_FS)
2730 struct igmp_mc_iter_state {
2731 struct seq_net_private p;
2732 struct net_device *dev;
2733 struct in_device *in_dev;
2736 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2738 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2740 struct net *net = seq_file_net(seq);
2741 struct ip_mc_list *im = NULL;
2742 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2744 state->in_dev = NULL;
2745 for_each_netdev_rcu(net, state->dev) {
2746 struct in_device *in_dev;
2748 in_dev = __in_dev_get_rcu(state->dev);
2749 if (!in_dev)
2750 continue;
2751 im = rcu_dereference(in_dev->mc_list);
2752 if (im) {
2753 state->in_dev = in_dev;
2754 break;
2757 return im;
2760 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2762 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2764 im = rcu_dereference(im->next_rcu);
2765 while (!im) {
2766 state->dev = next_net_device_rcu(state->dev);
2767 if (!state->dev) {
2768 state->in_dev = NULL;
2769 break;
2771 state->in_dev = __in_dev_get_rcu(state->dev);
2772 if (!state->in_dev)
2773 continue;
2774 im = rcu_dereference(state->in_dev->mc_list);
2776 return im;
2779 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2781 struct ip_mc_list *im = igmp_mc_get_first(seq);
2782 if (im)
2783 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2784 --pos;
2785 return pos ? NULL : im;
2788 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2789 __acquires(rcu)
2791 rcu_read_lock();
2792 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2795 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2797 struct ip_mc_list *im;
2798 if (v == SEQ_START_TOKEN)
2799 im = igmp_mc_get_first(seq);
2800 else
2801 im = igmp_mc_get_next(seq, v);
2802 ++*pos;
2803 return im;
2806 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2807 __releases(rcu)
2809 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2811 state->in_dev = NULL;
2812 state->dev = NULL;
2813 rcu_read_unlock();
2816 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2818 if (v == SEQ_START_TOKEN)
2819 seq_puts(seq,
2820 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2821 else {
2822 struct ip_mc_list *im = (struct ip_mc_list *)v;
2823 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2824 char *querier;
2825 long delta;
2827 #ifdef CONFIG_IP_MULTICAST
2828 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2829 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2830 "V3";
2831 #else
2832 querier = "NONE";
2833 #endif
2835 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2836 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2837 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2840 delta = im->timer.expires - jiffies;
2841 seq_printf(seq,
2842 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2843 im->multiaddr, im->users,
2844 im->tm_running,
2845 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2846 im->reporter);
2848 return 0;
2851 static const struct seq_operations igmp_mc_seq_ops = {
2852 .start = igmp_mc_seq_start,
2853 .next = igmp_mc_seq_next,
2854 .stop = igmp_mc_seq_stop,
2855 .show = igmp_mc_seq_show,
2858 struct igmp_mcf_iter_state {
2859 struct seq_net_private p;
2860 struct net_device *dev;
2861 struct in_device *idev;
2862 struct ip_mc_list *im;
2865 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2867 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2869 struct net *net = seq_file_net(seq);
2870 struct ip_sf_list *psf = NULL;
2871 struct ip_mc_list *im = NULL;
2872 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2874 state->idev = NULL;
2875 state->im = NULL;
2876 for_each_netdev_rcu(net, state->dev) {
2877 struct in_device *idev;
2878 idev = __in_dev_get_rcu(state->dev);
2879 if (unlikely(!idev))
2880 continue;
2881 im = rcu_dereference(idev->mc_list);
2882 if (likely(im)) {
2883 spin_lock_bh(&im->lock);
2884 psf = im->sources;
2885 if (likely(psf)) {
2886 state->im = im;
2887 state->idev = idev;
2888 break;
2890 spin_unlock_bh(&im->lock);
2893 return psf;
2896 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2898 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2900 psf = psf->sf_next;
2901 while (!psf) {
2902 spin_unlock_bh(&state->im->lock);
2903 state->im = state->im->next;
2904 while (!state->im) {
2905 state->dev = next_net_device_rcu(state->dev);
2906 if (!state->dev) {
2907 state->idev = NULL;
2908 goto out;
2910 state->idev = __in_dev_get_rcu(state->dev);
2911 if (!state->idev)
2912 continue;
2913 state->im = rcu_dereference(state->idev->mc_list);
2915 if (!state->im)
2916 break;
2917 spin_lock_bh(&state->im->lock);
2918 psf = state->im->sources;
2920 out:
2921 return psf;
2924 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2926 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2927 if (psf)
2928 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2929 --pos;
2930 return pos ? NULL : psf;
2933 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2934 __acquires(rcu)
2936 rcu_read_lock();
2937 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2940 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2942 struct ip_sf_list *psf;
2943 if (v == SEQ_START_TOKEN)
2944 psf = igmp_mcf_get_first(seq);
2945 else
2946 psf = igmp_mcf_get_next(seq, v);
2947 ++*pos;
2948 return psf;
2951 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2952 __releases(rcu)
2954 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2955 if (likely(state->im)) {
2956 spin_unlock_bh(&state->im->lock);
2957 state->im = NULL;
2959 state->idev = NULL;
2960 state->dev = NULL;
2961 rcu_read_unlock();
2964 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2966 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2967 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2969 if (v == SEQ_START_TOKEN) {
2970 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2971 } else {
2972 seq_printf(seq,
2973 "%3d %6.6s 0x%08x "
2974 "0x%08x %6lu %6lu\n",
2975 state->dev->ifindex, state->dev->name,
2976 ntohl(state->im->multiaddr),
2977 ntohl(psf->sf_inaddr),
2978 psf->sf_count[MCAST_INCLUDE],
2979 psf->sf_count[MCAST_EXCLUDE]);
2981 return 0;
2984 static const struct seq_operations igmp_mcf_seq_ops = {
2985 .start = igmp_mcf_seq_start,
2986 .next = igmp_mcf_seq_next,
2987 .stop = igmp_mcf_seq_stop,
2988 .show = igmp_mcf_seq_show,
2991 static int __net_init igmp_net_init(struct net *net)
2993 struct proc_dir_entry *pde;
2994 int err;
2996 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
2997 sizeof(struct igmp_mc_iter_state));
2998 if (!pde)
2999 goto out_igmp;
3000 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3001 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
3002 if (!pde)
3003 goto out_mcfilter;
3004 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3005 SOCK_DGRAM, 0, net);
3006 if (err < 0) {
3007 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3008 err);
3009 goto out_sock;
3012 return 0;
3014 out_sock:
3015 remove_proc_entry("mcfilter", net->proc_net);
3016 out_mcfilter:
3017 remove_proc_entry("igmp", net->proc_net);
3018 out_igmp:
3019 return -ENOMEM;
3022 static void __net_exit igmp_net_exit(struct net *net)
3024 remove_proc_entry("mcfilter", net->proc_net);
3025 remove_proc_entry("igmp", net->proc_net);
3026 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3029 static struct pernet_operations igmp_net_ops = {
3030 .init = igmp_net_init,
3031 .exit = igmp_net_exit,
3033 #endif
3035 static int igmp_netdev_event(struct notifier_block *this,
3036 unsigned long event, void *ptr)
3038 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3039 struct in_device *in_dev;
3041 switch (event) {
3042 case NETDEV_RESEND_IGMP:
3043 in_dev = __in_dev_get_rtnl(dev);
3044 if (in_dev)
3045 ip_mc_rejoin_groups(in_dev);
3046 break;
3047 default:
3048 break;
3050 return NOTIFY_DONE;
3053 static struct notifier_block igmp_notifier = {
3054 .notifier_call = igmp_netdev_event,
3057 int __init igmp_mc_init(void)
3059 #if defined(CONFIG_PROC_FS)
3060 int err;
3062 err = register_pernet_subsys(&igmp_net_ops);
3063 if (err)
3064 return err;
3065 err = register_netdevice_notifier(&igmp_notifier);
3066 if (err)
3067 goto reg_notif_fail;
3068 return 0;
3070 reg_notif_fail:
3071 unregister_pernet_subsys(&igmp_net_ops);
3072 return err;
3073 #else
3074 return register_netdevice_notifier(&igmp_notifier);
3075 #endif