Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.16-rc1 2503 lines 61 kB view raw
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 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 261static int 262igmp_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 275static 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 334static 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 351static 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 356static 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 381static 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 486static 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 */ 522static 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 540static 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 613static 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 682static 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 691static 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 703static 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 713static 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 738static 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 756static 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 775static 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 865int 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 922drop: 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 935static 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 955static 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 */ 968static 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 = 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 1006static 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 1035static 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 1068static 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 } 1100done: 1101#endif 1102 ip_mc_clear_src(im); 1103} 1104 1105static 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 1144void 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 = 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); 1195out: 1196 return; 1197} 1198 1199/* 1200 * A socket has left a multicast group on device dev 1201 */ 1202 1203void 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 1230void 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 1252void 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 1275void 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 1291void 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 1313static 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 */ 1348int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS; 1349int sysctl_igmp_max_msf = IP_MAX_MSF; 1350 1351 1352static 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 1401static 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 } 1459out_unlock: 1460 spin_unlock_bh(&pmc->lock); 1461 return err; 1462} 1463 1464/* 1465 * Add multicast single-source filter to the interface list 1466 */ 1467static 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 = 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 1497static 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 1511static 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 */ 1537static 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 1605static 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 */ 1628int 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 = 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; 1673done: 1674 rtnl_shunlock(); 1675 return err; 1676} 1677 1678static 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 1700int 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 1732int 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 = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 1827 if (!newpsl) { 1828 err = -ENOBUFS; 1829 goto done; 1830 } 1831 newpsl->sl_max = count; 1832 newpsl->sl_count = count - IP_SFBLOCK; 1833 if (psl) { 1834 for (i=0; i<psl->sl_count; i++) 1835 newpsl->sl_addr[i] = psl->sl_addr[i]; 1836 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max)); 1837 } 1838 pmc->sflist = psl = newpsl; 1839 } 1840 rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 1841 for (i=0; i<psl->sl_count; i++) { 1842 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 1843 sizeof(__u32)); 1844 if (rv == 0) 1845 break; 1846 } 1847 if (rv == 0) /* address already there is an error */ 1848 goto done; 1849 for (j=psl->sl_count-1; j>=i; j--) 1850 psl->sl_addr[j+1] = psl->sl_addr[j]; 1851 psl->sl_addr[i] = mreqs->imr_sourceaddr; 1852 psl->sl_count++; 1853 err = 0; 1854 /* update the interface list */ 1855 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 1856 &mreqs->imr_sourceaddr, 1); 1857done: 1858 rtnl_shunlock(); 1859 if (leavegroup) 1860 return ip_mc_leave_group(sk, &imr); 1861 return err; 1862} 1863 1864int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 1865{ 1866 int err = 0; 1867 struct ip_mreqn imr; 1868 u32 addr = msf->imsf_multiaddr; 1869 struct ip_mc_socklist *pmc; 1870 struct in_device *in_dev; 1871 struct inet_sock *inet = inet_sk(sk); 1872 struct ip_sf_socklist *newpsl, *psl; 1873 int leavegroup = 0; 1874 1875 if (!MULTICAST(addr)) 1876 return -EINVAL; 1877 if (msf->imsf_fmode != MCAST_INCLUDE && 1878 msf->imsf_fmode != MCAST_EXCLUDE) 1879 return -EINVAL; 1880 1881 rtnl_shlock(); 1882 1883 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 1884 imr.imr_address.s_addr = msf->imsf_interface; 1885 imr.imr_ifindex = ifindex; 1886 in_dev = ip_mc_find_dev(&imr); 1887 1888 if (!in_dev) { 1889 err = -ENODEV; 1890 goto done; 1891 } 1892 1893 /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 1894 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 1895 leavegroup = 1; 1896 goto done; 1897 } 1898 1899 for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 1900 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 1901 pmc->multi.imr_ifindex == imr.imr_ifindex) 1902 break; 1903 } 1904 if (!pmc) { /* must have a prior join */ 1905 err = -EINVAL; 1906 goto done; 1907 } 1908 if (msf->imsf_numsrc) { 1909 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 1910 GFP_KERNEL); 1911 if (!newpsl) { 1912 err = -ENOBUFS; 1913 goto done; 1914 } 1915 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 1916 memcpy(newpsl->sl_addr, msf->imsf_slist, 1917 msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 1918 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 1919 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 1920 if (err) { 1921 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 1922 goto done; 1923 } 1924 } else { 1925 newpsl = NULL; 1926 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 1927 msf->imsf_fmode, 0, NULL, 0); 1928 } 1929 psl = pmc->sflist; 1930 if (psl) { 1931 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 1932 psl->sl_count, psl->sl_addr, 0); 1933 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max)); 1934 } else 1935 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 1936 0, NULL, 0); 1937 pmc->sflist = newpsl; 1938 pmc->sfmode = msf->imsf_fmode; 1939 err = 0; 1940done: 1941 rtnl_shunlock(); 1942 if (leavegroup) 1943 err = ip_mc_leave_group(sk, &imr); 1944 return err; 1945} 1946 1947int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 1948 struct ip_msfilter __user *optval, int __user *optlen) 1949{ 1950 int err, len, count, copycount; 1951 struct ip_mreqn imr; 1952 u32 addr = msf->imsf_multiaddr; 1953 struct ip_mc_socklist *pmc; 1954 struct in_device *in_dev; 1955 struct inet_sock *inet = inet_sk(sk); 1956 struct ip_sf_socklist *psl; 1957 1958 if (!MULTICAST(addr)) 1959 return -EINVAL; 1960 1961 rtnl_shlock(); 1962 1963 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 1964 imr.imr_address.s_addr = msf->imsf_interface; 1965 imr.imr_ifindex = 0; 1966 in_dev = ip_mc_find_dev(&imr); 1967 1968 if (!in_dev) { 1969 err = -ENODEV; 1970 goto done; 1971 } 1972 err = -EADDRNOTAVAIL; 1973 1974 for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 1975 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 1976 pmc->multi.imr_ifindex == imr.imr_ifindex) 1977 break; 1978 } 1979 if (!pmc) /* must have a prior join */ 1980 goto done; 1981 msf->imsf_fmode = pmc->sfmode; 1982 psl = pmc->sflist; 1983 rtnl_shunlock(); 1984 if (!psl) { 1985 len = 0; 1986 count = 0; 1987 } else { 1988 count = psl->sl_count; 1989 } 1990 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 1991 len = copycount * sizeof(psl->sl_addr[0]); 1992 msf->imsf_numsrc = count; 1993 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 1994 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 1995 return -EFAULT; 1996 } 1997 if (len && 1998 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 1999 return -EFAULT; 2000 return 0; 2001done: 2002 rtnl_shunlock(); 2003 return err; 2004} 2005 2006int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 2007 struct group_filter __user *optval, int __user *optlen) 2008{ 2009 int err, i, count, copycount; 2010 struct sockaddr_in *psin; 2011 u32 addr; 2012 struct ip_mc_socklist *pmc; 2013 struct inet_sock *inet = inet_sk(sk); 2014 struct ip_sf_socklist *psl; 2015 2016 psin = (struct sockaddr_in *)&gsf->gf_group; 2017 if (psin->sin_family != AF_INET) 2018 return -EINVAL; 2019 addr = psin->sin_addr.s_addr; 2020 if (!MULTICAST(addr)) 2021 return -EINVAL; 2022 2023 rtnl_shlock(); 2024 2025 err = -EADDRNOTAVAIL; 2026 2027 for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 2028 if (pmc->multi.imr_multiaddr.s_addr == addr && 2029 pmc->multi.imr_ifindex == gsf->gf_interface) 2030 break; 2031 } 2032 if (!pmc) /* must have a prior join */ 2033 goto done; 2034 gsf->gf_fmode = pmc->sfmode; 2035 psl = pmc->sflist; 2036 rtnl_shunlock(); 2037 count = psl ? psl->sl_count : 0; 2038 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 2039 gsf->gf_numsrc = count; 2040 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 2041 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 2042 return -EFAULT; 2043 } 2044 for (i=0; i<copycount; i++) { 2045 struct sockaddr_in *psin; 2046 struct sockaddr_storage ss; 2047 2048 psin = (struct sockaddr_in *)&ss; 2049 memset(&ss, 0, sizeof(ss)); 2050 psin->sin_family = AF_INET; 2051 psin->sin_addr.s_addr = psl->sl_addr[i]; 2052 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 2053 return -EFAULT; 2054 } 2055 return 0; 2056done: 2057 rtnl_shunlock(); 2058 return err; 2059} 2060 2061/* 2062 * check if a multicast source filter allows delivery for a given <src,dst,intf> 2063 */ 2064int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif) 2065{ 2066 struct inet_sock *inet = inet_sk(sk); 2067 struct ip_mc_socklist *pmc; 2068 struct ip_sf_socklist *psl; 2069 int i; 2070 2071 if (!MULTICAST(loc_addr)) 2072 return 1; 2073 2074 for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 2075 if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 2076 pmc->multi.imr_ifindex == dif) 2077 break; 2078 } 2079 if (!pmc) 2080 return 1; 2081 psl = pmc->sflist; 2082 if (!psl) 2083 return pmc->sfmode == MCAST_EXCLUDE; 2084 2085 for (i=0; i<psl->sl_count; i++) { 2086 if (psl->sl_addr[i] == rmt_addr) 2087 break; 2088 } 2089 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2090 return 0; 2091 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2092 return 0; 2093 return 1; 2094} 2095 2096/* 2097 * A socket is closing. 2098 */ 2099 2100void ip_mc_drop_socket(struct sock *sk) 2101{ 2102 struct inet_sock *inet = inet_sk(sk); 2103 struct ip_mc_socklist *iml; 2104 2105 if (inet->mc_list == NULL) 2106 return; 2107 2108 rtnl_lock(); 2109 while ((iml = inet->mc_list) != NULL) { 2110 struct in_device *in_dev; 2111 inet->mc_list = iml->next; 2112 2113 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) { 2114 (void) ip_mc_leave_src(sk, iml, in_dev); 2115 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2116 in_dev_put(in_dev); 2117 } 2118 sock_kfree_s(sk, iml, sizeof(*iml)); 2119 2120 } 2121 rtnl_unlock(); 2122} 2123 2124int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto) 2125{ 2126 struct ip_mc_list *im; 2127 struct ip_sf_list *psf; 2128 int rv = 0; 2129 2130 read_lock(&in_dev->mc_list_lock); 2131 for (im=in_dev->mc_list; im; im=im->next) { 2132 if (im->multiaddr == mc_addr) 2133 break; 2134 } 2135 if (im && proto == IPPROTO_IGMP) { 2136 rv = 1; 2137 } else if (im) { 2138 if (src_addr) { 2139 for (psf=im->sources; psf; psf=psf->sf_next) { 2140 if (psf->sf_inaddr == src_addr) 2141 break; 2142 } 2143 if (psf) 2144 rv = psf->sf_count[MCAST_INCLUDE] || 2145 psf->sf_count[MCAST_EXCLUDE] != 2146 im->sfcount[MCAST_EXCLUDE]; 2147 else 2148 rv = im->sfcount[MCAST_EXCLUDE] != 0; 2149 } else 2150 rv = 1; /* unspecified source; tentatively allow */ 2151 } 2152 read_unlock(&in_dev->mc_list_lock); 2153 return rv; 2154} 2155 2156#if defined(CONFIG_PROC_FS) 2157struct igmp_mc_iter_state { 2158 struct net_device *dev; 2159 struct in_device *in_dev; 2160}; 2161 2162#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 2163 2164static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 2165{ 2166 struct ip_mc_list *im = NULL; 2167 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 2168 2169 for (state->dev = dev_base, state->in_dev = NULL; 2170 state->dev; 2171 state->dev = state->dev->next) { 2172 struct in_device *in_dev; 2173 in_dev = in_dev_get(state->dev); 2174 if (!in_dev) 2175 continue; 2176 read_lock(&in_dev->mc_list_lock); 2177 im = in_dev->mc_list; 2178 if (im) { 2179 state->in_dev = in_dev; 2180 break; 2181 } 2182 read_unlock(&in_dev->mc_list_lock); 2183 in_dev_put(in_dev); 2184 } 2185 return im; 2186} 2187 2188static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 2189{ 2190 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 2191 im = im->next; 2192 while (!im) { 2193 if (likely(state->in_dev != NULL)) { 2194 read_unlock(&state->in_dev->mc_list_lock); 2195 in_dev_put(state->in_dev); 2196 } 2197 state->dev = state->dev->next; 2198 if (!state->dev) { 2199 state->in_dev = NULL; 2200 break; 2201 } 2202 state->in_dev = in_dev_get(state->dev); 2203 if (!state->in_dev) 2204 continue; 2205 read_lock(&state->in_dev->mc_list_lock); 2206 im = state->in_dev->mc_list; 2207 } 2208 return im; 2209} 2210 2211static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 2212{ 2213 struct ip_mc_list *im = igmp_mc_get_first(seq); 2214 if (im) 2215 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 2216 --pos; 2217 return pos ? NULL : im; 2218} 2219 2220static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 2221{ 2222 read_lock(&dev_base_lock); 2223 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 2224} 2225 2226static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2227{ 2228 struct ip_mc_list *im; 2229 if (v == SEQ_START_TOKEN) 2230 im = igmp_mc_get_first(seq); 2231 else 2232 im = igmp_mc_get_next(seq, v); 2233 ++*pos; 2234 return im; 2235} 2236 2237static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 2238{ 2239 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 2240 if (likely(state->in_dev != NULL)) { 2241 read_unlock(&state->in_dev->mc_list_lock); 2242 in_dev_put(state->in_dev); 2243 state->in_dev = NULL; 2244 } 2245 state->dev = NULL; 2246 read_unlock(&dev_base_lock); 2247} 2248 2249static int igmp_mc_seq_show(struct seq_file *seq, void *v) 2250{ 2251 if (v == SEQ_START_TOKEN) 2252 seq_puts(seq, 2253 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 2254 else { 2255 struct ip_mc_list *im = (struct ip_mc_list *)v; 2256 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 2257 char *querier; 2258#ifdef CONFIG_IP_MULTICAST 2259 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 2260 IGMP_V2_SEEN(state->in_dev) ? "V2" : 2261 "V3"; 2262#else 2263 querier = "NONE"; 2264#endif 2265 2266 if (state->in_dev->mc_list == im) { 2267 seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2268 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier); 2269 } 2270 2271 seq_printf(seq, 2272 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n", 2273 im->multiaddr, im->users, 2274 im->tm_running, im->tm_running ? 2275 jiffies_to_clock_t(im->timer.expires-jiffies) : 0, 2276 im->reporter); 2277 } 2278 return 0; 2279} 2280 2281static struct seq_operations igmp_mc_seq_ops = { 2282 .start = igmp_mc_seq_start, 2283 .next = igmp_mc_seq_next, 2284 .stop = igmp_mc_seq_stop, 2285 .show = igmp_mc_seq_show, 2286}; 2287 2288static int igmp_mc_seq_open(struct inode *inode, struct file *file) 2289{ 2290 struct seq_file *seq; 2291 int rc = -ENOMEM; 2292 struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL); 2293 2294 if (!s) 2295 goto out; 2296 rc = seq_open(file, &igmp_mc_seq_ops); 2297 if (rc) 2298 goto out_kfree; 2299 2300 seq = file->private_data; 2301 seq->private = s; 2302 memset(s, 0, sizeof(*s)); 2303out: 2304 return rc; 2305out_kfree: 2306 kfree(s); 2307 goto out; 2308} 2309 2310static struct file_operations igmp_mc_seq_fops = { 2311 .owner = THIS_MODULE, 2312 .open = igmp_mc_seq_open, 2313 .read = seq_read, 2314 .llseek = seq_lseek, 2315 .release = seq_release_private, 2316}; 2317 2318struct igmp_mcf_iter_state { 2319 struct net_device *dev; 2320 struct in_device *idev; 2321 struct ip_mc_list *im; 2322}; 2323 2324#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 2325 2326static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 2327{ 2328 struct ip_sf_list *psf = NULL; 2329 struct ip_mc_list *im = NULL; 2330 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 2331 2332 for (state->dev = dev_base, state->idev = NULL, state->im = NULL; 2333 state->dev; 2334 state->dev = state->dev->next) { 2335 struct in_device *idev; 2336 idev = in_dev_get(state->dev); 2337 if (unlikely(idev == NULL)) 2338 continue; 2339 read_lock(&idev->mc_list_lock); 2340 im = idev->mc_list; 2341 if (likely(im != NULL)) { 2342 spin_lock_bh(&im->lock); 2343 psf = im->sources; 2344 if (likely(psf != NULL)) { 2345 state->im = im; 2346 state->idev = idev; 2347 break; 2348 } 2349 spin_unlock_bh(&im->lock); 2350 } 2351 read_unlock(&idev->mc_list_lock); 2352 in_dev_put(idev); 2353 } 2354 return psf; 2355} 2356 2357static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 2358{ 2359 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 2360 2361 psf = psf->sf_next; 2362 while (!psf) { 2363 spin_unlock_bh(&state->im->lock); 2364 state->im = state->im->next; 2365 while (!state->im) { 2366 if (likely(state->idev != NULL)) { 2367 read_unlock(&state->idev->mc_list_lock); 2368 in_dev_put(state->idev); 2369 } 2370 state->dev = state->dev->next; 2371 if (!state->dev) { 2372 state->idev = NULL; 2373 goto out; 2374 } 2375 state->idev = in_dev_get(state->dev); 2376 if (!state->idev) 2377 continue; 2378 read_lock(&state->idev->mc_list_lock); 2379 state->im = state->idev->mc_list; 2380 } 2381 if (!state->im) 2382 break; 2383 spin_lock_bh(&state->im->lock); 2384 psf = state->im->sources; 2385 } 2386out: 2387 return psf; 2388} 2389 2390static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 2391{ 2392 struct ip_sf_list *psf = igmp_mcf_get_first(seq); 2393 if (psf) 2394 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 2395 --pos; 2396 return pos ? NULL : psf; 2397} 2398 2399static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 2400{ 2401 read_lock(&dev_base_lock); 2402 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 2403} 2404 2405static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2406{ 2407 struct ip_sf_list *psf; 2408 if (v == SEQ_START_TOKEN) 2409 psf = igmp_mcf_get_first(seq); 2410 else 2411 psf = igmp_mcf_get_next(seq, v); 2412 ++*pos; 2413 return psf; 2414} 2415 2416static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 2417{ 2418 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 2419 if (likely(state->im != NULL)) { 2420 spin_unlock_bh(&state->im->lock); 2421 state->im = NULL; 2422 } 2423 if (likely(state->idev != NULL)) { 2424 read_unlock(&state->idev->mc_list_lock); 2425 in_dev_put(state->idev); 2426 state->idev = NULL; 2427 } 2428 state->dev = NULL; 2429 read_unlock(&dev_base_lock); 2430} 2431 2432static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 2433{ 2434 struct ip_sf_list *psf = (struct ip_sf_list *)v; 2435 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 2436 2437 if (v == SEQ_START_TOKEN) { 2438 seq_printf(seq, 2439 "%3s %6s " 2440 "%10s %10s %6s %6s\n", "Idx", 2441 "Device", "MCA", 2442 "SRC", "INC", "EXC"); 2443 } else { 2444 seq_printf(seq, 2445 "%3d %6.6s 0x%08x " 2446 "0x%08x %6lu %6lu\n", 2447 state->dev->ifindex, state->dev->name, 2448 ntohl(state->im->multiaddr), 2449 ntohl(psf->sf_inaddr), 2450 psf->sf_count[MCAST_INCLUDE], 2451 psf->sf_count[MCAST_EXCLUDE]); 2452 } 2453 return 0; 2454} 2455 2456static struct seq_operations igmp_mcf_seq_ops = { 2457 .start = igmp_mcf_seq_start, 2458 .next = igmp_mcf_seq_next, 2459 .stop = igmp_mcf_seq_stop, 2460 .show = igmp_mcf_seq_show, 2461}; 2462 2463static int igmp_mcf_seq_open(struct inode *inode, struct file *file) 2464{ 2465 struct seq_file *seq; 2466 int rc = -ENOMEM; 2467 struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL); 2468 2469 if (!s) 2470 goto out; 2471 rc = seq_open(file, &igmp_mcf_seq_ops); 2472 if (rc) 2473 goto out_kfree; 2474 2475 seq = file->private_data; 2476 seq->private = s; 2477 memset(s, 0, sizeof(*s)); 2478out: 2479 return rc; 2480out_kfree: 2481 kfree(s); 2482 goto out; 2483} 2484 2485static struct file_operations igmp_mcf_seq_fops = { 2486 .owner = THIS_MODULE, 2487 .open = igmp_mcf_seq_open, 2488 .read = seq_read, 2489 .llseek = seq_lseek, 2490 .release = seq_release_private, 2491}; 2492 2493int __init igmp_mc_proc_init(void) 2494{ 2495 proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops); 2496 proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops); 2497 return 0; 2498} 2499#endif 2500 2501EXPORT_SYMBOL(ip_mc_dec_group); 2502EXPORT_SYMBOL(ip_mc_inc_group); 2503EXPORT_SYMBOL(ip_mc_join_group);