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