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