1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $ 3053541Sshin */ 3153541Sshin 32139826Simp/*- 3353541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3453541Sshin * The Regents of the University of California. All rights reserved. 3553541Sshin * 3653541Sshin * Redistribution and use in source and binary forms, with or without 3753541Sshin * modification, are permitted provided that the following conditions 3853541Sshin * are met: 3953541Sshin * 1. Redistributions of source code must retain the above copyright 4053541Sshin * notice, this list of conditions and the following disclaimer. 4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4253541Sshin * notice, this list of conditions and the following disclaimer in the 4353541Sshin * documentation and/or other materials provided with the distribution. 4453541Sshin * 4. Neither the name of the University nor the names of its contributors 4553541Sshin * may be used to endorse or promote products derived from this software 4653541Sshin * without specific prior written permission. 4753541Sshin * 4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5153541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5853541Sshin * SUCH DAMAGE. 5953541Sshin * 6053541Sshin * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 6153541Sshin */ 6253541Sshin 63174510Sobrien#include <sys/cdefs.h> 64174510Sobrien__FBSDID("$FreeBSD$"); 65174510Sobrien 6662587Sitojun#include "opt_inet.h" 6762587Sitojun#include "opt_inet6.h" 68225044Sbz#include "opt_ipfw.h" 6955009Sshin#include "opt_ipsec.h" 70207828Skmacy#include "opt_route.h" 7155009Sshin 7253541Sshin#include <sys/param.h> 7353541Sshin#include <sys/systm.h> 7478064Sume#include <sys/malloc.h> 7553541Sshin#include <sys/mbuf.h> 7683366Sjulian#include <sys/proc.h> 7753541Sshin#include <sys/domain.h> 7853541Sshin#include <sys/protosw.h> 7953541Sshin#include <sys/socket.h> 8053541Sshin#include <sys/socketvar.h> 8153541Sshin#include <sys/errno.h> 8253541Sshin#include <sys/time.h> 8353541Sshin#include <sys/kernel.h> 8453541Sshin#include <sys/syslog.h> 8553541Sshin 8653541Sshin#include <net/if.h> 8753541Sshin#include <net/if_types.h> 8853541Sshin#include <net/if_dl.h> 8953541Sshin#include <net/route.h> 9053541Sshin#include <net/netisr.h> 9164060Sdarrenr#include <net/pfil.h> 92185571Sbz#include <net/vnet.h> 9353541Sshin 9453541Sshin#include <netinet/in.h> 95225044Sbz#include <netinet/ip_var.h> 9653541Sshin#include <netinet/in_systm.h> 97186119Sqingli#include <net/if_llatbl.h> 9862587Sitojun#ifdef INET 9953541Sshin#include <netinet/ip.h> 10053541Sshin#include <netinet/ip_icmp.h> 10195023Ssuz#endif /* INET */ 10262587Sitojun#include <netinet/ip6.h> 10353541Sshin#include <netinet6/in6_var.h> 10453541Sshin#include <netinet6/ip6_var.h> 10562587Sitojun#include <netinet/in_pcb.h> 10662587Sitojun#include <netinet/icmp6.h> 107121161Sume#include <netinet6/scope6_var.h> 10853541Sshin#include <netinet6/in6_ifattach.h> 10953541Sshin#include <netinet6/nd6.h> 11053541Sshin 111171167Sgnn#ifdef IPSEC 112105199Ssam#include <netipsec/ipsec.h> 113171133Sgnn#include <netinet6/ip6_ipsec.h> 114105199Ssam#include <netipsec/ipsec6.h> 115171167Sgnn#endif /* IPSEC */ 116105199Ssam 11753541Sshin#include <netinet6/ip6protosw.h> 11853541Sshin 119207828Skmacy#ifdef FLOWTABLE 120207828Skmacy#include <net/flowtable.h> 121208043SkmacyVNET_DECLARE(int, ip6_output_flowtable_size); 122207828Skmacy#define V_ip6_output_flowtable_size VNET(ip6_output_flowtable_size) 123207828Skmacy#endif 124207828Skmacy 12562587Sitojunextern struct domain inet6domain; 12653541Sshin 12762587Sitojunu_char ip6_protox[IPPROTO_MAX]; 128207369SbzVNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead); 129185088Szec 130193219Srwatsonstatic struct netisr_handler ip6_nh = { 131193219Srwatson .nh_name = "ip6", 132193219Srwatson .nh_handler = ip6_input, 133193219Srwatson .nh_proto = NETISR_IPV6, 134193219Srwatson .nh_policy = NETISR_POLICY_FLOW, 135193219Srwatson}; 136193219Srwatson 137195699SrwatsonVNET_DECLARE(struct callout, in6_tmpaddrtimer_ch); 138195727Srwatson#define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch) 13978064Sume 140207369SbzVNET_DEFINE(struct pfil_head, inet6_pfil_hook); 141207369Sbz 142207369SbzVNET_DEFINE(struct ip6stat, ip6stat); 143207369Sbz 144194971Srwatsonstruct rwlock in6_ifaddr_lock; 145194971SrwatsonRW_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock"); 146194971Srwatson 147175162Sobrienstatic void ip6_init2(void *); 148175162Sobrienstatic struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *); 149238233Sbzstatic struct ip6aux *ip6_addaux(struct mbuf *); 150238233Sbzstatic struct ip6aux *ip6_findaux(struct mbuf *m); 151238233Sbzstatic void ip6_delaux (struct mbuf *); 152175162Sobrienstatic int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *); 15362587Sitojun#ifdef PULLDOWN_TEST 154175162Sobrienstatic struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int); 15553541Sshin#endif 15653541Sshin 15753541Sshin/* 15853541Sshin * IP6 initialization: fill in IP6 protocol switch table. 15953541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler. 16053541Sshin */ 16153541Sshinvoid 162171259Sdelphijip6_init(void) 16353541Sshin{ 16478064Sume struct ip6protosw *pr; 16578064Sume int i; 16653541Sshin 167185895Szec TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal", 168185895Szec &V_ip6_auto_linklocal); 169253239Shrs TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv); 170253239Shrs TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr); 171185088Szec 172207369Sbz TAILQ_INIT(&V_in6_ifaddrhead); 173185088Szec 174207369Sbz /* Initialize packet filter hooks. */ 175207369Sbz V_inet6_pfil_hook.ph_type = PFIL_TYPE_AF; 176207369Sbz V_inet6_pfil_hook.ph_af = AF_INET6; 177207369Sbz if ((i = pfil_head_register(&V_inet6_pfil_hook)) != 0) 178207369Sbz printf("%s: WARNING: unable to register pfil hook, " 179207369Sbz "error %d\n", __func__, i); 180185088Szec 181190787Szec scope6_init(); 182190787Szec addrsel_policy_init(); 183190787Szec nd6_init(); 184190787Szec frag6_init(); 185190787Szec 186207828Skmacy#ifdef FLOWTABLE 187207902Skmacy if (TUNABLE_INT_FETCH("net.inet6.ip6.output_flowtable_size", 188207902Skmacy &V_ip6_output_flowtable_size)) { 189207902Skmacy if (V_ip6_output_flowtable_size < 256) 190207902Skmacy V_ip6_output_flowtable_size = 256; 191207902Skmacy if (!powerof2(V_ip6_output_flowtable_size)) { 192207902Skmacy printf("flowtable must be power of 2 size\n"); 193207902Skmacy V_ip6_output_flowtable_size = 2048; 194207902Skmacy } 195207902Skmacy } else { 196207902Skmacy /* 197207902Skmacy * round up to the next power of 2 198207902Skmacy */ 199207902Skmacy V_ip6_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1); 200207902Skmacy } 201208171Skmacy V_ip6_ft = flowtable_alloc("ipv6", V_ip6_output_flowtable_size, FL_IPV6|FL_PCPU); 202207828Skmacy#endif 203207828Skmacy 204190787Szec V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR; 205190787Szec 206190787Szec /* Skip global initialization stuff for non-default instances. */ 207190787Szec if (!IS_DEFAULT_VNET(curvnet)) 208190787Szec return; 209190787Szec 21078064Sume#ifdef DIAGNOSTIC 21178064Sume if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 21278064Sume panic("sizeof(protosw) != sizeof(ip6protosw)"); 21378064Sume#endif 21453541Sshin pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 215191433Sbz if (pr == NULL) 21653541Sshin panic("ip6_init"); 217136689Sandre 218186393Sbz /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */ 21953541Sshin for (i = 0; i < IPPROTO_MAX; i++) 22053541Sshin ip6_protox[i] = pr - inet6sw; 221136689Sandre /* 222136689Sandre * Cycle through IP protocols and put them into the appropriate place 223136689Sandre * in ip6_protox[]. 224136689Sandre */ 22553541Sshin for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 22653541Sshin pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 22753541Sshin if (pr->pr_domain->dom_family == PF_INET6 && 228136689Sandre pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 229136689Sandre /* Be careful to only index valid IP protocols. */ 230143675Ssam if (pr->pr_protocol < IPPROTO_MAX) 231136689Sandre ip6_protox[pr->pr_protocol] = pr - inet6sw; 232136689Sandre } 233134383Sandre 234193219Srwatson netisr_register(&ip6_nh); 23553541Sshin} 23653541Sshin 237212155Sbz/* 238212155Sbz * The protocol to be inserted into ip6_protox[] must be already registered 239212155Sbz * in inet6sw[], either statically or through pf_proto_register(). 240212155Sbz */ 241212155Sbzint 242212155Sbzip6proto_register(short ip6proto) 243212155Sbz{ 244212155Sbz struct ip6protosw *pr; 245212155Sbz 246212155Sbz /* Sanity checks. */ 247212155Sbz if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX) 248212155Sbz return (EPROTONOSUPPORT); 249212155Sbz 250212155Sbz /* 251212155Sbz * The protocol slot must not be occupied by another protocol 252212155Sbz * already. An index pointing to IPPROTO_RAW is unused. 253212155Sbz */ 254212155Sbz pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 255212155Sbz if (pr == NULL) 256212155Sbz return (EPFNOSUPPORT); 257212155Sbz if (ip6_protox[ip6proto] != pr - inet6sw) /* IPPROTO_RAW */ 258212155Sbz return (EEXIST); 259212155Sbz 260212155Sbz /* 261212155Sbz * Find the protocol position in inet6sw[] and set the index. 262212155Sbz */ 263212155Sbz for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 264212155Sbz pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) { 265212155Sbz if (pr->pr_domain->dom_family == PF_INET6 && 266212155Sbz pr->pr_protocol && pr->pr_protocol == ip6proto) { 267212155Sbz ip6_protox[pr->pr_protocol] = pr - inet6sw; 268212155Sbz return (0); 269212155Sbz } 270212155Sbz } 271212155Sbz return (EPROTONOSUPPORT); 272212155Sbz} 273212155Sbz 274212155Sbzint 275212155Sbzip6proto_unregister(short ip6proto) 276212155Sbz{ 277212155Sbz struct ip6protosw *pr; 278212155Sbz 279212155Sbz /* Sanity checks. */ 280212155Sbz if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX) 281212155Sbz return (EPROTONOSUPPORT); 282212155Sbz 283212155Sbz /* Check if the protocol was indeed registered. */ 284212155Sbz pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 285212155Sbz if (pr == NULL) 286212155Sbz return (EPFNOSUPPORT); 287212155Sbz if (ip6_protox[ip6proto] == pr - inet6sw) /* IPPROTO_RAW */ 288212155Sbz return (ENOENT); 289212155Sbz 290212155Sbz /* Reset the protocol slot to IPPROTO_RAW. */ 291212155Sbz ip6_protox[ip6proto] = pr - inet6sw; 292212155Sbz return (0); 293212155Sbz} 294212155Sbz 295193731Szec#ifdef VIMAGE 296193731Szecvoid 297193731Szecip6_destroy() 298193731Szec{ 299193731Szec 300193731Szec nd6_destroy(); 301193731Szec callout_drain(&V_in6_tmpaddrtimer_ch); 302193731Szec} 303193731Szec#endif 304193731Szec 305190787Szecstatic int 306190787Szecip6_init2_vnet(const void *unused __unused) 30753541Sshin{ 30853541Sshin 30953541Sshin /* nd6_timer_init */ 310181803Sbz callout_init(&V_nd6_timer_ch, 0); 311191688Szec callout_reset(&V_nd6_timer_ch, hz, nd6_timer, curvnet); 31278064Sume 31378064Sume /* timer for regeneranation of temporary addresses randomize ID */ 314181803Sbz callout_init(&V_in6_tmpaddrtimer_ch, 0); 315181803Sbz callout_reset(&V_in6_tmpaddrtimer_ch, 316181803Sbz (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 317181803Sbz V_ip6_temp_regen_advance) * hz, 318191688Szec in6_tmpaddrtimer, curvnet); 319190787Szec 320190787Szec return (0); 32153541Sshin} 32253541Sshin 323190787Szecstatic void 324190787Szecip6_init2(void *dummy) 325190787Szec{ 326190787Szec 327190787Szec ip6_init2_vnet(NULL); 328190787Szec} 329190787Szec 33053541Sshin/* cheat */ 33155009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */ 33255009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 33353541Sshin 334238238Sbzstatic int 335238238Sbzip6_input_hbh(struct mbuf *m, uint32_t *plen, uint32_t *rtalert, int *off, 336238238Sbz int *nxt, int *ours) 337238238Sbz{ 338238238Sbz struct ip6_hdr *ip6; 339238238Sbz struct ip6_hbh *hbh; 340238238Sbz 341238238Sbz if (ip6_hopopts_input(plen, rtalert, &m, off)) { 342238238Sbz#if 0 /*touches NULL pointer*/ 343238238Sbz in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 344238238Sbz#endif 345238238Sbz goto out; /* m have already been freed */ 346238238Sbz } 347238238Sbz 348238238Sbz /* adjust pointer */ 349238238Sbz ip6 = mtod(m, struct ip6_hdr *); 350238238Sbz 351238238Sbz /* 352238238Sbz * if the payload length field is 0 and the next header field 353238238Sbz * indicates Hop-by-Hop Options header, then a Jumbo Payload 354238238Sbz * option MUST be included. 355238238Sbz */ 356238238Sbz if (ip6->ip6_plen == 0 && *plen == 0) { 357238238Sbz /* 358238238Sbz * Note that if a valid jumbo payload option is 359238238Sbz * contained, ip6_hopopts_input() must set a valid 360238238Sbz * (non-zero) payload length to the variable plen. 361238238Sbz */ 362250044Sae IP6STAT_INC(ip6s_badoptions); 363238238Sbz in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 364238238Sbz in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 365238238Sbz icmp6_error(m, ICMP6_PARAM_PROB, 366238238Sbz ICMP6_PARAMPROB_HEADER, 367238238Sbz (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 368238238Sbz goto out; 369238238Sbz } 370238238Sbz#ifndef PULLDOWN_TEST 371238238Sbz /* ip6_hopopts_input() ensures that mbuf is contiguous */ 372238238Sbz hbh = (struct ip6_hbh *)(ip6 + 1); 373238238Sbz#else 374238238Sbz IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 375238238Sbz sizeof(struct ip6_hbh)); 376238238Sbz if (hbh == NULL) { 377250044Sae IP6STAT_INC(ip6s_tooshort); 378238238Sbz goto out; 379238238Sbz } 380238238Sbz#endif 381238238Sbz *nxt = hbh->ip6h_nxt; 382238238Sbz 383238238Sbz /* 384238238Sbz * If we are acting as a router and the packet contains a 385238238Sbz * router alert option, see if we know the option value. 386238238Sbz * Currently, we only support the option value for MLD, in which 387238238Sbz * case we should pass the packet to the multicast routing 388238238Sbz * daemon. 389238238Sbz */ 390238238Sbz if (*rtalert != ~0) { 391238238Sbz switch (*rtalert) { 392238238Sbz case IP6OPT_RTALERT_MLD: 393238238Sbz if (V_ip6_forwarding) 394238238Sbz *ours = 1; 395238238Sbz break; 396238238Sbz default: 397238238Sbz /* 398238238Sbz * RFC2711 requires unrecognized values must be 399238238Sbz * silently ignored. 400238238Sbz */ 401238238Sbz break; 402238238Sbz } 403238238Sbz } 404238238Sbz 405238238Sbz return (0); 406238238Sbz 407238238Sbzout: 408238238Sbz return (1); 409238238Sbz} 410238238Sbz 41153541Sshinvoid 412171259Sdelphijip6_input(struct mbuf *m) 41353541Sshin{ 41453541Sshin struct ip6_hdr *ip6; 41553541Sshin int off = sizeof(struct ip6_hdr), nest; 41653541Sshin u_int32_t plen; 41753541Sshin u_int32_t rtalert = ~0; 41853541Sshin int nxt, ours = 0; 419186119Sqingli struct ifnet *deliverifp = NULL, *ifp = NULL; 420121143Ssam struct in6_addr odst; 421187989Sbz struct route_in6 rin6; 422121143Ssam int srcrt = 0; 423186119Sqingli struct llentry *lle = NULL; 424187989Sbz struct sockaddr_in6 dst6, *dst; 425121144Ssam 426187989Sbz bzero(&rin6, sizeof(struct route_in6)); 427171167Sgnn#ifdef IPSEC 42853541Sshin /* 42953541Sshin * should the inner packet be considered authentic? 43053541Sshin * see comment in ah4_input(). 431171133Sgnn * NB: m cannot be NULL when passed to the input routine 43253541Sshin */ 43353541Sshin 434171133Sgnn m->m_flags &= ~M_AUTHIPHDR; 435171133Sgnn m->m_flags &= ~M_AUTHIPDGM; 436171133Sgnn 437171167Sgnn#endif /* IPSEC */ 438171133Sgnn 43953541Sshin /* 440121630Sume * make sure we don't have onion peering information into m_tag. 44178064Sume */ 44278064Sume ip6_delaux(m); 44378064Sume 444225044Sbz if (m->m_flags & M_FASTFWD_OURS) { 445225044Sbz /* 446225044Sbz * Firewall changed destination to local. 447225044Sbz */ 448225044Sbz m->m_flags &= ~M_FASTFWD_OURS; 449225044Sbz ours = 1; 450225044Sbz deliverifp = m->m_pkthdr.rcvif; 451225044Sbz ip6 = mtod(m, struct ip6_hdr *); 452225044Sbz goto hbhcheck; 453225044Sbz } 454225044Sbz 45578064Sume /* 45695023Ssuz * mbuf statistics 45753541Sshin */ 45853541Sshin if (m->m_flags & M_EXT) { 45953541Sshin if (m->m_next) 460250044Sae IP6STAT_INC(ip6s_mext2m); 46153541Sshin else 462250044Sae IP6STAT_INC(ip6s_mext1); 46353541Sshin } else { 46453541Sshin if (m->m_next) { 46553541Sshin if (m->m_flags & M_LOOP) { 466250044Sae IP6STAT_INC(ip6s_m2m[V_loif->if_index]); 467250044Sae } else if (m->m_pkthdr.rcvif->if_index < IP6S_M2MMAX) 468250044Sae IP6STAT_INC( 469250044Sae ip6s_m2m[m->m_pkthdr.rcvif->if_index]); 47053541Sshin else 471250044Sae IP6STAT_INC(ip6s_m2m[0]); 47253541Sshin } else 473250044Sae IP6STAT_INC(ip6s_m1); 47453541Sshin } 47553541Sshin 476151474Ssuz /* drop the packet if IPv6 operation is disabled on the IF */ 477151474Ssuz if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) { 478151474Ssuz m_freem(m); 479151474Ssuz return; 480151474Ssuz } 481151474Ssuz 48253541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 483250044Sae IP6STAT_INC(ip6s_total); 48453541Sshin 48562587Sitojun#ifndef PULLDOWN_TEST 48674336Skuriyama /* 48774336Skuriyama * L2 bridge code and some other code can return mbuf chain 48874336Skuriyama * that does not conform to KAME requirement. too bad. 48974336Skuriyama * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 49074336Skuriyama */ 49174336Skuriyama if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 49274336Skuriyama struct mbuf *n; 49374336Skuriyama 494111119Simp MGETHDR(n, M_DONTWAIT, MT_HEADER); 49577003Sume if (n) 496108466Ssam M_MOVE_PKTHDR(n, m); 497108825Ssam if (n && n->m_pkthdr.len > MHLEN) { 498111119Simp MCLGET(n, M_DONTWAIT); 49974336Skuriyama if ((n->m_flags & M_EXT) == 0) { 50074336Skuriyama m_freem(n); 50174336Skuriyama n = NULL; 50274336Skuriyama } 50374336Skuriyama } 50495023Ssuz if (n == NULL) { 50577003Sume m_freem(m); 506120913Sume return; /* ENOBUFS */ 50777003Sume } 50874336Skuriyama 509108825Ssam m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 510108825Ssam n->m_len = n->m_pkthdr.len; 51174336Skuriyama m_freem(m); 51274336Skuriyama m = n; 51374336Skuriyama } 514120913Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */); 51562587Sitojun#endif 51653541Sshin 51753541Sshin if (m->m_len < sizeof(struct ip6_hdr)) { 51853541Sshin struct ifnet *inifp; 51953541Sshin inifp = m->m_pkthdr.rcvif; 520120913Sume if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 521250044Sae IP6STAT_INC(ip6s_toosmall); 52253541Sshin in6_ifstat_inc(inifp, ifs6_in_hdrerr); 52353541Sshin return; 52453541Sshin } 52553541Sshin } 52653541Sshin 52753541Sshin ip6 = mtod(m, struct ip6_hdr *); 52853541Sshin 52953541Sshin if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 530250044Sae IP6STAT_INC(ip6s_badvers); 53153541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 53253541Sshin goto bad; 53353541Sshin } 53453541Sshin 535250044Sae IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]); 53653541Sshin 53753541Sshin /* 53878064Sume * Check against address spoofing/corruption. 53953541Sshin */ 54053541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 54153541Sshin IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 54278064Sume /* 54378064Sume * XXX: "badscope" is not very suitable for a multicast source. 54478064Sume */ 545250044Sae IP6STAT_INC(ip6s_badscope); 54653541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 54753541Sshin goto bad; 54853541Sshin } 549126444Sume if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 550126444Sume !(m->m_flags & M_LOOP)) { 551126444Sume /* 552126444Sume * In this case, the packet should come from the loopback 553126444Sume * interface. However, we cannot just check the if_flags, 554126444Sume * because ip6_mloopback() passes the "actual" interface 555126444Sume * as the outgoing/incoming interface. 556126444Sume */ 557250044Sae IP6STAT_INC(ip6s_badscope); 55878064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 55978064Sume goto bad; 56078064Sume } 561262257Sae if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 562262257Sae IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) { 563262257Sae /* 564262257Sae * RFC4291 2.7: 565262257Sae * Nodes must not originate a packet to a multicast address 566262257Sae * whose scop field contains the reserved value 0; if such 567262257Sae * a packet is received, it must be silently dropped. 568262257Sae */ 569262257Sae IP6STAT_INC(ip6s_badscope); 570262257Sae in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 571262257Sae goto bad; 572262257Sae } 573130416Smlaier#ifdef ALTQ 574130416Smlaier if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) { 575130416Smlaier /* packet is dropped by traffic conditioner */ 576130416Smlaier return; 577130416Smlaier } 578130416Smlaier#endif 57962587Sitojun /* 58078064Sume * The following check is not documented in specs. A malicious 58178064Sume * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 58278064Sume * and bypass security checks (act as if it was from 127.0.0.1 by using 583120913Sume * IPv6 src ::ffff:127.0.0.1). Be cautious. 58478064Sume * 58578064Sume * This check chokes if we are in an SIIT cloud. As none of BSDs 58678064Sume * support IPv4-less kernel compilation, we cannot support SIIT 58778064Sume * environment at all. So, it makes more sense for us to reject any 58878064Sume * malicious packets for non-SIIT environment, than try to do a 589120913Sume * partial support for SIIT environment. 59062587Sitojun */ 59178064Sume if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 59278064Sume IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 593250044Sae IP6STAT_INC(ip6s_badscope); 59478064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 59578064Sume goto bad; 59678064Sume } 59762587Sitojun#if 0 59862587Sitojun /* 59962587Sitojun * Reject packets with IPv4 compatible addresses (auto tunnel). 60062587Sitojun * 60162587Sitojun * The code forbids auto tunnel relay case in RFC1933 (the check is 60262587Sitojun * stronger than RFC1933). We may want to re-enable it if mech-xx 60362587Sitojun * is revised to forbid relaying case. 60462587Sitojun */ 60562587Sitojun if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 60662587Sitojun IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 607250044Sae IP6STAT_INC(ip6s_badscope); 60862587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 60962587Sitojun goto bad; 61062587Sitojun } 61162587Sitojun#endif 612222856Sbz#ifdef IPSEC 613222856Sbz /* 614222856Sbz * Bypass packet filtering for packets previously handled by IPsec. 615222856Sbz */ 616222856Sbz if (ip6_ipsec_filtertunnel(m)) 617222856Sbz goto passin; 618222856Sbz#endif /* IPSEC */ 61975246Sume 620126444Sume /* 621126508Smlaier * Run through list of hooks for input packets. 622126508Smlaier * 623126508Smlaier * NB: Beware of the destination address changing 624126508Smlaier * (e.g. by NAT rewriting). When this happens, 625126508Smlaier * tell ip6_forward to do the right thing. 626126508Smlaier */ 627126508Smlaier odst = ip6->ip6_dst; 628134383Sandre 629134383Sandre /* Jump over all PFIL processing if hooks are not active. */ 630197952Sjulian if (!PFIL_HOOKED(&V_inet6_pfil_hook)) 631134383Sandre goto passin; 632134383Sandre 633197952Sjulian if (pfil_run_hooks(&V_inet6_pfil_hook, &m, 634197952Sjulian m->m_pkthdr.rcvif, PFIL_IN, NULL)) 635126508Smlaier return; 636126508Smlaier if (m == NULL) /* consumed by filter */ 637126508Smlaier return; 638126508Smlaier ip6 = mtod(m, struct ip6_hdr *); 639126508Smlaier srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 640126508Smlaier 641225044Sbz if (m->m_flags & M_FASTFWD_OURS) { 642225044Sbz m->m_flags &= ~M_FASTFWD_OURS; 643225044Sbz ours = 1; 644225044Sbz deliverifp = m->m_pkthdr.rcvif; 645225044Sbz goto hbhcheck; 646225044Sbz } 647243586Sae if ((m->m_flags & M_IP6_NEXTHOP) && 648243586Sae m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) { 649225044Sbz /* 650225044Sbz * Directly ship the packet on. This allows forwarding 651225044Sbz * packets originally destined to us to some other directly 652225044Sbz * connected host. 653225044Sbz */ 654225044Sbz ip6_forward(m, 1); 655225044Sbz goto out; 656225044Sbz } 657225044Sbz 658134383Sandrepassin: 659126508Smlaier /* 660154804Sume * Disambiguate address scope zones (if there is ambiguity). 661154804Sume * We first make sure that the original source or destination address 662154804Sume * is not in our internal form for scoped addresses. Such addresses 663154804Sume * are not necessarily invalid spec-wise, but we cannot accept them due 664154804Sume * to the usage conflict. 665154804Sume * in6_setscope() then also checks and rejects the cases where src or 666154804Sume * dst are the loopback address and the receiving interface 667154804Sume * is not loopback. 668154804Sume */ 669154804Sume if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) { 670250044Sae IP6STAT_INC(ip6s_badscope); /* XXX */ 671154804Sume goto bad; 672154804Sume } 673154804Sume if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) || 674154804Sume in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) { 675250044Sae IP6STAT_INC(ip6s_badscope); 676154804Sume goto bad; 677154804Sume } 678154804Sume 679154804Sume /* 680191672Sbms * Multicast check. Assume packet is for us to avoid 681191672Sbms * prematurely taking locks. 68253541Sshin */ 68353541Sshin if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 684191672Sbms ours = 1; 68553541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 68653541Sshin deliverifp = m->m_pkthdr.rcvif; 68753541Sshin goto hbhcheck; 68853541Sshin } 68953541Sshin 69053541Sshin /* 69153541Sshin * Unicast check 69253541Sshin */ 693186119Sqingli 694186119Sqingli bzero(&dst6, sizeof(dst6)); 695186119Sqingli dst6.sin6_family = AF_INET6; 696186119Sqingli dst6.sin6_len = sizeof(struct sockaddr_in6); 697186119Sqingli dst6.sin6_addr = ip6->ip6_dst; 698186119Sqingli ifp = m->m_pkthdr.rcvif; 699243382Sae IF_AFDATA_RLOCK(ifp); 700186119Sqingli lle = lla_lookup(LLTABLE6(ifp), 0, 701186119Sqingli (struct sockaddr *)&dst6); 702243382Sae IF_AFDATA_RUNLOCK(ifp); 703186119Sqingli if ((lle != NULL) && (lle->la_flags & LLE_IFADDR)) { 704210350Sbz struct ifaddr *ifa; 705210350Sbz struct in6_ifaddr *ia6; 706210350Sbz int bad; 707210350Sbz 708210350Sbz bad = 1; 709210350Sbz#define sa_equal(a1, a2) \ 710210350Sbz (bcmp((a1), (a2), ((a1))->sin6_len) == 0) 711233200Sjhb IF_ADDR_RLOCK(ifp); 712210350Sbz TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 713210350Sbz if (ifa->ifa_addr->sa_family != dst6.sin6_family) 714210350Sbz continue; 715210350Sbz if (sa_equal(&dst6, ifa->ifa_addr)) 716210350Sbz break; 717210350Sbz } 718210350Sbz KASSERT(ifa != NULL, ("%s: ifa not found for lle %p", 719210350Sbz __func__, lle)); 720210350Sbz#undef sa_equal 721210350Sbz 722210350Sbz ia6 = (struct in6_ifaddr *)ifa; 723210350Sbz if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 724210350Sbz /* Count the packet in the ip address stats */ 725210350Sbz ia6->ia_ifa.if_ipackets++; 726210350Sbz ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 727210350Sbz 728210350Sbz /* 729210350Sbz * record address information into m_tag. 730210350Sbz */ 731210350Sbz (void)ip6_setdstifaddr(m, ia6); 732210350Sbz 733210350Sbz bad = 0; 734210350Sbz } else { 735210350Sbz char ip6bufs[INET6_ADDRSTRLEN]; 736210350Sbz char ip6bufd[INET6_ADDRSTRLEN]; 737210350Sbz /* address is not ready, so discard the packet. */ 738210350Sbz nd6log((LOG_INFO, 739210350Sbz "ip6_input: packet to an unready address %s->%s\n", 740210350Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 741210350Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst))); 742210350Sbz } 743233200Sjhb IF_ADDR_RUNLOCK(ifp); 744186119Sqingli LLE_RUNLOCK(lle); 745210350Sbz if (bad) 746210350Sbz goto bad; 747210350Sbz else { 748210350Sbz ours = 1; 749210350Sbz deliverifp = ifp; 750210350Sbz goto hbhcheck; 751210350Sbz } 752186119Sqingli } 753186293Sbz if (lle != NULL) 754186293Sbz LLE_RUNLOCK(lle); 755186119Sqingli 756187989Sbz dst = &rin6.ro_dst; 757187989Sbz dst->sin6_len = sizeof(struct sockaddr_in6); 758187989Sbz dst->sin6_family = AF_INET6; 759187989Sbz dst->sin6_addr = ip6->ip6_dst; 760232292Sbz rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m)); 761187989Sbz if (rin6.ro_rt) 762187989Sbz RT_UNLOCK(rin6.ro_rt); 76378064Sume 76453541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 76553541Sshin 76653541Sshin /* 76753541Sshin * Accept the packet if the forwarding interface to the destination 76853541Sshin * according to the routing table is the loopback interface, 76953541Sshin * unless the associated route has a gateway. 77053541Sshin * Note that this approach causes to accept a packet if there is a 77153541Sshin * route to the loopback interface for the destination of the packet. 77253541Sshin * But we think it's even useful in some situations, e.g. when using 77353541Sshin * a special daemon which wants to intercept the packet. 77478064Sume * 77578064Sume * XXX: some OSes automatically make a cloned route for the destination 77678064Sume * of an outgoing packet. If the outgoing interface of the packet 77778064Sume * is a loopback one, the kernel would consider the packet to be 77878064Sume * accepted, even if we have no such address assinged on the interface. 77978064Sume * We check the cloned flag of the route entry to reject such cases, 78078064Sume * assuming that route entries for our own addresses are not made by 78178064Sume * cloning (it should be true because in6_addloop explicitly installs 78278064Sume * the host route). However, we might have to do an explicit check 78378064Sume * while it would be less efficient. Or, should we rather install a 78478064Sume * reject route for such a case? 78553541Sshin */ 786187989Sbz if (rin6.ro_rt && 787187989Sbz (rin6.ro_rt->rt_flags & 78853541Sshin (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 78978064Sume#ifdef RTF_WASCLONED 790187989Sbz !(rin6.ro_rt->rt_flags & RTF_WASCLONED) && 79178064Sume#endif 79278064Sume#ifdef RTF_CLONED 793187989Sbz !(rin6.ro_rt->rt_flags & RTF_CLONED) && 79478064Sume#endif 79562587Sitojun#if 0 79653541Sshin /* 79762587Sitojun * The check below is redundant since the comparison of 79862587Sitojun * the destination and the key of the rtentry has 79962587Sitojun * already done through looking up the routing table. 80053541Sshin */ 80162587Sitojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 802187989Sbz &rt6_key(rin6.ro_rt)->sin6_addr) 80362587Sitojun#endif 804187989Sbz rin6.ro_rt->rt_ifp->if_type == IFT_LOOP) { 805196569Sqingli int free_ia6 = 0; 806196569Sqingli struct in6_ifaddr *ia6; 80778064Sume 80862587Sitojun /* 809196569Sqingli * found the loopback route to the interface address 810196569Sqingli */ 811196569Sqingli if (rin6.ro_rt->rt_gateway->sa_family == AF_LINK) { 812196569Sqingli struct sockaddr_in6 dest6; 813196569Sqingli 814196569Sqingli bzero(&dest6, sizeof(dest6)); 815196569Sqingli dest6.sin6_family = AF_INET6; 816196569Sqingli dest6.sin6_len = sizeof(dest6); 817196569Sqingli dest6.sin6_addr = ip6->ip6_dst; 818196569Sqingli ia6 = (struct in6_ifaddr *) 819196569Sqingli ifa_ifwithaddr((struct sockaddr *)&dest6); 820196569Sqingli if (ia6 == NULL) 821196569Sqingli goto bad; 822196569Sqingli free_ia6 = 1; 823196569Sqingli } 824196569Sqingli else 825196569Sqingli ia6 = (struct in6_ifaddr *)rin6.ro_rt->rt_ifa; 826196569Sqingli 827196569Sqingli /* 828121630Sume * record address information into m_tag. 82978064Sume */ 83078064Sume (void)ip6_setdstifaddr(m, ia6); 83178064Sume 83278064Sume /* 83362587Sitojun * packets to a tentative, duplicated, or somehow invalid 83462587Sitojun * address must not be accepted. 83562587Sitojun */ 83653541Sshin if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 83762587Sitojun /* this address is ready */ 83853541Sshin ours = 1; 83953541Sshin deliverifp = ia6->ia_ifp; /* correct? */ 84067334Sjoe /* Count the packet in the ip address stats */ 84167334Sjoe ia6->ia_ifa.if_ipackets++; 84267334Sjoe ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 843196569Sqingli if (ia6 != NULL && free_ia6 != 0) 844196569Sqingli ifa_free(&ia6->ia_ifa); 84553541Sshin goto hbhcheck; 84653541Sshin } else { 847165118Sbz char ip6bufs[INET6_ADDRSTRLEN]; 848165118Sbz char ip6bufd[INET6_ADDRSTRLEN]; 84962587Sitojun /* address is not ready, so discard the packet. */ 85078064Sume nd6log((LOG_INFO, 85178064Sume "ip6_input: packet to an unready address %s->%s\n", 852165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 853165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst))); 85462587Sitojun 855196569Sqingli if (ia6 != NULL && free_ia6 != 0) 856196569Sqingli ifa_free(&ia6->ia_ifa); 85762587Sitojun goto bad; 85853541Sshin } 85953541Sshin } 86053541Sshin 86153541Sshin /* 862120913Sume * FAITH (Firewall Aided Internet Translator) 86353541Sshin */ 864181803Sbz if (V_ip6_keepfaith) { 865187989Sbz if (rin6.ro_rt && rin6.ro_rt->rt_ifp && 866187989Sbz rin6.ro_rt->rt_ifp->if_type == IFT_FAITH) { 86753541Sshin /* XXX do we need more sanity checks? */ 86853541Sshin ours = 1; 869187989Sbz deliverifp = rin6.ro_rt->rt_ifp; /* faith */ 87053541Sshin goto hbhcheck; 87153541Sshin } 87253541Sshin } 87353541Sshin 87453541Sshin /* 87553541Sshin * Now there is no reason to process the packet if it's not our own 87653541Sshin * and we're not a router. 87753541Sshin */ 878181803Sbz if (!V_ip6_forwarding) { 879250044Sae IP6STAT_INC(ip6s_cantforward); 88053541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 88153541Sshin goto bad; 88253541Sshin } 88353541Sshin 88453541Sshin hbhcheck: 88553541Sshin /* 886121630Sume * record address information into m_tag, if we don't have one yet. 88778064Sume * note that we are unable to record it, if the address is not listed 88878064Sume * as our interface address (e.g. multicast addresses, addresses 88978064Sume * within FAITH prefixes and such). 89078064Sume */ 891236608Sbz if (deliverifp) { 89278064Sume struct in6_ifaddr *ia6; 89378064Sume 894236608Sbz if ((ia6 = ip6_getdstifaddr(m)) != NULL) { 895236608Sbz ifa_free(&ia6->ia_ifa); 896236608Sbz } else { 897236608Sbz ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 898236608Sbz if (ia6) { 899236608Sbz if (!ip6_setdstifaddr(m, ia6)) { 900236608Sbz /* 901236608Sbz * XXX maybe we should drop the packet here, 902236608Sbz * as we could not provide enough information 903236608Sbz * to the upper layers. 904236608Sbz */ 905236608Sbz } 906236608Sbz ifa_free(&ia6->ia_ifa); 90778064Sume } 90878064Sume } 90978064Sume } 91078064Sume 91178064Sume /* 91253541Sshin * Process Hop-by-Hop options header if it's contained. 91353541Sshin * m may be modified in ip6_hopopts_input(). 91453541Sshin * If a JumboPayload option is included, plen will also be modified. 91553541Sshin */ 91653541Sshin plen = (u_int32_t)ntohs(ip6->ip6_plen); 91753541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 918238238Sbz int error; 91962587Sitojun 920238238Sbz error = ip6_input_hbh(m, &plen, &rtalert, &off, &nxt, &ours); 921238238Sbz if (error != 0) 922187989Sbz goto out; 92353541Sshin } else 92453541Sshin nxt = ip6->ip6_nxt; 92553541Sshin 92653541Sshin /* 92753541Sshin * Check that the amount of data in the buffers 92853541Sshin * is as at least much as the IPv6 header would have us expect. 92953541Sshin * Trim mbufs if longer than we expect. 93053541Sshin * Drop packet if shorter than we expect. 93153541Sshin */ 93253541Sshin if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 933250044Sae IP6STAT_INC(ip6s_tooshort); 93453541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 93553541Sshin goto bad; 93653541Sshin } 93753541Sshin if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 93853541Sshin if (m->m_len == m->m_pkthdr.len) { 93953541Sshin m->m_len = sizeof(struct ip6_hdr) + plen; 94053541Sshin m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 94153541Sshin } else 94253541Sshin m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 94353541Sshin } 94453541Sshin 94553541Sshin /* 94653541Sshin * Forward if desirable. 94753541Sshin */ 948191672Sbms if (V_ip6_mrouter && 949191672Sbms IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 95056723Sshin /* 95156723Sshin * If we are acting as a multicast router, all 95256723Sshin * incoming multicast packets are passed to the 95356723Sshin * kernel-level multicast forwarding function. 95456723Sshin * The packet is returned (relatively) intact; if 95556723Sshin * ip6_mforward() returns a non-zero value, the packet 95656723Sshin * must be discarded, else it may be accepted below. 957192923Sbms * 958192923Sbms * XXX TODO: Check hlim and multicast scope here to avoid 959192923Sbms * unnecessarily calling into ip6_mforward(). 96056723Sshin */ 961191672Sbms if (ip6_mforward && 962166938Sbms ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 963191672Sbms IP6STAT_INC(ip6s_cantforward); 964191672Sbms in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 965187989Sbz goto bad; 96656723Sshin } 96753541Sshin } else if (!ours) { 968121143Ssam ip6_forward(m, srcrt); 969187989Sbz goto out; 970121673Sume } 97153541Sshin 97262587Sitojun ip6 = mtod(m, struct ip6_hdr *); 97362587Sitojun 97453541Sshin /* 97562587Sitojun * Malicious party may be able to use IPv4 mapped addr to confuse 97662587Sitojun * tcp/udp stack and bypass security checks (act as if it was from 97762587Sitojun * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 97862587Sitojun * 97962587Sitojun * For SIIT end node behavior, you may want to disable the check. 98062587Sitojun * However, you will become vulnerable to attacks using IPv4 mapped 98162587Sitojun * source. 98262587Sitojun */ 98362587Sitojun if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 98462587Sitojun IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 985250044Sae IP6STAT_INC(ip6s_badscope); 98662587Sitojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 98762587Sitojun goto bad; 98862587Sitojun } 98962587Sitojun 99062587Sitojun /* 99153541Sshin * Tell launch routine the next header 99253541Sshin */ 993250044Sae IP6STAT_INC(ip6s_delivered); 99453541Sshin in6_ifstat_inc(deliverifp, ifs6_in_deliver); 99553541Sshin nest = 0; 99678064Sume 99753541Sshin while (nxt != IPPROTO_DONE) { 998181803Sbz if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) { 999250044Sae IP6STAT_INC(ip6s_toomanyhdr); 100053541Sshin goto bad; 100153541Sshin } 100253541Sshin 100353541Sshin /* 100453541Sshin * protection against faulty packet - there should be 100553541Sshin * more sanity checks in header chain processing. 100653541Sshin */ 100753541Sshin if (m->m_pkthdr.len < off) { 1008250044Sae IP6STAT_INC(ip6s_tooshort); 100953541Sshin in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 101053541Sshin goto bad; 101153541Sshin } 101253541Sshin 1013171167Sgnn#ifdef IPSEC 101478064Sume /* 101578064Sume * enforce IPsec policy checking if we are seeing last header. 101678064Sume * note that we do not visit this with protocols with pcb layer 101778064Sume * code - like udp/tcp/raw ip. 101878064Sume */ 1019171133Sgnn if (ip6_ipsec_input(m, nxt)) 102078064Sume goto bad; 1021171167Sgnn#endif /* IPSEC */ 1022192923Sbms 1023192923Sbms /* 1024192923Sbms * Use mbuf flags to propagate Router Alert option to 1025192923Sbms * ICMPv6 layer, as hop-by-hop options have been stripped. 1026192923Sbms */ 1027192923Sbms if (nxt == IPPROTO_ICMPV6 && rtalert != ~0) 1028192923Sbms m->m_flags |= M_RTALERT_MLD; 1029192923Sbms 103053541Sshin nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 103153541Sshin } 1032187989Sbz goto out; 1033187989Sbzbad: 103453541Sshin m_freem(m); 1035187989Sbzout: 1036187989Sbz if (rin6.ro_rt) 1037187989Sbz RTFREE(rin6.ro_rt); 103853541Sshin} 103953541Sshin 104053541Sshin/* 104178064Sume * set/grab in6_ifaddr correspond to IPv6 destination address. 104278064Sume * XXX backward compatibility wrapper 1043194760Srwatson * 1044194760Srwatson * XXXRW: We should bump the refcount on ia6 before sticking it in the m_tag, 1045194760Srwatson * and then bump it when the tag is copied, and release it when the tag is 1046194760Srwatson * freed. Unfortunately, m_tags don't support deep copies (yet), so instead 1047194760Srwatson * we just bump the ia refcount when we receive it. This should be fixed. 104878064Sume */ 1049121673Sumestatic struct ip6aux * 1050171259Sdelphijip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6) 105178064Sume{ 1052121673Sume struct ip6aux *ip6a; 105378064Sume 1054121673Sume ip6a = ip6_addaux(m); 1055121673Sume if (ip6a) 1056121673Sume ip6a->ip6a_dstia6 = ia6; 1057121673Sume return ip6a; /* NULL if failed to set */ 105878064Sume} 105978064Sume 106078064Sumestruct in6_ifaddr * 1061171259Sdelphijip6_getdstifaddr(struct mbuf *m) 106278064Sume{ 1063121673Sume struct ip6aux *ip6a; 1064194760Srwatson struct in6_ifaddr *ia; 106578064Sume 1066121673Sume ip6a = ip6_findaux(m); 1067194760Srwatson if (ip6a) { 1068194760Srwatson ia = ip6a->ip6a_dstia6; 1069194760Srwatson ifa_ref(&ia->ia_ifa); 1070194760Srwatson return ia; 1071194760Srwatson } else 107278064Sume return NULL; 107378064Sume} 107478064Sume 107578064Sume/* 107653541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is 107753541Sshin * included, the real payload length will be stored in plenp. 1078171259Sdelphij * 1079171259Sdelphij * rtalertp - XXX: should be stored more smart way 108053541Sshin */ 108153541Sshinstatic int 1082171259Sdelphijip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp, 1083171259Sdelphij struct mbuf **mp, int *offp) 108453541Sshin{ 108578064Sume struct mbuf *m = *mp; 108653541Sshin int off = *offp, hbhlen; 108753541Sshin struct ip6_hbh *hbh; 108853541Sshin 108953541Sshin /* validation of the length of the header */ 109062587Sitojun#ifndef PULLDOWN_TEST 109153541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 109253541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 109353541Sshin hbhlen = (hbh->ip6h_len + 1) << 3; 109453541Sshin 109553541Sshin IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 109653541Sshin hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 109762587Sitojun#else 109862587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 109962587Sitojun sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 110062587Sitojun if (hbh == NULL) { 1101250044Sae IP6STAT_INC(ip6s_tooshort); 110262587Sitojun return -1; 110362587Sitojun } 110462587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 110562587Sitojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 110662587Sitojun hbhlen); 110762587Sitojun if (hbh == NULL) { 1108250044Sae IP6STAT_INC(ip6s_tooshort); 110962587Sitojun return -1; 111062587Sitojun } 111162587Sitojun#endif 111253541Sshin off += hbhlen; 111353541Sshin hbhlen -= sizeof(struct ip6_hbh); 111453541Sshin if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 111553541Sshin hbhlen, rtalertp, plenp) < 0) 1116120856Sume return (-1); 111753541Sshin 111853541Sshin *offp = off; 111953541Sshin *mp = m; 1120120856Sume return (0); 112153541Sshin} 112253541Sshin 112353541Sshin/* 112453541Sshin * Search header for all Hop-by-hop options and process each option. 112553541Sshin * This function is separate from ip6_hopopts_input() in order to 112653541Sshin * handle a case where the sending node itself process its hop-by-hop 112753541Sshin * options header. In such a case, the function is called from ip6_output(). 112878064Sume * 112978064Sume * The function assumes that hbh header is located right after the IPv6 header 113078064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 1131215956Sbrucec * opthead + hbhlen is located in contiguous memory region. 113253541Sshin */ 113353541Sshinint 1134171259Sdelphijip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen, 1135171259Sdelphij u_int32_t *rtalertp, u_int32_t *plenp) 113653541Sshin{ 113753541Sshin struct ip6_hdr *ip6; 113853541Sshin int optlen = 0; 113953541Sshin u_int8_t *opt = opthead; 114053541Sshin u_int16_t rtalert_val; 114162587Sitojun u_int32_t jumboplen; 114278064Sume const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 114353541Sshin 114453541Sshin for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 114578064Sume switch (*opt) { 114678064Sume case IP6OPT_PAD1: 114778064Sume optlen = 1; 114878064Sume break; 114978064Sume case IP6OPT_PADN: 115078064Sume if (hbhlen < IP6OPT_MINLEN) { 1151250044Sae IP6STAT_INC(ip6s_toosmall); 115278064Sume goto bad; 115378064Sume } 115478064Sume optlen = *(opt + 1) + 2; 115578064Sume break; 1156121472Sume case IP6OPT_ROUTER_ALERT: 115762587Sitojun /* XXX may need check for alignment */ 115878064Sume if (hbhlen < IP6OPT_RTALERT_LEN) { 1159250044Sae IP6STAT_INC(ip6s_toosmall); 116078064Sume goto bad; 116178064Sume } 116278064Sume if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 116378064Sume /* XXX stat */ 116478064Sume icmp6_error(m, ICMP6_PARAM_PROB, 1165120913Sume ICMP6_PARAMPROB_HEADER, 1166120913Sume erroff + opt + 1 - opthead); 1167120856Sume return (-1); 116878064Sume } 116978064Sume optlen = IP6OPT_RTALERT_LEN; 117078064Sume bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 117178064Sume *rtalertp = ntohs(rtalert_val); 117278064Sume break; 117378064Sume case IP6OPT_JUMBO: 117478064Sume /* XXX may need check for alignment */ 117562587Sitojun if (hbhlen < IP6OPT_JUMBO_LEN) { 1176250044Sae IP6STAT_INC(ip6s_toosmall); 117762587Sitojun goto bad; 117862587Sitojun } 117978064Sume if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 118078064Sume /* XXX stat */ 118178064Sume icmp6_error(m, ICMP6_PARAM_PROB, 1182120913Sume ICMP6_PARAMPROB_HEADER, 1183120913Sume erroff + opt + 1 - opthead); 1184120856Sume return (-1); 118578064Sume } 118662587Sitojun optlen = IP6OPT_JUMBO_LEN; 118753541Sshin 118862587Sitojun /* 118962587Sitojun * IPv6 packets that have non 0 payload length 119078064Sume * must not contain a jumbo payload option. 119162587Sitojun */ 119262587Sitojun ip6 = mtod(m, struct ip6_hdr *); 119362587Sitojun if (ip6->ip6_plen) { 1194250044Sae IP6STAT_INC(ip6s_badoptions); 119562587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 1196120913Sume ICMP6_PARAMPROB_HEADER, 1197120913Sume erroff + opt - opthead); 1198120856Sume return (-1); 119962587Sitojun } 120053541Sshin 120162587Sitojun /* 120262587Sitojun * We may see jumbolen in unaligned location, so 120362587Sitojun * we'd need to perform bcopy(). 120462587Sitojun */ 120562587Sitojun bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 120662587Sitojun jumboplen = (u_int32_t)htonl(jumboplen); 120762587Sitojun 120862587Sitojun#if 1 120962587Sitojun /* 121062587Sitojun * if there are multiple jumbo payload options, 121162587Sitojun * *plenp will be non-zero and the packet will be 121262587Sitojun * rejected. 121362587Sitojun * the behavior may need some debate in ipngwg - 121462587Sitojun * multiple options does not make sense, however, 121562587Sitojun * there's no explicit mention in specification. 121662587Sitojun */ 121762587Sitojun if (*plenp != 0) { 1218250044Sae IP6STAT_INC(ip6s_badoptions); 121962587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 1220120913Sume ICMP6_PARAMPROB_HEADER, 1221120913Sume erroff + opt + 2 - opthead); 1222120856Sume return (-1); 122362587Sitojun } 122462587Sitojun#endif 122562587Sitojun 122662587Sitojun /* 122762587Sitojun * jumbo payload length must be larger than 65535. 122862587Sitojun */ 122962587Sitojun if (jumboplen <= IPV6_MAXPACKET) { 1230250044Sae IP6STAT_INC(ip6s_badoptions); 123162587Sitojun icmp6_error(m, ICMP6_PARAM_PROB, 1232120913Sume ICMP6_PARAMPROB_HEADER, 1233120913Sume erroff + opt + 2 - opthead); 1234120856Sume return (-1); 123562587Sitojun } 123662587Sitojun *plenp = jumboplen; 123762587Sitojun 123862587Sitojun break; 123978064Sume default: /* unknown option */ 124078064Sume if (hbhlen < IP6OPT_MINLEN) { 1241250044Sae IP6STAT_INC(ip6s_toosmall); 124278064Sume goto bad; 124378064Sume } 124478064Sume optlen = ip6_unknown_opt(opt, m, 124578064Sume erroff + opt - opthead); 124678064Sume if (optlen == -1) 1247120856Sume return (-1); 124878064Sume optlen += 2; 124978064Sume break; 125053541Sshin } 125153541Sshin } 125253541Sshin 1253120856Sume return (0); 125453541Sshin 125553541Sshin bad: 125653541Sshin m_freem(m); 1257120856Sume return (-1); 125853541Sshin} 125953541Sshin 126053541Sshin/* 126153541Sshin * Unknown option processing. 126253541Sshin * The third argument `off' is the offset from the IPv6 header to the option, 126353541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header 1264215956Sbrucec * is not contiguous in order to return an ICMPv6 error. 126553541Sshin */ 126653541Sshinint 1267171259Sdelphijip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off) 126853541Sshin{ 126953541Sshin struct ip6_hdr *ip6; 127053541Sshin 127178064Sume switch (IP6OPT_TYPE(*optp)) { 127278064Sume case IP6OPT_TYPE_SKIP: /* ignore the option */ 1273120856Sume return ((int)*(optp + 1)); 127478064Sume case IP6OPT_TYPE_DISCARD: /* silently discard */ 127578064Sume m_freem(m); 1276120856Sume return (-1); 127778064Sume case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1278250044Sae IP6STAT_INC(ip6s_badoptions); 127978064Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1280120856Sume return (-1); 128178064Sume case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1282250044Sae IP6STAT_INC(ip6s_badoptions); 128378064Sume ip6 = mtod(m, struct ip6_hdr *); 128478064Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 128578064Sume (m->m_flags & (M_BCAST|M_MCAST))) 128678064Sume m_freem(m); 128778064Sume else 128878064Sume icmp6_error(m, ICMP6_PARAM_PROB, 128978064Sume ICMP6_PARAMPROB_OPTION, off); 1290120856Sume return (-1); 129153541Sshin } 129253541Sshin 129353541Sshin m_freem(m); /* XXX: NOTREACHED */ 1294120856Sume return (-1); 129553541Sshin} 129653541Sshin 129753541Sshin/* 129862587Sitojun * Create the "control" list for this pcb. 1299179289Sbz * These functions will not modify mbuf chain at all. 130062587Sitojun * 1301179289Sbz * With KAME mbuf chain restriction: 130262587Sitojun * The routine will be called from upper layer handlers like tcp6_input(). 130362587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already 130462587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 130562587Sitojun * very first mbuf on the mbuf chain. 1306179289Sbz * 1307179289Sbz * ip6_savecontrol_v4 will handle those options that are possible to be 1308179289Sbz * set on a v4-mapped socket. 1309179289Sbz * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those 1310179289Sbz * options and handle the v6-only ones itself. 131153541Sshin */ 1312181782Sbzstruct mbuf ** 1313181782Sbzip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp, 1314181782Sbz int *v4only) 131553541Sshin{ 1316121674Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 131753541Sshin 131878064Sume#ifdef SO_TIMESTAMP 1319179289Sbz if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) { 132053541Sshin struct timeval tv; 132153541Sshin 132253541Sshin microtime(&tv); 132353541Sshin *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1324120913Sume SCM_TIMESTAMP, SOL_SOCKET); 1325121472Sume if (*mp) 132653541Sshin mp = &(*mp)->m_next; 132797658Stanimura } 132862587Sitojun#endif 132962587Sitojun 1330179289Sbz#define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y)) 133153541Sshin /* RFC 2292 sec. 5 */ 1332179289Sbz if ((inp->inp_flags & IN6P_PKTINFO) != 0) { 133353541Sshin struct in6_pktinfo pi6; 1334120913Sume 1335237909Stuexen if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1336237909Stuexen#ifdef INET 1337237909Stuexen struct ip *ip; 1338237909Stuexen 1339237909Stuexen ip = mtod(m, struct ip *); 1340237909Stuexen pi6.ipi6_addr.s6_addr32[0] = 0; 1341237909Stuexen pi6.ipi6_addr.s6_addr32[1] = 0; 1342237909Stuexen pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 1343237909Stuexen pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr; 1344237909Stuexen#else 1345237909Stuexen /* We won't hit this code */ 1346237909Stuexen bzero(&pi6.ipi6_addr, sizeof(struct in6_addr)); 1347237909Stuexen#endif 1348237909Stuexen } else { 1349237909Stuexen bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1350237909Stuexen in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1351237909Stuexen } 1352120913Sume pi6.ipi6_ifindex = 1353120913Sume (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1354120913Sume 135553541Sshin *mp = sbcreatecontrol((caddr_t) &pi6, 1356120913Sume sizeof(struct in6_pktinfo), 1357179289Sbz IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1358121472Sume if (*mp) 135953541Sshin mp = &(*mp)->m_next; 136053541Sshin } 136178064Sume 1362179289Sbz if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) { 1363237909Stuexen int hlim; 1364120913Sume 1365237909Stuexen if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1366237909Stuexen#ifdef INET 1367237909Stuexen struct ip *ip; 1368237909Stuexen 1369237909Stuexen ip = mtod(m, struct ip *); 1370237909Stuexen hlim = ip->ip_ttl; 1371237909Stuexen#else 1372237909Stuexen /* We won't hit this code */ 1373237909Stuexen hlim = 0; 1374237909Stuexen#endif 1375237909Stuexen } else { 1376237909Stuexen hlim = ip6->ip6_hlim & 0xff; 1377237909Stuexen } 1378120913Sume *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1379179289Sbz IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), 1380179289Sbz IPPROTO_IPV6); 1381121472Sume if (*mp) 138253541Sshin mp = &(*mp)->m_next; 138353541Sshin } 138453541Sshin 1385237909Stuexen if ((inp->inp_flags & IN6P_TCLASS) != 0) { 1386237909Stuexen int tclass; 1387237909Stuexen 1388237909Stuexen if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1389237909Stuexen#ifdef INET 1390237909Stuexen struct ip *ip; 1391237909Stuexen 1392237909Stuexen ip = mtod(m, struct ip *); 1393237909Stuexen tclass = ip->ip_tos; 1394237909Stuexen#else 1395237909Stuexen /* We won't hit this code */ 1396237909Stuexen tclass = 0; 1397237909Stuexen#endif 1398237909Stuexen } else { 1399237909Stuexen u_int32_t flowinfo; 1400237909Stuexen 1401237909Stuexen flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1402237909Stuexen flowinfo >>= 20; 1403237909Stuexen tclass = flowinfo & 0xff; 1404237909Stuexen } 1405237909Stuexen *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int), 1406237909Stuexen IPV6_TCLASS, IPPROTO_IPV6); 1407237909Stuexen if (*mp) 1408237909Stuexen mp = &(*mp)->m_next; 1409237909Stuexen } 1410237909Stuexen 1411237909Stuexen if (v4only != NULL) { 1412237909Stuexen if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1413237909Stuexen *v4only = 1; 1414237909Stuexen } else { 1415237909Stuexen *v4only = 0; 1416237909Stuexen } 1417237909Stuexen } 1418237909Stuexen 1419181782Sbz return (mp); 1420179289Sbz} 1421179289Sbz 1422179289Sbzvoid 1423179289Sbzip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp) 1424179289Sbz{ 1425179289Sbz struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1426181782Sbz int v4only = 0; 1427179289Sbz 1428181782Sbz mp = ip6_savecontrol_v4(in6p, m, mp, &v4only); 1429181782Sbz if (v4only) 1430179289Sbz return; 1431179289Sbz 143253541Sshin /* 1433130002Sume * IPV6_HOPOPTS socket option. Recall that we required super-user 1434130002Sume * privilege for the option (see ip6_ctloutput), but it might be too 1435130002Sume * strict, since there might be some hop-by-hop options which can be 1436130002Sume * returned to normal user. 1437130002Sume * See also RFC 2292 section 6 (or RFC 3542 section 8). 143853541Sshin */ 1439186141Sbz if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) { 144053541Sshin /* 144153541Sshin * Check if a hop-by-hop options header is contatined in the 144253541Sshin * received packet, and if so, store the options as ancillary 144353541Sshin * data. Note that a hop-by-hop options header must be 1444120913Sume * just after the IPv6 header, which is assured through the 1445120913Sume * IPv6 input processing. 144653541Sshin */ 144753541Sshin if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 144862587Sitojun struct ip6_hbh *hbh; 144978064Sume int hbhlen = 0; 145078064Sume#ifdef PULLDOWN_TEST 145178064Sume struct mbuf *ext; 145278064Sume#endif 145353541Sshin 145462587Sitojun#ifndef PULLDOWN_TEST 145562587Sitojun hbh = (struct ip6_hbh *)(ip6 + 1); 145662587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 145762587Sitojun#else 145878064Sume ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 145978064Sume ip6->ip6_nxt); 146078064Sume if (ext == NULL) { 1461250044Sae IP6STAT_INC(ip6s_tooshort); 146262587Sitojun return; 146362587Sitojun } 146478064Sume hbh = mtod(ext, struct ip6_hbh *); 146562587Sitojun hbhlen = (hbh->ip6h_len + 1) << 3; 146678064Sume if (hbhlen != ext->m_len) { 146778064Sume m_freem(ext); 1468250044Sae IP6STAT_INC(ip6s_tooshort); 146962587Sitojun return; 147062587Sitojun } 147162587Sitojun#endif 147262587Sitojun 147353541Sshin /* 1474120913Sume * XXX: We copy the whole header even if a 1475120913Sume * jumbo payload option is included, the option which 1476120913Sume * is to be removed before returning according to 1477120913Sume * RFC2292. 1478148169Sume * Note: this constraint is removed in RFC3542 147953541Sshin */ 148062587Sitojun *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1481179289Sbz IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1482121472Sume IPPROTO_IPV6); 1483121472Sume if (*mp) 148453541Sshin mp = &(*mp)->m_next; 148578064Sume#ifdef PULLDOWN_TEST 148678064Sume m_freem(ext); 148778064Sume#endif 148853541Sshin } 148953541Sshin } 149053541Sshin 1491186141Sbz if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 149278064Sume int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 149353541Sshin 149453541Sshin /* 149553541Sshin * Search for destination options headers or routing 149653541Sshin * header(s) through the header chain, and stores each 149753541Sshin * header as ancillary data. 149853541Sshin * Note that the order of the headers remains in 149953541Sshin * the chain of ancillary data. 150053541Sshin */ 150178064Sume while (1) { /* is explicit loop prevention necessary? */ 150278064Sume struct ip6_ext *ip6e = NULL; 150362587Sitojun int elen; 150478064Sume#ifdef PULLDOWN_TEST 150578064Sume struct mbuf *ext = NULL; 150678064Sume#endif 150753541Sshin 150878064Sume /* 150978064Sume * if it is not an extension header, don't try to 151078064Sume * pull it from the chain. 151178064Sume */ 151278064Sume switch (nxt) { 151378064Sume case IPPROTO_DSTOPTS: 151478064Sume case IPPROTO_ROUTING: 151578064Sume case IPPROTO_HOPOPTS: 151678064Sume case IPPROTO_AH: /* is it possible? */ 151778064Sume break; 151878064Sume default: 151978064Sume goto loopend; 152078064Sume } 152178064Sume 152262587Sitojun#ifndef PULLDOWN_TEST 152378064Sume if (off + sizeof(*ip6e) > m->m_len) 152478064Sume goto loopend; 152562587Sitojun ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 152662587Sitojun if (nxt == IPPROTO_AH) 152762587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 152862587Sitojun else 152962587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 153078064Sume if (off + elen > m->m_len) 153178064Sume goto loopend; 153262587Sitojun#else 153378064Sume ext = ip6_pullexthdr(m, off, nxt); 153478064Sume if (ext == NULL) { 1535250044Sae IP6STAT_INC(ip6s_tooshort); 153662587Sitojun return; 153762587Sitojun } 153878064Sume ip6e = mtod(ext, struct ip6_ext *); 153962587Sitojun if (nxt == IPPROTO_AH) 154062587Sitojun elen = (ip6e->ip6e_len + 2) << 2; 154162587Sitojun else 154262587Sitojun elen = (ip6e->ip6e_len + 1) << 3; 154378064Sume if (elen != ext->m_len) { 154478064Sume m_freem(ext); 1545250044Sae IP6STAT_INC(ip6s_tooshort); 154662587Sitojun return; 154762587Sitojun } 154862587Sitojun#endif 154962587Sitojun 155078064Sume switch (nxt) { 155178064Sume case IPPROTO_DSTOPTS: 1552186141Sbz if (!(in6p->inp_flags & IN6P_DSTOPTS)) 155378064Sume break; 155453541Sshin 155578064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1556179289Sbz IS2292(in6p, 1557179289Sbz IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1558120913Sume IPPROTO_IPV6); 155978064Sume if (*mp) 156078064Sume mp = &(*mp)->m_next; 156178064Sume break; 156278064Sume case IPPROTO_ROUTING: 1563201794Strasz if (!(in6p->inp_flags & IN6P_RTHDR)) 156478064Sume break; 156553541Sshin 156678064Sume *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1567179289Sbz IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR), 1568120913Sume IPPROTO_IPV6); 156978064Sume if (*mp) 157078064Sume mp = &(*mp)->m_next; 157178064Sume break; 157278064Sume case IPPROTO_HOPOPTS: 157378064Sume case IPPROTO_AH: /* is it possible? */ 157478064Sume break; 157553541Sshin 157678064Sume default: 157778064Sume /* 1578171260Sdelphij * other cases have been filtered in the above. 157978064Sume * none will visit this case. here we supply 158078064Sume * the code just in case (nxt overwritten or 158178064Sume * other cases). 158278064Sume */ 158378064Sume#ifdef PULLDOWN_TEST 158478064Sume m_freem(ext); 158578064Sume#endif 158678064Sume goto loopend; 158753541Sshin 158853541Sshin } 158953541Sshin 159053541Sshin /* proceed with the next header. */ 159162587Sitojun off += elen; 159253541Sshin nxt = ip6e->ip6e_nxt; 159378064Sume ip6e = NULL; 159478064Sume#ifdef PULLDOWN_TEST 159578064Sume m_freem(ext); 159678064Sume ext = NULL; 159778064Sume#endif 159853541Sshin } 159953541Sshin loopend: 160078064Sume ; 160153541Sshin } 1602179289Sbz} 1603121472Sume#undef IS2292 160478064Sume 1605125776Sumevoid 1606171259Sdelphijip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu) 1607125776Sume{ 1608125776Sume struct socket *so; 1609125776Sume struct mbuf *m_mtu; 1610125776Sume struct ip6_mtuinfo mtuctl; 1611125776Sume 1612125776Sume so = in6p->inp_socket; 1613125776Sume 1614125776Sume if (mtu == NULL) 1615125776Sume return; 1616125776Sume 1617125776Sume#ifdef DIAGNOSTIC 1618125776Sume if (so == NULL) /* I believe this is impossible */ 1619125776Sume panic("ip6_notify_pmtu: socket is NULL"); 1620125776Sume#endif 1621125776Sume 1622125776Sume bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */ 1623125776Sume mtuctl.ip6m_mtu = *mtu; 1624125776Sume mtuctl.ip6m_addr = *dst; 1625148385Sume if (sa6_recoverscope(&mtuctl.ip6m_addr)) 1626148385Sume return; 1627125776Sume 1628125776Sume if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1629125776Sume IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1630125776Sume return; 1631125776Sume 1632125776Sume if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1633125776Sume == 0) { 1634125776Sume m_freem(m_mtu); 1635125776Sume /* XXX: should count statistics */ 1636125776Sume } else 1637125776Sume sorwakeup(so); 1638125776Sume 1639125776Sume return; 1640125776Sume} 1641125776Sume 164278064Sume#ifdef PULLDOWN_TEST 164378064Sume/* 164478064Sume * pull single extension header from mbuf chain. returns single mbuf that 164578064Sume * contains the result, or NULL on error. 164678064Sume */ 164778064Sumestatic struct mbuf * 1648171259Sdelphijip6_pullexthdr(struct mbuf *m, size_t off, int nxt) 164978064Sume{ 165078064Sume struct ip6_ext ip6e; 165178064Sume size_t elen; 165278064Sume struct mbuf *n; 165378064Sume 165478064Sume#ifdef DIAGNOSTIC 165578064Sume switch (nxt) { 165678064Sume case IPPROTO_DSTOPTS: 165778064Sume case IPPROTO_ROUTING: 165878064Sume case IPPROTO_HOPOPTS: 165978064Sume case IPPROTO_AH: /* is it possible? */ 166078064Sume break; 166178064Sume default: 166278064Sume printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 166353541Sshin } 166478064Sume#endif 166578064Sume 166678064Sume m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 166778064Sume if (nxt == IPPROTO_AH) 166878064Sume elen = (ip6e.ip6e_len + 2) << 2; 166978064Sume else 167078064Sume elen = (ip6e.ip6e_len + 1) << 3; 167178064Sume 1672111119Simp MGET(n, M_DONTWAIT, MT_DATA); 167378064Sume if (n && elen >= MLEN) { 1674111119Simp MCLGET(n, M_DONTWAIT); 167578064Sume if ((n->m_flags & M_EXT) == 0) { 167678064Sume m_free(n); 167778064Sume n = NULL; 167878064Sume } 167953541Sshin } 168078064Sume if (!n) 168178064Sume return NULL; 168262587Sitojun 168378064Sume n->m_len = 0; 168478064Sume if (elen >= M_TRAILINGSPACE(n)) { 168578064Sume m_free(n); 168678064Sume return NULL; 168778064Sume } 168878064Sume 168978064Sume m_copydata(m, off, elen, mtod(n, caddr_t)); 169078064Sume n->m_len = elen; 169178064Sume return n; 169253541Sshin} 169378064Sume#endif 169453541Sshin 169553541Sshin/* 169653541Sshin * Get pointer to the previous header followed by the header 169753541Sshin * currently processed. 169853541Sshin * XXX: This function supposes that 169953541Sshin * M includes all headers, 170053541Sshin * the next header field and the header length field of each header 170153541Sshin * are valid, and 170253541Sshin * the sum of each header length equals to OFF. 170353541Sshin * Because of these assumptions, this function must be called very 170453541Sshin * carefully. Moreover, it will not be used in the near future when 170553541Sshin * we develop `neater' mechanism to process extension headers. 170653541Sshin */ 170753541Sshinchar * 1708171259Sdelphijip6_get_prevhdr(struct mbuf *m, int off) 170953541Sshin{ 171053541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 171153541Sshin 171253541Sshin if (off == sizeof(struct ip6_hdr)) 1713120856Sume return (&ip6->ip6_nxt); 171453541Sshin else { 171553541Sshin int len, nxt; 171653541Sshin struct ip6_ext *ip6e = NULL; 171753541Sshin 171853541Sshin nxt = ip6->ip6_nxt; 171953541Sshin len = sizeof(struct ip6_hdr); 172053541Sshin while (len < off) { 172153541Sshin ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 172253541Sshin 172378064Sume switch (nxt) { 172453541Sshin case IPPROTO_FRAGMENT: 172553541Sshin len += sizeof(struct ip6_frag); 172653541Sshin break; 172753541Sshin case IPPROTO_AH: 172853541Sshin len += (ip6e->ip6e_len + 2) << 2; 172953541Sshin break; 173053541Sshin default: 173153541Sshin len += (ip6e->ip6e_len + 1) << 3; 173253541Sshin break; 173353541Sshin } 173453541Sshin nxt = ip6e->ip6e_nxt; 173553541Sshin } 173653541Sshin if (ip6e) 1737120856Sume return (&ip6e->ip6e_nxt); 173853541Sshin else 173953541Sshin return NULL; 174053541Sshin } 174153541Sshin} 174253541Sshin 174353541Sshin/* 174462587Sitojun * get next header offset. m will be retained. 174562587Sitojun */ 174662587Sitojunint 1747171259Sdelphijip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp) 174862587Sitojun{ 174962587Sitojun struct ip6_hdr ip6; 175062587Sitojun struct ip6_ext ip6e; 175162587Sitojun struct ip6_frag fh; 175262587Sitojun 175362587Sitojun /* just in case */ 175462587Sitojun if (m == NULL) 175562587Sitojun panic("ip6_nexthdr: m == NULL"); 175662587Sitojun if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 175762587Sitojun return -1; 175862587Sitojun 175962587Sitojun switch (proto) { 176062587Sitojun case IPPROTO_IPV6: 176162587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6)) 176262587Sitojun return -1; 176362587Sitojun m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 176462587Sitojun if (nxtp) 176562587Sitojun *nxtp = ip6.ip6_nxt; 176662587Sitojun off += sizeof(ip6); 176762587Sitojun return off; 176862587Sitojun 176962587Sitojun case IPPROTO_FRAGMENT: 177062587Sitojun /* 177162587Sitojun * terminate parsing if it is not the first fragment, 177262587Sitojun * it does not make sense to parse through it. 177362587Sitojun */ 177462587Sitojun if (m->m_pkthdr.len < off + sizeof(fh)) 177562587Sitojun return -1; 177662587Sitojun m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1777120978Sume /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1778120978Sume if (fh.ip6f_offlg & IP6F_OFF_MASK) 177962587Sitojun return -1; 178062587Sitojun if (nxtp) 178162587Sitojun *nxtp = fh.ip6f_nxt; 178262587Sitojun off += sizeof(struct ip6_frag); 178362587Sitojun return off; 178462587Sitojun 178562587Sitojun case IPPROTO_AH: 178662587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6e)) 178762587Sitojun return -1; 178862587Sitojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 178962587Sitojun if (nxtp) 179062587Sitojun *nxtp = ip6e.ip6e_nxt; 179162587Sitojun off += (ip6e.ip6e_len + 2) << 2; 179262587Sitojun return off; 179362587Sitojun 179462587Sitojun case IPPROTO_HOPOPTS: 179562587Sitojun case IPPROTO_ROUTING: 179662587Sitojun case IPPROTO_DSTOPTS: 179762587Sitojun if (m->m_pkthdr.len < off + sizeof(ip6e)) 179862587Sitojun return -1; 179962587Sitojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 180062587Sitojun if (nxtp) 180162587Sitojun *nxtp = ip6e.ip6e_nxt; 180262587Sitojun off += (ip6e.ip6e_len + 1) << 3; 180362587Sitojun return off; 180462587Sitojun 180562587Sitojun case IPPROTO_NONE: 180662587Sitojun case IPPROTO_ESP: 180762587Sitojun case IPPROTO_IPCOMP: 180862587Sitojun /* give up */ 180962587Sitojun return -1; 181062587Sitojun 181162587Sitojun default: 181262587Sitojun return -1; 181362587Sitojun } 181462587Sitojun 181562587Sitojun return -1; 181662587Sitojun} 181762587Sitojun 181862587Sitojun/* 181962587Sitojun * get offset for the last header in the chain. m will be kept untainted. 182062587Sitojun */ 182162587Sitojunint 1822171259Sdelphijip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp) 182362587Sitojun{ 182462587Sitojun int newoff; 182562587Sitojun int nxt; 182662587Sitojun 182762587Sitojun if (!nxtp) { 182862587Sitojun nxt = -1; 182962587Sitojun nxtp = &nxt; 183062587Sitojun } 183162587Sitojun while (1) { 183262587Sitojun newoff = ip6_nexthdr(m, off, proto, nxtp); 183362587Sitojun if (newoff < 0) 183462587Sitojun return off; 183562587Sitojun else if (newoff < off) 183662587Sitojun return -1; /* invalid */ 183762587Sitojun else if (newoff == off) 183862587Sitojun return newoff; 183962587Sitojun 184062587Sitojun off = newoff; 184162587Sitojun proto = *nxtp; 184262587Sitojun } 184362587Sitojun} 184462587Sitojun 1845238233Sbzstatic struct ip6aux * 1846171259Sdelphijip6_addaux(struct mbuf *m) 184778064Sume{ 1848120913Sume struct m_tag *mtag; 1849120913Sume 1850120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1851120913Sume if (!mtag) { 1852120913Sume mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux), 1853120913Sume M_NOWAIT); 1854121630Sume if (mtag) { 1855120913Sume m_tag_prepend(m, mtag); 1856121630Sume bzero(mtag + 1, sizeof(struct ip6aux)); 1857121630Sume } 185878064Sume } 1859121673Sume return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 186078064Sume} 186178064Sume 1862238233Sbzstatic struct ip6aux * 1863171259Sdelphijip6_findaux(struct mbuf *m) 186478064Sume{ 1865120913Sume struct m_tag *mtag; 1866120913Sume 1867120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1868121673Sume return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 186978064Sume} 187078064Sume 1871238233Sbzstatic void 1872171259Sdelphijip6_delaux(struct mbuf *m) 187378064Sume{ 1874120913Sume struct m_tag *mtag; 1875120913Sume 1876120913Sume mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1877120913Sume if (mtag) 1878120913Sume m_tag_delete(m, mtag); 187978064Sume} 188078064Sume 188162587Sitojun/* 188253541Sshin * System control for IP6 188353541Sshin */ 188453541Sshin 188553541Sshinu_char inet6ctlerrmap[PRC_NCMDS] = { 188653541Sshin 0, 0, 0, 0, 188753541Sshin 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 188853541Sshin EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 188953541Sshin EMSGSIZE, EHOSTUNREACH, 0, 0, 189053541Sshin 0, 0, 0, 0, 189153541Sshin ENOPROTOOPT 189253541Sshin}; 1893