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