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