]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
52 | extern lck_mtx_t *sadb_mutex; | |
53 | ||
54 | MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management"); | |
55 | ||
56 | // static void keydb_delsecasvar(struct secasvar *); // not used | |
57 | ||
58 | /* | |
59 | * secpolicy management | |
60 | */ | |
61 | struct secpolicy * | |
62 | keydb_newsecpolicy() | |
63 | { | |
64 | struct secpolicy *p; | |
65 | ||
66 | lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); | |
67 | ||
68 | return (struct secpolicy *)_MALLOC(sizeof(*p), M_SECA, | |
69 | M_WAITOK | M_ZERO); | |
70 | } | |
71 | ||
72 | void | |
73 | keydb_delsecpolicy(p) | |
74 | struct secpolicy *p; | |
75 | { | |
76 | ||
77 | _FREE(p, M_SECA); | |
78 | } | |
79 | ||
80 | /* | |
81 | * secashead management | |
82 | */ | |
83 | struct secashead * | |
84 | keydb_newsecashead() | |
85 | { | |
86 | struct secashead *p; | |
87 | int i; | |
88 | ||
89 | lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_OWNED); | |
90 | ||
91 | p = (struct secashead *)_MALLOC(sizeof(*p), M_SECA, M_NOWAIT | M_ZERO); | |
92 | if (!p) { | |
93 | lck_mtx_unlock(sadb_mutex); | |
94 | p = (struct secashead *)_MALLOC(sizeof(*p), M_SECA, | |
95 | M_WAITOK | M_ZERO); | |
96 | lck_mtx_lock(sadb_mutex); | |
97 | } | |
98 | if (!p) | |
99 | return p; | |
100 | for (i = 0; i < sizeof(p->savtree)/sizeof(p->savtree[0]); i++) | |
101 | LIST_INIT(&p->savtree[i]); | |
102 | return p; | |
103 | } | |
104 | ||
105 | #if 0 | |
106 | void | |
107 | keydb_delsecashead(p) | |
108 | struct secashead *p; | |
109 | { | |
110 | ||
111 | _FREE(p, M_SECA); | |
112 | } | |
113 | ||
114 | ||
115 | ||
116 | /* | |
117 | * secasvar management (reference counted) | |
118 | */ | |
119 | struct secasvar * | |
120 | keydb_newsecasvar() | |
121 | { | |
122 | struct secasvar *p; | |
123 | ||
124 | lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); | |
125 | ||
126 | p = (struct secasvar *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK); | |
127 | if (!p) | |
128 | return p; | |
129 | bzero(p, sizeof(*p)); | |
130 | p->refcnt = 1; | |
131 | return p; | |
132 | } | |
133 | ||
134 | void | |
135 | keydb_refsecasvar(p) | |
136 | struct secasvar *p; | |
137 | { | |
138 | ||
139 | lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_OWNED); | |
140 | ||
141 | p->refcnt++; | |
142 | } | |
143 | ||
144 | void | |
145 | keydb_freesecasvar(p) | |
146 | struct secasvar *p; | |
147 | { | |
148 | ||
149 | lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_OWNED); | |
150 | ||
151 | p->refcnt--; | |
152 | /* negative refcnt will cause panic intentionally */ | |
153 | if (p->refcnt <= 0) | |
154 | keydb_delsecasvar(p); | |
155 | } | |
156 | ||
157 | static void | |
158 | keydb_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 | } | |
167 | #endif | |
168 | ||
169 | /* | |
170 | * secreplay management | |
171 | */ | |
172 | struct secreplay * | |
173 | keydb_newsecreplay(wsize) | |
174 | size_t wsize; | |
175 | { | |
176 | struct secreplay *p; | |
177 | ||
178 | lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_OWNED); | |
179 | ||
180 | p = (struct secreplay *)_MALLOC(sizeof(*p), M_SECA, M_NOWAIT | M_ZERO); | |
181 | if (!p) { | |
182 | lck_mtx_unlock(sadb_mutex); | |
183 | p = (struct secreplay *)_MALLOC(sizeof(*p), M_SECA, | |
184 | M_WAITOK | M_ZERO); | |
185 | lck_mtx_lock(sadb_mutex); | |
186 | } | |
187 | if (!p) | |
188 | return p; | |
189 | ||
190 | if (wsize != 0) { | |
191 | p->bitmap = (caddr_t)_MALLOC(wsize, M_SECA, M_NOWAIT | M_ZERO); | |
192 | if (!p->bitmap) { | |
193 | lck_mtx_unlock(sadb_mutex); | |
194 | p->bitmap = (caddr_t)_MALLOC(wsize, M_SECA, | |
195 | M_WAITOK | M_ZERO); | |
196 | lck_mtx_lock(sadb_mutex); | |
197 | if (!p->bitmap) { | |
198 | _FREE(p, M_SECA); | |
199 | return NULL; | |
200 | } | |
201 | } | |
202 | } | |
203 | p->wsize = wsize; | |
204 | return p; | |
205 | } | |
206 | ||
207 | void | |
208 | keydb_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 | ||
217 | #if 0 | |
218 | /* NOT USED | |
219 | * secreg management | |
220 | */ | |
221 | struct secreg * | |
222 | keydb_newsecreg() | |
223 | { | |
224 | struct secreg *p; | |
225 | ||
226 | p = (struct secreg *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK); | |
227 | if (p) | |
228 | bzero(p, sizeof(*p)); | |
229 | return p; | |
230 | } | |
231 | ||
232 | void | |
233 | keydb_delsecreg(p) | |
234 | struct secreg *p; | |
235 | { | |
236 | ||
237 | _FREE(p, M_SECA); | |
238 | } | |
239 | #endif |