]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/netinet/ip_encap.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / netinet / ip_encap.c
index f0dfd14de6c6536daedf9164c5e85ce9f1f92163..294b9875220edbc29c44cd345019c02cb9e84928 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2016 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
  * 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,
@@ -22,7 +22,7 @@
  * 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/netinet/ip_encap.c,v 1.1.2.2 2001/07/03 11:01:46 ume Exp $    */
@@ -76,7 +76,7 @@
  * Well, what can I say.  They impose different en/decapsulation mechanism
  * from each other, so they need separate protocol handler.  The only one
  * we can easily determine by protocol # is IPsec, which always has
- * AH/ESP/IPComp header right after outer IP header.
+ * AH/ESP header right after outer IP header.
  *
  * So, clearly good old protosw does not work for protocol #4 and #41.
  * The code will let you match protocol via src/dst address pair.
 #include <netinet/ip.h>
 #include <netinet/ip_var.h>
 #include <netinet/ip_encap.h>
-#if MROUTING
-#include <netinet/ip_mroute.h>
-#endif /* MROUTING */
 
-#if INET6
 #include <netinet/ip6.h>
 #include <netinet6/ip6_var.h>
 #include <netinet6/ip6protosw.h>
-#endif
-
 
 #include <net/net_osdep.h>
 
@@ -122,10 +116,10 @@ MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
 #endif
 
 static void encap_init(struct protosw *, struct domain *);
-static void encap_add(struct encaptab *);
+static void encap_add_locked(struct encaptab *);
 static int mask_match(const struct encaptab *, const struct sockaddr *,
-               const struct sockaddr *);
-static void encap_fillarg(struct mbuf *, const struct encaptab *);
+    const struct sockaddr *);
+static void encap_fillarg(struct mbuf *, void *arg);
 
 #ifndef LIST_HEAD_INITIALIZER
 /* rely upon BSS initialization */
@@ -134,17 +128,34 @@ LIST_HEAD(, encaptab) encaptab;
 LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
 #endif
 
+decl_lck_rw_data(static, encaptab_lock);
+
 static void
 encap_init(struct protosw *pp, struct domain *dp)
 {
 #pragma unused(dp)
        static int encap_initialized = 0;
+       lck_grp_attr_t *encaptab_grp_attrib = NULL;
+       lck_attr_t *encaptab_lck_attrib = NULL;
+       lck_grp_t *encaptab_lck_group = NULL;
 
-       VERIFY((pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED);
+       VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
 
        /* This gets called by more than one protocols, so initialize once */
-       if (encap_initialized)
+       if (encap_initialized) {
                return;
+       }
+
+       encaptab_grp_attrib = lck_grp_attr_alloc_init();
+       encaptab_lck_group = lck_grp_alloc_init("encaptab lock", encaptab_grp_attrib);
+       lck_grp_attr_free(encaptab_grp_attrib);
+
+       encaptab_lck_attrib = lck_attr_alloc_init();
+       lck_rw_init(&encaptab_lock, encaptab_lck_group, encaptab_lck_attrib);
+
+       lck_grp_free(encaptab_lck_group);
+       lck_attr_free(encaptab_lck_attrib);
+
        encap_initialized = 1;
 #if 0
        /*
@@ -172,9 +183,7 @@ encap6_init(struct ip6protosw *pp, struct domain *dp)
 
 #if INET
 void
-encap4_input(m, off)
-       struct mbuf *m;
-       int off;
+encap4_input(struct mbuf *m, int off)
 {
        int proto;
        struct ip *ip;
@@ -182,6 +191,7 @@ encap4_input(m, off)
        const struct protosw *psw;
        struct encaptab *ep, *match;
        int prio, matchprio;
+       void *match_arg = NULL;
 
 #ifndef __APPLE__
        va_start(ap, m);
@@ -209,14 +219,18 @@ encap4_input(m, off)
 
        match = NULL;
        matchprio = 0;
+
+       lck_rw_lock_shared(&encaptab_lock);
        for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
-               if (ep->af != AF_INET)
+               if (ep->af != AF_INET) {
                        continue;
-               if (ep->proto >= 0 && ep->proto != proto)
+               }
+               if (ep->proto >= 0 && ep->proto != proto) {
                        continue;
-               if (ep->func)
+               }
+               if (ep->func) {
                        prio = (*ep->func)(m, off, proto, ep->arg);
-               else {
+               else {
                        /*
                         * it's inbound traffic, we need to match in reverse
                         * order
@@ -243,43 +257,34 @@ encap4_input(m, off)
                 * to get the best match - the search takes O(n) for
                 * n attachments (i.e. interfaces).
                 */
-               if (prio <= 0)
+               if (prio <= 0) {
                        continue;
+               }
                if (prio > matchprio) {
                        matchprio = prio;
                        match = ep;
+                       psw = (const struct protosw *)match->psw;
+                       match_arg = ep->arg;
                }
        }
+       lck_rw_unlock_shared(&encaptab_lock);
 
        if (match) {
                /* found a match, "match" has the best one */
-               psw = (const struct protosw *)match->psw;
                if (psw && psw->pr_input) {
-                       encap_fillarg(m, match);
+                       encap_fillarg(m, match_arg);
                        (*psw->pr_input)(m, off);
-               } else
+               } else {
                        m_freem(m);
+               }
                return;
        }
 
