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