ip6_input.c revision 171167
162587Sitojun/* $FreeBSD: head/sys/netinet6/ip6_input.c 171167 2007-07-03 12:13:45Z gnn $ */ 295023Ssuz/* $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $ */ 362587Sitojun 4139826Simp/*- 553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 653541Sshin * All rights reserved. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. Neither the name of the project nor the names of its contributors 1753541Sshin * may be used to endorse or promote products derived from this software 1853541Sshin * without specific prior written permission. 1953541Sshin * 2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3053541Sshin * SUCH DAMAGE. 3153541Sshin */ 3253541Sshin 33139826Simp/*- 3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3553541Sshin * The Regents of the University of California. All rights reserved. 3653541Sshin * 3753541Sshin * Redistribution and use in source and binary forms, with or without 3853541Sshin * modification, are permitted provided that the following conditions 3953541Sshin * are met: 4053541Sshin * 1. Redistributions of source code must retain the above copyright 4153541Sshin * notice, this list of conditions and the following disclaimer. 4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4353541Sshin * notice, this list of conditions and the following disclaimer in the 4453541Sshin * documentation and/or other materials provided with the distribution. 4553541Sshin * 4. Neither the name of the University nor the names of its contributors 4653541Sshin * may be used to endorse or promote products derived from this software 4753541Sshin * without specific prior written permission. 4853541Sshin * 4953541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5053541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5153541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5253541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5353541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5453541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5553541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5653541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5753541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5853541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5953541Sshin * SUCH DAMAGE. 6053541Sshin * 6153541Sshin * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 6253541Sshin */ 6353541Sshin 6462587Sitojun#include "opt_inet.h" 6562587Sitojun#include "opt_inet6.h" 6655009Sshin#include "opt_ipsec.h" 6755009Sshin 6853541Sshin#include <sys/param.h> 6953541Sshin#include <sys/systm.h> 7078064Sume#include <sys/malloc.h> 7153541Sshin#include <sys/mbuf.h> 7283366Sjulian#include <sys/proc.h> 7353541Sshin#include <sys/domain.h> 7453541Sshin#include <sys/protosw.h> 7553541Sshin#include <sys/socket.h> 7653541Sshin#include <sys/socketvar.h> 7753541Sshin#include <sys/errno.h> 7853541Sshin#include <sys/time.h> 7953541Sshin#include <sys/kernel.h> 8053541Sshin#include <sys/syslog.h> 8153541Sshin 8253541Sshin#include <net/if.h> 8353541Sshin#include <net/if_types.h> 8453541Sshin#include <net/if_dl.h> 8553541Sshin#include <net/route.h> 8653541Sshin#include <net/netisr.h> 8764060Sdarrenr#include <net/pfil.h> 8853541Sshin 8953541Sshin#include <netinet/in.h> 9053541Sshin#include <netinet/in_systm.h> 9162587Sitojun#ifdef INET 9253541Sshin#include <netinet/ip.h> 9353541Sshin#include <netinet/ip_icmp.h> 9495023Ssuz#endif /* INET */ 9562587Sitojun#include <netinet/ip6.h> 9653541Sshin#include <netinet6/in6_var.h> 9753541Sshin#include <netinet6/ip6_var.h> 9862587Sitojun#include <netinet/in_pcb.h> 9962587Sitojun#include <netinet/icmp6.h> 100121161Sume#include <netinet6/scope6_var.h> 10153541Sshin#include <netinet6/in6_ifattach.h> 10253541Sshin#include <netinet6/nd6.h> 10353541Sshin 104171167Sgnn#ifdef IPSEC 105105199Ssam#include <netipsec/ipsec.h> 106171133Sgnn#include <netinet6/ip6_ipsec.h> 107105199Ssam#include <netipsec/ipsec6.h> 108171167Sgnn#endif /* IPSEC */ 109105199Ssam 11053541Sshin#include <netinet6/ip6protosw.h> 11153541Sshin 11262587Sitojunextern struct domain inet6domain; 11353541Sshin 11462587Sitojunu_char ip6_protox[IPPROTO_MAX]; 115111888Sjlemonstatic struct ifqueue ip6intrq; 11662587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN; 11762587Sitojunstruct in6_ifaddr *in6_ifaddr; 11853541Sshin 11978064Sumeextern struct callout in6_tmpaddrtimer_ch; 12078064Sume 12162587Sitojunint ip6_forward_srcrt; /* XXX */ 12262587Sitojunint ip6_sourcecheck; /* XXX */ 12362587Sitojunint ip6_sourcecheck_interval; /* XXX */ 12453541Sshin 12578064Sumeint ip6_ours_check_algorithm; 12678064Sume 127120386Ssamstruct pfil_head inet6_pfil_hook; 12878064Sume 12962587Sitojunstruct ip6stat ip6stat; 13053541Sshin 13162587Sitojunstatic void ip6_init2 __P((void *)); 132121673Sumestatic struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *)); 13362587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 13462587Sitojun#ifdef PULLDOWN_TEST 13562587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int)); 13653541Sshin#endif 13753541Sshin 13853541Sshin/* 13953541Sshin * IP6 initialization: fill in IP6 protocol switch table. 14053541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler. 14153541Sshin */ 14253541Sshinvoid 14353541Sshinip6_init() 14453541Sshin{ 14578064Sume struct ip6protosw *pr; 14678064Sume int i; 14753541Sshin 14878064Sume#ifdef DIAGNOSTIC 14978064Sume if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 15078064Sume panic("sizeof(protosw) != sizeof(ip6protosw)"); 15178064Sume#endif 15253541Sshin pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 15353541Sshin if (pr == 0) 15453541Sshin panic("ip6_init"); 155136689Sandre 156136689Sandre /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 15753541Sshin for (i = 0; i < IPPROTO_MAX; i++) 15853541Sshin ip6_protox[i] = pr - inet6sw; 159136689Sandre /* 160136689Sandre * Cycle through IP protocols and put them into the appropriate place 161136689Sandre * in ip6_protox[]. 162136689Sandre */ 16353541Sshin for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 16453541Sshin pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 16553541Sshin if (pr->pr_domain->dom_family == PF_INET6 && 166136689Sandre pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 167136689Sandre /* Be careful to only index valid IP protocols. */ 168143675Ssam if (pr->pr_protocol < IPPROTO_MAX) 169136689Sandre ip6_protox[pr->pr_protocol] = pr - inet6sw; 170136689Sandre } 171134383Sandre 172134383Sandre /* Initialize packet filter hooks. */ 173120386Ssam inet6_pfil_hook.ph_type = PFIL_TYPE_AF; 174120386Ssam inet6_pfil_hook.ph_af = AF_INET6; 175120386Ssam if ((i = pfil_head_register(&inet6_pfil_hook)) != 0) 176120386Ssam printf("%s: WARNING: unable to register pfil hook, " 177120386Ssam "error %d\n", __func__, i); 178134383Sandre 17953541Sshin ip6intrq.ifq_maxlen = ip6qmaxlen; 18093818Sjhb mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF); 181122320Ssam netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0); 182121161Sume scope6_init(); 183121742Sume addrsel_policy_init(); 18453541Sshin nd6_init(); 18553541Sshin frag6_init(); 186120648Sume ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR; 18753541Sshin} 18853541Sshin 18953541Sshinstatic void 19053541Sshinip6_init2(dummy) 19153541Sshin void *dummy; 19253541Sshin{ 19353541Sshin 19453541Sshin /* nd6_timer_init */ 19578064Sume callout_init(&nd6_timer_ch, 0); 19678064Sume callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 19778064Sume 19878064Sume /* timer for regeneranation of temporary addresses randomize ID */ 199121806Sume callout_init(&in6_tmpaddrtimer_ch, 0); 20078064Sume callout_reset(&in6_tmpaddrtimer_ch, 20178064Sume (ip6_temp_preferred_lifetime - ip6_desync_factor - 20278064Sume ip6_temp_regen_advance) * hz, 20378064Sume in6_tmpaddrtimer, NULL); 20453541Sshin} 20553541Sshin 20653541Sshin/* cheat */ 20755009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */ 20855009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 20953541Sshin 21053541Sshinextern struct route_in6 ip6_forward_rt; 21153541Sshin 21253541Sshinvoid 21353541Sshinip6_input(m) 21453541Sshin struct mbuf *m; 21553541Sshin{ 21653541Sshin struct ip6_hdr *ip6; 21753541Sshin int off = sizeof(struct ip6_hdr), nest; 21853541Sshin u_int32_t plen; 21953541Sshin u_int32_t rtalert = ~0; 22053541Sshin int nxt, ours = 0; 22153541Sshin struct ifnet *deliverifp = NULL; 222121143Ssam struct in6_addr odst; 223121143Ssam int srcrt = 0; 224121144Ssam 225122320Ssam GIANT_REQUIRED; /* XXX for now */ 226171133Sgnn 227171167Sgnn#ifdef IPSEC 22853541Sshin /* 22953541Sshin * should the inner packet be considered authentic? 23053541Sshin * see comment in ah4_input(). 231171133Sgnn * NB: m cannot be NULL when passed to the input routine 23253541Sshin */ 23353541Sshin 234171133Sgnn m->m_flags &= ~M_AUTHIPHDR; 235171133Sgnn m->m_flags &= ~M_AUTHIPDGM; 236171133Sgnn 237171167Sgnn#endif /* IPSEC */ 238171133Sgnn 23953541Sshin /* 240121630Sume * make sure we don't have onion peering information into m_tag. 24178064Sume */ 24278064Sume ip6_delaux(m); 24378064Sume 24478064Sume /* 24595023Ssuz * mbuf statistics 24653541Sshin */ 24753541Sshin if (m->m_flags & M_EXT) { 24853541Sshin if (m->m_next) 24953541Sshin ip6stat.ip6s_mext2m++; 25053541Sshin else 25153541Sshin ip6stat.ip6s_mext1++; 25253541Sshin } else { 25378064Sume#define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0])) 25453541Sshin if (m->m_next) { 25553541Sshin if (m->m_flags & M_LOOP) { 256120913Sume ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */ 25778064Sume } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 25853541Sshin ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 25953541Sshin else 26053541Sshin ip6stat.ip6s_m2m[0]++; 26153541Sshin } else 26253541Sshin ip6stat.ip6s_m1++; 26378064Sume#undef M2MMAX 26453541Sshin } 26553541Sshin 266151474Ssuz /* drop the packet if IPv6 operation is disabled on the IF */ 267151474Ssuz if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) { 268151474Ssuz m_freem(m); 269151474Ssuz return; 270151474Ssuz } 271151474Ssuz 27253541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 27353541Sshin ip6stat.ip6s_total++; 27453541Sshin 27562587Sitojun#ifndef PULLDOWN_TEST 27674336Skuriyama /* 27774336Skuriyama * L2 bridge code and some other code can return mbuf chain 27874336Skuriyama * that does not conform to KAME requirement. too bad. 27974336Skuriyama * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 28074336Skuriyama */ 28174336Skuriyama if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 28274336Skuriyama struct mbuf *n; 28374336Skuriyama 284111119Simp MGETHDR(n, M_DONTWAIT, MT_HEADER); 28577003Sume if (n) 286108466Ssam M_MOVE_PKTHDR(n, m); 287108825Ssam if (n && n->m_pkthdr.len > MHLEN) { 288111119Simp MCLGET(n, M_DONTWAIT); 28974336Skuriyama if ((n->m_flags & M_EXT) == 0) { 29074336Skuriyama m_freem(n); 29174336Skuriyama n = NULL; 29274336Skuriyama } 29374336Skuriyama } 29495023Ssuz if (n == NULL) { 29577003Sume m_freem(m); 296120913Sume return; /* ENOBUFS */ 29777003Sume } 29874336Skuriyama 299108825Ssam m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 300108825Ssam n->m_len = n->m_pkthdr.len; 30174336Skuriyama m_freem(m); 30274336Skuriyama m = n; 30374336Skuriyama } 304120913Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */); 30562587Sitojun#endif 30653541Sshin 30753541Sshin if (m->m_len < sizeof(struct ip6_hdr)) { 30853541Sshin struct ifnet *inifp; 30953541Sshin inifp = m->m_pkthdr.rcvif; 310120913Sume if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 31153541Sshin ip6stat.ip6s_toosmall++; 31253541Sshin in6_ifstat_inc(inifp, ifs6_in_hdrerr); 31353541Sshin return; 31453541Sshin } 31553541Sshin } 31653541Sshin 31753541Sshin ip6 = mtod(m, struct ip6_hdr *); 31853541Sshin 31953541Sshin if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 32053541Sshin ip6stat.ip6s_badvers++; 32153541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 32253541Sshin goto bad; 32353541Sshin } 32453541Sshin 32553541Sshin ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 32653541Sshin 32753541Sshin /* 32878064Sume * Check against address spoofing/corruption. 32953541Sshin */ 33053541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 33153541Sshin IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 33278064Sume /* 33378064Sume * XXX: "badscope" is not very suitable for a multicast source. 33478064Sume */ 33553541Sshin ip6stat.ip6s_badscope++; 33653541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 33753541Sshin goto bad; 33853541Sshin } 339126444Sume if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 340126444Sume !(m->m_flags & M_LOOP)) { 341126444Sume /* 342126444Sume * In this case, the packet should come from the loopback 343126444Sume * interface. However, we cannot just check the if_flags, 344126444Sume * because ip6_mloopback() passes the "actual" interface 345126444Sume * as the outgoing/incoming interface. 346126444Sume */ 34778064Sume ip6stat.ip6s_badscope++; 34878064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 34978064Sume goto bad; 35078064Sume } 35195023Ssuz 352130416Smlaier#ifdef ALTQ 353130416Smlaier if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) { 354130416Smlaier /* packet is dropped by traffic conditioner */ 355130416Smlaier return; 356130416Smlaier } 357130416Smlaier#endif 35862587Sitojun /* 35978064Sume * The following check is not documented in specs. A malicious 36078064Sume * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 36178064Sume * and bypass security checks (act as if it was from 127.0.0.1 by using 362120913Sume * IPv6 src ::ffff:127.0.0.1). Be cautious. 36378064Sume * 36478064Sume * This check chokes if we are in an SIIT cloud. As none of BSDs 36578064Sume * support IPv4-less kernel compilation, we cannot support SIIT 36678064Sume * environment at all. So, it makes more sense for us to reject any 36778064Sume * malicious packets for non-SIIT environment, than try to do a 368120913Sume * partial support for SIIT environment. 36962587Sitojun */ 37078064Sume if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 37178064Sume IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 37278064Sume ip6stat.ip6s_badscope++; 37378064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 37478064Sume goto bad; 37578064Sume } 37662587Sitojun#if 0 37762587Sitojun /* 37862587Sitojun * Reject packets with IPv4 compatible addresses (auto tunnel). 37962587Sitojun * 38062587Sitojun * The code forbids auto tunnel relay case in RFC1933 (the check is 38162587Sitojun * stronger than RFC1933). We may want to re-enable it if mech-xx 38262587Sitojun * is revised to forbid relaying case. 38362587Sitojun */ 38462587Sitojun if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 38562587Sitojun IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 38662587Sitojun ip6stat.ip6s_badscope++; 38762587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 38862587Sitojun goto bad; 38962587Sitojun } 39062587Sitojun#endif 39175246Sume 392126444Sume /* 393126508Smlaier * Run through list of hooks for input packets. 394126508Smlaier * 395126508Smlaier * NB: Beware of the destination address changing 396126508Smlaier * (e.g. by NAT rewriting). When this happens, 397126508Smlaier * tell ip6_forward to do the right thing. 398126508Smlaier */ 399126508Smlaier odst = ip6->ip6_dst; 400134383Sandre 401134383Sandre /* Jump over all PFIL processing if hooks are not active. */ 402155201Scsjp if (!PFIL_HOOKED(&inet6_pfil_hook)) 403134383Sandre goto passin; 404134383Sandre 405135920Smlaier if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL)) 406126508Smlaier return; 407126508Smlaier if (m == NULL) /* consumed by filter */ 408126508Smlaier return; 409126508Smlaier ip6 = mtod(m, struct ip6_hdr *); 410126508Smlaier srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 411126508Smlaier 412134383Sandrepassin: 413126508Smlaier /* 414154804Sume * Disambiguate address scope zones (if there is ambiguity). 415154804Sume * We first make sure that the original source or destination address 416154804Sume * is not in our internal form for scoped addresses. Such addresses 417154804Sume * are not necessarily invalid spec-wise, but we cannot accept them due 418154804Sume * to the usage conflict. 419154804Sume * in6_setscope() then also checks and rejects the cases where src or 420154804Sume * dst are the loopback address and the receiving interface 421154804Sume * is not loopback. 422154804Sume */ 423154804Sume if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) { 424154804Sume ip6stat.ip6s_badscope++; /* XXX */ 425154804Sume goto bad; 426154804Sume } 427154804Sume if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) || 428154804Sume in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) { 429154804Sume ip6stat.ip6s_badscope++; 430154804Sume goto bad; 431154804Sume } 432154804Sume 433154804Sume /* 43453541Sshin * Multicast check 43553541Sshin */ 43653541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 437120913Sume struct in6_multi *in6m = 0; 43853541Sshin 43953541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 44053541Sshin /* 44153541Sshin * See if we belong to the destination multicast group on the 44253541Sshin * arrival interface. 44353541Sshin */ 44453541Sshin IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 44553541Sshin if (in6m) 44653541Sshin ours = 1; 44756723Sshin else if (!ip6_mrouter) { 44853541Sshin ip6stat.ip6s_notmember++; 44953541Sshin ip6stat.ip6s_cantforward++; 45053541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 45153541Sshin goto bad; 45253541Sshin } 45353541Sshin deliverifp = m->m_pkthdr.rcvif; 45453541Sshin goto hbhcheck; 45553541Sshin } 45653541Sshin 45753541Sshin /* 45853541Sshin * Unicast check 45953541Sshin */ 46062587Sitojun if (ip6_forward_rt.ro_rt != NULL && 461120913Sume (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 46262587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 463120913Sume &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 46462587Sitojun ip6stat.ip6s_forward_cachehit++; 46562587Sitojun else { 46678064Sume struct sockaddr_in6 *dst6; 46778064Sume 46853541Sshin if (ip6_forward_rt.ro_rt) { 46962587Sitojun /* route is down or destination is different */ 47062587Sitojun ip6stat.ip6s_forward_cachemiss++; 47153541Sshin RTFREE(ip6_forward_rt.ro_rt); 47253541Sshin ip6_forward_rt.ro_rt = 0; 47353541Sshin } 47462587Sitojun 47553541Sshin bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 47678064Sume dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 47778064Sume dst6->sin6_len = sizeof(struct sockaddr_in6); 47878064Sume dst6->sin6_family = AF_INET6; 47978064Sume dst6->sin6_addr = ip6->ip6_dst; 48053541Sshin 481122921Sandre rtalloc((struct route *)&ip6_forward_rt); 48253541Sshin } 48353541Sshin 48453541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 48553541Sshin 48653541Sshin /* 48753541Sshin * Accept the packet if the forwarding interface to the destination 48853541Sshin * according to the routing table is the loopback interface, 48953541Sshin * unless the associated route has a gateway. 49053541Sshin * Note that this approach causes to accept a packet if there is a 49153541Sshin * route to the loopback interface for the destination of the packet. 49253541Sshin * But we think it's even useful in some situations, e.g. when using 49353541Sshin * a special daemon which wants to intercept the packet. 49478064Sume * 49578064Sume * XXX: some OSes automatically make a cloned route for the destination 49678064Sume * of an outgoing packet. If the outgoing interface of the packet 49778064Sume * is a loopback one, the kernel would consider the packet to be 49878064Sume * accepted, even if we have no such address assinged on the interface. 49978064Sume * We check the cloned flag of the route entry to reject such cases, 50078064Sume * assuming that route entries for our own addresses are not made by 50178064Sume * cloning (it should be true because in6_addloop explicitly installs 50278064Sume * the host route). However, we might have to do an explicit check 50378064Sume * while it would be less efficient. Or, should we rather install a 50478064Sume * reject route for such a case? 50553541Sshin */ 50653541Sshin if (ip6_forward_rt.ro_rt && 50753541Sshin (ip6_forward_rt.ro_rt->rt_flags & 50853541Sshin (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 50978064Sume#ifdef RTF_WASCLONED 51078064Sume !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 51178064Sume#endif 51278064Sume#ifdef RTF_CLONED 51378064Sume !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 51478064Sume#endif 51562587Sitojun#if 0 51653541Sshin /* 51762587Sitojun * The check below is redundant since the comparison of 51862587Sitojun * the destination and the key of the rtentry has 51962587Sitojun * already done through looking up the routing table. 52053541Sshin */ 52162587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 522120913Sume &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 52362587Sitojun#endif 52453541Sshin ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 52553541Sshin struct in6_ifaddr *ia6 = 52653541Sshin (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 52778064Sume 52862587Sitojun /* 529121630Sume * record address information into m_tag. 53078064Sume */ 53178064Sume (void)ip6_setdstifaddr(m, ia6); 53278064Sume 53378064Sume /* 53462587Sitojun * packets to a tentative, duplicated, or somehow invalid 53562587Sitojun * address must not be accepted. 53662587Sitojun */ 53753541Sshin if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 53862587Sitojun /* this address is ready */ 53953541Sshin ours = 1; 54053541Sshin deliverifp = ia6->ia_ifp; /* correct? */ 54167334Sjoe /* Count the packet in the ip address stats */ 54267334Sjoe ia6->ia_ifa.if_ipackets++; 54367334Sjoe ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 54453541Sshin goto hbhcheck; 54553541Sshin } else { 546165118Sbz char ip6bufs[INET6_ADDRSTRLEN]; 547165118Sbz char ip6bufd[INET6_ADDRSTRLEN]; 54862587Sitojun /* address is not ready, so discard the packet. */ 54978064Sume nd6log((LOG_INFO, 55078064Sume "ip6_input: packet to an unready address %s->%s\n", 551165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 552165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst))); 55362587Sitojun 55462587Sitojun goto bad; 55553541Sshin } 55653541Sshin } 55753541Sshin 55853541Sshin /* 559120913Sume * FAITH (Firewall Aided Internet Translator) 56053541Sshin */ 56153541Sshin if (ip6_keepfaith) { 56253541Sshin if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 56353541Sshin && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 56453541Sshin /* XXX do we need more sanity checks? */ 56553541Sshin ours = 1; 56695023Ssuz deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ 56753541Sshin goto hbhcheck; 56853541Sshin } 56953541Sshin } 57053541Sshin 57153541Sshin /* 57253541Sshin * Now there is no reason to process the packet if it's not our own 57353541Sshin * and we're not a router. 57453541Sshin */ 57553541Sshin if (!ip6_forwarding) { 57653541Sshin ip6stat.ip6s_cantforward++; 57753541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 57853541Sshin goto bad; 57953541Sshin } 58053541Sshin 58153541Sshin hbhcheck: 58253541Sshin /* 583121630Sume * record address information into m_tag, if we don't have one yet. 58478064Sume * note that we are unable to record it, if the address is not listed 58578064Sume * as our interface address (e.g. multicast addresses, addresses 58678064Sume * within FAITH prefixes and such). 58778064Sume */ 58878064Sume if (deliverifp && !ip6_getdstifaddr(m)) { 58978064Sume struct in6_ifaddr *ia6; 59078064Sume 59178064Sume ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 59278064Sume if (ia6) { 59378064Sume if (!ip6_setdstifaddr(m, ia6)) { 59478064Sume /* 59578064Sume * XXX maybe we should drop the packet here, 59678064Sume * as we could not provide enough information 59778064Sume * to the upper layers. 59878064Sume */ 59978064Sume } 60078064Sume } 60178064Sume } 60278064Sume 60378064Sume /* 60453541Sshin * Process Hop-by-Hop options header if it's contained. 60553541Sshin * m may be modified in ip6_hopopts_input(). 60653541Sshin * If a JumboPayload option is included, plen will also be modified. 60753541Sshin */ 60853541Sshin plen = (u_int32_t)ntohs(ip6->ip6_plen); 60953541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 61062587Sitojun struct ip6_hbh *hbh; 61162587Sitojun 61253541Sshin if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 61362587Sitojun#if 0 /*touches NULL pointer*/ 61453541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 61562587Sitojun#endif 61653541Sshin return; /* m have already been freed */ 61753541Sshin } 61862587Sitojun 61953541Sshin /* adjust pointer */ 62053541Sshin ip6 = mtod(m, struct ip6_hdr *); 62153541Sshin 62253541Sshin /* 62395023Ssuz * if the payload length field is 0 and the next header field 62462587Sitojun * indicates Hop-by-Hop Options header, then a Jumbo Payload 62562587Sitojun * option MUST be included. 62662587Sitojun */ 62762587Sitojun if (ip6->ip6_plen == 0 && plen == 0) { 62862587Sitojun /* 62962587Sitojun * Note that if a valid jumbo payload option is 630120913Sume * contained, ip6_hopopts_input() must set a valid 631120913Sume * (non-zero) payload length to the variable plen. 63262587Sitojun */ 63362587Sitojun ip6stat.ip6s_badoptions++; 63462587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 63562587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 63662587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 63762587Sitojun ICMP6_PARAMPROB_HEADER, 63862587Sitojun (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 63962587Sitojun return; 64062587Sitojun } 64162587Sitojun#ifndef PULLDOWN_TEST 64262587Sitojun /* ip6_hopopts_input() ensures that mbuf is contiguous */ 64362587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 64462587Sitojun#else 64562587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 64662587Sitojun sizeof(struct ip6_hbh)); 64762587Sitojun if (hbh == NULL) { 64862587Sitojun ip6stat.ip6s_tooshort++; 64962587Sitojun return; 65062587Sitojun } 65162587Sitojun#endif 65262587Sitojun nxt = hbh->ip6h_nxt; 65362587Sitojun 65462587Sitojun /* 655169557Sjinmei * If we are acting as a router and the packet contains a 656169557Sjinmei * router alert option, see if we know the option value. 657169557Sjinmei * Currently, we only support the option value for MLD, in which 658169557Sjinmei * case we should pass the packet to the multicast routing 659169557Sjinmei * daemon. 66053541Sshin */ 661169557Sjinmei if (rtalert != ~0 && ip6_forwarding) { 662169557Sjinmei switch (rtalert) { 663169557Sjinmei case IP6OPT_RTALERT_MLD: 664169557Sjinmei ours = 1; 665169557Sjinmei break; 666169557Sjinmei default: 667169557Sjinmei /* 668169557Sjinmei * RFC2711 requires unrecognized values must be 669169557Sjinmei * silently ignored. 670169557Sjinmei */ 671169557Sjinmei break; 672169557Sjinmei } 673169557Sjinmei } 67453541Sshin } else 67553541Sshin nxt = ip6->ip6_nxt; 67653541Sshin 67753541Sshin /* 67853541Sshin * Check that the amount of data in the buffers 67953541Sshin * is as at least much as the IPv6 header would have us expect. 68053541Sshin * Trim mbufs if longer than we expect. 68153541Sshin * Drop packet if shorter than we expect. 68253541Sshin */ 68353541Sshin if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 68453541Sshin ip6stat.ip6s_tooshort++; 68553541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 68653541Sshin goto bad; 68753541Sshin } 68853541Sshin if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 68953541Sshin if (m->m_len == m->m_pkthdr.len) { 69053541Sshin m->m_len = sizeof(struct ip6_hdr) + plen; 69153541Sshin m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 69253541Sshin } else 69353541Sshin m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 69453541Sshin } 69553541Sshin 69653541Sshin /* 69753541Sshin * Forward if desirable. 69853541Sshin */ 69953541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 70056723Sshin /* 70156723Sshin * If we are acting as a multicast router, all 70256723Sshin * incoming multicast packets are passed to the 70356723Sshin * kernel-level multicast forwarding function. 70456723Sshin * The packet is returned (relatively) intact; if 70556723Sshin * ip6_mforward() returns a non-zero value, the packet 70656723Sshin * must be discarded, else it may be accepted below. 70756723Sshin */ 708166938Sbms if (ip6_mrouter && ip6_mforward && 709166938Sbms ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 71056723Sshin ip6stat.ip6s_cantforward++; 71156723Sshin m_freem(m); 71256723Sshin return; 71356723Sshin } 71453541Sshin if (!ours) { 71553541Sshin m_freem(m); 71653541Sshin return; 71753541Sshin } 71853541Sshin } else if (!ours) { 719121143Ssam ip6_forward(m, srcrt); 72053541Sshin return; 721121673Sume } 72253541Sshin 72362587Sitojun ip6 = mtod(m, struct ip6_hdr *); 72462587Sitojun 72553541Sshin /* 72662587Sitojun * Malicious party may be able to use IPv4 mapped addr to confuse 72762587Sitojun * tcp/udp stack and bypass security checks (act as if it was from 72862587Sitojun * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 72962587Sitojun * 73062587Sitojun * For SIIT end node behavior, you may want to disable the check. 73162587Sitojun * However, you will become vulnerable to attacks using IPv4 mapped 73262587Sitojun * source. 73362587Sitojun */ 73462587Sitojun if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 73562587Sitojun IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 73662587Sitojun ip6stat.ip6s_badscope++; 73762587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 73862587Sitojun goto bad; 73962587Sitojun } 74062587Sitojun 74162587Sitojun /* 74253541Sshin * Tell launch routine the next header 74353541Sshin */ 74453541Sshin ip6stat.ip6s_delivered++; 74553541Sshin in6_ifstat_inc(deliverifp, ifs6_in_deliver); 74653541Sshin nest = 0; 74778064Sume 74853541Sshin while (nxt != IPPROTO_DONE) { 74953541Sshin if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 75053541Sshin ip6stat.ip6s_toomanyhdr++; 75153541Sshin goto bad; 75253541Sshin } 75353541Sshin 75453541Sshin /* 75553541Sshin * protection against faulty packet - there should be 75653541Sshin * more sanity checks in header chain processing. 75753541Sshin */ 75853541Sshin if (m->m_pkthdr.len < off) { 75953541Sshin ip6stat.ip6s_tooshort++; 76053541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 76153541Sshin goto bad; 76253541Sshin } 76353541Sshin 764171167Sgnn#ifdef IPSEC 76578064Sume /* 76678064Sume * enforce IPsec policy checking if we are seeing last header. 76778064Sume * note that we do not visit this with protocols with pcb layer 76878064Sume * code - like udp/tcp/raw ip. 76978064Sume */ 770171133Sgnn if (ip6_ipsec_input(m, nxt)) 77178064Sume goto bad; 772171167Sgnn#endif /* IPSEC */ 77353541Sshin nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 77453541Sshin } 77553541Sshin return; 77653541Sshin bad: 77753541Sshin m_freem(m); 77853541Sshin} 77953541Sshin 78053541Sshin/* 78178064Sume * set/grab in6_ifaddr correspond to IPv6 destination address. 78278064Sume * XXX backward compatibility wrapper 78378064Sume */ 784121673Sumestatic struct ip6aux * 78578064Sumeip6_setdstifaddr(m, ia6) 78678064Sume struct mbuf *m; 78778064Sume struct in6_ifaddr *ia6; 78878064Sume{ 789121673Sume struct ip6aux *ip6a; 79078064Sume 791121673Sume ip6a = ip6_addaux(m); 792121673Sume if (ip6a) 793121673Sume ip6a->ip6a_dstia6 = ia6; 794121673Sume return ip6a; /* NULL if failed to set */ 79578064Sume} 79678064Sume 79778064Sumestruct in6_ifaddr * 79878064Sumeip6_getdstifaddr(m) 79978064Sume struct mbuf *m; 80078064Sume{ 801121673Sume struct ip6aux *ip6a; 80278064Sume 803121673Sume ip6a = ip6_findaux(m); 804121673Sume if (ip6a) 805121673Sume return ip6a->ip6a_dstia6; 80678064Sume else 80778064Sume return NULL; 80878064Sume} 80978064Sume 81078064Sume/* 81153541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is 81253541Sshin * included, the real payload length will be stored in plenp. 81353541Sshin */ 81453541Sshinstatic int 81553541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp) 81653541Sshin u_int32_t *plenp; 81753541Sshin u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 81853541Sshin struct mbuf **mp; 81953541Sshin int *offp; 82053541Sshin{ 82178064Sume struct mbuf *m = *mp; 82253541Sshin int off = *offp, hbhlen; 82353541Sshin struct ip6_hbh *hbh; 82453541Sshin u_int8_t *opt; 82553541Sshin 82653541Sshin /* validation of the length of the header */ 82762587Sitojun#ifndef PULLDOWN_TEST 82853541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 82953541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 83053541Sshin hbhlen = (hbh->ip6h_len + 1) << 3; 83153541Sshin 83253541Sshin IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 83353541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 83462587Sitojun#else 83562587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 83662587Sitojun sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 83762587Sitojun if (hbh == NULL) { 83862587Sitojun ip6stat.ip6s_tooshort++; 83962587Sitojun return -1; 84062587Sitojun } 84162587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 84262587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 84362587Sitojun hbhlen); 84462587Sitojun if (hbh == NULL) { 84562587Sitojun ip6stat.ip6s_tooshort++; 84662587Sitojun return -1; 84762587Sitojun } 84862587Sitojun#endif 84953541Sshin off += hbhlen; 85053541Sshin hbhlen -= sizeof(struct ip6_hbh); 85153541Sshin opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 85253541Sshin 85353541Sshin if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 85453541Sshin hbhlen, rtalertp, plenp) < 0) 855120856Sume return (-1); 85653541Sshin 85753541Sshin *offp = off; 85853541Sshin *mp = m; 859120856Sume return (0); 86053541Sshin} 86153541Sshin 86253541Sshin/* 86353541Sshin * Search header for all Hop-by-hop options and process each option. 86453541Sshin * This function is separate from ip6_hopopts_input() in order to 86553541Sshin * handle a case where the sending node itself process its hop-by-hop 86653541Sshin * options header. In such a case, the function is called from ip6_output(). 86778064Sume * 86878064Sume * The function assumes that hbh header is located right after the IPv6 header 86978064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 87078064Sume * opthead + hbhlen is located in continuous memory region. 87153541Sshin */ 87253541Sshinint 87353541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 87453541Sshin struct mbuf *m; 87553541Sshin u_int8_t *opthead; 87653541Sshin int hbhlen; 87753541Sshin u_int32_t *rtalertp; 87853541Sshin u_int32_t *plenp; 87953541Sshin{ 88053541Sshin struct ip6_hdr *ip6; 88153541Sshin int optlen = 0; 88253541Sshin u_int8_t *opt = opthead; 88353541Sshin u_int16_t rtalert_val; 88462587Sitojun u_int32_t jumboplen; 88578064Sume const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 88653541Sshin 88753541Sshin for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 88878064Sume switch (*opt) { 88978064Sume case IP6OPT_PAD1: 89078064Sume optlen = 1; 89178064Sume break; 89278064Sume case IP6OPT_PADN: 89378064Sume if (hbhlen < IP6OPT_MINLEN) { 89478064Sume ip6stat.ip6s_toosmall++; 89578064Sume goto bad; 89678064Sume } 89778064Sume optlen = *(opt + 1) + 2; 89878064Sume break; 899121472Sume case IP6OPT_ROUTER_ALERT: 90062587Sitojun /* XXX may need check for alignment */ 90178064Sume if (hbhlen < IP6OPT_RTALERT_LEN) { 90278064Sume ip6stat.ip6s_toosmall++; 90378064Sume goto bad; 90478064Sume } 90578064Sume if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 90678064Sume /* XXX stat */ 90778064Sume icmp6_error(m, ICMP6_PARAM_PROB, 908120913Sume ICMP6_PARAMPROB_HEADER, 909120913Sume erroff + opt + 1 - opthead); 910120856Sume return (-1); 91178064Sume } 91278064Sume optlen = IP6OPT_RTALERT_LEN; 91378064Sume bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 91478064Sume *rtalertp = ntohs(rtalert_val); 91578064Sume break; 91678064Sume case IP6OPT_JUMBO: 91778064Sume /* XXX may need check for alignment */ 91862587Sitojun if (hbhlen < IP6OPT_JUMBO_LEN) { 91962587Sitojun ip6stat.ip6s_toosmall++; 92062587Sitojun goto bad; 92162587Sitojun } 92278064Sume if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 92378064Sume /* XXX stat */ 92478064Sume icmp6_error(m, ICMP6_PARAM_PROB, 925120913Sume ICMP6_PARAMPROB_HEADER, 926120913Sume erroff + opt + 1 - opthead); 927120856Sume return (-1); 92878064Sume } 92962587Sitojun optlen = IP6OPT_JUMBO_LEN; 93053541Sshin 93162587Sitojun /* 93262587Sitojun * IPv6 packets that have non 0 payload length 93378064Sume * must not contain a jumbo payload option. 93462587Sitojun */ 93562587Sitojun ip6 = mtod(m, struct ip6_hdr *); 93662587Sitojun if (ip6->ip6_plen) { 93762587Sitojun ip6stat.ip6s_badoptions++; 93862587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 939120913Sume ICMP6_PARAMPROB_HEADER, 940120913Sume erroff + opt - opthead); 941120856Sume return (-1); 94262587Sitojun } 94353541Sshin 94462587Sitojun /* 94562587Sitojun * We may see jumbolen in unaligned location, so 94662587Sitojun * we'd need to perform bcopy(). 94762587Sitojun */ 94862587Sitojun bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 94962587Sitojun jumboplen = (u_int32_t)htonl(jumboplen); 95062587Sitojun 95162587Sitojun#if 1 95262587Sitojun /* 95362587Sitojun * if there are multiple jumbo payload options, 95462587Sitojun * *plenp will be non-zero and the packet will be 95562587Sitojun * rejected. 95662587Sitojun * the behavior may need some debate in ipngwg - 95762587Sitojun * multiple options does not make sense, however, 95862587Sitojun * there's no explicit mention in specification. 95962587Sitojun */ 96062587Sitojun if (*plenp != 0) { 96162587Sitojun ip6stat.ip6s_badoptions++; 96262587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 963120913Sume ICMP6_PARAMPROB_HEADER, 964120913Sume erroff + opt + 2 - opthead); 965120856Sume return (-1); 96662587Sitojun } 96762587Sitojun#endif 96862587Sitojun 96962587Sitojun /* 97062587Sitojun * jumbo payload length must be larger than 65535. 97162587Sitojun */ 97262587Sitojun if (jumboplen <= IPV6_MAXPACKET) { 97362587Sitojun ip6stat.ip6s_badoptions++; 97462587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 975120913Sume ICMP6_PARAMPROB_HEADER, 976120913Sume erroff + opt + 2 - opthead); 977120856Sume return (-1); 97862587Sitojun } 97962587Sitojun *plenp = jumboplen; 98062587Sitojun 98162587Sitojun break; 98278064Sume default: /* unknown option */ 98378064Sume if (hbhlen < IP6OPT_MINLEN) { 98478064Sume ip6stat.ip6s_toosmall++; 98578064Sume goto bad; 98678064Sume } 98778064Sume optlen = ip6_unknown_opt(opt, m, 98878064Sume erroff + opt - opthead); 98978064Sume if (optlen == -1) 990120856Sume return (-1); 99178064Sume optlen += 2; 99278064Sume break; 99353541Sshin } 99453541Sshin } 99553541Sshin 996120856Sume return (0); 99753541Sshin 99853541Sshin bad: 99953541Sshin m_freem(m); 1000120856Sume return (-1); 100153541Sshin} 100253541Sshin 100353541Sshin/* 100453541Sshin * Unknown option processing. 100553541Sshin * The third argument `off' is the offset from the IPv6 header to the option, 100653541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header 100753541Sshin * is not continuous in order to return an ICMPv6 error. 100853541Sshin */ 100953541Sshinint 101053541Sshinip6_unknown_opt(optp, m, off) 101153541Sshin u_int8_t *optp; 101253541Sshin struct mbuf *m; 101353541Sshin int off; 101453541Sshin{ 101553541Sshin struct ip6_hdr *ip6; 101653541Sshin 101778064Sume switch (IP6OPT_TYPE(*optp)) { 101878064Sume case IP6OPT_TYPE_SKIP: /* ignore the option */ 1019120856Sume return ((int)*(optp + 1)); 102078064Sume case IP6OPT_TYPE_DISCARD: /* silently discard */ 102178064Sume m_freem(m); 1022120856Sume return (-1); 102378064Sume case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 102478064Sume ip6stat.ip6s_badoptions++; 102578064Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1026120856Sume return (-1); 102778064Sume case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 102878064Sume ip6stat.ip6s_badoptions++; 102978064Sume ip6 = mtod(m, struct ip6_hdr *); 103078064Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 103178064Sume (m->m_flags & (M_BCAST|M_MCAST))) 103278064Sume m_freem(m); 103378064Sume else 103478064Sume icmp6_error(m, ICMP6_PARAM_PROB, 103578064Sume ICMP6_PARAMPROB_OPTION, off); 1036120856Sume return (-1); 103753541Sshin } 103853541Sshin 103953541Sshin m_freem(m); /* XXX: NOTREACHED */ 1040120856Sume return (-1); 104153541Sshin} 104253541Sshin 104353541Sshin/* 104462587Sitojun * Create the "control" list for this pcb. 104578064Sume * The function will not modify mbuf chain at all. 104662587Sitojun * 104778064Sume * with KAME mbuf chain restriction: 104862587Sitojun * The routine will be called from upper layer handlers like tcp6_input(). 104962587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already 105062587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 105162587Sitojun * very first mbuf on the mbuf chain. 105253541Sshin */ 105353541Sshinvoid 1054121674Sumeip6_savecontrol(in6p, m, mp) 105578064Sume struct inpcb *in6p; 1056121674Sume struct mbuf *m, **mp; 105753541Sshin{ 1058121472Sume#define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y)) 1059121674Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 106053541Sshin 106178064Sume#ifdef SO_TIMESTAMP 106278064Sume if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 106353541Sshin struct timeval tv; 106453541Sshin 106553541Sshin microtime(&tv); 106653541Sshin *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1067120913Sume SCM_TIMESTAMP, SOL_SOCKET); 1068121472Sume if (*mp) 106953541Sshin mp = &(*mp)->m_next; 107097658Stanimura } 107162587Sitojun#endif 107262587Sitojun 1073121631Sume if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) 1074121631Sume return; 1075121631Sume 107653541Sshin /* RFC 2292 sec. 5 */ 107778064Sume if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 107853541Sshin struct in6_pktinfo pi6; 1079120913Sume 108053541Sshin bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1081121315Sume in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1082120913Sume pi6.ipi6_ifindex = 1083120913Sume (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1084120913Sume 108553541Sshin *mp = sbcreatecontrol((caddr_t) &pi6, 1086120913Sume sizeof(struct in6_pktinfo), 1087121472Sume IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1088121472Sume if (*mp) 108953541Sshin mp = &(*mp)->m_next; 109053541Sshin } 109178064Sume 109278064Sume if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 109353541Sshin int hlim = ip6->ip6_hlim & 0xff; 1094120913Sume 1095120913Sume *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1096121472Sume IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6); 1097121472Sume if (*mp) 109853541Sshin mp = &(*mp)->m_next; 109953541Sshin } 110053541Sshin 1101121472Sume if ((in6p->in6p_flags & IN6P_TCLASS) != 0) { 1102121472Sume u_int32_t flowinfo; 1103121472Sume int tclass; 1104121472Sume 1105121472Sume flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1106121472Sume flowinfo >>= 20; 1107121472Sume 1108121472Sume tclass = flowinfo & 0xff; 1109121472Sume *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass), 1110121472Sume IPV6_TCLASS, IPPROTO_IPV6); 1111121472Sume if (*mp) 1112121472Sume mp = &(*mp)->m_next; 1113121472Sume } 1114121472Sume 111553541Sshin /* 1116130002Sume * IPV6_HOPOPTS socket option. Recall that we required super-user 1117130002Sume * privilege for the option (see ip6_ctloutput), but it might be too 1118130002Sume * strict, since there might be some hop-by-hop options which can be 1119130002Sume * returned to normal user. 1120130002Sume * See also RFC 2292 section 6 (or RFC 3542 section 8). 112153541Sshin */ 1122121472Sume if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) { 112353541Sshin /* 112453541Sshin * Check if a hop-by-hop options header is contatined in the 112553541Sshin * received packet, and if so, store the options as ancillary 112653541Sshin * data. Note that a hop-by-hop options header must be 1127120913Sume * just after the IPv6 header, which is assured through the 1128120913Sume * IPv6 input processing. 112953541Sshin */ 113053541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 113162587Sitojun struct ip6_hbh *hbh; 113278064Sume int hbhlen = 0; 113378064Sume#ifdef PULLDOWN_TEST 113478064Sume struct mbuf *ext; 113578064Sume#endif 113653541Sshin 113762587Sitojun#ifndef PULLDOWN_TEST 113862587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 113962587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 114062587Sitojun#else 114178064Sume ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 114278064Sume ip6->ip6_nxt); 114378064Sume if (ext == NULL) { 114462587Sitojun ip6stat.ip6s_tooshort++; 114562587Sitojun return; 114662587Sitojun } 114778064Sume hbh = mtod(ext, struct ip6_hbh *); 114862587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 114978064Sume if (hbhlen != ext->m_len) { 115078064Sume m_freem(ext); 115162587Sitojun ip6stat.ip6s_tooshort++; 115262587Sitojun return; 115362587Sitojun } 115462587Sitojun#endif 115562587Sitojun 115653541Sshin /* 1157120913Sume * XXX: We copy the whole header even if a 1158120913Sume * jumbo payload option is included, the option which 1159120913Sume * is to be removed before returning according to 1160120913Sume * RFC2292. 1161148169Sume * Note: this constraint is removed in RFC3542 116253541Sshin */ 116362587Sitojun *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1164121472Sume IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1165121472Sume IPPROTO_IPV6); 1166121472Sume if (*mp) 116753541Sshin mp = &(*mp)->m_next; 116878064Sume#ifdef PULLDOWN_TEST 116978064Sume m_freem(ext); 117078064Sume#endif 117153541Sshin } 117253541Sshin } 117353541Sshin 1174121472Sume if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 117578064Sume int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 117653541Sshin 117753541Sshin /* 117853541Sshin * Search for destination options headers or routing 117953541Sshin * header(s) through the header chain, and stores each 118053541Sshin * header as ancillary data. 118153541Sshin * Note that the order of the headers remains in 118253541Sshin * the chain of ancillary data. 118353541Sshin */ 118478064Sume while (1) { /* is explicit loop prevention necessary? */ 118578064Sume struct ip6_ext *ip6e = NULL; 118662587Sitojun int elen; 118778064Sume#ifdef PULLDOWN_TEST 118878064Sume struct mbuf *ext = NULL; 118978064Sume#endif 119053541Sshin 119178064Sume /* 119278064Sume * if it is not an extension header, don't try to 119378064Sume * pull it from the chain. 119478064Sume */ 119578064Sume switch (nxt) { 119678064Sume case IPPROTO_DSTOPTS: 119778064Sume case IPPROTO_ROUTING: 119878064Sume case IPPROTO_HOPOPTS: 119978064Sume case IPPROTO_AH: /* is it possible? */ 120078064Sume break; 120178064Sume default: 120278064Sume goto loopend; 120378064Sume } 120478064Sume 120562587Sitojun#ifndef PULLDOWN_TEST 120678064Sume if (off + sizeof(*ip6e) > m->m_len) 120778064Sume goto loopend; 120862587Sitojun ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 120962587Sitojun if (nxt == IPPROTO_AH) 121062587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 121162587Sitojun else 121262587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 121378064Sume if (off + elen > m->m_len) 121478064Sume goto loopend; 121562587Sitojun#else 121678064Sume ext = ip6_pullexthdr(m, off, nxt); 121778064Sume if (ext == NULL) { 121862587Sitojun ip6stat.ip6s_tooshort++; 121962587Sitojun return; 122062587Sitojun } 122178064Sume ip6e = mtod(ext, struct ip6_ext *); 122262587Sitojun if (nxt == IPPROTO_AH) 122362587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 122462587Sitojun else 122562587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 122678064Sume if (elen != ext->m_len) { 122778064Sume m_freem(ext); 122862587Sitojun ip6stat.ip6s_tooshort++; 122962587Sitojun return; 123062587Sitojun } 123162587Sitojun#endif 123262587Sitojun 123378064Sume switch (nxt) { 123478064Sume case IPPROTO_DSTOPTS: 1235120913Sume if (!(in6p->in6p_flags & IN6P_DSTOPTS)) 123678064Sume break; 123753541Sshin 123878064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1239121472Sume IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1240120913Sume IPPROTO_IPV6); 124178064Sume if (*mp) 124278064Sume mp = &(*mp)->m_next; 124378064Sume break; 124478064Sume case IPPROTO_ROUTING: 124578064Sume if (!in6p->in6p_flags & IN6P_RTHDR) 124678064Sume break; 124753541Sshin 124878064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1249121472Sume IS2292(IPV6_2292RTHDR, IPV6_RTHDR), 1250120913Sume IPPROTO_IPV6); 125178064Sume if (*mp) 125278064Sume mp = &(*mp)->m_next; 125378064Sume break; 125478064Sume case IPPROTO_HOPOPTS: 125578064Sume case IPPROTO_AH: /* is it possible? */ 125678064Sume break; 125753541Sshin 125878064Sume default: 125978064Sume /* 126078064Sume * other cases have been filtered in the above. 126178064Sume * none will visit this case. here we supply 126278064Sume * the code just in case (nxt overwritten or 126378064Sume * other cases). 126478064Sume */ 126578064Sume#ifdef PULLDOWN_TEST 126678064Sume m_freem(ext); 126778064Sume#endif 126878064Sume goto loopend; 126953541Sshin 127053541Sshin } 127153541Sshin 127253541Sshin /* proceed with the next header. */ 127362587Sitojun off += elen; 127453541Sshin nxt = ip6e->ip6e_nxt; 127578064Sume ip6e = NULL; 127678064Sume#ifdef PULLDOWN_TEST 127778064Sume m_freem(ext); 127878064Sume ext = NULL; 127978064Sume#endif 128053541Sshin } 128153541Sshin loopend: 128278064Sume ; 128353541Sshin } 128478064Sume 1285121472Sume#undef IS2292 128678064Sume} 128778064Sume 1288125776Sumevoid 1289125776Sumeip6_notify_pmtu(in6p, dst, mtu) 1290125776Sume struct inpcb *in6p; 1291125776Sume struct sockaddr_in6 *dst; 1292125776Sume u_int32_t *mtu; 1293125776Sume{ 1294125776Sume struct socket *so; 1295125776Sume struct mbuf *m_mtu; 1296125776Sume struct ip6_mtuinfo mtuctl; 1297125776Sume 1298125776Sume so = in6p->inp_socket; 1299125776Sume 1300125776Sume if (mtu == NULL) 1301125776Sume return; 1302125776Sume 1303125776Sume#ifdef DIAGNOSTIC 1304125776Sume if (so == NULL) /* I believe this is impossible */ 1305125776Sume panic("ip6_notify_pmtu: socket is NULL"); 1306125776Sume#endif 1307125776Sume 1308125776Sume bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */ 1309125776Sume mtuctl.ip6m_mtu = *mtu; 1310125776Sume mtuctl.ip6m_addr = *dst; 1311148385Sume if (sa6_recoverscope(&mtuctl.ip6m_addr)) 1312148385Sume return; 1313125776Sume 1314125776Sume if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1315125776Sume IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1316125776Sume return; 1317125776Sume 1318125776Sume if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1319125776Sume == 0) { 1320125776Sume m_freem(m_mtu); 1321125776Sume /* XXX: should count statistics */ 1322125776Sume } else 1323125776Sume sorwakeup(so); 1324125776Sume 1325125776Sume return; 1326125776Sume} 1327125776Sume 132878064Sume#ifdef PULLDOWN_TEST 132978064Sume/* 133078064Sume * pull single extension header from mbuf chain. returns single mbuf that 133178064Sume * contains the result, or NULL on error. 133278064Sume */ 133378064Sumestatic struct mbuf * 133478064Sumeip6_pullexthdr(m, off, nxt) 133578064Sume struct mbuf *m; 133678064Sume size_t off; 133778064Sume int nxt; 133878064Sume{ 133978064Sume struct ip6_ext ip6e; 134078064Sume size_t elen; 134178064Sume struct mbuf *n; 134278064Sume 134378064Sume#ifdef DIAGNOSTIC 134478064Sume switch (nxt) { 134578064Sume case IPPROTO_DSTOPTS: 134678064Sume case IPPROTO_ROUTING: 134778064Sume case IPPROTO_HOPOPTS: 134878064Sume case IPPROTO_AH: /* is it possible? */ 134978064Sume break; 135078064Sume default: 135178064Sume printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 135253541Sshin } 135378064Sume#endif 135478064Sume 135578064Sume m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 135678064Sume if (nxt == IPPROTO_AH) 135778064Sume elen = (ip6e.ip6e_len + 2) << 2; 135878064Sume else 135978064Sume elen = (ip6e.ip6e_len + 1) << 3; 136078064Sume 1361111119Simp MGET(n, M_DONTWAIT, MT_DATA); 136278064Sume if (n && elen >= MLEN) { 1363111119Simp MCLGET(n, M_DONTWAIT); 136478064Sume if ((n->m_flags & M_EXT) == 0) { 136578064Sume m_free(n); 136678064Sume n = NULL; 136778064Sume } 136853541Sshin } 136978064Sume if (!n) 137078064Sume return NULL; 137162587Sitojun 137278064Sume n->m_len = 0; 137378064Sume if (elen >= M_TRAILINGSPACE(n)) { 137478064Sume m_free(n); 137578064Sume return NULL; 137678064Sume } 137778064Sume 137878064Sume m_copydata(m, off, elen, mtod(n, caddr_t)); 137978064Sume n->m_len = elen; 138078064Sume return n; 138153541Sshin} 138278064Sume#endif 138353541Sshin 138453541Sshin/* 138553541Sshin * Get pointer to the previous header followed by the header 138653541Sshin * currently processed. 138753541Sshin * XXX: This function supposes that 138853541Sshin * M includes all headers, 138953541Sshin * the next header field and the header length field of each header 139053541Sshin * are valid, and 139153541Sshin * the sum of each header length equals to OFF. 139253541Sshin * Because of these assumptions, this function must be called very 139353541Sshin * carefully. Moreover, it will not be used in the near future when 139453541Sshin * we develop `neater' mechanism to process extension headers. 139553541Sshin */ 139653541Sshinchar * 139753541Sshinip6_get_prevhdr(m, off) 139853541Sshin struct mbuf *m; 139953541Sshin int off; 140053541Sshin{ 140153541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 140253541Sshin 140353541Sshin if (off == sizeof(struct ip6_hdr)) 1404120856Sume return (&ip6->ip6_nxt); 140553541Sshin else { 140653541Sshin int len, nxt; 140753541Sshin struct ip6_ext *ip6e = NULL; 140853541Sshin 140953541Sshin nxt = ip6->ip6_nxt; 141053541Sshin len = sizeof(struct ip6_hdr); 141153541Sshin while (len < off) { 141253541Sshin ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 141353541Sshin 141478064Sume switch (nxt) { 141553541Sshin case IPPROTO_FRAGMENT: 141653541Sshin len += sizeof(struct ip6_frag); 141753541Sshin break; 141853541Sshin case IPPROTO_AH: 141953541Sshin len += (ip6e->ip6e_len + 2) << 2; 142053541Sshin break; 142153541Sshin default: 142253541Sshin len += (ip6e->ip6e_len + 1) << 3; 142353541Sshin break; 142453541Sshin } 142553541Sshin nxt = ip6e->ip6e_nxt; 142653541Sshin } 142753541Sshin if (ip6e) 1428120856Sume return (&ip6e->ip6e_nxt); 142953541Sshin else 143053541Sshin return NULL; 143153541Sshin } 143253541Sshin} 143353541Sshin 143453541Sshin/* 143562587Sitojun * get next header offset. m will be retained. 143662587Sitojun */ 143762587Sitojunint 143862587Sitojunip6_nexthdr(m, off, proto, nxtp) 143962587Sitojun struct mbuf *m; 144062587Sitojun int off; 144162587Sitojun int proto; 144262587Sitojun int *nxtp; 144362587Sitojun{ 144462587Sitojun struct ip6_hdr ip6; 144562587Sitojun struct ip6_ext ip6e; 144662587Sitojun struct ip6_frag fh; 144762587Sitojun 144862587Sitojun /* just in case */ 144962587Sitojun if (m == NULL) 145062587Sitojun panic("ip6_nexthdr: m == NULL"); 145162587Sitojun if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 145262587Sitojun return -1; 145362587Sitojun 145462587Sitojun switch (proto) { 145562587Sitojun case IPPROTO_IPV6: 145662587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6)) 145762587Sitojun return -1; 145862587Sitojun m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 145962587Sitojun if (nxtp) 146062587Sitojun *nxtp = ip6.ip6_nxt; 146162587Sitojun off += sizeof(ip6); 146262587Sitojun return off; 146362587Sitojun 146462587Sitojun case IPPROTO_FRAGMENT: 146562587Sitojun /* 146662587Sitojun * terminate parsing if it is not the first fragment, 146762587Sitojun * it does not make sense to parse through it. 146862587Sitojun */ 146962587Sitojun if (m->m_pkthdr.len < off + sizeof(fh)) 147062587Sitojun return -1; 147162587Sitojun m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1472120978Sume /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1473120978Sume if (fh.ip6f_offlg & IP6F_OFF_MASK) 147462587Sitojun return -1; 147562587Sitojun if (nxtp) 147662587Sitojun *nxtp = fh.ip6f_nxt; 147762587Sitojun off += sizeof(struct ip6_frag); 147862587Sitojun return off; 147962587Sitojun 148062587Sitojun case IPPROTO_AH: 148162587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6e)) 148262587Sitojun return -1; 148362587Sitojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 148462587Sitojun if (nxtp) 148562587Sitojun *nxtp = ip6e.ip6e_nxt; 148662587Sitojun off += (ip6e.ip6e_len + 2) << 2; 148762587Sitojun return off; 148862587Sitojun 148962587Sitojun case IPPROTO_HOPOPTS: 149062587Sitojun case IPPROTO_ROUTING: 149162587Sitojun case IPPROTO_DSTOPTS: 149262587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6e)) 149362587Sitojun return -1; 149462587Sitojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 149562587Sitojun if (nxtp) 149662587Sitojun *nxtp = ip6e.ip6e_nxt; 149762587Sitojun off += (ip6e.ip6e_len + 1) << 3; 149862587Sitojun return off; 149962587Sitojun 150062587Sitojun case IPPROTO_NONE: 150162587Sitojun case IPPROTO_ESP: 150262587Sitojun case IPPROTO_IPCOMP: 150362587Sitojun /* give up */ 150462587Sitojun return -1; 150562587Sitojun 150662587Sitojun default: 150762587Sitojun return -1; 150862587Sitojun } 150962587Sitojun 151062587Sitojun return -1; 151162587Sitojun} 151262587Sitojun 151362587Sitojun/* 151462587Sitojun * get offset for the last header in the chain. m will be kept untainted. 151562587Sitojun */ 151662587Sitojunint 151762587Sitojunip6_lasthdr(m, off, proto, nxtp) 151862587Sitojun struct mbuf *m; 151962587Sitojun int off; 152062587Sitojun int proto; 152162587Sitojun int *nxtp; 152262587Sitojun{ 152362587Sitojun int newoff; 152462587Sitojun int nxt; 152562587Sitojun 152662587Sitojun if (!nxtp) { 152762587Sitojun nxt = -1; 152862587Sitojun nxtp = &nxt; 152962587Sitojun } 153062587Sitojun while (1) { 153162587Sitojun newoff = ip6_nexthdr(m, off, proto, nxtp); 153262587Sitojun if (newoff < 0) 153362587Sitojun return off; 153462587Sitojun else if (newoff < off) 153562587Sitojun return -1; /* invalid */ 153662587Sitojun else if (newoff == off) 153762587Sitojun return newoff; 153862587Sitojun 153962587Sitojun off = newoff; 154062587Sitojun proto = *nxtp; 154162587Sitojun } 154262587Sitojun} 154362587Sitojun 1544121673Sumestruct ip6aux * 154578064Sumeip6_addaux(m) 154678064Sume struct mbuf *m; 154778064Sume{ 1548120913Sume struct m_tag *mtag; 1549120913Sume 1550120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1551120913Sume if (!mtag) { 1552120913Sume mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux), 1553120913Sume M_NOWAIT); 1554121630Sume if (mtag) { 1555120913Sume m_tag_prepend(m, mtag); 1556121630Sume bzero(mtag + 1, sizeof(struct ip6aux)); 1557121630Sume } 155878064Sume } 1559121673Sume return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 156078064Sume} 156178064Sume 1562121673Sumestruct ip6aux * 156378064Sumeip6_findaux(m) 156478064Sume struct mbuf *m; 156578064Sume{ 1566120913Sume struct m_tag *mtag; 1567120913Sume 1568120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1569121673Sume return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 157078064Sume} 157178064Sume 157278064Sumevoid 157378064Sumeip6_delaux(m) 157478064Sume struct mbuf *m; 157578064Sume{ 1576120913Sume struct m_tag *mtag; 1577120913Sume 1578120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1579120913Sume if (mtag) 1580120913Sume m_tag_delete(m, mtag); 158178064Sume} 158278064Sume 158362587Sitojun/* 158453541Sshin * System control for IP6 158553541Sshin */ 158653541Sshin 158753541Sshinu_char inet6ctlerrmap[PRC_NCMDS] = { 158853541Sshin 0, 0, 0, 0, 158953541Sshin 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 159053541Sshin EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 159153541Sshin EMSGSIZE, EHOSTUNREACH, 0, 0, 159253541Sshin 0, 0, 0, 0, 159353541Sshin ENOPROTOOPT 159453541Sshin}; 1595