1 /* $FreeBSD$ */ 2 /* $KAME: mld6.c,v 1.27 2001/04/04 05:17:30 itojun Exp $ */ 3 4 /*- 5 * Copyright (C) 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /*- 34 * Copyright (c) 1988 Stephen Deering. 35 * Copyright (c) 1992, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * This code is derived from software contributed to Berkeley by 39 * Stephen Deering of Stanford University. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)igmp.c 8.1 (Berkeley) 7/19/93 66 */ 67 68 #include "opt_inet.h" 69 #include "opt_inet6.h" 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/mbuf.h> 74 #include <sys/socket.h> 75 #include <sys/protosw.h> 76 #include <sys/syslog.h> 77 #include <sys/malloc.h> 78 79 #include <net/if.h> 80 81 #include <netinet/in.h> 82 #include <netinet/in_var.h> 83 #include <netinet/ip6.h> 84 #include <netinet6/ip6_var.h> 85 #include <netinet6/scope6_var.h> 86 #include <netinet/icmp6.h> 87 #include <netinet6/mld6_var.h> 88 89 #include <net/net_osdep.h> 90 91 /* 92 * Protocol constants 93 */ 94 95 /* denotes that the MLD max response delay field specifies time in milliseconds */ 96 #define MLD_TIMER_SCALE 1000 97 /* 98 * time between repetitions of a node's initial report of interest in a 99 * multicast address(in seconds) 100 */ 101 #define MLD_UNSOLICITED_REPORT_INTERVAL 10 102 103 static struct ip6_pktopts ip6_opts; 104 static int mld6_timers_are_running; 105 106 static void mld6_sendpkt(struct in6_multi *, int, const struct in6_addr *); 107 108 void 109 mld6_init() 110 { 111 static u_int8_t hbh_buf[8]; 112 struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf; 113 u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD); 114 115 mld6_timers_are_running = 0; 116 117 /* ip6h_nxt will be fill in later */ 118 hbh->ip6h_len = 0; /* (8 >> 3) - 1 */ 119 120 /* XXX: grotty hard coding... */ 121 hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */ 122 hbh_buf[3] = 0; 123 hbh_buf[4] = IP6OPT_ROUTER_ALERT; 124 hbh_buf[5] = IP6OPT_RTALERT_LEN - 2; 125 bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t)); 126 127 ip6_initpktopts(&ip6_opts); 128 ip6_opts.ip6po_hbh = hbh; 129 } 130 131 void 132 mld6_start_listening(in6m) 133 struct in6_multi *in6m; 134 { 135 struct in6_addr all_in6; 136 int s = splnet(); 137 138 /* 139 * RFC2710 page 10: 140 * The node never sends a Report or Done for the link-scope all-nodes 141 * address. 142 * MLD messages are never sent for multicast addresses whose scope is 0 143 * (reserved) or 1 (node-local). 144 */ 145 all_in6 = in6addr_linklocal_allnodes; 146 if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) { 147 /* XXX: this should not happen! */ 148 in6m->in6m_timer = 0; 149 in6m->in6m_state = MLD_OTHERLISTENER; 150 } 151 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) || 152 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < 153 IPV6_ADDR_SCOPE_LINKLOCAL) { 154 in6m->in6m_timer = 0; 155 in6m->in6m_state = MLD_OTHERLISTENER; 156 } else { 157 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL); 158 in6m->in6m_timer = 159 MLD_RANDOM_DELAY(MLD_UNSOLICITED_REPORT_INTERVAL * 160 PR_FASTHZ); 161 in6m->in6m_state = MLD_IREPORTEDLAST; 162 mld6_timers_are_running = 1; 163 } 164 splx(s); 165 } 166 167 void 168 mld6_stop_listening(in6m) 169 struct in6_multi *in6m; 170 { 171 struct in6_addr allnode, allrouter; 172 173 allnode = in6addr_linklocal_allnodes; 174 if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) { 175 /* XXX: this should not happen! */ 176 return; 177 } 178 allrouter = in6addr_linklocal_allrouters; 179 if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) { 180 /* XXX impossible */ 181 return; 182 } 183 if (in6m->in6m_state == MLD_IREPORTEDLAST && 184 !IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode) && 185 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > 186 IPV6_ADDR_SCOPE_INTFACELOCAL) { 187 mld6_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter); 188 } 189 } 190 191 void 192 mld6_input(m, off) 193 struct mbuf *m; 194 int off; 195 { 196 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 197 struct mld_hdr *mldh; 198 struct ifnet *ifp = m->m_pkthdr.rcvif; 199 struct in6_multi *in6m; 200 struct in6_addr mld_addr, all_in6; 201 struct in6_ifaddr *ia; 202 struct ifmultiaddr *ifma; 203 int timer; /* timer value in the MLD query header */ 204 205 #ifndef PULLDOWN_TEST 206 IP6_EXTHDR_CHECK(m, off, sizeof(*mldh),); 207 mldh = (struct mld_hdr *)(mtod(m, caddr_t) + off); 208 #else 209 IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh)); 210 if (mldh == NULL) { 211 icmp6stat.icp6s_tooshort++; 212 return; 213 } 214 #endif 215 216 /* source address validation */ 217 ip6 = mtod(m, struct ip6_hdr *); /* in case mpullup */ 218 if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) { 219 log(LOG_ERR, 220 "mld6_input: src %s is not link-local (grp=%s)\n", 221 ip6_sprintf(&ip6->ip6_src), 222 ip6_sprintf(&mldh->mld_addr)); 223 /* 224 * spec (RFC2710) does not explicitly 225 * specify to discard the packet from a non link-local 226 * source address. But we believe it's expected to do so. 227 * XXX: do we have to allow :: as source? 228 */ 229 m_freem(m); 230 return; 231 } 232 233 /* 234 * make a copy for local work (in6_setscope() may modify the 1st arg) 235 */ 236 mld_addr = mldh->mld_addr; 237 if (in6_setscope(&mld_addr, ifp, NULL)) { 238 /* XXX: this should not happen! */ 239 m_free(m); 240 return; 241 } 242 243 /* 244 * In the MLD6 specification, there are 3 states and a flag. 245 * 246 * In Non-Listener state, we simply don't have a membership record. 247 * In Delaying Listener state, our timer is running (in6m->in6m_timer) 248 * In Idle Listener state, our timer is not running (in6m->in6m_timer==0) 249 * 250 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if 251 * we have heard a report from another member, or MLD_IREPORTEDLAST 252 * if we sent the last report. 253 */ 254 switch(mldh->mld_type) { 255 case MLD_LISTENER_QUERY: 256 if (ifp->if_flags & IFF_LOOPBACK) 257 break; 258 259 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) && 260 !IN6_IS_ADDR_MULTICAST(&mld_addr)) 261 break; /* print error or log stat? */ 262 263 all_in6 = in6addr_linklocal_allnodes; 264 if (in6_setscope(&all_in6, ifp, NULL)) { 265 /* XXX: this should not happen! */ 266 break; 267 } 268 269 /* 270 * - Start the timers in all of our membership records 271 * that the query applies to for the interface on 272 * which the query arrived excl. those that belong 273 * to the "all-nodes" group (ff02::1). 274 * - Restart any timer that is already running but has 275 * A value longer than the requested timeout. 276 * - Use the value specified in the query message as 277 * the maximum timeout. 278 */ 279 IFP_TO_IA6(ifp, ia); 280 if (ia == NULL) 281 break; 282 283 /* 284 * XXX: System timer resolution is too low to handle Max 285 * Response Delay, so set 1 to the internal timer even if 286 * the calculated value equals to zero when Max Response 287 * Delay is positive. 288 */ 289 timer = ntohs(mldh->mld_maxdelay) * PR_FASTHZ / MLD_TIMER_SCALE; 290 if (timer == 0 && mldh->mld_maxdelay) 291 timer = 1; 292 293 IF_ADDR_LOCK(ifp); 294 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 295 if (ifma->ifma_addr->sa_family != AF_INET6) 296 continue; 297 in6m = (struct in6_multi *)ifma->ifma_protospec; 298 299 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) || 300 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < 301 IPV6_ADDR_SCOPE_LINKLOCAL) 302 continue; 303 304 if (IN6_IS_ADDR_UNSPECIFIED(&mld_addr) || 305 IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr)) { 306 if (timer == 0) { 307 /* send a report immediately */ 308 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, 309 NULL); 310 in6m->in6m_timer = 0; /* reset timer */ 311 in6m->in6m_state = MLD_IREPORTEDLAST; 312 } 313 else if (in6m->in6m_timer == 0 || /*idle state*/ 314 in6m->in6m_timer > timer) { 315 in6m->in6m_timer = 316 MLD_RANDOM_DELAY(timer); 317 mld6_timers_are_running = 1; 318 } 319 } 320 } 321 IF_ADDR_UNLOCK(ifp); 322 break; 323 324 case MLD_LISTENER_REPORT: 325 /* 326 * For fast leave to work, we have to know that we are the 327 * last person to send a report for this group. Reports 328 * can potentially get looped back if we are a multicast 329 * router, so discard reports sourced by me. 330 * Note that it is impossible to check IFF_LOOPBACK flag of 331 * ifp for this purpose, since ip6_mloopback pass the physical 332 * interface to looutput. 333 */ 334 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */ 335 break; 336 337 if (!IN6_IS_ADDR_MULTICAST(&mld_addr)) 338 break; 339 340 /* 341 * If we belong to the group being reported, stop 342 * our timer for that group. 343 */ 344 IN6_LOOKUP_MULTI(mld_addr, ifp, in6m); 345 if (in6m) { 346 in6m->in6m_timer = 0; /* transit to idle state */ 347 in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */ 348 } 349 break; 350 default: /* this is impossible */ 351 log(LOG_ERR, "mld6_input: illegal type(%d)", mldh->mld_type); 352 break; 353 } 354 355 m_freem(m); 356 } 357 358 void 359 mld6_fasttimeo() 360 { 361 struct in6_multi *in6m; 362 struct in6_multistep step; 363 int s; 364 365 /* 366 * Quick check to see if any work needs to be done, in order 367 * to minimize the overhead of fasttimo processing. 368 */ 369 if (!mld6_timers_are_running) 370 return; 371 372 s = splnet(); 373 374 mld6_timers_are_running = 0; 375 IN6_FIRST_MULTI(step, in6m); 376 while (in6m != NULL) { 377 if (in6m->in6m_timer == 0) { 378 /* do nothing */ 379 } else if (--in6m->in6m_timer == 0) { 380 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL); 381 in6m->in6m_state = MLD_IREPORTEDLAST; 382 } else { 383 mld6_timers_are_running = 1; 384 } 385 IN6_NEXT_MULTI(step, in6m); 386 } 387 388 splx(s); 389 } 390 391 static void 392 mld6_sendpkt(in6m, type, dst) 393 struct in6_multi *in6m; 394 int type; 395 const struct in6_addr *dst; 396 { 397 struct mbuf *mh, *md; 398 struct mld_hdr *mldh; 399 struct ip6_hdr *ip6; 400 struct ip6_moptions im6o; 401 struct in6_ifaddr *ia; 402 struct ifnet *ifp = in6m->in6m_ifp; 403 struct ifnet *outif = NULL; 404 405 /* 406 * At first, find a link local address on the outgoing interface 407 * to use as the source address of the MLD packet. 408 */ 409 if ((ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST)) 410 == NULL) 411 return; 412 413 /* 414 * Allocate mbufs to store ip6 header and MLD header. 415 * We allocate 2 mbufs and make chain in advance because 416 * it is more convenient when inserting the hop-by-hop option later. 417 */ 418 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 419 if (mh == NULL) 420 return; 421 MGET(md, M_DONTWAIT, MT_DATA); 422 if (md == NULL) { 423 m_free(mh); 424 return; 425 } 426 mh->m_next = md; 427 428 mh->m_pkthdr.rcvif = NULL; 429 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr); 430 mh->m_len = sizeof(struct ip6_hdr); 431 MH_ALIGN(mh, sizeof(struct ip6_hdr)); 432 433 /* fill in the ip6 header */ 434 ip6 = mtod(mh, struct ip6_hdr *); 435 ip6->ip6_flow = 0; 436 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 437 ip6->ip6_vfc |= IPV6_VERSION; 438 /* ip6_plen will be set later */ 439 ip6->ip6_nxt = IPPROTO_ICMPV6; 440 /* ip6_hlim will be set by im6o.im6o_multicast_hlim */ 441 ip6->ip6_src = ia->ia_addr.sin6_addr; 442 ip6->ip6_dst = dst ? *dst : in6m->in6m_addr; 443 444 /* fill in the MLD header */ 445 md->m_len = sizeof(struct mld_hdr); 446 mldh = mtod(md, struct mld_hdr *); 447 mldh->mld_type = type; 448 mldh->mld_code = 0; 449 mldh->mld_cksum = 0; 450 /* XXX: we assume the function will not be called for query messages */ 451 mldh->mld_maxdelay = 0; 452 mldh->mld_reserved = 0; 453 mldh->mld_addr = in6m->in6m_addr; 454 in6_clearscope(&mldh->mld_addr); /* XXX */ 455 mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), 456 sizeof(struct mld_hdr)); 457 458 /* construct multicast option */ 459 bzero(&im6o, sizeof(im6o)); 460 im6o.im6o_multicast_ifp = ifp; 461 im6o.im6o_multicast_hlim = 1; 462 463 /* 464 * Request loopback of the report if we are acting as a multicast 465 * router, so that the process-level routing daemon can hear it. 466 */ 467 im6o.im6o_multicast_loop = (ip6_mrouter != NULL); 468 469 /* increment output statictics */ 470 icmp6stat.icp6s_outhist[type]++; 471 472 ip6_output(mh, &ip6_opts, NULL, 0, &im6o, &outif, NULL); 473 if (outif) { 474 icmp6_ifstat_inc(outif, ifs6_out_msg); 475 switch (type) { 476 case MLD_LISTENER_QUERY: 477 icmp6_ifstat_inc(outif, ifs6_out_mldquery); 478 break; 479 case MLD_LISTENER_REPORT: 480 icmp6_ifstat_inc(outif, ifs6_out_mldreport); 481 break; 482 case MLD_LISTENER_DONE: 483 icmp6_ifstat_inc(outif, ifs6_out_mlddone); 484 break; 485 } 486 } 487 } 488 489 /* 490 * Add an address to the list of IP6 multicast addresses for a given interface. 491 * Add source addresses to the list also, if upstream router is MLDv2 capable 492 * and the number of source is not 0. 493 */ 494 struct in6_multi * 495 in6_addmulti(maddr6, ifp, errorp) 496 struct in6_addr *maddr6; 497 struct ifnet *ifp; 498 int *errorp; 499 { 500 struct in6_multi *in6m; 501 struct ifmultiaddr *ifma; 502 struct sockaddr_in6 sa6; 503 int s = splnet(); 504 505 *errorp = 0; 506 507 /* 508 * Call generic routine to add membership or increment 509 * refcount. It wants addresses in the form of a sockaddr, 510 * so we build one here (being careful to zero the unused bytes). 511 */ 512 bzero(&sa6, sizeof(sa6)); 513 sa6.sin6_family = AF_INET6; 514 sa6.sin6_len = sizeof(struct sockaddr_in6); 515 sa6.sin6_addr = *maddr6; 516 *errorp = if_addmulti(ifp, (struct sockaddr *)&sa6, &ifma); 517 if (*errorp) { 518 splx(s); 519 return 0; 520 } 521 522 /* 523 * If ifma->ifma_protospec is null, then if_addmulti() created 524 * a new record. Otherwise, we are done. 525 */ 526 if (ifma->ifma_protospec != NULL) { 527 splx(s); 528 return ifma->ifma_protospec; 529 } 530 531 /* XXX - if_addmulti uses M_WAITOK. Can this really be called 532 at interrupt time? If so, need to fix if_addmulti. XXX */ 533 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IP6MADDR, M_NOWAIT); 534 if (in6m == NULL) { 535 splx(s); 536 return (NULL); 537 } 538 539 bzero(in6m, sizeof *in6m); 540 in6m->in6m_addr = *maddr6; 541 in6m->in6m_ifp = ifp; 542 in6m->in6m_refcount = 1; 543 in6m->in6m_ifma = ifma; 544 ifma->ifma_protospec = in6m; 545 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry); 546 547 /* 548 * Let MLD6 know that we have joined a new IPv6 multicast 549 * group. 550 */ 551 mld6_start_listening(in6m); 552 splx(s); 553 return (in6m); 554 } 555 556 /* 557 * Delete a multicast address record. 558 */ 559 void 560 in6_delmulti(in6m) 561 struct in6_multi *in6m; 562 { 563 struct ifmultiaddr *ifma = in6m->in6m_ifma; 564 int s = splnet(); 565 566 if (ifma->ifma_refcount == 1) { 567 /* 568 * No remaining claims to this record; let MLD6 know 569 * that we are leaving the multicast group. 570 */ 571 mld6_stop_listening(in6m); 572 ifma->ifma_protospec = NULL; 573 LIST_REMOVE(in6m, in6m_entry); 574 free(in6m, M_IP6MADDR); 575 } 576 /* XXX - should be separate API for when we have an ifma? */ 577 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 578 splx(s); 579 } 580