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