Pull bugzilla-8346 into release branch
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / igmp.c
blob8cedb2a2c9dfa8832a28b8f274aea8fb3681f1ee
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
75 #include <linux/module.h>
76 #include <asm/uaccess.h>
77 #include <asm/system.h>
78 #include <linux/types.h>
79 #include <linux/kernel.h>
80 #include <linux/jiffies.h>
81 #include <linux/string.h>
82 #include <linux/socket.h>
83 #include <linux/sockios.h>
84 #include <linux/in.h>
85 #include <linux/inet.h>
86 #include <linux/netdevice.h>
87 #include <linux/skbuff.h>
88 #include <linux/inetdevice.h>
89 #include <linux/igmp.h>
90 #include <linux/if_arp.h>
91 #include <linux/rtnetlink.h>
92 #include <linux/times.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) (ipv4_devconf.force_igmp_version == 1 || \
132 (in_dev)->cnf.force_igmp_version == 1 || \
133 ((in_dev)->mr_v1_seen && \
134 time_before(jiffies, (in_dev)->mr_v1_seen)))
135 #define IGMP_V2_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 2 || \
136 (in_dev)->cnf.force_igmp_version == 2 || \
137 ((in_dev)->mr_v2_seen && \
138 time_before(jiffies, (in_dev)->mr_v2_seen)))
140 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
141 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
142 static void igmpv3_clear_delrec(struct in_device *in_dev);
143 static int sf_setstate(struct ip_mc_list *pmc);
144 static void sf_markstate(struct ip_mc_list *pmc);
145 #endif
146 static void ip_mc_clear_src(struct ip_mc_list *pmc);
147 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
148 int sfcount, __be32 *psfsrc, int delta);
150 static void ip_ma_put(struct ip_mc_list *im)
152 if (atomic_dec_and_test(&im->refcnt)) {
153 in_dev_put(im->interface);
154 kfree(im);
158 #ifdef CONFIG_IP_MULTICAST
161 * Timer management
164 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
166 spin_lock_bh(&im->lock);
167 if (del_timer(&im->timer))
168 atomic_dec(&im->refcnt);
169 im->tm_running=0;
170 im->reporter = 0;
171 im->unsolicit_count = 0;
172 spin_unlock_bh(&im->lock);
175 /* It must be called with locked im->lock */
176 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
178 int tv=net_random() % max_delay;
180 im->tm_running=1;
181 if (!mod_timer(&im->timer, jiffies+tv+2))
182 atomic_inc(&im->refcnt);
185 static void igmp_gq_start_timer(struct in_device *in_dev)
187 int tv = net_random() % in_dev->mr_maxdelay;
189 in_dev->mr_gq_running = 1;
190 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
191 in_dev_hold(in_dev);
194 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
196 int tv = net_random() % delay;
198 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
199 in_dev_hold(in_dev);
202 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
204 spin_lock_bh(&im->lock);
205 im->unsolicit_count = 0;
206 if (del_timer(&im->timer)) {
207 if ((long)(im->timer.expires-jiffies) < max_delay) {
208 add_timer(&im->timer);
209 im->tm_running=1;
210 spin_unlock_bh(&im->lock);
211 return;
213 atomic_dec(&im->refcnt);
215 igmp_start_timer(im, max_delay);
216 spin_unlock_bh(&im->lock);
221 * Send an IGMP report.
224 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
227 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
228 int gdeleted, int sdeleted)
230 switch (type) {
231 case IGMPV3_MODE_IS_INCLUDE:
232 case IGMPV3_MODE_IS_EXCLUDE:
233 if (gdeleted || sdeleted)
234 return 0;
235 if (!(pmc->gsquery && !psf->sf_gsresp)) {
236 if (pmc->sfmode == MCAST_INCLUDE)
237 return 1;
238 /* don't include if this source is excluded
239 * in all filters
241 if (psf->sf_count[MCAST_INCLUDE])
242 return type == IGMPV3_MODE_IS_INCLUDE;
243 return pmc->sfcount[MCAST_EXCLUDE] ==
244 psf->sf_count[MCAST_EXCLUDE];
246 return 0;
247 case IGMPV3_CHANGE_TO_INCLUDE:
248 if (gdeleted || sdeleted)
249 return 0;
250 return psf->sf_count[MCAST_INCLUDE] != 0;
251 case IGMPV3_CHANGE_TO_EXCLUDE:
252 if (gdeleted || sdeleted)
253 return 0;
254 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
255 psf->sf_count[MCAST_INCLUDE])
256 return 0;
257 return pmc->sfcount[MCAST_EXCLUDE] ==
258 psf->sf_count[MCAST_EXCLUDE];
259 case IGMPV3_ALLOW_NEW_SOURCES:
260 if (gdeleted || !psf->sf_crcount)
261 return 0;
262 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
263 case IGMPV3_BLOCK_OLD_SOURCES:
264 if (pmc->sfmode == MCAST_INCLUDE)
265 return gdeleted || (psf->sf_crcount && sdeleted);
266 return psf->sf_crcount && !gdeleted && !sdeleted;
268 return 0;
271 static int
272 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
274 struct ip_sf_list *psf;
275 int scount = 0;
277 for (psf=pmc->sources; psf; psf=psf->sf_next) {
278 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
279 continue;
280 scount++;
282 return scount;
285 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
287 struct sk_buff *skb;
288 struct rtable *rt;
289 struct iphdr *pip;
290 struct igmpv3_report *pig;
292 skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
293 if (skb == NULL)
294 return NULL;
297 struct flowi fl = { .oif = dev->ifindex,
298 .nl_u = { .ip4_u = {
299 .daddr = IGMPV3_ALL_MCR } },
300 .proto = IPPROTO_IGMP };
301 if (ip_route_output_key(&rt, &fl)) {
302 kfree_skb(skb);
303 return NULL;
306 if (rt->rt_src == 0) {
307 kfree_skb(skb);
308 ip_rt_put(rt);
309 return NULL;
312 skb->dst = &rt->u.dst;
313 skb->dev = dev;
315 skb_reserve(skb, LL_RESERVED_SPACE(dev));
317 skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
319 pip->version = 4;
320 pip->ihl = (sizeof(struct iphdr)+4)>>2;
321 pip->tos = 0xc0;
322 pip->frag_off = htons(IP_DF);
323 pip->ttl = 1;
324 pip->daddr = rt->rt_dst;
325 pip->saddr = rt->rt_src;
326 pip->protocol = IPPROTO_IGMP;
327 pip->tot_len = 0; /* filled in later */
328 ip_select_ident(pip, &rt->u.dst, NULL);
329 ((u8*)&pip[1])[0] = IPOPT_RA;
330 ((u8*)&pip[1])[1] = 4;
331 ((u8*)&pip[1])[2] = 0;
332 ((u8*)&pip[1])[3] = 0;
334 pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
335 skb->h.igmph = (struct igmphdr *)pig;
336 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
337 pig->resv1 = 0;
338 pig->csum = 0;
339 pig->resv2 = 0;
340 pig->ngrec = 0;
341 return skb;
344 static int igmpv3_sendpack(struct sk_buff *skb)
346 struct iphdr *pip = skb->nh.iph;
347 struct igmphdr *pig = skb->h.igmph;
348 int iplen, igmplen;
350 iplen = skb->tail - (unsigned char *)skb->nh.iph;
351 pip->tot_len = htons(iplen);
352 ip_send_check(pip);
354 igmplen = skb->tail - (unsigned char *)skb->h.igmph;
355 pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
357 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
358 dst_output);
361 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
363 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
366 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
367 int type, struct igmpv3_grec **ppgr)
369 struct net_device *dev = pmc->interface->dev;
370 struct igmpv3_report *pih;
371 struct igmpv3_grec *pgr;
373 if (!skb)
374 skb = igmpv3_newpack(dev, dev->mtu);
375 if (!skb)
376 return NULL;
377 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
378 pgr->grec_type = type;
379 pgr->grec_auxwords = 0;
380 pgr->grec_nsrcs = 0;
381 pgr->grec_mca = pmc->multiaddr;
382 pih = (struct igmpv3_report *)skb->h.igmph;
383 pih->ngrec = htons(ntohs(pih->ngrec)+1);
384 *ppgr = pgr;
385 return skb;
388 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
389 skb_tailroom(skb)) : 0)
391 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
392 int type, int gdeleted, int sdeleted)
394 struct net_device *dev = pmc->interface->dev;
395 struct igmpv3_report *pih;
396 struct igmpv3_grec *pgr = NULL;
397 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
398 int scount, stotal, first, isquery, truncate;
400 if (pmc->multiaddr == IGMP_ALL_HOSTS)
401 return skb;
403 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
404 type == IGMPV3_MODE_IS_EXCLUDE;
405 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
406 type == IGMPV3_CHANGE_TO_EXCLUDE;
408 stotal = scount = 0;
410 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
412 if (!*psf_list)
413 goto empty_source;
415 pih = skb ? (struct igmpv3_report *)skb->h.igmph : NULL;
417 /* EX and TO_EX get a fresh packet, if needed */
418 if (truncate) {
419 if (pih && pih->ngrec &&
420 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
421 if (skb)
422 igmpv3_sendpack(skb);
423 skb = igmpv3_newpack(dev, dev->mtu);
426 first = 1;
427 psf_prev = NULL;
428 for (psf=*psf_list; psf; psf=psf_next) {
429 __be32 *psrc;
431 psf_next = psf->sf_next;
433 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
434 psf_prev = psf;
435 continue;
438 /* clear marks on query responses */
439 if (isquery)
440 psf->sf_gsresp = 0;
442 if (AVAILABLE(skb) < sizeof(__be32) +
443 first*sizeof(struct igmpv3_grec)) {
444 if (truncate && !first)
445 break; /* truncate these */
446 if (pgr)
447 pgr->grec_nsrcs = htons(scount);
448 if (skb)
449 igmpv3_sendpack(skb);
450 skb = igmpv3_newpack(dev, dev->mtu);
451 first = 1;
452 scount = 0;
454 if (first) {
455 skb = add_grhead(skb, pmc, type, &pgr);
456 first = 0;
458 if (!skb)
459 return NULL;
460 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
461 *psrc = psf->sf_inaddr;
462 scount++; stotal++;
463 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
464 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
465 psf->sf_crcount--;
466 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
467 if (psf_prev)
468 psf_prev->sf_next = psf->sf_next;
469 else
470 *psf_list = psf->sf_next;
471 kfree(psf);
472 continue;
475 psf_prev = psf;
478 empty_source:
479 if (!stotal) {
480 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
481 type == IGMPV3_BLOCK_OLD_SOURCES)
482 return skb;
483 if (pmc->crcount || isquery) {
484 /* make sure we have room for group header */
485 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
486 igmpv3_sendpack(skb);
487 skb = NULL; /* add_grhead will get a new one */
489 skb = add_grhead(skb, pmc, type, &pgr);
492 if (pgr)
493 pgr->grec_nsrcs = htons(scount);
495 if (isquery)
496 pmc->gsquery = 0; /* clear query state on report */
497 return skb;
500 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
502 struct sk_buff *skb = NULL;
503 int type;
505 if (!pmc) {
506 read_lock(&in_dev->mc_list_lock);
507 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
508 if (pmc->multiaddr == IGMP_ALL_HOSTS)
509 continue;
510 spin_lock_bh(&pmc->lock);
511 if (pmc->sfcount[MCAST_EXCLUDE])
512 type = IGMPV3_MODE_IS_EXCLUDE;
513 else
514 type = IGMPV3_MODE_IS_INCLUDE;
515 skb = add_grec(skb, pmc, type, 0, 0);
516 spin_unlock_bh(&pmc->lock);
518 read_unlock(&in_dev->mc_list_lock);
519 } else {
520 spin_lock_bh(&pmc->lock);
521 if (pmc->sfcount[MCAST_EXCLUDE])
522 type = IGMPV3_MODE_IS_EXCLUDE;
523 else
524 type = IGMPV3_MODE_IS_INCLUDE;
525 skb = add_grec(skb, pmc, type, 0, 0);
526 spin_unlock_bh(&pmc->lock);
528 if (!skb)
529 return 0;
530 return igmpv3_sendpack(skb);
534 * remove zero-count source records from a source filter list
536 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
538 struct ip_sf_list *psf_prev, *psf_next, *psf;
540 psf_prev = NULL;
541 for (psf=*ppsf; psf; psf = psf_next) {
542 psf_next = psf->sf_next;
543 if (psf->sf_crcount == 0) {
544 if (psf_prev)
545 psf_prev->sf_next = psf->sf_next;
546 else
547 *ppsf = psf->sf_next;
548 kfree(psf);
549 } else
550 psf_prev = psf;
554 static void igmpv3_send_cr(struct in_device *in_dev)
556 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
557 struct sk_buff *skb = NULL;
558 int type, dtype;
560 read_lock(&in_dev->mc_list_lock);
561 spin_lock_bh(&in_dev->mc_tomb_lock);
563 /* deleted MCA's */
564 pmc_prev = NULL;
565 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
566 pmc_next = pmc->next;
567 if (pmc->sfmode == MCAST_INCLUDE) {
568 type = IGMPV3_BLOCK_OLD_SOURCES;
569 dtype = IGMPV3_BLOCK_OLD_SOURCES;
570 skb = add_grec(skb, pmc, type, 1, 0);
571 skb = add_grec(skb, pmc, dtype, 1, 1);
573 if (pmc->crcount) {
574 if (pmc->sfmode == MCAST_EXCLUDE) {
575 type = IGMPV3_CHANGE_TO_INCLUDE;
576 skb = add_grec(skb, pmc, type, 1, 0);
578 pmc->crcount--;
579 if (pmc->crcount == 0) {
580 igmpv3_clear_zeros(&pmc->tomb);
581 igmpv3_clear_zeros(&pmc->sources);
584 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
585 if (pmc_prev)
586 pmc_prev->next = pmc_next;
587 else
588 in_dev->mc_tomb = pmc_next;
589 in_dev_put(pmc->interface);
590 kfree(pmc);
591 } else
592 pmc_prev = pmc;
594 spin_unlock_bh(&in_dev->mc_tomb_lock);
596 /* change recs */
597 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
598 spin_lock_bh(&pmc->lock);
599 if (pmc->sfcount[MCAST_EXCLUDE]) {
600 type = IGMPV3_BLOCK_OLD_SOURCES;
601 dtype = IGMPV3_ALLOW_NEW_SOURCES;
602 } else {
603 type = IGMPV3_ALLOW_NEW_SOURCES;
604 dtype = IGMPV3_BLOCK_OLD_SOURCES;
606 skb = add_grec(skb, pmc, type, 0, 0);
607 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
609 /* filter mode changes */
610 if (pmc->crcount) {
611 if (pmc->sfmode == MCAST_EXCLUDE)
612 type = IGMPV3_CHANGE_TO_EXCLUDE;
613 else
614 type = IGMPV3_CHANGE_TO_INCLUDE;
615 skb = add_grec(skb, pmc, type, 0, 0);
616 pmc->crcount--;
618 spin_unlock_bh(&pmc->lock);
620 read_unlock(&in_dev->mc_list_lock);
622 if (!skb)
623 return;
624 (void) igmpv3_sendpack(skb);
627 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
628 int type)
630 struct sk_buff *skb;
631 struct iphdr *iph;
632 struct igmphdr *ih;
633 struct rtable *rt;
634 struct net_device *dev = in_dev->dev;
635 __be32 group = pmc ? pmc->multiaddr : 0;
636 __be32 dst;
638 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
639 return igmpv3_send_report(in_dev, pmc);
640 else if (type == IGMP_HOST_LEAVE_MESSAGE)
641 dst = IGMP_ALL_ROUTER;
642 else
643 dst = group;
646 struct flowi fl = { .oif = dev->ifindex,
647 .nl_u = { .ip4_u = { .daddr = dst } },
648 .proto = IPPROTO_IGMP };
649 if (ip_route_output_key(&rt, &fl))
650 return -1;
652 if (rt->rt_src == 0) {
653 ip_rt_put(rt);
654 return -1;
657 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
658 if (skb == NULL) {
659 ip_rt_put(rt);
660 return -1;
663 skb->dst = &rt->u.dst;
665 skb_reserve(skb, LL_RESERVED_SPACE(dev));
667 skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
669 iph->version = 4;
670 iph->ihl = (sizeof(struct iphdr)+4)>>2;
671 iph->tos = 0xc0;
672 iph->frag_off = htons(IP_DF);
673 iph->ttl = 1;
674 iph->daddr = dst;
675 iph->saddr = rt->rt_src;
676 iph->protocol = IPPROTO_IGMP;
677 iph->tot_len = htons(IGMP_SIZE);
678 ip_select_ident(iph, &rt->u.dst, NULL);
679 ((u8*)&iph[1])[0] = IPOPT_RA;
680 ((u8*)&iph[1])[1] = 4;
681 ((u8*)&iph[1])[2] = 0;
682 ((u8*)&iph[1])[3] = 0;
683 ip_send_check(iph);
685 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
686 ih->type=type;
687 ih->code=0;
688 ih->csum=0;
689 ih->group=group;
690 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
692 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
693 dst_output);
696 static void igmp_gq_timer_expire(unsigned long data)
698 struct in_device *in_dev = (struct in_device *)data;
700 in_dev->mr_gq_running = 0;
701 igmpv3_send_report(in_dev, NULL);
702 __in_dev_put(in_dev);
705 static void igmp_ifc_timer_expire(unsigned long data)
707 struct in_device *in_dev = (struct in_device *)data;
709 igmpv3_send_cr(in_dev);
710 if (in_dev->mr_ifc_count) {
711 in_dev->mr_ifc_count--;
712 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
714 __in_dev_put(in_dev);
717 static void igmp_ifc_event(struct in_device *in_dev)
719 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
720 return;
721 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
722 IGMP_Unsolicited_Report_Count;
723 igmp_ifc_start_timer(in_dev, 1);
727 static void igmp_timer_expire(unsigned long data)
729 struct ip_mc_list *im=(struct ip_mc_list *)data;
730 struct in_device *in_dev = im->interface;
732 spin_lock(&im->lock);
733 im->tm_running=0;
735 if (im->unsolicit_count) {
736 im->unsolicit_count--;
737 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
739 im->reporter = 1;
740 spin_unlock(&im->lock);
742 if (IGMP_V1_SEEN(in_dev))
743 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
744 else if (IGMP_V2_SEEN(in_dev))
745 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
746 else
747 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
749 ip_ma_put(im);
752 /* mark EXCLUDE-mode sources */
753 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
755 struct ip_sf_list *psf;
756 int i, scount;
758 scount = 0;
759 for (psf=pmc->sources; psf; psf=psf->sf_next) {
760 if (scount == nsrcs)
761 break;
762 for (i=0; i<nsrcs; i++) {
763 /* skip inactive filters */
764 if (pmc->sfcount[MCAST_INCLUDE] ||
765 pmc->sfcount[MCAST_EXCLUDE] !=
766 psf->sf_count[MCAST_EXCLUDE])
767 continue;
768 if (srcs[i] == psf->sf_inaddr) {
769 scount++;
770 break;
774 pmc->gsquery = 0;
775 if (scount == nsrcs) /* all sources excluded */
776 return 0;
777 return 1;
780 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
782 struct ip_sf_list *psf;
783 int i, scount;
785 if (pmc->sfmode == MCAST_EXCLUDE)
786 return igmp_xmarksources(pmc, nsrcs, srcs);
788 /* mark INCLUDE-mode sources */
789 scount = 0;
790 for (psf=pmc->sources; psf; psf=psf->sf_next) {
791 if (scount == nsrcs)
792 break;
793 for (i=0; i<nsrcs; i++)
794 if (srcs[i] == psf->sf_inaddr) {
795 psf->sf_gsresp = 1;
796 scount++;
797 break;
800 if (!scount) {
801 pmc->gsquery = 0;
802 return 0;
804 pmc->gsquery = 1;
805 return 1;
808 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
810 struct ip_mc_list *im;
812 /* Timers are only set for non-local groups */
814 if (group == IGMP_ALL_HOSTS)
815 return;
817 read_lock(&in_dev->mc_list_lock);
818 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
819 if (im->multiaddr == group) {
820 igmp_stop_timer(im);
821 break;
824 read_unlock(&in_dev->mc_list_lock);
827 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
828 int len)
830 struct igmphdr *ih = skb->h.igmph;
831 struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
832 struct ip_mc_list *im;
833 __be32 group = ih->group;
834 int max_delay;
835 int mark = 0;
838 if (len == 8) {
839 if (ih->code == 0) {
840 /* Alas, old v1 router presents here. */
842 max_delay = IGMP_Query_Response_Interval;
843 in_dev->mr_v1_seen = jiffies +
844 IGMP_V1_Router_Present_Timeout;
845 group = 0;
846 } else {
847 /* v2 router present */
848 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
849 in_dev->mr_v2_seen = jiffies +
850 IGMP_V2_Router_Present_Timeout;
852 /* cancel the interface change timer */
853 in_dev->mr_ifc_count = 0;
854 if (del_timer(&in_dev->mr_ifc_timer))
855 __in_dev_put(in_dev);
856 /* clear deleted report items */
857 igmpv3_clear_delrec(in_dev);
858 } else if (len < 12) {
859 return; /* ignore bogus packet; freed by caller */
860 } else { /* v3 */
861 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
862 return;
864 ih3 = (struct igmpv3_query *) skb->h.raw;
865 if (ih3->nsrcs) {
866 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
867 + ntohs(ih3->nsrcs)*sizeof(__be32)))
868 return;
869 ih3 = (struct igmpv3_query *) skb->h.raw;
872 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
873 if (!max_delay)
874 max_delay = 1; /* can't mod w/ 0 */
875 in_dev->mr_maxdelay = max_delay;
876 if (ih3->qrv)
877 in_dev->mr_qrv = ih3->qrv;
878 if (!group) { /* general query */
879 if (ih3->nsrcs)
880 return; /* no sources allowed */
881 igmp_gq_start_timer(in_dev);
882 return;
884 /* mark sources to include, if group & source-specific */
885 mark = ih3->nsrcs != 0;
889 * - Start the timers in all of our membership records
890 * that the query applies to for the interface on
891 * which the query arrived excl. those that belong
892 * to a "local" group (224.0.0.X)
893 * - For timers already running check if they need to
894 * be reset.
895 * - Use the igmp->igmp_code field as the maximum
896 * delay possible
898 read_lock(&in_dev->mc_list_lock);
899 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
900 int changed;
902 if (group && group != im->multiaddr)
903 continue;
904 if (im->multiaddr == IGMP_ALL_HOSTS)
905 continue;
906 spin_lock_bh(&im->lock);
907 if (im->tm_running)
908 im->gsquery = im->gsquery && mark;
909 else
910 im->gsquery = mark;
911 changed = !im->gsquery ||
912 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
913 spin_unlock_bh(&im->lock);
914 if (changed)
915 igmp_mod_timer(im, max_delay);
917 read_unlock(&in_dev->mc_list_lock);
920 int igmp_rcv(struct sk_buff *skb)
922 /* This basically follows the spec line by line -- see RFC1112 */
923 struct igmphdr *ih;
924 struct in_device *in_dev = in_dev_get(skb->dev);
925 int len = skb->len;
927 if (in_dev==NULL) {
928 kfree_skb(skb);
929 return 0;
932 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
933 goto drop;
935 switch (skb->ip_summed) {
936 case CHECKSUM_COMPLETE:
937 if (!csum_fold(skb->csum))
938 break;
939 /* fall through */
940 case CHECKSUM_NONE:
941 skb->csum = 0;
942 if (__skb_checksum_complete(skb))
943 goto drop;
946 ih = skb->h.igmph;
947 switch (ih->type) {
948 case IGMP_HOST_MEMBERSHIP_QUERY:
949 igmp_heard_query(in_dev, skb, len);
950 break;
951 case IGMP_HOST_MEMBERSHIP_REPORT:
952 case IGMPV2_HOST_MEMBERSHIP_REPORT:
953 case IGMPV3_HOST_MEMBERSHIP_REPORT:
954 /* Is it our report looped back? */
955 if (((struct rtable*)skb->dst)->fl.iif == 0)
956 break;
957 /* don't rely on MC router hearing unicast reports */
958 if (skb->pkt_type == PACKET_MULTICAST ||
959 skb->pkt_type == PACKET_BROADCAST)
960 igmp_heard_report(in_dev, ih->group);
961 break;
962 case IGMP_PIM:
963 #ifdef CONFIG_IP_PIMSM_V1
964 in_dev_put(in_dev);
965 return pim_rcv_v1(skb);
966 #endif
967 case IGMP_DVMRP:
968 case IGMP_TRACE:
969 case IGMP_HOST_LEAVE_MESSAGE:
970 case IGMP_MTRACE:
971 case IGMP_MTRACE_RESP:
972 break;
973 default:
974 break;
977 drop:
978 in_dev_put(in_dev);
979 kfree_skb(skb);
980 return 0;
983 #endif
987 * Add a filter to a device
990 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
992 char buf[MAX_ADDR_LEN];
993 struct net_device *dev = in_dev->dev;
995 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
996 We will get multicast token leakage, when IFF_MULTICAST
997 is changed. This check should be done in dev->set_multicast_list
998 routine. Something sort of:
999 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1000 --ANK
1002 if (arp_mc_map(addr, buf, dev, 0) == 0)
1003 dev_mc_add(dev,buf,dev->addr_len,0);
1007 * Remove a filter from a device
1010 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1012 char buf[MAX_ADDR_LEN];
1013 struct net_device *dev = in_dev->dev;
1015 if (arp_mc_map(addr, buf, dev, 0) == 0)
1016 dev_mc_delete(dev,buf,dev->addr_len,0);
1019 #ifdef CONFIG_IP_MULTICAST
1021 * deleted ip_mc_list manipulation
1023 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1025 struct ip_mc_list *pmc;
1027 /* this is an "ip_mc_list" for convenience; only the fields below
1028 * are actually used. In particular, the refcnt and users are not
1029 * used for management of the delete list. Using the same structure
1030 * for deleted items allows change reports to use common code with
1031 * non-deleted or query-response MCA's.
1033 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1034 if (!pmc)
1035 return;
1036 spin_lock_bh(&im->lock);
1037 pmc->interface = im->interface;
1038 in_dev_hold(in_dev);
1039 pmc->multiaddr = im->multiaddr;
1040 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1041 IGMP_Unsolicited_Report_Count;
1042 pmc->sfmode = im->sfmode;
1043 if (pmc->sfmode == MCAST_INCLUDE) {
1044 struct ip_sf_list *psf;
1046 pmc->tomb = im->tomb;
1047 pmc->sources = im->sources;
1048 im->tomb = im->sources = NULL;
1049 for (psf=pmc->sources; psf; psf=psf->sf_next)
1050 psf->sf_crcount = pmc->crcount;
1052 spin_unlock_bh(&im->lock);
1054 spin_lock_bh(&in_dev->mc_tomb_lock);
1055 pmc->next = in_dev->mc_tomb;
1056 in_dev->mc_tomb = pmc;
1057 spin_unlock_bh(&in_dev->mc_tomb_lock);
1060 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1062 struct ip_mc_list *pmc, *pmc_prev;
1063 struct ip_sf_list *psf, *psf_next;
1065 spin_lock_bh(&in_dev->mc_tomb_lock);
1066 pmc_prev = NULL;
1067 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1068 if (pmc->multiaddr == multiaddr)
1069 break;
1070 pmc_prev = pmc;
1072 if (pmc) {
1073 if (pmc_prev)
1074 pmc_prev->next = pmc->next;
1075 else
1076 in_dev->mc_tomb = pmc->next;
1078 spin_unlock_bh(&in_dev->mc_tomb_lock);
1079 if (pmc) {
1080 for (psf=pmc->tomb; psf; psf=psf_next) {
1081 psf_next = psf->sf_next;
1082 kfree(psf);
1084 in_dev_put(pmc->interface);
1085 kfree(pmc);
1089 static void igmpv3_clear_delrec(struct in_device *in_dev)
1091 struct ip_mc_list *pmc, *nextpmc;
1093 spin_lock_bh(&in_dev->mc_tomb_lock);
1094 pmc = in_dev->mc_tomb;
1095 in_dev->mc_tomb = NULL;
1096 spin_unlock_bh(&in_dev->mc_tomb_lock);
1098 for (; pmc; pmc = nextpmc) {
1099 nextpmc = pmc->next;
1100 ip_mc_clear_src(pmc);
1101 in_dev_put(pmc->interface);
1102 kfree(pmc);
1104 /* clear dead sources, too */
1105 read_lock(&in_dev->mc_list_lock);
1106 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1107 struct ip_sf_list *psf, *psf_next;
1109 spin_lock_bh(&pmc->lock);
1110 psf = pmc->tomb;
1111 pmc->tomb = NULL;
1112 spin_unlock_bh(&pmc->lock);
1113 for (; psf; psf=psf_next) {
1114 psf_next = psf->sf_next;
1115 kfree(psf);
1118 read_unlock(&in_dev->mc_list_lock);
1120 #endif
1122 static void igmp_group_dropped(struct ip_mc_list *im)
1124 struct in_device *in_dev = im->interface;
1125 #ifdef CONFIG_IP_MULTICAST
1126 int reporter;
1127 #endif
1129 if (im->loaded) {
1130 im->loaded = 0;
1131 ip_mc_filter_del(in_dev, im->multiaddr);
1134 #ifdef CONFIG_IP_MULTICAST
1135 if (im->multiaddr == IGMP_ALL_HOSTS)
1136 return;
1138 reporter = im->reporter;
1139 igmp_stop_timer(im);
1141 if (!in_dev->dead) {
1142 if (IGMP_V1_SEEN(in_dev))
1143 goto done;
1144 if (IGMP_V2_SEEN(in_dev)) {
1145 if (reporter)
1146 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1147 goto done;
1149 /* IGMPv3 */
1150 igmpv3_add_delrec(in_dev, im);
1152 igmp_ifc_event(in_dev);
1154 done:
1155 #endif
1156 ip_mc_clear_src(im);
1159 static void igmp_group_added(struct ip_mc_list *im)
1161 struct in_device *in_dev = im->interface;
1163 if (im->loaded == 0) {
1164 im->loaded = 1;
1165 ip_mc_filter_add(in_dev, im->multiaddr);
1168 #ifdef CONFIG_IP_MULTICAST
1169 if (im->multiaddr == IGMP_ALL_HOSTS)
1170 return;
1172 if (in_dev->dead)
1173 return;
1174 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1175 spin_lock_bh(&im->lock);
1176 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1177 spin_unlock_bh(&im->lock);
1178 return;
1180 /* else, v3 */
1182 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1183 IGMP_Unsolicited_Report_Count;
1184 igmp_ifc_event(in_dev);
1185 #endif
1190 * Multicast list managers
1195 * A socket has joined a multicast group on device dev.
1198 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1200 struct ip_mc_list *im;
1202 ASSERT_RTNL();
1204 for (im=in_dev->mc_list; im; im=im->next) {
1205 if (im->multiaddr == addr) {
1206 im->users++;
1207 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1208 goto out;
1212 im = kmalloc(sizeof(*im), GFP_KERNEL);
1213 if (!im)
1214 goto out;
1216 im->users=1;
1217 im->interface=in_dev;
1218 in_dev_hold(in_dev);
1219 im->multiaddr=addr;
1220 /* initial mode is (EX, empty) */
1221 im->sfmode = MCAST_EXCLUDE;
1222 im->sfcount[MCAST_INCLUDE] = 0;
1223 im->sfcount[MCAST_EXCLUDE] = 1;
1224 im->sources = NULL;
1225 im->tomb = NULL;
1226 im->crcount = 0;
1227 atomic_set(&im->refcnt, 1);
1228 spin_lock_init(&im->lock);
1229 #ifdef CONFIG_IP_MULTICAST
1230 im->tm_running=0;
1231 init_timer(&im->timer);
1232 im->timer.data=(unsigned long)im;
1233 im->timer.function=&igmp_timer_expire;
1234 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1235 im->reporter = 0;
1236 im->gsquery = 0;
1237 #endif
1238 im->loaded = 0;
1239 write_lock_bh(&in_dev->mc_list_lock);
1240 im->next=in_dev->mc_list;
1241 in_dev->mc_list=im;
1242 write_unlock_bh(&in_dev->mc_list_lock);
1243 #ifdef CONFIG_IP_MULTICAST
1244 igmpv3_del_delrec(in_dev, im->multiaddr);
1245 #endif
1246 igmp_group_added(im);
1247 if (!in_dev->dead)
1248 ip_rt_multicast_event(in_dev);
1249 out:
1250 return;
1254 * Resend IGMP JOIN report; used for bonding.
1256 void ip_mc_rejoin_group(struct ip_mc_list *im)
1258 #ifdef CONFIG_IP_MULTICAST
1259 struct in_device *in_dev = im->interface;
1261 if (im->multiaddr == IGMP_ALL_HOSTS)
1262 return;
1264 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1265 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1266 return;
1268 /* else, v3 */
1269 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1270 IGMP_Unsolicited_Report_Count;
1271 igmp_ifc_event(in_dev);
1272 #endif
1276 * A socket has left a multicast group on device dev
1279 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1281 struct ip_mc_list *i, **ip;
1283 ASSERT_RTNL();
1285 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1286 if (i->multiaddr==addr) {
1287 if (--i->users == 0) {
1288 write_lock_bh(&in_dev->mc_list_lock);
1289 *ip = i->next;
1290 write_unlock_bh(&in_dev->mc_list_lock);
1291 igmp_group_dropped(i);
1293 if (!in_dev->dead)
1294 ip_rt_multicast_event(in_dev);
1296 ip_ma_put(i);
1297 return;
1299 break;
1304 /* Device going down */
1306 void ip_mc_down(struct in_device *in_dev)
1308 struct ip_mc_list *i;
1310 ASSERT_RTNL();
1312 for (i=in_dev->mc_list; i; i=i->next)
1313 igmp_group_dropped(i);
1315 #ifdef CONFIG_IP_MULTICAST
1316 in_dev->mr_ifc_count = 0;
1317 if (del_timer(&in_dev->mr_ifc_timer))
1318 __in_dev_put(in_dev);
1319 in_dev->mr_gq_running = 0;
1320 if (del_timer(&in_dev->mr_gq_timer))
1321 __in_dev_put(in_dev);
1322 igmpv3_clear_delrec(in_dev);
1323 #endif
1325 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1328 void ip_mc_init_dev(struct in_device *in_dev)
1330 ASSERT_RTNL();
1332 in_dev->mc_tomb = NULL;
1333 #ifdef CONFIG_IP_MULTICAST
1334 in_dev->mr_gq_running = 0;
1335 init_timer(&in_dev->mr_gq_timer);
1336 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1337 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1338 in_dev->mr_ifc_count = 0;
1339 init_timer(&in_dev->mr_ifc_timer);
1340 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1341 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1342 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1343 #endif
1345 rwlock_init(&in_dev->mc_list_lock);
1346 spin_lock_init(&in_dev->mc_tomb_lock);
1349 /* Device going up */
1351 void ip_mc_up(struct in_device *in_dev)
1353 struct ip_mc_list *i;
1355 ASSERT_RTNL();
1357 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1359 for (i=in_dev->mc_list; i; i=i->next)
1360 igmp_group_added(i);
1364 * Device is about to be destroyed: clean up.
1367 void ip_mc_destroy_dev(struct in_device *in_dev)
1369 struct ip_mc_list *i;
1371 ASSERT_RTNL();
1373 /* Deactivate timers */
1374 ip_mc_down(in_dev);
1376 write_lock_bh(&in_dev->mc_list_lock);
1377 while ((i = in_dev->mc_list) != NULL) {
1378 in_dev->mc_list = i->next;
1379 write_unlock_bh(&in_dev->mc_list_lock);
1381 igmp_group_dropped(i);
1382 ip_ma_put(i);
1384 write_lock_bh(&in_dev->mc_list_lock);
1386 write_unlock_bh(&in_dev->mc_list_lock);
1389 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1391 struct flowi fl = { .nl_u = { .ip4_u =
1392 { .daddr = imr->imr_multiaddr.s_addr } } };
1393 struct rtable *rt;
1394 struct net_device *dev = NULL;
1395 struct in_device *idev = NULL;
1397 if (imr->imr_ifindex) {
1398 idev = inetdev_by_index(imr->imr_ifindex);
1399 if (idev)
1400 __in_dev_put(idev);
1401 return idev;
1403 if (imr->imr_address.s_addr) {
1404 dev = ip_dev_find(imr->imr_address.s_addr);
1405 if (!dev)
1406 return NULL;
1407 dev_put(dev);
1410 if (!dev && !ip_route_output_key(&rt, &fl)) {
1411 dev = rt->u.dst.dev;
1412 ip_rt_put(rt);
1414 if (dev) {
1415 imr->imr_ifindex = dev->ifindex;
1416 idev = __in_dev_get_rtnl(dev);
1418 return idev;
1422 * Join a socket to a group
1424 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1425 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1428 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1429 __be32 *psfsrc)
1431 struct ip_sf_list *psf, *psf_prev;
1432 int rv = 0;
1434 psf_prev = NULL;
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 || psf->sf_count[sfmode] == 0) {
1441 /* source filter not found, or count wrong => bug */
1442 return -ESRCH;
1444 psf->sf_count[sfmode]--;
1445 if (psf->sf_count[sfmode] == 0) {
1446 ip_rt_multicast_event(pmc->interface);
1448 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1449 #ifdef CONFIG_IP_MULTICAST
1450 struct in_device *in_dev = pmc->interface;
1451 #endif
1453 /* no more filters for this source */
1454 if (psf_prev)
1455 psf_prev->sf_next = psf->sf_next;
1456 else
1457 pmc->sources = psf->sf_next;
1458 #ifdef CONFIG_IP_MULTICAST
1459 if (psf->sf_oldin &&
1460 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1461 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1462 IGMP_Unsolicited_Report_Count;
1463 psf->sf_next = pmc->tomb;
1464 pmc->tomb = psf;
1465 rv = 1;
1466 } else
1467 #endif
1468 kfree(psf);
1470 return rv;
1473 #ifndef CONFIG_IP_MULTICAST
1474 #define igmp_ifc_event(x) do { } while (0)
1475 #endif
1477 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1478 int sfcount, __be32 *psfsrc, int delta)
1480 struct ip_mc_list *pmc;
1481 int changerec = 0;
1482 int i, err;
1484 if (!in_dev)
1485 return -ENODEV;
1486 read_lock(&in_dev->mc_list_lock);
1487 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1488 if (*pmca == pmc->multiaddr)
1489 break;
1491 if (!pmc) {
1492 /* MCA not found?? bug */
1493 read_unlock(&in_dev->mc_list_lock);
1494 return -ESRCH;
1496 spin_lock_bh(&pmc->lock);
1497 read_unlock(&in_dev->mc_list_lock);
1498 #ifdef CONFIG_IP_MULTICAST
1499 sf_markstate(pmc);
1500 #endif
1501 if (!delta) {
1502 err = -EINVAL;
1503 if (!pmc->sfcount[sfmode])
1504 goto out_unlock;
1505 pmc->sfcount[sfmode]--;
1507 err = 0;
1508 for (i=0; i<sfcount; i++) {
1509 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1511 changerec |= rv > 0;
1512 if (!err && rv < 0)
1513 err = rv;
1515 if (pmc->sfmode == MCAST_EXCLUDE &&
1516 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1517 pmc->sfcount[MCAST_INCLUDE]) {
1518 #ifdef CONFIG_IP_MULTICAST
1519 struct ip_sf_list *psf;
1520 #endif
1522 /* filter mode change */
1523 pmc->sfmode = MCAST_INCLUDE;
1524 #ifdef CONFIG_IP_MULTICAST
1525 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1526 IGMP_Unsolicited_Report_Count;
1527 in_dev->mr_ifc_count = pmc->crcount;
1528 for (psf=pmc->sources; psf; psf = psf->sf_next)
1529 psf->sf_crcount = 0;
1530 igmp_ifc_event(pmc->interface);
1531 } else if (sf_setstate(pmc) || changerec) {
1532 igmp_ifc_event(pmc->interface);
1533 #endif
1535 out_unlock:
1536 spin_unlock_bh(&pmc->lock);
1537 return err;
1541 * Add multicast single-source filter to the interface list
1543 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1544 __be32 *psfsrc, int delta)
1546 struct ip_sf_list *psf, *psf_prev;
1548 psf_prev = NULL;
1549 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1550 if (psf->sf_inaddr == *psfsrc)
1551 break;
1552 psf_prev = psf;
1554 if (!psf) {
1555 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1556 if (!psf)
1557 return -ENOBUFS;
1558 psf->sf_inaddr = *psfsrc;
1559 if (psf_prev) {
1560 psf_prev->sf_next = psf;
1561 } else
1562 pmc->sources = psf;
1564 psf->sf_count[sfmode]++;
1565 if (psf->sf_count[sfmode] == 1) {
1566 ip_rt_multicast_event(pmc->interface);
1568 return 0;
1571 #ifdef CONFIG_IP_MULTICAST
1572 static void sf_markstate(struct ip_mc_list *pmc)
1574 struct ip_sf_list *psf;
1575 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1577 for (psf=pmc->sources; psf; psf=psf->sf_next)
1578 if (pmc->sfcount[MCAST_EXCLUDE]) {
1579 psf->sf_oldin = mca_xcount ==
1580 psf->sf_count[MCAST_EXCLUDE] &&
1581 !psf->sf_count[MCAST_INCLUDE];
1582 } else
1583 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1586 static int sf_setstate(struct ip_mc_list *pmc)
1588 struct ip_sf_list *psf, *dpsf;
1589 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1590 int qrv = pmc->interface->mr_qrv;
1591 int new_in, rv;
1593 rv = 0;
1594 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1595 if (pmc->sfcount[MCAST_EXCLUDE]) {
1596 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1597 !psf->sf_count[MCAST_INCLUDE];
1598 } else
1599 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1600 if (new_in) {
1601 if (!psf->sf_oldin) {
1602 struct ip_sf_list *prev = NULL;
1604 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1605 if (dpsf->sf_inaddr == psf->sf_inaddr)
1606 break;
1607 prev = dpsf;
1609 if (dpsf) {
1610 if (prev)
1611 prev->sf_next = dpsf->sf_next;
1612 else
1613 pmc->tomb = dpsf->sf_next;
1614 kfree(dpsf);
1616 psf->sf_crcount = qrv;
1617 rv++;
1619 } else if (psf->sf_oldin) {
1621 psf->sf_crcount = 0;
1623 * add or update "delete" records if an active filter
1624 * is now inactive
1626 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1627 if (dpsf->sf_inaddr == psf->sf_inaddr)
1628 break;
1629 if (!dpsf) {
1630 dpsf = (struct ip_sf_list *)
1631 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1632 if (!dpsf)
1633 continue;
1634 *dpsf = *psf;
1635 /* pmc->lock held by callers */
1636 dpsf->sf_next = pmc->tomb;
1637 pmc->tomb = dpsf;
1639 dpsf->sf_crcount = qrv;
1640 rv++;
1643 return rv;
1645 #endif
1648 * Add multicast source filter list to the interface list
1650 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1651 int sfcount, __be32 *psfsrc, int delta)
1653 struct ip_mc_list *pmc;
1654 int isexclude;
1655 int i, err;
1657 if (!in_dev)
1658 return -ENODEV;
1659 read_lock(&in_dev->mc_list_lock);
1660 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1661 if (*pmca == pmc->multiaddr)
1662 break;
1664 if (!pmc) {
1665 /* MCA not found?? bug */
1666 read_unlock(&in_dev->mc_list_lock);
1667 return -ESRCH;
1669 spin_lock_bh(&pmc->lock);
1670 read_unlock(&in_dev->mc_list_lock);
1672 #ifdef CONFIG_IP_MULTICAST
1673 sf_markstate(pmc);
1674 #endif
1675 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1676 if (!delta)
1677 pmc->sfcount[sfmode]++;
1678 err = 0;
1679 for (i=0; i<sfcount; i++) {
1680 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1681 if (err)
1682 break;
1684 if (err) {
1685 int j;
1687 pmc->sfcount[sfmode]--;
1688 for (j=0; j<i; j++)
1689 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1690 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1691 #ifdef CONFIG_IP_MULTICAST
1692 struct in_device *in_dev = pmc->interface;
1693 struct ip_sf_list *psf;
1694 #endif
1696 /* filter mode change */
1697 if (pmc->sfcount[MCAST_EXCLUDE])
1698 pmc->sfmode = MCAST_EXCLUDE;
1699 else if (pmc->sfcount[MCAST_INCLUDE])
1700 pmc->sfmode = MCAST_INCLUDE;
1701 #ifdef CONFIG_IP_MULTICAST
1702 /* else no filters; keep old mode for reports */
1704 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1705 IGMP_Unsolicited_Report_Count;
1706 in_dev->mr_ifc_count = pmc->crcount;
1707 for (psf=pmc->sources; psf; psf = psf->sf_next)
1708 psf->sf_crcount = 0;
1709 igmp_ifc_event(in_dev);
1710 } else if (sf_setstate(pmc)) {
1711 igmp_ifc_event(in_dev);
1712 #endif
1714 spin_unlock_bh(&pmc->lock);
1715 return err;
1718 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1720 struct ip_sf_list *psf, *nextpsf;
1722 for (psf=pmc->tomb; psf; psf=nextpsf) {
1723 nextpsf = psf->sf_next;
1724 kfree(psf);
1726 pmc->tomb = NULL;
1727 for (psf=pmc->sources; psf; psf=nextpsf) {
1728 nextpsf = psf->sf_next;
1729 kfree(psf);
1731 pmc->sources = NULL;
1732 pmc->sfmode = MCAST_EXCLUDE;
1733 pmc->sfcount[MCAST_INCLUDE] = 0;
1734 pmc->sfcount[MCAST_EXCLUDE] = 1;
1739 * Join a multicast group
1741 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1743 int err;
1744 __be32 addr = imr->imr_multiaddr.s_addr;
1745 struct ip_mc_socklist *iml=NULL, *i;
1746 struct in_device *in_dev;
1747 struct inet_sock *inet = inet_sk(sk);
1748 int ifindex;
1749 int count = 0;
1751 if (!MULTICAST(addr))
1752 return -EINVAL;
1754 rtnl_lock();
1756 in_dev = ip_mc_find_dev(imr);
1758 if (!in_dev) {
1759 iml = NULL;
1760 err = -ENODEV;
1761 goto done;
1764 err = -EADDRINUSE;
1765 ifindex = imr->imr_ifindex;
1766 for (i = inet->mc_list; i; i = i->next) {
1767 if (i->multi.imr_multiaddr.s_addr == addr &&
1768 i->multi.imr_ifindex == ifindex)
1769 goto done;
1770 count++;
1772 err = -ENOBUFS;
1773 if (count >= sysctl_igmp_max_memberships)
1774 goto done;
1775 iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1776 if (iml == NULL)
1777 goto done;
1779 memcpy(&iml->multi, imr, sizeof(*imr));
1780 iml->next = inet->mc_list;
1781 iml->sflist = NULL;
1782 iml->sfmode = MCAST_EXCLUDE;
1783 inet->mc_list = iml;
1784 ip_mc_inc_group(in_dev, addr);
1785 err = 0;
1786 done:
1787 rtnl_unlock();
1788 return err;
1791 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1792 struct in_device *in_dev)
1794 int err;
1796 if (iml->sflist == 0) {
1797 /* any-source empty exclude case */
1798 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1799 iml->sfmode, 0, NULL, 0);
1801 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1802 iml->sfmode, iml->sflist->sl_count,
1803 iml->sflist->sl_addr, 0);
1804 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1805 iml->sflist = NULL;
1806 return err;
1810 * Ask a socket to leave a group.
1813 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1815 struct inet_sock *inet = inet_sk(sk);
1816 struct ip_mc_socklist *iml, **imlp;
1817 struct in_device *in_dev;
1818 __be32 group = imr->imr_multiaddr.s_addr;
1819 u32 ifindex;
1820 int ret = -EADDRNOTAVAIL;
1822 rtnl_lock();
1823 in_dev = ip_mc_find_dev(imr);
1824 ifindex = imr->imr_ifindex;
1825 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1826 if (iml->multi.imr_multiaddr.s_addr != group)
1827 continue;
1828 if (ifindex) {
1829 if (iml->multi.imr_ifindex != ifindex)
1830 continue;
1831 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1832 iml->multi.imr_address.s_addr)
1833 continue;
1835 (void) ip_mc_leave_src(sk, iml, in_dev);
1837 *imlp = iml->next;
1839 if (in_dev)
1840 ip_mc_dec_group(in_dev, group);
1841 rtnl_unlock();
1842 sock_kfree_s(sk, iml, sizeof(*iml));
1843 return 0;
1845 if (!in_dev)
1846 ret = -ENODEV;
1847 rtnl_unlock();
1848 return ret;
1851 int ip_mc_source(int add, int omode, struct sock *sk, struct
1852 ip_mreq_source *mreqs, int ifindex)
1854 int err;
1855 struct ip_mreqn imr;
1856 __be32 addr = mreqs->imr_multiaddr;
1857 struct ip_mc_socklist *pmc;
1858 struct in_device *in_dev = NULL;
1859 struct inet_sock *inet = inet_sk(sk);
1860 struct ip_sf_socklist *psl;
1861 int leavegroup = 0;
1862 int i, j, rv;
1864 if (!MULTICAST(addr))
1865 return -EINVAL;
1867 rtnl_lock();
1869 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1870 imr.imr_address.s_addr = mreqs->imr_interface;
1871 imr.imr_ifindex = ifindex;
1872 in_dev = ip_mc_find_dev(&imr);
1874 if (!in_dev) {
1875 err = -ENODEV;
1876 goto done;
1878 err = -EADDRNOTAVAIL;
1880 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1881 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1882 && pmc->multi.imr_ifindex == imr.imr_ifindex)
1883 break;
1885 if (!pmc) { /* must have a prior join */
1886 err = -EINVAL;
1887 goto done;
1889 /* if a source filter was set, must be the same mode as before */
1890 if (pmc->sflist) {
1891 if (pmc->sfmode != omode) {
1892 err = -EINVAL;
1893 goto done;
1895 } else if (pmc->sfmode != omode) {
1896 /* allow mode switches for empty-set filters */
1897 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1898 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1899 NULL, 0);
1900 pmc->sfmode = omode;
1903 psl = pmc->sflist;
1904 if (!add) {
1905 if (!psl)
1906 goto done; /* err = -EADDRNOTAVAIL */
1907 rv = !0;
1908 for (i=0; i<psl->sl_count; i++) {
1909 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1910 sizeof(__be32));
1911 if (rv == 0)
1912 break;
1914 if (rv) /* source not found */
1915 goto done; /* err = -EADDRNOTAVAIL */
1917 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1918 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1919 leavegroup = 1;
1920 goto done;
1923 /* update the interface filter */
1924 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1925 &mreqs->imr_sourceaddr, 1);
1927 for (j=i+1; j<psl->sl_count; j++)
1928 psl->sl_addr[j-1] = psl->sl_addr[j];
1929 psl->sl_count--;
1930 err = 0;
1931 goto done;
1933 /* else, add a new source to the filter */
1935 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1936 err = -ENOBUFS;
1937 goto done;
1939 if (!psl || psl->sl_count == psl->sl_max) {
1940 struct ip_sf_socklist *newpsl;
1941 int count = IP_SFBLOCK;
1943 if (psl)
1944 count += psl->sl_max;
1945 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1946 if (!newpsl) {
1947 err = -ENOBUFS;
1948 goto done;
1950 newpsl->sl_max = count;
1951 newpsl->sl_count = count - IP_SFBLOCK;
1952 if (psl) {
1953 for (i=0; i<psl->sl_count; i++)
1954 newpsl->sl_addr[i] = psl->sl_addr[i];
1955 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1957 pmc->sflist = psl = newpsl;
1959 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1960 for (i=0; i<psl->sl_count; i++) {
1961 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1962 sizeof(__be32));
1963 if (rv == 0)
1964 break;
1966 if (rv == 0) /* address already there is an error */
1967 goto done;
1968 for (j=psl->sl_count-1; j>=i; j--)
1969 psl->sl_addr[j+1] = psl->sl_addr[j];
1970 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1971 psl->sl_count++;
1972 err = 0;
1973 /* update the interface list */
1974 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1975 &mreqs->imr_sourceaddr, 1);
1976 done:
1977 rtnl_unlock();
1978 if (leavegroup)
1979 return ip_mc_leave_group(sk, &imr);
1980 return err;
1983 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1985 int err = 0;
1986 struct ip_mreqn imr;
1987 __be32 addr = msf->imsf_multiaddr;
1988 struct ip_mc_socklist *pmc;
1989 struct in_device *in_dev;
1990 struct inet_sock *inet = inet_sk(sk);
1991 struct ip_sf_socklist *newpsl, *psl;
1992 int leavegroup = 0;
1994 if (!MULTICAST(addr))
1995 return -EINVAL;
1996 if (msf->imsf_fmode != MCAST_INCLUDE &&
1997 msf->imsf_fmode != MCAST_EXCLUDE)
1998 return -EINVAL;
2000 rtnl_lock();
2002 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2003 imr.imr_address.s_addr = msf->imsf_interface;
2004 imr.imr_ifindex = ifindex;
2005 in_dev = ip_mc_find_dev(&imr);
2007 if (!in_dev) {
2008 err = -ENODEV;
2009 goto done;
2012 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2013 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2014 leavegroup = 1;
2015 goto done;
2018 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2019 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2020 pmc->multi.imr_ifindex == imr.imr_ifindex)
2021 break;
2023 if (!pmc) { /* must have a prior join */
2024 err = -EINVAL;
2025 goto done;
2027 if (msf->imsf_numsrc) {
2028 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2029 GFP_KERNEL);
2030 if (!newpsl) {
2031 err = -ENOBUFS;
2032 goto done;
2034 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2035 memcpy(newpsl->sl_addr, msf->imsf_slist,
2036 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2037 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2038 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2039 if (err) {
2040 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2041 goto done;
2043 } else {
2044 newpsl = NULL;
2045 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2046 msf->imsf_fmode, 0, NULL, 0);
2048 psl = pmc->sflist;
2049 if (psl) {
2050 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2051 psl->sl_count, psl->sl_addr, 0);
2052 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2053 } else
2054 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2055 0, NULL, 0);
2056 pmc->sflist = newpsl;
2057 pmc->sfmode = msf->imsf_fmode;
2058 err = 0;
2059 done:
2060 rtnl_unlock();
2061 if (leavegroup)
2062 err = ip_mc_leave_group(sk, &imr);
2063 return err;
2066 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2067 struct ip_msfilter __user *optval, int __user *optlen)
2069 int err, len, count, copycount;
2070 struct ip_mreqn imr;
2071 __be32 addr = msf->imsf_multiaddr;
2072 struct ip_mc_socklist *pmc;
2073 struct in_device *in_dev;
2074 struct inet_sock *inet = inet_sk(sk);
2075 struct ip_sf_socklist *psl;
2077 if (!MULTICAST(addr))
2078 return -EINVAL;
2080 rtnl_lock();
2082 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2083 imr.imr_address.s_addr = msf->imsf_interface;
2084 imr.imr_ifindex = 0;
2085 in_dev = ip_mc_find_dev(&imr);
2087 if (!in_dev) {
2088 err = -ENODEV;
2089 goto done;
2091 err = -EADDRNOTAVAIL;
2093 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2094 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2095 pmc->multi.imr_ifindex == imr.imr_ifindex)
2096 break;
2098 if (!pmc) /* must have a prior join */
2099 goto done;
2100 msf->imsf_fmode = pmc->sfmode;
2101 psl = pmc->sflist;
2102 rtnl_unlock();
2103 if (!psl) {
2104 len = 0;
2105 count = 0;
2106 } else {
2107 count = psl->sl_count;
2109 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2110 len = copycount * sizeof(psl->sl_addr[0]);
2111 msf->imsf_numsrc = count;
2112 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2113 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2114 return -EFAULT;
2116 if (len &&
2117 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2118 return -EFAULT;
2119 return 0;
2120 done:
2121 rtnl_unlock();
2122 return err;
2125 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2126 struct group_filter __user *optval, int __user *optlen)
2128 int err, i, count, copycount;
2129 struct sockaddr_in *psin;
2130 __be32 addr;
2131 struct ip_mc_socklist *pmc;
2132 struct inet_sock *inet = inet_sk(sk);
2133 struct ip_sf_socklist *psl;
2135 psin = (struct sockaddr_in *)&gsf->gf_group;
2136 if (psin->sin_family != AF_INET)
2137 return -EINVAL;
2138 addr = psin->sin_addr.s_addr;
2139 if (!MULTICAST(addr))
2140 return -EINVAL;
2142 rtnl_lock();
2144 err = -EADDRNOTAVAIL;
2146 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2147 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2148 pmc->multi.imr_ifindex == gsf->gf_interface)
2149 break;
2151 if (!pmc) /* must have a prior join */
2152 goto done;
2153 gsf->gf_fmode = pmc->sfmode;
2154 psl = pmc->sflist;
2155 rtnl_unlock();
2156 count = psl ? psl->sl_count : 0;
2157 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2158 gsf->gf_numsrc = count;
2159 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2160 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2161 return -EFAULT;
2163 for (i=0; i<copycount; i++) {
2164 struct sockaddr_in *psin;
2165 struct sockaddr_storage ss;
2167 psin = (struct sockaddr_in *)&ss;
2168 memset(&ss, 0, sizeof(ss));
2169 psin->sin_family = AF_INET;
2170 psin->sin_addr.s_addr = psl->sl_addr[i];
2171 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2172 return -EFAULT;
2174 return 0;
2175 done:
2176 rtnl_unlock();
2177 return err;
2181 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2183 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2185 struct inet_sock *inet = inet_sk(sk);
2186 struct ip_mc_socklist *pmc;
2187 struct ip_sf_socklist *psl;
2188 int i;
2190 if (!MULTICAST(loc_addr))
2191 return 1;
2193 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2194 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2195 pmc->multi.imr_ifindex == dif)
2196 break;
2198 if (!pmc)
2199 return 1;
2200 psl = pmc->sflist;
2201 if (!psl)
2202 return pmc->sfmode == MCAST_EXCLUDE;
2204 for (i=0; i<psl->sl_count; i++) {
2205 if (psl->sl_addr[i] == rmt_addr)
2206 break;
2208 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2209 return 0;
2210 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2211 return 0;
2212 return 1;
2216 * A socket is closing.
2219 void ip_mc_drop_socket(struct sock *sk)
2221 struct inet_sock *inet = inet_sk(sk);
2222 struct ip_mc_socklist *iml;
2224 if (inet->mc_list == NULL)
2225 return;
2227 rtnl_lock();
2228 while ((iml = inet->mc_list) != NULL) {
2229 struct in_device *in_dev;
2230 inet->mc_list = iml->next;
2232 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
2233 (void) ip_mc_leave_src(sk, iml, in_dev);
2234 if (in_dev != NULL) {
2235 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2236 in_dev_put(in_dev);
2238 sock_kfree_s(sk, iml, sizeof(*iml));
2240 rtnl_unlock();
2243 int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2245 struct ip_mc_list *im;
2246 struct ip_sf_list *psf;
2247 int rv = 0;
2249 read_lock(&in_dev->mc_list_lock);
2250 for (im=in_dev->mc_list; im; im=im->next) {
2251 if (im->multiaddr == mc_addr)
2252 break;
2254 if (im && proto == IPPROTO_IGMP) {
2255 rv = 1;
2256 } else if (im) {
2257 if (src_addr) {
2258 for (psf=im->sources; psf; psf=psf->sf_next) {
2259 if (psf->sf_inaddr == src_addr)
2260 break;
2262 if (psf)
2263 rv = psf->sf_count[MCAST_INCLUDE] ||
2264 psf->sf_count[MCAST_EXCLUDE] !=
2265 im->sfcount[MCAST_EXCLUDE];
2266 else
2267 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2268 } else
2269 rv = 1; /* unspecified source; tentatively allow */
2271 read_unlock(&in_dev->mc_list_lock);
2272 return rv;
2275 #if defined(CONFIG_PROC_FS)
2276 struct igmp_mc_iter_state {
2277 struct net_device *dev;
2278 struct in_device *in_dev;
2281 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2283 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2285 struct ip_mc_list *im = NULL;
2286 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2288 for (state->dev = dev_base, state->in_dev = NULL;
2289 state->dev;
2290 state->dev = state->dev->next) {
2291 struct in_device *in_dev;
2292 in_dev = in_dev_get(state->dev);
2293 if (!in_dev)
2294 continue;
2295 read_lock(&in_dev->mc_list_lock);
2296 im = in_dev->mc_list;
2297 if (im) {
2298 state->in_dev = in_dev;
2299 break;
2301 read_unlock(&in_dev->mc_list_lock);
2302 in_dev_put(in_dev);
2304 return im;
2307 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2309 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2310 im = im->next;
2311 while (!im) {
2312 if (likely(state->in_dev != NULL)) {
2313 read_unlock(&state->in_dev->mc_list_lock);
2314 in_dev_put(state->in_dev);
2316 state->dev = state->dev->next;
2317 if (!state->dev) {
2318 state->in_dev = NULL;
2319 break;
2321 state->in_dev = in_dev_get(state->dev);
2322 if (!state->in_dev)
2323 continue;
2324 read_lock(&state->in_dev->mc_list_lock);
2325 im = state->in_dev->mc_list;
2327 return im;
2330 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2332 struct ip_mc_list *im = igmp_mc_get_first(seq);
2333 if (im)
2334 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2335 --pos;
2336 return pos ? NULL : im;
2339 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2341 read_lock(&dev_base_lock);
2342 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2345 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2347 struct ip_mc_list *im;
2348 if (v == SEQ_START_TOKEN)
2349 im = igmp_mc_get_first(seq);
2350 else
2351 im = igmp_mc_get_next(seq, v);
2352 ++*pos;
2353 return im;
2356 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2358 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2359 if (likely(state->in_dev != NULL)) {
2360 read_unlock(&state->in_dev->mc_list_lock);
2361 in_dev_put(state->in_dev);
2362 state->in_dev = NULL;
2364 state->dev = NULL;
2365 read_unlock(&dev_base_lock);
2368 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2370 if (v == SEQ_START_TOKEN)
2371 seq_puts(seq,
2372 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2373 else {
2374 struct ip_mc_list *im = (struct ip_mc_list *)v;
2375 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2376 char *querier;
2377 #ifdef CONFIG_IP_MULTICAST
2378 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2379 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2380 "V3";
2381 #else
2382 querier = "NONE";
2383 #endif
2385 if (state->in_dev->mc_list == im) {
2386 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2387 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2390 seq_printf(seq,
2391 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2392 im->multiaddr, im->users,
2393 im->tm_running, im->tm_running ?
2394 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2395 im->reporter);
2397 return 0;
2400 static struct seq_operations igmp_mc_seq_ops = {
2401 .start = igmp_mc_seq_start,
2402 .next = igmp_mc_seq_next,
2403 .stop = igmp_mc_seq_stop,
2404 .show = igmp_mc_seq_show,
2407 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2409 struct seq_file *seq;
2410 int rc = -ENOMEM;
2411 struct igmp_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2413 if (!s)
2414 goto out;
2415 rc = seq_open(file, &igmp_mc_seq_ops);
2416 if (rc)
2417 goto out_kfree;
2419 seq = file->private_data;
2420 seq->private = s;
2421 out:
2422 return rc;
2423 out_kfree:
2424 kfree(s);
2425 goto out;
2428 static const struct file_operations igmp_mc_seq_fops = {
2429 .owner = THIS_MODULE,
2430 .open = igmp_mc_seq_open,
2431 .read = seq_read,
2432 .llseek = seq_lseek,
2433 .release = seq_release_private,
2436 struct igmp_mcf_iter_state {
2437 struct net_device *dev;
2438 struct in_device *idev;
2439 struct ip_mc_list *im;
2442 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2444 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2446 struct ip_sf_list *psf = NULL;
2447 struct ip_mc_list *im = NULL;
2448 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2450 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2451 state->dev;
2452 state->dev = state->dev->next) {
2453 struct in_device *idev;
2454 idev = in_dev_get(state->dev);
2455 if (unlikely(idev == NULL))
2456 continue;
2457 read_lock(&idev->mc_list_lock);
2458 im = idev->mc_list;
2459 if (likely(im != NULL)) {
2460 spin_lock_bh(&im->lock);
2461 psf = im->sources;
2462 if (likely(psf != NULL)) {
2463 state->im = im;
2464 state->idev = idev;
2465 break;
2467 spin_unlock_bh(&im->lock);
2469 read_unlock(&idev->mc_list_lock);
2470 in_dev_put(idev);
2472 return psf;
2475 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2477 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2479 psf = psf->sf_next;
2480 while (!psf) {
2481 spin_unlock_bh(&state->im->lock);
2482 state->im = state->im->next;
2483 while (!state->im) {
2484 if (likely(state->idev != NULL)) {
2485 read_unlock(&state->idev->mc_list_lock);
2486 in_dev_put(state->idev);
2488 state->dev = state->dev->next;
2489 if (!state->dev) {
2490 state->idev = NULL;
2491 goto out;
2493 state->idev = in_dev_get(state->dev);
2494 if (!state->idev)
2495 continue;
2496 read_lock(&state->idev->mc_list_lock);
2497 state->im = state->idev->mc_list;
2499 if (!state->im)
2500 break;
2501 spin_lock_bh(&state->im->lock);
2502 psf = state->im->sources;
2504 out:
2505 return psf;
2508 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2510 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2511 if (psf)
2512 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2513 --pos;
2514 return pos ? NULL : psf;
2517 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2519 read_lock(&dev_base_lock);
2520 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2523 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2525 struct ip_sf_list *psf;
2526 if (v == SEQ_START_TOKEN)
2527 psf = igmp_mcf_get_first(seq);
2528 else
2529 psf = igmp_mcf_get_next(seq, v);
2530 ++*pos;
2531 return psf;
2534 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2536 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2537 if (likely(state->im != NULL)) {
2538 spin_unlock_bh(&state->im->lock);
2539 state->im = NULL;
2541 if (likely(state->idev != NULL)) {
2542 read_unlock(&state->idev->mc_list_lock);
2543 in_dev_put(state->idev);
2544 state->idev = NULL;
2546 state->dev = NULL;
2547 read_unlock(&dev_base_lock);
2550 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2552 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2553 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2555 if (v == SEQ_START_TOKEN) {
2556 seq_printf(seq,
2557 "%3s %6s "
2558 "%10s %10s %6s %6s\n", "Idx",
2559 "Device", "MCA",
2560 "SRC", "INC", "EXC");
2561 } else {
2562 seq_printf(seq,
2563 "%3d %6.6s 0x%08x "
2564 "0x%08x %6lu %6lu\n",
2565 state->dev->ifindex, state->dev->name,
2566 ntohl(state->im->multiaddr),
2567 ntohl(psf->sf_inaddr),
2568 psf->sf_count[MCAST_INCLUDE],
2569 psf->sf_count[MCAST_EXCLUDE]);
2571 return 0;
2574 static struct seq_operations igmp_mcf_seq_ops = {
2575 .start = igmp_mcf_seq_start,
2576 .next = igmp_mcf_seq_next,
2577 .stop = igmp_mcf_seq_stop,
2578 .show = igmp_mcf_seq_show,
2581 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2583 struct seq_file *seq;
2584 int rc = -ENOMEM;
2585 struct igmp_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2587 if (!s)
2588 goto out;
2589 rc = seq_open(file, &igmp_mcf_seq_ops);
2590 if (rc)
2591 goto out_kfree;
2593 seq = file->private_data;
2594 seq->private = s;
2595 out:
2596 return rc;
2597 out_kfree:
2598 kfree(s);
2599 goto out;
2602 static const struct file_operations igmp_mcf_seq_fops = {
2603 .owner = THIS_MODULE,
2604 .open = igmp_mcf_seq_open,
2605 .read = seq_read,
2606 .llseek = seq_lseek,
2607 .release = seq_release_private,
2610 int __init igmp_mc_proc_init(void)
2612 proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2613 proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2614 return 0;
2616 #endif
2618 EXPORT_SYMBOL(ip_mc_dec_group);
2619 EXPORT_SYMBOL(ip_mc_inc_group);
2620 EXPORT_SYMBOL(ip_mc_join_group);
2621 EXPORT_SYMBOL(ip_mc_rejoin_group);