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