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