-       /* for backward compatibility */
-# if MROUTING
-#  define COMPATFUNC   ipip_input
-# endif /*MROUTING*/
-
-#if COMPATFUNC
-       if (proto == IPPROTO_IPV4) {
-               COMPATFUNC(m, off);
-               return;
-       }
-#endif
-
        /* last resort: inject to raw socket */
        rip_input(m, off);
 }
 #endif
 
-#if INET6
 int
 encap6_input(struct mbuf **mp, int *offp, int proto)
 {
@@ -289,6 +294,7 @@ encap6_input(struct mbuf **mp, int *offp, int proto)
        const struct ip6protosw *psw;
        struct encaptab *ep, *match;
        int prio, matchprio;
+       void *match_arg = NULL;
 
        /* Expect 32-bit aligned data pointer on strict-align platforms */
        MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
@@ -305,14 +311,18 @@ encap6_input(struct mbuf **mp, int *offp, int proto)
 
        match = NULL;
        matchprio = 0;
+
+       lck_rw_lock_shared(&encaptab_lock);
        for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
-               if (ep->af != AF_INET6)
+               if (ep->af != AF_INET6) {
                        continue;
-               if (ep->proto >= 0 && ep->proto != proto)
+               }
+               if (ep->proto >= 0 && ep->proto != proto) {
                        continue;
-               if (ep->func)
+               }
+               if (ep->func) {
                        prio = (*ep->func)(m, *offp, proto, ep->arg);
-               else {
+               else {
                        /*
                         * it's inbound traffic, we need to match in reverse
                         * order
@@ -322,19 +332,22 @@ encap6_input(struct mbuf **mp, int *offp, int proto)
                }
 
                /* see encap4_input() for issues here */
-               if (prio <= 0)
+               if (prio <= 0) {
                        continue;
+               }
                if (prio > matchprio) {
                        matchprio = prio;
                        match = ep;
+                       psw = (const struct ip6protosw *)match->psw;
+                       match_arg = ep->arg;
                }
        }
+       lck_rw_unlock_shared(&encaptab_lock);
 
        if (match) {
                /* found a match */
-               psw = (const struct ip6protosw *)match->psw;
                if (psw && psw->pr_input) {
-                       encap_fillarg(m, match);
+                       encap_fillarg(m, match_arg);
                        return (*psw->pr_input)(mp, offp, proto);
                } else {
                        m_freem(m);
@@ -345,13 +358,11 @@ encap6_input(struct mbuf **mp, int *offp, int proto)
        /* last resort: inject to raw socket */
        return rip6_input(mp, offp, proto);
 }
-#endif
 
 static void
-encap_add(ep)
-       struct encaptab *ep;
+encap_add_locked(struct encaptab *ep)
 {
-
+       LCK_RW_ASSERT(&encaptab_lock, LCK_RW_ASSERT_EXCLUSIVE);
        LIST_INSERT_HEAD(&encaptab, ep, chain);
 }
 
@@ -361,19 +372,16 @@ encap_add(ep)
  * Return value will be necessary as input (cookie) for encap_detach().
  */
 const struct encaptab *
-encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
-       int af;
-       int proto;
-       const struct sockaddr *sp, *sm;
-       const struct sockaddr *dp, *dm;
-       const struct protosw *psw;
-       void *arg;
+encap_attach(int af, int proto, const struct sockaddr *sp,
+    const struct sockaddr *sm, const struct sockaddr *dp,
+    const struct sockaddr *dm, const struct protosw *psw, void *arg)
 {
-       struct encaptab *ep;
+       struct encaptab *ep = NULL;
+       struct encaptab *new_ep = NULL;
        int error;
 
        /* sanity check on args */
-       if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) {
+       if (sp->sa_len > sizeof(new_ep->src) || dp->sa_len > sizeof(new_ep->dst)) {
                error = EINVAL;
                goto fail;
        }
@@ -386,57 +394,64 @@ encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
                goto fail;
        }
 
+       new_ep = _MALLOC(sizeof(*new_ep), M_NETADDR, M_WAITOK | M_ZERO);
+       if (new_ep == NULL) {
+               error = ENOBUFS;
+               goto fail;
+       }
+
        /* check if anyone have already attached with exactly same config */
+       lck_rw_lock_exclusive(&encaptab_lock);
        for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
-               if (ep->af != af)
+               if (ep->af != af) {
                        continue;
-               if (ep->proto != proto)
+               }
+               if (ep->proto != proto) {
                        continue;
+               }
                if (ep->src.ss_len != sp->sa_len ||
                    bcmp(&ep->src, sp, sp->sa_len) != 0 ||
-                   bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
+                   bcmp(&ep->srcmask, sm, sp->sa_len) != 0) {
                        continue;
+               }
                if (ep->dst.ss_len != dp->sa_len ||
                    bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
-                   bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
+                   bcmp(&ep->dstmask, dm, dp->sa_len) != 0) {
                        continue;
+               }
 
                error = EEXIST;
-               goto fail;
-       }
-
-       ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK); /*XXX*/
-       if (ep == NULL) {
-               error = ENOBUFS;
-               goto fail;
+               goto fail_locked;
        }
-       bzero(ep, sizeof(*ep));
 
-       ep->af = af;
-       ep->proto = proto;
-       bcopy(sp, &ep->src, sp->sa_len);
-       bcopy(sm, &ep->srcmask, sp->sa_len);
-       bcopy(dp, &ep->dst, dp->sa_len);
-       bcopy(dm, &ep->dstmask, dp->sa_len);
-       ep->psw = psw;
-       ep->arg = arg;
+       new_ep->af = af;
+       new_ep->proto = proto;
+       bcopy(sp, &new_ep->src, sp->sa_len);
+       bcopy(sm, &new_ep->srcmask, sp->sa_len);
+       bcopy(dp, &new_ep->dst, dp->sa_len);
+       bcopy(dm, &new_ep->dstmask, dp->sa_len);
+       new_ep->psw = psw;
+       new_ep->arg = arg;
 
-       encap_add(ep);
+       encap_add_locked(new_ep);
+       lck_rw_unlock_exclusive(&encaptab_lock);
 
        error = 0;
-       return ep;
+       return new_ep;
 
+fail_locked:
+       lck_rw_unlock_exclusive(&encaptab_lock);
+       if (new_ep != NULL) {
+               _FREE(new_ep, M_NETADDR);
+       }
 fail:
        return NULL;
 }
 
 const struct encaptab *
-encap_attach_func(af, proto, func, psw, arg)
-       int af;
-       int proto;
-       int (*func)(const struct mbuf *, int, int, void *);
-       const struct protosw *psw;
-       void *arg;
+encap_attach_func( int af, int proto,
+    int (*func)(const struct mbuf *, int, int, void *),
+    const struct protosw *psw, void *arg)
 {
        struct encaptab *ep;
        int error;
@@ -447,12 +462,11 @@ encap_attach_func(af, proto, func, psw, arg)
                goto fail;
        }
 
-       ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK); /*XXX*/
+       ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK | M_ZERO); /* XXX */
        if (ep == NULL) {
                error = ENOBUFS;
                goto fail;
        }
