]> git.saurik.com Git - apple/xnu.git/blob - bsd/netkey/keydb.c
xnu-201.tar.gz
[apple/xnu.git] / bsd / netkey / keydb.c
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 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
33 #include "opt_inet.h"
34 #ifdef __NetBSD__
35 #include "opt_ipsec.h"
36 #endif
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/errno.h>
46 #include <sys/queue.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50
51 #include <netinet/in.h>
52
53 #include <net/pfkeyv2.h>
54 #include <netkey/keydb.h>
55 #include <netinet6/ipsec.h>
56
57 #include <net/net_osdep.h>
58
59 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
60 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
61 #endif
62
63 static void keydb_delsecasvar __P((struct secasvar *));
64
65 /*
66 * secpolicy management
67 */
68 struct secpolicy *
69 keydb_newsecpolicy()
70 {
71 struct secpolicy *p;
72
73 p = (struct secpolicy *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
74 if (!p)
75 return p;
76 bzero(p, sizeof(*p));
77 return p;
78 }
79
80 void
81 keydb_delsecpolicy(p)
82 struct secpolicy *p;
83 {
84
85 _FREE(p, M_SECA);
86 }
87
88 /*
89 * secashead management
90 */
91 struct secashead *
92 keydb_newsecashead()
93 {
94 struct secashead *p;
95 int i;
96
97 p = (struct secashead *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
98 if (!p)
99 return p;
100 bzero(p, sizeof(*p));
101 for (i = 0; i < sizeof(p->savtree)/sizeof(p->savtree[0]); i++)
102 LIST_INIT(&p->savtree[i]);
103 return p;
104 }
105
106 void
107 keydb_delsecashead(p)
108 struct secashead *p;
109 {
110
111 _FREE(p, M_SECA);
112 }
113
114 /*
115 * secasvar management (reference counted)
116 */
117 struct secasvar *
118 keydb_newsecasvar()
119 {
120 struct secasvar *p;
121
122 p = (struct secasvar *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
123 if (!p)
124 return p;
125 bzero(p, sizeof(*p));
126 p->refcnt = 1;
127 return p;
128 }
129
130 void
131 keydb_refsecasvar(p)
132 struct secasvar *p;
133 {
134 int s;
135
136 #ifdef __NetBSD__
137 s = splsoftnet();
138 #else
139 s = splnet();
140 #endif
141 p->refcnt++;
142 splx(s);
143 }
144
145 void
146 keydb_freesecasvar(p)
147 struct secasvar *p;
148 {
149 int s;
150
151 #ifdef __NetBSD__
152 s = splsoftnet();
153 #else
154 s = splnet();
155 #endif
156 p->refcnt--;
157 if (p->refcnt == 0)
158 keydb_delsecasvar(p);
159 splx(s);
160 }
161
162 static void
163 keydb_delsecasvar(p)
164 struct secasvar *p;
165 {
166
167 if (p->refcnt)
168 panic("keydb_delsecasvar called with refcnt != 0");
169
170 _FREE(p, M_SECA);
171 }
172
173 /*
174 * secreplay management
175 */
176 struct secreplay *
177 keydb_newsecreplay(wsize)
178 size_t wsize;
179 {
180 struct secreplay *p;
181
182 p = (struct secreplay *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
183 if (!p)
184 return p;
185
186 bzero(p, sizeof(*p));
187 if (wsize != 0) {
188 p->bitmap = (caddr_t)_MALLOC(wsize, M_SECA, M_WAITOK);
189 if (!p->bitmap) {
190 _FREE(p, M_SECA);
191 return NULL;
192 }
193 bzero(p->bitmap, wsize);
194 }
195 p->wsize = wsize;
196 return p;
197 }
198
199 void
200 keydb_delsecreplay(p)
201 struct secreplay *p;
202 {
203
204 if (p->bitmap)
205 _FREE(p->bitmap, M_SECA);
206 _FREE(p, M_SECA);
207 }
208
209 /*
210 * secreg management
211 */
212 struct secreg *
213 keydb_newsecreg()
214 {
215 struct secreg *p;
216
217 p = (struct secreg *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
218 if (p)
219 bzero(p, sizeof(*p));
220 return p;
221 }
222
223 void
224 keydb_delsecreg(p)
225 struct secreg *p;
226 {
227
228 _FREE(p, M_SECA);
229 }