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