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