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