icmp6.c revision 1.83
1/* $NetBSD: icmp6.c,v 1.83 2002/06/30 22:40:38 thorpej 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.83 2002/06/30 22:40:38 thorpej 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#ifdef IPSEC 104#include <netinet6/ipsec.h> 105#include <netkey/key.h> 106#endif 107 108#include "faith.h" 109#if defined(NFAITH) && 0 < NFAITH 110#include <net/if_faith.h> 111#endif 112 113#include <net/net_osdep.h> 114 115extern struct domain inet6domain; 116 117struct icmp6stat icmp6stat; 118 119extern struct in6pcb rawin6pcb; 120extern int icmp6errppslim; 121static int icmp6errpps_count = 0; 122static struct timeval icmp6errppslim_last; 123extern int icmp6_nodeinfo; 124 125/* 126 * List of callbacks to notify when Path MTU changes are made. 127 */ 128struct icmp6_mtudisc_callback { 129 LIST_ENTRY(icmp6_mtudisc_callback) mc_list; 130 void (*mc_func) __P((struct in6_addr *)); 131}; 132 133LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks = 134 LIST_HEAD_INITIALIZER(&icmp6_mtudisc_callbacks); 135 136static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL; 137extern int pmtu_expire; 138 139/* XXX do these values make any sense? */ 140static int icmp6_mtudisc_hiwat = 1280; 141static int icmp6_mtudisc_lowat = 256; 142 143/* 144 * keep track of # of redirect routes. 145 */ 146static struct rttimer_queue *icmp6_redirect_timeout_q = NULL; 147 148/* XXX experimental, turned off */ 149static int icmp6_redirect_hiwat = -1; 150static int icmp6_redirect_lowat = -1; 151 152static void icmp6_errcount __P((struct icmp6errstat *, int, int)); 153static int icmp6_rip6_input __P((struct mbuf **, int)); 154static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); 155static const char *icmp6_redirect_diag __P((struct in6_addr *, 156 struct in6_addr *, struct in6_addr *)); 157static struct mbuf *ni6_input __P((struct mbuf *, int)); 158static struct mbuf *ni6_nametodns __P((const char *, int, int)); 159static int ni6_dnsmatch __P((const char *, int, const char *, int)); 160static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 161 struct ifnet **, char *)); 162static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 163 struct ifnet *, int)); 164static int icmp6_notify_error __P((struct mbuf *, int, int, int)); 165static struct rtentry *icmp6_mtudisc_clone __P((struct sockaddr *)); 166static void icmp6_mtudisc_timeout __P((struct rtentry *, struct rttimer *)); 167static void icmp6_redirect_timeout __P((struct rtentry *, struct rttimer *)); 168 169void 170icmp6_init() 171{ 172 mld6_init(); 173 icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire); 174 icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout); 175} 176 177static void 178icmp6_errcount(stat, type, code) 179 struct icmp6errstat *stat; 180 int type, code; 181{ 182 switch (type) { 183 case ICMP6_DST_UNREACH: 184 switch (code) { 185 case ICMP6_DST_UNREACH_NOROUTE: 186 stat->icp6errs_dst_unreach_noroute++; 187 return; 188 case ICMP6_DST_UNREACH_ADMIN: 189 stat->icp6errs_dst_unreach_admin++; 190 return; 191 case ICMP6_DST_UNREACH_BEYONDSCOPE: 192 stat->icp6errs_dst_unreach_beyondscope++; 193 return; 194 case ICMP6_DST_UNREACH_ADDR: 195 stat->icp6errs_dst_unreach_addr++; 196 return; 197 case ICMP6_DST_UNREACH_NOPORT: 198 stat->icp6errs_dst_unreach_noport++; 199 return; 200 } 201 break; 202 case ICMP6_PACKET_TOO_BIG: 203 stat->icp6errs_packet_too_big++; 204 return; 205 case ICMP6_TIME_EXCEEDED: 206 switch (code) { 207 case ICMP6_TIME_EXCEED_TRANSIT: 208 stat->icp6errs_time_exceed_transit++; 209 return; 210 case ICMP6_TIME_EXCEED_REASSEMBLY: 211 stat->icp6errs_time_exceed_reassembly++; 212 return; 213 } 214 break; 215 case ICMP6_PARAM_PROB: 216 switch (code) { 217 case ICMP6_PARAMPROB_HEADER: 218 stat->icp6errs_paramprob_header++; 219 return; 220 case ICMP6_PARAMPROB_NEXTHEADER: 221 stat->icp6errs_paramprob_nextheader++; 222 return; 223 case ICMP6_PARAMPROB_OPTION: 224 stat->icp6errs_paramprob_option++; 225 return; 226 } 227 break; 228 case ND_REDIRECT: 229 stat->icp6errs_redirect++; 230 return; 231 } 232 stat->icp6errs_unknown++; 233} 234 235/* 236 * Register a Path MTU Discovery callback. 237 */ 238void 239icmp6_mtudisc_callback_register(func) 240 void (*func) __P((struct in6_addr *)); 241{ 242 struct icmp6_mtudisc_callback *mc; 243 244 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL; 245 mc = LIST_NEXT(mc, mc_list)) { 246 if (mc->mc_func == func) 247 return; 248 } 249 250 mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT); 251 if (mc == NULL) 252 panic("icmp6_mtudisc_callback_register"); 253 254 mc->mc_func = func; 255 LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, mc, mc_list); 256} 257 258/* 259 * Generate an error packet of type error in response to bad IP6 packet. 260 */ 261void 262icmp6_error(m, type, code, param) 263 struct mbuf *m; 264 int type, code, param; 265{ 266 struct ip6_hdr *oip6, *nip6; 267 struct icmp6_hdr *icmp6; 268 u_int preplen; 269 int off; 270 int nxt; 271 272 icmp6stat.icp6s_error++; 273 274 /* count per-type-code statistics */ 275 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 276 277 if (m->m_flags & M_DECRYPTED) { 278 icmp6stat.icp6s_canterror++; 279 goto freeit; 280 } 281 282#ifndef PULLDOWN_TEST 283 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 284#else 285 if (m->m_len < sizeof(struct ip6_hdr)) { 286 m = m_pullup(m, sizeof(struct ip6_hdr)); 287 if (m == NULL) 288 return; 289 } 290#endif 291 oip6 = mtod(m, struct ip6_hdr *); 292 293 /* 294 * If the destination address of the erroneous packet is a multicast 295 * address, or the packet was sent using link-layer multicast, 296 * we should basically suppress sending an error (RFC 2463, Section 297 * 2.4). 298 * We have two exceptions (the item e.2 in that section): 299 * - the Pakcet Too Big message can be sent for path MTU discovery. 300 * - the Parameter Problem Message that can be allowed an icmp6 error 301 * in the option type field. This check has been done in 302 * ip6_unknown_opt(), so we can just check the type and code. 303 */ 304 if ((m->m_flags & (M_BCAST|M_MCAST) || 305 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 306 (type != ICMP6_PACKET_TOO_BIG && 307 (type != ICMP6_PARAM_PROB || 308 code != ICMP6_PARAMPROB_OPTION))) 309 goto freeit; 310 311 /* 312 * RFC 2463, 2.4 (e.5): source address check. 313 * XXX: the case of anycast source? 314 */ 315 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 316 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 317 goto freeit; 318 319 /* 320 * If we are about to send ICMPv6 against ICMPv6 error/redirect, 321 * don't do it. 322 */ 323 nxt = -1; 324 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 325 if (off >= 0 && nxt == IPPROTO_ICMPV6) { 326 struct icmp6_hdr *icp; 327 328#ifndef PULLDOWN_TEST 329 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 330 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 331#else 332 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 333 sizeof(*icp)); 334 if (icp == NULL) { 335 icmp6stat.icp6s_tooshort++; 336 return; 337 } 338#endif 339 if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 340 icp->icmp6_type == ND_REDIRECT) { 341 /* 342 * ICMPv6 error 343 * Special case: for redirect (which is 344 * informational) we must not send icmp6 error. 345 */ 346 icmp6stat.icp6s_canterror++; 347 goto freeit; 348 } else { 349 /* ICMPv6 informational - send the error */ 350 } 351 } 352#if 0 /* controversial */ 353 else if (off >= 0 && nxt == IPPROTO_ESP) { 354 /* 355 * It could be ICMPv6 error inside ESP. Take a safer side, 356 * don't respond. 357 */ 358 icmp6stat.icp6s_canterror++; 359 goto freeit; 360 } 361#endif 362 else { 363 /* non-ICMPv6 - send the error */ 364 } 365 366 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 367 368 /* Finally, do rate limitation check. */ 369 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 370 icmp6stat.icp6s_toofreq++; 371 goto freeit; 372 } 373 374 /* 375 * OK, ICMP6 can be generated. 376 */ 377 378 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 379 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 380 381 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 382 M_PREPEND(m, preplen, M_DONTWAIT); 383 if (m && m->m_len < preplen) 384 m = m_pullup(m, preplen); 385 if (m == NULL) { 386 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 387 return; 388 } 389 390 nip6 = mtod(m, struct ip6_hdr *); 391 nip6->ip6_src = oip6->ip6_src; 392 nip6->ip6_dst = oip6->ip6_dst; 393 394 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src)) 395 oip6->ip6_src.s6_addr16[1] = 0; 396 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst)) 397 oip6->ip6_dst.s6_addr16[1] = 0; 398 399 icmp6 = (struct icmp6_hdr *)(nip6 + 1); 400 icmp6->icmp6_type = type; 401 icmp6->icmp6_code = code; 402 icmp6->icmp6_pptr = htonl((u_int32_t)param); 403 404 /* 405 * icmp6_reflect() is designed to be in the input path. 406 * icmp6_error() can be called from both input and outut path, 407 * and if we are in output path rcvif could contain bogus value. 408 * clear m->m_pkthdr.rcvif for safety, we should have enough scope 409 * information in ip header (nip6). 410 */ 411 m->m_pkthdr.rcvif = NULL; 412 413 icmp6stat.icp6s_outhist[type]++; 414 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 415 416 return; 417 418 freeit: 419 /* 420 * If we can't tell wheter or not we can generate ICMP6, free it. 421 */ 422 m_freem(m); 423} 424 425/* 426 * Process a received ICMP6 message. 427 */ 428int 429icmp6_input(mp, offp, proto) 430 struct mbuf **mp; 431 int *offp, proto; 432{ 433 struct mbuf *m = *mp, *n; 434 struct ip6_hdr *ip6, *nip6; 435 struct icmp6_hdr *icmp6, *nicmp6; 436 int off = *offp; 437 int icmp6len = m->m_pkthdr.len - *offp; 438 int code, sum, noff; 439 440 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 441 442#ifndef PULLDOWN_TEST 443 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 444 /* m might change if M_LOOP. So, call mtod after this */ 445#endif 446 447 /* 448 * Locate icmp6 structure in mbuf, and check 449 * that not corrupted and of at least minimum length 450 */ 451 452 ip6 = mtod(m, struct ip6_hdr *); 453 if (icmp6len < sizeof(struct icmp6_hdr)) { 454 icmp6stat.icp6s_tooshort++; 455 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 456 goto freeit; 457 } 458 459 /* 460 * calculate the checksum 461 */ 462#ifndef PULLDOWN_TEST 463 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 464#else 465 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 466 if (icmp6 == NULL) { 467 icmp6stat.icp6s_tooshort++; 468 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 469 return IPPROTO_DONE; 470 } 471#endif 472 KASSERT(IP6_HDR_ALIGNED_P(icmp6)); 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 (rt->rt_rmx.rmx_mtu > mtu || rt->rt_rmx.rmx_mtu == 0)) { 1215 if (mtu < IN6_LINKMTU(rt->rt_ifp)) { 1216 icmp6stat.icp6s_pmtuchg++; 1217 rt->rt_rmx.rmx_mtu = mtu; 1218 } 1219 } 1220 if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */ 1221 RTFREE(rt); 1222 } 1223 1224 /* 1225 * Notify protocols that the MTU for this destination 1226 * has changed. 1227 */ 1228 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL; 1229 mc = LIST_NEXT(mc, mc_list)) 1230 (*mc->mc_func)(&sin6.sin6_addr); 1231} 1232 1233/* 1234 * Process a Node Information Query packet, based on 1235 * draft-ietf-ipngwg-icmp-name-lookups-07. 1236 * 1237 * Spec incompatibilities: 1238 * - IPv6 Subject address handling 1239 * - IPv4 Subject address handling support missing 1240 * - Proxy reply (answer even if it's not for me) 1241 * - joins NI group address at in6_ifattach() time only, does not cope 1242 * with hostname changes by sethostname(3) 1243 */ 1244#ifndef offsetof /* XXX */ 1245#define offsetof(type, member) ((size_t)(&((type *)0)->member)) 1246#endif 1247static struct mbuf * 1248ni6_input(m, off) 1249 struct mbuf *m; 1250 int off; 1251{ 1252 struct icmp6_nodeinfo *ni6, *nni6; 1253 struct mbuf *n = NULL; 1254 u_int16_t qtype; 1255 int subjlen; 1256 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1257 struct ni_reply_fqdn *fqdn; 1258 int addrs; /* for NI_QTYPE_NODEADDR */ 1259 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1260 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */ 1261 struct ip6_hdr *ip6; 1262 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 1263 char *subj = NULL; 1264 1265 ip6 = mtod(m, struct ip6_hdr *); 1266#ifndef PULLDOWN_TEST 1267 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 1268#else 1269 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 1270 if (ni6 == NULL) { 1271 /* m is already reclaimed */ 1272 return NULL; 1273 } 1274#endif 1275 1276 /* 1277 * Validate IPv6 destination address. 1278 * 1279 * The Responder must discard the Query without further processing 1280 * unless it is one of the Responder's unicast or anycast addresses, or 1281 * a link-local scope multicast address which the Responder has joined. 1282 * [icmp-name-lookups-07, Section 4.] 1283 */ 1284 bzero(&sin6, sizeof(sin6)); 1285 sin6.sin6_family = AF_INET6; 1286 sin6.sin6_len = sizeof(struct sockaddr_in6); 1287 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr)); 1288 /* XXX scopeid */ 1289 if (ifa_ifwithaddr((struct sockaddr *)&sin6)) 1290 ; /* unicast/anycast, fine */ 1291 else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) 1292 ; /* link-local multicast, fine */ 1293 else 1294 goto bad; 1295 1296 /* validate query Subject field. */ 1297 qtype = ntohs(ni6->ni_qtype); 1298 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 1299 switch (qtype) { 1300 case NI_QTYPE_NOOP: 1301 case NI_QTYPE_SUPTYPES: 1302 /* 07 draft */ 1303 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 1304 break; 1305 /* FALLTHROUGH */ 1306 case NI_QTYPE_FQDN: 1307 case NI_QTYPE_NODEADDR: 1308 switch (ni6->ni_code) { 1309 case ICMP6_NI_SUBJ_IPV6: 1310#if ICMP6_NI_SUBJ_IPV6 != 0 1311 case 0: 1312#endif 1313 /* 1314 * backward compatibility - try to accept 03 draft 1315 * format, where no Subject is present. 1316 */ 1317 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 1318 subjlen == 0) { 1319 oldfqdn++; 1320 break; 1321 } 1322#if ICMP6_NI_SUBJ_IPV6 != 0 1323 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 1324 goto bad; 1325#endif 1326 1327 if (subjlen != sizeof(sin6.sin6_addr)) 1328 goto bad; 1329 1330 /* 1331 * Validate Subject address. 1332 * 1333 * Not sure what exactly "address belongs to the node" 1334 * means in the spec, is it just unicast, or what? 1335 * 1336 * At this moment we consider Subject address as 1337 * "belong to the node" if the Subject address equals 1338 * to the IPv6 destination address; validation for 1339 * IPv6 destination address should have done enough 1340 * check for us. 1341 * 1342 * We do not do proxy at this moment. 1343 */ 1344 /* m_pulldown instead of copy? */ 1345 m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1346 subjlen, (caddr_t)&sin6.sin6_addr); 1347 /* XXX kame scope hack */ 1348 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) { 1349 if ((m->m_flags & M_PKTHDR) != 0 && 1350 m->m_pkthdr.rcvif) { 1351 sin6.sin6_addr.s6_addr16[1] = 1352 htons(m->m_pkthdr.rcvif->if_index); 1353 } 1354 } 1355 subj = (char *)&sin6; 1356 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &sin6.sin6_addr)) 1357 break; 1358 1359 /* 1360 * XXX if we are to allow other cases, we should really 1361 * be careful about scope here. 1362 * basically, we should disallow queries toward IPv6 1363 * destination X with subject Y, if scope(X) > scope(Y). 1364 * if we allow scope(X) > scope(Y), it will result in 1365 * information leakage across scope boundary. 1366 */ 1367 goto bad; 1368 1369 case ICMP6_NI_SUBJ_FQDN: 1370 /* 1371 * Validate Subject name with gethostname(3). 1372 * 1373 * The behavior may need some debate, since: 1374 * - we are not sure if the node has FQDN as 1375 * hostname (returned by gethostname(3)). 1376 * - the code does wildcard match for truncated names. 1377 * however, we are not sure if we want to perform 1378 * wildcard match, if gethostname(3) side has 1379 * truncated hostname. 1380 */ 1381 n = ni6_nametodns(hostname, hostnamelen, 0); 1382 if (!n || n->m_next || n->m_len == 0) 1383 goto bad; 1384 IP6_EXTHDR_GET(subj, char *, m, 1385 off + sizeof(struct icmp6_nodeinfo), subjlen); 1386 if (subj == NULL) 1387 goto bad; 1388 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1389 n->m_len)) { 1390 goto bad; 1391 } 1392 m_freem(n); 1393 n = NULL; 1394 break; 1395 1396 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 1397 default: 1398 goto bad; 1399 } 1400 break; 1401 } 1402 1403 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 1404 switch (qtype) { 1405 case NI_QTYPE_FQDN: 1406 if ((icmp6_nodeinfo & 1) == 0) 1407 goto bad; 1408 break; 1409 case NI_QTYPE_NODEADDR: 1410 if ((icmp6_nodeinfo & 2) == 0) 1411 goto bad; 1412 break; 1413 } 1414 1415 /* guess reply length */ 1416 switch (qtype) { 1417 case NI_QTYPE_NOOP: 1418 break; /* no reply data */ 1419 case NI_QTYPE_SUPTYPES: 1420 replylen += sizeof(u_int32_t); 1421 break; 1422 case NI_QTYPE_FQDN: 1423 /* XXX will append an mbuf */ 1424 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1425 break; 1426 case NI_QTYPE_NODEADDR: 1427 addrs = ni6_addrs(ni6, m, &ifp, subj); 1428 if ((replylen += addrs * (sizeof(struct in6_addr) + 1429 sizeof(u_int32_t))) > MCLBYTES) 1430 replylen = MCLBYTES; /* XXX: will truncate pkt later */ 1431 break; 1432 default: 1433 /* 1434 * XXX: We must return a reply with the ICMP6 code 1435 * `unknown Qtype' in this case. However we regard the case 1436 * as an FQDN query for backward compatibility. 1437 * Older versions set a random value to this field, 1438 * so it rarely varies in the defined qtypes. 1439 * But the mechanism is not reliable... 1440 * maybe we should obsolete older versions. 1441 */ 1442 qtype = NI_QTYPE_FQDN; 1443 /* XXX will append an mbuf */ 1444 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1445 oldfqdn++; 1446 break; 1447 } 1448 1449 /* allocate an mbuf to reply. */ 1450 MGETHDR(n, M_DONTWAIT, m->m_type); 1451 if (n == NULL) { 1452 m_freem(m); 1453 return(NULL); 1454 } 1455 M_COPY_PKTHDR(n, m); /* just for rcvif */ 1456 if (replylen > MHLEN) { 1457 if (replylen > MCLBYTES) { 1458 /* 1459 * XXX: should we try to allocate more? But MCLBYTES 1460 * is probably much larger than IPV6_MMTU... 1461 */ 1462 goto bad; 1463 } 1464 MCLGET(n, M_DONTWAIT); 1465 if ((n->m_flags & M_EXT) == 0) { 1466 goto bad; 1467 } 1468 } 1469 n->m_pkthdr.len = n->m_len = replylen; 1470 1471 /* copy mbuf header and IPv6 + Node Information base headers */ 1472 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 1473 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 1474 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 1475 1476 /* qtype dependent procedure */ 1477 switch (qtype) { 1478 case NI_QTYPE_NOOP: 1479 nni6->ni_code = ICMP6_NI_SUCCESS; 1480 nni6->ni_flags = 0; 1481 break; 1482 case NI_QTYPE_SUPTYPES: 1483 { 1484 u_int32_t v; 1485 nni6->ni_code = ICMP6_NI_SUCCESS; 1486 nni6->ni_flags = htons(0x0000); /* raw bitmap */ 1487 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 1488 v = (u_int32_t)htonl(0x0000000f); 1489 bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 1490 break; 1491 } 1492 case NI_QTYPE_FQDN: 1493 nni6->ni_code = ICMP6_NI_SUCCESS; 1494 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1495 sizeof(struct ip6_hdr) + 1496 sizeof(struct icmp6_nodeinfo)); 1497 nni6->ni_flags = 0; /* XXX: meaningless TTL */ 1498 fqdn->ni_fqdn_ttl = 0; /* ditto. */ 1499 /* 1500 * XXX do we really have FQDN in variable "hostname"? 1501 */ 1502 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 1503 if (n->m_next == NULL) 1504 goto bad; 1505 /* XXX we assume that n->m_next is not a chain */ 1506 if (n->m_next->m_next != NULL) 1507 goto bad; 1508 n->m_pkthdr.len += n->m_next->m_len; 1509 break; 1510 case NI_QTYPE_NODEADDR: 1511 { 1512 int lenlim, copied; 1513 1514 nni6->ni_code = ICMP6_NI_SUCCESS; 1515 n->m_pkthdr.len = n->m_len = 1516 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1517 lenlim = M_TRAILINGSPACE(n); 1518 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 1519 /* XXX: reset mbuf length */ 1520 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1521 sizeof(struct icmp6_nodeinfo) + copied; 1522 break; 1523 } 1524 default: 1525 break; /* XXX impossible! */ 1526 } 1527 1528 nni6->ni_type = ICMP6_NI_REPLY; 1529 m_freem(m); 1530 return(n); 1531 1532 bad: 1533 m_freem(m); 1534 if (n) 1535 m_freem(n); 1536 return(NULL); 1537} 1538#undef hostnamelen 1539 1540#define isupper(x) ('A' <= (x) && (x) <= 'Z') 1541#define isalpha(x) (('A' <= (x) && (x) <= 'Z') || ('a' <= (x) && (x) <= 'z')) 1542#define isalnum(x) (isalpha(x) || ('0' <= (x) && (x) <= '9')) 1543#define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x)) 1544 1545/* 1546 * make a mbuf with DNS-encoded string. no compression support. 1547 * 1548 * XXX names with less than 2 dots (like "foo" or "foo.section") will be 1549 * treated as truncated name (two \0 at the end). this is a wild guess. 1550 */ 1551static struct mbuf * 1552ni6_nametodns(name, namelen, old) 1553 const char *name; 1554 int namelen; 1555 int old; /* return pascal string if non-zero */ 1556{ 1557 struct mbuf *m; 1558 char *cp, *ep; 1559 const char *p, *q; 1560 int i, len, nterm; 1561 1562 if (old) 1563 len = namelen + 1; 1564 else 1565 len = MCLBYTES; 1566 1567 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1568 MGET(m, M_DONTWAIT, MT_DATA); 1569 if (m && len > MLEN) { 1570 MCLGET(m, M_DONTWAIT); 1571 if ((m->m_flags & M_EXT) == 0) 1572 goto fail; 1573 } 1574 if (!m) 1575 goto fail; 1576 m->m_next = NULL; 1577 1578 if (old) { 1579 m->m_len = len; 1580 *mtod(m, char *) = namelen; 1581 bcopy(name, mtod(m, char *) + 1, namelen); 1582 return m; 1583 } else { 1584 m->m_len = 0; 1585 cp = mtod(m, char *); 1586 ep = mtod(m, char *) + M_TRAILINGSPACE(m); 1587 1588 /* if not certain about my name, return empty buffer */ 1589 if (namelen == 0) 1590 return m; 1591 1592 /* 1593 * guess if it looks like shortened hostname, or FQDN. 1594 * shortened hostname needs two trailing "\0". 1595 */ 1596 i = 0; 1597 for (p = name; p < name + namelen; p++) { 1598 if (*p && *p == '.') 1599 i++; 1600 } 1601 if (i < 2) 1602 nterm = 2; 1603 else 1604 nterm = 1; 1605 1606 p = name; 1607 while (cp < ep && p < name + namelen) { 1608 i = 0; 1609 for (q = p; q < name + namelen && *q && *q != '.'; q++) 1610 i++; 1611 /* result does not fit into mbuf */ 1612 if (cp + i + 1 >= ep) 1613 goto fail; 1614 /* 1615 * DNS label length restriction, RFC1035 page 8. 1616 * "i == 0" case is included here to avoid returning 1617 * 0-length label on "foo..bar". 1618 */ 1619 if (i <= 0 || i >= 64) 1620 goto fail; 1621 *cp++ = i; 1622 if (!isalpha(p[0]) || !isalnum(p[i - 1])) 1623 goto fail; 1624 while (i > 0) { 1625 if (!isalnum(*p) && *p != '-') 1626 goto fail; 1627 if (isupper(*p)) 1628 *cp++ = tolower(*p++); 1629 else 1630 *cp++ = *p++; 1631 i--; 1632 } 1633 p = q; 1634 if (p < name + namelen && *p == '.') 1635 p++; 1636 } 1637 /* termination */ 1638 if (cp + nterm >= ep) 1639 goto fail; 1640 while (nterm-- > 0) 1641 *cp++ = '\0'; 1642 m->m_len = cp - mtod(m, char *); 1643 return m; 1644 } 1645 1646 panic("should not reach here"); 1647 /* NOTREACHED */ 1648 1649 fail: 1650 if (m) 1651 m_freem(m); 1652 return NULL; 1653} 1654 1655/* 1656 * check if two DNS-encoded string matches. takes care of truncated 1657 * form (with \0\0 at the end). no compression support. 1658 * XXX upper/lowercase match (see RFC2065) 1659 */ 1660static int 1661ni6_dnsmatch(a, alen, b, blen) 1662 const char *a; 1663 int alen; 1664 const char *b; 1665 int blen; 1666{ 1667 const char *a0, *b0; 1668 int l; 1669 1670 /* simplest case - need validation? */ 1671 if (alen == blen && bcmp(a, b, alen) == 0) 1672 return 1; 1673 1674 a0 = a; 1675 b0 = b; 1676 1677 /* termination is mandatory */ 1678 if (alen < 2 || blen < 2) 1679 return 0; 1680 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 1681 return 0; 1682 alen--; 1683 blen--; 1684 1685 while (a - a0 < alen && b - b0 < blen) { 1686 if (a - a0 + 1 > alen || b - b0 + 1 > blen) 1687 return 0; 1688 1689 if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 1690 return 0; 1691 /* we don't support compression yet */ 1692 if (a[0] >= 64 || b[0] >= 64) 1693 return 0; 1694 1695 /* truncated case */ 1696 if (a[0] == 0 && a - a0 == alen - 1) 1697 return 1; 1698 if (b[0] == 0 && b - b0 == blen - 1) 1699 return 1; 1700 if (a[0] == 0 || b[0] == 0) 1701 return 0; 1702 1703 if (a[0] != b[0]) 1704 return 0; 1705 l = a[0]; 1706 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 1707 return 0; 1708 if (bcmp(a + 1, b + 1, l) != 0) 1709 return 0; 1710 1711 a += 1 + l; 1712 b += 1 + l; 1713 } 1714 1715 if (a - a0 == alen && b - b0 == blen) 1716 return 1; 1717 else 1718 return 0; 1719} 1720 1721/* 1722 * calculate the number of addresses to be returned in the node info reply. 1723 */ 1724static int 1725ni6_addrs(ni6, m, ifpp, subj) 1726 struct icmp6_nodeinfo *ni6; 1727 struct mbuf *m; 1728 struct ifnet **ifpp; 1729 char *subj; 1730{ 1731 struct ifnet *ifp; 1732 struct in6_ifaddr *ifa6; 1733 struct ifaddr *ifa; 1734 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ 1735 int addrs = 0, addrsofif, iffound = 0; 1736 int niflags = ni6->ni_flags; 1737 1738 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 1739 switch (ni6->ni_code) { 1740 case ICMP6_NI_SUBJ_IPV6: 1741 if (subj == NULL) /* must be impossible... */ 1742 return(0); 1743 subj_ip6 = (struct sockaddr_in6 *)subj; 1744 break; 1745 default: 1746 /* 1747 * XXX: we only support IPv6 subject address for 1748 * this Qtype. 1749 */ 1750 return(0); 1751 } 1752 } 1753 1754 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 1755 { 1756 addrsofif = 0; 1757 for (ifa = ifp->if_addrlist.tqh_first; ifa; 1758 ifa = ifa->ifa_list.tqe_next) 1759 { 1760 if (ifa->ifa_addr->sa_family != AF_INET6) 1761 continue; 1762 ifa6 = (struct in6_ifaddr *)ifa; 1763 1764 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1765 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, 1766 &ifa6->ia_addr.sin6_addr)) 1767 iffound = 1; 1768 1769 /* 1770 * IPv4-mapped addresses can only be returned by a 1771 * Node Information proxy, since they represent 1772 * addresses of IPv4-only nodes, which perforce do 1773 * not implement this protocol. 1774 * [icmp-name-lookups-07, Section 5.4] 1775 * So we don't support NI_NODEADDR_FLAG_COMPAT in 1776 * this function at this moment. 1777 */ 1778 1779 /* What do we have to do about ::1? */ 1780 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 1781 case IPV6_ADDR_SCOPE_LINKLOCAL: 1782 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1783 continue; 1784 break; 1785 case IPV6_ADDR_SCOPE_SITELOCAL: 1786 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1787 continue; 1788 break; 1789 case IPV6_ADDR_SCOPE_GLOBAL: 1790 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1791 continue; 1792 break; 1793 default: 1794 continue; 1795 } 1796 1797 /* 1798 * check if anycast is okay. 1799 * XXX: just experimental. not in the spec. 1800 */ 1801 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 1802 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 1803 continue; /* we need only unicast addresses */ 1804 1805 addrsofif++; /* count the address */ 1806 } 1807 if (iffound) { 1808 *ifpp = ifp; 1809 return(addrsofif); 1810 } 1811 1812 addrs += addrsofif; 1813 } 1814 1815 return(addrs); 1816} 1817 1818static int 1819ni6_store_addrs(ni6, nni6, ifp0, resid) 1820 struct icmp6_nodeinfo *ni6, *nni6; 1821 struct ifnet *ifp0; 1822 int resid; 1823{ 1824 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 1825 struct in6_ifaddr *ifa6; 1826 struct ifaddr *ifa; 1827 struct ifnet *ifp_dep = NULL; 1828 int copied = 0, allow_deprecated = 0; 1829 u_char *cp = (u_char *)(nni6 + 1); 1830 int niflags = ni6->ni_flags; 1831 u_int32_t ltime; 1832 long time_second = time.tv_sec; 1833 1834 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1835 return(0); /* needless to copy */ 1836 1837 again: 1838 1839 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) 1840 { 1841 for (ifa = ifp->if_addrlist.tqh_first; ifa; 1842 ifa = ifa->ifa_list.tqe_next) 1843 { 1844 if (ifa->ifa_addr->sa_family != AF_INET6) 1845 continue; 1846 ifa6 = (struct in6_ifaddr *)ifa; 1847 1848 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 1849 allow_deprecated == 0) { 1850 /* 1851 * prefererred address should be put before 1852 * deprecated addresses. 1853 */ 1854 1855 /* record the interface for later search */ 1856 if (ifp_dep == NULL) 1857 ifp_dep = ifp; 1858 1859 continue; 1860 } 1861 else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1862 allow_deprecated != 0) 1863 continue; /* we now collect deprecated addrs */ 1864 1865 /* What do we have to do about ::1? */ 1866 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 1867 case IPV6_ADDR_SCOPE_LINKLOCAL: 1868 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1869 continue; 1870 break; 1871 case IPV6_ADDR_SCOPE_SITELOCAL: 1872 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1873 continue; 1874 break; 1875 case IPV6_ADDR_SCOPE_GLOBAL: 1876 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1877 continue; 1878 break; 1879 default: 1880 continue; 1881 } 1882 1883 /* 1884 * check if anycast is okay. 1885 * XXX: just experimental. not in the spec. 1886 */ 1887 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 1888 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 1889 continue; 1890 1891 /* now we can copy the address */ 1892 if (resid < sizeof(struct in6_addr) + 1893 sizeof(u_int32_t)) { 1894 /* 1895 * We give up much more copy. 1896 * Set the truncate flag and return. 1897 */ 1898 nni6->ni_flags |= 1899 NI_NODEADDR_FLAG_TRUNCATE; 1900 return(copied); 1901 } 1902 1903 /* 1904 * Set the TTL of the address. 1905 * The TTL value should be one of the following 1906 * according to the specification: 1907 * 1908 * 1. The remaining lifetime of a DHCP lease on the 1909 * address, or 1910 * 2. The remaining Valid Lifetime of a prefix from 1911 * which the address was derived through Stateless 1912 * Autoconfiguration. 1913 * 1914 * Note that we currently do not support stateful 1915 * address configuration by DHCPv6, so the former 1916 * case can't happen. 1917 * 1918 * TTL must be 2^31 > TTL >= 0. 1919 */ 1920 if (ifa6->ia6_lifetime.ia6t_expire == 0) 1921 ltime = ND6_INFINITE_LIFETIME; 1922 else { 1923 if (ifa6->ia6_lifetime.ia6t_expire > 1924 time_second) 1925 ltime = ifa6->ia6_lifetime.ia6t_expire - time_second; 1926 else 1927 ltime = 0; 1928 } 1929 if (ltime > 0x7fffffff) 1930 ltime = 0x7fffffff; 1931 ltime = htonl(ltime); 1932 1933 bcopy(<ime, cp, sizeof(u_int32_t)); 1934 cp += sizeof(u_int32_t); 1935 1936 /* copy the address itself */ 1937 bcopy(&ifa6->ia_addr.sin6_addr, cp, 1938 sizeof(struct in6_addr)); 1939 /* XXX: KAME link-local hack; remove ifindex */ 1940 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr)) 1941 ((struct in6_addr *)cp)->s6_addr16[1] = 0; 1942 cp += sizeof(struct in6_addr); 1943 1944 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1945 copied += (sizeof(struct in6_addr) + 1946 sizeof(u_int32_t)); 1947 } 1948 if (ifp0) /* we need search only on the specified IF */ 1949 break; 1950 } 1951 1952 if (allow_deprecated == 0 && ifp_dep != NULL) { 1953 ifp = ifp_dep; 1954 allow_deprecated = 1; 1955 1956 goto again; 1957 } 1958 1959 return(copied); 1960} 1961 1962/* 1963 * XXX almost dup'ed code with rip6_input. 1964 */ 1965static int 1966icmp6_rip6_input(mp, off) 1967 struct mbuf **mp; 1968 int off; 1969{ 1970 struct mbuf *m = *mp; 1971 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1972 struct in6pcb *in6p; 1973 struct in6pcb *last = NULL; 1974 struct sockaddr_in6 rip6src; 1975 struct icmp6_hdr *icmp6; 1976 struct mbuf *opts = NULL; 1977 1978#ifndef PULLDOWN_TEST 1979 /* this is assumed to be safe. */ 1980 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 1981#else 1982 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 1983 if (icmp6 == NULL) { 1984 /* m is already reclaimed */ 1985 return IPPROTO_DONE; 1986 } 1987#endif 1988 1989 bzero(&rip6src, sizeof(rip6src)); 1990 rip6src.sin6_len = sizeof(struct sockaddr_in6); 1991 rip6src.sin6_family = AF_INET6; 1992 /* KAME hack: recover scopeid */ 1993 (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif); 1994 1995 for (in6p = rawin6pcb.in6p_next; 1996 in6p != &rawin6pcb; in6p = in6p->in6p_next) 1997 { 1998 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 1999 continue; 2000 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 2001 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 2002 continue; 2003 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 2004 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 2005 continue; 2006 if (in6p->in6p_icmp6filt 2007 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 2008 in6p->in6p_icmp6filt)) 2009 continue; 2010 if (last) { 2011 struct mbuf *n; 2012 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 2013 if (last->in6p_flags & IN6P_CONTROLOPTS) 2014 ip6_savecontrol(last, &opts, ip6, n); 2015 /* strip intermediate headers */ 2016 m_adj(n, off); 2017 if (sbappendaddr(&last->in6p_socket->so_rcv, 2018 (struct sockaddr *)&rip6src, 2019 n, opts) == 0) { 2020 /* should notify about lost packet */ 2021 m_freem(n); 2022 if (opts) 2023 m_freem(opts); 2024 } else 2025 sorwakeup(last->in6p_socket); 2026 opts = NULL; 2027 } 2028 } 2029 last = in6p; 2030 } 2031 if (last) { 2032 if (last->in6p_flags & IN6P_CONTROLOPTS) 2033 ip6_savecontrol(last, &opts, ip6, m); 2034 /* strip intermediate headers */ 2035 m_adj(m, off); 2036 if (sbappendaddr(&last->in6p_socket->so_rcv, 2037 (struct sockaddr *)&rip6src, m, opts) == 0) { 2038 m_freem(m); 2039 if (opts) 2040 m_freem(opts); 2041 } else 2042 sorwakeup(last->in6p_socket); 2043 } else { 2044 m_freem(m); 2045 ip6stat.ip6s_delivered--; 2046 } 2047 return IPPROTO_DONE; 2048} 2049 2050/* 2051 * Reflect the ip6 packet back to the source. 2052 * OFF points to the icmp6 header, counted from the top of the mbuf. 2053 * 2054 * Note: RFC 1885 required that an echo reply should be truncated if it 2055 * did not fit in with (return) path MTU, and KAME code supported the 2056 * behavior. However, as a clarification after the RFC, this limitation 2057 * was removed in a revised version of the spec, RFC 2463. We had kept the 2058 * old behavior, with a (non-default) ifdef block, while the new version of 2059 * the spec was an internet-draft status, and even after the new RFC was 2060 * published. But it would rather make sense to clean the obsoleted part 2061 * up, and to make the code simpler at this stage. 2062 */ 2063void 2064icmp6_reflect(m, off) 2065 struct mbuf *m; 2066 size_t off; 2067{ 2068 struct ip6_hdr *ip6; 2069 struct icmp6_hdr *icmp6; 2070 struct in6_ifaddr *ia; 2071 struct in6_addr t, *src = 0; 2072 int plen; 2073 int type, code; 2074 struct ifnet *outif = NULL; 2075 struct sockaddr_in6 sa6_src, sa6_dst; 2076 2077 /* too short to reflect */ 2078 if (off < sizeof(struct ip6_hdr)) { 2079 nd6log((LOG_DEBUG, 2080 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 2081 (u_long)off, (u_long)sizeof(struct ip6_hdr), 2082 __FILE__, __LINE__)); 2083 goto bad; 2084 } 2085 2086 /* 2087 * If there are extra headers between IPv6 and ICMPv6, strip 2088 * off that header first. 2089 */ 2090#ifdef DIAGNOSTIC 2091 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 2092 panic("assumption failed in icmp6_reflect"); 2093#endif 2094 if (off > sizeof(struct ip6_hdr)) { 2095 size_t l; 2096 struct ip6_hdr nip6; 2097 2098 l = off - sizeof(struct ip6_hdr); 2099 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 2100 m_adj(m, l); 2101 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 2102 if (m->m_len < l) { 2103 if ((m = m_pullup(m, l)) == NULL) 2104 return; 2105 } 2106 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 2107 } else /* off == sizeof(struct ip6_hdr) */ { 2108 size_t l; 2109 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 2110 if (m->m_len < l) { 2111 if ((m = m_pullup(m, l)) == NULL) 2112 return; 2113 } 2114 } 2115 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 2116 ip6 = mtod(m, struct ip6_hdr *); 2117 ip6->ip6_nxt = IPPROTO_ICMPV6; 2118 icmp6 = (struct icmp6_hdr *)(ip6 + 1); 2119 type = icmp6->icmp6_type; /* keep type for statistics */ 2120 code = icmp6->icmp6_code; /* ditto. */ 2121 2122 t = ip6->ip6_dst; 2123 /* 2124 * ip6_input() drops a packet if its src is multicast. 2125 * So, the src is never multicast. 2126 */ 2127 ip6->ip6_dst = ip6->ip6_src; 2128 2129 /* 2130 * XXX: make sure to embed scope zone information, using 2131 * already embedded IDs or the received interface (if any). 2132 * Note that rcvif may be NULL. 2133 * TODO: scoped routing case (XXX). 2134 */ 2135 bzero(&sa6_src, sizeof(sa6_src)); 2136 sa6_src.sin6_family = AF_INET6; 2137 sa6_src.sin6_len = sizeof(sa6_src); 2138 sa6_src.sin6_addr = ip6->ip6_dst; 2139 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); 2140 in6_embedscope(&sa6_src.sin6_addr, &sa6_src, NULL, NULL); 2141 ip6->ip6_dst = sa6_src.sin6_addr; 2142 2143 bzero(&sa6_dst, sizeof(sa6_dst)); 2144 sa6_dst.sin6_family = AF_INET6; 2145 sa6_dst.sin6_len = sizeof(sa6_dst); 2146 sa6_dst.sin6_addr = t; 2147 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); 2148 in6_embedscope(&t, &sa6_dst, NULL, NULL); 2149 2150 /* 2151 * If the incoming packet was addressed directly to us (i.e. unicast), 2152 * use dst as the src for the reply. 2153 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible 2154 * (for example) when we encounter an error while forwarding procedure 2155 * destined to a duplicated address of ours. 2156 */ 2157 for (ia = in6_ifaddr; ia; ia = ia->ia_next) 2158 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && 2159 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 2160 src = &t; 2161 break; 2162 } 2163 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { 2164 /* 2165 * This is the case if the dst is our link-local address 2166 * and the sender is also ourselves. 2167 */ 2168 src = &t; 2169 } 2170 2171 if (src == 0) { 2172 int e; 2173 struct route_in6 ro; 2174 2175 /* 2176 * This case matches to multicasts, our anycast, or unicasts 2177 * that we do not own. Select a source address based on the 2178 * source address of the erroneous packet. 2179 */ 2180 bzero(&ro, sizeof(ro)); 2181 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e); 2182 if (ro.ro_rt) { /* XXX: see comments in icmp6_mtudisc_update */ 2183 RTFREE(ro.ro_rt); /* XXX: we could use this */ 2184 } 2185 if (src == NULL) { 2186 nd6log((LOG_DEBUG, 2187 "icmp6_reflect: source can't be determined: " 2188 "dst=%s, error=%d\n", 2189 ip6_sprintf(&sa6_src.sin6_addr), e)); 2190 goto bad; 2191 } 2192 } 2193 2194 ip6->ip6_src = *src; 2195 2196 ip6->ip6_flow = 0; 2197 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2198 ip6->ip6_vfc |= IPV6_VERSION; 2199 ip6->ip6_nxt = IPPROTO_ICMPV6; 2200 if (m->m_pkthdr.rcvif) { 2201 /* XXX: This may not be the outgoing interface */ 2202 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 2203 } else 2204 ip6->ip6_hlim = ip6_defhlim; 2205 2206 icmp6->icmp6_cksum = 0; 2207 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2208 sizeof(struct ip6_hdr), plen); 2209 2210 /* 2211 * XXX option handling 2212 */ 2213 2214 m->m_flags &= ~(M_BCAST|M_MCAST); 2215#ifdef IPSEC 2216 /* Don't lookup socket */ 2217 (void)ipsec_setsocket(m, NULL); 2218#endif /* IPSEC */ 2219 2220 /* 2221 * To avoid a "too big" situation at an intermediate router 2222 * and the path MTU discovery process, specify the IPV6_MINMTU flag. 2223 * Note that only echo and node information replies are affected, 2224 * since the length of ICMP6 errors is limited to the minimum MTU. 2225 */ 2226 if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, &outif) != 0 && outif) 2227 icmp6_ifstat_inc(outif, ifs6_out_error); 2228 2229 if (outif) 2230 icmp6_ifoutstat_inc(outif, type, code); 2231 2232 return; 2233 2234 bad: 2235 m_freem(m); 2236 return; 2237} 2238 2239void 2240icmp6_fasttimo() 2241{ 2242 2243 mld6_fasttimeo(); 2244} 2245 2246static const char * 2247icmp6_redirect_diag(src6, dst6, tgt6) 2248 struct in6_addr *src6; 2249 struct in6_addr *dst6; 2250 struct in6_addr *tgt6; 2251{ 2252 static char buf[1024]; 2253 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2254 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); 2255 return buf; 2256} 2257 2258void 2259icmp6_redirect_input(m, off) 2260 struct mbuf *m; 2261 int off; 2262{ 2263 struct ifnet *ifp = m->m_pkthdr.rcvif; 2264 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 2265 struct nd_redirect *nd_rd; 2266 int icmp6len = ntohs(ip6->ip6_plen); 2267 char *lladdr = NULL; 2268 int lladdrlen = 0; 2269 u_char *redirhdr = NULL; 2270 int redirhdrlen = 0; 2271 struct rtentry *rt = NULL; 2272 int is_router; 2273 int is_onlink; 2274 struct in6_addr src6 = ip6->ip6_src; 2275 struct in6_addr redtgt6; 2276 struct in6_addr reddst6; 2277 union nd_opts ndopts; 2278 2279 if (!m || !ifp) 2280 return; 2281 2282 /* XXX if we are router, we don't update route by icmp6 redirect */ 2283 if (ip6_forwarding) 2284 goto freeit; 2285 if (!icmp6_rediraccept) 2286 goto freeit; 2287 2288#ifndef PULLDOWN_TEST 2289 IP6_EXTHDR_CHECK(m, off, icmp6len,); 2290 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 2291#else 2292 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 2293 if (nd_rd == NULL) { 2294 icmp6stat.icp6s_tooshort++; 2295 return; 2296 } 2297#endif 2298 redtgt6 = nd_rd->nd_rd_target; 2299 reddst6 = nd_rd->nd_rd_dst; 2300 2301 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 2302 redtgt6.s6_addr16[1] = htons(ifp->if_index); 2303 if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) 2304 reddst6.s6_addr16[1] = htons(ifp->if_index); 2305 2306 /* validation */ 2307 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 2308 nd6log((LOG_ERR, 2309 "ICMP6 redirect sent from %s rejected; " 2310 "must be from linklocal\n", ip6_sprintf(&src6))); 2311 goto bad; 2312 } 2313 if (ip6->ip6_hlim != 255) { 2314 nd6log((LOG_ERR, 2315 "ICMP6 redirect sent from %s rejected; " 2316 "hlim=%d (must be 255)\n", 2317 ip6_sprintf(&src6), ip6->ip6_hlim)); 2318 goto bad; 2319 } 2320 { 2321 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 2322 struct sockaddr_in6 sin6; 2323 struct in6_addr *gw6; 2324 2325 bzero(&sin6, sizeof(sin6)); 2326 sin6.sin6_family = AF_INET6; 2327 sin6.sin6_len = sizeof(struct sockaddr_in6); 2328 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 2329 rt = rtalloc1((struct sockaddr *)&sin6, 0); 2330 if (rt) { 2331 if (rt->rt_gateway == NULL || 2332 rt->rt_gateway->sa_family != AF_INET6) { 2333 nd6log((LOG_ERR, 2334 "ICMP6 redirect rejected; no route " 2335 "with inet6 gateway found for redirect dst: %s\n", 2336 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2337 RTFREE(rt); 2338 goto bad; 2339 } 2340 2341 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 2342 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 2343 nd6log((LOG_ERR, 2344 "ICMP6 redirect rejected; " 2345 "not equal to gw-for-src=%s (must be same): " 2346 "%s\n", 2347 ip6_sprintf(gw6), 2348 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2349 RTFREE(rt); 2350 goto bad; 2351 } 2352 } else { 2353 nd6log((LOG_ERR, 2354 "ICMP6 redirect rejected; " 2355 "no route found for redirect dst: %s\n", 2356 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2357 goto bad; 2358 } 2359 RTFREE(rt); 2360 rt = NULL; 2361 } 2362 if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 2363 nd6log((LOG_ERR, 2364 "ICMP6 redirect rejected; " 2365 "redirect dst must be unicast: %s\n", 2366 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2367 goto bad; 2368 } 2369 2370 is_router = is_onlink = 0; 2371 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 2372 is_router = 1; /* router case */ 2373 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 2374 is_onlink = 1; /* on-link destination case */ 2375 if (!is_router && !is_onlink) { 2376 nd6log((LOG_ERR, 2377 "ICMP6 redirect rejected; " 2378 "neither router case nor onlink case: %s\n", 2379 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2380 goto bad; 2381 } 2382 /* validation passed */ 2383 2384 icmp6len -= sizeof(*nd_rd); 2385 nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 2386 if (nd6_options(&ndopts) < 0) { 2387 nd6log((LOG_INFO, "icmp6_redirect_input: " 2388 "invalid ND option, rejected: %s\n", 2389 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2390 /* nd6_options have incremented stats */ 2391 goto freeit; 2392 } 2393 2394 if (ndopts.nd_opts_tgt_lladdr) { 2395 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 2396 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 2397 } 2398 2399 if (ndopts.nd_opts_rh) { 2400 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 2401 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 2402 } 2403 2404 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 2405 nd6log((LOG_INFO, 2406 "icmp6_redirect_input: lladdrlen mismatch for %s " 2407 "(if %d, icmp6 packet %d): %s\n", 2408 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, 2409 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2410 goto bad; 2411 } 2412 2413 /* RFC 2461 8.3 */ 2414 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2415 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 2416 2417 if (!is_onlink) { /* better router case. perform rtredirect. */ 2418 /* perform rtredirect */ 2419 struct sockaddr_in6 sdst; 2420 struct sockaddr_in6 sgw; 2421 struct sockaddr_in6 ssrc; 2422 unsigned long rtcount; 2423 struct rtentry *newrt = NULL; 2424 2425 /* 2426 * do not install redirect route, if the number of entries 2427 * is too much (> hiwat). note that, the node (= host) will 2428 * work just fine even if we do not install redirect route 2429 * (there will be additional hops, though). 2430 */ 2431 rtcount = rt_timer_count(icmp6_redirect_timeout_q); 2432 if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat) 2433 return; 2434 else if (0 <= icmp6_redirect_lowat && 2435 rtcount > icmp6_redirect_lowat) { 2436 /* 2437 * XXX nuke a victim, install the new one. 2438 */ 2439 } 2440 2441 bzero(&sdst, sizeof(sdst)); 2442 bzero(&sgw, sizeof(sgw)); 2443 bzero(&ssrc, sizeof(ssrc)); 2444 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 2445 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 2446 sizeof(struct sockaddr_in6); 2447 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 2448 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 2449 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 2450 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2451 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2452 (struct sockaddr *)&ssrc, 2453 &newrt); 2454 2455 if (newrt) { 2456 (void)rt_timer_add(newrt, icmp6_redirect_timeout, 2457 icmp6_redirect_timeout_q); 2458 rtfree(newrt); 2459 } 2460 } 2461 /* finally update cached route in each socket via pfctlinput */ 2462 { 2463 struct sockaddr_in6 sdst; 2464 2465 bzero(&sdst, sizeof(sdst)); 2466 sdst.sin6_family = AF_INET6; 2467 sdst.sin6_len = sizeof(struct sockaddr_in6); 2468 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 2469 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2470#ifdef IPSEC 2471 key_sa_routechange((struct sockaddr *)&sdst); 2472#endif 2473 } 2474 2475 freeit: 2476 m_freem(m); 2477 return; 2478 2479 bad: 2480 icmp6stat.icp6s_badredirect++; 2481 m_freem(m); 2482} 2483 2484void 2485icmp6_redirect_output(m0, rt) 2486 struct mbuf *m0; 2487 struct rtentry *rt; 2488{ 2489 struct ifnet *ifp; /* my outgoing interface */ 2490 struct in6_addr *ifp_ll6; 2491 struct in6_addr *nexthop; 2492 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 2493 struct mbuf *m = NULL; /* newly allocated one */ 2494 struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 2495 struct nd_redirect *nd_rd; 2496 size_t maxlen; 2497 u_char *p; 2498 struct sockaddr_in6 src_sa; 2499 2500 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 2501 2502 /* if we are not router, we don't send icmp6 redirect */ 2503 if (!ip6_forwarding || ip6_accept_rtadv) 2504 goto fail; 2505 2506 /* sanity check */ 2507 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 2508 goto fail; 2509 2510 /* 2511 * Address check: 2512 * the source address must identify a neighbor, and 2513 * the destination address must not be a multicast address 2514 * [RFC 2461, sec 8.2] 2515 */ 2516 sip6 = mtod(m0, struct ip6_hdr *); 2517 bzero(&src_sa, sizeof(src_sa)); 2518 src_sa.sin6_family = AF_INET6; 2519 src_sa.sin6_len = sizeof(src_sa); 2520 src_sa.sin6_addr = sip6->ip6_src; 2521 /* we don't currently use sin6_scope_id, but eventually use it */ 2522 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src); 2523 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 2524 goto fail; 2525 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 2526 goto fail; /* what should we do here? */ 2527 2528 /* rate limit */ 2529 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 2530 goto fail; 2531 2532 /* 2533 * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 2534 * we almost always ask for an mbuf cluster for simplicity. 2535 * (MHLEN < IPV6_MMTU is almost always true) 2536 */ 2537#if IPV6_MMTU >= MCLBYTES 2538# error assumption failed about IPV6_MMTU and MCLBYTES 2539#endif 2540 MGETHDR(m, M_DONTWAIT, MT_HEADER); 2541 if (m && IPV6_MMTU >= MHLEN) 2542 MCLGET(m, M_DONTWAIT); 2543 if (!m) 2544 goto fail; 2545 m->m_pkthdr.rcvif = NULL; 2546 m->m_len = 0; 2547 maxlen = M_TRAILINGSPACE(m); 2548 maxlen = min(IPV6_MMTU, maxlen); 2549 /* just for safety */ 2550 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 2551 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 2552 goto fail; 2553 } 2554 2555 { 2556 /* get ip6 linklocal address for ifp(my outgoing interface). */ 2557 struct in6_ifaddr *ia; 2558 if ((ia = in6ifa_ifpforlinklocal(ifp, 2559 IN6_IFF_NOTREADY| 2560 IN6_IFF_ANYCAST)) == NULL) 2561 goto fail; 2562 ifp_ll6 = &ia->ia_addr.sin6_addr; 2563 } 2564 2565 /* get ip6 linklocal address for the router. */ 2566 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 2567 struct sockaddr_in6 *sin6; 2568 sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 2569 nexthop = &sin6->sin6_addr; 2570 if (!IN6_IS_ADDR_LINKLOCAL(nexthop)) 2571 nexthop = NULL; 2572 } else 2573 nexthop = NULL; 2574 2575 /* ip6 */ 2576 ip6 = mtod(m, struct ip6_hdr *); 2577 ip6->ip6_flow = 0; 2578 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2579 ip6->ip6_vfc |= IPV6_VERSION; 2580 /* ip6->ip6_plen will be set later */ 2581 ip6->ip6_nxt = IPPROTO_ICMPV6; 2582 ip6->ip6_hlim = 255; 2583 /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 2584 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 2585 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 2586 2587 /* ND Redirect */ 2588 nd_rd = (struct nd_redirect *)(ip6 + 1); 2589 nd_rd->nd_rd_type = ND_REDIRECT; 2590 nd_rd->nd_rd_code = 0; 2591 nd_rd->nd_rd_reserved = 0; 2592 if (rt->rt_flags & RTF_GATEWAY) { 2593 /* 2594 * nd_rd->nd_rd_target must be a link-local address in 2595 * better router cases. 2596 */ 2597 if (!nexthop) 2598 goto fail; 2599 bcopy(nexthop, &nd_rd->nd_rd_target, 2600 sizeof(nd_rd->nd_rd_target)); 2601 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2602 sizeof(nd_rd->nd_rd_dst)); 2603 } else { 2604 /* make sure redtgt == reddst */ 2605 nexthop = &sip6->ip6_dst; 2606 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2607 sizeof(nd_rd->nd_rd_target)); 2608 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2609 sizeof(nd_rd->nd_rd_dst)); 2610 } 2611 2612 p = (u_char *)(nd_rd + 1); 2613 2614 { 2615 /* target lladdr option */ 2616 struct rtentry *rt_nexthop = NULL; 2617 int len; 2618 struct sockaddr_dl *sdl; 2619 struct nd_opt_hdr *nd_opt; 2620 char *lladdr; 2621 2622 rt_nexthop = nd6_lookup(nexthop, 0, ifp); 2623 if (!rt_nexthop) 2624 goto nolladdropt; 2625 len = sizeof(*nd_opt) + ifp->if_addrlen; 2626 len = (len + 7) & ~7; /* round by 8 */ 2627 /* safety check */ 2628 if (len + (p - (u_char *)ip6) > maxlen) 2629 goto nolladdropt; 2630 if (!(rt_nexthop->rt_flags & RTF_GATEWAY) && 2631 (rt_nexthop->rt_flags & RTF_LLINFO) && 2632 (rt_nexthop->rt_gateway->sa_family == AF_LINK) && 2633 (sdl = (struct sockaddr_dl *)rt_nexthop->rt_gateway) && 2634 sdl->sdl_alen) { 2635 nd_opt = (struct nd_opt_hdr *)p; 2636 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2637 nd_opt->nd_opt_len = len >> 3; 2638 lladdr = (char *)(nd_opt + 1); 2639 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 2640 p += len; 2641 } 2642 } 2643 nolladdropt:; 2644 2645 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 2646 2647 /* just to be safe */ 2648 if (m0->m_flags & M_DECRYPTED) 2649 goto noredhdropt; 2650 if (p - (u_char *)ip6 > maxlen) 2651 goto noredhdropt; 2652 2653 { 2654 /* redirected header option */ 2655 int len; 2656 struct nd_opt_rd_hdr *nd_opt_rh; 2657 2658 /* 2659 * compute the maximum size for icmp6 redirect header option. 2660 * XXX room for auth header? 2661 */ 2662 len = maxlen - (p - (u_char *)ip6); 2663 len &= ~7; 2664 2665 /* This is just for simplicity. */ 2666 if (m0->m_pkthdr.len != m0->m_len) { 2667 if (m0->m_next) { 2668 m_freem(m0->m_next); 2669 m0->m_next = NULL; 2670 } 2671 m0->m_pkthdr.len = m0->m_len; 2672 } 2673 2674 /* 2675 * Redirected header option spec (RFC2461 4.6.3) talks nothing 2676 * about padding/truncate rule for the original IP packet. 2677 * From the discussion on IPv6imp in Feb 1999, the consensus was: 2678 * - "attach as much as possible" is the goal 2679 * - pad if not aligned (original size can be guessed by original 2680 * ip6 header) 2681 * Following code adds the padding if it is simple enough, 2682 * and truncates if not. 2683 */ 2684 if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2685 panic("assumption failed in %s:%d\n", __FILE__, __LINE__); 2686 2687 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2688 /* not enough room, truncate */ 2689 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh); 2690 } else { 2691 /* enough room, pad or truncate */ 2692 size_t extra; 2693 2694 extra = m0->m_pkthdr.len % 8; 2695 if (extra) { 2696 /* pad if easy enough, truncate if not */ 2697 if (8 - extra <= M_TRAILINGSPACE(m0)) { 2698 /* pad */ 2699 m0->m_len += (8 - extra); 2700 m0->m_pkthdr.len += (8 - extra); 2701 } else { 2702 /* truncate */ 2703 m0->m_pkthdr.len -= extra; 2704 m0->m_len -= extra; 2705 } 2706 } 2707 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2708 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh); 2709 } 2710 2711 nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2712 bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2713 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2714 nd_opt_rh->nd_opt_rh_len = len >> 3; 2715 p += sizeof(*nd_opt_rh); 2716 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 2717 2718 /* connect m0 to m */ 2719 m->m_next = m0; 2720 m->m_pkthdr.len = m->m_len + m0->m_len; 2721 } 2722noredhdropt:; 2723 2724 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src)) 2725 sip6->ip6_src.s6_addr16[1] = 0; 2726 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst)) 2727 sip6->ip6_dst.s6_addr16[1] = 0; 2728#if 0 2729 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 2730 ip6->ip6_src.s6_addr16[1] = 0; 2731 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) 2732 ip6->ip6_dst.s6_addr16[1] = 0; 2733#endif 2734 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target)) 2735 nd_rd->nd_rd_target.s6_addr16[1] = 0; 2736 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst)) 2737 nd_rd->nd_rd_dst.s6_addr16[1] = 0; 2738 2739 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 2740 2741 nd_rd->nd_rd_cksum = 0; 2742 nd_rd->nd_rd_cksum 2743 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen)); 2744 2745 /* send the packet to outside... */ 2746#ifdef IPSEC 2747 /* Don't lookup socket */ 2748 (void)ipsec_setsocket(m, NULL); 2749#endif /* IPSEC */ 2750 if (ip6_output(m, NULL, NULL, 0, NULL, NULL) != 0) 2751 icmp6_ifstat_inc(ifp, ifs6_out_error); 2752 2753 icmp6_ifstat_inc(ifp, ifs6_out_msg); 2754 icmp6_ifstat_inc(ifp, ifs6_out_redirect); 2755 icmp6stat.icp6s_outhist[ND_REDIRECT]++; 2756 2757 return; 2758 2759fail: 2760 if (m) 2761 m_freem(m); 2762 if (m0) 2763 m_freem(m0); 2764} 2765 2766/* 2767 * ICMPv6 socket option processing. 2768 */ 2769int 2770icmp6_ctloutput(op, so, level, optname, mp) 2771 int op; 2772 struct socket *so; 2773 int level, optname; 2774 struct mbuf **mp; 2775{ 2776 int error = 0; 2777 int optlen; 2778 struct in6pcb *in6p = sotoin6pcb(so); 2779 struct mbuf *m = *mp; 2780 2781 optlen = m ? m->m_len : 0; 2782 2783 if (level != IPPROTO_ICMPV6) { 2784 if (op == PRCO_SETOPT && m) 2785 (void)m_free(m); 2786 return EINVAL; 2787 } 2788 2789 switch (op) { 2790 case PRCO_SETOPT: 2791 switch (optname) { 2792 case ICMP6_FILTER: 2793 { 2794 struct icmp6_filter *p; 2795 2796 if (optlen != sizeof(*p)) { 2797 error = EMSGSIZE; 2798 break; 2799 } 2800 p = mtod(m, struct icmp6_filter *); 2801 if (!p || !in6p->in6p_icmp6filt) { 2802 error = EINVAL; 2803 break; 2804 } 2805 bcopy(p, in6p->in6p_icmp6filt, 2806 sizeof(struct icmp6_filter)); 2807 error = 0; 2808 break; 2809 } 2810 2811 default: 2812 error = ENOPROTOOPT; 2813 break; 2814 } 2815 if (m) 2816 (void)m_freem(m); 2817 break; 2818 2819 case PRCO_GETOPT: 2820 switch (optname) { 2821 case ICMP6_FILTER: 2822 { 2823 struct icmp6_filter *p; 2824 2825 if (!in6p->in6p_icmp6filt) { 2826 error = EINVAL; 2827 break; 2828 } 2829 *mp = m = m_get(M_WAIT, MT_SOOPTS); 2830 m->m_len = sizeof(struct icmp6_filter); 2831 p = mtod(m, struct icmp6_filter *); 2832 bcopy(in6p->in6p_icmp6filt, p, 2833 sizeof(struct icmp6_filter)); 2834 error = 0; 2835 break; 2836 } 2837 2838 default: 2839 error = ENOPROTOOPT; 2840 break; 2841 } 2842 break; 2843 } 2844 2845 return(error); 2846} 2847 2848/* 2849 * Perform rate limit check. 2850 * Returns 0 if it is okay to send the icmp6 packet. 2851 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 2852 * limitation. 2853 * 2854 * XXX per-destination/type check necessary? 2855 */ 2856static int 2857icmp6_ratelimit(dst, type, code) 2858 const struct in6_addr *dst; /* not used at this moment */ 2859 const int type; /* not used at this moment */ 2860 const int code; /* not used at this moment */ 2861{ 2862 int ret; 2863 2864 ret = 0; /* okay to send */ 2865 2866 /* PPS limit */ 2867 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 2868 icmp6errppslim)) { 2869 /* The packet is subject to rate limit */ 2870 ret++; 2871 } 2872 2873 return ret; 2874} 2875 2876static struct rtentry * 2877icmp6_mtudisc_clone(dst) 2878 struct sockaddr *dst; 2879{ 2880 struct rtentry *rt; 2881 int error; 2882 2883 rt = rtalloc1(dst, 1); 2884 if (rt == 0) 2885 return NULL; 2886 2887 /* If we didn't get a host route, allocate one */ 2888 if ((rt->rt_flags & RTF_HOST) == 0) { 2889 struct rtentry *nrt; 2890 2891 error = rtrequest((int) RTM_ADD, dst, 2892 (struct sockaddr *) rt->rt_gateway, 2893 (struct sockaddr *) 0, 2894 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt); 2895 if (error) { 2896 rtfree(rt); 2897 return NULL; 2898 } 2899 nrt->rt_rmx = rt->rt_rmx; 2900 rtfree(rt); 2901 rt = nrt; 2902 } 2903 error = rt_timer_add(rt, icmp6_mtudisc_timeout, 2904 icmp6_mtudisc_timeout_q); 2905 if (error) { 2906 rtfree(rt); 2907 return NULL; 2908 } 2909 2910 return rt; /* caller need to call rtfree() */ 2911} 2912 2913static void 2914icmp6_mtudisc_timeout(rt, r) 2915 struct rtentry *rt; 2916 struct rttimer *r; 2917{ 2918 if (rt == NULL) 2919 panic("icmp6_mtudisc_timeout: bad route to timeout"); 2920 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) == 2921 (RTF_DYNAMIC | RTF_HOST)) { 2922 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt), 2923 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 2924 } else { 2925 if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) 2926 rt->rt_rmx.rmx_mtu = 0; 2927 } 2928} 2929 2930static void 2931icmp6_redirect_timeout(rt, r) 2932 struct rtentry *rt; 2933 struct rttimer *r; 2934{ 2935 if (rt == NULL) 2936 panic("icmp6_redirect_timeout: bad route to timeout"); 2937 if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) == 2938 (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) { 2939 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt), 2940 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 2941 } 2942} 2943 2944int 2945icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 2946 int *name; 2947 u_int namelen; 2948 void *oldp; 2949 size_t *oldlenp; 2950 void *newp; 2951 size_t newlen; 2952{ 2953 2954 /* All sysctl names at this level are terminal. */ 2955 if (namelen != 1) 2956 return ENOTDIR; 2957 2958 switch (name[0]) { 2959 2960 case ICMPV6CTL_REDIRACCEPT: 2961 return sysctl_int(oldp, oldlenp, newp, newlen, 2962 &icmp6_rediraccept); 2963 case ICMPV6CTL_REDIRTIMEOUT: 2964 return sysctl_int(oldp, oldlenp, newp, newlen, 2965 &icmp6_redirtimeout); 2966 case ICMPV6CTL_STATS: 2967 return sysctl_rdstruct(oldp, oldlenp, newp, 2968 &icmp6stat, sizeof(icmp6stat)); 2969 case ICMPV6CTL_ND6_PRUNE: 2970 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_prune); 2971 case ICMPV6CTL_ND6_DELAY: 2972 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_delay); 2973 case ICMPV6CTL_ND6_UMAXTRIES: 2974 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_umaxtries); 2975 case ICMPV6CTL_ND6_MMAXTRIES: 2976 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_mmaxtries); 2977 case ICMPV6CTL_ND6_USELOOPBACK: 2978 return sysctl_int(oldp, oldlenp, newp, newlen, 2979 &nd6_useloopback); 2980 case ICMPV6CTL_NODEINFO: 2981 return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6_nodeinfo); 2982 case ICMPV6CTL_ERRPPSLIMIT: 2983 return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6errppslim); 2984 case ICMPV6CTL_ND6_MAXNUDHINT: 2985 return sysctl_int(oldp, oldlenp, newp, newlen, 2986 &nd6_maxnudhint); 2987 case ICMPV6CTL_MTUDISC_HIWAT: 2988 return sysctl_int(oldp, oldlenp, newp, newlen, 2989 &icmp6_mtudisc_hiwat); 2990 case ICMPV6CTL_MTUDISC_LOWAT: 2991 return sysctl_int(oldp, oldlenp, newp, newlen, 2992 &icmp6_mtudisc_lowat); 2993 case ICMPV6CTL_ND6_DEBUG: 2994 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_debug); 2995 case ICMPV6CTL_ND6_DRLIST: 2996 case ICMPV6CTL_ND6_PRLIST: 2997 return nd6_sysctl(name[0], oldp, oldlenp, newp, newlen); 2998 default: 2999 return ENOPROTOOPT; 3000 } 3001 /* NOTREACHED */ 3002} 3003