1 /* 2 * Multicast support for IPv6 3 * Linux INET6 implementation 4 * 5 * Authors: 6 * Pedro Roque <pedro_m@yahoo.com> 7 * 8 * $Id: mcast.c,v 1.38 2001/08/15 07:36:31 davem Exp $ 9 * 10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 15 * 2 of the License, or (at your option) any later version. 16 */ 17 18 /* Changes: 19 * 20 * yoshfuji : fix format of router-alert option 21 * YOSHIFUJI Hideaki @USAGI: 22 * Fixed source address for MLD message based on 23 * <draft-ietf-magma-mld-source-02.txt>. 24 * YOSHIFUJI Hideaki @USAGI: 25 * - Ignore Queries for invalid addresses. 26 * - MLD for link-local addresses. 27 * David L Stevens <dlstevens@us.ibm.com>: 28 - MLDv2 support 29 */ 30 31 #include <linux/config.h> 32 #include <linux/module.h> 33 #include <linux/errno.h> 34 #include <linux/types.h> 35 #include <linux/string.h> 36 #include <linux/socket.h> 37 #include <linux/sockios.h> 38 #include <linux/sched.h> 39 #include <linux/net.h> 40 #include <linux/in.h> 41 #include <linux/in6.h> 42 #include <linux/netdevice.h> 43 #include <linux/if_arp.h> 44 #include <linux/route.h> 45 #include <linux/init.h> 46 #include <linux/proc_fs.h> 47 48 #include <linux/netfilter.h> 49 #include <linux/netfilter_ipv6.h> 50 51 #include <net/sock.h> 52 #include <net/snmp.h> 53 54 #include <net/ipv6.h> 55 #include <net/protocol.h> 56 #include <net/if_inet6.h> 57 #include <net/ndisc.h> 58 #include <net/addrconf.h> 59 #include <net/ip6_route.h> 60 61 #include <net/checksum.h> 62 63 /* Set to 3 to get tracing... */ 64 #define MCAST_DEBUG 2 65 66 #if MCAST_DEBUG >= 3 67 #define MDBG(x) printk x 68 #else 69 #define MDBG(x) 70 #endif 71 72 /* 73 * These header formats should be in a separate include file, but icmpv6.h 74 * doesn't have in6_addr defined in all cases, there is no __u128, and no 75 * other files reference these. 76 * 77 * +-DLS 4/14/03 78 */ 79 80 /* Multicast Listener Discovery version 2 headers */ 81 82 struct mld2_grec { 83 __u8 grec_type; 84 __u8 grec_auxwords; 85 __u16 grec_nsrcs; 86 struct in6_addr grec_mca; 87 struct in6_addr grec_src[0]; 88 }; 89 90 struct mld2_report { 91 __u8 type; 92 __u8 resv1; 93 __u16 csum; 94 __u16 resv2; 95 __u16 ngrec; 96 struct mld2_grec grec[0]; 97 }; 98 99 struct mld2_query { 100 __u8 type; 101 __u8 code; 102 __u16 csum; 103 __u16 mrc; 104 __u16 resv1; 105 struct in6_addr mca; 106 #if defined(__LITTLE_ENDIAN_BITFIELD) 107 __u8 qrv:3, 108 suppress:1, 109 resv2:4; 110 #elif defined(__BIG_ENDIAN_BITFIELD) 111 __u8 resv2:4, 112 suppress:1, 113 qrv:3; 114 #else 115 #error "Please fix <asm/byteorder.h>" 116 #endif 117 __u8 qqic; 118 __u16 nsrcs; 119 struct in6_addr srcs[0]; 120 }; 121 122 struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT; 123 struct in6_addr all_nodes_addr = {{{0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,0x1}}}; 124 125 /* Big mc list lock for all the sockets */ 126 static rwlock_t ipv6_sk_mc_lock = RW_LOCK_UNLOCKED; 127 128 static struct socket *igmp6_socket; 129 130 static void igmp6_join_group(struct ifmcaddr6 *ma); 131 static void igmp6_leave_group(struct ifmcaddr6 *ma); 132 static void igmp6_timer_handler(unsigned long data); 133 134 static void mld_gq_timer_expire(unsigned long data); 135 static void mld_ifc_timer_expire(unsigned long data); 136 static void mld_ifc_event(struct inet6_dev *idev); 137 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc); 138 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr); 139 static void mld_clear_delrec(struct inet6_dev *idev); 140 static int sf_setstate(struct ifmcaddr6 *pmc); 141 static void sf_markstate(struct ifmcaddr6 *pmc); 142 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc); 143 int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca, int sfmode, 144 int sfcount, struct in6_addr *psfsrc, int delta); 145 int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca, int sfmode, 146 int sfcount, struct in6_addr *psfsrc, int delta); 147 int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml, 148 struct inet6_dev *idev); 149 150 151 #define IGMP6_UNSOLICITED_IVAL (10*HZ) 152 #define MLD_QRV_DEFAULT 2 153 154 #define MLD_V1_SEEN(idev) ((idev)->mc_v1_seen && \ 155 time_before(jiffies, (idev)->mc_v1_seen)) 156 157 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value)) 158 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \ 159 ((value) < (thresh) ? (value) : \ 160 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant+nbexp))) << \ 161 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp)))) 162 163 #define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value) 164 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value) 165 166 #define IPV6_MLD_MAX_MSF 10 167 168 int sysctl_mld_max_msf = IPV6_MLD_MAX_MSF; 169 170 /* 171 * socket join on multicast group 172 */ 173 174 int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr) 175 { 176 struct net_device *dev = NULL; 177 struct ipv6_mc_socklist *mc_lst; 178 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6; 179 int err; 180 181 if (!(ipv6_addr_type(addr) & IPV6_ADDR_MULTICAST)) 182 return -EINVAL; 183 184 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL); 185 186 if (mc_lst == NULL) 187 return -ENOMEM; 188 189 mc_lst->next = NULL; 190 memcpy(&mc_lst->addr, addr, sizeof(struct in6_addr)); 191 192 if (ifindex == 0) { 193 struct rt6_info *rt; 194 rt = rt6_lookup(addr, NULL, 0, 0); 195 if (rt) { 196 dev = rt->rt6i_dev; 197 dev_hold(dev); 198 dst_release(&rt->u.dst); 199 } 200 } else 201 dev = dev_get_by_index(ifindex); 202 203 if (dev == NULL) { 204 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst)); 205 return -ENODEV; 206 } 207 208 mc_lst->ifindex = dev->ifindex; 209 mc_lst->sfmode = MCAST_EXCLUDE; 210 mc_lst->sflist = 0; 211 212 /* 213 * now add/increase the group membership on the device 214 */ 215 216 err = ipv6_dev_mc_inc(dev, addr); 217 218 if (err) { 219 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst)); 220 dev_put(dev); 221 return err; 222 } 223 224 write_lock_bh(&ipv6_sk_mc_lock); 225 mc_lst->next = np->ipv6_mc_list; 226 np->ipv6_mc_list = mc_lst; 227 write_unlock_bh(&ipv6_sk_mc_lock); 228 229 dev_put(dev); 230 231 return 0; 232 } 233 234 /* 235 * socket leave on multicast group 236 */ 237 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr) 238 { 239 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6; 240 struct ipv6_mc_socklist *mc_lst, **lnk; 241 242 write_lock_bh(&ipv6_sk_mc_lock); 243 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) { 244 if (mc_lst->ifindex == ifindex && 245 ipv6_addr_cmp(&mc_lst->addr, addr) == 0) { 246 struct net_device *dev; 247 248 *lnk = mc_lst->next; 249 write_unlock_bh(&ipv6_sk_mc_lock); 250 251 if ((dev = dev_get_by_index(ifindex)) != NULL) { 252 struct inet6_dev *idev = in6_dev_get(dev); 253 254 if (idev) { 255 (void) ip6_mc_leave_src(sk,mc_lst,idev); 256 in6_dev_put(idev); 257 } 258 ipv6_dev_mc_dec(dev, &mc_lst->addr); 259 dev_put(dev); 260 } 261 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst)); 262 return 0; 263 } 264 } 265 write_unlock_bh(&ipv6_sk_mc_lock); 266 267 return -ENOENT; 268 } 269 270 struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex) 271 { 272 struct net_device *dev = 0; 273 struct inet6_dev *idev = 0; 274 275 if (ifindex == 0) { 276 struct rt6_info *rt; 277 278 rt = rt6_lookup(group, NULL, 0, 0); 279 if (rt) { 280 dev = rt->rt6i_dev; 281 dev_hold(dev); 282 dst_release(&rt->u.dst); 283 } 284 } else 285 dev = dev_get_by_index(ifindex); 286 287 if (!dev) 288 return 0; 289 idev = in6_dev_get(dev); 290 if (!idev) { 291 dev_put(dev); 292 return 0; 293 } 294 read_lock_bh(&idev->lock); 295 if (idev->dead) { 296 read_unlock_bh(&idev->lock); 297 in6_dev_put(idev); 298 dev_put(dev); 299 return 0; 300 } 301 return idev; 302 } 303 304 void ipv6_sock_mc_close(struct sock *sk) 305 { 306 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6; 307 struct ipv6_mc_socklist *mc_lst; 308 309 write_lock_bh(&ipv6_sk_mc_lock); 310 while ((mc_lst = np->ipv6_mc_list) != NULL) { 311 struct net_device *dev; 312 313 np->ipv6_mc_list = mc_lst->next; 314 write_unlock_bh(&ipv6_sk_mc_lock); 315 316 dev = dev_get_by_index(mc_lst->ifindex); 317 if (dev) { 318 struct inet6_dev *idev = in6_dev_get(dev); 319 320 if (idev) { 321 (void) ip6_mc_leave_src(sk, mc_lst, idev); 322 in6_dev_put(idev); 323 } 324 ipv6_dev_mc_dec(dev, &mc_lst->addr); 325 dev_put(dev); 326 } 327 328 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst)); 329 330 write_lock_bh(&ipv6_sk_mc_lock); 331 } 332 write_unlock_bh(&ipv6_sk_mc_lock); 333 } 334 335 int ip6_mc_source(int add, int omode, struct sock *sk, 336 struct group_source_req *pgsr) 337 { 338 struct in6_addr *source, *group; 339 struct ipv6_mc_socklist *pmc; 340 struct net_device *dev; 341 struct inet6_dev *idev; 342 struct ipv6_pinfo *inet6 = &sk->net_pinfo.af_inet6; 343 struct ip6_sf_socklist *psl; 344 int i, j, rv; 345 int err; 346 347 if (pgsr->gsr_group.ss_family != AF_INET6 || 348 pgsr->gsr_source.ss_family != AF_INET6) 349 return -EINVAL; 350 351 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr; 352 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr; 353 354 if (!(ipv6_addr_type(group) & IPV6_ADDR_MULTICAST)) 355 return -EINVAL; 356 357 idev = ip6_mc_find_dev(group, pgsr->gsr_interface); 358 if (!idev) 359 return -ENODEV; 360 dev = idev->dev; 361 362 err = -EADDRNOTAVAIL; 363 364 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) { 365 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface) 366 continue; 367 if (ipv6_addr_cmp(&pmc->addr, group) == 0) 368 break; 369 } 370 if (!pmc) /* must have a prior join */ 371 goto done; 372 /* if a source filter was set, must be the same mode as before */ 373 if (pmc->sflist) { 374 if (pmc->sfmode != omode) 375 goto done; 376 } else if (pmc->sfmode != omode) { 377 /* allow mode switches for empty-set filters */ 378 ip6_mc_add_src(idev, group, omode, 0, 0, 0); 379 ip6_mc_del_src(idev, group, pmc->sfmode, 0, 0, 0); 380 pmc->sfmode = omode; 381 } 382 383 psl = pmc->sflist; 384 if (!add) { 385 if (!psl) 386 goto done; 387 rv = !0; 388 for (i=0; i<psl->sl_count; i++) { 389 rv = memcmp(&psl->sl_addr[i], source, 390 sizeof(struct in6_addr)); 391 if (rv == 0) 392 break; 393 } 394 if (rv) /* source not found */ 395 goto done; 396 397 /* update the interface filter */ 398 ip6_mc_del_src(idev, group, omode, 1, source, 1); 399 400 for (j=i+1; j<psl->sl_count; j++) 401 psl->sl_addr[j-1] = psl->sl_addr[j]; 402 psl->sl_count--; 403 err = 0; 404 goto done; 405 } 406 /* else, add a new source to the filter */ 407 408 if (psl && psl->sl_count >= sysctl_mld_max_msf) { 409 err = -ENOBUFS; 410 goto done; 411 } 412 if (!psl || psl->sl_count == psl->sl_max) { 413 struct ip6_sf_socklist *newpsl; 414 int count = IP6_SFBLOCK; 415 416 if (psl) 417 count += psl->sl_max; 418 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk, 419 IP6_SFLSIZE(count), GFP_ATOMIC); 420 if (!newpsl) { 421 err = -ENOBUFS; 422 goto done; 423 } 424 newpsl->sl_max = count; 425 newpsl->sl_count = count - IP6_SFBLOCK; 426 if (psl) { 427 for (i=0; i<psl->sl_count; i++) 428 newpsl->sl_addr[i] = psl->sl_addr[i]; 429 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max)); 430 } 431 pmc->sflist = psl = newpsl; 432 } 433 rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 434 for (i=0; i<psl->sl_count; i++) { 435 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr)); 436 if (rv == 0) 437 break; 438 } 439 if (rv == 0) /* address already there is an error */ 440 goto done; 441 for (j=psl->sl_count-1; j>=i; j--) 442 psl->sl_addr[j+1] = psl->sl_addr[j]; 443 psl->sl_addr[i] = *source; 444 psl->sl_count++; 445 err = 0; 446 /* update the interface list */ 447 ip6_mc_add_src(idev, group, omode, 1, source, 1); 448 done: 449 read_unlock_bh(&idev->lock); 450 in6_dev_put(idev); 451 dev_put(dev); 452 return err; 453 } 454 455 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf) 456 { 457 struct in6_addr *group; 458 struct ipv6_mc_socklist *pmc; 459 struct net_device *dev; 460 struct inet6_dev *idev; 461 struct ipv6_pinfo *inet6 = &sk->net_pinfo.af_inet6; 462 struct ip6_sf_socklist *newpsl, *psl; 463 int i, err; 464 465 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr; 466 467 if (!(ipv6_addr_type(group) & IPV6_ADDR_MULTICAST)) 468 return -EINVAL; 469 if (gsf->gf_fmode != MCAST_INCLUDE && 470 gsf->gf_fmode != MCAST_EXCLUDE) 471 return -EINVAL; 472 473 idev = ip6_mc_find_dev(group, gsf->gf_interface); 474 475 if (!idev) 476 return -ENODEV; 477 dev = idev->dev; 478 err = -EADDRNOTAVAIL; 479 480 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) { 481 if (pmc->ifindex != gsf->gf_interface) 482 continue; 483 if (ipv6_addr_cmp(&pmc->addr, group) == 0) 484 break; 485 } 486 if (!pmc) /* must have a prior join */ 487 goto done; 488 if (gsf->gf_numsrc) { 489 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk, 490 IP6_SFLSIZE(gsf->gf_numsrc), GFP_ATOMIC); 491 if (!newpsl) { 492 err = -ENOBUFS; 493 goto done; 494 } 495 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc; 496 for (i=0; i<newpsl->sl_count; ++i) { 497 struct sockaddr_in6 *psin6; 498 499 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i]; 500 newpsl->sl_addr[i] = psin6->sin6_addr; 501 } 502 err = ip6_mc_add_src(idev, group, gsf->gf_fmode, 503 newpsl->sl_count, newpsl->sl_addr, 0); 504 if (err) { 505 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max)); 506 goto done; 507 } 508 } else { 509 newpsl = NULL; 510 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0); 511 } 512 513 psl = pmc->sflist; 514 if (psl) { 515 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 516 psl->sl_count, psl->sl_addr, 0); 517 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max)); 518 } else 519 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, 0, 0); 520 pmc->sflist = newpsl; 521 pmc->sfmode = gsf->gf_fmode; 522 done: 523 read_unlock_bh(&idev->lock); 524 in6_dev_put(idev); 525 dev_put(dev); 526 return err; 527 } 528 529 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf, 530 struct group_filter *optval, int *optlen) 531 { 532 int err, i, count, copycount; 533 struct in6_addr *group; 534 struct ipv6_mc_socklist *pmc; 535 struct inet6_dev *idev; 536 struct net_device *dev; 537 struct ipv6_pinfo *inet6 = &sk->net_pinfo.af_inet6; 538 struct ip6_sf_socklist *psl; 539 540 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr; 541 542 if (!(ipv6_addr_type(group) & IPV6_ADDR_MULTICAST)) 543 return -EINVAL; 544 545 idev = ip6_mc_find_dev(group, gsf->gf_interface); 546 547 if (!idev) 548 return -ENODEV; 549 550 dev = idev->dev; 551 552 err = -EADDRNOTAVAIL; 553 554 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) { 555 if (pmc->ifindex != gsf->gf_interface) 556 continue; 557 if (ipv6_addr_cmp(group, &pmc->addr) == 0) 558 break; 559 } 560 if (!pmc) /* must have a prior join */ 561 goto done; 562 gsf->gf_fmode = pmc->sfmode; 563 psl = pmc->sflist; 564 count = psl ? psl->sl_count : 0; 565 read_unlock_bh(&idev->lock); 566 in6_dev_put(idev); 567 dev_put(dev); 568 569 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 570 gsf->gf_numsrc = count; 571 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 572 copy_to_user((void *)optval, gsf, GROUP_FILTER_SIZE(0))) { 573 return -EFAULT; 574 } 575 for (i=0; i<copycount; i++) { 576 struct sockaddr_in6 *psin6; 577 struct sockaddr_storage ss; 578 579 psin6 = (struct sockaddr_in6 *)&ss; 580 memset(&ss, 0, sizeof(ss)); 581 psin6->sin6_family = AF_INET6; 582 psin6->sin6_addr = psl->sl_addr[i]; 583 if (copy_to_user((void *)&optval->gf_slist[i], &ss, sizeof(ss))) 584 return -EFAULT; 585 } 586 return 0; 587 done: 588 read_unlock_bh(&idev->lock); 589 in6_dev_put(idev); 590 dev_put(dev); 591 return err; 592 } 593 594 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr, 595 struct in6_addr *src_addr) 596 { 597 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6; 598 struct ipv6_mc_socklist *mc; 599 struct ip6_sf_socklist *psl; 600 int rv = 1; 601 602 read_lock(&ipv6_sk_mc_lock); 603 for (mc = np->ipv6_mc_list; mc; mc = mc->next) { 604 if (ipv6_addr_cmp(&mc->addr, mc_addr) == 0) 605 break; 606 } 607 if (!mc) { 608 read_unlock(&ipv6_sk_mc_lock); 609 return 1; 610 } 611 psl = mc->sflist; 612 if (!psl) { 613 rv = mc->sfmode == MCAST_EXCLUDE; 614 } else { 615 int i; 616 617 for (i=0; i<psl->sl_count; i++) { 618 if (ipv6_addr_cmp(&psl->sl_addr[i], src_addr) == 0) 619 break; 620 } 621 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 622 rv = 0; 623 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 624 rv = 0; 625 } 626 read_unlock(&ipv6_sk_mc_lock); 627 628 return rv; 629 } 630 631 static void ma_put(struct ifmcaddr6 *mc) 632 { 633 if (atomic_dec_and_test(&mc->mca_refcnt)) { 634 in6_dev_put(mc->idev); 635 kfree(mc); 636 } 637 } 638 639 static void igmp6_group_added(struct ifmcaddr6 *mc) 640 { 641 struct net_device *dev = mc->idev->dev; 642 char buf[MAX_ADDR_LEN]; 643 644 spin_lock_bh(&mc->mca_lock); 645 if (!(mc->mca_flags&MAF_LOADED)) { 646 mc->mca_flags |= MAF_LOADED; 647 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0) 648 dev_mc_add(dev, buf, dev->addr_len, 0); 649 } 650 spin_unlock_bh(&mc->mca_lock); 651 652 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT)) 653 return; 654 655 if (MLD_V1_SEEN(mc->idev)) { 656 igmp6_join_group(mc); 657 return; 658 } 659 /* else v2 */ 660 661 mc->mca_crcount = mc->idev->mc_qrv; 662 mld_ifc_event(mc->idev); 663 } 664 665 static void igmp6_group_dropped(struct ifmcaddr6 *mc) 666 { 667 struct net_device *dev = mc->idev->dev; 668 char buf[MAX_ADDR_LEN]; 669 670 spin_lock_bh(&mc->mca_lock); 671 if (mc->mca_flags&MAF_LOADED) { 672 mc->mca_flags &= ~MAF_LOADED; 673 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0) 674 dev_mc_delete(dev, buf, dev->addr_len, 0); 675 } 676 677 if (mc->mca_flags & MAF_NOREPORT) 678 goto done; 679 spin_unlock_bh(&mc->mca_lock); 680 681 if (!mc->idev->dead) 682 igmp6_leave_group(mc); 683 684 spin_lock_bh(&mc->mca_lock); 685 if (del_timer(&mc->mca_timer)) 686 atomic_dec(&mc->mca_refcnt); 687 done: 688 ip6_mc_clear_src(mc); 689 spin_unlock_bh(&mc->mca_lock); 690 } 691 692 /* 693 * deleted ifmcaddr6 manipulation 694 */ 695 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im) 696 { 697 struct ifmcaddr6 *pmc; 698 699 /* this is an "ifmcaddr6" for convenience; only the fields below 700 * are actually used. In particular, the refcnt and users are not 701 * used for management of the delete list. Using the same structure 702 * for deleted items allows change reports to use common code with 703 * non-deleted or query-response MCA's. 704 */ 705 pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC); 706 if (!pmc) 707 return; 708 memset(pmc, 0, sizeof(*pmc)); 709 spin_lock_bh(&im->mca_lock); 710 pmc->mca_lock = SPIN_LOCK_UNLOCKED; 711 pmc->idev = im->idev; 712 in6_dev_hold(idev); 713 pmc->mca_addr = im->mca_addr; 714 pmc->mca_crcount = idev->mc_qrv; 715 pmc->mca_sfmode = im->mca_sfmode; 716 if (pmc->mca_sfmode == MCAST_INCLUDE) { 717 struct ip6_sf_list *psf; 718 719 pmc->mca_tomb = im->mca_tomb; 720 pmc->mca_sources = im->mca_sources; 721 im->mca_tomb = im->mca_sources = 0; 722 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) 723 psf->sf_crcount = pmc->mca_crcount; 724 } 725 spin_unlock_bh(&im->mca_lock); 726 727 write_lock_bh(&idev->mc_lock); 728 pmc->next = idev->mc_tomb; 729 idev->mc_tomb = pmc; 730 write_unlock_bh(&idev->mc_lock); 731 } 732 733 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca) 734 { 735 struct ifmcaddr6 *pmc, *pmc_prev; 736 struct ip6_sf_list *psf, *psf_next; 737 738 write_lock_bh(&idev->mc_lock); 739 pmc_prev = 0; 740 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) { 741 if (ipv6_addr_cmp(&pmc->mca_addr, pmca) == 0) 742 break; 743 pmc_prev = pmc; 744 } 745 if (pmc) { 746 if (pmc_prev) 747 pmc_prev->next = pmc->next; 748 else 749 idev->mc_tomb = pmc->next; 750 } 751 write_unlock_bh(&idev->mc_lock); 752 if (pmc) { 753 for (psf=pmc->mca_tomb; psf; psf=psf_next) { 754 psf_next = psf->sf_next; 755 kfree(psf); 756 } 757 in6_dev_put(pmc->idev); 758 kfree(pmc); 759 } 760 } 761 762 static void mld_clear_delrec(struct inet6_dev *idev) 763 { 764 struct ifmcaddr6 *pmc, *nextpmc; 765 766 write_lock_bh(&idev->mc_lock); 767 pmc = idev->mc_tomb; 768 idev->mc_tomb = 0; 769 write_unlock_bh(&idev->mc_lock); 770 771 for (; pmc; pmc = nextpmc) { 772 nextpmc = pmc->next; 773 ip6_mc_clear_src(pmc); 774 in6_dev_put(pmc->idev); 775 kfree(pmc); 776 } 777 778 /* clear dead sources, too */ 779 read_lock_bh(&idev->lock); 780 for (pmc=idev->mc_list; pmc; pmc=pmc->next) { 781 struct ip6_sf_list *psf, *psf_next; 782 783 spin_lock_bh(&pmc->mca_lock); 784 psf = pmc->mca_tomb; 785 pmc->mca_tomb = 0; 786 spin_unlock_bh(&pmc->mca_lock); 787 for (; psf; psf=psf_next) { 788 psf_next = psf->sf_next; 789 kfree(psf); 790 } 791 } 792 read_unlock_bh(&idev->lock); 793 } 794 795 796 /* 797 * device multicast group inc (add if not found) 798 */ 799 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr) 800 { 801 struct ifmcaddr6 *mc; 802 struct inet6_dev *idev; 803 804 idev = in6_dev_get(dev); 805 806 if (idev == NULL) 807 return -EINVAL; 808 809 write_lock_bh(&idev->lock); 810 if (idev->dead) { 811 write_unlock_bh(&idev->lock); 812 in6_dev_put(idev); 813 return -ENODEV; 814 } 815 816 for (mc = idev->mc_list; mc; mc = mc->next) { 817 if (ipv6_addr_cmp(&mc->mca_addr, addr) == 0) { 818 mc->mca_users++; 819 write_unlock_bh(&idev->lock); 820 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0, 821 0, 0); 822 in6_dev_put(idev); 823 return 0; 824 } 825 } 826 827 /* 828 * not found: create a new one. 829 */ 830 831 mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC); 832 833 if (mc == NULL) { 834 write_unlock_bh(&idev->lock); 835 in6_dev_put(idev); 836 return -ENOMEM; 837 } 838 839 memset(mc, 0, sizeof(struct ifmcaddr6)); 840 mc->mca_timer.function = igmp6_timer_handler; 841 mc->mca_timer.data = (unsigned long) mc; 842 843 memcpy(&mc->mca_addr, addr, sizeof(struct in6_addr)); 844 mc->idev = idev; 845 mc->mca_users = 1; 846 atomic_set(&mc->mca_refcnt, 2); 847 mc->mca_lock = SPIN_LOCK_UNLOCKED; 848 849 /* initial mode is (EX, empty) */ 850 mc->mca_sfmode = MCAST_EXCLUDE; 851 mc->mca_sfcount[MCAST_EXCLUDE] = 1; 852 853 if (ipv6_addr_cmp(&mc->mca_addr, &all_nodes_addr) == 0 || 854 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) 855 mc->mca_flags |= MAF_NOREPORT; 856 857 mc->next = idev->mc_list; 858 idev->mc_list = mc; 859 write_unlock_bh(&idev->lock); 860 861 mld_del_delrec(idev, &mc->mca_addr); 862 igmp6_group_added(mc); 863 ma_put(mc); 864 return 0; 865 } 866 867 /* 868 * device multicast group del 869 */ 870 static int __ipv6_dev_mc_dec(struct net_device *dev, struct inet6_dev *idev, struct in6_addr *addr) 871 { 872 struct ifmcaddr6 *ma, **map; 873 874 write_lock_bh(&idev->lock); 875 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) { 876 if (ipv6_addr_cmp(&ma->mca_addr, addr) == 0) { 877 if (--ma->mca_users == 0) { 878 *map = ma->next; 879 write_unlock_bh(&idev->lock); 880 881 igmp6_group_dropped(ma); 882 883 ma_put(ma); 884 return 0; 885 } 886 write_unlock_bh(&idev->lock); 887 return 0; 888 } 889 } 890 write_unlock_bh(&idev->lock); 891 892 return -ENOENT; 893 } 894 895 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr) 896 { 897 struct inet6_dev *idev = in6_dev_get(dev); 898 int err; 899 900 if (!idev) 901 return -ENODEV; 902 903 err = __ipv6_dev_mc_dec(dev, idev, addr); 904 905 in6_dev_put(idev); 906 907 return err; 908 } 909 910 /* 911 * check if the interface/address pair is valid 912 */ 913 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group, 914 struct in6_addr *src_addr) 915 { 916 struct inet6_dev *idev; 917 struct ifmcaddr6 *mc; 918 int rv = 0; 919 920 idev = in6_dev_get(dev); 921 if (idev) { 922 read_lock_bh(&idev->lock); 923 for (mc = idev->mc_list; mc; mc=mc->next) { 924 if (ipv6_addr_cmp(&mc->mca_addr, group) == 0) 925 break; 926 } 927 if (mc) { 928 if (!ipv6_addr_any(src_addr)) { 929 struct ip6_sf_list *psf; 930 931 spin_lock_bh(&mc->mca_lock); 932 for (psf=mc->mca_sources;psf;psf=psf->sf_next) { 933 if (ipv6_addr_cmp(&psf->sf_addr, 934 src_addr) == 0) 935 break; 936 } 937 if (psf) 938 rv = psf->sf_count[MCAST_INCLUDE] || 939 psf->sf_count[MCAST_EXCLUDE] != 940 mc->mca_sfcount[MCAST_EXCLUDE]; 941 else 942 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0; 943 spin_unlock_bh(&mc->mca_lock); 944 } else 945 rv = 1; /* don't filter unspecified source */ 946 } 947 read_unlock_bh(&idev->lock); 948 in6_dev_put(idev); 949 } 950 return rv; 951 } 952 953 static void mld_gq_start_timer(struct inet6_dev *idev) 954 { 955 int tv = net_random() % idev->mc_maxdelay; 956 957 idev->mc_gq_running = 1; 958 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2)) 959 in6_dev_hold(idev); 960 } 961 962 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay) 963 { 964 int tv = net_random() % delay; 965 966 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2)) 967 in6_dev_hold(idev); 968 } 969 970 /* 971 * IGMP handling (alias multicast ICMPv6 messages) 972 */ 973 974 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime) 975 { 976 unsigned long delay = resptime; 977 978 /* Do not start timer for these addresses */ 979 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) || 980 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) 981 return; 982 983 if (del_timer(&ma->mca_timer)) { 984 atomic_dec(&ma->mca_refcnt); 985 delay = ma->mca_timer.expires - jiffies; 986 } 987 988 if (delay >= resptime) { 989 if (resptime) 990 delay = net_random() % resptime; 991 else 992 delay = 1; 993 } 994 995 ma->mca_timer.expires = jiffies + delay; 996 if (!mod_timer(&ma->mca_timer, jiffies + delay)) 997 atomic_inc(&ma->mca_refcnt); 998 ma->mca_flags |= MAF_TIMER_RUNNING; 999 spin_unlock(&ma->mca_lock); 1000 } 1001 1002 static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs, 1003 struct in6_addr *srcs) 1004 { 1005 struct ip6_sf_list *psf; 1006 int i, scount; 1007 1008 scount = 0; 1009 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { 1010 if (scount == nsrcs) 1011 break; 1012 for (i=0; i<nsrcs; i++) 1013 if (ipv6_addr_cmp(&srcs[i], &psf->sf_addr) == 0) { 1014 psf->sf_gsresp = 1; 1015 scount++; 1016 break; 1017 } 1018 } 1019 } 1020 1021 int igmp6_event_query(struct sk_buff *skb) 1022 { 1023 struct mld2_query *mlh2 = (struct mld2_query *) skb->h.raw; 1024 struct ifmcaddr6 *ma; 1025 struct in6_addr *group; 1026 unsigned long max_delay; 1027 struct inet6_dev *idev; 1028 struct icmp6hdr *hdr; 1029 int group_type; 1030 int mark = 0; 1031 int len; 1032 1033 if (!pskb_may_pull(skb, sizeof(struct in6_addr))) 1034 return -EINVAL; 1035 1036 len = ntohs(skb->nh.ipv6h->payload_len); 1037 1038 /* Drop queries with not link local source */ 1039 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL)) 1040 return -EINVAL; 1041 1042 idev = in6_dev_get(skb->dev); 1043 1044 if (idev == NULL) 1045 return 0; 1046 1047 hdr = (struct icmp6hdr *) skb->h.raw; 1048 group = (struct in6_addr *) (hdr + 1); 1049 group_type = ipv6_addr_type(group); 1050 1051 if (group_type != IPV6_ADDR_ANY && 1052 !(group_type&IPV6_ADDR_MULTICAST)) { 1053 in6_dev_put(idev); 1054 return -EINVAL; 1055 } 1056 1057 if (len == 24) { 1058 int switchback; 1059 /* MLDv1 router present */ 1060 1061 /* Translate milliseconds to jiffies */ 1062 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000; 1063 1064 switchback = (idev->mc_qrv + 1) * max_delay; 1065 idev->mc_v1_seen = jiffies + switchback; 1066 1067 /* cancel the interface change timer */ 1068 idev->mc_ifc_count = 0; 1069 if (del_timer(&idev->mc_ifc_timer)) 1070 __in6_dev_put(idev); 1071 /* clear deleted report items */ 1072 mld_clear_delrec(idev); 1073 } else if (len >= 28) { 1074 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000; 1075 if (!max_delay) 1076 max_delay = 1; 1077 idev->mc_maxdelay = max_delay; 1078 if (mlh2->qrv) 1079 idev->mc_qrv = mlh2->qrv; 1080 if (group_type == IPV6_ADDR_ANY) { /* general query */ 1081 if (mlh2->nsrcs) { 1082 in6_dev_put(idev); 1083 return -EINVAL; /* no sources allowed */ 1084 } 1085 mld_gq_start_timer(idev); 1086 in6_dev_put(idev); 1087 return 0; 1088 } 1089 /* mark sources to include, if group & source-specific */ 1090 mark = mlh2->nsrcs != 0; 1091 } else { 1092 in6_dev_put(idev); 1093 return -EINVAL; 1094 } 1095 1096 read_lock_bh(&idev->lock); 1097 if (group_type == IPV6_ADDR_ANY) { 1098 for (ma = idev->mc_list; ma; ma=ma->next) { 1099 spin_lock_bh(&ma->mca_lock); 1100 igmp6_group_queried(ma, max_delay); 1101 spin_unlock_bh(&ma->mca_lock); 1102 } 1103 } else { 1104 for (ma = idev->mc_list; ma; ma=ma->next) { 1105 if (group_type != IPV6_ADDR_ANY && 1106 ipv6_addr_cmp(group, &ma->mca_addr) != 0) 1107 continue; 1108 spin_lock_bh(&ma->mca_lock); 1109 if (ma->mca_flags & MAF_TIMER_RUNNING) { 1110 /* gsquery <- gsquery && mark */ 1111 if (!mark) 1112 ma->mca_flags &= ~MAF_GSQUERY; 1113 } else { 1114 /* gsquery <- mark */ 1115 if (mark) 1116 ma->mca_flags |= MAF_GSQUERY; 1117 else 1118 ma->mca_flags &= ~MAF_GSQUERY; 1119 } 1120 if (ma->mca_flags & MAF_GSQUERY) 1121 mld_marksources(ma, ntohs(mlh2->nsrcs), 1122 mlh2->srcs); 1123 igmp6_group_queried(ma, max_delay); 1124 spin_unlock_bh(&ma->mca_lock); 1125 if (group_type != IPV6_ADDR_ANY) 1126 break; 1127 } 1128 } 1129 read_unlock_bh(&idev->lock); 1130 in6_dev_put(idev); 1131 1132 return 0; 1133 } 1134 1135 1136 int igmp6_event_report(struct sk_buff *skb) 1137 { 1138 struct ifmcaddr6 *ma; 1139 struct in6_addr *addrp; 1140 struct inet6_dev *idev; 1141 struct icmp6hdr *hdr; 1142 int addr_type; 1143 1144 /* Our own report looped back. Ignore it. */ 1145 if (skb->pkt_type == PACKET_LOOPBACK) 1146 return 0; 1147 1148 /* send our report if the MC router may not have heard this report */ 1149 if (skb->pkt_type != PACKET_MULTICAST && 1150 skb->pkt_type != PACKET_BROADCAST) 1151 return 0; 1152 1153 if (!pskb_may_pull(skb, sizeof(struct in6_addr))) 1154 return -EINVAL; 1155 1156 hdr = (struct icmp6hdr*) skb->h.raw; 1157 1158 /* Drop reports with not link local source */ 1159 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr); 1160 if (addr_type != IPV6_ADDR_ANY && 1161 !(addr_type&IPV6_ADDR_LINKLOCAL)) 1162 return -EINVAL; 1163 1164 addrp = (struct in6_addr *) (hdr + 1); 1165 1166 idev = in6_dev_get(skb->dev); 1167 if (idev == NULL) 1168 return -ENODEV; 1169 1170 /* 1171 * Cancel the timer for this group 1172 */ 1173 1174 read_lock_bh(&idev->lock); 1175 for (ma = idev->mc_list; ma; ma=ma->next) { 1176 if (ipv6_addr_cmp(&ma->mca_addr, addrp) == 0) { 1177 spin_lock(&ma->mca_lock); 1178 if (del_timer(&ma->mca_timer)) 1179 atomic_dec(&ma->mca_refcnt); 1180 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING); 1181 spin_unlock(&ma->mca_lock); 1182 break; 1183 } 1184 } 1185 read_unlock_bh(&idev->lock); 1186 in6_dev_put(idev); 1187 return 0; 1188 } 1189 1190 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type, 1191 int gdeleted, int sdeleted) 1192 { 1193 switch (type) { 1194 case MLD2_MODE_IS_INCLUDE: 1195 case MLD2_MODE_IS_EXCLUDE: 1196 if (gdeleted || sdeleted) 1197 return 0; 1198 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp); 1199 case MLD2_CHANGE_TO_INCLUDE: 1200 if (gdeleted || sdeleted) 1201 return 0; 1202 return psf->sf_count[MCAST_INCLUDE] != 0; 1203 case MLD2_CHANGE_TO_EXCLUDE: 1204 if (gdeleted || sdeleted) 1205 return 0; 1206 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 || 1207 psf->sf_count[MCAST_INCLUDE]) 1208 return 0; 1209 return pmc->mca_sfcount[MCAST_EXCLUDE] == 1210 psf->sf_count[MCAST_EXCLUDE]; 1211 case MLD2_ALLOW_NEW_SOURCES: 1212 if (gdeleted || !psf->sf_crcount) 1213 return 0; 1214 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted; 1215 case MLD2_BLOCK_OLD_SOURCES: 1216 if (pmc->mca_sfmode == MCAST_INCLUDE) 1217 return gdeleted || (psf->sf_crcount && sdeleted); 1218 return psf->sf_crcount && !gdeleted && !sdeleted; 1219 } 1220 return 0; 1221 } 1222 1223 static int 1224 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted) 1225 { 1226 struct ip6_sf_list *psf; 1227 int scount = 0; 1228 1229 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { 1230 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 1231 continue; 1232 scount++; 1233 } 1234 return scount; 1235 } 1236 1237 static struct sk_buff *mld_newpack(struct net_device *dev, int size) 1238 { 1239 struct sock *sk = igmp6_socket->sk; 1240 struct sk_buff *skb; 1241 struct mld2_report *pmr; 1242 struct in6_addr addr_buf; 1243 int err; 1244 u8 ra[8] = { IPPROTO_ICMPV6, 0, 1245 IPV6_TLV_ROUTERALERT, 2, 0, 0, 1246 IPV6_TLV_PADN, 0 }; 1247 1248 skb = sock_alloc_send_skb(sk, size + dev->hard_header_len+15, 1, &err); 1249 1250 if (skb == 0) 1251 return 0; 1252 1253 skb_reserve(skb, (dev->hard_header_len + 15) & ~15); 1254 1255 if (ipv6_get_lladdr(dev, &addr_buf)) { 1256 /* <draft-ietf-magma-mld-source-02.txt>: 1257 * use unspecified address as the source address 1258 * when a valid link-local address is not available. 1259 */ 1260 memset(&addr_buf, 0, sizeof(addr_buf)); 1261 } 1262 1263 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0); 1264 1265 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra)); 1266 1267 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr)); 1268 skb->h.raw = (unsigned char *)pmr; 1269 pmr->type = ICMPV6_MLD2_REPORT; 1270 pmr->resv1 = 0; 1271 pmr->csum = 0; 1272 pmr->resv2 = 0; 1273 pmr->ngrec = 0; 1274 return skb; 1275 } 1276 1277 static inline int mld_dev_queue_xmit2(struct sk_buff *skb) 1278 { 1279 struct net_device *dev = skb->dev; 1280 1281 if (dev->hard_header) { 1282 unsigned char ha[MAX_ADDR_LEN]; 1283 int err; 1284 1285 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1); 1286 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len); 1287 if (err < 0) { 1288 kfree_skb(skb); 1289 return err; 1290 } 1291 } 1292 return dev_queue_xmit(skb); 1293 } 1294 1295 static inline int mld_dev_queue_xmit(struct sk_buff *skb) 1296 { 1297 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev, 1298 mld_dev_queue_xmit2); 1299 } 1300 1301 static void mld_sendpack(struct sk_buff *skb) 1302 { 1303 struct ipv6hdr *pip6 = skb->nh.ipv6h; 1304 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw; 1305 int payload_len, mldlen, err; 1306 1307 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h - 1308 sizeof(struct ipv6hdr); 1309 mldlen = skb->tail - skb->h.raw; 1310 pip6->payload_len = htons(payload_len); 1311 1312 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen, 1313 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0)); 1314 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev, 1315 mld_dev_queue_xmit); 1316 if (!err) 1317 ICMP6_INC_STATS(Icmp6OutMsgs); 1318 } 1319 1320 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel) 1321 { 1322 return sizeof(struct mld2_grec) + 4*mld_scount(pmc,type,gdel,sdel); 1323 } 1324 1325 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc, 1326 int type, struct mld2_grec **ppgr) 1327 { 1328 struct net_device *dev = pmc->idev->dev; 1329 struct mld2_report *pmr; 1330 struct mld2_grec *pgr; 1331 1332 if (!skb) 1333 skb = mld_newpack(dev, dev->mtu); 1334 if (!skb) 1335 return 0; 1336 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec)); 1337 pgr->grec_type = type; 1338 pgr->grec_auxwords = 0; 1339 pgr->grec_nsrcs = 0; 1340 pgr->grec_mca = pmc->mca_addr; /* structure copy */ 1341 pmr = (struct mld2_report *)skb->h.raw; 1342 pmr->ngrec = htons(ntohs(pmr->ngrec)+1); 1343 *ppgr = pgr; 1344 return skb; 1345 } 1346 1347 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \ 1348 skb_tailroom(skb)) : 0) 1349 1350 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc, 1351 int type, int gdeleted, int sdeleted) 1352 { 1353 struct net_device *dev = pmc->idev->dev; 1354 struct mld2_report *pmr; 1355 struct mld2_grec *pgr = 0; 1356 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list; 1357 int scount, first, isquery, truncate; 1358 1359 if (pmc->mca_flags & MAF_NOREPORT) 1360 return skb; 1361 1362 isquery = type == MLD2_MODE_IS_INCLUDE || 1363 type == MLD2_MODE_IS_EXCLUDE; 1364 truncate = type == MLD2_MODE_IS_EXCLUDE || 1365 type == MLD2_CHANGE_TO_EXCLUDE; 1366 1367 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources; 1368 1369 if (!*psf_list) { 1370 if (type == MLD2_ALLOW_NEW_SOURCES || 1371 type == MLD2_BLOCK_OLD_SOURCES) 1372 return skb; 1373 if (pmc->mca_crcount || isquery) { 1374 /* make sure we have room for group header and at 1375 * least one source. 1376 */ 1377 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+ 1378 sizeof(struct in6_addr)) { 1379 mld_sendpack(skb); 1380 skb = 0; /* add_grhead will get a new one */ 1381 } 1382 skb = add_grhead(skb, pmc, type, &pgr); 1383 } 1384 return skb; 1385 } 1386 pmr = skb ? (struct mld2_report *)skb->h.raw : 0; 1387 1388 /* EX and TO_EX get a fresh packet, if needed */ 1389 if (truncate) { 1390 if (pmr && pmr->ngrec && 1391 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { 1392 if (skb) 1393 mld_sendpack(skb); 1394 skb = mld_newpack(dev, dev->mtu); 1395 } 1396 } 1397 first = 1; 1398 scount = 0; 1399 psf_prev = 0; 1400 for (psf=*psf_list; psf; psf=psf_next) { 1401 struct in6_addr *psrc; 1402 1403 psf_next = psf->sf_next; 1404 1405 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { 1406 psf_prev = psf; 1407 continue; 1408 } 1409 1410 /* clear marks on query responses */ 1411 if (isquery) 1412 psf->sf_gsresp = 0; 1413 1414 if (AVAILABLE(skb) < sizeof(*psrc) + 1415 first*sizeof(struct mld2_grec)) { 1416 if (truncate && !first) 1417 break; /* truncate these */ 1418 if (pgr) 1419 pgr->grec_nsrcs = htons(scount); 1420 if (skb) 1421 mld_sendpack(skb); 1422 skb = mld_newpack(dev, dev->mtu); 1423 first = 1; 1424 scount = 0; 1425 } 1426 if (first) { 1427 skb = add_grhead(skb, pmc, type, &pgr); 1428 first = 0; 1429 } 1430 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc)); 1431 *psrc = psf->sf_addr; 1432 scount++; 1433 if ((type == MLD2_ALLOW_NEW_SOURCES || 1434 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) { 1435 psf->sf_crcount--; 1436 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { 1437 if (psf_prev) 1438 psf_prev->sf_next = psf->sf_next; 1439 else 1440 *psf_list = psf->sf_next; 1441 kfree(psf); 1442 continue; 1443 } 1444 } 1445 psf_prev = psf; 1446 } 1447 if (pgr) 1448 pgr->grec_nsrcs = htons(scount); 1449 1450 if (isquery) 1451 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */ 1452 return skb; 1453 } 1454 1455 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc) 1456 { 1457 struct sk_buff *skb = 0; 1458 int type; 1459 1460 if (!pmc) { 1461 read_lock_bh(&idev->lock); 1462 for (pmc=idev->mc_list; pmc; pmc=pmc->next) { 1463 if (pmc->mca_flags & MAF_NOREPORT) 1464 continue; 1465 spin_lock_bh(&pmc->mca_lock); 1466 if (pmc->mca_sfcount[MCAST_EXCLUDE]) 1467 type = MLD2_MODE_IS_EXCLUDE; 1468 else 1469 type = MLD2_MODE_IS_INCLUDE; 1470 skb = add_grec(skb, pmc, type, 0, 0); 1471 spin_unlock_bh(&pmc->mca_lock); 1472 } 1473 read_unlock_bh(&idev->lock); 1474 } else { 1475 spin_lock_bh(&pmc->mca_lock); 1476 if (pmc->mca_sfcount[MCAST_EXCLUDE]) 1477 type = MLD2_MODE_IS_EXCLUDE; 1478 else 1479 type = MLD2_MODE_IS_INCLUDE; 1480 skb = add_grec(skb, pmc, type, 0, 0); 1481 spin_unlock_bh(&pmc->mca_lock); 1482 } 1483 if (skb) 1484 mld_sendpack(skb); 1485 } 1486 1487 /* 1488 * remove zero-count source records from a source filter list 1489 */ 1490 static void mld_clear_zeros(struct ip6_sf_list **ppsf) 1491 { 1492 struct ip6_sf_list *psf_prev, *psf_next, *psf; 1493 1494 psf_prev = 0; 1495 for (psf=*ppsf; psf; psf = psf_next) { 1496 psf_next = psf->sf_next; 1497 if (psf->sf_crcount == 0) { 1498 if (psf_prev) 1499 psf_prev->sf_next = psf->sf_next; 1500 else 1501 *ppsf = psf->sf_next; 1502 kfree(psf); 1503 } else 1504 psf_prev = psf; 1505 } 1506 } 1507 1508 static void mld_send_cr(struct inet6_dev *idev) 1509 { 1510 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next; 1511 struct sk_buff *skb = 0; 1512 int type, dtype; 1513 1514 read_lock_bh(&idev->lock); 1515 write_lock_bh(&idev->mc_lock); 1516 1517 /* deleted MCA's */ 1518 pmc_prev = 0; 1519 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) { 1520 pmc_next = pmc->next; 1521 if (pmc->mca_sfmode == MCAST_INCLUDE) { 1522 type = MLD2_BLOCK_OLD_SOURCES; 1523 dtype = MLD2_BLOCK_OLD_SOURCES; 1524 skb = add_grec(skb, pmc, type, 1, 0); 1525 skb = add_grec(skb, pmc, dtype, 1, 1); 1526 } 1527 if (pmc->mca_crcount) { 1528 pmc->mca_crcount--; 1529 if (pmc->mca_sfmode == MCAST_EXCLUDE) { 1530 type = MLD2_CHANGE_TO_INCLUDE; 1531 skb = add_grec(skb, pmc, type, 1, 0); 1532 } 1533 if (pmc->mca_crcount == 0) { 1534 mld_clear_zeros(&pmc->mca_tomb); 1535 mld_clear_zeros(&pmc->mca_sources); 1536 } 1537 } 1538 if (pmc->mca_crcount == 0 && !pmc->mca_tomb && 1539 !pmc->mca_sources) { 1540 if (pmc_prev) 1541 pmc_prev->next = pmc_next; 1542 else 1543 idev->mc_tomb = pmc_next; 1544 in6_dev_put(pmc->idev); 1545 kfree(pmc); 1546 } else 1547 pmc_prev = pmc; 1548 } 1549 write_unlock_bh(&idev->mc_lock); 1550 1551 /* change recs */ 1552 for (pmc=idev->mc_list; pmc; pmc=pmc->next) { 1553 spin_lock_bh(&pmc->mca_lock); 1554 if (pmc->mca_sfcount[MCAST_EXCLUDE]) { 1555 type = MLD2_BLOCK_OLD_SOURCES; 1556 dtype = MLD2_ALLOW_NEW_SOURCES; 1557 } else { 1558 type = MLD2_ALLOW_NEW_SOURCES; 1559 dtype = MLD2_BLOCK_OLD_SOURCES; 1560 } 1561 skb = add_grec(skb, pmc, type, 0, 0); 1562 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 1563 1564 /* filter mode changes */ 1565 if (pmc->mca_crcount) { 1566 pmc->mca_crcount--; 1567 if (pmc->mca_sfmode == MCAST_EXCLUDE) 1568 type = MLD2_CHANGE_TO_EXCLUDE; 1569 else 1570 type = MLD2_CHANGE_TO_INCLUDE; 1571 skb = add_grec(skb, pmc, type, 0, 0); 1572 } 1573 spin_unlock_bh(&pmc->mca_lock); 1574 } 1575 read_unlock_bh(&idev->lock); 1576 if (!skb) 1577 return; 1578 (void) mld_sendpack(skb); 1579 } 1580 1581 void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type) 1582 { 1583 struct sock *sk = igmp6_socket->sk; 1584 struct sk_buff *skb; 1585 struct icmp6hdr *hdr; 1586 struct in6_addr *snd_addr; 1587 struct in6_addr *addrp; 1588 struct in6_addr addr_buf; 1589 struct in6_addr all_routers; 1590 int err, len, payload_len, full_len; 1591 u8 ra[8] = { IPPROTO_ICMPV6, 0, 1592 IPV6_TLV_ROUTERALERT, 2, 0, 0, 1593 IPV6_TLV_PADN, 0 }; 1594 1595 snd_addr = addr; 1596 if (type == ICMPV6_MGM_REDUCTION) { 1597 snd_addr = &all_routers; 1598 ipv6_addr_all_routers(&all_routers); 1599 } 1600 1601 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr); 1602 payload_len = len + sizeof(ra); 1603 full_len = sizeof(struct ipv6hdr) + payload_len; 1604 1605 skb = sock_alloc_send_skb(sk, dev->hard_header_len + full_len + 15, 1, &err); 1606 1607 if (skb == NULL) 1608 return; 1609 1610 skb_reserve(skb, (dev->hard_header_len + 15) & ~15); 1611 1612 if (ipv6_get_lladdr(dev, &addr_buf)) { 1613 /* <draft-ietf-magma-mld-source-02.txt>: 1614 * use unspecified address as the source address 1615 * when a valid link-local address is not available. 1616 */ 1617 memset(&addr_buf, 0, sizeof(addr_buf)); 1618 } 1619 1620 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len); 1621 1622 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra)); 1623 1624 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr)); 1625 memset(hdr, 0, sizeof(struct icmp6hdr)); 1626 hdr->icmp6_type = type; 1627 1628 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr)); 1629 ipv6_addr_copy(addrp, addr); 1630 1631 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len, 1632 IPPROTO_ICMPV6, 1633 csum_partial((__u8 *) hdr, len, 0)); 1634 1635 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev, 1636 mld_dev_queue_xmit); 1637 if (!err) { 1638 if (type == ICMPV6_MGM_REDUCTION) 1639 ICMP6_INC_STATS(Icmp6OutGroupMembReductions); 1640 else 1641 ICMP6_INC_STATS(Icmp6OutGroupMembResponses); 1642 ICMP6_INC_STATS(Icmp6OutMsgs); 1643 } 1644 1645 return; 1646 } 1647 1648 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode, 1649 struct in6_addr *psfsrc) 1650 { 1651 struct ip6_sf_list *psf, *psf_prev; 1652 int rv = 0; 1653 1654 psf_prev = 0; 1655 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { 1656 if (ipv6_addr_cmp(&psf->sf_addr, psfsrc) == 0) 1657 break; 1658 psf_prev = psf; 1659 } 1660 if (!psf || psf->sf_count[sfmode] == 0) { 1661 /* source filter not found, or count wrong => bug */ 1662 return -ESRCH; 1663 } 1664 psf->sf_count[sfmode]--; 1665 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 1666 struct inet6_dev *idev = pmc->idev; 1667 1668 /* no more filters for this source */ 1669 if (psf_prev) 1670 psf_prev->sf_next = psf->sf_next; 1671 else 1672 pmc->mca_sources = psf->sf_next; 1673 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) && 1674 !MLD_V1_SEEN(idev)) { 1675 psf->sf_crcount = idev->mc_qrv; 1676 psf->sf_next = pmc->mca_tomb; 1677 pmc->mca_tomb = psf; 1678 rv = 1; 1679 } else 1680 kfree(psf); 1681 } 1682 return rv; 1683 } 1684 1685 int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca, int sfmode, 1686 int sfcount, struct in6_addr *psfsrc, int delta) 1687 { 1688 struct ifmcaddr6 *pmc; 1689 int changerec = 0; 1690 int i, err; 1691 1692 if (!idev) 1693 return -ENODEV; 1694 read_lock_bh(&idev->lock); 1695 for (pmc=idev->mc_list; pmc; pmc=pmc->next) { 1696 if (ipv6_addr_cmp(pmca, &pmc->mca_addr) == 0) 1697 break; 1698 } 1699 if (!pmc) { 1700 /* MCA not found?? bug */ 1701 read_unlock_bh(&idev->lock); 1702 return -ESRCH; 1703 } 1704 spin_lock_bh(&pmc->mca_lock); 1705 sf_markstate(pmc); 1706 if (!delta) { 1707 if (!pmc->mca_sfcount[sfmode]) { 1708 spin_unlock_bh(&pmc->mca_lock); 1709 read_unlock_bh(&idev->lock); 1710 return -EINVAL; 1711 } 1712 pmc->mca_sfcount[sfmode]--; 1713 } 1714 err = 0; 1715 for (i=0; i<sfcount; i++) { 1716 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]); 1717 1718 changerec |= rv > 0; 1719 if (!err && rv < 0) 1720 err = rv; 1721 } 1722 if (pmc->mca_sfmode == MCAST_EXCLUDE && 1723 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 && 1724 pmc->mca_sfcount[MCAST_INCLUDE]) { 1725 struct ip6_sf_list *psf; 1726 1727 /* filter mode change */ 1728 pmc->mca_sfmode = MCAST_INCLUDE; 1729 pmc->mca_crcount = idev->mc_qrv; 1730 idev->mc_ifc_count = pmc->mca_crcount; 1731 for (psf=pmc->mca_sources; psf; psf = psf->sf_next) 1732 psf->sf_crcount = 0; 1733 mld_ifc_event(pmc->idev); 1734 } else if (sf_setstate(pmc) || changerec) 1735 mld_ifc_event(pmc->idev); 1736 spin_unlock_bh(&pmc->mca_lock); 1737 read_unlock_bh(&idev->lock); 1738 return err; 1739 } 1740 1741 /* 1742 * Add multicast single-source filter to the interface list 1743 */ 1744 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode, 1745 struct in6_addr *psfsrc, int delta) 1746 { 1747 struct ip6_sf_list *psf, *psf_prev; 1748 1749 psf_prev = 0; 1750 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { 1751 if (ipv6_addr_cmp(&psf->sf_addr, psfsrc) == 0) 1752 break; 1753 psf_prev = psf; 1754 } 1755 if (!psf) { 1756 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC); 1757 if (!psf) 1758 return -ENOBUFS; 1759 memset(psf, 0, sizeof(*psf)); 1760 psf->sf_addr = *psfsrc; 1761 if (psf_prev) { 1762 psf_prev->sf_next = psf; 1763 } else 1764 pmc->mca_sources = psf; 1765 } 1766 psf->sf_count[sfmode]++; 1767 return 0; 1768 } 1769 1770 static void sf_markstate(struct ifmcaddr6 *pmc) 1771 { 1772 struct ip6_sf_list *psf; 1773 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE]; 1774 1775 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) 1776 if (pmc->mca_sfcount[MCAST_EXCLUDE]) { 1777 psf->sf_oldin = mca_xcount == 1778 psf->sf_count[MCAST_EXCLUDE] && 1779 !psf->sf_count[MCAST_INCLUDE]; 1780 } else 1781 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 1782 } 1783 1784 static int sf_setstate(struct ifmcaddr6 *pmc) 1785 { 1786 struct ip6_sf_list *psf; 1787 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE]; 1788 int qrv = pmc->idev->mc_qrv; 1789 int new_in, rv; 1790 1791 rv = 0; 1792 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { 1793 if (pmc->mca_sfcount[MCAST_EXCLUDE]) { 1794 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 1795 !psf->sf_count[MCAST_INCLUDE]; 1796 } else 1797 new_in = psf->sf_count[MCAST_INCLUDE] != 0; 1798 if (new_in != psf->sf_oldin) { 1799 psf->sf_crcount = qrv; 1800 rv++; 1801 } 1802 } 1803 return rv; 1804 } 1805 1806 /* 1807 * Add multicast source filter list to the interface list 1808 */ 1809 int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca, int sfmode, 1810 int sfcount, struct in6_addr *psfsrc, int delta) 1811 { 1812 struct ifmcaddr6 *pmc; 1813 int isexclude; 1814 int i, err; 1815 1816 if (!idev) 1817 return -ENODEV; 1818 read_lock_bh(&idev->lock); 1819 for (pmc=idev->mc_list; pmc; pmc=pmc->next) { 1820 if (ipv6_addr_cmp(pmca, &pmc->mca_addr) == 0) 1821 break; 1822 } 1823 if (!pmc) { 1824 /* MCA not found?? bug */ 1825 read_unlock_bh(&idev->lock); 1826 return -ESRCH; 1827 } 1828 spin_lock_bh(&pmc->mca_lock); 1829 1830 sf_markstate(pmc); 1831 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE; 1832 if (!delta) 1833 pmc->mca_sfcount[sfmode]++; 1834 err = 0; 1835 for (i=0; i<sfcount; i++) { 1836 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta); 1837 if (err) 1838 break; 1839 } 1840 if (err) { 1841 int j; 1842 1843 pmc->mca_sfcount[sfmode]--; 1844 for (j=0; j<i; j++) 1845 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]); 1846 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) { 1847 struct inet6_dev *idev = pmc->idev; 1848 struct ip6_sf_list *psf; 1849 1850 /* filter mode change */ 1851 if (pmc->mca_sfcount[MCAST_EXCLUDE]) 1852 pmc->mca_sfmode = MCAST_EXCLUDE; 1853 else if (pmc->mca_sfcount[MCAST_INCLUDE]) 1854 pmc->mca_sfmode = MCAST_INCLUDE; 1855 /* else no filters; keep old mode for reports */ 1856 1857 pmc->mca_crcount = idev->mc_qrv; 1858 idev->mc_ifc_count = pmc->mca_crcount; 1859 for (psf=pmc->mca_sources; psf; psf = psf->sf_next) 1860 psf->sf_crcount = 0; 1861 mld_ifc_event(idev); 1862 } else if (sf_setstate(pmc)) 1863 mld_ifc_event(idev); 1864 spin_unlock_bh(&pmc->mca_lock); 1865 read_unlock_bh(&idev->lock); 1866 return err; 1867 } 1868 1869 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc) 1870 { 1871 struct ip6_sf_list *psf, *nextpsf; 1872 1873 for (psf=pmc->mca_tomb; psf; psf=nextpsf) { 1874 nextpsf = psf->sf_next; 1875 kfree(psf); 1876 } 1877 pmc->mca_tomb = 0; 1878 for (psf=pmc->mca_sources; psf; psf=nextpsf) { 1879 nextpsf = psf->sf_next; 1880 kfree(psf); 1881 } 1882 pmc->mca_sources = 0; 1883 pmc->mca_sfmode = MCAST_EXCLUDE; 1884 pmc->mca_sfcount[MCAST_INCLUDE] = 0; 1885 pmc->mca_sfcount[MCAST_EXCLUDE] = 1; 1886 } 1887 1888 static void igmp6_join_group(struct ifmcaddr6 *ma) 1889 { 1890 unsigned long delay; 1891 1892 if (ma->mca_flags & MAF_NOREPORT) 1893 return; 1894 1895 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT); 1896 1897 delay = net_random() % IGMP6_UNSOLICITED_IVAL; 1898 1899 spin_lock_bh(&ma->mca_lock); 1900 if (del_timer(&ma->mca_timer)) { 1901 atomic_dec(&ma->mca_refcnt); 1902 delay = ma->mca_timer.expires - jiffies; 1903 } 1904 1905 if (!mod_timer(&ma->mca_timer, jiffies + delay)) 1906 atomic_inc(&ma->mca_refcnt); 1907 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER; 1908 spin_unlock_bh(&ma->mca_lock); 1909 } 1910 1911 int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml, 1912 struct inet6_dev *idev) 1913 { 1914 int err; 1915 1916 if (iml->sflist == 0) { 1917 /* any-source empty exclude case */ 1918 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, 0, 0); 1919 } 1920 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 1921 iml->sflist->sl_count, iml->sflist->sl_addr, 0); 1922 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max)); 1923 iml->sflist = 0; 1924 return err; 1925 } 1926 1927 static void igmp6_leave_group(struct ifmcaddr6 *ma) 1928 { 1929 if (MLD_V1_SEEN(ma->idev)) { 1930 if (ma->mca_flags & MAF_LAST_REPORTER) 1931 igmp6_send(&ma->mca_addr, ma->idev->dev, 1932 ICMPV6_MGM_REDUCTION); 1933 } else { 1934 mld_add_delrec(ma->idev, ma); 1935 mld_ifc_event(ma->idev); 1936 } 1937 } 1938 1939 static void mld_gq_timer_expire(unsigned long data) 1940 { 1941 struct inet6_dev *idev = (struct inet6_dev *)data; 1942 1943 idev->mc_gq_running = 0; 1944 mld_send_report(idev, 0); 1945 __in6_dev_put(idev); 1946 } 1947 1948 static void mld_ifc_timer_expire(unsigned long data) 1949 { 1950 struct inet6_dev *idev = (struct inet6_dev *)data; 1951 1952 mld_send_cr(idev); 1953 if (idev->mc_ifc_count) { 1954 idev->mc_ifc_count--; 1955 if (idev->mc_ifc_count) 1956 mld_ifc_start_timer(idev, idev->mc_maxdelay); 1957 } 1958 __in6_dev_put(idev); 1959 } 1960 1961 static void mld_ifc_event(struct inet6_dev *idev) 1962 { 1963 if (MLD_V1_SEEN(idev)) 1964 return; 1965 idev->mc_ifc_count = idev->mc_qrv; 1966 mld_ifc_start_timer(idev, 1); 1967 } 1968 1969 1970 static void igmp6_timer_handler(unsigned long data) 1971 { 1972 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data; 1973 1974 if (MLD_V1_SEEN(ma->idev)) 1975 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT); 1976 else 1977 mld_send_report(ma->idev, ma); 1978 1979 spin_lock(&ma->mca_lock); 1980 ma->mca_flags |= MAF_LAST_REPORTER; 1981 ma->mca_flags &= ~MAF_TIMER_RUNNING; 1982 spin_unlock(&ma->mca_lock); 1983 ma_put(ma); 1984 } 1985 1986 /* Device going down */ 1987 1988 void ipv6_mc_down(struct inet6_dev *idev) 1989 { 1990 struct ifmcaddr6 *i; 1991 1992 /* Withdraw multicast list */ 1993 1994 read_lock_bh(&idev->lock); 1995 idev->mc_ifc_count = 0; 1996 if (del_timer(&idev->mc_ifc_timer)) 1997 __in6_dev_put(idev); 1998 idev->mc_gq_running = 0; 1999 if (del_timer(&idev->mc_gq_timer)) 2000 __in6_dev_put(idev); 2001 2002 for (i = idev->mc_list; i; i=i->next) 2003 igmp6_group_dropped(i); 2004 read_unlock_bh(&idev->lock); 2005 2006 mld_clear_delrec(idev); 2007 } 2008 2009 2010 /* Device going up */ 2011 2012 void ipv6_mc_up(struct inet6_dev *idev) 2013 { 2014 struct ifmcaddr6 *i; 2015 2016 /* Install multicast list, except for all-nodes (already installed) */ 2017 2018 read_lock_bh(&idev->lock); 2019 for (i = idev->mc_list; i; i=i->next) 2020 igmp6_group_added(i); 2021 read_unlock_bh(&idev->lock); 2022 } 2023 2024 /* IPv6 device initialization. */ 2025 2026 void ipv6_mc_init_dev(struct inet6_dev *idev) 2027 { 2028 struct in6_addr maddr; 2029 2030 write_lock_bh(&idev->lock); 2031 idev->mc_lock = RW_LOCK_UNLOCKED; 2032 idev->mc_gq_running = 0; 2033 init_timer(&idev->mc_gq_timer); 2034 idev->mc_gq_timer.data = (unsigned long) idev; 2035 idev->mc_gq_timer.function = &mld_gq_timer_expire; 2036 idev->mc_tomb = 0; 2037 idev->mc_ifc_count = 0; 2038 init_timer(&idev->mc_ifc_timer); 2039 idev->mc_ifc_timer.data = (unsigned long) idev; 2040 idev->mc_ifc_timer.function = &mld_ifc_timer_expire; 2041 idev->mc_qrv = MLD_QRV_DEFAULT; 2042 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL; 2043 idev->mc_v1_seen = 0; 2044 write_unlock_bh(&idev->lock); 2045 2046 /* Add all-nodes address. */ 2047 ipv6_addr_all_nodes(&maddr); 2048 ipv6_dev_mc_inc(idev->dev, &maddr); 2049 } 2050 2051 /* 2052 * Device is about to be destroyed: clean up. 2053 */ 2054 2055 void ipv6_mc_destroy_dev(struct inet6_dev *idev) 2056 { 2057 struct ifmcaddr6 *i; 2058 struct in6_addr maddr; 2059 2060 /* Deactivate timers */ 2061 ipv6_mc_down(idev); 2062 2063 /* Delete all-nodes address. */ 2064 ipv6_addr_all_nodes(&maddr); 2065 2066 /* We cannot call ipv6_dev_mc_dec() directly, our caller in 2067 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will 2068 * fail. 2069 */ 2070 __ipv6_dev_mc_dec(idev->dev, idev, &maddr); 2071 2072 write_lock_bh(&idev->lock); 2073 while ((i = idev->mc_list) != NULL) { 2074 idev->mc_list = i->next; 2075 write_unlock_bh(&idev->lock); 2076 2077 igmp6_group_dropped(i); 2078 ma_put(i); 2079 2080 write_lock_bh(&idev->lock); 2081 } 2082 write_unlock_bh(&idev->lock); 2083 } 2084 2085 #ifdef CONFIG_PROC_FS 2086 static int igmp6_read_proc(char *buffer, char **start, off_t offset, 2087 int length, int *eof, void *data) 2088 { 2089 off_t pos=0, begin=0; 2090 struct ifmcaddr6 *im; 2091 int len=0; 2092 struct net_device *dev; 2093 2094 read_lock(&dev_base_lock); 2095 for (dev = dev_base; dev; dev = dev->next) { 2096 struct inet6_dev *idev; 2097 2098 if ((idev = in6_dev_get(dev)) == NULL) 2099 continue; 2100 2101 read_lock_bh(&idev->lock); 2102 for (im = idev->mc_list; im; im = im->next) { 2103 int i; 2104 2105 len += sprintf(buffer+len,"%-4d %-15s ", dev->ifindex, dev->name); 2106 2107 for (i=0; i<16; i++) 2108 len += sprintf(buffer+len, "%02x", im->mca_addr.s6_addr[i]); 2109 2110 len+=sprintf(buffer+len, 2111 " %5d %08X %ld\n", 2112 im->mca_users, 2113 im->mca_flags, 2114 (im->mca_flags&MAF_TIMER_RUNNING) ? im->mca_timer.expires-jiffies : 0); 2115 2116 pos=begin+len; 2117 if (pos < offset) { 2118 len=0; 2119 begin=pos; 2120 } 2121 if (pos > offset+length) { 2122 read_unlock_bh(&idev->lock); 2123 in6_dev_put(idev); 2124 goto done; 2125 } 2126 } 2127 read_unlock_bh(&idev->lock); 2128 in6_dev_put(idev); 2129 } 2130 *eof = 1; 2131 2132 done: 2133 read_unlock(&dev_base_lock); 2134 2135 *start=buffer+(offset-begin); 2136 len-=(offset-begin); 2137 if(len>length) 2138 len=length; 2139 if (len<0) 2140 len=0; 2141 return len; 2142 } 2143 2144 static int ip6_mcf_read_proc(char *buffer, char **start, off_t offset, 2145 int length, int *eof, void *data) 2146 { 2147 off_t pos=0, begin=0; 2148 int len=0; 2149 int first=1; 2150 struct net_device *dev; 2151 2152 read_lock(&dev_base_lock); 2153 for (dev=dev_base; dev; dev=dev->next) { 2154 struct inet6_dev *idev = in6_dev_get(dev); 2155 struct ifmcaddr6 *imc; 2156 2157 if (idev == NULL) 2158 continue; 2159 2160 read_lock_bh(&idev->lock); 2161 2162 for (imc=idev->mc_list; imc; imc=imc->next) { 2163 struct ip6_sf_list *psf; 2164 unsigned long i; 2165 2166 spin_lock_bh(&imc->mca_lock); 2167 for (psf=imc->mca_sources; psf; psf=psf->sf_next) { 2168 if (first) { 2169 len += sprintf(buffer+len, "%3s %6s " 2170 "%32s %32s %6s %6s\n", "Idx", 2171 "Device", "Multicast Address", 2172 "Source Address", "INC", "EXC"); 2173 first = 0; 2174 } 2175 len += sprintf(buffer+len,"%3d %6.6s ", 2176 dev->ifindex, dev->name); 2177 2178 for (i=0; i<16; i++) 2179 len += sprintf(buffer+len, "%02x", 2180 imc->mca_addr.s6_addr[i]); 2181 buffer[len++] = ' '; 2182 for (i=0; i<16; i++) 2183 len += sprintf(buffer+len, "%02x", 2184 psf->sf_addr.s6_addr[i]); 2185 len += sprintf(buffer+len, " %6lu %6lu\n", 2186 psf->sf_count[MCAST_INCLUDE], 2187 psf->sf_count[MCAST_EXCLUDE]); 2188 pos = begin+len; 2189 if (pos < offset) { 2190 len=0; 2191 begin=pos; 2192 } 2193 if (pos > offset+length) { 2194 spin_unlock_bh(&imc->mca_lock); 2195 read_unlock_bh(&idev->lock); 2196 in6_dev_put(idev); 2197 goto done; 2198 } 2199 } 2200 spin_unlock_bh(&imc->mca_lock); 2201 } 2202 read_unlock_bh(&idev->lock); 2203 in6_dev_put(idev); 2204 } 2205 *eof = 1; 2206 2207 done: 2208 read_unlock(&dev_base_lock); 2209 2210 *start=buffer+(offset-begin); 2211 len-=(offset-begin); 2212 if(len>length) 2213 len=length; 2214 if (len<0) 2215 len=0; 2216 return len; 2217 } 2218 #endif 2219 2220 int __init igmp6_init(struct net_proto_family *ops) 2221 { 2222 struct sock *sk; 2223 int err; 2224 2225 igmp6_socket = sock_alloc(); 2226 if (igmp6_socket == NULL) { 2227 printk(KERN_ERR 2228 "Failed to create the IGMP6 control socket.\n"); 2229 return -1; 2230 } 2231 igmp6_socket->inode->i_uid = 0; 2232 igmp6_socket->inode->i_gid = 0; 2233 igmp6_socket->type = SOCK_RAW; 2234 2235 if((err = ops->create(igmp6_socket, IPPROTO_ICMPV6)) < 0) { 2236 printk(KERN_DEBUG 2237 "Failed to initialize the IGMP6 control socket (err %d).\n", 2238 err); 2239 sock_release(igmp6_socket); 2240 igmp6_socket = NULL; /* For safety. */ 2241 return err; 2242 } 2243 2244 sk = igmp6_socket->sk; 2245 sk->allocation = GFP_ATOMIC; 2246 sk->prot->unhash(sk); 2247 2248 sk->net_pinfo.af_inet6.hop_limit = 1; 2249 #ifdef CONFIG_PROC_FS 2250 create_proc_read_entry("net/igmp6", 0, 0, igmp6_read_proc, NULL); 2251 create_proc_read_entry("net/mcfilter6", 0, 0, ip6_mcf_read_proc, NULL); 2252 #endif 2253 2254 return 0; 2255 } 2256 2257 void igmp6_cleanup(void) 2258 { 2259 sock_release(igmp6_socket); 2260 igmp6_socket = NULL; /* for safety */ 2261 #ifdef CONFIG_PROC_FS 2262 remove_proc_entry("net/igmp6", 0); 2263 #endif 2264 } 2265
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.