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