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