Merge branch 'fix/hda' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux/fpc-iii.git] / net / ipv4 / igmp.c
blob76c08402c933941ea475045545ff3206cc0cadb8
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 <asm/uaccess.h>
75 #include <asm/system.h>
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/jiffies.h>
79 #include <linux/string.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
82 #include <linux/in.h>
83 #include <linux/inet.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/if_arp.h>
89 #include <linux/rtnetlink.h>
90 #include <linux/times.h>
92 #include <net/net_namespace.h>
93 #include <net/arp.h>
94 #include <net/ip.h>
95 #include <net/protocol.h>
96 #include <net/route.h>
97 #include <net/sock.h>
98 #include <net/checksum.h>
99 #include <linux/netfilter_ipv4.h>
100 #ifdef CONFIG_IP_MROUTE
101 #include <linux/mroute.h>
102 #endif
103 #ifdef CONFIG_PROC_FS
104 #include <linux/proc_fs.h>
105 #include <linux/seq_file.h>
106 #endif
108 #define IP_MAX_MEMBERSHIPS 20
109 #define IP_MAX_MSF 10
111 #ifdef CONFIG_IP_MULTICAST
112 /* Parameter names and values are taken from igmp-v2-06 draft */
114 #define IGMP_V1_Router_Present_Timeout (400*HZ)
115 #define IGMP_V2_Router_Present_Timeout (400*HZ)
116 #define IGMP_Unsolicited_Report_Interval (10*HZ)
117 #define IGMP_Query_Response_Interval (10*HZ)
118 #define IGMP_Unsolicited_Report_Count 2
121 #define IGMP_Initial_Report_Delay (1)
123 /* IGMP_Initial_Report_Delay is not from IGMP specs!
124 * IGMP specs require to report membership immediately after
125 * joining a group, but we delay the first report by a
126 * small interval. It seems more natural and still does not
127 * contradict to specs provided this delay is small enough.
130 #define IGMP_V1_SEEN(in_dev) \
131 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
132 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
133 ((in_dev)->mr_v1_seen && \
134 time_before(jiffies, (in_dev)->mr_v1_seen)))
135 #define IGMP_V2_SEEN(in_dev) \
136 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
137 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
141 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
142 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
143 static void igmpv3_clear_delrec(struct in_device *in_dev);
144 static int sf_setstate(struct ip_mc_list *pmc);
145 static void sf_markstate(struct ip_mc_list *pmc);
146 #endif
147 static void ip_mc_clear_src(struct ip_mc_list *pmc);
148 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
149 int sfcount, __be32 *psfsrc, int delta);
151 static void ip_ma_put(struct ip_mc_list *im)
153 if (atomic_dec_and_test(&im->refcnt)) {
154 in_dev_put(im->interface);
155 kfree(im);
159 #ifdef CONFIG_IP_MULTICAST
162 * Timer management
165 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
167 spin_lock_bh(&im->lock);
168 if (del_timer(&im->timer))
169 atomic_dec(&im->refcnt);
170 im->tm_running = 0;
171 im->reporter = 0;
172 im->unsolicit_count = 0;
173 spin_unlock_bh(&im->lock);
176 /* It must be called with locked im->lock */
177 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
179 int tv = net_random() % max_delay;
181 im->tm_running = 1;
182 if (!mod_timer(&im->timer, jiffies+tv+2))
183 atomic_inc(&im->refcnt);
186 static void igmp_gq_start_timer(struct in_device *in_dev)
188 int tv = net_random() % in_dev->mr_maxdelay;
190 in_dev->mr_gq_running = 1;
191 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
192 in_dev_hold(in_dev);
195 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
197 int tv = net_random() % delay;
199 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
200 in_dev_hold(in_dev);
203 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
205 spin_lock_bh(&im->lock);
206 im->unsolicit_count = 0;
207 if (del_timer(&im->timer)) {
208 if ((long)(im->timer.expires-jiffies) < max_delay) {
209 add_timer(&im->timer);
210 im->tm_running = 1;
211 spin_unlock_bh(&im->lock);
212 return;
214 atomic_dec(&im->refcnt);
216 igmp_start_timer(im, max_delay);
217 spin_unlock_bh(&im->lock);
222 * Send an IGMP report.
225 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
228 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
229 int gdeleted, int sdeleted)
231 switch (type) {
232 case IGMPV3_MODE_IS_INCLUDE:
233 case IGMPV3_MODE_IS_EXCLUDE:
234 if (gdeleted || sdeleted)
235 return 0;
236 if (!(pmc->gsquery && !psf->sf_gsresp)) {
237 if (pmc->sfmode == MCAST_INCLUDE)
238 return 1;
239 /* don't include if this source is excluded
240 * in all filters
242 if (psf->sf_count[MCAST_INCLUDE])
243 return type == IGMPV3_MODE_IS_INCLUDE;
244 return pmc->sfcount[MCAST_EXCLUDE] ==
245 psf->sf_count[MCAST_EXCLUDE];
247 return 0;
248 case IGMPV3_CHANGE_TO_INCLUDE:
249 if (gdeleted || sdeleted)
250 return 0;
251 return psf->sf_count[MCAST_INCLUDE] != 0;
252 case IGMPV3_CHANGE_TO_EXCLUDE:
253 if (gdeleted || sdeleted)
254 return 0;
255 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
256 psf->sf_count[MCAST_INCLUDE])
257 return 0;
258 return pmc->sfcount[MCAST_EXCLUDE] ==
259 psf->sf_count[MCAST_EXCLUDE];
260 case IGMPV3_ALLOW_NEW_SOURCES:
261 if (gdeleted || !psf->sf_crcount)
262 return 0;
263 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
264 case IGMPV3_BLOCK_OLD_SOURCES:
265 if (pmc->sfmode == MCAST_INCLUDE)
266 return gdeleted || (psf->sf_crcount && sdeleted);
267 return psf->sf_crcount && !gdeleted && !sdeleted;
269 return 0;
272 static int
273 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
275 struct ip_sf_list *psf;
276 int scount = 0;
278 for (psf=pmc->sources; psf; psf=psf->sf_next) {
279 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
280 continue;
281 scount++;
283 return scount;
286 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
288 struct sk_buff *skb;
289 struct rtable *rt;
290 struct iphdr *pip;
291 struct igmpv3_report *pig;
292 struct net *net = dev_net(dev);
294 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
295 if (skb == NULL)
296 return NULL;
299 struct flowi fl = { .oif = dev->ifindex,
300 .nl_u = { .ip4_u = {
301 .daddr = IGMPV3_ALL_MCR } },
302 .proto = IPPROTO_IGMP };
303 if (ip_route_output_key(net, &rt, &fl)) {
304 kfree_skb(skb);
305 return NULL;
308 if (rt->rt_src == 0) {
309 kfree_skb(skb);
310 ip_rt_put(rt);
311 return NULL;
314 skb_dst_set(skb, &rt->u.dst);
315 skb->dev = dev;
317 skb_reserve(skb, LL_RESERVED_SPACE(dev));
319 skb_reset_network_header(skb);
320 pip = ip_hdr(skb);
321 skb_put(skb, sizeof(struct iphdr) + 4);
323 pip->version = 4;
324 pip->ihl = (sizeof(struct iphdr)+4)>>2;
325 pip->tos = 0xc0;
326 pip->frag_off = htons(IP_DF);
327 pip->ttl = 1;
328 pip->daddr = rt->rt_dst;
329 pip->saddr = rt->rt_src;
330 pip->protocol = IPPROTO_IGMP;
331 pip->tot_len = 0; /* filled in later */
332 ip_select_ident(pip, &rt->u.dst, NULL);
333 ((u8*)&pip[1])[0] = IPOPT_RA;
334 ((u8*)&pip[1])[1] = 4;
335 ((u8*)&pip[1])[2] = 0;
336 ((u8*)&pip[1])[3] = 0;
338 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
339 skb_put(skb, sizeof(*pig));
340 pig = igmpv3_report_hdr(skb);
341 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
342 pig->resv1 = 0;
343 pig->csum = 0;
344 pig->resv2 = 0;
345 pig->ngrec = 0;
346 return skb;
349 static int igmpv3_sendpack(struct sk_buff *skb)
351 struct igmphdr *pig = igmp_hdr(skb);
352 const int igmplen = skb->tail - skb->transport_header;
354 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
356 return ip_local_out(skb);
359 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
361 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
364 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
365 int type, struct igmpv3_grec **ppgr)
367 struct net_device *dev = pmc->interface->dev;
368 struct igmpv3_report *pih;
369 struct igmpv3_grec *pgr;
371 if (!skb)
372 skb = igmpv3_newpack(dev, dev->mtu);
373 if (!skb)
374 return NULL;
375 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
376 pgr->grec_type = type;
377 pgr->grec_auxwords = 0;
378 pgr->grec_nsrcs = 0;
379 pgr->grec_mca = pmc->multiaddr;
380 pih = igmpv3_report_hdr(skb);
381 pih->ngrec = htons(ntohs(pih->ngrec)+1);
382 *ppgr = pgr;
383 return skb;
386 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
387 skb_tailroom(skb)) : 0)
389 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
390 int type, int gdeleted, int sdeleted)
392 struct net_device *dev = pmc->interface->dev;
393 struct igmpv3_report *pih;
394 struct igmpv3_grec *pgr = NULL;
395 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
396 int scount, stotal, first, isquery, truncate;
398 if (pmc->multiaddr == IGMP_ALL_HOSTS)
399 return skb;
401 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
402 type == IGMPV3_MODE_IS_EXCLUDE;
403 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
404 type == IGMPV3_CHANGE_TO_EXCLUDE;
406 stotal = scount = 0;
408 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
410 if (!*psf_list)
411 goto empty_source;
413 pih = skb ? igmpv3_report_hdr(skb) : NULL;
415 /* EX and TO_EX get a fresh packet, if needed */
416 if (truncate) {
417 if (pih && pih->ngrec &&
418 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
419 if (skb)
420 igmpv3_sendpack(skb);
421 skb = igmpv3_newpack(dev, dev->mtu);
424 first = 1;
425 psf_prev = NULL;
426 for (psf=*psf_list; psf; psf=psf_next) {
427 __be32 *psrc;
429 psf_next = psf->sf_next;
431 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
432 psf_prev = psf;
433 continue;
436 /* clear marks on query responses */
437 if (isquery)
438 psf->sf_gsresp = 0;
440 if (AVAILABLE(skb) < sizeof(__be32) +
441 first*sizeof(struct igmpv3_grec)) {
442 if (truncate && !first)
443 break; /* truncate these */
444 if (pgr)
445 pgr->grec_nsrcs = htons(scount);
446 if (skb)
447 igmpv3_sendpack(skb);
448 skb = igmpv3_newpack(dev, dev->mtu);
449 first = 1;
450 scount = 0;
452 if (first) {
453 skb = add_grhead(skb, pmc, type, &pgr);
454 first = 0;
456 if (!skb)
457 return NULL;
458 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
459 *psrc = psf->sf_inaddr;
460 scount++; stotal++;
461 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
462 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
463 psf->sf_crcount--;
464 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
465 if (psf_prev)
466 psf_prev->sf_next = psf->sf_next;
467 else
468 *psf_list = psf->sf_next;
469 kfree(psf);
470 continue;
473 psf_prev = psf;
476 empty_source:
477 if (!stotal) {
478 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
479 type == IGMPV3_BLOCK_OLD_SOURCES)
480 return skb;
481 if (pmc->crcount || isquery) {
482 /* make sure we have room for group header */
483 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
484 igmpv3_sendpack(skb);
485 skb = NULL; /* add_grhead will get a new one */
487 skb = add_grhead(skb, pmc, type, &pgr);
490 if (pgr)
491 pgr->grec_nsrcs = htons(scount);
493 if (isquery)
494 pmc->gsquery = 0; /* clear query state on report */
495 return skb;
498 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
500 struct sk_buff *skb = NULL;
501 int type;
503 if (!pmc) {
504 read_lock(&in_dev->mc_list_lock);
505 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
506 if (pmc->multiaddr == IGMP_ALL_HOSTS)
507 continue;
508 spin_lock_bh(&pmc->lock);
509 if (pmc->sfcount[MCAST_EXCLUDE])
510 type = IGMPV3_MODE_IS_EXCLUDE;
511 else
512 type = IGMPV3_MODE_IS_INCLUDE;
513 skb = add_grec(skb, pmc, type, 0, 0);
514 spin_unlock_bh(&pmc->lock);
516 read_unlock(&in_dev->mc_list_lock);
517 } else {
518 spin_lock_bh(&pmc->lock);
519 if (pmc->sfcount[MCAST_EXCLUDE])
520 type = IGMPV3_MODE_IS_EXCLUDE;
521 else
522 type = IGMPV3_MODE_IS_INCLUDE;
523 skb = add_grec(skb, pmc, type, 0, 0);
524 spin_unlock_bh(&pmc->lock);
526 if (!skb)
527 return 0;
528 return igmpv3_sendpack(skb);
532 * remove zero-count source records from a source filter list
534 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
536 struct ip_sf_list *psf_prev, *psf_next, *psf;
538 psf_prev = NULL;
539 for (psf=*ppsf; psf; psf = psf_next) {
540 psf_next = psf->sf_next;
541 if (psf->sf_crcount == 0) {
542 if (psf_prev)
543 psf_prev->sf_next = psf->sf_next;
544 else
545 *ppsf = psf->sf_next;
546 kfree(psf);
547 } else
548 psf_prev = psf;
552 static void igmpv3_send_cr(struct in_device *in_dev)
554 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
555 struct sk_buff *skb = NULL;
556 int type, dtype;
558 read_lock(&in_dev->mc_list_lock);
559 spin_lock_bh(&in_dev->mc_tomb_lock);
561 /* deleted MCA's */
562 pmc_prev = NULL;
563 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
564 pmc_next = pmc->next;
565 if (pmc->sfmode == MCAST_INCLUDE) {
566 type = IGMPV3_BLOCK_OLD_SOURCES;
567 dtype = IGMPV3_BLOCK_OLD_SOURCES;
568 skb = add_grec(skb, pmc, type, 1, 0);
569 skb = add_grec(skb, pmc, dtype, 1, 1);
571 if (pmc->crcount) {
572 if (pmc->sfmode == MCAST_EXCLUDE) {
573 type = IGMPV3_CHANGE_TO_INCLUDE;
574 skb = add_grec(skb, pmc, type, 1, 0);
576 pmc->crcount--;
577 if (pmc->crcount == 0) {
578 igmpv3_clear_zeros(&pmc->tomb);
579 igmpv3_clear_zeros(&pmc->sources);
582 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
583 if (pmc_prev)
584 pmc_prev->next = pmc_next;
585 else
586 in_dev->mc_tomb = pmc_next;
587 in_dev_put(pmc->interface);
588 kfree(pmc);
589 } else
590 pmc_prev = pmc;
592 spin_unlock_bh(&in_dev->mc_tomb_lock);
594 /* change recs */
595 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
596 spin_lock_bh(&pmc->lock);
597 if (pmc->sfcount[MCAST_EXCLUDE]) {
598 type = IGMPV3_BLOCK_OLD_SOURCES;
599 dtype = IGMPV3_ALLOW_NEW_SOURCES;
600 } else {
601 type = IGMPV3_ALLOW_NEW_SOURCES;
602 dtype = IGMPV3_BLOCK_OLD_SOURCES;
604 skb = add_grec(skb, pmc, type, 0, 0);
605 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
607 /* filter mode changes */
608 if (pmc->crcount) {
609 if (pmc->sfmode == MCAST_EXCLUDE)
610 type = IGMPV3_CHANGE_TO_EXCLUDE;
611 else
612 type = IGMPV3_CHANGE_TO_INCLUDE;
613 skb = add_grec(skb, pmc, type, 0, 0);
614 pmc->crcount--;
616 spin_unlock_bh(&pmc->lock);
618 read_unlock(&in_dev->mc_list_lock);
620 if (!skb)
621 return;
622 (void) igmpv3_sendpack(skb);
625 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
626 int type)
628 struct sk_buff *skb;
629 struct iphdr *iph;
630 struct igmphdr *ih;
631 struct rtable *rt;
632 struct net_device *dev = in_dev->dev;
633 struct net *net = dev_net(dev);
634 __be32 group = pmc ? pmc->multiaddr : 0;
635 __be32 dst;
637 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
638 return igmpv3_send_report(in_dev, pmc);
639 else if (type == IGMP_HOST_LEAVE_MESSAGE)
640 dst = IGMP_ALL_ROUTER;
641 else
642 dst = group;
645 struct flowi fl = { .oif = dev->ifindex,
646 .nl_u = { .ip4_u = { .daddr = dst } },
647 .proto = IPPROTO_IGMP };
648 if (ip_route_output_key(net, &rt, &fl))
649 return -1;
651 if (rt->rt_src == 0) {
652 ip_rt_put(rt);
653 return -1;
656 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
657 if (skb == NULL) {
658 ip_rt_put(rt);
659 return -1;
662 skb_dst_set(skb, &rt->u.dst);
664 skb_reserve(skb, LL_RESERVED_SPACE(dev));
666 skb_reset_network_header(skb);
667 iph = ip_hdr(skb);
668 skb_put(skb, sizeof(struct iphdr) + 4);
670 iph->version = 4;
671 iph->ihl = (sizeof(struct iphdr)+4)>>2;
672 iph->tos = 0xc0;
673 iph->frag_off = htons(IP_DF);
674 iph->ttl = 1;
675 iph->daddr = dst;
676 iph->saddr = rt->rt_src;
677 iph->protocol = IPPROTO_IGMP;
678 ip_select_ident(iph, &rt->u.dst, NULL);
679 ((u8*)&iph[1])[0] = IPOPT_RA;
680 ((u8*)&iph[1])[1] = 4;
681 ((u8*)&iph[1])[2] = 0;
682 ((u8*)&iph[1])[3] = 0;
684 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
685 ih->type = type;
686 ih->code = 0;
687 ih->csum = 0;
688 ih->group = group;
689 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
691 return ip_local_out(skb);
694 static void igmp_gq_timer_expire(unsigned long data)
696 struct in_device *in_dev = (struct in_device *)data;
698 in_dev->mr_gq_running = 0;
699 igmpv3_send_report(in_dev, NULL);
700 __in_dev_put(in_dev);
703 static void igmp_ifc_timer_expire(unsigned long data)
705 struct in_device *in_dev = (struct in_device *)data;
707 igmpv3_send_cr(in_dev);
708 if (in_dev->mr_ifc_count) {
709 in_dev->mr_ifc_count--;
710 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
712 __in_dev_put(in_dev);
715 static void igmp_ifc_event(struct in_device *in_dev)
717 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
718 return;
719 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
720 IGMP_Unsolicited_Report_Count;
721 igmp_ifc_start_timer(in_dev, 1);
725 static void igmp_timer_expire(unsigned long data)
727 struct ip_mc_list *im=(struct ip_mc_list *)data;
728 struct in_device *in_dev = im->interface;
730 spin_lock(&im->lock);
731 im->tm_running = 0;
733 if (im->unsolicit_count) {
734 im->unsolicit_count--;
735 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
737 im->reporter = 1;
738 spin_unlock(&im->lock);
740 if (IGMP_V1_SEEN(in_dev))
741 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
742 else if (IGMP_V2_SEEN(in_dev))
743 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
744 else
745 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
747 ip_ma_put(im);
750 /* mark EXCLUDE-mode sources */
751 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
753 struct ip_sf_list *psf;
754 int i, scount;
756 scount = 0;
757 for (psf=pmc->sources; psf; psf=psf->sf_next) {
758 if (scount == nsrcs)
759 break;
760 for (i=0; i<nsrcs; i++) {
761 /* skip inactive filters */
762 if (pmc->sfcount[MCAST_INCLUDE] ||
763 pmc->sfcount[MCAST_EXCLUDE] !=
764 psf->sf_count[MCAST_EXCLUDE])
765 continue;
766 if (srcs[i] == psf->sf_inaddr) {
767 scount++;
768 break;
772 pmc->gsquery = 0;
773 if (scount == nsrcs) /* all sources excluded */
774 return 0;
775 return 1;
778 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
780 struct ip_sf_list *psf;
781 int i, scount;
783 if (pmc->sfmode == MCAST_EXCLUDE)
784 return igmp_xmarksources(pmc, nsrcs, srcs);
786 /* mark INCLUDE-mode sources */
787 scount = 0;
788 for (psf=pmc->sources; psf; psf=psf->sf_next) {
789 if (scount == nsrcs)
790 break;
791 for (i=0; i<nsrcs; i++)
792 if (srcs[i] == psf->sf_inaddr) {
793 psf->sf_gsresp = 1;
794 scount++;
795 break;
798 if (!scount) {
799 pmc->gsquery = 0;
800 return 0;
802 pmc->gsquery = 1;
803 return 1;
806 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
808 struct ip_mc_list *im;
810 /* Timers are only set for non-local groups */
812 if (group == IGMP_ALL_HOSTS)
813 return;
815 read_lock(&in_dev->mc_list_lock);
816 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
817 if (im->multiaddr == group) {
818 igmp_stop_timer(im);
819 break;
822 read_unlock(&in_dev->mc_list_lock);
825 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
826 int len)
828 struct igmphdr *ih = igmp_hdr(skb);
829 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
830 struct ip_mc_list *im;
831 __be32 group = ih->group;
832 int max_delay;
833 int mark = 0;
836 if (len == 8) {
837 if (ih->code == 0) {
838 /* Alas, old v1 router presents here. */
840 max_delay = IGMP_Query_Response_Interval;
841 in_dev->mr_v1_seen = jiffies +
842 IGMP_V1_Router_Present_Timeout;
843 group = 0;
844 } else {
845 /* v2 router present */
846 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
847 in_dev->mr_v2_seen = jiffies +
848 IGMP_V2_Router_Present_Timeout;
850 /* cancel the interface change timer */
851 in_dev->mr_ifc_count = 0;
852 if (del_timer(&in_dev->mr_ifc_timer))
853 __in_dev_put(in_dev);
854 /* clear deleted report items */
855 igmpv3_clear_delrec(in_dev);
856 } else if (len < 12) {
857 return; /* ignore bogus packet; freed by caller */
858 } else { /* v3 */
859 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
860 return;
862 ih3 = igmpv3_query_hdr(skb);
863 if (ih3->nsrcs) {
864 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
865 + ntohs(ih3->nsrcs)*sizeof(__be32)))
866 return;
867 ih3 = igmpv3_query_hdr(skb);
870 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
871 if (!max_delay)
872 max_delay = 1; /* can't mod w/ 0 */
873 in_dev->mr_maxdelay = max_delay;
874 if (ih3->qrv)
875 in_dev->mr_qrv = ih3->qrv;
876 if (!group) { /* general query */
877 if (ih3->nsrcs)
878 return; /* no sources allowed */
879 igmp_gq_start_timer(in_dev);
880 return;
882 /* mark sources to include, if group & source-specific */
883 mark = ih3->nsrcs != 0;
887 * - Start the timers in all of our membership records
888 * that the query applies to for the interface on
889 * which the query arrived excl. those that belong
890 * to a "local" group (224.0.0.X)
891 * - For timers already running check if they need to
892 * be reset.
893 * - Use the igmp->igmp_code field as the maximum
894 * delay possible
896 read_lock(&in_dev->mc_list_lock);
897 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
898 int changed;
900 if (group && group != im->multiaddr)
901 continue;
902 if (im->multiaddr == IGMP_ALL_HOSTS)
903 continue;
904 spin_lock_bh(&im->lock);
905 if (im->tm_running)
906 im->gsquery = im->gsquery && mark;
907 else
908 im->gsquery = mark;
909 changed = !im->gsquery ||
910 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
911 spin_unlock_bh(&im->lock);
912 if (changed)
913 igmp_mod_timer(im, max_delay);
915 read_unlock(&in_dev->mc_list_lock);
918 int igmp_rcv(struct sk_buff *skb)
920 /* This basically follows the spec line by line -- see RFC1112 */
921 struct igmphdr *ih;
922 struct in_device *in_dev = in_dev_get(skb->dev);
923 int len = skb->len;
925 if (in_dev == NULL)
926 goto drop;
928 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
929 goto drop_ref;
931 switch (skb->ip_summed) {
932 case CHECKSUM_COMPLETE:
933 if (!csum_fold(skb->csum))
934 break;
935 /* fall through */
936 case CHECKSUM_NONE:
937 skb->csum = 0;
938 if (__skb_checksum_complete(skb))
939 goto drop_ref;
942 ih = igmp_hdr(skb);
943 switch (ih->type) {
944 case IGMP_HOST_MEMBERSHIP_QUERY:
945 igmp_heard_query(in_dev, skb, len);
946 break;
947 case IGMP_HOST_MEMBERSHIP_REPORT:
948 case IGMPV2_HOST_MEMBERSHIP_REPORT:
949 case IGMPV3_HOST_MEMBERSHIP_REPORT:
950 /* Is it our report looped back? */
951 if (skb_rtable(skb)->fl.iif == 0)
952 break;
953 /* don't rely on MC router hearing unicast reports */
954 if (skb->pkt_type == PACKET_MULTICAST ||
955 skb->pkt_type == PACKET_BROADCAST)
956 igmp_heard_report(in_dev, ih->group);
957 break;
958 case IGMP_PIM:
959 #ifdef CONFIG_IP_PIMSM_V1
960 in_dev_put(in_dev);
961 return pim_rcv_v1(skb);
962 #endif
963 case IGMP_DVMRP:
964 case IGMP_TRACE:
965 case IGMP_HOST_LEAVE_MESSAGE:
966 case IGMP_MTRACE:
967 case IGMP_MTRACE_RESP:
968 break;
969 default:
970 break;
973 drop_ref:
974 in_dev_put(in_dev);
975 drop:
976 kfree_skb(skb);
977 return 0;
980 #endif
984 * Add a filter to a device
987 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
989 char buf[MAX_ADDR_LEN];
990 struct net_device *dev = in_dev->dev;
992 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
993 We will get multicast token leakage, when IFF_MULTICAST
994 is changed. This check should be done in dev->set_multicast_list
995 routine. Something sort of:
996 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
997 --ANK
999 if (arp_mc_map(addr, buf, dev, 0) == 0)
1000 dev_mc_add(dev, buf, dev->addr_len, 0);
1004 * Remove a filter from a device
1007 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1009 char buf[MAX_ADDR_LEN];
1010 struct net_device *dev = in_dev->dev;
1012 if (arp_mc_map(addr, buf, dev, 0) == 0)
1013 dev_mc_delete(dev, buf, dev->addr_len, 0);
1016 #ifdef CONFIG_IP_MULTICAST
1018 * deleted ip_mc_list manipulation
1020 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1022 struct ip_mc_list *pmc;
1024 /* this is an "ip_mc_list" for convenience; only the fields below
1025 * are actually used. In particular, the refcnt and users are not
1026 * used for management of the delete list. Using the same structure
1027 * for deleted items allows change reports to use common code with
1028 * non-deleted or query-response MCA's.
1030 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1031 if (!pmc)
1032 return;
1033 spin_lock_bh(&im->lock);
1034 pmc->interface = im->interface;
1035 in_dev_hold(in_dev);
1036 pmc->multiaddr = im->multiaddr;
1037 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1038 IGMP_Unsolicited_Report_Count;
1039 pmc->sfmode = im->sfmode;
1040 if (pmc->sfmode == MCAST_INCLUDE) {
1041 struct ip_sf_list *psf;
1043 pmc->tomb = im->tomb;
1044 pmc->sources = im->sources;
1045 im->tomb = im->sources = NULL;
1046 for (psf=pmc->sources; psf; psf=psf->sf_next)
1047 psf->sf_crcount = pmc->crcount;
1049 spin_unlock_bh(&im->lock);
1051 spin_lock_bh(&in_dev->mc_tomb_lock);
1052 pmc->next = in_dev->mc_tomb;
1053 in_dev->mc_tomb = pmc;
1054 spin_unlock_bh(&in_dev->mc_tomb_lock);
1057 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1059 struct ip_mc_list *pmc, *pmc_prev;
1060 struct ip_sf_list *psf, *psf_next;
1062 spin_lock_bh(&in_dev->mc_tomb_lock);
1063 pmc_prev = NULL;
1064 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1065 if (pmc->multiaddr == multiaddr)
1066 break;
1067 pmc_prev = pmc;
1069 if (pmc) {
1070 if (pmc_prev)
1071 pmc_prev->next = pmc->next;
1072 else
1073 in_dev->mc_tomb = pmc->next;
1075 spin_unlock_bh(&in_dev->mc_tomb_lock);
1076 if (pmc) {
1077 for (psf=pmc->tomb; psf; psf=psf_next) {
1078 psf_next = psf->sf_next;
1079 kfree(psf);
1081 in_dev_put(pmc->interface);
1082 kfree(pmc);
1086 static void igmpv3_clear_delrec(struct in_device *in_dev)
1088 struct ip_mc_list *pmc, *nextpmc;
1090 spin_lock_bh(&in_dev->mc_tomb_lock);
1091 pmc = in_dev->mc_tomb;
1092 in_dev->mc_tomb = NULL;
1093 spin_unlock_bh(&in_dev->mc_tomb_lock);
1095 for (; pmc; pmc = nextpmc) {
1096 nextpmc = pmc->next;
1097 ip_mc_clear_src(pmc);
1098 in_dev_put(pmc->interface);
1099 kfree(pmc);
1101 /* clear dead sources, too */
1102 read_lock(&in_dev->mc_list_lock);
1103 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1104 struct ip_sf_list *psf, *psf_next;
1106 spin_lock_bh(&pmc->lock);
1107 psf = pmc->tomb;
1108 pmc->tomb = NULL;
1109 spin_unlock_bh(&pmc->lock);
1110 for (; psf; psf=psf_next) {
1111 psf_next = psf->sf_next;
1112 kfree(psf);
1115 read_unlock(&in_dev->mc_list_lock);
1117 #endif
1119 static void igmp_group_dropped(struct ip_mc_list *im)
1121 struct in_device *in_dev = im->interface;
1122 #ifdef CONFIG_IP_MULTICAST
1123 int reporter;
1124 #endif
1126 if (im->loaded) {
1127 im->loaded = 0;
1128 ip_mc_filter_del(in_dev, im->multiaddr);
1131 #ifdef CONFIG_IP_MULTICAST
1132 if (im->multiaddr == IGMP_ALL_HOSTS)
1133 return;
1135 reporter = im->reporter;
1136 igmp_stop_timer(im);
1138 if (!in_dev->dead) {
1139 if (IGMP_V1_SEEN(in_dev))
1140 goto done;
1141 if (IGMP_V2_SEEN(in_dev)) {
1142 if (reporter)
1143 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1144 goto done;
1146 /* IGMPv3 */
1147 igmpv3_add_delrec(in_dev, im);
1149 igmp_ifc_event(in_dev);
1151 done:
1152 #endif
1153 ip_mc_clear_src(im);
1156 static void igmp_group_added(struct ip_mc_list *im)
1158 struct in_device *in_dev = im->interface;
1160 if (im->loaded == 0) {
1161 im->loaded = 1;
1162 ip_mc_filter_add(in_dev, im->multiaddr);
1165 #ifdef CONFIG_IP_MULTICAST
1166 if (im->multiaddr == IGMP_ALL_HOSTS)
1167 return;
1169 if (in_dev->dead)
1170 return;
1171 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1172 spin_lock_bh(&im->lock);
1173 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1174 spin_unlock_bh(&im->lock);
1175 return;
1177 /* else, v3 */
1179 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1180 IGMP_Unsolicited_Report_Count;
1181 igmp_ifc_event(in_dev);
1182 #endif
1187 * Multicast list managers
1192 * A socket has joined a multicast group on device dev.
1195 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1197 struct ip_mc_list *im;
1199 ASSERT_RTNL();
1201 for (im=in_dev->mc_list; im; im=im->next) {
1202 if (im->multiaddr == addr) {
1203 im->users++;
1204 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1205 goto out;
1209 im = kmalloc(sizeof(*im), GFP_KERNEL);
1210 if (!im)
1211 goto out;
1213 im->users = 1;
1214 im->interface = in_dev;
1215 in_dev_hold(in_dev);
1216 im->multiaddr = addr;
1217 /* initial mode is (EX, empty) */
1218 im->sfmode = MCAST_EXCLUDE;
1219 im->sfcount[MCAST_INCLUDE] = 0;
1220 im->sfcount[MCAST_EXCLUDE] = 1;
1221 im->sources = NULL;
1222 im->tomb = NULL;
1223 im->crcount = 0;
1224 atomic_set(&im->refcnt, 1);
1225 spin_lock_init(&im->lock);
1226 #ifdef CONFIG_IP_MULTICAST
1227 im->tm_running = 0;
1228 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1229 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1230 im->reporter = 0;
1231 im->gsquery = 0;
1232 #endif
1233 im->loaded = 0;
1234 write_lock_bh(&in_dev->mc_list_lock);
1235 im->next = in_dev->mc_list;
1236 in_dev->mc_list = im;
1237 in_dev->mc_count++;
1238 write_unlock_bh(&in_dev->mc_list_lock);
1239 #ifdef CONFIG_IP_MULTICAST
1240 igmpv3_del_delrec(in_dev, im->multiaddr);
1241 #endif
1242 igmp_group_added(im);
1243 if (!in_dev->dead)
1244 ip_rt_multicast_event(in_dev);
1245 out:
1246 return;
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
1272 * A socket has left a multicast group on device dev
1275 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1277 struct ip_mc_list *i, **ip;
1279 ASSERT_RTNL();
1281 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1282 if (i->multiaddr == addr) {
1283 if (--i->users == 0) {
1284 write_lock_bh(&in_dev->mc_list_lock);
1285 *ip = i->next;
1286 in_dev->mc_count--;
1287 write_unlock_bh(&in_dev->mc_list_lock);
1288 igmp_group_dropped(i);
1290 if (!in_dev->dead)
1291 ip_rt_multicast_event(in_dev);
1293 ip_ma_put(i);
1294 return;
1296 break;
1301 /* Device changing type */
1303 void ip_mc_unmap(struct in_device *in_dev)
1305 struct ip_mc_list *i;
1307 ASSERT_RTNL();
1309 for (i = in_dev->mc_list; i; i = i->next)
1310 igmp_group_dropped(i);
1313 void ip_mc_remap(struct in_device *in_dev)
1315 struct ip_mc_list *i;
1317 ASSERT_RTNL();
1319 for (i = in_dev->mc_list; i; i = i->next)
1320 igmp_group_added(i);
1323 /* Device going down */
1325 void ip_mc_down(struct in_device *in_dev)
1327 struct ip_mc_list *i;
1329 ASSERT_RTNL();
1331 for (i=in_dev->mc_list; i; i=i->next)
1332 igmp_group_dropped(i);
1334 #ifdef CONFIG_IP_MULTICAST
1335 in_dev->mr_ifc_count = 0;
1336 if (del_timer(&in_dev->mr_ifc_timer))
1337 __in_dev_put(in_dev);
1338 in_dev->mr_gq_running = 0;
1339 if (del_timer(&in_dev->mr_gq_timer))
1340 __in_dev_put(in_dev);
1341 igmpv3_clear_delrec(in_dev);
1342 #endif
1344 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1347 void ip_mc_init_dev(struct in_device *in_dev)
1349 ASSERT_RTNL();
1351 in_dev->mc_tomb = NULL;
1352 #ifdef CONFIG_IP_MULTICAST
1353 in_dev->mr_gq_running = 0;
1354 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1355 (unsigned long)in_dev);
1356 in_dev->mr_ifc_count = 0;
1357 in_dev->mc_count = 0;
1358 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1359 (unsigned long)in_dev);
1360 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1361 #endif
1363 rwlock_init(&in_dev->mc_list_lock);
1364 spin_lock_init(&in_dev->mc_tomb_lock);
1367 /* Device going up */
1369 void ip_mc_up(struct in_device *in_dev)
1371 struct ip_mc_list *i;
1373 ASSERT_RTNL();
1375 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1377 for (i=in_dev->mc_list; i; i=i->next)
1378 igmp_group_added(i);
1382 * Device is about to be destroyed: clean up.
1385 void ip_mc_destroy_dev(struct in_device *in_dev)
1387 struct ip_mc_list *i;
1389 ASSERT_RTNL();
1391 /* Deactivate timers */
1392 ip_mc_down(in_dev);
1394 write_lock_bh(&in_dev->mc_list_lock);
1395 while ((i = in_dev->mc_list) != NULL) {
1396 in_dev->mc_list = i->next;
1397 in_dev->mc_count--;
1398 write_unlock_bh(&in_dev->mc_list_lock);
1399 igmp_group_dropped(i);
1400 ip_ma_put(i);
1402 write_lock_bh(&in_dev->mc_list_lock);
1404 write_unlock_bh(&in_dev->mc_list_lock);
1407 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1409 struct flowi fl = { .nl_u = { .ip4_u =
1410 { .daddr = imr->imr_multiaddr.s_addr } } };
1411 struct rtable *rt;
1412 struct net_device *dev = NULL;
1413 struct in_device *idev = NULL;
1415 if (imr->imr_ifindex) {
1416 idev = inetdev_by_index(net, imr->imr_ifindex);
1417 if (idev)
1418 __in_dev_put(idev);
1419 return idev;
1421 if (imr->imr_address.s_addr) {
1422 dev = ip_dev_find(net, imr->imr_address.s_addr);
1423 if (!dev)
1424 return NULL;
1425 dev_put(dev);
1428 if (!dev && !ip_route_output_key(net, &rt, &fl)) {
1429 dev = rt->u.dst.dev;
1430 ip_rt_put(rt);
1432 if (dev) {
1433 imr->imr_ifindex = dev->ifindex;
1434 idev = __in_dev_get_rtnl(dev);
1436 return idev;
1440 * Join a socket to a group
1442 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1443 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1446 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1447 __be32 *psfsrc)
1449 struct ip_sf_list *psf, *psf_prev;
1450 int rv = 0;
1452 psf_prev = NULL;
1453 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1454 if (psf->sf_inaddr == *psfsrc)
1455 break;
1456 psf_prev = psf;
1458 if (!psf || psf->sf_count[sfmode] == 0) {
1459 /* source filter not found, or count wrong => bug */
1460 return -ESRCH;
1462 psf->sf_count[sfmode]--;
1463 if (psf->sf_count[sfmode] == 0) {
1464 ip_rt_multicast_event(pmc->interface);
1466 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1467 #ifdef CONFIG_IP_MULTICAST
1468 struct in_device *in_dev = pmc->interface;
1469 #endif
1471 /* no more filters for this source */
1472 if (psf_prev)
1473 psf_prev->sf_next = psf->sf_next;
1474 else
1475 pmc->sources = psf->sf_next;
1476 #ifdef CONFIG_IP_MULTICAST
1477 if (psf->sf_oldin &&
1478 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1479 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1480 IGMP_Unsolicited_Report_Count;
1481 psf->sf_next = pmc->tomb;
1482 pmc->tomb = psf;
1483 rv = 1;
1484 } else
1485 #endif
1486 kfree(psf);
1488 return rv;
1491 #ifndef CONFIG_IP_MULTICAST
1492 #define igmp_ifc_event(x) do { } while (0)
1493 #endif
1495 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1496 int sfcount, __be32 *psfsrc, int delta)
1498 struct ip_mc_list *pmc;
1499 int changerec = 0;
1500 int i, err;
1502 if (!in_dev)
1503 return -ENODEV;
1504 read_lock(&in_dev->mc_list_lock);
1505 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1506 if (*pmca == pmc->multiaddr)
1507 break;
1509 if (!pmc) {
1510 /* MCA not found?? bug */
1511 read_unlock(&in_dev->mc_list_lock);
1512 return -ESRCH;
1514 spin_lock_bh(&pmc->lock);
1515 read_unlock(&in_dev->mc_list_lock);
1516 #ifdef CONFIG_IP_MULTICAST
1517 sf_markstate(pmc);
1518 #endif
1519 if (!delta) {
1520 err = -EINVAL;
1521 if (!pmc->sfcount[sfmode])
1522 goto out_unlock;
1523 pmc->sfcount[sfmode]--;
1525 err = 0;
1526 for (i=0; i<sfcount; i++) {
1527 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1529 changerec |= rv > 0;
1530 if (!err && rv < 0)
1531 err = rv;
1533 if (pmc->sfmode == MCAST_EXCLUDE &&
1534 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1535 pmc->sfcount[MCAST_INCLUDE]) {
1536 #ifdef CONFIG_IP_MULTICAST
1537 struct ip_sf_list *psf;
1538 #endif
1540 /* filter mode change */
1541 pmc->sfmode = MCAST_INCLUDE;
1542 #ifdef CONFIG_IP_MULTICAST
1543 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1544 IGMP_Unsolicited_Report_Count;
1545 in_dev->mr_ifc_count = pmc->crcount;
1546 for (psf=pmc->sources; psf; psf = psf->sf_next)
1547 psf->sf_crcount = 0;
1548 igmp_ifc_event(pmc->interface);
1549 } else if (sf_setstate(pmc) || changerec) {
1550 igmp_ifc_event(pmc->interface);
1551 #endif
1553 out_unlock:
1554 spin_unlock_bh(&pmc->lock);
1555 return err;
1559 * Add multicast single-source filter to the interface list
1561 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1562 __be32 *psfsrc, int delta)
1564 struct ip_sf_list *psf, *psf_prev;
1566 psf_prev = NULL;
1567 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1568 if (psf->sf_inaddr == *psfsrc)
1569 break;
1570 psf_prev = psf;
1572 if (!psf) {
1573 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1574 if (!psf)
1575 return -ENOBUFS;
1576 psf->sf_inaddr = *psfsrc;
1577 if (psf_prev) {
1578 psf_prev->sf_next = psf;
1579 } else
1580 pmc->sources = psf;
1582 psf->sf_count[sfmode]++;
1583 if (psf->sf_count[sfmode] == 1) {
1584 ip_rt_multicast_event(pmc->interface);
1586 return 0;
1589 #ifdef CONFIG_IP_MULTICAST
1590 static void sf_markstate(struct ip_mc_list *pmc)
1592 struct ip_sf_list *psf;
1593 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1595 for (psf=pmc->sources; psf; psf=psf->sf_next)
1596 if (pmc->sfcount[MCAST_EXCLUDE]) {
1597 psf->sf_oldin = mca_xcount ==
1598 psf->sf_count[MCAST_EXCLUDE] &&
1599 !psf->sf_count[MCAST_INCLUDE];
1600 } else
1601 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1604 static int sf_setstate(struct ip_mc_list *pmc)
1606 struct ip_sf_list *psf, *dpsf;
1607 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1608 int qrv = pmc->interface->mr_qrv;
1609 int new_in, rv;
1611 rv = 0;
1612 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1613 if (pmc->sfcount[MCAST_EXCLUDE]) {
1614 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1615 !psf->sf_count[MCAST_INCLUDE];
1616 } else
1617 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1618 if (new_in) {
1619 if (!psf->sf_oldin) {
1620 struct ip_sf_list *prev = NULL;
1622 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1623 if (dpsf->sf_inaddr == psf->sf_inaddr)
1624 break;
1625 prev = dpsf;
1627 if (dpsf) {
1628 if (prev)
1629 prev->sf_next = dpsf->sf_next;
1630 else
1631 pmc->tomb = dpsf->sf_next;
1632 kfree(dpsf);
1634 psf->sf_crcount = qrv;
1635 rv++;
1637 } else if (psf->sf_oldin) {
1639 psf->sf_crcount = 0;
1641 * add or update "delete" records if an active filter
1642 * is now inactive
1644 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1645 if (dpsf->sf_inaddr == psf->sf_inaddr)
1646 break;
1647 if (!dpsf) {
1648 dpsf = (struct ip_sf_list *)
1649 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1650 if (!dpsf)
1651 continue;
1652 *dpsf = *psf;
1653 /* pmc->lock held by callers */
1654 dpsf->sf_next = pmc->tomb;
1655 pmc->tomb = dpsf;
1657 dpsf->sf_crcount = qrv;
1658 rv++;
1661 return rv;
1663 #endif
1666 * Add multicast source filter list to the interface list
1668 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1669 int sfcount, __be32 *psfsrc, int delta)
1671 struct ip_mc_list *pmc;
1672 int isexclude;
1673 int i, err;
1675 if (!in_dev)
1676 return -ENODEV;
1677 read_lock(&in_dev->mc_list_lock);
1678 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1679 if (*pmca == pmc->multiaddr)
1680 break;
1682 if (!pmc) {
1683 /* MCA not found?? bug */
1684 read_unlock(&in_dev->mc_list_lock);
1685 return -ESRCH;
1687 spin_lock_bh(&pmc->lock);
1688 read_unlock(&in_dev->mc_list_lock);
1690 #ifdef CONFIG_IP_MULTICAST
1691 sf_markstate(pmc);
1692 #endif
1693 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1694 if (!delta)
1695 pmc->sfcount[sfmode]++;
1696 err = 0;
1697 for (i=0; i<sfcount; i++) {
1698 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1699 if (err)
1700 break;
1702 if (err) {
1703 int j;
1705 pmc->sfcount[sfmode]--;
1706 for (j=0; j<i; j++)
1707 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1708 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1709 #ifdef CONFIG_IP_MULTICAST
1710 struct ip_sf_list *psf;
1711 in_dev = pmc->interface;
1712 #endif
1714 /* filter mode change */
1715 if (pmc->sfcount[MCAST_EXCLUDE])
1716 pmc->sfmode = MCAST_EXCLUDE;
1717 else if (pmc->sfcount[MCAST_INCLUDE])
1718 pmc->sfmode = MCAST_INCLUDE;
1719 #ifdef CONFIG_IP_MULTICAST
1720 /* else no filters; keep old mode for reports */
1722 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1723 IGMP_Unsolicited_Report_Count;
1724 in_dev->mr_ifc_count = pmc->crcount;
1725 for (psf=pmc->sources; psf; psf = psf->sf_next)
1726 psf->sf_crcount = 0;
1727 igmp_ifc_event(in_dev);
1728 } else if (sf_setstate(pmc)) {
1729 igmp_ifc_event(in_dev);
1730 #endif
1732 spin_unlock_bh(&pmc->lock);
1733 return err;
1736 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1738 struct ip_sf_list *psf, *nextpsf;
1740 for (psf=pmc->tomb; psf; psf=nextpsf) {
1741 nextpsf = psf->sf_next;
1742 kfree(psf);
1744 pmc->tomb = NULL;
1745 for (psf=pmc->sources; psf; psf=nextpsf) {
1746 nextpsf = psf->sf_next;
1747 kfree(psf);
1749 pmc->sources = NULL;
1750 pmc->sfmode = MCAST_EXCLUDE;
1751 pmc->sfcount[MCAST_INCLUDE] = 0;
1752 pmc->sfcount[MCAST_EXCLUDE] = 1;
1757 * Join a multicast group
1759 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1761 int err;
1762 __be32 addr = imr->imr_multiaddr.s_addr;
1763 struct ip_mc_socklist *iml = NULL, *i;
1764 struct in_device *in_dev;
1765 struct inet_sock *inet = inet_sk(sk);
1766 struct net *net = sock_net(sk);
1767 int ifindex;
1768 int count = 0;
1770 if (!ipv4_is_multicast(addr))
1771 return -EINVAL;
1773 rtnl_lock();
1775 in_dev = ip_mc_find_dev(net, imr);
1777 if (!in_dev) {
1778 iml = NULL;
1779 err = -ENODEV;
1780 goto done;
1783 err = -EADDRINUSE;
1784 ifindex = imr->imr_ifindex;
1785 for (i = inet->mc_list; i; i = i->next) {
1786 if (i->multi.imr_multiaddr.s_addr == addr &&
1787 i->multi.imr_ifindex == ifindex)
1788 goto done;
1789 count++;
1791 err = -ENOBUFS;
1792 if (count >= sysctl_igmp_max_memberships)
1793 goto done;
1794 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1795 if (iml == NULL)
1796 goto done;
1798 memcpy(&iml->multi, imr, sizeof(*imr));
1799 iml->next = inet->mc_list;
1800 iml->sflist = NULL;
1801 iml->sfmode = MCAST_EXCLUDE;
1802 inet->mc_list = iml;
1803 ip_mc_inc_group(in_dev, addr);
1804 err = 0;
1805 done:
1806 rtnl_unlock();
1807 return err;
1810 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1811 struct in_device *in_dev)
1813 int err;
1815 if (iml->sflist == NULL) {
1816 /* any-source empty exclude case */
1817 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1818 iml->sfmode, 0, NULL, 0);
1820 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1821 iml->sfmode, iml->sflist->sl_count,
1822 iml->sflist->sl_addr, 0);
1823 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1824 iml->sflist = NULL;
1825 return err;
1829 * Ask a socket to leave a group.
1832 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1834 struct inet_sock *inet = inet_sk(sk);
1835 struct ip_mc_socklist *iml, **imlp;
1836 struct in_device *in_dev;
1837 struct net *net = sock_net(sk);
1838 __be32 group = imr->imr_multiaddr.s_addr;
1839 u32 ifindex;
1840 int ret = -EADDRNOTAVAIL;
1842 rtnl_lock();
1843 in_dev = ip_mc_find_dev(net, imr);
1844 ifindex = imr->imr_ifindex;
1845 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1846 if (iml->multi.imr_multiaddr.s_addr != group)
1847 continue;
1848 if (ifindex) {
1849 if (iml->multi.imr_ifindex != ifindex)
1850 continue;
1851 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1852 iml->multi.imr_address.s_addr)
1853 continue;
1855 (void) ip_mc_leave_src(sk, iml, in_dev);
1857 *imlp = iml->next;
1859 if (in_dev)
1860 ip_mc_dec_group(in_dev, group);
1861 rtnl_unlock();
1862 sock_kfree_s(sk, iml, sizeof(*iml));
1863 return 0;
1865 if (!in_dev)
1866 ret = -ENODEV;
1867 rtnl_unlock();
1868 return ret;
1871 int ip_mc_source(int add, int omode, struct sock *sk, struct
1872 ip_mreq_source *mreqs, int ifindex)
1874 int err;
1875 struct ip_mreqn imr;
1876 __be32 addr = mreqs->imr_multiaddr;
1877 struct ip_mc_socklist *pmc;
1878 struct in_device *in_dev = NULL;
1879 struct inet_sock *inet = inet_sk(sk);
1880 struct ip_sf_socklist *psl;
1881 struct net *net = sock_net(sk);
1882 int leavegroup = 0;
1883 int i, j, rv;
1885 if (!ipv4_is_multicast(addr))
1886 return -EINVAL;
1888 rtnl_lock();
1890 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1891 imr.imr_address.s_addr = mreqs->imr_interface;
1892 imr.imr_ifindex = ifindex;
1893 in_dev = ip_mc_find_dev(net, &imr);
1895 if (!in_dev) {
1896 err = -ENODEV;
1897 goto done;
1899 err = -EADDRNOTAVAIL;
1901 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1902 if ((pmc->multi.imr_multiaddr.s_addr ==
1903 imr.imr_multiaddr.s_addr) &&
1904 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1905 break;
1907 if (!pmc) { /* must have a prior join */
1908 err = -EINVAL;
1909 goto done;
1911 /* if a source filter was set, must be the same mode as before */
1912 if (pmc->sflist) {
1913 if (pmc->sfmode != omode) {
1914 err = -EINVAL;
1915 goto done;
1917 } else if (pmc->sfmode != omode) {
1918 /* allow mode switches for empty-set filters */
1919 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1920 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1921 NULL, 0);
1922 pmc->sfmode = omode;
1925 psl = pmc->sflist;
1926 if (!add) {
1927 if (!psl)
1928 goto done; /* err = -EADDRNOTAVAIL */
1929 rv = !0;
1930 for (i=0; i<psl->sl_count; i++) {
1931 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1932 sizeof(__be32));
1933 if (rv == 0)
1934 break;
1936 if (rv) /* source not found */
1937 goto done; /* err = -EADDRNOTAVAIL */
1939 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1940 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1941 leavegroup = 1;
1942 goto done;
1945 /* update the interface filter */
1946 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1947 &mreqs->imr_sourceaddr, 1);
1949 for (j=i+1; j<psl->sl_count; j++)
1950 psl->sl_addr[j-1] = psl->sl_addr[j];
1951 psl->sl_count--;
1952 err = 0;
1953 goto done;
1955 /* else, add a new source to the filter */
1957 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1958 err = -ENOBUFS;
1959 goto done;
1961 if (!psl || psl->sl_count == psl->sl_max) {
1962 struct ip_sf_socklist *newpsl;
1963 int count = IP_SFBLOCK;
1965 if (psl)
1966 count += psl->sl_max;
1967 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1968 if (!newpsl) {
1969 err = -ENOBUFS;
1970 goto done;
1972 newpsl->sl_max = count;
1973 newpsl->sl_count = count - IP_SFBLOCK;
1974 if (psl) {
1975 for (i=0; i<psl->sl_count; i++)
1976 newpsl->sl_addr[i] = psl->sl_addr[i];
1977 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1979 pmc->sflist = psl = newpsl;
1981 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1982 for (i=0; i<psl->sl_count; i++) {
1983 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1984 sizeof(__be32));
1985 if (rv == 0)
1986 break;
1988 if (rv == 0) /* address already there is an error */
1989 goto done;
1990 for (j=psl->sl_count-1; j>=i; j--)
1991 psl->sl_addr[j+1] = psl->sl_addr[j];
1992 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1993 psl->sl_count++;
1994 err = 0;
1995 /* update the interface list */
1996 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1997 &mreqs->imr_sourceaddr, 1);
1998 done:
1999 rtnl_unlock();
2000 if (leavegroup)
2001 return ip_mc_leave_group(sk, &imr);
2002 return err;
2005 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2007 int err = 0;
2008 struct ip_mreqn imr;
2009 __be32 addr = msf->imsf_multiaddr;
2010 struct ip_mc_socklist *pmc;
2011 struct in_device *in_dev;
2012 struct inet_sock *inet = inet_sk(sk);
2013 struct ip_sf_socklist *newpsl, *psl;
2014 struct net *net = sock_net(sk);
2015 int leavegroup = 0;
2017 if (!ipv4_is_multicast(addr))
2018 return -EINVAL;
2019 if (msf->imsf_fmode != MCAST_INCLUDE &&
2020 msf->imsf_fmode != MCAST_EXCLUDE)
2021 return -EINVAL;
2023 rtnl_lock();
2025 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2026 imr.imr_address.s_addr = msf->imsf_interface;
2027 imr.imr_ifindex = ifindex;
2028 in_dev = ip_mc_find_dev(net, &imr);
2030 if (!in_dev) {
2031 err = -ENODEV;
2032 goto done;
2035 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2036 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2037 leavegroup = 1;
2038 goto done;
2041 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2042 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2043 pmc->multi.imr_ifindex == imr.imr_ifindex)
2044 break;
2046 if (!pmc) { /* must have a prior join */
2047 err = -EINVAL;
2048 goto done;
2050 if (msf->imsf_numsrc) {
2051 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2052 GFP_KERNEL);
2053 if (!newpsl) {
2054 err = -ENOBUFS;
2055 goto done;
2057 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2058 memcpy(newpsl->sl_addr, msf->imsf_slist,
2059 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2060 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2061 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2062 if (err) {
2063 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2064 goto done;
2066 } else {
2067 newpsl = NULL;
2068 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2069 msf->imsf_fmode, 0, NULL, 0);
2071 psl = pmc->sflist;
2072 if (psl) {
2073 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2074 psl->sl_count, psl->sl_addr, 0);
2075 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2076 } else
2077 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2078 0, NULL, 0);
2079 pmc->sflist = newpsl;
2080 pmc->sfmode = msf->imsf_fmode;
2081 err = 0;
2082 done:
2083 rtnl_unlock();
2084 if (leavegroup)
2085 err = ip_mc_leave_group(sk, &imr);
2086 return err;
2089 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2090 struct ip_msfilter __user *optval, int __user *optlen)
2092 int err, len, count, copycount;
2093 struct ip_mreqn imr;
2094 __be32 addr = msf->imsf_multiaddr;
2095 struct ip_mc_socklist *pmc;
2096 struct in_device *in_dev;
2097 struct inet_sock *inet = inet_sk(sk);
2098 struct ip_sf_socklist *psl;
2099 struct net *net = sock_net(sk);
2101 if (!ipv4_is_multicast(addr))
2102 return -EINVAL;
2104 rtnl_lock();
2106 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2107 imr.imr_address.s_addr = msf->imsf_interface;
2108 imr.imr_ifindex = 0;
2109 in_dev = ip_mc_find_dev(net, &imr);
2111 if (!in_dev) {
2112 err = -ENODEV;
2113 goto done;
2115 err = -EADDRNOTAVAIL;
2117 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2118 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2119 pmc->multi.imr_ifindex == imr.imr_ifindex)
2120 break;
2122 if (!pmc) /* must have a prior join */
2123 goto done;
2124 msf->imsf_fmode = pmc->sfmode;
2125 psl = pmc->sflist;
2126 rtnl_unlock();
2127 if (!psl) {
2128 len = 0;
2129 count = 0;
2130 } else {
2131 count = psl->sl_count;
2133 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2134 len = copycount * sizeof(psl->sl_addr[0]);
2135 msf->imsf_numsrc = count;
2136 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2137 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2138 return -EFAULT;
2140 if (len &&
2141 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2142 return -EFAULT;
2143 return 0;
2144 done:
2145 rtnl_unlock();
2146 return err;
2149 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2150 struct group_filter __user *optval, int __user *optlen)
2152 int err, i, count, copycount;
2153 struct sockaddr_in *psin;
2154 __be32 addr;
2155 struct ip_mc_socklist *pmc;
2156 struct inet_sock *inet = inet_sk(sk);
2157 struct ip_sf_socklist *psl;
2159 psin = (struct sockaddr_in *)&gsf->gf_group;
2160 if (psin->sin_family != AF_INET)
2161 return -EINVAL;
2162 addr = psin->sin_addr.s_addr;
2163 if (!ipv4_is_multicast(addr))
2164 return -EINVAL;
2166 rtnl_lock();
2168 err = -EADDRNOTAVAIL;
2170 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2171 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2172 pmc->multi.imr_ifindex == gsf->gf_interface)
2173 break;
2175 if (!pmc) /* must have a prior join */
2176 goto done;
2177 gsf->gf_fmode = pmc->sfmode;
2178 psl = pmc->sflist;
2179 rtnl_unlock();
2180 count = psl ? psl->sl_count : 0;
2181 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2182 gsf->gf_numsrc = count;
2183 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2184 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2185 return -EFAULT;
2187 for (i=0; i<copycount; i++) {
2188 struct sockaddr_storage ss;
2190 psin = (struct sockaddr_in *)&ss;
2191 memset(&ss, 0, sizeof(ss));
2192 psin->sin_family = AF_INET;
2193 psin->sin_addr.s_addr = psl->sl_addr[i];
2194 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2195 return -EFAULT;
2197 return 0;
2198 done:
2199 rtnl_unlock();
2200 return err;
2204 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2206 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2208 struct inet_sock *inet = inet_sk(sk);
2209 struct ip_mc_socklist *pmc;
2210 struct ip_sf_socklist *psl;
2211 int i;
2213 if (!ipv4_is_multicast(loc_addr))
2214 return 1;
2216 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2217 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2218 pmc->multi.imr_ifindex == dif)
2219 break;
2221 if (!pmc)
2222 return inet->mc_all;
2223 psl = pmc->sflist;
2224 if (!psl)
2225 return pmc->sfmode == MCAST_EXCLUDE;
2227 for (i=0; i<psl->sl_count; i++) {
2228 if (psl->sl_addr[i] == rmt_addr)
2229 break;
2231 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2232 return 0;
2233 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2234 return 0;
2235 return 1;
2239 * A socket is closing.
2242 void ip_mc_drop_socket(struct sock *sk)
2244 struct inet_sock *inet = inet_sk(sk);
2245 struct ip_mc_socklist *iml;
2246 struct net *net = sock_net(sk);
2248 if (inet->mc_list == NULL)
2249 return;
2251 rtnl_lock();
2252 while ((iml = inet->mc_list) != NULL) {
2253 struct in_device *in_dev;
2254 inet->mc_list = iml->next;
2256 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2257 (void) ip_mc_leave_src(sk, iml, in_dev);
2258 if (in_dev != NULL) {
2259 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2260 in_dev_put(in_dev);
2262 sock_kfree_s(sk, iml, sizeof(*iml));
2264 rtnl_unlock();
2267 int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2269 struct ip_mc_list *im;
2270 struct ip_sf_list *psf;
2271 int rv = 0;
2273 read_lock(&in_dev->mc_list_lock);
2274 for (im=in_dev->mc_list; im; im=im->next) {
2275 if (im->multiaddr == mc_addr)
2276 break;
2278 if (im && proto == IPPROTO_IGMP) {
2279 rv = 1;
2280 } else if (im) {
2281 if (src_addr) {
2282 for (psf=im->sources; psf; psf=psf->sf_next) {
2283 if (psf->sf_inaddr == src_addr)
2284 break;
2286 if (psf)
2287 rv = psf->sf_count[MCAST_INCLUDE] ||
2288 psf->sf_count[MCAST_EXCLUDE] !=
2289 im->sfcount[MCAST_EXCLUDE];
2290 else
2291 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2292 } else
2293 rv = 1; /* unspecified source; tentatively allow */
2295 read_unlock(&in_dev->mc_list_lock);
2296 return rv;
2299 #if defined(CONFIG_PROC_FS)
2300 struct igmp_mc_iter_state {
2301 struct seq_net_private p;
2302 struct net_device *dev;
2303 struct in_device *in_dev;
2306 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2308 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2310 struct net *net = seq_file_net(seq);
2311 struct ip_mc_list *im = NULL;
2312 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2314 state->in_dev = NULL;
2315 for_each_netdev_rcu(net, state->dev) {
2316 struct in_device *in_dev;
2318 in_dev = __in_dev_get_rcu(state->dev);
2319 if (!in_dev)
2320 continue;
2321 read_lock(&in_dev->mc_list_lock);
2322 im = in_dev->mc_list;
2323 if (im) {
2324 state->in_dev = in_dev;
2325 break;
2327 read_unlock(&in_dev->mc_list_lock);
2329 return im;
2332 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2334 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2335 im = im->next;
2336 while (!im) {
2337 if (likely(state->in_dev != NULL))
2338 read_unlock(&state->in_dev->mc_list_lock);
2340 state->dev = next_net_device_rcu(state->dev);
2341 if (!state->dev) {
2342 state->in_dev = NULL;
2343 break;
2345 state->in_dev = __in_dev_get_rcu(state->dev);
2346 if (!state->in_dev)
2347 continue;
2348 read_lock(&state->in_dev->mc_list_lock);
2349 im = state->in_dev->mc_list;
2351 return im;
2354 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2356 struct ip_mc_list *im = igmp_mc_get_first(seq);
2357 if (im)
2358 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2359 --pos;
2360 return pos ? NULL : im;
2363 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2364 __acquires(rcu)
2366 rcu_read_lock();
2367 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2370 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2372 struct ip_mc_list *im;
2373 if (v == SEQ_START_TOKEN)
2374 im = igmp_mc_get_first(seq);
2375 else
2376 im = igmp_mc_get_next(seq, v);
2377 ++*pos;
2378 return im;
2381 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2382 __releases(rcu)
2384 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2385 if (likely(state->in_dev != NULL)) {
2386 read_unlock(&state->in_dev->mc_list_lock);
2387 state->in_dev = NULL;
2389 state->dev = NULL;
2390 rcu_read_unlock();
2393 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2395 if (v == SEQ_START_TOKEN)
2396 seq_puts(seq,
2397 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2398 else {
2399 struct ip_mc_list *im = (struct ip_mc_list *)v;
2400 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2401 char *querier;
2402 #ifdef CONFIG_IP_MULTICAST
2403 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2404 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2405 "V3";
2406 #else
2407 querier = "NONE";
2408 #endif
2410 if (state->in_dev->mc_list == im) {
2411 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2412 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2415 seq_printf(seq,
2416 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2417 im->multiaddr, im->users,
2418 im->tm_running, im->tm_running ?
2419 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2420 im->reporter);
2422 return 0;
2425 static const struct seq_operations igmp_mc_seq_ops = {
2426 .start = igmp_mc_seq_start,
2427 .next = igmp_mc_seq_next,
2428 .stop = igmp_mc_seq_stop,
2429 .show = igmp_mc_seq_show,
2432 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2434 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2435 sizeof(struct igmp_mc_iter_state));
2438 static const struct file_operations igmp_mc_seq_fops = {
2439 .owner = THIS_MODULE,
2440 .open = igmp_mc_seq_open,
2441 .read = seq_read,
2442 .llseek = seq_lseek,
2443 .release = seq_release_net,
2446 struct igmp_mcf_iter_state {
2447 struct seq_net_private p;
2448 struct net_device *dev;
2449 struct in_device *idev;
2450 struct ip_mc_list *im;
2453 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2455 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2457 struct net *net = seq_file_net(seq);
2458 struct ip_sf_list *psf = NULL;
2459 struct ip_mc_list *im = NULL;
2460 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2462 state->idev = NULL;
2463 state->im = NULL;
2464 for_each_netdev_rcu(net, state->dev) {
2465 struct in_device *idev;
2466 idev = __in_dev_get_rcu(state->dev);
2467 if (unlikely(idev == NULL))
2468 continue;
2469 read_lock(&idev->mc_list_lock);
2470 im = idev->mc_list;
2471 if (likely(im != NULL)) {
2472 spin_lock_bh(&im->lock);
2473 psf = im->sources;
2474 if (likely(psf != NULL)) {
2475 state->im = im;
2476 state->idev = idev;
2477 break;
2479 spin_unlock_bh(&im->lock);
2481 read_unlock(&idev->mc_list_lock);
2483 return psf;
2486 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2488 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2490 psf = psf->sf_next;
2491 while (!psf) {
2492 spin_unlock_bh(&state->im->lock);
2493 state->im = state->im->next;
2494 while (!state->im) {
2495 if (likely(state->idev != NULL))
2496 read_unlock(&state->idev->mc_list_lock);
2498 state->dev = next_net_device_rcu(state->dev);
2499 if (!state->dev) {
2500 state->idev = NULL;
2501 goto out;
2503 state->idev = __in_dev_get_rcu(state->dev);
2504 if (!state->idev)
2505 continue;
2506 read_lock(&state->idev->mc_list_lock);
2507 state->im = state->idev->mc_list;
2509 if (!state->im)
2510 break;
2511 spin_lock_bh(&state->im->lock);
2512 psf = state->im->sources;
2514 out:
2515 return psf;
2518 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2520 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2521 if (psf)
2522 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2523 --pos;
2524 return pos ? NULL : psf;
2527 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2528 __acquires(rcu)
2530 rcu_read_lock();
2531 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2534 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2536 struct ip_sf_list *psf;
2537 if (v == SEQ_START_TOKEN)
2538 psf = igmp_mcf_get_first(seq);
2539 else
2540 psf = igmp_mcf_get_next(seq, v);
2541 ++*pos;
2542 return psf;
2545 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2546 __releases(rcu)
2548 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2549 if (likely(state->im != NULL)) {
2550 spin_unlock_bh(&state->im->lock);
2551 state->im = NULL;
2553 if (likely(state->idev != NULL)) {
2554 read_unlock(&state->idev->mc_list_lock);
2555 state->idev = NULL;
2557 state->dev = NULL;
2558 rcu_read_unlock();
2561 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2563 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2564 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2566 if (v == SEQ_START_TOKEN) {
2567 seq_printf(seq,
2568 "%3s %6s "
2569 "%10s %10s %6s %6s\n", "Idx",
2570 "Device", "MCA",
2571 "SRC", "INC", "EXC");
2572 } else {
2573 seq_printf(seq,
2574 "%3d %6.6s 0x%08x "
2575 "0x%08x %6lu %6lu\n",
2576 state->dev->ifindex, state->dev->name,
2577 ntohl(state->im->multiaddr),
2578 ntohl(psf->sf_inaddr),
2579 psf->sf_count[MCAST_INCLUDE],
2580 psf->sf_count[MCAST_EXCLUDE]);
2582 return 0;
2585 static const struct seq_operations igmp_mcf_seq_ops = {
2586 .start = igmp_mcf_seq_start,
2587 .next = igmp_mcf_seq_next,
2588 .stop = igmp_mcf_seq_stop,
2589 .show = igmp_mcf_seq_show,
2592 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2594 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2595 sizeof(struct igmp_mcf_iter_state));
2598 static const struct file_operations igmp_mcf_seq_fops = {
2599 .owner = THIS_MODULE,
2600 .open = igmp_mcf_seq_open,
2601 .read = seq_read,
2602 .llseek = seq_lseek,
2603 .release = seq_release_net,
2606 static int igmp_net_init(struct net *net)
2608 struct proc_dir_entry *pde;
2610 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2611 if (!pde)
2612 goto out_igmp;
2613 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2614 if (!pde)
2615 goto out_mcfilter;
2616 return 0;
2618 out_mcfilter:
2619 proc_net_remove(net, "igmp");
2620 out_igmp:
2621 return -ENOMEM;
2624 static void igmp_net_exit(struct net *net)
2626 proc_net_remove(net, "mcfilter");
2627 proc_net_remove(net, "igmp");
2630 static struct pernet_operations igmp_net_ops = {
2631 .init = igmp_net_init,
2632 .exit = igmp_net_exit,
2635 int __init igmp_mc_proc_init(void)
2637 return register_pernet_subsys(&igmp_net_ops);
2639 #endif
2641 EXPORT_SYMBOL(ip_mc_dec_group);
2642 EXPORT_SYMBOL(ip_mc_inc_group);
2643 EXPORT_SYMBOL(ip_mc_join_group);
2644 EXPORT_SYMBOL(ip_mc_rejoin_group);