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