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