-       bzero(ep, sizeof(*ep));
 
        ep->af = af;
        ep->proto = proto;
@@ -460,7 +474,9 @@ encap_attach_func(af, proto, func, psw, arg)
        ep->psw = psw;
        ep->arg = arg;
 
-       encap_add(ep);
+       lck_rw_lock_exclusive(&encaptab_lock);
+       encap_add_locked(ep);
+       lck_rw_unlock_exclusive(&encaptab_lock);
 
        error = 0;
        return ep;
@@ -470,28 +486,28 @@ fail:
 }
 
 int
-encap_detach(cookie)
-       const struct encaptab *cookie;
+encap_detach(const struct encaptab *cookie)
 {
        const struct encaptab *ep = cookie;
        struct encaptab *p;
 
+       lck_rw_lock_exclusive(&encaptab_lock);
        for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
                if (p == ep) {
                        LIST_REMOVE(p, chain);
-                       _FREE(p, M_NETADDR);    /*XXX*/
+                       lck_rw_unlock_exclusive(&encaptab_lock);
+                       _FREE(p, M_NETADDR);    /*XXX*/
                        return 0;
                }
        }
+       lck_rw_unlock_exclusive(&encaptab_lock);
 
        return EINVAL;
 }
 
 static int
-mask_match(ep, sp, dp)
-       const struct encaptab *ep;
-       const struct sockaddr *sp;
-       const struct sockaddr *dp;
+mask_match(const struct encaptab *ep, const struct sockaddr *sp,
+    const struct sockaddr *dp)
 {
        struct sockaddr_storage s;
        struct sockaddr_storage d;
@@ -500,19 +516,22 @@ mask_match(ep, sp, dp)
        u_int8_t *r;
        int matchlen;
 
-       if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
+       if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d)) {
                return 0;
-       if (sp->sa_family != ep->af || dp->sa_family != ep->af)
+       }
+       if (sp->sa_family != ep->af || dp->sa_family != ep->af) {
                return 0;
-       if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
+       }
+       if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len) {
                return 0;
+       }
 
        matchlen = 0;
 
        p = (const u_int8_t *)sp;
        q = (const u_int8_t *)&ep->srcmask;
        r = (u_int8_t *)&s;
