]> git.saurik.com Git - apple/network_cmds.git/blame - racoon.tproj/sainfo.c
network_cmds-115.2.tar.gz
[apple/network_cmds.git] / racoon.tproj / sainfo.c
CommitLineData
7ba0088d
A
1/* $KAME: sainfo.c,v 1.14 2001/04/03 15:51:56 thorpej 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 <sys/param.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <sys/queue.h>
36
37#include <netkey/key_var.h>
38#include <netinet/in.h>
39#include <netinet6/ipsec.h>
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <string.h>
44#include <errno.h>
45
46#include "var.h"
47#include "misc.h"
48#include "vmbuf.h"
49#include "plog.h"
50#include "sockmisc.h"
51#include "debug.h"
52
53#include "localconf.h"
54#include "isakmp_var.h"
55#include "isakmp.h"
56#include "ipsec_doi.h"
57#include "oakley.h"
58#include "handler.h"
59#include "algorithm.h"
60#include "sainfo.h"
61#include "gcmalloc.h"
62
63static LIST_HEAD(_sitree, sainfo) sitree;
64
65/* %%%
66 * modules for ipsec sa info
67 */
68/*
69 * return matching entry.
70 * no matching entry found and if there is anonymous entry, return it.
71 * else return NULL.
72 * XXX by each data type, should be changed to compare the buffer.
73 */
74struct sainfo *
75getsainfo(src, dst)
76 const vchar_t *src, *dst;
77{
78 struct sainfo *s = NULL;
79 struct sainfo *anonymous = NULL;
80
81 LIST_FOREACH(s, &sitree, chain) {
82 if (s->idsrc == NULL) {
83 anonymous = s;
84 continue;
85 }
86
87 /* anonymous ? */
88 if (src == NULL) {
89 if (anonymous != NULL)
90 break;
91 continue;
92 }
93
94 if (memcmp(src->v, s->idsrc->v, s->idsrc->l) == 0
95 && memcmp(dst->v, s->iddst->v, s->iddst->l) == 0)
96 return s;
97 }
98
99 if (anonymous) {
100 plog(LLV_DEBUG, LOCATION, NULL,
101 "anonymous sainfo selected.\n");
102 }
103 return anonymous;
104}
105
106struct sainfo *
107newsainfo()
108{
109 struct sainfo *new;
110
111 new = racoon_calloc(1, sizeof(*new));
112 if (new == NULL)
113 return NULL;
114
115 new->idvtype = IDTYPE_ADDRESS;
116 new->lifetime = IPSECDOI_ATTR_SA_LD_SEC_DEFAULT;
117 new->lifebyte = IPSECDOI_ATTR_SA_LD_KB_MAX;
118
119 return new;
120}
121
122void
123delsainfo(si)
124 struct sainfo *si;
125{
126 int i;
127
128 for (i = 0; i < MAXALGCLASS; i++)
129 delsainfoalg(si->algs[i]);
130
131 if (si->idsrc)
132 vfree(si->idsrc);
133 if (si->iddst)
134 vfree(si->iddst);
135
136 racoon_free(si);
137}
138
139void
140inssainfo(new)
141 struct sainfo *new;
142{
143 LIST_INSERT_HEAD(&sitree, new, chain);
144}
145
146void
147remsainfo(si)
148 struct sainfo *si;
149{
150 LIST_REMOVE(si, chain);
151}
152
153void
154flushsainfo()
155{
156 struct sainfo *s, *next;
157
158 for (s = LIST_FIRST(&sitree); s; s = next) {
159 next = LIST_NEXT(s, chain);
160 remsainfo(s);
161 delsainfo(s);
162 }
163}
164
165void
166initsainfo()
167{
168 LIST_INIT(&sitree);
169}
170
171struct sainfoalg *
172newsainfoalg()
173{
174 struct sainfoalg *new;
175
176 new = racoon_calloc(1, sizeof(*new));
177 if (new == NULL)
178 return NULL;
179
180 return new;
181}
182
183void
184delsainfoalg(alg)
185 struct sainfoalg *alg;
186{
187 struct sainfoalg *a, *next;
188
189 for (a = alg; a; a = next) {
190 next = a->next;
191 racoon_free(a);
192 }
193}
194
195void
196inssainfoalg(head, new)
197 struct sainfoalg **head;
198 struct sainfoalg *new;
199{
200 struct sainfoalg *a;
201
202 for (a = *head; a && a->next; a = a->next)
203 ;
204 if (a)
205 a->next = new;
206 else
207 *head = new;
208}
209
210const char *
211sainfo2str(si)
212 const struct sainfo *si;
213{
214 static char buf[256];
215
216 if (si->idsrc == NULL)
217 return "anonymous";
218
219 snprintf(buf, sizeof(buf), "%s", ipsecdoi_id2str(si->idsrc));
220 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
221 " %s", ipsecdoi_id2str(si->iddst));
222
223 return buf;
224}