Initial import of binutils 2.22 on the new vendor branch
[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/param.h>
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 #include <sys/thread2.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49
50 #include <netinet/in.h>
51
52 #include <net/pfkeyv2.h>
53 #include "keydb.h"
54 #include "key.h"
55 #include <netinet6/ipsec.h>
56
57 #include <net/net_osdep.h>
58
59 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
60
61 static void keydb_delsecasvar (struct secasvar *);
62
63 /*
64  * secpolicy management
65  */
66 struct secpolicy *
67 keydb_newsecpolicy(void)
68 {
69         return(kmalloc(sizeof(struct secpolicy), M_SECA,
70                        M_INTWAIT | M_NULLOK | M_ZERO));
71 }
72
73 void
74 keydb_delsecpolicy(struct secpolicy *p)
75 {
76         kfree(p, M_SECA);
77 }
78
79 /*
80  * secashead management
81  */
82 struct secashead *
83 keydb_newsecashead(void)
84 {
85         struct secashead *p;
86         int i;
87
88         p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
89         if (!p)
90                 return p;
91         for (i = 0; i < NELEM(p->savtree); i++)
92                 LIST_INIT(&p->savtree[i]);
93         return p;
94 }
95
96 void
97 keydb_delsecashead(struct secashead *p)
98 {
99
100         kfree(p, M_SECA);
101 }
102
103 /*
104  * secasvar management (reference counted)
105  */
106 struct secasvar *
107 keydb_newsecasvar(void)
108 {
109         struct secasvar *p;
110
111         p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
112         if (!p)
113                 return p;
114         p->refcnt = 1;
115         return p;
116 }
117
118 void
119 keydb_refsecasvar(struct secasvar *p)
120 {
121         lwkt_gettoken(&key_token);
122         p->refcnt++;
123         lwkt_reltoken(&key_token);
124 }
125
126 void
127 keydb_freesecasvar(struct secasvar *p)
128 {
129         lwkt_gettoken(&key_token);
130         p->refcnt--;
131         /* negative refcnt will cause panic intentionally */
132         if (p->refcnt <= 0)
133                 keydb_delsecasvar(p);
134         lwkt_reltoken(&key_token);
135 }
136
137 static void
138 keydb_delsecasvar(struct secasvar *p)
139 {
140
141         if (p->refcnt)
142                 panic("keydb_delsecasvar called with refcnt != 0");
143
144         kfree(p, M_SECA);
145 }
146
147 /*
148  * secreplay management
149  */
150 struct secreplay *
151 keydb_newsecreplay(size_t wsize)
152 {
153         struct secreplay *p;
154
155         p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
156         if (!p)
157                 return p;
158
159         if (wsize != 0) {
160                 p->bitmap = (caddr_t)kmalloc(wsize, M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
161                 if (!p->bitmap) {
162                         kfree(p, M_SECA);
163                         return NULL;
164                 }
165         }
166         p->wsize = wsize;
167         return p;
168 }
169
170 void
171 keydb_delsecreplay(struct secreplay *p)
172 {
173
174         if (p->bitmap)
175                 kfree(p->bitmap, M_SECA);
176         kfree(p, M_SECA);
177 }
178
179 /*
180  * secreg management
181  */
182 struct secreg *
183 keydb_newsecreg(void)
184 {
185         struct secreg *p;
186
187         p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
188         return p;
189 }
190
191 void
192 keydb_delsecreg(struct secreg *p)
193 {
194
195         kfree(p, M_SECA);
196 }