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