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 <asm/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>
93 #include <net/net_namespace.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
99 #include <net/checksum.h>
100 #include <net/inet_common.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
110 #define IP_MAX_MEMBERSHIPS 20
111 #define IP_MAX_MSF 10
113 /* IGMP reports for link-local multicast groups are enabled by default */
114 int sysctl_igmp_llm_reports __read_mostly = 1;
116 #ifdef CONFIG_IP_MULTICAST
117 /* Parameter names and values are taken from igmp-v2-06 draft */
119 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
120 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
121 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
122 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
123 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
124 #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
127 #define IGMP_INITIAL_REPORT_DELAY (1)
129 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
130 * IGMP specs require to report membership immediately after
131 * joining a group, but we delay the first report by a
132 * small interval. It seems more natural and still does not
133 * contradict to specs provided this delay is small enough.
136 #define IGMP_V1_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
139 ((in_dev)->mr_v1_seen && \
140 time_before(jiffies, (in_dev)->mr_v1_seen)))
141 #define IGMP_V2_SEEN(in_dev) \
142 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
143 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
144 ((in_dev)->mr_v2_seen && \
145 time_before(jiffies, (in_dev)->mr_v2_seen)))
147 static int unsolicited_report_interval(struct in_device *in_dev)
149 int interval_ms, interval_jiffies;
151 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
152 interval_ms = IN_DEV_CONF_GET(
154 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
156 interval_ms = IN_DEV_CONF_GET(
158 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
160 interval_jiffies = msecs_to_jiffies(interval_ms);
162 /* _timer functions can't handle a delay of 0 jiffies so ensure
163 * we always return a positive value.
165 if (interval_jiffies <= 0)
166 interval_jiffies = 1;
167 return interval_jiffies;
170 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
171 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
172 static void igmpv3_clear_delrec(struct in_device *in_dev);
173 static int sf_setstate(struct ip_mc_list *pmc);
174 static void sf_markstate(struct ip_mc_list *pmc);
176 static void ip_mc_clear_src(struct ip_mc_list *pmc);
177 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
178 int sfcount, __be32 *psfsrc, int delta);
180 static void ip_ma_put(struct ip_mc_list *im)
182 if (atomic_dec_and_test(&im->refcnt)) {
183 in_dev_put(im->interface);
188 #define for_each_pmc_rcu(in_dev, pmc) \
189 for (pmc = rcu_dereference(in_dev->mc_list); \
191 pmc = rcu_dereference(pmc->next_rcu))
193 #define for_each_pmc_rtnl(in_dev, pmc) \
194 for (pmc = rtnl_dereference(in_dev->mc_list); \
196 pmc = rtnl_dereference(pmc->next_rcu))
198 #ifdef CONFIG_IP_MULTICAST
204 static void igmp_stop_timer(struct ip_mc_list *im)
206 spin_lock_bh(&im->lock);
207 if (del_timer(&im->timer))
208 atomic_dec(&im->refcnt);
211 im->unsolicit_count = 0;
212 spin_unlock_bh(&im->lock);
215 /* It must be called with locked im->lock */
216 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
218 int tv = prandom_u32() % max_delay;
221 if (!mod_timer(&im->timer, jiffies+tv+2))
222 atomic_inc(&im->refcnt);
225 static void igmp_gq_start_timer(struct in_device *in_dev)
227 int tv = prandom_u32() % in_dev->mr_maxdelay;
229 in_dev->mr_gq_running = 1;
230 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
234 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
236 int tv = prandom_u32() % delay;
238 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
242 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
244 spin_lock_bh(&im->lock);
245 im->unsolicit_count = 0;
246 if (del_timer(&im->timer)) {
247 if ((long)(im->timer.expires-jiffies) < max_delay) {
248 add_timer(&im->timer);
250 spin_unlock_bh(&im->lock);
253 atomic_dec(&im->refcnt);
255 igmp_start_timer(im, max_delay);
256 spin_unlock_bh(&im->lock);
261 * Send an IGMP report.
264 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
267 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
268 int gdeleted, int sdeleted)
271 case IGMPV3_MODE_IS_INCLUDE:
272 case IGMPV3_MODE_IS_EXCLUDE:
273 if (gdeleted || sdeleted)
275 if (!(pmc->gsquery && !psf->sf_gsresp)) {
276 if (pmc->sfmode == MCAST_INCLUDE)
278 /* don't include if this source is excluded
281 if (psf->sf_count[MCAST_INCLUDE])
282 return type == IGMPV3_MODE_IS_INCLUDE;
283 return pmc->sfcount[MCAST_EXCLUDE] ==
284 psf->sf_count[MCAST_EXCLUDE];
287 case IGMPV3_CHANGE_TO_INCLUDE:
288 if (gdeleted || sdeleted)
290 return psf->sf_count[MCAST_INCLUDE] != 0;
291 case IGMPV3_CHANGE_TO_EXCLUDE:
292 if (gdeleted || sdeleted)
294 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
295 psf->sf_count[MCAST_INCLUDE])
297 return pmc->sfcount[MCAST_EXCLUDE] ==
298 psf->sf_count[MCAST_EXCLUDE];
299 case IGMPV3_ALLOW_NEW_SOURCES:
300 if (gdeleted || !psf->sf_crcount)
302 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
303 case IGMPV3_BLOCK_OLD_SOURCES:
304 if (pmc->sfmode == MCAST_INCLUDE)
305 return gdeleted || (psf->sf_crcount && sdeleted);
306 return psf->sf_crcount && !gdeleted && !sdeleted;
312 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
314 struct ip_sf_list *psf;
317 for (psf = pmc->sources; psf; psf = psf->sf_next) {
318 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
325 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
330 struct igmpv3_report *pig;
331 struct net *net = dev_net(dev);
333 int hlen = LL_RESERVED_SPACE(dev);
334 int tlen = dev->needed_tailroom;
335 unsigned int size = mtu;
338 skb = alloc_skb(size + hlen + tlen,
339 GFP_ATOMIC | __GFP_NOWARN);
346 skb->priority = TC_PRIO_CONTROL;
348 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
350 IPPROTO_IGMP, 0, dev->ifindex);
356 skb_dst_set(skb, &rt->dst);
359 skb_reserve(skb, hlen);
360 skb_tailroom_reserve(skb, mtu, tlen);
362 skb_reset_network_header(skb);
364 skb_put(skb, sizeof(struct iphdr) + 4);
367 pip->ihl = (sizeof(struct iphdr)+4)>>2;
369 pip->frag_off = htons(IP_DF);
371 pip->daddr = fl4.daddr;
372 pip->saddr = fl4.saddr;
373 pip->protocol = IPPROTO_IGMP;
374 pip->tot_len = 0; /* filled in later */
375 ip_select_ident(net, skb, NULL);
376 ((u8 *)&pip[1])[0] = IPOPT_RA;
377 ((u8 *)&pip[1])[1] = 4;
378 ((u8 *)&pip[1])[2] = 0;
379 ((u8 *)&pip[1])[3] = 0;
381 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
382 skb_put(skb, sizeof(*pig));
383 pig = igmpv3_report_hdr(skb);
384 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
392 static int igmpv3_sendpack(struct sk_buff *skb)
394 struct igmphdr *pig = igmp_hdr(skb);
395 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
397 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
399 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
402 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
404 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
407 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
408 int type, struct igmpv3_grec **ppgr)
410 struct net_device *dev = pmc->interface->dev;
411 struct igmpv3_report *pih;
412 struct igmpv3_grec *pgr;
415 skb = igmpv3_newpack(dev, dev->mtu);
418 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
419 pgr->grec_type = type;
420 pgr->grec_auxwords = 0;
422 pgr->grec_mca = pmc->multiaddr;
423 pih = igmpv3_report_hdr(skb);
424 pih->ngrec = htons(ntohs(pih->ngrec)+1);
429 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
431 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
432 int type, int gdeleted, int sdeleted)
434 struct net_device *dev = pmc->interface->dev;
435 struct igmpv3_report *pih;
436 struct igmpv3_grec *pgr = NULL;
437 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
438 int scount, stotal, first, isquery, truncate;
440 if (pmc->multiaddr == IGMP_ALL_HOSTS)
442 if (ipv4_is_local_multicast(pmc->multiaddr) && !sysctl_igmp_llm_reports)
445 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
446 type == IGMPV3_MODE_IS_EXCLUDE;
447 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
448 type == IGMPV3_CHANGE_TO_EXCLUDE;
452 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
457 pih = skb ? igmpv3_report_hdr(skb) : NULL;
459 /* EX and TO_EX get a fresh packet, if needed */
461 if (pih && pih->ngrec &&
462 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
464 igmpv3_sendpack(skb);
465 skb = igmpv3_newpack(dev, dev->mtu);
470 for (psf = *psf_list; psf; psf = psf_next) {
473 psf_next = psf->sf_next;
475 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
480 /* clear marks on query responses */
484 if (AVAILABLE(skb) < sizeof(__be32) +
485 first*sizeof(struct igmpv3_grec)) {
486 if (truncate && !first)
487 break; /* truncate these */
489 pgr->grec_nsrcs = htons(scount);
491 igmpv3_sendpack(skb);
492 skb = igmpv3_newpack(dev, dev->mtu);
497 skb = add_grhead(skb, pmc, type, &pgr);
502 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
503 *psrc = psf->sf_inaddr;
505 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
506 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
508 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
510 psf_prev->sf_next = psf->sf_next;
512 *psf_list = psf->sf_next;
522 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
523 type == IGMPV3_BLOCK_OLD_SOURCES)
525 if (pmc->crcount || isquery) {
526 /* make sure we have room for group header */
527 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
528 igmpv3_sendpack(skb);
529 skb = NULL; /* add_grhead will get a new one */
531 skb = add_grhead(skb, pmc, type, &pgr);
535 pgr->grec_nsrcs = htons(scount);
538 pmc->gsquery = 0; /* clear query state on report */
542 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
544 struct sk_buff *skb = NULL;
549 for_each_pmc_rcu(in_dev, pmc) {
550 if (pmc->multiaddr == IGMP_ALL_HOSTS)
552 if (ipv4_is_local_multicast(pmc->multiaddr) &&
553 !sysctl_igmp_llm_reports)
555 spin_lock_bh(&pmc->lock);
556 if (pmc->sfcount[MCAST_EXCLUDE])
557 type = IGMPV3_MODE_IS_EXCLUDE;
559 type = IGMPV3_MODE_IS_INCLUDE;
560 skb = add_grec(skb, pmc, type, 0, 0);
561 spin_unlock_bh(&pmc->lock);
565 spin_lock_bh(&pmc->lock);
566 if (pmc->sfcount[MCAST_EXCLUDE])
567 type = IGMPV3_MODE_IS_EXCLUDE;
569 type = IGMPV3_MODE_IS_INCLUDE;
570 skb = add_grec(skb, pmc, type, 0, 0);
571 spin_unlock_bh(&pmc->lock);
575 return igmpv3_sendpack(skb);
579 * remove zero-count source records from a source filter list
581 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
583 struct ip_sf_list *psf_prev, *psf_next, *psf;
586 for (psf = *ppsf; psf; psf = psf_next) {
587 psf_next = psf->sf_next;
588 if (psf->sf_crcount == 0) {
590 psf_prev->sf_next = psf->sf_next;
592 *ppsf = psf->sf_next;
599 static void igmpv3_send_cr(struct in_device *in_dev)
601 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
602 struct sk_buff *skb = NULL;
606 spin_lock_bh(&in_dev->mc_tomb_lock);
610 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
611 pmc_next = pmc->next;
612 if (pmc->sfmode == MCAST_INCLUDE) {
613 type = IGMPV3_BLOCK_OLD_SOURCES;
614 dtype = IGMPV3_BLOCK_OLD_SOURCES;
615 skb = add_grec(skb, pmc, type, 1, 0);
616 skb = add_grec(skb, pmc, dtype, 1, 1);
619 if (pmc->sfmode == MCAST_EXCLUDE) {
620 type = IGMPV3_CHANGE_TO_INCLUDE;
621 skb = add_grec(skb, pmc, type, 1, 0);
624 if (pmc->crcount == 0) {
625 igmpv3_clear_zeros(&pmc->tomb);
626 igmpv3_clear_zeros(&pmc->sources);
629 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
631 pmc_prev->next = pmc_next;
633 in_dev->mc_tomb = pmc_next;
634 in_dev_put(pmc->interface);
639 spin_unlock_bh(&in_dev->mc_tomb_lock);
642 for_each_pmc_rcu(in_dev, pmc) {
643 spin_lock_bh(&pmc->lock);
644 if (pmc->sfcount[MCAST_EXCLUDE]) {
645 type = IGMPV3_BLOCK_OLD_SOURCES;
646 dtype = IGMPV3_ALLOW_NEW_SOURCES;
648 type = IGMPV3_ALLOW_NEW_SOURCES;
649 dtype = IGMPV3_BLOCK_OLD_SOURCES;
651 skb = add_grec(skb, pmc, type, 0, 0);
652 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
654 /* filter mode changes */
656 if (pmc->sfmode == MCAST_EXCLUDE)
657 type = IGMPV3_CHANGE_TO_EXCLUDE;
659 type = IGMPV3_CHANGE_TO_INCLUDE;
660 skb = add_grec(skb, pmc, type, 0, 0);
663 spin_unlock_bh(&pmc->lock);
669 (void) igmpv3_sendpack(skb);
672 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
679 struct net_device *dev = in_dev->dev;
680 struct net *net = dev_net(dev);
681 __be32 group = pmc ? pmc->multiaddr : 0;
686 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
687 return igmpv3_send_report(in_dev, pmc);
689 if (ipv4_is_local_multicast(group) && !sysctl_igmp_llm_reports)
692 if (type == IGMP_HOST_LEAVE_MESSAGE)
693 dst = IGMP_ALL_ROUTER;
697 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
699 IPPROTO_IGMP, 0, dev->ifindex);
703 hlen = LL_RESERVED_SPACE(dev);
704 tlen = dev->needed_tailroom;
705 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
710 skb->priority = TC_PRIO_CONTROL;
712 skb_dst_set(skb, &rt->dst);
714 skb_reserve(skb, hlen);
716 skb_reset_network_header(skb);
718 skb_put(skb, sizeof(struct iphdr) + 4);
721 iph->ihl = (sizeof(struct iphdr)+4)>>2;
723 iph->frag_off = htons(IP_DF);
726 iph->saddr = fl4.saddr;
727 iph->protocol = IPPROTO_IGMP;
728 ip_select_ident(net, skb, NULL);
729 ((u8 *)&iph[1])[0] = IPOPT_RA;
730 ((u8 *)&iph[1])[1] = 4;
731 ((u8 *)&iph[1])[2] = 0;
732 ((u8 *)&iph[1])[3] = 0;
734 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
739 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
741 return ip_local_out(net, skb->sk, skb);
744 static void igmp_gq_timer_expire(unsigned long data)
746 struct in_device *in_dev = (struct in_device *)data;
748 in_dev->mr_gq_running = 0;
749 igmpv3_send_report(in_dev, NULL);
753 static void igmp_ifc_timer_expire(unsigned long data)
755 struct in_device *in_dev = (struct in_device *)data;
757 igmpv3_send_cr(in_dev);
758 if (in_dev->mr_ifc_count) {
759 in_dev->mr_ifc_count--;
760 igmp_ifc_start_timer(in_dev,
761 unsolicited_report_interval(in_dev));
766 static void igmp_ifc_event(struct in_device *in_dev)
768 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
770 in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv;
771 igmp_ifc_start_timer(in_dev, 1);
775 static void igmp_timer_expire(unsigned long data)
777 struct ip_mc_list *im = (struct ip_mc_list *)data;
778 struct in_device *in_dev = im->interface;
780 spin_lock(&im->lock);
783 if (im->unsolicit_count) {
784 im->unsolicit_count--;
785 igmp_start_timer(im, unsolicited_report_interval(in_dev));
788 spin_unlock(&im->lock);
790 if (IGMP_V1_SEEN(in_dev))
791 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
792 else if (IGMP_V2_SEEN(in_dev))
793 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
795 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
800 /* mark EXCLUDE-mode sources */
801 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
803 struct ip_sf_list *psf;
807 for (psf = pmc->sources; psf; psf = psf->sf_next) {
810 for (i = 0; i < nsrcs; i++) {
811 /* skip inactive filters */
812 if (psf->sf_count[MCAST_INCLUDE] ||
813 pmc->sfcount[MCAST_EXCLUDE] !=
814 psf->sf_count[MCAST_EXCLUDE])
816 if (srcs[i] == psf->sf_inaddr) {
823 if (scount == nsrcs) /* all sources excluded */
828 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
830 struct ip_sf_list *psf;
833 if (pmc->sfmode == MCAST_EXCLUDE)
834 return igmp_xmarksources(pmc, nsrcs, srcs);
836 /* mark INCLUDE-mode sources */
838 for (psf = pmc->sources; psf; psf = psf->sf_next) {
841 for (i = 0; i < nsrcs; i++)
842 if (srcs[i] == psf->sf_inaddr) {
856 /* return true if packet was dropped */
857 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
859 struct ip_mc_list *im;
861 /* Timers are only set for non-local groups */
863 if (group == IGMP_ALL_HOSTS)
865 if (ipv4_is_local_multicast(group) && !sysctl_igmp_llm_reports)
869 for_each_pmc_rcu(in_dev, im) {
870 if (im->multiaddr == group) {
879 /* return true if packet was dropped */
880 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
883 struct igmphdr *ih = igmp_hdr(skb);
884 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
885 struct ip_mc_list *im;
886 __be32 group = ih->group;
893 /* Alas, old v1 router presents here. */
895 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
896 in_dev->mr_v1_seen = jiffies +
897 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
900 /* v2 router present */
901 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
902 in_dev->mr_v2_seen = jiffies +
903 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
905 /* cancel the interface change timer */
906 in_dev->mr_ifc_count = 0;
907 if (del_timer(&in_dev->mr_ifc_timer))
908 __in_dev_put(in_dev);
909 /* clear deleted report items */
910 igmpv3_clear_delrec(in_dev);
911 } else if (len < 12) {
912 return true; /* ignore bogus packet; freed by caller */
913 } else if (IGMP_V1_SEEN(in_dev)) {
914 /* This is a v3 query with v1 queriers present */
915 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
917 } else if (IGMP_V2_SEEN(in_dev)) {
918 /* this is a v3 query with v2 queriers present;
919 * Interpretation of the max_delay code is problematic here.
920 * A real v2 host would use ih_code directly, while v3 has a
921 * different encoding. We use the v3 encoding as more likely
922 * to be intended in a v3 query.
924 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
926 max_delay = 1; /* can't mod w/ 0 */
928 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
931 ih3 = igmpv3_query_hdr(skb);
933 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
934 + ntohs(ih3->nsrcs)*sizeof(__be32)))
936 ih3 = igmpv3_query_hdr(skb);
939 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
941 max_delay = 1; /* can't mod w/ 0 */
942 in_dev->mr_maxdelay = max_delay;
944 in_dev->mr_qrv = ih3->qrv;
945 if (!group) { /* general query */
947 return true; /* no sources allowed */
948 igmp_gq_start_timer(in_dev);
951 /* mark sources to include, if group & source-specific */
952 mark = ih3->nsrcs != 0;
956 * - Start the timers in all of our membership records
957 * that the query applies to for the interface on
958 * which the query arrived excl. those that belong
959 * to a "local" group (224.0.0.X)
960 * - For timers already running check if they need to
962 * - Use the igmp->igmp_code field as the maximum
966 for_each_pmc_rcu(in_dev, im) {
969 if (group && group != im->multiaddr)
971 if (im->multiaddr == IGMP_ALL_HOSTS)
973 if (ipv4_is_local_multicast(im->multiaddr) &&
974 !sysctl_igmp_llm_reports)
976 spin_lock_bh(&im->lock);
978 im->gsquery = im->gsquery && mark;
981 changed = !im->gsquery ||
982 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
983 spin_unlock_bh(&im->lock);
985 igmp_mod_timer(im, max_delay);
991 /* called in rcu_read_lock() section */
992 int igmp_rcv(struct sk_buff *skb)
994 /* This basically follows the spec line by line -- see RFC1112 */
996 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
1003 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1006 if (skb_checksum_simple_validate(skb))
1011 case IGMP_HOST_MEMBERSHIP_QUERY:
1012 dropped = igmp_heard_query(in_dev, skb, len);
1014 case IGMP_HOST_MEMBERSHIP_REPORT:
1015 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1016 /* Is it our report looped back? */
1017 if (rt_is_output_route(skb_rtable(skb)))
1019 /* don't rely on MC router hearing unicast reports */
1020 if (skb->pkt_type == PACKET_MULTICAST ||
1021 skb->pkt_type == PACKET_BROADCAST)
1022 dropped = igmp_heard_report(in_dev, ih->group);
1025 #ifdef CONFIG_IP_PIMSM_V1
1026 return pim_rcv_v1(skb);
1028 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1031 case IGMP_HOST_LEAVE_MESSAGE:
1033 case IGMP_MTRACE_RESP:
1051 * Add a filter to a device
1054 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1056 char buf[MAX_ADDR_LEN];
1057 struct net_device *dev = in_dev->dev;
1059 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1060 We will get multicast token leakage, when IFF_MULTICAST
1061 is changed. This check should be done in ndo_set_rx_mode
1062 routine. Something sort of:
1063 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1066 if (arp_mc_map(addr, buf, dev, 0) == 0)
1067 dev_mc_add(dev, buf);
1071 * Remove a filter from a device
1074 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1076 char buf[MAX_ADDR_LEN];
1077 struct net_device *dev = in_dev->dev;
1079 if (arp_mc_map(addr, buf, dev, 0) == 0)
1080 dev_mc_del(dev, buf);
1083 #ifdef CONFIG_IP_MULTICAST
1085 * deleted ip_mc_list manipulation
1087 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1089 struct ip_mc_list *pmc;
1091 /* this is an "ip_mc_list" for convenience; only the fields below
1092 * are actually used. In particular, the refcnt and users are not
1093 * used for management of the delete list. Using the same structure
1094 * for deleted items allows change reports to use common code with
1095 * non-deleted or query-response MCA's.
1097 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1100 spin_lock_bh(&im->lock);
1101 pmc->interface = im->interface;
1102 in_dev_hold(in_dev);
1103 pmc->multiaddr = im->multiaddr;
1104 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
1105 pmc->sfmode = im->sfmode;
1106 if (pmc->sfmode == MCAST_INCLUDE) {
1107 struct ip_sf_list *psf;
1109 pmc->tomb = im->tomb;
1110 pmc->sources = im->sources;
1111 im->tomb = im->sources = NULL;
1112 for (psf = pmc->sources; psf; psf = psf->sf_next)
1113 psf->sf_crcount = pmc->crcount;
1115 spin_unlock_bh(&im->lock);
1117 spin_lock_bh(&in_dev->mc_tomb_lock);
1118 pmc->next = in_dev->mc_tomb;
1119 in_dev->mc_tomb = pmc;
1120 spin_unlock_bh(&in_dev->mc_tomb_lock);
1123 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1125 struct ip_mc_list *pmc, *pmc_prev;
1126 struct ip_sf_list *psf, *psf_next;
1128 spin_lock_bh(&in_dev->mc_tomb_lock);
1130 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1131 if (pmc->multiaddr == multiaddr)
1137 pmc_prev->next = pmc->next;
1139 in_dev->mc_tomb = pmc->next;
1141 spin_unlock_bh(&in_dev->mc_tomb_lock);
1143 for (psf = pmc->tomb; psf; psf = psf_next) {
1144 psf_next = psf->sf_next;
1147 in_dev_put(pmc->interface);
1152 static void igmpv3_clear_delrec(struct in_device *in_dev)
1154 struct ip_mc_list *pmc, *nextpmc;
1156 spin_lock_bh(&in_dev->mc_tomb_lock);
1157 pmc = in_dev->mc_tomb;
1158 in_dev->mc_tomb = NULL;
1159 spin_unlock_bh(&in_dev->mc_tomb_lock);
1161 for (; pmc; pmc = nextpmc) {
1162 nextpmc = pmc->next;
1163 ip_mc_clear_src(pmc);
1164 in_dev_put(pmc->interface);
1167 /* clear dead sources, too */
1169 for_each_pmc_rcu(in_dev, pmc) {
1170 struct ip_sf_list *psf, *psf_next;
1172 spin_lock_bh(&pmc->lock);
1175 spin_unlock_bh(&pmc->lock);
1176 for (; psf; psf = psf_next) {
1177 psf_next = psf->sf_next;
1185 static void igmp_group_dropped(struct ip_mc_list *im)
1187 struct in_device *in_dev = im->interface;
1188 #ifdef CONFIG_IP_MULTICAST
1194 ip_mc_filter_del(in_dev, im->multiaddr);
1197 #ifdef CONFIG_IP_MULTICAST
1198 if (im->multiaddr == IGMP_ALL_HOSTS)
1200 if (ipv4_is_local_multicast(im->multiaddr) && !sysctl_igmp_llm_reports)
1203 reporter = im->reporter;
1204 igmp_stop_timer(im);
1206 if (!in_dev->dead) {
1207 if (IGMP_V1_SEEN(in_dev))
1209 if (IGMP_V2_SEEN(in_dev)) {
1211 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1215 igmpv3_add_delrec(in_dev, im);
1217 igmp_ifc_event(in_dev);
1222 static void igmp_group_added(struct ip_mc_list *im)
1224 struct in_device *in_dev = im->interface;
1226 if (im->loaded == 0) {
1228 ip_mc_filter_add(in_dev, im->multiaddr);
1231 #ifdef CONFIG_IP_MULTICAST
1232 if (im->multiaddr == IGMP_ALL_HOSTS)
1234 if (ipv4_is_local_multicast(im->multiaddr) && !sysctl_igmp_llm_reports)
1239 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1240 spin_lock_bh(&im->lock);
1241 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1242 spin_unlock_bh(&im->lock);
1247 im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
1248 igmp_ifc_event(in_dev);
1254 * Multicast list managers
1257 static u32 ip_mc_hash(const struct ip_mc_list *im)
1259 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1262 static void ip_mc_hash_add(struct in_device *in_dev,
1263 struct ip_mc_list *im)
1265 struct ip_mc_list __rcu **mc_hash;
1268 mc_hash = rtnl_dereference(in_dev->mc_hash);
1270 hash = ip_mc_hash(im);
1271 im->next_hash = mc_hash[hash];
1272 rcu_assign_pointer(mc_hash[hash], im);
1276 /* do not use a hash table for small number of items */
1277 if (in_dev->mc_count < 4)
1280 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1285 for_each_pmc_rtnl(in_dev, im) {
1286 hash = ip_mc_hash(im);
1287 im->next_hash = mc_hash[hash];
1288 RCU_INIT_POINTER(mc_hash[hash], im);
1291 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1294 static void ip_mc_hash_remove(struct in_device *in_dev,
1295 struct ip_mc_list *im)
1297 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1298 struct ip_mc_list *aux;
1302 mc_hash += ip_mc_hash(im);
1303 while ((aux = rtnl_dereference(*mc_hash)) != im)
1304 mc_hash = &aux->next_hash;
1305 *mc_hash = im->next_hash;
1310 * A socket has joined a multicast group on device dev.
1313 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1315 struct ip_mc_list *im;
1319 for_each_pmc_rtnl(in_dev, im) {
1320 if (im->multiaddr == addr) {
1322 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1327 im = kzalloc(sizeof(*im), GFP_KERNEL);
1332 im->interface = in_dev;
1333 in_dev_hold(in_dev);
1334 im->multiaddr = addr;
1335 /* initial mode is (EX, empty) */
1336 im->sfmode = MCAST_EXCLUDE;
1337 im->sfcount[MCAST_EXCLUDE] = 1;
1338 atomic_set(&im->refcnt, 1);
1339 spin_lock_init(&im->lock);
1340 #ifdef CONFIG_IP_MULTICAST
1341 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
1342 im->unsolicit_count = sysctl_igmp_qrv;
1345 im->next_rcu = in_dev->mc_list;
1347 rcu_assign_pointer(in_dev->mc_list, im);
1349 ip_mc_hash_add(in_dev, im);
1351 #ifdef CONFIG_IP_MULTICAST
1352 igmpv3_del_delrec(in_dev, im->multiaddr);
1354 igmp_group_added(im);
1356 ip_rt_multicast_event(in_dev);
1360 EXPORT_SYMBOL(ip_mc_inc_group);
1362 static int ip_mc_check_iphdr(struct sk_buff *skb)
1364 const struct iphdr *iph;
1366 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1368 if (!pskb_may_pull(skb, offset))
1373 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1376 offset += ip_hdrlen(skb) - sizeof(*iph);
1378 if (!pskb_may_pull(skb, offset))
1383 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1386 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1387 if (skb->len < len || len < offset)
1390 skb_set_transport_header(skb, offset);
1395 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1397 unsigned int len = skb_transport_offset(skb);
1399 len += sizeof(struct igmpv3_report);
1401 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1404 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1406 unsigned int len = skb_transport_offset(skb);
1408 len += sizeof(struct igmphdr);
1413 if (skb->len != len) {
1415 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1416 if (skb->len < len || !pskb_may_pull(skb, len))
1420 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1421 * all-systems destination addresses (224.0.0.1) for general queries
1423 if (!igmp_hdr(skb)->group &&
1424 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1430 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1432 switch (igmp_hdr(skb)->type) {
1433 case IGMP_HOST_LEAVE_MESSAGE:
1434 case IGMP_HOST_MEMBERSHIP_REPORT:
1435 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1438 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1439 return ip_mc_check_igmp_reportv3(skb);
1440 case IGMP_HOST_MEMBERSHIP_QUERY:
1441 return ip_mc_check_igmp_query(skb);
1447 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1449 return skb_checksum_simple_validate(skb);
1452 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1455 struct sk_buff *skb_chk;
1456 unsigned int transport_len;
1457 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1460 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1462 skb_chk = skb_checksum_trimmed(skb, transport_len,
1463 ip_mc_validate_checksum);
1467 if (!pskb_may_pull(skb_chk, len))
1470 ret = ip_mc_check_igmp_msg(skb_chk);
1475 *skb_trimmed = skb_chk;
1476 /* free now unneeded clone */
1477 else if (skb_chk != skb)
1483 if (ret && skb_chk && skb_chk != skb)
1490 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1491 * @skb: the skb to validate
1492 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1494 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1495 * skb transport header accordingly and returns zero.
1497 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1499 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1500 * -ENOMEM: A memory allocation failure happened.
1502 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1503 * to NULL): After parsing an IGMP packet successfully it will point to
1504 * an skb which has its tail aligned to the IP packet end. This might
1505 * either be the originally provided skb or a trimmed, cloned version if
1506 * the skb frame had data beyond the IP packet. A cloned skb allows us
1507 * to leave the original skb and its full frame unchanged (which might be
1508 * desirable for layer 2 frame jugglers).
1510 * Caller needs to set the skb network header and free any returned skb if it
1511 * differs from the provided skb.
1513 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1515 int ret = ip_mc_check_iphdr(skb);
1520 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1523 return __ip_mc_check_igmp(skb, skb_trimmed);
1525 EXPORT_SYMBOL(ip_mc_check_igmp);
1528 * Resend IGMP JOIN report; used by netdev notifier.
1530 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1532 #ifdef CONFIG_IP_MULTICAST
1533 struct ip_mc_list *im;
1538 for_each_pmc_rtnl(in_dev, im) {
1539 if (im->multiaddr == IGMP_ALL_HOSTS)
1541 if (ipv4_is_local_multicast(im->multiaddr) &&
1542 !sysctl_igmp_llm_reports)
1545 /* a failover is happening and switches
1546 * must be notified immediately
1548 if (IGMP_V1_SEEN(in_dev))
1549 type = IGMP_HOST_MEMBERSHIP_REPORT;
1550 else if (IGMP_V2_SEEN(in_dev))
1551 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1553 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1554 igmp_send_report(in_dev, im, type);
1560 * A socket has left a multicast group on device dev
1563 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1565 struct ip_mc_list *i;
1566 struct ip_mc_list __rcu **ip;
1570 for (ip = &in_dev->mc_list;
1571 (i = rtnl_dereference(*ip)) != NULL;
1572 ip = &i->next_rcu) {
1573 if (i->multiaddr == addr) {
1574 if (--i->users == 0) {
1575 ip_mc_hash_remove(in_dev, i);
1578 igmp_group_dropped(i);
1582 ip_rt_multicast_event(in_dev);
1591 EXPORT_SYMBOL(ip_mc_dec_group);
1593 /* Device changing type */
1595 void ip_mc_unmap(struct in_device *in_dev)
1597 struct ip_mc_list *pmc;
1601 for_each_pmc_rtnl(in_dev, pmc)
1602 igmp_group_dropped(pmc);
1605 void ip_mc_remap(struct in_device *in_dev)
1607 struct ip_mc_list *pmc;
1611 for_each_pmc_rtnl(in_dev, pmc)
1612 igmp_group_added(pmc);
1615 /* Device going down */
1617 void ip_mc_down(struct in_device *in_dev)
1619 struct ip_mc_list *pmc;
1623 for_each_pmc_rtnl(in_dev, pmc)
1624 igmp_group_dropped(pmc);
1626 #ifdef CONFIG_IP_MULTICAST
1627 in_dev->mr_ifc_count = 0;
1628 if (del_timer(&in_dev->mr_ifc_timer))
1629 __in_dev_put(in_dev);
1630 in_dev->mr_gq_running = 0;
1631 if (del_timer(&in_dev->mr_gq_timer))
1632 __in_dev_put(in_dev);
1633 igmpv3_clear_delrec(in_dev);
1636 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1639 void ip_mc_init_dev(struct in_device *in_dev)
1643 #ifdef CONFIG_IP_MULTICAST
1644 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1645 (unsigned long)in_dev);
1646 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1647 (unsigned long)in_dev);
1648 in_dev->mr_qrv = sysctl_igmp_qrv;
1651 spin_lock_init(&in_dev->mc_tomb_lock);
1654 /* Device going up */
1656 void ip_mc_up(struct in_device *in_dev)
1658 struct ip_mc_list *pmc;
1662 #ifdef CONFIG_IP_MULTICAST
1663 in_dev->mr_qrv = sysctl_igmp_qrv;
1665 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1667 for_each_pmc_rtnl(in_dev, pmc)
1668 igmp_group_added(pmc);
1672 * Device is about to be destroyed: clean up.
1675 void ip_mc_destroy_dev(struct in_device *in_dev)
1677 struct ip_mc_list *i;
1681 /* Deactivate timers */
1684 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1685 in_dev->mc_list = i->next_rcu;
1688 /* We've dropped the groups in ip_mc_down already */
1694 /* RTNL is locked */
1695 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1697 struct net_device *dev = NULL;
1698 struct in_device *idev = NULL;
1700 if (imr->imr_ifindex) {
1701 idev = inetdev_by_index(net, imr->imr_ifindex);
1704 if (imr->imr_address.s_addr) {
1705 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1711 struct rtable *rt = ip_route_output(net,
1712 imr->imr_multiaddr.s_addr,
1720 imr->imr_ifindex = dev->ifindex;
1721 idev = __in_dev_get_rtnl(dev);
1727 * Join a socket to a group
1729 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1730 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1731 #ifdef CONFIG_IP_MULTICAST
1732 int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE;
1735 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1738 struct ip_sf_list *psf, *psf_prev;
1742 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1743 if (psf->sf_inaddr == *psfsrc)
1747 if (!psf || psf->sf_count[sfmode] == 0) {
1748 /* source filter not found, or count wrong => bug */
1751 psf->sf_count[sfmode]--;
1752 if (psf->sf_count[sfmode] == 0) {
1753 ip_rt_multicast_event(pmc->interface);
1755 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1756 #ifdef CONFIG_IP_MULTICAST
1757 struct in_device *in_dev = pmc->interface;
1760 /* no more filters for this source */
1762 psf_prev->sf_next = psf->sf_next;
1764 pmc->sources = psf->sf_next;
1765 #ifdef CONFIG_IP_MULTICAST
1766 if (psf->sf_oldin &&
1767 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1768 psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
1769 psf->sf_next = pmc->tomb;
1779 #ifndef CONFIG_IP_MULTICAST
1780 #define igmp_ifc_event(x) do { } while (0)
1783 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1784 int sfcount, __be32 *psfsrc, int delta)
1786 struct ip_mc_list *pmc;
1793 for_each_pmc_rcu(in_dev, pmc) {
1794 if (*pmca == pmc->multiaddr)
1798 /* MCA not found?? bug */
1802 spin_lock_bh(&pmc->lock);
1804 #ifdef CONFIG_IP_MULTICAST
1809 if (!pmc->sfcount[sfmode])
1811 pmc->sfcount[sfmode]--;
1814 for (i = 0; i < sfcount; i++) {
1815 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1817 changerec |= rv > 0;
1821 if (pmc->sfmode == MCAST_EXCLUDE &&
1822 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1823 pmc->sfcount[MCAST_INCLUDE]) {
1824 #ifdef CONFIG_IP_MULTICAST
1825 struct ip_sf_list *psf;
1828 /* filter mode change */
1829 pmc->sfmode = MCAST_INCLUDE;
1830 #ifdef CONFIG_IP_MULTICAST
1831 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
1832 in_dev->mr_ifc_count = pmc->crcount;
1833 for (psf = pmc->sources; psf; psf = psf->sf_next)
1834 psf->sf_crcount = 0;
1835 igmp_ifc_event(pmc->interface);
1836 } else if (sf_setstate(pmc) || changerec) {
1837 igmp_ifc_event(pmc->interface);
1841 spin_unlock_bh(&pmc->lock);
1846 * Add multicast single-source filter to the interface list
1848 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1851 struct ip_sf_list *psf, *psf_prev;
1854 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1855 if (psf->sf_inaddr == *psfsrc)
1860 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1863 psf->sf_inaddr = *psfsrc;
1865 psf_prev->sf_next = psf;
1869 psf->sf_count[sfmode]++;
1870 if (psf->sf_count[sfmode] == 1) {
1871 ip_rt_multicast_event(pmc->interface);
1876 #ifdef CONFIG_IP_MULTICAST
1877 static void sf_markstate(struct ip_mc_list *pmc)
1879 struct ip_sf_list *psf;
1880 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1882 for (psf = pmc->sources; psf; psf = psf->sf_next)
1883 if (pmc->sfcount[MCAST_EXCLUDE]) {
1884 psf->sf_oldin = mca_xcount ==
1885 psf->sf_count[MCAST_EXCLUDE] &&
1886 !psf->sf_count[MCAST_INCLUDE];
1888 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1891 static int sf_setstate(struct ip_mc_list *pmc)
1893 struct ip_sf_list *psf, *dpsf;
1894 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1895 int qrv = pmc->interface->mr_qrv;
1899 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1900 if (pmc->sfcount[MCAST_EXCLUDE]) {
1901 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1902 !psf->sf_count[MCAST_INCLUDE];
1904 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1906 if (!psf->sf_oldin) {
1907 struct ip_sf_list *prev = NULL;
1909 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
1910 if (dpsf->sf_inaddr == psf->sf_inaddr)
1916 prev->sf_next = dpsf->sf_next;
1918 pmc->tomb = dpsf->sf_next;
1921 psf->sf_crcount = qrv;
1924 } else if (psf->sf_oldin) {
1926 psf->sf_crcount = 0;
1928 * add or update "delete" records if an active filter
1931 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
1932 if (dpsf->sf_inaddr == psf->sf_inaddr)
1935 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1939 /* pmc->lock held by callers */
1940 dpsf->sf_next = pmc->tomb;
1943 dpsf->sf_crcount = qrv;
1952 * Add multicast source filter list to the interface list
1954 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1955 int sfcount, __be32 *psfsrc, int delta)
1957 struct ip_mc_list *pmc;
1964 for_each_pmc_rcu(in_dev, pmc) {
1965 if (*pmca == pmc->multiaddr)
1969 /* MCA not found?? bug */
1973 spin_lock_bh(&pmc->lock);
1976 #ifdef CONFIG_IP_MULTICAST
1979 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1981 pmc->sfcount[sfmode]++;
1983 for (i = 0; i < sfcount; i++) {
1984 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
1992 pmc->sfcount[sfmode]--;
1993 for (j = 0; j < i; j++)
1994 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
1995 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1996 #ifdef CONFIG_IP_MULTICAST
1997 struct ip_sf_list *psf;
1998 in_dev = pmc->interface;
2001 /* filter mode change */
2002 if (pmc->sfcount[MCAST_EXCLUDE])
2003 pmc->sfmode = MCAST_EXCLUDE;
2004 else if (pmc->sfcount[MCAST_INCLUDE])
2005 pmc->sfmode = MCAST_INCLUDE;
2006 #ifdef CONFIG_IP_MULTICAST
2007 /* else no filters; keep old mode for reports */
2009 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
2010 in_dev->mr_ifc_count = pmc->crcount;
2011 for (psf = pmc->sources; psf; psf = psf->sf_next)
2012 psf->sf_crcount = 0;
2013 igmp_ifc_event(in_dev);
2014 } else if (sf_setstate(pmc)) {
2015 igmp_ifc_event(in_dev);
2018 spin_unlock_bh(&pmc->lock);
2022 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2024 struct ip_sf_list *psf, *nextpsf;
2026 for (psf = pmc->tomb; psf; psf = nextpsf) {
2027 nextpsf = psf->sf_next;
2031 for (psf = pmc->sources; psf; psf = nextpsf) {
2032 nextpsf = psf->sf_next;
2035 pmc->sources = NULL;
2036 pmc->sfmode = MCAST_EXCLUDE;
2037 pmc->sfcount[MCAST_INCLUDE] = 0;
2038 pmc->sfcount[MCAST_EXCLUDE] = 1;
2041 /* Join a multicast group
2044 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2046 __be32 addr = imr->imr_multiaddr.s_addr;
2047 struct ip_mc_socklist *iml, *i;
2048 struct in_device *in_dev;
2049 struct inet_sock *inet = inet_sk(sk);
2050 struct net *net = sock_net(sk);
2057 if (!ipv4_is_multicast(addr))
2060 in_dev = ip_mc_find_dev(net, imr);
2068 ifindex = imr->imr_ifindex;
2069 for_each_pmc_rtnl(inet, i) {
2070 if (i->multi.imr_multiaddr.s_addr == addr &&
2071 i->multi.imr_ifindex == ifindex)
2076 if (count >= sysctl_igmp_max_memberships)
2078 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2082 memcpy(&iml->multi, imr, sizeof(*imr));
2083 iml->next_rcu = inet->mc_list;
2085 iml->sfmode = MCAST_EXCLUDE;
2086 rcu_assign_pointer(inet->mc_list, iml);
2087 ip_mc_inc_group(in_dev, addr);
2092 EXPORT_SYMBOL(ip_mc_join_group);
2094 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2095 struct in_device *in_dev)
2097 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2101 /* any-source empty exclude case */
2102 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2103 iml->sfmode, 0, NULL, 0);
2105 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2106 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2107 RCU_INIT_POINTER(iml->sflist, NULL);
2108 /* decrease mem now to avoid the memleak warning */
2109 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2110 kfree_rcu(psf, rcu);
2114 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2116 struct inet_sock *inet = inet_sk(sk);
2117 struct ip_mc_socklist *iml;
2118 struct ip_mc_socklist __rcu **imlp;
2119 struct in_device *in_dev;
2120 struct net *net = sock_net(sk);
2121 __be32 group = imr->imr_multiaddr.s_addr;
2123 int ret = -EADDRNOTAVAIL;
2127 in_dev = ip_mc_find_dev(net, imr);
2128 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2132 ifindex = imr->imr_ifindex;
2133 for (imlp = &inet->mc_list;
2134 (iml = rtnl_dereference(*imlp)) != NULL;
2135 imlp = &iml->next_rcu) {
2136 if (iml->multi.imr_multiaddr.s_addr != group)
2139 if (iml->multi.imr_ifindex != ifindex)
2141 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2142 iml->multi.imr_address.s_addr)
2145 (void) ip_mc_leave_src(sk, iml, in_dev);
2147 *imlp = iml->next_rcu;
2150 ip_mc_dec_group(in_dev, group);
2152 /* decrease mem now to avoid the memleak warning */
2153 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2154 kfree_rcu(iml, rcu);
2160 EXPORT_SYMBOL(ip_mc_leave_group);
2162 int ip_mc_source(int add, int omode, struct sock *sk, struct
2163 ip_mreq_source *mreqs, int ifindex)
2166 struct ip_mreqn imr;
2167 __be32 addr = mreqs->imr_multiaddr;
2168 struct ip_mc_socklist *pmc;
2169 struct in_device *in_dev = NULL;
2170 struct inet_sock *inet = inet_sk(sk);
2171 struct ip_sf_socklist *psl;
2172 struct net *net = sock_net(sk);
2176 if (!ipv4_is_multicast(addr))
2181 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2182 imr.imr_address.s_addr = mreqs->imr_interface;
2183 imr.imr_ifindex = ifindex;
2184 in_dev = ip_mc_find_dev(net, &imr);
2190 err = -EADDRNOTAVAIL;
2192 for_each_pmc_rtnl(inet, pmc) {
2193 if ((pmc->multi.imr_multiaddr.s_addr ==
2194 imr.imr_multiaddr.s_addr) &&
2195 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2198 if (!pmc) { /* must have a prior join */
2202 /* if a source filter was set, must be the same mode as before */
2204 if (pmc->sfmode != omode) {
2208 } else if (pmc->sfmode != omode) {
2209 /* allow mode switches for empty-set filters */
2210 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2211 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2213 pmc->sfmode = omode;
2216 psl = rtnl_dereference(pmc->sflist);
2219 goto done; /* err = -EADDRNOTAVAIL */
2221 for (i = 0; i < psl->sl_count; i++) {
2222 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2227 if (rv) /* source not found */
2228 goto done; /* err = -EADDRNOTAVAIL */
2230 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2231 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2236 /* update the interface filter */
2237 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2238 &mreqs->imr_sourceaddr, 1);
2240 for (j = i+1; j < psl->sl_count; j++)
2241 psl->sl_addr[j-1] = psl->sl_addr[j];
2246 /* else, add a new source to the filter */
2248 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2252 if (!psl || psl->sl_count == psl->sl_max) {
2253 struct ip_sf_socklist *newpsl;
2254 int count = IP_SFBLOCK;
2257 count += psl->sl_max;
2258 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2263 newpsl->sl_max = count;
2264 newpsl->sl_count = count - IP_SFBLOCK;
2266 for (i = 0; i < psl->sl_count; i++)
2267 newpsl->sl_addr[i] = psl->sl_addr[i];
2268 /* decrease mem now to avoid the memleak warning */
2269 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2270 kfree_rcu(psl, rcu);
2272 rcu_assign_pointer(pmc->sflist, newpsl);
2275 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2276 for (i = 0; i < psl->sl_count; i++) {
2277 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2282 if (rv == 0) /* address already there is an error */
2284 for (j = psl->sl_count-1; j >= i; j--)
2285 psl->sl_addr[j+1] = psl->sl_addr[j];
2286 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2289 /* update the interface list */
2290 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2291 &mreqs->imr_sourceaddr, 1);
2294 err = ip_mc_leave_group(sk, &imr);
2298 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2301 struct ip_mreqn imr;
2302 __be32 addr = msf->imsf_multiaddr;
2303 struct ip_mc_socklist *pmc;
2304 struct in_device *in_dev;
2305 struct inet_sock *inet = inet_sk(sk);
2306 struct ip_sf_socklist *newpsl, *psl;
2307 struct net *net = sock_net(sk);
2310 if (!ipv4_is_multicast(addr))
2312 if (msf->imsf_fmode != MCAST_INCLUDE &&
2313 msf->imsf_fmode != MCAST_EXCLUDE)
2318 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2319 imr.imr_address.s_addr = msf->imsf_interface;
2320 imr.imr_ifindex = ifindex;
2321 in_dev = ip_mc_find_dev(net, &imr);
2328 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2329 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2334 for_each_pmc_rtnl(inet, pmc) {
2335 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2336 pmc->multi.imr_ifindex == imr.imr_ifindex)
2339 if (!pmc) { /* must have a prior join */
2343 if (msf->imsf_numsrc) {
2344 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2350 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2351 memcpy(newpsl->sl_addr, msf->imsf_slist,
2352 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2353 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2354 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2356 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2361 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2362 msf->imsf_fmode, 0, NULL, 0);
2364 psl = rtnl_dereference(pmc->sflist);
2366 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2367 psl->sl_count, psl->sl_addr, 0);
2368 /* decrease mem now to avoid the memleak warning */
2369 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2370 kfree_rcu(psl, rcu);
2372 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2374 rcu_assign_pointer(pmc->sflist, newpsl);
2375 pmc->sfmode = msf->imsf_fmode;
2379 err = ip_mc_leave_group(sk, &imr);
2383 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2384 struct ip_msfilter __user *optval, int __user *optlen)
2386 int err, len, count, copycount;
2387 struct ip_mreqn imr;
2388 __be32 addr = msf->imsf_multiaddr;
2389 struct ip_mc_socklist *pmc;
2390 struct in_device *in_dev;
2391 struct inet_sock *inet = inet_sk(sk);
2392 struct ip_sf_socklist *psl;
2393 struct net *net = sock_net(sk);
2397 if (!ipv4_is_multicast(addr))
2400 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2401 imr.imr_address.s_addr = msf->imsf_interface;
2402 imr.imr_ifindex = 0;
2403 in_dev = ip_mc_find_dev(net, &imr);
2409 err = -EADDRNOTAVAIL;
2411 for_each_pmc_rtnl(inet, pmc) {
2412 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2413 pmc->multi.imr_ifindex == imr.imr_ifindex)
2416 if (!pmc) /* must have a prior join */
2418 msf->imsf_fmode = pmc->sfmode;
2419 psl = rtnl_dereference(pmc->sflist);
2424 count = psl->sl_count;
2426 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2427 len = copycount * sizeof(psl->sl_addr[0]);
2428 msf->imsf_numsrc = count;
2429 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2430 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2434 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2441 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2442 struct group_filter __user *optval, int __user *optlen)
2444 int err, i, count, copycount;
2445 struct sockaddr_in *psin;
2447 struct ip_mc_socklist *pmc;
2448 struct inet_sock *inet = inet_sk(sk);
2449 struct ip_sf_socklist *psl;
2453 psin = (struct sockaddr_in *)&gsf->gf_group;
2454 if (psin->sin_family != AF_INET)
2456 addr = psin->sin_addr.s_addr;
2457 if (!ipv4_is_multicast(addr))
2460 err = -EADDRNOTAVAIL;
2462 for_each_pmc_rtnl(inet, pmc) {
2463 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2464 pmc->multi.imr_ifindex == gsf->gf_interface)
2467 if (!pmc) /* must have a prior join */
2469 gsf->gf_fmode = pmc->sfmode;
2470 psl = rtnl_dereference(pmc->sflist);
2471 count = psl ? psl->sl_count : 0;
2472 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2473 gsf->gf_numsrc = count;
2474 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2475 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2478 for (i = 0; i < copycount; i++) {
2479 struct sockaddr_storage ss;
2481 psin = (struct sockaddr_in *)&ss;
2482 memset(&ss, 0, sizeof(ss));
2483 psin->sin_family = AF_INET;
2484 psin->sin_addr.s_addr = psl->sl_addr[i];
2485 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2494 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2496 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2498 struct inet_sock *inet = inet_sk(sk);
2499 struct ip_mc_socklist *pmc;
2500 struct ip_sf_socklist *psl;
2505 if (!ipv4_is_multicast(loc_addr))
2509 for_each_pmc_rcu(inet, pmc) {
2510 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2511 pmc->multi.imr_ifindex == dif)
2517 psl = rcu_dereference(pmc->sflist);
2518 ret = (pmc->sfmode == MCAST_EXCLUDE);
2522 for (i = 0; i < psl->sl_count; i++) {
2523 if (psl->sl_addr[i] == rmt_addr)
2527 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2529 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2539 * A socket is closing.
2542 void ip_mc_drop_socket(struct sock *sk)
2544 struct inet_sock *inet = inet_sk(sk);
2545 struct ip_mc_socklist *iml;
2546 struct net *net = sock_net(sk);
2552 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2553 struct in_device *in_dev;
2555 inet->mc_list = iml->next_rcu;
2556 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2557 (void) ip_mc_leave_src(sk, iml, in_dev);
2559 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2560 /* decrease mem now to avoid the memleak warning */
2561 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2562 kfree_rcu(iml, rcu);
2567 /* called with rcu_read_lock() */
2568 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2570 struct ip_mc_list *im;
2571 struct ip_mc_list __rcu **mc_hash;
2572 struct ip_sf_list *psf;
2575 mc_hash = rcu_dereference(in_dev->mc_hash);
2577 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2579 for (im = rcu_dereference(mc_hash[hash]);
2581 im = rcu_dereference(im->next_hash)) {
2582 if (im->multiaddr == mc_addr)
2586 for_each_pmc_rcu(in_dev, im) {
2587 if (im->multiaddr == mc_addr)
2591 if (im && proto == IPPROTO_IGMP) {
2595 for (psf = im->sources; psf; psf = psf->sf_next) {
2596 if (psf->sf_inaddr == src_addr)
2600 rv = psf->sf_count[MCAST_INCLUDE] ||
2601 psf->sf_count[MCAST_EXCLUDE] !=
2602 im->sfcount[MCAST_EXCLUDE];
2604 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2606 rv = 1; /* unspecified source; tentatively allow */
2611 #if defined(CONFIG_PROC_FS)
2612 struct igmp_mc_iter_state {
2613 struct seq_net_private p;
2614 struct net_device *dev;
2615 struct in_device *in_dev;
2618 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2620 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2622 struct net *net = seq_file_net(seq);
2623 struct ip_mc_list *im = NULL;
2624 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2626 state->in_dev = NULL;
2627 for_each_netdev_rcu(net, state->dev) {
2628 struct in_device *in_dev;
2630 in_dev = __in_dev_get_rcu(state->dev);
2633 im = rcu_dereference(in_dev->mc_list);
2635 state->in_dev = in_dev;
2642 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2644 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2646 im = rcu_dereference(im->next_rcu);
2648 state->dev = next_net_device_rcu(state->dev);
2650 state->in_dev = NULL;
2653 state->in_dev = __in_dev_get_rcu(state->dev);
2656 im = rcu_dereference(state->in_dev->mc_list);
2661 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2663 struct ip_mc_list *im = igmp_mc_get_first(seq);
2665 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2667 return pos ? NULL : im;
2670 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2674 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2677 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2679 struct ip_mc_list *im;
2680 if (v == SEQ_START_TOKEN)
2681 im = igmp_mc_get_first(seq);
2683 im = igmp_mc_get_next(seq, v);
2688 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2691 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2693 state->in_dev = NULL;
2698 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2700 if (v == SEQ_START_TOKEN)
2702 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2704 struct ip_mc_list *im = (struct ip_mc_list *)v;
2705 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2709 #ifdef CONFIG_IP_MULTICAST
2710 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2711 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2717 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2718 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2719 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2722 delta = im->timer.expires - jiffies;
2724 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2725 im->multiaddr, im->users,
2727 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2733 static const struct seq_operations igmp_mc_seq_ops = {
2734 .start = igmp_mc_seq_start,
2735 .next = igmp_mc_seq_next,
2736 .stop = igmp_mc_seq_stop,
2737 .show = igmp_mc_seq_show,
2740 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2742 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2743 sizeof(struct igmp_mc_iter_state));
2746 static const struct file_operations igmp_mc_seq_fops = {
2747 .owner = THIS_MODULE,
2748 .open = igmp_mc_seq_open,
2750 .llseek = seq_lseek,
2751 .release = seq_release_net,
2754 struct igmp_mcf_iter_state {
2755 struct seq_net_private p;
2756 struct net_device *dev;
2757 struct in_device *idev;
2758 struct ip_mc_list *im;
2761 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2763 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2765 struct net *net = seq_file_net(seq);
2766 struct ip_sf_list *psf = NULL;
2767 struct ip_mc_list *im = NULL;
2768 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2772 for_each_netdev_rcu(net, state->dev) {
2773 struct in_device *idev;
2774 idev = __in_dev_get_rcu(state->dev);
2775 if (unlikely(!idev))
2777 im = rcu_dereference(idev->mc_list);
2779 spin_lock_bh(&im->lock);
2786 spin_unlock_bh(&im->lock);
2792 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2794 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2798 spin_unlock_bh(&state->im->lock);
2799 state->im = state->im->next;
2800 while (!state->im) {
2801 state->dev = next_net_device_rcu(state->dev);
2806 state->idev = __in_dev_get_rcu(state->dev);
2809 state->im = rcu_dereference(state->idev->mc_list);
2813 spin_lock_bh(&state->im->lock);
2814 psf = state->im->sources;
2820 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2822 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2824 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2826 return pos ? NULL : psf;
2829 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2833 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2836 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2838 struct ip_sf_list *psf;
2839 if (v == SEQ_START_TOKEN)
2840 psf = igmp_mcf_get_first(seq);
2842 psf = igmp_mcf_get_next(seq, v);
2847 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2850 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2851 if (likely(state->im)) {
2852 spin_unlock_bh(&state->im->lock);
2860 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2862 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2863 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2865 if (v == SEQ_START_TOKEN) {
2866 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2870 "0x%08x %6lu %6lu\n",
2871 state->dev->ifindex, state->dev->name,
2872 ntohl(state->im->multiaddr),
2873 ntohl(psf->sf_inaddr),
2874 psf->sf_count[MCAST_INCLUDE],
2875 psf->sf_count[MCAST_EXCLUDE]);
2880 static const struct seq_operations igmp_mcf_seq_ops = {
2881 .start = igmp_mcf_seq_start,
2882 .next = igmp_mcf_seq_next,
2883 .stop = igmp_mcf_seq_stop,
2884 .show = igmp_mcf_seq_show,
2887 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2889 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2890 sizeof(struct igmp_mcf_iter_state));
2893 static const struct file_operations igmp_mcf_seq_fops = {
2894 .owner = THIS_MODULE,
2895 .open = igmp_mcf_seq_open,
2897 .llseek = seq_lseek,
2898 .release = seq_release_net,
2901 static int __net_init igmp_net_init(struct net *net)
2903 struct proc_dir_entry *pde;
2906 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
2909 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2910 &igmp_mcf_seq_fops);
2913 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2914 SOCK_DGRAM, 0, net);
2916 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2924 remove_proc_entry("mcfilter", net->proc_net);
2926 remove_proc_entry("igmp", net->proc_net);
2931 static void __net_exit igmp_net_exit(struct net *net)
2933 remove_proc_entry("mcfilter", net->proc_net);
2934 remove_proc_entry("igmp", net->proc_net);
2935 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
2938 static struct pernet_operations igmp_net_ops = {
2939 .init = igmp_net_init,
2940 .exit = igmp_net_exit,
2944 static int igmp_netdev_event(struct notifier_block *this,
2945 unsigned long event, void *ptr)
2947 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2948 struct in_device *in_dev;
2951 case NETDEV_RESEND_IGMP:
2952 in_dev = __in_dev_get_rtnl(dev);
2954 ip_mc_rejoin_groups(in_dev);
2962 static struct notifier_block igmp_notifier = {
2963 .notifier_call = igmp_netdev_event,
2966 int __init igmp_mc_init(void)
2968 #if defined(CONFIG_PROC_FS)
2971 err = register_pernet_subsys(&igmp_net_ops);
2974 err = register_netdevice_notifier(&igmp_notifier);
2976 goto reg_notif_fail;
2980 unregister_pernet_subsys(&igmp_net_ops);
2983 return register_netdevice_notifier(&igmp_notifier);