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