1 /* $KAME: isakmp.c,v 1.176 2002/08/28 04:08:30 itojun 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
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/queue.h>
37 #include <netkey/key_var.h>
38 #include <netinet/in.h>
44 #if TIME_WITH_SYS_TIME
45 # include <sys/time.h>
49 # include <sys/time.h>
60 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
72 #include "remoteconf.h"
73 #include "localconf.h"
74 #include "grabmyaddr.h"
75 #include "isakmp_var.h"
79 #include "ipsec_doi.h"
81 #include "crypto_openssl.h"
83 #include "isakmp_ident.h"
84 #include "isakmp_agg.h"
85 #include "isakmp_base.h"
86 #include "isakmp_quick.h"
87 #include "isakmp_inf.h"
88 #include "isakmp_newg.h"
90 #ifndef HAVE_ARC4RANDOM
91 #include "arc4random.h"
94 static int nostate1
__P((struct ph1handle
*, vchar_t
*));
95 static int nostate2
__P((struct ph2handle
*, vchar_t
*));
97 extern caddr_t
val2str(const char *, size_t);
99 static int (*ph1exchange
[][2][PHASE1ST_MAX
])
100 __P((struct ph1handle
*, vchar_t
*)) = {
103 /* Identity Protection exchange */
105 { nostate1
, ident_i1send
, nostate1
, ident_i2recv
, ident_i2send
,
106 ident_i3recv
, ident_i3send
, ident_i4recv
, ident_i4send
, nostate1
, },
107 { nostate1
, ident_r1recv
, ident_r1send
, ident_r2recv
, ident_r2send
,
108 ident_r3recv
, ident_r3send
, nostate1
, nostate1
, nostate1
, },
110 /* Aggressive exchange */
112 { nostate1
, agg_i1send
, nostate1
, agg_i2recv
, agg_i2send
,
113 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
114 { nostate1
, agg_r1recv
, agg_r1send
, agg_r2recv
, agg_r2send
,
115 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
119 { nostate1
, base_i1send
, nostate1
, base_i2recv
, base_i2send
,
120 base_i3recv
, base_i3send
, nostate1
, nostate1
, nostate1
, },
121 { nostate1
, base_r1recv
, base_r1send
, base_r2recv
, base_r2send
,
122 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
126 static int (*ph2exchange
[][2][PHASE2ST_MAX
])
127 __P((struct ph2handle
*, vchar_t
*)) = {
130 /* Quick mode for IKE*/
132 { nostate2
, nostate2
, quick_i1prep
, nostate2
, quick_i1send
,
133 quick_i2recv
, quick_i2send
, quick_i3recv
, nostate2
, nostate2
, },
134 { nostate2
, quick_r1recv
, quick_r1prep
, nostate2
, quick_r2send
,
135 quick_r3recv
, quick_r3prep
, quick_r3send
, nostate2
, nostate2
, }
139 static u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
141 static int isakmp_main
__P((vchar_t
*, struct sockaddr
*, struct sockaddr
*));
142 static int ph1_main
__P((struct ph1handle
*, vchar_t
*));
143 static int quick_main
__P((struct ph2handle
*, vchar_t
*));
144 static int isakmp_ph1begin_r
__P((vchar_t
*,
145 struct sockaddr
*, struct sockaddr
*, u_int8_t
));
146 static int isakmp_ph2begin_i
__P((struct ph1handle
*, struct ph2handle
*));
147 static int isakmp_ph2begin_r
__P((struct ph1handle
*, vchar_t
*));
148 static int etypesw1
__P((int));
149 static int etypesw2
__P((int));
152 * isakmp packet handler
155 isakmp_handler(so_isakmp
)
158 struct isakmp isakmp
;
159 struct sockaddr_storage remote
;
160 struct sockaddr_storage local
;
161 int remote_len
= sizeof(remote
);
162 int local_len
= sizeof(local
);
168 /* read message by MSG_PEEK */
169 while ((len
= recvfromto(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
170 MSG_PEEK
, (struct sockaddr
*)&remote
, &remote_len
,
171 (struct sockaddr
*)&local
, &local_len
)) < 0) {
174 plog(LLV_ERROR
, LOCATION
, NULL
,
175 "failed to receive isakmp packet\n");
179 /* check isakmp header length, as well as sanity of header length */
180 if (len
< sizeof(isakmp
) || ntohl(isakmp
.len
) < sizeof(isakmp
)) {
181 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
182 "packet shorter than isakmp header size.\n");
184 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
185 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
186 plog(LLV_ERROR
, LOCATION
, NULL
,
187 "failed to receive isakmp packet\n");
192 /* reject if the size is toooo big */
193 if (ntohl(isakmp
.len
) > 0xffff) {
194 plog(LLV_ERROR
, LOCATION
, NULL
,
195 "the length of the isakmp header is too big.\n");
196 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
197 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
198 plog(LLV_ERROR
, LOCATION
, NULL
,
199 "failed to receive isakmp packet\n");
204 /* read real message */
205 if ((buf
= vmalloc(ntohl(isakmp
.len
))) == NULL
) {
206 plog(LLV_ERROR
, LOCATION
, NULL
,
207 "failed to allocate reading buffer\n");
209 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
210 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
211 plog(LLV_ERROR
, LOCATION
, NULL
,
212 "failed to receive isakmp packet\n");
217 while ((len
= recvfromto(so_isakmp
, buf
->v
, buf
->l
,
218 0, (struct sockaddr
*)&remote
, &remote_len
,
219 (struct sockaddr
*)&local
, &local_len
)) < 0) {
222 plog(LLV_ERROR
, LOCATION
, NULL
,
223 "failed to receive isakmp packet\n");
228 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
229 "received invalid length, why ?\n");
233 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
234 plog(LLV_DEBUG
, LOCATION
, (struct sockaddr
*)&local
,
235 "%d bytes message received from %s\n",
236 len
, saddr2str((struct sockaddr
*)&remote
));
237 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
239 /* avoid packets with malicious port/address */
240 switch (remote
.ss_family
) {
242 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
246 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
250 plog(LLV_ERROR
, LOCATION
, NULL
,
251 "invalid family: %d\n", remote
.ss_family
);
255 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
256 "src port == 0 (valid as UDP but not with IKE)\n");
260 /* XXX: check sender whether to be allowed or not to accept */
262 /* XXX: I don't know how to check isakmp half connection attack. */
264 /* simply reply if the packet was processed. */
265 if (check_recvdpkt((struct sockaddr
*)&remote
,
266 (struct sockaddr
*)&local
, buf
)) {
267 plog(LLV_NOTIFY
, LOCATION
, NULL
,
268 "the packet is retransmitted by %s.\n",
269 saddr2str((struct sockaddr
*)&remote
));
274 /* isakmp main routine */
275 if (isakmp_main(buf
, (struct sockaddr
*)&remote
,
276 (struct sockaddr
*)&local
) != 0) goto end
;
289 * isakmp packet handler for natt port (4500)
292 isakmp_natt_handler(so_isakmp
)
295 u_char temp_buffer
[sizeof(struct isakmp
) + 4];
296 struct isakmp
*isakmp
= (struct isakmp
*)(temp_buffer
+ 4);
297 struct sockaddr_storage remote
;
298 struct sockaddr_storage local
;
299 int remote_len
= sizeof(remote
);
300 int local_len
= sizeof(local
);
306 /* read message by MSG_PEEK */
307 while ((len
= recvfromto(so_isakmp
, temp_buffer
, sizeof(temp_buffer
),
308 MSG_PEEK
, (struct sockaddr
*)&remote
, &remote_len
,
309 (struct sockaddr
*)&local
, &local_len
)) < 0) {
312 plog(LLV_ERROR
, LOCATION
, NULL
,
313 "failed to receive isakmp packet\n");
317 /* remove the four bytes of zeros on nat traversal port */
318 if (*(u_long
*)temp_buffer
!= 0L)
321 * This is a UDP encapsulated IPSec packet,
324 * TBD: Need a way to read the packet.
325 * The kernel intercepts these packets on Mac OS X
326 * but not all kernels will handle this the same way.
331 /* check isakmp header length */
332 if (len
< sizeof(temp_buffer
)) {
333 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
334 "packet shorter than isakmp header size.\n");
336 if ((len
= recvfrom(so_isakmp
, (char *)temp_buffer
, sizeof(temp_buffer
),
337 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
338 plog(LLV_ERROR
, LOCATION
, NULL
,
339 "failed to receive isakmp packet\n");
344 /* read real message */
345 if ((buf
= vmalloc(ntohl(isakmp
->len
) + 4)) == NULL
) {
346 plog(LLV_ERROR
, LOCATION
, NULL
,
347 "failed to allocate reading buffer\n");
349 if ((len
= recvfrom(so_isakmp
, (char *)temp_buffer
, sizeof(temp_buffer
),
350 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
351 plog(LLV_ERROR
, LOCATION
, NULL
,
352 "failed to receive isakmp packet\n");
357 while ((len
= recvfromto(so_isakmp
, buf
->v
, buf
->l
,
358 0, (struct sockaddr
*)&remote
, &remote_len
,
359 (struct sockaddr
*)&local
, &local_len
)) < 0) {
362 plog(LLV_ERROR
, LOCATION
, NULL
,
363 "failed to receive isakmp packet\n");
368 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
369 "received invalid length, header says %d, packet is %d bytes why ?\n",
375 * Discard first 4 bytes, they're either:
376 * 0 - this is IKE traffic
377 * !0 - first four bytes are the SPI of a UDP encapsulated IPSec packet
378 * The seond type of packet should be interecepted by the kernel
379 * or dropped before we get to this point.
382 vchar_t
*newbuf
= vmalloc(buf
->l
- 4);
385 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
386 "couldn't allocate smaller buffer.\n");
389 memcpy(newbuf
->v
, buf
->v
+ 4, newbuf
->l
);
395 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
396 plog(LLV_DEBUG
, LOCATION
, (struct sockaddr
*)&local
,
397 "%d bytes message received from %s\n",
398 len
, saddr2str((struct sockaddr
*)&remote
));
399 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
401 /* avoid packets with malicious port/address */
402 switch (remote
.ss_family
) {
404 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
408 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
412 plog(LLV_ERROR
, LOCATION
, NULL
,
413 "invalid family: %d\n", remote
.ss_family
);
417 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
418 "src port == 0 (valid as UDP but not with IKE)\n");
423 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
424 plog(LLV_DEBUG
, LOCATION
, (struct sockaddr
*)&remote
,
425 "natt receiving packet %.8X%.8X:%.8X%.8X %u\n",
426 *(u_long
*)isakmp
->i_ck
, *(u_long
*)&isakmp
->i_ck
[4],
427 *(u_long
*)isakmp
->r_ck
, *(u_long
*)&isakmp
->r_ck
[4],
431 /* XXX: check sender whether to be allowed or not to accept */
433 /* XXX: I don't know how to check isakmp half connection attack. */
435 /* simply reply if the packet was processed. */
436 if (check_recvdpkt((struct sockaddr
*)&remote
,
437 (struct sockaddr
*)&local
, buf
)) {
438 plog(LLV_NOTIFY
, LOCATION
, NULL
,
439 "the packet is retransmitted by %s.\n",
440 saddr2str((struct sockaddr
*)&remote
));
445 /* isakmp main routine */
446 if (isakmp_main(buf
, (struct sockaddr
*)&remote
,
447 (struct sockaddr
*)&local
) != 0) goto end
;
461 * main processing to handle isakmp payload
464 isakmp_main(msg
, remote
, local
)
466 struct sockaddr
*remote
, *local
;
468 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
469 isakmp_index
*index
= (isakmp_index
*)isakmp
;
470 u_int32_t msgid
= isakmp
->msgid
;
471 struct ph1handle
*iph1
;
473 #ifdef HAVE_PRINT_ISAKMP_C
474 isakmp_printpacket(msg
, remote
, local
, 0);
477 /* the initiator's cookie must not be zero */
478 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
479 plog(LLV_ERROR
, LOCATION
, remote
,
480 "malformed cookie received.\n");
484 /* Check the Major and Minor Version fields. */
486 * XXX Is is right to check version here ?
487 * I think it may no be here because the version depends
488 * on exchange status.
490 if (isakmp
->v
< ISAKMP_VERSION_NUMBER
) {
491 if (ISAKMP_GETMAJORV(isakmp
->v
) < ISAKMP_MAJOR_VERSION
) {
492 plog(LLV_ERROR
, LOCATION
, remote
,
493 "invalid major version %d.\n",
494 ISAKMP_GETMAJORV(isakmp
->v
));
497 #if ISAKMP_MINOR_VERSION > 0
498 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) {
499 plog(LLV_ERROR
, LOCATION
, remote
,
500 "invalid minor version %d.\n",
501 ISAKMP_GETMINORV(isakmp
->v
));
507 /* check the Flags field. */
508 /* XXX How is the exclusive check, E and A ? */
509 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) {
510 plog(LLV_ERROR
, LOCATION
, remote
,
511 "invalid flag 0x%02x.\n", isakmp
->flags
);
515 /* ignore commit bit. */
516 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
517 if (isakmp
->msgid
== 0) {
518 isakmp_info_send_nx(isakmp
, remote
, local
,
519 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
520 plog(LLV_ERROR
, LOCATION
, remote
,
521 "Commit bit on phase1 forbidden.\n");
526 iph1
= getph1byindex(index
);
529 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
530 iph1
->side
== INITIATOR
) {
531 plog(LLV_DEBUG
, LOCATION
, remote
,
532 "malformed cookie received or "
533 "the initiator's cookies collide.\n");
537 /* must be same addresses in one stream of a phase at least. */
538 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
540 if (iph1
->side
== RESPONDER
&&
541 (iph1
->natt_flags
& natt_remote_support
) != 0 &&
542 cmpsaddrwop(iph1
->remote
, remote
) == 0)
545 * If the initiator detects a NAT it may switch to a
546 * new port. Technically, the remote address may change
547 * as well, depending on the NAT. Handling that would
548 * require more changes.
550 * We should record the new remote port so we can
553 plog(LLV_WARNING
, LOCATION
, remote
,
554 "remote port changed from %s\n", saddr2str(iph1
->remote
));
555 memcpy(iph1
->remote
, remote
, iph1
->remote
->sa_len
);
556 memcpy(iph1
->local
, local
, iph1
->local
->sa_len
);
561 char *saddr_db
, *saddr_act
;
563 saddr_db
= strdup(saddr2str(iph1
->remote
));
564 saddr_act
= strdup(saddr2str(remote
));
566 plog(LLV_WARNING
, LOCATION
, remote
,
567 "remote address mismatched. db=%s, act=%s\n",
568 saddr_db
, saddr_act
);
570 racoon_free(saddr_db
);
571 racoon_free(saddr_act
);
575 * don't check of exchange type here because other type will be
576 * with same index, for example, informational exchange.
579 /* XXX more acceptable check */
582 switch (isakmp
->etype
) {
583 case ISAKMP_ETYPE_IDENT
:
584 case ISAKMP_ETYPE_AGG
:
585 case ISAKMP_ETYPE_BASE
:
586 /* phase 1 validity check */
587 if (isakmp
->msgid
!= 0) {
588 plog(LLV_ERROR
, LOCATION
, remote
,
589 "message id should be zero in phase1.\n");
593 /* search for isakmp status record of phase 1 */
596 * the packet must be the 1st message from a initiator
597 * or the 2nd message from the responder.
600 /* search for phase1 handle by index without r_ck */
601 iph1
= getph1byindex0(index
);
603 /*it must be the 1st message from a initiator.*/
604 if (memcmp(&isakmp
->r_ck
, r_ck0
,
605 sizeof(cookie_t
)) != 0) {
607 plog(LLV_DEBUG
, LOCATION
, remote
,
608 "malformed cookie received "
609 "or the spi expired.\n");
613 /* it must be responder's 1st exchange. */
614 if (isakmp_ph1begin_r(msg
, remote
, local
,
622 /* it must be the 2nd message from the responder. */
623 if (iph1
->side
!= INITIATOR
) {
624 plog(LLV_DEBUG
, LOCATION
, remote
,
625 "malformed cookie received. "
626 "it has to be as the initiator. %s\n",
627 isakmp_pindex(&iph1
->index
, 0));
633 * Don't delete phase 1 handler when the exchange type
634 * in handler is not equal to packet's one because of no
635 * authencication completed.
637 if (iph1
->etype
!= isakmp
->etype
) {
638 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
639 "exchange type is mismatched: "
640 "db=%s packet=%s, ignore it.\n",
641 s_isakmp_etype(iph1
->etype
),
642 s_isakmp_etype(isakmp
->etype
));
646 /* call main process of phase 1 */
647 if (ph1_main(iph1
, msg
) < 0) {
648 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
649 "phase1 negotiation failed.\n");
656 case ISAKMP_ETYPE_AUTH
:
657 plog(LLV_INFO
, LOCATION
, remote
,
658 "unsupported exchange %d received.\n",
662 case ISAKMP_ETYPE_INFO
:
663 case ISAKMP_ETYPE_ACKINFO
:
665 * iph1 must be present for Information message.
666 * if iph1 is null then trying to get the phase1 status
667 * as the packet from responder againt initiator's 1st
668 * exchange in phase 1.
669 * NOTE: We think such informational exchange should be ignored.
672 iph1
= getph1byindex0(index
);
674 plog(LLV_ERROR
, LOCATION
, remote
,
675 "unknown Informational "
676 "exchange received.\n");
679 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
680 plog(LLV_WARNING
, LOCATION
, remote
,
681 "remote address mismatched. "
683 saddr2str(iph1
->remote
));
687 if (isakmp_info_recv(iph1
, msg
) < 0)
691 case ISAKMP_ETYPE_QUICK
:
693 struct ph2handle
*iph2
;
696 isakmp_info_send_nx(isakmp
, remote
, local
,
697 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
698 plog(LLV_ERROR
, LOCATION
, remote
,
699 "can't start the quick mode, "
700 "there is no ISAKMP-SA, %s\n",
701 isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
706 /* check status of phase 1 whether negotiated or not. */
707 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
708 plog(LLV_ERROR
, LOCATION
, remote
,
709 "can't start the quick mode, "
710 "there is no valid ISAKMP-SA, %s\n",
711 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
715 /* search isakmp phase 2 stauts record. */
716 iph2
= getph2bymsgid(iph1
, msgid
);
718 /* it must be new negotiation as responder */
719 if (isakmp_ph2begin_r(iph1
, msg
) < 0)
727 * we keep to set commit bit during negotiation.
728 * When SA is configured, bit will be reset.
730 * don't initiate commit bit. should be fixed in the future.
732 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
733 iph2
->flags
|= ISAKMP_FLAG_C
;
735 /* call main process of quick mode */
736 if (quick_main(iph2
, msg
) < 0) {
737 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
738 "phase2 negotiation failed.\n");
747 case ISAKMP_ETYPE_NEWGRP
:
749 plog(LLV_ERROR
, LOCATION
, remote
,
750 "Unknown new group mode exchange, "
751 "there is no ISAKMP-SA.\n");
754 isakmp_newgroup_r(iph1
, msg
);
757 case ISAKMP_ETYPE_NONE
:
759 plog(LLV_ERROR
, LOCATION
, NULL
,
760 "Invalid exchange type %d from %s.\n",
761 isakmp
->etype
, saddr2str(remote
));
769 * main function of phase 1.
773 struct ph1handle
*iph1
;
778 struct timeval start
, end
;
781 /* ignore a packet */
782 if (iph1
->status
== PHASE1ST_ESTABLISHED
)
786 gettimeofday(&start
, NULL
);
789 if (ph1exchange
[etypesw1(iph1
->etype
)]
791 [iph1
->status
] == NULL
) {
792 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
793 "why isn't the function defined.\n");
796 error
= (ph1exchange
[etypesw1(iph1
->etype
)]
798 [iph1
->status
])(iph1
, msg
);
802 * When an invalid packet is received on phase1, it should
803 * be selected to process this packet. That is to respond
804 * with a notify and delete phase 1 handler, OR not to respond
805 * and keep phase 1 handler.
807 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
808 "failed to pre-process packet.\n");
811 /* ignore the error and keep phase 1 handler */
816 /* free resend buffer */
817 if (iph1
->sendbuf
== NULL
) {
818 plog(LLV_ERROR
, LOCATION
, NULL
,
819 "no buffer found as sendbuf\n");
822 vfree(iph1
->sendbuf
);
823 iph1
->sendbuf
= NULL
;
825 /* turn off schedule */
827 SCHED_KILL(iph1
->scr
);
830 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
831 if ((ph1exchange
[etypesw1(iph1
->etype
)]
833 [iph1
->status
])(iph1
, msg
) != 0) {
834 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
835 "failed to process packet.\n");
840 gettimeofday(&end
, NULL
);
841 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
842 "phase1", s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
843 timedelta(&start
, &end
));
845 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
848 gettimeofday(&iph1
->end
, NULL
);
849 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
850 "phase1", s_isakmp_etype(iph1
->etype
),
851 timedelta(&iph1
->start
, &iph1
->end
));
854 /* save created date. */
855 (void)time(&iph1
->created
);
857 /* add to the schedule to expire, and seve back pointer. */
858 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
859 isakmp_ph1expire_stub
, iph1
);
861 /* INITIAL-CONTACT processing */
862 /* don't anything if local test mode. */
864 && iph1
->rmconf
->ini_contact
&& !getcontacted(iph1
->remote
)) {
865 /* send INITIAL-CONTACT */
866 isakmp_info_send_n1(iph1
,
867 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
868 /* insert a node into contacted list. */
869 if (inscontacted(iph1
->remote
) == -1) {
870 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
871 "failed to add contacted list.\n");
876 log_ph1established(iph1
);
877 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
884 * main function of quick mode.
887 quick_main(iph2
, msg
)
888 struct ph2handle
*iph2
;
891 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
894 struct timeval start
, end
;
897 /* ignore a packet */
898 if (iph2
->status
== PHASE2ST_ESTABLISHED
899 || iph2
->status
== PHASE2ST_GETSPISENT
)
903 gettimeofday(&start
, NULL
);
907 if (ph2exchange
[etypesw2(isakmp
->etype
)]
909 [iph2
->status
] == NULL
) {
910 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
911 "why isn't the function defined.\n");
914 error
= (ph2exchange
[etypesw2(isakmp
->etype
)]
916 [iph2
->status
])(iph2
, msg
);
918 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
919 "failed to pre-process packet.\n");
920 if (error
== ISAKMP_INTERNAL_ERROR
)
922 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
926 /* when using commit bit, status will be reached here. */
927 if (iph2
->status
== PHASE2ST_ADDSA
)
930 /* free resend buffer */
931 if (iph2
->sendbuf
== NULL
) {
932 plog(LLV_ERROR
, LOCATION
, NULL
,
933 "no buffer found as sendbuf\n");
936 vfree(iph2
->sendbuf
);
937 iph2
->sendbuf
= NULL
;
939 /* turn off schedule */
941 SCHED_KILL(iph2
->scr
);
944 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
945 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
947 [iph2
->status
])(iph2
, msg
) != 0) {
948 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
949 "failed to process packet.\n");
954 gettimeofday(&end
, NULL
);
955 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
957 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
958 timedelta(&start
, &end
));
964 /* new negotiation of phase 1 for initiator */
966 isakmp_ph1begin_i(rmconf
, remote
)
967 struct remoteconf
*rmconf
;
968 struct sockaddr
*remote
;
970 struct ph1handle
*iph1
;
972 struct timeval start
, end
;
975 /* get new entry to isakmp status table. */
980 iph1
->status
= PHASE1ST_START
;
981 iph1
->rmconf
= rmconf
;
982 iph1
->side
= INITIATOR
;
983 iph1
->version
= ISAKMP_VERSION_NUMBER
;
988 iph1
->gssapi_state
= NULL
;
990 iph1
->approval
= NULL
;
992 /* XXX copy remote address */
993 if (copy_ph1addresses(iph1
, rmconf
, remote
, NULL
) < 0)
998 /* start phase 1 exchange */
999 iph1
->etype
= rmconf
->etypes
->type
;
1001 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1005 a
= strdup(saddr2str(iph1
->local
));
1006 plog(LLV_INFO
, LOCATION
, NULL
,
1007 "initiate new phase 1 negotiation: %s<=>%s\n",
1008 a
, saddr2str(iph1
->remote
));
1011 plog(LLV_INFO
, LOCATION
, NULL
,
1013 s_isakmp_etype(iph1
->etype
));
1016 gettimeofday(&iph1
->start
, NULL
);
1017 gettimeofday(&start
, NULL
);
1019 /* start exchange */
1020 if ((ph1exchange
[etypesw1(iph1
->etype
)]
1022 [iph1
->status
])(iph1
, NULL
) != 0) {
1023 /* failed to start phase 1 negotiation */
1031 gettimeofday(&end
, NULL
);
1032 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1034 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
1035 timedelta(&start
, &end
));
1041 /* new negotiation of phase 1 for responder */
1043 isakmp_ph1begin_r(msg
, remote
, local
, etype
)
1045 struct sockaddr
*remote
, *local
;
1048 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1049 struct remoteconf
*rmconf
;
1050 struct ph1handle
*iph1
;
1051 struct etypes
*etypeok
;
1053 struct timeval start
, end
;
1056 /* look for my configuration */
1057 rmconf
= getrmconf(remote
);
1058 if (rmconf
== NULL
) {
1059 plog(LLV_ERROR
, LOCATION
, remote
,
1061 "configuration.\n");
1065 /* check to be acceptable exchange type */
1066 etypeok
= check_etypeok(rmconf
, etype
);
1067 if (etypeok
== NULL
) {
1068 plog(LLV_ERROR
, LOCATION
, remote
,
1069 "not acceptable %s mode\n", s_isakmp_etype(etype
));
1073 /* get new entry to isakmp status table. */
1078 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
1079 iph1
->status
= PHASE1ST_START
;
1080 iph1
->rmconf
= rmconf
;
1082 iph1
->side
= RESPONDER
;
1083 iph1
->etype
= etypeok
->type
;
1084 iph1
->version
= isakmp
->v
;
1087 iph1
->gssapi_state
= NULL
;
1089 iph1
->approval
= NULL
;
1091 /* copy remote address */
1092 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0)
1097 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1101 a
= strdup(saddr2str(iph1
->local
));
1102 plog(LLV_INFO
, LOCATION
, NULL
,
1103 "respond new phase 1 negotiation: %s<=>%s\n",
1104 a
, saddr2str(iph1
->remote
));
1107 plog(LLV_INFO
, LOCATION
, NULL
,
1108 "begin %s mode.\n", s_isakmp_etype(etype
));
1111 gettimeofday(&iph1
->start
, NULL
);
1112 gettimeofday(&start
, NULL
);
1114 /* start exchange */
1115 if ((ph1exchange
[etypesw1(iph1
->etype
)]
1117 [iph1
->status
])(iph1
, msg
) < 0
1118 || (ph1exchange
[etypesw1(iph1
->etype
)]
1120 [iph1
->status
])(iph1
, msg
) < 0) {
1121 plog(LLV_ERROR
, LOCATION
, remote
,
1122 "failed to process packet.\n");
1128 gettimeofday(&end
, NULL
);
1129 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1131 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
1132 timedelta(&start
, &end
));
1138 /* new negotiation of phase 2 for initiator */
1140 isakmp_ph2begin_i(iph1
, iph2
)
1141 struct ph1handle
*iph1
;
1142 struct ph2handle
*iph2
;
1144 /* found ISAKMP-SA. */
1145 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1146 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
1149 a
= strdup(saddr2str(iph2
->src
));
1150 plog(LLV_INFO
, LOCATION
, NULL
,
1151 "initiate new phase 2 negotiation: %s<=>%s\n",
1152 a
, saddr2str(iph2
->dst
));
1157 gettimeofday(&iph2
->start
, NULL
);
1159 /* found isakmp-sa */
1160 bindph12(iph1
, iph2
);
1161 iph2
->status
= PHASE2ST_STATUS2
;
1163 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1165 [iph2
->status
])(iph2
, NULL
) < 0) {
1167 /* release ipsecsa handler due to internal error. */
1175 /* new negotiation of phase 2 for responder */
1177 isakmp_ph2begin_r(iph1
, msg
)
1178 struct ph1handle
*iph1
;
1181 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1182 struct ph2handle
*iph2
= 0;
1185 struct timeval start
, end
;
1190 plog(LLV_ERROR
, LOCATION
, NULL
,
1191 "failed to allocate phase2 entry.\n");
1196 iph2
->side
= RESPONDER
;
1197 iph2
->status
= PHASE2ST_START
;
1198 iph2
->flags
= isakmp
->flags
;
1199 iph2
->msgid
= isakmp
->msgid
;
1200 iph2
->seq
= pk_getseq();
1201 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1202 if (iph2
->ivm
== NULL
) {
1206 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
1207 if (iph2
->dst
== NULL
) {
1211 switch (iph2
->dst
->sa_family
) {
1213 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1217 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1221 plog(LLV_ERROR
, LOCATION
, NULL
,
1222 "invalid family: %d\n", iph2
->dst
->sa_family
);
1227 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1228 if (iph2
->src
== NULL
) {
1232 switch (iph2
->src
->sa_family
) {
1234 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1238 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1242 plog(LLV_ERROR
, LOCATION
, NULL
,
1243 "invalid family: %d\n", iph2
->src
->sa_family
);
1248 /* add new entry to isakmp status table */
1250 bindph12(iph1
, iph2
);
1252 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1256 a
= strdup(saddr2str(iph2
->src
));
1257 plog(LLV_INFO
, LOCATION
, NULL
,
1258 "respond new phase 2 negotiation: %s<=>%s\n",
1259 a
, saddr2str(iph2
->dst
));
1264 gettimeofday(&start
, NULL
);
1267 error
= (ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1269 [iph2
->status
])(iph2
, msg
);
1271 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1272 "failed to pre-process packet.\n");
1273 if (error
!= ISAKMP_INTERNAL_ERROR
)
1274 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
1276 * release handler because it's wrong that ph2handle is kept
1277 * after failed to check message for responder's.
1286 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1287 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
1289 [iph2
->status
])(iph2
, msg
) < 0) {
1290 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1291 "failed to process packet.\n");
1292 /* don't release handler */
1296 gettimeofday(&end
, NULL
);
1297 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1299 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
1300 timedelta(&start
, &end
));
1307 * parse ISAKMP payloads, without ISAKMP base header.
1310 isakmp_parsewoh(np0
, gen
, len
)
1312 struct isakmp_gen
*gen
;
1315 u_char np
= np0
& 0xff;
1318 struct isakmp_parse_t
*p
, *ep
;
1320 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
1323 * 5 is a magic number, but any value larger than 2 should be fine
1324 * as we do vrealloc() in the following loop.
1326 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1327 if (result
== NULL
) {
1328 plog(LLV_ERROR
, LOCATION
, NULL
,
1329 "failed to get buffer.\n");
1332 p
= (struct isakmp_parse_t
*)result
->v
;
1333 ep
= (struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1337 /* parse through general headers */
1338 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1339 if (tlen
<= sizeof(struct isakmp_gen
)) {
1340 /* don't send information, see isakmp_ident_r1() */
1341 plog(LLV_ERROR
, LOCATION
, NULL
,
1342 "invalid length of payload\n");
1347 plog(LLV_DEBUG
, LOCATION
, NULL
,
1348 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1351 p
->len
= ntohs(gen
->len
);
1352 if (p
->len
== 0 || p
->len
> tlen
) {
1353 plog(LLV_DEBUG
, LOCATION
, NULL
,
1354 "invalid length of payload\n");
1363 off
= p
- (struct isakmp_parse_t
*)result
->v
;
1364 result
= vrealloc(result
, result
->l
* 2);
1365 if (result
== NULL
) {
1366 plog(LLV_DEBUG
, LOCATION
, NULL
,
1367 "failed to realloc buffer.\n");
1371 ep
= (struct isakmp_parse_t
*)
1372 (result
->v
+ result
->l
- sizeof(*ep
));
1373 p
= (struct isakmp_parse_t
*)result
->v
;
1378 plen
= ntohs(gen
->len
);
1379 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1382 p
->type
= ISAKMP_NPTYPE_NONE
;
1386 plog(LLV_DEBUG
, LOCATION
, NULL
, "succeed.\n");
1392 * parse ISAKMP payloads, including ISAKMP base header.
1398 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1399 struct isakmp_gen
*gen
;
1405 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1406 tlen
= buf
->l
- sizeof(struct isakmp
);
1407 result
= isakmp_parsewoh(np
, gen
, tlen
);
1416 /* initialize a isakmp status table */
1422 if (isakmp_open() < 0)
1433 * make strings containing i_cookie + r_cookie + msgid
1436 isakmp_pindex(index
, msgid
)
1437 const isakmp_index
*index
;
1438 const u_int32_t msgid
;
1440 static char buf
[64];
1444 memset(buf
, 0, sizeof(buf
));
1447 p
= (const u_char
*)index
;
1448 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1449 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1461 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1467 isakmp_setup_socket(struct sockaddr
* in_addr
)
1474 if ((sock
= socket(in_addr
->sa_family
, SOCK_DGRAM
, 0)) < 0) {
1475 plog(LLV_ERROR
, LOCATION
, NULL
,
1476 "socket (%s)\n", strerror(errno
));
1480 /* receive my interface address on inbound packets. */
1481 switch (in_addr
->sa_family
) {
1483 if (setsockopt(sock
, IPPROTO_IP
, IP_RECVDSTADDR
,
1484 (const void *)&yes
, sizeof(yes
)) < 0) {
1485 plog(LLV_ERROR
, LOCATION
, NULL
,
1486 "setsockopt (%s)\n", strerror(errno
));
1494 #ifdef IPV6_RECVPKTINFO
1495 pktinfo
= IPV6_RECVPKTINFO
;
1496 #else /* old adv. API */
1497 pktinfo
= IPV6_PKTINFO
;
1498 #endif /* IPV6_RECVPKTINFO */
1500 pktinfo
= IPV6_RECVDSTADDR
;
1502 if (setsockopt(sock
, IPPROTO_IPV6
, pktinfo
,
1503 (const void *)&yes
, sizeof(yes
)) < 0)
1505 plog(LLV_ERROR
, LOCATION
, NULL
,
1506 "setsockopt(%d): %s\n",
1507 pktinfo
, strerror(errno
));
1515 #ifdef IPV6_USE_MIN_MTU
1516 if (in_addr
->sa_family
== AF_INET6
&&
1517 setsockopt(sock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1518 (void *)&yes
, sizeof(yes
)) < 0) {
1519 plog(LLV_ERROR
, LOCATION
, NULL
,
1520 "setsockopt (%s)\n", strerror(errno
));
1526 if (setsockopt_bypass(sock
, in_addr
->sa_family
) < 0) {
1531 if (bind(sock
, in_addr
, in_addr
->sa_len
) < 0) {
1532 plog(LLV_ERROR
, LOCATION
, in_addr
,
1533 "failed to bind (%s).\n", strerror(errno
));
1541 /* open ISAKMP sockets. */
1549 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1553 /* warn if wildcard address - should we forbid this? */
1554 switch (p
->addr
->sa_family
) {
1556 if (((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
== 0)
1557 plog(LLV_WARNING
, LOCATION
, NULL
,
1558 "listening to wildcard address,"
1559 "broadcast IKE packet may kill you\n");
1563 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
))
1564 plog(LLV_WARNING
, LOCATION
, NULL
,
1565 "listening to wildcard address, "
1566 "broadcast IKE packet may kill you\n");
1570 plog(LLV_ERROR
, LOCATION
, NULL
,
1571 "unsupported address family %d\n",
1572 lcconf
->default_af
);
1576 p
->sock
= isakmp_setup_socket(p
->addr
);
1577 if (p
->sock
< 0) goto err_and_next
;
1579 plog(LLV_DEBUG
, LOCATION
, NULL
,
1580 "%s used as isakmp port (fd=%d)\n",
1581 saddr2str(p
->addr
), p
->sock
);
1587 * We have to listen on 4500 in addition to 500 with IPv4
1588 * to support NAT traversal.
1590 if (p
->addr
->sa_family
== AF_INET
)
1592 struct sockaddr_in sin
= *(struct sockaddr_in
*)p
->addr
;
1594 sin
.sin_port
= ntohs(PORT_ISAKMP_NATT
);
1595 p
->nattsock
= isakmp_setup_socket((struct sockaddr
*)&sin
);
1596 if (p
->nattsock
>= 0)
1598 plog(LLV_DEBUG
, LOCATION
, NULL
,
1599 "%s used as nat-t isakmp port (fd=%d)\n",
1600 saddr2str((struct sockaddr
*)&sin
), p
->nattsock
);
1608 racoon_free(p
->addr
);
1610 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
)
1616 plog(LLV_ERROR
, LOCATION
, NULL
,
1617 "no address could be bound.\n");
1627 struct myaddrs
*p
, *next
;
1629 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
1638 if (p
->nattsock
>= 0) close(p
->nattsock
);
1640 racoon_free(p
->addr
);
1644 lcconf
->myaddrs
= NULL
;
1648 isakmp_send(iph1
, sbuf
)
1649 struct ph1handle
*iph1
;
1654 vchar_t
*newbuf
= NULL
;
1656 /* select the socket to be sent */
1657 s
= getsockmyaddr(iph1
->local
);
1662 /* prepend four bytes of zeros if source or destination port is PORT_ISAKMP_NATT */
1663 if (iph1
->remote
->sa_family
== AF_INET
&&
1664 (((struct sockaddr_in
*)(iph1
->remote
))->sin_port
== htons(PORT_ISAKMP_NATT
)) ||
1665 ((struct sockaddr_in
*)(iph1
->local
))->sin_port
== htons(PORT_ISAKMP_NATT
))
1668 /* There's probably a better way to do this */
1669 newbuf
= vmalloc(sbuf
->l
+ 4);
1670 if (newbuf
== NULL
) {
1671 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto natt prepend failed\n");
1675 memset(newbuf
->v
, 0, 4);
1676 memcpy(newbuf
->v
+ 4, sbuf
->v
, sbuf
->l
);
1681 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1682 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1684 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto failed\n");
1688 if (newbuf
) vfree(newbuf
);
1693 /* called from scheduler */
1695 isakmp_ph1resend_stub(p
)
1698 (void)isakmp_ph1resend((struct ph1handle
*)p
);
1702 isakmp_ph1resend(iph1
)
1703 struct ph1handle
*iph1
;
1705 if (iph1
->retry_counter
< 0) {
1706 plog(LLV_ERROR
, LOCATION
, NULL
,
1707 "phase1 negotiation failed due to time up. %s\n",
1708 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1715 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0)
1718 plog(LLV_DEBUG
, LOCATION
, NULL
,
1719 "resend phase1 packet %s\n",
1720 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1722 iph1
->retry_counter
--;
1724 iph1
->scr
= sched_new(iph1
->rmconf
->retry_interval
,
1725 isakmp_ph1resend_stub
, iph1
);
1730 /* called from scheduler */
1732 isakmp_ph2resend_stub(p
)
1736 (void)isakmp_ph2resend((struct ph2handle
*)p
);
1740 isakmp_ph2resend(iph2
)
1741 struct ph2handle
*iph2
;
1743 if (iph2
->retry_counter
< 0) {
1744 plog(LLV_ERROR
, LOCATION
, NULL
,
1745 "phase2 negotiation failed due to time up. %s\n",
1746 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1753 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0)
1756 plog(LLV_DEBUG
, LOCATION
, NULL
,
1757 "resend phase2 packet %s\n",
1758 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1760 iph2
->retry_counter
--;
1762 iph2
->scr
= sched_new(iph2
->ph1
->rmconf
->retry_interval
,
1763 isakmp_ph2resend_stub
, iph2
);
1768 /* called from scheduler */
1770 isakmp_ph1expire_stub(p
)
1774 isakmp_ph1expire((struct ph1handle
*)p
);
1778 isakmp_ph1expire(iph1
)
1779 struct ph1handle
*iph1
;
1783 src
= strdup(saddr2str(iph1
->local
));
1784 dst
= strdup(saddr2str(iph1
->remote
));
1785 plog(LLV_INFO
, LOCATION
, NULL
,
1786 "ISAKMP-SA expired %s-%s spi:%s\n",
1788 isakmp_pindex(&iph1
->index
, 0));
1792 SCHED_KILL(iph1
->sce
);
1794 iph1
->status
= PHASE1ST_EXPIRED
;
1797 * the phase1 deletion is postponed until there is no phase2.
1799 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
1800 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
1804 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
1807 /* called from scheduler */
1809 isakmp_ph1delete_stub(p
)
1813 isakmp_ph1delete((struct ph1handle
*)p
);
1817 isakmp_ph1delete(iph1
)
1818 struct ph1handle
*iph1
;
1822 SCHED_KILL(iph1
->sce
);
1824 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
1825 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
1829 /* don't re-negosiation when the phase 1 SA expires. */
1831 src
= strdup(saddr2str(iph1
->local
));
1832 dst
= strdup(saddr2str(iph1
->remote
));
1833 plog(LLV_INFO
, LOCATION
, NULL
,
1834 "ISAKMP-SA deleted %s-%s spi:%s\n",
1835 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
1845 /* called from scheduler.
1846 * this function will call only isakmp_ph2delete().
1847 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
1848 * by something cause. That's why this function is called after phase 2 SA
1849 * expires in the userland.
1852 isakmp_ph2expire_stub(p
)
1856 isakmp_ph2expire((struct ph2handle
*)p
);
1860 isakmp_ph2expire(iph2
)
1861 struct ph2handle
*iph2
;
1865 SCHED_KILL(iph2
->sce
);
1867 src
= strdup(saddrwop2str(iph2
->src
));
1868 dst
= strdup(saddrwop2str(iph2
->dst
));
1869 plog(LLV_INFO
, LOCATION
, NULL
,
1870 "phase2 sa expired %s-%s\n", src
, dst
);
1874 iph2
->status
= PHASE2ST_EXPIRED
;
1876 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
1881 /* called from scheduler */
1883 isakmp_ph2delete_stub(p
)
1887 isakmp_ph2delete((struct ph2handle
*)p
);
1891 isakmp_ph2delete(iph2
)
1892 struct ph2handle
*iph2
;
1896 SCHED_KILL(iph2
->sce
);
1898 src
= strdup(saddrwop2str(iph2
->src
));
1899 dst
= strdup(saddrwop2str(iph2
->dst
));
1900 plog(LLV_INFO
, LOCATION
, NULL
,
1901 "phase2 sa deleted %s-%s\n", src
, dst
);
1913 * Interface between PF_KEYv2 and ISAKMP
1916 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
1917 * if phase1 has been finished, begin phase2.
1920 isakmp_post_acquire(iph2
)
1921 struct ph2handle
*iph2
;
1923 struct remoteconf
*rmconf
;
1924 struct ph1handle
*iph1
= NULL
;
1926 /* search appropreate configuration with masking port. */
1927 rmconf
= getrmconf(iph2
->dst
);
1928 if (rmconf
== NULL
) {
1929 plog(LLV_ERROR
, LOCATION
, NULL
,
1930 "no configuration found for %s.\n",
1931 saddrwop2str(iph2
->dst
));
1935 /* if passive mode, ignore the acquire message */
1936 if (rmconf
->passive
) {
1937 plog(LLV_DEBUG
, LOCATION
, NULL
,
1938 "because of passive mode, "
1939 "ignore the acquire message for %s.\n",
1940 saddrwop2str(iph2
->dst
));
1944 /* search isakmp status table by address with masking port */
1945 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
1947 /* no ISAKMP-SA found. */
1951 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
1952 sc
= sched_new(1, isakmp_chkph1there_stub
, iph2
);
1953 plog(LLV_INFO
, LOCATION
, NULL
,
1954 "IPsec-SA request for %s queued "
1955 "due to no phase1 found.\n",
1956 saddrwop2str(iph2
->dst
));
1958 /* start phase 1 negotiation as a initiator. */
1959 if (isakmp_ph1begin_i(rmconf
, iph2
->dst
) < 0) {
1968 /* found ISAKMP-SA, but on negotiation. */
1969 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
1970 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
1971 sched_new(1, isakmp_chkph1there_stub
, iph2
);
1972 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
1973 "request for establishing IPsec-SA was queued "
1974 "due to no phase1 found.\n");
1979 /* found established ISAKMP-SA */
1980 /* i.e. iph1->status == PHASE1ST_ESTABLISHED */
1982 /* found ISAKMP-SA. */
1983 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
1985 /* begin quick mode */
1986 if (isakmp_ph2begin_i(iph1
, iph2
))
1993 * receive GETSPI from kernel.
1996 isakmp_post_getspi(iph2
)
1997 struct ph2handle
*iph2
;
2000 struct timeval start
, end
;
2003 /* don't process it because there is no suitable phase1-sa. */
2004 if (iph2
->ph1
->status
== PHASE2ST_EXPIRED
) {
2005 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
2006 "the negotiation is stopped, "
2007 "because there is no suitable ISAKMP-SA.\n");
2012 gettimeofday(&start
, NULL
);
2014 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
2016 [iph2
->status
])(iph2
, NULL
) != 0)
2019 gettimeofday(&end
, NULL
);
2020 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
2022 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2023 timedelta(&start
, &end
));
2029 /* called by scheduler */
2031 isakmp_chkph1there_stub(p
)
2034 isakmp_chkph1there((struct ph2handle
*)p
);
2038 isakmp_chkph1there(iph2
)
2039 struct ph2handle
*iph2
;
2041 struct ph1handle
*iph1
;
2043 iph2
->retry_checkph1
--;
2044 if (iph2
->retry_checkph1
< 0) {
2045 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
2046 "phase2 negotiation failed "
2047 "due to time up waiting for phase1. %s\n",
2048 sadbsecas2str(iph2
->dst
, iph2
->src
,
2049 iph2
->satype
, 0, 0));
2050 plog(LLV_INFO
, LOCATION
, NULL
,
2051 "delete phase 2 handler.\n");
2053 /* send acquire to kernel as error */
2054 pk_sendeacquire(iph2
);
2063 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
2065 /* XXX Even if ph1 as responder is there, should we not start
2066 * phase 2 negotiation ? */
2068 && iph1
->status
== PHASE1ST_ESTABLISHED
) {
2069 /* found isakmp-sa */
2070 /* begin quick mode */
2071 (void)isakmp_ph2begin_i(iph1
, iph2
);
2075 /* no isakmp-sa found */
2076 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2081 /* copy variable data into ALLOCATED buffer. */
2083 isakmp_set_attr_v(buf
, type
, val
, len
)
2089 struct isakmp_data
*data
;
2091 data
= (struct isakmp_data
*)buf
;
2092 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2093 data
->lorv
= htons((u_int16_t
)len
);
2094 memcpy(data
+ 1, val
, len
);
2096 return buf
+ sizeof(*data
) + len
;
2099 /* copy fixed length data into ALLOCATED buffer. */
2101 isakmp_set_attr_l(buf
, type
, val
)
2106 struct isakmp_data
*data
;
2108 data
= (struct isakmp_data
*)buf
;
2109 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2110 data
->lorv
= htons((u_int16_t
)val
);
2112 return buf
+ sizeof(*data
);
2115 /* add a variable data attribute to the buffer by reallocating it. */
2117 isakmp_add_attr_v(buf0
, type
, val
, len
)
2123 vchar_t
*buf
= NULL
;
2124 struct isakmp_data
*data
;
2128 tlen
= sizeof(*data
) + len
;
2132 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2134 buf
= vmalloc(tlen
);
2136 plog(LLV_ERROR
, LOCATION
, NULL
,
2137 "failed to get a attribute buffer.\n");
2141 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2142 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2143 data
->lorv
= htons((u_int16_t
)len
);
2144 memcpy(data
+ 1, val
, len
);
2149 /* add a fixed data attribute to the buffer by reallocating it. */
2151 isakmp_add_attr_l(buf0
, type
, val
)
2156 vchar_t
*buf
= NULL
;
2157 struct isakmp_data
*data
;
2161 tlen
= sizeof(*data
);
2165 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2167 buf
= vmalloc(tlen
);
2169 plog(LLV_ERROR
, LOCATION
, NULL
,
2170 "failed to get a attribute buffer.\n");
2174 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2175 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2176 data
->lorv
= htons((u_int16_t
)val
);
2182 * calculate cookie and set.
2185 isakmp_newcookie(place
, remote
, local
)
2187 struct sockaddr
*remote
;
2188 struct sockaddr
*local
;
2190 vchar_t
*buf
= NULL
, *buf2
= NULL
;
2200 if (remote
->sa_family
!= local
->sa_family
) {
2201 plog(LLV_ERROR
, LOCATION
, NULL
,
2202 "address family mismatch, remote:%d local:%d\n",
2203 remote
->sa_family
, local
->sa_family
);
2206 switch (remote
->sa_family
) {
2208 alen
= sizeof(struct in_addr
);
2209 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
2210 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
2214 alen
= sizeof(struct in_addr
);
2215 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
2216 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
2220 plog(LLV_ERROR
, LOCATION
, NULL
,
2221 "invalid family: %d\n", remote
->sa_family
);
2224 blen
= (alen
+ sizeof(u_short
)) * 2
2225 + sizeof(time_t) + lcconf
->secret_size
;
2226 buf
= vmalloc(blen
);
2228 plog(LLV_ERROR
, LOCATION
, NULL
,
2229 "failed to get a cookie.\n");
2234 /* copy my address */
2235 memcpy(p
, sa1
, alen
);
2237 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
2238 memcpy(p
, &port
, sizeof(u_short
));
2239 p
+= sizeof(u_short
);
2241 /* copy target address */
2242 memcpy(p
, sa2
, alen
);
2244 port
= ((struct sockaddr_in
*)local
)->sin_port
;
2245 memcpy(p
, &port
, sizeof(u_short
));
2246 p
+= sizeof(u_short
);
2250 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
2253 /* copy random value */
2254 buf2
= eay_set_random(lcconf
->secret_size
);
2257 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
2258 p
+= lcconf
->secret_size
;
2261 buf2
= eay_sha1_one(buf
);
2262 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
2264 sa1
= val2str(place
, sizeof (cookie_t
));
2265 plog(LLV_DEBUG
, LOCATION
, NULL
, "new cookie:\n%s\n", sa1
);
2278 * save partner's(payload) data into phhandle.
2281 isakmp_p2ph(buf
, gen
)
2283 struct isakmp_gen
*gen
;
2285 /* XXX to be checked in each functions for logging. */
2287 plog(LLV_WARNING
, LOCATION
, NULL
,
2288 "ignore this payload, same payload type exist.\n");
2292 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2294 plog(LLV_ERROR
, LOCATION
, NULL
,
2295 "failed to get buffer.\n");
2298 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2304 isakmp_newmsgid2(iph1
)
2305 struct ph1handle
*iph1
;
2310 msgid2
= arc4random();
2311 } while (getph2bymsgid(iph1
, msgid2
));
2317 * set values into allocated buffer of isakmp header for phase 1
2320 set_isakmp_header(vbuf
, iph1
, nptype
)
2322 struct ph1handle
*iph1
;
2325 struct isakmp
*isakmp
;
2327 if (vbuf
->l
< sizeof(*isakmp
))
2330 isakmp
= (struct isakmp
*)vbuf
->v
;
2331 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2332 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2333 isakmp
->np
= nptype
;
2334 isakmp
->v
= iph1
->version
;
2335 isakmp
->etype
= iph1
->etype
;
2336 isakmp
->flags
= iph1
->flags
;
2337 isakmp
->msgid
= iph1
->msgid
;
2338 isakmp
->len
= htonl(vbuf
->l
);
2340 return vbuf
->v
+ sizeof(*isakmp
);
2344 * set values into allocated buffer of isakmp header for phase 2
2347 set_isakmp_header2(vbuf
, iph2
, nptype
)
2349 struct ph2handle
*iph2
;
2352 struct isakmp
*isakmp
;
2354 if (vbuf
->l
< sizeof(*isakmp
))
2357 isakmp
= (struct isakmp
*)vbuf
->v
;
2358 memcpy(&isakmp
->i_ck
, &iph2
->ph1
->index
.i_ck
, sizeof(cookie_t
));
2359 memcpy(&isakmp
->r_ck
, &iph2
->ph1
->index
.r_ck
, sizeof(cookie_t
));
2360 isakmp
->np
= nptype
;
2361 isakmp
->v
= iph2
->ph1
->version
;
2362 isakmp
->etype
= ISAKMP_ETYPE_QUICK
;
2363 isakmp
->flags
= iph2
->flags
;
2364 memcpy(&isakmp
->msgid
, &iph2
->msgid
, sizeof(isakmp
->msgid
));
2365 isakmp
->len
= htonl(vbuf
->l
);
2367 return vbuf
->v
+ sizeof(*isakmp
);
2371 * set values into allocated buffer of isakmp payload.
2374 set_isakmp_payload(buf
, src
, nptype
)
2379 struct isakmp_gen
*gen
;
2382 plog(LLV_DEBUG
, LOCATION
, NULL
, "add payload of len %d, next type %d\n",
2385 gen
= (struct isakmp_gen
*)p
;
2387 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2389 memcpy(p
, src
->v
, src
->l
);
2400 case ISAKMP_ETYPE_IDENT
:
2402 case ISAKMP_ETYPE_AGG
:
2404 case ISAKMP_ETYPE_BASE
:
2417 case ISAKMP_ETYPE_QUICK
:
2425 #ifdef HAVE_PRINT_ISAKMP_C
2426 /* for print-isakmp.c */
2428 extern void isakmp_print
__P((const u_char
*, u_int
, const u_char
*));
2430 char *getname
__P((const u_char
*));
2432 char *getname6
__P((const u_char
*));
2434 int safeputchar
__P((int));
2437 * Return a name for the IP address pointed to by ap. This address
2438 * is assumed to be in network byte order.
2444 struct sockaddr_in addr
;
2445 static char ntop_buf
[NI_MAXHOST
];
2447 memset(&addr
, 0, sizeof(addr
));
2448 addr
.sin_len
= sizeof(struct sockaddr_in
);
2449 addr
.sin_family
= AF_INET
;
2450 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2451 if (getnameinfo((struct sockaddr
*)&addr
, addr
.sin_len
,
2452 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2453 NI_NUMERICHOST
| niflags
))
2454 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2461 * Return a name for the IP6 address pointed to by ap. This address
2462 * is assumed to be in network byte order.
2468 struct sockaddr_in6 addr
;
2469 static char ntop_buf
[NI_MAXHOST
];
2471 memset(&addr
, 0, sizeof(addr
));
2472 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2473 addr
.sin6_family
= AF_INET6
;
2474 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2475 if (getnameinfo((struct sockaddr
*)&addr
, addr
.sin6_len
,
2476 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2477 NI_NUMERICHOST
| niflags
))
2478 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2490 ch
= (unsigned char)(c
& 0xff);
2491 if (c
< 0x80 && isprint(c
))
2492 return printf("%c", c
& 0xff);
2494 return printf("\\%03o", c
& 0xff);
2498 isakmp_printpacket(msg
, from
, my
, decoded
)
2500 struct sockaddr
*from
;
2501 struct sockaddr
*my
;
2507 char hostbuf
[NI_MAXHOST
];
2508 char portbuf
[NI_MAXSERV
];
2509 struct isakmp
*isakmp
;
2513 if (loglevel
< LLV_DEBUG
)
2517 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
2519 gettimeofday(&tv
, NULL
);
2520 s
= tv
.tv_sec
% 3600;
2521 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2524 if (getnameinfo(from
, from
->sa_len
, hostbuf
, sizeof(hostbuf
),
2525 portbuf
, sizeof(portbuf
),
2526 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2527 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2528 strlcpy(portbuf
, "?", sizeof(portbuf
));
2530 printf("%s:%s", hostbuf
, portbuf
);
2535 if (getnameinfo(my
, my
->sa_len
, hostbuf
, sizeof(hostbuf
),
2536 portbuf
, sizeof(portbuf
),
2537 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2538 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2539 strlcpy(portbuf
, "?", sizeof(portbuf
));
2541 printf("%s:%s", hostbuf
, portbuf
);
2548 printf("(malloc fail)\n");
2552 isakmp
= (struct isakmp
*)buf
->v
;
2553 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
2556 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
2557 if (buf
->l
< pad
&& 2 < vflag
)
2558 printf("(wrong padding)");
2560 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
2564 snapend
= buf
->v
+ buf
->l
;
2565 isakmp_print(buf
->v
, buf
->l
, NULL
);
2573 #endif /*HAVE_PRINT_ISAKMP_C*/
2576 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
2577 struct ph1handle
*iph1
;
2578 struct remoteconf
*rmconf
;
2579 struct sockaddr
*remote
, *local
;
2581 u_short
*port
= NULL
;
2583 /* address portion must be grabbed from real remote address "remote" */
2584 iph1
->remote
= dupsaddr(remote
);
2585 if (iph1
->remote
== NULL
) {
2591 * if remote has no port # (in case of initiator - from ACQUIRE msg)
2592 * - if remote.conf specifies port #, use that
2593 * - if remote.conf does not, use 500
2594 * if remote has port # (in case of responder - from recvfrom(2))
2595 * respect content of "remote".
2597 switch (iph1
->remote
->sa_family
) {
2599 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
2602 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
2605 *port
= htons(PORT_ISAKMP
);
2609 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
2612 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
2615 *port
= htons(PORT_ISAKMP
);
2619 plog(LLV_ERROR
, LOCATION
, NULL
,
2620 "invalid family: %d\n", iph1
->remote
->sa_family
);
2625 iph1
->local
= getlocaladdr(iph1
->remote
);
2627 iph1
->local
= dupsaddr(local
);
2628 if (iph1
->local
== NULL
) {
2632 switch (iph1
->local
->sa_family
) {
2634 ((struct sockaddr_in
*)iph1
->local
)->sin_port
2635 = getmyaddrsport(iph1
->local
);
2639 ((struct sockaddr_in6
*)iph1
->local
)->sin6_port
2640 = getmyaddrsport(iph1
->local
);
2644 plog(LLV_ERROR
, LOCATION
, NULL
,
2645 "invalid family: %d\n", iph1
->remote
->sa_family
);
2655 struct ph1handle
*iph1
;
2658 plog(LLV_ERROR
, LOCATION
, iph1
->remote
, "wrong state %u.\n",
2665 struct ph2handle
*iph2
;
2668 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
, "wrong state %u.\n",
2674 log_ph1established(iph1
)
2675 const struct ph1handle
*iph1
;
2679 src
= strdup(saddr2str(iph1
->local
));
2680 dst
= strdup(saddr2str(iph1
->remote
));
2681 plog(LLV_INFO
, LOCATION
, NULL
,
2682 "ISAKMP-SA established %s-%s spi:%s\n",
2684 isakmp_pindex(&iph1
->index
, 0));