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