]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/netinet6/ip6_input.c
xnu-1504.3.12.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_input.c
index 2dc986a66202cfc6393f2495df3286f94ec1396e..cdf3776b2b18ceca165d4f4950af1b774c2a1501 100644 (file)
@@ -1,3 +1,30 @@
+/*
+ * Copyright (c) 2003-2008 Apple Inc. All rights reserved.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
+ * 
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ * 
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
+ */
 /*     $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.10 2001/07/24 19:10:18 brooks Exp $     */
 /*     $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $     */
 
 #include <sys/kernel.h>
 #include <sys/syslog.h>
 #include <sys/proc.h>
+#include <sys/kauth.h>
 
 #include <net/if.h>
 #include <net/if_var.h>
 #include <net/if_types.h>
 #include <net/if_dl.h>
 #include <net/route.h>
-#include <net/netisr.h>
+#include <net/kpi_protocol.h>
 
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
@@ -112,6 +140,8 @@ extern int ipsec_bypass;
 
 #include <netinet6/ip6_fw.h>
 
+#include <netinet/kpi_ipfilter_var.h>
+
 #include <netinet6/ip6protosw.h>
 
 /* we need it for NLOOP. */
@@ -120,16 +150,16 @@ extern int ipsec_bypass;
 
 #include <net/net_osdep.h>
 
+#if PF
+#include <net/pfvar.h>
+#endif /* PF */
+
 extern struct domain inet6domain;
 extern struct ip6protosw inet6sw[];
 
 struct ip6protosw *  ip6_protox[IPPROTO_MAX];
 static int ip6qmaxlen = IFQ_MAXLEN;
-struct in6_ifaddr *in6_ifaddr;
-
-extern void in6_tmpaddrtimer_funneled(void *);
-extern void nd6_timer_funneled(void *);
-extern void in6_rr_timer_funneled(void *);
+struct in6_ifaddr *in6_ifaddrs;
 
 int ip6_forward_srcrt;                 /* XXX */
 int ip6_sourcecheck;                   /* XXX */
@@ -140,31 +170,54 @@ int ip6_ours_check_algorithm;
 int in6_init2done = 0;
 
 
+#if IPFW2
 /* firewall hooks */
 ip6_fw_chk_t *ip6_fw_chk_ptr;
 ip6_fw_ctl_t *ip6_fw_ctl_ptr;
 int ip6_fw_enable = 1;
+#endif
 
 struct ip6stat ip6stat;
 
 #ifdef __APPLE__
 struct ifqueue ip6intrq;
+lck_mtx_t              *ip6_mutex;
+lck_mtx_t              *dad6_mutex;
+lck_mtx_t              *nd6_mutex;
+lck_mtx_t              *prefix6_mutex;
+lck_mtx_t              *scope6_mutex;
+lck_attr_t             *ip6_mutex_attr;
+lck_grp_t              *ip6_mutex_grp;
+lck_grp_attr_t         *ip6_mutex_grp_attr;
+extern lck_mtx_t       *inet6_domain_mutex;
 #endif
+extern int loopattach_done;
 
-static void ip6_init2 __P((void *));
-static struct mbuf *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
+static void ip6_init2(void *);
+static struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *);
 
-static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
+static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
 #if PULLDOWN_TEST
-static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
+static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
 #endif
 
 #ifdef __APPLE__
-void gifattach __P((void));
-void faithattach __P((void));
-void stfattach __P((void));
+void gifattach(void);
+void faithattach(void);
+void stfattach(void);
 #endif
 
+extern lck_mtx_t *domain_proto_mtx;
+
+
+static void
+ip6_proto_input(
+       __unused protocol_family_t      protocol,
+       mbuf_t                          packet)
+{
+       ip6_input(packet);
+}
+
 /*
  * IP6 initialization: fill in IP6 protocol switch table.
  * All protocols not implemented in kernel go to raw IP6 protocol handler.
@@ -180,23 +233,47 @@ ip6_init()
        if (sizeof(struct protosw) != sizeof(struct ip6protosw))
                panic("sizeof(protosw) != sizeof(ip6protosw)");
 #endif
-       pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
+       pr = (struct ip6protosw *)pffindproto_locked(PF_INET6, IPPROTO_RAW, SOCK_RAW);
        if (pr == 0)
                panic("ip6_init");
        for (i = 0; i < IPPROTO_MAX; i++)
                ip6_protox[i] = pr;
        for (pr = (struct ip6protosw*)inet6domain.dom_protosw; pr; pr = pr->pr_next) {
-               if(!((unsigned int)pr->pr_domain)) continue;    /* If uninitialized, skip */
+               if(!(pr->pr_domain)) continue;    /* If uninitialized, skip */
                if (pr->pr_domain->dom_family == PF_INET6 &&
                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
                        ip6_protox[pr->pr_protocol] = pr;
                }
        }
 
