isakmp_inf.c revision 1.22
1/*	$NetBSD: isakmp_inf.c,v 1.22 2007/07/18 12:07:51 vanhu Exp $	*/
2
3/* Id: isakmp_inf.c,v 1.44 2006/05/06 20:45:52 manubsd Exp */
4
5/*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "config.h"
35
36#include <sys/types.h>
37#include <sys/param.h>
38#include <sys/socket.h>
39
40#include <net/pfkeyv2.h>
41#include <netinet/in.h>
42#include <sys/queue.h>
43#include PATH_IPSEC_H
44
45#include <stdlib.h>
46#include <stdio.h>
47#include <string.h>
48#include <errno.h>
49#if TIME_WITH_SYS_TIME
50# include <sys/time.h>
51# include <time.h>
52#else
53# if HAVE_SYS_TIME_H
54#  include <sys/time.h>
55# else
56#  include <time.h>
57# endif
58#endif
59#ifdef ENABLE_HYBRID
60#include <resolv.h>
61#endif
62
63#include "libpfkey.h"
64
65#include "var.h"
66#include "vmbuf.h"
67#include "schedule.h"
68#include "str2val.h"
69#include "misc.h"
70#include "plog.h"
71#include "debug.h"
72
73#include "localconf.h"
74#include "remoteconf.h"
75#include "sockmisc.h"
76#include "handler.h"
77#include "policy.h"
78#include "proposal.h"
79#include "isakmp_var.h"
80#include "evt.h"
81#include "isakmp.h"
82#ifdef ENABLE_HYBRID
83#include "isakmp_xauth.h"
84#include "isakmp_unity.h"
85#include "isakmp_cfg.h"
86#endif
87#include "isakmp_inf.h"
88#include "oakley.h"
89#include "ipsec_doi.h"
90#include "crypto_openssl.h"
91#include "pfkey.h"
92#include "policy.h"
93#include "algorithm.h"
94#include "proposal.h"
95#include "admin.h"
96#include "strnames.h"
97#ifdef ENABLE_NATT
98#include "nattraversal.h"
99#endif
100
101/* information exchange */
102static int isakmp_info_recv_n (struct ph1handle *, struct isakmp_pl_n *, u_int32_t, int);
103static int isakmp_info_recv_d (struct ph1handle *, struct isakmp_pl_d *, u_int32_t, int);
104
105#ifdef ENABLE_DPD
106static int isakmp_info_recv_r_u __P((struct ph1handle *,
107	struct isakmp_pl_ru *, u_int32_t));
108static int isakmp_info_recv_r_u_ack __P((struct ph1handle *,
109	struct isakmp_pl_ru *, u_int32_t));
110static void isakmp_info_send_r_u __P((void *));
111#endif
112
113static void purge_isakmp_spi __P((int, isakmp_index *, size_t));
114static void info_recv_initialcontact __P((struct ph1handle *));
115
116/* %%%
117 * Information Exchange
118 */
119/*
120 * receive Information
121 */
122int
123isakmp_info_recv(iph1, msg0)
124	struct ph1handle *iph1;
125	vchar_t *msg0;
126{
127	vchar_t *msg = NULL;
128	vchar_t *pbuf = NULL;
129	u_int32_t msgid = 0;
130	int error = -1;
131	struct isakmp *isakmp;
132	struct isakmp_gen *gen;
133	struct isakmp_parse_t *pa, *pap;
134	void *p;
135	vchar_t *hash, *payload;
136	struct isakmp_gen *nd;
137	u_int8_t np;
138	int encrypted;
139	int flag;
140
141	plog(LLV_DEBUG, LOCATION, NULL, "receive Information.\n");
142
143	encrypted = ISSET(((struct isakmp *)msg0->v)->flags, ISAKMP_FLAG_E);
144	msgid = ((struct isakmp *)msg0->v)->msgid;
145
146	/* Use new IV to decrypt Informational message. */
147	if (encrypted) {
148		struct isakmp_ivm *ivm;
149
150		if (iph1->ivm == NULL) {
151			plog(LLV_ERROR, LOCATION, NULL, "iph1->ivm == NULL\n");
152			return -1;
153		}
154
155		/* compute IV */
156		ivm = oakley_newiv2(iph1, ((struct isakmp *)msg0->v)->msgid);
157		if (ivm == NULL)
158			return -1;
159
160		msg = oakley_do_decrypt(iph1, msg0, ivm->iv, ivm->ive);
161		oakley_delivm(ivm);
162		if (msg == NULL)
163			return -1;
164
165	} else
166		msg = vdup(msg0);
167
168	/* Safety check */
169	if (msg->l < sizeof(*isakmp) + sizeof(*gen)) {
170		plog(LLV_ERROR, LOCATION, NULL,
171			"ignore information because the "
172			"message is way too short\n");
173		goto end;
174	}
175
176	isakmp = (struct isakmp *)msg->v;
177	gen = (struct isakmp_gen *)((caddr_t)isakmp + sizeof(struct isakmp));
178	np = gen->np;
179
180	if (encrypted) {
181		if (isakmp->np != ISAKMP_NPTYPE_HASH) {
182			plog(LLV_ERROR, LOCATION, NULL,
183			    "ignore information because the"
184			    "message has no hash payload.\n");
185			goto end;
186		}
187
188		if (iph1->status != PHASE1ST_ESTABLISHED) {
189			plog(LLV_ERROR, LOCATION, NULL,
190			    "ignore information because ISAKMP-SA"
191			    "has not been established yet.\n");
192			goto end;
193		}
194
195		/* Safety check */
196		if (msg->l < sizeof(*isakmp) + ntohs(gen->len) + sizeof(*nd)) {
197			plog(LLV_ERROR, LOCATION, NULL,
198				"ignore information because the "
199				"message is too short\n");
200			goto end;
201		}
202
203		p = (caddr_t) gen + sizeof(struct isakmp_gen);
204		nd = (struct isakmp_gen *) ((caddr_t) gen + ntohs(gen->len));
205
206		/* nd length check */
207		if (ntohs(nd->len) > msg->l - (sizeof(struct isakmp) +
208		    ntohs(gen->len))) {
209			plog(LLV_ERROR, LOCATION, NULL,
210				 "too long payload length (broken message?)\n");
211			goto end;
212		}
213
214		if (ntohs(nd->len) < sizeof(*nd)) {
215			plog(LLV_ERROR, LOCATION, NULL,
216				"too short payload length (broken message?)\n");
217			goto end;
218		}
219
220		payload = vmalloc(ntohs(nd->len));
221		if (payload == NULL) {
222			plog(LLV_ERROR, LOCATION, NULL,
223			    "cannot allocate memory\n");
224			goto end;
225		}
226
227		memcpy(payload->v, (caddr_t) nd, ntohs(nd->len));
228
229		/* compute HASH */
230		hash = oakley_compute_hash1(iph1, isakmp->msgid, payload);
231		if (hash == NULL) {
232			plog(LLV_ERROR, LOCATION, NULL,
233			    "cannot compute hash\n");
234
235			vfree(payload);
236			goto end;
237		}
238
239		if (ntohs(gen->len) - sizeof(struct isakmp_gen) != hash->l) {
240			plog(LLV_ERROR, LOCATION, NULL,
241			    "ignore information due to hash length mismatch\n");
242
243			vfree(hash);
244			vfree(payload);
245			goto end;
246		}
247
248		if (memcmp(p, hash->v, hash->l) != 0) {
249			plog(LLV_ERROR, LOCATION, NULL,
250			    "ignore information due to hash mismatch\n");
251
252			vfree(hash);
253			vfree(payload);
254			goto end;
255		}
256
257		plog(LLV_DEBUG, LOCATION, NULL, "hash validated.\n");
258
259		vfree(hash);
260		vfree(payload);
261	} else {
262		/* make sure the packet was encrypted after the beginning of phase 1. */
263		switch (iph1->etype) {
264		case ISAKMP_ETYPE_AGG:
265		case ISAKMP_ETYPE_BASE:
266		case ISAKMP_ETYPE_IDENT:
267			if ((iph1->side == INITIATOR && iph1->status < PHASE1ST_MSG3SENT)
268			 || (iph1->side == RESPONDER && iph1->status < PHASE1ST_MSG2SENT)) {
269				break;
270			}
271			/*FALLTHRU*/
272		default:
273			plog(LLV_ERROR, LOCATION, iph1->remote,
274				"%s message must be encrypted\n",
275				s_isakmp_nptype(np));
276			error = 0;
277			goto end;
278		}
279	}
280
281	if (!(pbuf = isakmp_parse(msg))) {
282		error = -1;
283		goto end;
284	}
285
286	error = 0;
287	for (pa = (struct isakmp_parse_t *)pbuf->v; pa->type; pa++) {
288		switch (pa->type) {
289		case ISAKMP_NPTYPE_HASH:
290			/* Handled above */
291			break;
292		case ISAKMP_NPTYPE_N:
293			error = isakmp_info_recv_n(iph1,
294				(struct isakmp_pl_n *)pa->ptr,
295				msgid, encrypted);
296			break;
297		case ISAKMP_NPTYPE_D:
298			error = isakmp_info_recv_d(iph1,
299				(struct isakmp_pl_d *)pa->ptr,
300				msgid, encrypted);
301			break;
302		case ISAKMP_NPTYPE_NONCE:
303			/* XXX to be 6.4.2 ike-01.txt */
304			/* XXX IV is to be synchronized. */
305			plog(LLV_ERROR, LOCATION, iph1->remote,
306				"ignore Acknowledged Informational\n");
307			break;
308		default:
309			/* don't send information, see isakmp_ident_r1() */
310			error = 0;
311			plog(LLV_ERROR, LOCATION, iph1->remote,
312				"reject the packet, "
313				"received unexpected payload type %s.\n",
314				s_isakmp_nptype(gen->np));
315		}
316		if(error < 0) {
317			break;
318		} else {
319			flag |= error;
320		}
321	}
322    end:
323	if (msg != NULL)
324		vfree(msg);
325	if (pbuf != NULL)
326		vfree(pbuf);
327	return error;
328}
329
330/*
331 * handling of Notification payload
332 */
333static int
334isakmp_info_recv_n(iph1, notify, msgid, encrypted)
335	struct ph1handle *iph1;
336	struct isakmp_pl_n *notify;
337	u_int32_t msgid;
338	int encrypted;
339{
340	u_int type;
341	vchar_t *pbuf;
342	vchar_t *ndata;
343	char *nraw;
344	size_t l;
345	char *spi;
346
347	type = ntohs(notify->type);
348
349	switch (type) {
350	case ISAKMP_NTYPE_CONNECTED:
351	case ISAKMP_NTYPE_RESPONDER_LIFETIME:
352	case ISAKMP_NTYPE_REPLAY_STATUS:
353#ifdef ENABLE_HYBRID
354	case ISAKMP_NTYPE_UNITY_HEARTBEAT:
355#endif
356		/* do something */
357		break;
358	case ISAKMP_NTYPE_INITIAL_CONTACT:
359		if (encrypted)
360			info_recv_initialcontact(iph1);
361			return 0;
362		break;
363#ifdef ENABLE_DPD
364	case ISAKMP_NTYPE_R_U_THERE:
365		if (encrypted)
366			return isakmp_info_recv_r_u(iph1,
367				(struct isakmp_pl_ru *)notify, msgid);
368		break;
369	case ISAKMP_NTYPE_R_U_THERE_ACK:
370		if (encrypted)
371			return isakmp_info_recv_r_u_ack(iph1,
372				(struct isakmp_pl_ru *)notify, msgid);
373		break;
374#endif
375	default:
376	    {
377		/* XXX there is a potential of dos attack. */
378		if(type >= ISAKMP_NTYPE_MINERROR &&
379		   type <= ISAKMP_NTYPE_MAXERROR) {
380			if (msgid == 0) {
381				/* don't think this realy deletes ph1 ? */
382				plog(LLV_ERROR, LOCATION, iph1->remote,
383					"delete phase1 handle.\n");
384				return -1;
385			} else {
386				if (getph2bymsgid(iph1, msgid) == NULL) {
387					plog(LLV_ERROR, LOCATION, iph1->remote,
388						"fatal %s notify messsage, "
389						"phase1 should be deleted.\n",
390						s_isakmp_notify_msg(type));
391				} else {
392					plog(LLV_ERROR, LOCATION, iph1->remote,
393						"fatal %s notify messsage, "
394						"phase2 should be deleted.\n",
395						s_isakmp_notify_msg(type));
396				}
397			}
398		} else {
399			plog(LLV_ERROR, LOCATION, iph1->remote,
400				"unhandled notify message %s, "
401				"no phase2 handle found.\n",
402				s_isakmp_notify_msg(type));
403		}
404	    }
405	    break;
406	}
407
408	/* get spi if specified and allocate */
409	if(notify->spi_size > 0) {
410		if (ntohs(notify->h.len) < sizeof(*notify) + notify->spi_size) {
411			plog(LLV_ERROR, LOCATION, iph1->remote,
412				"invalid spi_size in notification payload.\n");
413			return -1;
414		}
415		spi = val2str((char *)(notify + 1), notify->spi_size);
416
417		plog(LLV_DEBUG, LOCATION, iph1->remote,
418			"notification message %d:%s, "
419			"doi=%d proto_id=%d spi=%s(size=%d).\n",
420			type, s_isakmp_notify_msg(type),
421			ntohl(notify->doi), notify->proto_id, spi, notify->spi_size);
422
423		racoon_free(spi);
424	}
425
426	/* Send the message data to the logs */
427	if(type >= ISAKMP_NTYPE_MINERROR &&
428	   type <= ISAKMP_NTYPE_MAXERROR) {
429		l = ntohs(notify->h.len) - sizeof(*notify) - notify->spi_size;
430		if (l > 0) {
431			nraw = (char*)notify;
432			nraw += sizeof(*notify) + notify->spi_size;
433			if ((ndata = vmalloc(l)) != NULL) {
434				memcpy(ndata->v, nraw, ndata->l);
435				plog(LLV_ERROR, LOCATION, iph1->remote,
436				    "Message: '%s'.\n",
437				    binsanitize(ndata->v, ndata->l));
438				vfree(ndata);
439			} else {
440				plog(LLV_ERROR, LOCATION, iph1->remote,
441				    "Cannot allocate memory\n");
442			}
443		}
444	}
445	return 0;
446}
447
448/*
449 * handling of Deletion payload
450 */
451static int
452isakmp_info_recv_d(iph1, delete, msgid, encrypted)
453	struct ph1handle *iph1;
454	struct isakmp_pl_d *delete;
455	u_int32_t msgid;
456	int encrypted;
457{
458	int tlen, num_spi;
459	vchar_t *pbuf;
460	int protected = 0;
461	struct ph1handle *del_ph1;
462	struct ph2handle *iph2;
463	union {
464		u_int32_t spi32;
465		u_int16_t spi16[2];
466	} spi;
467
468	if (ntohl(delete->doi) != IPSEC_DOI) {
469		plog(LLV_ERROR, LOCATION, iph1->remote,
470			"delete payload with invalid doi:%d.\n",
471			ntohl(delete->doi));
472#ifdef ENABLE_HYBRID
473		/*
474		 * At deconnexion time, Cisco VPN client does this
475		 * with a zero DOI. Don't give up in that situation.
476		 */
477		if (((iph1->mode_cfg->flags &
478		    ISAKMP_CFG_VENDORID_UNITY) == 0) || (delete->doi != 0))
479			return 0;
480#else
481		return 0;
482#endif
483	}
484
485	num_spi = ntohs(delete->num_spi);
486	tlen = ntohs(delete->h.len) - sizeof(struct isakmp_pl_d);
487
488	if (tlen != num_spi * delete->spi_size) {
489		plog(LLV_ERROR, LOCATION, iph1->remote,
490			"deletion payload with invalid length.\n");
491		return 0;
492	}
493
494	plog(LLV_DEBUG, LOCATION, iph1->remote,
495		"delete payload for protocol %s\n",
496		s_ipsecdoi_proto(delete->proto_id));
497
498	if(!iph1->rmconf->weak_phase1_check && !encrypted) {
499		plog(LLV_WARNING, LOCATION, iph1->remote,
500			"Ignoring unencrypted delete payload "
501			"(check the weak_phase1_check option)\n");
502		return 0;
503	}
504
505	switch (delete->proto_id) {
506	case IPSECDOI_PROTO_ISAKMP:
507		if (delete->spi_size != sizeof(isakmp_index)) {
508			plog(LLV_ERROR, LOCATION, iph1->remote,
509				"delete payload with strange spi "
510				"size %d(proto_id:%d)\n",
511				delete->spi_size, delete->proto_id);
512			return 0;
513		}
514
515		del_ph1=getph1byindex((isakmp_index *)(delete + 1));
516		if(del_ph1 != NULL){
517
518			EVT_PUSH(del_ph1->local, del_ph1->remote,
519					 EVTT_PEERPH1_NOPROP, NULL);
520			SCHED_KILL(del_ph1->scr);
521
522			/*
523			 * Do not delete IPsec SAs when receiving an IKE delete notification.
524			 * Just delete the IKE SA.
525			 */
526			isakmp_ph1expire(del_ph1);
527		}
528		break;
529
530	case IPSECDOI_PROTO_IPSEC_AH:
531	case IPSECDOI_PROTO_IPSEC_ESP:
532		if (delete->spi_size != sizeof(u_int32_t)) {
533			plog(LLV_ERROR, LOCATION, iph1->remote,
534				"delete payload with strange spi "
535				"size %d(proto_id:%d)\n",
536				delete->spi_size, delete->proto_id);
537			return 0;
538		}
539		EVT_PUSH(iph1->local, iph1->remote,
540		    EVTT_PEER_DELETE, NULL);
541		purge_ipsec_spi(iph1->remote, delete->proto_id,
542		    (u_int32_t *)(delete + 1), num_spi);
543		break;
544
545	case IPSECDOI_PROTO_IPCOMP:
546		/* need to handle both 16bit/32bit SPI */
547		memset(&spi, 0, sizeof(spi));
548		if (delete->spi_size == sizeof(spi.spi16[1])) {
549			memcpy(&spi.spi16[1], delete + 1,
550			    sizeof(spi.spi16[1]));
551		} else if (delete->spi_size == sizeof(spi.spi32))
552			memcpy(&spi.spi32, delete + 1, sizeof(spi.spi32));
553		else {
554			plog(LLV_ERROR, LOCATION, iph1->remote,
555				"delete payload with strange spi "
556				"size %d(proto_id:%d)\n",
557				delete->spi_size, delete->proto_id);
558			return 0;
559		}
560		purge_ipsec_spi(iph1->remote, delete->proto_id,
561		    &spi.spi32, num_spi);
562		break;
563
564	default:
565		plog(LLV_ERROR, LOCATION, iph1->remote,
566			"deletion message received, "
567			"invalid proto_id: %d\n",
568			delete->proto_id);
569		return 0;
570	}
571
572	plog(LLV_DEBUG, LOCATION, NULL, "purged SAs.\n");
573
574	return 0;
575}
576
577/*
578 * send Delete payload (for ISAKMP SA) in Informational exchange.
579 */
580int
581isakmp_info_send_d1(iph1)
582	struct ph1handle *iph1;
583{
584	struct isakmp_pl_d *d;
585	vchar_t *payload = NULL;
586	int tlen;
587	int error = 0;
588
589	if (iph1->status != PHASE2ST_ESTABLISHED)
590		return 0;
591
592	/* create delete payload */
593
594	/* send SPIs of inbound SAs. */
595	/* XXX should send outbound SAs's ? */
596	tlen = sizeof(*d) + sizeof(isakmp_index);
597	payload = vmalloc(tlen);
598	if (payload == NULL) {
599		plog(LLV_ERROR, LOCATION, NULL,
600			"failed to get buffer for payload.\n");
601		return errno;
602	}
603
604	d = (struct isakmp_pl_d *)payload->v;
605	d->h.np = ISAKMP_NPTYPE_NONE;
606	d->h.len = htons(tlen);
607	d->doi = htonl(IPSEC_DOI);
608	d->proto_id = IPSECDOI_PROTO_ISAKMP;
609	d->spi_size = sizeof(isakmp_index);
610	d->num_spi = htons(1);
611	memcpy(d + 1, &iph1->index, sizeof(isakmp_index));
612
613	error = isakmp_info_send_common(iph1, payload,
614					ISAKMP_NPTYPE_D, 0);
615	vfree(payload);
616
617	return error;
618}
619
620/*
621 * send Delete payload (for IPsec SA) in Informational exchange, based on
622 * pfkey msg.  It sends always single SPI.
623 */
624int
625isakmp_info_send_d2(iph2)
626	struct ph2handle *iph2;
627{
628	struct ph1handle *iph1;
629	struct saproto *pr;
630	struct isakmp_pl_d *d;
631	vchar_t *payload = NULL;
632	int tlen;
633	int error = 0;
634	u_int8_t *spi;
635
636	if (iph2->status != PHASE2ST_ESTABLISHED)
637		return 0;
638
639	/*
640	 * don't send delete information if there is no phase 1 handler.
641	 * It's nonsensical to negotiate phase 1 to send the information.
642	 */
643	iph1 = getph1byaddr(iph2->src, iph2->dst);
644	if (iph1 == NULL){
645		plog(LLV_DEBUG2, LOCATION, NULL,
646			 "No ph1 handler found, could not send DELETE_SA\n");
647		return 0;
648	}
649
650	/* create delete payload */
651	for (pr = iph2->approval->head; pr != NULL; pr = pr->next) {
652
653		/* send SPIs of inbound SAs. */
654		/*
655		 * XXX should I send outbound SAs's ?
656		 * I send inbound SAs's SPI only at the moment because I can't
657		 * decode any more if peer send encoded packet without aware of
658		 * deletion of SA.  Outbound SAs don't come under the situation.
659		 */
660		tlen = sizeof(*d) + pr->spisize;
661		payload = vmalloc(tlen);
662		if (payload == NULL) {
663			plog(LLV_ERROR, LOCATION, NULL,
664				"failed to get buffer for payload.\n");
665			return errno;
666		}
667
668		d = (struct isakmp_pl_d *)payload->v;
669		d->h.np = ISAKMP_NPTYPE_NONE;
670		d->h.len = htons(tlen);
671		d->doi = htonl(IPSEC_DOI);
672		d->proto_id = pr->proto_id;
673		d->spi_size = pr->spisize;
674		d->num_spi = htons(1);
675		/*
676		 * XXX SPI bits are left-filled, for use with IPComp.
677		 * we should be switching to variable-length spi field...
678		 */
679		spi = (u_int8_t *)&pr->spi;
680		spi += sizeof(pr->spi);
681		spi -= pr->spisize;
682		memcpy(d + 1, spi, pr->spisize);
683
684		error = isakmp_info_send_common(iph1, payload,
685						ISAKMP_NPTYPE_D, 0);
686		vfree(payload);
687	}
688
689	return error;
690}
691
692/*
693 * send Notification payload (for without ISAKMP SA) in Informational exchange
694 */
695int
696isakmp_info_send_nx(isakmp, remote, local, type, data)
697	struct isakmp *isakmp;
698	struct sockaddr *remote, *local;
699	int type;
700	vchar_t *data;
701{
702	struct ph1handle *iph1 = NULL;
703	struct remoteconf *rmconf;
704	vchar_t *payload = NULL;
705	int tlen;
706	int error = -1;
707	struct isakmp_pl_n *n;
708	int spisiz = 0;		/* see below */
709
710	/* search appropreate configuration */
711	rmconf = getrmconf(remote);
712	if (rmconf == NULL) {
713		plog(LLV_ERROR, LOCATION, remote,
714			"no configuration found for peer address.\n");
715		goto end;
716	}
717
718	/* add new entry to isakmp status table. */
719	iph1 = newph1();
720	if (iph1 == NULL)
721		return -1;
722
723	memcpy(&iph1->index.i_ck, &isakmp->i_ck, sizeof(cookie_t));
724	isakmp_newcookie((char *)&iph1->index.r_ck, remote, local);
725	iph1->status = PHASE1ST_START;
726	iph1->rmconf = rmconf;
727	iph1->side = INITIATOR;
728	iph1->version = isakmp->v;
729	iph1->flags = 0;
730	iph1->msgid = 0;	/* XXX */
731#ifdef ENABLE_HYBRID
732	if ((iph1->mode_cfg = isakmp_cfg_mkstate()) == NULL) {
733		error = -1;
734		goto end;
735	}
736#endif
737#ifdef ENABLE_FRAG
738	iph1->frag = 0;
739	iph1->frag_chain = NULL;
740#endif
741
742	/* copy remote address */
743	if (copy_ph1addresses(iph1, rmconf, remote, local) < 0) {
744		error = -1;
745		goto end;
746	}
747
748	tlen = sizeof(*n) + spisiz;
749	if (data)
750		tlen += data->l;
751	payload = vmalloc(tlen);
752	if (payload == NULL) {
753		plog(LLV_ERROR, LOCATION, NULL,
754			"failed to get buffer to send.\n");
755		error = -1;
756		goto end;
757	}
758
759	n = (struct isakmp_pl_n *)payload->v;
760	n->h.np = ISAKMP_NPTYPE_NONE;
761	n->h.len = htons(tlen);
762	n->doi = htonl(IPSEC_DOI);
763	n->proto_id = IPSECDOI_KEY_IKE;
764	n->spi_size = spisiz;
765	n->type = htons(type);
766	if (spisiz)
767		memset(n + 1, 0, spisiz);	/* XXX spisiz is always 0 */
768	if (data)
769		memcpy((caddr_t)(n + 1) + spisiz, data->v, data->l);
770
771	error = isakmp_info_send_common(iph1, payload, ISAKMP_NPTYPE_N, 0);
772	vfree(payload);
773
774    end:
775	if (iph1 != NULL)
776		delph1(iph1);
777
778	return error;
779}
780
781/*
782 * send Notification payload (for ISAKMP SA) in Informational exchange
783 */
784int
785isakmp_info_send_n1(iph1, type, data)
786	struct ph1handle *iph1;
787	int type;
788	vchar_t *data;
789{
790	vchar_t *payload = NULL;
791	int tlen;
792	int error = 0;
793	struct isakmp_pl_n *n;
794	int spisiz;
795
796	/*
797	 * note on SPI size: which description is correct?  I have chosen
798	 * this to be 0.
799	 *
800	 * RFC2408 3.1, 2nd paragraph says: ISAKMP SA is identified by
801	 * Initiator/Responder cookie and SPI has no meaning, SPI size = 0.
802	 * RFC2408 3.1, first paragraph on page 40: ISAKMP SA is identified
803	 * by cookie and SPI has no meaning, 0 <= SPI size <= 16.
804	 * RFC2407 4.6.3.3, INITIAL-CONTACT is required to set to 16.
805	 */
806	if (type == ISAKMP_NTYPE_INITIAL_CONTACT)
807		spisiz = sizeof(isakmp_index);
808	else
809		spisiz = 0;
810
811	tlen = sizeof(*n) + spisiz;
812	if (data)
813		tlen += data->l;
814	payload = vmalloc(tlen);
815	if (payload == NULL) {
816		plog(LLV_ERROR, LOCATION, NULL,
817			"failed to get buffer to send.\n");
818		return errno;
819	}
820
821	n = (struct isakmp_pl_n *)payload->v;
822	n->h.np = ISAKMP_NPTYPE_NONE;
823	n->h.len = htons(tlen);
824	n->doi = htonl(iph1->rmconf->doitype);
825	n->proto_id = IPSECDOI_PROTO_ISAKMP; /* XXX to be configurable ? */
826	n->spi_size = spisiz;
827	n->type = htons(type);
828	if (spisiz)
829		memcpy(n + 1, &iph1->index, sizeof(isakmp_index));
830	if (data)
831		memcpy((caddr_t)(n + 1) + spisiz, data->v, data->l);
832
833	error = isakmp_info_send_common(iph1, payload, ISAKMP_NPTYPE_N, iph1->flags);
834	vfree(payload);
835
836	return error;
837}
838
839/*
840 * send Notification payload (for IPsec SA) in Informational exchange
841 */
842int
843isakmp_info_send_n2(iph2, type, data)
844	struct ph2handle *iph2;
845	int type;
846	vchar_t *data;
847{
848	struct ph1handle *iph1 = iph2->ph1;
849	vchar_t *payload = NULL;
850	int tlen;
851	int error = 0;
852	struct isakmp_pl_n *n;
853	struct saproto *pr;
854
855	if (!iph2->approval)
856		return EINVAL;
857
858	pr = iph2->approval->head;
859
860	/* XXX must be get proper spi */
861	tlen = sizeof(*n) + pr->spisize;
862	if (data)
863		tlen += data->l;
864	payload = vmalloc(tlen);
865	if (payload == NULL) {
866		plog(LLV_ERROR, LOCATION, NULL,
867			"failed to get buffer to send.\n");
868		return errno;
869	}
870
871	n = (struct isakmp_pl_n *)payload->v;
872	n->h.np = ISAKMP_NPTYPE_NONE;
873	n->h.len = htons(tlen);
874	n->doi = htonl(IPSEC_DOI);		/* IPSEC DOI (1) */
875	n->proto_id = pr->proto_id;		/* IPSEC AH/ESP/whatever*/
876	n->spi_size = pr->spisize;
877	n->type = htons(type);
878	*(u_int32_t *)(n + 1) = pr->spi;
879	if (data)
880		memcpy((caddr_t)(n + 1) + pr->spisize, data->v, data->l);
881
882	iph2->flags |= ISAKMP_FLAG_E;	/* XXX Should we do FLAG_A ? */
883	error = isakmp_info_send_common(iph1, payload, ISAKMP_NPTYPE_N, iph2->flags);
884	vfree(payload);
885
886	return error;
887}
888
889/*
890 * send Information
891 * When ph1->skeyid_a == NULL, send message without encoding.
892 */
893int
894isakmp_info_send_common(iph1, payload, np, flags)
895	struct ph1handle *iph1;
896	vchar_t *payload;
897	u_int32_t np;
898	int flags;
899{
900	struct ph2handle *iph2 = NULL;
901	vchar_t *hash = NULL;
902	struct isakmp *isakmp;
903	struct isakmp_gen *gen;
904	char *p;
905	int tlen;
906	int error = -1;
907
908	/* add new entry to isakmp status table */
909	iph2 = newph2();
910	if (iph2 == NULL)
911		goto end;
912
913	iph2->dst = dupsaddr(iph1->remote);
914	if (iph2->dst == NULL) {
915		delph2(iph2);
916		goto end;
917	}
918	iph2->src = dupsaddr(iph1->local);
919	if (iph2->src == NULL) {
920		delph2(iph2);
921		goto end;
922	}
923	switch (iph1->remote->sa_family) {
924	case AF_INET:
925#if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
926		((struct sockaddr_in *)iph2->dst)->sin_port = 0;
927		((struct sockaddr_in *)iph2->src)->sin_port = 0;
928#endif
929		break;
930#ifdef INET6
931	case AF_INET6:
932#if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
933		((struct sockaddr_in6 *)iph2->dst)->sin6_port = 0;
934		((struct sockaddr_in6 *)iph2->src)->sin6_port = 0;
935#endif
936		break;
937#endif
938	default:
939		plog(LLV_ERROR, LOCATION, NULL,
940			"invalid family: %d\n", iph1->remote->sa_family);
941		delph2(iph2);
942		goto end;
943	}
944	iph2->ph1 = iph1;
945	iph2->side = INITIATOR;
946	iph2->status = PHASE2ST_START;
947	iph2->msgid = isakmp_newmsgid2(iph1);
948
949	/* get IV and HASH(1) if skeyid_a was generated. */
950	if (iph1->skeyid_a != NULL) {
951		iph2->ivm = oakley_newiv2(iph1, iph2->msgid);
952		if (iph2->ivm == NULL) {
953			delph2(iph2);
954			goto end;
955		}
956
957		/* generate HASH(1) */
958		hash = oakley_compute_hash1(iph2->ph1, iph2->msgid, payload);
959		if (hash == NULL) {
960			delph2(iph2);
961			goto end;
962		}
963
964		/* initialized total buffer length */
965		tlen = hash->l;
966		tlen += sizeof(*gen);
967	} else {
968		/* IKE-SA is not established */
969		hash = NULL;
970
971		/* initialized total buffer length */
972		tlen = 0;
973	}
974	if ((flags & ISAKMP_FLAG_A) == 0)
975		iph2->flags = (hash == NULL ? 0 : ISAKMP_FLAG_E);
976	else
977		iph2->flags = (hash == NULL ? 0 : ISAKMP_FLAG_A);
978
979	insph2(iph2);
980	bindph12(iph1, iph2);
981
982	tlen += sizeof(*isakmp) + payload->l;
983
984	/* create buffer for isakmp payload */
985	iph2->sendbuf = vmalloc(tlen);
986	if (iph2->sendbuf == NULL) {
987		plog(LLV_ERROR, LOCATION, NULL,
988			"failed to get buffer to send.\n");
989		goto err;
990	}
991
992	/* create isakmp header */
993	isakmp = (struct isakmp *)iph2->sendbuf->v;
994	memcpy(&isakmp->i_ck, &iph1->index.i_ck, sizeof(cookie_t));
995	memcpy(&isakmp->r_ck, &iph1->index.r_ck, sizeof(cookie_t));
996	isakmp->np = hash == NULL ? (np & 0xff) : ISAKMP_NPTYPE_HASH;
997	isakmp->v = iph1->version;
998	isakmp->etype = ISAKMP_ETYPE_INFO;
999	isakmp->flags = iph2->flags;
1000	memcpy(&isakmp->msgid, &iph2->msgid, sizeof(isakmp->msgid));
1001	isakmp->len   = htonl(tlen);
1002	p = (char *)(isakmp + 1);
1003
1004	/* create HASH payload */
1005	if (hash != NULL) {
1006		gen = (struct isakmp_gen *)p;
1007		gen->np = np & 0xff;
1008		gen->len = htons(sizeof(*gen) + hash->l);
1009		p += sizeof(*gen);
1010		memcpy(p, hash->v, hash->l);
1011		p += hash->l;
1012	}
1013
1014	/* add payload */
1015	memcpy(p, payload->v, payload->l);
1016	p += payload->l;
1017
1018#ifdef HAVE_PRINT_ISAKMP_C
1019	isakmp_printpacket(iph2->sendbuf, iph1->local, iph1->remote, 1);
1020#endif
1021
1022	/* encoding */
1023	if (ISSET(isakmp->flags, ISAKMP_FLAG_E)) {
1024		vchar_t *tmp;
1025
1026		tmp = oakley_do_encrypt(iph2->ph1, iph2->sendbuf, iph2->ivm->ive,
1027				iph2->ivm->iv);
1028		VPTRINIT(iph2->sendbuf);
1029		if (tmp == NULL)
1030			goto err;
1031		iph2->sendbuf = tmp;
1032	}
1033
1034	/* HDR*, HASH(1), N */
1035	if (isakmp_send(iph2->ph1, iph2->sendbuf) < 0) {
1036		VPTRINIT(iph2->sendbuf);
1037		goto err;
1038	}
1039
1040	plog(LLV_DEBUG, LOCATION, NULL,
1041		"sendto Information %s.\n", s_isakmp_nptype(np));
1042
1043	/*
1044	 * don't resend notify message because peer can use Acknowledged
1045	 * Informational if peer requires the reply of the notify message.
1046	 */
1047
1048	/* XXX If Acknowledged Informational required, don't delete ph2handle */
1049	error = 0;
1050	VPTRINIT(iph2->sendbuf);
1051	goto err;	/* XXX */
1052
1053end:
1054	if (hash)
1055		vfree(hash);
1056	return error;
1057
1058err:
1059	unbindph12(iph2);
1060	remph2(iph2);
1061	delph2(iph2);
1062	goto end;
1063}
1064
1065/*
1066 * add a notify payload to buffer by reallocating buffer.
1067 * If buf == NULL, the function only create a notify payload.
1068 *
1069 * XXX Which is SPI to be included, inbound or outbound ?
1070 */
1071vchar_t *
1072isakmp_add_pl_n(buf0, np_p, type, pr, data)
1073	vchar_t *buf0;
1074	u_int8_t **np_p;
1075	int type;
1076	struct saproto *pr;
1077	vchar_t *data;
1078{
1079	vchar_t *buf = NULL;
1080	struct isakmp_pl_n *n;
1081	int tlen;
1082	int oldlen = 0;
1083
1084	if (*np_p)
1085		**np_p = ISAKMP_NPTYPE_N;
1086
1087	tlen = sizeof(*n) + pr->spisize;
1088
1089	if (data)
1090		tlen += data->l;
1091	if (buf0) {
1092		oldlen = buf0->l;
1093		buf = vrealloc(buf0, buf0->l + tlen);
1094	} else
1095		buf = vmalloc(tlen);
1096	if (!buf) {
1097		plog(LLV_ERROR, LOCATION, NULL,
1098			"failed to get a payload buffer.\n");
1099		return NULL;
1100	}
1101
1102	n = (struct isakmp_pl_n *)(buf->v + oldlen);
1103	n->h.np = ISAKMP_NPTYPE_NONE;
1104	n->h.len = htons(tlen);
1105	n->doi = htonl(IPSEC_DOI);		/* IPSEC DOI (1) */
1106	n->proto_id = pr->proto_id;		/* IPSEC AH/ESP/whatever*/
1107	n->spi_size = pr->spisize;
1108	n->type = htons(type);
1109	*(u_int32_t *)(n + 1) = pr->spi;	/* XXX */
1110	if (data)
1111		memcpy((caddr_t)(n + 1) + pr->spisize, data->v, data->l);
1112
1113	/* save the pointer of next payload type */
1114	*np_p = &n->h.np;
1115
1116	return buf;
1117}
1118
1119static void
1120purge_isakmp_spi(proto, spi, n)
1121	int proto;
1122	isakmp_index *spi;	/*network byteorder*/
1123	size_t n;
1124{
1125	struct ph1handle *iph1;
1126	size_t i;
1127
1128	for (i = 0; i < n; i++) {
1129		iph1 = getph1byindex(&spi[i]);
1130		if (!iph1)
1131			continue;
1132
1133		plog(LLV_INFO, LOCATION, NULL,
1134			"purged ISAKMP-SA proto_id=%s spi=%s.\n",
1135			s_ipsecdoi_proto(proto),
1136			isakmp_pindex(&spi[i], 0));
1137
1138		SCHED_KILL(iph1->sce);
1139		iph1->status = PHASE1ST_EXPIRED;
1140		iph1->sce = sched_new(1, isakmp_ph1delete_stub, iph1);
1141	}
1142}
1143
1144
1145
1146void
1147purge_ipsec_spi(dst0, proto, spi, n)
1148	struct sockaddr *dst0;
1149	int proto;
1150	u_int32_t *spi;	/*network byteorder*/
1151	size_t n;
1152{
1153	vchar_t *buf = NULL;
1154	struct sadb_msg *msg, *next, *end;
1155	struct sadb_sa *sa;
1156	struct sadb_lifetime *lt;
1157	struct sockaddr *src, *dst;
1158	struct ph2handle *iph2;
1159	u_int64_t created;
1160	size_t i;
1161	caddr_t mhp[SADB_EXT_MAX + 1];
1162
1163	plog(LLV_DEBUG2, LOCATION, NULL,
1164		 "purge_ipsec_spi:\n");
1165	plog(LLV_DEBUG2, LOCATION, NULL, "dst0: %s\n", saddr2str(dst0));
1166	plog(LLV_DEBUG2, LOCATION, NULL, "SPI: %08X\n", ntohl(spi[0]));
1167
1168	buf = pfkey_dump_sadb(ipsecdoi2pfkey_proto(proto));
1169	if (buf == NULL) {
1170		plog(LLV_DEBUG, LOCATION, NULL,
1171			"pfkey_dump_sadb returned nothing.\n");
1172		return;
1173	}
1174
1175	msg = (struct sadb_msg *)buf->v;
1176	end = (struct sadb_msg *)(buf->v + buf->l);
1177
1178	while (msg < end) {
1179		if ((msg->sadb_msg_len << 3) < sizeof(*msg))
1180			break;
1181		next = (struct sadb_msg *)((caddr_t)msg + (msg->sadb_msg_len << 3));
1182		if (msg->sadb_msg_type != SADB_DUMP) {
1183			msg = next;
1184			continue;
1185		}
1186
1187		if (pfkey_align(msg, mhp) || pfkey_check(mhp)) {
1188			plog(LLV_ERROR, LOCATION, NULL,
1189				"pfkey_check (%s)\n", ipsec_strerror());
1190			msg = next;
1191			continue;
1192		}
1193
1194		sa = (struct sadb_sa *)(mhp[SADB_EXT_SA]);
1195		if (!sa
1196		 || !mhp[SADB_EXT_ADDRESS_SRC]
1197		 || !mhp[SADB_EXT_ADDRESS_DST]) {
1198			msg = next;
1199			continue;
1200		}
1201		src = PFKEY_ADDR_SADDR(mhp[SADB_EXT_ADDRESS_SRC]);
1202		dst = PFKEY_ADDR_SADDR(mhp[SADB_EXT_ADDRESS_DST]);
1203		lt = (struct sadb_lifetime*)mhp[SADB_EXT_LIFETIME_HARD];
1204		if(lt != NULL)
1205			created = lt->sadb_lifetime_addtime;
1206		else
1207			created = 0;
1208
1209		if (sa->sadb_sa_state != SADB_SASTATE_MATURE
1210		 && sa->sadb_sa_state != SADB_SASTATE_DYING) {
1211			msg = next;
1212			continue;
1213		}
1214		plog(LLV_DEBUG2, LOCATION, NULL, "src: %s\n", saddr2str(src));
1215		plog(LLV_DEBUG2, LOCATION, NULL, "dst: %s\n", saddr2str(dst));
1216
1217
1218
1219		/* XXX n^2 algorithm, inefficient */
1220
1221		/* don't delete inbound SAs at the moment */
1222		/* XXX should we remove SAs with opposite direction as well? */
1223		if (CMPSADDR(dst0, dst)) {
1224			msg = next;
1225			continue;
1226		}
1227
1228		for (i = 0; i < n; i++) {
1229			plog(LLV_DEBUG, LOCATION, NULL,
1230				"check spi(packet)=%u spi(db)=%u.\n",
1231				ntohl(spi[i]), ntohl(sa->sadb_sa_spi));
1232			if (spi[i] != sa->sadb_sa_spi)
1233				continue;
1234
1235			pfkey_send_delete(lcconf->sock_pfkey,
1236				msg->sadb_msg_satype,
1237				IPSEC_MODE_ANY,
1238				src, dst, sa->sadb_sa_spi);
1239
1240			/*
1241			 * delete a relative phase 2 handler.
1242			 * continue to process if no relative phase 2 handler
1243			 * exists.
1244			 */
1245			iph2 = getph2bysaidx(src, dst, proto, spi[i]);
1246			if(iph2 != NULL){
1247				delete_spd(iph2, created);
1248				unbindph12(iph2);
1249				remph2(iph2);
1250				delph2(iph2);
1251			}
1252
1253			plog(LLV_INFO, LOCATION, NULL,
1254				"purged IPsec-SA proto_id=%s spi=%u.\n",
1255				s_ipsecdoi_proto(proto),
1256				ntohl(spi[i]));
1257		}
1258
1259		msg = next;
1260	}
1261
1262	if (buf)
1263		vfree(buf);
1264}
1265
1266/*
1267 * delete all phase2 sa relatived to the destination address.
1268 * Don't delete Phase 1 handlers on INITIAL-CONTACT, and don't ignore
1269 * an INITIAL-CONTACT if we have contacted the peer.  This matches the
1270 * Sun IKE behavior, and makes rekeying work much better when the peer
1271 * restarts.
1272 */
1273static void
1274info_recv_initialcontact(iph1)
1275	struct ph1handle *iph1;
1276{
1277	vchar_t *buf = NULL;
1278	struct sadb_msg *msg, *next, *end;
1279	struct sadb_sa *sa;
1280	struct sockaddr *src, *dst;
1281	caddr_t mhp[SADB_EXT_MAX + 1];
1282	int proto_id, i;
1283	struct ph2handle *iph2;
1284#if 0
1285	char *loc, *rem;
1286#endif
1287
1288	if (f_local)
1289		return;
1290
1291#if 0
1292	loc = racoon_strdup(saddrwop2str(iph1->local));
1293	rem = racoon_strdup(saddrwop2str(iph1->remote));
1294	STRDUP_FATAL(loc);
1295	STRDUP_FATAL(rem);
1296
1297	/*
1298	 * Purge all IPSEC-SAs for the peer.  We can do this
1299	 * the easy way (using a PF_KEY SADB_DELETE extension)
1300	 * or we can do it the hard way.
1301	 */
1302	for (i = 0; i < pfkey_nsatypes; i++) {
1303		proto_id = pfkey2ipsecdoi_proto(pfkey_satypes[i].ps_satype);
1304
1305		plog(LLV_INFO, LOCATION, NULL,
1306		    "purging %s SAs for %s -> %s\n",
1307		    pfkey_satypes[i].ps_name, loc, rem);
1308		if (pfkey_send_delete_all(lcconf->sock_pfkey,
1309		    pfkey_satypes[i].ps_satype, IPSEC_MODE_ANY,
1310		    iph1->local, iph1->remote) == -1) {
1311			plog(LLV_ERROR, LOCATION, NULL,
1312			    "delete_all %s -> %s failed for %s (%s)\n",
1313			    loc, rem,
1314			    pfkey_satypes[i].ps_name, ipsec_strerror());
1315			goto the_hard_way;
1316		}
1317
1318		deleteallph2(iph1->local, iph1->remote, proto_id);
1319
1320		plog(LLV_INFO, LOCATION, NULL,
1321		    "purging %s SAs for %s -> %s\n",
1322		    pfkey_satypes[i].ps_name, rem, loc);
1323		if (pfkey_send_delete_all(lcconf->sock_pfkey,
1324		    pfkey_satypes[i].ps_satype, IPSEC_MODE_ANY,
1325		    iph1->remote, iph1->local) == -1) {
1326			plog(LLV_ERROR, LOCATION, NULL,
1327			    "delete_all %s -> %s failed for %s (%s)\n",
1328			    rem, loc,
1329			    pfkey_satypes[i].ps_name, ipsec_strerror());
1330			goto the_hard_way;
1331		}
1332
1333		deleteallph2(iph1->remote, iph1->local, proto_id);
1334	}
1335
1336	racoon_free(loc);
1337	racoon_free(rem);
1338	return;
1339
1340 the_hard_way:
1341	racoon_free(loc);
1342	racoon_free(rem);
1343#endif
1344
1345	buf = pfkey_dump_sadb(SADB_SATYPE_UNSPEC);
1346	if (buf == NULL) {
1347		plog(LLV_DEBUG, LOCATION, NULL,
1348			"pfkey_dump_sadb returned nothing.\n");
1349		return;
1350	}
1351
1352	msg = (struct sadb_msg *)buf->v;
1353	end = (struct sadb_msg *)(buf->v + buf->l);
1354
1355	while (msg < end) {
1356		if ((msg->sadb_msg_len << 3) < sizeof(*msg))
1357			break;
1358		next = (struct sadb_msg *)((caddr_t)msg + (msg->sadb_msg_len << 3));
1359		if (msg->sadb_msg_type != SADB_DUMP) {
1360			msg = next;
1361			continue;
1362		}
1363
1364		if (pfkey_align(msg, mhp) || pfkey_check(mhp)) {
1365			plog(LLV_ERROR, LOCATION, NULL,
1366				"pfkey_check (%s)\n", ipsec_strerror());
1367			msg = next;
1368			continue;
1369		}
1370
1371		if (mhp[SADB_EXT_SA] == NULL
1372		 || mhp[SADB_EXT_ADDRESS_SRC] == NULL
1373		 || mhp[SADB_EXT_ADDRESS_DST] == NULL) {
1374			msg = next;
1375			continue;
1376		}
1377		sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
1378		src = PFKEY_ADDR_SADDR(mhp[SADB_EXT_ADDRESS_SRC]);
1379		dst = PFKEY_ADDR_SADDR(mhp[SADB_EXT_ADDRESS_DST]);
1380
1381		if (sa->sadb_sa_state != SADB_SASTATE_MATURE
1382		 && sa->sadb_sa_state != SADB_SASTATE_DYING) {
1383			msg = next;
1384			continue;
1385		}
1386
1387		/*
1388		 * RFC2407 4.6.3.3 INITIAL-CONTACT is the message that
1389		 * announces the sender of the message was rebooted.
1390		 * it is interpreted to delete all SAs which source address
1391		 * is the sender of the message.
1392		 * racoon only deletes SA which is matched both the
1393		 * source address and the destination accress.
1394		 */
1395#ifdef ENABLE_NATT
1396		/*
1397		 * XXX RFC 3947 says that whe MUST NOT use IP+port to find old SAs
1398		 * from this peer !
1399		 */
1400		if(iph1->natt_flags & NAT_DETECTED){
1401			if (CMPSADDR(iph1->local, src) == 0 &&
1402				CMPSADDR(iph1->remote, dst) == 0)
1403				;
1404			else if (CMPSADDR(iph1->remote, src) == 0 &&
1405					 CMPSADDR(iph1->local, dst) == 0)
1406				;
1407			else {
1408				msg = next;
1409				continue;
1410			}
1411		} else
1412#endif
1413		/* If there is no NAT-T, we don't have to check addr + port...
1414		 * XXX what about a configuration with a remote peers which is not
1415		 * NATed, but which NATs some other peers ?
1416		 * Here, the INITIAl-CONTACT would also flush all those NATed peers !!
1417		 */
1418		if (cmpsaddrwop(iph1->local, src) == 0 &&
1419		    cmpsaddrwop(iph1->remote, dst) == 0)
1420			;
1421		else if (cmpsaddrwop(iph1->remote, src) == 0 &&
1422		    cmpsaddrwop(iph1->local, dst) == 0)
1423			;
1424		else {
1425			msg = next;
1426			continue;
1427		}
1428
1429		/*
1430		 * Make sure this is an SATYPE that we manage.
1431		 * This is gross; too bad we couldn't do it the
1432		 * easy way.
1433		 */
1434		for (i = 0; i < pfkey_nsatypes; i++) {
1435			if (pfkey_satypes[i].ps_satype ==
1436			    msg->sadb_msg_satype)
1437				break;
1438		}
1439		if (i == pfkey_nsatypes) {
1440			msg = next;
1441			continue;
1442		}
1443
1444		plog(LLV_INFO, LOCATION, NULL,
1445			"purging spi=%u.\n", ntohl(sa->sadb_sa_spi));
1446		pfkey_send_delete(lcconf->sock_pfkey,
1447			msg->sadb_msg_satype,
1448			IPSEC_MODE_ANY, src, dst, sa->sadb_sa_spi);
1449
1450		/*
1451		 * delete a relative phase 2 handler.
1452		 * continue to process if no relative phase 2 handler
1453		 * exists.
1454		 */
1455		proto_id = pfkey2ipsecdoi_proto(msg->sadb_msg_satype);
1456		iph2 = getph2bysaidx(src, dst, proto_id, sa->sadb_sa_spi);
1457		if (iph2) {
1458			delete_spd(iph2, 0);
1459			unbindph12(iph2);
1460			remph2(iph2);
1461			delph2(iph2);
1462		}
1463
1464		msg = next;
1465	}
1466
1467	vfree(buf);
1468}
1469
1470void
1471isakmp_check_notify(gen, iph1)
1472	struct isakmp_gen *gen;		/* points to Notify payload */
1473	struct ph1handle *iph1;
1474{
1475	struct isakmp_pl_n *notify = (struct isakmp_pl_n *)gen;
1476
1477	plog(LLV_DEBUG, LOCATION, iph1->remote,
1478		"Notify Message received\n");
1479
1480	switch (ntohs(notify->type)) {
1481	case ISAKMP_NTYPE_CONNECTED:
1482	case ISAKMP_NTYPE_RESPONDER_LIFETIME:
1483	case ISAKMP_NTYPE_REPLAY_STATUS:
1484	case ISAKMP_NTYPE_HEARTBEAT:
1485#ifdef ENABLE_HYBRID
1486	case ISAKMP_NTYPE_UNITY_HEARTBEAT:
1487#endif
1488		plog(LLV_WARNING, LOCATION, iph1->remote,
1489			"ignore %s notification.\n",
1490			s_isakmp_notify_msg(ntohs(notify->type)));
1491		break;
1492	case ISAKMP_NTYPE_INITIAL_CONTACT:
1493		plog(LLV_WARNING, LOCATION, iph1->remote,
1494			"ignore INITIAL-CONTACT notification, "
1495			"because it is only accepted after phase1.\n");
1496		break;
1497	default:
1498		isakmp_info_send_n1(iph1, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE, NULL);
1499		plog(LLV_ERROR, LOCATION, iph1->remote,
1500			"received unknown notification type %s.\n",
1501			s_isakmp_notify_msg(ntohs(notify->type)));
1502	}
1503
1504	return;
1505}
1506
1507
1508#ifdef ENABLE_DPD
1509static int
1510isakmp_info_recv_r_u (iph1, ru, msgid)
1511	struct ph1handle *iph1;
1512	struct isakmp_pl_ru *ru;
1513	u_int32_t msgid;
1514{
1515	struct isakmp_pl_ru *ru_ack;
1516	vchar_t *payload = NULL;
1517	int tlen;
1518	int error = 0;
1519
1520	plog(LLV_DEBUG, LOCATION, iph1->remote,
1521		 "DPD R-U-There received\n");
1522
1523	/* XXX should compare cookies with iph1->index?
1524	   Or is this already done by calling function?  */
1525	tlen = sizeof(*ru_ack);
1526	payload = vmalloc(tlen);
1527	if (payload == NULL) {
1528		plog(LLV_ERROR, LOCATION, NULL,
1529			"failed to get buffer to send.\n");
1530		return errno;
1531	}
1532
1533	ru_ack = (struct isakmp_pl_ru *)payload->v;
1534	ru_ack->h.np = ISAKMP_NPTYPE_NONE;
1535	ru_ack->h.len = htons(tlen);
1536	ru_ack->doi = htonl(IPSEC_DOI);
1537	ru_ack->type = htons(ISAKMP_NTYPE_R_U_THERE_ACK);
1538	ru_ack->proto_id = IPSECDOI_PROTO_ISAKMP; /* XXX ? */
1539	ru_ack->spi_size = sizeof(isakmp_index);
1540	memcpy(ru_ack->i_ck, ru->i_ck, sizeof(cookie_t));
1541	memcpy(ru_ack->r_ck, ru->r_ck, sizeof(cookie_t));
1542	ru_ack->data = ru->data;
1543
1544	/* XXX Should we do FLAG_A ?  */
1545	error = isakmp_info_send_common(iph1, payload, ISAKMP_NPTYPE_N,
1546					ISAKMP_FLAG_E);
1547	vfree(payload);
1548
1549	plog(LLV_DEBUG, LOCATION, NULL, "received a valid R-U-THERE, ACK sent\n");
1550
1551	/* Should we mark tunnel as active ? */
1552	return error;
1553}
1554
1555static int
1556isakmp_info_recv_r_u_ack (iph1, ru, msgid)
1557	struct ph1handle *iph1;
1558	struct isakmp_pl_ru *ru;
1559	u_int32_t msgid;
1560{
1561
1562	plog(LLV_DEBUG, LOCATION, iph1->remote,
1563		 "DPD R-U-There-Ack received\n");
1564
1565	/* XXX Maintain window of acceptable sequence numbers ?
1566	 * => ru->data <= iph2->dpd_seq &&
1567	 *    ru->data >= iph2->dpd_seq - iph2->dpd_fails ? */
1568	if (ntohl(ru->data) != iph1->dpd_seq-1) {
1569		plog(LLV_ERROR, LOCATION, iph1->remote,
1570			 "Wrong DPD sequence number (%d, %d expected).\n",
1571			 ntohl(ru->data), iph1->dpd_seq-1);
1572		return 0;
1573	}
1574
1575	if (memcmp(ru->i_ck, iph1->index.i_ck, sizeof(cookie_t)) ||
1576	    memcmp(ru->r_ck, iph1->index.r_ck, sizeof(cookie_t))) {
1577		plog(LLV_ERROR, LOCATION, iph1->remote,
1578			 "Cookie mismatch in DPD ACK!.\n");
1579		return 0;
1580	}
1581
1582	iph1->dpd_fails = 0;
1583
1584	/* Useless ??? */
1585	iph1->dpd_lastack = time(NULL);
1586
1587	SCHED_KILL(iph1->dpd_r_u);
1588
1589	isakmp_sched_r_u(iph1, 0);
1590
1591	plog(LLV_DEBUG, LOCATION, NULL, "received an R-U-THERE-ACK\n");
1592
1593	return 0;
1594}
1595
1596
1597
1598
1599/*
1600 * send DPD R-U-THERE payload in Informational exchange.
1601 */
1602static void
1603isakmp_info_send_r_u(arg)
1604	void *arg;
1605{
1606	struct ph1handle *iph1 = arg;
1607
1608	/* create R-U-THERE payload */
1609	struct isakmp_pl_ru *ru;
1610	vchar_t *payload = NULL;
1611	int tlen;
1612	int error = 0;
1613
1614	plog(LLV_DEBUG, LOCATION, iph1->remote, "DPD monitoring....\n");
1615
1616	if (iph1->dpd_fails >= iph1->rmconf->dpd_maxfails) {
1617		EVT_PUSH(iph1->local, iph1->remote, EVTT_DPD_TIMEOUT, NULL);
1618		purge_remote(iph1);
1619		plog(LLV_DEBUG, LOCATION, iph1->remote,
1620			 "DPD: remote seems to be dead\n");
1621
1622		/* Do not reschedule here: phase1 is deleted,
1623		 * DPD will be reactivated when a new ph1 will be negociated
1624		 */
1625		return;
1626	}
1627
1628	/* TODO: check recent activity to avoid useless sends... */
1629
1630	tlen = sizeof(*ru);
1631	payload = vmalloc(tlen);
1632	if (payload == NULL) {
1633		plog(LLV_ERROR, LOCATION, NULL,
1634			 "failed to get buffer for payload.\n");
1635		return;
1636	}
1637	ru = (struct isakmp_pl_ru *)payload->v;
1638	ru->h.np = ISAKMP_NPTYPE_NONE;
1639	ru->h.len = htons(tlen);
1640	ru->doi = htonl(IPSEC_DOI);
1641	ru->type = htons(ISAKMP_NTYPE_R_U_THERE);
1642	ru->proto_id = IPSECDOI_PROTO_ISAKMP; /* XXX ?*/
1643	ru->spi_size = sizeof(isakmp_index);
1644
1645	memcpy(ru->i_ck, iph1->index.i_ck, sizeof(cookie_t));
1646	memcpy(ru->r_ck, iph1->index.r_ck, sizeof(cookie_t));
1647
1648	if (iph1->dpd_seq == 0){
1649		/* generate a random seq which is not too big */
1650		srand(time(NULL));
1651		iph1->dpd_seq = rand() & 0x0fff;
1652	}
1653
1654	ru->data = htonl(iph1->dpd_seq);
1655
1656	error = isakmp_info_send_common(iph1, payload, ISAKMP_NPTYPE_N, 0);
1657	vfree(payload);
1658
1659	plog(LLV_DEBUG, LOCATION, iph1->remote,
1660		 "DPD R-U-There sent (%d)\n", error);
1661
1662	/* will be decreased if ACK received... */
1663	iph1->dpd_fails++;
1664
1665	/* XXX should be increased only when ACKed ? */
1666	iph1->dpd_seq++;
1667
1668	/* Reschedule the r_u_there with a short delay,
1669	 * will be deleted/rescheduled if ACK received before */
1670	isakmp_sched_r_u(iph1, 1);
1671
1672	plog(LLV_DEBUG, LOCATION, iph1->remote,
1673		 "rescheduling send_r_u (%d).\n", iph1->rmconf->dpd_retry);
1674}
1675
1676/* Schedule a new R-U-THERE */
1677int
1678isakmp_sched_r_u(iph1, retry)
1679	struct ph1handle *iph1;
1680	int retry;
1681{
1682	if(iph1 == NULL ||
1683	   iph1->rmconf == NULL)
1684		return 1;
1685
1686
1687	if(iph1->dpd_support == 0 ||
1688	   iph1->rmconf->dpd_interval == 0)
1689		return 0;
1690
1691	if(retry)
1692		iph1->dpd_r_u = sched_new(iph1->rmconf->dpd_retry,
1693								  isakmp_info_send_r_u, iph1);
1694	else
1695		iph1->dpd_r_u = sched_new(iph1->rmconf->dpd_interval,
1696								  isakmp_info_send_r_u, iph1);
1697
1698	return 0;
1699}
1700#endif
1701