kernel - Fix races created by a comedy of circumstansces (3)
[dragonfly.git] / sys / netproto / key / keydb.c
blobdaac155a7e46669f7d87e177976e89c4ae8ad5c5
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 $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
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.
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.
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.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 #include <sys/thread2.h>
46 #include <net/if.h>
47 #include <net/route.h>
49 #include <netinet/in.h>
51 #include <net/pfkeyv2.h>
52 #include "keydb.h"
53 #include "key.h"
54 #include <netinet6/ipsec.h>
56 #include <net/net_osdep.h>
58 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
60 static void keydb_delsecasvar (struct secasvar *);
63 * secpolicy management
65 struct secpolicy *
66 keydb_newsecpolicy(void)
68 return(kmalloc(sizeof(struct secpolicy), M_SECA,
69 M_INTWAIT | M_NULLOK | M_ZERO));
72 void
73 keydb_delsecpolicy(struct secpolicy *p)
75 kfree(p, M_SECA);
79 * secashead management
81 struct secashead *
82 keydb_newsecashead(void)
84 struct secashead *p;
85 int i;
87 p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
88 if (!p)
89 return p;
90 for (i = 0; i < NELEM(p->savtree); i++)
91 LIST_INIT(&p->savtree[i]);
92 return p;
95 void
96 keydb_delsecashead(struct secashead *p)
99 kfree(p, M_SECA);
103 * secasvar management (reference counted)
105 struct secasvar *
106 keydb_newsecasvar(void)
108 struct secasvar *p;
110 p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
111 if (!p)
112 return p;
113 p->refcnt = 1;
114 return p;
117 void
118 keydb_refsecasvar(struct secasvar *p)
120 lwkt_gettoken(&key_token);
121 p->refcnt++;
122 lwkt_reltoken(&key_token);
125 void
126 keydb_freesecasvar(struct secasvar *p)
128 lwkt_gettoken(&key_token);
129 p->refcnt--;
130 /* negative refcnt will cause panic intentionally */
131 if (p->refcnt <= 0)
132 keydb_delsecasvar(p);
133 lwkt_reltoken(&key_token);
136 static void
137 keydb_delsecasvar(struct secasvar *p)
140 if (p->refcnt)
141 panic("keydb_delsecasvar called with refcnt != 0");
143 kfree(p, M_SECA);
147 * secreplay management
149 struct secreplay *
150 keydb_newsecreplay(size_t wsize)
152 struct secreplay *p;
154 p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
155 if (!p)
156 return p;
158 if (wsize != 0) {
159 p->bitmap = (caddr_t)kmalloc(wsize, M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
160 if (!p->bitmap) {
161 kfree(p, M_SECA);
162 return NULL;
165 p->wsize = wsize;
166 return p;
169 void
170 keydb_delsecreplay(struct secreplay *p)
173 if (p->bitmap)
174 kfree(p->bitmap, M_SECA);
175 kfree(p, M_SECA);
179 * secreg management
181 struct secreg *
182 keydb_newsecreg(void)
184 struct secreg *p;
186 p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
187 return p;
190 void
191 keydb_delsecreg(struct secreg *p)
194 kfree(p, M_SECA);