+       ip6_mutex_grp_attr  = lck_grp_attr_alloc_init();
+
+       ip6_mutex_grp = lck_grp_alloc_init("ip6", ip6_mutex_grp_attr);
+       ip6_mutex_attr = lck_attr_alloc_init();
+
+       if ((ip6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
+               panic("ip6_init: can't alloc ip6_mutex\n");
+       }
+       if ((dad6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
+               panic("ip6_init: can't alloc dad6_mutex\n");
+       }
+       if ((nd6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
+               panic("ip6_init: can't alloc nd6_mutex\n");
+       }
+
+       if ((prefix6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
+               panic("ip6_init: can't alloc prefix6_mutex\n");
+       }
+
+       if ((scope6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) {
+               panic("ip6_init: can't alloc scope6_mutex\n");
+       }
+
+
+       inet6domain.dom_flags = DOM_REENTRANT;  
+
        ip6intrq.ifq_maxlen = ip6qmaxlen;
-#ifndef __APPLE__
-       register_netisr(NETISR_IPV6, ip6intr);
-#endif
+       in6_ifaddr_init();
        nd6_init();
        frag6_init();
        icmp6_init();
@@ -208,32 +285,36 @@ ip6_init()
        ip6_flow_seq = random() ^ tv.tv_usec;
        microtime(&tv);
        ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR;
-       timeout(ip6_init2, (caddr_t)0, 2 * hz);
+       timeout(ip6_init2, (caddr_t)0, 1 * hz);
+
+       lck_mtx_unlock(domain_proto_mtx);       
+       proto_register_input(PF_INET6, ip6_proto_input, NULL, 0);
+       lck_mtx_lock(domain_proto_mtx); 
 }
 
 static void
-ip6_init2(dummy)
-       void *dummy;
+ip6_init2(
+       __unused void *dummy)
 {
-#ifdef __APPLE__
-       boolean_t   funnel_state;
-       funnel_state = thread_funnel_set(network_flock, TRUE);
-#endif
        /*
         * to route local address of p2p link to loopback,
         * assign loopback address first.
         */
-       in6_ifattach(&loif[0], NULL, NULL);
+       if (loopattach_done == 0) {
+               timeout(ip6_init2, (caddr_t)0, 1 * hz);
+               return;
+       }
+       (void) in6_ifattach(lo_ifp, NULL, NULL);
 
 #ifdef __APPLE__
        /* nd6_timer_init */
-       timeout(nd6_timer_funneled, (caddr_t)0, hz);
+       timeout(nd6_timer, (caddr_t)0, hz);
 
        /* router renumbering prefix list maintenance */
-       timeout(in6_rr_timer_funneled, (caddr_t)0, hz);
+       timeout(in6_rr_timer, (caddr_t)0, hz);
 
        /* timer for regeneranation of temporary addresses randomize ID */
-       timeout(in6_tmpaddrtimer_funneled, (caddr_t)0,
+       timeout(in6_tmpaddrtimer, (caddr_t)0,
                (ip6_temp_preferred_lifetime - ip6_desync_factor -
                       ip6_temp_regen_advance) * hz);
 
@@ -264,9 +345,6 @@ ip6_init2(dummy)
 #endif
 
        in6_init2done = 1;
-#ifdef __APPLE__
-        (void) thread_funnel_set(network_flock, FALSE);
-#endif
 }
 
 #if __FreeBSD__
@@ -276,25 +354,11 @@ SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
 #endif
 
 /*
- * IP6 input interrupt handling. Just pass the packet to ip6_input.
+ * ip6_forward_rt contains the route entry that was recently used during
+ * the forwarding of an IPv6 packet and thus acts as a route cache.  Access
+ * to this variable is protected by the global lock ip6_mutex.
  */
-void
-ip6intr(void)
-{
-       int s;
-       struct mbuf *m;
-
-       for (;;) {
-               s = splimp();
-               IF_DEQUEUE(&ip6intrq, m);
-               splx(s);
-               if (m == 0)
-                       return;
-               ip6_input(m);
-       }
-}
-
-extern struct  route_in6 ip6_forward_rt;
+static struct route_in6 ip6_forward_rt;
 
 void
 ip6_input(m)
@@ -306,7 +370,22 @@ ip6_input(m)
        u_int32_t rtalert = ~0;
        int nxt = 0, ours = 0;
        struct ifnet *deliverifp = NULL;
+       ipfilter_t inject_ipfref = 0;
+       int seen;
 
+       /*
+        * No need to proccess packet twice if we've 
+        * already seen it
+        */
+       inject_ipfref = ipf_get_inject_filter(m);
+       if (inject_ipfref != 0) {
+               ip6 = mtod(m, struct ip6_hdr *);
+               nxt = ip6->ip6_nxt;
+               seen = 0;
+               goto injectit;
+       } else
+               seen = 1;
+       
 #if IPSEC
        /*
         * should the inner packet be considered authentic?
@@ -323,6 +402,7 @@ ip6_input(m)
         */
        ip6_delaux(m);
 
+       lck_mtx_lock(ip6_mutex);
        /*
         * mbuf statistics
         */
@@ -335,7 +415,7 @@ ip6_input(m)
 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
                if (m->m_next) {
                        if (m->m_flags & M_LOOP) {
-                               ip6stat.ip6s_m2m[loif[0].if_index]++;   /* XXX */
+                               ip6stat.ip6s_m2m[ifnet_index(lo_ifp)]++;        /* XXX */
                        } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
                                ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
                        else
@@ -357,7 +437,7 @@ ip6_input(m)
        if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
                struct mbuf *n;
 
-               MGETHDR(n, M_DONTWAIT, MT_HEADER);
+               MGETHDR(n, M_DONTWAIT, MT_HEADER);      /* MAC-OK */
                if (n)
                        M_COPY_PKTHDR(n, m);
                if (n && m->m_pkthdr.len > MHLEN) {
@@ -369,6 +449,7 @@ ip6_input(m)
                }
                if (n == NULL) {
                        m_freem(m);
+                       lck_mtx_unlock(ip6_mutex);
                        return; /*ENOBUFS*/
                }
 
@@ -377,7 +458,8 @@ ip6_input(m)
                m_freem(m);
                m = n;
        }
-       IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
+       IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr),
+               {lck_mtx_unlock(ip6_mutex); return;}); 
 #endif
 
        if (m->m_len < sizeof(struct ip6_hdr)) {
@@ -386,6 +468,7 @@ ip6_input(m)
                if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
                        ip6stat.ip6s_toosmall++;
                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
+                       lck_mtx_unlock(ip6_mutex);
                        return;
                }
        }
@@ -400,6 +483,7 @@ ip6_input(m)
 
        ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
 
+#if IPFW2
        /*
         * Check with the firewall...
         */
@@ -411,9 +495,12 @@ ip6_input(m)
                        m_freem(m);
                        m = NULL;
                }
-               if (!m)
+               if (!m) {
+                       lck_mtx_unlock(ip6_mutex);
                        return;
+               }
        }
