xref: /xnu-8792.81.2/bsd/netkey/keydb.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
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 *
keydb_newsecpolicy(void)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
keydb_delsecpolicy(struct secpolicy * p)95 keydb_delsecpolicy(struct secpolicy *p)
96 {
97 	kfree_type(struct secpolicy, p);
98 }
99 
100 /*
101  * secashead management
102  */
103 struct secashead *
keydb_newsecashead(void)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 *
keydb_newsecreplay(u_int8_t wsize)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);
195 		lck_mtx_lock(sadb_mutex);
196 	}
197 	if (!p) {
198 		return p;
199 	}
200 
201 	if (wsize != 0) {
202 		p->bitmap = (caddr_t)kalloc_data(wsize, Z_NOWAIT | Z_ZERO);
203 		if (!p->bitmap) {
204 			lck_mtx_unlock(sadb_mutex);
205 			p->bitmap = (caddr_t)kalloc_data(wsize, Z_WAITOK | Z_ZERO);
206 			lck_mtx_lock(sadb_mutex);
207 			if (!p->bitmap) {
208 				kfree_type(struct secreplay, p);
209 				return NULL;
210 			}
211 		}
212 	}
213 	p->wsize = wsize;
214 	return p;
215 }
216 
217 void
keydb_delsecreplay(struct secreplay * p)218 keydb_delsecreplay(struct secreplay *p)
219 {
220 	if (p->bitmap) {
221 		kfree_data(p->bitmap, p->wsize);
222 	}
223 	kfree_type(struct secreplay, p);
224 }
225 
226 #if 0
227 /*	NOT USED
228  * secreg management
229  */
230 struct secreg *
231 keydb_newsecreg()
232 {
233 	return kalloc_type(struct secreg, Z_WAITOK_ZERO_NOFAIL);
234 }
235 
236 void
237 keydb_delsecreg(p)
238 struct secreg *p;
239 {
240 	kfree_type(struct secreg, p);
241 }
242 #endif
243