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