+#endif
 
        /*
         * Check against address spoofing/corruption.
@@ -469,6 +556,21 @@ ip6_input(m)
        }
 #endif
 
+#if PF
+       /* Invoke inbound packet filter */
+       lck_mtx_unlock(ip6_mutex);
+       if (pf_af_hook(m->m_pkthdr.rcvif, NULL, &m, AF_INET6, TRUE) != 0) {
+               if (m != NULL) {
+                       panic("%s: unexpected packet %p\n", __func__, m);
+                       /* NOTREACHED */
+               }
+               /* Already freed by callee */
+               return;
+       }
+       ip6 = mtod(m, struct ip6_hdr *);
+       lck_mtx_lock(ip6_mutex);
+#endif /* PF */
+
        /* drop packets if interface ID portion is already filled */
        if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
@@ -502,12 +604,15 @@ ip6_input(m)
         */
        if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
            IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
-               if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
+           struct in6_ifaddr *ia6;
+               if (!(ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst))) {
+                       lck_mtx_unlock(ip6_mutex);
                        icmp6_error(m, ICMP6_DST_UNREACH,
                            ICMP6_DST_UNREACH_ADDR, 0);
                        /* m is already freed */
                        return;
                }
+               ifafree(&ia6->ia_ifa);
 
                ours = 1;
                deliverifp = m->m_pkthdr.rcvif;
