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