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