@@ -519,49 +624,53 @@ ip6_input(m)
         * Multicast check
         */
        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
-               struct  in6_multi *in6m = 0;
+               struct  in6_multi *in6m = 0;
+               struct ifnet *ifp = m->m_pkthdr.rcvif;
 
-               in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
+               in6_ifstat_inc(ifp, ifs6_in_mcast);
                /*
                 * See if we belong to the destination multicast group on the
                 * arrival interface.
                 */
-               IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
+               ifnet_lock_shared(ifp);
+               IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
+               ifnet_lock_done(ifp);
                if (in6m)
                        ours = 1;
+#if MROUTING
                else if (!ip6_mrouter) {
+#else
+               else {
+#endif
                        ip6stat.ip6s_notmember++;
                        ip6stat.ip6s_cantforward++;
-                       in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
+                       in6_ifstat_inc(ifp, ifs6_in_discard);
                        goto bad;
                }
-               deliverifp = m->m_pkthdr.rcvif;
+               deliverifp = ifp;
                goto hbhcheck;
        }
 
+       if (ip6_forward_rt.ro_rt != NULL)
+               RT_LOCK(ip6_forward_rt.ro_rt);
        /*
         *  Unicast check
         */
-       switch (ip6_ours_check_algorithm) {
-       default:
-               /*
-                * XXX: I intentionally broke our indentation rule here,
-                *      since this switch-case is just for measurement and
-                *      therefore should soon be removed.
-                */
        if (ip6_forward_rt.ro_rt != NULL &&
-           (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 
+           (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) &&
            IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
-                              &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
+           &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr) &&
+           ip6_forward_rt.ro_rt->generation_id == route_generation) {
                ip6stat.ip6s_forward_cachehit++;
-       else {
+       else {
                struct sockaddr_in6 *dst6;
 
-               if (ip6_forward_rt.ro_rt) {
-                       /* route is down or destination is different */
+               if (ip6_forward_rt.ro_rt != NULL) {
+                       /* route is down/stale or destination is different */
                        ip6stat.ip6s_forward_cachemiss++;
+                       RT_UNLOCK(ip6_forward_rt.ro_rt);
                        rtfree(ip6_forward_rt.ro_rt);
-                       ip6_forward_rt.ro_rt = 0;
+                       ip6_forward_rt.ro_rt = NULL;
                }
 
                bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
@@ -575,6 +684,8 @@ ip6_input(m)
 #endif
 
                rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
+               if (ip6_forward_rt.ro_rt != NULL)
+                       RT_LOCK(ip6_forward_rt.ro_rt);
        }
 
 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
@@ -599,15 +710,12 @@ ip6_input(m)
         * while it would be less efficient.  Or, should we rather install a
         * reject route for such a case?
         */
-       if (ip6_forward_rt.ro_rt &&
+       if (ip6_forward_rt.ro_rt != NULL &&
            (ip6_forward_rt.ro_rt->rt_flags &
             (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
 #if RTF_WASCLONED
            !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
 #endif
-#if RTF_CLONED
-           !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
-#endif
 #if 0
            /*
             * The check below is redundant since the comparison of
@@ -640,18 +748,18 @@ ip6_input(m)
                        ia6->ia_ifa.if_ipackets++;
                        ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
 #endif
+                       RT_UNLOCK(ip6_forward_rt.ro_rt);
                        goto hbhcheck;
                } else {
+                       RT_UNLOCK(ip6_forward_rt.ro_rt);
                        /* address is not ready, so discard the packet. */
                        nd6log((LOG_INFO,
                            "ip6_input: packet to an unready address %s->%s\n",
                            ip6_sprintf(&ip6->ip6_src),
                            ip6_sprintf(&ip6->ip6_dst)));
-
                        goto bad;
                }
        }
-       } /* XXX indentation (see above) */
 
        /*
         * FAITH(Firewall Aided Internet Translator)
@@ -663,10 +771,13 @@ ip6_input(m)
                        /* XXX do we need more sanity checks? */
                        ours = 1;
                        deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
+                       RT_UNLOCK(ip6_forward_rt.ro_rt);
                        goto hbhcheck;
                }
        }
 #endif
+       if (ip6_forward_rt.ro_rt != NULL)
+               RT_UNLOCK(ip6_forward_rt.ro_rt);
 
        /*
         * Now there is no reason to process the packet if it's not our own
@@ -697,6 +808,7 @@ ip6_input(m)
                                 * to the upper layers.
                                 */
                        }
+                       ifafree(&ia6->ia_ifa);
                }
        }
 
