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