Merge branch 'fec'
[linux-2.6/btrfs-unstable.git] / net / ipv4 / igmp.c
blob6748d420f714f4add6acd23a4aefd1f35c0c067b
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 <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>
91 #include <linux/pkt_sched.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_V2_Unsolicited_Report_Interval (10*HZ)
118 #define IGMP_V3_Unsolicited_Report_Interval (1*HZ)
119 #define IGMP_Query_Response_Interval (10*HZ)
120 #define IGMP_Unsolicited_Report_Count 2
123 #define IGMP_Initial_Report_Delay (1)
125 /* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
132 #define IGMP_V1_SEEN(in_dev) \
133 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
134 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135 ((in_dev)->mr_v1_seen && \
136 time_before(jiffies, (in_dev)->mr_v1_seen)))
137 #define IGMP_V2_SEEN(in_dev) \
138 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140 ((in_dev)->mr_v2_seen && \
141 time_before(jiffies, (in_dev)->mr_v2_seen)))
143 static int unsolicited_report_interval(struct in_device *in_dev)
145 int interval_ms, interval_jiffies;
147 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
148 interval_ms = IN_DEV_CONF_GET(
149 in_dev,
150 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
151 else /* v3 */
152 interval_ms = IN_DEV_CONF_GET(
153 in_dev,
154 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
156 interval_jiffies = msecs_to_jiffies(interval_ms);
158 /* _timer functions can't handle a delay of 0 jiffies so ensure
159 * we always return a positive value.
161 if (interval_jiffies <= 0)
162 interval_jiffies = 1;
163 return interval_jiffies;
166 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
167 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
168 static void igmpv3_clear_delrec(struct in_device *in_dev);
169 static int sf_setstate(struct ip_mc_list *pmc);
170 static void sf_markstate(struct ip_mc_list *pmc);
171 #endif
172 static void ip_mc_clear_src(struct ip_mc_list *pmc);
173 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
174 int sfcount, __be32 *psfsrc, int delta);
176 static void ip_ma_put(struct ip_mc_list *im)
178 if (atomic_dec_and_test(&im->refcnt)) {
179 in_dev_put(im->interface);
180 kfree_rcu(im, rcu);
184 #define for_each_pmc_rcu(in_dev, pmc) \
185 for (pmc = rcu_dereference(in_dev->mc_list); \
186 pmc != NULL; \
187 pmc = rcu_dereference(pmc->next_rcu))
189 #define for_each_pmc_rtnl(in_dev, pmc) \
190 for (pmc = rtnl_dereference(in_dev->mc_list); \
191 pmc != NULL; \
192 pmc = rtnl_dereference(pmc->next_rcu))
194 #ifdef CONFIG_IP_MULTICAST
197 * Timer management
200 static void igmp_stop_timer(struct ip_mc_list *im)
202 spin_lock_bh(&im->lock);
203 if (del_timer(&im->timer))
204 atomic_dec(&im->refcnt);
205 im->tm_running = 0;
206 im->reporter = 0;
207 im->unsolicit_count = 0;
208 spin_unlock_bh(&im->lock);
211 /* It must be called with locked im->lock */
212 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
214 int tv = prandom_u32() % max_delay;
216 im->tm_running = 1;
217 if (!mod_timer(&im->timer, jiffies+tv+2))
218 atomic_inc(&im->refcnt);
221 static void igmp_gq_start_timer(struct in_device *in_dev)
223 int tv = prandom_u32() % in_dev->mr_maxdelay;
225 in_dev->mr_gq_running = 1;
226 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
227 in_dev_hold(in_dev);
230 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
232 int tv = prandom_u32() % delay;
234 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
235 in_dev_hold(in_dev);
238 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
240 spin_lock_bh(&im->lock);
241 im->unsolicit_count = 0;
242 if (del_timer(&im->timer)) {
243 if ((long)(im->timer.expires-jiffies) < max_delay) {
244 add_timer(&im->timer);
245 im->tm_running = 1;
246 spin_unlock_bh(&im->lock);
247 return;
249 atomic_dec(&im->refcnt);
251 igmp_start_timer(im, max_delay);
252 spin_unlock_bh(&im->lock);
257 * Send an IGMP report.
260 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
263 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
264 int gdeleted, int sdeleted)
266 switch (type) {
267 case IGMPV3_MODE_IS_INCLUDE:
268 case IGMPV3_MODE_IS_EXCLUDE:
269 if (gdeleted || sdeleted)
270 return 0;
271 if (!(pmc->gsquery && !psf->sf_gsresp)) {
272 if (pmc->sfmode == MCAST_INCLUDE)
273 return 1;
274 /* don't include if this source is excluded
275 * in all filters
277 if (psf->sf_count[MCAST_INCLUDE])
278 return type == IGMPV3_MODE_IS_INCLUDE;
279 return pmc->sfcount[MCAST_EXCLUDE] ==
280 psf->sf_count[MCAST_EXCLUDE];
282 return 0;
283 case IGMPV3_CHANGE_TO_INCLUDE:
284 if (gdeleted || sdeleted)
285 return 0;
286 return psf->sf_count[MCAST_INCLUDE] != 0;
287 case IGMPV3_CHANGE_TO_EXCLUDE:
288 if (gdeleted || sdeleted)
289 return 0;
290 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
291 psf->sf_count[MCAST_INCLUDE])
292 return 0;
293 return pmc->sfcount[MCAST_EXCLUDE] ==
294 psf->sf_count[MCAST_EXCLUDE];
295 case IGMPV3_ALLOW_NEW_SOURCES:
296 if (gdeleted || !psf->sf_crcount)
297 return 0;
298 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
299 case IGMPV3_BLOCK_OLD_SOURCES:
300 if (pmc->sfmode == MCAST_INCLUDE)
301 return gdeleted || (psf->sf_crcount && sdeleted);
302 return psf->sf_crcount && !gdeleted && !sdeleted;
304 return 0;
307 static int
308 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
310 struct ip_sf_list *psf;
311 int scount = 0;
313 for (psf = pmc->sources; psf; psf = psf->sf_next) {
314 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
315 continue;
316 scount++;
318 return scount;
321 #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
323 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
325 struct sk_buff *skb;
326 struct rtable *rt;
327 struct iphdr *pip;
328 struct igmpv3_report *pig;
329 struct net *net = dev_net(dev);
330 struct flowi4 fl4;
331 int hlen = LL_RESERVED_SPACE(dev);
332 int tlen = dev->needed_tailroom;
334 while (1) {
335 skb = alloc_skb(size + hlen + tlen,
336 GFP_ATOMIC | __GFP_NOWARN);
337 if (skb)
338 break;
339 size >>= 1;
340 if (size < 256)
341 return NULL;
343 skb->priority = TC_PRIO_CONTROL;
344 igmp_skb_size(skb) = size;
346 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
347 0, 0,
348 IPPROTO_IGMP, 0, dev->ifindex);
349 if (IS_ERR(rt)) {
350 kfree_skb(skb);
351 return NULL;
354 skb_dst_set(skb, &rt->dst);
355 skb->dev = dev;
357 skb_reserve(skb, hlen);
359 skb_reset_network_header(skb);
360 pip = ip_hdr(skb);
361 skb_put(skb, sizeof(struct iphdr) + 4);
363 pip->version = 4;
364 pip->ihl = (sizeof(struct iphdr)+4)>>2;
365 pip->tos = 0xc0;
366 pip->frag_off = htons(IP_DF);
367 pip->ttl = 1;
368 pip->daddr = fl4.daddr;
369 pip->saddr = fl4.saddr;
370 pip->protocol = IPPROTO_IGMP;
371 pip->tot_len = 0; /* filled in later */
372 ip_select_ident(skb, NULL);
373 ((u8 *)&pip[1])[0] = IPOPT_RA;
374 ((u8 *)&pip[1])[1] = 4;
375 ((u8 *)&pip[1])[2] = 0;
376 ((u8 *)&pip[1])[3] = 0;
378 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
379 skb_put(skb, sizeof(*pig));
380 pig = igmpv3_report_hdr(skb);
381 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
382 pig->resv1 = 0;
383 pig->csum = 0;
384 pig->resv2 = 0;
385 pig->ngrec = 0;
386 return skb;
389 static int igmpv3_sendpack(struct sk_buff *skb)
391 struct igmphdr *pig = igmp_hdr(skb);
392 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
394 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
396 return ip_local_out(skb);
399 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
401 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
404 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
405 int type, struct igmpv3_grec **ppgr)
407 struct net_device *dev = pmc->interface->dev;
408 struct igmpv3_report *pih;
409 struct igmpv3_grec *pgr;
411 if (!skb)
412 skb = igmpv3_newpack(dev, dev->mtu);
413 if (!skb)
414 return NULL;
415 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
416 pgr->grec_type = type;
417 pgr->grec_auxwords = 0;
418 pgr->grec_nsrcs = 0;
419 pgr->grec_mca = pmc->multiaddr;
420 pih = igmpv3_report_hdr(skb);
421 pih->ngrec = htons(ntohs(pih->ngrec)+1);
422 *ppgr = pgr;
423 return skb;
426 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
427 skb_tailroom(skb)) : 0)
429 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
430 int type, int gdeleted, int sdeleted)
432 struct net_device *dev = pmc->interface->dev;
433 struct igmpv3_report *pih;
434 struct igmpv3_grec *pgr = NULL;
435 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
436 int scount, stotal, first, isquery, truncate;
438 if (pmc->multiaddr == IGMP_ALL_HOSTS)
439 return skb;
441 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
442 type == IGMPV3_MODE_IS_EXCLUDE;
443 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
444 type == IGMPV3_CHANGE_TO_EXCLUDE;
446 stotal = scount = 0;
448 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
450 if (!*psf_list)
451 goto empty_source;
453 pih = skb ? igmpv3_report_hdr(skb) : NULL;
455 /* EX and TO_EX get a fresh packet, if needed */
456 if (truncate) {
457 if (pih && pih->ngrec &&
458 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
459 if (skb)
460 igmpv3_sendpack(skb);
461 skb = igmpv3_newpack(dev, dev->mtu);
464 first = 1;
465 psf_prev = NULL;
466 for (psf = *psf_list; psf; psf = psf_next) {
467 __be32 *psrc;
469 psf_next = psf->sf_next;
471 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
472 psf_prev = psf;
473 continue;
476 /* clear marks on query responses */
477 if (isquery)
478 psf->sf_gsresp = 0;
480 if (AVAILABLE(skb) < sizeof(__be32) +
481 first*sizeof(struct igmpv3_grec)) {
482 if (truncate && !first)
483 break; /* truncate these */
484 if (pgr)
485 pgr->grec_nsrcs = htons(scount);
486 if (skb)
487 igmpv3_sendpack(skb);
488 skb = igmpv3_newpack(dev, dev->mtu);
489 first = 1;
490 scount = 0;
492 if (first) {
493 skb = add_grhead(skb, pmc, type, &pgr);
494 first = 0;
496 if (!skb)
497 return NULL;
498 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
499 *psrc = psf->sf_inaddr;
500 scount++; stotal++;
501 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
502 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
503 psf->sf_crcount--;
504 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
505 if (psf_prev)
506 psf_prev->sf_next = psf->sf_next;
507 else
508 *psf_list = psf->sf_next;
509 kfree(psf);
510 continue;
513 psf_prev = psf;
516 empty_source:
517 if (!stotal) {
518 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
519 type == IGMPV3_BLOCK_OLD_SOURCES)
520 return skb;
521 if (pmc->crcount || isquery) {
522 /* make sure we have room for group header */
523 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
524 igmpv3_sendpack(skb);
525 skb = NULL; /* add_grhead will get a new one */
527 skb = add_grhead(skb, pmc, type, &pgr);
530 if (pgr)
531 pgr->grec_nsrcs = htons(scount);
533 if (isquery)
534 pmc->gsquery = 0; /* clear query state on report */
535 return skb;
538 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
540 struct sk_buff *skb = NULL;
541 int type;
543 if (!pmc) {
544 rcu_read_lock();
545 for_each_pmc_rcu(in_dev, pmc) {
546 if (pmc->multiaddr == IGMP_ALL_HOSTS)
547 continue;
548 spin_lock_bh(&pmc->lock);
549 if (pmc->sfcount[MCAST_EXCLUDE])
550 type = IGMPV3_MODE_IS_EXCLUDE;
551 else
552 type = IGMPV3_MODE_IS_INCLUDE;
553 skb = add_grec(skb, pmc, type, 0, 0);
554 spin_unlock_bh(&pmc->lock);
556 rcu_read_unlock();
557 } else {
558 spin_lock_bh(&pmc->lock);
559 if (pmc->sfcount[MCAST_EXCLUDE])
560 type = IGMPV3_MODE_IS_EXCLUDE;
561 else
562 type = IGMPV3_MODE_IS_INCLUDE;
563 skb = add_grec(skb, pmc, type, 0, 0);
564 spin_unlock_bh(&pmc->lock);
566 if (!skb)
567 return 0;
568 return igmpv3_sendpack(skb);
572 * remove zero-count source records from a source filter list
574 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
576 struct ip_sf_list *psf_prev, *psf_next, *psf;
578 psf_prev = NULL;
579 for (psf = *ppsf; psf; psf = psf_next) {
580 psf_next = psf->sf_next;
581 if (psf->sf_crcount == 0) {
582 if (psf_prev)
583 psf_prev->sf_next = psf->sf_next;
584 else
585 *ppsf = psf->sf_next;
586 kfree(psf);
587 } else
588 psf_prev = psf;
592 static void igmpv3_send_cr(struct in_device *in_dev)
594 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
595 struct sk_buff *skb = NULL;
596 int type, dtype;
598 rcu_read_lock();
599 spin_lock_bh(&in_dev->mc_tomb_lock);
601 /* deleted MCA's */
602 pmc_prev = NULL;
603 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
604 pmc_next = pmc->next;
605 if (pmc->sfmode == MCAST_INCLUDE) {
606 type = IGMPV3_BLOCK_OLD_SOURCES;
607 dtype = IGMPV3_BLOCK_OLD_SOURCES;
608 skb = add_grec(skb, pmc, type, 1, 0);
609 skb = add_grec(skb, pmc, dtype, 1, 1);
611 if (pmc->crcount) {
612 if (pmc->sfmode == MCAST_EXCLUDE) {
613 type = IGMPV3_CHANGE_TO_INCLUDE;
614 skb = add_grec(skb, pmc, type, 1, 0);
616 pmc->crcount--;
617 if (pmc->crcount == 0) {
618 igmpv3_clear_zeros(&pmc->tomb);
619 igmpv3_clear_zeros(&pmc->sources);
622 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
623 if (pmc_prev)
624 pmc_prev->next = pmc_next;
625 else
626 in_dev->mc_tomb = pmc_next;
627 in_dev_put(pmc->interface);
628 kfree(pmc);
629 } else
630 pmc_prev = pmc;
632 spin_unlock_bh(&in_dev->mc_tomb_lock);
634 /* change recs */
635 for_each_pmc_rcu(in_dev, pmc) {
636 spin_lock_bh(&pmc->lock);
637 if (pmc->sfcount[MCAST_EXCLUDE]) {
638 type = IGMPV3_BLOCK_OLD_SOURCES;
639 dtype = IGMPV3_ALLOW_NEW_SOURCES;
640 } else {
641 type = IGMPV3_ALLOW_NEW_SOURCES;
642 dtype = IGMPV3_BLOCK_OLD_SOURCES;
644 skb = add_grec(skb, pmc, type, 0, 0);
645 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
647 /* filter mode changes */
648 if (pmc->crcount) {
649 if (pmc->sfmode == MCAST_EXCLUDE)
650 type = IGMPV3_CHANGE_TO_EXCLUDE;
651 else
652 type = IGMPV3_CHANGE_TO_INCLUDE;
653 skb = add_grec(skb, pmc, type, 0, 0);
654 pmc->crcount--;
656 spin_unlock_bh(&pmc->lock);
658 rcu_read_unlock();
660 if (!skb)
661 return;
662 (void) igmpv3_sendpack(skb);
665 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
666 int type)
668 struct sk_buff *skb;
669 struct iphdr *iph;
670 struct igmphdr *ih;
671 struct rtable *rt;
672 struct net_device *dev = in_dev->dev;
673 struct net *net = dev_net(dev);
674 __be32 group = pmc ? pmc->multiaddr : 0;
675 struct flowi4 fl4;
676 __be32 dst;
677 int hlen, tlen;
679 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
680 return igmpv3_send_report(in_dev, pmc);
681 else if (type == IGMP_HOST_LEAVE_MESSAGE)
682 dst = IGMP_ALL_ROUTER;
683 else
684 dst = group;
686 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
687 0, 0,
688 IPPROTO_IGMP, 0, dev->ifindex);
689 if (IS_ERR(rt))
690 return -1;
692 hlen = LL_RESERVED_SPACE(dev);
693 tlen = dev->needed_tailroom;
694 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
695 if (skb == NULL) {
696 ip_rt_put(rt);
697 return -1;
699 skb->priority = TC_PRIO_CONTROL;
701 skb_dst_set(skb, &rt->dst);
703 skb_reserve(skb, hlen);
705 skb_reset_network_header(skb);
706 iph = ip_hdr(skb);
707 skb_put(skb, sizeof(struct iphdr) + 4);
709 iph->version = 4;
710 iph->ihl = (sizeof(struct iphdr)+4)>>2;
711 iph->tos = 0xc0;
712 iph->frag_off = htons(IP_DF);
713 iph->ttl = 1;
714 iph->daddr = dst;
715 iph->saddr = fl4.saddr;
716 iph->protocol = IPPROTO_IGMP;
717 ip_select_ident(skb, NULL);
718 ((u8 *)&iph[1])[0] = IPOPT_RA;
719 ((u8 *)&iph[1])[1] = 4;
720 ((u8 *)&iph[1])[2] = 0;
721 ((u8 *)&iph[1])[3] = 0;
723 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
724 ih->type = type;
725 ih->code = 0;
726 ih->csum = 0;
727 ih->group = group;
728 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
730 return ip_local_out(skb);
733 static void igmp_gq_timer_expire(unsigned long data)
735 struct in_device *in_dev = (struct in_device *)data;
737 in_dev->mr_gq_running = 0;
738 igmpv3_send_report(in_dev, NULL);
739 in_dev_put(in_dev);
742 static void igmp_ifc_timer_expire(unsigned long data)
744 struct in_device *in_dev = (struct in_device *)data;
746 igmpv3_send_cr(in_dev);
747 if (in_dev->mr_ifc_count) {
748 in_dev->mr_ifc_count--;
749 igmp_ifc_start_timer(in_dev,
750 unsolicited_report_interval(in_dev));
752 in_dev_put(in_dev);
755 static void igmp_ifc_event(struct in_device *in_dev)
757 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
758 return;
759 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
760 IGMP_Unsolicited_Report_Count;
761 igmp_ifc_start_timer(in_dev, 1);
765 static void igmp_timer_expire(unsigned long data)
767 struct ip_mc_list *im = (struct ip_mc_list *)data;
768 struct in_device *in_dev = im->interface;
770 spin_lock(&im->lock);
771 im->tm_running = 0;
773 if (im->unsolicit_count) {
774 im->unsolicit_count--;
775 igmp_start_timer(im, unsolicited_report_interval(in_dev));
777 im->reporter = 1;
778 spin_unlock(&im->lock);
780 if (IGMP_V1_SEEN(in_dev))
781 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
782 else if (IGMP_V2_SEEN(in_dev))
783 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
784 else
785 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
787 ip_ma_put(im);
790 /* mark EXCLUDE-mode sources */
791 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
793 struct ip_sf_list *psf;
794 int i, scount;
796 scount = 0;
797 for (psf = pmc->sources; psf; psf = psf->sf_next) {
798 if (scount == nsrcs)
799 break;
800 for (i = 0; i < nsrcs; i++) {
801 /* skip inactive filters */
802 if (psf->sf_count[MCAST_INCLUDE] ||
803 pmc->sfcount[MCAST_EXCLUDE] !=
804 psf->sf_count[MCAST_EXCLUDE])
805 break;
806 if (srcs[i] == psf->sf_inaddr) {
807 scount++;
808 break;
812 pmc->gsquery = 0;
813 if (scount == nsrcs) /* all sources excluded */
814 return 0;
815 return 1;
818 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
820 struct ip_sf_list *psf;
821 int i, scount;
823 if (pmc->sfmode == MCAST_EXCLUDE)
824 return igmp_xmarksources(pmc, nsrcs, srcs);
826 /* mark INCLUDE-mode sources */
827 scount = 0;
828 for (psf = pmc->sources; psf; psf = psf->sf_next) {
829 if (scount == nsrcs)
830 break;
831 for (i = 0; i < nsrcs; i++)
832 if (srcs[i] == psf->sf_inaddr) {
833 psf->sf_gsresp = 1;
834 scount++;
835 break;
838 if (!scount) {
839 pmc->gsquery = 0;
840 return 0;
842 pmc->gsquery = 1;
843 return 1;
846 /* return true if packet was dropped */
847 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
849 struct ip_mc_list *im;
851 /* Timers are only set for non-local groups */
853 if (group == IGMP_ALL_HOSTS)
854 return false;
856 rcu_read_lock();
857 for_each_pmc_rcu(in_dev, im) {
858 if (im->multiaddr == group) {
859 igmp_stop_timer(im);
860 break;
863 rcu_read_unlock();
864 return false;
867 /* return true if packet was dropped */
868 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
869 int len)
871 struct igmphdr *ih = igmp_hdr(skb);
872 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
873 struct ip_mc_list *im;
874 __be32 group = ih->group;
875 int max_delay;
876 int mark = 0;
879 if (len == 8) {
880 if (ih->code == 0) {
881 /* Alas, old v1 router presents here. */
883 max_delay = IGMP_Query_Response_Interval;
884 in_dev->mr_v1_seen = jiffies +
885 IGMP_V1_Router_Present_Timeout;
886 group = 0;
887 } else {
888 /* v2 router present */
889 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
890 in_dev->mr_v2_seen = jiffies +
891 IGMP_V2_Router_Present_Timeout;
893 /* cancel the interface change timer */
894 in_dev->mr_ifc_count = 0;
895 if (del_timer(&in_dev->mr_ifc_timer))
896 __in_dev_put(in_dev);
897 /* clear deleted report items */
898 igmpv3_clear_delrec(in_dev);
899 } else if (len < 12) {
900 return true; /* ignore bogus packet; freed by caller */
901 } else if (IGMP_V1_SEEN(in_dev)) {
902 /* This is a v3 query with v1 queriers present */
903 max_delay = IGMP_Query_Response_Interval;
904 group = 0;
905 } else if (IGMP_V2_SEEN(in_dev)) {
906 /* this is a v3 query with v2 queriers present;
907 * Interpretation of the max_delay code is problematic here.
908 * A real v2 host would use ih_code directly, while v3 has a
909 * different encoding. We use the v3 encoding as more likely
910 * to be intended in a v3 query.
912 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
913 if (!max_delay)
914 max_delay = 1; /* can't mod w/ 0 */
915 } else { /* v3 */
916 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
917 return true;
919 ih3 = igmpv3_query_hdr(skb);
920 if (ih3->nsrcs) {
921 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
922 + ntohs(ih3->nsrcs)*sizeof(__be32)))
923 return true;
924 ih3 = igmpv3_query_hdr(skb);
927 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
928 if (!max_delay)
929 max_delay = 1; /* can't mod w/ 0 */
930 in_dev->mr_maxdelay = max_delay;
931 if (ih3->qrv)
932 in_dev->mr_qrv = ih3->qrv;
933 if (!group) { /* general query */
934 if (ih3->nsrcs)
935 return false; /* no sources allowed */
936 igmp_gq_start_timer(in_dev);
937 return false;
939 /* mark sources to include, if group & source-specific */
940 mark = ih3->nsrcs != 0;
944 * - Start the timers in all of our membership records
945 * that the query applies to for the interface on
946 * which the query arrived excl. those that belong
947 * to a "local" group (224.0.0.X)
948 * - For timers already running check if they need to
949 * be reset.
950 * - Use the igmp->igmp_code field as the maximum
951 * delay possible
953 rcu_read_lock();
954 for_each_pmc_rcu(in_dev, im) {
955 int changed;
957 if (group && group != im->multiaddr)
958 continue;
959 if (im->multiaddr == IGMP_ALL_HOSTS)
960 continue;
961 spin_lock_bh(&im->lock);
962 if (im->tm_running)
963 im->gsquery = im->gsquery && mark;
964 else
965 im->gsquery = mark;
966 changed = !im->gsquery ||
967 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
968 spin_unlock_bh(&im->lock);
969 if (changed)
970 igmp_mod_timer(im, max_delay);
972 rcu_read_unlock();
973 return false;
976 /* called in rcu_read_lock() section */
977 int igmp_rcv(struct sk_buff *skb)
979 /* This basically follows the spec line by line -- see RFC1112 */
980 struct igmphdr *ih;
981 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
982 int len = skb->len;
983 bool dropped = true;
985 if (in_dev == NULL)
986 goto drop;
988 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
989 goto drop;
991 if (skb_checksum_simple_validate(skb))
992 goto drop;
994 ih = igmp_hdr(skb);
995 switch (ih->type) {
996 case IGMP_HOST_MEMBERSHIP_QUERY:
997 dropped = igmp_heard_query(in_dev, skb, len);
998 break;
999 case IGMP_HOST_MEMBERSHIP_REPORT:
1000 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1001 /* Is it our report looped back? */
1002 if (rt_is_output_route(skb_rtable(skb)))
1003 break;
1004 /* don't rely on MC router hearing unicast reports */
1005 if (skb->pkt_type == PACKET_MULTICAST ||
1006 skb->pkt_type == PACKET_BROADCAST)
1007 dropped = igmp_heard_report(in_dev, ih->group);
1008 break;
1009 case IGMP_PIM:
1010 #ifdef CONFIG_IP_PIMSM_V1
1011 return pim_rcv_v1(skb);
1012 #endif
1013 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1014 case IGMP_DVMRP:
1015 case IGMP_TRACE:
1016 case IGMP_HOST_LEAVE_MESSAGE:
1017 case IGMP_MTRACE:
1018 case IGMP_MTRACE_RESP:
1019 break;
1020 default:
1021 break;
1024 drop:
1025 if (dropped)
1026 kfree_skb(skb);
1027 else
1028 consume_skb(skb);
1029 return 0;
1032 #endif
1036 * Add a filter to a device
1039 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1041 char buf[MAX_ADDR_LEN];
1042 struct net_device *dev = in_dev->dev;
1044 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1045 We will get multicast token leakage, when IFF_MULTICAST
1046 is changed. This check should be done in ndo_set_rx_mode
1047 routine. Something sort of:
1048 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1049 --ANK
1051 if (arp_mc_map(addr, buf, dev, 0) == 0)
1052 dev_mc_add(dev, buf);
1056 * Remove a filter from a device
1059 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1061 char buf[MAX_ADDR_LEN];
1062 struct net_device *dev = in_dev->dev;
1064 if (arp_mc_map(addr, buf, dev, 0) == 0)
1065 dev_mc_del(dev, buf);
1068 #ifdef CONFIG_IP_MULTICAST
1070 * deleted ip_mc_list manipulation
1072 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1074 struct ip_mc_list *pmc;
1076 /* this is an "ip_mc_list" for convenience; only the fields below
1077 * are actually used. In particular, the refcnt and users are not
1078 * used for management of the delete list. Using the same structure
1079 * for deleted items allows change reports to use common code with
1080 * non-deleted or query-response MCA's.
1082 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1083 if (!pmc)
1084 return;
1085 spin_lock_bh(&im->lock);
1086 pmc->interface = im->interface;
1087 in_dev_hold(in_dev);
1088 pmc->multiaddr = im->multiaddr;
1089 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1090 IGMP_Unsolicited_Report_Count;
1091 pmc->sfmode = im->sfmode;
1092 if (pmc->sfmode == MCAST_INCLUDE) {
1093 struct ip_sf_list *psf;
1095 pmc->tomb = im->tomb;
1096 pmc->sources = im->sources;
1097 im->tomb = im->sources = NULL;
1098 for (psf = pmc->sources; psf; psf = psf->sf_next)
1099 psf->sf_crcount = pmc->crcount;
1101 spin_unlock_bh(&im->lock);
1103 spin_lock_bh(&in_dev->mc_tomb_lock);
1104 pmc->next = in_dev->mc_tomb;
1105 in_dev->mc_tomb = pmc;
1106 spin_unlock_bh(&in_dev->mc_tomb_lock);
1109 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1111 struct ip_mc_list *pmc, *pmc_prev;
1112 struct ip_sf_list *psf, *psf_next;
1114 spin_lock_bh(&in_dev->mc_tomb_lock);
1115 pmc_prev = NULL;
1116 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1117 if (pmc->multiaddr == multiaddr)
1118 break;
1119 pmc_prev = pmc;
1121 if (pmc) {
1122 if (pmc_prev)
1123 pmc_prev->next = pmc->next;
1124 else
1125 in_dev->mc_tomb = pmc->next;
1127 spin_unlock_bh(&in_dev->mc_tomb_lock);
1128 if (pmc) {
1129 for (psf = pmc->tomb; psf; psf = psf_next) {
1130 psf_next = psf->sf_next;
1131 kfree(psf);
1133 in_dev_put(pmc->interface);
1134 kfree(pmc);
1138 static void igmpv3_clear_delrec(struct in_device *in_dev)
1140 struct ip_mc_list *pmc, *nextpmc;
1142 spin_lock_bh(&in_dev->mc_tomb_lock);
1143 pmc = in_dev->mc_tomb;
1144 in_dev->mc_tomb = NULL;
1145 spin_unlock_bh(&in_dev->mc_tomb_lock);
1147 for (; pmc; pmc = nextpmc) {
1148 nextpmc = pmc->next;
1149 ip_mc_clear_src(pmc);
1150 in_dev_put(pmc->interface);
1151 kfree(pmc);
1153 /* clear dead sources, too */
1154 rcu_read_lock();
1155 for_each_pmc_rcu(in_dev, pmc) {
1156 struct ip_sf_list *psf, *psf_next;
1158 spin_lock_bh(&pmc->lock);
1159 psf = pmc->tomb;
1160 pmc->tomb = NULL;
1161 spin_unlock_bh(&pmc->lock);
1162 for (; psf; psf = psf_next) {
1163 psf_next = psf->sf_next;
1164 kfree(psf);
1167 rcu_read_unlock();
1169 #endif
1171 static void igmp_group_dropped(struct ip_mc_list *im)
1173 struct in_device *in_dev = im->interface;
1174 #ifdef CONFIG_IP_MULTICAST
1175 int reporter;
1176 #endif
1178 if (im->loaded) {
1179 im->loaded = 0;
1180 ip_mc_filter_del(in_dev, im->multiaddr);
1183 #ifdef CONFIG_IP_MULTICAST
1184 if (im->multiaddr == IGMP_ALL_HOSTS)
1185 return;
1187 reporter = im->reporter;
1188 igmp_stop_timer(im);
1190 if (!in_dev->dead) {
1191 if (IGMP_V1_SEEN(in_dev))
1192 return;
1193 if (IGMP_V2_SEEN(in_dev)) {
1194 if (reporter)
1195 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1196 return;
1198 /* IGMPv3 */
1199 igmpv3_add_delrec(in_dev, im);
1201 igmp_ifc_event(in_dev);
1203 #endif
1206 static void igmp_group_added(struct ip_mc_list *im)
1208 struct in_device *in_dev = im->interface;
1210 if (im->loaded == 0) {
1211 im->loaded = 1;
1212 ip_mc_filter_add(in_dev, im->multiaddr);
1215 #ifdef CONFIG_IP_MULTICAST
1216 if (im->multiaddr == IGMP_ALL_HOSTS)
1217 return;
1219 if (in_dev->dead)
1220 return;
1221 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1222 spin_lock_bh(&im->lock);
1223 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1224 spin_unlock_bh(&im->lock);
1225 return;
1227 /* else, v3 */
1229 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1230 IGMP_Unsolicited_Report_Count;
1231 igmp_ifc_event(in_dev);
1232 #endif
1237 * Multicast list managers
1240 static u32 ip_mc_hash(const struct ip_mc_list *im)
1242 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1245 static void ip_mc_hash_add(struct in_device *in_dev,
1246 struct ip_mc_list *im)
1248 struct ip_mc_list __rcu **mc_hash;
1249 u32 hash;
1251 mc_hash = rtnl_dereference(in_dev->mc_hash);
1252 if (mc_hash) {
1253 hash = ip_mc_hash(im);
1254 im->next_hash = mc_hash[hash];
1255 rcu_assign_pointer(mc_hash[hash], im);
1256 return;
1259 /* do not use a hash table for small number of items */
1260 if (in_dev->mc_count < 4)
1261 return;
1263 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1264 GFP_KERNEL);
1265 if (!mc_hash)
1266 return;
1268 for_each_pmc_rtnl(in_dev, im) {
1269 hash = ip_mc_hash(im);
1270 im->next_hash = mc_hash[hash];
1271 RCU_INIT_POINTER(mc_hash[hash], im);
1274 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1277 static void ip_mc_hash_remove(struct in_device *in_dev,
1278 struct ip_mc_list *im)
1280 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1281 struct ip_mc_list *aux;
1283 if (!mc_hash)
1284 return;
1285 mc_hash += ip_mc_hash(im);
1286 while ((aux = rtnl_dereference(*mc_hash)) != im)
1287 mc_hash = &aux->next_hash;
1288 *mc_hash = im->next_hash;
1293 * A socket has joined a multicast group on device dev.
1296 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1298 struct ip_mc_list *im;
1300 ASSERT_RTNL();
1302 for_each_pmc_rtnl(in_dev, im) {
1303 if (im->multiaddr == addr) {
1304 im->users++;
1305 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1306 goto out;
1310 im = kzalloc(sizeof(*im), GFP_KERNEL);
1311 if (!im)
1312 goto out;
1314 im->users = 1;
1315 im->interface = in_dev;
1316 in_dev_hold(in_dev);
1317 im->multiaddr = addr;
1318 /* initial mode is (EX, empty) */
1319 im->sfmode = MCAST_EXCLUDE;
1320 im->sfcount[MCAST_EXCLUDE] = 1;
1321 atomic_set(&im->refcnt, 1);
1322 spin_lock_init(&im->lock);
1323 #ifdef CONFIG_IP_MULTICAST
1324 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1325 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1326 #endif
1328 im->next_rcu = in_dev->mc_list;
1329 in_dev->mc_count++;
1330 rcu_assign_pointer(in_dev->mc_list, im);
1332 ip_mc_hash_add(in_dev, im);
1334 #ifdef CONFIG_IP_MULTICAST
1335 igmpv3_del_delrec(in_dev, im->multiaddr);
1336 #endif
1337 igmp_group_added(im);
1338 if (!in_dev->dead)
1339 ip_rt_multicast_event(in_dev);
1340 out:
1341 return;
1343 EXPORT_SYMBOL(ip_mc_inc_group);
1346 * Resend IGMP JOIN report; used by netdev notifier.
1348 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1350 #ifdef CONFIG_IP_MULTICAST
1351 struct ip_mc_list *im;
1352 int type;
1354 ASSERT_RTNL();
1356 for_each_pmc_rtnl(in_dev, im) {
1357 if (im->multiaddr == IGMP_ALL_HOSTS)
1358 continue;
1360 /* a failover is happening and switches
1361 * must be notified immediately
1363 if (IGMP_V1_SEEN(in_dev))
1364 type = IGMP_HOST_MEMBERSHIP_REPORT;
1365 else if (IGMP_V2_SEEN(in_dev))
1366 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1367 else
1368 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1369 igmp_send_report(in_dev, im, type);
1371 #endif
1375 * A socket has left a multicast group on device dev
1378 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1380 struct ip_mc_list *i;
1381 struct ip_mc_list __rcu **ip;
1383 ASSERT_RTNL();
1385 for (ip = &in_dev->mc_list;
1386 (i = rtnl_dereference(*ip)) != NULL;
1387 ip = &i->next_rcu) {
1388 if (i->multiaddr == addr) {
1389 if (--i->users == 0) {
1390 ip_mc_hash_remove(in_dev, i);
1391 *ip = i->next_rcu;
1392 in_dev->mc_count--;
1393 igmp_group_dropped(i);
1394 ip_mc_clear_src(i);
1396 if (!in_dev->dead)
1397 ip_rt_multicast_event(in_dev);
1399 ip_ma_put(i);
1400 return;
1402 break;
1406 EXPORT_SYMBOL(ip_mc_dec_group);
1408 /* Device changing type */
1410 void ip_mc_unmap(struct in_device *in_dev)
1412 struct ip_mc_list *pmc;
1414 ASSERT_RTNL();
1416 for_each_pmc_rtnl(in_dev, pmc)
1417 igmp_group_dropped(pmc);
1420 void ip_mc_remap(struct in_device *in_dev)
1422 struct ip_mc_list *pmc;
1424 ASSERT_RTNL();
1426 for_each_pmc_rtnl(in_dev, pmc)
1427 igmp_group_added(pmc);
1430 /* Device going down */
1432 void ip_mc_down(struct in_device *in_dev)
1434 struct ip_mc_list *pmc;
1436 ASSERT_RTNL();
1438 for_each_pmc_rtnl(in_dev, pmc)
1439 igmp_group_dropped(pmc);
1441 #ifdef CONFIG_IP_MULTICAST
1442 in_dev->mr_ifc_count = 0;
1443 if (del_timer(&in_dev->mr_ifc_timer))
1444 __in_dev_put(in_dev);
1445 in_dev->mr_gq_running = 0;
1446 if (del_timer(&in_dev->mr_gq_timer))
1447 __in_dev_put(in_dev);
1448 igmpv3_clear_delrec(in_dev);
1449 #endif
1451 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1454 void ip_mc_init_dev(struct in_device *in_dev)
1456 ASSERT_RTNL();
1458 #ifdef CONFIG_IP_MULTICAST
1459 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1460 (unsigned long)in_dev);
1461 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1462 (unsigned long)in_dev);
1463 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1464 #endif
1466 spin_lock_init(&in_dev->mc_tomb_lock);
1469 /* Device going up */
1471 void ip_mc_up(struct in_device *in_dev)
1473 struct ip_mc_list *pmc;
1475 ASSERT_RTNL();
1477 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1479 for_each_pmc_rtnl(in_dev, pmc)
1480 igmp_group_added(pmc);
1484 * Device is about to be destroyed: clean up.
1487 void ip_mc_destroy_dev(struct in_device *in_dev)
1489 struct ip_mc_list *i;
1491 ASSERT_RTNL();
1493 /* Deactivate timers */
1494 ip_mc_down(in_dev);
1496 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1497 in_dev->mc_list = i->next_rcu;
1498 in_dev->mc_count--;
1500 /* We've dropped the groups in ip_mc_down already */
1501 ip_mc_clear_src(i);
1502 ip_ma_put(i);
1506 /* RTNL is locked */
1507 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1509 struct net_device *dev = NULL;
1510 struct in_device *idev = NULL;
1512 if (imr->imr_ifindex) {
1513 idev = inetdev_by_index(net, imr->imr_ifindex);
1514 return idev;
1516 if (imr->imr_address.s_addr) {
1517 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1518 if (!dev)
1519 return NULL;
1522 if (!dev) {
1523 struct rtable *rt = ip_route_output(net,
1524 imr->imr_multiaddr.s_addr,
1525 0, 0, 0);
1526 if (!IS_ERR(rt)) {
1527 dev = rt->dst.dev;
1528 ip_rt_put(rt);
1531 if (dev) {
1532 imr->imr_ifindex = dev->ifindex;
1533 idev = __in_dev_get_rtnl(dev);
1535 return idev;
1539 * Join a socket to a group
1541 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1542 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1545 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1546 __be32 *psfsrc)
1548 struct ip_sf_list *psf, *psf_prev;
1549 int rv = 0;
1551 psf_prev = NULL;
1552 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1553 if (psf->sf_inaddr == *psfsrc)
1554 break;
1555 psf_prev = psf;
1557 if (!psf || psf->sf_count[sfmode] == 0) {
1558 /* source filter not found, or count wrong => bug */
1559 return -ESRCH;
1561 psf->sf_count[sfmode]--;
1562 if (psf->sf_count[sfmode] == 0) {
1563 ip_rt_multicast_event(pmc->interface);
1565 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1566 #ifdef CONFIG_IP_MULTICAST
1567 struct in_device *in_dev = pmc->interface;
1568 #endif
1570 /* no more filters for this source */
1571 if (psf_prev)
1572 psf_prev->sf_next = psf->sf_next;
1573 else
1574 pmc->sources = psf->sf_next;
1575 #ifdef CONFIG_IP_MULTICAST
1576 if (psf->sf_oldin &&
1577 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1578 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1579 IGMP_Unsolicited_Report_Count;
1580 psf->sf_next = pmc->tomb;
1581 pmc->tomb = psf;
1582 rv = 1;
1583 } else
1584 #endif
1585 kfree(psf);
1587 return rv;
1590 #ifndef CONFIG_IP_MULTICAST
1591 #define igmp_ifc_event(x) do { } while (0)
1592 #endif
1594 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1595 int sfcount, __be32 *psfsrc, int delta)
1597 struct ip_mc_list *pmc;
1598 int changerec = 0;
1599 int i, err;
1601 if (!in_dev)
1602 return -ENODEV;
1603 rcu_read_lock();
1604 for_each_pmc_rcu(in_dev, pmc) {
1605 if (*pmca == pmc->multiaddr)
1606 break;
1608 if (!pmc) {
1609 /* MCA not found?? bug */
1610 rcu_read_unlock();
1611 return -ESRCH;
1613 spin_lock_bh(&pmc->lock);
1614 rcu_read_unlock();
1615 #ifdef CONFIG_IP_MULTICAST
1616 sf_markstate(pmc);
1617 #endif
1618 if (!delta) {
1619 err = -EINVAL;
1620 if (!pmc->sfcount[sfmode])
1621 goto out_unlock;
1622 pmc->sfcount[sfmode]--;
1624 err = 0;
1625 for (i = 0; i < sfcount; i++) {
1626 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1628 changerec |= rv > 0;
1629 if (!err && rv < 0)
1630 err = rv;
1632 if (pmc->sfmode == MCAST_EXCLUDE &&
1633 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1634 pmc->sfcount[MCAST_INCLUDE]) {
1635 #ifdef CONFIG_IP_MULTICAST
1636 struct ip_sf_list *psf;
1637 #endif
1639 /* filter mode change */
1640 pmc->sfmode = MCAST_INCLUDE;
1641 #ifdef CONFIG_IP_MULTICAST
1642 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1643 IGMP_Unsolicited_Report_Count;
1644 in_dev->mr_ifc_count = pmc->crcount;
1645 for (psf = pmc->sources; psf; psf = psf->sf_next)
1646 psf->sf_crcount = 0;
1647 igmp_ifc_event(pmc->interface);
1648 } else if (sf_setstate(pmc) || changerec) {
1649 igmp_ifc_event(pmc->interface);
1650 #endif
1652 out_unlock:
1653 spin_unlock_bh(&pmc->lock);
1654 return err;
1658 * Add multicast single-source filter to the interface list
1660 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1661 __be32 *psfsrc)
1663 struct ip_sf_list *psf, *psf_prev;
1665 psf_prev = NULL;
1666 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1667 if (psf->sf_inaddr == *psfsrc)
1668 break;
1669 psf_prev = psf;
1671 if (!psf) {
1672 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1673 if (!psf)
1674 return -ENOBUFS;
1675 psf->sf_inaddr = *psfsrc;
1676 if (psf_prev) {
1677 psf_prev->sf_next = psf;
1678 } else
1679 pmc->sources = psf;
1681 psf->sf_count[sfmode]++;
1682 if (psf->sf_count[sfmode] == 1) {
1683 ip_rt_multicast_event(pmc->interface);
1685 return 0;
1688 #ifdef CONFIG_IP_MULTICAST
1689 static void sf_markstate(struct ip_mc_list *pmc)
1691 struct ip_sf_list *psf;
1692 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1694 for (psf = pmc->sources; psf; psf = psf->sf_next)
1695 if (pmc->sfcount[MCAST_EXCLUDE]) {
1696 psf->sf_oldin = mca_xcount ==
1697 psf->sf_count[MCAST_EXCLUDE] &&
1698 !psf->sf_count[MCAST_INCLUDE];
1699 } else
1700 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1703 static int sf_setstate(struct ip_mc_list *pmc)
1705 struct ip_sf_list *psf, *dpsf;
1706 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1707 int qrv = pmc->interface->mr_qrv;
1708 int new_in, rv;
1710 rv = 0;
1711 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1712 if (pmc->sfcount[MCAST_EXCLUDE]) {
1713 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1714 !psf->sf_count[MCAST_INCLUDE];
1715 } else
1716 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1717 if (new_in) {
1718 if (!psf->sf_oldin) {
1719 struct ip_sf_list *prev = NULL;
1721 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
1722 if (dpsf->sf_inaddr == psf->sf_inaddr)
1723 break;
1724 prev = dpsf;
1726 if (dpsf) {
1727 if (prev)
1728 prev->sf_next = dpsf->sf_next;
1729 else
1730 pmc->tomb = dpsf->sf_next;
1731 kfree(dpsf);
1733 psf->sf_crcount = qrv;
1734 rv++;
1736 } else if (psf->sf_oldin) {
1738 psf->sf_crcount = 0;
1740 * add or update "delete" records if an active filter
1741 * is now inactive
1743 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
1744 if (dpsf->sf_inaddr == psf->sf_inaddr)
1745 break;
1746 if (!dpsf) {
1747 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1748 if (!dpsf)
1749 continue;
1750 *dpsf = *psf;
1751 /* pmc->lock held by callers */
1752 dpsf->sf_next = pmc->tomb;
1753 pmc->tomb = dpsf;
1755 dpsf->sf_crcount = qrv;
1756 rv++;
1759 return rv;
1761 #endif
1764 * Add multicast source filter list to the interface list
1766 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1767 int sfcount, __be32 *psfsrc, int delta)
1769 struct ip_mc_list *pmc;
1770 int isexclude;
1771 int i, err;
1773 if (!in_dev)
1774 return -ENODEV;
1775 rcu_read_lock();
1776 for_each_pmc_rcu(in_dev, pmc) {
1777 if (*pmca == pmc->multiaddr)
1778 break;
1780 if (!pmc) {
1781 /* MCA not found?? bug */
1782 rcu_read_unlock();
1783 return -ESRCH;
1785 spin_lock_bh(&pmc->lock);
1786 rcu_read_unlock();
1788 #ifdef CONFIG_IP_MULTICAST
1789 sf_markstate(pmc);
1790 #endif
1791 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1792 if (!delta)
1793 pmc->sfcount[sfmode]++;
1794 err = 0;
1795 for (i = 0; i < sfcount; i++) {
1796 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
1797 if (err)
1798 break;
1800 if (err) {
1801 int j;
1803 if (!delta)
1804 pmc->sfcount[sfmode]--;
1805 for (j = 0; j < i; j++)
1806 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
1807 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1808 #ifdef CONFIG_IP_MULTICAST
1809 struct ip_sf_list *psf;
1810 in_dev = pmc->interface;
1811 #endif
1813 /* filter mode change */
1814 if (pmc->sfcount[MCAST_EXCLUDE])
1815 pmc->sfmode = MCAST_EXCLUDE;
1816 else if (pmc->sfcount[MCAST_INCLUDE])
1817 pmc->sfmode = MCAST_INCLUDE;
1818 #ifdef CONFIG_IP_MULTICAST
1819 /* else no filters; keep old mode for reports */
1821 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1822 IGMP_Unsolicited_Report_Count;
1823 in_dev->mr_ifc_count = pmc->crcount;
1824 for (psf = pmc->sources; psf; psf = psf->sf_next)
1825 psf->sf_crcount = 0;
1826 igmp_ifc_event(in_dev);
1827 } else if (sf_setstate(pmc)) {
1828 igmp_ifc_event(in_dev);
1829 #endif
1831 spin_unlock_bh(&pmc->lock);
1832 return err;
1835 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1837 struct ip_sf_list *psf, *nextpsf;
1839 for (psf = pmc->tomb; psf; psf = nextpsf) {
1840 nextpsf = psf->sf_next;
1841 kfree(psf);
1843 pmc->tomb = NULL;
1844 for (psf = pmc->sources; psf; psf = nextpsf) {
1845 nextpsf = psf->sf_next;
1846 kfree(psf);
1848 pmc->sources = NULL;
1849 pmc->sfmode = MCAST_EXCLUDE;
1850 pmc->sfcount[MCAST_INCLUDE] = 0;
1851 pmc->sfcount[MCAST_EXCLUDE] = 1;
1856 * Join a multicast group
1858 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1860 int err;
1861 __be32 addr = imr->imr_multiaddr.s_addr;
1862 struct ip_mc_socklist *iml = NULL, *i;
1863 struct in_device *in_dev;
1864 struct inet_sock *inet = inet_sk(sk);
1865 struct net *net = sock_net(sk);
1866 int ifindex;
1867 int count = 0;
1869 if (!ipv4_is_multicast(addr))
1870 return -EINVAL;
1872 rtnl_lock();
1874 in_dev = ip_mc_find_dev(net, imr);
1876 if (!in_dev) {
1877 iml = NULL;
1878 err = -ENODEV;
1879 goto done;
1882 err = -EADDRINUSE;
1883 ifindex = imr->imr_ifindex;
1884 for_each_pmc_rtnl(inet, i) {
1885 if (i->multi.imr_multiaddr.s_addr == addr &&
1886 i->multi.imr_ifindex == ifindex)
1887 goto done;
1888 count++;
1890 err = -ENOBUFS;
1891 if (count >= sysctl_igmp_max_memberships)
1892 goto done;
1893 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1894 if (iml == NULL)
1895 goto done;
1897 memcpy(&iml->multi, imr, sizeof(*imr));
1898 iml->next_rcu = inet->mc_list;
1899 iml->sflist = NULL;
1900 iml->sfmode = MCAST_EXCLUDE;
1901 rcu_assign_pointer(inet->mc_list, iml);
1902 ip_mc_inc_group(in_dev, addr);
1903 err = 0;
1904 done:
1905 rtnl_unlock();
1906 return err;
1908 EXPORT_SYMBOL(ip_mc_join_group);
1910 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1911 struct in_device *in_dev)
1913 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1914 int err;
1916 if (psf == NULL) {
1917 /* any-source empty exclude case */
1918 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1919 iml->sfmode, 0, NULL, 0);
1921 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1922 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1923 RCU_INIT_POINTER(iml->sflist, NULL);
1924 /* decrease mem now to avoid the memleak warning */
1925 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1926 kfree_rcu(psf, rcu);
1927 return err;
1931 * Ask a socket to leave a group.
1934 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1936 struct inet_sock *inet = inet_sk(sk);
1937 struct ip_mc_socklist *iml;
1938 struct ip_mc_socklist __rcu **imlp;
1939 struct in_device *in_dev;
1940 struct net *net = sock_net(sk);
1941 __be32 group = imr->imr_multiaddr.s_addr;
1942 u32 ifindex;
1943 int ret = -EADDRNOTAVAIL;
1945 rtnl_lock();
1946 in_dev = ip_mc_find_dev(net, imr);
1947 ifindex = imr->imr_ifindex;
1948 for (imlp = &inet->mc_list;
1949 (iml = rtnl_dereference(*imlp)) != NULL;
1950 imlp = &iml->next_rcu) {
1951 if (iml->multi.imr_multiaddr.s_addr != group)
1952 continue;
1953 if (ifindex) {
1954 if (iml->multi.imr_ifindex != ifindex)
1955 continue;
1956 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1957 iml->multi.imr_address.s_addr)
1958 continue;
1960 (void) ip_mc_leave_src(sk, iml, in_dev);
1962 *imlp = iml->next_rcu;
1964 if (in_dev)
1965 ip_mc_dec_group(in_dev, group);
1966 rtnl_unlock();
1967 /* decrease mem now to avoid the memleak warning */
1968 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1969 kfree_rcu(iml, rcu);
1970 return 0;
1972 if (!in_dev)
1973 ret = -ENODEV;
1974 rtnl_unlock();
1975 return ret;
1977 EXPORT_SYMBOL(ip_mc_leave_group);
1979 int ip_mc_source(int add, int omode, struct sock *sk, struct
1980 ip_mreq_source *mreqs, int ifindex)
1982 int err;
1983 struct ip_mreqn imr;
1984 __be32 addr = mreqs->imr_multiaddr;
1985 struct ip_mc_socklist *pmc;
1986 struct in_device *in_dev = NULL;
1987 struct inet_sock *inet = inet_sk(sk);
1988 struct ip_sf_socklist *psl;
1989 struct net *net = sock_net(sk);
1990 int leavegroup = 0;
1991 int i, j, rv;
1993 if (!ipv4_is_multicast(addr))
1994 return -EINVAL;
1996 rtnl_lock();
1998 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1999 imr.imr_address.s_addr = mreqs->imr_interface;
2000 imr.imr_ifindex = ifindex;
2001 in_dev = ip_mc_find_dev(net, &imr);
2003 if (!in_dev) {
2004 err = -ENODEV;
2005 goto done;
2007 err = -EADDRNOTAVAIL;
2009 for_each_pmc_rtnl(inet, pmc) {
2010 if ((pmc->multi.imr_multiaddr.s_addr ==
2011 imr.imr_multiaddr.s_addr) &&
2012 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2013 break;
2015 if (!pmc) { /* must have a prior join */
2016 err = -EINVAL;
2017 goto done;
2019 /* if a source filter was set, must be the same mode as before */
2020 if (pmc->sflist) {
2021 if (pmc->sfmode != omode) {
2022 err = -EINVAL;
2023 goto done;
2025 } else if (pmc->sfmode != omode) {
2026 /* allow mode switches for empty-set filters */
2027 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2028 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2029 NULL, 0);
2030 pmc->sfmode = omode;
2033 psl = rtnl_dereference(pmc->sflist);
2034 if (!add) {
2035 if (!psl)
2036 goto done; /* err = -EADDRNOTAVAIL */
2037 rv = !0;
2038 for (i = 0; i < psl->sl_count; i++) {
2039 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2040 sizeof(__be32));
2041 if (rv == 0)
2042 break;
2044 if (rv) /* source not found */
2045 goto done; /* err = -EADDRNOTAVAIL */
2047 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2048 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2049 leavegroup = 1;
2050 goto done;
2053 /* update the interface filter */
2054 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2055 &mreqs->imr_sourceaddr, 1);
2057 for (j = i+1; j < psl->sl_count; j++)
2058 psl->sl_addr[j-1] = psl->sl_addr[j];
2059 psl->sl_count--;
2060 err = 0;
2061 goto done;
2063 /* else, add a new source to the filter */
2065 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2066 err = -ENOBUFS;
2067 goto done;
2069 if (!psl || psl->sl_count == psl->sl_max) {
2070 struct ip_sf_socklist *newpsl;
2071 int count = IP_SFBLOCK;
2073 if (psl)
2074 count += psl->sl_max;
2075 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2076 if (!newpsl) {
2077 err = -ENOBUFS;
2078 goto done;
2080 newpsl->sl_max = count;
2081 newpsl->sl_count = count - IP_SFBLOCK;
2082 if (psl) {
2083 for (i = 0; i < psl->sl_count; i++)
2084 newpsl->sl_addr[i] = psl->sl_addr[i];
2085 /* decrease mem now to avoid the memleak warning */
2086 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2087 kfree_rcu(psl, rcu);
2089 rcu_assign_pointer(pmc->sflist, newpsl);
2090 psl = newpsl;
2092 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2093 for (i = 0; i < psl->sl_count; i++) {
2094 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2095 sizeof(__be32));
2096 if (rv == 0)
2097 break;
2099 if (rv == 0) /* address already there is an error */
2100 goto done;
2101 for (j = psl->sl_count-1; j >= i; j--)
2102 psl->sl_addr[j+1] = psl->sl_addr[j];
2103 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2104 psl->sl_count++;
2105 err = 0;
2106 /* update the interface list */
2107 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2108 &mreqs->imr_sourceaddr, 1);
2109 done:
2110 rtnl_unlock();
2111 if (leavegroup)
2112 return ip_mc_leave_group(sk, &imr);
2113 return err;
2116 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2118 int err = 0;
2119 struct ip_mreqn imr;
2120 __be32 addr = msf->imsf_multiaddr;
2121 struct ip_mc_socklist *pmc;
2122 struct in_device *in_dev;
2123 struct inet_sock *inet = inet_sk(sk);
2124 struct ip_sf_socklist *newpsl, *psl;
2125 struct net *net = sock_net(sk);
2126 int leavegroup = 0;
2128 if (!ipv4_is_multicast(addr))
2129 return -EINVAL;
2130 if (msf->imsf_fmode != MCAST_INCLUDE &&
2131 msf->imsf_fmode != MCAST_EXCLUDE)
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 = ifindex;
2139 in_dev = ip_mc_find_dev(net, &imr);
2141 if (!in_dev) {
2142 err = -ENODEV;
2143 goto done;
2146 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2147 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2148 leavegroup = 1;
2149 goto done;
2152 for_each_pmc_rtnl(inet, pmc) {
2153 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2154 pmc->multi.imr_ifindex == imr.imr_ifindex)
2155 break;
2157 if (!pmc) { /* must have a prior join */
2158 err = -EINVAL;
2159 goto done;
2161 if (msf->imsf_numsrc) {
2162 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2163 GFP_KERNEL);
2164 if (!newpsl) {
2165 err = -ENOBUFS;
2166 goto done;
2168 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2169 memcpy(newpsl->sl_addr, msf->imsf_slist,
2170 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2171 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2172 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2173 if (err) {
2174 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2175 goto done;
2177 } else {
2178 newpsl = NULL;
2179 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2180 msf->imsf_fmode, 0, NULL, 0);
2182 psl = rtnl_dereference(pmc->sflist);
2183 if (psl) {
2184 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2185 psl->sl_count, psl->sl_addr, 0);
2186 /* decrease mem now to avoid the memleak warning */
2187 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2188 kfree_rcu(psl, rcu);
2189 } else
2190 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2191 0, NULL, 0);
2192 rcu_assign_pointer(pmc->sflist, newpsl);
2193 pmc->sfmode = msf->imsf_fmode;
2194 err = 0;
2195 done:
2196 rtnl_unlock();
2197 if (leavegroup)
2198 err = ip_mc_leave_group(sk, &imr);
2199 return err;
2202 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2203 struct ip_msfilter __user *optval, int __user *optlen)
2205 int err, len, count, copycount;
2206 struct ip_mreqn imr;
2207 __be32 addr = msf->imsf_multiaddr;
2208 struct ip_mc_socklist *pmc;
2209 struct in_device *in_dev;
2210 struct inet_sock *inet = inet_sk(sk);
2211 struct ip_sf_socklist *psl;
2212 struct net *net = sock_net(sk);
2214 if (!ipv4_is_multicast(addr))
2215 return -EINVAL;
2217 rtnl_lock();
2219 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2220 imr.imr_address.s_addr = msf->imsf_interface;
2221 imr.imr_ifindex = 0;
2222 in_dev = ip_mc_find_dev(net, &imr);
2224 if (!in_dev) {
2225 err = -ENODEV;
2226 goto done;
2228 err = -EADDRNOTAVAIL;
2230 for_each_pmc_rtnl(inet, pmc) {
2231 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2232 pmc->multi.imr_ifindex == imr.imr_ifindex)
2233 break;
2235 if (!pmc) /* must have a prior join */
2236 goto done;
2237 msf->imsf_fmode = pmc->sfmode;
2238 psl = rtnl_dereference(pmc->sflist);
2239 rtnl_unlock();
2240 if (!psl) {
2241 len = 0;
2242 count = 0;
2243 } else {
2244 count = psl->sl_count;
2246 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2247 len = copycount * sizeof(psl->sl_addr[0]);
2248 msf->imsf_numsrc = count;
2249 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2250 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2251 return -EFAULT;
2253 if (len &&
2254 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2255 return -EFAULT;
2256 return 0;
2257 done:
2258 rtnl_unlock();
2259 return err;
2262 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2263 struct group_filter __user *optval, int __user *optlen)
2265 int err, i, count, copycount;
2266 struct sockaddr_in *psin;
2267 __be32 addr;
2268 struct ip_mc_socklist *pmc;
2269 struct inet_sock *inet = inet_sk(sk);
2270 struct ip_sf_socklist *psl;
2272 psin = (struct sockaddr_in *)&gsf->gf_group;
2273 if (psin->sin_family != AF_INET)
2274 return -EINVAL;
2275 addr = psin->sin_addr.s_addr;
2276 if (!ipv4_is_multicast(addr))
2277 return -EINVAL;
2279 rtnl_lock();
2281 err = -EADDRNOTAVAIL;
2283 for_each_pmc_rtnl(inet, pmc) {
2284 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2285 pmc->multi.imr_ifindex == gsf->gf_interface)
2286 break;
2288 if (!pmc) /* must have a prior join */
2289 goto done;
2290 gsf->gf_fmode = pmc->sfmode;
2291 psl = rtnl_dereference(pmc->sflist);
2292 rtnl_unlock();
2293 count = psl ? psl->sl_count : 0;
2294 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2295 gsf->gf_numsrc = count;
2296 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2297 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2298 return -EFAULT;
2300 for (i = 0; i < copycount; i++) {
2301 struct sockaddr_storage ss;
2303 psin = (struct sockaddr_in *)&ss;
2304 memset(&ss, 0, sizeof(ss));
2305 psin->sin_family = AF_INET;
2306 psin->sin_addr.s_addr = psl->sl_addr[i];
2307 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2308 return -EFAULT;
2310 return 0;
2311 done:
2312 rtnl_unlock();
2313 return err;
2317 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2319 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2321 struct inet_sock *inet = inet_sk(sk);
2322 struct ip_mc_socklist *pmc;
2323 struct ip_sf_socklist *psl;
2324 int i;
2325 int ret;
2327 ret = 1;
2328 if (!ipv4_is_multicast(loc_addr))
2329 goto out;
2331 rcu_read_lock();
2332 for_each_pmc_rcu(inet, pmc) {
2333 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2334 pmc->multi.imr_ifindex == dif)
2335 break;
2337 ret = inet->mc_all;
2338 if (!pmc)
2339 goto unlock;
2340 psl = rcu_dereference(pmc->sflist);
2341 ret = (pmc->sfmode == MCAST_EXCLUDE);
2342 if (!psl)
2343 goto unlock;
2345 for (i = 0; i < psl->sl_count; i++) {
2346 if (psl->sl_addr[i] == rmt_addr)
2347 break;
2349 ret = 0;
2350 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2351 goto unlock;
2352 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2353 goto unlock;
2354 ret = 1;
2355 unlock:
2356 rcu_read_unlock();
2357 out:
2358 return ret;
2362 * A socket is closing.
2365 void ip_mc_drop_socket(struct sock *sk)
2367 struct inet_sock *inet = inet_sk(sk);
2368 struct ip_mc_socklist *iml;
2369 struct net *net = sock_net(sk);
2371 if (inet->mc_list == NULL)
2372 return;
2374 rtnl_lock();
2375 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2376 struct in_device *in_dev;
2378 inet->mc_list = iml->next_rcu;
2379 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2380 (void) ip_mc_leave_src(sk, iml, in_dev);
2381 if (in_dev != NULL)
2382 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2383 /* decrease mem now to avoid the memleak warning */
2384 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2385 kfree_rcu(iml, rcu);
2387 rtnl_unlock();
2390 /* called with rcu_read_lock() */
2391 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2393 struct ip_mc_list *im;
2394 struct ip_mc_list __rcu **mc_hash;
2395 struct ip_sf_list *psf;
2396 int rv = 0;
2398 mc_hash = rcu_dereference(in_dev->mc_hash);
2399 if (mc_hash) {
2400 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2402 for (im = rcu_dereference(mc_hash[hash]);
2403 im != NULL;
2404 im = rcu_dereference(im->next_hash)) {
2405 if (im->multiaddr == mc_addr)
2406 break;
2408 } else {
2409 for_each_pmc_rcu(in_dev, im) {
2410 if (im->multiaddr == mc_addr)
2411 break;
2414 if (im && proto == IPPROTO_IGMP) {
2415 rv = 1;
2416 } else if (im) {
2417 if (src_addr) {
2418 for (psf = im->sources; psf; psf = psf->sf_next) {
2419 if (psf->sf_inaddr == src_addr)
2420 break;
2422 if (psf)
2423 rv = psf->sf_count[MCAST_INCLUDE] ||
2424 psf->sf_count[MCAST_EXCLUDE] !=
2425 im->sfcount[MCAST_EXCLUDE];
2426 else
2427 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2428 } else
2429 rv = 1; /* unspecified source; tentatively allow */
2431 return rv;
2434 #if defined(CONFIG_PROC_FS)
2435 struct igmp_mc_iter_state {
2436 struct seq_net_private p;
2437 struct net_device *dev;
2438 struct in_device *in_dev;
2441 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2443 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2445 struct net *net = seq_file_net(seq);
2446 struct ip_mc_list *im = NULL;
2447 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2449 state->in_dev = NULL;
2450 for_each_netdev_rcu(net, state->dev) {
2451 struct in_device *in_dev;
2453 in_dev = __in_dev_get_rcu(state->dev);
2454 if (!in_dev)
2455 continue;
2456 im = rcu_dereference(in_dev->mc_list);
2457 if (im) {
2458 state->in_dev = in_dev;
2459 break;
2462 return im;
2465 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2467 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2469 im = rcu_dereference(im->next_rcu);
2470 while (!im) {
2471 state->dev = next_net_device_rcu(state->dev);
2472 if (!state->dev) {
2473 state->in_dev = NULL;
2474 break;
2476 state->in_dev = __in_dev_get_rcu(state->dev);
2477 if (!state->in_dev)
2478 continue;
2479 im = rcu_dereference(state->in_dev->mc_list);
2481 return im;
2484 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2486 struct ip_mc_list *im = igmp_mc_get_first(seq);
2487 if (im)
2488 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2489 --pos;
2490 return pos ? NULL : im;
2493 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2494 __acquires(rcu)
2496 rcu_read_lock();
2497 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2500 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2502 struct ip_mc_list *im;
2503 if (v == SEQ_START_TOKEN)
2504 im = igmp_mc_get_first(seq);
2505 else
2506 im = igmp_mc_get_next(seq, v);
2507 ++*pos;
2508 return im;
2511 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2512 __releases(rcu)
2514 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2516 state->in_dev = NULL;
2517 state->dev = NULL;
2518 rcu_read_unlock();
2521 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2523 if (v == SEQ_START_TOKEN)
2524 seq_puts(seq,
2525 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2526 else {
2527 struct ip_mc_list *im = (struct ip_mc_list *)v;
2528 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2529 char *querier;
2530 long delta;
2532 #ifdef CONFIG_IP_MULTICAST
2533 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2534 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2535 "V3";
2536 #else
2537 querier = "NONE";
2538 #endif
2540 if (rcu_dereference(state->in_dev->mc_list) == im) {
2541 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2542 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2545 delta = im->timer.expires - jiffies;
2546 seq_printf(seq,
2547 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2548 im->multiaddr, im->users,
2549 im->tm_running,
2550 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2551 im->reporter);
2553 return 0;
2556 static const struct seq_operations igmp_mc_seq_ops = {
2557 .start = igmp_mc_seq_start,
2558 .next = igmp_mc_seq_next,
2559 .stop = igmp_mc_seq_stop,
2560 .show = igmp_mc_seq_show,
2563 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2565 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2566 sizeof(struct igmp_mc_iter_state));
2569 static const struct file_operations igmp_mc_seq_fops = {
2570 .owner = THIS_MODULE,
2571 .open = igmp_mc_seq_open,
2572 .read = seq_read,
2573 .llseek = seq_lseek,
2574 .release = seq_release_net,
2577 struct igmp_mcf_iter_state {
2578 struct seq_net_private p;
2579 struct net_device *dev;
2580 struct in_device *idev;
2581 struct ip_mc_list *im;
2584 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2586 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2588 struct net *net = seq_file_net(seq);
2589 struct ip_sf_list *psf = NULL;
2590 struct ip_mc_list *im = NULL;
2591 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2593 state->idev = NULL;
2594 state->im = NULL;
2595 for_each_netdev_rcu(net, state->dev) {
2596 struct in_device *idev;
2597 idev = __in_dev_get_rcu(state->dev);
2598 if (unlikely(idev == NULL))
2599 continue;
2600 im = rcu_dereference(idev->mc_list);
2601 if (likely(im != NULL)) {
2602 spin_lock_bh(&im->lock);
2603 psf = im->sources;
2604 if (likely(psf != NULL)) {
2605 state->im = im;
2606 state->idev = idev;
2607 break;
2609 spin_unlock_bh(&im->lock);
2612 return psf;
2615 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2617 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2619 psf = psf->sf_next;
2620 while (!psf) {
2621 spin_unlock_bh(&state->im->lock);
2622 state->im = state->im->next;
2623 while (!state->im) {
2624 state->dev = next_net_device_rcu(state->dev);
2625 if (!state->dev) {
2626 state->idev = NULL;
2627 goto out;
2629 state->idev = __in_dev_get_rcu(state->dev);
2630 if (!state->idev)
2631 continue;
2632 state->im = rcu_dereference(state->idev->mc_list);
2634 if (!state->im)
2635 break;
2636 spin_lock_bh(&state->im->lock);
2637 psf = state->im->sources;
2639 out:
2640 return psf;
2643 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2645 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2646 if (psf)
2647 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2648 --pos;
2649 return pos ? NULL : psf;
2652 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2653 __acquires(rcu)
2655 rcu_read_lock();
2656 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2659 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2661 struct ip_sf_list *psf;
2662 if (v == SEQ_START_TOKEN)
2663 psf = igmp_mcf_get_first(seq);
2664 else
2665 psf = igmp_mcf_get_next(seq, v);
2666 ++*pos;
2667 return psf;
2670 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2671 __releases(rcu)
2673 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2674 if (likely(state->im != NULL)) {
2675 spin_unlock_bh(&state->im->lock);
2676 state->im = NULL;
2678 state->idev = NULL;
2679 state->dev = NULL;
2680 rcu_read_unlock();
2683 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2685 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2686 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2688 if (v == SEQ_START_TOKEN) {
2689 seq_printf(seq,
2690 "%3s %6s "
2691 "%10s %10s %6s %6s\n", "Idx",
2692 "Device", "MCA",
2693 "SRC", "INC", "EXC");
2694 } else {
2695 seq_printf(seq,
2696 "%3d %6.6s 0x%08x "
2697 "0x%08x %6lu %6lu\n",
2698 state->dev->ifindex, state->dev->name,
2699 ntohl(state->im->multiaddr),
2700 ntohl(psf->sf_inaddr),
2701 psf->sf_count[MCAST_INCLUDE],
2702 psf->sf_count[MCAST_EXCLUDE]);
2704 return 0;
2707 static const struct seq_operations igmp_mcf_seq_ops = {
2708 .start = igmp_mcf_seq_start,
2709 .next = igmp_mcf_seq_next,
2710 .stop = igmp_mcf_seq_stop,
2711 .show = igmp_mcf_seq_show,
2714 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2716 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2717 sizeof(struct igmp_mcf_iter_state));
2720 static const struct file_operations igmp_mcf_seq_fops = {
2721 .owner = THIS_MODULE,
2722 .open = igmp_mcf_seq_open,
2723 .read = seq_read,
2724 .llseek = seq_lseek,
2725 .release = seq_release_net,
2728 static int __net_init igmp_net_init(struct net *net)
2730 struct proc_dir_entry *pde;
2732 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
2733 if (!pde)
2734 goto out_igmp;
2735 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2736 &igmp_mcf_seq_fops);
2737 if (!pde)
2738 goto out_mcfilter;
2739 return 0;
2741 out_mcfilter:
2742 remove_proc_entry("igmp", net->proc_net);
2743 out_igmp:
2744 return -ENOMEM;
2747 static void __net_exit igmp_net_exit(struct net *net)
2749 remove_proc_entry("mcfilter", net->proc_net);
2750 remove_proc_entry("igmp", net->proc_net);
2753 static struct pernet_operations igmp_net_ops = {
2754 .init = igmp_net_init,
2755 .exit = igmp_net_exit,
2757 #endif
2759 static int igmp_netdev_event(struct notifier_block *this,
2760 unsigned long event, void *ptr)
2762 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2763 struct in_device *in_dev;
2765 switch (event) {
2766 case NETDEV_RESEND_IGMP:
2767 in_dev = __in_dev_get_rtnl(dev);
2768 if (in_dev)
2769 ip_mc_rejoin_groups(in_dev);
2770 break;
2771 default:
2772 break;
2774 return NOTIFY_DONE;
2777 static struct notifier_block igmp_notifier = {
2778 .notifier_call = igmp_netdev_event,
2781 int __init igmp_mc_init(void)
2783 #if defined(CONFIG_PROC_FS)
2784 int err;
2786 err = register_pernet_subsys(&igmp_net_ops);
2787 if (err)
2788 return err;
2789 err = register_netdevice_notifier(&igmp_notifier);
2790 if (err)
2791 goto reg_notif_fail;
2792 return 0;
2794 reg_notif_fail:
2795 unregister_pernet_subsys(&igmp_net_ops);
2796 return err;
2797 #else
2798 return register_netdevice_notifier(&igmp_notifier);
2799 #endif