-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsr-rreq.c
728 lines (558 loc) · 14.9 KB
/
dsr-rreq.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* Copyright (C) Uppsala University
*
* This file is distributed under the terms of the GNU general Public
* License (GPL), see the file LICENSE
*
* Author: Erik Nordström, <[email protected]>
*/
#ifdef __KERNEL__
#include <linux/proc_fs.h>
#include <linux/timer.h>
#include <net/ip.h>
#include <linux/random.h>
#include "dsr-dev.h"
#endif
#ifdef NS2
#include "ns-agent.h"
#endif
#include "debug.h"
#include "dsr.h"
#include "tbl.h"
#include "dsr-rrep.h"
#include "dsr-rreq.h"
#include "dsr-opt.h"
#include "link-cache.h"
#include "send-buf.h"
#include "neigh.h"
#ifndef NS2
#define RREQ_TBL_PROC_NAME "dsr_rreq_tbl"
static TBL(rreq_tbl, RREQ_TBL_MAX_LEN);
static unsigned int rreq_seqno;
#endif
#ifndef MAXTTL
#define MAXTTL 255
#endif
#define STATE_IDLE 0
#define STATE_IN_ROUTE_DISC 1
struct rreq_tbl_entry {
list_t l;
int state;
struct in_addr node_addr;
int ttl;
atomic_t refcnt;
DSRUUTimer *timer;
struct timeval tx_time;
struct timeval last_used;
usecs_t timeout;
unsigned int num_rexmts;
struct tbl rreq_id_tbl;
};
struct id_entry {
list_t l;
atomic_t refcnt;
struct in_addr trg_addr;
unsigned short id;
};
struct rreq_tbl_query {
struct in_addr *initiator;
struct in_addr *target;
unsigned int *id;
};
static inline int crit_addr(void *pos, void *data)
{
struct rreq_tbl_entry *e = (struct rreq_tbl_entry *)pos;
struct in_addr *a = (struct in_addr *)data;
if (e->node_addr.s_addr == a->s_addr)
return 1;
return 0;
}
static inline int crit_duplicate(void *pos, void *data)
{
struct rreq_tbl_entry *e = (struct rreq_tbl_entry *)pos;
struct rreq_tbl_query *q = (struct rreq_tbl_query *)data;
if (e->node_addr.s_addr == q->initiator->s_addr) {
list_t *p;
list_for_each(p, &e->rreq_id_tbl.head) {
struct id_entry *id_e = (struct id_entry *)p;
if (id_e->trg_addr.s_addr == q->target->s_addr &&
id_e->id == *(q->id))
return 1;
}
}
return 0;
}
void NSCLASS rreq_tbl_set_max_len(unsigned int max_len)
{
rreq_tbl.max_len = max_len;
}
#ifdef __KERNEL__
static int rreq_tbl_print(struct tbl *t, char *buf)
{
list_t *pos1, *pos2;
int len = 0;
int first = 1;
struct timeval now;
gettime(&now);
read_lock_bh(&t->lock);
len +=
sprintf(buf, "# %-15s %-6s %-8s %15s:%s\n", "IPAddr", "TTL", "Used",
"TargetIPAddr", "ID");
list_for_each(pos1, &t->head) {
struct rreq_tbl_entry *e = (struct rreq_tbl_entry *)pos1;
struct id_entry *id_e;
if (TBL_EMPTY(&e->rreq_id_tbl))
len +=
sprintf(buf + len, " %-15s %-6u %-8lu %15s:%s\n",
print_ip(e->node_addr), e->ttl,
timeval_diff(&now, &e->last_used) / 1000000,
"-", "-");
else {
id_e = (struct id_entry *)TBL_FIRST(&e->rreq_id_tbl);
len +=
sprintf(buf + len, " %-15s %-6u %-8lu %15s:%u\n",
print_ip(e->node_addr), e->ttl,
timeval_diff(&now, &e->last_used) / 1000000,
print_ip(id_e->trg_addr), id_e->id);
}
list_for_each(pos2, &e->rreq_id_tbl.head) {
id_e = (struct id_entry *)pos2;
if (!first)
len +=
sprintf(buf + len, "%49s:%u\n",
print_ip(id_e->trg_addr), id_e->id);
first = 0;
}
}
read_unlock_bh(&t->lock);
return len;
}
#endif /* __KERNEL__ */
void NSCLASS rreq_tbl_timeout(unsigned long data)
{
struct rreq_tbl_entry *e = (struct rreq_tbl_entry *)data;
struct timeval expires;
if (!e)
return;
tbl_detach(&rreq_tbl, &e->l);
LOG_DBG("RREQ Timeout dst=%s timeout=%lu rexmts=%d \n",
print_ip(e->node_addr), e->timeout, e->num_rexmts);
if (e->num_rexmts >= ConfVal(MaxRequestRexmt)) {
LOG_DBG("MAX RREQs reached for %s\n", print_ip(e->node_addr));
e->state = STATE_IDLE;
tbl_add_tail(&rreq_tbl, &e->l);
return;
}
e->num_rexmts++;
/* if (e->ttl == 1) */
/* e->timeout = ConfValToUsecs(RequestPeriod); */
/* else */
e->timeout *= 2; /* Double timeout */
e->ttl *= 2; /* Double TTL */
if (e->ttl > MAXTTL)
e->ttl = MAXTTL;
if (e->timeout > ConfValToUsecs(MaxRequestPeriod))
e->timeout = ConfValToUsecs(MaxRequestPeriod);
gettime(&e->last_used);
dsr_rreq_send(e->node_addr, e->ttl);
expires = e->last_used;
timeval_add_usecs(&expires, e->timeout);
/* Put at end of list */
tbl_add_tail(&rreq_tbl, &e->l);
set_timer(e->timer, &expires);
}
struct rreq_tbl_entry *NSCLASS __rreq_tbl_entry_create(struct in_addr node_addr)
{
struct rreq_tbl_entry *e;
e = (struct rreq_tbl_entry *)kmalloc(sizeof(struct rreq_tbl_entry),
GFP_ATOMIC);
if (!e)
return NULL;
e->state = STATE_IDLE;
e->node_addr = node_addr;
e->ttl = 0;
atomic_set(&e->refcnt, 1);
memset(&e->tx_time, 0, sizeof(struct timeval));;
e->num_rexmts = 0;
#ifdef NS2
e->timer = new DSRUUTimer(this, "RREQTblTimer");
#else
e->timer = kmalloc(sizeof(DSRUUTimer), GFP_ATOMIC);
#endif
if (!e->timer) {
kfree(e);
return NULL;
}
init_timer(e->timer);
e->timer->function = &NSCLASS rreq_tbl_timeout;
e->timer->data = (unsigned long)e;
INIT_TBL(&e->rreq_id_tbl, ConfVal(RequestTableIds));
return e;
}
struct rreq_tbl_entry *NSCLASS __rreq_tbl_add(struct in_addr node_addr)
{
struct rreq_tbl_entry *e;
e = __rreq_tbl_entry_create(node_addr);
if (!e)
return NULL;
if (TBL_FULL(&rreq_tbl)) {
struct rreq_tbl_entry *f;
f = (struct rreq_tbl_entry *)TBL_FIRST(&rreq_tbl);
__tbl_detach(&rreq_tbl, &f->l);
del_timer_sync(f->timer);
#ifdef NS2
delete f->timer;
#else
kfree(f->timer);
#endif
tbl_flush(&f->rreq_id_tbl, NULL);
kfree(f);
}
__tbl_add_tail(&rreq_tbl, &e->l);
return e;
}
int NSCLASS
rreq_tbl_add_id(struct in_addr initiator, struct in_addr target,
unsigned short id)
{
struct rreq_tbl_entry *e;
struct id_entry *id_e;
int res = 0;
write_lock_bh(&rreq_tbl.lock);
e = (struct rreq_tbl_entry *)__tbl_find(&rreq_tbl, &initiator,
crit_addr);
if (!e)
e = __rreq_tbl_add(initiator);
else {
/* Put it last in the table */
__tbl_detach(&rreq_tbl, &e->l);
__tbl_add_tail(&rreq_tbl, &e->l);
}
if (!e) {
res = -ENOMEM;
goto out;
}
gettime(&e->last_used);
if (TBL_FULL(&e->rreq_id_tbl))
tbl_del_first(&e->rreq_id_tbl);
id_e = (struct id_entry *)kmalloc(sizeof(struct id_entry), GFP_ATOMIC);
if (!id_e) {
res = -ENOMEM;
goto out;
}
id_e->trg_addr = target;
id_e->id = id;
tbl_add_tail(&e->rreq_id_tbl, &id_e->l);
out:
write_unlock_bh(&rreq_tbl.lock);
return 1;
}
int NSCLASS rreq_tbl_route_discovery_cancel(struct in_addr dst)
{
struct rreq_tbl_entry *e;
e = (struct rreq_tbl_entry *)tbl_find_detach(&rreq_tbl, &dst,
crit_addr);
if (!e) {
LOG_DBG("%s not in RREQ table\n", print_ip(dst));
return -1;
}
if (e->state == STATE_IN_ROUTE_DISC)
del_timer_sync(e->timer);
e->state = STATE_IDLE;
gettime(&e->last_used);
tbl_add_tail(&rreq_tbl, &e->l);
return 1;
}
int NSCLASS dsr_rreq_route_discovery(struct in_addr target)
{
struct rreq_tbl_entry *e;
int ttl, res = 0;
struct timeval expires;
#define TTL_START 1
write_lock_bh(&rreq_tbl.lock);
e = (struct rreq_tbl_entry *)__tbl_find(&rreq_tbl, &target, crit_addr);
if (!e)
e = __rreq_tbl_add(target);
else {
/* Put it last in the table */
__tbl_detach(&rreq_tbl, &e->l);
__tbl_add_tail(&rreq_tbl, &e->l);
}
if (!e) {
res = -ENOMEM;
goto out;
}
if (e->state == STATE_IN_ROUTE_DISC) {
LOG_DBG("Route discovery for %s already in progress\n",
print_ip(target));
goto out;
}
LOG_DBG("Route discovery for %s\n", print_ip(target));
gettime(&e->last_used);
e->ttl = ttl = TTL_START;
/* The draft does not actually specify how these Request Timeout values
* should be used... ??? I am just guessing here. */
if (e->ttl == 1)
e->timeout = ConfValToUsecs(NonpropRequestTimeout);
else
e->timeout = ConfValToUsecs(RequestPeriod);
e->state = STATE_IN_ROUTE_DISC;
e->num_rexmts = 0;
expires = e->last_used;
timeval_add_usecs(&expires, e->timeout);
set_timer(e->timer, &expires);
write_unlock_bh(&rreq_tbl.lock);
dsr_rreq_send(target, ttl);
return 1;
out:
write_unlock_bh(&rreq_tbl.lock);
return res;
}
int NSCLASS dsr_rreq_duplicate(struct in_addr initiator, struct in_addr target,
unsigned int id)
{
struct {
struct in_addr *initiator;
struct in_addr *target;
unsigned int *id;
} d;
d.initiator = &initiator;
d.target = ⌖
d.id = &id;
return in_tbl(&rreq_tbl, &d, crit_duplicate);
}
static struct dsr_rreq_opt *dsr_rreq_opt_add(char *buf, unsigned int len,
struct in_addr target,
unsigned int seqno)
{
struct dsr_rreq_opt *rreq_opt;
if (!buf || len < DSR_RREQ_HDR_LEN)
return NULL;
rreq_opt = (struct dsr_rreq_opt *)buf;
rreq_opt->type = DSR_OPT_RREQ;
rreq_opt->length = 6;
rreq_opt->id = htons(seqno);
rreq_opt->target = target.s_addr;
return rreq_opt;
}
int NSCLASS dsr_rreq_send(struct in_addr target, int ttl)
{
struct dsr_pkt *dp;
char *buf;
int len = DSR_OPT_HDR_LEN + DSR_RREQ_HDR_LEN;
dp = dsr_pkt_alloc(NULL);
if (!dp) {
LOG_DBG("Could not allocate DSR packet\n");
return -1;
}
dp->dst.s_addr = DSR_BROADCAST;
dp->nxt_hop.s_addr = DSR_BROADCAST;
dp->src = my_addr();
buf = dsr_pkt_alloc_opts(dp, len);
if (!buf)
goto out_err;
dp->nh.iph =
dsr_build_ip(dp, dp->src, dp->dst, IP_HDR_LEN, IP_HDR_LEN + len,
IPPROTO_DSR, ttl);
if (!dp->nh.iph)
goto out_err;
dp->dh.opth = dsr_opt_hdr_add(buf, len, DSR_NO_NEXT_HDR_TYPE);
if (!dp->dh.opth) {
LOG_DBG("Could not create DSR opt header\n");
goto out_err;
}
buf += DSR_OPT_HDR_LEN;
len -= DSR_OPT_HDR_LEN;
dp->rreq_opt = dsr_rreq_opt_add(buf, len, target, ++rreq_seqno);
if (!dp->rreq_opt) {
LOG_DBG("Could not create RREQ opt\n");
goto out_err;
}
#ifdef NS2
LOG_DBG("Sending RREQ src=%s dst=%s target=%s ttl=%d iph->saddr()=%d\n",
print_ip(dp->src), print_ip(dp->dst), print_ip(target), ttl,
dp->nh.iph->saddr());
#endif
dp->flags |= PKT_XMIT_JITTER;
XMIT(dp);
return 0;
out_err:
dsr_pkt_free(dp);
return -1;
}
int NSCLASS dsr_rreq_opt_recv(struct dsr_pkt *dp, struct dsr_rreq_opt *rreq_opt)
{
struct in_addr myaddr;
struct in_addr trg;
struct dsr_srt *srt_rev, *srt_rc;
int action = DSR_PKT_NONE;
int i, n;
LOG_DBG("DSR RREQ\n");
if (!dp || !rreq_opt || dp->flags & PKT_PROMISC_RECV)
return DSR_PKT_DROP;
dp->num_rreq_opts++;
if (dp->num_rreq_opts > 1) {
LOG_DBG("More than one RREQ opt!!! - Ignoring\n");
return DSR_PKT_ERROR;
}
dp->rreq_opt = rreq_opt;
myaddr = my_addr();
trg.s_addr = rreq_opt->target;
if (dsr_rreq_duplicate(dp->src, trg, ntohs(rreq_opt->id))) {
LOG_DBG("Duplicate RREQ from %s\n", print_ip(dp->src));
return DSR_PKT_DROP;
}
rreq_tbl_add_id(dp->src, trg, ntohs(rreq_opt->id));
dp->srt = dsr_srt_new(dp->src, myaddr, DSR_RREQ_ADDRS_LEN(rreq_opt),
(char *)rreq_opt->addrs);
if (!dp->srt) {
LOG_DBG("Could not extract source route\n");
return DSR_PKT_ERROR;
}
LOG_DBG("RREQ target=%s src=%s dst=%s laddrs=%d\n",
print_ip(trg), print_ip(dp->src),
print_ip(dp->dst), DSR_RREQ_ADDRS_LEN(rreq_opt));
/* Add reversed source route */
srt_rev = dsr_srt_new_rev(dp->srt);
if (!srt_rev) {
LOG_DBG("Could not reverse source route\n");
return DSR_PKT_ERROR;
}
LOG_DBG("srt: %s\n", print_srt(dp->srt));
LOG_DBG("srt_rev: %s\n", print_srt(srt_rev));
dsr_rtc_add(srt_rev, ConfValToUsecs(RouteCacheTimeout), 0);
/* Set previous hop */
if (srt_rev->laddrs > 0)
dp->prv_hop = srt_rev->addrs[0];
else
dp->prv_hop = srt_rev->dst;
neigh_tbl_add(dp->prv_hop, dp->mac.ethh);
/* Send buffered packets */
send_buf_set_verdict(SEND_BUF_SEND, srt_rev->dst);
if (rreq_opt->target == myaddr.s_addr) {
LOG_DBG("RREQ OPT for me - Send RREP\n");
/* According to the draft, the dest addr in the IP header must
* be updated with the target address */
#ifdef NS2
dp->nh.iph->daddr() = (nsaddr_t) rreq_opt->target;
#else
dp->nh.iph->daddr = rreq_opt->target;
#endif
dsr_rrep_send(srt_rev, dp->srt);
action = DSR_PKT_NONE;
goto out;
}
n = DSR_RREQ_ADDRS_LEN(rreq_opt) / sizeof(struct in_addr);
if (dp->srt->src.s_addr == myaddr.s_addr)
return DSR_PKT_DROP;
for (i = 0; i < n; i++)
if (dp->srt->addrs[i].s_addr == myaddr.s_addr) {
action = DSR_PKT_DROP;
goto out;
}
/* TODO: Check Blacklist */
srt_rc = lc_srt_find(myaddr, trg);
if (srt_rc) {
struct dsr_srt *srt_cat;
/* Send cached route reply */
LOG_DBG("Send cached RREP\n");
srt_cat = dsr_srt_concatenate(dp->srt, srt_rc);
kfree(srt_rc);
if (!srt_cat) {
LOG_DBG("Could not concatenate\n");
goto rreq_forward;
}
LOG_DBG("srt_cat: %s\n", print_srt(srt_cat));
if (dsr_srt_check_duplicate(srt_cat) > 0) {
LOG_DBG("Duplicate address in source route!!!\n");
kfree(srt_cat);
goto rreq_forward;
}
#ifdef NS2
dp->nh.iph->daddr() = (nsaddr_t) rreq_opt->target;
#else
dp->nh.iph->daddr = rreq_opt->target;
#endif
LOG_DBG("Sending cached RREP to %s\n", print_ip(dp->src));
dsr_rrep_send(srt_rev, srt_cat);
action = DSR_PKT_NONE;
kfree(srt_cat);
} else {
rreq_forward:
dsr_pkt_alloc_opts_expand(dp, sizeof(struct in_addr));
if (!DSR_LAST_OPT(dp, rreq_opt)) {
char *to, *from;
to = (char *)rreq_opt + rreq_opt->length + 2 +
sizeof(struct in_addr);
from = (char *)rreq_opt + rreq_opt->length + 2;
memmove(to, from, sizeof(struct in_addr));
}
rreq_opt->addrs[n] = myaddr.s_addr;
rreq_opt->length += sizeof(struct in_addr);
dp->dh.opth->p_len = htons(ntohs(dp->dh.opth->p_len) +
sizeof(struct in_addr));
#ifdef __KERNEL__
dsr_build_ip(dp, dp->src, dp->dst, IP_HDR_LEN,
ntohs(dp->nh.iph->tot_len) +
sizeof(struct in_addr), IPPROTO_DSR,
dp->nh.iph->ttl);
#endif
/* Forward RREQ */
action = DSR_PKT_FORWARD_RREQ;
}
out:
kfree(srt_rev);
return action;
}
#ifdef __KERNEL__
static int
rreq_tbl_proc_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
{
int len;
len = rreq_tbl_print(&rreq_tbl, buffer);
*start = buffer + offset;
len -= offset;
if (len > length)
len = length;
else if (len < 0)
len = 0;
return len;
}
#endif /* __KERNEL__ */
int __init NSCLASS rreq_tbl_init(void)
{
#ifdef __KERNEL__
struct proc_dir_entry *proc;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23))
#define proc_net init_net.proc_net
#endif
proc = create_proc_read_entry(RREQ_TBL_PROC_NAME, 0, proc_net, rreq_tbl_proc_info, NULL);
if (!proc)
return -1;
get_random_bytes(&rreq_seqno, sizeof(unsigned int));
#else
rreq_seqno = 0;
#endif
INIT_TBL(&rreq_tbl, RREQ_TBL_MAX_LEN);
return 0;
}
void __exit NSCLASS rreq_tbl_cleanup(void)
{
struct rreq_tbl_entry *e;
while ((e = (struct rreq_tbl_entry *)tbl_detach_first(&rreq_tbl))) {
del_timer_sync(e->timer);
#ifdef NS2
delete e->timer;
#else
kfree(e->timer);
#endif
tbl_flush(&e->rreq_id_tbl, crit_none);
}
#ifdef __KERNEL__
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
proc_net_remove(RREQ_TBL_PROC_NAME);
#else
proc_net_remove(&init_net, RREQ_TBL_PROC_NAME);
#endif
#endif
}