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