summaryrefslogtreecommitdiffstats
path: root/ipsec-tools/src/racoon/isakmp_frag.c
blob: 3ae464a58f533163a255d1f5151d656fa1c3812d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <machine/rtems-bsd-user-space.h>
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#include "rtems-bsd-racoon-namespace.h"
#endif /* __rtems__ */

/*	$NetBSD: isakmp_frag.c,v 1.7 2017/07/23 05:40:27 christos Exp $	*/

/* Id: isakmp_frag.c,v 1.4 2004/11/13 17:31:36 manubsd Exp */

/*
 * Copyright (C) 2004 Emmanuel Dreyfus
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "config.h"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/queue.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <openssl/md5.h> 

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#include <netdb.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <ctype.h>

#include "var.h"
#include "misc.h"
#include "vmbuf.h"
#include "plog.h"
#include "sockmisc.h"
#include "schedule.h"
#include "debug.h"

#include "isakmp_var.h"
#include "isakmp.h"
#include "handler.h"
#include "isakmp_frag.h"
#include "strnames.h"

int
isakmp_sendfrags(iph1, buf) 
	struct ph1handle *iph1;
	vchar_t *buf;
{
	struct isakmp *hdr;
	struct isakmp_frag *fraghdr;
	caddr_t data;
	caddr_t sdata;
	size_t datalen;
	size_t max_datalen;
	size_t fraglen;
	vchar_t *frag;
	unsigned int trailer;
	unsigned int fragnum = 0;
	size_t len;
	int etype;

	/*
	 * Catch the exchange type for later: the fragments and the
	 * fragmented packet must have the same exchange type.
	 */
	hdr = (struct isakmp *)buf->v;
	etype = hdr->etype;

	/*
	 * We want to send a a packet smaller than ISAKMP_FRAG_MAXLEN
	 * First compute the maximum data length that will fit in it
	 */
	max_datalen = ISAKMP_FRAG_MAXLEN - 
	    (sizeof(*hdr) + sizeof(*fraghdr) + sizeof(trailer));

	sdata = buf->v;
	len = buf->l;

	while (len > 0) {
		fragnum++;

		if (len > max_datalen)
			datalen = max_datalen;
		else
			datalen = len;

		fraglen = sizeof(*hdr) 
			+ sizeof(*fraghdr) 
			+ datalen;

		if ((frag = vmalloc(fraglen)) == NULL) {
			plog(LLV_ERROR, LOCATION, NULL, 
			    "Cannot allocate memory\n");
			return -1;
		}

		set_isakmp_header1(frag, iph1, ISAKMP_NPTYPE_FRAG);
		hdr = (struct isakmp *)frag->v;
		hdr->etype = etype;

		fraghdr = (struct isakmp_frag *)(hdr + 1);
		fraghdr->unknown0 = htons(0);
		fraghdr->len = htons(fraglen - sizeof(*hdr));
		fraghdr->unknown1 = htons(1);
		fraghdr->index = fragnum;
		if (len == datalen)
			fraghdr->flags = ISAKMP_FRAG_LAST;
		else
			fraghdr->flags = 0;

		data = (caddr_t)(fraghdr + 1);
		memcpy(data, sdata, datalen);

		if (isakmp_send(iph1, frag) < 0) {
			plog(LLV_ERROR, LOCATION, NULL, "isakmp_send failed\n");
			return -1;
		}

		vfree(frag);

		len -= datalen;
		sdata += datalen;
	}
		
	return fragnum;
}

unsigned int 
vendorid_frag_cap(gen)
	struct isakmp_gen *gen;
{
	int *hp;

	hp = (int *)(gen + 1);

	return ntohl(hp[MD5_DIGEST_LENGTH / sizeof(*hp)]);
}

static int 
isakmp_frag_insert(struct ph1handle *iph1, struct isakmp_frag_item *item)
{
	struct isakmp_frag_item *pitem = NULL;
	struct isakmp_frag_item *citem = iph1->frag_chain;

	/* no frag yet, just insert at beginning of list */
	if (iph1->frag_chain == NULL) {
		iph1->frag_chain = item;
		return 0;
	}

	do {
		/* duplicate fragment number, abort (CVE-2016-10396) */
		if (citem->frag_num == item->frag_num)
			return -1;

		/* need to insert before current item */
		if (citem->frag_num > item->frag_num) {
			if (pitem != NULL)
				pitem->frag_next = item;
			else
				/* insert at the beginning of the list  */
				iph1->frag_chain = item;
			item->frag_next = citem;
			return 0;
		}

		pitem = citem;
		citem = citem->frag_next;
	} while (citem != NULL);

	/* we reached the end of the list, insert */
	pitem->frag_next = item;
	return 0;
}

