]>
Commit | Line | Data |
---|---|---|
52b7d2ce A |
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 * | |
05434fec | 82 | getsainfo(src, dst, peer, use_nat_addr) |
52b7d2ce | 83 | const vchar_t *src, *dst, *peer; |
05434fec | 84 | int use_nat_addr; |
52b7d2ce A |
85 | { |
86 | struct sainfo *s = NULL; | |
87 | struct sainfo *anonymous = NULL; | |
88 | int pass = 1; | |
05434fec A |
89 | |
90 | if (use_nat_addr && lcconf->ext_nat_id == NULL) | |
91 | return NULL; | |
52b7d2ce | 92 | |
d1e348cf A |
93 | plog(LLV_DEBUG2, LOCATION, NULL, "getsainfo - src id:\n"); |
94 | if (src != NULL) | |
95 | plogdump(LLV_DEBUG2, src->v, src->l); | |
96 | else | |
97 | plog(LLV_DEBUG2, LOCATION, NULL, " anonymous\n"); | |
98 | plog(LLV_DEBUG2, LOCATION, NULL, "getsainfo - dst id:\n"); | |
99 | if (dst != NULL) | |
100 | plogdump(LLV_DEBUG2, dst->v, dst->l); | |
101 | else | |
102 | plog(LLV_DEBUG2, LOCATION, NULL, " anonymous\n"); | |
52b7d2ce A |
103 | if (peer == NULL) |
104 | pass = 2; | |
105 | again: | |
106 | LIST_FOREACH(s, &sitree, chain) { | |
d1e348cf A |
107 | #ifdef __APPLE__ |
108 | if (s->to_delete || s->to_remove) { | |
109 | continue; | |
110 | } | |
111 | #endif /* __APPLE__ */ | |
112 | if (s->idsrc != NULL) { | |
113 | plog(LLV_DEBUG2, LOCATION, NULL, "getsainfo - sainfo id - src & dst:\n"); | |
114 | plogdump(LLV_DEBUG2, s->idsrc->v, s->idsrc->l); | |
115 | plogdump(LLV_DEBUG2, s->iddst->v, s->iddst->l); | |
116 | } else { | |
117 | plog(LLV_DEBUG2, LOCATION, NULL, "getsainfo - sainfo id = anonymous\n"); | |
118 | } | |
52b7d2ce A |
119 | if (s->id_i != NULL) { |
120 | if (pass == 2) | |
121 | continue; | |
122 | if (memcmp(peer->v, s->id_i->v, s->id_i->l) != 0) | |
123 | continue; | |
124 | } else if (pass == 1) | |
125 | continue; | |
126 | if (s->idsrc == NULL) { | |
127 | anonymous = s; | |
128 | continue; | |
129 | } | |
130 | ||
131 | /* anonymous ? */ | |
132 | if (src == NULL) { | |
133 | if (anonymous != NULL) | |
134 | break; | |
135 | continue; | |
136 | } | |
137 | ||
05434fec A |
138 | if (memcmp(src->v, s->idsrc->v, s->idsrc->l) == 0) { |
139 | if (use_nat_addr) { | |
d1e348cf A |
140 | if (memcmp(lcconf->ext_nat_id->v, s->iddst->v, s->iddst->l) == 0) { |
141 | plog(LLV_DEBUG, LOCATION, NULL, | |
142 | "matched external nat address.\n"); | |
143 | plogdump(LLV_DEBUG2, lcconf->ext_nat_id->v, lcconf->ext_nat_id->l); | |
05434fec | 144 | return s; |
d1e348cf | 145 | } |
05434fec A |
146 | } else if (memcmp(dst->v, s->iddst->v, s->iddst->l) == 0) |
147 | return s; | |
148 | } | |
52b7d2ce A |
149 | } |
150 | ||
151 | if (anonymous) { | |
152 | plog(LLV_DEBUG, LOCATION, NULL, | |
153 | "anonymous sainfo selected.\n"); | |
154 | } else if (pass == 1) { | |
155 | pass = 2; | |
156 | goto again; | |
157 | } | |
158 | ||
159 | return anonymous; | |
160 | } | |
161 | ||
d1e348cf A |
162 | #ifdef __APPLE__ |
163 | int | |
164 | link_sainfo_to_ph2 (struct sainfo *new) | |
165 | { | |
166 | if (!new) { | |
167 | return(-1); | |
168 | } | |
169 | if (new->to_delete || | |
170 | new->to_remove) { | |
171 | return(-1); | |
172 | } | |
173 | new->linked_to_ph2++; | |
174 | return(0); | |
175 | } | |
176 | ||
177 | int | |
178 | unlink_sainfo_from_ph2 (struct sainfo *old) | |
179 | { | |
180 | if (!old) { | |
181 | return(-1); | |
182 | } | |
183 | if (old->linked_to_ph2 <= 0) { | |
184 | return(-1); | |
185 | } | |
186 | old->linked_to_ph2--; | |
187 | if (old->linked_to_ph2 == 0) { | |
188 | if (old->to_remove) { | |
189 | remsainfo(old); | |
190 | } | |
191 | if (old->to_delete) { | |
192 | delsainfo(old); | |
193 | } | |
194 | } | |
195 | return(0); | |
196 | } | |
197 | #endif | |
198 | ||
52b7d2ce A |
199 | struct sainfo * |
200 | newsainfo() | |
201 | { | |
202 | struct sainfo *new; | |
203 | ||
204 | new = racoon_calloc(1, sizeof(*new)); | |
205 | if (new == NULL) | |
206 | return NULL; | |
207 | ||
208 | new->lifetime = IPSECDOI_ATTR_SA_LD_SEC_DEFAULT; | |
209 | new->lifebyte = IPSECDOI_ATTR_SA_LD_KB_MAX; | |
d1e348cf A |
210 | #ifdef __APPLE__ |
211 | new->to_remove = FALSE; | |
212 | new->to_delete = FALSE; | |
213 | new->linked_to_ph2 = 0; | |
214 | #endif | |
52b7d2ce A |
215 | |
216 | return new; | |
217 | } | |
218 | ||
219 | void | |
220 | delsainfo(si) | |
221 | struct sainfo *si; | |
222 | { | |
223 | int i; | |
224 | ||
d1e348cf A |
225 | #ifdef __APPLE__ |
226 | if (si->linked_to_ph2) { | |
227 | si->to_delete = TRUE; | |
228 | return; | |
229 | } | |
230 | #endif | |
231 | ||
52b7d2ce A |
232 | for (i = 0; i < MAXALGCLASS; i++) |
233 | delsainfoalg(si->algs[i]); | |
234 | ||
235 | if (si->idsrc) | |
236 | vfree(si->idsrc); | |
237 | if (si->iddst) | |
238 | vfree(si->iddst); | |
239 | ||
d1e348cf A |
240 | #ifdef ENABLE_HYBRID |
241 | if (si->group) | |
242 | vfree(si->group); | |
243 | #endif | |
244 | ||
52b7d2ce A |
245 | racoon_free(si); |
246 | } | |
247 | ||
248 | void | |
249 | inssainfo(new) | |
250 | struct sainfo *new; | |
251 | { | |
252 | LIST_INSERT_HEAD(&sitree, new, chain); | |
253 | } | |
254 | ||
255 | void | |
256 | remsainfo(si) | |
257 | struct sainfo *si; | |
258 | { | |
d1e348cf A |
259 | #ifdef __APPLE__ |
260 | if (si->linked_to_ph2) { | |
261 | si->to_remove = TRUE; | |
262 | return; | |
263 | } | |
264 | #endif | |
52b7d2ce A |
265 | LIST_REMOVE(si, chain); |
266 | } | |
267 | ||
268 | void | |
269 | flushsainfo() | |
270 | { | |
271 | struct sainfo *s, *next; | |
272 | ||
273 | for (s = LIST_FIRST(&sitree); s; s = next) { | |
274 | next = LIST_NEXT(s, chain); | |
d1e348cf A |
275 | if (s->dynamic == 0) { |
276 | remsainfo(s); | |
277 | delsainfo(s); | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | void | |
283 | flushsainfo_dynamic(u_int32_t addr) | |
284 | { | |
285 | struct sainfo *s, *next; | |
286 | ||
287 | for (s = LIST_FIRST(&sitree); s; s = next) { | |
288 | next = LIST_NEXT(s, chain); | |
289 | if (s->dynamic == addr) { | |
290 | remsainfo(s); | |
291 | delsainfo(s); | |
292 | } | |
52b7d2ce A |
293 | } |
294 | } | |
295 | ||
296 | void | |
297 | initsainfo() | |
298 | { | |
299 | LIST_INIT(&sitree); | |
300 | } | |
301 | ||
302 | struct sainfoalg * | |
303 | newsainfoalg() | |
304 | { | |
305 | struct sainfoalg *new; | |
306 | ||
307 | new = racoon_calloc(1, sizeof(*new)); | |
308 | if (new == NULL) | |
309 | return NULL; | |
310 | ||
311 | return new; | |
312 | } | |
313 | ||
314 | void | |
315 | delsainfoalg(alg) | |
316 | struct sainfoalg *alg; | |
317 | { | |
318 | struct sainfoalg *a, *next; | |
319 | ||
320 | for (a = alg; a; a = next) { | |
321 | next = a->next; | |
322 | racoon_free(a); | |
323 | } | |
324 | } | |
325 | ||
326 | void | |
327 | inssainfoalg(head, new) | |
328 | struct sainfoalg **head; | |
329 | struct sainfoalg *new; | |
330 | { | |
331 | struct sainfoalg *a; | |
332 | ||
333 | for (a = *head; a && a->next; a = a->next) | |
334 | ; | |
335 | if (a) | |
336 | a->next = new; | |
337 | else | |
338 | *head = new; | |
339 | } | |
340 | ||
341 | const char * | |
342 | sainfo2str(si) | |
343 | const struct sainfo *si; | |
344 | { | |
d1e348cf A |
345 | char *idsrc_str; |
346 | char *iddst_str; | |
347 | char *idi_str; | |
52b7d2ce A |
348 | static char buf[256]; |
349 | ||
350 | if (si->idsrc == NULL) | |
351 | snprintf(buf, sizeof(buf), "anonymous"); | |
352 | else { | |
d1e348cf A |
353 | idsrc_str = ipsecdoi_id2str(si->idsrc); |
354 | if (idsrc_str) { | |
355 | snprintf(buf, sizeof(buf), "%s", idsrc_str); | |
356 | racoon_free(idsrc_str); | |
357 | } | |
358 | iddst_str = ipsecdoi_id2str(si->iddst); | |
359 | if (iddst_str) { | |
360 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
361 | " %s", iddst_str); | |
362 | racoon_free(iddst_str); | |
363 | } | |
52b7d2ce A |
364 | } |
365 | ||
d1e348cf A |
366 | if (si->id_i != NULL) { |
367 | idi_str = ipsecdoi_id2str(si->id_i); | |
368 | if (idi_str) { | |
369 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
370 | " from %s", idi_str); | |
371 | racoon_free(idi_str); | |
372 | } | |
373 | } | |
52b7d2ce A |
374 | |
375 | return buf; | |
376 | } |