-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpacket_in.c
More file actions
executable file
·264 lines (214 loc) · 6.85 KB
/
packet_in.c
File metadata and controls
executable file
·264 lines (214 loc) · 6.85 KB
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
/***************************************************************************
packet_in.c - description
-------------------
begin : Mon Jul 29 2002
copyright : (C) 2002 by Luke Klein-Berndt
email : kleinb@nist.gov
***************************************************************************/
/***************************************************************************
Modified by Miguel Catalan Cid - miguel.catcid@gmail.com - Version: Mon Jan 1 2010
***************************************************************************/
#include "packet_in.h"
#ifdef DTN
extern int dtn_register;
#endif
#ifdef BLACKLIST
extern u_int32_t aodv_blacklist_ip[10];
extern u_int32_t dtn_blacklist_ip[10];
extern int aodv_blacksize;
extern int dtn_blacksize;
#endif
int valid_aodv_packet(int numbytes, int type, char *data, struct timeval tv) {
rerr *tmp_rerr;
rrep *tmp_rrep;
hello *tmp_hello;
rreq *tmp_rreq;
rreq_st *tmp_strreq;
#ifdef RECOVERYPATH
rcvp *tmp_rcvp;
rrdp *tmp_rrdp;
#endif
s_probe *tmp_sprobe;
l_probe *tmp_lprobe;
ett_info *tmp_ett_info;
switch (type) {
case ST_RREQ_MESSAGE:
tmp_strreq = (rreq_st *) data;
if (numbytes == sizeof(rreq_st)) {
return 1;
}
break;
case RREP_MESSAGE:
tmp_rrep = (rrep *) data;
if (numbytes == sizeof(rrep)) {
return 1;
}
break;
case RERR_MESSAGE:
tmp_rerr = (rerr *) data;
if (numbytes == sizeof(rerr)) {
return 1;
}
break;
case HELLO_MESSAGE:
tmp_hello = (hello *) data;
if (numbytes == (sizeof(hello) + (sizeof(hello_extension)
* tmp_hello->neigh_count))) {
return 1;
}
break;
/****************Ìí¼ÓÀàÐÍRCVP_MESSAGE*******************/
#ifdef RECOVERYPATH
case RCVP_MESSAGE:
tmp_rcvp = (rcvp *) data;
if(numbytes == sizeof(rcvp))
return 1;
break;
case RRDP_MESSAGE:
tmp_rrdp = (rrdp *) data;
if(numbytes == sizeof(rrdp))
return 1;
break;
#endif
case ETT_S_MESSAGE: //ETT small message received
tmp_sprobe = (s_probe *) data;
if (numbytes == sizeof(s_probe)) {
tmp_sprobe->sec = (u_int32_t) tv.tv_sec;
tmp_sprobe->usec= (u_int32_t) tv.tv_usec;
return 1;
}
break;
case ETT_L_MESSAGE: //ETT large message received
tmp_lprobe = (l_probe *) data;
if (numbytes == sizeof(l_probe)) {
tmp_lprobe->sec = (u_int32_t) tv.tv_sec;
tmp_lprobe->usec= (u_int32_t) tv.tv_usec;
return 1;
}
break;
case RREQ_MESSAGE:
tmp_rreq = (rreq *) data;
if (numbytes == sizeof(rreq))
return 1;
break;
case ETT_INFO_MSG:
tmp_ett_info = (ett_info *) data;
if (numbytes == sizeof(ett_info))
return 1;
break;
default:
break;
}
return 0;
}
int packet_in(struct sk_buff *packet, struct timeval tv) {
struct iphdr *ip;
// Create aodv message types
u_int8_t aodv_type;
//The packets which come in still have their headers from the IP and UDP
int start_point = sizeof(struct udphdr) + sizeof(struct iphdr);
//get pointers to the important parts of the message
ip = ip_hdr(packet);
aodv_type = (int) packet->data[start_point];
#ifdef CaiDebug
if( aodv_type == RCVP_MESSAGE)
printk("It's a rcvp message\n");
#endif
#ifdef DEBUG
if ( aodv_type != HELLO_MESSAGE )
printk("packet_in: type: %d and of size %u from: %s\n", aodv_type, packet->len - start_point, inet_ntoa(ip->saddr));
#endif
if (!valid_aodv_packet(packet->len - start_point, aodv_type, packet->data
+ start_point, tv)) {
#ifdef DEBUG
printk("Packet of type: %d and of size %u from: %s failed packet check!\n", aodv_type, packet->len - start_point, inet_ntoa(ip->saddr));
#endif
return NF_DROP;
}
insert_task(aodv_type, packet);
return NF_ACCEPT;
}
extern int initialized;
unsigned int input_handler(unsigned int hooknum, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out, int (*okfn) (struct sk_buff *)) {
struct timeval tv;
u_int8_t aodv_type;
int start_point = sizeof(struct udphdr) + sizeof(struct iphdr);
aodv_type = (int) skb->data[start_point];
struct iphdr *ip = ip_hdr(skb);
struct in_device *tmp_indev = (struct in_device *) in->ip_ptr;
void *p = (uint32_t *) ip + ip->ihl;
struct udphdr *udp = (struct udphdr *) p;
if (!initialized) { // this is required otherwise kernel calls this function without insmod completing the module loading process.
return NF_ACCEPT;
}
char src[16];
char dst[16];
strcpy(src, inet_ntoa(ip->saddr));
strcpy(dst, inet_ntoa(ip->daddr));
#ifdef DTN
static int t = 1;
//DTN register
if (t && !dtn_register && udp->dest == htons(DTNREGISTER)){
dtn_register = 1;
t = 0;
printk("*************DTN registered*************\n");
}
extern u_int32_t g_mesh_ip;
if( dtn_register && udp->dest == htons(GET_DTN_HELLO) ){
insert_timer_simple(TASK_DTN_HELLO, 0, g_mesh_ip);
update_timer_queue();
printk("*************GOT DTN HELLO TASK*************\n");
}
if( dtn_register && udp->dest == htons(AODV_LOCATION_PORT)){
extern u_int32_t dtn_hello_ip;
extern u_int32_t longitude;
extern u_int32_t latitude;
void *loc_data;
loc_data = &(skb->data[start_point]);
u_int32_t src_ip = ((u_int32_t *)loc_data)[0];
u_int32_t tos = ((u_int32_t *)loc_data)[1];
u_int32_t x = ((u_int32_t *)loc_data)[2];
u_int32_t y = ((u_int32_t *)loc_data)[3];
longitude = x;
latitude = y;
/*u_int32_t src_ip = ((u_int32_t *)skb->data)[0];
u_int32_t tos = ((u_int32_t *)skb->data)[1];
u_int32_t x = ((u_int32_t *)skb->data)[2];
u_int32_t y = ((u_int32_t *)skb->data)[3];*/
#ifdef CaiDebug
printk("*********GOT Location Info%s %ld %ld**********\n",inet_ntoa(src_ip),ntohl(x),ntohl(y));
#endif
//gen_rrep(src_ip,dtn_hello_ip,(unsigned char)tos);
insert_timer_directional(TASK_SEND_RREP, RREP_TIMER, 0,
src_ip, dtn_hello_ip,(unsigned char)tos);
update_timer_queue();
}
#ifdef BLACKLIST
int k = 0;
if (udp->dest == htons(9556)) {
for(k=0; k<dtn_blacksize; k++) {
if (!strcmp("192.168.1.255",dst) && dtn_blacklist_ip[k] == ip->saddr) {
return NF_DROP;
}
}
}
#endif
#endif
#ifdef DEBUG
if (ip->protocol == IPPROTO_ICMP) {
printk("input_handler: Recv a ICMP from %s to %s\n", src, dst);
} else if (aodv_type != HELLO_MESSAGE) {
printk("input_handler: Recv a %d Packet, from %s to %s\n",ip->protocol, src, dst);
}
#endif
if (udp != NULL && ip->protocol == IPPROTO_UDP && skb->protocol == htons(ETH_P_IP) && udp->dest == htons(AODVPORT) && tmp_indev->ifa_list->ifa_address != ip->saddr) {
do_gettimeofday(&tv); //MCC - capture the time of arrival needed for ett_probes
return packet_in((skb), tv);
}
#ifdef DEBUG0
if (aodv_type != HELLO_MESSAGE)
printk("input_handler: input without process.\n");
#endif
return NF_ACCEPT;
}