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;
389 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
390 pip->protocol = IPPROTO_IGMP;
391 pip->tot_len = 0; /* filled in later */
392 ip_select_ident(net, skb, NULL);
393 ((u8 *)&pip[1])[0] = IPOPT_RA;
394 ((u8 *)&pip[1])[1] = 4;
395 ((u8 *)&pip[1])[2] = 0;
396 ((u8 *)&pip[1])[3] = 0;
398 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
399 skb_put(skb, sizeof(*pig));
400 pig = igmpv3_report_hdr(skb);
401 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
409 static int igmpv3_sendpack(struct sk_buff *skb)
411 struct igmphdr *pig = igmp_hdr(skb);
412 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
414 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
416 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
419 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
421 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
424 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
425 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
427 struct net_device *dev = pmc->interface->dev;
428 struct igmpv3_report *pih;
429 struct igmpv3_grec *pgr;
432 skb = igmpv3_newpack(dev, mtu);
436 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
437 pgr->grec_type = type;
438 pgr->grec_auxwords = 0;
440 pgr->grec_mca = pmc->multiaddr;
441 pih = igmpv3_report_hdr(skb);
442 pih->ngrec = htons(ntohs(pih->ngrec)+1);
447 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
449 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
450 int type, int gdeleted, int sdeleted)
452 struct net_device *dev = pmc->interface->dev;
453 struct net *net = dev_net(dev);
454 struct igmpv3_report *pih;
455 struct igmpv3_grec *pgr = NULL;
456 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
457 int scount, stotal, first, isquery, truncate;
460 if (pmc->multiaddr == IGMP_ALL_HOSTS)
462 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
465 mtu = READ_ONCE(dev->mtu);
466 if (mtu < IPV4_MIN_MTU)
469 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
470 type == IGMPV3_MODE_IS_EXCLUDE;
471 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
472 type == IGMPV3_CHANGE_TO_EXCLUDE;
476 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
481 pih = skb ? igmpv3_report_hdr(skb) : NULL;
483 /* EX and TO_EX get a fresh packet, if needed */
485 if (pih && pih->ngrec &&
486 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
488 igmpv3_sendpack(skb);
489 skb = igmpv3_newpack(dev, mtu);
494 for (psf = *psf_list; psf; psf = psf_next) {
497 psf_next = psf->sf_next;
499 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
504 /* Based on RFC3376 5.1. Should not send source-list change
505 * records when there is a filter mode change.
507 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
508 (!gdeleted && pmc->crcount)) &&
509 (type == IGMPV3_ALLOW_NEW_SOURCES ||
510 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
511 goto decrease_sf_crcount;
513 /* clear marks on query responses */
517 if (AVAILABLE(skb) < sizeof(__be32) +
518 first*sizeof(struct igmpv3_grec)) {
519 if (truncate && !first)
520 break; /* truncate these */
522 pgr->grec_nsrcs = htons(scount);
524 igmpv3_sendpack(skb);
525 skb = igmpv3_newpack(dev, mtu);
530 skb = add_grhead(skb, pmc, type, &pgr, mtu);
535 psrc = skb_put(skb, sizeof(__be32));
536 *psrc = psf->sf_inaddr;
538 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
539 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
542 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
544 psf_prev->sf_next = psf->sf_next;
546 *psf_list = psf->sf_next;
556 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
557 type == IGMPV3_BLOCK_OLD_SOURCES)
559 if (pmc->crcount || isquery) {
560 /* make sure we have room for group header */
561 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
562 igmpv3_sendpack(skb);
563 skb = NULL; /* add_grhead will get a new one */
565 skb = add_grhead(skb, pmc, type, &pgr, mtu);
569 pgr->grec_nsrcs = htons(scount);
572 pmc->gsquery = 0; /* clear query state on report */
576 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
578 struct sk_buff *skb = NULL;
579 struct net *net = dev_net(in_dev->dev);
584 for_each_pmc_rcu(in_dev, pmc) {
585 if (pmc->multiaddr == IGMP_ALL_HOSTS)
587 if (ipv4_is_local_multicast(pmc->multiaddr) &&
588 !net->ipv4.sysctl_igmp_llm_reports)
590 spin_lock_bh(&pmc->lock);
591 if (pmc->sfcount[MCAST_EXCLUDE])
592 type = IGMPV3_MODE_IS_EXCLUDE;
594 type = IGMPV3_MODE_IS_INCLUDE;
595 skb = add_grec(skb, pmc, type, 0, 0);
596 spin_unlock_bh(&pmc->lock);
600 spin_lock_bh(&pmc->lock);
601 if (pmc->sfcount[MCAST_EXCLUDE])
602 type = IGMPV3_MODE_IS_EXCLUDE;
604 type = IGMPV3_MODE_IS_INCLUDE;
605 skb = add_grec(skb, pmc, type, 0, 0);
606 spin_unlock_bh(&pmc->lock);
610 return igmpv3_sendpack(skb);
614 * remove zero-count source records from a source filter list
616 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
618 struct ip_sf_list *psf_prev, *psf_next, *psf;
621 for (psf = *ppsf; psf; psf = psf_next) {
622 psf_next = psf->sf_next;
623 if (psf->sf_crcount == 0) {
625 psf_prev->sf_next = psf->sf_next;
627 *ppsf = psf->sf_next;
634 static void igmpv3_send_cr(struct in_device *in_dev)
636 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
637 struct sk_buff *skb = NULL;
641 spin_lock_bh(&in_dev->mc_tomb_lock);
645 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
646 pmc_next = pmc->next;
647 if (pmc->sfmode == MCAST_INCLUDE) {
648 type = IGMPV3_BLOCK_OLD_SOURCES;
649 dtype = IGMPV3_BLOCK_OLD_SOURCES;
650 skb = add_grec(skb, pmc, type, 1, 0);
651 skb = add_grec(skb, pmc, dtype, 1, 1);
654 if (pmc->sfmode == MCAST_EXCLUDE) {
655 type = IGMPV3_CHANGE_TO_INCLUDE;
656 skb = add_grec(skb, pmc, type, 1, 0);
659 if (pmc->crcount == 0) {
660 igmpv3_clear_zeros(&pmc->tomb);
661 igmpv3_clear_zeros(&pmc->sources);
664 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
666 pmc_prev->next = pmc_next;
668 in_dev->mc_tomb = pmc_next;
669 in_dev_put(pmc->interface);
674 spin_unlock_bh(&in_dev->mc_tomb_lock);
677 for_each_pmc_rcu(in_dev, pmc) {
678 spin_lock_bh(&pmc->lock);
679 if (pmc->sfcount[MCAST_EXCLUDE]) {
680 type = IGMPV3_BLOCK_OLD_SOURCES;
681 dtype = IGMPV3_ALLOW_NEW_SOURCES;
683 type = IGMPV3_ALLOW_NEW_SOURCES;
684 dtype = IGMPV3_BLOCK_OLD_SOURCES;
686 skb = add_grec(skb, pmc, type, 0, 0);
687 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
689 /* filter mode changes */
691 if (pmc->sfmode == MCAST_EXCLUDE)
692 type = IGMPV3_CHANGE_TO_EXCLUDE;
694 type = IGMPV3_CHANGE_TO_INCLUDE;
695 skb = add_grec(skb, pmc, type, 0, 0);
698 spin_unlock_bh(&pmc->lock);
704 (void) igmpv3_sendpack(skb);
707 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
714 struct net_device *dev = in_dev->dev;
715 struct net *net = dev_net(dev);
716 __be32 group = pmc ? pmc->multiaddr : 0;
721 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
722 return igmpv3_send_report(in_dev, pmc);
724 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
727 if (type == IGMP_HOST_LEAVE_MESSAGE)
728 dst = IGMP_ALL_ROUTER;
732 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
734 IPPROTO_IGMP, 0, dev->ifindex);
738 hlen = LL_RESERVED_SPACE(dev);
739 tlen = dev->needed_tailroom;
740 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
745 skb->priority = TC_PRIO_CONTROL;
747 skb_dst_set(skb, &rt->dst);
749 skb_reserve(skb, hlen);
751 skb_reset_network_header(skb);
753 skb_put(skb, sizeof(struct iphdr) + 4);
756 iph->ihl = (sizeof(struct iphdr)+4)>>2;
758 iph->frag_off = htons(IP_DF);
761 iph->saddr = fl4.saddr;
762 iph->protocol = IPPROTO_IGMP;
763 ip_select_ident(net, skb, NULL);
764 ((u8 *)&iph[1])[0] = IPOPT_RA;
765 ((u8 *)&iph[1])[1] = 4;
766 ((u8 *)&iph[1])[2] = 0;
767 ((u8 *)&iph[1])[3] = 0;
769 ih = skb_put(skb, sizeof(struct igmphdr));
774 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
776 return ip_local_out(net, skb->sk, skb);
779 static void igmp_gq_timer_expire(struct timer_list *t)
781 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
783 in_dev->mr_gq_running = 0;
784 igmpv3_send_report(in_dev, NULL);
788 static void igmp_ifc_timer_expire(struct timer_list *t)
790 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
792 igmpv3_send_cr(in_dev);
793 if (in_dev->mr_ifc_count) {
794 in_dev->mr_ifc_count--;
795 igmp_ifc_start_timer(in_dev,
796 unsolicited_report_interval(in_dev));
801 static void igmp_ifc_event(struct in_device *in_dev)
803 struct net *net = dev_net(in_dev->dev);
804 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
806 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
807 igmp_ifc_start_timer(in_dev, 1);
811 static void igmp_timer_expire(struct timer_list *t)
813 struct ip_mc_list *im = from_timer(im, t, timer);
814 struct in_device *in_dev = im->interface;
816 spin_lock(&im->lock);
819 if (im->unsolicit_count) {
820 im->unsolicit_count--;
821 igmp_start_timer(im, unsolicited_report_interval(in_dev));
824 spin_unlock(&im->lock);
826 if (IGMP_V1_SEEN(in_dev))
827 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
828 else if (IGMP_V2_SEEN(in_dev))
829 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
831 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
836 /* mark EXCLUDE-mode sources */
837 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
839 struct ip_sf_list *psf;
843 for (psf = pmc->sources; psf; psf = psf->sf_next) {
846 for (i = 0; i < nsrcs; i++) {
847 /* skip inactive filters */
848 if (psf->sf_count[MCAST_INCLUDE] ||
849 pmc->sfcount[MCAST_EXCLUDE] !=
850 psf->sf_count[MCAST_EXCLUDE])
852 if (srcs[i] == psf->sf_inaddr) {
859 if (scount == nsrcs) /* all sources excluded */
864 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
866 struct ip_sf_list *psf;
869 if (pmc->sfmode == MCAST_EXCLUDE)
870 return igmp_xmarksources(pmc, nsrcs, srcs);
872 /* mark INCLUDE-mode sources */
874 for (psf = pmc->sources; psf; psf = psf->sf_next) {
877 for (i = 0; i < nsrcs; i++)
878 if (srcs[i] == psf->sf_inaddr) {
892 /* return true if packet was dropped */
893 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
895 struct ip_mc_list *im;
896 struct net *net = dev_net(in_dev->dev);
898 /* Timers are only set for non-local groups */
900 if (group == IGMP_ALL_HOSTS)
902 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
906 for_each_pmc_rcu(in_dev, im) {
907 if (im->multiaddr == group) {
916 /* return true if packet was dropped */
917 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
920 struct igmphdr *ih = igmp_hdr(skb);
921 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
922 struct ip_mc_list *im;
923 __be32 group = ih->group;
926 struct net *net = dev_net(in_dev->dev);
931 /* Alas, old v1 router presents here. */
933 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
934 in_dev->mr_v1_seen = jiffies +
935 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
938 /* v2 router present */
939 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
940 in_dev->mr_v2_seen = jiffies +
941 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
943 /* cancel the interface change timer */
944 in_dev->mr_ifc_count = 0;
945 if (del_timer(&in_dev->mr_ifc_timer))
946 __in_dev_put(in_dev);
947 /* clear deleted report items */
948 igmpv3_clear_delrec(in_dev);
949 } else if (len < 12) {
950 return true; /* ignore bogus packet; freed by caller */
951 } else if (IGMP_V1_SEEN(in_dev)) {
952 /* This is a v3 query with v1 queriers present */
953 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
955 } else if (IGMP_V2_SEEN(in_dev)) {
956 /* this is a v3 query with v2 queriers present;
957 * Interpretation of the max_delay code is problematic here.
958 * A real v2 host would use ih_code directly, while v3 has a
959 * different encoding. We use the v3 encoding as more likely
960 * to be intended in a v3 query.
962 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
964 max_delay = 1; /* can't mod w/ 0 */
966 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
969 ih3 = igmpv3_query_hdr(skb);
971 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
972 + ntohs(ih3->nsrcs)*sizeof(__be32)))
974 ih3 = igmpv3_query_hdr(skb);
977 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
979 max_delay = 1; /* can't mod w/ 0 */
980 in_dev->mr_maxdelay = max_delay;
982 in_dev->mr_qrv = ih3->qrv;
983 if (!group) { /* general query */
985 return true; /* no sources allowed */
986 igmp_gq_start_timer(in_dev);
989 /* mark sources to include, if group & source-specific */
990 mark = ih3->nsrcs != 0;
994 * - Start the timers in all of our membership records
995 * that the query applies to for the interface on
996 * which the query arrived excl. those that belong
997 * to a "local" group (224.0.0.X)
998 * - For timers already running check if they need to
1000 * - Use the igmp->igmp_code field as the maximum
1004 for_each_pmc_rcu(in_dev, im) {
1007 if (group && group != im->multiaddr)
1009 if (im->multiaddr == IGMP_ALL_HOSTS)
1011 if (ipv4_is_local_multicast(im->multiaddr) &&
1012 !net->ipv4.sysctl_igmp_llm_reports)
1014 spin_lock_bh(&im->lock);
1016 im->gsquery = im->gsquery && mark;
1019 changed = !im->gsquery ||
1020 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1021 spin_unlock_bh(&im->lock);
1023 igmp_mod_timer(im, max_delay);
1029 /* called in rcu_read_lock() section */
1030 int igmp_rcv(struct sk_buff *skb)
1032 /* This basically follows the spec line by line -- see RFC1112 */
1034 struct net_device *dev = skb->dev;
1035 struct in_device *in_dev;
1037 bool dropped = true;
1039 if (netif_is_l3_master(dev)) {
1040 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1045 in_dev = __in_dev_get_rcu(dev);
1049 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1052 if (skb_checksum_simple_validate(skb))
1057 case IGMP_HOST_MEMBERSHIP_QUERY:
1058 dropped = igmp_heard_query(in_dev, skb, len);
1060 case IGMP_HOST_MEMBERSHIP_REPORT:
1061 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1062 /* Is it our report looped back? */
1063 if (rt_is_output_route(skb_rtable(skb)))
1065 /* don't rely on MC router hearing unicast reports */
1066 if (skb->pkt_type == PACKET_MULTICAST ||
1067 skb->pkt_type == PACKET_BROADCAST)
1068 dropped = igmp_heard_report(in_dev, ih->group);
1071 #ifdef CONFIG_IP_PIMSM_V1
1072 return pim_rcv_v1(skb);
1074 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1077 case IGMP_HOST_LEAVE_MESSAGE:
1079 case IGMP_MTRACE_RESP:
1097 * Add a filter to a device
1100 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1102 char buf[MAX_ADDR_LEN];
1103 struct net_device *dev = in_dev->dev;
1105 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1106 We will get multicast token leakage, when IFF_MULTICAST
1107 is changed. This check should be done in ndo_set_rx_mode
1108 routine. Something sort of:
1109 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1112 if (arp_mc_map(addr, buf, dev, 0) == 0)
1113 dev_mc_add(dev, buf);
1117 * Remove a filter from a device
1120 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1122 char buf[MAX_ADDR_LEN];
1123 struct net_device *dev = in_dev->dev;
1125 if (arp_mc_map(addr, buf, dev, 0) == 0)
1126 dev_mc_del(dev, buf);
1129 #ifdef CONFIG_IP_MULTICAST
1131 * deleted ip_mc_list manipulation
1133 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1135 struct ip_mc_list *pmc;
1136 struct net *net = dev_net(in_dev->dev);
1138 /* this is an "ip_mc_list" for convenience; only the fields below
1139 * are actually used. In particular, the refcnt and users are not
1140 * used for management of the delete list. Using the same structure
1141 * for deleted items allows change reports to use common code with
1142 * non-deleted or query-response MCA's.
1144 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1147 spin_lock_init(&pmc->lock);
1148 spin_lock_bh(&im->lock);
1149 pmc->interface = im->interface;
1150 in_dev_hold(in_dev);
1151 pmc->multiaddr = im->multiaddr;
1152 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1153 pmc->sfmode = im->sfmode;
1154 if (pmc->sfmode == MCAST_INCLUDE) {
1155 struct ip_sf_list *psf;
1157 pmc->tomb = im->tomb;
1158 pmc->sources = im->sources;
1159 im->tomb = im->sources = NULL;
1160 for (psf = pmc->sources; psf; psf = psf->sf_next)
1161 psf->sf_crcount = pmc->crcount;
1163 spin_unlock_bh(&im->lock);
1165 spin_lock_bh(&in_dev->mc_tomb_lock);
1166 pmc->next = in_dev->mc_tomb;
1167 in_dev->mc_tomb = pmc;
1168 spin_unlock_bh(&in_dev->mc_tomb_lock);
1172 * restore ip_mc_list deleted records
1174 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1176 struct ip_mc_list *pmc, *pmc_prev;
1177 struct ip_sf_list *psf;
1178 struct net *net = dev_net(in_dev->dev);
1179 __be32 multiaddr = im->multiaddr;
1181 spin_lock_bh(&in_dev->mc_tomb_lock);
1183 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1184 if (pmc->multiaddr == multiaddr)
1190 pmc_prev->next = pmc->next;
1192 in_dev->mc_tomb = pmc->next;
1194 spin_unlock_bh(&in_dev->mc_tomb_lock);
1196 spin_lock_bh(&im->lock);
1198 im->interface = pmc->interface;
1199 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1200 im->sfmode = pmc->sfmode;
1201 if (pmc->sfmode == MCAST_INCLUDE) {
1202 im->tomb = pmc->tomb;
1203 im->sources = pmc->sources;
1204 for (psf = im->sources; psf; psf = psf->sf_next)
1205 psf->sf_crcount = im->crcount;
1207 in_dev_put(pmc->interface);
1210 spin_unlock_bh(&im->lock);
1214 * flush ip_mc_list deleted records
1216 static void igmpv3_clear_delrec(struct in_device *in_dev)
1218 struct ip_mc_list *pmc, *nextpmc;
1220 spin_lock_bh(&in_dev->mc_tomb_lock);
1221 pmc = in_dev->mc_tomb;
1222 in_dev->mc_tomb = NULL;
1223 spin_unlock_bh(&in_dev->mc_tomb_lock);
1225 for (; pmc; pmc = nextpmc) {
1226 nextpmc = pmc->next;
1227 ip_mc_clear_src(pmc);
1228 in_dev_put(pmc->interface);
1231 /* clear dead sources, too */
1233 for_each_pmc_rcu(in_dev, pmc) {
1234 struct ip_sf_list *psf, *psf_next;
1236 spin_lock_bh(&pmc->lock);
1239 spin_unlock_bh(&pmc->lock);
1240 for (; psf; psf = psf_next) {
1241 psf_next = psf->sf_next;
1249 static void igmp_group_dropped(struct ip_mc_list *im)
1251 struct in_device *in_dev = im->interface;
1252 #ifdef CONFIG_IP_MULTICAST
1253 struct net *net = dev_net(in_dev->dev);
1259 ip_mc_filter_del(in_dev, im->multiaddr);
1262 #ifdef CONFIG_IP_MULTICAST
1263 if (im->multiaddr == IGMP_ALL_HOSTS)
1265 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1268 reporter = im->reporter;
1269 igmp_stop_timer(im);
1271 if (!in_dev->dead) {
1272 if (IGMP_V1_SEEN(in_dev))
1274 if (IGMP_V2_SEEN(in_dev)) {
1276 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1280 igmpv3_add_delrec(in_dev, im);
1282 igmp_ifc_event(in_dev);
1287 static void igmp_group_added(struct ip_mc_list *im)
1289 struct in_device *in_dev = im->interface;
1290 #ifdef CONFIG_IP_MULTICAST
1291 struct net *net = dev_net(in_dev->dev);
1294 if (im->loaded == 0) {
1296 ip_mc_filter_add(in_dev, im->multiaddr);
1299 #ifdef CONFIG_IP_MULTICAST
1300 if (im->multiaddr == IGMP_ALL_HOSTS)
1302 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1307 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1308 spin_lock_bh(&im->lock);
1309 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1310 spin_unlock_bh(&im->lock);
1315 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1316 igmp_ifc_event(in_dev);
1322 * Multicast list managers
1325 static u32 ip_mc_hash(const struct ip_mc_list *im)
1327 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1330 static void ip_mc_hash_add(struct in_device *in_dev,
1331 struct ip_mc_list *im)
1333 struct ip_mc_list __rcu **mc_hash;
1336 mc_hash = rtnl_dereference(in_dev->mc_hash);
1338 hash = ip_mc_hash(im);
1339 im->next_hash = mc_hash[hash];
1340 rcu_assign_pointer(mc_hash[hash], im);
1344 /* do not use a hash table for small number of items */
1345 if (in_dev->mc_count < 4)
1348 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1353 for_each_pmc_rtnl(in_dev, im) {
1354 hash = ip_mc_hash(im);
1355 im->next_hash = mc_hash[hash];
1356 RCU_INIT_POINTER(mc_hash[hash], im);
1359 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1362 static void ip_mc_hash_remove(struct in_device *in_dev,
1363 struct ip_mc_list *im)
1365 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1366 struct ip_mc_list *aux;
1370 mc_hash += ip_mc_hash(im);
1371 while ((aux = rtnl_dereference(*mc_hash)) != im)
1372 mc_hash = &aux->next_hash;
1373 *mc_hash = im->next_hash;
1378 * A socket has joined a multicast group on device dev.
1381 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1383 struct ip_mc_list *im;
1384 #ifdef CONFIG_IP_MULTICAST
1385 struct net *net = dev_net(in_dev->dev);
1390 for_each_pmc_rtnl(in_dev, im) {
1391 if (im->multiaddr == addr) {
1393 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1398 im = kzalloc(sizeof(*im), GFP_KERNEL);
1403 im->interface = in_dev;
1404 in_dev_hold(in_dev);
1405 im->multiaddr = addr;
1406 /* initial mode is (EX, empty) */
1407 im->sfmode = MCAST_EXCLUDE;
1408 im->sfcount[MCAST_EXCLUDE] = 1;
1409 refcount_set(&im->refcnt, 1);
1410 spin_lock_init(&im->lock);
1411 #ifdef CONFIG_IP_MULTICAST
1412 timer_setup(&im->timer, igmp_timer_expire, 0);
1413 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1416 im->next_rcu = in_dev->mc_list;
1418 rcu_assign_pointer(in_dev->mc_list, im);
1420 ip_mc_hash_add(in_dev, im);
1422 #ifdef CONFIG_IP_MULTICAST
1423 igmpv3_del_delrec(in_dev, im);
1425 igmp_group_added(im);
1427 ip_rt_multicast_event(in_dev);
1431 EXPORT_SYMBOL(ip_mc_inc_group);
1433 static int ip_mc_check_iphdr(struct sk_buff *skb)
1435 const struct iphdr *iph;
1437 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1439 if (!pskb_may_pull(skb, offset))
1444 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1447 offset += ip_hdrlen(skb) - sizeof(*iph);
1449 if (!pskb_may_pull(skb, offset))
1454 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1457 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1458 if (skb->len < len || len < offset)
1461 skb_set_transport_header(skb, offset);
1466 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1468 unsigned int len = skb_transport_offset(skb);
1470 len += sizeof(struct igmpv3_report);
1472 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1475 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1477 unsigned int len = skb_transport_offset(skb);
1479 len += sizeof(struct igmphdr);
1484 if (skb->len != len) {
1486 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1487 if (skb->len < len || !pskb_may_pull(skb, len))
1491 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1492 * all-systems destination addresses (224.0.0.1) for general queries
1494 if (!igmp_hdr(skb)->group &&
1495 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1501 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1503 switch (igmp_hdr(skb)->type) {
1504 case IGMP_HOST_LEAVE_MESSAGE:
1505 case IGMP_HOST_MEMBERSHIP_REPORT:
1506 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1509 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1510 return ip_mc_check_igmp_reportv3(skb);
1511 case IGMP_HOST_MEMBERSHIP_QUERY:
1512 return ip_mc_check_igmp_query(skb);
1518 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1520 return skb_checksum_simple_validate(skb);
1523 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1526 struct sk_buff *skb_chk;
1527 unsigned int transport_len;
1528 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1531 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1533 skb_chk = skb_checksum_trimmed(skb, transport_len,
1534 ip_mc_validate_checksum);
1538 if (!pskb_may_pull(skb_chk, len))
1541 ret = ip_mc_check_igmp_msg(skb_chk);
1546 *skb_trimmed = skb_chk;
1547 /* free now unneeded clone */
1548 else if (skb_chk != skb)
1554 if (ret && skb_chk && skb_chk != skb)
1561 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1562 * @skb: the skb to validate
1563 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1565 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1566 * skb transport header accordingly and returns zero.
1568 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1570 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1571 * -ENOMEM: A memory allocation failure happened.
1573 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1574 * to NULL): After parsing an IGMP packet successfully it will point to
1575 * an skb which has its tail aligned to the IP packet end. This might
1576 * either be the originally provided skb or a trimmed, cloned version if
1577 * the skb frame had data beyond the IP packet. A cloned skb allows us
1578 * to leave the original skb and its full frame unchanged (which might be
1579 * desirable for layer 2 frame jugglers).
1581 * Caller needs to set the skb network header and free any returned skb if it
1582 * differs from the provided skb.
1584 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1586 int ret = ip_mc_check_iphdr(skb);
1591 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1594 return __ip_mc_check_igmp(skb, skb_trimmed);
1596 EXPORT_SYMBOL(ip_mc_check_igmp);
1599 * Resend IGMP JOIN report; used by netdev notifier.
1601 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1603 #ifdef CONFIG_IP_MULTICAST
1604 struct ip_mc_list *im;
1606 struct net *net = dev_net(in_dev->dev);
1610 for_each_pmc_rtnl(in_dev, im) {
1611 if (im->multiaddr == IGMP_ALL_HOSTS)
1613 if (ipv4_is_local_multicast(im->multiaddr) &&
1614 !net->ipv4.sysctl_igmp_llm_reports)
1617 /* a failover is happening and switches
1618 * must be notified immediately
1620 if (IGMP_V1_SEEN(in_dev))
1621 type = IGMP_HOST_MEMBERSHIP_REPORT;
1622 else if (IGMP_V2_SEEN(in_dev))
1623 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1625 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1626 igmp_send_report(in_dev, im, type);
1632 * A socket has left a multicast group on device dev
1635 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1637 struct ip_mc_list *i;
1638 struct ip_mc_list __rcu **ip;
1642 for (ip = &in_dev->mc_list;
1643 (i = rtnl_dereference(*ip)) != NULL;
1644 ip = &i->next_rcu) {
1645 if (i->multiaddr == addr) {
1646 if (--i->users == 0) {
1647 ip_mc_hash_remove(in_dev, i);
1650 igmp_group_dropped(i);
1654 ip_rt_multicast_event(in_dev);
1663 EXPORT_SYMBOL(ip_mc_dec_group);
1665 /* Device changing type */
1667 void ip_mc_unmap(struct in_device *in_dev)
1669 struct ip_mc_list *pmc;
1673 for_each_pmc_rtnl(in_dev, pmc)
1674 igmp_group_dropped(pmc);
1677 void ip_mc_remap(struct in_device *in_dev)
1679 struct ip_mc_list *pmc;
1683 for_each_pmc_rtnl(in_dev, pmc) {
1684 #ifdef CONFIG_IP_MULTICAST
1685 igmpv3_del_delrec(in_dev, pmc);
1687 igmp_group_added(pmc);
1691 /* Device going down */
1693 void ip_mc_down(struct in_device *in_dev)
1695 struct ip_mc_list *pmc;
1699 for_each_pmc_rtnl(in_dev, pmc)
1700 igmp_group_dropped(pmc);
1702 #ifdef CONFIG_IP_MULTICAST
1703 in_dev->mr_ifc_count = 0;
1704 if (del_timer(&in_dev->mr_ifc_timer))
1705 __in_dev_put(in_dev);
1706 in_dev->mr_gq_running = 0;
1707 if (del_timer(&in_dev->mr_gq_timer))
1708 __in_dev_put(in_dev);
1711 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1714 void ip_mc_init_dev(struct in_device *in_dev)
1716 #ifdef CONFIG_IP_MULTICAST
1717 struct net *net = dev_net(in_dev->dev);
1721 #ifdef CONFIG_IP_MULTICAST
1722 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1723 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
1724 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1727 spin_lock_init(&in_dev->mc_tomb_lock);
1730 /* Device going up */
1732 void ip_mc_up(struct in_device *in_dev)
1734 struct ip_mc_list *pmc;
1735 #ifdef CONFIG_IP_MULTICAST
1736 struct net *net = dev_net(in_dev->dev);
1741 #ifdef CONFIG_IP_MULTICAST
1742 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1744 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1746 for_each_pmc_rtnl(in_dev, pmc) {
1747 #ifdef CONFIG_IP_MULTICAST
1748 igmpv3_del_delrec(in_dev, pmc);
1750 igmp_group_added(pmc);
1755 * Device is about to be destroyed: clean up.
1758 void ip_mc_destroy_dev(struct in_device *in_dev)
1760 struct ip_mc_list *i;
1764 /* Deactivate timers */
1766 #ifdef CONFIG_IP_MULTICAST
1767 igmpv3_clear_delrec(in_dev);
1770 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1771 in_dev->mc_list = i->next_rcu;
1777 /* RTNL is locked */
1778 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1780 struct net_device *dev = NULL;
1781 struct in_device *idev = NULL;
1783 if (imr->imr_ifindex) {
1784 idev = inetdev_by_index(net, imr->imr_ifindex);
1787 if (imr->imr_address.s_addr) {
1788 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1794 struct rtable *rt = ip_route_output(net,
1795 imr->imr_multiaddr.s_addr,
1803 imr->imr_ifindex = dev->ifindex;
1804 idev = __in_dev_get_rtnl(dev);
1810 * Join a socket to a group
1813 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1816 struct ip_sf_list *psf, *psf_prev;
1820 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1821 if (psf->sf_inaddr == *psfsrc)
1825 if (!psf || psf->sf_count[sfmode] == 0) {
1826 /* source filter not found, or count wrong => bug */
1829 psf->sf_count[sfmode]--;
1830 if (psf->sf_count[sfmode] == 0) {
1831 ip_rt_multicast_event(pmc->interface);
1833 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1834 #ifdef CONFIG_IP_MULTICAST
1835 struct in_device *in_dev = pmc->interface;
1836 struct net *net = dev_net(in_dev->dev);
1839 /* no more filters for this source */
1841 psf_prev->sf_next = psf->sf_next;
1843 pmc->sources = psf->sf_next;
1844 #ifdef CONFIG_IP_MULTICAST
1845 if (psf->sf_oldin &&
1846 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1847 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1848 psf->sf_next = pmc->tomb;
1858 #ifndef CONFIG_IP_MULTICAST
1859 #define igmp_ifc_event(x) do { } while (0)
1862 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1863 int sfcount, __be32 *psfsrc, int delta)
1865 struct ip_mc_list *pmc;
1872 for_each_pmc_rcu(in_dev, pmc) {
1873 if (*pmca == pmc->multiaddr)
1877 /* MCA not found?? bug */
1881 spin_lock_bh(&pmc->lock);
1883 #ifdef CONFIG_IP_MULTICAST
1888 if (!pmc->sfcount[sfmode])
1890 pmc->sfcount[sfmode]--;
1893 for (i = 0; i < sfcount; i++) {
1894 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1896 changerec |= rv > 0;
1900 if (pmc->sfmode == MCAST_EXCLUDE &&
1901 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1902 pmc->sfcount[MCAST_INCLUDE]) {
1903 #ifdef CONFIG_IP_MULTICAST
1904 struct ip_sf_list *psf;
1905 struct net *net = dev_net(in_dev->dev);
1908 /* filter mode change */
1909 pmc->sfmode = MCAST_INCLUDE;
1910 #ifdef CONFIG_IP_MULTICAST
1911 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1912 in_dev->mr_ifc_count = pmc->crcount;
1913 for (psf = pmc->sources; psf; psf = psf->sf_next)
1914 psf->sf_crcount = 0;
1915 igmp_ifc_event(pmc->interface);
1916 } else if (sf_setstate(pmc) || changerec) {
1917 igmp_ifc_event(pmc->interface);
1921 spin_unlock_bh(&pmc->lock);
1926 * Add multicast single-source filter to the interface list
1928 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1931 struct ip_sf_list *psf, *psf_prev;
1934 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1935 if (psf->sf_inaddr == *psfsrc)
1940 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1943 psf->sf_inaddr = *psfsrc;
1945 psf_prev->sf_next = psf;
1949 psf->sf_count[sfmode]++;
1950 if (psf->sf_count[sfmode] == 1) {
1951 ip_rt_multicast_event(pmc->interface);
1956 #ifdef CONFIG_IP_MULTICAST
1957 static void sf_markstate(struct ip_mc_list *pmc)
1959 struct ip_sf_list *psf;
1960 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1962 for (psf = pmc->sources; psf; psf = psf->sf_next)
1963 if (pmc->sfcount[MCAST_EXCLUDE]) {
1964 psf->sf_oldin = mca_xcount ==
1965 psf->sf_count[MCAST_EXCLUDE] &&
1966 !psf->sf_count[MCAST_INCLUDE];
1968 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1971 static int sf_setstate(struct ip_mc_list *pmc)
1973 struct ip_sf_list *psf, *dpsf;
1974 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1975 int qrv = pmc->interface->mr_qrv;
1979 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1980 if (pmc->sfcount[MCAST_EXCLUDE]) {
1981 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1982 !psf->sf_count[MCAST_INCLUDE];
1984 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1986 if (!psf->sf_oldin) {
1987 struct ip_sf_list *prev = NULL;
1989 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
1990 if (dpsf->sf_inaddr == psf->sf_inaddr)
1996 prev->sf_next = dpsf->sf_next;
1998 pmc->tomb = dpsf->sf_next;
2001 psf->sf_crcount = qrv;
2004 } else if (psf->sf_oldin) {
2006 psf->sf_crcount = 0;
2008 * add or update "delete" records if an active filter
2011 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2012 if (dpsf->sf_inaddr == psf->sf_inaddr)
2015 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2019 /* pmc->lock held by callers */
2020 dpsf->sf_next = pmc->tomb;
2023 dpsf->sf_crcount = qrv;
2032 * Add multicast source filter list to the interface list
2034 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2035 int sfcount, __be32 *psfsrc, int delta)
2037 struct ip_mc_list *pmc;
2044 for_each_pmc_rcu(in_dev, pmc) {
2045 if (*pmca == pmc->multiaddr)
2049 /* MCA not found?? bug */
2053 spin_lock_bh(&pmc->lock);
2056 #ifdef CONFIG_IP_MULTICAST
2059 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2061 pmc->sfcount[sfmode]++;
2063 for (i = 0; i < sfcount; i++) {
2064 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2072 pmc->sfcount[sfmode]--;
2073 for (j = 0; j < i; j++)
2074 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2075 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2076 #ifdef CONFIG_IP_MULTICAST
2077 struct ip_sf_list *psf;
2078 struct net *net = dev_net(pmc->interface->dev);
2079 in_dev = pmc->interface;
2082 /* filter mode change */
2083 if (pmc->sfcount[MCAST_EXCLUDE])
2084 pmc->sfmode = MCAST_EXCLUDE;
2085 else if (pmc->sfcount[MCAST_INCLUDE])
2086 pmc->sfmode = MCAST_INCLUDE;
2087 #ifdef CONFIG_IP_MULTICAST
2088 /* else no filters; keep old mode for reports */
2090 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2091 in_dev->mr_ifc_count = pmc->crcount;
2092 for (psf = pmc->sources; psf; psf = psf->sf_next)
2093 psf->sf_crcount = 0;
2094 igmp_ifc_event(in_dev);
2095 } else if (sf_setstate(pmc)) {
2096 igmp_ifc_event(in_dev);
2099 spin_unlock_bh(&pmc->lock);
2103 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2105 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
2107 spin_lock_bh(&pmc->lock);
2110 sources = pmc->sources;
2111 pmc->sources = NULL;
2112 pmc->sfmode = MCAST_EXCLUDE;
2113 pmc->sfcount[MCAST_INCLUDE] = 0;
2114 pmc->sfcount[MCAST_EXCLUDE] = 1;
2115 spin_unlock_bh(&pmc->lock);
2117 for (psf = tomb; psf; psf = nextpsf) {
2118 nextpsf = psf->sf_next;
2121 for (psf = sources; psf; psf = nextpsf) {
2122 nextpsf = psf->sf_next;
2127 /* Join a multicast group
2130 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2132 __be32 addr = imr->imr_multiaddr.s_addr;
2133 struct ip_mc_socklist *iml, *i;
2134 struct in_device *in_dev;
2135 struct inet_sock *inet = inet_sk(sk);
2136 struct net *net = sock_net(sk);
2143 if (!ipv4_is_multicast(addr))
2146 in_dev = ip_mc_find_dev(net, imr);
2154 ifindex = imr->imr_ifindex;
2155 for_each_pmc_rtnl(inet, i) {
2156 if (i->multi.imr_multiaddr.s_addr == addr &&
2157 i->multi.imr_ifindex == ifindex)
2162 if (count >= net->ipv4.sysctl_igmp_max_memberships)
2164 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2168 memcpy(&iml->multi, imr, sizeof(*imr));
2169 iml->next_rcu = inet->mc_list;
2171 iml->sfmode = MCAST_EXCLUDE;
2172 rcu_assign_pointer(inet->mc_list, iml);
2173 ip_mc_inc_group(in_dev, addr);
2178 EXPORT_SYMBOL(ip_mc_join_group);
2180 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2181 struct in_device *in_dev)
2183 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2187 /* any-source empty exclude case */
2188 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2189 iml->sfmode, 0, NULL, 0);
2191 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2192 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2193 RCU_INIT_POINTER(iml->sflist, NULL);
2194 /* decrease mem now to avoid the memleak warning */
2195 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2196 kfree_rcu(psf, rcu);
2200 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2202 struct inet_sock *inet = inet_sk(sk);
2203 struct ip_mc_socklist *iml;
2204 struct ip_mc_socklist __rcu **imlp;
2205 struct in_device *in_dev;
2206 struct net *net = sock_net(sk);
2207 __be32 group = imr->imr_multiaddr.s_addr;
2209 int ret = -EADDRNOTAVAIL;
2213 in_dev = ip_mc_find_dev(net, imr);
2214 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2218 ifindex = imr->imr_ifindex;
2219 for (imlp = &inet->mc_list;
2220 (iml = rtnl_dereference(*imlp)) != NULL;
2221 imlp = &iml->next_rcu) {
2222 if (iml->multi.imr_multiaddr.s_addr != group)
2225 if (iml->multi.imr_ifindex != ifindex)
2227 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2228 iml->multi.imr_address.s_addr)
2231 (void) ip_mc_leave_src(sk, iml, in_dev);
2233 *imlp = iml->next_rcu;
2236 ip_mc_dec_group(in_dev, group);
2238 /* decrease mem now to avoid the memleak warning */
2239 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2240 kfree_rcu(iml, rcu);
2246 EXPORT_SYMBOL(ip_mc_leave_group);
2248 int ip_mc_source(int add, int omode, struct sock *sk, struct
2249 ip_mreq_source *mreqs, int ifindex)
2252 struct ip_mreqn imr;
2253 __be32 addr = mreqs->imr_multiaddr;
2254 struct ip_mc_socklist *pmc;
2255 struct in_device *in_dev = NULL;
2256 struct inet_sock *inet = inet_sk(sk);
2257 struct ip_sf_socklist *psl;
2258 struct net *net = sock_net(sk);
2262 if (!ipv4_is_multicast(addr))
2267 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2268 imr.imr_address.s_addr = mreqs->imr_interface;
2269 imr.imr_ifindex = ifindex;
2270 in_dev = ip_mc_find_dev(net, &imr);
2276 err = -EADDRNOTAVAIL;
2278 for_each_pmc_rtnl(inet, pmc) {
2279 if ((pmc->multi.imr_multiaddr.s_addr ==
2280 imr.imr_multiaddr.s_addr) &&
2281 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2284 if (!pmc) { /* must have a prior join */
2288 /* if a source filter was set, must be the same mode as before */
2290 if (pmc->sfmode != omode) {
2294 } else if (pmc->sfmode != omode) {
2295 /* allow mode switches for empty-set filters */
2296 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2297 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2299 pmc->sfmode = omode;
2302 psl = rtnl_dereference(pmc->sflist);
2305 goto done; /* err = -EADDRNOTAVAIL */
2307 for (i = 0; i < psl->sl_count; i++) {
2308 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2313 if (rv) /* source not found */
2314 goto done; /* err = -EADDRNOTAVAIL */
2316 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2317 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2322 /* update the interface filter */
2323 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2324 &mreqs->imr_sourceaddr, 1);
2326 for (j = i+1; j < psl->sl_count; j++)
2327 psl->sl_addr[j-1] = psl->sl_addr[j];
2332 /* else, add a new source to the filter */
2334 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2338 if (!psl || psl->sl_count == psl->sl_max) {
2339 struct ip_sf_socklist *newpsl;
2340 int count = IP_SFBLOCK;
2343 count += psl->sl_max;
2344 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2349 newpsl->sl_max = count;
2350 newpsl->sl_count = count - IP_SFBLOCK;
2352 for (i = 0; i < psl->sl_count; i++)
2353 newpsl->sl_addr[i] = psl->sl_addr[i];
2354 /* decrease mem now to avoid the memleak warning */
2355 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2356 kfree_rcu(psl, rcu);
2358 rcu_assign_pointer(pmc->sflist, newpsl);
2361 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2362 for (i = 0; i < psl->sl_count; i++) {
2363 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2368 if (rv == 0) /* address already there is an error */
2370 for (j = psl->sl_count-1; j >= i; j--)
2371 psl->sl_addr[j+1] = psl->sl_addr[j];
2372 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2375 /* update the interface list */
2376 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2377 &mreqs->imr_sourceaddr, 1);
2380 err = ip_mc_leave_group(sk, &imr);
2384 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2387 struct ip_mreqn imr;
2388 __be32 addr = msf->imsf_multiaddr;
2389 struct ip_mc_socklist *pmc;
2390 struct in_device *in_dev;
2391 struct inet_sock *inet = inet_sk(sk);
2392 struct ip_sf_socklist *newpsl, *psl;
2393 struct net *net = sock_net(sk);
2396 if (!ipv4_is_multicast(addr))
2398 if (msf->imsf_fmode != MCAST_INCLUDE &&
2399 msf->imsf_fmode != MCAST_EXCLUDE)
2404 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2405 imr.imr_address.s_addr = msf->imsf_interface;
2406 imr.imr_ifindex = ifindex;
2407 in_dev = ip_mc_find_dev(net, &imr);
2414 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2415 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2420 for_each_pmc_rtnl(inet, pmc) {
2421 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2422 pmc->multi.imr_ifindex == imr.imr_ifindex)
2425 if (!pmc) { /* must have a prior join */
2429 if (msf->imsf_numsrc) {
2430 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2436 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2437 memcpy(newpsl->sl_addr, msf->imsf_slist,
2438 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2439 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2440 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2442 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2447 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2448 msf->imsf_fmode, 0, NULL, 0);
2450 psl = rtnl_dereference(pmc->sflist);
2452 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2453 psl->sl_count, psl->sl_addr, 0);
2454 /* decrease mem now to avoid the memleak warning */
2455 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2456 kfree_rcu(psl, rcu);
2458 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2460 rcu_assign_pointer(pmc->sflist, newpsl);
2461 pmc->sfmode = msf->imsf_fmode;
2465 err = ip_mc_leave_group(sk, &imr);
2469 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2470 struct ip_msfilter __user *optval, int __user *optlen)
2472 int err, len, count, copycount;
2473 struct ip_mreqn imr;
2474 __be32 addr = msf->imsf_multiaddr;
2475 struct ip_mc_socklist *pmc;
2476 struct in_device *in_dev;
2477 struct inet_sock *inet = inet_sk(sk);
2478 struct ip_sf_socklist *psl;
2479 struct net *net = sock_net(sk);
2483 if (!ipv4_is_multicast(addr))
2486 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2487 imr.imr_address.s_addr = msf->imsf_interface;
2488 imr.imr_ifindex = 0;
2489 in_dev = ip_mc_find_dev(net, &imr);
2495 err = -EADDRNOTAVAIL;
2497 for_each_pmc_rtnl(inet, pmc) {
2498 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2499 pmc->multi.imr_ifindex == imr.imr_ifindex)
2502 if (!pmc) /* must have a prior join */
2504 msf->imsf_fmode = pmc->sfmode;
2505 psl = rtnl_dereference(pmc->sflist);
2510 count = psl->sl_count;
2512 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2513 len = copycount * sizeof(psl->sl_addr[0]);
2514 msf->imsf_numsrc = count;
2515 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2516 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2520 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2527 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2528 struct group_filter __user *optval, int __user *optlen)
2530 int err, i, count, copycount;
2531 struct sockaddr_in *psin;
2533 struct ip_mc_socklist *pmc;
2534 struct inet_sock *inet = inet_sk(sk);
2535 struct ip_sf_socklist *psl;
2539 psin = (struct sockaddr_in *)&gsf->gf_group;
2540 if (psin->sin_family != AF_INET)
2542 addr = psin->sin_addr.s_addr;
2543 if (!ipv4_is_multicast(addr))
2546 err = -EADDRNOTAVAIL;
2548 for_each_pmc_rtnl(inet, pmc) {
2549 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2550 pmc->multi.imr_ifindex == gsf->gf_interface)
2553 if (!pmc) /* must have a prior join */
2555 gsf->gf_fmode = pmc->sfmode;
2556 psl = rtnl_dereference(pmc->sflist);
2557 count = psl ? psl->sl_count : 0;
2558 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2559 gsf->gf_numsrc = count;
2560 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2561 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2564 for (i = 0; i < copycount; i++) {
2565 struct sockaddr_storage ss;
2567 psin = (struct sockaddr_in *)&ss;
2568 memset(&ss, 0, sizeof(ss));
2569 psin->sin_family = AF_INET;
2570 psin->sin_addr.s_addr = psl->sl_addr[i];
2571 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2580 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2582 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2585 struct inet_sock *inet = inet_sk(sk);
2586 struct ip_mc_socklist *pmc;
2587 struct ip_sf_socklist *psl;
2592 if (!ipv4_is_multicast(loc_addr))
2596 for_each_pmc_rcu(inet, pmc) {
2597 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2598 (pmc->multi.imr_ifindex == dif ||
2599 (sdif && pmc->multi.imr_ifindex == sdif)))
2605 psl = rcu_dereference(pmc->sflist);
2606 ret = (pmc->sfmode == MCAST_EXCLUDE);
2610 for (i = 0; i < psl->sl_count; i++) {
2611 if (psl->sl_addr[i] == rmt_addr)
2615 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2617 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2627 * A socket is closing.
2630 void ip_mc_drop_socket(struct sock *sk)
2632 struct inet_sock *inet = inet_sk(sk);
2633 struct ip_mc_socklist *iml;
2634 struct net *net = sock_net(sk);
2640 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2641 struct in_device *in_dev;
2643 inet->mc_list = iml->next_rcu;
2644 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2645 (void) ip_mc_leave_src(sk, iml, in_dev);
2647 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2648 /* decrease mem now to avoid the memleak warning */
2649 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2650 kfree_rcu(iml, rcu);
2655 /* called with rcu_read_lock() */
2656 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2658 struct ip_mc_list *im;
2659 struct ip_mc_list __rcu **mc_hash;
2660 struct ip_sf_list *psf;
2663 mc_hash = rcu_dereference(in_dev->mc_hash);
2665 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2667 for (im = rcu_dereference(mc_hash[hash]);
2669 im = rcu_dereference(im->next_hash)) {
2670 if (im->multiaddr == mc_addr)
2674 for_each_pmc_rcu(in_dev, im) {
2675 if (im->multiaddr == mc_addr)
2679 if (im && proto == IPPROTO_IGMP) {
2683 for (psf = im->sources; psf; psf = psf->sf_next) {
2684 if (psf->sf_inaddr == src_addr)
2688 rv = psf->sf_count[MCAST_INCLUDE] ||
2689 psf->sf_count[MCAST_EXCLUDE] !=
2690 im->sfcount[MCAST_EXCLUDE];
2692 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2694 rv = 1; /* unspecified source; tentatively allow */
2699 #if defined(CONFIG_PROC_FS)
2700 struct igmp_mc_iter_state {
2701 struct seq_net_private p;
2702 struct net_device *dev;
2703 struct in_device *in_dev;
2706 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2708 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2710 struct net *net = seq_file_net(seq);
2711 struct ip_mc_list *im = NULL;
2712 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2714 state->in_dev = NULL;
2715 for_each_netdev_rcu(net, state->dev) {
2716 struct in_device *in_dev;
2718 in_dev = __in_dev_get_rcu(state->dev);
2721 im = rcu_dereference(in_dev->mc_list);
2723 state->in_dev = in_dev;
2730 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2732 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2734 im = rcu_dereference(im->next_rcu);
2736 state->dev = next_net_device_rcu(state->dev);
2738 state->in_dev = NULL;
2741 state->in_dev = __in_dev_get_rcu(state->dev);
2744 im = rcu_dereference(state->in_dev->mc_list);
2749 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2751 struct ip_mc_list *im = igmp_mc_get_first(seq);
2753 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2755 return pos ? NULL : im;
2758 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2762 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2765 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2767 struct ip_mc_list *im;
2768 if (v == SEQ_START_TOKEN)
2769 im = igmp_mc_get_first(seq);
2771 im = igmp_mc_get_next(seq, v);
2776 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2779 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2781 state->in_dev = NULL;
2786 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2788 if (v == SEQ_START_TOKEN)
2790 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2792 struct ip_mc_list *im = (struct ip_mc_list *)v;
2793 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2797 #ifdef CONFIG_IP_MULTICAST
2798 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2799 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2805 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2806 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2807 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2810 delta = im->timer.expires - jiffies;
2812 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2813 im->multiaddr, im->users,
2815 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2821 static const struct seq_operations igmp_mc_seq_ops = {
2822 .start = igmp_mc_seq_start,
2823 .next = igmp_mc_seq_next,
2824 .stop = igmp_mc_seq_stop,
2825 .show = igmp_mc_seq_show,
2828 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2830 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2831 sizeof(struct igmp_mc_iter_state));
2834 static const struct file_operations igmp_mc_seq_fops = {
2835 .open = igmp_mc_seq_open,
2837 .llseek = seq_lseek,
2838 .release = seq_release_net,
2841 struct igmp_mcf_iter_state {
2842 struct seq_net_private p;
2843 struct net_device *dev;
2844 struct in_device *idev;
2845 struct ip_mc_list *im;
2848 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2850 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2852 struct net *net = seq_file_net(seq);
2853 struct ip_sf_list *psf = NULL;
2854 struct ip_mc_list *im = NULL;
2855 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2859 for_each_netdev_rcu(net, state->dev) {
2860 struct in_device *idev;
2861 idev = __in_dev_get_rcu(state->dev);
2862 if (unlikely(!idev))
2864 im = rcu_dereference(idev->mc_list);
2866 spin_lock_bh(&im->lock);
2873 spin_unlock_bh(&im->lock);
2879 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2881 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2885 spin_unlock_bh(&state->im->lock);
2886 state->im = state->im->next;
2887 while (!state->im) {
2888 state->dev = next_net_device_rcu(state->dev);
2893 state->idev = __in_dev_get_rcu(state->dev);
2896 state->im = rcu_dereference(state->idev->mc_list);
2900 spin_lock_bh(&state->im->lock);
2901 psf = state->im->sources;
2907 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2909 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2911 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2913 return pos ? NULL : psf;
2916 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2920 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2923 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2925 struct ip_sf_list *psf;
2926 if (v == SEQ_START_TOKEN)
2927 psf = igmp_mcf_get_first(seq);
2929 psf = igmp_mcf_get_next(seq, v);
2934 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2937 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2938 if (likely(state->im)) {
2939 spin_unlock_bh(&state->im->lock);
2947 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2949 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2950 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2952 if (v == SEQ_START_TOKEN) {
2953 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2957 "0x%08x %6lu %6lu\n",
2958 state->dev->ifindex, state->dev->name,
2959 ntohl(state->im->multiaddr),
2960 ntohl(psf->sf_inaddr),
2961 psf->sf_count[MCAST_INCLUDE],
2962 psf->sf_count[MCAST_EXCLUDE]);
2967 static const struct seq_operations igmp_mcf_seq_ops = {
2968 .start = igmp_mcf_seq_start,
2969 .next = igmp_mcf_seq_next,
2970 .stop = igmp_mcf_seq_stop,
2971 .show = igmp_mcf_seq_show,
2974 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2976 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2977 sizeof(struct igmp_mcf_iter_state));
2980 static const struct file_operations igmp_mcf_seq_fops = {
2981 .open = igmp_mcf_seq_open,
2983 .llseek = seq_lseek,
2984 .release = seq_release_net,
2987 static int __net_init igmp_net_init(struct net *net)
2989 struct proc_dir_entry *pde;
2992 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
2995 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2996 &igmp_mcf_seq_fops);
2999 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3000 SOCK_DGRAM, 0, net);
3002 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3010 remove_proc_entry("mcfilter", net->proc_net);
3012 remove_proc_entry("igmp", net->proc_net);
3017 static void __net_exit igmp_net_exit(struct net *net)
3019 remove_proc_entry("mcfilter", net->proc_net);
3020 remove_proc_entry("igmp", net->proc_net);
3021 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3024 static struct pernet_operations igmp_net_ops = {
3025 .init = igmp_net_init,
3026 .exit = igmp_net_exit,
3030 static int igmp_netdev_event(struct notifier_block *this,
3031 unsigned long event, void *ptr)
3033 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3034 struct in_device *in_dev;
3037 case NETDEV_RESEND_IGMP:
3038 in_dev = __in_dev_get_rtnl(dev);
3040 ip_mc_rejoin_groups(in_dev);
3048 static struct notifier_block igmp_notifier = {
3049 .notifier_call = igmp_netdev_event,
3052 int __init igmp_mc_init(void)
3054 #if defined(CONFIG_PROC_FS)
3057 err = register_pernet_subsys(&igmp_net_ops);
3060 err = register_netdevice_notifier(&igmp_notifier);
3062 goto reg_notif_fail;
3066 unregister_pernet_subsys(&igmp_net_ops);
3069 return register_netdevice_notifier(&igmp_notifier);