net,rcu: convert call_rcu(ip_mc_list_reclaim) to kfree_rcu()
[linux-2.6.git] / net / ipv4 / igmp.c
blobf22f30e3f1af5e72dec3e1a7244ce2f459881954
1 /*
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.
11 * Authors:
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.
19 * Fixes:
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
23 * functions.
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
32 * points.
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"
53 * message (960131).
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
58 * true (960216).
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
70 * Vinay Kulkarni
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/types.h>
78 #include <linux/kernel.h>
79 #include <linux/jiffies.h>
80 #include <linux/string.h>
81 #include <linux/socket.h>
82 #include <linux/sockios.h>
83 #include <linux/in.h>
84 #include <linux/inet.h>
85 #include <linux/netdevice.h>
86 #include <linux/skbuff.h>
87 #include <linux/inetdevice.h>
88 #include <linux/igmp.h>
89 #include <linux/if_arp.h>
90 #include <linux/rtnetlink.h>
91 #include <linux/times.h>
93 #include <net/net_namespace.h>
94 #include <net/arp.h>
95 #include <net/ip.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
98 #include <net/sock.h>
99 #include <net/checksum.h>
100 #include <linux/netfilter_ipv4.h>
101 #ifdef CONFIG_IP_MROUTE
102 #include <linux/mroute.h>
103 #endif
104 #ifdef CONFIG_PROC_FS
105 #include <linux/proc_fs.h>
106 #include <linux/seq_file.h>
107 #endif
109 #define IP_MAX_MEMBERSHIPS 20
110 #define IP_MAX_MSF 10
112 #ifdef CONFIG_IP_MULTICAST
113 /* Parameter names and values are taken from igmp-v2-06 draft */
115 #define IGMP_V1_Router_Present_Timeout (400*HZ)
116 #define IGMP_V2_Router_Present_Timeout (400*HZ)
117 #define IGMP_Unsolicited_Report_Interval (10*HZ)
118 #define IGMP_Query_Response_Interval (10*HZ)
119 #define IGMP_Unsolicited_Report_Count 2
122 #define IGMP_Initial_Report_Delay (1)
124 /* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
131 #define IGMP_V1_SEEN(in_dev) \
132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
142 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
143 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
144 static void igmpv3_clear_delrec(struct in_device *in_dev);
145 static int sf_setstate(struct ip_mc_list *pmc);
146 static void sf_markstate(struct ip_mc_list *pmc);
147 #endif
148 static void ip_mc_clear_src(struct ip_mc_list *pmc);
149 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
152 static void ip_ma_put(struct ip_mc_list *im)
154 if (atomic_dec_and_test(&im->refcnt)) {
155 in_dev_put(im->interface);
156 kfree_rcu(im, rcu);
160 #define for_each_pmc_rcu(in_dev, pmc) \
161 for (pmc = rcu_dereference(in_dev->mc_list); \
162 pmc != NULL; \
163 pmc = rcu_dereference(pmc->next_rcu))
165 #define for_each_pmc_rtnl(in_dev, pmc) \
166 for (pmc = rtnl_dereference(in_dev->mc_list); \
167 pmc != NULL; \
168 pmc = rtnl_dereference(pmc->next_rcu))
170 #ifdef CONFIG_IP_MULTICAST
173 * Timer management
176 static void igmp_stop_timer(struct ip_mc_list *im)
178 spin_lock_bh(&im->lock);
179 if (del_timer(&im->timer))
180 atomic_dec(&im->refcnt);
181 im->tm_running = 0;
182 im->reporter = 0;
183 im->unsolicit_count = 0;
184 spin_unlock_bh(&im->lock);
187 /* It must be called with locked im->lock */
188 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
190 int tv = net_random() % max_delay;
192 im->tm_running = 1;
193 if (!mod_timer(&im->timer, jiffies+tv+2))
194 atomic_inc(&im->refcnt);
197 static void igmp_gq_start_timer(struct in_device *in_dev)
199 int tv = net_random() % in_dev->mr_maxdelay;
201 in_dev->mr_gq_running = 1;
202 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
203 in_dev_hold(in_dev);
206 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
208 int tv = net_random() % delay;
210 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
211 in_dev_hold(in_dev);
214 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
216 spin_lock_bh(&im->lock);
217 im->unsolicit_count = 0;
218 if (del_timer(&im->timer)) {
219 if ((long)(im->timer.expires-jiffies) < max_delay) {
220 add_timer(&im->timer);
221 im->tm_running = 1;
222 spin_unlock_bh(&im->lock);
223 return;
225 atomic_dec(&im->refcnt);
227 igmp_start_timer(im, max_delay);
228 spin_unlock_bh(&im->lock);
233 * Send an IGMP report.
236 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
239 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
240 int gdeleted, int sdeleted)
242 switch (type) {
243 case IGMPV3_MODE_IS_INCLUDE:
244 case IGMPV3_MODE_IS_EXCLUDE:
245 if (gdeleted || sdeleted)
246 return 0;
247 if (!(pmc->gsquery && !psf->sf_gsresp)) {
248 if (pmc->sfmode == MCAST_INCLUDE)
249 return 1;
250 /* don't include if this source is excluded
251 * in all filters
253 if (psf->sf_count[MCAST_INCLUDE])
254 return type == IGMPV3_MODE_IS_INCLUDE;
255 return pmc->sfcount[MCAST_EXCLUDE] ==
256 psf->sf_count[MCAST_EXCLUDE];
258 return 0;
259 case IGMPV3_CHANGE_TO_INCLUDE:
260 if (gdeleted || sdeleted)
261 return 0;
262 return psf->sf_count[MCAST_INCLUDE] != 0;
263 case IGMPV3_CHANGE_TO_EXCLUDE:
264 if (gdeleted || sdeleted)
265 return 0;
266 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
267 psf->sf_count[MCAST_INCLUDE])
268 return 0;
269 return pmc->sfcount[MCAST_EXCLUDE] ==
270 psf->sf_count[MCAST_EXCLUDE];
271 case IGMPV3_ALLOW_NEW_SOURCES:
272 if (gdeleted || !psf->sf_crcount)
273 return 0;
274 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
275 case IGMPV3_BLOCK_OLD_SOURCES:
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return gdeleted || (psf->sf_crcount && sdeleted);
278 return psf->sf_crcount && !gdeleted && !sdeleted;
280 return 0;
283 static int
284 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
286 struct ip_sf_list *psf;
287 int scount = 0;
289 for (psf=pmc->sources; psf; psf=psf->sf_next) {
290 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
291 continue;
292 scount++;
294 return scount;
297 #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
299 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
301 struct sk_buff *skb;
302 struct rtable *rt;
303 struct iphdr *pip;
304 struct igmpv3_report *pig;
305 struct net *net = dev_net(dev);
307 while (1) {
308 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
309 GFP_ATOMIC | __GFP_NOWARN);
310 if (skb)
311 break;
312 size >>= 1;
313 if (size < 256)
314 return NULL;
316 igmp_skb_size(skb) = size;
318 rt = ip_route_output_ports(net, NULL, IGMPV3_ALL_MCR, 0,
319 0, 0,
320 IPPROTO_IGMP, 0, dev->ifindex);
321 if (IS_ERR(rt)) {
322 kfree_skb(skb);
323 return NULL;
325 if (rt->rt_src == 0) {
326 kfree_skb(skb);
327 ip_rt_put(rt);
328 return NULL;
331 skb_dst_set(skb, &rt->dst);
332 skb->dev = dev;
334 skb_reserve(skb, LL_RESERVED_SPACE(dev));
336 skb_reset_network_header(skb);
337 pip = ip_hdr(skb);
338 skb_put(skb, sizeof(struct iphdr) + 4);
340 pip->version = 4;
341 pip->ihl = (sizeof(struct iphdr)+4)>>2;
342 pip->tos = 0xc0;
343 pip->frag_off = htons(IP_DF);
344 pip->ttl = 1;
345 pip->daddr = rt->rt_dst;
346 pip->saddr = rt->rt_src;
347 pip->protocol = IPPROTO_IGMP;
348 pip->tot_len = 0; /* filled in later */
349 ip_select_ident(pip, &rt->dst, NULL);
350 ((u8*)&pip[1])[0] = IPOPT_RA;
351 ((u8*)&pip[1])[1] = 4;
352 ((u8*)&pip[1])[2] = 0;
353 ((u8*)&pip[1])[3] = 0;
355 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
356 skb_put(skb, sizeof(*pig));
357 pig = igmpv3_report_hdr(skb);
358 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
359 pig->resv1 = 0;
360 pig->csum = 0;
361 pig->resv2 = 0;
362 pig->ngrec = 0;
363 return skb;
366 static int igmpv3_sendpack(struct sk_buff *skb)
368 struct igmphdr *pig = igmp_hdr(skb);
369 const int igmplen = skb->tail - skb->transport_header;
371 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
373 return ip_local_out(skb);
376 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
378 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
381 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
382 int type, struct igmpv3_grec **ppgr)
384 struct net_device *dev = pmc->interface->dev;
385 struct igmpv3_report *pih;
386 struct igmpv3_grec *pgr;
388 if (!skb)
389 skb = igmpv3_newpack(dev, dev->mtu);
390 if (!skb)
391 return NULL;
392 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
393 pgr->grec_type = type;
394 pgr->grec_auxwords = 0;
395 pgr->grec_nsrcs = 0;
396 pgr->grec_mca = pmc->multiaddr;
397 pih = igmpv3_report_hdr(skb);
398 pih->ngrec = htons(ntohs(pih->ngrec)+1);
399 *ppgr = pgr;
400 return skb;
403 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
404 skb_tailroom(skb)) : 0)
406 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
407 int type, int gdeleted, int sdeleted)
409 struct net_device *dev = pmc->interface->dev;
410 struct igmpv3_report *pih;
411 struct igmpv3_grec *pgr = NULL;
412 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
413 int scount, stotal, first, isquery, truncate;
415 if (pmc->multiaddr == IGMP_ALL_HOSTS)
416 return skb;
418 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
419 type == IGMPV3_MODE_IS_EXCLUDE;
420 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
421 type == IGMPV3_CHANGE_TO_EXCLUDE;
423 stotal = scount = 0;
425 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
427 if (!*psf_list)
428 goto empty_source;
430 pih = skb ? igmpv3_report_hdr(skb) : NULL;
432 /* EX and TO_EX get a fresh packet, if needed */
433 if (truncate) {
434 if (pih && pih->ngrec &&
435 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
436 if (skb)
437 igmpv3_sendpack(skb);
438 skb = igmpv3_newpack(dev, dev->mtu);
441 first = 1;
442 psf_prev = NULL;
443 for (psf=*psf_list; psf; psf=psf_next) {
444 __be32 *psrc;
446 psf_next = psf->sf_next;
448 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
449 psf_prev = psf;
450 continue;
453 /* clear marks on query responses */
454 if (isquery)
455 psf->sf_gsresp = 0;
457 if (AVAILABLE(skb) < sizeof(__be32) +
458 first*sizeof(struct igmpv3_grec)) {
459 if (truncate && !first)
460 break; /* truncate these */
461 if (pgr)
462 pgr->grec_nsrcs = htons(scount);
463 if (skb)
464 igmpv3_sendpack(skb);
465 skb = igmpv3_newpack(dev, dev->mtu);
466 first = 1;
467 scount = 0;
469 if (first) {
470 skb = add_grhead(skb, pmc, type, &pgr);
471 first = 0;
473 if (!skb)
474 return NULL;
475 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
476 *psrc = psf->sf_inaddr;
477 scount++; stotal++;
478 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
479 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
480 psf->sf_crcount--;
481 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
482 if (psf_prev)
483 psf_prev->sf_next = psf->sf_next;
484 else
485 *psf_list = psf->sf_next;
486 kfree(psf);
487 continue;
490 psf_prev = psf;
493 empty_source:
494 if (!stotal) {
495 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
496 type == IGMPV3_BLOCK_OLD_SOURCES)
497 return skb;
498 if (pmc->crcount || isquery) {
499 /* make sure we have room for group header */
500 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
501 igmpv3_sendpack(skb);
502 skb = NULL; /* add_grhead will get a new one */
504 skb = add_grhead(skb, pmc, type, &pgr);
507 if (pgr)
508 pgr->grec_nsrcs = htons(scount);
510 if (isquery)
511 pmc->gsquery = 0; /* clear query state on report */
512 return skb;
515 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
517 struct sk_buff *skb = NULL;
518 int type;
520 if (!pmc) {
521 rcu_read_lock();
522 for_each_pmc_rcu(in_dev, pmc) {
523 if (pmc->multiaddr == IGMP_ALL_HOSTS)
524 continue;
525 spin_lock_bh(&pmc->lock);
526 if (pmc->sfcount[MCAST_EXCLUDE])
527 type = IGMPV3_MODE_IS_EXCLUDE;
528 else
529 type = IGMPV3_MODE_IS_INCLUDE;
530 skb = add_grec(skb, pmc, type, 0, 0);
531 spin_unlock_bh(&pmc->lock);
533 rcu_read_unlock();
534 } else {
535 spin_lock_bh(&pmc->lock);
536 if (pmc->sfcount[MCAST_EXCLUDE])
537 type = IGMPV3_MODE_IS_EXCLUDE;
538 else
539 type = IGMPV3_MODE_IS_INCLUDE;
540 skb = add_grec(skb, pmc, type, 0, 0);
541 spin_unlock_bh(&pmc->lock);
543 if (!skb)
544 return 0;
545 return igmpv3_sendpack(skb);
549 * remove zero-count source records from a source filter list
551 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
553 struct ip_sf_list *psf_prev, *psf_next, *psf;
555 psf_prev = NULL;
556 for (psf=*ppsf; psf; psf = psf_next) {
557 psf_next = psf->sf_next;
558 if (psf->sf_crcount == 0) {
559 if (psf_prev)
560 psf_prev->sf_next = psf->sf_next;
561 else
562 *ppsf = psf->sf_next;
563 kfree(psf);
564 } else
565 psf_prev = psf;
569 static void igmpv3_send_cr(struct in_device *in_dev)
571 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
572 struct sk_buff *skb = NULL;
573 int type, dtype;
575 rcu_read_lock();
576 spin_lock_bh(&in_dev->mc_tomb_lock);
578 /* deleted MCA's */
579 pmc_prev = NULL;
580 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
581 pmc_next = pmc->next;
582 if (pmc->sfmode == MCAST_INCLUDE) {
583 type = IGMPV3_BLOCK_OLD_SOURCES;
584 dtype = IGMPV3_BLOCK_OLD_SOURCES;
585 skb = add_grec(skb, pmc, type, 1, 0);
586 skb = add_grec(skb, pmc, dtype, 1, 1);
588 if (pmc->crcount) {
589 if (pmc->sfmode == MCAST_EXCLUDE) {
590 type = IGMPV3_CHANGE_TO_INCLUDE;
591 skb = add_grec(skb, pmc, type, 1, 0);
593 pmc->crcount--;
594 if (pmc->crcount == 0) {
595 igmpv3_clear_zeros(&pmc->tomb);
596 igmpv3_clear_zeros(&pmc->sources);
599 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
600 if (pmc_prev)
601 pmc_prev->next = pmc_next;
602 else
603 in_dev->mc_tomb = pmc_next;
604 in_dev_put(pmc->interface);
605 kfree(pmc);
606 } else
607 pmc_prev = pmc;
609 spin_unlock_bh(&in_dev->mc_tomb_lock);
611 /* change recs */
612 for_each_pmc_rcu(in_dev, pmc) {
613 spin_lock_bh(&pmc->lock);
614 if (pmc->sfcount[MCAST_EXCLUDE]) {
615 type = IGMPV3_BLOCK_OLD_SOURCES;
616 dtype = IGMPV3_ALLOW_NEW_SOURCES;
617 } else {
618 type = IGMPV3_ALLOW_NEW_SOURCES;
619 dtype = IGMPV3_BLOCK_OLD_SOURCES;
621 skb = add_grec(skb, pmc, type, 0, 0);
622 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
624 /* filter mode changes */
625 if (pmc->crcount) {
626 if (pmc->sfmode == MCAST_EXCLUDE)
627 type = IGMPV3_CHANGE_TO_EXCLUDE;
628 else
629 type = IGMPV3_CHANGE_TO_INCLUDE;
630 skb = add_grec(skb, pmc, type, 0, 0);
631 pmc->crcount--;
633 spin_unlock_bh(&pmc->lock);
635 rcu_read_unlock();
637 if (!skb)
638 return;
639 (void) igmpv3_sendpack(skb);
642 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
643 int type)
645 struct sk_buff *skb;
646 struct iphdr *iph;
647 struct igmphdr *ih;
648 struct rtable *rt;
649 struct net_device *dev = in_dev->dev;
650 struct net *net = dev_net(dev);
651 __be32 group = pmc ? pmc->multiaddr : 0;
652 __be32 dst;
654 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
655 return igmpv3_send_report(in_dev, pmc);
656 else if (type == IGMP_HOST_LEAVE_MESSAGE)
657 dst = IGMP_ALL_ROUTER;
658 else
659 dst = group;
661 rt = ip_route_output_ports(net, NULL, dst, 0,
662 0, 0,
663 IPPROTO_IGMP, 0, dev->ifindex);
664 if (IS_ERR(rt))
665 return -1;
667 if (rt->rt_src == 0) {
668 ip_rt_put(rt);
669 return -1;
672 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
673 if (skb == NULL) {
674 ip_rt_put(rt);
675 return -1;
678 skb_dst_set(skb, &rt->dst);
680 skb_reserve(skb, LL_RESERVED_SPACE(dev));
682 skb_reset_network_header(skb);
683 iph = ip_hdr(skb);
684 skb_put(skb, sizeof(struct iphdr) + 4);
686 iph->version = 4;
687 iph->ihl = (sizeof(struct iphdr)+4)>>2;
688 iph->tos = 0xc0;
689 iph->frag_off = htons(IP_DF);
690 iph->ttl = 1;
691 iph->daddr = dst;
692 iph->saddr = rt->rt_src;
693 iph->protocol = IPPROTO_IGMP;
694 ip_select_ident(iph, &rt->dst, NULL);
695 ((u8*)&iph[1])[0] = IPOPT_RA;
696 ((u8*)&iph[1])[1] = 4;
697 ((u8*)&iph[1])[2] = 0;
698 ((u8*)&iph[1])[3] = 0;
700 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
701 ih->type = type;
702 ih->code = 0;
703 ih->csum = 0;
704 ih->group = group;
705 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
707 return ip_local_out(skb);
710 static void igmp_gq_timer_expire(unsigned long data)
712 struct in_device *in_dev = (struct in_device *)data;
714 in_dev->mr_gq_running = 0;
715 igmpv3_send_report(in_dev, NULL);
716 __in_dev_put(in_dev);
719 static void igmp_ifc_timer_expire(unsigned long data)
721 struct in_device *in_dev = (struct in_device *)data;
723 igmpv3_send_cr(in_dev);
724 if (in_dev->mr_ifc_count) {
725 in_dev->mr_ifc_count--;
726 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
728 __in_dev_put(in_dev);
731 static void igmp_ifc_event(struct in_device *in_dev)
733 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
734 return;
735 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
736 IGMP_Unsolicited_Report_Count;
737 igmp_ifc_start_timer(in_dev, 1);
741 static void igmp_timer_expire(unsigned long data)
743 struct ip_mc_list *im=(struct ip_mc_list *)data;
744 struct in_device *in_dev = im->interface;
746 spin_lock(&im->lock);
747 im->tm_running = 0;
749 if (im->unsolicit_count) {
750 im->unsolicit_count--;
751 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
753 im->reporter = 1;
754 spin_unlock(&im->lock);
756 if (IGMP_V1_SEEN(in_dev))
757 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
758 else if (IGMP_V2_SEEN(in_dev))
759 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
760 else
761 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
763 ip_ma_put(im);
766 /* mark EXCLUDE-mode sources */
767 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
769 struct ip_sf_list *psf;
770 int i, scount;
772 scount = 0;
773 for (psf=pmc->sources; psf; psf=psf->sf_next) {
774 if (scount == nsrcs)
775 break;
776 for (i=0; i<nsrcs; i++) {
777 /* skip inactive filters */
778 if (pmc->sfcount[MCAST_INCLUDE] ||
779 pmc->sfcount[MCAST_EXCLUDE] !=
780 psf->sf_count[MCAST_EXCLUDE])
781 continue;
782 if (srcs[i] == psf->sf_inaddr) {
783 scount++;
784 break;
788 pmc->gsquery = 0;
789 if (scount == nsrcs) /* all sources excluded */
790 return 0;
791 return 1;
794 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
796 struct ip_sf_list *psf;
797 int i, scount;
799 if (pmc->sfmode == MCAST_EXCLUDE)
800 return igmp_xmarksources(pmc, nsrcs, srcs);
802 /* mark INCLUDE-mode sources */
803 scount = 0;
804 for (psf=pmc->sources; psf; psf=psf->sf_next) {
805 if (scount == nsrcs)
806 break;
807 for (i=0; i<nsrcs; i++)
808 if (srcs[i] == psf->sf_inaddr) {
809 psf->sf_gsresp = 1;
810 scount++;
811 break;
814 if (!scount) {
815 pmc->gsquery = 0;
816 return 0;
818 pmc->gsquery = 1;
819 return 1;
822 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
824 struct ip_mc_list *im;
826 /* Timers are only set for non-local groups */
828 if (group == IGMP_ALL_HOSTS)
829 return;
831 rcu_read_lock();
832 for_each_pmc_rcu(in_dev, im) {
833 if (im->multiaddr == group) {
834 igmp_stop_timer(im);
835 break;
838 rcu_read_unlock();
841 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
842 int len)
844 struct igmphdr *ih = igmp_hdr(skb);
845 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
846 struct ip_mc_list *im;
847 __be32 group = ih->group;
848 int max_delay;
849 int mark = 0;
852 if (len == 8) {
853 if (ih->code == 0) {
854 /* Alas, old v1 router presents here. */
856 max_delay = IGMP_Query_Response_Interval;
857 in_dev->mr_v1_seen = jiffies +
858 IGMP_V1_Router_Present_Timeout;
859 group = 0;
860 } else {
861 /* v2 router present */
862 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
863 in_dev->mr_v2_seen = jiffies +
864 IGMP_V2_Router_Present_Timeout;
866 /* cancel the interface change timer */
867 in_dev->mr_ifc_count = 0;
868 if (del_timer(&in_dev->mr_ifc_timer))
869 __in_dev_put(in_dev);
870 /* clear deleted report items */
871 igmpv3_clear_delrec(in_dev);
872 } else if (len < 12) {
873 return; /* ignore bogus packet; freed by caller */
874 } else if (IGMP_V1_SEEN(in_dev)) {
875 /* This is a v3 query with v1 queriers present */
876 max_delay = IGMP_Query_Response_Interval;
877 group = 0;
878 } else if (IGMP_V2_SEEN(in_dev)) {
879 /* this is a v3 query with v2 queriers present;
880 * Interpretation of the max_delay code is problematic here.
881 * A real v2 host would use ih_code directly, while v3 has a
882 * different encoding. We use the v3 encoding as more likely
883 * to be intended in a v3 query.
885 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
886 } else { /* v3 */
887 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
888 return;
890 ih3 = igmpv3_query_hdr(skb);
891 if (ih3->nsrcs) {
892 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
893 + ntohs(ih3->nsrcs)*sizeof(__be32)))
894 return;
895 ih3 = igmpv3_query_hdr(skb);
898 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
899 if (!max_delay)
900 max_delay = 1; /* can't mod w/ 0 */
901 in_dev->mr_maxdelay = max_delay;
902 if (ih3->qrv)
903 in_dev->mr_qrv = ih3->qrv;
904 if (!group) { /* general query */
905 if (ih3->nsrcs)
906 return; /* no sources allowed */
907 igmp_gq_start_timer(in_dev);
908 return;
910 /* mark sources to include, if group & source-specific */
911 mark = ih3->nsrcs != 0;
915 * - Start the timers in all of our membership records
916 * that the query applies to for the interface on
917 * which the query arrived excl. those that belong
918 * to a "local" group (224.0.0.X)
919 * - For timers already running check if they need to
920 * be reset.
921 * - Use the igmp->igmp_code field as the maximum
922 * delay possible
924 rcu_read_lock();
925 for_each_pmc_rcu(in_dev, im) {
926 int changed;
928 if (group && group != im->multiaddr)
929 continue;
930 if (im->multiaddr == IGMP_ALL_HOSTS)
931 continue;
932 spin_lock_bh(&im->lock);
933 if (im->tm_running)
934 im->gsquery = im->gsquery && mark;
935 else
936 im->gsquery = mark;
937 changed = !im->gsquery ||
938 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
939 spin_unlock_bh(&im->lock);
940 if (changed)
941 igmp_mod_timer(im, max_delay);
943 rcu_read_unlock();
946 /* called in rcu_read_lock() section */
947 int igmp_rcv(struct sk_buff *skb)
949 /* This basically follows the spec line by line -- see RFC1112 */
950 struct igmphdr *ih;
951 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
952 int len = skb->len;
954 if (in_dev == NULL)
955 goto drop;
957 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
958 goto drop;
960 switch (skb->ip_summed) {
961 case CHECKSUM_COMPLETE:
962 if (!csum_fold(skb->csum))
963 break;
964 /* fall through */
965 case CHECKSUM_NONE:
966 skb->csum = 0;
967 if (__skb_checksum_complete(skb))
968 goto drop;
971 ih = igmp_hdr(skb);
972 switch (ih->type) {
973 case IGMP_HOST_MEMBERSHIP_QUERY:
974 igmp_heard_query(in_dev, skb, len);
975 break;
976 case IGMP_HOST_MEMBERSHIP_REPORT:
977 case IGMPV2_HOST_MEMBERSHIP_REPORT:
978 /* Is it our report looped back? */
979 if (rt_is_output_route(skb_rtable(skb)))
980 break;
981 /* don't rely on MC router hearing unicast reports */
982 if (skb->pkt_type == PACKET_MULTICAST ||
983 skb->pkt_type == PACKET_BROADCAST)
984 igmp_heard_report(in_dev, ih->group);
985 break;
986 case IGMP_PIM:
987 #ifdef CONFIG_IP_PIMSM_V1
988 return pim_rcv_v1(skb);
989 #endif
990 case IGMPV3_HOST_MEMBERSHIP_REPORT:
991 case IGMP_DVMRP:
992 case IGMP_TRACE:
993 case IGMP_HOST_LEAVE_MESSAGE:
994 case IGMP_MTRACE:
995 case IGMP_MTRACE_RESP:
996 break;
997 default:
998 break;
1001 drop:
1002 kfree_skb(skb);
1003 return 0;
1006 #endif
1010 * Add a filter to a device
1013 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1015 char buf[MAX_ADDR_LEN];
1016 struct net_device *dev = in_dev->dev;
1018 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1019 We will get multicast token leakage, when IFF_MULTICAST
1020 is changed. This check should be done in dev->set_multicast_list
1021 routine. Something sort of:
1022 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1023 --ANK
1025 if (arp_mc_map(addr, buf, dev, 0) == 0)
1026 dev_mc_add(dev, buf);
1030 * Remove a filter from a device
1033 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1035 char buf[MAX_ADDR_LEN];
1036 struct net_device *dev = in_dev->dev;
1038 if (arp_mc_map(addr, buf, dev, 0) == 0)
1039 dev_mc_del(dev, buf);
1042 #ifdef CONFIG_IP_MULTICAST
1044 * deleted ip_mc_list manipulation
1046 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1048 struct ip_mc_list *pmc;
1050 /* this is an "ip_mc_list" for convenience; only the fields below
1051 * are actually used. In particular, the refcnt and users are not
1052 * used for management of the delete list. Using the same structure
1053 * for deleted items allows change reports to use common code with
1054 * non-deleted or query-response MCA's.
1056 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1057 if (!pmc)
1058 return;
1059 spin_lock_bh(&im->lock);
1060 pmc->interface = im->interface;
1061 in_dev_hold(in_dev);
1062 pmc->multiaddr = im->multiaddr;
1063 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1064 IGMP_Unsolicited_Report_Count;
1065 pmc->sfmode = im->sfmode;
1066 if (pmc->sfmode == MCAST_INCLUDE) {
1067 struct ip_sf_list *psf;
1069 pmc->tomb = im->tomb;
1070 pmc->sources = im->sources;
1071 im->tomb = im->sources = NULL;
1072 for (psf=pmc->sources; psf; psf=psf->sf_next)
1073 psf->sf_crcount = pmc->crcount;
1075 spin_unlock_bh(&im->lock);
1077 spin_lock_bh(&in_dev->mc_tomb_lock);
1078 pmc->next = in_dev->mc_tomb;
1079 in_dev->mc_tomb = pmc;
1080 spin_unlock_bh(&in_dev->mc_tomb_lock);
1083 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1085 struct ip_mc_list *pmc, *pmc_prev;
1086 struct ip_sf_list *psf, *psf_next;
1088 spin_lock_bh(&in_dev->mc_tomb_lock);
1089 pmc_prev = NULL;
1090 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1091 if (pmc->multiaddr == multiaddr)
1092 break;
1093 pmc_prev = pmc;
1095 if (pmc) {
1096 if (pmc_prev)
1097 pmc_prev->next = pmc->next;
1098 else
1099 in_dev->mc_tomb = pmc->next;
1101 spin_unlock_bh(&in_dev->mc_tomb_lock);
1102 if (pmc) {
1103 for (psf=pmc->tomb; psf; psf=psf_next) {
1104 psf_next = psf->sf_next;
1105 kfree(psf);
1107 in_dev_put(pmc->interface);
1108 kfree(pmc);
1112 static void igmpv3_clear_delrec(struct in_device *in_dev)
1114 struct ip_mc_list *pmc, *nextpmc;
1116 spin_lock_bh(&in_dev->mc_tomb_lock);
1117 pmc = in_dev->mc_tomb;
1118 in_dev->mc_tomb = NULL;
1119 spin_unlock_bh(&in_dev->mc_tomb_lock);
1121 for (; pmc; pmc = nextpmc) {
1122 nextpmc = pmc->next;
1123 ip_mc_clear_src(pmc);
1124 in_dev_put(pmc->interface);
1125 kfree(pmc);
1127 /* clear dead sources, too */
1128 rcu_read_lock();
1129 for_each_pmc_rcu(in_dev, pmc) {
1130 struct ip_sf_list *psf, *psf_next;
1132 spin_lock_bh(&pmc->lock);
1133 psf = pmc->tomb;
1134 pmc->tomb = NULL;
1135 spin_unlock_bh(&pmc->lock);
1136 for (; psf; psf=psf_next) {
1137 psf_next = psf->sf_next;
1138 kfree(psf);
1141 rcu_read_unlock();
1143 #endif
1145 static void igmp_group_dropped(struct ip_mc_list *im)
1147 struct in_device *in_dev = im->interface;
1148 #ifdef CONFIG_IP_MULTICAST
1149 int reporter;
1150 #endif
1152 if (im->loaded) {
1153 im->loaded = 0;
1154 ip_mc_filter_del(in_dev, im->multiaddr);
1157 #ifdef CONFIG_IP_MULTICAST
1158 if (im->multiaddr == IGMP_ALL_HOSTS)
1159 return;
1161 reporter = im->reporter;
1162 igmp_stop_timer(im);
1164 if (!in_dev->dead) {
1165 if (IGMP_V1_SEEN(in_dev))
1166 goto done;
1167 if (IGMP_V2_SEEN(in_dev)) {
1168 if (reporter)
1169 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1170 goto done;
1172 /* IGMPv3 */
1173 igmpv3_add_delrec(in_dev, im);
1175 igmp_ifc_event(in_dev);
1177 done:
1178 #endif
1179 ip_mc_clear_src(im);
1182 static void igmp_group_added(struct ip_mc_list *im)
1184 struct in_device *in_dev = im->interface;
1186 if (im->loaded == 0) {
1187 im->loaded = 1;
1188 ip_mc_filter_add(in_dev, im->multiaddr);
1191 #ifdef CONFIG_IP_MULTICAST
1192 if (im->multiaddr == IGMP_ALL_HOSTS)
1193 return;
1195 if (in_dev->dead)
1196 return;
1197 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1198 spin_lock_bh(&im->lock);
1199 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1200 spin_unlock_bh(&im->lock);
1201 return;
1203 /* else, v3 */
1205 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1206 IGMP_Unsolicited_Report_Count;
1207 igmp_ifc_event(in_dev);
1208 #endif
1213 * Multicast list managers
1218 * A socket has joined a multicast group on device dev.
1221 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1223 struct ip_mc_list *im;
1225 ASSERT_RTNL();
1227 for_each_pmc_rtnl(in_dev, im) {
1228 if (im->multiaddr == addr) {
1229 im->users++;
1230 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1231 goto out;
1235 im = kzalloc(sizeof(*im), GFP_KERNEL);
1236 if (!im)
1237 goto out;
1239 im->users = 1;
1240 im->interface = in_dev;
1241 in_dev_hold(in_dev);
1242 im->multiaddr = addr;
1243 /* initial mode is (EX, empty) */
1244 im->sfmode = MCAST_EXCLUDE;
1245 im->sfcount[MCAST_EXCLUDE] = 1;
1246 atomic_set(&im->refcnt, 1);
1247 spin_lock_init(&im->lock);
1248 #ifdef CONFIG_IP_MULTICAST
1249 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1250 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1251 #endif
1253 im->next_rcu = in_dev->mc_list;
1254 in_dev->mc_count++;
1255 rcu_assign_pointer(in_dev->mc_list, im);
1257 #ifdef CONFIG_IP_MULTICAST
1258 igmpv3_del_delrec(in_dev, im->multiaddr);
1259 #endif
1260 igmp_group_added(im);
1261 if (!in_dev->dead)
1262 ip_rt_multicast_event(in_dev);
1263 out:
1264 return;
1266 EXPORT_SYMBOL(ip_mc_inc_group);
1269 * Resend IGMP JOIN report; used for bonding.
1270 * Called with rcu_read_lock()
1272 void ip_mc_rejoin_groups(struct in_device *in_dev)
1274 #ifdef CONFIG_IP_MULTICAST
1275 struct ip_mc_list *im;
1276 int type;
1278 for_each_pmc_rcu(in_dev, im) {
1279 if (im->multiaddr == IGMP_ALL_HOSTS)
1280 continue;
1282 /* a failover is happening and switches
1283 * must be notified immediately
1285 if (IGMP_V1_SEEN(in_dev))
1286 type = IGMP_HOST_MEMBERSHIP_REPORT;
1287 else if (IGMP_V2_SEEN(in_dev))
1288 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1289 else
1290 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1291 igmp_send_report(in_dev, im, type);
1293 #endif
1295 EXPORT_SYMBOL(ip_mc_rejoin_groups);
1298 * A socket has left a multicast group on device dev
1301 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1303 struct ip_mc_list *i;
1304 struct ip_mc_list __rcu **ip;
1306 ASSERT_RTNL();
1308 for (ip = &in_dev->mc_list;
1309 (i = rtnl_dereference(*ip)) != NULL;
1310 ip = &i->next_rcu) {
1311 if (i->multiaddr == addr) {
1312 if (--i->users == 0) {
1313 *ip = i->next_rcu;
1314 in_dev->mc_count--;
1315 igmp_group_dropped(i);
1317 if (!in_dev->dead)
1318 ip_rt_multicast_event(in_dev);
1320 ip_ma_put(i);
1321 return;
1323 break;
1327 EXPORT_SYMBOL(ip_mc_dec_group);
1329 /* Device changing type */
1331 void ip_mc_unmap(struct in_device *in_dev)
1333 struct ip_mc_list *pmc;
1335 ASSERT_RTNL();
1337 for_each_pmc_rtnl(in_dev, pmc)
1338 igmp_group_dropped(pmc);
1341 void ip_mc_remap(struct in_device *in_dev)
1343 struct ip_mc_list *pmc;
1345 ASSERT_RTNL();
1347 for_each_pmc_rtnl(in_dev, pmc)
1348 igmp_group_added(pmc);
1351 /* Device going down */
1353 void ip_mc_down(struct in_device *in_dev)
1355 struct ip_mc_list *pmc;
1357 ASSERT_RTNL();
1359 for_each_pmc_rtnl(in_dev, pmc)
1360 igmp_group_dropped(pmc);
1362 #ifdef CONFIG_IP_MULTICAST
1363 in_dev->mr_ifc_count = 0;
1364 if (del_timer(&in_dev->mr_ifc_timer))
1365 __in_dev_put(in_dev);
1366 in_dev->mr_gq_running = 0;
1367 if (del_timer(&in_dev->mr_gq_timer))
1368 __in_dev_put(in_dev);
1369 igmpv3_clear_delrec(in_dev);
1370 #endif
1372 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1375 void ip_mc_init_dev(struct in_device *in_dev)
1377 ASSERT_RTNL();
1379 in_dev->mc_tomb = NULL;
1380 #ifdef CONFIG_IP_MULTICAST
1381 in_dev->mr_gq_running = 0;
1382 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1383 (unsigned long)in_dev);
1384 in_dev->mr_ifc_count = 0;
1385 in_dev->mc_count = 0;
1386 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1387 (unsigned long)in_dev);
1388 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1389 #endif
1391 spin_lock_init(&in_dev->mc_tomb_lock);
1394 /* Device going up */
1396 void ip_mc_up(struct in_device *in_dev)
1398 struct ip_mc_list *pmc;
1400 ASSERT_RTNL();
1402 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1404 for_each_pmc_rtnl(in_dev, pmc)
1405 igmp_group_added(pmc);
1409 * Device is about to be destroyed: clean up.
1412 void ip_mc_destroy_dev(struct in_device *in_dev)
1414 struct ip_mc_list *i;
1416 ASSERT_RTNL();
1418 /* Deactivate timers */
1419 ip_mc_down(in_dev);
1421 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1422 in_dev->mc_list = i->next_rcu;
1423 in_dev->mc_count--;
1425 igmp_group_dropped(i);
1426 ip_ma_put(i);
1430 /* RTNL is locked */
1431 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1433 struct net_device *dev = NULL;
1434 struct in_device *idev = NULL;
1436 if (imr->imr_ifindex) {
1437 idev = inetdev_by_index(net, imr->imr_ifindex);
1438 return idev;
1440 if (imr->imr_address.s_addr) {
1441 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1442 if (!dev)
1443 return NULL;
1446 if (!dev) {
1447 struct rtable *rt = ip_route_output(net,
1448 imr->imr_multiaddr.s_addr,
1449 0, 0, 0);
1450 if (!IS_ERR(rt)) {
1451 dev = rt->dst.dev;
1452 ip_rt_put(rt);
1455 if (dev) {
1456 imr->imr_ifindex = dev->ifindex;
1457 idev = __in_dev_get_rtnl(dev);
1459 return idev;
1463 * Join a socket to a group
1465 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1466 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1469 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1470 __be32 *psfsrc)
1472 struct ip_sf_list *psf, *psf_prev;
1473 int rv = 0;
1475 psf_prev = NULL;
1476 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1477 if (psf->sf_inaddr == *psfsrc)
1478 break;
1479 psf_prev = psf;
1481 if (!psf || psf->sf_count[sfmode] == 0) {
1482 /* source filter not found, or count wrong => bug */
1483 return -ESRCH;
1485 psf->sf_count[sfmode]--;
1486 if (psf->sf_count[sfmode] == 0) {
1487 ip_rt_multicast_event(pmc->interface);
1489 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1490 #ifdef CONFIG_IP_MULTICAST
1491 struct in_device *in_dev = pmc->interface;
1492 #endif
1494 /* no more filters for this source */
1495 if (psf_prev)
1496 psf_prev->sf_next = psf->sf_next;
1497 else
1498 pmc->sources = psf->sf_next;
1499 #ifdef CONFIG_IP_MULTICAST
1500 if (psf->sf_oldin &&
1501 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1502 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1503 IGMP_Unsolicited_Report_Count;
1504 psf->sf_next = pmc->tomb;
1505 pmc->tomb = psf;
1506 rv = 1;
1507 } else
1508 #endif
1509 kfree(psf);
1511 return rv;
1514 #ifndef CONFIG_IP_MULTICAST
1515 #define igmp_ifc_event(x) do { } while (0)
1516 #endif
1518 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1519 int sfcount, __be32 *psfsrc, int delta)
1521 struct ip_mc_list *pmc;
1522 int changerec = 0;
1523 int i, err;
1525 if (!in_dev)
1526 return -ENODEV;
1527 rcu_read_lock();
1528 for_each_pmc_rcu(in_dev, pmc) {
1529 if (*pmca == pmc->multiaddr)
1530 break;
1532 if (!pmc) {
1533 /* MCA not found?? bug */
1534 rcu_read_unlock();
1535 return -ESRCH;
1537 spin_lock_bh(&pmc->lock);
1538 rcu_read_unlock();
1539 #ifdef CONFIG_IP_MULTICAST
1540 sf_markstate(pmc);
1541 #endif
1542 if (!delta) {
1543 err = -EINVAL;
1544 if (!pmc->sfcount[sfmode])
1545 goto out_unlock;
1546 pmc->sfcount[sfmode]--;
1548 err = 0;
1549 for (i=0; i<sfcount; i++) {
1550 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1552 changerec |= rv > 0;
1553 if (!err && rv < 0)
1554 err = rv;
1556 if (pmc->sfmode == MCAST_EXCLUDE &&
1557 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1558 pmc->sfcount[MCAST_INCLUDE]) {
1559 #ifdef CONFIG_IP_MULTICAST
1560 struct ip_sf_list *psf;
1561 #endif
1563 /* filter mode change */
1564 pmc->sfmode = MCAST_INCLUDE;
1565 #ifdef CONFIG_IP_MULTICAST
1566 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1567 IGMP_Unsolicited_Report_Count;
1568 in_dev->mr_ifc_count = pmc->crcount;
1569 for (psf=pmc->sources; psf; psf = psf->sf_next)
1570 psf->sf_crcount = 0;
1571 igmp_ifc_event(pmc->interface);
1572 } else if (sf_setstate(pmc) || changerec) {
1573 igmp_ifc_event(pmc->interface);
1574 #endif
1576 out_unlock:
1577 spin_unlock_bh(&pmc->lock);
1578 return err;
1582 * Add multicast single-source filter to the interface list
1584 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1585 __be32 *psfsrc, int delta)
1587 struct ip_sf_list *psf, *psf_prev;
1589 psf_prev = NULL;
1590 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1591 if (psf->sf_inaddr == *psfsrc)
1592 break;
1593 psf_prev = psf;
1595 if (!psf) {
1596 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1597 if (!psf)
1598 return -ENOBUFS;
1599 psf->sf_inaddr = *psfsrc;
1600 if (psf_prev) {
1601 psf_prev->sf_next = psf;
1602 } else
1603 pmc->sources = psf;
1605 psf->sf_count[sfmode]++;
1606 if (psf->sf_count[sfmode] == 1) {
1607 ip_rt_multicast_event(pmc->interface);
1609 return 0;
1612 #ifdef CONFIG_IP_MULTICAST
1613 static void sf_markstate(struct ip_mc_list *pmc)
1615 struct ip_sf_list *psf;
1616 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1618 for (psf=pmc->sources; psf; psf=psf->sf_next)
1619 if (pmc->sfcount[MCAST_EXCLUDE]) {
1620 psf->sf_oldin = mca_xcount ==
1621 psf->sf_count[MCAST_EXCLUDE] &&
1622 !psf->sf_count[MCAST_INCLUDE];
1623 } else
1624 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1627 static int sf_setstate(struct ip_mc_list *pmc)
1629 struct ip_sf_list *psf, *dpsf;
1630 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1631 int qrv = pmc->interface->mr_qrv;
1632 int new_in, rv;
1634 rv = 0;
1635 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1636 if (pmc->sfcount[MCAST_EXCLUDE]) {
1637 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1638 !psf->sf_count[MCAST_INCLUDE];
1639 } else
1640 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1641 if (new_in) {
1642 if (!psf->sf_oldin) {
1643 struct ip_sf_list *prev = NULL;
1645 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1646 if (dpsf->sf_inaddr == psf->sf_inaddr)
1647 break;
1648 prev = dpsf;
1650 if (dpsf) {
1651 if (prev)
1652 prev->sf_next = dpsf->sf_next;
1653 else
1654 pmc->tomb = dpsf->sf_next;
1655 kfree(dpsf);
1657 psf->sf_crcount = qrv;
1658 rv++;
1660 } else if (psf->sf_oldin) {
1662 psf->sf_crcount = 0;
1664 * add or update "delete" records if an active filter
1665 * is now inactive
1667 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1668 if (dpsf->sf_inaddr == psf->sf_inaddr)
1669 break;
1670 if (!dpsf) {
1671 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1672 if (!dpsf)
1673 continue;
1674 *dpsf = *psf;
1675 /* pmc->lock held by callers */
1676 dpsf->sf_next = pmc->tomb;
1677 pmc->tomb = dpsf;
1679 dpsf->sf_crcount = qrv;
1680 rv++;
1683 return rv;
1685 #endif
1688 * Add multicast source filter list to the interface list
1690 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1691 int sfcount, __be32 *psfsrc, int delta)
1693 struct ip_mc_list *pmc;
1694 int isexclude;
1695 int i, err;
1697 if (!in_dev)
1698 return -ENODEV;
1699 rcu_read_lock();
1700 for_each_pmc_rcu(in_dev, pmc) {
1701 if (*pmca == pmc->multiaddr)
1702 break;
1704 if (!pmc) {
1705 /* MCA not found?? bug */
1706 rcu_read_unlock();
1707 return -ESRCH;
1709 spin_lock_bh(&pmc->lock);
1710 rcu_read_unlock();
1712 #ifdef CONFIG_IP_MULTICAST
1713 sf_markstate(pmc);
1714 #endif
1715 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1716 if (!delta)
1717 pmc->sfcount[sfmode]++;
1718 err = 0;
1719 for (i=0; i<sfcount; i++) {
1720 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1721 if (err)
1722 break;
1724 if (err) {
1725 int j;
1727 pmc->sfcount[sfmode]--;
1728 for (j=0; j<i; j++)
1729 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1730 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1731 #ifdef CONFIG_IP_MULTICAST
1732 struct ip_sf_list *psf;
1733 in_dev = pmc->interface;
1734 #endif
1736 /* filter mode change */
1737 if (pmc->sfcount[MCAST_EXCLUDE])
1738 pmc->sfmode = MCAST_EXCLUDE;
1739 else if (pmc->sfcount[MCAST_INCLUDE])
1740 pmc->sfmode = MCAST_INCLUDE;
1741 #ifdef CONFIG_IP_MULTICAST
1742 /* else no filters; keep old mode for reports */
1744 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1745 IGMP_Unsolicited_Report_Count;
1746 in_dev->mr_ifc_count = pmc->crcount;
1747 for (psf=pmc->sources; psf; psf = psf->sf_next)
1748 psf->sf_crcount = 0;
1749 igmp_ifc_event(in_dev);
1750 } else if (sf_setstate(pmc)) {
1751 igmp_ifc_event(in_dev);
1752 #endif
1754 spin_unlock_bh(&pmc->lock);
1755 return err;
1758 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1760 struct ip_sf_list *psf, *nextpsf;
1762 for (psf=pmc->tomb; psf; psf=nextpsf) {
1763 nextpsf = psf->sf_next;
1764 kfree(psf);
1766 pmc->tomb = NULL;
1767 for (psf=pmc->sources; psf; psf=nextpsf) {
1768 nextpsf = psf->sf_next;
1769 kfree(psf);
1771 pmc->sources = NULL;
1772 pmc->sfmode = MCAST_EXCLUDE;
1773 pmc->sfcount[MCAST_INCLUDE] = 0;
1774 pmc->sfcount[MCAST_EXCLUDE] = 1;
1779 * Join a multicast group
1781 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1783 int err;
1784 __be32 addr = imr->imr_multiaddr.s_addr;
1785 struct ip_mc_socklist *iml = NULL, *i;
1786 struct in_device *in_dev;
1787 struct inet_sock *inet = inet_sk(sk);
1788 struct net *net = sock_net(sk);
1789 int ifindex;
1790 int count = 0;
1792 if (!ipv4_is_multicast(addr))
1793 return -EINVAL;
1795 rtnl_lock();
1797 in_dev = ip_mc_find_dev(net, imr);
1799 if (!in_dev) {
1800 iml = NULL;
1801 err = -ENODEV;
1802 goto done;
1805 err = -EADDRINUSE;
1806 ifindex = imr->imr_ifindex;
1807 for_each_pmc_rtnl(inet, i) {
1808 if (i->multi.imr_multiaddr.s_addr == addr &&
1809 i->multi.imr_ifindex == ifindex)
1810 goto done;
1811 count++;
1813 err = -ENOBUFS;
1814 if (count >= sysctl_igmp_max_memberships)
1815 goto done;
1816 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1817 if (iml == NULL)
1818 goto done;
1820 memcpy(&iml->multi, imr, sizeof(*imr));
1821 iml->next_rcu = inet->mc_list;
1822 iml->sflist = NULL;
1823 iml->sfmode = MCAST_EXCLUDE;
1824 rcu_assign_pointer(inet->mc_list, iml);
1825 ip_mc_inc_group(in_dev, addr);
1826 err = 0;
1827 done:
1828 rtnl_unlock();
1829 return err;
1831 EXPORT_SYMBOL(ip_mc_join_group);
1833 static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1835 kfree(container_of(rp, struct ip_sf_socklist, rcu));
1836 /* sk_omem_alloc should have been decreased by the caller*/
1839 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1840 struct in_device *in_dev)
1842 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1843 int err;
1845 if (psf == NULL) {
1846 /* any-source empty exclude case */
1847 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1848 iml->sfmode, 0, NULL, 0);
1850 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1851 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1852 rcu_assign_pointer(iml->sflist, NULL);
1853 /* decrease mem now to avoid the memleak warning */
1854 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1855 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
1856 return err;
1860 static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1862 kfree(container_of(rp, struct ip_mc_socklist, rcu));
1863 /* sk_omem_alloc should have been decreased by the caller*/
1868 * Ask a socket to leave a group.
1871 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1873 struct inet_sock *inet = inet_sk(sk);
1874 struct ip_mc_socklist *iml;
1875 struct ip_mc_socklist __rcu **imlp;
1876 struct in_device *in_dev;
1877 struct net *net = sock_net(sk);
1878 __be32 group = imr->imr_multiaddr.s_addr;
1879 u32 ifindex;
1880 int ret = -EADDRNOTAVAIL;
1882 rtnl_lock();
1883 in_dev = ip_mc_find_dev(net, imr);
1884 ifindex = imr->imr_ifindex;
1885 for (imlp = &inet->mc_list;
1886 (iml = rtnl_dereference(*imlp)) != NULL;
1887 imlp = &iml->next_rcu) {
1888 if (iml->multi.imr_multiaddr.s_addr != group)
1889 continue;
1890 if (ifindex) {
1891 if (iml->multi.imr_ifindex != ifindex)
1892 continue;
1893 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1894 iml->multi.imr_address.s_addr)
1895 continue;
1897 (void) ip_mc_leave_src(sk, iml, in_dev);
1899 *imlp = iml->next_rcu;
1901 if (in_dev)
1902 ip_mc_dec_group(in_dev, group);
1903 rtnl_unlock();
1904 /* decrease mem now to avoid the memleak warning */
1905 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1906 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
1907 return 0;
1909 if (!in_dev)
1910 ret = -ENODEV;
1911 rtnl_unlock();
1912 return ret;
1915 int ip_mc_source(int add, int omode, struct sock *sk, struct
1916 ip_mreq_source *mreqs, int ifindex)
1918 int err;
1919 struct ip_mreqn imr;
1920 __be32 addr = mreqs->imr_multiaddr;
1921 struct ip_mc_socklist *pmc;
1922 struct in_device *in_dev = NULL;
1923 struct inet_sock *inet = inet_sk(sk);
1924 struct ip_sf_socklist *psl;
1925 struct net *net = sock_net(sk);
1926 int leavegroup = 0;
1927 int i, j, rv;
1929 if (!ipv4_is_multicast(addr))
1930 return -EINVAL;
1932 rtnl_lock();
1934 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1935 imr.imr_address.s_addr = mreqs->imr_interface;
1936 imr.imr_ifindex = ifindex;
1937 in_dev = ip_mc_find_dev(net, &imr);
1939 if (!in_dev) {
1940 err = -ENODEV;
1941 goto done;
1943 err = -EADDRNOTAVAIL;
1945 for_each_pmc_rtnl(inet, pmc) {
1946 if ((pmc->multi.imr_multiaddr.s_addr ==
1947 imr.imr_multiaddr.s_addr) &&
1948 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1949 break;
1951 if (!pmc) { /* must have a prior join */
1952 err = -EINVAL;
1953 goto done;
1955 /* if a source filter was set, must be the same mode as before */
1956 if (pmc->sflist) {
1957 if (pmc->sfmode != omode) {
1958 err = -EINVAL;
1959 goto done;
1961 } else if (pmc->sfmode != omode) {
1962 /* allow mode switches for empty-set filters */
1963 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1964 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1965 NULL, 0);
1966 pmc->sfmode = omode;
1969 psl = rtnl_dereference(pmc->sflist);
1970 if (!add) {
1971 if (!psl)
1972 goto done; /* err = -EADDRNOTAVAIL */
1973 rv = !0;
1974 for (i=0; i<psl->sl_count; i++) {
1975 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1976 sizeof(__be32));
1977 if (rv == 0)
1978 break;
1980 if (rv) /* source not found */
1981 goto done; /* err = -EADDRNOTAVAIL */
1983 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1984 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1985 leavegroup = 1;
1986 goto done;
1989 /* update the interface filter */
1990 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1991 &mreqs->imr_sourceaddr, 1);
1993 for (j=i+1; j<psl->sl_count; j++)
1994 psl->sl_addr[j-1] = psl->sl_addr[j];
1995 psl->sl_count--;
1996 err = 0;
1997 goto done;
1999 /* else, add a new source to the filter */
2001 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2002 err = -ENOBUFS;
2003 goto done;
2005 if (!psl || psl->sl_count == psl->sl_max) {
2006 struct ip_sf_socklist *newpsl;
2007 int count = IP_SFBLOCK;
2009 if (psl)
2010 count += psl->sl_max;
2011 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2012 if (!newpsl) {
2013 err = -ENOBUFS;
2014 goto done;
2016 newpsl->sl_max = count;
2017 newpsl->sl_count = count - IP_SFBLOCK;
2018 if (psl) {
2019 for (i=0; i<psl->sl_count; i++)
2020 newpsl->sl_addr[i] = psl->sl_addr[i];
2021 /* decrease mem now to avoid the memleak warning */
2022 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2023 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2025 rcu_assign_pointer(pmc->sflist, newpsl);
2026 psl = newpsl;
2028 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2029 for (i=0; i<psl->sl_count; i++) {
2030 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2031 sizeof(__be32));
2032 if (rv == 0)
2033 break;
2035 if (rv == 0) /* address already there is an error */
2036 goto done;
2037 for (j=psl->sl_count-1; j>=i; j--)
2038 psl->sl_addr[j+1] = psl->sl_addr[j];
2039 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2040 psl->sl_count++;
2041 err = 0;
2042 /* update the interface list */
2043 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2044 &mreqs->imr_sourceaddr, 1);
2045 done:
2046 rtnl_unlock();
2047 if (leavegroup)
2048 return ip_mc_leave_group(sk, &imr);
2049 return err;
2052 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2054 int err = 0;
2055 struct ip_mreqn imr;
2056 __be32 addr = msf->imsf_multiaddr;
2057 struct ip_mc_socklist *pmc;
2058 struct in_device *in_dev;
2059 struct inet_sock *inet = inet_sk(sk);
2060 struct ip_sf_socklist *newpsl, *psl;
2061 struct net *net = sock_net(sk);
2062 int leavegroup = 0;
2064 if (!ipv4_is_multicast(addr))
2065 return -EINVAL;
2066 if (msf->imsf_fmode != MCAST_INCLUDE &&
2067 msf->imsf_fmode != MCAST_EXCLUDE)
2068 return -EINVAL;
2070 rtnl_lock();
2072 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2073 imr.imr_address.s_addr = msf->imsf_interface;
2074 imr.imr_ifindex = ifindex;
2075 in_dev = ip_mc_find_dev(net, &imr);
2077 if (!in_dev) {
2078 err = -ENODEV;
2079 goto done;
2082 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2083 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2084 leavegroup = 1;
2085 goto done;
2088 for_each_pmc_rtnl(inet, pmc) {
2089 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2090 pmc->multi.imr_ifindex == imr.imr_ifindex)
2091 break;
2093 if (!pmc) { /* must have a prior join */
2094 err = -EINVAL;
2095 goto done;
2097 if (msf->imsf_numsrc) {
2098 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2099 GFP_KERNEL);
2100 if (!newpsl) {
2101 err = -ENOBUFS;
2102 goto done;
2104 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2105 memcpy(newpsl->sl_addr, msf->imsf_slist,
2106 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2107 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2108 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2109 if (err) {
2110 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2111 goto done;
2113 } else {
2114 newpsl = NULL;
2115 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2116 msf->imsf_fmode, 0, NULL, 0);
2118 psl = rtnl_dereference(pmc->sflist);
2119 if (psl) {
2120 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2121 psl->sl_count, psl->sl_addr, 0);
2122 /* decrease mem now to avoid the memleak warning */
2123 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2124 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2125 } else
2126 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2127 0, NULL, 0);
2128 rcu_assign_pointer(pmc->sflist, newpsl);
2129 pmc->sfmode = msf->imsf_fmode;
2130 err = 0;
2131 done:
2132 rtnl_unlock();
2133 if (leavegroup)
2134 err = ip_mc_leave_group(sk, &imr);
2135 return err;
2138 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2139 struct ip_msfilter __user *optval, int __user *optlen)
2141 int err, len, count, copycount;
2142 struct ip_mreqn imr;
2143 __be32 addr = msf->imsf_multiaddr;
2144 struct ip_mc_socklist *pmc;
2145 struct in_device *in_dev;
2146 struct inet_sock *inet = inet_sk(sk);
2147 struct ip_sf_socklist *psl;
2148 struct net *net = sock_net(sk);
2150 if (!ipv4_is_multicast(addr))
2151 return -EINVAL;
2153 rtnl_lock();
2155 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2156 imr.imr_address.s_addr = msf->imsf_interface;
2157 imr.imr_ifindex = 0;
2158 in_dev = ip_mc_find_dev(net, &imr);
2160 if (!in_dev) {
2161 err = -ENODEV;
2162 goto done;
2164 err = -EADDRNOTAVAIL;
2166 for_each_pmc_rtnl(inet, pmc) {
2167 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2168 pmc->multi.imr_ifindex == imr.imr_ifindex)
2169 break;
2171 if (!pmc) /* must have a prior join */
2172 goto done;
2173 msf->imsf_fmode = pmc->sfmode;
2174 psl = rtnl_dereference(pmc->sflist);
2175 rtnl_unlock();
2176 if (!psl) {
2177 len = 0;
2178 count = 0;
2179 } else {
2180 count = psl->sl_count;
2182 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2183 len = copycount * sizeof(psl->sl_addr[0]);
2184 msf->imsf_numsrc = count;
2185 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2186 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2187 return -EFAULT;
2189 if (len &&
2190 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2191 return -EFAULT;
2192 return 0;
2193 done:
2194 rtnl_unlock();
2195 return err;
2198 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2199 struct group_filter __user *optval, int __user *optlen)
2201 int err, i, count, copycount;
2202 struct sockaddr_in *psin;
2203 __be32 addr;
2204 struct ip_mc_socklist *pmc;
2205 struct inet_sock *inet = inet_sk(sk);
2206 struct ip_sf_socklist *psl;
2208 psin = (struct sockaddr_in *)&gsf->gf_group;
2209 if (psin->sin_family != AF_INET)
2210 return -EINVAL;
2211 addr = psin->sin_addr.s_addr;
2212 if (!ipv4_is_multicast(addr))
2213 return -EINVAL;
2215 rtnl_lock();
2217 err = -EADDRNOTAVAIL;
2219 for_each_pmc_rtnl(inet, pmc) {
2220 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2221 pmc->multi.imr_ifindex == gsf->gf_interface)
2222 break;
2224 if (!pmc) /* must have a prior join */
2225 goto done;
2226 gsf->gf_fmode = pmc->sfmode;
2227 psl = rtnl_dereference(pmc->sflist);
2228 rtnl_unlock();
2229 count = psl ? psl->sl_count : 0;
2230 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2231 gsf->gf_numsrc = count;
2232 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2233 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2234 return -EFAULT;
2236 for (i=0; i<copycount; i++) {
2237 struct sockaddr_storage ss;
2239 psin = (struct sockaddr_in *)&ss;
2240 memset(&ss, 0, sizeof(ss));
2241 psin->sin_family = AF_INET;
2242 psin->sin_addr.s_addr = psl->sl_addr[i];
2243 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2244 return -EFAULT;
2246 return 0;
2247 done:
2248 rtnl_unlock();
2249 return err;
2253 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2255 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2257 struct inet_sock *inet = inet_sk(sk);
2258 struct ip_mc_socklist *pmc;
2259 struct ip_sf_socklist *psl;
2260 int i;
2261 int ret;
2263 ret = 1;
2264 if (!ipv4_is_multicast(loc_addr))
2265 goto out;
2267 rcu_read_lock();
2268 for_each_pmc_rcu(inet, pmc) {
2269 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2270 pmc->multi.imr_ifindex == dif)
2271 break;
2273 ret = inet->mc_all;
2274 if (!pmc)
2275 goto unlock;
2276 psl = rcu_dereference(pmc->sflist);
2277 ret = (pmc->sfmode == MCAST_EXCLUDE);
2278 if (!psl)
2279 goto unlock;
2281 for (i=0; i<psl->sl_count; i++) {
2282 if (psl->sl_addr[i] == rmt_addr)
2283 break;
2285 ret = 0;
2286 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2287 goto unlock;
2288 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2289 goto unlock;
2290 ret = 1;
2291 unlock:
2292 rcu_read_unlock();
2293 out:
2294 return ret;
2298 * A socket is closing.
2301 void ip_mc_drop_socket(struct sock *sk)
2303 struct inet_sock *inet = inet_sk(sk);
2304 struct ip_mc_socklist *iml;
2305 struct net *net = sock_net(sk);
2307 if (inet->mc_list == NULL)
2308 return;
2310 rtnl_lock();
2311 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2312 struct in_device *in_dev;
2314 inet->mc_list = iml->next_rcu;
2315 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2316 (void) ip_mc_leave_src(sk, iml, in_dev);
2317 if (in_dev != NULL)
2318 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2319 /* decrease mem now to avoid the memleak warning */
2320 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2321 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
2323 rtnl_unlock();
2326 /* called with rcu_read_lock() */
2327 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2329 struct ip_mc_list *im;
2330 struct ip_sf_list *psf;
2331 int rv = 0;
2333 for_each_pmc_rcu(in_dev, im) {
2334 if (im->multiaddr == mc_addr)
2335 break;
2337 if (im && proto == IPPROTO_IGMP) {
2338 rv = 1;
2339 } else if (im) {
2340 if (src_addr) {
2341 for (psf=im->sources; psf; psf=psf->sf_next) {
2342 if (psf->sf_inaddr == src_addr)
2343 break;
2345 if (psf)
2346 rv = psf->sf_count[MCAST_INCLUDE] ||
2347 psf->sf_count[MCAST_EXCLUDE] !=
2348 im->sfcount[MCAST_EXCLUDE];
2349 else
2350 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2351 } else
2352 rv = 1; /* unspecified source; tentatively allow */
2354 return rv;
2357 #if defined(CONFIG_PROC_FS)
2358 struct igmp_mc_iter_state {
2359 struct seq_net_private p;
2360 struct net_device *dev;
2361 struct in_device *in_dev;
2364 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2366 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2368 struct net *net = seq_file_net(seq);
2369 struct ip_mc_list *im = NULL;
2370 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2372 state->in_dev = NULL;
2373 for_each_netdev_rcu(net, state->dev) {
2374 struct in_device *in_dev;
2376 in_dev = __in_dev_get_rcu(state->dev);
2377 if (!in_dev)
2378 continue;
2379 im = rcu_dereference(in_dev->mc_list);
2380 if (im) {
2381 state->in_dev = in_dev;
2382 break;
2385 return im;
2388 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2390 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2392 im = rcu_dereference(im->next_rcu);
2393 while (!im) {
2394 state->dev = next_net_device_rcu(state->dev);
2395 if (!state->dev) {
2396 state->in_dev = NULL;
2397 break;
2399 state->in_dev = __in_dev_get_rcu(state->dev);
2400 if (!state->in_dev)
2401 continue;
2402 im = rcu_dereference(state->in_dev->mc_list);
2404 return im;
2407 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2409 struct ip_mc_list *im = igmp_mc_get_first(seq);
2410 if (im)
2411 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2412 --pos;
2413 return pos ? NULL : im;
2416 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2417 __acquires(rcu)
2419 rcu_read_lock();
2420 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2423 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2425 struct ip_mc_list *im;
2426 if (v == SEQ_START_TOKEN)
2427 im = igmp_mc_get_first(seq);
2428 else
2429 im = igmp_mc_get_next(seq, v);
2430 ++*pos;
2431 return im;
2434 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2435 __releases(rcu)
2437 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2439 state->in_dev = NULL;
2440 state->dev = NULL;
2441 rcu_read_unlock();
2444 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2446 if (v == SEQ_START_TOKEN)
2447 seq_puts(seq,
2448 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2449 else {
2450 struct ip_mc_list *im = (struct ip_mc_list *)v;
2451 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2452 char *querier;
2453 #ifdef CONFIG_IP_MULTICAST
2454 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2455 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2456 "V3";
2457 #else
2458 querier = "NONE";
2459 #endif
2461 if (rcu_dereference(state->in_dev->mc_list) == im) {
2462 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2463 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2466 seq_printf(seq,
2467 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2468 im->multiaddr, im->users,
2469 im->tm_running, im->tm_running ?
2470 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2471 im->reporter);
2473 return 0;
2476 static const struct seq_operations igmp_mc_seq_ops = {
2477 .start = igmp_mc_seq_start,
2478 .next = igmp_mc_seq_next,
2479 .stop = igmp_mc_seq_stop,
2480 .show = igmp_mc_seq_show,
2483 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2485 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2486 sizeof(struct igmp_mc_iter_state));
2489 static const struct file_operations igmp_mc_seq_fops = {
2490 .owner = THIS_MODULE,
2491 .open = igmp_mc_seq_open,
2492 .read = seq_read,
2493 .llseek = seq_lseek,
2494 .release = seq_release_net,
2497 struct igmp_mcf_iter_state {
2498 struct seq_net_private p;
2499 struct net_device *dev;
2500 struct in_device *idev;
2501 struct ip_mc_list *im;
2504 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2506 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2508 struct net *net = seq_file_net(seq);
2509 struct ip_sf_list *psf = NULL;
2510 struct ip_mc_list *im = NULL;
2511 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2513 state->idev = NULL;
2514 state->im = NULL;
2515 for_each_netdev_rcu(net, state->dev) {
2516 struct in_device *idev;
2517 idev = __in_dev_get_rcu(state->dev);
2518 if (unlikely(idev == NULL))
2519 continue;
2520 im = rcu_dereference(idev->mc_list);
2521 if (likely(im != NULL)) {
2522 spin_lock_bh(&im->lock);
2523 psf = im->sources;
2524 if (likely(psf != NULL)) {
2525 state->im = im;
2526 state->idev = idev;
2527 break;
2529 spin_unlock_bh(&im->lock);
2532 return psf;
2535 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2537 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2539 psf = psf->sf_next;
2540 while (!psf) {
2541 spin_unlock_bh(&state->im->lock);
2542 state->im = state->im->next;
2543 while (!state->im) {
2544 state->dev = next_net_device_rcu(state->dev);
2545 if (!state->dev) {
2546 state->idev = NULL;
2547 goto out;
2549 state->idev = __in_dev_get_rcu(state->dev);
2550 if (!state->idev)
2551 continue;
2552 state->im = rcu_dereference(state->idev->mc_list);
2554 if (!state->im)
2555 break;
2556 spin_lock_bh(&state->im->lock);
2557 psf = state->im->sources;
2559 out:
2560 return psf;
2563 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2565 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2566 if (psf)
2567 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2568 --pos;
2569 return pos ? NULL : psf;
2572 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2573 __acquires(rcu)
2575 rcu_read_lock();
2576 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2579 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2581 struct ip_sf_list *psf;
2582 if (v == SEQ_START_TOKEN)
2583 psf = igmp_mcf_get_first(seq);
2584 else
2585 psf = igmp_mcf_get_next(seq, v);
2586 ++*pos;
2587 return psf;
2590 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2591 __releases(rcu)
2593 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2594 if (likely(state->im != NULL)) {
2595 spin_unlock_bh(&state->im->lock);
2596 state->im = NULL;
2598 state->idev = NULL;
2599 state->dev = NULL;
2600 rcu_read_unlock();
2603 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2605 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2606 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2608 if (v == SEQ_START_TOKEN) {
2609 seq_printf(seq,
2610 "%3s %6s "
2611 "%10s %10s %6s %6s\n", "Idx",
2612 "Device", "MCA",
2613 "SRC", "INC", "EXC");
2614 } else {
2615 seq_printf(seq,
2616 "%3d %6.6s 0x%08x "
2617 "0x%08x %6lu %6lu\n",
2618 state->dev->ifindex, state->dev->name,
2619 ntohl(state->im->multiaddr),
2620 ntohl(psf->sf_inaddr),
2621 psf->sf_count[MCAST_INCLUDE],
2622 psf->sf_count[MCAST_EXCLUDE]);
2624 return 0;
2627 static const struct seq_operations igmp_mcf_seq_ops = {
2628 .start = igmp_mcf_seq_start,
2629 .next = igmp_mcf_seq_next,
2630 .stop = igmp_mcf_seq_stop,
2631 .show = igmp_mcf_seq_show,
2634 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2636 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2637 sizeof(struct igmp_mcf_iter_state));
2640 static const struct file_operations igmp_mcf_seq_fops = {
2641 .owner = THIS_MODULE,
2642 .open = igmp_mcf_seq_open,
2643 .read = seq_read,
2644 .llseek = seq_lseek,
2645 .release = seq_release_net,
2648 static int __net_init igmp_net_init(struct net *net)
2650 struct proc_dir_entry *pde;
2652 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2653 if (!pde)
2654 goto out_igmp;
2655 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2656 if (!pde)
2657 goto out_mcfilter;
2658 return 0;
2660 out_mcfilter:
2661 proc_net_remove(net, "igmp");
2662 out_igmp:
2663 return -ENOMEM;
2666 static void __net_exit igmp_net_exit(struct net *net)
2668 proc_net_remove(net, "mcfilter");
2669 proc_net_remove(net, "igmp");
2672 static struct pernet_operations igmp_net_ops = {
2673 .init = igmp_net_init,
2674 .exit = igmp_net_exit,
2677 int __init igmp_mc_proc_init(void)
2679 return register_pernet_subsys(&igmp_net_ops);
2681 #endif