-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdsr-opt.c
306 lines (263 loc) · 6.3 KB
/
dsr-opt.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
/* 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 <net/ip.h>
#endif
#ifdef NS2
#include "ns-agent.h"
#endif
#include "debug.h"
#include "dsr.h"
#include "dsr-opt.h"
#include "dsr-rreq.h"
#include "dsr-rrep.h"
#include "dsr-rerr.h"
#include "dsr-srt.h"
#include "dsr-ack.h"
struct dsr_opt_hdr *dsr_opt_hdr_add(char *buf, unsigned int len,
unsigned int protocol)
{
struct dsr_opt_hdr *opt_hdr;
if (len < DSR_OPT_HDR_LEN)
return NULL;
opt_hdr = (struct dsr_opt_hdr *)buf;
opt_hdr->nh = protocol;
opt_hdr->f = 0;
opt_hdr->res = 0;
opt_hdr->p_len = htons(len - DSR_OPT_HDR_LEN);
return opt_hdr;
}
#ifdef __KERNEL__
struct iphdr *dsr_build_ip(struct dsr_pkt *dp, struct in_addr src,
struct in_addr dst, int ip_len, int tot_len,
int protocol, int ttl)
{
struct iphdr *iph;
dp->nh.iph = iph = (struct iphdr *)dp->ip_data;
if (dp->skb && SKB_NETWORK_HDR_RAW(dp->skb)) {
memcpy(dp->ip_data, SKB_NETWORK_HDR_RAW(dp->skb), ip_len);
} else {
iph->version = IPVERSION;
iph->ihl = 5;
iph->tos = 0;
iph->id = 0;
iph->frag_off = 0;
iph->ttl = (ttl ? ttl : IPDEFTTL);
iph->saddr = src.s_addr;
iph->daddr = dst.s_addr;
}
iph->tot_len = htons(tot_len);
iph->protocol = protocol;
ip_send_check(iph);
return iph;
}
#endif
struct dsr_opt *dsr_opt_find_opt(struct dsr_pkt *dp, int type)
{
int dsr_len, l;
struct dsr_opt *dopt;
dsr_len = dsr_pkt_opts_len(dp);
l = DSR_OPT_HDR_LEN;
dopt = DSR_GET_OPT(dp->dh.opth);
while (l < dsr_len && (dsr_len - l) > 2) {
if (type == dopt->type)
return dopt;
l += dopt->length + 2;
dopt = DSR_GET_NEXT_OPT(dopt);
}
return NULL;
}
int NSCLASS dsr_opt_remove(struct dsr_pkt *dp)
{
int len, ip_len, prot, ttl;
if (!dp || !dp->dh.raw)
return -1;
prot = dp->dh.opth->nh;
#ifdef NS2
ip_len = 20;
ttl = dp->nh.iph->ttl();
#else
ip_len = (dp->nh.iph->ihl << 2);
ttl = dp->nh.iph->ttl;
#endif
dsr_build_ip(dp, dp->src, dp->dst, ip_len,
ip_len + dp->payload_len, prot, ttl);
len = dsr_pkt_free_opts(dp);
/* Return bytes removed */
return len;
}
int dsr_opt_parse(struct dsr_pkt *dp)
{
int dsr_len, l, n = 0;
struct dsr_opt *dopt;
if (!dp)
return -1;
dsr_len = dsr_pkt_opts_len(dp);
l = DSR_OPT_HDR_LEN;
dopt = DSR_GET_OPT(dp->dh.opth);
dp->num_rrep_opts = dp->num_rerr_opts = dp->num_rreq_opts = dp->num_ack_opts = 0;
dp->srt_opt = NULL;
dp->ack_req_opt = NULL;
while (l < dsr_len && (dsr_len - l) > 2) {
switch (dopt->type) {
case DSR_OPT_PADN:
break;
case DSR_OPT_RREQ:
if (dp->num_rreq_opts == 0)
dp->rreq_opt = (struct dsr_rreq_opt *)dopt;
#ifndef NS2
else
LOG_DBG("ERROR: More than one RREQ option!!\n");
#endif
break;
case DSR_OPT_RREP:
if (dp->num_rrep_opts < MAX_RREP_OPTS)
dp->rrep_opt[dp->num_rrep_opts++] = (struct dsr_rrep_opt *)dopt;
#ifndef NS2
else
LOG_DBG("Maximum RREP opts in one packet reached\n");
#endif
break;
case DSR_OPT_RERR:
if (dp->num_rerr_opts < MAX_RERR_OPTS)
dp->rerr_opt[dp->num_rerr_opts++] = (struct dsr_rerr_opt *)dopt;
#ifndef NS2
else
LOG_DBG("Maximum RERR opts in one packet reached\n");
#endif
break;
case DSR_OPT_PREV_HOP:
break;
case DSR_OPT_ACK:
if (dp->num_ack_opts < MAX_ACK_OPTS)
dp->ack_opt[dp->num_ack_opts++] = (struct dsr_ack_opt *)dopt;
#ifndef NS2
else
LOG_DBG("Maximum ACK opts in one packet reached\n");
#endif
break;
case DSR_OPT_SRT:
if (!dp->srt_opt)
dp->srt_opt = (struct dsr_srt_opt *)dopt;
#ifndef NS2
else
LOG_DBG("More than one source route in packet\n");
#endif
break;
case DSR_OPT_TIMEOUT:
break;
case DSR_OPT_FLOWID:
break;
case DSR_OPT_ACK_REQ:
if (!dp->ack_req_opt)
dp->ack_req_opt = (struct dsr_ack_req_opt *)dopt;
#ifndef NS2
else
LOG_DBG("More than one ACK REQ in packet\n");
#endif
break;
case DSR_OPT_PAD1:
l++;
dopt++;
continue;
#ifndef NS2
default:
LOG_DBG("Unknown DSR option type=%d\n", dopt->type);
#endif
}
l += dopt->length + 2;
dopt = DSR_GET_NEXT_OPT(dopt);
n++;
}
return n;
}
int NSCLASS dsr_opt_recv(struct dsr_pkt *dp)
{
int dsr_len, l;
int action = 0;
struct dsr_opt *dopt;
struct in_addr myaddr;
if (!dp)
return DSR_PKT_ERROR;
myaddr = my_addr();
/* Packet for us ? */
#ifdef NS2
//LOG_DBG("Next header=%s\n", packet_info.name((packet_t)dp->dh.opth->nh));
if (dp->dst.s_addr == myaddr.s_addr &&
(DATA_PACKET(dp->dh.opth->nh) || dp->dh.opth->nh == PT_PING))
action |= DSR_PKT_DELIVER;
#else
if (dp->dst.s_addr == myaddr.s_addr && dp->payload_len != 0)
action |= DSR_PKT_DELIVER;
#endif
dsr_len = dsr_pkt_opts_len(dp);
l = DSR_OPT_HDR_LEN;
dopt = DSR_GET_OPT(dp->dh.opth);
//LOG_DBG("Parsing DSR packet l=%d dsr_len=%d\n", l, dsr_len);
while (l < dsr_len && (dsr_len - l) > 2) {
//LOG_DBG("dsr_len=%d l=%d\n", dsr_len, l);
switch (dopt->type) {
case DSR_OPT_PADN:
break;
case DSR_OPT_RREQ:
if (dp->flags & PKT_PROMISC_RECV)
break;
action |= dsr_rreq_opt_recv(dp, (struct dsr_rreq_opt *)dopt);
break;
case DSR_OPT_RREP:
/* We should probably allow promisuously
* receiving RREPs */
if (dp->flags & PKT_PROMISC_RECV)
break;
action |= dsr_rrep_opt_recv(dp, (struct dsr_rrep_opt *)dopt);
break;
case DSR_OPT_RERR:
if (dp->flags & PKT_PROMISC_RECV)
break;
if (dp->num_rerr_opts < MAX_RERR_OPTS) {
action |=
dsr_rerr_opt_recv(dp, (struct dsr_rerr_opt *)dopt);
}
break;
case DSR_OPT_PREV_HOP:
break;
case DSR_OPT_ACK:
if (dp->flags & PKT_PROMISC_RECV)
break;
if (dp->num_ack_opts < MAX_ACK_OPTS) {
dp->ack_opt[dp->num_ack_opts++] =
(struct dsr_ack_opt *)dopt;
action |=
dsr_ack_opt_recv((struct dsr_ack_opt *)
dopt);
}
break;
case DSR_OPT_SRT:
action |= dsr_srt_opt_recv(dp, (struct dsr_srt_opt *)dopt);
break;
case DSR_OPT_TIMEOUT:
break;
case DSR_OPT_FLOWID:
break;
case DSR_OPT_ACK_REQ:
action |=
dsr_ack_req_opt_recv(dp, (struct dsr_ack_req_opt *)
dopt);
break;
case DSR_OPT_PAD1:
l++;
dopt++;
continue;
default:
LOG_DBG("Unknown DSR option type=%d\n", dopt->type);
}
l += dopt->length + 2;
dopt = DSR_GET_NEXT_OPT(dopt);
}
return action;
}