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