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(const vchar_t
*src
, const vchar_t
*dst
, const vchar_t
*peer
, int use_nat_addr
)
84 struct sainfo
*s
= NULL
;
85 struct sainfo
*anonymous
= NULL
;
88 if (use_nat_addr
&& lcconf
->ext_nat_id
== NULL
)
94 LIST_FOREACH(s
, &sitree
, chain
) {
95 if (s
->id_i
!= NULL
) {
98 if (memcmp(peer
->v
, s
->id_i
->v
, s
->id_i
->l
) != 0)
100 } else if (pass
== 1)
102 if (s
->idsrc
== NULL
) {
109 if (anonymous
!= NULL
)
114 // TODO: handle wildcard port numbers in the id
115 if ((src
->l
== s
->idsrc
->l
) && memcmp(src
->v
, s
->idsrc
->v
, s
->idsrc
->l
) == 0) {
117 if (memcmp(lcconf
->ext_nat_id
->v
, s
->iddst
->v
, s
->iddst
->l
) == 0) {
118 plogdump(ASL_LEVEL_DEBUG
, lcconf
->ext_nat_id
->v
, lcconf
->ext_nat_id
->l
, "matched external nat address.\n");
121 } else if ((dst
->l
== s
->iddst
->l
) && memcmp(dst
->v
, s
->iddst
->v
, s
->iddst
->l
) == 0) {
128 plog(ASL_LEVEL_DEBUG
,
129 "anonymous sainfo selected.\n");
130 } else if (pass
== 1) {
139 * return matching entry.
140 * no matching entry found and if there is anonymous entry, return it.
142 * XXX by each data type, should be changed to compare the buffer.
145 getsainfo_by_dst_id(const vchar_t
*dst
, const vchar_t
*peer
)
147 struct sainfo
*s
= NULL
;
148 struct sainfo
*anonymous
= NULL
;
150 plog(ASL_LEVEL_DEBUG
, "getsainfo_by_dst_id - dst id:\n");
152 plogdump(ASL_LEVEL_DEBUG
, dst
->v
, dst
->l
, "getsainfo_by_dst_id - dst id:\n");
156 LIST_FOREACH(s
, &sitree
, chain
) {
157 if (s
->idsrc
!= NULL
) {
158 plogdump(ASL_LEVEL_DEBUG
, s
->idsrc
->v
, s
->idsrc
->l
, "getsainfo_by_dst_id - sainfo id - src:\n");
159 plogdump(ASL_LEVEL_DEBUG
, s
->iddst
->v
, s
->iddst
->l
, "getsainfo_by_dst_id - sainfo id - dst:\n");
161 plog(ASL_LEVEL_DEBUG
, "getsainfo_by_dst_id - sainfo id = anonymous\n");
163 if (s
->id_i
!= NULL
) {
164 plogdump(ASL_LEVEL_DEBUG
, s
->id_i
->v
, s
->id_i
->l
, "getsainfo_by_dst_id - sainfo id_i:\n");
167 if (memcmp(peer
->v
, s
->id_i
->v
, s
->id_i
->l
) != 0)
170 if (s
->idsrc
== NULL
) {
175 if (memcmp(dst
->v
, s
->iddst
->v
, s
->iddst
->l
) == 0)
180 plog(ASL_LEVEL_DEBUG
,
181 "anonymous sainfo selected.\n");
193 new = racoon_calloc(1, sizeof(*new));
197 new->lifetime
= IPSECDOI_ATTR_SA_LD_SEC_DEFAULT
;
198 new->lifebyte
= IPSECDOI_ATTR_SA_LD_KB_MAX
;
207 delsainfo(struct sainfo
*si
)
211 for (i
= 0; i
< MAXALGCLASS
; i
++)
212 delsainfoalg(si
->algs
[i
]);
228 inssainfo(struct sainfo
*new)
230 LIST_INSERT_HEAD(&sitree
, new, chain
);
235 remsainfo(struct sainfo
*si
)
238 LIST_REMOVE(si
, chain
);
243 // remove sainfos from linked list
244 // if not used - delete it
248 struct sainfo
*s
, *next
;
250 LIST_FOREACH_SAFE(s
, &sitree
, chain
, next
) {
251 if (s
->dynamic
== 0) {
253 if (--(s
->refcount
) <= 0)
259 // remove sainfos from linked list
260 // if not used - delete it
262 flushsainfo_dynamic(u_int32_t addr
)
264 struct sainfo
*s
, *next
;
266 LIST_FOREACH_SAFE(s
, &sitree
, chain
, next
) {
267 if (s
->dynamic
== addr
) {
269 if (--(s
->refcount
) <= 0)
276 retain_sainfo(struct sainfo
*si
)
282 release_sainfo(struct sainfo
*si
)
284 if (--(si
->refcount
) <= 0) {
299 struct sainfoalg
*new;
301 new = racoon_calloc(1, sizeof(*new));
309 delsainfoalg(struct sainfoalg
*alg
)
311 struct sainfoalg
*a
, *next
;
313 for (a
= alg
; a
; a
= next
) {
320 inssainfoalg(struct sainfoalg
**head
, struct sainfoalg
*new)
324 for (a
= *head
; a
&& a
->next
; a
= a
->next
)
335 sainfo2str(const struct sainfo
*si
)
340 static char buf
[256];
342 if (si
->idsrc
== NULL
)
343 snprintf(buf
, sizeof(buf
), "anonymous");
345 idsrc_str
= ipsecdoi_id2str(si
->idsrc
);
347 snprintf(buf
, sizeof(buf
), "%s", idsrc_str
);
348 racoon_free(idsrc_str
);
350 if (si
->iddst
== NULL
) {
351 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
), " anonymous");
353 iddst_str
= ipsecdoi_id2str(si
->iddst
);
355 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
357 racoon_free(iddst_str
);
362 if (si
->id_i
!= NULL
) {
363 idi_str
= ipsecdoi_id2str(si
->id_i
);
365 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
366 " from %s", idi_str
);
367 racoon_free(idi_str
);