X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/2d21ac55c334faf3a56e5634905ed6987fc787d4..d190cdc3f5544636abb56dc1874be391d3e1b148:/bsd/netinet/ip_encap.c diff --git a/bsd/netinet/ip_encap.c b/bsd/netinet/ip_encap.c index 6dd02fe9e..5276504a9 100644 --- a/bsd/netinet/ip_encap.c +++ b/bsd/netinet/ip_encap.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 2000,2007 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 @@ -11,10 +11,10 @@ * 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 $ */ @@ -88,7 +88,9 @@ #include #include #include +#include #include +#include #include #include @@ -100,9 +102,6 @@ #include #include #include -#if MROUTING -#include -#endif /* MROUTING */ #if INET6 #include @@ -119,6 +118,7 @@ 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 int mask_match(const struct encaptab *, const struct sockaddr *, const struct sockaddr *); @@ -131,14 +131,18 @@ LIST_HEAD(, encaptab) encaptab; LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab); #endif -void -encap_init() +static void +encap_init(struct protosw *pp, struct domain *dp) { - static int initialized = 0; +#pragma unused(dp) + static int encap_initialized = 0; - if (initialized) + 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; - initialized++; + encap_initialized = 1; #if 0 /* * we cannot use LIST_INIT() here, since drivers may want to call @@ -151,11 +155,21 @@ encap_init() #endif } +void +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; +encap4_input(struct mbuf *m, int off) { int proto; struct ip *ip; @@ -171,6 +185,9 @@ encap4_input(m, off) 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 __APPLE__ proto = ip->ip_p; @@ -240,18 +257,6 @@ encap4_input(m, off) 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); } @@ -259,9 +264,7 @@ encap4_input(m, off) #if INET6 int -encap6_input(mp, offp) - struct mbuf **mp; - int *offp; +encap6_input(struct mbuf **mp, int *offp, int proto) { struct mbuf *m = *mp; struct ip6_hdr *ip6; @@ -269,11 +272,11 @@ encap6_input(mp, offp) const struct ip6protosw *psw; struct encaptab *ep, *match; int prio, matchprio; - int proto; - ip6 = mtod(m, struct ip6_hdr *); - proto = ip6->ip6_nxt; + /* 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); @@ -315,7 +318,7 @@ encap6_input(mp, offp) psw = (const struct ip6protosw *)match->psw; if (psw && psw->pr_input) { encap_fillarg(m, match); - return (*psw->pr_input)(mp, offp); + return (*psw->pr_input)(mp, offp, proto); } else { m_freem(m); return IPPROTO_DONE; @@ -323,15 +326,13 @@ encap6_input(mp, offp) } /* last resort: inject to raw socket */ - return rip6_input(mp, offp); + return rip6_input(mp, offp, proto); } #endif static void -encap_add(ep) - struct encaptab *ep; +encap_add(struct encaptab *ep) { - LIST_INSERT_HEAD(&encaptab, ep, chain); } @@ -341,13 +342,9 @@ 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; int error; @@ -385,12 +382,11 @@ encap_attach(af, proto, sp, sm, dp, dm, 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; @@ -411,12 +407,9 @@ fail: } 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; @@ -427,12 +420,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; @@ -450,8 +442,7 @@ fail: } int -encap_detach(cookie) - const struct encaptab *cookie; +encap_detach(const struct encaptab *cookie) { const struct encaptab *ep = cookie; struct encaptab *p; @@ -468,10 +459,8 @@ encap_detach(cookie) } 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; @@ -532,8 +521,8 @@ encap_fillarg( struct m_tag *tag; struct encaptabtag *et; - tag = m_tag_alloc(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, - sizeof(struct encaptabtag), M_WAITOK); + 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); @@ -543,8 +532,7 @@ encap_fillarg( } void * -encap_getarg(m) - struct mbuf *m; +encap_getarg(struct mbuf *m) { struct m_tag *tag; struct encaptabtag *et;