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