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