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