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.
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.
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
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
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"
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
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
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <asm/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>
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>
97 #include <net/protocol.h>
98 #include <net/route.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>
106 #ifdef CONFIG_PROC_FS
107 #include <linux/proc_fs.h>
108 #include <linux/seq_file.h>
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(
149 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
151 interval_ms = IN_DEV_CONF_GET(
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);
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 (atomic_dec_and_test(&im->refcnt)) {
178 in_dev_put(im->interface);
183 #define for_each_pmc_rcu(in_dev, pmc) \
184 for (pmc = rcu_dereference(in_dev->mc_list); \
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); \
191 pmc = rtnl_dereference(pmc->next_rcu))
193 static void ip_sf_list_clear_all(struct ip_sf_list *psf)
195 struct ip_sf_list *next;
204 #ifdef CONFIG_IP_MULTICAST
210 static void igmp_stop_timer(struct ip_mc_list *im)
212 spin_lock_bh(&im->lock);
213 if (del_timer(&im->timer))
214 atomic_dec(&im->refcnt);
217 im->unsolicit_count = 0;
218 spin_unlock_bh(&im->lock);
221 /* It must be called with locked im->lock */
222 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
224 int tv = prandom_u32() % max_delay;
227 if (!mod_timer(&im->timer, jiffies+tv+2))
228 atomic_inc(&im->refcnt);
231 static void igmp_gq_start_timer(struct in_device *in_dev)
233 int tv = prandom_u32() % in_dev->mr_maxdelay;
234 unsigned long exp = jiffies + tv + 2;
236 if (in_dev->mr_gq_running &&
237 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
240 in_dev->mr_gq_running = 1;
241 if (!mod_timer(&in_dev->mr_gq_timer, exp))
245 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
247 int tv = prandom_u32() % delay;
249 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
253 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
255 spin_lock_bh(&im->lock);
256 im->unsolicit_count = 0;
257 if (del_timer(&im->timer)) {
258 if ((long)(im->timer.expires-jiffies) < max_delay) {
259 add_timer(&im->timer);
261 spin_unlock_bh(&im->lock);
264 atomic_dec(&im->refcnt);
266 igmp_start_timer(im, max_delay);
267 spin_unlock_bh(&im->lock);
272 * Send an IGMP report.
275 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
278 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
279 int gdeleted, int sdeleted)
282 case IGMPV3_MODE_IS_INCLUDE:
283 case IGMPV3_MODE_IS_EXCLUDE:
284 if (gdeleted || sdeleted)
286 if (!(pmc->gsquery && !psf->sf_gsresp)) {
287 if (pmc->sfmode == MCAST_INCLUDE)
289 /* don't include if this source is excluded
292 if (psf->sf_count[MCAST_INCLUDE])
293 return type == IGMPV3_MODE_IS_INCLUDE;
294 return pmc->sfcount[MCAST_EXCLUDE] ==
295 psf->sf_count[MCAST_EXCLUDE];
298 case IGMPV3_CHANGE_TO_INCLUDE:
299 if (gdeleted || sdeleted)
301 return psf->sf_count[MCAST_INCLUDE] != 0;
302 case IGMPV3_CHANGE_TO_EXCLUDE:
303 if (gdeleted || sdeleted)
305 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
306 psf->sf_count[MCAST_INCLUDE])
308 return pmc->sfcount[MCAST_EXCLUDE] ==
309 psf->sf_count[MCAST_EXCLUDE];
310 case IGMPV3_ALLOW_NEW_SOURCES:
311 if (gdeleted || !psf->sf_crcount)
313 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
314 case IGMPV3_BLOCK_OLD_SOURCES:
315 if (pmc->sfmode == MCAST_INCLUDE)
316 return gdeleted || (psf->sf_crcount && sdeleted);
317 return psf->sf_crcount && !gdeleted && !sdeleted;
323 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
325 struct ip_sf_list *psf;
328 for (psf = pmc->sources; psf; psf = psf->sf_next) {
329 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
336 /* source address selection per RFC 3376 section 4.2.13 */
337 static __be32 igmpv3_get_srcaddr(struct net_device *dev,
338 const struct flowi4 *fl4)
340 struct in_device *in_dev = __in_dev_get_rcu(dev);
343 return htonl(INADDR_ANY);
346 if (fl4->saddr == ifa->ifa_local)
348 } endfor_ifa(in_dev);
350 return htonl(INADDR_ANY);
353 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
358 struct igmpv3_report *pig;
359 struct net *net = dev_net(dev);
361 int hlen = LL_RESERVED_SPACE(dev);
362 int tlen = dev->needed_tailroom;
363 unsigned int size = mtu;
366 skb = alloc_skb(size + hlen + tlen,
367 GFP_ATOMIC | __GFP_NOWARN);
374 skb->priority = TC_PRIO_CONTROL;
376 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
378 IPPROTO_IGMP, 0, dev->ifindex);
384 skb_dst_set(skb, &rt->dst);
387 skb_reserve(skb, hlen);
388 skb_tailroom_reserve(skb, mtu, tlen);
390 skb_reset_network_header(skb);
392 skb_put(skb, sizeof(struct iphdr) + 4);
395 pip->ihl = (sizeof(struct iphdr)+4)>>2;
397 pip->frag_off = htons(IP_DF);
399 pip->daddr = fl4.daddr;
402 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
405 pip->protocol = IPPROTO_IGMP;
406 pip->tot_len = 0; /* filled in later */
407 ip_select_ident(net, skb, NULL);
408 ((u8 *)&pip[1])[0] = IPOPT_RA;
409 ((u8 *)&pip[1])[1] = 4;
410 ((u8 *)&pip[1])[2] = 0;
411 ((u8 *)&pip[1])[3] = 0;
413 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
414 skb_put(skb, sizeof(*pig));
415 pig = igmpv3_report_hdr(skb);
416 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
424 static int igmpv3_sendpack(struct sk_buff *skb)
426 struct igmphdr *pig = igmp_hdr(skb);
427 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
429 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
431 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
434 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
436 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
439 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
440 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
442 struct net_device *dev = pmc->interface->dev;
443 struct igmpv3_report *pih;
444 struct igmpv3_grec *pgr;
447 skb = igmpv3_newpack(dev, mtu);
451 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
452 pgr->grec_type = type;
453 pgr->grec_auxwords = 0;
455 pgr->grec_mca = pmc->multiaddr;
456 pih = igmpv3_report_hdr(skb);
457 pih->ngrec = htons(ntohs(pih->ngrec)+1);
462 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
464 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
465 int type, int gdeleted, int sdeleted)
467 struct net_device *dev = pmc->interface->dev;
468 struct net *net = dev_net(dev);
469 struct igmpv3_report *pih;
470 struct igmpv3_grec *pgr = NULL;
471 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
472 int scount, stotal, first, isquery, truncate;
475 if (pmc->multiaddr == IGMP_ALL_HOSTS)
477 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
480 mtu = READ_ONCE(dev->mtu);
481 if (mtu < IPV4_MIN_MTU)
484 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
485 type == IGMPV3_MODE_IS_EXCLUDE;
486 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
487 type == IGMPV3_CHANGE_TO_EXCLUDE;
491 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
496 pih = skb ? igmpv3_report_hdr(skb) : NULL;
498 /* EX and TO_EX get a fresh packet, if needed */
500 if (pih && pih->ngrec &&
501 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
503 igmpv3_sendpack(skb);
504 skb = igmpv3_newpack(dev, mtu);
509 for (psf = *psf_list; psf; psf = psf_next) {
512 psf_next = psf->sf_next;
514 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
519 /* Based on RFC3376 5.1. Should not send source-list change
520 * records when there is a filter mode change.
522 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
523 (!gdeleted && pmc->crcount)) &&
524 (type == IGMPV3_ALLOW_NEW_SOURCES ||
525 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
526 goto decrease_sf_crcount;
528 /* clear marks on query responses */
532 if (AVAILABLE(skb) < sizeof(__be32) +
533 first*sizeof(struct igmpv3_grec)) {
534 if (truncate && !first)
535 break; /* truncate these */
537 pgr->grec_nsrcs = htons(scount);
539 igmpv3_sendpack(skb);
540 skb = igmpv3_newpack(dev, mtu);
545 skb = add_grhead(skb, pmc, type, &pgr, mtu);
550 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
551 *psrc = psf->sf_inaddr;
553 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
554 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
557 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
559 psf_prev->sf_next = psf->sf_next;
561 *psf_list = psf->sf_next;
571 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
572 type == IGMPV3_BLOCK_OLD_SOURCES)
574 if (pmc->crcount || isquery) {
575 /* make sure we have room for group header */
576 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
577 igmpv3_sendpack(skb);
578 skb = NULL; /* add_grhead will get a new one */
580 skb = add_grhead(skb, pmc, type, &pgr, mtu);
584 pgr->grec_nsrcs = htons(scount);
587 pmc->gsquery = 0; /* clear query state on report */
591 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
593 struct sk_buff *skb = NULL;
594 struct net *net = dev_net(in_dev->dev);
599 for_each_pmc_rcu(in_dev, pmc) {
600 if (pmc->multiaddr == IGMP_ALL_HOSTS)
602 if (ipv4_is_local_multicast(pmc->multiaddr) &&
603 !net->ipv4.sysctl_igmp_llm_reports)
605 spin_lock_bh(&pmc->lock);
606 if (pmc->sfcount[MCAST_EXCLUDE])
607 type = IGMPV3_MODE_IS_EXCLUDE;
609 type = IGMPV3_MODE_IS_INCLUDE;
610 skb = add_grec(skb, pmc, type, 0, 0);
611 spin_unlock_bh(&pmc->lock);
615 spin_lock_bh(&pmc->lock);
616 if (pmc->sfcount[MCAST_EXCLUDE])
617 type = IGMPV3_MODE_IS_EXCLUDE;
619 type = IGMPV3_MODE_IS_INCLUDE;
620 skb = add_grec(skb, pmc, type, 0, 0);
621 spin_unlock_bh(&pmc->lock);
625 return igmpv3_sendpack(skb);
629 * remove zero-count source records from a source filter list
631 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
633 struct ip_sf_list *psf_prev, *psf_next, *psf;
636 for (psf = *ppsf; psf; psf = psf_next) {
637 psf_next = psf->sf_next;
638 if (psf->sf_crcount == 0) {
640 psf_prev->sf_next = psf->sf_next;
642 *ppsf = psf->sf_next;
649 static void kfree_pmc(struct ip_mc_list *pmc)
651 ip_sf_list_clear_all(pmc->sources);
652 ip_sf_list_clear_all(pmc->tomb);
656 static void igmpv3_send_cr(struct in_device *in_dev)
658 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
659 struct sk_buff *skb = NULL;
663 spin_lock_bh(&in_dev->mc_tomb_lock);
667 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
668 pmc_next = pmc->next;
669 if (pmc->sfmode == MCAST_INCLUDE) {
670 type = IGMPV3_BLOCK_OLD_SOURCES;
671 dtype = IGMPV3_BLOCK_OLD_SOURCES;
672 skb = add_grec(skb, pmc, type, 1, 0);
673 skb = add_grec(skb, pmc, dtype, 1, 1);
676 if (pmc->sfmode == MCAST_EXCLUDE) {
677 type = IGMPV3_CHANGE_TO_INCLUDE;
678 skb = add_grec(skb, pmc, type, 1, 0);
681 if (pmc->crcount == 0) {
682 igmpv3_clear_zeros(&pmc->tomb);
683 igmpv3_clear_zeros(&pmc->sources);
686 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
688 pmc_prev->next = pmc_next;
690 in_dev->mc_tomb = pmc_next;
691 in_dev_put(pmc->interface);
696 spin_unlock_bh(&in_dev->mc_tomb_lock);
699 for_each_pmc_rcu(in_dev, pmc) {
700 spin_lock_bh(&pmc->lock);
701 if (pmc->sfcount[MCAST_EXCLUDE]) {
702 type = IGMPV3_BLOCK_OLD_SOURCES;
703 dtype = IGMPV3_ALLOW_NEW_SOURCES;
705 type = IGMPV3_ALLOW_NEW_SOURCES;
706 dtype = IGMPV3_BLOCK_OLD_SOURCES;
708 skb = add_grec(skb, pmc, type, 0, 0);
709 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
711 /* filter mode changes */
713 if (pmc->sfmode == MCAST_EXCLUDE)
714 type = IGMPV3_CHANGE_TO_EXCLUDE;
716 type = IGMPV3_CHANGE_TO_INCLUDE;
717 skb = add_grec(skb, pmc, type, 0, 0);
720 spin_unlock_bh(&pmc->lock);
726 (void) igmpv3_sendpack(skb);
729 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
736 struct net_device *dev = in_dev->dev;
737 struct net *net = dev_net(dev);
738 __be32 group = pmc ? pmc->multiaddr : 0;
743 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
744 return igmpv3_send_report(in_dev, pmc);
746 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
749 if (type == IGMP_HOST_LEAVE_MESSAGE)
750 dst = IGMP_ALL_ROUTER;
754 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
756 IPPROTO_IGMP, 0, dev->ifindex);
760 hlen = LL_RESERVED_SPACE(dev);
761 tlen = dev->needed_tailroom;
762 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
767 skb->priority = TC_PRIO_CONTROL;
769 skb_dst_set(skb, &rt->dst);
771 skb_reserve(skb, hlen);
773 skb_reset_network_header(skb);
775 skb_put(skb, sizeof(struct iphdr) + 4);
778 iph->ihl = (sizeof(struct iphdr)+4)>>2;
780 iph->frag_off = htons(IP_DF);
783 iph->saddr = fl4.saddr;
784 iph->protocol = IPPROTO_IGMP;
785 ip_select_ident(net, skb, NULL);
786 ((u8 *)&iph[1])[0] = IPOPT_RA;
787 ((u8 *)&iph[1])[1] = 4;
788 ((u8 *)&iph[1])[2] = 0;
789 ((u8 *)&iph[1])[3] = 0;
791 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
796 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
798 return ip_local_out(net, skb->sk, skb);
801 static void igmp_gq_timer_expire(unsigned long data)
803 struct in_device *in_dev = (struct in_device *)data;
805 in_dev->mr_gq_running = 0;
806 igmpv3_send_report(in_dev, NULL);
810 static void igmp_ifc_timer_expire(unsigned long data)
812 struct in_device *in_dev = (struct in_device *)data;
814 igmpv3_send_cr(in_dev);
815 if (in_dev->mr_ifc_count) {
816 in_dev->mr_ifc_count--;
817 igmp_ifc_start_timer(in_dev,
818 unsolicited_report_interval(in_dev));
823 static void igmp_ifc_event(struct in_device *in_dev)
825 struct net *net = dev_net(in_dev->dev);
826 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
828 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
829 igmp_ifc_start_timer(in_dev, 1);
833 static void igmp_timer_expire(unsigned long data)
835 struct ip_mc_list *im = (struct ip_mc_list *)data;
836 struct in_device *in_dev = im->interface;
838 spin_lock(&im->lock);
841 if (im->unsolicit_count) {
842 im->unsolicit_count--;
843 igmp_start_timer(im, unsolicited_report_interval(in_dev));
846 spin_unlock(&im->lock);
848 if (IGMP_V1_SEEN(in_dev))
849 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
850 else if (IGMP_V2_SEEN(in_dev))
851 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
853 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
858 /* mark EXCLUDE-mode sources */
859 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
861 struct ip_sf_list *psf;
865 for (psf = pmc->sources; psf; psf = psf->sf_next) {
868 for (i = 0; i < nsrcs; i++) {
869 /* skip inactive filters */
870 if (psf->sf_count[MCAST_INCLUDE] ||
871 pmc->sfcount[MCAST_EXCLUDE] !=
872 psf->sf_count[MCAST_EXCLUDE])
874 if (srcs[i] == psf->sf_inaddr) {
881 if (scount == nsrcs) /* all sources excluded */
886 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
888 struct ip_sf_list *psf;
891 if (pmc->sfmode == MCAST_EXCLUDE)
892 return igmp_xmarksources(pmc, nsrcs, srcs);
894 /* mark INCLUDE-mode sources */
896 for (psf = pmc->sources; psf; psf = psf->sf_next) {
899 for (i = 0; i < nsrcs; i++)
900 if (srcs[i] == psf->sf_inaddr) {
914 /* return true if packet was dropped */
915 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
917 struct ip_mc_list *im;
918 struct net *net = dev_net(in_dev->dev);
920 /* Timers are only set for non-local groups */
922 if (group == IGMP_ALL_HOSTS)
924 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
928 for_each_pmc_rcu(in_dev, im) {
929 if (im->multiaddr == group) {
938 /* return true if packet was dropped */
939 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
942 struct igmphdr *ih = igmp_hdr(skb);
943 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
944 struct ip_mc_list *im;
945 __be32 group = ih->group;
948 struct net *net = dev_net(in_dev->dev);
953 /* Alas, old v1 router presents here. */
955 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
956 in_dev->mr_v1_seen = jiffies +
957 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
960 /* v2 router present */
961 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
962 in_dev->mr_v2_seen = jiffies +
963 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
965 /* cancel the interface change timer */
966 in_dev->mr_ifc_count = 0;
967 if (del_timer(&in_dev->mr_ifc_timer))
968 __in_dev_put(in_dev);
969 /* clear deleted report items */
970 igmpv3_clear_delrec(in_dev);
971 } else if (len < 12) {
972 return true; /* ignore bogus packet; freed by caller */
973 } else if (IGMP_V1_SEEN(in_dev)) {
974 /* This is a v3 query with v1 queriers present */
975 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
977 } else if (IGMP_V2_SEEN(in_dev)) {
978 /* this is a v3 query with v2 queriers present;
979 * Interpretation of the max_delay code is problematic here.
980 * A real v2 host would use ih_code directly, while v3 has a
981 * different encoding. We use the v3 encoding as more likely
982 * to be intended in a v3 query.
984 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
986 max_delay = 1; /* can't mod w/ 0 */
988 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
991 ih3 = igmpv3_query_hdr(skb);
993 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
994 + ntohs(ih3->nsrcs)*sizeof(__be32)))
996 ih3 = igmpv3_query_hdr(skb);
999 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
1001 max_delay = 1; /* can't mod w/ 0 */
1002 in_dev->mr_maxdelay = max_delay;
1004 in_dev->mr_qrv = ih3->qrv;
1005 if (!group) { /* general query */
1007 return true; /* no sources allowed */
1008 igmp_gq_start_timer(in_dev);
1011 /* mark sources to include, if group & source-specific */
1012 mark = ih3->nsrcs != 0;
1016 * - Start the timers in all of our membership records
1017 * that the query applies to for the interface on
1018 * which the query arrived excl. those that belong
1019 * to a "local" group (224.0.0.X)
1020 * - For timers already running check if they need to
1022 * - Use the igmp->igmp_code field as the maximum
1026 for_each_pmc_rcu(in_dev, im) {
1029 if (group && group != im->multiaddr)
1031 if (im->multiaddr == IGMP_ALL_HOSTS)
1033 if (ipv4_is_local_multicast(im->multiaddr) &&
1034 !net->ipv4.sysctl_igmp_llm_reports)
1036 spin_lock_bh(&im->lock);
1038 im->gsquery = im->gsquery && mark;
1041 changed = !im->gsquery ||
1042 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1043 spin_unlock_bh(&im->lock);
1045 igmp_mod_timer(im, max_delay);
1051 /* called in rcu_read_lock() section */
1052 int igmp_rcv(struct sk_buff *skb)
1054 /* This basically follows the spec line by line -- see RFC1112 */
1056 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
1058 bool dropped = true;
1063 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1066 if (skb_checksum_simple_validate(skb))
1071 case IGMP_HOST_MEMBERSHIP_QUERY:
1072 dropped = igmp_heard_query(in_dev, skb, len);
1074 case IGMP_HOST_MEMBERSHIP_REPORT:
1075 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1076 /* Is it our report looped back? */
1077 if (rt_is_output_route(skb_rtable(skb)))
1079 /* don't rely on MC router hearing unicast reports */
1080 if (skb->pkt_type == PACKET_MULTICAST ||
1081 skb->pkt_type == PACKET_BROADCAST)
1082 dropped = igmp_heard_report(in_dev, ih->group);
1085 #ifdef CONFIG_IP_PIMSM_V1
1086 return pim_rcv_v1(skb);
1088 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1091 case IGMP_HOST_LEAVE_MESSAGE:
1093 case IGMP_MTRACE_RESP:
1111 * Add a filter to a device
1114 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1116 char buf[MAX_ADDR_LEN];
1117 struct net_device *dev = in_dev->dev;
1119 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1120 We will get multicast token leakage, when IFF_MULTICAST
1121 is changed. This check should be done in ndo_set_rx_mode
1122 routine. Something sort of:
1123 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1126 if (arp_mc_map(addr, buf, dev, 0) == 0)
1127 dev_mc_add(dev, buf);
1131 * Remove a filter from a device
1134 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1136 char buf[MAX_ADDR_LEN];
1137 struct net_device *dev = in_dev->dev;
1139 if (arp_mc_map(addr, buf, dev, 0) == 0)
1140 dev_mc_del(dev, buf);
1143 #ifdef CONFIG_IP_MULTICAST
1145 * deleted ip_mc_list manipulation
1147 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1149 struct ip_mc_list *pmc;
1150 struct net *net = dev_net(in_dev->dev);
1152 /* this is an "ip_mc_list" for convenience; only the fields below
1153 * are actually used. In particular, the refcnt and users are not
1154 * used for management of the delete list. Using the same structure
1155 * for deleted items allows change reports to use common code with
1156 * non-deleted or query-response MCA's.
1158 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1161 spin_lock_init(&pmc->lock);
1162 spin_lock_bh(&im->lock);
1163 pmc->interface = im->interface;
1164 in_dev_hold(in_dev);
1165 pmc->multiaddr = im->multiaddr;
1166 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1167 pmc->sfmode = im->sfmode;
1168 if (pmc->sfmode == MCAST_INCLUDE) {
1169 struct ip_sf_list *psf;
1171 pmc->tomb = im->tomb;
1172 pmc->sources = im->sources;
1173 im->tomb = im->sources = NULL;
1174 for (psf = pmc->sources; psf; psf = psf->sf_next)
1175 psf->sf_crcount = pmc->crcount;
1177 spin_unlock_bh(&im->lock);
1179 spin_lock_bh(&in_dev->mc_tomb_lock);
1180 pmc->next = in_dev->mc_tomb;
1181 in_dev->mc_tomb = pmc;
1182 spin_unlock_bh(&in_dev->mc_tomb_lock);
1186 * restore ip_mc_list deleted records
1188 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1190 struct ip_mc_list *pmc, *pmc_prev;
1191 struct ip_sf_list *psf;
1192 struct net *net = dev_net(in_dev->dev);
1193 __be32 multiaddr = im->multiaddr;
1195 spin_lock_bh(&in_dev->mc_tomb_lock);
1197 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1198 if (pmc->multiaddr == multiaddr)
1204 pmc_prev->next = pmc->next;
1206 in_dev->mc_tomb = pmc->next;
1208 spin_unlock_bh(&in_dev->mc_tomb_lock);
1210 spin_lock_bh(&im->lock);
1212 im->interface = pmc->interface;
1213 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1214 if (im->sfmode == MCAST_INCLUDE) {
1215 im->tomb = pmc->tomb;
1218 im->sources = pmc->sources;
1219 pmc->sources = NULL;
1221 for (psf = im->sources; psf; psf = psf->sf_next)
1222 psf->sf_crcount = im->crcount;
1224 in_dev_put(pmc->interface);
1227 spin_unlock_bh(&im->lock);
1231 * flush ip_mc_list deleted records
1233 static void igmpv3_clear_delrec(struct in_device *in_dev)
1235 struct ip_mc_list *pmc, *nextpmc;
1237 spin_lock_bh(&in_dev->mc_tomb_lock);
1238 pmc = in_dev->mc_tomb;
1239 in_dev->mc_tomb = NULL;
1240 spin_unlock_bh(&in_dev->mc_tomb_lock);
1242 for (; pmc; pmc = nextpmc) {
1243 nextpmc = pmc->next;
1244 ip_mc_clear_src(pmc);
1245 in_dev_put(pmc->interface);
1248 /* clear dead sources, too */
1250 for_each_pmc_rcu(in_dev, pmc) {
1251 struct ip_sf_list *psf;
1253 spin_lock_bh(&pmc->lock);
1256 spin_unlock_bh(&pmc->lock);
1257 ip_sf_list_clear_all(psf);
1263 static void igmp_group_dropped(struct ip_mc_list *im)
1265 struct in_device *in_dev = im->interface;
1266 #ifdef CONFIG_IP_MULTICAST
1267 struct net *net = dev_net(in_dev->dev);
1273 ip_mc_filter_del(in_dev, im->multiaddr);
1276 #ifdef CONFIG_IP_MULTICAST
1277 if (im->multiaddr == IGMP_ALL_HOSTS)
1279 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1282 reporter = im->reporter;
1283 igmp_stop_timer(im);
1285 if (!in_dev->dead) {
1286 if (IGMP_V1_SEEN(in_dev))
1288 if (IGMP_V2_SEEN(in_dev)) {
1290 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1294 igmpv3_add_delrec(in_dev, im);
1296 igmp_ifc_event(in_dev);
1301 static void igmp_group_added(struct ip_mc_list *im)
1303 struct in_device *in_dev = im->interface;
1304 #ifdef CONFIG_IP_MULTICAST
1305 struct net *net = dev_net(in_dev->dev);
1308 if (im->loaded == 0) {
1310 ip_mc_filter_add(in_dev, im->multiaddr);
1313 #ifdef CONFIG_IP_MULTICAST
1314 if (im->multiaddr == IGMP_ALL_HOSTS)
1316 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1321 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1322 spin_lock_bh(&im->lock);
1323 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1324 spin_unlock_bh(&im->lock);
1329 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1330 igmp_ifc_event(in_dev);
1336 * Multicast list managers
1339 static u32 ip_mc_hash(const struct ip_mc_list *im)
1341 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1344 static void ip_mc_hash_add(struct in_device *in_dev,
1345 struct ip_mc_list *im)
1347 struct ip_mc_list __rcu **mc_hash;
1350 mc_hash = rtnl_dereference(in_dev->mc_hash);
1352 hash = ip_mc_hash(im);
1353 im->next_hash = mc_hash[hash];
1354 rcu_assign_pointer(mc_hash[hash], im);
1358 /* do not use a hash table for small number of items */
1359 if (in_dev->mc_count < 4)
1362 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1367 for_each_pmc_rtnl(in_dev, im) {
1368 hash = ip_mc_hash(im);
1369 im->next_hash = mc_hash[hash];
1370 RCU_INIT_POINTER(mc_hash[hash], im);
1373 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1376 static void ip_mc_hash_remove(struct in_device *in_dev,
1377 struct ip_mc_list *im)
1379 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1380 struct ip_mc_list *aux;
1384 mc_hash += ip_mc_hash(im);
1385 while ((aux = rtnl_dereference(*mc_hash)) != im)
1386 mc_hash = &aux->next_hash;
1387 *mc_hash = im->next_hash;
1392 * A socket has joined a multicast group on device dev.
1395 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1397 struct ip_mc_list *im;
1398 #ifdef CONFIG_IP_MULTICAST
1399 struct net *net = dev_net(in_dev->dev);
1404 for_each_pmc_rtnl(in_dev, im) {
1405 if (im->multiaddr == addr) {
1407 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1412 im = kzalloc(sizeof(*im), GFP_KERNEL);
1417 im->interface = in_dev;
1418 in_dev_hold(in_dev);
1419 im->multiaddr = addr;
1420 /* initial mode is (EX, empty) */
1421 im->sfmode = MCAST_EXCLUDE;
1422 im->sfcount[MCAST_EXCLUDE] = 1;
1423 atomic_set(&im->refcnt, 1);
1424 spin_lock_init(&im->lock);
1425 #ifdef CONFIG_IP_MULTICAST
1426 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
1427 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1430 im->next_rcu = in_dev->mc_list;
1432 rcu_assign_pointer(in_dev->mc_list, im);
1434 ip_mc_hash_add(in_dev, im);
1436 #ifdef CONFIG_IP_MULTICAST
1437 igmpv3_del_delrec(in_dev, im);
1439 igmp_group_added(im);
1441 ip_rt_multicast_event(in_dev);
1445 EXPORT_SYMBOL(ip_mc_inc_group);
1447 static int ip_mc_check_iphdr(struct sk_buff *skb)
1449 const struct iphdr *iph;
1451 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1453 if (!pskb_may_pull(skb, offset))
1458 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1461 offset += ip_hdrlen(skb) - sizeof(*iph);
1463 if (!pskb_may_pull(skb, offset))
1468 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1471 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1472 if (skb->len < len || len < offset)
1475 skb_set_transport_header(skb, offset);
1480 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1482 unsigned int len = skb_transport_offset(skb);
1484 len += sizeof(struct igmpv3_report);
1486 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1489 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1491 unsigned int len = skb_transport_offset(skb);
1493 len += sizeof(struct igmphdr);
1498 if (skb->len != len) {
1500 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1501 if (skb->len < len || !pskb_may_pull(skb, len))
1505 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1506 * all-systems destination addresses (224.0.0.1) for general queries
1508 if (!igmp_hdr(skb)->group &&
1509 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1515 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1517 switch (igmp_hdr(skb)->type) {
1518 case IGMP_HOST_LEAVE_MESSAGE:
1519 case IGMP_HOST_MEMBERSHIP_REPORT:
1520 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1523 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1524 return ip_mc_check_igmp_reportv3(skb);
1525 case IGMP_HOST_MEMBERSHIP_QUERY:
1526 return ip_mc_check_igmp_query(skb);
1532 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1534 return skb_checksum_simple_validate(skb);
1537 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1540 struct sk_buff *skb_chk;
1541 unsigned int transport_len;
1542 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1545 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1547 skb_chk = skb_checksum_trimmed(skb, transport_len,
1548 ip_mc_validate_checksum);
1552 if (!pskb_may_pull(skb_chk, len))
1555 ret = ip_mc_check_igmp_msg(skb_chk);
1560 *skb_trimmed = skb_chk;
1561 /* free now unneeded clone */
1562 else if (skb_chk != skb)
1568 if (ret && skb_chk && skb_chk != skb)
1575 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1576 * @skb: the skb to validate
1577 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1579 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1580 * skb transport header accordingly and returns zero.
1582 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1584 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1585 * -ENOMEM: A memory allocation failure happened.
1587 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1588 * to NULL): After parsing an IGMP packet successfully it will point to
1589 * an skb which has its tail aligned to the IP packet end. This might
1590 * either be the originally provided skb or a trimmed, cloned version if
1591 * the skb frame had data beyond the IP packet. A cloned skb allows us
1592 * to leave the original skb and its full frame unchanged (which might be
1593 * desirable for layer 2 frame jugglers).
1595 * Caller needs to set the skb network header and free any returned skb if it
1596 * differs from the provided skb.
1598 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1600 int ret = ip_mc_check_iphdr(skb);
1605 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1608 return __ip_mc_check_igmp(skb, skb_trimmed);
1610 EXPORT_SYMBOL(ip_mc_check_igmp);
1613 * Resend IGMP JOIN report; used by netdev notifier.
1615 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1617 #ifdef CONFIG_IP_MULTICAST
1618 struct ip_mc_list *im;
1620 struct net *net = dev_net(in_dev->dev);
1624 for_each_pmc_rtnl(in_dev, im) {
1625 if (im->multiaddr == IGMP_ALL_HOSTS)
1627 if (ipv4_is_local_multicast(im->multiaddr) &&
1628 !net->ipv4.sysctl_igmp_llm_reports)
1631 /* a failover is happening and switches
1632 * must be notified immediately
1634 if (IGMP_V1_SEEN(in_dev))
1635 type = IGMP_HOST_MEMBERSHIP_REPORT;
1636 else if (IGMP_V2_SEEN(in_dev))
1637 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1639 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1640 igmp_send_report(in_dev, im, type);
1646 * A socket has left a multicast group on device dev
1649 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1651 struct ip_mc_list *i;
1652 struct ip_mc_list __rcu **ip;
1656 for (ip = &in_dev->mc_list;
1657 (i = rtnl_dereference(*ip)) != NULL;
1658 ip = &i->next_rcu) {
1659 if (i->multiaddr == addr) {
1660 if (--i->users == 0) {
1661 ip_mc_hash_remove(in_dev, i);
1664 igmp_group_dropped(i);
1668 ip_rt_multicast_event(in_dev);
1677 EXPORT_SYMBOL(ip_mc_dec_group);
1679 /* Device changing type */
1681 void ip_mc_unmap(struct in_device *in_dev)
1683 struct ip_mc_list *pmc;
1687 for_each_pmc_rtnl(in_dev, pmc)
1688 igmp_group_dropped(pmc);
1691 void ip_mc_remap(struct in_device *in_dev)
1693 struct ip_mc_list *pmc;
1697 for_each_pmc_rtnl(in_dev, pmc) {
1698 #ifdef CONFIG_IP_MULTICAST
1699 igmpv3_del_delrec(in_dev, pmc);
1701 igmp_group_added(pmc);
1705 /* Device going down */
1707 void ip_mc_down(struct in_device *in_dev)
1709 struct ip_mc_list *pmc;
1713 for_each_pmc_rtnl(in_dev, pmc)
1714 igmp_group_dropped(pmc);
1716 #ifdef CONFIG_IP_MULTICAST
1717 in_dev->mr_ifc_count = 0;
1718 if (del_timer(&in_dev->mr_ifc_timer))
1719 __in_dev_put(in_dev);
1720 in_dev->mr_gq_running = 0;
1721 if (del_timer(&in_dev->mr_gq_timer))
1722 __in_dev_put(in_dev);
1725 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1728 void ip_mc_init_dev(struct in_device *in_dev)
1730 #ifdef CONFIG_IP_MULTICAST
1731 struct net *net = dev_net(in_dev->dev);
1735 #ifdef CONFIG_IP_MULTICAST
1736 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1737 (unsigned long)in_dev);
1738 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1739 (unsigned long)in_dev);
1740 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1743 spin_lock_init(&in_dev->mc_tomb_lock);
1746 /* Device going up */
1748 void ip_mc_up(struct in_device *in_dev)
1750 struct ip_mc_list *pmc;
1751 #ifdef CONFIG_IP_MULTICAST
1752 struct net *net = dev_net(in_dev->dev);
1757 #ifdef CONFIG_IP_MULTICAST
1758 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1760 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1762 for_each_pmc_rtnl(in_dev, pmc) {
1763 #ifdef CONFIG_IP_MULTICAST
1764 igmpv3_del_delrec(in_dev, pmc);
1766 igmp_group_added(pmc);
1771 * Device is about to be destroyed: clean up.
1774 void ip_mc_destroy_dev(struct in_device *in_dev)
1776 struct ip_mc_list *i;
1780 /* Deactivate timers */
1782 #ifdef CONFIG_IP_MULTICAST
1783 igmpv3_clear_delrec(in_dev);
1786 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1787 in_dev->mc_list = i->next_rcu;
1793 /* RTNL is locked */
1794 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1796 struct net_device *dev = NULL;
1797 struct in_device *idev = NULL;
1799 if (imr->imr_ifindex) {
1800 idev = inetdev_by_index(net, imr->imr_ifindex);
1803 if (imr->imr_address.s_addr) {
1804 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1810 struct rtable *rt = ip_route_output(net,
1811 imr->imr_multiaddr.s_addr,
1819 imr->imr_ifindex = dev->ifindex;
1820 idev = __in_dev_get_rtnl(dev);
1826 * Join a socket to a group
1829 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1832 struct ip_sf_list *psf, *psf_prev;
1836 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1837 if (psf->sf_inaddr == *psfsrc)
1841 if (!psf || psf->sf_count[sfmode] == 0) {
1842 /* source filter not found, or count wrong => bug */
1845 psf->sf_count[sfmode]--;
1846 if (psf->sf_count[sfmode] == 0) {
1847 ip_rt_multicast_event(pmc->interface);
1849 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1850 #ifdef CONFIG_IP_MULTICAST
1851 struct in_device *in_dev = pmc->interface;
1852 struct net *net = dev_net(in_dev->dev);
1855 /* no more filters for this source */
1857 psf_prev->sf_next = psf->sf_next;
1859 pmc->sources = psf->sf_next;
1860 #ifdef CONFIG_IP_MULTICAST
1861 if (psf->sf_oldin &&
1862 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1863 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1864 psf->sf_next = pmc->tomb;
1874 #ifndef CONFIG_IP_MULTICAST
1875 #define igmp_ifc_event(x) do { } while (0)
1878 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1879 int sfcount, __be32 *psfsrc, int delta)
1881 struct ip_mc_list *pmc;
1888 for_each_pmc_rcu(in_dev, pmc) {
1889 if (*pmca == pmc->multiaddr)
1893 /* MCA not found?? bug */
1897 spin_lock_bh(&pmc->lock);
1899 #ifdef CONFIG_IP_MULTICAST
1904 if (!pmc->sfcount[sfmode])
1906 pmc->sfcount[sfmode]--;
1909 for (i = 0; i < sfcount; i++) {
1910 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1912 changerec |= rv > 0;
1916 if (pmc->sfmode == MCAST_EXCLUDE &&
1917 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1918 pmc->sfcount[MCAST_INCLUDE]) {
1919 #ifdef CONFIG_IP_MULTICAST
1920 struct ip_sf_list *psf;
1921 struct net *net = dev_net(in_dev->dev);
1924 /* filter mode change */
1925 pmc->sfmode = MCAST_INCLUDE;
1926 #ifdef CONFIG_IP_MULTICAST
1927 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1928 in_dev->mr_ifc_count = pmc->crcount;
1929 for (psf = pmc->sources; psf; psf = psf->sf_next)
1930 psf->sf_crcount = 0;
1931 igmp_ifc_event(pmc->interface);
1932 } else if (sf_setstate(pmc) || changerec) {
1933 igmp_ifc_event(pmc->interface);
1937 spin_unlock_bh(&pmc->lock);
1942 * Add multicast single-source filter to the interface list
1944 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1947 struct ip_sf_list *psf, *psf_prev;
1950 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1951 if (psf->sf_inaddr == *psfsrc)
1956 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1959 psf->sf_inaddr = *psfsrc;
1961 psf_prev->sf_next = psf;
1965 psf->sf_count[sfmode]++;
1966 if (psf->sf_count[sfmode] == 1) {
1967 ip_rt_multicast_event(pmc->interface);
1972 #ifdef CONFIG_IP_MULTICAST
1973 static void sf_markstate(struct ip_mc_list *pmc)
1975 struct ip_sf_list *psf;
1976 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1978 for (psf = pmc->sources; psf; psf = psf->sf_next)
1979 if (pmc->sfcount[MCAST_EXCLUDE]) {
1980 psf->sf_oldin = mca_xcount ==
1981 psf->sf_count[MCAST_EXCLUDE] &&
1982 !psf->sf_count[MCAST_INCLUDE];
1984 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1987 static int sf_setstate(struct ip_mc_list *pmc)
1989 struct ip_sf_list *psf, *dpsf;
1990 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1991 int qrv = pmc->interface->mr_qrv;
1995 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1996 if (pmc->sfcount[MCAST_EXCLUDE]) {
1997 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1998 !psf->sf_count[MCAST_INCLUDE];
2000 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2002 if (!psf->sf_oldin) {
2003 struct ip_sf_list *prev = NULL;
2005 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2006 if (dpsf->sf_inaddr == psf->sf_inaddr)
2012 prev->sf_next = dpsf->sf_next;
2014 pmc->tomb = dpsf->sf_next;
2017 psf->sf_crcount = qrv;
2020 } else if (psf->sf_oldin) {
2022 psf->sf_crcount = 0;
2024 * add or update "delete" records if an active filter
2027 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2028 if (dpsf->sf_inaddr == psf->sf_inaddr)
2031 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2035 /* pmc->lock held by callers */
2036 dpsf->sf_next = pmc->tomb;
2039 dpsf->sf_crcount = qrv;
2048 * Add multicast source filter list to the interface list
2050 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2051 int sfcount, __be32 *psfsrc, int delta)
2053 struct ip_mc_list *pmc;
2060 for_each_pmc_rcu(in_dev, pmc) {
2061 if (*pmca == pmc->multiaddr)
2065 /* MCA not found?? bug */
2069 spin_lock_bh(&pmc->lock);
2072 #ifdef CONFIG_IP_MULTICAST
2075 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2077 pmc->sfcount[sfmode]++;
2079 for (i = 0; i < sfcount; i++) {
2080 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2088 pmc->sfcount[sfmode]--;
2089 for (j = 0; j < i; j++)
2090 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2091 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2092 #ifdef CONFIG_IP_MULTICAST
2093 struct ip_sf_list *psf;
2094 struct net *net = dev_net(pmc->interface->dev);
2095 in_dev = pmc->interface;
2098 /* filter mode change */
2099 if (pmc->sfcount[MCAST_EXCLUDE])
2100 pmc->sfmode = MCAST_EXCLUDE;
2101 else if (pmc->sfcount[MCAST_INCLUDE])
2102 pmc->sfmode = MCAST_INCLUDE;
2103 #ifdef CONFIG_IP_MULTICAST
2104 /* else no filters; keep old mode for reports */
2106 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2107 in_dev->mr_ifc_count = pmc->crcount;
2108 for (psf = pmc->sources; psf; psf = psf->sf_next)
2109 psf->sf_crcount = 0;
2110 igmp_ifc_event(in_dev);
2111 } else if (sf_setstate(pmc)) {
2112 igmp_ifc_event(in_dev);
2115 spin_unlock_bh(&pmc->lock);
2119 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2121 struct ip_sf_list *tomb, *sources;
2123 spin_lock_bh(&pmc->lock);
2126 sources = pmc->sources;
2127 pmc->sources = NULL;
2128 pmc->sfmode = MCAST_EXCLUDE;
2129 pmc->sfcount[MCAST_INCLUDE] = 0;
2130 pmc->sfcount[MCAST_EXCLUDE] = 1;
2131 spin_unlock_bh(&pmc->lock);
2133 ip_sf_list_clear_all(tomb);
2134 ip_sf_list_clear_all(sources);
2137 /* Join a multicast group
2140 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2142 __be32 addr = imr->imr_multiaddr.s_addr;
2143 struct ip_mc_socklist *iml, *i;
2144 struct in_device *in_dev;
2145 struct inet_sock *inet = inet_sk(sk);
2146 struct net *net = sock_net(sk);
2153 if (!ipv4_is_multicast(addr))
2156 in_dev = ip_mc_find_dev(net, imr);
2164 ifindex = imr->imr_ifindex;
2165 for_each_pmc_rtnl(inet, i) {
2166 if (i->multi.imr_multiaddr.s_addr == addr &&
2167 i->multi.imr_ifindex == ifindex)
2172 if (count >= net->ipv4.sysctl_igmp_max_memberships)
2174 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2178 memcpy(&iml->multi, imr, sizeof(*imr));
2179 iml->next_rcu = inet->mc_list;
2181 iml->sfmode = MCAST_EXCLUDE;
2182 rcu_assign_pointer(inet->mc_list, iml);
2183 ip_mc_inc_group(in_dev, addr);
2188 EXPORT_SYMBOL(ip_mc_join_group);
2190 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2191 struct in_device *in_dev)
2193 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2197 /* any-source empty exclude case */
2198 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2199 iml->sfmode, 0, NULL, 0);
2201 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2202 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2203 RCU_INIT_POINTER(iml->sflist, NULL);
2204 /* decrease mem now to avoid the memleak warning */
2205 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2206 kfree_rcu(psf, rcu);
2210 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2212 struct inet_sock *inet = inet_sk(sk);
2213 struct ip_mc_socklist *iml;
2214 struct ip_mc_socklist __rcu **imlp;
2215 struct in_device *in_dev;
2216 struct net *net = sock_net(sk);
2217 __be32 group = imr->imr_multiaddr.s_addr;
2219 int ret = -EADDRNOTAVAIL;
2223 in_dev = ip_mc_find_dev(net, imr);
2224 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2228 ifindex = imr->imr_ifindex;
2229 for (imlp = &inet->mc_list;
2230 (iml = rtnl_dereference(*imlp)) != NULL;
2231 imlp = &iml->next_rcu) {
2232 if (iml->multi.imr_multiaddr.s_addr != group)
2235 if (iml->multi.imr_ifindex != ifindex)
2237 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2238 iml->multi.imr_address.s_addr)
2241 (void) ip_mc_leave_src(sk, iml, in_dev);
2243 *imlp = iml->next_rcu;
2246 ip_mc_dec_group(in_dev, group);
2248 /* decrease mem now to avoid the memleak warning */
2249 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2250 kfree_rcu(iml, rcu);
2256 EXPORT_SYMBOL(ip_mc_leave_group);
2258 int ip_mc_source(int add, int omode, struct sock *sk, struct
2259 ip_mreq_source *mreqs, int ifindex)
2262 struct ip_mreqn imr;
2263 __be32 addr = mreqs->imr_multiaddr;
2264 struct ip_mc_socklist *pmc;
2265 struct in_device *in_dev = NULL;
2266 struct inet_sock *inet = inet_sk(sk);
2267 struct ip_sf_socklist *psl;
2268 struct net *net = sock_net(sk);
2272 if (!ipv4_is_multicast(addr))
2277 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2278 imr.imr_address.s_addr = mreqs->imr_interface;
2279 imr.imr_ifindex = ifindex;
2280 in_dev = ip_mc_find_dev(net, &imr);
2286 err = -EADDRNOTAVAIL;
2288 for_each_pmc_rtnl(inet, pmc) {
2289 if ((pmc->multi.imr_multiaddr.s_addr ==
2290 imr.imr_multiaddr.s_addr) &&
2291 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2294 if (!pmc) { /* must have a prior join */
2298 /* if a source filter was set, must be the same mode as before */
2300 if (pmc->sfmode != omode) {
2304 } else if (pmc->sfmode != omode) {
2305 /* allow mode switches for empty-set filters */
2306 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2307 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2309 pmc->sfmode = omode;
2312 psl = rtnl_dereference(pmc->sflist);
2315 goto done; /* err = -EADDRNOTAVAIL */
2317 for (i = 0; i < psl->sl_count; i++) {
2318 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2323 if (rv) /* source not found */
2324 goto done; /* err = -EADDRNOTAVAIL */
2326 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2327 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2332 /* update the interface filter */
2333 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2334 &mreqs->imr_sourceaddr, 1);
2336 for (j = i+1; j < psl->sl_count; j++)
2337 psl->sl_addr[j-1] = psl->sl_addr[j];
2342 /* else, add a new source to the filter */
2344 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2348 if (!psl || psl->sl_count == psl->sl_max) {
2349 struct ip_sf_socklist *newpsl;
2350 int count = IP_SFBLOCK;
2353 count += psl->sl_max;
2354 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2359 newpsl->sl_max = count;
2360 newpsl->sl_count = count - IP_SFBLOCK;
2362 for (i = 0; i < psl->sl_count; i++)
2363 newpsl->sl_addr[i] = psl->sl_addr[i];
2364 /* decrease mem now to avoid the memleak warning */
2365 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2366 kfree_rcu(psl, rcu);
2368 rcu_assign_pointer(pmc->sflist, newpsl);
2371 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2372 for (i = 0; i < psl->sl_count; i++) {
2373 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2378 if (rv == 0) /* address already there is an error */
2380 for (j = psl->sl_count-1; j >= i; j--)
2381 psl->sl_addr[j+1] = psl->sl_addr[j];
2382 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2385 /* update the interface list */
2386 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2387 &mreqs->imr_sourceaddr, 1);
2390 err = ip_mc_leave_group(sk, &imr);
2394 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2397 struct ip_mreqn imr;
2398 __be32 addr = msf->imsf_multiaddr;
2399 struct ip_mc_socklist *pmc;
2400 struct in_device *in_dev;
2401 struct inet_sock *inet = inet_sk(sk);
2402 struct ip_sf_socklist *newpsl, *psl;
2403 struct net *net = sock_net(sk);
2406 if (!ipv4_is_multicast(addr))
2408 if (msf->imsf_fmode != MCAST_INCLUDE &&
2409 msf->imsf_fmode != MCAST_EXCLUDE)
2414 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2415 imr.imr_address.s_addr = msf->imsf_interface;
2416 imr.imr_ifindex = ifindex;
2417 in_dev = ip_mc_find_dev(net, &imr);
2424 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2425 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2430 for_each_pmc_rtnl(inet, pmc) {
2431 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2432 pmc->multi.imr_ifindex == imr.imr_ifindex)
2435 if (!pmc) { /* must have a prior join */
2439 if (msf->imsf_numsrc) {
2440 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2446 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2447 memcpy(newpsl->sl_addr, msf->imsf_slist,
2448 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2449 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2450 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2452 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2457 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2458 msf->imsf_fmode, 0, NULL, 0);
2460 psl = rtnl_dereference(pmc->sflist);
2462 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2463 psl->sl_count, psl->sl_addr, 0);
2464 /* decrease mem now to avoid the memleak warning */
2465 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2466 kfree_rcu(psl, rcu);
2468 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2470 rcu_assign_pointer(pmc->sflist, newpsl);
2471 pmc->sfmode = msf->imsf_fmode;
2475 err = ip_mc_leave_group(sk, &imr);
2479 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2480 struct ip_msfilter __user *optval, int __user *optlen)
2482 int err, len, count, copycount;
2483 struct ip_mreqn imr;
2484 __be32 addr = msf->imsf_multiaddr;
2485 struct ip_mc_socklist *pmc;
2486 struct in_device *in_dev;
2487 struct inet_sock *inet = inet_sk(sk);
2488 struct ip_sf_socklist *psl;
2489 struct net *net = sock_net(sk);
2493 if (!ipv4_is_multicast(addr))
2496 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2497 imr.imr_address.s_addr = msf->imsf_interface;
2498 imr.imr_ifindex = 0;
2499 in_dev = ip_mc_find_dev(net, &imr);
2505 err = -EADDRNOTAVAIL;
2507 for_each_pmc_rtnl(inet, pmc) {
2508 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2509 pmc->multi.imr_ifindex == imr.imr_ifindex)
2512 if (!pmc) /* must have a prior join */
2514 msf->imsf_fmode = pmc->sfmode;
2515 psl = rtnl_dereference(pmc->sflist);
2520 count = psl->sl_count;
2522 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2523 len = copycount * sizeof(psl->sl_addr[0]);
2524 msf->imsf_numsrc = count;
2525 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2526 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2530 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2537 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2538 struct group_filter __user *optval, int __user *optlen)
2540 int err, i, count, copycount;
2541 struct sockaddr_in *psin;
2543 struct ip_mc_socklist *pmc;
2544 struct inet_sock *inet = inet_sk(sk);
2545 struct ip_sf_socklist *psl;
2549 psin = (struct sockaddr_in *)&gsf->gf_group;
2550 if (psin->sin_family != AF_INET)
2552 addr = psin->sin_addr.s_addr;
2553 if (!ipv4_is_multicast(addr))
2556 err = -EADDRNOTAVAIL;
2558 for_each_pmc_rtnl(inet, pmc) {
2559 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2560 pmc->multi.imr_ifindex == gsf->gf_interface)
2563 if (!pmc) /* must have a prior join */
2565 gsf->gf_fmode = pmc->sfmode;
2566 psl = rtnl_dereference(pmc->sflist);
2567 count = psl ? psl->sl_count : 0;
2568 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2569 gsf->gf_numsrc = count;
2570 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2571 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2574 for (i = 0; i < copycount; i++) {
2575 struct sockaddr_storage ss;
2577 psin = (struct sockaddr_in *)&ss;
2578 memset(&ss, 0, sizeof(ss));
2579 psin->sin_family = AF_INET;
2580 psin->sin_addr.s_addr = psl->sl_addr[i];
2581 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2590 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2592 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2594 struct inet_sock *inet = inet_sk(sk);
2595 struct ip_mc_socklist *pmc;
2596 struct ip_sf_socklist *psl;
2601 if (!ipv4_is_multicast(loc_addr))
2605 for_each_pmc_rcu(inet, pmc) {
2606 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2607 pmc->multi.imr_ifindex == dif)
2613 psl = rcu_dereference(pmc->sflist);
2614 ret = (pmc->sfmode == MCAST_EXCLUDE);
2618 for (i = 0; i < psl->sl_count; i++) {
2619 if (psl->sl_addr[i] == rmt_addr)
2623 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2625 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2635 * A socket is closing.
2638 void ip_mc_drop_socket(struct sock *sk)
2640 struct inet_sock *inet = inet_sk(sk);
2641 struct ip_mc_socklist *iml;
2642 struct net *net = sock_net(sk);
2648 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2649 struct in_device *in_dev;
2651 inet->mc_list = iml->next_rcu;
2652 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2653 (void) ip_mc_leave_src(sk, iml, in_dev);
2655 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2656 /* decrease mem now to avoid the memleak warning */
2657 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2658 kfree_rcu(iml, rcu);
2663 /* called with rcu_read_lock() */
2664 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2666 struct ip_mc_list *im;
2667 struct ip_mc_list __rcu **mc_hash;
2668 struct ip_sf_list *psf;
2671 mc_hash = rcu_dereference(in_dev->mc_hash);
2673 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2675 for (im = rcu_dereference(mc_hash[hash]);
2677 im = rcu_dereference(im->next_hash)) {
2678 if (im->multiaddr == mc_addr)
2682 for_each_pmc_rcu(in_dev, im) {
2683 if (im->multiaddr == mc_addr)
2687 if (im && proto == IPPROTO_IGMP) {
2691 for (psf = im->sources; psf; psf = psf->sf_next) {
2692 if (psf->sf_inaddr == src_addr)
2696 rv = psf->sf_count[MCAST_INCLUDE] ||
2697 psf->sf_count[MCAST_EXCLUDE] !=
2698 im->sfcount[MCAST_EXCLUDE];
2700 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2702 rv = 1; /* unspecified source; tentatively allow */
2707 #if defined(CONFIG_PROC_FS)
2708 struct igmp_mc_iter_state {
2709 struct seq_net_private p;
2710 struct net_device *dev;
2711 struct in_device *in_dev;
2714 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2716 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2718 struct net *net = seq_file_net(seq);
2719 struct ip_mc_list *im = NULL;
2720 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2722 state->in_dev = NULL;
2723 for_each_netdev_rcu(net, state->dev) {
2724 struct in_device *in_dev;
2726 in_dev = __in_dev_get_rcu(state->dev);
2729 im = rcu_dereference(in_dev->mc_list);
2731 state->in_dev = in_dev;
2738 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2740 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2742 im = rcu_dereference(im->next_rcu);
2744 state->dev = next_net_device_rcu(state->dev);
2746 state->in_dev = NULL;
2749 state->in_dev = __in_dev_get_rcu(state->dev);
2752 im = rcu_dereference(state->in_dev->mc_list);
2757 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2759 struct ip_mc_list *im = igmp_mc_get_first(seq);
2761 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2763 return pos ? NULL : im;
2766 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2770 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2773 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2775 struct ip_mc_list *im;
2776 if (v == SEQ_START_TOKEN)
2777 im = igmp_mc_get_first(seq);
2779 im = igmp_mc_get_next(seq, v);
2784 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2787 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2789 state->in_dev = NULL;
2794 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2796 if (v == SEQ_START_TOKEN)
2798 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2800 struct ip_mc_list *im = (struct ip_mc_list *)v;
2801 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2805 #ifdef CONFIG_IP_MULTICAST
2806 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2807 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2813 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2814 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2815 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2818 delta = im->timer.expires - jiffies;
2820 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2821 im->multiaddr, im->users,
2823 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2829 static const struct seq_operations igmp_mc_seq_ops = {
2830 .start = igmp_mc_seq_start,
2831 .next = igmp_mc_seq_next,
2832 .stop = igmp_mc_seq_stop,
2833 .show = igmp_mc_seq_show,
2836 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2838 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2839 sizeof(struct igmp_mc_iter_state));
2842 static const struct file_operations igmp_mc_seq_fops = {
2843 .owner = THIS_MODULE,
2844 .open = igmp_mc_seq_open,
2846 .llseek = seq_lseek,
2847 .release = seq_release_net,
2850 struct igmp_mcf_iter_state {
2851 struct seq_net_private p;
2852 struct net_device *dev;
2853 struct in_device *idev;
2854 struct ip_mc_list *im;
2857 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2859 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2861 struct net *net = seq_file_net(seq);
2862 struct ip_sf_list *psf = NULL;
2863 struct ip_mc_list *im = NULL;
2864 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2868 for_each_netdev_rcu(net, state->dev) {
2869 struct in_device *idev;
2870 idev = __in_dev_get_rcu(state->dev);
2871 if (unlikely(!idev))
2873 im = rcu_dereference(idev->mc_list);
2875 spin_lock_bh(&im->lock);
2882 spin_unlock_bh(&im->lock);
2888 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2890 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2894 spin_unlock_bh(&state->im->lock);
2895 state->im = state->im->next;
2896 while (!state->im) {
2897 state->dev = next_net_device_rcu(state->dev);
2902 state->idev = __in_dev_get_rcu(state->dev);
2905 state->im = rcu_dereference(state->idev->mc_list);
2909 spin_lock_bh(&state->im->lock);
2910 psf = state->im->sources;
2916 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2918 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2920 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2922 return pos ? NULL : psf;
2925 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2929 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2932 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2934 struct ip_sf_list *psf;
2935 if (v == SEQ_START_TOKEN)
2936 psf = igmp_mcf_get_first(seq);
2938 psf = igmp_mcf_get_next(seq, v);
2943 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2946 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2947 if (likely(state->im)) {
2948 spin_unlock_bh(&state->im->lock);
2956 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2958 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2959 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2961 if (v == SEQ_START_TOKEN) {
2962 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2966 "0x%08x %6lu %6lu\n",
2967 state->dev->ifindex, state->dev->name,
2968 ntohl(state->im->multiaddr),
2969 ntohl(psf->sf_inaddr),
2970 psf->sf_count[MCAST_INCLUDE],
2971 psf->sf_count[MCAST_EXCLUDE]);
2976 static const struct seq_operations igmp_mcf_seq_ops = {
2977 .start = igmp_mcf_seq_start,
2978 .next = igmp_mcf_seq_next,
2979 .stop = igmp_mcf_seq_stop,
2980 .show = igmp_mcf_seq_show,
2983 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2985 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2986 sizeof(struct igmp_mcf_iter_state));
2989 static const struct file_operations igmp_mcf_seq_fops = {
2990 .owner = THIS_MODULE,
2991 .open = igmp_mcf_seq_open,
2993 .llseek = seq_lseek,
2994 .release = seq_release_net,
2997 static int __net_init igmp_net_init(struct net *net)
2999 struct proc_dir_entry *pde;
3002 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
3005 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
3006 &igmp_mcf_seq_fops);
3009 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3010 SOCK_DGRAM, 0, net);
3012 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3020 remove_proc_entry("mcfilter", net->proc_net);
3022 remove_proc_entry("igmp", net->proc_net);
3027 static void __net_exit igmp_net_exit(struct net *net)
3029 remove_proc_entry("mcfilter", net->proc_net);
3030 remove_proc_entry("igmp", net->proc_net);
3031 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3034 static struct pernet_operations igmp_net_ops = {
3035 .init = igmp_net_init,
3036 .exit = igmp_net_exit,
3040 static int igmp_netdev_event(struct notifier_block *this,
3041 unsigned long event, void *ptr)
3043 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3044 struct in_device *in_dev;
3047 case NETDEV_RESEND_IGMP:
3048 in_dev = __in_dev_get_rtnl(dev);
3050 ip_mc_rejoin_groups(in_dev);
3058 static struct notifier_block igmp_notifier = {
3059 .notifier_call = igmp_netdev_event,
3062 int __init igmp_mc_init(void)
3064 #if defined(CONFIG_PROC_FS)
3067 err = register_pernet_subsys(&igmp_net_ops);
3070 err = register_netdevice_notifier(&igmp_notifier);
3072 goto reg_notif_fail;
3076 unregister_pernet_subsys(&igmp_net_ops);
3079 return register_netdevice_notifier(&igmp_notifier);