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 in6_clearscope(&oip6->ip6_src); 359 in6_clearscope(&oip6->ip6_dst); 360 361 icmp6 = (struct icmp6_hdr *)(nip6 + 1); 362 icmp6->icmp6_type = type; 363 icmp6->icmp6_code = code; 364 icmp6->icmp6_pptr = htonl((u_int32_t)param); 365 366 /* 367 * icmp6_reflect() is designed to be in the input path. 368 * icmp6_error() can be called from both input and outut path, 369 * and if we are in output path rcvif could contain bogus value. 370 * clear m->m_pkthdr.rcvif for safety, we should have enough scope 371 * information in ip header (nip6). 372 */ 373 m->m_pkthdr.rcvif = NULL; 374 375 icmp6stat.icp6s_outhist[type]++; 376 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 377 378 return; 379 380 freeit: 381 /* 382 * If we can't tell wheter or not we can generate ICMP6, free it. 383 */ 384 m_freem(m); 385 } 386 387 /* 388 * Process a received ICMP6 message. 389 */ 390 int 391 icmp6_input(struct mbuf **mp, int *offp, int proto) 392 { 393 struct mbuf *m = *mp, *n; 394 struct ip6_hdr *ip6, *nip6; 395 struct icmp6_hdr *icmp6, *nicmp6; 396 int off = *offp; 397 int icmp6len = m->m_pkthdr.len - *offp; 398 int code, sum, noff; 399 400 #ifndef PULLDOWN_TEST 401 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 402 /* m might change if M_LOOP. So, call mtod after this */ 403 #endif 404 405 /* 406 * Locate icmp6 structure in mbuf, and check 407 * that not corrupted and of at least minimum length 408 */ 409 410 ip6 = mtod(m, struct ip6_hdr *); 411 if (icmp6len < sizeof(struct icmp6_hdr)) { 412 icmp6stat.icp6s_tooshort++; 413 goto freeit; 414 } 415 416 /* 417 * calculate the checksum 418 */ 419 #ifndef PULLDOWN_TEST 420 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 421 #else 422 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 423 if (icmp6 == NULL) { 424 icmp6stat.icp6s_tooshort++; 425 return IPPROTO_DONE; 426 } 427 #endif 428 code = icmp6->icmp6_code; 429 430 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 431 nd6log((LOG_ERR, 432 "ICMP6 checksum error(%d|%x) %s\n", 433 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); 434 icmp6stat.icp6s_checksum++; 435 goto freeit; 436 } 437 438 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 439 /* 440 * Deliver very specific ICMP6 type only. 441 * This is important to deilver TOOBIG. Otherwise PMTUD 442 * will not work. 443 */ 444 switch (icmp6->icmp6_type) { 445 case ICMP6_DST_UNREACH: 446 case ICMP6_PACKET_TOO_BIG: 447 case ICMP6_TIME_EXCEEDED: 448 break; 449 default: 450 goto freeit; 451 } 452 } 453 454 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 455 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 456 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 457 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 458 459 switch (icmp6->icmp6_type) { 460 case ICMP6_DST_UNREACH: 461 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 462 switch (code) { 463 case ICMP6_DST_UNREACH_NOROUTE: 464 code = PRC_UNREACH_NET; 465 break; 466 case ICMP6_DST_UNREACH_ADMIN: 467 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 468 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 469 break; 470 case ICMP6_DST_UNREACH_ADDR: 471 code = PRC_HOSTDEAD; 472 break; 473 #ifdef COMPAT_RFC1885 474 case ICMP6_DST_UNREACH_NOTNEIGHBOR: 475 code = PRC_UNREACH_SRCFAIL; 476 break; 477 #else 478 case ICMP6_DST_UNREACH_BEYONDSCOPE: 479 /* I mean "source address was incorrect." */ 480 code = PRC_PARAMPROB; 481 break; 482 #endif 483 case ICMP6_DST_UNREACH_NOPORT: 484 code = PRC_UNREACH_PORT; 485 break; 486 default: 487 goto badcode; 488 } 489 goto deliver; 490 break; 491 492 case ICMP6_PACKET_TOO_BIG: 493 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 494 if (code != 0) 495 goto badcode; 496 497 code = PRC_MSGSIZE; 498 499 /* 500 * Updating the path MTU will be done after examining 501 * intermediate extension headers. 502 */ 503 goto deliver; 504 break; 505 506 case ICMP6_TIME_EXCEEDED: 507 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 508 switch (code) { 509 case ICMP6_TIME_EXCEED_TRANSIT: 510 case ICMP6_TIME_EXCEED_REASSEMBLY: 511 code += PRC_TIMXCEED_INTRANS; 512 break; 513 default: 514 goto badcode; 515 } 516 goto deliver; 517 break; 518 519 case ICMP6_PARAM_PROB: 520 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 521 switch (code) { 522 case ICMP6_PARAMPROB_NEXTHEADER: 523 code = PRC_UNREACH_PROTOCOL; 524 break; 525 case ICMP6_PARAMPROB_HEADER: 526 case ICMP6_PARAMPROB_OPTION: 527 code = PRC_PARAMPROB; 528 break; 529 default: 530 goto badcode; 531 } 532 goto deliver; 533 break; 534 535 case ICMP6_ECHO_REQUEST: 536 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 537 if (code != 0) 538 goto badcode; 539 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 540 /* Give up remote */ 541 break; 542 } 543 if ((n->m_flags & M_EXT) || 544 n->m_len < off + sizeof(struct icmp6_hdr)) { 545 struct mbuf *n0 = n; 546 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 547 int n0len; 548 549 /* 550 * Prepare an internal mbuf. m_pullup() doesn't 551 * always copy the length we specified. 552 */ 553 if (maxlen >= MCLBYTES) { 554 /* Give up remote */ 555 m_freem(n0); 556 break; 557 } 558 n = m_getb(maxlen, M_NOWAIT, n0->m_type, M_PKTHDR); 559 if (n == NULL) { 560 /* Give up remote */ 561 m_freem(n0); 562 break; 563 } 564 n0len = n0->m_pkthdr.len; /* save for use below */ 565 M_MOVE_PKTHDR(n, n0); 566 /* 567 * Copy IPv6 and ICMPv6 only. 568 */ 569 nip6 = mtod(n, struct ip6_hdr *); 570 bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 571 nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 572 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 573 noff = sizeof(struct ip6_hdr); 574 /* new mbuf contains only ipv6+icmpv6 headers */ 575 n->m_len = noff + sizeof(struct icmp6_hdr); 576 /* 577 * Adjust mbuf. ip6_plen will be adjusted in 578 * ip6_output(). 579 */ 580 m_adj(n0, off + sizeof(struct icmp6_hdr)); 581 /* recalculate complete packet size */ 582 n->m_pkthdr.len = n0len + (noff - off); 583 n->m_next = n0; 584 } else { 585 nip6 = mtod(n, struct ip6_hdr *); 586 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); 587 noff = off; 588 } 589 nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 590 nicmp6->icmp6_code = 0; 591 if (n) { 592 icmp6stat.icp6s_reflect++; 593 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 594 icmp6_reflect(n, noff); 595 } 596 break; 597 598 case ICMP6_ECHO_REPLY: 599 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 600 if (code != 0) 601 goto badcode; 602 break; 603 604 case MLD_LISTENER_QUERY: 605 case MLD_LISTENER_REPORT: 606 if (icmp6len < sizeof(struct mld_hdr)) 607 goto badlen; 608 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 609 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 610 else 611 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 612 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 613 /* give up local */ 614 mld6_input(m, off); 615 m = NULL; 616 goto freeit; 617 } 618 mld6_input(n, off); 619 /* m stays. */ 620 break; 621 622 case MLD_LISTENER_DONE: 623 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 624 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 625 goto badlen; 626 break; /* nothing to be done in kernel */ 627 628 case MLD_MTRACE_RESP: 629 case MLD_MTRACE: 630 /* XXX: these two are experimental. not officially defind. */ 631 /* XXX: per-interface statistics? */ 632 break; /* just pass it to applications */ 633 634 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 635 { 636 enum { WRU, FQDN } mode; 637 638 if (!icmp6_nodeinfo) 639 break; 640 641 if (icmp6len == sizeof(struct icmp6_hdr) + 4) 642 mode = WRU; 643 else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 644 mode = FQDN; 645 else 646 goto badlen; 647 648 #define hostnamelen strlen(hostname) 649 if (mode == FQDN) { 650 #ifndef PULLDOWN_TEST 651 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 652 IPPROTO_DONE); 653 #endif 654 n = m_copy(m, 0, M_COPYALL); 655 if (n) 656 n = ni6_input(n, off); 657 /* XXX meaningless if n == NULL */ 658 noff = sizeof(struct ip6_hdr); 659 } else { 660 u_char *p; 661 int maxlen, maxhlen; 662 663 if ((icmp6_nodeinfo & 5) != 5) 664 break; 665 666 if (code != 0) 667 goto badcode; 668 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 669 if (maxlen >= MCLBYTES) { 670 /* Give up remote */ 671 break; 672 } 673 n = m_getb(maxlen, M_NOWAIT, m->m_type, M_PKTHDR); 674 if (n == NULL) { 675 /* Give up remote */ 676 break; 677 } 678 if (!m_dup_pkthdr(n, m, M_NOWAIT)) { 679 /* 680 * Previous code did a blind M_COPY_PKTHDR 681 * and said "just for rcvif". If true, then 682 * we could tolerate the dup failing (due to 683 * the deep copy of the tag chain). For now 684 * be conservative and just fail. 685 */ 686 m_free(n); 687 break; 688 } 689 n->m_len = 0; 690 maxhlen = M_TRAILINGSPACE(n) - maxlen; 691 if (maxhlen > hostnamelen) 692 maxhlen = hostnamelen; 693 /* 694 * Copy IPv6 and ICMPv6 only. 695 */ 696 nip6 = mtod(n, struct ip6_hdr *); 697 bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 698 nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 699 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 700 p = (u_char *)(nicmp6 + 1); 701 bzero(p, 4); 702 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 703 noff = sizeof(struct ip6_hdr); 704 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 705 sizeof(struct icmp6_hdr) + 4 + maxhlen; 706 nicmp6->icmp6_type = ICMP6_WRUREPLY; 707 nicmp6->icmp6_code = 0; 708 } 709 #undef hostnamelen 710 if (n) { 711 icmp6stat.icp6s_reflect++; 712 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 713 icmp6_reflect(n, noff); 714 } 715 break; 716 } 717 718 case ICMP6_WRUREPLY: 719 if (code != 0) 720 goto badcode; 721 break; 722 723 case ND_ROUTER_SOLICIT: 724 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 725 if (code != 0) 726 goto badcode; 727 if (icmp6len < sizeof(struct nd_router_solicit)) 728 goto badlen; 729 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 730 /* give up local */ 731 nd6_rs_input(m, off, icmp6len); 732 m = NULL; 733 goto freeit; 734 } 735 nd6_rs_input(n, off, icmp6len); 736 /* m stays. */ 737 break; 738 739 case ND_ROUTER_ADVERT: 740 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 741 if (code != 0) 742 goto badcode; 743 if (icmp6len < sizeof(struct nd_router_advert)) 744 goto badlen; 745 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 746 /* give up local */ 747 nd6_ra_input(m, off, icmp6len); 748 m = NULL; 749 goto freeit; 750 } 751 nd6_ra_input(n, off, icmp6len); 752 /* m stays. */ 753 break; 754 755 case ND_NEIGHBOR_SOLICIT: 756 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 757 if (code != 0) 758 goto badcode; 759 if (icmp6len < sizeof(struct nd_neighbor_solicit)) 760 goto badlen; 761 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 762 /* give up local */ 763 nd6_ns_input(m, off, icmp6len); 764 m = NULL; 765 goto freeit; 766 } 767 nd6_ns_input(n, off, icmp6len); 768 /* m stays. */ 769 break; 770 771 case ND_NEIGHBOR_ADVERT: 772 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 773 if (code != 0) 774 goto badcode; 775 if (icmp6len < sizeof(struct nd_neighbor_advert)) 776 goto badlen; 777 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 778 /* give up local */ 779 nd6_na_input(m, off, icmp6len); 780 m = NULL; 781 goto freeit; 782 } 783 nd6_na_input(n, off, icmp6len); 784 /* m stays. */ 785 break; 786 787 case ND_REDIRECT: 788 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 789 if (code != 0) 790 goto badcode; 791 if (icmp6len < sizeof(struct nd_redirect)) 792 goto badlen; 793 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 794 /* give up local */ 795 icmp6_redirect_input(m, off); 796 m = NULL; 797 goto freeit; 798 } 799 icmp6_redirect_input(n, off); 800 /* m stays. */ 801 break; 802 803 case ICMP6_ROUTER_RENUMBERING: 804 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 805 code != ICMP6_ROUTER_RENUMBERING_RESULT) 806 goto badcode; 807 if (icmp6len < sizeof(struct icmp6_router_renum)) 808 goto badlen; 809 break; 810 811 default: 812 nd6log((LOG_DEBUG, 813 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 814 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), 815 ip6_sprintf(&ip6->ip6_dst), 816 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 817 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 818 /* ICMPv6 error: MUST deliver it by spec... */ 819 code = PRC_NCMDS; 820 /* deliver */ 821 } else { 822 /* ICMPv6 informational: MUST not deliver */ 823 break; 824 } 825 deliver: 826 if (icmp6_notify_error(m, off, icmp6len, code)) { 827 /* In this case, m should've been freed. */ 828 return (IPPROTO_DONE); 829 } 830 break; 831 832 badcode: 833 icmp6stat.icp6s_badcode++; 834 break; 835 836 badlen: 837 icmp6stat.icp6s_badlen++; 838 break; 839 } 840 841 /* deliver the packet to appropriate sockets */ 842 icmp6_rip6_input(&m, *offp); 843 844 return IPPROTO_DONE; 845 846 freeit: 847 m_freem(m); 848 return IPPROTO_DONE; 849 } 850 851 static int 852 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code) 853 { 854 struct icmp6_hdr *icmp6; 855 struct ip6_hdr *eip6; 856 u_int32_t notifymtu; 857 struct sockaddr_in6 icmp6src, icmp6dst; 858 859 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 860 icmp6stat.icp6s_tooshort++; 861 goto freeit; 862 } 863 #ifndef PULLDOWN_TEST 864 IP6_EXTHDR_CHECK(m, off, 865 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), 866 -1); 867 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 868 #else 869 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 870 sizeof(*icmp6) + sizeof(struct ip6_hdr)); 871 if (icmp6 == NULL) { 872 icmp6stat.icp6s_tooshort++; 873 return (-1); 874 } 875 #endif 876 eip6 = (struct ip6_hdr *)(icmp6 + 1); 877 878 /* Detect the upper level protocol */ 879 { 880 u_int8_t nxt = eip6->ip6_nxt; 881 int eoff = off + sizeof(struct icmp6_hdr) + 882 sizeof(struct ip6_hdr); 883 struct ip6ctlparam ip6cp; 884 struct in6_addr *finaldst = NULL; 885 int icmp6type = icmp6->icmp6_type; 886 struct ip6_frag *fh; 887 struct ip6_rthdr *rth; 888 int rthlen; 889 890 while (1) { /* XXX: should avoid infinite loop explicitly? */ 891 struct ip6_ext *eh; 892 893 switch (nxt) { 894 case IPPROTO_HOPOPTS: 895 case IPPROTO_DSTOPTS: 896 case IPPROTO_AH: 897 #ifndef PULLDOWN_TEST 898 IP6_EXTHDR_CHECK(m, 0, 899 eoff + sizeof(struct ip6_ext), -1); 900 eh = (struct ip6_ext *) 901 (mtod(m, caddr_t) + eoff); 902 #else 903 IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 904 eoff, sizeof(*eh)); 905 if (eh == NULL) { 906 icmp6stat.icp6s_tooshort++; 907 return (-1); 908 } 909 #endif 910 911 if (nxt == IPPROTO_AH) 912 eoff += (eh->ip6e_len + 2) << 2; 913 else 914 eoff += (eh->ip6e_len + 1) << 3; 915 nxt = eh->ip6e_nxt; 916 break; 917 case IPPROTO_ROUTING: 918 /* 919 * When the erroneous packet contains a 920 * routing header, we should examine the 921 * header to determine the final destination. 922 * Otherwise, we can't properly update 923 * information that depends on the final 924 * destination (e.g. path MTU). 925 */ 926 #ifndef PULLDOWN_TEST 927 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), 928 -1); 929 rth = (struct ip6_rthdr *)(mtod(m, caddr_t) 930 + eoff); 931 #else 932 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 933 eoff, sizeof(*rth)); 934 if (rth == NULL) { 935 icmp6stat.icp6s_tooshort++; 936 return (-1); 937 } 938 #endif 939 rthlen = (rth->ip6r_len + 1) << 3; 940 eoff += rthlen; 941 nxt = rth->ip6r_nxt; 942 break; 943 case IPPROTO_FRAGMENT: 944 #ifndef PULLDOWN_TEST 945 IP6_EXTHDR_CHECK(m, 0, eoff + 946 sizeof(struct ip6_frag), 947 -1); 948 fh = (struct ip6_frag *)(mtod(m, caddr_t) 949 + eoff); 950 #else 951 IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 952 eoff, sizeof(*fh)); 953 if (fh == NULL) { 954 icmp6stat.icp6s_tooshort++; 955 return (-1); 956 } 957 #endif 958 /* 959 * Data after a fragment header is meaningless 960 * unless it is the first fragment, but 961 * we'll go to the notify label for path MTU 962 * discovery. 963 */ 964 if (fh->ip6f_offlg & IP6F_OFF_MASK) 965 goto notify; 966 967 eoff += sizeof(struct ip6_frag); 968 nxt = fh->ip6f_nxt; 969 break; 970 default: 971 /* 972 * This case includes ESP and the No Next 973 * Header. In such cases going to the notify 974 * label does not have any meaning 975 * (i.e. pr_ctlinput will be NULL), but we go 976 * anyway since we might have to update 977 * path MTU information. 978 */ 979 goto notify; 980 } 981 } 982 notify: 983 #ifndef PULLDOWN_TEST 984 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 985 #else 986 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 987 sizeof(*icmp6) + sizeof(struct ip6_hdr)); 988 if (icmp6 == NULL) { 989 icmp6stat.icp6s_tooshort++; 990 return (-1); 991 } 992 #endif 993 994 eip6 = (struct ip6_hdr *)(icmp6 + 1); 995 bzero(&icmp6dst, sizeof(icmp6dst)); 996 icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 997 icmp6dst.sin6_family = AF_INET6; 998 if (finaldst == NULL) 999 icmp6dst.sin6_addr = eip6->ip6_dst; 1000 else 1001 icmp6dst.sin6_addr = *finaldst; 1002 if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6dst.sin6_addr, 1003 &icmp6dst.sin6_scope_id)) 1004 goto freeit; 1005 if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst, 1006 NULL, NULL)) { 1007 /* should be impossbile */ 1008 nd6log((LOG_DEBUG, 1009 "icmp6_notify_error: in6_embedscope failed\n")); 1010 goto freeit; 1011 } 1012 1013 /* 1014 * retrieve parameters from the inner IPv6 header, and convert 1015 * them into sockaddr structures. 1016 */ 1017 bzero(&icmp6src, sizeof(icmp6src)); 1018 icmp6src.sin6_len = sizeof(struct sockaddr_in6); 1019 icmp6src.sin6_family = AF_INET6; 1020 icmp6src.sin6_addr = eip6->ip6_src; 1021 if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6src.sin6_addr, 1022 &icmp6src.sin6_scope_id)) 1023 goto freeit; 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 if (in6_addr2zoneid(m->m_pkthdr.rcvif, 1219 &sin6.sin6_addr, &sin6.sin6_scope_id)) 1220 goto bad; 1221 in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL); 1222 bzero(&sin6_d, sizeof(sin6_d)); 1223 sin6_d.sin6_family = AF_INET6; /* not used, actually */ 1224 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */ 1225 sin6_d.sin6_addr = ip6->ip6_dst; 1226 if (in6_addr2zoneid(m->m_pkthdr.rcvif, 1227 &ip6->ip6_dst, &sin6_d.sin6_scope_id)) 1228 goto bad; 1229 in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL); 1230 subj = (char *)&sin6; 1231 if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d)) 1232 break; 1233 1234 /* 1235 * XXX if we are to allow other cases, we should really 1236 * be careful about scope here. 1237 * basically, we should disallow queries toward IPv6 1238 * destination X with subject Y, if scope(X) > scope(Y). 1239 * if we allow scope(X) > scope(Y), it will result in 1240 * information leakage across scope boundary. 1241 */ 1242 goto bad; 1243 1244 case ICMP6_NI_SUBJ_FQDN: 1245 /* 1246 * Validate Subject name with gethostname(3). 1247 * 1248 * The behavior may need some debate, since: 1249 * - we are not sure if the node has FQDN as 1250 * hostname (returned by gethostname(3)). 1251 * - the code does wildcard match for truncated names. 1252 * however, we are not sure if we want to perform 1253 * wildcard match, if gethostname(3) side has 1254 * truncated hostname. 1255 */ 1256 n = ni6_nametodns(hostname, hostnamelen, 0); 1257 if (!n || n->m_next || n->m_len == 0) 1258 goto bad; 1259 IP6_EXTHDR_GET(subj, char *, m, 1260 off + sizeof(struct icmp6_nodeinfo), subjlen); 1261 if (subj == NULL) 1262 goto bad; 1263 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1264 n->m_len)) { 1265 goto bad; 1266 } 1267 m_freem(n); 1268 n = NULL; 1269 break; 1270 1271 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 1272 default: 1273 goto bad; 1274 } 1275 break; 1276 } 1277 1278 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 1279 switch (qtype) { 1280 case NI_QTYPE_FQDN: 1281 if ((icmp6_nodeinfo & 1) == 0) 1282 goto bad; 1283 break; 1284 case NI_QTYPE_NODEADDR: 1285 if ((icmp6_nodeinfo & 2) == 0) 1286 goto bad; 1287 break; 1288 } 1289 1290 /* guess reply length */ 1291 switch (qtype) { 1292 case NI_QTYPE_NOOP: 1293 break; /* no reply data */ 1294 case NI_QTYPE_SUPTYPES: 1295 replylen += sizeof(u_int32_t); 1296 break; 1297 case NI_QTYPE_FQDN: 1298 /* XXX will append an mbuf */ 1299 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1300 break; 1301 case NI_QTYPE_NODEADDR: 1302 addrs = ni6_addrs(ni6, m, &ifp, subj); 1303 if ((replylen += addrs * (sizeof(struct in6_addr) + 1304 sizeof(u_int32_t))) > MCLBYTES) 1305 replylen = MCLBYTES; /* XXX: will truncate pkt later */ 1306 break; 1307 default: 1308 /* 1309 * XXX: We must return a reply with the ICMP6 code 1310 * `unknown Qtype' in this case. However we regard the case 1311 * as an FQDN query for backward compatibility. 1312 * Older versions set a random value to this field, 1313 * so it rarely varies in the defined qtypes. 1314 * But the mechanism is not reliable... 1315 * maybe we should obsolete older versions. 1316 */ 1317 qtype = NI_QTYPE_FQDN; 1318 /* XXX will append an mbuf */ 1319 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1320 oldfqdn++; 1321 break; 1322 } 1323 1324 /* allocate an mbuf to reply. */ 1325 if (replylen > MCLBYTES) { 1326 /* 1327 * XXX: should we try to allocate more? But MCLBYTES 1328 * is probably much larger than IPV6_MMTU... 1329 */ 1330 goto bad; 1331 } 1332 n = m_getb(replylen, M_NOWAIT, m->m_type, M_PKTHDR); 1333 if (n == NULL) { 1334 m_freem(m); 1335 return (NULL); 1336 } 1337 M_MOVE_PKTHDR(n, m); /* just for recvif */ 1338 n->m_pkthdr.len = n->m_len = replylen; 1339 1340 /* copy mbuf header and IPv6 + Node Information base headers */ 1341 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 1342 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 1343 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 1344 1345 /* qtype dependent procedure */ 1346 switch (qtype) { 1347 case NI_QTYPE_NOOP: 1348 nni6->ni_code = ICMP6_NI_SUCCESS; 1349 nni6->ni_flags = 0; 1350 break; 1351 case NI_QTYPE_SUPTYPES: 1352 { 1353 u_int32_t v; 1354 nni6->ni_code = ICMP6_NI_SUCCESS; 1355 nni6->ni_flags = htons(0x0000); /* raw bitmap */ 1356 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 1357 v = (u_int32_t)htonl(0x0000000f); 1358 bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 1359 break; 1360 } 1361 case NI_QTYPE_FQDN: 1362 nni6->ni_code = ICMP6_NI_SUCCESS; 1363 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1364 sizeof(struct ip6_hdr) + 1365 sizeof(struct icmp6_nodeinfo)); 1366 nni6->ni_flags = 0; /* XXX: meaningless TTL */ 1367 fqdn->ni_fqdn_ttl = 0; /* ditto. */ 1368 /* 1369 * XXX do we really have FQDN in variable "hostname"? 1370 */ 1371 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 1372 if (n->m_next == NULL) 1373 goto bad; 1374 /* XXX we assume that n->m_next is not a chain */ 1375 if (n->m_next->m_next != NULL) 1376 goto bad; 1377 n->m_pkthdr.len += n->m_next->m_len; 1378 break; 1379 case NI_QTYPE_NODEADDR: 1380 { 1381 int lenlim, copied; 1382 1383 nni6->ni_code = ICMP6_NI_SUCCESS; 1384 n->m_pkthdr.len = n->m_len = 1385 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1386 lenlim = M_TRAILINGSPACE(n); 1387 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 1388 /* XXX: reset mbuf length */ 1389 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1390 sizeof(struct icmp6_nodeinfo) + copied; 1391 break; 1392 } 1393 default: 1394 break; /* XXX impossible! */ 1395 } 1396 1397 nni6->ni_type = ICMP6_NI_REPLY; 1398 m_freem(m); 1399 return (n); 1400 1401 bad: 1402 m_freem(m); 1403 if (n) 1404 m_freem(n); 1405 return (NULL); 1406 } 1407 #undef hostnamelen 1408 1409 /* 1410 * make a mbuf with DNS-encoded string. no compression support. 1411 * 1412 * XXX names with less than 2 dots (like "foo" or "foo.section") will be 1413 * treated as truncated name (two \0 at the end). this is a wild guess. 1414 */ 1415 static struct mbuf * 1416 ni6_nametodns(const char *name, int namelen, 1417 int old) /* return pascal string if non-zero */ 1418 { 1419 struct mbuf *m; 1420 char *cp, *ep; 1421 const char *p, *q; 1422 int i, len, nterm; 1423 1424 if (old) 1425 len = namelen + 1; 1426 else 1427 len = MCLBYTES; 1428 1429 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1430 m = m_getb(len, M_NOWAIT, MT_DATA, 0); 1431 if (!m) 1432 goto fail; 1433 1434 if (old) { 1435 m->m_len = len; 1436 *mtod(m, char *) = namelen; 1437 bcopy(name, mtod(m, char *) + 1, namelen); 1438 return m; 1439 } else { 1440 m->m_len = 0; 1441 cp = mtod(m, char *); 1442 ep = mtod(m, char *) + M_TRAILINGSPACE(m); 1443 1444 /* if not certain about my name, return empty buffer */ 1445 if (namelen == 0) 1446 return m; 1447 1448 /* 1449 * guess if it looks like shortened hostname, or FQDN. 1450 * shortened hostname needs two trailing "\0". 1451 */ 1452 i = 0; 1453 for (p = name; p < name + namelen; p++) { 1454 if (*p && *p == '.') 1455 i++; 1456 } 1457 if (i < 2) 1458 nterm = 2; 1459 else 1460 nterm = 1; 1461 1462 p = name; 1463 while (cp < ep && p < name + namelen) { 1464 i = 0; 1465 for (q = p; q < name + namelen && *q && *q != '.'; q++) 1466 i++; 1467 /* result does not fit into mbuf */ 1468 if (cp + i + 1 >= ep) 1469 goto fail; 1470 /* 1471 * DNS label length restriction, RFC1035 page 8. 1472 * "i == 0" case is included here to avoid returning 1473 * 0-length label on "foo..bar". 1474 */ 1475 if (i <= 0 || i >= 64) 1476 goto fail; 1477 *cp++ = i; 1478 bcopy(p, cp, i); 1479 cp += i; 1480 p = q; 1481 if (p < name + namelen && *p == '.') 1482 p++; 1483 } 1484 /* termination */ 1485 if (cp + nterm >= ep) 1486 goto fail; 1487 while (nterm-- > 0) 1488 *cp++ = '\0'; 1489 m->m_len = cp - mtod(m, char *); 1490 return m; 1491 } 1492 1493 panic("should not reach here"); 1494 /* NOTREACHED */ 1495 1496 fail: 1497 if (m) 1498 m_freem(m); 1499 return NULL; 1500 } 1501 1502 /* 1503 * check if two DNS-encoded string matches. takes care of truncated 1504 * form (with \0\0 at the end). no compression support. 1505 * XXX upper/lowercase match (see RFC2065) 1506 */ 1507 static int 1508 ni6_dnsmatch(const char *a, int alen, const char *b, int blen) 1509 { 1510 const char *a0, *b0; 1511 int l; 1512 1513 /* simplest case - need validation? */ 1514 if (alen == blen && bcmp(a, b, alen) == 0) 1515 return 1; 1516 1517 a0 = a; 1518 b0 = b; 1519 1520 /* termination is mandatory */ 1521 if (alen < 2 || blen < 2) 1522 return 0; 1523 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 1524 return 0; 1525 alen--; 1526 blen--; 1527 1528 while (a - a0 < alen && b - b0 < blen) { 1529 if (a - a0 + 1 > alen || b - b0 + 1 > blen) 1530 return 0; 1531 1532 if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 1533 return 0; 1534 /* we don't support compression yet */ 1535 if (a[0] >= 64 || b[0] >= 64) 1536 return 0; 1537 1538 /* truncated case */ 1539 if (a[0] == 0 && a - a0 == alen - 1) 1540 return 1; 1541 if (b[0] == 0 && b - b0 == blen - 1) 1542 return 1; 1543 if (a[0] == 0 || b[0] == 0) 1544 return 0; 1545 1546 if (a[0] != b[0]) 1547 return 0; 1548 l = a[0]; 1549 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 1550 return 0; 1551 if (bcmp(a + 1, b + 1, l) != 0) 1552 return 0; 1553 1554 a += 1 + l; 1555 b += 1 + l; 1556 } 1557 1558 if (a - a0 == alen && b - b0 == blen) 1559 return 1; 1560 else 1561 return 0; 1562 } 1563 1564 /* 1565 * calculate the number of addresses to be returned in the node info reply. 1566 */ 1567 static int 1568 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp, 1569 char *subj) 1570 { 1571 const struct ifnet_array *arr; 1572 struct in6_ifaddr *ifa6; 1573 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ 1574 int addrs = 0, addrsofif, iffound = 0; 1575 int niflags = ni6->ni_flags; 1576 int i; 1577 1578 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 1579 switch (ni6->ni_code) { 1580 case ICMP6_NI_SUBJ_IPV6: 1581 if (subj == NULL) /* must be impossible... */ 1582 return (0); 1583 subj_ip6 = (struct sockaddr_in6 *)subj; 1584 break; 1585 default: 1586 /* 1587 * XXX: we only support IPv6 subject address for 1588 * this Qtype. 1589 */ 1590 return (0); 1591 } 1592 } 1593 1594 arr = ifnet_array_get(); 1595 for (i = 0; i < arr->ifnet_count; ++i) { 1596 struct ifnet *ifp = arr->ifnet_arr[i]; 1597 struct ifaddr_container *ifac; 1598 1599 addrsofif = 0; 1600 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) 1601 { 1602 struct ifaddr *ifa = ifac->ifa; 1603 1604 if (ifa->ifa_addr->sa_family != AF_INET6) 1605 continue; 1606 ifa6 = (struct in6_ifaddr *)ifa; 1607 1608 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1609 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, 1610 &ifa6->ia_addr.sin6_addr)) 1611 iffound = 1; 1612 1613 /* 1614 * IPv4-mapped addresses can only be returned by a 1615 * Node Information proxy, since they represent 1616 * addresses of IPv4-only nodes, which perforce do 1617 * not implement this protocol. 1618 * [icmp-name-lookups-07, Section 5.4] 1619 * So we don't support NI_NODEADDR_FLAG_COMPAT in 1620 * this function at this moment. 1621 */ 1622 1623 /* What do we have to do about ::1? */ 1624 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 1625 case IPV6_ADDR_SCOPE_LINKLOCAL: 1626 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1627 continue; 1628 break; 1629 case IPV6_ADDR_SCOPE_SITELOCAL: 1630 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1631 continue; 1632 break; 1633 case IPV6_ADDR_SCOPE_GLOBAL: 1634 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1635 continue; 1636 break; 1637 default: 1638 continue; 1639 } 1640 1641 /* 1642 * check if anycast is okay. 1643 * XXX: just experimental. not in the spec. 1644 */ 1645 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) && 1646 !(niflags & NI_NODEADDR_FLAG_ANYCAST)) 1647 continue; /* we need only unicast addresses */ 1648 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) && 1649 (icmp6_nodeinfo & 4) == 0) { 1650 continue; 1651 } 1652 addrsofif++; /* count the address */ 1653 } 1654 if (iffound) { 1655 *ifpp = ifp; 1656 return (addrsofif); 1657 } 1658 1659 addrs += addrsofif; 1660 } 1661 1662 return (addrs); 1663 } 1664 1665 static int 1666 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, 1667 struct ifnet *ifp0, int resid) 1668 { 1669 const struct ifnet_array *arr; 1670 struct in6_ifaddr *ifa6; 1671 int copied = 0, allow_deprecated = 0; 1672 u_char *cp = (u_char *)(nni6 + 1); 1673 int niflags = ni6->ni_flags; 1674 u_int32_t ltime; 1675 int idx, idx_dep = -1; 1676 1677 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1678 return (0); /* needless to copy */ 1679 1680 arr = ifnet_array_get(); 1681 if (ifp0 == NULL) { 1682 idx = 0; 1683 } else { 1684 for (idx = 0; idx < arr->ifnet_count; ++idx) { 1685 if (arr->ifnet_arr[idx] == ifp0) 1686 break; 1687 } 1688 } 1689 1690 again: 1691 1692 for (; idx < arr->ifnet_count; ++idx) { 1693 struct ifnet *ifp = arr->ifnet_arr[idx]; 1694 struct ifaddr_container *ifac; 1695 1696 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1697 struct ifaddr *ifa = ifac->ifa; 1698 1699 if (ifa->ifa_addr->sa_family != AF_INET6) 1700 continue; 1701 ifa6 = (struct in6_ifaddr *)ifa; 1702 1703 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) && 1704 allow_deprecated == 0) { 1705 /* 1706 * prefererred address should be put before 1707 * deprecated addresses. 1708 */ 1709 1710 /* record the interface for later search */ 1711 if (idx_dep < 0) 1712 idx_dep = idx; 1713 1714 continue; 1715 } 1716 else if (!(ifa6->ia6_flags & IN6_IFF_DEPRECATED) && 1717 allow_deprecated != 0) 1718 continue; /* we now collect deprecated addrs */ 1719 1720 /* What do we have to do about ::1? */ 1721 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 1722 case IPV6_ADDR_SCOPE_LINKLOCAL: 1723 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1724 continue; 1725 break; 1726 case IPV6_ADDR_SCOPE_SITELOCAL: 1727 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1728 continue; 1729 break; 1730 case IPV6_ADDR_SCOPE_GLOBAL: 1731 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1732 continue; 1733 break; 1734 default: 1735 continue; 1736 } 1737 1738 /* 1739 * check if anycast is okay. 1740 * XXX: just experimental. not in the spec. 1741 */ 1742 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) && 1743 !(niflags & NI_NODEADDR_FLAG_ANYCAST)) 1744 continue; 1745 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) && 1746 (icmp6_nodeinfo & 4) == 0) { 1747 continue; 1748 } 1749 1750 /* now we can copy the address */ 1751 if (resid < sizeof(struct in6_addr) + 1752 sizeof(u_int32_t)) { 1753 /* 1754 * We give up much more copy. 1755 * Set the truncate flag and return. 1756 */ 1757 nni6->ni_flags |= 1758 NI_NODEADDR_FLAG_TRUNCATE; 1759 return (copied); 1760 } 1761 1762 /* 1763 * Set the TTL of the address. 1764 * The TTL value should be one of the following 1765 * according to the specification: 1766 * 1767 * 1. The remaining lifetime of a DHCP lease on the 1768 * address, or 1769 * 2. The remaining Valid Lifetime of a prefix from 1770 * which the address was derived through Stateless 1771 * Autoconfiguration. 1772 * 1773 * Note that we currently do not support stateful 1774 * address configuration by DHCPv6, so the former 1775 * case can't happen. 1776 */ 1777 if (ifa6->ia6_lifetime.ia6t_expire == 0) 1778 ltime = ND6_INFINITE_LIFETIME; 1779 else { 1780 if (ifa6->ia6_lifetime.ia6t_expire > 1781 time_uptime) 1782 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime); 1783 else 1784 ltime = 0; 1785 } 1786 1787 bcopy(<ime, cp, sizeof(u_int32_t)); 1788 cp += sizeof(u_int32_t); 1789 1790 /* copy the address itself */ 1791 bcopy(&ifa6->ia_addr.sin6_addr, cp, 1792 sizeof(struct in6_addr)); 1793 in6_clearscope((struct in6_addr *)cp); /* XXX */ 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 fromsa; 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(&fromsa, sizeof(fromsa)); 1840 fromsa.sin6_len = sizeof(struct sockaddr_in6); 1841 fromsa.sin6_family = AF_INET6; 1842 /* KAME hack: recover scopeid */ 1843 in6_recoverscope(&fromsa, &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 *)&fromsa, n, opts) == 0) { 1876 /* should notify about lost packet */ 1877 m_freem(n); 1878 if (opts) 1879 m_freem(opts); 1880 } else { 1881 sorwakeup(so); 1882 } 1883 lwkt_reltoken(&so->so_rcv.ssb_token); 1884 opts = NULL; 1885 } 1886 } 1887 last = in6p; 1888 } 1889 if (last) { 1890 struct socket *so; 1891 1892 if (last->in6p_flags & IN6P_CONTROLOPTS) 1893 ip6_savecontrol(last, &opts, ip6, m); 1894 /* strip intermediate headers */ 1895 m_adj(m, off); 1896 so = last->in6p_socket; 1897 lwkt_gettoken(&so->so_rcv.ssb_token); 1898 if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *)&fromsa, 1899 m, opts) == 0) { 1900 m_freem(m); 1901 if (opts) 1902 m_freem(opts); 1903 } else { 1904 sorwakeup(so); 1905 } 1906 lwkt_reltoken(&so->so_rcv.ssb_token); 1907 } else { 1908 m_freem(m); 1909 ip6stat.ip6s_delivered--; 1910 } 1911 return IPPROTO_DONE; 1912 } 1913 1914 /* 1915 * Reflect the ip6 packet back to the source. 1916 * OFF points to the icmp6 header, counted from the top of the mbuf. 1917 */ 1918 void 1919 icmp6_reflect(struct mbuf *m, size_t off) 1920 { 1921 struct ip6_hdr *ip6; 1922 struct icmp6_hdr *icmp6; 1923 struct in6_ifaddr *ia; 1924 struct in6_addr t, *src = NULL; 1925 int plen; 1926 int type, code; 1927 struct ifnet *outif = NULL; 1928 struct sockaddr_in6 sa6_src, sa6_dst; 1929 #ifdef COMPAT_RFC1885 1930 int mtu = IPV6_MMTU; 1931 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; 1932 #endif 1933 1934 /* too short to reflect */ 1935 if (off < sizeof(struct ip6_hdr)) { 1936 nd6log((LOG_DEBUG, 1937 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 1938 (u_long)off, (u_long)sizeof(struct ip6_hdr), 1939 __FILE__, __LINE__)); 1940 goto bad; 1941 } 1942 1943 /* 1944 * If there are extra headers between IPv6 and ICMPv6, strip 1945 * off that header first. 1946 */ 1947 #ifdef DIAGNOSTIC 1948 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 1949 panic("assumption failed in icmp6_reflect"); 1950 #endif 1951 if (off > sizeof(struct ip6_hdr)) { 1952 size_t l; 1953 struct ip6_hdr nip6; 1954 1955 l = off - sizeof(struct ip6_hdr); 1956 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 1957 m_adj(m, l); 1958 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 1959 if (m->m_len < l) { 1960 if ((m = m_pullup(m, l)) == NULL) 1961 return; 1962 } 1963 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 1964 } else /* off == sizeof(struct ip6_hdr) */ { 1965 size_t l; 1966 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 1967 if (m->m_len < l) { 1968 if ((m = m_pullup(m, l)) == NULL) 1969 return; 1970 } 1971 } 1972 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 1973 ip6 = mtod(m, struct ip6_hdr *); 1974 ip6->ip6_nxt = IPPROTO_ICMPV6; 1975 icmp6 = (struct icmp6_hdr *)(ip6 + 1); 1976 type = icmp6->icmp6_type; /* keep type for statistics */ 1977 code = icmp6->icmp6_code; /* ditto. */ 1978 1979 t = ip6->ip6_dst; 1980 /* 1981 * ip6_input() drops a packet if its src is multicast. 1982 * So, the src is never multicast. 1983 */ 1984 ip6->ip6_dst = ip6->ip6_src; 1985 1986 /* 1987 * XXX: make sure to embed scope zone information, using 1988 * already embedded IDs or the received interface (if any). 1989 * Note that rcvif may be NULL. 1990 * TODO: scoped routing case (XXX). 1991 */ 1992 bzero(&sa6_src, sizeof(sa6_src)); 1993 sa6_src.sin6_family = AF_INET6; 1994 sa6_src.sin6_len = sizeof(sa6_src); 1995 sa6_src.sin6_addr = ip6->ip6_dst; 1996 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); 1997 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL); 1998 bzero(&sa6_dst, sizeof(sa6_dst)); 1999 sa6_dst.sin6_family = AF_INET6; 2000 sa6_dst.sin6_len = sizeof(sa6_dst); 2001 sa6_dst.sin6_addr = t; 2002 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); 2003 in6_embedscope(&t, &sa6_dst, NULL, NULL); 2004 2005 #ifdef COMPAT_RFC1885 2006 /* 2007 * xxx guess MTU 2008 * RFC 1885 requires that echo reply should be truncated if it 2009 * does not fit in with (return) path MTU, but the description was 2010 * removed in the new spec. 2011 */ 2012 if (icmp6_reflect_rt.ro_rt == 0 || 2013 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { 2014 if (icmp6_reflect_rt.ro_rt) { 2015 RTFREE(icmp6_reflect_rt.ro_rt); 2016 icmp6_reflect_rt.ro_rt = 0; 2017 } 2018 bzero(sin6, sizeof(*sin6)); 2019 sin6->sin6_family = PF_INET6; 2020 sin6->sin6_len = sizeof(struct sockaddr_in6); 2021 sin6->sin6_addr = ip6->ip6_dst; 2022 2023 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt, 2024 RTF_PRCLONING); 2025 } 2026 2027 if (icmp6_reflect_rt.ro_rt == 0) 2028 goto bad; 2029 2030 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) 2031 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) 2032 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; 2033 2034 if (mtu < m->m_pkthdr.len) { 2035 plen -= (m->m_pkthdr.len - mtu); 2036 m_adj(m, mtu - m->m_pkthdr.len); 2037 } 2038 #endif 2039 /* 2040 * If the incoming packet was addressed directly to us(i.e. unicast), 2041 * use dst as the src for the reply. 2042 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible 2043 * (for example) when we encounter an error while forwarding procedure 2044 * destined to a duplicated address of ours. 2045 */ 2046 for (ia = in6_ifaddr; ia; ia = ia->ia_next) 2047 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && 2048 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 2049 src = &t; 2050 break; 2051 } 2052 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { 2053 /* 2054 * This is the case if the dst is our link-local address 2055 * and the sender is also ourselves. 2056 */ 2057 src = &t; 2058 } 2059 2060 if (src == NULL) { 2061 int e; 2062 struct route_in6 ro; 2063 2064 /* 2065 * This case matches to multicasts, our anycast, or unicasts 2066 * that we do not own. Select a source address based on the 2067 * source address of the erroneous packet. 2068 */ 2069 bzero(&ro, sizeof(ro)); 2070 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e, NULL); 2071 if (ro.ro_rt) 2072 RTFREE(ro.ro_rt); /* XXX: we could use this */ 2073 if (src == NULL) { 2074 nd6log((LOG_DEBUG, 2075 "icmp6_reflect: source can't be determined: " 2076 "dst=%s, error=%d\n", 2077 ip6_sprintf(&sa6_src.sin6_addr), e)); 2078 goto bad; 2079 } 2080 } 2081 2082 ip6->ip6_src = *src; 2083 2084 ip6->ip6_flow = 0; 2085 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2086 ip6->ip6_vfc |= IPV6_VERSION; 2087 ip6->ip6_nxt = IPPROTO_ICMPV6; 2088 if (m->m_pkthdr.rcvif) { 2089 /* XXX: This may not be the outgoing interface */ 2090 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 2091 if (ip6->ip6_hlim < ip6_minhlim) 2092 ip6->ip6_hlim = ip6_minhlim; 2093 } else { 2094 ip6->ip6_hlim = ip6_defhlim; 2095 } 2096 2097 icmp6->icmp6_cksum = 0; 2098 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2099 sizeof(struct ip6_hdr), plen); 2100 2101 /* 2102 * XXX option handling 2103 */ 2104 2105 m->m_flags &= ~(M_BCAST|M_MCAST); 2106 2107 #ifdef COMPAT_RFC1885 2108 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL); 2109 #else 2110 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 2111 #endif 2112 if (outif) 2113 icmp6_ifoutstat_inc(outif, type, code); 2114 2115 return; 2116 2117 bad: 2118 m_freem(m); 2119 return; 2120 } 2121 2122 void 2123 icmp6_fasttimo(void) 2124 { 2125 2126 mld6_fasttimeo(); 2127 } 2128 2129 static const char * 2130 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6, 2131 struct in6_addr *tgt6) 2132 { 2133 static char buf[1024]; 2134 ksnprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2135 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); 2136 return buf; 2137 } 2138 2139 void 2140 icmp6_redirect_input(struct mbuf *m, int off) 2141 { 2142 struct ifnet *ifp = m->m_pkthdr.rcvif; 2143 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 2144 struct nd_redirect *nd_rd; 2145 int icmp6len = ntohs(ip6->ip6_plen); 2146 char *lladdr = NULL; 2147 int lladdrlen = 0; 2148 #if 0 2149 u_char *redirhdr = NULL; 2150 int redirhdrlen = 0; 2151 #endif 2152 struct rtentry *rt = NULL; 2153 int is_router; 2154 int is_onlink; 2155 struct in6_addr src6 = ip6->ip6_src; 2156 struct in6_addr redtgt6; 2157 struct in6_addr reddst6; 2158 union nd_opts ndopts; 2159 2160 if (!m || !ifp) 2161 return; 2162 2163 /* XXX if we are router, we don't update route by icmp6 redirect */ 2164 if (ip6_forwarding) 2165 goto freeit; 2166 if (!icmp6_rediraccept) 2167 goto freeit; 2168 2169 #ifndef PULLDOWN_TEST 2170 IP6_EXTHDR_CHECK(m, off, icmp6len,); 2171 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 2172 #else 2173 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 2174 if (nd_rd == NULL) { 2175 icmp6stat.icp6s_tooshort++; 2176 return; 2177 } 2178 #endif 2179 redtgt6 = nd_rd->nd_rd_target; 2180 reddst6 = nd_rd->nd_rd_dst; 2181 2182 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 2183 redtgt6.s6_addr16[1] = htons(ifp->if_index); 2184 if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) 2185 reddst6.s6_addr16[1] = htons(ifp->if_index); 2186 2187 /* validation */ 2188 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 2189 nd6log((LOG_ERR, 2190 "ICMP6 redirect sent from %s rejected; " 2191 "must be from linklocal\n", ip6_sprintf(&src6))); 2192 goto bad; 2193 } 2194 if (ip6->ip6_hlim != 255) { 2195 nd6log((LOG_ERR, 2196 "ICMP6 redirect sent from %s rejected; " 2197 "hlim=%d (must be 255)\n", 2198 ip6_sprintf(&src6), ip6->ip6_hlim)); 2199 goto bad; 2200 } 2201 { 2202 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 2203 struct sockaddr_in6 sin6; 2204 struct in6_addr *gw6; 2205 2206 bzero(&sin6, sizeof(sin6)); 2207 sin6.sin6_family = AF_INET6; 2208 sin6.sin6_len = sizeof(struct sockaddr_in6); 2209 bcopy(&reddst6, &sin6.sin6_addr, sizeof reddst6); 2210 rt = rtpurelookup((struct sockaddr *)&sin6); 2211 if (rt != NULL) { 2212 if (rt->rt_gateway == NULL || 2213 rt->rt_gateway->sa_family != AF_INET6) { 2214 nd6log((LOG_ERR, 2215 "ICMP6 redirect rejected; no route " 2216 "with inet6 gateway found for redirect dst: %s\n", 2217 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2218 --rt->rt_refcnt; 2219 goto bad; 2220 } 2221 2222 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 2223 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 2224 nd6log((LOG_ERR, 2225 "ICMP6 redirect rejected; " 2226 "not equal to gw-for-src=%s (must be same): " 2227 "%s\n", 2228 ip6_sprintf(gw6), 2229 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2230 --rt->rt_refcnt; 2231 goto bad; 2232 } 2233 } else { 2234 nd6log((LOG_ERR, 2235 "ICMP6 redirect rejected; " 2236 "no route found for redirect dst: %s\n", 2237 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2238 goto bad; 2239 } 2240 --rt->rt_refcnt; 2241 rt = NULL; 2242 } 2243 if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 2244 nd6log((LOG_ERR, 2245 "ICMP6 redirect rejected; " 2246 "redirect dst must be unicast: %s\n", 2247 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2248 goto bad; 2249 } 2250 2251 is_router = is_onlink = 0; 2252 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 2253 is_router = 1; /* router case */ 2254 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 2255 is_onlink = 1; /* on-link destination case */ 2256 if (!is_router && !is_onlink) { 2257 nd6log((LOG_ERR, 2258 "ICMP6 redirect rejected; " 2259 "neither router case nor onlink case: %s\n", 2260 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2261 goto bad; 2262 } 2263 /* validation passed */ 2264 2265 icmp6len -= sizeof(*nd_rd); 2266 nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 2267 if (nd6_options(&ndopts) < 0) { 2268 nd6log((LOG_INFO, "icmp6_redirect_input: " 2269 "invalid ND option, rejected: %s\n", 2270 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2271 /* nd6_options have incremented stats */ 2272 goto freeit; 2273 } 2274 2275 if (ndopts.nd_opts_tgt_lladdr) { 2276 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 2277 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 2278 } 2279 2280 #if 0 2281 if (ndopts.nd_opts_rh) { 2282 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 2283 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 2284 } 2285 #endif 2286 2287 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 2288 nd6log((LOG_INFO, 2289 "icmp6_redirect_input: lladdrlen mismatch for %s " 2290 "(if %d, icmp6 packet %d): %s\n", 2291 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, 2292 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2293 goto bad; 2294 } 2295 2296 /* RFC 2461 8.3 */ 2297 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2298 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 2299 2300 if (!is_onlink) { /* better router case. perform rtredirect. */ 2301 /* perform rtredirect */ 2302 struct sockaddr_in6 sdst; 2303 struct sockaddr_in6 sgw; 2304 struct sockaddr_in6 ssrc; 2305 2306 bzero(&sdst, sizeof(sdst)); 2307 bzero(&sgw, sizeof(sgw)); 2308 bzero(&ssrc, sizeof(ssrc)); 2309 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 2310 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 2311 sizeof(struct sockaddr_in6); 2312 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 2313 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 2314 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 2315 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2316 NULL, RTF_GATEWAY | RTF_HOST, 2317 (struct sockaddr *)&ssrc); 2318 } 2319 /* finally update cached route in each socket via kpfctlinput */ 2320 { 2321 struct sockaddr_in6 sdst; 2322 2323 bzero(&sdst, sizeof(sdst)); 2324 sdst.sin6_family = AF_INET6; 2325 sdst.sin6_len = sizeof(struct sockaddr_in6); 2326 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 2327 kpfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2328 #ifdef IPSEC 2329 key_sa_routechange((struct sockaddr *)&sdst); 2330 #endif 2331 } 2332 2333 freeit: 2334 m_freem(m); 2335 return; 2336 2337 bad: 2338 icmp6stat.icp6s_badredirect++; 2339 m_freem(m); 2340 } 2341 2342 void 2343 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) 2344 { 2345 struct ifnet *ifp; /* my outgoing interface */ 2346 struct in6_addr *ifp_ll6; 2347 struct in6_addr *router_ll6; 2348 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 2349 struct mbuf *m = NULL; /* newly allocated one */ 2350 struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 2351 struct nd_redirect *nd_rd; 2352 size_t maxlen; 2353 u_char *p; 2354 struct ifnet *outif = NULL; 2355 struct sockaddr_in6 src_sa; 2356 2357 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 2358 2359 /* if we are not router, we don't send icmp6 redirect */ 2360 if (!ip6_forwarding || ip6_accept_rtadv) 2361 goto fail; 2362 2363 /* sanity check */ 2364 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 2365 goto fail; 2366 2367 /* 2368 * Address check: 2369 * the source address must identify a neighbor, and 2370 * the destination address must not be a multicast address 2371 * [RFC 2461, sec 8.2] 2372 */ 2373 sip6 = mtod(m0, struct ip6_hdr *); 2374 bzero(&src_sa, sizeof(src_sa)); 2375 src_sa.sin6_family = AF_INET6; 2376 src_sa.sin6_len = sizeof(src_sa); 2377 src_sa.sin6_addr = sip6->ip6_src; 2378 /* we don't currently use sin6_scope_id, but eventually use it */ 2379 if (in6_addr2zoneid(ifp, &sip6->ip6_src, &src_sa.sin6_scope_id)) 2380 goto fail; 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 /* XXX: clear embedded link IDs in the inner header */ 2589 in6_clearscope(&sip6->ip6_src); 2590 in6_clearscope(&sip6->ip6_dst); 2591 in6_clearscope(&nd_rd->nd_rd_target); 2592 in6_clearscope(&nd_rd->nd_rd_dst); 2593 2594 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 2595 2596 nd_rd->nd_rd_cksum = 0; 2597 nd_rd->nd_rd_cksum 2598 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen)); 2599 2600 /* send the packet to outside... */ 2601 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 2602 if (outif) { 2603 icmp6_ifstat_inc(outif, ifs6_out_msg); 2604 icmp6_ifstat_inc(outif, ifs6_out_redirect); 2605 } 2606 icmp6stat.icp6s_outhist[ND_REDIRECT]++; 2607 2608 return; 2609 2610 fail: 2611 if (m) 2612 m_freem(m); 2613 if (m0) 2614 m_freem(m0); 2615 } 2616 2617 #ifdef HAVE_NRL_INPCB 2618 #define in6pcb inpcb 2619 #define in6p_icmp6filt inp_icmp6filt 2620 #endif 2621 /* 2622 * ICMPv6 socket option processing. 2623 */ 2624 void 2625 icmp6_ctloutput(netmsg_t msg) 2626 { 2627 struct socket *so = msg->ctloutput.base.nm_so; 2628 struct sockopt *sopt = msg->ctloutput.nm_sopt; 2629 int error = 0; 2630 int optlen; 2631 struct inpcb *inp = so->so_pcb; 2632 int level, op, optname; 2633 2634 if (sopt) { 2635 level = sopt->sopt_level; 2636 op = sopt->sopt_dir; 2637 optname = sopt->sopt_name; 2638 optlen = sopt->sopt_valsize; 2639 } else 2640 level = op = optname = optlen = 0; 2641 2642 if (level != IPPROTO_ICMPV6) { 2643 error = EINVAL; 2644 goto out; 2645 } 2646 2647 switch (op) { 2648 case PRCO_SETOPT: 2649 switch (optname) { 2650 case ICMP6_FILTER: 2651 { 2652 if (optlen != sizeof(struct icmp6_filter)) { 2653 error = EMSGSIZE; 2654 break; 2655 } 2656 if (inp->in6p_icmp6filt == NULL) { 2657 error = EINVAL; 2658 break; 2659 } 2660 error = soopt_to_kbuf(sopt, inp->in6p_icmp6filt, optlen, 2661 optlen); 2662 break; 2663 } 2664 2665 default: 2666 error = ENOPROTOOPT; 2667 break; 2668 } 2669 break; 2670 2671 case PRCO_GETOPT: 2672 switch (optname) { 2673 case ICMP6_FILTER: 2674 { 2675 if (inp->in6p_icmp6filt == NULL) { 2676 error = EINVAL; 2677 break; 2678 } 2679 soopt_from_kbuf(sopt, inp->in6p_icmp6filt, 2680 sizeof(struct icmp6_filter)); 2681 break; 2682 } 2683 2684 default: 2685 error = ENOPROTOOPT; 2686 break; 2687 } 2688 break; 2689 } 2690 out: 2691 lwkt_replymsg(&msg->ctloutput.base.lmsg, error); 2692 } 2693 #ifdef HAVE_NRL_INPCB 2694 #undef in6pcb 2695 #undef in6p_icmp6filt 2696 #endif 2697 2698 #ifndef HAVE_PPSRATECHECK 2699 #ifndef timersub 2700 #define timersub(tvp, uvp, vvp) \ 2701 do { \ 2702 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 2703 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 2704 if ((vvp)->tv_usec < 0) { \ 2705 (vvp)->tv_sec--; \ 2706 (vvp)->tv_usec += 1000000; \ 2707 } \ 2708 } while (0) 2709 #endif 2710 2711 /* 2712 * ppsratecheck(): packets (or events) per second limitation. 2713 */ 2714 static int 2715 ppsratecheck(struct timeval *lasttime, int *curpps, 2716 int maxpps) /* maximum pps allowed */ 2717 { 2718 struct timeval tv, delta; 2719 int rv; 2720 2721 microtime(&tv); 2722 2723 timersub(&tv, lasttime, &delta); 2724 2725 /* 2726 * Check for 0,0 so that the message will be seen at least once. 2727 * If more than one second has passed since the last update of 2728 * lasttime, reset the counter. 2729 * 2730 * We do increment *curpps even in *curpps < maxpps case, as some may 2731 * try to use *curpps for stat purposes as well. 2732 */ 2733 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || 2734 delta.tv_sec >= 1) { 2735 *lasttime = tv; 2736 *curpps = 0; 2737 rv = 1; 2738 } else if (maxpps < 0) 2739 rv = 1; 2740 else if (*curpps < maxpps) 2741 rv = 1; 2742 else 2743 rv = 0; 2744 2745 #if 1 /* DIAGNOSTIC? */ 2746 /* be careful about wrap-around */ 2747 if (*curpps + 1 > *curpps) 2748 *curpps = *curpps + 1; 2749 #else 2750 /* 2751 * assume that there's not too many calls to this function. 2752 * not sure if the assumption holds, as it depends on *caller's* 2753 * behavior, not the behavior of this function. 2754 * IMHO it is wrong to make assumption on the caller's behavior, 2755 * so the above #if is #if 1, not #ifdef DIAGNOSTIC. 2756 */ 2757 *curpps = *curpps + 1; 2758 #endif 2759 2760 return (rv); 2761 } 2762 #endif 2763 2764 /* 2765 * Perform rate limit check. 2766 * Returns 0 if it is okay to send the icmp6 packet. 2767 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 2768 * limitation. 2769 * 2770 * XXX per-destination/type check necessary? 2771 */ 2772 static int 2773 icmp6_ratelimit(const struct in6_addr *dst, /* not used at this moment */ 2774 const int type, /* not used at this moment */ 2775 const int code) /* not used at this moment */ 2776 { 2777 int ret; 2778 2779 ret = 0; /* okay to send */ 2780 2781 /* PPS limit */ 2782 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 2783 icmp6errppslim)) { 2784 /* The packet is subject to rate limit */ 2785 ret++; 2786 } 2787 2788 return ret; 2789 } 2790