[NET]: Make /proc/net per network namespace
[linux-2.6/kmemtrace.git] / net / ipv4 / igmp.c
blobd78599a9dbd5a2ebdeee1a2429cd2b243d341680
1 /*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
11 * Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
13 * Authors:
14 * Alan Cox <Alan.Cox@linux.org>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
21 * Fixes:
23 * Alan Cox : Added lots of __inline__ to optimise
24 * the memory usage of all the tiny little
25 * functions.
26 * Alan Cox : Dumped the header building experiment.
27 * Alan Cox : Minor tweaks ready for multicast routing
28 * and extended IGMP protocol.
29 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
30 * writes utterly bogus code otherwise (sigh)
31 * fixed IGMP loopback to behave in the manner
32 * desired by mrouted, fixed the fact it has been
33 * broken since 1.3.6 and cleaned up a few minor
34 * points.
36 * Chih-Jen Chang : Tried to revise IGMP to Version 2
37 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
38 * The enhancements are mainly based on Steve Deering's
39 * ipmulti-3.5 source code.
40 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
41 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
42 * the mrouted version on that device.
43 * Chih-Jen Chang : Added the max_resp_time parameter to
44 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
45 * to identify the multicast router version
46 * and do what the IGMP version 2 specified.
47 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
48 * Tsu-Sheng Tsao if the specified time expired.
49 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
50 * Alan Cox : Use GFP_ATOMIC in the right places.
51 * Christian Daudt : igmp timer wasn't set for local group
52 * memberships but was being deleted,
53 * which caused a "del_timer() called
54 * from %p with timer not initialized\n"
55 * message (960131).
56 * Christian Daudt : removed del_timer from
57 * igmp_timer_expire function (960205).
58 * Christian Daudt : igmp_heard_report now only calls
59 * igmp_timer_expire if tm->running is
60 * true (960216).
61 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
62 * igmp_heard_query never trigger. Expiry
63 * miscalculation fixed in igmp_heard_query
64 * and random() made to return unsigned to
65 * prevent negative expiry times.
66 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
67 * fix from pending 2.1.x patches.
68 * Alan Cox: Forget to enable FDDI support earlier.
69 * Alexey Kuznetsov: Fixed leaving groups on device down.
70 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
71 * David L Stevens: IGMPv3 support, with help from
72 * Vinay Kulkarni
75 #include <linux/module.h>
76 #include <asm/uaccess.h>
77 #include <asm/system.h>
78 #include <linux/types.h>
79 #include <linux/kernel.h>
80 #include <linux/jiffies.h>
81 #include <linux/string.h>
82 #include <linux/socket.h>
83 #include <linux/sockios.h>
84 #include <linux/in.h>
85 #include <linux/inet.h>
86 #include <linux/netdevice.h>
87 #include <linux/skbuff.h>
88 #include <linux/inetdevice.h>
89 #include <linux/igmp.h>
90 #include <linux/if_arp.h>
91 #include <linux/rtnetlink.h>
92 #include <linux/times.h>
94 #include <net/net_namespace.h>
95 #include <net/arp.h>
96 #include <net/ip.h>
97 #include <net/protocol.h>
98 #include <net/route.h>
99 #include <net/sock.h>
100 #include <net/checksum.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
104 #endif
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
108 #endif
110 #define IP_MAX_MEMBERSHIPS 20
111 #define IP_MAX_MSF 10
113 #ifdef CONFIG_IP_MULTICAST
114 /* Parameter names and values are taken from igmp-v2-06 draft */
116 #define IGMP_V1_Router_Present_Timeout (400*HZ)
117 #define IGMP_V2_Router_Present_Timeout (400*HZ)
118 #define IGMP_Unsolicited_Report_Interval (10*HZ)
119 #define IGMP_Query_Response_Interval (10*HZ)
120 #define IGMP_Unsolicited_Report_Count 2
123 #define IGMP_Initial_Report_Delay (1)
125 /* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
132 #define IGMP_V1_SEEN(in_dev) \
133 (IPV4_DEVCONF_ALL(FORCE_IGMP_VERSION) == 1 || \
134 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135 ((in_dev)->mr_v1_seen && \
136 time_before(jiffies, (in_dev)->mr_v1_seen)))
137 #define IGMP_V2_SEEN(in_dev) \
138 (IPV4_DEVCONF_ALL(FORCE_IGMP_VERSION) == 2 || \
139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140 ((in_dev)->mr_v2_seen && \
141 time_before(jiffies, (in_dev)->mr_v2_seen)))
143 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
144 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
145 static void igmpv3_clear_delrec(struct in_device *in_dev);
146 static int sf_setstate(struct ip_mc_list *pmc);
147 static void sf_markstate(struct ip_mc_list *pmc);
148 #endif
149 static void ip_mc_clear_src(struct ip_mc_list *pmc);
150 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
151 int sfcount, __be32 *psfsrc, int delta);
153 static void ip_ma_put(struct ip_mc_list *im)
155 if (atomic_dec_and_test(&im->refcnt)) {
156 in_dev_put(im->interface);
157 kfree(im);
161 #ifdef CONFIG_IP_MULTICAST
164 * Timer management
167 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
169 spin_lock_bh(&im->lock);
170 if (del_timer(&im->timer))
171 atomic_dec(&im->refcnt);
172 im->tm_running=0;
173 im->reporter = 0;
174 im->unsolicit_count = 0;
175 spin_unlock_bh(&im->lock);
178 /* It must be called with locked im->lock */
179 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
181 int tv=net_random() % max_delay;
183 im->tm_running=1;
184 if (!mod_timer(&im->timer, jiffies+tv+2))
185 atomic_inc(&im->refcnt);
188 static void igmp_gq_start_timer(struct in_device *in_dev)
190 int tv = net_random() % in_dev->mr_maxdelay;
192 in_dev->mr_gq_running = 1;
193 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
194 in_dev_hold(in_dev);
197 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
199 int tv = net_random() % delay;
201 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
202 in_dev_hold(in_dev);
205 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
207 spin_lock_bh(&im->lock);
208 im->unsolicit_count = 0;
209 if (del_timer(&im->timer)) {
210 if ((long)(im->timer.expires-jiffies) < max_delay) {
211 add_timer(&im->timer);
212 im->tm_running=1;
213 spin_unlock_bh(&im->lock);
214 return;
216 atomic_dec(&im->refcnt);
218 igmp_start_timer(im, max_delay);
219 spin_unlock_bh(&im->lock);
224 * Send an IGMP report.
227 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
230 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
231 int gdeleted, int sdeleted)
233 switch (type) {
234 case IGMPV3_MODE_IS_INCLUDE:
235 case IGMPV3_MODE_IS_EXCLUDE:
236 if (gdeleted || sdeleted)
237 return 0;
238 if (!(pmc->gsquery && !psf->sf_gsresp)) {
239 if (pmc->sfmode == MCAST_INCLUDE)
240 return 1;
241 /* don't include if this source is excluded
242 * in all filters
244 if (psf->sf_count[MCAST_INCLUDE])
245 return type == IGMPV3_MODE_IS_INCLUDE;
246 return pmc->sfcount[MCAST_EXCLUDE] ==
247 psf->sf_count[MCAST_EXCLUDE];
249 return 0;
250 case IGMPV3_CHANGE_TO_INCLUDE:
251 if (gdeleted || sdeleted)
252 return 0;
253 return psf->sf_count[MCAST_INCLUDE] != 0;
254 case IGMPV3_CHANGE_TO_EXCLUDE:
255 if (gdeleted || sdeleted)
256 return 0;
257 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
258 psf->sf_count[MCAST_INCLUDE])
259 return 0;
260 return pmc->sfcount[MCAST_EXCLUDE] ==
261 psf->sf_count[MCAST_EXCLUDE];
262 case IGMPV3_ALLOW_NEW_SOURCES:
263 if (gdeleted || !psf->sf_crcount)
264 return 0;
265 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
266 case IGMPV3_BLOCK_OLD_SOURCES:
267 if (pmc->sfmode == MCAST_INCLUDE)
268 return gdeleted || (psf->sf_crcount && sdeleted);
269 return psf->sf_crcount && !gdeleted && !sdeleted;
271 return 0;
274 static int
275 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
277 struct ip_sf_list *psf;
278 int scount = 0;
280 for (psf=pmc->sources; psf; psf=psf->sf_next) {
281 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
282 continue;
283 scount++;
285 return scount;
288 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
290 struct sk_buff *skb;
291 struct rtable *rt;
292 struct iphdr *pip;
293 struct igmpv3_report *pig;
295 skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
296 if (skb == NULL)
297 return NULL;
300 struct flowi fl = { .oif = dev->ifindex,
301 .nl_u = { .ip4_u = {
302 .daddr = IGMPV3_ALL_MCR } },
303 .proto = IPPROTO_IGMP };
304 if (ip_route_output_key(&rt, &fl)) {
305 kfree_skb(skb);
306 return NULL;
309 if (rt->rt_src == 0) {
310 kfree_skb(skb);
311 ip_rt_put(rt);
312 return NULL;
315 skb->dst = &rt->u.dst;
316 skb->dev = dev;
318 skb_reserve(skb, LL_RESERVED_SPACE(dev));
320 skb_reset_network_header(skb);
321 pip = ip_hdr(skb);
322 skb_put(skb, sizeof(struct iphdr) + 4);
324 pip->version = 4;
325 pip->ihl = (sizeof(struct iphdr)+4)>>2;
326 pip->tos = 0xc0;
327 pip->frag_off = htons(IP_DF);
328 pip->ttl = 1;
329 pip->daddr = rt->rt_dst;
330 pip->saddr = rt->rt_src;
331 pip->protocol = IPPROTO_IGMP;
332 pip->tot_len = 0; /* filled in later */
333 ip_select_ident(pip, &rt->u.dst, NULL);
334 ((u8*)&pip[1])[0] = IPOPT_RA;
335 ((u8*)&pip[1])[1] = 4;
336 ((u8*)&pip[1])[2] = 0;
337 ((u8*)&pip[1])[3] = 0;
339 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
340 skb_put(skb, sizeof(*pig));
341 pig = igmpv3_report_hdr(skb);
342 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
343 pig->resv1 = 0;
344 pig->csum = 0;
345 pig->resv2 = 0;
346 pig->ngrec = 0;
347 return skb;
350 static int igmpv3_sendpack(struct sk_buff *skb)
352 struct iphdr *pip = ip_hdr(skb);
353 struct igmphdr *pig = igmp_hdr(skb);
354 const int iplen = skb->tail - skb->network_header;
355 const int igmplen = skb->tail - skb->transport_header;
357 pip->tot_len = htons(iplen);
358 ip_send_check(pip);
359 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
361 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
362 dst_output);
365 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
367 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
370 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
371 int type, struct igmpv3_grec **ppgr)
373 struct net_device *dev = pmc->interface->dev;
374 struct igmpv3_report *pih;
375 struct igmpv3_grec *pgr;
377 if (!skb)
378 skb = igmpv3_newpack(dev, dev->mtu);
379 if (!skb)
380 return NULL;
381 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
382 pgr->grec_type = type;
383 pgr->grec_auxwords = 0;
384 pgr->grec_nsrcs = 0;
385 pgr->grec_mca = pmc->multiaddr;
386 pih = igmpv3_report_hdr(skb);
387 pih->ngrec = htons(ntohs(pih->ngrec)+1);
388 *ppgr = pgr;
389 return skb;
392 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
393 skb_tailroom(skb)) : 0)
395 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
396 int type, int gdeleted, int sdeleted)
398 struct net_device *dev = pmc->interface->dev;
399 struct igmpv3_report *pih;
400 struct igmpv3_grec *pgr = NULL;
401 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
402 int scount, stotal, first, isquery, truncate;
404 if (pmc->multiaddr == IGMP_ALL_HOSTS)
405 return skb;
407 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
408 type == IGMPV3_MODE_IS_EXCLUDE;
409 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
410 type == IGMPV3_CHANGE_TO_EXCLUDE;
412 stotal = scount = 0;
414 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
416 if (!*psf_list)
417 goto empty_source;
419 pih = skb ? igmpv3_report_hdr(skb) : NULL;
421 /* EX and TO_EX get a fresh packet, if needed */
422 if (truncate) {
423 if (pih && pih->ngrec &&
424 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
425 if (skb)
426 igmpv3_sendpack(skb);
427 skb = igmpv3_newpack(dev, dev->mtu);
430 first = 1;
431 psf_prev = NULL;
432 for (psf=*psf_list; psf; psf=psf_next) {
433 __be32 *psrc;
435 psf_next = psf->sf_next;
437 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
438 psf_prev = psf;
439 continue;
442 /* clear marks on query responses */
443 if (isquery)
444 psf->sf_gsresp = 0;
446 if (AVAILABLE(skb) < sizeof(__be32) +
447 first*sizeof(struct igmpv3_grec)) {
448 if (truncate && !first)
449 break; /* truncate these */
450 if (pgr)
451 pgr->grec_nsrcs = htons(scount);
452 if (skb)
453 igmpv3_sendpack(skb);
454 skb = igmpv3_newpack(dev, dev->mtu);
455 first = 1;
456 scount = 0;
458 if (first) {
459 skb = add_grhead(skb, pmc, type, &pgr);
460 first = 0;
462 if (!skb)
463 return NULL;
464 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
465 *psrc = psf->sf_inaddr;
466 scount++; stotal++;
467 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
468 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
469 psf->sf_crcount--;
470 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
471 if (psf_prev)
472 psf_prev->sf_next = psf->sf_next;
473 else
474 *psf_list = psf->sf_next;
475 kfree(psf);
476 continue;
479 psf_prev = psf;
482 empty_source:
483 if (!stotal) {
484 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
485 type == IGMPV3_BLOCK_OLD_SOURCES)
486 return skb;
487 if (pmc->crcount || isquery) {
488 /* make sure we have room for group header */
489 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
490 igmpv3_sendpack(skb);
491 skb = NULL; /* add_grhead will get a new one */
493 skb = add_grhead(skb, pmc, type, &pgr);
496 if (pgr)
497 pgr->grec_nsrcs = htons(scount);
499 if (isquery)
500 pmc->gsquery = 0; /* clear query state on report */
501 return skb;
504 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
506 struct sk_buff *skb = NULL;
507 int type;
509 if (!pmc) {
510 read_lock(&in_dev->mc_list_lock);
511 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
512 if (pmc->multiaddr == IGMP_ALL_HOSTS)
513 continue;
514 spin_lock_bh(&pmc->lock);
515 if (pmc->sfcount[MCAST_EXCLUDE])
516 type = IGMPV3_MODE_IS_EXCLUDE;
517 else
518 type = IGMPV3_MODE_IS_INCLUDE;
519 skb = add_grec(skb, pmc, type, 0, 0);
520 spin_unlock_bh(&pmc->lock);
522 read_unlock(&in_dev->mc_list_lock);
523 } else {
524 spin_lock_bh(&pmc->lock);
525 if (pmc->sfcount[MCAST_EXCLUDE])
526 type = IGMPV3_MODE_IS_EXCLUDE;
527 else
528 type = IGMPV3_MODE_IS_INCLUDE;
529 skb = add_grec(skb, pmc, type, 0, 0);
530 spin_unlock_bh(&pmc->lock);
532 if (!skb)
533 return 0;
534 return igmpv3_sendpack(skb);
538 * remove zero-count source records from a source filter list
540 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
542 struct ip_sf_list *psf_prev, *psf_next, *psf;
544 psf_prev = NULL;
545 for (psf=*ppsf; psf; psf = psf_next) {
546 psf_next = psf->sf_next;
547 if (psf->sf_crcount == 0) {
548 if (psf_prev)
549 psf_prev->sf_next = psf->sf_next;
550 else
551 *ppsf = psf->sf_next;
552 kfree(psf);
553 } else
554 psf_prev = psf;
558 static void igmpv3_send_cr(struct in_device *in_dev)
560 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
561 struct sk_buff *skb = NULL;
562 int type, dtype;
564 read_lock(&in_dev->mc_list_lock);
565 spin_lock_bh(&in_dev->mc_tomb_lock);
567 /* deleted MCA's */
568 pmc_prev = NULL;
569 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
570 pmc_next = pmc->next;
571 if (pmc->sfmode == MCAST_INCLUDE) {
572 type = IGMPV3_BLOCK_OLD_SOURCES;
573 dtype = IGMPV3_BLOCK_OLD_SOURCES;
574 skb = add_grec(skb, pmc, type, 1, 0);
575 skb = add_grec(skb, pmc, dtype, 1, 1);
577 if (pmc->crcount) {
578 if (pmc->sfmode == MCAST_EXCLUDE) {
579 type = IGMPV3_CHANGE_TO_INCLUDE;
580 skb = add_grec(skb, pmc, type, 1, 0);
582 pmc->crcount--;
583 if (pmc->crcount == 0) {
584 igmpv3_clear_zeros(&pmc->tomb);
585 igmpv3_clear_zeros(&pmc->sources);
588 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
589 if (pmc_prev)
590 pmc_prev->next = pmc_next;
591 else
592 in_dev->mc_tomb = pmc_next;
593 in_dev_put(pmc->interface);
594 kfree(pmc);
595 } else
596 pmc_prev = pmc;
598 spin_unlock_bh(&in_dev->mc_tomb_lock);
600 /* change recs */
601 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
602 spin_lock_bh(&pmc->lock);
603 if (pmc->sfcount[MCAST_EXCLUDE]) {
604 type = IGMPV3_BLOCK_OLD_SOURCES;
605 dtype = IGMPV3_ALLOW_NEW_SOURCES;
606 } else {
607 type = IGMPV3_ALLOW_NEW_SOURCES;
608 dtype = IGMPV3_BLOCK_OLD_SOURCES;
610 skb = add_grec(skb, pmc, type, 0, 0);
611 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
613 /* filter mode changes */
614 if (pmc->crcount) {
615 if (pmc->sfmode == MCAST_EXCLUDE)
616 type = IGMPV3_CHANGE_TO_EXCLUDE;
617 else
618 type = IGMPV3_CHANGE_TO_INCLUDE;
619 skb = add_grec(skb, pmc, type, 0, 0);
620 pmc->crcount--;
622 spin_unlock_bh(&pmc->lock);
624 read_unlock(&in_dev->mc_list_lock);
626 if (!skb)
627 return;
628 (void) igmpv3_sendpack(skb);
631 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
632 int type)
634 struct sk_buff *skb;
635 struct iphdr *iph;
636 struct igmphdr *ih;
637 struct rtable *rt;
638 struct net_device *dev = in_dev->dev;
639 __be32 group = pmc ? pmc->multiaddr : 0;
640 __be32 dst;
642 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
643 return igmpv3_send_report(in_dev, pmc);
644 else if (type == IGMP_HOST_LEAVE_MESSAGE)
645 dst = IGMP_ALL_ROUTER;
646 else
647 dst = group;
650 struct flowi fl = { .oif = dev->ifindex,
651 .nl_u = { .ip4_u = { .daddr = dst } },
652 .proto = IPPROTO_IGMP };
653 if (ip_route_output_key(&rt, &fl))
654 return -1;
656 if (rt->rt_src == 0) {
657 ip_rt_put(rt);
658 return -1;
661 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
662 if (skb == NULL) {
663 ip_rt_put(rt);
664 return -1;
667 skb->dst = &rt->u.dst;
669 skb_reserve(skb, LL_RESERVED_SPACE(dev));
671 skb_reset_network_header(skb);
672 iph = ip_hdr(skb);
673 skb_put(skb, sizeof(struct iphdr) + 4);
675 iph->version = 4;
676 iph->ihl = (sizeof(struct iphdr)+4)>>2;
677 iph->tos = 0xc0;
678 iph->frag_off = htons(IP_DF);
679 iph->ttl = 1;
680 iph->daddr = dst;
681 iph->saddr = rt->rt_src;
682 iph->protocol = IPPROTO_IGMP;
683 iph->tot_len = htons(IGMP_SIZE);
684 ip_select_ident(iph, &rt->u.dst, NULL);
685 ((u8*)&iph[1])[0] = IPOPT_RA;
686 ((u8*)&iph[1])[1] = 4;
687 ((u8*)&iph[1])[2] = 0;
688 ((u8*)&iph[1])[3] = 0;
689 ip_send_check(iph);
691 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
692 ih->type=type;
693 ih->code=0;
694 ih->csum=0;
695 ih->group=group;
696 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
698 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
699 dst_output);
702 static void igmp_gq_timer_expire(unsigned long data)
704 struct in_device *in_dev = (struct in_device *)data;
706 in_dev->mr_gq_running = 0;
707 igmpv3_send_report(in_dev, NULL);
708 __in_dev_put(in_dev);
711 static void igmp_ifc_timer_expire(unsigned long data)
713 struct in_device *in_dev = (struct in_device *)data;
715 igmpv3_send_cr(in_dev);
716 if (in_dev->mr_ifc_count) {
717 in_dev->mr_ifc_count--;
718 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
720 __in_dev_put(in_dev);
723 static void igmp_ifc_event(struct in_device *in_dev)
725 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
726 return;
727 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
728 IGMP_Unsolicited_Report_Count;
729 igmp_ifc_start_timer(in_dev, 1);
733 static void igmp_timer_expire(unsigned long data)
735 struct ip_mc_list *im=(struct ip_mc_list *)data;
736 struct in_device *in_dev = im->interface;
738 spin_lock(&im->lock);
739 im->tm_running=0;
741 if (im->unsolicit_count) {
742 im->unsolicit_count--;
743 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
745 im->reporter = 1;
746 spin_unlock(&im->lock);
748 if (IGMP_V1_SEEN(in_dev))
749 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
750 else if (IGMP_V2_SEEN(in_dev))
751 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
752 else
753 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
755 ip_ma_put(im);
758 /* mark EXCLUDE-mode sources */
759 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
761 struct ip_sf_list *psf;
762 int i, scount;
764 scount = 0;
765 for (psf=pmc->sources; psf; psf=psf->sf_next) {
766 if (scount == nsrcs)
767 break;
768 for (i=0; i<nsrcs; i++) {
769 /* skip inactive filters */
770 if (pmc->sfcount[MCAST_INCLUDE] ||
771 pmc->sfcount[MCAST_EXCLUDE] !=
772 psf->sf_count[MCAST_EXCLUDE])
773 continue;
774 if (srcs[i] == psf->sf_inaddr) {
775 scount++;
776 break;
780 pmc->gsquery = 0;
781 if (scount == nsrcs) /* all sources excluded */
782 return 0;
783 return 1;
786 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
788 struct ip_sf_list *psf;
789 int i, scount;
791 if (pmc->sfmode == MCAST_EXCLUDE)
792 return igmp_xmarksources(pmc, nsrcs, srcs);
794 /* mark INCLUDE-mode sources */
795 scount = 0;
796 for (psf=pmc->sources; psf; psf=psf->sf_next) {
797 if (scount == nsrcs)
798 break;
799 for (i=0; i<nsrcs; i++)
800 if (srcs[i] == psf->sf_inaddr) {
801 psf->sf_gsresp = 1;
802 scount++;
803 break;
806 if (!scount) {
807 pmc->gsquery = 0;
808 return 0;
810 pmc->gsquery = 1;
811 return 1;
814 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
816 struct ip_mc_list *im;
818 /* Timers are only set for non-local groups */
820 if (group == IGMP_ALL_HOSTS)
821 return;
823 read_lock(&in_dev->mc_list_lock);
824 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
825 if (im->multiaddr == group) {
826 igmp_stop_timer(im);
827 break;
830 read_unlock(&in_dev->mc_list_lock);
833 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
834 int len)
836 struct igmphdr *ih = igmp_hdr(skb);
837 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
838 struct ip_mc_list *im;
839 __be32 group = ih->group;
840 int max_delay;
841 int mark = 0;
844 if (len == 8) {
845 if (ih->code == 0) {
846 /* Alas, old v1 router presents here. */
848 max_delay = IGMP_Query_Response_Interval;
849 in_dev->mr_v1_seen = jiffies +
850 IGMP_V1_Router_Present_Timeout;
851 group = 0;
852 } else {
853 /* v2 router present */
854 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
855 in_dev->mr_v2_seen = jiffies +
856 IGMP_V2_Router_Present_Timeout;
858 /* cancel the interface change timer */
859 in_dev->mr_ifc_count = 0;
860 if (del_timer(&in_dev->mr_ifc_timer))
861 __in_dev_put(in_dev);
862 /* clear deleted report items */
863 igmpv3_clear_delrec(in_dev);
864 } else if (len < 12) {
865 return; /* ignore bogus packet; freed by caller */
866 } else { /* v3 */
867 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
868 return;
870 ih3 = igmpv3_query_hdr(skb);
871 if (ih3->nsrcs) {
872 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
873 + ntohs(ih3->nsrcs)*sizeof(__be32)))
874 return;
875 ih3 = igmpv3_query_hdr(skb);
878 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
879 if (!max_delay)
880 max_delay = 1; /* can't mod w/ 0 */
881 in_dev->mr_maxdelay = max_delay;
882 if (ih3->qrv)
883 in_dev->mr_qrv = ih3->qrv;
884 if (!group) { /* general query */
885 if (ih3->nsrcs)
886 return; /* no sources allowed */
887 igmp_gq_start_timer(in_dev);
888 return;
890 /* mark sources to include, if group & source-specific */
891 mark = ih3->nsrcs != 0;
895 * - Start the timers in all of our membership records
896 * that the query applies to for the interface on
897 * which the query arrived excl. those that belong
898 * to a "local" group (224.0.0.X)
899 * - For timers already running check if they need to
900 * be reset.
901 * - Use the igmp->igmp_code field as the maximum
902 * delay possible
904 read_lock(&in_dev->mc_list_lock);
905 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
906 int changed;
908 if (group && group != im->multiaddr)
909 continue;
910 if (im->multiaddr == IGMP_ALL_HOSTS)
911 continue;
912 spin_lock_bh(&im->lock);
913 if (im->tm_running)
914 im->gsquery = im->gsquery && mark;
915 else
916 im->gsquery = mark;
917 changed = !im->gsquery ||
918 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
919 spin_unlock_bh(&im->lock);
920 if (changed)
921 igmp_mod_timer(im, max_delay);
923 read_unlock(&in_dev->mc_list_lock);
926 int igmp_rcv(struct sk_buff *skb)
928 /* This basically follows the spec line by line -- see RFC1112 */
929 struct igmphdr *ih;
930 struct in_device *in_dev = in_dev_get(skb->dev);
931 int len = skb->len;
933 if (in_dev==NULL) {
934 kfree_skb(skb);
935 return 0;
938 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
939 goto drop;
941 switch (skb->ip_summed) {
942 case CHECKSUM_COMPLETE:
943 if (!csum_fold(skb->csum))
944 break;
945 /* fall through */
946 case CHECKSUM_NONE:
947 skb->csum = 0;
948 if (__skb_checksum_complete(skb))
949 goto drop;
952 ih = igmp_hdr(skb);
953 switch (ih->type) {
954 case IGMP_HOST_MEMBERSHIP_QUERY:
955 igmp_heard_query(in_dev, skb, len);
956 break;
957 case IGMP_HOST_MEMBERSHIP_REPORT:
958 case IGMPV2_HOST_MEMBERSHIP_REPORT:
959 case IGMPV3_HOST_MEMBERSHIP_REPORT:
960 /* Is it our report looped back? */
961 if (((struct rtable*)skb->dst)->fl.iif == 0)
962 break;
963 /* don't rely on MC router hearing unicast reports */
964 if (skb->pkt_type == PACKET_MULTICAST ||
965 skb->pkt_type == PACKET_BROADCAST)
966 igmp_heard_report(in_dev, ih->group);
967 break;
968 case IGMP_PIM:
969 #ifdef CONFIG_IP_PIMSM_V1
970 in_dev_put(in_dev);
971 return pim_rcv_v1(skb);
972 #endif
973 case IGMP_DVMRP:
974 case IGMP_TRACE:
975 case IGMP_HOST_LEAVE_MESSAGE:
976 case IGMP_MTRACE:
977 case IGMP_MTRACE_RESP:
978 break;
979 default:
980 break;
983 drop:
984 in_dev_put(in_dev);
985 kfree_skb(skb);
986 return 0;
989 #endif
993 * Add a filter to a device
996 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
998 char buf[MAX_ADDR_LEN];
999 struct net_device *dev = in_dev->dev;
1001 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1002 We will get multicast token leakage, when IFF_MULTICAST
1003 is changed. This check should be done in dev->set_multicast_list
1004 routine. Something sort of:
1005 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1006 --ANK
1008 if (arp_mc_map(addr, buf, dev, 0) == 0)
1009 dev_mc_add(dev,buf,dev->addr_len,0);
1013 * Remove a filter from a device
1016 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1018 char buf[MAX_ADDR_LEN];
1019 struct net_device *dev = in_dev->dev;
1021 if (arp_mc_map(addr, buf, dev, 0) == 0)
1022 dev_mc_delete(dev,buf,dev->addr_len,0);
1025 #ifdef CONFIG_IP_MULTICAST
1027 * deleted ip_mc_list manipulation
1029 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1031 struct ip_mc_list *pmc;
1033 /* this is an "ip_mc_list" for convenience; only the fields below
1034 * are actually used. In particular, the refcnt and users are not
1035 * used for management of the delete list. Using the same structure
1036 * for deleted items allows change reports to use common code with
1037 * non-deleted or query-response MCA's.
1039 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1040 if (!pmc)
1041 return;
1042 spin_lock_bh(&im->lock);
1043 pmc->interface = im->interface;
1044 in_dev_hold(in_dev);
1045 pmc->multiaddr = im->multiaddr;
1046 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1047 IGMP_Unsolicited_Report_Count;
1048 pmc->sfmode = im->sfmode;
1049 if (pmc->sfmode == MCAST_INCLUDE) {
1050 struct ip_sf_list *psf;
1052 pmc->tomb = im->tomb;
1053 pmc->sources = im->sources;
1054 im->tomb = im->sources = NULL;
1055 for (psf=pmc->sources; psf; psf=psf->sf_next)
1056 psf->sf_crcount = pmc->crcount;
1058 spin_unlock_bh(&im->lock);
1060 spin_lock_bh(&in_dev->mc_tomb_lock);
1061 pmc->next = in_dev->mc_tomb;
1062 in_dev->mc_tomb = pmc;
1063 spin_unlock_bh(&in_dev->mc_tomb_lock);
1066 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1068 struct ip_mc_list *pmc, *pmc_prev;
1069 struct ip_sf_list *psf, *psf_next;
1071 spin_lock_bh(&in_dev->mc_tomb_lock);
1072 pmc_prev = NULL;
1073 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1074 if (pmc->multiaddr == multiaddr)
1075 break;
1076 pmc_prev = pmc;
1078 if (pmc) {
1079 if (pmc_prev)
1080 pmc_prev->next = pmc->next;
1081 else
1082 in_dev->mc_tomb = pmc->next;
1084 spin_unlock_bh(&in_dev->mc_tomb_lock);
1085 if (pmc) {
1086 for (psf=pmc->tomb; psf; psf=psf_next) {
1087 psf_next = psf->sf_next;
1088 kfree(psf);
1090 in_dev_put(pmc->interface);
1091 kfree(pmc);
1095 static void igmpv3_clear_delrec(struct in_device *in_dev)
1097 struct ip_mc_list *pmc, *nextpmc;
1099 spin_lock_bh(&in_dev->mc_tomb_lock);
1100 pmc = in_dev->mc_tomb;
1101 in_dev->mc_tomb = NULL;
1102 spin_unlock_bh(&in_dev->mc_tomb_lock);
1104 for (; pmc; pmc = nextpmc) {
1105 nextpmc = pmc->next;
1106 ip_mc_clear_src(pmc);
1107 in_dev_put(pmc->interface);
1108 kfree(pmc);
1110 /* clear dead sources, too */
1111 read_lock(&in_dev->mc_list_lock);
1112 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1113 struct ip_sf_list *psf, *psf_next;
1115 spin_lock_bh(&pmc->lock);
1116 psf = pmc->tomb;
1117 pmc->tomb = NULL;
1118 spin_unlock_bh(&pmc->lock);
1119 for (; psf; psf=psf_next) {
1120 psf_next = psf->sf_next;
1121 kfree(psf);
1124 read_unlock(&in_dev->mc_list_lock);
1126 #endif
1128 static void igmp_group_dropped(struct ip_mc_list *im)
1130 struct in_device *in_dev = im->interface;
1131 #ifdef CONFIG_IP_MULTICAST
1132 int reporter;
1133 #endif
1135 if (im->loaded) {
1136 im->loaded = 0;
1137 ip_mc_filter_del(in_dev, im->multiaddr);
1140 #ifdef CONFIG_IP_MULTICAST
1141 if (im->multiaddr == IGMP_ALL_HOSTS)
1142 return;
1144 reporter = im->reporter;
1145 igmp_stop_timer(im);
1147 if (!in_dev->dead) {
1148 if (IGMP_V1_SEEN(in_dev))
1149 goto done;
1150 if (IGMP_V2_SEEN(in_dev)) {
1151 if (reporter)
1152 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1153 goto done;
1155 /* IGMPv3 */
1156 igmpv3_add_delrec(in_dev, im);
1158 igmp_ifc_event(in_dev);
1160 done:
1161 #endif
1162 ip_mc_clear_src(im);
1165 static void igmp_group_added(struct ip_mc_list *im)
1167 struct in_device *in_dev = im->interface;
1169 if (im->loaded == 0) {
1170 im->loaded = 1;
1171 ip_mc_filter_add(in_dev, im->multiaddr);
1174 #ifdef CONFIG_IP_MULTICAST
1175 if (im->multiaddr == IGMP_ALL_HOSTS)
1176 return;
1178 if (in_dev->dead)
1179 return;
1180 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1181 spin_lock_bh(&im->lock);
1182 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1183 spin_unlock_bh(&im->lock);
1184 return;
1186 /* else, v3 */
1188 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1189 IGMP_Unsolicited_Report_Count;
1190 igmp_ifc_event(in_dev);
1191 #endif
1196 * Multicast list managers
1201 * A socket has joined a multicast group on device dev.
1204 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1206 struct ip_mc_list *im;
1208 ASSERT_RTNL();
1210 for (im=in_dev->mc_list; im; im=im->next) {
1211 if (im->multiaddr == addr) {
1212 im->users++;
1213 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1214 goto out;
1218 im = kmalloc(sizeof(*im), GFP_KERNEL);
1219 if (!im)
1220 goto out;
1222 im->users=1;
1223 im->interface=in_dev;
1224 in_dev_hold(in_dev);
1225 im->multiaddr=addr;
1226 /* initial mode is (EX, empty) */
1227 im->sfmode = MCAST_EXCLUDE;
1228 im->sfcount[MCAST_INCLUDE] = 0;
1229 im->sfcount[MCAST_EXCLUDE] = 1;
1230 im->sources = NULL;
1231 im->tomb = NULL;
1232 im->crcount = 0;
1233 atomic_set(&im->refcnt, 1);
1234 spin_lock_init(&im->lock);
1235 #ifdef CONFIG_IP_MULTICAST
1236 im->tm_running=0;
1237 init_timer(&im->timer);
1238 im->timer.data=(unsigned long)im;
1239 im->timer.function=&igmp_timer_expire;
1240 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1241 im->reporter = 0;
1242 im->gsquery = 0;
1243 #endif
1244 im->loaded = 0;
1245 write_lock_bh(&in_dev->mc_list_lock);
1246 im->next=in_dev->mc_list;
1247 in_dev->mc_list=im;
1248 write_unlock_bh(&in_dev->mc_list_lock);
1249 #ifdef CONFIG_IP_MULTICAST
1250 igmpv3_del_delrec(in_dev, im->multiaddr);
1251 #endif
1252 igmp_group_added(im);
1253 if (!in_dev->dead)
1254 ip_rt_multicast_event(in_dev);
1255 out:
1256 return;
1260 * Resend IGMP JOIN report; used for bonding.
1262 void ip_mc_rejoin_group(struct ip_mc_list *im)
1264 #ifdef CONFIG_IP_MULTICAST
1265 struct in_device *in_dev = im->interface;
1267 if (im->multiaddr == IGMP_ALL_HOSTS)
1268 return;
1270 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1271 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1272 return;
1274 /* else, v3 */
1275 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1276 IGMP_Unsolicited_Report_Count;
1277 igmp_ifc_event(in_dev);
1278 #endif
1282 * A socket has left a multicast group on device dev
1285 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1287 struct ip_mc_list *i, **ip;
1289 ASSERT_RTNL();
1291 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1292 if (i->multiaddr==addr) {
1293 if (--i->users == 0) {
1294 write_lock_bh(&in_dev->mc_list_lock);
1295 *ip = i->next;
1296 write_unlock_bh(&in_dev->mc_list_lock);
1297 igmp_group_dropped(i);
1299 if (!in_dev->dead)
1300 ip_rt_multicast_event(in_dev);
1302 ip_ma_put(i);
1303 return;
1305 break;
1310 /* Device going down */
1312 void ip_mc_down(struct in_device *in_dev)
1314 struct ip_mc_list *i;
1316 ASSERT_RTNL();
1318 for (i=in_dev->mc_list; i; i=i->next)
1319 igmp_group_dropped(i);
1321 #ifdef CONFIG_IP_MULTICAST
1322 in_dev->mr_ifc_count = 0;
1323 if (del_timer(&in_dev->mr_ifc_timer))
1324 __in_dev_put(in_dev);
1325 in_dev->mr_gq_running = 0;
1326 if (del_timer(&in_dev->mr_gq_timer))
1327 __in_dev_put(in_dev);
1328 igmpv3_clear_delrec(in_dev);
1329 #endif
1331 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1334 void ip_mc_init_dev(struct in_device *in_dev)
1336 ASSERT_RTNL();
1338 in_dev->mc_tomb = NULL;
1339 #ifdef CONFIG_IP_MULTICAST
1340 in_dev->mr_gq_running = 0;
1341 init_timer(&in_dev->mr_gq_timer);
1342 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1343 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1344 in_dev->mr_ifc_count = 0;
1345 init_timer(&in_dev->mr_ifc_timer);
1346 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1347 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1348 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1349 #endif
1351 rwlock_init(&in_dev->mc_list_lock);
1352 spin_lock_init(&in_dev->mc_tomb_lock);
1355 /* Device going up */
1357 void ip_mc_up(struct in_device *in_dev)
1359 struct ip_mc_list *i;
1361 ASSERT_RTNL();
1363 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1365 for (i=in_dev->mc_list; i; i=i->next)
1366 igmp_group_added(i);
1370 * Device is about to be destroyed: clean up.
1373 void ip_mc_destroy_dev(struct in_device *in_dev)
1375 struct ip_mc_list *i;
1377 ASSERT_RTNL();
1379 /* Deactivate timers */
1380 ip_mc_down(in_dev);
1382 write_lock_bh(&in_dev->mc_list_lock);
1383 while ((i = in_dev->mc_list) != NULL) {
1384 in_dev->mc_list = i->next;
1385 write_unlock_bh(&in_dev->mc_list_lock);
1387 igmp_group_dropped(i);
1388 ip_ma_put(i);
1390 write_lock_bh(&in_dev->mc_list_lock);
1392 write_unlock_bh(&in_dev->mc_list_lock);
1395 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1397 struct flowi fl = { .nl_u = { .ip4_u =
1398 { .daddr = imr->imr_multiaddr.s_addr } } };
1399 struct rtable *rt;
1400 struct net_device *dev = NULL;
1401 struct in_device *idev = NULL;
1403 if (imr->imr_ifindex) {
1404 idev = inetdev_by_index(imr->imr_ifindex);
1405 if (idev)
1406 __in_dev_put(idev);
1407 return idev;
1409 if (imr->imr_address.s_addr) {
1410 dev = ip_dev_find(imr->imr_address.s_addr);
1411 if (!dev)
1412 return NULL;
1413 dev_put(dev);
1416 if (!dev && !ip_route_output_key(&rt, &fl)) {
1417 dev = rt->u.dst.dev;
1418 ip_rt_put(rt);
1420 if (dev) {
1421 imr->imr_ifindex = dev->ifindex;
1422 idev = __in_dev_get_rtnl(dev);
1424 return idev;
1428 * Join a socket to a group
1430 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1431 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1434 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1435 __be32 *psfsrc)
1437 struct ip_sf_list *psf, *psf_prev;
1438 int rv = 0;
1440 psf_prev = NULL;
1441 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1442 if (psf->sf_inaddr == *psfsrc)
1443 break;
1444 psf_prev = psf;
1446 if (!psf || psf->sf_count[sfmode] == 0) {
1447 /* source filter not found, or count wrong => bug */
1448 return -ESRCH;
1450 psf->sf_count[sfmode]--;
1451 if (psf->sf_count[sfmode] == 0) {
1452 ip_rt_multicast_event(pmc->interface);
1454 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1455 #ifdef CONFIG_IP_MULTICAST
1456 struct in_device *in_dev = pmc->interface;
1457 #endif
1459 /* no more filters for this source */
1460 if (psf_prev)
1461 psf_prev->sf_next = psf->sf_next;
1462 else
1463 pmc->sources = psf->sf_next;
1464 #ifdef CONFIG_IP_MULTICAST
1465 if (psf->sf_oldin &&
1466 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1467 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1468 IGMP_Unsolicited_Report_Count;
1469 psf->sf_next = pmc->tomb;
1470 pmc->tomb = psf;
1471 rv = 1;
1472 } else
1473 #endif
1474 kfree(psf);
1476 return rv;
1479 #ifndef CONFIG_IP_MULTICAST
1480 #define igmp_ifc_event(x) do { } while (0)
1481 #endif
1483 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1484 int sfcount, __be32 *psfsrc, int delta)
1486 struct ip_mc_list *pmc;
1487 int changerec = 0;
1488 int i, err;
1490 if (!in_dev)
1491 return -ENODEV;
1492 read_lock(&in_dev->mc_list_lock);
1493 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1494 if (*pmca == pmc->multiaddr)
1495 break;
1497 if (!pmc) {
1498 /* MCA not found?? bug */
1499 read_unlock(&in_dev->mc_list_lock);
1500 return -ESRCH;
1502 spin_lock_bh(&pmc->lock);
1503 read_unlock(&in_dev->mc_list_lock);
1504 #ifdef CONFIG_IP_MULTICAST
1505 sf_markstate(pmc);
1506 #endif
1507 if (!delta) {
1508 err = -EINVAL;
1509 if (!pmc->sfcount[sfmode])
1510 goto out_unlock;
1511 pmc->sfcount[sfmode]--;
1513 err = 0;
1514 for (i=0; i<sfcount; i++) {
1515 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1517 changerec |= rv > 0;
1518 if (!err && rv < 0)
1519 err = rv;
1521 if (pmc->sfmode == MCAST_EXCLUDE &&
1522 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1523 pmc->sfcount[MCAST_INCLUDE]) {
1524 #ifdef CONFIG_IP_MULTICAST
1525 struct ip_sf_list *psf;
1526 #endif
1528 /* filter mode change */
1529 pmc->sfmode = MCAST_INCLUDE;
1530 #ifdef CONFIG_IP_MULTICAST
1531 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1532 IGMP_Unsolicited_Report_Count;
1533 in_dev->mr_ifc_count = pmc->crcount;
1534 for (psf=pmc->sources; psf; psf = psf->sf_next)
1535 psf->sf_crcount = 0;
1536 igmp_ifc_event(pmc->interface);
1537 } else if (sf_setstate(pmc) || changerec) {
1538 igmp_ifc_event(pmc->interface);
1539 #endif
1541 out_unlock:
1542 spin_unlock_bh(&pmc->lock);
1543 return err;
1547 * Add multicast single-source filter to the interface list
1549 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1550 __be32 *psfsrc, int delta)
1552 struct ip_sf_list *psf, *psf_prev;
1554 psf_prev = NULL;
1555 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1556 if (psf->sf_inaddr == *psfsrc)
1557 break;
1558 psf_prev = psf;
1560 if (!psf) {
1561 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1562 if (!psf)
1563 return -ENOBUFS;
1564 psf->sf_inaddr = *psfsrc;
1565 if (psf_prev) {
1566 psf_prev->sf_next = psf;
1567 } else
1568 pmc->sources = psf;
1570 psf->sf_count[sfmode]++;
1571 if (psf->sf_count[sfmode] == 1) {
1572 ip_rt_multicast_event(pmc->interface);
1574 return 0;
1577 #ifdef CONFIG_IP_MULTICAST
1578 static void sf_markstate(struct ip_mc_list *pmc)
1580 struct ip_sf_list *psf;
1581 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1583 for (psf=pmc->sources; psf; psf=psf->sf_next)
1584 if (pmc->sfcount[MCAST_EXCLUDE]) {
1585 psf->sf_oldin = mca_xcount ==
1586 psf->sf_count[MCAST_EXCLUDE] &&
1587 !psf->sf_count[MCAST_INCLUDE];
1588 } else
1589 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1592 static int sf_setstate(struct ip_mc_list *pmc)
1594 struct ip_sf_list *psf, *dpsf;
1595 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1596 int qrv = pmc->interface->mr_qrv;
1597 int new_in, rv;
1599 rv = 0;
1600 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1601 if (pmc->sfcount[MCAST_EXCLUDE]) {
1602 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1603 !psf->sf_count[MCAST_INCLUDE];
1604 } else
1605 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1606 if (new_in) {
1607 if (!psf->sf_oldin) {
1608 struct ip_sf_list *prev = NULL;
1610 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1611 if (dpsf->sf_inaddr == psf->sf_inaddr)
1612 break;
1613 prev = dpsf;
1615 if (dpsf) {
1616 if (prev)
1617 prev->sf_next = dpsf->sf_next;
1618 else
1619 pmc->tomb = dpsf->sf_next;
1620 kfree(dpsf);
1622 psf->sf_crcount = qrv;
1623 rv++;
1625 } else if (psf->sf_oldin) {
1627 psf->sf_crcount = 0;
1629 * add or update "delete" records if an active filter
1630 * is now inactive
1632 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1633 if (dpsf->sf_inaddr == psf->sf_inaddr)
1634 break;
1635 if (!dpsf) {
1636 dpsf = (struct ip_sf_list *)
1637 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1638 if (!dpsf)
1639 continue;
1640 *dpsf = *psf;
1641 /* pmc->lock held by callers */
1642 dpsf->sf_next = pmc->tomb;
1643 pmc->tomb = dpsf;
1645 dpsf->sf_crcount = qrv;
1646 rv++;
1649 return rv;
1651 #endif
1654 * Add multicast source filter list to the interface list
1656 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1657 int sfcount, __be32 *psfsrc, int delta)
1659 struct ip_mc_list *pmc;
1660 int isexclude;
1661 int i, err;
1663 if (!in_dev)
1664 return -ENODEV;
1665 read_lock(&in_dev->mc_list_lock);
1666 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1667 if (*pmca == pmc->multiaddr)
1668 break;
1670 if (!pmc) {
1671 /* MCA not found?? bug */
1672 read_unlock(&in_dev->mc_list_lock);
1673 return -ESRCH;
1675 spin_lock_bh(&pmc->lock);
1676 read_unlock(&in_dev->mc_list_lock);
1678 #ifdef CONFIG_IP_MULTICAST
1679 sf_markstate(pmc);
1680 #endif
1681 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1682 if (!delta)
1683 pmc->sfcount[sfmode]++;
1684 err = 0;
1685 for (i=0; i<sfcount; i++) {
1686 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1687 if (err)
1688 break;
1690 if (err) {
1691 int j;
1693 pmc->sfcount[sfmode]--;
1694 for (j=0; j<i; j++)
1695 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1696 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1697 #ifdef CONFIG_IP_MULTICAST
1698 struct in_device *in_dev = pmc->interface;
1699 struct ip_sf_list *psf;
1700 #endif
1702 /* filter mode change */
1703 if (pmc->sfcount[MCAST_EXCLUDE])
1704 pmc->sfmode = MCAST_EXCLUDE;
1705 else if (pmc->sfcount[MCAST_INCLUDE])
1706 pmc->sfmode = MCAST_INCLUDE;
1707 #ifdef CONFIG_IP_MULTICAST
1708 /* else no filters; keep old mode for reports */
1710 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1711 IGMP_Unsolicited_Report_Count;
1712 in_dev->mr_ifc_count = pmc->crcount;
1713 for (psf=pmc->sources; psf; psf = psf->sf_next)
1714 psf->sf_crcount = 0;
1715 igmp_ifc_event(in_dev);
1716 } else if (sf_setstate(pmc)) {
1717 igmp_ifc_event(in_dev);
1718 #endif
1720 spin_unlock_bh(&pmc->lock);
1721 return err;
1724 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1726 struct ip_sf_list *psf, *nextpsf;
1728 for (psf=pmc->tomb; psf; psf=nextpsf) {
1729 nextpsf = psf->sf_next;
1730 kfree(psf);
1732 pmc->tomb = NULL;
1733 for (psf=pmc->sources; psf; psf=nextpsf) {
1734 nextpsf = psf->sf_next;
1735 kfree(psf);
1737 pmc->sources = NULL;
1738 pmc->sfmode = MCAST_EXCLUDE;
1739 pmc->sfcount[MCAST_INCLUDE] = 0;
1740 pmc->sfcount[MCAST_EXCLUDE] = 1;
1745 * Join a multicast group
1747 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1749 int err;
1750 __be32 addr = imr->imr_multiaddr.s_addr;
1751 struct ip_mc_socklist *iml=NULL, *i;
1752 struct in_device *in_dev;
1753 struct inet_sock *inet = inet_sk(sk);
1754 int ifindex;
1755 int count = 0;
1757 if (!MULTICAST(addr))
1758 return -EINVAL;
1760 rtnl_lock();
1762 in_dev = ip_mc_find_dev(imr);
1764 if (!in_dev) {
1765 iml = NULL;
1766 err = -ENODEV;
1767 goto done;
1770 err = -EADDRINUSE;
1771 ifindex = imr->imr_ifindex;
1772 for (i = inet->mc_list; i; i = i->next) {
1773 if (i->multi.imr_multiaddr.s_addr == addr &&
1774 i->multi.imr_ifindex == ifindex)
1775 goto done;
1776 count++;
1778 err = -ENOBUFS;
1779 if (count >= sysctl_igmp_max_memberships)
1780 goto done;
1781 iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1782 if (iml == NULL)
1783 goto done;
1785 memcpy(&iml->multi, imr, sizeof(*imr));
1786 iml->next = inet->mc_list;
1787 iml->sflist = NULL;
1788 iml->sfmode = MCAST_EXCLUDE;
1789 inet->mc_list = iml;
1790 ip_mc_inc_group(in_dev, addr);
1791 err = 0;
1792 done:
1793 rtnl_unlock();
1794 return err;
1797 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1798 struct in_device *in_dev)
1800 int err;
1802 if (iml->sflist == 0) {
1803 /* any-source empty exclude case */
1804 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1805 iml->sfmode, 0, NULL, 0);
1807 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1808 iml->sfmode, iml->sflist->sl_count,
1809 iml->sflist->sl_addr, 0);
1810 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1811 iml->sflist = NULL;
1812 return err;
1816 * Ask a socket to leave a group.
1819 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1821 struct inet_sock *inet = inet_sk(sk);
1822 struct ip_mc_socklist *iml, **imlp;
1823 struct in_device *in_dev;
1824 __be32 group = imr->imr_multiaddr.s_addr;
1825 u32 ifindex;
1826 int ret = -EADDRNOTAVAIL;
1828 rtnl_lock();
1829 in_dev = ip_mc_find_dev(imr);
1830 ifindex = imr->imr_ifindex;
1831 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1832 if (iml->multi.imr_multiaddr.s_addr != group)
1833 continue;
1834 if (ifindex) {
1835 if (iml->multi.imr_ifindex != ifindex)
1836 continue;
1837 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1838 iml->multi.imr_address.s_addr)
1839 continue;
1841 (void) ip_mc_leave_src(sk, iml, in_dev);
1843 *imlp = iml->next;
1845 if (in_dev)
1846 ip_mc_dec_group(in_dev, group);
1847 rtnl_unlock();
1848 sock_kfree_s(sk, iml, sizeof(*iml));
1849 return 0;
1851 if (!in_dev)
1852 ret = -ENODEV;
1853 rtnl_unlock();
1854 return ret;
1857 int ip_mc_source(int add, int omode, struct sock *sk, struct
1858 ip_mreq_source *mreqs, int ifindex)
1860 int err;
1861 struct ip_mreqn imr;
1862 __be32 addr = mreqs->imr_multiaddr;
1863 struct ip_mc_socklist *pmc;
1864 struct in_device *in_dev = NULL;
1865 struct inet_sock *inet = inet_sk(sk);
1866 struct ip_sf_socklist *psl;
1867 int leavegroup = 0;
1868 int i, j, rv;
1870 if (!MULTICAST(addr))
1871 return -EINVAL;
1873 rtnl_lock();
1875 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1876 imr.imr_address.s_addr = mreqs->imr_interface;
1877 imr.imr_ifindex = ifindex;
1878 in_dev = ip_mc_find_dev(&imr);
1880 if (!in_dev) {
1881 err = -ENODEV;
1882 goto done;
1884 err = -EADDRNOTAVAIL;
1886 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1887 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1888 && pmc->multi.imr_ifindex == imr.imr_ifindex)
1889 break;
1891 if (!pmc) { /* must have a prior join */
1892 err = -EINVAL;
1893 goto done;
1895 /* if a source filter was set, must be the same mode as before */
1896 if (pmc->sflist) {
1897 if (pmc->sfmode != omode) {
1898 err = -EINVAL;
1899 goto done;
1901 } else if (pmc->sfmode != omode) {
1902 /* allow mode switches for empty-set filters */
1903 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1904 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1905 NULL, 0);
1906 pmc->sfmode = omode;
1909 psl = pmc->sflist;
1910 if (!add) {
1911 if (!psl)
1912 goto done; /* err = -EADDRNOTAVAIL */
1913 rv = !0;
1914 for (i=0; i<psl->sl_count; i++) {
1915 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1916 sizeof(__be32));
1917 if (rv == 0)
1918 break;
1920 if (rv) /* source not found */
1921 goto done; /* err = -EADDRNOTAVAIL */
1923 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1924 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1925 leavegroup = 1;
1926 goto done;
1929 /* update the interface filter */
1930 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1931 &mreqs->imr_sourceaddr, 1);
1933 for (j=i+1; j<psl->sl_count; j++)
1934 psl->sl_addr[j-1] = psl->sl_addr[j];
1935 psl->sl_count--;
1936 err = 0;
1937 goto done;
1939 /* else, add a new source to the filter */
1941 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1942 err = -ENOBUFS;
1943 goto done;
1945 if (!psl || psl->sl_count == psl->sl_max) {
1946 struct ip_sf_socklist *newpsl;
1947 int count = IP_SFBLOCK;
1949 if (psl)
1950 count += psl->sl_max;
1951 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1952 if (!newpsl) {
1953 err = -ENOBUFS;
1954 goto done;
1956 newpsl->sl_max = count;
1957 newpsl->sl_count = count - IP_SFBLOCK;
1958 if (psl) {
1959 for (i=0; i<psl->sl_count; i++)
1960 newpsl->sl_addr[i] = psl->sl_addr[i];
1961 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1963 pmc->sflist = psl = newpsl;
1965 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1966 for (i=0; i<psl->sl_count; i++) {
1967 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1968 sizeof(__be32));
1969 if (rv == 0)
1970 break;
1972 if (rv == 0) /* address already there is an error */
1973 goto done;
1974 for (j=psl->sl_count-1; j>=i; j--)
1975 psl->sl_addr[j+1] = psl->sl_addr[j];
1976 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1977 psl->sl_count++;
1978 err = 0;
1979 /* update the interface list */
1980 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1981 &mreqs->imr_sourceaddr, 1);
1982 done:
1983 rtnl_unlock();
1984 if (leavegroup)
1985 return ip_mc_leave_group(sk, &imr);
1986 return err;
1989 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1991 int err = 0;
1992 struct ip_mreqn imr;
1993 __be32 addr = msf->imsf_multiaddr;
1994 struct ip_mc_socklist *pmc;
1995 struct in_device *in_dev;
1996 struct inet_sock *inet = inet_sk(sk);
1997 struct ip_sf_socklist *newpsl, *psl;
1998 int leavegroup = 0;
2000 if (!MULTICAST(addr))
2001 return -EINVAL;
2002 if (msf->imsf_fmode != MCAST_INCLUDE &&
2003 msf->imsf_fmode != MCAST_EXCLUDE)
2004 return -EINVAL;
2006 rtnl_lock();
2008 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2009 imr.imr_address.s_addr = msf->imsf_interface;
2010 imr.imr_ifindex = ifindex;
2011 in_dev = ip_mc_find_dev(&imr);
2013 if (!in_dev) {
2014 err = -ENODEV;
2015 goto done;
2018 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2019 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2020 leavegroup = 1;
2021 goto done;
2024 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2025 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2026 pmc->multi.imr_ifindex == imr.imr_ifindex)
2027 break;
2029 if (!pmc) { /* must have a prior join */
2030 err = -EINVAL;
2031 goto done;
2033 if (msf->imsf_numsrc) {
2034 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2035 GFP_KERNEL);
2036 if (!newpsl) {
2037 err = -ENOBUFS;
2038 goto done;
2040 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2041 memcpy(newpsl->sl_addr, msf->imsf_slist,
2042 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2043 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2044 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2045 if (err) {
2046 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2047 goto done;
2049 } else {
2050 newpsl = NULL;
2051 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2052 msf->imsf_fmode, 0, NULL, 0);
2054 psl = pmc->sflist;
2055 if (psl) {
2056 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2057 psl->sl_count, psl->sl_addr, 0);
2058 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2059 } else
2060 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2061 0, NULL, 0);
2062 pmc->sflist = newpsl;
2063 pmc->sfmode = msf->imsf_fmode;
2064 err = 0;
2065 done:
2066 rtnl_unlock();
2067 if (leavegroup)
2068 err = ip_mc_leave_group(sk, &imr);
2069 return err;
2072 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2073 struct ip_msfilter __user *optval, int __user *optlen)
2075 int err, len, count, copycount;
2076 struct ip_mreqn imr;
2077 __be32 addr = msf->imsf_multiaddr;
2078 struct ip_mc_socklist *pmc;
2079 struct in_device *in_dev;
2080 struct inet_sock *inet = inet_sk(sk);
2081 struct ip_sf_socklist *psl;
2083 if (!MULTICAST(addr))
2084 return -EINVAL;
2086 rtnl_lock();
2088 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2089 imr.imr_address.s_addr = msf->imsf_interface;
2090 imr.imr_ifindex = 0;
2091 in_dev = ip_mc_find_dev(&imr);
2093 if (!in_dev) {
2094 err = -ENODEV;
2095 goto done;
2097 err = -EADDRNOTAVAIL;
2099 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2100 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2101 pmc->multi.imr_ifindex == imr.imr_ifindex)
2102 break;
2104 if (!pmc) /* must have a prior join */
2105 goto done;
2106 msf->imsf_fmode = pmc->sfmode;
2107 psl = pmc->sflist;
2108 rtnl_unlock();
2109 if (!psl) {
2110 len = 0;
2111 count = 0;
2112 } else {
2113 count = psl->sl_count;
2115 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2116 len = copycount * sizeof(psl->sl_addr[0]);
2117 msf->imsf_numsrc = count;
2118 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2119 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2120 return -EFAULT;
2122 if (len &&
2123 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2124 return -EFAULT;
2125 return 0;
2126 done:
2127 rtnl_unlock();
2128 return err;
2131 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2132 struct group_filter __user *optval, int __user *optlen)
2134 int err, i, count, copycount;
2135 struct sockaddr_in *psin;
2136 __be32 addr;
2137 struct ip_mc_socklist *pmc;
2138 struct inet_sock *inet = inet_sk(sk);
2139 struct ip_sf_socklist *psl;
2141 psin = (struct sockaddr_in *)&gsf->gf_group;
2142 if (psin->sin_family != AF_INET)
2143 return -EINVAL;
2144 addr = psin->sin_addr.s_addr;
2145 if (!MULTICAST(addr))
2146 return -EINVAL;
2148 rtnl_lock();
2150 err = -EADDRNOTAVAIL;
2152 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2153 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2154 pmc->multi.imr_ifindex == gsf->gf_interface)
2155 break;
2157 if (!pmc) /* must have a prior join */
2158 goto done;
2159 gsf->gf_fmode = pmc->sfmode;
2160 psl = pmc->sflist;
2161 rtnl_unlock();
2162 count = psl ? psl->sl_count : 0;
2163 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2164 gsf->gf_numsrc = count;
2165 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2166 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2167 return -EFAULT;
2169 for (i=0; i<copycount; i++) {
2170 struct sockaddr_in *psin;
2171 struct sockaddr_storage ss;
2173 psin = (struct sockaddr_in *)&ss;
2174 memset(&ss, 0, sizeof(ss));
2175 psin->sin_family = AF_INET;
2176 psin->sin_addr.s_addr = psl->sl_addr[i];
2177 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2178 return -EFAULT;
2180 return 0;
2181 done:
2182 rtnl_unlock();
2183 return err;
2187 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2189 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2191 struct inet_sock *inet = inet_sk(sk);
2192 struct ip_mc_socklist *pmc;
2193 struct ip_sf_socklist *psl;
2194 int i;
2196 if (!MULTICAST(loc_addr))
2197 return 1;
2199 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2200 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2201 pmc->multi.imr_ifindex == dif)
2202 break;
2204 if (!pmc)
2205 return 1;
2206 psl = pmc->sflist;
2207 if (!psl)
2208 return pmc->sfmode == MCAST_EXCLUDE;
2210 for (i=0; i<psl->sl_count; i++) {
2211 if (psl->sl_addr[i] == rmt_addr)
2212 break;
2214 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2215 return 0;
2216 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2217 return 0;
2218 return 1;
2222 * A socket is closing.
2225 void ip_mc_drop_socket(struct sock *sk)
2227 struct inet_sock *inet = inet_sk(sk);
2228 struct ip_mc_socklist *iml;
2230 if (inet->mc_list == NULL)
2231 return;
2233 rtnl_lock();
2234 while ((iml = inet->mc_list) != NULL) {
2235 struct in_device *in_dev;
2236 inet->mc_list = iml->next;
2238 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
2239 (void) ip_mc_leave_src(sk, iml, in_dev);
2240 if (in_dev != NULL) {
2241 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2242 in_dev_put(in_dev);
2244 sock_kfree_s(sk, iml, sizeof(*iml));
2246 rtnl_unlock();
2249 int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2251 struct ip_mc_list *im;
2252 struct ip_sf_list *psf;
2253 int rv = 0;
2255 read_lock(&in_dev->mc_list_lock);
2256 for (im=in_dev->mc_list; im; im=im->next) {
2257 if (im->multiaddr == mc_addr)
2258 break;
2260 if (im && proto == IPPROTO_IGMP) {
2261 rv = 1;
2262 } else if (im) {
2263 if (src_addr) {
2264 for (psf=im->sources; psf; psf=psf->sf_next) {
2265 if (psf->sf_inaddr == src_addr)
2266 break;
2268 if (psf)
2269 rv = psf->sf_count[MCAST_INCLUDE] ||
2270 psf->sf_count[MCAST_EXCLUDE] !=
2271 im->sfcount[MCAST_EXCLUDE];
2272 else
2273 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2274 } else
2275 rv = 1; /* unspecified source; tentatively allow */
2277 read_unlock(&in_dev->mc_list_lock);
2278 return rv;
2281 #if defined(CONFIG_PROC_FS)
2282 struct igmp_mc_iter_state {
2283 struct net_device *dev;
2284 struct in_device *in_dev;
2287 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2289 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2291 struct ip_mc_list *im = NULL;
2292 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2294 state->in_dev = NULL;
2295 for_each_netdev(state->dev) {
2296 struct in_device *in_dev;
2297 in_dev = in_dev_get(state->dev);
2298 if (!in_dev)
2299 continue;
2300 read_lock(&in_dev->mc_list_lock);
2301 im = in_dev->mc_list;
2302 if (im) {
2303 state->in_dev = in_dev;
2304 break;
2306 read_unlock(&in_dev->mc_list_lock);
2307 in_dev_put(in_dev);
2309 return im;
2312 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2314 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2315 im = im->next;
2316 while (!im) {
2317 if (likely(state->in_dev != NULL)) {
2318 read_unlock(&state->in_dev->mc_list_lock);
2319 in_dev_put(state->in_dev);
2321 state->dev = next_net_device(state->dev);
2322 if (!state->dev) {
2323 state->in_dev = NULL;
2324 break;
2326 state->in_dev = in_dev_get(state->dev);
2327 if (!state->in_dev)
2328 continue;
2329 read_lock(&state->in_dev->mc_list_lock);
2330 im = state->in_dev->mc_list;
2332 return im;
2335 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2337 struct ip_mc_list *im = igmp_mc_get_first(seq);
2338 if (im)
2339 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2340 --pos;
2341 return pos ? NULL : im;
2344 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2346 read_lock(&dev_base_lock);
2347 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2350 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2352 struct ip_mc_list *im;
2353 if (v == SEQ_START_TOKEN)
2354 im = igmp_mc_get_first(seq);
2355 else
2356 im = igmp_mc_get_next(seq, v);
2357 ++*pos;
2358 return im;
2361 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2363 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2364 if (likely(state->in_dev != NULL)) {
2365 read_unlock(&state->in_dev->mc_list_lock);
2366 in_dev_put(state->in_dev);
2367 state->in_dev = NULL;
2369 state->dev = NULL;
2370 read_unlock(&dev_base_lock);
2373 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2375 if (v == SEQ_START_TOKEN)
2376 seq_puts(seq,
2377 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2378 else {
2379 struct ip_mc_list *im = (struct ip_mc_list *)v;
2380 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2381 char *querier;
2382 #ifdef CONFIG_IP_MULTICAST
2383 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2384 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2385 "V3";
2386 #else
2387 querier = "NONE";
2388 #endif
2390 if (state->in_dev->mc_list == im) {
2391 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2392 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2395 seq_printf(seq,
2396 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2397 im->multiaddr, im->users,
2398 im->tm_running, im->tm_running ?
2399 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2400 im->reporter);
2402 return 0;
2405 static const struct seq_operations igmp_mc_seq_ops = {
2406 .start = igmp_mc_seq_start,
2407 .next = igmp_mc_seq_next,
2408 .stop = igmp_mc_seq_stop,
2409 .show = igmp_mc_seq_show,
2412 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2414 struct seq_file *seq;
2415 int rc = -ENOMEM;
2416 struct igmp_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2418 if (!s)
2419 goto out;
2420 rc = seq_open(file, &igmp_mc_seq_ops);
2421 if (rc)
2422 goto out_kfree;
2424 seq = file->private_data;
2425 seq->private = s;
2426 out:
2427 return rc;
2428 out_kfree:
2429 kfree(s);
2430 goto out;
2433 static const struct file_operations igmp_mc_seq_fops = {
2434 .owner = THIS_MODULE,
2435 .open = igmp_mc_seq_open,
2436 .read = seq_read,
2437 .llseek = seq_lseek,
2438 .release = seq_release_private,
2441 struct igmp_mcf_iter_state {
2442 struct net_device *dev;
2443 struct in_device *idev;
2444 struct ip_mc_list *im;
2447 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2449 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2451 struct ip_sf_list *psf = NULL;
2452 struct ip_mc_list *im = NULL;
2453 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2455 state->idev = NULL;
2456 state->im = NULL;
2457 for_each_netdev(state->dev) {
2458 struct in_device *idev;
2459 idev = in_dev_get(state->dev);
2460 if (unlikely(idev == NULL))
2461 continue;
2462 read_lock(&idev->mc_list_lock);
2463 im = idev->mc_list;
2464 if (likely(im != NULL)) {
2465 spin_lock_bh(&im->lock);
2466 psf = im->sources;
2467 if (likely(psf != NULL)) {
2468 state->im = im;
2469 state->idev = idev;
2470 break;
2472 spin_unlock_bh(&im->lock);
2474 read_unlock(&idev->mc_list_lock);
2475 in_dev_put(idev);
2477 return psf;
2480 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2482 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2484 psf = psf->sf_next;
2485 while (!psf) {
2486 spin_unlock_bh(&state->im->lock);
2487 state->im = state->im->next;
2488 while (!state->im) {
2489 if (likely(state->idev != NULL)) {
2490 read_unlock(&state->idev->mc_list_lock);
2491 in_dev_put(state->idev);
2493 state->dev = next_net_device(state->dev);
2494 if (!state->dev) {
2495 state->idev = NULL;
2496 goto out;
2498 state->idev = in_dev_get(state->dev);
2499 if (!state->idev)
2500 continue;
2501 read_lock(&state->idev->mc_list_lock);
2502 state->im = state->idev->mc_list;
2504 if (!state->im)
2505 break;
2506 spin_lock_bh(&state->im->lock);
2507 psf = state->im->sources;
2509 out:
2510 return psf;
2513 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2515 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2516 if (psf)
2517 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2518 --pos;
2519 return pos ? NULL : psf;
2522 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2524 read_lock(&dev_base_lock);
2525 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2528 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2530 struct ip_sf_list *psf;
2531 if (v == SEQ_START_TOKEN)
2532 psf = igmp_mcf_get_first(seq);
2533 else
2534 psf = igmp_mcf_get_next(seq, v);
2535 ++*pos;
2536 return psf;
2539 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2541 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2542 if (likely(state->im != NULL)) {
2543 spin_unlock_bh(&state->im->lock);
2544 state->im = NULL;
2546 if (likely(state->idev != NULL)) {
2547 read_unlock(&state->idev->mc_list_lock);
2548 in_dev_put(state->idev);
2549 state->idev = NULL;
2551 state->dev = NULL;
2552 read_unlock(&dev_base_lock);
2555 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2557 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2558 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2560 if (v == SEQ_START_TOKEN) {
2561 seq_printf(seq,
2562 "%3s %6s "
2563 "%10s %10s %6s %6s\n", "Idx",
2564 "Device", "MCA",
2565 "SRC", "INC", "EXC");
2566 } else {
2567 seq_printf(seq,
2568 "%3d %6.6s 0x%08x "
2569 "0x%08x %6lu %6lu\n",
2570 state->dev->ifindex, state->dev->name,
2571 ntohl(state->im->multiaddr),
2572 ntohl(psf->sf_inaddr),
2573 psf->sf_count[MCAST_INCLUDE],
2574 psf->sf_count[MCAST_EXCLUDE]);
2576 return 0;
2579 static const struct seq_operations igmp_mcf_seq_ops = {
2580 .start = igmp_mcf_seq_start,
2581 .next = igmp_mcf_seq_next,
2582 .stop = igmp_mcf_seq_stop,
2583 .show = igmp_mcf_seq_show,
2586 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2588 struct seq_file *seq;
2589 int rc = -ENOMEM;
2590 struct igmp_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2592 if (!s)
2593 goto out;
2594 rc = seq_open(file, &igmp_mcf_seq_ops);
2595 if (rc)
2596 goto out_kfree;
2598 seq = file->private_data;
2599 seq->private = s;
2600 out:
2601 return rc;
2602 out_kfree:
2603 kfree(s);
2604 goto out;
2607 static const struct file_operations igmp_mcf_seq_fops = {
2608 .owner = THIS_MODULE,
2609 .open = igmp_mcf_seq_open,
2610 .read = seq_read,
2611 .llseek = seq_lseek,
2612 .release = seq_release_private,
2615 int __init igmp_mc_proc_init(void)
2617 proc_net_fops_create(&init_net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2618 proc_net_fops_create(&init_net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2619 return 0;
2621 #endif
2623 EXPORT_SYMBOL(ip_mc_dec_group);
2624 EXPORT_SYMBOL(ip_mc_inc_group);
2625 EXPORT_SYMBOL(ip_mc_join_group);
2626 EXPORT_SYMBOL(ip_mc_rejoin_group);