]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/igmp.c
[IPV4]: multicast API "join" issues
[linux-2.6-omap-h63xx.git] / net / ipv4 / igmp.c
1 /*
2  *      Linux NET3:     Internet Group Management Protocol  [IGMP]
3  *
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.
6  *
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.
10  *
11  *      Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
12  *
13  *      Authors:
14  *              Alan Cox <Alan.Cox@linux.org>
15  *
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.
20  *
21  *      Fixes:
22  *
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.
35  *
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
73  */
74
75 #include <linux/config.h>
76 #include <linux/module.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 <linux/times.h>
94 #include <net/ip.h>
95 #include <net/protocol.h>
96 #include <net/route.h>
97 #include <net/sock.h>
98 #include <net/checksum.h>
99 #include <linux/netfilter_ipv4.h>
100 #ifdef CONFIG_IP_MROUTE
101 #include <linux/mroute.h>
102 #endif
103 #ifdef CONFIG_PROC_FS
104 #include <linux/proc_fs.h>
105 #include <linux/seq_file.h>
106 #endif
107
108 #define IP_MAX_MEMBERSHIPS      20
109 #define IP_MAX_MSF              10
110
111 #ifdef CONFIG_IP_MULTICAST
112 /* Parameter names and values are taken from igmp-v2-06 draft */
113
114 #define IGMP_V1_Router_Present_Timeout          (400*HZ)
115 #define IGMP_V2_Router_Present_Timeout          (400*HZ)
116 #define IGMP_Unsolicited_Report_Interval        (10*HZ)
117 #define IGMP_Query_Response_Interval            (10*HZ)
118 #define IGMP_Unsolicited_Report_Count           2
119
120
121 #define IGMP_Initial_Report_Delay               (1)
122
123 /* IGMP_Initial_Report_Delay is not from IGMP specs!
124  * IGMP specs require to report membership immediately after
125  * joining a group, but we delay the first report by a
126  * small interval. It seems more natural and still does not
127  * contradict to specs provided this delay is small enough.
128  */
129
130 #define IGMP_V1_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 1 || \
131                 (in_dev)->cnf.force_igmp_version == 1 || \
132                 ((in_dev)->mr_v1_seen && \
133                 time_before(jiffies, (in_dev)->mr_v1_seen)))
134 #define IGMP_V2_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 2 || \
135                 (in_dev)->cnf.force_igmp_version == 2 || \
136                 ((in_dev)->mr_v2_seen && \
137                 time_before(jiffies, (in_dev)->mr_v2_seen)))
138
139 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
140 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr);
141 static void igmpv3_clear_delrec(struct in_device *in_dev);
142 static int sf_setstate(struct ip_mc_list *pmc);
143 static void sf_markstate(struct ip_mc_list *pmc);
144 #endif
145 static void ip_mc_clear_src(struct ip_mc_list *pmc);
146 static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
147                          int sfcount, __u32 *psfsrc, int delta);
148
149 static void ip_ma_put(struct ip_mc_list *im)
150 {
151         if (atomic_dec_and_test(&im->refcnt)) {
152                 in_dev_put(im->interface);
153                 kfree(im);
154         }
155 }
156
157 #ifdef CONFIG_IP_MULTICAST
158
159 /*
160  *      Timer management
161  */
162
163 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
164 {
165         spin_lock_bh(&im->lock);
166         if (del_timer(&im->timer))
167                 atomic_dec(&im->refcnt);
168         im->tm_running=0;
169         im->reporter = 0;
170         im->unsolicit_count = 0;
171         spin_unlock_bh(&im->lock);
172 }
173
174 /* It must be called with locked im->lock */
175 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
176 {
177         int tv=net_random() % max_delay;
178
179         im->tm_running=1;
180         if (!mod_timer(&im->timer, jiffies+tv+2))
181                 atomic_inc(&im->refcnt);
182 }
183
184 static void igmp_gq_start_timer(struct in_device *in_dev)
185 {
186         int tv = net_random() % in_dev->mr_maxdelay;
187
188         in_dev->mr_gq_running = 1;
189         if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
190                 in_dev_hold(in_dev);
191 }
192
193 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
194 {
195         int tv = net_random() % delay;
196
197         if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
198                 in_dev_hold(in_dev);
199 }
200
201 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
202 {
203         spin_lock_bh(&im->lock);
204         im->unsolicit_count = 0;
205         if (del_timer(&im->timer)) {
206                 if ((long)(im->timer.expires-jiffies) < max_delay) {
207                         add_timer(&im->timer);
208                         im->tm_running=1;
209                         spin_unlock_bh(&im->lock);
210                         return;
211                 }
212                 atomic_dec(&im->refcnt);
213         }
214         igmp_start_timer(im, max_delay);
215         spin_unlock_bh(&im->lock);
216 }
217
218
219 /*
220  *      Send an IGMP report.
221  */
222
223 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
224
225
226 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
227         int gdeleted, int sdeleted)
228 {
229         switch (type) {
230         case IGMPV3_MODE_IS_INCLUDE:
231         case IGMPV3_MODE_IS_EXCLUDE:
232                 if (gdeleted || sdeleted)
233                         return 0;
234                 return !(pmc->gsquery && !psf->sf_gsresp);
235         case IGMPV3_CHANGE_TO_INCLUDE:
236                 if (gdeleted || sdeleted)
237                         return 0;
238                 return psf->sf_count[MCAST_INCLUDE] != 0;
239         case IGMPV3_CHANGE_TO_EXCLUDE:
240                 if (gdeleted || sdeleted)
241                         return 0;
242                 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
243                     psf->sf_count[MCAST_INCLUDE])
244                         return 0;
245                 return pmc->sfcount[MCAST_EXCLUDE] ==
246                         psf->sf_count[MCAST_EXCLUDE];
247         case IGMPV3_ALLOW_NEW_SOURCES:
248                 if (gdeleted || !psf->sf_crcount)
249                         return 0;
250                 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
251         case IGMPV3_BLOCK_OLD_SOURCES:
252                 if (pmc->sfmode == MCAST_INCLUDE)
253                         return gdeleted || (psf->sf_crcount && sdeleted);
254                 return psf->sf_crcount && !gdeleted && !sdeleted;
255         }
256         return 0;
257 }
258
259 static int
260 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
261 {
262         struct ip_sf_list *psf;
263         int scount = 0;
264
265         for (psf=pmc->sources; psf; psf=psf->sf_next) {
266                 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
267                         continue;
268                 scount++;
269         }
270         return scount;
271 }
272
273 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
274 {
275         struct sk_buff *skb;
276         struct rtable *rt;
277         struct iphdr *pip;
278         struct igmpv3_report *pig;
279
280         skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
281         if (skb == NULL)
282                 return NULL;
283
284         {
285                 struct flowi fl = { .oif = dev->ifindex,
286                                     .nl_u = { .ip4_u = {
287                                     .daddr = IGMPV3_ALL_MCR } },
288                                     .proto = IPPROTO_IGMP };
289                 if (ip_route_output_key(&rt, &fl)) {
290                         kfree_skb(skb);
291                         return NULL;
292                 }
293         }
294         if (rt->rt_src == 0) {
295                 kfree_skb(skb);
296                 ip_rt_put(rt);
297                 return NULL;
298         }
299
300         skb->dst = &rt->u.dst;
301         skb->dev = dev;
302
303         skb_reserve(skb, LL_RESERVED_SPACE(dev));
304
305         skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
306
307         pip->version  = 4;
308         pip->ihl      = (sizeof(struct iphdr)+4)>>2;
309         pip->tos      = 0xc0;
310         pip->frag_off = htons(IP_DF);
311         pip->ttl      = 1;
312         pip->daddr    = rt->rt_dst;
313         pip->saddr    = rt->rt_src;
314         pip->protocol = IPPROTO_IGMP;
315         pip->tot_len  = 0;      /* filled in later */
316         ip_select_ident(pip, &rt->u.dst, NULL);
317         ((u8*)&pip[1])[0] = IPOPT_RA;
318         ((u8*)&pip[1])[1] = 4;
319         ((u8*)&pip[1])[2] = 0;
320         ((u8*)&pip[1])[3] = 0;
321
322         pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
323         skb->h.igmph = (struct igmphdr *)pig;
324         pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
325         pig->resv1 = 0;
326         pig->csum = 0;
327         pig->resv2 = 0;
328         pig->ngrec = 0;
329         return skb;
330 }
331
332 static int igmpv3_sendpack(struct sk_buff *skb)
333 {
334         struct iphdr *pip = skb->nh.iph;
335         struct igmphdr *pig = skb->h.igmph;
336         int iplen, igmplen;
337
338         iplen = skb->tail - (unsigned char *)skb->nh.iph;
339         pip->tot_len = htons(iplen);
340         ip_send_check(pip);
341
342         igmplen = skb->tail - (unsigned char *)skb->h.igmph;
343         pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
344
345         return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
346                        dst_output);
347 }
348
349 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
350 {
351         return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
352 }
353
354 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
355         int type, struct igmpv3_grec **ppgr)
356 {
357         struct net_device *dev = pmc->interface->dev;
358         struct igmpv3_report *pih;
359         struct igmpv3_grec *pgr;
360
361         if (!skb)
362                 skb = igmpv3_newpack(dev, dev->mtu);
363         if (!skb)
364                 return NULL;
365         pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
366         pgr->grec_type = type;
367         pgr->grec_auxwords = 0;
368         pgr->grec_nsrcs = 0;
369         pgr->grec_mca = pmc->multiaddr;
370         pih = (struct igmpv3_report *)skb->h.igmph;
371         pih->ngrec = htons(ntohs(pih->ngrec)+1);
372         *ppgr = pgr;
373         return skb;
374 }
375
376 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
377         skb_tailroom(skb)) : 0)
378
379 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
380         int type, int gdeleted, int sdeleted)
381 {
382         struct net_device *dev = pmc->interface->dev;
383         struct igmpv3_report *pih;
384         struct igmpv3_grec *pgr = NULL;
385         struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
386         int scount, first, isquery, truncate;
387
388         if (pmc->multiaddr == IGMP_ALL_HOSTS)
389                 return skb;
390
391         isquery = type == IGMPV3_MODE_IS_INCLUDE ||
392                   type == IGMPV3_MODE_IS_EXCLUDE;
393         truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
394                     type == IGMPV3_CHANGE_TO_EXCLUDE;
395
396         psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
397
398         if (!*psf_list) {
399                 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
400                     type == IGMPV3_BLOCK_OLD_SOURCES)
401                         return skb;
402                 if (pmc->crcount || isquery) {
403                         /* make sure we have room for group header and at
404                          * least one source.
405                          */
406                         if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)+
407                             sizeof(__u32)) {
408                                 igmpv3_sendpack(skb);
409                                 skb = NULL; /* add_grhead will get a new one */
410                         }
411                         skb = add_grhead(skb, pmc, type, &pgr);
412                 }
413                 return skb;
414         }
415         pih = skb ? (struct igmpv3_report *)skb->h.igmph : NULL;
416
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);
424                 }
425         }
426         first = 1;
427         scount = 0;
428         psf_prev = NULL;
429         for (psf=*psf_list; psf; psf=psf_next) {
430                 u32 *psrc;
431
432                 psf_next = psf->sf_next;
433
434                 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
435                         psf_prev = psf;
436                         continue;
437                 }
438
439                 /* clear marks on query responses */
440                 if (isquery)
441                         psf->sf_gsresp = 0;
442
443                 if (AVAILABLE(skb) < sizeof(u32) +
444                     first*sizeof(struct igmpv3_grec)) {
445                         if (truncate && !first)
446                                 break;   /* truncate these */
447                         if (pgr)
448                                 pgr->grec_nsrcs = htons(scount);
449                         if (skb)
450                                 igmpv3_sendpack(skb);
451                         skb = igmpv3_newpack(dev, dev->mtu);
452                         first = 1;
453                         scount = 0;
454                 }
455                 if (first) {
456                         skb = add_grhead(skb, pmc, type, &pgr);
457                         first = 0;
458                 }
459                 psrc = (u32 *)skb_put(skb, sizeof(u32));
460                 *psrc = psf->sf_inaddr;
461                 scount++;
462                 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
463                      type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
464                         psf->sf_crcount--;
465                         if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
466                                 if (psf_prev)
467                                         psf_prev->sf_next = psf->sf_next;
468                                 else
469                                         *psf_list = psf->sf_next;
470                                 kfree(psf);
471                                 continue;
472                         }
473                 }
474                 psf_prev = psf;
475         }
476         if (pgr)
477                 pgr->grec_nsrcs = htons(scount);
478
479         if (isquery)
480                 pmc->gsquery = 0;       /* clear query state on report */
481         return skb;
482 }
483
484 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
485 {
486         struct sk_buff *skb = NULL;
487         int type;
488
489         if (!pmc) {
490                 read_lock(&in_dev->mc_list_lock);
491                 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
492                         if (pmc->multiaddr == IGMP_ALL_HOSTS)
493                                 continue;
494                         spin_lock_bh(&pmc->lock);
495                         if (pmc->sfcount[MCAST_EXCLUDE])
496                                 type = IGMPV3_MODE_IS_EXCLUDE;
497                         else
498                                 type = IGMPV3_MODE_IS_INCLUDE;
499                         skb = add_grec(skb, pmc, type, 0, 0);
500                         spin_unlock_bh(&pmc->lock);
501                 }
502                 read_unlock(&in_dev->mc_list_lock);
503         } else {
504                 spin_lock_bh(&pmc->lock);
505                 if (pmc->sfcount[MCAST_EXCLUDE])
506                         type = IGMPV3_MODE_IS_EXCLUDE;
507                 else
508                         type = IGMPV3_MODE_IS_INCLUDE;
509                 skb = add_grec(skb, pmc, type, 0, 0);
510                 spin_unlock_bh(&pmc->lock);
511         }
512         if (!skb)
513                 return 0;
514         return igmpv3_sendpack(skb);
515 }
516
517 /*
518  * remove zero-count source records from a source filter list
519  */
520 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
521 {
522         struct ip_sf_list *psf_prev, *psf_next, *psf;
523
524         psf_prev = NULL;
525         for (psf=*ppsf; psf; psf = psf_next) {
526                 psf_next = psf->sf_next;
527                 if (psf->sf_crcount == 0) {
528                         if (psf_prev)
529                                 psf_prev->sf_next = psf->sf_next;
530                         else
531                                 *ppsf = psf->sf_next;
532                         kfree(psf);
533                 } else
534                         psf_prev = psf;
535         }
536 }
537
538 static void igmpv3_send_cr(struct in_device *in_dev)
539 {
540         struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
541         struct sk_buff *skb = NULL;
542         int type, dtype;
543
544         read_lock(&in_dev->mc_list_lock);
545         spin_lock_bh(&in_dev->mc_tomb_lock);
546
547         /* deleted MCA's */
548         pmc_prev = NULL;
549         for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
550                 pmc_next = pmc->next;
551                 if (pmc->sfmode == MCAST_INCLUDE) {
552                         type = IGMPV3_BLOCK_OLD_SOURCES;
553                         dtype = IGMPV3_BLOCK_OLD_SOURCES;
554                         skb = add_grec(skb, pmc, type, 1, 0);
555                         skb = add_grec(skb, pmc, dtype, 1, 1);
556                 }
557                 if (pmc->crcount) {
558                         pmc->crcount--;
559                         if (pmc->sfmode == MCAST_EXCLUDE) {
560                                 type = IGMPV3_CHANGE_TO_INCLUDE;
561                                 skb = add_grec(skb, pmc, type, 1, 0);
562                         }
563                         if (pmc->crcount == 0) {
564                                 igmpv3_clear_zeros(&pmc->tomb);
565                                 igmpv3_clear_zeros(&pmc->sources);
566                         }
567                 }
568                 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
569                         if (pmc_prev)
570                                 pmc_prev->next = pmc_next;
571                         else
572                                 in_dev->mc_tomb = pmc_next;
573                         in_dev_put(pmc->interface);
574                         kfree(pmc);
575                 } else
576                         pmc_prev = pmc;
577         }
578         spin_unlock_bh(&in_dev->mc_tomb_lock);
579
580         /* change recs */
581         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
582                 spin_lock_bh(&pmc->lock);
583                 if (pmc->sfcount[MCAST_EXCLUDE]) {
584                         type = IGMPV3_BLOCK_OLD_SOURCES;
585                         dtype = IGMPV3_ALLOW_NEW_SOURCES;
586                 } else {
587                         type = IGMPV3_ALLOW_NEW_SOURCES;
588                         dtype = IGMPV3_BLOCK_OLD_SOURCES;
589                 }
590                 skb = add_grec(skb, pmc, type, 0, 0);
591                 skb = add_grec(skb, pmc, dtype, 0, 1);  /* deleted sources */
592
593                 /* filter mode changes */
594                 if (pmc->crcount) {
595                         pmc->crcount--;
596                         if (pmc->sfmode == MCAST_EXCLUDE)
597                                 type = IGMPV3_CHANGE_TO_EXCLUDE;
598                         else
599                                 type = IGMPV3_CHANGE_TO_INCLUDE;
600                         skb = add_grec(skb, pmc, type, 0, 0);
601                 }
602                 spin_unlock_bh(&pmc->lock);
603         }
604         read_unlock(&in_dev->mc_list_lock);
605
606         if (!skb)
607                 return;
608         (void) igmpv3_sendpack(skb);
609 }
610
611 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
612         int type)
613 {
614         struct sk_buff *skb;
615         struct iphdr *iph;
616         struct igmphdr *ih;
617         struct rtable *rt;
618         struct net_device *dev = in_dev->dev;
619         u32     group = pmc ? pmc->multiaddr : 0;
620         u32     dst;
621
622         if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
623                 return igmpv3_send_report(in_dev, pmc);
624         else if (type == IGMP_HOST_LEAVE_MESSAGE)
625                 dst = IGMP_ALL_ROUTER;
626         else
627                 dst = group;
628
629         {
630                 struct flowi fl = { .oif = dev->ifindex,
631                                     .nl_u = { .ip4_u = { .daddr = dst } },
632                                     .proto = IPPROTO_IGMP };
633                 if (ip_route_output_key(&rt, &fl))
634                         return -1;
635         }
636         if (rt->rt_src == 0) {
637                 ip_rt_put(rt);
638                 return -1;
639         }
640
641         skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
642         if (skb == NULL) {
643                 ip_rt_put(rt);
644                 return -1;
645         }
646
647         skb->dst = &rt->u.dst;
648
649         skb_reserve(skb, LL_RESERVED_SPACE(dev));
650
651         skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
652
653         iph->version  = 4;
654         iph->ihl      = (sizeof(struct iphdr)+4)>>2;
655         iph->tos      = 0xc0;
656         iph->frag_off = htons(IP_DF);
657         iph->ttl      = 1;
658         iph->daddr    = dst;
659         iph->saddr    = rt->rt_src;
660         iph->protocol = IPPROTO_IGMP;
661         iph->tot_len  = htons(IGMP_SIZE);
662         ip_select_ident(iph, &rt->u.dst, NULL);
663         ((u8*)&iph[1])[0] = IPOPT_RA;
664         ((u8*)&iph[1])[1] = 4;
665         ((u8*)&iph[1])[2] = 0;
666         ((u8*)&iph[1])[3] = 0;
667         ip_send_check(iph);
668
669         ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
670         ih->type=type;
671         ih->code=0;
672         ih->csum=0;
673         ih->group=group;
674         ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
675
676         return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
677                        dst_output);
678 }
679
680 static void igmp_gq_timer_expire(unsigned long data)
681 {
682         struct in_device *in_dev = (struct in_device *)data;
683
684         in_dev->mr_gq_running = 0;
685         igmpv3_send_report(in_dev, NULL);
686         __in_dev_put(in_dev);
687 }
688
689 static void igmp_ifc_timer_expire(unsigned long data)
690 {
691         struct in_device *in_dev = (struct in_device *)data;
692
693         igmpv3_send_cr(in_dev);
694         if (in_dev->mr_ifc_count) {
695                 in_dev->mr_ifc_count--;
696                 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
697         }
698         __in_dev_put(in_dev);
699 }
700
701 static void igmp_ifc_event(struct in_device *in_dev)
702 {
703         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
704                 return;
705         in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv : 
706                 IGMP_Unsolicited_Report_Count;
707         igmp_ifc_start_timer(in_dev, 1);
708 }
709
710
711 static void igmp_timer_expire(unsigned long data)
712 {
713         struct ip_mc_list *im=(struct ip_mc_list *)data;
714         struct in_device *in_dev = im->interface;
715
716         spin_lock(&im->lock);
717         im->tm_running=0;
718
719         if (im->unsolicit_count) {
720                 im->unsolicit_count--;
721                 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
722         }
723         im->reporter = 1;
724         spin_unlock(&im->lock);
725
726         if (IGMP_V1_SEEN(in_dev))
727                 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
728         else if (IGMP_V2_SEEN(in_dev))
729                 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
730         else
731                 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
732
733         ip_ma_put(im);
734 }
735
736 static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
737 {
738         struct ip_sf_list *psf;
739         int i, scount;
740
741         scount = 0;
742         for (psf=pmc->sources; psf; psf=psf->sf_next) {
743                 if (scount == nsrcs)
744                         break;
745                 for (i=0; i<nsrcs; i++)
746                         if (srcs[i] == psf->sf_inaddr) {
747                                 psf->sf_gsresp = 1;
748                                 scount++;
749                                 break;
750                         }
751         }
752 }
753
754 static void igmp_heard_report(struct in_device *in_dev, u32 group)
755 {
756         struct ip_mc_list *im;
757
758         /* Timers are only set for non-local groups */
759
760         if (group == IGMP_ALL_HOSTS)
761                 return;
762
763         read_lock(&in_dev->mc_list_lock);
764         for (im=in_dev->mc_list; im!=NULL; im=im->next) {
765                 if (im->multiaddr == group) {
766                         igmp_stop_timer(im);
767                         break;
768                 }
769         }
770         read_unlock(&in_dev->mc_list_lock);
771 }
772
773 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
774         int len)
775 {
776         struct igmphdr          *ih = skb->h.igmph;
777         struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
778         struct ip_mc_list       *im;
779         u32                     group = ih->group;
780         int                     max_delay;
781         int                     mark = 0;
782
783
784         if (len == 8) {
785                 if (ih->code == 0) {
786                         /* Alas, old v1 router presents here. */
787         
788                         max_delay = IGMP_Query_Response_Interval;
789                         in_dev->mr_v1_seen = jiffies +
790                                 IGMP_V1_Router_Present_Timeout;
791                         group = 0;
792                 } else {
793                         /* v2 router present */
794                         max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
795                         in_dev->mr_v2_seen = jiffies +
796                                 IGMP_V2_Router_Present_Timeout;
797                 }
798                 /* cancel the interface change timer */
799                 in_dev->mr_ifc_count = 0;
800                 if (del_timer(&in_dev->mr_ifc_timer))
801                         __in_dev_put(in_dev);
802                 /* clear deleted report items */
803                 igmpv3_clear_delrec(in_dev);
804         } else if (len < 12) {
805                 return; /* ignore bogus packet; freed by caller */
806         } else { /* v3 */
807                 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
808                         return;
809                 
810                 ih3 = (struct igmpv3_query *) skb->h.raw;
811                 if (ih3->nsrcs) {
812                         if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 
813                                            + ntohs(ih3->nsrcs)*sizeof(__u32)))
814                                 return;
815                         ih3 = (struct igmpv3_query *) skb->h.raw;
816                 }
817
818                 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
819                 if (!max_delay)
820                         max_delay = 1;  /* can't mod w/ 0 */
821                 in_dev->mr_maxdelay = max_delay;
822                 if (ih3->qrv)
823                         in_dev->mr_qrv = ih3->qrv;
824                 if (!group) { /* general query */
825                         if (ih3->nsrcs)
826                                 return; /* no sources allowed */
827                         igmp_gq_start_timer(in_dev);
828                         return;
829                 }
830                 /* mark sources to include, if group & source-specific */
831                 mark = ih3->nsrcs != 0;
832         }
833
834         /*
835          * - Start the timers in all of our membership records
836          *   that the query applies to for the interface on
837          *   which the query arrived excl. those that belong
838          *   to a "local" group (224.0.0.X)
839          * - For timers already running check if they need to
840          *   be reset.
841          * - Use the igmp->igmp_code field as the maximum
842          *   delay possible
843          */
844         read_lock(&in_dev->mc_list_lock);
845         for (im=in_dev->mc_list; im!=NULL; im=im->next) {
846                 if (group && group != im->multiaddr)
847                         continue;
848                 if (im->multiaddr == IGMP_ALL_HOSTS)
849                         continue;
850                 spin_lock_bh(&im->lock);
851                 if (im->tm_running)
852                         im->gsquery = im->gsquery && mark;
853                 else
854                         im->gsquery = mark;
855                 if (im->gsquery)
856                         igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
857                 spin_unlock_bh(&im->lock);
858                 igmp_mod_timer(im, max_delay);
859         }
860         read_unlock(&in_dev->mc_list_lock);
861 }
862
863 int igmp_rcv(struct sk_buff *skb)
864 {
865         /* This basically follows the spec line by line -- see RFC1112 */
866         struct igmphdr *ih;
867         struct in_device *in_dev = in_dev_get(skb->dev);
868         int len = skb->len;
869
870         if (in_dev==NULL) {
871                 kfree_skb(skb);
872                 return 0;
873         }
874
875         if (!pskb_may_pull(skb, sizeof(struct igmphdr)) || 
876             (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
877                 in_dev_put(in_dev);
878                 kfree_skb(skb);
879                 return 0;
880         }
881
882         ih = skb->h.igmph;
883         switch (ih->type) {
884         case IGMP_HOST_MEMBERSHIP_QUERY:
885                 igmp_heard_query(in_dev, skb, len);
886                 break;
887         case IGMP_HOST_MEMBERSHIP_REPORT:
888         case IGMPV2_HOST_MEMBERSHIP_REPORT:
889         case IGMPV3_HOST_MEMBERSHIP_REPORT:
890                 /* Is it our report looped back? */
891                 if (((struct rtable*)skb->dst)->fl.iif == 0)
892                         break;
893                 igmp_heard_report(in_dev, ih->group);
894                 break;
895         case IGMP_PIM:
896 #ifdef CONFIG_IP_PIMSM_V1
897                 in_dev_put(in_dev);
898                 return pim_rcv_v1(skb);
899 #endif
900         case IGMP_DVMRP:
901         case IGMP_TRACE:
902         case IGMP_HOST_LEAVE_MESSAGE:
903         case IGMP_MTRACE:
904         case IGMP_MTRACE_RESP:
905                 break;
906         default:
907                 NETDEBUG(printk(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type));
908         }
909         in_dev_put(in_dev);
910         kfree_skb(skb);
911         return 0;
912 }
913
914 #endif
915
916
917 /*
918  *      Add a filter to a device
919  */
920
921 static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
922 {
923         char buf[MAX_ADDR_LEN];
924         struct net_device *dev = in_dev->dev;
925
926         /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
927            We will get multicast token leakage, when IFF_MULTICAST
928            is changed. This check should be done in dev->set_multicast_list
929            routine. Something sort of:
930            if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
931            --ANK
932            */
933         if (arp_mc_map(addr, buf, dev, 0) == 0)
934                 dev_mc_add(dev,buf,dev->addr_len,0);
935 }
936
937 /*
938  *      Remove a filter from a device
939  */
940
941 static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
942 {
943         char buf[MAX_ADDR_LEN];
944         struct net_device *dev = in_dev->dev;
945
946         if (arp_mc_map(addr, buf, dev, 0) == 0)
947                 dev_mc_delete(dev,buf,dev->addr_len,0);
948 }
949
950 #ifdef CONFIG_IP_MULTICAST
951 /*
952  * deleted ip_mc_list manipulation
953  */
954 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
955 {
956         struct ip_mc_list *pmc;
957
958         /* this is an "ip_mc_list" for convenience; only the fields below
959          * are actually used. In particular, the refcnt and users are not
960          * used for management of the delete list. Using the same structure
961          * for deleted items allows change reports to use common code with
962          * non-deleted or query-response MCA's.
963          */
964         pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
965         if (!pmc)
966                 return;
967         memset(pmc, 0, sizeof(*pmc));
968         spin_lock_bh(&im->lock);
969         pmc->interface = im->interface;
970         in_dev_hold(in_dev);
971         pmc->multiaddr = im->multiaddr;
972         pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
973                 IGMP_Unsolicited_Report_Count;
974         pmc->sfmode = im->sfmode;
975         if (pmc->sfmode == MCAST_INCLUDE) {
976                 struct ip_sf_list *psf;
977
978                 pmc->tomb = im->tomb;
979                 pmc->sources = im->sources;
980                 im->tomb = im->sources = NULL;
981                 for (psf=pmc->sources; psf; psf=psf->sf_next)
982                         psf->sf_crcount = pmc->crcount;
983         }
984         spin_unlock_bh(&im->lock);
985
986         spin_lock_bh(&in_dev->mc_tomb_lock);
987         pmc->next = in_dev->mc_tomb;
988         in_dev->mc_tomb = pmc;
989         spin_unlock_bh(&in_dev->mc_tomb_lock);
990 }
991
992 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
993 {
994         struct ip_mc_list *pmc, *pmc_prev;
995         struct ip_sf_list *psf, *psf_next;
996
997         spin_lock_bh(&in_dev->mc_tomb_lock);
998         pmc_prev = NULL;
999         for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1000                 if (pmc->multiaddr == multiaddr)
1001                         break;
1002                 pmc_prev = pmc;
1003         }
1004         if (pmc) {
1005                 if (pmc_prev)
1006                         pmc_prev->next = pmc->next;
1007                 else
1008                         in_dev->mc_tomb = pmc->next;
1009         }
1010         spin_unlock_bh(&in_dev->mc_tomb_lock);
1011         if (pmc) {
1012                 for (psf=pmc->tomb; psf; psf=psf_next) {
1013                         psf_next = psf->sf_next;
1014                         kfree(psf);
1015                 }
1016                 in_dev_put(pmc->interface);
1017                 kfree(pmc);
1018         }
1019 }
1020
1021 static void igmpv3_clear_delrec(struct in_device *in_dev)
1022 {
1023         struct ip_mc_list *pmc, *nextpmc;
1024
1025         spin_lock_bh(&in_dev->mc_tomb_lock);
1026         pmc = in_dev->mc_tomb;
1027         in_dev->mc_tomb = NULL;
1028         spin_unlock_bh(&in_dev->mc_tomb_lock);
1029
1030         for (; pmc; pmc = nextpmc) {
1031                 nextpmc = pmc->next;
1032                 ip_mc_clear_src(pmc);
1033                 in_dev_put(pmc->interface);
1034                 kfree(pmc);
1035         }
1036         /* clear dead sources, too */
1037         read_lock(&in_dev->mc_list_lock);
1038         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1039                 struct ip_sf_list *psf, *psf_next;
1040
1041                 spin_lock_bh(&pmc->lock);
1042                 psf = pmc->tomb;
1043                 pmc->tomb = NULL;
1044                 spin_unlock_bh(&pmc->lock);
1045                 for (; psf; psf=psf_next) {
1046                         psf_next = psf->sf_next;
1047                         kfree(psf);
1048                 }
1049         }
1050         read_unlock(&in_dev->mc_list_lock);
1051 }
1052 #endif
1053
1054 static void igmp_group_dropped(struct ip_mc_list *im)
1055 {
1056         struct in_device *in_dev = im->interface;
1057 #ifdef CONFIG_IP_MULTICAST
1058         int reporter;
1059 #endif
1060
1061         if (im->loaded) {
1062                 im->loaded = 0;
1063                 ip_mc_filter_del(in_dev, im->multiaddr);
1064         }
1065
1066 #ifdef CONFIG_IP_MULTICAST
1067         if (im->multiaddr == IGMP_ALL_HOSTS)
1068                 return;
1069
1070         reporter = im->reporter;
1071         igmp_stop_timer(im);
1072
1073         if (!in_dev->dead) {
1074                 if (IGMP_V1_SEEN(in_dev))
1075                         goto done;
1076                 if (IGMP_V2_SEEN(in_dev)) {
1077                         if (reporter)
1078                                 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1079                         goto done;
1080                 }
1081                 /* IGMPv3 */
1082                 igmpv3_add_delrec(in_dev, im);
1083
1084                 igmp_ifc_event(in_dev);
1085         }
1086 done:
1087 #endif
1088         ip_mc_clear_src(im);
1089 }
1090
1091 static void igmp_group_added(struct ip_mc_list *im)
1092 {
1093         struct in_device *in_dev = im->interface;
1094
1095         if (im->loaded == 0) {
1096                 im->loaded = 1;
1097                 ip_mc_filter_add(in_dev, im->multiaddr);
1098         }
1099
1100 #ifdef CONFIG_IP_MULTICAST
1101         if (im->multiaddr == IGMP_ALL_HOSTS)
1102                 return;
1103
1104         if (in_dev->dead)
1105                 return;
1106         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1107                 spin_lock_bh(&im->lock);
1108                 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1109                 spin_unlock_bh(&im->lock);
1110                 return;
1111         }
1112         /* else, v3 */
1113
1114         im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1115                 IGMP_Unsolicited_Report_Count;
1116         igmp_ifc_event(in_dev);
1117 #endif
1118 }
1119
1120
1121 /*
1122  *      Multicast list managers
1123  */
1124
1125
1126 /*
1127  *      A socket has joined a multicast group on device dev.
1128  */
1129
1130 void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1131 {
1132         struct ip_mc_list *im;
1133
1134         ASSERT_RTNL();
1135
1136         for (im=in_dev->mc_list; im; im=im->next) {
1137                 if (im->multiaddr == addr) {
1138                         im->users++;
1139                         ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1140                         goto out;
1141                 }
1142         }
1143
1144         im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1145         if (!im)
1146                 goto out;
1147
1148         im->users=1;
1149         im->interface=in_dev;
1150         in_dev_hold(in_dev);
1151         im->multiaddr=addr;
1152         /* initial mode is (EX, empty) */
1153         im->sfmode = MCAST_EXCLUDE;
1154         im->sfcount[MCAST_INCLUDE] = 0;
1155         im->sfcount[MCAST_EXCLUDE] = 1;
1156         im->sources = NULL;
1157         im->tomb = NULL;
1158         im->crcount = 0;
1159         atomic_set(&im->refcnt, 1);
1160         spin_lock_init(&im->lock);
1161 #ifdef CONFIG_IP_MULTICAST
1162         im->tm_running=0;
1163         init_timer(&im->timer);
1164         im->timer.data=(unsigned long)im;
1165         im->timer.function=&igmp_timer_expire;
1166         im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1167         im->reporter = 0;
1168         im->gsquery = 0;
1169 #endif
1170         im->loaded = 0;
1171         write_lock_bh(&in_dev->mc_list_lock);
1172         im->next=in_dev->mc_list;
1173         in_dev->mc_list=im;
1174         write_unlock_bh(&in_dev->mc_list_lock);
1175 #ifdef CONFIG_IP_MULTICAST
1176         igmpv3_del_delrec(in_dev, im->multiaddr);
1177 #endif
1178         igmp_group_added(im);
1179         if (!in_dev->dead)
1180                 ip_rt_multicast_event(in_dev);
1181 out:
1182         return;
1183 }
1184
1185 /*
1186  *      A socket has left a multicast group on device dev
1187  */
1188
1189 void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1190 {
1191         struct ip_mc_list *i, **ip;
1192         
1193         ASSERT_RTNL();
1194         
1195         for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1196                 if (i->multiaddr==addr) {
1197                         if (--i->users == 0) {
1198                                 write_lock_bh(&in_dev->mc_list_lock);
1199                                 *ip = i->next;
1200                                 write_unlock_bh(&in_dev->mc_list_lock);
1201                                 igmp_group_dropped(i);
1202
1203                                 if (!in_dev->dead)
1204                                         ip_rt_multicast_event(in_dev);
1205
1206                                 ip_ma_put(i);
1207                                 return;
1208                         }
1209                         break;
1210                 }
1211         }
1212 }
1213
1214 /* Device going down */
1215
1216 void ip_mc_down(struct in_device *in_dev)
1217 {
1218         struct ip_mc_list *i;
1219
1220         ASSERT_RTNL();
1221
1222         for (i=in_dev->mc_list; i; i=i->next)
1223                 igmp_group_dropped(i);
1224
1225 #ifdef CONFIG_IP_MULTICAST
1226         in_dev->mr_ifc_count = 0;
1227         if (del_timer(&in_dev->mr_ifc_timer))
1228                 __in_dev_put(in_dev);
1229         in_dev->mr_gq_running = 0;
1230         if (del_timer(&in_dev->mr_gq_timer))
1231                 __in_dev_put(in_dev);
1232         igmpv3_clear_delrec(in_dev);
1233 #endif
1234
1235         ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1236 }
1237
1238 void ip_mc_init_dev(struct in_device *in_dev)
1239 {
1240         ASSERT_RTNL();
1241
1242         in_dev->mc_tomb = NULL;
1243 #ifdef CONFIG_IP_MULTICAST
1244         in_dev->mr_gq_running = 0;
1245         init_timer(&in_dev->mr_gq_timer);
1246         in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1247         in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1248         in_dev->mr_ifc_count = 0;
1249         init_timer(&in_dev->mr_ifc_timer);
1250         in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1251         in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1252         in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1253 #endif
1254
1255         rwlock_init(&in_dev->mc_list_lock);
1256         spin_lock_init(&in_dev->mc_tomb_lock);
1257 }
1258
1259 /* Device going up */
1260
1261 void ip_mc_up(struct in_device *in_dev)
1262 {
1263         struct ip_mc_list *i;
1264
1265         ASSERT_RTNL();
1266
1267         ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1268
1269         for (i=in_dev->mc_list; i; i=i->next)
1270                 igmp_group_added(i);
1271 }
1272
1273 /*
1274  *      Device is about to be destroyed: clean up.
1275  */
1276
1277 void ip_mc_destroy_dev(struct in_device *in_dev)
1278 {
1279         struct ip_mc_list *i;
1280
1281         ASSERT_RTNL();
1282
1283         /* Deactivate timers */
1284         ip_mc_down(in_dev);
1285
1286         write_lock_bh(&in_dev->mc_list_lock);
1287         while ((i = in_dev->mc_list) != NULL) {
1288                 in_dev->mc_list = i->next;
1289                 write_unlock_bh(&in_dev->mc_list_lock);
1290
1291                 igmp_group_dropped(i);
1292                 ip_ma_put(i);
1293
1294                 write_lock_bh(&in_dev->mc_list_lock);
1295         }
1296         write_unlock_bh(&in_dev->mc_list_lock);
1297 }
1298
1299 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1300 {
1301         struct flowi fl = { .nl_u = { .ip4_u =
1302                                       { .daddr = imr->imr_multiaddr.s_addr } } };
1303         struct rtable *rt;
1304         struct net_device *dev = NULL;
1305         struct in_device *idev = NULL;
1306
1307         if (imr->imr_ifindex) {
1308                 idev = inetdev_by_index(imr->imr_ifindex);
1309                 if (idev)
1310                         __in_dev_put(idev);
1311                 return idev;
1312         }
1313         if (imr->imr_address.s_addr) {
1314                 dev = ip_dev_find(imr->imr_address.s_addr);
1315                 if (!dev)
1316                         return NULL;
1317                 __dev_put(dev);
1318         }
1319
1320         if (!dev && !ip_route_output_key(&rt, &fl)) {
1321                 dev = rt->u.dst.dev;
1322                 ip_rt_put(rt);
1323         }
1324         if (dev) {
1325                 imr->imr_ifindex = dev->ifindex;
1326                 idev = __in_dev_get(dev);
1327         }
1328         return idev;
1329 }
1330
1331 /*
1332  *      Join a socket to a group
1333  */
1334 int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1335 int sysctl_igmp_max_msf = IP_MAX_MSF;
1336
1337
1338 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1339         __u32 *psfsrc)
1340 {
1341         struct ip_sf_list *psf, *psf_prev;
1342         int rv = 0;
1343
1344         psf_prev = NULL;
1345         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1346                 if (psf->sf_inaddr == *psfsrc)
1347                         break;
1348                 psf_prev = psf;
1349         }
1350         if (!psf || psf->sf_count[sfmode] == 0) {
1351                 /* source filter not found, or count wrong =>  bug */
1352                 return -ESRCH;
1353         }
1354         psf->sf_count[sfmode]--;
1355         if (psf->sf_count[sfmode] == 0) {
1356                 ip_rt_multicast_event(pmc->interface);
1357         }
1358         if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1359 #ifdef CONFIG_IP_MULTICAST
1360                 struct in_device *in_dev = pmc->interface;
1361 #endif
1362
1363                 /* no more filters for this source */
1364                 if (psf_prev)
1365                         psf_prev->sf_next = psf->sf_next;
1366                 else
1367                         pmc->sources = psf->sf_next;
1368 #ifdef CONFIG_IP_MULTICAST
1369                 if (psf->sf_oldin &&
1370                     !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1371                         psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1372                                 IGMP_Unsolicited_Report_Count;
1373                         psf->sf_next = pmc->tomb;
1374                         pmc->tomb = psf;
1375                         rv = 1;
1376                 } else
1377 #endif
1378                         kfree(psf);
1379         }
1380         return rv;
1381 }
1382
1383 #ifndef CONFIG_IP_MULTICAST
1384 #define igmp_ifc_event(x)       do { } while (0)
1385 #endif
1386
1387 static int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1388                          int sfcount, __u32 *psfsrc, int delta)
1389 {
1390         struct ip_mc_list *pmc;
1391         int     changerec = 0;
1392         int     i, err;
1393
1394         if (!in_dev)
1395                 return -ENODEV;
1396         read_lock(&in_dev->mc_list_lock);
1397         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1398                 if (*pmca == pmc->multiaddr)
1399                         break;
1400         }
1401         if (!pmc) {
1402                 /* MCA not found?? bug */
1403                 read_unlock(&in_dev->mc_list_lock);
1404                 return -ESRCH;
1405         }
1406         spin_lock_bh(&pmc->lock);
1407         read_unlock(&in_dev->mc_list_lock);
1408 #ifdef CONFIG_IP_MULTICAST
1409         sf_markstate(pmc);
1410 #endif
1411         if (!delta) {
1412                 err = -EINVAL;
1413                 if (!pmc->sfcount[sfmode])
1414                         goto out_unlock;
1415                 pmc->sfcount[sfmode]--;
1416         }
1417         err = 0;
1418         for (i=0; i<sfcount; i++) {
1419                 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1420
1421                 changerec |= rv > 0;
1422                 if (!err && rv < 0)
1423                         err = rv;
1424         }
1425         if (pmc->sfmode == MCAST_EXCLUDE &&
1426             pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1427             pmc->sfcount[MCAST_INCLUDE]) {
1428 #ifdef CONFIG_IP_MULTICAST
1429                 struct ip_sf_list *psf;
1430 #endif
1431
1432                 /* filter mode change */
1433                 pmc->sfmode = MCAST_INCLUDE;
1434 #ifdef CONFIG_IP_MULTICAST
1435                 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1436                         IGMP_Unsolicited_Report_Count;
1437                 in_dev->mr_ifc_count = pmc->crcount;
1438                 for (psf=pmc->sources; psf; psf = psf->sf_next)
1439                         psf->sf_crcount = 0;
1440                 igmp_ifc_event(pmc->interface);
1441         } else if (sf_setstate(pmc) || changerec) {
1442                 igmp_ifc_event(pmc->interface);
1443 #endif
1444         }
1445 out_unlock:
1446         spin_unlock_bh(&pmc->lock);
1447         return err;
1448 }
1449
1450 /*
1451  * Add multicast single-source filter to the interface list
1452  */
1453 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1454         __u32 *psfsrc, int delta)
1455 {
1456         struct ip_sf_list *psf, *psf_prev;
1457
1458         psf_prev = NULL;
1459         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1460                 if (psf->sf_inaddr == *psfsrc)
1461                         break;
1462                 psf_prev = psf;
1463         }
1464         if (!psf) {
1465                 psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1466                 if (!psf)
1467                         return -ENOBUFS;
1468                 memset(psf, 0, sizeof(*psf));
1469                 psf->sf_inaddr = *psfsrc;
1470                 if (psf_prev) {
1471                         psf_prev->sf_next = psf;
1472                 } else
1473                         pmc->sources = psf;
1474         }
1475         psf->sf_count[sfmode]++;
1476         if (psf->sf_count[sfmode] == 1) {
1477                 ip_rt_multicast_event(pmc->interface);
1478         }
1479         return 0;
1480 }
1481
1482 #ifdef CONFIG_IP_MULTICAST
1483 static void sf_markstate(struct ip_mc_list *pmc)
1484 {
1485         struct ip_sf_list *psf;
1486         int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1487
1488         for (psf=pmc->sources; psf; psf=psf->sf_next)
1489                 if (pmc->sfcount[MCAST_EXCLUDE]) {
1490                         psf->sf_oldin = mca_xcount ==
1491                                 psf->sf_count[MCAST_EXCLUDE] &&
1492                                 !psf->sf_count[MCAST_INCLUDE];
1493                 } else
1494                         psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1495 }
1496
1497 static int sf_setstate(struct ip_mc_list *pmc)
1498 {
1499         struct ip_sf_list *psf;
1500         int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1501         int qrv = pmc->interface->mr_qrv;
1502         int new_in, rv;
1503
1504         rv = 0;
1505         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1506                 if (pmc->sfcount[MCAST_EXCLUDE]) {
1507                         new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1508                                 !psf->sf_count[MCAST_INCLUDE];
1509                 } else
1510                         new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1511                 if (new_in != psf->sf_oldin) {
1512                         psf->sf_crcount = qrv;
1513                         rv++;
1514                 }
1515         }
1516         return rv;
1517 }
1518 #endif
1519
1520 /*
1521  * Add multicast source filter list to the interface list
1522  */
1523 static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1524                          int sfcount, __u32 *psfsrc, int delta)
1525 {
1526         struct ip_mc_list *pmc;
1527         int     isexclude;
1528         int     i, err;
1529
1530         if (!in_dev)
1531                 return -ENODEV;
1532         read_lock(&in_dev->mc_list_lock);
1533         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1534                 if (*pmca == pmc->multiaddr)
1535                         break;
1536         }
1537         if (!pmc) {
1538                 /* MCA not found?? bug */
1539                 read_unlock(&in_dev->mc_list_lock);
1540                 return -ESRCH;
1541         }
1542         spin_lock_bh(&pmc->lock);
1543         read_unlock(&in_dev->mc_list_lock);
1544
1545 #ifdef CONFIG_IP_MULTICAST
1546         sf_markstate(pmc);
1547 #endif
1548         isexclude = pmc->sfmode == MCAST_EXCLUDE;
1549         if (!delta)
1550                 pmc->sfcount[sfmode]++;
1551         err = 0;
1552         for (i=0; i<sfcount; i++) {
1553                 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1554                 if (err)
1555                         break;
1556         }
1557         if (err) {
1558                 int j;
1559
1560                 pmc->sfcount[sfmode]--;
1561                 for (j=0; j<i; j++)
1562                         (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1563         } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1564 #ifdef CONFIG_IP_MULTICAST
1565                 struct in_device *in_dev = pmc->interface;
1566                 struct ip_sf_list *psf;
1567 #endif
1568
1569                 /* filter mode change */
1570                 if (pmc->sfcount[MCAST_EXCLUDE])
1571                         pmc->sfmode = MCAST_EXCLUDE;
1572                 else if (pmc->sfcount[MCAST_INCLUDE])
1573                         pmc->sfmode = MCAST_INCLUDE;
1574 #ifdef CONFIG_IP_MULTICAST
1575                 /* else no filters; keep old mode for reports */
1576
1577                 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1578                         IGMP_Unsolicited_Report_Count;
1579                 in_dev->mr_ifc_count = pmc->crcount;
1580                 for (psf=pmc->sources; psf; psf = psf->sf_next)
1581                         psf->sf_crcount = 0;
1582                 igmp_ifc_event(in_dev);
1583         } else if (sf_setstate(pmc)) {
1584                 igmp_ifc_event(in_dev);
1585 #endif
1586         }
1587         spin_unlock_bh(&pmc->lock);
1588         return err;
1589 }
1590
1591 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1592 {
1593         struct ip_sf_list *psf, *nextpsf;
1594
1595         for (psf=pmc->tomb; psf; psf=nextpsf) {
1596                 nextpsf = psf->sf_next;
1597                 kfree(psf);
1598         }
1599         pmc->tomb = NULL;
1600         for (psf=pmc->sources; psf; psf=nextpsf) {
1601                 nextpsf = psf->sf_next;
1602                 kfree(psf);
1603         }
1604         pmc->sources = NULL;
1605         pmc->sfmode = MCAST_EXCLUDE;
1606         pmc->sfcount[MCAST_EXCLUDE] = 0;
1607         pmc->sfcount[MCAST_EXCLUDE] = 1;
1608 }
1609
1610
1611 /*
1612  * Join a multicast group
1613  */
1614 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1615 {
1616         int err;
1617         u32 addr = imr->imr_multiaddr.s_addr;
1618         struct ip_mc_socklist *iml=NULL, *i;
1619         struct in_device *in_dev;
1620         struct inet_sock *inet = inet_sk(sk);
1621         int ifindex;
1622         int count = 0;
1623
1624         if (!MULTICAST(addr))
1625                 return -EINVAL;
1626
1627         rtnl_shlock();
1628
1629         in_dev = ip_mc_find_dev(imr);
1630
1631         if (!in_dev) {
1632                 iml = NULL;
1633                 err = -ENODEV;
1634                 goto done;
1635         }
1636
1637         err = -EADDRINUSE;
1638         ifindex = imr->imr_ifindex;
1639         for (i = inet->mc_list; i; i = i->next) {
1640                 if (i->multi.imr_multiaddr.s_addr == addr &&
1641                     i->multi.imr_ifindex == ifindex)
1642                         goto done;
1643                 count++;
1644         }
1645         err = -ENOBUFS;
1646         if (count >= sysctl_igmp_max_memberships)
1647                 goto done;
1648         iml = (struct ip_mc_socklist *)sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1649         if (iml == NULL)
1650                 goto done;
1651
1652         memcpy(&iml->multi, imr, sizeof(*imr));
1653         iml->next = inet->mc_list;
1654         iml->sflist = NULL;
1655         iml->sfmode = MCAST_EXCLUDE;
1656         inet->mc_list = iml;
1657         ip_mc_inc_group(in_dev, addr);
1658         err = 0;
1659 done:
1660         rtnl_shunlock();
1661         return err;
1662 }
1663
1664 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1665                            struct in_device *in_dev)
1666 {
1667         int err;
1668
1669         if (iml->sflist == 0) {
1670                 /* any-source empty exclude case */
1671                 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1672                         iml->sfmode, 0, NULL, 0);
1673         }
1674         err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1675                         iml->sfmode, iml->sflist->sl_count,
1676                         iml->sflist->sl_addr, 0);
1677         sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1678         iml->sflist = NULL;
1679         return err;
1680 }
1681
1682 /*
1683  *      Ask a socket to leave a group.
1684  */
1685
1686 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1687 {
1688         struct inet_sock *inet = inet_sk(sk);
1689         struct ip_mc_socklist *iml, **imlp;
1690
1691         rtnl_lock();
1692         for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1693                 if (iml->multi.imr_multiaddr.s_addr==imr->imr_multiaddr.s_addr &&
1694                     iml->multi.imr_address.s_addr==imr->imr_address.s_addr &&
1695                     (!imr->imr_ifindex || iml->multi.imr_ifindex==imr->imr_ifindex)) {
1696                         struct in_device *in_dev;
1697
1698                         in_dev = inetdev_by_index(iml->multi.imr_ifindex);
1699                         if (in_dev)
1700                                 (void) ip_mc_leave_src(sk, iml, in_dev);
1701
1702                         *imlp = iml->next;
1703
1704                         if (in_dev) {
1705                                 ip_mc_dec_group(in_dev, imr->imr_multiaddr.s_addr);
1706                                 in_dev_put(in_dev);
1707                         }
1708                         rtnl_unlock();
1709                         sock_kfree_s(sk, iml, sizeof(*iml));
1710                         return 0;
1711                 }
1712         }
1713         rtnl_unlock();
1714         return -EADDRNOTAVAIL;
1715 }
1716
1717 int ip_mc_source(int add, int omode, struct sock *sk, struct
1718         ip_mreq_source *mreqs, int ifindex)
1719 {
1720         int err;
1721         struct ip_mreqn imr;
1722         u32 addr = mreqs->imr_multiaddr;
1723         struct ip_mc_socklist *pmc;
1724         struct in_device *in_dev = NULL;
1725         struct inet_sock *inet = inet_sk(sk);
1726         struct ip_sf_socklist *psl;
1727         int i, j, rv;
1728
1729         if (!MULTICAST(addr))
1730                 return -EINVAL;
1731
1732         rtnl_shlock();
1733
1734         imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1735         imr.imr_address.s_addr = mreqs->imr_interface;
1736         imr.imr_ifindex = ifindex;
1737         in_dev = ip_mc_find_dev(&imr);
1738
1739         if (!in_dev) {
1740                 err = -ENODEV;
1741                 goto done;
1742         }
1743         err = -EADDRNOTAVAIL;
1744
1745         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1746                 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1747                     && pmc->multi.imr_ifindex == imr.imr_ifindex)
1748                         break;
1749         }
1750         if (!pmc)               /* must have a prior join */
1751                 goto done;
1752         /* if a source filter was set, must be the same mode as before */
1753         if (pmc->sflist) {
1754                 if (pmc->sfmode != omode)
1755                         goto done;
1756         } else if (pmc->sfmode != omode) {
1757                 /* allow mode switches for empty-set filters */
1758                 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1759                 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 
1760                         NULL, 0);
1761                 pmc->sfmode = omode;
1762         }
1763
1764         psl = pmc->sflist;
1765         if (!add) {
1766                 if (!psl)
1767                         goto done;
1768                 rv = !0;
1769                 for (i=0; i<psl->sl_count; i++) {
1770                         rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1771                                 sizeof(__u32));
1772                         if (rv == 0)
1773                                 break;
1774                 }
1775                 if (rv)         /* source not found */
1776                         goto done;
1777
1778                 /* update the interface filter */
1779                 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 
1780                         &mreqs->imr_sourceaddr, 1);
1781
1782                 for (j=i+1; j<psl->sl_count; j++)
1783                         psl->sl_addr[j-1] = psl->sl_addr[j];
1784                 psl->sl_count--;
1785                 err = 0;
1786                 goto done;
1787         }
1788         /* else, add a new source to the filter */
1789
1790         if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1791                 err = -ENOBUFS;
1792                 goto done;
1793         }
1794         if (!psl || psl->sl_count == psl->sl_max) {
1795                 struct ip_sf_socklist *newpsl;
1796                 int count = IP_SFBLOCK;
1797
1798                 if (psl)
1799                         count += psl->sl_max;
1800                 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1801                         IP_SFLSIZE(count), GFP_KERNEL);
1802                 if (!newpsl) {
1803                         err = -ENOBUFS;
1804                         goto done;
1805                 }
1806                 newpsl->sl_max = count;
1807                 newpsl->sl_count = count - IP_SFBLOCK;
1808                 if (psl) {
1809                         for (i=0; i<psl->sl_count; i++)
1810                                 newpsl->sl_addr[i] = psl->sl_addr[i];
1811                         sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1812                 }
1813                 pmc->sflist = psl = newpsl;
1814         }
1815         rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1816         for (i=0; i<psl->sl_count; i++) {
1817                 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1818                         sizeof(__u32));
1819                 if (rv == 0)
1820                         break;
1821         }
1822         if (rv == 0)            /* address already there is an error */
1823                 goto done;
1824         for (j=psl->sl_count-1; j>=i; j--)
1825                 psl->sl_addr[j+1] = psl->sl_addr[j];
1826         psl->sl_addr[i] = mreqs->imr_sourceaddr;
1827         psl->sl_count++;
1828         err = 0;
1829         /* update the interface list */
1830         ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 
1831                 &mreqs->imr_sourceaddr, 1);
1832 done:
1833         rtnl_shunlock();
1834         return err;
1835 }
1836
1837 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1838 {
1839         int err;
1840         struct ip_mreqn imr;
1841         u32 addr = msf->imsf_multiaddr;
1842         struct ip_mc_socklist *pmc;
1843         struct in_device *in_dev;
1844         struct inet_sock *inet = inet_sk(sk);
1845         struct ip_sf_socklist *newpsl, *psl;
1846
1847         if (!MULTICAST(addr))
1848                 return -EINVAL;
1849         if (msf->imsf_fmode != MCAST_INCLUDE &&
1850             msf->imsf_fmode != MCAST_EXCLUDE)
1851                 return -EINVAL;
1852
1853         rtnl_shlock();
1854
1855         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1856         imr.imr_address.s_addr = msf->imsf_interface;
1857         imr.imr_ifindex = ifindex;
1858         in_dev = ip_mc_find_dev(&imr);
1859
1860         if (!in_dev) {
1861                 err = -ENODEV;
1862                 goto done;
1863         }
1864         err = -EADDRNOTAVAIL;
1865
1866         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1867                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1868                     pmc->multi.imr_ifindex == imr.imr_ifindex)
1869                         break;
1870         }
1871         if (!pmc)               /* must have a prior join */
1872                 goto done;
1873         if (msf->imsf_numsrc) {
1874                 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1875                                 IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1876                 if (!newpsl) {
1877                         err = -ENOBUFS;
1878                         goto done;
1879                 }
1880                 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1881                 memcpy(newpsl->sl_addr, msf->imsf_slist,
1882                         msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1883                 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1884                         msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1885                 if (err) {
1886                         sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1887                         goto done;
1888                 }
1889         } else
1890                 newpsl = NULL;
1891         psl = pmc->sflist;
1892         if (psl) {
1893                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1894                         psl->sl_count, psl->sl_addr, 0);
1895                 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1896         } else
1897                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1898                         0, NULL, 0);
1899         pmc->sflist = newpsl;
1900         pmc->sfmode = msf->imsf_fmode;
1901 done:
1902         rtnl_shunlock();
1903         return err;
1904 }
1905
1906 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1907         struct ip_msfilter __user *optval, int __user *optlen)
1908 {
1909         int err, len, count, copycount;
1910         struct ip_mreqn imr;
1911         u32 addr = msf->imsf_multiaddr;
1912         struct ip_mc_socklist *pmc;
1913         struct in_device *in_dev;
1914         struct inet_sock *inet = inet_sk(sk);
1915         struct ip_sf_socklist *psl;
1916
1917         if (!MULTICAST(addr))
1918                 return -EINVAL;
1919
1920         rtnl_shlock();
1921
1922         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1923         imr.imr_address.s_addr = msf->imsf_interface;
1924         imr.imr_ifindex = 0;
1925         in_dev = ip_mc_find_dev(&imr);
1926
1927         if (!in_dev) {
1928                 err = -ENODEV;
1929                 goto done;
1930         }
1931         err = -EADDRNOTAVAIL;
1932
1933         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1934                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1935                     pmc->multi.imr_ifindex == imr.imr_ifindex)
1936                         break;
1937         }
1938         if (!pmc)               /* must have a prior join */
1939                 goto done;
1940         msf->imsf_fmode = pmc->sfmode;
1941         psl = pmc->sflist;
1942         rtnl_shunlock();
1943         if (!psl) {
1944                 len = 0;
1945                 count = 0;
1946         } else {
1947                 count = psl->sl_count;
1948         }
1949         copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1950         len = copycount * sizeof(psl->sl_addr[0]);
1951         msf->imsf_numsrc = count;
1952         if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1953             copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
1954                 return -EFAULT;
1955         }
1956         if (len &&
1957             copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
1958                 return -EFAULT;
1959         return 0;
1960 done:
1961         rtnl_shunlock();
1962         return err;
1963 }
1964
1965 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
1966         struct group_filter __user *optval, int __user *optlen)
1967 {
1968         int err, i, count, copycount;
1969         struct sockaddr_in *psin;
1970         u32 addr;
1971         struct ip_mc_socklist *pmc;
1972         struct inet_sock *inet = inet_sk(sk);
1973         struct ip_sf_socklist *psl;
1974
1975         psin = (struct sockaddr_in *)&gsf->gf_group;
1976         if (psin->sin_family != AF_INET)
1977                 return -EINVAL;
1978         addr = psin->sin_addr.s_addr;
1979         if (!MULTICAST(addr))
1980                 return -EINVAL;
1981
1982         rtnl_shlock();
1983
1984         err = -EADDRNOTAVAIL;
1985
1986         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1987                 if (pmc->multi.imr_multiaddr.s_addr == addr &&
1988                     pmc->multi.imr_ifindex == gsf->gf_interface)
1989                         break;
1990         }
1991         if (!pmc)               /* must have a prior join */
1992                 goto done;
1993         gsf->gf_fmode = pmc->sfmode;
1994         psl = pmc->sflist;
1995         rtnl_shunlock();
1996         count = psl ? psl->sl_count : 0;
1997         copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
1998         gsf->gf_numsrc = count;
1999         if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2000             copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2001                 return -EFAULT;
2002         }
2003         for (i=0; i<copycount; i++) {
2004                 struct sockaddr_in *psin;
2005                 struct sockaddr_storage ss;
2006
2007                 psin = (struct sockaddr_in *)&ss;
2008                 memset(&ss, 0, sizeof(ss));
2009                 psin->sin_family = AF_INET;
2010                 psin->sin_addr.s_addr = psl->sl_addr[i];
2011                 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2012                         return -EFAULT;
2013         }
2014         return 0;
2015 done:
2016         rtnl_shunlock();
2017         return err;
2018 }
2019
2020 /*
2021  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2022  */
2023 int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2024 {
2025         struct inet_sock *inet = inet_sk(sk);
2026         struct ip_mc_socklist *pmc;
2027         struct ip_sf_socklist *psl;
2028         int i;
2029
2030         if (!MULTICAST(loc_addr))
2031                 return 1;
2032
2033         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2034                 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2035                     pmc->multi.imr_ifindex == dif)
2036                         break;
2037         }
2038         if (!pmc)
2039                 return 1;
2040         psl = pmc->sflist;
2041         if (!psl)
2042                 return pmc->sfmode == MCAST_EXCLUDE;
2043
2044         for (i=0; i<psl->sl_count; i++) {
2045                 if (psl->sl_addr[i] == rmt_addr)
2046                         break;
2047         }
2048         if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2049                 return 0;
2050         if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2051                 return 0;
2052         return 1;
2053 }
2054
2055 /*
2056  *      A socket is closing.
2057  */
2058
2059 void ip_mc_drop_socket(struct sock *sk)
2060 {
2061         struct inet_sock *inet = inet_sk(sk);
2062         struct ip_mc_socklist *iml;
2063
2064         if (inet->mc_list == NULL)
2065                 return;
2066
2067         rtnl_lock();
2068         while ((iml = inet->mc_list) != NULL) {
2069                 struct in_device *in_dev;
2070                 inet->mc_list = iml->next;
2071
2072                 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2073                         (void) ip_mc_leave_src(sk, iml, in_dev);
2074                         ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2075                         in_dev_put(in_dev);
2076                 }
2077                 sock_kfree_s(sk, iml, sizeof(*iml));
2078
2079         }
2080         rtnl_unlock();
2081 }
2082
2083 int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2084 {
2085         struct ip_mc_list *im;
2086         struct ip_sf_list *psf;
2087         int rv = 0;
2088
2089         read_lock(&in_dev->mc_list_lock);
2090         for (im=in_dev->mc_list; im; im=im->next) {
2091                 if (im->multiaddr == mc_addr)
2092                         break;
2093         }
2094         if (im && proto == IPPROTO_IGMP) {
2095                 rv = 1;
2096         } else if (im) {
2097                 if (src_addr) {
2098                         for (psf=im->sources; psf; psf=psf->sf_next) {
2099                                 if (psf->sf_inaddr == src_addr)
2100                                         break;
2101                         }
2102                         if (psf)
2103                                 rv = psf->sf_count[MCAST_INCLUDE] ||
2104                                         psf->sf_count[MCAST_EXCLUDE] !=
2105                                         im->sfcount[MCAST_EXCLUDE];
2106                         else
2107                                 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2108                 } else
2109                         rv = 1; /* unspecified source; tentatively allow */
2110         }
2111         read_unlock(&in_dev->mc_list_lock);
2112         return rv;
2113 }
2114
2115 #if defined(CONFIG_PROC_FS)
2116 struct igmp_mc_iter_state {
2117         struct net_device *dev;
2118         struct in_device *in_dev;
2119 };
2120
2121 #define igmp_mc_seq_private(seq)        ((struct igmp_mc_iter_state *)(seq)->private)
2122
2123 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2124 {
2125         struct ip_mc_list *im = NULL;
2126         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2127
2128         for (state->dev = dev_base, state->in_dev = NULL;
2129              state->dev; 
2130              state->dev = state->dev->next) {
2131                 struct in_device *in_dev;
2132                 in_dev = in_dev_get(state->dev);
2133                 if (!in_dev)
2134                         continue;
2135                 read_lock(&in_dev->mc_list_lock);
2136                 im = in_dev->mc_list;
2137                 if (im) {
2138                         state->in_dev = in_dev;
2139                         break;
2140                 }
2141                 read_unlock(&in_dev->mc_list_lock);
2142                 in_dev_put(in_dev);
2143         }
2144         return im;
2145 }
2146
2147 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2148 {
2149         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2150         im = im->next;
2151         while (!im) {
2152                 if (likely(state->in_dev != NULL)) {
2153                         read_unlock(&state->in_dev->mc_list_lock);
2154                         in_dev_put(state->in_dev);
2155                 }
2156                 state->dev = state->dev->next;
2157                 if (!state->dev) {
2158                         state->in_dev = NULL;
2159                         break;
2160                 }
2161                 state->in_dev = in_dev_get(state->dev);
2162                 if (!state->in_dev)
2163                         continue;
2164                 read_lock(&state->in_dev->mc_list_lock);
2165                 im = state->in_dev->mc_list;
2166         }
2167         return im;
2168 }
2169
2170 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2171 {
2172         struct ip_mc_list *im = igmp_mc_get_first(seq);
2173         if (im)
2174                 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2175                         --pos;
2176         return pos ? NULL : im;
2177 }
2178
2179 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2180 {
2181         read_lock(&dev_base_lock);
2182         return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2183 }
2184
2185 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2186 {
2187         struct ip_mc_list *im;
2188         if (v == SEQ_START_TOKEN)
2189                 im = igmp_mc_get_first(seq);
2190         else
2191                 im = igmp_mc_get_next(seq, v);
2192         ++*pos;
2193         return im;
2194 }
2195
2196 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2197 {
2198         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2199         if (likely(state->in_dev != NULL)) {
2200                 read_unlock(&state->in_dev->mc_list_lock);
2201                 in_dev_put(state->in_dev);
2202                 state->in_dev = NULL;
2203         }
2204         state->dev = NULL;
2205         read_unlock(&dev_base_lock);
2206 }
2207
2208 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2209 {
2210         if (v == SEQ_START_TOKEN)
2211                 seq_puts(seq, 
2212                          "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2213         else {
2214                 struct ip_mc_list *im = (struct ip_mc_list *)v;
2215                 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2216                 char   *querier;
2217 #ifdef CONFIG_IP_MULTICAST
2218                 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2219                           IGMP_V2_SEEN(state->in_dev) ? "V2" :
2220                           "V3";
2221 #else
2222                 querier = "NONE";
2223 #endif
2224
2225                 if (state->in_dev->mc_list == im) {
2226                         seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2227                                    state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2228                 }
2229
2230                 seq_printf(seq,
2231                            "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2232                            im->multiaddr, im->users,
2233                            im->tm_running, im->tm_running ?
2234                            jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2235                            im->reporter);
2236         }
2237         return 0;
2238 }
2239
2240 static struct seq_operations igmp_mc_seq_ops = {
2241         .start  =       igmp_mc_seq_start,
2242         .next   =       igmp_mc_seq_next,
2243         .stop   =       igmp_mc_seq_stop,
2244         .show   =       igmp_mc_seq_show,
2245 };
2246
2247 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2248 {
2249         struct seq_file *seq;
2250         int rc = -ENOMEM;
2251         struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2252
2253         if (!s)
2254                 goto out;
2255         rc = seq_open(file, &igmp_mc_seq_ops);
2256         if (rc)
2257                 goto out_kfree;
2258
2259         seq = file->private_data;
2260         seq->private = s;
2261         memset(s, 0, sizeof(*s));
2262 out:
2263         return rc;
2264 out_kfree:
2265         kfree(s);
2266         goto out;
2267 }
2268
2269 static struct file_operations igmp_mc_seq_fops = {
2270         .owner          =       THIS_MODULE,
2271         .open           =       igmp_mc_seq_open,
2272         .read           =       seq_read,
2273         .llseek         =       seq_lseek,
2274         .release        =       seq_release_private,
2275 };
2276
2277 struct igmp_mcf_iter_state {
2278         struct net_device *dev;
2279         struct in_device *idev;
2280         struct ip_mc_list *im;
2281 };
2282
2283 #define igmp_mcf_seq_private(seq)       ((struct igmp_mcf_iter_state *)(seq)->private)
2284
2285 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2286 {
2287         struct ip_sf_list *psf = NULL;
2288         struct ip_mc_list *im = NULL;
2289         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2290
2291         for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2292              state->dev; 
2293              state->dev = state->dev->next) {
2294                 struct in_device *idev;
2295                 idev = in_dev_get(state->dev);
2296                 if (unlikely(idev == NULL))
2297                         continue;
2298                 read_lock(&idev->mc_list_lock);
2299                 im = idev->mc_list;
2300                 if (likely(im != NULL)) {
2301                         spin_lock_bh(&im->lock);
2302                         psf = im->sources;
2303                         if (likely(psf != NULL)) {
2304                                 state->im = im;
2305                                 state->idev = idev;
2306                                 break;
2307                         }
2308                         spin_unlock_bh(&im->lock);
2309                 }
2310                 read_unlock(&idev->mc_list_lock);
2311                 in_dev_put(idev);
2312         }
2313         return psf;
2314 }
2315
2316 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2317 {
2318         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2319
2320         psf = psf->sf_next;
2321         while (!psf) {
2322                 spin_unlock_bh(&state->im->lock);
2323                 state->im = state->im->next;
2324                 while (!state->im) {
2325                         if (likely(state->idev != NULL)) {
2326                                 read_unlock(&state->idev->mc_list_lock);
2327                                 in_dev_put(state->idev);
2328                         }
2329                         state->dev = state->dev->next;
2330                         if (!state->dev) {
2331                                 state->idev = NULL;
2332                                 goto out;
2333                         }
2334                         state->idev = in_dev_get(state->dev);
2335                         if (!state->idev)
2336                                 continue;
2337                         read_lock(&state->idev->mc_list_lock);
2338                         state->im = state->idev->mc_list;
2339                 }
2340                 if (!state->im)
2341                         break;
2342                 spin_lock_bh(&state->im->lock);
2343                 psf = state->im->sources;
2344         }
2345 out:
2346         return psf;
2347 }
2348
2349 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2350 {
2351         struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2352         if (psf)
2353                 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2354                         --pos;
2355         return pos ? NULL : psf;
2356 }
2357
2358 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2359 {
2360         read_lock(&dev_base_lock);
2361         return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2362 }
2363
2364 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2365 {
2366         struct ip_sf_list *psf;
2367         if (v == SEQ_START_TOKEN)
2368                 psf = igmp_mcf_get_first(seq);
2369         else
2370                 psf = igmp_mcf_get_next(seq, v);
2371         ++*pos;
2372         return psf;
2373 }
2374
2375 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2376 {
2377         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2378         if (likely(state->im != NULL)) {
2379                 spin_unlock_bh(&state->im->lock);
2380                 state->im = NULL;
2381         }
2382         if (likely(state->idev != NULL)) {
2383                 read_unlock(&state->idev->mc_list_lock);
2384                 in_dev_put(state->idev);
2385                 state->idev = NULL;
2386         }
2387         state->dev = NULL;
2388         read_unlock(&dev_base_lock);
2389 }
2390
2391 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2392 {
2393         struct ip_sf_list *psf = (struct ip_sf_list *)v;
2394         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2395
2396         if (v == SEQ_START_TOKEN) {
2397                 seq_printf(seq, 
2398                            "%3s %6s "
2399                            "%10s %10s %6s %6s\n", "Idx",
2400                            "Device", "MCA",
2401                            "SRC", "INC", "EXC");
2402         } else {
2403                 seq_printf(seq,
2404                            "%3d %6.6s 0x%08x "
2405                            "0x%08x %6lu %6lu\n", 
2406                            state->dev->ifindex, state->dev->name, 
2407                            ntohl(state->im->multiaddr),
2408                            ntohl(psf->sf_inaddr),
2409                            psf->sf_count[MCAST_INCLUDE],
2410                            psf->sf_count[MCAST_EXCLUDE]);
2411         }
2412         return 0;
2413 }
2414
2415 static struct seq_operations igmp_mcf_seq_ops = {
2416         .start  =       igmp_mcf_seq_start,
2417         .next   =       igmp_mcf_seq_next,
2418         .stop   =       igmp_mcf_seq_stop,
2419         .show   =       igmp_mcf_seq_show,
2420 };
2421
2422 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2423 {
2424         struct seq_file *seq;
2425         int rc = -ENOMEM;
2426         struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2427
2428         if (!s)
2429                 goto out;
2430         rc = seq_open(file, &igmp_mcf_seq_ops);
2431         if (rc)
2432                 goto out_kfree;
2433
2434         seq = file->private_data;
2435         seq->private = s;
2436         memset(s, 0, sizeof(*s));
2437 out:
2438         return rc;
2439 out_kfree:
2440         kfree(s);
2441         goto out;
2442 }
2443
2444 static struct file_operations igmp_mcf_seq_fops = {
2445         .owner          =       THIS_MODULE,
2446         .open           =       igmp_mcf_seq_open,
2447         .read           =       seq_read,
2448         .llseek         =       seq_lseek,
2449         .release        =       seq_release_private,
2450 };
2451
2452 int __init igmp_mc_proc_init(void)
2453 {
2454         proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2455         proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2456         return 0;
2457 }
2458 #endif
2459
2460 EXPORT_SYMBOL(ip_mc_dec_group);
2461 EXPORT_SYMBOL(ip_mc_inc_group);
2462 EXPORT_SYMBOL(ip_mc_join_group);