mmc: avoid getting CID on SDIO-only cards
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / igmp.c
bloba1ad0e7180d2bd7051df4f78bed75fbb7612da40
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 * Authors:
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
19 * Fixes:
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
23 * functions.
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
32 * points.
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
53 * message (960131).
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
58 * true (960216).
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
70 * Vinay Kulkarni
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/types.h>
78 #include <linux/kernel.h>
79 #include <linux/jiffies.h>
80 #include <linux/string.h>
81 #include <linux/socket.h>
82 #include <linux/sockios.h>
83 #include <linux/in.h>
84 #include <linux/inet.h>
85 #include <linux/netdevice.h>
86 #include <linux/skbuff.h>
87 #include <linux/inetdevice.h>
88 #include <linux/igmp.h>
89 #include <linux/if_arp.h>
90 #include <linux/rtnetlink.h>
91 #include <linux/times.h>
93 #include <net/net_namespace.h>
94 #include <net/arp.h>
95 #include <net/ip.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
98 #include <net/sock.h>
99 #include <net/checksum.h>
100 #include <linux/netfilter_ipv4.h>
101 #ifdef CONFIG_IP_MROUTE
102 #include <linux/mroute.h>
103 #endif
104 #ifdef CONFIG_PROC_FS
105 #include <linux/proc_fs.h>
106 #include <linux/seq_file.h>
107 #endif
109 #define IP_MAX_MEMBERSHIPS 20
110 #define IP_MAX_MSF 10
112 #ifdef CONFIG_IP_MULTICAST
113 /* Parameter names and values are taken from igmp-v2-06 draft */
115 #define IGMP_V1_Router_Present_Timeout (400*HZ)
116 #define IGMP_V2_Router_Present_Timeout (400*HZ)
117 #define IGMP_Unsolicited_Report_Interval (10*HZ)
118 #define IGMP_Query_Response_Interval (10*HZ)
119 #define IGMP_Unsolicited_Report_Count 2
122 #define IGMP_Initial_Report_Delay (1)
124 /* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
131 #define IGMP_V1_SEEN(in_dev) \
132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
142 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
143 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
144 static void igmpv3_clear_delrec(struct in_device *in_dev);
145 static int sf_setstate(struct ip_mc_list *pmc);
146 static void sf_markstate(struct ip_mc_list *pmc);
147 #endif
148 static void ip_mc_clear_src(struct ip_mc_list *pmc);
149 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
152 static void ip_ma_put(struct ip_mc_list *im)
154 if (atomic_dec_and_test(&im->refcnt)) {
155 in_dev_put(im->interface);
156 kfree(im);
160 #ifdef CONFIG_IP_MULTICAST
163 * Timer management
166 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
168 spin_lock_bh(&im->lock);
169 if (del_timer(&im->timer))
170 atomic_dec(&im->refcnt);
171 im->tm_running = 0;
172 im->reporter = 0;
173 im->unsolicit_count = 0;
174 spin_unlock_bh(&im->lock);
177 /* It must be called with locked im->lock */
178 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
180 int tv = net_random() % max_delay;
182 im->tm_running = 1;
183 if (!mod_timer(&im->timer, jiffies+tv+2))
184 atomic_inc(&im->refcnt);
187 static void igmp_gq_start_timer(struct in_device *in_dev)
189 int tv = net_random() % in_dev->mr_maxdelay;
191 in_dev->mr_gq_running = 1;
192 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
193 in_dev_hold(in_dev);
196 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
198 int tv = net_random() % delay;
200 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
201 in_dev_hold(in_dev);
204 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
206 spin_lock_bh(&im->lock);
207 im->unsolicit_count = 0;
208 if (del_timer(&im->timer)) {
209 if ((long)(im->timer.expires-jiffies) < max_delay) {
210 add_timer(&im->timer);
211 im->tm_running = 1;
212 spin_unlock_bh(&im->lock);
213 return;
215 atomic_dec(&im->refcnt);
217 igmp_start_timer(im, max_delay);
218 spin_unlock_bh(&im->lock);
223 * Send an IGMP report.
226 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
229 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
230 int gdeleted, int sdeleted)
232 switch (type) {
233 case IGMPV3_MODE_IS_INCLUDE:
234 case IGMPV3_MODE_IS_EXCLUDE:
235 if (gdeleted || sdeleted)
236 return 0;
237 if (!(pmc->gsquery && !psf->sf_gsresp)) {
238 if (pmc->sfmode == MCAST_INCLUDE)
239 return 1;
240 /* don't include if this source is excluded
241 * in all filters
243 if (psf->sf_count[MCAST_INCLUDE])
244 return type == IGMPV3_MODE_IS_INCLUDE;
245 return pmc->sfcount[MCAST_EXCLUDE] ==
246 psf->sf_count[MCAST_EXCLUDE];
248 return 0;
249 case IGMPV3_CHANGE_TO_INCLUDE:
250 if (gdeleted || sdeleted)
251 return 0;
252 return psf->sf_count[MCAST_INCLUDE] != 0;
253 case IGMPV3_CHANGE_TO_EXCLUDE:
254 if (gdeleted || sdeleted)
255 return 0;
256 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
257 psf->sf_count[MCAST_INCLUDE])
258 return 0;
259 return pmc->sfcount[MCAST_EXCLUDE] ==
260 psf->sf_count[MCAST_EXCLUDE];
261 case IGMPV3_ALLOW_NEW_SOURCES:
262 if (gdeleted || !psf->sf_crcount)
263 return 0;
264 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
265 case IGMPV3_BLOCK_OLD_SOURCES:
266 if (pmc->sfmode == MCAST_INCLUDE)
267 return gdeleted || (psf->sf_crcount && sdeleted);
268 return psf->sf_crcount && !gdeleted && !sdeleted;
270 return 0;
273 static int
274 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
276 struct ip_sf_list *psf;
277 int scount = 0;
279 for (psf=pmc->sources; psf; psf=psf->sf_next) {
280 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
281 continue;
282 scount++;
284 return scount;
287 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
289 struct sk_buff *skb;
290 struct rtable *rt;
291 struct iphdr *pip;
292 struct igmpv3_report *pig;
293 struct net *net = dev_net(dev);
295 skb = alloc_skb(size + LL_ALLOCATED_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(net, &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_set(skb, &rt->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->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 igmphdr *pig = igmp_hdr(skb);
353 const int igmplen = skb->tail - skb->transport_header;
355 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
357 return ip_local_out(skb);
360 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
362 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
365 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
366 int type, struct igmpv3_grec **ppgr)
368 struct net_device *dev = pmc->interface->dev;
369 struct igmpv3_report *pih;
370 struct igmpv3_grec *pgr;
372 if (!skb)
373 skb = igmpv3_newpack(dev, dev->mtu);
374 if (!skb)
375 return NULL;
376 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
377 pgr->grec_type = type;
378 pgr->grec_auxwords = 0;
379 pgr->grec_nsrcs = 0;
380 pgr->grec_mca = pmc->multiaddr;
381 pih = igmpv3_report_hdr(skb);
382 pih->ngrec = htons(ntohs(pih->ngrec)+1);
383 *ppgr = pgr;
384 return skb;
387 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
388 skb_tailroom(skb)) : 0)
390 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
391 int type, int gdeleted, int sdeleted)
393 struct net_device *dev = pmc->interface->dev;
394 struct igmpv3_report *pih;
395 struct igmpv3_grec *pgr = NULL;
396 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
397 int scount, stotal, first, isquery, truncate;
399 if (pmc->multiaddr == IGMP_ALL_HOSTS)
400 return skb;
402 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
403 type == IGMPV3_MODE_IS_EXCLUDE;
404 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
405 type == IGMPV3_CHANGE_TO_EXCLUDE;
407 stotal = scount = 0;
409 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
411 if (!*psf_list)
412 goto empty_source;
414 pih = skb ? igmpv3_report_hdr(skb) : NULL;
416 /* EX and TO_EX get a fresh packet, if needed */
417 if (truncate) {
418 if (pih && pih->ngrec &&
419 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
420 if (skb)
421 igmpv3_sendpack(skb);
422 skb = igmpv3_newpack(dev, dev->mtu);
425 first = 1;
426 psf_prev = NULL;
427 for (psf=*psf_list; psf; psf=psf_next) {
428 __be32 *psrc;
430 psf_next = psf->sf_next;
432 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
433 psf_prev = psf;
434 continue;
437 /* clear marks on query responses */
438 if (isquery)
439 psf->sf_gsresp = 0;
441 if (AVAILABLE(skb) < sizeof(__be32) +
442 first*sizeof(struct igmpv3_grec)) {
443 if (truncate && !first)
444 break; /* truncate these */
445 if (pgr)
446 pgr->grec_nsrcs = htons(scount);
447 if (skb)
448 igmpv3_sendpack(skb);
449 skb = igmpv3_newpack(dev, dev->mtu);
450 first = 1;
451 scount = 0;
453 if (first) {
454 skb = add_grhead(skb, pmc, type, &pgr);
455 first = 0;
457 if (!skb)
458 return NULL;
459 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
460 *psrc = psf->sf_inaddr;
461 scount++; stotal++;
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;
474 psf_prev = psf;
477 empty_source:
478 if (!stotal) {
479 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
480 type == IGMPV3_BLOCK_OLD_SOURCES)
481 return skb;
482 if (pmc->crcount || isquery) {
483 /* make sure we have room for group header */
484 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
485 igmpv3_sendpack(skb);
486 skb = NULL; /* add_grhead will get a new one */
488 skb = add_grhead(skb, pmc, type, &pgr);
491 if (pgr)
492 pgr->grec_nsrcs = htons(scount);
494 if (isquery)
495 pmc->gsquery = 0; /* clear query state on report */
496 return skb;
499 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
501 struct sk_buff *skb = NULL;
502 int type;
504 if (!pmc) {
505 read_lock(&in_dev->mc_list_lock);
506 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
507 if (pmc->multiaddr == IGMP_ALL_HOSTS)
508 continue;
509 spin_lock_bh(&pmc->lock);
510 if (pmc->sfcount[MCAST_EXCLUDE])
511 type = IGMPV3_MODE_IS_EXCLUDE;
512 else
513 type = IGMPV3_MODE_IS_INCLUDE;
514 skb = add_grec(skb, pmc, type, 0, 0);
515 spin_unlock_bh(&pmc->lock);
517 read_unlock(&in_dev->mc_list_lock);
518 } else {
519 spin_lock_bh(&pmc->lock);
520 if (pmc->sfcount[MCAST_EXCLUDE])
521 type = IGMPV3_MODE_IS_EXCLUDE;
522 else
523 type = IGMPV3_MODE_IS_INCLUDE;
524 skb = add_grec(skb, pmc, type, 0, 0);
525 spin_unlock_bh(&pmc->lock);
527 if (!skb)
528 return 0;
529 return igmpv3_sendpack(skb);
533 * remove zero-count source records from a source filter list
535 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
537 struct ip_sf_list *psf_prev, *psf_next, *psf;
539 psf_prev = NULL;
540 for (psf=*ppsf; psf; psf = psf_next) {
541 psf_next = psf->sf_next;
542 if (psf->sf_crcount == 0) {
543 if (psf_prev)
544 psf_prev->sf_next = psf->sf_next;
545 else
546 *ppsf = psf->sf_next;
547 kfree(psf);
548 } else
549 psf_prev = psf;
553 static void igmpv3_send_cr(struct in_device *in_dev)
555 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
556 struct sk_buff *skb = NULL;
557 int type, dtype;
559 read_lock(&in_dev->mc_list_lock);
560 spin_lock_bh(&in_dev->mc_tomb_lock);
562 /* deleted MCA's */
563 pmc_prev = NULL;
564 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
565 pmc_next = pmc->next;
566 if (pmc->sfmode == MCAST_INCLUDE) {
567 type = IGMPV3_BLOCK_OLD_SOURCES;
568 dtype = IGMPV3_BLOCK_OLD_SOURCES;
569 skb = add_grec(skb, pmc, type, 1, 0);
570 skb = add_grec(skb, pmc, dtype, 1, 1);
572 if (pmc->crcount) {
573 if (pmc->sfmode == MCAST_EXCLUDE) {
574 type = IGMPV3_CHANGE_TO_INCLUDE;
575 skb = add_grec(skb, pmc, type, 1, 0);
577 pmc->crcount--;
578 if (pmc->crcount == 0) {
579 igmpv3_clear_zeros(&pmc->tomb);
580 igmpv3_clear_zeros(&pmc->sources);
583 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
584 if (pmc_prev)
585 pmc_prev->next = pmc_next;
586 else
587 in_dev->mc_tomb = pmc_next;
588 in_dev_put(pmc->interface);
589 kfree(pmc);
590 } else
591 pmc_prev = pmc;
593 spin_unlock_bh(&in_dev->mc_tomb_lock);
595 /* change recs */
596 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
597 spin_lock_bh(&pmc->lock);
598 if (pmc->sfcount[MCAST_EXCLUDE]) {
599 type = IGMPV3_BLOCK_OLD_SOURCES;
600 dtype = IGMPV3_ALLOW_NEW_SOURCES;
601 } else {
602 type = IGMPV3_ALLOW_NEW_SOURCES;
603 dtype = IGMPV3_BLOCK_OLD_SOURCES;
605 skb = add_grec(skb, pmc, type, 0, 0);
606 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
608 /* filter mode changes */
609 if (pmc->crcount) {
610 if (pmc->sfmode == MCAST_EXCLUDE)
611 type = IGMPV3_CHANGE_TO_EXCLUDE;
612 else
613 type = IGMPV3_CHANGE_TO_INCLUDE;
614 skb = add_grec(skb, pmc, type, 0, 0);
615 pmc->crcount--;
617 spin_unlock_bh(&pmc->lock);
619 read_unlock(&in_dev->mc_list_lock);
621 if (!skb)
622 return;
623 (void) igmpv3_sendpack(skb);
626 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
627 int type)
629 struct sk_buff *skb;
630 struct iphdr *iph;
631 struct igmphdr *ih;
632 struct rtable *rt;
633 struct net_device *dev = in_dev->dev;
634 struct net *net = dev_net(dev);
635 __be32 group = pmc ? pmc->multiaddr : 0;
636 __be32 dst;
638 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
639 return igmpv3_send_report(in_dev, pmc);
640 else if (type == IGMP_HOST_LEAVE_MESSAGE)
641 dst = IGMP_ALL_ROUTER;
642 else
643 dst = group;
646 struct flowi fl = { .oif = dev->ifindex,
647 .nl_u = { .ip4_u = { .daddr = dst } },
648 .proto = IPPROTO_IGMP };
649 if (ip_route_output_key(net, &rt, &fl))
650 return -1;
652 if (rt->rt_src == 0) {
653 ip_rt_put(rt);
654 return -1;
657 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
658 if (skb == NULL) {
659 ip_rt_put(rt);
660 return -1;
663 skb_dst_set(skb, &rt->dst);
665 skb_reserve(skb, LL_RESERVED_SPACE(dev));
667 skb_reset_network_header(skb);
668 iph = ip_hdr(skb);
669 skb_put(skb, sizeof(struct iphdr) + 4);
671 iph->version = 4;
672 iph->ihl = (sizeof(struct iphdr)+4)>>2;
673 iph->tos = 0xc0;
674 iph->frag_off = htons(IP_DF);
675 iph->ttl = 1;
676 iph->daddr = dst;
677 iph->saddr = rt->rt_src;
678 iph->protocol = IPPROTO_IGMP;
679 ip_select_ident(iph, &rt->dst, NULL);
680 ((u8*)&iph[1])[0] = IPOPT_RA;
681 ((u8*)&iph[1])[1] = 4;
682 ((u8*)&iph[1])[2] = 0;
683 ((u8*)&iph[1])[3] = 0;
685 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
686 ih->type = type;
687 ih->code = 0;
688 ih->csum = 0;
689 ih->group = group;
690 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
692 return ip_local_out(skb);
695 static void igmp_gq_timer_expire(unsigned long data)
697 struct in_device *in_dev = (struct in_device *)data;
699 in_dev->mr_gq_running = 0;
700 igmpv3_send_report(in_dev, NULL);
701 __in_dev_put(in_dev);
704 static void igmp_ifc_timer_expire(unsigned long data)
706 struct in_device *in_dev = (struct in_device *)data;
708 igmpv3_send_cr(in_dev);
709 if (in_dev->mr_ifc_count) {
710 in_dev->mr_ifc_count--;
711 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
713 __in_dev_put(in_dev);
716 static void igmp_ifc_event(struct in_device *in_dev)
718 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
719 return;
720 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
721 IGMP_Unsolicited_Report_Count;
722 igmp_ifc_start_timer(in_dev, 1);
726 static void igmp_timer_expire(unsigned long data)
728 struct ip_mc_list *im=(struct ip_mc_list *)data;
729 struct in_device *in_dev = im->interface;
731 spin_lock(&im->lock);
732 im->tm_running = 0;
734 if (im->unsolicit_count) {
735 im->unsolicit_count--;
736 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
738 im->reporter = 1;
739 spin_unlock(&im->lock);
741 if (IGMP_V1_SEEN(in_dev))
742 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
743 else if (IGMP_V2_SEEN(in_dev))
744 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
745 else
746 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
748 ip_ma_put(im);
751 /* mark EXCLUDE-mode sources */
752 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
754 struct ip_sf_list *psf;
755 int i, scount;
757 scount = 0;
758 for (psf=pmc->sources; psf; psf=psf->sf_next) {
759 if (scount == nsrcs)
760 break;
761 for (i=0; i<nsrcs; i++) {
762 /* skip inactive filters */
763 if (pmc->sfcount[MCAST_INCLUDE] ||
764 pmc->sfcount[MCAST_EXCLUDE] !=
765 psf->sf_count[MCAST_EXCLUDE])
766 continue;
767 if (srcs[i] == psf->sf_inaddr) {
768 scount++;
769 break;
773 pmc->gsquery = 0;
774 if (scount == nsrcs) /* all sources excluded */
775 return 0;
776 return 1;
779 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
781 struct ip_sf_list *psf;
782 int i, scount;
784 if (pmc->sfmode == MCAST_EXCLUDE)
785 return igmp_xmarksources(pmc, nsrcs, srcs);
787 /* mark INCLUDE-mode sources */
788 scount = 0;
789 for (psf=pmc->sources; psf; psf=psf->sf_next) {
790 if (scount == nsrcs)
791 break;
792 for (i=0; i<nsrcs; i++)
793 if (srcs[i] == psf->sf_inaddr) {
794 psf->sf_gsresp = 1;
795 scount++;
796 break;
799 if (!scount) {
800 pmc->gsquery = 0;
801 return 0;
803 pmc->gsquery = 1;
804 return 1;
807 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
809 struct ip_mc_list *im;
811 /* Timers are only set for non-local groups */
813 if (group == IGMP_ALL_HOSTS)
814 return;
816 read_lock(&in_dev->mc_list_lock);
817 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
818 if (im->multiaddr == group) {
819 igmp_stop_timer(im);
820 break;
823 read_unlock(&in_dev->mc_list_lock);
826 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
827 int len)
829 struct igmphdr *ih = igmp_hdr(skb);
830 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
831 struct ip_mc_list *im;
832 __be32 group = ih->group;
833 int max_delay;
834 int mark = 0;
837 if (len == 8) {
838 if (ih->code == 0) {
839 /* Alas, old v1 router presents here. */
841 max_delay = IGMP_Query_Response_Interval;
842 in_dev->mr_v1_seen = jiffies +
843 IGMP_V1_Router_Present_Timeout;
844 group = 0;
845 } else {
846 /* v2 router present */
847 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
848 in_dev->mr_v2_seen = jiffies +
849 IGMP_V2_Router_Present_Timeout;
851 /* cancel the interface change timer */
852 in_dev->mr_ifc_count = 0;
853 if (del_timer(&in_dev->mr_ifc_timer))
854 __in_dev_put(in_dev);
855 /* clear deleted report items */
856 igmpv3_clear_delrec(in_dev);
857 } else if (len < 12) {
858 return; /* ignore bogus packet; freed by caller */
859 } else { /* v3 */
860 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
861 return;
863 ih3 = igmpv3_query_hdr(skb);
864 if (ih3->nsrcs) {
865 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
866 + ntohs(ih3->nsrcs)*sizeof(__be32)))
867 return;
868 ih3 = igmpv3_query_hdr(skb);
871 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
872 if (!max_delay)
873 max_delay = 1; /* can't mod w/ 0 */
874 in_dev->mr_maxdelay = max_delay;
875 if (ih3->qrv)
876 in_dev->mr_qrv = ih3->qrv;
877 if (!group) { /* general query */
878 if (ih3->nsrcs)
879 return; /* no sources allowed */
880 igmp_gq_start_timer(in_dev);
881 return;
883 /* mark sources to include, if group & source-specific */
884 mark = ih3->nsrcs != 0;
888 * - Start the timers in all of our membership records
889 * that the query applies to for the interface on
890 * which the query arrived excl. those that belong
891 * to a "local" group (224.0.0.X)
892 * - For timers already running check if they need to
893 * be reset.
894 * - Use the igmp->igmp_code field as the maximum
895 * delay possible
897 read_lock(&in_dev->mc_list_lock);
898 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
899 int changed;
901 if (group && group != im->multiaddr)
902 continue;
903 if (im->multiaddr == IGMP_ALL_HOSTS)
904 continue;
905 spin_lock_bh(&im->lock);
906 if (im->tm_running)
907 im->gsquery = im->gsquery && mark;
908 else
909 im->gsquery = mark;
910 changed = !im->gsquery ||
911 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
912 spin_unlock_bh(&im->lock);
913 if (changed)
914 igmp_mod_timer(im, max_delay);
916 read_unlock(&in_dev->mc_list_lock);
919 /* called in rcu_read_lock() section */
920 int igmp_rcv(struct sk_buff *skb)
922 /* This basically follows the spec line by line -- see RFC1112 */
923 struct igmphdr *ih;
924 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
925 int len = skb->len;
927 if (in_dev == NULL)
928 goto drop;
930 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
931 goto drop;
933 switch (skb->ip_summed) {
934 case CHECKSUM_COMPLETE:
935 if (!csum_fold(skb->csum))
936 break;
937 /* fall through */
938 case CHECKSUM_NONE:
939 skb->csum = 0;
940 if (__skb_checksum_complete(skb))
941 goto drop;
944 ih = igmp_hdr(skb);
945 switch (ih->type) {
946 case IGMP_HOST_MEMBERSHIP_QUERY:
947 igmp_heard_query(in_dev, skb, len);
948 break;
949 case IGMP_HOST_MEMBERSHIP_REPORT:
950 case IGMPV2_HOST_MEMBERSHIP_REPORT:
951 /* Is it our report looped back? */
952 if (skb_rtable(skb)->fl.iif == 0)
953 break;
954 /* don't rely on MC router hearing unicast reports */
955 if (skb->pkt_type == PACKET_MULTICAST ||
956 skb->pkt_type == PACKET_BROADCAST)
957 igmp_heard_report(in_dev, ih->group);
958 break;
959 case IGMP_PIM:
960 #ifdef CONFIG_IP_PIMSM_V1
961 return pim_rcv_v1(skb);
962 #endif
963 case IGMPV3_HOST_MEMBERSHIP_REPORT:
964 case IGMP_DVMRP:
965 case IGMP_TRACE:
966 case IGMP_HOST_LEAVE_MESSAGE:
967 case IGMP_MTRACE:
968 case IGMP_MTRACE_RESP:
969 break;
970 default:
971 break;
974 drop:
975 kfree_skb(skb);
976 return 0;
979 #endif
983 * Add a filter to a device
986 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
988 char buf[MAX_ADDR_LEN];
989 struct net_device *dev = in_dev->dev;
991 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
992 We will get multicast token leakage, when IFF_MULTICAST
993 is changed. This check should be done in dev->set_multicast_list
994 routine. Something sort of:
995 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
996 --ANK
998 if (arp_mc_map(addr, buf, dev, 0) == 0)
999 dev_mc_add(dev, buf);
1003 * Remove a filter from a device
1006 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1008 char buf[MAX_ADDR_LEN];
1009 struct net_device *dev = in_dev->dev;
1011 if (arp_mc_map(addr, buf, dev, 0) == 0)
1012 dev_mc_del(dev, buf);
1015 #ifdef CONFIG_IP_MULTICAST
1017 * deleted ip_mc_list manipulation
1019 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1021 struct ip_mc_list *pmc;
1023 /* this is an "ip_mc_list" for convenience; only the fields below
1024 * are actually used. In particular, the refcnt and users are not
1025 * used for management of the delete list. Using the same structure
1026 * for deleted items allows change reports to use common code with
1027 * non-deleted or query-response MCA's.
1029 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1030 if (!pmc)
1031 return;
1032 spin_lock_bh(&im->lock);
1033 pmc->interface = im->interface;
1034 in_dev_hold(in_dev);
1035 pmc->multiaddr = im->multiaddr;
1036 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1037 IGMP_Unsolicited_Report_Count;
1038 pmc->sfmode = im->sfmode;
1039 if (pmc->sfmode == MCAST_INCLUDE) {
1040 struct ip_sf_list *psf;
1042 pmc->tomb = im->tomb;
1043 pmc->sources = im->sources;
1044 im->tomb = im->sources = NULL;
1045 for (psf=pmc->sources; psf; psf=psf->sf_next)
1046 psf->sf_crcount = pmc->crcount;
1048 spin_unlock_bh(&im->lock);
1050 spin_lock_bh(&in_dev->mc_tomb_lock);
1051 pmc->next = in_dev->mc_tomb;
1052 in_dev->mc_tomb = pmc;
1053 spin_unlock_bh(&in_dev->mc_tomb_lock);
1056 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1058 struct ip_mc_list *pmc, *pmc_prev;
1059 struct ip_sf_list *psf, *psf_next;
1061 spin_lock_bh(&in_dev->mc_tomb_lock);
1062 pmc_prev = NULL;
1063 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1064 if (pmc->multiaddr == multiaddr)
1065 break;
1066 pmc_prev = pmc;
1068 if (pmc) {
1069 if (pmc_prev)
1070 pmc_prev->next = pmc->next;
1071 else
1072 in_dev->mc_tomb = pmc->next;
1074 spin_unlock_bh(&in_dev->mc_tomb_lock);
1075 if (pmc) {
1076 for (psf=pmc->tomb; psf; psf=psf_next) {
1077 psf_next = psf->sf_next;
1078 kfree(psf);
1080 in_dev_put(pmc->interface);
1081 kfree(pmc);
1085 static void igmpv3_clear_delrec(struct in_device *in_dev)
1087 struct ip_mc_list *pmc, *nextpmc;
1089 spin_lock_bh(&in_dev->mc_tomb_lock);
1090 pmc = in_dev->mc_tomb;
1091 in_dev->mc_tomb = NULL;
1092 spin_unlock_bh(&in_dev->mc_tomb_lock);
1094 for (; pmc; pmc = nextpmc) {
1095 nextpmc = pmc->next;
1096 ip_mc_clear_src(pmc);
1097 in_dev_put(pmc->interface);
1098 kfree(pmc);
1100 /* clear dead sources, too */
1101 read_lock(&in_dev->mc_list_lock);
1102 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1103 struct ip_sf_list *psf, *psf_next;
1105 spin_lock_bh(&pmc->lock);
1106 psf = pmc->tomb;
1107 pmc->tomb = NULL;
1108 spin_unlock_bh(&pmc->lock);
1109 for (; psf; psf=psf_next) {
1110 psf_next = psf->sf_next;
1111 kfree(psf);
1114 read_unlock(&in_dev->mc_list_lock);
1116 #endif
1118 static void igmp_group_dropped(struct ip_mc_list *im)
1120 struct in_device *in_dev = im->interface;
1121 #ifdef CONFIG_IP_MULTICAST
1122 int reporter;
1123 #endif
1125 if (im->loaded) {
1126 im->loaded = 0;
1127 ip_mc_filter_del(in_dev, im->multiaddr);
1130 #ifdef CONFIG_IP_MULTICAST
1131 if (im->multiaddr == IGMP_ALL_HOSTS)
1132 return;
1134 reporter = im->reporter;
1135 igmp_stop_timer(im);
1137 if (!in_dev->dead) {
1138 if (IGMP_V1_SEEN(in_dev))
1139 goto done;
1140 if (IGMP_V2_SEEN(in_dev)) {
1141 if (reporter)
1142 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1143 goto done;
1145 /* IGMPv3 */
1146 igmpv3_add_delrec(in_dev, im);
1148 igmp_ifc_event(in_dev);
1150 done:
1151 #endif
1152 ip_mc_clear_src(im);
1155 static void igmp_group_added(struct ip_mc_list *im)
1157 struct in_device *in_dev = im->interface;
1159 if (im->loaded == 0) {
1160 im->loaded = 1;
1161 ip_mc_filter_add(in_dev, im->multiaddr);
1164 #ifdef CONFIG_IP_MULTICAST
1165 if (im->multiaddr == IGMP_ALL_HOSTS)
1166 return;
1168 if (in_dev->dead)
1169 return;
1170 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1171 spin_lock_bh(&im->lock);
1172 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1173 spin_unlock_bh(&im->lock);
1174 return;
1176 /* else, v3 */
1178 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1179 IGMP_Unsolicited_Report_Count;
1180 igmp_ifc_event(in_dev);
1181 #endif
1186 * Multicast list managers
1191 * A socket has joined a multicast group on device dev.
1194 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1196 struct ip_mc_list *im;
1198 ASSERT_RTNL();
1200 for (im=in_dev->mc_list; im; im=im->next) {
1201 if (im->multiaddr == addr) {
1202 im->users++;
1203 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1204 goto out;
1208 im = kmalloc(sizeof(*im), GFP_KERNEL);
1209 if (!im)
1210 goto out;
1212 im->users = 1;
1213 im->interface = in_dev;
1214 in_dev_hold(in_dev);
1215 im->multiaddr = addr;
1216 /* initial mode is (EX, empty) */
1217 im->sfmode = MCAST_EXCLUDE;
1218 im->sfcount[MCAST_INCLUDE] = 0;
1219 im->sfcount[MCAST_EXCLUDE] = 1;
1220 im->sources = NULL;
1221 im->tomb = NULL;
1222 im->crcount = 0;
1223 atomic_set(&im->refcnt, 1);
1224 spin_lock_init(&im->lock);
1225 #ifdef CONFIG_IP_MULTICAST
1226 im->tm_running = 0;
1227 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1228 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1229 im->reporter = 0;
1230 im->gsquery = 0;
1231 #endif
1232 im->loaded = 0;
1233 write_lock_bh(&in_dev->mc_list_lock);
1234 im->next = in_dev->mc_list;
1235 in_dev->mc_list = im;
1236 in_dev->mc_count++;
1237 write_unlock_bh(&in_dev->mc_list_lock);
1238 #ifdef CONFIG_IP_MULTICAST
1239 igmpv3_del_delrec(in_dev, im->multiaddr);
1240 #endif
1241 igmp_group_added(im);
1242 if (!in_dev->dead)
1243 ip_rt_multicast_event(in_dev);
1244 out:
1245 return;
1247 EXPORT_SYMBOL(ip_mc_inc_group);
1250 * Resend IGMP JOIN report; used for bonding.
1252 void ip_mc_rejoin_group(struct ip_mc_list *im)
1254 #ifdef CONFIG_IP_MULTICAST
1255 struct in_device *in_dev = im->interface;
1257 if (im->multiaddr == IGMP_ALL_HOSTS)
1258 return;
1260 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1261 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1262 return;
1264 /* else, v3 */
1265 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1266 IGMP_Unsolicited_Report_Count;
1267 igmp_ifc_event(in_dev);
1268 #endif
1270 EXPORT_SYMBOL(ip_mc_rejoin_group);
1273 * A socket has left a multicast group on device dev
1276 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1278 struct ip_mc_list *i, **ip;
1280 ASSERT_RTNL();
1282 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1283 if (i->multiaddr == addr) {
1284 if (--i->users == 0) {
1285 write_lock_bh(&in_dev->mc_list_lock);
1286 *ip = i->next;
1287 in_dev->mc_count--;
1288 write_unlock_bh(&in_dev->mc_list_lock);
1289 igmp_group_dropped(i);
1291 if (!in_dev->dead)
1292 ip_rt_multicast_event(in_dev);
1294 ip_ma_put(i);
1295 return;
1297 break;
1301 EXPORT_SYMBOL(ip_mc_dec_group);
1303 /* Device changing type */
1305 void ip_mc_unmap(struct in_device *in_dev)
1307 struct ip_mc_list *i;
1309 ASSERT_RTNL();
1311 for (i = in_dev->mc_list; i; i = i->next)
1312 igmp_group_dropped(i);
1315 void ip_mc_remap(struct in_device *in_dev)
1317 struct ip_mc_list *i;
1319 ASSERT_RTNL();
1321 for (i = in_dev->mc_list; i; i = i->next)
1322 igmp_group_added(i);
1325 /* Device going down */
1327 void ip_mc_down(struct in_device *in_dev)
1329 struct ip_mc_list *i;
1331 ASSERT_RTNL();
1333 for (i=in_dev->mc_list; i; i=i->next)
1334 igmp_group_dropped(i);
1336 #ifdef CONFIG_IP_MULTICAST
1337 in_dev->mr_ifc_count = 0;
1338 if (del_timer(&in_dev->mr_ifc_timer))
1339 __in_dev_put(in_dev);
1340 in_dev->mr_gq_running = 0;
1341 if (del_timer(&in_dev->mr_gq_timer))
1342 __in_dev_put(in_dev);
1343 igmpv3_clear_delrec(in_dev);
1344 #endif
1346 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1349 void ip_mc_init_dev(struct in_device *in_dev)
1351 ASSERT_RTNL();
1353 in_dev->mc_tomb = NULL;
1354 #ifdef CONFIG_IP_MULTICAST
1355 in_dev->mr_gq_running = 0;
1356 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1357 (unsigned long)in_dev);
1358 in_dev->mr_ifc_count = 0;
1359 in_dev->mc_count = 0;
1360 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1361 (unsigned long)in_dev);
1362 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1363 #endif
1365 rwlock_init(&in_dev->mc_list_lock);
1366 spin_lock_init(&in_dev->mc_tomb_lock);
1369 /* Device going up */
1371 void ip_mc_up(struct in_device *in_dev)
1373 struct ip_mc_list *i;
1375 ASSERT_RTNL();
1377 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1379 for (i=in_dev->mc_list; i; i=i->next)
1380 igmp_group_added(i);
1384 * Device is about to be destroyed: clean up.
1387 void ip_mc_destroy_dev(struct in_device *in_dev)
1389 struct ip_mc_list *i;
1391 ASSERT_RTNL();
1393 /* Deactivate timers */
1394 ip_mc_down(in_dev);
1396 write_lock_bh(&in_dev->mc_list_lock);
1397 while ((i = in_dev->mc_list) != NULL) {
1398 in_dev->mc_list = i->next;
1399 in_dev->mc_count--;
1400 write_unlock_bh(&in_dev->mc_list_lock);
1401 igmp_group_dropped(i);
1402 ip_ma_put(i);
1404 write_lock_bh(&in_dev->mc_list_lock);
1406 write_unlock_bh(&in_dev->mc_list_lock);
1409 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1411 struct flowi fl = { .nl_u = { .ip4_u =
1412 { .daddr = imr->imr_multiaddr.s_addr } } };
1413 struct rtable *rt;
1414 struct net_device *dev = NULL;
1415 struct in_device *idev = NULL;
1417 if (imr->imr_ifindex) {
1418 idev = inetdev_by_index(net, imr->imr_ifindex);
1419 if (idev)
1420 __in_dev_put(idev);
1421 return idev;
1423 if (imr->imr_address.s_addr) {
1424 dev = ip_dev_find(net, imr->imr_address.s_addr);
1425 if (!dev)
1426 return NULL;
1427 dev_put(dev);
1430 if (!dev && !ip_route_output_key(net, &rt, &fl)) {
1431 dev = rt->dst.dev;
1432 ip_rt_put(rt);
1434 if (dev) {
1435 imr->imr_ifindex = dev->ifindex;
1436 idev = __in_dev_get_rtnl(dev);
1438 return idev;
1442 * Join a socket to a group
1444 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1445 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1448 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1449 __be32 *psfsrc)
1451 struct ip_sf_list *psf, *psf_prev;
1452 int rv = 0;
1454 psf_prev = NULL;
1455 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1456 if (psf->sf_inaddr == *psfsrc)
1457 break;
1458 psf_prev = psf;
1460 if (!psf || psf->sf_count[sfmode] == 0) {
1461 /* source filter not found, or count wrong => bug */
1462 return -ESRCH;
1464 psf->sf_count[sfmode]--;
1465 if (psf->sf_count[sfmode] == 0) {
1466 ip_rt_multicast_event(pmc->interface);
1468 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1469 #ifdef CONFIG_IP_MULTICAST
1470 struct in_device *in_dev = pmc->interface;
1471 #endif
1473 /* no more filters for this source */
1474 if (psf_prev)
1475 psf_prev->sf_next = psf->sf_next;
1476 else
1477 pmc->sources = psf->sf_next;
1478 #ifdef CONFIG_IP_MULTICAST
1479 if (psf->sf_oldin &&
1480 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1481 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1482 IGMP_Unsolicited_Report_Count;
1483 psf->sf_next = pmc->tomb;
1484 pmc->tomb = psf;
1485 rv = 1;
1486 } else
1487 #endif
1488 kfree(psf);
1490 return rv;
1493 #ifndef CONFIG_IP_MULTICAST
1494 #define igmp_ifc_event(x) do { } while (0)
1495 #endif
1497 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1498 int sfcount, __be32 *psfsrc, int delta)
1500 struct ip_mc_list *pmc;
1501 int changerec = 0;
1502 int i, err;
1504 if (!in_dev)
1505 return -ENODEV;
1506 read_lock(&in_dev->mc_list_lock);
1507 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1508 if (*pmca == pmc->multiaddr)
1509 break;
1511 if (!pmc) {
1512 /* MCA not found?? bug */
1513 read_unlock(&in_dev->mc_list_lock);
1514 return -ESRCH;
1516 spin_lock_bh(&pmc->lock);
1517 read_unlock(&in_dev->mc_list_lock);
1518 #ifdef CONFIG_IP_MULTICAST
1519 sf_markstate(pmc);
1520 #endif
1521 if (!delta) {
1522 err = -EINVAL;
1523 if (!pmc->sfcount[sfmode])
1524 goto out_unlock;
1525 pmc->sfcount[sfmode]--;
1527 err = 0;
1528 for (i=0; i<sfcount; i++) {
1529 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1531 changerec |= rv > 0;
1532 if (!err && rv < 0)
1533 err = rv;
1535 if (pmc->sfmode == MCAST_EXCLUDE &&
1536 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1537 pmc->sfcount[MCAST_INCLUDE]) {
1538 #ifdef CONFIG_IP_MULTICAST
1539 struct ip_sf_list *psf;
1540 #endif
1542 /* filter mode change */
1543 pmc->sfmode = MCAST_INCLUDE;
1544 #ifdef CONFIG_IP_MULTICAST
1545 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1546 IGMP_Unsolicited_Report_Count;
1547 in_dev->mr_ifc_count = pmc->crcount;
1548 for (psf=pmc->sources; psf; psf = psf->sf_next)
1549 psf->sf_crcount = 0;
1550 igmp_ifc_event(pmc->interface);
1551 } else if (sf_setstate(pmc) || changerec) {
1552 igmp_ifc_event(pmc->interface);
1553 #endif
1555 out_unlock:
1556 spin_unlock_bh(&pmc->lock);
1557 return err;
1561 * Add multicast single-source filter to the interface list
1563 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1564 __be32 *psfsrc, int delta)
1566 struct ip_sf_list *psf, *psf_prev;
1568 psf_prev = NULL;
1569 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1570 if (psf->sf_inaddr == *psfsrc)
1571 break;
1572 psf_prev = psf;
1574 if (!psf) {
1575 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1576 if (!psf)
1577 return -ENOBUFS;
1578 psf->sf_inaddr = *psfsrc;
1579 if (psf_prev) {
1580 psf_prev->sf_next = psf;
1581 } else
1582 pmc->sources = psf;
1584 psf->sf_count[sfmode]++;
1585 if (psf->sf_count[sfmode] == 1) {
1586 ip_rt_multicast_event(pmc->interface);
1588 return 0;
1591 #ifdef CONFIG_IP_MULTICAST
1592 static void sf_markstate(struct ip_mc_list *pmc)
1594 struct ip_sf_list *psf;
1595 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1597 for (psf=pmc->sources; psf; psf=psf->sf_next)
1598 if (pmc->sfcount[MCAST_EXCLUDE]) {
1599 psf->sf_oldin = mca_xcount ==
1600 psf->sf_count[MCAST_EXCLUDE] &&
1601 !psf->sf_count[MCAST_INCLUDE];
1602 } else
1603 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1606 static int sf_setstate(struct ip_mc_list *pmc)
1608 struct ip_sf_list *psf, *dpsf;
1609 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1610 int qrv = pmc->interface->mr_qrv;
1611 int new_in, rv;
1613 rv = 0;
1614 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1615 if (pmc->sfcount[MCAST_EXCLUDE]) {
1616 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1617 !psf->sf_count[MCAST_INCLUDE];
1618 } else
1619 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1620 if (new_in) {
1621 if (!psf->sf_oldin) {
1622 struct ip_sf_list *prev = NULL;
1624 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1625 if (dpsf->sf_inaddr == psf->sf_inaddr)
1626 break;
1627 prev = dpsf;
1629 if (dpsf) {
1630 if (prev)
1631 prev->sf_next = dpsf->sf_next;
1632 else
1633 pmc->tomb = dpsf->sf_next;
1634 kfree(dpsf);
1636 psf->sf_crcount = qrv;
1637 rv++;
1639 } else if (psf->sf_oldin) {
1641 psf->sf_crcount = 0;
1643 * add or update "delete" records if an active filter
1644 * is now inactive
1646 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1647 if (dpsf->sf_inaddr == psf->sf_inaddr)
1648 break;
1649 if (!dpsf) {
1650 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1651 if (!dpsf)
1652 continue;
1653 *dpsf = *psf;
1654 /* pmc->lock held by callers */
1655 dpsf->sf_next = pmc->tomb;
1656 pmc->tomb = dpsf;
1658 dpsf->sf_crcount = qrv;
1659 rv++;
1662 return rv;
1664 #endif
1667 * Add multicast source filter list to the interface list
1669 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1670 int sfcount, __be32 *psfsrc, int delta)
1672 struct ip_mc_list *pmc;
1673 int isexclude;
1674 int i, err;
1676 if (!in_dev)
1677 return -ENODEV;
1678 read_lock(&in_dev->mc_list_lock);
1679 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1680 if (*pmca == pmc->multiaddr)
1681 break;
1683 if (!pmc) {
1684 /* MCA not found?? bug */
1685 read_unlock(&in_dev->mc_list_lock);
1686 return -ESRCH;
1688 spin_lock_bh(&pmc->lock);
1689 read_unlock(&in_dev->mc_list_lock);
1691 #ifdef CONFIG_IP_MULTICAST
1692 sf_markstate(pmc);
1693 #endif
1694 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1695 if (!delta)
1696 pmc->sfcount[sfmode]++;
1697 err = 0;
1698 for (i=0; i<sfcount; i++) {
1699 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1700 if (err)
1701 break;
1703 if (err) {
1704 int j;
1706 pmc->sfcount[sfmode]--;
1707 for (j=0; j<i; j++)
1708 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1709 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1710 #ifdef CONFIG_IP_MULTICAST
1711 struct ip_sf_list *psf;
1712 in_dev = pmc->interface;
1713 #endif
1715 /* filter mode change */
1716 if (pmc->sfcount[MCAST_EXCLUDE])
1717 pmc->sfmode = MCAST_EXCLUDE;
1718 else if (pmc->sfcount[MCAST_INCLUDE])
1719 pmc->sfmode = MCAST_INCLUDE;
1720 #ifdef CONFIG_IP_MULTICAST
1721 /* else no filters; keep old mode for reports */
1723 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1724 IGMP_Unsolicited_Report_Count;
1725 in_dev->mr_ifc_count = pmc->crcount;
1726 for (psf=pmc->sources; psf; psf = psf->sf_next)
1727 psf->sf_crcount = 0;
1728 igmp_ifc_event(in_dev);
1729 } else if (sf_setstate(pmc)) {
1730 igmp_ifc_event(in_dev);
1731 #endif
1733 spin_unlock_bh(&pmc->lock);
1734 return err;
1737 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1739 struct ip_sf_list *psf, *nextpsf;
1741 for (psf=pmc->tomb; psf; psf=nextpsf) {
1742 nextpsf = psf->sf_next;
1743 kfree(psf);
1745 pmc->tomb = NULL;
1746 for (psf=pmc->sources; psf; psf=nextpsf) {
1747 nextpsf = psf->sf_next;
1748 kfree(psf);
1750 pmc->sources = NULL;
1751 pmc->sfmode = MCAST_EXCLUDE;
1752 pmc->sfcount[MCAST_INCLUDE] = 0;
1753 pmc->sfcount[MCAST_EXCLUDE] = 1;
1758 * Join a multicast group
1760 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1762 int err;
1763 __be32 addr = imr->imr_multiaddr.s_addr;
1764 struct ip_mc_socklist *iml = NULL, *i;
1765 struct in_device *in_dev;
1766 struct inet_sock *inet = inet_sk(sk);
1767 struct net *net = sock_net(sk);
1768 int ifindex;
1769 int count = 0;
1771 if (!ipv4_is_multicast(addr))
1772 return -EINVAL;
1774 rtnl_lock();
1776 in_dev = ip_mc_find_dev(net, imr);
1778 if (!in_dev) {
1779 iml = NULL;
1780 err = -ENODEV;
1781 goto done;
1784 err = -EADDRINUSE;
1785 ifindex = imr->imr_ifindex;
1786 for (i = inet->mc_list; i; i = i->next) {
1787 if (i->multi.imr_multiaddr.s_addr == addr &&
1788 i->multi.imr_ifindex == ifindex)
1789 goto done;
1790 count++;
1792 err = -ENOBUFS;
1793 if (count >= sysctl_igmp_max_memberships)
1794 goto done;
1795 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1796 if (iml == NULL)
1797 goto done;
1799 memcpy(&iml->multi, imr, sizeof(*imr));
1800 iml->next = inet->mc_list;
1801 iml->sflist = NULL;
1802 iml->sfmode = MCAST_EXCLUDE;
1803 rcu_assign_pointer(inet->mc_list, iml);
1804 ip_mc_inc_group(in_dev, addr);
1805 err = 0;
1806 done:
1807 rtnl_unlock();
1808 return err;
1810 EXPORT_SYMBOL(ip_mc_join_group);
1812 static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1814 struct ip_sf_socklist *psf;
1816 psf = container_of(rp, struct ip_sf_socklist, rcu);
1817 /* sk_omem_alloc should have been decreased by the caller*/
1818 kfree(psf);
1821 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1822 struct in_device *in_dev)
1824 struct ip_sf_socklist *psf = iml->sflist;
1825 int err;
1827 if (psf == NULL) {
1828 /* any-source empty exclude case */
1829 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1830 iml->sfmode, 0, NULL, 0);
1832 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1833 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1834 rcu_assign_pointer(iml->sflist, NULL);
1835 /* decrease mem now to avoid the memleak warning */
1836 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1837 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
1838 return err;
1842 static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1844 struct ip_mc_socklist *iml;
1846 iml = container_of(rp, struct ip_mc_socklist, rcu);
1847 /* sk_omem_alloc should have been decreased by the caller*/
1848 kfree(iml);
1853 * Ask a socket to leave a group.
1856 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1858 struct inet_sock *inet = inet_sk(sk);
1859 struct ip_mc_socklist *iml, **imlp;
1860 struct in_device *in_dev;
1861 struct net *net = sock_net(sk);
1862 __be32 group = imr->imr_multiaddr.s_addr;
1863 u32 ifindex;
1864 int ret = -EADDRNOTAVAIL;
1866 rtnl_lock();
1867 in_dev = ip_mc_find_dev(net, imr);
1868 ifindex = imr->imr_ifindex;
1869 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1870 if (iml->multi.imr_multiaddr.s_addr != group)
1871 continue;
1872 if (ifindex) {
1873 if (iml->multi.imr_ifindex != ifindex)
1874 continue;
1875 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1876 iml->multi.imr_address.s_addr)
1877 continue;
1879 (void) ip_mc_leave_src(sk, iml, in_dev);
1881 rcu_assign_pointer(*imlp, iml->next);
1883 if (in_dev)
1884 ip_mc_dec_group(in_dev, group);
1885 rtnl_unlock();
1886 /* decrease mem now to avoid the memleak warning */
1887 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1888 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
1889 return 0;
1891 if (!in_dev)
1892 ret = -ENODEV;
1893 rtnl_unlock();
1894 return ret;
1897 int ip_mc_source(int add, int omode, struct sock *sk, struct
1898 ip_mreq_source *mreqs, int ifindex)
1900 int err;
1901 struct ip_mreqn imr;
1902 __be32 addr = mreqs->imr_multiaddr;
1903 struct ip_mc_socklist *pmc;
1904 struct in_device *in_dev = NULL;
1905 struct inet_sock *inet = inet_sk(sk);
1906 struct ip_sf_socklist *psl;
1907 struct net *net = sock_net(sk);
1908 int leavegroup = 0;
1909 int i, j, rv;
1911 if (!ipv4_is_multicast(addr))
1912 return -EINVAL;
1914 rtnl_lock();
1916 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1917 imr.imr_address.s_addr = mreqs->imr_interface;
1918 imr.imr_ifindex = ifindex;
1919 in_dev = ip_mc_find_dev(net, &imr);
1921 if (!in_dev) {
1922 err = -ENODEV;
1923 goto done;
1925 err = -EADDRNOTAVAIL;
1927 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1928 if ((pmc->multi.imr_multiaddr.s_addr ==
1929 imr.imr_multiaddr.s_addr) &&
1930 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1931 break;
1933 if (!pmc) { /* must have a prior join */
1934 err = -EINVAL;
1935 goto done;
1937 /* if a source filter was set, must be the same mode as before */
1938 if (pmc->sflist) {
1939 if (pmc->sfmode != omode) {
1940 err = -EINVAL;
1941 goto done;
1943 } else if (pmc->sfmode != omode) {
1944 /* allow mode switches for empty-set filters */
1945 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1946 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1947 NULL, 0);
1948 pmc->sfmode = omode;
1951 psl = pmc->sflist;
1952 if (!add) {
1953 if (!psl)
1954 goto done; /* err = -EADDRNOTAVAIL */
1955 rv = !0;
1956 for (i=0; i<psl->sl_count; i++) {
1957 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1958 sizeof(__be32));
1959 if (rv == 0)
1960 break;
1962 if (rv) /* source not found */
1963 goto done; /* err = -EADDRNOTAVAIL */
1965 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1966 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1967 leavegroup = 1;
1968 goto done;
1971 /* update the interface filter */
1972 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1973 &mreqs->imr_sourceaddr, 1);
1975 for (j=i+1; j<psl->sl_count; j++)
1976 psl->sl_addr[j-1] = psl->sl_addr[j];
1977 psl->sl_count--;
1978 err = 0;
1979 goto done;
1981 /* else, add a new source to the filter */
1983 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1984 err = -ENOBUFS;
1985 goto done;
1987 if (!psl || psl->sl_count == psl->sl_max) {
1988 struct ip_sf_socklist *newpsl;
1989 int count = IP_SFBLOCK;
1991 if (psl)
1992 count += psl->sl_max;
1993 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1994 if (!newpsl) {
1995 err = -ENOBUFS;
1996 goto done;
1998 newpsl->sl_max = count;
1999 newpsl->sl_count = count - IP_SFBLOCK;
2000 if (psl) {
2001 for (i=0; i<psl->sl_count; i++)
2002 newpsl->sl_addr[i] = psl->sl_addr[i];
2003 /* decrease mem now to avoid the memleak warning */
2004 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2005 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2007 rcu_assign_pointer(pmc->sflist, newpsl);
2008 psl = newpsl;
2010 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2011 for (i=0; i<psl->sl_count; i++) {
2012 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2013 sizeof(__be32));
2014 if (rv == 0)
2015 break;
2017 if (rv == 0) /* address already there is an error */
2018 goto done;
2019 for (j=psl->sl_count-1; j>=i; j--)
2020 psl->sl_addr[j+1] = psl->sl_addr[j];
2021 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2022 psl->sl_count++;
2023 err = 0;
2024 /* update the interface list */
2025 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2026 &mreqs->imr_sourceaddr, 1);
2027 done:
2028 rtnl_unlock();
2029 if (leavegroup)
2030 return ip_mc_leave_group(sk, &imr);
2031 return err;
2034 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2036 int err = 0;
2037 struct ip_mreqn imr;
2038 __be32 addr = msf->imsf_multiaddr;
2039 struct ip_mc_socklist *pmc;
2040 struct in_device *in_dev;
2041 struct inet_sock *inet = inet_sk(sk);
2042 struct ip_sf_socklist *newpsl, *psl;
2043 struct net *net = sock_net(sk);
2044 int leavegroup = 0;
2046 if (!ipv4_is_multicast(addr))
2047 return -EINVAL;
2048 if (msf->imsf_fmode != MCAST_INCLUDE &&
2049 msf->imsf_fmode != MCAST_EXCLUDE)
2050 return -EINVAL;
2052 rtnl_lock();
2054 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2055 imr.imr_address.s_addr = msf->imsf_interface;
2056 imr.imr_ifindex = ifindex;
2057 in_dev = ip_mc_find_dev(net, &imr);
2059 if (!in_dev) {
2060 err = -ENODEV;
2061 goto done;
2064 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2065 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2066 leavegroup = 1;
2067 goto done;
2070 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2071 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2072 pmc->multi.imr_ifindex == imr.imr_ifindex)
2073 break;
2075 if (!pmc) { /* must have a prior join */
2076 err = -EINVAL;
2077 goto done;
2079 if (msf->imsf_numsrc) {
2080 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2081 GFP_KERNEL);
2082 if (!newpsl) {
2083 err = -ENOBUFS;
2084 goto done;
2086 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2087 memcpy(newpsl->sl_addr, msf->imsf_slist,
2088 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2089 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2090 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2091 if (err) {
2092 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2093 goto done;
2095 } else {
2096 newpsl = NULL;
2097 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2098 msf->imsf_fmode, 0, NULL, 0);
2100 psl = pmc->sflist;
2101 if (psl) {
2102 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2103 psl->sl_count, psl->sl_addr, 0);
2104 /* decrease mem now to avoid the memleak warning */
2105 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2106 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2107 } else
2108 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2109 0, NULL, 0);
2110 rcu_assign_pointer(pmc->sflist, newpsl);
2111 pmc->sfmode = msf->imsf_fmode;
2112 err = 0;
2113 done:
2114 rtnl_unlock();
2115 if (leavegroup)
2116 err = ip_mc_leave_group(sk, &imr);
2117 return err;
2120 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2121 struct ip_msfilter __user *optval, int __user *optlen)
2123 int err, len, count, copycount;
2124 struct ip_mreqn imr;
2125 __be32 addr = msf->imsf_multiaddr;
2126 struct ip_mc_socklist *pmc;
2127 struct in_device *in_dev;
2128 struct inet_sock *inet = inet_sk(sk);
2129 struct ip_sf_socklist *psl;
2130 struct net *net = sock_net(sk);
2132 if (!ipv4_is_multicast(addr))
2133 return -EINVAL;
2135 rtnl_lock();
2137 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2138 imr.imr_address.s_addr = msf->imsf_interface;
2139 imr.imr_ifindex = 0;
2140 in_dev = ip_mc_find_dev(net, &imr);
2142 if (!in_dev) {
2143 err = -ENODEV;
2144 goto done;
2146 err = -EADDRNOTAVAIL;
2148 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2149 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2150 pmc->multi.imr_ifindex == imr.imr_ifindex)
2151 break;
2153 if (!pmc) /* must have a prior join */
2154 goto done;
2155 msf->imsf_fmode = pmc->sfmode;
2156 psl = pmc->sflist;
2157 rtnl_unlock();
2158 if (!psl) {
2159 len = 0;
2160 count = 0;
2161 } else {
2162 count = psl->sl_count;
2164 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2165 len = copycount * sizeof(psl->sl_addr[0]);
2166 msf->imsf_numsrc = count;
2167 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2168 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2169 return -EFAULT;
2171 if (len &&
2172 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2173 return -EFAULT;
2174 return 0;
2175 done:
2176 rtnl_unlock();
2177 return err;
2180 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2181 struct group_filter __user *optval, int __user *optlen)
2183 int err, i, count, copycount;
2184 struct sockaddr_in *psin;
2185 __be32 addr;
2186 struct ip_mc_socklist *pmc;
2187 struct inet_sock *inet = inet_sk(sk);
2188 struct ip_sf_socklist *psl;
2190 psin = (struct sockaddr_in *)&gsf->gf_group;
2191 if (psin->sin_family != AF_INET)
2192 return -EINVAL;
2193 addr = psin->sin_addr.s_addr;
2194 if (!ipv4_is_multicast(addr))
2195 return -EINVAL;
2197 rtnl_lock();
2199 err = -EADDRNOTAVAIL;
2201 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2202 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2203 pmc->multi.imr_ifindex == gsf->gf_interface)
2204 break;
2206 if (!pmc) /* must have a prior join */
2207 goto done;
2208 gsf->gf_fmode = pmc->sfmode;
2209 psl = pmc->sflist;
2210 rtnl_unlock();
2211 count = psl ? psl->sl_count : 0;
2212 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2213 gsf->gf_numsrc = count;
2214 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2215 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2216 return -EFAULT;
2218 for (i=0; i<copycount; i++) {
2219 struct sockaddr_storage ss;
2221 psin = (struct sockaddr_in *)&ss;
2222 memset(&ss, 0, sizeof(ss));
2223 psin->sin_family = AF_INET;
2224 psin->sin_addr.s_addr = psl->sl_addr[i];
2225 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2226 return -EFAULT;
2228 return 0;
2229 done:
2230 rtnl_unlock();
2231 return err;
2235 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2237 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2239 struct inet_sock *inet = inet_sk(sk);
2240 struct ip_mc_socklist *pmc;
2241 struct ip_sf_socklist *psl;
2242 int i;
2243 int ret;
2245 ret = 1;
2246 if (!ipv4_is_multicast(loc_addr))
2247 goto out;
2249 rcu_read_lock();
2250 for (pmc=rcu_dereference(inet->mc_list); pmc; pmc=rcu_dereference(pmc->next)) {
2251 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2252 pmc->multi.imr_ifindex == dif)
2253 break;
2255 ret = inet->mc_all;
2256 if (!pmc)
2257 goto unlock;
2258 psl = pmc->sflist;
2259 ret = (pmc->sfmode == MCAST_EXCLUDE);
2260 if (!psl)
2261 goto unlock;
2263 for (i=0; i<psl->sl_count; i++) {
2264 if (psl->sl_addr[i] == rmt_addr)
2265 break;
2267 ret = 0;
2268 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2269 goto unlock;
2270 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2271 goto unlock;
2272 ret = 1;
2273 unlock:
2274 rcu_read_unlock();
2275 out:
2276 return ret;
2280 * A socket is closing.
2283 void ip_mc_drop_socket(struct sock *sk)
2285 struct inet_sock *inet = inet_sk(sk);
2286 struct ip_mc_socklist *iml;
2287 struct net *net = sock_net(sk);
2289 if (inet->mc_list == NULL)
2290 return;
2292 rtnl_lock();
2293 while ((iml = inet->mc_list) != NULL) {
2294 struct in_device *in_dev;
2295 rcu_assign_pointer(inet->mc_list, iml->next);
2297 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2298 (void) ip_mc_leave_src(sk, iml, in_dev);
2299 if (in_dev != NULL) {
2300 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2301 in_dev_put(in_dev);
2303 /* decrease mem now to avoid the memleak warning */
2304 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2305 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
2307 rtnl_unlock();
2310 int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2312 struct ip_mc_list *im;
2313 struct ip_sf_list *psf;
2314 int rv = 0;
2316 read_lock(&in_dev->mc_list_lock);
2317 for (im=in_dev->mc_list; im; im=im->next) {
2318 if (im->multiaddr == mc_addr)
2319 break;
2321 if (im && proto == IPPROTO_IGMP) {
2322 rv = 1;
2323 } else if (im) {
2324 if (src_addr) {
2325 for (psf=im->sources; psf; psf=psf->sf_next) {
2326 if (psf->sf_inaddr == src_addr)
2327 break;
2329 if (psf)
2330 rv = psf->sf_count[MCAST_INCLUDE] ||
2331 psf->sf_count[MCAST_EXCLUDE] !=
2332 im->sfcount[MCAST_EXCLUDE];
2333 else
2334 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2335 } else
2336 rv = 1; /* unspecified source; tentatively allow */
2338 read_unlock(&in_dev->mc_list_lock);
2339 return rv;
2342 #if defined(CONFIG_PROC_FS)
2343 struct igmp_mc_iter_state {
2344 struct seq_net_private p;
2345 struct net_device *dev;
2346 struct in_device *in_dev;
2349 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2351 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2353 struct net *net = seq_file_net(seq);
2354 struct ip_mc_list *im = NULL;
2355 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2357 state->in_dev = NULL;
2358 for_each_netdev_rcu(net, state->dev) {
2359 struct in_device *in_dev;
2361 in_dev = __in_dev_get_rcu(state->dev);
2362 if (!in_dev)
2363 continue;
2364 read_lock(&in_dev->mc_list_lock);
2365 im = in_dev->mc_list;
2366 if (im) {
2367 state->in_dev = in_dev;
2368 break;
2370 read_unlock(&in_dev->mc_list_lock);
2372 return im;
2375 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2377 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2378 im = im->next;
2379 while (!im) {
2380 if (likely(state->in_dev != NULL))
2381 read_unlock(&state->in_dev->mc_list_lock);
2383 state->dev = next_net_device_rcu(state->dev);
2384 if (!state->dev) {
2385 state->in_dev = NULL;
2386 break;
2388 state->in_dev = __in_dev_get_rcu(state->dev);
2389 if (!state->in_dev)
2390 continue;
2391 read_lock(&state->in_dev->mc_list_lock);
2392 im = state->in_dev->mc_list;
2394 return im;
2397 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2399 struct ip_mc_list *im = igmp_mc_get_first(seq);
2400 if (im)
2401 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2402 --pos;
2403 return pos ? NULL : im;
2406 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2407 __acquires(rcu)
2409 rcu_read_lock();
2410 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2413 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2415 struct ip_mc_list *im;
2416 if (v == SEQ_START_TOKEN)
2417 im = igmp_mc_get_first(seq);
2418 else
2419 im = igmp_mc_get_next(seq, v);
2420 ++*pos;
2421 return im;
2424 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2425 __releases(rcu)
2427 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2428 if (likely(state->in_dev != NULL)) {
2429 read_unlock(&state->in_dev->mc_list_lock);
2430 state->in_dev = NULL;
2432 state->dev = NULL;
2433 rcu_read_unlock();
2436 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2438 if (v == SEQ_START_TOKEN)
2439 seq_puts(seq,
2440 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2441 else {
2442 struct ip_mc_list *im = (struct ip_mc_list *)v;
2443 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2444 char *querier;
2445 #ifdef CONFIG_IP_MULTICAST
2446 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2447 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2448 "V3";
2449 #else
2450 querier = "NONE";
2451 #endif
2453 if (state->in_dev->mc_list == im) {
2454 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2455 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2458 seq_printf(seq,
2459 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2460 im->multiaddr, im->users,
2461 im->tm_running, im->tm_running ?
2462 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2463 im->reporter);
2465 return 0;
2468 static const struct seq_operations igmp_mc_seq_ops = {
2469 .start = igmp_mc_seq_start,
2470 .next = igmp_mc_seq_next,
2471 .stop = igmp_mc_seq_stop,
2472 .show = igmp_mc_seq_show,
2475 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2477 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2478 sizeof(struct igmp_mc_iter_state));
2481 static const struct file_operations igmp_mc_seq_fops = {
2482 .owner = THIS_MODULE,
2483 .open = igmp_mc_seq_open,
2484 .read = seq_read,
2485 .llseek = seq_lseek,
2486 .release = seq_release_net,
2489 struct igmp_mcf_iter_state {
2490 struct seq_net_private p;
2491 struct net_device *dev;
2492 struct in_device *idev;
2493 struct ip_mc_list *im;
2496 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2498 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2500 struct net *net = seq_file_net(seq);
2501 struct ip_sf_list *psf = NULL;
2502 struct ip_mc_list *im = NULL;
2503 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2505 state->idev = NULL;
2506 state->im = NULL;
2507 for_each_netdev_rcu(net, state->dev) {
2508 struct in_device *idev;
2509 idev = __in_dev_get_rcu(state->dev);
2510 if (unlikely(idev == NULL))
2511 continue;
2512 read_lock(&idev->mc_list_lock);
2513 im = idev->mc_list;
2514 if (likely(im != NULL)) {
2515 spin_lock_bh(&im->lock);
2516 psf = im->sources;
2517 if (likely(psf != NULL)) {
2518 state->im = im;
2519 state->idev = idev;
2520 break;
2522 spin_unlock_bh(&im->lock);
2524 read_unlock(&idev->mc_list_lock);
2526 return psf;
2529 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2531 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2533 psf = psf->sf_next;
2534 while (!psf) {
2535 spin_unlock_bh(&state->im->lock);
2536 state->im = state->im->next;
2537 while (!state->im) {
2538 if (likely(state->idev != NULL))
2539 read_unlock(&state->idev->mc_list_lock);
2541 state->dev = next_net_device_rcu(state->dev);
2542 if (!state->dev) {
2543 state->idev = NULL;
2544 goto out;
2546 state->idev = __in_dev_get_rcu(state->dev);
2547 if (!state->idev)
2548 continue;
2549 read_lock(&state->idev->mc_list_lock);
2550 state->im = state->idev->mc_list;
2552 if (!state->im)
2553 break;
2554 spin_lock_bh(&state->im->lock);
2555 psf = state->im->sources;
2557 out:
2558 return psf;
2561 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2563 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2564 if (psf)
2565 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2566 --pos;
2567 return pos ? NULL : psf;
2570 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2571 __acquires(rcu)
2573 rcu_read_lock();
2574 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2577 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2579 struct ip_sf_list *psf;
2580 if (v == SEQ_START_TOKEN)
2581 psf = igmp_mcf_get_first(seq);
2582 else
2583 psf = igmp_mcf_get_next(seq, v);
2584 ++*pos;
2585 return psf;
2588 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2589 __releases(rcu)
2591 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2592 if (likely(state->im != NULL)) {
2593 spin_unlock_bh(&state->im->lock);
2594 state->im = NULL;
2596 if (likely(state->idev != NULL)) {
2597 read_unlock(&state->idev->mc_list_lock);
2598 state->idev = NULL;
2600 state->dev = NULL;
2601 rcu_read_unlock();
2604 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2606 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2607 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2609 if (v == SEQ_START_TOKEN) {
2610 seq_printf(seq,
2611 "%3s %6s "
2612 "%10s %10s %6s %6s\n", "Idx",
2613 "Device", "MCA",
2614 "SRC", "INC", "EXC");
2615 } else {
2616 seq_printf(seq,
2617 "%3d %6.6s 0x%08x "
2618 "0x%08x %6lu %6lu\n",
2619 state->dev->ifindex, state->dev->name,
2620 ntohl(state->im->multiaddr),
2621 ntohl(psf->sf_inaddr),
2622 psf->sf_count[MCAST_INCLUDE],
2623 psf->sf_count[MCAST_EXCLUDE]);
2625 return 0;
2628 static const struct seq_operations igmp_mcf_seq_ops = {
2629 .start = igmp_mcf_seq_start,
2630 .next = igmp_mcf_seq_next,
2631 .stop = igmp_mcf_seq_stop,
2632 .show = igmp_mcf_seq_show,
2635 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2637 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2638 sizeof(struct igmp_mcf_iter_state));
2641 static const struct file_operations igmp_mcf_seq_fops = {
2642 .owner = THIS_MODULE,
2643 .open = igmp_mcf_seq_open,
2644 .read = seq_read,
2645 .llseek = seq_lseek,
2646 .release = seq_release_net,
2649 static int __net_init igmp_net_init(struct net *net)
2651 struct proc_dir_entry *pde;
2653 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2654 if (!pde)
2655 goto out_igmp;
2656 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2657 if (!pde)
2658 goto out_mcfilter;
2659 return 0;
2661 out_mcfilter:
2662 proc_net_remove(net, "igmp");
2663 out_igmp:
2664 return -ENOMEM;
2667 static void __net_exit igmp_net_exit(struct net *net)
2669 proc_net_remove(net, "mcfilter");
2670 proc_net_remove(net, "igmp");
2673 static struct pernet_operations igmp_net_ops = {
2674 .init = igmp_net_init,
2675 .exit = igmp_net_exit,
2678 int __init igmp_mc_proc_init(void)
2680 return register_pernet_subsys(&igmp_net_ops);
2682 #endif