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