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