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