2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <linux/uaccess.h>
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/jiffies.h>
79 #include <linux/string.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
83 #include <linux/inet.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/if_arp.h>
89 #include <linux/rtnetlink.h>
90 #include <linux/times.h>
91 #include <linux/pkt_sched.h>
92 #include <linux/byteorder/generic.h>
94 #include <net/net_namespace.h>
97 #include <net/protocol.h>
98 #include <net/route.h>
100 #include <net/checksum.h>
101 #include <net/inet_common.h>
102 #include <linux/netfilter_ipv4.h>
103 #ifdef CONFIG_IP_MROUTE
104 #include <linux/mroute.h>
106 #ifdef CONFIG_PROC_FS
107 #include <linux/proc_fs.h>
108 #include <linux/seq_file.h>
111 #ifdef CONFIG_IP_MULTICAST
112 /* Parameter names and values are taken from igmp-v2-06 draft */
114 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
115 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
116 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
117 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
118 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
119 #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
122 #define IGMP_INITIAL_REPORT_DELAY (1)
124 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
131 #define IGMP_V1_SEEN(in_dev) \
132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
142 static int unsolicited_report_interval(struct in_device *in_dev)
144 int interval_ms, interval_jiffies;
146 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
147 interval_ms = IN_DEV_CONF_GET(
149 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
151 interval_ms = IN_DEV_CONF_GET(
153 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
155 interval_jiffies = msecs_to_jiffies(interval_ms);
157 /* _timer functions can't handle a delay of 0 jiffies so ensure
158 * we always return a positive value.
160 if (interval_jiffies <= 0)
161 interval_jiffies = 1;
162 return interval_jiffies;
165 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
166 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
167 static void igmpv3_clear_delrec(struct in_device *in_dev);
168 static int sf_setstate(struct ip_mc_list *pmc);
169 static void sf_markstate(struct ip_mc_list *pmc);
171 static void ip_mc_clear_src(struct ip_mc_list *pmc);
172 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
173 int sfcount, __be32 *psfsrc, int delta);
175 static void ip_ma_put(struct ip_mc_list *im)
177 if (refcount_dec_and_test(&im->refcnt)) {
178 in_dev_put(im->interface);
183 #define for_each_pmc_rcu(in_dev, pmc) \
184 for (pmc = rcu_dereference(in_dev->mc_list); \
186 pmc = rcu_dereference(pmc->next_rcu))
188 #define for_each_pmc_rtnl(in_dev, pmc) \
189 for (pmc = rtnl_dereference(in_dev->mc_list); \
191 pmc = rtnl_dereference(pmc->next_rcu))
193 static void ip_sf_list_clear_all(struct ip_sf_list *psf)
195 struct ip_sf_list *next;
204 #ifdef CONFIG_IP_MULTICAST
210 static void igmp_stop_timer(struct ip_mc_list *im)
212 spin_lock_bh(&im->lock);
213 if (del_timer(&im->timer))
214 refcount_dec(&im->refcnt);
217 im->unsolicit_count = 0;
218 spin_unlock_bh(&im->lock);
221 /* It must be called with locked im->lock */
222 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
224 int tv = prandom_u32() % max_delay;
227 if (!mod_timer(&im->timer, jiffies+tv+2))
228 refcount_inc(&im->refcnt);
231 static void igmp_gq_start_timer(struct in_device *in_dev)
233 int tv = prandom_u32() % in_dev->mr_maxdelay;
234 unsigned long exp = jiffies + tv + 2;
236 if (in_dev->mr_gq_running &&
237 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
240 in_dev->mr_gq_running = 1;
241 if (!mod_timer(&in_dev->mr_gq_timer, exp))
245 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
247 int tv = prandom_u32() % delay;
249 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
253 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
255 spin_lock_bh(&im->lock);
256 im->unsolicit_count = 0;
257 if (del_timer(&im->timer)) {
258 if ((long)(im->timer.expires-jiffies) < max_delay) {
259 add_timer(&im->timer);
261 spin_unlock_bh(&im->lock);
264 refcount_dec(&im->refcnt);
266 igmp_start_timer(im, max_delay);
267 spin_unlock_bh(&im->lock);
272 * Send an IGMP report.
275 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
278 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
279 int gdeleted, int sdeleted)
282 case IGMPV3_MODE_IS_INCLUDE:
283 case IGMPV3_MODE_IS_EXCLUDE:
284 if (gdeleted || sdeleted)
286 if (!(pmc->gsquery && !psf->sf_gsresp)) {
287 if (pmc->sfmode == MCAST_INCLUDE)
289 /* don't include if this source is excluded
292 if (psf->sf_count[MCAST_INCLUDE])
293 return type == IGMPV3_MODE_IS_INCLUDE;
294 return pmc->sfcount[MCAST_EXCLUDE] ==
295 psf->sf_count[MCAST_EXCLUDE];
298 case IGMPV3_CHANGE_TO_INCLUDE:
299 if (gdeleted || sdeleted)
301 return psf->sf_count[MCAST_INCLUDE] != 0;
302 case IGMPV3_CHANGE_TO_EXCLUDE:
303 if (gdeleted || sdeleted)
305 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
306 psf->sf_count[MCAST_INCLUDE])
308 return pmc->sfcount[MCAST_EXCLUDE] ==
309 psf->sf_count[MCAST_EXCLUDE];
310 case IGMPV3_ALLOW_NEW_SOURCES:
311 if (gdeleted || !psf->sf_crcount)
313 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
314 case IGMPV3_BLOCK_OLD_SOURCES:
315 if (pmc->sfmode == MCAST_INCLUDE)
316 return gdeleted || (psf->sf_crcount && sdeleted);
317 return psf->sf_crcount && !gdeleted && !sdeleted;
323 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
325 struct ip_sf_list *psf;
328 for (psf = pmc->sources; psf; psf = psf->sf_next) {
329 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
336 /* source address selection per RFC 3376 section 4.2.13 */
337 static __be32 igmpv3_get_srcaddr(struct net_device *dev,
338 const struct flowi4 *fl4)
340 struct in_device *in_dev = __in_dev_get_rcu(dev);
343 return htonl(INADDR_ANY);
346 if (fl4->saddr == ifa->ifa_local)
348 } endfor_ifa(in_dev);
350 return htonl(INADDR_ANY);
353 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
358 struct igmpv3_report *pig;
359 struct net *net = dev_net(dev);
361 int hlen = LL_RESERVED_SPACE(dev);
362 int tlen = dev->needed_tailroom;
363 unsigned int size = mtu;
366 skb = alloc_skb(size + hlen + tlen,
367 GFP_ATOMIC | __GFP_NOWARN);
374 skb->priority = TC_PRIO_CONTROL;
376 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
378 IPPROTO_IGMP, 0, dev->ifindex);
384 skb_dst_set(skb, &rt->dst);
387 skb_reserve(skb, hlen);
388 skb_tailroom_reserve(skb, mtu, tlen);
390 skb_reset_network_header(skb);
392 skb_put(skb, sizeof(struct iphdr) + 4);
395 pip->ihl = (sizeof(struct iphdr)+4)>>2;
397 pip->frag_off = htons(IP_DF);
399 pip->daddr = fl4.daddr;
402 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
405 pip->protocol = IPPROTO_IGMP;
406 pip->tot_len = 0; /* filled in later */
407 ip_select_ident(net, skb, NULL);
408 ((u8 *)&pip[1])[0] = IPOPT_RA;
409 ((u8 *)&pip[1])[1] = 4;
410 ((u8 *)&pip[1])[2] = 0;
411 ((u8 *)&pip[1])[3] = 0;
413 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
414 skb_put(skb, sizeof(*pig));
415 pig = igmpv3_report_hdr(skb);
416 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
424 static int igmpv3_sendpack(struct sk_buff *skb)
426 struct igmphdr *pig = igmp_hdr(skb);
427 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
429 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
431 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
434 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
436 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
439 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
440 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
442 struct net_device *dev = pmc->interface->dev;
443 struct igmpv3_report *pih;
444 struct igmpv3_grec *pgr;
447 skb = igmpv3_newpack(dev, mtu);
451 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
452 pgr->grec_type = type;
453 pgr->grec_auxwords = 0;
455 pgr->grec_mca = pmc->multiaddr;
456 pih = igmpv3_report_hdr(skb);
457 pih->ngrec = htons(ntohs(pih->ngrec)+1);
462 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
464 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
465 int type, int gdeleted, int sdeleted)
467 struct net_device *dev = pmc->interface->dev;
468 struct net *net = dev_net(dev);
469 struct igmpv3_report *pih;
470 struct igmpv3_grec *pgr = NULL;
471 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
472 int scount, stotal, first, isquery, truncate;
475 if (pmc->multiaddr == IGMP_ALL_HOSTS)
477 if (ipv4_is_local_multicast(pmc->multiaddr) &&
478 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
481 mtu = READ_ONCE(dev->mtu);
482 if (mtu < IPV4_MIN_MTU)
485 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
486 type == IGMPV3_MODE_IS_EXCLUDE;
487 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
488 type == IGMPV3_CHANGE_TO_EXCLUDE;
492 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
497 pih = skb ? igmpv3_report_hdr(skb) : NULL;
499 /* EX and TO_EX get a fresh packet, if needed */
501 if (pih && pih->ngrec &&
502 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
504 igmpv3_sendpack(skb);
505 skb = igmpv3_newpack(dev, mtu);
510 for (psf = *psf_list; psf; psf = psf_next) {
513 psf_next = psf->sf_next;
515 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
520 /* Based on RFC3376 5.1. Should not send source-list change
521 * records when there is a filter mode change.
523 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
524 (!gdeleted && pmc->crcount)) &&
525 (type == IGMPV3_ALLOW_NEW_SOURCES ||
526 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
527 goto decrease_sf_crcount;
529 /* clear marks on query responses */
533 if (AVAILABLE(skb) < sizeof(__be32) +
534 first*sizeof(struct igmpv3_grec)) {
535 if (truncate && !first)
536 break; /* truncate these */
538 pgr->grec_nsrcs = htons(scount);
540 igmpv3_sendpack(skb);
541 skb = igmpv3_newpack(dev, mtu);
546 skb = add_grhead(skb, pmc, type, &pgr, mtu);
551 psrc = skb_put(skb, sizeof(__be32));
552 *psrc = psf->sf_inaddr;
554 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
555 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
558 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
560 psf_prev->sf_next = psf->sf_next;
562 *psf_list = psf->sf_next;
572 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
573 type == IGMPV3_BLOCK_OLD_SOURCES)
575 if (pmc->crcount || isquery) {
576 /* make sure we have room for group header */
577 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
578 igmpv3_sendpack(skb);
579 skb = NULL; /* add_grhead will get a new one */
581 skb = add_grhead(skb, pmc, type, &pgr, mtu);
585 pgr->grec_nsrcs = htons(scount);
588 pmc->gsquery = 0; /* clear query state on report */
592 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
594 struct sk_buff *skb = NULL;
595 struct net *net = dev_net(in_dev->dev);
600 for_each_pmc_rcu(in_dev, pmc) {
601 if (pmc->multiaddr == IGMP_ALL_HOSTS)
603 if (ipv4_is_local_multicast(pmc->multiaddr) &&
604 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
606 spin_lock_bh(&pmc->lock);
607 if (pmc->sfcount[MCAST_EXCLUDE])
608 type = IGMPV3_MODE_IS_EXCLUDE;
610 type = IGMPV3_MODE_IS_INCLUDE;
611 skb = add_grec(skb, pmc, type, 0, 0);
612 spin_unlock_bh(&pmc->lock);
616 spin_lock_bh(&pmc->lock);
617 if (pmc->sfcount[MCAST_EXCLUDE])
618 type = IGMPV3_MODE_IS_EXCLUDE;
620 type = IGMPV3_MODE_IS_INCLUDE;
621 skb = add_grec(skb, pmc, type, 0, 0);
622 spin_unlock_bh(&pmc->lock);
626 return igmpv3_sendpack(skb);
630 * remove zero-count source records from a source filter list
632 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
634 struct ip_sf_list *psf_prev, *psf_next, *psf;
637 for (psf = *ppsf; psf; psf = psf_next) {
638 psf_next = psf->sf_next;
639 if (psf->sf_crcount == 0) {
641 psf_prev->sf_next = psf->sf_next;
643 *ppsf = psf->sf_next;
650 static void kfree_pmc(struct ip_mc_list *pmc)
652 ip_sf_list_clear_all(pmc->sources);
653 ip_sf_list_clear_all(pmc->tomb);
657 static void igmpv3_send_cr(struct in_device *in_dev)
659 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
660 struct sk_buff *skb = NULL;
664 spin_lock_bh(&in_dev->mc_tomb_lock);
668 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
669 pmc_next = pmc->next;
670 if (pmc->sfmode == MCAST_INCLUDE) {
671 type = IGMPV3_BLOCK_OLD_SOURCES;
672 dtype = IGMPV3_BLOCK_OLD_SOURCES;
673 skb = add_grec(skb, pmc, type, 1, 0);
674 skb = add_grec(skb, pmc, dtype, 1, 1);
677 if (pmc->sfmode == MCAST_EXCLUDE) {
678 type = IGMPV3_CHANGE_TO_INCLUDE;
679 skb = add_grec(skb, pmc, type, 1, 0);
682 if (pmc->crcount == 0) {
683 igmpv3_clear_zeros(&pmc->tomb);
684 igmpv3_clear_zeros(&pmc->sources);
687 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
689 pmc_prev->next = pmc_next;
691 in_dev->mc_tomb = pmc_next;
692 in_dev_put(pmc->interface);
697 spin_unlock_bh(&in_dev->mc_tomb_lock);
700 for_each_pmc_rcu(in_dev, pmc) {
701 spin_lock_bh(&pmc->lock);
702 if (pmc->sfcount[MCAST_EXCLUDE]) {
703 type = IGMPV3_BLOCK_OLD_SOURCES;
704 dtype = IGMPV3_ALLOW_NEW_SOURCES;
706 type = IGMPV3_ALLOW_NEW_SOURCES;
707 dtype = IGMPV3_BLOCK_OLD_SOURCES;
709 skb = add_grec(skb, pmc, type, 0, 0);
710 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
712 /* filter mode changes */
714 if (pmc->sfmode == MCAST_EXCLUDE)
715 type = IGMPV3_CHANGE_TO_EXCLUDE;
717 type = IGMPV3_CHANGE_TO_INCLUDE;
718 skb = add_grec(skb, pmc, type, 0, 0);
721 spin_unlock_bh(&pmc->lock);
727 (void) igmpv3_sendpack(skb);
730 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
737 struct net_device *dev = in_dev->dev;
738 struct net *net = dev_net(dev);
739 __be32 group = pmc ? pmc->multiaddr : 0;
744 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
745 return igmpv3_send_report(in_dev, pmc);
747 if (ipv4_is_local_multicast(group) &&
748 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
751 if (type == IGMP_HOST_LEAVE_MESSAGE)
752 dst = IGMP_ALL_ROUTER;
756 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
758 IPPROTO_IGMP, 0, dev->ifindex);
762 hlen = LL_RESERVED_SPACE(dev);
763 tlen = dev->needed_tailroom;
764 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
769 skb->priority = TC_PRIO_CONTROL;
771 skb_dst_set(skb, &rt->dst);
773 skb_reserve(skb, hlen);
775 skb_reset_network_header(skb);
777 skb_put(skb, sizeof(struct iphdr) + 4);
780 iph->ihl = (sizeof(struct iphdr)+4)>>2;
782 iph->frag_off = htons(IP_DF);
785 iph->saddr = fl4.saddr;
786 iph->protocol = IPPROTO_IGMP;
787 ip_select_ident(net, skb, NULL);
788 ((u8 *)&iph[1])[0] = IPOPT_RA;
789 ((u8 *)&iph[1])[1] = 4;
790 ((u8 *)&iph[1])[2] = 0;
791 ((u8 *)&iph[1])[3] = 0;
793 ih = skb_put(skb, sizeof(struct igmphdr));
798 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
800 return ip_local_out(net, skb->sk, skb);
803 static void igmp_gq_timer_expire(unsigned long data)
805 struct in_device *in_dev = (struct in_device *)data;
807 in_dev->mr_gq_running = 0;
808 igmpv3_send_report(in_dev, NULL);
812 static void igmp_ifc_timer_expire(unsigned long data)
814 struct in_device *in_dev = (struct in_device *)data;
816 igmpv3_send_cr(in_dev);
817 if (in_dev->mr_ifc_count) {
818 in_dev->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 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(unsigned long data)
837 struct ip_mc_list *im = (struct ip_mc_list *)data;
838 struct in_device *in_dev = im->interface;
840 spin_lock(&im->lock);
843 if (im->unsolicit_count) {
844 im->unsolicit_count--;
845 igmp_start_timer(im, unsolicited_report_interval(in_dev));
848 spin_unlock(&im->lock);
850 if (IGMP_V1_SEEN(in_dev))
851 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
852 else if (IGMP_V2_SEEN(in_dev))
853 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
855 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
860 /* mark EXCLUDE-mode sources */
861 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
863 struct ip_sf_list *psf;
867 for (psf = pmc->sources; psf; psf = psf->sf_next) {
870 for (i = 0; i < nsrcs; i++) {
871 /* skip inactive filters */
872 if (psf->sf_count[MCAST_INCLUDE] ||
873 pmc->sfcount[MCAST_EXCLUDE] !=
874 psf->sf_count[MCAST_EXCLUDE])
876 if (srcs[i] == psf->sf_inaddr) {
883 if (scount == nsrcs) /* all sources excluded */
888 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
890 struct ip_sf_list *psf;
893 if (pmc->sfmode == MCAST_EXCLUDE)
894 return igmp_xmarksources(pmc, nsrcs, srcs);
896 /* mark INCLUDE-mode sources */
898 for (psf = pmc->sources; psf; psf = psf->sf_next) {
901 for (i = 0; i < nsrcs; i++)
902 if (srcs[i] == psf->sf_inaddr) {
916 /* return true if packet was dropped */
917 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
919 struct ip_mc_list *im;
920 struct net *net = dev_net(in_dev->dev);
922 /* Timers are only set for non-local groups */
924 if (group == IGMP_ALL_HOSTS)
926 if (ipv4_is_local_multicast(group) &&
927 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
931 for_each_pmc_rcu(in_dev, im) {
932 if (im->multiaddr == group) {
941 /* return true if packet was dropped */
942 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
945 struct igmphdr *ih = igmp_hdr(skb);
946 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
947 struct ip_mc_list *im;
948 __be32 group = ih->group;
951 struct net *net = dev_net(in_dev->dev);
956 /* Alas, old v1 router presents here. */
958 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
959 in_dev->mr_v1_seen = jiffies +
960 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
963 /* v2 router present */
964 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
965 in_dev->mr_v2_seen = jiffies +
966 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
968 /* cancel the interface change timer */
969 in_dev->mr_ifc_count = 0;
970 if (del_timer(&in_dev->mr_ifc_timer))
971 __in_dev_put(in_dev);
972 /* clear deleted report items */
973 igmpv3_clear_delrec(in_dev);
974 } else if (len < 12) {
975 return true; /* ignore bogus packet; freed by caller */
976 } else if (IGMP_V1_SEEN(in_dev)) {
977 /* This is a v3 query with v1 queriers present */
978 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
980 } else if (IGMP_V2_SEEN(in_dev)) {
981 /* this is a v3 query with v2 queriers present;
982 * Interpretation of the max_delay code is problematic here.
983 * A real v2 host would use ih_code directly, while v3 has a
984 * different encoding. We use the v3 encoding as more likely
985 * to be intended in a v3 query.
987 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
989 max_delay = 1; /* can't mod w/ 0 */
991 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
994 ih3 = igmpv3_query_hdr(skb);
996 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
997 + ntohs(ih3->nsrcs)*sizeof(__be32)))
999 ih3 = igmpv3_query_hdr(skb);
1002 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
1004 max_delay = 1; /* can't mod w/ 0 */
1005 in_dev->mr_maxdelay = max_delay;
1007 in_dev->mr_qrv = ih3->qrv;
1008 if (!group) { /* general query */
1010 return true; /* no sources allowed */
1011 igmp_gq_start_timer(in_dev);
1014 /* mark sources to include, if group & source-specific */
1015 mark = ih3->nsrcs != 0;
1019 * - Start the timers in all of our membership records
1020 * that the query applies to for the interface on
1021 * which the query arrived excl. those that belong
1022 * to a "local" group (224.0.0.X)
1023 * - For timers already running check if they need to
1025 * - Use the igmp->igmp_code field as the maximum
1029 for_each_pmc_rcu(in_dev, im) {
1032 if (group && group != im->multiaddr)
1034 if (im->multiaddr == IGMP_ALL_HOSTS)
1036 if (ipv4_is_local_multicast(im->multiaddr) &&
1037 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1039 spin_lock_bh(&im->lock);
1041 im->gsquery = im->gsquery && mark;
1044 changed = !im->gsquery ||
1045 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1046 spin_unlock_bh(&im->lock);
1048 igmp_mod_timer(im, max_delay);
1054 /* called in rcu_read_lock() section */
1055 int igmp_rcv(struct sk_buff *skb)
1057 /* This basically follows the spec line by line -- see RFC1112 */
1059 struct net_device *dev = skb->dev;
1060 struct in_device *in_dev;
1062 bool dropped = true;
1064 if (netif_is_l3_master(dev)) {
1065 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1070 in_dev = __in_dev_get_rcu(dev);
1074 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1077 if (skb_checksum_simple_validate(skb))
1082 case IGMP_HOST_MEMBERSHIP_QUERY:
1083 dropped = igmp_heard_query(in_dev, skb, len);
1085 case IGMP_HOST_MEMBERSHIP_REPORT:
1086 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1087 /* Is it our report looped back? */
1088 if (rt_is_output_route(skb_rtable(skb)))
1090 /* don't rely on MC router hearing unicast reports */
1091 if (skb->pkt_type == PACKET_MULTICAST ||
1092 skb->pkt_type == PACKET_BROADCAST)
1093 dropped = igmp_heard_report(in_dev, ih->group);
1096 #ifdef CONFIG_IP_PIMSM_V1
1097 return pim_rcv_v1(skb);
1099 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1102 case IGMP_HOST_LEAVE_MESSAGE:
1104 case IGMP_MTRACE_RESP:
1122 * Add a filter to a device
1125 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1127 char buf[MAX_ADDR_LEN];
1128 struct net_device *dev = in_dev->dev;
1130 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1131 We will get multicast token leakage, when IFF_MULTICAST
1132 is changed. This check should be done in ndo_set_rx_mode
1133 routine. Something sort of:
1134 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1137 if (arp_mc_map(addr, buf, dev, 0) == 0)
1138 dev_mc_add(dev, buf);
1142 * Remove a filter from a device
1145 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1147 char buf[MAX_ADDR_LEN];
1148 struct net_device *dev = in_dev->dev;
1150 if (arp_mc_map(addr, buf, dev, 0) == 0)
1151 dev_mc_del(dev, buf);
1154 #ifdef CONFIG_IP_MULTICAST
1156 * deleted ip_mc_list manipulation
1158 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1160 struct ip_mc_list *pmc;
1161 struct net *net = dev_net(in_dev->dev);
1163 /* this is an "ip_mc_list" for convenience; only the fields below
1164 * are actually used. In particular, the refcnt and users are not
1165 * used for management of the delete list. Using the same structure
1166 * for deleted items allows change reports to use common code with
1167 * non-deleted or query-response MCA's.
1169 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1172 spin_lock_init(&pmc->lock);
1173 spin_lock_bh(&im->lock);
1174 pmc->interface = im->interface;
1175 in_dev_hold(in_dev);
1176 pmc->multiaddr = im->multiaddr;
1177 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1178 pmc->sfmode = im->sfmode;
1179 if (pmc->sfmode == MCAST_INCLUDE) {
1180 struct ip_sf_list *psf;
1182 pmc->tomb = im->tomb;
1183 pmc->sources = im->sources;
1184 im->tomb = im->sources = NULL;
1185 for (psf = pmc->sources; psf; psf = psf->sf_next)
1186 psf->sf_crcount = pmc->crcount;
1188 spin_unlock_bh(&im->lock);
1190 spin_lock_bh(&in_dev->mc_tomb_lock);
1191 pmc->next = in_dev->mc_tomb;
1192 in_dev->mc_tomb = pmc;
1193 spin_unlock_bh(&in_dev->mc_tomb_lock);
1197 * restore ip_mc_list deleted records
1199 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1201 struct ip_mc_list *pmc, *pmc_prev;
1202 struct ip_sf_list *psf;
1203 struct net *net = dev_net(in_dev->dev);
1204 __be32 multiaddr = im->multiaddr;
1206 spin_lock_bh(&in_dev->mc_tomb_lock);
1208 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1209 if (pmc->multiaddr == multiaddr)
1215 pmc_prev->next = pmc->next;
1217 in_dev->mc_tomb = pmc->next;
1219 spin_unlock_bh(&in_dev->mc_tomb_lock);
1221 spin_lock_bh(&im->lock);
1223 im->interface = pmc->interface;
1224 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1225 if (im->sfmode == MCAST_INCLUDE) {
1226 swap(im->tomb, pmc->tomb);
1227 swap(im->sources, pmc->sources);
1228 for (psf = im->sources; psf; psf = psf->sf_next)
1229 psf->sf_crcount = im->crcount;
1231 in_dev_put(pmc->interface);
1234 spin_unlock_bh(&im->lock);
1238 * flush ip_mc_list deleted records
1240 static void igmpv3_clear_delrec(struct in_device *in_dev)
1242 struct ip_mc_list *pmc, *nextpmc;
1244 spin_lock_bh(&in_dev->mc_tomb_lock);
1245 pmc = in_dev->mc_tomb;
1246 in_dev->mc_tomb = NULL;
1247 spin_unlock_bh(&in_dev->mc_tomb_lock);
1249 for (; pmc; pmc = nextpmc) {
1250 nextpmc = pmc->next;
1251 ip_mc_clear_src(pmc);
1252 in_dev_put(pmc->interface);
1255 /* clear dead sources, too */
1257 for_each_pmc_rcu(in_dev, pmc) {
1258 struct ip_sf_list *psf;
1260 spin_lock_bh(&pmc->lock);
1263 spin_unlock_bh(&pmc->lock);
1264 ip_sf_list_clear_all(psf);
1270 static void igmp_group_dropped(struct ip_mc_list *im)
1272 struct in_device *in_dev = im->interface;
1273 #ifdef CONFIG_IP_MULTICAST
1274 struct net *net = dev_net(in_dev->dev);
1280 ip_mc_filter_del(in_dev, im->multiaddr);
1283 #ifdef CONFIG_IP_MULTICAST
1284 if (im->multiaddr == IGMP_ALL_HOSTS)
1286 if (ipv4_is_local_multicast(im->multiaddr) &&
1287 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1290 reporter = im->reporter;
1291 igmp_stop_timer(im);
1293 if (!in_dev->dead) {
1294 if (IGMP_V1_SEEN(in_dev))
1296 if (IGMP_V2_SEEN(in_dev)) {
1298 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1302 igmpv3_add_delrec(in_dev, im);
1304 igmp_ifc_event(in_dev);
1309 static void igmp_group_added(struct ip_mc_list *im)
1311 struct in_device *in_dev = im->interface;
1312 #ifdef CONFIG_IP_MULTICAST
1313 struct net *net = dev_net(in_dev->dev);
1316 if (im->loaded == 0) {
1318 ip_mc_filter_add(in_dev, im->multiaddr);
1321 #ifdef CONFIG_IP_MULTICAST
1322 if (im->multiaddr == IGMP_ALL_HOSTS)
1324 if (ipv4_is_local_multicast(im->multiaddr) &&
1325 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1330 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1331 spin_lock_bh(&im->lock);
1332 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1333 spin_unlock_bh(&im->lock);
1338 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1339 igmp_ifc_event(in_dev);
1345 * Multicast list managers
1348 static u32 ip_mc_hash(const struct ip_mc_list *im)
1350 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1353 static void ip_mc_hash_add(struct in_device *in_dev,
1354 struct ip_mc_list *im)
1356 struct ip_mc_list __rcu **mc_hash;
1359 mc_hash = rtnl_dereference(in_dev->mc_hash);
1361 hash = ip_mc_hash(im);
1362 im->next_hash = mc_hash[hash];
1363 rcu_assign_pointer(mc_hash[hash], im);
1367 /* do not use a hash table for small number of items */
1368 if (in_dev->mc_count < 4)
1371 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1376 for_each_pmc_rtnl(in_dev, im) {
1377 hash = ip_mc_hash(im);
1378 im->next_hash = mc_hash[hash];
1379 RCU_INIT_POINTER(mc_hash[hash], im);
1382 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1385 static void ip_mc_hash_remove(struct in_device *in_dev,
1386 struct ip_mc_list *im)
1388 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1389 struct ip_mc_list *aux;
1393 mc_hash += ip_mc_hash(im);
1394 while ((aux = rtnl_dereference(*mc_hash)) != im)
1395 mc_hash = &aux->next_hash;
1396 *mc_hash = im->next_hash;
1401 * A socket has joined a multicast group on device dev.
1404 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1406 struct ip_mc_list *im;
1407 #ifdef CONFIG_IP_MULTICAST
1408 struct net *net = dev_net(in_dev->dev);
1413 for_each_pmc_rtnl(in_dev, im) {
1414 if (im->multiaddr == addr) {
1416 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1421 im = kzalloc(sizeof(*im), GFP_KERNEL);
1426 im->interface = in_dev;
1427 in_dev_hold(in_dev);
1428 im->multiaddr = addr;
1429 /* initial mode is (EX, empty) */
1430 im->sfmode = MCAST_EXCLUDE;
1431 im->sfcount[MCAST_EXCLUDE] = 1;
1432 refcount_set(&im->refcnt, 1);
1433 spin_lock_init(&im->lock);
1434 #ifdef CONFIG_IP_MULTICAST
1435 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
1436 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1439 im->next_rcu = in_dev->mc_list;
1441 rcu_assign_pointer(in_dev->mc_list, im);
1443 ip_mc_hash_add(in_dev, im);
1445 #ifdef CONFIG_IP_MULTICAST
1446 igmpv3_del_delrec(in_dev, im);
1448 igmp_group_added(im);
1450 ip_rt_multicast_event(in_dev);
1454 EXPORT_SYMBOL(ip_mc_inc_group);
1456 static int ip_mc_check_iphdr(struct sk_buff *skb)
1458 const struct iphdr *iph;
1460 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1462 if (!pskb_may_pull(skb, offset))
1467 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1470 offset += ip_hdrlen(skb) - sizeof(*iph);
1472 if (!pskb_may_pull(skb, offset))
1477 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1480 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1481 if (skb->len < len || len < offset)
1484 skb_set_transport_header(skb, offset);
1489 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1491 unsigned int len = skb_transport_offset(skb);
1493 len += sizeof(struct igmpv3_report);
1495 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1498 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1500 unsigned int len = skb_transport_offset(skb);
1502 len += sizeof(struct igmphdr);
1507 if (skb->len != len) {
1509 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1510 if (skb->len < len || !pskb_may_pull(skb, len))
1514 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1515 * all-systems destination addresses (224.0.0.1) for general queries
1517 if (!igmp_hdr(skb)->group &&
1518 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1524 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1526 switch (igmp_hdr(skb)->type) {
1527 case IGMP_HOST_LEAVE_MESSAGE:
1528 case IGMP_HOST_MEMBERSHIP_REPORT:
1529 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1532 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1533 return ip_mc_check_igmp_reportv3(skb);
1534 case IGMP_HOST_MEMBERSHIP_QUERY:
1535 return ip_mc_check_igmp_query(skb);
1541 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1543 return skb_checksum_simple_validate(skb);
1546 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1549 struct sk_buff *skb_chk;
1550 unsigned int transport_len;
1551 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1554 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1556 skb_chk = skb_checksum_trimmed(skb, transport_len,
1557 ip_mc_validate_checksum);
1561 if (!pskb_may_pull(skb_chk, len))
1564 ret = ip_mc_check_igmp_msg(skb_chk);
1569 *skb_trimmed = skb_chk;
1570 /* free now unneeded clone */
1571 else if (skb_chk != skb)
1577 if (ret && skb_chk && skb_chk != skb)
1584 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1585 * @skb: the skb to validate
1586 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1588 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1589 * skb transport header accordingly and returns zero.
1591 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1593 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1594 * -ENOMEM: A memory allocation failure happened.
1596 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1597 * to NULL): After parsing an IGMP packet successfully it will point to
1598 * an skb which has its tail aligned to the IP packet end. This might
1599 * either be the originally provided skb or a trimmed, cloned version if
1600 * the skb frame had data beyond the IP packet. A cloned skb allows us
1601 * to leave the original skb and its full frame unchanged (which might be
1602 * desirable for layer 2 frame jugglers).
1604 * Caller needs to set the skb network header and free any returned skb if it
1605 * differs from the provided skb.
1607 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1609 int ret = ip_mc_check_iphdr(skb);
1614 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1617 return __ip_mc_check_igmp(skb, skb_trimmed);
1619 EXPORT_SYMBOL(ip_mc_check_igmp);
1622 * Resend IGMP JOIN report; used by netdev notifier.
1624 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1626 #ifdef CONFIG_IP_MULTICAST
1627 struct ip_mc_list *im;
1629 struct net *net = dev_net(in_dev->dev);
1633 for_each_pmc_rtnl(in_dev, im) {
1634 if (im->multiaddr == IGMP_ALL_HOSTS)
1636 if (ipv4_is_local_multicast(im->multiaddr) &&
1637 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1640 /* a failover is happening and switches
1641 * must be notified immediately
1643 if (IGMP_V1_SEEN(in_dev))
1644 type = IGMP_HOST_MEMBERSHIP_REPORT;
1645 else if (IGMP_V2_SEEN(in_dev))
1646 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1648 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1649 igmp_send_report(in_dev, im, type);
1655 * A socket has left a multicast group on device dev
1658 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1660 struct ip_mc_list *i;
1661 struct ip_mc_list __rcu **ip;
1665 for (ip = &in_dev->mc_list;
1666 (i = rtnl_dereference(*ip)) != NULL;
1667 ip = &i->next_rcu) {
1668 if (i->multiaddr == addr) {
1669 if (--i->users == 0) {
1670 ip_mc_hash_remove(in_dev, i);
1673 igmp_group_dropped(i);
1677 ip_rt_multicast_event(in_dev);
1686 EXPORT_SYMBOL(ip_mc_dec_group);
1688 /* Device changing type */
1690 void ip_mc_unmap(struct in_device *in_dev)
1692 struct ip_mc_list *pmc;
1696 for_each_pmc_rtnl(in_dev, pmc)
1697 igmp_group_dropped(pmc);
1700 void ip_mc_remap(struct in_device *in_dev)
1702 struct ip_mc_list *pmc;
1706 for_each_pmc_rtnl(in_dev, pmc) {
1707 #ifdef CONFIG_IP_MULTICAST
1708 igmpv3_del_delrec(in_dev, pmc);
1710 igmp_group_added(pmc);
1714 /* Device going down */
1716 void ip_mc_down(struct in_device *in_dev)
1718 struct ip_mc_list *pmc;
1722 for_each_pmc_rtnl(in_dev, pmc)
1723 igmp_group_dropped(pmc);
1725 #ifdef CONFIG_IP_MULTICAST
1726 in_dev->mr_ifc_count = 0;
1727 if (del_timer(&in_dev->mr_ifc_timer))
1728 __in_dev_put(in_dev);
1729 in_dev->mr_gq_running = 0;
1730 if (del_timer(&in_dev->mr_gq_timer))
1731 __in_dev_put(in_dev);
1734 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1737 void ip_mc_init_dev(struct in_device *in_dev)
1739 #ifdef CONFIG_IP_MULTICAST
1740 struct net *net = dev_net(in_dev->dev);
1744 #ifdef CONFIG_IP_MULTICAST
1745 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1746 (unsigned long)in_dev);
1747 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1748 (unsigned long)in_dev);
1749 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1752 spin_lock_init(&in_dev->mc_tomb_lock);
1755 /* Device going up */
1757 void ip_mc_up(struct in_device *in_dev)
1759 struct ip_mc_list *pmc;
1760 #ifdef CONFIG_IP_MULTICAST
1761 struct net *net = dev_net(in_dev->dev);
1766 #ifdef CONFIG_IP_MULTICAST
1767 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1769 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1771 for_each_pmc_rtnl(in_dev, pmc) {
1772 #ifdef CONFIG_IP_MULTICAST
1773 igmpv3_del_delrec(in_dev, pmc);
1775 igmp_group_added(pmc);
1780 * Device is about to be destroyed: clean up.
1783 void ip_mc_destroy_dev(struct in_device *in_dev)
1785 struct ip_mc_list *i;
1789 /* Deactivate timers */
1791 #ifdef CONFIG_IP_MULTICAST
1792 igmpv3_clear_delrec(in_dev);
1795 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1796 in_dev->mc_list = i->next_rcu;
1803 /* RTNL is locked */
1804 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1806 struct net_device *dev = NULL;
1807 struct in_device *idev = NULL;
1809 if (imr->imr_ifindex) {
1810 idev = inetdev_by_index(net, imr->imr_ifindex);
1813 if (imr->imr_address.s_addr) {
1814 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1820 struct rtable *rt = ip_route_output(net,
1821 imr->imr_multiaddr.s_addr,
1829 imr->imr_ifindex = dev->ifindex;
1830 idev = __in_dev_get_rtnl(dev);
1836 * Join a socket to a group
1839 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1842 struct ip_sf_list *psf, *psf_prev;
1846 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1847 if (psf->sf_inaddr == *psfsrc)
1851 if (!psf || psf->sf_count[sfmode] == 0) {
1852 /* source filter not found, or count wrong => bug */
1855 psf->sf_count[sfmode]--;
1856 if (psf->sf_count[sfmode] == 0) {
1857 ip_rt_multicast_event(pmc->interface);
1859 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1860 #ifdef CONFIG_IP_MULTICAST
1861 struct in_device *in_dev = pmc->interface;
1862 struct net *net = dev_net(in_dev->dev);
1865 /* no more filters for this source */
1867 psf_prev->sf_next = psf->sf_next;
1869 pmc->sources = psf->sf_next;
1870 #ifdef CONFIG_IP_MULTICAST
1871 if (psf->sf_oldin &&
1872 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1873 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1874 psf->sf_next = pmc->tomb;
1884 #ifndef CONFIG_IP_MULTICAST
1885 #define igmp_ifc_event(x) do { } while (0)
1888 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1889 int sfcount, __be32 *psfsrc, int delta)
1891 struct ip_mc_list *pmc;
1898 for_each_pmc_rcu(in_dev, pmc) {
1899 if (*pmca == pmc->multiaddr)
1903 /* MCA not found?? bug */
1907 spin_lock_bh(&pmc->lock);
1909 #ifdef CONFIG_IP_MULTICAST
1914 if (!pmc->sfcount[sfmode])
1916 pmc->sfcount[sfmode]--;
1919 for (i = 0; i < sfcount; i++) {
1920 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1922 changerec |= rv > 0;
1926 if (pmc->sfmode == MCAST_EXCLUDE &&
1927 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1928 pmc->sfcount[MCAST_INCLUDE]) {
1929 #ifdef CONFIG_IP_MULTICAST
1930 struct ip_sf_list *psf;
1931 struct net *net = dev_net(in_dev->dev);
1934 /* filter mode change */
1935 pmc->sfmode = MCAST_INCLUDE;
1936 #ifdef CONFIG_IP_MULTICAST
1937 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1938 in_dev->mr_ifc_count = pmc->crcount;
1939 for (psf = pmc->sources; psf; psf = psf->sf_next)
1940 psf->sf_crcount = 0;
1941 igmp_ifc_event(pmc->interface);
1942 } else if (sf_setstate(pmc) || changerec) {
1943 igmp_ifc_event(pmc->interface);
1947 spin_unlock_bh(&pmc->lock);
1952 * Add multicast single-source filter to the interface list
1954 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1957 struct ip_sf_list *psf, *psf_prev;
1960 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1961 if (psf->sf_inaddr == *psfsrc)
1966 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1969 psf->sf_inaddr = *psfsrc;
1971 psf_prev->sf_next = psf;
1975 psf->sf_count[sfmode]++;
1976 if (psf->sf_count[sfmode] == 1) {
1977 ip_rt_multicast_event(pmc->interface);
1982 #ifdef CONFIG_IP_MULTICAST
1983 static void sf_markstate(struct ip_mc_list *pmc)
1985 struct ip_sf_list *psf;
1986 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1988 for (psf = pmc->sources; psf; psf = psf->sf_next)
1989 if (pmc->sfcount[MCAST_EXCLUDE]) {
1990 psf->sf_oldin = mca_xcount ==
1991 psf->sf_count[MCAST_EXCLUDE] &&
1992 !psf->sf_count[MCAST_INCLUDE];
1994 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1997 static int sf_setstate(struct ip_mc_list *pmc)
1999 struct ip_sf_list *psf, *dpsf;
2000 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2001 int qrv = pmc->interface->mr_qrv;
2005 for (psf = pmc->sources; psf; psf = psf->sf_next) {
2006 if (pmc->sfcount[MCAST_EXCLUDE]) {
2007 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2008 !psf->sf_count[MCAST_INCLUDE];
2010 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2012 if (!psf->sf_oldin) {
2013 struct ip_sf_list *prev = NULL;
2015 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2016 if (dpsf->sf_inaddr == psf->sf_inaddr)
2022 prev->sf_next = dpsf->sf_next;
2024 pmc->tomb = dpsf->sf_next;
2027 psf->sf_crcount = qrv;
2030 } else if (psf->sf_oldin) {
2032 psf->sf_crcount = 0;
2034 * add or update "delete" records if an active filter
2037 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2038 if (dpsf->sf_inaddr == psf->sf_inaddr)
2041 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2045 /* pmc->lock held by callers */
2046 dpsf->sf_next = pmc->tomb;
2049 dpsf->sf_crcount = qrv;
2058 * Add multicast source filter list to the interface list
2060 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2061 int sfcount, __be32 *psfsrc, int delta)
2063 struct ip_mc_list *pmc;
2070 for_each_pmc_rcu(in_dev, pmc) {
2071 if (*pmca == pmc->multiaddr)
2075 /* MCA not found?? bug */
2079 spin_lock_bh(&pmc->lock);
2082 #ifdef CONFIG_IP_MULTICAST
2085 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2087 pmc->sfcount[sfmode]++;
2089 for (i = 0; i < sfcount; i++) {
2090 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2098 pmc->sfcount[sfmode]--;
2099 for (j = 0; j < i; j++)
2100 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2101 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2102 #ifdef CONFIG_IP_MULTICAST
2103 struct ip_sf_list *psf;
2104 struct net *net = dev_net(pmc->interface->dev);
2105 in_dev = pmc->interface;
2108 /* filter mode change */
2109 if (pmc->sfcount[MCAST_EXCLUDE])
2110 pmc->sfmode = MCAST_EXCLUDE;
2111 else if (pmc->sfcount[MCAST_INCLUDE])
2112 pmc->sfmode = MCAST_INCLUDE;
2113 #ifdef CONFIG_IP_MULTICAST
2114 /* else no filters; keep old mode for reports */
2116 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2117 in_dev->mr_ifc_count = pmc->crcount;
2118 for (psf = pmc->sources; psf; psf = psf->sf_next)
2119 psf->sf_crcount = 0;
2120 igmp_ifc_event(in_dev);
2121 } else if (sf_setstate(pmc)) {
2122 igmp_ifc_event(in_dev);
2125 spin_unlock_bh(&pmc->lock);
2129 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2131 struct ip_sf_list *tomb, *sources;
2133 spin_lock_bh(&pmc->lock);
2136 sources = pmc->sources;
2137 pmc->sources = NULL;
2138 pmc->sfmode = MCAST_EXCLUDE;
2139 pmc->sfcount[MCAST_INCLUDE] = 0;
2140 pmc->sfcount[MCAST_EXCLUDE] = 1;
2141 spin_unlock_bh(&pmc->lock);
2143 ip_sf_list_clear_all(tomb);
2144 ip_sf_list_clear_all(sources);
2147 /* Join a multicast group
2150 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2152 __be32 addr = imr->imr_multiaddr.s_addr;
2153 struct ip_mc_socklist *iml, *i;
2154 struct in_device *in_dev;
2155 struct inet_sock *inet = inet_sk(sk);
2156 struct net *net = sock_net(sk);
2163 if (!ipv4_is_multicast(addr))
2166 in_dev = ip_mc_find_dev(net, imr);
2174 ifindex = imr->imr_ifindex;
2175 for_each_pmc_rtnl(inet, i) {
2176 if (i->multi.imr_multiaddr.s_addr == addr &&
2177 i->multi.imr_ifindex == ifindex)
2182 if (count >= READ_ONCE(net->ipv4.sysctl_igmp_max_memberships))
2184 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2188 memcpy(&iml->multi, imr, sizeof(*imr));
2189 iml->next_rcu = inet->mc_list;
2191 iml->sfmode = MCAST_EXCLUDE;
2192 rcu_assign_pointer(inet->mc_list, iml);
2193 ip_mc_inc_group(in_dev, addr);
2198 EXPORT_SYMBOL(ip_mc_join_group);
2200 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2201 struct in_device *in_dev)
2203 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2207 /* any-source empty exclude case */
2208 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2209 iml->sfmode, 0, NULL, 0);
2211 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2212 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2213 RCU_INIT_POINTER(iml->sflist, NULL);
2214 /* decrease mem now to avoid the memleak warning */
2215 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2216 kfree_rcu(psf, rcu);
2220 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2222 struct inet_sock *inet = inet_sk(sk);
2223 struct ip_mc_socklist *iml;
2224 struct ip_mc_socklist __rcu **imlp;
2225 struct in_device *in_dev;
2226 struct net *net = sock_net(sk);
2227 __be32 group = imr->imr_multiaddr.s_addr;
2229 int ret = -EADDRNOTAVAIL;
2233 in_dev = ip_mc_find_dev(net, imr);
2234 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2238 ifindex = imr->imr_ifindex;
2239 for (imlp = &inet->mc_list;
2240 (iml = rtnl_dereference(*imlp)) != NULL;
2241 imlp = &iml->next_rcu) {
2242 if (iml->multi.imr_multiaddr.s_addr != group)
2245 if (iml->multi.imr_ifindex != ifindex)
2247 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2248 iml->multi.imr_address.s_addr)
2251 (void) ip_mc_leave_src(sk, iml, in_dev);
2253 *imlp = iml->next_rcu;
2256 ip_mc_dec_group(in_dev, group);
2258 /* decrease mem now to avoid the memleak warning */
2259 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2260 kfree_rcu(iml, rcu);
2266 EXPORT_SYMBOL(ip_mc_leave_group);
2268 int ip_mc_source(int add, int omode, struct sock *sk, struct
2269 ip_mreq_source *mreqs, int ifindex)
2272 struct ip_mreqn imr;
2273 __be32 addr = mreqs->imr_multiaddr;
2274 struct ip_mc_socklist *pmc;
2275 struct in_device *in_dev = NULL;
2276 struct inet_sock *inet = inet_sk(sk);
2277 struct ip_sf_socklist *psl;
2278 struct net *net = sock_net(sk);
2282 if (!ipv4_is_multicast(addr))
2287 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2288 imr.imr_address.s_addr = mreqs->imr_interface;
2289 imr.imr_ifindex = ifindex;
2290 in_dev = ip_mc_find_dev(net, &imr);
2296 err = -EADDRNOTAVAIL;
2298 for_each_pmc_rtnl(inet, pmc) {
2299 if ((pmc->multi.imr_multiaddr.s_addr ==
2300 imr.imr_multiaddr.s_addr) &&
2301 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2304 if (!pmc) { /* must have a prior join */
2308 /* if a source filter was set, must be the same mode as before */
2310 if (pmc->sfmode != omode) {
2314 } else if (pmc->sfmode != omode) {
2315 /* allow mode switches for empty-set filters */
2316 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2317 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2319 pmc->sfmode = omode;
2322 psl = rtnl_dereference(pmc->sflist);
2325 goto done; /* err = -EADDRNOTAVAIL */
2327 for (i = 0; i < psl->sl_count; i++) {
2328 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2333 if (rv) /* source not found */
2334 goto done; /* err = -EADDRNOTAVAIL */
2336 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2337 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2342 /* update the interface filter */
2343 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2344 &mreqs->imr_sourceaddr, 1);
2346 for (j = i+1; j < psl->sl_count; j++)
2347 psl->sl_addr[j-1] = psl->sl_addr[j];
2352 /* else, add a new source to the filter */
2354 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2358 if (!psl || psl->sl_count == psl->sl_max) {
2359 struct ip_sf_socklist *newpsl;
2360 int count = IP_SFBLOCK;
2363 count += psl->sl_max;
2364 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2369 newpsl->sl_max = count;
2370 newpsl->sl_count = count - IP_SFBLOCK;
2372 for (i = 0; i < psl->sl_count; i++)
2373 newpsl->sl_addr[i] = psl->sl_addr[i];
2374 /* decrease mem now to avoid the memleak warning */
2375 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2377 rcu_assign_pointer(pmc->sflist, newpsl);
2379 kfree_rcu(psl, rcu);
2382 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2383 for (i = 0; i < psl->sl_count; i++) {
2384 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2389 if (rv == 0) /* address already there is an error */
2391 for (j = psl->sl_count-1; j >= i; j--)
2392 psl->sl_addr[j+1] = psl->sl_addr[j];
2393 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2396 /* update the interface list */
2397 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2398 &mreqs->imr_sourceaddr, 1);
2401 err = ip_mc_leave_group(sk, &imr);
2405 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2408 struct ip_mreqn imr;
2409 __be32 addr = msf->imsf_multiaddr;
2410 struct ip_mc_socklist *pmc;
2411 struct in_device *in_dev;
2412 struct inet_sock *inet = inet_sk(sk);
2413 struct ip_sf_socklist *newpsl, *psl;
2414 struct net *net = sock_net(sk);
2417 if (!ipv4_is_multicast(addr))
2419 if (msf->imsf_fmode != MCAST_INCLUDE &&
2420 msf->imsf_fmode != MCAST_EXCLUDE)
2425 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2426 imr.imr_address.s_addr = msf->imsf_interface;
2427 imr.imr_ifindex = ifindex;
2428 in_dev = ip_mc_find_dev(net, &imr);
2435 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2436 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2441 for_each_pmc_rtnl(inet, pmc) {
2442 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2443 pmc->multi.imr_ifindex == imr.imr_ifindex)
2446 if (!pmc) { /* must have a prior join */
2450 if (msf->imsf_numsrc) {
2451 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2457 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2458 memcpy(newpsl->sl_addr, msf->imsf_slist,
2459 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2460 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2461 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2463 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2468 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2469 msf->imsf_fmode, 0, NULL, 0);
2471 psl = rtnl_dereference(pmc->sflist);
2473 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2474 psl->sl_count, psl->sl_addr, 0);
2475 /* decrease mem now to avoid the memleak warning */
2476 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2478 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2481 rcu_assign_pointer(pmc->sflist, newpsl);
2483 kfree_rcu(psl, rcu);
2484 pmc->sfmode = msf->imsf_fmode;
2488 err = ip_mc_leave_group(sk, &imr);
2492 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2493 struct ip_msfilter __user *optval, int __user *optlen)
2495 int err, len, count, copycount;
2496 struct ip_mreqn imr;
2497 __be32 addr = msf->imsf_multiaddr;
2498 struct ip_mc_socklist *pmc;
2499 struct in_device *in_dev;
2500 struct inet_sock *inet = inet_sk(sk);
2501 struct ip_sf_socklist *psl;
2502 struct net *net = sock_net(sk);
2506 if (!ipv4_is_multicast(addr))
2509 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2510 imr.imr_address.s_addr = msf->imsf_interface;
2511 imr.imr_ifindex = 0;
2512 in_dev = ip_mc_find_dev(net, &imr);
2518 err = -EADDRNOTAVAIL;
2520 for_each_pmc_rtnl(inet, pmc) {
2521 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2522 pmc->multi.imr_ifindex == imr.imr_ifindex)
2525 if (!pmc) /* must have a prior join */
2527 msf->imsf_fmode = pmc->sfmode;
2528 psl = rtnl_dereference(pmc->sflist);
2533 count = psl->sl_count;
2535 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2536 len = copycount * sizeof(psl->sl_addr[0]);
2537 msf->imsf_numsrc = count;
2538 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2539 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2543 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2550 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2551 struct group_filter __user *optval, int __user *optlen)
2553 int err, i, count, copycount;
2554 struct sockaddr_in *psin;
2556 struct ip_mc_socklist *pmc;
2557 struct inet_sock *inet = inet_sk(sk);
2558 struct ip_sf_socklist *psl;
2562 psin = (struct sockaddr_in *)&gsf->gf_group;
2563 if (psin->sin_family != AF_INET)
2565 addr = psin->sin_addr.s_addr;
2566 if (!ipv4_is_multicast(addr))
2569 err = -EADDRNOTAVAIL;
2571 for_each_pmc_rtnl(inet, pmc) {
2572 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2573 pmc->multi.imr_ifindex == gsf->gf_interface)
2576 if (!pmc) /* must have a prior join */
2578 gsf->gf_fmode = pmc->sfmode;
2579 psl = rtnl_dereference(pmc->sflist);
2580 count = psl ? psl->sl_count : 0;
2581 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2582 gsf->gf_numsrc = count;
2583 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2584 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2587 for (i = 0; i < copycount; i++) {
2588 struct sockaddr_storage ss;
2590 psin = (struct sockaddr_in *)&ss;
2591 memset(&ss, 0, sizeof(ss));
2592 psin->sin_family = AF_INET;
2593 psin->sin_addr.s_addr = psl->sl_addr[i];
2594 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2603 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2605 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2608 struct inet_sock *inet = inet_sk(sk);
2609 struct ip_mc_socklist *pmc;
2610 struct ip_sf_socklist *psl;
2615 if (!ipv4_is_multicast(loc_addr))
2619 for_each_pmc_rcu(inet, pmc) {
2620 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2621 (pmc->multi.imr_ifindex == dif ||
2622 (sdif && pmc->multi.imr_ifindex == sdif)))
2628 psl = rcu_dereference(pmc->sflist);
2629 ret = (pmc->sfmode == MCAST_EXCLUDE);
2633 for (i = 0; i < psl->sl_count; i++) {
2634 if (psl->sl_addr[i] == rmt_addr)
2638 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2640 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2650 * A socket is closing.
2653 void ip_mc_drop_socket(struct sock *sk)
2655 struct inet_sock *inet = inet_sk(sk);
2656 struct ip_mc_socklist *iml;
2657 struct net *net = sock_net(sk);
2663 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2664 struct in_device *in_dev;
2666 inet->mc_list = iml->next_rcu;
2667 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2668 (void) ip_mc_leave_src(sk, iml, in_dev);
2670 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2671 /* decrease mem now to avoid the memleak warning */
2672 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2673 kfree_rcu(iml, rcu);
2678 /* called with rcu_read_lock() */
2679 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2681 struct ip_mc_list *im;
2682 struct ip_mc_list __rcu **mc_hash;
2683 struct ip_sf_list *psf;
2686 mc_hash = rcu_dereference(in_dev->mc_hash);
2688 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2690 for (im = rcu_dereference(mc_hash[hash]);
2692 im = rcu_dereference(im->next_hash)) {
2693 if (im->multiaddr == mc_addr)
2697 for_each_pmc_rcu(in_dev, im) {
2698 if (im->multiaddr == mc_addr)
2702 if (im && proto == IPPROTO_IGMP) {
2706 spin_lock_bh(&im->lock);
2707 for (psf = im->sources; psf; psf = psf->sf_next) {
2708 if (psf->sf_inaddr == src_addr)
2712 rv = psf->sf_count[MCAST_INCLUDE] ||
2713 psf->sf_count[MCAST_EXCLUDE] !=
2714 im->sfcount[MCAST_EXCLUDE];
2716 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2717 spin_unlock_bh(&im->lock);
2719 rv = 1; /* unspecified source; tentatively allow */
2724 #if defined(CONFIG_PROC_FS)
2725 struct igmp_mc_iter_state {
2726 struct seq_net_private p;
2727 struct net_device *dev;
2728 struct in_device *in_dev;
2731 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2733 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2735 struct net *net = seq_file_net(seq);
2736 struct ip_mc_list *im = NULL;
2737 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2739 state->in_dev = NULL;
2740 for_each_netdev_rcu(net, state->dev) {
2741 struct in_device *in_dev;
2743 in_dev = __in_dev_get_rcu(state->dev);
2746 im = rcu_dereference(in_dev->mc_list);
2748 state->in_dev = in_dev;
2755 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2757 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2759 im = rcu_dereference(im->next_rcu);
2761 state->dev = next_net_device_rcu(state->dev);
2763 state->in_dev = NULL;
2766 state->in_dev = __in_dev_get_rcu(state->dev);
2769 im = rcu_dereference(state->in_dev->mc_list);
2774 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2776 struct ip_mc_list *im = igmp_mc_get_first(seq);
2778 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2780 return pos ? NULL : im;
2783 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2787 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2790 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2792 struct ip_mc_list *im;
2793 if (v == SEQ_START_TOKEN)
2794 im = igmp_mc_get_first(seq);
2796 im = igmp_mc_get_next(seq, v);
2801 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2804 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2806 state->in_dev = NULL;
2811 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2813 if (v == SEQ_START_TOKEN)
2815 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2817 struct ip_mc_list *im = (struct ip_mc_list *)v;
2818 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2822 #ifdef CONFIG_IP_MULTICAST
2823 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2824 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2830 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2831 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2832 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2835 delta = im->timer.expires - jiffies;
2837 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2838 im->multiaddr, im->users,
2840 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2846 static const struct seq_operations igmp_mc_seq_ops = {
2847 .start = igmp_mc_seq_start,
2848 .next = igmp_mc_seq_next,
2849 .stop = igmp_mc_seq_stop,
2850 .show = igmp_mc_seq_show,
2853 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2855 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2856 sizeof(struct igmp_mc_iter_state));
2859 static const struct file_operations igmp_mc_seq_fops = {
2860 .owner = THIS_MODULE,
2861 .open = igmp_mc_seq_open,
2863 .llseek = seq_lseek,
2864 .release = seq_release_net,
2867 struct igmp_mcf_iter_state {
2868 struct seq_net_private p;
2869 struct net_device *dev;
2870 struct in_device *idev;
2871 struct ip_mc_list *im;
2874 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2876 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2878 struct net *net = seq_file_net(seq);
2879 struct ip_sf_list *psf = NULL;
2880 struct ip_mc_list *im = NULL;
2881 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2885 for_each_netdev_rcu(net, state->dev) {
2886 struct in_device *idev;
2887 idev = __in_dev_get_rcu(state->dev);
2888 if (unlikely(!idev))
2890 im = rcu_dereference(idev->mc_list);
2892 spin_lock_bh(&im->lock);
2899 spin_unlock_bh(&im->lock);
2905 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2907 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2911 spin_unlock_bh(&state->im->lock);
2912 state->im = state->im->next;
2913 while (!state->im) {
2914 state->dev = next_net_device_rcu(state->dev);
2919 state->idev = __in_dev_get_rcu(state->dev);
2922 state->im = rcu_dereference(state->idev->mc_list);
2926 spin_lock_bh(&state->im->lock);
2927 psf = state->im->sources;
2933 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2935 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2937 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2939 return pos ? NULL : psf;
2942 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2946 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2949 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2951 struct ip_sf_list *psf;
2952 if (v == SEQ_START_TOKEN)
2953 psf = igmp_mcf_get_first(seq);
2955 psf = igmp_mcf_get_next(seq, v);
2960 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2963 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2964 if (likely(state->im)) {
2965 spin_unlock_bh(&state->im->lock);
2973 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2975 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2976 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2978 if (v == SEQ_START_TOKEN) {
2979 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2983 "0x%08x %6lu %6lu\n",
2984 state->dev->ifindex, state->dev->name,
2985 ntohl(state->im->multiaddr),
2986 ntohl(psf->sf_inaddr),
2987 psf->sf_count[MCAST_INCLUDE],
2988 psf->sf_count[MCAST_EXCLUDE]);
2993 static const struct seq_operations igmp_mcf_seq_ops = {
2994 .start = igmp_mcf_seq_start,
2995 .next = igmp_mcf_seq_next,
2996 .stop = igmp_mcf_seq_stop,
2997 .show = igmp_mcf_seq_show,
3000 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
3002 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
3003 sizeof(struct igmp_mcf_iter_state));
3006 static const struct file_operations igmp_mcf_seq_fops = {
3007 .owner = THIS_MODULE,
3008 .open = igmp_mcf_seq_open,
3010 .llseek = seq_lseek,
3011 .release = seq_release_net,
3014 static int __net_init igmp_net_init(struct net *net)
3016 struct proc_dir_entry *pde;
3019 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
3022 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
3023 &igmp_mcf_seq_fops);
3026 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3027 SOCK_DGRAM, 0, net);
3029 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3037 remove_proc_entry("mcfilter", net->proc_net);
3039 remove_proc_entry("igmp", net->proc_net);
3044 static void __net_exit igmp_net_exit(struct net *net)
3046 remove_proc_entry("mcfilter", net->proc_net);
3047 remove_proc_entry("igmp", net->proc_net);
3048 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3051 static struct pernet_operations igmp_net_ops = {
3052 .init = igmp_net_init,
3053 .exit = igmp_net_exit,
3057 static int igmp_netdev_event(struct notifier_block *this,
3058 unsigned long event, void *ptr)
3060 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3061 struct in_device *in_dev;
3064 case NETDEV_RESEND_IGMP:
3065 in_dev = __in_dev_get_rtnl(dev);
3067 ip_mc_rejoin_groups(in_dev);
3075 static struct notifier_block igmp_notifier = {
3076 .notifier_call = igmp_netdev_event,
3079 int __init igmp_mc_init(void)
3081 #if defined(CONFIG_PROC_FS)
3084 err = register_pernet_subsys(&igmp_net_ops);
3087 err = register_netdevice_notifier(&igmp_notifier);
3089 goto reg_notif_fail;
3093 unregister_pernet_subsys(&igmp_net_ops);
3096 return register_netdevice_notifier(&igmp_notifier);