2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <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_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
115 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
116 #define IGMP_QUERY_INTERVAL (125*HZ)
117 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
119 #define IGMP_INITIAL_REPORT_DELAY (1)
121 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
122 * IGMP specs require to report membership immediately after
123 * joining a group, but we delay the first report by a
124 * small interval. It seems more natural and still does not
125 * contradict to specs provided this delay is small enough.
128 #define IGMP_V1_SEEN(in_dev) \
129 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
130 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
131 ((in_dev)->mr_v1_seen && \
132 time_before(jiffies, (in_dev)->mr_v1_seen)))
133 #define IGMP_V2_SEEN(in_dev) \
134 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
135 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
136 ((in_dev)->mr_v2_seen && \
137 time_before(jiffies, (in_dev)->mr_v2_seen)))
139 static int unsolicited_report_interval(struct in_device *in_dev)
141 int interval_ms, interval_jiffies;
143 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
144 interval_ms = IN_DEV_CONF_GET(
146 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
148 interval_ms = IN_DEV_CONF_GET(
150 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
152 interval_jiffies = msecs_to_jiffies(interval_ms);
154 /* _timer functions can't handle a delay of 0 jiffies so ensure
155 * we always return a positive value.
157 if (interval_jiffies <= 0)
158 interval_jiffies = 1;
159 return interval_jiffies;
162 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
163 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
164 static void igmpv3_clear_delrec(struct in_device *in_dev);
165 static int sf_setstate(struct ip_mc_list *pmc);
166 static void sf_markstate(struct ip_mc_list *pmc);
168 static void ip_mc_clear_src(struct ip_mc_list *pmc);
169 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
170 int sfcount, __be32 *psfsrc, int delta);
172 static void ip_ma_put(struct ip_mc_list *im)
174 if (refcount_dec_and_test(&im->refcnt)) {
175 in_dev_put(im->interface);
180 #define for_each_pmc_rcu(in_dev, pmc) \
181 for (pmc = rcu_dereference(in_dev->mc_list); \
183 pmc = rcu_dereference(pmc->next_rcu))
185 #define for_each_pmc_rtnl(in_dev, pmc) \
186 for (pmc = rtnl_dereference(in_dev->mc_list); \
188 pmc = rtnl_dereference(pmc->next_rcu))
190 static void ip_sf_list_clear_all(struct ip_sf_list *psf)
192 struct ip_sf_list *next;
201 #ifdef CONFIG_IP_MULTICAST
207 static void igmp_stop_timer(struct ip_mc_list *im)
209 spin_lock_bh(&im->lock);
210 if (del_timer(&im->timer))
211 refcount_dec(&im->refcnt);
214 im->unsolicit_count = 0;
215 spin_unlock_bh(&im->lock);
218 /* It must be called with locked im->lock */
219 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
221 int tv = prandom_u32() % max_delay;
224 if (!mod_timer(&im->timer, jiffies+tv+2))
225 refcount_inc(&im->refcnt);
228 static void igmp_gq_start_timer(struct in_device *in_dev)
230 int tv = prandom_u32() % in_dev->mr_maxdelay;
231 unsigned long exp = jiffies + tv + 2;
233 if (in_dev->mr_gq_running &&
234 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
237 in_dev->mr_gq_running = 1;
238 if (!mod_timer(&in_dev->mr_gq_timer, exp))
242 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
244 int tv = prandom_u32() % delay;
246 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
250 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
252 spin_lock_bh(&im->lock);
253 im->unsolicit_count = 0;
254 if (del_timer(&im->timer)) {
255 if ((long)(im->timer.expires-jiffies) < max_delay) {
256 add_timer(&im->timer);
258 spin_unlock_bh(&im->lock);
261 refcount_dec(&im->refcnt);
263 igmp_start_timer(im, max_delay);
264 spin_unlock_bh(&im->lock);
269 * Send an IGMP report.
272 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
275 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
276 int gdeleted, int sdeleted)
279 case IGMPV3_MODE_IS_INCLUDE:
280 case IGMPV3_MODE_IS_EXCLUDE:
281 if (gdeleted || sdeleted)
283 if (!(pmc->gsquery && !psf->sf_gsresp)) {
284 if (pmc->sfmode == MCAST_INCLUDE)
286 /* don't include if this source is excluded
289 if (psf->sf_count[MCAST_INCLUDE])
290 return type == IGMPV3_MODE_IS_INCLUDE;
291 return pmc->sfcount[MCAST_EXCLUDE] ==
292 psf->sf_count[MCAST_EXCLUDE];
295 case IGMPV3_CHANGE_TO_INCLUDE:
296 if (gdeleted || sdeleted)
298 return psf->sf_count[MCAST_INCLUDE] != 0;
299 case IGMPV3_CHANGE_TO_EXCLUDE:
300 if (gdeleted || sdeleted)
302 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
303 psf->sf_count[MCAST_INCLUDE])
305 return pmc->sfcount[MCAST_EXCLUDE] ==
306 psf->sf_count[MCAST_EXCLUDE];
307 case IGMPV3_ALLOW_NEW_SOURCES:
308 if (gdeleted || !psf->sf_crcount)
310 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
311 case IGMPV3_BLOCK_OLD_SOURCES:
312 if (pmc->sfmode == MCAST_INCLUDE)
313 return gdeleted || (psf->sf_crcount && sdeleted);
314 return psf->sf_crcount && !gdeleted && !sdeleted;
320 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
322 struct ip_sf_list *psf;
325 for (psf = pmc->sources; psf; psf = psf->sf_next) {
326 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
333 /* source address selection per RFC 3376 section 4.2.13 */
334 static __be32 igmpv3_get_srcaddr(struct net_device *dev,
335 const struct flowi4 *fl4)
337 struct in_device *in_dev = __in_dev_get_rcu(dev);
340 return htonl(INADDR_ANY);
343 if (fl4->saddr == ifa->ifa_local)
345 } endfor_ifa(in_dev);
347 return htonl(INADDR_ANY);
350 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
355 struct igmpv3_report *pig;
356 struct net *net = dev_net(dev);
358 int hlen = LL_RESERVED_SPACE(dev);
359 int tlen = dev->needed_tailroom;
360 unsigned int size = mtu;
363 skb = alloc_skb(size + hlen + tlen,
364 GFP_ATOMIC | __GFP_NOWARN);
371 skb->priority = TC_PRIO_CONTROL;
373 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
375 IPPROTO_IGMP, 0, dev->ifindex);
381 skb_dst_set(skb, &rt->dst);
384 skb_reserve(skb, hlen);
385 skb_tailroom_reserve(skb, mtu, tlen);
387 skb_reset_network_header(skb);
389 skb_put(skb, sizeof(struct iphdr) + 4);
392 pip->ihl = (sizeof(struct iphdr)+4)>>2;
394 pip->frag_off = htons(IP_DF);
396 pip->daddr = fl4.daddr;
399 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
402 pip->protocol = IPPROTO_IGMP;
403 pip->tot_len = 0; /* filled in later */
404 ip_select_ident(net, skb, NULL);
405 ((u8 *)&pip[1])[0] = IPOPT_RA;
406 ((u8 *)&pip[1])[1] = 4;
407 ((u8 *)&pip[1])[2] = 0;
408 ((u8 *)&pip[1])[3] = 0;
410 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
411 skb_put(skb, sizeof(*pig));
412 pig = igmpv3_report_hdr(skb);
413 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
421 static int igmpv3_sendpack(struct sk_buff *skb)
423 struct igmphdr *pig = igmp_hdr(skb);
424 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
426 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
428 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
431 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
433 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
436 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
437 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
439 struct net_device *dev = pmc->interface->dev;
440 struct igmpv3_report *pih;
441 struct igmpv3_grec *pgr;
444 skb = igmpv3_newpack(dev, mtu);
448 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
449 pgr->grec_type = type;
450 pgr->grec_auxwords = 0;
452 pgr->grec_mca = pmc->multiaddr;
453 pih = igmpv3_report_hdr(skb);
454 pih->ngrec = htons(ntohs(pih->ngrec)+1);
459 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
461 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
462 int type, int gdeleted, int sdeleted)
464 struct net_device *dev = pmc->interface->dev;
465 struct net *net = dev_net(dev);
466 struct igmpv3_report *pih;
467 struct igmpv3_grec *pgr = NULL;
468 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
469 int scount, stotal, first, isquery, truncate;
472 if (pmc->multiaddr == IGMP_ALL_HOSTS)
474 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
477 mtu = READ_ONCE(dev->mtu);
478 if (mtu < IPV4_MIN_MTU)
481 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
482 type == IGMPV3_MODE_IS_EXCLUDE;
483 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
484 type == IGMPV3_CHANGE_TO_EXCLUDE;
488 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
493 pih = skb ? igmpv3_report_hdr(skb) : NULL;
495 /* EX and TO_EX get a fresh packet, if needed */
497 if (pih && pih->ngrec &&
498 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
500 igmpv3_sendpack(skb);
501 skb = igmpv3_newpack(dev, mtu);
506 for (psf = *psf_list; psf; psf = psf_next) {
509 psf_next = psf->sf_next;
511 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
516 /* Based on RFC3376 5.1. Should not send source-list change
517 * records when there is a filter mode change.
519 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
520 (!gdeleted && pmc->crcount)) &&
521 (type == IGMPV3_ALLOW_NEW_SOURCES ||
522 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
523 goto decrease_sf_crcount;
525 /* clear marks on query responses */
529 if (AVAILABLE(skb) < sizeof(__be32) +
530 first*sizeof(struct igmpv3_grec)) {
531 if (truncate && !first)
532 break; /* truncate these */
534 pgr->grec_nsrcs = htons(scount);
536 igmpv3_sendpack(skb);
537 skb = igmpv3_newpack(dev, mtu);
542 skb = add_grhead(skb, pmc, type, &pgr, mtu);
547 psrc = skb_put(skb, sizeof(__be32));
548 *psrc = psf->sf_inaddr;
550 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
551 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
554 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
556 psf_prev->sf_next = psf->sf_next;
558 *psf_list = psf->sf_next;
568 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
569 type == IGMPV3_BLOCK_OLD_SOURCES)
571 if (pmc->crcount || isquery) {
572 /* make sure we have room for group header */
573 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
574 igmpv3_sendpack(skb);
575 skb = NULL; /* add_grhead will get a new one */
577 skb = add_grhead(skb, pmc, type, &pgr, mtu);
581 pgr->grec_nsrcs = htons(scount);
584 pmc->gsquery = 0; /* clear query state on report */
588 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
590 struct sk_buff *skb = NULL;
591 struct net *net = dev_net(in_dev->dev);
596 for_each_pmc_rcu(in_dev, pmc) {
597 if (pmc->multiaddr == IGMP_ALL_HOSTS)
599 if (ipv4_is_local_multicast(pmc->multiaddr) &&
600 !net->ipv4.sysctl_igmp_llm_reports)
602 spin_lock_bh(&pmc->lock);
603 if (pmc->sfcount[MCAST_EXCLUDE])
604 type = IGMPV3_MODE_IS_EXCLUDE;
606 type = IGMPV3_MODE_IS_INCLUDE;
607 skb = add_grec(skb, pmc, type, 0, 0);
608 spin_unlock_bh(&pmc->lock);
612 spin_lock_bh(&pmc->lock);
613 if (pmc->sfcount[MCAST_EXCLUDE])
614 type = IGMPV3_MODE_IS_EXCLUDE;
616 type = IGMPV3_MODE_IS_INCLUDE;
617 skb = add_grec(skb, pmc, type, 0, 0);
618 spin_unlock_bh(&pmc->lock);
622 return igmpv3_sendpack(skb);
626 * remove zero-count source records from a source filter list
628 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
630 struct ip_sf_list *psf_prev, *psf_next, *psf;
633 for (psf = *ppsf; psf; psf = psf_next) {
634 psf_next = psf->sf_next;
635 if (psf->sf_crcount == 0) {
637 psf_prev->sf_next = psf->sf_next;
639 *ppsf = psf->sf_next;
646 static void kfree_pmc(struct ip_mc_list *pmc)
648 ip_sf_list_clear_all(pmc->sources);
649 ip_sf_list_clear_all(pmc->tomb);
653 static void igmpv3_send_cr(struct in_device *in_dev)
655 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
656 struct sk_buff *skb = NULL;
660 spin_lock_bh(&in_dev->mc_tomb_lock);
664 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
665 pmc_next = pmc->next;
666 if (pmc->sfmode == MCAST_INCLUDE) {
667 type = IGMPV3_BLOCK_OLD_SOURCES;
668 dtype = IGMPV3_BLOCK_OLD_SOURCES;
669 skb = add_grec(skb, pmc, type, 1, 0);
670 skb = add_grec(skb, pmc, dtype, 1, 1);
673 if (pmc->sfmode == MCAST_EXCLUDE) {
674 type = IGMPV3_CHANGE_TO_INCLUDE;
675 skb = add_grec(skb, pmc, type, 1, 0);
678 if (pmc->crcount == 0) {
679 igmpv3_clear_zeros(&pmc->tomb);
680 igmpv3_clear_zeros(&pmc->sources);
683 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
685 pmc_prev->next = pmc_next;
687 in_dev->mc_tomb = pmc_next;
688 in_dev_put(pmc->interface);
693 spin_unlock_bh(&in_dev->mc_tomb_lock);
696 for_each_pmc_rcu(in_dev, pmc) {
697 spin_lock_bh(&pmc->lock);
698 if (pmc->sfcount[MCAST_EXCLUDE]) {
699 type = IGMPV3_BLOCK_OLD_SOURCES;
700 dtype = IGMPV3_ALLOW_NEW_SOURCES;
702 type = IGMPV3_ALLOW_NEW_SOURCES;
703 dtype = IGMPV3_BLOCK_OLD_SOURCES;
705 skb = add_grec(skb, pmc, type, 0, 0);
706 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
708 /* filter mode changes */
710 if (pmc->sfmode == MCAST_EXCLUDE)
711 type = IGMPV3_CHANGE_TO_EXCLUDE;
713 type = IGMPV3_CHANGE_TO_INCLUDE;
714 skb = add_grec(skb, pmc, type, 0, 0);
717 spin_unlock_bh(&pmc->lock);
723 (void) igmpv3_sendpack(skb);
726 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
733 struct net_device *dev = in_dev->dev;
734 struct net *net = dev_net(dev);
735 __be32 group = pmc ? pmc->multiaddr : 0;
740 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
741 return igmpv3_send_report(in_dev, pmc);
743 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
746 if (type == IGMP_HOST_LEAVE_MESSAGE)
747 dst = IGMP_ALL_ROUTER;
751 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
753 IPPROTO_IGMP, 0, dev->ifindex);
757 hlen = LL_RESERVED_SPACE(dev);
758 tlen = dev->needed_tailroom;
759 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
764 skb->priority = TC_PRIO_CONTROL;
766 skb_dst_set(skb, &rt->dst);
768 skb_reserve(skb, hlen);
770 skb_reset_network_header(skb);
772 skb_put(skb, sizeof(struct iphdr) + 4);
775 iph->ihl = (sizeof(struct iphdr)+4)>>2;
777 iph->frag_off = htons(IP_DF);
780 iph->saddr = fl4.saddr;
781 iph->protocol = IPPROTO_IGMP;
782 ip_select_ident(net, skb, NULL);
783 ((u8 *)&iph[1])[0] = IPOPT_RA;
784 ((u8 *)&iph[1])[1] = 4;
785 ((u8 *)&iph[1])[2] = 0;
786 ((u8 *)&iph[1])[3] = 0;
788 ih = skb_put(skb, sizeof(struct igmphdr));
793 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
795 return ip_local_out(net, skb->sk, skb);
798 static void igmp_gq_timer_expire(struct timer_list *t)
800 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
802 in_dev->mr_gq_running = 0;
803 igmpv3_send_report(in_dev, NULL);
807 static void igmp_ifc_timer_expire(struct timer_list *t)
809 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
812 igmpv3_send_cr(in_dev);
814 mr_ifc_count = READ_ONCE(in_dev->mr_ifc_count);
817 if (cmpxchg(&in_dev->mr_ifc_count,
819 mr_ifc_count - 1) != mr_ifc_count)
821 igmp_ifc_start_timer(in_dev,
822 unsolicited_report_interval(in_dev));
827 static void igmp_ifc_event(struct in_device *in_dev)
829 struct net *net = dev_net(in_dev->dev);
830 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
832 WRITE_ONCE(in_dev->mr_ifc_count, in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv);
833 igmp_ifc_start_timer(in_dev, 1);
837 static void igmp_timer_expire(struct timer_list *t)
839 struct ip_mc_list *im = from_timer(im, t, timer);
840 struct in_device *in_dev = im->interface;
842 spin_lock(&im->lock);
845 if (im->unsolicit_count && --im->unsolicit_count)
846 igmp_start_timer(im, unsolicited_report_interval(in_dev));
849 spin_unlock(&im->lock);
851 if (IGMP_V1_SEEN(in_dev))
852 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
853 else if (IGMP_V2_SEEN(in_dev))
854 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
856 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
861 /* mark EXCLUDE-mode sources */
862 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
864 struct ip_sf_list *psf;
868 for (psf = pmc->sources; psf; psf = psf->sf_next) {
871 for (i = 0; i < nsrcs; i++) {
872 /* skip inactive filters */
873 if (psf->sf_count[MCAST_INCLUDE] ||
874 pmc->sfcount[MCAST_EXCLUDE] !=
875 psf->sf_count[MCAST_EXCLUDE])
877 if (srcs[i] == psf->sf_inaddr) {
884 if (scount == nsrcs) /* all sources excluded */
889 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
891 struct ip_sf_list *psf;
894 if (pmc->sfmode == MCAST_EXCLUDE)
895 return igmp_xmarksources(pmc, nsrcs, srcs);
897 /* mark INCLUDE-mode sources */
899 for (psf = pmc->sources; psf; psf = psf->sf_next) {
902 for (i = 0; i < nsrcs; i++)
903 if (srcs[i] == psf->sf_inaddr) {
917 /* return true if packet was dropped */
918 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
920 struct ip_mc_list *im;
921 struct net *net = dev_net(in_dev->dev);
923 /* Timers are only set for non-local groups */
925 if (group == IGMP_ALL_HOSTS)
927 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
931 for_each_pmc_rcu(in_dev, im) {
932 if (im->multiaddr == group) {
941 /* return true if packet was dropped */
942 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
945 struct igmphdr *ih = igmp_hdr(skb);
946 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
947 struct ip_mc_list *im;
948 __be32 group = ih->group;
951 struct net *net = dev_net(in_dev->dev);
956 /* Alas, old v1 router presents here. */
958 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
959 in_dev->mr_v1_seen = jiffies +
960 (in_dev->mr_qrv * in_dev->mr_qi) +
964 /* v2 router present */
965 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
966 in_dev->mr_v2_seen = jiffies +
967 (in_dev->mr_qrv * in_dev->mr_qi) +
970 /* cancel the interface change timer */
971 WRITE_ONCE(in_dev->mr_ifc_count, 0);
972 if (del_timer(&in_dev->mr_ifc_timer))
973 __in_dev_put(in_dev);
974 /* clear deleted report items */
975 igmpv3_clear_delrec(in_dev);
976 } else if (len < 12) {
977 return true; /* ignore bogus packet; freed by caller */
978 } else if (IGMP_V1_SEEN(in_dev)) {
979 /* This is a v3 query with v1 queriers present */
980 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
982 } else if (IGMP_V2_SEEN(in_dev)) {
983 /* this is a v3 query with v2 queriers present;
984 * Interpretation of the max_delay code is problematic here.
985 * A real v2 host would use ih_code directly, while v3 has a
986 * different encoding. We use the v3 encoding as more likely
987 * to be intended in a v3 query.
989 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
991 max_delay = 1; /* can't mod w/ 0 */
993 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
996 ih3 = igmpv3_query_hdr(skb);
998 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
999 + ntohs(ih3->nsrcs)*sizeof(__be32)))
1001 ih3 = igmpv3_query_hdr(skb);
1004 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
1006 max_delay = 1; /* can't mod w/ 0 */
1007 in_dev->mr_maxdelay = max_delay;
1009 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
1010 * received value was zero, use the default or statically
1013 in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
1014 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
1016 /* RFC3376, 8.3. Query Response Interval:
1017 * The number of seconds represented by the [Query Response
1018 * Interval] must be less than the [Query Interval].
1020 if (in_dev->mr_qri >= in_dev->mr_qi)
1021 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
1023 if (!group) { /* general query */
1025 return true; /* no sources allowed */
1026 igmp_gq_start_timer(in_dev);
1029 /* mark sources to include, if group & source-specific */
1030 mark = ih3->nsrcs != 0;
1034 * - Start the timers in all of our membership records
1035 * that the query applies to for the interface on
1036 * which the query arrived excl. those that belong
1037 * to a "local" group (224.0.0.X)
1038 * - For timers already running check if they need to
1040 * - Use the igmp->igmp_code field as the maximum
1044 for_each_pmc_rcu(in_dev, im) {
1047 if (group && group != im->multiaddr)
1049 if (im->multiaddr == IGMP_ALL_HOSTS)
1051 if (ipv4_is_local_multicast(im->multiaddr) &&
1052 !net->ipv4.sysctl_igmp_llm_reports)
1054 spin_lock_bh(&im->lock);
1056 im->gsquery = im->gsquery && mark;
1059 changed = !im->gsquery ||
1060 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1061 spin_unlock_bh(&im->lock);
1063 igmp_mod_timer(im, max_delay);
1069 /* called in rcu_read_lock() section */
1070 int igmp_rcv(struct sk_buff *skb)
1072 /* This basically follows the spec line by line -- see RFC1112 */
1074 struct net_device *dev = skb->dev;
1075 struct in_device *in_dev;
1077 bool dropped = true;
1079 if (netif_is_l3_master(dev)) {
1080 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1085 in_dev = __in_dev_get_rcu(dev);
1089 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1092 if (skb_checksum_simple_validate(skb))
1097 case IGMP_HOST_MEMBERSHIP_QUERY:
1098 dropped = igmp_heard_query(in_dev, skb, len);
1100 case IGMP_HOST_MEMBERSHIP_REPORT:
1101 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1102 /* Is it our report looped back? */
1103 if (rt_is_output_route(skb_rtable(skb)))
1105 /* don't rely on MC router hearing unicast reports */
1106 if (skb->pkt_type == PACKET_MULTICAST ||
1107 skb->pkt_type == PACKET_BROADCAST)
1108 dropped = igmp_heard_report(in_dev, ih->group);
1111 #ifdef CONFIG_IP_PIMSM_V1
1112 return pim_rcv_v1(skb);
1114 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1117 case IGMP_HOST_LEAVE_MESSAGE:
1119 case IGMP_MTRACE_RESP:
1137 * Add a filter to a device
1140 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1142 char buf[MAX_ADDR_LEN];
1143 struct net_device *dev = in_dev->dev;
1145 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1146 We will get multicast token leakage, when IFF_MULTICAST
1147 is changed. This check should be done in ndo_set_rx_mode
1148 routine. Something sort of:
1149 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1152 if (arp_mc_map(addr, buf, dev, 0) == 0)
1153 dev_mc_add(dev, buf);
1157 * Remove a filter from a device
1160 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1162 char buf[MAX_ADDR_LEN];
1163 struct net_device *dev = in_dev->dev;
1165 if (arp_mc_map(addr, buf, dev, 0) == 0)
1166 dev_mc_del(dev, buf);
1169 #ifdef CONFIG_IP_MULTICAST
1171 * deleted ip_mc_list manipulation
1173 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1175 struct ip_mc_list *pmc;
1176 struct net *net = dev_net(in_dev->dev);
1178 /* this is an "ip_mc_list" for convenience; only the fields below
1179 * are actually used. In particular, the refcnt and users are not
1180 * used for management of the delete list. Using the same structure
1181 * for deleted items allows change reports to use common code with
1182 * non-deleted or query-response MCA's.
1184 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1187 spin_lock_init(&pmc->lock);
1188 spin_lock_bh(&im->lock);
1189 pmc->interface = im->interface;
1190 in_dev_hold(in_dev);
1191 pmc->multiaddr = im->multiaddr;
1192 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1193 pmc->sfmode = im->sfmode;
1194 if (pmc->sfmode == MCAST_INCLUDE) {
1195 struct ip_sf_list *psf;
1197 pmc->tomb = im->tomb;
1198 pmc->sources = im->sources;
1199 im->tomb = im->sources = NULL;
1200 for (psf = pmc->sources; psf; psf = psf->sf_next)
1201 psf->sf_crcount = pmc->crcount;
1203 spin_unlock_bh(&im->lock);
1205 spin_lock_bh(&in_dev->mc_tomb_lock);
1206 pmc->next = in_dev->mc_tomb;
1207 in_dev->mc_tomb = pmc;
1208 spin_unlock_bh(&in_dev->mc_tomb_lock);
1212 * restore ip_mc_list deleted records
1214 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1216 struct ip_mc_list *pmc, *pmc_prev;
1217 struct ip_sf_list *psf;
1218 struct net *net = dev_net(in_dev->dev);
1219 __be32 multiaddr = im->multiaddr;
1221 spin_lock_bh(&in_dev->mc_tomb_lock);
1223 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1224 if (pmc->multiaddr == multiaddr)
1230 pmc_prev->next = pmc->next;
1232 in_dev->mc_tomb = pmc->next;
1234 spin_unlock_bh(&in_dev->mc_tomb_lock);
1236 spin_lock_bh(&im->lock);
1238 im->interface = pmc->interface;
1239 if (im->sfmode == MCAST_INCLUDE) {
1240 swap(im->tomb, pmc->tomb);
1241 swap(im->sources, pmc->sources);
1242 for (psf = im->sources; psf; psf = psf->sf_next)
1243 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1245 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1247 in_dev_put(pmc->interface);
1250 spin_unlock_bh(&im->lock);
1254 * flush ip_mc_list deleted records
1256 static void igmpv3_clear_delrec(struct in_device *in_dev)
1258 struct ip_mc_list *pmc, *nextpmc;
1260 spin_lock_bh(&in_dev->mc_tomb_lock);
1261 pmc = in_dev->mc_tomb;
1262 in_dev->mc_tomb = NULL;
1263 spin_unlock_bh(&in_dev->mc_tomb_lock);
1265 for (; pmc; pmc = nextpmc) {
1266 nextpmc = pmc->next;
1267 ip_mc_clear_src(pmc);
1268 in_dev_put(pmc->interface);
1271 /* clear dead sources, too */
1273 for_each_pmc_rcu(in_dev, pmc) {
1274 struct ip_sf_list *psf;
1276 spin_lock_bh(&pmc->lock);
1279 spin_unlock_bh(&pmc->lock);
1280 ip_sf_list_clear_all(psf);
1286 static void igmp_group_dropped(struct ip_mc_list *im)
1288 struct in_device *in_dev = im->interface;
1289 #ifdef CONFIG_IP_MULTICAST
1290 struct net *net = dev_net(in_dev->dev);
1296 ip_mc_filter_del(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)
1305 reporter = im->reporter;
1306 igmp_stop_timer(im);
1308 if (!in_dev->dead) {
1309 if (IGMP_V1_SEEN(in_dev))
1311 if (IGMP_V2_SEEN(in_dev)) {
1313 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1317 igmpv3_add_delrec(in_dev, im);
1319 igmp_ifc_event(in_dev);
1324 static void igmp_group_added(struct ip_mc_list *im)
1326 struct in_device *in_dev = im->interface;
1327 #ifdef CONFIG_IP_MULTICAST
1328 struct net *net = dev_net(in_dev->dev);
1331 if (im->loaded == 0) {
1333 ip_mc_filter_add(in_dev, im->multiaddr);
1336 #ifdef CONFIG_IP_MULTICAST
1337 if (im->multiaddr == IGMP_ALL_HOSTS)
1339 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1345 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1346 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1347 spin_lock_bh(&im->lock);
1348 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1349 spin_unlock_bh(&im->lock);
1354 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1355 * not send filter-mode change record as the mode should be from
1358 if (im->sfmode == MCAST_EXCLUDE)
1359 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1361 igmp_ifc_event(in_dev);
1367 * Multicast list managers
1370 static u32 ip_mc_hash(const struct ip_mc_list *im)
1372 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1375 static void ip_mc_hash_add(struct in_device *in_dev,
1376 struct ip_mc_list *im)
1378 struct ip_mc_list __rcu **mc_hash;
1381 mc_hash = rtnl_dereference(in_dev->mc_hash);
1383 hash = ip_mc_hash(im);
1384 im->next_hash = mc_hash[hash];
1385 rcu_assign_pointer(mc_hash[hash], im);
1389 /* do not use a hash table for small number of items */
1390 if (in_dev->mc_count < 4)
1393 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1398 for_each_pmc_rtnl(in_dev, im) {
1399 hash = ip_mc_hash(im);
1400 im->next_hash = mc_hash[hash];
1401 RCU_INIT_POINTER(mc_hash[hash], im);
1404 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1407 static void ip_mc_hash_remove(struct in_device *in_dev,
1408 struct ip_mc_list *im)
1410 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1411 struct ip_mc_list *aux;
1415 mc_hash += ip_mc_hash(im);
1416 while ((aux = rtnl_dereference(*mc_hash)) != im)
1417 mc_hash = &aux->next_hash;
1418 *mc_hash = im->next_hash;
1423 * A socket has joined a multicast group on device dev.
1425 static void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1428 struct ip_mc_list *im;
1432 for_each_pmc_rtnl(in_dev, im) {
1433 if (im->multiaddr == addr) {
1435 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
1440 im = kzalloc(sizeof(*im), GFP_KERNEL);
1445 im->interface = in_dev;
1446 in_dev_hold(in_dev);
1447 im->multiaddr = addr;
1448 /* initial mode is (EX, empty) */
1450 im->sfcount[mode] = 1;
1451 refcount_set(&im->refcnt, 1);
1452 spin_lock_init(&im->lock);
1453 #ifdef CONFIG_IP_MULTICAST
1454 timer_setup(&im->timer, igmp_timer_expire, 0);
1457 im->next_rcu = in_dev->mc_list;
1459 rcu_assign_pointer(in_dev->mc_list, im);
1461 ip_mc_hash_add(in_dev, im);
1463 #ifdef CONFIG_IP_MULTICAST
1464 igmpv3_del_delrec(in_dev, im);
1466 igmp_group_added(im);
1468 ip_rt_multicast_event(in_dev);
1473 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1475 __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE);
1477 EXPORT_SYMBOL(ip_mc_inc_group);
1479 static int ip_mc_check_iphdr(struct sk_buff *skb)
1481 const struct iphdr *iph;
1483 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1485 if (!pskb_may_pull(skb, offset))
1490 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1493 offset += ip_hdrlen(skb) - sizeof(*iph);
1495 if (!pskb_may_pull(skb, offset))
1500 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1503 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1504 if (skb->len < len || len < offset)
1507 skb_set_transport_header(skb, offset);
1512 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1514 unsigned int len = skb_transport_offset(skb);
1516 len += sizeof(struct igmpv3_report);
1518 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1521 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1523 unsigned int len = skb_transport_offset(skb);
1525 len += sizeof(struct igmphdr);
1530 if (skb->len != len) {
1532 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1533 if (skb->len < len || !pskb_may_pull(skb, len))
1537 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1538 * all-systems destination addresses (224.0.0.1) for general queries
1540 if (!igmp_hdr(skb)->group &&
1541 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1547 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1549 switch (igmp_hdr(skb)->type) {
1550 case IGMP_HOST_LEAVE_MESSAGE:
1551 case IGMP_HOST_MEMBERSHIP_REPORT:
1552 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1555 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1556 return ip_mc_check_igmp_reportv3(skb);
1557 case IGMP_HOST_MEMBERSHIP_QUERY:
1558 return ip_mc_check_igmp_query(skb);
1564 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1566 return skb_checksum_simple_validate(skb);
1569 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1572 struct sk_buff *skb_chk;
1573 unsigned int transport_len;
1574 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1577 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1579 skb_chk = skb_checksum_trimmed(skb, transport_len,
1580 ip_mc_validate_checksum);
1584 if (!pskb_may_pull(skb_chk, len))
1587 ret = ip_mc_check_igmp_msg(skb_chk);
1592 *skb_trimmed = skb_chk;
1593 /* free now unneeded clone */
1594 else if (skb_chk != skb)
1600 if (ret && skb_chk && skb_chk != skb)
1607 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1608 * @skb: the skb to validate
1609 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1611 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1612 * skb transport header accordingly and returns zero.
1614 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1616 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1617 * -ENOMEM: A memory allocation failure happened.
1619 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1620 * to NULL): After parsing an IGMP packet successfully it will point to
1621 * an skb which has its tail aligned to the IP packet end. This might
1622 * either be the originally provided skb or a trimmed, cloned version if
1623 * the skb frame had data beyond the IP packet. A cloned skb allows us
1624 * to leave the original skb and its full frame unchanged (which might be
1625 * desirable for layer 2 frame jugglers).
1627 * Caller needs to set the skb network header and free any returned skb if it
1628 * differs from the provided skb.
1630 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1632 int ret = ip_mc_check_iphdr(skb);
1637 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1640 return __ip_mc_check_igmp(skb, skb_trimmed);
1642 EXPORT_SYMBOL(ip_mc_check_igmp);
1645 * Resend IGMP JOIN report; used by netdev notifier.
1647 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1649 #ifdef CONFIG_IP_MULTICAST
1650 struct ip_mc_list *im;
1652 struct net *net = dev_net(in_dev->dev);
1656 for_each_pmc_rtnl(in_dev, im) {
1657 if (im->multiaddr == IGMP_ALL_HOSTS)
1659 if (ipv4_is_local_multicast(im->multiaddr) &&
1660 !net->ipv4.sysctl_igmp_llm_reports)
1663 /* a failover is happening and switches
1664 * must be notified immediately
1666 if (IGMP_V1_SEEN(in_dev))
1667 type = IGMP_HOST_MEMBERSHIP_REPORT;
1668 else if (IGMP_V2_SEEN(in_dev))
1669 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1671 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1672 igmp_send_report(in_dev, im, type);
1678 * A socket has left a multicast group on device dev
1681 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1683 struct ip_mc_list *i;
1684 struct ip_mc_list __rcu **ip;
1688 for (ip = &in_dev->mc_list;
1689 (i = rtnl_dereference(*ip)) != NULL;
1690 ip = &i->next_rcu) {
1691 if (i->multiaddr == addr) {
1692 if (--i->users == 0) {
1693 ip_mc_hash_remove(in_dev, i);
1696 igmp_group_dropped(i);
1700 ip_rt_multicast_event(in_dev);
1709 EXPORT_SYMBOL(ip_mc_dec_group);
1711 /* Device changing type */
1713 void ip_mc_unmap(struct in_device *in_dev)
1715 struct ip_mc_list *pmc;
1719 for_each_pmc_rtnl(in_dev, pmc)
1720 igmp_group_dropped(pmc);
1723 void ip_mc_remap(struct in_device *in_dev)
1725 struct ip_mc_list *pmc;
1729 for_each_pmc_rtnl(in_dev, pmc) {
1730 #ifdef CONFIG_IP_MULTICAST
1731 igmpv3_del_delrec(in_dev, pmc);
1733 igmp_group_added(pmc);
1737 /* Device going down */
1739 void ip_mc_down(struct in_device *in_dev)
1741 struct ip_mc_list *pmc;
1745 for_each_pmc_rtnl(in_dev, pmc)
1746 igmp_group_dropped(pmc);
1748 #ifdef CONFIG_IP_MULTICAST
1749 WRITE_ONCE(in_dev->mr_ifc_count, 0);
1750 if (del_timer(&in_dev->mr_ifc_timer))
1751 __in_dev_put(in_dev);
1752 in_dev->mr_gq_running = 0;
1753 if (del_timer(&in_dev->mr_gq_timer))
1754 __in_dev_put(in_dev);
1757 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1760 #ifdef CONFIG_IP_MULTICAST
1761 static void ip_mc_reset(struct in_device *in_dev)
1763 struct net *net = dev_net(in_dev->dev);
1765 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1766 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1767 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1770 static void ip_mc_reset(struct in_device *in_dev)
1775 void ip_mc_init_dev(struct in_device *in_dev)
1779 #ifdef CONFIG_IP_MULTICAST
1780 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1781 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
1783 ip_mc_reset(in_dev);
1785 spin_lock_init(&in_dev->mc_tomb_lock);
1788 /* Device going up */
1790 void ip_mc_up(struct in_device *in_dev)
1792 struct ip_mc_list *pmc;
1796 ip_mc_reset(in_dev);
1797 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1799 for_each_pmc_rtnl(in_dev, pmc) {
1800 #ifdef CONFIG_IP_MULTICAST
1801 igmpv3_del_delrec(in_dev, pmc);
1803 igmp_group_added(pmc);
1808 * Device is about to be destroyed: clean up.
1811 void ip_mc_destroy_dev(struct in_device *in_dev)
1813 struct ip_mc_list *i;
1817 /* Deactivate timers */
1819 #ifdef CONFIG_IP_MULTICAST
1820 igmpv3_clear_delrec(in_dev);
1823 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1824 in_dev->mc_list = i->next_rcu;
1831 /* RTNL is locked */
1832 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1834 struct net_device *dev = NULL;
1835 struct in_device *idev = NULL;
1837 if (imr->imr_ifindex) {
1838 idev = inetdev_by_index(net, imr->imr_ifindex);
1841 if (imr->imr_address.s_addr) {
1842 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1848 struct rtable *rt = ip_route_output(net,
1849 imr->imr_multiaddr.s_addr,
1857 imr->imr_ifindex = dev->ifindex;
1858 idev = __in_dev_get_rtnl(dev);
1864 * Join a socket to a group
1867 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1870 struct ip_sf_list *psf, *psf_prev;
1874 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1875 if (psf->sf_inaddr == *psfsrc)
1879 if (!psf || psf->sf_count[sfmode] == 0) {
1880 /* source filter not found, or count wrong => bug */
1883 psf->sf_count[sfmode]--;
1884 if (psf->sf_count[sfmode] == 0) {
1885 ip_rt_multicast_event(pmc->interface);
1887 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1888 #ifdef CONFIG_IP_MULTICAST
1889 struct in_device *in_dev = pmc->interface;
1890 struct net *net = dev_net(in_dev->dev);
1893 /* no more filters for this source */
1895 psf_prev->sf_next = psf->sf_next;
1897 pmc->sources = psf->sf_next;
1898 #ifdef CONFIG_IP_MULTICAST
1899 if (psf->sf_oldin &&
1900 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1901 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1902 psf->sf_next = pmc->tomb;
1912 #ifndef CONFIG_IP_MULTICAST
1913 #define igmp_ifc_event(x) do { } while (0)
1916 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1917 int sfcount, __be32 *psfsrc, int delta)
1919 struct ip_mc_list *pmc;
1926 for_each_pmc_rcu(in_dev, pmc) {
1927 if (*pmca == pmc->multiaddr)
1931 /* MCA not found?? bug */
1935 spin_lock_bh(&pmc->lock);
1937 #ifdef CONFIG_IP_MULTICAST
1942 if (!pmc->sfcount[sfmode])
1944 pmc->sfcount[sfmode]--;
1947 for (i = 0; i < sfcount; i++) {
1948 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1950 changerec |= rv > 0;
1954 if (pmc->sfmode == MCAST_EXCLUDE &&
1955 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1956 pmc->sfcount[MCAST_INCLUDE]) {
1957 #ifdef CONFIG_IP_MULTICAST
1958 struct ip_sf_list *psf;
1959 struct net *net = dev_net(in_dev->dev);
1962 /* filter mode change */
1963 pmc->sfmode = MCAST_INCLUDE;
1964 #ifdef CONFIG_IP_MULTICAST
1965 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1966 WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
1967 for (psf = pmc->sources; psf; psf = psf->sf_next)
1968 psf->sf_crcount = 0;
1969 igmp_ifc_event(pmc->interface);
1970 } else if (sf_setstate(pmc) || changerec) {
1971 igmp_ifc_event(pmc->interface);
1975 spin_unlock_bh(&pmc->lock);
1980 * Add multicast single-source filter to the interface list
1982 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1985 struct ip_sf_list *psf, *psf_prev;
1988 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1989 if (psf->sf_inaddr == *psfsrc)
1994 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1997 psf->sf_inaddr = *psfsrc;
1999 psf_prev->sf_next = psf;
2003 psf->sf_count[sfmode]++;
2004 if (psf->sf_count[sfmode] == 1) {
2005 ip_rt_multicast_event(pmc->interface);
2010 #ifdef CONFIG_IP_MULTICAST
2011 static void sf_markstate(struct ip_mc_list *pmc)
2013 struct ip_sf_list *psf;
2014 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2016 for (psf = pmc->sources; psf; psf = psf->sf_next)
2017 if (pmc->sfcount[MCAST_EXCLUDE]) {
2018 psf->sf_oldin = mca_xcount ==
2019 psf->sf_count[MCAST_EXCLUDE] &&
2020 !psf->sf_count[MCAST_INCLUDE];
2022 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2025 static int sf_setstate(struct ip_mc_list *pmc)
2027 struct ip_sf_list *psf, *dpsf;
2028 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2029 int qrv = pmc->interface->mr_qrv;
2033 for (psf = pmc->sources; psf; psf = psf->sf_next) {
2034 if (pmc->sfcount[MCAST_EXCLUDE]) {
2035 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2036 !psf->sf_count[MCAST_INCLUDE];
2038 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2040 if (!psf->sf_oldin) {
2041 struct ip_sf_list *prev = NULL;
2043 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2044 if (dpsf->sf_inaddr == psf->sf_inaddr)
2050 prev->sf_next = dpsf->sf_next;
2052 pmc->tomb = dpsf->sf_next;
2055 psf->sf_crcount = qrv;
2058 } else if (psf->sf_oldin) {
2060 psf->sf_crcount = 0;
2062 * add or update "delete" records if an active filter
2065 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2066 if (dpsf->sf_inaddr == psf->sf_inaddr)
2069 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2073 /* pmc->lock held by callers */
2074 dpsf->sf_next = pmc->tomb;
2077 dpsf->sf_crcount = qrv;
2086 * Add multicast source filter list to the interface list
2088 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2089 int sfcount, __be32 *psfsrc, int delta)
2091 struct ip_mc_list *pmc;
2098 for_each_pmc_rcu(in_dev, pmc) {
2099 if (*pmca == pmc->multiaddr)
2103 /* MCA not found?? bug */
2107 spin_lock_bh(&pmc->lock);
2110 #ifdef CONFIG_IP_MULTICAST
2113 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2115 pmc->sfcount[sfmode]++;
2117 for (i = 0; i < sfcount; i++) {
2118 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2126 pmc->sfcount[sfmode]--;
2127 for (j = 0; j < i; j++)
2128 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2129 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2130 #ifdef CONFIG_IP_MULTICAST
2131 struct ip_sf_list *psf;
2132 struct net *net = dev_net(pmc->interface->dev);
2133 in_dev = pmc->interface;
2136 /* filter mode change */
2137 if (pmc->sfcount[MCAST_EXCLUDE])
2138 pmc->sfmode = MCAST_EXCLUDE;
2139 else if (pmc->sfcount[MCAST_INCLUDE])
2140 pmc->sfmode = MCAST_INCLUDE;
2141 #ifdef CONFIG_IP_MULTICAST
2142 /* else no filters; keep old mode for reports */
2144 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2145 WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
2146 for (psf = pmc->sources; psf; psf = psf->sf_next)
2147 psf->sf_crcount = 0;
2148 igmp_ifc_event(in_dev);
2149 } else if (sf_setstate(pmc)) {
2150 igmp_ifc_event(in_dev);
2153 spin_unlock_bh(&pmc->lock);
2157 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2159 struct ip_sf_list *tomb, *sources;
2161 spin_lock_bh(&pmc->lock);
2164 sources = pmc->sources;
2165 pmc->sources = NULL;
2166 pmc->sfmode = MCAST_EXCLUDE;
2167 pmc->sfcount[MCAST_INCLUDE] = 0;
2168 pmc->sfcount[MCAST_EXCLUDE] = 1;
2169 spin_unlock_bh(&pmc->lock);
2171 ip_sf_list_clear_all(tomb);
2172 ip_sf_list_clear_all(sources);
2175 /* Join a multicast group
2177 static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2180 __be32 addr = imr->imr_multiaddr.s_addr;
2181 struct ip_mc_socklist *iml, *i;
2182 struct in_device *in_dev;
2183 struct inet_sock *inet = inet_sk(sk);
2184 struct net *net = sock_net(sk);
2191 if (!ipv4_is_multicast(addr))
2194 in_dev = ip_mc_find_dev(net, imr);
2202 ifindex = imr->imr_ifindex;
2203 for_each_pmc_rtnl(inet, i) {
2204 if (i->multi.imr_multiaddr.s_addr == addr &&
2205 i->multi.imr_ifindex == ifindex)
2210 if (count >= net->ipv4.sysctl_igmp_max_memberships)
2212 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2216 memcpy(&iml->multi, imr, sizeof(*imr));
2217 iml->next_rcu = inet->mc_list;
2220 rcu_assign_pointer(inet->mc_list, iml);
2221 __ip_mc_inc_group(in_dev, addr, mode);
2227 /* Join ASM (Any-Source Multicast) group
2229 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2231 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2233 EXPORT_SYMBOL(ip_mc_join_group);
2235 /* Join SSM (Source-Specific Multicast) group
2237 int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2240 return __ip_mc_join_group(sk, imr, mode);
2243 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2244 struct in_device *in_dev)
2246 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2250 /* any-source empty exclude case */
2251 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2252 iml->sfmode, 0, NULL, 0);
2254 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2255 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2256 RCU_INIT_POINTER(iml->sflist, NULL);
2257 /* decrease mem now to avoid the memleak warning */
2258 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2259 kfree_rcu(psf, rcu);
2263 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2265 struct inet_sock *inet = inet_sk(sk);
2266 struct ip_mc_socklist *iml;
2267 struct ip_mc_socklist __rcu **imlp;
2268 struct in_device *in_dev;
2269 struct net *net = sock_net(sk);
2270 __be32 group = imr->imr_multiaddr.s_addr;
2272 int ret = -EADDRNOTAVAIL;
2276 in_dev = ip_mc_find_dev(net, imr);
2277 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2281 ifindex = imr->imr_ifindex;
2282 for (imlp = &inet->mc_list;
2283 (iml = rtnl_dereference(*imlp)) != NULL;
2284 imlp = &iml->next_rcu) {
2285 if (iml->multi.imr_multiaddr.s_addr != group)
2288 if (iml->multi.imr_ifindex != ifindex)
2290 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2291 iml->multi.imr_address.s_addr)
2294 (void) ip_mc_leave_src(sk, iml, in_dev);
2296 *imlp = iml->next_rcu;
2299 ip_mc_dec_group(in_dev, group);
2301 /* decrease mem now to avoid the memleak warning */
2302 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2303 kfree_rcu(iml, rcu);
2309 EXPORT_SYMBOL(ip_mc_leave_group);
2311 int ip_mc_source(int add, int omode, struct sock *sk, struct
2312 ip_mreq_source *mreqs, int ifindex)
2315 struct ip_mreqn imr;
2316 __be32 addr = mreqs->imr_multiaddr;
2317 struct ip_mc_socklist *pmc;
2318 struct in_device *in_dev = NULL;
2319 struct inet_sock *inet = inet_sk(sk);
2320 struct ip_sf_socklist *psl;
2321 struct net *net = sock_net(sk);
2325 if (!ipv4_is_multicast(addr))
2330 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2331 imr.imr_address.s_addr = mreqs->imr_interface;
2332 imr.imr_ifindex = ifindex;
2333 in_dev = ip_mc_find_dev(net, &imr);
2339 err = -EADDRNOTAVAIL;
2341 for_each_pmc_rtnl(inet, pmc) {
2342 if ((pmc->multi.imr_multiaddr.s_addr ==
2343 imr.imr_multiaddr.s_addr) &&
2344 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2347 if (!pmc) { /* must have a prior join */
2351 /* if a source filter was set, must be the same mode as before */
2353 if (pmc->sfmode != omode) {
2357 } else if (pmc->sfmode != omode) {
2358 /* allow mode switches for empty-set filters */
2359 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2360 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2362 pmc->sfmode = omode;
2365 psl = rtnl_dereference(pmc->sflist);
2368 goto done; /* err = -EADDRNOTAVAIL */
2370 for (i = 0; i < psl->sl_count; i++) {
2371 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2376 if (rv) /* source not found */
2377 goto done; /* err = -EADDRNOTAVAIL */
2379 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2380 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2385 /* update the interface filter */
2386 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2387 &mreqs->imr_sourceaddr, 1);
2389 for (j = i+1; j < psl->sl_count; j++)
2390 psl->sl_addr[j-1] = psl->sl_addr[j];
2395 /* else, add a new source to the filter */
2397 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2401 if (!psl || psl->sl_count == psl->sl_max) {
2402 struct ip_sf_socklist *newpsl;
2403 int count = IP_SFBLOCK;
2406 count += psl->sl_max;
2407 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2412 newpsl->sl_max = count;
2413 newpsl->sl_count = count - IP_SFBLOCK;
2415 for (i = 0; i < psl->sl_count; i++)
2416 newpsl->sl_addr[i] = psl->sl_addr[i];
2417 /* decrease mem now to avoid the memleak warning */
2418 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2419 kfree_rcu(psl, rcu);
2421 rcu_assign_pointer(pmc->sflist, newpsl);
2424 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2425 for (i = 0; i < psl->sl_count; i++) {
2426 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2431 if (rv == 0) /* address already there is an error */
2433 for (j = psl->sl_count-1; j >= i; j--)
2434 psl->sl_addr[j+1] = psl->sl_addr[j];
2435 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2438 /* update the interface list */
2439 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2440 &mreqs->imr_sourceaddr, 1);
2443 err = ip_mc_leave_group(sk, &imr);
2447 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2450 struct ip_mreqn imr;
2451 __be32 addr = msf->imsf_multiaddr;
2452 struct ip_mc_socklist *pmc;
2453 struct in_device *in_dev;
2454 struct inet_sock *inet = inet_sk(sk);
2455 struct ip_sf_socklist *newpsl, *psl;
2456 struct net *net = sock_net(sk);
2459 if (!ipv4_is_multicast(addr))
2461 if (msf->imsf_fmode != MCAST_INCLUDE &&
2462 msf->imsf_fmode != MCAST_EXCLUDE)
2467 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2468 imr.imr_address.s_addr = msf->imsf_interface;
2469 imr.imr_ifindex = ifindex;
2470 in_dev = ip_mc_find_dev(net, &imr);
2477 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2478 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2483 for_each_pmc_rtnl(inet, pmc) {
2484 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2485 pmc->multi.imr_ifindex == imr.imr_ifindex)
2488 if (!pmc) { /* must have a prior join */
2492 if (msf->imsf_numsrc) {
2493 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2499 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2500 memcpy(newpsl->sl_addr, msf->imsf_slist,
2501 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2502 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2503 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2505 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2510 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2511 msf->imsf_fmode, 0, NULL, 0);
2513 psl = rtnl_dereference(pmc->sflist);
2515 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2516 psl->sl_count, psl->sl_addr, 0);
2517 /* decrease mem now to avoid the memleak warning */
2518 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2519 kfree_rcu(psl, rcu);
2521 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2523 rcu_assign_pointer(pmc->sflist, newpsl);
2524 pmc->sfmode = msf->imsf_fmode;
2528 err = ip_mc_leave_group(sk, &imr);
2532 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2533 struct ip_msfilter __user *optval, int __user *optlen)
2535 int err, len, count, copycount;
2536 struct ip_mreqn imr;
2537 __be32 addr = msf->imsf_multiaddr;
2538 struct ip_mc_socklist *pmc;
2539 struct in_device *in_dev;
2540 struct inet_sock *inet = inet_sk(sk);
2541 struct ip_sf_socklist *psl;
2542 struct net *net = sock_net(sk);
2546 if (!ipv4_is_multicast(addr))
2549 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2550 imr.imr_address.s_addr = msf->imsf_interface;
2551 imr.imr_ifindex = 0;
2552 in_dev = ip_mc_find_dev(net, &imr);
2558 err = -EADDRNOTAVAIL;
2560 for_each_pmc_rtnl(inet, pmc) {
2561 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2562 pmc->multi.imr_ifindex == imr.imr_ifindex)
2565 if (!pmc) /* must have a prior join */
2567 msf->imsf_fmode = pmc->sfmode;
2568 psl = rtnl_dereference(pmc->sflist);
2573 count = psl->sl_count;
2575 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2576 len = copycount * sizeof(psl->sl_addr[0]);
2577 msf->imsf_numsrc = count;
2578 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2579 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2583 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2590 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2591 struct group_filter __user *optval, int __user *optlen)
2593 int err, i, count, copycount;
2594 struct sockaddr_in *psin;
2596 struct ip_mc_socklist *pmc;
2597 struct inet_sock *inet = inet_sk(sk);
2598 struct ip_sf_socklist *psl;
2602 psin = (struct sockaddr_in *)&gsf->gf_group;
2603 if (psin->sin_family != AF_INET)
2605 addr = psin->sin_addr.s_addr;
2606 if (!ipv4_is_multicast(addr))
2609 err = -EADDRNOTAVAIL;
2611 for_each_pmc_rtnl(inet, pmc) {
2612 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2613 pmc->multi.imr_ifindex == gsf->gf_interface)
2616 if (!pmc) /* must have a prior join */
2618 gsf->gf_fmode = pmc->sfmode;
2619 psl = rtnl_dereference(pmc->sflist);
2620 count = psl ? psl->sl_count : 0;
2621 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2622 gsf->gf_numsrc = count;
2623 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2624 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2627 for (i = 0; i < copycount; i++) {
2628 struct sockaddr_storage ss;
2630 psin = (struct sockaddr_in *)&ss;
2631 memset(&ss, 0, sizeof(ss));
2632 psin->sin_family = AF_INET;
2633 psin->sin_addr.s_addr = psl->sl_addr[i];
2634 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2643 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2645 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2648 struct inet_sock *inet = inet_sk(sk);
2649 struct ip_mc_socklist *pmc;
2650 struct ip_sf_socklist *psl;
2655 if (!ipv4_is_multicast(loc_addr))
2659 for_each_pmc_rcu(inet, pmc) {
2660 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2661 (pmc->multi.imr_ifindex == dif ||
2662 (sdif && pmc->multi.imr_ifindex == sdif)))
2668 psl = rcu_dereference(pmc->sflist);
2669 ret = (pmc->sfmode == MCAST_EXCLUDE);
2673 for (i = 0; i < psl->sl_count; i++) {
2674 if (psl->sl_addr[i] == rmt_addr)
2678 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2680 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2690 * A socket is closing.
2693 void ip_mc_drop_socket(struct sock *sk)
2695 struct inet_sock *inet = inet_sk(sk);
2696 struct ip_mc_socklist *iml;
2697 struct net *net = sock_net(sk);
2703 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2704 struct in_device *in_dev;
2706 inet->mc_list = iml->next_rcu;
2707 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2708 (void) ip_mc_leave_src(sk, iml, in_dev);
2710 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2711 /* decrease mem now to avoid the memleak warning */
2712 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2713 kfree_rcu(iml, rcu);
2718 /* called with rcu_read_lock() */
2719 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2721 struct ip_mc_list *im;
2722 struct ip_mc_list __rcu **mc_hash;
2723 struct ip_sf_list *psf;
2726 mc_hash = rcu_dereference(in_dev->mc_hash);
2728 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2730 for (im = rcu_dereference(mc_hash[hash]);
2732 im = rcu_dereference(im->next_hash)) {
2733 if (im->multiaddr == mc_addr)
2737 for_each_pmc_rcu(in_dev, im) {
2738 if (im->multiaddr == mc_addr)
2742 if (im && proto == IPPROTO_IGMP) {
2746 spin_lock_bh(&im->lock);
2747 for (psf = im->sources; psf; psf = psf->sf_next) {
2748 if (psf->sf_inaddr == src_addr)
2752 rv = psf->sf_count[MCAST_INCLUDE] ||
2753 psf->sf_count[MCAST_EXCLUDE] !=
2754 im->sfcount[MCAST_EXCLUDE];
2756 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2757 spin_unlock_bh(&im->lock);
2759 rv = 1; /* unspecified source; tentatively allow */
2764 #if defined(CONFIG_PROC_FS)
2765 struct igmp_mc_iter_state {
2766 struct seq_net_private p;
2767 struct net_device *dev;
2768 struct in_device *in_dev;
2771 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2773 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2775 struct net *net = seq_file_net(seq);
2776 struct ip_mc_list *im = NULL;
2777 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2779 state->in_dev = NULL;
2780 for_each_netdev_rcu(net, state->dev) {
2781 struct in_device *in_dev;
2783 in_dev = __in_dev_get_rcu(state->dev);
2786 im = rcu_dereference(in_dev->mc_list);
2788 state->in_dev = in_dev;
2795 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2797 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2799 im = rcu_dereference(im->next_rcu);
2801 state->dev = next_net_device_rcu(state->dev);
2803 state->in_dev = NULL;
2806 state->in_dev = __in_dev_get_rcu(state->dev);
2809 im = rcu_dereference(state->in_dev->mc_list);
2814 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2816 struct ip_mc_list *im = igmp_mc_get_first(seq);
2818 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2820 return pos ? NULL : im;
2823 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2827 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2830 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2832 struct ip_mc_list *im;
2833 if (v == SEQ_START_TOKEN)
2834 im = igmp_mc_get_first(seq);
2836 im = igmp_mc_get_next(seq, v);
2841 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2844 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2846 state->in_dev = NULL;
2851 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2853 if (v == SEQ_START_TOKEN)
2855 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2857 struct ip_mc_list *im = (struct ip_mc_list *)v;
2858 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2862 #ifdef CONFIG_IP_MULTICAST
2863 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2864 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2870 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2871 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2872 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2875 delta = im->timer.expires - jiffies;
2877 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2878 im->multiaddr, im->users,
2880 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2886 static const struct seq_operations igmp_mc_seq_ops = {
2887 .start = igmp_mc_seq_start,
2888 .next = igmp_mc_seq_next,
2889 .stop = igmp_mc_seq_stop,
2890 .show = igmp_mc_seq_show,
2893 struct igmp_mcf_iter_state {
2894 struct seq_net_private p;
2895 struct net_device *dev;
2896 struct in_device *idev;
2897 struct ip_mc_list *im;
2900 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2902 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2904 struct net *net = seq_file_net(seq);
2905 struct ip_sf_list *psf = NULL;
2906 struct ip_mc_list *im = NULL;
2907 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2911 for_each_netdev_rcu(net, state->dev) {
2912 struct in_device *idev;
2913 idev = __in_dev_get_rcu(state->dev);
2914 if (unlikely(!idev))
2916 im = rcu_dereference(idev->mc_list);
2918 spin_lock_bh(&im->lock);
2925 spin_unlock_bh(&im->lock);
2931 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2933 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2937 spin_unlock_bh(&state->im->lock);
2938 state->im = state->im->next;
2939 while (!state->im) {
2940 state->dev = next_net_device_rcu(state->dev);
2945 state->idev = __in_dev_get_rcu(state->dev);
2948 state->im = rcu_dereference(state->idev->mc_list);
2952 spin_lock_bh(&state->im->lock);
2953 psf = state->im->sources;
2959 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2961 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2963 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2965 return pos ? NULL : psf;
2968 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2972 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2975 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2977 struct ip_sf_list *psf;
2978 if (v == SEQ_START_TOKEN)
2979 psf = igmp_mcf_get_first(seq);
2981 psf = igmp_mcf_get_next(seq, v);
2986 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2989 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2990 if (likely(state->im)) {
2991 spin_unlock_bh(&state->im->lock);
2999 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
3001 struct ip_sf_list *psf = (struct ip_sf_list *)v;
3002 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
3004 if (v == SEQ_START_TOKEN) {
3005 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
3009 "0x%08x %6lu %6lu\n",
3010 state->dev->ifindex, state->dev->name,
3011 ntohl(state->im->multiaddr),
3012 ntohl(psf->sf_inaddr),
3013 psf->sf_count[MCAST_INCLUDE],
3014 psf->sf_count[MCAST_EXCLUDE]);
3019 static const struct seq_operations igmp_mcf_seq_ops = {
3020 .start = igmp_mcf_seq_start,
3021 .next = igmp_mcf_seq_next,
3022 .stop = igmp_mcf_seq_stop,
3023 .show = igmp_mcf_seq_show,
3026 static int __net_init igmp_net_init(struct net *net)
3028 struct proc_dir_entry *pde;
3031 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3032 sizeof(struct igmp_mc_iter_state));
3035 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3036 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
3039 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3040 SOCK_DGRAM, 0, net);
3042 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3050 remove_proc_entry("mcfilter", net->proc_net);
3052 remove_proc_entry("igmp", net->proc_net);
3057 static void __net_exit igmp_net_exit(struct net *net)
3059 remove_proc_entry("mcfilter", net->proc_net);
3060 remove_proc_entry("igmp", net->proc_net);
3061 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3064 static struct pernet_operations igmp_net_ops = {
3065 .init = igmp_net_init,
3066 .exit = igmp_net_exit,
3070 static int igmp_netdev_event(struct notifier_block *this,
3071 unsigned long event, void *ptr)
3073 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3074 struct in_device *in_dev;
3077 case NETDEV_RESEND_IGMP:
3078 in_dev = __in_dev_get_rtnl(dev);
3080 ip_mc_rejoin_groups(in_dev);
3088 static struct notifier_block igmp_notifier = {
3089 .notifier_call = igmp_netdev_event,
3092 int __init igmp_mc_init(void)
3094 #if defined(CONFIG_PROC_FS)
3097 err = register_pernet_subsys(&igmp_net_ops);
3100 err = register_netdevice_notifier(&igmp_notifier);
3102 goto reg_notif_fail;
3106 unregister_pernet_subsys(&igmp_net_ops);
3109 return register_netdevice_notifier(&igmp_notifier);