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");
457 newtr
->trns_no
= tr1
->trns_no
;
458 newtr
->trns_id
= tr1
->trns_id
;
459 newtr
->encklen
= tr1
->encklen
;
460 newtr
->authtype
= tr1
->authtype
;
462 inssatrns(newpr
, newtr
);
463 inssaproto(newpp
, newpr
);
469 /* XXX should check if we have visited all items or not */
483 /* should be matched all protocols in a proposal */
484 if (pr1
!= NULL
|| pr2
!= NULL
)
494 /* take a single match between saprop. returns 0 if pp1 equals to pp2. */
497 const struct saprop
*pp1
, *pp2
;
499 if (pp1
->pfs_group
!= pp2
->pfs_group
) {
500 plog(LLV_WARNING
, LOCATION
, NULL
,
501 "pfs_group mismatch. mine:%d peer:%d\n",
502 pp1
->pfs_group
, pp2
->pfs_group
);
506 if (pp1
->lifetime
> pp2
->lifetime
) {
507 plog(LLV_WARNING
, LOCATION
, NULL
,
508 "less lifetime proposed. mine:%d peer:%d\n",
509 (int)pp1
->lifetime
, (int)pp2
->lifetime
);
512 if (pp1
->lifebyte
> pp2
->lifebyte
) {
513 plog(LLV_WARNING
, LOCATION
, NULL
,
514 "less lifebyte proposed. mine:%d peer:%d\n",
515 pp1
->lifebyte
, pp2
->lifebyte
);
523 * take a single match between satrns. returns 0 if tr1 equals to tr2.
528 cmpsatrns(proto_id
, tr1
, tr2
)
530 const struct satrns
*tr1
, *tr2
;
532 if (tr1
->trns_id
!= tr2
->trns_id
) {
533 plog(LLV_WARNING
, LOCATION
, NULL
,
534 "trns_id mismatched: "
536 s_ipsecdoi_trns(proto_id
, tr2
->trns_id
),
537 s_ipsecdoi_trns(proto_id
, tr1
->trns_id
));
541 if (tr1
->authtype
!= tr2
->authtype
) {
542 plog(LLV_WARNING
, LOCATION
, NULL
,
543 "authtype mismatched: "
545 s_ipsecdoi_attr_v(IPSECDOI_ATTR_AUTH
, tr2
->authtype
),
546 s_ipsecdoi_attr_v(IPSECDOI_ATTR_AUTH
, tr1
->authtype
));
551 * At this moment for interoperability, the responder obey
552 * the initiator. It should be defined a notify message.
554 if (tr1
->encklen
> tr2
->encklen
) {
555 plog(LLV_WARNING
, LOCATION
, NULL
,
556 "less key length proposed, "
557 "mine:%d peer:%d. Use initiaotr's one.\n",
558 tr2
->encklen
, tr1
->encklen
);
566 set_satrnsbysainfo(pr
, sainfo
)
568 struct sainfo
*sainfo
;
570 struct sainfoalg
*a
, *b
;
571 struct satrns
*newtr
;
574 switch (pr
->proto_id
) {
575 case IPSECDOI_PROTO_IPSEC_AH
:
576 if (sainfo
->algs
[algclass_ipsec_auth
] == NULL
) {
577 plog(LLV_ERROR
, LOCATION
, NULL
,
578 "no auth algorithm found\n");
582 for (a
= sainfo
->algs
[algclass_ipsec_auth
]; a
; a
= a
->next
) {
584 if (a
->alg
== IPSECDOI_ATTR_AUTH_NONE
)
587 /* allocate satrns */
590 plog(LLV_ERROR
, LOCATION
, NULL
,
591 "failed to allocate satrns.\n");
595 newtr
->trns_no
= t
++;
596 newtr
->trns_id
= ipsecdoi_authalg2trnsid(a
->alg
);
597 newtr
->authtype
= a
->alg
;
599 inssatrns(pr
, newtr
);
602 case IPSECDOI_PROTO_IPSEC_ESP
:
603 if (sainfo
->algs
[algclass_ipsec_enc
] == NULL
) {
604 plog(LLV_ERROR
, LOCATION
, NULL
,
605 "no encryption algorithm found\n");
609 for (a
= sainfo
->algs
[algclass_ipsec_enc
]; a
; a
= a
->next
) {
610 for (b
= sainfo
->algs
[algclass_ipsec_auth
]; b
; b
= b
->next
) {
611 /* allocate satrns */
614 plog(LLV_ERROR
, LOCATION
, NULL
,
615 "failed to allocate satrns.\n");
619 newtr
->trns_no
= t
++;
620 newtr
->trns_id
= a
->alg
;
621 newtr
->encklen
= a
->encklen
;
622 newtr
->authtype
= b
->alg
;
624 inssatrns(pr
, newtr
);
628 case IPSECDOI_PROTO_IPCOMP
:
629 if (sainfo
->algs
[algclass_ipsec_comp
] == NULL
) {
630 plog(LLV_ERROR
, LOCATION
, NULL
,
631 "no ipcomp algorithm found\n");
635 for (a
= sainfo
->algs
[algclass_ipsec_comp
]; a
; a
= a
->next
) {
637 /* allocate satrns */
640 plog(LLV_ERROR
, LOCATION
, NULL
,
641 "failed to allocate satrns.\n");
645 newtr
->trns_no
= t
++;
646 newtr
->trns_id
= a
->alg
;
647 newtr
->authtype
= IPSECDOI_ATTR_AUTH_NONE
; /*no auth*/
649 inssatrns(pr
, newtr
);
653 plog(LLV_ERROR
, LOCATION
, NULL
,
654 "unknown proto_id (%d).\n", pr
->proto_id
);
658 /* no proposal found */
659 if (pr
->head
== NULL
) {
660 plog(LLV_ERROR
, LOCATION
, NULL
, "no algorithms found.\n");
667 flushsatrns(pr
->head
);
673 struct prop_pair
*p0
;
675 struct prop_pair
*p
, *t
;
676 struct saprop
*newpp
;
677 struct saproto
*newpr
;
678 struct satrns
*newtr
;
684 /* allocate ipsec a sa proposal */
687 plog(LLV_ERROR
, LOCATION
, NULL
,
688 "failed to allocate saprop.\n");
691 newpp
->prop_no
= p0
->prop
->p_no
;
692 /* lifetime & lifebyte must be updated later */
694 for (p
= p0
; p
; p
= p
->next
) {
696 /* allocate ipsec sa protocol */
697 newpr
= newsaproto();
699 plog(LLV_ERROR
, LOCATION
, NULL
,
700 "failed to allocate saproto.\n");
705 /* XXX should be handled isakmp cookie */
706 if (sizeof(newpr
->spi
) < p
->prop
->spi_size
) {
707 plog(LLV_ERROR
, LOCATION
, NULL
,
708 "invalid spi size %d.\n", p
->prop
->spi_size
);
713 * XXX SPI bits are left-filled, for use with IPComp.
714 * we should be switching to variable-length spi field...
716 newpr
->proto_id
= p
->prop
->proto_id
;
717 newpr
->spisize
= p
->prop
->spi_size
;
718 memset(&newpr
->spi
, 0, sizeof(newpr
->spi
));
719 spi
= (u_int8_t
*)&newpr
->spi
;
720 spi
+= sizeof(newpr
->spi
);
721 spi
-= p
->prop
->spi_size
;
722 memcpy(spi
, p
->prop
+ 1, p
->prop
->spi_size
);
724 newpr
->reqid_out
= 0;
726 for (t
= p
; t
; t
= t
->tnext
) {
728 plog(LLV_DEBUG
, LOCATION
, NULL
,
729 "prop#=%d prot-id=%s spi-size=%d "
730 "#trns=%d trns#=%d trns-id=%s\n",
732 s_ipsecdoi_proto(t
->prop
->proto_id
),
733 t
->prop
->spi_size
, t
->prop
->num_t
,
735 s_ipsecdoi_trns(t
->prop
->proto_id
,
738 /* allocate ipsec sa transform */
741 plog(LLV_ERROR
, LOCATION
, NULL
,
742 "failed to allocate satrns.\n");
746 if (ipsecdoi_t2satrns(t
->trns
, newpp
, newpr
, newtr
) < 0) {
751 inssatrns(newpr
, newtr
);
755 * If the peer does not specify encryption mode, use
756 * transport mode by default. This is to conform to
757 * draft-shacham-ippcp-rfc2393bis-08.txt (explicitly specifies
758 * that unspecified == transport), as well as RFC2407
759 * (unspecified == implementation dependent default).
761 if (newpr
->encmode
== 0)
762 newpr
->encmode
= IPSECDOI_ATTR_ENC_MODE_TRNS
;
764 inssaproto(newpp
, newpr
);
778 struct saprop
*p
, *save
;
780 for (p
= head
; p
!= NULL
; p
= save
) {
782 flushsaproto(p
->head
);
791 struct saproto
*head
;
793 struct saproto
*p
, *save
;
795 for (p
= head
; p
!= NULL
; p
= save
) {
797 flushsatrns(p
->head
);
810 struct satrns
*p
, *save
;
812 for (p
= head
; p
!= NULL
; p
= save
) {
821 * print multiple proposals
826 const struct saprop
*pp
;
828 const struct saprop
*p
;
831 plog(pri
, LOCATION
, NULL
, "(null)");
835 for (p
= pp
; p
; p
= p
->next
) {
836 printsaprop0(pri
, p
);
843 * print one proposal.
846 printsaprop0(pri
, pp
)
848 const struct saprop
*pp
;
850 const struct saproto
*p
;
855 for (p
= pp
->head
; p
; p
= p
->next
) {
856 printsaproto(pri
, p
);
863 printsaproto(pri
, pr
)
865 const struct saproto
*pr
;
872 plog(pri
, LOCATION
, NULL
,
873 " (proto_id=%s spisize=%d spi=%08lx spi_p=%08lx "
874 "encmode=%s reqid=%d:%d)\n",
875 s_ipsecdoi_proto(pr
->proto_id
),
877 (unsigned long)ntohl(pr
->spi
),
878 (unsigned long)ntohl(pr
->spi_p
),
879 s_ipsecdoi_attr_v(IPSECDOI_ATTR_ENC_MODE
, pr
->encmode
),
880 (int)pr
->reqid_in
, (int)pr
->reqid_out
);
882 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
883 printsatrns(pri
, pr
->proto_id
, tr
);
890 printsatrns(pri
, proto_id
, tr
)
893 const struct satrns
*tr
;
899 case IPSECDOI_PROTO_IPSEC_AH
:
900 plog(pri
, LOCATION
, NULL
,
901 " (trns_id=%s authtype=%s)\n",
902 s_ipsecdoi_trns(proto_id
, tr
->trns_id
),
903 s_ipsecdoi_attr_v(IPSECDOI_ATTR_AUTH
, tr
->authtype
));
905 case IPSECDOI_PROTO_IPSEC_ESP
:
906 plog(pri
, LOCATION
, NULL
,
907 " (trns_id=%s encklen=%d authtype=%s)\n",
908 s_ipsecdoi_trns(proto_id
, tr
->trns_id
),
910 s_ipsecdoi_attr_v(IPSECDOI_ATTR_AUTH
, tr
->authtype
));
912 case IPSECDOI_PROTO_IPCOMP
:
913 plog(pri
, LOCATION
, NULL
,
915 s_ipsecdoi_trns(proto_id
, tr
->trns_id
));
918 plog(pri
, LOCATION
, NULL
,
919 "(unknown proto_id %d)\n", proto_id
);
926 print_proppair0(pri
, p
, level
)
933 memset(spc
, ' ', sizeof(spc
));
934 spc
[sizeof(spc
) - 1] = '\0';
939 plog(pri
, LOCATION
, NULL
,
940 "%s%p: next=%p tnext=%p\n", spc
, p
, p
->next
, p
->tnext
);
942 print_proppair0(pri
, p
->next
, level
+ 1);
944 print_proppair0(pri
, p
->tnext
, level
+ 1);
948 print_proppair(pri
, p
)
952 print_proppair0(pri
, p
, 1);
956 set_proposal_from_policy(iph2
, sp_main
, sp_sub
)
957 struct ph2handle
*iph2
;
958 struct secpolicy
*sp_main
, *sp_sub
;
960 struct saprop
*newpp
;
961 struct ipsecrequest
*req
;
962 int encmodesv
= IPSEC_MODE_TRANSPORT
; /* use only when complex_bundle */
966 plog(LLV_ERROR
, LOCATION
, NULL
,
967 "failed to allocate saprop.\n");
971 newpp
->lifetime
= iph2
->sainfo
->lifetime
;
972 newpp
->lifebyte
= iph2
->sainfo
->lifebyte
;
973 newpp
->pfs_group
= iph2
->sainfo
->pfs_group
;
975 if (lcconf
->complex_bundle
)
979 * decide the encryption mode of this SA bundle.
980 * the mode becomes tunnel mode when there is even one policy
981 * of tunnel mode in the SPD. otherwise the mode becomes
984 encmodesv
= IPSEC_MODE_TRANSPORT
;
985 for (req
= sp_main
->req
; req
; req
= req
->next
) {
986 if (req
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
987 encmodesv
= pfkey2ipsecdoi_mode(req
->saidx
.mode
);
989 if (iph2
->ph1
&& (iph2
->ph1
->natt_flags
& NAT_DETECTED
))
990 encmodesv
+= iph2
->ph1
->natt_options
->mode_udp_diff
;
997 for (req
= sp_main
->req
; req
; req
= req
->next
) {
998 struct saproto
*newpr
;
999 caddr_t paddr
= NULL
;
1002 * check if SA bundle ?
1003 * nested SAs negotiation is NOT supported.
1004 * me +--- SA1 ---+ peer1
1005 * me +--- SA2 --------------+ peer2
1008 if (req
->saidx
.src
.ss_family
&& req
->saidx
.dst
.ss_family
) {
1010 if (req
->saidx
.src
.ss_len
&& req
->saidx
.dst
.ss_len
) {
1012 /* check the end of ip addresses of SA */
1013 if (iph2
->side
== INITIATOR
)
1014 paddr
= (caddr_t
)&req
->saidx
.dst
;
1016 paddr
= (caddr_t
)&req
->saidx
.src
;
1019 /* allocate ipsec sa protocol */
1020 newpr
= newsaproto();
1021 if (newpr
== NULL
) {
1022 plog(LLV_ERROR
, LOCATION
, NULL
,
1023 "failed to allocate saproto.\n");
1027 newpr
->proto_id
= ipproto2doi(req
->saidx
.proto
);
1028 if (newpr
->proto_id
== IPSECDOI_PROTO_IPCOMP
)
1032 if (lcconf
->complex_bundle
) {
1033 newpr
->encmode
= pfkey2ipsecdoi_mode(req
->saidx
.mode
);
1035 if (iph2
->ph1
&& (iph2
->ph1
->natt_flags
& NAT_DETECTED
))
1036 newpr
->encmode
+= iph2
->ph1
->natt_options
->mode_udp_diff
;
1040 newpr
->encmode
= encmodesv
;
1042 if (iph2
->side
== INITIATOR
)
1043 newpr
->reqid_out
= req
->saidx
.reqid
;
1045 newpr
->reqid_in
= req
->saidx
.reqid
;
1047 if (set_satrnsbysainfo(newpr
, iph2
->sainfo
) < 0) {
1048 plog(LLV_ERROR
, LOCATION
, NULL
,
1049 "failed to get algorithms.\n");
1053 /* set new saproto */
1054 inssaprotorev(newpp
, newpr
);
1057 /* get reqid_in from inbound policy */
1064 if (iph2
->side
== INITIATOR
)
1065 pr
->reqid_in
= req
->saidx
.reqid
;
1067 pr
->reqid_out
= req
->saidx
.reqid
;
1072 plog(LLV_NOTIFY
, LOCATION
, NULL
,
1073 "There is a difference "
1074 "between the in/out bound policies in SPD.\n");
1078 iph2
->proposal
= newpp
;
1080 printsaprop0(LLV_DEBUG
, newpp
);
1088 * generate a policy from peer's proposal.
1089 * this function unconditionally chooses the first proposal the in SA payload
1093 set_proposal_from_proposal(iph2
)
1094 struct ph2handle
*iph2
;
1096 struct saprop
*newpp
= NULL
, *pp0
, *pp_peer
= NULL
;
1097 struct saproto
*newpr
= NULL
, *pr
;
1098 struct prop_pair
**pair
;
1102 /* get proposal pair */
1103 pair
= get_proppair(iph2
->sa
, IPSECDOI_TYPE_PH2
);
1108 * make my proposal according as the client proposal.
1109 * XXX assumed there is only one proposal even if it's the SA bundle.
1111 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
1112 if (pair
[i
] == NULL
)
1114 pp_peer
= aproppair2saprop(pair
[i
]);
1115 if (pp_peer
== NULL
)
1120 plog(LLV_ERROR
, LOCATION
, NULL
,
1121 "failed to allocate saprop.\n");
1125 pp0
->lifetime
= iph2
->sainfo
->lifetime
;
1126 pp0
->lifebyte
= iph2
->sainfo
->lifebyte
;
1127 pp0
->pfs_group
= iph2
->sainfo
->pfs_group
;
1129 if (pp_peer
->next
!= NULL
) {
1130 plog(LLV_ERROR
, LOCATION
, NULL
,
1131 "pp_peer is inconsistency, ignore it.\n");
1135 for (pr
= pp_peer
->head
; pr
; pr
= pr
->next
) {
1137 newpr
= newsaproto();
1138 if (newpr
== NULL
) {
1139 plog(LLV_ERROR
, LOCATION
, NULL
,
1140 "failed to allocate saproto.\n");
1143 newpr
->proto_id
= pr
->proto_id
;
1144 newpr
->spisize
= pr
->spisize
;
1145 newpr
->encmode
= pr
->encmode
;
1147 newpr
->spi_p
= pr
->spi
; /* copy peer's SPI */
1148 newpr
->reqid_in
= 0;
1149 newpr
->reqid_out
= 0;
1152 if (set_satrnsbysainfo(newpr
, iph2
->sainfo
) < 0) {
1153 plog(LLV_ERROR
, LOCATION
, NULL
,
1154 "failed to get algorithms.\n");
1158 inssaproto(pp0
, newpr
);
1159 inssaprop(&newpp
, pp0
);
1162 plog(LLV_DEBUG
, LOCATION
, NULL
, "make a proposal from peer's:\n");
1163 printsaprop0(LLV_DEBUG
, newpp
);
1165 iph2
->proposal
= newpp
;
1174 flushsaprop(pp_peer
);
1175 free_proppair(pair
);
1185 for (pr
= p
->head
; pr
; pr
= pr
->next
)
1186 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
)