kernel: Disable TCP_SIGNATURE in preparation for removing IPSEC.
[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/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>
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 "keydb.h"
53 #include "key.h"
54 #include <netinet6/ipsec.h>
55
56 #include <net/net_osdep.h>
57
58 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
59
60 static void keydb_delsecasvar (struct secasvar *);
61
62 /*
63  * secpolicy management
64  */
65 struct secpolicy *
66 keydb_newsecpolicy(void)
67 {
68         return(kmalloc(sizeof(struct secpolicy), M_SECA,
69                        M_INTWAIT | M_NULLOK | M_ZERO));
70 }
71
72 void
73 keydb_delsecpolicy(struct secpolicy *p)
74 {
75         kfree(p, M_SECA);
76 }
77
78 /*
79  * secashead management
80  */
81 struct secashead *
82 keydb_newsecashead(void)
83 {
84         struct secashead *p;
85         int i;
86
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;
93 }
94
95 void
96 keydb_delsecashead(struct secashead *p)
97 {
98
99         kfree(p, M_SECA);
100 }
101
102 /*
103  * secasvar management (reference counted)
104  */
105 struct secasvar *
106 keydb_newsecasvar(void)
107 {
108         struct secasvar *p;
109
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;
115 }
116
117 void
118 keydb_refsecasvar(struct secasvar *p)
119 {
120         lwkt_gettoken(&key_token);
121         p->refcnt++;
122         lwkt_reltoken(&key_token);
123 }
124
125 void
126 keydb_freesecasvar(struct secasvar *p)
127 {
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);
134 }
135
136 static void
137 keydb_delsecasvar(struct secasvar *p)
138 {
139
140         if (p->refcnt)
141                 panic("keydb_delsecasvar called with refcnt != 0");
142
143         kfree(p, M_SECA);
144 }
145
146 /*
147  * secreplay management
148  */
149 struct secreplay *
150 keydb_newsecreplay(size_t wsize)
151 {
152         struct secreplay *p;
153
154         p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_NULLOK | M_ZERO);
155         if (!p)
156                 return p;
157
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;
163                 }
164         }
165         p->wsize = wsize;
166         return p;
167 }
168
169 void
170 keydb_delsecreplay(struct secreplay *p)
171 {
172
173         if (p->bitmap)
174                 kfree(p->bitmap, M_SECA);
175         kfree(p, M_SECA);
176 }
177
178 /*
179  * secreg management
180  */
181 struct secreg *
182 keydb_newsecreg(void)
183 {
184         struct secreg *p;
185
186         p = kmalloc(sizeof(*p), M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
187         return p;
188 }
189
190 void
191 keydb_delsecreg(struct secreg *p)
192 {
193
194         kfree(p, M_SECA);
195 }