-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfrag.c
476 lines (380 loc) · 10 KB
/
ipfrag.c
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/param.h>
#include <sys/types.h>
#include "config.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/stat.h>
#include <sys/tree.h>
#include <sys/wait.h>
#include <sys/queue.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <dumbnet.h>
#include <ctype.h>
#undef timeout_pending
#undef timeout_initialized
#include <event.h>
#include "honeyd.h"
#include "template.h"
#include "personality.h"
#include "ipfrag.h"
#include "pool.h"
extern struct pool *pool_pkt;
static u_char buf[IP_LEN_MAX]; /* for complete packet */
SPLAY_HEAD(fragtree, fragment) fragments;
#define DIFF(a,b) do { \
if ((a) < (b)) return -1; \
if ((a) > (b)) return 1; \
} while (0)
int
fragcompare(struct fragment *a, struct fragment *b)
{
DIFF(a->ip_src, b->ip_src);
DIFF(a->ip_dst, b->ip_dst);
DIFF(a->ip_id, b->ip_id);
DIFF(a->ip_proto, b->ip_proto);
return (0);
}
SPLAY_PROTOTYPE(fragtree, fragment, node, fragcompare);
SPLAY_GENERATE(fragtree, fragment, node, fragcompare);
TAILQ_HEAD(fragqueue, fragment) fraglru;
int nfragments;
int nfragmem;
void
ip_fragment_init(void)
{
SPLAY_INIT(&fragments);
TAILQ_INIT(&fraglru);
nfragments = 0;
nfragmem = 0;
}
struct fragment *
ip_fragment_find(ip_addr_t src, ip_addr_t dst, u_short id, u_char proto)
{
struct fragment tmp, *frag;
tmp.ip_src = src;
tmp.ip_dst = dst;
tmp.ip_id = id;
tmp.ip_proto = proto;
frag = SPLAY_FIND(fragtree, &fragments, &tmp);
if (frag != NULL) {
TAILQ_REMOVE(&fraglru, frag, next);
TAILQ_INSERT_HEAD(&fraglru, frag, next);
}
return (frag);
}
/* Free a fragment by removing it from all lists, etc... */
void
ip_fragent_free(struct fragent *ent)
{
nfragmem -= ent->size;
free(ent->data);
free(ent);
}
void
ip_fragment_free(struct fragment *tmp)
{
struct fragent *ent;
evtimer_del(&tmp->timeout);
SPLAY_REMOVE(fragtree, &fragments, tmp);
TAILQ_REMOVE(&fraglru, tmp, next);
nfragments--;
for (ent = TAILQ_FIRST(&tmp->fraglist); ent;
ent = TAILQ_FIRST(&tmp->fraglist)) {
TAILQ_REMOVE(&tmp->fraglist, ent, next);
ip_fragent_free(ent);
}
free(tmp);
}
void
ip_fragment_timeout(int fd, short which, void *arg)
{
struct fragment *tmp = arg;
struct addr src;
addr_pack(&src, ADDR_TYPE_IP, IP_ADDR_BITS, &tmp->ip_src, IP_ADDR_LEN);
syslog(LOG_DEBUG, "Expiring fragment from %s, id %d",
addr_ntoa(&src), ntohs(tmp->ip_id));
ip_fragment_free(tmp);
}
void
ip_fragment_reclaim(int count)
{
struct fragment *tmp;
for (tmp = TAILQ_LAST(&fraglru, fragqueue); tmp && count;
tmp = TAILQ_LAST(&fraglru, fragqueue)) {
ip_fragment_free(tmp);
count--;
}
}
struct fragment *
ip_fragment_new(ip_addr_t src, ip_addr_t dst, u_short id, u_char proto,
enum fragpolicy pl)
{
struct fragment *tmp = NULL;
struct timeval tv = { IPFRAG_TIMEOUT, 0};
int reclaim = 0;
if (nfragmem > IPFRAG_MAX_MEM || nfragments > IPFRAG_MAX_FRAGS)
ip_fragment_reclaim(nfragments/10);
while (tmp == NULL && reclaim < 2) {
tmp = calloc(1, sizeof(struct fragment));
if (tmp == NULL) {
reclaim++;
ip_fragment_reclaim(nfragments/10);
}
}
if (tmp == NULL)
return (NULL);
tmp->ip_src = src;
tmp->ip_dst = dst;
tmp->ip_id = id;
tmp->ip_proto = proto;
tmp->fragp = pl;
TAILQ_INIT(&tmp->fraglist);
evtimer_set(&tmp->timeout, ip_fragment_timeout, tmp);
evtimer_add(&tmp->timeout, &tv);
SPLAY_INSERT(fragtree, &fragments, tmp);
TAILQ_INSERT_HEAD(&fraglru, tmp, next);
nfragments++;
return (tmp);
}
int
ip_fragment_insert(struct fragment *fragq, struct fragent *ent, short mf)
{
struct fragent *prev, *after;
struct ip_hdr *ip;
u_char *data;
u_short overlap;
u_short max;
u_short off;
u_short len;
off = ent->off;
len = ent->len;
max = off + len;
if (fragq->maxlen < max)
fragq->maxlen = max;
if (!mf)
fragq->hadlastpacket = 1;
prev = NULL;
for (after = TAILQ_FIRST(&fragq->fraglist); after;
after = TAILQ_NEXT(after, next)) {
if (off < after->off)
break;
prev = after;
}
if (prev && prev->off + prev->len > off) {
overlap = prev->off + prev->len - off;
if (overlap >= len) {
if (fragq->fragp == FRAG_NEW) {
u_char *odata = prev->data + off - prev->off;
memcpy(odata, ent->data, len);
}
goto free_fragment;
}
if (fragq->fragp == FRAG_OLD) {
u_char *odata = prev->data + prev->len - overlap;
memcpy(ent->data, odata, overlap);
}
prev->len -= overlap;
}
if (after && off + len > after->off) {
overlap = off + len - after->off;
if (overlap >= after->len) {
if (fragq->fragp == FRAG_OLD) {
u_char *ndata = ent->data + after->off - off;
memcpy(ndata, after->data, after->len);
}
/* Drop the old fragment */
TAILQ_REMOVE(&fragq->fraglist, after, next);
ip_fragent_free(after);
} else {
/* Trim the overlap */
if (fragq->fragp == FRAG_NEW) {
u_char *ndata = ent->data + len - overlap;
memcpy(after->data, ndata, overlap);
}
len -= overlap;
ent->len = len;
}
}
if (prev)
TAILQ_INSERT_AFTER(&fragq->fraglist, prev, ent, next);
else
TAILQ_INSERT_HEAD(&fragq->fraglist, ent, next);
/* Waiting for more data */
if (!fragq->hadlastpacket)
return (0);
off = 0;
TAILQ_FOREACH(ent, &fragq->fraglist, next) {
if (ent->off != off)
break;
off = ent->off + ent->len;
}
if (ent)
return (0);
/* Completely assembled */
data = buf;
ip = (struct ip_hdr *)data;
for(ent = TAILQ_FIRST(&fragq->fraglist); ent;
ent = TAILQ_FIRST(&fragq->fraglist)) {
TAILQ_REMOVE(&fragq->fraglist, ent, next);
memcpy(data, ent->data, ent->len);
data += ent->len;
ip_fragent_free(ent);
}
ip->ip_len = htons(fragq->maxlen);
ip->ip_off = 0;
ip_fragment_free(fragq);
return (1);
free_fragment:
ip_fragent_free(ent);
return (0);
}
/*
* Reassembles fragmented IP packets.
*
* Return:
* 0 - successfully reassembled
* -1 - not reassembled yet.
*/
int
ip_fragment(struct template *tmpl, struct ip_hdr *ip, u_short len,
struct ip_hdr **pip, u_short *piplen)
{
struct addr src;
struct personality *person = NULL;
struct fragment *fragq;
struct fragent *ent;
u_char *dat;
short mf;
u_short off;
u_short hlen;
enum fragpolicy fragp = FRAG_OLD;
addr_pack(&src, ADDR_TYPE_IP, IP_ADDR_BITS, &ip->ip_src, IP_ADDR_LEN);
if (tmpl != NULL && (person = tmpl->person) != NULL)
fragp = person->fragp;
if (fragp == FRAG_DROP)
goto drop;
fragq = ip_fragment_find(ip->ip_src, ip->ip_dst, ip->ip_id, ip->ip_p);
/* Nothing here for now */
off = ntohs(ip->ip_off);
if (off & IP_DF)
goto freeall;
mf = off & IP_MF;
off &= IP_OFFMASK;
off <<= 3;
dat = (u_char *)ip;
hlen = ip->ip_hl << 2;
if (mf && ((len - hlen) & 0x7))
goto freeall;
if (off) {
len -= hlen;
dat += hlen;
off += hlen;
}
if (off + len > IP_LEN_MAX || len == 0)
goto freeall;
if (fragq == NULL) {
fragq = ip_fragment_new(ip->ip_src, ip->ip_dst, ip->ip_id,
ip->ip_p, fragp);
if (fragq == NULL)
goto drop;
}
if ((ent = calloc(1, sizeof(struct fragent))) == NULL)
goto freeall;
ent->off = off;
ent->len = len;
ent->size = len;
if ((ent->data = malloc(len)) == NULL) {
free(ent);
goto freeall;
}
memcpy(ent->data, dat, len);
nfragmem += len;
syslog(LOG_DEBUG, "Received fragment from %s, id %d: %d@%d",
addr_ntoa(&src), ntohs(ip->ip_id), len, off);
if (ip_fragment_insert(fragq, ent, mf)) {
ip = (struct ip_hdr *)buf;
len = ntohs(ip->ip_len);
*pip = ip;
*piplen = len;
/* Successfully reassembled */
return (0);
}
return (-1);
freeall:
syslog(LOG_DEBUG, "%s fragment from %s, id %d: %d@%d",
fragq ? "Freeing" : "Dropping",
addr_ntoa(&src), ntohs(ip->ip_id), len, off);
if (fragq)
ip_fragment_free(fragq);
return (-1);
drop:
syslog(LOG_DEBUG, "Dropping fragment from %s", addr_ntoa(&src));
return (-1);
}
void
ip_send_fragments(u_int mtu, struct ip_hdr *ip, u_int iplen, struct spoof spoof)
{
struct ip_hdr *nip;
u_int iphlen, datlen, offset, pdatlen, size;
u_char *p;
/* Need to calculate the checksum for the protocol */
ip_checksum(ip, iplen);
iphlen = ip->ip_hl << 2;
datlen = iplen - iphlen;
pdatlen = (mtu - iphlen) & (~0x7);
offset = 0;
p = (u_char *)ip + iphlen;
while (datlen) {
nip = pool_alloc(pool_pkt);
size = datlen > pdatlen ? pdatlen : datlen;
memcpy(nip, ip, iphlen);
memcpy((u_char *)nip + iphlen, p, size);
p += size;
datlen -= size;
nip->ip_len = htons(iphlen + size);
nip->ip_off = htons((offset >> 3) | (datlen ? IP_MF : 0));
/* Send the packet:
* takes also care of deallocating this packet.
*/
honeyd_ip_send((u_char *)nip, iphlen + size, spoof);
offset += size;
}
}