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