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