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