xref: /xnu-11215.1.10/bsd/netkey/keydb.c (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
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 /*
123  * secreplay management
124  */
125 struct secreplay *
keydb_newsecreplay(u_int8_t wsize)126 keydb_newsecreplay(u_int8_t wsize)
127 {
128 	struct secreplay *p;
129 	caddr_t tmp_bitmap = NULL;
130 
131 	LCK_MTX_ASSERT(sadb_mutex, LCK_MTX_ASSERT_OWNED);
132 
133 	p = kalloc_type(struct secreplay, Z_NOWAIT | Z_ZERO);
134 	if (!p) {
135 		lck_mtx_unlock(sadb_mutex);
136 		p = kalloc_type(struct secreplay, Z_WAITOK | Z_ZERO | Z_NOFAIL);
137 		lck_mtx_lock(sadb_mutex);
138 	}
139 
140 	if (wsize != 0) {
141 		tmp_bitmap = (caddr_t)kalloc_data(wsize, Z_NOWAIT | Z_ZERO);
142 		if (!tmp_bitmap) {
143 			lck_mtx_unlock(sadb_mutex);
144 			tmp_bitmap = (caddr_t)kalloc_data(wsize, Z_WAITOK | Z_ZERO | Z_NOFAIL);
145 			lck_mtx_lock(sadb_mutex);
146 		}
147 
148 		p->bitmap = tmp_bitmap;
149 		p->wsize = wsize;
150 	}
151 	return p;
152 }
153 
154 void
keydb_delsecreplay(struct secreplay * p)155 keydb_delsecreplay(struct secreplay *p)
156 {
157 	if (p->bitmap) {
158 		kfree_data_sized_by(p->bitmap, p->wsize);
159 	}
160 	kfree_type(struct secreplay, p);
161 }
162