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 = prandom_u32() % max_delay;
219 if (!mod_timer(&im->timer, jiffies+tv+2))
220 refcount_inc(&im->refcnt);
223 static void igmp_gq_start_timer(struct in_device *in_dev)
225 int tv = prandom_u32() % in_dev->mr_maxdelay;
226 unsigned long exp = jiffies + tv + 2;
228 if (in_dev->mr_gq_running &&
229 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
232 in_dev->mr_gq_running = 1;
233 if (!mod_timer(&in_dev->mr_gq_timer, exp))
237 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
239 int tv = prandom_u32() % delay;
241 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
245 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
247 spin_lock_bh(&im->lock);
248 im->unsolicit_count = 0;
249 if (del_timer(&im->timer)) {
250 if ((long)(im->timer.expires-jiffies) < max_delay) {
251 add_timer(&im->timer);
253 spin_unlock_bh(&im->lock);
256 refcount_dec(&im->refcnt);
258 igmp_start_timer(im, max_delay);
259 spin_unlock_bh(&im->lock);
264 * Send an IGMP report.
267 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
270 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
271 int gdeleted, int sdeleted)
274 case IGMPV3_MODE_IS_INCLUDE:
275 case IGMPV3_MODE_IS_EXCLUDE:
276 if (gdeleted || sdeleted)
278 if (!(pmc->gsquery && !psf->sf_gsresp)) {
279 if (pmc->sfmode == MCAST_INCLUDE)
281 /* don't include if this source is excluded
284 if (psf->sf_count[MCAST_INCLUDE])
285 return type == IGMPV3_MODE_IS_INCLUDE;
286 return pmc->sfcount[MCAST_EXCLUDE] ==
287 psf->sf_count[MCAST_EXCLUDE];
290 case IGMPV3_CHANGE_TO_INCLUDE:
291 if (gdeleted || sdeleted)
293 return psf->sf_count[MCAST_INCLUDE] != 0;
294 case IGMPV3_CHANGE_TO_EXCLUDE:
295 if (gdeleted || sdeleted)
297 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
298 psf->sf_count[MCAST_INCLUDE])
300 return pmc->sfcount[MCAST_EXCLUDE] ==
301 psf->sf_count[MCAST_EXCLUDE];
302 case IGMPV3_ALLOW_NEW_SOURCES:
303 if (gdeleted || !psf->sf_crcount)
305 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
306 case IGMPV3_BLOCK_OLD_SOURCES:
307 if (pmc->sfmode == MCAST_INCLUDE)
308 return gdeleted || (psf->sf_crcount && sdeleted);
309 return psf->sf_crcount && !gdeleted && !sdeleted;
315 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
317 struct ip_sf_list *psf;
320 for (psf = pmc->sources; psf; psf = psf->sf_next) {
321 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
328 /* source address selection per RFC 3376 section 4.2.13 */
329 static __be32 igmpv3_get_srcaddr(struct net_device *dev,
330 const struct flowi4 *fl4)
332 struct in_device *in_dev = __in_dev_get_rcu(dev);
333 const struct in_ifaddr *ifa;
336 return htonl(INADDR_ANY);
338 in_dev_for_each_ifa_rcu(ifa, in_dev) {
339 if (fl4->saddr == ifa->ifa_local)
343 return htonl(INADDR_ANY);
346 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
351 struct igmpv3_report *pig;
352 struct net *net = dev_net(dev);
354 int hlen = LL_RESERVED_SPACE(dev);
355 int tlen = dev->needed_tailroom;
356 unsigned int size = mtu;
359 skb = alloc_skb(size + hlen + tlen,
360 GFP_ATOMIC | __GFP_NOWARN);
367 skb->priority = TC_PRIO_CONTROL;
369 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
371 IPPROTO_IGMP, 0, dev->ifindex);
377 skb_dst_set(skb, &rt->dst);
380 skb_reserve(skb, hlen);
381 skb_tailroom_reserve(skb, mtu, tlen);
383 skb_reset_network_header(skb);
385 skb_put(skb, sizeof(struct iphdr) + 4);
388 pip->ihl = (sizeof(struct iphdr)+4)>>2;
390 pip->frag_off = htons(IP_DF);
392 pip->daddr = fl4.daddr;
395 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
398 pip->protocol = IPPROTO_IGMP;
399 pip->tot_len = 0; /* filled in later */
400 ip_select_ident(net, skb, NULL);
401 ((u8 *)&pip[1])[0] = IPOPT_RA;
402 ((u8 *)&pip[1])[1] = 4;
403 ((u8 *)&pip[1])[2] = 0;
404 ((u8 *)&pip[1])[3] = 0;
406 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
407 skb_put(skb, sizeof(*pig));
408 pig = igmpv3_report_hdr(skb);
409 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
417 static int igmpv3_sendpack(struct sk_buff *skb)
419 struct igmphdr *pig = igmp_hdr(skb);
420 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
422 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
424 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
427 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
429 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
432 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
433 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
435 struct net_device *dev = pmc->interface->dev;
436 struct igmpv3_report *pih;
437 struct igmpv3_grec *pgr;
440 skb = igmpv3_newpack(dev, mtu);
444 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
445 pgr->grec_type = type;
446 pgr->grec_auxwords = 0;
448 pgr->grec_mca = pmc->multiaddr;
449 pih = igmpv3_report_hdr(skb);
450 pih->ngrec = htons(ntohs(pih->ngrec)+1);
455 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
457 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
458 int type, int gdeleted, int sdeleted)
460 struct net_device *dev = pmc->interface->dev;
461 struct net *net = dev_net(dev);
462 struct igmpv3_report *pih;
463 struct igmpv3_grec *pgr = NULL;
464 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
465 int scount, stotal, first, isquery, truncate;
468 if (pmc->multiaddr == IGMP_ALL_HOSTS)
470 if (ipv4_is_local_multicast(pmc->multiaddr) &&
471 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
474 mtu = READ_ONCE(dev->mtu);
475 if (mtu < IPV4_MIN_MTU)
478 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
479 type == IGMPV3_MODE_IS_EXCLUDE;
480 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
481 type == IGMPV3_CHANGE_TO_EXCLUDE;
485 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
490 pih = skb ? igmpv3_report_hdr(skb) : NULL;
492 /* EX and TO_EX get a fresh packet, if needed */
494 if (pih && pih->ngrec &&
495 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
497 igmpv3_sendpack(skb);
498 skb = igmpv3_newpack(dev, mtu);
503 for (psf = *psf_list; psf; psf = psf_next) {
506 psf_next = psf->sf_next;
508 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
513 /* Based on RFC3376 5.1. Should not send source-list change
514 * records when there is a filter mode change.
516 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
517 (!gdeleted && pmc->crcount)) &&
518 (type == IGMPV3_ALLOW_NEW_SOURCES ||
519 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
520 goto decrease_sf_crcount;
522 /* clear marks on query responses */
526 if (AVAILABLE(skb) < sizeof(__be32) +
527 first*sizeof(struct igmpv3_grec)) {
528 if (truncate && !first)
529 break; /* truncate these */
531 pgr->grec_nsrcs = htons(scount);
533 igmpv3_sendpack(skb);
534 skb = igmpv3_newpack(dev, mtu);
539 skb = add_grhead(skb, pmc, type, &pgr, mtu);
544 psrc = skb_put(skb, sizeof(__be32));
545 *psrc = psf->sf_inaddr;
547 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
548 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
551 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
553 psf_prev->sf_next = psf->sf_next;
555 *psf_list = psf->sf_next;
565 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
566 type == IGMPV3_BLOCK_OLD_SOURCES)
568 if (pmc->crcount || isquery) {
569 /* make sure we have room for group header */
570 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
571 igmpv3_sendpack(skb);
572 skb = NULL; /* add_grhead will get a new one */
574 skb = add_grhead(skb, pmc, type, &pgr, mtu);
578 pgr->grec_nsrcs = htons(scount);
581 pmc->gsquery = 0; /* clear query state on report */
585 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
587 struct sk_buff *skb = NULL;
588 struct net *net = dev_net(in_dev->dev);
593 for_each_pmc_rcu(in_dev, pmc) {
594 if (pmc->multiaddr == IGMP_ALL_HOSTS)
596 if (ipv4_is_local_multicast(pmc->multiaddr) &&
597 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
599 spin_lock_bh(&pmc->lock);
600 if (pmc->sfcount[MCAST_EXCLUDE])
601 type = IGMPV3_MODE_IS_EXCLUDE;
603 type = IGMPV3_MODE_IS_INCLUDE;
604 skb = add_grec(skb, pmc, type, 0, 0);
605 spin_unlock_bh(&pmc->lock);
609 spin_lock_bh(&pmc->lock);
610 if (pmc->sfcount[MCAST_EXCLUDE])
611 type = IGMPV3_MODE_IS_EXCLUDE;
613 type = IGMPV3_MODE_IS_INCLUDE;
614 skb = add_grec(skb, pmc, type, 0, 0);
615 spin_unlock_bh(&pmc->lock);
619 return igmpv3_sendpack(skb);
623 * remove zero-count source records from a source filter list
625 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
627 struct ip_sf_list *psf_prev, *psf_next, *psf;
630 for (psf = *ppsf; psf; psf = psf_next) {
631 psf_next = psf->sf_next;
632 if (psf->sf_crcount == 0) {
634 psf_prev->sf_next = psf->sf_next;
636 *ppsf = psf->sf_next;
643 static void kfree_pmc(struct ip_mc_list *pmc)
645 ip_sf_list_clear_all(pmc->sources);
646 ip_sf_list_clear_all(pmc->tomb);
650 static void igmpv3_send_cr(struct in_device *in_dev)
652 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
653 struct sk_buff *skb = NULL;
657 spin_lock_bh(&in_dev->mc_tomb_lock);
661 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
662 pmc_next = pmc->next;
663 if (pmc->sfmode == MCAST_INCLUDE) {
664 type = IGMPV3_BLOCK_OLD_SOURCES;
665 dtype = IGMPV3_BLOCK_OLD_SOURCES;
666 skb = add_grec(skb, pmc, type, 1, 0);
667 skb = add_grec(skb, pmc, dtype, 1, 1);
670 if (pmc->sfmode == MCAST_EXCLUDE) {
671 type = IGMPV3_CHANGE_TO_INCLUDE;
672 skb = add_grec(skb, pmc, type, 1, 0);
675 if (pmc->crcount == 0) {
676 igmpv3_clear_zeros(&pmc->tomb);
677 igmpv3_clear_zeros(&pmc->sources);
680 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
682 pmc_prev->next = pmc_next;
684 in_dev->mc_tomb = pmc_next;
685 in_dev_put(pmc->interface);
690 spin_unlock_bh(&in_dev->mc_tomb_lock);
693 for_each_pmc_rcu(in_dev, pmc) {
694 spin_lock_bh(&pmc->lock);
695 if (pmc->sfcount[MCAST_EXCLUDE]) {
696 type = IGMPV3_BLOCK_OLD_SOURCES;
697 dtype = IGMPV3_ALLOW_NEW_SOURCES;
699 type = IGMPV3_ALLOW_NEW_SOURCES;
700 dtype = IGMPV3_BLOCK_OLD_SOURCES;
702 skb = add_grec(skb, pmc, type, 0, 0);
703 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
705 /* filter mode changes */
707 if (pmc->sfmode == MCAST_EXCLUDE)
708 type = IGMPV3_CHANGE_TO_EXCLUDE;
710 type = IGMPV3_CHANGE_TO_INCLUDE;
711 skb = add_grec(skb, pmc, type, 0, 0);
714 spin_unlock_bh(&pmc->lock);
720 (void) igmpv3_sendpack(skb);
723 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
730 struct net_device *dev = in_dev->dev;
731 struct net *net = dev_net(dev);
732 __be32 group = pmc ? pmc->multiaddr : 0;
737 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
738 return igmpv3_send_report(in_dev, pmc);
740 if (ipv4_is_local_multicast(group) &&
741 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
744 if (type == IGMP_HOST_LEAVE_MESSAGE)
745 dst = IGMP_ALL_ROUTER;
749 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
751 IPPROTO_IGMP, 0, dev->ifindex);
755 hlen = LL_RESERVED_SPACE(dev);
756 tlen = dev->needed_tailroom;
757 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
762 skb->priority = TC_PRIO_CONTROL;
764 skb_dst_set(skb, &rt->dst);
766 skb_reserve(skb, hlen);
768 skb_reset_network_header(skb);
770 skb_put(skb, sizeof(struct iphdr) + 4);
773 iph->ihl = (sizeof(struct iphdr)+4)>>2;
775 iph->frag_off = htons(IP_DF);
778 iph->saddr = fl4.saddr;
779 iph->protocol = IPPROTO_IGMP;
780 ip_select_ident(net, skb, NULL);
781 ((u8 *)&iph[1])[0] = IPOPT_RA;
782 ((u8 *)&iph[1])[1] = 4;
783 ((u8 *)&iph[1])[2] = 0;
784 ((u8 *)&iph[1])[3] = 0;
786 ih = skb_put(skb, sizeof(struct igmphdr));
791 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
793 return ip_local_out(net, skb->sk, skb);
796 static void igmp_gq_timer_expire(struct timer_list *t)
798 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
800 in_dev->mr_gq_running = 0;
801 igmpv3_send_report(in_dev, NULL);
805 static void igmp_ifc_timer_expire(struct timer_list *t)
807 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
810 igmpv3_send_cr(in_dev);
812 mr_ifc_count = READ_ONCE(in_dev->mr_ifc_count);
815 if (cmpxchg(&in_dev->mr_ifc_count,
817 mr_ifc_count - 1) != mr_ifc_count)
819 igmp_ifc_start_timer(in_dev,
820 unsolicited_report_interval(in_dev));
825 static void igmp_ifc_event(struct in_device *in_dev)
827 struct net *net = dev_net(in_dev->dev);
828 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
830 WRITE_ONCE(in_dev->mr_ifc_count, in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv);
831 igmp_ifc_start_timer(in_dev, 1);
835 static void igmp_timer_expire(struct timer_list *t)
837 struct ip_mc_list *im = from_timer(im, t, timer);
838 struct in_device *in_dev = im->interface;
840 spin_lock(&im->lock);
843 if (im->unsolicit_count && --im->unsolicit_count)
844 igmp_start_timer(im, unsolicited_report_interval(in_dev));
847 spin_unlock(&im->lock);
849 if (IGMP_V1_SEEN(in_dev))
850 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
851 else if (IGMP_V2_SEEN(in_dev))
852 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
854 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
859 /* mark EXCLUDE-mode sources */
860 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
862 struct ip_sf_list *psf;
866 for (psf = pmc->sources; psf; psf = psf->sf_next) {
869 for (i = 0; i < nsrcs; i++) {
870 /* skip inactive filters */
871 if (psf->sf_count[MCAST_INCLUDE] ||
872 pmc->sfcount[MCAST_EXCLUDE] !=
873 psf->sf_count[MCAST_EXCLUDE])
875 if (srcs[i] == psf->sf_inaddr) {
882 if (scount == nsrcs) /* all sources excluded */
887 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
889 struct ip_sf_list *psf;
892 if (pmc->sfmode == MCAST_EXCLUDE)
893 return igmp_xmarksources(pmc, nsrcs, srcs);
895 /* mark INCLUDE-mode sources */
897 for (psf = pmc->sources; psf; psf = psf->sf_next) {
900 for (i = 0; i < nsrcs; i++)
901 if (srcs[i] == psf->sf_inaddr) {
915 /* return true if packet was dropped */
916 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
918 struct ip_mc_list *im;
919 struct net *net = dev_net(in_dev->dev);
921 /* Timers are only set for non-local groups */
923 if (group == IGMP_ALL_HOSTS)
925 if (ipv4_is_local_multicast(group) &&
926 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
930 for_each_pmc_rcu(in_dev, im) {
931 if (im->multiaddr == group) {
940 /* return true if packet was dropped */
941 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
944 struct igmphdr *ih = igmp_hdr(skb);
945 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
946 struct ip_mc_list *im;
947 __be32 group = ih->group;
950 struct net *net = dev_net(in_dev->dev);
955 /* Alas, old v1 router presents here. */
957 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
958 in_dev->mr_v1_seen = jiffies +
959 (in_dev->mr_qrv * in_dev->mr_qi) +
963 /* v2 router present */
964 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
965 in_dev->mr_v2_seen = jiffies +
966 (in_dev->mr_qrv * in_dev->mr_qi) +
969 /* cancel the interface change timer */
970 WRITE_ONCE(in_dev->mr_ifc_count, 0);
971 if (del_timer(&in_dev->mr_ifc_timer))
972 __in_dev_put(in_dev);
973 /* clear deleted report items */
974 igmpv3_clear_delrec(in_dev);
975 } else if (len < 12) {
976 return true; /* ignore bogus packet; freed by caller */
977 } else if (IGMP_V1_SEEN(in_dev)) {
978 /* This is a v3 query with v1 queriers present */
979 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
981 } else if (IGMP_V2_SEEN(in_dev)) {
982 /* this is a v3 query with v2 queriers present;
983 * Interpretation of the max_delay code is problematic here.
984 * A real v2 host would use ih_code directly, while v3 has a
985 * different encoding. We use the v3 encoding as more likely
986 * to be intended in a v3 query.
988 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
990 max_delay = 1; /* can't mod w/ 0 */
992 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
995 ih3 = igmpv3_query_hdr(skb);
997 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
998 + ntohs(ih3->nsrcs)*sizeof(__be32)))
1000 ih3 = igmpv3_query_hdr(skb);
1003 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
1005 max_delay = 1; /* can't mod w/ 0 */
1006 in_dev->mr_maxdelay = max_delay;
1008 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
1009 * received value was zero, use the default or statically
1012 in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
1013 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
1015 /* RFC3376, 8.3. Query Response Interval:
1016 * The number of seconds represented by the [Query Response
1017 * Interval] must be less than the [Query Interval].
1019 if (in_dev->mr_qri >= in_dev->mr_qi)
1020 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
1022 if (!group) { /* general query */
1024 return true; /* no sources allowed */
1025 igmp_gq_start_timer(in_dev);
1028 /* mark sources to include, if group & source-specific */
1029 mark = ih3->nsrcs != 0;
1033 * - Start the timers in all of our membership records
1034 * that the query applies to for the interface on
1035 * which the query arrived excl. those that belong
1036 * to a "local" group (224.0.0.X)
1037 * - For timers already running check if they need to
1039 * - Use the igmp->igmp_code field as the maximum
1043 for_each_pmc_rcu(in_dev, im) {
1046 if (group && group != im->multiaddr)
1048 if (im->multiaddr == IGMP_ALL_HOSTS)
1050 if (ipv4_is_local_multicast(im->multiaddr) &&
1051 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1053 spin_lock_bh(&im->lock);
1055 im->gsquery = im->gsquery && mark;
1058 changed = !im->gsquery ||
1059 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1060 spin_unlock_bh(&im->lock);
1062 igmp_mod_timer(im, max_delay);
1068 /* called in rcu_read_lock() section */
1069 int igmp_rcv(struct sk_buff *skb)
1071 /* This basically follows the spec line by line -- see RFC1112 */
1073 struct net_device *dev = skb->dev;
1074 struct in_device *in_dev;
1076 bool dropped = true;
1078 if (netif_is_l3_master(dev)) {
1079 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1084 in_dev = __in_dev_get_rcu(dev);
1088 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1091 if (skb_checksum_simple_validate(skb))
1096 case IGMP_HOST_MEMBERSHIP_QUERY:
1097 dropped = igmp_heard_query(in_dev, skb, len);
1099 case IGMP_HOST_MEMBERSHIP_REPORT:
1100 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1101 /* Is it our report looped back? */
1102 if (rt_is_output_route(skb_rtable(skb)))
1104 /* don't rely on MC router hearing unicast reports */
1105 if (skb->pkt_type == PACKET_MULTICAST ||
1106 skb->pkt_type == PACKET_BROADCAST)
1107 dropped = igmp_heard_report(in_dev, ih->group);
1110 #ifdef CONFIG_IP_PIMSM_V1
1111 return pim_rcv_v1(skb);
1113 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1116 case IGMP_HOST_LEAVE_MESSAGE:
1118 case IGMP_MTRACE_RESP:
1136 * Add a filter to a device
1139 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1141 char buf[MAX_ADDR_LEN];
1142 struct net_device *dev = in_dev->dev;
1144 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1145 We will get multicast token leakage, when IFF_MULTICAST
1146 is changed. This check should be done in ndo_set_rx_mode
1147 routine. Something sort of:
1148 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1151 if (arp_mc_map(addr, buf, dev, 0) == 0)
1152 dev_mc_add(dev, buf);
1156 * Remove a filter from a device
1159 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1161 char buf[MAX_ADDR_LEN];
1162 struct net_device *dev = in_dev->dev;
1164 if (arp_mc_map(addr, buf, dev, 0) == 0)
1165 dev_mc_del(dev, buf);
1168 #ifdef CONFIG_IP_MULTICAST
1170 * deleted ip_mc_list manipulation
1172 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);
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, gfp_t gfp)
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) &&
1303 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1306 reporter = im->reporter;
1307 igmp_stop_timer(im);
1309 if (!in_dev->dead) {
1310 if (IGMP_V1_SEEN(in_dev))
1312 if (IGMP_V2_SEEN(in_dev)) {
1314 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1318 igmpv3_add_delrec(in_dev, im, gfp);
1320 igmp_ifc_event(in_dev);
1325 static void igmp_group_dropped(struct ip_mc_list *im)
1327 __igmp_group_dropped(im, GFP_KERNEL);
1330 static void igmp_group_added(struct ip_mc_list *im)
1332 struct in_device *in_dev = im->interface;
1333 #ifdef CONFIG_IP_MULTICAST
1334 struct net *net = dev_net(in_dev->dev);
1337 if (im->loaded == 0) {
1339 ip_mc_filter_add(in_dev, im->multiaddr);
1342 #ifdef CONFIG_IP_MULTICAST
1343 if (im->multiaddr == IGMP_ALL_HOSTS)
1345 if (ipv4_is_local_multicast(im->multiaddr) &&
1346 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1352 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1353 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1354 spin_lock_bh(&im->lock);
1355 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1356 spin_unlock_bh(&im->lock);
1361 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1362 * not send filter-mode change record as the mode should be from
1365 if (im->sfmode == MCAST_EXCLUDE)
1366 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1368 igmp_ifc_event(in_dev);
1374 * Multicast list managers
1377 static u32 ip_mc_hash(const struct ip_mc_list *im)
1379 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1382 static void ip_mc_hash_add(struct in_device *in_dev,
1383 struct ip_mc_list *im)
1385 struct ip_mc_list __rcu **mc_hash;
1388 mc_hash = rtnl_dereference(in_dev->mc_hash);
1390 hash = ip_mc_hash(im);
1391 im->next_hash = mc_hash[hash];
1392 rcu_assign_pointer(mc_hash[hash], im);
1396 /* do not use a hash table for small number of items */
1397 if (in_dev->mc_count < 4)
1400 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1405 for_each_pmc_rtnl(in_dev, im) {
1406 hash = ip_mc_hash(im);
1407 im->next_hash = mc_hash[hash];
1408 RCU_INIT_POINTER(mc_hash[hash], im);
1411 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1414 static void ip_mc_hash_remove(struct in_device *in_dev,
1415 struct ip_mc_list *im)
1417 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1418 struct ip_mc_list *aux;
1422 mc_hash += ip_mc_hash(im);
1423 while ((aux = rtnl_dereference(*mc_hash)) != im)
1424 mc_hash = &aux->next_hash;
1425 *mc_hash = im->next_hash;
1430 * A socket has joined a multicast group on device dev.
1432 static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1433 unsigned int mode, gfp_t gfp)
1435 struct ip_mc_list *im;
1439 for_each_pmc_rtnl(in_dev, im) {
1440 if (im->multiaddr == addr) {
1442 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
1447 im = kzalloc(sizeof(*im), gfp);
1452 im->interface = in_dev;
1453 in_dev_hold(in_dev);
1454 im->multiaddr = addr;
1455 /* initial mode is (EX, empty) */
1457 im->sfcount[mode] = 1;
1458 refcount_set(&im->refcnt, 1);
1459 spin_lock_init(&im->lock);
1460 #ifdef CONFIG_IP_MULTICAST
1461 timer_setup(&im->timer, igmp_timer_expire, 0);
1464 im->next_rcu = in_dev->mc_list;
1466 rcu_assign_pointer(in_dev->mc_list, im);
1468 ip_mc_hash_add(in_dev, im);
1470 #ifdef CONFIG_IP_MULTICAST
1471 igmpv3_del_delrec(in_dev, im);
1473 igmp_group_added(im);
1475 ip_rt_multicast_event(in_dev);
1480 void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1482 ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
1484 EXPORT_SYMBOL(__ip_mc_inc_group);
1486 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1488 __ip_mc_inc_group(in_dev, addr, GFP_KERNEL);
1490 EXPORT_SYMBOL(ip_mc_inc_group);
1492 static int ip_mc_check_iphdr(struct sk_buff *skb)
1494 const struct iphdr *iph;
1496 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1498 if (!pskb_may_pull(skb, offset))
1503 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1506 offset += ip_hdrlen(skb) - sizeof(*iph);
1508 if (!pskb_may_pull(skb, offset))
1513 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1516 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1517 if (skb->len < len || len < offset)
1520 skb_set_transport_header(skb, offset);
1525 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1527 unsigned int len = skb_transport_offset(skb);
1529 len += sizeof(struct igmpv3_report);
1531 return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
1534 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1536 unsigned int transport_len = ip_transport_len(skb);
1540 if (transport_len != sizeof(struct igmphdr)) {
1542 if (transport_len < sizeof(struct igmpv3_query))
1545 len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1546 if (!ip_mc_may_pull(skb, len))
1550 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1551 * all-systems destination addresses (224.0.0.1) for general queries
1553 if (!igmp_hdr(skb)->group &&
1554 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1560 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1562 switch (igmp_hdr(skb)->type) {
1563 case IGMP_HOST_LEAVE_MESSAGE:
1564 case IGMP_HOST_MEMBERSHIP_REPORT:
1565 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1567 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1568 return ip_mc_check_igmp_reportv3(skb);
1569 case IGMP_HOST_MEMBERSHIP_QUERY:
1570 return ip_mc_check_igmp_query(skb);
1576 static __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1578 return skb_checksum_simple_validate(skb);
1581 static int ip_mc_check_igmp_csum(struct sk_buff *skb)
1583 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1584 unsigned int transport_len = ip_transport_len(skb);
1585 struct sk_buff *skb_chk;
1587 if (!ip_mc_may_pull(skb, len))
1590 skb_chk = skb_checksum_trimmed(skb, transport_len,
1591 ip_mc_validate_checksum);
1602 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1603 * @skb: the skb to validate
1605 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1606 * skb transport header accordingly and returns zero.
1608 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1610 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1611 * -ENOMEM: A memory allocation failure happened.
1613 * Caller needs to set the skb network header and free any returned skb if it
1614 * differs from the provided skb.
1616 int ip_mc_check_igmp(struct sk_buff *skb)
1618 int ret = ip_mc_check_iphdr(skb);
1623 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1626 ret = ip_mc_check_igmp_csum(skb);
1630 return ip_mc_check_igmp_msg(skb);
1632 EXPORT_SYMBOL(ip_mc_check_igmp);
1635 * Resend IGMP JOIN report; used by netdev notifier.
1637 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1639 #ifdef CONFIG_IP_MULTICAST
1640 struct ip_mc_list *im;
1642 struct net *net = dev_net(in_dev->dev);
1646 for_each_pmc_rtnl(in_dev, im) {
1647 if (im->multiaddr == IGMP_ALL_HOSTS)
1649 if (ipv4_is_local_multicast(im->multiaddr) &&
1650 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1653 /* a failover is happening and switches
1654 * must be notified immediately
1656 if (IGMP_V1_SEEN(in_dev))
1657 type = IGMP_HOST_MEMBERSHIP_REPORT;
1658 else if (IGMP_V2_SEEN(in_dev))
1659 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1661 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1662 igmp_send_report(in_dev, im, type);
1668 * A socket has left a multicast group on device dev
1671 void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1673 struct ip_mc_list *i;
1674 struct ip_mc_list __rcu **ip;
1678 for (ip = &in_dev->mc_list;
1679 (i = rtnl_dereference(*ip)) != NULL;
1680 ip = &i->next_rcu) {
1681 if (i->multiaddr == addr) {
1682 if (--i->users == 0) {
1683 ip_mc_hash_remove(in_dev, i);
1686 __igmp_group_dropped(i, gfp);
1690 ip_rt_multicast_event(in_dev);
1699 EXPORT_SYMBOL(__ip_mc_dec_group);
1701 /* Device changing type */
1703 void ip_mc_unmap(struct in_device *in_dev)
1705 struct ip_mc_list *pmc;
1709 for_each_pmc_rtnl(in_dev, pmc)
1710 igmp_group_dropped(pmc);
1713 void ip_mc_remap(struct in_device *in_dev)
1715 struct ip_mc_list *pmc;
1719 for_each_pmc_rtnl(in_dev, pmc) {
1720 #ifdef CONFIG_IP_MULTICAST
1721 igmpv3_del_delrec(in_dev, pmc);
1723 igmp_group_added(pmc);
1727 /* Device going down */
1729 void ip_mc_down(struct in_device *in_dev)
1731 struct ip_mc_list *pmc;
1735 for_each_pmc_rtnl(in_dev, pmc)
1736 igmp_group_dropped(pmc);
1738 #ifdef CONFIG_IP_MULTICAST
1739 WRITE_ONCE(in_dev->mr_ifc_count, 0);
1740 if (del_timer(&in_dev->mr_ifc_timer))
1741 __in_dev_put(in_dev);
1742 in_dev->mr_gq_running = 0;
1743 if (del_timer(&in_dev->mr_gq_timer))
1744 __in_dev_put(in_dev);
1747 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1750 #ifdef CONFIG_IP_MULTICAST
1751 static void ip_mc_reset(struct in_device *in_dev)
1753 struct net *net = dev_net(in_dev->dev);
1755 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1756 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1757 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1760 static void ip_mc_reset(struct in_device *in_dev)
1765 void ip_mc_init_dev(struct in_device *in_dev)
1769 #ifdef CONFIG_IP_MULTICAST
1770 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1771 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
1773 ip_mc_reset(in_dev);
1775 spin_lock_init(&in_dev->mc_tomb_lock);
1778 /* Device going up */
1780 void ip_mc_up(struct in_device *in_dev)
1782 struct ip_mc_list *pmc;
1786 ip_mc_reset(in_dev);
1787 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1789 for_each_pmc_rtnl(in_dev, pmc) {
1790 #ifdef CONFIG_IP_MULTICAST
1791 igmpv3_del_delrec(in_dev, pmc);
1793 igmp_group_added(pmc);
1798 * Device is about to be destroyed: clean up.
1801 void ip_mc_destroy_dev(struct in_device *in_dev)
1803 struct ip_mc_list *i;
1807 /* Deactivate timers */
1809 #ifdef CONFIG_IP_MULTICAST
1810 igmpv3_clear_delrec(in_dev);
1813 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1814 in_dev->mc_list = i->next_rcu;
1821 /* RTNL is locked */
1822 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1824 struct net_device *dev = NULL;
1825 struct in_device *idev = NULL;
1827 if (imr->imr_ifindex) {
1828 idev = inetdev_by_index(net, imr->imr_ifindex);
1831 if (imr->imr_address.s_addr) {
1832 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1838 struct rtable *rt = ip_route_output(net,
1839 imr->imr_multiaddr.s_addr,
1847 imr->imr_ifindex = dev->ifindex;
1848 idev = __in_dev_get_rtnl(dev);
1854 * Join a socket to a group
1857 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1860 struct ip_sf_list *psf, *psf_prev;
1864 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1865 if (psf->sf_inaddr == *psfsrc)
1869 if (!psf || psf->sf_count[sfmode] == 0) {
1870 /* source filter not found, or count wrong => bug */
1873 psf->sf_count[sfmode]--;
1874 if (psf->sf_count[sfmode] == 0) {
1875 ip_rt_multicast_event(pmc->interface);
1877 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1878 #ifdef CONFIG_IP_MULTICAST
1879 struct in_device *in_dev = pmc->interface;
1880 struct net *net = dev_net(in_dev->dev);
1883 /* no more filters for this source */
1885 psf_prev->sf_next = psf->sf_next;
1887 pmc->sources = psf->sf_next;
1888 #ifdef CONFIG_IP_MULTICAST
1889 if (psf->sf_oldin &&
1890 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1891 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1892 psf->sf_next = pmc->tomb;
1902 #ifndef CONFIG_IP_MULTICAST
1903 #define igmp_ifc_event(x) do { } while (0)
1906 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1907 int sfcount, __be32 *psfsrc, int delta)
1909 struct ip_mc_list *pmc;
1916 for_each_pmc_rcu(in_dev, pmc) {
1917 if (*pmca == pmc->multiaddr)
1921 /* MCA not found?? bug */
1925 spin_lock_bh(&pmc->lock);
1927 #ifdef CONFIG_IP_MULTICAST
1932 if (!pmc->sfcount[sfmode])
1934 pmc->sfcount[sfmode]--;
1937 for (i = 0; i < sfcount; i++) {
1938 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1940 changerec |= rv > 0;
1944 if (pmc->sfmode == MCAST_EXCLUDE &&
1945 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1946 pmc->sfcount[MCAST_INCLUDE]) {
1947 #ifdef CONFIG_IP_MULTICAST
1948 struct ip_sf_list *psf;
1949 struct net *net = dev_net(in_dev->dev);
1952 /* filter mode change */
1953 pmc->sfmode = MCAST_INCLUDE;
1954 #ifdef CONFIG_IP_MULTICAST
1955 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1956 WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
1957 for (psf = pmc->sources; psf; psf = psf->sf_next)
1958 psf->sf_crcount = 0;
1959 igmp_ifc_event(pmc->interface);
1960 } else if (sf_setstate(pmc) || changerec) {
1961 igmp_ifc_event(pmc->interface);
1965 spin_unlock_bh(&pmc->lock);
1970 * Add multicast single-source filter to the interface list
1972 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1975 struct ip_sf_list *psf, *psf_prev;
1978 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1979 if (psf->sf_inaddr == *psfsrc)
1984 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1987 psf->sf_inaddr = *psfsrc;
1989 psf_prev->sf_next = psf;
1993 psf->sf_count[sfmode]++;
1994 if (psf->sf_count[sfmode] == 1) {
1995 ip_rt_multicast_event(pmc->interface);
2000 #ifdef CONFIG_IP_MULTICAST
2001 static void sf_markstate(struct ip_mc_list *pmc)
2003 struct ip_sf_list *psf;
2004 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2006 for (psf = pmc->sources; psf; psf = psf->sf_next)
2007 if (pmc->sfcount[MCAST_EXCLUDE]) {
2008 psf->sf_oldin = mca_xcount ==
2009 psf->sf_count[MCAST_EXCLUDE] &&
2010 !psf->sf_count[MCAST_INCLUDE];
2012 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2015 static int sf_setstate(struct ip_mc_list *pmc)
2017 struct ip_sf_list *psf, *dpsf;
2018 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2019 int qrv = pmc->interface->mr_qrv;
2023 for (psf = pmc->sources; psf; psf = psf->sf_next) {
2024 if (pmc->sfcount[MCAST_EXCLUDE]) {
2025 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2026 !psf->sf_count[MCAST_INCLUDE];
2028 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2030 if (!psf->sf_oldin) {
2031 struct ip_sf_list *prev = NULL;
2033 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2034 if (dpsf->sf_inaddr == psf->sf_inaddr)
2040 prev->sf_next = dpsf->sf_next;
2042 pmc->tomb = dpsf->sf_next;
2045 psf->sf_crcount = qrv;
2048 } else if (psf->sf_oldin) {
2050 psf->sf_crcount = 0;
2052 * add or update "delete" records if an active filter
2055 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2056 if (dpsf->sf_inaddr == psf->sf_inaddr)
2059 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2063 /* pmc->lock held by callers */
2064 dpsf->sf_next = pmc->tomb;
2067 dpsf->sf_crcount = qrv;
2076 * Add multicast source filter list to the interface list
2078 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2079 int sfcount, __be32 *psfsrc, int delta)
2081 struct ip_mc_list *pmc;
2088 for_each_pmc_rcu(in_dev, pmc) {
2089 if (*pmca == pmc->multiaddr)
2093 /* MCA not found?? bug */
2097 spin_lock_bh(&pmc->lock);
2100 #ifdef CONFIG_IP_MULTICAST
2103 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2105 pmc->sfcount[sfmode]++;
2107 for (i = 0; i < sfcount; i++) {
2108 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2116 pmc->sfcount[sfmode]--;
2117 for (j = 0; j < i; j++)
2118 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2119 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2120 #ifdef CONFIG_IP_MULTICAST
2121 struct ip_sf_list *psf;
2122 struct net *net = dev_net(pmc->interface->dev);
2123 in_dev = pmc->interface;
2126 /* filter mode change */
2127 if (pmc->sfcount[MCAST_EXCLUDE])
2128 pmc->sfmode = MCAST_EXCLUDE;
2129 else if (pmc->sfcount[MCAST_INCLUDE])
2130 pmc->sfmode = MCAST_INCLUDE;
2131 #ifdef CONFIG_IP_MULTICAST
2132 /* else no filters; keep old mode for reports */
2134 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2135 WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
2136 for (psf = pmc->sources; psf; psf = psf->sf_next)
2137 psf->sf_crcount = 0;
2138 igmp_ifc_event(in_dev);
2139 } else if (sf_setstate(pmc)) {
2140 igmp_ifc_event(in_dev);
2143 spin_unlock_bh(&pmc->lock);
2147 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2149 struct ip_sf_list *tomb, *sources;
2151 spin_lock_bh(&pmc->lock);
2154 sources = pmc->sources;
2155 pmc->sources = NULL;
2156 pmc->sfmode = MCAST_EXCLUDE;
2157 pmc->sfcount[MCAST_INCLUDE] = 0;
2158 pmc->sfcount[MCAST_EXCLUDE] = 1;
2159 spin_unlock_bh(&pmc->lock);
2161 ip_sf_list_clear_all(tomb);
2162 ip_sf_list_clear_all(sources);
2165 /* Join a multicast group
2167 static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2170 __be32 addr = imr->imr_multiaddr.s_addr;
2171 struct ip_mc_socklist *iml, *i;
2172 struct in_device *in_dev;
2173 struct inet_sock *inet = inet_sk(sk);
2174 struct net *net = sock_net(sk);
2181 if (!ipv4_is_multicast(addr))
2184 in_dev = ip_mc_find_dev(net, imr);
2192 ifindex = imr->imr_ifindex;
2193 for_each_pmc_rtnl(inet, i) {
2194 if (i->multi.imr_multiaddr.s_addr == addr &&
2195 i->multi.imr_ifindex == ifindex)
2200 if (count >= net->ipv4.sysctl_igmp_max_memberships)
2202 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2206 memcpy(&iml->multi, imr, sizeof(*imr));
2207 iml->next_rcu = inet->mc_list;
2210 rcu_assign_pointer(inet->mc_list, iml);
2211 ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL);
2217 /* Join ASM (Any-Source Multicast) group
2219 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2221 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2223 EXPORT_SYMBOL(ip_mc_join_group);
2225 /* Join SSM (Source-Specific Multicast) group
2227 int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2230 return __ip_mc_join_group(sk, imr, mode);
2233 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2234 struct in_device *in_dev)
2236 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2240 /* any-source empty exclude case */
2241 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2242 iml->sfmode, 0, NULL, 0);
2244 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2245 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2246 RCU_INIT_POINTER(iml->sflist, NULL);
2247 /* decrease mem now to avoid the memleak warning */
2248 atomic_sub(struct_size(psf, sl_addr, psf->sl_max), &sk->sk_omem_alloc);
2249 kfree_rcu(psf, rcu);
2253 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2255 struct inet_sock *inet = inet_sk(sk);
2256 struct ip_mc_socklist *iml;
2257 struct ip_mc_socklist __rcu **imlp;
2258 struct in_device *in_dev;
2259 struct net *net = sock_net(sk);
2260 __be32 group = imr->imr_multiaddr.s_addr;
2262 int ret = -EADDRNOTAVAIL;
2266 in_dev = ip_mc_find_dev(net, imr);
2267 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2271 ifindex = imr->imr_ifindex;
2272 for (imlp = &inet->mc_list;
2273 (iml = rtnl_dereference(*imlp)) != NULL;
2274 imlp = &iml->next_rcu) {
2275 if (iml->multi.imr_multiaddr.s_addr != group)
2278 if (iml->multi.imr_ifindex != ifindex)
2280 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2281 iml->multi.imr_address.s_addr)
2284 (void) ip_mc_leave_src(sk, iml, in_dev);
2286 *imlp = iml->next_rcu;
2289 ip_mc_dec_group(in_dev, group);
2291 /* decrease mem now to avoid the memleak warning */
2292 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2293 kfree_rcu(iml, rcu);
2299 EXPORT_SYMBOL(ip_mc_leave_group);
2301 int ip_mc_source(int add, int omode, struct sock *sk, struct
2302 ip_mreq_source *mreqs, int ifindex)
2305 struct ip_mreqn imr;
2306 __be32 addr = mreqs->imr_multiaddr;
2307 struct ip_mc_socklist *pmc;
2308 struct in_device *in_dev = NULL;
2309 struct inet_sock *inet = inet_sk(sk);
2310 struct ip_sf_socklist *psl;
2311 struct net *net = sock_net(sk);
2315 if (!ipv4_is_multicast(addr))
2320 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2321 imr.imr_address.s_addr = mreqs->imr_interface;
2322 imr.imr_ifindex = ifindex;
2323 in_dev = ip_mc_find_dev(net, &imr);
2329 err = -EADDRNOTAVAIL;
2331 for_each_pmc_rtnl(inet, pmc) {
2332 if ((pmc->multi.imr_multiaddr.s_addr ==
2333 imr.imr_multiaddr.s_addr) &&
2334 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2337 if (!pmc) { /* must have a prior join */
2341 /* if a source filter was set, must be the same mode as before */
2343 if (pmc->sfmode != omode) {
2347 } else if (pmc->sfmode != omode) {
2348 /* allow mode switches for empty-set filters */
2349 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2350 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2352 pmc->sfmode = omode;
2355 psl = rtnl_dereference(pmc->sflist);
2358 goto done; /* err = -EADDRNOTAVAIL */
2360 for (i = 0; i < psl->sl_count; i++) {
2361 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2366 if (rv) /* source not found */
2367 goto done; /* err = -EADDRNOTAVAIL */
2369 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2370 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2375 /* update the interface filter */
2376 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2377 &mreqs->imr_sourceaddr, 1);
2379 for (j = i+1; j < psl->sl_count; j++)
2380 psl->sl_addr[j-1] = psl->sl_addr[j];
2385 /* else, add a new source to the filter */
2387 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2391 if (!psl || psl->sl_count == psl->sl_max) {
2392 struct ip_sf_socklist *newpsl;
2393 int count = IP_SFBLOCK;
2396 count += psl->sl_max;
2397 newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, count),
2403 newpsl->sl_max = count;
2404 newpsl->sl_count = count - IP_SFBLOCK;
2406 for (i = 0; i < psl->sl_count; i++)
2407 newpsl->sl_addr[i] = psl->sl_addr[i];
2408 /* decrease mem now to avoid the memleak warning */
2409 atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
2410 &sk->sk_omem_alloc);
2412 rcu_assign_pointer(pmc->sflist, newpsl);
2414 kfree_rcu(psl, rcu);
2417 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2418 for (i = 0; i < psl->sl_count; i++) {
2419 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2424 if (rv == 0) /* address already there is an error */
2426 for (j = psl->sl_count-1; j >= i; j--)
2427 psl->sl_addr[j+1] = psl->sl_addr[j];
2428 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2431 /* update the interface list */
2432 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2433 &mreqs->imr_sourceaddr, 1);
2436 err = ip_mc_leave_group(sk, &imr);
2440 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2443 struct ip_mreqn imr;
2444 __be32 addr = msf->imsf_multiaddr;
2445 struct ip_mc_socklist *pmc;
2446 struct in_device *in_dev;
2447 struct inet_sock *inet = inet_sk(sk);
2448 struct ip_sf_socklist *newpsl, *psl;
2449 struct net *net = sock_net(sk);
2452 if (!ipv4_is_multicast(addr))
2454 if (msf->imsf_fmode != MCAST_INCLUDE &&
2455 msf->imsf_fmode != MCAST_EXCLUDE)
2460 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2461 imr.imr_address.s_addr = msf->imsf_interface;
2462 imr.imr_ifindex = ifindex;
2463 in_dev = ip_mc_find_dev(net, &imr);
2470 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2471 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2476 for_each_pmc_rtnl(inet, pmc) {
2477 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2478 pmc->multi.imr_ifindex == imr.imr_ifindex)
2481 if (!pmc) { /* must have a prior join */
2485 if (msf->imsf_numsrc) {
2486 newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr,
2493 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2494 memcpy(newpsl->sl_addr, msf->imsf_slist_flex,
2495 flex_array_size(msf, imsf_slist_flex, msf->imsf_numsrc));
2496 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2497 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2499 sock_kfree_s(sk, newpsl,
2500 struct_size(newpsl, sl_addr,
2506 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2507 msf->imsf_fmode, 0, NULL, 0);
2509 psl = rtnl_dereference(pmc->sflist);
2511 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2512 psl->sl_count, psl->sl_addr, 0);
2513 /* decrease mem now to avoid the memleak warning */
2514 atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
2515 &sk->sk_omem_alloc);
2517 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2520 rcu_assign_pointer(pmc->sflist, newpsl);
2522 kfree_rcu(psl, rcu);
2523 pmc->sfmode = msf->imsf_fmode;
2527 err = ip_mc_leave_group(sk, &imr);
2531 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2532 struct ip_msfilter __user *optval, int __user *optlen)
2534 int err, len, count, copycount;
2535 struct ip_mreqn imr;
2536 __be32 addr = msf->imsf_multiaddr;
2537 struct ip_mc_socklist *pmc;
2538 struct in_device *in_dev;
2539 struct inet_sock *inet = inet_sk(sk);
2540 struct ip_sf_socklist *psl;
2541 struct net *net = sock_net(sk);
2545 if (!ipv4_is_multicast(addr))
2548 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2549 imr.imr_address.s_addr = msf->imsf_interface;
2550 imr.imr_ifindex = 0;
2551 in_dev = ip_mc_find_dev(net, &imr);
2557 err = -EADDRNOTAVAIL;
2559 for_each_pmc_rtnl(inet, pmc) {
2560 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2561 pmc->multi.imr_ifindex == imr.imr_ifindex)
2564 if (!pmc) /* must have a prior join */
2566 msf->imsf_fmode = pmc->sfmode;
2567 psl = rtnl_dereference(pmc->sflist);
2571 count = psl->sl_count;
2573 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2574 len = flex_array_size(psl, sl_addr, copycount);
2575 msf->imsf_numsrc = count;
2576 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2577 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2581 copy_to_user(&optval->imsf_slist_flex[0], psl->sl_addr, len))
2588 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2589 struct sockaddr_storage __user *p)
2591 int i, count, copycount;
2592 struct sockaddr_in *psin;
2594 struct ip_mc_socklist *pmc;
2595 struct inet_sock *inet = inet_sk(sk);
2596 struct ip_sf_socklist *psl;
2600 psin = (struct sockaddr_in *)&gsf->gf_group;
2601 if (psin->sin_family != AF_INET)
2603 addr = psin->sin_addr.s_addr;
2604 if (!ipv4_is_multicast(addr))
2607 for_each_pmc_rtnl(inet, pmc) {
2608 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2609 pmc->multi.imr_ifindex == gsf->gf_interface)
2612 if (!pmc) /* must have a prior join */
2613 return -EADDRNOTAVAIL;
2614 gsf->gf_fmode = pmc->sfmode;
2615 psl = rtnl_dereference(pmc->sflist);
2616 count = psl ? psl->sl_count : 0;
2617 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2618 gsf->gf_numsrc = count;
2619 for (i = 0; i < copycount; i++, p++) {
2620 struct sockaddr_storage ss;
2622 psin = (struct sockaddr_in *)&ss;
2623 memset(&ss, 0, sizeof(ss));
2624 psin->sin_family = AF_INET;
2625 psin->sin_addr.s_addr = psl->sl_addr[i];
2626 if (copy_to_user(p, &ss, sizeof(ss)))
2633 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2635 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2638 struct inet_sock *inet = inet_sk(sk);
2639 struct ip_mc_socklist *pmc;
2640 struct ip_sf_socklist *psl;
2645 if (!ipv4_is_multicast(loc_addr))
2649 for_each_pmc_rcu(inet, pmc) {
2650 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2651 (pmc->multi.imr_ifindex == dif ||
2652 (sdif && pmc->multi.imr_ifindex == sdif)))
2658 psl = rcu_dereference(pmc->sflist);
2659 ret = (pmc->sfmode == MCAST_EXCLUDE);
2663 for (i = 0; i < psl->sl_count; i++) {
2664 if (psl->sl_addr[i] == rmt_addr)
2668 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2670 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2680 * A socket is closing.
2683 void ip_mc_drop_socket(struct sock *sk)
2685 struct inet_sock *inet = inet_sk(sk);
2686 struct ip_mc_socklist *iml;
2687 struct net *net = sock_net(sk);
2693 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2694 struct in_device *in_dev;
2696 inet->mc_list = iml->next_rcu;
2697 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2698 (void) ip_mc_leave_src(sk, iml, in_dev);
2700 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2701 /* decrease mem now to avoid the memleak warning */
2702 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2703 kfree_rcu(iml, rcu);
2708 /* called with rcu_read_lock() */
2709 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2711 struct ip_mc_list *im;
2712 struct ip_mc_list __rcu **mc_hash;
2713 struct ip_sf_list *psf;
2716 mc_hash = rcu_dereference(in_dev->mc_hash);
2718 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2720 for (im = rcu_dereference(mc_hash[hash]);
2722 im = rcu_dereference(im->next_hash)) {
2723 if (im->multiaddr == mc_addr)
2727 for_each_pmc_rcu(in_dev, im) {
2728 if (im->multiaddr == mc_addr)
2732 if (im && proto == IPPROTO_IGMP) {
2736 spin_lock_bh(&im->lock);
2737 for (psf = im->sources; psf; psf = psf->sf_next) {
2738 if (psf->sf_inaddr == src_addr)
2742 rv = psf->sf_count[MCAST_INCLUDE] ||
2743 psf->sf_count[MCAST_EXCLUDE] !=
2744 im->sfcount[MCAST_EXCLUDE];
2746 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2747 spin_unlock_bh(&im->lock);
2749 rv = 1; /* unspecified source; tentatively allow */
2754 #if defined(CONFIG_PROC_FS)
2755 struct igmp_mc_iter_state {
2756 struct seq_net_private p;
2757 struct net_device *dev;
2758 struct in_device *in_dev;
2761 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2763 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2765 struct net *net = seq_file_net(seq);
2766 struct ip_mc_list *im = NULL;
2767 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2769 state->in_dev = NULL;
2770 for_each_netdev_rcu(net, state->dev) {
2771 struct in_device *in_dev;
2773 in_dev = __in_dev_get_rcu(state->dev);
2776 im = rcu_dereference(in_dev->mc_list);
2778 state->in_dev = in_dev;
2785 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2787 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2789 im = rcu_dereference(im->next_rcu);
2791 state->dev = next_net_device_rcu(state->dev);
2793 state->in_dev = NULL;
2796 state->in_dev = __in_dev_get_rcu(state->dev);
2799 im = rcu_dereference(state->in_dev->mc_list);
2804 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2806 struct ip_mc_list *im = igmp_mc_get_first(seq);
2808 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2810 return pos ? NULL : im;
2813 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2817 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2820 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2822 struct ip_mc_list *im;
2823 if (v == SEQ_START_TOKEN)
2824 im = igmp_mc_get_first(seq);
2826 im = igmp_mc_get_next(seq, v);
2831 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2834 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2836 state->in_dev = NULL;
2841 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2843 if (v == SEQ_START_TOKEN)
2845 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2847 struct ip_mc_list *im = v;
2848 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2852 #ifdef CONFIG_IP_MULTICAST
2853 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2854 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2860 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2861 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2862 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2865 delta = im->timer.expires - jiffies;
2867 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2868 im->multiaddr, im->users,
2870 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2876 static const struct seq_operations igmp_mc_seq_ops = {
2877 .start = igmp_mc_seq_start,
2878 .next = igmp_mc_seq_next,
2879 .stop = igmp_mc_seq_stop,
2880 .show = igmp_mc_seq_show,
2883 struct igmp_mcf_iter_state {
2884 struct seq_net_private p;
2885 struct net_device *dev;
2886 struct in_device *idev;
2887 struct ip_mc_list *im;
2890 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2892 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2894 struct net *net = seq_file_net(seq);
2895 struct ip_sf_list *psf = NULL;
2896 struct ip_mc_list *im = NULL;
2897 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2901 for_each_netdev_rcu(net, state->dev) {
2902 struct in_device *idev;
2903 idev = __in_dev_get_rcu(state->dev);
2904 if (unlikely(!idev))
2906 im = rcu_dereference(idev->mc_list);
2908 spin_lock_bh(&im->lock);
2915 spin_unlock_bh(&im->lock);
2921 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2923 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2927 spin_unlock_bh(&state->im->lock);
2928 state->im = state->im->next;
2929 while (!state->im) {
2930 state->dev = next_net_device_rcu(state->dev);
2935 state->idev = __in_dev_get_rcu(state->dev);
2938 state->im = rcu_dereference(state->idev->mc_list);
2942 spin_lock_bh(&state->im->lock);
2943 psf = state->im->sources;
2949 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2951 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2953 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2955 return pos ? NULL : psf;
2958 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2962 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2965 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2967 struct ip_sf_list *psf;
2968 if (v == SEQ_START_TOKEN)
2969 psf = igmp_mcf_get_first(seq);
2971 psf = igmp_mcf_get_next(seq, v);
2976 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2979 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2980 if (likely(state->im)) {
2981 spin_unlock_bh(&state->im->lock);
2989 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2991 struct ip_sf_list *psf = v;
2992 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2994 if (v == SEQ_START_TOKEN) {
2995 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2999 "0x%08x %6lu %6lu\n",
3000 state->dev->ifindex, state->dev->name,
3001 ntohl(state->im->multiaddr),
3002 ntohl(psf->sf_inaddr),
3003 psf->sf_count[MCAST_INCLUDE],
3004 psf->sf_count[MCAST_EXCLUDE]);
3009 static const struct seq_operations igmp_mcf_seq_ops = {
3010 .start = igmp_mcf_seq_start,
3011 .next = igmp_mcf_seq_next,
3012 .stop = igmp_mcf_seq_stop,
3013 .show = igmp_mcf_seq_show,
3016 static int __net_init igmp_net_init(struct net *net)
3018 struct proc_dir_entry *pde;
3021 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3022 sizeof(struct igmp_mc_iter_state));
3025 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3026 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
3029 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3030 SOCK_DGRAM, 0, net);
3032 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3040 remove_proc_entry("mcfilter", net->proc_net);
3042 remove_proc_entry("igmp", net->proc_net);
3047 static void __net_exit igmp_net_exit(struct net *net)
3049 remove_proc_entry("mcfilter", net->proc_net);
3050 remove_proc_entry("igmp", net->proc_net);
3051 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3054 static struct pernet_operations igmp_net_ops = {
3055 .init = igmp_net_init,
3056 .exit = igmp_net_exit,
3060 static int igmp_netdev_event(struct notifier_block *this,
3061 unsigned long event, void *ptr)
3063 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3064 struct in_device *in_dev;
3067 case NETDEV_RESEND_IGMP:
3068 in_dev = __in_dev_get_rtnl(dev);
3070 ip_mc_rejoin_groups(in_dev);
3078 static struct notifier_block igmp_notifier = {
3079 .notifier_call = igmp_netdev_event,
3082 int __init igmp_mc_init(void)
3084 #if defined(CONFIG_PROC_FS)
3087 err = register_pernet_subsys(&igmp_net_ops);
3090 err = register_netdevice_notifier(&igmp_notifier);
3092 goto reg_notif_fail;
3096 unregister_pernet_subsys(&igmp_net_ops);
3099 return register_netdevice_notifier(&igmp_notifier);