1 /* $FreeBSD$ */ 2 /* $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 66 */ 67 68 #include "opt_ip6fw.h" 69 #include "opt_inet.h" 70 #include "opt_inet6.h" 71 #include "opt_ipsec.h" 72 #include "opt_pfil_hooks.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/malloc.h> 77 #include <sys/mbuf.h> 78 #include <sys/proc.h> 79 #include <sys/domain.h> 80 #include <sys/protosw.h> 81 #include <sys/socket.h> 82 #include <sys/socketvar.h> 83 #include <sys/errno.h> 84 #include <sys/time.h> 85 #include <sys/kernel.h> 86 #include <sys/syslog.h> 87 88 #include <net/if.h> 89 #include <net/if_types.h> 90 #include <net/if_dl.h> 91 #include <net/route.h> 92 #include <net/netisr.h> 93 #include <net/intrq.h> 94 #ifdef PFIL_HOOKS 95 #include <net/pfil.h> 96 #endif 97 98 #include <netinet/in.h> 99 #include <netinet/in_systm.h> 100 #ifdef INET 101 #include <netinet/ip.h> 102 #include <netinet/ip_icmp.h> 103 #endif /*INET*/ 104 #include <netinet/ip6.h> 105 #include <netinet6/in6_var.h> 106 #include <netinet6/ip6_var.h> 107 #include <netinet/in_pcb.h> 108 #include <netinet/icmp6.h> 109 #include <netinet6/in6_ifattach.h> 110 #include <netinet6/nd6.h> 111 #include <netinet6/in6_prefix.h> 112 113 #ifdef IPSEC 114 #include <netinet6/ipsec.h> 115 #ifdef INET6 116 #include <netinet6/ipsec6.h> 117 #endif 118 #endif 119 120 #include <netinet6/ip6_fw.h> 121 122 #include <netinet6/ip6protosw.h> 123 124 #include <net/net_osdep.h> 125 126 extern struct domain inet6domain; 127 128 u_char ip6_protox[IPPROTO_MAX]; 129 static int ip6qmaxlen = IFQ_MAXLEN; 130 struct in6_ifaddr *in6_ifaddr; 131 132 extern struct callout in6_tmpaddrtimer_ch; 133 134 int ip6_forward_srcrt; /* XXX */ 135 int ip6_sourcecheck; /* XXX */ 136 int ip6_sourcecheck_interval; /* XXX */ 137 138 int ip6_ours_check_algorithm; 139 140 141 /* firewall hooks */ 142 ip6_fw_chk_t *ip6_fw_chk_ptr; 143 ip6_fw_ctl_t *ip6_fw_ctl_ptr; 144 int ip6_fw_enable = 1; 145 146 struct ip6stat ip6stat; 147 148 static void ip6_init2 __P((void *)); 149 static struct mbuf *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *)); 150 151 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 152 #ifdef PULLDOWN_TEST 153 static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int)); 154 #endif 155 156 157 /* 158 * IP6 initialization: fill in IP6 protocol switch table. 159 * All protocols not implemented in kernel go to raw IP6 protocol handler. 160 */ 161 void 162 ip6_init() 163 { 164 struct ip6protosw *pr; 165 int i; 166 struct timeval tv; 167 168 #ifdef DIAGNOSTIC 169 if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 170 panic("sizeof(protosw) != sizeof(ip6protosw)"); 171 #endif 172 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 173 if (pr == 0) 174 panic("ip6_init"); 175 for (i = 0; i < IPPROTO_MAX; i++) 176 ip6_protox[i] = pr - inet6sw; 177 for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 178 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 179 if (pr->pr_domain->dom_family == PF_INET6 && 180 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 181 ip6_protox[pr->pr_protocol] = pr - inet6sw; 182 ip6intrq.ifq_maxlen = ip6qmaxlen; 183 mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", MTX_DEF); 184 ip6intrq_present = 1; 185 register_netisr(NETISR_IPV6, ip6intr); 186 nd6_init(); 187 frag6_init(); 188 /* 189 * in many cases, random() here does NOT return random number 190 * as initialization during bootstrap time occur in fixed order. 191 */ 192 microtime(&tv); 193 ip6_flow_seq = random() ^ tv.tv_usec; 194 microtime(&tv); 195 ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR; 196 } 197 198 static void 199 ip6_init2(dummy) 200 void *dummy; 201 { 202 203 /* 204 * to route local address of p2p link to loopback, 205 * assign loopback address first. 206 */ 207 in6_ifattach(&loif[0], NULL); 208 209 /* nd6_timer_init */ 210 callout_init(&nd6_timer_ch, 0); 211 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 212 213 /* router renumbering prefix list maintenance */ 214 callout_init(&in6_rr_timer_ch, 0); 215 callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL); 216 217 /* timer for regeneranation of temporary addresses randomize ID */ 218 callout_reset(&in6_tmpaddrtimer_ch, 219 (ip6_temp_preferred_lifetime - ip6_desync_factor - 220 ip6_temp_regen_advance) * hz, 221 in6_tmpaddrtimer, NULL); 222 } 223 224 /* cheat */ 225 /* This must be after route_init(), which is now SI_ORDER_THIRD */ 226 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 227 228 /* 229 * IP6 input interrupt handling. Just pass the packet to ip6_input. 230 */ 231 void 232 ip6intr() 233 { 234 int s; 235 struct mbuf *m; 236 237 for (;;) { 238 s = splimp(); 239 IF_DEQUEUE(&ip6intrq, m); 240 splx(s); 241 if (m == 0) 242 return; 243 ip6_input(m); 244 } 245 } 246 247 extern struct route_in6 ip6_forward_rt; 248 249 void 250 ip6_input(m) 251 struct mbuf *m; 252 { 253 struct ip6_hdr *ip6; 254 int off = sizeof(struct ip6_hdr), nest; 255 u_int32_t plen; 256 u_int32_t rtalert = ~0; 257 int nxt, ours = 0; 258 struct ifnet *deliverifp = NULL; 259 #ifdef PFIL_HOOKS 260 struct packet_filter_hook *pfh; 261 struct mbuf *m0; 262 int rv; 263 #endif /* PFIL_HOOKS */ 264 265 #ifdef IPSEC 266 /* 267 * should the inner packet be considered authentic? 268 * see comment in ah4_input(). 269 */ 270 if (m) { 271 m->m_flags &= ~M_AUTHIPHDR; 272 m->m_flags &= ~M_AUTHIPDGM; 273 } 274 #endif 275 276 /* 277 * make sure we don't have onion peering information into m_aux. 278 */ 279 ip6_delaux(m); 280 281 /* 282 * mbuf statistics by kazu 283 */ 284 if (m->m_flags & M_EXT) { 285 if (m->m_next) 286 ip6stat.ip6s_mext2m++; 287 else 288 ip6stat.ip6s_mext1++; 289 } else { 290 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0])) 291 if (m->m_next) { 292 if (m->m_flags & M_LOOP) { 293 ip6stat.ip6s_m2m[loif[0].if_index]++; /*XXX*/ 294 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 295 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 296 else 297 ip6stat.ip6s_m2m[0]++; 298 } else 299 ip6stat.ip6s_m1++; 300 #undef M2MMAX 301 } 302 303 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 304 ip6stat.ip6s_total++; 305 306 #ifndef PULLDOWN_TEST 307 /* 308 * L2 bridge code and some other code can return mbuf chain 309 * that does not conform to KAME requirement. too bad. 310 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 311 */ 312 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 313 struct mbuf *n; 314 315 MGETHDR(n, M_DONTWAIT, MT_HEADER); 316 if (n) 317 M_COPY_PKTHDR(n, m); 318 if (n && m->m_pkthdr.len > MHLEN) { 319 MCLGET(n, M_DONTWAIT); 320 if ((n->m_flags & M_EXT) == 0) { 321 m_freem(n); 322 n = NULL; 323 } 324 } 325 if (!n) { 326 m_freem(m); 327 return; /*ENOBUFS*/ 328 } 329 330 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); 331 n->m_len = m->m_pkthdr.len; 332 m_freem(m); 333 m = n; 334 } 335 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/); 336 #endif 337 338 if (m->m_len < sizeof(struct ip6_hdr)) { 339 struct ifnet *inifp; 340 inifp = m->m_pkthdr.rcvif; 341 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) { 342 ip6stat.ip6s_toosmall++; 343 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 344 return; 345 } 346 } 347 348 ip6 = mtod(m, struct ip6_hdr *); 349 350 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 351 ip6stat.ip6s_badvers++; 352 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 353 goto bad; 354 } 355 356 #ifdef PFIL_HOOKS 357 /* 358 * Run through list of hooks for input packets. If there are any 359 * filters which require that additional packets in the flow are 360 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 361 * Note that filters must _never_ set this flag, as another filter 362 * in the list may have previously cleared it. 363 */ 364 m0 = m; 365 pfh = pfil_hook_get(PFIL_IN, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh); 366 for (; pfh; pfh = pfh->pfil_link.tqe_next) 367 if (pfh->pfil_func) { 368 rv = pfh->pfil_func(ip6, sizeof(*ip6), 369 m->m_pkthdr.rcvif, 0, &m0); 370 if (rv) 371 return; 372 m = m0; 373 if (m == NULL) 374 return; 375 ip6 = mtod(m, struct ip6_hdr *); 376 } 377 #endif /* PFIL_HOOKS */ 378 379 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 380 381 /* 382 * Check with the firewall... 383 */ 384 if (ip6_fw_enable && ip6_fw_chk_ptr) { 385 u_short port = 0; 386 /* If ipfw says divert, we have to just drop packet */ 387 /* use port as a dummy argument */ 388 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 389 m_freem(m); 390 m = NULL; 391 } 392 if (!m) 393 return; 394 } 395 396 /* 397 * Check against address spoofing/corruption. 398 */ 399 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 400 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 401 /* 402 * XXX: "badscope" is not very suitable for a multicast source. 403 */ 404 ip6stat.ip6s_badscope++; 405 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 406 goto bad; 407 } 408 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 409 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && 410 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 411 ip6stat.ip6s_badscope++; 412 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 413 goto bad; 414 } 415 /* 416 * The following check is not documented in specs. A malicious 417 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 418 * and bypass security checks (act as if it was from 127.0.0.1 by using 419 * IPv6 src ::ffff:127.0.0.1). Be cautious. 420 * 421 * This check chokes if we are in an SIIT cloud. As none of BSDs 422 * support IPv4-less kernel compilation, we cannot support SIIT 423 * environment at all. So, it makes more sense for us to reject any 424 * malicious packets for non-SIIT environment, than try to do a 425 * partical support for SIIT environment. 426 */ 427 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 428 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 429 ip6stat.ip6s_badscope++; 430 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 431 goto bad; 432 } 433 #if 0 434 /* 435 * Reject packets with IPv4 compatible addresses (auto tunnel). 436 * 437 * The code forbids auto tunnel relay case in RFC1933 (the check is 438 * stronger than RFC1933). We may want to re-enable it if mech-xx 439 * is revised to forbid relaying case. 440 */ 441 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 442 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 443 ip6stat.ip6s_badscope++; 444 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 445 goto bad; 446 } 447 #endif 448 449 /* drop packets if interface ID portion is already filled */ 450 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 451 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 452 ip6->ip6_src.s6_addr16[1]) { 453 ip6stat.ip6s_badscope++; 454 goto bad; 455 } 456 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) && 457 ip6->ip6_dst.s6_addr16[1]) { 458 ip6stat.ip6s_badscope++; 459 goto bad; 460 } 461 } 462 463 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 464 ip6->ip6_src.s6_addr16[1] 465 = htons(m->m_pkthdr.rcvif->if_index); 466 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 467 ip6->ip6_dst.s6_addr16[1] 468 = htons(m->m_pkthdr.rcvif->if_index); 469 470 #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */ 471 /* 472 * We use rt->rt_ifp to determine if the address is ours or not. 473 * If rt_ifp is lo0, the address is ours. 474 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0, 475 * so any address under fe80::%lo0/64 will be mistakenly considered 476 * local. The special case is supplied to handle the case properly 477 * by actually looking at interface addresses 478 * (using in6ifa_ifpwithaddr). 479 */ 480 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 && 481 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 482 if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) { 483 icmp6_error(m, ICMP6_DST_UNREACH, 484 ICMP6_DST_UNREACH_ADDR, 0); 485 /* m is already freed */ 486 return; 487 } 488 489 ours = 1; 490 deliverifp = m->m_pkthdr.rcvif; 491 goto hbhcheck; 492 } 493 #endif 494 495 /* 496 * Multicast check 497 */ 498 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 499 struct in6_multi *in6m = 0; 500 501 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 502 /* 503 * See if we belong to the destination multicast group on the 504 * arrival interface. 505 */ 506 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 507 if (in6m) 508 ours = 1; 509 else if (!ip6_mrouter) { 510 ip6stat.ip6s_notmember++; 511 ip6stat.ip6s_cantforward++; 512 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 513 goto bad; 514 } 515 deliverifp = m->m_pkthdr.rcvif; 516 goto hbhcheck; 517 } 518 519 /* 520 * Unicast check 521 */ 522 switch (ip6_ours_check_algorithm) { 523 default: 524 /* 525 * XXX: I intentionally broke our indentation rule here, 526 * since this switch-case is just for measurement and 527 * therefore should soon be removed. 528 */ 529 if (ip6_forward_rt.ro_rt != NULL && 530 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 531 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 532 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 533 ip6stat.ip6s_forward_cachehit++; 534 else { 535 struct sockaddr_in6 *dst6; 536 537 if (ip6_forward_rt.ro_rt) { 538 /* route is down or destination is different */ 539 ip6stat.ip6s_forward_cachemiss++; 540 RTFREE(ip6_forward_rt.ro_rt); 541 ip6_forward_rt.ro_rt = 0; 542 } 543 544 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 545 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 546 dst6->sin6_len = sizeof(struct sockaddr_in6); 547 dst6->sin6_family = AF_INET6; 548 dst6->sin6_addr = ip6->ip6_dst; 549 #ifdef SCOPEDROUTING 550 ip6_forward_rt.ro_dst.sin6_scope_id = 551 in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst); 552 #endif 553 554 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 555 } 556 557 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 558 559 /* 560 * Accept the packet if the forwarding interface to the destination 561 * according to the routing table is the loopback interface, 562 * unless the associated route has a gateway. 563 * Note that this approach causes to accept a packet if there is a 564 * route to the loopback interface for the destination of the packet. 565 * But we think it's even useful in some situations, e.g. when using 566 * a special daemon which wants to intercept the packet. 567 * 568 * XXX: some OSes automatically make a cloned route for the destination 569 * of an outgoing packet. If the outgoing interface of the packet 570 * is a loopback one, the kernel would consider the packet to be 571 * accepted, even if we have no such address assinged on the interface. 572 * We check the cloned flag of the route entry to reject such cases, 573 * assuming that route entries for our own addresses are not made by 574 * cloning (it should be true because in6_addloop explicitly installs 575 * the host route). However, we might have to do an explicit check 576 * while it would be less efficient. Or, should we rather install a 577 * reject route for such a case? 578 */ 579 if (ip6_forward_rt.ro_rt && 580 (ip6_forward_rt.ro_rt->rt_flags & 581 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 582 #ifdef RTF_WASCLONED 583 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 584 #endif 585 #ifdef RTF_CLONED 586 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 587 #endif 588 #if 0 589 /* 590 * The check below is redundant since the comparison of 591 * the destination and the key of the rtentry has 592 * already done through looking up the routing table. 593 */ 594 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 595 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 596 #endif 597 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 598 struct in6_ifaddr *ia6 = 599 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 600 601 /* 602 * record address information into m_aux. 603 */ 604 (void)ip6_setdstifaddr(m, ia6); 605 606 /* 607 * packets to a tentative, duplicated, or somehow invalid 608 * address must not be accepted. 609 */ 610 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 611 /* this address is ready */ 612 ours = 1; 613 deliverifp = ia6->ia_ifp; /* correct? */ 614 /* Count the packet in the ip address stats */ 615 ia6->ia_ifa.if_ipackets++; 616 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 617 goto hbhcheck; 618 } else { 619 /* address is not ready, so discard the packet. */ 620 nd6log((LOG_INFO, 621 "ip6_input: packet to an unready address %s->%s\n", 622 ip6_sprintf(&ip6->ip6_src), 623 ip6_sprintf(&ip6->ip6_dst))); 624 625 goto bad; 626 } 627 } 628 } /* XXX indentation (see above) */ 629 630 /* 631 * FAITH(Firewall Aided Internet Translator) 632 */ 633 if (ip6_keepfaith) { 634 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 635 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 636 /* XXX do we need more sanity checks? */ 637 ours = 1; 638 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/ 639 goto hbhcheck; 640 } 641 } 642 643 /* 644 * Now there is no reason to process the packet if it's not our own 645 * and we're not a router. 646 */ 647 if (!ip6_forwarding) { 648 ip6stat.ip6s_cantforward++; 649 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 650 goto bad; 651 } 652 653 hbhcheck: 654 /* 655 * record address information into m_aux, if we don't have one yet. 656 * note that we are unable to record it, if the address is not listed 657 * as our interface address (e.g. multicast addresses, addresses 658 * within FAITH prefixes and such). 659 */ 660 if (deliverifp && !ip6_getdstifaddr(m)) { 661 struct in6_ifaddr *ia6; 662 663 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 664 if (ia6) { 665 if (!ip6_setdstifaddr(m, ia6)) { 666 /* 667 * XXX maybe we should drop the packet here, 668 * as we could not provide enough information 669 * to the upper layers. 670 */ 671 } 672 } 673 } 674 675 /* 676 * Process Hop-by-Hop options header if it's contained. 677 * m may be modified in ip6_hopopts_input(). 678 * If a JumboPayload option is included, plen will also be modified. 679 */ 680 plen = (u_int32_t)ntohs(ip6->ip6_plen); 681 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 682 struct ip6_hbh *hbh; 683 684 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 685 #if 0 /*touches NULL pointer*/ 686 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 687 #endif 688 return; /* m have already been freed */ 689 } 690 691 /* adjust pointer */ 692 ip6 = mtod(m, struct ip6_hdr *); 693 694 /* 695 * if the payload length field is 0 and the next header field 696 * indicates Hop-by-Hop Options header, then a Jumbo Payload 697 * option MUST be included. 698 */ 699 if (ip6->ip6_plen == 0 && plen == 0) { 700 /* 701 * Note that if a valid jumbo payload option is 702 * contained, ip6_hoptops_input() must set a valid 703 * (non-zero) payload length to the variable plen. 704 */ 705 ip6stat.ip6s_badoptions++; 706 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 707 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 708 icmp6_error(m, ICMP6_PARAM_PROB, 709 ICMP6_PARAMPROB_HEADER, 710 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 711 return; 712 } 713 #ifndef PULLDOWN_TEST 714 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 715 hbh = (struct ip6_hbh *)(ip6 + 1); 716 #else 717 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 718 sizeof(struct ip6_hbh)); 719 if (hbh == NULL) { 720 ip6stat.ip6s_tooshort++; 721 return; 722 } 723 #endif 724 nxt = hbh->ip6h_nxt; 725 726 /* 727 * accept the packet if a router alert option is included 728 * and we act as an IPv6 router. 729 */ 730 if (rtalert != ~0 && ip6_forwarding) 731 ours = 1; 732 } else 733 nxt = ip6->ip6_nxt; 734 735 /* 736 * Check that the amount of data in the buffers 737 * is as at least much as the IPv6 header would have us expect. 738 * Trim mbufs if longer than we expect. 739 * Drop packet if shorter than we expect. 740 */ 741 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 742 ip6stat.ip6s_tooshort++; 743 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 744 goto bad; 745 } 746 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 747 if (m->m_len == m->m_pkthdr.len) { 748 m->m_len = sizeof(struct ip6_hdr) + plen; 749 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 750 } else 751 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 752 } 753 754 /* 755 * Forward if desirable. 756 */ 757 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 758 /* 759 * If we are acting as a multicast router, all 760 * incoming multicast packets are passed to the 761 * kernel-level multicast forwarding function. 762 * The packet is returned (relatively) intact; if 763 * ip6_mforward() returns a non-zero value, the packet 764 * must be discarded, else it may be accepted below. 765 */ 766 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 767 ip6stat.ip6s_cantforward++; 768 m_freem(m); 769 return; 770 } 771 if (!ours) { 772 m_freem(m); 773 return; 774 } 775 } else if (!ours) { 776 ip6_forward(m, 0); 777 return; 778 } 779 780 ip6 = mtod(m, struct ip6_hdr *); 781 782 /* 783 * Malicious party may be able to use IPv4 mapped addr to confuse 784 * tcp/udp stack and bypass security checks (act as if it was from 785 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 786 * 787 * For SIIT end node behavior, you may want to disable the check. 788 * However, you will become vulnerable to attacks using IPv4 mapped 789 * source. 790 */ 791 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 792 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 793 ip6stat.ip6s_badscope++; 794 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 795 goto bad; 796 } 797 798 /* 799 * Tell launch routine the next header 800 */ 801 ip6stat.ip6s_delivered++; 802 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 803 nest = 0; 804 805 while (nxt != IPPROTO_DONE) { 806 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 807 ip6stat.ip6s_toomanyhdr++; 808 goto bad; 809 } 810 811 /* 812 * protection against faulty packet - there should be 813 * more sanity checks in header chain processing. 814 */ 815 if (m->m_pkthdr.len < off) { 816 ip6stat.ip6s_tooshort++; 817 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 818 goto bad; 819 } 820 821 #if 0 822 /* 823 * do we need to do it for every header? yeah, other 824 * functions can play with it (like re-allocate and copy). 825 */ 826 mhist = ip6_addaux(m); 827 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) { 828 hist = mtod(mhist, caddr_t) + mhist->m_len; 829 bcopy(&nxt, hist, sizeof(nxt)); 830 mhist->m_len += sizeof(nxt); 831 } else { 832 ip6stat.ip6s_toomanyhdr++; 833 goto bad; 834 } 835 #endif 836 837 #ifdef IPSEC 838 /* 839 * enforce IPsec policy checking if we are seeing last header. 840 * note that we do not visit this with protocols with pcb layer 841 * code - like udp/tcp/raw ip. 842 */ 843 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 844 ipsec6_in_reject(m, NULL)) { 845 ipsec6stat.in_polvio++; 846 goto bad; 847 } 848 #endif 849 850 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 851 } 852 return; 853 bad: 854 m_freem(m); 855 } 856 857 /* 858 * set/grab in6_ifaddr correspond to IPv6 destination address. 859 * XXX backward compatibility wrapper 860 */ 861 static struct mbuf * 862 ip6_setdstifaddr(m, ia6) 863 struct mbuf *m; 864 struct in6_ifaddr *ia6; 865 { 866 struct mbuf *n; 867 868 n = ip6_addaux(m); 869 if (n) 870 mtod(n, struct ip6aux *)->ip6a_dstia6 = ia6; 871 return n; /* NULL if failed to set */ 872 } 873 874 struct in6_ifaddr * 875 ip6_getdstifaddr(m) 876 struct mbuf *m; 877 { 878 struct mbuf *n; 879 880 n = ip6_findaux(m); 881 if (n) 882 return mtod(n, struct ip6aux *)->ip6a_dstia6; 883 else 884 return NULL; 885 } 886 887 /* 888 * Hop-by-Hop options header processing. If a valid jumbo payload option is 889 * included, the real payload length will be stored in plenp. 890 */ 891 static int 892 ip6_hopopts_input(plenp, rtalertp, mp, offp) 893 u_int32_t *plenp; 894 u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 895 struct mbuf **mp; 896 int *offp; 897 { 898 struct mbuf *m = *mp; 899 int off = *offp, hbhlen; 900 struct ip6_hbh *hbh; 901 u_int8_t *opt; 902 903 /* validation of the length of the header */ 904 #ifndef PULLDOWN_TEST 905 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 906 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 907 hbhlen = (hbh->ip6h_len + 1) << 3; 908 909 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 910 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 911 #else 912 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 913 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 914 if (hbh == NULL) { 915 ip6stat.ip6s_tooshort++; 916 return -1; 917 } 918 hbhlen = (hbh->ip6h_len + 1) << 3; 919 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 920 hbhlen); 921 if (hbh == NULL) { 922 ip6stat.ip6s_tooshort++; 923 return -1; 924 } 925 #endif 926 off += hbhlen; 927 hbhlen -= sizeof(struct ip6_hbh); 928 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 929 930 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 931 hbhlen, rtalertp, plenp) < 0) 932 return(-1); 933 934 *offp = off; 935 *mp = m; 936 return(0); 937 } 938 939 /* 940 * Search header for all Hop-by-hop options and process each option. 941 * This function is separate from ip6_hopopts_input() in order to 942 * handle a case where the sending node itself process its hop-by-hop 943 * options header. In such a case, the function is called from ip6_output(). 944 * 945 * The function assumes that hbh header is located right after the IPv6 header 946 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 947 * opthead + hbhlen is located in continuous memory region. 948 */ 949 int 950 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 951 struct mbuf *m; 952 u_int8_t *opthead; 953 int hbhlen; 954 u_int32_t *rtalertp; 955 u_int32_t *plenp; 956 { 957 struct ip6_hdr *ip6; 958 int optlen = 0; 959 u_int8_t *opt = opthead; 960 u_int16_t rtalert_val; 961 u_int32_t jumboplen; 962 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 963 964 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 965 switch (*opt) { 966 case IP6OPT_PAD1: 967 optlen = 1; 968 break; 969 case IP6OPT_PADN: 970 if (hbhlen < IP6OPT_MINLEN) { 971 ip6stat.ip6s_toosmall++; 972 goto bad; 973 } 974 optlen = *(opt + 1) + 2; 975 break; 976 case IP6OPT_RTALERT: 977 /* XXX may need check for alignment */ 978 if (hbhlen < IP6OPT_RTALERT_LEN) { 979 ip6stat.ip6s_toosmall++; 980 goto bad; 981 } 982 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 983 /* XXX stat */ 984 icmp6_error(m, ICMP6_PARAM_PROB, 985 ICMP6_PARAMPROB_HEADER, 986 erroff + opt + 1 - opthead); 987 return(-1); 988 } 989 optlen = IP6OPT_RTALERT_LEN; 990 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 991 *rtalertp = ntohs(rtalert_val); 992 break; 993 case IP6OPT_JUMBO: 994 /* XXX may need check for alignment */ 995 if (hbhlen < IP6OPT_JUMBO_LEN) { 996 ip6stat.ip6s_toosmall++; 997 goto bad; 998 } 999 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 1000 /* XXX stat */ 1001 icmp6_error(m, ICMP6_PARAM_PROB, 1002 ICMP6_PARAMPROB_HEADER, 1003 erroff + opt + 1 - opthead); 1004 return(-1); 1005 } 1006 optlen = IP6OPT_JUMBO_LEN; 1007 1008 /* 1009 * IPv6 packets that have non 0 payload length 1010 * must not contain a jumbo payload option. 1011 */ 1012 ip6 = mtod(m, struct ip6_hdr *); 1013 if (ip6->ip6_plen) { 1014 ip6stat.ip6s_badoptions++; 1015 icmp6_error(m, ICMP6_PARAM_PROB, 1016 ICMP6_PARAMPROB_HEADER, 1017 erroff + opt - opthead); 1018 return(-1); 1019 } 1020 1021 /* 1022 * We may see jumbolen in unaligned location, so 1023 * we'd need to perform bcopy(). 1024 */ 1025 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 1026 jumboplen = (u_int32_t)htonl(jumboplen); 1027 1028 #if 1 1029 /* 1030 * if there are multiple jumbo payload options, 1031 * *plenp will be non-zero and the packet will be 1032 * rejected. 1033 * the behavior may need some debate in ipngwg - 1034 * multiple options does not make sense, however, 1035 * there's no explicit mention in specification. 1036 */ 1037 if (*plenp != 0) { 1038 ip6stat.ip6s_badoptions++; 1039 icmp6_error(m, ICMP6_PARAM_PROB, 1040 ICMP6_PARAMPROB_HEADER, 1041 erroff + opt + 2 - opthead); 1042 return(-1); 1043 } 1044 #endif 1045 1046 /* 1047 * jumbo payload length must be larger than 65535. 1048 */ 1049 if (jumboplen <= IPV6_MAXPACKET) { 1050 ip6stat.ip6s_badoptions++; 1051 icmp6_error(m, ICMP6_PARAM_PROB, 1052 ICMP6_PARAMPROB_HEADER, 1053 erroff + opt + 2 - opthead); 1054 return(-1); 1055 } 1056 *plenp = jumboplen; 1057 1058 break; 1059 default: /* unknown option */ 1060 if (hbhlen < IP6OPT_MINLEN) { 1061 ip6stat.ip6s_toosmall++; 1062 goto bad; 1063 } 1064 optlen = ip6_unknown_opt(opt, m, 1065 erroff + opt - opthead); 1066 if (optlen == -1) 1067 return(-1); 1068 optlen += 2; 1069 break; 1070 } 1071 } 1072 1073 return(0); 1074 1075 bad: 1076 m_freem(m); 1077 return(-1); 1078 } 1079 1080 /* 1081 * Unknown option processing. 1082 * The third argument `off' is the offset from the IPv6 header to the option, 1083 * which is necessary if the IPv6 header the and option header and IPv6 header 1084 * is not continuous in order to return an ICMPv6 error. 1085 */ 1086 int 1087 ip6_unknown_opt(optp, m, off) 1088 u_int8_t *optp; 1089 struct mbuf *m; 1090 int off; 1091 { 1092 struct ip6_hdr *ip6; 1093 1094 switch (IP6OPT_TYPE(*optp)) { 1095 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1096 return((int)*(optp + 1)); 1097 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1098 m_freem(m); 1099 return(-1); 1100 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1101 ip6stat.ip6s_badoptions++; 1102 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1103 return(-1); 1104 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1105 ip6stat.ip6s_badoptions++; 1106 ip6 = mtod(m, struct ip6_hdr *); 1107 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1108 (m->m_flags & (M_BCAST|M_MCAST))) 1109 m_freem(m); 1110 else 1111 icmp6_error(m, ICMP6_PARAM_PROB, 1112 ICMP6_PARAMPROB_OPTION, off); 1113 return(-1); 1114 } 1115 1116 m_freem(m); /* XXX: NOTREACHED */ 1117 return(-1); 1118 } 1119 1120 /* 1121 * Create the "control" list for this pcb. 1122 * The function will not modify mbuf chain at all. 1123 * 1124 * with KAME mbuf chain restriction: 1125 * The routine will be called from upper layer handlers like tcp6_input(). 1126 * Thus the routine assumes that the caller (tcp6_input) have already 1127 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1128 * very first mbuf on the mbuf chain. 1129 */ 1130 void 1131 ip6_savecontrol(in6p, mp, ip6, m) 1132 struct inpcb *in6p; 1133 struct mbuf **mp; 1134 struct ip6_hdr *ip6; 1135 struct mbuf *m; 1136 { 1137 struct proc *p = curproc; /* XXX */ 1138 int privileged = 0; 1139 int rthdr_exist = 0; 1140 1141 1142 if (p && !suser(p)) 1143 privileged++; 1144 1145 #ifdef SO_TIMESTAMP 1146 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 1147 struct timeval tv; 1148 1149 microtime(&tv); 1150 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1151 SCM_TIMESTAMP, SOL_SOCKET); 1152 if (*mp) { 1153 mp = &(*mp)->m_next; 1154 } 1155 } 1156 #endif 1157 1158 /* RFC 2292 sec. 5 */ 1159 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 1160 struct in6_pktinfo pi6; 1161 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1162 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr)) 1163 pi6.ipi6_addr.s6_addr16[1] = 0; 1164 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif) 1165 ? m->m_pkthdr.rcvif->if_index 1166 : 0; 1167 *mp = sbcreatecontrol((caddr_t) &pi6, 1168 sizeof(struct in6_pktinfo), IPV6_PKTINFO, 1169 IPPROTO_IPV6); 1170 if (*mp) 1171 mp = &(*mp)->m_next; 1172 } 1173 1174 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 1175 int hlim = ip6->ip6_hlim & 0xff; 1176 *mp = sbcreatecontrol((caddr_t) &hlim, 1177 sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6); 1178 if (*mp) 1179 mp = &(*mp)->m_next; 1180 } 1181 1182 /* 1183 * IPV6_HOPOPTS socket option. We require super-user privilege 1184 * for the option, but it might be too strict, since there might 1185 * be some hop-by-hop options which can be returned to normal user. 1186 * See RFC 2292 section 6. 1187 */ 1188 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) { 1189 /* 1190 * Check if a hop-by-hop options header is contatined in the 1191 * received packet, and if so, store the options as ancillary 1192 * data. Note that a hop-by-hop options header must be 1193 * just after the IPv6 header, which fact is assured through 1194 * the IPv6 input processing. 1195 */ 1196 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1197 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1198 struct ip6_hbh *hbh; 1199 int hbhlen = 0; 1200 #ifdef PULLDOWN_TEST 1201 struct mbuf *ext; 1202 #endif 1203 1204 #ifndef PULLDOWN_TEST 1205 hbh = (struct ip6_hbh *)(ip6 + 1); 1206 hbhlen = (hbh->ip6h_len + 1) << 3; 1207 #else 1208 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1209 ip6->ip6_nxt); 1210 if (ext == NULL) { 1211 ip6stat.ip6s_tooshort++; 1212 return; 1213 } 1214 hbh = mtod(ext, struct ip6_hbh *); 1215 hbhlen = (hbh->ip6h_len + 1) << 3; 1216 if (hbhlen != ext->m_len) { 1217 m_freem(ext); 1218 ip6stat.ip6s_tooshort++; 1219 return; 1220 } 1221 #endif 1222 1223 /* 1224 * XXX: We copy whole the header even if a jumbo 1225 * payload option is included, which option is to 1226 * be removed before returning in the RFC 2292. 1227 * Note: this constraint is removed in 2292bis. 1228 */ 1229 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1230 IPV6_HOPOPTS, IPPROTO_IPV6); 1231 if (*mp) 1232 mp = &(*mp)->m_next; 1233 #ifdef PULLDOWN_TEST 1234 m_freem(ext); 1235 #endif 1236 } 1237 } 1238 1239 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ 1240 if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1241 int proto, off, nxt; 1242 1243 /* 1244 * go through the header chain to see if a routing header is 1245 * contained in the packet. We need this information to store 1246 * destination options headers (if any) properly. 1247 * XXX: performance issue. We should record this info when 1248 * processing extension headers in incoming routine. 1249 * (todo) use m_aux? 1250 */ 1251 proto = IPPROTO_IPV6; 1252 off = 0; 1253 nxt = -1; 1254 while (1) { 1255 int newoff; 1256 1257 newoff = ip6_nexthdr(m, off, proto, &nxt); 1258 if (newoff < 0) 1259 break; 1260 if (newoff < off) /* invalid, check for safety */ 1261 break; 1262 if ((proto = nxt) == IPPROTO_ROUTING) { 1263 rthdr_exist = 1; 1264 break; 1265 } 1266 off = newoff; 1267 } 1268 } 1269 1270 if ((in6p->in6p_flags & 1271 (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1272 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1273 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1274 1275 /* 1276 * Search for destination options headers or routing 1277 * header(s) through the header chain, and stores each 1278 * header as ancillary data. 1279 * Note that the order of the headers remains in 1280 * the chain of ancillary data. 1281 */ 1282 while (1) { /* is explicit loop prevention necessary? */ 1283 struct ip6_ext *ip6e = NULL; 1284 int elen; 1285 #ifdef PULLDOWN_TEST 1286 struct mbuf *ext = NULL; 1287 #endif 1288 1289 /* 1290 * if it is not an extension header, don't try to 1291 * pull it from the chain. 1292 */ 1293 switch (nxt) { 1294 case IPPROTO_DSTOPTS: 1295 case IPPROTO_ROUTING: 1296 case IPPROTO_HOPOPTS: 1297 case IPPROTO_AH: /* is it possible? */ 1298 break; 1299 default: 1300 goto loopend; 1301 } 1302 1303 #ifndef PULLDOWN_TEST 1304 if (off + sizeof(*ip6e) > m->m_len) 1305 goto loopend; 1306 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1307 if (nxt == IPPROTO_AH) 1308 elen = (ip6e->ip6e_len + 2) << 2; 1309 else 1310 elen = (ip6e->ip6e_len + 1) << 3; 1311 if (off + elen > m->m_len) 1312 goto loopend; 1313 #else 1314 ext = ip6_pullexthdr(m, off, nxt); 1315 if (ext == NULL) { 1316 ip6stat.ip6s_tooshort++; 1317 return; 1318 } 1319 ip6e = mtod(ext, struct ip6_ext *); 1320 if (nxt == IPPROTO_AH) 1321 elen = (ip6e->ip6e_len + 2) << 2; 1322 else 1323 elen = (ip6e->ip6e_len + 1) << 3; 1324 if (elen != ext->m_len) { 1325 m_freem(ext); 1326 ip6stat.ip6s_tooshort++; 1327 return; 1328 } 1329 #endif 1330 1331 switch (nxt) { 1332 case IPPROTO_DSTOPTS: 1333 if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0) 1334 break; 1335 1336 /* 1337 * We also require super-user privilege for 1338 * the option. 1339 * See the comments on IN6_HOPOPTS. 1340 */ 1341 if (!privileged) 1342 break; 1343 1344 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1345 IPV6_DSTOPTS, 1346 IPPROTO_IPV6); 1347 if (*mp) 1348 mp = &(*mp)->m_next; 1349 break; 1350 case IPPROTO_ROUTING: 1351 if (!in6p->in6p_flags & IN6P_RTHDR) 1352 break; 1353 1354 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1355 IPV6_RTHDR, 1356 IPPROTO_IPV6); 1357 if (*mp) 1358 mp = &(*mp)->m_next; 1359 break; 1360 case IPPROTO_HOPOPTS: 1361 case IPPROTO_AH: /* is it possible? */ 1362 break; 1363 1364 default: 1365 /* 1366 * other cases have been filtered in the above. 1367 * none will visit this case. here we supply 1368 * the code just in case (nxt overwritten or 1369 * other cases). 1370 */ 1371 #ifdef PULLDOWN_TEST 1372 m_freem(ext); 1373 #endif 1374 goto loopend; 1375 1376 } 1377 1378 /* proceed with the next header. */ 1379 off += elen; 1380 nxt = ip6e->ip6e_nxt; 1381 ip6e = NULL; 1382 #ifdef PULLDOWN_TEST 1383 m_freem(ext); 1384 ext = NULL; 1385 #endif 1386 } 1387 loopend: 1388 ; 1389 } 1390 1391 } 1392 1393 #ifdef PULLDOWN_TEST 1394 /* 1395 * pull single extension header from mbuf chain. returns single mbuf that 1396 * contains the result, or NULL on error. 1397 */ 1398 static struct mbuf * 1399 ip6_pullexthdr(m, off, nxt) 1400 struct mbuf *m; 1401 size_t off; 1402 int nxt; 1403 { 1404 struct ip6_ext ip6e; 1405 size_t elen; 1406 struct mbuf *n; 1407 1408 #ifdef DIAGNOSTIC 1409 switch (nxt) { 1410 case IPPROTO_DSTOPTS: 1411 case IPPROTO_ROUTING: 1412 case IPPROTO_HOPOPTS: 1413 case IPPROTO_AH: /* is it possible? */ 1414 break; 1415 default: 1416 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1417 } 1418 #endif 1419 1420 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1421 if (nxt == IPPROTO_AH) 1422 elen = (ip6e.ip6e_len + 2) << 2; 1423 else 1424 elen = (ip6e.ip6e_len + 1) << 3; 1425 1426 MGET(n, M_DONTWAIT, MT_DATA); 1427 if (n && elen >= MLEN) { 1428 MCLGET(n, M_DONTWAIT); 1429 if ((n->m_flags & M_EXT) == 0) { 1430 m_free(n); 1431 n = NULL; 1432 } 1433 } 1434 if (!n) 1435 return NULL; 1436 1437 n->m_len = 0; 1438 if (elen >= M_TRAILINGSPACE(n)) { 1439 m_free(n); 1440 return NULL; 1441 } 1442 1443 m_copydata(m, off, elen, mtod(n, caddr_t)); 1444 n->m_len = elen; 1445 return n; 1446 } 1447 #endif 1448 1449 /* 1450 * Get pointer to the previous header followed by the header 1451 * currently processed. 1452 * XXX: This function supposes that 1453 * M includes all headers, 1454 * the next header field and the header length field of each header 1455 * are valid, and 1456 * the sum of each header length equals to OFF. 1457 * Because of these assumptions, this function must be called very 1458 * carefully. Moreover, it will not be used in the near future when 1459 * we develop `neater' mechanism to process extension headers. 1460 */ 1461 char * 1462 ip6_get_prevhdr(m, off) 1463 struct mbuf *m; 1464 int off; 1465 { 1466 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1467 1468 if (off == sizeof(struct ip6_hdr)) 1469 return(&ip6->ip6_nxt); 1470 else { 1471 int len, nxt; 1472 struct ip6_ext *ip6e = NULL; 1473 1474 nxt = ip6->ip6_nxt; 1475 len = sizeof(struct ip6_hdr); 1476 while (len < off) { 1477 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1478 1479 switch (nxt) { 1480 case IPPROTO_FRAGMENT: 1481 len += sizeof(struct ip6_frag); 1482 break; 1483 case IPPROTO_AH: 1484 len += (ip6e->ip6e_len + 2) << 2; 1485 break; 1486 default: 1487 len += (ip6e->ip6e_len + 1) << 3; 1488 break; 1489 } 1490 nxt = ip6e->ip6e_nxt; 1491 } 1492 if (ip6e) 1493 return(&ip6e->ip6e_nxt); 1494 else 1495 return NULL; 1496 } 1497 } 1498 1499 /* 1500 * get next header offset. m will be retained. 1501 */ 1502 int 1503 ip6_nexthdr(m, off, proto, nxtp) 1504 struct mbuf *m; 1505 int off; 1506 int proto; 1507 int *nxtp; 1508 { 1509 struct ip6_hdr ip6; 1510 struct ip6_ext ip6e; 1511 struct ip6_frag fh; 1512 1513 /* just in case */ 1514 if (m == NULL) 1515 panic("ip6_nexthdr: m == NULL"); 1516 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1517 return -1; 1518 1519 switch (proto) { 1520 case IPPROTO_IPV6: 1521 if (m->m_pkthdr.len < off + sizeof(ip6)) 1522 return -1; 1523 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1524 if (nxtp) 1525 *nxtp = ip6.ip6_nxt; 1526 off += sizeof(ip6); 1527 return off; 1528 1529 case IPPROTO_FRAGMENT: 1530 /* 1531 * terminate parsing if it is not the first fragment, 1532 * it does not make sense to parse through it. 1533 */ 1534 if (m->m_pkthdr.len < off + sizeof(fh)) 1535 return -1; 1536 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1537 if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0) 1538 return -1; 1539 if (nxtp) 1540 *nxtp = fh.ip6f_nxt; 1541 off += sizeof(struct ip6_frag); 1542 return off; 1543 1544 case IPPROTO_AH: 1545 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1546 return -1; 1547 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1548 if (nxtp) 1549 *nxtp = ip6e.ip6e_nxt; 1550 off += (ip6e.ip6e_len + 2) << 2; 1551 return off; 1552 1553 case IPPROTO_HOPOPTS: 1554 case IPPROTO_ROUTING: 1555 case IPPROTO_DSTOPTS: 1556 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1557 return -1; 1558 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1559 if (nxtp) 1560 *nxtp = ip6e.ip6e_nxt; 1561 off += (ip6e.ip6e_len + 1) << 3; 1562 return off; 1563 1564 case IPPROTO_NONE: 1565 case IPPROTO_ESP: 1566 case IPPROTO_IPCOMP: 1567 /* give up */ 1568 return -1; 1569 1570 default: 1571 return -1; 1572 } 1573 1574 return -1; 1575 } 1576 1577 /* 1578 * get offset for the last header in the chain. m will be kept untainted. 1579 */ 1580 int 1581 ip6_lasthdr(m, off, proto, nxtp) 1582 struct mbuf *m; 1583 int off; 1584 int proto; 1585 int *nxtp; 1586 { 1587 int newoff; 1588 int nxt; 1589 1590 if (!nxtp) { 1591 nxt = -1; 1592 nxtp = &nxt; 1593 } 1594 while (1) { 1595 newoff = ip6_nexthdr(m, off, proto, nxtp); 1596 if (newoff < 0) 1597 return off; 1598 else if (newoff < off) 1599 return -1; /* invalid */ 1600 else if (newoff == off) 1601 return newoff; 1602 1603 off = newoff; 1604 proto = *nxtp; 1605 } 1606 } 1607 1608 struct mbuf * 1609 ip6_addaux(m) 1610 struct mbuf *m; 1611 { 1612 struct mbuf *n; 1613 1614 #ifdef DIAGNOSTIC 1615 if (sizeof(struct ip6aux) > MHLEN) 1616 panic("assumption failed on sizeof(ip6aux)"); 1617 #endif 1618 n = m_aux_find(m, AF_INET6, -1); 1619 if (n) { 1620 if (n->m_len < sizeof(struct ip6aux)) { 1621 printf("conflicting use of ip6aux"); 1622 return NULL; 1623 } 1624 } else { 1625 n = m_aux_add(m, AF_INET6, -1); 1626 n->m_len = sizeof(struct ip6aux); 1627 bzero(mtod(n, caddr_t), n->m_len); 1628 } 1629 return n; 1630 } 1631 1632 struct mbuf * 1633 ip6_findaux(m) 1634 struct mbuf *m; 1635 { 1636 struct mbuf *n; 1637 1638 n = m_aux_find(m, AF_INET6, -1); 1639 if (n && n->m_len < sizeof(struct ip6aux)) { 1640 printf("conflicting use of ip6aux"); 1641 n = NULL; 1642 } 1643 return n; 1644 } 1645 1646 void 1647 ip6_delaux(m) 1648 struct mbuf *m; 1649 { 1650 struct mbuf *n; 1651 1652 n = m_aux_find(m, AF_INET6, -1); 1653 if (n) 1654 m_aux_delete(m, n); 1655 } 1656 1657 /* 1658 * System control for IP6 1659 */ 1660 1661 u_char inet6ctlerrmap[PRC_NCMDS] = { 1662 0, 0, 0, 0, 1663 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1664 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1665 EMSGSIZE, EHOSTUNREACH, 0, 0, 1666 0, 0, 0, 0, 1667 ENOPROTOOPT 1668 }; 1669