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