forked from vollero/openCAPWAP
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WTPJoinState.c
392 lines (323 loc) · 13.9 KB
/
WTPJoinState.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
/*******************************************************************************************
* 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. *
* *
* In addition, as a special exception, the copyright holders give permission to link the *
* code of portions of this program with the OpenSSL library under certain conditions as *
* described in each individual source file, and distribute linked combinations including *
* the two. You must obey the GNU General Public License in all respects for all of the *
* code used other than OpenSSL. If you modify file(s) with this exception, you may *
* extend this exception to your version of the file(s), but you are not obligated to do *
* so. If you do not wish to do so, delete this exception statement from your version. *
* If you delete this exception statement from all source files in the program, then also *
* delete it here. *
* *
* --------------------------------------------------------------------------------------- *
* 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 "CWWTP.h"
static CWBool gSuccessfulHandshake = CW_TRUE;
int gCWWaitJoin = CW_JOIN_INTERVAL_DEFAULT;
/*__________________________________________________________*/
/* *******************___PROTOTYPES___******************* */
static void CWWTPWaitJoinExpired(CWTimerArg arg);
static CWBool CWAssembleJoinRequest(CWTransportMessage *tm, int PMTU, int seqNum, CWList msgElemList);
static CWBool CWParseJoinResponseMessage(CWProtocolMessage *pm, int seqNum, void *values);
static CWBool CWSaveJoinResponseMessage(void *values);
/*_____________________________________________________*/
/* *******************___FUNCTIONS___******************* */
/*
* Manage Join State.
*/
CWStateTransition CWWTPEnterJoin()
{
CWTimerID waitJoinTimer;
int seqNum;
CWProtocolJoinResponseValues *values = NULL;
CWLog("\n");
CWLog("######### Join State #########");
/* reset Join state */
CWNetworkCloseSocket(gWTPSocket);
CWNetworkCloseSocket(gWTPDataSocket);
#ifndef CW_NO_DTLS
CWSecurityDestroySession(&gWTPSession);
CWSecurityDestroyContext(&gWTPSecurityContext);
#endif
cw_restart_join:
gSuccessfulHandshake = CW_TRUE;
ralloc_free(values);
if (!(values = rzalloc(NULL, CWProtocolJoinResponseValues)))
return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
/* Initialize gACInfoPtr */
gACInfoPtr->ACIPv4ListInfo.ACIPv4ListCount = 0;
gACInfoPtr->ACIPv4ListInfo.ACIPv4List = NULL;
gACInfoPtr->ACIPv6ListInfo.ACIPv6ListCount = 0;
gACInfoPtr->ACIPv6ListInfo.ACIPv6List = NULL;
if ((waitJoinTimer = timer_add(gCWWaitJoin, 0, CWWTPWaitJoinExpired, NULL)) == -1) {
ralloc_free(values);
return CW_ENTER_DISCOVERY;
}
if (gWTPForceACAddress != NULL) {
if (!(gACInfoPtr = ralloc(NULL, CWACInfoValues)))
return CWErrorRaise(CW_ERROR_OUT_OF_MEMORY, NULL);
CWNetworkGetAddressForHost(gWTPForceACAddress, &(gACInfoPtr->preferredAddress));
gACInfoPtr->security = gWTPForceSecurity;
}
/* Init DTLS session */
if (!CWErr(CWNetworkInitSocketClient(&gWTPSocket, &(gACInfoPtr->preferredAddress))))
goto cw_join_err;
if (!CWErr(CWNetworkInitSocketClientDataChannel(&gWTPDataSocket, &(gACInfoPtr->preferredAddress))))
goto cw_join_err;
CWLog("Initiate Data Channel");
CWDebugLog("gWTPSocket:%d, gWTPDataSocket:%d", gWTPSocket, gWTPDataSocket);
#ifndef CW_NO_DTLS
if (gACInfoPtr->security == CW_X509_CERTIFICATE) {
if (!CWErr(CWSecurityInitContext(&gWTPSecurityContext,
"root.pem", "client.pem", "prova", CW_TRUE, NULL)))
goto cw_join_err;
} else {
/* pre-shared keys */
if (!CWErr(CWSecurityInitContext(&gWTPSecurityContext, NULL, NULL, NULL, CW_TRUE, NULL)))
goto cw_join_err;
}
#endif
/* make sure we start with a fresh, empty list */
CWLockSafeList(gPacketReceiveList);
CWCleanSafeList(gPacketReceiveList, ralloc_free);
CWUnlockSafeList(gPacketReceiveList);
CWThread thread_receiveFrame;
if (!CWErr(CWCreateThread(&thread_receiveFrame, CWWTPReceiveDtlsPacket, (void *)(intptr_t)gWTPSocket))) {
CWLog("Error starting Thread that receive DTLS packet");
goto cw_join_err;
}
CWThread thread_receiveDataFrame;
if (!CWErr(CWCreateThread(&thread_receiveDataFrame, CWWTPReceiveDataPacket, (void *)(intptr_t)gWTPDataSocket))) {
CWLog("Error starting Thread that receive data packet");
goto cw_join_err;
}
#ifndef CW_NO_DTLS
if (!CWErr(CWSecurityInitSessionClient(gWTPSocket,
&(gACInfoPtr->preferredAddress),
gPacketReceiveList, gWTPSecurityContext, &gWTPSession, &gWTPPathMTU)))
goto cw_join_err;
#endif
if (gCWForceMTU > 0)
gWTPPathMTU = gCWForceMTU;
CWDebugLog("Path MTU for this Session: %d", gWTPPathMTU);
/* send Join Request */
seqNum = CWGetSeqNum();
if (!CWErr(CWWTPSendAcknowledgedPacket(seqNum,
NULL,
CWAssembleJoinRequest,
CWParseJoinResponseMessage,
CWSaveJoinResponseMessage, values)))
goto cw_join_err;
CWDebugLog("Join Handshake State: %d", gSuccessfulHandshake);
timer_rem(waitJoinTimer, NULL);
if (!gSuccessfulHandshake)
/* timer expired */
goto cw_join_err;
ralloc_free(values);
CWLog("Join Completed");
return CW_ENTER_CONFIGURE;
cw_join_err:
timer_rem(waitJoinTimer, NULL);
CWNetworkCloseSocket(gWTPSocket);
CWNetworkCloseSocket(gWTPDataSocket);
#ifndef CW_NO_DTLS
CWSecurityDestroySession(&gWTPSession);
CWSecurityDestroyContext(&gWTPSecurityContext);
#endif
if (CWWTPPickAC())
/* selected a new AC from the list */
goto cw_restart_join;
ralloc_free(values);
return CW_ENTER_DISCOVERY;
}
void CWWTPWaitJoinExpired(CWTimerArg arg)
{
CWLog("WTP Wait Join Expired");
CWDebugLog("gWTPSocket:%d, gWTPDataSocket:%d", gWTPSocket, gWTPDataSocket);
gSuccessfulHandshake = CW_FALSE;
CWNetworkCloseSocket(gWTPSocket);
CWNetworkCloseSocket(gWTPDataSocket);
}
CWBool CWAssembleJoinRequest(CWTransportMessage *tm, int PMTU, int seqNum, CWList msgElemList)
{
CWProtocolMessage msg;
assert(tm != NULL);
CWLog("Sending Join Request...");
/* Assemble Message Elements */
if (!CWInitMessage(NULL, &msg, CW_MSG_TYPE_VALUE_JOIN_REQUEST, seqNum) ||
!CWAssembleMsgElemLocationData(NULL, &msg) ||
!CWAssembleMsgElemWTPBoardData(NULL, &msg) ||
!CWAssembleMsgElemWTPDescriptor(NULL, &msg) ||
!CWAssembleMsgElemWTPIPv4Address(NULL, &msg) ||
!CWAssembleMsgElemWTPName(NULL, &msg) ||
!CWAssembleMsgElemSessionID(NULL, &msg, &gWTPSessionID[0]) ||
!CWAssembleMsgElemWTPFrameTunnelMode(NULL, &msg) ||
!CWAssembleMsgElemWTPMACType(NULL, &msg) ||
!CWAssembleMsgElemWTPRadioInformation(NULL, &msg))
goto cw_assemble_error;
CWFinalizeMessage(&msg);
if (!CWAssembleMessage(tm, PMTU, &msg))
goto cw_assemble_error;
return CW_TRUE;
cw_assemble_error:
CWReleaseMessage(&msg);
return CW_FALSE;
}
/*
* Parse Join Response and return informations in *valuesPtr.
*/
CWBool CWParseJoinResponseMessage(CWProtocolMessage *pm, int seqNum, void *values)
{
CWProtocolJoinResponseValues *jr = (CWProtocolJoinResponseValues *)values;
CWControlHeaderValues controlVal;
char tmp_ABGNTypes;
assert(pm != NULL);
assert(values != NULL);
CWDebugLog("Parsing Join Response...");
/* error will be handled by the caller */
if (!(CWParseControlHeader(pm, &controlVal)))
return CW_FALSE;
if (controlVal.messageTypeValue != CW_MSG_TYPE_VALUE_JOIN_RESPONSE)
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Message is not Join Response as Expected");
if (controlVal.seqNum != seqNum)
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Different Sequence Number");
/* skip timestamp */
controlVal.msgElemsLen -= CW_CONTROL_HEADER_OFFSET_FOR_MSG_ELEMS;
jr->ACInfoPtr.IPv4AddressesCount = 0;
jr->ACInfoPtr.IPv6AddressesCount = 0;
jr->ACIPv4ListInfo.ACIPv4ListCount = 0;
jr->ACIPv4ListInfo.ACIPv4List = NULL;
jr->ACIPv6ListInfo.ACIPv6ListCount = 0;
jr->ACIPv6ListInfo.ACIPv6List = NULL;
CWParseMessageElementStart(pm);
/* parse message elements */
CWParseMessageElementWhile(pm, controlVal.msgElemsLen) {
unsigned short int type = 0;
unsigned short int len = 0;
CWParseFormatMsgElem(pm, &type, &len);
CWDebugLog("Parsing Message Element: %u, len: %u", type, len);
switch (type) {
case CW_MSG_ELEMENT_AC_DESCRIPTOR_CW_TYPE:
/* will be handled by the caller */
if (!(CWParseACDescriptor(jr, pm, len, &(jr->ACInfoPtr))))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_IEEE80211_WTP_RADIO_INFORMATION_CW_TYPE:
/* will be handled by the caller */
if (!CWParseWTPRadioInformation_FromAC(pm, len, &tmp_ABGNTypes))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_AC_IPV4_LIST_CW_TYPE:
if (!(CWParseACIPv4List(jr, pm, len, &(jr->ACIPv4ListInfo))))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_AC_IPV6_LIST_CW_TYPE:
if (!(CWParseACIPv6List(jr, pm, len, &(jr->ACIPv6ListInfo))))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_RESULT_CODE_CW_TYPE:
if (!(CWParseResultCode(pm, len, &(jr->code))))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_AC_NAME_CW_TYPE:
if (!(CWParseACName(jr, pm, len, &(jr->ACInfoPtr.name))))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_CW_CONTROL_IPV4_ADDRESS_CW_TYPE:
if (!CWParseCWControlIPv4Addresses(jr, pm, len, &jr->ACInfoPtr))
return CW_FALSE;
break;
case CW_MSG_ELEMENT_CW_CONTROL_IPV6_ADDRESS_CW_TYPE:
if (!CWParseCWControlIPv6Addresses(jr, pm, len, &jr->ACInfoPtr))
return CW_FALSE;
break;
#if 0
case CW_MSG_ELEMENT_SESSION_ID_CW_TYPE:
if (!CWParseSessionID(pm, len, jr))
return CW_FALSE;
break;
#endif
default:
CWLog("unknown Message Element, Element; %u", type);
/* ignore unknown IE */
CWParseSkipElement(pm, len);
}
/* CWDebugLog("bytes: %d/%d", (pm.offset-offsetTillMessages), controlVal.msgElemsLen); */
}
CWParseMessageElementEnd(pm, controlVal.msgElemsLen);
return CWParseTransportMessageEnd(pm);
}
CWBool CWSaveJoinResponseMessage(void *values)
{
CWProtocolJoinResponseValues *jr = (CWProtocolJoinResponseValues *)values;
assert(values != NULL);
if ((jr->code == CW_PROTOCOL_SUCCESS) || (jr->code == CW_PROTOCOL_SUCCESS_NAT)) {
if (gACInfoPtr == NULL)
return CWErrorRaise(CW_ERROR_NEED_RESOURCE, NULL);
gACInfoPtr->stations = jr->ACInfoPtr.stations;
gACInfoPtr->limit = jr->ACInfoPtr.limit;
gACInfoPtr->activeWTPs = jr->ACInfoPtr.activeWTPs;
gACInfoPtr->maxWTPs = jr->ACInfoPtr.maxWTPs;
gACInfoPtr->security = jr->ACInfoPtr.security;
gACInfoPtr->RMACField = jr->ACInfoPtr.RMACField;
ralloc_steal(gACInfoPtr, jr->ACInfoPtr.vendorInfos.vendorInfos);
ralloc_free(gACInfoPtr->vendorInfos.vendorInfos);
gACInfoPtr->vendorInfos.vendorInfos = jr->ACInfoPtr.vendorInfos.vendorInfos;
if (jr->ACIPv4ListInfo.ACIPv4ListCount > 0) {
gACInfoPtr->ACIPv4ListInfo.ACIPv4ListCount =
jr->ACIPv4ListInfo.ACIPv4ListCount;
ralloc_steal(gACInfoPtr, jr->ACIPv4ListInfo.ACIPv4List);
ralloc_free(gACInfoPtr->ACIPv4ListInfo.ACIPv4List);
gACInfoPtr->ACIPv4ListInfo.ACIPv4List = jr->ACIPv4ListInfo.ACIPv4List;
}
if (jr->ACIPv6ListInfo.ACIPv6ListCount > 0) {
gACInfoPtr->ACIPv6ListInfo.ACIPv6ListCount =
jr->ACIPv6ListInfo.ACIPv6ListCount;
ralloc_steal(gACInfoPtr, jr->ACIPv6ListInfo.ACIPv6List);
ralloc_free(gACInfoPtr->ACIPv6ListInfo.ACIPv6List);
gACInfoPtr->ACIPv6ListInfo.ACIPv6List = jr->ACIPv6ListInfo.ACIPv6List;
}
/*
* This field name was allocated for storing the AC name; however, it
* doesn't seem to be used and it is certainly lost when we exit
* CWWTPEnterJoin() as v is actually a local variable of that
* function.
*
* Thus, it seems good to free it now.
*
* BUG ML03
* 16/10/2009 - Donato Capitella
*/
CW_FREE_OBJECT(jr->ACInfoPtr.name);
/* BUG ML08 */
CW_FREE_OBJECT(jr->ACInfoPtr.IPv4Addresses);
CWDebugLog("Join Response Saved");
return CW_TRUE;
} else {
CWDebugLog("Join Response said \"Failure\"");
return CW_FALSE;
}
}