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