1 /* $OpenBSD: mld6.c,v 1.38 2014/04/14 09:06:42 mpi Exp $ */ 2 /* $KAME: mld6.c,v 1.26 2001/02/16 14:50:35 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 * 3. 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 <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/mbuf.h> 71 #include <sys/socket.h> 72 #include <sys/protosw.h> 73 #include <sys/syslog.h> 74 #include <dev/rndvar.h> 75 76 #include <net/if.h> 77 #include <net/if_var.h> 78 79 #include <netinet/in.h> 80 #include <netinet6/in6_var.h> 81 #include <netinet/ip6.h> 82 #include <netinet6/ip6_var.h> 83 #include <netinet/icmp6.h> 84 #include <netinet6/mld6.h> 85 #include <netinet6/mld6_var.h> 86 87 static struct ip6_pktopts ip6_opts; 88 static int mld_timers_are_running; 89 /* XXX: These are necessary for KAME's link-local hack */ 90 static struct in6_addr mld_all_nodes_linklocal = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 91 static struct in6_addr mld_all_routers_linklocal = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 92 93 void mld6_checktimer(struct ifnet *); 94 static void mld6_sendpkt(struct in6_multi *, int, const struct in6_addr *); 95 96 void 97 mld6_init() 98 { 99 static u_int8_t hbh_buf[8]; 100 struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf; 101 u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD); 102 103 mld_timers_are_running = 0; 104 105 /* ip6h_nxt will be fill in later */ 106 hbh->ip6h_len = 0; /* (8 >> 3) - 1 */ 107 108 /* XXX: grotty hard coding... */ 109 hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */ 110 hbh_buf[3] = 0; 111 hbh_buf[4] = IP6OPT_ROUTER_ALERT; 112 hbh_buf[5] = IP6OPT_RTALERT_LEN - 2; 113 bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t)); 114 115 ip6_opts.ip6po_hbh = hbh; 116 } 117 118 void 119 mld6_start_listening(struct in6_multi *in6m) 120 { 121 int s = splsoftnet(); 122 123 /* 124 * RFC2710 page 10: 125 * The node never sends a Report or Done for the link-scope all-nodes 126 * address. 127 * MLD messages are never sent for multicast addresses whose scope is 0 128 * (reserved) or 1 (node-local). 129 */ 130 mld_all_nodes_linklocal.s6_addr16[1] = htons(in6m->in6m_ifidx);/* XXX */ 131 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld_all_nodes_linklocal) || 132 __IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < __IPV6_ADDR_SCOPE_LINKLOCAL) { 133 in6m->in6m_timer = 0; 134 in6m->in6m_state = MLD_OTHERLISTENER; 135 } else { 136 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL); 137 in6m->in6m_timer = 138 MLD_RANDOM_DELAY(MLD_V1_MAX_RI * 139 PR_FASTHZ); 140 in6m->in6m_state = MLD_IREPORTEDLAST; 141 mld_timers_are_running = 1; 142 } 143 splx(s); 144 } 145 146 void 147 mld6_stop_listening(struct in6_multi *in6m) 148 { 149 int s = splsoftnet(); 150 151 mld_all_nodes_linklocal.s6_addr16[1] = htons(in6m->in6m_ifidx);/* XXX */ 152 mld_all_routers_linklocal.s6_addr16[1] = 153 htons(in6m->in6m_ifidx); /* XXX: necessary when mrouting */ 154 155 if (in6m->in6m_state == MLD_IREPORTEDLAST && 156 (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld_all_nodes_linklocal)) && 157 __IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > __IPV6_ADDR_SCOPE_INTFACELOCAL) 158 mld6_sendpkt(in6m, MLD_LISTENER_DONE, 159 &mld_all_routers_linklocal); 160 splx(s); 161 } 162 163 void 164 mld6_input(struct mbuf *m, int off) 165 { 166 struct ip6_hdr *ip6; 167 struct mld_hdr *mldh; 168 struct ifnet *ifp = m->m_pkthdr.rcvif; 169 struct in6_multi *in6m; 170 struct ifmaddr *ifma; 171 int timer; /* timer value in the MLD query header */ 172 173 IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh)); 174 if (mldh == NULL) { 175 icmp6stat.icp6s_tooshort++; 176 return; 177 } 178 179 /* source address validation */ 180 ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */ 181 if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) { 182 #if 0 183 char src[INET6_ADDRSTRLEN], grp[INET6_ADDRSTRLEN]; 184 185 log(LOG_ERR, 186 "mld_input: src %s is not link-local (grp=%s)\n", 187 inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src)), 188 inet_ntop(AF_INET6, &mldh->mld_addr, grp, sizeof(grp))); 189 #endif 190 /* 191 * spec (RFC2710) does not explicitly 192 * specify to discard the packet from a non link-local 193 * source address. But we believe it's expected to do so. 194 */ 195 m_freem(m); 196 return; 197 } 198 199 /* 200 * In the MLD6 specification, there are 3 states and a flag. 201 * 202 * In Non-Listener state, we simply don't have a membership record. 203 * In Delaying Listener state, our timer is running (in6m->in6m_timer) 204 * In Idle Listener state, our timer is not running (in6m->in6m_timer==0) 205 * 206 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if 207 * we have heard a report from another member, or MLD_IREPORTEDLAST 208 * if we sent the last report. 209 */ 210 switch(mldh->mld_type) { 211 case MLD_LISTENER_QUERY: 212 if (ifp->if_flags & IFF_LOOPBACK) 213 break; 214 215 if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) && 216 !IN6_IS_ADDR_MULTICAST(&mldh->mld_addr)) 217 break; /* print error or log stat? */ 218 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr)) 219 mldh->mld_addr.s6_addr16[1] = 220 htons(ifp->if_index); /* XXX */ 221 222 /* 223 * - Start the timers in all of our membership records 224 * that the query applies to for the interface on 225 * which the query arrived excl. those that belong 226 * to the "all-nodes" group (ff02::1). 227 * - Restart any timer that is already running but has 228 * A value longer than the requested timeout. 229 * - Use the value specified in the query message as 230 * the maximum timeout. 231 */ 232 233 /* 234 * XXX: System timer resolution is too low to handle Max 235 * Response Delay, so set 1 to the internal timer even if 236 * the calculated value equals to zero when Max Response 237 * Delay is positive. 238 */ 239 timer = ntohs(mldh->mld_maxdelay)*PR_FASTHZ/MLD_TIMER_SCALE; 240 if (timer == 0 && mldh->mld_maxdelay) 241 timer = 1; 242 mld_all_nodes_linklocal.s6_addr16[1] = 243 htons(ifp->if_index); /* XXX */ 244 245 TAILQ_FOREACH(ifma, &ifp->if_maddrlist, ifma_list) { 246 if (ifma->ifma_addr->sa_family != AF_INET6) 247 continue; 248 in6m = ifmatoin6m(ifma); 249 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, 250 &mld_all_nodes_linklocal) || 251 __IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < 252 __IPV6_ADDR_SCOPE_LINKLOCAL) 253 continue; 254 255 if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) || 256 IN6_ARE_ADDR_EQUAL(&mldh->mld_addr, 257 &in6m->in6m_addr)) 258 { 259 if (timer == 0) { 260 /* send a report immediately */ 261 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, 262 NULL); 263 in6m->in6m_timer = 0; /* reset timer */ 264 in6m->in6m_state = MLD_IREPORTEDLAST; 265 } else if (in6m->in6m_timer == 0 || /* idle */ 266 in6m->in6m_timer > timer) { 267 in6m->in6m_timer = 268 MLD_RANDOM_DELAY(timer); 269 mld_timers_are_running = 1; 270 } 271 } 272 } 273 274 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr)) 275 mldh->mld_addr.s6_addr16[1] = 0; /* XXX */ 276 break; 277 case MLD_LISTENER_REPORT: 278 /* 279 * For fast leave to work, we have to know that we are the 280 * last person to send a report for this group. Reports 281 * can potentially get looped back if we are a multicast 282 * router, so discard reports sourced by me. 283 * Note that it is impossible to check IFF_LOOPBACK flag of 284 * ifp for this purpose, since ip6_mloopback pass the physical 285 * interface to looutput. 286 */ 287 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */ 288 break; 289 290 if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr)) 291 break; 292 293 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr)) 294 mldh->mld_addr.s6_addr16[1] = 295 htons(ifp->if_index); /* XXX */ 296 /* 297 * If we belong to the group being reported, stop 298 * our timer for that group. 299 */ 300 IN6_LOOKUP_MULTI(mldh->mld_addr, ifp, in6m); 301 if (in6m) { 302 in6m->in6m_timer = 0; /* transit to idle state */ 303 in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */ 304 } 305 306 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr)) 307 mldh->mld_addr.s6_addr16[1] = 0; /* XXX */ 308 break; 309 default: /* this is impossible */ 310 #if 0 311 /* 312 * this case should be impossible because of filtering in 313 * icmp6_input(). But we explicitly disabled this part 314 * just in case. 315 */ 316 log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type); 317 #endif 318 break; 319 } 320 321 m_freem(m); 322 } 323 324 void 325 mld6_fasttimeo(void) 326 { 327 struct ifnet *ifp; 328 int s; 329 330 /* 331 * Quick check to see if any work needs to be done, in order 332 * to minimize the overhead of fasttimo processing. 333 */ 334 if (!mld_timers_are_running) 335 return; 336 337 s = splsoftnet(); 338 mld_timers_are_running = 0; 339 TAILQ_FOREACH(ifp, &ifnet, if_list) 340 mld6_checktimer(ifp); 341 splx(s); 342 } 343 344 void 345 mld6_checktimer(struct ifnet *ifp) 346 { 347 struct in6_multi *in6m; 348 struct ifmaddr *ifma; 349 350 splsoftassert(IPL_SOFTNET); 351 352 TAILQ_FOREACH(ifma, &ifp->if_maddrlist, ifma_list) { 353 if (ifma->ifma_addr->sa_family != AF_INET6) 354 continue; 355 in6m = ifmatoin6m(ifma); 356 if (in6m->in6m_timer == 0) { 357 /* do nothing */ 358 } else if (--in6m->in6m_timer == 0) { 359 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL); 360 in6m->in6m_state = MLD_IREPORTEDLAST; 361 } else { 362 mld_timers_are_running = 1; 363 } 364 } 365 } 366 367 static void 368 mld6_sendpkt(struct in6_multi *in6m, int type, const struct in6_addr *dst) 369 { 370 struct mbuf *mh, *md; 371 struct mld_hdr *mldh; 372 struct ip6_hdr *ip6; 373 struct ip6_moptions im6o; 374 struct in6_ifaddr *ia6; 375 struct ifnet *ifp; 376 int ignflags; 377 378 ifp = if_get(in6m->in6m_ifidx); 379 if (ifp == NULL) 380 return; 381 382 /* 383 * At first, find a link local address on the outgoing interface 384 * to use as the source address of the MLD packet. 385 * We do not reject tentative addresses for MLD report to deal with 386 * the case where we first join a link-local address. 387 */ 388 ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE; 389 if ((ia6 = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL) 390 return; 391 if ((ia6->ia6_flags & IN6_IFF_TENTATIVE)) 392 ia6 = NULL; 393 394 /* 395 * Allocate mbufs to store ip6 header and MLD header. 396 * We allocate 2 mbufs and make chain in advance because 397 * it is more convenient when inserting the hop-by-hop option later. 398 */ 399 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 400 if (mh == NULL) 401 return; 402 MGET(md, M_DONTWAIT, MT_DATA); 403 if (md == NULL) { 404 m_free(mh); 405 return; 406 } 407 mh->m_next = md; 408 409 mh->m_pkthdr.rcvif = NULL; 410 mh->m_pkthdr.ph_rtableid = ifp->if_rdomain; 411 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr); 412 mh->m_len = sizeof(struct ip6_hdr); 413 MH_ALIGN(mh, sizeof(struct ip6_hdr)); 414 415 /* fill in the ip6 header */ 416 ip6 = mtod(mh, struct ip6_hdr *); 417 ip6->ip6_flow = 0; 418 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 419 ip6->ip6_vfc |= IPV6_VERSION; 420 /* ip6_plen will be set later */ 421 ip6->ip6_nxt = IPPROTO_ICMPV6; 422 /* ip6_hlim will be set by im6o.im6o_multicast_hlim */ 423 ip6->ip6_src = ia6 ? ia6->ia_addr.sin6_addr : in6addr_any; 424 ip6->ip6_dst = dst ? *dst : in6m->in6m_addr; 425 426 /* fill in the MLD header */ 427 md->m_len = sizeof(struct mld_hdr); 428 mldh = mtod(md, struct mld_hdr *); 429 mldh->mld_type = type; 430 mldh->mld_code = 0; 431 mldh->mld_cksum = 0; 432 /* XXX: we assume the function will not be called for query messages */ 433 mldh->mld_maxdelay = 0; 434 mldh->mld_reserved = 0; 435 mldh->mld_addr = in6m->in6m_addr; 436 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr)) 437 mldh->mld_addr.s6_addr16[1] = 0; /* XXX */ 438 mh->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT; 439 440 /* construct multicast option */ 441 bzero(&im6o, sizeof(im6o)); 442 im6o.im6o_multicast_ifp = ifp; 443 im6o.im6o_multicast_hlim = 1; 444 445 /* 446 * Request loopback of the report if we are acting as a multicast 447 * router, so that the process-level routing daemon can hear it. 448 */ 449 #ifdef MROUTING 450 im6o.im6o_multicast_loop = (ip6_mrouter != NULL); 451 #endif 452 453 /* increment output statictics */ 454 icmp6stat.icp6s_outhist[type]++; 455 icmp6_ifstat_inc(ifp, ifs6_out_msg); 456 switch (type) { 457 case MLD_LISTENER_QUERY: 458 icmp6_ifstat_inc(ifp, ifs6_out_mldquery); 459 break; 460 case MLD_LISTENER_REPORT: 461 icmp6_ifstat_inc(ifp, ifs6_out_mldreport); 462 break; 463 case MLD_LISTENER_DONE: 464 icmp6_ifstat_inc(ifp, ifs6_out_mlddone); 465 break; 466 } 467 468 ip6_output(mh, &ip6_opts, NULL, ia6 ? 0 : IPV6_UNSPECSRC, &im6o, NULL, 469 NULL); 470 } 471