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