int 
isakmp_frag_extract(iph1, msg)
	struct ph1handle *iph1;
	vchar_t *msg;
{
	struct isakmp *isakmp;
	struct isakmp_frag *frag;
	struct isakmp_frag_item *item;
	vchar_t *buf;
	size_t len;
	int last_frag = 0;
	char *data;
	int i;

	if (msg->l < sizeof(*isakmp) + sizeof(*frag)) {
		plog(LLV_ERROR, LOCATION, NULL, "Message too short\n");
		return -1;
	}

	isakmp = (struct isakmp *)msg->v;
	frag = (struct isakmp_frag *)(isakmp + 1);

	/* 
	 * frag->len is the frag payload data plus the frag payload header,
	 * whose size is sizeof(*frag) 
	 */
	if (msg->l < sizeof(*isakmp) + ntohs(frag->len) ||
	    ntohs(frag->len) < sizeof(*frag) + 1) {
		plog(LLV_ERROR, LOCATION, NULL, "Fragment too short\n");
		return -1;
	}

	if ((buf = vmalloc(ntohs(frag->len) - sizeof(*frag))) == NULL) {
		plog(LLV_ERROR, LOCATION, NULL, "Cannot allocate memory\n");
		return -1;
	}

	if ((item = racoon_malloc(sizeof(*item))) == NULL) {
		plog(LLV_ERROR, LOCATION, NULL, "Cannot allocate memory\n");
		vfree(buf);
		return -1;
	}

	data = (char *)(frag + 1);
	memcpy(buf->v, data, buf->l);

	item->frag_num = frag->index;
	item->frag_last = (frag->flags & ISAKMP_FRAG_LAST);
	item->frag_next = NULL;
	item->frag_packet = buf;

	/* Check for the last frag before inserting the new item in the chain */
	if (item->frag_last) {
		/* if we have the last fragment, indices must match */
		if (iph1->frag_last_index != 0 &&
		    item->frag_last != iph1->frag_last_index) {
			plog(LLV_ERROR, LOCATION, NULL,
			     "Repeated last fragment index mismatch\n");
			racoon_free(item);
			vfree(buf);
			return -1;
		}

		last_frag = iph1->frag_last_index = item->frag_num;
	}

	/* insert fragment into chain */
	if (isakmp_frag_insert(iph1, item) == -1) {
		plog(LLV_ERROR, LOCATION, NULL,
		    "Repeated fragment index mismatch\n");
		racoon_free(item);
		vfree(buf);
		return -1;
	}

	/* If we saw the last frag, check if the chain is complete
	 * we have a sorted list now, so just walk through */
	if (last_frag != 0) {
		item = iph1->frag_chain;
		for (i = 1; i <= last_frag; i++) {
			if (item->frag_num != i)
				break;
			item = item->frag_next;
			if (item == NULL) /* Not found */
				break;
		}

		if (i > last_frag) /* It is complete */
			return 1;
	}
		
	return 0;
}

vchar_t *
isakmp_frag_reassembly(iph1)
	struct ph1handle *iph1;
{
	struct isakmp_frag_item *item;
	size_t len = 0;
	vchar_t *buf = NULL;
	int frag_count = 0;
	int i;
	char *data;

	if ((item = iph1->frag_chain) == NULL) {
		plog(LLV_ERROR, LOCATION, NULL, "No fragment to reassemble\n");
		goto out;
	}

	do {
		frag_count++;
		len += item->frag_packet->l;
		item = item->frag_next;
	} while (item != NULL);
	
	if ((buf = vmalloc(len)) == NULL) {
		plog(LLV_ERROR, LOCATION, NULL, "Cannot allocate memory\n");
		goto out;
	}
	data = buf->v;

	item = iph1->frag_chain;
	for (i = 1; i <= frag_count; i++) {
		if (item->frag_num != i) {
			plog(LLV_ERROR, LOCATION, NULL, 
			    "Missing fragment #%d\n", i);
			vfree(buf);
			buf = NULL;
			goto out;
		}
		memcpy(data, item->frag_packet->v, item->frag_packet->l);
		data += item->frag_packet->l;
		item = item->frag_next;
	}

out:
	item = iph1->frag_chain;		
	do {
		struct isakmp_frag_item *next_item;

		next_item = item->frag_next;

		vfree(item->frag_packet);
		racoon_free(item);

		item = next_item;
	} while (item != NULL);

	iph1->frag_chain = NULL;

	return buf;
}

vchar_t *
isakmp_frag_addcap(buf, cap)
	vchar_t *buf;
	int cap;
{
	int *capp;
	size_t len;

	/* If the capability has not been added, add room now */
	len = buf->l;
	if (len == MD5_DIGEST_LENGTH) {
		if ((buf = vrealloc(buf, len + sizeof(cap))) == NULL) {
			plog(LLV_ERROR, LOCATION, NULL, 
			    "Cannot allocate memory\n");
			return NULL;
		}
		capp = (int *)(buf->v + len);
		*capp = htonl(0);
	}

	capp = (int *)(buf->v + MD5_DIGEST_LENGTH);
	*capp |= htonl(cap);

	return buf;
}

#ifdef __rtems__
#include "rtems-bsd-racoon-isakmp_frag-data.h"
#endif /* __rtems__ */