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