1 /* $KAME: sainfo.c,v 1.16 2003/06/27 07:32:39 sakane Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/queue.h>
39 #include <netinet/in.h>
40 #include <netinet/in.h>
41 #ifdef HAVE_NETINET6_IPSEC
42 # include <netinet6/ipsec.h>
44 # include <netinet/ipsec.h>
59 #include "localconf.h"
60 #include "isakmp_var.h"
62 #include "ipsec_doi.h"
65 #include "algorithm.h"
69 static LIST_HEAD(_sitree
, sainfo
) sitree
;
72 * modules for ipsec sa info
75 * return matching entry.
76 * no matching entry found and if there is anonymous entry, return it.
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.
82 getsainfo(src
, dst
, peer
, use_nat_addr
)
83 const vchar_t
*src
, *dst
, *peer
;
86 struct sainfo
*s
= NULL
;
87 struct sainfo
*anonymous
= NULL
;
90 if (use_nat_addr
&& lcconf
->ext_nat_id
== NULL
)
93 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo - src id:\n");
95 plogdump(LLV_DEBUG2
, src
->v
, src
->l
);
97 plog(LLV_DEBUG2
, LOCATION
, NULL
, " anonymous\n");
98 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo - dst id:\n");
100 plogdump(LLV_DEBUG2
, dst
->v
, dst
->l
);
102 plog(LLV_DEBUG2
, LOCATION
, NULL
, " anonymous\n");
106 LIST_FOREACH(s
, &sitree
, chain
) {
108 if (s
->to_delete
|| s
->to_remove
) {
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
);
117 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo - sainfo id = anonymous\n");
119 if (s
->id_i
!= NULL
) {
122 if (memcmp(peer
->v
, s
->id_i
->v
, s
->id_i
->l
) != 0)
124 } else if (pass
== 1)
126 if (s
->idsrc
== NULL
) {
133 if (anonymous
!= NULL
)
138 if (memcmp(src
->v
, s
->idsrc
->v
, s
->idsrc
->l
) == 0) {
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
);
146 } else if (memcmp(dst
->v
, s
->iddst
->v
, s
->iddst
->l
) == 0)
152 plog(LLV_DEBUG
, LOCATION
, NULL
,
153 "anonymous sainfo selected.\n");
154 } else if (pass
== 1) {
164 link_sainfo_to_ph2 (struct sainfo
*new)
169 if (new->to_delete
||
173 new->linked_to_ph2
++;
178 unlink_sainfo_from_ph2 (struct sainfo
*old
)
183 if (old
->linked_to_ph2
<= 0) {
186 old
->linked_to_ph2
--;
187 if (old
->linked_to_ph2
== 0) {
188 if (old
->to_remove
) {
191 if (old
->to_delete
) {
204 new = racoon_calloc(1, sizeof(*new));
208 new->lifetime
= IPSECDOI_ATTR_SA_LD_SEC_DEFAULT
;
209 new->lifebyte
= IPSECDOI_ATTR_SA_LD_KB_MAX
;
211 new->to_remove
= FALSE
;
212 new->to_delete
= FALSE
;
213 new->linked_to_ph2
= 0;
226 if (si
->linked_to_ph2
) {
227 si
->to_delete
= TRUE
;
232 for (i
= 0; i
< MAXALGCLASS
; i
++)
233 delsainfoalg(si
->algs
[i
]);
252 LIST_INSERT_HEAD(&sitree
, new, chain
);
260 if (si
->linked_to_ph2
) {
261 si
->to_remove
= TRUE
;
265 LIST_REMOVE(si
, chain
);
271 struct sainfo
*s
, *next
;
273 for (s
= LIST_FIRST(&sitree
); s
; s
= next
) {
274 next
= LIST_NEXT(s
, chain
);
275 if (s
->dynamic
== 0) {
283 flushsainfo_dynamic(u_int32_t addr
)
285 struct sainfo
*s
, *next
;
287 for (s
= LIST_FIRST(&sitree
); s
; s
= next
) {
288 next
= LIST_NEXT(s
, chain
);
289 if (s
->dynamic
== addr
) {
305 struct sainfoalg
*new;
307 new = racoon_calloc(1, sizeof(*new));
316 struct sainfoalg
*alg
;
318 struct sainfoalg
*a
, *next
;
320 for (a
= alg
; a
; a
= next
) {
327 inssainfoalg(head
, new)
328 struct sainfoalg
**head
;
329 struct sainfoalg
*new;
333 for (a
= *head
; a
&& a
->next
; a
= a
->next
)
343 const struct sainfo
*si
;
348 static char buf
[256];
350 if (si
->idsrc
== NULL
)
351 snprintf(buf
, sizeof(buf
), "anonymous");
353 idsrc_str
= ipsecdoi_id2str(si
->idsrc
);
355 snprintf(buf
, sizeof(buf
), "%s", idsrc_str
);
356 racoon_free(idsrc_str
);
358 iddst_str
= ipsecdoi_id2str(si
->iddst
);
360 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
362 racoon_free(iddst_str
);
366 if (si
->id_i
!= NULL
) {
367 idi_str
= ipsecdoi_id2str(si
->id_i
);
369 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
370 " from %s", idi_str
);
371 racoon_free(idi_str
);