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