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