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