1 /* $Id: isakmp.c,v 1.34.2.21 2006/02/02 10:31:01 vanhu 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 #define __APPLE_API_PRIVATE
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/queue.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
45 #ifndef HAVE_NETINET6_IPSEC
46 #include <netinet/ipsec.h>
48 #include <netinet6/ipsec.h>
55 #if TIME_WITH_SYS_TIME
56 # include <sys/time.h>
60 # include <sys/time.h>
80 #include "remoteconf.h"
81 #include "localconf.h"
82 #include "grabmyaddr.h"
85 #include "isakmp_var.h"
91 #include "ipsec_doi.h"
93 #include "crypto_openssl.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"
101 #include "vpn_control.h"
102 #include "vpn_control_var.h"
104 #include "isakmp_xauth.h"
105 #include "isakmp_cfg.h"
108 #include "isakmp_frag.h"
110 #include "strnames.h"
113 # include "nattraversal.h"
115 # include <linux/udp.h>
121 # endif /* __linux__ */
122 # if defined(__NetBSD__) || defined(__FreeBSD__)
123 # include <netinet/in.h>
124 # include <netinet/udp.h>
125 # define SOL_UDP IPPROTO_UDP
126 # endif /* __NetBSD__ / __FreeBSD__ */
129 static int nostate1
__P((struct ph1handle
*, vchar_t
*));
130 static int nostate2
__P((struct ph2handle
*, vchar_t
*));
132 extern caddr_t
val2str(const char *, size_t);
134 static int (*ph1exchange
[][2][PHASE1ST_MAX
])
135 __P((struct ph1handle
*, vchar_t
*)) = {
138 /* Identity Protection exchange */
140 { nostate1
, ident_i1send
, nostate1
, ident_i2recv
, ident_i2send
,
141 ident_i3recv
, ident_i3send
, ident_i4recv
, ident_i4send
, nostate1
, },
142 { nostate1
, ident_r1recv
, ident_r1send
, ident_r2recv
, ident_r2send
,
143 ident_r3recv
, ident_r3send
, nostate1
, nostate1
, nostate1
, },
145 /* Aggressive exchange */
147 { nostate1
, agg_i1send
, nostate1
, agg_i2recv
, agg_i2send
,
148 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
149 { nostate1
, agg_r1recv
, agg_r1send
, agg_r2recv
, agg_r2send
,
150 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
154 { nostate1
, base_i1send
, nostate1
, base_i2recv
, base_i2send
,
155 base_i3recv
, base_i3send
, nostate1
, nostate1
, nostate1
, },
156 { nostate1
, base_r1recv
, base_r1send
, base_r2recv
, base_r2send
,
157 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
161 static int (*ph2exchange
[][2][PHASE2ST_MAX
])
162 __P((struct ph2handle
*, vchar_t
*)) = {
165 /* Quick mode for IKE*/
167 { nostate2
, nostate2
, quick_i1prep
, nostate2
, quick_i1send
,
168 quick_i2recv
, quick_i2send
, quick_i3recv
, nostate2
, nostate2
, },
169 { nostate2
, quick_r1recv
, quick_r1prep
, nostate2
, quick_r2send
,
170 quick_r3recv
, quick_r3prep
, quick_r3send
, nostate2
, nostate2
, }
174 static u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
176 static int isakmp_main
__P((vchar_t
*, struct sockaddr
*, struct sockaddr
*));
177 static int ph1_main
__P((struct ph1handle
*, vchar_t
*));
178 static int quick_main
__P((struct ph2handle
*, vchar_t
*));
179 static int isakmp_ph1begin_r
__P((vchar_t
*,
180 struct sockaddr
*, struct sockaddr
*, u_int8_t
));
181 static int isakmp_ph2begin_i
__P((struct ph1handle
*, struct ph2handle
*));
182 static int isakmp_ph2begin_r
__P((struct ph1handle
*, vchar_t
*));
183 static int etypesw1
__P((int));
184 static int etypesw2
__P((int));
186 static int frag_handler(struct ph1handle
*,
187 vchar_t
*, struct sockaddr
*, struct sockaddr
*);
191 * isakmp packet handler
194 isakmp_handler(so_isakmp
)
197 struct isakmp isakmp
;
199 char buf
[sizeof (isakmp
) + 4];
200 u_int32_t non_esp
[2];
202 struct sockaddr_storage remote
;
203 struct sockaddr_storage local
;
204 unsigned int remote_len
= sizeof(remote
);
205 unsigned int local_len
= sizeof(local
);
206 int len
= 0, extralen
= 0;
208 vchar_t
*buf
= NULL
, *tmpbuf
= NULL
;
211 /* read message by MSG_PEEK */
212 while ((len
= recvfromto(so_isakmp
, x
.buf
, sizeof(x
),
213 MSG_PEEK
, (struct sockaddr
*)&remote
, &remote_len
,
214 (struct sockaddr
*)&local
, &local_len
)) < 0) {
217 plog(LLV_ERROR
, LOCATION
, NULL
,
218 "failed to receive isakmp packet: %s\n",
223 /* keep-alive packet - ignore */
224 if (len
== 1 && (x
.buf
[0]&0xff) == 0xff) {
225 /* Pull the keep-alive packet */
226 if ((len
= recvfrom(so_isakmp
, (char *)x
.buf
, 1,
227 0, (struct sockaddr
*)&remote
, &remote_len
)) != 1) {
228 plog(LLV_ERROR
, LOCATION
, NULL
,
229 "failed to receive keep alive packet: %s\n",
236 /* we don't know about portchange yet,
237 look for non-esp marker instead */
238 if (x
.non_esp
[0] == 0 && x
.non_esp
[1] != 0)
239 extralen
= NON_ESP_MARKER_LEN
;
242 /* now we know if there is an extra non-esp
243 marker at the beginning or not */
244 memcpy ((char *)&isakmp
, x
.buf
+ extralen
, sizeof (isakmp
));
246 /* check isakmp header length, as well as sanity of header length */
247 if (len
< sizeof(isakmp
) || ntohl(isakmp
.len
) < sizeof(isakmp
)) {
248 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
249 "packet shorter than isakmp header size (%u, %u, %zu)\n",
250 len
, ntohl(isakmp
.len
), sizeof(isakmp
));
252 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
253 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
254 plog(LLV_ERROR
, LOCATION
, NULL
,
255 "failed to receive isakmp packet: %s\n",
261 /* reject it if the size is tooooo big. */
262 if (ntohl(isakmp
.len
) > 0xffff) {
263 plog(LLV_ERROR
, LOCATION
, NULL
,
264 "the length in the isakmp header is too big.\n");
265 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
266 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
267 plog(LLV_ERROR
, LOCATION
, NULL
,
268 "failed to receive isakmp packet: %s\n",
274 /* read real message */
275 if ((tmpbuf
= vmalloc(ntohl(isakmp
.len
) + extralen
)) == NULL
) {
276 plog(LLV_ERROR
, LOCATION
, NULL
,
277 "failed to allocate reading buffer (%u Bytes)\n",
278 ntohl(isakmp
.len
) + extralen
);
280 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
281 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
282 plog(LLV_ERROR
, LOCATION
, NULL
,
283 "failed to receive isakmp packet: %s\n",
286 error
= -2; /* serious problem with socket */
292 while ((len
= recvfromto(so_isakmp
, (char *)tmpbuf
->v
, tmpbuf
->l
,
293 0, (struct sockaddr
*)&remote
, &remote_len
,
294 (struct sockaddr
*)&local
, &local_len
)) < 0) {
297 plog(LLV_ERROR
, LOCATION
, NULL
,
298 "failed to receive isakmp packet: %s\n",
303 if ((buf
= vmalloc(len
- extralen
)) == NULL
) {
304 plog(LLV_ERROR
, LOCATION
, NULL
,
305 "failed to allocate reading buffer (%u Bytes)\n",
310 memcpy (buf
->v
, tmpbuf
->v
+ extralen
, buf
->l
);
317 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
318 "received invalid length (%d != %zu), why ?\n",
323 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
324 plog(LLV_DEBUG
, LOCATION
, NULL
,
325 "%d bytes message received %s\n",
326 len
, saddr2str_fromto("from %s to %s",
327 (struct sockaddr
*)&remote
,
328 (struct sockaddr
*)&local
));
329 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
331 /* avoid packets with malicious port/address */
332 switch (remote
.ss_family
) {
334 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
338 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
342 plog(LLV_ERROR
, LOCATION
, NULL
,
343 "invalid family: %d\n", remote
.ss_family
);
347 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
348 "src port == 0 (valid as UDP but not with IKE)\n");
352 /* XXX: check sender whether to be allowed or not to accept */
354 /* XXX: I don't know how to check isakmp half connection attack. */
356 /* simply reply if the packet was processed. */
357 if (check_recvdpkt((struct sockaddr
*)&remote
,
358 (struct sockaddr
*)&local
, buf
)) {
359 plog(LLV_NOTIFY
, LOCATION
, NULL
,
360 "the packet is retransmitted by %s.\n",
361 saddr2str((struct sockaddr
*)&remote
));
366 /* isakmp main routine */
367 if (isakmp_main(buf
, (struct sockaddr
*)&remote
,
368 (struct sockaddr
*)&local
) != 0) goto end
;
380 * main processing to handle isakmp payload
383 isakmp_main(msg
, remote
, local
)
385 struct sockaddr
*remote
, *local
;
387 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
388 isakmp_index
*index
= (isakmp_index
*)isakmp
;
389 u_int32_t msgid
= isakmp
->msgid
;
390 struct ph1handle
*iph1
;
392 #ifdef HAVE_PRINT_ISAKMP_C
393 isakmp_printpacket(msg
, remote
, local
, 0);
396 /* the initiator's cookie must not be zero */
397 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
398 plog(LLV_ERROR
, LOCATION
, remote
,
399 "malformed cookie received.\n");
403 /* Check the Major and Minor Version fields. */
405 * XXX Is is right to check version here ?
406 * I think it may no be here because the version depends
407 * on exchange status.
409 if (isakmp
->v
< ISAKMP_VERSION_NUMBER
) {
410 if (ISAKMP_GETMAJORV(isakmp
->v
) < ISAKMP_MAJOR_VERSION
) {
411 plog(LLV_ERROR
, LOCATION
, remote
,
412 "invalid major version %d.\n",
413 ISAKMP_GETMAJORV(isakmp
->v
));
416 #if ISAKMP_MINOR_VERSION > 0
417 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) {
418 plog(LLV_ERROR
, LOCATION
, remote
,
419 "invalid minor version %d.\n",
420 ISAKMP_GETMINORV(isakmp
->v
));
426 /* check the Flags field. */
427 /* XXX How is the exclusive check, E and A ? */
428 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) {
429 plog(LLV_ERROR
, LOCATION
, remote
,
430 "invalid flag 0x%02x.\n", isakmp
->flags
);
434 /* ignore commit bit. */
435 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
436 if (isakmp
->msgid
== 0) {
437 isakmp_info_send_nx(isakmp
, remote
, local
,
438 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
439 plog(LLV_ERROR
, LOCATION
, remote
,
440 "Commit bit on phase1 forbidden.\n");
445 iph1
= getph1byindex(index
);
448 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
449 iph1
->side
== INITIATOR
) {
450 plog(LLV_DEBUG
, LOCATION
, remote
,
451 "malformed cookie received or "
452 "the initiator's cookies collide.\n");
457 /* Floating ports for NAT-T */
458 if (NATT_AVAILABLE(iph1
) &&
459 ! (iph1
->natt_flags
& NAT_PORTS_CHANGED
) &&
460 ((cmpsaddrstrict(iph1
->remote
, remote
) != 0) ||
461 (cmpsaddrstrict(iph1
->local
, local
) != 0)))
463 /* prevent memory leak */
464 racoon_free(iph1
->remote
);
465 racoon_free(iph1
->local
);
467 /* copy-in new addresses */
468 iph1
->remote
= dupsaddr(remote
);
469 iph1
->local
= dupsaddr(local
);
471 /* set the flag to prevent further port floating
472 (FIXME: should we allow it? E.g. when the NAT gw
474 iph1
->natt_flags
|= NAT_PORTS_CHANGED
| NAT_ADD_NON_ESP_MARKER
;
476 /* print some neat info */
477 plog (LLV_INFO
, LOCATION
, NULL
,
478 "NAT-T: ports changed to: %s\n",
479 saddr2str_fromto ("%s<->%s", iph1
->remote
, iph1
->local
));
481 natt_keepalive_add_ph1 (iph1
);
486 /* must be same addresses in one stream of a phase at least. */
487 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
488 char *saddr_db
, *saddr_act
;
490 saddr_db
= strdup(saddr2str(iph1
->remote
));
491 saddr_act
= strdup(saddr2str(remote
));
493 plog(LLV_WARNING
, LOCATION
, remote
,
494 "remote address mismatched. db=%s, act=%s\n",
495 saddr_db
, saddr_act
);
497 racoon_free(saddr_db
);
498 racoon_free(saddr_act
);
502 * don't check of exchange type here because other type will be
503 * with same index, for example, informational exchange.
506 /* XXX more acceptable check */
509 switch (isakmp
->etype
) {
510 case ISAKMP_ETYPE_IDENT
:
511 case ISAKMP_ETYPE_AGG
:
512 case ISAKMP_ETYPE_BASE
:
513 /* phase 1 validity check */
514 if (isakmp
->msgid
!= 0) {
515 plog(LLV_ERROR
, LOCATION
, remote
,
516 "message id should be zero in phase1.\n");
520 /* search for isakmp status record of phase 1 */
523 * the packet must be the 1st message from a initiator
524 * or the 2nd message from the responder.
527 /* search for phase1 handle by index without r_ck */
528 iph1
= getph1byindex0(index
);
530 /*it must be the 1st message from a initiator.*/
531 if (memcmp(&isakmp
->r_ck
, r_ck0
,
532 sizeof(cookie_t
)) != 0) {
534 plog(LLV_DEBUG
, LOCATION
, remote
,
535 "malformed cookie received "
536 "or the spi expired.\n");
540 /* it must be responder's 1st exchange. */
541 if (isakmp_ph1begin_r(msg
, remote
, local
,
549 /* it must be the 2nd message from the responder. */
550 if (iph1
->side
!= INITIATOR
) {
551 plog(LLV_DEBUG
, LOCATION
, remote
,
552 "malformed cookie received. "
553 "it has to be as the initiator. %s\n",
554 isakmp_pindex(&iph1
->index
, 0));
560 * Don't delete phase 1 handler when the exchange type
561 * in handler is not equal to packet's one because of no
562 * authencication completed.
564 if (iph1
->etype
!= isakmp
->etype
) {
565 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
566 "exchange type is mismatched: "
567 "db=%s packet=%s, ignore it.\n",
568 s_isakmp_etype(iph1
->etype
),
569 s_isakmp_etype(isakmp
->etype
));
574 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
575 return frag_handler(iph1
, msg
, remote
, local
);
578 /* call main process of phase 1 */
579 if (ph1_main(iph1
, msg
) < 0) {
580 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
581 "phase1 negotiation failed.\n");
588 case ISAKMP_ETYPE_AUTH
:
589 plog(LLV_INFO
, LOCATION
, remote
,
590 "unsupported exchange %d received.\n",
594 case ISAKMP_ETYPE_INFO
:
595 case ISAKMP_ETYPE_ACKINFO
:
597 * iph1 must be present for Information message.
598 * if iph1 is null then trying to get the phase1 status
599 * as the packet from responder againt initiator's 1st
600 * exchange in phase 1.
601 * NOTE: We think such informational exchange should be ignored.
604 iph1
= getph1byindex0(index
);
606 plog(LLV_ERROR
, LOCATION
, remote
,
607 "unknown Informational "
608 "exchange received.\n");
611 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
612 plog(LLV_WARNING
, LOCATION
, remote
,
613 "remote address mismatched. "
615 saddr2str(iph1
->remote
));
620 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
621 return frag_handler(iph1
, msg
, remote
, local
);
624 if (isakmp_info_recv(iph1
, msg
) < 0)
628 case ISAKMP_ETYPE_QUICK
:
630 struct ph2handle
*iph2
;
633 isakmp_info_send_nx(isakmp
, remote
, local
,
634 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
635 plog(LLV_ERROR
, LOCATION
, remote
,
636 "can't start the quick mode, "
637 "there is no ISAKMP-SA, %s\n",
638 isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
644 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
645 return frag_handler(iph1
, msg
, remote
, local
);
648 /* check status of phase 1 whether negotiated or not. */
649 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
650 plog(LLV_ERROR
, LOCATION
, remote
,
651 "can't start the quick mode, "
652 "there is no valid ISAKMP-SA, %s\n",
653 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
657 /* search isakmp phase 2 stauts record. */
658 iph2
= getph2bymsgid(iph1
, msgid
);
660 /* it must be new negotiation as responder */
661 if (isakmp_ph2begin_r(iph1
, msg
) < 0)
669 * we keep to set commit bit during negotiation.
670 * When SA is configured, bit will be reset.
672 * don't initiate commit bit. should be fixed in the future.
674 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
675 iph2
->flags
|= ISAKMP_FLAG_C
;
677 /* call main process of quick mode */
678 if (quick_main(iph2
, msg
) < 0) {
679 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
680 "phase2 negotiation failed.\n");
689 case ISAKMP_ETYPE_NEWGRP
:
691 plog(LLV_ERROR
, LOCATION
, remote
,
692 "Unknown new group mode exchange, "
693 "there is no ISAKMP-SA.\n");
698 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
699 return frag_handler(iph1
, msg
, remote
, local
);
702 isakmp_newgroup_r(iph1
, msg
);
706 case ISAKMP_ETYPE_CFG
:
708 plog(LLV_ERROR
, LOCATION
, NULL
,
709 "mode config %d from %s, "
710 "but we have no ISAKMP-SA.\n",
711 isakmp
->etype
, saddr2str(remote
));
716 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
717 return frag_handler(iph1
, msg
, remote
, local
);
720 isakmp_cfg_r(iph1
, msg
);
724 case ISAKMP_ETYPE_NONE
:
726 plog(LLV_ERROR
, LOCATION
, NULL
,
727 "Invalid exchange type %d from %s.\n",
728 isakmp
->etype
, saddr2str(remote
));
736 * main function of phase 1.
740 struct ph1handle
*iph1
;
745 struct timeval start
, end
;
748 /* ignore a packet */
749 if (iph1
->status
== PHASE1ST_ESTABLISHED
)
753 gettimeofday(&start
, NULL
);
756 if (ph1exchange
[etypesw1(iph1
->etype
)]
758 [iph1
->status
] == NULL
) {
759 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
760 "why isn't the function defined.\n");
763 error
= (ph1exchange
[etypesw1(iph1
->etype
)]
765 [iph1
->status
])(iph1
, msg
);
768 * When an invalid packet is received on phase1, it should
769 * be selected to process this packet. That is to respond
770 * with a notify and delete phase 1 handler, OR not to respond
771 * and keep phase 1 handler. However, in PHASE1ST_START when
772 * acting as RESPONDER we must not keep phase 1 handler or else
773 * it will stay forever.
776 if (iph1
->side
== RESPONDER
&& iph1
->status
== PHASE1ST_START
) {
777 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
778 "failed to pre-process packet.\n");
781 /* ignore the error and keep phase 1 handler */
786 /* free resend buffer */
787 if (iph1
->sendbuf
== NULL
) {
788 plog(LLV_ERROR
, LOCATION
, NULL
,
789 "no buffer found as sendbuf\n");
792 VPTRINIT(iph1
->sendbuf
);
794 /* turn off schedule */
796 SCHED_KILL(iph1
->scr
);
799 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
800 if ((ph1exchange
[etypesw1(iph1
->etype
)]
802 [iph1
->status
])(iph1
, msg
) != 0) {
803 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
804 "failed to process packet.\n");
809 gettimeofday(&end
, NULL
);
810 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
811 "phase1", s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
812 timedelta(&start
, &end
));
814 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
817 gettimeofday(&iph1
->end
, NULL
);
818 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
819 "phase1", s_isakmp_etype(iph1
->etype
),
820 timedelta(&iph1
->start
, &iph1
->end
));
823 #ifdef ENABLE_VPNCONTROL_PORT
825 if (iph1
->side
== RESPONDER
&&
826 iph1
->local
->sa_family
== AF_INET
) {
828 struct redirect
*addr
;
830 LIST_FOREACH(addr
, &lcconf
->redirect_addresses
, chain
) {
831 if (((struct sockaddr_in
*)iph1
->local
)->sin_addr
.s_addr
== addr
->cluster_address
) {
832 vchar_t
*raddr
= vmalloc(sizeof(u_int32_t
));
835 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
836 "failed to send redirect message - memory error.\n");
838 memcpy(raddr
->v
, &addr
->redirect_address
, sizeof(u_int32_t
));
839 (void)isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_LOAD_BALANCE
, raddr
);
840 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
, "sent redirect notification - address = %x.\n", ntohl(addr
->redirect_address
));
843 isakmp_ph1delete(iph1
);
850 /* save created date. */
851 (void)time(&iph1
->created
);
853 /* add to the schedule to expire, and save back pointer. */
854 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
855 isakmp_ph1expire_stub
, iph1
);
857 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
858 switch(iph1
->approval
->authmethod
) {
859 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
860 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
862 /* XXX Don't process INITIAL_CONTACT */
863 iph1
->rmconf
->ini_contact
= 0;
871 /* Schedule the r_u_there.... */
872 if(iph1
->dpd_support
&& iph1
->rmconf
->dpd_interval
)
873 isakmp_sched_r_u(iph1
, 0);
876 /* INITIAL-CONTACT processing */
877 /* don't send anything if local test mode. */
879 && iph1
->rmconf
->ini_contact
&& !getcontacted(iph1
->remote
)) {
880 /* send INITIAL-CONTACT */
881 isakmp_info_send_n1(iph1
,
882 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
883 /* insert a node into contacted list. */
884 if (inscontacted(iph1
->remote
) == -1) {
885 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
886 "failed to add contacted list.\n");
891 log_ph1established(iph1
);
892 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
894 #ifdef ENABLE_VPNCONTROL_PORT
895 vpncontrol_notify_phase_change(0, FROM_LOCAL
, iph1
, NULL
);
900 * SA up shell script hook: do it now,except if
901 * ISAKMP mode config was requested. In the later
902 * case it is done when we receive the configuration.
904 if ((iph1
->status
== PHASE1ST_ESTABLISHED
) &&
905 !iph1
->rmconf
->mode_cfg
)
906 script_hook(iph1
, SCRIPT_PHASE1_UP
);
913 * main function of quick mode.
916 quick_main(iph2
, msg
)
917 struct ph2handle
*iph2
;
920 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
923 struct timeval start
, end
;
926 /* ignore a packet */
927 if (iph2
->status
== PHASE2ST_ESTABLISHED
928 || iph2
->status
== PHASE2ST_GETSPISENT
)
932 gettimeofday(&start
, NULL
);
936 if (ph2exchange
[etypesw2(isakmp
->etype
)]
938 [iph2
->status
] == NULL
) {
939 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
940 "why isn't the function defined.\n");
943 error
= (ph2exchange
[etypesw2(isakmp
->etype
)]
945 [iph2
->status
])(iph2
, msg
);
947 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
948 "failed to pre-process packet.\n");
949 if (error
== ISAKMP_INTERNAL_ERROR
)
951 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
955 /* when using commit bit, status will be reached here. */
956 //if (iph2->status == PHASE2ST_ADDSA) //%%% BUG FIX - wrong place
959 /* free resend buffer */
960 if (iph2
->sendbuf
== NULL
) {
961 plog(LLV_ERROR
, LOCATION
, NULL
,
962 "no buffer found as sendbuf\n");
965 VPTRINIT(iph2
->sendbuf
);
967 /* turn off schedule */
969 SCHED_KILL(iph2
->scr
);
971 /* when using commit bit, status will be reached here. */
972 if (iph2
->status
== PHASE2ST_ADDSA
) //%%% BUG FIX - moved to here
976 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
977 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
979 [iph2
->status
])(iph2
, msg
) != 0) {
980 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
981 "failed to process packet.\n");
986 gettimeofday(&end
, NULL
);
987 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
989 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
990 timedelta(&start
, &end
));
996 /* new negotiation of phase 1 for initiator */
998 isakmp_ph1begin_i(rmconf
, remote
, local
)
999 struct remoteconf
*rmconf
;
1000 struct sockaddr
*remote
, *local
;
1002 struct ph1handle
*iph1
;
1004 struct timeval start
, end
;
1007 /* get new entry to isakmp status table. */
1012 iph1
->status
= PHASE1ST_START
;
1013 iph1
->rmconf
= rmconf
;
1014 iph1
->side
= INITIATOR
;
1015 iph1
->version
= ISAKMP_VERSION_NUMBER
;
1020 iph1
->gssapi_state
= NULL
;
1022 #ifdef ENABLE_HYBRID
1023 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
)
1028 iph1
->frag_chain
= NULL
;
1030 iph1
->approval
= NULL
;
1032 /* XXX copy remote address */
1033 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0)
1038 /* start phase 1 exchange */
1039 iph1
->etype
= rmconf
->etypes
->type
;
1041 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1045 a
= strdup(saddr2str(iph1
->local
));
1046 plog(LLV_INFO
, LOCATION
, NULL
,
1047 "initiate new phase 1 negotiation: %s<=>%s\n",
1048 a
, saddr2str(iph1
->remote
));
1051 plog(LLV_INFO
, LOCATION
, NULL
,
1053 s_isakmp_etype(iph1
->etype
));
1056 gettimeofday(&iph1
->start
, NULL
);
1057 gettimeofday(&start
, NULL
);
1059 /* start exchange */
1060 if ((ph1exchange
[etypesw1(iph1
->etype
)]
1062 [iph1
->status
])(iph1
, NULL
) != 0) {
1063 /* failed to start phase 1 negotiation */
1071 gettimeofday(&end
, NULL
);
1072 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1074 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
1075 timedelta(&start
, &end
));
1078 #ifdef ENABLE_VPNCONTROL_PORT
1079 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
1086 /* new negotiation of phase 1 for responder */
1088 isakmp_ph1begin_r(msg
, remote
, local
, etype
)
1090 struct sockaddr
*remote
, *local
;
1093 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1094 struct remoteconf
*rmconf
;
1095 struct ph1handle
*iph1
;
1096 struct etypes
*etypeok
;
1098 struct timeval start
, end
;
1101 /* look for my configuration */
1102 rmconf
= getrmconf(remote
);
1103 if (rmconf
== NULL
) {
1104 plog(LLV_ERROR
, LOCATION
, remote
,
1106 "configuration.\n");
1110 /* check to be acceptable exchange type */
1111 etypeok
= check_etypeok(rmconf
, etype
);
1112 if (etypeok
== NULL
) {
1113 plog(LLV_ERROR
, LOCATION
, remote
,
1114 "not acceptable %s mode\n", s_isakmp_etype(etype
));
1118 /* get new entry to isakmp status table. */
1123 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
1124 iph1
->status
= PHASE1ST_START
;
1125 iph1
->rmconf
= rmconf
;
1127 iph1
->side
= RESPONDER
;
1128 iph1
->etype
= etypeok
->type
;
1129 iph1
->version
= isakmp
->v
;
1132 iph1
->gssapi_state
= NULL
;
1134 #ifdef ENABLE_HYBRID
1135 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
)
1140 iph1
->frag_chain
= NULL
;
1142 iph1
->approval
= NULL
;
1145 /* RFC3947 says that we MUST accept new phases1 on NAT-T floated port.
1146 * We have to setup this flag now to correctly generate the first reply.
1147 * Don't know if a better check could be done for that ?
1149 if(extract_port(local
) == lcconf
->port_isakmp_natt
)
1150 iph1
->natt_flags
|= (NAT_PORTS_CHANGED
);
1153 /* copy remote address */
1154 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0)
1159 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1163 a
= strdup(saddr2str(iph1
->local
));
1164 plog(LLV_INFO
, LOCATION
, NULL
,
1165 "respond new phase 1 negotiation: %s<=>%s\n",
1166 a
, saddr2str(iph1
->remote
));
1169 plog(LLV_INFO
, LOCATION
, NULL
,
1170 "begin %s mode.\n", s_isakmp_etype(etype
));
1173 gettimeofday(&iph1
->start
, NULL
);
1174 gettimeofday(&start
, NULL
);
1176 /* start exchange */
1177 if ((ph1exchange
[etypesw1(iph1
->etype
)]
1179 [iph1
->status
])(iph1
, msg
) < 0
1180 || (ph1exchange
[etypesw1(iph1
->etype
)]
1182 [iph1
->status
])(iph1
, msg
) < 0) {
1183 plog(LLV_ERROR
, LOCATION
, remote
,
1184 "failed to process packet.\n");
1190 gettimeofday(&end
, NULL
);
1191 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1193 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
1194 timedelta(&start
, &end
));
1196 #ifdef ENABLE_VPNCONTROL_PORT
1197 vpncontrol_notify_phase_change(1, FROM_REMOTE
, iph1
, NULL
);
1203 /* new negotiation of phase 2 for initiator */
1205 isakmp_ph2begin_i(iph1
, iph2
)
1206 struct ph1handle
*iph1
;
1207 struct ph2handle
*iph2
;
1209 #ifdef ENABLE_HYBRID
1210 if (xauth_check(iph1
) != 0) {
1211 plog(LLV_ERROR
, LOCATION
, NULL
,
1212 "Attempt to start phase 2 whereas Xauth failed\n");
1217 /* found ISAKMP-SA. */
1218 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1219 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
1222 a
= strdup(saddr2str(iph2
->src
));
1223 plog(LLV_INFO
, LOCATION
, NULL
,
1224 "initiate new phase 2 negotiation: %s<=>%s\n",
1225 a
, saddr2str(iph2
->dst
));
1230 gettimeofday(&iph2
->start
, NULL
);
1232 /* found isakmp-sa */
1233 bindph12(iph1
, iph2
);
1234 iph2
->status
= PHASE2ST_STATUS2
;
1236 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1238 [iph2
->status
])(iph2
, NULL
) < 0) {
1240 /* release ipsecsa handler due to internal error. */
1245 #ifdef ENABLE_VPNCONTROL_PORT
1246 vpncontrol_notify_phase_change(1, FROM_LOCAL
, NULL
, iph2
);
1252 /* new negotiation of phase 2 for responder */
1254 isakmp_ph2begin_r(iph1
, msg
)
1255 struct ph1handle
*iph1
;
1258 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1259 struct ph2handle
*iph2
= 0;
1262 struct timeval start
, end
;
1264 #ifdef ENABLE_HYBRID
1265 if (xauth_check(iph1
) != 0) {
1266 plog(LLV_ERROR
, LOCATION
, NULL
,
1267 "Attempt to start phase 2 whereas Xauth failed\n");
1274 plog(LLV_ERROR
, LOCATION
, NULL
,
1275 "failed to allocate phase2 entry.\n");
1280 iph2
->side
= RESPONDER
;
1281 iph2
->status
= PHASE2ST_START
;
1282 iph2
->flags
= isakmp
->flags
;
1283 iph2
->msgid
= isakmp
->msgid
;
1284 iph2
->seq
= pk_getseq();
1285 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1286 if (iph2
->ivm
== NULL
) {
1290 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
1291 if (iph2
->dst
== NULL
) {
1295 switch (iph2
->dst
->sa_family
) {
1298 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1304 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1309 plog(LLV_ERROR
, LOCATION
, NULL
,
1310 "invalid family: %d\n", iph2
->dst
->sa_family
);
1315 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1316 if (iph2
->src
== NULL
) {
1320 switch (iph2
->src
->sa_family
) {
1323 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1329 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1334 plog(LLV_ERROR
, LOCATION
, NULL
,
1335 "invalid family: %d\n", iph2
->src
->sa_family
);
1340 /* add new entry to isakmp status table */
1342 bindph12(iph1
, iph2
);
1344 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1348 a
= strdup(saddr2str(iph2
->src
));
1349 plog(LLV_INFO
, LOCATION
, NULL
,
1350 "respond new phase 2 negotiation: %s<=>%s\n",
1351 a
, saddr2str(iph2
->dst
));
1356 gettimeofday(&start
, NULL
);
1359 error
= (ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1361 [iph2
->status
])(iph2
, msg
);
1363 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1364 "failed to pre-process packet.\n");
1365 if (error
!= ISAKMP_INTERNAL_ERROR
)
1366 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
1368 * release handler because it's wrong that ph2handle is kept
1369 * after failed to check message for responder's.
1378 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1379 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
1381 [iph2
->status
])(iph2
, msg
) < 0) {
1382 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1383 "failed to process packet.\n");
1384 /* don't release handler */
1388 gettimeofday(&end
, NULL
);
1389 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1391 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
1392 timedelta(&start
, &end
));
1395 #ifdef ENABLE_VPNCONTROL_PORT
1396 vpncontrol_notify_phase_change(1, FROM_REMOTE
, NULL
, iph2
);
1404 * parse ISAKMP payloads, without ISAKMP base header.
1407 isakmp_parsewoh(np0
, gen
, len
)
1409 struct isakmp_gen
*gen
;
1412 u_char np
= np0
& 0xff;
1415 struct isakmp_parse_t
*p
, *ep
;
1417 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
1420 * 5 is a magic number, but any value larger than 2 should be fine
1421 * as we do vrealloc() in the following loop.
1423 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1424 if (result
== NULL
) {
1425 plog(LLV_ERROR
, LOCATION
, NULL
,
1426 "failed to get buffer.\n");
1429 p
= (struct isakmp_parse_t
*)result
->v
;
1430 ep
= (struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1434 /* parse through general headers */
1435 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1436 if (tlen
<= sizeof(struct isakmp_gen
)) {
1437 /* don't send information, see isakmp_ident_r1() */
1438 plog(LLV_ERROR
, LOCATION
, NULL
,
1439 "invalid length of payload\n");
1444 plog(LLV_DEBUG
, LOCATION
, NULL
,
1445 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1448 p
->len
= ntohs(gen
->len
);
1449 if (p
->len
< sizeof(struct isakmp_gen
) || p
->len
> tlen
) {
1450 plog(LLV_DEBUG
, LOCATION
, NULL
,
1451 "invalid length of payload\n");
1460 off
= p
- (struct isakmp_parse_t
*)result
->v
;
1461 result
= vrealloc(result
, result
->l
* 2);
1462 if (result
== NULL
) {
1463 plog(LLV_DEBUG
, LOCATION
, NULL
,
1464 "failed to realloc buffer.\n");
1468 ep
= (struct isakmp_parse_t
*)
1469 (result
->v
+ result
->l
- sizeof(*ep
));
1470 p
= (struct isakmp_parse_t
*)result
->v
;
1475 plen
= ntohs(gen
->len
);
1476 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1479 p
->type
= ISAKMP_NPTYPE_NONE
;
1483 plog(LLV_DEBUG
, LOCATION
, NULL
, "succeed.\n");
1489 * parse ISAKMP payloads, including ISAKMP base header.
1495 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1496 struct isakmp_gen
*gen
;
1502 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1503 tlen
= buf
->l
- sizeof(struct isakmp
);
1504 result
= isakmp_parsewoh(np
, gen
, tlen
);
1513 /* initialize a isakmp status table */
1519 if (isakmp_open() < 0)
1537 * make strings containing i_cookie + r_cookie + msgid
1540 isakmp_pindex(index
, msgid
)
1541 const isakmp_index
*index
;
1542 const u_int32_t msgid
;
1544 static char buf
[64];
1548 memset(buf
, 0, sizeof(buf
));
1551 p
= (const u_char
*)index
;
1552 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1553 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1565 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1570 /* open ISAKMP sockets. */
1575 int ifnum
= 0, encap_ifnum
= 0;
1581 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1586 if (p
->sock
!= -1) {
1590 continue; // socket already open
1594 /* warn if wildcard address - should we forbid this? */
1595 switch (p
->addr
->sa_family
) {
1597 if (((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
== 0)
1598 plog(LLV_WARNING
, LOCATION
, NULL
,
1599 "listening to wildcard address,"
1600 "broadcast IKE packet may kill you\n");
1604 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
))
1605 plog(LLV_WARNING
, LOCATION
, NULL
,
1606 "listening to wildcard address, "
1607 "broadcast IKE packet may kill you\n");
1611 plog(LLV_ERROR
, LOCATION
, NULL
,
1612 "unsupported address family %d\n",
1613 lcconf
->default_af
);
1618 if (p
->addr
->sa_family
== AF_INET6
&&
1619 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)
1620 p
->addr
)->sin6_addr
))
1622 plog(LLV_DEBUG
, LOCATION
, NULL
,
1623 "Ignoring multicast address %s\n",
1624 saddr2str(p
->addr
));
1625 racoon_free(p
->addr
);
1631 if ((p
->sock
= socket(p
->addr
->sa_family
, SOCK_DGRAM
, 0)) < 0) {
1632 plog(LLV_ERROR
, LOCATION
, NULL
,
1633 "socket (%s)\n", strerror(errno
));
1637 /* receive my interface address on inbound packets. */
1638 switch (p
->addr
->sa_family
) {
1640 if (setsockopt(p
->sock
, IPPROTO_IP
,
1646 (const void *)&yes
, sizeof(yes
)) < 0) {
1647 plog(LLV_ERROR
, LOCATION
, NULL
,
1648 "setsockopt (%s)\n", strerror(errno
));
1655 #ifdef IPV6_RECVPKTINFO
1656 pktinfo
= IPV6_RECVPKTINFO
;
1657 #else /* old adv. API */
1658 pktinfo
= IPV6_PKTINFO
;
1659 #endif /* IPV6_RECVPKTINFO */
1661 pktinfo
= IPV6_RECVDSTADDR
;
1663 if (setsockopt(p
->sock
, IPPROTO_IPV6
, pktinfo
,
1664 (const void *)&yes
, sizeof(yes
)) < 0)
1666 plog(LLV_ERROR
, LOCATION
, NULL
,
1667 "setsockopt(%d): %s\n",
1668 pktinfo
, strerror(errno
));
1669 if (fcntl(p
->sock
, F_SETFL
, O_NONBLOCK
) == -1)
1670 plog(LLV_WARNING
, LOCATION
, NULL
,
1671 "failed to put socket in non-blocking mode\n");
1679 #ifdef IPV6_USE_MIN_MTU
1680 if (p
->addr
->sa_family
== AF_INET6
&&
1681 setsockopt(p
->sock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1682 (void *)&yes
, sizeof(yes
)) < 0) {
1683 plog(LLV_ERROR
, LOCATION
, NULL
,
1684 "setsockopt (%s)\n", strerror(errno
));
1689 if (setsockopt_bypass(p
->sock
, p
->addr
->sa_family
) < 0)
1693 if (extract_port(p
->addr
) == PORT_ISAKMP
) {
1694 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_NOTIFYCONFLICT
,
1695 (void *)&yes
, sizeof(yes
)) < 0) {
1696 plog(LLV_ERROR
, LOCATION
, p
->addr
,
1697 "setsockopt (%s)\n", strerror(errno
));
1703 if (bind(p
->sock
, p
->addr
, sysdep_sa_len(p
->addr
)) < 0) {
1704 plog(LLV_ERROR
, LOCATION
, p
->addr
,
1705 "failed to bind to address %s (%s).\n",
1706 saddr2str(p
->addr
), strerror(errno
));
1718 plog(LLV_INFO
, LOCATION
, NULL
,
1719 "%s used as isakmp port (fd=%d)\n",
1720 saddr2str(p
->addr
), p
->sock
);
1724 if (p
->addr
->sa_family
== AF_INET
) {
1728 option
= UDP_ENCAP_ESPINUDP
;
1729 #if defined(ENABLE_NATT_00) || defined(ENABLE_NATT_01)
1731 option
= UDP_ENCAP_ESPINUDP_NON_IKE
;
1734 if (setsockopt (p
->sock
, SOL_UDP
, UDP_ENCAP
,
1735 &option
, sizeof (option
)) < 0) {
1736 plog(LLV_WARNING
, LOCATION
, NULL
,
1737 "setsockopt(%s): %s\n",
1738 option
== UDP_ENCAP_ESPINUDP
? "UDP_ENCAP_ESPINUDP" : "UDP_ENCAP_ESPINUDP_NON_IKE",
1743 plog(LLV_INFO
, LOCATION
, NULL
,
1744 "%s used for NAT-T\n",
1745 saddr2str(p
->addr
));
1752 #endif /* __APPLE__ */
1757 racoon_free(p
->addr
);
1759 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
)
1765 plog(LLV_ERROR
, LOCATION
, NULL
,
1766 "no address could be bound.\n");
1771 if (natt_enabled_in_rmconf() && !encap_ifnum
) {
1772 plog(LLV_WARNING
, LOCATION
, NULL
,
1773 "NAT-T is enabled in at least one remote{} section,\n");
1774 plog(LLV_WARNING
, LOCATION
, NULL
,
1775 "but no 'isakmp_natt' address was specified!\n");
1785 isakmp_close_sockets();
1790 isakmp_close_sockets()
1794 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1808 // close sockets for addresses that have gone away
1810 isakmp_close_unused()
1812 struct myaddrs
*p
, *next
, **prev
;
1814 prev
= &(lcconf
->myaddrs
);
1815 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
1817 if (p
->in_use
== 0) { // not in use ?
1822 racoon_free(p
->addr
);
1831 isakmp_send(iph1
, sbuf
)
1832 struct ph1handle
*iph1
;
1837 vchar_t
*vbuf
= NULL
;
1840 size_t extralen
= NON_ESP_MARKER_USE(iph1
) ? NON_ESP_MARKER_LEN
: 0;
1844 * Do not add the non ESP marker for a packet that will
1845 * be fragmented. The non ESP marker should appear in
1846 * all fragment's packets, but not in the fragmented packet
1848 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
)
1852 plog (LLV_DEBUG
, LOCATION
, NULL
, "Adding NON-ESP marker\n");
1854 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1855 must added just before the packet itself. For this we must
1856 allocate a new buffer and release it at the end. */
1858 vbuf
= vmalloc (sbuf
->l
+ extralen
);
1859 *(u_int32_t
*)vbuf
->v
= 0;
1860 memcpy (vbuf
->v
+ extralen
, sbuf
->v
, sbuf
->l
);
1865 /* select the socket to be sent */
1866 s
= getsockmyaddr(iph1
->local
);
1873 plog (LLV_DEBUG
, LOCATION
, NULL
, "%zu bytes %s\n", sbuf
->l
,
1874 saddr2str_fromto("from %s to %s", iph1
->local
, iph1
->remote
));
1877 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1878 if (isakmp_sendfrags(iph1
, sbuf
) == -1) {
1879 plog(LLV_ERROR
, LOCATION
, NULL
,
1880 "isakmp_sendfrags failed\n");
1888 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1889 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1891 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto failed\n");
1904 /* called from scheduler */
1906 isakmp_ph1resend_stub(p
)
1909 (void)isakmp_ph1resend((struct ph1handle
*)p
);
1913 isakmp_ph1resend(iph1
)
1914 struct ph1handle
*iph1
;
1916 if (iph1
->retry_counter
< 0) {
1917 plog(LLV_ERROR
, LOCATION
, NULL
,
1918 "phase1 negotiation failed due to time up. %s\n",
1919 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1920 EVT_PUSH(iph1
->local
, iph1
->remote
,
1921 EVTT_PEER_NO_RESPONSE
, NULL
);
1928 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0){
1929 iph1
->retry_counter
--;
1931 iph1
->scr
= sched_new(iph1
->rmconf
->retry_interval
,
1932 isakmp_ph1resend_stub
, iph1
);
1936 plog(LLV_DEBUG
, LOCATION
, NULL
,
1937 "resend phase1 packet %s\n",
1938 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1940 iph1
->retry_counter
--;
1942 iph1
->scr
= sched_new(iph1
->rmconf
->retry_interval
,
1943 isakmp_ph1resend_stub
, iph1
);
1948 /* called from scheduler */
1950 isakmp_ph2resend_stub(p
)
1954 (void)isakmp_ph2resend((struct ph2handle
*)p
);
1958 isakmp_ph2resend(iph2
)
1959 struct ph2handle
*iph2
;
1961 if (iph2
->retry_counter
< 0) {
1962 plog(LLV_ERROR
, LOCATION
, NULL
,
1963 "phase2 negotiation failed due to time up. %s\n",
1964 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1965 EVT_PUSH(iph2
->src
, iph2
->dst
, EVTT_PEER_NO_RESPONSE
, NULL
);
1972 //%%% BUG FIX - related to commit bit usage - crash happened here
1973 if (iph2
->ph1
== 0) {
1974 plog(LLV_ERROR
, LOCATION
, NULL
,
1975 "internal error - attempt to re-send phase2 with no phase1 bound.\n");
1976 iph2
->retry_counter
= -1;
1982 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0)
1985 plog(LLV_DEBUG
, LOCATION
, NULL
,
1986 "resend phase2 packet %s\n",
1987 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1989 iph2
->retry_counter
--;
1991 iph2
->scr
= sched_new(iph2
->ph1
->rmconf
->retry_interval
,
1992 isakmp_ph2resend_stub
, iph2
);
1997 /* called from scheduler */
1999 isakmp_ph1expire_stub(p
)
2003 isakmp_ph1expire((struct ph1handle
*)p
);
2007 isakmp_ph1expire(iph1
)
2008 struct ph1handle
*iph1
;
2012 SCHED_KILL(iph1
->sce
);
2014 if(iph1
->status
!= PHASE1ST_EXPIRED
){
2015 src
= strdup(saddr2str(iph1
->local
));
2016 dst
= strdup(saddr2str(iph1
->remote
));
2017 plog(LLV_INFO
, LOCATION
, NULL
,
2018 "ISAKMP-SA expired %s-%s spi:%s\n",
2020 isakmp_pindex(&iph1
->index
, 0));
2023 iph1
->status
= PHASE1ST_EXPIRED
;
2027 * the phase1 deletion is postponed until there is no phase2.
2029 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
2030 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
2034 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2037 /* called from scheduler */
2039 isakmp_ph1delete_stub(p
)
2043 isakmp_ph1delete((struct ph1handle
*)p
);
2047 isakmp_ph1delete(iph1
)
2048 struct ph1handle
*iph1
;
2053 SCHED_KILL(iph1
->sce
);
2055 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
2056 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2060 /* don't re-negosiation when the phase 1 SA expires. */
2062 src
= strdup(saddr2str(iph1
->local
));
2063 dst
= strdup(saddr2str(iph1
->remote
));
2064 plog(LLV_INFO
, LOCATION
, NULL
,
2065 "ISAKMP-SA deleted %s-%s spi:%s\n",
2066 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
2067 EVT_PUSH(iph1
->local
, iph1
->remote
, EVTT_PHASE1_DOWN
, NULL
);
2077 /* called from scheduler.
2078 * this function will call only isakmp_ph2delete().
2079 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
2080 * by something cause. That's why this function is called after phase 2 SA
2081 * expires in the userland.
2084 isakmp_ph2expire_stub(p
)
2088 isakmp_ph2expire((struct ph2handle
*)p
);
2092 isakmp_ph2expire(iph2
)
2093 struct ph2handle
*iph2
;
2097 SCHED_KILL(iph2
->sce
);
2099 src
= strdup(saddrwop2str(iph2
->src
));
2100 dst
= strdup(saddrwop2str(iph2
->dst
));
2101 plog(LLV_INFO
, LOCATION
, NULL
,
2102 "phase2 sa expired %s-%s\n", src
, dst
);
2106 iph2
->status
= PHASE2ST_EXPIRED
;
2108 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
2113 /* called from scheduler */
2115 isakmp_ph2delete_stub(p
)
2119 isakmp_ph2delete((struct ph2handle
*)p
);
2123 isakmp_ph2delete(iph2
)
2124 struct ph2handle
*iph2
;
2128 SCHED_KILL(iph2
->sce
);
2130 src
= strdup(saddrwop2str(iph2
->src
));
2131 dst
= strdup(saddrwop2str(iph2
->dst
));
2132 plog(LLV_INFO
, LOCATION
, NULL
,
2133 "phase2 sa deleted %s-%s\n", src
, dst
);
2145 * Interface between PF_KEYv2 and ISAKMP
2148 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
2149 * if phase1 has been finished, begin phase2.
2152 isakmp_post_acquire(iph2
)
2153 struct ph2handle
*iph2
;
2155 struct remoteconf
*rmconf
;
2156 struct ph1handle
*iph1
= NULL
;
2158 /* search appropreate configuration with masking port. */
2159 rmconf
= getrmconf(iph2
->dst
);
2160 if (rmconf
== NULL
) {
2161 plog(LLV_ERROR
, LOCATION
, NULL
,
2162 "no configuration found for %s.\n",
2163 saddrwop2str(iph2
->dst
));
2167 /* if passive mode, ignore the acquire message */
2168 if (rmconf
->passive
) {
2169 plog(LLV_DEBUG
, LOCATION
, NULL
,
2170 "because of passive mode, "
2171 "ignore the acquire message for %s.\n",
2172 saddrwop2str(iph2
->dst
));
2177 * Search isakmp status table by address and port
2178 * If NAT-T is in use, consider null ports as a
2179 * wildcard and use IKE ports instead.
2182 if (!extract_port(iph2
->src
) && !extract_port(iph2
->dst
)) {
2183 if ((iph1
= getph1byaddrwop(iph2
->src
, iph2
->dst
)) != NULL
) {
2184 set_port(iph2
->src
, extract_port(iph1
->local
));
2185 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2188 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
2191 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
2194 /* no ISAKMP-SA found. */
2198 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2199 sc
= sched_new(1, isakmp_chkph1there_stub
, iph2
);
2200 plog(LLV_INFO
, LOCATION
, NULL
,
2201 "IPsec-SA request for %s queued "
2202 "due to no phase1 found.\n",
2203 saddrwop2str(iph2
->dst
));
2205 /* start phase 1 negotiation as a initiator. */
2206 if (isakmp_ph1begin_i(rmconf
, iph2
->dst
, iph2
->src
) < 0) {
2215 /* found ISAKMP-SA, but on negotiation. */
2216 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
2217 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2218 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2219 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
2220 "request for establishing IPsec-SA was queued "
2221 "due to no phase1 found.\n");
2226 /* found established ISAKMP-SA */
2227 /* i.e. iph1->status == PHASE1ST_ESTABLISHED */
2229 /* found ISAKMP-SA. */
2230 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
2232 /* begin quick mode */
2233 if (isakmp_ph2begin_i(iph1
, iph2
))
2240 * receive GETSPI from kernel.
2243 isakmp_post_getspi(iph2
)
2244 struct ph2handle
*iph2
;
2247 struct timeval start
, end
;
2250 /* don't process it because there is no suitable phase1-sa. */
2251 if (iph2
->ph1
->status
== PHASE1ST_EXPIRED
) {
2252 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
2253 "the negotiation is stopped, "
2254 "because there is no suitable ISAKMP-SA.\n");
2259 gettimeofday(&start
, NULL
);
2261 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
2263 [iph2
->status
])(iph2
, NULL
) != 0)
2266 gettimeofday(&end
, NULL
);
2267 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
2269 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2270 timedelta(&start
, &end
));
2276 /* called by scheduler */
2278 isakmp_chkph1there_stub(p
)
2281 isakmp_chkph1there((struct ph2handle
*)p
);
2285 isakmp_chkph1there(iph2
)
2286 struct ph2handle
*iph2
;
2288 struct ph1handle
*iph1
;
2290 iph2
->retry_checkph1
--;
2291 if (iph2
->retry_checkph1
< 0) {
2292 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
2293 "phase2 negotiation failed "
2294 "due to time up waiting for phase1. %s\n",
2295 sadbsecas2str(iph2
->dst
, iph2
->src
,
2296 iph2
->satype
, 0, 0));
2297 plog(LLV_INFO
, LOCATION
, NULL
,
2298 "delete phase 2 handler.\n");
2300 /* send acquire to kernel as error */
2301 pk_sendeacquire(iph2
);
2311 * Search isakmp status table by address and port
2312 * If NAT-T is in use, consider null ports as a
2313 * wildcard and use IKE ports instead.
2316 if (!extract_port(iph2
->src
) && !extract_port(iph2
->dst
)) {
2317 if ((iph1
= getph1byaddrwop(iph2
->src
, iph2
->dst
)) != NULL
) {
2319 * cannot set ph2 ports until after switch to natt port
2320 * otherwise this function will never again find phase 1
2322 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
2323 set_port(iph2
->src
, extract_port(iph1
->local
));
2324 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2328 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
2331 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
2334 /* XXX Even if ph1 as responder is there, should we not start
2335 * phase 2 negotiation ? */
2337 && iph1
->status
== PHASE1ST_ESTABLISHED
) {
2338 /* found isakmp-sa */
2339 /* begin quick mode */
2340 (void)isakmp_ph2begin_i(iph1
, iph2
);
2344 /* no isakmp-sa found */
2345 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2350 /* copy variable data into ALLOCATED buffer. */
2352 isakmp_set_attr_v(buf
, type
, val
, len
)
2358 struct isakmp_data
*data
;
2360 data
= (struct isakmp_data
*)buf
;
2361 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2362 data
->lorv
= htons((u_int16_t
)len
);
2363 memcpy(data
+ 1, val
, len
);
2365 return buf
+ sizeof(*data
) + len
;
2368 /* copy fixed length data into ALLOCATED buffer. */
2370 isakmp_set_attr_l(buf
, type
, val
)
2375 struct isakmp_data
*data
;
2377 data
= (struct isakmp_data
*)buf
;
2378 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2379 data
->lorv
= htons((u_int16_t
)val
);
2381 return buf
+ sizeof(*data
);
2384 /* add a variable data attribute to the buffer by reallocating it. */
2386 isakmp_add_attr_v(buf0
, type
, val
, len
)
2392 vchar_t
*buf
= NULL
;
2393 struct isakmp_data
*data
;
2397 tlen
= sizeof(*data
) + len
;
2401 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2403 buf
= vmalloc(tlen
);
2405 plog(LLV_ERROR
, LOCATION
, NULL
,
2406 "failed to get a attribute buffer.\n");
2410 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2411 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2412 data
->lorv
= htons((u_int16_t
)len
);
2413 memcpy(data
+ 1, val
, len
);
2418 /* add a fixed data attribute to the buffer by reallocating it. */
2420 isakmp_add_attr_l(buf0
, type
, val
)
2425 vchar_t
*buf
= NULL
;
2426 struct isakmp_data
*data
;
2430 tlen
= sizeof(*data
);
2434 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2436 buf
= vmalloc(tlen
);
2438 plog(LLV_ERROR
, LOCATION
, NULL
,
2439 "failed to get a attribute buffer.\n");
2443 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2444 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2445 data
->lorv
= htons((u_int16_t
)val
);
2451 * calculate cookie and set.
2454 isakmp_newcookie(place
, remote
, local
)
2456 struct sockaddr
*remote
;
2457 struct sockaddr
*local
;
2459 vchar_t
*buf
= NULL
, *buf2
= NULL
;
2469 if (remote
->sa_family
!= local
->sa_family
) {
2470 plog(LLV_ERROR
, LOCATION
, NULL
,
2471 "address family mismatch, remote:%d local:%d\n",
2472 remote
->sa_family
, local
->sa_family
);
2475 switch (remote
->sa_family
) {
2477 alen
= sizeof(struct in_addr
);
2478 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
2479 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
2483 alen
= sizeof(struct in_addr
);
2484 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
2485 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
2489 plog(LLV_ERROR
, LOCATION
, NULL
,
2490 "invalid family: %d\n", remote
->sa_family
);
2493 blen
= (alen
+ sizeof(u_short
)) * 2
2494 + sizeof(time_t) + lcconf
->secret_size
;
2495 buf
= vmalloc(blen
);
2497 plog(LLV_ERROR
, LOCATION
, NULL
,
2498 "failed to get a cookie.\n");
2503 /* copy my address */
2504 memcpy(p
, sa1
, alen
);
2506 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
2507 memcpy(p
, &port
, sizeof(u_short
));
2508 p
+= sizeof(u_short
);
2510 /* copy target address */
2511 memcpy(p
, sa2
, alen
);
2513 port
= ((struct sockaddr_in
*)local
)->sin_port
;
2514 memcpy(p
, &port
, sizeof(u_short
));
2515 p
+= sizeof(u_short
);
2519 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
2522 /* copy random value */
2523 buf2
= eay_set_random(lcconf
->secret_size
);
2526 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
2527 p
+= lcconf
->secret_size
;
2530 buf2
= eay_sha1_one(buf
);
2531 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
2533 sa1
= val2str(place
, sizeof (cookie_t
));
2534 plog(LLV_DEBUG
, LOCATION
, NULL
, "new cookie:\n%s\n", sa1
);
2547 * save partner's(payload) data into phhandle.
2550 isakmp_p2ph(buf
, gen
)
2552 struct isakmp_gen
*gen
;
2554 /* XXX to be checked in each functions for logging. */
2556 plog(LLV_WARNING
, LOCATION
, NULL
,
2557 "ignore this payload, same payload type exist.\n");
2561 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2563 plog(LLV_ERROR
, LOCATION
, NULL
,
2564 "failed to get buffer.\n");
2567 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2573 isakmp_newmsgid2(iph1
)
2574 struct ph1handle
*iph1
;
2579 msgid2
= eay_random();
2580 } while (getph2bymsgid(iph1
, msgid2
));
2586 * set values into allocated buffer of isakmp header for phase 1
2589 set_isakmp_header(vbuf
, iph1
, nptype
, etype
, flags
, msgid
)
2591 struct ph1handle
*iph1
;
2597 struct isakmp
*isakmp
;
2599 if (vbuf
->l
< sizeof(*isakmp
))
2602 isakmp
= (struct isakmp
*)vbuf
->v
;
2604 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2605 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2606 isakmp
->np
= nptype
;
2607 isakmp
->v
= iph1
->version
;
2608 isakmp
->etype
= etype
;
2609 isakmp
->flags
= flags
;
2610 isakmp
->msgid
= msgid
;
2611 isakmp
->len
= htonl(vbuf
->l
);
2613 return vbuf
->v
+ sizeof(*isakmp
);
2617 * set values into allocated buffer of isakmp header for phase 1
2620 set_isakmp_header1(vbuf
, iph1
, nptype
)
2622 struct ph1handle
*iph1
;
2625 return set_isakmp_header (vbuf
, iph1
, nptype
, iph1
->etype
, iph1
->flags
, iph1
->msgid
);
2629 * set values into allocated buffer of isakmp header for phase 2
2632 set_isakmp_header2(vbuf
, iph2
, nptype
)
2634 struct ph2handle
*iph2
;
2637 return set_isakmp_header (vbuf
, iph2
->ph1
, nptype
, ISAKMP_ETYPE_QUICK
, iph2
->flags
, iph2
->msgid
);
2641 * set values into allocated buffer of isakmp payload.
2644 set_isakmp_payload(buf
, src
, nptype
)
2649 struct isakmp_gen
*gen
;
2652 plog(LLV_DEBUG
, LOCATION
, NULL
, "add payload of len %zu, next type %d\n",
2655 gen
= (struct isakmp_gen
*)p
;
2657 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2659 memcpy(p
, src
->v
, src
->l
);
2670 case ISAKMP_ETYPE_IDENT
:
2672 case ISAKMP_ETYPE_AGG
:
2674 case ISAKMP_ETYPE_BASE
:
2687 case ISAKMP_ETYPE_QUICK
:
2695 #ifdef HAVE_PRINT_ISAKMP_C
2696 /* for print-isakmp.c */
2698 extern void isakmp_print
__P((const u_char
*, u_int
, const u_char
*));
2700 char *getname
__P((const u_char
*));
2702 char *getname6
__P((const u_char
*));
2704 int safeputchar
__P((int));
2707 * Return a name for the IP address pointed to by ap. This address
2708 * is assumed to be in network byte order.
2714 struct sockaddr_in addr
;
2715 static char ntop_buf
[NI_MAXHOST
];
2717 memset(&addr
, 0, sizeof(addr
));
2719 addr
.sin_len
= sizeof(struct sockaddr_in
);
2721 addr
.sin_family
= AF_INET
;
2722 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2723 if (getnameinfo((struct sockaddr
*)&addr
, sizeof(addr
),
2724 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2725 NI_NUMERICHOST
| niflags
))
2726 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2733 * Return a name for the IP6 address pointed to by ap. This address
2734 * is assumed to be in network byte order.
2740 struct sockaddr_in6 addr
;
2741 static char ntop_buf
[NI_MAXHOST
];
2743 memset(&addr
, 0, sizeof(addr
));
2744 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2745 addr
.sin6_family
= AF_INET6
;
2746 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2747 if (getnameinfo((struct sockaddr
*)&addr
, addr
.sin6_len
,
2748 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2749 NI_NUMERICHOST
| niflags
))
2750 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2762 ch
= (unsigned char)(c
& 0xff);
2763 if (c
< 0x80 && isprint(c
))
2764 return printf("%c", c
& 0xff);
2766 return printf("\\%03o", c
& 0xff);
2770 isakmp_printpacket(msg
, from
, my
, decoded
)
2772 struct sockaddr
*from
;
2773 struct sockaddr
*my
;
2779 char hostbuf
[NI_MAXHOST
];
2780 char portbuf
[NI_MAXSERV
];
2781 struct isakmp
*isakmp
;
2785 if (loglevel
< LLV_DEBUG
)
2789 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
2791 gettimeofday(&tv
, NULL
);
2792 s
= tv
.tv_sec
% 3600;
2793 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2796 if (getnameinfo(from
, sysdep_sa_len(from
), hostbuf
, sizeof(hostbuf
),
2797 portbuf
, sizeof(portbuf
),
2798 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2799 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2800 strlcpy(portbuf
, "?", sizeof(portbuf
));
2802 printf("%s:%s", hostbuf
, portbuf
);
2807 if (getnameinfo(my
, sysdep_sa_len(my
), hostbuf
, sizeof(hostbuf
),
2808 portbuf
, sizeof(portbuf
),
2809 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2810 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2811 strlcpy(portbuf
, "?", sizeof(portbuf
));
2813 printf("%s:%s", hostbuf
, portbuf
);
2820 printf("(malloc fail)\n");
2824 isakmp
= (struct isakmp
*)buf
->v
;
2825 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
2828 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
2829 if (buf
->l
< pad
&& 2 < vflag
)
2830 printf("(wrong padding)");
2832 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
2836 snapend
= buf
->v
+ buf
->l
;
2837 isakmp_print(buf
->v
, buf
->l
, NULL
);
2845 #endif /*HAVE_PRINT_ISAKMP_C*/
2848 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
2849 struct ph1handle
*iph1
;
2850 struct remoteconf
*rmconf
;
2851 struct sockaddr
*remote
, *local
;
2853 u_short
*port
= NULL
;
2855 /* address portion must be grabbed from real remote address "remote" */
2856 iph1
->remote
= dupsaddr(remote
);
2857 if (iph1
->remote
== NULL
) {
2863 * if remote has no port # (in case of initiator - from ACQUIRE msg)
2864 * - if remote.conf specifies port #, use that
2865 * - if remote.conf does not, use 500
2866 * if remote has port # (in case of responder - from recvfrom(2))
2867 * respect content of "remote".
2869 switch (iph1
->remote
->sa_family
) {
2871 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
2874 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
2877 *port
= htons(PORT_ISAKMP
);
2881 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
2884 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
2887 *port
= htons(PORT_ISAKMP
);
2891 plog(LLV_ERROR
, LOCATION
, NULL
,
2892 "invalid family: %d\n", iph1
->remote
->sa_family
);
2897 iph1
->local
= getlocaladdr(iph1
->remote
);
2899 iph1
->local
= dupsaddr(local
);
2900 if (iph1
->local
== NULL
) {
2905 switch (iph1
->local
->sa_family
) {
2907 port
= &((struct sockaddr_in
*)iph1
->local
)->sin_port
;
2910 *port
= ((struct sockaddr_in
*)local
)->sin_port
;
2913 *port
= getmyaddrsport(iph1
->local
);
2917 port
= &((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
2920 *port
= ((struct sockaddr_in6
*)local
)->sin6_port
;
2923 *port
= getmyaddrsport(iph1
->local
);
2927 plog(LLV_ERROR
, LOCATION
, NULL
,
2928 "invalid family: %d\n", iph1
->local
->sa_family
);
2933 if ( port
!= NULL
&& *port
== htons(lcconf
->port_isakmp_natt
) ) {
2934 plog (LLV_DEBUG
, LOCATION
, NULL
, "Marking ports as changed\n");
2935 iph1
->natt_flags
|= NAT_ADD_NON_ESP_MARKER
;
2944 struct ph1handle
*iph1
;
2947 plog(LLV_ERROR
, LOCATION
, iph1
->remote
, "wrong state %u.\n",
2954 struct ph2handle
*iph2
;
2957 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
, "wrong state %u.\n",
2963 log_ph1established(iph1
)
2964 const struct ph1handle
*iph1
;
2968 src
= strdup(saddr2str(iph1
->local
));
2969 dst
= strdup(saddr2str(iph1
->remote
));
2970 plog(LLV_INFO
, LOCATION
, NULL
,
2971 "ISAKMP-SA established %s-%s spi:%s\n",
2973 isakmp_pindex(&iph1
->index
, 0));
2974 EVT_PUSH(iph1
->local
, iph1
->remote
, EVTT_PHASE1_UP
, NULL
);
2981 struct payload_list
*
2982 isakmp_plist_append (struct payload_list
*plist
, vchar_t
*payload
, int payload_type
)
2985 plist
= racoon_malloc (sizeof (struct payload_list
));
2989 plist
->next
= racoon_malloc (sizeof (struct payload_list
));
2990 plist
->next
->prev
= plist
;
2991 plist
= plist
->next
;
2995 plist
->payload
= payload
;
2996 plist
->payload_type
= payload_type
;
3002 isakmp_plist_set_all (struct payload_list
**plist
, struct ph1handle
*iph1
)
3004 struct payload_list
*ptr
, *first
;
3005 size_t tlen
= sizeof (struct isakmp
), n
= 0;
3009 if (plist
== NULL
) {
3010 plog(LLV_ERROR
, LOCATION
, NULL
,
3011 "in isakmp_plist_set_all: plist == NULL\n");
3015 /* Seek to the first item. */
3021 /* Compute the whole length. */
3023 tlen
+= ptr
->payload
->l
+ sizeof (struct isakmp_gen
);
3027 buf
= vmalloc(tlen
);
3029 plog(LLV_ERROR
, LOCATION
, NULL
,
3030 "failed to get buffer to send.\n");
3036 p
= set_isakmp_header1(buf
, iph1
, ptr
->payload_type
);
3042 p
= set_isakmp_payload (p
, ptr
->payload
, ptr
->next
? ptr
->next
->payload_type
: ISAKMP_NPTYPE_NONE
);
3045 racoon_free (first
);
3046 /* ptr->prev = NULL; first = NULL; ... omitted. */
3059 frag_handler(iph1
, msg
, remote
, local
)
3060 struct ph1handle
*iph1
;
3062 struct sockaddr
*remote
;
3063 struct sockaddr
*local
;
3067 if (isakmp_frag_extract(iph1
, msg
) == 1) {
3068 if ((newmsg
= isakmp_frag_reassembly(iph1
)) == NULL
) {
3069 plog(LLV_ERROR
, LOCATION
, remote
,
3070 "Packet reassembly failed\n");
3073 return isakmp_main(newmsg
, remote
, local
);
3081 script_hook(iph1
, script
)
3082 struct ph1handle
*iph1
;
3087 char addrstr
[IP_MAX
];
3088 char portstr
[PORT_MAX
];
3091 struct sockaddr_in
*sin
;
3094 if (iph1
->rmconf
->script
[script
] == -1)
3097 #ifdef ENABLE_HYBRID
3098 (void)isakmp_cfg_setenv(iph1
, &envp
, &envc
);
3102 sin
= (struct sockaddr_in
*)iph1
->local
;
3103 inet_ntop(sin
->sin_family
, &sin
->sin_addr
, addrstr
, IP_MAX
);
3104 snprintf(portstr
, PORT_MAX
, "%d", ntohs(sin
->sin_port
));
3106 if (script_env_append(&envp
, &envc
, "LOCAL_ADDR", addrstr
) != 0) {
3107 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot set LOCAL_ADDR\n");
3111 if (script_env_append(&envp
, &envc
, "LOCAL_PORT", portstr
) != 0) {
3112 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot set LOCAL_PORT\n");
3117 sin
= (struct sockaddr_in
*)iph1
->remote
;
3118 inet_ntop(sin
->sin_family
, &sin
->sin_addr
, addrstr
, IP_MAX
);
3119 snprintf(portstr
, PORT_MAX
, "%d", ntohs(sin
->sin_port
));
3121 if (script_env_append(&envp
, &envc
, "REMOTE_ADDR", addrstr
) != 0) {
3122 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot set REMOTE_ADDR\n");
3126 if (script_env_append(&envp
, &envc
, "REMOTE_PORT", portstr
) != 0) {
3127 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot set REMOTEL_PORT\n");
3131 if (privsep_script_exec(iph1
->rmconf
->script
[script
],
3133 plog(LLV_ERROR
, LOCATION
, NULL
,
3134 "Script %s execution failed\n", script_names
[script
]);
3137 for (c
= envp
; *c
; c
++)
3146 script_env_append(envp
, envc
, name
, value
)
3155 int envitemlen
= strlen(name
) + 1 + strlen(value
) + 1;
3157 envitem
= racoon_malloc(envitemlen
);
3158 if (envitem
== NULL
) {
3159 plog(LLV_ERROR
, LOCATION
, NULL
,
3160 "Cannot allocate memory: %s\n", strerror(errno
));
3163 snprintf(envitem
, envitemlen
, "%s=%s", name
, value
);
3165 newenvc
= (*envc
) + 1;
3166 newenvp
= racoon_realloc(*envp
, newenvc
* sizeof(char *));
3167 if (newenvp
== NULL
) {
3168 plog(LLV_ERROR
, LOCATION
, NULL
,
3169 "Cannot allocate memory: %s\n", strerror(errno
));
3173 newenvp
[newenvc
- 2] = envitem
;
3174 newenvp
[newenvc
- 1] = NULL
;
3182 script_exec(script
, name
, envp
)
3187 char *argv
[] = { NULL
, NULL
, NULL
};
3190 if (script_paths
== NULL
) {
3191 plog(LLV_ERROR
, LOCATION
, NULL
,
3192 "privsep_script_exec: script_paths was not initialized\n");
3196 sp
= (vchar_t
**)(script_paths
->v
);
3198 argv
[0] = sp
[script
]->v
;
3199 argv
[1] = script_names
[name
];
3204 execve(argv
[0], argv
, envp
);
3205 plog(LLV_ERROR2
, LOCATION
, NULL
,
3206 "execve(\"%s\") failed: %s\n",
3207 argv
[0], strerror(errno
));
3211 plog(LLV_ERROR
, LOCATION
, NULL
,
3212 "Cannot fork: %s\n", strerror(errno
));
3224 struct ph1handle
*iph1
;
3226 vchar_t
*buf
= NULL
;
3227 struct sadb_msg
*msg
, *next
, *end
;
3229 struct sockaddr
*src
, *dst
;
3230 caddr_t mhp
[SADB_EXT_MAX
+ 1];
3232 struct ph2handle
*iph2
;
3233 struct ph1handle
*new_iph1
;
3235 plog(LLV_INFO
, LOCATION
, NULL
,
3236 "purging ISAKMP-SA spi=%s.\n",
3237 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3239 /* Mark as expired. */
3240 iph1
->status
= PHASE1ST_EXPIRED
;
3242 /* Check if we have another, still valid, phase1 SA. */
3243 new_iph1
= getph1byaddr(iph1
->local
, iph1
->remote
);
3246 * Delete all orphaned or binded to the deleting ph1handle phase2 SAs.
3247 * Keep all others phase2 SAs.
3249 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
3251 plog(LLV_DEBUG
, LOCATION
, NULL
,
3252 "pfkey_dump_sadb returned nothing.\n");
3256 msg
= (struct sadb_msg
*)buf
->v
;
3257 end
= (struct sadb_msg
*)(buf
->v
+ buf
->l
);
3260 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
3262 next
= (struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
3263 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
3268 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
3269 plog(LLV_ERROR
, LOCATION
, NULL
,
3270 "pfkey_check (%s)\n", ipsec_strerror());
3275 sa
= (struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
3277 !mhp
[SADB_EXT_ADDRESS_SRC
] ||
3278 !mhp
[SADB_EXT_ADDRESS_DST
]) {
3282 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
3283 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
3285 if (sa
->sadb_sa_state
!= SADB_SASTATE_LARVAL
&&
3286 sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
&&
3287 sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
3292 /* check in/outbound SAs */
3293 if ((CMPSADDR(iph1
->local
, src
) || CMPSADDR(iph1
->remote
, dst
)) &&
3294 (CMPSADDR(iph1
->local
, dst
) || CMPSADDR(iph1
->remote
, src
))) {
3299 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
3300 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
3302 /* Check if there is another valid ISAKMP-SA */
3303 if (new_iph1
!= NULL
) {
3306 /* No handler... still send a pfkey_delete message, but log this !*/
3307 plog(LLV_INFO
, LOCATION
, NULL
,
3308 "Unknown IPsec-SA spi=%u, hmmmm?\n",
3309 ntohl(sa
->sadb_sa_spi
));
3313 * If we have a new ph1, do not purge IPsec-SAs binded
3314 * to a different ISAKMP-SA
3316 if (iph2
->ph1
!= NULL
&& iph2
->ph1
!= iph1
){
3321 /* If the ph2handle is established, do not purge IPsec-SA */
3322 if (iph2
->status
== PHASE2ST_ESTABLISHED
||
3323 iph2
->status
== PHASE2ST_EXPIRED
) {
3325 plog(LLV_INFO
, LOCATION
, NULL
,
3326 "keeping IPsec-SA spi=%u - found valid ISAKMP-SA spi=%s.\n",
3327 ntohl(sa
->sadb_sa_spi
),
3328 isakmp_pindex(&(new_iph1
->index
), new_iph1
->msgid
));
3336 pfkey_send_delete(lcconf
->sock_pfkey
,
3337 msg
->sadb_msg_satype
,
3339 src
, dst
, sa
->sadb_sa_spi
);
3341 /* delete a relative phase 2 handle. */
3349 plog(LLV_INFO
, LOCATION
, NULL
,
3350 "purged IPsec-SA spi=%u.\n",
3351 ntohl(sa
->sadb_sa_spi
));
3359 /* Mark the phase1 handler as EXPIRED */
3360 plog(LLV_INFO
, LOCATION
, NULL
,
3361 "purged ISAKMP-SA spi=%s.\n",
3362 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3365 SCHED_KILL(iph1
->sce
);
3367 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
3372 struct ph2handle
*iph2
;
3377 /* Delete the SPD entry if we generated it
3379 if (iph2
->generated_spidx
) {
3380 struct policyindex spidx
;
3381 struct sockaddr_storage addr
;
3383 struct sockaddr
*src
= iph2
->src
;
3384 struct sockaddr
*dst
= iph2
->dst
;
3386 int idi2type
= 0;/* switch whether copy IDs into id[src,dst]. */
3388 plog(LLV_INFO
, LOCATION
, NULL
,
3389 "generated policy, deleting it.\n");
3391 memset(&spidx
, 0, sizeof(spidx
));
3392 iph2
->spidx_gen
= (caddr_t
)&spidx
;
3394 /* make inbound policy */
3397 spidx
.dir
= IPSEC_DIR_INBOUND
;
3401 * Note: code from get_proposal_r
3404 #define _XIDT(d) ((struct ipsecdoi_id_b *)(d)->v)->type
3407 * make destination address in spidx from either ID payload
3408 * or phase 1 address into a address in spidx.
3410 if (iph2
->id
!= NULL
3411 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3412 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
3413 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3414 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3415 /* get a destination address of a policy */
3416 error
= ipsecdoi_id2sockaddr(iph2
->id
,
3417 (struct sockaddr
*)&spidx
.dst
,
3418 &spidx
.prefd
, &spidx
.ul_proto
);
3424 * get scopeid from the SA address.
3425 * note that the phase 1 source address is used as
3426 * a destination address to search for a inbound
3427 * policy entry because rcoon is responder.
3429 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
3431 setscopeid((struct sockaddr
*)&spidx
.dst
,
3437 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3438 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
3439 idi2type
= _XIDT(iph2
->id
);
3443 plog(LLV_DEBUG
, LOCATION
, NULL
,
3444 "get a destination address of SP index "
3445 "from phase1 address "
3446 "due to no ID payloads found "
3447 "OR because ID type is not address.\n");
3450 * copy the SOURCE address of IKE into the
3451 * DESTINATION address of the key to search the
3452 * SPD because the direction of policy is inbound.
3454 memcpy(&spidx
.dst
, iph2
->src
, sysdep_sa_len(iph2
->src
));
3455 switch (spidx
.dst
.ss_family
) {
3458 sizeof(struct in_addr
) << 3;
3463 sizeof(struct in6_addr
) << 3;
3472 /* make source address in spidx */
3473 if (iph2
->id_p
!= NULL
3474 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
3475 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
3476 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3477 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3478 /* get a source address of inbound SA */
3479 error
= ipsecdoi_id2sockaddr(iph2
->id_p
,
3480 (struct sockaddr
*)&spidx
.src
,
3481 &spidx
.prefs
, &spidx
.ul_proto
);
3487 * get scopeid from the SA address.
3488 * for more detail, see above of this function.
3490 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
3492 setscopeid((struct sockaddr
*)&spidx
.src
,
3499 /* make id[src,dst] if both ID types are IP address and same */
3500 if (_XIDT(iph2
->id_p
) == idi2type
3501 && spidx
.dst
.ss_family
== spidx
.src
.ss_family
) {
3503 dupsaddr((struct sockaddr
*)&spidx
.dst
);
3505 dupsaddr((struct sockaddr
*)&spidx
.src
);
3509 plog(LLV_DEBUG
, LOCATION
, NULL
,
3510 "get a source address of SP index "
3511 "from phase1 address "
3512 "due to no ID payloads found "
3513 "OR because ID type is not address.\n");
3515 /* see above comment. */
3516 memcpy(&spidx
.src
, iph2
->dst
, sysdep_sa_len(iph2
->dst
));
3517 switch (spidx
.src
.ss_family
) {
3520 sizeof(struct in_addr
) << 3;
3525 sizeof(struct in6_addr
) << 3;
3536 plog(LLV_DEBUG
, LOCATION
, NULL
,
3537 "get a src address from ID payload "
3538 "%s prefixlen=%u ul_proto=%u\n",
3539 saddr2str((struct sockaddr
*)&spidx
.src
),
3540 spidx
.prefs
, spidx
.ul_proto
);
3541 plog(LLV_DEBUG
, LOCATION
, NULL
,
3542 "get dst address from ID payload "
3543 "%s prefixlen=%u ul_proto=%u\n",
3544 saddr2str((struct sockaddr
*)&spidx
.dst
),
3545 spidx
.prefd
, spidx
.ul_proto
);
3548 * convert the ul_proto if it is 0
3549 * because 0 in ID payload means a wild card.
3551 if (spidx
.ul_proto
== 0)
3552 spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
3556 /* End of code from get_proposal_r
3559 if (pk_sendspddelete(iph2
) < 0) {
3560 plog(LLV_ERROR
, LOCATION
, NULL
,
3561 "pfkey spddelete(inbound) failed.\n");
3563 plog(LLV_DEBUG
, LOCATION
, NULL
,
3564 "pfkey spddelete(inbound) sent.\n");
3567 #ifdef HAVE_POLICY_FWD
3568 /* make forward policy if required */
3569 if (tunnel_mode_prop(iph2
->approval
)) {
3570 spidx
.dir
= IPSEC_DIR_FWD
;
3571 if (pk_sendspddelete(iph2
) < 0) {
3572 plog(LLV_ERROR
, LOCATION
, NULL
,
3573 "pfkey spddelete(forward) failed.\n");
3575 plog(LLV_DEBUG
, LOCATION
, NULL
,
3576 "pfkey spddelete(forward) sent.\n");
3581 /* make outbound policy */
3584 spidx
.dir
= IPSEC_DIR_OUTBOUND
;
3586 spidx
.src
= spidx
.dst
;
3589 spidx
.prefs
= spidx
.prefd
;
3592 if (pk_sendspddelete(iph2
) < 0) {
3593 plog(LLV_ERROR
, LOCATION
, NULL
,
3594 "pfkey spddelete(outbound) failed.\n");
3596 plog(LLV_DEBUG
, LOCATION
, NULL
,
3597 "pfkey spddelete(outbound) sent.\n");
3600 iph2
->spidx_gen
=NULL
;
3606 setscopeid(sp_addr0
, sa_addr0
)
3607 struct sockaddr
*sp_addr0
, *sa_addr0
;
3609 struct sockaddr_in6
*sp_addr
, *sa_addr
;
3611 sp_addr
= (struct sockaddr_in6
*)sp_addr0
;
3612 sa_addr
= (struct sockaddr_in6
*)sa_addr0
;
3614 if (!IN6_IS_ADDR_LINKLOCAL(&sp_addr
->sin6_addr
)
3615 && !IN6_IS_ADDR_SITELOCAL(&sp_addr
->sin6_addr
)
3616 && !IN6_IS_ADDR_MULTICAST(&sp_addr
->sin6_addr
))
3619 /* this check should not be here ? */
3620 if (sa_addr
->sin6_family
!= AF_INET6
) {
3621 plog(LLV_ERROR
, LOCATION
, NULL
,
3622 "can't get scope ID: family mismatch\n");
3626 if (!IN6_IS_ADDR_LINKLOCAL(&sa_addr
->sin6_addr
)) {
3627 plog(LLV_ERROR
, LOCATION
, NULL
,
3628 "scope ID is not supported except of lladdr.\n");
3632 sp_addr
->sin6_scope_id
= sa_addr
->sin6_scope_id
;