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/key.h> |
76 | #include <netkey/keydb.h> |
77 | #include <netinet6/ipsec.h> |
78 | |
79 | #include <net/net_osdep.h> |
80 | |
81 | // static void keydb_delsecasvar(struct secasvar *); // not used |
82 | |
83 | /* |
84 | * secpolicy management |
85 | */ |
86 | struct secpolicy * |
87 | keydb_newsecpolicy(void) |
88 | { |
89 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
90 | |
91 | return kalloc_type(struct secpolicy, Z_WAITOK | Z_ZERO); |
92 | } |
93 | |
94 | void |
95 | keydb_delsecpolicy(struct secpolicy *p) |
96 | { |
97 | kfree_type(struct secpolicy, p); |
98 | } |
99 | |
100 | /* |
101 | * secashead management |
102 | */ |
103 | struct secashead * |
104 | keydb_newsecashead(void) |
105 | { |
106 | struct secashead *p; |
107 | |
108 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
109 | |
110 | p = kalloc_type(struct secashead, Z_NOWAIT | Z_ZERO); |
111 | if (!p) { |
112 | lck_mtx_unlock(sadb_mutex); |
113 | p = kalloc_type(struct secashead, Z_WAITOK | Z_ZERO | Z_NOFAIL); |
114 | lck_mtx_lock(sadb_mutex); |
115 | } |
116 | for (size_t i = 0; i < ARRAY_COUNT(p->savtree); i++) { |
117 | LIST_INIT(&p->savtree[i]); |
118 | } |
119 | return p; |
120 | } |
121 | |
122 | #if 0 |
123 | void |
124 | keydb_delsecashead(struct secashead *p) |
125 | { |
126 | kfree_type(struct secashead, p); |
127 | } |
128 | |
129 | |
130 | |
131 | /* |
132 | * secasvar management (reference counted) |
133 | */ |
134 | struct secasvar * |
135 | keydb_newsecasvar() |
136 | { |
137 | struct secasvar *p; |
138 | |
139 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_NOTOWNED); |
140 | |
141 | p = kalloc_type(struct secasvar, Z_WAITOK_ZERO_NOFAIL); |
142 | p->refcnt = 1; |
143 | |
144 | return p; |
145 | } |
146 | |
147 | void |
148 | keydb_refsecasvar(p) |
149 | struct secasvar *p; |
150 | { |
151 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
152 | |
153 | p->refcnt++; |
154 | } |
155 | |
156 | void |
157 | keydb_freesecasvar(p) |
158 | struct secasvar *p; |
159 | { |
160 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
161 | |
162 | p->refcnt--; |
163 | /* negative refcnt will cause panic intentionally */ |
164 | if (p->refcnt <= 0) { |
165 | keydb_delsecasvar(p); |
166 | } |
167 | } |
168 | |
169 | static void |
170 | keydb_delsecasvar(p) |
171 | struct secasvar *p; |
172 | { |
173 | if (p->refcnt) { |
174 | panic("keydb_delsecasvar called with refcnt != 0" ); |
175 | } |
176 | |
177 | kfree_type(struct secasvar, p); |
178 | } |
179 | #endif |
180 | |
181 | /* |
182 | * secreplay management |
183 | */ |
184 | struct secreplay * |
185 | keydb_newsecreplay(u_int8_t wsize) |
186 | { |
187 | struct secreplay *p; |
188 | |
189 | LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED); |
190 | |
191 | p = kalloc_type(struct secreplay, Z_NOWAIT | Z_ZERO); |
192 | if (!p) { |
193 | lck_mtx_unlock(sadb_mutex); |
194 | p = kalloc_type(struct secreplay, Z_WAITOK | Z_ZERO | Z_NOFAIL); |
195 | lck_mtx_lock(sadb_mutex); |
196 | } |
197 | |
198 | if (wsize != 0) { |
199 | p->wsize = wsize; |
200 | p->bitmap = (caddr_t)kalloc_data(wsize, Z_NOWAIT | Z_ZERO); |
201 | if (!p->bitmap) { |
202 | lck_mtx_unlock(sadb_mutex); |
203 | p->bitmap = (caddr_t)kalloc_data(wsize, Z_WAITOK | Z_ZERO | Z_NOFAIL); |
204 | lck_mtx_lock(sadb_mutex); |
205 | } |
206 | } |
207 | return p; |
208 | } |
209 | |
210 | void |
211 | keydb_delsecreplay(struct secreplay *p) |
212 | { |
213 | if (p->bitmap) { |
214 | kfree_data(p->bitmap, p->wsize); |
215 | } |
216 | kfree_type(struct secreplay, p); |
217 | } |
218 | |
219 | #if 0 |
220 | /* NOT USED |
221 | * secreg management |
222 | */ |
223 | struct secreg * |
224 | keydb_newsecreg() |
225 | { |
226 | return kalloc_type(struct secreg, Z_WAITOK_ZERO_NOFAIL); |
227 | } |
228 | |
229 | void |
230 | keydb_delsecreg(p) |
231 | struct secreg *p; |
232 | { |
233 | kfree_type(struct secreg, p); |
234 | } |
235 | #endif |
236 | |