-       for (i = 0 ; i < sp->sa_len; i++) {
+       for (i = 0; i < sp->sa_len; i++) {
                r[i] = p[i] & q[i];
                /* XXX estimate */
                matchlen += (q[i] ? 8 : 0);
@@ -521,7 +540,7 @@ mask_match(ep, sp, dp)
        p = (const u_int8_t *)dp;
        q = (const u_int8_t *)&ep->dstmask;
        r = (u_int8_t *)&d;
-       for (i = 0 ; i < dp->sa_len; i++) {
+       for (i = 0; i < dp->sa_len; i++) {
                r[i] = p[i] & q[i];
                /* XXX rough estimate */
                matchlen += (q[i] ? 8 : 0);
@@ -536,46 +555,46 @@ mask_match(ep, sp, dp)
        if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
            bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
                return matchlen;
-       } else
+       } else {
                return 0;
+       }
 }
 
 struct encaptabtag {
-       void*                   *arg;
+       void*                   *arg;
 };
 
 static void
 encap_fillarg(
        struct mbuf *m,
-       const struct encaptab *ep)
+       void *arg)
 {
-       struct m_tag    *tag;
+       struct m_tag    *tag;
        struct encaptabtag *et;
-       
+
        tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP,
-                                         sizeof(struct encaptabtag), M_WAITOK, m);
-       
+           sizeof(struct encaptabtag), M_WAITOK, m);
+
        if (tag != NULL) {
                et = (struct encaptabtag*)(tag + 1);
-               et->arg = ep->arg;
+               et->arg = arg;
                m_tag_prepend(m, tag);
        }
 }
 
 void *
-encap_getarg(m)
-       struct mbuf *m;
+encap_getarg(struct mbuf *m)
 {
-       struct m_tag    *tag;
+       struct m_tag    *tag;
        struct encaptabtag *et;
        void *p = NULL;
-       
+
        tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, NULL);
        if (tag) {
                et = (struct encaptabtag*)(tag + 1);
                p = et->arg;
                m_tag_delete(m, tag);
        }
-       
+
        return p;
 }