@@ -713,6 +825,7 @@ ip6_input(m)
 #if 0  /*touches NULL pointer*/
                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
 #endif
+                       lck_mtx_unlock(ip6_mutex);
                        return; /* m have already been freed */
                }
 
@@ -733,6 +846,7 @@ ip6_input(m)
                        ip6stat.ip6s_badoptions++;
                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
+                       lck_mtx_unlock(ip6_mutex);
                        icmp6_error(m, ICMP6_PARAM_PROB,
                                    ICMP6_PARAMPROB_HEADER,
                                    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
@@ -746,6 +860,7 @@ ip6_input(m)
                        sizeof(struct ip6_hbh));
                if (hbh == NULL) {
                        ip6stat.ip6s_tooshort++;
+                       lck_mtx_unlock(ip6_mutex);
                        return;
                }
 #endif
@@ -791,17 +906,22 @@ ip6_input(m)
                 * ip6_mforward() returns a non-zero value, the packet
                 * must be discarded, else it may be accepted below.
                 */
+#if MROUTING
                if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
                        ip6stat.ip6s_cantforward++;
                        m_freem(m);
+                       lck_mtx_unlock(ip6_mutex);
                        return;
                }
+#endif
                if (!ours) {
                        m_freem(m);
+                       lck_mtx_unlock(ip6_mutex);
                        return;
                }
        } else if (!ours) {
-               ip6_forward(m, 0);
+               ip6_forward(m, &ip6_forward_rt, 0, 1);
+               lck_mtx_unlock(ip6_mutex);
                return;
        }       
 
@@ -828,12 +948,17 @@ ip6_input(m)
         */
        ip6stat.ip6s_delivered++;
        in6_ifstat_inc(deliverifp, ifs6_in_deliver);
+
+       lck_mtx_unlock(ip6_mutex);
+injectit:
        nest = 0;
 
        while (nxt != IPPROTO_DONE) {
+               struct ipfilter *filter;
+               
                if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
                        ip6stat.ip6s_toomanyhdr++;
-                       goto bad;
+                       goto badunlocked;
                }
 
                /*
@@ -843,7 +968,7 @@ ip6_input(m)
                if (m->m_pkthdr.len < off) {
                        ip6stat.ip6s_tooshort++;
                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
-                       goto bad;
+                       goto badunlocked;
                }
 
 #if 0
@@ -868,46 +993,81 @@ ip6_input(m)
                 * note that we do not visit this with protocols with pcb layer
                 * code - like udp/tcp/raw ip.
                 */
-               if ((ipsec_bypass == 0) && (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0 &&
-                   ipsec6_in_reject(m, NULL)) {
-                       ipsec6stat.in_polvio++;
-                       goto bad;
+               if ((ipsec_bypass == 0) && (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0) {
+                       if (ipsec6_in_reject(m, NULL)) {
+                               IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
+                               goto badunlocked;
+                   }
                }
 #endif
 
-               nxt = (*ip6_protox[nxt]->pr_input)(&m, &off);
+               /*
+                * Call IP filter
+                */
+               if (!TAILQ_EMPTY(&ipv6_filters)) {
+                       ipf_ref();
+                       TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
+                               if (seen == 0) {
+                                       if ((struct ipfilter *)inject_ipfref == filter)
+                                               seen = 1;
+                               } else if (filter->ipf_filter.ipf_input) {
+                                       errno_t result;
+                                       
+                                       result = filter->ipf_filter.ipf_input(
+                                               filter->ipf_filter.cookie, (mbuf_t*)&m, off, nxt);
+                                       if (result == EJUSTRETURN) {
+                                               ipf_unref();
+                                               return;
+                                       }
+                                       if (result != 0) {
+                                               ipf_unref();
+                                               m_freem(m);
+                                               return;
+                                       }
+                               }
+                       }
+                       ipf_unref();
+               }
+               if (!(ip6_protox[nxt]->pr_flags & PR_PROTOLOCK)) {
+                       lck_mtx_lock(inet6_domain_mutex);
+                       nxt = (*ip6_protox[nxt]->pr_input)(&m, &off);
+                       lck_mtx_unlock(inet6_domain_mutex);
+               }
+               else
+                       nxt = (*ip6_protox[nxt]->pr_input)(&m, &off);
        }
        return;
  bad:
+       lck_mtx_unlock(ip6_mutex);
+ badunlocked:
        m_freem(m);
+       return;
 }
 
 /*
  * set/grab in6_ifaddr correspond to IPv6 destination address.
  * XXX backward compatibility wrapper
  */
