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