Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / net / ipv4 / igmp.c
blob9dd6fa44045fc5793b8e3c8cf8d6d3ff550fb45d
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 * Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
13 * Authors:
14 * Alan Cox <Alan.Cox@linux.org>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
21 * Fixes:
23 * Alan Cox : Added lots of __inline__ to optimise
24 * the memory usage of all the tiny little
25 * functions.
26 * Alan Cox : Dumped the header building experiment.
27 * Alan Cox : Minor tweaks ready for multicast routing
28 * and extended IGMP protocol.
29 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
30 * writes utterly bogus code otherwise (sigh)
31 * fixed IGMP loopback to behave in the manner
32 * desired by mrouted, fixed the fact it has been
33 * broken since 1.3.6 and cleaned up a few minor
34 * points.
36 * Chih-Jen Chang : Tried to revise IGMP to Version 2
37 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
38 * The enhancements are mainly based on Steve Deering's
39 * ipmulti-3.5 source code.
40 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
41 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
42 * the mrouted version on that device.
43 * Chih-Jen Chang : Added the max_resp_time parameter to
44 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
45 * to identify the multicast router version
46 * and do what the IGMP version 2 specified.
47 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
48 * Tsu-Sheng Tsao if the specified time expired.
49 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
50 * Alan Cox : Use GFP_ATOMIC in the right places.
51 * Christian Daudt : igmp timer wasn't set for local group
52 * memberships but was being deleted,
53 * which caused a "del_timer() called
54 * from %p with timer not initialized\n"
55 * message (960131).
56 * Christian Daudt : removed del_timer from
57 * igmp_timer_expire function (960205).
58 * Christian Daudt : igmp_heard_report now only calls
59 * igmp_timer_expire if tm->running is
60 * true (960216).
61 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
62 * igmp_heard_query never trigger. Expiry
63 * miscalculation fixed in igmp_heard_query
64 * and random() made to return unsigned to
65 * prevent negative expiry times.
66 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
67 * fix from pending 2.1.x patches.
68 * Alan Cox: Forget to enable FDDI support earlier.
69 * Alexey Kuznetsov: Fixed leaving groups on device down.
70 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
71 * David L Stevens: IGMPv3 support, with help from
72 * Vinay Kulkarni
76 #include <linux/config.h>
77 #include <asm/uaccess.h>
78 #include <asm/system.h>
79 #include <linux/types.h>
80 #include <linux/kernel.h>
81 #include <linux/jiffies.h>
82 #include <linux/string.h>
83 #include <linux/socket.h>
84 #include <linux/sockios.h>
85 #include <linux/in.h>
86 #include <linux/inet.h>
87 #include <linux/netdevice.h>
88 #include <linux/skbuff.h>
89 #include <linux/inetdevice.h>
90 #include <linux/igmp.h>
91 #include <linux/if_arp.h>
92 #include <linux/rtnetlink.h>
93 #include <net/ip.h>
94 #include <net/protocol.h>
95 #include <net/route.h>
96 #include <net/sock.h>
97 #include <net/checksum.h>
98 #include <linux/netfilter_ipv4.h>
99 #ifdef CONFIG_IP_MROUTE
100 #include <linux/mroute.h>
101 #endif
102 #ifdef CONFIG_PROC_FS
103 #include <linux/proc_fs.h>
104 #include <linux/seq_file.h>
105 #endif
107 #define IP_MAX_MEMBERSHIPS 20
109 #ifdef CONFIG_IP_MULTICAST
110 /* Parameter names and values are taken from igmp-v2-06 draft */
112 #define IGMP_V1_Router_Present_Timeout (400*HZ)
113 #define IGMP_V2_Router_Present_Timeout (400*HZ)
114 #define IGMP_Unsolicited_Report_Interval (10*HZ)
115 #define IGMP_Query_Response_Interval (10*HZ)
116 #define IGMP_Unsolicited_Report_Count 2
119 #define IGMP_Initial_Report_Delay (1)
121 /* IGMP_Initial_Report_Delay is not from IGMP specs!
122 * IGMP specs require to report membership immediately after
123 * joining a group, but we delay the first report by a
124 * small interval. It seems more natural and still does not
125 * contradict to specs provided this delay is small enough.
128 #define IGMP_V1_SEEN(in_dev) ((in_dev)->mr_v1_seen && \
129 time_before(jiffies, (in_dev)->mr_v1_seen))
130 #define IGMP_V2_SEEN(in_dev) ((in_dev)->mr_v2_seen && \
131 time_before(jiffies, (in_dev)->mr_v2_seen))
133 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
134 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr);
135 static void igmpv3_clear_delrec(struct in_device *in_dev);
136 static int sf_setstate(struct ip_mc_list *pmc);
137 static void sf_markstate(struct ip_mc_list *pmc);
138 #endif
139 static void ip_mc_clear_src(struct ip_mc_list *pmc);
140 int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
141 int sfcount, __u32 *psfsrc, int delta);
143 static void ip_ma_put(struct ip_mc_list *im)
145 if (atomic_dec_and_test(&im->refcnt)) {
146 in_dev_put(im->interface);
147 kfree(im);
151 #ifdef CONFIG_IP_MULTICAST
154 * Timer management
157 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
159 spin_lock_bh(&im->lock);
160 if (del_timer(&im->timer))
161 atomic_dec(&im->refcnt);
162 im->tm_running=0;
163 im->reporter = 0;
164 im->unsolicit_count = 0;
165 spin_unlock_bh(&im->lock);
168 /* It must be called with locked im->lock */
169 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
171 int tv=net_random() % max_delay;
173 im->tm_running=1;
174 if (!mod_timer(&im->timer, jiffies+tv+2))
175 atomic_inc(&im->refcnt);
178 static void igmp_gq_start_timer(struct in_device *in_dev)
180 int tv = net_random() % in_dev->mr_maxdelay;
182 in_dev->mr_gq_running = 1;
183 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
184 in_dev_hold(in_dev);
187 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
189 int tv = net_random() % delay;
191 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
192 in_dev_hold(in_dev);
195 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
197 spin_lock_bh(&im->lock);
198 im->unsolicit_count = 0;
199 if (del_timer(&im->timer)) {
200 if ((long)(im->timer.expires-jiffies) < max_delay) {
201 add_timer(&im->timer);
202 im->tm_running=1;
203 spin_unlock_bh(&im->lock);
204 return;
206 atomic_dec(&im->refcnt);
208 igmp_start_timer(im, max_delay);
209 spin_unlock_bh(&im->lock);
214 * Send an IGMP report.
217 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
220 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
221 int gdeleted, int sdeleted)
223 switch (type) {
224 case IGMPV3_MODE_IS_INCLUDE:
225 case IGMPV3_MODE_IS_EXCLUDE:
226 if (gdeleted || sdeleted)
227 return 0;
228 return !(pmc->gsquery && !psf->sf_gsresp);
229 case IGMPV3_CHANGE_TO_INCLUDE:
230 if (gdeleted || sdeleted)
231 return 0;
232 return psf->sf_count[MCAST_INCLUDE] != 0;
233 case IGMPV3_CHANGE_TO_EXCLUDE:
234 if (gdeleted || sdeleted)
235 return 0;
236 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
237 psf->sf_count[MCAST_INCLUDE])
238 return 0;
239 return pmc->sfcount[MCAST_EXCLUDE] ==
240 psf->sf_count[MCAST_EXCLUDE];
241 case IGMPV3_ALLOW_NEW_SOURCES:
242 if (gdeleted || !psf->sf_crcount)
243 return 0;
244 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
245 case IGMPV3_BLOCK_OLD_SOURCES:
246 if (pmc->sfmode == MCAST_INCLUDE)
247 return gdeleted || (psf->sf_crcount && sdeleted);
248 return psf->sf_crcount && !gdeleted && !sdeleted;
250 return 0;
253 static int
254 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
256 struct ip_sf_list *psf;
257 int scount = 0;
259 for (psf=pmc->sources; psf; psf=psf->sf_next) {
260 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
261 continue;
262 scount++;
264 return scount;
267 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
269 struct sk_buff *skb;
270 struct rtable *rt;
271 struct iphdr *pip;
272 struct igmpv3_report *pig;
274 skb = alloc_skb(size + dev->hard_header_len + 15, GFP_ATOMIC);
275 if (skb == NULL)
276 return 0;
279 struct flowi fl = { .oif = dev->ifindex,
280 .nl_u = { .ip4_u = {
281 .daddr = IGMPV3_ALL_MCR } },
282 .proto = IPPROTO_IGMP };
283 if (ip_route_output_key(&rt, &fl))
284 return 0;
286 if (rt->rt_src == 0) {
287 ip_rt_put(rt);
288 return 0;
291 skb->dst = &rt->u.dst;
292 skb->dev = dev;
294 skb_reserve(skb, (dev->hard_header_len+15)&~15);
296 skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
298 pip->version = 4;
299 pip->ihl = (sizeof(struct iphdr)+4)>>2;
300 pip->tos = 0xc0;
301 pip->frag_off = htons(IP_DF);
302 pip->ttl = 1;
303 pip->daddr = rt->rt_dst;
304 pip->saddr = rt->rt_src;
305 pip->protocol = IPPROTO_IGMP;
306 pip->tot_len = 0; /* filled in later */
307 ip_select_ident(pip, &rt->u.dst, NULL);
308 ((u8*)&pip[1])[0] = IPOPT_RA;
309 ((u8*)&pip[1])[1] = 4;
310 ((u8*)&pip[1])[2] = 0;
311 ((u8*)&pip[1])[3] = 0;
313 pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
314 skb->h.igmph = (struct igmphdr *)pig;
315 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
316 pig->resv1 = 0;
317 pig->csum = 0;
318 pig->resv2 = 0;
319 pig->ngrec = 0;
320 return skb;
323 static int igmpv3_sendpack(struct sk_buff *skb)
325 struct iphdr *pip = skb->nh.iph;
326 struct igmphdr *pig = skb->h.igmph;
327 int iplen, igmplen;
329 iplen = skb->tail - (unsigned char *)skb->nh.iph;
330 pip->tot_len = htons(iplen);
331 ip_send_check(pip);
333 igmplen = skb->tail - (unsigned char *)skb->h.igmph;
334 pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
336 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
337 dst_output);
340 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
342 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
345 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
346 int type, struct igmpv3_grec **ppgr)
348 struct net_device *dev = pmc->interface->dev;
349 struct igmpv3_report *pih;
350 struct igmpv3_grec *pgr;
352 if (!skb)
353 skb = igmpv3_newpack(dev, dev->mtu);
354 if (!skb)
355 return 0;
356 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
357 pgr->grec_type = type;
358 pgr->grec_auxwords = 0;
359 pgr->grec_nsrcs = 0;
360 pgr->grec_mca = pmc->multiaddr;
361 pih = (struct igmpv3_report *)skb->h.igmph;
362 pih->ngrec = htons(ntohs(pih->ngrec)+1);
363 *ppgr = pgr;
364 return skb;
367 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
368 skb_tailroom(skb)) : 0)
370 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
371 int type, int gdeleted, int sdeleted)
373 struct net_device *dev = pmc->interface->dev;
374 struct igmpv3_report *pih;
375 struct igmpv3_grec *pgr = 0;
376 struct ip_sf_list *psf, *psf_next, *psf_prev, *psf_list;
377 int scount, first, isquery, truncate;
379 if (pmc->multiaddr == IGMP_ALL_HOSTS)
380 return skb;
382 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
383 type == IGMPV3_MODE_IS_EXCLUDE;
384 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
385 type == IGMPV3_CHANGE_TO_EXCLUDE;
387 psf_list = sdeleted ? pmc->tomb : pmc->sources;
389 if (!psf_list) {
390 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
391 type == IGMPV3_BLOCK_OLD_SOURCES)
392 return skb;
393 if (pmc->crcount || isquery) {
394 /* make sure we have room for group header and at
395 * least one source.
397 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)+
398 sizeof(__u32)) {
399 igmpv3_sendpack(skb);
400 skb = 0; /* add_grhead will get a new one */
402 skb = add_grhead(skb, pmc, type, &pgr);
404 return skb;
406 pih = skb ? (struct igmpv3_report *)skb->h.igmph : 0;
408 /* EX and TO_EX get a fresh packet, if needed */
409 if (truncate) {
410 if (pih && pih->ngrec &&
411 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
412 if (skb)
413 igmpv3_sendpack(skb);
414 skb = igmpv3_newpack(dev, dev->mtu);
417 first = 1;
418 scount = 0;
419 psf_prev = 0;
420 for (psf=psf_list; psf; psf=psf_next) {
421 u32 *psrc;
423 psf_next = psf->sf_next;
425 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
426 psf_prev = psf;
427 continue;
430 /* clear marks on query responses */
431 if (isquery)
432 psf->sf_gsresp = 0;
434 if (AVAILABLE(skb) < sizeof(u32) +
435 first*sizeof(struct igmpv3_grec)) {
436 if (truncate && !first)
437 break; /* truncate these */
438 if (pgr)
439 pgr->grec_nsrcs = htons(scount);
440 if (skb)
441 igmpv3_sendpack(skb);
442 skb = igmpv3_newpack(dev, dev->mtu);
443 first = 1;
444 scount = 0;
446 if (first) {
447 skb = add_grhead(skb, pmc, type, &pgr);
448 first = 0;
450 psrc = (u32 *)skb_put(skb, sizeof(u32));
451 *psrc = psf->sf_inaddr;
452 scount++;
453 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
454 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
455 psf->sf_crcount--;
456 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
457 if (psf_prev)
458 psf_prev->sf_next = psf->sf_next;
459 else
460 pmc->tomb = psf->sf_next;
461 kfree(psf);
462 continue;
465 psf_prev = psf;
467 if (pgr)
468 pgr->grec_nsrcs = htons(scount);
470 if (isquery)
471 pmc->gsquery = 0; /* clear query state on report */
472 return skb;
475 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
477 struct sk_buff *skb = 0;
478 int type;
480 if (!pmc) {
481 read_lock(&in_dev->lock);
482 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
483 if (pmc->multiaddr == IGMP_ALL_HOSTS)
484 continue;
485 spin_lock_bh(&pmc->lock);
486 if (pmc->sfcount[MCAST_EXCLUDE])
487 type = IGMPV3_MODE_IS_EXCLUDE;
488 else
489 type = IGMPV3_MODE_IS_INCLUDE;
490 skb = add_grec(skb, pmc, type, 0, 0);
491 spin_unlock_bh(&pmc->lock);
493 read_unlock(&in_dev->lock);
494 } else {
495 spin_lock_bh(&pmc->lock);
496 if (pmc->sfcount[MCAST_EXCLUDE])
497 type = IGMPV3_MODE_IS_EXCLUDE;
498 else
499 type = IGMPV3_MODE_IS_INCLUDE;
500 skb = add_grec(skb, pmc, type, 0, 0);
501 spin_unlock_bh(&pmc->lock);
503 if (!skb)
504 return 0;
505 return igmpv3_sendpack(skb);
509 * remove zero-count source records from a source filter list
511 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
513 struct ip_sf_list *psf_prev, *psf_next, *psf;
515 psf_prev = 0;
516 for (psf=*ppsf; psf; psf = psf_next) {
517 psf_next = psf->sf_next;
518 if (psf->sf_crcount == 0) {
519 if (psf_prev)
520 psf_prev->sf_next = psf->sf_next;
521 else
522 *ppsf = psf->sf_next;
523 kfree(psf);
524 } else
525 psf_prev = psf;
529 static void igmpv3_send_cr(struct in_device *in_dev)
531 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
532 struct sk_buff *skb = 0;
533 int type, dtype;
535 read_lock(&in_dev->lock);
536 write_lock_bh(&in_dev->mc_lock);
538 /* deleted MCA's */
539 pmc_prev = 0;
540 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
541 pmc_next = pmc->next;
542 if (pmc->sfmode == MCAST_INCLUDE) {
543 type = IGMPV3_BLOCK_OLD_SOURCES;
544 dtype = IGMPV3_BLOCK_OLD_SOURCES;
545 skb = add_grec(skb, pmc, type, 1, 0);
546 skb = add_grec(skb, pmc, dtype, 1, 1);
548 if (pmc->crcount) {
549 pmc->crcount--;
550 if (pmc->sfmode == MCAST_EXCLUDE) {
551 type = IGMPV3_CHANGE_TO_INCLUDE;
552 skb = add_grec(skb, pmc, type, 1, 0);
554 if (pmc->crcount == 0) {
555 igmpv3_clear_zeros(&pmc->tomb);
556 igmpv3_clear_zeros(&pmc->sources);
559 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
560 if (pmc_prev)
561 pmc_prev->next = pmc_next;
562 else
563 in_dev->mc_tomb = pmc_next;
564 in_dev_put(pmc->interface);
565 kfree(pmc);
566 } else
567 pmc_prev = pmc;
569 write_unlock_bh(&in_dev->mc_lock);
571 /* change recs */
572 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
573 spin_lock_bh(&pmc->lock);
574 if (pmc->sfcount[MCAST_EXCLUDE]) {
575 type = IGMPV3_BLOCK_OLD_SOURCES;
576 dtype = IGMPV3_ALLOW_NEW_SOURCES;
577 } else {
578 type = IGMPV3_ALLOW_NEW_SOURCES;
579 dtype = IGMPV3_BLOCK_OLD_SOURCES;
581 skb = add_grec(skb, pmc, type, 0, 0);
582 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
584 /* filter mode changes */
585 if (pmc->crcount) {
586 pmc->crcount--;
587 if (pmc->sfmode == MCAST_EXCLUDE)
588 type = IGMPV3_CHANGE_TO_EXCLUDE;
589 else
590 type = IGMPV3_CHANGE_TO_INCLUDE;
591 skb = add_grec(skb, pmc, type, 0, 0);
593 spin_unlock_bh(&pmc->lock);
595 read_unlock(&in_dev->lock);
596 if (!skb)
597 return;
598 (void) igmpv3_sendpack(skb);
601 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
602 int type)
604 struct sk_buff *skb;
605 struct iphdr *iph;
606 struct igmphdr *ih;
607 struct rtable *rt;
608 struct net_device *dev = in_dev->dev;
609 u32 group = pmc ? pmc->multiaddr : 0;
610 u32 dst;
612 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
613 return igmpv3_send_report(in_dev, pmc);
614 else if (type == IGMP_HOST_LEAVE_MESSAGE)
615 dst = IGMP_ALL_ROUTER;
616 else
617 dst = group;
620 struct flowi fl = { .oif = dev->ifindex,
621 .nl_u = { .ip4_u = { .daddr = dst } },
622 .proto = IPPROTO_IGMP };
623 if (ip_route_output_key(&rt, &fl))
624 return -1;
626 if (rt->rt_src == 0) {
627 ip_rt_put(rt);
628 return -1;
631 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
632 if (skb == NULL) {
633 ip_rt_put(rt);
634 return -1;
637 skb->dst = &rt->u.dst;
639 skb_reserve(skb, LL_RESERVED_SPACE(dev));
641 skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
643 iph->version = 4;
644 iph->ihl = (sizeof(struct iphdr)+4)>>2;
645 iph->tos = 0xc0;
646 iph->frag_off = htons(IP_DF);
647 iph->ttl = 1;
648 iph->daddr = dst;
649 iph->saddr = rt->rt_src;
650 iph->protocol = IPPROTO_IGMP;
651 iph->tot_len = htons(IGMP_SIZE);
652 ip_select_ident(iph, &rt->u.dst, NULL);
653 ((u8*)&iph[1])[0] = IPOPT_RA;
654 ((u8*)&iph[1])[1] = 4;
655 ((u8*)&iph[1])[2] = 0;
656 ((u8*)&iph[1])[3] = 0;
657 ip_send_check(iph);
659 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
660 ih->type=type;
661 ih->code=0;
662 ih->csum=0;
663 ih->group=group;
664 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
666 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
667 dst_output);
670 static void igmp_gq_timer_expire(unsigned long data)
672 struct in_device *in_dev = (struct in_device *)data;
674 in_dev->mr_gq_running = 0;
675 igmpv3_send_report(in_dev, 0);
676 __in_dev_put(in_dev);
679 static void igmp_ifc_timer_expire(unsigned long data)
681 struct in_device *in_dev = (struct in_device *)data;
683 igmpv3_send_cr(in_dev);
684 if (in_dev->mr_ifc_count) {
685 in_dev->mr_ifc_count--;
686 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
688 __in_dev_put(in_dev);
691 static void igmp_ifc_event(struct in_device *in_dev)
693 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
694 return;
695 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
696 IGMP_Unsolicited_Report_Count;
697 igmp_ifc_start_timer(in_dev, 1);
701 static void igmp_timer_expire(unsigned long data)
703 struct ip_mc_list *im=(struct ip_mc_list *)data;
704 struct in_device *in_dev = im->interface;
706 spin_lock(&im->lock);
707 im->tm_running=0;
709 if (im->unsolicit_count) {
710 im->unsolicit_count--;
711 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
713 im->reporter = 1;
714 spin_unlock(&im->lock);
716 if (IGMP_V1_SEEN(in_dev))
717 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
718 else if (IGMP_V2_SEEN(in_dev))
719 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
720 else
721 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
723 ip_ma_put(im);
726 static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
728 struct ip_sf_list *psf;
729 int i, scount;
731 scount = 0;
732 for (psf=pmc->sources; psf; psf=psf->sf_next) {
733 if (scount == nsrcs)
734 break;
735 for (i=0; i<nsrcs; i++)
736 if (srcs[i] == psf->sf_inaddr) {
737 psf->sf_gsresp = 1;
738 scount++;
739 break;
744 static void igmp_heard_report(struct in_device *in_dev, u32 group)
746 struct ip_mc_list *im;
748 /* Timers are only set for non-local groups */
750 if (group == IGMP_ALL_HOSTS)
751 return;
753 read_lock(&in_dev->lock);
754 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
755 if (im->multiaddr == group) {
756 igmp_stop_timer(im);
757 break;
760 read_unlock(&in_dev->lock);
763 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
764 int len)
766 struct igmphdr *ih = skb->h.igmph;
767 struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
768 struct ip_mc_list *im;
769 u32 group = ih->group;
770 int max_delay;
771 int mark = 0;
774 if (len == 8) {
775 if (ih->code == 0) {
776 /* Alas, old v1 router presents here. */
778 max_delay = IGMP_Query_Response_Interval;
779 in_dev->mr_v1_seen = jiffies +
780 IGMP_V1_Router_Present_Timeout;
781 group = 0;
782 } else {
783 /* v2 router present */
784 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
785 in_dev->mr_v2_seen = jiffies +
786 IGMP_V2_Router_Present_Timeout;
788 /* cancel the interface change timer */
789 in_dev->mr_ifc_count = 0;
790 if (del_timer(&in_dev->mr_ifc_timer))
791 __in_dev_put(in_dev);
792 /* clear deleted report items */
793 igmpv3_clear_delrec(in_dev);
794 } else if (len < 12) {
795 return; /* ignore bogus packet; freed by caller */
796 } else { /* v3 */
797 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
798 return;
800 ih3 = (struct igmpv3_query *) skb->h.raw;
801 if (ih3->nsrcs) {
802 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
803 + ntohs(ih3->nsrcs)*sizeof(__u32)))
804 return;
805 ih3 = (struct igmpv3_query *) skb->h.raw;
808 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
809 if (!max_delay)
810 max_delay = 1; /* can't mod w/ 0 */
811 in_dev->mr_maxdelay = max_delay;
812 if (ih3->qrv)
813 in_dev->mr_qrv = ih3->qrv;
814 if (!group) { /* general query */
815 if (ih3->nsrcs)
816 return; /* no sources allowed */
817 igmp_gq_start_timer(in_dev);
818 return;
820 /* mark sources to include, if group & source-specific */
821 mark = ih3->nsrcs != 0;
825 * - Start the timers in all of our membership records
826 * that the query applies to for the interface on
827 * which the query arrived excl. those that belong
828 * to a "local" group (224.0.0.X)
829 * - For timers already running check if they need to
830 * be reset.
831 * - Use the igmp->igmp_code field as the maximum
832 * delay possible
834 read_lock(&in_dev->lock);
835 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
836 if (group && group != im->multiaddr)
837 continue;
838 if (im->multiaddr == IGMP_ALL_HOSTS)
839 continue;
840 spin_lock_bh(&im->lock);
841 if (im->tm_running)
842 im->gsquery = im->gsquery && mark;
843 else
844 im->gsquery = mark;
845 if (im->gsquery)
846 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
847 spin_unlock_bh(&im->lock);
848 igmp_mod_timer(im, max_delay);
850 read_unlock(&in_dev->lock);
853 int igmp_rcv(struct sk_buff *skb)
855 /* This basically follows the spec line by line -- see RFC1112 */
856 struct igmphdr *ih;
857 struct in_device *in_dev = in_dev_get(skb->dev);
858 int len = skb->len;
860 if (in_dev==NULL) {
861 kfree_skb(skb);
862 return 0;
865 if (!pskb_may_pull(skb, sizeof(struct igmphdr)) ||
866 (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
867 in_dev_put(in_dev);
868 kfree_skb(skb);
869 return 0;
872 ih = skb->h.igmph;
873 switch (ih->type) {
874 case IGMP_HOST_MEMBERSHIP_QUERY:
875 igmp_heard_query(in_dev, skb, len);
876 break;
877 case IGMP_HOST_MEMBERSHIP_REPORT:
878 case IGMPV2_HOST_MEMBERSHIP_REPORT:
879 case IGMPV3_HOST_MEMBERSHIP_REPORT:
880 /* Is it our report looped back? */
881 if (((struct rtable*)skb->dst)->fl.iif == 0)
882 break;
883 igmp_heard_report(in_dev, ih->group);
884 break;
885 case IGMP_PIM:
886 #ifdef CONFIG_IP_PIMSM_V1
887 in_dev_put(in_dev);
888 return pim_rcv_v1(skb);
889 #endif
890 case IGMP_DVMRP:
891 case IGMP_TRACE:
892 case IGMP_HOST_LEAVE_MESSAGE:
893 case IGMP_MTRACE:
894 case IGMP_MTRACE_RESP:
895 break;
896 default:
897 NETDEBUG(printk(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type));
899 in_dev_put(in_dev);
900 kfree_skb(skb);
901 return 0;
904 #endif
908 * Add a filter to a device
911 static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
913 char buf[MAX_ADDR_LEN];
914 struct net_device *dev = in_dev->dev;
916 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
917 We will get multicast token leakage, when IFF_MULTICAST
918 is changed. This check should be done in dev->set_multicast_list
919 routine. Something sort of:
920 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
921 --ANK
923 if (arp_mc_map(addr, buf, dev, 0) == 0)
924 dev_mc_add(dev,buf,dev->addr_len,0);
928 * Remove a filter from a device
931 static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
933 char buf[MAX_ADDR_LEN];
934 struct net_device *dev = in_dev->dev;
936 if (arp_mc_map(addr, buf, dev, 0) == 0)
937 dev_mc_delete(dev,buf,dev->addr_len,0);
940 #ifdef CONFIG_IP_MULTICAST
942 * deleted ip_mc_list manipulation
944 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
946 struct ip_mc_list *pmc;
948 /* this is an "ip_mc_list" for convenience; only the fields below
949 * are actually used. In particular, the refcnt and users are not
950 * used for management of the delete list. Using the same structure
951 * for deleted items allows change reports to use common code with
952 * non-deleted or query-response MCA's.
954 pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
955 if (!pmc)
956 return;
957 memset(pmc, 0, sizeof(*pmc));
958 spin_lock_bh(&im->lock);
959 pmc->interface = im->interface;
960 in_dev_hold(in_dev);
961 pmc->multiaddr = im->multiaddr;
962 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
963 IGMP_Unsolicited_Report_Count;
964 pmc->sfmode = im->sfmode;
965 if (pmc->sfmode == MCAST_INCLUDE) {
966 struct ip_sf_list *psf;
968 pmc->tomb = im->tomb;
969 pmc->sources = im->sources;
970 im->tomb = im->sources = 0;
971 for (psf=pmc->sources; psf; psf=psf->sf_next)
972 psf->sf_crcount = pmc->crcount;
974 spin_unlock_bh(&im->lock);
976 write_lock_bh(&in_dev->mc_lock);
977 pmc->next = in_dev->mc_tomb;
978 in_dev->mc_tomb = pmc;
979 write_unlock_bh(&in_dev->mc_lock);
982 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
984 struct ip_mc_list *pmc, *pmc_prev;
985 struct ip_sf_list *psf, *psf_next;
987 write_lock_bh(&in_dev->mc_lock);
988 pmc_prev = 0;
989 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
990 if (pmc->multiaddr == multiaddr)
991 break;
992 pmc_prev = pmc;
994 if (pmc) {
995 if (pmc_prev)
996 pmc_prev->next = pmc->next;
997 else
998 in_dev->mc_tomb = pmc->next;
1000 write_unlock_bh(&in_dev->mc_lock);
1001 if (pmc) {
1002 for (psf=pmc->tomb; psf; psf=psf_next) {
1003 psf_next = psf->sf_next;
1004 kfree(psf);
1006 in_dev_put(pmc->interface);
1007 kfree(pmc);
1011 static void igmpv3_clear_delrec(struct in_device *in_dev)
1013 struct ip_mc_list *pmc, *nextpmc;
1015 write_lock_bh(&in_dev->mc_lock);
1016 pmc = in_dev->mc_tomb;
1017 in_dev->mc_tomb = 0;
1018 write_unlock_bh(&in_dev->mc_lock);
1020 for (; pmc; pmc = nextpmc) {
1021 nextpmc = pmc->next;
1022 ip_mc_clear_src(pmc);
1023 in_dev_put(pmc->interface);
1024 kfree(pmc);
1026 /* clear dead sources, too */
1027 read_lock(&in_dev->lock);
1028 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1029 struct ip_sf_list *psf, *psf_next;
1031 spin_lock_bh(&pmc->lock);
1032 psf = pmc->tomb;
1033 pmc->tomb = 0;
1034 spin_unlock_bh(&pmc->lock);
1035 for (; psf; psf=psf_next) {
1036 psf_next = psf->sf_next;
1037 kfree(psf);
1040 read_unlock(&in_dev->lock);
1042 #endif
1044 static void igmp_group_dropped(struct ip_mc_list *im)
1046 struct in_device *in_dev = im->interface;
1047 #ifdef CONFIG_IP_MULTICAST
1048 int reporter;
1049 #endif
1051 if (im->loaded) {
1052 im->loaded = 0;
1053 ip_mc_filter_del(in_dev, im->multiaddr);
1056 #ifdef CONFIG_IP_MULTICAST
1057 if (im->multiaddr == IGMP_ALL_HOSTS)
1058 return;
1060 reporter = im->reporter;
1061 igmp_stop_timer(im);
1063 if (IGMP_V1_SEEN(in_dev))
1064 goto done;
1065 if (IGMP_V2_SEEN(in_dev)) {
1066 if (reporter)
1067 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1068 goto done;
1070 /* IGMPv3 */
1071 igmpv3_add_delrec(in_dev, im);
1073 igmp_ifc_event(in_dev);
1074 done:
1075 #endif
1076 ip_mc_clear_src(im);
1079 static void igmp_group_added(struct ip_mc_list *im)
1081 struct in_device *in_dev = im->interface;
1083 if (im->loaded == 0) {
1084 im->loaded = 1;
1085 ip_mc_filter_add(in_dev, im->multiaddr);
1088 #ifdef CONFIG_IP_MULTICAST
1089 if (im->multiaddr == IGMP_ALL_HOSTS)
1090 return;
1092 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1093 spin_lock_bh(&im->lock);
1094 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1095 spin_unlock_bh(&im->lock);
1096 return;
1098 /* else, v3 */
1100 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1101 IGMP_Unsolicited_Report_Count;
1102 igmp_ifc_event(in_dev);
1103 #endif
1108 * Multicast list managers
1113 * A socket has joined a multicast group on device dev.
1116 void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1118 struct ip_mc_list *im;
1120 ASSERT_RTNL();
1122 for (im=in_dev->mc_list; im; im=im->next) {
1123 if (im->multiaddr == addr) {
1124 im->users++;
1125 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, 0, 0);
1126 goto out;
1130 im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1131 if (!im)
1132 goto out;
1134 im->users=1;
1135 im->interface=in_dev;
1136 in_dev_hold(in_dev);
1137 im->multiaddr=addr;
1138 /* initial mode is (EX, empty) */
1139 im->sfmode = MCAST_EXCLUDE;
1140 im->sfcount[MCAST_INCLUDE] = 0;
1141 im->sfcount[MCAST_EXCLUDE] = 1;
1142 im->sources = 0;
1143 im->tomb = 0;
1144 im->crcount = 0;
1145 atomic_set(&im->refcnt, 1);
1146 spin_lock_init(&im->lock);
1147 #ifdef CONFIG_IP_MULTICAST
1148 im->tm_running=0;
1149 init_timer(&im->timer);
1150 im->timer.data=(unsigned long)im;
1151 im->timer.function=&igmp_timer_expire;
1152 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1153 im->reporter = 0;
1154 im->gsquery = 0;
1155 #endif
1156 im->loaded = 0;
1157 write_lock_bh(&in_dev->lock);
1158 im->next=in_dev->mc_list;
1159 in_dev->mc_list=im;
1160 write_unlock_bh(&in_dev->lock);
1161 #ifdef CONFIG_IP_MULTICAST
1162 igmpv3_del_delrec(in_dev, im->multiaddr);
1163 #endif
1164 igmp_group_added(im);
1165 if (in_dev->dev->flags & IFF_UP)
1166 ip_rt_multicast_event(in_dev);
1167 out:
1168 return;
1172 * A socket has left a multicast group on device dev
1175 void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1177 struct ip_mc_list *i, **ip;
1179 ASSERT_RTNL();
1181 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1182 if (i->multiaddr==addr) {
1183 if (--i->users == 0) {
1184 write_lock_bh(&in_dev->lock);
1185 *ip = i->next;
1186 write_unlock_bh(&in_dev->lock);
1187 igmp_group_dropped(i);
1189 if (in_dev->dev->flags & IFF_UP)
1190 ip_rt_multicast_event(in_dev);
1192 ip_ma_put(i);
1193 return;
1195 break;
1200 /* Device going down */
1202 void ip_mc_down(struct in_device *in_dev)
1204 struct ip_mc_list *i;
1206 ASSERT_RTNL();
1208 #ifdef CONFIG_IP_MULTICAST
1209 in_dev->mr_ifc_count = 0;
1210 if (del_timer(&in_dev->mr_ifc_timer))
1211 __in_dev_put(in_dev);
1212 in_dev->mr_gq_running = 0;
1213 if (del_timer(&in_dev->mr_gq_timer))
1214 __in_dev_put(in_dev);
1215 #endif
1217 for (i=in_dev->mc_list; i; i=i->next)
1218 igmp_group_dropped(i);
1220 #ifdef CONFIG_IP_MULTICAST
1221 igmpv3_clear_delrec(in_dev);
1222 #endif
1224 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1227 /* Device going up */
1229 void ip_mc_up(struct in_device *in_dev)
1231 struct ip_mc_list *i;
1233 ASSERT_RTNL();
1235 in_dev->mc_tomb = 0;
1236 #ifdef CONFIG_IP_MULTICAST
1237 in_dev->mr_gq_running = 0;
1238 init_timer(&in_dev->mr_gq_timer);
1239 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1240 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1241 in_dev->mr_ifc_count = 0;
1242 init_timer(&in_dev->mr_ifc_timer);
1243 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1244 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1245 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1246 #endif
1248 in_dev->mc_lock = RW_LOCK_UNLOCKED;
1249 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1251 for (i=in_dev->mc_list; i; i=i->next)
1252 igmp_group_added(i);
1256 * Device is about to be destroyed: clean up.
1259 void ip_mc_destroy_dev(struct in_device *in_dev)
1261 struct ip_mc_list *i;
1263 ASSERT_RTNL();
1265 write_lock_bh(&in_dev->lock);
1266 while ((i = in_dev->mc_list) != NULL) {
1267 in_dev->mc_list = i->next;
1268 write_unlock_bh(&in_dev->lock);
1270 igmp_group_dropped(i);
1271 ip_ma_put(i);
1273 write_lock_bh(&in_dev->lock);
1275 write_unlock_bh(&in_dev->lock);
1278 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1280 struct flowi fl = { .nl_u = { .ip4_u =
1281 { .daddr = imr->imr_multiaddr.s_addr } } };
1282 struct rtable *rt;
1283 struct net_device *dev = NULL;
1284 struct in_device *idev = NULL;
1286 if (imr->imr_ifindex) {
1287 idev = inetdev_by_index(imr->imr_ifindex);
1288 if (idev)
1289 __in_dev_put(idev);
1290 return idev;
1292 if (imr->imr_address.s_addr) {
1293 dev = ip_dev_find(imr->imr_address.s_addr);
1294 if (!dev)
1295 return NULL;
1296 __dev_put(dev);
1299 if (!dev && !ip_route_output_key(&rt, &fl)) {
1300 dev = rt->u.dst.dev;
1301 ip_rt_put(rt);
1303 if (dev) {
1304 imr->imr_ifindex = dev->ifindex;
1305 idev = __in_dev_get(dev);
1307 return idev;
1311 * Join a socket to a group
1313 int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1316 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1317 __u32 *psfsrc)
1319 struct ip_sf_list *psf, *psf_prev;
1320 int rv = 0;
1322 psf_prev = 0;
1323 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1324 if (psf->sf_inaddr == *psfsrc)
1325 break;
1326 psf_prev = psf;
1328 if (!psf || psf->sf_count[sfmode] == 0) {
1329 /* source filter not found, or count wrong => bug */
1330 return -ESRCH;
1332 psf->sf_count[sfmode]--;
1333 if (psf->sf_count[sfmode] == 0) {
1334 ip_rt_multicast_event(pmc->interface);
1336 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1337 #ifdef CONFIG_IP_MULTICAST
1338 struct in_device *in_dev = pmc->interface;
1339 #endif
1341 /* no more filters for this source */
1342 if (psf_prev)
1343 psf_prev->sf_next = psf->sf_next;
1344 else
1345 pmc->sources = psf->sf_next;
1346 #ifdef CONFIG_IP_MULTICAST
1347 if (psf->sf_oldin &&
1348 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1349 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1350 IGMP_Unsolicited_Report_Count;
1351 psf->sf_next = pmc->tomb;
1352 pmc->tomb = psf;
1353 rv = 1;
1354 } else
1355 #endif
1356 kfree(psf);
1358 return rv;
1361 #ifndef CONFIG_IP_MULTICAST
1362 #define igmp_ifc_event(x) do { } while (0)
1363 #endif
1365 int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1366 int sfcount, __u32 *psfsrc, int delta)
1368 struct ip_mc_list *pmc;
1369 int changerec = 0;
1370 int i, err;
1372 if (!in_dev)
1373 return -ENODEV;
1374 read_lock(&in_dev->lock);
1375 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1376 if (*pmca == pmc->multiaddr)
1377 break;
1379 if (!pmc) {
1380 /* MCA not found?? bug */
1381 read_unlock(&in_dev->lock);
1382 return -ESRCH;
1384 spin_lock_bh(&pmc->lock);
1385 read_unlock(&in_dev->lock);
1386 #ifdef CONFIG_IP_MULTICAST
1387 sf_markstate(pmc);
1388 #endif
1389 if (!delta) {
1390 if (!pmc->sfcount[sfmode])
1391 return -EINVAL;
1392 pmc->sfcount[sfmode]--;
1394 err = 0;
1395 for (i=0; i<sfcount; i++) {
1396 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1398 changerec |= rv > 0;
1399 if (!err && rv < 0)
1400 err = rv;
1402 if (pmc->sfmode == MCAST_EXCLUDE &&
1403 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1404 pmc->sfcount[MCAST_INCLUDE]) {
1405 #ifdef CONFIG_IP_MULTICAST
1406 struct ip_sf_list *psf;
1407 #endif
1409 /* filter mode change */
1410 pmc->sfmode = MCAST_INCLUDE;
1411 #ifdef CONFIG_IP_MULTICAST
1412 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1413 IGMP_Unsolicited_Report_Count;
1414 in_dev->mr_ifc_count = pmc->crcount;
1415 for (psf=pmc->sources; psf; psf = psf->sf_next)
1416 psf->sf_crcount = 0;
1417 igmp_ifc_event(pmc->interface);
1418 } else if (sf_setstate(pmc) || changerec) {
1419 igmp_ifc_event(pmc->interface);
1420 #endif
1422 spin_unlock_bh(&pmc->lock);
1423 return err;
1427 * Add multicast single-source filter to the interface list
1429 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1430 __u32 *psfsrc, int delta)
1432 struct ip_sf_list *psf, *psf_prev;
1434 psf_prev = 0;
1435 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1436 if (psf->sf_inaddr == *psfsrc)
1437 break;
1438 psf_prev = psf;
1440 if (!psf) {
1441 psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1442 if (!psf)
1443 return -ENOBUFS;
1444 memset(psf, 0, sizeof(*psf));
1445 psf->sf_inaddr = *psfsrc;
1446 if (psf_prev) {
1447 psf_prev->sf_next = psf;
1448 } else
1449 pmc->sources = psf;
1451 psf->sf_count[sfmode]++;
1452 if (psf->sf_count[sfmode] == 1) {
1453 ip_rt_multicast_event(pmc->interface);
1455 return 0;
1458 #ifdef CONFIG_IP_MULTICAST
1459 static void sf_markstate(struct ip_mc_list *pmc)
1461 struct ip_sf_list *psf;
1462 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1464 for (psf=pmc->sources; psf; psf=psf->sf_next)
1465 if (pmc->sfcount[MCAST_EXCLUDE]) {
1466 psf->sf_oldin = mca_xcount ==
1467 psf->sf_count[MCAST_EXCLUDE] &&
1468 !psf->sf_count[MCAST_INCLUDE];
1469 } else
1470 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1473 static int sf_setstate(struct ip_mc_list *pmc)
1475 struct ip_sf_list *psf;
1476 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1477 int qrv = pmc->interface->mr_qrv;
1478 int new_in, rv;
1480 rv = 0;
1481 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1482 if (pmc->sfcount[MCAST_EXCLUDE]) {
1483 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1484 !psf->sf_count[MCAST_INCLUDE];
1485 } else
1486 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1487 if (new_in != psf->sf_oldin) {
1488 psf->sf_crcount = qrv;
1489 rv++;
1492 return rv;
1494 #endif
1497 * Add multicast source filter list to the interface list
1499 int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1500 int sfcount, __u32 *psfsrc, int delta)
1502 struct ip_mc_list *pmc;
1503 int isexclude;
1504 int i, err;
1506 if (!in_dev)
1507 return -ENODEV;
1508 read_lock(&in_dev->lock);
1509 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1510 if (*pmca == pmc->multiaddr)
1511 break;
1513 if (!pmc) {
1514 /* MCA not found?? bug */
1515 read_unlock(&in_dev->lock);
1516 return -ESRCH;
1518 spin_lock_bh(&pmc->lock);
1519 read_unlock(&in_dev->lock);
1521 #ifdef CONFIG_IP_MULTICAST
1522 sf_markstate(pmc);
1523 #endif
1524 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1525 if (!delta)
1526 pmc->sfcount[sfmode]++;
1527 err = 0;
1528 for (i=0; i<sfcount; i++) {
1529 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1530 if (err)
1531 break;
1533 if (err) {
1534 int j;
1536 pmc->sfcount[sfmode]--;
1537 for (j=0; j<i; j++)
1538 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1539 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1540 #ifdef CONFIG_IP_MULTICAST
1541 struct in_device *in_dev = pmc->interface;
1542 struct ip_sf_list *psf;
1543 #endif
1545 /* filter mode change */
1546 if (pmc->sfcount[MCAST_EXCLUDE])
1547 pmc->sfmode = MCAST_EXCLUDE;
1548 else if (pmc->sfcount[MCAST_INCLUDE])
1549 pmc->sfmode = MCAST_INCLUDE;
1550 #ifdef CONFIG_IP_MULTICAST
1551 /* else no filters; keep old mode for reports */
1553 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1554 IGMP_Unsolicited_Report_Count;
1555 in_dev->mr_ifc_count = pmc->crcount;
1556 for (psf=pmc->sources; psf; psf = psf->sf_next)
1557 psf->sf_crcount = 0;
1558 igmp_ifc_event(in_dev);
1559 } else if (sf_setstate(pmc)) {
1560 igmp_ifc_event(in_dev);
1561 #endif
1563 spin_unlock_bh(&pmc->lock);
1564 return err;
1567 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1569 struct ip_sf_list *psf, *nextpsf;
1571 for (psf=pmc->tomb; psf; psf=nextpsf) {
1572 nextpsf = psf->sf_next;
1573 kfree(psf);
1575 pmc->tomb = 0;
1576 for (psf=pmc->sources; psf; psf=nextpsf) {
1577 nextpsf = psf->sf_next;
1578 kfree(psf);
1580 pmc->sources = 0;
1581 pmc->sfmode = MCAST_EXCLUDE;
1582 pmc->sfcount[MCAST_EXCLUDE] = 0;
1583 pmc->sfcount[MCAST_EXCLUDE] = 1;
1588 * Join a multicast group
1590 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1592 int err;
1593 u32 addr = imr->imr_multiaddr.s_addr;
1594 struct ip_mc_socklist *iml, *i;
1595 struct in_device *in_dev;
1596 struct inet_opt *inet = inet_sk(sk);
1597 int count = 0;
1599 if (!MULTICAST(addr))
1600 return -EINVAL;
1602 rtnl_shlock();
1604 in_dev = ip_mc_find_dev(imr);
1606 if (!in_dev) {
1607 iml = NULL;
1608 err = -ENODEV;
1609 goto done;
1612 iml = (struct ip_mc_socklist *)sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1614 err = -EADDRINUSE;
1615 for (i = inet->mc_list; i; i = i->next) {
1616 if (memcmp(&i->multi, imr, sizeof(*imr)) == 0) {
1617 /* New style additions are reference counted */
1618 if (imr->imr_address.s_addr == 0) {
1619 i->count++;
1620 err = 0;
1622 goto done;
1624 count++;
1626 err = -ENOBUFS;
1627 if (iml == NULL || count >= sysctl_igmp_max_memberships)
1628 goto done;
1629 memcpy(&iml->multi, imr, sizeof(*imr));
1630 iml->next = inet->mc_list;
1631 iml->count = 1;
1632 iml->sflist = NULL;
1633 iml->sfmode = MCAST_EXCLUDE;
1634 inet->mc_list = iml;
1635 ip_mc_inc_group(in_dev, addr);
1636 iml = NULL;
1637 err = 0;
1639 done:
1640 rtnl_shunlock();
1641 if (iml)
1642 sock_kfree_s(sk, iml, sizeof(*iml));
1643 return err;
1646 int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1647 struct in_device *in_dev)
1649 int err;
1651 if (iml->sflist == 0) {
1652 /* any-source empty exclude case */
1653 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1654 iml->sfmode, 0, 0, 0);
1656 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1657 iml->sfmode, iml->sflist->sl_count,
1658 iml->sflist->sl_addr, 0);
1659 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1660 iml->sflist = 0;
1661 return err;
1665 * Ask a socket to leave a group.
1668 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1670 struct inet_opt *inet = inet_sk(sk);
1671 struct ip_mc_socklist *iml, **imlp;
1673 rtnl_lock();
1674 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1675 if (iml->multi.imr_multiaddr.s_addr==imr->imr_multiaddr.s_addr &&
1676 iml->multi.imr_address.s_addr==imr->imr_address.s_addr &&
1677 (!imr->imr_ifindex || iml->multi.imr_ifindex==imr->imr_ifindex)) {
1678 struct in_device *in_dev;
1680 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
1681 if (in_dev)
1682 (void) ip_mc_leave_src(sk, iml, in_dev);
1683 if (--iml->count) {
1684 rtnl_unlock();
1685 if (in_dev)
1686 in_dev_put(in_dev);
1687 return 0;
1690 *imlp = iml->next;
1692 if (in_dev) {
1693 ip_mc_dec_group(in_dev, imr->imr_multiaddr.s_addr);
1694 in_dev_put(in_dev);
1696 rtnl_unlock();
1697 sock_kfree_s(sk, iml, sizeof(*iml));
1698 return 0;
1701 rtnl_unlock();
1702 return -EADDRNOTAVAIL;
1705 int ip_mc_source(int add, int omode, struct sock *sk, struct
1706 ip_mreq_source *mreqs, int ifindex)
1708 int err;
1709 struct ip_mreqn imr;
1710 u32 addr = mreqs->imr_multiaddr;
1711 struct ip_mc_socklist *pmc;
1712 struct in_device *in_dev = 0;
1713 struct inet_opt *inet = inet_sk(sk);
1714 struct ip_sf_socklist *psl;
1715 int i, j, rv;
1717 if (!MULTICAST(addr))
1718 return -EINVAL;
1720 rtnl_shlock();
1722 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1723 imr.imr_address.s_addr = mreqs->imr_interface;
1724 imr.imr_ifindex = ifindex;
1725 in_dev = ip_mc_find_dev(&imr);
1727 if (!in_dev) {
1728 err = -ENODEV;
1729 goto done;
1731 err = -EADDRNOTAVAIL;
1733 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1734 if (memcmp(&pmc->multi, mreqs, 2*sizeof(__u32)) == 0)
1735 break;
1737 if (!pmc) /* must have a prior join */
1738 goto done;
1739 /* if a source filter was set, must be the same mode as before */
1740 if (pmc->sflist) {
1741 if (pmc->sfmode != omode)
1742 goto done;
1743 } else if (pmc->sfmode != omode) {
1744 /* allow mode switches for empty-set filters */
1745 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1746 0, 0);
1747 pmc->sfmode = omode;
1748 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1749 0, 0);
1752 psl = pmc->sflist;
1753 if (!add) {
1754 if (!psl)
1755 goto done;
1756 rv = !0;
1757 for (i=0; i<psl->sl_count; i++) {
1758 rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1759 sizeof(__u32));
1760 if (rv >= 0)
1761 break;
1763 if (!rv) /* source not found */
1764 goto done;
1766 /* update the interface filter */
1767 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1768 &mreqs->imr_sourceaddr, 1);
1770 for (j=i+1; j<psl->sl_count; j++)
1771 psl->sl_addr[j-1] = psl->sl_addr[j];
1772 psl->sl_count--;
1773 err = 0;
1774 goto done;
1776 /* else, add a new source to the filter */
1778 if (!psl || psl->sl_count == psl->sl_max) {
1779 struct ip_sf_socklist *newpsl;
1780 int count = IP_SFBLOCK;
1782 if (psl)
1783 count += psl->sl_max;
1784 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1785 IP_SFLSIZE(count), GFP_KERNEL);
1786 if (!newpsl) {
1787 err = -ENOBUFS;
1788 goto done;
1790 newpsl->sl_max = count;
1791 newpsl->sl_count = count - IP_SFBLOCK;
1792 if (psl) {
1793 for (i=0; i<psl->sl_count; i++)
1794 newpsl->sl_addr[i] = psl->sl_addr[i];
1795 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1797 pmc->sflist = psl = newpsl;
1799 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1800 for (i=0; i<psl->sl_count; i++) {
1801 rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1802 sizeof(__u32));
1803 if (rv >= 0)
1804 break;
1806 if (rv == 0) /* address already there is an error */
1807 goto done;
1808 for (j=psl->sl_count-1; j>=i; j--)
1809 psl->sl_addr[j+1] = psl->sl_addr[j];
1810 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1811 psl->sl_count++;
1812 err = 0;
1813 /* update the interface list */
1814 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1815 &mreqs->imr_sourceaddr, 1);
1816 done:
1817 rtnl_shunlock();
1818 return err;
1821 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1823 int err;
1824 struct ip_mreqn imr;
1825 u32 addr = msf->imsf_multiaddr;
1826 struct ip_mc_socklist *pmc;
1827 struct in_device *in_dev;
1828 struct inet_opt *inet = inet_sk(sk);
1829 struct ip_sf_socklist *newpsl, *psl;
1831 if (!MULTICAST(addr))
1832 return -EINVAL;
1833 if (msf->imsf_fmode != MCAST_INCLUDE &&
1834 msf->imsf_fmode != MCAST_EXCLUDE)
1835 return -EINVAL;
1837 rtnl_shlock();
1839 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1840 imr.imr_address.s_addr = msf->imsf_interface;
1841 imr.imr_ifindex = ifindex;
1842 in_dev = ip_mc_find_dev(&imr);
1844 if (!in_dev) {
1845 err = -ENODEV;
1846 goto done;
1848 err = -EADDRNOTAVAIL;
1850 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1851 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1852 pmc->multi.imr_ifindex == imr.imr_ifindex)
1853 break;
1855 if (!pmc) /* must have a prior join */
1856 goto done;
1857 if (msf->imsf_numsrc) {
1858 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1859 IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1860 if (!newpsl) {
1861 err = -ENOBUFS;
1862 goto done;
1864 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1865 memcpy(newpsl->sl_addr, msf->imsf_slist,
1866 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1867 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1868 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1869 if (err) {
1870 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1871 goto done;
1873 } else
1874 newpsl = 0;
1875 psl = pmc->sflist;
1876 if (psl) {
1877 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1878 psl->sl_count, psl->sl_addr, 0);
1879 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1880 } else
1881 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1882 0, 0, 0);
1883 pmc->sflist = newpsl;
1884 pmc->sfmode = msf->imsf_fmode;
1885 done:
1886 rtnl_shunlock();
1887 return err;
1890 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1891 struct ip_msfilter *optval, int *optlen)
1893 int err, len, count, copycount;
1894 struct ip_mreqn imr;
1895 u32 addr = msf->imsf_multiaddr;
1896 struct ip_mc_socklist *pmc;
1897 struct in_device *in_dev;
1898 struct inet_opt *inet = inet_sk(sk);
1899 struct ip_sf_socklist *psl;
1901 if (!MULTICAST(addr))
1902 return -EINVAL;
1904 rtnl_shlock();
1906 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1907 imr.imr_address.s_addr = msf->imsf_interface;
1908 imr.imr_ifindex = 0;
1909 in_dev = ip_mc_find_dev(&imr);
1911 if (!in_dev) {
1912 err = -ENODEV;
1913 goto done;
1915 err = -EADDRNOTAVAIL;
1917 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1918 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1919 pmc->multi.imr_ifindex == imr.imr_ifindex)
1920 break;
1922 if (!pmc) /* must have a prior join */
1923 goto done;
1924 msf->imsf_fmode = pmc->sfmode;
1925 psl = pmc->sflist;
1926 rtnl_shunlock();
1927 if (!psl) {
1928 len = 0;
1929 count = 0;
1930 } else {
1931 count = psl->sl_count;
1933 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1934 len = copycount * sizeof(psl->sl_addr[0]);
1935 msf->imsf_numsrc = count;
1936 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1937 copy_to_user((void *)optval, msf, IP_MSFILTER_SIZE(0))) {
1938 return -EFAULT;
1940 if (len &&
1941 copy_to_user((void *)&optval->imsf_slist[0], psl->sl_addr, len))
1942 return -EFAULT;
1943 return 0;
1944 done:
1945 rtnl_shunlock();
1946 return err;
1949 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
1950 struct group_filter *optval, int *optlen)
1952 int err, i, count, copycount;
1953 struct sockaddr_in *psin;
1954 u32 addr;
1955 struct ip_mc_socklist *pmc;
1956 struct inet_opt *inet = inet_sk(sk);
1957 struct ip_sf_socklist *psl;
1959 psin = (struct sockaddr_in *)&gsf->gf_group;
1960 if (psin->sin_family != AF_INET)
1961 return -EINVAL;
1962 addr = psin->sin_addr.s_addr;
1963 if (!MULTICAST(addr))
1964 return -EINVAL;
1966 rtnl_shlock();
1968 err = -EADDRNOTAVAIL;
1970 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1971 if (pmc->multi.imr_multiaddr.s_addr == addr &&
1972 pmc->multi.imr_ifindex == gsf->gf_interface)
1973 break;
1975 if (!pmc) /* must have a prior join */
1976 goto done;
1977 gsf->gf_fmode = pmc->sfmode;
1978 psl = pmc->sflist;
1979 rtnl_shunlock();
1980 count = psl ? psl->sl_count : 0;
1981 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
1982 gsf->gf_numsrc = count;
1983 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
1984 copy_to_user((void *)optval, gsf, GROUP_FILTER_SIZE(0))) {
1985 return -EFAULT;
1987 for (i=0; i<copycount; i++) {
1988 struct sockaddr_in *psin;
1989 struct sockaddr_storage ss;
1991 psin = (struct sockaddr_in *)&ss;
1992 memset(&ss, 0, sizeof(ss));
1993 psin->sin_family = AF_INET;
1994 psin->sin_addr.s_addr = psl->sl_addr[i];
1995 if (copy_to_user((void *)&optval->gf_slist[i], &ss, sizeof(ss)))
1996 return -EFAULT;
1998 return 0;
1999 done:
2000 rtnl_shunlock();
2001 return err;
2005 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2007 int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2009 struct inet_opt *inet = inet_sk(sk);
2010 struct ip_mc_socklist *pmc;
2011 struct ip_sf_socklist *psl;
2012 int i;
2014 if (!MULTICAST(loc_addr))
2015 return 1;
2017 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2018 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2019 pmc->multi.imr_ifindex == dif)
2020 break;
2022 if (!pmc)
2023 return 1;
2024 psl = pmc->sflist;
2025 if (!psl)
2026 return pmc->sfmode == MCAST_EXCLUDE;
2028 for (i=0; i<psl->sl_count; i++) {
2029 if (psl->sl_addr[i] == rmt_addr)
2030 break;
2032 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2033 return 0;
2034 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2035 return 0;
2036 return 1;
2040 * A socket is closing.
2043 void ip_mc_drop_socket(struct sock *sk)
2045 struct inet_opt *inet = inet_sk(sk);
2046 struct ip_mc_socklist *iml;
2048 if (inet->mc_list == NULL)
2049 return;
2051 rtnl_lock();
2052 while ((iml = inet->mc_list) != NULL) {
2053 struct in_device *in_dev;
2054 inet->mc_list = iml->next;
2056 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2057 (void) ip_mc_leave_src(sk, iml, in_dev);
2058 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2059 in_dev_put(in_dev);
2061 sock_kfree_s(sk, iml, sizeof(*iml));
2064 rtnl_unlock();
2067 int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2069 struct ip_mc_list *im;
2070 struct ip_sf_list *psf;
2071 int rv = 0;
2073 read_lock(&in_dev->lock);
2074 for (im=in_dev->mc_list; im; im=im->next) {
2075 if (im->multiaddr == mc_addr)
2076 break;
2078 if (im && proto == IPPROTO_IGMP) {
2079 rv = 1;
2080 } else if (im) {
2081 for (psf=im->sources; psf; psf=psf->sf_next) {
2082 if (psf->sf_inaddr == src_addr)
2083 break;
2085 if (psf)
2086 rv = psf->sf_count[MCAST_INCLUDE] ||
2087 psf->sf_count[MCAST_EXCLUDE] !=
2088 im->sfcount[MCAST_EXCLUDE];
2089 else
2090 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2092 read_unlock(&in_dev->lock);
2093 return rv;
2096 #if defined(CONFIG_PROC_FS)
2097 struct igmp_mc_iter_state {
2098 struct net_device *dev;
2099 struct in_device *in_dev;
2102 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)&seq->private)
2104 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2106 struct ip_mc_list *im = NULL;
2107 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2109 for (state->dev = dev_base, state->in_dev = NULL;
2110 state->dev;
2111 state->dev = state->dev->next) {
2112 struct in_device *in_dev;
2113 in_dev = in_dev_get(state->dev);
2114 if (!in_dev)
2115 continue;
2116 read_lock(&in_dev->lock);
2117 im = in_dev->mc_list;
2118 if (im) {
2119 state->in_dev = in_dev;
2120 break;
2122 read_unlock(&in_dev->lock);
2124 return im;
2127 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2129 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2130 im = im->next;
2131 while (!im) {
2132 if (likely(state->in_dev != NULL)) {
2133 read_unlock(&state->in_dev->lock);
2134 in_dev_put(state->in_dev);
2136 state->dev = state->dev->next;
2137 if (!state->dev) {
2138 state->in_dev = NULL;
2139 break;
2141 state->in_dev = in_dev_get(state->dev);
2142 if (!state->in_dev)
2143 continue;
2144 read_lock(&state->in_dev->lock);
2145 im = state->in_dev->mc_list;
2147 return im;
2150 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2152 struct ip_mc_list *im = igmp_mc_get_first(seq);
2153 if (im)
2154 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2155 --pos;
2156 return pos ? NULL : im;
2159 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2161 read_lock(&dev_base_lock);
2162 return *pos ? igmp_mc_get_idx(seq, *pos) : (void *)1;
2165 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2167 struct ip_mc_list *im;
2168 if (v == (void *)1)
2169 im = igmp_mc_get_first(seq);
2170 else
2171 im = igmp_mc_get_next(seq, v);
2172 ++*pos;
2173 return im;
2176 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2178 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2179 if (likely(state->in_dev != NULL)) {
2180 read_unlock(&state->in_dev->lock);
2181 in_dev_put(state->in_dev);
2183 read_unlock(&dev_base_lock);
2186 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2188 if (v == (void *)1)
2189 seq_printf(seq,
2190 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2191 else {
2192 struct ip_mc_list *im = (struct ip_mc_list *)v;
2193 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2194 char *querier;
2195 #ifdef CONFIG_IP_MULTICAST
2196 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : "V2";
2197 #else
2198 querier = "NONE";
2199 #endif
2201 if (state->in_dev->mc_list == im) {
2202 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2203 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2206 seq_printf(seq,
2207 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2208 im->multiaddr, im->users,
2209 im->tm_running, im->timer.expires-jiffies, im->reporter);
2211 return 0;
2214 static struct seq_operations igmp_mc_seq_ops = {
2215 .start = igmp_mc_seq_start,
2216 .next = igmp_mc_seq_next,
2217 .stop = igmp_mc_seq_stop,
2218 .show = igmp_mc_seq_show,
2221 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2223 struct seq_file *seq;
2224 int rc = -ENOMEM;
2225 struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2227 if (!s)
2228 goto out;
2229 rc = seq_open(file, &igmp_mc_seq_ops);
2230 if (rc)
2231 goto out_kfree;
2233 seq = file->private_data;
2234 seq->private = s;
2235 memset(s, 0, sizeof(*s));
2236 out:
2237 return rc;
2238 out_kfree:
2239 kfree(s);
2240 goto out;
2243 static struct file_operations igmp_mc_seq_fops = {
2244 .owner = THIS_MODULE,
2245 .open = igmp_mc_seq_open,
2246 .read = seq_read,
2247 .llseek = seq_lseek,
2248 .release = seq_release_private,
2251 struct igmp_mcf_iter_state {
2252 struct net_device *dev;
2253 struct in_device *idev;
2254 struct ip_mc_list *im;
2257 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)&seq->private)
2259 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2261 struct ip_sf_list *psf = NULL;
2262 struct ip_mc_list *im = NULL;
2263 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2265 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2266 state->dev;
2267 state->dev = state->dev->next) {
2268 struct in_device *idev;
2269 idev = in_dev_get(state->dev);
2270 if (unlikely(idev == NULL))
2271 continue;
2272 read_lock_bh(&idev->lock);
2273 im = idev->mc_list;
2274 if (likely(im != NULL)) {
2275 spin_lock_bh(&im->lock);
2276 psf = im->sources;
2277 if (likely(psf != NULL)) {
2278 state->im = im;
2279 state->idev = idev;
2280 break;
2282 spin_unlock_bh(&im->lock);
2284 read_unlock_bh(&idev->lock);
2286 return psf;
2289 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2291 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2293 psf = psf->sf_next;
2294 while (!psf) {
2295 spin_unlock_bh(&state->im->lock);
2296 state->im = state->im->next;
2297 while (!state->im) {
2298 if (likely(state->idev != NULL)) {
2299 read_unlock_bh(&state->idev->lock);
2300 in_dev_put(state->idev);
2302 state->dev = state->dev->next;
2303 if (!state->dev) {
2304 state->idev = NULL;
2305 goto out;
2307 state->idev = in_dev_get(state->dev);
2308 if (!state->idev)
2309 continue;
2310 read_lock_bh(&state->idev->lock);
2311 state->im = state->idev->mc_list;
2313 if (!state->im)
2314 break;
2315 spin_lock_bh(&state->im->lock);
2316 psf = state->im->sources;
2318 out:
2319 return psf;
2322 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2324 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2325 if (psf)
2326 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2327 --pos;
2328 return pos ? NULL : psf;
2331 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2333 read_lock(&dev_base_lock);
2334 return *pos ? igmp_mcf_get_idx(seq, *pos) : (void *)1;
2337 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2339 struct ip_sf_list *psf;
2340 if (v == (void *)1)
2341 psf = igmp_mcf_get_first(seq);
2342 else
2343 psf = igmp_mcf_get_next(seq, v);
2344 ++*pos;
2345 return psf;
2348 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2350 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2351 if (likely(state->im != NULL))
2352 spin_unlock_bh(&state->im->lock);
2353 if (likely(state->idev != NULL)) {
2354 read_unlock_bh(&state->idev->lock);
2355 in_dev_put(state->idev);
2357 read_unlock(&dev_base_lock);
2360 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2362 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2363 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2365 if (v == (void *)1) {
2366 seq_printf(seq,
2367 "%3s %6s "
2368 "%10s %10s %6s %6s\n", "Idx",
2369 "Device", "MCA",
2370 "SRC", "INC", "EXC");
2371 } else {
2372 seq_printf(seq,
2373 "%3d %6.6s 0x%08x "
2374 "0x%08x %6lu %6lu\n",
2375 state->dev->ifindex, state->dev->name,
2376 ntohl(state->im->multiaddr),
2377 ntohl(psf->sf_inaddr),
2378 psf->sf_count[MCAST_INCLUDE],
2379 psf->sf_count[MCAST_EXCLUDE]);
2381 return 0;
2384 static struct seq_operations igmp_mcf_seq_ops = {
2385 .start = igmp_mcf_seq_start,
2386 .next = igmp_mcf_seq_next,
2387 .stop = igmp_mcf_seq_stop,
2388 .show = igmp_mcf_seq_show,
2391 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2393 struct seq_file *seq;
2394 int rc = -ENOMEM;
2395 struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2397 if (!s)
2398 goto out;
2399 rc = seq_open(file, &igmp_mcf_seq_ops);
2400 if (rc)
2401 goto out_kfree;
2403 seq = file->private_data;
2404 seq->private = s;
2405 memset(s, 0, sizeof(*s));
2406 out:
2407 return rc;
2408 out_kfree:
2409 kfree(s);
2410 goto out;
2413 static struct file_operations igmp_mcf_seq_fops = {
2414 .owner = THIS_MODULE,
2415 .open = igmp_mcf_seq_open,
2416 .read = seq_read,
2417 .llseek = seq_lseek,
2418 .release = seq_release_private,
2421 int __init igmp_mc_proc_init(void)
2423 struct proc_dir_entry *p;
2425 p = create_proc_entry("igmp", S_IRUGO, proc_net);
2426 if (p)
2427 p->proc_fops = &igmp_mc_seq_fops;
2429 p = create_proc_entry("mcfilter", S_IRUGO, proc_net);
2430 if (p)
2431 p->proc_fops = &igmp_mcf_seq_fops;
2432 return 0;
2434 #endif