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