]> git.saurik.com Git - apple/xnu.git/blame - bsd/netkey/keydb.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / bsd / netkey / keydb.c
CommitLineData
1c79356b
A
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
1c79356b
A
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
1c79356b 52MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
1c79356b 53
91447636 54static void keydb_delsecasvar(struct secasvar *);
1c79356b
A
55
56/*
57 * secpolicy management
58 */
59struct secpolicy *
60keydb_newsecpolicy()
61{
62 struct secpolicy *p;
63
0b4e3aa0 64 p = (struct secpolicy *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
1c79356b
A
65 if (!p)
66 return p;
67 bzero(p, sizeof(*p));
68 return p;
69}
70
71void
72keydb_delsecpolicy(p)
73 struct secpolicy *p;
74{
75
76 _FREE(p, M_SECA);
77}
78
79/*
80 * secashead management
81 */
82struct secashead *
83keydb_newsecashead()
84{
85 struct secashead *p;
86 int i;
87
0b4e3aa0 88 p = (struct secashead *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
1c79356b
A
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
97void
98keydb_delsecashead(p)
99 struct secashead *p;
100{
101
102 _FREE(p, M_SECA);
103}
104
105/*
106 * secasvar management (reference counted)
107 */
108struct secasvar *
109keydb_newsecasvar()
110{
111 struct secasvar *p;
112
0b4e3aa0 113 p = (struct secasvar *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
1c79356b
A
114 if (!p)
115 return p;
116 bzero(p, sizeof(*p));
117 p->refcnt = 1;
118 return p;
119}
120
121void
122keydb_refsecasvar(p)
123 struct secasvar *p;
124{
125 int s;
126
1c79356b 127 s = splnet();
1c79356b
A
128 p->refcnt++;
129 splx(s);
130}
131
132void
133keydb_freesecasvar(p)
134 struct secasvar *p;
135{
136 int s;
137
1c79356b 138 s = splnet();
1c79356b 139 p->refcnt--;
9bccf70c
A
140 /* negative refcnt will cause panic intentionally */
141 if (p->refcnt <= 0)
1c79356b
A
142 keydb_delsecasvar(p);
143 splx(s);
144}
145
146static void
147keydb_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 */
160struct secreplay *
161keydb_newsecreplay(wsize)
162 size_t wsize;
163{
164 struct secreplay *p;
165
0b4e3aa0 166 p = (struct secreplay *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
1c79356b
A
167 if (!p)
168 return p;
169
170 bzero(p, sizeof(*p));
171 if (wsize != 0) {
0b4e3aa0 172 p->bitmap = (caddr_t)_MALLOC(wsize, M_SECA, M_WAITOK);
1c79356b
A
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
183void
184keydb_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 */
196struct secreg *
197keydb_newsecreg()
198{
199 struct secreg *p;
200
0b4e3aa0 201 p = (struct secreg *)_MALLOC(sizeof(*p), M_SECA, M_WAITOK);
1c79356b
A
202 if (p)
203 bzero(p, sizeof(*p));
204 return p;
205}
206
207void
208keydb_delsecreg(p)
209 struct secreg *p;
210{
211
212 _FREE(p, M_SECA);
213}