1 /* $FreeBSD$ */ 2 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 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) 1982, 1986, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 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 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 66 */ 67 68 #include "opt_inet.h" 69 #include "opt_inet6.h" 70 #include "opt_ipsec.h" 71 72 #include <sys/param.h> 73 #include <sys/domain.h> 74 #include <sys/kernel.h> 75 #include <sys/lock.h> 76 #include <sys/malloc.h> 77 #include <sys/mbuf.h> 78 #include <sys/protosw.h> 79 #include <sys/signalvar.h> 80 #include <sys/socket.h> 81 #include <sys/socketvar.h> 82 #include <sys/sx.h> 83 #include <sys/syslog.h> 84 #include <sys/systm.h> 85 #include <sys/time.h> 86 87 #include <net/if.h> 88 #include <net/if_dl.h> 89 #include <net/if_types.h> 90 #include <net/route.h> 91 92 #include <netinet/in.h> 93 #include <netinet/in_pcb.h> 94 #include <netinet/in_var.h> 95 #include <netinet/ip6.h> 96 #include <netinet/icmp6.h> 97 #include <netinet6/in6_ifattach.h> 98 #include <netinet6/in6_pcb.h> 99 #include <netinet6/ip6protosw.h> 100 #include <netinet6/ip6_var.h> 101 #include <netinet6/mld6_var.h> 102 #include <netinet6/nd6.h> 103 104 #ifdef IPSEC 105 #include <netinet6/ipsec.h> 106 #include <netkey/key.h> 107 #endif 108 109 #ifdef FAST_IPSEC 110 #include <netipsec/ipsec.h> 111 #include <netipsec/key.h> 112 #endif 113 114 #include <net/net_osdep.h> 115 116 extern struct domain inet6domain; 117 118 struct icmp6stat icmp6stat; 119 120 extern struct inpcbhead ripcb; 121 extern int icmp6errppslim; 122 static int icmp6errpps_count = 0; 123 static struct timeval icmp6errppslim_last; 124 extern int icmp6_nodeinfo; 125 126 static void icmp6_errcount __P((struct icmp6errstat *, int, int)); 127 static int icmp6_rip6_input __P((struct mbuf **, int)); 128 static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); 129 static const char *icmp6_redirect_diag __P((struct in6_addr *, 130 struct in6_addr *, struct in6_addr *)); 131 static struct mbuf *ni6_input __P((struct mbuf *, int)); 132 static struct mbuf *ni6_nametodns __P((const char *, int, int)); 133 static int ni6_dnsmatch __P((const char *, int, const char *, int)); 134 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 135 struct ifnet **, char *)); 136 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 137 struct ifnet *, int)); 138 static int icmp6_notify_error __P((struct mbuf *, int, int, int)); 139 140 #ifdef COMPAT_RFC1885 141 static struct route_in6 icmp6_reflect_rt; 142 #endif 143 144 145 void 146 icmp6_init() 147 { 148 mld6_init(); 149 } 150 151 static void 152 icmp6_errcount(stat, type, code) 153 struct icmp6errstat *stat; 154 int type, code; 155 { 156 switch (type) { 157 case ICMP6_DST_UNREACH: 158 switch (code) { 159 case ICMP6_DST_UNREACH_NOROUTE: 160 stat->icp6errs_dst_unreach_noroute++; 161 return; 162 case ICMP6_DST_UNREACH_ADMIN: 163 stat->icp6errs_dst_unreach_admin++; 164 return; 165 case ICMP6_DST_UNREACH_BEYONDSCOPE: 166 stat->icp6errs_dst_unreach_beyondscope++; 167 return; 168 case ICMP6_DST_UNREACH_ADDR: 169 stat->icp6errs_dst_unreach_addr++; 170 return; 171 case ICMP6_DST_UNREACH_NOPORT: 172 stat->icp6errs_dst_unreach_noport++; 173 return; 174 } 175 break; 176 case ICMP6_PACKET_TOO_BIG: 177 stat->icp6errs_packet_too_big++; 178 return; 179 case ICMP6_TIME_EXCEEDED: 180 switch (code) { 181 case ICMP6_TIME_EXCEED_TRANSIT: 182 stat->icp6errs_time_exceed_transit++; 183 return; 184 case ICMP6_TIME_EXCEED_REASSEMBLY: 185 stat->icp6errs_time_exceed_reassembly++; 186 return; 187 } 188 break; 189 case ICMP6_PARAM_PROB: 190 switch (code) { 191 case ICMP6_PARAMPROB_HEADER: 192 stat->icp6errs_paramprob_header++; 193 return; 194 case ICMP6_PARAMPROB_NEXTHEADER: 195 stat->icp6errs_paramprob_nextheader++; 196 return; 197 case ICMP6_PARAMPROB_OPTION: 198 stat->icp6errs_paramprob_option++; 199 return; 200 } 201 break; 202 case ND_REDIRECT: 203 stat->icp6errs_redirect++; 204 return; 205 } 206 stat->icp6errs_unknown++; 207 } 208 209 /* 210 * Generate an error packet of type error in response to bad IP6 packet. 211 */ 212 void 213 icmp6_error(m, type, code, param) 214 struct mbuf *m; 215 int type, code, param; 216 { 217 struct ip6_hdr *oip6, *nip6; 218 struct icmp6_hdr *icmp6; 219 u_int preplen; 220 int off; 221 int nxt; 222 223 icmp6stat.icp6s_error++; 224 225 /* count per-type-code statistics */ 226 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 227 228 #ifdef M_DECRYPTED /*not openbsd*/ 229 if (m->m_flags & M_DECRYPTED) { 230 icmp6stat.icp6s_canterror++; 231 goto freeit; 232 } 233 #endif 234 235 #ifndef PULLDOWN_TEST 236 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 237 #else 238 if (m->m_len < sizeof(struct ip6_hdr)) { 239 m = m_pullup(m, sizeof(struct ip6_hdr)); 240 if (m == NULL) 241 return; 242 } 243 #endif 244 oip6 = mtod(m, struct ip6_hdr *); 245 246 /* 247 * If the destination address of the erroneous packet is a multicast 248 * address, or the packet was sent using link-layer multicast, 249 * we should basically suppress sending an error (RFC 2463, Section 250 * 2.4). 251 * We have two exceptions (the item e.2 in that section): 252 * - the Pakcet Too Big message can be sent for path MTU discovery. 253 * - the Parameter Problem Message that can be allowed an icmp6 error 254 * in the option type field. This check has been done in 255 * ip6_unknown_opt(), so we can just check the type and code. 256 */ 257 if ((m->m_flags & (M_BCAST|M_MCAST) || 258 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 259 (type != ICMP6_PACKET_TOO_BIG && 260 (type != ICMP6_PARAM_PROB || 261 code != ICMP6_PARAMPROB_OPTION))) 262 goto freeit; 263 264 /* 265 * RFC 2463, 2.4 (e.5): source address check. 266 * XXX: the case of anycast source? 267 */ 268 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 269 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 270 goto freeit; 271 272 /* 273 * If we are about to send ICMPv6 against ICMPv6 error/redirect, 274 * don't do it. 275 */ 276 nxt = -1; 277 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 278 if (off >= 0 && nxt == IPPROTO_ICMPV6) { 279 struct icmp6_hdr *icp; 280 281 #ifndef PULLDOWN_TEST 282 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 283 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 284 #else 285 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 286 sizeof(*icp)); 287 if (icp == NULL) { 288 icmp6stat.icp6s_tooshort++; 289 return; 290 } 291 #endif 292 if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 293 icp->icmp6_type == ND_REDIRECT) { 294 /* 295 * ICMPv6 error 296 * Special case: for redirect (which is 297 * informational) we must not send icmp6 error. 298 */ 299 icmp6stat.icp6s_canterror++; 300 goto freeit; 301 } else { 302 /* ICMPv6 informational - send the error */ 303 } 304 } else { 305 /* non-ICMPv6 - send the error */ 306 } 307 308 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 309 310 /* Finally, do rate limitation check. */ 311 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 312 icmp6stat.icp6s_toofreq++; 313 goto freeit; 314 } 315 316 /* 317 * OK, ICMP6 can be generated. 318 */ 319 320 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 321 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 322 323 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 324 M_PREPEND(m, preplen, M_DONTWAIT); 325 if (m && m->m_len < preplen) 326 m = m_pullup(m, preplen); 327 if (m == NULL) { 328 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 329 return; 330 } 331 332 nip6 = mtod(m, struct ip6_hdr *); 333 nip6->ip6_src = oip6->ip6_src; 334 nip6->ip6_dst = oip6->ip6_dst; 335 336 in6_clearscope(&oip6->ip6_src); 337 in6_clearscope(&oip6->ip6_dst); 338 339 icmp6 = (struct icmp6_hdr *)(nip6 + 1); 340 icmp6->icmp6_type = type; 341 icmp6->icmp6_code = code; 342 icmp6->icmp6_pptr = htonl((u_int32_t)param); 343 344 /* 345 * icmp6_reflect() is designed to be in the input path. 346 * icmp6_error() can be called from both input and outut path, 347 * and if we are in output path rcvif could contain bogus value. 348 * clear m->m_pkthdr.rcvif for safety, we should have enough scope 349 * information in ip header (nip6). 350 */ 351 m->m_pkthdr.rcvif = NULL; 352 353 icmp6stat.icp6s_outhist[type]++; 354 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 355 356 return; 357 358 freeit: 359 /* 360 * If we can't tell wheter or not we can generate ICMP6, free it. 361 */ 362 m_freem(m); 363 } 364 365 /* 366 * Process a received ICMP6 message. 367 */ 368 int 369 icmp6_input(mp, offp, proto) 370 struct mbuf **mp; 371 int *offp, proto; 372 { 373 struct mbuf *m = *mp, *n; 374 struct ip6_hdr *ip6, *nip6; 375 struct icmp6_hdr *icmp6, *nicmp6; 376 int off = *offp; 377 int icmp6len = m->m_pkthdr.len - *offp; 378 int code, sum, noff; 379 380 #ifndef PULLDOWN_TEST 381 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 382 /* m might change if M_LOOP. So, call mtod after this */ 383 #endif 384 385 /* 386 * Locate icmp6 structure in mbuf, and check 387 * that not corrupted and of at least minimum length 388 */ 389 390 ip6 = mtod(m, struct ip6_hdr *); 391 if (icmp6len < sizeof(struct icmp6_hdr)) { 392 icmp6stat.icp6s_tooshort++; 393 goto freeit; 394 } 395 396 /* 397 * calculate the checksum 398 */ 399 #ifndef PULLDOWN_TEST 400 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 401 #else 402 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 403 if (icmp6 == NULL) { 404 icmp6stat.icp6s_tooshort++; 405 return IPPROTO_DONE; 406 } 407 #endif 408 code = icmp6->icmp6_code; 409 410 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 411 nd6log((LOG_ERR, 412 "ICMP6 checksum error(%d|%x) %s\n", 413 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); 414 icmp6stat.icp6s_checksum++; 415 goto freeit; 416 } 417 418 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 419 /* 420 * Deliver very specific ICMP6 type only. 421 * This is important to deilver TOOBIG. Otherwise PMTUD 422 * will not work. 423 */ 424 switch (icmp6->icmp6_type) { 425 case ICMP6_DST_UNREACH: 426 case ICMP6_PACKET_TOO_BIG: 427 case ICMP6_TIME_EXCEEDED: 428 break; 429 default: 430 goto freeit; 431 } 432 } 433 434 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 435 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 436 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 437 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 438 439 switch (icmp6->icmp6_type) { 440 case ICMP6_DST_UNREACH: 441 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 442 switch (code) { 443 case ICMP6_DST_UNREACH_NOROUTE: 444 code = PRC_UNREACH_NET; 445 break; 446 case ICMP6_DST_UNREACH_ADMIN: 447 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 448 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 449 break; 450 case ICMP6_DST_UNREACH_ADDR: 451 code = PRC_HOSTDEAD; 452 break; 453 #ifdef COMPAT_RFC1885 454 case ICMP6_DST_UNREACH_NOTNEIGHBOR: 455 code = PRC_UNREACH_SRCFAIL; 456 break; 457 #else 458 case ICMP6_DST_UNREACH_BEYONDSCOPE: 459 /* I mean "source address was incorrect." */ 460 code = PRC_PARAMPROB; 461 break; 462 #endif 463 case ICMP6_DST_UNREACH_NOPORT: 464 code = PRC_UNREACH_PORT; 465 break; 466 default: 467 goto badcode; 468 } 469 goto deliver; 470 break; 471 472 case ICMP6_PACKET_TOO_BIG: 473 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 474 if (code != 0) 475 goto badcode; 476 477 code = PRC_MSGSIZE; 478 479 /* 480 * Updating the path MTU will be done after examining 481 * intermediate extension headers. 482 */ 483 goto deliver; 484 break; 485 486 case ICMP6_TIME_EXCEEDED: 487 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 488 switch (code) { 489 case ICMP6_TIME_EXCEED_TRANSIT: 490 case ICMP6_TIME_EXCEED_REASSEMBLY: 491 code += PRC_TIMXCEED_INTRANS; 492 break; 493 default: 494 goto badcode; 495 } 496 goto deliver; 497 break; 498 499 case ICMP6_PARAM_PROB: 500 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 501 switch (code) { 502 case ICMP6_PARAMPROB_NEXTHEADER: 503 code = PRC_UNREACH_PROTOCOL; 504 break; 505 case ICMP6_PARAMPROB_HEADER: 506 case ICMP6_PARAMPROB_OPTION: 507 code = PRC_PARAMPROB; 508 break; 509 default: 510 goto badcode; 511 } 512 goto deliver; 513 break; 514 515 case ICMP6_ECHO_REQUEST: 516 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 517 if (code != 0) 518 goto badcode; 519 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 520 /* Give up remote */ 521 break; 522 } 523 if ((n->m_flags & M_EXT) != 0 524 || n->m_len < off + sizeof(struct icmp6_hdr)) { 525 struct mbuf *n0 = n; 526 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 527 int n0len; 528 529 /* 530 * Prepare an internal mbuf. m_pullup() doesn't 531 * always copy the length we specified. 532 */ 533 if (maxlen >= MCLBYTES) { 534 /* Give up remote */ 535 m_freem(n0); 536 break; 537 } 538 MGETHDR(n, M_DONTWAIT, n0->m_type); 539 n0len = n0->m_pkthdr.len; /* save for use below */ 540 if (n) 541 M_MOVE_PKTHDR(n, n0); 542 if (n && maxlen >= MHLEN) { 543 MCLGET(n, M_DONTWAIT); 544 if ((n->m_flags & M_EXT) == 0) { 545 m_free(n); 546 n = NULL; 547 } 548 } 549 if (n == NULL) { 550 /* Give up remote */ 551 m_freem(n0); 552 break; 553 } 554 /* 555 * Copy IPv6 and ICMPv6 only. 556 */ 557 nip6 = mtod(n, struct ip6_hdr *); 558 bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 559 nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 560 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 561 noff = sizeof(struct ip6_hdr); 562 /* new mbuf contains only ipv6+icmpv6 headers */ 563 n->m_len = noff + sizeof(struct icmp6_hdr); 564 /* 565 * Adjust mbuf. ip6_plen will be adjusted in 566 * ip6_output(). 567 */ 568 m_adj(n0, off + sizeof(struct icmp6_hdr)); 569 /* recalculate complete packet size */ 570 n->m_pkthdr.len = n0len + (noff - off); 571 n->m_next = n0; 572 } else { 573 nip6 = mtod(n, struct ip6_hdr *); 574 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); 575 noff = off; 576 } 577 nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 578 nicmp6->icmp6_code = 0; 579 if (n) { 580 icmp6stat.icp6s_reflect++; 581 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 582 icmp6_reflect(n, noff); 583 } 584 break; 585 586 case ICMP6_ECHO_REPLY: 587 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 588 if (code != 0) 589 goto badcode; 590 break; 591 592 case MLD_LISTENER_QUERY: 593 case MLD_LISTENER_REPORT: 594 if (icmp6len < sizeof(struct mld_hdr)) 595 goto badlen; 596 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 597 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 598 else 599 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 600 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 601 /* give up local */ 602 mld6_input(m, off); 603 m = NULL; 604 goto freeit; 605 } 606 mld6_input(n, off); 607 /* m stays. */ 608 break; 609 610 case MLD_LISTENER_DONE: 611 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 612 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 613 goto badlen; 614 break; /* nothing to be done in kernel */ 615 616 case MLD_MTRACE_RESP: 617 case MLD_MTRACE: 618 /* XXX: these two are experimental. not officially defined. */ 619 /* XXX: per-interface statistics? */ 620 break; /* just pass it to applications */ 621 622 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 623 { 624 enum { WRU, FQDN } mode; 625 626 if (!icmp6_nodeinfo) 627 break; 628 629 if (icmp6len == sizeof(struct icmp6_hdr) + 4) 630 mode = WRU; 631 else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 632 mode = FQDN; 633 else 634 goto badlen; 635 636 #define hostnamelen strlen(hostname) 637 if (mode == FQDN) { 638 #ifndef PULLDOWN_TEST 639 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 640 IPPROTO_DONE); 641 #endif 642 n = m_copy(m, 0, M_COPYALL); 643 if (n) 644 n = ni6_input(n, off); 645 /* XXX meaningless if n == NULL */ 646 noff = sizeof(struct ip6_hdr); 647 } else { 648 u_char *p; 649 int maxlen, maxhlen; 650 651 if ((icmp6_nodeinfo & 5) != 5) 652 break; 653 654 if (code != 0) 655 goto badcode; 656 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 657 if (maxlen >= MCLBYTES) { 658 /* Give up remote */ 659 break; 660 } 661 MGETHDR(n, M_DONTWAIT, m->m_type); 662 if (n && maxlen > MHLEN) { 663 MCLGET(n, M_DONTWAIT); 664 if ((n->m_flags & M_EXT) == 0) { 665 m_free(n); 666 n = NULL; 667 } 668 } 669 if (!m_dup_pkthdr(n, m, M_DONTWAIT)) { 670 /* 671 * Previous code did a blind M_COPY_PKTHDR 672 * and said "just for rcvif". If true, then 673 * we could tolerate the dup failing (due to 674 * the deep copy of the tag chain). For now 675 * be conservative and just fail. 676 */ 677 m_free(n); 678 n = NULL; 679 } 680 if (n == NULL) { 681 /* Give up remote */ 682 break; 683 } 684 n->m_pkthdr.rcvif = NULL; 685 n->m_len = 0; 686 maxhlen = M_TRAILINGSPACE(n) - maxlen; 687 if (maxhlen > hostnamelen) 688 maxhlen = hostnamelen; 689 /* 690 * Copy IPv6 and ICMPv6 only. 691 */ 692 nip6 = mtod(n, struct ip6_hdr *); 693 bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 694 nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 695 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 696 p = (u_char *)(nicmp6 + 1); 697 bzero(p, 4); 698 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 699 noff = sizeof(struct ip6_hdr); 700 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 701 sizeof(struct icmp6_hdr) + 4 + maxhlen; 702 nicmp6->icmp6_type = ICMP6_WRUREPLY; 703 nicmp6->icmp6_code = 0; 704 } 705 #undef hostnamelen 706 if (n) { 707 icmp6stat.icp6s_reflect++; 708 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 709 icmp6_reflect(n, noff); 710 } 711 break; 712 } 713 714 case ICMP6_WRUREPLY: 715 if (code != 0) 716 goto badcode; 717 break; 718 719 case ND_ROUTER_SOLICIT: 720 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 721 if (code != 0) 722 goto badcode; 723 if (icmp6len < sizeof(struct nd_router_solicit)) 724 goto badlen; 725 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 726 /* give up local */ 727 nd6_rs_input(m, off, icmp6len); 728 m = NULL; 729 goto freeit; 730 } 731 nd6_rs_input(n, off, icmp6len); 732 /* m stays. */ 733 break; 734 735 case ND_ROUTER_ADVERT: 736 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 737 if (code != 0) 738 goto badcode; 739 if (icmp6len < sizeof(struct nd_router_advert)) 740 goto badlen; 741 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 742 /* give up local */ 743 nd6_ra_input(m, off, icmp6len); 744 m = NULL; 745 goto freeit; 746 } 747 nd6_ra_input(n, off, icmp6len); 748 /* m stays. */ 749 break; 750 751 case ND_NEIGHBOR_SOLICIT: 752 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 753 if (code != 0) 754 goto badcode; 755 if (icmp6len < sizeof(struct nd_neighbor_solicit)) 756 goto badlen; 757 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 758 /* give up local */ 759 nd6_ns_input(m, off, icmp6len); 760 m = NULL; 761 goto freeit; 762 } 763 nd6_ns_input(n, off, icmp6len); 764 /* m stays. */ 765 break; 766 767 case ND_NEIGHBOR_ADVERT: 768 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 769 if (code != 0) 770 goto badcode; 771 if (icmp6len < sizeof(struct nd_neighbor_advert)) 772 goto badlen; 773 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 774 /* give up local */ 775 nd6_na_input(m, off, icmp6len); 776 m = NULL; 777 goto freeit; 778 } 779 nd6_na_input(n, off, icmp6len); 780 /* m stays. */ 781 break; 782 783 case ND_REDIRECT: 784 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 785 if (code != 0) 786 goto badcode; 787 if (icmp6len < sizeof(struct nd_redirect)) 788 goto badlen; 789 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 790 /* give up local */ 791 icmp6_redirect_input(m, off); 792 m = NULL; 793 goto freeit; 794 } 795 icmp6_redirect_input(n, off); 796 /* m stays. */ 797 break; 798 799 case ICMP6_ROUTER_RENUMBERING: 800 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 801 code != ICMP6_ROUTER_RENUMBERING_RESULT) 802 goto badcode; 803 if (icmp6len < sizeof(struct icmp6_router_renum)) 804 goto badlen; 805 break; 806 807 default: 808 nd6log((LOG_DEBUG, 809 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 810 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), 811 ip6_sprintf(&ip6->ip6_dst), 812 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 813 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 814 /* ICMPv6 error: MUST deliver it by spec... */ 815 code = PRC_NCMDS; 816 /* deliver */ 817 } else { 818 /* ICMPv6 informational: MUST not deliver */ 819 break; 820 } 821 deliver: 822 if (icmp6_notify_error(m, off, icmp6len, code)) { 823 /* In this case, m should've been freed. */ 824 return (IPPROTO_DONE); 825 } 826 break; 827 828 badcode: 829 icmp6stat.icp6s_badcode++; 830 break; 831 832 badlen: 833 icmp6stat.icp6s_badlen++; 834 break; 835 } 836 837 /* deliver the packet to appropriate sockets */ 838 icmp6_rip6_input(&m, *offp); 839 840 return IPPROTO_DONE; 841 842 freeit: 843 m_freem(m); 844 return IPPROTO_DONE; 845 } 846 847 static int 848 icmp6_notify_error(m, off, icmp6len, code) 849 struct mbuf *m; 850 int off, icmp6len, code; 851 { 852 struct icmp6_hdr *icmp6; 853 struct ip6_hdr *eip6; 854 u_int32_t notifymtu; 855 struct sockaddr_in6 icmp6src, icmp6dst; 856 857 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 858 icmp6stat.icp6s_tooshort++; 859 goto freeit; 860 } 861 #ifndef PULLDOWN_TEST 862 IP6_EXTHDR_CHECK(m, off, 863 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 864 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 865 #else 866 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 867 sizeof(*icmp6) + sizeof(struct ip6_hdr)); 868 if (icmp6 == NULL) { 869 icmp6stat.icp6s_tooshort++; 870 return (-1); 871 } 872 #endif 873 eip6 = (struct ip6_hdr *)(icmp6 + 1); 874 875 /* Detect the upper level protocol */ 876 { 877 void (*ctlfunc) __P((int, struct sockaddr *, void *)); 878 u_int8_t nxt = eip6->ip6_nxt; 879 int eoff = off + sizeof(struct icmp6_hdr) + 880 sizeof(struct ip6_hdr); 881 struct ip6ctlparam ip6cp; 882 struct in6_addr *finaldst = NULL; 883 int icmp6type = icmp6->icmp6_type; 884 struct ip6_frag *fh; 885 struct ip6_rthdr *rth; 886 struct ip6_rthdr0 *rth0; 887 int rthlen; 888 889 while (1) { /* XXX: should avoid infinite loop explicitly? */ 890 struct ip6_ext *eh; 891 892 switch (nxt) { 893 case IPPROTO_HOPOPTS: 894 case IPPROTO_DSTOPTS: 895 case IPPROTO_AH: 896 #ifndef PULLDOWN_TEST 897 IP6_EXTHDR_CHECK(m, 0, 898 eoff + sizeof(struct ip6_ext), -1); 899 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 900 #else 901 IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 902 eoff, sizeof(*eh)); 903 if (eh == NULL) { 904 icmp6stat.icp6s_tooshort++; 905 return (-1); 906 } 907 #endif 908 909 if (nxt == IPPROTO_AH) 910 eoff += (eh->ip6e_len + 2) << 2; 911 else 912 eoff += (eh->ip6e_len + 1) << 3; 913 nxt = eh->ip6e_nxt; 914 break; 915 case IPPROTO_ROUTING: 916 /* 917 * When the erroneous packet contains a 918 * routing header, we should examine the 919 * header to determine the final destination. 920 * Otherwise, we can't properly update 921 * information that depends on the final 922 * destination (e.g. path MTU). 923 */ 924 #ifndef PULLDOWN_TEST 925 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 926 rth = (struct ip6_rthdr *) 927 (mtod(m, caddr_t) + eoff); 928 #else 929 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 930 eoff, sizeof(*rth)); 931 if (rth == NULL) { 932 icmp6stat.icp6s_tooshort++; 933 return (-1); 934 } 935 #endif 936 rthlen = (rth->ip6r_len + 1) << 3; 937 /* 938 * XXX: currently there is no 939 * officially defined type other 940 * than type-0. 941 * Note that if the segment left field 942 * is 0, all intermediate hops must 943 * have been passed. 944 */ 945 if (rth->ip6r_segleft && 946 rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 947 int hops; 948 949 #ifndef PULLDOWN_TEST 950 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 951 rth0 = (struct ip6_rthdr0 *) 952 (mtod(m, caddr_t) + eoff); 953 #else 954 IP6_EXTHDR_GET(rth0, 955 struct ip6_rthdr0 *, m, 956 eoff, rthlen); 957 if (rth0 == NULL) { 958 icmp6stat.icp6s_tooshort++; 959 return (-1); 960 } 961 #endif 962 /* just ignore a bogus header */ 963 if ((rth0->ip6r0_len % 2) == 0 && 964 (hops = rth0->ip6r0_len/2)) 965 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 966 } 967 eoff += rthlen; 968 nxt = rth->ip6r_nxt; 969 break; 970 case IPPROTO_FRAGMENT: 971 #ifndef PULLDOWN_TEST 972 IP6_EXTHDR_CHECK(m, 0, eoff + 973 sizeof(struct ip6_frag), -1); 974 fh = (struct ip6_frag *)(mtod(m, caddr_t) + 975 eoff); 976 #else 977 IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 978 eoff, sizeof(*fh)); 979 if (fh == NULL) { 980 icmp6stat.icp6s_tooshort++; 981 return (-1); 982 } 983 #endif 984 /* 985 * Data after a fragment header is meaningless 986 * unless it is the first fragment, but 987 * we'll go to the notify label for path MTU 988 * discovery. 989 */ 990 if (fh->ip6f_offlg & IP6F_OFF_MASK) 991 goto notify; 992 993 eoff += sizeof(struct ip6_frag); 994 nxt = fh->ip6f_nxt; 995 break; 996 default: 997 /* 998 * This case includes ESP and the No Next 999 * Header. In such cases going to the notify 1000 * label does not have any meaning 1001 * (i.e. ctlfunc will be NULL), but we go 1002 * anyway since we might have to update 1003 * path MTU information. 1004 */ 1005 goto notify; 1006 } 1007 } 1008 notify: 1009 #ifndef PULLDOWN_TEST 1010 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 1011 #else 1012 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1013 sizeof(*icmp6) + sizeof(struct ip6_hdr)); 1014 if (icmp6 == NULL) { 1015 icmp6stat.icp6s_tooshort++; 1016 return (-1); 1017 } 1018 #endif 1019 1020 /* 1021 * retrieve parameters from the inner IPv6 header, and convert 1022 * them into sockaddr structures. 1023 * XXX: there is no guarantee that the source or destination 1024 * addresses of the inner packet are in the same scope as 1025 * the addresses of the icmp packet. But there is no other 1026 * way to determine the zone. 1027 */ 1028 eip6 = (struct ip6_hdr *)(icmp6 + 1); 1029 1030 bzero(&icmp6dst, sizeof(icmp6dst)); 1031 icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 1032 icmp6dst.sin6_family = AF_INET6; 1033 if (finaldst == NULL) 1034 icmp6dst.sin6_addr = eip6->ip6_dst; 1035 else 1036 icmp6dst.sin6_addr = *finaldst; 1037 if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6dst.sin6_addr, 1038 &icmp6dst.sin6_scope_id)) 1039 goto freeit; 1040 if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst, 1041 NULL, NULL)) { 1042 /* should be impossbile */ 1043 nd6log((LOG_DEBUG, 1044 "icmp6_notify_error: in6_embedscope failed\n")); 1045 goto freeit; 1046 } 1047 1048 /* 1049 * retrieve parameters from the inner IPv6 header, and convert 1050 * them into sockaddr structures. 1051 */ 1052 bzero(&icmp6src, sizeof(icmp6src)); 1053 icmp6src.sin6_len = sizeof(struct sockaddr_in6); 1054 icmp6src.sin6_family = AF_INET6; 1055 icmp6src.sin6_addr = eip6->ip6_src; 1056 if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6src.sin6_addr, 1057 &icmp6src.sin6_scope_id)) { 1058 goto freeit; 1059 } 1060 if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src, 1061 NULL, NULL)) { 1062 /* should be impossbile */ 1063 nd6log((LOG_DEBUG, 1064 "icmp6_notify_error: in6_embedscope failed\n")); 1065 goto freeit; 1066 } 1067 icmp6src.sin6_flowinfo = (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 1068 1069 if (finaldst == NULL) 1070 finaldst = &eip6->ip6_dst; 1071 ip6cp.ip6c_m = m; 1072 ip6cp.ip6c_icmp6 = icmp6; 1073 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 1074 ip6cp.ip6c_off = eoff; 1075 ip6cp.ip6c_finaldst = finaldst; 1076 ip6cp.ip6c_src = &icmp6src; 1077 ip6cp.ip6c_nxt = nxt; 1078 1079 if (icmp6type == ICMP6_PACKET_TOO_BIG) { 1080 notifymtu = ntohl(icmp6->icmp6_mtu); 1081 ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 1082 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 1083 } 1084 1085 ctlfunc = (void (*) __P((int, struct sockaddr *, void *))) 1086 (inet6sw[ip6_protox[nxt]].pr_ctlinput); 1087 if (ctlfunc) { 1088 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1089 &ip6cp); 1090 } 1091 } 1092 return (0); 1093 1094 freeit: 1095 m_freem(m); 1096 return (-1); 1097 } 1098 1099 void 1100 icmp6_mtudisc_update(ip6cp, validated) 1101 struct ip6ctlparam *ip6cp; 1102 int validated; 1103 { 1104 struct in6_addr *dst = ip6cp->ip6c_finaldst; 1105 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 1106 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 1107 u_int mtu = ntohl(icmp6->icmp6_mtu); 1108 struct rtentry *rt = NULL; 1109 struct sockaddr_in6 sin6; 1110 1111 #if 0 1112 /* 1113 * RFC2460 section 5, last paragraph. 1114 * even though minimum link MTU for IPv6 is IPV6_MMTU, 1115 * we may see ICMPv6 too big with mtu < IPV6_MMTU 1116 * due to packet translator in the middle. 1117 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1118 * special handling. 1119 */ 1120 if (mtu < IPV6_MMTU) 1121 return; 1122 #endif 1123 1124 /* 1125 * we reject ICMPv6 too big with abnormally small value. 1126 * XXX what is the good definition of "abnormally small"? 1127 */ 1128 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1129 return; 1130 1131 if (!validated) 1132 return; 1133 1134 bzero(&sin6, sizeof(sin6)); 1135 sin6.sin6_family = PF_INET6; 1136 sin6.sin6_len = sizeof(struct sockaddr_in6); 1137 sin6.sin6_addr = *dst; 1138 /* XXX normally, this won't happen */ 1139 if (IN6_IS_ADDR_LINKLOCAL(dst)) { 1140 sin6.sin6_addr.s6_addr16[1] = 1141 htons(m->m_pkthdr.rcvif->if_index); 1142 } 1143 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */ 1144 rt = rtalloc1((struct sockaddr *)&sin6, 0, 1145 RTF_CLONING | RTF_PRCLONING); 1146 1147 if (rt && (rt->rt_flags & RTF_HOST) && 1148 !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 1149 if (mtu < IPV6_MMTU) { 1150 /* xxx */ 1151 rt->rt_rmx.rmx_locks |= RTV_MTU; 1152 } else if (mtu < rt->rt_ifp->if_mtu && 1153 rt->rt_rmx.rmx_mtu > mtu) { 1154 icmp6stat.icp6s_pmtuchg++; 1155 rt->rt_rmx.rmx_mtu = mtu; 1156 } 1157 } 1158 if (rt) 1159 rtfree(rt); 1160 } 1161 1162 /* 1163 * Process a Node Information Query packet, based on 1164 * draft-ietf-ipngwg-icmp-name-lookups-07. 1165 * 1166 * Spec incompatibilities: 1167 * - IPv6 Subject address handling 1168 * - IPv4 Subject address handling support missing 1169 * - Proxy reply (answer even if it's not for me) 1170 * - joins NI group address at in6_ifattach() time only, does not cope 1171 * with hostname changes by sethostname(3) 1172 */ 1173 #define hostnamelen strlen(hostname) 1174 static struct mbuf * 1175 ni6_input(m, off) 1176 struct mbuf *m; 1177 int off; 1178 { 1179 struct icmp6_nodeinfo *ni6, *nni6; 1180 struct mbuf *n = NULL; 1181 u_int16_t qtype; 1182 int subjlen; 1183 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1184 struct ni_reply_fqdn *fqdn; 1185 int addrs; /* for NI_QTYPE_NODEADDR */ 1186 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1187 struct sockaddr_in6 sin6_sbj; /* subject address */ 1188 struct sockaddr_in6 sin6_d; 1189 struct ip6_hdr *ip6; 1190 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 1191 char *subj = NULL; 1192 struct in6_ifaddr *ia6 = NULL; 1193 1194 ip6 = mtod(m, struct ip6_hdr *); 1195 #ifndef PULLDOWN_TEST 1196 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 1197 #else 1198 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 1199 if (ni6 == NULL) { 1200 /* m is already reclaimed */ 1201 return (NULL); 1202 } 1203 #endif 1204 1205 bzero(&sin6_d, sizeof(sin6_d)); 1206 sin6_d.sin6_family = AF_INET6; /* not used, actually */ 1207 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */ 1208 sin6_d.sin6_addr = ip6->ip6_dst; 1209 if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_dst, 1210 &sin6_d.sin6_scope_id)) { 1211 goto bad; 1212 } 1213 if (in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL)) 1214 goto bad; /* XXX should not happen */ 1215 1216 /* 1217 * Validate IPv6 destination address. 1218 * 1219 * The Responder must discard the Query without further processing 1220 * unless it is one of the Responder's unicast or anycast addresses, or 1221 * a link-local scope multicast address which the Responder has joined. 1222 * [icmp-name-lookups-08, Section 4.] 1223 */ 1224 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1225 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1226 goto bad; 1227 /* else it's a link-local multicast, fine */ 1228 } else { /* unicast or anycast */ 1229 if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1230 goto bad; /* XXX impossible */ 1231 1232 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1233 !(icmp6_nodeinfo & 4)) { 1234 nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 1235 "a temporary address in %s:%d", 1236 __FILE__, __LINE__)); 1237 goto bad; 1238 } 1239 } 1240 1241 /* validate query Subject field. */ 1242 qtype = ntohs(ni6->ni_qtype); 1243 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 1244 switch (qtype) { 1245 case NI_QTYPE_NOOP: 1246 case NI_QTYPE_SUPTYPES: 1247 /* 07 draft */ 1248 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 1249 break; 1250 /* FALLTHROUGH */ 1251 case NI_QTYPE_FQDN: 1252 case NI_QTYPE_NODEADDR: 1253 switch (ni6->ni_code) { 1254 case ICMP6_NI_SUBJ_IPV6: 1255 #if ICMP6_NI_SUBJ_IPV6 != 0 1256 case 0: 1257 #endif 1258 /* 1259 * backward compatibility - try to accept 03 draft 1260 * format, where no Subject is present. 1261 */ 1262 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 1263 subjlen == 0) { 1264 oldfqdn++; 1265 break; 1266 } 1267 #if ICMP6_NI_SUBJ_IPV6 != 0 1268 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 1269 goto bad; 1270 #endif 1271 1272 if (subjlen != sizeof(struct in6_addr)) 1273 goto bad; 1274 1275 /* 1276 * Validate Subject address. 1277 * 1278 * Not sure what exactly "address belongs to the node" 1279 * means in the spec, is it just unicast, or what? 1280 * 1281 * At this moment we consider Subject address as 1282 * "belong to the node" if the Subject address equals 1283 * to the IPv6 destination address; validation for 1284 * IPv6 destination address should have done enough 1285 * check for us. 1286 * 1287 * We do not do proxy at this moment. 1288 */ 1289 /* m_pulldown instead of copy? */ 1290 bzero(&sin6_sbj, sizeof(sin6_sbj)); 1291 sin6_sbj.sin6_family = AF_INET6; 1292 sin6_sbj.sin6_len = sizeof(sin6_sbj); 1293 m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1294 subjlen, (caddr_t)&sin6_sbj.sin6_addr); 1295 if (in6_addr2zoneid(m->m_pkthdr.rcvif, 1296 &sin6_sbj.sin6_addr, &sin6_sbj.sin6_scope_id)) { 1297 goto bad; 1298 } 1299 if (in6_embedscope(&sin6_sbj.sin6_addr, &sin6_sbj, 1300 NULL, NULL)) 1301 goto bad; /* XXX should not happen */ 1302 1303 subj = (char *)&sin6_sbj; 1304 if (SA6_ARE_ADDR_EQUAL(&sin6_sbj, &sin6_d)) 1305 break; 1306 1307 /* 1308 * XXX if we are to allow other cases, we should really 1309 * be careful about scope here. 1310 * basically, we should disallow queries toward IPv6 1311 * destination X with subject Y, 1312 * if scope(X) > scope(Y). 1313 * if we allow scope(X) > scope(Y), it will result in 1314 * information leakage across scope boundary. 1315 */ 1316 goto bad; 1317 1318 case ICMP6_NI_SUBJ_FQDN: 1319 /* 1320 * Validate Subject name with gethostname(3). 1321 * 1322 * The behavior may need some debate, since: 1323 * - we are not sure if the node has FQDN as 1324 * hostname (returned by gethostname(3)). 1325 * - the code does wildcard match for truncated names. 1326 * however, we are not sure if we want to perform 1327 * wildcard match, if gethostname(3) side has 1328 * truncated hostname. 1329 */ 1330 n = ni6_nametodns(hostname, hostnamelen, 0); 1331 if (!n || n->m_next || n->m_len == 0) 1332 goto bad; 1333 IP6_EXTHDR_GET(subj, char *, m, 1334 off + sizeof(struct icmp6_nodeinfo), subjlen); 1335 if (subj == NULL) 1336 goto bad; 1337 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1338 n->m_len)) { 1339 goto bad; 1340 } 1341 m_freem(n); 1342 n = NULL; 1343 break; 1344 1345 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 1346 default: 1347 goto bad; 1348 } 1349 break; 1350 } 1351 1352 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 1353 switch (qtype) { 1354 case NI_QTYPE_FQDN: 1355 if ((icmp6_nodeinfo & 1) == 0) 1356 goto bad; 1357 break; 1358 case NI_QTYPE_NODEADDR: 1359 if ((icmp6_nodeinfo & 2) == 0) 1360 goto bad; 1361 break; 1362 } 1363 1364 /* guess reply length */ 1365 switch (qtype) { 1366 case NI_QTYPE_NOOP: 1367 break; /* no reply data */ 1368 case NI_QTYPE_SUPTYPES: 1369 replylen += sizeof(u_int32_t); 1370 break; 1371 case NI_QTYPE_FQDN: 1372 /* XXX will append an mbuf */ 1373 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1374 break; 1375 case NI_QTYPE_NODEADDR: 1376 addrs = ni6_addrs(ni6, m, &ifp, subj); 1377 if ((replylen += addrs * (sizeof(struct in6_addr) + 1378 sizeof(u_int32_t))) > MCLBYTES) 1379 replylen = MCLBYTES; /* XXX: will truncate pkt later */ 1380 break; 1381 default: 1382 /* 1383 * XXX: We must return a reply with the ICMP6 code 1384 * `unknown Qtype' in this case. However we regard the case 1385 * as an FQDN query for backward compatibility. 1386 * Older versions set a random value to this field, 1387 * so it rarely varies in the defined qtypes. 1388 * But the mechanism is not reliable... 1389 * maybe we should obsolete older versions. 1390 */ 1391 qtype = NI_QTYPE_FQDN; 1392 /* XXX will append an mbuf */ 1393 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1394 oldfqdn++; 1395 break; 1396 } 1397 1398 /* allocate an mbuf to reply. */ 1399 MGETHDR(n, M_DONTWAIT, m->m_type); 1400 if (n == NULL) { 1401 m_freem(m); 1402 return (NULL); 1403 } 1404 M_MOVE_PKTHDR(n, m); /* just for recvif */ 1405 if (replylen > MHLEN) { 1406 if (replylen > MCLBYTES) { 1407 /* 1408 * XXX: should we try to allocate more? But MCLBYTES 1409 * is probably much larger than IPV6_MMTU... 1410 */ 1411 goto bad; 1412 } 1413 MCLGET(n, M_DONTWAIT); 1414 if ((n->m_flags & M_EXT) == 0) { 1415 goto bad; 1416 } 1417 } 1418 n->m_pkthdr.len = n->m_len = replylen; 1419 1420 /* copy mbuf header and IPv6 + Node Information base headers */ 1421 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 1422 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 1423 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 1424 1425 /* qtype dependent procedure */ 1426 switch (qtype) { 1427 case NI_QTYPE_NOOP: 1428 nni6->ni_code = ICMP6_NI_SUCCESS; 1429 nni6->ni_flags = 0; 1430 break; 1431 case NI_QTYPE_SUPTYPES: 1432 { 1433 u_int32_t v; 1434 nni6->ni_code = ICMP6_NI_SUCCESS; 1435 nni6->ni_flags = htons(0x0000); /* raw bitmap */ 1436 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 1437 v = (u_int32_t)htonl(0x0000000f); 1438 bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 1439 break; 1440 } 1441 case NI_QTYPE_FQDN: 1442 nni6->ni_code = ICMP6_NI_SUCCESS; 1443 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1444 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 1445 nni6->ni_flags = 0; /* XXX: meaningless TTL */ 1446 fqdn->ni_fqdn_ttl = 0; /* ditto. */ 1447 /* 1448 * XXX do we really have FQDN in variable "hostname"? 1449 */ 1450 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 1451 if (n->m_next == NULL) 1452 goto bad; 1453 /* XXX we assume that n->m_next is not a chain */ 1454 if (n->m_next->m_next != NULL) 1455 goto bad; 1456 n->m_pkthdr.len += n->m_next->m_len; 1457 break; 1458 case NI_QTYPE_NODEADDR: 1459 { 1460 int lenlim, copied; 1461 1462 nni6->ni_code = ICMP6_NI_SUCCESS; 1463 n->m_pkthdr.len = n->m_len = 1464 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1465 lenlim = M_TRAILINGSPACE(n); 1466 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 1467 /* XXX: reset mbuf length */ 1468 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1469 sizeof(struct icmp6_nodeinfo) + copied; 1470 break; 1471 } 1472 default: 1473 break; /* XXX impossible! */ 1474 } 1475 1476 nni6->ni_type = ICMP6_NI_REPLY; 1477 m_freem(m); 1478 return (n); 1479 1480 bad: 1481 m_freem(m); 1482 if (n) 1483 m_freem(n); 1484 return (NULL); 1485 } 1486 #undef hostnamelen 1487 1488 /* 1489 * make a mbuf with DNS-encoded string. no compression support. 1490 * 1491 * XXX names with less than 2 dots (like "foo" or "foo.section") will be 1492 * treated as truncated name (two \0 at the end). this is a wild guess. 1493 */ 1494 static struct mbuf * 1495 ni6_nametodns(name, namelen, old) 1496 const char *name; 1497 int namelen; 1498 int old; /* return pascal string if non-zero */ 1499 { 1500 struct mbuf *m; 1501 char *cp, *ep; 1502 const char *p, *q; 1503 int i, len, nterm; 1504 1505 if (old) 1506 len = namelen + 1; 1507 else 1508 len = MCLBYTES; 1509 1510 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1511 MGET(m, M_DONTWAIT, MT_DATA); 1512 if (m && len > MLEN) { 1513 MCLGET(m, M_DONTWAIT); 1514 if ((m->m_flags & M_EXT) == 0) 1515 goto fail; 1516 } 1517 if (!m) 1518 goto fail; 1519 m->m_next = NULL; 1520 1521 if (old) { 1522 m->m_len = len; 1523 *mtod(m, char *) = namelen; 1524 bcopy(name, mtod(m, char *) + 1, namelen); 1525 return m; 1526 } else { 1527 m->m_len = 0; 1528 cp = mtod(m, char *); 1529 ep = mtod(m, char *) + M_TRAILINGSPACE(m); 1530 1531 /* if not certain about my name, return empty buffer */ 1532 if (namelen == 0) 1533 return m; 1534 1535 /* 1536 * guess if it looks like shortened hostname, or FQDN. 1537 * shortened hostname needs two trailing "\0". 1538 */ 1539 i = 0; 1540 for (p = name; p < name + namelen; p++) { 1541 if (*p && *p == '.') 1542 i++; 1543 } 1544 if (i < 2) 1545 nterm = 2; 1546 else 1547 nterm = 1; 1548 1549 p = name; 1550 while (cp < ep && p < name + namelen) { 1551 i = 0; 1552 for (q = p; q < name + namelen && *q && *q != '.'; q++) 1553 i++; 1554 /* result does not fit into mbuf */ 1555 if (cp + i + 1 >= ep) 1556 goto fail; 1557 /* 1558 * DNS label length restriction, RFC1035 page 8. 1559 * "i == 0" case is included here to avoid returning 1560 * 0-length label on "foo..bar". 1561 */ 1562 if (i <= 0 || i >= 64) 1563 goto fail; 1564 *cp++ = i; 1565 bcopy(p, cp, i); 1566 cp += i; 1567 p = q; 1568 if (p < name + namelen && *p == '.') 1569 p++; 1570 } 1571 /* termination */ 1572 if (cp + nterm >= ep) 1573 goto fail; 1574 while (nterm-- > 0) 1575 *cp++ = '\0'; 1576 m->m_len = cp - mtod(m, char *); 1577 return m; 1578 } 1579 1580 panic("should not reach here"); 1581 /* NOTREACHED */ 1582 1583 fail: 1584 if (m) 1585 m_freem(m); 1586 return NULL; 1587 } 1588 1589 /* 1590 * check if two DNS-encoded string matches. takes care of truncated 1591 * form (with \0\0 at the end). no compression support. 1592 * XXX upper/lowercase match (see RFC2065) 1593 */ 1594 static int 1595 ni6_dnsmatch(a, alen, b, blen) 1596 const char *a; 1597 int alen; 1598 const char *b; 1599 int blen; 1600 { 1601 const char *a0, *b0; 1602 int l; 1603 1604 /* simplest case - need validation? */ 1605 if (alen == blen && bcmp(a, b, alen) == 0) 1606 return 1; 1607 1608 a0 = a; 1609 b0 = b; 1610 1611 /* termination is mandatory */ 1612 if (alen < 2 || blen < 2) 1613 return 0; 1614 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 1615 return 0; 1616 alen--; 1617 blen--; 1618 1619 while (a - a0 < alen && b - b0 < blen) { 1620 if (a - a0 + 1 > alen || b - b0 + 1 > blen) 1621 return 0; 1622 1623 if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 1624 return 0; 1625 /* we don't support compression yet */ 1626 if (a[0] >= 64 || b[0] >= 64) 1627 return 0; 1628 1629 /* truncated case */ 1630 if (a[0] == 0 && a - a0 == alen - 1) 1631 return 1; 1632 if (b[0] == 0 && b - b0 == blen - 1) 1633 return 1; 1634 if (a[0] == 0 || b[0] == 0) 1635 return 0; 1636 1637 if (a[0] != b[0]) 1638 return 0; 1639 l = a[0]; 1640 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 1641 return 0; 1642 if (bcmp(a + 1, b + 1, l) != 0) 1643 return 0; 1644 1645 a += 1 + l; 1646 b += 1 + l; 1647 } 1648 1649 if (a - a0 == alen && b - b0 == blen) 1650 return 1; 1651 else 1652 return 0; 1653 } 1654 1655 /* 1656 * calculate the number of addresses to be returned in the node info reply. 1657 */ 1658 static int 1659 ni6_addrs(ni6, m, ifpp, subj) 1660 struct icmp6_nodeinfo *ni6; 1661 struct mbuf *m; 1662 struct ifnet **ifpp; 1663 char *subj; 1664 { 1665 struct ifnet *ifp; 1666 struct in6_ifaddr *ifa6; 1667 struct ifaddr *ifa; 1668 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ 1669 int addrs = 0, addrsofif, iffound = 0; 1670 int niflags = ni6->ni_flags; 1671 1672 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 1673 switch (ni6->ni_code) { 1674 case ICMP6_NI_SUBJ_IPV6: 1675 if (subj == NULL) /* must be impossible... */ 1676 return (0); 1677 subj_ip6 = (struct sockaddr_in6 *)subj; 1678 break; 1679 default: 1680 /* 1681 * XXX: we only support IPv6 subject address for 1682 * this Qtype. 1683 */ 1684 return (0); 1685 } 1686 } 1687 1688 IFNET_RLOCK(); 1689 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 1690 addrsofif = 0; 1691 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1692 if (ifa->ifa_addr->sa_family != AF_INET6) 1693 continue; 1694 ifa6 = (struct in6_ifaddr *)ifa; 1695 1696 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1697 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, 1698 &ifa6->ia_addr.sin6_addr)) 1699 iffound = 1; 1700 1701 /* 1702 * IPv4-mapped addresses can only be returned by a 1703 * Node Information proxy, since they represent 1704 * addresses of IPv4-only nodes, which perforce do 1705 * not implement this protocol. 1706 * [icmp-name-lookups-07, Section 5.4] 1707 * So we don't support NI_NODEADDR_FLAG_COMPAT in 1708 * this function at this moment. 1709 */ 1710 1711 /* What do we have to do about ::1? */ 1712 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 1713 case IPV6_ADDR_SCOPE_LINKLOCAL: 1714 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1715 continue; 1716 break; 1717 case IPV6_ADDR_SCOPE_SITELOCAL: 1718 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1719 continue; 1720 break; 1721 case IPV6_ADDR_SCOPE_GLOBAL: 1722 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1723 continue; 1724 break; 1725 default: 1726 continue; 1727 } 1728 1729 /* 1730 * check if anycast is okay. 1731 * XXX: just experimental. not in the spec. 1732 */ 1733 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 1734 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 1735 continue; /* we need only unicast addresses */ 1736 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1737 (icmp6_nodeinfo & 4) == 0) { 1738 continue; 1739 } 1740 addrsofif++; /* count the address */ 1741 } 1742 if (iffound) { 1743 *ifpp = ifp; 1744 IFNET_RUNLOCK(); 1745 return (addrsofif); 1746 } 1747 1748 addrs += addrsofif; 1749 } 1750 IFNET_RUNLOCK(); 1751 1752 return (addrs); 1753 } 1754 1755 static int 1756 ni6_store_addrs(ni6, nni6, ifp0, resid) 1757 struct icmp6_nodeinfo *ni6, *nni6; 1758 struct ifnet *ifp0; 1759 int resid; 1760 { 1761 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 1762 struct in6_ifaddr *ifa6; 1763 struct ifaddr *ifa; 1764 struct ifnet *ifp_dep = NULL; 1765 int copied = 0, allow_deprecated = 0; 1766 u_char *cp = (u_char *)(nni6 + 1); 1767 int niflags = ni6->ni_flags; 1768 u_int32_t ltime; 1769 1770 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1771 return (0); /* needless to copy */ 1772 1773 IFNET_RLOCK(); 1774 again: 1775 1776 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 1777 for (ifa = ifp->if_addrlist.tqh_first; ifa; 1778 ifa = ifa->ifa_list.tqe_next) { 1779 if (ifa->ifa_addr->sa_family != AF_INET6) 1780 continue; 1781 ifa6 = (struct in6_ifaddr *)ifa; 1782 1783 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 1784 allow_deprecated == 0) { 1785 /* 1786 * prefererred address should be put before 1787 * deprecated addresses. 1788 */ 1789 1790 /* record the interface for later search */ 1791 if (ifp_dep == NULL) 1792 ifp_dep = ifp; 1793 1794 continue; 1795 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1796 allow_deprecated != 0) 1797 continue; /* we now collect deprecated addrs */ 1798 1799 /* What do we have to do about ::1? */ 1800 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 1801 case IPV6_ADDR_SCOPE_LINKLOCAL: 1802 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1803 continue; 1804 break; 1805 case IPV6_ADDR_SCOPE_SITELOCAL: 1806 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1807 continue; 1808 break; 1809 case IPV6_ADDR_SCOPE_GLOBAL: 1810 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1811 continue; 1812 break; 1813 default: 1814 continue; 1815 } 1816 1817 /* 1818 * check if anycast is okay. 1819 * XXX: just experimental. not in the spec. 1820 */ 1821 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 1822 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 1823 continue; 1824 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1825 (icmp6_nodeinfo & 4) == 0) { 1826 continue; 1827 } 1828 1829 /* now we can copy the address */ 1830 if (resid < sizeof(struct in6_addr) + 1831 sizeof(u_int32_t)) { 1832 /* 1833 * We give up much more copy. 1834 * Set the truncate flag and return. 1835 */ 1836 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1837 IFNET_RUNLOCK(); 1838 return (copied); 1839 } 1840 1841 /* 1842 * Set the TTL of the address. 1843 * The TTL value should be one of the following 1844 * according to the specification: 1845 * 1846 * 1. The remaining lifetime of a DHCP lease on the 1847 * address, or 1848 * 2. The remaining Valid Lifetime of a prefix from 1849 * which the address was derived through Stateless 1850 * Autoconfiguration. 1851 * 1852 * Note that we currently do not support stateful 1853 * address configuration by DHCPv6, so the former 1854 * case can't happen. 1855 */ 1856 if (ifa6->ia6_lifetime.ia6t_expire == 0) 1857 ltime = ND6_INFINITE_LIFETIME; 1858 else { 1859 if (ifa6->ia6_lifetime.ia6t_expire > 1860 time_second) 1861 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 1862 else 1863 ltime = 0; 1864 } 1865 1866 bcopy(<ime, cp, sizeof(u_int32_t)); 1867 cp += sizeof(u_int32_t); 1868 1869 /* copy the address itself */ 1870 bcopy(&ifa6->ia_addr.sin6_addr, cp, 1871 sizeof(struct in6_addr)); 1872 in6_clearscope((struct in6_addr *)cp); /* XXX */ 1873 cp += sizeof(struct in6_addr); 1874 1875 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1876 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1877 } 1878 if (ifp0) /* we need search only on the specified IF */ 1879 break; 1880 } 1881 1882 if (allow_deprecated == 0 && ifp_dep != NULL) { 1883 ifp = ifp_dep; 1884 allow_deprecated = 1; 1885 1886 goto again; 1887 } 1888 1889 IFNET_RUNLOCK(); 1890 1891 return (copied); 1892 } 1893 1894 /* 1895 * XXX almost dup'ed code with rip6_input. 1896 */ 1897 static int 1898 icmp6_rip6_input(mp, off) 1899 struct mbuf **mp; 1900 int off; 1901 { 1902 struct mbuf *m = *mp; 1903 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1904 struct in6pcb *in6p; 1905 struct in6pcb *last = NULL; 1906 struct sockaddr_in6 fromsa; 1907 struct icmp6_hdr *icmp6; 1908 struct mbuf *opts = NULL; 1909 1910 #ifndef PULLDOWN_TEST 1911 /* this is assumed to be safe. */ 1912 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 1913 #else 1914 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 1915 if (icmp6 == NULL) { 1916 /* m is already reclaimed */ 1917 return (IPPROTO_DONE); 1918 } 1919 #endif 1920 1921 bzero(&fromsa, sizeof(fromsa)); 1922 fromsa.sin6_len = sizeof(struct sockaddr_in6); 1923 fromsa.sin6_family = AF_INET6; 1924 /* KAME hack: recover scopeid */ 1925 (void)in6_recoverscope(&fromsa, &ip6->ip6_src, m->m_pkthdr.rcvif); 1926 1927 LIST_FOREACH(in6p, &ripcb, inp_list) { 1928 if ((in6p->inp_vflag & INP_IPV6) == 0) 1929 continue; 1930 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 1931 continue; 1932 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 1933 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 1934 continue; 1935 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 1936 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 1937 continue; 1938 if (in6p->in6p_icmp6filt 1939 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 1940 in6p->in6p_icmp6filt)) 1941 continue; 1942 if (last) { 1943 struct mbuf *n = NULL; 1944 1945 /* 1946 * Recent network drivers tend to allocate a single 1947 * mbuf cluster, rather than to make a couple of 1948 * mbufs without clusters. Also, since the IPv6 code 1949 * path tries to avoid m_pullup(), it is highly 1950 * probable that we still have an mbuf cluster here 1951 * even though the necessary length can be stored in an 1952 * mbuf's internal buffer. 1953 * Meanwhile, the default size of the receive socket 1954 * buffer for raw sockets is not so large. This means 1955 * the possibility of packet loss is relatively higher 1956 * than before. To avoid this scenario, we copy the 1957 * received data to a separate mbuf that does not use 1958 * a cluster, if possible. 1959 * XXX: it is better to copy the data after stripping 1960 * intermediate headers. 1961 */ 1962 if ((m->m_flags & M_EXT) && m->m_next == NULL && 1963 m->m_len <= MHLEN) { 1964 MGET(n, M_DONTWAIT, m->m_type); 1965 if (n != NULL) { 1966 m_dup_pkthdr(n, m, M_NOWAIT); 1967 bcopy(m->m_data, n->m_data, m->m_len); 1968 n->m_len = m->m_len; 1969 } 1970 } 1971 if (n != NULL || 1972 (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 1973 if (last->in6p_flags & IN6P_CONTROLOPTS) 1974 ip6_savecontrol(last, n, &opts); 1975 /* strip intermediate headers */ 1976 m_adj(n, off); 1977 if (sbappendaddr(&last->in6p_socket->so_rcv, 1978 (struct sockaddr *)&fromsa, n, opts) 1979 == 0) { 1980 /* should notify about lost packet */ 1981 m_freem(n); 1982 if (opts) { 1983 m_freem(opts); 1984 } 1985 } else 1986 sorwakeup(last->in6p_socket); 1987 opts = NULL; 1988 } 1989 } 1990 last = in6p; 1991 } 1992 if (last) { 1993 if (last->in6p_flags & IN6P_CONTROLOPTS) 1994 ip6_savecontrol(last, m, &opts); 1995 /* strip intermediate headers */ 1996 m_adj(m, off); 1997 1998 /* avoid using mbuf clusters if possible (see above) */ 1999 if ((m->m_flags & M_EXT) && m->m_next == NULL && 2000 m->m_len <= MHLEN) { 2001 struct mbuf *n; 2002 2003 MGET(n, M_DONTWAIT, m->m_type); 2004 if (n != NULL) { 2005 m_dup_pkthdr(n, m, M_NOWAIT); 2006 bcopy(m->m_data, n->m_data, m->m_len); 2007 n->m_len = m->m_len; 2008 2009 m_freem(m); 2010 m = n; 2011 } 2012 } 2013 if (sbappendaddr(&last->in6p_socket->so_rcv, 2014 (struct sockaddr *)&fromsa, m, opts) == 0) { 2015 m_freem(m); 2016 if (opts) 2017 m_freem(opts); 2018 } else 2019 sorwakeup(last->in6p_socket); 2020 } else { 2021 m_freem(m); 2022 ip6stat.ip6s_delivered--; 2023 } 2024 return IPPROTO_DONE; 2025 } 2026 2027 /* 2028 * Reflect the ip6 packet back to the source. 2029 * OFF points to the icmp6 header, counted from the top of the mbuf. 2030 */ 2031 void 2032 icmp6_reflect(m, off) 2033 struct mbuf *m; 2034 size_t off; 2035 { 2036 struct ip6_hdr *ip6; 2037 struct icmp6_hdr *icmp6; 2038 struct in6_ifaddr *ia; 2039 struct in6_addr t, *src = 0; 2040 int plen; 2041 int type, code; 2042 struct ifnet *outif = NULL; 2043 struct sockaddr_in6 sa6_src, sa6_dst; 2044 #ifdef COMPAT_RFC1885 2045 int mtu = IPV6_MMTU; 2046 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; 2047 #endif 2048 2049 /* too short to reflect */ 2050 if (off < sizeof(struct ip6_hdr)) { 2051 nd6log((LOG_DEBUG, 2052 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 2053 (u_long)off, (u_long)sizeof(struct ip6_hdr), 2054 __FILE__, __LINE__)); 2055 goto bad; 2056 } 2057 2058 /* 2059 * If there are extra headers between IPv6 and ICMPv6, strip 2060 * off that header first. 2061 */ 2062 #ifdef DIAGNOSTIC 2063 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 2064 panic("assumption failed in icmp6_reflect"); 2065 #endif 2066 if (off > sizeof(struct ip6_hdr)) { 2067 size_t l; 2068 struct ip6_hdr nip6; 2069 2070 l = off - sizeof(struct ip6_hdr); 2071 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 2072 m_adj(m, l); 2073 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 2074 if (m->m_len < l) { 2075 if ((m = m_pullup(m, l)) == NULL) 2076 return; 2077 } 2078 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 2079 } else /* off == sizeof(struct ip6_hdr) */ { 2080 size_t l; 2081 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 2082 if (m->m_len < l) { 2083 if ((m = m_pullup(m, l)) == NULL) 2084 return; 2085 } 2086 } 2087 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 2088 ip6 = mtod(m, struct ip6_hdr *); 2089 ip6->ip6_nxt = IPPROTO_ICMPV6; 2090 icmp6 = (struct icmp6_hdr *)(ip6 + 1); 2091 type = icmp6->icmp6_type; /* keep type for statistics */ 2092 code = icmp6->icmp6_code; /* ditto. */ 2093 2094 t = ip6->ip6_dst; 2095 /* 2096 * ip6_input() drops a packet if its src is multicast. 2097 * So, the src is never multicast. 2098 */ 2099 ip6->ip6_dst = ip6->ip6_src; 2100 2101 /* 2102 * XXX: make sure to embed scope zone information, using 2103 * already embedded IDs or the received interface (if any). 2104 * Note that rcvif may be NULL. 2105 * TODO: scoped routing case (XXX). 2106 */ 2107 bzero(&sa6_src, sizeof(sa6_src)); 2108 sa6_src.sin6_family = AF_INET6; 2109 sa6_src.sin6_len = sizeof(sa6_src); 2110 sa6_src.sin6_addr = ip6->ip6_dst; 2111 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); 2112 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL); 2113 bzero(&sa6_dst, sizeof(sa6_dst)); 2114 sa6_dst.sin6_family = AF_INET6; 2115 sa6_dst.sin6_len = sizeof(sa6_dst); 2116 sa6_dst.sin6_addr = t; 2117 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); 2118 in6_embedscope(&t, &sa6_dst, NULL, NULL); 2119 2120 #ifdef COMPAT_RFC1885 2121 /* 2122 * xxx guess MTU 2123 * RFC 1885 requires that echo reply should be truncated if it 2124 * does not fit in with (return) path MTU, but the description was 2125 * removed in the new spec. 2126 */ 2127 if (icmp6_reflect_rt.ro_rt == 0 || 2128 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { 2129 if (icmp6_reflect_rt.ro_rt) { 2130 RTFREE(icmp6_reflect_rt.ro_rt); 2131 icmp6_reflect_rt.ro_rt = 0; 2132 } 2133 bzero(sin6, sizeof(*sin6)); 2134 sin6->sin6_family = PF_INET6; 2135 sin6->sin6_len = sizeof(struct sockaddr_in6); 2136 sin6->sin6_addr = ip6->ip6_dst; 2137 2138 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt, 2139 RTF_PRCLONING); 2140 } 2141 2142 if (icmp6_reflect_rt.ro_rt == 0) 2143 goto bad; 2144 2145 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) 2146 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) 2147 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; 2148 2149 if (mtu < m->m_pkthdr.len) { 2150 plen -= (m->m_pkthdr.len - mtu); 2151 m_adj(m, mtu - m->m_pkthdr.len); 2152 } 2153 #endif 2154 /* 2155 * If the incoming packet was addressed directly to us (i.e. unicast), 2156 * use dst as the src for the reply. 2157 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 2158 * (for example) when we encounter an error while forwarding procedure 2159 * destined to a duplicated address of ours. 2160 */ 2161 for (ia = in6_ifaddr; ia; ia = ia->ia_next) 2162 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && 2163 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 2164 src = &t; 2165 break; 2166 } 2167 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { 2168 /* 2169 * This is the case if the dst is our link-local address 2170 * and the sender is also ourselves. 2171 */ 2172 src = &t; 2173 } 2174 2175 if (src == 0) { 2176 int e; 2177 struct route_in6 ro; 2178 2179 /* 2180 * This case matches to multicasts, our anycast, or unicasts 2181 * that we do not own. Select a source address based on the 2182 * source address of the erroneous packet. 2183 */ 2184 bzero(&ro, sizeof(ro)); 2185 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e); 2186 if (ro.ro_rt) 2187 RTFREE(ro.ro_rt); /* XXX: we could use this */ 2188 if (src == NULL) { 2189 nd6log((LOG_DEBUG, 2190 "icmp6_reflect: source can't be determined: " 2191 "dst=%s, error=%d\n", 2192 ip6_sprintf(&sa6_src.sin6_addr), e)); 2193 goto bad; 2194 } 2195 } 2196 2197 ip6->ip6_src = *src; 2198 2199 ip6->ip6_flow = 0; 2200 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2201 ip6->ip6_vfc |= IPV6_VERSION; 2202 ip6->ip6_nxt = IPPROTO_ICMPV6; 2203 if (outif) 2204 ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2205 else if (m->m_pkthdr.rcvif) { 2206 /* XXX: This may not be the outgoing interface */ 2207 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 2208 } else 2209 ip6->ip6_hlim = ip6_defhlim; 2210 2211 icmp6->icmp6_cksum = 0; 2212 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2213 sizeof(struct ip6_hdr), plen); 2214 2215 /* 2216 * XXX option handling 2217 */ 2218 2219 m->m_flags &= ~(M_BCAST|M_MCAST); 2220 #ifdef IPSEC 2221 /* Don't lookup socket */ 2222 (void)ipsec_setsocket(m, NULL); 2223 #endif /* IPSEC */ 2224 2225 #ifdef COMPAT_RFC1885 2226 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL); 2227 #else 2228 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 2229 #endif 2230 if (outif) 2231 icmp6_ifoutstat_inc(outif, type, code); 2232 2233 return; 2234 2235 bad: 2236 m_freem(m); 2237 return; 2238 } 2239 2240 void 2241 icmp6_fasttimo() 2242 { 2243 2244 mld6_fasttimeo(); 2245 } 2246 2247 static const char * 2248 icmp6_redirect_diag(src6, dst6, tgt6) 2249 struct in6_addr *src6; 2250 struct in6_addr *dst6; 2251 struct in6_addr *tgt6; 2252 { 2253 static char buf[1024]; 2254 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2255 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); 2256 return buf; 2257 } 2258 2259 void 2260 icmp6_redirect_input(m, off) 2261 struct mbuf *m; 2262 int off; 2263 { 2264 struct ifnet *ifp = m->m_pkthdr.rcvif; 2265 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 2266 struct nd_redirect *nd_rd; 2267 int icmp6len = ntohs(ip6->ip6_plen); 2268 char *lladdr = NULL; 2269 int lladdrlen = 0; 2270 u_char *redirhdr = NULL; 2271 int redirhdrlen = 0; 2272 struct rtentry *rt = NULL; 2273 int is_router; 2274 int is_onlink; 2275 struct in6_addr src6 = ip6->ip6_src; 2276 struct in6_addr redtgt6; 2277 struct in6_addr reddst6; 2278 union nd_opts ndopts; 2279 2280 if (!m || !ifp) 2281 return; 2282 2283 /* XXX if we are router, we don't update route by icmp6 redirect */ 2284 if (ip6_forwarding) 2285 goto freeit; 2286 if (!icmp6_rediraccept) 2287 goto freeit; 2288 2289 #ifndef PULLDOWN_TEST 2290 IP6_EXTHDR_CHECK(m, off, icmp6len,); 2291 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 2292 #else 2293 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 2294 if (nd_rd == NULL) { 2295 icmp6stat.icp6s_tooshort++; 2296 return; 2297 } 2298 #endif 2299 redtgt6 = nd_rd->nd_rd_target; 2300 reddst6 = nd_rd->nd_rd_dst; 2301 2302 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 2303 redtgt6.s6_addr16[1] = htons(ifp->if_index); 2304 if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) 2305 reddst6.s6_addr16[1] = htons(ifp->if_index); 2306 2307 /* validation */ 2308 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 2309 nd6log((LOG_ERR, 2310 "ICMP6 redirect sent from %s rejected; " 2311 "must be from linklocal\n", 2312 ip6_sprintf(&src6))); 2313 goto bad; 2314 } 2315 if (ip6->ip6_hlim != 255) { 2316 nd6log((LOG_ERR, 2317 "ICMP6 redirect sent from %s rejected; " 2318 "hlim=%d (must be 255)\n", 2319 ip6_sprintf(&src6), ip6->ip6_hlim)); 2320 goto bad; 2321 } 2322 { 2323 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 2324 struct sockaddr_in6 sin6; 2325 struct in6_addr *gw6; 2326 2327 bzero(&sin6, sizeof(sin6)); 2328 sin6.sin6_family = AF_INET6; 2329 sin6.sin6_len = sizeof(struct sockaddr_in6); 2330 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 2331 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 2332 if (rt) { 2333 if (rt->rt_gateway == NULL || 2334 rt->rt_gateway->sa_family != AF_INET6) { 2335 nd6log((LOG_ERR, 2336 "ICMP6 redirect rejected; no route " 2337 "with inet6 gateway found for redirect dst: %s\n", 2338 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2339 RTFREE_LOCKED(rt); 2340 goto bad; 2341 } 2342 2343 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 2344 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 2345 nd6log((LOG_ERR, 2346 "ICMP6 redirect rejected; " 2347 "not equal to gw-for-src=%s (must be same): " 2348 "%s\n", 2349 ip6_sprintf(gw6), 2350 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2351 RTFREE_LOCKED(rt); 2352 goto bad; 2353 } 2354 } else { 2355 nd6log((LOG_ERR, 2356 "ICMP6 redirect rejected; " 2357 "no route found for redirect dst: %s\n", 2358 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2359 goto bad; 2360 } 2361 RTFREE_LOCKED(rt); 2362 rt = NULL; 2363 } 2364 if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 2365 nd6log((LOG_ERR, 2366 "ICMP6 redirect rejected; " 2367 "redirect dst must be unicast: %s\n", 2368 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2369 goto bad; 2370 } 2371 2372 is_router = is_onlink = 0; 2373 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 2374 is_router = 1; /* router case */ 2375 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 2376 is_onlink = 1; /* on-link destination case */ 2377 if (!is_router && !is_onlink) { 2378 nd6log((LOG_ERR, 2379 "ICMP6 redirect rejected; " 2380 "neither router case nor onlink case: %s\n", 2381 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2382 goto bad; 2383 } 2384 /* validation passed */ 2385 2386 icmp6len -= sizeof(*nd_rd); 2387 nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 2388 if (nd6_options(&ndopts) < 0) { 2389 nd6log((LOG_INFO, "icmp6_redirect_input: " 2390 "invalid ND option, rejected: %s\n", 2391 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2392 /* nd6_options have incremented stats */ 2393 goto freeit; 2394 } 2395 2396 if (ndopts.nd_opts_tgt_lladdr) { 2397 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 2398 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 2399 } 2400 2401 if (ndopts.nd_opts_rh) { 2402 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 2403 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 2404 } 2405 2406 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 2407 nd6log((LOG_INFO, 2408 "icmp6_redirect_input: lladdrlen mismatch for %s " 2409 "(if %d, icmp6 packet %d): %s\n", 2410 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, 2411 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2412 goto bad; 2413 } 2414 2415 /* RFC 2461 8.3 */ 2416 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2417 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 2418 2419 if (!is_onlink) { /* better router case. perform rtredirect. */ 2420 /* perform rtredirect */ 2421 struct sockaddr_in6 sdst; 2422 struct sockaddr_in6 sgw; 2423 struct sockaddr_in6 ssrc; 2424 2425 bzero(&sdst, sizeof(sdst)); 2426 bzero(&sgw, sizeof(sgw)); 2427 bzero(&ssrc, sizeof(ssrc)); 2428 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 2429 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 2430 sizeof(struct sockaddr_in6); 2431 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 2432 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 2433 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 2434 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2435 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2436 (struct sockaddr *)&ssrc); 2437 } 2438 /* finally update cached route in each socket via pfctlinput */ 2439 { 2440 struct sockaddr_in6 sdst; 2441 2442 bzero(&sdst, sizeof(sdst)); 2443 sdst.sin6_family = AF_INET6; 2444 sdst.sin6_len = sizeof(struct sockaddr_in6); 2445 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 2446 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2447 #if defined(IPSEC) || defined(FAST_IPSEC) 2448 key_sa_routechange((struct sockaddr *)&sdst); 2449 #endif 2450 } 2451 2452 freeit: 2453 m_freem(m); 2454 return; 2455 2456 bad: 2457 icmp6stat.icp6s_badredirect++; 2458 m_freem(m); 2459 } 2460 2461 void 2462 icmp6_redirect_output(m0, rt) 2463 struct mbuf *m0; 2464 struct rtentry *rt; 2465 { 2466 struct ifnet *ifp; /* my outgoing interface */ 2467 struct in6_addr *ifp_ll6; 2468 struct in6_addr *router_ll6; 2469 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 2470 struct mbuf *m = NULL; /* newly allocated one */ 2471 struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 2472 struct nd_redirect *nd_rd; 2473 size_t maxlen; 2474 u_char *p; 2475 struct ifnet *outif = NULL; 2476 struct sockaddr_in6 src_sa; 2477 2478 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 2479 2480 /* if we are not router, we don't send icmp6 redirect */ 2481 if (!ip6_forwarding || ip6_accept_rtadv) 2482 goto fail; 2483 2484 /* sanity check */ 2485 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 2486 goto fail; 2487 2488 /* 2489 * Address check: 2490 * the source address must identify a neighbor, and 2491 * the destination address must not be a multicast address 2492 * [RFC 2461, sec 8.2] 2493 */ 2494 sip6 = mtod(m0, struct ip6_hdr *); 2495 bzero(&src_sa, sizeof(src_sa)); 2496 src_sa.sin6_family = AF_INET6; 2497 src_sa.sin6_len = sizeof(src_sa); 2498 src_sa.sin6_addr = sip6->ip6_src; 2499 /* we don't currently use sin6_scope_id, but eventually use it */ 2500 if (in6_addr2zoneid(ifp, &sip6->ip6_src, &src_sa.sin6_scope_id)) 2501 goto fail; 2502 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 2503 goto fail; 2504 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 2505 goto fail; /* what should we do here? */ 2506 2507 /* rate limit */ 2508 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 2509 goto fail; 2510 2511 /* 2512 * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 2513 * we almost always ask for an mbuf cluster for simplicity. 2514 * (MHLEN < IPV6_MMTU is almost always true) 2515 */ 2516 #if IPV6_MMTU >= MCLBYTES 2517 # error assumption failed about IPV6_MMTU and MCLBYTES 2518 #endif 2519 MGETHDR(m, M_DONTWAIT, MT_HEADER); 2520 if (m && IPV6_MMTU >= MHLEN) 2521 MCLGET(m, M_DONTWAIT); 2522 if (!m) 2523 goto fail; 2524 m->m_pkthdr.rcvif = NULL; 2525 m->m_len = 0; 2526 maxlen = M_TRAILINGSPACE(m); 2527 maxlen = min(IPV6_MMTU, maxlen); 2528 /* just for safety */ 2529 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 2530 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 2531 goto fail; 2532 } 2533 2534 { 2535 /* get ip6 linklocal address for ifp(my outgoing interface). */ 2536 struct in6_ifaddr *ia; 2537 if ((ia = in6ifa_ifpforlinklocal(ifp, 2538 IN6_IFF_NOTREADY| 2539 IN6_IFF_ANYCAST)) == NULL) 2540 goto fail; 2541 ifp_ll6 = &ia->ia_addr.sin6_addr; 2542 } 2543 2544 /* get ip6 linklocal address for the router. */ 2545 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 2546 struct sockaddr_in6 *sin6; 2547 sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 2548 router_ll6 = &sin6->sin6_addr; 2549 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 2550 router_ll6 = (struct in6_addr *)NULL; 2551 } else 2552 router_ll6 = (struct in6_addr *)NULL; 2553 2554 /* ip6 */ 2555 ip6 = mtod(m, struct ip6_hdr *); 2556 ip6->ip6_flow = 0; 2557 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2558 ip6->ip6_vfc |= IPV6_VERSION; 2559 /* ip6->ip6_plen will be set later */ 2560 ip6->ip6_nxt = IPPROTO_ICMPV6; 2561 ip6->ip6_hlim = 255; 2562 /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 2563 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 2564 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 2565 2566 /* ND Redirect */ 2567 nd_rd = (struct nd_redirect *)(ip6 + 1); 2568 nd_rd->nd_rd_type = ND_REDIRECT; 2569 nd_rd->nd_rd_code = 0; 2570 nd_rd->nd_rd_reserved = 0; 2571 if (rt->rt_flags & RTF_GATEWAY) { 2572 /* 2573 * nd_rd->nd_rd_target must be a link-local address in 2574 * better router cases. 2575 */ 2576 if (!router_ll6) 2577 goto fail; 2578 bcopy(router_ll6, &nd_rd->nd_rd_target, 2579 sizeof(nd_rd->nd_rd_target)); 2580 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2581 sizeof(nd_rd->nd_rd_dst)); 2582 } else { 2583 /* make sure redtgt == reddst */ 2584 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2585 sizeof(nd_rd->nd_rd_target)); 2586 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2587 sizeof(nd_rd->nd_rd_dst)); 2588 } 2589 2590 p = (u_char *)(nd_rd + 1); 2591 2592 if (!router_ll6) 2593 goto nolladdropt; 2594 2595 { 2596 /* target lladdr option */ 2597 struct rtentry *rt_router = NULL; 2598 int len; 2599 struct sockaddr_dl *sdl; 2600 struct nd_opt_hdr *nd_opt; 2601 char *lladdr; 2602 2603 rt_router = nd6_lookup(router_ll6, 0, ifp); 2604 if (!rt_router) 2605 goto nolladdropt; 2606 len = sizeof(*nd_opt) + ifp->if_addrlen; 2607 len = (len + 7) & ~7; /* round by 8 */ 2608 /* safety check */ 2609 if (len + (p - (u_char *)ip6) > maxlen) 2610 goto nolladdropt; 2611 if (!(rt_router->rt_flags & RTF_GATEWAY) && 2612 (rt_router->rt_flags & RTF_LLINFO) && 2613 (rt_router->rt_gateway->sa_family == AF_LINK) && 2614 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && 2615 sdl->sdl_alen) { 2616 nd_opt = (struct nd_opt_hdr *)p; 2617 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2618 nd_opt->nd_opt_len = len >> 3; 2619 lladdr = (char *)(nd_opt + 1); 2620 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 2621 p += len; 2622 } 2623 } 2624 nolladdropt:; 2625 2626 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 2627 2628 /* just to be safe */ 2629 #ifdef M_DECRYPTED /*not openbsd*/ 2630 if (m0->m_flags & M_DECRYPTED) 2631 goto noredhdropt; 2632 #endif 2633 if (p - (u_char *)ip6 > maxlen) 2634 goto noredhdropt; 2635 2636 { 2637 /* redirected header option */ 2638 int len; 2639 struct nd_opt_rd_hdr *nd_opt_rh; 2640 2641 /* 2642 * compute the maximum size for icmp6 redirect header option. 2643 * XXX room for auth header? 2644 */ 2645 len = maxlen - (p - (u_char *)ip6); 2646 len &= ~7; 2647 2648 /* This is just for simplicity. */ 2649 if (m0->m_pkthdr.len != m0->m_len) { 2650 if (m0->m_next) { 2651 m_freem(m0->m_next); 2652 m0->m_next = NULL; 2653 } 2654 m0->m_pkthdr.len = m0->m_len; 2655 } 2656 2657 /* 2658 * Redirected header option spec (RFC2461 4.6.3) talks nothing 2659 * about padding/truncate rule for the original IP packet. 2660 * From the discussion on IPv6imp in Feb 1999, 2661 * the consensus was: 2662 * - "attach as much as possible" is the goal 2663 * - pad if not aligned (original size can be guessed by 2664 * original ip6 header) 2665 * Following code adds the padding if it is simple enough, 2666 * and truncates if not. 2667 */ 2668 if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2669 panic("assumption failed in %s:%d", __FILE__, 2670 __LINE__); 2671 2672 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2673 /* not enough room, truncate */ 2674 m0->m_pkthdr.len = m0->m_len = len - 2675 sizeof(*nd_opt_rh); 2676 } else { 2677 /* enough room, pad or truncate */ 2678 size_t extra; 2679 2680 extra = m0->m_pkthdr.len % 8; 2681 if (extra) { 2682 /* pad if easy enough, truncate if not */ 2683 if (8 - extra <= M_TRAILINGSPACE(m0)) { 2684 /* pad */ 2685 m0->m_len += (8 - extra); 2686 m0->m_pkthdr.len += (8 - extra); 2687 } else { 2688 /* truncate */ 2689 m0->m_pkthdr.len -= extra; 2690 m0->m_len -= extra; 2691 } 2692 } 2693 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2694 m0->m_pkthdr.len = m0->m_len = len - 2695 sizeof(*nd_opt_rh); 2696 } 2697 2698 nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2699 bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2700 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2701 nd_opt_rh->nd_opt_rh_len = len >> 3; 2702 p += sizeof(*nd_opt_rh); 2703 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 2704 2705 /* connect m0 to m */ 2706 m_tag_delete_chain(m0, NULL); 2707 m0->m_flags &= ~M_PKTHDR; 2708 m->m_next = m0; 2709 m->m_pkthdr.len = m->m_len + m0->m_len; 2710 m0 = NULL; 2711 } 2712 noredhdropt:; 2713 if (m0) { 2714 m_freem(m0); 2715 m0 = NULL; 2716 } 2717 2718 /* XXX: clear embedded link IDs in the inner header */ 2719 in6_clearscope(&sip6->ip6_src); 2720 in6_clearscope(&sip6->ip6_dst); 2721 in6_clearscope(&nd_rd->nd_rd_target); 2722 in6_clearscope(&nd_rd->nd_rd_dst); 2723 2724 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 2725 2726 nd_rd->nd_rd_cksum = 0; 2727 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2728 sizeof(*ip6), ntohs(ip6->ip6_plen)); 2729 2730 /* send the packet to outside... */ 2731 #ifdef IPSEC 2732 /* Don't lookup socket */ 2733 (void)ipsec_setsocket(m, NULL); 2734 #endif /* IPSEC */ 2735 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 2736 if (outif) { 2737 icmp6_ifstat_inc(outif, ifs6_out_msg); 2738 icmp6_ifstat_inc(outif, ifs6_out_redirect); 2739 } 2740 icmp6stat.icp6s_outhist[ND_REDIRECT]++; 2741 2742 return; 2743 2744 fail: 2745 if (m) 2746 m_freem(m); 2747 if (m0) 2748 m_freem(m0); 2749 } 2750 2751 /* 2752 * ICMPv6 socket option processing. 2753 */ 2754 int 2755 icmp6_ctloutput(so, sopt) 2756 struct socket *so; 2757 struct sockopt *sopt; 2758 { 2759 int error = 0; 2760 int optlen; 2761 struct inpcb *inp = sotoinpcb(so); 2762 int level, op, optname; 2763 2764 if (sopt) { 2765 level = sopt->sopt_level; 2766 op = sopt->sopt_dir; 2767 optname = sopt->sopt_name; 2768 optlen = sopt->sopt_valsize; 2769 } else 2770 level = op = optname = optlen = 0; 2771 2772 if (level != IPPROTO_ICMPV6) { 2773 return EINVAL; 2774 } 2775 2776 switch (op) { 2777 case PRCO_SETOPT: 2778 switch (optname) { 2779 case ICMP6_FILTER: 2780 { 2781 struct icmp6_filter *p; 2782 2783 if (optlen != sizeof(*p)) { 2784 error = EMSGSIZE; 2785 break; 2786 } 2787 if (inp->in6p_icmp6filt == NULL) { 2788 error = EINVAL; 2789 break; 2790 } 2791 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, 2792 optlen); 2793 break; 2794 } 2795 2796 default: 2797 error = ENOPROTOOPT; 2798 break; 2799 } 2800 break; 2801 2802 case PRCO_GETOPT: 2803 switch (optname) { 2804 case ICMP6_FILTER: 2805 { 2806 if (inp->in6p_icmp6filt == NULL) { 2807 error = EINVAL; 2808 break; 2809 } 2810 error = sooptcopyout(sopt, inp->in6p_icmp6filt, 2811 sizeof(struct icmp6_filter)); 2812 break; 2813 } 2814 2815 default: 2816 error = ENOPROTOOPT; 2817 break; 2818 } 2819 break; 2820 } 2821 2822 return (error); 2823 } 2824 2825 /* 2826 * Perform rate limit check. 2827 * Returns 0 if it is okay to send the icmp6 packet. 2828 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 2829 * limitation. 2830 * 2831 * XXX per-destination/type check necessary? 2832 */ 2833 static int 2834 icmp6_ratelimit(dst, type, code) 2835 const struct in6_addr *dst; /* not used at this moment */ 2836 const int type; /* not used at this moment */ 2837 const int code; /* not used at this moment */ 2838 { 2839 int ret; 2840 2841 ret = 0; /* okay to send */ 2842 2843 /* PPS limit */ 2844 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 2845 icmp6errppslim)) { 2846 /* The packet is subject to rate limit */ 2847 ret++; 2848 } 2849 2850 return ret; 2851 } 2852