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