and added files
[bcm963xx.git] / userapps / opensource / ipsec-tools / src / racoon / sainfo.c
1 /*      $KAME: sainfo.c,v 1.16 2003/06/27 07:32:39 sakane 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 "config.h"
33
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/queue.h>
38
39 #include <netinet/in.h>
40 #include <netinet/in.h> 
41 #ifdef HAVE_NETINET6_IPSEC
42 #  include <netinet6/ipsec.h>
43 #else 
44 #  include <netinet/ipsec.h>
45 #endif
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <errno.h>
51
52 #include "var.h"
53 #include "misc.h"
54 #include "vmbuf.h"
55 #include "plog.h"
56 #include "sockmisc.h"
57 #include "debug.h"
58
59 #include "localconf.h"
60 #include "isakmp_var.h"
61 #include "isakmp.h"
62 #include "ipsec_doi.h"
63 #include "oakley.h"
64 #include "handler.h"
65 #include "algorithm.h"
66 #include "sainfo.h"
67 #include "gcmalloc.h"
68
69 static LIST_HEAD(_sitree, sainfo) sitree;
70
71 /* %%%
72  * modules for ipsec sa info
73  */
74 /*
75  * return matching entry.
76  * no matching entry found and if there is anonymous entry, return it.
77  * else return NULL.
78  * XXX by each data type, should be changed to compare the buffer.
79  * First pass is for sainfo from a specified peer, second for others.
80  */
81 struct sainfo *
82 getsainfo(src, dst, peer)
83         const vchar_t *src, *dst, *peer;
84 {
85         struct sainfo *s = NULL;
86         struct sainfo *anonymous = NULL;
87         int pass = 1;
88
89         if (peer == NULL)
90                 pass = 2;
91     again:
92         LIST_FOREACH(s, &sitree, chain) {
93                 if (s->id_i != NULL) {
94                         if (pass == 2)
95                                 continue;
96                         if (memcmp(peer->v, s->id_i->v, s->id_i->l) != 0)
97                                 continue;
98                 } else if (pass == 1)
99                         continue;
100                 if (s->idsrc == NULL) {
101                         anonymous = s;
102                         continue;
103                 }
104
105                 /* anonymous ? */
106                 if (src == NULL) {
107                         if (anonymous != NULL)
108                                 break;
109                         continue;
110                 }
111
112                 if (memcmp(src->v, s->idsrc->v, s->idsrc->l) == 0
113                  && memcmp(dst->v, s->iddst->v, s->iddst->l) == 0)
114                         return s;
115         }
116
117         if (anonymous) {
118                 plog(LLV_DEBUG, LOCATION, NULL,
119                         "anonymous sainfo selected.\n");
120         } else if (pass == 1) {
121                 pass = 2;
122                 goto again;
123         }
124
125         return anonymous;
126 }
127
128 struct sainfo *
129 newsainfo()
130 {
131         struct sainfo *new;
132
133         new = racoon_calloc(1, sizeof(*new));
134         if (new == NULL)
135                 return NULL;
136
137         new->lifetime = IPSECDOI_ATTR_SA_LD_SEC_DEFAULT;
138         new->lifebyte = IPSECDOI_ATTR_SA_LD_KB_MAX;
139
140         return new;
141 }
142
143 void
144 delsainfo(si)
145         struct sainfo *si;
146 {
147         int i;
148
149         for (i = 0; i < MAXALGCLASS; i++)
150                 delsainfoalg(si->algs[i]);
151
152         if (si->idsrc)
153                 vfree(si->idsrc);
154         if (si->iddst)
155                 vfree(si->iddst);
156
157         racoon_free(si);
158 }
159
160 void
161 inssainfo(new)
162         struct sainfo *new;
163 {
164         LIST_INSERT_HEAD(&sitree, new, chain);
165 }
166
167 void
168 remsainfo(si)
169         struct sainfo *si;
170 {
171         LIST_REMOVE(si, chain);
172 }
173
174 void
175 flushsainfo()
176 {
177         struct sainfo *s, *next;
178
179         for (s = LIST_FIRST(&sitree); s; s = next) {
180                 next = LIST_NEXT(s, chain);
181                 remsainfo(s);
182                 delsainfo(s);
183         }
184 }
185
186 void
187 initsainfo()
188 {
189         LIST_INIT(&sitree);
190 }
191
192 struct sainfoalg *
193 newsainfoalg()
194 {
195         struct sainfoalg *new;
196
197         new = racoon_calloc(1, sizeof(*new));
198         if (new == NULL)
199                 return NULL;
200
201         return new;
202 }
203
204 void
205 delsainfoalg(alg)
206         struct sainfoalg *alg;
207 {
208         struct sainfoalg *a, *next;
209
210         for (a = alg; a; a = next) {
211                 next = a->next;
212                 racoon_free(a);
213         }
214 }
215
216 void
217 inssainfoalg(head, new)
218         struct sainfoalg **head;
219         struct sainfoalg *new;
220 {
221         struct sainfoalg *a;
222
223         for (a = *head; a && a->next; a = a->next)
224                 ;
225         if (a)
226                 a->next = new;
227         else
228                 *head = new;
229 }
230
231 const char *
232 sainfo2str(si)
233         const struct sainfo *si;
234 {
235         static char buf[256];
236
237         if (si->idsrc == NULL)
238                 snprintf(buf, sizeof(buf), "anonymous");
239         else {
240                 snprintf(buf, sizeof(buf), "%s", ipsecdoi_id2str(si->idsrc));
241                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
242                         " %s", ipsecdoi_id2str(si->iddst));
243         }
244
245         if (si->id_i != NULL)
246                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
247                         " from %s", ipsecdoi_id2str(si->id_i));
248
249         return buf;
250 }