1 /* $NetBSD: sainfo.c,v 1.10 2007/09/12 23:39:51 mgrooms Exp $ */
3 /* $KAME: sainfo.c,v 1.16 2003/06/27 07:32:39 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
41 #include <netinet/in.h>
42 #include <netinet/in.h>
57 #include "localconf.h"
58 #include "isakmp_var.h"
60 #include "ipsec_doi.h"
63 #include "algorithm.h"
67 static LIST_HEAD(_sitree
, sainfo
) sitree
, sitree_save
, sitree_tmp
;
70 * modules for ipsec sa info
73 * return matching entry.
74 * no matching entry found and if there is anonymous entry, return it.
76 * First pass is for sainfo from a specified peer, second for others.
79 getsainfo(loc
, rmt
, peer
, client
, remoteid
)
80 const vchar_t
*loc
, *rmt
, *peer
, *client
;
83 struct sainfo
*s
= NULL
;
85 /* debug level output */
86 if(loglevel
>= LLV_DEBUG
) {
87 char *dloc
, *drmt
, *dpeer
, *dclient
;
90 dloc
= strdup("ANONYMOUS");
92 dloc
= ipsecdoi_id2str(loc
);
94 if (rmt
== SAINFO_ANONYMOUS
)
95 drmt
= strdup("ANONYMOUS");
96 else if (rmt
== SAINFO_CLIENTADDR
)
97 drmt
= strdup("CLIENTADDR");
99 drmt
= ipsecdoi_id2str(rmt
);
102 dpeer
= strdup("NULL");
104 dpeer
= ipsecdoi_id2str(peer
);
107 dclient
= strdup("NULL");
109 dclient
= ipsecdoi_id2str(client
);
111 plog(LLV_DEBUG
, LOCATION
, NULL
,
112 "getsainfo params: loc=\'%s\' rmt=\'%s\' peer=\'%s\' client=\'%s\' id=%i\n",
113 dloc
, drmt
, dpeer
, dclient
, remoteid
);
120 LIST_FOREACH(s
, &sitree
, chain
) {
121 const char *sainfostr
= sainfo2str(s
);
122 plog(LLV_DEBUG
, LOCATION
, NULL
,
123 "evaluating sainfo: %s\n", sainfostr
);
125 if(s
->remoteid
!= remoteid
) {
126 plog(LLV_DEBUG
, LOCATION
, NULL
,
127 "remoteid mismatch: %i != %i\n",
128 s
->remoteid
, remoteid
);
132 /* compare 'from' id value */
134 if (ipsecdoi_chkcmpids(peer
, s
->id_i
, 0))
137 /* compare ids - client */
138 if( s
->iddst
== SAINFO_CLIENTADDR
) {
140 * This sainfo section enforces client address
141 * checking. Prevent match if the client value
142 * ( modecfg or tunnel address ) is NULL.
148 if( rmt
== SAINFO_CLIENTADDR
) {
150 * In the case where a supplied rmt value is
151 * also SAINFO_CLIENTADDR, we are comparing
152 * with another sainfo to check for duplicate.
153 * Only compare the local values to determine
157 if (!ipsecdoi_chkcmpids(loc
, s
->idsrc
, 0))
162 * In the case where a supplied rmt value is
163 * not SAINFO_CLIENTADDR, do a standard match
164 * for local values and enforce that the rmt
165 * id matches the client address value.
168 if (!ipsecdoi_chkcmpids(loc
, s
->idsrc
, 0) &&
169 !ipsecdoi_chkcmpids(rmt
, client
, 0))
177 /* compare ids - standard */
178 if (!ipsecdoi_chkcmpids(loc
, s
->idsrc
, 0) &&
179 !ipsecdoi_chkcmpids(rmt
, s
->iddst
, 0))
191 new = racoon_calloc(1, sizeof(*new));
195 new->lifetime
= IPSECDOI_ATTR_SA_LD_SEC_DEFAULT
;
196 new->lifebyte
= IPSECDOI_ATTR_SA_LD_KB_MAX
;
207 for (i
= 0; i
< MAXALGCLASS
; i
++)
208 delsainfoalg(si
->algs
[i
]);
212 if (si
->iddst
!= NULL
&&
213 si
->iddst
!= SAINFO_CLIENTADDR
)
228 * determine the matching priority
229 * of an sainfo section
253 if(LIST_EMPTY(&sitree
)) {
256 LIST_INSERT_HEAD(&sitree
, new, chain
);
260 struct sainfo
*s
, *n
;
263 * insert our new sainfo section
264 * into our list which is sorted
265 * based on the match priority
268 npri
= prisainfo(new);
270 s
= LIST_FIRST(&sitree
);
274 n
= LIST_NEXT(s
, chain
);
278 /* higher priority */
279 LIST_INSERT_BEFORE(s
, new, chain
);
286 LIST_INSERT_AFTER(s
, new, chain
);
299 LIST_REMOVE(si
, chain
);
305 struct sainfo
*s
, *next
;
307 for (s
= LIST_FIRST(&sitree
); s
; s
= next
) {
308 next
= LIST_NEXT(s
, chain
);
323 struct sainfoalg
*new;
325 new = racoon_calloc(1, sizeof(*new));
334 struct sainfoalg
*alg
;
336 struct sainfoalg
*a
, *next
;
338 for (a
= alg
; a
; a
= next
) {
345 inssainfoalg(head
, new)
346 struct sainfoalg
**head
;
347 struct sainfoalg
*new;
351 for (a
= *head
; a
&& a
->next
; a
= a
->next
)
361 const struct sainfo
*si
;
363 static char buf
[256];
365 char *idloc
= NULL
, *idrmt
= NULL
, *id_i
;
367 if (si
->idsrc
== SAINFO_ANONYMOUS
)
368 idloc
= strdup("ANONYMOUS");
370 idloc
= ipsecdoi_id2str(si
->idsrc
);
372 if (si
->iddst
== SAINFO_ANONYMOUS
)
373 idrmt
= strdup("ANONYMOUS");
374 else if (si
->iddst
== SAINFO_CLIENTADDR
)
375 idrmt
= strdup("CLIENTADDR");
377 idrmt
= ipsecdoi_id2str(si
->iddst
);
379 if (si
->id_i
== NULL
)
380 id_i
= strdup("ANY");
382 id_i
= ipsecdoi_id2str(si
->id_i
);
384 snprintf(buf
, 255, "loc=\'%s\', rmt=\'%s\', peer=\'%s\', id=%i",
385 idloc
, idrmt
, id_i
, si
->remoteid
);
394 void save_sainfotree(void){
399 void save_sainfotree_flush(void){
406 void save_sainfotree_restore(void){