HID: microsoft: do not use compound literal - fix build
[linux/fpc-iii.git] / net / ipv4 / igmp.c
blobc8989a71bbd9a227febcc568de2d1323947731bb
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_rcu(im, rcu);
160 #define for_each_pmc_rcu(in_dev, pmc) \
161 for (pmc = rcu_dereference(in_dev->mc_list); \
162 pmc != NULL; \
163 pmc = rcu_dereference(pmc->next_rcu))
165 #define for_each_pmc_rtnl(in_dev, pmc) \
166 for (pmc = rtnl_dereference(in_dev->mc_list); \
167 pmc != NULL; \
168 pmc = rtnl_dereference(pmc->next_rcu))
170 #ifdef CONFIG_IP_MULTICAST
173 * Timer management
176 static void igmp_stop_timer(struct ip_mc_list *im)
178 spin_lock_bh(&im->lock);
179 if (del_timer(&im->timer))
180 atomic_dec(&im->refcnt);
181 im->tm_running = 0;
182 im->reporter = 0;
183 im->unsolicit_count = 0;
184 spin_unlock_bh(&im->lock);
187 /* It must be called with locked im->lock */
188 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
190 int tv = net_random() % max_delay;
192 im->tm_running = 1;
193 if (!mod_timer(&im->timer, jiffies+tv+2))
194 atomic_inc(&im->refcnt);
197 static void igmp_gq_start_timer(struct in_device *in_dev)
199 int tv = net_random() % in_dev->mr_maxdelay;
201 in_dev->mr_gq_running = 1;
202 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
203 in_dev_hold(in_dev);
206 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
208 int tv = net_random() % delay;
210 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
211 in_dev_hold(in_dev);
214 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
216 spin_lock_bh(&im->lock);
217 im->unsolicit_count = 0;
218 if (del_timer(&im->timer)) {
219 if ((long)(im->timer.expires-jiffies) < max_delay) {
220 add_timer(&im->timer);
221 im->tm_running = 1;
222 spin_unlock_bh(&im->lock);
223 return;
225 atomic_dec(&im->refcnt);
227 igmp_start_timer(im, max_delay);
228 spin_unlock_bh(&im->lock);
233 * Send an IGMP report.
236 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
239 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
240 int gdeleted, int sdeleted)
242 switch (type) {
243 case IGMPV3_MODE_IS_INCLUDE:
244 case IGMPV3_MODE_IS_EXCLUDE:
245 if (gdeleted || sdeleted)
246 return 0;
247 if (!(pmc->gsquery && !psf->sf_gsresp)) {
248 if (pmc->sfmode == MCAST_INCLUDE)
249 return 1;
250 /* don't include if this source is excluded
251 * in all filters
253 if (psf->sf_count[MCAST_INCLUDE])
254 return type == IGMPV3_MODE_IS_INCLUDE;
255 return pmc->sfcount[MCAST_EXCLUDE] ==
256 psf->sf_count[MCAST_EXCLUDE];
258 return 0;
259 case IGMPV3_CHANGE_TO_INCLUDE:
260 if (gdeleted || sdeleted)
261 return 0;
262 return psf->sf_count[MCAST_INCLUDE] != 0;
263 case IGMPV3_CHANGE_TO_EXCLUDE:
264 if (gdeleted || sdeleted)
265 return 0;
266 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
267 psf->sf_count[MCAST_INCLUDE])
268 return 0;
269 return pmc->sfcount[MCAST_EXCLUDE] ==
270 psf->sf_count[MCAST_EXCLUDE];
271 case IGMPV3_ALLOW_NEW_SOURCES:
272 if (gdeleted || !psf->sf_crcount)
273 return 0;
274 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
275 case IGMPV3_BLOCK_OLD_SOURCES:
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return gdeleted || (psf->sf_crcount && sdeleted);
278 return psf->sf_crcount && !gdeleted && !sdeleted;
280 return 0;
283 static int
284 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
286 struct ip_sf_list *psf;
287 int scount = 0;
289 for (psf=pmc->sources; psf; psf=psf->sf_next) {
290 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
291 continue;
292 scount++;
294 return scount;
297 #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
299 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
301 struct sk_buff *skb;
302 struct rtable *rt;
303 struct iphdr *pip;
304 struct igmpv3_report *pig;
305 struct net *net = dev_net(dev);
306 struct flowi4 fl4;
308 while (1) {
309 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
310 GFP_ATOMIC | __GFP_NOWARN);
311 if (skb)
312 break;
313 size >>= 1;
314 if (size < 256)
315 return NULL;
317 igmp_skb_size(skb) = size;
319 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
320 0, 0,
321 IPPROTO_IGMP, 0, dev->ifindex);
322 if (IS_ERR(rt)) {
323 kfree_skb(skb);
324 return NULL;
327 skb_dst_set(skb, &rt->dst);
328 skb->dev = dev;
330 skb_reserve(skb, LL_RESERVED_SPACE(dev));
332 skb_reset_network_header(skb);
333 pip = ip_hdr(skb);
334 skb_put(skb, sizeof(struct iphdr) + 4);
336 pip->version = 4;
337 pip->ihl = (sizeof(struct iphdr)+4)>>2;
338 pip->tos = 0xc0;
339 pip->frag_off = htons(IP_DF);
340 pip->ttl = 1;
341 pip->daddr = fl4.daddr;
342 pip->saddr = fl4.saddr;
343 pip->protocol = IPPROTO_IGMP;
344 pip->tot_len = 0; /* filled in later */
345 ip_select_ident(pip, &rt->dst, NULL);
346 ((u8*)&pip[1])[0] = IPOPT_RA;
347 ((u8*)&pip[1])[1] = 4;
348 ((u8*)&pip[1])[2] = 0;
349 ((u8*)&pip[1])[3] = 0;
351 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
352 skb_put(skb, sizeof(*pig));
353 pig = igmpv3_report_hdr(skb);
354 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
355 pig->resv1 = 0;
356 pig->csum = 0;
357 pig->resv2 = 0;
358 pig->ngrec = 0;
359 return skb;
362 static int igmpv3_sendpack(struct sk_buff *skb)
364 struct igmphdr *pig = igmp_hdr(skb);
365 const int igmplen = skb->tail - skb->transport_header;
367 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
369 return ip_local_out(skb);
372 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
374 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
377 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
378 int type, struct igmpv3_grec **ppgr)
380 struct net_device *dev = pmc->interface->dev;
381 struct igmpv3_report *pih;
382 struct igmpv3_grec *pgr;
384 if (!skb)
385 skb = igmpv3_newpack(dev, dev->mtu);
386 if (!skb)
387 return NULL;
388 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
389 pgr->grec_type = type;
390 pgr->grec_auxwords = 0;
391 pgr->grec_nsrcs = 0;
392 pgr->grec_mca = pmc->multiaddr;
393 pih = igmpv3_report_hdr(skb);
394 pih->ngrec = htons(ntohs(pih->ngrec)+1);
395 *ppgr = pgr;
396 return skb;
399 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
400 skb_tailroom(skb)) : 0)
402 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
403 int type, int gdeleted, int sdeleted)
405 struct net_device *dev = pmc->interface->dev;
406 struct igmpv3_report *pih;
407 struct igmpv3_grec *pgr = NULL;
408 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
409 int scount, stotal, first, isquery, truncate;
411 if (pmc->multiaddr == IGMP_ALL_HOSTS)
412 return skb;
414 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
415 type == IGMPV3_MODE_IS_EXCLUDE;
416 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
417 type == IGMPV3_CHANGE_TO_EXCLUDE;
419 stotal = scount = 0;
421 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
423 if (!*psf_list)
424 goto empty_source;
426 pih = skb ? igmpv3_report_hdr(skb) : NULL;
428 /* EX and TO_EX get a fresh packet, if needed */
429 if (truncate) {
430 if (pih && pih->ngrec &&
431 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
432 if (skb)
433 igmpv3_sendpack(skb);
434 skb = igmpv3_newpack(dev, dev->mtu);
437 first = 1;
438 psf_prev = NULL;
439 for (psf=*psf_list; psf; psf=psf_next) {
440 __be32 *psrc;
442 psf_next = psf->sf_next;
444 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
445 psf_prev = psf;
446 continue;
449 /* clear marks on query responses */
450 if (isquery)
451 psf->sf_gsresp = 0;
453 if (AVAILABLE(skb) < sizeof(__be32) +
454 first*sizeof(struct igmpv3_grec)) {
455 if (truncate && !first)
456 break; /* truncate these */
457 if (pgr)
458 pgr->grec_nsrcs = htons(scount);
459 if (skb)
460 igmpv3_sendpack(skb);
461 skb = igmpv3_newpack(dev, dev->mtu);
462 first = 1;
463 scount = 0;
465 if (first) {
466 skb = add_grhead(skb, pmc, type, &pgr);
467 first = 0;
469 if (!skb)
470 return NULL;
471 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
472 *psrc = psf->sf_inaddr;
473 scount++; stotal++;
474 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
475 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
476 psf->sf_crcount--;
477 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
478 if (psf_prev)
479 psf_prev->sf_next = psf->sf_next;
480 else
481 *psf_list = psf->sf_next;
482 kfree(psf);
483 continue;
486 psf_prev = psf;
489 empty_source:
490 if (!stotal) {
491 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
492 type == IGMPV3_BLOCK_OLD_SOURCES)
493 return skb;
494 if (pmc->crcount || isquery) {
495 /* make sure we have room for group header */
496 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
497 igmpv3_sendpack(skb);
498 skb = NULL; /* add_grhead will get a new one */
500 skb = add_grhead(skb, pmc, type, &pgr);
503 if (pgr)
504 pgr->grec_nsrcs = htons(scount);
506 if (isquery)
507 pmc->gsquery = 0; /* clear query state on report */
508 return skb;
511 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
513 struct sk_buff *skb = NULL;
514 int type;
516 if (!pmc) {
517 rcu_read_lock();
518 for_each_pmc_rcu(in_dev, pmc) {
519 if (pmc->multiaddr == IGMP_ALL_HOSTS)
520 continue;
521 spin_lock_bh(&pmc->lock);
522 if (pmc->sfcount[MCAST_EXCLUDE])
523 type = IGMPV3_MODE_IS_EXCLUDE;
524 else
525 type = IGMPV3_MODE_IS_INCLUDE;
526 skb = add_grec(skb, pmc, type, 0, 0);
527 spin_unlock_bh(&pmc->lock);
529 rcu_read_unlock();
530 } else {
531 spin_lock_bh(&pmc->lock);
532 if (pmc->sfcount[MCAST_EXCLUDE])
533 type = IGMPV3_MODE_IS_EXCLUDE;
534 else
535 type = IGMPV3_MODE_IS_INCLUDE;
536 skb = add_grec(skb, pmc, type, 0, 0);
537 spin_unlock_bh(&pmc->lock);
539 if (!skb)
540 return 0;
541 return igmpv3_sendpack(skb);
545 * remove zero-count source records from a source filter list
547 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
549 struct ip_sf_list *psf_prev, *psf_next, *psf;
551 psf_prev = NULL;
552 for (psf=*ppsf; psf; psf = psf_next) {
553 psf_next = psf->sf_next;
554 if (psf->sf_crcount == 0) {
555 if (psf_prev)
556 psf_prev->sf_next = psf->sf_next;
557 else
558 *ppsf = psf->sf_next;
559 kfree(psf);
560 } else
561 psf_prev = psf;
565 static void igmpv3_send_cr(struct in_device *in_dev)
567 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
568 struct sk_buff *skb = NULL;
569 int type, dtype;
571 rcu_read_lock();
572 spin_lock_bh(&in_dev->mc_tomb_lock);
574 /* deleted MCA's */
575 pmc_prev = NULL;
576 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
577 pmc_next = pmc->next;
578 if (pmc->sfmode == MCAST_INCLUDE) {
579 type = IGMPV3_BLOCK_OLD_SOURCES;
580 dtype = IGMPV3_BLOCK_OLD_SOURCES;
581 skb = add_grec(skb, pmc, type, 1, 0);
582 skb = add_grec(skb, pmc, dtype, 1, 1);
584 if (pmc->crcount) {
585 if (pmc->sfmode == MCAST_EXCLUDE) {
586 type = IGMPV3_CHANGE_TO_INCLUDE;
587 skb = add_grec(skb, pmc, type, 1, 0);
589 pmc->crcount--;
590 if (pmc->crcount == 0) {
591 igmpv3_clear_zeros(&pmc->tomb);
592 igmpv3_clear_zeros(&pmc->sources);
595 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
596 if (pmc_prev)
597 pmc_prev->next = pmc_next;
598 else
599 in_dev->mc_tomb = pmc_next;
600 in_dev_put(pmc->interface);
601 kfree(pmc);
602 } else
603 pmc_prev = pmc;
605 spin_unlock_bh(&in_dev->mc_tomb_lock);
607 /* change recs */
608 for_each_pmc_rcu(in_dev, pmc) {
609 spin_lock_bh(&pmc->lock);
610 if (pmc->sfcount[MCAST_EXCLUDE]) {
611 type = IGMPV3_BLOCK_OLD_SOURCES;
612 dtype = IGMPV3_ALLOW_NEW_SOURCES;
613 } else {
614 type = IGMPV3_ALLOW_NEW_SOURCES;
615 dtype = IGMPV3_BLOCK_OLD_SOURCES;
617 skb = add_grec(skb, pmc, type, 0, 0);
618 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
620 /* filter mode changes */
621 if (pmc->crcount) {
622 if (pmc->sfmode == MCAST_EXCLUDE)
623 type = IGMPV3_CHANGE_TO_EXCLUDE;
624 else
625 type = IGMPV3_CHANGE_TO_INCLUDE;
626 skb = add_grec(skb, pmc, type, 0, 0);
627 pmc->crcount--;
629 spin_unlock_bh(&pmc->lock);
631 rcu_read_unlock();
633 if (!skb)
634 return;
635 (void) igmpv3_sendpack(skb);
638 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
639 int type)
641 struct sk_buff *skb;
642 struct iphdr *iph;
643 struct igmphdr *ih;
644 struct rtable *rt;
645 struct net_device *dev = in_dev->dev;
646 struct net *net = dev_net(dev);
647 __be32 group = pmc ? pmc->multiaddr : 0;
648 struct flowi4 fl4;
649 __be32 dst;
651 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
652 return igmpv3_send_report(in_dev, pmc);
653 else if (type == IGMP_HOST_LEAVE_MESSAGE)
654 dst = IGMP_ALL_ROUTER;
655 else
656 dst = group;
658 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
659 0, 0,
660 IPPROTO_IGMP, 0, dev->ifindex);
661 if (IS_ERR(rt))
662 return -1;
664 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
665 if (skb == NULL) {
666 ip_rt_put(rt);
667 return -1;
670 skb_dst_set(skb, &rt->dst);
672 skb_reserve(skb, LL_RESERVED_SPACE(dev));
674 skb_reset_network_header(skb);
675 iph = ip_hdr(skb);
676 skb_put(skb, sizeof(struct iphdr) + 4);
678 iph->version = 4;
679 iph->ihl = (sizeof(struct iphdr)+4)>>2;
680 iph->tos = 0xc0;
681 iph->frag_off = htons(IP_DF);
682 iph->ttl = 1;
683 iph->daddr = dst;
684 iph->saddr = fl4.saddr;
685 iph->protocol = IPPROTO_IGMP;
686 ip_select_ident(iph, &rt->dst, NULL);
687 ((u8*)&iph[1])[0] = IPOPT_RA;
688 ((u8*)&iph[1])[1] = 4;
689 ((u8*)&iph[1])[2] = 0;
690 ((u8*)&iph[1])[3] = 0;
692 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
693 ih->type = type;
694 ih->code = 0;
695 ih->csum = 0;
696 ih->group = group;
697 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
699 return ip_local_out(skb);
702 static void igmp_gq_timer_expire(unsigned long data)
704 struct in_device *in_dev = (struct in_device *)data;
706 in_dev->mr_gq_running = 0;
707 igmpv3_send_report(in_dev, NULL);
708 __in_dev_put(in_dev);
711 static void igmp_ifc_timer_expire(unsigned long data)
713 struct in_device *in_dev = (struct in_device *)data;
715 igmpv3_send_cr(in_dev);
716 if (in_dev->mr_ifc_count) {
717 in_dev->mr_ifc_count--;
718 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
720 __in_dev_put(in_dev);
723 static void igmp_ifc_event(struct in_device *in_dev)
725 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
726 return;
727 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
728 IGMP_Unsolicited_Report_Count;
729 igmp_ifc_start_timer(in_dev, 1);
733 static void igmp_timer_expire(unsigned long data)
735 struct ip_mc_list *im=(struct ip_mc_list *)data;
736 struct in_device *in_dev = im->interface;
738 spin_lock(&im->lock);
739 im->tm_running = 0;
741 if (im->unsolicit_count) {
742 im->unsolicit_count--;
743 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
745 im->reporter = 1;
746 spin_unlock(&im->lock);
748 if (IGMP_V1_SEEN(in_dev))
749 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
750 else if (IGMP_V2_SEEN(in_dev))
751 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
752 else
753 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
755 ip_ma_put(im);
758 /* mark EXCLUDE-mode sources */
759 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
761 struct ip_sf_list *psf;
762 int i, scount;
764 scount = 0;
765 for (psf=pmc->sources; psf; psf=psf->sf_next) {
766 if (scount == nsrcs)
767 break;
768 for (i=0; i<nsrcs; i++) {
769 /* skip inactive filters */
770 if (psf->sf_count[MCAST_INCLUDE] ||
771 pmc->sfcount[MCAST_EXCLUDE] !=
772 psf->sf_count[MCAST_EXCLUDE])
773 continue;
774 if (srcs[i] == psf->sf_inaddr) {
775 scount++;
776 break;
780 pmc->gsquery = 0;
781 if (scount == nsrcs) /* all sources excluded */
782 return 0;
783 return 1;
786 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
788 struct ip_sf_list *psf;
789 int i, scount;
791 if (pmc->sfmode == MCAST_EXCLUDE)
792 return igmp_xmarksources(pmc, nsrcs, srcs);
794 /* mark INCLUDE-mode sources */
795 scount = 0;
796 for (psf=pmc->sources; psf; psf=psf->sf_next) {
797 if (scount == nsrcs)
798 break;
799 for (i=0; i<nsrcs; i++)
800 if (srcs[i] == psf->sf_inaddr) {
801 psf->sf_gsresp = 1;
802 scount++;
803 break;
806 if (!scount) {
807 pmc->gsquery = 0;
808 return 0;
810 pmc->gsquery = 1;
811 return 1;
814 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
816 struct ip_mc_list *im;
818 /* Timers are only set for non-local groups */
820 if (group == IGMP_ALL_HOSTS)
821 return;
823 rcu_read_lock();
824 for_each_pmc_rcu(in_dev, im) {
825 if (im->multiaddr == group) {
826 igmp_stop_timer(im);
827 break;
830 rcu_read_unlock();
833 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
834 int len)
836 struct igmphdr *ih = igmp_hdr(skb);
837 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
838 struct ip_mc_list *im;
839 __be32 group = ih->group;
840 int max_delay;
841 int mark = 0;
844 if (len == 8) {
845 if (ih->code == 0) {
846 /* Alas, old v1 router presents here. */
848 max_delay = IGMP_Query_Response_Interval;
849 in_dev->mr_v1_seen = jiffies +
850 IGMP_V1_Router_Present_Timeout;
851 group = 0;
852 } else {
853 /* v2 router present */
854 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
855 in_dev->mr_v2_seen = jiffies +
856 IGMP_V2_Router_Present_Timeout;
858 /* cancel the interface change timer */
859 in_dev->mr_ifc_count = 0;
860 if (del_timer(&in_dev->mr_ifc_timer))
861 __in_dev_put(in_dev);
862 /* clear deleted report items */
863 igmpv3_clear_delrec(in_dev);
864 } else if (len < 12) {
865 return; /* ignore bogus packet; freed by caller */
866 } else if (IGMP_V1_SEEN(in_dev)) {
867 /* This is a v3 query with v1 queriers present */
868 max_delay = IGMP_Query_Response_Interval;
869 group = 0;
870 } else if (IGMP_V2_SEEN(in_dev)) {
871 /* this is a v3 query with v2 queriers present;
872 * Interpretation of the max_delay code is problematic here.
873 * A real v2 host would use ih_code directly, while v3 has a
874 * different encoding. We use the v3 encoding as more likely
875 * to be intended in a v3 query.
877 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
878 if (!max_delay)
879 max_delay = 1; /* can't mod w/ 0 */
880 } else { /* v3 */
881 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
882 return;
884 ih3 = igmpv3_query_hdr(skb);
885 if (ih3->nsrcs) {
886 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
887 + ntohs(ih3->nsrcs)*sizeof(__be32)))
888 return;
889 ih3 = igmpv3_query_hdr(skb);
892 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
893 if (!max_delay)
894 max_delay = 1; /* can't mod w/ 0 */
895 in_dev->mr_maxdelay = max_delay;
896 if (ih3->qrv)
897 in_dev->mr_qrv = ih3->qrv;
898 if (!group) { /* general query */
899 if (ih3->nsrcs)
900 return; /* no sources allowed */
901 igmp_gq_start_timer(in_dev);
902 return;
904 /* mark sources to include, if group & source-specific */
905 mark = ih3->nsrcs != 0;
909 * - Start the timers in all of our membership records
910 * that the query applies to for the interface on
911 * which the query arrived excl. those that belong
912 * to a "local" group (224.0.0.X)
913 * - For timers already running check if they need to
914 * be reset.
915 * - Use the igmp->igmp_code field as the maximum
916 * delay possible
918 rcu_read_lock();
919 for_each_pmc_rcu(in_dev, im) {
920 int changed;
922 if (group && group != im->multiaddr)
923 continue;
924 if (im->multiaddr == IGMP_ALL_HOSTS)
925 continue;
926 spin_lock_bh(&im->lock);
927 if (im->tm_running)
928 im->gsquery = im->gsquery && mark;
929 else
930 im->gsquery = mark;
931 changed = !im->gsquery ||
932 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
933 spin_unlock_bh(&im->lock);
934 if (changed)
935 igmp_mod_timer(im, max_delay);
937 rcu_read_unlock();
940 /* called in rcu_read_lock() section */
941 int igmp_rcv(struct sk_buff *skb)
943 /* This basically follows the spec line by line -- see RFC1112 */
944 struct igmphdr *ih;
945 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
946 int len = skb->len;
948 if (in_dev == NULL)
949 goto drop;
951 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
952 goto drop;
954 switch (skb->ip_summed) {
955 case CHECKSUM_COMPLETE:
956 if (!csum_fold(skb->csum))
957 break;
958 /* fall through */
959 case CHECKSUM_NONE:
960 skb->csum = 0;
961 if (__skb_checksum_complete(skb))
962 goto drop;
965 ih = igmp_hdr(skb);
966 switch (ih->type) {
967 case IGMP_HOST_MEMBERSHIP_QUERY:
968 igmp_heard_query(in_dev, skb, len);
969 break;
970 case IGMP_HOST_MEMBERSHIP_REPORT:
971 case IGMPV2_HOST_MEMBERSHIP_REPORT:
972 /* Is it our report looped back? */
973 if (rt_is_output_route(skb_rtable(skb)))
974 break;
975 /* don't rely on MC router hearing unicast reports */
976 if (skb->pkt_type == PACKET_MULTICAST ||
977 skb->pkt_type == PACKET_BROADCAST)
978 igmp_heard_report(in_dev, ih->group);
979 break;
980 case IGMP_PIM:
981 #ifdef CONFIG_IP_PIMSM_V1
982 return pim_rcv_v1(skb);
983 #endif
984 case IGMPV3_HOST_MEMBERSHIP_REPORT:
985 case IGMP_DVMRP:
986 case IGMP_TRACE:
987 case IGMP_HOST_LEAVE_MESSAGE:
988 case IGMP_MTRACE:
989 case IGMP_MTRACE_RESP:
990 break;
991 default:
992 break;
995 drop:
996 kfree_skb(skb);
997 return 0;
1000 #endif
1004 * Add a filter to a device
1007 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1009 char buf[MAX_ADDR_LEN];
1010 struct net_device *dev = in_dev->dev;
1012 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1013 We will get multicast token leakage, when IFF_MULTICAST
1014 is changed. This check should be done in ndo_set_rx_mode
1015 routine. Something sort of:
1016 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1017 --ANK
1019 if (arp_mc_map(addr, buf, dev, 0) == 0)
1020 dev_mc_add(dev, buf);
1024 * Remove a filter from a device
1027 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1029 char buf[MAX_ADDR_LEN];
1030 struct net_device *dev = in_dev->dev;
1032 if (arp_mc_map(addr, buf, dev, 0) == 0)
1033 dev_mc_del(dev, buf);
1036 #ifdef CONFIG_IP_MULTICAST
1038 * deleted ip_mc_list manipulation
1040 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1042 struct ip_mc_list *pmc;
1044 /* this is an "ip_mc_list" for convenience; only the fields below
1045 * are actually used. In particular, the refcnt and users are not
1046 * used for management of the delete list. Using the same structure
1047 * for deleted items allows change reports to use common code with
1048 * non-deleted or query-response MCA's.
1050 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1051 if (!pmc)
1052 return;
1053 spin_lock_bh(&im->lock);
1054 pmc->interface = im->interface;
1055 in_dev_hold(in_dev);
1056 pmc->multiaddr = im->multiaddr;
1057 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1058 IGMP_Unsolicited_Report_Count;
1059 pmc->sfmode = im->sfmode;
1060 if (pmc->sfmode == MCAST_INCLUDE) {
1061 struct ip_sf_list *psf;
1063 pmc->tomb = im->tomb;
1064 pmc->sources = im->sources;
1065 im->tomb = im->sources = NULL;
1066 for (psf=pmc->sources; psf; psf=psf->sf_next)
1067 psf->sf_crcount = pmc->crcount;
1069 spin_unlock_bh(&im->lock);
1071 spin_lock_bh(&in_dev->mc_tomb_lock);
1072 pmc->next = in_dev->mc_tomb;
1073 in_dev->mc_tomb = pmc;
1074 spin_unlock_bh(&in_dev->mc_tomb_lock);
1077 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1079 struct ip_mc_list *pmc, *pmc_prev;
1080 struct ip_sf_list *psf, *psf_next;
1082 spin_lock_bh(&in_dev->mc_tomb_lock);
1083 pmc_prev = NULL;
1084 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1085 if (pmc->multiaddr == multiaddr)
1086 break;
1087 pmc_prev = pmc;
1089 if (pmc) {
1090 if (pmc_prev)
1091 pmc_prev->next = pmc->next;
1092 else
1093 in_dev->mc_tomb = pmc->next;
1095 spin_unlock_bh(&in_dev->mc_tomb_lock);
1096 if (pmc) {
1097 for (psf=pmc->tomb; psf; psf=psf_next) {
1098 psf_next = psf->sf_next;
1099 kfree(psf);
1101 in_dev_put(pmc->interface);
1102 kfree(pmc);
1106 static void igmpv3_clear_delrec(struct in_device *in_dev)
1108 struct ip_mc_list *pmc, *nextpmc;
1110 spin_lock_bh(&in_dev->mc_tomb_lock);
1111 pmc = in_dev->mc_tomb;
1112 in_dev->mc_tomb = NULL;
1113 spin_unlock_bh(&in_dev->mc_tomb_lock);
1115 for (; pmc; pmc = nextpmc) {
1116 nextpmc = pmc->next;
1117 ip_mc_clear_src(pmc);
1118 in_dev_put(pmc->interface);
1119 kfree(pmc);
1121 /* clear dead sources, too */
1122 rcu_read_lock();
1123 for_each_pmc_rcu(in_dev, pmc) {
1124 struct ip_sf_list *psf, *psf_next;
1126 spin_lock_bh(&pmc->lock);
1127 psf = pmc->tomb;
1128 pmc->tomb = NULL;
1129 spin_unlock_bh(&pmc->lock);
1130 for (; psf; psf=psf_next) {
1131 psf_next = psf->sf_next;
1132 kfree(psf);
1135 rcu_read_unlock();
1137 #endif
1139 static void igmp_group_dropped(struct ip_mc_list *im)
1141 struct in_device *in_dev = im->interface;
1142 #ifdef CONFIG_IP_MULTICAST
1143 int reporter;
1144 #endif
1146 if (im->loaded) {
1147 im->loaded = 0;
1148 ip_mc_filter_del(in_dev, im->multiaddr);
1151 #ifdef CONFIG_IP_MULTICAST
1152 if (im->multiaddr == IGMP_ALL_HOSTS)
1153 return;
1155 reporter = im->reporter;
1156 igmp_stop_timer(im);
1158 if (!in_dev->dead) {
1159 if (IGMP_V1_SEEN(in_dev))
1160 return;
1161 if (IGMP_V2_SEEN(in_dev)) {
1162 if (reporter)
1163 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1164 return;
1166 /* IGMPv3 */
1167 igmpv3_add_delrec(in_dev, im);
1169 igmp_ifc_event(in_dev);
1171 #endif
1174 static void igmp_group_added(struct ip_mc_list *im)
1176 struct in_device *in_dev = im->interface;
1178 if (im->loaded == 0) {
1179 im->loaded = 1;
1180 ip_mc_filter_add(in_dev, im->multiaddr);
1183 #ifdef CONFIG_IP_MULTICAST
1184 if (im->multiaddr == IGMP_ALL_HOSTS)
1185 return;
1187 if (in_dev->dead)
1188 return;
1189 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1190 spin_lock_bh(&im->lock);
1191 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1192 spin_unlock_bh(&im->lock);
1193 return;
1195 /* else, v3 */
1197 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1198 IGMP_Unsolicited_Report_Count;
1199 igmp_ifc_event(in_dev);
1200 #endif
1205 * Multicast list managers
1210 * A socket has joined a multicast group on device dev.
1213 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1215 struct ip_mc_list *im;
1217 ASSERT_RTNL();
1219 for_each_pmc_rtnl(in_dev, im) {
1220 if (im->multiaddr == addr) {
1221 im->users++;
1222 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1223 goto out;
1227 im = kzalloc(sizeof(*im), GFP_KERNEL);
1228 if (!im)
1229 goto out;
1231 im->users = 1;
1232 im->interface = in_dev;
1233 in_dev_hold(in_dev);
1234 im->multiaddr = addr;
1235 /* initial mode is (EX, empty) */
1236 im->sfmode = MCAST_EXCLUDE;
1237 im->sfcount[MCAST_EXCLUDE] = 1;
1238 atomic_set(&im->refcnt, 1);
1239 spin_lock_init(&im->lock);
1240 #ifdef CONFIG_IP_MULTICAST
1241 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1242 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1243 #endif
1245 im->next_rcu = in_dev->mc_list;
1246 in_dev->mc_count++;
1247 rcu_assign_pointer(in_dev->mc_list, im);
1249 #ifdef CONFIG_IP_MULTICAST
1250 igmpv3_del_delrec(in_dev, im->multiaddr);
1251 #endif
1252 igmp_group_added(im);
1253 if (!in_dev->dead)
1254 ip_rt_multicast_event(in_dev);
1255 out:
1256 return;
1258 EXPORT_SYMBOL(ip_mc_inc_group);
1261 * Resend IGMP JOIN report; used for bonding.
1262 * Called with rcu_read_lock()
1264 void ip_mc_rejoin_groups(struct in_device *in_dev)
1266 #ifdef CONFIG_IP_MULTICAST
1267 struct ip_mc_list *im;
1268 int type;
1270 for_each_pmc_rcu(in_dev, im) {
1271 if (im->multiaddr == IGMP_ALL_HOSTS)
1272 continue;
1274 /* a failover is happening and switches
1275 * must be notified immediately
1277 if (IGMP_V1_SEEN(in_dev))
1278 type = IGMP_HOST_MEMBERSHIP_REPORT;
1279 else if (IGMP_V2_SEEN(in_dev))
1280 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1281 else
1282 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1283 igmp_send_report(in_dev, im, type);
1285 #endif
1287 EXPORT_SYMBOL(ip_mc_rejoin_groups);
1290 * A socket has left a multicast group on device dev
1293 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1295 struct ip_mc_list *i;
1296 struct ip_mc_list __rcu **ip;
1298 ASSERT_RTNL();
1300 for (ip = &in_dev->mc_list;
1301 (i = rtnl_dereference(*ip)) != NULL;
1302 ip = &i->next_rcu) {
1303 if (i->multiaddr == addr) {
1304 if (--i->users == 0) {
1305 *ip = i->next_rcu;
1306 in_dev->mc_count--;
1307 igmp_group_dropped(i);
1308 ip_mc_clear_src(i);
1310 if (!in_dev->dead)
1311 ip_rt_multicast_event(in_dev);
1313 ip_ma_put(i);
1314 return;
1316 break;
1320 EXPORT_SYMBOL(ip_mc_dec_group);
1322 /* Device changing type */
1324 void ip_mc_unmap(struct in_device *in_dev)
1326 struct ip_mc_list *pmc;
1328 ASSERT_RTNL();
1330 for_each_pmc_rtnl(in_dev, pmc)
1331 igmp_group_dropped(pmc);
1334 void ip_mc_remap(struct in_device *in_dev)
1336 struct ip_mc_list *pmc;
1338 ASSERT_RTNL();
1340 for_each_pmc_rtnl(in_dev, pmc)
1341 igmp_group_added(pmc);
1344 /* Device going down */
1346 void ip_mc_down(struct in_device *in_dev)
1348 struct ip_mc_list *pmc;
1350 ASSERT_RTNL();
1352 for_each_pmc_rtnl(in_dev, pmc)
1353 igmp_group_dropped(pmc);
1355 #ifdef CONFIG_IP_MULTICAST
1356 in_dev->mr_ifc_count = 0;
1357 if (del_timer(&in_dev->mr_ifc_timer))
1358 __in_dev_put(in_dev);
1359 in_dev->mr_gq_running = 0;
1360 if (del_timer(&in_dev->mr_gq_timer))
1361 __in_dev_put(in_dev);
1362 igmpv3_clear_delrec(in_dev);
1363 #endif
1365 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1368 void ip_mc_init_dev(struct in_device *in_dev)
1370 ASSERT_RTNL();
1372 in_dev->mc_tomb = NULL;
1373 #ifdef CONFIG_IP_MULTICAST
1374 in_dev->mr_gq_running = 0;
1375 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1376 (unsigned long)in_dev);
1377 in_dev->mr_ifc_count = 0;
1378 in_dev->mc_count = 0;
1379 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1380 (unsigned long)in_dev);
1381 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1382 #endif
1384 spin_lock_init(&in_dev->mc_tomb_lock);
1387 /* Device going up */
1389 void ip_mc_up(struct in_device *in_dev)
1391 struct ip_mc_list *pmc;
1393 ASSERT_RTNL();
1395 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1397 for_each_pmc_rtnl(in_dev, pmc)
1398 igmp_group_added(pmc);
1402 * Device is about to be destroyed: clean up.
1405 void ip_mc_destroy_dev(struct in_device *in_dev)
1407 struct ip_mc_list *i;
1409 ASSERT_RTNL();
1411 /* Deactivate timers */
1412 ip_mc_down(in_dev);
1414 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1415 in_dev->mc_list = i->next_rcu;
1416 in_dev->mc_count--;
1418 /* We've dropped the groups in ip_mc_down already */
1419 ip_mc_clear_src(i);
1420 ip_ma_put(i);
1424 /* RTNL is locked */
1425 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1427 struct net_device *dev = NULL;
1428 struct in_device *idev = NULL;
1430 if (imr->imr_ifindex) {
1431 idev = inetdev_by_index(net, imr->imr_ifindex);
1432 return idev;
1434 if (imr->imr_address.s_addr) {
1435 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1436 if (!dev)
1437 return NULL;
1440 if (!dev) {
1441 struct rtable *rt = ip_route_output(net,
1442 imr->imr_multiaddr.s_addr,
1443 0, 0, 0);
1444 if (!IS_ERR(rt)) {
1445 dev = rt->dst.dev;
1446 ip_rt_put(rt);
1449 if (dev) {
1450 imr->imr_ifindex = dev->ifindex;
1451 idev = __in_dev_get_rtnl(dev);
1453 return idev;
1457 * Join a socket to a group
1459 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1460 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1463 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1464 __be32 *psfsrc)
1466 struct ip_sf_list *psf, *psf_prev;
1467 int rv = 0;
1469 psf_prev = NULL;
1470 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1471 if (psf->sf_inaddr == *psfsrc)
1472 break;
1473 psf_prev = psf;
1475 if (!psf || psf->sf_count[sfmode] == 0) {
1476 /* source filter not found, or count wrong => bug */
1477 return -ESRCH;
1479 psf->sf_count[sfmode]--;
1480 if (psf->sf_count[sfmode] == 0) {
1481 ip_rt_multicast_event(pmc->interface);
1483 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1484 #ifdef CONFIG_IP_MULTICAST
1485 struct in_device *in_dev = pmc->interface;
1486 #endif
1488 /* no more filters for this source */
1489 if (psf_prev)
1490 psf_prev->sf_next = psf->sf_next;
1491 else
1492 pmc->sources = psf->sf_next;
1493 #ifdef CONFIG_IP_MULTICAST
1494 if (psf->sf_oldin &&
1495 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1496 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1497 IGMP_Unsolicited_Report_Count;
1498 psf->sf_next = pmc->tomb;
1499 pmc->tomb = psf;
1500 rv = 1;
1501 } else
1502 #endif
1503 kfree(psf);
1505 return rv;
1508 #ifndef CONFIG_IP_MULTICAST
1509 #define igmp_ifc_event(x) do { } while (0)
1510 #endif
1512 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1513 int sfcount, __be32 *psfsrc, int delta)
1515 struct ip_mc_list *pmc;
1516 int changerec = 0;
1517 int i, err;
1519 if (!in_dev)
1520 return -ENODEV;
1521 rcu_read_lock();
1522 for_each_pmc_rcu(in_dev, pmc) {
1523 if (*pmca == pmc->multiaddr)
1524 break;
1526 if (!pmc) {
1527 /* MCA not found?? bug */
1528 rcu_read_unlock();
1529 return -ESRCH;
1531 spin_lock_bh(&pmc->lock);
1532 rcu_read_unlock();
1533 #ifdef CONFIG_IP_MULTICAST
1534 sf_markstate(pmc);
1535 #endif
1536 if (!delta) {
1537 err = -EINVAL;
1538 if (!pmc->sfcount[sfmode])
1539 goto out_unlock;
1540 pmc->sfcount[sfmode]--;
1542 err = 0;
1543 for (i=0; i<sfcount; i++) {
1544 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1546 changerec |= rv > 0;
1547 if (!err && rv < 0)
1548 err = rv;
1550 if (pmc->sfmode == MCAST_EXCLUDE &&
1551 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1552 pmc->sfcount[MCAST_INCLUDE]) {
1553 #ifdef CONFIG_IP_MULTICAST
1554 struct ip_sf_list *psf;
1555 #endif
1557 /* filter mode change */
1558 pmc->sfmode = MCAST_INCLUDE;
1559 #ifdef CONFIG_IP_MULTICAST
1560 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1561 IGMP_Unsolicited_Report_Count;
1562 in_dev->mr_ifc_count = pmc->crcount;
1563 for (psf=pmc->sources; psf; psf = psf->sf_next)
1564 psf->sf_crcount = 0;
1565 igmp_ifc_event(pmc->interface);
1566 } else if (sf_setstate(pmc) || changerec) {
1567 igmp_ifc_event(pmc->interface);
1568 #endif
1570 out_unlock:
1571 spin_unlock_bh(&pmc->lock);
1572 return err;
1576 * Add multicast single-source filter to the interface list
1578 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1579 __be32 *psfsrc, int delta)
1581 struct ip_sf_list *psf, *psf_prev;
1583 psf_prev = NULL;
1584 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1585 if (psf->sf_inaddr == *psfsrc)
1586 break;
1587 psf_prev = psf;
1589 if (!psf) {
1590 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1591 if (!psf)
1592 return -ENOBUFS;
1593 psf->sf_inaddr = *psfsrc;
1594 if (psf_prev) {
1595 psf_prev->sf_next = psf;
1596 } else
1597 pmc->sources = psf;
1599 psf->sf_count[sfmode]++;
1600 if (psf->sf_count[sfmode] == 1) {
1601 ip_rt_multicast_event(pmc->interface);
1603 return 0;
1606 #ifdef CONFIG_IP_MULTICAST
1607 static void sf_markstate(struct ip_mc_list *pmc)
1609 struct ip_sf_list *psf;
1610 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1612 for (psf=pmc->sources; psf; psf=psf->sf_next)
1613 if (pmc->sfcount[MCAST_EXCLUDE]) {
1614 psf->sf_oldin = mca_xcount ==
1615 psf->sf_count[MCAST_EXCLUDE] &&
1616 !psf->sf_count[MCAST_INCLUDE];
1617 } else
1618 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1621 static int sf_setstate(struct ip_mc_list *pmc)
1623 struct ip_sf_list *psf, *dpsf;
1624 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1625 int qrv = pmc->interface->mr_qrv;
1626 int new_in, rv;
1628 rv = 0;
1629 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1630 if (pmc->sfcount[MCAST_EXCLUDE]) {
1631 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1632 !psf->sf_count[MCAST_INCLUDE];
1633 } else
1634 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1635 if (new_in) {
1636 if (!psf->sf_oldin) {
1637 struct ip_sf_list *prev = NULL;
1639 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1640 if (dpsf->sf_inaddr == psf->sf_inaddr)
1641 break;
1642 prev = dpsf;
1644 if (dpsf) {
1645 if (prev)
1646 prev->sf_next = dpsf->sf_next;
1647 else
1648 pmc->tomb = dpsf->sf_next;
1649 kfree(dpsf);
1651 psf->sf_crcount = qrv;
1652 rv++;
1654 } else if (psf->sf_oldin) {
1656 psf->sf_crcount = 0;
1658 * add or update "delete" records if an active filter
1659 * is now inactive
1661 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1662 if (dpsf->sf_inaddr == psf->sf_inaddr)
1663 break;
1664 if (!dpsf) {
1665 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1666 if (!dpsf)
1667 continue;
1668 *dpsf = *psf;
1669 /* pmc->lock held by callers */
1670 dpsf->sf_next = pmc->tomb;
1671 pmc->tomb = dpsf;
1673 dpsf->sf_crcount = qrv;
1674 rv++;
1677 return rv;
1679 #endif
1682 * Add multicast source filter list to the interface list
1684 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1685 int sfcount, __be32 *psfsrc, int delta)
1687 struct ip_mc_list *pmc;
1688 int isexclude;
1689 int i, err;
1691 if (!in_dev)
1692 return -ENODEV;
1693 rcu_read_lock();
1694 for_each_pmc_rcu(in_dev, pmc) {
1695 if (*pmca == pmc->multiaddr)
1696 break;
1698 if (!pmc) {
1699 /* MCA not found?? bug */
1700 rcu_read_unlock();
1701 return -ESRCH;
1703 spin_lock_bh(&pmc->lock);
1704 rcu_read_unlock();
1706 #ifdef CONFIG_IP_MULTICAST
1707 sf_markstate(pmc);
1708 #endif
1709 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1710 if (!delta)
1711 pmc->sfcount[sfmode]++;
1712 err = 0;
1713 for (i=0; i<sfcount; i++) {
1714 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1715 if (err)
1716 break;
1718 if (err) {
1719 int j;
1721 if (!delta)
1722 pmc->sfcount[sfmode]--;
1723 for (j=0; j<i; j++)
1724 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
1725 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1726 #ifdef CONFIG_IP_MULTICAST
1727 struct ip_sf_list *psf;
1728 in_dev = pmc->interface;
1729 #endif
1731 /* filter mode change */
1732 if (pmc->sfcount[MCAST_EXCLUDE])
1733 pmc->sfmode = MCAST_EXCLUDE;
1734 else if (pmc->sfcount[MCAST_INCLUDE])
1735 pmc->sfmode = MCAST_INCLUDE;
1736 #ifdef CONFIG_IP_MULTICAST
1737 /* else no filters; keep old mode for reports */
1739 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1740 IGMP_Unsolicited_Report_Count;
1741 in_dev->mr_ifc_count = pmc->crcount;
1742 for (psf=pmc->sources; psf; psf = psf->sf_next)
1743 psf->sf_crcount = 0;
1744 igmp_ifc_event(in_dev);
1745 } else if (sf_setstate(pmc)) {
1746 igmp_ifc_event(in_dev);
1747 #endif
1749 spin_unlock_bh(&pmc->lock);
1750 return err;
1753 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1755 struct ip_sf_list *psf, *nextpsf;
1757 for (psf=pmc->tomb; psf; psf=nextpsf) {
1758 nextpsf = psf->sf_next;
1759 kfree(psf);
1761 pmc->tomb = NULL;
1762 for (psf=pmc->sources; psf; psf=nextpsf) {
1763 nextpsf = psf->sf_next;
1764 kfree(psf);
1766 pmc->sources = NULL;
1767 pmc->sfmode = MCAST_EXCLUDE;
1768 pmc->sfcount[MCAST_INCLUDE] = 0;
1769 pmc->sfcount[MCAST_EXCLUDE] = 1;
1774 * Join a multicast group
1776 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1778 int err;
1779 __be32 addr = imr->imr_multiaddr.s_addr;
1780 struct ip_mc_socklist *iml = NULL, *i;
1781 struct in_device *in_dev;
1782 struct inet_sock *inet = inet_sk(sk);
1783 struct net *net = sock_net(sk);
1784 int ifindex;
1785 int count = 0;
1787 if (!ipv4_is_multicast(addr))
1788 return -EINVAL;
1790 rtnl_lock();
1792 in_dev = ip_mc_find_dev(net, imr);
1794 if (!in_dev) {
1795 iml = NULL;
1796 err = -ENODEV;
1797 goto done;
1800 err = -EADDRINUSE;
1801 ifindex = imr->imr_ifindex;
1802 for_each_pmc_rtnl(inet, i) {
1803 if (i->multi.imr_multiaddr.s_addr == addr &&
1804 i->multi.imr_ifindex == ifindex)
1805 goto done;
1806 count++;
1808 err = -ENOBUFS;
1809 if (count >= sysctl_igmp_max_memberships)
1810 goto done;
1811 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1812 if (iml == NULL)
1813 goto done;
1815 memcpy(&iml->multi, imr, sizeof(*imr));
1816 iml->next_rcu = inet->mc_list;
1817 iml->sflist = NULL;
1818 iml->sfmode = MCAST_EXCLUDE;
1819 rcu_assign_pointer(inet->mc_list, iml);
1820 ip_mc_inc_group(in_dev, addr);
1821 err = 0;
1822 done:
1823 rtnl_unlock();
1824 return err;
1826 EXPORT_SYMBOL(ip_mc_join_group);
1828 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1829 struct in_device *in_dev)
1831 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1832 int err;
1834 if (psf == NULL) {
1835 /* any-source empty exclude case */
1836 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1837 iml->sfmode, 0, NULL, 0);
1839 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1840 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1841 RCU_INIT_POINTER(iml->sflist, NULL);
1842 /* decrease mem now to avoid the memleak warning */
1843 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1844 kfree_rcu(psf, rcu);
1845 return err;
1849 * Ask a socket to leave a group.
1852 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1854 struct inet_sock *inet = inet_sk(sk);
1855 struct ip_mc_socklist *iml;
1856 struct ip_mc_socklist __rcu **imlp;
1857 struct in_device *in_dev;
1858 struct net *net = sock_net(sk);
1859 __be32 group = imr->imr_multiaddr.s_addr;
1860 u32 ifindex;
1861 int ret = -EADDRNOTAVAIL;
1863 rtnl_lock();
1864 in_dev = ip_mc_find_dev(net, imr);
1865 ifindex = imr->imr_ifindex;
1866 for (imlp = &inet->mc_list;
1867 (iml = rtnl_dereference(*imlp)) != NULL;
1868 imlp = &iml->next_rcu) {
1869 if (iml->multi.imr_multiaddr.s_addr != group)
1870 continue;
1871 if (ifindex) {
1872 if (iml->multi.imr_ifindex != ifindex)
1873 continue;
1874 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1875 iml->multi.imr_address.s_addr)
1876 continue;
1878 (void) ip_mc_leave_src(sk, iml, in_dev);
1880 *imlp = iml->next_rcu;
1882 if (in_dev)
1883 ip_mc_dec_group(in_dev, group);
1884 rtnl_unlock();
1885 /* decrease mem now to avoid the memleak warning */
1886 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1887 kfree_rcu(iml, rcu);
1888 return 0;
1890 if (!in_dev)
1891 ret = -ENODEV;
1892 rtnl_unlock();
1893 return ret;
1896 int ip_mc_source(int add, int omode, struct sock *sk, struct
1897 ip_mreq_source *mreqs, int ifindex)
1899 int err;
1900 struct ip_mreqn imr;
1901 __be32 addr = mreqs->imr_multiaddr;
1902 struct ip_mc_socklist *pmc;
1903 struct in_device *in_dev = NULL;
1904 struct inet_sock *inet = inet_sk(sk);
1905 struct ip_sf_socklist *psl;
1906 struct net *net = sock_net(sk);
1907 int leavegroup = 0;
1908 int i, j, rv;
1910 if (!ipv4_is_multicast(addr))
1911 return -EINVAL;
1913 rtnl_lock();
1915 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1916 imr.imr_address.s_addr = mreqs->imr_interface;
1917 imr.imr_ifindex = ifindex;
1918 in_dev = ip_mc_find_dev(net, &imr);
1920 if (!in_dev) {
1921 err = -ENODEV;
1922 goto done;
1924 err = -EADDRNOTAVAIL;
1926 for_each_pmc_rtnl(inet, pmc) {
1927 if ((pmc->multi.imr_multiaddr.s_addr ==
1928 imr.imr_multiaddr.s_addr) &&
1929 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1930 break;
1932 if (!pmc) { /* must have a prior join */
1933 err = -EINVAL;
1934 goto done;
1936 /* if a source filter was set, must be the same mode as before */
1937 if (pmc->sflist) {
1938 if (pmc->sfmode != omode) {
1939 err = -EINVAL;
1940 goto done;
1942 } else if (pmc->sfmode != omode) {
1943 /* allow mode switches for empty-set filters */
1944 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1945 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1946 NULL, 0);
1947 pmc->sfmode = omode;
1950 psl = rtnl_dereference(pmc->sflist);
1951 if (!add) {
1952 if (!psl)
1953 goto done; /* err = -EADDRNOTAVAIL */
1954 rv = !0;
1955 for (i=0; i<psl->sl_count; i++) {
1956 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1957 sizeof(__be32));
1958 if (rv == 0)
1959 break;
1961 if (rv) /* source not found */
1962 goto done; /* err = -EADDRNOTAVAIL */
1964 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1965 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1966 leavegroup = 1;
1967 goto done;
1970 /* update the interface filter */
1971 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1972 &mreqs->imr_sourceaddr, 1);
1974 for (j=i+1; j<psl->sl_count; j++)
1975 psl->sl_addr[j-1] = psl->sl_addr[j];
1976 psl->sl_count--;
1977 err = 0;
1978 goto done;
1980 /* else, add a new source to the filter */
1982 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1983 err = -ENOBUFS;
1984 goto done;
1986 if (!psl || psl->sl_count == psl->sl_max) {
1987 struct ip_sf_socklist *newpsl;
1988 int count = IP_SFBLOCK;
1990 if (psl)
1991 count += psl->sl_max;
1992 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1993 if (!newpsl) {
1994 err = -ENOBUFS;
1995 goto done;
1997 newpsl->sl_max = count;
1998 newpsl->sl_count = count - IP_SFBLOCK;
1999 if (psl) {
2000 for (i=0; i<psl->sl_count; i++)
2001 newpsl->sl_addr[i] = psl->sl_addr[i];
2002 /* decrease mem now to avoid the memleak warning */
2003 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2004 kfree_rcu(psl, rcu);
2006 rcu_assign_pointer(pmc->sflist, newpsl);
2007 psl = newpsl;
2009 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2010 for (i=0; i<psl->sl_count; i++) {
2011 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2012 sizeof(__be32));
2013 if (rv == 0)
2014 break;
2016 if (rv == 0) /* address already there is an error */
2017 goto done;
2018 for (j=psl->sl_count-1; j>=i; j--)
2019 psl->sl_addr[j+1] = psl->sl_addr[j];
2020 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2021 psl->sl_count++;
2022 err = 0;
2023 /* update the interface list */
2024 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2025 &mreqs->imr_sourceaddr, 1);
2026 done:
2027 rtnl_unlock();
2028 if (leavegroup)
2029 return ip_mc_leave_group(sk, &imr);
2030 return err;
2033 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2035 int err = 0;
2036 struct ip_mreqn imr;
2037 __be32 addr = msf->imsf_multiaddr;
2038 struct ip_mc_socklist *pmc;
2039 struct in_device *in_dev;
2040 struct inet_sock *inet = inet_sk(sk);
2041 struct ip_sf_socklist *newpsl, *psl;
2042 struct net *net = sock_net(sk);
2043 int leavegroup = 0;
2045 if (!ipv4_is_multicast(addr))
2046 return -EINVAL;
2047 if (msf->imsf_fmode != MCAST_INCLUDE &&
2048 msf->imsf_fmode != MCAST_EXCLUDE)
2049 return -EINVAL;
2051 rtnl_lock();
2053 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2054 imr.imr_address.s_addr = msf->imsf_interface;
2055 imr.imr_ifindex = ifindex;
2056 in_dev = ip_mc_find_dev(net, &imr);
2058 if (!in_dev) {
2059 err = -ENODEV;
2060 goto done;
2063 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2064 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2065 leavegroup = 1;
2066 goto done;
2069 for_each_pmc_rtnl(inet, pmc) {
2070 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2071 pmc->multi.imr_ifindex == imr.imr_ifindex)
2072 break;
2074 if (!pmc) { /* must have a prior join */
2075 err = -EINVAL;
2076 goto done;
2078 if (msf->imsf_numsrc) {
2079 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2080 GFP_KERNEL);
2081 if (!newpsl) {
2082 err = -ENOBUFS;
2083 goto done;
2085 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2086 memcpy(newpsl->sl_addr, msf->imsf_slist,
2087 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2088 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2089 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2090 if (err) {
2091 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2092 goto done;
2094 } else {
2095 newpsl = NULL;
2096 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2097 msf->imsf_fmode, 0, NULL, 0);
2099 psl = rtnl_dereference(pmc->sflist);
2100 if (psl) {
2101 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2102 psl->sl_count, psl->sl_addr, 0);
2103 /* decrease mem now to avoid the memleak warning */
2104 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2105 kfree_rcu(psl, rcu);
2106 } else
2107 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2108 0, NULL, 0);
2109 rcu_assign_pointer(pmc->sflist, newpsl);
2110 pmc->sfmode = msf->imsf_fmode;
2111 err = 0;
2112 done:
2113 rtnl_unlock();
2114 if (leavegroup)
2115 err = ip_mc_leave_group(sk, &imr);
2116 return err;
2119 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2120 struct ip_msfilter __user *optval, int __user *optlen)
2122 int err, len, count, copycount;
2123 struct ip_mreqn imr;
2124 __be32 addr = msf->imsf_multiaddr;
2125 struct ip_mc_socklist *pmc;
2126 struct in_device *in_dev;
2127 struct inet_sock *inet = inet_sk(sk);
2128 struct ip_sf_socklist *psl;
2129 struct net *net = sock_net(sk);
2131 if (!ipv4_is_multicast(addr))
2132 return -EINVAL;
2134 rtnl_lock();
2136 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2137 imr.imr_address.s_addr = msf->imsf_interface;
2138 imr.imr_ifindex = 0;
2139 in_dev = ip_mc_find_dev(net, &imr);
2141 if (!in_dev) {
2142 err = -ENODEV;
2143 goto done;
2145 err = -EADDRNOTAVAIL;
2147 for_each_pmc_rtnl(inet, pmc) {
2148 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2149 pmc->multi.imr_ifindex == imr.imr_ifindex)
2150 break;
2152 if (!pmc) /* must have a prior join */
2153 goto done;
2154 msf->imsf_fmode = pmc->sfmode;
2155 psl = rtnl_dereference(pmc->sflist);
2156 rtnl_unlock();
2157 if (!psl) {
2158 len = 0;
2159 count = 0;
2160 } else {
2161 count = psl->sl_count;
2163 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2164 len = copycount * sizeof(psl->sl_addr[0]);
2165 msf->imsf_numsrc = count;
2166 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2167 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2168 return -EFAULT;
2170 if (len &&
2171 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2172 return -EFAULT;
2173 return 0;
2174 done:
2175 rtnl_unlock();
2176 return err;
2179 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2180 struct group_filter __user *optval, int __user *optlen)
2182 int err, i, count, copycount;
2183 struct sockaddr_in *psin;
2184 __be32 addr;
2185 struct ip_mc_socklist *pmc;
2186 struct inet_sock *inet = inet_sk(sk);
2187 struct ip_sf_socklist *psl;
2189 psin = (struct sockaddr_in *)&gsf->gf_group;
2190 if (psin->sin_family != AF_INET)
2191 return -EINVAL;
2192 addr = psin->sin_addr.s_addr;
2193 if (!ipv4_is_multicast(addr))
2194 return -EINVAL;
2196 rtnl_lock();
2198 err = -EADDRNOTAVAIL;
2200 for_each_pmc_rtnl(inet, pmc) {
2201 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2202 pmc->multi.imr_ifindex == gsf->gf_interface)
2203 break;
2205 if (!pmc) /* must have a prior join */
2206 goto done;
2207 gsf->gf_fmode = pmc->sfmode;
2208 psl = rtnl_dereference(pmc->sflist);
2209 rtnl_unlock();
2210 count = psl ? psl->sl_count : 0;
2211 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2212 gsf->gf_numsrc = count;
2213 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2214 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2215 return -EFAULT;
2217 for (i=0; i<copycount; i++) {
2218 struct sockaddr_storage ss;
2220 psin = (struct sockaddr_in *)&ss;
2221 memset(&ss, 0, sizeof(ss));
2222 psin->sin_family = AF_INET;
2223 psin->sin_addr.s_addr = psl->sl_addr[i];
2224 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2225 return -EFAULT;
2227 return 0;
2228 done:
2229 rtnl_unlock();
2230 return err;
2234 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2236 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2238 struct inet_sock *inet = inet_sk(sk);
2239 struct ip_mc_socklist *pmc;
2240 struct ip_sf_socklist *psl;
2241 int i;
2242 int ret;
2244 ret = 1;
2245 if (!ipv4_is_multicast(loc_addr))
2246 goto out;
2248 rcu_read_lock();
2249 for_each_pmc_rcu(inet, pmc) {
2250 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2251 pmc->multi.imr_ifindex == dif)
2252 break;
2254 ret = inet->mc_all;
2255 if (!pmc)
2256 goto unlock;
2257 psl = rcu_dereference(pmc->sflist);
2258 ret = (pmc->sfmode == MCAST_EXCLUDE);
2259 if (!psl)
2260 goto unlock;
2262 for (i=0; i<psl->sl_count; i++) {
2263 if (psl->sl_addr[i] == rmt_addr)
2264 break;
2266 ret = 0;
2267 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2268 goto unlock;
2269 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2270 goto unlock;
2271 ret = 1;
2272 unlock:
2273 rcu_read_unlock();
2274 out:
2275 return ret;
2279 * A socket is closing.
2282 void ip_mc_drop_socket(struct sock *sk)
2284 struct inet_sock *inet = inet_sk(sk);
2285 struct ip_mc_socklist *iml;
2286 struct net *net = sock_net(sk);
2288 if (inet->mc_list == NULL)
2289 return;
2291 rtnl_lock();
2292 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2293 struct in_device *in_dev;
2295 inet->mc_list = iml->next_rcu;
2296 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2297 (void) ip_mc_leave_src(sk, iml, in_dev);
2298 if (in_dev != NULL)
2299 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2300 /* decrease mem now to avoid the memleak warning */
2301 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2302 kfree_rcu(iml, rcu);
2304 rtnl_unlock();
2307 /* called with rcu_read_lock() */
2308 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2310 struct ip_mc_list *im;
2311 struct ip_sf_list *psf;
2312 int rv = 0;
2314 for_each_pmc_rcu(in_dev, im) {
2315 if (im->multiaddr == mc_addr)
2316 break;
2318 if (im && proto == IPPROTO_IGMP) {
2319 rv = 1;
2320 } else if (im) {
2321 if (src_addr) {
2322 for (psf=im->sources; psf; psf=psf->sf_next) {
2323 if (psf->sf_inaddr == src_addr)
2324 break;
2326 if (psf)
2327 rv = psf->sf_count[MCAST_INCLUDE] ||
2328 psf->sf_count[MCAST_EXCLUDE] !=
2329 im->sfcount[MCAST_EXCLUDE];
2330 else
2331 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2332 } else
2333 rv = 1; /* unspecified source; tentatively allow */
2335 return rv;
2338 #if defined(CONFIG_PROC_FS)
2339 struct igmp_mc_iter_state {
2340 struct seq_net_private p;
2341 struct net_device *dev;
2342 struct in_device *in_dev;
2345 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2347 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2349 struct net *net = seq_file_net(seq);
2350 struct ip_mc_list *im = NULL;
2351 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2353 state->in_dev = NULL;
2354 for_each_netdev_rcu(net, state->dev) {
2355 struct in_device *in_dev;
2357 in_dev = __in_dev_get_rcu(state->dev);
2358 if (!in_dev)
2359 continue;
2360 im = rcu_dereference(in_dev->mc_list);
2361 if (im) {
2362 state->in_dev = in_dev;
2363 break;
2366 return im;
2369 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2371 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2373 im = rcu_dereference(im->next_rcu);
2374 while (!im) {
2375 state->dev = next_net_device_rcu(state->dev);
2376 if (!state->dev) {
2377 state->in_dev = NULL;
2378 break;
2380 state->in_dev = __in_dev_get_rcu(state->dev);
2381 if (!state->in_dev)
2382 continue;
2383 im = rcu_dereference(state->in_dev->mc_list);
2385 return im;
2388 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2390 struct ip_mc_list *im = igmp_mc_get_first(seq);
2391 if (im)
2392 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2393 --pos;
2394 return pos ? NULL : im;
2397 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2398 __acquires(rcu)
2400 rcu_read_lock();
2401 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2404 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2406 struct ip_mc_list *im;
2407 if (v == SEQ_START_TOKEN)
2408 im = igmp_mc_get_first(seq);
2409 else
2410 im = igmp_mc_get_next(seq, v);
2411 ++*pos;
2412 return im;
2415 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2416 __releases(rcu)
2418 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2420 state->in_dev = NULL;
2421 state->dev = NULL;
2422 rcu_read_unlock();
2425 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2427 if (v == SEQ_START_TOKEN)
2428 seq_puts(seq,
2429 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2430 else {
2431 struct ip_mc_list *im = (struct ip_mc_list *)v;
2432 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2433 char *querier;
2434 #ifdef CONFIG_IP_MULTICAST
2435 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2436 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2437 "V3";
2438 #else
2439 querier = "NONE";
2440 #endif
2442 if (rcu_dereference(state->in_dev->mc_list) == im) {
2443 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2444 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2447 seq_printf(seq,
2448 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2449 im->multiaddr, im->users,
2450 im->tm_running, im->tm_running ?
2451 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2452 im->reporter);
2454 return 0;
2457 static const struct seq_operations igmp_mc_seq_ops = {
2458 .start = igmp_mc_seq_start,
2459 .next = igmp_mc_seq_next,
2460 .stop = igmp_mc_seq_stop,
2461 .show = igmp_mc_seq_show,
2464 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2466 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2467 sizeof(struct igmp_mc_iter_state));
2470 static const struct file_operations igmp_mc_seq_fops = {
2471 .owner = THIS_MODULE,
2472 .open = igmp_mc_seq_open,
2473 .read = seq_read,
2474 .llseek = seq_lseek,
2475 .release = seq_release_net,
2478 struct igmp_mcf_iter_state {
2479 struct seq_net_private p;
2480 struct net_device *dev;
2481 struct in_device *idev;
2482 struct ip_mc_list *im;
2485 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2487 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2489 struct net *net = seq_file_net(seq);
2490 struct ip_sf_list *psf = NULL;
2491 struct ip_mc_list *im = NULL;
2492 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2494 state->idev = NULL;
2495 state->im = NULL;
2496 for_each_netdev_rcu(net, state->dev) {
2497 struct in_device *idev;
2498 idev = __in_dev_get_rcu(state->dev);
2499 if (unlikely(idev == NULL))
2500 continue;
2501 im = rcu_dereference(idev->mc_list);
2502 if (likely(im != NULL)) {
2503 spin_lock_bh(&im->lock);
2504 psf = im->sources;
2505 if (likely(psf != NULL)) {
2506 state->im = im;
2507 state->idev = idev;
2508 break;
2510 spin_unlock_bh(&im->lock);
2513 return psf;
2516 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2518 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2520 psf = psf->sf_next;
2521 while (!psf) {
2522 spin_unlock_bh(&state->im->lock);
2523 state->im = state->im->next;
2524 while (!state->im) {
2525 state->dev = next_net_device_rcu(state->dev);
2526 if (!state->dev) {
2527 state->idev = NULL;
2528 goto out;
2530 state->idev = __in_dev_get_rcu(state->dev);
2531 if (!state->idev)
2532 continue;
2533 state->im = rcu_dereference(state->idev->mc_list);
2535 if (!state->im)
2536 break;
2537 spin_lock_bh(&state->im->lock);
2538 psf = state->im->sources;
2540 out:
2541 return psf;
2544 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2546 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2547 if (psf)
2548 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2549 --pos;
2550 return pos ? NULL : psf;
2553 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2554 __acquires(rcu)
2556 rcu_read_lock();
2557 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2560 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2562 struct ip_sf_list *psf;
2563 if (v == SEQ_START_TOKEN)
2564 psf = igmp_mcf_get_first(seq);
2565 else
2566 psf = igmp_mcf_get_next(seq, v);
2567 ++*pos;
2568 return psf;
2571 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2572 __releases(rcu)
2574 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2575 if (likely(state->im != NULL)) {
2576 spin_unlock_bh(&state->im->lock);
2577 state->im = NULL;
2579 state->idev = NULL;
2580 state->dev = NULL;
2581 rcu_read_unlock();
2584 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2586 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2587 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2589 if (v == SEQ_START_TOKEN) {
2590 seq_printf(seq,
2591 "%3s %6s "
2592 "%10s %10s %6s %6s\n", "Idx",
2593 "Device", "MCA",
2594 "SRC", "INC", "EXC");
2595 } else {
2596 seq_printf(seq,
2597 "%3d %6.6s 0x%08x "
2598 "0x%08x %6lu %6lu\n",
2599 state->dev->ifindex, state->dev->name,
2600 ntohl(state->im->multiaddr),
2601 ntohl(psf->sf_inaddr),
2602 psf->sf_count[MCAST_INCLUDE],
2603 psf->sf_count[MCAST_EXCLUDE]);
2605 return 0;
2608 static const struct seq_operations igmp_mcf_seq_ops = {
2609 .start = igmp_mcf_seq_start,
2610 .next = igmp_mcf_seq_next,
2611 .stop = igmp_mcf_seq_stop,
2612 .show = igmp_mcf_seq_show,
2615 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2617 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2618 sizeof(struct igmp_mcf_iter_state));
2621 static const struct file_operations igmp_mcf_seq_fops = {
2622 .owner = THIS_MODULE,
2623 .open = igmp_mcf_seq_open,
2624 .read = seq_read,
2625 .llseek = seq_lseek,
2626 .release = seq_release_net,
2629 static int __net_init igmp_net_init(struct net *net)
2631 struct proc_dir_entry *pde;
2633 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2634 if (!pde)
2635 goto out_igmp;
2636 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2637 if (!pde)
2638 goto out_mcfilter;
2639 return 0;
2641 out_mcfilter:
2642 proc_net_remove(net, "igmp");
2643 out_igmp:
2644 return -ENOMEM;
2647 static void __net_exit igmp_net_exit(struct net *net)
2649 proc_net_remove(net, "mcfilter");
2650 proc_net_remove(net, "igmp");
2653 static struct pernet_operations igmp_net_ops = {
2654 .init = igmp_net_init,
2655 .exit = igmp_net_exit,
2658 int __init igmp_mc_proc_init(void)
2660 return register_pernet_subsys(&igmp_net_ops);
2662 #endif