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