1 /* $Id: proposal.c,v 1.13.8.5 2005/07/28 05:05:52 manubsd 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 #ifdef HAVE_NETINET6_IPSEC
41 # include <netinet6/ipsec.h>
43 # include <netinet/ipsec.h>
60 #include "isakmp_var.h"
62 #include "ipsec_doi.h"
63 #include "algorithm.h"
66 #include "localconf.h"
67 #include "remoteconf.h"
73 #include "nattraversal.h"
77 * modules for ipsec sa spec
84 new = racoon_calloc(1, sizeof(*new));
96 new = racoon_calloc(1, sizeof(*new));
103 /* set saprop to last part of the prop tree */
106 struct saprop
**head
;
116 for (p
= *head
; p
->next
; p
= p
->next
)
123 /* set saproto to the end of the proto tree in saprop */
131 for (p
= pp
->head
; p
&& p
->next
; p
= p
->next
)
141 /* set saproto to the top of the proto tree in saprop */
143 inssaprotorev(pp
, new)
147 new->next
= pp
->head
;
158 new = racoon_calloc(1, sizeof(*new));
165 /* set saproto to last part of the proto tree in saprop */
173 for (tr
= pr
->head
; tr
&& tr
->next
; tr
= tr
->next
)
184 * take a single match between saprop. allocate a new proposal and return it
185 * for future use (like picking single proposal from a bundle).
186 * pp1: peer's proposal.
188 * NOTE: In the case of initiator, must be ensured that there is no
189 * modification of the proposal by calling cmp_aproppair_i() before
191 * XXX cannot understand the comment!
194 cmpsaprop_alloc(ph1
, pp1
, pp2
, side
)
195 struct ph1handle
*ph1
;
196 const struct saprop
*pp1
, *pp2
;
199 struct saprop
*newpp
= NULL
;
200 struct saproto
*pr1
, *pr2
, *newpr
= NULL
;
201 struct satrns
*tr1
, *tr2
, *newtr
;
202 const int ordermatters
= 0;
208 plog(LLV_ERROR
, LOCATION
, NULL
,
209 "failed to allocate saprop.\n");
212 newpp
->prop_no
= pp1
->prop_no
;
214 /* see proposal.h about lifetime/key length and PFS selection. */
216 /* check time/bytes lifetime and PFS */
217 switch (ph1
->rmconf
->pcheck_level
) {
218 case PROP_CHECK_OBEY
:
219 newpp
->lifetime
= pp1
->lifetime
;
220 newpp
->lifebyte
= pp1
->lifebyte
;
221 newpp
->pfs_group
= pp1
->pfs_group
;
224 case PROP_CHECK_STRICT
:
225 if (pp1
->lifetime
> pp2
->lifetime
) {
226 plog(LLV_ERROR
, LOCATION
, NULL
,
227 "long lifetime proposed: "
229 (int)pp2
->lifetime
, (int)pp1
->lifetime
);
232 if (pp1
->lifebyte
> pp2
->lifebyte
) {
233 plog(LLV_ERROR
, LOCATION
, NULL
,
234 "long lifebyte proposed: "
236 pp2
->lifebyte
, pp1
->lifebyte
);
239 newpp
->lifetime
= pp1
->lifetime
;
240 newpp
->lifebyte
= pp1
->lifebyte
;
243 if (pp2
->pfs_group
!= 0 && pp1
->pfs_group
!= pp2
->pfs_group
) {
244 plog(LLV_ERROR
, LOCATION
, NULL
,
245 "pfs group mismatched: "
247 pp2
->pfs_group
, pp1
->pfs_group
);
250 newpp
->pfs_group
= pp1
->pfs_group
;
253 case PROP_CHECK_CLAIM
:
255 if (pp1
->lifetime
<= pp2
->lifetime
) {
256 newpp
->lifetime
= pp1
->lifetime
;
258 newpp
->lifetime
= pp2
->lifetime
;
259 newpp
->claim
|= IPSECDOI_ATTR_SA_LD_TYPE_SEC
;
260 plog(LLV_NOTIFY
, LOCATION
, NULL
,
263 (int)pp2
->lifetime
, (int)pp1
->lifetime
);
267 if (pp1
->lifebyte
> pp2
->lifebyte
) {
268 newpp
->lifebyte
= pp2
->lifebyte
;
269 newpp
->claim
|= IPSECDOI_ATTR_SA_LD_TYPE_SEC
;
270 plog(LLV_NOTIFY
, LOCATION
, NULL
,
273 pp2
->lifebyte
, pp1
->lifebyte
);
275 newpp
->lifebyte
= pp1
->lifebyte
;
280 case PROP_CHECK_EXACT
:
281 if (pp1
->lifetime
!= pp2
->lifetime
) {
282 plog(LLV_ERROR
, LOCATION
, NULL
,
283 "lifetime mismatched: "
285 (int)pp2
->lifetime
, (int)pp1
->lifetime
);
289 if (pp1
->lifebyte
!= pp2
->lifebyte
) {
290 plog(LLV_ERROR
, LOCATION
, NULL
,
291 "lifebyte mismatched: "
293 pp2
->lifebyte
, pp1
->lifebyte
);
296 if (pp1
->pfs_group
!= pp2
->pfs_group
) {
297 plog(LLV_ERROR
, LOCATION
, NULL
,
298 "pfs group mismatched: "
300 pp2
->pfs_group
, pp1
->pfs_group
);
303 newpp
->lifetime
= pp1
->lifetime
;
304 newpp
->lifebyte
= pp1
->lifebyte
;
305 newpp
->pfs_group
= pp1
->pfs_group
;
309 plog(LLV_ERROR
, LOCATION
, NULL
,
310 "invalid pcheck_level why?.\n");
315 for (pr1
= pp1
->head
; pr1
; pr1
= pr1
->next
)
317 for (pr2
= pp2
->head
; pr2
; pr2
= pr2
->next
)
322 /* check protocol order */
329 * XXX does not work if we have multiple proposals
330 * with the same proto_id
336 for (pr1
= pp1
->head
; pr1
; pr1
= pr1
->next
) {
337 if (pr1
->proto_id
== pr2
->proto_id
)
344 for (pr2
= pp2
->head
; pr2
; pr2
= pr2
->next
) {
345 if (pr2
->proto_id
== pr1
->proto_id
)
354 if (pr1
->proto_id
!= pr2
->proto_id
) {
355 plog(LLV_ERROR
, LOCATION
, NULL
,
356 "proto_id mismatched: "
358 s_ipsecdoi_proto(pr2
->proto_id
),
359 s_ipsecdoi_proto(pr1
->proto_id
));
363 if (pr1
->spisize
== pr2
->spisize
)
365 else if (pr1
->proto_id
== IPSECDOI_PROTO_IPCOMP
) {
367 * draft-shacham-ippcp-rfc2393bis-05.txt:
368 * need to accept 16bit and 32bit SPI (CPI) for IPComp.
370 if (pr1
->spisize
== sizeof(u_int16_t
) &&
371 pr2
->spisize
== sizeof(u_int32_t
)) {
373 } else if (pr2
->spisize
== sizeof(u_int16_t
) &&
374 pr1
->spisize
== sizeof(u_int32_t
)) {
378 plog(LLV_ERROR
, LOCATION
, NULL
,
379 "IPComp SPI size promoted "
380 "from 16bit to 32bit\n");
384 plog(LLV_ERROR
, LOCATION
, NULL
,
385 "spisize mismatched: "
387 (int)pr2
->spisize
, (int)pr1
->spisize
);
392 if ((ph1
->natt_flags
& NAT_DETECTED
) &&
393 natt_udp_encap (pr2
->encmode
))
395 plog(LLV_INFO
, LOCATION
, NULL
, "Adjusting my encmode %s->%s\n",
396 s_ipsecdoi_encmode(pr2
->encmode
),
397 s_ipsecdoi_encmode(pr2
->encmode
- ph1
->natt_options
->mode_udp_diff
));
398 pr2
->encmode
-= ph1
->natt_options
->mode_udp_diff
;
402 if ((ph1
->natt_flags
& NAT_DETECTED
) &&
403 natt_udp_encap (pr1
->encmode
))
405 plog(LLV_INFO
, LOCATION
, NULL
, "Adjusting peer's encmode %s(%d)->%s(%d)\n",
406 s_ipsecdoi_encmode(pr1
->encmode
),
408 s_ipsecdoi_encmode(pr1
->encmode
- ph1
->natt_options
->mode_udp_diff
),
409 pr1
->encmode
- ph1
->natt_options
->mode_udp_diff
);
410 pr1
->encmode
-= ph1
->natt_options
->mode_udp_diff
;
415 if (pr1
->encmode
!= pr2
->encmode
) {
416 plog(LLV_ERROR
, LOCATION
, NULL
,
417 "encmode mismatched: "
419 s_ipsecdoi_encmode(pr2
->encmode
),
420 s_ipsecdoi_encmode(pr1
->encmode
));
424 for (tr1
= pr1
->head
; tr1
; tr1
= tr1
->next
) {
425 for (tr2
= pr2
->head
; tr2
; tr2
= tr2
->next
) {
426 if (cmpsatrns(pr1
->proto_id
, tr1
, tr2
) == 0)
434 newpr
= newsaproto();
436 plog(LLV_ERROR
, LOCATION
, NULL
,
437 "failed to allocate saproto.\n");
440 newpr
->proto_id
= pr1
->proto_id
;
441 newpr
->spisize
= pr1
->spisize
;
442 newpr
->encmode
= pr1
->encmode
;
443 newpr
->spi
= pr2
->spi
; /* copy my SPI */
444 newpr
->spi_p
= pr1
->spi
; /* copy peer's SPI */
445 newpr
->reqid_in
= pr2
->reqid_in
;
446 newpr
->reqid_out
= pr2
->reqid_out
;
448 newpr
->udp_encap
= pr1
->udp_encap
| pr2
->udp_encap
;
453 plog(LLV_ERROR
, LOCATION
, NULL
,
454 "failed to allocate satrns.\n");
458 newtr
->trns_no
= tr1
->trns_no
;
459 newtr
->trns_id
= tr1
->trns_id
;
460 newtr
->encklen
= tr1
->encklen
;
461 newtr
->authtype
= tr1
->authtype
;
463 inssatrns(newpr
, newtr
);
464 inssaproto(newpp
, newpr
);
470 /* XXX should check if we have visited all items or not */
484 /* should be matched all protocols in a proposal */
485 if (pr1
!= NULL
|| pr2
!= NULL
)
495 /* take a single match between saprop. returns 0 if pp1 equals to pp2. */
498 const struct saprop
*pp1
, *pp2
;
500 if (pp1
->pfs_group
!= pp2
->pfs_group
) {
501 plog(LLV_WARNING
, LOCATION
, NULL
,
502 "pfs_group mismatch. mine:%d peer:%d\n",
503 pp1
->pfs_group
, pp2
->pfs_group
);
507 if (pp1
->lifetime
> pp2
->lifetime
) {
508 plog(LLV_WARNING
, LOCATION
, NULL
,
509 "less lifetime proposed. mine:%d peer:%d\n",
510 (int)pp1
->lifetime
, (int)pp2
->lifetime
);
513 if (pp1
->lifebyte
> pp2
->lifebyte
) {
514 plog(LLV_WARNING
, LOCATION
, NULL
,
515 "less lifebyte proposed. mine:%d peer:%d\n",
516 pp1
->lifebyte
, pp2
->lifebyte
);
524 * take a single match between satrns. returns 0 if tr1 equals to tr2.
529 cmpsatrns(proto_id
, tr1
, tr2
)
531 const struct satrns
*tr1
, *tr2
;
533 if (tr1
->trns_id
!= tr2
->trns_id
) {
534 plog(LLV_WARNING
, LOCATION
, NULL
,
535 "trns_id mismatched: "
537 s_ipsecdoi_trns(proto_id
, tr2
->trns_id
),
538 s_ipsecdoi_trns(proto_id
, tr1
->trns_id
));
542 if (tr1
->authtype
!= tr2
->authtype
) {
543 plog(LLV_WARNING
, LOCATION
, NULL
,
544 "authtype mismatched: "
546 s_ipsecdoi_attr_v(IPSECDOI_ATTR_AUTH
, tr2
->authtype
),
547 s_ipsecdoi_attr_v(IPSECDOI_ATTR_AUTH
, tr1
->authtype
));
552 * At this moment for interoperability, the responder obey
553 * the initiator. It should be defined a notify message.
555 if (tr1
->encklen
> tr2
->encklen
) {
556 plog(LLV_WARNING
, LOCATION
, NULL
,
557 "less key length proposed, "
558 "mine:%d peer:%d. Use initiaotr's one.\n",
559 tr2
->encklen
, tr1
->encklen
);
567 set_satrnsbysainfo(pr
, sainfo
)
569 struct sainfo
*sainfo
;
571 struct sainfoalg
*a
, *b
;
572 struct satrns
*newtr
;
575 switch (pr
->proto_id
) {
576 case IPSECDOI_PROTO_IPSEC_AH
:
577 if (sainfo
->algs
[algclass_ipsec_auth
] == NULL
) {
578 plog(LLV_ERROR
, LOCATION
, NULL
,
579 "no auth algorithm found\n");
583 for (a
= sainfo
->algs
[algclass_ipsec_auth
]; a
; a
= a
->next
) {
585 if (a
->alg
== IPSECDOI_ATTR_AUTH_NONE
)
588 /* allocate satrns */
591 plog(LLV_ERROR
, LOCATION
, NULL
,
592 "failed to allocate satrns.\n");
596 newtr
->trns_no
= t
++;
597 newtr
->trns_id
= ipsecdoi_authalg2trnsid(a
->alg
);
598 newtr
->authtype
= a
->alg
;
600 inssatrns(pr
, newtr
);
603 case IPSECDOI_PROTO_IPSEC_ESP
:
604 if (sainfo
->algs
[algclass_ipsec_enc
] == NULL
) {
605 plog(LLV_ERROR
, LOCATION
, NULL
,
606 "no encryption algorithm found\n");
610 for (a
= sainfo
->algs
[algclass_ipsec_enc
]; a
; a
= a
->next
) {
611 for (b
= sainfo
->algs
[algclass_ipsec_auth
]; b
; b
= b
->next
) {
612 /* allocate satrns */
615 plog(LLV_ERROR
, LOCATION
, NULL
,
616 "failed to allocate satrns.\n");
620 newtr
->trns_no
= t
++;
621 newtr
->trns_id
= a
->alg
;
622 newtr
->encklen
= a
->encklen
;
623 newtr
->authtype
= b
->alg
;
625 inssatrns(pr
, newtr
);
629 case IPSECDOI_PROTO_IPCOMP
:
630 if (sainfo
->algs
[algclass_ipsec_comp
] == NULL
) {
631 plog(LLV_ERROR
, LOCATION
, NULL
,
632 "no ipcomp algorithm found\n");
636 for (a
= sainfo
->algs
[algclass_ipsec_comp
]; a
; a
= a
->next
) {
638 /* allocate satrns */
641 plog(LLV_ERROR
, LOCATION
, NULL
,
642 "failed to allocate satrns.\n");
646 newtr
->trns_no
= t
++;
647 newtr
->trns_id
= a
->alg
;
648 newtr
->authtype
= IPSECDOI_ATTR_AUTH_NONE
; /*no auth*/
650 inssatrns(pr
, newtr
);
654 plog(LLV_ERROR
, LOCATION
, NULL
,
655 "unknown proto_id (%d).\n", pr
->proto_id
);
659 /* no proposal found */
660 if (pr
->head
== NULL
) {
661 plog(LLV_ERROR
, LOCATION
, NULL
, "no algorithms found.\n");
668 flushsatrns(pr
->head
);
674 struct prop_pair
*p0
;
676 struct prop_pair
*p
, *t
;
677 struct saprop
*newpp
;
678 struct saproto
*newpr
;
679 struct satrns
*newtr
;
685 /* allocate ipsec a sa proposal */
688 plog(LLV_ERROR
, LOCATION
, NULL
,
689 "failed to allocate saprop.\n");
692 newpp
->prop_no
= p0
->prop
->p_no
;
693 /* lifetime & lifebyte must be updated later */
695 for (p
= p0
; p
; p
= p
->next
) {
697 /* allocate ipsec sa protocol */
698 newpr
= newsaproto();
700 plog(LLV_ERROR
, LOCATION
, NULL
,
701 "failed to allocate saproto.\n");
706 /* XXX should be handled isakmp cookie */
707 if (sizeof(newpr
->spi
) < p
->prop
->spi_size
) {
708 plog(LLV_ERROR
, LOCATION
, NULL
,
709 "invalid spi size %d.\n", p
->prop
->spi_size
);
715 * XXX SPI bits are left-filled, for use with IPComp.
716 * we should be switching to variable-length spi field...
718 newpr
->proto_id
= p
->prop
->proto_id
;
719 newpr
->spisize
= p
->prop
->spi_size
;
720 memset(&newpr
->spi
, 0, sizeof(newpr
->spi
));
721 spi
= (u_int8_t
*)&newpr
->spi
;
722 spi
+= sizeof(newpr
->spi
);
723 spi
-= p
->prop
->spi_size
;
724 memcpy(spi
, p
->prop
+ 1, p
->prop
->spi_size
);
726 newpr
->reqid_out
= 0;
728 for (t
= p
; t
; t
= t
->tnext
) {
730 plog(LLV_DEBUG
, LOCATION
, NULL
,
731 "prop#=%d prot-id=%s spi-size=%d "
732 "#trns=%d trns#=%d trns-id=%s\n",
734 s_ipsecdoi_proto(t
->prop
->proto_id
),
735 t
->prop
->spi_size
, t
->prop
->num_t
,
737 s_ipsecdoi_trns(t
->prop
->proto_id
,
740 /* allocate ipsec sa transform */
743 plog(LLV_ERROR
, LOCATION
, NULL
,
744 "failed to allocate satrns.\n");
749 if (ipsecdoi_t2satrns(t
->trns
,
750 newpp
, newpr
, newtr
) < 0) {
757 inssatrns(newpr
, newtr
);
761 * If the peer does not specify encryption mode, use
762 * transport mode by default. This is to conform to
763 * draft-shacham-ippcp-rfc2393bis-08.txt (explicitly specifies
764 * that unspecified == transport), as well as RFC2407
765 * (unspecified == implementation dependent default).
767 if (newpr
->encmode
== 0)
768 newpr
->encmode
= IPSECDOI_ATTR_ENC_MODE_TRNS
;
770 inssaproto(newpp
, newpr
);
784 struct saprop
*p
, *save
;
786 for (p
= head
; p
!= NULL
; p
= save
) {
788 flushsaproto(p
->head
);
797 struct saproto
*head
;
799 struct saproto
*p
, *save
;
801 for (p
= head
; p
!= NULL
; p
= save
) {
803 flushsatrns(p
->head
);
816 struct satrns
*p
, *save
;
818 for (p
= head
; p
!= NULL
; p
= save
) {
827 * print multiple proposals
832 const struct saprop
*pp
;
834 const struct saprop
*p
;
837 plog(pri
, LOCATION
, NULL
, "(null)");
841 for (p
= pp
; p
; p
= p
->next
) {
842 printsaprop0(pri
, p
);
849 * print one proposal.
852 printsaprop0(pri
, pp
)
854 const struct saprop
*pp
;
856 const struct saproto
*p
;
861 for (p
= pp
->head
; p
; p
= p
->next
) {
862 printsaproto(pri
, p
);
869 printsaproto(pri
, pr
)
871 const struct saproto
*pr
;
878 plog(pri
, LOCATION
, NULL
,
879 " (proto_id=%s spisize=%d spi=%08lx spi_p=%08lx "
880 "encmode=%s reqid=%d:%d)\n",
881 s_ipsecdoi_proto(pr
->proto_id
),
883 (unsigned long)ntohl(pr
->spi
),
884 (unsigned long)ntohl(pr
->spi_p
),
885 s_ipsecdoi_attr_v(IPSECDOI_ATTR_ENC_MODE
, pr
->encmode
),
886 (int)pr
->reqid_in
, (int)pr
->reqid_out
);
888 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
889 printsatrns(pri
, pr
->proto_id
, tr
);
896 printsatrns(pri
, proto_id
, tr
)
899 const struct satrns
*tr
;
905 case IPSECDOI_PROTO_IPSEC_AH
:
906 plog(pri
, LOCATION
, NULL
,
907 " (trns_id=%s authtype=%s)\n",
908 s_ipsecdoi_trns(proto_id
, tr
->trns_id
),
909 s_ipsecdoi_attr_v(IPSECDOI_ATTR_AUTH
, tr
->authtype
));
911 case IPSECDOI_PROTO_IPSEC_ESP
:
912 plog(pri
, LOCATION
, NULL
,
913 " (trns_id=%s encklen=%d authtype=%s)\n",
914 s_ipsecdoi_trns(proto_id
, tr
->trns_id
),
916 s_ipsecdoi_attr_v(IPSECDOI_ATTR_AUTH
, tr
->authtype
));
918 case IPSECDOI_PROTO_IPCOMP
:
919 plog(pri
, LOCATION
, NULL
,
921 s_ipsecdoi_trns(proto_id
, tr
->trns_id
));
924 plog(pri
, LOCATION
, NULL
,
925 "(unknown proto_id %d)\n", proto_id
);
932 print_proppair0(pri
, p
, level
)
939 memset(spc
, ' ', sizeof(spc
));
940 spc
[sizeof(spc
) - 1] = '\0';
945 plog(pri
, LOCATION
, NULL
,
946 "%s%p: next=%p tnext=%p\n", spc
, p
, p
->next
, p
->tnext
);
948 print_proppair0(pri
, p
->next
, level
+ 1);
950 print_proppair0(pri
, p
->tnext
, level
+ 1);
954 print_proppair(pri
, p
)
958 print_proppair0(pri
, p
, 1);
962 set_proposal_from_policy(iph2
, sp_main
, sp_sub
)
963 struct ph2handle
*iph2
;
964 struct secpolicy
*sp_main
, *sp_sub
;
966 struct saprop
*newpp
;
967 struct ipsecrequest
*req
;
968 int encmodesv
= IPSEC_MODE_TRANSPORT
; /* use only when complex_bundle */
972 plog(LLV_ERROR
, LOCATION
, NULL
,
973 "failed to allocate saprop.\n");
977 newpp
->lifetime
= iph2
->sainfo
->lifetime
;
978 newpp
->lifebyte
= iph2
->sainfo
->lifebyte
;
979 newpp
->pfs_group
= iph2
->sainfo
->pfs_group
;
981 if (lcconf
->complex_bundle
)
985 * decide the encryption mode of this SA bundle.
986 * the mode becomes tunnel mode when there is even one policy
987 * of tunnel mode in the SPD. otherwise the mode becomes
990 encmodesv
= IPSEC_MODE_TRANSPORT
;
991 for (req
= sp_main
->req
; req
; req
= req
->next
) {
992 if (req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
993 encmodesv
= pfkey2ipsecdoi_mode(req
->saidx
.mode
);
995 if (iph2
->ph1
&& (iph2
->ph1
->natt_flags
& NAT_DETECTED
))
996 encmodesv
+= iph2
->ph1
->natt_options
->mode_udp_diff
;
1003 for (req
= sp_main
->req
; req
; req
= req
->next
) {
1004 struct saproto
*newpr
;
1005 caddr_t paddr
= NULL
;
1008 * check if SA bundle ?
1009 * nested SAs negotiation is NOT supported.
1010 * me +--- SA1 ---+ peer1
1011 * me +--- SA2 --------------+ peer2
1014 if (req
->saidx
.src
.ss_family
&& req
->saidx
.dst
.ss_family
) {
1016 if (req
->saidx
.src
.ss_len
&& req
->saidx
.dst
.ss_len
) {
1018 /* check the end of ip addresses of SA */
1019 if (iph2
->side
== INITIATOR
)
1020 paddr
= (caddr_t
)&req
->saidx
.dst
;
1022 paddr
= (caddr_t
)&req
->saidx
.src
;
1025 /* allocate ipsec sa protocol */
1026 newpr
= newsaproto();
1027 if (newpr
== NULL
) {
1028 plog(LLV_ERROR
, LOCATION
, NULL
,
1029 "failed to allocate saproto.\n");
1033 newpr
->proto_id
= ipproto2doi(req
->saidx
.proto
);
1034 if (newpr
->proto_id
== IPSECDOI_PROTO_IPCOMP
)
1038 if (lcconf
->complex_bundle
) {
1039 newpr
->encmode
= pfkey2ipsecdoi_mode(req
->saidx
.mode
);
1041 if (iph2
->ph1
&& (iph2
->ph1
->natt_flags
& NAT_DETECTED
))
1042 newpr
->encmode
+= iph2
->ph1
->natt_options
->mode_udp_diff
;
1046 newpr
->encmode
= encmodesv
;
1048 if (iph2
->side
== INITIATOR
)
1049 newpr
->reqid_out
= req
->saidx
.reqid
;
1051 newpr
->reqid_in
= req
->saidx
.reqid
;
1053 if (set_satrnsbysainfo(newpr
, iph2
->sainfo
) < 0) {
1054 plog(LLV_ERROR
, LOCATION
, NULL
,
1055 "failed to get algorithms.\n");
1060 /* set new saproto */
1061 inssaprotorev(newpp
, newpr
);
1064 /* get reqid_in from inbound policy */
1071 if (iph2
->side
== INITIATOR
)
1072 pr
->reqid_in
= req
->saidx
.reqid
;
1074 pr
->reqid_out
= req
->saidx
.reqid
;
1079 plog(LLV_NOTIFY
, LOCATION
, NULL
,
1080 "There is a difference "
1081 "between the in/out bound policies in SPD.\n");
1085 iph2
->proposal
= newpp
;
1087 ike_session_update_mode(iph2
);
1089 printsaprop0(LLV_DEBUG
, newpp
);
1099 * generate a policy from peer's proposal.
1100 * this function unconditionally chooses the first proposal the in SA payload
1104 set_proposal_from_proposal(iph2
)
1105 struct ph2handle
*iph2
;
1107 struct saprop
*newpp
= NULL
, *pp0
, *pp_peer
= NULL
;
1108 struct saproto
*newpr
= NULL
, *pr
;
1109 struct prop_pair
**pair
;
1113 /* get proposal pair */
1114 pair
= get_proppair(iph2
->sa
, IPSECDOI_TYPE_PH2
);
1119 * make my proposal according as the client proposal.
1120 * XXX assumed there is only one proposal even if it's the SA bundle.
1122 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
1123 if (pair
[i
] == NULL
)
1126 if (pp_peer
!= NULL
)
1127 flushsaprop(pp_peer
);
1129 pp_peer
= aproppair2saprop(pair
[i
]);
1130 if (pp_peer
== NULL
)
1135 plog(LLV_ERROR
, LOCATION
, NULL
,
1136 "failed to allocate saprop.\n");
1140 pp0
->lifetime
= iph2
->sainfo
->lifetime
;
1141 pp0
->lifebyte
= iph2
->sainfo
->lifebyte
;
1142 pp0
->pfs_group
= iph2
->sainfo
->pfs_group
;
1144 if (pp_peer
->next
!= NULL
) {
1145 plog(LLV_ERROR
, LOCATION
, NULL
,
1146 "pp_peer is inconsistency, ignore it.\n");
1150 for (pr
= pp_peer
->head
; pr
; pr
= pr
->next
) {
1152 newpr
= newsaproto();
1153 if (newpr
== NULL
) {
1154 plog(LLV_ERROR
, LOCATION
, NULL
,
1155 "failed to allocate saproto.\n");
1159 newpr
->proto_id
= pr
->proto_id
;
1160 newpr
->spisize
= pr
->spisize
;
1161 newpr
->encmode
= pr
->encmode
;
1163 newpr
->spi_p
= pr
->spi
; /* copy peer's SPI */
1164 newpr
->reqid_in
= 0;
1165 newpr
->reqid_out
= 0;
1167 if (set_satrnsbysainfo(newpr
, iph2
->sainfo
) < 0) {
1168 plog(LLV_ERROR
, LOCATION
, NULL
,
1169 "failed to get algorithms.\n");
1174 inssaproto(pp0
, newpr
);
1177 inssaprop(&newpp
, pp0
);
1180 plog(LLV_DEBUG
, LOCATION
, NULL
, "make a proposal from peer's:\n");
1181 printsaprop0(LLV_DEBUG
, newpp
);
1183 iph2
->proposal
= newpp
;
1185 ike_session_update_mode(iph2
);
1194 flushsaprop(pp_peer
);
1196 free_proppair(pair
);
1206 for (pr
= p
->head
; pr
; pr
= pr
->next
)
1207 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
)