ip6_input.c revision 126508
162587Sitojun/* $FreeBSD: head/sys/netinet6/ip6_input.c 126508 2004-03-02 20:29:55Z mlaier $ */ 295023Ssuz/* $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei 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" 73120649Sume#include "opt_random_ip_id.h" 7455009Sshin 7553541Sshin#include <sys/param.h> 7653541Sshin#include <sys/systm.h> 7778064Sume#include <sys/malloc.h> 7853541Sshin#include <sys/mbuf.h> 7983366Sjulian#include <sys/proc.h> 8053541Sshin#include <sys/domain.h> 8153541Sshin#include <sys/protosw.h> 8253541Sshin#include <sys/socket.h> 8353541Sshin#include <sys/socketvar.h> 8453541Sshin#include <sys/errno.h> 8553541Sshin#include <sys/time.h> 8653541Sshin#include <sys/kernel.h> 8753541Sshin#include <sys/syslog.h> 8853541Sshin 8953541Sshin#include <net/if.h> 9053541Sshin#include <net/if_types.h> 9153541Sshin#include <net/if_dl.h> 9253541Sshin#include <net/route.h> 9353541Sshin#include <net/netisr.h> 9464060Sdarrenr#ifdef PFIL_HOOKS 9564060Sdarrenr#include <net/pfil.h> 9664060Sdarrenr#endif 9753541Sshin 9853541Sshin#include <netinet/in.h> 9953541Sshin#include <netinet/in_systm.h> 10062587Sitojun#ifdef INET 10153541Sshin#include <netinet/ip.h> 10253541Sshin#include <netinet/ip_icmp.h> 10395023Ssuz#endif /* INET */ 10462587Sitojun#include <netinet/ip6.h> 10553541Sshin#include <netinet6/in6_var.h> 10653541Sshin#include <netinet6/ip6_var.h> 10762587Sitojun#include <netinet/in_pcb.h> 10862587Sitojun#include <netinet/icmp6.h> 109121161Sume#include <netinet6/scope6_var.h> 11053541Sshin#include <netinet6/in6_ifattach.h> 11153541Sshin#include <netinet6/nd6.h> 11253541Sshin#include <netinet6/in6_prefix.h> 11353541Sshin 11478064Sume#ifdef IPSEC 11578064Sume#include <netinet6/ipsec.h> 11678064Sume#ifdef INET6 11778064Sume#include <netinet6/ipsec6.h> 11878064Sume#endif 11978064Sume#endif 12078064Sume 121105199Ssam#ifdef FAST_IPSEC 122105199Ssam#include <netipsec/ipsec.h> 123105199Ssam#include <netipsec/ipsec6.h> 124105199Ssam#define IPSEC 125105199Ssam#endif /* FAST_IPSEC */ 126105199Ssam 12753541Sshin#include <netinet6/ip6_fw.h> 12853541Sshin 12953541Sshin#include <netinet6/ip6protosw.h> 13053541Sshin 13153541Sshin#include <net/net_osdep.h> 13253541Sshin 13362587Sitojunextern struct domain inet6domain; 13453541Sshin 13562587Sitojunu_char ip6_protox[IPPROTO_MAX]; 136111888Sjlemonstatic struct ifqueue ip6intrq; 13762587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN; 13862587Sitojunstruct in6_ifaddr *in6_ifaddr; 13953541Sshin 14078064Sumeextern struct callout in6_tmpaddrtimer_ch; 14178064Sume 14262587Sitojunint ip6_forward_srcrt; /* XXX */ 14362587Sitojunint ip6_sourcecheck; /* XXX */ 14462587Sitojunint ip6_sourcecheck_interval; /* XXX */ 14553541Sshin 14678064Sumeint ip6_ours_check_algorithm; 14778064Sume 148120386Ssam#ifdef PFIL_HOOKS 149120386Ssamstruct pfil_head inet6_pfil_hook; 150120386Ssam#endif 15178064Sume 15253541Sshin/* firewall hooks */ 15362587Sitojunip6_fw_chk_t *ip6_fw_chk_ptr; 15462587Sitojunip6_fw_ctl_t *ip6_fw_ctl_ptr; 15566303Sumeint ip6_fw_enable = 1; 15653541Sshin 15762587Sitojunstruct ip6stat ip6stat; 15853541Sshin 15962587Sitojunstatic void ip6_init2 __P((void *)); 160121673Sumestatic struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *)); 16162587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 16262587Sitojun#ifdef PULLDOWN_TEST 16362587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int)); 16453541Sshin#endif 16553541Sshin 16653541Sshin/* 16753541Sshin * IP6 initialization: fill in IP6 protocol switch table. 16853541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler. 16953541Sshin */ 17053541Sshinvoid 17153541Sshinip6_init() 17253541Sshin{ 17378064Sume struct ip6protosw *pr; 17478064Sume int i; 17553541Sshin 17678064Sume#ifdef DIAGNOSTIC 17778064Sume if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 17878064Sume panic("sizeof(protosw) != sizeof(ip6protosw)"); 17978064Sume#endif 18053541Sshin pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 18153541Sshin if (pr == 0) 18253541Sshin panic("ip6_init"); 18353541Sshin for (i = 0; i < IPPROTO_MAX; i++) 18453541Sshin ip6_protox[i] = pr - inet6sw; 18553541Sshin for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 18653541Sshin pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 18753541Sshin if (pr->pr_domain->dom_family == PF_INET6 && 18853541Sshin pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 18953541Sshin ip6_protox[pr->pr_protocol] = pr - inet6sw; 190120386Ssam#ifdef PFIL_HOOKS 191120386Ssam inet6_pfil_hook.ph_type = PFIL_TYPE_AF; 192120386Ssam inet6_pfil_hook.ph_af = AF_INET6; 193120386Ssam if ((i = pfil_head_register(&inet6_pfil_hook)) != 0) 194120386Ssam printf("%s: WARNING: unable to register pfil hook, " 195120386Ssam "error %d\n", __func__, i); 196120386Ssam#endif /* PFIL_HOOKS */ 19753541Sshin ip6intrq.ifq_maxlen = ip6qmaxlen; 19893818Sjhb mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF); 199122320Ssam netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0); 200121161Sume scope6_init(); 201121742Sume addrsel_policy_init(); 20253541Sshin nd6_init(); 20353541Sshin frag6_init(); 204120649Sume#ifndef RANDOM_IP_ID 205120648Sume ip6_flow_seq = arc4random(); 206120649Sume#endif 207120648Sume ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR; 20853541Sshin} 20953541Sshin 21053541Sshinstatic void 21153541Sshinip6_init2(dummy) 21253541Sshin void *dummy; 21353541Sshin{ 21453541Sshin 21553541Sshin /* nd6_timer_init */ 21678064Sume callout_init(&nd6_timer_ch, 0); 21778064Sume callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 21878064Sume 21953541Sshin /* router renumbering prefix list maintenance */ 22078064Sume callout_init(&in6_rr_timer_ch, 0); 22178064Sume callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL); 22278064Sume 22378064Sume /* timer for regeneranation of temporary addresses randomize ID */ 224121806Sume callout_init(&in6_tmpaddrtimer_ch, 0); 22578064Sume callout_reset(&in6_tmpaddrtimer_ch, 22678064Sume (ip6_temp_preferred_lifetime - ip6_desync_factor - 22778064Sume ip6_temp_regen_advance) * hz, 22878064Sume in6_tmpaddrtimer, NULL); 22953541Sshin} 23053541Sshin 23153541Sshin/* cheat */ 23255009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */ 23355009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 23453541Sshin 23553541Sshinextern struct route_in6 ip6_forward_rt; 23653541Sshin 23753541Sshinvoid 23853541Sshinip6_input(m) 23953541Sshin struct mbuf *m; 24053541Sshin{ 24153541Sshin struct ip6_hdr *ip6; 24253541Sshin int off = sizeof(struct ip6_hdr), nest; 24353541Sshin u_int32_t plen; 24453541Sshin u_int32_t rtalert = ~0; 24553541Sshin int nxt, ours = 0; 24653541Sshin struct ifnet *deliverifp = NULL; 247126444Sume struct sockaddr_in6 sa6; 248126444Sume u_int32_t srczone, dstzone; 249121143Ssam#ifdef PFIL_HOOKS 250121143Ssam struct in6_addr odst; 251121143Ssam#endif 252121143Ssam int srcrt = 0; 253121144Ssam 254122320Ssam GIANT_REQUIRED; /* XXX for now */ 25553541Sshin#ifdef IPSEC 25653541Sshin /* 25753541Sshin * should the inner packet be considered authentic? 25853541Sshin * see comment in ah4_input(). 25953541Sshin */ 26053541Sshin if (m) { 26153541Sshin m->m_flags &= ~M_AUTHIPHDR; 26253541Sshin m->m_flags &= ~M_AUTHIPDGM; 26353541Sshin } 26453541Sshin#endif 26553541Sshin 26653541Sshin /* 267121630Sume * make sure we don't have onion peering information into m_tag. 26878064Sume */ 26978064Sume ip6_delaux(m); 27078064Sume 27178064Sume /* 27295023Ssuz * mbuf statistics 27353541Sshin */ 27453541Sshin if (m->m_flags & M_EXT) { 27553541Sshin if (m->m_next) 27653541Sshin ip6stat.ip6s_mext2m++; 27753541Sshin else 27853541Sshin ip6stat.ip6s_mext1++; 27953541Sshin } else { 28078064Sume#define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0])) 28153541Sshin if (m->m_next) { 28253541Sshin if (m->m_flags & M_LOOP) { 283120913Sume ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */ 28478064Sume } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 28553541Sshin ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 28653541Sshin else 28753541Sshin ip6stat.ip6s_m2m[0]++; 28853541Sshin } else 28953541Sshin ip6stat.ip6s_m1++; 29078064Sume#undef M2MMAX 29153541Sshin } 29253541Sshin 29353541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 29453541Sshin ip6stat.ip6s_total++; 29553541Sshin 29662587Sitojun#ifndef PULLDOWN_TEST 29774336Skuriyama /* 29874336Skuriyama * L2 bridge code and some other code can return mbuf chain 29974336Skuriyama * that does not conform to KAME requirement. too bad. 30074336Skuriyama * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 30174336Skuriyama */ 30274336Skuriyama if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 30374336Skuriyama struct mbuf *n; 30474336Skuriyama 305111119Simp MGETHDR(n, M_DONTWAIT, MT_HEADER); 30677003Sume if (n) 307108466Ssam M_MOVE_PKTHDR(n, m); 308108825Ssam if (n && n->m_pkthdr.len > MHLEN) { 309111119Simp MCLGET(n, M_DONTWAIT); 31074336Skuriyama if ((n->m_flags & M_EXT) == 0) { 31174336Skuriyama m_freem(n); 31274336Skuriyama n = NULL; 31374336Skuriyama } 31474336Skuriyama } 31595023Ssuz if (n == NULL) { 31677003Sume m_freem(m); 317120913Sume return; /* ENOBUFS */ 31877003Sume } 31974336Skuriyama 320108825Ssam m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 321108825Ssam n->m_len = n->m_pkthdr.len; 32274336Skuriyama m_freem(m); 32374336Skuriyama m = n; 32474336Skuriyama } 325120913Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */); 32662587Sitojun#endif 32753541Sshin 32853541Sshin if (m->m_len < sizeof(struct ip6_hdr)) { 32953541Sshin struct ifnet *inifp; 33053541Sshin inifp = m->m_pkthdr.rcvif; 331120913Sume if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 33253541Sshin ip6stat.ip6s_toosmall++; 33353541Sshin in6_ifstat_inc(inifp, ifs6_in_hdrerr); 33453541Sshin return; 33553541Sshin } 33653541Sshin } 33753541Sshin 33853541Sshin ip6 = mtod(m, struct ip6_hdr *); 33953541Sshin 34053541Sshin if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 34153541Sshin ip6stat.ip6s_badvers++; 34253541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 34353541Sshin goto bad; 34453541Sshin } 34553541Sshin 34653541Sshin ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 34753541Sshin 34853541Sshin /* 34978064Sume * Check against address spoofing/corruption. 35053541Sshin */ 35153541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 35253541Sshin IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 35378064Sume /* 35478064Sume * XXX: "badscope" is not very suitable for a multicast source. 35578064Sume */ 35653541Sshin ip6stat.ip6s_badscope++; 35753541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 35853541Sshin goto bad; 35953541Sshin } 360126444Sume if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 361126444Sume !(m->m_flags & M_LOOP)) { 362126444Sume /* 363126444Sume * In this case, the packet should come from the loopback 364126444Sume * interface. However, we cannot just check the if_flags, 365126444Sume * because ip6_mloopback() passes the "actual" interface 366126444Sume * as the outgoing/incoming interface. 367126444Sume */ 36878064Sume ip6stat.ip6s_badscope++; 36978064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 37078064Sume goto bad; 37178064Sume } 37295023Ssuz 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 /* 408126444Sume * Drop packets if the link ID portion is already filled. 409126444Sume * XXX: this is technically not a good behavior. But, we internally 410126444Sume * use the field to disambiguate link-local addresses, so we cannot 411126444Sume * be generous against those a bit strange addresses. 412126444Sume */ 41378064Sume if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 41478064Sume if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 41578064Sume ip6->ip6_src.s6_addr16[1]) { 41653541Sshin ip6stat.ip6s_badscope++; 41753541Sshin goto bad; 41853541Sshin } 419121315Sume if ((IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) || 420121315Sume IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) && 42178064Sume ip6->ip6_dst.s6_addr16[1]) { 42278064Sume ip6stat.ip6s_badscope++; 42378064Sume goto bad; 42478064Sume } 42553541Sshin } 42653541Sshin 427126508Smlaier#ifdef PFIL_HOOKS 428126444Sume /* 429126508Smlaier * Run through list of hooks for input packets. 430126508Smlaier * 431126508Smlaier * NB: Beware of the destination address changing 432126508Smlaier * (e.g. by NAT rewriting). When this happens, 433126508Smlaier * tell ip6_forward to do the right thing. 434126508Smlaier */ 435126508Smlaier odst = ip6->ip6_dst; 436126508Smlaier if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN)) 437126508Smlaier return; 438126508Smlaier if (m == NULL) /* consumed by filter */ 439126508Smlaier return; 440126508Smlaier ip6 = mtod(m, struct ip6_hdr *); 441126508Smlaier srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 442126508Smlaier#endif /* PFIL_HOOKS */ 443126508Smlaier 444126508Smlaier /* 445126508Smlaier * Check with the firewall... 446126508Smlaier */ 447126508Smlaier if (ip6_fw_enable && ip6_fw_chk_ptr) { 448126508Smlaier u_short port = 0; 449126508Smlaier /* If ipfw says divert, we have to just drop packet */ 450126508Smlaier /* use port as a dummy argument */ 451126508Smlaier if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 452126508Smlaier m_freem(m); 453126508Smlaier m = NULL; 454126508Smlaier } 455126508Smlaier if (!m) 456126508Smlaier return; 457126508Smlaier } 458126508Smlaier 459126508Smlaier /* 460126444Sume * construct source and destination address structures with 461126444Sume * disambiguating their scope zones (if there is ambiguity). 462126444Sume * XXX: sin6_family and sin6_len will NOT be referred to, but we fill 463126444Sume * in these fields just in case. 464126444Sume */ 465126444Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_src, &srczone) || 466126444Sume in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_dst, &dstzone)) { 467126444Sume /* 468126444Sume * Note that these generic checks cover cases that src or 469126444Sume * dst are the loopback address and the receiving interface 470126444Sume * is not loopback. 471126444Sume */ 472126444Sume ip6stat.ip6s_badscope++; 473126444Sume goto bad; 474126444Sume } 47553541Sshin 476126444Sume bzero(&sa6, sizeof(sa6)); 477126444Sume sa6.sin6_family = AF_INET6; 478126444Sume sa6.sin6_len = sizeof(struct sockaddr_in6); 479126444Sume 480126444Sume sa6.sin6_addr = ip6->ip6_src; 481126444Sume sa6.sin6_scope_id = srczone; 482126444Sume if (in6_embedscope(&ip6->ip6_src, &sa6, NULL, NULL)) { 483126444Sume /* XXX: should not happen */ 484126444Sume ip6stat.ip6s_badscope++; 485126444Sume goto bad; 486126444Sume } 487126444Sume 488126444Sume sa6.sin6_addr = ip6->ip6_dst; 489126444Sume sa6.sin6_scope_id = dstzone; 490126444Sume if (in6_embedscope(&ip6->ip6_dst, &sa6, NULL, NULL)) { 491126444Sume /* XXX: should not happen */ 492126444Sume ip6stat.ip6s_badscope++; 493126444Sume goto bad; 494126444Sume } 495126444Sume 496126489Sume /* XXX: ff01::%ifN awareness is not merged, yet. */ 497126489Sume if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_src)) 498126489Sume ip6->ip6_src.s6_addr16[1] = 0; 499126489Sume if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) 500126489Sume ip6->ip6_dst.s6_addr16[1] = 0; 501126489Sume 50253541Sshin /* 50353541Sshin * Multicast check 50453541Sshin */ 50553541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 506120913Sume struct in6_multi *in6m = 0; 50753541Sshin 50853541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 50953541Sshin /* 51053541Sshin * See if we belong to the destination multicast group on the 51153541Sshin * arrival interface. 51253541Sshin */ 51353541Sshin IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 51453541Sshin if (in6m) 51553541Sshin ours = 1; 51656723Sshin else if (!ip6_mrouter) { 51753541Sshin ip6stat.ip6s_notmember++; 51853541Sshin ip6stat.ip6s_cantforward++; 51953541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 52053541Sshin goto bad; 52153541Sshin } 52253541Sshin deliverifp = m->m_pkthdr.rcvif; 52353541Sshin goto hbhcheck; 52453541Sshin } 52553541Sshin 52653541Sshin /* 52753541Sshin * Unicast check 52853541Sshin */ 52962587Sitojun if (ip6_forward_rt.ro_rt != NULL && 530120913Sume (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 53162587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 532120913Sume &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 53362587Sitojun ip6stat.ip6s_forward_cachehit++; 53462587Sitojun else { 53578064Sume struct sockaddr_in6 *dst6; 53678064Sume 53753541Sshin if (ip6_forward_rt.ro_rt) { 53862587Sitojun /* route is down or destination is different */ 53962587Sitojun ip6stat.ip6s_forward_cachemiss++; 54053541Sshin RTFREE(ip6_forward_rt.ro_rt); 54153541Sshin ip6_forward_rt.ro_rt = 0; 54253541Sshin } 54362587Sitojun 54453541Sshin bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 54578064Sume dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 54678064Sume dst6->sin6_len = sizeof(struct sockaddr_in6); 54778064Sume dst6->sin6_family = AF_INET6; 54878064Sume dst6->sin6_addr = ip6->ip6_dst; 54953541Sshin 550122921Sandre rtalloc((struct route *)&ip6_forward_rt); 55153541Sshin } 55253541Sshin 55353541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 55453541Sshin 55553541Sshin /* 55653541Sshin * Accept the packet if the forwarding interface to the destination 55753541Sshin * according to the routing table is the loopback interface, 55853541Sshin * unless the associated route has a gateway. 55953541Sshin * Note that this approach causes to accept a packet if there is a 56053541Sshin * route to the loopback interface for the destination of the packet. 56153541Sshin * But we think it's even useful in some situations, e.g. when using 56253541Sshin * a special daemon which wants to intercept the packet. 56378064Sume * 56478064Sume * XXX: some OSes automatically make a cloned route for the destination 56578064Sume * of an outgoing packet. If the outgoing interface of the packet 56678064Sume * is a loopback one, the kernel would consider the packet to be 56778064Sume * accepted, even if we have no such address assinged on the interface. 56878064Sume * We check the cloned flag of the route entry to reject such cases, 56978064Sume * assuming that route entries for our own addresses are not made by 57078064Sume * cloning (it should be true because in6_addloop explicitly installs 57178064Sume * the host route). However, we might have to do an explicit check 57278064Sume * while it would be less efficient. Or, should we rather install a 57378064Sume * reject route for such a case? 57453541Sshin */ 57553541Sshin if (ip6_forward_rt.ro_rt && 57653541Sshin (ip6_forward_rt.ro_rt->rt_flags & 57753541Sshin (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 57878064Sume#ifdef RTF_WASCLONED 57978064Sume !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 58078064Sume#endif 58178064Sume#ifdef RTF_CLONED 58278064Sume !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 58378064Sume#endif 58462587Sitojun#if 0 58553541Sshin /* 58662587Sitojun * The check below is redundant since the comparison of 58762587Sitojun * the destination and the key of the rtentry has 58862587Sitojun * already done through looking up the routing table. 58953541Sshin */ 59062587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 591120913Sume &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 59262587Sitojun#endif 59353541Sshin ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 59453541Sshin struct in6_ifaddr *ia6 = 59553541Sshin (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 59678064Sume 59762587Sitojun /* 598121630Sume * record address information into m_tag. 59978064Sume */ 60078064Sume (void)ip6_setdstifaddr(m, ia6); 60178064Sume 60278064Sume /* 60362587Sitojun * packets to a tentative, duplicated, or somehow invalid 60462587Sitojun * address must not be accepted. 60562587Sitojun */ 60653541Sshin if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 60762587Sitojun /* this address is ready */ 60853541Sshin ours = 1; 60953541Sshin deliverifp = ia6->ia_ifp; /* correct? */ 61067334Sjoe /* Count the packet in the ip address stats */ 61167334Sjoe ia6->ia_ifa.if_ipackets++; 61267334Sjoe ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 61353541Sshin goto hbhcheck; 61453541Sshin } else { 61562587Sitojun /* address is not ready, so discard the packet. */ 61678064Sume nd6log((LOG_INFO, 61778064Sume "ip6_input: packet to an unready address %s->%s\n", 61862587Sitojun ip6_sprintf(&ip6->ip6_src), 61978064Sume ip6_sprintf(&ip6->ip6_dst))); 62062587Sitojun 62162587Sitojun goto bad; 62253541Sshin } 62353541Sshin } 62453541Sshin 62553541Sshin /* 626120913Sume * FAITH (Firewall Aided Internet Translator) 62753541Sshin */ 62853541Sshin if (ip6_keepfaith) { 62953541Sshin if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 63053541Sshin && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 63153541Sshin /* XXX do we need more sanity checks? */ 63253541Sshin ours = 1; 63395023Ssuz deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ 63453541Sshin goto hbhcheck; 63553541Sshin } 63653541Sshin } 63753541Sshin 63853541Sshin /* 63953541Sshin * Now there is no reason to process the packet if it's not our own 64053541Sshin * and we're not a router. 64153541Sshin */ 64253541Sshin if (!ip6_forwarding) { 64353541Sshin ip6stat.ip6s_cantforward++; 64453541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 64553541Sshin goto bad; 64653541Sshin } 64753541Sshin 64853541Sshin hbhcheck: 64953541Sshin /* 650121630Sume * record address information into m_tag, if we don't have one yet. 65178064Sume * note that we are unable to record it, if the address is not listed 65278064Sume * as our interface address (e.g. multicast addresses, addresses 65378064Sume * within FAITH prefixes and such). 65478064Sume */ 65578064Sume if (deliverifp && !ip6_getdstifaddr(m)) { 65678064Sume struct in6_ifaddr *ia6; 65778064Sume 65878064Sume ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 65978064Sume if (ia6) { 66078064Sume if (!ip6_setdstifaddr(m, ia6)) { 66178064Sume /* 66278064Sume * XXX maybe we should drop the packet here, 66378064Sume * as we could not provide enough information 66478064Sume * to the upper layers. 66578064Sume */ 66678064Sume } 66778064Sume } 66878064Sume } 66978064Sume 67078064Sume /* 67153541Sshin * Process Hop-by-Hop options header if it's contained. 67253541Sshin * m may be modified in ip6_hopopts_input(). 67353541Sshin * If a JumboPayload option is included, plen will also be modified. 67453541Sshin */ 67553541Sshin plen = (u_int32_t)ntohs(ip6->ip6_plen); 67653541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 67762587Sitojun struct ip6_hbh *hbh; 67862587Sitojun 67953541Sshin if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 68062587Sitojun#if 0 /*touches NULL pointer*/ 68153541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 68262587Sitojun#endif 68353541Sshin return; /* m have already been freed */ 68453541Sshin } 68562587Sitojun 68653541Sshin /* adjust pointer */ 68753541Sshin ip6 = mtod(m, struct ip6_hdr *); 68853541Sshin 68953541Sshin /* 69095023Ssuz * if the payload length field is 0 and the next header field 69162587Sitojun * indicates Hop-by-Hop Options header, then a Jumbo Payload 69262587Sitojun * option MUST be included. 69362587Sitojun */ 69462587Sitojun if (ip6->ip6_plen == 0 && plen == 0) { 69562587Sitojun /* 69662587Sitojun * Note that if a valid jumbo payload option is 697120913Sume * contained, ip6_hopopts_input() must set a valid 698120913Sume * (non-zero) payload length to the variable plen. 69962587Sitojun */ 70062587Sitojun ip6stat.ip6s_badoptions++; 70162587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 70262587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 70362587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 70462587Sitojun ICMP6_PARAMPROB_HEADER, 70562587Sitojun (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 70662587Sitojun return; 70762587Sitojun } 70862587Sitojun#ifndef PULLDOWN_TEST 70962587Sitojun /* ip6_hopopts_input() ensures that mbuf is contiguous */ 71062587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 71162587Sitojun#else 71262587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 71362587Sitojun sizeof(struct ip6_hbh)); 71462587Sitojun if (hbh == NULL) { 71562587Sitojun ip6stat.ip6s_tooshort++; 71662587Sitojun return; 71762587Sitojun } 71862587Sitojun#endif 71962587Sitojun nxt = hbh->ip6h_nxt; 72062587Sitojun 72162587Sitojun /* 72253541Sshin * accept the packet if a router alert option is included 72353541Sshin * and we act as an IPv6 router. 72453541Sshin */ 72553541Sshin if (rtalert != ~0 && ip6_forwarding) 72653541Sshin ours = 1; 72753541Sshin } else 72853541Sshin nxt = ip6->ip6_nxt; 72953541Sshin 73053541Sshin /* 73153541Sshin * Check that the amount of data in the buffers 73253541Sshin * is as at least much as the IPv6 header would have us expect. 73353541Sshin * Trim mbufs if longer than we expect. 73453541Sshin * Drop packet if shorter than we expect. 73553541Sshin */ 73653541Sshin if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 73753541Sshin ip6stat.ip6s_tooshort++; 73853541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 73953541Sshin goto bad; 74053541Sshin } 74153541Sshin if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 74253541Sshin if (m->m_len == m->m_pkthdr.len) { 74353541Sshin m->m_len = sizeof(struct ip6_hdr) + plen; 74453541Sshin m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 74553541Sshin } else 74653541Sshin m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 74753541Sshin } 74853541Sshin 74953541Sshin /* 75053541Sshin * Forward if desirable. 75153541Sshin */ 75253541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 75356723Sshin /* 75456723Sshin * If we are acting as a multicast router, all 75556723Sshin * incoming multicast packets are passed to the 75656723Sshin * kernel-level multicast forwarding function. 75756723Sshin * The packet is returned (relatively) intact; if 75856723Sshin * ip6_mforward() returns a non-zero value, the packet 75956723Sshin * must be discarded, else it may be accepted below. 76056723Sshin */ 76156723Sshin if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 76256723Sshin ip6stat.ip6s_cantforward++; 76356723Sshin m_freem(m); 76456723Sshin return; 76556723Sshin } 76653541Sshin if (!ours) { 76753541Sshin m_freem(m); 76853541Sshin return; 76953541Sshin } 77053541Sshin } else if (!ours) { 771121143Ssam ip6_forward(m, srcrt); 77253541Sshin return; 773121673Sume } 77453541Sshin 77562587Sitojun ip6 = mtod(m, struct ip6_hdr *); 77662587Sitojun 77753541Sshin /* 77862587Sitojun * Malicious party may be able to use IPv4 mapped addr to confuse 77962587Sitojun * tcp/udp stack and bypass security checks (act as if it was from 78062587Sitojun * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 78162587Sitojun * 78262587Sitojun * For SIIT end node behavior, you may want to disable the check. 78362587Sitojun * However, you will become vulnerable to attacks using IPv4 mapped 78462587Sitojun * source. 78562587Sitojun */ 78662587Sitojun if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 78762587Sitojun IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 78862587Sitojun ip6stat.ip6s_badscope++; 78962587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 79062587Sitojun goto bad; 79162587Sitojun } 79262587Sitojun 79362587Sitojun /* 79453541Sshin * Tell launch routine the next header 79553541Sshin */ 79653541Sshin ip6stat.ip6s_delivered++; 79753541Sshin in6_ifstat_inc(deliverifp, ifs6_in_deliver); 79853541Sshin nest = 0; 79978064Sume 80053541Sshin while (nxt != IPPROTO_DONE) { 80153541Sshin if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 80253541Sshin ip6stat.ip6s_toomanyhdr++; 80353541Sshin goto bad; 80453541Sshin } 80553541Sshin 80653541Sshin /* 80753541Sshin * protection against faulty packet - there should be 80853541Sshin * more sanity checks in header chain processing. 80953541Sshin */ 81053541Sshin if (m->m_pkthdr.len < off) { 81153541Sshin ip6stat.ip6s_tooshort++; 81253541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 81353541Sshin goto bad; 81453541Sshin } 81553541Sshin 81678064Sume#ifdef IPSEC 81778064Sume /* 81878064Sume * enforce IPsec policy checking if we are seeing last header. 81978064Sume * note that we do not visit this with protocols with pcb layer 82078064Sume * code - like udp/tcp/raw ip. 82178064Sume */ 82278064Sume if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 82378064Sume ipsec6_in_reject(m, NULL)) { 82478064Sume ipsec6stat.in_polvio++; 82578064Sume goto bad; 82678064Sume } 82778064Sume#endif 82853541Sshin nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 82953541Sshin } 83053541Sshin return; 83153541Sshin bad: 83253541Sshin m_freem(m); 83353541Sshin} 83453541Sshin 83553541Sshin/* 83678064Sume * set/grab in6_ifaddr correspond to IPv6 destination address. 83778064Sume * XXX backward compatibility wrapper 83878064Sume */ 839121673Sumestatic struct ip6aux * 84078064Sumeip6_setdstifaddr(m, ia6) 84178064Sume struct mbuf *m; 84278064Sume struct in6_ifaddr *ia6; 84378064Sume{ 844121673Sume struct ip6aux *ip6a; 84578064Sume 846121673Sume ip6a = ip6_addaux(m); 847121673Sume if (ip6a) 848121673Sume ip6a->ip6a_dstia6 = ia6; 849121673Sume return ip6a; /* NULL if failed to set */ 85078064Sume} 85178064Sume 85278064Sumestruct in6_ifaddr * 85378064Sumeip6_getdstifaddr(m) 85478064Sume struct mbuf *m; 85578064Sume{ 856121673Sume struct ip6aux *ip6a; 85778064Sume 858121673Sume ip6a = ip6_findaux(m); 859121673Sume if (ip6a) 860121673Sume return ip6a->ip6a_dstia6; 86178064Sume else 86278064Sume return NULL; 86378064Sume} 86478064Sume 86578064Sume/* 86653541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is 86753541Sshin * included, the real payload length will be stored in plenp. 86853541Sshin */ 86953541Sshinstatic int 87053541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp) 87153541Sshin u_int32_t *plenp; 87253541Sshin u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 87353541Sshin struct mbuf **mp; 87453541Sshin int *offp; 87553541Sshin{ 87678064Sume struct mbuf *m = *mp; 87753541Sshin int off = *offp, hbhlen; 87853541Sshin struct ip6_hbh *hbh; 87953541Sshin u_int8_t *opt; 88053541Sshin 88153541Sshin /* validation of the length of the header */ 88262587Sitojun#ifndef PULLDOWN_TEST 88353541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 88453541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 88553541Sshin hbhlen = (hbh->ip6h_len + 1) << 3; 88653541Sshin 88753541Sshin IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 88853541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 88962587Sitojun#else 89062587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 89162587Sitojun sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 89262587Sitojun if (hbh == NULL) { 89362587Sitojun ip6stat.ip6s_tooshort++; 89462587Sitojun return -1; 89562587Sitojun } 89662587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 89762587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 89862587Sitojun hbhlen); 89962587Sitojun if (hbh == NULL) { 90062587Sitojun ip6stat.ip6s_tooshort++; 90162587Sitojun return -1; 90262587Sitojun } 90362587Sitojun#endif 90453541Sshin off += hbhlen; 90553541Sshin hbhlen -= sizeof(struct ip6_hbh); 90653541Sshin opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 90753541Sshin 90853541Sshin if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 90953541Sshin hbhlen, rtalertp, plenp) < 0) 910120856Sume return (-1); 91153541Sshin 91253541Sshin *offp = off; 91353541Sshin *mp = m; 914120856Sume return (0); 91553541Sshin} 91653541Sshin 91753541Sshin/* 91853541Sshin * Search header for all Hop-by-hop options and process each option. 91953541Sshin * This function is separate from ip6_hopopts_input() in order to 92053541Sshin * handle a case where the sending node itself process its hop-by-hop 92153541Sshin * options header. In such a case, the function is called from ip6_output(). 92278064Sume * 92378064Sume * The function assumes that hbh header is located right after the IPv6 header 92478064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 92578064Sume * opthead + hbhlen is located in continuous memory region. 92653541Sshin */ 92753541Sshinint 92853541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 92953541Sshin struct mbuf *m; 93053541Sshin u_int8_t *opthead; 93153541Sshin int hbhlen; 93253541Sshin u_int32_t *rtalertp; 93353541Sshin u_int32_t *plenp; 93453541Sshin{ 93553541Sshin struct ip6_hdr *ip6; 93653541Sshin int optlen = 0; 93753541Sshin u_int8_t *opt = opthead; 93853541Sshin u_int16_t rtalert_val; 93962587Sitojun u_int32_t jumboplen; 94078064Sume const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 94153541Sshin 94253541Sshin for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 94378064Sume switch (*opt) { 94478064Sume case IP6OPT_PAD1: 94578064Sume optlen = 1; 94678064Sume break; 94778064Sume case IP6OPT_PADN: 94878064Sume if (hbhlen < IP6OPT_MINLEN) { 94978064Sume ip6stat.ip6s_toosmall++; 95078064Sume goto bad; 95178064Sume } 95278064Sume optlen = *(opt + 1) + 2; 95378064Sume break; 954121472Sume case IP6OPT_ROUTER_ALERT: 95562587Sitojun /* XXX may need check for alignment */ 95678064Sume if (hbhlen < IP6OPT_RTALERT_LEN) { 95778064Sume ip6stat.ip6s_toosmall++; 95878064Sume goto bad; 95978064Sume } 96078064Sume if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 96178064Sume /* XXX stat */ 96278064Sume icmp6_error(m, ICMP6_PARAM_PROB, 963120913Sume ICMP6_PARAMPROB_HEADER, 964120913Sume erroff + opt + 1 - opthead); 965120856Sume return (-1); 96678064Sume } 96778064Sume optlen = IP6OPT_RTALERT_LEN; 96878064Sume bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 96978064Sume *rtalertp = ntohs(rtalert_val); 97078064Sume break; 97178064Sume case IP6OPT_JUMBO: 97278064Sume /* XXX may need check for alignment */ 97362587Sitojun if (hbhlen < IP6OPT_JUMBO_LEN) { 97462587Sitojun ip6stat.ip6s_toosmall++; 97562587Sitojun goto bad; 97662587Sitojun } 97778064Sume if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 97878064Sume /* XXX stat */ 97978064Sume icmp6_error(m, ICMP6_PARAM_PROB, 980120913Sume ICMP6_PARAMPROB_HEADER, 981120913Sume erroff + opt + 1 - opthead); 982120856Sume return (-1); 98378064Sume } 98462587Sitojun optlen = IP6OPT_JUMBO_LEN; 98553541Sshin 98662587Sitojun /* 98762587Sitojun * IPv6 packets that have non 0 payload length 98878064Sume * must not contain a jumbo payload option. 98962587Sitojun */ 99062587Sitojun ip6 = mtod(m, struct ip6_hdr *); 99162587Sitojun if (ip6->ip6_plen) { 99262587Sitojun ip6stat.ip6s_badoptions++; 99362587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 994120913Sume ICMP6_PARAMPROB_HEADER, 995120913Sume erroff + opt - opthead); 996120856Sume return (-1); 99762587Sitojun } 99853541Sshin 99962587Sitojun /* 100062587Sitojun * We may see jumbolen in unaligned location, so 100162587Sitojun * we'd need to perform bcopy(). 100262587Sitojun */ 100362587Sitojun bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 100462587Sitojun jumboplen = (u_int32_t)htonl(jumboplen); 100562587Sitojun 100662587Sitojun#if 1 100762587Sitojun /* 100862587Sitojun * if there are multiple jumbo payload options, 100962587Sitojun * *plenp will be non-zero and the packet will be 101062587Sitojun * rejected. 101162587Sitojun * the behavior may need some debate in ipngwg - 101262587Sitojun * multiple options does not make sense, however, 101362587Sitojun * there's no explicit mention in specification. 101462587Sitojun */ 101562587Sitojun if (*plenp != 0) { 101662587Sitojun ip6stat.ip6s_badoptions++; 101762587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 1018120913Sume ICMP6_PARAMPROB_HEADER, 1019120913Sume erroff + opt + 2 - opthead); 1020120856Sume return (-1); 102162587Sitojun } 102262587Sitojun#endif 102362587Sitojun 102462587Sitojun /* 102562587Sitojun * jumbo payload length must be larger than 65535. 102662587Sitojun */ 102762587Sitojun if (jumboplen <= IPV6_MAXPACKET) { 102862587Sitojun ip6stat.ip6s_badoptions++; 102962587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 1030120913Sume ICMP6_PARAMPROB_HEADER, 1031120913Sume erroff + opt + 2 - opthead); 1032120856Sume return (-1); 103362587Sitojun } 103462587Sitojun *plenp = jumboplen; 103562587Sitojun 103662587Sitojun break; 103778064Sume default: /* unknown option */ 103878064Sume if (hbhlen < IP6OPT_MINLEN) { 103978064Sume ip6stat.ip6s_toosmall++; 104078064Sume goto bad; 104178064Sume } 104278064Sume optlen = ip6_unknown_opt(opt, m, 104378064Sume erroff + opt - opthead); 104478064Sume if (optlen == -1) 1045120856Sume return (-1); 104678064Sume optlen += 2; 104778064Sume break; 104853541Sshin } 104953541Sshin } 105053541Sshin 1051120856Sume return (0); 105253541Sshin 105353541Sshin bad: 105453541Sshin m_freem(m); 1055120856Sume return (-1); 105653541Sshin} 105753541Sshin 105853541Sshin/* 105953541Sshin * Unknown option processing. 106053541Sshin * The third argument `off' is the offset from the IPv6 header to the option, 106153541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header 106253541Sshin * is not continuous in order to return an ICMPv6 error. 106353541Sshin */ 106453541Sshinint 106553541Sshinip6_unknown_opt(optp, m, off) 106653541Sshin u_int8_t *optp; 106753541Sshin struct mbuf *m; 106853541Sshin int off; 106953541Sshin{ 107053541Sshin struct ip6_hdr *ip6; 107153541Sshin 107278064Sume switch (IP6OPT_TYPE(*optp)) { 107378064Sume case IP6OPT_TYPE_SKIP: /* ignore the option */ 1074120856Sume return ((int)*(optp + 1)); 107578064Sume case IP6OPT_TYPE_DISCARD: /* silently discard */ 107678064Sume m_freem(m); 1077120856Sume return (-1); 107878064Sume case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 107978064Sume ip6stat.ip6s_badoptions++; 108078064Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1081120856Sume return (-1); 108278064Sume case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 108378064Sume ip6stat.ip6s_badoptions++; 108478064Sume ip6 = mtod(m, struct ip6_hdr *); 108578064Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 108678064Sume (m->m_flags & (M_BCAST|M_MCAST))) 108778064Sume m_freem(m); 108878064Sume else 108978064Sume icmp6_error(m, ICMP6_PARAM_PROB, 109078064Sume ICMP6_PARAMPROB_OPTION, off); 1091120856Sume return (-1); 109253541Sshin } 109353541Sshin 109453541Sshin m_freem(m); /* XXX: NOTREACHED */ 1095120856Sume return (-1); 109653541Sshin} 109753541Sshin 109853541Sshin/* 109962587Sitojun * Create the "control" list for this pcb. 110078064Sume * The function will not modify mbuf chain at all. 110162587Sitojun * 110278064Sume * with KAME mbuf chain restriction: 110362587Sitojun * The routine will be called from upper layer handlers like tcp6_input(). 110462587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already 110562587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 110662587Sitojun * very first mbuf on the mbuf chain. 110753541Sshin */ 110853541Sshinvoid 1109121674Sumeip6_savecontrol(in6p, m, mp) 111078064Sume struct inpcb *in6p; 1111121674Sume struct mbuf *m, **mp; 111253541Sshin{ 1113121472Sume#define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y)) 111493593Sjhb struct thread *td = curthread; /* XXX */ 111578064Sume int privileged = 0; 1116121674Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 111753541Sshin 111893593Sjhb if (td && !suser(td)) 1119120913Sume privileged++; 112053541Sshin 112178064Sume#ifdef SO_TIMESTAMP 112278064Sume if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 112353541Sshin struct timeval tv; 112453541Sshin 112553541Sshin microtime(&tv); 112653541Sshin *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1127120913Sume SCM_TIMESTAMP, SOL_SOCKET); 1128121472Sume if (*mp) 112953541Sshin mp = &(*mp)->m_next; 113097658Stanimura } 113162587Sitojun#endif 113262587Sitojun 1133121631Sume if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) 1134121631Sume return; 1135121631Sume 113653541Sshin /* RFC 2292 sec. 5 */ 113778064Sume if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 113853541Sshin struct in6_pktinfo pi6; 1139120913Sume 114053541Sshin bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1141121315Sume in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1142120913Sume pi6.ipi6_ifindex = 1143120913Sume (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1144120913Sume 114553541Sshin *mp = sbcreatecontrol((caddr_t) &pi6, 1146120913Sume sizeof(struct in6_pktinfo), 1147121472Sume IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1148121472Sume if (*mp) 114953541Sshin mp = &(*mp)->m_next; 115053541Sshin } 115178064Sume 115278064Sume if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 115353541Sshin int hlim = ip6->ip6_hlim & 0xff; 1154120913Sume 1155120913Sume *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1156121472Sume IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6); 1157121472Sume if (*mp) 115853541Sshin mp = &(*mp)->m_next; 115953541Sshin } 116053541Sshin 1161121472Sume if ((in6p->in6p_flags & IN6P_TCLASS) != 0) { 1162121472Sume u_int32_t flowinfo; 1163121472Sume int tclass; 1164121472Sume 1165121472Sume flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1166121472Sume flowinfo >>= 20; 1167121472Sume 1168121472Sume tclass = flowinfo & 0xff; 1169121472Sume *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass), 1170121472Sume IPV6_TCLASS, IPPROTO_IPV6); 1171121472Sume if (*mp) 1172121472Sume mp = &(*mp)->m_next; 1173121472Sume } 1174121472Sume 117553541Sshin /* 117653541Sshin * IPV6_HOPOPTS socket option. We require super-user privilege 117753541Sshin * for the option, but it might be too strict, since there might 117853541Sshin * be some hop-by-hop options which can be returned to normal user. 117953541Sshin * See RFC 2292 section 6. 118053541Sshin */ 1181121472Sume if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) { 1182121472Sume#ifdef DIAGNOSTIC 1183121472Sume if (!privileged) 1184121472Sume panic("IN6P_HOPOPTS is set for unprivileged socket"); 1185121472Sume#endif 118653541Sshin /* 118753541Sshin * Check if a hop-by-hop options header is contatined in the 118853541Sshin * received packet, and if so, store the options as ancillary 118953541Sshin * data. Note that a hop-by-hop options header must be 1190120913Sume * just after the IPv6 header, which is assured through the 1191120913Sume * IPv6 input processing. 119253541Sshin */ 119353541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 119462587Sitojun struct ip6_hbh *hbh; 119578064Sume int hbhlen = 0; 119678064Sume#ifdef PULLDOWN_TEST 119778064Sume struct mbuf *ext; 119878064Sume#endif 119953541Sshin 120062587Sitojun#ifndef PULLDOWN_TEST 120162587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 120262587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 120362587Sitojun#else 120478064Sume ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 120578064Sume ip6->ip6_nxt); 120678064Sume if (ext == NULL) { 120762587Sitojun ip6stat.ip6s_tooshort++; 120862587Sitojun return; 120962587Sitojun } 121078064Sume hbh = mtod(ext, struct ip6_hbh *); 121162587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 121278064Sume if (hbhlen != ext->m_len) { 121378064Sume m_freem(ext); 121462587Sitojun ip6stat.ip6s_tooshort++; 121562587Sitojun return; 121662587Sitojun } 121762587Sitojun#endif 121862587Sitojun 121953541Sshin /* 1220120913Sume * XXX: We copy the whole header even if a 1221120913Sume * jumbo payload option is included, the option which 1222120913Sume * is to be removed before returning according to 1223120913Sume * RFC2292. 122478064Sume * Note: this constraint is removed in 2292bis. 122553541Sshin */ 122662587Sitojun *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1227121472Sume IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1228121472Sume IPPROTO_IPV6); 1229121472Sume if (*mp) 123053541Sshin mp = &(*mp)->m_next; 123178064Sume#ifdef PULLDOWN_TEST 123278064Sume m_freem(ext); 123378064Sume#endif 123453541Sshin } 123553541Sshin } 123653541Sshin 1237121472Sume if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 123878064Sume int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 123953541Sshin 124053541Sshin /* 124153541Sshin * Search for destination options headers or routing 124253541Sshin * header(s) through the header chain, and stores each 124353541Sshin * header as ancillary data. 124453541Sshin * Note that the order of the headers remains in 124553541Sshin * the chain of ancillary data. 124653541Sshin */ 124778064Sume while (1) { /* is explicit loop prevention necessary? */ 124878064Sume struct ip6_ext *ip6e = NULL; 124962587Sitojun int elen; 125078064Sume#ifdef PULLDOWN_TEST 125178064Sume struct mbuf *ext = NULL; 125278064Sume#endif 125353541Sshin 125478064Sume /* 125578064Sume * if it is not an extension header, don't try to 125678064Sume * pull it from the chain. 125778064Sume */ 125878064Sume switch (nxt) { 125978064Sume case IPPROTO_DSTOPTS: 126078064Sume case IPPROTO_ROUTING: 126178064Sume case IPPROTO_HOPOPTS: 126278064Sume case IPPROTO_AH: /* is it possible? */ 126378064Sume break; 126478064Sume default: 126578064Sume goto loopend; 126678064Sume } 126778064Sume 126862587Sitojun#ifndef PULLDOWN_TEST 126978064Sume if (off + sizeof(*ip6e) > m->m_len) 127078064Sume goto loopend; 127162587Sitojun ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 127262587Sitojun if (nxt == IPPROTO_AH) 127362587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 127462587Sitojun else 127562587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 127678064Sume if (off + elen > m->m_len) 127778064Sume goto loopend; 127862587Sitojun#else 127978064Sume ext = ip6_pullexthdr(m, off, nxt); 128078064Sume if (ext == NULL) { 128162587Sitojun ip6stat.ip6s_tooshort++; 128262587Sitojun return; 128362587Sitojun } 128478064Sume ip6e = mtod(ext, struct ip6_ext *); 128562587Sitojun if (nxt == IPPROTO_AH) 128662587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 128762587Sitojun else 128862587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 128978064Sume if (elen != ext->m_len) { 129078064Sume m_freem(ext); 129162587Sitojun ip6stat.ip6s_tooshort++; 129262587Sitojun return; 129362587Sitojun } 129462587Sitojun#endif 129562587Sitojun 129678064Sume switch (nxt) { 129778064Sume case IPPROTO_DSTOPTS: 1298120913Sume if (!(in6p->in6p_flags & IN6P_DSTOPTS)) 129978064Sume break; 130053541Sshin 130178064Sume /* 130278064Sume * We also require super-user privilege for 1303120913Sume * the option. See comments on IN6_HOPOPTS. 130478064Sume */ 130578064Sume if (!privileged) 130678064Sume break; 130753541Sshin 130878064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1309121472Sume IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1310120913Sume IPPROTO_IPV6); 131178064Sume if (*mp) 131278064Sume mp = &(*mp)->m_next; 131378064Sume break; 131478064Sume case IPPROTO_ROUTING: 131578064Sume if (!in6p->in6p_flags & IN6P_RTHDR) 131678064Sume break; 131753541Sshin 131878064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1319121472Sume IS2292(IPV6_2292RTHDR, IPV6_RTHDR), 1320120913Sume IPPROTO_IPV6); 132178064Sume if (*mp) 132278064Sume mp = &(*mp)->m_next; 132378064Sume break; 132478064Sume case IPPROTO_HOPOPTS: 132578064Sume case IPPROTO_AH: /* is it possible? */ 132678064Sume break; 132753541Sshin 132878064Sume default: 132978064Sume /* 133078064Sume * other cases have been filtered in the above. 133178064Sume * none will visit this case. here we supply 133278064Sume * the code just in case (nxt overwritten or 133378064Sume * other cases). 133478064Sume */ 133578064Sume#ifdef PULLDOWN_TEST 133678064Sume m_freem(ext); 133778064Sume#endif 133878064Sume goto loopend; 133953541Sshin 134053541Sshin } 134153541Sshin 134253541Sshin /* proceed with the next header. */ 134362587Sitojun off += elen; 134453541Sshin nxt = ip6e->ip6e_nxt; 134578064Sume ip6e = NULL; 134678064Sume#ifdef PULLDOWN_TEST 134778064Sume m_freem(ext); 134878064Sume ext = NULL; 134978064Sume#endif 135053541Sshin } 135153541Sshin loopend: 135278064Sume ; 135353541Sshin } 135478064Sume 1355121472Sume#undef IS2292 135678064Sume} 135778064Sume 1358125776Sumevoid 1359125776Sumeip6_notify_pmtu(in6p, dst, mtu) 1360125776Sume struct inpcb *in6p; 1361125776Sume struct sockaddr_in6 *dst; 1362125776Sume u_int32_t *mtu; 1363125776Sume{ 1364125776Sume struct socket *so; 1365125776Sume struct mbuf *m_mtu; 1366125776Sume struct ip6_mtuinfo mtuctl; 1367125776Sume 1368125776Sume so = in6p->inp_socket; 1369125776Sume 1370125776Sume if (mtu == NULL) 1371125776Sume return; 1372125776Sume 1373125776Sume#ifdef DIAGNOSTIC 1374125776Sume if (so == NULL) /* I believe this is impossible */ 1375125776Sume panic("ip6_notify_pmtu: socket is NULL"); 1376125776Sume#endif 1377125776Sume 1378125776Sume bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */ 1379125776Sume mtuctl.ip6m_mtu = *mtu; 1380125776Sume mtuctl.ip6m_addr = *dst; 1381125776Sume in6_recoverscope(&mtuctl.ip6m_addr, &mtuctl.ip6m_addr.sin6_addr, NULL); 1382125776Sume 1383125776Sume if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1384125776Sume IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1385125776Sume return; 1386125776Sume 1387125776Sume if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1388125776Sume == 0) { 1389125776Sume m_freem(m_mtu); 1390125776Sume /* XXX: should count statistics */ 1391125776Sume } else 1392125776Sume sorwakeup(so); 1393125776Sume 1394125776Sume return; 1395125776Sume} 1396125776Sume 139778064Sume#ifdef PULLDOWN_TEST 139878064Sume/* 139978064Sume * pull single extension header from mbuf chain. returns single mbuf that 140078064Sume * contains the result, or NULL on error. 140178064Sume */ 140278064Sumestatic struct mbuf * 140378064Sumeip6_pullexthdr(m, off, nxt) 140478064Sume struct mbuf *m; 140578064Sume size_t off; 140678064Sume int nxt; 140778064Sume{ 140878064Sume struct ip6_ext ip6e; 140978064Sume size_t elen; 141078064Sume struct mbuf *n; 141178064Sume 141278064Sume#ifdef DIAGNOSTIC 141378064Sume switch (nxt) { 141478064Sume case IPPROTO_DSTOPTS: 141578064Sume case IPPROTO_ROUTING: 141678064Sume case IPPROTO_HOPOPTS: 141778064Sume case IPPROTO_AH: /* is it possible? */ 141878064Sume break; 141978064Sume default: 142078064Sume printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 142153541Sshin } 142278064Sume#endif 142378064Sume 142478064Sume m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 142578064Sume if (nxt == IPPROTO_AH) 142678064Sume elen = (ip6e.ip6e_len + 2) << 2; 142778064Sume else 142878064Sume elen = (ip6e.ip6e_len + 1) << 3; 142978064Sume 1430111119Simp MGET(n, M_DONTWAIT, MT_DATA); 143178064Sume if (n && elen >= MLEN) { 1432111119Simp MCLGET(n, M_DONTWAIT); 143378064Sume if ((n->m_flags & M_EXT) == 0) { 143478064Sume m_free(n); 143578064Sume n = NULL; 143678064Sume } 143753541Sshin } 143878064Sume if (!n) 143978064Sume return NULL; 144062587Sitojun 144178064Sume n->m_len = 0; 144278064Sume if (elen >= M_TRAILINGSPACE(n)) { 144378064Sume m_free(n); 144478064Sume return NULL; 144578064Sume } 144678064Sume 144778064Sume m_copydata(m, off, elen, mtod(n, caddr_t)); 144878064Sume n->m_len = elen; 144978064Sume return n; 145053541Sshin} 145178064Sume#endif 145253541Sshin 145353541Sshin/* 145453541Sshin * Get pointer to the previous header followed by the header 145553541Sshin * currently processed. 145653541Sshin * XXX: This function supposes that 145753541Sshin * M includes all headers, 145853541Sshin * the next header field and the header length field of each header 145953541Sshin * are valid, and 146053541Sshin * the sum of each header length equals to OFF. 146153541Sshin * Because of these assumptions, this function must be called very 146253541Sshin * carefully. Moreover, it will not be used in the near future when 146353541Sshin * we develop `neater' mechanism to process extension headers. 146453541Sshin */ 146553541Sshinchar * 146653541Sshinip6_get_prevhdr(m, off) 146753541Sshin struct mbuf *m; 146853541Sshin int off; 146953541Sshin{ 147053541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 147153541Sshin 147253541Sshin if (off == sizeof(struct ip6_hdr)) 1473120856Sume return (&ip6->ip6_nxt); 147453541Sshin else { 147553541Sshin int len, nxt; 147653541Sshin struct ip6_ext *ip6e = NULL; 147753541Sshin 147853541Sshin nxt = ip6->ip6_nxt; 147953541Sshin len = sizeof(struct ip6_hdr); 148053541Sshin while (len < off) { 148153541Sshin ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 148253541Sshin 148378064Sume switch (nxt) { 148453541Sshin case IPPROTO_FRAGMENT: 148553541Sshin len += sizeof(struct ip6_frag); 148653541Sshin break; 148753541Sshin case IPPROTO_AH: 148853541Sshin len += (ip6e->ip6e_len + 2) << 2; 148953541Sshin break; 149053541Sshin default: 149153541Sshin len += (ip6e->ip6e_len + 1) << 3; 149253541Sshin break; 149353541Sshin } 149453541Sshin nxt = ip6e->ip6e_nxt; 149553541Sshin } 149653541Sshin if (ip6e) 1497120856Sume return (&ip6e->ip6e_nxt); 149853541Sshin else 149953541Sshin return NULL; 150053541Sshin } 150153541Sshin} 150253541Sshin 150353541Sshin/* 150462587Sitojun * get next header offset. m will be retained. 150562587Sitojun */ 150662587Sitojunint 150762587Sitojunip6_nexthdr(m, off, proto, nxtp) 150862587Sitojun struct mbuf *m; 150962587Sitojun int off; 151062587Sitojun int proto; 151162587Sitojun int *nxtp; 151262587Sitojun{ 151362587Sitojun struct ip6_hdr ip6; 151462587Sitojun struct ip6_ext ip6e; 151562587Sitojun struct ip6_frag fh; 151662587Sitojun 151762587Sitojun /* just in case */ 151862587Sitojun if (m == NULL) 151962587Sitojun panic("ip6_nexthdr: m == NULL"); 152062587Sitojun if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 152162587Sitojun return -1; 152262587Sitojun 152362587Sitojun switch (proto) { 152462587Sitojun case IPPROTO_IPV6: 152562587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6)) 152662587Sitojun return -1; 152762587Sitojun m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 152862587Sitojun if (nxtp) 152962587Sitojun *nxtp = ip6.ip6_nxt; 153062587Sitojun off += sizeof(ip6); 153162587Sitojun return off; 153262587Sitojun 153362587Sitojun case IPPROTO_FRAGMENT: 153462587Sitojun /* 153562587Sitojun * terminate parsing if it is not the first fragment, 153662587Sitojun * it does not make sense to parse through it. 153762587Sitojun */ 153862587Sitojun if (m->m_pkthdr.len < off + sizeof(fh)) 153962587Sitojun return -1; 154062587Sitojun m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1541120978Sume /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1542120978Sume if (fh.ip6f_offlg & IP6F_OFF_MASK) 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 1613121673Sumestruct ip6aux * 161478064Sumeip6_addaux(m) 161578064Sume struct mbuf *m; 161678064Sume{ 1617120913Sume struct m_tag *mtag; 1618120913Sume 1619120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1620120913Sume if (!mtag) { 1621120913Sume mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux), 1622120913Sume M_NOWAIT); 1623121630Sume if (mtag) { 1624120913Sume m_tag_prepend(m, mtag); 1625121630Sume bzero(mtag + 1, sizeof(struct ip6aux)); 1626121630Sume } 162778064Sume } 1628121673Sume return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 162978064Sume} 163078064Sume 1631121673Sumestruct ip6aux * 163278064Sumeip6_findaux(m) 163378064Sume struct mbuf *m; 163478064Sume{ 1635120913Sume struct m_tag *mtag; 1636120913Sume 1637120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1638121673Sume return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 163978064Sume} 164078064Sume 164178064Sumevoid 164278064Sumeip6_delaux(m) 164378064Sume struct mbuf *m; 164478064Sume{ 1645120913Sume struct m_tag *mtag; 1646120913Sume 1647120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1648120913Sume if (mtag) 1649120913Sume m_tag_delete(m, mtag); 165078064Sume} 165178064Sume 165262587Sitojun/* 165353541Sshin * System control for IP6 165453541Sshin */ 165553541Sshin 165653541Sshinu_char inet6ctlerrmap[PRC_NCMDS] = { 165753541Sshin 0, 0, 0, 0, 165853541Sshin 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 165953541Sshin EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 166053541Sshin EMSGSIZE, EHOSTUNREACH, 0, 0, 166153541Sshin 0, 0, 0, 0, 166253541Sshin ENOPROTOOPT 166353541Sshin}; 1664