ip6_input.c revision 78064
162587Sitojun/* $FreeBSD: head/sys/netinet6/ip6_input.c 78064 2001-06-11 12:39:29Z ume $ */ 278064Sume/* $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $ */ 362587Sitojun 453541Sshin/* 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 3353541Sshin/* 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 * 3. All advertising materials mentioning features or use of this software 4653541Sshin * must display the following acknowledgement: 4753541Sshin * This product includes software developed by the University of 4853541Sshin * California, Berkeley and its contributors. 4953541Sshin * 4. Neither the name of the University nor the names of its contributors 5053541Sshin * may be used to endorse or promote products derived from this software 5153541Sshin * without specific prior written permission. 5253541Sshin * 5353541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5453541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5553541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5653541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5753541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5853541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5953541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6053541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6153541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6253541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6353541Sshin * SUCH DAMAGE. 6453541Sshin * 6553541Sshin * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 6653541Sshin */ 6753541Sshin 6862587Sitojun#include "opt_ip6fw.h" 6962587Sitojun#include "opt_inet.h" 7062587Sitojun#include "opt_inet6.h" 7155009Sshin#include "opt_ipsec.h" 7264060Sdarrenr#include "opt_pfil_hooks.h" 7355009Sshin 7453541Sshin#include <sys/param.h> 7553541Sshin#include <sys/systm.h> 7678064Sume#include <sys/malloc.h> 7753541Sshin#include <sys/mbuf.h> 7853541Sshin#include <sys/domain.h> 7953541Sshin#include <sys/protosw.h> 8053541Sshin#include <sys/socket.h> 8153541Sshin#include <sys/socketvar.h> 8253541Sshin#include <sys/errno.h> 8353541Sshin#include <sys/time.h> 8453541Sshin#include <sys/kernel.h> 8553541Sshin#include <sys/syslog.h> 8653541Sshin 8753541Sshin#include <net/if.h> 8853541Sshin#include <net/if_types.h> 8953541Sshin#include <net/if_dl.h> 9053541Sshin#include <net/route.h> 9153541Sshin#include <net/netisr.h> 9256555Sbrian#include <net/intrq.h> 9364060Sdarrenr#ifdef PFIL_HOOKS 9464060Sdarrenr#include <net/pfil.h> 9564060Sdarrenr#endif 9653541Sshin 9753541Sshin#include <netinet/in.h> 9853541Sshin#include <netinet/in_systm.h> 9962587Sitojun#ifdef INET 10053541Sshin#include <netinet/ip.h> 10153541Sshin#include <netinet/ip_icmp.h> 10262587Sitojun#endif /*INET*/ 10362587Sitojun#include <netinet/ip6.h> 10453541Sshin#include <netinet6/in6_var.h> 10553541Sshin#include <netinet6/ip6_var.h> 10662587Sitojun#include <netinet/in_pcb.h> 10762587Sitojun#include <netinet/icmp6.h> 10853541Sshin#include <netinet6/in6_ifattach.h> 10953541Sshin#include <netinet6/nd6.h> 11053541Sshin#include <netinet6/in6_prefix.h> 11153541Sshin 11278064Sume#ifdef IPSEC 11378064Sume#include <netinet6/ipsec.h> 11478064Sume#ifdef INET6 11578064Sume#include <netinet6/ipsec6.h> 11678064Sume#endif 11778064Sume#endif 11878064Sume 11953541Sshin#include <netinet6/ip6_fw.h> 12053541Sshin 12153541Sshin#include <netinet6/ip6protosw.h> 12253541Sshin 12354263Sshin#include "faith.h" 12454263Sshin#include "gif.h" 12553541Sshin 12653541Sshin#include <net/net_osdep.h> 12753541Sshin 12862587Sitojunextern struct domain inet6domain; 12962587Sitojunextern struct ip6protosw inet6sw[]; 13053541Sshin 13162587Sitojunu_char ip6_protox[IPPROTO_MAX]; 13262587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN; 13362587Sitojunstruct in6_ifaddr *in6_ifaddr; 13453541Sshin 13578064Sumeextern struct callout in6_tmpaddrtimer_ch; 13678064Sume 13762587Sitojunint ip6_forward_srcrt; /* XXX */ 13862587Sitojunint ip6_sourcecheck; /* XXX */ 13962587Sitojunint ip6_sourcecheck_interval; /* XXX */ 14062587Sitojunconst int int6intrq_present = 1; 14153541Sshin 14278064Sumeint ip6_ours_check_algorithm; 14378064Sume 14478064Sume 14553541Sshin/* firewall hooks */ 14662587Sitojunip6_fw_chk_t *ip6_fw_chk_ptr; 14762587Sitojunip6_fw_ctl_t *ip6_fw_ctl_ptr; 14866303Sumeint ip6_fw_enable = 1; 14953541Sshin 15062587Sitojunstruct ip6stat ip6stat; 15153541Sshin 15262587Sitojunstatic void ip6_init2 __P((void *)); 15378064Sumestatic struct mbuf *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *)); 15453541Sshin 15562587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 15662587Sitojun#ifdef PULLDOWN_TEST 15762587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int)); 15853541Sshin#endif 15953541Sshin 16078064Sume 16153541Sshin/* 16253541Sshin * IP6 initialization: fill in IP6 protocol switch table. 16353541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler. 16453541Sshin */ 16553541Sshinvoid 16653541Sshinip6_init() 16753541Sshin{ 16878064Sume struct ip6protosw *pr; 16978064Sume int i; 17053541Sshin struct timeval tv; 17153541Sshin 17278064Sume#ifdef DIAGNOSTIC 17378064Sume if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 17478064Sume panic("sizeof(protosw) != sizeof(ip6protosw)"); 17578064Sume#endif 17653541Sshin pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 17753541Sshin if (pr == 0) 17853541Sshin panic("ip6_init"); 17953541Sshin for (i = 0; i < IPPROTO_MAX; i++) 18053541Sshin ip6_protox[i] = pr - inet6sw; 18153541Sshin for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 18253541Sshin pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 18353541Sshin if (pr->pr_domain->dom_family == PF_INET6 && 18453541Sshin pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 18553541Sshin ip6_protox[pr->pr_protocol] = pr - inet6sw; 18653541Sshin ip6intrq.ifq_maxlen = ip6qmaxlen; 18769152Sjlemon mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", MTX_DEF); 18857178Speter register_netisr(NETISR_IPV6, ip6intr); 18953541Sshin nd6_init(); 19053541Sshin frag6_init(); 19153541Sshin /* 19253541Sshin * in many cases, random() here does NOT return random number 19353541Sshin * as initialization during bootstrap time occur in fixed order. 19453541Sshin */ 19553541Sshin microtime(&tv); 19653541Sshin ip6_flow_seq = random() ^ tv.tv_usec; 19778064Sume microtime(&tv); 19878064Sume ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR; 19953541Sshin} 20053541Sshin 20153541Sshinstatic void 20253541Sshinip6_init2(dummy) 20353541Sshin void *dummy; 20453541Sshin{ 20553541Sshin 20653541Sshin /* 20753541Sshin * to route local address of p2p link to loopback, 20853541Sshin * assign loopback address first. 20953541Sshin */ 21062587Sitojun in6_ifattach(&loif[0], NULL); 21153541Sshin 21253541Sshin /* nd6_timer_init */ 21378064Sume callout_init(&nd6_timer_ch, 0); 21478064Sume callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 21578064Sume 21653541Sshin /* router renumbering prefix list maintenance */ 21778064Sume callout_init(&in6_rr_timer_ch, 0); 21878064Sume callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL); 21978064Sume 22078064Sume /* timer for regeneranation of temporary addresses randomize ID */ 22178064Sume callout_reset(&in6_tmpaddrtimer_ch, 22278064Sume (ip6_temp_preferred_lifetime - ip6_desync_factor - 22378064Sume ip6_temp_regen_advance) * hz, 22478064Sume in6_tmpaddrtimer, NULL); 22553541Sshin} 22653541Sshin 22753541Sshin/* cheat */ 22855009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */ 22955009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 23053541Sshin 23153541Sshin/* 23253541Sshin * IP6 input interrupt handling. Just pass the packet to ip6_input. 23353541Sshin */ 23453541Sshinvoid 23553541Sshinip6intr() 23653541Sshin{ 23753541Sshin int s; 23853541Sshin struct mbuf *m; 23953541Sshin 24053541Sshin for (;;) { 24153541Sshin s = splimp(); 24253541Sshin IF_DEQUEUE(&ip6intrq, m); 24353541Sshin splx(s); 24453541Sshin if (m == 0) 24553541Sshin return; 24653541Sshin ip6_input(m); 24753541Sshin } 24853541Sshin} 24953541Sshin 25053541Sshinextern struct route_in6 ip6_forward_rt; 25153541Sshin 25253541Sshinvoid 25353541Sshinip6_input(m) 25453541Sshin struct mbuf *m; 25553541Sshin{ 25653541Sshin struct ip6_hdr *ip6; 25753541Sshin int off = sizeof(struct ip6_hdr), nest; 25853541Sshin u_int32_t plen; 25953541Sshin u_int32_t rtalert = ~0; 26053541Sshin int nxt, ours = 0; 26153541Sshin struct ifnet *deliverifp = NULL; 26264060Sdarrenr#ifdef PFIL_HOOKS 26364060Sdarrenr struct packet_filter_hook *pfh; 26464060Sdarrenr struct mbuf *m0; 26564060Sdarrenr int rv; 26664060Sdarrenr#endif /* PFIL_HOOKS */ 26753541Sshin 26853541Sshin#ifdef IPSEC 26953541Sshin /* 27053541Sshin * should the inner packet be considered authentic? 27153541Sshin * see comment in ah4_input(). 27253541Sshin */ 27353541Sshin if (m) { 27453541Sshin m->m_flags &= ~M_AUTHIPHDR; 27553541Sshin m->m_flags &= ~M_AUTHIPDGM; 27653541Sshin } 27753541Sshin#endif 27853541Sshin 27953541Sshin /* 28078064Sume * make sure we don't have onion peering information into m_aux. 28178064Sume */ 28278064Sume ip6_delaux(m); 28378064Sume 28478064Sume /* 28553541Sshin * mbuf statistics by kazu 28653541Sshin */ 28753541Sshin if (m->m_flags & M_EXT) { 28853541Sshin if (m->m_next) 28953541Sshin ip6stat.ip6s_mext2m++; 29053541Sshin else 29153541Sshin ip6stat.ip6s_mext1++; 29253541Sshin } else { 29378064Sume#define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0])) 29453541Sshin if (m->m_next) { 29553541Sshin if (m->m_flags & M_LOOP) { 29653541Sshin ip6stat.ip6s_m2m[loif[0].if_index]++; /*XXX*/ 29778064Sume } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 29853541Sshin ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 29953541Sshin else 30053541Sshin ip6stat.ip6s_m2m[0]++; 30153541Sshin } else 30253541Sshin ip6stat.ip6s_m1++; 30378064Sume#undef M2MMAX 30453541Sshin } 30553541Sshin 30653541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 30753541Sshin ip6stat.ip6s_total++; 30853541Sshin 30962587Sitojun#ifndef PULLDOWN_TEST 31074336Skuriyama /* 31174336Skuriyama * L2 bridge code and some other code can return mbuf chain 31274336Skuriyama * that does not conform to KAME requirement. too bad. 31374336Skuriyama * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 31474336Skuriyama */ 31574336Skuriyama if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 31674336Skuriyama struct mbuf *n; 31774336Skuriyama 31874336Skuriyama MGETHDR(n, M_DONTWAIT, MT_HEADER); 31977003Sume if (n) 32077003Sume M_COPY_PKTHDR(n, m); 32174336Skuriyama if (n && m->m_pkthdr.len > MHLEN) { 32274336Skuriyama MCLGET(n, M_DONTWAIT); 32374336Skuriyama if ((n->m_flags & M_EXT) == 0) { 32474336Skuriyama m_freem(n); 32574336Skuriyama n = NULL; 32674336Skuriyama } 32774336Skuriyama } 32877003Sume if (!n) { 32977003Sume m_freem(m); 33074336Skuriyama return; /*ENOBUFS*/ 33177003Sume } 33274336Skuriyama 33374336Skuriyama m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); 33474336Skuriyama n->m_len = m->m_pkthdr.len; 33574336Skuriyama m_freem(m); 33674336Skuriyama m = n; 33774336Skuriyama } 33853541Sshin IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/); 33962587Sitojun#endif 34053541Sshin 34153541Sshin if (m->m_len < sizeof(struct ip6_hdr)) { 34253541Sshin struct ifnet *inifp; 34353541Sshin inifp = m->m_pkthdr.rcvif; 34453541Sshin if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) { 34553541Sshin ip6stat.ip6s_toosmall++; 34653541Sshin in6_ifstat_inc(inifp, ifs6_in_hdrerr); 34753541Sshin return; 34853541Sshin } 34953541Sshin } 35053541Sshin 35153541Sshin ip6 = mtod(m, struct ip6_hdr *); 35253541Sshin 35353541Sshin if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 35453541Sshin ip6stat.ip6s_badvers++; 35553541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 35653541Sshin goto bad; 35753541Sshin } 35853541Sshin 35964060Sdarrenr#ifdef PFIL_HOOKS 36064060Sdarrenr /* 36164060Sdarrenr * Run through list of hooks for input packets. If there are any 36264060Sdarrenr * filters which require that additional packets in the flow are 36364060Sdarrenr * not fast-forwarded, they must clear the M_CANFASTFWD flag. 36464060Sdarrenr * Note that filters must _never_ set this flag, as another filter 36564060Sdarrenr * in the list may have previously cleared it. 36664060Sdarrenr */ 36764060Sdarrenr m0 = m; 36864558Sume pfh = pfil_hook_get(PFIL_IN, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh); 36964060Sdarrenr for (; pfh; pfh = pfh->pfil_link.tqe_next) 37064060Sdarrenr if (pfh->pfil_func) { 37164060Sdarrenr rv = pfh->pfil_func(ip6, sizeof(*ip6), 37264060Sdarrenr m->m_pkthdr.rcvif, 0, &m0); 37364060Sdarrenr if (rv) 37464060Sdarrenr return; 37564060Sdarrenr m = m0; 37664060Sdarrenr if (m == NULL) 37764060Sdarrenr return; 37864060Sdarrenr ip6 = mtod(m, struct ip6_hdr *); 37964060Sdarrenr } 38064060Sdarrenr#endif /* PFIL_HOOKS */ 38164060Sdarrenr 38253541Sshin ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 38353541Sshin 38453541Sshin /* 38553541Sshin * Check with the firewall... 38653541Sshin */ 38766303Sume if (ip6_fw_enable && ip6_fw_chk_ptr) { 38853541Sshin u_short port = 0; 38953541Sshin /* If ipfw says divert, we have to just drop packet */ 39053541Sshin /* use port as a dummy argument */ 39153541Sshin if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 39253541Sshin m_freem(m); 39353541Sshin m = NULL; 39453541Sshin } 39553541Sshin if (!m) 39653541Sshin return; 39753541Sshin } 39853541Sshin 39953541Sshin /* 40078064Sume * Check against address spoofing/corruption. 40153541Sshin */ 40253541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 40353541Sshin IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 40478064Sume /* 40578064Sume * XXX: "badscope" is not very suitable for a multicast source. 40678064Sume */ 40753541Sshin ip6stat.ip6s_badscope++; 40853541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 40953541Sshin goto bad; 41053541Sshin } 41178064Sume if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 41278064Sume IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && 41378064Sume (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 41478064Sume ip6stat.ip6s_badscope++; 41578064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 41678064Sume goto bad; 41778064Sume } 41862587Sitojun /* 41978064Sume * The following check is not documented in specs. A malicious 42078064Sume * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 42178064Sume * and bypass security checks (act as if it was from 127.0.0.1 by using 42278064Sume * IPv6 src ::ffff:127.0.0.1). Be cautious. 42378064Sume * 42478064Sume * This check chokes if we are in an SIIT cloud. As none of BSDs 42578064Sume * support IPv4-less kernel compilation, we cannot support SIIT 42678064Sume * environment at all. So, it makes more sense for us to reject any 42778064Sume * malicious packets for non-SIIT environment, than try to do a 42878064Sume * partical support for SIIT environment. 42962587Sitojun */ 43078064Sume if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 43178064Sume IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 43278064Sume ip6stat.ip6s_badscope++; 43378064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 43478064Sume goto bad; 43578064Sume } 43662587Sitojun#if 0 43762587Sitojun /* 43862587Sitojun * Reject packets with IPv4 compatible addresses (auto tunnel). 43962587Sitojun * 44062587Sitojun * The code forbids auto tunnel relay case in RFC1933 (the check is 44162587Sitojun * stronger than RFC1933). We may want to re-enable it if mech-xx 44262587Sitojun * is revised to forbid relaying case. 44362587Sitojun */ 44462587Sitojun if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 44562587Sitojun IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 44662587Sitojun ip6stat.ip6s_badscope++; 44762587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 44862587Sitojun goto bad; 44962587Sitojun } 45062587Sitojun#endif 45175246Sume 45278064Sume /* drop packets if interface ID portion is already filled */ 45378064Sume if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 45478064Sume if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 45578064Sume ip6->ip6_src.s6_addr16[1]) { 45653541Sshin ip6stat.ip6s_badscope++; 45753541Sshin goto bad; 45853541Sshin } 45978064Sume if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) && 46078064Sume ip6->ip6_dst.s6_addr16[1]) { 46178064Sume ip6stat.ip6s_badscope++; 46278064Sume goto bad; 46378064Sume } 46453541Sshin } 46553541Sshin 46678064Sume if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 46778064Sume ip6->ip6_src.s6_addr16[1] 46878064Sume = htons(m->m_pkthdr.rcvif->if_index); 46978064Sume if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 47078064Sume ip6->ip6_dst.s6_addr16[1] 47178064Sume = htons(m->m_pkthdr.rcvif->if_index); 47253541Sshin 47378064Sume#if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */ 47453541Sshin /* 47578064Sume * We use rt->rt_ifp to determine if the address is ours or not. 47678064Sume * If rt_ifp is lo0, the address is ours. 47778064Sume * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0, 47878064Sume * so any address under fe80::%lo0/64 will be mistakenly considered 47978064Sume * local. The special case is supplied to handle the case properly 48078064Sume * by actually looking at interface addresses 48178064Sume * (using in6ifa_ifpwithaddr). 48253541Sshin */ 48378064Sume if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 && 48478064Sume IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 48578064Sume if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) { 48678064Sume icmp6_error(m, ICMP6_DST_UNREACH, 48778064Sume ICMP6_DST_UNREACH_ADDR, 0); 48878064Sume /* m is already freed */ 48978064Sume return; 49078064Sume } 49175246Sume 49278064Sume ours = 1; 49378064Sume deliverifp = m->m_pkthdr.rcvif; 49478064Sume goto hbhcheck; 49578064Sume } 49675719Sume#endif 49753541Sshin 49853541Sshin /* 49953541Sshin * Multicast check 50053541Sshin */ 50153541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 50253541Sshin struct in6_multi *in6m = 0; 50353541Sshin 50453541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 50553541Sshin /* 50653541Sshin * See if we belong to the destination multicast group on the 50753541Sshin * arrival interface. 50853541Sshin */ 50953541Sshin IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 51053541Sshin if (in6m) 51153541Sshin ours = 1; 51256723Sshin else if (!ip6_mrouter) { 51353541Sshin ip6stat.ip6s_notmember++; 51453541Sshin ip6stat.ip6s_cantforward++; 51553541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 51653541Sshin goto bad; 51753541Sshin } 51853541Sshin deliverifp = m->m_pkthdr.rcvif; 51953541Sshin goto hbhcheck; 52053541Sshin } 52153541Sshin 52253541Sshin /* 52353541Sshin * Unicast check 52453541Sshin */ 52578064Sume switch (ip6_ours_check_algorithm) { 52678064Sume default: 52778064Sume /* 52878064Sume * XXX: I intentionally broke our indentation rule here, 52978064Sume * since this switch-case is just for measurement and 53078064Sume * therefore should soon be removed. 53178064Sume */ 53262587Sitojun if (ip6_forward_rt.ro_rt != NULL && 53362587Sitojun (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 53462587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 53578064Sume &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 53662587Sitojun ip6stat.ip6s_forward_cachehit++; 53762587Sitojun else { 53878064Sume struct sockaddr_in6 *dst6; 53978064Sume 54053541Sshin if (ip6_forward_rt.ro_rt) { 54162587Sitojun /* route is down or destination is different */ 54262587Sitojun ip6stat.ip6s_forward_cachemiss++; 54353541Sshin RTFREE(ip6_forward_rt.ro_rt); 54453541Sshin ip6_forward_rt.ro_rt = 0; 54553541Sshin } 54662587Sitojun 54753541Sshin bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 54878064Sume dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 54978064Sume dst6->sin6_len = sizeof(struct sockaddr_in6); 55078064Sume dst6->sin6_family = AF_INET6; 55178064Sume dst6->sin6_addr = ip6->ip6_dst; 55262587Sitojun#ifdef SCOPEDROUTING 55362587Sitojun ip6_forward_rt.ro_dst.sin6_scope_id = 55462587Sitojun in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst); 55562587Sitojun#endif 55653541Sshin 55753541Sshin rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 55853541Sshin } 55953541Sshin 56053541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 56153541Sshin 56253541Sshin /* 56353541Sshin * Accept the packet if the forwarding interface to the destination 56453541Sshin * according to the routing table is the loopback interface, 56553541Sshin * unless the associated route has a gateway. 56653541Sshin * Note that this approach causes to accept a packet if there is a 56753541Sshin * route to the loopback interface for the destination of the packet. 56853541Sshin * But we think it's even useful in some situations, e.g. when using 56953541Sshin * a special daemon which wants to intercept the packet. 57078064Sume * 57178064Sume * XXX: some OSes automatically make a cloned route for the destination 57278064Sume * of an outgoing packet. If the outgoing interface of the packet 57378064Sume * is a loopback one, the kernel would consider the packet to be 57478064Sume * accepted, even if we have no such address assinged on the interface. 57578064Sume * We check the cloned flag of the route entry to reject such cases, 57678064Sume * assuming that route entries for our own addresses are not made by 57778064Sume * cloning (it should be true because in6_addloop explicitly installs 57878064Sume * the host route). However, we might have to do an explicit check 57978064Sume * while it would be less efficient. Or, should we rather install a 58078064Sume * reject route for such a case? 58153541Sshin */ 58253541Sshin if (ip6_forward_rt.ro_rt && 58353541Sshin (ip6_forward_rt.ro_rt->rt_flags & 58453541Sshin (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 58578064Sume#ifdef RTF_WASCLONED 58678064Sume !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 58778064Sume#endif 58878064Sume#ifdef RTF_CLONED 58978064Sume !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 59078064Sume#endif 59162587Sitojun#if 0 59253541Sshin /* 59362587Sitojun * The check below is redundant since the comparison of 59462587Sitojun * the destination and the key of the rtentry has 59562587Sitojun * already done through looking up the routing table. 59653541Sshin */ 59762587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 59878064Sume &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 59962587Sitojun#endif 60053541Sshin ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 60153541Sshin struct in6_ifaddr *ia6 = 60253541Sshin (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 60378064Sume 60462587Sitojun /* 60578064Sume * record address information into m_aux. 60678064Sume */ 60778064Sume (void)ip6_setdstifaddr(m, ia6); 60878064Sume 60978064Sume /* 61062587Sitojun * packets to a tentative, duplicated, or somehow invalid 61162587Sitojun * address must not be accepted. 61262587Sitojun */ 61353541Sshin if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 61462587Sitojun /* this address is ready */ 61553541Sshin ours = 1; 61653541Sshin deliverifp = ia6->ia_ifp; /* correct? */ 61767334Sjoe /* Count the packet in the ip address stats */ 61867334Sjoe ia6->ia_ifa.if_ipackets++; 61967334Sjoe ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 62053541Sshin goto hbhcheck; 62153541Sshin } else { 62262587Sitojun /* address is not ready, so discard the packet. */ 62378064Sume nd6log((LOG_INFO, 62478064Sume "ip6_input: packet to an unready address %s->%s\n", 62562587Sitojun ip6_sprintf(&ip6->ip6_src), 62678064Sume ip6_sprintf(&ip6->ip6_dst))); 62762587Sitojun 62862587Sitojun goto bad; 62953541Sshin } 63053541Sshin } 63178064Sume } /* XXX indentation (see above) */ 63253541Sshin 63353541Sshin /* 63453541Sshin * FAITH(Firewall Aided Internet Translator) 63553541Sshin */ 63653541Sshin#if defined(NFAITH) && 0 < NFAITH 63753541Sshin if (ip6_keepfaith) { 63853541Sshin if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 63953541Sshin && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 64053541Sshin /* XXX do we need more sanity checks? */ 64153541Sshin ours = 1; 64253541Sshin deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/ 64353541Sshin goto hbhcheck; 64453541Sshin } 64553541Sshin } 64653541Sshin#endif 64753541Sshin 64853541Sshin /* 64953541Sshin * Now there is no reason to process the packet if it's not our own 65053541Sshin * and we're not a router. 65153541Sshin */ 65253541Sshin if (!ip6_forwarding) { 65353541Sshin ip6stat.ip6s_cantforward++; 65453541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 65553541Sshin goto bad; 65653541Sshin } 65753541Sshin 65853541Sshin hbhcheck: 65953541Sshin /* 66078064Sume * record address information into m_aux, if we don't have one yet. 66178064Sume * note that we are unable to record it, if the address is not listed 66278064Sume * as our interface address (e.g. multicast addresses, addresses 66378064Sume * within FAITH prefixes and such). 66478064Sume */ 66578064Sume if (deliverifp && !ip6_getdstifaddr(m)) { 66678064Sume struct in6_ifaddr *ia6; 66778064Sume 66878064Sume ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 66978064Sume if (ia6) { 67078064Sume if (!ip6_setdstifaddr(m, ia6)) { 67178064Sume /* 67278064Sume * XXX maybe we should drop the packet here, 67378064Sume * as we could not provide enough information 67478064Sume * to the upper layers. 67578064Sume */ 67678064Sume } 67778064Sume } 67878064Sume } 67978064Sume 68078064Sume /* 68153541Sshin * Process Hop-by-Hop options header if it's contained. 68253541Sshin * m may be modified in ip6_hopopts_input(). 68353541Sshin * If a JumboPayload option is included, plen will also be modified. 68453541Sshin */ 68553541Sshin plen = (u_int32_t)ntohs(ip6->ip6_plen); 68653541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 68762587Sitojun struct ip6_hbh *hbh; 68862587Sitojun 68953541Sshin if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 69062587Sitojun#if 0 /*touches NULL pointer*/ 69153541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 69262587Sitojun#endif 69353541Sshin return; /* m have already been freed */ 69453541Sshin } 69562587Sitojun 69653541Sshin /* adjust pointer */ 69753541Sshin ip6 = mtod(m, struct ip6_hdr *); 69853541Sshin 69953541Sshin /* 70062587Sitojun * if the payload length field is 0 and the next header field 70162587Sitojun * indicates Hop-by-Hop Options header, then a Jumbo Payload 70262587Sitojun * option MUST be included. 70362587Sitojun */ 70462587Sitojun if (ip6->ip6_plen == 0 && plen == 0) { 70562587Sitojun /* 70662587Sitojun * Note that if a valid jumbo payload option is 70762587Sitojun * contained, ip6_hoptops_input() must set a valid 70862587Sitojun * (non-zero) payload length to the variable plen. 70962587Sitojun */ 71062587Sitojun ip6stat.ip6s_badoptions++; 71162587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 71262587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 71362587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 71462587Sitojun ICMP6_PARAMPROB_HEADER, 71562587Sitojun (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 71662587Sitojun return; 71762587Sitojun } 71862587Sitojun#ifndef PULLDOWN_TEST 71962587Sitojun /* ip6_hopopts_input() ensures that mbuf is contiguous */ 72062587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 72162587Sitojun#else 72262587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 72362587Sitojun sizeof(struct ip6_hbh)); 72462587Sitojun if (hbh == NULL) { 72562587Sitojun ip6stat.ip6s_tooshort++; 72662587Sitojun return; 72762587Sitojun } 72862587Sitojun#endif 72962587Sitojun nxt = hbh->ip6h_nxt; 73062587Sitojun 73162587Sitojun /* 73253541Sshin * accept the packet if a router alert option is included 73353541Sshin * and we act as an IPv6 router. 73453541Sshin */ 73553541Sshin if (rtalert != ~0 && ip6_forwarding) 73653541Sshin ours = 1; 73753541Sshin } else 73853541Sshin nxt = ip6->ip6_nxt; 73953541Sshin 74053541Sshin /* 74153541Sshin * Check that the amount of data in the buffers 74253541Sshin * is as at least much as the IPv6 header would have us expect. 74353541Sshin * Trim mbufs if longer than we expect. 74453541Sshin * Drop packet if shorter than we expect. 74553541Sshin */ 74653541Sshin if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 74753541Sshin ip6stat.ip6s_tooshort++; 74853541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 74953541Sshin goto bad; 75053541Sshin } 75153541Sshin if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 75253541Sshin if (m->m_len == m->m_pkthdr.len) { 75353541Sshin m->m_len = sizeof(struct ip6_hdr) + plen; 75453541Sshin m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 75553541Sshin } else 75653541Sshin m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 75753541Sshin } 75853541Sshin 75953541Sshin /* 76053541Sshin * Forward if desirable. 76153541Sshin */ 76253541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 76356723Sshin /* 76456723Sshin * If we are acting as a multicast router, all 76556723Sshin * incoming multicast packets are passed to the 76656723Sshin * kernel-level multicast forwarding function. 76756723Sshin * The packet is returned (relatively) intact; if 76856723Sshin * ip6_mforward() returns a non-zero value, the packet 76956723Sshin * must be discarded, else it may be accepted below. 77056723Sshin */ 77156723Sshin if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 77256723Sshin ip6stat.ip6s_cantforward++; 77356723Sshin m_freem(m); 77456723Sshin return; 77556723Sshin } 77653541Sshin if (!ours) { 77753541Sshin m_freem(m); 77853541Sshin return; 77953541Sshin } 78053541Sshin } else if (!ours) { 78153541Sshin ip6_forward(m, 0); 78253541Sshin return; 78353541Sshin } 78453541Sshin 78562587Sitojun ip6 = mtod(m, struct ip6_hdr *); 78662587Sitojun 78753541Sshin /* 78862587Sitojun * Malicious party may be able to use IPv4 mapped addr to confuse 78962587Sitojun * tcp/udp stack and bypass security checks (act as if it was from 79062587Sitojun * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 79162587Sitojun * 79262587Sitojun * For SIIT end node behavior, you may want to disable the check. 79362587Sitojun * However, you will become vulnerable to attacks using IPv4 mapped 79462587Sitojun * source. 79562587Sitojun */ 79662587Sitojun if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 79762587Sitojun IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 79862587Sitojun ip6stat.ip6s_badscope++; 79962587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 80062587Sitojun goto bad; 80162587Sitojun } 80262587Sitojun 80362587Sitojun /* 80453541Sshin * Tell launch routine the next header 80553541Sshin */ 80653541Sshin ip6stat.ip6s_delivered++; 80753541Sshin in6_ifstat_inc(deliverifp, ifs6_in_deliver); 80853541Sshin nest = 0; 80978064Sume 81053541Sshin while (nxt != IPPROTO_DONE) { 81153541Sshin if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 81253541Sshin ip6stat.ip6s_toomanyhdr++; 81353541Sshin goto bad; 81453541Sshin } 81553541Sshin 81653541Sshin /* 81753541Sshin * protection against faulty packet - there should be 81853541Sshin * more sanity checks in header chain processing. 81953541Sshin */ 82053541Sshin if (m->m_pkthdr.len < off) { 82153541Sshin ip6stat.ip6s_tooshort++; 82253541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 82353541Sshin goto bad; 82453541Sshin } 82553541Sshin 82678064Sume#if 0 82778064Sume /* 82878064Sume * do we need to do it for every header? yeah, other 82978064Sume * functions can play with it (like re-allocate and copy). 83078064Sume */ 83178064Sume mhist = ip6_addaux(m); 83278064Sume if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) { 83378064Sume hist = mtod(mhist, caddr_t) + mhist->m_len; 83478064Sume bcopy(&nxt, hist, sizeof(nxt)); 83578064Sume mhist->m_len += sizeof(nxt); 83678064Sume } else { 83778064Sume ip6stat.ip6s_toomanyhdr++; 83878064Sume goto bad; 83978064Sume } 84078064Sume#endif 84178064Sume 84278064Sume#ifdef IPSEC 84378064Sume /* 84478064Sume * enforce IPsec policy checking if we are seeing last header. 84578064Sume * note that we do not visit this with protocols with pcb layer 84678064Sume * code - like udp/tcp/raw ip. 84778064Sume */ 84878064Sume if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 84978064Sume ipsec6_in_reject(m, NULL)) { 85078064Sume ipsec6stat.in_polvio++; 85178064Sume goto bad; 85278064Sume } 85378064Sume#endif 85478064Sume 85553541Sshin nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 85653541Sshin } 85753541Sshin return; 85853541Sshin bad: 85953541Sshin m_freem(m); 86053541Sshin} 86153541Sshin 86253541Sshin/* 86378064Sume * set/grab in6_ifaddr correspond to IPv6 destination address. 86478064Sume * XXX backward compatibility wrapper 86578064Sume */ 86678064Sumestatic struct mbuf * 86778064Sumeip6_setdstifaddr(m, ia6) 86878064Sume struct mbuf *m; 86978064Sume struct in6_ifaddr *ia6; 87078064Sume{ 87178064Sume struct mbuf *n; 87278064Sume 87378064Sume n = ip6_addaux(m); 87478064Sume if (n) 87578064Sume mtod(n, struct ip6aux *)->ip6a_dstia6 = ia6; 87678064Sume return n; /* NULL if failed to set */ 87778064Sume} 87878064Sume 87978064Sumestruct in6_ifaddr * 88078064Sumeip6_getdstifaddr(m) 88178064Sume struct mbuf *m; 88278064Sume{ 88378064Sume struct mbuf *n; 88478064Sume 88578064Sume n = ip6_findaux(m); 88678064Sume if (n) 88778064Sume return mtod(n, struct ip6aux *)->ip6a_dstia6; 88878064Sume else 88978064Sume return NULL; 89078064Sume} 89178064Sume 89278064Sume/* 89353541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is 89453541Sshin * included, the real payload length will be stored in plenp. 89553541Sshin */ 89653541Sshinstatic int 89753541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp) 89853541Sshin u_int32_t *plenp; 89953541Sshin u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 90053541Sshin struct mbuf **mp; 90153541Sshin int *offp; 90253541Sshin{ 90378064Sume struct mbuf *m = *mp; 90453541Sshin int off = *offp, hbhlen; 90553541Sshin struct ip6_hbh *hbh; 90653541Sshin u_int8_t *opt; 90753541Sshin 90853541Sshin /* validation of the length of the header */ 90962587Sitojun#ifndef PULLDOWN_TEST 91053541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 91153541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 91253541Sshin hbhlen = (hbh->ip6h_len + 1) << 3; 91353541Sshin 91453541Sshin IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 91553541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 91662587Sitojun#else 91762587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 91862587Sitojun sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 91962587Sitojun if (hbh == NULL) { 92062587Sitojun ip6stat.ip6s_tooshort++; 92162587Sitojun return -1; 92262587Sitojun } 92362587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 92462587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 92562587Sitojun hbhlen); 92662587Sitojun if (hbh == NULL) { 92762587Sitojun ip6stat.ip6s_tooshort++; 92862587Sitojun return -1; 92962587Sitojun } 93062587Sitojun#endif 93153541Sshin off += hbhlen; 93253541Sshin hbhlen -= sizeof(struct ip6_hbh); 93353541Sshin opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 93453541Sshin 93553541Sshin if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 93653541Sshin hbhlen, rtalertp, plenp) < 0) 93753541Sshin return(-1); 93853541Sshin 93953541Sshin *offp = off; 94053541Sshin *mp = m; 94153541Sshin return(0); 94253541Sshin} 94353541Sshin 94453541Sshin/* 94553541Sshin * Search header for all Hop-by-hop options and process each option. 94653541Sshin * This function is separate from ip6_hopopts_input() in order to 94753541Sshin * handle a case where the sending node itself process its hop-by-hop 94853541Sshin * options header. In such a case, the function is called from ip6_output(). 94978064Sume * 95078064Sume * The function assumes that hbh header is located right after the IPv6 header 95178064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 95278064Sume * opthead + hbhlen is located in continuous memory region. 95353541Sshin */ 95453541Sshinint 95553541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 95653541Sshin struct mbuf *m; 95753541Sshin u_int8_t *opthead; 95853541Sshin int hbhlen; 95953541Sshin u_int32_t *rtalertp; 96053541Sshin u_int32_t *plenp; 96153541Sshin{ 96253541Sshin struct ip6_hdr *ip6; 96353541Sshin int optlen = 0; 96453541Sshin u_int8_t *opt = opthead; 96553541Sshin u_int16_t rtalert_val; 96662587Sitojun u_int32_t jumboplen; 96778064Sume const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 96853541Sshin 96953541Sshin for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 97078064Sume switch (*opt) { 97178064Sume case IP6OPT_PAD1: 97278064Sume optlen = 1; 97378064Sume break; 97478064Sume case IP6OPT_PADN: 97578064Sume if (hbhlen < IP6OPT_MINLEN) { 97678064Sume ip6stat.ip6s_toosmall++; 97778064Sume goto bad; 97878064Sume } 97978064Sume optlen = *(opt + 1) + 2; 98078064Sume break; 98178064Sume case IP6OPT_RTALERT: 98262587Sitojun /* XXX may need check for alignment */ 98378064Sume if (hbhlen < IP6OPT_RTALERT_LEN) { 98478064Sume ip6stat.ip6s_toosmall++; 98578064Sume goto bad; 98678064Sume } 98778064Sume if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 98878064Sume /* XXX stat */ 98978064Sume icmp6_error(m, ICMP6_PARAM_PROB, 99078064Sume ICMP6_PARAMPROB_HEADER, 99178064Sume erroff + opt + 1 - opthead); 99278064Sume return(-1); 99378064Sume } 99478064Sume optlen = IP6OPT_RTALERT_LEN; 99578064Sume bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 99678064Sume *rtalertp = ntohs(rtalert_val); 99778064Sume break; 99878064Sume case IP6OPT_JUMBO: 99978064Sume /* XXX may need check for alignment */ 100062587Sitojun if (hbhlen < IP6OPT_JUMBO_LEN) { 100162587Sitojun ip6stat.ip6s_toosmall++; 100262587Sitojun goto bad; 100362587Sitojun } 100478064Sume if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 100578064Sume /* XXX stat */ 100678064Sume icmp6_error(m, ICMP6_PARAM_PROB, 100778064Sume ICMP6_PARAMPROB_HEADER, 100878064Sume erroff + opt + 1 - opthead); 100978064Sume return(-1); 101078064Sume } 101162587Sitojun optlen = IP6OPT_JUMBO_LEN; 101253541Sshin 101362587Sitojun /* 101462587Sitojun * IPv6 packets that have non 0 payload length 101578064Sume * must not contain a jumbo payload option. 101662587Sitojun */ 101762587Sitojun ip6 = mtod(m, struct ip6_hdr *); 101862587Sitojun if (ip6->ip6_plen) { 101962587Sitojun ip6stat.ip6s_badoptions++; 102062587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 102162587Sitojun ICMP6_PARAMPROB_HEADER, 102278064Sume erroff + opt - opthead); 102362587Sitojun return(-1); 102462587Sitojun } 102553541Sshin 102662587Sitojun /* 102762587Sitojun * We may see jumbolen in unaligned location, so 102862587Sitojun * we'd need to perform bcopy(). 102962587Sitojun */ 103062587Sitojun bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 103162587Sitojun jumboplen = (u_int32_t)htonl(jumboplen); 103262587Sitojun 103362587Sitojun#if 1 103462587Sitojun /* 103562587Sitojun * if there are multiple jumbo payload options, 103662587Sitojun * *plenp will be non-zero and the packet will be 103762587Sitojun * rejected. 103862587Sitojun * the behavior may need some debate in ipngwg - 103962587Sitojun * multiple options does not make sense, however, 104062587Sitojun * there's no explicit mention in specification. 104162587Sitojun */ 104262587Sitojun if (*plenp != 0) { 104362587Sitojun ip6stat.ip6s_badoptions++; 104462587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 104562587Sitojun ICMP6_PARAMPROB_HEADER, 104678064Sume erroff + opt + 2 - opthead); 104762587Sitojun return(-1); 104862587Sitojun } 104962587Sitojun#endif 105062587Sitojun 105162587Sitojun /* 105262587Sitojun * jumbo payload length must be larger than 65535. 105362587Sitojun */ 105462587Sitojun if (jumboplen <= IPV6_MAXPACKET) { 105562587Sitojun ip6stat.ip6s_badoptions++; 105662587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 105762587Sitojun ICMP6_PARAMPROB_HEADER, 105878064Sume erroff + opt + 2 - opthead); 105962587Sitojun return(-1); 106062587Sitojun } 106162587Sitojun *plenp = jumboplen; 106262587Sitojun 106362587Sitojun break; 106478064Sume default: /* unknown option */ 106578064Sume if (hbhlen < IP6OPT_MINLEN) { 106678064Sume ip6stat.ip6s_toosmall++; 106778064Sume goto bad; 106878064Sume } 106978064Sume optlen = ip6_unknown_opt(opt, m, 107078064Sume erroff + opt - opthead); 107178064Sume if (optlen == -1) 107278064Sume return(-1); 107378064Sume optlen += 2; 107478064Sume break; 107553541Sshin } 107653541Sshin } 107753541Sshin 107853541Sshin return(0); 107953541Sshin 108053541Sshin bad: 108153541Sshin m_freem(m); 108253541Sshin return(-1); 108353541Sshin} 108453541Sshin 108553541Sshin/* 108653541Sshin * Unknown option processing. 108753541Sshin * The third argument `off' is the offset from the IPv6 header to the option, 108853541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header 108953541Sshin * is not continuous in order to return an ICMPv6 error. 109053541Sshin */ 109153541Sshinint 109253541Sshinip6_unknown_opt(optp, m, off) 109353541Sshin u_int8_t *optp; 109453541Sshin struct mbuf *m; 109553541Sshin int off; 109653541Sshin{ 109753541Sshin struct ip6_hdr *ip6; 109853541Sshin 109978064Sume switch (IP6OPT_TYPE(*optp)) { 110078064Sume case IP6OPT_TYPE_SKIP: /* ignore the option */ 110178064Sume return((int)*(optp + 1)); 110278064Sume case IP6OPT_TYPE_DISCARD: /* silently discard */ 110378064Sume m_freem(m); 110478064Sume return(-1); 110578064Sume case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 110678064Sume ip6stat.ip6s_badoptions++; 110778064Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 110878064Sume return(-1); 110978064Sume case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 111078064Sume ip6stat.ip6s_badoptions++; 111178064Sume ip6 = mtod(m, struct ip6_hdr *); 111278064Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 111378064Sume (m->m_flags & (M_BCAST|M_MCAST))) 111478064Sume m_freem(m); 111578064Sume else 111678064Sume icmp6_error(m, ICMP6_PARAM_PROB, 111778064Sume ICMP6_PARAMPROB_OPTION, off); 111878064Sume return(-1); 111953541Sshin } 112053541Sshin 112153541Sshin m_freem(m); /* XXX: NOTREACHED */ 112253541Sshin return(-1); 112353541Sshin} 112453541Sshin 112553541Sshin/* 112662587Sitojun * Create the "control" list for this pcb. 112778064Sume * The function will not modify mbuf chain at all. 112862587Sitojun * 112978064Sume * with KAME mbuf chain restriction: 113062587Sitojun * The routine will be called from upper layer handlers like tcp6_input(). 113162587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already 113262587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 113362587Sitojun * very first mbuf on the mbuf chain. 113453541Sshin */ 113553541Sshinvoid 113653541Sshinip6_savecontrol(in6p, mp, ip6, m) 113778064Sume struct inpcb *in6p; 113878064Sume struct mbuf **mp; 113978064Sume struct ip6_hdr *ip6; 114078064Sume struct mbuf *m; 114153541Sshin{ 114253541Sshin struct proc *p = curproc; /* XXX */ 114378064Sume int privileged = 0; 114478064Sume int rthdr_exist = 0; 114553541Sshin 114678064Sume 114753541Sshin if (p && !suser(p)) 114878064Sume privileged++; 114953541Sshin 115078064Sume#ifdef SO_TIMESTAMP 115178064Sume if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 115253541Sshin struct timeval tv; 115353541Sshin 115453541Sshin microtime(&tv); 115553541Sshin *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 115678064Sume SCM_TIMESTAMP, SOL_SOCKET); 115778064Sume if (*mp) { 115853541Sshin mp = &(*mp)->m_next; 115978064Sume } 116053541Sshin } 116162587Sitojun#endif 116262587Sitojun 116353541Sshin /* RFC 2292 sec. 5 */ 116478064Sume if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 116553541Sshin struct in6_pktinfo pi6; 116653541Sshin bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 116753541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr)) 116853541Sshin pi6.ipi6_addr.s6_addr16[1] = 0; 116953541Sshin pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif) 117053541Sshin ? m->m_pkthdr.rcvif->if_index 117153541Sshin : 0; 117253541Sshin *mp = sbcreatecontrol((caddr_t) &pi6, 117353541Sshin sizeof(struct in6_pktinfo), IPV6_PKTINFO, 117453541Sshin IPPROTO_IPV6); 117553541Sshin if (*mp) 117653541Sshin mp = &(*mp)->m_next; 117753541Sshin } 117878064Sume 117978064Sume if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 118053541Sshin int hlim = ip6->ip6_hlim & 0xff; 118153541Sshin *mp = sbcreatecontrol((caddr_t) &hlim, 118253541Sshin sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6); 118353541Sshin if (*mp) 118453541Sshin mp = &(*mp)->m_next; 118553541Sshin } 118653541Sshin 118753541Sshin /* 118853541Sshin * IPV6_HOPOPTS socket option. We require super-user privilege 118953541Sshin * for the option, but it might be too strict, since there might 119053541Sshin * be some hop-by-hop options which can be returned to normal user. 119153541Sshin * See RFC 2292 section 6. 119253541Sshin */ 119378064Sume if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) { 119453541Sshin /* 119553541Sshin * Check if a hop-by-hop options header is contatined in the 119653541Sshin * received packet, and if so, store the options as ancillary 119753541Sshin * data. Note that a hop-by-hop options header must be 119853541Sshin * just after the IPv6 header, which fact is assured through 119953541Sshin * the IPv6 input processing. 120053541Sshin */ 120153541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 120253541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 120362587Sitojun struct ip6_hbh *hbh; 120478064Sume int hbhlen = 0; 120578064Sume#ifdef PULLDOWN_TEST 120678064Sume struct mbuf *ext; 120778064Sume#endif 120853541Sshin 120962587Sitojun#ifndef PULLDOWN_TEST 121062587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 121162587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 121262587Sitojun#else 121378064Sume ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 121478064Sume ip6->ip6_nxt); 121578064Sume if (ext == NULL) { 121662587Sitojun ip6stat.ip6s_tooshort++; 121762587Sitojun return; 121862587Sitojun } 121978064Sume hbh = mtod(ext, struct ip6_hbh *); 122062587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 122178064Sume if (hbhlen != ext->m_len) { 122278064Sume m_freem(ext); 122362587Sitojun ip6stat.ip6s_tooshort++; 122462587Sitojun return; 122562587Sitojun } 122662587Sitojun#endif 122762587Sitojun 122853541Sshin /* 122953541Sshin * XXX: We copy whole the header even if a jumbo 123053541Sshin * payload option is included, which option is to 123153541Sshin * be removed before returning in the RFC 2292. 123278064Sume * Note: this constraint is removed in 2292bis. 123353541Sshin */ 123462587Sitojun *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 123553541Sshin IPV6_HOPOPTS, IPPROTO_IPV6); 123653541Sshin if (*mp) 123753541Sshin mp = &(*mp)->m_next; 123878064Sume#ifdef PULLDOWN_TEST 123978064Sume m_freem(ext); 124078064Sume#endif 124153541Sshin } 124253541Sshin } 124353541Sshin 124453541Sshin /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ 124578064Sume if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 124678064Sume int proto, off, nxt; 124778064Sume 124878064Sume /* 124978064Sume * go through the header chain to see if a routing header is 125078064Sume * contained in the packet. We need this information to store 125178064Sume * destination options headers (if any) properly. 125278064Sume * XXX: performance issue. We should record this info when 125378064Sume * processing extension headers in incoming routine. 125478064Sume * (todo) use m_aux? 125578064Sume */ 125678064Sume proto = IPPROTO_IPV6; 125778064Sume off = 0; 125878064Sume nxt = -1; 125978064Sume while (1) { 126078064Sume int newoff; 126178064Sume 126278064Sume newoff = ip6_nexthdr(m, off, proto, &nxt); 126378064Sume if (newoff < 0) 126478064Sume break; 126578064Sume if (newoff < off) /* invalid, check for safety */ 126678064Sume break; 126778064Sume if ((proto = nxt) == IPPROTO_ROUTING) { 126878064Sume rthdr_exist = 1; 126978064Sume break; 127078064Sume } 127178064Sume off = newoff; 127278064Sume } 127378064Sume } 127478064Sume 127578064Sume if ((in6p->in6p_flags & 127678064Sume (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 127753541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 127878064Sume int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 127953541Sshin 128053541Sshin /* 128153541Sshin * Search for destination options headers or routing 128253541Sshin * header(s) through the header chain, and stores each 128353541Sshin * header as ancillary data. 128453541Sshin * Note that the order of the headers remains in 128553541Sshin * the chain of ancillary data. 128653541Sshin */ 128778064Sume while (1) { /* is explicit loop prevention necessary? */ 128878064Sume struct ip6_ext *ip6e = NULL; 128962587Sitojun int elen; 129078064Sume#ifdef PULLDOWN_TEST 129178064Sume struct mbuf *ext = NULL; 129278064Sume#endif 129353541Sshin 129478064Sume /* 129578064Sume * if it is not an extension header, don't try to 129678064Sume * pull it from the chain. 129778064Sume */ 129878064Sume switch (nxt) { 129978064Sume case IPPROTO_DSTOPTS: 130078064Sume case IPPROTO_ROUTING: 130178064Sume case IPPROTO_HOPOPTS: 130278064Sume case IPPROTO_AH: /* is it possible? */ 130378064Sume break; 130478064Sume default: 130578064Sume goto loopend; 130678064Sume } 130778064Sume 130862587Sitojun#ifndef PULLDOWN_TEST 130978064Sume if (off + sizeof(*ip6e) > m->m_len) 131078064Sume goto loopend; 131162587Sitojun ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 131262587Sitojun if (nxt == IPPROTO_AH) 131362587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 131462587Sitojun else 131562587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 131678064Sume if (off + elen > m->m_len) 131778064Sume goto loopend; 131862587Sitojun#else 131978064Sume ext = ip6_pullexthdr(m, off, nxt); 132078064Sume if (ext == NULL) { 132162587Sitojun ip6stat.ip6s_tooshort++; 132262587Sitojun return; 132362587Sitojun } 132478064Sume ip6e = mtod(ext, struct ip6_ext *); 132562587Sitojun if (nxt == IPPROTO_AH) 132662587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 132762587Sitojun else 132862587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 132978064Sume if (elen != ext->m_len) { 133078064Sume m_freem(ext); 133162587Sitojun ip6stat.ip6s_tooshort++; 133262587Sitojun return; 133362587Sitojun } 133462587Sitojun#endif 133562587Sitojun 133678064Sume switch (nxt) { 133778064Sume case IPPROTO_DSTOPTS: 133878064Sume if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0) 133978064Sume break; 134053541Sshin 134178064Sume /* 134278064Sume * We also require super-user privilege for 134378064Sume * the option. 134478064Sume * See the comments on IN6_HOPOPTS. 134578064Sume */ 134678064Sume if (!privileged) 134778064Sume break; 134853541Sshin 134978064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 135078064Sume IPV6_DSTOPTS, 135178064Sume IPPROTO_IPV6); 135278064Sume if (*mp) 135378064Sume mp = &(*mp)->m_next; 135478064Sume break; 135578064Sume case IPPROTO_ROUTING: 135678064Sume if (!in6p->in6p_flags & IN6P_RTHDR) 135778064Sume break; 135853541Sshin 135978064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 136078064Sume IPV6_RTHDR, 136178064Sume IPPROTO_IPV6); 136278064Sume if (*mp) 136378064Sume mp = &(*mp)->m_next; 136478064Sume break; 136578064Sume case IPPROTO_HOPOPTS: 136678064Sume case IPPROTO_AH: /* is it possible? */ 136778064Sume break; 136853541Sshin 136978064Sume default: 137078064Sume /* 137178064Sume * other cases have been filtered in the above. 137278064Sume * none will visit this case. here we supply 137378064Sume * the code just in case (nxt overwritten or 137478064Sume * other cases). 137578064Sume */ 137678064Sume#ifdef PULLDOWN_TEST 137778064Sume m_freem(ext); 137878064Sume#endif 137978064Sume goto loopend; 138053541Sshin 138153541Sshin } 138253541Sshin 138353541Sshin /* proceed with the next header. */ 138462587Sitojun off += elen; 138553541Sshin nxt = ip6e->ip6e_nxt; 138678064Sume ip6e = NULL; 138778064Sume#ifdef PULLDOWN_TEST 138878064Sume m_freem(ext); 138978064Sume ext = NULL; 139078064Sume#endif 139153541Sshin } 139253541Sshin loopend: 139378064Sume ; 139453541Sshin } 139578064Sume 139678064Sume} 139778064Sume 139878064Sume#ifdef PULLDOWN_TEST 139978064Sume/* 140078064Sume * pull single extension header from mbuf chain. returns single mbuf that 140178064Sume * contains the result, or NULL on error. 140278064Sume */ 140378064Sumestatic struct mbuf * 140478064Sumeip6_pullexthdr(m, off, nxt) 140578064Sume struct mbuf *m; 140678064Sume size_t off; 140778064Sume int nxt; 140878064Sume{ 140978064Sume struct ip6_ext ip6e; 141078064Sume size_t elen; 141178064Sume struct mbuf *n; 141278064Sume 141378064Sume#ifdef DIAGNOSTIC 141478064Sume switch (nxt) { 141578064Sume case IPPROTO_DSTOPTS: 141678064Sume case IPPROTO_ROUTING: 141778064Sume case IPPROTO_HOPOPTS: 141878064Sume case IPPROTO_AH: /* is it possible? */ 141978064Sume break; 142078064Sume default: 142178064Sume printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 142253541Sshin } 142378064Sume#endif 142478064Sume 142578064Sume m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 142678064Sume if (nxt == IPPROTO_AH) 142778064Sume elen = (ip6e.ip6e_len + 2) << 2; 142878064Sume else 142978064Sume elen = (ip6e.ip6e_len + 1) << 3; 143078064Sume 143178064Sume MGET(n, M_DONTWAIT, MT_DATA); 143278064Sume if (n && elen >= MLEN) { 143378064Sume MCLGET(n, M_DONTWAIT); 143478064Sume if ((n->m_flags & M_EXT) == 0) { 143578064Sume m_free(n); 143678064Sume n = NULL; 143778064Sume } 143853541Sshin } 143978064Sume if (!n) 144078064Sume return NULL; 144162587Sitojun 144278064Sume n->m_len = 0; 144378064Sume if (elen >= M_TRAILINGSPACE(n)) { 144478064Sume m_free(n); 144578064Sume return NULL; 144678064Sume } 144778064Sume 144878064Sume m_copydata(m, off, elen, mtod(n, caddr_t)); 144978064Sume n->m_len = elen; 145078064Sume return n; 145153541Sshin} 145278064Sume#endif 145353541Sshin 145453541Sshin/* 145553541Sshin * Get pointer to the previous header followed by the header 145653541Sshin * currently processed. 145753541Sshin * XXX: This function supposes that 145853541Sshin * M includes all headers, 145953541Sshin * the next header field and the header length field of each header 146053541Sshin * are valid, and 146153541Sshin * the sum of each header length equals to OFF. 146253541Sshin * Because of these assumptions, this function must be called very 146353541Sshin * carefully. Moreover, it will not be used in the near future when 146453541Sshin * we develop `neater' mechanism to process extension headers. 146553541Sshin */ 146653541Sshinchar * 146753541Sshinip6_get_prevhdr(m, off) 146853541Sshin struct mbuf *m; 146953541Sshin int off; 147053541Sshin{ 147153541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 147253541Sshin 147353541Sshin if (off == sizeof(struct ip6_hdr)) 147453541Sshin return(&ip6->ip6_nxt); 147553541Sshin else { 147653541Sshin int len, nxt; 147753541Sshin struct ip6_ext *ip6e = NULL; 147853541Sshin 147953541Sshin nxt = ip6->ip6_nxt; 148053541Sshin len = sizeof(struct ip6_hdr); 148153541Sshin while (len < off) { 148253541Sshin ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 148353541Sshin 148478064Sume switch (nxt) { 148553541Sshin case IPPROTO_FRAGMENT: 148653541Sshin len += sizeof(struct ip6_frag); 148753541Sshin break; 148853541Sshin case IPPROTO_AH: 148953541Sshin len += (ip6e->ip6e_len + 2) << 2; 149053541Sshin break; 149153541Sshin default: 149253541Sshin len += (ip6e->ip6e_len + 1) << 3; 149353541Sshin break; 149453541Sshin } 149553541Sshin nxt = ip6e->ip6e_nxt; 149653541Sshin } 149753541Sshin if (ip6e) 149853541Sshin return(&ip6e->ip6e_nxt); 149953541Sshin else 150053541Sshin return NULL; 150153541Sshin } 150253541Sshin} 150353541Sshin 150453541Sshin/* 150562587Sitojun * get next header offset. m will be retained. 150662587Sitojun */ 150762587Sitojunint 150862587Sitojunip6_nexthdr(m, off, proto, nxtp) 150962587Sitojun struct mbuf *m; 151062587Sitojun int off; 151162587Sitojun int proto; 151262587Sitojun int *nxtp; 151362587Sitojun{ 151462587Sitojun struct ip6_hdr ip6; 151562587Sitojun struct ip6_ext ip6e; 151662587Sitojun struct ip6_frag fh; 151762587Sitojun 151862587Sitojun /* just in case */ 151962587Sitojun if (m == NULL) 152062587Sitojun panic("ip6_nexthdr: m == NULL"); 152162587Sitojun if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 152262587Sitojun return -1; 152362587Sitojun 152462587Sitojun switch (proto) { 152562587Sitojun case IPPROTO_IPV6: 152662587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6)) 152762587Sitojun return -1; 152862587Sitojun m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 152962587Sitojun if (nxtp) 153062587Sitojun *nxtp = ip6.ip6_nxt; 153162587Sitojun off += sizeof(ip6); 153262587Sitojun return off; 153362587Sitojun 153462587Sitojun case IPPROTO_FRAGMENT: 153562587Sitojun /* 153662587Sitojun * terminate parsing if it is not the first fragment, 153762587Sitojun * it does not make sense to parse through it. 153862587Sitojun */ 153962587Sitojun if (m->m_pkthdr.len < off + sizeof(fh)) 154062587Sitojun return -1; 154162587Sitojun m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 154262587Sitojun if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0) 154362587Sitojun return -1; 154462587Sitojun if (nxtp) 154562587Sitojun *nxtp = fh.ip6f_nxt; 154662587Sitojun off += sizeof(struct ip6_frag); 154762587Sitojun return off; 154862587Sitojun 154962587Sitojun case IPPROTO_AH: 155062587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6e)) 155162587Sitojun return -1; 155262587Sitojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 155362587Sitojun if (nxtp) 155462587Sitojun *nxtp = ip6e.ip6e_nxt; 155562587Sitojun off += (ip6e.ip6e_len + 2) << 2; 155662587Sitojun return off; 155762587Sitojun 155862587Sitojun case IPPROTO_HOPOPTS: 155962587Sitojun case IPPROTO_ROUTING: 156062587Sitojun case IPPROTO_DSTOPTS: 156162587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6e)) 156262587Sitojun return -1; 156362587Sitojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 156462587Sitojun if (nxtp) 156562587Sitojun *nxtp = ip6e.ip6e_nxt; 156662587Sitojun off += (ip6e.ip6e_len + 1) << 3; 156762587Sitojun return off; 156862587Sitojun 156962587Sitojun case IPPROTO_NONE: 157062587Sitojun case IPPROTO_ESP: 157162587Sitojun case IPPROTO_IPCOMP: 157262587Sitojun /* give up */ 157362587Sitojun return -1; 157462587Sitojun 157562587Sitojun default: 157662587Sitojun return -1; 157762587Sitojun } 157862587Sitojun 157962587Sitojun return -1; 158062587Sitojun} 158162587Sitojun 158262587Sitojun/* 158362587Sitojun * get offset for the last header in the chain. m will be kept untainted. 158462587Sitojun */ 158562587Sitojunint 158662587Sitojunip6_lasthdr(m, off, proto, nxtp) 158762587Sitojun struct mbuf *m; 158862587Sitojun int off; 158962587Sitojun int proto; 159062587Sitojun int *nxtp; 159162587Sitojun{ 159262587Sitojun int newoff; 159362587Sitojun int nxt; 159462587Sitojun 159562587Sitojun if (!nxtp) { 159662587Sitojun nxt = -1; 159762587Sitojun nxtp = &nxt; 159862587Sitojun } 159962587Sitojun while (1) { 160062587Sitojun newoff = ip6_nexthdr(m, off, proto, nxtp); 160162587Sitojun if (newoff < 0) 160262587Sitojun return off; 160362587Sitojun else if (newoff < off) 160462587Sitojun return -1; /* invalid */ 160562587Sitojun else if (newoff == off) 160662587Sitojun return newoff; 160762587Sitojun 160862587Sitojun off = newoff; 160962587Sitojun proto = *nxtp; 161062587Sitojun } 161162587Sitojun} 161262587Sitojun 161378064Sumestruct mbuf * 161478064Sumeip6_addaux(m) 161578064Sume struct mbuf *m; 161678064Sume{ 161778064Sume struct mbuf *n; 161878064Sume 161978064Sume#ifdef DIAGNOSTIC 162078064Sume if (sizeof(struct ip6aux) > MHLEN) 162178064Sume panic("assumption failed on sizeof(ip6aux)"); 162278064Sume#endif 162378064Sume n = m_aux_find(m, AF_INET6, -1); 162478064Sume if (n) { 162578064Sume if (n->m_len < sizeof(struct ip6aux)) { 162678064Sume printf("conflicting use of ip6aux"); 162778064Sume return NULL; 162878064Sume } 162978064Sume } else { 163078064Sume n = m_aux_add(m, AF_INET6, -1); 163178064Sume n->m_len = sizeof(struct ip6aux); 163278064Sume bzero(mtod(n, caddr_t), n->m_len); 163378064Sume } 163478064Sume return n; 163578064Sume} 163678064Sume 163778064Sumestruct mbuf * 163878064Sumeip6_findaux(m) 163978064Sume struct mbuf *m; 164078064Sume{ 164178064Sume struct mbuf *n; 164278064Sume 164378064Sume n = m_aux_find(m, AF_INET6, -1); 164478064Sume if (n && n->m_len < sizeof(struct ip6aux)) { 164578064Sume printf("conflicting use of ip6aux"); 164678064Sume n = NULL; 164778064Sume } 164878064Sume return n; 164978064Sume} 165078064Sume 165178064Sumevoid 165278064Sumeip6_delaux(m) 165378064Sume struct mbuf *m; 165478064Sume{ 165578064Sume struct mbuf *n; 165678064Sume 165778064Sume n = m_aux_find(m, AF_INET6, -1); 165878064Sume if (n) 165978064Sume m_aux_delete(m, n); 166078064Sume} 166178064Sume 166262587Sitojun/* 166353541Sshin * System control for IP6 166453541Sshin */ 166553541Sshin 166653541Sshinu_char inet6ctlerrmap[PRC_NCMDS] = { 166753541Sshin 0, 0, 0, 0, 166853541Sshin 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 166953541Sshin EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 167053541Sshin EMSGSIZE, EHOSTUNREACH, 0, 0, 167153541Sshin 0, 0, 0, 0, 167253541Sshin ENOPROTOOPT 167353541Sshin}; 1674