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