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
) {
107 if (s
->to_delete
|| s
->to_remove
) {
110 if (s
->idsrc
!= NULL
) {
111 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo - sainfo id - src & dst:\n");
112 plogdump(LLV_DEBUG2
, s
->idsrc
->v
, s
->idsrc
->l
);
113 plogdump(LLV_DEBUG2
, s
->iddst
->v
, s
->iddst
->l
);
115 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo - sainfo id = anonymous\n");
117 if (s
->id_i
!= NULL
) {
120 if (memcmp(peer
->v
, s
->id_i
->v
, s
->id_i
->l
) != 0)
122 } else if (pass
== 1)
124 if (s
->idsrc
== NULL
) {
131 if (anonymous
!= NULL
)
136 if (memcmp(src
->v
, s
->idsrc
->v
, s
->idsrc
->l
) == 0) {
138 if (memcmp(lcconf
->ext_nat_id
->v
, s
->iddst
->v
, s
->iddst
->l
) == 0) {
139 plog(LLV_DEBUG
, LOCATION
, NULL
,
140 "matched external nat address.\n");
141 plogdump(LLV_DEBUG2
, lcconf
->ext_nat_id
->v
, lcconf
->ext_nat_id
->l
);
144 } else if (memcmp(dst
->v
, s
->iddst
->v
, s
->iddst
->l
) == 0)
150 plog(LLV_DEBUG
, LOCATION
, NULL
,
151 "anonymous sainfo selected.\n");
152 } else if (pass
== 1) {
161 * return matching entry.
162 * no matching entry found and if there is anonymous entry, return it.
164 * XXX by each data type, should be changed to compare the buffer.
167 getsainfo_by_dst_id(dst
, peer
)
168 const vchar_t
*dst
, *peer
;
170 struct sainfo
*s
= NULL
;
171 struct sainfo
*anonymous
= NULL
;
173 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo_by_dst_id - dst id:\n");
175 plogdump(LLV_DEBUG2
, dst
->v
, dst
->l
);
179 LIST_FOREACH(s
, &sitree
, chain
) {
180 if (s
->to_delete
|| s
->to_remove
) {
183 if (s
->idsrc
!= NULL
) {
184 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo_by_dst_id - sainfo id - src & dst:\n");
185 plogdump(LLV_DEBUG2
, s
->idsrc
->v
, s
->idsrc
->l
);
186 plogdump(LLV_DEBUG2
, s
->iddst
->v
, s
->iddst
->l
);
188 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo_by_dst_id - sainfo id = anonymous\n");
190 if (s
->id_i
!= NULL
) {
191 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getsainfo_by_dst_id - sainfo id_i:\n");
192 plogdump(LLV_DEBUG2
, s
->id_i
->v
, s
->id_i
->l
);
195 if (memcmp(peer
->v
, s
->id_i
->v
, s
->id_i
->l
) != 0)
198 if (s
->idsrc
== NULL
) {
203 if (memcmp(dst
->v
, s
->iddst
->v
, s
->iddst
->l
) == 0)
208 plog(LLV_DEBUG
, LOCATION
, NULL
,
209 "anonymous sainfo selected.\n");
216 link_sainfo_to_ph2 (struct sainfo
*new)
221 if (new->to_delete
||
225 new->linked_to_ph2
++;
230 unlink_sainfo_from_ph2 (struct sainfo
*old
)
235 if (old
->linked_to_ph2
<= 0) {
238 old
->linked_to_ph2
--;
239 if (old
->linked_to_ph2
== 0) {
240 if (old
->to_remove
) {
243 if (old
->to_delete
) {
255 new = racoon_calloc(1, sizeof(*new));
259 new->lifetime
= IPSECDOI_ATTR_SA_LD_SEC_DEFAULT
;
260 new->lifebyte
= IPSECDOI_ATTR_SA_LD_KB_MAX
;
261 new->to_remove
= FALSE
;
262 new->to_delete
= FALSE
;
263 new->linked_to_ph2
= 0;
274 if (si
->linked_to_ph2
) {
275 si
->to_delete
= TRUE
;
279 for (i
= 0; i
< MAXALGCLASS
; i
++)
280 delsainfoalg(si
->algs
[i
]);
299 LIST_INSERT_HEAD(&sitree
, new, chain
);
306 if (si
->linked_to_ph2
) {
307 si
->to_remove
= TRUE
;
310 LIST_REMOVE(si
, chain
);
316 struct sainfo
*s
, *next
;
318 for (s
= LIST_FIRST(&sitree
); s
; s
= next
) {
319 next
= LIST_NEXT(s
, chain
);
320 if (s
->dynamic
== 0) {
328 flushsainfo_dynamic(u_int32_t addr
)
330 struct sainfo
*s
, *next
;
332 for (s
= LIST_FIRST(&sitree
); s
; s
= next
) {
333 next
= LIST_NEXT(s
, chain
);
334 if (s
->dynamic
== addr
) {
350 struct sainfoalg
*new;
352 new = racoon_calloc(1, sizeof(*new));
361 struct sainfoalg
*alg
;
363 struct sainfoalg
*a
, *next
;
365 for (a
= alg
; a
; a
= next
) {
372 inssainfoalg(head
, new)
373 struct sainfoalg
**head
;
374 struct sainfoalg
*new;
378 for (a
= *head
; a
&& a
->next
; a
= a
->next
)
388 const struct sainfo
*si
;
393 static char buf
[256];
395 if (si
->idsrc
== NULL
)
396 snprintf(buf
, sizeof(buf
), "anonymous");
398 idsrc_str
= ipsecdoi_id2str(si
->idsrc
);
400 snprintf(buf
, sizeof(buf
), "%s", idsrc_str
);
401 racoon_free(idsrc_str
);
403 iddst_str
= ipsecdoi_id2str(si
->iddst
);
405 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
407 racoon_free(iddst_str
);
411 if (si
->id_i
!= NULL
) {
412 idi_str
= ipsecdoi_id2str(si
->id_i
);
414 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
415 " from %s", idi_str
);
416 racoon_free(idi_str
);