Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / netproto / key / keydb.c
1 /*      $FreeBSD: src/sys/netkey/keydb.c,v 1.1.2.1 2000/07/15 07:14:42 kris Exp $       */
2 /*      $KAME: keydb.c,v 1.64 2000/05/11 17:02:30 itojun Exp $  */
3
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/errno.h>
43 #include <sys/queue.h>
44
45 #include <net/if.h>
46 #include <net/route.h>
47
48 #include <netinet/in.h>
49
50 #include <net/pfkeyv2.h>
51 #include <netkey/keydb.h>
52 #include <netinet6/ipsec.h>
53
54 #include <net/net_osdep.h>
55
56 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
57
58 static void keydb_delsecasvar __P((struct secasvar *));
59
60 /*
61  * secpolicy management
62  */
63 struct secpolicy *
64 keydb_newsecpolicy()
65 {
66         struct secpolicy *p;
67
68         p = (struct secpolicy *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
69         if (!p)
70                 return p;
71         bzero(p, sizeof(*p));
72         return p;
73 }
74
75 void
76 keydb_delsecpolicy(p)
77         struct secpolicy *p;
78 {
79
80         free(p, M_SECA);
81 }
82
83 /*
84  * secashead management
85  */
86 struct secashead *
87 keydb_newsecashead()
88 {
89         struct secashead *p;
90         int i;
91
92         p = (struct secashead *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
93         if (!p)
94                 return p;
95         bzero(p, sizeof(*p));
96         for (i = 0; i < sizeof(p->savtree)/sizeof(p->savtree[0]); i++)
97                 LIST_INIT(&p->savtree[i]);
98         return p;
99 }
100
101 void
102 keydb_delsecashead(p)
103         struct secashead *p;
104 {
105
106         free(p, M_SECA);
107 }
108
109 /*
110  * secasvar management (reference counted)
111  */
112 struct secasvar *
113 keydb_newsecasvar()
114 {
115         struct secasvar *p;
116
117         p = (struct secasvar *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
118         if (!p)
119                 return p;
120         bzero(p, sizeof(*p));
121         p->refcnt = 1;
122         return p;
123 }
124
125 void
126 keydb_refsecasvar(p)
127         struct secasvar *p;
128 {
129         int s;
130
131         s = splnet();
132         p->refcnt++;
133         splx(s);
134 }
135
136 void
137 keydb_freesecasvar(p)
138         struct secasvar *p;
139 {
140         int s;
141
142         s = splnet();
143         p->refcnt--;
144         /* negative refcnt will cause panic intentionally */
145         if (p->refcnt <= 0)
146                 keydb_delsecasvar(p);
147         splx(s);
148 }
149
150 static void
151 keydb_delsecasvar(p)
152         struct secasvar *p;
153 {
154
155         if (p->refcnt)
156                 panic("keydb_delsecasvar called with refcnt != 0");
157
158         free(p, M_SECA);
159 }
160
161 /*
162  * secreplay management
163  */
164 struct secreplay *
165 keydb_newsecreplay(wsize)
166         size_t wsize;
167 {
168         struct secreplay *p;
169
170         p = (struct secreplay *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
171         if (!p)
172                 return p;
173
174         bzero(p, sizeof(*p));
175         if (wsize != 0) {
176                 p->bitmap = (caddr_t)malloc(wsize, M_SECA, M_NOWAIT);
177                 if (!p->bitmap) {
178                         free(p, M_SECA);
179                         return NULL;
180                 }
181                 bzero(p->bitmap, wsize);
182         }
183         p->wsize = wsize;
184         return p;
185 }
186
187 void
188 keydb_delsecreplay(p)
189         struct secreplay *p;
190 {
191
192         if (p->bitmap)
193                 free(p->bitmap, M_SECA);
194         free(p, M_SECA);
195 }
196
197 /*
198  * secreg management
199  */
200 struct secreg *
201 keydb_newsecreg()
202 {
203         struct secreg *p;
204
205         p = (struct secreg *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
206         if (p)
207                 bzero(p, sizeof(*p));
208         return p;
209 }
210
211 void
212 keydb_delsecreg(p)
213         struct secreg *p;
214 {
215
216         free(p, M_SECA);
217 }