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>
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 #define IP_MAX_MEMBERSHIPS 20
112 #define IP_MAX_MSF 10
114 /* IGMP reports for link-local multicast groups are enabled by default */
115 int sysctl_igmp_llm_reports __read_mostly = 1;
117 #ifdef CONFIG_IP_MULTICAST
118 /* Parameter names and values are taken from igmp-v2-06 draft */
120 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
121 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
122 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
123 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
124 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
125 #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
128 #define IGMP_INITIAL_REPORT_DELAY (1)
130 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
131 * IGMP specs require to report membership immediately after
132 * joining a group, but we delay the first report by a
133 * small interval. It seems more natural and still does not
134 * contradict to specs provided this delay is small enough.
137 #define IGMP_V1_SEEN(in_dev) \
138 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
140 ((in_dev)->mr_v1_seen && \
141 time_before(jiffies, (in_dev)->mr_v1_seen)))
142 #define IGMP_V2_SEEN(in_dev) \
143 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
144 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
145 ((in_dev)->mr_v2_seen && \
146 time_before(jiffies, (in_dev)->mr_v2_seen)))
148 static int unsolicited_report_interval(struct in_device *in_dev)
150 int interval_ms, interval_jiffies;
152 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
153 interval_ms = IN_DEV_CONF_GET(
155 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
157 interval_ms = IN_DEV_CONF_GET(
159 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
161 interval_jiffies = msecs_to_jiffies(interval_ms);
163 /* _timer functions can't handle a delay of 0 jiffies so ensure
164 * we always return a positive value.
166 if (interval_jiffies <= 0)
167 interval_jiffies = 1;
168 return interval_jiffies;
171 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
172 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
173 static void igmpv3_clear_delrec(struct in_device *in_dev);
174 static int sf_setstate(struct ip_mc_list *pmc);
175 static void sf_markstate(struct ip_mc_list *pmc);
177 static void ip_mc_clear_src(struct ip_mc_list *pmc);
178 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
179 int sfcount, __be32 *psfsrc, int delta);
181 static void ip_ma_put(struct ip_mc_list *im)
183 if (atomic_dec_and_test(&im->refcnt)) {
184 in_dev_put(im->interface);
189 #define for_each_pmc_rcu(in_dev, pmc) \
190 for (pmc = rcu_dereference(in_dev->mc_list); \
192 pmc = rcu_dereference(pmc->next_rcu))
194 #define for_each_pmc_rtnl(in_dev, pmc) \
195 for (pmc = rtnl_dereference(in_dev->mc_list); \
197 pmc = rtnl_dereference(pmc->next_rcu))
199 #ifdef CONFIG_IP_MULTICAST
205 static void igmp_stop_timer(struct ip_mc_list *im)
207 spin_lock_bh(&im->lock);
208 if (del_timer(&im->timer))
209 atomic_dec(&im->refcnt);
212 im->unsolicit_count = 0;
213 spin_unlock_bh(&im->lock);
216 /* It must be called with locked im->lock */
217 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
219 int tv = prandom_u32() % max_delay;
222 if (!mod_timer(&im->timer, jiffies+tv+2))
223 atomic_inc(&im->refcnt);
226 static void igmp_gq_start_timer(struct in_device *in_dev)
228 int tv = prandom_u32() % in_dev->mr_maxdelay;
229 unsigned long exp = jiffies + tv + 2;
231 if (in_dev->mr_gq_running &&
232 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
235 in_dev->mr_gq_running = 1;
236 if (!mod_timer(&in_dev->mr_gq_timer, exp))
240 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
242 int tv = prandom_u32() % delay;
244 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
248 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
250 spin_lock_bh(&im->lock);
251 im->unsolicit_count = 0;
252 if (del_timer(&im->timer)) {
253 if ((long)(im->timer.expires-jiffies) < max_delay) {
254 add_timer(&im->timer);
256 spin_unlock_bh(&im->lock);
259 atomic_dec(&im->refcnt);
261 igmp_start_timer(im, max_delay);
262 spin_unlock_bh(&im->lock);
267 * Send an IGMP report.
270 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
273 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
274 int gdeleted, int sdeleted)
277 case IGMPV3_MODE_IS_INCLUDE:
278 case IGMPV3_MODE_IS_EXCLUDE:
279 if (gdeleted || sdeleted)
281 if (!(pmc->gsquery && !psf->sf_gsresp)) {
282 if (pmc->sfmode == MCAST_INCLUDE)
284 /* don't include if this source is excluded
287 if (psf->sf_count[MCAST_INCLUDE])
288 return type == IGMPV3_MODE_IS_INCLUDE;
289 return pmc->sfcount[MCAST_EXCLUDE] ==
290 psf->sf_count[MCAST_EXCLUDE];
293 case IGMPV3_CHANGE_TO_INCLUDE:
294 if (gdeleted || sdeleted)
296 return psf->sf_count[MCAST_INCLUDE] != 0;
297 case IGMPV3_CHANGE_TO_EXCLUDE:
298 if (gdeleted || sdeleted)
300 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
301 psf->sf_count[MCAST_INCLUDE])
303 return pmc->sfcount[MCAST_EXCLUDE] ==
304 psf->sf_count[MCAST_EXCLUDE];
305 case IGMPV3_ALLOW_NEW_SOURCES:
306 if (gdeleted || !psf->sf_crcount)
308 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
309 case IGMPV3_BLOCK_OLD_SOURCES:
310 if (pmc->sfmode == MCAST_INCLUDE)
311 return gdeleted || (psf->sf_crcount && sdeleted);
312 return psf->sf_crcount && !gdeleted && !sdeleted;
318 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
320 struct ip_sf_list *psf;
323 for (psf = pmc->sources; psf; psf = psf->sf_next) {
324 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
331 /* source address selection per RFC 3376 section 4.2.13 */
332 static __be32 igmpv3_get_srcaddr(struct net_device *dev,
333 const struct flowi4 *fl4)
335 struct in_device *in_dev = __in_dev_get_rcu(dev);
338 return htonl(INADDR_ANY);
341 if (fl4->saddr == ifa->ifa_local)
343 } endfor_ifa(in_dev);
345 return htonl(INADDR_ANY);
348 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
353 struct igmpv3_report *pig;
354 struct net *net = dev_net(dev);
356 int hlen = LL_RESERVED_SPACE(dev);
357 int tlen = dev->needed_tailroom;
358 unsigned int size = mtu;
361 skb = alloc_skb(size + hlen + tlen,
362 GFP_ATOMIC | __GFP_NOWARN);
369 skb->priority = TC_PRIO_CONTROL;
371 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
373 IPPROTO_IGMP, 0, dev->ifindex);
379 skb_dst_set(skb, &rt->dst);
382 skb_reserve(skb, hlen);
383 skb_tailroom_reserve(skb, mtu, tlen);
385 skb_reset_network_header(skb);
387 skb_put(skb, sizeof(struct iphdr) + 4);
390 pip->ihl = (sizeof(struct iphdr)+4)>>2;
392 pip->frag_off = htons(IP_DF);
394 pip->daddr = fl4.daddr;
397 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
400 pip->protocol = IPPROTO_IGMP;
401 pip->tot_len = 0; /* filled in later */
402 ip_select_ident(net, skb, NULL);
403 ((u8 *)&pip[1])[0] = IPOPT_RA;
404 ((u8 *)&pip[1])[1] = 4;
405 ((u8 *)&pip[1])[2] = 0;
406 ((u8 *)&pip[1])[3] = 0;
408 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
409 skb_put(skb, sizeof(*pig));
410 pig = igmpv3_report_hdr(skb);
411 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
419 static int igmpv3_sendpack(struct sk_buff *skb)
421 struct igmphdr *pig = igmp_hdr(skb);
422 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
424 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
426 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
429 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
431 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
434 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
435 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
437 struct net_device *dev = pmc->interface->dev;
438 struct igmpv3_report *pih;
439 struct igmpv3_grec *pgr;
442 skb = igmpv3_newpack(dev, mtu);
446 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
447 pgr->grec_type = type;
448 pgr->grec_auxwords = 0;
450 pgr->grec_mca = pmc->multiaddr;
451 pih = igmpv3_report_hdr(skb);
452 pih->ngrec = htons(ntohs(pih->ngrec)+1);
457 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
459 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
460 int type, int gdeleted, int sdeleted)
462 struct net_device *dev = pmc->interface->dev;
463 struct igmpv3_report *pih;
464 struct igmpv3_grec *pgr = NULL;
465 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
466 int scount, stotal, first, isquery, truncate;
469 if (pmc->multiaddr == IGMP_ALL_HOSTS)
471 if (ipv4_is_local_multicast(pmc->multiaddr) && !sysctl_igmp_llm_reports)
474 mtu = READ_ONCE(dev->mtu);
475 if (mtu < IPV4_MIN_MTU)
478 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
479 type == IGMPV3_MODE_IS_EXCLUDE;
480 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
481 type == IGMPV3_CHANGE_TO_EXCLUDE;
485 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
490 pih = skb ? igmpv3_report_hdr(skb) : NULL;
492 /* EX and TO_EX get a fresh packet, if needed */
494 if (pih && pih->ngrec &&
495 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
497 igmpv3_sendpack(skb);
498 skb = igmpv3_newpack(dev, mtu);
503 for (psf = *psf_list; psf; psf = psf_next) {
506 psf_next = psf->sf_next;
508 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
513 /* clear marks on query responses */
517 if (AVAILABLE(skb) < sizeof(__be32) +
518 first*sizeof(struct igmpv3_grec)) {
519 if (truncate && !first)
520 break; /* truncate these */
522 pgr->grec_nsrcs = htons(scount);
524 igmpv3_sendpack(skb);
525 skb = igmpv3_newpack(dev, mtu);
530 skb = add_grhead(skb, pmc, type, &pgr, mtu);
535 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
536 *psrc = psf->sf_inaddr;
538 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
539 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
541 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
543 psf_prev->sf_next = psf->sf_next;
545 *psf_list = psf->sf_next;
555 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
556 type == IGMPV3_BLOCK_OLD_SOURCES)
558 if (pmc->crcount || isquery) {
559 /* make sure we have room for group header */
560 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
561 igmpv3_sendpack(skb);
562 skb = NULL; /* add_grhead will get a new one */
564 skb = add_grhead(skb, pmc, type, &pgr, mtu);
568 pgr->grec_nsrcs = htons(scount);
571 pmc->gsquery = 0; /* clear query state on report */
575 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
577 struct sk_buff *skb = NULL;
582 for_each_pmc_rcu(in_dev, pmc) {
583 if (pmc->multiaddr == IGMP_ALL_HOSTS)
585 if (ipv4_is_local_multicast(pmc->multiaddr) &&
586 !sysctl_igmp_llm_reports)
588 spin_lock_bh(&pmc->lock);
589 if (pmc->sfcount[MCAST_EXCLUDE])
590 type = IGMPV3_MODE_IS_EXCLUDE;
592 type = IGMPV3_MODE_IS_INCLUDE;
593 skb = add_grec(skb, pmc, type, 0, 0);
594 spin_unlock_bh(&pmc->lock);
598 spin_lock_bh(&pmc->lock);
599 if (pmc->sfcount[MCAST_EXCLUDE])
600 type = IGMPV3_MODE_IS_EXCLUDE;
602 type = IGMPV3_MODE_IS_INCLUDE;
603 skb = add_grec(skb, pmc, type, 0, 0);
604 spin_unlock_bh(&pmc->lock);
608 return igmpv3_sendpack(skb);
612 * remove zero-count source records from a source filter list
614 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
616 struct ip_sf_list *psf_prev, *psf_next, *psf;
619 for (psf = *ppsf; psf; psf = psf_next) {
620 psf_next = psf->sf_next;
621 if (psf->sf_crcount == 0) {
623 psf_prev->sf_next = psf->sf_next;
625 *ppsf = psf->sf_next;
632 static void igmpv3_send_cr(struct in_device *in_dev)
634 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
635 struct sk_buff *skb = NULL;
639 spin_lock_bh(&in_dev->mc_tomb_lock);
643 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
644 pmc_next = pmc->next;
645 if (pmc->sfmode == MCAST_INCLUDE) {
646 type = IGMPV3_BLOCK_OLD_SOURCES;
647 dtype = IGMPV3_BLOCK_OLD_SOURCES;
648 skb = add_grec(skb, pmc, type, 1, 0);
649 skb = add_grec(skb, pmc, dtype, 1, 1);
652 if (pmc->sfmode == MCAST_EXCLUDE) {
653 type = IGMPV3_CHANGE_TO_INCLUDE;
654 skb = add_grec(skb, pmc, type, 1, 0);
657 if (pmc->crcount == 0) {
658 igmpv3_clear_zeros(&pmc->tomb);
659 igmpv3_clear_zeros(&pmc->sources);
662 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
664 pmc_prev->next = pmc_next;
666 in_dev->mc_tomb = pmc_next;
667 in_dev_put(pmc->interface);
672 spin_unlock_bh(&in_dev->mc_tomb_lock);
675 for_each_pmc_rcu(in_dev, pmc) {
676 spin_lock_bh(&pmc->lock);
677 if (pmc->sfcount[MCAST_EXCLUDE]) {
678 type = IGMPV3_BLOCK_OLD_SOURCES;
679 dtype = IGMPV3_ALLOW_NEW_SOURCES;
681 type = IGMPV3_ALLOW_NEW_SOURCES;
682 dtype = IGMPV3_BLOCK_OLD_SOURCES;
684 skb = add_grec(skb, pmc, type, 0, 0);
685 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
687 /* filter mode changes */
689 if (pmc->sfmode == MCAST_EXCLUDE)
690 type = IGMPV3_CHANGE_TO_EXCLUDE;
692 type = IGMPV3_CHANGE_TO_INCLUDE;
693 skb = add_grec(skb, pmc, type, 0, 0);
696 spin_unlock_bh(&pmc->lock);
702 (void) igmpv3_sendpack(skb);
705 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
712 struct net_device *dev = in_dev->dev;
713 struct net *net = dev_net(dev);
714 __be32 group = pmc ? pmc->multiaddr : 0;
719 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
720 return igmpv3_send_report(in_dev, pmc);
722 if (ipv4_is_local_multicast(group) && !sysctl_igmp_llm_reports)
725 if (type == IGMP_HOST_LEAVE_MESSAGE)
726 dst = IGMP_ALL_ROUTER;
730 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
732 IPPROTO_IGMP, 0, dev->ifindex);
736 hlen = LL_RESERVED_SPACE(dev);
737 tlen = dev->needed_tailroom;
738 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
743 skb->priority = TC_PRIO_CONTROL;
745 skb_dst_set(skb, &rt->dst);
747 skb_reserve(skb, hlen);
749 skb_reset_network_header(skb);
751 skb_put(skb, sizeof(struct iphdr) + 4);
754 iph->ihl = (sizeof(struct iphdr)+4)>>2;
756 iph->frag_off = htons(IP_DF);
759 iph->saddr = fl4.saddr;
760 iph->protocol = IPPROTO_IGMP;
761 ip_select_ident(net, skb, NULL);
762 ((u8 *)&iph[1])[0] = IPOPT_RA;
763 ((u8 *)&iph[1])[1] = 4;
764 ((u8 *)&iph[1])[2] = 0;
765 ((u8 *)&iph[1])[3] = 0;
767 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
772 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
774 return ip_local_out(net, skb->sk, skb);
777 static void igmp_gq_timer_expire(unsigned long data)
779 struct in_device *in_dev = (struct in_device *)data;
781 in_dev->mr_gq_running = 0;
782 igmpv3_send_report(in_dev, NULL);
786 static void igmp_ifc_timer_expire(unsigned long data)
788 struct in_device *in_dev = (struct in_device *)data;
790 igmpv3_send_cr(in_dev);
791 if (in_dev->mr_ifc_count) {
792 in_dev->mr_ifc_count--;
793 igmp_ifc_start_timer(in_dev,
794 unsolicited_report_interval(in_dev));
799 static void igmp_ifc_event(struct in_device *in_dev)
801 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
803 in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv;
804 igmp_ifc_start_timer(in_dev, 1);
808 static void igmp_timer_expire(unsigned long data)
810 struct ip_mc_list *im = (struct ip_mc_list *)data;
811 struct in_device *in_dev = im->interface;
813 spin_lock(&im->lock);
816 if (im->unsolicit_count) {
817 im->unsolicit_count--;
818 igmp_start_timer(im, unsolicited_report_interval(in_dev));
821 spin_unlock(&im->lock);
823 if (IGMP_V1_SEEN(in_dev))
824 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
825 else if (IGMP_V2_SEEN(in_dev))
826 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
828 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
833 /* mark EXCLUDE-mode sources */
834 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
836 struct ip_sf_list *psf;
840 for (psf = pmc->sources; psf; psf = psf->sf_next) {
843 for (i = 0; i < nsrcs; i++) {
844 /* skip inactive filters */
845 if (psf->sf_count[MCAST_INCLUDE] ||
846 pmc->sfcount[MCAST_EXCLUDE] !=
847 psf->sf_count[MCAST_EXCLUDE])
849 if (srcs[i] == psf->sf_inaddr) {
856 if (scount == nsrcs) /* all sources excluded */
861 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
863 struct ip_sf_list *psf;
866 if (pmc->sfmode == MCAST_EXCLUDE)
867 return igmp_xmarksources(pmc, nsrcs, srcs);
869 /* mark INCLUDE-mode sources */
871 for (psf = pmc->sources; psf; psf = psf->sf_next) {
874 for (i = 0; i < nsrcs; i++)
875 if (srcs[i] == psf->sf_inaddr) {
889 /* return true if packet was dropped */
890 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
892 struct ip_mc_list *im;
894 /* Timers are only set for non-local groups */
896 if (group == IGMP_ALL_HOSTS)
898 if (ipv4_is_local_multicast(group) && !sysctl_igmp_llm_reports)
902 for_each_pmc_rcu(in_dev, im) {
903 if (im->multiaddr == group) {
912 /* return true if packet was dropped */
913 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
916 struct igmphdr *ih = igmp_hdr(skb);
917 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
918 struct ip_mc_list *im;
919 __be32 group = ih->group;
926 /* Alas, old v1 router presents here. */
928 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
929 in_dev->mr_v1_seen = jiffies +
930 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
933 /* v2 router present */
934 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
935 in_dev->mr_v2_seen = jiffies +
936 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
938 /* cancel the interface change timer */
939 in_dev->mr_ifc_count = 0;
940 if (del_timer(&in_dev->mr_ifc_timer))
941 __in_dev_put(in_dev);
942 /* clear deleted report items */
943 igmpv3_clear_delrec(in_dev);
944 } else if (len < 12) {
945 return true; /* ignore bogus packet; freed by caller */
946 } else if (IGMP_V1_SEEN(in_dev)) {
947 /* This is a v3 query with v1 queriers present */
948 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
950 } else if (IGMP_V2_SEEN(in_dev)) {
951 /* this is a v3 query with v2 queriers present;
952 * Interpretation of the max_delay code is problematic here.
953 * A real v2 host would use ih_code directly, while v3 has a
954 * different encoding. We use the v3 encoding as more likely
955 * to be intended in a v3 query.
957 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
959 max_delay = 1; /* can't mod w/ 0 */
961 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
964 ih3 = igmpv3_query_hdr(skb);
966 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
967 + ntohs(ih3->nsrcs)*sizeof(__be32)))
969 ih3 = igmpv3_query_hdr(skb);
972 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
974 max_delay = 1; /* can't mod w/ 0 */
975 in_dev->mr_maxdelay = max_delay;
977 in_dev->mr_qrv = ih3->qrv;
978 if (!group) { /* general query */
980 return true; /* no sources allowed */
981 igmp_gq_start_timer(in_dev);
984 /* mark sources to include, if group & source-specific */
985 mark = ih3->nsrcs != 0;
989 * - Start the timers in all of our membership records
990 * that the query applies to for the interface on
991 * which the query arrived excl. those that belong
992 * to a "local" group (224.0.0.X)
993 * - For timers already running check if they need to
995 * - Use the igmp->igmp_code field as the maximum
999 for_each_pmc_rcu(in_dev, im) {
1002 if (group && group != im->multiaddr)
1004 if (im->multiaddr == IGMP_ALL_HOSTS)
1006 if (ipv4_is_local_multicast(im->multiaddr) &&
1007 !sysctl_igmp_llm_reports)
1009 spin_lock_bh(&im->lock);
1011 im->gsquery = im->gsquery && mark;
1014 changed = !im->gsquery ||
1015 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1016 spin_unlock_bh(&im->lock);
1018 igmp_mod_timer(im, max_delay);
1024 /* called in rcu_read_lock() section */
1025 int igmp_rcv(struct sk_buff *skb)
1027 /* This basically follows the spec line by line -- see RFC1112 */
1029 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
1031 bool dropped = true;
1036 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1039 if (skb_checksum_simple_validate(skb))
1044 case IGMP_HOST_MEMBERSHIP_QUERY:
1045 dropped = igmp_heard_query(in_dev, skb, len);
1047 case IGMP_HOST_MEMBERSHIP_REPORT:
1048 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1049 /* Is it our report looped back? */
1050 if (rt_is_output_route(skb_rtable(skb)))
1052 /* don't rely on MC router hearing unicast reports */
1053 if (skb->pkt_type == PACKET_MULTICAST ||
1054 skb->pkt_type == PACKET_BROADCAST)
1055 dropped = igmp_heard_report(in_dev, ih->group);
1058 #ifdef CONFIG_IP_PIMSM_V1
1059 return pim_rcv_v1(skb);
1061 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1064 case IGMP_HOST_LEAVE_MESSAGE:
1066 case IGMP_MTRACE_RESP:
1084 * Add a filter to a device
1087 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1089 char buf[MAX_ADDR_LEN];
1090 struct net_device *dev = in_dev->dev;
1092 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1093 We will get multicast token leakage, when IFF_MULTICAST
1094 is changed. This check should be done in ndo_set_rx_mode
1095 routine. Something sort of:
1096 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1099 if (arp_mc_map(addr, buf, dev, 0) == 0)
1100 dev_mc_add(dev, buf);
1104 * Remove a filter from a device
1107 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1109 char buf[MAX_ADDR_LEN];
1110 struct net_device *dev = in_dev->dev;
1112 if (arp_mc_map(addr, buf, dev, 0) == 0)
1113 dev_mc_del(dev, buf);
1116 #ifdef CONFIG_IP_MULTICAST
1118 * deleted ip_mc_list manipulation
1120 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1122 struct ip_mc_list *pmc;
1124 /* this is an "ip_mc_list" for convenience; only the fields below
1125 * are actually used. In particular, the refcnt and users are not
1126 * used for management of the delete list. Using the same structure
1127 * for deleted items allows change reports to use common code with
1128 * non-deleted or query-response MCA's.
1130 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1133 spin_lock_init(&pmc->lock);
1134 spin_lock_bh(&im->lock);
1135 pmc->interface = im->interface;
1136 in_dev_hold(in_dev);
1137 pmc->multiaddr = im->multiaddr;
1138 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
1139 pmc->sfmode = im->sfmode;
1140 if (pmc->sfmode == MCAST_INCLUDE) {
1141 struct ip_sf_list *psf;
1143 pmc->tomb = im->tomb;
1144 pmc->sources = im->sources;
1145 im->tomb = im->sources = NULL;
1146 for (psf = pmc->sources; psf; psf = psf->sf_next)
1147 psf->sf_crcount = pmc->crcount;
1149 spin_unlock_bh(&im->lock);
1151 spin_lock_bh(&in_dev->mc_tomb_lock);
1152 pmc->next = in_dev->mc_tomb;
1153 in_dev->mc_tomb = pmc;
1154 spin_unlock_bh(&in_dev->mc_tomb_lock);
1157 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1159 struct ip_mc_list *pmc, *pmc_prev;
1160 struct ip_sf_list *psf, *psf_next;
1162 spin_lock_bh(&in_dev->mc_tomb_lock);
1164 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1165 if (pmc->multiaddr == multiaddr)
1171 pmc_prev->next = pmc->next;
1173 in_dev->mc_tomb = pmc->next;
1175 spin_unlock_bh(&in_dev->mc_tomb_lock);
1177 for (psf = pmc->tomb; psf; psf = psf_next) {
1178 psf_next = psf->sf_next;
1181 in_dev_put(pmc->interface);
1186 static void igmpv3_clear_delrec(struct in_device *in_dev)
1188 struct ip_mc_list *pmc, *nextpmc;
1190 spin_lock_bh(&in_dev->mc_tomb_lock);
1191 pmc = in_dev->mc_tomb;
1192 in_dev->mc_tomb = NULL;
1193 spin_unlock_bh(&in_dev->mc_tomb_lock);
1195 for (; pmc; pmc = nextpmc) {
1196 nextpmc = pmc->next;
1197 ip_mc_clear_src(pmc);
1198 in_dev_put(pmc->interface);
1201 /* clear dead sources, too */
1203 for_each_pmc_rcu(in_dev, pmc) {
1204 struct ip_sf_list *psf, *psf_next;
1206 spin_lock_bh(&pmc->lock);
1209 spin_unlock_bh(&pmc->lock);
1210 for (; psf; psf = psf_next) {
1211 psf_next = psf->sf_next;
1219 static void igmp_group_dropped(struct ip_mc_list *im)
1221 struct in_device *in_dev = im->interface;
1222 #ifdef CONFIG_IP_MULTICAST
1228 ip_mc_filter_del(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)
1237 reporter = im->reporter;
1238 igmp_stop_timer(im);
1240 if (!in_dev->dead) {
1241 if (IGMP_V1_SEEN(in_dev))
1243 if (IGMP_V2_SEEN(in_dev)) {
1245 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1249 igmpv3_add_delrec(in_dev, im);
1251 igmp_ifc_event(in_dev);
1256 static void igmp_group_added(struct ip_mc_list *im)
1258 struct in_device *in_dev = im->interface;
1260 if (im->loaded == 0) {
1262 ip_mc_filter_add(in_dev, im->multiaddr);
1265 #ifdef CONFIG_IP_MULTICAST
1266 if (im->multiaddr == IGMP_ALL_HOSTS)
1268 if (ipv4_is_local_multicast(im->multiaddr) && !sysctl_igmp_llm_reports)
1273 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1274 spin_lock_bh(&im->lock);
1275 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1276 spin_unlock_bh(&im->lock);
1281 im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
1282 igmp_ifc_event(in_dev);
1288 * Multicast list managers
1291 static u32 ip_mc_hash(const struct ip_mc_list *im)
1293 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1296 static void ip_mc_hash_add(struct in_device *in_dev,
1297 struct ip_mc_list *im)
1299 struct ip_mc_list __rcu **mc_hash;
1302 mc_hash = rtnl_dereference(in_dev->mc_hash);
1304 hash = ip_mc_hash(im);
1305 im->next_hash = mc_hash[hash];
1306 rcu_assign_pointer(mc_hash[hash], im);
1310 /* do not use a hash table for small number of items */
1311 if (in_dev->mc_count < 4)
1314 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1319 for_each_pmc_rtnl(in_dev, im) {
1320 hash = ip_mc_hash(im);
1321 im->next_hash = mc_hash[hash];
1322 RCU_INIT_POINTER(mc_hash[hash], im);
1325 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1328 static void ip_mc_hash_remove(struct in_device *in_dev,
1329 struct ip_mc_list *im)
1331 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1332 struct ip_mc_list *aux;
1336 mc_hash += ip_mc_hash(im);
1337 while ((aux = rtnl_dereference(*mc_hash)) != im)
1338 mc_hash = &aux->next_hash;
1339 *mc_hash = im->next_hash;
1344 * A socket has joined a multicast group on device dev.
1347 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1349 struct ip_mc_list *im;
1353 for_each_pmc_rtnl(in_dev, im) {
1354 if (im->multiaddr == addr) {
1356 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1361 im = kzalloc(sizeof(*im), GFP_KERNEL);
1366 im->interface = in_dev;
1367 in_dev_hold(in_dev);
1368 im->multiaddr = addr;
1369 /* initial mode is (EX, empty) */
1370 im->sfmode = MCAST_EXCLUDE;
1371 im->sfcount[MCAST_EXCLUDE] = 1;
1372 atomic_set(&im->refcnt, 1);
1373 spin_lock_init(&im->lock);
1374 #ifdef CONFIG_IP_MULTICAST
1375 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
1376 im->unsolicit_count = sysctl_igmp_qrv;
1379 im->next_rcu = in_dev->mc_list;
1381 rcu_assign_pointer(in_dev->mc_list, im);
1383 ip_mc_hash_add(in_dev, im);
1385 #ifdef CONFIG_IP_MULTICAST
1386 igmpv3_del_delrec(in_dev, im->multiaddr);
1388 igmp_group_added(im);
1390 ip_rt_multicast_event(in_dev);
1394 EXPORT_SYMBOL(ip_mc_inc_group);
1396 static int ip_mc_check_iphdr(struct sk_buff *skb)
1398 const struct iphdr *iph;
1400 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1402 if (!pskb_may_pull(skb, offset))
1407 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1410 offset += ip_hdrlen(skb) - sizeof(*iph);
1412 if (!pskb_may_pull(skb, offset))
1417 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1420 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1421 if (skb->len < len || len < offset)
1424 skb_set_transport_header(skb, offset);
1429 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1431 unsigned int len = skb_transport_offset(skb);
1433 len += sizeof(struct igmpv3_report);
1435 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1438 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1440 unsigned int len = skb_transport_offset(skb);
1442 len += sizeof(struct igmphdr);
1447 if (skb->len != len) {
1449 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1450 if (skb->len < len || !pskb_may_pull(skb, len))
1454 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1455 * all-systems destination addresses (224.0.0.1) for general queries
1457 if (!igmp_hdr(skb)->group &&
1458 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1464 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1466 switch (igmp_hdr(skb)->type) {
1467 case IGMP_HOST_LEAVE_MESSAGE:
1468 case IGMP_HOST_MEMBERSHIP_REPORT:
1469 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1472 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1473 return ip_mc_check_igmp_reportv3(skb);
1474 case IGMP_HOST_MEMBERSHIP_QUERY:
1475 return ip_mc_check_igmp_query(skb);
1481 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1483 return skb_checksum_simple_validate(skb);
1486 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1489 struct sk_buff *skb_chk;
1490 unsigned int transport_len;
1491 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1494 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1496 skb_chk = skb_checksum_trimmed(skb, transport_len,
1497 ip_mc_validate_checksum);
1501 if (!pskb_may_pull(skb_chk, len))
1504 ret = ip_mc_check_igmp_msg(skb_chk);
1509 *skb_trimmed = skb_chk;
1510 /* free now unneeded clone */
1511 else if (skb_chk != skb)
1517 if (ret && skb_chk && skb_chk != skb)
1524 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1525 * @skb: the skb to validate
1526 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1528 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1529 * skb transport header accordingly and returns zero.
1531 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1533 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1534 * -ENOMEM: A memory allocation failure happened.
1536 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1537 * to NULL): After parsing an IGMP packet successfully it will point to
1538 * an skb which has its tail aligned to the IP packet end. This might
1539 * either be the originally provided skb or a trimmed, cloned version if
1540 * the skb frame had data beyond the IP packet. A cloned skb allows us
1541 * to leave the original skb and its full frame unchanged (which might be
1542 * desirable for layer 2 frame jugglers).
1544 * Caller needs to set the skb network header and free any returned skb if it
1545 * differs from the provided skb.
1547 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1549 int ret = ip_mc_check_iphdr(skb);
1554 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1557 return __ip_mc_check_igmp(skb, skb_trimmed);
1559 EXPORT_SYMBOL(ip_mc_check_igmp);
1562 * Resend IGMP JOIN report; used by netdev notifier.
1564 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1566 #ifdef CONFIG_IP_MULTICAST
1567 struct ip_mc_list *im;
1572 for_each_pmc_rtnl(in_dev, im) {
1573 if (im->multiaddr == IGMP_ALL_HOSTS)
1575 if (ipv4_is_local_multicast(im->multiaddr) &&
1576 !sysctl_igmp_llm_reports)
1579 /* a failover is happening and switches
1580 * must be notified immediately
1582 if (IGMP_V1_SEEN(in_dev))
1583 type = IGMP_HOST_MEMBERSHIP_REPORT;
1584 else if (IGMP_V2_SEEN(in_dev))
1585 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1587 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1588 igmp_send_report(in_dev, im, type);
1594 * A socket has left a multicast group on device dev
1597 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1599 struct ip_mc_list *i;
1600 struct ip_mc_list __rcu **ip;
1604 for (ip = &in_dev->mc_list;
1605 (i = rtnl_dereference(*ip)) != NULL;
1606 ip = &i->next_rcu) {
1607 if (i->multiaddr == addr) {
1608 if (--i->users == 0) {
1609 ip_mc_hash_remove(in_dev, i);
1612 igmp_group_dropped(i);
1616 ip_rt_multicast_event(in_dev);
1625 EXPORT_SYMBOL(ip_mc_dec_group);
1627 /* Device changing type */
1629 void ip_mc_unmap(struct in_device *in_dev)
1631 struct ip_mc_list *pmc;
1635 for_each_pmc_rtnl(in_dev, pmc)
1636 igmp_group_dropped(pmc);
1639 void ip_mc_remap(struct in_device *in_dev)
1641 struct ip_mc_list *pmc;
1645 for_each_pmc_rtnl(in_dev, pmc)
1646 igmp_group_added(pmc);
1649 /* Device going down */
1651 void ip_mc_down(struct in_device *in_dev)
1653 struct ip_mc_list *pmc;
1657 for_each_pmc_rtnl(in_dev, pmc)
1658 igmp_group_dropped(pmc);
1660 #ifdef CONFIG_IP_MULTICAST
1661 in_dev->mr_ifc_count = 0;
1662 if (del_timer(&in_dev->mr_ifc_timer))
1663 __in_dev_put(in_dev);
1664 in_dev->mr_gq_running = 0;
1665 if (del_timer(&in_dev->mr_gq_timer))
1666 __in_dev_put(in_dev);
1667 igmpv3_clear_delrec(in_dev);
1670 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1673 void ip_mc_init_dev(struct in_device *in_dev)
1677 #ifdef CONFIG_IP_MULTICAST
1678 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1679 (unsigned long)in_dev);
1680 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1681 (unsigned long)in_dev);
1682 in_dev->mr_qrv = sysctl_igmp_qrv;
1685 spin_lock_init(&in_dev->mc_tomb_lock);
1688 /* Device going up */
1690 void ip_mc_up(struct in_device *in_dev)
1692 struct ip_mc_list *pmc;
1696 #ifdef CONFIG_IP_MULTICAST
1697 in_dev->mr_qrv = sysctl_igmp_qrv;
1699 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1701 for_each_pmc_rtnl(in_dev, pmc)
1702 igmp_group_added(pmc);
1706 * Device is about to be destroyed: clean up.
1709 void ip_mc_destroy_dev(struct in_device *in_dev)
1711 struct ip_mc_list *i;
1715 /* Deactivate timers */
1718 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1719 in_dev->mc_list = i->next_rcu;
1722 /* We've dropped the groups in ip_mc_down already */
1728 /* RTNL is locked */
1729 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1731 struct net_device *dev = NULL;
1732 struct in_device *idev = NULL;
1734 if (imr->imr_ifindex) {
1735 idev = inetdev_by_index(net, imr->imr_ifindex);
1738 if (imr->imr_address.s_addr) {
1739 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1745 struct rtable *rt = ip_route_output(net,
1746 imr->imr_multiaddr.s_addr,
1754 imr->imr_ifindex = dev->ifindex;
1755 idev = __in_dev_get_rtnl(dev);
1761 * Join a socket to a group
1763 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1764 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1765 #ifdef CONFIG_IP_MULTICAST
1766 int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE;
1769 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1772 struct ip_sf_list *psf, *psf_prev;
1776 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1777 if (psf->sf_inaddr == *psfsrc)
1781 if (!psf || psf->sf_count[sfmode] == 0) {
1782 /* source filter not found, or count wrong => bug */
1785 psf->sf_count[sfmode]--;
1786 if (psf->sf_count[sfmode] == 0) {
1787 ip_rt_multicast_event(pmc->interface);
1789 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1790 #ifdef CONFIG_IP_MULTICAST
1791 struct in_device *in_dev = pmc->interface;
1794 /* no more filters for this source */
1796 psf_prev->sf_next = psf->sf_next;
1798 pmc->sources = psf->sf_next;
1799 #ifdef CONFIG_IP_MULTICAST
1800 if (psf->sf_oldin &&
1801 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1802 psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
1803 psf->sf_next = pmc->tomb;
1813 #ifndef CONFIG_IP_MULTICAST
1814 #define igmp_ifc_event(x) do { } while (0)
1817 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1818 int sfcount, __be32 *psfsrc, int delta)
1820 struct ip_mc_list *pmc;
1827 for_each_pmc_rcu(in_dev, pmc) {
1828 if (*pmca == pmc->multiaddr)
1832 /* MCA not found?? bug */
1836 spin_lock_bh(&pmc->lock);
1838 #ifdef CONFIG_IP_MULTICAST
1843 if (!pmc->sfcount[sfmode])
1845 pmc->sfcount[sfmode]--;
1848 for (i = 0; i < sfcount; i++) {
1849 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1851 changerec |= rv > 0;
1855 if (pmc->sfmode == MCAST_EXCLUDE &&
1856 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1857 pmc->sfcount[MCAST_INCLUDE]) {
1858 #ifdef CONFIG_IP_MULTICAST
1859 struct ip_sf_list *psf;
1862 /* filter mode change */
1863 pmc->sfmode = MCAST_INCLUDE;
1864 #ifdef CONFIG_IP_MULTICAST
1865 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
1866 in_dev->mr_ifc_count = pmc->crcount;
1867 for (psf = pmc->sources; psf; psf = psf->sf_next)
1868 psf->sf_crcount = 0;
1869 igmp_ifc_event(pmc->interface);
1870 } else if (sf_setstate(pmc) || changerec) {
1871 igmp_ifc_event(pmc->interface);
1875 spin_unlock_bh(&pmc->lock);
1880 * Add multicast single-source filter to the interface list
1882 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1885 struct ip_sf_list *psf, *psf_prev;
1888 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1889 if (psf->sf_inaddr == *psfsrc)
1894 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1897 psf->sf_inaddr = *psfsrc;
1899 psf_prev->sf_next = psf;
1903 psf->sf_count[sfmode]++;
1904 if (psf->sf_count[sfmode] == 1) {
1905 ip_rt_multicast_event(pmc->interface);
1910 #ifdef CONFIG_IP_MULTICAST
1911 static void sf_markstate(struct ip_mc_list *pmc)
1913 struct ip_sf_list *psf;
1914 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1916 for (psf = pmc->sources; psf; psf = psf->sf_next)
1917 if (pmc->sfcount[MCAST_EXCLUDE]) {
1918 psf->sf_oldin = mca_xcount ==
1919 psf->sf_count[MCAST_EXCLUDE] &&
1920 !psf->sf_count[MCAST_INCLUDE];
1922 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1925 static int sf_setstate(struct ip_mc_list *pmc)
1927 struct ip_sf_list *psf, *dpsf;
1928 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1929 int qrv = pmc->interface->mr_qrv;
1933 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1934 if (pmc->sfcount[MCAST_EXCLUDE]) {
1935 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1936 !psf->sf_count[MCAST_INCLUDE];
1938 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1940 if (!psf->sf_oldin) {
1941 struct ip_sf_list *prev = NULL;
1943 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
1944 if (dpsf->sf_inaddr == psf->sf_inaddr)
1950 prev->sf_next = dpsf->sf_next;
1952 pmc->tomb = dpsf->sf_next;
1955 psf->sf_crcount = qrv;
1958 } else if (psf->sf_oldin) {
1960 psf->sf_crcount = 0;
1962 * add or update "delete" records if an active filter
1965 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
1966 if (dpsf->sf_inaddr == psf->sf_inaddr)
1969 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1973 /* pmc->lock held by callers */
1974 dpsf->sf_next = pmc->tomb;
1977 dpsf->sf_crcount = qrv;
1986 * Add multicast source filter list to the interface list
1988 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1989 int sfcount, __be32 *psfsrc, int delta)
1991 struct ip_mc_list *pmc;
1998 for_each_pmc_rcu(in_dev, pmc) {
1999 if (*pmca == pmc->multiaddr)
2003 /* MCA not found?? bug */
2007 spin_lock_bh(&pmc->lock);
2010 #ifdef CONFIG_IP_MULTICAST
2013 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2015 pmc->sfcount[sfmode]++;
2017 for (i = 0; i < sfcount; i++) {
2018 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2026 pmc->sfcount[sfmode]--;
2027 for (j = 0; j < i; j++)
2028 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2029 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2030 #ifdef CONFIG_IP_MULTICAST
2031 struct ip_sf_list *psf;
2032 in_dev = pmc->interface;
2035 /* filter mode change */
2036 if (pmc->sfcount[MCAST_EXCLUDE])
2037 pmc->sfmode = MCAST_EXCLUDE;
2038 else if (pmc->sfcount[MCAST_INCLUDE])
2039 pmc->sfmode = MCAST_INCLUDE;
2040 #ifdef CONFIG_IP_MULTICAST
2041 /* else no filters; keep old mode for reports */
2043 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
2044 in_dev->mr_ifc_count = pmc->crcount;
2045 for (psf = pmc->sources; psf; psf = psf->sf_next)
2046 psf->sf_crcount = 0;
2047 igmp_ifc_event(in_dev);
2048 } else if (sf_setstate(pmc)) {
2049 igmp_ifc_event(in_dev);
2052 spin_unlock_bh(&pmc->lock);
2056 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2058 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
2060 spin_lock_bh(&pmc->lock);
2063 sources = pmc->sources;
2064 pmc->sources = NULL;
2065 pmc->sfmode = MCAST_EXCLUDE;
2066 pmc->sfcount[MCAST_INCLUDE] = 0;
2067 pmc->sfcount[MCAST_EXCLUDE] = 1;
2068 spin_unlock_bh(&pmc->lock);
2070 for (psf = tomb; psf; psf = nextpsf) {
2071 nextpsf = psf->sf_next;
2074 for (psf = sources; psf; psf = nextpsf) {
2075 nextpsf = psf->sf_next;
2080 /* Join a multicast group
2083 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2085 __be32 addr = imr->imr_multiaddr.s_addr;
2086 struct ip_mc_socklist *iml, *i;
2087 struct in_device *in_dev;
2088 struct inet_sock *inet = inet_sk(sk);
2089 struct net *net = sock_net(sk);
2096 if (!ipv4_is_multicast(addr))
2099 in_dev = ip_mc_find_dev(net, imr);
2107 ifindex = imr->imr_ifindex;
2108 for_each_pmc_rtnl(inet, i) {
2109 if (i->multi.imr_multiaddr.s_addr == addr &&
2110 i->multi.imr_ifindex == ifindex)
2115 if (count >= sysctl_igmp_max_memberships)
2117 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2121 memcpy(&iml->multi, imr, sizeof(*imr));
2122 iml->next_rcu = inet->mc_list;
2124 iml->sfmode = MCAST_EXCLUDE;
2125 rcu_assign_pointer(inet->mc_list, iml);
2126 ip_mc_inc_group(in_dev, addr);
2131 EXPORT_SYMBOL(ip_mc_join_group);
2133 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2134 struct in_device *in_dev)
2136 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2140 /* any-source empty exclude case */
2141 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2142 iml->sfmode, 0, NULL, 0);
2144 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2145 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2146 RCU_INIT_POINTER(iml->sflist, NULL);
2147 /* decrease mem now to avoid the memleak warning */
2148 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2149 kfree_rcu(psf, rcu);
2153 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2155 struct inet_sock *inet = inet_sk(sk);
2156 struct ip_mc_socklist *iml;
2157 struct ip_mc_socklist __rcu **imlp;
2158 struct in_device *in_dev;
2159 struct net *net = sock_net(sk);
2160 __be32 group = imr->imr_multiaddr.s_addr;
2162 int ret = -EADDRNOTAVAIL;
2166 in_dev = ip_mc_find_dev(net, imr);
2167 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2171 ifindex = imr->imr_ifindex;
2172 for (imlp = &inet->mc_list;
2173 (iml = rtnl_dereference(*imlp)) != NULL;
2174 imlp = &iml->next_rcu) {
2175 if (iml->multi.imr_multiaddr.s_addr != group)
2178 if (iml->multi.imr_ifindex != ifindex)
2180 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2181 iml->multi.imr_address.s_addr)
2184 (void) ip_mc_leave_src(sk, iml, in_dev);
2186 *imlp = iml->next_rcu;
2189 ip_mc_dec_group(in_dev, group);
2191 /* decrease mem now to avoid the memleak warning */
2192 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2193 kfree_rcu(iml, rcu);
2199 EXPORT_SYMBOL(ip_mc_leave_group);
2201 int ip_mc_source(int add, int omode, struct sock *sk, struct
2202 ip_mreq_source *mreqs, int ifindex)
2205 struct ip_mreqn imr;
2206 __be32 addr = mreqs->imr_multiaddr;
2207 struct ip_mc_socklist *pmc;
2208 struct in_device *in_dev = NULL;
2209 struct inet_sock *inet = inet_sk(sk);
2210 struct ip_sf_socklist *psl;
2211 struct net *net = sock_net(sk);
2215 if (!ipv4_is_multicast(addr))
2220 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2221 imr.imr_address.s_addr = mreqs->imr_interface;
2222 imr.imr_ifindex = ifindex;
2223 in_dev = ip_mc_find_dev(net, &imr);
2229 err = -EADDRNOTAVAIL;
2231 for_each_pmc_rtnl(inet, pmc) {
2232 if ((pmc->multi.imr_multiaddr.s_addr ==
2233 imr.imr_multiaddr.s_addr) &&
2234 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2237 if (!pmc) { /* must have a prior join */
2241 /* if a source filter was set, must be the same mode as before */
2243 if (pmc->sfmode != omode) {
2247 } else if (pmc->sfmode != omode) {
2248 /* allow mode switches for empty-set filters */
2249 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2250 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2252 pmc->sfmode = omode;
2255 psl = rtnl_dereference(pmc->sflist);
2258 goto done; /* err = -EADDRNOTAVAIL */
2260 for (i = 0; i < psl->sl_count; i++) {
2261 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2266 if (rv) /* source not found */
2267 goto done; /* err = -EADDRNOTAVAIL */
2269 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2270 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2275 /* update the interface filter */
2276 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2277 &mreqs->imr_sourceaddr, 1);
2279 for (j = i+1; j < psl->sl_count; j++)
2280 psl->sl_addr[j-1] = psl->sl_addr[j];
2285 /* else, add a new source to the filter */
2287 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2291 if (!psl || psl->sl_count == psl->sl_max) {
2292 struct ip_sf_socklist *newpsl;
2293 int count = IP_SFBLOCK;
2296 count += psl->sl_max;
2297 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2302 newpsl->sl_max = count;
2303 newpsl->sl_count = count - IP_SFBLOCK;
2305 for (i = 0; i < psl->sl_count; i++)
2306 newpsl->sl_addr[i] = psl->sl_addr[i];
2307 /* decrease mem now to avoid the memleak warning */
2308 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2309 kfree_rcu(psl, rcu);
2311 rcu_assign_pointer(pmc->sflist, newpsl);
2314 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2315 for (i = 0; i < psl->sl_count; i++) {
2316 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2321 if (rv == 0) /* address already there is an error */
2323 for (j = psl->sl_count-1; j >= i; j--)
2324 psl->sl_addr[j+1] = psl->sl_addr[j];
2325 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2328 /* update the interface list */
2329 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2330 &mreqs->imr_sourceaddr, 1);
2333 err = ip_mc_leave_group(sk, &imr);
2337 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2340 struct ip_mreqn imr;
2341 __be32 addr = msf->imsf_multiaddr;
2342 struct ip_mc_socklist *pmc;
2343 struct in_device *in_dev;
2344 struct inet_sock *inet = inet_sk(sk);
2345 struct ip_sf_socklist *newpsl, *psl;
2346 struct net *net = sock_net(sk);
2349 if (!ipv4_is_multicast(addr))
2351 if (msf->imsf_fmode != MCAST_INCLUDE &&
2352 msf->imsf_fmode != MCAST_EXCLUDE)
2357 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2358 imr.imr_address.s_addr = msf->imsf_interface;
2359 imr.imr_ifindex = ifindex;
2360 in_dev = ip_mc_find_dev(net, &imr);
2367 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2368 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2373 for_each_pmc_rtnl(inet, pmc) {
2374 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2375 pmc->multi.imr_ifindex == imr.imr_ifindex)
2378 if (!pmc) { /* must have a prior join */
2382 if (msf->imsf_numsrc) {
2383 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2389 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2390 memcpy(newpsl->sl_addr, msf->imsf_slist,
2391 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2392 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2393 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2395 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2400 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2401 msf->imsf_fmode, 0, NULL, 0);
2403 psl = rtnl_dereference(pmc->sflist);
2405 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2406 psl->sl_count, psl->sl_addr, 0);
2407 /* decrease mem now to avoid the memleak warning */
2408 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2409 kfree_rcu(psl, rcu);
2411 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2413 rcu_assign_pointer(pmc->sflist, newpsl);
2414 pmc->sfmode = msf->imsf_fmode;
2418 err = ip_mc_leave_group(sk, &imr);
2422 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2423 struct ip_msfilter __user *optval, int __user *optlen)
2425 int err, len, count, copycount;
2426 struct ip_mreqn imr;
2427 __be32 addr = msf->imsf_multiaddr;
2428 struct ip_mc_socklist *pmc;
2429 struct in_device *in_dev;
2430 struct inet_sock *inet = inet_sk(sk);
2431 struct ip_sf_socklist *psl;
2432 struct net *net = sock_net(sk);
2436 if (!ipv4_is_multicast(addr))
2439 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2440 imr.imr_address.s_addr = msf->imsf_interface;
2441 imr.imr_ifindex = 0;
2442 in_dev = ip_mc_find_dev(net, &imr);
2448 err = -EADDRNOTAVAIL;
2450 for_each_pmc_rtnl(inet, pmc) {
2451 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2452 pmc->multi.imr_ifindex == imr.imr_ifindex)
2455 if (!pmc) /* must have a prior join */
2457 msf->imsf_fmode = pmc->sfmode;
2458 psl = rtnl_dereference(pmc->sflist);
2463 count = psl->sl_count;
2465 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2466 len = copycount * sizeof(psl->sl_addr[0]);
2467 msf->imsf_numsrc = count;
2468 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2469 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2473 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2480 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2481 struct group_filter __user *optval, int __user *optlen)
2483 int err, i, count, copycount;
2484 struct sockaddr_in *psin;
2486 struct ip_mc_socklist *pmc;
2487 struct inet_sock *inet = inet_sk(sk);
2488 struct ip_sf_socklist *psl;
2492 psin = (struct sockaddr_in *)&gsf->gf_group;
2493 if (psin->sin_family != AF_INET)
2495 addr = psin->sin_addr.s_addr;
2496 if (!ipv4_is_multicast(addr))
2499 err = -EADDRNOTAVAIL;
2501 for_each_pmc_rtnl(inet, pmc) {
2502 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2503 pmc->multi.imr_ifindex == gsf->gf_interface)
2506 if (!pmc) /* must have a prior join */
2508 gsf->gf_fmode = pmc->sfmode;
2509 psl = rtnl_dereference(pmc->sflist);
2510 count = psl ? psl->sl_count : 0;
2511 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2512 gsf->gf_numsrc = count;
2513 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2514 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2517 for (i = 0; i < copycount; i++) {
2518 struct sockaddr_storage ss;
2520 psin = (struct sockaddr_in *)&ss;
2521 memset(&ss, 0, sizeof(ss));
2522 psin->sin_family = AF_INET;
2523 psin->sin_addr.s_addr = psl->sl_addr[i];
2524 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2533 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2535 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2537 struct inet_sock *inet = inet_sk(sk);
2538 struct ip_mc_socklist *pmc;
2539 struct ip_sf_socklist *psl;
2544 if (!ipv4_is_multicast(loc_addr))
2548 for_each_pmc_rcu(inet, pmc) {
2549 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2550 pmc->multi.imr_ifindex == dif)
2556 psl = rcu_dereference(pmc->sflist);
2557 ret = (pmc->sfmode == MCAST_EXCLUDE);
2561 for (i = 0; i < psl->sl_count; i++) {
2562 if (psl->sl_addr[i] == rmt_addr)
2566 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2568 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2578 * A socket is closing.
2581 void ip_mc_drop_socket(struct sock *sk)
2583 struct inet_sock *inet = inet_sk(sk);
2584 struct ip_mc_socklist *iml;
2585 struct net *net = sock_net(sk);
2591 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2592 struct in_device *in_dev;
2594 inet->mc_list = iml->next_rcu;
2595 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2596 (void) ip_mc_leave_src(sk, iml, in_dev);
2598 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2599 /* decrease mem now to avoid the memleak warning */
2600 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2601 kfree_rcu(iml, rcu);
2606 /* called with rcu_read_lock() */
2607 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2609 struct ip_mc_list *im;
2610 struct ip_mc_list __rcu **mc_hash;
2611 struct ip_sf_list *psf;
2614 mc_hash = rcu_dereference(in_dev->mc_hash);
2616 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2618 for (im = rcu_dereference(mc_hash[hash]);
2620 im = rcu_dereference(im->next_hash)) {
2621 if (im->multiaddr == mc_addr)
2625 for_each_pmc_rcu(in_dev, im) {
2626 if (im->multiaddr == mc_addr)
2630 if (im && proto == IPPROTO_IGMP) {
2634 spin_lock_bh(&im->lock);
2635 for (psf = im->sources; psf; psf = psf->sf_next) {
2636 if (psf->sf_inaddr == src_addr)
2640 rv = psf->sf_count[MCAST_INCLUDE] ||
2641 psf->sf_count[MCAST_EXCLUDE] !=
2642 im->sfcount[MCAST_EXCLUDE];
2644 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2645 spin_unlock_bh(&im->lock);
2647 rv = 1; /* unspecified source; tentatively allow */
2652 #if defined(CONFIG_PROC_FS)
2653 struct igmp_mc_iter_state {
2654 struct seq_net_private p;
2655 struct net_device *dev;
2656 struct in_device *in_dev;
2659 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2661 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2663 struct net *net = seq_file_net(seq);
2664 struct ip_mc_list *im = NULL;
2665 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2667 state->in_dev = NULL;
2668 for_each_netdev_rcu(net, state->dev) {
2669 struct in_device *in_dev;
2671 in_dev = __in_dev_get_rcu(state->dev);
2674 im = rcu_dereference(in_dev->mc_list);
2676 state->in_dev = in_dev;
2683 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2685 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2687 im = rcu_dereference(im->next_rcu);
2689 state->dev = next_net_device_rcu(state->dev);
2691 state->in_dev = NULL;
2694 state->in_dev = __in_dev_get_rcu(state->dev);
2697 im = rcu_dereference(state->in_dev->mc_list);
2702 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2704 struct ip_mc_list *im = igmp_mc_get_first(seq);
2706 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2708 return pos ? NULL : im;
2711 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2715 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2718 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2720 struct ip_mc_list *im;
2721 if (v == SEQ_START_TOKEN)
2722 im = igmp_mc_get_first(seq);
2724 im = igmp_mc_get_next(seq, v);
2729 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2732 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2734 state->in_dev = NULL;
2739 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2741 if (v == SEQ_START_TOKEN)
2743 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2745 struct ip_mc_list *im = (struct ip_mc_list *)v;
2746 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2750 #ifdef CONFIG_IP_MULTICAST
2751 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2752 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2758 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2759 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2760 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2763 delta = im->timer.expires - jiffies;
2765 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2766 im->multiaddr, im->users,
2768 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2774 static const struct seq_operations igmp_mc_seq_ops = {
2775 .start = igmp_mc_seq_start,
2776 .next = igmp_mc_seq_next,
2777 .stop = igmp_mc_seq_stop,
2778 .show = igmp_mc_seq_show,
2781 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2783 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2784 sizeof(struct igmp_mc_iter_state));
2787 static const struct file_operations igmp_mc_seq_fops = {
2788 .owner = THIS_MODULE,
2789 .open = igmp_mc_seq_open,
2791 .llseek = seq_lseek,
2792 .release = seq_release_net,
2795 struct igmp_mcf_iter_state {
2796 struct seq_net_private p;
2797 struct net_device *dev;
2798 struct in_device *idev;
2799 struct ip_mc_list *im;
2802 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2804 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2806 struct net *net = seq_file_net(seq);
2807 struct ip_sf_list *psf = NULL;
2808 struct ip_mc_list *im = NULL;
2809 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2813 for_each_netdev_rcu(net, state->dev) {
2814 struct in_device *idev;
2815 idev = __in_dev_get_rcu(state->dev);
2816 if (unlikely(!idev))
2818 im = rcu_dereference(idev->mc_list);
2820 spin_lock_bh(&im->lock);
2827 spin_unlock_bh(&im->lock);
2833 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2835 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2839 spin_unlock_bh(&state->im->lock);
2840 state->im = state->im->next;
2841 while (!state->im) {
2842 state->dev = next_net_device_rcu(state->dev);
2847 state->idev = __in_dev_get_rcu(state->dev);
2850 state->im = rcu_dereference(state->idev->mc_list);
2854 spin_lock_bh(&state->im->lock);
2855 psf = state->im->sources;
2861 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2863 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2865 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2867 return pos ? NULL : psf;
2870 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2874 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2877 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2879 struct ip_sf_list *psf;
2880 if (v == SEQ_START_TOKEN)
2881 psf = igmp_mcf_get_first(seq);
2883 psf = igmp_mcf_get_next(seq, v);
2888 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2891 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2892 if (likely(state->im)) {
2893 spin_unlock_bh(&state->im->lock);
2901 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2903 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2904 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2906 if (v == SEQ_START_TOKEN) {
2907 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2911 "0x%08x %6lu %6lu\n",
2912 state->dev->ifindex, state->dev->name,
2913 ntohl(state->im->multiaddr),
2914 ntohl(psf->sf_inaddr),
2915 psf->sf_count[MCAST_INCLUDE],
2916 psf->sf_count[MCAST_EXCLUDE]);
2921 static const struct seq_operations igmp_mcf_seq_ops = {
2922 .start = igmp_mcf_seq_start,
2923 .next = igmp_mcf_seq_next,
2924 .stop = igmp_mcf_seq_stop,
2925 .show = igmp_mcf_seq_show,
2928 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2930 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2931 sizeof(struct igmp_mcf_iter_state));
2934 static const struct file_operations igmp_mcf_seq_fops = {
2935 .owner = THIS_MODULE,
2936 .open = igmp_mcf_seq_open,
2938 .llseek = seq_lseek,
2939 .release = seq_release_net,
2942 static int __net_init igmp_net_init(struct net *net)
2944 struct proc_dir_entry *pde;
2947 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
2950 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2951 &igmp_mcf_seq_fops);
2954 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2955 SOCK_DGRAM, 0, net);
2957 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2965 remove_proc_entry("mcfilter", net->proc_net);
2967 remove_proc_entry("igmp", net->proc_net);
2972 static void __net_exit igmp_net_exit(struct net *net)
2974 remove_proc_entry("mcfilter", net->proc_net);
2975 remove_proc_entry("igmp", net->proc_net);
2976 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
2979 static struct pernet_operations igmp_net_ops = {
2980 .init = igmp_net_init,
2981 .exit = igmp_net_exit,
2985 static int igmp_netdev_event(struct notifier_block *this,
2986 unsigned long event, void *ptr)
2988 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2989 struct in_device *in_dev;
2992 case NETDEV_RESEND_IGMP:
2993 in_dev = __in_dev_get_rtnl(dev);
2995 ip_mc_rejoin_groups(in_dev);
3003 static struct notifier_block igmp_notifier = {
3004 .notifier_call = igmp_netdev_event,
3007 int __init igmp_mc_init(void)
3009 #if defined(CONFIG_PROC_FS)
3012 err = register_pernet_subsys(&igmp_net_ops);
3015 err = register_netdevice_notifier(&igmp_notifier);
3017 goto reg_notif_fail;
3021 unregister_pernet_subsys(&igmp_net_ops);
3024 return register_netdevice_notifier(&igmp_notifier);