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