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