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.
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
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 <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>
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 (refcount_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 #ifdef CONFIG_IP_MULTICAST
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);
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;
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))
229 in_dev->mr_gq_running = 1;
230 if (!mod_timer(&in_dev->mr_gq_timer, exp))
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))
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);
250 spin_unlock_bh(&im->lock);
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)
271 case IGMPV3_MODE_IS_INCLUDE:
272 case IGMPV3_MODE_IS_EXCLUDE:
273 if (gdeleted || sdeleted)
275 if (!(pmc->gsquery && !psf->sf_gsresp)) {
276 if (pmc->sfmode == MCAST_INCLUDE)
278 /* don't include if this source is excluded
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];
287 case IGMPV3_CHANGE_TO_INCLUDE:
288 if (gdeleted || sdeleted)
290 return psf->sf_count[MCAST_INCLUDE] != 0;
291 case IGMPV3_CHANGE_TO_EXCLUDE:
292 if (gdeleted || sdeleted)
294 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
295 psf->sf_count[MCAST_INCLUDE])
297 return pmc->sfcount[MCAST_EXCLUDE] ==
298 psf->sf_count[MCAST_EXCLUDE];
299 case IGMPV3_ALLOW_NEW_SOURCES:
300 if (gdeleted || !psf->sf_crcount)
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;
312 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
314 struct ip_sf_list *psf;
317 for (psf = pmc->sources; psf; psf = psf->sf_next) {
318 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
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);
332 return htonl(INADDR_ANY);
335 if (fl4->saddr == ifa->ifa_local)
337 } endfor_ifa(in_dev);
339 return htonl(INADDR_ANY);
342 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
347 struct igmpv3_report *pig;
348 struct net *net = dev_net(dev);
350 int hlen = LL_RESERVED_SPACE(dev);
351 int tlen = dev->needed_tailroom;
352 unsigned int size = mtu;
355 skb = alloc_skb(size + hlen + tlen,
356 GFP_ATOMIC | __GFP_NOWARN);
363 skb->priority = TC_PRIO_CONTROL;
365 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
367 IPPROTO_IGMP, 0, dev->ifindex);
373 skb_dst_set(skb, &rt->dst);
376 skb_reserve(skb, hlen);
377 skb_tailroom_reserve(skb, mtu, tlen);
379 skb_reset_network_header(skb);
381 skb_put(skb, sizeof(struct iphdr) + 4);
384 pip->ihl = (sizeof(struct iphdr)+4)>>2;
386 pip->frag_off = htons(IP_DF);
388 pip->daddr = fl4.daddr;
391 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
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;
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;
436 skb = igmpv3_newpack(dev, mtu);
440 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
441 pgr->grec_type = type;
442 pgr->grec_auxwords = 0;
444 pgr->grec_mca = pmc->multiaddr;
445 pih = igmpv3_report_hdr(skb);
446 pih->ngrec = htons(ntohs(pih->ngrec)+1);
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;
464 if (pmc->multiaddr == IGMP_ALL_HOSTS)
466 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
469 mtu = READ_ONCE(dev->mtu);
470 if (mtu < IPV4_MIN_MTU)
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;
480 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
485 pih = skb ? igmpv3_report_hdr(skb) : NULL;
487 /* EX and TO_EX get a fresh packet, if needed */
489 if (pih && pih->ngrec &&
490 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
492 igmpv3_sendpack(skb);
493 skb = igmpv3_newpack(dev, mtu);
498 for (psf = *psf_list; psf; psf = psf_next) {
501 psf_next = psf->sf_next;
503 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
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 */
521 if (AVAILABLE(skb) < sizeof(__be32) +
522 first*sizeof(struct igmpv3_grec)) {
523 if (truncate && !first)
524 break; /* truncate these */
526 pgr->grec_nsrcs = htons(scount);
528 igmpv3_sendpack(skb);
529 skb = igmpv3_newpack(dev, mtu);
534 skb = add_grhead(skb, pmc, type, &pgr, mtu);
539 psrc = skb_put(skb, sizeof(__be32));
540 *psrc = psf->sf_inaddr;
542 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
543 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
546 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
548 psf_prev->sf_next = psf->sf_next;
550 *psf_list = psf->sf_next;
560 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
561 type == IGMPV3_BLOCK_OLD_SOURCES)
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);
573 pgr->grec_nsrcs = htons(scount);
576 pmc->gsquery = 0; /* clear query state on report */
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);
588 for_each_pmc_rcu(in_dev, pmc) {
589 if (pmc->multiaddr == IGMP_ALL_HOSTS)
591 if (ipv4_is_local_multicast(pmc->multiaddr) &&
592 !net->ipv4.sysctl_igmp_llm_reports)
594 spin_lock_bh(&pmc->lock);
595 if (pmc->sfcount[MCAST_EXCLUDE])
596 type = IGMPV3_MODE_IS_EXCLUDE;
598 type = IGMPV3_MODE_IS_INCLUDE;
599 skb = add_grec(skb, pmc, type, 0, 0);
600 spin_unlock_bh(&pmc->lock);
604 spin_lock_bh(&pmc->lock);
605 if (pmc->sfcount[MCAST_EXCLUDE])
606 type = IGMPV3_MODE_IS_EXCLUDE;
608 type = IGMPV3_MODE_IS_INCLUDE;
609 skb = add_grec(skb, pmc, type, 0, 0);
610 spin_unlock_bh(&pmc->lock);
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;
625 for (psf = *ppsf; psf; psf = psf_next) {
626 psf_next = psf->sf_next;
627 if (psf->sf_crcount == 0) {
629 psf_prev->sf_next = psf->sf_next;
631 *ppsf = psf->sf_next;
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;
645 spin_lock_bh(&in_dev->mc_tomb_lock);
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);
658 if (pmc->sfmode == MCAST_EXCLUDE) {
659 type = IGMPV3_CHANGE_TO_INCLUDE;
660 skb = add_grec(skb, pmc, type, 1, 0);
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) {
670 pmc_prev->next = pmc_next;
672 in_dev->mc_tomb = pmc_next;
673 in_dev_put(pmc->interface);
678 spin_unlock_bh(&in_dev->mc_tomb_lock);
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;
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 */
695 if (pmc->sfmode == MCAST_EXCLUDE)
696 type = IGMPV3_CHANGE_TO_EXCLUDE;
698 type = IGMPV3_CHANGE_TO_INCLUDE;
699 skb = add_grec(skb, pmc, type, 0, 0);
702 spin_unlock_bh(&pmc->lock);
708 (void) igmpv3_sendpack(skb);
711 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
718 struct net_device *dev = in_dev->dev;
719 struct net *net = dev_net(dev);
720 __be32 group = pmc ? pmc->multiaddr : 0;
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)
731 if (type == IGMP_HOST_LEAVE_MESSAGE)
732 dst = IGMP_ALL_ROUTER;
736 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
738 IPPROTO_IGMP, 0, dev->ifindex);
742 hlen = LL_RESERVED_SPACE(dev);
743 tlen = dev->needed_tailroom;
744 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
749 skb->priority = TC_PRIO_CONTROL;
751 skb_dst_set(skb, &rt->dst);
753 skb_reserve(skb, hlen);
755 skb_reset_network_header(skb);
757 skb_put(skb, sizeof(struct iphdr) + 4);
760 iph->ihl = (sizeof(struct iphdr)+4)>>2;
762 iph->frag_off = htons(IP_DF);
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));
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);
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));
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))
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);
823 if (im->unsolicit_count) {
824 im->unsolicit_count--;
825 igmp_start_timer(im, unsolicited_report_interval(in_dev));
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);
835 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
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;
847 for (psf = pmc->sources; psf; psf = psf->sf_next) {
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])
856 if (srcs[i] == psf->sf_inaddr) {
863 if (scount == nsrcs) /* all sources excluded */
868 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
870 struct ip_sf_list *psf;
873 if (pmc->sfmode == MCAST_EXCLUDE)
874 return igmp_xmarksources(pmc, nsrcs, srcs);
876 /* mark INCLUDE-mode sources */
878 for (psf = pmc->sources; psf; psf = psf->sf_next) {
881 for (i = 0; i < nsrcs; i++)
882 if (srcs[i] == psf->sf_inaddr) {
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)
906 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
910 for_each_pmc_rcu(in_dev, im) {
911 if (im->multiaddr == group) {
920 /* return true if packet was dropped */
921 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
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;
930 struct net *net = dev_net(in_dev->dev);
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;
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;
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);
968 max_delay = 1; /* can't mod w/ 0 */
970 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
973 ih3 = igmpv3_query_hdr(skb);
975 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
976 + ntohs(ih3->nsrcs)*sizeof(__be32)))
978 ih3 = igmpv3_query_hdr(skb);
981 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
983 max_delay = 1; /* can't mod w/ 0 */
984 in_dev->mr_maxdelay = max_delay;
986 in_dev->mr_qrv = ih3->qrv;
987 if (!group) { /* general query */
989 return true; /* no sources allowed */
990 igmp_gq_start_timer(in_dev);
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
1004 * - Use the igmp->igmp_code field as the maximum
1008 for_each_pmc_rcu(in_dev, im) {
1011 if (group && group != im->multiaddr)
1013 if (im->multiaddr == IGMP_ALL_HOSTS)
1015 if (ipv4_is_local_multicast(im->multiaddr) &&
1016 !net->ipv4.sysctl_igmp_llm_reports)
1018 spin_lock_bh(&im->lock);
1020 im->gsquery = im->gsquery && mark;
1023 changed = !im->gsquery ||
1024 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1025 spin_unlock_bh(&im->lock);
1027 igmp_mod_timer(im, max_delay);
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 */
1038 struct net_device *dev = skb->dev;
1039 struct in_device *in_dev;
1041 bool dropped = true;
1043 if (netif_is_l3_master(dev)) {
1044 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1049 in_dev = __in_dev_get_rcu(dev);
1053 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1056 if (skb_checksum_simple_validate(skb))
1061 case IGMP_HOST_MEMBERSHIP_QUERY:
1062 dropped = igmp_heard_query(in_dev, skb, len);
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)))
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);
1075 #ifdef CONFIG_IP_PIMSM_V1
1076 return pim_rcv_v1(skb);
1078 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1081 case IGMP_HOST_LEAVE_MESSAGE:
1083 case IGMP_MTRACE_RESP:
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; }
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);
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);
1187 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1188 if (pmc->multiaddr == multiaddr)
1194 pmc_prev->next = pmc->next;
1196 in_dev->mc_tomb = pmc->next;
1198 spin_unlock_bh(&in_dev->mc_tomb_lock);
1200 spin_lock_bh(&im->lock);
1202 im->interface = pmc->interface;
1203 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1204 im->sfmode = pmc->sfmode;
1205 if (pmc->sfmode == MCAST_INCLUDE) {
1206 im->tomb = pmc->tomb;
1207 im->sources = pmc->sources;
1208 for (psf = im->sources; psf; psf = psf->sf_next)
1209 psf->sf_crcount = im->crcount;
1211 in_dev_put(pmc->interface);
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);
1235 /* clear dead sources, too */
1237 for_each_pmc_rcu(in_dev, pmc) {
1238 struct ip_sf_list *psf, *psf_next;
1240 spin_lock_bh(&pmc->lock);
1243 spin_unlock_bh(&pmc->lock);
1244 for (; psf; psf = psf_next) {
1245 psf_next = psf->sf_next;
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);
1263 ip_mc_filter_del(in_dev, im->multiaddr);
1266 #ifdef CONFIG_IP_MULTICAST
1267 if (im->multiaddr == IGMP_ALL_HOSTS)
1269 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1272 reporter = im->reporter;
1273 igmp_stop_timer(im);
1275 if (!in_dev->dead) {
1276 if (IGMP_V1_SEEN(in_dev))
1278 if (IGMP_V2_SEEN(in_dev)) {
1280 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1284 igmpv3_add_delrec(in_dev, im);
1286 igmp_ifc_event(in_dev);
1291 static void igmp_group_added(struct ip_mc_list *im)
1293 struct in_device *in_dev = im->interface;
1294 #ifdef CONFIG_IP_MULTICAST
1295 struct net *net = dev_net(in_dev->dev);
1298 if (im->loaded == 0) {
1300 ip_mc_filter_add(in_dev, im->multiaddr);
1303 #ifdef CONFIG_IP_MULTICAST
1304 if (im->multiaddr == IGMP_ALL_HOSTS)
1306 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
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);
1319 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1320 igmp_ifc_event(in_dev);
1326 * Multicast list managers
1329 static u32 ip_mc_hash(const struct ip_mc_list *im)
1331 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1334 static void ip_mc_hash_add(struct in_device *in_dev,
1335 struct ip_mc_list *im)
1337 struct ip_mc_list __rcu **mc_hash;
1340 mc_hash = rtnl_dereference(in_dev->mc_hash);
1342 hash = ip_mc_hash(im);
1343 im->next_hash = mc_hash[hash];
1344 rcu_assign_pointer(mc_hash[hash], im);
1348 /* do not use a hash table for small number of items */
1349 if (in_dev->mc_count < 4)
1352 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1357 for_each_pmc_rtnl(in_dev, im) {
1358 hash = ip_mc_hash(im);
1359 im->next_hash = mc_hash[hash];
1360 RCU_INIT_POINTER(mc_hash[hash], im);
1363 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1366 static void ip_mc_hash_remove(struct in_device *in_dev,
1367 struct ip_mc_list *im)
1369 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1370 struct ip_mc_list *aux;
1374 mc_hash += ip_mc_hash(im);
1375 while ((aux = rtnl_dereference(*mc_hash)) != im)
1376 mc_hash = &aux->next_hash;
1377 *mc_hash = im->next_hash;
1382 * A socket has joined a multicast group on device dev.
1385 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1387 struct ip_mc_list *im;
1388 #ifdef CONFIG_IP_MULTICAST
1389 struct net *net = dev_net(in_dev->dev);
1394 for_each_pmc_rtnl(in_dev, im) {
1395 if (im->multiaddr == addr) {
1397 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1402 im = kzalloc(sizeof(*im), GFP_KERNEL);
1407 im->interface = in_dev;
1408 in_dev_hold(in_dev);
1409 im->multiaddr = addr;
1410 /* initial mode is (EX, empty) */
1411 im->sfmode = MCAST_EXCLUDE;
1412 im->sfcount[MCAST_EXCLUDE] = 1;
1413 refcount_set(&im->refcnt, 1);
1414 spin_lock_init(&im->lock);
1415 #ifdef CONFIG_IP_MULTICAST
1416 timer_setup(&im->timer, igmp_timer_expire, 0);
1417 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1420 im->next_rcu = in_dev->mc_list;
1422 rcu_assign_pointer(in_dev->mc_list, im);
1424 ip_mc_hash_add(in_dev, im);
1426 #ifdef CONFIG_IP_MULTICAST
1427 igmpv3_del_delrec(in_dev, im);
1429 igmp_group_added(im);
1431 ip_rt_multicast_event(in_dev);
1435 EXPORT_SYMBOL(ip_mc_inc_group);
1437 static int ip_mc_check_iphdr(struct sk_buff *skb)
1439 const struct iphdr *iph;
1441 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1443 if (!pskb_may_pull(skb, offset))
1448 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1451 offset += ip_hdrlen(skb) - sizeof(*iph);
1453 if (!pskb_may_pull(skb, offset))
1458 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1461 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1462 if (skb->len < len || len < offset)
1465 skb_set_transport_header(skb, offset);
1470 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1472 unsigned int len = skb_transport_offset(skb);
1474 len += sizeof(struct igmpv3_report);
1476 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1479 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1481 unsigned int len = skb_transport_offset(skb);
1483 len += sizeof(struct igmphdr);
1488 if (skb->len != len) {
1490 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1491 if (skb->len < len || !pskb_may_pull(skb, len))
1495 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1496 * all-systems destination addresses (224.0.0.1) for general queries
1498 if (!igmp_hdr(skb)->group &&
1499 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1505 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1507 switch (igmp_hdr(skb)->type) {
1508 case IGMP_HOST_LEAVE_MESSAGE:
1509 case IGMP_HOST_MEMBERSHIP_REPORT:
1510 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1513 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1514 return ip_mc_check_igmp_reportv3(skb);
1515 case IGMP_HOST_MEMBERSHIP_QUERY:
1516 return ip_mc_check_igmp_query(skb);
1522 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1524 return skb_checksum_simple_validate(skb);
1527 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1530 struct sk_buff *skb_chk;
1531 unsigned int transport_len;
1532 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1535 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1537 skb_chk = skb_checksum_trimmed(skb, transport_len,
1538 ip_mc_validate_checksum);
1542 if (!pskb_may_pull(skb_chk, len))
1545 ret = ip_mc_check_igmp_msg(skb_chk);
1550 *skb_trimmed = skb_chk;
1551 /* free now unneeded clone */
1552 else if (skb_chk != skb)
1558 if (ret && skb_chk && skb_chk != skb)
1565 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1566 * @skb: the skb to validate
1567 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1569 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1570 * skb transport header accordingly and returns zero.
1572 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1574 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1575 * -ENOMEM: A memory allocation failure happened.
1577 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1578 * to NULL): After parsing an IGMP packet successfully it will point to
1579 * an skb which has its tail aligned to the IP packet end. This might
1580 * either be the originally provided skb or a trimmed, cloned version if
1581 * the skb frame had data beyond the IP packet. A cloned skb allows us
1582 * to leave the original skb and its full frame unchanged (which might be
1583 * desirable for layer 2 frame jugglers).
1585 * Caller needs to set the skb network header and free any returned skb if it
1586 * differs from the provided skb.
1588 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1590 int ret = ip_mc_check_iphdr(skb);
1595 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1598 return __ip_mc_check_igmp(skb, skb_trimmed);
1600 EXPORT_SYMBOL(ip_mc_check_igmp);
1603 * Resend IGMP JOIN report; used by netdev notifier.
1605 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1607 #ifdef CONFIG_IP_MULTICAST
1608 struct ip_mc_list *im;
1610 struct net *net = dev_net(in_dev->dev);
1614 for_each_pmc_rtnl(in_dev, im) {
1615 if (im->multiaddr == IGMP_ALL_HOSTS)
1617 if (ipv4_is_local_multicast(im->multiaddr) &&
1618 !net->ipv4.sysctl_igmp_llm_reports)
1621 /* a failover is happening and switches
1622 * must be notified immediately
1624 if (IGMP_V1_SEEN(in_dev))
1625 type = IGMP_HOST_MEMBERSHIP_REPORT;
1626 else if (IGMP_V2_SEEN(in_dev))
1627 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1629 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1630 igmp_send_report(in_dev, im, type);
1636 * A socket has left a multicast group on device dev
1639 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1641 struct ip_mc_list *i;
1642 struct ip_mc_list __rcu **ip;
1646 for (ip = &in_dev->mc_list;
1647 (i = rtnl_dereference(*ip)) != NULL;
1648 ip = &i->next_rcu) {
1649 if (i->multiaddr == addr) {
1650 if (--i->users == 0) {
1651 ip_mc_hash_remove(in_dev, i);
1654 igmp_group_dropped(i);
1658 ip_rt_multicast_event(in_dev);
1667 EXPORT_SYMBOL(ip_mc_dec_group);
1669 /* Device changing type */
1671 void ip_mc_unmap(struct in_device *in_dev)
1673 struct ip_mc_list *pmc;
1677 for_each_pmc_rtnl(in_dev, pmc)
1678 igmp_group_dropped(pmc);
1681 void ip_mc_remap(struct in_device *in_dev)
1683 struct ip_mc_list *pmc;
1687 for_each_pmc_rtnl(in_dev, pmc) {
1688 #ifdef CONFIG_IP_MULTICAST
1689 igmpv3_del_delrec(in_dev, pmc);
1691 igmp_group_added(pmc);
1695 /* Device going down */
1697 void ip_mc_down(struct in_device *in_dev)
1699 struct ip_mc_list *pmc;
1703 for_each_pmc_rtnl(in_dev, pmc)
1704 igmp_group_dropped(pmc);
1706 #ifdef CONFIG_IP_MULTICAST
1707 in_dev->mr_ifc_count = 0;
1708 if (del_timer(&in_dev->mr_ifc_timer))
1709 __in_dev_put(in_dev);
1710 in_dev->mr_gq_running = 0;
1711 if (del_timer(&in_dev->mr_gq_timer))
1712 __in_dev_put(in_dev);
1715 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1718 void ip_mc_init_dev(struct in_device *in_dev)
1720 #ifdef CONFIG_IP_MULTICAST
1721 struct net *net = dev_net(in_dev->dev);
1725 #ifdef CONFIG_IP_MULTICAST
1726 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1727 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
1728 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1731 spin_lock_init(&in_dev->mc_tomb_lock);
1734 /* Device going up */
1736 void ip_mc_up(struct in_device *in_dev)
1738 struct ip_mc_list *pmc;
1739 #ifdef CONFIG_IP_MULTICAST
1740 struct net *net = dev_net(in_dev->dev);
1745 #ifdef CONFIG_IP_MULTICAST
1746 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1748 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1750 for_each_pmc_rtnl(in_dev, pmc) {
1751 #ifdef CONFIG_IP_MULTICAST
1752 igmpv3_del_delrec(in_dev, pmc);
1754 igmp_group_added(pmc);
1759 * Device is about to be destroyed: clean up.
1762 void ip_mc_destroy_dev(struct in_device *in_dev)
1764 struct ip_mc_list *i;
1768 /* Deactivate timers */
1770 #ifdef CONFIG_IP_MULTICAST
1771 igmpv3_clear_delrec(in_dev);
1774 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1775 in_dev->mc_list = i->next_rcu;
1781 /* RTNL is locked */
1782 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1784 struct net_device *dev = NULL;
1785 struct in_device *idev = NULL;
1787 if (imr->imr_ifindex) {
1788 idev = inetdev_by_index(net, imr->imr_ifindex);
1791 if (imr->imr_address.s_addr) {
1792 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1798 struct rtable *rt = ip_route_output(net,
1799 imr->imr_multiaddr.s_addr,
1807 imr->imr_ifindex = dev->ifindex;
1808 idev = __in_dev_get_rtnl(dev);
1814 * Join a socket to a group
1817 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1820 struct ip_sf_list *psf, *psf_prev;
1824 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1825 if (psf->sf_inaddr == *psfsrc)
1829 if (!psf || psf->sf_count[sfmode] == 0) {
1830 /* source filter not found, or count wrong => bug */
1833 psf->sf_count[sfmode]--;
1834 if (psf->sf_count[sfmode] == 0) {
1835 ip_rt_multicast_event(pmc->interface);
1837 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1838 #ifdef CONFIG_IP_MULTICAST
1839 struct in_device *in_dev = pmc->interface;
1840 struct net *net = dev_net(in_dev->dev);
1843 /* no more filters for this source */
1845 psf_prev->sf_next = psf->sf_next;
1847 pmc->sources = psf->sf_next;
1848 #ifdef CONFIG_IP_MULTICAST
1849 if (psf->sf_oldin &&
1850 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1851 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1852 psf->sf_next = pmc->tomb;
1862 #ifndef CONFIG_IP_MULTICAST
1863 #define igmp_ifc_event(x) do { } while (0)
1866 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1867 int sfcount, __be32 *psfsrc, int delta)
1869 struct ip_mc_list *pmc;
1876 for_each_pmc_rcu(in_dev, pmc) {
1877 if (*pmca == pmc->multiaddr)
1881 /* MCA not found?? bug */
1885 spin_lock_bh(&pmc->lock);
1887 #ifdef CONFIG_IP_MULTICAST
1892 if (!pmc->sfcount[sfmode])
1894 pmc->sfcount[sfmode]--;
1897 for (i = 0; i < sfcount; i++) {
1898 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1900 changerec |= rv > 0;
1904 if (pmc->sfmode == MCAST_EXCLUDE &&
1905 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1906 pmc->sfcount[MCAST_INCLUDE]) {
1907 #ifdef CONFIG_IP_MULTICAST
1908 struct ip_sf_list *psf;
1909 struct net *net = dev_net(in_dev->dev);
1912 /* filter mode change */
1913 pmc->sfmode = MCAST_INCLUDE;
1914 #ifdef CONFIG_IP_MULTICAST
1915 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1916 in_dev->mr_ifc_count = pmc->crcount;
1917 for (psf = pmc->sources; psf; psf = psf->sf_next)
1918 psf->sf_crcount = 0;
1919 igmp_ifc_event(pmc->interface);
1920 } else if (sf_setstate(pmc) || changerec) {
1921 igmp_ifc_event(pmc->interface);
1925 spin_unlock_bh(&pmc->lock);
1930 * Add multicast single-source filter to the interface list
1932 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1935 struct ip_sf_list *psf, *psf_prev;
1938 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1939 if (psf->sf_inaddr == *psfsrc)
1944 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1947 psf->sf_inaddr = *psfsrc;
1949 psf_prev->sf_next = psf;
1953 psf->sf_count[sfmode]++;
1954 if (psf->sf_count[sfmode] == 1) {
1955 ip_rt_multicast_event(pmc->interface);
1960 #ifdef CONFIG_IP_MULTICAST
1961 static void sf_markstate(struct ip_mc_list *pmc)
1963 struct ip_sf_list *psf;
1964 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1966 for (psf = pmc->sources; psf; psf = psf->sf_next)
1967 if (pmc->sfcount[MCAST_EXCLUDE]) {
1968 psf->sf_oldin = mca_xcount ==
1969 psf->sf_count[MCAST_EXCLUDE] &&
1970 !psf->sf_count[MCAST_INCLUDE];
1972 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1975 static int sf_setstate(struct ip_mc_list *pmc)
1977 struct ip_sf_list *psf, *dpsf;
1978 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1979 int qrv = pmc->interface->mr_qrv;
1983 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1984 if (pmc->sfcount[MCAST_EXCLUDE]) {
1985 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1986 !psf->sf_count[MCAST_INCLUDE];
1988 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1990 if (!psf->sf_oldin) {
1991 struct ip_sf_list *prev = NULL;
1993 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
1994 if (dpsf->sf_inaddr == psf->sf_inaddr)
2000 prev->sf_next = dpsf->sf_next;
2002 pmc->tomb = dpsf->sf_next;
2005 psf->sf_crcount = qrv;
2008 } else if (psf->sf_oldin) {
2010 psf->sf_crcount = 0;
2012 * add or update "delete" records if an active filter
2015 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2016 if (dpsf->sf_inaddr == psf->sf_inaddr)
2019 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2023 /* pmc->lock held by callers */
2024 dpsf->sf_next = pmc->tomb;
2027 dpsf->sf_crcount = qrv;
2036 * Add multicast source filter list to the interface list
2038 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2039 int sfcount, __be32 *psfsrc, int delta)
2041 struct ip_mc_list *pmc;
2048 for_each_pmc_rcu(in_dev, pmc) {
2049 if (*pmca == pmc->multiaddr)
2053 /* MCA not found?? bug */
2057 spin_lock_bh(&pmc->lock);
2060 #ifdef CONFIG_IP_MULTICAST
2063 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2065 pmc->sfcount[sfmode]++;
2067 for (i = 0; i < sfcount; i++) {
2068 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2076 pmc->sfcount[sfmode]--;
2077 for (j = 0; j < i; j++)
2078 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2079 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2080 #ifdef CONFIG_IP_MULTICAST
2081 struct ip_sf_list *psf;
2082 struct net *net = dev_net(pmc->interface->dev);
2083 in_dev = pmc->interface;
2086 /* filter mode change */
2087 if (pmc->sfcount[MCAST_EXCLUDE])
2088 pmc->sfmode = MCAST_EXCLUDE;
2089 else if (pmc->sfcount[MCAST_INCLUDE])
2090 pmc->sfmode = MCAST_INCLUDE;
2091 #ifdef CONFIG_IP_MULTICAST
2092 /* else no filters; keep old mode for reports */
2094 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2095 in_dev->mr_ifc_count = pmc->crcount;
2096 for (psf = pmc->sources; psf; psf = psf->sf_next)
2097 psf->sf_crcount = 0;
2098 igmp_ifc_event(in_dev);
2099 } else if (sf_setstate(pmc)) {
2100 igmp_ifc_event(in_dev);
2103 spin_unlock_bh(&pmc->lock);
2107 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2109 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
2111 spin_lock_bh(&pmc->lock);
2114 sources = pmc->sources;
2115 pmc->sources = NULL;
2116 pmc->sfmode = MCAST_EXCLUDE;
2117 pmc->sfcount[MCAST_INCLUDE] = 0;
2118 pmc->sfcount[MCAST_EXCLUDE] = 1;
2119 spin_unlock_bh(&pmc->lock);
2121 for (psf = tomb; psf; psf = nextpsf) {
2122 nextpsf = psf->sf_next;
2125 for (psf = sources; psf; psf = nextpsf) {
2126 nextpsf = psf->sf_next;
2131 /* Join a multicast group
2134 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2136 __be32 addr = imr->imr_multiaddr.s_addr;
2137 struct ip_mc_socklist *iml, *i;
2138 struct in_device *in_dev;
2139 struct inet_sock *inet = inet_sk(sk);
2140 struct net *net = sock_net(sk);
2147 if (!ipv4_is_multicast(addr))
2150 in_dev = ip_mc_find_dev(net, imr);
2158 ifindex = imr->imr_ifindex;
2159 for_each_pmc_rtnl(inet, i) {
2160 if (i->multi.imr_multiaddr.s_addr == addr &&
2161 i->multi.imr_ifindex == ifindex)
2166 if (count >= net->ipv4.sysctl_igmp_max_memberships)
2168 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2172 memcpy(&iml->multi, imr, sizeof(*imr));
2173 iml->next_rcu = inet->mc_list;
2175 iml->sfmode = MCAST_EXCLUDE;
2176 rcu_assign_pointer(inet->mc_list, iml);
2177 ip_mc_inc_group(in_dev, addr);
2182 EXPORT_SYMBOL(ip_mc_join_group);
2184 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2185 struct in_device *in_dev)
2187 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2191 /* any-source empty exclude case */
2192 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2193 iml->sfmode, 0, NULL, 0);
2195 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2196 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2197 RCU_INIT_POINTER(iml->sflist, NULL);
2198 /* decrease mem now to avoid the memleak warning */
2199 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2200 kfree_rcu(psf, rcu);
2204 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2206 struct inet_sock *inet = inet_sk(sk);
2207 struct ip_mc_socklist *iml;
2208 struct ip_mc_socklist __rcu **imlp;
2209 struct in_device *in_dev;
2210 struct net *net = sock_net(sk);
2211 __be32 group = imr->imr_multiaddr.s_addr;
2213 int ret = -EADDRNOTAVAIL;
2217 in_dev = ip_mc_find_dev(net, imr);
2218 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2222 ifindex = imr->imr_ifindex;
2223 for (imlp = &inet->mc_list;
2224 (iml = rtnl_dereference(*imlp)) != NULL;
2225 imlp = &iml->next_rcu) {
2226 if (iml->multi.imr_multiaddr.s_addr != group)
2229 if (iml->multi.imr_ifindex != ifindex)
2231 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2232 iml->multi.imr_address.s_addr)
2235 (void) ip_mc_leave_src(sk, iml, in_dev);
2237 *imlp = iml->next_rcu;
2240 ip_mc_dec_group(in_dev, group);
2242 /* decrease mem now to avoid the memleak warning */
2243 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2244 kfree_rcu(iml, rcu);
2250 EXPORT_SYMBOL(ip_mc_leave_group);
2252 int ip_mc_source(int add, int omode, struct sock *sk, struct
2253 ip_mreq_source *mreqs, int ifindex)
2256 struct ip_mreqn imr;
2257 __be32 addr = mreqs->imr_multiaddr;
2258 struct ip_mc_socklist *pmc;
2259 struct in_device *in_dev = NULL;
2260 struct inet_sock *inet = inet_sk(sk);
2261 struct ip_sf_socklist *psl;
2262 struct net *net = sock_net(sk);
2266 if (!ipv4_is_multicast(addr))
2271 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2272 imr.imr_address.s_addr = mreqs->imr_interface;
2273 imr.imr_ifindex = ifindex;
2274 in_dev = ip_mc_find_dev(net, &imr);
2280 err = -EADDRNOTAVAIL;
2282 for_each_pmc_rtnl(inet, pmc) {
2283 if ((pmc->multi.imr_multiaddr.s_addr ==
2284 imr.imr_multiaddr.s_addr) &&
2285 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2288 if (!pmc) { /* must have a prior join */
2292 /* if a source filter was set, must be the same mode as before */
2294 if (pmc->sfmode != omode) {
2298 } else if (pmc->sfmode != omode) {
2299 /* allow mode switches for empty-set filters */
2300 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2301 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2303 pmc->sfmode = omode;
2306 psl = rtnl_dereference(pmc->sflist);
2309 goto done; /* err = -EADDRNOTAVAIL */
2311 for (i = 0; i < psl->sl_count; i++) {
2312 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2317 if (rv) /* source not found */
2318 goto done; /* err = -EADDRNOTAVAIL */
2320 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2321 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2326 /* update the interface filter */
2327 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2328 &mreqs->imr_sourceaddr, 1);
2330 for (j = i+1; j < psl->sl_count; j++)
2331 psl->sl_addr[j-1] = psl->sl_addr[j];
2336 /* else, add a new source to the filter */
2338 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2342 if (!psl || psl->sl_count == psl->sl_max) {
2343 struct ip_sf_socklist *newpsl;
2344 int count = IP_SFBLOCK;
2347 count += psl->sl_max;
2348 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2353 newpsl->sl_max = count;
2354 newpsl->sl_count = count - IP_SFBLOCK;
2356 for (i = 0; i < psl->sl_count; i++)
2357 newpsl->sl_addr[i] = psl->sl_addr[i];
2358 /* decrease mem now to avoid the memleak warning */
2359 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2360 kfree_rcu(psl, rcu);
2362 rcu_assign_pointer(pmc->sflist, newpsl);
2365 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2366 for (i = 0; i < psl->sl_count; i++) {
2367 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2372 if (rv == 0) /* address already there is an error */
2374 for (j = psl->sl_count-1; j >= i; j--)
2375 psl->sl_addr[j+1] = psl->sl_addr[j];
2376 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2379 /* update the interface list */
2380 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2381 &mreqs->imr_sourceaddr, 1);
2384 err = ip_mc_leave_group(sk, &imr);
2388 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2391 struct ip_mreqn imr;
2392 __be32 addr = msf->imsf_multiaddr;
2393 struct ip_mc_socklist *pmc;
2394 struct in_device *in_dev;
2395 struct inet_sock *inet = inet_sk(sk);
2396 struct ip_sf_socklist *newpsl, *psl;
2397 struct net *net = sock_net(sk);
2400 if (!ipv4_is_multicast(addr))
2402 if (msf->imsf_fmode != MCAST_INCLUDE &&
2403 msf->imsf_fmode != MCAST_EXCLUDE)
2408 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2409 imr.imr_address.s_addr = msf->imsf_interface;
2410 imr.imr_ifindex = ifindex;
2411 in_dev = ip_mc_find_dev(net, &imr);
2418 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2419 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2424 for_each_pmc_rtnl(inet, pmc) {
2425 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2426 pmc->multi.imr_ifindex == imr.imr_ifindex)
2429 if (!pmc) { /* must have a prior join */
2433 if (msf->imsf_numsrc) {
2434 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2440 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2441 memcpy(newpsl->sl_addr, msf->imsf_slist,
2442 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2443 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2444 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2446 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2451 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2452 msf->imsf_fmode, 0, NULL, 0);
2454 psl = rtnl_dereference(pmc->sflist);
2456 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2457 psl->sl_count, psl->sl_addr, 0);
2458 /* decrease mem now to avoid the memleak warning */
2459 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2460 kfree_rcu(psl, rcu);
2462 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2464 rcu_assign_pointer(pmc->sflist, newpsl);
2465 pmc->sfmode = msf->imsf_fmode;
2469 err = ip_mc_leave_group(sk, &imr);
2473 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2474 struct ip_msfilter __user *optval, int __user *optlen)
2476 int err, len, count, copycount;
2477 struct ip_mreqn imr;
2478 __be32 addr = msf->imsf_multiaddr;
2479 struct ip_mc_socklist *pmc;
2480 struct in_device *in_dev;
2481 struct inet_sock *inet = inet_sk(sk);
2482 struct ip_sf_socklist *psl;
2483 struct net *net = sock_net(sk);
2487 if (!ipv4_is_multicast(addr))
2490 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2491 imr.imr_address.s_addr = msf->imsf_interface;
2492 imr.imr_ifindex = 0;
2493 in_dev = ip_mc_find_dev(net, &imr);
2499 err = -EADDRNOTAVAIL;
2501 for_each_pmc_rtnl(inet, pmc) {
2502 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2503 pmc->multi.imr_ifindex == imr.imr_ifindex)
2506 if (!pmc) /* must have a prior join */
2508 msf->imsf_fmode = pmc->sfmode;
2509 psl = rtnl_dereference(pmc->sflist);
2514 count = psl->sl_count;
2516 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2517 len = copycount * sizeof(psl->sl_addr[0]);
2518 msf->imsf_numsrc = count;
2519 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2520 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2524 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2531 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2532 struct group_filter __user *optval, int __user *optlen)
2534 int err, i, count, copycount;
2535 struct sockaddr_in *psin;
2537 struct ip_mc_socklist *pmc;
2538 struct inet_sock *inet = inet_sk(sk);
2539 struct ip_sf_socklist *psl;
2543 psin = (struct sockaddr_in *)&gsf->gf_group;
2544 if (psin->sin_family != AF_INET)
2546 addr = psin->sin_addr.s_addr;
2547 if (!ipv4_is_multicast(addr))
2550 err = -EADDRNOTAVAIL;
2552 for_each_pmc_rtnl(inet, pmc) {
2553 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2554 pmc->multi.imr_ifindex == gsf->gf_interface)
2557 if (!pmc) /* must have a prior join */
2559 gsf->gf_fmode = pmc->sfmode;
2560 psl = rtnl_dereference(pmc->sflist);
2561 count = psl ? psl->sl_count : 0;
2562 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2563 gsf->gf_numsrc = count;
2564 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2565 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2568 for (i = 0; i < copycount; i++) {
2569 struct sockaddr_storage ss;
2571 psin = (struct sockaddr_in *)&ss;
2572 memset(&ss, 0, sizeof(ss));
2573 psin->sin_family = AF_INET;
2574 psin->sin_addr.s_addr = psl->sl_addr[i];
2575 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2584 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2586 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2589 struct inet_sock *inet = inet_sk(sk);
2590 struct ip_mc_socklist *pmc;
2591 struct ip_sf_socklist *psl;
2596 if (!ipv4_is_multicast(loc_addr))
2600 for_each_pmc_rcu(inet, pmc) {
2601 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2602 (pmc->multi.imr_ifindex == dif ||
2603 (sdif && pmc->multi.imr_ifindex == sdif)))
2609 psl = rcu_dereference(pmc->sflist);
2610 ret = (pmc->sfmode == MCAST_EXCLUDE);
2614 for (i = 0; i < psl->sl_count; i++) {
2615 if (psl->sl_addr[i] == rmt_addr)
2619 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2621 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2631 * A socket is closing.
2634 void ip_mc_drop_socket(struct sock *sk)
2636 struct inet_sock *inet = inet_sk(sk);
2637 struct ip_mc_socklist *iml;
2638 struct net *net = sock_net(sk);
2644 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2645 struct in_device *in_dev;
2647 inet->mc_list = iml->next_rcu;
2648 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2649 (void) ip_mc_leave_src(sk, iml, in_dev);
2651 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2652 /* decrease mem now to avoid the memleak warning */
2653 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2654 kfree_rcu(iml, rcu);
2659 /* called with rcu_read_lock() */
2660 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2662 struct ip_mc_list *im;
2663 struct ip_mc_list __rcu **mc_hash;
2664 struct ip_sf_list *psf;
2667 mc_hash = rcu_dereference(in_dev->mc_hash);
2669 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2671 for (im = rcu_dereference(mc_hash[hash]);
2673 im = rcu_dereference(im->next_hash)) {
2674 if (im->multiaddr == mc_addr)
2678 for_each_pmc_rcu(in_dev, im) {
2679 if (im->multiaddr == mc_addr)
2683 if (im && proto == IPPROTO_IGMP) {
2687 for (psf = im->sources; psf; psf = psf->sf_next) {
2688 if (psf->sf_inaddr == src_addr)
2692 rv = psf->sf_count[MCAST_INCLUDE] ||
2693 psf->sf_count[MCAST_EXCLUDE] !=
2694 im->sfcount[MCAST_EXCLUDE];
2696 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2698 rv = 1; /* unspecified source; tentatively allow */
2703 #if defined(CONFIG_PROC_FS)
2704 struct igmp_mc_iter_state {
2705 struct seq_net_private p;
2706 struct net_device *dev;
2707 struct in_device *in_dev;
2710 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2712 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2714 struct net *net = seq_file_net(seq);
2715 struct ip_mc_list *im = NULL;
2716 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2718 state->in_dev = NULL;
2719 for_each_netdev_rcu(net, state->dev) {
2720 struct in_device *in_dev;
2722 in_dev = __in_dev_get_rcu(state->dev);
2725 im = rcu_dereference(in_dev->mc_list);
2727 state->in_dev = in_dev;
2734 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2736 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2738 im = rcu_dereference(im->next_rcu);
2740 state->dev = next_net_device_rcu(state->dev);
2742 state->in_dev = NULL;
2745 state->in_dev = __in_dev_get_rcu(state->dev);
2748 im = rcu_dereference(state->in_dev->mc_list);
2753 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2755 struct ip_mc_list *im = igmp_mc_get_first(seq);
2757 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2759 return pos ? NULL : im;
2762 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2766 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2769 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2771 struct ip_mc_list *im;
2772 if (v == SEQ_START_TOKEN)
2773 im = igmp_mc_get_first(seq);
2775 im = igmp_mc_get_next(seq, v);
2780 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2783 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2785 state->in_dev = NULL;
2790 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2792 if (v == SEQ_START_TOKEN)
2794 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2796 struct ip_mc_list *im = (struct ip_mc_list *)v;
2797 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2801 #ifdef CONFIG_IP_MULTICAST
2802 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2803 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2809 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2810 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2811 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2814 delta = im->timer.expires - jiffies;
2816 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2817 im->multiaddr, im->users,
2819 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2825 static const struct seq_operations igmp_mc_seq_ops = {
2826 .start = igmp_mc_seq_start,
2827 .next = igmp_mc_seq_next,
2828 .stop = igmp_mc_seq_stop,
2829 .show = igmp_mc_seq_show,
2832 struct igmp_mcf_iter_state {
2833 struct seq_net_private p;
2834 struct net_device *dev;
2835 struct in_device *idev;
2836 struct ip_mc_list *im;
2839 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2841 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2843 struct net *net = seq_file_net(seq);
2844 struct ip_sf_list *psf = NULL;
2845 struct ip_mc_list *im = NULL;
2846 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2850 for_each_netdev_rcu(net, state->dev) {
2851 struct in_device *idev;
2852 idev = __in_dev_get_rcu(state->dev);
2853 if (unlikely(!idev))
2855 im = rcu_dereference(idev->mc_list);
2857 spin_lock_bh(&im->lock);
2864 spin_unlock_bh(&im->lock);
2870 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2872 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2876 spin_unlock_bh(&state->im->lock);
2877 state->im = state->im->next;
2878 while (!state->im) {
2879 state->dev = next_net_device_rcu(state->dev);
2884 state->idev = __in_dev_get_rcu(state->dev);
2887 state->im = rcu_dereference(state->idev->mc_list);
2891 spin_lock_bh(&state->im->lock);
2892 psf = state->im->sources;
2898 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2900 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2902 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2904 return pos ? NULL : psf;
2907 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2911 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2914 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2916 struct ip_sf_list *psf;
2917 if (v == SEQ_START_TOKEN)
2918 psf = igmp_mcf_get_first(seq);
2920 psf = igmp_mcf_get_next(seq, v);
2925 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2928 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2929 if (likely(state->im)) {
2930 spin_unlock_bh(&state->im->lock);
2938 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2940 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2941 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2943 if (v == SEQ_START_TOKEN) {
2944 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2948 "0x%08x %6lu %6lu\n",
2949 state->dev->ifindex, state->dev->name,
2950 ntohl(state->im->multiaddr),
2951 ntohl(psf->sf_inaddr),
2952 psf->sf_count[MCAST_INCLUDE],
2953 psf->sf_count[MCAST_EXCLUDE]);
2958 static const struct seq_operations igmp_mcf_seq_ops = {
2959 .start = igmp_mcf_seq_start,
2960 .next = igmp_mcf_seq_next,
2961 .stop = igmp_mcf_seq_stop,
2962 .show = igmp_mcf_seq_show,
2965 static int __net_init igmp_net_init(struct net *net)
2967 struct proc_dir_entry *pde;
2970 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
2971 sizeof(struct igmp_mc_iter_state));
2974 pde = proc_create_net("mcfilter", 0444, net->proc_net,
2975 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
2978 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2979 SOCK_DGRAM, 0, net);
2981 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2989 remove_proc_entry("mcfilter", net->proc_net);
2991 remove_proc_entry("igmp", net->proc_net);
2996 static void __net_exit igmp_net_exit(struct net *net)
2998 remove_proc_entry("mcfilter", net->proc_net);
2999 remove_proc_entry("igmp", net->proc_net);
3000 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3003 static struct pernet_operations igmp_net_ops = {
3004 .init = igmp_net_init,
3005 .exit = igmp_net_exit,
3009 static int igmp_netdev_event(struct notifier_block *this,
3010 unsigned long event, void *ptr)
3012 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3013 struct in_device *in_dev;
3016 case NETDEV_RESEND_IGMP:
3017 in_dev = __in_dev_get_rtnl(dev);
3019 ip_mc_rejoin_groups(in_dev);
3027 static struct notifier_block igmp_notifier = {
3028 .notifier_call = igmp_netdev_event,
3031 int __init igmp_mc_init(void)
3033 #if defined(CONFIG_PROC_FS)
3036 err = register_pernet_subsys(&igmp_net_ops);
3039 err = register_netdevice_notifier(&igmp_notifier);
3041 goto reg_notif_fail;
3045 unregister_pernet_subsys(&igmp_net_ops);
3048 return register_netdevice_notifier(&igmp_notifier);