add added files
[bcm963xx.git] / kernel / linux / 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 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->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->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->lock);
545         write_lock_bh(&in_dev->mc_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         write_unlock_bh(&in_dev->mc_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->lock);
605         if (!skb)
606                 return;
607         (void) igmpv3_sendpack(skb);
608 }
609
610 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
611         int type)
612 {
613         struct sk_buff *skb;
614         struct iphdr *iph;
615         struct igmphdr *ih;
616         struct rtable *rt;
617         struct net_device *dev = in_dev->dev;
618         u32     group = pmc ? pmc->multiaddr : 0;
619         u32     dst;
620
621         if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
622                 return igmpv3_send_report(in_dev, pmc);
623         else if (type == IGMP_HOST_LEAVE_MESSAGE)
624                 dst = IGMP_ALL_ROUTER;
625         else
626                 dst = group;
627
628         {
629                 struct flowi fl = { .oif = dev->ifindex,
630                                     .nl_u = { .ip4_u = { .daddr = dst } },
631                                     .proto = IPPROTO_IGMP };
632                 if (ip_route_output_key(&rt, &fl))
633                         return -1;
634         }
635         if (rt->rt_src == 0) {
636                 ip_rt_put(rt);
637                 return -1;
638         }
639
640         skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
641         if (skb == NULL) {
642                 ip_rt_put(rt);
643                 return -1;
644         }
645
646         skb->dst = &rt->u.dst;
647
648         skb_reserve(skb, LL_RESERVED_SPACE(dev));
649
650         skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
651
652         iph->version  = 4;
653         iph->ihl      = (sizeof(struct iphdr)+4)>>2;
654         iph->tos      = 0xc0;
655         iph->frag_off = htons(IP_DF);
656         iph->ttl      = 1;
657         iph->daddr    = dst;
658         iph->saddr    = rt->rt_src;
659         iph->protocol = IPPROTO_IGMP;
660         iph->tot_len  = htons(IGMP_SIZE);
661         ip_select_ident(iph, &rt->u.dst, NULL);
662         ((u8*)&iph[1])[0] = IPOPT_RA;
663         ((u8*)&iph[1])[1] = 4;
664         ((u8*)&iph[1])[2] = 0;
665         ((u8*)&iph[1])[3] = 0;
666         ip_send_check(iph);
667
668         ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
669         ih->type=type;
670         ih->code=0;
671         ih->csum=0;
672         ih->group=group;
673         ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
674
675         return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
676                        dst_output);
677 }
678
679 static void igmp_gq_timer_expire(unsigned long data)
680 {
681         struct in_device *in_dev = (struct in_device *)data;
682
683         in_dev->mr_gq_running = 0;
684         igmpv3_send_report(in_dev, NULL);
685         __in_dev_put(in_dev);
686 }
687
688 static void igmp_ifc_timer_expire(unsigned long data)
689 {
690         struct in_device *in_dev = (struct in_device *)data;
691
692         igmpv3_send_cr(in_dev);
693         if (in_dev->mr_ifc_count) {
694                 in_dev->mr_ifc_count--;
695                 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
696         }
697         __in_dev_put(in_dev);
698 }
699
700 static void igmp_ifc_event(struct in_device *in_dev)
701 {
702         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
703                 return;
704         in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv : 
705                 IGMP_Unsolicited_Report_Count;
706         igmp_ifc_start_timer(in_dev, 1);
707 }
708
709
710 static void igmp_timer_expire(unsigned long data)
711 {
712         struct ip_mc_list *im=(struct ip_mc_list *)data;
713         struct in_device *in_dev = im->interface;
714
715         spin_lock(&im->lock);
716         im->tm_running=0;
717
718         if (im->unsolicit_count) {
719                 im->unsolicit_count--;
720                 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
721         }
722         im->reporter = 1;
723         spin_unlock(&im->lock);
724
725         if (IGMP_V1_SEEN(in_dev))
726                 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
727         else if (IGMP_V2_SEEN(in_dev))
728                 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
729         else
730                 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
731
732         ip_ma_put(im);
733 }
734
735 static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
736 {
737         struct ip_sf_list *psf;
738         int i, scount;
739
740         scount = 0;
741         for (psf=pmc->sources; psf; psf=psf->sf_next) {
742                 if (scount == nsrcs)
743                         break;
744                 for (i=0; i<nsrcs; i++)
745                         if (srcs[i] == psf->sf_inaddr) {
746                                 psf->sf_gsresp = 1;
747                                 scount++;
748                                 break;
749                         }
750         }
751 }
752
753 static void igmp_heard_report(struct in_device *in_dev, u32 group)
754 {
755         struct ip_mc_list *im;
756
757         /* Timers are only set for non-local groups */
758
759         if (group == IGMP_ALL_HOSTS)
760                 return;
761
762         read_lock(&in_dev->lock);
763         for (im=in_dev->mc_list; im!=NULL; im=im->next) {
764                 if (im->multiaddr == group) {
765                         igmp_stop_timer(im);
766                         break;
767                 }
768         }
769         read_unlock(&in_dev->lock);
770 }
771
772 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
773         int len)
774 {
775         struct igmphdr          *ih = skb->h.igmph;
776         struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
777         struct ip_mc_list       *im;
778         u32                     group = ih->group;
779         int                     max_delay;
780         int                     mark = 0;
781
782
783         if (len == 8) {
784                 if (ih->code == 0) {
785                         /* Alas, old v1 router presents here. */
786         
787                         max_delay = IGMP_Query_Response_Interval;
788                         in_dev->mr_v1_seen = jiffies +
789                                 IGMP_V1_Router_Present_Timeout;
790                         group = 0;
791                 } else {
792                         /* v2 router present */
793                         max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
794                         in_dev->mr_v2_seen = jiffies +
795                                 IGMP_V2_Router_Present_Timeout;
796                 }
797                 /* cancel the interface change timer */
798                 in_dev->mr_ifc_count = 0;
799                 if (del_timer(&in_dev->mr_ifc_timer))
800                         __in_dev_put(in_dev);
801                 /* clear deleted report items */
802                 igmpv3_clear_delrec(in_dev);
803         } else if (len < 12) {
804                 return; /* ignore bogus packet; freed by caller */
805         } else { /* v3 */
806                 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
807                         return;
808                 
809                 ih3 = (struct igmpv3_query *) skb->h.raw;
810                 if (ih3->nsrcs) {
811                         if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 
812                                            + ntohs(ih3->nsrcs)*sizeof(__u32)))
813                                 return;
814                         ih3 = (struct igmpv3_query *) skb->h.raw;
815                 }
816
817                 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
818                 if (!max_delay)
819                         max_delay = 1;  /* can't mod w/ 0 */
820                 in_dev->mr_maxdelay = max_delay;
821                 if (ih3->qrv)
822                         in_dev->mr_qrv = ih3->qrv;
823                 if (!group) { /* general query */
824                         if (ih3->nsrcs)
825                                 return; /* no sources allowed */
826                         igmp_gq_start_timer(in_dev);
827                         return;
828                 }
829                 /* mark sources to include, if group & source-specific */
830                 mark = ih3->nsrcs != 0;
831         }
832
833         /*
834          * - Start the timers in all of our membership records
835          *   that the query applies to for the interface on
836          *   which the query arrived excl. those that belong
837          *   to a "local" group (224.0.0.X)
838          * - For timers already running check if they need to
839          *   be reset.
840          * - Use the igmp->igmp_code field as the maximum
841          *   delay possible
842          */
843         read_lock(&in_dev->lock);
844         for (im=in_dev->mc_list; im!=NULL; im=im->next) {
845                 if (group && group != im->multiaddr)
846                         continue;
847                 if (im->multiaddr == IGMP_ALL_HOSTS)
848                         continue;
849                 spin_lock_bh(&im->lock);
850                 if (im->tm_running)
851                         im->gsquery = im->gsquery && mark;
852                 else
853                         im->gsquery = mark;
854                 if (im->gsquery)
855                         igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
856                 spin_unlock_bh(&im->lock);
857                 igmp_mod_timer(im, max_delay);
858         }
859         read_unlock(&in_dev->lock);
860 }
861
862 int igmp_rcv(struct sk_buff *skb)
863 {
864         /* This basically follows the spec line by line -- see RFC1112 */
865         struct igmphdr *ih;
866         struct in_device *in_dev = in_dev_get(skb->dev);
867         int len = skb->len;
868
869         if (in_dev==NULL) {
870                 kfree_skb(skb);
871                 return 0;
872         }
873
874         if (!pskb_may_pull(skb, sizeof(struct igmphdr)) || 
875             (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
876                 in_dev_put(in_dev);
877                 kfree_skb(skb);
878                 return 0;
879         }
880
881         ih = skb->h.igmph;
882         switch (ih->type) {
883         case IGMP_HOST_MEMBERSHIP_QUERY:
884                 igmp_heard_query(in_dev, skb, len);
885                 break;
886         case IGMP_HOST_MEMBERSHIP_REPORT:
887         case IGMPV2_HOST_MEMBERSHIP_REPORT:
888         case IGMPV3_HOST_MEMBERSHIP_REPORT:
889                 /* Is it our report looped back? */
890                 if (((struct rtable*)skb->dst)->fl.iif == 0)
891                         break;
892                 igmp_heard_report(in_dev, ih->group);
893                 break;
894         case IGMP_PIM:
895 #ifdef CONFIG_IP_PIMSM_V1
896                 in_dev_put(in_dev);
897                 return pim_rcv_v1(skb);
898 #endif
899         case IGMP_DVMRP:
900         case IGMP_TRACE:
901         case IGMP_HOST_LEAVE_MESSAGE:
902         case IGMP_MTRACE:
903         case IGMP_MTRACE_RESP:
904                 break;
905         default:
906                 NETDEBUG(printk(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type));
907         }
908         in_dev_put(in_dev);
909         kfree_skb(skb);
910         return 0;
911 }
912
913 #endif
914
915
916 /*
917  *      Add a filter to a device
918  */
919
920 static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
921 {
922         char buf[MAX_ADDR_LEN];
923         struct net_device *dev = in_dev->dev;
924
925         /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
926            We will get multicast token leakage, when IFF_MULTICAST
927            is changed. This check should be done in dev->set_multicast_list
928            routine. Something sort of:
929            if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
930            --ANK
931            */
932         if (arp_mc_map(addr, buf, dev, 0) == 0)
933                 dev_mc_add(dev,buf,dev->addr_len,0);
934 }
935
936 /*
937  *      Remove a filter from a device
938  */
939
940 static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
941 {
942         char buf[MAX_ADDR_LEN];
943         struct net_device *dev = in_dev->dev;
944
945         if (arp_mc_map(addr, buf, dev, 0) == 0)
946                 dev_mc_delete(dev,buf,dev->addr_len,0);
947 }
948
949 #ifdef CONFIG_IP_MULTICAST
950 /*
951  * deleted ip_mc_list manipulation
952  */
953 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
954 {
955         struct ip_mc_list *pmc;
956
957         /* this is an "ip_mc_list" for convenience; only the fields below
958          * are actually used. In particular, the refcnt and users are not
959          * used for management of the delete list. Using the same structure
960          * for deleted items allows change reports to use common code with
961          * non-deleted or query-response MCA's.
962          */
963         pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
964         if (!pmc)
965                 return;
966         memset(pmc, 0, sizeof(*pmc));
967         spin_lock_bh(&im->lock);
968         pmc->interface = im->interface;
969         in_dev_hold(in_dev);
970         pmc->multiaddr = im->multiaddr;
971         pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
972                 IGMP_Unsolicited_Report_Count;
973         pmc->sfmode = im->sfmode;
974         if (pmc->sfmode == MCAST_INCLUDE) {
975                 struct ip_sf_list *psf;
976
977                 pmc->tomb = im->tomb;
978                 pmc->sources = im->sources;
979                 im->tomb = im->sources = NULL;
980                 for (psf=pmc->sources; psf; psf=psf->sf_next)
981                         psf->sf_crcount = pmc->crcount;
982         }
983         spin_unlock_bh(&im->lock);
984
985         write_lock_bh(&in_dev->mc_lock);
986         pmc->next = in_dev->mc_tomb;
987         in_dev->mc_tomb = pmc;
988         write_unlock_bh(&in_dev->mc_lock);
989 }
990
991 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
992 {
993         struct ip_mc_list *pmc, *pmc_prev;
994         struct ip_sf_list *psf, *psf_next;
995
996         write_lock_bh(&in_dev->mc_lock);
997         pmc_prev = NULL;
998         for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
999                 if (pmc->multiaddr == multiaddr)
1000                         break;
1001                 pmc_prev = pmc;
1002         }
1003         if (pmc) {
1004                 if (pmc_prev)
1005                         pmc_prev->next = pmc->next;
1006                 else
1007                         in_dev->mc_tomb = pmc->next;
1008         }
1009         write_unlock_bh(&in_dev->mc_lock);
1010         if (pmc) {
1011                 for (psf=pmc->tomb; psf; psf=psf_next) {
1012                         psf_next = psf->sf_next;
1013                         kfree(psf);
1014                 }
1015                 in_dev_put(pmc->interface);
1016                 kfree(pmc);
1017         }
1018 }
1019
1020 static void igmpv3_clear_delrec(struct in_device *in_dev)
1021 {
1022         struct ip_mc_list *pmc, *nextpmc;
1023
1024         write_lock_bh(&in_dev->mc_lock);
1025         pmc = in_dev->mc_tomb;
1026         in_dev->mc_tomb = NULL;
1027         write_unlock_bh(&in_dev->mc_lock);
1028
1029         for (; pmc; pmc = nextpmc) {
1030                 nextpmc = pmc->next;
1031                 ip_mc_clear_src(pmc);
1032                 in_dev_put(pmc->interface);
1033                 kfree(pmc);
1034         }
1035         /* clear dead sources, too */
1036         read_lock(&in_dev->lock);
1037         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1038                 struct ip_sf_list *psf, *psf_next;
1039
1040                 spin_lock_bh(&pmc->lock);
1041                 psf = pmc->tomb;
1042                 pmc->tomb = NULL;
1043                 spin_unlock_bh(&pmc->lock);
1044                 for (; psf; psf=psf_next) {
1045                         psf_next = psf->sf_next;
1046                         kfree(psf);
1047                 }
1048         }
1049         read_unlock(&in_dev->lock);
1050 }
1051 #endif
1052
1053 #if defined(CONFIG_MIPS_BRCM)
1054 #define IGMP_RIP_ROUTER htonl(0xE0000009L)
1055 #endif
1056
1057 static void igmp_group_dropped(struct ip_mc_list *im)
1058 {
1059         struct in_device *in_dev = im->interface;
1060 #ifdef CONFIG_IP_MULTICAST
1061         int reporter;
1062 #endif
1063
1064         if (im->loaded) {
1065                 im->loaded = 0;
1066                 ip_mc_filter_del(in_dev, im->multiaddr);
1067         }
1068
1069 #ifdef CONFIG_IP_MULTICAST
1070         if (im->multiaddr == IGMP_ALL_HOSTS)
1071                 return;
1072
1073 #if defined(CONFIG_MIPS_BRCM)
1074         if (im->multiaddr == IGMP_RIP_ROUTER)
1075                 return;
1076 #endif
1077
1078         reporter = im->reporter;
1079         igmp_stop_timer(im);
1080
1081         if (!in_dev->dead) {
1082                 if (IGMP_V1_SEEN(in_dev))
1083                         goto done;
1084                 if (IGMP_V2_SEEN(in_dev)) {
1085                         if (reporter)
1086                                 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1087                         goto done;
1088                 }
1089                 /* IGMPv3 */
1090                 igmpv3_add_delrec(in_dev, im);
1091
1092                 igmp_ifc_event(in_dev);
1093         }
1094 done:
1095 #endif
1096         ip_mc_clear_src(im);
1097 }
1098
1099 static void igmp_group_added(struct ip_mc_list *im)
1100 {
1101         struct in_device *in_dev = im->interface;
1102
1103         if (im->loaded == 0) {
1104                 im->loaded = 1;
1105                 ip_mc_filter_add(in_dev, im->multiaddr);
1106         }
1107
1108 #ifdef CONFIG_IP_MULTICAST
1109         if (im->multiaddr == IGMP_ALL_HOSTS)
1110                 return;
1111
1112 #if defined(CONFIG_MIPS_BRCM)
1113         if (im->multiaddr == IGMP_RIP_ROUTER)
1114                 return;
1115 #endif
1116
1117         if (in_dev->dead)
1118                 return;
1119         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1120                 spin_lock_bh(&im->lock);
1121                 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1122                 spin_unlock_bh(&im->lock);
1123                 return;
1124         }
1125         /* else, v3 */
1126
1127         im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1128                 IGMP_Unsolicited_Report_Count;
1129         igmp_ifc_event(in_dev);
1130 #endif
1131 }
1132
1133
1134 /*
1135  *      Multicast list managers
1136  */
1137
1138
1139 /*
1140  *      A socket has joined a multicast group on device dev.
1141  */
1142
1143 void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1144 {
1145         struct ip_mc_list *im;
1146
1147         ASSERT_RTNL();
1148
1149         for (im=in_dev->mc_list; im; im=im->next) {
1150                 if (im->multiaddr == addr) {
1151                         im->users++;
1152                         ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1153                         goto out;
1154                 }
1155         }
1156
1157         im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1158         if (!im)
1159                 goto out;
1160
1161         im->users=1;
1162         im->interface=in_dev;
1163         in_dev_hold(in_dev);
1164         im->multiaddr=addr;
1165         /* initial mode is (EX, empty) */
1166         im->sfmode = MCAST_EXCLUDE;
1167         im->sfcount[MCAST_INCLUDE] = 0;
1168         im->sfcount[MCAST_EXCLUDE] = 1;
1169         im->sources = NULL;
1170         im->tomb = NULL;
1171         im->crcount = 0;
1172         atomic_set(&im->refcnt, 1);
1173         spin_lock_init(&im->lock);
1174 #ifdef CONFIG_IP_MULTICAST
1175         im->tm_running=0;
1176         init_timer(&im->timer);
1177         im->timer.data=(unsigned long)im;
1178         im->timer.function=&igmp_timer_expire;
1179         im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1180         im->reporter = 0;
1181         im->gsquery = 0;
1182 #endif
1183         im->loaded = 0;
1184         write_lock_bh(&in_dev->lock);
1185         im->next=in_dev->mc_list;
1186         in_dev->mc_list=im;
1187         write_unlock_bh(&in_dev->lock);
1188 #ifdef CONFIG_IP_MULTICAST
1189         igmpv3_del_delrec(in_dev, im->multiaddr);
1190 #endif
1191         igmp_group_added(im);
1192         if (!in_dev->dead)
1193                 ip_rt_multicast_event(in_dev);
1194 out:
1195         return;
1196 }
1197
1198 /*
1199  *      A socket has left a multicast group on device dev
1200  */
1201
1202 void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1203 {
1204         struct ip_mc_list *i, **ip;
1205         
1206         ASSERT_RTNL();
1207         
1208         for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1209                 if (i->multiaddr==addr) {
1210                         if (--i->users == 0) {
1211                                 write_lock_bh(&in_dev->lock);
1212                                 *ip = i->next;
1213                                 write_unlock_bh(&in_dev->lock);
1214                                 igmp_group_dropped(i);
1215
1216                                 if (!in_dev->dead)
1217                                         ip_rt_multicast_event(in_dev);
1218
1219                                 ip_ma_put(i);
1220                                 return;
1221                         }
1222                         break;
1223                 }
1224         }
1225 }
1226
1227 /* Device going down */
1228
1229 void ip_mc_down(struct in_device *in_dev)
1230 {
1231         struct ip_mc_list *i;
1232
1233         ASSERT_RTNL();
1234
1235         for (i=in_dev->mc_list; i; i=i->next)
1236                 igmp_group_dropped(i);
1237
1238 #ifdef CONFIG_IP_MULTICAST
1239         in_dev->mr_ifc_count = 0;
1240         if (del_timer(&in_dev->mr_ifc_timer))
1241                 __in_dev_put(in_dev);
1242         in_dev->mr_gq_running = 0;
1243         if (del_timer(&in_dev->mr_gq_timer))
1244                 __in_dev_put(in_dev);
1245         igmpv3_clear_delrec(in_dev);
1246 #endif
1247
1248         ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1249 }
1250
1251 void ip_mc_init_dev(struct in_device *in_dev)
1252 {
1253         ASSERT_RTNL();
1254
1255         in_dev->mc_tomb = NULL;
1256 #ifdef CONFIG_IP_MULTICAST
1257         in_dev->mr_gq_running = 0;
1258         init_timer(&in_dev->mr_gq_timer);
1259         in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1260         in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1261         in_dev->mr_ifc_count = 0;
1262         init_timer(&in_dev->mr_ifc_timer);
1263         in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1264         in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1265         in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1266 #endif
1267
1268         in_dev->mc_lock = RW_LOCK_UNLOCKED;
1269 }
1270
1271 /* Device going up */
1272
1273 void ip_mc_up(struct in_device *in_dev)
1274 {
1275         struct ip_mc_list *i;
1276
1277         ASSERT_RTNL();
1278
1279         ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1280
1281         for (i=in_dev->mc_list; i; i=i->next)
1282                 igmp_group_added(i);
1283 }
1284
1285 /*
1286  *      Device is about to be destroyed: clean up.
1287  */
1288
1289 void ip_mc_destroy_dev(struct in_device *in_dev)
1290 {
1291         struct ip_mc_list *i;
1292
1293         ASSERT_RTNL();
1294
1295         /* Deactivate timers */
1296         ip_mc_down(in_dev);
1297
1298         write_lock_bh(&in_dev->lock);
1299         while ((i = in_dev->mc_list) != NULL) {
1300                 in_dev->mc_list = i->next;
1301                 write_unlock_bh(&in_dev->lock);
1302
1303                 igmp_group_dropped(i);
1304                 ip_ma_put(i);
1305
1306                 write_lock_bh(&in_dev->lock);
1307         }
1308         write_unlock_bh(&in_dev->lock);
1309 }
1310
1311 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1312 {
1313         struct flowi fl = { .nl_u = { .ip4_u =
1314                                       { .daddr = imr->imr_multiaddr.s_addr } } };
1315         struct rtable *rt;
1316         struct net_device *dev = NULL;
1317         struct in_device *idev = NULL;
1318
1319         if (imr->imr_ifindex) {
1320                 idev = inetdev_by_index(imr->imr_ifindex);
1321                 if (idev)
1322                         __in_dev_put(idev);
1323                 return idev;
1324         }
1325         if (imr->imr_address.s_addr) {
1326                 dev = ip_dev_find(imr->imr_address.s_addr);
1327                 if (!dev)
1328                         return NULL;
1329                 __dev_put(dev);
1330         }
1331
1332         if (!dev && !ip_route_output_key(&rt, &fl)) {
1333                 dev = rt->u.dst.dev;
1334                 ip_rt_put(rt);
1335         }
1336         if (dev) {
1337                 imr->imr_ifindex = dev->ifindex;
1338                 idev = __in_dev_get(dev);
1339         }
1340         return idev;
1341 }
1342
1343 /*
1344  *      Join a socket to a group
1345  */
1346 int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1347 int sysctl_igmp_max_msf = IP_MAX_MSF;
1348
1349
1350 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1351         __u32 *psfsrc)
1352 {
1353         struct ip_sf_list *psf, *psf_prev;
1354         int rv = 0;
1355
1356         psf_prev = NULL;
1357         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1358                 if (psf->sf_inaddr == *psfsrc)
1359                         break;
1360                 psf_prev = psf;
1361         }
1362         if (!psf || psf->sf_count[sfmode] == 0) {
1363                 /* source filter not found, or count wrong =>  bug */
1364                 return -ESRCH;
1365         }
1366         psf->sf_count[sfmode]--;
1367         if (psf->sf_count[sfmode] == 0) {
1368                 ip_rt_multicast_event(pmc->interface);
1369         }
1370         if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1371 #ifdef CONFIG_IP_MULTICAST
1372                 struct in_device *in_dev = pmc->interface;
1373 #endif
1374
1375                 /* no more filters for this source */
1376                 if (psf_prev)
1377                         psf_prev->sf_next = psf->sf_next;
1378                 else
1379                         pmc->sources = psf->sf_next;
1380 #ifdef CONFIG_IP_MULTICAST
1381                 if (psf->sf_oldin &&
1382                     !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1383                         psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1384                                 IGMP_Unsolicited_Report_Count;
1385                         psf->sf_next = pmc->tomb;
1386                         pmc->tomb = psf;
1387                         rv = 1;
1388                 } else
1389 #endif
1390                         kfree(psf);
1391         }
1392         return rv;
1393 }
1394
1395 #ifndef CONFIG_IP_MULTICAST
1396 #define igmp_ifc_event(x)       do { } while (0)
1397 #endif
1398
1399 int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1400         int sfcount, __u32 *psfsrc, int delta)
1401 {
1402         struct ip_mc_list *pmc;
1403         int     changerec = 0;
1404         int     i, err;
1405
1406         if (!in_dev)
1407                 return -ENODEV;
1408         read_lock(&in_dev->lock);
1409         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1410                 if (*pmca == pmc->multiaddr)
1411                         break;
1412         }
1413         if (!pmc) {
1414                 /* MCA not found?? bug */
1415                 read_unlock(&in_dev->lock);
1416                 return -ESRCH;
1417         }
1418         spin_lock_bh(&pmc->lock);
1419         read_unlock(&in_dev->lock);
1420 #ifdef CONFIG_IP_MULTICAST
1421         sf_markstate(pmc);
1422 #endif
1423         if (!delta) {
1424                 err = -EINVAL;
1425                 if (!pmc->sfcount[sfmode])
1426                         goto out_unlock;
1427                 pmc->sfcount[sfmode]--;
1428         }
1429         err = 0;
1430         for (i=0; i<sfcount; i++) {
1431                 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1432
1433                 changerec |= rv > 0;
1434                 if (!err && rv < 0)
1435                         err = rv;
1436         }
1437         if (pmc->sfmode == MCAST_EXCLUDE &&
1438             pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1439             pmc->sfcount[MCAST_INCLUDE]) {
1440 #ifdef CONFIG_IP_MULTICAST
1441                 struct ip_sf_list *psf;
1442 #endif
1443
1444                 /* filter mode change */
1445                 pmc->sfmode = MCAST_INCLUDE;
1446 #ifdef CONFIG_IP_MULTICAST
1447                 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1448                         IGMP_Unsolicited_Report_Count;
1449                 in_dev->mr_ifc_count = pmc->crcount;
1450                 for (psf=pmc->sources; psf; psf = psf->sf_next)
1451                         psf->sf_crcount = 0;
1452                 igmp_ifc_event(pmc->interface);
1453         } else if (sf_setstate(pmc) || changerec) {
1454                 igmp_ifc_event(pmc->interface);
1455 #endif
1456         }
1457 out_unlock:
1458         spin_unlock_bh(&pmc->lock);
1459         return err;
1460 }
1461
1462 /*
1463  * Add multicast single-source filter to the interface list
1464  */
1465 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1466         __u32 *psfsrc, int delta)
1467 {
1468         struct ip_sf_list *psf, *psf_prev;
1469
1470         psf_prev = NULL;
1471         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1472                 if (psf->sf_inaddr == *psfsrc)
1473                         break;
1474                 psf_prev = psf;
1475         }
1476         if (!psf) {
1477                 psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1478                 if (!psf)
1479                         return -ENOBUFS;
1480                 memset(psf, 0, sizeof(*psf));
1481                 psf->sf_inaddr = *psfsrc;
1482                 if (psf_prev) {
1483                         psf_prev->sf_next = psf;
1484                 } else
1485                         pmc->sources = psf;
1486         }
1487         psf->sf_count[sfmode]++;
1488         if (psf->sf_count[sfmode] == 1) {
1489                 ip_rt_multicast_event(pmc->interface);
1490         }
1491         return 0;
1492 }
1493
1494 #ifdef CONFIG_IP_MULTICAST
1495 static void sf_markstate(struct ip_mc_list *pmc)
1496 {
1497         struct ip_sf_list *psf;
1498         int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1499
1500         for (psf=pmc->sources; psf; psf=psf->sf_next)
1501                 if (pmc->sfcount[MCAST_EXCLUDE]) {
1502                         psf->sf_oldin = mca_xcount ==
1503                                 psf->sf_count[MCAST_EXCLUDE] &&
1504                                 !psf->sf_count[MCAST_INCLUDE];
1505                 } else
1506                         psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1507 }
1508
1509 static int sf_setstate(struct ip_mc_list *pmc)
1510 {
1511         struct ip_sf_list *psf;
1512         int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1513         int qrv = pmc->interface->mr_qrv;
1514         int new_in, rv;
1515
1516         rv = 0;
1517         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1518                 if (pmc->sfcount[MCAST_EXCLUDE]) {
1519                         new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1520                                 !psf->sf_count[MCAST_INCLUDE];
1521                 } else
1522                         new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1523                 if (new_in != psf->sf_oldin) {
1524                         psf->sf_crcount = qrv;
1525                         rv++;
1526                 }
1527         }
1528         return rv;
1529 }
1530 #endif
1531
1532 /*
1533  * Add multicast source filter list to the interface list
1534  */
1535 int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1536         int sfcount, __u32 *psfsrc, int delta)
1537 {
1538         struct ip_mc_list *pmc;
1539         int     isexclude;
1540         int     i, err;
1541
1542         if (!in_dev)
1543                 return -ENODEV;
1544         read_lock(&in_dev->lock);
1545         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1546                 if (*pmca == pmc->multiaddr)
1547                         break;
1548         }
1549         if (!pmc) {
1550                 /* MCA not found?? bug */
1551                 read_unlock(&in_dev->lock);
1552                 return -ESRCH;
1553         }
1554         spin_lock_bh(&pmc->lock);
1555         read_unlock(&in_dev->lock);
1556
1557 #ifdef CONFIG_IP_MULTICAST
1558         sf_markstate(pmc);
1559 #endif
1560         isexclude = pmc->sfmode == MCAST_EXCLUDE;
1561         if (!delta)
1562                 pmc->sfcount[sfmode]++;
1563         err = 0;
1564         for (i=0; i<sfcount; i++) {
1565                 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1566                 if (err)
1567                         break;
1568         }
1569         if (err) {
1570                 int j;
1571
1572                 pmc->sfcount[sfmode]--;
1573                 for (j=0; j<i; j++)
1574                         (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1575         } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1576 #ifdef CONFIG_IP_MULTICAST
1577                 struct in_device *in_dev = pmc->interface;
1578                 struct ip_sf_list *psf;
1579 #endif
1580
1581                 /* filter mode change */
1582                 if (pmc->sfcount[MCAST_EXCLUDE])
1583                         pmc->sfmode = MCAST_EXCLUDE;
1584                 else if (pmc->sfcount[MCAST_INCLUDE])
1585                         pmc->sfmode = MCAST_INCLUDE;
1586 #ifdef CONFIG_IP_MULTICAST
1587                 /* else no filters; keep old mode for reports */
1588
1589                 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1590                         IGMP_Unsolicited_Report_Count;
1591                 in_dev->mr_ifc_count = pmc->crcount;
1592                 for (psf=pmc->sources; psf; psf = psf->sf_next)
1593                         psf->sf_crcount = 0;
1594                 igmp_ifc_event(in_dev);
1595         } else if (sf_setstate(pmc)) {
1596                 igmp_ifc_event(in_dev);
1597 #endif
1598         }
1599         spin_unlock_bh(&pmc->lock);
1600         return err;
1601 }
1602
1603 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1604 {
1605         struct ip_sf_list *psf, *nextpsf;
1606
1607         for (psf=pmc->tomb; psf; psf=nextpsf) {
1608                 nextpsf = psf->sf_next;
1609                 kfree(psf);
1610         }
1611         pmc->tomb = NULL;
1612         for (psf=pmc->sources; psf; psf=nextpsf) {
1613                 nextpsf = psf->sf_next;
1614                 kfree(psf);
1615         }
1616         pmc->sources = NULL;
1617         pmc->sfmode = MCAST_EXCLUDE;
1618         pmc->sfcount[MCAST_EXCLUDE] = 0;
1619         pmc->sfcount[MCAST_EXCLUDE] = 1;
1620 }
1621
1622
1623 /*
1624  * Join a multicast group
1625  */
1626 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1627 {
1628         int err;
1629         u32 addr = imr->imr_multiaddr.s_addr;
1630         struct ip_mc_socklist *iml, *i;
1631         struct in_device *in_dev;
1632         struct inet_opt *inet = inet_sk(sk);
1633         int count = 0;
1634
1635         if (!MULTICAST(addr))
1636                 return -EINVAL;
1637
1638         rtnl_shlock();
1639
1640         in_dev = ip_mc_find_dev(imr);
1641
1642         if (!in_dev) {
1643                 iml = NULL;
1644                 err = -ENODEV;
1645                 goto done;
1646         }
1647
1648         iml = (struct ip_mc_socklist *)sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1649
1650         err = -EADDRINUSE;
1651         for (i = inet->mc_list; i; i = i->next) {
1652                 if (memcmp(&i->multi, imr, sizeof(*imr)) == 0) {
1653                         /* New style additions are reference counted */
1654                         if (imr->imr_address.s_addr == 0) {
1655                                 i->count++;
1656                                 err = 0;
1657                         }
1658                         goto done;
1659                 }
1660                 count++;
1661         }
1662         err = -ENOBUFS;
1663         if (iml == NULL || count >= sysctl_igmp_max_memberships)
1664                 goto done;
1665         memcpy(&iml->multi, imr, sizeof(*imr));
1666         iml->next = inet->mc_list;
1667         iml->count = 1;
1668         iml->sflist = NULL;
1669         iml->sfmode = MCAST_EXCLUDE;
1670         inet->mc_list = iml;
1671         ip_mc_inc_group(in_dev, addr);
1672         iml = NULL;
1673         err = 0;
1674
1675 done:
1676         rtnl_shunlock();
1677         if (iml)
1678                 sock_kfree_s(sk, iml, sizeof(*iml));
1679         return err;
1680 }
1681
1682 int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1683         struct in_device *in_dev)
1684 {
1685         int err;
1686
1687         if (iml->sflist == 0) {
1688                 /* any-source empty exclude case */
1689                 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1690                         iml->sfmode, 0, NULL, 0);
1691         }
1692         err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1693                         iml->sfmode, iml->sflist->sl_count,
1694                         iml->sflist->sl_addr, 0);
1695         sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1696         iml->sflist = NULL;
1697         return err;
1698 }
1699
1700 /*
1701  *      Ask a socket to leave a group.
1702  */
1703
1704 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1705 {
1706         struct inet_opt *inet = inet_sk(sk);
1707         struct ip_mc_socklist *iml, **imlp;
1708
1709         rtnl_lock();
1710         for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1711                 if (iml->multi.imr_multiaddr.s_addr==imr->imr_multiaddr.s_addr &&
1712                     iml->multi.imr_address.s_addr==imr->imr_address.s_addr &&
1713                     (!imr->imr_ifindex || iml->multi.imr_ifindex==imr->imr_ifindex)) {
1714                         struct in_device *in_dev;
1715
1716                         in_dev = inetdev_by_index(iml->multi.imr_ifindex);
1717                         if (in_dev)
1718                                 (void) ip_mc_leave_src(sk, iml, in_dev);
1719                         if (--iml->count) {
1720                                 rtnl_unlock();
1721                                 if (in_dev)
1722                                         in_dev_put(in_dev);
1723                                 return 0;
1724                         }
1725
1726                         *imlp = iml->next;
1727
1728                         if (in_dev) {
1729                                 ip_mc_dec_group(in_dev, imr->imr_multiaddr.s_addr);
1730                                 in_dev_put(in_dev);
1731                         }
1732                         rtnl_unlock();
1733                         sock_kfree_s(sk, iml, sizeof(*iml));
1734                         return 0;
1735                 }
1736         }
1737         rtnl_unlock();
1738         return -EADDRNOTAVAIL;
1739 }
1740
1741 int ip_mc_source(int add, int omode, struct sock *sk, struct
1742         ip_mreq_source *mreqs, int ifindex)
1743 {
1744         int err;
1745         struct ip_mreqn imr;
1746         u32 addr = mreqs->imr_multiaddr;
1747         struct ip_mc_socklist *pmc;
1748         struct in_device *in_dev = NULL;
1749         struct inet_opt *inet = inet_sk(sk);
1750         struct ip_sf_socklist *psl;
1751         int i, j, rv;
1752
1753         if (!MULTICAST(addr))
1754                 return -EINVAL;
1755
1756         rtnl_shlock();
1757
1758         imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1759         imr.imr_address.s_addr = mreqs->imr_interface;
1760         imr.imr_ifindex = ifindex;
1761         in_dev = ip_mc_find_dev(&imr);
1762
1763         if (!in_dev) {
1764                 err = -ENODEV;
1765                 goto done;
1766         }
1767         err = -EADDRNOTAVAIL;
1768
1769         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1770                 if (memcmp(&pmc->multi, mreqs, 2*sizeof(__u32)) == 0)
1771                         break;
1772         }
1773         if (!pmc)               /* must have a prior join */
1774                 goto done;
1775         /* if a source filter was set, must be the same mode as before */
1776         if (pmc->sflist) {
1777                 if (pmc->sfmode != omode)
1778                         goto done;
1779         } else if (pmc->sfmode != omode) {
1780                 /* allow mode switches for empty-set filters */
1781                 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1782                 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 
1783                         NULL, 0);
1784                 pmc->sfmode = omode;
1785         }
1786
1787         psl = pmc->sflist;
1788         if (!add) {
1789                 if (!psl)
1790                         goto done;
1791                 rv = !0;
1792                 for (i=0; i<psl->sl_count; i++) {
1793                         rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1794                                 sizeof(__u32));
1795                         if (rv >= 0)
1796                                 break;
1797                 }
1798                 if (!rv)        /* source not found */
1799                         goto done;
1800
1801                 /* update the interface filter */
1802                 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 
1803                         &mreqs->imr_sourceaddr, 1);
1804
1805                 for (j=i+1; j<psl->sl_count; j++)
1806                         psl->sl_addr[j-1] = psl->sl_addr[j];
1807                 psl->sl_count--;
1808                 err = 0;
1809                 goto done;
1810         }
1811         /* else, add a new source to the filter */
1812
1813         if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1814                 err = -ENOBUFS;
1815                 goto done;
1816         }
1817         if (!psl || psl->sl_count == psl->sl_max) {
1818                 struct ip_sf_socklist *newpsl;
1819                 int count = IP_SFBLOCK;
1820
1821                 if (psl)
1822                         count += psl->sl_max;
1823                 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1824                         IP_SFLSIZE(count), GFP_KERNEL);
1825                 if (!newpsl) {
1826                         err = -ENOBUFS;
1827                         goto done;
1828                 }
1829                 newpsl->sl_max = count;
1830                 newpsl->sl_count = count - IP_SFBLOCK;
1831                 if (psl) {
1832                         for (i=0; i<psl->sl_count; i++)
1833                                 newpsl->sl_addr[i] = psl->sl_addr[i];
1834                         sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1835                 }
1836                 pmc->sflist = psl = newpsl;
1837         }
1838         rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1839         for (i=0; i<psl->sl_count; i++) {
1840                 rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1841                         sizeof(__u32));
1842                 if (rv >= 0)
1843                         break;
1844         }
1845         if (rv == 0)            /* address already there is an error */
1846                 goto done;
1847         for (j=psl->sl_count-1; j>=i; j--)
1848                 psl->sl_addr[j+1] = psl->sl_addr[j];
1849         psl->sl_addr[i] = mreqs->imr_sourceaddr;
1850         psl->sl_count++;
1851         err = 0;
1852         /* update the interface list */
1853         ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 
1854                 &mreqs->imr_sourceaddr, 1);
1855 done:
1856         rtnl_shunlock();
1857         return err;
1858 }
1859
1860 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1861 {
1862         int err;
1863         struct ip_mreqn imr;
1864         u32 addr = msf->imsf_multiaddr;
1865         struct ip_mc_socklist *pmc;
1866         struct in_device *in_dev;
1867         struct inet_opt *inet = inet_sk(sk);
1868         struct ip_sf_socklist *newpsl, *psl;
1869
1870         if (!MULTICAST(addr))
1871                 return -EINVAL;
1872         if (msf->imsf_fmode != MCAST_INCLUDE &&
1873             msf->imsf_fmode != MCAST_EXCLUDE)
1874                 return -EINVAL;
1875
1876         rtnl_shlock();
1877
1878         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1879         imr.imr_address.s_addr = msf->imsf_interface;
1880         imr.imr_ifindex = ifindex;
1881         in_dev = ip_mc_find_dev(&imr);
1882
1883         if (!in_dev) {
1884                 err = -ENODEV;
1885                 goto done;
1886         }
1887         err = -EADDRNOTAVAIL;
1888
1889         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1890                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1891                     pmc->multi.imr_ifindex == imr.imr_ifindex)
1892                         break;
1893         }
1894         if (!pmc)               /* must have a prior join */
1895                 goto done;
1896         if (msf->imsf_numsrc) {
1897                 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1898                                 IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1899                 if (!newpsl) {
1900                         err = -ENOBUFS;
1901                         goto done;
1902                 }
1903                 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1904                 memcpy(newpsl->sl_addr, msf->imsf_slist,
1905                         msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1906                 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1907                         msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1908                 if (err) {
1909                         sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1910                         goto done;
1911                 }
1912         } else
1913                 newpsl = NULL;
1914         psl = pmc->sflist;
1915         if (psl) {
1916                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1917                         psl->sl_count, psl->sl_addr, 0);
1918                 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1919         } else
1920                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1921                         0, NULL, 0);
1922         pmc->sflist = newpsl;
1923         pmc->sfmode = msf->imsf_fmode;
1924 done:
1925         rtnl_shunlock();
1926         return err;
1927 }
1928
1929 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1930         struct ip_msfilter __user *optval, int __user *optlen)
1931 {
1932         int err, len, count, copycount;
1933         struct ip_mreqn imr;
1934         u32 addr = msf->imsf_multiaddr;
1935         struct ip_mc_socklist *pmc;
1936         struct in_device *in_dev;
1937         struct inet_opt *inet = inet_sk(sk);
1938         struct ip_sf_socklist *psl;
1939
1940         if (!MULTICAST(addr))
1941                 return -EINVAL;
1942
1943         rtnl_shlock();
1944
1945         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1946         imr.imr_address.s_addr = msf->imsf_interface;
1947         imr.imr_ifindex = 0;
1948         in_dev = ip_mc_find_dev(&imr);
1949
1950         if (!in_dev) {
1951                 err = -ENODEV;
1952                 goto done;
1953         }
1954         err = -EADDRNOTAVAIL;
1955
1956         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1957                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1958                     pmc->multi.imr_ifindex == imr.imr_ifindex)
1959                         break;
1960         }
1961         if (!pmc)               /* must have a prior join */
1962                 goto done;
1963         msf->imsf_fmode = pmc->sfmode;
1964         psl = pmc->sflist;
1965         rtnl_shunlock();
1966         if (!psl) {
1967                 len = 0;
1968                 count = 0;
1969         } else {
1970                 count = psl->sl_count;
1971         }
1972         copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1973         len = copycount * sizeof(psl->sl_addr[0]);
1974         msf->imsf_numsrc = count;
1975         if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1976             copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
1977                 return -EFAULT;
1978         }
1979         if (len &&
1980             copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
1981                 return -EFAULT;
1982         return 0;
1983 done:
1984         rtnl_shunlock();
1985         return err;
1986 }
1987
1988 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
1989         struct group_filter __user *optval, int __user *optlen)
1990 {
1991         int err, i, count, copycount;
1992         struct sockaddr_in *psin;
1993         u32 addr;
1994         struct ip_mc_socklist *pmc;
1995         struct inet_opt *inet = inet_sk(sk);
1996         struct ip_sf_socklist *psl;
1997
1998         psin = (struct sockaddr_in *)&gsf->gf_group;
1999         if (psin->sin_family != AF_INET)
2000                 return -EINVAL;
2001         addr = psin->sin_addr.s_addr;
2002         if (!MULTICAST(addr))
2003                 return -EINVAL;
2004
2005         rtnl_shlock();
2006
2007         err = -EADDRNOTAVAIL;
2008
2009         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2010                 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2011                     pmc->multi.imr_ifindex == gsf->gf_interface)
2012                         break;
2013         }
2014         if (!pmc)               /* must have a prior join */
2015                 goto done;
2016         gsf->gf_fmode = pmc->sfmode;
2017         psl = pmc->sflist;
2018         rtnl_shunlock();
2019         count = psl ? psl->sl_count : 0;
2020         copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2021         gsf->gf_numsrc = count;
2022         if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2023             copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2024                 return -EFAULT;
2025         }
2026         for (i=0; i<copycount; i++) {
2027                 struct sockaddr_in *psin;
2028                 struct sockaddr_storage ss;
2029
2030                 psin = (struct sockaddr_in *)&ss;
2031                 memset(&ss, 0, sizeof(ss));
2032                 psin->sin_family = AF_INET;
2033                 psin->sin_addr.s_addr = psl->sl_addr[i];
2034                 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2035                         return -EFAULT;
2036         }
2037         return 0;
2038 done:
2039         rtnl_shunlock();
2040         return err;
2041 }
2042
2043 /*
2044  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2045  */
2046 int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2047 {
2048         struct inet_opt *inet = inet_sk(sk);
2049         struct ip_mc_socklist *pmc;
2050         struct ip_sf_socklist *psl;
2051         int i;
2052
2053         if (!MULTICAST(loc_addr))
2054                 return 1;
2055
2056         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2057                 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2058                     pmc->multi.imr_ifindex == dif)
2059                         break;
2060         }
2061         if (!pmc)
2062                 return 1;
2063         psl = pmc->sflist;
2064         if (!psl)
2065                 return pmc->sfmode == MCAST_EXCLUDE;
2066
2067         for (i=0; i<psl->sl_count; i++) {
2068                 if (psl->sl_addr[i] == rmt_addr)
2069                         break;
2070         }
2071         if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2072                 return 0;
2073         if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2074                 return 0;
2075         return 1;
2076 }
2077
2078 /*
2079  *      A socket is closing.
2080  */
2081
2082 void ip_mc_drop_socket(struct sock *sk)
2083 {
2084         struct inet_opt *inet = inet_sk(sk);
2085         struct ip_mc_socklist *iml;
2086
2087         if (inet->mc_list == NULL)
2088                 return;
2089
2090         rtnl_lock();
2091         while ((iml = inet->mc_list) != NULL) {
2092                 struct in_device *in_dev;
2093                 inet->mc_list = iml->next;
2094
2095                 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2096                         (void) ip_mc_leave_src(sk, iml, in_dev);
2097                         ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2098                         in_dev_put(in_dev);
2099                 }
2100                 sock_kfree_s(sk, iml, sizeof(*iml));
2101
2102         }
2103         rtnl_unlock();
2104 }
2105
2106 int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2107 {
2108         struct ip_mc_list *im;
2109         struct ip_sf_list *psf;
2110         int rv = 0;
2111
2112         read_lock(&in_dev->lock);
2113         for (im=in_dev->mc_list; im; im=im->next) {
2114                 if (im->multiaddr == mc_addr)
2115                         break;
2116         }
2117         if (im && proto == IPPROTO_IGMP) {
2118                 rv = 1;
2119         } else if (im) {
2120                 if (src_addr) {
2121                         for (psf=im->sources; psf; psf=psf->sf_next) {
2122                                 if (psf->sf_inaddr == src_addr)
2123                                         break;
2124                         }
2125                         if (psf)
2126                                 rv = psf->sf_count[MCAST_INCLUDE] ||
2127                                         psf->sf_count[MCAST_EXCLUDE] !=
2128                                         im->sfcount[MCAST_EXCLUDE];
2129                         else
2130                                 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2131                 } else
2132                         rv = 1; /* unspecified source; tentatively allow */
2133         }
2134         read_unlock(&in_dev->lock);
2135         return rv;
2136 }
2137
2138 #if defined(CONFIG_PROC_FS)
2139 struct igmp_mc_iter_state {
2140         struct net_device *dev;
2141         struct in_device *in_dev;
2142 };
2143
2144 #define igmp_mc_seq_private(seq)        ((struct igmp_mc_iter_state *)(seq)->private)
2145
2146 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2147 {
2148         struct ip_mc_list *im = NULL;
2149         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2150
2151         for (state->dev = dev_base, state->in_dev = NULL;
2152              state->dev; 
2153              state->dev = state->dev->next) {
2154                 struct in_device *in_dev;
2155                 in_dev = in_dev_get(state->dev);
2156                 if (!in_dev)
2157                         continue;
2158                 read_lock(&in_dev->lock);
2159                 im = in_dev->mc_list;
2160                 if (im) {
2161                         state->in_dev = in_dev;
2162                         break;
2163                 }
2164                 read_unlock(&in_dev->lock);
2165                 in_dev_put(in_dev);
2166         }
2167         return im;
2168 }
2169
2170 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2171 {
2172         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2173         im = im->next;
2174         while (!im) {
2175                 if (likely(state->in_dev != NULL)) {
2176                         read_unlock(&state->in_dev->lock);
2177                         in_dev_put(state->in_dev);
2178                 }
2179                 state->dev = state->dev->next;
2180                 if (!state->dev) {
2181                         state->in_dev = NULL;
2182                         break;
2183                 }
2184                 state->in_dev = in_dev_get(state->dev);
2185                 if (!state->in_dev)
2186                         continue;
2187                 read_lock(&state->in_dev->lock);
2188                 im = state->in_dev->mc_list;
2189         }
2190         return im;
2191 }
2192
2193 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2194 {
2195         struct ip_mc_list *im = igmp_mc_get_first(seq);
2196         if (im)
2197                 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2198                         --pos;
2199         return pos ? NULL : im;
2200 }
2201
2202 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2203 {
2204         read_lock(&dev_base_lock);
2205         return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2206 }
2207
2208 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2209 {
2210         struct ip_mc_list *im;
2211         if (v == SEQ_START_TOKEN)
2212                 im = igmp_mc_get_first(seq);
2213         else
2214                 im = igmp_mc_get_next(seq, v);
2215         ++*pos;
2216         return im;
2217 }
2218
2219 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2220 {
2221         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2222         if (likely(state->in_dev != NULL)) {
2223                 read_unlock(&state->in_dev->lock);
2224                 in_dev_put(state->in_dev);
2225                 state->in_dev = NULL;
2226         }
2227         state->dev = NULL;
2228         read_unlock(&dev_base_lock);
2229 }
2230
2231 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2232 {
2233         if (v == SEQ_START_TOKEN)
2234                 seq_puts(seq, 
2235                          "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2236         else {
2237                 struct ip_mc_list *im = (struct ip_mc_list *)v;
2238                 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2239                 char   *querier;
2240 #ifdef CONFIG_IP_MULTICAST
2241                 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2242                           IGMP_V2_SEEN(state->in_dev) ? "V2" :
2243                           "V3";
2244 #else
2245                 querier = "NONE";
2246 #endif
2247
2248                 if (state->in_dev->mc_list == im) {
2249                         seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2250                                    state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2251                 }
2252
2253                 seq_printf(seq,
2254                            "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2255                            im->multiaddr, im->users,
2256                            im->tm_running, im->tm_running ?
2257                            jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2258                            im->reporter);
2259         }
2260         return 0;
2261 }
2262
2263 static struct seq_operations igmp_mc_seq_ops = {
2264         .start  =       igmp_mc_seq_start,
2265         .next   =       igmp_mc_seq_next,
2266         .stop   =       igmp_mc_seq_stop,
2267         .show   =       igmp_mc_seq_show,
2268 };
2269
2270 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2271 {
2272         struct seq_file *seq;
2273         int rc = -ENOMEM;
2274         struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2275
2276         if (!s)
2277                 goto out;
2278         rc = seq_open(file, &igmp_mc_seq_ops);
2279         if (rc)
2280                 goto out_kfree;
2281
2282         seq = file->private_data;
2283         seq->private = s;
2284         memset(s, 0, sizeof(*s));
2285 out:
2286         return rc;
2287 out_kfree:
2288         kfree(s);
2289         goto out;
2290 }
2291
2292 static struct file_operations igmp_mc_seq_fops = {
2293         .owner          =       THIS_MODULE,
2294         .open           =       igmp_mc_seq_open,
2295         .read           =       seq_read,
2296         .llseek         =       seq_lseek,
2297         .release        =       seq_release_private,
2298 };
2299
2300 struct igmp_mcf_iter_state {
2301         struct net_device *dev;
2302         struct in_device *idev;
2303         struct ip_mc_list *im;
2304 };
2305
2306 #define igmp_mcf_seq_private(seq)       ((struct igmp_mcf_iter_state *)(seq)->private)
2307
2308 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2309 {
2310         struct ip_sf_list *psf = NULL;
2311         struct ip_mc_list *im = NULL;
2312         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2313
2314         for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2315              state->dev; 
2316              state->dev = state->dev->next) {
2317                 struct in_device *idev;
2318                 idev = in_dev_get(state->dev);
2319                 if (unlikely(idev == NULL))
2320                         continue;
2321                 read_lock_bh(&idev->lock);
2322                 im = idev->mc_list;
2323                 if (likely(im != NULL)) {
2324                         spin_lock_bh(&im->lock);
2325                         psf = im->sources;
2326                         if (likely(psf != NULL)) {
2327                                 state->im = im;
2328                                 state->idev = idev;
2329                                 break;
2330                         }
2331                         spin_unlock_bh(&im->lock);
2332                 }
2333                 read_unlock_bh(&idev->lock);
2334                 in_dev_put(idev);
2335         }
2336         return psf;
2337 }
2338
2339 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2340 {
2341         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2342
2343         psf = psf->sf_next;
2344         while (!psf) {
2345                 spin_unlock_bh(&state->im->lock);
2346                 state->im = state->im->next;
2347                 while (!state->im) {
2348                         if (likely(state->idev != NULL)) {
2349                                 read_unlock_bh(&state->idev->lock);
2350                                 in_dev_put(state->idev);
2351                         }
2352                         state->dev = state->dev->next;
2353                         if (!state->dev) {
2354                                 state->idev = NULL;
2355                                 goto out;
2356                         }
2357                         state->idev = in_dev_get(state->dev);
2358                         if (!state->idev)
2359                                 continue;
2360                         read_lock_bh(&state->idev->lock);
2361                         state->im = state->idev->mc_list;
2362                 }
2363                 if (!state->im)
2364                         break;
2365                 spin_lock_bh(&state->im->lock);
2366                 psf = state->im->sources;
2367         }
2368 out:
2369         return psf;
2370 }
2371
2372 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2373 {
2374         struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2375         if (psf)
2376                 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2377                         --pos;
2378         return pos ? NULL : psf;
2379 }
2380
2381 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2382 {
2383         read_lock(&dev_base_lock);
2384         return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2385 }
2386
2387 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2388 {
2389         struct ip_sf_list *psf;
2390         if (v == SEQ_START_TOKEN)
2391                 psf = igmp_mcf_get_first(seq);
2392         else
2393                 psf = igmp_mcf_get_next(seq, v);
2394         ++*pos;
2395         return psf;
2396 }
2397
2398 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2399 {
2400         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2401         if (likely(state->im != NULL)) {
2402                 spin_unlock_bh(&state->im->lock);
2403                 state->im = NULL;
2404         }
2405         if (likely(state->idev != NULL)) {
2406                 read_unlock_bh(&state->idev->lock);
2407                 in_dev_put(state->idev);
2408                 state->idev = NULL;
2409         }
2410         state->dev = NULL;
2411         read_unlock(&dev_base_lock);
2412 }
2413
2414 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2415 {
2416         struct ip_sf_list *psf = (struct ip_sf_list *)v;
2417         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2418
2419         if (v == SEQ_START_TOKEN) {
2420                 seq_printf(seq, 
2421                            "%3s %6s "
2422                            "%10s %10s %6s %6s\n", "Idx",
2423                            "Device", "MCA",
2424                            "SRC", "INC", "EXC");
2425         } else {
2426                 seq_printf(seq,
2427                            "%3d %6.6s 0x%08x "
2428                            "0x%08x %6lu %6lu\n", 
2429                            state->dev->ifindex, state->dev->name, 
2430                            ntohl(state->im->multiaddr),
2431                            ntohl(psf->sf_inaddr),
2432                            psf->sf_count[MCAST_INCLUDE],
2433                            psf->sf_count[MCAST_EXCLUDE]);
2434         }
2435         return 0;
2436 }
2437
2438 static struct seq_operations igmp_mcf_seq_ops = {
2439         .start  =       igmp_mcf_seq_start,
2440         .next   =       igmp_mcf_seq_next,
2441         .stop   =       igmp_mcf_seq_stop,
2442         .show   =       igmp_mcf_seq_show,
2443 };
2444
2445 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2446 {
2447         struct seq_file *seq;
2448         int rc = -ENOMEM;
2449         struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2450
2451         if (!s)
2452                 goto out;
2453         rc = seq_open(file, &igmp_mcf_seq_ops);
2454         if (rc)
2455                 goto out_kfree;
2456
2457         seq = file->private_data;
2458         seq->private = s;
2459         memset(s, 0, sizeof(*s));
2460 out:
2461         return rc;
2462 out_kfree:
2463         kfree(s);
2464         goto out;
2465 }
2466
2467 static struct file_operations igmp_mcf_seq_fops = {
2468         .owner          =       THIS_MODULE,
2469         .open           =       igmp_mcf_seq_open,
2470         .read           =       seq_read,
2471         .llseek         =       seq_lseek,
2472         .release        =       seq_release_private,
2473 };
2474
2475 int __init igmp_mc_proc_init(void)
2476 {
2477         proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2478         proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2479         return 0;
2480 }
2481 #endif
2482
2483 EXPORT_SYMBOL(ip_mc_dec_group);
2484 EXPORT_SYMBOL(ip_mc_inc_group);
2485 EXPORT_SYMBOL(ip_mc_join_group);