1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions * Copyright (c) 2016 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions *
4*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions *
6*5e3eaea3SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions *
15*5e3eaea3SApple OSS Distributions * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions *
18*5e3eaea3SApple OSS Distributions * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions * limitations under the License.
25*5e3eaea3SApple OSS Distributions *
26*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions */
28*5e3eaea3SApple OSS Distributions
29*5e3eaea3SApple OSS Distributions /* $KAME: keydb.c,v 1.61 2000/03/25 07:24:13 sumikawa Exp $ */
30*5e3eaea3SApple OSS Distributions
31*5e3eaea3SApple OSS Distributions /*
32*5e3eaea3SApple OSS Distributions * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33*5e3eaea3SApple OSS Distributions * All rights reserved.
34*5e3eaea3SApple OSS Distributions *
35*5e3eaea3SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
36*5e3eaea3SApple OSS Distributions * modification, are permitted provided that the following conditions
37*5e3eaea3SApple OSS Distributions * are met:
38*5e3eaea3SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
39*5e3eaea3SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
40*5e3eaea3SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
41*5e3eaea3SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
42*5e3eaea3SApple OSS Distributions * documentation and/or other materials provided with the distribution.
43*5e3eaea3SApple OSS Distributions * 3. Neither the name of the project nor the names of its contributors
44*5e3eaea3SApple OSS Distributions * may be used to endorse or promote products derived from this software
45*5e3eaea3SApple OSS Distributions * without specific prior written permission.
46*5e3eaea3SApple OSS Distributions *
47*5e3eaea3SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48*5e3eaea3SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49*5e3eaea3SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50*5e3eaea3SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51*5e3eaea3SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52*5e3eaea3SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53*5e3eaea3SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54*5e3eaea3SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55*5e3eaea3SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56*5e3eaea3SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57*5e3eaea3SApple OSS Distributions * SUCH DAMAGE.
58*5e3eaea3SApple OSS Distributions */
59*5e3eaea3SApple OSS Distributions
60*5e3eaea3SApple OSS Distributions #include <sys/types.h>
61*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
62*5e3eaea3SApple OSS Distributions #include <sys/param.h>
63*5e3eaea3SApple OSS Distributions #include <sys/systm.h>
64*5e3eaea3SApple OSS Distributions #include <sys/kernel.h>
65*5e3eaea3SApple OSS Distributions #include <sys/malloc.h>
66*5e3eaea3SApple OSS Distributions #include <sys/errno.h>
67*5e3eaea3SApple OSS Distributions #include <sys/queue.h>
68*5e3eaea3SApple OSS Distributions
69*5e3eaea3SApple OSS Distributions #include <net/if.h>
70*5e3eaea3SApple OSS Distributions #include <net/route.h>
71*5e3eaea3SApple OSS Distributions
72*5e3eaea3SApple OSS Distributions #include <netinet/in.h>
73*5e3eaea3SApple OSS Distributions
74*5e3eaea3SApple OSS Distributions #include <net/pfkeyv2.h>
75*5e3eaea3SApple OSS Distributions #include <netkey/key.h>
76*5e3eaea3SApple OSS Distributions #include <netkey/keydb.h>
77*5e3eaea3SApple OSS Distributions #include <netinet6/ipsec.h>
78*5e3eaea3SApple OSS Distributions
79*5e3eaea3SApple OSS Distributions #include <net/net_osdep.h>
80*5e3eaea3SApple OSS Distributions
81*5e3eaea3SApple OSS Distributions // static void keydb_delsecasvar(struct secasvar *); // not used
82*5e3eaea3SApple OSS Distributions
83*5e3eaea3SApple OSS Distributions /*
84*5e3eaea3SApple OSS Distributions * secpolicy management
85*5e3eaea3SApple OSS Distributions */
86*5e3eaea3SApple OSS Distributions struct secpolicy *
keydb_newsecpolicy(void)87*5e3eaea3SApple OSS Distributions keydb_newsecpolicy(void)
88*5e3eaea3SApple OSS Distributions {
89*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED);
90*5e3eaea3SApple OSS Distributions
91*5e3eaea3SApple OSS Distributions return kalloc_type(struct secpolicy, Z_WAITOK | Z_ZERO);
92*5e3eaea3SApple OSS Distributions }
93*5e3eaea3SApple OSS Distributions
94*5e3eaea3SApple OSS Distributions void
keydb_delsecpolicy(struct secpolicy * p)95*5e3eaea3SApple OSS Distributions keydb_delsecpolicy(struct secpolicy *p)
96*5e3eaea3SApple OSS Distributions {
97*5e3eaea3SApple OSS Distributions kfree_type(struct secpolicy, p);
98*5e3eaea3SApple OSS Distributions }
99*5e3eaea3SApple OSS Distributions
100*5e3eaea3SApple OSS Distributions /*
101*5e3eaea3SApple OSS Distributions * secashead management
102*5e3eaea3SApple OSS Distributions */
103*5e3eaea3SApple OSS Distributions struct secashead *
keydb_newsecashead(void)104*5e3eaea3SApple OSS Distributions keydb_newsecashead(void)
105*5e3eaea3SApple OSS Distributions {
106*5e3eaea3SApple OSS Distributions struct secashead *p;
107*5e3eaea3SApple OSS Distributions
108*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
109*5e3eaea3SApple OSS Distributions
110*5e3eaea3SApple OSS Distributions p = kalloc_type(struct secashead, Z_NOWAIT | Z_ZERO);
111*5e3eaea3SApple OSS Distributions if (!p) {
112*5e3eaea3SApple OSS Distributions lck_mtx_unlock(sadb_mutex);
113*5e3eaea3SApple OSS Distributions p = kalloc_type(struct secashead, Z_WAITOK | Z_ZERO | Z_NOFAIL);
114*5e3eaea3SApple OSS Distributions lck_mtx_lock(sadb_mutex);
115*5e3eaea3SApple OSS Distributions }
116*5e3eaea3SApple OSS Distributions for (size_t i = 0; i < ARRAY_COUNT(p->savtree); i++) {
117*5e3eaea3SApple OSS Distributions LIST_INIT(&p->savtree[i]);
118*5e3eaea3SApple OSS Distributions }
119*5e3eaea3SApple OSS Distributions return p;
120*5e3eaea3SApple OSS Distributions }
121*5e3eaea3SApple OSS Distributions
122*5e3eaea3SApple OSS Distributions #if 0
123*5e3eaea3SApple OSS Distributions void
124*5e3eaea3SApple OSS Distributions keydb_delsecashead(struct secashead *p)
125*5e3eaea3SApple OSS Distributions {
126*5e3eaea3SApple OSS Distributions kfree_type(struct secashead, p);
127*5e3eaea3SApple OSS Distributions }
128*5e3eaea3SApple OSS Distributions
129*5e3eaea3SApple OSS Distributions
130*5e3eaea3SApple OSS Distributions
131*5e3eaea3SApple OSS Distributions /*
132*5e3eaea3SApple OSS Distributions * secasvar management (reference counted)
133*5e3eaea3SApple OSS Distributions */
134*5e3eaea3SApple OSS Distributions struct secasvar *
135*5e3eaea3SApple OSS Distributions keydb_newsecasvar()
136*5e3eaea3SApple OSS Distributions {
137*5e3eaea3SApple OSS Distributions struct secasvar *p;
138*5e3eaea3SApple OSS Distributions
139*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED);
140*5e3eaea3SApple OSS Distributions
141*5e3eaea3SApple OSS Distributions p = kalloc_type(struct secasvar, Z_WAITOK_ZERO_NOFAIL);
142*5e3eaea3SApple OSS Distributions p->refcnt = 1;
143*5e3eaea3SApple OSS Distributions
144*5e3eaea3SApple OSS Distributions return p;
145*5e3eaea3SApple OSS Distributions }
146*5e3eaea3SApple OSS Distributions
147*5e3eaea3SApple OSS Distributions void
148*5e3eaea3SApple OSS Distributions keydb_refsecasvar(p)
149*5e3eaea3SApple OSS Distributions struct secasvar *p;
150*5e3eaea3SApple OSS Distributions {
151*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
152*5e3eaea3SApple OSS Distributions
153*5e3eaea3SApple OSS Distributions p->refcnt++;
154*5e3eaea3SApple OSS Distributions }
155*5e3eaea3SApple OSS Distributions
156*5e3eaea3SApple OSS Distributions void
157*5e3eaea3SApple OSS Distributions keydb_freesecasvar(p)
158*5e3eaea3SApple OSS Distributions struct secasvar *p;
159*5e3eaea3SApple OSS Distributions {
160*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
161*5e3eaea3SApple OSS Distributions
162*5e3eaea3SApple OSS Distributions p->refcnt--;
163*5e3eaea3SApple OSS Distributions /* negative refcnt will cause panic intentionally */
164*5e3eaea3SApple OSS Distributions if (p->refcnt <= 0) {
165*5e3eaea3SApple OSS Distributions keydb_delsecasvar(p);
166*5e3eaea3SApple OSS Distributions }
167*5e3eaea3SApple OSS Distributions }
168*5e3eaea3SApple OSS Distributions
169*5e3eaea3SApple OSS Distributions static void
170*5e3eaea3SApple OSS Distributions keydb_delsecasvar(p)
171*5e3eaea3SApple OSS Distributions struct secasvar *p;
172*5e3eaea3SApple OSS Distributions {
173*5e3eaea3SApple OSS Distributions if (p->refcnt) {
174*5e3eaea3SApple OSS Distributions panic("keydb_delsecasvar called with refcnt != 0");
175*5e3eaea3SApple OSS Distributions }
176*5e3eaea3SApple OSS Distributions
177*5e3eaea3SApple OSS Distributions kfree_type(struct secasvar, p);
178*5e3eaea3SApple OSS Distributions }
179*5e3eaea3SApple OSS Distributions #endif
180*5e3eaea3SApple OSS Distributions
181*5e3eaea3SApple OSS Distributions /*
182*5e3eaea3SApple OSS Distributions * secreplay management
183*5e3eaea3SApple OSS Distributions */
184*5e3eaea3SApple OSS Distributions struct secreplay *
keydb_newsecreplay(u_int8_t wsize)185*5e3eaea3SApple OSS Distributions keydb_newsecreplay(u_int8_t wsize)
186*5e3eaea3SApple OSS Distributions {
187*5e3eaea3SApple OSS Distributions struct secreplay *p;
188*5e3eaea3SApple OSS Distributions
189*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
190*5e3eaea3SApple OSS Distributions
191*5e3eaea3SApple OSS Distributions p = kalloc_type(struct secreplay, Z_NOWAIT | Z_ZERO);
192*5e3eaea3SApple OSS Distributions if (!p) {
193*5e3eaea3SApple OSS Distributions lck_mtx_unlock(sadb_mutex);
194*5e3eaea3SApple OSS Distributions p = kalloc_type(struct secreplay, Z_WAITOK | Z_ZERO | Z_NOFAIL);
195*5e3eaea3SApple OSS Distributions lck_mtx_lock(sadb_mutex);
196*5e3eaea3SApple OSS Distributions }
197*5e3eaea3SApple OSS Distributions
198*5e3eaea3SApple OSS Distributions if (wsize != 0) {
199*5e3eaea3SApple OSS Distributions p->wsize = wsize;
200*5e3eaea3SApple OSS Distributions p->bitmap = (caddr_t)kalloc_data(wsize, Z_NOWAIT | Z_ZERO);
201*5e3eaea3SApple OSS Distributions if (!p->bitmap) {
202*5e3eaea3SApple OSS Distributions lck_mtx_unlock(sadb_mutex);
203*5e3eaea3SApple OSS Distributions p->bitmap = (caddr_t)kalloc_data(wsize, Z_WAITOK | Z_ZERO | Z_NOFAIL);
204*5e3eaea3SApple OSS Distributions lck_mtx_lock(sadb_mutex);
205*5e3eaea3SApple OSS Distributions }
206*5e3eaea3SApple OSS Distributions }
207*5e3eaea3SApple OSS Distributions return p;
208*5e3eaea3SApple OSS Distributions }
209*5e3eaea3SApple OSS Distributions
210*5e3eaea3SApple OSS Distributions void
keydb_delsecreplay(struct secreplay * p)211*5e3eaea3SApple OSS Distributions keydb_delsecreplay(struct secreplay *p)
212*5e3eaea3SApple OSS Distributions {
213*5e3eaea3SApple OSS Distributions if (p->bitmap) {
214*5e3eaea3SApple OSS Distributions kfree_data(p->bitmap, p->wsize);
215*5e3eaea3SApple OSS Distributions }
216*5e3eaea3SApple OSS Distributions kfree_type(struct secreplay, p);
217*5e3eaea3SApple OSS Distributions }
218*5e3eaea3SApple OSS Distributions
219*5e3eaea3SApple OSS Distributions #if 0
220*5e3eaea3SApple OSS Distributions /* NOT USED
221*5e3eaea3SApple OSS Distributions * secreg management
222*5e3eaea3SApple OSS Distributions */
223*5e3eaea3SApple OSS Distributions struct secreg *
224*5e3eaea3SApple OSS Distributions keydb_newsecreg()
225*5e3eaea3SApple OSS Distributions {
226*5e3eaea3SApple OSS Distributions return kalloc_type(struct secreg, Z_WAITOK_ZERO_NOFAIL);
227*5e3eaea3SApple OSS Distributions }
228*5e3eaea3SApple OSS Distributions
229*5e3eaea3SApple OSS Distributions void
230*5e3eaea3SApple OSS Distributions keydb_delsecreg(p)
231*5e3eaea3SApple OSS Distributions struct secreg *p;
232*5e3eaea3SApple OSS Distributions {
233*5e3eaea3SApple OSS Distributions kfree_type(struct secreg, p);
234*5e3eaea3SApple OSS Distributions }
235*5e3eaea3SApple OSS Distributions #endif
236