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