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