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