X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/1c79356b52d46aa6b508fb032f5ae709b1f2897b..ecc0ceb4089d506a0b8d16686a95817b331af9cb:/bsd/netinet/ip_encap.c diff --git a/bsd/netinet/ip_encap.c b/bsd/netinet/ip_encap.c index 0fae46c85..6c4d33072 100644 --- a/bsd/netinet/ip_encap.c +++ b/bsd/netinet/ip_encap.c @@ -1,25 +1,32 @@ /* - * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2012 Apple Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. + * 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. * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * 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 OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. + * 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_LICENSE_HEADER_END@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ -/* $KAME: ip_encap.c,v 1.21 2000/03/30 14:30:06 itojun Exp $ */ +/* $FreeBSD: src/sys/netinet/ip_encap.c,v 1.1.2.2 2001/07/03 11:01:46 ume Exp $ */ +/* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -62,7 +69,8 @@ * mobile-ip6 (uses RFC2473) * 6to4 tunnel * Here's a list of protocol that want protocol #4: - * RFC1853 IPv4-in-IPv4 tunnel + * RFC1853 IPv4-in-IPv4 tunnelling + * RFC2003 IPv4 encapsulation within IPv4 * RFC2344 reverse tunnelling for mobile-ip4 * RFC2401 IPsec tunnel * Well, what can I say. They impose different en/decapsulation mechanism @@ -73,30 +81,18 @@ * 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. */ - -#ifdef __FreeBSD__ -# include "opt_mrouting.h" -# if __FreeBSD__ == 3 -# include "opt_inet.h" -# endif -# if __FreeBSD__ >= 4 -# include "opt_inet.h" -# include "opt_inet6.h" -# endif -#else -# ifdef __NetBSD__ -# include "opt_inet.h" -# endif -#endif +/* XXX is M_NETADDR correct? */ #include #include #include #include #include +#include #include +#include #include -#include +#include #include #include @@ -106,12 +102,6 @@ #include #include #include -#if MROUTING -#include -#endif /* MROUTING */ -#ifdef __OpenBSD__ -#include -#endif #if INET6 #include @@ -122,26 +112,41 @@ #include -#if defined(__FreeBSD__) && __FreeBSD__ >= 3 +#ifndef __APPLE__ #include #include MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure"); #endif -static int mask_match __P((const struct encaptab *, const struct sockaddr *, - const struct sockaddr *)); -static void encap_fillarg __P((struct mbuf *, const struct encaptab *)); +static void encap_init(struct protosw *, struct domain *); +static void encap_add(struct encaptab *); +static int mask_match(const struct encaptab *, const struct sockaddr *, + const struct sockaddr *); +static void encap_fillarg(struct mbuf *, const struct encaptab *); +#ifndef LIST_HEAD_INITIALIZER /* rely upon BSS initialization */ LIST_HEAD(, encaptab) encaptab; +#else +LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab); +#endif -void -encap_init() +static void +encap_init(struct protosw *pp, struct domain *dp) { +#pragma unused(dp) + static int encap_initialized = 0; + + VERIFY((pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED); + + /* This gets called by more than one protocols, so initialize once */ + if (encap_initialized) + return; + encap_initialized = 1; #if 0 /* * we cannot use LIST_INIT() here, since drivers may want to call - * encap_attach(), on driver attach. encap_init() wlil be called + * encap_attach(), on driver attach. encap_init() will be called * on AF_INET{,6} initialization, which happens after driver * initialization - using LIST_INIT() here can nuke encap_attach() * from drivers. @@ -151,18 +156,42 @@ encap_init() } void -encap4_input(m, off, proto) +encap4_init(struct protosw *pp, struct domain *dp) +{ + encap_init(pp, dp); +} + +void +encap6_init(struct ip6protosw *pp, struct domain *dp) +{ + encap_init((struct protosw *)pp, dp); +} + +#if INET +void +encap4_input(m, off) struct mbuf *m; int off; - int proto; { + int proto; struct ip *ip; struct sockaddr_in s, d; - struct encaptab *ep; + const struct protosw *psw; + struct encaptab *ep, *match; + int prio, matchprio; + +#ifndef __APPLE__ + va_start(ap, m); + off = va_arg(ap, int); + proto = va_arg(ap, int); + va_end(ap); +#endif + /* Expect 32-bit aligned data pointer on strict-align platforms */ + MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); ip = mtod(m, struct ip *); -#ifdef __OpenBSD__ +#ifdef __APPLE__ proto = ip->ip_p; #endif @@ -175,67 +204,81 @@ encap4_input(m, off, proto) d.sin_len = sizeof(struct sockaddr_in); d.sin_addr = ip->ip_dst; + match = NULL; + matchprio = 0; for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) { + if (ep->af != AF_INET) + continue; if (ep->proto >= 0 && ep->proto != proto) continue; - - if (ep->func) { - if ((*ep->func)(m, off, proto, ep->arg) == 0) - continue; - } else { + if (ep->func) + prio = (*ep->func)(m, off, proto, ep->arg); + else { /* * it's inbound traffic, we need to match in reverse * order */ - if (mask_match(ep, (struct sockaddr *)&d, - (struct sockaddr *)&s) == 0) - continue; + prio = mask_match(ep, (struct sockaddr *)&d, + (struct sockaddr *)&s); } - /* found a match */ - if (ep->psw && ep->psw->pr_input) { - encap_fillarg(m, ep); -#warning watchout pr_input! - (*ep->psw->pr_input)(m, off); - } else - m_freem(m); - return; + /* + * We prioritize the matches by using bit length of the + * matches. mask_match() and user-supplied matching function + * should return the bit length of the matches (for example, + * if both src/dst are matched for IPv4, 64 should be returned). + * 0 or negative return value means "it did not match". + * + * The question is, since we have two "mask" portion, we + * cannot really define total order between entries. + * For example, which of these should be preferred? + * mask_match() returns 48 (32 + 16) for both of them. + * src=3ffe::/16, dst=3ffe:501::/32 + * src=3ffe:501::/32, dst=3ffe::/16 + * + * We need to loop through all the possible candidates + * to get the best match - the search takes O(n) for + * n attachments (i.e. interfaces). + */ + if (prio <= 0) + continue; + if (prio > matchprio) { + matchprio = prio; + match = ep; + } } - /* for backward compatibility */ - if (proto == IPPROTO_IPV4) { -#ifdef __OpenBSD__ -#if defined(MROUTING) || defined(IPSEC) - ip4_input(m, off, proto); - return; -#endif -#else -#if MROUTING - ipip_input(m, off); + 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); + (*psw->pr_input)(m, off); + } else + m_freem(m); return; -#endif /*MROUTING*/ -#endif } /* last resort: inject to raw socket */ rip_input(m, off); } +#endif #if INET6 int -encap6_input(mp, offp, proto) - struct mbuf **mp; - int *offp; - int proto; +encap6_input(struct mbuf **mp, int *offp, int proto) { struct mbuf *m = *mp; struct ip6_hdr *ip6; struct sockaddr_in6 s, d; - struct ip6protosw *psw; - struct encaptab *ep; + const struct ip6protosw *psw; + struct encaptab *ep, *match; + int prio, matchprio; - ip6 = mtod(m, struct ip6_hdr *); + /* Expect 32-bit aligned data pointer on strict-align platforms */ + MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); + ip6 = mtod(m, struct ip6_hdr *); bzero(&s, sizeof(s)); s.sin6_family = AF_INET6; s.sin6_len = sizeof(struct sockaddr_in6); @@ -245,27 +288,38 @@ encap6_input(mp, offp, proto) d.sin6_len = sizeof(struct sockaddr_in6); d.sin6_addr = ip6->ip6_dst; + match = NULL; + matchprio = 0; for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) { + if (ep->af != AF_INET6) + continue; if (ep->proto >= 0 && ep->proto != proto) continue; - if (ep->func) { - if ((*ep->func)(m, *offp, proto, ep->arg) == 0) - continue; - } else { + if (ep->func) + prio = (*ep->func)(m, *offp, proto, ep->arg); + else { /* * it's inbound traffic, we need to match in reverse * order */ - if (mask_match(ep, (struct sockaddr *)&d, - (struct sockaddr *)&s) == 0) - continue; + prio = mask_match(ep, (struct sockaddr *)&d, + (struct sockaddr *)&s); } + /* see encap4_input() for issues here */ + if (prio <= 0) + continue; + if (prio > matchprio) { + matchprio = prio; + match = ep; + } + } + + if (match) { /* found a match */ - psw = (struct ip6protosw *)ep->psw; -#warning watchout pr_input! + psw = (const struct ip6protosw *)match->psw; if (psw && psw->pr_input) { - encap_fillarg(m, ep); + encap_fillarg(m, match); return (*psw->pr_input)(mp, offp, proto); } else { m_freem(m); @@ -278,6 +332,14 @@ encap6_input(mp, offp, proto) } #endif +static void +encap_add(ep) + struct encaptab *ep; +{ + + LIST_INSERT_HEAD(&encaptab, ep, chain); +} + /* * sp (src ptr) is always my side, and dp (dst ptr) is always remote side. * length of mask (sm and dm) is assumed to be same as sp/dp. @@ -294,13 +356,7 @@ encap_attach(af, proto, sp, sm, dp, dm, psw, arg) { struct encaptab *ep; int error; - int s; -#if defined(__NetBSD__) || defined(__OpenBSD__) - s = splsoftnet(); -#else - s = splnet(); -#endif /* sanity check on args */ if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) { error = EINVAL; @@ -334,12 +390,11 @@ encap_attach(af, proto, sp, sm, dp, dm, psw, arg) goto fail; } - ep = _MALLOC(sizeof(*ep), M_NETADDR, M_NOWAIT); /*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; @@ -350,24 +405,12 @@ encap_attach(af, proto, sp, sm, dp, dm, psw, arg) ep->psw = psw; ep->arg = arg; - /* - * Order of insertion will determine the priority in lookup. - * We should be careful putting them in specific-one-first order. - * The question is, since we have two "mask" portion, we cannot really - * define total order between entries. - * For example, which of these should be preferred? - * src=3ffe::/16, dst=3ffe:501::/32 - * src=3ffe:501::/32, dst=3ffe::/16 - * - * At this moment we don't care about the ordering. - */ - LIST_INSERT_HEAD(&encaptab, ep, chain); + encap_add(ep); + error = 0; - splx(s); return ep; fail: - splx(s); return NULL; } @@ -375,31 +418,24 @@ const struct encaptab * encap_attach_func(af, proto, func, psw, arg) int af; int proto; - int (*func) __P((const struct mbuf *, int, int, void *)); + int (*func)(const struct mbuf *, int, int, void *); const struct protosw *psw; void *arg; { struct encaptab *ep; int error; - int s; -#if defined(__NetBSD__) || defined(__OpenBSD__) - s = splsoftnet(); -#else - s = splnet(); -#endif /* sanity check on args */ if (!func) { error = EINVAL; goto fail; } - ep = _MALLOC(sizeof(*ep), M_NETADDR, M_NOWAIT); /*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; @@ -407,24 +443,12 @@ encap_attach_func(af, proto, func, psw, arg) ep->psw = psw; ep->arg = arg; - /* - * Order of insertion will determine the priority in lookup. - * We should be careful putting them in specific-one-first order. - * The question is, since we have two "mask" portion, we cannot really - * define total order between entries. - * For example, which of these should be checked first? - * src=3ffe::/16, dst=3ffe:501::/32 - * src=3ffe:501::/32, dst=3ffe::/16 - * - * At this moment we don't care about the ordering. - */ - LIST_INSERT_HEAD(&encaptab, ep, chain); + encap_add(ep); + error = 0; - splx(s); return ep; fail: - splx(s); return NULL; } @@ -455,7 +479,9 @@ mask_match(ep, sp, dp) struct sockaddr_storage s; struct sockaddr_storage d; int i; - u_int8_t *p, *q, *r; + const u_int8_t *p, *q; + u_int8_t *r; + int matchlen; if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d)) return 0; @@ -464,17 +490,25 @@ mask_match(ep, sp, dp) if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len) return 0; - p = (u_int8_t *)sp; - q = (u_int8_t *)&ep->srcmask; + 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); + } - p = (u_int8_t *)dp; - q = (u_int8_t *)&ep->dstmask; + 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); + } /* need to overwrite len/family portion as we don't compare them */ s.ss_len = sp->sa_len; @@ -484,48 +518,47 @@ 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 1; + return matchlen; } else return 0; } +struct encaptabtag { + void* *arg; +}; + static void -encap_fillarg(m, ep) - struct mbuf *m; - const struct encaptab *ep; +encap_fillarg( + struct mbuf *m, + const struct encaptab *ep) { -#if 0 - m->m_pkthdr.aux = ep->arg; -#else - struct mbuf *n; - - n = m_aux_add(m, AF_INET, IPPROTO_IPV4); - if (n) { - *mtod(n, void **) = ep->arg; - n->m_len = sizeof(void *); + 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); + + if (tag != NULL) { + et = (struct encaptabtag*)(tag + 1); + et->arg = ep->arg; + m_tag_prepend(m, tag); } -#endif } void * encap_getarg(m) struct mbuf *m; { - void *p; -#if 0 - p = m->m_pkthdr.aux; - m->m_pkthdr.aux = NULL; - return p; -#else - struct mbuf *n; - - p = NULL; - n = m_aux_find(m, AF_INET, IPPROTO_IPV4); - if (n) { - if (n->m_len == sizeof(void *)) - p = *mtod(n, void **); - m_aux_delete(m, n); + 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; -#endif }