-
Notifications
You must be signed in to change notification settings - Fork 1
/
ll.cc
341 lines (301 loc) · 9.26 KB
/
ll.cc
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
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
* Copyright (c) 1997 Regents of the University of California.
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Daedalus Research
* Group at the University of California Berkeley.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* Contributed by the Daedalus Research Group, http://daedalus.cs.berkeley.edu
*/
#ifndef lint
static const char rcsid[] =
"@(#) $Header: /cvsroot/nsnam/ns-2/mac/ll.cc,v 1.47 2010/03/08 05:54:51 tom_henderson Exp $ (UCB)";
#endif
#include <errmodel.h>
#include <mac.h>
#include <ll.h>
#include <address.h>
#include <dsr/hdr_sr.h>
int hdr_ll::offset_;
static class LLHeaderClass : public PacketHeaderClass {
public:
LLHeaderClass() : PacketHeaderClass("PacketHeader/LL",
sizeof(hdr_ll)) {
bind_offset(&hdr_ll::offset_);
}
} class_hdr_ll;
static class LLClass : public TclClass {
public:
LLClass() : TclClass("LL") {}
TclObject* create(int, const char*const*) {
return (new LL);
}
} class_ll;
LL::LL() : LinkDelay(), seqno_(0), ackno_(0), macDA_(0), ifq_(0),
mac_(0), lanrouter_(0), arptable_(0), varp_(0),
downtarget_(0), uptarget_(0)
{
bind("macDA_", &macDA_);
// Following three lines for Wormhole to initilize wormhole peer list head
wormhole_head.ll = NULL;
wormhole_head.id = -1;
wormhole_head.next = NULL;
}
int LL::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc == 3) {
if (strcmp(argv[1], "ifq") == 0) {
ifq_ = (Queue*) TclObject::lookup(argv[2]);
return (TCL_OK);
}
if(strcmp(argv[1], "arptable") == 0) {
arptable_ = (ARPTable*)TclObject::lookup(argv[2]);
assert(arptable_);
return TCL_OK;
}
if(strcmp(argv[1], "varp") == 0) {
varp_ = (VARPTable*)TclObject::lookup(argv[2]);
assert(varp_);
return TCL_OK;
}
if (strcmp(argv[1], "mac") == 0) {
mac_ = (Mac*) TclObject::lookup(argv[2]);
assert(mac_);
return (TCL_OK);
}
if (strcmp(argv[1], "down-target") == 0) {
downtarget_ = (NsObject*) TclObject::lookup(argv[2]);
return (TCL_OK);
}
if (strcmp(argv[1], "up-target") == 0) {
uptarget_ = (NsObject*) TclObject::lookup(argv[2]);
return (TCL_OK);
}
if (strcmp(argv[1], "lanrouter") == 0) {
lanrouter_ = (LanRouter*) TclObject::lookup(argv[2]);
return (TCL_OK);
}
//The following block of Else if for Wormhole Attack
else if( strcmp( argv[1], "wormhole-peer" ) == 0 ) {
wormhole_peer* wp = (wormhole_peer*) malloc( sizeof( wormhole_peer ) );
if( !wp ) {
fprintf( stderr, "(%03d) - LL::command - error allocating memory for new wormhole peer!" );
exit(-1);
}
// init fields
wp->ll = (LL *) TclObject::lookup( argv[2] );
wp->id = wp->ll->mac_->addr();
// insert at head of list
wp->next = wormhole_head.next;
wormhole_head.next = wp;
printf( "(%03d) - LL::command - added %d to wormhole peer list\n", mac_->addr(), wp->id );
return TCL_OK;
}
}
else if (argc == 2) {
if (strcmp(argv[1], "ifq") == 0) {
tcl.resultf("%s", ifq_->name());
return (TCL_OK);
}
if (strcmp(argv[1], "mac") == 0) {
tcl.resultf("%s", mac_->name());
return (TCL_OK);
}
if (strcmp(argv[1], "down-target") == 0) {
tcl.resultf("%s", downtarget_->name());
return (TCL_OK);
}
if (strcmp(argv[1], "up-target") == 0) {
tcl.resultf("%s", uptarget_->name());
return (TCL_OK);
}
}
return LinkDelay::command(argc, argv);
}
void LL::recv(Packet* p, Handler* /*h*/)
{
hdr_cmn *ch = HDR_CMN(p);
//char *mh = (char*) HDR_MAC(p);
//struct hdr_sr *hsr = HDR_SR(p);
/*
* Sanity Check
*/
assert(initialized());
//if(p->incoming) {
//p->incoming = 0;
//}
// XXXXX NOTE: use of incoming flag has been depracated; In order to track direction of pkt flow, direction_ in hdr_cmn is used instead. see packet.h for details.
// If direction = UP, then pass it up the stack
// Otherwise, set direction to DOWN and pass it down the stack
if(ch->direction() == hdr_cmn::UP) {
//if(mac_->hdr_type(mh) == ETHERTYPE_ARP)
if(ch->ptype_ == PT_ARP)
arptable_->arpinput(p, this);
else
uptarget_ ? sendUp(p) : drop(p);
return;
}
ch->direction() = hdr_cmn::DOWN;
sendDown(p);
}
void LL::sendDown(Packet* p)
{
hdr_cmn *ch = HDR_CMN(p);
hdr_ip *ih = HDR_IP(p);
//following three lines for Wormhole Attack
int is_broadcast = 0;
int unicast_addr = -1;
int is_routing = 0;
nsaddr_t dst = (nsaddr_t)Address::instance().get_nodeaddr(ih->daddr());
//nsaddr_t dst = ih->dst();
hdr_ll *llh = HDR_LL(p);
char *mh = (char*)p->access(hdr_mac::offset_);
llh->seqno_ = ++seqno_;
llh->lltype() = LL_DATA;
mac_->hdr_src(mh, mac_->addr());
mac_->hdr_type(mh, ETHERTYPE_IP);
int tx = 0;
switch(ch->addr_type()) {
case NS_AF_ILINK:
mac_->hdr_dst((char*) HDR_MAC(p), ch->next_hop());
// check next hop for wormhole peer / broadcast
if( ch->next_hop() == MAC_BROADCAST )
is_broadcast = 1;
else
unicast_addr = ch->next_hop();
// till here for WormHole
break;
case NS_AF_INET:
dst = ch->next_hop();
/* FALL THROUGH */
case NS_AF_NONE:
//From here for Wormhole
if (IP_BROADCAST == (u_int32_t) dst)
{
mac_->hdr_dst((char*) HDR_MAC(p), MAC_BROADCAST);
is_broadcast = 1;
break;
}
else {
unicast_addr = dst;
}
// till here for Wormhole
/* Assuming arptable is present, send query */
if (arptable_) {
tx = arptable_->arpresolve(dst, p, this);
break;
}
//if (varp_) {
//tx = varp_->arpresolve(dst, p);
//break;
//}
/* FALL THROUGH */
default:
int IPnh = (lanrouter_) ? lanrouter_->next_hop(p) : -1;
if (IPnh < 0)
mac_->hdr_dst((char*) HDR_MAC(p),macDA_);
else if (varp_)
tx = varp_->arpresolve(IPnh, p);
else
mac_->hdr_dst((char*) HDR_MAC(p), IPnh);
break;
}
if (tx == 0) {
//from here for Wormhole attack
Scheduler& s = Scheduler::instance();
// wormhole decision point (decide if this packet is going throught the wormhole or not)
if( wormhole_head.next ) {
if( is_broadcast ) {
// send a copy to each wormhole peer
wormhole_peer *wp = &wormhole_head;
while( wp->next ) {
wp = wp->next;
Packet *p_copy = p->copy();
hdr_cmn::access(p_copy)->direction() = hdr_cmn::UP;
s.schedule( wp->ll, p_copy, delay_ );
}
// AND send it out our "real" interface (and gather stats)
if( is_routing ) {
routing_packet_count++;
routing_byte_count += ch->size_;
}
else {
data_packet_count++;
data_byte_count += ch->size_;
}
s.schedule(downtarget_, p, delay_);
return;
}
else {
// scan through the list to see if it is for a wormhole peer
wormhole_peer *wp_curr = wormhole_head.next;
wormhole_peer *wp_prev = &wormhole_head;
while( wp_curr ) {
if( wp_curr->id == unicast_addr ) {
// if we found a match then send the packet too this wormhole peer only
hdr_cmn::access(p)->direction() = hdr_cmn::UP;
s.schedule( wp_curr->ll, p, delay_ );
// move this wormhole peer to the front of the list
// (optimizes many unicasts to the same peers)
wp_prev->next = wp_curr->next;
wp_curr->next = wormhole_head.next;
wormhole_head.next = wp_curr;
return;
}
// otherwise keep looking through list
wp_prev = wp_curr;
wp_curr = wp_curr->next;
}
// fall through if we don't find a matching wormhole peer
}
}
// let mac decide when to take a new packet from the queue.
if( is_routing ) {
routing_packet_count++;
routing_byte_count += ch->size_;
}
else {
data_packet_count++;
data_byte_count += ch->size_;
}
s.schedule(downtarget_, p, delay_);
}
}
//Wormhole till here
void LL::sendUp(Packet* p)
{
Scheduler& s = Scheduler::instance();
if (hdr_cmn::access(p)->error() > 0)
drop(p);
else
s.schedule(uptarget_, p, delay_);
}
inline void LL::hdr_dst(Packet *, int)
{}