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