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