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