ip6_input.c revision 154804
162587Sitojun/* $FreeBSD: head/sys/netinet6/ip6_input.c 154804 2006-01-25 08:17:02Z ume $ */ 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_ip6fw.h" 6562587Sitojun#include "opt_inet.h" 6662587Sitojun#include "opt_inet6.h" 6755009Sshin#include "opt_ipsec.h" 6855009Sshin 6953541Sshin#include <sys/param.h> 7053541Sshin#include <sys/systm.h> 7178064Sume#include <sys/malloc.h> 7253541Sshin#include <sys/mbuf.h> 7383366Sjulian#include <sys/proc.h> 7453541Sshin#include <sys/domain.h> 7553541Sshin#include <sys/protosw.h> 7653541Sshin#include <sys/socket.h> 7753541Sshin#include <sys/socketvar.h> 7853541Sshin#include <sys/errno.h> 7953541Sshin#include <sys/time.h> 8053541Sshin#include <sys/kernel.h> 8153541Sshin#include <sys/syslog.h> 8253541Sshin 8353541Sshin#include <net/if.h> 8453541Sshin#include <net/if_types.h> 8553541Sshin#include <net/if_dl.h> 8653541Sshin#include <net/route.h> 8753541Sshin#include <net/netisr.h> 8864060Sdarrenr#include <net/pfil.h> 8953541Sshin 9053541Sshin#include <netinet/in.h> 9153541Sshin#include <netinet/in_systm.h> 9262587Sitojun#ifdef INET 9353541Sshin#include <netinet/ip.h> 9453541Sshin#include <netinet/ip_icmp.h> 9595023Ssuz#endif /* INET */ 9662587Sitojun#include <netinet/ip6.h> 9753541Sshin#include <netinet6/in6_var.h> 9853541Sshin#include <netinet6/ip6_var.h> 9962587Sitojun#include <netinet/in_pcb.h> 10062587Sitojun#include <netinet/icmp6.h> 101121161Sume#include <netinet6/scope6_var.h> 10253541Sshin#include <netinet6/in6_ifattach.h> 10353541Sshin#include <netinet6/nd6.h> 10453541Sshin 10578064Sume#ifdef IPSEC 10678064Sume#include <netinet6/ipsec.h> 10778064Sume#ifdef INET6 10878064Sume#include <netinet6/ipsec6.h> 10978064Sume#endif 11078064Sume#endif 11178064Sume 112105199Ssam#ifdef FAST_IPSEC 113105199Ssam#include <netipsec/ipsec.h> 114105199Ssam#include <netipsec/ipsec6.h> 115105199Ssam#define IPSEC 116105199Ssam#endif /* FAST_IPSEC */ 117105199Ssam 11853541Sshin#include <netinet6/ip6_fw.h> 11953541Sshin 12053541Sshin#include <netinet6/ip6protosw.h> 12153541Sshin 12253541Sshin#include <net/net_osdep.h> 12353541Sshin 12462587Sitojunextern struct domain inet6domain; 12553541Sshin 12662587Sitojunu_char ip6_protox[IPPROTO_MAX]; 127111888Sjlemonstatic struct ifqueue ip6intrq; 12862587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN; 12962587Sitojunstruct in6_ifaddr *in6_ifaddr; 13053541Sshin 13178064Sumeextern struct callout in6_tmpaddrtimer_ch; 13278064Sume 13362587Sitojunint ip6_forward_srcrt; /* XXX */ 13462587Sitojunint ip6_sourcecheck; /* XXX */ 13562587Sitojunint ip6_sourcecheck_interval; /* XXX */ 13653541Sshin 13778064Sumeint ip6_ours_check_algorithm; 13878064Sume 139120386Ssamstruct pfil_head inet6_pfil_hook; 14078064Sume 14153541Sshin/* firewall hooks */ 14262587Sitojunip6_fw_chk_t *ip6_fw_chk_ptr; 14362587Sitojunip6_fw_ctl_t *ip6_fw_ctl_ptr; 14466303Sumeint ip6_fw_enable = 1; 14553541Sshin 14662587Sitojunstruct ip6stat ip6stat; 14753541Sshin 14862587Sitojunstatic void ip6_init2 __P((void *)); 149121673Sumestatic struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *)); 15062587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 15162587Sitojun#ifdef PULLDOWN_TEST 15262587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int)); 15353541Sshin#endif 15453541Sshin 15553541Sshin/* 15653541Sshin * IP6 initialization: fill in IP6 protocol switch table. 15753541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler. 15853541Sshin */ 15953541Sshinvoid 16053541Sshinip6_init() 16153541Sshin{ 16278064Sume struct ip6protosw *pr; 16378064Sume int i; 16453541Sshin 16578064Sume#ifdef DIAGNOSTIC 16678064Sume if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 16778064Sume panic("sizeof(protosw) != sizeof(ip6protosw)"); 16878064Sume#endif 16953541Sshin pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 17053541Sshin if (pr == 0) 17153541Sshin panic("ip6_init"); 172136689Sandre 173136689Sandre /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 17453541Sshin for (i = 0; i < IPPROTO_MAX; i++) 17553541Sshin ip6_protox[i] = pr - inet6sw; 176136689Sandre /* 177136689Sandre * Cycle through IP protocols and put them into the appropriate place 178136689Sandre * in ip6_protox[]. 179136689Sandre */ 18053541Sshin for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 18153541Sshin pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 18253541Sshin if (pr->pr_domain->dom_family == PF_INET6 && 183136689Sandre pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 184136689Sandre /* Be careful to only index valid IP protocols. */ 185143675Ssam if (pr->pr_protocol < IPPROTO_MAX) 186136689Sandre ip6_protox[pr->pr_protocol] = pr - inet6sw; 187136689Sandre } 188134383Sandre 189134383Sandre /* Initialize packet filter hooks. */ 190120386Ssam inet6_pfil_hook.ph_type = PFIL_TYPE_AF; 191120386Ssam inet6_pfil_hook.ph_af = AF_INET6; 192120386Ssam if ((i = pfil_head_register(&inet6_pfil_hook)) != 0) 193120386Ssam printf("%s: WARNING: unable to register pfil hook, " 194120386Ssam "error %d\n", __func__, i); 195134383Sandre 19653541Sshin ip6intrq.ifq_maxlen = ip6qmaxlen; 19793818Sjhb mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF); 198122320Ssam netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0); 199121161Sume scope6_init(); 200121742Sume addrsel_policy_init(); 20153541Sshin nd6_init(); 20253541Sshin frag6_init(); 203120648Sume ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR; 20453541Sshin} 20553541Sshin 20653541Sshinstatic void 20753541Sshinip6_init2(dummy) 20853541Sshin void *dummy; 20953541Sshin{ 21053541Sshin 21153541Sshin /* nd6_timer_init */ 21278064Sume callout_init(&nd6_timer_ch, 0); 21378064Sume callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 21478064Sume 21578064Sume /* timer for regeneranation of temporary addresses randomize ID */ 216121806Sume callout_init(&in6_tmpaddrtimer_ch, 0); 21778064Sume callout_reset(&in6_tmpaddrtimer_ch, 21878064Sume (ip6_temp_preferred_lifetime - ip6_desync_factor - 21978064Sume ip6_temp_regen_advance) * hz, 22078064Sume in6_tmpaddrtimer, NULL); 22153541Sshin} 22253541Sshin 22353541Sshin/* cheat */ 22455009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */ 22555009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 22653541Sshin 22753541Sshinextern struct route_in6 ip6_forward_rt; 22853541Sshin 22953541Sshinvoid 23053541Sshinip6_input(m) 23153541Sshin struct mbuf *m; 23253541Sshin{ 23353541Sshin struct ip6_hdr *ip6; 23453541Sshin int off = sizeof(struct ip6_hdr), nest; 23553541Sshin u_int32_t plen; 23653541Sshin u_int32_t rtalert = ~0; 23753541Sshin int nxt, ours = 0; 23853541Sshin struct ifnet *deliverifp = NULL; 239121143Ssam struct in6_addr odst; 240121143Ssam int srcrt = 0; 241121144Ssam 242122320Ssam GIANT_REQUIRED; /* XXX for now */ 24353541Sshin#ifdef IPSEC 24453541Sshin /* 24553541Sshin * should the inner packet be considered authentic? 24653541Sshin * see comment in ah4_input(). 24753541Sshin */ 24853541Sshin if (m) { 24953541Sshin m->m_flags &= ~M_AUTHIPHDR; 25053541Sshin m->m_flags &= ~M_AUTHIPDGM; 25153541Sshin } 25253541Sshin#endif 25353541Sshin 25453541Sshin /* 255121630Sume * make sure we don't have onion peering information into m_tag. 25678064Sume */ 25778064Sume ip6_delaux(m); 25878064Sume 25978064Sume /* 26095023Ssuz * mbuf statistics 26153541Sshin */ 26253541Sshin if (m->m_flags & M_EXT) { 26353541Sshin if (m->m_next) 26453541Sshin ip6stat.ip6s_mext2m++; 26553541Sshin else 26653541Sshin ip6stat.ip6s_mext1++; 26753541Sshin } else { 26878064Sume#define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0])) 26953541Sshin if (m->m_next) { 27053541Sshin if (m->m_flags & M_LOOP) { 271120913Sume ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */ 27278064Sume } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 27353541Sshin ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 27453541Sshin else 27553541Sshin ip6stat.ip6s_m2m[0]++; 27653541Sshin } else 27753541Sshin ip6stat.ip6s_m1++; 27878064Sume#undef M2MMAX 27953541Sshin } 28053541Sshin 281151474Ssuz /* drop the packet if IPv6 operation is disabled on the IF */ 282151474Ssuz if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) { 283151474Ssuz m_freem(m); 284151474Ssuz return; 285151474Ssuz } 286151474Ssuz 28753541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 28853541Sshin ip6stat.ip6s_total++; 28953541Sshin 29062587Sitojun#ifndef PULLDOWN_TEST 29174336Skuriyama /* 29274336Skuriyama * L2 bridge code and some other code can return mbuf chain 29374336Skuriyama * that does not conform to KAME requirement. too bad. 29474336Skuriyama * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 29574336Skuriyama */ 29674336Skuriyama if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 29774336Skuriyama struct mbuf *n; 29874336Skuriyama 299111119Simp MGETHDR(n, M_DONTWAIT, MT_HEADER); 30077003Sume if (n) 301108466Ssam M_MOVE_PKTHDR(n, m); 302108825Ssam if (n && n->m_pkthdr.len > MHLEN) { 303111119Simp MCLGET(n, M_DONTWAIT); 30474336Skuriyama if ((n->m_flags & M_EXT) == 0) { 30574336Skuriyama m_freem(n); 30674336Skuriyama n = NULL; 30774336Skuriyama } 30874336Skuriyama } 30995023Ssuz if (n == NULL) { 31077003Sume m_freem(m); 311120913Sume return; /* ENOBUFS */ 31277003Sume } 31374336Skuriyama 314108825Ssam m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 315108825Ssam n->m_len = n->m_pkthdr.len; 31674336Skuriyama m_freem(m); 31774336Skuriyama m = n; 31874336Skuriyama } 319120913Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */); 32062587Sitojun#endif 32153541Sshin 32253541Sshin if (m->m_len < sizeof(struct ip6_hdr)) { 32353541Sshin struct ifnet *inifp; 32453541Sshin inifp = m->m_pkthdr.rcvif; 325120913Sume if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 32653541Sshin ip6stat.ip6s_toosmall++; 32753541Sshin in6_ifstat_inc(inifp, ifs6_in_hdrerr); 32853541Sshin return; 32953541Sshin } 33053541Sshin } 33153541Sshin 33253541Sshin ip6 = mtod(m, struct ip6_hdr *); 33353541Sshin 33453541Sshin if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 33553541Sshin ip6stat.ip6s_badvers++; 33653541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 33753541Sshin goto bad; 33853541Sshin } 33953541Sshin 34053541Sshin ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 34153541Sshin 34253541Sshin /* 34378064Sume * Check against address spoofing/corruption. 34453541Sshin */ 34553541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 34653541Sshin IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 34778064Sume /* 34878064Sume * XXX: "badscope" is not very suitable for a multicast source. 34978064Sume */ 35053541Sshin ip6stat.ip6s_badscope++; 35153541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 35253541Sshin goto bad; 35353541Sshin } 354126444Sume if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 355126444Sume !(m->m_flags & M_LOOP)) { 356126444Sume /* 357126444Sume * In this case, the packet should come from the loopback 358126444Sume * interface. However, we cannot just check the if_flags, 359126444Sume * because ip6_mloopback() passes the "actual" interface 360126444Sume * as the outgoing/incoming interface. 361126444Sume */ 36278064Sume ip6stat.ip6s_badscope++; 36378064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 36478064Sume goto bad; 36578064Sume } 36695023Ssuz 367130416Smlaier#ifdef ALTQ 368130416Smlaier if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) { 369130416Smlaier /* packet is dropped by traffic conditioner */ 370130416Smlaier return; 371130416Smlaier } 372130416Smlaier#endif 37362587Sitojun /* 37478064Sume * The following check is not documented in specs. A malicious 37578064Sume * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 37678064Sume * and bypass security checks (act as if it was from 127.0.0.1 by using 377120913Sume * IPv6 src ::ffff:127.0.0.1). Be cautious. 37878064Sume * 37978064Sume * This check chokes if we are in an SIIT cloud. As none of BSDs 38078064Sume * support IPv4-less kernel compilation, we cannot support SIIT 38178064Sume * environment at all. So, it makes more sense for us to reject any 38278064Sume * malicious packets for non-SIIT environment, than try to do a 383120913Sume * partial support for SIIT environment. 38462587Sitojun */ 38578064Sume if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 38678064Sume IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 38778064Sume ip6stat.ip6s_badscope++; 38878064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 38978064Sume goto bad; 39078064Sume } 39162587Sitojun#if 0 39262587Sitojun /* 39362587Sitojun * Reject packets with IPv4 compatible addresses (auto tunnel). 39462587Sitojun * 39562587Sitojun * The code forbids auto tunnel relay case in RFC1933 (the check is 39662587Sitojun * stronger than RFC1933). We may want to re-enable it if mech-xx 39762587Sitojun * is revised to forbid relaying case. 39862587Sitojun */ 39962587Sitojun if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 40062587Sitojun IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 40162587Sitojun ip6stat.ip6s_badscope++; 40262587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 40362587Sitojun goto bad; 40462587Sitojun } 40562587Sitojun#endif 40675246Sume 407126444Sume /* 408126508Smlaier * Run through list of hooks for input packets. 409126508Smlaier * 410126508Smlaier * NB: Beware of the destination address changing 411126508Smlaier * (e.g. by NAT rewriting). When this happens, 412126508Smlaier * tell ip6_forward to do the right thing. 413126508Smlaier */ 414126508Smlaier odst = ip6->ip6_dst; 415134383Sandre 416134383Sandre /* Jump over all PFIL processing if hooks are not active. */ 417134383Sandre if (inet6_pfil_hook.ph_busy_count == -1) 418134383Sandre goto passin; 419134383Sandre 420135920Smlaier if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL)) 421126508Smlaier return; 422126508Smlaier if (m == NULL) /* consumed by filter */ 423126508Smlaier return; 424126508Smlaier ip6 = mtod(m, struct ip6_hdr *); 425126508Smlaier srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 426126508Smlaier 427134383Sandrepassin: 428126508Smlaier /* 429126508Smlaier * Check with the firewall... 430126508Smlaier */ 431126508Smlaier if (ip6_fw_enable && ip6_fw_chk_ptr) { 432126508Smlaier u_short port = 0; 433126508Smlaier /* If ipfw says divert, we have to just drop packet */ 434126508Smlaier /* use port as a dummy argument */ 435126508Smlaier if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 436126508Smlaier m_freem(m); 437126508Smlaier m = NULL; 438126508Smlaier } 439126508Smlaier if (!m) 440126508Smlaier return; 441126508Smlaier } 442126508Smlaier 443126508Smlaier /* 444154804Sume * Disambiguate address scope zones (if there is ambiguity). 445154804Sume * We first make sure that the original source or destination address 446154804Sume * is not in our internal form for scoped addresses. Such addresses 447154804Sume * are not necessarily invalid spec-wise, but we cannot accept them due 448154804Sume * to the usage conflict. 449154804Sume * in6_setscope() then also checks and rejects the cases where src or 450154804Sume * dst are the loopback address and the receiving interface 451154804Sume * is not loopback. 452154804Sume */ 453154804Sume if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) { 454154804Sume ip6stat.ip6s_badscope++; /* XXX */ 455154804Sume goto bad; 456154804Sume } 457154804Sume if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) || 458154804Sume in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) { 459154804Sume ip6stat.ip6s_badscope++; 460154804Sume goto bad; 461154804Sume } 462154804Sume 463154804Sume /* 46453541Sshin * Multicast check 46553541Sshin */ 46653541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 467120913Sume struct in6_multi *in6m = 0; 46853541Sshin 46953541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 47053541Sshin /* 47153541Sshin * See if we belong to the destination multicast group on the 47253541Sshin * arrival interface. 47353541Sshin */ 47453541Sshin IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 47553541Sshin if (in6m) 47653541Sshin ours = 1; 47756723Sshin else if (!ip6_mrouter) { 47853541Sshin ip6stat.ip6s_notmember++; 47953541Sshin ip6stat.ip6s_cantforward++; 48053541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 48153541Sshin goto bad; 48253541Sshin } 48353541Sshin deliverifp = m->m_pkthdr.rcvif; 48453541Sshin goto hbhcheck; 48553541Sshin } 48653541Sshin 48753541Sshin /* 48853541Sshin * Unicast check 48953541Sshin */ 49062587Sitojun if (ip6_forward_rt.ro_rt != NULL && 491120913Sume (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 49262587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 493120913Sume &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 49462587Sitojun ip6stat.ip6s_forward_cachehit++; 49562587Sitojun else { 49678064Sume struct sockaddr_in6 *dst6; 49778064Sume 49853541Sshin if (ip6_forward_rt.ro_rt) { 49962587Sitojun /* route is down or destination is different */ 50062587Sitojun ip6stat.ip6s_forward_cachemiss++; 50153541Sshin RTFREE(ip6_forward_rt.ro_rt); 50253541Sshin ip6_forward_rt.ro_rt = 0; 50353541Sshin } 50462587Sitojun 50553541Sshin bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 50678064Sume dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 50778064Sume dst6->sin6_len = sizeof(struct sockaddr_in6); 50878064Sume dst6->sin6_family = AF_INET6; 50978064Sume dst6->sin6_addr = ip6->ip6_dst; 51053541Sshin 511122921Sandre rtalloc((struct route *)&ip6_forward_rt); 51253541Sshin } 51353541Sshin 51453541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 51553541Sshin 51653541Sshin /* 51753541Sshin * Accept the packet if the forwarding interface to the destination 51853541Sshin * according to the routing table is the loopback interface, 51953541Sshin * unless the associated route has a gateway. 52053541Sshin * Note that this approach causes to accept a packet if there is a 52153541Sshin * route to the loopback interface for the destination of the packet. 52253541Sshin * But we think it's even useful in some situations, e.g. when using 52353541Sshin * a special daemon which wants to intercept the packet. 52478064Sume * 52578064Sume * XXX: some OSes automatically make a cloned route for the destination 52678064Sume * of an outgoing packet. If the outgoing interface of the packet 52778064Sume * is a loopback one, the kernel would consider the packet to be 52878064Sume * accepted, even if we have no such address assinged on the interface. 52978064Sume * We check the cloned flag of the route entry to reject such cases, 53078064Sume * assuming that route entries for our own addresses are not made by 53178064Sume * cloning (it should be true because in6_addloop explicitly installs 53278064Sume * the host route). However, we might have to do an explicit check 53378064Sume * while it would be less efficient. Or, should we rather install a 53478064Sume * reject route for such a case? 53553541Sshin */ 53653541Sshin if (ip6_forward_rt.ro_rt && 53753541Sshin (ip6_forward_rt.ro_rt->rt_flags & 53853541Sshin (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 53978064Sume#ifdef RTF_WASCLONED 54078064Sume !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 54178064Sume#endif 54278064Sume#ifdef RTF_CLONED 54378064Sume !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 54478064Sume#endif 54562587Sitojun#if 0 54653541Sshin /* 54762587Sitojun * The check below is redundant since the comparison of 54862587Sitojun * the destination and the key of the rtentry has 54962587Sitojun * already done through looking up the routing table. 55053541Sshin */ 55162587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 552120913Sume &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 55362587Sitojun#endif 55453541Sshin ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 55553541Sshin struct in6_ifaddr *ia6 = 55653541Sshin (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 55778064Sume 55862587Sitojun /* 559121630Sume * record address information into m_tag. 56078064Sume */ 56178064Sume (void)ip6_setdstifaddr(m, ia6); 56278064Sume 56378064Sume /* 56462587Sitojun * packets to a tentative, duplicated, or somehow invalid 56562587Sitojun * address must not be accepted. 56662587Sitojun */ 56753541Sshin if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 56862587Sitojun /* this address is ready */ 56953541Sshin ours = 1; 57053541Sshin deliverifp = ia6->ia_ifp; /* correct? */ 57167334Sjoe /* Count the packet in the ip address stats */ 57267334Sjoe ia6->ia_ifa.if_ipackets++; 57367334Sjoe ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 57453541Sshin goto hbhcheck; 57553541Sshin } else { 57662587Sitojun /* address is not ready, so discard the packet. */ 57778064Sume nd6log((LOG_INFO, 57878064Sume "ip6_input: packet to an unready address %s->%s\n", 57962587Sitojun ip6_sprintf(&ip6->ip6_src), 58078064Sume ip6_sprintf(&ip6->ip6_dst))); 58162587Sitojun 58262587Sitojun goto bad; 58353541Sshin } 58453541Sshin } 58553541Sshin 58653541Sshin /* 587120913Sume * FAITH (Firewall Aided Internet Translator) 58853541Sshin */ 58953541Sshin if (ip6_keepfaith) { 59053541Sshin if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 59153541Sshin && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 59253541Sshin /* XXX do we need more sanity checks? */ 59353541Sshin ours = 1; 59495023Ssuz deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ 59553541Sshin goto hbhcheck; 59653541Sshin } 59753541Sshin } 59853541Sshin 59953541Sshin /* 60053541Sshin * Now there is no reason to process the packet if it's not our own 60153541Sshin * and we're not a router. 60253541Sshin */ 60353541Sshin if (!ip6_forwarding) { 60453541Sshin ip6stat.ip6s_cantforward++; 60553541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 60653541Sshin goto bad; 60753541Sshin } 60853541Sshin 60953541Sshin hbhcheck: 61053541Sshin /* 611121630Sume * record address information into m_tag, if we don't have one yet. 61278064Sume * note that we are unable to record it, if the address is not listed 61378064Sume * as our interface address (e.g. multicast addresses, addresses 61478064Sume * within FAITH prefixes and such). 61578064Sume */ 61678064Sume if (deliverifp && !ip6_getdstifaddr(m)) { 61778064Sume struct in6_ifaddr *ia6; 61878064Sume 61978064Sume ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 62078064Sume if (ia6) { 62178064Sume if (!ip6_setdstifaddr(m, ia6)) { 62278064Sume /* 62378064Sume * XXX maybe we should drop the packet here, 62478064Sume * as we could not provide enough information 62578064Sume * to the upper layers. 62678064Sume */ 62778064Sume } 62878064Sume } 62978064Sume } 63078064Sume 63178064Sume /* 63253541Sshin * Process Hop-by-Hop options header if it's contained. 63353541Sshin * m may be modified in ip6_hopopts_input(). 63453541Sshin * If a JumboPayload option is included, plen will also be modified. 63553541Sshin */ 63653541Sshin plen = (u_int32_t)ntohs(ip6->ip6_plen); 63753541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 63862587Sitojun struct ip6_hbh *hbh; 63962587Sitojun 64053541Sshin if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 64162587Sitojun#if 0 /*touches NULL pointer*/ 64253541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 64362587Sitojun#endif 64453541Sshin return; /* m have already been freed */ 64553541Sshin } 64662587Sitojun 64753541Sshin /* adjust pointer */ 64853541Sshin ip6 = mtod(m, struct ip6_hdr *); 64953541Sshin 65053541Sshin /* 65195023Ssuz * if the payload length field is 0 and the next header field 65262587Sitojun * indicates Hop-by-Hop Options header, then a Jumbo Payload 65362587Sitojun * option MUST be included. 65462587Sitojun */ 65562587Sitojun if (ip6->ip6_plen == 0 && plen == 0) { 65662587Sitojun /* 65762587Sitojun * Note that if a valid jumbo payload option is 658120913Sume * contained, ip6_hopopts_input() must set a valid 659120913Sume * (non-zero) payload length to the variable plen. 66062587Sitojun */ 66162587Sitojun ip6stat.ip6s_badoptions++; 66262587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 66362587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 66462587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 66562587Sitojun ICMP6_PARAMPROB_HEADER, 66662587Sitojun (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 66762587Sitojun return; 66862587Sitojun } 66962587Sitojun#ifndef PULLDOWN_TEST 67062587Sitojun /* ip6_hopopts_input() ensures that mbuf is contiguous */ 67162587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 67262587Sitojun#else 67362587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 67462587Sitojun sizeof(struct ip6_hbh)); 67562587Sitojun if (hbh == NULL) { 67662587Sitojun ip6stat.ip6s_tooshort++; 67762587Sitojun return; 67862587Sitojun } 67962587Sitojun#endif 68062587Sitojun nxt = hbh->ip6h_nxt; 68162587Sitojun 68262587Sitojun /* 68353541Sshin * accept the packet if a router alert option is included 68453541Sshin * and we act as an IPv6 router. 68553541Sshin */ 68653541Sshin if (rtalert != ~0 && ip6_forwarding) 68753541Sshin ours = 1; 68853541Sshin } else 68953541Sshin nxt = ip6->ip6_nxt; 69053541Sshin 69153541Sshin /* 69253541Sshin * Check that the amount of data in the buffers 69353541Sshin * is as at least much as the IPv6 header would have us expect. 69453541Sshin * Trim mbufs if longer than we expect. 69553541Sshin * Drop packet if shorter than we expect. 69653541Sshin */ 69753541Sshin if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 69853541Sshin ip6stat.ip6s_tooshort++; 69953541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 70053541Sshin goto bad; 70153541Sshin } 70253541Sshin if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 70353541Sshin if (m->m_len == m->m_pkthdr.len) { 70453541Sshin m->m_len = sizeof(struct ip6_hdr) + plen; 70553541Sshin m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 70653541Sshin } else 70753541Sshin m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 70853541Sshin } 70953541Sshin 71053541Sshin /* 71153541Sshin * Forward if desirable. 71253541Sshin */ 71353541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 71456723Sshin /* 71556723Sshin * If we are acting as a multicast router, all 71656723Sshin * incoming multicast packets are passed to the 71756723Sshin * kernel-level multicast forwarding function. 71856723Sshin * The packet is returned (relatively) intact; if 71956723Sshin * ip6_mforward() returns a non-zero value, the packet 72056723Sshin * must be discarded, else it may be accepted below. 72156723Sshin */ 72256723Sshin if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 72356723Sshin ip6stat.ip6s_cantforward++; 72456723Sshin m_freem(m); 72556723Sshin return; 72656723Sshin } 72753541Sshin if (!ours) { 72853541Sshin m_freem(m); 72953541Sshin return; 73053541Sshin } 73153541Sshin } else if (!ours) { 732121143Ssam ip6_forward(m, srcrt); 73353541Sshin return; 734121673Sume } 73553541Sshin 73662587Sitojun ip6 = mtod(m, struct ip6_hdr *); 73762587Sitojun 73853541Sshin /* 73962587Sitojun * Malicious party may be able to use IPv4 mapped addr to confuse 74062587Sitojun * tcp/udp stack and bypass security checks (act as if it was from 74162587Sitojun * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 74262587Sitojun * 74362587Sitojun * For SIIT end node behavior, you may want to disable the check. 74462587Sitojun * However, you will become vulnerable to attacks using IPv4 mapped 74562587Sitojun * source. 74662587Sitojun */ 74762587Sitojun if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 74862587Sitojun IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 74962587Sitojun ip6stat.ip6s_badscope++; 75062587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 75162587Sitojun goto bad; 75262587Sitojun } 75362587Sitojun 75462587Sitojun /* 75553541Sshin * Tell launch routine the next header 75653541Sshin */ 75753541Sshin ip6stat.ip6s_delivered++; 75853541Sshin in6_ifstat_inc(deliverifp, ifs6_in_deliver); 75953541Sshin nest = 0; 76078064Sume 76153541Sshin while (nxt != IPPROTO_DONE) { 76253541Sshin if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 76353541Sshin ip6stat.ip6s_toomanyhdr++; 76453541Sshin goto bad; 76553541Sshin } 76653541Sshin 76753541Sshin /* 76853541Sshin * protection against faulty packet - there should be 76953541Sshin * more sanity checks in header chain processing. 77053541Sshin */ 77153541Sshin if (m->m_pkthdr.len < off) { 77253541Sshin ip6stat.ip6s_tooshort++; 77353541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 77453541Sshin goto bad; 77553541Sshin } 77653541Sshin 77778064Sume#ifdef IPSEC 77878064Sume /* 77978064Sume * enforce IPsec policy checking if we are seeing last header. 78078064Sume * note that we do not visit this with protocols with pcb layer 78178064Sume * code - like udp/tcp/raw ip. 78278064Sume */ 78378064Sume if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 78478064Sume ipsec6_in_reject(m, NULL)) { 78578064Sume ipsec6stat.in_polvio++; 78678064Sume goto bad; 78778064Sume } 78878064Sume#endif 78953541Sshin nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 79053541Sshin } 79153541Sshin return; 79253541Sshin bad: 79353541Sshin m_freem(m); 79453541Sshin} 79553541Sshin 79653541Sshin/* 79778064Sume * set/grab in6_ifaddr correspond to IPv6 destination address. 79878064Sume * XXX backward compatibility wrapper 79978064Sume */ 800121673Sumestatic struct ip6aux * 80178064Sumeip6_setdstifaddr(m, ia6) 80278064Sume struct mbuf *m; 80378064Sume struct in6_ifaddr *ia6; 80478064Sume{ 805121673Sume struct ip6aux *ip6a; 80678064Sume 807121673Sume ip6a = ip6_addaux(m); 808121673Sume if (ip6a) 809121673Sume ip6a->ip6a_dstia6 = ia6; 810121673Sume return ip6a; /* NULL if failed to set */ 81178064Sume} 81278064Sume 81378064Sumestruct in6_ifaddr * 81478064Sumeip6_getdstifaddr(m) 81578064Sume struct mbuf *m; 81678064Sume{ 817121673Sume struct ip6aux *ip6a; 81878064Sume 819121673Sume ip6a = ip6_findaux(m); 820121673Sume if (ip6a) 821121673Sume return ip6a->ip6a_dstia6; 82278064Sume else 82378064Sume return NULL; 82478064Sume} 82578064Sume 82678064Sume/* 82753541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is 82853541Sshin * included, the real payload length will be stored in plenp. 82953541Sshin */ 83053541Sshinstatic int 83153541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp) 83253541Sshin u_int32_t *plenp; 83353541Sshin u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 83453541Sshin struct mbuf **mp; 83553541Sshin int *offp; 83653541Sshin{ 83778064Sume struct mbuf *m = *mp; 83853541Sshin int off = *offp, hbhlen; 83953541Sshin struct ip6_hbh *hbh; 84053541Sshin u_int8_t *opt; 84153541Sshin 84253541Sshin /* validation of the length of the header */ 84362587Sitojun#ifndef PULLDOWN_TEST 84453541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 84553541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 84653541Sshin hbhlen = (hbh->ip6h_len + 1) << 3; 84753541Sshin 84853541Sshin IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 84953541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 85062587Sitojun#else 85162587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 85262587Sitojun sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 85362587Sitojun if (hbh == NULL) { 85462587Sitojun ip6stat.ip6s_tooshort++; 85562587Sitojun return -1; 85662587Sitojun } 85762587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 85862587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 85962587Sitojun hbhlen); 86062587Sitojun if (hbh == NULL) { 86162587Sitojun ip6stat.ip6s_tooshort++; 86262587Sitojun return -1; 86362587Sitojun } 86462587Sitojun#endif 86553541Sshin off += hbhlen; 86653541Sshin hbhlen -= sizeof(struct ip6_hbh); 86753541Sshin opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 86853541Sshin 86953541Sshin if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 87053541Sshin hbhlen, rtalertp, plenp) < 0) 871120856Sume return (-1); 87253541Sshin 87353541Sshin *offp = off; 87453541Sshin *mp = m; 875120856Sume return (0); 87653541Sshin} 87753541Sshin 87853541Sshin/* 87953541Sshin * Search header for all Hop-by-hop options and process each option. 88053541Sshin * This function is separate from ip6_hopopts_input() in order to 88153541Sshin * handle a case where the sending node itself process its hop-by-hop 88253541Sshin * options header. In such a case, the function is called from ip6_output(). 88378064Sume * 88478064Sume * The function assumes that hbh header is located right after the IPv6 header 88578064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 88678064Sume * opthead + hbhlen is located in continuous memory region. 88753541Sshin */ 88853541Sshinint 88953541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 89053541Sshin struct mbuf *m; 89153541Sshin u_int8_t *opthead; 89253541Sshin int hbhlen; 89353541Sshin u_int32_t *rtalertp; 89453541Sshin u_int32_t *plenp; 89553541Sshin{ 89653541Sshin struct ip6_hdr *ip6; 89753541Sshin int optlen = 0; 89853541Sshin u_int8_t *opt = opthead; 89953541Sshin u_int16_t rtalert_val; 90062587Sitojun u_int32_t jumboplen; 90178064Sume const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 90253541Sshin 90353541Sshin for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 90478064Sume switch (*opt) { 90578064Sume case IP6OPT_PAD1: 90678064Sume optlen = 1; 90778064Sume break; 90878064Sume case IP6OPT_PADN: 90978064Sume if (hbhlen < IP6OPT_MINLEN) { 91078064Sume ip6stat.ip6s_toosmall++; 91178064Sume goto bad; 91278064Sume } 91378064Sume optlen = *(opt + 1) + 2; 91478064Sume break; 915121472Sume case IP6OPT_ROUTER_ALERT: 91662587Sitojun /* XXX may need check for alignment */ 91778064Sume if (hbhlen < IP6OPT_RTALERT_LEN) { 91878064Sume ip6stat.ip6s_toosmall++; 91978064Sume goto bad; 92078064Sume } 92178064Sume if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 92278064Sume /* XXX stat */ 92378064Sume icmp6_error(m, ICMP6_PARAM_PROB, 924120913Sume ICMP6_PARAMPROB_HEADER, 925120913Sume erroff + opt + 1 - opthead); 926120856Sume return (-1); 92778064Sume } 92878064Sume optlen = IP6OPT_RTALERT_LEN; 92978064Sume bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 93078064Sume *rtalertp = ntohs(rtalert_val); 93178064Sume break; 93278064Sume case IP6OPT_JUMBO: 93378064Sume /* XXX may need check for alignment */ 93462587Sitojun if (hbhlen < IP6OPT_JUMBO_LEN) { 93562587Sitojun ip6stat.ip6s_toosmall++; 93662587Sitojun goto bad; 93762587Sitojun } 93878064Sume if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 93978064Sume /* XXX stat */ 94078064Sume icmp6_error(m, ICMP6_PARAM_PROB, 941120913Sume ICMP6_PARAMPROB_HEADER, 942120913Sume erroff + opt + 1 - opthead); 943120856Sume return (-1); 94478064Sume } 94562587Sitojun optlen = IP6OPT_JUMBO_LEN; 94653541Sshin 94762587Sitojun /* 94862587Sitojun * IPv6 packets that have non 0 payload length 94978064Sume * must not contain a jumbo payload option. 95062587Sitojun */ 95162587Sitojun ip6 = mtod(m, struct ip6_hdr *); 95262587Sitojun if (ip6->ip6_plen) { 95362587Sitojun ip6stat.ip6s_badoptions++; 95462587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 955120913Sume ICMP6_PARAMPROB_HEADER, 956120913Sume erroff + opt - opthead); 957120856Sume return (-1); 95862587Sitojun } 95953541Sshin 96062587Sitojun /* 96162587Sitojun * We may see jumbolen in unaligned location, so 96262587Sitojun * we'd need to perform bcopy(). 96362587Sitojun */ 96462587Sitojun bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 96562587Sitojun jumboplen = (u_int32_t)htonl(jumboplen); 96662587Sitojun 96762587Sitojun#if 1 96862587Sitojun /* 96962587Sitojun * if there are multiple jumbo payload options, 97062587Sitojun * *plenp will be non-zero and the packet will be 97162587Sitojun * rejected. 97262587Sitojun * the behavior may need some debate in ipngwg - 97362587Sitojun * multiple options does not make sense, however, 97462587Sitojun * there's no explicit mention in specification. 97562587Sitojun */ 97662587Sitojun if (*plenp != 0) { 97762587Sitojun ip6stat.ip6s_badoptions++; 97862587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 979120913Sume ICMP6_PARAMPROB_HEADER, 980120913Sume erroff + opt + 2 - opthead); 981120856Sume return (-1); 98262587Sitojun } 98362587Sitojun#endif 98462587Sitojun 98562587Sitojun /* 98662587Sitojun * jumbo payload length must be larger than 65535. 98762587Sitojun */ 98862587Sitojun if (jumboplen <= IPV6_MAXPACKET) { 98962587Sitojun ip6stat.ip6s_badoptions++; 99062587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 991120913Sume ICMP6_PARAMPROB_HEADER, 992120913Sume erroff + opt + 2 - opthead); 993120856Sume return (-1); 99462587Sitojun } 99562587Sitojun *plenp = jumboplen; 99662587Sitojun 99762587Sitojun break; 99878064Sume default: /* unknown option */ 99978064Sume if (hbhlen < IP6OPT_MINLEN) { 100078064Sume ip6stat.ip6s_toosmall++; 100178064Sume goto bad; 100278064Sume } 100378064Sume optlen = ip6_unknown_opt(opt, m, 100478064Sume erroff + opt - opthead); 100578064Sume if (optlen == -1) 1006120856Sume return (-1); 100778064Sume optlen += 2; 100878064Sume break; 100953541Sshin } 101053541Sshin } 101153541Sshin 1012120856Sume return (0); 101353541Sshin 101453541Sshin bad: 101553541Sshin m_freem(m); 1016120856Sume return (-1); 101753541Sshin} 101853541Sshin 101953541Sshin/* 102053541Sshin * Unknown option processing. 102153541Sshin * The third argument `off' is the offset from the IPv6 header to the option, 102253541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header 102353541Sshin * is not continuous in order to return an ICMPv6 error. 102453541Sshin */ 102553541Sshinint 102653541Sshinip6_unknown_opt(optp, m, off) 102753541Sshin u_int8_t *optp; 102853541Sshin struct mbuf *m; 102953541Sshin int off; 103053541Sshin{ 103153541Sshin struct ip6_hdr *ip6; 103253541Sshin 103378064Sume switch (IP6OPT_TYPE(*optp)) { 103478064Sume case IP6OPT_TYPE_SKIP: /* ignore the option */ 1035120856Sume return ((int)*(optp + 1)); 103678064Sume case IP6OPT_TYPE_DISCARD: /* silently discard */ 103778064Sume m_freem(m); 1038120856Sume return (-1); 103978064Sume case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 104078064Sume ip6stat.ip6s_badoptions++; 104178064Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1042120856Sume return (-1); 104378064Sume case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 104478064Sume ip6stat.ip6s_badoptions++; 104578064Sume ip6 = mtod(m, struct ip6_hdr *); 104678064Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 104778064Sume (m->m_flags & (M_BCAST|M_MCAST))) 104878064Sume m_freem(m); 104978064Sume else 105078064Sume icmp6_error(m, ICMP6_PARAM_PROB, 105178064Sume ICMP6_PARAMPROB_OPTION, off); 1052120856Sume return (-1); 105353541Sshin } 105453541Sshin 105553541Sshin m_freem(m); /* XXX: NOTREACHED */ 1056120856Sume return (-1); 105753541Sshin} 105853541Sshin 105953541Sshin/* 106062587Sitojun * Create the "control" list for this pcb. 106178064Sume * The function will not modify mbuf chain at all. 106262587Sitojun * 106378064Sume * with KAME mbuf chain restriction: 106462587Sitojun * The routine will be called from upper layer handlers like tcp6_input(). 106562587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already 106662587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 106762587Sitojun * very first mbuf on the mbuf chain. 106853541Sshin */ 106953541Sshinvoid 1070121674Sumeip6_savecontrol(in6p, m, mp) 107178064Sume struct inpcb *in6p; 1072121674Sume struct mbuf *m, **mp; 107353541Sshin{ 1074121472Sume#define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y)) 1075121674Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 107653541Sshin 107778064Sume#ifdef SO_TIMESTAMP 107878064Sume if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 107953541Sshin struct timeval tv; 108053541Sshin 108153541Sshin microtime(&tv); 108253541Sshin *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1083120913Sume SCM_TIMESTAMP, SOL_SOCKET); 1084121472Sume if (*mp) 108553541Sshin mp = &(*mp)->m_next; 108697658Stanimura } 108762587Sitojun#endif 108862587Sitojun 1089121631Sume if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) 1090121631Sume return; 1091121631Sume 109253541Sshin /* RFC 2292 sec. 5 */ 109378064Sume if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 109453541Sshin struct in6_pktinfo pi6; 1095120913Sume 109653541Sshin bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1097121315Sume in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1098120913Sume pi6.ipi6_ifindex = 1099120913Sume (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1100120913Sume 110153541Sshin *mp = sbcreatecontrol((caddr_t) &pi6, 1102120913Sume sizeof(struct in6_pktinfo), 1103121472Sume IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1104121472Sume if (*mp) 110553541Sshin mp = &(*mp)->m_next; 110653541Sshin } 110778064Sume 110878064Sume if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 110953541Sshin int hlim = ip6->ip6_hlim & 0xff; 1110120913Sume 1111120913Sume *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1112121472Sume IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6); 1113121472Sume if (*mp) 111453541Sshin mp = &(*mp)->m_next; 111553541Sshin } 111653541Sshin 1117121472Sume if ((in6p->in6p_flags & IN6P_TCLASS) != 0) { 1118121472Sume u_int32_t flowinfo; 1119121472Sume int tclass; 1120121472Sume 1121121472Sume flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1122121472Sume flowinfo >>= 20; 1123121472Sume 1124121472Sume tclass = flowinfo & 0xff; 1125121472Sume *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass), 1126121472Sume IPV6_TCLASS, IPPROTO_IPV6); 1127121472Sume if (*mp) 1128121472Sume mp = &(*mp)->m_next; 1129121472Sume } 1130121472Sume 113153541Sshin /* 1132130002Sume * IPV6_HOPOPTS socket option. Recall that we required super-user 1133130002Sume * privilege for the option (see ip6_ctloutput), but it might be too 1134130002Sume * strict, since there might be some hop-by-hop options which can be 1135130002Sume * returned to normal user. 1136130002Sume * See also RFC 2292 section 6 (or RFC 3542 section 8). 113753541Sshin */ 1138121472Sume if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) { 113953541Sshin /* 114053541Sshin * Check if a hop-by-hop options header is contatined in the 114153541Sshin * received packet, and if so, store the options as ancillary 114253541Sshin * data. Note that a hop-by-hop options header must be 1143120913Sume * just after the IPv6 header, which is assured through the 1144120913Sume * IPv6 input processing. 114553541Sshin */ 114653541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 114762587Sitojun struct ip6_hbh *hbh; 114878064Sume int hbhlen = 0; 114978064Sume#ifdef PULLDOWN_TEST 115078064Sume struct mbuf *ext; 115178064Sume#endif 115253541Sshin 115362587Sitojun#ifndef PULLDOWN_TEST 115462587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 115562587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 115662587Sitojun#else 115778064Sume ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 115878064Sume ip6->ip6_nxt); 115978064Sume if (ext == NULL) { 116062587Sitojun ip6stat.ip6s_tooshort++; 116162587Sitojun return; 116262587Sitojun } 116378064Sume hbh = mtod(ext, struct ip6_hbh *); 116462587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 116578064Sume if (hbhlen != ext->m_len) { 116678064Sume m_freem(ext); 116762587Sitojun ip6stat.ip6s_tooshort++; 116862587Sitojun return; 116962587Sitojun } 117062587Sitojun#endif 117162587Sitojun 117253541Sshin /* 1173120913Sume * XXX: We copy the whole header even if a 1174120913Sume * jumbo payload option is included, the option which 1175120913Sume * is to be removed before returning according to 1176120913Sume * RFC2292. 1177148169Sume * Note: this constraint is removed in RFC3542 117853541Sshin */ 117962587Sitojun *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1180121472Sume IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1181121472Sume IPPROTO_IPV6); 1182121472Sume if (*mp) 118353541Sshin mp = &(*mp)->m_next; 118478064Sume#ifdef PULLDOWN_TEST 118578064Sume m_freem(ext); 118678064Sume#endif 118753541Sshin } 118853541Sshin } 118953541Sshin 1190121472Sume if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 119178064Sume int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 119253541Sshin 119353541Sshin /* 119453541Sshin * Search for destination options headers or routing 119553541Sshin * header(s) through the header chain, and stores each 119653541Sshin * header as ancillary data. 119753541Sshin * Note that the order of the headers remains in 119853541Sshin * the chain of ancillary data. 119953541Sshin */ 120078064Sume while (1) { /* is explicit loop prevention necessary? */ 120178064Sume struct ip6_ext *ip6e = NULL; 120262587Sitojun int elen; 120378064Sume#ifdef PULLDOWN_TEST 120478064Sume struct mbuf *ext = NULL; 120578064Sume#endif 120653541Sshin 120778064Sume /* 120878064Sume * if it is not an extension header, don't try to 120978064Sume * pull it from the chain. 121078064Sume */ 121178064Sume switch (nxt) { 121278064Sume case IPPROTO_DSTOPTS: 121378064Sume case IPPROTO_ROUTING: 121478064Sume case IPPROTO_HOPOPTS: 121578064Sume case IPPROTO_AH: /* is it possible? */ 121678064Sume break; 121778064Sume default: 121878064Sume goto loopend; 121978064Sume } 122078064Sume 122162587Sitojun#ifndef PULLDOWN_TEST 122278064Sume if (off + sizeof(*ip6e) > m->m_len) 122378064Sume goto loopend; 122462587Sitojun ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 122562587Sitojun if (nxt == IPPROTO_AH) 122662587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 122762587Sitojun else 122862587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 122978064Sume if (off + elen > m->m_len) 123078064Sume goto loopend; 123162587Sitojun#else 123278064Sume ext = ip6_pullexthdr(m, off, nxt); 123378064Sume if (ext == NULL) { 123462587Sitojun ip6stat.ip6s_tooshort++; 123562587Sitojun return; 123662587Sitojun } 123778064Sume ip6e = mtod(ext, struct ip6_ext *); 123862587Sitojun if (nxt == IPPROTO_AH) 123962587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 124062587Sitojun else 124162587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 124278064Sume if (elen != ext->m_len) { 124378064Sume m_freem(ext); 124462587Sitojun ip6stat.ip6s_tooshort++; 124562587Sitojun return; 124662587Sitojun } 124762587Sitojun#endif 124862587Sitojun 124978064Sume switch (nxt) { 125078064Sume case IPPROTO_DSTOPTS: 1251120913Sume if (!(in6p->in6p_flags & IN6P_DSTOPTS)) 125278064Sume break; 125353541Sshin 125478064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1255121472Sume IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1256120913Sume IPPROTO_IPV6); 125778064Sume if (*mp) 125878064Sume mp = &(*mp)->m_next; 125978064Sume break; 126078064Sume case IPPROTO_ROUTING: 126178064Sume if (!in6p->in6p_flags & IN6P_RTHDR) 126278064Sume break; 126353541Sshin 126478064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1265121472Sume IS2292(IPV6_2292RTHDR, IPV6_RTHDR), 1266120913Sume IPPROTO_IPV6); 126778064Sume if (*mp) 126878064Sume mp = &(*mp)->m_next; 126978064Sume break; 127078064Sume case IPPROTO_HOPOPTS: 127178064Sume case IPPROTO_AH: /* is it possible? */ 127278064Sume break; 127353541Sshin 127478064Sume default: 127578064Sume /* 127678064Sume * other cases have been filtered in the above. 127778064Sume * none will visit this case. here we supply 127878064Sume * the code just in case (nxt overwritten or 127978064Sume * other cases). 128078064Sume */ 128178064Sume#ifdef PULLDOWN_TEST 128278064Sume m_freem(ext); 128378064Sume#endif 128478064Sume goto loopend; 128553541Sshin 128653541Sshin } 128753541Sshin 128853541Sshin /* proceed with the next header. */ 128962587Sitojun off += elen; 129053541Sshin nxt = ip6e->ip6e_nxt; 129178064Sume ip6e = NULL; 129278064Sume#ifdef PULLDOWN_TEST 129378064Sume m_freem(ext); 129478064Sume ext = NULL; 129578064Sume#endif 129653541Sshin } 129753541Sshin loopend: 129878064Sume ; 129953541Sshin } 130078064Sume 1301121472Sume#undef IS2292 130278064Sume} 130378064Sume 1304125776Sumevoid 1305125776Sumeip6_notify_pmtu(in6p, dst, mtu) 1306125776Sume struct inpcb *in6p; 1307125776Sume struct sockaddr_in6 *dst; 1308125776Sume u_int32_t *mtu; 1309125776Sume{ 1310125776Sume struct socket *so; 1311125776Sume struct mbuf *m_mtu; 1312125776Sume struct ip6_mtuinfo mtuctl; 1313125776Sume 1314125776Sume so = in6p->inp_socket; 1315125776Sume 1316125776Sume if (mtu == NULL) 1317125776Sume return; 1318125776Sume 1319125776Sume#ifdef DIAGNOSTIC 1320125776Sume if (so == NULL) /* I believe this is impossible */ 1321125776Sume panic("ip6_notify_pmtu: socket is NULL"); 1322125776Sume#endif 1323125776Sume 1324125776Sume bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */ 1325125776Sume mtuctl.ip6m_mtu = *mtu; 1326125776Sume mtuctl.ip6m_addr = *dst; 1327148385Sume if (sa6_recoverscope(&mtuctl.ip6m_addr)) 1328148385Sume return; 1329125776Sume 1330125776Sume if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1331125776Sume IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1332125776Sume return; 1333125776Sume 1334125776Sume if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1335125776Sume == 0) { 1336125776Sume m_freem(m_mtu); 1337125776Sume /* XXX: should count statistics */ 1338125776Sume } else 1339125776Sume sorwakeup(so); 1340125776Sume 1341125776Sume return; 1342125776Sume} 1343125776Sume 134478064Sume#ifdef PULLDOWN_TEST 134578064Sume/* 134678064Sume * pull single extension header from mbuf chain. returns single mbuf that 134778064Sume * contains the result, or NULL on error. 134878064Sume */ 134978064Sumestatic struct mbuf * 135078064Sumeip6_pullexthdr(m, off, nxt) 135178064Sume struct mbuf *m; 135278064Sume size_t off; 135378064Sume int nxt; 135478064Sume{ 135578064Sume struct ip6_ext ip6e; 135678064Sume size_t elen; 135778064Sume struct mbuf *n; 135878064Sume 135978064Sume#ifdef DIAGNOSTIC 136078064Sume switch (nxt) { 136178064Sume case IPPROTO_DSTOPTS: 136278064Sume case IPPROTO_ROUTING: 136378064Sume case IPPROTO_HOPOPTS: 136478064Sume case IPPROTO_AH: /* is it possible? */ 136578064Sume break; 136678064Sume default: 136778064Sume printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 136853541Sshin } 136978064Sume#endif 137078064Sume 137178064Sume m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 137278064Sume if (nxt == IPPROTO_AH) 137378064Sume elen = (ip6e.ip6e_len + 2) << 2; 137478064Sume else 137578064Sume elen = (ip6e.ip6e_len + 1) << 3; 137678064Sume 1377111119Simp MGET(n, M_DONTWAIT, MT_DATA); 137878064Sume if (n && elen >= MLEN) { 1379111119Simp MCLGET(n, M_DONTWAIT); 138078064Sume if ((n->m_flags & M_EXT) == 0) { 138178064Sume m_free(n); 138278064Sume n = NULL; 138378064Sume } 138453541Sshin } 138578064Sume if (!n) 138678064Sume return NULL; 138762587Sitojun 138878064Sume n->m_len = 0; 138978064Sume if (elen >= M_TRAILINGSPACE(n)) { 139078064Sume m_free(n); 139178064Sume return NULL; 139278064Sume } 139378064Sume 139478064Sume m_copydata(m, off, elen, mtod(n, caddr_t)); 139578064Sume n->m_len = elen; 139678064Sume return n; 139753541Sshin} 139878064Sume#endif 139953541Sshin 140053541Sshin/* 140153541Sshin * Get pointer to the previous header followed by the header 140253541Sshin * currently processed. 140353541Sshin * XXX: This function supposes that 140453541Sshin * M includes all headers, 140553541Sshin * the next header field and the header length field of each header 140653541Sshin * are valid, and 140753541Sshin * the sum of each header length equals to OFF. 140853541Sshin * Because of these assumptions, this function must be called very 140953541Sshin * carefully. Moreover, it will not be used in the near future when 141053541Sshin * we develop `neater' mechanism to process extension headers. 141153541Sshin */ 141253541Sshinchar * 141353541Sshinip6_get_prevhdr(m, off) 141453541Sshin struct mbuf *m; 141553541Sshin int off; 141653541Sshin{ 141753541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 141853541Sshin 141953541Sshin if (off == sizeof(struct ip6_hdr)) 1420120856Sume return (&ip6->ip6_nxt); 142153541Sshin else { 142253541Sshin int len, nxt; 142353541Sshin struct ip6_ext *ip6e = NULL; 142453541Sshin 142553541Sshin nxt = ip6->ip6_nxt; 142653541Sshin len = sizeof(struct ip6_hdr); 142753541Sshin while (len < off) { 142853541Sshin ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 142953541Sshin 143078064Sume switch (nxt) { 143153541Sshin case IPPROTO_FRAGMENT: 143253541Sshin len += sizeof(struct ip6_frag); 143353541Sshin break; 143453541Sshin case IPPROTO_AH: 143553541Sshin len += (ip6e->ip6e_len + 2) << 2; 143653541Sshin break; 143753541Sshin default: 143853541Sshin len += (ip6e->ip6e_len + 1) << 3; 143953541Sshin break; 144053541Sshin } 144153541Sshin nxt = ip6e->ip6e_nxt; 144253541Sshin } 144353541Sshin if (ip6e) 1444120856Sume return (&ip6e->ip6e_nxt); 144553541Sshin else 144653541Sshin return NULL; 144753541Sshin } 144853541Sshin} 144953541Sshin 145053541Sshin/* 145162587Sitojun * get next header offset. m will be retained. 145262587Sitojun */ 145362587Sitojunint 145462587Sitojunip6_nexthdr(m, off, proto, nxtp) 145562587Sitojun struct mbuf *m; 145662587Sitojun int off; 145762587Sitojun int proto; 145862587Sitojun int *nxtp; 145962587Sitojun{ 146062587Sitojun struct ip6_hdr ip6; 146162587Sitojun struct ip6_ext ip6e; 146262587Sitojun struct ip6_frag fh; 146362587Sitojun 146462587Sitojun /* just in case */ 146562587Sitojun if (m == NULL) 146662587Sitojun panic("ip6_nexthdr: m == NULL"); 146762587Sitojun if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 146862587Sitojun return -1; 146962587Sitojun 147062587Sitojun switch (proto) { 147162587Sitojun case IPPROTO_IPV6: 147262587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6)) 147362587Sitojun return -1; 147462587Sitojun m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 147562587Sitojun if (nxtp) 147662587Sitojun *nxtp = ip6.ip6_nxt; 147762587Sitojun off += sizeof(ip6); 147862587Sitojun return off; 147962587Sitojun 148062587Sitojun case IPPROTO_FRAGMENT: 148162587Sitojun /* 148262587Sitojun * terminate parsing if it is not the first fragment, 148362587Sitojun * it does not make sense to parse through it. 148462587Sitojun */ 148562587Sitojun if (m->m_pkthdr.len < off + sizeof(fh)) 148662587Sitojun return -1; 148762587Sitojun m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1488120978Sume /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1489120978Sume if (fh.ip6f_offlg & IP6F_OFF_MASK) 149062587Sitojun return -1; 149162587Sitojun if (nxtp) 149262587Sitojun *nxtp = fh.ip6f_nxt; 149362587Sitojun off += sizeof(struct ip6_frag); 149462587Sitojun return off; 149562587Sitojun 149662587Sitojun case IPPROTO_AH: 149762587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6e)) 149862587Sitojun return -1; 149962587Sitojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 150062587Sitojun if (nxtp) 150162587Sitojun *nxtp = ip6e.ip6e_nxt; 150262587Sitojun off += (ip6e.ip6e_len + 2) << 2; 150362587Sitojun return off; 150462587Sitojun 150562587Sitojun case IPPROTO_HOPOPTS: 150662587Sitojun case IPPROTO_ROUTING: 150762587Sitojun case IPPROTO_DSTOPTS: 150862587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6e)) 150962587Sitojun return -1; 151062587Sitojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 151162587Sitojun if (nxtp) 151262587Sitojun *nxtp = ip6e.ip6e_nxt; 151362587Sitojun off += (ip6e.ip6e_len + 1) << 3; 151462587Sitojun return off; 151562587Sitojun 151662587Sitojun case IPPROTO_NONE: 151762587Sitojun case IPPROTO_ESP: 151862587Sitojun case IPPROTO_IPCOMP: 151962587Sitojun /* give up */ 152062587Sitojun return -1; 152162587Sitojun 152262587Sitojun default: 152362587Sitojun return -1; 152462587Sitojun } 152562587Sitojun 152662587Sitojun return -1; 152762587Sitojun} 152862587Sitojun 152962587Sitojun/* 153062587Sitojun * get offset for the last header in the chain. m will be kept untainted. 153162587Sitojun */ 153262587Sitojunint 153362587Sitojunip6_lasthdr(m, off, proto, nxtp) 153462587Sitojun struct mbuf *m; 153562587Sitojun int off; 153662587Sitojun int proto; 153762587Sitojun int *nxtp; 153862587Sitojun{ 153962587Sitojun int newoff; 154062587Sitojun int nxt; 154162587Sitojun 154262587Sitojun if (!nxtp) { 154362587Sitojun nxt = -1; 154462587Sitojun nxtp = &nxt; 154562587Sitojun } 154662587Sitojun while (1) { 154762587Sitojun newoff = ip6_nexthdr(m, off, proto, nxtp); 154862587Sitojun if (newoff < 0) 154962587Sitojun return off; 155062587Sitojun else if (newoff < off) 155162587Sitojun return -1; /* invalid */ 155262587Sitojun else if (newoff == off) 155362587Sitojun return newoff; 155462587Sitojun 155562587Sitojun off = newoff; 155662587Sitojun proto = *nxtp; 155762587Sitojun } 155862587Sitojun} 155962587Sitojun 1560121673Sumestruct ip6aux * 156178064Sumeip6_addaux(m) 156278064Sume struct mbuf *m; 156378064Sume{ 1564120913Sume struct m_tag *mtag; 1565120913Sume 1566120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1567120913Sume if (!mtag) { 1568120913Sume mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux), 1569120913Sume M_NOWAIT); 1570121630Sume if (mtag) { 1571120913Sume m_tag_prepend(m, mtag); 1572121630Sume bzero(mtag + 1, sizeof(struct ip6aux)); 1573121630Sume } 157478064Sume } 1575121673Sume return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 157678064Sume} 157778064Sume 1578121673Sumestruct ip6aux * 157978064Sumeip6_findaux(m) 158078064Sume struct mbuf *m; 158178064Sume{ 1582120913Sume struct m_tag *mtag; 1583120913Sume 1584120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1585121673Sume return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 158678064Sume} 158778064Sume 158878064Sumevoid 158978064Sumeip6_delaux(m) 159078064Sume struct mbuf *m; 159178064Sume{ 1592120913Sume struct m_tag *mtag; 1593120913Sume 1594120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1595120913Sume if (mtag) 1596120913Sume m_tag_delete(m, mtag); 159778064Sume} 159878064Sume 159962587Sitojun/* 160053541Sshin * System control for IP6 160153541Sshin */ 160253541Sshin 160353541Sshinu_char inet6ctlerrmap[PRC_NCMDS] = { 160453541Sshin 0, 0, 0, 0, 160553541Sshin 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 160653541Sshin EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 160753541Sshin EMSGSIZE, EHOSTUNREACH, 0, 0, 160853541Sshin 0, 0, 0, 0, 160953541Sshin ENOPROTOOPT 161053541Sshin}; 1611