-static struct mbuf *
-ip6_setdstifaddr(m, ia6)
-       struct mbuf *m;
-       struct in6_ifaddr *ia6;
+static struct ip6aux *
+ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
 {
-       struct mbuf *n;
+       struct ip6aux *n;
 
        n = ip6_addaux(m);
        if (n)
-               mtod(n, struct ip6aux *)->ip6a_dstia6 = ia6;
-       return n;       /* NULL if failed to set */
+               n->ip6a_dstia6 = ia6;
+       return (struct ip6aux *)n;      /* NULL if failed to set */
 }
 
 struct in6_ifaddr *
 ip6_getdstifaddr(m)
        struct mbuf *m;
 {
-       struct mbuf *n;
+       struct ip6aux *n;
 
        n = ip6_findaux(m);
        if (n)
-               return mtod(n, struct ip6aux *)->ip6a_dstia6;
+               return n->ip6a_dstia6;
        else
                return NULL;
 }
@@ -930,11 +1090,11 @@ ip6_hopopts_input(plenp, rtalertp, mp, offp)
 
        /* validation of the length of the header */
 #ifndef PULLDOWN_TEST
-       IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
+       IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), return -1);
        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
        hbhlen = (hbh->ip6h_len + 1) << 3;
 
-       IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
+       IP6_EXTHDR_CHECK(m, off, hbhlen, return -1);
        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
 #else
        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
@@ -1009,9 +1169,11 @@ ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                        }
                        if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
                                /* XXX stat */
+                               lck_mtx_unlock(ip6_mutex);
                                icmp6_error(m, ICMP6_PARAM_PROB,
                                            ICMP6_PARAMPROB_HEADER,
                                            erroff + opt + 1 - opthead);
+                               lck_mtx_lock(ip6_mutex);
                                return(-1);
                        }
                        optlen = IP6OPT_RTALERT_LEN;
@@ -1026,9 +1188,11 @@ ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                        }
                        if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
                                /* XXX stat */
+                               lck_mtx_unlock(ip6_mutex);
                                icmp6_error(m, ICMP6_PARAM_PROB,
                                            ICMP6_PARAMPROB_HEADER,
                                            erroff + opt + 1 - opthead);
+                               lck_mtx_lock(ip6_mutex);
                                return(-1);
                        }
                        optlen = IP6OPT_JUMBO_LEN;
@@ -1040,9 +1204,11 @@ ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                        ip6 = mtod(m, struct ip6_hdr *);
                        if (ip6->ip6_plen) {
                                ip6stat.ip6s_badoptions++;
+                               lck_mtx_unlock(ip6_mutex);
                                icmp6_error(m, ICMP6_PARAM_PROB,
                                            ICMP6_PARAMPROB_HEADER,
                                            erroff + opt - opthead);
+                               lck_mtx_lock(ip6_mutex);
                                return(-1);
                        }
 
@@ -1064,9 +1230,11 @@ ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                         */
                        if (*plenp != 0) {
                                ip6stat.ip6s_badoptions++;
+                               lck_mtx_unlock(ip6_mutex);
                                icmp6_error(m, ICMP6_PARAM_PROB,
                                            ICMP6_PARAMPROB_HEADER,
                                            erroff + opt + 2 - opthead);
+                               lck_mtx_lock(ip6_mutex);
                                return(-1);
                        }
 #endif
@@ -1076,9 +1244,11 @@ ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                         */
                        if (jumboplen <= IPV6_MAXPACKET) {
                                ip6stat.ip6s_badoptions++;
+                               lck_mtx_unlock(ip6_mutex);
                                icmp6_error(m, ICMP6_PARAM_PROB,
                                            ICMP6_PARAMPROB_HEADER,
                                            erroff + opt + 2 - opthead);
+                               lck_mtx_lock(ip6_mutex);
                                return(-1);
                        }
                        *plenp = jumboplen;
