1/*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
3 *
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.
6 *
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.
10 *
11 * Authors:
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
13 *
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.
18 *
19 * Fixes:
20 *
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.
33 *
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
71 */
72
73#include <linux/module.h>
74#include <linux/slab.h>
75#include <linux/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>
92#include <linux/byteorder/generic.h>
93
94#include <net/net_namespace.h>
95#include <net/arp.h>
96#include <net/ip.h>
97#include <net/protocol.h>
98#include <net/route.h>
99#include <net/sock.h>
100#include <net/checksum.h>
101#include <net/inet_common.h>
102#include <linux/netfilter_ipv4.h>
103#ifdef CONFIG_IP_MROUTE
104#include <linux/mroute.h>
105#endif
106#ifdef CONFIG_PROC_FS
107#include <linux/proc_fs.h>
108#include <linux/seq_file.h>
109#endif
110
111#ifdef CONFIG_IP_MULTICAST
112/* Parameter names and values are taken from igmp-v2-06 draft */
113
114#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
115#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
116#define IGMP_QUERY_INTERVAL (125*HZ)
117#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
118
119#define IGMP_INITIAL_REPORT_DELAY (1)
120
121/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
122 * IGMP specs require to report membership immediately after
123 * joining a group, but we delay the first report by a
124 * small interval. It seems more natural and still does not
125 * contradict to specs provided this delay is small enough.
126 */
127
128#define IGMP_V1_SEEN(in_dev) \
129 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
130 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
131 ((in_dev)->mr_v1_seen && \
132 time_before(jiffies, (in_dev)->mr_v1_seen)))
133#define IGMP_V2_SEEN(in_dev) \
134 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
135 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
136 ((in_dev)->mr_v2_seen && \
137 time_before(jiffies, (in_dev)->mr_v2_seen)))
138
139static int unsolicited_report_interval(struct in_device *in_dev)
140{
141 int interval_ms, interval_jiffies;
142
143 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
144 interval_ms = IN_DEV_CONF_GET(
145 in_dev,
146 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
147 else /* v3 */
148 interval_ms = IN_DEV_CONF_GET(
149 in_dev,
150 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
151
152 interval_jiffies = msecs_to_jiffies(interval_ms);
153
154 /* _timer functions can't handle a delay of 0 jiffies so ensure
155 * we always return a positive value.
156 */
157 if (interval_jiffies <= 0)
158 interval_jiffies = 1;
159 return interval_jiffies;
160}
161
162static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
163 gfp_t gfp);
164static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
165static void igmpv3_clear_delrec(struct in_device *in_dev);
166static int sf_setstate(struct ip_mc_list *pmc);
167static void sf_markstate(struct ip_mc_list *pmc);
168#endif
169static void ip_mc_clear_src(struct ip_mc_list *pmc);
170static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
171 int sfcount, __be32 *psfsrc, int delta);
172
173static void ip_ma_put(struct ip_mc_list *im)
174{
175 if (refcount_dec_and_test(&im->refcnt)) {
176 in_dev_put(im->interface);
177 kfree_rcu(im, rcu);
178 }
179}
180
181#define for_each_pmc_rcu(in_dev, pmc) \
182 for (pmc = rcu_dereference(in_dev->mc_list); \
183 pmc != NULL; \
184 pmc = rcu_dereference(pmc->next_rcu))
185
186#define for_each_pmc_rtnl(in_dev, pmc) \
187 for (pmc = rtnl_dereference(in_dev->mc_list); \
188 pmc != NULL; \
189 pmc = rtnl_dereference(pmc->next_rcu))
190
191#ifdef CONFIG_IP_MULTICAST
192
193/*
194 * Timer management
195 */
196
197static void igmp_stop_timer(struct ip_mc_list *im)
198{
199 spin_lock_bh(&im->lock);
200 if (del_timer(&im->timer))
201 refcount_dec(&im->refcnt);
202 im->tm_running = 0;
203 im->reporter = 0;
204 im->unsolicit_count = 0;
205 spin_unlock_bh(&im->lock);
206}
207
208/* It must be called with locked im->lock */
209static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
210{
211 int tv = prandom_u32() % max_delay;
212
213 im->tm_running = 1;
214 if (!mod_timer(&im->timer, jiffies+tv+2))
215 refcount_inc(&im->refcnt);
216}
217
218static void igmp_gq_start_timer(struct in_device *in_dev)
219{
220 int tv = prandom_u32() % in_dev->mr_maxdelay;
221 unsigned long exp = jiffies + tv + 2;
222
223 if (in_dev->mr_gq_running &&
224 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
225 return;
226
227 in_dev->mr_gq_running = 1;
228 if (!mod_timer(&in_dev->mr_gq_timer, exp))
229 in_dev_hold(in_dev);
230}
231
232static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
233{
234 int tv = prandom_u32() % delay;
235
236 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
237 in_dev_hold(in_dev);
238}
239
240static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
241{
242 spin_lock_bh(&im->lock);
243 im->unsolicit_count = 0;
244 if (del_timer(&im->timer)) {
245 if ((long)(im->timer.expires-jiffies) < max_delay) {
246 add_timer(&im->timer);
247 im->tm_running = 1;
248 spin_unlock_bh(&im->lock);
249 return;
250 }
251 refcount_dec(&im->refcnt);
252 }
253 igmp_start_timer(im, max_delay);
254 spin_unlock_bh(&im->lock);
255}
256
257
258/*
259 * Send an IGMP report.
260 */
261
262#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
263
264
265static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
266 int gdeleted, int sdeleted)
267{
268 switch (type) {
269 case IGMPV3_MODE_IS_INCLUDE:
270 case IGMPV3_MODE_IS_EXCLUDE:
271 if (gdeleted || sdeleted)
272 return 0;
273 if (!(pmc->gsquery && !psf->sf_gsresp)) {
274 if (pmc->sfmode == MCAST_INCLUDE)
275 return 1;
276 /* don't include if this source is excluded
277 * in all filters
278 */
279 if (psf->sf_count[MCAST_INCLUDE])
280 return type == IGMPV3_MODE_IS_INCLUDE;
281 return pmc->sfcount[MCAST_EXCLUDE] ==
282 psf->sf_count[MCAST_EXCLUDE];
283 }
284 return 0;
285 case IGMPV3_CHANGE_TO_INCLUDE:
286 if (gdeleted || sdeleted)
287 return 0;
288 return psf->sf_count[MCAST_INCLUDE] != 0;
289 case IGMPV3_CHANGE_TO_EXCLUDE:
290 if (gdeleted || sdeleted)
291 return 0;
292 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
293 psf->sf_count[MCAST_INCLUDE])
294 return 0;
295 return pmc->sfcount[MCAST_EXCLUDE] ==
296 psf->sf_count[MCAST_EXCLUDE];
297 case IGMPV3_ALLOW_NEW_SOURCES:
298 if (gdeleted || !psf->sf_crcount)
299 return 0;
300 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
301 case IGMPV3_BLOCK_OLD_SOURCES:
302 if (pmc->sfmode == MCAST_INCLUDE)
303 return gdeleted || (psf->sf_crcount && sdeleted);
304 return psf->sf_crcount && !gdeleted && !sdeleted;
305 }
306 return 0;
307}
308
309static int
310igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
311{
312 struct ip_sf_list *psf;
313 int scount = 0;
314
315 for (psf = pmc->sources; psf; psf = psf->sf_next) {
316 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
317 continue;
318 scount++;
319 }
320 return scount;
321}
322
323/* source address selection per RFC 3376 section 4.2.13 */
324static __be32 igmpv3_get_srcaddr(struct net_device *dev,
325 const struct flowi4 *fl4)
326{
327 struct in_device *in_dev = __in_dev_get_rcu(dev);
328
329 if (!in_dev)
330 return htonl(INADDR_ANY);
331
332 for_ifa(in_dev) {
333 if (fl4->saddr == ifa->ifa_local)
334 return fl4->saddr;
335 } endfor_ifa(in_dev);
336
337 return htonl(INADDR_ANY);
338}
339
340static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
341{
342 struct sk_buff *skb;
343 struct rtable *rt;
344 struct iphdr *pip;
345 struct igmpv3_report *pig;
346 struct net *net = dev_net(dev);
347 struct flowi4 fl4;
348 int hlen = LL_RESERVED_SPACE(dev);
349 int tlen = dev->needed_tailroom;
350 unsigned int size = mtu;
351
352 while (1) {
353 skb = alloc_skb(size + hlen + tlen,
354 GFP_ATOMIC | __GFP_NOWARN);
355 if (skb)
356 break;
357 size >>= 1;
358 if (size < 256)
359 return NULL;
360 }
361 skb->priority = TC_PRIO_CONTROL;
362
363 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
364 0, 0,
365 IPPROTO_IGMP, 0, dev->ifindex);
366 if (IS_ERR(rt)) {
367 kfree_skb(skb);
368 return NULL;
369 }
370
371 skb_dst_set(skb, &rt->dst);
372 skb->dev = dev;
373
374 skb_reserve(skb, hlen);
375 skb_tailroom_reserve(skb, mtu, tlen);
376
377 skb_reset_network_header(skb);
378 pip = ip_hdr(skb);
379 skb_put(skb, sizeof(struct iphdr) + 4);
380
381 pip->version = 4;
382 pip->ihl = (sizeof(struct iphdr)+4)>>2;
383 pip->tos = 0xc0;
384 pip->frag_off = htons(IP_DF);
385 pip->ttl = 1;
386 pip->daddr = fl4.daddr;
387
388 rcu_read_lock();
389 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
390 rcu_read_unlock();
391
392 pip->protocol = IPPROTO_IGMP;
393 pip->tot_len = 0; /* filled in later */
394 ip_select_ident(net, skb, NULL);
395 ((u8 *)&pip[1])[0] = IPOPT_RA;
396 ((u8 *)&pip[1])[1] = 4;
397 ((u8 *)&pip[1])[2] = 0;
398 ((u8 *)&pip[1])[3] = 0;
399
400 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
401 skb_put(skb, sizeof(*pig));
402 pig = igmpv3_report_hdr(skb);
403 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
404 pig->resv1 = 0;
405 pig->csum = 0;
406 pig->resv2 = 0;
407 pig->ngrec = 0;
408 return skb;
409}
410
411static int igmpv3_sendpack(struct sk_buff *skb)
412{
413 struct igmphdr *pig = igmp_hdr(skb);
414 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
415
416 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
417
418 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
419}
420
421static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
422{
423 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
424}
425
426static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
427 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
428{
429 struct net_device *dev = pmc->interface->dev;
430 struct igmpv3_report *pih;
431 struct igmpv3_grec *pgr;
432
433 if (!skb) {
434 skb = igmpv3_newpack(dev, mtu);
435 if (!skb)
436 return NULL;
437 }
438 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
439 pgr->grec_type = type;
440 pgr->grec_auxwords = 0;
441 pgr->grec_nsrcs = 0;
442 pgr->grec_mca = pmc->multiaddr;
443 pih = igmpv3_report_hdr(skb);
444 pih->ngrec = htons(ntohs(pih->ngrec)+1);
445 *ppgr = pgr;
446 return skb;
447}
448
449#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
450
451static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
452 int type, int gdeleted, int sdeleted)
453{
454 struct net_device *dev = pmc->interface->dev;
455 struct net *net = dev_net(dev);
456 struct igmpv3_report *pih;
457 struct igmpv3_grec *pgr = NULL;
458 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
459 int scount, stotal, first, isquery, truncate;
460 unsigned int mtu;
461
462 if (pmc->multiaddr == IGMP_ALL_HOSTS)
463 return skb;
464 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
465 return skb;
466
467 mtu = READ_ONCE(dev->mtu);
468 if (mtu < IPV4_MIN_MTU)
469 return skb;
470
471 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
472 type == IGMPV3_MODE_IS_EXCLUDE;
473 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
474 type == IGMPV3_CHANGE_TO_EXCLUDE;
475
476 stotal = scount = 0;
477
478 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
479
480 if (!*psf_list)
481 goto empty_source;
482
483 pih = skb ? igmpv3_report_hdr(skb) : NULL;
484
485 /* EX and TO_EX get a fresh packet, if needed */
486 if (truncate) {
487 if (pih && pih->ngrec &&
488 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
489 if (skb)
490 igmpv3_sendpack(skb);
491 skb = igmpv3_newpack(dev, mtu);
492 }
493 }
494 first = 1;
495 psf_prev = NULL;
496 for (psf = *psf_list; psf; psf = psf_next) {
497 __be32 *psrc;
498
499 psf_next = psf->sf_next;
500
501 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
502 psf_prev = psf;
503 continue;
504 }
505
506 /* Based on RFC3376 5.1. Should not send source-list change
507 * records when there is a filter mode change.
508 */
509 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
510 (!gdeleted && pmc->crcount)) &&
511 (type == IGMPV3_ALLOW_NEW_SOURCES ||
512 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
513 goto decrease_sf_crcount;
514
515 /* clear marks on query responses */
516 if (isquery)
517 psf->sf_gsresp = 0;
518
519 if (AVAILABLE(skb) < sizeof(__be32) +
520 first*sizeof(struct igmpv3_grec)) {
521 if (truncate && !first)
522 break; /* truncate these */
523 if (pgr)
524 pgr->grec_nsrcs = htons(scount);
525 if (skb)
526 igmpv3_sendpack(skb);
527 skb = igmpv3_newpack(dev, mtu);
528 first = 1;
529 scount = 0;
530 }
531 if (first) {
532 skb = add_grhead(skb, pmc, type, &pgr, mtu);
533 first = 0;
534 }
535 if (!skb)
536 return NULL;
537 psrc = skb_put(skb, sizeof(__be32));
538 *psrc = psf->sf_inaddr;
539 scount++; stotal++;
540 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
541 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
542decrease_sf_crcount:
543 psf->sf_crcount--;
544 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
545 if (psf_prev)
546 psf_prev->sf_next = psf->sf_next;
547 else
548 *psf_list = psf->sf_next;
549 kfree(psf);
550 continue;
551 }
552 }
553 psf_prev = psf;
554 }
555
556empty_source:
557 if (!stotal) {
558 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
559 type == IGMPV3_BLOCK_OLD_SOURCES)
560 return skb;
561 if (pmc->crcount || isquery) {
562 /* make sure we have room for group header */
563 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
564 igmpv3_sendpack(skb);
565 skb = NULL; /* add_grhead will get a new one */
566 }
567 skb = add_grhead(skb, pmc, type, &pgr, mtu);
568 }
569 }
570 if (pgr)
571 pgr->grec_nsrcs = htons(scount);
572
573 if (isquery)
574 pmc->gsquery = 0; /* clear query state on report */
575 return skb;
576}
577
578static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
579{
580 struct sk_buff *skb = NULL;
581 struct net *net = dev_net(in_dev->dev);
582 int type;
583
584 if (!pmc) {
585 rcu_read_lock();
586 for_each_pmc_rcu(in_dev, pmc) {
587 if (pmc->multiaddr == IGMP_ALL_HOSTS)
588 continue;
589 if (ipv4_is_local_multicast(pmc->multiaddr) &&
590 !net->ipv4.sysctl_igmp_llm_reports)
591 continue;
592 spin_lock_bh(&pmc->lock);
593 if (pmc->sfcount[MCAST_EXCLUDE])
594 type = IGMPV3_MODE_IS_EXCLUDE;
595 else
596 type = IGMPV3_MODE_IS_INCLUDE;
597 skb = add_grec(skb, pmc, type, 0, 0);
598 spin_unlock_bh(&pmc->lock);
599 }
600 rcu_read_unlock();
601 } else {
602 spin_lock_bh(&pmc->lock);
603 if (pmc->sfcount[MCAST_EXCLUDE])
604 type = IGMPV3_MODE_IS_EXCLUDE;
605 else
606 type = IGMPV3_MODE_IS_INCLUDE;
607 skb = add_grec(skb, pmc, type, 0, 0);
608 spin_unlock_bh(&pmc->lock);
609 }
610 if (!skb)
611 return 0;
612 return igmpv3_sendpack(skb);
613}
614
615/*
616 * remove zero-count source records from a source filter list
617 */
618static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
619{
620 struct ip_sf_list *psf_prev, *psf_next, *psf;
621
622 psf_prev = NULL;
623 for (psf = *ppsf; psf; psf = psf_next) {
624 psf_next = psf->sf_next;
625 if (psf->sf_crcount == 0) {
626 if (psf_prev)
627 psf_prev->sf_next = psf->sf_next;
628 else
629 *ppsf = psf->sf_next;
630 kfree(psf);
631 } else
632 psf_prev = psf;
633 }
634}
635
636static void igmpv3_send_cr(struct in_device *in_dev)
637{
638 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
639 struct sk_buff *skb = NULL;
640 int type, dtype;
641
642 rcu_read_lock();
643 spin_lock_bh(&in_dev->mc_tomb_lock);
644
645 /* deleted MCA's */
646 pmc_prev = NULL;
647 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
648 pmc_next = pmc->next;
649 if (pmc->sfmode == MCAST_INCLUDE) {
650 type = IGMPV3_BLOCK_OLD_SOURCES;
651 dtype = IGMPV3_BLOCK_OLD_SOURCES;
652 skb = add_grec(skb, pmc, type, 1, 0);
653 skb = add_grec(skb, pmc, dtype, 1, 1);
654 }
655 if (pmc->crcount) {
656 if (pmc->sfmode == MCAST_EXCLUDE) {
657 type = IGMPV3_CHANGE_TO_INCLUDE;
658 skb = add_grec(skb, pmc, type, 1, 0);
659 }
660 pmc->crcount--;
661 if (pmc->crcount == 0) {
662 igmpv3_clear_zeros(&pmc->tomb);
663 igmpv3_clear_zeros(&pmc->sources);
664 }
665 }
666 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
667 if (pmc_prev)
668 pmc_prev->next = pmc_next;
669 else
670 in_dev->mc_tomb = pmc_next;
671 in_dev_put(pmc->interface);
672 kfree(pmc);
673 } else
674 pmc_prev = pmc;
675 }
676 spin_unlock_bh(&in_dev->mc_tomb_lock);
677
678 /* change recs */
679 for_each_pmc_rcu(in_dev, pmc) {
680 spin_lock_bh(&pmc->lock);
681 if (pmc->sfcount[MCAST_EXCLUDE]) {
682 type = IGMPV3_BLOCK_OLD_SOURCES;
683 dtype = IGMPV3_ALLOW_NEW_SOURCES;
684 } else {
685 type = IGMPV3_ALLOW_NEW_SOURCES;
686 dtype = IGMPV3_BLOCK_OLD_SOURCES;
687 }
688 skb = add_grec(skb, pmc, type, 0, 0);
689 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
690
691 /* filter mode changes */
692 if (pmc->crcount) {
693 if (pmc->sfmode == MCAST_EXCLUDE)
694 type = IGMPV3_CHANGE_TO_EXCLUDE;
695 else
696 type = IGMPV3_CHANGE_TO_INCLUDE;
697 skb = add_grec(skb, pmc, type, 0, 0);
698 pmc->crcount--;
699 }
700 spin_unlock_bh(&pmc->lock);
701 }
702 rcu_read_unlock();
703
704 if (!skb)
705 return;
706 (void) igmpv3_sendpack(skb);
707}
708
709static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
710 int type)
711{
712 struct sk_buff *skb;
713 struct iphdr *iph;
714 struct igmphdr *ih;
715 struct rtable *rt;
716 struct net_device *dev = in_dev->dev;
717 struct net *net = dev_net(dev);
718 __be32 group = pmc ? pmc->multiaddr : 0;
719 struct flowi4 fl4;
720 __be32 dst;
721 int hlen, tlen;
722
723 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
724 return igmpv3_send_report(in_dev, pmc);
725
726 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
727 return 0;
728
729 if (type == IGMP_HOST_LEAVE_MESSAGE)
730 dst = IGMP_ALL_ROUTER;
731 else
732 dst = group;
733
734 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
735 0, 0,
736 IPPROTO_IGMP, 0, dev->ifindex);
737 if (IS_ERR(rt))
738 return -1;
739
740 hlen = LL_RESERVED_SPACE(dev);
741 tlen = dev->needed_tailroom;
742 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
743 if (!skb) {
744 ip_rt_put(rt);
745 return -1;
746 }
747 skb->priority = TC_PRIO_CONTROL;
748
749 skb_dst_set(skb, &rt->dst);
750
751 skb_reserve(skb, hlen);
752
753 skb_reset_network_header(skb);
754 iph = ip_hdr(skb);
755 skb_put(skb, sizeof(struct iphdr) + 4);
756
757 iph->version = 4;
758 iph->ihl = (sizeof(struct iphdr)+4)>>2;
759 iph->tos = 0xc0;
760 iph->frag_off = htons(IP_DF);
761 iph->ttl = 1;
762 iph->daddr = dst;
763 iph->saddr = fl4.saddr;
764 iph->protocol = IPPROTO_IGMP;
765 ip_select_ident(net, skb, NULL);
766 ((u8 *)&iph[1])[0] = IPOPT_RA;
767 ((u8 *)&iph[1])[1] = 4;
768 ((u8 *)&iph[1])[2] = 0;
769 ((u8 *)&iph[1])[3] = 0;
770
771 ih = skb_put(skb, sizeof(struct igmphdr));
772 ih->type = type;
773 ih->code = 0;
774 ih->csum = 0;
775 ih->group = group;
776 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
777
778 return ip_local_out(net, skb->sk, skb);
779}
780
781static void igmp_gq_timer_expire(struct timer_list *t)
782{
783 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
784
785 in_dev->mr_gq_running = 0;
786 igmpv3_send_report(in_dev, NULL);
787 in_dev_put(in_dev);
788}
789
790static void igmp_ifc_timer_expire(struct timer_list *t)
791{
792 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
793
794 igmpv3_send_cr(in_dev);
795 if (in_dev->mr_ifc_count) {
796 in_dev->mr_ifc_count--;
797 igmp_ifc_start_timer(in_dev,
798 unsolicited_report_interval(in_dev));
799 }
800 in_dev_put(in_dev);
801}
802
803static void igmp_ifc_event(struct in_device *in_dev)
804{
805 struct net *net = dev_net(in_dev->dev);
806 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
807 return;
808 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
809 igmp_ifc_start_timer(in_dev, 1);
810}
811
812
813static void igmp_timer_expire(struct timer_list *t)
814{
815 struct ip_mc_list *im = from_timer(im, t, timer);
816 struct in_device *in_dev = im->interface;
817
818 spin_lock(&im->lock);
819 im->tm_running = 0;
820
821 if (im->unsolicit_count && --im->unsolicit_count)
822 igmp_start_timer(im, unsolicited_report_interval(in_dev));
823
824 im->reporter = 1;
825 spin_unlock(&im->lock);
826
827 if (IGMP_V1_SEEN(in_dev))
828 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
829 else if (IGMP_V2_SEEN(in_dev))
830 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
831 else
832 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
833
834 ip_ma_put(im);
835}
836
837/* mark EXCLUDE-mode sources */
838static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
839{
840 struct ip_sf_list *psf;
841 int i, scount;
842
843 scount = 0;
844 for (psf = pmc->sources; psf; psf = psf->sf_next) {
845 if (scount == nsrcs)
846 break;
847 for (i = 0; i < nsrcs; i++) {
848 /* skip inactive filters */
849 if (psf->sf_count[MCAST_INCLUDE] ||
850 pmc->sfcount[MCAST_EXCLUDE] !=
851 psf->sf_count[MCAST_EXCLUDE])
852 break;
853 if (srcs[i] == psf->sf_inaddr) {
854 scount++;
855 break;
856 }
857 }
858 }
859 pmc->gsquery = 0;
860 if (scount == nsrcs) /* all sources excluded */
861 return 0;
862 return 1;
863}
864
865static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
866{
867 struct ip_sf_list *psf;
868 int i, scount;
869
870 if (pmc->sfmode == MCAST_EXCLUDE)
871 return igmp_xmarksources(pmc, nsrcs, srcs);
872
873 /* mark INCLUDE-mode sources */
874 scount = 0;
875 for (psf = pmc->sources; psf; psf = psf->sf_next) {
876 if (scount == nsrcs)
877 break;
878 for (i = 0; i < nsrcs; i++)
879 if (srcs[i] == psf->sf_inaddr) {
880 psf->sf_gsresp = 1;
881 scount++;
882 break;
883 }
884 }
885 if (!scount) {
886 pmc->gsquery = 0;
887 return 0;
888 }
889 pmc->gsquery = 1;
890 return 1;
891}
892
893/* return true if packet was dropped */
894static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
895{
896 struct ip_mc_list *im;
897 struct net *net = dev_net(in_dev->dev);
898
899 /* Timers are only set for non-local groups */
900
901 if (group == IGMP_ALL_HOSTS)
902 return false;
903 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
904 return false;
905
906 rcu_read_lock();
907 for_each_pmc_rcu(in_dev, im) {
908 if (im->multiaddr == group) {
909 igmp_stop_timer(im);
910 break;
911 }
912 }
913 rcu_read_unlock();
914 return false;
915}
916
917/* return true if packet was dropped */
918static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
919 int len)
920{
921 struct igmphdr *ih = igmp_hdr(skb);
922 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
923 struct ip_mc_list *im;
924 __be32 group = ih->group;
925 int max_delay;
926 int mark = 0;
927 struct net *net = dev_net(in_dev->dev);
928
929
930 if (len == 8) {
931 if (ih->code == 0) {
932 /* Alas, old v1 router presents here. */
933
934 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
935 in_dev->mr_v1_seen = jiffies +
936 (in_dev->mr_qrv * in_dev->mr_qi) +
937 in_dev->mr_qri;
938 group = 0;
939 } else {
940 /* v2 router present */
941 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
942 in_dev->mr_v2_seen = jiffies +
943 (in_dev->mr_qrv * in_dev->mr_qi) +
944 in_dev->mr_qri;
945 }
946 /* cancel the interface change timer */
947 in_dev->mr_ifc_count = 0;
948 if (del_timer(&in_dev->mr_ifc_timer))
949 __in_dev_put(in_dev);
950 /* clear deleted report items */
951 igmpv3_clear_delrec(in_dev);
952 } else if (len < 12) {
953 return true; /* ignore bogus packet; freed by caller */
954 } else if (IGMP_V1_SEEN(in_dev)) {
955 /* This is a v3 query with v1 queriers present */
956 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
957 group = 0;
958 } else if (IGMP_V2_SEEN(in_dev)) {
959 /* this is a v3 query with v2 queriers present;
960 * Interpretation of the max_delay code is problematic here.
961 * A real v2 host would use ih_code directly, while v3 has a
962 * different encoding. We use the v3 encoding as more likely
963 * to be intended in a v3 query.
964 */
965 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
966 if (!max_delay)
967 max_delay = 1; /* can't mod w/ 0 */
968 } else { /* v3 */
969 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
970 return true;
971
972 ih3 = igmpv3_query_hdr(skb);
973 if (ih3->nsrcs) {
974 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
975 + ntohs(ih3->nsrcs)*sizeof(__be32)))
976 return true;
977 ih3 = igmpv3_query_hdr(skb);
978 }
979
980 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
981 if (!max_delay)
982 max_delay = 1; /* can't mod w/ 0 */
983 in_dev->mr_maxdelay = max_delay;
984
985 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
986 * received value was zero, use the default or statically
987 * configured value.
988 */
989 in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
990 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
991
992 /* RFC3376, 8.3. Query Response Interval:
993 * The number of seconds represented by the [Query Response
994 * Interval] must be less than the [Query Interval].
995 */
996 if (in_dev->mr_qri >= in_dev->mr_qi)
997 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
998
999 if (!group) { /* general query */
1000 if (ih3->nsrcs)
1001 return true; /* no sources allowed */
1002 igmp_gq_start_timer(in_dev);
1003 return false;
1004 }
1005 /* mark sources to include, if group & source-specific */
1006 mark = ih3->nsrcs != 0;
1007 }
1008
1009 /*
1010 * - Start the timers in all of our membership records
1011 * that the query applies to for the interface on
1012 * which the query arrived excl. those that belong
1013 * to a "local" group (224.0.0.X)
1014 * - For timers already running check if they need to
1015 * be reset.
1016 * - Use the igmp->igmp_code field as the maximum
1017 * delay possible
1018 */
1019 rcu_read_lock();
1020 for_each_pmc_rcu(in_dev, im) {
1021 int changed;
1022
1023 if (group && group != im->multiaddr)
1024 continue;
1025 if (im->multiaddr == IGMP_ALL_HOSTS)
1026 continue;
1027 if (ipv4_is_local_multicast(im->multiaddr) &&
1028 !net->ipv4.sysctl_igmp_llm_reports)
1029 continue;
1030 spin_lock_bh(&im->lock);
1031 if (im->tm_running)
1032 im->gsquery = im->gsquery && mark;
1033 else
1034 im->gsquery = mark;
1035 changed = !im->gsquery ||
1036 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1037 spin_unlock_bh(&im->lock);
1038 if (changed)
1039 igmp_mod_timer(im, max_delay);
1040 }
1041 rcu_read_unlock();
1042 return false;
1043}
1044
1045/* called in rcu_read_lock() section */
1046int igmp_rcv(struct sk_buff *skb)
1047{
1048 /* This basically follows the spec line by line -- see RFC1112 */
1049 struct igmphdr *ih;
1050 struct net_device *dev = skb->dev;
1051 struct in_device *in_dev;
1052 int len = skb->len;
1053 bool dropped = true;
1054
1055 if (netif_is_l3_master(dev)) {
1056 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1057 if (!dev)
1058 goto drop;
1059 }
1060
1061 in_dev = __in_dev_get_rcu(dev);
1062 if (!in_dev)
1063 goto drop;
1064
1065 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1066 goto drop;
1067
1068 if (skb_checksum_simple_validate(skb))
1069 goto drop;
1070
1071 ih = igmp_hdr(skb);
1072 switch (ih->type) {
1073 case IGMP_HOST_MEMBERSHIP_QUERY:
1074 dropped = igmp_heard_query(in_dev, skb, len);
1075 break;
1076 case IGMP_HOST_MEMBERSHIP_REPORT:
1077 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1078 /* Is it our report looped back? */
1079 if (rt_is_output_route(skb_rtable(skb)))
1080 break;
1081 /* don't rely on MC router hearing unicast reports */
1082 if (skb->pkt_type == PACKET_MULTICAST ||
1083 skb->pkt_type == PACKET_BROADCAST)
1084 dropped = igmp_heard_report(in_dev, ih->group);
1085 break;
1086 case IGMP_PIM:
1087#ifdef CONFIG_IP_PIMSM_V1
1088 return pim_rcv_v1(skb);
1089#endif
1090 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1091 case IGMP_DVMRP:
1092 case IGMP_TRACE:
1093 case IGMP_HOST_LEAVE_MESSAGE:
1094 case IGMP_MTRACE:
1095 case IGMP_MTRACE_RESP:
1096 break;
1097 default:
1098 break;
1099 }
1100
1101drop:
1102 if (dropped)
1103 kfree_skb(skb);
1104 else
1105 consume_skb(skb);
1106 return 0;
1107}
1108
1109#endif
1110
1111
1112/*
1113 * Add a filter to a device
1114 */
1115
1116static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1117{
1118 char buf[MAX_ADDR_LEN];
1119 struct net_device *dev = in_dev->dev;
1120
1121 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1122 We will get multicast token leakage, when IFF_MULTICAST
1123 is changed. This check should be done in ndo_set_rx_mode
1124 routine. Something sort of:
1125 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1126 --ANK
1127 */
1128 if (arp_mc_map(addr, buf, dev, 0) == 0)
1129 dev_mc_add(dev, buf);
1130}
1131
1132/*
1133 * Remove a filter from a device
1134 */
1135
1136static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1137{
1138 char buf[MAX_ADDR_LEN];
1139 struct net_device *dev = in_dev->dev;
1140
1141 if (arp_mc_map(addr, buf, dev, 0) == 0)
1142 dev_mc_del(dev, buf);
1143}
1144
1145#ifdef CONFIG_IP_MULTICAST
1146/*
1147 * deleted ip_mc_list manipulation
1148 */
1149static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
1150 gfp_t gfp)
1151{
1152 struct ip_mc_list *pmc;
1153 struct net *net = dev_net(in_dev->dev);
1154
1155 /* this is an "ip_mc_list" for convenience; only the fields below
1156 * are actually used. In particular, the refcnt and users are not
1157 * used for management of the delete list. Using the same structure
1158 * for deleted items allows change reports to use common code with
1159 * non-deleted or query-response MCA's.
1160 */
1161 pmc = kzalloc(sizeof(*pmc), gfp);
1162 if (!pmc)
1163 return;
1164 spin_lock_init(&pmc->lock);
1165 spin_lock_bh(&im->lock);
1166 pmc->interface = im->interface;
1167 in_dev_hold(in_dev);
1168 pmc->multiaddr = im->multiaddr;
1169 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1170 pmc->sfmode = im->sfmode;
1171 if (pmc->sfmode == MCAST_INCLUDE) {
1172 struct ip_sf_list *psf;
1173
1174 pmc->tomb = im->tomb;
1175 pmc->sources = im->sources;
1176 im->tomb = im->sources = NULL;
1177 for (psf = pmc->sources; psf; psf = psf->sf_next)
1178 psf->sf_crcount = pmc->crcount;
1179 }
1180 spin_unlock_bh(&im->lock);
1181
1182 spin_lock_bh(&in_dev->mc_tomb_lock);
1183 pmc->next = in_dev->mc_tomb;
1184 in_dev->mc_tomb = pmc;
1185 spin_unlock_bh(&in_dev->mc_tomb_lock);
1186}
1187
1188/*
1189 * restore ip_mc_list deleted records
1190 */
1191static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1192{
1193 struct ip_mc_list *pmc, *pmc_prev;
1194 struct ip_sf_list *psf;
1195 struct net *net = dev_net(in_dev->dev);
1196 __be32 multiaddr = im->multiaddr;
1197
1198 spin_lock_bh(&in_dev->mc_tomb_lock);
1199 pmc_prev = NULL;
1200 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1201 if (pmc->multiaddr == multiaddr)
1202 break;
1203 pmc_prev = pmc;
1204 }
1205 if (pmc) {
1206 if (pmc_prev)
1207 pmc_prev->next = pmc->next;
1208 else
1209 in_dev->mc_tomb = pmc->next;
1210 }
1211 spin_unlock_bh(&in_dev->mc_tomb_lock);
1212
1213 spin_lock_bh(&im->lock);
1214 if (pmc) {
1215 im->interface = pmc->interface;
1216 if (im->sfmode == MCAST_INCLUDE) {
1217 im->tomb = pmc->tomb;
1218 im->sources = pmc->sources;
1219 for (psf = im->sources; psf; psf = psf->sf_next)
1220 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1221 } else {
1222 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1223 }
1224 in_dev_put(pmc->interface);
1225 kfree(pmc);
1226 }
1227 spin_unlock_bh(&im->lock);
1228}
1229
1230/*
1231 * flush ip_mc_list deleted records
1232 */
1233static void igmpv3_clear_delrec(struct in_device *in_dev)
1234{
1235 struct ip_mc_list *pmc, *nextpmc;
1236
1237 spin_lock_bh(&in_dev->mc_tomb_lock);
1238 pmc = in_dev->mc_tomb;
1239 in_dev->mc_tomb = NULL;
1240 spin_unlock_bh(&in_dev->mc_tomb_lock);
1241
1242 for (; pmc; pmc = nextpmc) {
1243 nextpmc = pmc->next;
1244 ip_mc_clear_src(pmc);
1245 in_dev_put(pmc->interface);
1246 kfree(pmc);
1247 }
1248 /* clear dead sources, too */
1249 rcu_read_lock();
1250 for_each_pmc_rcu(in_dev, pmc) {
1251 struct ip_sf_list *psf, *psf_next;
1252
1253 spin_lock_bh(&pmc->lock);
1254 psf = pmc->tomb;
1255 pmc->tomb = NULL;
1256 spin_unlock_bh(&pmc->lock);
1257 for (; psf; psf = psf_next) {
1258 psf_next = psf->sf_next;
1259 kfree(psf);
1260 }
1261 }
1262 rcu_read_unlock();
1263}
1264#endif
1265
1266static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
1267{
1268 struct in_device *in_dev = im->interface;
1269#ifdef CONFIG_IP_MULTICAST
1270 struct net *net = dev_net(in_dev->dev);
1271 int reporter;
1272#endif
1273
1274 if (im->loaded) {
1275 im->loaded = 0;
1276 ip_mc_filter_del(in_dev, im->multiaddr);
1277 }
1278
1279#ifdef CONFIG_IP_MULTICAST
1280 if (im->multiaddr == IGMP_ALL_HOSTS)
1281 return;
1282 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1283 return;
1284
1285 reporter = im->reporter;
1286 igmp_stop_timer(im);
1287
1288 if (!in_dev->dead) {
1289 if (IGMP_V1_SEEN(in_dev))
1290 return;
1291 if (IGMP_V2_SEEN(in_dev)) {
1292 if (reporter)
1293 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1294 return;
1295 }
1296 /* IGMPv3 */
1297 igmpv3_add_delrec(in_dev, im, gfp);
1298
1299 igmp_ifc_event(in_dev);
1300 }
1301#endif
1302}
1303
1304static void igmp_group_dropped(struct ip_mc_list *im)
1305{
1306 __igmp_group_dropped(im, GFP_KERNEL);
1307}
1308
1309static void igmp_group_added(struct ip_mc_list *im)
1310{
1311 struct in_device *in_dev = im->interface;
1312#ifdef CONFIG_IP_MULTICAST
1313 struct net *net = dev_net(in_dev->dev);
1314#endif
1315
1316 if (im->loaded == 0) {
1317 im->loaded = 1;
1318 ip_mc_filter_add(in_dev, im->multiaddr);
1319 }
1320
1321#ifdef CONFIG_IP_MULTICAST
1322 if (im->multiaddr == IGMP_ALL_HOSTS)
1323 return;
1324 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1325 return;
1326
1327 if (in_dev->dead)
1328 return;
1329
1330 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1331 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1332 spin_lock_bh(&im->lock);
1333 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1334 spin_unlock_bh(&im->lock);
1335 return;
1336 }
1337 /* else, v3 */
1338
1339 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1340 * not send filter-mode change record as the mode should be from
1341 * IN() to IN(A).
1342 */
1343 if (im->sfmode == MCAST_EXCLUDE)
1344 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1345
1346 igmp_ifc_event(in_dev);
1347#endif
1348}
1349
1350
1351/*
1352 * Multicast list managers
1353 */
1354
1355static u32 ip_mc_hash(const struct ip_mc_list *im)
1356{
1357 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1358}
1359
1360static void ip_mc_hash_add(struct in_device *in_dev,
1361 struct ip_mc_list *im)
1362{
1363 struct ip_mc_list __rcu **mc_hash;
1364 u32 hash;
1365
1366 mc_hash = rtnl_dereference(in_dev->mc_hash);
1367 if (mc_hash) {
1368 hash = ip_mc_hash(im);
1369 im->next_hash = mc_hash[hash];
1370 rcu_assign_pointer(mc_hash[hash], im);
1371 return;
1372 }
1373
1374 /* do not use a hash table for small number of items */
1375 if (in_dev->mc_count < 4)
1376 return;
1377
1378 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1379 GFP_KERNEL);
1380 if (!mc_hash)
1381 return;
1382
1383 for_each_pmc_rtnl(in_dev, im) {
1384 hash = ip_mc_hash(im);
1385 im->next_hash = mc_hash[hash];
1386 RCU_INIT_POINTER(mc_hash[hash], im);
1387 }
1388
1389 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1390}
1391
1392static void ip_mc_hash_remove(struct in_device *in_dev,
1393 struct ip_mc_list *im)
1394{
1395 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1396 struct ip_mc_list *aux;
1397
1398 if (!mc_hash)
1399 return;
1400 mc_hash += ip_mc_hash(im);
1401 while ((aux = rtnl_dereference(*mc_hash)) != im)
1402 mc_hash = &aux->next_hash;
1403 *mc_hash = im->next_hash;
1404}
1405
1406
1407/*
1408 * A socket has joined a multicast group on device dev.
1409 */
1410static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1411 unsigned int mode, gfp_t gfp)
1412{
1413 struct ip_mc_list *im;
1414
1415 ASSERT_RTNL();
1416
1417 for_each_pmc_rtnl(in_dev, im) {
1418 if (im->multiaddr == addr) {
1419 im->users++;
1420 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
1421 goto out;
1422 }
1423 }
1424
1425 im = kzalloc(sizeof(*im), gfp);
1426 if (!im)
1427 goto out;
1428
1429 im->users = 1;
1430 im->interface = in_dev;
1431 in_dev_hold(in_dev);
1432 im->multiaddr = addr;
1433 /* initial mode is (EX, empty) */
1434 im->sfmode = mode;
1435 im->sfcount[mode] = 1;
1436 refcount_set(&im->refcnt, 1);
1437 spin_lock_init(&im->lock);
1438#ifdef CONFIG_IP_MULTICAST
1439 timer_setup(&im->timer, igmp_timer_expire, 0);
1440#endif
1441
1442 im->next_rcu = in_dev->mc_list;
1443 in_dev->mc_count++;
1444 rcu_assign_pointer(in_dev->mc_list, im);
1445
1446 ip_mc_hash_add(in_dev, im);
1447
1448#ifdef CONFIG_IP_MULTICAST
1449 igmpv3_del_delrec(in_dev, im);
1450#endif
1451 igmp_group_added(im);
1452 if (!in_dev->dead)
1453 ip_rt_multicast_event(in_dev);
1454out:
1455 return;
1456}
1457
1458void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1459{
1460 ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
1461}
1462EXPORT_SYMBOL(__ip_mc_inc_group);
1463
1464void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1465{
1466 __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE);
1467}
1468EXPORT_SYMBOL(ip_mc_inc_group);
1469
1470static int ip_mc_check_iphdr(struct sk_buff *skb)
1471{
1472 const struct iphdr *iph;
1473 unsigned int len;
1474 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1475
1476 if (!pskb_may_pull(skb, offset))
1477 return -EINVAL;
1478
1479 iph = ip_hdr(skb);
1480
1481 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1482 return -EINVAL;
1483
1484 offset += ip_hdrlen(skb) - sizeof(*iph);
1485
1486 if (!pskb_may_pull(skb, offset))
1487 return -EINVAL;
1488
1489 iph = ip_hdr(skb);
1490
1491 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1492 return -EINVAL;
1493
1494 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1495 if (skb->len < len || len < offset)
1496 return -EINVAL;
1497
1498 skb_set_transport_header(skb, offset);
1499
1500 return 0;
1501}
1502
1503static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1504{
1505 unsigned int len = skb_transport_offset(skb);
1506
1507 len += sizeof(struct igmpv3_report);
1508
1509 return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
1510}
1511
1512static int ip_mc_check_igmp_query(struct sk_buff *skb)
1513{
1514 unsigned int transport_len = ip_transport_len(skb);
1515 unsigned int len;
1516
1517 /* IGMPv{1,2}? */
1518 if (transport_len != sizeof(struct igmphdr)) {
1519 /* or IGMPv3? */
1520 if (transport_len < sizeof(struct igmpv3_query))
1521 return -EINVAL;
1522
1523 len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1524 if (!ip_mc_may_pull(skb, len))
1525 return -EINVAL;
1526 }
1527
1528 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1529 * all-systems destination addresses (224.0.0.1) for general queries
1530 */
1531 if (!igmp_hdr(skb)->group &&
1532 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1533 return -EINVAL;
1534
1535 return 0;
1536}
1537
1538static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1539{
1540 switch (igmp_hdr(skb)->type) {
1541 case IGMP_HOST_LEAVE_MESSAGE:
1542 case IGMP_HOST_MEMBERSHIP_REPORT:
1543 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1544 return 0;
1545 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1546 return ip_mc_check_igmp_reportv3(skb);
1547 case IGMP_HOST_MEMBERSHIP_QUERY:
1548 return ip_mc_check_igmp_query(skb);
1549 default:
1550 return -ENOMSG;
1551 }
1552}
1553
1554static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1555{
1556 return skb_checksum_simple_validate(skb);
1557}
1558
1559static int ip_mc_check_igmp_csum(struct sk_buff *skb)
1560{
1561 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1562 unsigned int transport_len = ip_transport_len(skb);
1563 struct sk_buff *skb_chk;
1564
1565 if (!ip_mc_may_pull(skb, len))
1566 return -EINVAL;
1567
1568 skb_chk = skb_checksum_trimmed(skb, transport_len,
1569 ip_mc_validate_checksum);
1570 if (!skb_chk)
1571 return -EINVAL;
1572
1573 if (skb_chk != skb)
1574 kfree_skb(skb_chk);
1575
1576 return 0;
1577}
1578
1579/**
1580 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1581 * @skb: the skb to validate
1582 *
1583 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1584 * skb transport header accordingly and returns zero.
1585 *
1586 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1587 * standard
1588 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1589 * -ENOMEM: A memory allocation failure happened.
1590 *
1591 * Caller needs to set the skb network header and free any returned skb if it
1592 * differs from the provided skb.
1593 */
1594int ip_mc_check_igmp(struct sk_buff *skb)
1595{
1596 int ret = ip_mc_check_iphdr(skb);
1597
1598 if (ret < 0)
1599 return ret;
1600
1601 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1602 return -ENOMSG;
1603
1604 ret = ip_mc_check_igmp_csum(skb);
1605 if (ret < 0)
1606 return ret;
1607
1608 return ip_mc_check_igmp_msg(skb);
1609}
1610EXPORT_SYMBOL(ip_mc_check_igmp);
1611
1612/*
1613 * Resend IGMP JOIN report; used by netdev notifier.
1614 */
1615static void ip_mc_rejoin_groups(struct in_device *in_dev)
1616{
1617#ifdef CONFIG_IP_MULTICAST
1618 struct ip_mc_list *im;
1619 int type;
1620 struct net *net = dev_net(in_dev->dev);
1621
1622 ASSERT_RTNL();
1623
1624 for_each_pmc_rtnl(in_dev, im) {
1625 if (im->multiaddr == IGMP_ALL_HOSTS)
1626 continue;
1627 if (ipv4_is_local_multicast(im->multiaddr) &&
1628 !net->ipv4.sysctl_igmp_llm_reports)
1629 continue;
1630
1631 /* a failover is happening and switches
1632 * must be notified immediately
1633 */
1634 if (IGMP_V1_SEEN(in_dev))
1635 type = IGMP_HOST_MEMBERSHIP_REPORT;
1636 else if (IGMP_V2_SEEN(in_dev))
1637 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1638 else
1639 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1640 igmp_send_report(in_dev, im, type);
1641 }
1642#endif
1643}
1644
1645/*
1646 * A socket has left a multicast group on device dev
1647 */
1648
1649void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1650{
1651 struct ip_mc_list *i;
1652 struct ip_mc_list __rcu **ip;
1653
1654 ASSERT_RTNL();
1655
1656 for (ip = &in_dev->mc_list;
1657 (i = rtnl_dereference(*ip)) != NULL;
1658 ip = &i->next_rcu) {
1659 if (i->multiaddr == addr) {
1660 if (--i->users == 0) {
1661 ip_mc_hash_remove(in_dev, i);
1662 *ip = i->next_rcu;
1663 in_dev->mc_count--;
1664 __igmp_group_dropped(i, gfp);
1665 ip_mc_clear_src(i);
1666
1667 if (!in_dev->dead)
1668 ip_rt_multicast_event(in_dev);
1669
1670 ip_ma_put(i);
1671 return;
1672 }
1673 break;
1674 }
1675 }
1676}
1677EXPORT_SYMBOL(__ip_mc_dec_group);
1678
1679/* Device changing type */
1680
1681void ip_mc_unmap(struct in_device *in_dev)
1682{
1683 struct ip_mc_list *pmc;
1684
1685 ASSERT_RTNL();
1686
1687 for_each_pmc_rtnl(in_dev, pmc)
1688 igmp_group_dropped(pmc);
1689}
1690
1691void ip_mc_remap(struct in_device *in_dev)
1692{
1693 struct ip_mc_list *pmc;
1694
1695 ASSERT_RTNL();
1696
1697 for_each_pmc_rtnl(in_dev, pmc) {
1698#ifdef CONFIG_IP_MULTICAST
1699 igmpv3_del_delrec(in_dev, pmc);
1700#endif
1701 igmp_group_added(pmc);
1702 }
1703}
1704
1705/* Device going down */
1706
1707void ip_mc_down(struct in_device *in_dev)
1708{
1709 struct ip_mc_list *pmc;
1710
1711 ASSERT_RTNL();
1712
1713 for_each_pmc_rtnl(in_dev, pmc)
1714 igmp_group_dropped(pmc);
1715
1716#ifdef CONFIG_IP_MULTICAST
1717 in_dev->mr_ifc_count = 0;
1718 if (del_timer(&in_dev->mr_ifc_timer))
1719 __in_dev_put(in_dev);
1720 in_dev->mr_gq_running = 0;
1721 if (del_timer(&in_dev->mr_gq_timer))
1722 __in_dev_put(in_dev);
1723#endif
1724
1725 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1726}
1727
1728#ifdef CONFIG_IP_MULTICAST
1729static void ip_mc_reset(struct in_device *in_dev)
1730{
1731 struct net *net = dev_net(in_dev->dev);
1732
1733 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1734 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1735 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1736}
1737#else
1738static void ip_mc_reset(struct in_device *in_dev)
1739{
1740}
1741#endif
1742
1743void ip_mc_init_dev(struct in_device *in_dev)
1744{
1745 ASSERT_RTNL();
1746
1747#ifdef CONFIG_IP_MULTICAST
1748 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1749 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
1750#endif
1751 ip_mc_reset(in_dev);
1752
1753 spin_lock_init(&in_dev->mc_tomb_lock);
1754}
1755
1756/* Device going up */
1757
1758void ip_mc_up(struct in_device *in_dev)
1759{
1760 struct ip_mc_list *pmc;
1761
1762 ASSERT_RTNL();
1763
1764 ip_mc_reset(in_dev);
1765 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1766
1767 for_each_pmc_rtnl(in_dev, pmc) {
1768#ifdef CONFIG_IP_MULTICAST
1769 igmpv3_del_delrec(in_dev, pmc);
1770#endif
1771 igmp_group_added(pmc);
1772 }
1773}
1774
1775/*
1776 * Device is about to be destroyed: clean up.
1777 */
1778
1779void ip_mc_destroy_dev(struct in_device *in_dev)
1780{
1781 struct ip_mc_list *i;
1782
1783 ASSERT_RTNL();
1784
1785 /* Deactivate timers */
1786 ip_mc_down(in_dev);
1787#ifdef CONFIG_IP_MULTICAST
1788 igmpv3_clear_delrec(in_dev);
1789#endif
1790
1791 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1792 in_dev->mc_list = i->next_rcu;
1793 in_dev->mc_count--;
1794 ip_ma_put(i);
1795 }
1796}
1797
1798/* RTNL is locked */
1799static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1800{
1801 struct net_device *dev = NULL;
1802 struct in_device *idev = NULL;
1803
1804 if (imr->imr_ifindex) {
1805 idev = inetdev_by_index(net, imr->imr_ifindex);
1806 return idev;
1807 }
1808 if (imr->imr_address.s_addr) {
1809 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1810 if (!dev)
1811 return NULL;
1812 }
1813
1814 if (!dev) {
1815 struct rtable *rt = ip_route_output(net,
1816 imr->imr_multiaddr.s_addr,
1817 0, 0, 0);
1818 if (!IS_ERR(rt)) {
1819 dev = rt->dst.dev;
1820 ip_rt_put(rt);
1821 }
1822 }
1823 if (dev) {
1824 imr->imr_ifindex = dev->ifindex;
1825 idev = __in_dev_get_rtnl(dev);
1826 }
1827 return idev;
1828}
1829
1830/*
1831 * Join a socket to a group
1832 */
1833
1834static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1835 __be32 *psfsrc)
1836{
1837 struct ip_sf_list *psf, *psf_prev;
1838 int rv = 0;
1839
1840 psf_prev = NULL;
1841 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1842 if (psf->sf_inaddr == *psfsrc)
1843 break;
1844 psf_prev = psf;
1845 }
1846 if (!psf || psf->sf_count[sfmode] == 0) {
1847 /* source filter not found, or count wrong => bug */
1848 return -ESRCH;
1849 }
1850 psf->sf_count[sfmode]--;
1851 if (psf->sf_count[sfmode] == 0) {
1852 ip_rt_multicast_event(pmc->interface);
1853 }
1854 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1855#ifdef CONFIG_IP_MULTICAST
1856 struct in_device *in_dev = pmc->interface;
1857 struct net *net = dev_net(in_dev->dev);
1858#endif
1859
1860 /* no more filters for this source */
1861 if (psf_prev)
1862 psf_prev->sf_next = psf->sf_next;
1863 else
1864 pmc->sources = psf->sf_next;
1865#ifdef CONFIG_IP_MULTICAST
1866 if (psf->sf_oldin &&
1867 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1868 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1869 psf->sf_next = pmc->tomb;
1870 pmc->tomb = psf;
1871 rv = 1;
1872 } else
1873#endif
1874 kfree(psf);
1875 }
1876 return rv;
1877}
1878
1879#ifndef CONFIG_IP_MULTICAST
1880#define igmp_ifc_event(x) do { } while (0)
1881#endif
1882
1883static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1884 int sfcount, __be32 *psfsrc, int delta)
1885{
1886 struct ip_mc_list *pmc;
1887 int changerec = 0;
1888 int i, err;
1889
1890 if (!in_dev)
1891 return -ENODEV;
1892 rcu_read_lock();
1893 for_each_pmc_rcu(in_dev, pmc) {
1894 if (*pmca == pmc->multiaddr)
1895 break;
1896 }
1897 if (!pmc) {
1898 /* MCA not found?? bug */
1899 rcu_read_unlock();
1900 return -ESRCH;
1901 }
1902 spin_lock_bh(&pmc->lock);
1903 rcu_read_unlock();
1904#ifdef CONFIG_IP_MULTICAST
1905 sf_markstate(pmc);
1906#endif
1907 if (!delta) {
1908 err = -EINVAL;
1909 if (!pmc->sfcount[sfmode])
1910 goto out_unlock;
1911 pmc->sfcount[sfmode]--;
1912 }
1913 err = 0;
1914 for (i = 0; i < sfcount; i++) {
1915 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1916
1917 changerec |= rv > 0;
1918 if (!err && rv < 0)
1919 err = rv;
1920 }
1921 if (pmc->sfmode == MCAST_EXCLUDE &&
1922 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1923 pmc->sfcount[MCAST_INCLUDE]) {
1924#ifdef CONFIG_IP_MULTICAST
1925 struct ip_sf_list *psf;
1926 struct net *net = dev_net(in_dev->dev);
1927#endif
1928
1929 /* filter mode change */
1930 pmc->sfmode = MCAST_INCLUDE;
1931#ifdef CONFIG_IP_MULTICAST
1932 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1933 in_dev->mr_ifc_count = pmc->crcount;
1934 for (psf = pmc->sources; psf; psf = psf->sf_next)
1935 psf->sf_crcount = 0;
1936 igmp_ifc_event(pmc->interface);
1937 } else if (sf_setstate(pmc) || changerec) {
1938 igmp_ifc_event(pmc->interface);
1939#endif
1940 }
1941out_unlock:
1942 spin_unlock_bh(&pmc->lock);
1943 return err;
1944}
1945
1946/*
1947 * Add multicast single-source filter to the interface list
1948 */
1949static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1950 __be32 *psfsrc)
1951{
1952 struct ip_sf_list *psf, *psf_prev;
1953
1954 psf_prev = NULL;
1955 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1956 if (psf->sf_inaddr == *psfsrc)
1957 break;
1958 psf_prev = psf;
1959 }
1960 if (!psf) {
1961 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1962 if (!psf)
1963 return -ENOBUFS;
1964 psf->sf_inaddr = *psfsrc;
1965 if (psf_prev) {
1966 psf_prev->sf_next = psf;
1967 } else
1968 pmc->sources = psf;
1969 }
1970 psf->sf_count[sfmode]++;
1971 if (psf->sf_count[sfmode] == 1) {
1972 ip_rt_multicast_event(pmc->interface);
1973 }
1974 return 0;
1975}
1976
1977#ifdef CONFIG_IP_MULTICAST
1978static void sf_markstate(struct ip_mc_list *pmc)
1979{
1980 struct ip_sf_list *psf;
1981 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1982
1983 for (psf = pmc->sources; psf; psf = psf->sf_next)
1984 if (pmc->sfcount[MCAST_EXCLUDE]) {
1985 psf->sf_oldin = mca_xcount ==
1986 psf->sf_count[MCAST_EXCLUDE] &&
1987 !psf->sf_count[MCAST_INCLUDE];
1988 } else
1989 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1990}
1991
1992static int sf_setstate(struct ip_mc_list *pmc)
1993{
1994 struct ip_sf_list *psf, *dpsf;
1995 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1996 int qrv = pmc->interface->mr_qrv;
1997 int new_in, rv;
1998
1999 rv = 0;
2000 for (psf = pmc->sources; psf; psf = psf->sf_next) {
2001 if (pmc->sfcount[MCAST_EXCLUDE]) {
2002 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2003 !psf->sf_count[MCAST_INCLUDE];
2004 } else
2005 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2006 if (new_in) {
2007 if (!psf->sf_oldin) {
2008 struct ip_sf_list *prev = NULL;
2009
2010 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2011 if (dpsf->sf_inaddr == psf->sf_inaddr)
2012 break;
2013 prev = dpsf;
2014 }
2015 if (dpsf) {
2016 if (prev)
2017 prev->sf_next = dpsf->sf_next;
2018 else
2019 pmc->tomb = dpsf->sf_next;
2020 kfree(dpsf);
2021 }
2022 psf->sf_crcount = qrv;
2023 rv++;
2024 }
2025 } else if (psf->sf_oldin) {
2026
2027 psf->sf_crcount = 0;
2028 /*
2029 * add or update "delete" records if an active filter
2030 * is now inactive
2031 */
2032 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2033 if (dpsf->sf_inaddr == psf->sf_inaddr)
2034 break;
2035 if (!dpsf) {
2036 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2037 if (!dpsf)
2038 continue;
2039 *dpsf = *psf;
2040 /* pmc->lock held by callers */
2041 dpsf->sf_next = pmc->tomb;
2042 pmc->tomb = dpsf;
2043 }
2044 dpsf->sf_crcount = qrv;
2045 rv++;
2046 }
2047 }
2048 return rv;
2049}
2050#endif
2051
2052/*
2053 * Add multicast source filter list to the interface list
2054 */
2055static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2056 int sfcount, __be32 *psfsrc, int delta)
2057{
2058 struct ip_mc_list *pmc;
2059 int isexclude;
2060 int i, err;
2061
2062 if (!in_dev)
2063 return -ENODEV;
2064 rcu_read_lock();
2065 for_each_pmc_rcu(in_dev, pmc) {
2066 if (*pmca == pmc->multiaddr)
2067 break;
2068 }
2069 if (!pmc) {
2070 /* MCA not found?? bug */
2071 rcu_read_unlock();
2072 return -ESRCH;
2073 }
2074 spin_lock_bh(&pmc->lock);
2075 rcu_read_unlock();
2076
2077#ifdef CONFIG_IP_MULTICAST
2078 sf_markstate(pmc);
2079#endif
2080 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2081 if (!delta)
2082 pmc->sfcount[sfmode]++;
2083 err = 0;
2084 for (i = 0; i < sfcount; i++) {
2085 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2086 if (err)
2087 break;
2088 }
2089 if (err) {
2090 int j;
2091
2092 if (!delta)
2093 pmc->sfcount[sfmode]--;
2094 for (j = 0; j < i; j++)
2095 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2096 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2097#ifdef CONFIG_IP_MULTICAST
2098 struct ip_sf_list *psf;
2099 struct net *net = dev_net(pmc->interface->dev);
2100 in_dev = pmc->interface;
2101#endif
2102
2103 /* filter mode change */
2104 if (pmc->sfcount[MCAST_EXCLUDE])
2105 pmc->sfmode = MCAST_EXCLUDE;
2106 else if (pmc->sfcount[MCAST_INCLUDE])
2107 pmc->sfmode = MCAST_INCLUDE;
2108#ifdef CONFIG_IP_MULTICAST
2109 /* else no filters; keep old mode for reports */
2110
2111 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2112 in_dev->mr_ifc_count = pmc->crcount;
2113 for (psf = pmc->sources; psf; psf = psf->sf_next)
2114 psf->sf_crcount = 0;
2115 igmp_ifc_event(in_dev);
2116 } else if (sf_setstate(pmc)) {
2117 igmp_ifc_event(in_dev);
2118#endif
2119 }
2120 spin_unlock_bh(&pmc->lock);
2121 return err;
2122}
2123
2124static void ip_mc_clear_src(struct ip_mc_list *pmc)
2125{
2126 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
2127
2128 spin_lock_bh(&pmc->lock);
2129 tomb = pmc->tomb;
2130 pmc->tomb = NULL;
2131 sources = pmc->sources;
2132 pmc->sources = NULL;
2133 pmc->sfmode = MCAST_EXCLUDE;
2134 pmc->sfcount[MCAST_INCLUDE] = 0;
2135 pmc->sfcount[MCAST_EXCLUDE] = 1;
2136 spin_unlock_bh(&pmc->lock);
2137
2138 for (psf = tomb; psf; psf = nextpsf) {
2139 nextpsf = psf->sf_next;
2140 kfree(psf);
2141 }
2142 for (psf = sources; psf; psf = nextpsf) {
2143 nextpsf = psf->sf_next;
2144 kfree(psf);
2145 }
2146}
2147
2148/* Join a multicast group
2149 */
2150static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2151 unsigned int mode)
2152{
2153 __be32 addr = imr->imr_multiaddr.s_addr;
2154 struct ip_mc_socklist *iml, *i;
2155 struct in_device *in_dev;
2156 struct inet_sock *inet = inet_sk(sk);
2157 struct net *net = sock_net(sk);
2158 int ifindex;
2159 int count = 0;
2160 int err;
2161
2162 ASSERT_RTNL();
2163
2164 if (!ipv4_is_multicast(addr))
2165 return -EINVAL;
2166
2167 in_dev = ip_mc_find_dev(net, imr);
2168
2169 if (!in_dev) {
2170 err = -ENODEV;
2171 goto done;
2172 }
2173
2174 err = -EADDRINUSE;
2175 ifindex = imr->imr_ifindex;
2176 for_each_pmc_rtnl(inet, i) {
2177 if (i->multi.imr_multiaddr.s_addr == addr &&
2178 i->multi.imr_ifindex == ifindex)
2179 goto done;
2180 count++;
2181 }
2182 err = -ENOBUFS;
2183 if (count >= net->ipv4.sysctl_igmp_max_memberships)
2184 goto done;
2185 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2186 if (!iml)
2187 goto done;
2188
2189 memcpy(&iml->multi, imr, sizeof(*imr));
2190 iml->next_rcu = inet->mc_list;
2191 iml->sflist = NULL;
2192 iml->sfmode = mode;
2193 rcu_assign_pointer(inet->mc_list, iml);
2194 __ip_mc_inc_group(in_dev, addr, mode);
2195 err = 0;
2196done:
2197 return err;
2198}
2199
2200/* Join ASM (Any-Source Multicast) group
2201 */
2202int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2203{
2204 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2205}
2206EXPORT_SYMBOL(ip_mc_join_group);
2207
2208/* Join SSM (Source-Specific Multicast) group
2209 */
2210int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2211 unsigned int mode)
2212{
2213 return __ip_mc_join_group(sk, imr, mode);
2214}
2215
2216static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2217 struct in_device *in_dev)
2218{
2219 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2220 int err;
2221
2222 if (!psf) {
2223 /* any-source empty exclude case */
2224 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2225 iml->sfmode, 0, NULL, 0);
2226 }
2227 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2228 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2229 RCU_INIT_POINTER(iml->sflist, NULL);
2230 /* decrease mem now to avoid the memleak warning */
2231 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2232 kfree_rcu(psf, rcu);
2233 return err;
2234}
2235
2236int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2237{
2238 struct inet_sock *inet = inet_sk(sk);
2239 struct ip_mc_socklist *iml;
2240 struct ip_mc_socklist __rcu **imlp;
2241 struct in_device *in_dev;
2242 struct net *net = sock_net(sk);
2243 __be32 group = imr->imr_multiaddr.s_addr;
2244 u32 ifindex;
2245 int ret = -EADDRNOTAVAIL;
2246
2247 ASSERT_RTNL();
2248
2249 in_dev = ip_mc_find_dev(net, imr);
2250 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2251 ret = -ENODEV;
2252 goto out;
2253 }
2254 ifindex = imr->imr_ifindex;
2255 for (imlp = &inet->mc_list;
2256 (iml = rtnl_dereference(*imlp)) != NULL;
2257 imlp = &iml->next_rcu) {
2258 if (iml->multi.imr_multiaddr.s_addr != group)
2259 continue;
2260 if (ifindex) {
2261 if (iml->multi.imr_ifindex != ifindex)
2262 continue;
2263 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2264 iml->multi.imr_address.s_addr)
2265 continue;
2266
2267 (void) ip_mc_leave_src(sk, iml, in_dev);
2268
2269 *imlp = iml->next_rcu;
2270
2271 if (in_dev)
2272 ip_mc_dec_group(in_dev, group);
2273
2274 /* decrease mem now to avoid the memleak warning */
2275 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2276 kfree_rcu(iml, rcu);
2277 return 0;
2278 }
2279out:
2280 return ret;
2281}
2282EXPORT_SYMBOL(ip_mc_leave_group);
2283
2284int ip_mc_source(int add, int omode, struct sock *sk, struct
2285 ip_mreq_source *mreqs, int ifindex)
2286{
2287 int err;
2288 struct ip_mreqn imr;
2289 __be32 addr = mreqs->imr_multiaddr;
2290 struct ip_mc_socklist *pmc;
2291 struct in_device *in_dev = NULL;
2292 struct inet_sock *inet = inet_sk(sk);
2293 struct ip_sf_socklist *psl;
2294 struct net *net = sock_net(sk);
2295 int leavegroup = 0;
2296 int i, j, rv;
2297
2298 if (!ipv4_is_multicast(addr))
2299 return -EINVAL;
2300
2301 ASSERT_RTNL();
2302
2303 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2304 imr.imr_address.s_addr = mreqs->imr_interface;
2305 imr.imr_ifindex = ifindex;
2306 in_dev = ip_mc_find_dev(net, &imr);
2307
2308 if (!in_dev) {
2309 err = -ENODEV;
2310 goto done;
2311 }
2312 err = -EADDRNOTAVAIL;
2313
2314 for_each_pmc_rtnl(inet, pmc) {
2315 if ((pmc->multi.imr_multiaddr.s_addr ==
2316 imr.imr_multiaddr.s_addr) &&
2317 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2318 break;
2319 }
2320 if (!pmc) { /* must have a prior join */
2321 err = -EINVAL;
2322 goto done;
2323 }
2324 /* if a source filter was set, must be the same mode as before */
2325 if (pmc->sflist) {
2326 if (pmc->sfmode != omode) {
2327 err = -EINVAL;
2328 goto done;
2329 }
2330 } else if (pmc->sfmode != omode) {
2331 /* allow mode switches for empty-set filters */
2332 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2333 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2334 NULL, 0);
2335 pmc->sfmode = omode;
2336 }
2337
2338 psl = rtnl_dereference(pmc->sflist);
2339 if (!add) {
2340 if (!psl)
2341 goto done; /* err = -EADDRNOTAVAIL */
2342 rv = !0;
2343 for (i = 0; i < psl->sl_count; i++) {
2344 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2345 sizeof(__be32));
2346 if (rv == 0)
2347 break;
2348 }
2349 if (rv) /* source not found */
2350 goto done; /* err = -EADDRNOTAVAIL */
2351
2352 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2353 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2354 leavegroup = 1;
2355 goto done;
2356 }
2357
2358 /* update the interface filter */
2359 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2360 &mreqs->imr_sourceaddr, 1);
2361
2362 for (j = i+1; j < psl->sl_count; j++)
2363 psl->sl_addr[j-1] = psl->sl_addr[j];
2364 psl->sl_count--;
2365 err = 0;
2366 goto done;
2367 }
2368 /* else, add a new source to the filter */
2369
2370 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2371 err = -ENOBUFS;
2372 goto done;
2373 }
2374 if (!psl || psl->sl_count == psl->sl_max) {
2375 struct ip_sf_socklist *newpsl;
2376 int count = IP_SFBLOCK;
2377
2378 if (psl)
2379 count += psl->sl_max;
2380 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2381 if (!newpsl) {
2382 err = -ENOBUFS;
2383 goto done;
2384 }
2385 newpsl->sl_max = count;
2386 newpsl->sl_count = count - IP_SFBLOCK;
2387 if (psl) {
2388 for (i = 0; i < psl->sl_count; i++)
2389 newpsl->sl_addr[i] = psl->sl_addr[i];
2390 /* decrease mem now to avoid the memleak warning */
2391 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2392 kfree_rcu(psl, rcu);
2393 }
2394 rcu_assign_pointer(pmc->sflist, newpsl);
2395 psl = newpsl;
2396 }
2397 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2398 for (i = 0; i < psl->sl_count; i++) {
2399 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2400 sizeof(__be32));
2401 if (rv == 0)
2402 break;
2403 }
2404 if (rv == 0) /* address already there is an error */
2405 goto done;
2406 for (j = psl->sl_count-1; j >= i; j--)
2407 psl->sl_addr[j+1] = psl->sl_addr[j];
2408 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2409 psl->sl_count++;
2410 err = 0;
2411 /* update the interface list */
2412 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2413 &mreqs->imr_sourceaddr, 1);
2414done:
2415 if (leavegroup)
2416 err = ip_mc_leave_group(sk, &imr);
2417 return err;
2418}
2419
2420int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2421{
2422 int err = 0;
2423 struct ip_mreqn imr;
2424 __be32 addr = msf->imsf_multiaddr;
2425 struct ip_mc_socklist *pmc;
2426 struct in_device *in_dev;
2427 struct inet_sock *inet = inet_sk(sk);
2428 struct ip_sf_socklist *newpsl, *psl;
2429 struct net *net = sock_net(sk);
2430 int leavegroup = 0;
2431
2432 if (!ipv4_is_multicast(addr))
2433 return -EINVAL;
2434 if (msf->imsf_fmode != MCAST_INCLUDE &&
2435 msf->imsf_fmode != MCAST_EXCLUDE)
2436 return -EINVAL;
2437
2438 ASSERT_RTNL();
2439
2440 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2441 imr.imr_address.s_addr = msf->imsf_interface;
2442 imr.imr_ifindex = ifindex;
2443 in_dev = ip_mc_find_dev(net, &imr);
2444
2445 if (!in_dev) {
2446 err = -ENODEV;
2447 goto done;
2448 }
2449
2450 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2451 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2452 leavegroup = 1;
2453 goto done;
2454 }
2455
2456 for_each_pmc_rtnl(inet, pmc) {
2457 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2458 pmc->multi.imr_ifindex == imr.imr_ifindex)
2459 break;
2460 }
2461 if (!pmc) { /* must have a prior join */
2462 err = -EINVAL;
2463 goto done;
2464 }
2465 if (msf->imsf_numsrc) {
2466 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2467 GFP_KERNEL);
2468 if (!newpsl) {
2469 err = -ENOBUFS;
2470 goto done;
2471 }
2472 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2473 memcpy(newpsl->sl_addr, msf->imsf_slist,
2474 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2475 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2476 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2477 if (err) {
2478 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2479 goto done;
2480 }
2481 } else {
2482 newpsl = NULL;
2483 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2484 msf->imsf_fmode, 0, NULL, 0);
2485 }
2486 psl = rtnl_dereference(pmc->sflist);
2487 if (psl) {
2488 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2489 psl->sl_count, psl->sl_addr, 0);
2490 /* decrease mem now to avoid the memleak warning */
2491 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2492 kfree_rcu(psl, rcu);
2493 } else
2494 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2495 0, NULL, 0);
2496 rcu_assign_pointer(pmc->sflist, newpsl);
2497 pmc->sfmode = msf->imsf_fmode;
2498 err = 0;
2499done:
2500 if (leavegroup)
2501 err = ip_mc_leave_group(sk, &imr);
2502 return err;
2503}
2504
2505int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2506 struct ip_msfilter __user *optval, int __user *optlen)
2507{
2508 int err, len, count, copycount;
2509 struct ip_mreqn imr;
2510 __be32 addr = msf->imsf_multiaddr;
2511 struct ip_mc_socklist *pmc;
2512 struct in_device *in_dev;
2513 struct inet_sock *inet = inet_sk(sk);
2514 struct ip_sf_socklist *psl;
2515 struct net *net = sock_net(sk);
2516
2517 ASSERT_RTNL();
2518
2519 if (!ipv4_is_multicast(addr))
2520 return -EINVAL;
2521
2522 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2523 imr.imr_address.s_addr = msf->imsf_interface;
2524 imr.imr_ifindex = 0;
2525 in_dev = ip_mc_find_dev(net, &imr);
2526
2527 if (!in_dev) {
2528 err = -ENODEV;
2529 goto done;
2530 }
2531 err = -EADDRNOTAVAIL;
2532
2533 for_each_pmc_rtnl(inet, pmc) {
2534 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2535 pmc->multi.imr_ifindex == imr.imr_ifindex)
2536 break;
2537 }
2538 if (!pmc) /* must have a prior join */
2539 goto done;
2540 msf->imsf_fmode = pmc->sfmode;
2541 psl = rtnl_dereference(pmc->sflist);
2542 if (!psl) {
2543 len = 0;
2544 count = 0;
2545 } else {
2546 count = psl->sl_count;
2547 }
2548 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2549 len = copycount * sizeof(psl->sl_addr[0]);
2550 msf->imsf_numsrc = count;
2551 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2552 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2553 return -EFAULT;
2554 }
2555 if (len &&
2556 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2557 return -EFAULT;
2558 return 0;
2559done:
2560 return err;
2561}
2562
2563int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2564 struct group_filter __user *optval, int __user *optlen)
2565{
2566 int err, i, count, copycount;
2567 struct sockaddr_in *psin;
2568 __be32 addr;
2569 struct ip_mc_socklist *pmc;
2570 struct inet_sock *inet = inet_sk(sk);
2571 struct ip_sf_socklist *psl;
2572
2573 ASSERT_RTNL();
2574
2575 psin = (struct sockaddr_in *)&gsf->gf_group;
2576 if (psin->sin_family != AF_INET)
2577 return -EINVAL;
2578 addr = psin->sin_addr.s_addr;
2579 if (!ipv4_is_multicast(addr))
2580 return -EINVAL;
2581
2582 err = -EADDRNOTAVAIL;
2583
2584 for_each_pmc_rtnl(inet, pmc) {
2585 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2586 pmc->multi.imr_ifindex == gsf->gf_interface)
2587 break;
2588 }
2589 if (!pmc) /* must have a prior join */
2590 goto done;
2591 gsf->gf_fmode = pmc->sfmode;
2592 psl = rtnl_dereference(pmc->sflist);
2593 count = psl ? psl->sl_count : 0;
2594 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2595 gsf->gf_numsrc = count;
2596 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2597 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2598 return -EFAULT;
2599 }
2600 for (i = 0; i < copycount; i++) {
2601 struct sockaddr_storage ss;
2602
2603 psin = (struct sockaddr_in *)&ss;
2604 memset(&ss, 0, sizeof(ss));
2605 psin->sin_family = AF_INET;
2606 psin->sin_addr.s_addr = psl->sl_addr[i];
2607 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2608 return -EFAULT;
2609 }
2610 return 0;
2611done:
2612 return err;
2613}
2614
2615/*
2616 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2617 */
2618int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2619 int dif, int sdif)
2620{
2621 struct inet_sock *inet = inet_sk(sk);
2622 struct ip_mc_socklist *pmc;
2623 struct ip_sf_socklist *psl;
2624 int i;
2625 int ret;
2626
2627 ret = 1;
2628 if (!ipv4_is_multicast(loc_addr))
2629 goto out;
2630
2631 rcu_read_lock();
2632 for_each_pmc_rcu(inet, pmc) {
2633 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2634 (pmc->multi.imr_ifindex == dif ||
2635 (sdif && pmc->multi.imr_ifindex == sdif)))
2636 break;
2637 }
2638 ret = inet->mc_all;
2639 if (!pmc)
2640 goto unlock;
2641 psl = rcu_dereference(pmc->sflist);
2642 ret = (pmc->sfmode == MCAST_EXCLUDE);
2643 if (!psl)
2644 goto unlock;
2645
2646 for (i = 0; i < psl->sl_count; i++) {
2647 if (psl->sl_addr[i] == rmt_addr)
2648 break;
2649 }
2650 ret = 0;
2651 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2652 goto unlock;
2653 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2654 goto unlock;
2655 ret = 1;
2656unlock:
2657 rcu_read_unlock();
2658out:
2659 return ret;
2660}
2661
2662/*
2663 * A socket is closing.
2664 */
2665
2666void ip_mc_drop_socket(struct sock *sk)
2667{
2668 struct inet_sock *inet = inet_sk(sk);
2669 struct ip_mc_socklist *iml;
2670 struct net *net = sock_net(sk);
2671
2672 if (!inet->mc_list)
2673 return;
2674
2675 rtnl_lock();
2676 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2677 struct in_device *in_dev;
2678
2679 inet->mc_list = iml->next_rcu;
2680 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2681 (void) ip_mc_leave_src(sk, iml, in_dev);
2682 if (in_dev)
2683 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2684 /* decrease mem now to avoid the memleak warning */
2685 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2686 kfree_rcu(iml, rcu);
2687 }
2688 rtnl_unlock();
2689}
2690
2691/* called with rcu_read_lock() */
2692int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2693{
2694 struct ip_mc_list *im;
2695 struct ip_mc_list __rcu **mc_hash;
2696 struct ip_sf_list *psf;
2697 int rv = 0;
2698
2699 mc_hash = rcu_dereference(in_dev->mc_hash);
2700 if (mc_hash) {
2701 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2702
2703 for (im = rcu_dereference(mc_hash[hash]);
2704 im != NULL;
2705 im = rcu_dereference(im->next_hash)) {
2706 if (im->multiaddr == mc_addr)
2707 break;
2708 }
2709 } else {
2710 for_each_pmc_rcu(in_dev, im) {
2711 if (im->multiaddr == mc_addr)
2712 break;
2713 }
2714 }
2715 if (im && proto == IPPROTO_IGMP) {
2716 rv = 1;
2717 } else if (im) {
2718 if (src_addr) {
2719 for (psf = im->sources; psf; psf = psf->sf_next) {
2720 if (psf->sf_inaddr == src_addr)
2721 break;
2722 }
2723 if (psf)
2724 rv = psf->sf_count[MCAST_INCLUDE] ||
2725 psf->sf_count[MCAST_EXCLUDE] !=
2726 im->sfcount[MCAST_EXCLUDE];
2727 else
2728 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2729 } else
2730 rv = 1; /* unspecified source; tentatively allow */
2731 }
2732 return rv;
2733}
2734
2735#if defined(CONFIG_PROC_FS)
2736struct igmp_mc_iter_state {
2737 struct seq_net_private p;
2738 struct net_device *dev;
2739 struct in_device *in_dev;
2740};
2741
2742#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2743
2744static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2745{
2746 struct net *net = seq_file_net(seq);
2747 struct ip_mc_list *im = NULL;
2748 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2749
2750 state->in_dev = NULL;
2751 for_each_netdev_rcu(net, state->dev) {
2752 struct in_device *in_dev;
2753
2754 in_dev = __in_dev_get_rcu(state->dev);
2755 if (!in_dev)
2756 continue;
2757 im = rcu_dereference(in_dev->mc_list);
2758 if (im) {
2759 state->in_dev = in_dev;
2760 break;
2761 }
2762 }
2763 return im;
2764}
2765
2766static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2767{
2768 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2769
2770 im = rcu_dereference(im->next_rcu);
2771 while (!im) {
2772 state->dev = next_net_device_rcu(state->dev);
2773 if (!state->dev) {
2774 state->in_dev = NULL;
2775 break;
2776 }
2777 state->in_dev = __in_dev_get_rcu(state->dev);
2778 if (!state->in_dev)
2779 continue;
2780 im = rcu_dereference(state->in_dev->mc_list);
2781 }
2782 return im;
2783}
2784
2785static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2786{
2787 struct ip_mc_list *im = igmp_mc_get_first(seq);
2788 if (im)
2789 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2790 --pos;
2791 return pos ? NULL : im;
2792}
2793
2794static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2795 __acquires(rcu)
2796{
2797 rcu_read_lock();
2798 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2799}
2800
2801static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2802{
2803 struct ip_mc_list *im;
2804 if (v == SEQ_START_TOKEN)
2805 im = igmp_mc_get_first(seq);
2806 else
2807 im = igmp_mc_get_next(seq, v);
2808 ++*pos;
2809 return im;
2810}
2811
2812static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2813 __releases(rcu)
2814{
2815 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2816
2817 state->in_dev = NULL;
2818 state->dev = NULL;
2819 rcu_read_unlock();
2820}
2821
2822static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2823{
2824 if (v == SEQ_START_TOKEN)
2825 seq_puts(seq,
2826 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2827 else {
2828 struct ip_mc_list *im = (struct ip_mc_list *)v;
2829 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2830 char *querier;
2831 long delta;
2832
2833#ifdef CONFIG_IP_MULTICAST
2834 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2835 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2836 "V3";
2837#else
2838 querier = "NONE";
2839#endif
2840
2841 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2842 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2843 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2844 }
2845
2846 delta = im->timer.expires - jiffies;
2847 seq_printf(seq,
2848 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2849 im->multiaddr, im->users,
2850 im->tm_running,
2851 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2852 im->reporter);
2853 }
2854 return 0;
2855}
2856
2857static const struct seq_operations igmp_mc_seq_ops = {
2858 .start = igmp_mc_seq_start,
2859 .next = igmp_mc_seq_next,
2860 .stop = igmp_mc_seq_stop,
2861 .show = igmp_mc_seq_show,
2862};
2863
2864struct igmp_mcf_iter_state {
2865 struct seq_net_private p;
2866 struct net_device *dev;
2867 struct in_device *idev;
2868 struct ip_mc_list *im;
2869};
2870
2871#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2872
2873static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2874{
2875 struct net *net = seq_file_net(seq);
2876 struct ip_sf_list *psf = NULL;
2877 struct ip_mc_list *im = NULL;
2878 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2879
2880 state->idev = NULL;
2881 state->im = NULL;
2882 for_each_netdev_rcu(net, state->dev) {
2883 struct in_device *idev;
2884 idev = __in_dev_get_rcu(state->dev);
2885 if (unlikely(!idev))
2886 continue;
2887 im = rcu_dereference(idev->mc_list);
2888 if (likely(im)) {
2889 spin_lock_bh(&im->lock);
2890 psf = im->sources;
2891 if (likely(psf)) {
2892 state->im = im;
2893 state->idev = idev;
2894 break;
2895 }
2896 spin_unlock_bh(&im->lock);
2897 }
2898 }
2899 return psf;
2900}
2901
2902static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2903{
2904 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2905
2906 psf = psf->sf_next;
2907 while (!psf) {
2908 spin_unlock_bh(&state->im->lock);
2909 state->im = state->im->next;
2910 while (!state->im) {
2911 state->dev = next_net_device_rcu(state->dev);
2912 if (!state->dev) {
2913 state->idev = NULL;
2914 goto out;
2915 }
2916 state->idev = __in_dev_get_rcu(state->dev);
2917 if (!state->idev)
2918 continue;
2919 state->im = rcu_dereference(state->idev->mc_list);
2920 }
2921 if (!state->im)
2922 break;
2923 spin_lock_bh(&state->im->lock);
2924 psf = state->im->sources;
2925 }
2926out:
2927 return psf;
2928}
2929
2930static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2931{
2932 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2933 if (psf)
2934 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2935 --pos;
2936 return pos ? NULL : psf;
2937}
2938
2939static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2940 __acquires(rcu)
2941{
2942 rcu_read_lock();
2943 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2944}
2945
2946static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2947{
2948 struct ip_sf_list *psf;
2949 if (v == SEQ_START_TOKEN)
2950 psf = igmp_mcf_get_first(seq);
2951 else
2952 psf = igmp_mcf_get_next(seq, v);
2953 ++*pos;
2954 return psf;
2955}
2956
2957static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2958 __releases(rcu)
2959{
2960 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2961 if (likely(state->im)) {
2962 spin_unlock_bh(&state->im->lock);
2963 state->im = NULL;
2964 }
2965 state->idev = NULL;
2966 state->dev = NULL;
2967 rcu_read_unlock();
2968}
2969
2970static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2971{
2972 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2973 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2974
2975 if (v == SEQ_START_TOKEN) {
2976 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2977 } else {
2978 seq_printf(seq,
2979 "%3d %6.6s 0x%08x "
2980 "0x%08x %6lu %6lu\n",
2981 state->dev->ifindex, state->dev->name,
2982 ntohl(state->im->multiaddr),
2983 ntohl(psf->sf_inaddr),
2984 psf->sf_count[MCAST_INCLUDE],
2985 psf->sf_count[MCAST_EXCLUDE]);
2986 }
2987 return 0;
2988}
2989
2990static const struct seq_operations igmp_mcf_seq_ops = {
2991 .start = igmp_mcf_seq_start,
2992 .next = igmp_mcf_seq_next,
2993 .stop = igmp_mcf_seq_stop,
2994 .show = igmp_mcf_seq_show,
2995};
2996
2997static int __net_init igmp_net_init(struct net *net)
2998{
2999 struct proc_dir_entry *pde;
3000 int err;
3001
3002 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3003 sizeof(struct igmp_mc_iter_state));
3004 if (!pde)
3005 goto out_igmp;
3006 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3007 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
3008 if (!pde)
3009 goto out_mcfilter;
3010 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3011 SOCK_DGRAM, 0, net);
3012 if (err < 0) {
3013 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3014 err);
3015 goto out_sock;
3016 }
3017
3018 return 0;
3019
3020out_sock:
3021 remove_proc_entry("mcfilter", net->proc_net);
3022out_mcfilter:
3023 remove_proc_entry("igmp", net->proc_net);
3024out_igmp:
3025 return -ENOMEM;
3026}
3027
3028static void __net_exit igmp_net_exit(struct net *net)
3029{
3030 remove_proc_entry("mcfilter", net->proc_net);
3031 remove_proc_entry("igmp", net->proc_net);
3032 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3033}
3034
3035static struct pernet_operations igmp_net_ops = {
3036 .init = igmp_net_init,
3037 .exit = igmp_net_exit,
3038};
3039#endif
3040
3041static int igmp_netdev_event(struct notifier_block *this,
3042 unsigned long event, void *ptr)
3043{
3044 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3045 struct in_device *in_dev;
3046
3047 switch (event) {
3048 case NETDEV_RESEND_IGMP:
3049 in_dev = __in_dev_get_rtnl(dev);
3050 if (in_dev)
3051 ip_mc_rejoin_groups(in_dev);
3052 break;
3053 default:
3054 break;
3055 }
3056 return NOTIFY_DONE;
3057}
3058
3059static struct notifier_block igmp_notifier = {
3060 .notifier_call = igmp_netdev_event,
3061};
3062
3063int __init igmp_mc_init(void)
3064{
3065#if defined(CONFIG_PROC_FS)
3066 int err;
3067
3068 err = register_pernet_subsys(&igmp_net_ops);
3069 if (err)
3070 return err;
3071 err = register_netdevice_notifier(&igmp_notifier);
3072 if (err)
3073 goto reg_notif_fail;
3074 return 0;
3075
3076reg_notif_fail:
3077 unregister_pernet_subsys(&igmp_net_ops);
3078 return err;
3079#else
3080 return register_netdevice_notifier(&igmp_notifier);
3081#endif
3082}
3083