1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux NET3: Internet Group Management Protocol [IGMP]
5 * This code implements the IGMP protocol as defined in RFC1112. There has
6 * been a further revision of this protocol since which is now supported.
8 * If you have trouble with this module be careful what gcc you have used,
9 * the older version didn't come out right using gcc 2.5.8, the newer one
10 * seems to fall out with gcc 2.6.2.
13 * Alan Cox <alan@lxorguk.ukuu.org.uk>
17 * Alan Cox : Added lots of __inline__ to optimise
18 * the memory usage of all the tiny little
20 * Alan Cox : Dumped the header building experiment.
21 * Alan Cox : Minor tweaks ready for multicast routing
22 * and extended IGMP protocol.
23 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
24 * writes utterly bogus code otherwise (sigh)
25 * fixed IGMP loopback to behave in the manner
26 * desired by mrouted, fixed the fact it has been
27 * broken since 1.3.6 and cleaned up a few minor
30 * Chih-Jen Chang : Tried to revise IGMP to Version 2
31 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
32 * The enhancements are mainly based on Steve Deering's
33 * ipmulti-3.5 source code.
34 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
35 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
36 * the mrouted version on that device.
37 * Chih-Jen Chang : Added the max_resp_time parameter to
38 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
39 * to identify the multicast router version
40 * and do what the IGMP version 2 specified.
41 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
42 * Tsu-Sheng Tsao if the specified time expired.
43 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
44 * Alan Cox : Use GFP_ATOMIC in the right places.
45 * Christian Daudt : igmp timer wasn't set for local group
46 * memberships but was being deleted,
47 * which caused a "del_timer() called
48 * from %p with timer not initialized\n"
50 * Christian Daudt : removed del_timer from
51 * igmp_timer_expire function (960205).
52 * Christian Daudt : igmp_heard_report now only calls
53 * igmp_timer_expire if tm->running is
55 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
56 * igmp_heard_query never trigger. Expiry
57 * miscalculation fixed in igmp_heard_query
58 * and random() made to return unsigned to
59 * prevent negative expiry times.
60 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
61 * fix from pending 2.1.x patches.
62 * Alan Cox: Forget to enable FDDI support earlier.
63 * Alexey Kuznetsov: Fixed leaving groups on device down.
64 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
65 * David L Stevens: IGMPv3 support, with help from
69 #include <linux/module.h>
70 #include <linux/slab.h>
71 #include <linux/uaccess.h>
72 #include <linux/types.h>
73 #include <linux/kernel.h>
74 #include <linux/jiffies.h>
75 #include <linux/string.h>
76 #include <linux/socket.h>
77 #include <linux/sockios.h>
79 #include <linux/inet.h>
80 #include <linux/netdevice.h>
81 #include <linux/skbuff.h>
82 #include <linux/inetdevice.h>
83 #include <linux/igmp.h>
84 #include <linux/if_arp.h>
85 #include <linux/rtnetlink.h>
86 #include <linux/times.h>
87 #include <linux/pkt_sched.h>
88 #include <linux/byteorder/generic.h>
90 #include <net/net_namespace.h>
93 #include <net/protocol.h>
94 #include <net/route.h>
96 #include <net/checksum.h>
97 #include <net/inet_common.h>
98 #include <linux/netfilter_ipv4.h>
99 #ifdef CONFIG_IP_MROUTE
100 #include <linux/mroute.h>
102 #ifdef CONFIG_PROC_FS
103 #include <linux/proc_fs.h>
104 #include <linux/seq_file.h>
107 #ifdef CONFIG_IP_MULTICAST
108 /* Parameter names and values are taken from igmp-v2-06 draft */
110 #define IGMP_QUERY_INTERVAL (125*HZ)
111 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
113 #define IGMP_INITIAL_REPORT_DELAY (1)
115 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
116 * IGMP specs require to report membership immediately after
117 * joining a group, but we delay the first report by a
118 * small interval. It seems more natural and still does not
119 * contradict to specs provided this delay is small enough.
122 #define IGMP_V1_SEEN(in_dev) \
123 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
124 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
125 ((in_dev)->mr_v1_seen && \
126 time_before(jiffies, (in_dev)->mr_v1_seen)))
127 #define IGMP_V2_SEEN(in_dev) \
128 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
129 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
130 ((in_dev)->mr_v2_seen && \
131 time_before(jiffies, (in_dev)->mr_v2_seen)))
133 static int unsolicited_report_interval(struct in_device *in_dev)
135 int interval_ms, interval_jiffies;
137 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
138 interval_ms = IN_DEV_CONF_GET(
140 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
142 interval_ms = IN_DEV_CONF_GET(
144 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
146 interval_jiffies = msecs_to_jiffies(interval_ms);
148 /* _timer functions can't handle a delay of 0 jiffies so ensure
149 * we always return a positive value.
151 if (interval_jiffies <= 0)
152 interval_jiffies = 1;
153 return interval_jiffies;
156 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
158 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
159 static void igmpv3_clear_delrec(struct in_device *in_dev);
160 static int sf_setstate(struct ip_mc_list *pmc);
161 static void sf_markstate(struct ip_mc_list *pmc);
163 static void ip_mc_clear_src(struct ip_mc_list *pmc);
164 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
165 int sfcount, __be32 *psfsrc, int delta);
167 static void ip_ma_put(struct ip_mc_list *im)
169 if (refcount_dec_and_test(&im->refcnt)) {
170 in_dev_put(im->interface);
175 #define for_each_pmc_rcu(in_dev, pmc) \
176 for (pmc = rcu_dereference(in_dev->mc_list); \
178 pmc = rcu_dereference(pmc->next_rcu))
180 #define for_each_pmc_rtnl(in_dev, pmc) \
181 for (pmc = rtnl_dereference(in_dev->mc_list); \
183 pmc = rtnl_dereference(pmc->next_rcu))
185 static void ip_sf_list_clear_all(struct ip_sf_list *psf)
187 struct ip_sf_list *next;
196 #ifdef CONFIG_IP_MULTICAST
202 static void igmp_stop_timer(struct ip_mc_list *im)
204 spin_lock_bh(&im->lock);
205 if (del_timer(&im->timer))
206 refcount_dec(&im->refcnt);
209 im->unsolicit_count = 0;
210 spin_unlock_bh(&im->lock);
213 /* It must be called with locked im->lock */
214 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
216 int tv = get_random_u32_below(max_delay);
219 if (refcount_inc_not_zero(&im->refcnt)) {
220 if (mod_timer(&im->timer, jiffies + tv + 2))
225 static void igmp_gq_start_timer(struct in_device *in_dev)
227 int tv = get_random_u32_below(in_dev->mr_maxdelay);
228 unsigned long exp = jiffies + tv + 2;
230 if (in_dev->mr_gq_running &&
231 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
234 in_dev->mr_gq_running = 1;
235 if (!mod_timer(&in_dev->mr_gq_timer, exp))
239 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
241 int tv = get_random_u32_below(delay);
243 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
247 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
249 spin_lock_bh(&im->lock);
250 im->unsolicit_count = 0;
251 if (del_timer(&im->timer)) {
252 if ((long)(im->timer.expires-jiffies) < max_delay) {
253 add_timer(&im->timer);
255 spin_unlock_bh(&im->lock);
258 refcount_dec(&im->refcnt);
260 igmp_start_timer(im, max_delay);
261 spin_unlock_bh(&im->lock);
266 * Send an IGMP report.
269 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
272 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
273 int gdeleted, int sdeleted)
276 case IGMPV3_MODE_IS_INCLUDE:
277 case IGMPV3_MODE_IS_EXCLUDE:
278 if (gdeleted || sdeleted)
280 if (!(pmc->gsquery && !psf->sf_gsresp)) {
281 if (pmc->sfmode == MCAST_INCLUDE)
283 /* don't include if this source is excluded
286 if (psf->sf_count[MCAST_INCLUDE])
287 return type == IGMPV3_MODE_IS_INCLUDE;
288 return pmc->sfcount[MCAST_EXCLUDE] ==
289 psf->sf_count[MCAST_EXCLUDE];
292 case IGMPV3_CHANGE_TO_INCLUDE:
293 if (gdeleted || sdeleted)
295 return psf->sf_count[MCAST_INCLUDE] != 0;
296 case IGMPV3_CHANGE_TO_EXCLUDE:
297 if (gdeleted || sdeleted)
299 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
300 psf->sf_count[MCAST_INCLUDE])
302 return pmc->sfcount[MCAST_EXCLUDE] ==
303 psf->sf_count[MCAST_EXCLUDE];
304 case IGMPV3_ALLOW_NEW_SOURCES:
305 if (gdeleted || !psf->sf_crcount)
307 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
308 case IGMPV3_BLOCK_OLD_SOURCES:
309 if (pmc->sfmode == MCAST_INCLUDE)
310 return gdeleted || (psf->sf_crcount && sdeleted);
311 return psf->sf_crcount && !gdeleted && !sdeleted;
317 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
319 struct ip_sf_list *psf;
322 for (psf = pmc->sources; psf; psf = psf->sf_next) {
323 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
330 /* source address selection per RFC 3376 section 4.2.13 */
331 static __be32 igmpv3_get_srcaddr(struct net_device *dev,
332 const struct flowi4 *fl4)
334 struct in_device *in_dev = __in_dev_get_rcu(dev);
335 const struct in_ifaddr *ifa;
338 return htonl(INADDR_ANY);
340 in_dev_for_each_ifa_rcu(ifa, in_dev) {
341 if (fl4->saddr == ifa->ifa_local)
345 return htonl(INADDR_ANY);
348 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
353 struct igmpv3_report *pig;
354 struct net *net = dev_net(dev);
356 int hlen = LL_RESERVED_SPACE(dev);
357 int tlen = dev->needed_tailroom;
360 size = min(mtu, IP_MAX_MTU);
362 skb = alloc_skb(size + hlen + tlen,
363 GFP_ATOMIC | __GFP_NOWARN);
370 skb->priority = TC_PRIO_CONTROL;
372 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
374 IPPROTO_IGMP, 0, dev->ifindex);
380 skb_dst_set(skb, &rt->dst);
383 skb_reserve(skb, hlen);
384 skb_tailroom_reserve(skb, mtu, tlen);
386 skb_reset_network_header(skb);
388 skb_put(skb, sizeof(struct iphdr) + 4);
391 pip->ihl = (sizeof(struct iphdr)+4)>>2;
393 pip->frag_off = htons(IP_DF);
395 pip->daddr = fl4.daddr;
398 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
401 pip->protocol = IPPROTO_IGMP;
402 pip->tot_len = 0; /* filled in later */
403 ip_select_ident(net, skb, NULL);
404 ((u8 *)&pip[1])[0] = IPOPT_RA;
405 ((u8 *)&pip[1])[1] = 4;
406 ((u8 *)&pip[1])[2] = 0;
407 ((u8 *)&pip[1])[3] = 0;
409 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
410 skb_put(skb, sizeof(*pig));
411 pig = igmpv3_report_hdr(skb);
412 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
420 static int igmpv3_sendpack(struct sk_buff *skb)
422 struct igmphdr *pig = igmp_hdr(skb);
423 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
425 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
427 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
430 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
432 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
435 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
436 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
438 struct net_device *dev = pmc->interface->dev;
439 struct igmpv3_report *pih;
440 struct igmpv3_grec *pgr;
443 skb = igmpv3_newpack(dev, mtu);
447 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
448 pgr->grec_type = type;
449 pgr->grec_auxwords = 0;
451 pgr->grec_mca = pmc->multiaddr;
452 pih = igmpv3_report_hdr(skb);
453 pih->ngrec = htons(ntohs(pih->ngrec)+1);
458 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
460 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
461 int type, int gdeleted, int sdeleted)
463 struct net_device *dev = pmc->interface->dev;
464 struct net *net = dev_net(dev);
465 struct igmpv3_report *pih;
466 struct igmpv3_grec *pgr = NULL;
467 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
468 int scount, stotal, first, isquery, truncate;
471 if (pmc->multiaddr == IGMP_ALL_HOSTS)
473 if (ipv4_is_local_multicast(pmc->multiaddr) &&
474 !READ_ONCE(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 !READ_ONCE(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) &&
744 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
747 if (type == IGMP_HOST_LEAVE_MESSAGE)
748 dst = IGMP_ALL_ROUTER;
752 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
754 IPPROTO_IGMP, 0, dev->ifindex);
758 hlen = LL_RESERVED_SPACE(dev);
759 tlen = dev->needed_tailroom;
760 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
765 skb->priority = TC_PRIO_CONTROL;
767 skb_dst_set(skb, &rt->dst);
769 skb_reserve(skb, hlen);
771 skb_reset_network_header(skb);
773 skb_put(skb, sizeof(struct iphdr) + 4);
776 iph->ihl = (sizeof(struct iphdr)+4)>>2;
778 iph->frag_off = htons(IP_DF);
781 iph->saddr = fl4.saddr;
782 iph->protocol = IPPROTO_IGMP;
783 ip_select_ident(net, skb, NULL);
784 ((u8 *)&iph[1])[0] = IPOPT_RA;
785 ((u8 *)&iph[1])[1] = 4;
786 ((u8 *)&iph[1])[2] = 0;
787 ((u8 *)&iph[1])[3] = 0;
789 ih = skb_put(skb, sizeof(struct igmphdr));
794 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
796 return ip_local_out(net, skb->sk, skb);
799 static void igmp_gq_timer_expire(struct timer_list *t)
801 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
803 in_dev->mr_gq_running = 0;
804 igmpv3_send_report(in_dev, NULL);
808 static void igmp_ifc_timer_expire(struct timer_list *t)
810 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
813 igmpv3_send_cr(in_dev);
815 mr_ifc_count = READ_ONCE(in_dev->mr_ifc_count);
818 if (cmpxchg(&in_dev->mr_ifc_count,
820 mr_ifc_count - 1) != mr_ifc_count)
822 igmp_ifc_start_timer(in_dev,
823 unsolicited_report_interval(in_dev));
828 static void igmp_ifc_event(struct in_device *in_dev)
830 struct net *net = dev_net(in_dev->dev);
831 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
833 WRITE_ONCE(in_dev->mr_ifc_count, in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv));
834 igmp_ifc_start_timer(in_dev, 1);
838 static void igmp_timer_expire(struct timer_list *t)
840 struct ip_mc_list *im = from_timer(im, t, timer);
841 struct in_device *in_dev = im->interface;
843 spin_lock(&im->lock);
846 if (im->unsolicit_count && --im->unsolicit_count)
847 igmp_start_timer(im, unsolicited_report_interval(in_dev));
850 spin_unlock(&im->lock);
852 if (IGMP_V1_SEEN(in_dev))
853 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
854 else if (IGMP_V2_SEEN(in_dev))
855 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
857 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
862 /* mark EXCLUDE-mode sources */
863 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
865 struct ip_sf_list *psf;
869 for (psf = pmc->sources; psf; psf = psf->sf_next) {
872 for (i = 0; i < nsrcs; i++) {
873 /* skip inactive filters */
874 if (psf->sf_count[MCAST_INCLUDE] ||
875 pmc->sfcount[MCAST_EXCLUDE] !=
876 psf->sf_count[MCAST_EXCLUDE])
878 if (srcs[i] == psf->sf_inaddr) {
885 if (scount == nsrcs) /* all sources excluded */
890 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
892 struct ip_sf_list *psf;
895 if (pmc->sfmode == MCAST_EXCLUDE)
896 return igmp_xmarksources(pmc, nsrcs, srcs);
898 /* mark INCLUDE-mode sources */
900 for (psf = pmc->sources; psf; psf = psf->sf_next) {
903 for (i = 0; i < nsrcs; i++)
904 if (srcs[i] == psf->sf_inaddr) {
918 /* return true if packet was dropped */
919 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
921 struct ip_mc_list *im;
922 struct net *net = dev_net(in_dev->dev);
924 /* Timers are only set for non-local groups */
926 if (group == IGMP_ALL_HOSTS)
928 if (ipv4_is_local_multicast(group) &&
929 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
933 for_each_pmc_rcu(in_dev, im) {
934 if (im->multiaddr == group) {
943 /* return true if packet was dropped */
944 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
947 struct igmphdr *ih = igmp_hdr(skb);
948 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
949 struct ip_mc_list *im;
950 __be32 group = ih->group;
953 struct net *net = dev_net(in_dev->dev);
958 /* Alas, old v1 router presents here. */
960 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
961 in_dev->mr_v1_seen = jiffies +
962 (in_dev->mr_qrv * in_dev->mr_qi) +
966 /* v2 router present */
967 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
968 in_dev->mr_v2_seen = jiffies +
969 (in_dev->mr_qrv * in_dev->mr_qi) +
972 /* cancel the interface change timer */
973 WRITE_ONCE(in_dev->mr_ifc_count, 0);
974 if (del_timer(&in_dev->mr_ifc_timer))
975 __in_dev_put(in_dev);
976 /* clear deleted report items */
977 igmpv3_clear_delrec(in_dev);
978 } else if (len < 12) {
979 return true; /* ignore bogus packet; freed by caller */
980 } else if (IGMP_V1_SEEN(in_dev)) {
981 /* This is a v3 query with v1 queriers present */
982 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
984 } else if (IGMP_V2_SEEN(in_dev)) {
985 /* this is a v3 query with v2 queriers present;
986 * Interpretation of the max_delay code is problematic here.
987 * A real v2 host would use ih_code directly, while v3 has a
988 * different encoding. We use the v3 encoding as more likely
989 * to be intended in a v3 query.
991 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
993 max_delay = 1; /* can't mod w/ 0 */
995 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
998 ih3 = igmpv3_query_hdr(skb);
1000 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
1001 + ntohs(ih3->nsrcs)*sizeof(__be32)))
1003 ih3 = igmpv3_query_hdr(skb);
1006 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
1008 max_delay = 1; /* can't mod w/ 0 */
1009 in_dev->mr_maxdelay = max_delay;
1011 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
1012 * received value was zero, use the default or statically
1015 in_dev->mr_qrv = ih3->qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1016 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
1018 /* RFC3376, 8.3. Query Response Interval:
1019 * The number of seconds represented by the [Query Response
1020 * Interval] must be less than the [Query Interval].
1022 if (in_dev->mr_qri >= in_dev->mr_qi)
1023 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
1025 if (!group) { /* general query */
1027 return true; /* no sources allowed */
1028 igmp_gq_start_timer(in_dev);
1031 /* mark sources to include, if group & source-specific */
1032 mark = ih3->nsrcs != 0;
1036 * - Start the timers in all of our membership records
1037 * that the query applies to for the interface on
1038 * which the query arrived excl. those that belong
1039 * to a "local" group (224.0.0.X)
1040 * - For timers already running check if they need to
1042 * - Use the igmp->igmp_code field as the maximum
1046 for_each_pmc_rcu(in_dev, im) {
1049 if (group && group != im->multiaddr)
1051 if (im->multiaddr == IGMP_ALL_HOSTS)
1053 if (ipv4_is_local_multicast(im->multiaddr) &&
1054 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1056 spin_lock_bh(&im->lock);
1058 im->gsquery = im->gsquery && mark;
1061 changed = !im->gsquery ||
1062 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1063 spin_unlock_bh(&im->lock);
1065 igmp_mod_timer(im, max_delay);
1071 /* called in rcu_read_lock() section */
1072 int igmp_rcv(struct sk_buff *skb)
1074 /* This basically follows the spec line by line -- see RFC1112 */
1076 struct net_device *dev = skb->dev;
1077 struct in_device *in_dev;
1079 bool dropped = true;
1081 if (netif_is_l3_master(dev)) {
1082 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1087 in_dev = __in_dev_get_rcu(dev);
1091 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1094 if (skb_checksum_simple_validate(skb))
1099 case IGMP_HOST_MEMBERSHIP_QUERY:
1100 dropped = igmp_heard_query(in_dev, skb, len);
1102 case IGMP_HOST_MEMBERSHIP_REPORT:
1103 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1104 /* Is it our report looped back? */
1105 if (rt_is_output_route(skb_rtable(skb)))
1107 /* don't rely on MC router hearing unicast reports */
1108 if (skb->pkt_type == PACKET_MULTICAST ||
1109 skb->pkt_type == PACKET_BROADCAST)
1110 dropped = igmp_heard_report(in_dev, ih->group);
1113 #ifdef CONFIG_IP_PIMSM_V1
1114 return pim_rcv_v1(skb);
1116 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1119 case IGMP_HOST_LEAVE_MESSAGE:
1121 case IGMP_MTRACE_RESP:
1139 * Add a filter to a device
1142 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1144 char buf[MAX_ADDR_LEN];
1145 struct net_device *dev = in_dev->dev;
1147 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1148 We will get multicast token leakage, when IFF_MULTICAST
1149 is changed. This check should be done in ndo_set_rx_mode
1150 routine. Something sort of:
1151 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1154 if (arp_mc_map(addr, buf, dev, 0) == 0)
1155 dev_mc_add(dev, buf);
1159 * Remove a filter from a device
1162 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1164 char buf[MAX_ADDR_LEN];
1165 struct net_device *dev = in_dev->dev;
1167 if (arp_mc_map(addr, buf, dev, 0) == 0)
1168 dev_mc_del(dev, buf);
1171 #ifdef CONFIG_IP_MULTICAST
1173 * deleted ip_mc_list manipulation
1175 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
1178 struct ip_mc_list *pmc;
1179 struct net *net = dev_net(in_dev->dev);
1181 /* this is an "ip_mc_list" for convenience; only the fields below
1182 * are actually used. In particular, the refcnt and users are not
1183 * used for management of the delete list. Using the same structure
1184 * for deleted items allows change reports to use common code with
1185 * non-deleted or query-response MCA's.
1187 pmc = kzalloc(sizeof(*pmc), gfp);
1190 spin_lock_init(&pmc->lock);
1191 spin_lock_bh(&im->lock);
1192 pmc->interface = im->interface;
1193 in_dev_hold(in_dev);
1194 pmc->multiaddr = im->multiaddr;
1195 pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1196 pmc->sfmode = im->sfmode;
1197 if (pmc->sfmode == MCAST_INCLUDE) {
1198 struct ip_sf_list *psf;
1200 pmc->tomb = im->tomb;
1201 pmc->sources = im->sources;
1202 im->tomb = im->sources = NULL;
1203 for (psf = pmc->sources; psf; psf = psf->sf_next)
1204 psf->sf_crcount = pmc->crcount;
1206 spin_unlock_bh(&im->lock);
1208 spin_lock_bh(&in_dev->mc_tomb_lock);
1209 pmc->next = in_dev->mc_tomb;
1210 in_dev->mc_tomb = pmc;
1211 spin_unlock_bh(&in_dev->mc_tomb_lock);
1215 * restore ip_mc_list deleted records
1217 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1219 struct ip_mc_list *pmc, *pmc_prev;
1220 struct ip_sf_list *psf;
1221 struct net *net = dev_net(in_dev->dev);
1222 __be32 multiaddr = im->multiaddr;
1224 spin_lock_bh(&in_dev->mc_tomb_lock);
1226 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1227 if (pmc->multiaddr == multiaddr)
1233 pmc_prev->next = pmc->next;
1235 in_dev->mc_tomb = pmc->next;
1237 spin_unlock_bh(&in_dev->mc_tomb_lock);
1239 spin_lock_bh(&im->lock);
1241 im->interface = pmc->interface;
1242 if (im->sfmode == MCAST_INCLUDE) {
1243 swap(im->tomb, pmc->tomb);
1244 swap(im->sources, pmc->sources);
1245 for (psf = im->sources; psf; psf = psf->sf_next)
1246 psf->sf_crcount = in_dev->mr_qrv ?:
1247 READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1249 im->crcount = in_dev->mr_qrv ?:
1250 READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1252 in_dev_put(pmc->interface);
1255 spin_unlock_bh(&im->lock);
1259 * flush ip_mc_list deleted records
1261 static void igmpv3_clear_delrec(struct in_device *in_dev)
1263 struct ip_mc_list *pmc, *nextpmc;
1265 spin_lock_bh(&in_dev->mc_tomb_lock);
1266 pmc = in_dev->mc_tomb;
1267 in_dev->mc_tomb = NULL;
1268 spin_unlock_bh(&in_dev->mc_tomb_lock);
1270 for (; pmc; pmc = nextpmc) {
1271 nextpmc = pmc->next;
1272 ip_mc_clear_src(pmc);
1273 in_dev_put(pmc->interface);
1276 /* clear dead sources, too */
1278 for_each_pmc_rcu(in_dev, pmc) {
1279 struct ip_sf_list *psf;
1281 spin_lock_bh(&pmc->lock);
1284 spin_unlock_bh(&pmc->lock);
1285 ip_sf_list_clear_all(psf);
1291 static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
1293 struct in_device *in_dev = im->interface;
1294 #ifdef CONFIG_IP_MULTICAST
1295 struct net *net = dev_net(in_dev->dev);
1301 ip_mc_filter_del(in_dev, im->multiaddr);
1304 #ifdef CONFIG_IP_MULTICAST
1305 if (im->multiaddr == IGMP_ALL_HOSTS)
1307 if (ipv4_is_local_multicast(im->multiaddr) &&
1308 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1311 reporter = im->reporter;
1312 igmp_stop_timer(im);
1314 if (!in_dev->dead) {
1315 if (IGMP_V1_SEEN(in_dev))
1317 if (IGMP_V2_SEEN(in_dev)) {
1319 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1323 igmpv3_add_delrec(in_dev, im, gfp);
1325 igmp_ifc_event(in_dev);
1330 static void igmp_group_dropped(struct ip_mc_list *im)
1332 __igmp_group_dropped(im, GFP_KERNEL);
1335 static void igmp_group_added(struct ip_mc_list *im)
1337 struct in_device *in_dev = im->interface;
1338 #ifdef CONFIG_IP_MULTICAST
1339 struct net *net = dev_net(in_dev->dev);
1342 if (im->loaded == 0) {
1344 ip_mc_filter_add(in_dev, im->multiaddr);
1347 #ifdef CONFIG_IP_MULTICAST
1348 if (im->multiaddr == IGMP_ALL_HOSTS)
1350 if (ipv4_is_local_multicast(im->multiaddr) &&
1351 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1357 im->unsolicit_count = READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1358 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1359 spin_lock_bh(&im->lock);
1360 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1361 spin_unlock_bh(&im->lock);
1366 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1367 * not send filter-mode change record as the mode should be from
1370 if (im->sfmode == MCAST_EXCLUDE)
1371 im->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1373 igmp_ifc_event(in_dev);
1379 * Multicast list managers
1382 static u32 ip_mc_hash(const struct ip_mc_list *im)
1384 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1387 static void ip_mc_hash_add(struct in_device *in_dev,
1388 struct ip_mc_list *im)
1390 struct ip_mc_list __rcu **mc_hash;
1393 mc_hash = rtnl_dereference(in_dev->mc_hash);
1395 hash = ip_mc_hash(im);
1396 im->next_hash = mc_hash[hash];
1397 rcu_assign_pointer(mc_hash[hash], im);
1401 /* do not use a hash table for small number of items */
1402 if (in_dev->mc_count < 4)
1405 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1410 for_each_pmc_rtnl(in_dev, im) {
1411 hash = ip_mc_hash(im);
1412 im->next_hash = mc_hash[hash];
1413 RCU_INIT_POINTER(mc_hash[hash], im);
1416 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1419 static void ip_mc_hash_remove(struct in_device *in_dev,
1420 struct ip_mc_list *im)
1422 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1423 struct ip_mc_list *aux;
1427 mc_hash += ip_mc_hash(im);
1428 while ((aux = rtnl_dereference(*mc_hash)) != im)
1429 mc_hash = &aux->next_hash;
1430 *mc_hash = im->next_hash;
1435 * A socket has joined a multicast group on device dev.
1437 static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1438 unsigned int mode, gfp_t gfp)
1440 struct ip_mc_list *im;
1444 for_each_pmc_rtnl(in_dev, im) {
1445 if (im->multiaddr == addr) {
1447 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
1452 im = kzalloc(sizeof(*im), gfp);
1457 im->interface = in_dev;
1458 in_dev_hold(in_dev);
1459 im->multiaddr = addr;
1460 /* initial mode is (EX, empty) */
1462 im->sfcount[mode] = 1;
1463 refcount_set(&im->refcnt, 1);
1464 spin_lock_init(&im->lock);
1465 #ifdef CONFIG_IP_MULTICAST
1466 timer_setup(&im->timer, igmp_timer_expire, 0);
1469 im->next_rcu = in_dev->mc_list;
1471 rcu_assign_pointer(in_dev->mc_list, im);
1473 ip_mc_hash_add(in_dev, im);
1475 #ifdef CONFIG_IP_MULTICAST
1476 igmpv3_del_delrec(in_dev, im);
1478 igmp_group_added(im);
1480 ip_rt_multicast_event(in_dev);
1485 void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1487 ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
1489 EXPORT_SYMBOL(__ip_mc_inc_group);
1491 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1493 __ip_mc_inc_group(in_dev, addr, GFP_KERNEL);
1495 EXPORT_SYMBOL(ip_mc_inc_group);
1497 static int ip_mc_check_iphdr(struct sk_buff *skb)
1499 const struct iphdr *iph;
1501 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1503 if (!pskb_may_pull(skb, offset))
1508 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1511 offset += ip_hdrlen(skb) - sizeof(*iph);
1513 if (!pskb_may_pull(skb, offset))
1518 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1521 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1522 if (skb->len < len || len < offset)
1525 skb_set_transport_header(skb, offset);
1530 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1532 unsigned int len = skb_transport_offset(skb);
1534 len += sizeof(struct igmpv3_report);
1536 return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
1539 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1541 unsigned int transport_len = ip_transport_len(skb);
1545 if (transport_len != sizeof(struct igmphdr)) {
1547 if (transport_len < sizeof(struct igmpv3_query))
1550 len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1551 if (!ip_mc_may_pull(skb, len))
1555 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1556 * all-systems destination addresses (224.0.0.1) for general queries
1558 if (!igmp_hdr(skb)->group &&
1559 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1565 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1567 switch (igmp_hdr(skb)->type) {
1568 case IGMP_HOST_LEAVE_MESSAGE:
1569 case IGMP_HOST_MEMBERSHIP_REPORT:
1570 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1572 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1573 return ip_mc_check_igmp_reportv3(skb);
1574 case IGMP_HOST_MEMBERSHIP_QUERY:
1575 return ip_mc_check_igmp_query(skb);
1581 static __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1583 return skb_checksum_simple_validate(skb);
1586 static int ip_mc_check_igmp_csum(struct sk_buff *skb)
1588 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1589 unsigned int transport_len = ip_transport_len(skb);
1590 struct sk_buff *skb_chk;
1592 if (!ip_mc_may_pull(skb, len))
1595 skb_chk = skb_checksum_trimmed(skb, transport_len,
1596 ip_mc_validate_checksum);
1607 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1608 * @skb: the skb to validate
1610 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1611 * skb transport header accordingly and returns zero.
1613 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1615 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1616 * -ENOMEM: A memory allocation failure happened.
1618 * Caller needs to set the skb network header and free any returned skb if it
1619 * differs from the provided skb.
1621 int ip_mc_check_igmp(struct sk_buff *skb)
1623 int ret = ip_mc_check_iphdr(skb);
1628 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1631 ret = ip_mc_check_igmp_csum(skb);
1635 return ip_mc_check_igmp_msg(skb);
1637 EXPORT_SYMBOL(ip_mc_check_igmp);
1640 * Resend IGMP JOIN report; used by netdev notifier.
1642 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1644 #ifdef CONFIG_IP_MULTICAST
1645 struct ip_mc_list *im;
1647 struct net *net = dev_net(in_dev->dev);
1651 for_each_pmc_rtnl(in_dev, im) {
1652 if (im->multiaddr == IGMP_ALL_HOSTS)
1654 if (ipv4_is_local_multicast(im->multiaddr) &&
1655 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1658 /* a failover is happening and switches
1659 * must be notified immediately
1661 if (IGMP_V1_SEEN(in_dev))
1662 type = IGMP_HOST_MEMBERSHIP_REPORT;
1663 else if (IGMP_V2_SEEN(in_dev))
1664 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1666 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1667 igmp_send_report(in_dev, im, type);
1673 * A socket has left a multicast group on device dev
1676 void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1678 struct ip_mc_list *i;
1679 struct ip_mc_list __rcu **ip;
1683 for (ip = &in_dev->mc_list;
1684 (i = rtnl_dereference(*ip)) != NULL;
1685 ip = &i->next_rcu) {
1686 if (i->multiaddr == addr) {
1687 if (--i->users == 0) {
1688 ip_mc_hash_remove(in_dev, i);
1691 __igmp_group_dropped(i, gfp);
1695 ip_rt_multicast_event(in_dev);
1704 EXPORT_SYMBOL(__ip_mc_dec_group);
1706 /* Device changing type */
1708 void ip_mc_unmap(struct in_device *in_dev)
1710 struct ip_mc_list *pmc;
1714 for_each_pmc_rtnl(in_dev, pmc)
1715 igmp_group_dropped(pmc);
1718 void ip_mc_remap(struct in_device *in_dev)
1720 struct ip_mc_list *pmc;
1724 for_each_pmc_rtnl(in_dev, pmc) {
1725 #ifdef CONFIG_IP_MULTICAST
1726 igmpv3_del_delrec(in_dev, pmc);
1728 igmp_group_added(pmc);
1732 /* Device going down */
1734 void ip_mc_down(struct in_device *in_dev)
1736 struct ip_mc_list *pmc;
1740 for_each_pmc_rtnl(in_dev, pmc)
1741 igmp_group_dropped(pmc);
1743 #ifdef CONFIG_IP_MULTICAST
1744 WRITE_ONCE(in_dev->mr_ifc_count, 0);
1745 if (del_timer(&in_dev->mr_ifc_timer))
1746 __in_dev_put(in_dev);
1747 in_dev->mr_gq_running = 0;
1748 if (del_timer(&in_dev->mr_gq_timer))
1749 __in_dev_put(in_dev);
1752 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1755 #ifdef CONFIG_IP_MULTICAST
1756 static void ip_mc_reset(struct in_device *in_dev)
1758 struct net *net = dev_net(in_dev->dev);
1760 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1761 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1762 in_dev->mr_qrv = READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1765 static void ip_mc_reset(struct in_device *in_dev)
1770 void ip_mc_init_dev(struct in_device *in_dev)
1774 #ifdef CONFIG_IP_MULTICAST
1775 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1776 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
1778 ip_mc_reset(in_dev);
1780 spin_lock_init(&in_dev->mc_tomb_lock);
1783 /* Device going up */
1785 void ip_mc_up(struct in_device *in_dev)
1787 struct ip_mc_list *pmc;
1791 ip_mc_reset(in_dev);
1792 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1794 for_each_pmc_rtnl(in_dev, pmc) {
1795 #ifdef CONFIG_IP_MULTICAST
1796 igmpv3_del_delrec(in_dev, pmc);
1798 igmp_group_added(pmc);
1803 * Device is about to be destroyed: clean up.
1806 void ip_mc_destroy_dev(struct in_device *in_dev)
1808 struct ip_mc_list *i;
1812 /* Deactivate timers */
1814 #ifdef CONFIG_IP_MULTICAST
1815 igmpv3_clear_delrec(in_dev);
1818 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1819 in_dev->mc_list = i->next_rcu;
1826 /* RTNL is locked */
1827 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1829 struct net_device *dev = NULL;
1830 struct in_device *idev = NULL;
1832 if (imr->imr_ifindex) {
1833 idev = inetdev_by_index(net, imr->imr_ifindex);
1836 if (imr->imr_address.s_addr) {
1837 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1843 struct rtable *rt = ip_route_output(net,
1844 imr->imr_multiaddr.s_addr,
1852 imr->imr_ifindex = dev->ifindex;
1853 idev = __in_dev_get_rtnl(dev);
1859 * Join a socket to a group
1862 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1865 struct ip_sf_list *psf, *psf_prev;
1869 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1870 if (psf->sf_inaddr == *psfsrc)
1874 if (!psf || psf->sf_count[sfmode] == 0) {
1875 /* source filter not found, or count wrong => bug */
1878 psf->sf_count[sfmode]--;
1879 if (psf->sf_count[sfmode] == 0) {
1880 ip_rt_multicast_event(pmc->interface);
1882 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1883 #ifdef CONFIG_IP_MULTICAST
1884 struct in_device *in_dev = pmc->interface;
1885 struct net *net = dev_net(in_dev->dev);
1888 /* no more filters for this source */
1890 psf_prev->sf_next = psf->sf_next;
1892 pmc->sources = psf->sf_next;
1893 #ifdef CONFIG_IP_MULTICAST
1894 if (psf->sf_oldin &&
1895 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1896 psf->sf_crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1897 psf->sf_next = pmc->tomb;
1907 #ifndef CONFIG_IP_MULTICAST
1908 #define igmp_ifc_event(x) do { } while (0)
1911 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1912 int sfcount, __be32 *psfsrc, int delta)
1914 struct ip_mc_list *pmc;
1921 for_each_pmc_rcu(in_dev, pmc) {
1922 if (*pmca == pmc->multiaddr)
1926 /* MCA not found?? bug */
1930 spin_lock_bh(&pmc->lock);
1932 #ifdef CONFIG_IP_MULTICAST
1937 if (!pmc->sfcount[sfmode])
1939 pmc->sfcount[sfmode]--;
1942 for (i = 0; i < sfcount; i++) {
1943 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1945 changerec |= rv > 0;
1949 if (pmc->sfmode == MCAST_EXCLUDE &&
1950 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1951 pmc->sfcount[MCAST_INCLUDE]) {
1952 #ifdef CONFIG_IP_MULTICAST
1953 struct ip_sf_list *psf;
1954 struct net *net = dev_net(in_dev->dev);
1957 /* filter mode change */
1958 pmc->sfmode = MCAST_INCLUDE;
1959 #ifdef CONFIG_IP_MULTICAST
1960 pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1961 WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
1962 for (psf = pmc->sources; psf; psf = psf->sf_next)
1963 psf->sf_crcount = 0;
1964 igmp_ifc_event(pmc->interface);
1965 } else if (sf_setstate(pmc) || changerec) {
1966 igmp_ifc_event(pmc->interface);
1970 spin_unlock_bh(&pmc->lock);
1975 * Add multicast single-source filter to the interface list
1977 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1980 struct ip_sf_list *psf, *psf_prev;
1983 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1984 if (psf->sf_inaddr == *psfsrc)
1989 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1992 psf->sf_inaddr = *psfsrc;
1994 psf_prev->sf_next = psf;
1998 psf->sf_count[sfmode]++;
1999 if (psf->sf_count[sfmode] == 1) {
2000 ip_rt_multicast_event(pmc->interface);
2005 #ifdef CONFIG_IP_MULTICAST
2006 static void sf_markstate(struct ip_mc_list *pmc)
2008 struct ip_sf_list *psf;
2009 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2011 for (psf = pmc->sources; psf; psf = psf->sf_next)
2012 if (pmc->sfcount[MCAST_EXCLUDE]) {
2013 psf->sf_oldin = mca_xcount ==
2014 psf->sf_count[MCAST_EXCLUDE] &&
2015 !psf->sf_count[MCAST_INCLUDE];
2017 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2020 static int sf_setstate(struct ip_mc_list *pmc)
2022 struct ip_sf_list *psf, *dpsf;
2023 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2024 int qrv = pmc->interface->mr_qrv;
2028 for (psf = pmc->sources; psf; psf = psf->sf_next) {
2029 if (pmc->sfcount[MCAST_EXCLUDE]) {
2030 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2031 !psf->sf_count[MCAST_INCLUDE];
2033 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2035 if (!psf->sf_oldin) {
2036 struct ip_sf_list *prev = NULL;
2038 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2039 if (dpsf->sf_inaddr == psf->sf_inaddr)
2045 prev->sf_next = dpsf->sf_next;
2047 pmc->tomb = dpsf->sf_next;
2050 psf->sf_crcount = qrv;
2053 } else if (psf->sf_oldin) {
2055 psf->sf_crcount = 0;
2057 * add or update "delete" records if an active filter
2060 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2061 if (dpsf->sf_inaddr == psf->sf_inaddr)
2064 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2068 /* pmc->lock held by callers */
2069 dpsf->sf_next = pmc->tomb;
2072 dpsf->sf_crcount = qrv;
2081 * Add multicast source filter list to the interface list
2083 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2084 int sfcount, __be32 *psfsrc, int delta)
2086 struct ip_mc_list *pmc;
2093 for_each_pmc_rcu(in_dev, pmc) {
2094 if (*pmca == pmc->multiaddr)
2098 /* MCA not found?? bug */
2102 spin_lock_bh(&pmc->lock);
2105 #ifdef CONFIG_IP_MULTICAST
2108 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2110 pmc->sfcount[sfmode]++;
2112 for (i = 0; i < sfcount; i++) {
2113 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2121 pmc->sfcount[sfmode]--;
2122 for (j = 0; j < i; j++)
2123 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2124 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2125 #ifdef CONFIG_IP_MULTICAST
2126 struct ip_sf_list *psf;
2127 struct net *net = dev_net(pmc->interface->dev);
2128 in_dev = pmc->interface;
2131 /* filter mode change */
2132 if (pmc->sfcount[MCAST_EXCLUDE])
2133 pmc->sfmode = MCAST_EXCLUDE;
2134 else if (pmc->sfcount[MCAST_INCLUDE])
2135 pmc->sfmode = MCAST_INCLUDE;
2136 #ifdef CONFIG_IP_MULTICAST
2137 /* else no filters; keep old mode for reports */
2139 pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
2140 WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
2141 for (psf = pmc->sources; psf; psf = psf->sf_next)
2142 psf->sf_crcount = 0;
2143 igmp_ifc_event(in_dev);
2144 } else if (sf_setstate(pmc)) {
2145 igmp_ifc_event(in_dev);
2148 spin_unlock_bh(&pmc->lock);
2152 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2154 struct ip_sf_list *tomb, *sources;
2156 spin_lock_bh(&pmc->lock);
2159 sources = pmc->sources;
2160 pmc->sources = NULL;
2161 pmc->sfmode = MCAST_EXCLUDE;
2162 pmc->sfcount[MCAST_INCLUDE] = 0;
2163 pmc->sfcount[MCAST_EXCLUDE] = 1;
2164 spin_unlock_bh(&pmc->lock);
2166 ip_sf_list_clear_all(tomb);
2167 ip_sf_list_clear_all(sources);
2170 /* Join a multicast group
2172 static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2175 __be32 addr = imr->imr_multiaddr.s_addr;
2176 struct ip_mc_socklist *iml, *i;
2177 struct in_device *in_dev;
2178 struct inet_sock *inet = inet_sk(sk);
2179 struct net *net = sock_net(sk);
2186 if (!ipv4_is_multicast(addr))
2189 in_dev = ip_mc_find_dev(net, imr);
2197 ifindex = imr->imr_ifindex;
2198 for_each_pmc_rtnl(inet, i) {
2199 if (i->multi.imr_multiaddr.s_addr == addr &&
2200 i->multi.imr_ifindex == ifindex)
2205 if (count >= READ_ONCE(net->ipv4.sysctl_igmp_max_memberships))
2207 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2211 memcpy(&iml->multi, imr, sizeof(*imr));
2212 iml->next_rcu = inet->mc_list;
2215 rcu_assign_pointer(inet->mc_list, iml);
2216 ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL);
2222 /* Join ASM (Any-Source Multicast) group
2224 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2226 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2228 EXPORT_SYMBOL(ip_mc_join_group);
2230 /* Join SSM (Source-Specific Multicast) group
2232 int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2235 return __ip_mc_join_group(sk, imr, mode);
2238 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2239 struct in_device *in_dev)
2241 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2245 /* any-source empty exclude case */
2246 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2247 iml->sfmode, 0, NULL, 0);
2249 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2250 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2251 RCU_INIT_POINTER(iml->sflist, NULL);
2252 /* decrease mem now to avoid the memleak warning */
2253 atomic_sub(struct_size(psf, sl_addr, psf->sl_max), &sk->sk_omem_alloc);
2254 kfree_rcu(psf, rcu);
2258 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2260 struct inet_sock *inet = inet_sk(sk);
2261 struct ip_mc_socklist *iml;
2262 struct ip_mc_socklist __rcu **imlp;
2263 struct in_device *in_dev;
2264 struct net *net = sock_net(sk);
2265 __be32 group = imr->imr_multiaddr.s_addr;
2267 int ret = -EADDRNOTAVAIL;
2271 in_dev = ip_mc_find_dev(net, imr);
2272 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2276 ifindex = imr->imr_ifindex;
2277 for (imlp = &inet->mc_list;
2278 (iml = rtnl_dereference(*imlp)) != NULL;
2279 imlp = &iml->next_rcu) {
2280 if (iml->multi.imr_multiaddr.s_addr != group)
2283 if (iml->multi.imr_ifindex != ifindex)
2285 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2286 iml->multi.imr_address.s_addr)
2289 (void) ip_mc_leave_src(sk, iml, in_dev);
2291 *imlp = iml->next_rcu;
2294 ip_mc_dec_group(in_dev, group);
2296 /* decrease mem now to avoid the memleak warning */
2297 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2298 kfree_rcu(iml, rcu);
2304 EXPORT_SYMBOL(ip_mc_leave_group);
2306 int ip_mc_source(int add, int omode, struct sock *sk, struct
2307 ip_mreq_source *mreqs, int ifindex)
2310 struct ip_mreqn imr;
2311 __be32 addr = mreqs->imr_multiaddr;
2312 struct ip_mc_socklist *pmc;
2313 struct in_device *in_dev = NULL;
2314 struct inet_sock *inet = inet_sk(sk);
2315 struct ip_sf_socklist *psl;
2316 struct net *net = sock_net(sk);
2320 if (!ipv4_is_multicast(addr))
2325 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2326 imr.imr_address.s_addr = mreqs->imr_interface;
2327 imr.imr_ifindex = ifindex;
2328 in_dev = ip_mc_find_dev(net, &imr);
2334 err = -EADDRNOTAVAIL;
2336 for_each_pmc_rtnl(inet, pmc) {
2337 if ((pmc->multi.imr_multiaddr.s_addr ==
2338 imr.imr_multiaddr.s_addr) &&
2339 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2342 if (!pmc) { /* must have a prior join */
2346 /* if a source filter was set, must be the same mode as before */
2348 if (pmc->sfmode != omode) {
2352 } else if (pmc->sfmode != omode) {
2353 /* allow mode switches for empty-set filters */
2354 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2355 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2357 pmc->sfmode = omode;
2360 psl = rtnl_dereference(pmc->sflist);
2363 goto done; /* err = -EADDRNOTAVAIL */
2365 for (i = 0; i < psl->sl_count; i++) {
2366 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2371 if (rv) /* source not found */
2372 goto done; /* err = -EADDRNOTAVAIL */
2374 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2375 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2380 /* update the interface filter */
2381 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2382 &mreqs->imr_sourceaddr, 1);
2384 for (j = i+1; j < psl->sl_count; j++)
2385 psl->sl_addr[j-1] = psl->sl_addr[j];
2390 /* else, add a new source to the filter */
2392 if (psl && psl->sl_count >= READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) {
2396 if (!psl || psl->sl_count == psl->sl_max) {
2397 struct ip_sf_socklist *newpsl;
2398 int count = IP_SFBLOCK;
2401 count += psl->sl_max;
2402 newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, count),
2408 newpsl->sl_max = count;
2409 newpsl->sl_count = count - IP_SFBLOCK;
2411 for (i = 0; i < psl->sl_count; i++)
2412 newpsl->sl_addr[i] = psl->sl_addr[i];
2413 /* decrease mem now to avoid the memleak warning */
2414 atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
2415 &sk->sk_omem_alloc);
2417 rcu_assign_pointer(pmc->sflist, newpsl);
2419 kfree_rcu(psl, rcu);
2422 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2423 for (i = 0; i < psl->sl_count; i++) {
2424 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2429 if (rv == 0) /* address already there is an error */
2431 for (j = psl->sl_count-1; j >= i; j--)
2432 psl->sl_addr[j+1] = psl->sl_addr[j];
2433 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2436 /* update the interface list */
2437 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2438 &mreqs->imr_sourceaddr, 1);
2441 err = ip_mc_leave_group(sk, &imr);
2445 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2448 struct ip_mreqn imr;
2449 __be32 addr = msf->imsf_multiaddr;
2450 struct ip_mc_socklist *pmc;
2451 struct in_device *in_dev;
2452 struct inet_sock *inet = inet_sk(sk);
2453 struct ip_sf_socklist *newpsl, *psl;
2454 struct net *net = sock_net(sk);
2457 if (!ipv4_is_multicast(addr))
2459 if (msf->imsf_fmode != MCAST_INCLUDE &&
2460 msf->imsf_fmode != MCAST_EXCLUDE)
2465 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2466 imr.imr_address.s_addr = msf->imsf_interface;
2467 imr.imr_ifindex = ifindex;
2468 in_dev = ip_mc_find_dev(net, &imr);
2475 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2476 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2481 for_each_pmc_rtnl(inet, pmc) {
2482 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2483 pmc->multi.imr_ifindex == imr.imr_ifindex)
2486 if (!pmc) { /* must have a prior join */
2490 if (msf->imsf_numsrc) {
2491 newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr,
2498 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2499 memcpy(newpsl->sl_addr, msf->imsf_slist_flex,
2500 flex_array_size(msf, imsf_slist_flex, msf->imsf_numsrc));
2501 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2502 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2504 sock_kfree_s(sk, newpsl,
2505 struct_size(newpsl, sl_addr,
2511 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2512 msf->imsf_fmode, 0, NULL, 0);
2514 psl = rtnl_dereference(pmc->sflist);
2516 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2517 psl->sl_count, psl->sl_addr, 0);
2518 /* decrease mem now to avoid the memleak warning */
2519 atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
2520 &sk->sk_omem_alloc);
2522 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2525 rcu_assign_pointer(pmc->sflist, newpsl);
2527 kfree_rcu(psl, rcu);
2528 pmc->sfmode = msf->imsf_fmode;
2532 err = ip_mc_leave_group(sk, &imr);
2535 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2536 sockptr_t optval, sockptr_t optlen)
2538 int err, len, count, copycount, msf_size;
2539 struct ip_mreqn imr;
2540 __be32 addr = msf->imsf_multiaddr;
2541 struct ip_mc_socklist *pmc;
2542 struct in_device *in_dev;
2543 struct inet_sock *inet = inet_sk(sk);
2544 struct ip_sf_socklist *psl;
2545 struct net *net = sock_net(sk);
2549 if (!ipv4_is_multicast(addr))
2552 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2553 imr.imr_address.s_addr = msf->imsf_interface;
2554 imr.imr_ifindex = 0;
2555 in_dev = ip_mc_find_dev(net, &imr);
2561 err = -EADDRNOTAVAIL;
2563 for_each_pmc_rtnl(inet, pmc) {
2564 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2565 pmc->multi.imr_ifindex == imr.imr_ifindex)
2568 if (!pmc) /* must have a prior join */
2570 msf->imsf_fmode = pmc->sfmode;
2571 psl = rtnl_dereference(pmc->sflist);
2575 count = psl->sl_count;
2577 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2578 len = flex_array_size(psl, sl_addr, copycount);
2579 msf->imsf_numsrc = count;
2580 msf_size = IP_MSFILTER_SIZE(copycount);
2581 if (copy_to_sockptr(optlen, &msf_size, sizeof(int)) ||
2582 copy_to_sockptr(optval, msf, IP_MSFILTER_SIZE(0))) {
2586 copy_to_sockptr_offset(optval,
2587 offsetof(struct ip_msfilter, imsf_slist_flex),
2595 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2596 sockptr_t optval, size_t ss_offset)
2598 int i, count, copycount;
2599 struct sockaddr_in *psin;
2601 struct ip_mc_socklist *pmc;
2602 struct inet_sock *inet = inet_sk(sk);
2603 struct ip_sf_socklist *psl;
2607 psin = (struct sockaddr_in *)&gsf->gf_group;
2608 if (psin->sin_family != AF_INET)
2610 addr = psin->sin_addr.s_addr;
2611 if (!ipv4_is_multicast(addr))
2614 for_each_pmc_rtnl(inet, pmc) {
2615 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2616 pmc->multi.imr_ifindex == gsf->gf_interface)
2619 if (!pmc) /* must have a prior join */
2620 return -EADDRNOTAVAIL;
2621 gsf->gf_fmode = pmc->sfmode;
2622 psl = rtnl_dereference(pmc->sflist);
2623 count = psl ? psl->sl_count : 0;
2624 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2625 gsf->gf_numsrc = count;
2626 for (i = 0; i < copycount; i++) {
2627 struct sockaddr_storage ss;
2629 psin = (struct sockaddr_in *)&ss;
2630 memset(&ss, 0, sizeof(ss));
2631 psin->sin_family = AF_INET;
2632 psin->sin_addr.s_addr = psl->sl_addr[i];
2633 if (copy_to_sockptr_offset(optval, ss_offset,
2636 ss_offset += sizeof(ss);
2642 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2644 int ip_mc_sf_allow(const struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2647 const struct inet_sock *inet = inet_sk(sk);
2648 struct ip_mc_socklist *pmc;
2649 struct ip_sf_socklist *psl;
2654 if (!ipv4_is_multicast(loc_addr))
2658 for_each_pmc_rcu(inet, pmc) {
2659 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2660 (pmc->multi.imr_ifindex == dif ||
2661 (sdif && pmc->multi.imr_ifindex == sdif)))
2664 ret = inet_test_bit(MC_ALL, sk);
2667 psl = rcu_dereference(pmc->sflist);
2668 ret = (pmc->sfmode == MCAST_EXCLUDE);
2672 for (i = 0; i < psl->sl_count; i++) {
2673 if (psl->sl_addr[i] == rmt_addr)
2677 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2679 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2689 * A socket is closing.
2692 void ip_mc_drop_socket(struct sock *sk)
2694 struct inet_sock *inet = inet_sk(sk);
2695 struct ip_mc_socklist *iml;
2696 struct net *net = sock_net(sk);
2702 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2703 struct in_device *in_dev;
2705 inet->mc_list = iml->next_rcu;
2706 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2707 (void) ip_mc_leave_src(sk, iml, in_dev);
2709 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2710 /* decrease mem now to avoid the memleak warning */
2711 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2712 kfree_rcu(iml, rcu);
2717 /* called with rcu_read_lock() */
2718 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2720 struct ip_mc_list *im;
2721 struct ip_mc_list __rcu **mc_hash;
2722 struct ip_sf_list *psf;
2725 mc_hash = rcu_dereference(in_dev->mc_hash);
2727 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2729 for (im = rcu_dereference(mc_hash[hash]);
2731 im = rcu_dereference(im->next_hash)) {
2732 if (im->multiaddr == mc_addr)
2736 for_each_pmc_rcu(in_dev, im) {
2737 if (im->multiaddr == mc_addr)
2741 if (im && proto == IPPROTO_IGMP) {
2745 spin_lock_bh(&im->lock);
2746 for (psf = im->sources; psf; psf = psf->sf_next) {
2747 if (psf->sf_inaddr == src_addr)
2751 rv = psf->sf_count[MCAST_INCLUDE] ||
2752 psf->sf_count[MCAST_EXCLUDE] !=
2753 im->sfcount[MCAST_EXCLUDE];
2755 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2756 spin_unlock_bh(&im->lock);
2758 rv = 1; /* unspecified source; tentatively allow */
2763 #if defined(CONFIG_PROC_FS)
2764 struct igmp_mc_iter_state {
2765 struct seq_net_private p;
2766 struct net_device *dev;
2767 struct in_device *in_dev;
2770 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2772 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2774 struct net *net = seq_file_net(seq);
2775 struct ip_mc_list *im = NULL;
2776 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2778 state->in_dev = NULL;
2779 for_each_netdev_rcu(net, state->dev) {
2780 struct in_device *in_dev;
2782 in_dev = __in_dev_get_rcu(state->dev);
2785 im = rcu_dereference(in_dev->mc_list);
2787 state->in_dev = in_dev;
2794 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2796 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2798 im = rcu_dereference(im->next_rcu);
2800 state->dev = next_net_device_rcu(state->dev);
2802 state->in_dev = NULL;
2805 state->in_dev = __in_dev_get_rcu(state->dev);
2808 im = rcu_dereference(state->in_dev->mc_list);
2813 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2815 struct ip_mc_list *im = igmp_mc_get_first(seq);
2817 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2819 return pos ? NULL : im;
2822 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2826 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2829 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2831 struct ip_mc_list *im;
2832 if (v == SEQ_START_TOKEN)
2833 im = igmp_mc_get_first(seq);
2835 im = igmp_mc_get_next(seq, v);
2840 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2843 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2845 state->in_dev = NULL;
2850 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2852 if (v == SEQ_START_TOKEN)
2854 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2856 struct ip_mc_list *im = v;
2857 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2861 #ifdef CONFIG_IP_MULTICAST
2862 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2863 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2869 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2870 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2871 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2874 delta = im->timer.expires - jiffies;
2876 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2877 im->multiaddr, im->users,
2879 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2885 static const struct seq_operations igmp_mc_seq_ops = {
2886 .start = igmp_mc_seq_start,
2887 .next = igmp_mc_seq_next,
2888 .stop = igmp_mc_seq_stop,
2889 .show = igmp_mc_seq_show,
2892 struct igmp_mcf_iter_state {
2893 struct seq_net_private p;
2894 struct net_device *dev;
2895 struct in_device *idev;
2896 struct ip_mc_list *im;
2899 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2901 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2903 struct net *net = seq_file_net(seq);
2904 struct ip_sf_list *psf = NULL;
2905 struct ip_mc_list *im = NULL;
2906 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2910 for_each_netdev_rcu(net, state->dev) {
2911 struct in_device *idev;
2912 idev = __in_dev_get_rcu(state->dev);
2913 if (unlikely(!idev))
2915 im = rcu_dereference(idev->mc_list);
2917 spin_lock_bh(&im->lock);
2924 spin_unlock_bh(&im->lock);
2930 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2932 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2936 spin_unlock_bh(&state->im->lock);
2937 state->im = state->im->next;
2938 while (!state->im) {
2939 state->dev = next_net_device_rcu(state->dev);
2944 state->idev = __in_dev_get_rcu(state->dev);
2947 state->im = rcu_dereference(state->idev->mc_list);
2951 spin_lock_bh(&state->im->lock);
2952 psf = state->im->sources;
2958 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2960 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2962 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2964 return pos ? NULL : psf;
2967 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2971 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2974 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2976 struct ip_sf_list *psf;
2977 if (v == SEQ_START_TOKEN)
2978 psf = igmp_mcf_get_first(seq);
2980 psf = igmp_mcf_get_next(seq, v);
2985 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2988 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2989 if (likely(state->im)) {
2990 spin_unlock_bh(&state->im->lock);
2998 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
3000 struct ip_sf_list *psf = v;
3001 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
3003 if (v == SEQ_START_TOKEN) {
3004 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
3008 "0x%08x %6lu %6lu\n",
3009 state->dev->ifindex, state->dev->name,
3010 ntohl(state->im->multiaddr),
3011 ntohl(psf->sf_inaddr),
3012 psf->sf_count[MCAST_INCLUDE],
3013 psf->sf_count[MCAST_EXCLUDE]);
3018 static const struct seq_operations igmp_mcf_seq_ops = {
3019 .start = igmp_mcf_seq_start,
3020 .next = igmp_mcf_seq_next,
3021 .stop = igmp_mcf_seq_stop,
3022 .show = igmp_mcf_seq_show,
3025 static int __net_init igmp_net_init(struct net *net)
3027 struct proc_dir_entry *pde;
3030 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3031 sizeof(struct igmp_mc_iter_state));
3034 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3035 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
3038 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3039 SOCK_DGRAM, 0, net);
3041 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3049 remove_proc_entry("mcfilter", net->proc_net);
3051 remove_proc_entry("igmp", net->proc_net);
3056 static void __net_exit igmp_net_exit(struct net *net)
3058 remove_proc_entry("mcfilter", net->proc_net);
3059 remove_proc_entry("igmp", net->proc_net);
3060 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3063 static struct pernet_operations igmp_net_ops = {
3064 .init = igmp_net_init,
3065 .exit = igmp_net_exit,
3069 static int igmp_netdev_event(struct notifier_block *this,
3070 unsigned long event, void *ptr)
3072 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3073 struct in_device *in_dev;
3076 case NETDEV_RESEND_IGMP:
3077 in_dev = __in_dev_get_rtnl(dev);
3079 ip_mc_rejoin_groups(in_dev);
3087 static struct notifier_block igmp_notifier = {
3088 .notifier_call = igmp_netdev_event,
3091 int __init igmp_mc_init(void)
3093 #if defined(CONFIG_PROC_FS)
3096 err = register_pernet_subsys(&igmp_net_ops);
3099 err = register_netdevice_notifier(&igmp_notifier);
3101 goto reg_notif_fail;
3105 unregister_pernet_subsys(&igmp_net_ops);
3108 return register_netdevice_notifier(&igmp_notifier);