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