@@ -1090,9 +1260,11 @@ ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                                goto bad;
                        }
                        optlen = ip6_unknown_opt(opt, m,
-                           erroff + opt - opthead);
-                       if (optlen == -1)
+                           erroff + opt - opthead, 1);
+                       if (optlen == -1) {
+                               /* ip6_unknown opt unlocked ip6_mutex */
                                return(-1);
+                       }
                        optlen += 2;
                        break;
                }
@@ -1100,7 +1272,7 @@ ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
 
        return(0);
 
-  bad:
+  bad: 
        m_freem(m);
        return(-1);
 }
@@ -1112,10 +1284,11 @@ ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
  * is not continuous in order to return an ICMPv6 error.
  */
 int
-ip6_unknown_opt(optp, m, off)
+ip6_unknown_opt(optp, m, off, locked)
        u_int8_t *optp;
        struct mbuf *m;
        int off;
+       int locked;
 {
        struct ip6_hdr *ip6;
 
@@ -1127,7 +1300,11 @@ ip6_unknown_opt(optp, m, off)
                return(-1);
        case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
                ip6stat.ip6s_badoptions++;
+               if (locked)
+                       lck_mtx_unlock(ip6_mutex);
                icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
+               if (locked)
+                       lck_mtx_lock(ip6_mutex);
                return(-1);
        case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
                ip6stat.ip6s_badoptions++;
@@ -1135,9 +1312,14 @@ ip6_unknown_opt(optp, m, off)
                if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
                    (m->m_flags & (M_BCAST|M_MCAST)))
                        m_freem(m);
-               else
+               else {
+                       if (locked)
+                               lck_mtx_unlock(ip6_mutex);
                        icmp6_error(m, ICMP6_PARAM_PROB,
                                    ICMP6_PARAMPROB_OPTION, off);
+                       if (locked)
+                               lck_mtx_lock(ip6_mutex);
+               }
                return(-1);
        }
 
@@ -1162,17 +1344,8 @@ ip6_savecontrol(in6p, mp, ip6, m)
        struct ip6_hdr *ip6;
        struct mbuf *m;
 {
-       struct proc *p = current_proc();        /* XXX */
-       int privileged = 0;
        int rthdr_exist = 0;
 
-#ifdef __APPLE__
-        if (p && !suser(p->p_ucred, &p->p_acflag))
-#else
-       if (p && !suser(p))
-#endif
-               privileged++;
-
 #if SO_TIMESTAMP
        if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
                struct timeval tv;
@@ -1186,6 +1359,10 @@ ip6_savecontrol(in6p, mp, ip6, m)
        }
 #endif
 
+       /* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
+       if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
+               return;
+
        /* RFC 2292 sec. 5 */
        if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
                struct in6_pktinfo pi6;
@@ -1210,13 +1387,28 @@ ip6_savecontrol(in6p, mp, ip6, m)
                        mp = &(*mp)->m_next;
        }
 
+        if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
+                u_int32_t flowinfo;
+                int tclass;
+
+                flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
+                flowinfo >>= 20;
+
+                tclass = flowinfo & 0xff;
+                *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
+                    IPV6_TCLASS, IPPROTO_IPV6);
+                if (*mp)
+                        mp = &(*mp)->m_next;
+        }
+
        /*
-        * IPV6_HOPOPTS socket option. We require super-user privilege
-        * for the option, but it might be too strict, since there might
-        * be some hop-by-hop options which can be returned to normal user.
+        * IPV6_HOPOPTS socket option.  Recall that we required super-user
+        * privilege for the option (see ip6_ctloutput), but it might be too
+        * strict, since there might be some hop-by-hop options which can be
+        * returned to normal user.
         * See RFC 2292 section 6.
         */
-       if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
+       if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
                /*
                 * Check if a hop-by-hop options header is contatined in the
                 * received packet, and if so, store the options as ancillary
@@ -1224,7 +1416,7 @@ ip6_savecontrol(in6p, mp, ip6, m)
                 * just after the IPv6 header, which fact is assured through
                 * the IPv6 input processing.
                 */
-               struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
+               ip6 = mtod(m, struct ip6_hdr *);
                if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
                        struct ip6_hbh *hbh;
                        int hbhlen = 0;
