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