-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsr-pkt.c
220 lines (158 loc) · 4.14 KB
/
dsr-pkt.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
/* 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/skbuff.h>
#include <linux/if_ether.h>
#endif
#ifdef NS2
#include "ns-agent.h"
#endif
#include "debug.h"
#include "dsr-opt.h"
#include "dsr.h"
char *dsr_pkt_alloc_opts(struct dsr_pkt *dp, int len)
{
if (!dp)
return NULL;
dp->dh.raw = (char *)kmalloc(len + DEFAULT_TAILROOM, GFP_ATOMIC);
if (!dp->dh.raw)
return NULL;
dp->dh.tail = dp->dh.raw + len;
dp->dh.end = dp->dh.tail + DEFAULT_TAILROOM;
return dp->dh.raw;
}
char *dsr_pkt_alloc_opts_expand(struct dsr_pkt *dp, int len)
{
char *tmp;
int old_len;
if (!dp || !dp->dh.raw)
return NULL;
if (dsr_pkt_tailroom(dp) > len) {
tmp = dp->dh.tail;
dp->dh.tail += len;
return tmp;
}
tmp = dp->dh.raw;
old_len = dsr_pkt_opts_len(dp);
if (!dsr_pkt_alloc_opts(dp, old_len + len))
return NULL;
memcpy(dp->dh.raw, tmp, old_len);
kfree(tmp);
return (dp->dh.raw + old_len);
}
int dsr_pkt_free_opts(struct dsr_pkt *dp)
{
int len;
if (!dp->dh.raw)
return -1;
len = dsr_pkt_opts_len(dp);
kfree(dp->dh.raw);
dp->dh.raw = dp->dh.end = dp->dh.tail = NULL;
dp->srt_opt = NULL;
dp->rreq_opt = NULL;
memset(dp->rrep_opt, 0, sizeof(struct dsr_rrep_opt *) * MAX_RREP_OPTS);
memset(dp->rerr_opt, 0, sizeof(struct dsr_rerr_opt *) * MAX_RERR_OPTS);
memset(dp->ack_opt, 0, sizeof(struct dsr_ack_opt *) * MAX_ACK_OPTS);
dp->num_rrep_opts = dp->num_rerr_opts = dp->num_ack_opts = 0;
return len;
}
#ifdef NS2
struct dsr_pkt *dsr_pkt_alloc(Packet * p)
{
struct dsr_pkt *dp;
struct hdr_cmn *cmh;
int dsr_opts_len = 0;
dp = (struct dsr_pkt *)kmalloc(sizeof(struct dsr_pkt), GFP_ATOMIC);
if (!dp)
return NULL;
memset(dp, 0, sizeof(struct dsr_pkt));
if (p) {
cmh = hdr_cmn::access(p);
dp->p = p;
dp->mac.raw = p->access(hdr_mac::offset_);
dp->nh.iph = HDR_IP(p);
dp->src.s_addr =
Address::instance().get_nodeaddr(dp->nh.iph->saddr());
dp->dst.s_addr =
Address::instance().get_nodeaddr(dp->nh.iph->daddr());
if (cmh->ptype() == PT_DSR) {
struct dsr_opt_hdr *opth;
opth = HDR_DSRUU(p);
dsr_opts_len = opth->p_len + DSR_OPT_HDR_LEN;
if (!dsr_pkt_alloc_opts(dp, dsr_opts_len)) {
kfree(dp);
return NULL;
}
memcpy(dp->dh.raw, (char *)opth, dsr_opts_len);
dsr_opt_parse(dp);
if ((DATA_PACKET(dp->dh.opth->nh) ||
dp->dh.opth->nh == PT_PING) &&
ConfVal(UseNetworkLayerAck))
dp->flags |= PKT_REQUEST_ACK;
} else if ((DATA_PACKET(cmh->ptype()) ||
cmh->ptype() == PT_PING) &&
ConfVal(UseNetworkLayerAck))
dp->flags |= PKT_REQUEST_ACK;
/* A trick to calculate payload length... */
dp->payload_len = cmh->size() - dsr_opts_len - IP_HDR_LEN;
}
return dp;
}
#else
struct dsr_pkt *dsr_pkt_alloc(struct sk_buff *skb)
{
struct dsr_pkt *dp;
int dsr_opts_len = 0;
dp = (struct dsr_pkt *)kmalloc(sizeof(struct dsr_pkt), GFP_ATOMIC);
if (!dp)
return NULL;
memset(dp, 0, sizeof(struct dsr_pkt));
if (skb) {
/* skb_unlink(skb); */
dp->skb = skb;
dp->mac.raw = SKB_MAC_HDR_RAW(skb);
dp->nh.iph = SKB_NETWORK_HDR_IPH(skb);
dp->src.s_addr = dp->nh.iph->saddr;
dp->dst.s_addr = dp->nh.iph->daddr;
if (dp->nh.iph->protocol == IPPROTO_DSR) {
struct dsr_opt_hdr *opth;
int n;
opth = (struct dsr_opt_hdr *)(dp->nh.raw + (dp->nh.iph->ihl << 2));
dsr_opts_len = ntohs(opth->p_len) + DSR_OPT_HDR_LEN;
if (!dsr_pkt_alloc_opts(dp, dsr_opts_len)) {
kfree(dp);
return NULL;
}
memcpy(dp->dh.raw, (char *)opth, dsr_opts_len);
n = dsr_opt_parse(dp);
LOG_DBG("Packet has %d DSR option(s)\n", n);
}
dp->payload = dp->nh.raw +
(dp->nh.iph->ihl << 2) + dsr_opts_len;
dp->payload_len = ntohs(dp->nh.iph->tot_len) -
(dp->nh.iph->ihl << 2) - dsr_opts_len;
if (dp->payload_len)
dp->flags |= PKT_REQUEST_ACK;
}
return dp;
}
#endif
void dsr_pkt_free(struct dsr_pkt *dp)
{
if (!dp)
return;
#ifndef NS2
if (dp->skb)
dev_kfree_skb_any(dp->skb);
#endif
dsr_pkt_free_opts(dp);
if (dp->srt)
kfree(dp->srt);
kfree(dp);
return;
}