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