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