]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netkey/keydb.c
Add MODULE_DEPENDS for cam, pci, mca, eisa and isa where needed.
[FreeBSD/FreeBSD.git] / sys / netkey / keydb.c
1 /*      $KAME: keydb.c,v 1.82 2003/09/07 07:47:33 itojun 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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/errno.h>
45 #include <sys/queue.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 <netkey/keydb.h>
54 #include <netkey/key.h>
55 #include <netinet6/ipsec.h>
56
57 MALLOC_DEFINE(M_SECA, "key_mgmt", "security associations, key management");
58
59 /*
60  * secpolicy management
61  */
62 struct secpolicy *
63 keydb_newsecpolicy()
64 {
65         struct secpolicy *p;
66
67         p = (struct secpolicy *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
68         if (!p)
69                 return p;
70         bzero(p, sizeof(*p));
71         TAILQ_INSERT_TAIL(&sptailq, p, tailq);
72
73         return p;
74 }
75
76 u_int32_t
77 keydb_newspid(void)
78 {
79         u_int32_t newid = 0;
80         static u_int32_t lastalloc = IPSEC_MANUAL_POLICYID_MAX;
81         struct secpolicy *sp;
82
83         newid = lastalloc + 1;
84         /* XXX possible infinite loop */
85 again:
86         TAILQ_FOREACH(sp, &sptailq, tailq) {
87                 if (sp->id == newid)
88                         break;
89         }
90         if (sp != NULL) {
91                 if (newid + 1 < newid)  /* wraparound */
92                         newid = IPSEC_MANUAL_POLICYID_MAX + 1;
93                 else
94                         newid++;
95                 goto again;
96         }
97         lastalloc = newid;
98
99         return newid;
100 }
101
102 void
103 keydb_delsecpolicy(p)
104         struct secpolicy *p;
105 {
106
107         TAILQ_REMOVE(&sptailq, p, tailq);
108         if (p->spidx)
109                 free(p->spidx, M_SECA);
110         free(p, M_SECA);
111 }
112
113 int
114 keydb_setsecpolicyindex(p, idx)
115         struct secpolicy *p;
116         struct secpolicyindex *idx;
117 {
118
119         if (!p->spidx)
120                 p->spidx = (struct secpolicyindex *)malloc(sizeof(*p->spidx),
121                     M_SECA, M_NOWAIT);
122         if (!p->spidx)
123                 return ENOMEM;
124         memcpy(p->spidx, idx, sizeof(*p->spidx));
125         return 0;
126 }
127
128 /*
129  * secashead management
130  */
131 struct secashead *
132 keydb_newsecashead()
133 {
134         struct secashead *p;
135         int i;
136
137         p = (struct secashead *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
138         if (!p)
139                 return p;
140         bzero(p, sizeof(*p));
141         for (i = 0; i < sizeof(p->savtree)/sizeof(p->savtree[0]); i++)
142                 LIST_INIT(&p->savtree[i]);
143         return p;
144 }
145
146 void
147 keydb_delsecashead(p)
148         struct secashead *p;
149 {
150
151         free(p, M_SECA);
152 }
153
154 /*
155  * secasvar management (reference counted)
156  */
157 struct secasvar *
158 keydb_newsecasvar()
159 {
160         struct secasvar *p, *q;
161         static u_int32_t said = 0;
162
163         p = (struct secasvar *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
164         if (!p)
165                 return p;
166
167 again:
168         said++;
169         if (said == 0)
170                 said++;
171         TAILQ_FOREACH(q, &satailq, tailq) {
172                 if (q->id == said)
173                         goto again;
174                 if (TAILQ_NEXT(q, tailq)) {
175                         if (q->id < said && said < TAILQ_NEXT(q, tailq)->id)
176                                 break;
177                         if (q->id + 1 < TAILQ_NEXT(q, tailq)->id) {
178                                 said = q->id + 1;
179                                 break;
180                         }
181                 }
182         }
183
184         bzero(p, sizeof(*p));
185         p->id = said;
186         if (q)
187                 TAILQ_INSERT_AFTER(&satailq, q, p, tailq);
188         else
189                 TAILQ_INSERT_TAIL(&satailq, p, tailq);
190         return p;
191 }
192
193 void
194 keydb_delsecasvar(p)
195         struct secasvar *p;
196 {
197
198         TAILQ_REMOVE(&satailq, p, tailq);
199
200         free(p, M_SECA);
201 }
202
203 /*
204  * secreplay management
205  */
206 struct secreplay *
207 keydb_newsecreplay(wsize)
208         size_t wsize;
209 {
210         struct secreplay *p;
211
212         p = (struct secreplay *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
213         if (!p)
214                 return p;
215
216         bzero(p, sizeof(*p));
217         if (wsize != 0) {
218                 p->bitmap = malloc(wsize, M_SECA, M_NOWAIT);
219                 if (!p->bitmap) {
220                         free(p, M_SECA);
221                         return NULL;
222                 }
223                 bzero(p->bitmap, wsize);
224         }
225         p->wsize = wsize;
226         return p;
227 }
228
229 void
230 keydb_delsecreplay(p)
231         struct secreplay *p;
232 {
233
234         if (p->bitmap)
235                 free(p->bitmap, M_SECA);
236         free(p, M_SECA);
237 }
238
239 /*
240  * secreg management
241  */
242 struct secreg *
243 keydb_newsecreg()
244 {
245         struct secreg *p;
246
247         p = (struct secreg *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
248         if (p)
249                 bzero(p, sizeof(*p));
250         return p;
251 }
252
253 void
254 keydb_delsecreg(p)
255         struct secreg *p;
256 {
257
258         free(p, M_SECA);
259 }