]> git.saurik.com Git - apple/xnu.git/blame - bsd/netkey/keydb.c
xnu-4570.31.3.tar.gz
[apple/xnu.git] / bsd / netkey / keydb.c
CommitLineData
39037602
A
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
1c79356b
A
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
1c79356b
A
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
2d21ac55
A
80extern lck_mtx_t *sadb_mutex;
81
1c79356b 82MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
1c79356b 83
2d21ac55 84// static void keydb_delsecasvar(struct secasvar *); // not used
1c79356b
A
85
86/*
87 * secpolicy management
88 */
89struct secpolicy *
39037602 90keydb_newsecpolicy(void)
1c79356b
A
91{
92 struct secpolicy *p;
93
5ba3f43e 94 LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED);
2d21ac55 95
3e170ce0
A
96 return (struct secpolicy *)_MALLOC(sizeof(*p), M_SECA,
97 M_WAITOK | M_ZERO);
1c79356b
A
98}
99
100void
39037602 101keydb_delsecpolicy(struct secpolicy *p)
1c79356b 102{
1c79356b
A
103 _FREE(p, M_SECA);
104}
105
106/*
107 * secashead management
108 */
109struct secashead *
39037602 110keydb_newsecashead(void)
1c79356b
A
111{
112 struct secashead *p;
113 int i;
114
5ba3f43e 115 LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
2d21ac55 116
3e170ce0 117 p = (struct secashead *)_MALLOC(sizeof(*p), M_SECA, M_NOWAIT | M_ZERO);
2d21ac55
A
118 if (!p) {
119 lck_mtx_unlock(sadb_mutex);
3e170ce0
A
120 p = (struct secashead *)_MALLOC(sizeof(*p), M_SECA,
121 M_WAITOK | M_ZERO);
2d21ac55
A
122 lck_mtx_lock(sadb_mutex);
123 }
124 if (!p)
1c79356b 125 return p;
1c79356b
A
126 for (i = 0; i < sizeof(p->savtree)/sizeof(p->savtree[0]); i++)
127 LIST_INIT(&p->savtree[i]);
128 return p;
129}
130
2d21ac55 131#if 0
1c79356b
A
132void
133keydb_delsecashead(p)
134 struct secashead *p;
135{
136
137 _FREE(p, M_SECA);
138}
139
2d21ac55
A
140
141
142/*
1c79356b
A
143 * secasvar management (reference counted)
144 */
145struct secasvar *
146keydb_newsecasvar()
147{
148 struct secasvar *p;
149
5ba3f43e 150 LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED);
2d21ac55 151
0b4e3aa0 152 p = (struct secasvar *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
1c79356b
A
153 if (!p)
154 return p;
155 bzero(p, sizeof(*p));
156 p->refcnt = 1;
157 return p;
158}
159
160void
161keydb_refsecasvar(p)
162 struct secasvar *p;
163{
1c79356b 164
5ba3f43e 165 LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
2d21ac55 166
1c79356b 167 p->refcnt++;
1c79356b
A
168}
169
170void
171keydb_freesecasvar(p)
172 struct secasvar *p;
173{
1c79356b 174
5ba3f43e 175 LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
2d21ac55 176
1c79356b 177 p->refcnt--;
9bccf70c
A
178 /* negative refcnt will cause panic intentionally */
179 if (p->refcnt <= 0)
1c79356b 180 keydb_delsecasvar(p);
1c79356b
A
181}
182
183static void
184keydb_delsecasvar(p)
185 struct secasvar *p;
186{
187
188 if (p->refcnt)
189 panic("keydb_delsecasvar called with refcnt != 0");
190
191 _FREE(p, M_SECA);
192}
2d21ac55 193#endif
1c79356b
A
194
195/*
196 * secreplay management
197 */
198struct secreplay *
39037602 199keydb_newsecreplay(size_t wsize)
1c79356b
A
200{
201 struct secreplay *p;
2d21ac55 202
5ba3f43e 203 LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
2d21ac55 204
3e170ce0 205 p = (struct secreplay *)_MALLOC(sizeof(*p), M_SECA, M_NOWAIT | M_ZERO);
2d21ac55
A
206 if (!p) {
207 lck_mtx_unlock(sadb_mutex);
3e170ce0
A
208 p = (struct secreplay *)_MALLOC(sizeof(*p), M_SECA,
209 M_WAITOK | M_ZERO);
2d21ac55
A
210 lck_mtx_lock(sadb_mutex);
211 }
1c79356b
A
212 if (!p)
213 return p;
214
1c79356b 215 if (wsize != 0) {
3e170ce0 216 p->bitmap = (caddr_t)_MALLOC(wsize, M_SECA, M_NOWAIT | M_ZERO);
1c79356b 217 if (!p->bitmap) {
2d21ac55 218 lck_mtx_unlock(sadb_mutex);
3e170ce0
A
219 p->bitmap = (caddr_t)_MALLOC(wsize, M_SECA,
220 M_WAITOK | M_ZERO);
2d21ac55
A
221 lck_mtx_lock(sadb_mutex);
222 if (!p->bitmap) {
223 _FREE(p, M_SECA);
224 return NULL;
225 }
1c79356b 226 }
1c79356b
A
227 }
228 p->wsize = wsize;
229 return p;
230}
231
232void
39037602 233keydb_delsecreplay(struct secreplay *p)
1c79356b 234{
1c79356b
A
235 if (p->bitmap)
236 _FREE(p->bitmap, M_SECA);
237 _FREE(p, M_SECA);
238}
239
2d21ac55
A
240#if 0
241/* NOT USED
1c79356b
A
242 * secreg management
243 */
244struct secreg *
245keydb_newsecreg()
246{
247 struct secreg *p;
248
0b4e3aa0 249 p = (struct secreg *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
1c79356b
A
250 if (p)
251 bzero(p, sizeof(*p));
252 return p;
253}
254
255void
256keydb_delsecreg(p)
257 struct secreg *p;
258{
259
260 _FREE(p, M_SECA);
261}
2d21ac55 262#endif