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