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