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