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