forked from vollero/openCAPWAP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWTPmacFrameReceive.c
More file actions
264 lines (192 loc) · 8.21 KB
/
WTPmacFrameReceive.c
File metadata and controls
264 lines (192 loc) · 8.21 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
/*******************************************************************************************
* Copyright (c) 2006-7 Laboratorio di Sistemi di Elaborazione e Bioingegneria Informatica *
* Universita' Campus BioMedico - Italy *
* *
* This program is free software; you can redistribute it and/or modify it under the terms *
* of the GNU General Public License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with this *
* program; if not, write to the: *
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, *
* MA 02111-1307, USA. *
* *
* --------------------------------------------------------------------------------------- *
* Project: Capwap *
* *
* Author : Ludovico Rossi (ludo@bluepixysw.com) *
* Del Moro Andrea (andrea_delmoro@libero.it) *
* Giovannini Federica (giovannini.federica@gmail.com) *
* Massimo Vellucci (m.vellucci@unicampus.it) *
* Mauro Bisson (mauro.bis@gmail.com) *
*******************************************************************************************/
#include "WTPmacFrameReceive.h"
#include "common.h"
#include "ieee802_11_defs.h"
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
#define TYPE_LEN 2
#define ETH_ALEN 6
#define ETH_HLEN 14
#define FRAME_80211_LEN 24
#ifdef DMALLOC
#include "../dmalloc-5.5.0/dmalloc.h"
#endif
#define EXIT_FRAME_THREAD(sock) CWLog("ERROR Handling Frames: application will be closed!"); \
close(sock); \
exit(1);
int getMacAddr(int sock, char* interface, unsigned char* macAddr){
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strcpy(s.ifr_name, interface);
if(!ioctl(fd, SIOCGIFHWADDR, &s))
memcpy(macAddr, s.ifr_addr.sa_data, MAC_ADDR_LEN);
CWDebugLog("\n");
return 1;
}
int extractFrameInfo(char* buffer, char* RSSI, char* SNR, int* dataRate){
int signal, noise;
*RSSI=buffer[RSSI_BYTE]-ATHEROS_CONV_VALUE; //RSSI in dBm
signal=buffer[SIGNAL_BYTE]-ATHEROS_CONV_VALUE;
noise=buffer[NOISE_BYTE];
*SNR=(char)signal-noise; //RSN in dB
*dataRate=(buffer[DATARATE_BYTE]/2)*10; //Data rate in Mbps*10
return 1;
}
int extractFrame(CWProtocolMessage** frame, unsigned char* buffer, int len){
CW_CREATE_OBJECT_ERR(*frame, CWProtocolMessage, return 0;);
CWProtocolMessage *auxPtr = *frame;
CW_CREATE_PROTOCOL_MESSAGE(*auxPtr, len-PRISMH_LEN, return 0;);
memcpy(auxPtr->msg, buffer+PRISMH_LEN, len-PRISMH_LEN);
auxPtr->offset=len-PRISMH_LEN;
return 1;
}
int extract802_11_Frame(CWProtocolMessage** frame, unsigned char* buffer, int len){
CW_CREATE_OBJECT_ERR(*frame, CWProtocolMessage, return 0;);
CWProtocolMessage *auxPtr = *frame;
CW_CREATE_PROTOCOL_MESSAGE(*auxPtr, len, return 0;);
memcpy(auxPtr->msg, buffer, len);
auxPtr->offset=len;
return 1;
}
int extractAddr(unsigned char* destAddr, unsigned char* sourceAddr, char* frame){
memset(destAddr, 0, MAC_ADDR_LEN);
memset(sourceAddr, 0, MAC_ADDR_LEN);
memcpy(destAddr, frame+DEST_ADDR_START, MAC_ADDR_LEN);
memcpy(sourceAddr, frame+SOURCE_ADDR_START, MAC_ADDR_LEN);
return 1;
}
int macAddrCmp (unsigned char* addr1, unsigned char* addr2){
int i, ok=1;
for (i=0; i<MAC_ADDR_LEN; i++) {
if (addr1[i]!=addr2[i])
{ok=0;}
}
if (ok==1) {CWDebugLog("MAC Address test: OK\n");}
else {CWDebugLog("MAC Address test: Failed\n");}
return ok;
}
int from_8023_to_80211( unsigned char *inbuffer,int inlen, unsigned char *outbuffer, unsigned char *own_addr){
int indx=0;
struct ieee80211_hdr hdr;
os_memset(&hdr,0,sizeof(struct ieee80211_hdr));
hdr.frame_control = IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
hdr.duration_id = 0;
hdr.seq_ctrl = 0;
os_memcpy(hdr.addr1, own_addr, ETH_ALEN);
os_memcpy(hdr.addr2, inbuffer + ETH_ALEN, ETH_ALEN);
os_memcpy(hdr.addr3, inbuffer, ETH_ALEN);
CLEARBIT(hdr.frame_control,9);
SETBIT(hdr.frame_control,8);
os_memcpy(outbuffer + indx,&hdr,sizeof(hdr));
indx += sizeof(hdr);
os_memcpy(outbuffer + indx, inbuffer, inlen);
indx += inlen;
return indx;
}
int gRawSock;
extern int wtpInRunState;
int CWWTPSendFrame(unsigned char *buf, int len){
if( send(gRawSock, buf + FRAME_80211_LEN, len - FRAME_80211_LEN,0) < 1 ){
CWDebugLog("Error to send frame on raw socket");
return -1;
}
CWDebugLog("Send (%d) bytes on raw socket",len - FRAME_80211_LEN);
return 1;
}
CW_THREAD_RETURN_TYPE CWWTPReceiveFrame(void *arg){
int n,encaps_len;
unsigned char buffer[CW_BUFFER_SIZE];
unsigned char buf80211[CW_BUFFER_SIZE];
unsigned char macAddr[MAC_ADDR_LEN];
struct sockaddr_ll addr;
CWProtocolMessage* frame=NULL;
CWBindingDataListElement* listElement=NULL;
struct ifreq ethreq;
CWThreadSetSignals(SIG_BLOCK, 1, SIGALRM);
if ((gRawSock=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))<0) {
CWDebugLog("THR FRAME: Error creating socket");
CWExitThread();
}
memset(&addr, 0, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_protocol = htons(ETH_P_ALL);
addr.sll_pkttype = PACKET_HOST;
addr.sll_ifindex = if_nametoindex(gRadioInterfaceName_0);
if ((bind(gRawSock, (struct sockaddr*)&addr, sizeof(addr)))<0) {
CWDebugLog("THR FRAME: Error binding socket");
CWExitThread();
}
if (!getMacAddr(gRawSock, gRadioInterfaceName_0, macAddr)){
CWDebugLog("THR FRAME: Ioctl error");
EXIT_FRAME_THREAD(gRawSock);
}
/* Set the network card in promiscuos mode */
strncpy(ethreq.ifr_name,gRadioInterfaceName_0,IFNAMSIZ);
if (ioctl(gRawSock,SIOCGIFFLAGS,ðreq)==-1){
CWDebugLog("THR FRAME: Error ioctl");
EXIT_FRAME_THREAD(gRawSock);
}
ethreq.ifr_flags|=IFF_PROMISC;
if (ioctl(gRawSock,SIOCSIFFLAGS,ðreq)==-1){
CWDebugLog("THR FRAME: Error ioctl");
EXIT_FRAME_THREAD(gRawSock);
}
#ifdef FILTER_ON
/* Attach the filter to the socket */
if(setsockopt(gRawSock, SOL_SOCKET, SO_ATTACH_FILTER, &Filter, sizeof(Filter))<0)
{
CWDebugLog("THR FRAME: Error attaching filter");
EXIT_FRAME_THREAD(gRawSock);
}
#endif
CW_REPEAT_FOREVER{
n = recvfrom(gRawSock,buffer,sizeof(buffer),0,NULL,NULL);
if(n<0)continue;
if (!wtpInRunState){
CWLog("WTP is not in RUN state");
continue;
}
encaps_len = from_8023_to_80211(buffer, n, buf80211, macAddr);
if (!extract802_11_Frame(&frame, buf80211, encaps_len)){
CWDebugLog("THR FRAME: Error extracting a frame");
EXIT_FRAME_THREAD(gRawSock);
}
CWDebugLog("Recv 802.11 data(len:%d) from %s",encaps_len, gRadioInterfaceName_0);
CWBindingTransportHeaderValues *bindValues;
CW_CREATE_OBJECT_ERR(listElement, CWBindingDataListElement, EXIT_FRAME_THREAD(gRawSock););
listElement->frame = frame;
listElement->bindingValues = NULL;
listElement->frame->data_msgType = CW_IEEE_802_11_FRAME_TYPE;
CWLockSafeList(gFrameList);
CWAddElementToSafeListTail(gFrameList, listElement, sizeof(CWBindingDataListElement));
CWUnlockSafeList(gFrameList);
}
close(gRawSock);
return(NULL);
}