]> git.saurik.com Git - apple/ipsec.git/blame - ipsec-tools/racoon/policy.c
ipsec-146.3.tar.gz
[apple/ipsec.git] / ipsec-tools / racoon / policy.c
CommitLineData
52b7d2ce
A
1/* $KAME: policy.c,v 1.46 2001/11/16 04:08:10 sakane 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 "config.h"
33
34#include <sys/param.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <sys/queue.h>
38
39#include <netinet/in.h>
40#ifdef HAVE_NETINET6_IPSEC
41# include <netinet6/ipsec.h>
42#else
43# include <netinet/ipsec.h>
44#endif
45
46#include <stdlib.h>
47#include <stdio.h>
48#include <string.h>
49#include <errno.h>
50
51#include "var.h"
52#include "misc.h"
53#include "vmbuf.h"
54#include "plog.h"
55#include "sockmisc.h"
56#include "debug.h"
57
58#include "policy.h"
59#include "localconf.h"
60#include "isakmp_var.h"
61#include "isakmp.h"
62#include "oakley.h"
63#include "handler.h"
64#include "strnames.h"
65#include "gcmalloc.h"
66#include "session.h"
67
68static TAILQ_HEAD(_sptree, secpolicy) sptree;
69
70/* perform exact match against security policy table. */
71struct secpolicy *
72getsp(spidx)
73 struct policyindex *spidx;
74{
75 struct secpolicy *p;
76
77 for (p = TAILQ_FIRST(&sptree); p; p = TAILQ_NEXT(p, chain)) {
78 if (!cmpspidxstrict(spidx, &p->spidx))
79 return p;
80 }
81
82 return NULL;
83}
84
85/*
86 * perform non-exact match against security policy table, only if this is
87 * transport mode SA negotiation. for example, 0.0.0.0/0 -> 0.0.0.0/0
88 * entry in policy.txt can be returned when we're negotiating transport
89 * mode SA. this is how the kernel works.
90 */
91#if 1
92struct secpolicy *
e97d2cf9 93getsp_r(spidx, iph2)
52b7d2ce 94 struct policyindex *spidx;
e97d2cf9 95 struct ph2handle *iph2;
52b7d2ce
A
96{
97 struct secpolicy *p;
e97d2cf9 98 int mismatched_outer_addr = 0;
52b7d2ce
A
99
100 for (p = TAILQ_FIRST(&sptree); p; p = TAILQ_NEXT(p, chain)) {
e97d2cf9
A
101 if (!cmpspidxwild(spidx, &p->spidx)) {
102 if (spidx->dir != IPSEC_DIR_ANY) {
103 struct ipsecrequest *isr;
104 for (isr = p->req; isr != NULL; isr = isr->next) {
105 if (isr->saidx.mode != IPSEC_MODE_TUNNEL) {
106 plog(LLV_DEBUG2, LOCATION, NULL, "%s, skipping policy. dir %d, mode %d\n",
107 __FUNCTION__, spidx->dir, isr->saidx.mode);
108 continue;
109 }
110
111 // for tunnel mode: verify the outer ip addresses match the phase2's addresses
112 if (spidx->dir == IPSEC_DIR_INBOUND) {
113 // TODO: look out for wildcards
114 if (!cmpsaddrwop(iph2->dst, (struct sockaddr *)&isr->saidx.src) &&
115 !cmpsaddrwop(iph2->src, (struct sockaddr *)&isr->saidx.dst)) {
116 plog(LLV_DEBUG2, LOCATION, NULL, "%s, inbound policy outer addresses matched phase2's addresses\n",
117 __FUNCTION__);
118 return p;
119 } else {
120 mismatched_outer_addr = 1;
121 }
122 } else if (spidx->dir == IPSEC_DIR_OUTBOUND) {
123 // TODO: look out for wildcards
124 if (!cmpsaddrwop(iph2->src, (struct sockaddr *)&isr->saidx.src) &&
125 !cmpsaddrwop(iph2->dst, (struct sockaddr *)&isr->saidx.dst)) {
126 plog(LLV_DEBUG2, LOCATION, NULL, "%s, outbound policy outer addresses matched phase2's addresses\n",
127 __FUNCTION__);
128 return p;
129 } else {
130 mismatched_outer_addr = 1;
131 }
132 } else {
133 mismatched_outer_addr = 1;
134 }
135 if (mismatched_outer_addr) {
136 plog(LLV_DEBUG2, LOCATION, NULL, "%s, policy outer addresses matched phase2's addresses: dir %d\n",
137 __FUNCTION__, spidx->dir);
138 plog(LLV_DEBUG, LOCATION, NULL, "src1: %s\n",
139 saddr2str(iph2->src));
140 plog(LLV_DEBUG, LOCATION, NULL, "src2: %s\n",
141 saddr2str((struct sockaddr *)&isr->saidx.src));
142 plog(LLV_DEBUG, LOCATION, NULL, "dst1: %s\n",
143 saddr2str(iph2->dst));
144 plog(LLV_DEBUG, LOCATION, NULL, "dst2: %s\n",
145 saddr2str((struct sockaddr *)&isr->saidx.dst));
146 }
147 }
148 }
149 if (!mismatched_outer_addr) {
150 return p;
151 }
152 }
52b7d2ce
A
153 }
154
155 return NULL;
156}
157#else
158struct secpolicy *
159getsp_r(spidx, iph2)
160 struct policyindex *spidx;
161 struct ph2handle *iph2;
162{
163 struct secpolicy *p;
164 u_int8_t prefixlen;
165
166 plog(LLV_DEBUG, LOCATION, NULL, "checking for transport mode\n");
167
168 if (spidx->src.ss_family != spidx->dst.ss_family) {
169 plog(LLV_ERROR, LOCATION, NULL,
170 "address family mismatch, src:%d dst:%d\n",
171 spidx->src.ss_family,
172 spidx->dst.ss_family);
173 return NULL;
174 }
175 switch (spidx->src.ss_family) {
176 case AF_INET:
177 prefixlen = sizeof(struct in_addr) << 3;
178 break;
179#ifdef INET6
180 case AF_INET6:
181 prefixlen = sizeof(struct in6_addr) << 3;
182 break;
183#endif
184 default:
185 plog(LLV_ERROR, LOCATION, NULL,
186 "invalid family: %d\n", spidx->src.ss_family);
187 return NULL;
188 }
189
190 /* is it transport mode SA negotiation? */
191 plog(LLV_DEBUG, LOCATION, NULL, "src1: %s\n",
192 saddr2str(iph2->src));
193 plog(LLV_DEBUG, LOCATION, NULL, "src2: %s\n",
194 saddr2str((struct sockaddr *)&spidx->src));
195 if (cmpsaddrwop(iph2->src, (struct sockaddr *)&spidx->src)
196 || spidx->prefs != prefixlen)
197 return NULL;
198
199 plog(LLV_DEBUG, LOCATION, NULL, "dst1: %s\n",
200 saddr2str(iph2->dst));
201 plog(LLV_DEBUG, LOCATION, NULL, "dst2: %s\n",
202 saddr2str((struct sockaddr *)&spidx->dst));
203 if (cmpsaddrwop(iph2->dst, (struct sockaddr *)&spidx->dst)
204 || spidx->prefd != prefixlen)
205 return NULL;
206
207 plog(LLV_DEBUG, LOCATION, NULL, "looks to be transport mode\n");
208
209 for (p = TAILQ_FIRST(&sptree); p; p = TAILQ_NEXT(p, chain)) {
210 if (!cmpspidx_wild(spidx, &p->spidx))
211 return p;
212 }
213
214 return NULL;
215}
216#endif
217
218struct secpolicy *
219getspbyspid(spid)
220 u_int32_t spid;
221{
222 struct secpolicy *p;
223
224 for (p = TAILQ_FIRST(&sptree); p; p = TAILQ_NEXT(p, chain)) {
225 if (p->id == spid)
226 return p;
227 }
228
229 return NULL;
230}
231
232/*
233 * compare policyindex.
234 * a: subject b: db
235 * OUT: 0: equal
236 * 1: not equal
237 */
238int
239cmpspidxstrict(a, b)
240 struct policyindex *a, *b;
241{
242 plog(LLV_DEBUG, LOCATION, NULL, "sub:%p: %s\n", a, spidx2str(a));
243 plog(LLV_DEBUG, LOCATION, NULL, "db :%p: %s\n", b, spidx2str(b));
244
245 /* XXX don't check direction now, but it's to be checked carefully. */
246 if (a->dir != b->dir
247 || a->prefs != b->prefs
248 || a->prefd != b->prefd
249 || a->ul_proto != b->ul_proto)
250 return 1;
251
252 if (cmpsaddrstrict((struct sockaddr *)&a->src,
253 (struct sockaddr *)&b->src))
254 return 1;
255 if (cmpsaddrstrict((struct sockaddr *)&a->dst,
256 (struct sockaddr *)&b->dst))
257 return 1;
258
259 return 0;
260}
261
262/*
263 * compare policyindex, with wildcard address/protocol match.
264 * a: subject b: db, can contain wildcard things.
265 * OUT: 0: equal
266 * 1: not equal
267 */
268int
269cmpspidxwild(a, b)
270 struct policyindex *a, *b;
271{
272 struct sockaddr_storage sa1, sa2;
273
274 plog(LLV_DEBUG, LOCATION, NULL, "sub:%p: %s\n", a, spidx2str(a));
275 plog(LLV_DEBUG, LOCATION, NULL, "db: %p: %s\n", b, spidx2str(b));
276
277 if (!(b->dir == IPSEC_DIR_ANY || a->dir == b->dir))
278 return 1;
279
280 if (!(a->ul_proto == IPSEC_ULPROTO_ANY ||
281 b->ul_proto == IPSEC_ULPROTO_ANY ||
282 a->ul_proto == b->ul_proto))
283 return 1;
284
285 if (a->src.ss_family != b->src.ss_family)
286 return 1;
287 if (a->dst.ss_family != b->dst.ss_family)
288 return 1;
289
52b7d2ce
A
290 /* compare src address */
291 if (sizeof(sa1) < a->src.ss_len || sizeof(sa2) < b->src.ss_len) {
292 plog(LLV_ERROR, LOCATION, NULL,
293 "unexpected error: "
294 "src.ss_len:%d dst.ss_len:%d\n",
295 a->src.ss_len, b->src.ss_len);
296 return 1;
297 }
52b7d2ce
A
298 mask_sockaddr((struct sockaddr *)&sa1, (struct sockaddr *)&a->src,
299 b->prefs);
300 mask_sockaddr((struct sockaddr *)&sa2, (struct sockaddr *)&b->src,
301 b->prefs);
302 plog(LLV_DEBUG, LOCATION, NULL, "%p masked with /%d: %s\n",
303 a, b->prefs, saddr2str((struct sockaddr *)&sa1));
304 plog(LLV_DEBUG, LOCATION, NULL, "%p masked with /%d: %s\n",
305 b, b->prefs, saddr2str((struct sockaddr *)&sa2));
306 if (cmpsaddrwild((struct sockaddr *)&sa1, (struct sockaddr *)&sa2))
307 return 1;
308
52b7d2ce
A
309 /* compare dst address */
310 if (sizeof(sa1) < a->dst.ss_len || sizeof(sa2) < b->dst.ss_len) {
311 plog(LLV_ERROR, LOCATION, NULL, "unexpected error\n");
312 exit(1);
313 }
52b7d2ce
A
314 mask_sockaddr((struct sockaddr *)&sa1, (struct sockaddr *)&a->dst,
315 b->prefd);
316 mask_sockaddr((struct sockaddr *)&sa2, (struct sockaddr *)&b->dst,
317 b->prefd);
318 plog(LLV_DEBUG, LOCATION, NULL, "%p masked with /%d: %s\n",
319 a, b->prefd, saddr2str((struct sockaddr *)&sa1));
320 plog(LLV_DEBUG, LOCATION, NULL, "%p masked with /%d: %s\n",
321 b, b->prefd, saddr2str((struct sockaddr *)&sa2));
322 if (cmpsaddrwild((struct sockaddr *)&sa1, (struct sockaddr *)&sa2))
323 return 1;
324
325 return 0;
326}
327
328struct secpolicy *
329newsp()
330{
331 struct secpolicy *new;
332
333 new = racoon_calloc(1, sizeof(*new));
334 if (new == NULL)
335 return NULL;
336
337 return new;
338}
339
340void
341delsp(sp)
342 struct secpolicy *sp;
343{
344 struct ipsecrequest *req = NULL, *next;
345
346 for (req = sp->req; req; req = next) {
347 next = req->next;
348 racoon_free(req);
349 }
350
351 racoon_free(sp);
352}
353
354void
355delsp_bothdir(spidx0)
356 struct policyindex *spidx0;
357{
358 struct policyindex spidx;
359 struct secpolicy *sp;
360 struct sockaddr_storage src, dst;
361 u_int8_t prefs, prefd;
362
363 memcpy(&spidx, spidx0, sizeof(spidx));
364 switch (spidx.dir) {
365 case IPSEC_DIR_INBOUND:
366#ifdef HAVE_POLICY_FWD
367 case IPSEC_DIR_FWD:
368#endif
369 src = spidx.src;
370 dst = spidx.dst;
371 prefs = spidx.prefs;
372 prefd = spidx.prefd;
373 break;
374 case IPSEC_DIR_OUTBOUND:
375 src = spidx.dst;
376 dst = spidx.src;
377 prefs = spidx.prefd;
378 prefd = spidx.prefs;
379 break;
380 default:
381 return;
382 }
383
384 spidx.src = src;
385 spidx.dst = dst;
386 spidx.prefs = prefs;
387 spidx.prefd = prefd;
388 spidx.dir = IPSEC_DIR_INBOUND;
389
390 sp = getsp(&spidx);
391 if (sp) {
392 remsp(sp);
393 delsp(sp);
394 }
395
396#ifdef HAVE_POLICY_FWD
397 spidx.dir = IPSEC_DIR_FWD;
398
399 sp = getsp(&spidx);
400 if (sp) {
401 remsp(sp);
402 delsp(sp);
403 }
404#endif
405
406 spidx.src = dst;
407 spidx.dst = src;
408 spidx.prefs = prefd;
409 spidx.prefd = prefs;
410 spidx.dir = IPSEC_DIR_OUTBOUND;
411
412 sp = getsp(&spidx);
413 if (sp) {
414 remsp(sp);
415 delsp(sp);
416 }
417}
418
419void
420inssp(new)
421 struct secpolicy *new;
422{
423#ifdef HAVE_PFKEY_POLICY_PRIORITY
424 struct secpolicy *p;
425
426 TAILQ_FOREACH(p, &sptree, chain) {
427 if (new->spidx.priority < p->spidx.priority) {
428 TAILQ_INSERT_BEFORE(p, new, chain);
429 return;
430 }
431 }
432 if (p == NULL)
433#endif
d1e348cf 434 TAILQ_INSERT_HEAD(&sptree, new, chain);
52b7d2ce
A
435
436 check_auto_exit();
437 return;
438}
439
440void
441remsp(sp)
442 struct secpolicy *sp;
443{
444 TAILQ_REMOVE(&sptree, sp, chain);
445 check_auto_exit();
446}
447
448void
449flushsp()
450{
451 struct secpolicy *p, *next;
452
453 for (p = TAILQ_FIRST(&sptree); p; p = next) {
454 next = TAILQ_NEXT(p, chain);
455 remsp(p);
456 delsp(p);
457 }
458}
459
460int
461policies_installed(void)
462{
463 if (TAILQ_EMPTY(&sptree))
464 return 0;
465 else
466 return 1;
467}
468
469void
470initsp()
471{
472 TAILQ_INIT(&sptree);
473}
474
475struct ipsecrequest *
476newipsecreq()
477{
478 struct ipsecrequest *new;
479
480 new = racoon_calloc(1, sizeof(*new));
481 if (new == NULL)
482 return NULL;
483
484 return new;
485}
486
487const char *
488spidx2str(spidx)
489 const struct policyindex *spidx;
490{
491 /* addr/pref[port] addr/pref[port] ul dir act */
492 static char buf[256];
493 char *p, *a, *b;
494 int blen, i;
495
496 blen = sizeof(buf) - 1;
497 p = buf;
498
499 a = saddr2str((const struct sockaddr *)&spidx->src);
500 for (b = a; *b != '\0'; b++)
501 if (*b == '[') {
502 *b = '\0';
503 b++;
504 break;
505 }
506 i = snprintf(p, blen, "%s/%d[%s ", a, spidx->prefs, b);
507 if (i < 0 || i >= blen)
508 return NULL;
509 p += i;
510 blen -= i;
511
512 a = saddr2str((const struct sockaddr *)&spidx->dst);
513 for (b = a; *b != '\0'; b++)
514 if (*b == '[') {
515 *b = '\0';
516 b++;
517 break;
518 }
519 i = snprintf(p, blen, "%s/%d[%s ", a, spidx->prefd, b);
520 if (i < 0 || i >= blen)
521 return NULL;
522 p += i;
523 blen -= i;
524
525 snprintf(p, blen, "proto=%s dir=%s",
526 s_proto(spidx->ul_proto), s_direction(spidx->dir));
527
528 return buf;
529}