1 /* $NetBSD: isakmp.c,v 1.59 2009/09/01 09:24:21 tteras Exp $ */
3 /* Id: isakmp.c,v 1.74 2006/05/07 21:32:59 manubsd 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/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
50 #if TIME_WITH_SYS_TIME
51 # include <sys/time.h>
55 # include <sys/time.h>
78 #include "remoteconf.h"
79 #include "localconf.h"
80 #include "grabmyaddr.h"
83 #include "isakmp_var.h"
88 #include "ipsec_doi.h"
90 #include "crypto_openssl.h"
92 #include "algorithm.h"
95 #include "isakmp_ident.h"
96 #include "isakmp_agg.h"
97 #include "isakmp_base.h"
98 #include "isakmp_quick.h"
99 #include "isakmp_inf.h"
100 #include "isakmp_newg.h"
102 #include "vendorid.h"
103 #include "isakmp_xauth.h"
104 #include "isakmp_unity.h"
105 #include "isakmp_cfg.h"
108 #include "isakmp_frag.h"
110 #include "strnames.h"
115 # include "nattraversal.h"
118 # include <linux/udp.h>
119 # include <linux/ip.h>
123 # endif /* __linux__ */
124 # if defined(__NetBSD__) || defined(__FreeBSD__) || \
125 (defined(__APPLE__) && defined(__MACH__))
126 # include <netinet/in.h>
127 # include <netinet/udp.h>
128 # include <netinet/in_systm.h>
129 # include <netinet/ip.h>
130 # define SOL_UDP IPPROTO_UDP
131 # endif /* __NetBSD__ / __FreeBSD__ */
133 static int nostate1
__P((struct ph1handle
*, vchar_t
*));
134 static int nostate2
__P((struct ph2handle
*, vchar_t
*));
136 extern caddr_t
val2str(const char *, size_t);
138 static int (*ph1exchange
[][2][PHASE1ST_MAX
])
139 __P((struct ph1handle
*, vchar_t
*)) = {
142 /* Identity Protection exchange */
144 { nostate1
, ident_i1send
, nostate1
, ident_i2recv
, ident_i2send
,
145 ident_i3recv
, ident_i3send
, ident_i4recv
, ident_i4send
, nostate1
, nostate1
,},
146 { nostate1
, ident_r1recv
, ident_r1send
, ident_r2recv
, ident_r2send
,
147 ident_r3recv
, ident_r3send
, nostate1
, nostate1
, nostate1
, nostate1
, },
149 /* Aggressive exchange */
151 { nostate1
, agg_i1send
, nostate1
, agg_i2recv
, agg_i2send
,
152 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
153 { nostate1
, agg_r1recv
, agg_r1send
, agg_r2recv
, agg_r2send
,
154 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
158 { nostate1
, base_i1send
, nostate1
, base_i2recv
, base_i2send
,
159 base_i3recv
, base_i3send
, nostate1
, nostate1
, nostate1
, nostate1
, },
160 { nostate1
, base_r1recv
, base_r1send
, base_r2recv
, base_r2send
,
161 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
165 static int (*ph2exchange
[][2][PHASE2ST_MAX
])
166 __P((struct ph2handle
*, vchar_t
*)) = {
169 /* Quick mode for IKE */
171 { nostate2
, nostate2
, quick_i1prep
, nostate2
, quick_i1send
,
172 quick_i2recv
, quick_i2send
, quick_i3recv
, nostate2
, nostate2
, },
173 { nostate2
, quick_r1recv
, quick_r1prep
, nostate2
, quick_r2send
,
174 quick_r3recv
, quick_r3prep
, quick_r3send
, nostate2
, nostate2
, }
178 static u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
180 static int isakmp_main
__P((vchar_t
*, struct sockaddr
*, struct sockaddr
*));
181 static int ph1_main
__P((struct ph1handle
*, vchar_t
*));
182 static int quick_main
__P((struct ph2handle
*, vchar_t
*));
183 static int isakmp_ph1begin_r
__P((vchar_t
*,
184 struct sockaddr
*, struct sockaddr
*, u_int8_t
));
185 static int isakmp_ph2begin_i
__P((struct ph1handle
*, struct ph2handle
*));
186 static int isakmp_ph2begin_r
__P((struct ph1handle
*, vchar_t
*));
187 static int etypesw1
__P((int));
188 static int etypesw2
__P((int));
189 static int isakmp_ph1resend
__P((struct ph1handle
*));
190 static int isakmp_ph2resend
__P((struct ph2handle
*));
193 static int frag_handler(struct ph1handle
*,
194 vchar_t
*, struct sockaddr
*, struct sockaddr
*);
198 * isakmp packet handler
201 isakmp_handler(ctx
, so_isakmp
)
205 struct isakmp isakmp
;
207 char buf
[sizeof (isakmp
) + 4];
208 u_int32_t non_esp
[2];
216 char buf
[sizeof(isakmp
) + 4];
219 struct sockaddr_storage remote
;
220 struct sockaddr_storage local
;
221 unsigned int remote_len
= sizeof(remote
);
222 unsigned int local_len
= sizeof(local
);
223 int len
= 0, extralen
= 0;
224 vchar_t
*buf
= NULL
, *tmpbuf
= NULL
;
227 /* read message by MSG_PEEK */
228 while ((len
= recvfromto(so_isakmp
, x
.buf
, sizeof(x
),
229 MSG_PEEK
, (struct sockaddr
*)&remote
, &remote_len
,
230 (struct sockaddr
*)&local
, &local_len
)) < 0) {
233 plog(LLV_ERROR
, LOCATION
, NULL
,
234 "failed to receive isakmp packet: %s\n",
239 /* keep-alive packet - ignore */
240 if (len
== 1 && (x
.buf
[0]&0xff) == 0xff) {
241 /* Pull the keep-alive packet */
242 if ((len
= recvfrom(so_isakmp
, (char *)x
.buf
, 1,
243 0, (struct sockaddr
*)&remote
, &remote_len
)) != 1) {
244 plog(LLV_ERROR
, LOCATION
, NULL
,
245 "failed to receive keep alive packet: %s\n",
251 /* Lucent IKE in UDP encapsulation */
254 if (ntohs(x
.lbuf
.udp
.dest
) == 501) {
255 extralen
+= sizeof(x
.lbuf
.udp
) + x
.lbuf
.ip
.ihl
;
258 if (ntohs(x
.lbuf
.udp
.uh_dport
) == 501) {
259 extralen
+= sizeof(x
.lbuf
.udp
) + x
.lbuf
.ip
.ip_hl
;
265 /* we don't know about portchange yet,
266 look for non-esp marker instead */
267 if (x
.non_esp
[0] == 0 && x
.non_esp
[1] != 0)
268 extralen
= NON_ESP_MARKER_LEN
;
271 /* now we know if there is an extra non-esp
272 marker at the beginning or not */
273 memcpy ((char *)&isakmp
, x
.buf
+ extralen
, sizeof (isakmp
));
275 /* check isakmp header length, as well as sanity of header length */
276 if (len
< sizeof(isakmp
) || ntohl(isakmp
.len
) < sizeof(isakmp
)) {
277 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
278 "packet shorter than isakmp header size (%u, %u, %zu)\n",
279 len
, ntohl(isakmp
.len
), sizeof(isakmp
));
281 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
282 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
283 plog(LLV_ERROR
, LOCATION
, NULL
,
284 "failed to receive isakmp packet: %s\n",
290 /* reject it if the size is tooooo big. */
291 if (ntohl(isakmp
.len
) > 0xffff) {
292 plog(LLV_ERROR
, LOCATION
, NULL
,
293 "the length in the isakmp header is too big.\n");
294 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
295 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
296 plog(LLV_ERROR
, LOCATION
, NULL
,
297 "failed to receive isakmp packet: %s\n",
303 /* read real message */
304 if ((tmpbuf
= vmalloc(ntohl(isakmp
.len
) + extralen
)) == NULL
) {
305 plog(LLV_ERROR
, LOCATION
, NULL
,
306 "failed to allocate reading buffer (%u Bytes)\n",
307 ntohl(isakmp
.len
) + extralen
);
309 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
310 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
311 plog(LLV_ERROR
, LOCATION
, NULL
,
312 "failed to receive isakmp packet: %s\n",
318 while ((len
= recvfromto(so_isakmp
, (char *)tmpbuf
->v
, tmpbuf
->l
,
319 0, (struct sockaddr
*)&remote
, &remote_len
,
320 (struct sockaddr
*)&local
, &local_len
)) < 0) {
323 plog(LLV_ERROR
, LOCATION
, NULL
,
324 "failed to receive isakmp packet: %s\n",
329 if ((buf
= vmalloc(len
- extralen
)) == NULL
) {
330 plog(LLV_ERROR
, LOCATION
, NULL
,
331 "failed to allocate reading buffer (%u Bytes)\n",
336 memcpy (buf
->v
, tmpbuf
->v
+ extralen
, buf
->l
);
341 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
342 "received invalid length (%d != %zu), why ?\n",
347 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
348 plog(LLV_DEBUG
, LOCATION
, NULL
,
349 "%d bytes message received %s\n",
350 len
, saddr2str_fromto("from %s to %s",
351 (struct sockaddr
*)&remote
,
352 (struct sockaddr
*)&local
));
353 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
355 /* avoid packets with malicious port/address */
356 if (extract_port((struct sockaddr
*)&remote
) == 0) {
357 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
358 "src port == 0 (valid as UDP but not with IKE)\n");
362 /* XXX: check sender whether to be allowed or not to accept */
364 /* XXX: I don't know how to check isakmp half connection attack. */
366 /* simply reply if the packet was processed. */
367 res
=check_recvdpkt((struct sockaddr
*)&remote
,(struct sockaddr
*)&local
, buf
);
369 plog(LLV_NOTIFY
, LOCATION
, NULL
,
370 "the packet is retransmitted by %s (%d).\n",
371 saddr2str((struct sockaddr
*)&remote
), res
);
376 /* isakmp main routine */
377 if (isakmp_main(buf
, (struct sockaddr
*)&remote
,
378 (struct sockaddr
*)&local
) != 0) goto end
;
391 * main processing to handle isakmp payload
394 isakmp_main(msg
, remote
, local
)
396 struct sockaddr
*remote
, *local
;
398 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
399 isakmp_index
*index
= (isakmp_index
*)isakmp
;
400 u_int32_t msgid
= isakmp
->msgid
;
401 struct ph1handle
*iph1
;
403 #ifdef HAVE_PRINT_ISAKMP_C
404 isakmp_printpacket(msg
, remote
, local
, 0);
407 /* the initiator's cookie must not be zero */
408 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
409 plog(LLV_ERROR
, LOCATION
, remote
,
410 "malformed cookie received.\n");
414 /* Check the Major and Minor Version fields. */
416 * XXX Is is right to check version here ?
417 * I think it may no be here because the version depends
418 * on exchange status.
420 if (isakmp
->v
< ISAKMP_VERSION_NUMBER
) {
421 if (ISAKMP_GETMAJORV(isakmp
->v
) < ISAKMP_MAJOR_VERSION
) {
422 plog(LLV_ERROR
, LOCATION
, remote
,
423 "invalid major version %d.\n",
424 ISAKMP_GETMAJORV(isakmp
->v
));
427 #if ISAKMP_MINOR_VERSION > 0
428 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) {
429 plog(LLV_ERROR
, LOCATION
, remote
,
430 "invalid minor version %d.\n",
431 ISAKMP_GETMINORV(isakmp
->v
));
437 /* check the Flags field. */
438 /* XXX How is the exclusive check, E and A ? */
439 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) {
440 plog(LLV_ERROR
, LOCATION
, remote
,
441 "invalid flag 0x%02x.\n", isakmp
->flags
);
445 /* ignore commit bit. */
446 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
447 if (isakmp
->msgid
== 0) {
448 isakmp_info_send_nx(isakmp
, remote
, local
,
449 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
450 plog(LLV_ERROR
, LOCATION
, remote
,
451 "Commit bit on phase1 forbidden.\n");
456 iph1
= getph1byindex(index
);
459 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
460 iph1
->side
== INITIATOR
) {
461 plog(LLV_DEBUG
, LOCATION
, remote
,
462 "malformed cookie received or "
463 "the initiator's cookies collide.\n");
468 /* Floating ports for NAT-T */
469 if (NATT_AVAILABLE(iph1
) &&
470 ! (iph1
->natt_flags
& NAT_PORTS_CHANGED
) &&
471 ((cmpsaddr(iph1
->remote
, remote
) != 0) ||
472 (cmpsaddr(iph1
->local
, local
) != 0)))
474 /* prevent memory leak */
475 racoon_free(iph1
->remote
);
476 racoon_free(iph1
->local
);
480 /* copy-in new addresses */
481 iph1
->remote
= dupsaddr(remote
);
482 if (iph1
->remote
== NULL
) {
483 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
484 "phase1 failed: dupsaddr failed.\n");
489 iph1
->local
= dupsaddr(local
);
490 if (iph1
->local
== NULL
) {
491 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
492 "phase1 failed: dupsaddr failed.\n");
498 /* set the flag to prevent further port floating
499 (FIXME: should we allow it? E.g. when the NAT gw
501 iph1
->natt_flags
|= NAT_PORTS_CHANGED
| NAT_ADD_NON_ESP_MARKER
;
503 /* print some neat info */
504 plog (LLV_INFO
, LOCATION
, NULL
,
505 "NAT-T: ports changed to: %s\n",
506 saddr2str_fromto ("%s<->%s", iph1
->remote
, iph1
->local
));
508 natt_keepalive_add_ph1 (iph1
);
512 /* must be same addresses in one stream of a phase at least. */
513 if (cmpsaddr(iph1
->remote
, remote
) != 0) {
514 char *saddr_db
, *saddr_act
;
516 saddr_db
= racoon_strdup(saddr2str(iph1
->remote
));
517 saddr_act
= racoon_strdup(saddr2str(remote
));
518 STRDUP_FATAL(saddr_db
);
519 STRDUP_FATAL(saddr_act
);
521 plog(LLV_WARNING
, LOCATION
, remote
,
522 "remote address mismatched. db=%s, act=%s\n",
523 saddr_db
, saddr_act
);
525 racoon_free(saddr_db
);
526 racoon_free(saddr_act
);
530 * don't check of exchange type here because other type will be
531 * with same index, for example, informational exchange.
534 /* XXX more acceptable check */
537 switch (isakmp
->etype
) {
538 case ISAKMP_ETYPE_IDENT
:
539 case ISAKMP_ETYPE_AGG
:
540 case ISAKMP_ETYPE_BASE
:
541 /* phase 1 validity check */
542 if (isakmp
->msgid
!= 0) {
543 plog(LLV_ERROR
, LOCATION
, remote
,
544 "message id should be zero in phase1.\n");
548 /* search for isakmp status record of phase 1 */
551 * the packet must be the 1st message from a initiator
552 * or the 2nd message from the responder.
555 /* search for phase1 handle by index without r_ck */
556 iph1
= getph1byindex0(index
);
558 /*it must be the 1st message from a initiator.*/
559 if (memcmp(&isakmp
->r_ck
, r_ck0
,
560 sizeof(cookie_t
)) != 0) {
562 plog(LLV_DEBUG
, LOCATION
, remote
,
563 "malformed cookie received "
564 "or the spi expired.\n");
568 /* it must be responder's 1st exchange. */
569 if (isakmp_ph1begin_r(msg
, remote
, local
,
577 /* it must be the 2nd message from the responder. */
578 if (iph1
->side
!= INITIATOR
) {
579 plog(LLV_DEBUG
, LOCATION
, remote
,
580 "malformed cookie received. "
581 "it has to be as the initiator. %s\n",
582 isakmp_pindex(&iph1
->index
, 0));
588 * Don't delete phase 1 handler when the exchange type
589 * in handler is not equal to packet's one because of no
590 * authencication completed.
592 if (iph1
->etype
!= isakmp
->etype
) {
593 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
594 "exchange type is mismatched: "
595 "db=%s packet=%s, ignore it.\n",
596 s_isakmp_etype(iph1
->etype
),
597 s_isakmp_etype(isakmp
->etype
));
602 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
603 return frag_handler(iph1
, msg
, remote
, local
);
606 /* call main process of phase 1 */
607 if (ph1_main(iph1
, msg
) < 0) {
608 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
609 "phase1 negotiation failed.\n");
616 case ISAKMP_ETYPE_AUTH
:
617 plog(LLV_INFO
, LOCATION
, remote
,
618 "unsupported exchange %d received.\n",
622 case ISAKMP_ETYPE_INFO
:
623 case ISAKMP_ETYPE_ACKINFO
:
625 * iph1 must be present for Information message.
626 * if iph1 is null then trying to get the phase1 status
627 * as the packet from responder againt initiator's 1st
628 * exchange in phase 1.
629 * NOTE: We think such informational exchange should be ignored.
632 iph1
= getph1byindex0(index
);
634 plog(LLV_ERROR
, LOCATION
, remote
,
635 "unknown Informational "
636 "exchange received.\n");
639 if (cmpsaddr(iph1
->remote
, remote
) != 0) {
640 plog(LLV_WARNING
, LOCATION
, remote
,
641 "remote address mismatched. "
643 saddr2str(iph1
->remote
));
648 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
649 return frag_handler(iph1
, msg
, remote
, local
);
652 if (isakmp_info_recv(iph1
, msg
) < 0)
656 case ISAKMP_ETYPE_QUICK
:
658 struct ph2handle
*iph2
;
661 isakmp_info_send_nx(isakmp
, remote
, local
,
662 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
663 plog(LLV_ERROR
, LOCATION
, remote
,
664 "can't start the quick mode, "
665 "there is no ISAKMP-SA, %s\n",
666 isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
671 /* Reinit the IVM if it's still there */
672 if (iph1
->mode_cfg
&& iph1
->mode_cfg
->ivm
) {
673 oakley_delivm(iph1
->mode_cfg
->ivm
);
674 iph1
->mode_cfg
->ivm
= NULL
;
678 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
679 return frag_handler(iph1
, msg
, remote
, local
);
682 /* check status of phase 1 whether negotiated or not. */
683 if (iph1
->status
!= PHASE1ST_ESTABLISHED
&&
684 iph1
->status
!= PHASE1ST_DYING
) {
685 plog(LLV_ERROR
, LOCATION
, remote
,
686 "can't start the quick mode, "
687 "there is no valid ISAKMP-SA, %s\n",
688 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
692 /* search isakmp phase 2 stauts record. */
693 iph2
= getph2bymsgid(iph1
, msgid
);
695 /* it must be new negotiation as responder */
696 if (isakmp_ph2begin_r(iph1
, msg
) < 0)
704 * we keep to set commit bit during negotiation.
705 * When SA is configured, bit will be reset.
707 * don't initiate commit bit. should be fixed in the future.
709 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
710 iph2
->flags
|= ISAKMP_FLAG_C
;
712 /* call main process of quick mode */
713 if (quick_main(iph2
, msg
) < 0) {
714 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
715 "phase2 negotiation failed.\n");
723 case ISAKMP_ETYPE_NEWGRP
:
725 plog(LLV_ERROR
, LOCATION
, remote
,
726 "Unknown new group mode exchange, "
727 "there is no ISAKMP-SA.\n");
732 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
733 return frag_handler(iph1
, msg
, remote
, local
);
736 isakmp_newgroup_r(iph1
, msg
);
740 case ISAKMP_ETYPE_CFG
:
742 plog(LLV_ERROR
, LOCATION
, NULL
,
743 "mode config %d from %s, "
744 "but we have no ISAKMP-SA.\n",
745 isakmp
->etype
, saddr2str(remote
));
750 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
751 return frag_handler(iph1
, msg
, remote
, local
);
754 isakmp_cfg_r(iph1
, msg
);
758 case ISAKMP_ETYPE_NONE
:
760 plog(LLV_ERROR
, LOCATION
, NULL
,
761 "Invalid exchange type %d from %s.\n",
762 isakmp
->etype
, saddr2str(remote
));
770 * main function of phase 1.
774 struct ph1handle
*iph1
;
779 struct timeval start
, end
;
782 /* ignore a packet */
783 if (iph1
->status
>= PHASE1ST_ESTABLISHED
)
787 gettimeofday(&start
, NULL
);
790 if (ph1exchange
[etypesw1(iph1
->etype
)]
792 [iph1
->status
] == NULL
) {
793 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
794 "why isn't the function defined.\n");
797 error
= (ph1exchange
[etypesw1(iph1
->etype
)]
799 [iph1
->status
])(iph1
, msg
);
803 * When an invalid packet is received on phase1, it should
804 * be selected to process this packet. That is to respond
805 * with a notify and delete phase 1 handler, OR not to respond
806 * and keep phase 1 handler. However, in PHASE1ST_START when
807 * acting as RESPONDER we must not keep phase 1 handler or else
808 * it will stay forever.
811 if (iph1
->side
== RESPONDER
&& iph1
->status
== PHASE1ST_START
) {
812 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
813 "failed to pre-process packet.\n");
816 /* ignore the error and keep phase 1 handler */
822 /* free resend buffer */
823 if (iph1
->sendbuf
== NULL
) {
824 plog(LLV_ERROR
, LOCATION
, NULL
,
825 "no buffer found as sendbuf\n");
830 VPTRINIT(iph1
->sendbuf
);
832 /* turn off schedule */
833 sched_cancel(&iph1
->scr
);
836 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
837 if ((ph1exchange
[etypesw1(iph1
->etype
)]
839 [iph1
->status
])(iph1
, msg
) != 0) {
840 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
841 "failed to process packet.\n");
846 gettimeofday(&end
, NULL
);
847 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
848 "phase1", s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
849 timedelta(&start
, &end
));
851 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
854 gettimeofday(&iph1
->end
, NULL
);
855 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
856 "phase1", s_isakmp_etype(iph1
->etype
),
857 timedelta(&iph1
->start
, &iph1
->end
));
860 /* save created date. */
861 (void)time(&iph1
->created
);
863 /* migrate ph2s from dying ph1s */
864 migrate_dying_ph12(iph1
);
866 /* add to the schedule to expire, and seve back pointer. */
867 if ((iph1
->rmconf
->rekey
== REKEY_FORCE
)
870 (iph1
->rmconf
->rekey
== REKEY_ON
&& iph1
->dpd_support
&&
871 iph1
->rmconf
->dpd_interval
)
874 sched_schedule(&iph1
->sce
,
875 iph1
->approval
->lifetime
*
876 PFKEY_SOFT_LIFETIME_RATE
/ 100,
877 isakmp_ph1dying_stub
);
879 sched_schedule(&iph1
->sce
, iph1
->approval
->lifetime
,
880 isakmp_ph1expire_stub
);
884 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
885 switch (iph1
->approval
->authmethod
) {
886 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
887 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
888 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
889 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
890 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
891 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
892 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
894 /* XXX Don't process INITIAL_CONTACT */
895 iph1
->rmconf
->ini_contact
= 0;
903 /* Schedule the r_u_there.... */
904 if(iph1
->dpd_support
&& iph1
->rmconf
->dpd_interval
)
905 isakmp_sched_r_u(iph1
, 0);
908 /* INITIAL-CONTACT processing */
909 /* don't anything if local test mode. */
911 && iph1
->rmconf
->ini_contact
&& !getcontacted(iph1
->remote
)) {
912 /* send INITIAL-CONTACT */
913 isakmp_info_send_n1(iph1
,
914 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
915 /* insert a node into contacted list. */
916 if (inscontacted(iph1
->remote
) == -1) {
917 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
918 "failed to add contacted list.\n");
922 if (iph1
->initial_contact_received
)
923 isakmp_info_recv_initialcontact(iph1
, NULL
);
925 log_ph1established(iph1
);
926 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
929 * SA up shell script hook: do it now,except if
930 * ISAKMP mode config was requested. In the later
931 * case it is done when we receive the configuration.
933 if ((iph1
->status
== PHASE1ST_ESTABLISHED
) &&
934 !iph1
->rmconf
->mode_cfg
) {
935 switch (iph1
->approval
->authmethod
) {
937 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
938 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
939 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
940 /* Unimplemeted... */
941 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
942 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
943 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
944 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
948 script_hook(iph1
, SCRIPT_PHASE1_UP
);
958 * main function of quick mode.
961 quick_main(iph2
, msg
)
962 struct ph2handle
*iph2
;
965 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
968 struct timeval start
, end
;
971 /* ignore a packet */
972 if (iph2
->status
== PHASE2ST_ESTABLISHED
973 || iph2
->status
== PHASE2ST_GETSPISENT
)
977 gettimeofday(&start
, NULL
);
981 if (ph2exchange
[etypesw2(isakmp
->etype
)]
983 [iph2
->status
] == NULL
) {
984 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
985 "why isn't the function defined.\n");
988 error
= (ph2exchange
[etypesw2(isakmp
->etype
)]
990 [iph2
->status
])(iph2
, msg
);
992 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
993 "failed to pre-process packet.\n");
994 if (error
== ISAKMP_INTERNAL_ERROR
)
996 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
1000 /* when using commit bit, status will be reached here. */
1001 if (iph2
->status
== PHASE2ST_ADDSA
)
1004 /* free resend buffer */
1005 if (iph2
->sendbuf
== NULL
) {
1006 plog(LLV_ERROR
, LOCATION
, NULL
,
1007 "no buffer found as sendbuf\n");
1010 VPTRINIT(iph2
->sendbuf
);
1012 /* turn off schedule */
1013 sched_cancel(&iph2
->scr
);
1016 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1017 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
1019 [iph2
->status
])(iph2
, msg
) != 0) {
1020 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1021 "failed to process packet.\n");
1026 gettimeofday(&end
, NULL
);
1027 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1029 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
1030 timedelta(&start
, &end
));
1036 /* new negotiation of phase 1 for initiator */
1038 isakmp_ph1begin_i(rmconf
, remote
, local
)
1039 struct remoteconf
*rmconf
;
1040 struct sockaddr
*remote
, *local
;
1042 struct ph1handle
*iph1
;
1044 struct timeval start
, end
;
1047 /* get new entry to isakmp status table. */
1052 iph1
->status
= PHASE1ST_START
;
1053 iph1
->rmconf
= rmconf
;
1054 iph1
->side
= INITIATOR
;
1055 iph1
->version
= ISAKMP_VERSION_NUMBER
;
1060 iph1
->gssapi_state
= NULL
;
1062 #ifdef ENABLE_HYBRID
1063 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
1070 if(rmconf
->ike_frag
== ISAKMP_FRAG_FORCE
)
1074 iph1
->frag_chain
= NULL
;
1076 iph1
->approval
= NULL
;
1078 /* XXX copy remote address */
1079 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
1086 /* start phase 1 exchange */
1087 iph1
->etype
= rmconf
->etypes
->type
;
1089 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1093 a
= racoon_strdup(saddr2str(iph1
->local
));
1096 plog(LLV_INFO
, LOCATION
, NULL
,
1097 "initiate new phase 1 negotiation: %s<=>%s\n",
1098 a
, saddr2str(iph1
->remote
));
1101 plog(LLV_INFO
, LOCATION
, NULL
,
1103 s_isakmp_etype(iph1
->etype
));
1106 gettimeofday(&iph1
->start
, NULL
);
1107 gettimeofday(&start
, NULL
);
1109 /* start exchange */
1110 if ((ph1exchange
[etypesw1(iph1
->etype
)]
1112 [iph1
->status
])(iph1
, NULL
) != 0) {
1113 /* failed to start phase 1 negotiation */
1121 gettimeofday(&end
, NULL
);
1122 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1124 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
1125 timedelta(&start
, &end
));
1131 /* new negotiation of phase 1 for responder */
1133 isakmp_ph1begin_r(msg
, remote
, local
, etype
)
1135 struct sockaddr
*remote
, *local
;
1138 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1139 struct ph1handle
*iph1
;
1140 struct rmconfselector rmsel
;
1142 struct timeval start
, end
;
1145 /* check if this etype is allowed */
1146 memset(&rmsel
, 0, sizeof(rmsel
));
1147 rmsel
.remote
= remote
;
1148 if (enumrmconf(&rmsel
, check_etypeok
, (void *) (intptr_t) etype
) == 0) {
1149 plog(LLV_ERROR
, LOCATION
, remote
,
1150 "exchange %s not allowed in any applicable rmconf.\n",
1151 s_isakmp_etype(etype
));
1155 /* get new entry to isakmp status table. */
1160 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
1161 iph1
->status
= PHASE1ST_START
;
1163 iph1
->side
= RESPONDER
;
1164 iph1
->etype
= etype
;
1165 iph1
->version
= isakmp
->v
;
1168 iph1
->gssapi_state
= NULL
;
1170 #ifdef ENABLE_HYBRID
1171 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
1178 iph1
->frag_chain
= NULL
;
1180 iph1
->approval
= NULL
;
1183 /* RFC3947 says that we MUST accept new phases1 on NAT-T floated port.
1184 * We have to setup this flag now to correctly generate the first reply.
1185 * Don't know if a better check could be done for that ?
1187 if(extract_port(local
) == lcconf
->port_isakmp_natt
)
1188 iph1
->natt_flags
|= (NAT_PORTS_CHANGED
);
1191 /* copy remote address; remote and local always contain
1192 * port numbers so rmconf is not needed */
1193 if (copy_ph1addresses(iph1
, NULL
, remote
, local
) < 0) {
1199 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1203 a
= racoon_strdup(saddr2str(iph1
->local
));
1206 plog(LLV_INFO
, LOCATION
, NULL
,
1207 "respond new phase 1 negotiation: %s<=>%s\n",
1208 a
, saddr2str(iph1
->remote
));
1211 plog(LLV_INFO
, LOCATION
, NULL
,
1212 "begin %s mode.\n", s_isakmp_etype(etype
));
1215 gettimeofday(&iph1
->start
, NULL
);
1216 gettimeofday(&start
, NULL
);
1221 /* start exchange */
1222 if ((ph1exchange
[etypesw1(iph1
->etype
)]
1224 [iph1
->status
])(iph1
, msg
) < 0
1225 || (ph1exchange
[etypesw1(iph1
->etype
)]
1227 [iph1
->status
])(iph1
, msg
) < 0) {
1228 plog(LLV_ERROR
, LOCATION
, remote
,
1229 "failed to process packet.\n");
1236 gettimeofday(&end
, NULL
);
1237 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1239 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
1240 timedelta(&start
, &end
));
1245 #else /* ENABLE_FRAG */
1247 /* now that we have a phase1 handle, feed back into our
1248 * main receive function to catch fragmented packets
1251 return isakmp_main(msg
, remote
, local
);
1253 #endif /* ENABLE_FRAG */
1257 /* new negotiation of phase 2 for initiator */
1259 isakmp_ph2begin_i(iph1
, iph2
)
1260 struct ph1handle
*iph1
;
1261 struct ph2handle
*iph2
;
1263 #ifdef ENABLE_HYBRID
1264 if (xauth_check(iph1
) != 0) {
1265 plog(LLV_ERROR
, LOCATION
, NULL
,
1266 "Attempt to start phase 2 whereas Xauth failed\n");
1271 /* fixup ph2 ports for this ph1 */
1272 if (extract_port(iph2
->src
) == 0)
1273 set_port(iph2
->src
, extract_port(iph1
->local
));
1274 if (extract_port(iph2
->dst
) == 0)
1275 set_port(iph2
->dst
, extract_port(iph1
->remote
));
1277 /* found ISAKMP-SA. */
1278 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1279 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
1282 a
= racoon_strdup(saddr2str(iph2
->src
));
1285 plog(LLV_INFO
, LOCATION
, NULL
,
1286 "initiate new phase 2 negotiation: %s<=>%s\n",
1287 a
, saddr2str(iph2
->dst
));
1292 gettimeofday(&iph2
->start
, NULL
);
1294 if (iph2
->status
!= PHASE2ST_EXPIRED
) /* Phase 1 is already bound (ongoing rekeying) */
1295 bindph12(iph1
, iph2
);
1296 iph2
->status
= PHASE2ST_STATUS2
;
1298 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1300 [iph2
->status
])(iph2
, NULL
) < 0) {
1301 /* release ipsecsa handler due to internal error. */
1308 /* new negotiation of phase 2 for responder */
1310 isakmp_ph2begin_r(iph1
, msg
)
1311 struct ph1handle
*iph1
;
1314 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1315 struct ph2handle
*iph2
= 0;
1318 struct timeval start
, end
;
1320 #ifdef ENABLE_HYBRID
1321 if (xauth_check(iph1
) != 0) {
1322 plog(LLV_ERROR
, LOCATION
, NULL
,
1323 "Attempt to start phase 2 whereas Xauth failed\n");
1330 plog(LLV_ERROR
, LOCATION
, NULL
,
1331 "failed to allocate phase2 entry.\n");
1335 iph2
->side
= RESPONDER
;
1336 iph2
->status
= PHASE2ST_START
;
1337 iph2
->flags
= isakmp
->flags
;
1338 iph2
->msgid
= isakmp
->msgid
;
1339 iph2
->seq
= pk_getseq();
1340 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1341 if (iph2
->ivm
== NULL
) {
1345 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
1346 if (iph2
->dst
== NULL
) {
1350 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1351 if (iph2
->src
== NULL
) {
1356 /* add new entry to isakmp status table */
1358 bindph12(iph1
, iph2
);
1360 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1364 a
= racoon_strdup(saddr2str(iph2
->src
));
1367 plog(LLV_INFO
, LOCATION
, NULL
,
1368 "respond new phase 2 negotiation: %s<=>%s\n",
1369 a
, saddr2str(iph2
->dst
));
1374 gettimeofday(&start
, NULL
);
1377 error
= (ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1379 [iph2
->status
])(iph2
, msg
);
1381 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1382 "failed to pre-process packet.\n");
1383 if (error
!= ISAKMP_INTERNAL_ERROR
)
1384 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
1386 * release handler because it's wrong that ph2handle is kept
1387 * after failed to check message for responder's.
1395 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1396 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
1398 [iph2
->status
])(iph2
, msg
) < 0) {
1399 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1400 "failed to process packet.\n");
1401 /* don't release handler */
1405 gettimeofday(&end
, NULL
);
1406 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1408 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
1409 timedelta(&start
, &end
));
1416 * parse ISAKMP payloads, without ISAKMP base header.
1419 isakmp_parsewoh(np0
, gen
, len
)
1421 struct isakmp_gen
*gen
;
1424 u_char np
= np0
& 0xff;
1427 struct isakmp_parse_t
*p
, *ep
;
1429 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
1432 * 5 is a magic number, but any value larger than 2 should be fine
1433 * as we do vrealloc() in the following loop.
1435 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1436 if (result
== NULL
) {
1437 plog(LLV_ERROR
, LOCATION
, NULL
,
1438 "failed to get buffer.\n");
1441 p
= (struct isakmp_parse_t
*)result
->v
;
1442 ep
= (struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1446 /* parse through general headers */
1447 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1448 if (tlen
<= sizeof(struct isakmp_gen
)) {
1449 /* don't send information, see isakmp_ident_r1() */
1450 plog(LLV_ERROR
, LOCATION
, NULL
,
1451 "invalid length of payload\n");
1456 plog(LLV_DEBUG
, LOCATION
, NULL
,
1457 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1460 p
->len
= ntohs(gen
->len
);
1461 if (p
->len
< sizeof(struct isakmp_gen
) || p
->len
> tlen
) {
1462 plog(LLV_DEBUG
, LOCATION
, NULL
,
1463 "invalid length of payload\n");
1472 off
= p
- (struct isakmp_parse_t
*)result
->v
;
1473 result
= vrealloc(result
, result
->l
* 2);
1474 if (result
== NULL
) {
1475 plog(LLV_DEBUG
, LOCATION
, NULL
,
1476 "failed to realloc buffer.\n");
1480 ep
= (struct isakmp_parse_t
*)
1481 (result
->v
+ result
->l
- sizeof(*ep
));
1482 p
= (struct isakmp_parse_t
*)result
->v
;
1487 plen
= ntohs(gen
->len
);
1488 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1491 p
->type
= ISAKMP_NPTYPE_NONE
;
1495 plog(LLV_DEBUG
, LOCATION
, NULL
, "succeed.\n");
1501 * parse ISAKMP payloads, including ISAKMP base header.
1507 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1508 struct isakmp_gen
*gen
;
1514 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1515 tlen
= buf
->l
- sizeof(struct isakmp
);
1516 result
= isakmp_parsewoh(np
, gen
, tlen
);
1525 /* initialize a isakmp status table */
1535 * make strings containing i_cookie + r_cookie + msgid
1538 isakmp_pindex(index
, msgid
)
1539 const isakmp_index
*index
;
1540 const u_int32_t msgid
;
1542 static char buf
[64];
1546 memset(buf
, 0, sizeof(buf
));
1549 p
= (const u_char
*)index
;
1550 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1551 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1563 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1568 /* open ISAKMP sockets. */
1570 isakmp_open(struct sockaddr
*addr
, int udp_encap
)
1573 int ifnum
= 0, encap_ifnum
= 0, fd
;
1574 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
1576 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
1583 /* warn if wildcard address - should we forbid this? */
1584 switch (addr
->sa_family
) {
1586 if (sin
->sin_addr
.s_addr
== 0)
1587 plog(LLV_WARNING
, LOCATION
, NULL
,
1588 "listening to wildcard address,"
1589 "broadcast IKE packet may kill you\n");
1593 if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
1594 plog(LLV_DEBUG
, LOCATION
, NULL
,
1595 "ignoring multicast address %s\n",
1600 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
1601 plog(LLV_WARNING
, LOCATION
, NULL
,
1602 "listening to wildcard address, "
1603 "broadcast IKE packet may kill you\n");
1607 plog(LLV_ERROR
, LOCATION
, NULL
,
1608 "unsupported address family %d\n",
1613 if ((fd
= privsep_socket(addr
->sa_family
, SOCK_DGRAM
, 0)) < 0) {
1614 plog(LLV_ERROR
, LOCATION
, NULL
,
1615 "socket(%s)\n", strerror(errno
));
1619 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
) == -1)
1620 plog(LLV_WARNING
, LOCATION
, NULL
,
1621 "failed to put socket in non-blocking mode\n");
1623 /* receive my interface address on inbound packets. */
1624 switch (addr
->sa_family
) {
1626 if (setsockopt(fd
, IPPROTO_IP
,
1632 (const void *) &yes
, sizeof(yes
)) < 0) {
1633 plog(LLV_ERROR
, LOCATION
, NULL
,
1634 "setsockopt IP_RECVDSTADDR (%s)\n",
1641 option
= UDP_ENCAP_ESPINUDP
;
1642 #if defined(ENABLE_NATT_00) || defined(ENABLE_NATT_01)
1644 option
= UDP_ENCAP_ESPINUDP_NON_IKE
;
1649 if (setsockopt(fd
, SOL_UDP
,
1651 sizeof(option
)) < 0) {
1652 plog(LLV_WARNING
, LOCATION
, NULL
,
1653 "setsockopt(%s): UDP_ENCAP %s\n",
1654 option
== UDP_ENCAP_ESPINUDP
? "UDP_ENCAP_ESPINUDP" : "UDP_ENCAP_ESPINUDP_NON_IKE",
1657 plog(LLV_INFO
, LOCATION
, NULL
,
1658 "%s used for NAT-T\n",
1666 #if defined(INET6_ADVAPI)
1667 #ifdef IPV6_RECVPKTINFO
1668 pktinfo
= IPV6_RECVPKTINFO
;
1669 #else /* old adv. API */
1670 pktinfo
= IPV6_PKTINFO
;
1671 #endif /* IPV6_RECVPKTINFO */
1673 pktinfo
= IPV6_RECVDSTADDR
;
1675 if (setsockopt(fd
, IPPROTO_IPV6
, pktinfo
,
1676 (const void *) &yes
, sizeof(yes
)) < 0) {
1677 plog(LLV_ERROR
, LOCATION
, NULL
,
1678 "setsockopt IPV6_RECVDSTADDR (%d):%s\n",
1679 pktinfo
, strerror(errno
));
1683 #ifdef IPV6_USE_MIN_MTU
1684 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1685 (void *) &yes
, sizeof(yes
)) < 0) {
1686 plog(LLV_ERROR
, LOCATION
, NULL
,
1687 "setsockopt IPV6_USE_MIN_MTU (%s)\n",
1696 if (setsockopt(fd
, SOL_SOCKET
,
1702 (void *) &yes
, sizeof(yes
)) < 0) {
1703 plog(LLV_ERROR
, LOCATION
, NULL
,
1704 "failed to set REUSE flag on %s (%s).\n",
1705 saddr2str(addr
), strerror(errno
));
1709 if (setsockopt_bypass(fd
, addr
->sa_family
) < 0)
1712 if (privsep_bind(fd
, addr
, sysdep_sa_len(addr
)) < 0) {
1713 plog(LLV_ERROR
, LOCATION
, addr
,
1714 "failed to bind to address %s (%s).\n",
1715 saddr2str(addr
), strerror(errno
));
1719 plog(LLV_INFO
, LOCATION
, NULL
,
1720 "%s used as isakmp port (fd=%d)\n",
1721 saddr2str(addr
), fd
);
1723 monitor_fd(fd
, isakmp_handler
, NULL
);
1732 isakmp_close(int fd
)
1739 isakmp_send(iph1
, sbuf
)
1740 struct ph1handle
*iph1
;
1745 vchar_t
*vbuf
= NULL
, swap
;
1748 size_t extralen
= NON_ESP_MARKER_USE(iph1
) ? NON_ESP_MARKER_LEN
: 0;
1750 /* Check if NON_ESP_MARKER_LEN is already there (happens when resending packets)
1752 if(extralen
== NON_ESP_MARKER_LEN
&&
1753 *(u_int32_t
*)sbuf
->v
== 0)
1758 * Do not add the non ESP marker for a packet that will
1759 * be fragmented. The non ESP marker should appear in
1760 * all fragment's packets, but not in the fragmented packet
1762 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
)
1766 plog (LLV_DEBUG
, LOCATION
, NULL
, "Adding NON-ESP marker\n");
1768 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1769 must added just before the packet itself. For this we must
1770 allocate a new buffer and release it at the end. */
1772 if ((vbuf
= vmalloc (sbuf
->l
+ extralen
)) == NULL
) {
1773 plog(LLV_ERROR
, LOCATION
, NULL
,
1774 "vbuf allocation failed\n");
1777 *(u_int32_t
*)vbuf
->v
= 0;
1778 memcpy (vbuf
->v
+ extralen
, sbuf
->v
, sbuf
->l
);
1779 /* ensures that the modified buffer will be sent back to the caller, so
1780 * add_recvdpkt() will add the correct buffer
1789 /* select the socket to be sent */
1790 s
= myaddr_getfd(iph1
->local
);
1794 plog (LLV_DEBUG
, LOCATION
, NULL
, "%zu bytes %s\n", sbuf
->l
,
1795 saddr2str_fromto("from %s to %s", iph1
->local
, iph1
->remote
));
1798 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1799 if (isakmp_sendfrags(iph1
, sbuf
) == -1) {
1800 plog(LLV_ERROR
, LOCATION
, NULL
,
1801 "isakmp_sendfrags failed\n");
1807 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1808 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1811 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto failed\n");
1819 /* called from scheduler */
1821 isakmp_ph1resend_stub(p
)
1824 struct ph1handle
*iph1
= container_of(p
, struct ph1handle
, scr
);
1826 if (isakmp_ph1resend(iph1
) < 0) {
1833 isakmp_ph1resend(iph1
)
1834 struct ph1handle
*iph1
;
1836 /* Note: NEVER do the rem/del here, it will be done by the caller or by the _stub function
1838 if (iph1
->retry_counter
<= 0) {
1839 plog(LLV_ERROR
, LOCATION
, NULL
,
1840 "phase1 negotiation failed due to time up. %s\n",
1841 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1842 evt_phase1(iph1
, EVT_PHASE1_NO_RESPONSE
, NULL
);
1847 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0){
1848 plog(LLV_ERROR
, LOCATION
, NULL
,
1849 "phase1 negotiation failed due to send error. %s\n",
1850 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1851 evt_phase1(iph1
, EVT_PHASE1_NO_RESPONSE
, NULL
);
1855 plog(LLV_DEBUG
, LOCATION
, NULL
,
1856 "resend phase1 packet %s\n",
1857 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1859 iph1
->retry_counter
--;
1861 sched_schedule(&iph1
->scr
, lcconf
->retry_interval
,
1862 isakmp_ph1resend_stub
);
1868 isakmp_ph1send(iph1
)
1869 struct ph1handle
*iph1
;
1871 iph1
->retry_counter
= lcconf
->retry_counter
;
1872 return isakmp_ph1resend(iph1
);
1875 /* called from scheduler */
1877 isakmp_ph2resend_stub(p
)
1880 struct ph2handle
*iph2
= container_of(p
, struct ph2handle
, scr
);
1882 if (isakmp_ph2resend(iph2
) < 0) {
1889 isakmp_ph2resend(iph2
)
1890 struct ph2handle
*iph2
;
1892 /* Note: NEVER do the unbind/rem/del here, it will be done by the caller or by the _stub function
1894 if (iph2
->ph1
->status
>= PHASE1ST_EXPIRED
) {
1895 plog(LLV_ERROR
, LOCATION
, NULL
,
1896 "phase2 negotiation failed due to phase1 expired. %s\n",
1897 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1901 if (iph2
->retry_counter
<= 0) {
1902 plog(LLV_ERROR
, LOCATION
, NULL
,
1903 "phase2 negotiation failed due to time up. %s\n",
1904 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1905 evt_phase2(iph2
, EVT_PHASE2_NO_RESPONSE
, NULL
);
1910 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0){
1911 plog(LLV_ERROR
, LOCATION
, NULL
,
1912 "phase2 negotiation failed due to send error. %s\n",
1913 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1914 evt_phase2(iph2
, EVT_PHASE2_NO_RESPONSE
, NULL
);
1918 plog(LLV_DEBUG
, LOCATION
, NULL
,
1919 "resend phase2 packet %s\n",
1920 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1922 iph2
->retry_counter
--;
1924 sched_schedule(&iph2
->scr
, lcconf
->retry_interval
,
1925 isakmp_ph2resend_stub
);
1931 isakmp_ph2send(iph2
)
1932 struct ph2handle
*iph2
;
1934 iph2
->retry_counter
= lcconf
->retry_counter
;
1935 return isakmp_ph2resend(iph2
);
1938 /* called from scheduler */
1940 isakmp_ph1dying_stub(p
)
1944 isakmp_ph1dying(container_of(p
, struct ph1handle
, sce
));
1948 isakmp_ph1dying(iph1
)
1949 struct ph1handle
*iph1
;
1951 struct ph1handle
*new_iph1
;
1952 struct ph2handle
*p
;
1953 struct remoteconf
*rmconf
;
1955 if (iph1
->status
>= PHASE1ST_DYING
)
1958 /* Going away in after a while... */
1959 iph1
->status
= PHASE1ST_DYING
;
1961 /* Any fresh phase1s? */
1962 new_iph1
= getph1(iph1
, iph1
->local
, iph1
->remote
, 1);
1963 if (new_iph1
== NULL
) {
1964 LIST_FOREACH(p
, &iph1
->ph2tree
, ph1bind
) {
1965 if (p
->status
!= PHASE2ST_ESTABLISHED
)
1968 plog(LLV_INFO
, LOCATION
, NULL
,
1969 "renegotiating phase1 to %s due to "
1971 saddrwop2str(iph1
->remote
));
1973 if (iph1
->side
== INITIATOR
)
1974 isakmp_ph1begin_i(iph1
->rmconf
, iph1
->remote
,
1980 migrate_ph12(iph1
, new_iph1
);
1983 /* Schedule for expiration */
1984 sched_schedule(&iph1
->sce
, iph1
->approval
->lifetime
*
1985 (100 - PFKEY_SOFT_LIFETIME_RATE
) / 100,
1986 isakmp_ph1expire_stub
);
1989 /* called from scheduler */
1991 isakmp_ph1expire_stub(p
)
1994 isakmp_ph1expire(container_of(p
, struct ph1handle
, sce
));
1998 isakmp_ph1expire(iph1
)
1999 struct ph1handle
*iph1
;
2003 if (iph1
->status
< PHASE1ST_EXPIRED
) {
2004 src
= racoon_strdup(saddr2str(iph1
->local
));
2005 dst
= racoon_strdup(saddr2str(iph1
->remote
));
2009 plog(LLV_INFO
, LOCATION
, NULL
,
2010 "ISAKMP-SA expired %s-%s spi:%s\n",
2012 isakmp_pindex(&iph1
->index
, 0));
2015 iph1
->status
= PHASE1ST_EXPIRED
;
2018 sched_schedule(&iph1
->sce
, 1, isakmp_ph1delete_stub
);
2021 /* called from scheduler */
2023 isakmp_ph1delete_stub(p
)
2027 isakmp_ph1delete(container_of(p
, struct ph1handle
, sce
));
2031 isakmp_ph1delete(iph1
)
2032 struct ph1handle
*iph1
;
2034 struct ph2handle
*p
, *next
;
2035 struct ph1handle
*new_iph1
;
2038 /* Migrate established phase2s. Any fresh phase1s? */
2039 new_iph1
= getph1(iph1
, iph1
->local
, iph1
->remote
, 1);
2040 if (new_iph1
!= NULL
)
2041 migrate_ph12(iph1
, new_iph1
);
2043 /* Discard any left phase2s */
2044 for (p
= LIST_FIRST(&iph1
->ph2tree
); p
; p
= next
) {
2045 next
= LIST_NEXT(p
, ph1bind
);
2046 if (p
->status
>= PHASE2ST_ESTABLISHED
)
2050 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
2051 sched_schedule(&iph1
->sce
, 1, isakmp_ph1delete_stub
);
2055 /* don't re-negosiation when the phase 1 SA expires. */
2057 src
= racoon_strdup(saddr2str(iph1
->local
));
2058 dst
= racoon_strdup(saddr2str(iph1
->remote
));
2062 plog(LLV_INFO
, LOCATION
, NULL
,
2063 "ISAKMP-SA deleted %s-%s spi:%s\n",
2064 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
2065 evt_phase1(iph1
, EVT_PHASE1_DOWN
, NULL
);
2073 /* called from scheduler.
2074 * this function will call only isakmp_ph2delete().
2075 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
2076 * by something cause. That's why this function is called after phase 2 SA
2077 * expires in the userland.
2080 isakmp_ph2expire_stub(p
)
2084 isakmp_ph2expire(container_of(p
, struct ph2handle
, sce
));
2088 isakmp_ph2expire(iph2
)
2089 struct ph2handle
*iph2
;
2093 src
= racoon_strdup(saddrwop2str(iph2
->src
));
2094 dst
= racoon_strdup(saddrwop2str(iph2
->dst
));
2098 plog(LLV_INFO
, LOCATION
, NULL
,
2099 "phase2 sa expired %s-%s\n", src
, dst
);
2103 iph2
->status
= PHASE2ST_EXPIRED
;
2104 sched_schedule(&iph2
->sce
, 1, isakmp_ph2delete_stub
);
2107 /* called from scheduler */
2109 isakmp_ph2delete_stub(p
)
2113 isakmp_ph2delete(container_of(p
, struct ph2handle
, sce
));
2117 isakmp_ph2delete(iph2
)
2118 struct ph2handle
*iph2
;
2122 src
= racoon_strdup(saddrwop2str(iph2
->src
));
2123 dst
= racoon_strdup(saddrwop2str(iph2
->dst
));
2127 plog(LLV_INFO
, LOCATION
, NULL
,
2128 "phase2 sa deleted %s-%s\n", src
, dst
);
2139 * Interface between PF_KEYv2 and ISAKMP
2142 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
2143 * if phase1 has been finished, begin phase2.
2146 isakmp_post_acquire(iph2
, iph1hint
)
2147 struct ph2handle
*iph2
;
2148 struct ph1handle
*iph1hint
;
2150 struct remoteconf
*rmconf
;
2151 struct ph1handle
*iph1
= NULL
;
2153 plog(LLV_DEBUG
, LOCATION
, NULL
, "in post_acquire\n");
2155 /* Search appropriate configuration with masking port. Note that
2156 * we always use iph2->dst, and not iph2->sa_dst.
2158 * XXX One possible need for using iph2->sa_dst if not NULL would
2159 * be for selecting a remote configuration based on a stable
2160 * address of a mobile node (not a CoA provided by MIGRATE/KMADDRESS
2161 * as iph2->dst hint). This scenario would require additional changes,
2162 * so no need to bother yet. --arno */
2164 if (iph1hint
== NULL
|| iph1hint
->rmconf
== NULL
) {
2165 rmconf
= getrmconf(iph2
->dst
, GETRMCONF_F_NO_PASSIVE
);
2166 if (rmconf
== NULL
) {
2167 plog(LLV_ERROR
, LOCATION
, NULL
,
2168 "no configuration found for %s.\n",
2169 saddrwop2str(iph2
->dst
));
2173 rmconf
= iph1hint
->rmconf
;
2176 /* if passive mode, ignore the acquire message */
2177 if (rmconf
->passive
) {
2178 plog(LLV_DEBUG
, LOCATION
, NULL
,
2179 "because of passive mode, "
2180 "ignore the acquire message for %s.\n",
2181 saddrwop2str(iph2
->dst
));
2186 * XXX Searching by IP addresses + ports might fail on
2187 * some cases, we should use the ISAKMP identity to search
2190 iph1
= getph1(iph1hint
, iph2
->src
, iph2
->dst
, 0);
2192 /* no ISAKMP-SA found. */
2194 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2195 sched_schedule(&iph2
->sce
, 1, isakmp_chkph1there_stub
);
2196 plog(LLV_INFO
, LOCATION
, NULL
,
2197 "IPsec-SA request for %s queued "
2198 "due to no phase1 found.\n",
2199 saddrwop2str(iph2
->dst
));
2201 /* start phase 1 negotiation as a initiator. */
2202 if (isakmp_ph1begin_i(rmconf
, iph2
->dst
, iph2
->src
) == NULL
) {
2203 sched_cancel(&iph2
->sce
);
2211 /* found ISAKMP-SA, but on negotiation. */
2212 if (iph1
->status
< PHASE1ST_ESTABLISHED
) {
2213 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2214 sched_schedule(&iph2
->sce
, 1, isakmp_chkph1there_stub
);
2215 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
2216 "request for establishing IPsec-SA was queued "
2217 "due to no phase1 found.\n");
2222 /* found established ISAKMP-SA */
2223 /* i.e. iph1->status == PHASE1ST_ESTABLISHED */
2225 /* found ISAKMP-SA. */
2226 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
2228 /* begin quick mode */
2229 if (isakmp_ph2begin_i(iph1
, iph2
))
2236 isakmp_get_sainfo(iph2
, sp_out
, sp_in
)
2237 struct ph2handle
*iph2
;
2238 struct secpolicy
*sp_out
, *sp_in
;
2240 struct remoteconf
*conf
;
2243 plog(LLV_DEBUG
, LOCATION
, NULL
,
2244 "new acquire %s\n", spidx2str(&sp_out
->spidx
));
2248 vchar_t
*idsrc
, *iddst
;
2250 idsrc
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.src
,
2251 sp_out
->spidx
.prefs
, sp_out
->spidx
.ul_proto
);
2252 if (idsrc
== NULL
) {
2253 plog(LLV_ERROR
, LOCATION
, NULL
,
2254 "failed to get ID for %s\n",
2255 spidx2str(&sp_out
->spidx
));
2258 iddst
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.dst
,
2259 sp_out
->spidx
.prefd
, sp_out
->spidx
.ul_proto
);
2260 if (iddst
== NULL
) {
2261 plog(LLV_ERROR
, LOCATION
, NULL
,
2262 "failed to get ID for %s\n",
2263 spidx2str(&sp_out
->spidx
));
2268 conf
= getrmconf(iph2
->dst
, 0);
2270 remoteid
= conf
->ph1id
;
2272 plog(LLV_DEBUG
, LOCATION
, NULL
, "Warning: no valid rmconf !\n");
2274 iph2
->sainfo
= getsainfo(idsrc
, iddst
, NULL
, NULL
, remoteid
);
2277 if (iph2
->sainfo
== NULL
) {
2278 plog(LLV_ERROR
, LOCATION
, NULL
,
2279 "failed to get sainfo.\n");
2281 /* XXX should use the algorithm list from register message */
2284 plog(LLV_DEBUG
, LOCATION
, NULL
,
2285 "selected sainfo: %s\n", sainfo2str(iph2
->sainfo
));
2288 if (set_proposal_from_policy(iph2
, sp_out
, sp_in
) < 0) {
2289 plog(LLV_ERROR
, LOCATION
, NULL
,
2290 "failed to create saprop.\n");
2299 * receive GETSPI from kernel.
2302 isakmp_post_getspi(iph2
)
2303 struct ph2handle
*iph2
;
2306 struct timeval start
, end
;
2309 /* don't process it because there is no suitable phase1-sa. */
2310 if (iph2
->ph1
->status
>= PHASE1ST_EXPIRED
) {
2311 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
2312 "the negotiation is stopped, "
2313 "because there is no suitable ISAKMP-SA.\n");
2318 gettimeofday(&start
, NULL
);
2320 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
2322 [iph2
->status
])(iph2
, NULL
) != 0)
2325 gettimeofday(&end
, NULL
);
2326 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
2328 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2329 timedelta(&start
, &end
));
2335 /* called by scheduler */
2337 isakmp_chkph1there_stub(p
)
2340 isakmp_chkph1there(container_of(p
, struct ph2handle
, sce
));
2344 isakmp_chkph1there(iph2
)
2345 struct ph2handle
*iph2
;
2347 struct ph1handle
*iph1
;
2349 iph2
->retry_checkph1
--;
2350 if (iph2
->retry_checkph1
< 0) {
2351 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
2352 "phase2 negotiation failed "
2353 "due to time up waiting for phase1. %s\n",
2354 sadbsecas2str(iph2
->dst
, iph2
->src
,
2355 iph2
->satype
, 0, 0));
2356 plog(LLV_INFO
, LOCATION
, NULL
,
2357 "delete phase 2 handler.\n");
2359 /* send acquire to kernel as error */
2360 pk_sendeacquire(iph2
);
2368 /* Search isakmp status table by address and port */
2369 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
, 0);
2371 /* XXX Even if ph1 as responder is there, should we not start
2372 * phase 2 negotiation ? */
2374 && iph1
->status
== PHASE1ST_ESTABLISHED
) {
2375 /* found isakmp-sa */
2377 plog(LLV_DEBUG2
, LOCATION
, NULL
, "CHKPH1THERE: got a ph1 handler, setting ports.\n");
2378 plog(LLV_DEBUG2
, LOCATION
, NULL
, "iph1->local: %s\n", saddr2str(iph1
->local
));
2379 plog(LLV_DEBUG2
, LOCATION
, NULL
, "iph1->remote: %s\n", saddr2str(iph1
->remote
));
2380 plog(LLV_DEBUG2
, LOCATION
, NULL
, "before:\n");
2381 plog(LLV_DEBUG2
, LOCATION
, NULL
, "src: %s\n", saddr2str(iph2
->src
));
2382 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dst: %s\n", saddr2str(iph2
->dst
));
2383 set_port(iph2
->src
, extract_port(iph1
->local
));
2384 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2385 plog(LLV_DEBUG2
, LOCATION
, NULL
, "After:\n");
2386 plog(LLV_DEBUG2
, LOCATION
, NULL
, "src: %s\n", saddr2str(iph2
->src
));
2387 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dst: %s\n", saddr2str(iph2
->dst
));
2389 /* begin quick mode */
2390 (void)isakmp_ph2begin_i(iph1
, iph2
);
2394 plog(LLV_DEBUG2
, LOCATION
, NULL
, "CHKPH1THERE: no established ph1 handler found\n");
2396 /* no isakmp-sa found */
2397 sched_schedule(&iph2
->sce
, 1, isakmp_chkph1there_stub
);
2402 /* copy variable data into ALLOCATED buffer. */
2404 isakmp_set_attr_v(buf
, type
, val
, len
)
2410 struct isakmp_data
*data
;
2412 data
= (struct isakmp_data
*)buf
;
2413 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2414 data
->lorv
= htons((u_int16_t
)len
);
2415 memcpy(data
+ 1, val
, len
);
2417 return buf
+ sizeof(*data
) + len
;
2420 /* copy fixed length data into ALLOCATED buffer. */
2422 isakmp_set_attr_l(buf
, type
, val
)
2427 struct isakmp_data
*data
;
2429 data
= (struct isakmp_data
*)buf
;
2430 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2431 data
->lorv
= htons((u_int16_t
)val
);
2433 return buf
+ sizeof(*data
);
2436 /* add a variable data attribute to the buffer by reallocating it. */
2438 isakmp_add_attr_v(buf0
, type
, val
, len
)
2444 vchar_t
*buf
= NULL
;
2445 struct isakmp_data
*data
;
2449 tlen
= sizeof(*data
) + len
;
2453 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2455 buf
= vmalloc(tlen
);
2457 plog(LLV_ERROR
, LOCATION
, NULL
,
2458 "failed to get a attribute buffer.\n");
2462 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2463 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2464 data
->lorv
= htons((u_int16_t
)len
);
2465 memcpy(data
+ 1, val
, len
);
2470 /* add a fixed data attribute to the buffer by reallocating it. */
2472 isakmp_add_attr_l(buf0
, type
, val
)
2477 vchar_t
*buf
= NULL
;
2478 struct isakmp_data
*data
;
2482 tlen
= sizeof(*data
);
2486 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2488 buf
= vmalloc(tlen
);
2490 plog(LLV_ERROR
, LOCATION
, NULL
,
2491 "failed to get a attribute buffer.\n");
2495 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2496 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2497 data
->lorv
= htons((u_int16_t
)val
);
2503 * calculate cookie and set.
2506 isakmp_newcookie(place
, remote
, local
)
2508 struct sockaddr
*remote
;
2509 struct sockaddr
*local
;
2511 vchar_t
*buf
= NULL
, *buf2
= NULL
;
2521 if (remote
->sa_family
!= local
->sa_family
) {
2522 plog(LLV_ERROR
, LOCATION
, NULL
,
2523 "address family mismatch, remote:%d local:%d\n",
2524 remote
->sa_family
, local
->sa_family
);
2527 switch (remote
->sa_family
) {
2529 alen
= sizeof(struct in_addr
);
2530 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
2531 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
2535 alen
= sizeof(struct in6_addr
);
2536 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
2537 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
2541 plog(LLV_ERROR
, LOCATION
, NULL
,
2542 "invalid family: %d\n", remote
->sa_family
);
2545 blen
= (alen
+ sizeof(u_short
)) * 2
2546 + sizeof(time_t) + lcconf
->secret_size
;
2547 buf
= vmalloc(blen
);
2549 plog(LLV_ERROR
, LOCATION
, NULL
,
2550 "failed to get a cookie.\n");
2555 /* copy my address */
2556 memcpy(p
, sa1
, alen
);
2558 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
2559 memcpy(p
, &port
, sizeof(u_short
));
2560 p
+= sizeof(u_short
);
2562 /* copy target address */
2563 memcpy(p
, sa2
, alen
);
2565 port
= ((struct sockaddr_in
*)local
)->sin_port
;
2566 memcpy(p
, &port
, sizeof(u_short
));
2567 p
+= sizeof(u_short
);
2571 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
2574 /* copy random value */
2575 buf2
= eay_set_random(lcconf
->secret_size
);
2578 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
2579 p
+= lcconf
->secret_size
;
2582 buf2
= eay_sha1_one(buf
);
2583 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
2585 sa1
= val2str(place
, sizeof (cookie_t
));
2586 plog(LLV_DEBUG
, LOCATION
, NULL
, "new cookie:\n%s\n", sa1
);
2599 * save partner's(payload) data into phhandle.
2602 isakmp_p2ph(buf
, gen
)
2604 struct isakmp_gen
*gen
;
2606 /* XXX to be checked in each functions for logging. */
2608 plog(LLV_WARNING
, LOCATION
, NULL
,
2609 "ignore this payload, same payload type exist.\n");
2613 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2615 plog(LLV_ERROR
, LOCATION
, NULL
,
2616 "failed to get buffer.\n");
2619 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2625 isakmp_newmsgid2(iph1
)
2626 struct ph1handle
*iph1
;
2631 msgid2
= eay_random();
2632 } while (getph2bymsgid(iph1
, msgid2
));
2638 * set values into allocated buffer of isakmp header for phase 1
2641 set_isakmp_header(vbuf
, iph1
, nptype
, etype
, flags
, msgid
)
2643 struct ph1handle
*iph1
;
2649 struct isakmp
*isakmp
;
2651 if (vbuf
->l
< sizeof(*isakmp
))
2654 isakmp
= (struct isakmp
*)vbuf
->v
;
2656 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2657 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2658 isakmp
->np
= nptype
;
2659 isakmp
->v
= iph1
->version
;
2660 isakmp
->etype
= etype
;
2661 isakmp
->flags
= flags
;
2662 isakmp
->msgid
= msgid
;
2663 isakmp
->len
= htonl(vbuf
->l
);
2665 return vbuf
->v
+ sizeof(*isakmp
);
2669 * set values into allocated buffer of isakmp header for phase 1
2672 set_isakmp_header1(vbuf
, iph1
, nptype
)
2674 struct ph1handle
*iph1
;
2677 return set_isakmp_header (vbuf
, iph1
, nptype
, iph1
->etype
, iph1
->flags
, iph1
->msgid
);
2681 * set values into allocated buffer of isakmp header for phase 2
2684 set_isakmp_header2(vbuf
, iph2
, nptype
)
2686 struct ph2handle
*iph2
;
2689 return set_isakmp_header (vbuf
, iph2
->ph1
, nptype
, ISAKMP_ETYPE_QUICK
, iph2
->flags
, iph2
->msgid
);
2693 * set values into allocated buffer of isakmp payload.
2696 set_isakmp_payload(buf
, src
, nptype
)
2701 struct isakmp_gen
*gen
;
2704 plog(LLV_DEBUG
, LOCATION
, NULL
, "add payload of len %zu, next type %d\n",
2707 gen
= (struct isakmp_gen
*)p
;
2709 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2711 memcpy(p
, src
->v
, src
->l
);
2722 case ISAKMP_ETYPE_IDENT
:
2724 case ISAKMP_ETYPE_AGG
:
2726 case ISAKMP_ETYPE_BASE
:
2739 case ISAKMP_ETYPE_QUICK
:
2747 #ifdef HAVE_PRINT_ISAKMP_C
2748 /* for print-isakmp.c */
2750 extern void isakmp_print
__P((const u_char
*, u_int
, const u_char
*));
2752 char *getname
__P((const u_char
*));
2754 char *getname6
__P((const u_char
*));
2756 int safeputchar
__P((int));
2759 * Return a name for the IP address pointed to by ap. This address
2760 * is assumed to be in network byte order.
2766 struct sockaddr_in addr
;
2767 static char ntop_buf
[NI_MAXHOST
];
2769 memset(&addr
, 0, sizeof(addr
));
2771 addr
.sin_len
= sizeof(struct sockaddr_in
);
2773 addr
.sin_family
= AF_INET
;
2774 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2775 if (getnameinfo((struct sockaddr
*)&addr
, sizeof(addr
),
2776 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2777 NI_NUMERICHOST
| niflags
))
2778 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2785 * Return a name for the IP6 address pointed to by ap. This address
2786 * is assumed to be in network byte order.
2792 struct sockaddr_in6 addr
;
2793 static char ntop_buf
[NI_MAXHOST
];
2795 memset(&addr
, 0, sizeof(addr
));
2796 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2797 addr
.sin6_family
= AF_INET6
;
2798 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2799 if (getnameinfo((struct sockaddr
*)&addr
, addr
.sin6_len
,
2800 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2801 NI_NUMERICHOST
| niflags
))
2802 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2814 ch
= (unsigned char)(c
& 0xff);
2815 if (c
< 0x80 && isprint(c
))
2816 return printf("%c", c
& 0xff);
2818 return printf("\\%03o", c
& 0xff);
2822 isakmp_printpacket(msg
, from
, my
, decoded
)
2824 struct sockaddr
*from
;
2825 struct sockaddr
*my
;
2831 char hostbuf
[NI_MAXHOST
];
2832 char portbuf
[NI_MAXSERV
];
2833 struct isakmp
*isakmp
;
2837 if (loglevel
< LLV_DEBUG
)
2841 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
2843 gettimeofday(&tv
, NULL
);
2844 s
= tv
.tv_sec
% 3600;
2845 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2848 if (getnameinfo(from
, sysdep_sa_len(from
), hostbuf
, sizeof(hostbuf
),
2849 portbuf
, sizeof(portbuf
),
2850 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2851 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2852 strlcpy(portbuf
, "?", sizeof(portbuf
));
2854 printf("%s:%s", hostbuf
, portbuf
);
2859 if (getnameinfo(my
, sysdep_sa_len(my
), hostbuf
, sizeof(hostbuf
),
2860 portbuf
, sizeof(portbuf
),
2861 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2862 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2863 strlcpy(portbuf
, "?", sizeof(portbuf
));
2865 printf("%s:%s", hostbuf
, portbuf
);
2872 printf("(malloc fail)\n");
2876 isakmp
= (struct isakmp
*)buf
->v
;
2877 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
2880 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
2881 if (buf
->l
< pad
&& 2 < vflag
)
2882 printf("(wrong padding)");
2884 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
2888 snapend
= buf
->v
+ buf
->l
;
2889 isakmp_print(buf
->v
, buf
->l
, NULL
);
2897 #endif /*HAVE_PRINT_ISAKMP_C*/
2900 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
2901 struct ph1handle
*iph1
;
2902 struct remoteconf
*rmconf
;
2903 struct sockaddr
*remote
, *local
;
2907 /* address portion must be grabbed from real remote address "remote" */
2908 iph1
->remote
= dupsaddr(remote
);
2909 if (iph1
->remote
== NULL
)
2913 * if remote has no port # (in case of initiator - from ACQUIRE msg)
2914 * - if remote.conf specifies port #, use that
2915 * - if remote.conf does not, use 500
2916 * if remote has port # (in case of responder - from recvfrom(2))
2917 * respect content of "remote".
2919 if (extract_port(iph1
->remote
) == 0) {
2922 port
= extract_port(rmconf
->remote
);
2925 set_port(iph1
->remote
, port
);
2929 iph1
->local
= getlocaladdr(iph1
->remote
);
2931 iph1
->local
= dupsaddr(local
);
2932 if (iph1
->local
== NULL
)
2935 if (extract_port(iph1
->local
) == 0) {
2936 port
= myaddr_getsport(iph1
->local
);
2939 set_port(iph1
->local
, PORT_ISAKMP
);
2943 if (extract_port(iph1
->local
) == lcconf
->port_isakmp_natt
) {
2944 plog(LLV_DEBUG
, LOCATION
, NULL
, "Marking ports as changed\n");
2945 iph1
->natt_flags
|= NAT_ADD_NON_ESP_MARKER
;
2954 struct ph1handle
*iph1
;
2957 plog(LLV_ERROR
, LOCATION
, iph1
->remote
, "wrong state %u.\n",
2964 struct ph2handle
*iph2
;
2967 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
, "wrong state %u.\n",
2973 log_ph1established(iph1
)
2974 const struct ph1handle
*iph1
;
2978 src
= racoon_strdup(saddr2str(iph1
->local
));
2979 dst
= racoon_strdup(saddr2str(iph1
->remote
));
2983 plog(LLV_INFO
, LOCATION
, NULL
,
2984 "ISAKMP-SA established %s-%s spi:%s\n",
2986 isakmp_pindex(&iph1
->index
, 0));
2988 evt_phase1(iph1
, EVT_PHASE1_UP
, NULL
);
2989 if(!iph1
->rmconf
->mode_cfg
)
2990 evt_phase1(iph1
, EVT_PHASE1_MODE_CFG
, NULL
);
2998 struct payload_list
*
2999 isakmp_plist_append_full (struct payload_list
*plist
, vchar_t
*payload
,
3000 u_int8_t payload_type
, u_int8_t free_payload
)
3003 plist
= racoon_malloc (sizeof (struct payload_list
));
3007 plist
->next
= racoon_malloc (sizeof (struct payload_list
));
3008 plist
->next
->prev
= plist
;
3009 plist
= plist
->next
;
3013 plist
->payload
= payload
;
3014 plist
->payload_type
= payload_type
;
3015 plist
->free_payload
= free_payload
;
3021 isakmp_plist_set_all (struct payload_list
**plist
, struct ph1handle
*iph1
)
3023 struct payload_list
*ptr
= *plist
, *first
;
3024 size_t tlen
= sizeof (struct isakmp
), n
= 0;
3025 vchar_t
*buf
= NULL
;
3028 /* Seek to the first item. */
3029 while (ptr
->prev
) ptr
= ptr
->prev
;
3032 /* Compute the whole length. */
3034 tlen
+= ptr
->payload
->l
+ sizeof (struct isakmp_gen
);
3038 buf
= vmalloc(tlen
);
3040 plog(LLV_ERROR
, LOCATION
, NULL
,
3041 "failed to get buffer to send.\n");
3047 p
= set_isakmp_header1(buf
, iph1
, ptr
->payload_type
);
3053 p
= set_isakmp_payload (p
, ptr
->payload
, ptr
->next
? ptr
->next
->payload_type
: ISAKMP_NPTYPE_NONE
);
3056 if (first
->free_payload
)
3057 vfree(first
->payload
);
3058 racoon_free (first
);
3059 /* ptr->prev = NULL; first = NULL; ... omitted. */
3074 frag_handler(iph1
, msg
, remote
, local
)
3075 struct ph1handle
*iph1
;
3077 struct sockaddr
*remote
;
3078 struct sockaddr
*local
;
3082 if (isakmp_frag_extract(iph1
, msg
) == 1) {
3083 if ((newmsg
= isakmp_frag_reassembly(iph1
)) == NULL
) {
3084 plog(LLV_ERROR
, LOCATION
, remote
,
3085 "Packet reassembly failed\n");
3088 return isakmp_main(newmsg
, remote
, local
);
3096 script_hook(iph1
, script
)
3097 struct ph1handle
*iph1
;
3102 char addrstr
[IP_MAX
];
3103 char portstr
[PORT_MAX
];
3109 iph1
->rmconf
== NULL
||
3110 iph1
->rmconf
->script
[script
] == NULL
)
3113 #ifdef ENABLE_HYBRID
3114 (void)isakmp_cfg_setenv(iph1
, &envp
, &envc
);
3118 GETNAMEINFO(iph1
->local
, addrstr
, portstr
);
3120 if (script_env_append(&envp
, &envc
, "LOCAL_ADDR", addrstr
) != 0) {
3121 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot set LOCAL_ADDR\n");
3125 if (script_env_append(&envp
, &envc
, "LOCAL_PORT", portstr
) != 0) {
3126 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot set LOCAL_PORT\n");
3131 if (iph1
->remote
!= NULL
) {
3132 GETNAMEINFO(iph1
->remote
, addrstr
, portstr
);
3134 if (script_env_append(&envp
, &envc
,
3135 "REMOTE_ADDR", addrstr
) != 0) {
3136 plog(LLV_ERROR
, LOCATION
, NULL
,
3137 "Cannot set REMOTE_ADDR\n");
3141 if (script_env_append(&envp
, &envc
,
3142 "REMOTE_PORT", portstr
) != 0) {
3143 plog(LLV_ERROR
, LOCATION
, NULL
,
3144 "Cannot set REMOTEL_PORT\n");
3149 /* Peer identity. */
3150 if (iph1
->id_p
!= NULL
) {
3151 if (script_env_append(&envp
, &envc
, "REMOTE_ID",
3152 ipsecdoi_id2str(iph1
->id_p
)) != 0) {
3153 plog(LLV_ERROR
, LOCATION
, NULL
,
3154 "Cannot set REMOTE_ID\n");
3159 if (privsep_script_exec(iph1
->rmconf
->script
[script
]->v
,
3161 plog(LLV_ERROR
, LOCATION
, NULL
,
3162 "Script %s execution failed\n", script_names
[script
]);
3165 for (c
= envp
; *c
; c
++)
3174 script_env_append(envp
, envc
, name
, value
)
3184 envitem
= racoon_malloc(strlen(name
) + 1 + strlen(value
) + 1);
3185 if (envitem
== NULL
) {
3186 plog(LLV_ERROR
, LOCATION
, NULL
,
3187 "Cannot allocate memory: %s\n", strerror(errno
));
3190 sprintf(envitem
, "%s=%s", name
, value
);
3192 newenvc
= (*envc
) + 1;
3193 newenvp
= racoon_realloc(*envp
, newenvc
* sizeof(char *));
3194 if (newenvp
== NULL
) {
3195 plog(LLV_ERROR
, LOCATION
, NULL
,
3196 "Cannot allocate memory: %s\n", strerror(errno
));
3197 racoon_free(envitem
);
3201 newenvp
[newenvc
- 2] = envitem
;
3202 newenvp
[newenvc
- 1] = NULL
;
3210 script_exec(script
, name
, envp
)
3215 char *argv
[] = { NULL
, NULL
, NULL
};
3218 argv
[1] = script_names
[name
];
3223 execve(argv
[0], argv
, envp
);
3224 plog(LLV_ERROR
, LOCATION
, NULL
,
3225 "execve(\"%s\") failed: %s\n",
3226 argv
[0], strerror(errno
));
3230 plog(LLV_ERROR
, LOCATION
, NULL
,
3231 "Cannot fork: %s\n", strerror(errno
));
3243 struct ph1handle
*iph1
;
3245 vchar_t
*buf
= NULL
;
3246 struct sadb_msg
*msg
, *next
, *end
;
3248 struct sockaddr
*src
, *dst
;
3249 caddr_t mhp
[SADB_EXT_MAX
+ 1];
3251 struct ph2handle
*iph2
;
3252 struct ph1handle
*new_iph1
;
3254 plog(LLV_INFO
, LOCATION
, NULL
,
3255 "purging ISAKMP-SA spi=%s.\n",
3256 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3258 /* Mark as expired. */
3259 iph1
->status
= PHASE1ST_EXPIRED
;
3261 /* Check if we have another, still valid, phase1 SA. */
3262 new_iph1
= getph1(iph1
, iph1
->local
, iph1
->remote
, GETPH1_F_ESTABLISHED
);
3265 * Delete all orphaned or binded to the deleting ph1handle phase2 SAs.
3266 * Keep all others phase2 SAs.
3268 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
3270 plog(LLV_DEBUG
, LOCATION
, NULL
,
3271 "pfkey_dump_sadb returned nothing.\n");
3275 msg
= (struct sadb_msg
*)buf
->v
;
3276 end
= (struct sadb_msg
*)(buf
->v
+ buf
->l
);
3279 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
3281 next
= (struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
3282 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
3287 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
3288 plog(LLV_ERROR
, LOCATION
, NULL
,
3289 "pfkey_check (%s)\n", ipsec_strerror());
3294 sa
= (struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
3296 !mhp
[SADB_EXT_ADDRESS_SRC
] ||
3297 !mhp
[SADB_EXT_ADDRESS_DST
]) {
3301 pk_fixup_sa_addresses(mhp
);
3302 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
3303 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
3305 if (sa
->sadb_sa_state
!= SADB_SASTATE_LARVAL
&&
3306 sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
&&
3307 sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
3313 * check in/outbound SAs.
3314 * Select only SAs where src == local and dst == remote (outgoing)
3315 * or src == remote and dst == local (incoming).
3317 if ((cmpsaddr(iph1
->local
, src
) ||
3318 cmpsaddr(iph1
->remote
, dst
)) &&
3319 (cmpsaddr(iph1
->local
, dst
) ||
3320 cmpsaddr(iph1
->remote
, src
))) {
3325 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
3326 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
3328 /* Check if there is another valid ISAKMP-SA */
3329 if (new_iph1
!= NULL
) {
3332 /* No handler... still send a pfkey_delete message, but log this !*/
3333 plog(LLV_INFO
, LOCATION
, NULL
,
3334 "Unknown IPsec-SA spi=%u, hmmmm?\n",
3335 ntohl(sa
->sadb_sa_spi
));
3339 * If we have a new ph1, do not purge IPsec-SAs binded
3340 * to a different ISAKMP-SA
3342 if (iph2
->ph1
!= NULL
&& iph2
->ph1
!= iph1
){
3347 /* If the ph2handle is established, do not purge IPsec-SA */
3348 if (iph2
->status
== PHASE2ST_ESTABLISHED
||
3349 iph2
->status
== PHASE2ST_EXPIRED
) {
3351 plog(LLV_INFO
, LOCATION
, NULL
,
3352 "keeping IPsec-SA spi=%u - found valid ISAKMP-SA spi=%s.\n",
3353 ntohl(sa
->sadb_sa_spi
),
3354 isakmp_pindex(&(new_iph1
->index
), new_iph1
->msgid
));
3362 pfkey_send_delete(lcconf
->sock_pfkey
,
3363 msg
->sadb_msg_satype
,
3365 src
, dst
, sa
->sadb_sa_spi
);
3367 /* delete a relative phase 2 handle. */
3369 delete_spd(iph2
, 0);
3374 plog(LLV_INFO
, LOCATION
, NULL
,
3375 "purged IPsec-SA spi=%u.\n",
3376 ntohl(sa
->sadb_sa_spi
));
3384 /* Mark the phase1 handler as EXPIRED */
3385 plog(LLV_INFO
, LOCATION
, NULL
,
3386 "purged ISAKMP-SA spi=%s.\n",
3387 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3389 sched_schedule(&iph1
->sce
, 1, isakmp_ph1delete_stub
);
3393 delete_spd(iph2
, created
)
3394 struct ph2handle
*iph2
;
3397 struct policyindex spidx
;
3398 struct sockaddr_storage addr
;
3400 struct sockaddr
*src
;
3401 struct sockaddr
*dst
;
3403 int idi2type
= 0;/* switch whether copy IDs into id[src,dst]. */
3408 /* Delete the SPD entry if we generated it
3410 if (! iph2
->generated_spidx
)
3416 plog(LLV_INFO
, LOCATION
, NULL
,
3417 "generated policy, deleting it.\n");
3419 memset(&spidx
, 0, sizeof(spidx
));
3420 iph2
->spidx_gen
= (caddr_t
)&spidx
;
3422 /* make inbound policy */
3425 spidx
.dir
= IPSEC_DIR_INBOUND
;
3429 * Note: code from get_proposal_r
3432 #define _XIDT(d) ((struct ipsecdoi_id_b *)(d)->v)->type
3435 * make destination address in spidx from either ID payload
3436 * or phase 1 address into a address in spidx.
3438 if (iph2
->id
!= NULL
3439 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3440 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
3441 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3442 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3443 /* get a destination address of a policy */
3444 error
= ipsecdoi_id2sockaddr(iph2
->id
,
3445 (struct sockaddr
*)&spidx
.dst
,
3446 &spidx
.prefd
, &spidx
.ul_proto
);
3452 * get scopeid from the SA address.
3453 * note that the phase 1 source address is used as
3454 * a destination address to search for a inbound
3455 * policy entry because rcoon is responder.
3457 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
3459 setscopeid((struct sockaddr
*)&spidx
.dst
,
3465 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3466 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
3467 idi2type
= _XIDT(iph2
->id
);
3471 plog(LLV_DEBUG
, LOCATION
, NULL
,
3472 "get a destination address of SP index "
3473 "from phase1 address "
3474 "due to no ID payloads found "
3475 "OR because ID type is not address.\n");
3478 * copy the SOURCE address of IKE into the
3479 * DESTINATION address of the key to search the
3480 * SPD because the direction of policy is inbound.
3482 memcpy(&spidx
.dst
, iph2
->src
, sysdep_sa_len(iph2
->src
));
3483 switch (spidx
.dst
.ss_family
) {
3486 sizeof(struct in_addr
) << 3;
3491 sizeof(struct in6_addr
) << 3;
3500 /* make source address in spidx */
3501 if (iph2
->id_p
!= NULL
3502 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
3503 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
3504 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3505 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3506 /* get a source address of inbound SA */
3507 error
= ipsecdoi_id2sockaddr(iph2
->id_p
,
3508 (struct sockaddr
*)&spidx
.src
,
3509 &spidx
.prefs
, &spidx
.ul_proto
);
3515 * get scopeid from the SA address.
3516 * for more detail, see above of this function.
3518 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
3520 setscopeid((struct sockaddr
*)&spidx
.src
,
3527 /* make sa_[src,dst] if both ID types are IP address and same */
3528 if (_XIDT(iph2
->id_p
) == idi2type
3529 && spidx
.dst
.ss_family
== spidx
.src
.ss_family
) {
3531 dupsaddr((struct sockaddr
*)&spidx
.dst
);
3532 if (iph2
->sa_src
== NULL
) {
3533 plog(LLV_ERROR
, LOCATION
, NULL
,
3534 "allocation failed\n");
3538 dupsaddr((struct sockaddr
*)&spidx
.src
);
3539 if (iph2
->sa_dst
== NULL
) {
3540 plog(LLV_ERROR
, LOCATION
, NULL
,
3541 "allocation failed\n");
3547 plog(LLV_DEBUG
, LOCATION
, NULL
,
3548 "get a source address of SP index "
3549 "from phase1 address "
3550 "due to no ID payloads found "
3551 "OR because ID type is not address.\n");
3553 /* see above comment. */
3554 memcpy(&spidx
.src
, iph2
->dst
, sysdep_sa_len(iph2
->dst
));
3555 switch (spidx
.src
.ss_family
) {
3558 sizeof(struct in_addr
) << 3;
3563 sizeof(struct in6_addr
) << 3;
3574 plog(LLV_DEBUG
, LOCATION
, NULL
,
3575 "get a src address from ID payload "
3576 "%s prefixlen=%u ul_proto=%u\n",
3577 saddr2str((struct sockaddr
*)&spidx
.src
),
3578 spidx
.prefs
, spidx
.ul_proto
);
3579 plog(LLV_DEBUG
, LOCATION
, NULL
,
3580 "get dst address from ID payload "
3581 "%s prefixlen=%u ul_proto=%u\n",
3582 saddr2str((struct sockaddr
*)&spidx
.dst
),
3583 spidx
.prefd
, spidx
.ul_proto
);
3586 * convert the ul_proto if it is 0
3587 * because 0 in ID payload means a wild card.
3589 if (spidx
.ul_proto
== 0)
3590 spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
3594 /* Check if the generated SPD has the same timestamp as the SA.
3595 * If timestamps are different, this means that the SPD entry has been
3596 * refreshed by another SA, and should NOT be deleted with the current SA.
3599 struct secpolicy
*p
;
3603 /* just do no test if p is NULL, because this probably just means
3604 * that the policy has already be deleted for some reason.
3606 if(p
->spidx
.created
!= created
)
3611 /* End of code from get_proposal_r
3614 if (pk_sendspddelete(iph2
) < 0) {
3615 plog(LLV_ERROR
, LOCATION
, NULL
,
3616 "pfkey spddelete(inbound) failed.\n");
3618 plog(LLV_DEBUG
, LOCATION
, NULL
,
3619 "pfkey spddelete(inbound) sent.\n");
3622 #ifdef HAVE_POLICY_FWD
3623 /* make forward policy if required */
3624 if (tunnel_mode_prop(iph2
->approval
)) {
3625 spidx
.dir
= IPSEC_DIR_FWD
;
3626 if (pk_sendspddelete(iph2
) < 0) {
3627 plog(LLV_ERROR
, LOCATION
, NULL
,
3628 "pfkey spddelete(forward) failed.\n");
3630 plog(LLV_DEBUG
, LOCATION
, NULL
,
3631 "pfkey spddelete(forward) sent.\n");
3636 /* make outbound policy */
3639 spidx
.dir
= IPSEC_DIR_OUTBOUND
;
3641 spidx
.src
= spidx
.dst
;
3644 spidx
.prefs
= spidx
.prefd
;
3647 if (pk_sendspddelete(iph2
) < 0) {
3648 plog(LLV_ERROR
, LOCATION
, NULL
,
3649 "pfkey spddelete(outbound) failed.\n");
3651 plog(LLV_DEBUG
, LOCATION
, NULL
,
3652 "pfkey spddelete(outbound) sent.\n");
3655 iph2
->spidx_gen
=NULL
;
3661 setscopeid(sp_addr0
, sa_addr0
)
3662 struct sockaddr
*sp_addr0
, *sa_addr0
;
3664 struct sockaddr_in6
*sp_addr
, *sa_addr
;
3666 sp_addr
= (struct sockaddr_in6
*)sp_addr0
;
3667 sa_addr
= (struct sockaddr_in6
*)sa_addr0
;
3669 if (!IN6_IS_ADDR_LINKLOCAL(&sp_addr
->sin6_addr
)
3670 && !IN6_IS_ADDR_SITELOCAL(&sp_addr
->sin6_addr
)
3671 && !IN6_IS_ADDR_MULTICAST(&sp_addr
->sin6_addr
))
3674 /* this check should not be here ? */
3675 if (sa_addr
->sin6_family
!= AF_INET6
) {
3676 plog(LLV_ERROR
, LOCATION
, NULL
,
3677 "can't get scope ID: family mismatch\n");
3681 if (!IN6_IS_ADDR_LINKLOCAL(&sa_addr
->sin6_addr
)) {
3682 plog(LLV_ERROR
, LOCATION
, NULL
,
3683 "scope ID is not supported except of lladdr.\n");
3687 sp_addr
->sin6_scope_id
= sa_addr
->sin6_scope_id
;