-
Notifications
You must be signed in to change notification settings - Fork 39
/
ACJoinState.c
394 lines (323 loc) · 13.5 KB
/
ACJoinState.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*******************************************************************************************
* 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 ([email protected]) *
* Del Moro Andrea ([email protected]) *
* Giovannini Federica ([email protected]) *
* Massimo Vellucci ([email protected]) *
* Mauro Bisson ([email protected]) *
*******************************************************************************************/
#include "CWAC.h"
#ifdef DMALLOC
#include "../dmalloc-5.5.0/dmalloc.h"
#endif
CWBool CWAssembleJoinResponse(CWProtocolMessage ** messagesPtr,
int *fragmentsNumPtr, int PMTU, int seqNum, CWList msgElemList);
CWBool CWParseJoinRequestMessage(unsigned char *msg, int len, int *seqNumPtr, CWProtocolJoinRequestValues * valuesPtr);
CWBool CWSaveJoinRequestMessage(CWProtocolJoinRequestValues * joinRequest, CWWTPProtocolManager * WTPProtocolManager);
CWBool ACEnterJoin(int WTPIndex, CWProtocolMessage * msgPtr)
{
int seqNum;
CWProtocolJoinRequestValues joinRequest;
CWList msgElemList = NULL;
CWLog("\n");
CWLog("######### Join State #########");
if (msgPtr == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
if (!(CWParseJoinRequestMessage(msgPtr->msg, msgPtr->offset, &seqNum, &joinRequest))) {
/* note: we can kill our thread in case of out-of-memory
* error to free some space.
* we can see this just calling CWErrorGetLastErrorCode()
*/
return CW_FALSE;
}
// cancel waitJoin timer
if (!CWTimerCancel(&(gWTPs[WTPIndex].currentTimer))) {
return CW_FALSE;
}
CWBool ACIpv4List = CW_FALSE;
CWBool ACIpv6List = CW_FALSE;
CWBool resultCode = CW_TRUE;
int resultCodeValue = CW_PROTOCOL_SUCCESS;
/* CWBool sessionID = CW_FALSE; */
if (!(CWSaveJoinRequestMessage(&joinRequest, &(gWTPs[WTPIndex].WTPProtocolManager)))) {
resultCodeValue = CW_PROTOCOL_FAILURE_RES_DEPLETION;
}
CWMsgElemData *auxData;
if (ACIpv4List) {
CW_CREATE_OBJECT_ERR(auxData, CWMsgElemData, return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
);
auxData->type = CW_MSG_ELEMENT_AC_IPV4_LIST_CW_TYPE;
auxData->value = 0;
CWAddElementToList(&msgElemList, auxData);
}
if (ACIpv6List) {
CW_CREATE_OBJECT_ERR(auxData, CWMsgElemData, return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
);
auxData->type = CW_MSG_ELEMENT_AC_IPV6_LIST_CW_TYPE;
auxData->value = 0;
CWAddElementToList(&msgElemList, auxData);
}
if (resultCode) {
CW_CREATE_OBJECT_ERR(auxData, CWMsgElemData, return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
);
auxData->type = CW_MSG_ELEMENT_RESULT_CODE_CW_TYPE;
auxData->value = resultCodeValue;
CWAddElementToList(&msgElemList, auxData);
}
/*
if(sessionID){
CW_CREATE_OBJECT_ERR(auxData, CWMsgElemData, return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL););
auxData->type = CW_MSG_ELEMENT_SESSION_ID_CW_TYPE;
auxData->value = CWRandomIntInRange(0, INT_MAX);
CWAddElementToList(&msgElemList,auxData);
}
*/
/* random session ID */
if (!(CWAssembleJoinResponse(&(gWTPs[WTPIndex].messages),
&(gWTPs[WTPIndex].messagesCount), gWTPs[WTPIndex].pathMTU, seqNum, msgElemList))) {
CWDeleteList(&msgElemList, CWProtocolDestroyMsgElemData);
return CW_FALSE;
}
CWDeleteList(&msgElemList, CWProtocolDestroyMsgElemData);
if (!CWACSendFragments(WTPIndex)) {
return CW_FALSE;
}
gWTPs[WTPIndex].currentState = CW_ENTER_CONFIGURE;
return CW_TRUE;
}
/*
* Assemble Join Response.
*/
CWBool CWAssembleJoinResponse(CWProtocolMessage ** messagesPtr,
int *fragmentsNumPtr, int PMTU, int seqNum, CWList msgElemList)
{
CWProtocolMessage *msgElems = NULL;
int msgElemCount = 0;
/* Result code is not included because it's already
* in msgElemList. Control IPv6 to be added.
*/
const int mandatoryMsgElemCount = 4;
CWProtocolMessage *msgElemsBinding = NULL;
const int msgElemBindingCount = 0;
int i;
CWListElement *current;
int k = -1;
if (messagesPtr == NULL || fragmentsNumPtr == NULL || msgElemList == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
msgElemCount = CWCountElementInList(msgElemList);
CW_CREATE_PROTOCOL_MSG_ARRAY_ERR(msgElems,
msgElemCount + mandatoryMsgElemCount,
return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
);
CWDebugLog("Assembling Join Response...");
if ((!(CWAssembleMsgElemACDescriptor(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemACName(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemCWControlIPv4Addresses(&(msgElems[++k])))) ||
(!(CWAssembleMsgElemACWTPRadioInformation(&(msgElems[++k]))))
) {
CWErrorHandleLast();
int i;
for (i = 0; i <= k; i++) {
CW_FREE_PROTOCOL_MESSAGE(msgElems[i]);
}
CW_FREE_OBJECT(msgElems);
/* error will be handled by the caller */
return CW_FALSE;
}
current = msgElemList;
for (i = 0; i < msgElemCount; i++) {
switch (((CWMsgElemData *) (current->data))->type) {
case CW_MSG_ELEMENT_AC_IPV4_LIST_CW_TYPE:
if (!(CWAssembleMsgElemACIPv4List(&(msgElems[++k]))))
goto cw_assemble_error;
break;
case CW_MSG_ELEMENT_AC_IPV6_LIST_CW_TYPE:
if (!(CWAssembleMsgElemACIPv6List(&(msgElems[++k]))))
goto cw_assemble_error;
break;
case CW_MSG_ELEMENT_RESULT_CODE_CW_TYPE:
if (!(CWAssembleMsgElemResultCode(&(msgElems[++k]), ((CWMsgElemData *) current->data)->value)))
goto cw_assemble_error;
break;
/*
case CW_MSG_ELEMENT_SESSION_ID_CW_TYPE:
if (!(CWAssembleMsgElemSessionID(&(msgElems[++k]), ((CWMsgElemData *) current->data)->value)))
goto cw_assemble_error;
break;
*/
default:{
int j;
for (j = 0; j <= k; j++) {
CW_FREE_PROTOCOL_MESSAGE(msgElems[j]);
}
CW_FREE_OBJECT(msgElems);
return CWErrorRaise(CW_ERROR_INVALID_FORMAT,
"Unrecognized Message Element for Join Response Message");
break;
}
}
current = current->next;
}
if (!(CWAssembleMessage(messagesPtr,
fragmentsNumPtr,
PMTU,
seqNum,
CW_MSG_TYPE_VALUE_JOIN_RESPONSE,
msgElems, msgElemCount + mandatoryMsgElemCount, msgElemsBinding, msgElemBindingCount)))
return CW_FALSE;
CWDebugLog("Join Response Assembled");
return CW_TRUE;
cw_assemble_error:{
int i;
for (i = 0; i <= k; i++) {
CW_FREE_PROTOCOL_MESSAGE(msgElems[i]);
}
CW_FREE_OBJECT(msgElems);
/* error will be handled by the caller */
return CW_FALSE;
}
return CW_TRUE;
}
/*
* Parses Join Request.
*/
CWBool CWParseJoinRequestMessage(unsigned char *msg, int len, int *seqNumPtr, CWProtocolJoinRequestValues * valuesPtr)
{
CWControlHeaderValues controlVal;
int offsetTillMessages;
CWProtocolMessage completeMsg;
unsigned char RadioInfoABGN;
if (msg == NULL || seqNumPtr == NULL || valuesPtr == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
CWDebugLog("Parse Join Request");
completeMsg.msg = msg;
completeMsg.offset = 0;
if (!(CWParseControlHeader(&completeMsg, &controlVal)))
/* will be handled by the caller */
return CW_FALSE;
/* different type */
if (controlVal.messageTypeValue != CW_MSG_TYPE_VALUE_JOIN_REQUEST)
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Message is not Join Request as Expected");
*seqNumPtr = controlVal.seqNum;
/* skip timestamp */
controlVal.msgElemsLen -= CW_CONTROL_HEADER_OFFSET_FOR_MSG_ELEMS;
offsetTillMessages = completeMsg.offset;
/* parse message elements */
while ((completeMsg.offset - offsetTillMessages) < controlVal.msgElemsLen) {
unsigned short int elemType = 0; /* = CWProtocolRetrieve32(&completeMsg); */
unsigned short int elemLen = 0; /* = CWProtocolRetrieve16(&completeMsg); */
CWParseFormatMsgElem(&completeMsg, &elemType, &elemLen);
/* CWDebugLog("Parsing Message Element: %u, elemLen: %u", elemType, elemLen); */
switch (elemType) {
case CW_MSG_ELEMENT_LOCATION_DATA_CW_TYPE:
if (!(CWParseLocationData(&completeMsg, elemLen, &(valuesPtr->location))))
/* will be handled by the caller */
return CW_FALSE;
break;
case CW_MSG_ELEMENT_WTP_BOARD_DATA_CW_TYPE:
if (!(CWParseWTPBoardData(&completeMsg, elemLen, &(valuesPtr->WTPBoardData))))
/* will be handled by the caller */
return CW_FALSE;
break;
case CW_MSG_ELEMENT_SESSION_ID_CW_TYPE:
valuesPtr->sessionID = CWParseSessionID(&completeMsg, elemLen);
break;
case CW_MSG_ELEMENT_WTP_DESCRIPTOR_CW_TYPE:
if (!(CWParseWTPDescriptor(&completeMsg, elemLen, &(valuesPtr->WTPDescriptor))))
/* will be handled by the caller */
return CW_FALSE;
break;
case CW_MSG_ELEMENT_WTP_IPV4_ADDRESS_CW_TYPE:
if (!(CWParseWTPIPv4Address(&completeMsg, elemLen, valuesPtr)))
/* will be handled by the caller */
return CW_FALSE;
break;
case CW_MSG_ELEMENT_WTP_NAME_CW_TYPE:
if (!(CWParseWTPName(&completeMsg, elemLen, &(valuesPtr->name))))
/* will be handled by the caller */
return CW_FALSE;
break;
case CW_MSG_ELEMENT_WTP_FRAME_TUNNEL_MODE_CW_TYPE:
if (!(CWParseWTPFrameTunnelMode(&completeMsg, elemLen, &(valuesPtr->frameTunnelMode))))
/* will be handled by the caller */
return CW_FALSE;
break;
case CW_MSG_ELEMENT_WTP_MAC_TYPE_CW_TYPE:
if (!(CWParseWTPMACType(&completeMsg, elemLen, &(valuesPtr->MACType))))
/* will be handled by the caller */
return CW_FALSE;
break;
case CW_MSG_ELEMENT_IEEE80211_WTP_RADIO_INFORMATION_CW_TYPE:
if (!(CWParseWTPRadioInformation(&completeMsg, elemLen, &RadioInfoABGN)))
return CW_FALSE;
break;
default:
completeMsg.offset += elemLen;
CWLog("Unrecognized Message Element(%d) in Discovery response", elemType);
break;
}
/*CWDebugLog("bytes: %d/%d", (completeMsg.offset-offsetTillMessages), controlVal.msgElemsLen); */
}
if (completeMsg.offset != len)
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Garbage at the End of the Message");
return CW_TRUE;
}
CWBool CWSaveJoinRequestMessage(CWProtocolJoinRequestValues * joinRequest, CWWTPProtocolManager * WTPProtocolManager)
{
CWDebugLog("Saving Join Request...");
if (joinRequest == NULL || WTPProtocolManager == NULL)
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
if ((joinRequest->location) != NULL) {
CW_FREE_OBJECT(WTPProtocolManager->locationData);
WTPProtocolManager->locationData = joinRequest->location;
} else
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
if ((joinRequest->name) != NULL) {
CW_FREE_OBJECT(WTPProtocolManager->name);
WTPProtocolManager->name = joinRequest->name;
} else
return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL);
CW_FREE_OBJECT((WTPProtocolManager->WTPBoardData).vendorInfos);
WTPProtocolManager->WTPBoardData = joinRequest->WTPBoardData;
WTPProtocolManager->sessionID = joinRequest->sessionID;
WTPProtocolManager->ipv4Address = joinRequest->addr;
WTPProtocolManager->descriptor = joinRequest->WTPDescriptor;
WTPProtocolManager->radiosInfo.radioCount = (joinRequest->WTPDescriptor).radiosInUse;
CW_FREE_OBJECT(WTPProtocolManager->radiosInfo.radiosInfo);
CW_CREATE_ARRAY_ERR(WTPProtocolManager->radiosInfo.radiosInfo,
WTPProtocolManager->radiosInfo.radioCount,
CWWTPRadioInfoValues, return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
);
int i;
for (i = 0; i < WTPProtocolManager->radiosInfo.radioCount; i++) {
WTPProtocolManager->radiosInfo.radiosInfo[i].radioID = i;
/*WTPProtocolManager->radiosInfo.radiosInfo[i].stationCount = 0; */
/* default value for CAPWAP */
WTPProtocolManager->radiosInfo.radiosInfo[i].adminState = ENABLED;
WTPProtocolManager->radiosInfo.radiosInfo[i].adminCause = AD_NORMAL;
WTPProtocolManager->radiosInfo.radiosInfo[i].operationalState = DISABLED;
WTPProtocolManager->radiosInfo.radiosInfo[i].operationalCause = OP_NORMAL;
WTPProtocolManager->radiosInfo.radiosInfo[i].TxQueueLevel = 0;
WTPProtocolManager->radiosInfo.radiosInfo[i].wirelessLinkFramesPerSec = 0;
}
CWDebugLog("Join Request Saved");
return CW_TRUE;
}