@@ -1300,7 +1492,7 @@ ip6_savecontrol(in6p, mp, ip6, m)
 
        if ((in6p->in6p_flags &
             (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
-               struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
+               ip6 = mtod(m, struct ip6_hdr *);
                int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
 
                /*
@@ -1364,14 +1556,6 @@ ip6_savecontrol(in6p, mp, ip6, m)
                                if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0)
                                        break;
 
-                               /*
-                                * We also require super-user privilege for
-                                * the option.
-                                * See the comments on IN6_HOPOPTS.
-                                */
-                               if (!privileged)
-                                       break;
-
                                *mp = sbcreatecontrol((caddr_t)ip6e, elen,
                                                      IPV6_DSTOPTS,
                                                      IPPROTO_IPV6);
@@ -1497,7 +1681,7 @@ ip6_get_prevhdr(m, off)
        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 
        if (off == sizeof(struct ip6_hdr))
-               return(&ip6->ip6_nxt);
+               return((char *) &ip6->ip6_nxt);
        else {
                int len, nxt;
                struct ip6_ext *ip6e = NULL;
@@ -1521,7 +1705,7 @@ ip6_get_prevhdr(m, off)
                        nxt = ip6e->ip6e_nxt;
                }
                if (ip6e)
-                       return(&ip6e->ip6e_nxt);
+                       return((char *) &ip6e->ip6e_nxt);
                else
                        return NULL;
        }
@@ -1565,7 +1749,8 @@ ip6_nexthdr(m, off, proto, nxtp)
                if (m->m_pkthdr.len < off + sizeof(fh))
                        return -1;
                m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
-               if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
+               /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
+               if (fh.ip6f_offlg & IP6F_OFF_MASK)
                        return -1;
                if (nxtp)
                        *nxtp = fh.ip6f_nxt;
@@ -1636,55 +1821,49 @@ ip6_lasthdr(m, off, proto, nxtp)
        }
 }
 
-struct mbuf *
-ip6_addaux(m)
-       struct mbuf *m;
+struct ip6aux *
+ip6_addaux(
+       struct mbuf *m)
 {
-       struct mbuf *n;
-
-#if DIAGNOSTIC
-       if (sizeof(struct ip6aux) > MHLEN)
-               panic("assumption failed on sizeof(ip6aux)");
-#endif
-       n = m_aux_find(m, AF_INET6, -1);
-       if (n) {
-               if (n->m_len < sizeof(struct ip6aux)) {
-                       printf("conflicting use of ip6aux");
-                       return NULL;
-               }
-       } else {
-               n = m_aux_add(m, AF_INET6, -1);
-               if (n) {
-                       n->m_len = sizeof(struct ip6aux);
-                       bzero(mtod(n, caddr_t), n->m_len);
+       struct m_tag            *tag;
+       
+       /* Check if one is already allocated */
+       tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6, NULL);
+       if (tag == NULL) {
+               /* Allocate a tag */
+               tag = m_tag_alloc(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6,
+                   sizeof (struct ip6aux), M_DONTWAIT);
+
+               /* Attach it to the mbuf */
+               if (tag) {
+                       m_tag_prepend(m, tag);
                }
        }
-       return n;
+       
+       return tag ? (struct ip6aux*)(tag + 1) : NULL;
 }
 
-struct mbuf *
-ip6_findaux(m)
-       struct mbuf *m;
+struct ip6aux *
+ip6_findaux(
+       struct mbuf *m)
 {
-       struct mbuf *n;
-
-       n = m_aux_find(m, AF_INET6, -1);
-       if (n && n->m_len < sizeof(struct ip6aux)) {
-               printf("conflicting use of ip6aux");
-               n = NULL;
-       }
-       return n;
+       struct m_tag    *tag;
+       
+       tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, NULL);
+       
+       return tag ? (struct ip6aux*)(tag + 1) : NULL;
 }
 
 void
-ip6_delaux(m)
-       struct mbuf *m;
+ip6_delaux(
+       struct mbuf *m)
 {
-       struct mbuf *n;
+       struct m_tag    *tag;
 
-       n = m_aux_find(m, AF_INET6, -1);
-       if (n)
-               m_aux_delete(m, n);
+       tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, NULL);
+       if (tag) {
+               m_tag_delete(m, tag);
+       }
 }
 
 /*