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