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