forked from vollero/openCAPWAP
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WTPRunState.c
1467 lines (1159 loc) · 42.4 KB
/
WTPRunState.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************************************
* Copyright (c) 2006-2009 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 *
* *
* Authors : 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) *
* Antonio Davoli (antonio.davoli@gmail.com) *
************************************************************************************************/
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netpacket/packet.h>
#include "CWWTP.h"
#include "CWVendorPayloads.h"
#include "WTPipcHostapd.h"
#include "WTPmacFrameReceive.h"
#include "common.h"
#include "ieee802_11_defs.h"
static CWBool CWWTPManageGenericRunMessage(CWProtocolMessage *pm);
CWBool CWWTPCheckForWTPEventRequest();
#if 0
static CWBool CWParseWTPEventResponseMessage(unsigned char *msg, int len, int seqNum, void *values);
static CWBool CWSaveWTPEventResponseMessage(void *WTPEventResp);
#endif
static CWBool CWAssembleEchoRequest(CWTransportMessage *tm, int PMTU, int seqNum, CWList msgElemList);
static CWBool CWParseEchoResponse(CWProtocolMessage *pm, int len);
static CWBool CWParseConfigurationUpdateRequest(CWProtocolMessage *pm, int len,
CWProtocolConfigurationUpdateRequestValues * valuesPtr,
int *updateRequestType);
static CWBool CWSaveConfigurationUpdateRequest(CWProtocolConfigurationUpdateRequestValues * valuesPtr,
CWProtocolResultCode * resultCode, int *updateRequestType);
static CWBool CWAssembleConfigurationUpdateResponse(CWTransportMessage *tm, int PMTU, int seqNum, CWProtocolResultCode resultCode,
CWProtocolConfigurationUpdateRequestValues values);
static CWBool CWSaveClearConfigurationRequest(CWProtocolResultCode * resultCode);
static CWBool CWAssembleClearConfigurationResponse(CWTransportMessage *tm, int PMTU, int seqNum, CWProtocolResultCode resultCode);
static CWBool CWAssembleStationConfigurationResponse(CWTransportMessage *tm, int PMTU, int seqNum, CWProtocolResultCode resultCode);
static CWBool CWAssembleWLANConfigurationResponse(CWTransportMessage *tm, int PMTU, int seqNum, CWProtocolResultCode resultCode);
static CWBool CWParseStationConfigurationRequest(CWProtocolMessage *pm, int len);
static CWBool CWParseWLANConfigurationRequest(CWProtocolMessage *pm, int len);
static CWBool CWParseKeepAlivePacket(CWProtocolMessage *pm, int len);
static void CWWTPHeartBeatTimerExpiredHandler(void *arg);
static void CWWTPKeepAliveDataTimerExpiredHandler(void *arg);
static void CWWTPNeighborDeadTimerExpired(void *arg);
static CWBool CWResetHeartbeatTimer();
static CWBool CWStopDataChannelKeepAlive();
static CWBool CWResetDataChannelKeepAlive();
static CWBool CWResetNeighborDeadTimer();
CWTimerID gCWHeartBeatTimerID;
CWTimerID gCWKeepAliveTimerID;
CWTimerID gCWNeighborDeadTimerID;
CWBool gNeighborDeadTimerSet = CW_FALSE;
struct timeval gEchoLatency = {0,0};
/* record the state of the control and data channel keep alives */
static enum tRunChannelState {
CS_OK,
CS_FAILED,
CS_TIMEOUT
} lRunChannelState;
static void setRunChannelState(enum tRunChannelState newState);
/*
* set the gRunChannelState and notify the run thread
*/
static void setRunChannelState(enum tRunChannelState newState)
{
CWThreadMutexLock(&gInterfaceMutex);
lRunChannelState = newState;
CWSignalThreadCondition(&gInterfaceWait);
CWThreadMutexUnlock(&gInterfaceMutex);
}
/*
* Manage DTLS packets.
*/
CW_THREAD_RETURN_TYPE CWWTPReceiveDtlsPacket(void *arg)
{
int readBytes;
char buf[CW_BUFFER_SIZE];
CWSocket sockDTLS = (intptr_t)arg;
CWNetworkLev4Address addr;
char *pData;
CW_REPEAT_FOREVER {
CWDebugLog("CWWTPReceiveDtlsPacket: recvfrom on DtlsSocket %d", sockDTLS);
if (!CWErr(CWNetworkReceiveUnsafe(sockDTLS, buf, CW_BUFFER_SIZE - 1, 0, &addr, &readBytes))) {
CWDebugLog("CWWTPReceiveDtlsPacket Error: %d", CWErrorGetLastErrorCode());
if (CWErrorGetLastErrorCode() == CW_ERROR_INTERRUPTED)
continue;
break;
}
/* Clone data packet */
if (!(pData = ralloc_memdup(NULL, buf, readBytes))) {
CWLog("Out Of Memory");
return NULL;
}
CWLockSafeList(gPacketReceiveList);
CWAddElementToSafeListTailwitDataFlag(gPacketReceiveList, pData, readBytes, CW_FALSE);
CWUnlockSafeList(gPacketReceiveList);
if (readBytes == 0)
/* no error, but no data == orderly shutdown
* the zero lenght payload packet signals the BIO read to terminate */
break;
}
CWLog("CWWTPReceiveDtlsPacket:: exited");
return NULL;
}
extern int gRawSock;
/*
* Manage data packets.
*/
#define HLEN_80211 24
static
int isEAPOL_Frame(unsigned char *buf, unsigned int len)
{
static const unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
return (memcmp(rfc1042_header, buf + HLEN_80211, sizeof(rfc1042_header)) == 0);
}
CW_THREAD_RETURN_TYPE CWWTPReceiveDataPacket(void *arg)
{
int readBytes;
unsigned char buf[CW_BUFFER_SIZE];
struct sockaddr_ll rawSockaddr;
CWNetworkLev4Address addr;
CWProtocolMessage pm;
CWProtocolMessage msg;
CWFragmentBufferList frag_buffer;
memset(&rawSockaddr, 0, sizeof(rawSockaddr));
rawSockaddr.sll_family = AF_PACKET;
rawSockaddr.sll_protocol = htons(ETH_P_ALL);
rawSockaddr.sll_ifindex = if_nametoindex(gRadioInterfaceName_0);
rawSockaddr.sll_pkttype = PACKET_OTHERHOST;
rawSockaddr.sll_halen = ETH_ALEN;
CW_REPEAT_FOREVER {
if (!CWErr(CWNetworkReceiveUnsafe(gWTPDataSocket, buf, CW_BUFFER_SIZE, 0, &addr, &readBytes))) {
if (CWErrorGetLastErrorCode() == CW_ERROR_INTERRUPTED)
continue;
break;
}
if (readBytes == 0)
/* no error, but no data == orderly shutdown */
break;
CWInitTransportMessage(&msg, buf, readBytes, 1);
if (!CWProtocolParseFragment(&msg, &frag_buffer, &pm)) {
CWDebugErrorLog();
CWReleaseMessage(&msg);
continue;
}
CWProtocolTransportHeaderValues transportHeader;
unsigned char radioMAC[6];
CWResetNeighborDeadTimer();
CWParseTransportHeader(&pm, &transportHeader, radioMAC);
if (CWTransportHeaderIsKeepAlive(&pm)) {
unsigned int plen = CWProtocolLength(&pm);
unsigned short len = CWProtocolRetrieve16(&pm);
CWDebugLog("Got KeepAlive packet len %zd, payload %d from AC", pm.space, len);
if (len < 2 || len != plen)
CWLog("KeepAlive Element Malformed, invalid length %d, payload length %d", len, plen);
else
CWParseKeepAlivePacket(&pm, len - 2);
}
else switch (CWTransportBinding(&pm)) {
case BINDING_IEEE_802_3:
{
unsigned char *data = CWProtocolRetrievePtr(&pm);
unsigned int length = CWProtocolLength(&pm);
CWDebugLog("Got 802.3 len: %d from AC", length);
/*MAC - begin */
memcpy(&rawSockaddr.sll_addr, data, 6);
/*MAC - end */
rawSockaddr.sll_addr[6] = 0x00; /*not used */
rawSockaddr.sll_addr[7] = 0x00; /*not used */
rawSockaddr.sll_hatype = htons(data[12] << 8 | data[13]);
if (sendto(gRawSock, data, length, 0, (struct sockaddr *)&rawSockaddr,
sizeof(rawSockaddr)) < 0)
CWLog("Sending a data packet failed with: %s", strerror(errno));
break;
}
case BINDING_IEEE_802_11:
{
unsigned char *data = CWProtocolRetrievePtr(&pm);
unsigned int length = CWProtocolLength(&pm);
struct ieee80211_hdr *hdr;
u16 fc;
hdr = (struct ieee80211_hdr *)data;
fc = le_to_host16(hdr->frame_control);
if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT
|| isEAPOL_Frame(data, length))
{
CWDebugLog("Got 802.11 Management Packet (stype=%d) from AC(hostapd) len: %d",
WLAN_FC_GET_STYPE(fc), length);
CWWTPsend_data_to_hostapd(data, length);
}
else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_NULLFUNC) {
CWDebugLog("Got 802.11 Data Packet (stype=%d) from AC(hostapd) len: %d",
WLAN_FC_GET_STYPE(fc), length);
CWWTPsend_data_to_hostapd(data, length);
} else {
CWDebugLog("Got 802.11 Data Packet (stype=%d) from AC(hostapd) len: %d",
WLAN_FC_GET_STYPE(fc), length);
CWWTPSendFrame(data, length);
}
} else
CWLog("Control/Unknow Type type=%d", WLAN_FC_GET_TYPE(fc));
break;
}
default:
CWLog("Unknow transport binding");
break;
}
CWReleaseMessage(&pm);
}
CWLog("CWWTPReceiveDataPacket:: exited");
return NULL;
}
/*
* Manage Run State.
*/
extern int gRawSock;
int wtpInRunState = 0;
CWStateTransition CWWTPEnterRun()
{
int k;
struct timespec timenow;
CWLog("\n");
CWLog("######### WTP enters in RUN State #########");
lRunChannelState = CS_OK;
gDataChannelKeepAliveInterval = gAggressiveDataChannelKeepAliveInterval;
CWWTPKeepAliveDataTimerExpiredHandler(NULL);
for (k = 0; k < MAX_PENDING_REQUEST_MSGS; k++)
CWResetPendingMsgBox(gPendingRequestMsgs + k);
if (!CWErr(CWStartHeartbeatTimer()))
return CW_ENTER_RESET;
if (!CWErr(CWStartNeighborDeadTimer()))
return CW_ENTER_RESET;
/* Wait packet */
timenow.tv_sec = time(0) + gCWNeighborDeadInterval + gCWNeighborDeadRestartDelta; /* greater than NeighborDeadInterval */
timenow.tv_nsec = 0;
wtpInRunState = 1;
CW_REPEAT_FOREVER {
CWBool bReceivePacket = CW_FALSE;
CWBool bReveiveBinding = CW_FALSE;
CWThreadMutexLock(&gInterfaceMutex);
if (lRunChannelState != CS_OK) {
CWDebugLog("WTP Channel State set to not OK (%d)", lRunChannelState);
CWThreadMutexUnlock(&gInterfaceMutex);
break;
}
/*
* if there are no frames from stations
* and no packets from AC...
*/
if ((CWGetCountElementFromSafeList(gPacketReceiveList) == 0)
&& (CWGetCountElementFromSafeList(gFrameList) == 0)) {
/*
* ...wait at most 4 mins for a frame or packet.
*/
if (!CWErr(CWWaitThreadConditionTimeout(&gInterfaceWait, &gInterfaceMutex, &timenow))) {
CWThreadMutexUnlock(&gInterfaceMutex);
if (CWErrorGetLastErrorCode() == CW_ERROR_TIME_EXPIRED) {
CWLog("No Message from AC for a long time... restart Discovery State");
break;
}
continue;
}
}
bReceivePacket = ((CWGetCountElementFromSafeList(gPacketReceiveList) != 0) ? CW_TRUE : CW_FALSE);
bReveiveBinding = ((CWGetCountElementFromSafeList(gFrameList) != 0) ? CW_TRUE : CW_FALSE);
CWThreadMutexUnlock(&gInterfaceMutex);
if (bReceivePacket) {
CWProtocolMessage pm;
CWProtocolTransportHeaderValues transportHeader;
/*
msg.data = NULL;
msg.space = msg.pos = 0;
*/
if (!(CWReceiveMessage(&pm))) {
CWReleaseMessage(&pm);
CWLog("Failure Receiving Response");
break;
}
CWParseTransportHeader(&pm, &transportHeader, NULL);
if (!CWErr(CWWTPManageGenericRunMessage(&pm))) {
if (CWErrorGetLastErrorCode() == CW_ERROR_INVALID_FORMAT) {
/* Log and ignore message */
CWErrorHandleLast();
CWLog("--> Received something different from a valid Run Message");
} else {
CWReleaseMessage(&pm);
CWLog("--> Critical Error Managing Generic Run Message... we enter RESET State");
break;
}
}
/* Wait packet */
timenow.tv_sec = time(0) + gCWNeighborDeadInterval + gCWNeighborDeadRestartDelta; /* greater than NeighborDeadInterval */
timenow.tv_nsec = 0;
}
if (bReveiveBinding)
CWWTPCheckForBindingFrame();
}
wtpInRunState = 0;
CWStopHeartbeatTimer();
CWStopDataChannelKeepAlive();
CWStopNeighborDeadTimer();
CWNetworkCloseSocket(gWTPSocket);
CWNetworkCloseSocket(gWTPDataSocket);
#ifndef CW_NO_DTLS
CWSecurityDestroySession(&gWTPSession);
CWSecurityDestroyContext(&gWTPSecurityContext);
#endif
/* shutdown wifi before leaving RUN state */
unsigned char dummy_ssid[3] = {0,};
CWWTPsend_command_to_hostapd_DEL_WLAN(dummy_ssid, sizeof(dummy_ssid));
return CW_ENTER_RESET;
}
CWBool CWWTPManageGenericRunMessage(CWProtocolMessage *pm)
{
CWControlHeaderValues controlVal;
assert(pm != NULL);
/* TODO:
* check to see if a time-out on session occure...
* In case it happens it should go back to CW_ENTER_RESET
*/
if (!CWResetHeartbeatTimer())
return CW_FALSE;
/* will be handled by the caller */
if (!(CWParseControlHeader(pm, &controlVal)))
return CW_FALSE;
int len = controlVal.msgElemsLen - CW_CONTROL_HEADER_OFFSET_FOR_MSG_ELEMS;
int pendingMsgIndex = 0;
pendingMsgIndex = CWFindPendingRequestMsgsBox(gPendingRequestMsgs,
MAX_PENDING_REQUEST_MSGS,
controlVal.messageTypeValue, controlVal.seqNum);
/* we have received a new Request or an Echo Response */
if (pendingMsgIndex < 0) {
CWTransportMessage tm;
CW_ZERO_MEMORY(&tm, sizeof(tm));
switch (controlVal.messageTypeValue) {
case CW_MSG_TYPE_VALUE_CONFIGURE_UPDATE_REQUEST:
{
CWProtocolResultCode resultCode = CW_PROTOCOL_FAILURE;
CWProtocolConfigurationUpdateRequestValues values;
int updateRequestType;
CWLog("Configuration Update Request received");
/* assume AC has gone to Run state, reset Data Channel Keep Alive */
gDataChannelKeepAliveInterval = gConfigDataChannelKeepAliveInterval;
if (!CWParseConfigurationUpdateRequest(pm, len, &values, &updateRequestType))
return CW_FALSE;
if (!CWSaveConfigurationUpdateRequest(&values, &resultCode, &updateRequestType))
return CW_FALSE;
/*
if ( updateRequestType == BINDING_MSG_ELEMENT_TYPE_OFDM_CONTROL )
break;
*/
if (!CWAssembleConfigurationUpdateResponse(&tm, gWTPPathMTU, controlVal.seqNum, resultCode, values))
return CW_FALSE;
break;
}
case CW_MSG_TYPE_VALUE_CLEAR_CONFIGURATION_REQUEST:
{
CWProtocolResultCode resultCode = CW_PROTOCOL_FAILURE;
CWLog("Clear Configuration Request received");
/*WTP RESET ITS CONFIGURAION TO MANUFACTURING DEFAULT} */
if (!CWSaveClearConfigurationRequest(&resultCode))
return CW_FALSE;
if (!CWAssembleClearConfigurationResponse(&tm, gWTPPathMTU, controlVal.seqNum, resultCode))
return CW_FALSE;
break;
}
case CW_MSG_TYPE_VALUE_STATION_CONFIGURATION_REQUEST:
{
CWProtocolResultCode resultCode = CW_PROTOCOL_SUCCESS;
//CWProtocolStationConfigurationRequestValues values; --> da implementare
CWLog("Station Configuration Request received");
if (!CWParseStationConfigurationRequest(pm, len))
return CW_FALSE;
if (!CWAssembleStationConfigurationResponse(&tm, gWTPPathMTU, controlVal.seqNum, resultCode))
return CW_FALSE;
break;
}
case CW_MSG_TYPE_VALUE_WLAN_CONFIGURATION_REQUEST:
{
CWProtocolResultCode resultCode = CW_PROTOCOL_SUCCESS;
CWLog("WLAN Configuration Request received");
/* assume AC has gone to Run state, reset Data Channel Keep Alive */
gDataChannelKeepAliveInterval = gConfigDataChannelKeepAliveInterval;
if (!CWParseWLANConfigurationRequest(pm, len))
return CW_FALSE;
if (!CWAssembleWLANConfigurationResponse(&tm, gWTPPathMTU, controlVal.seqNum, resultCode))
return CW_FALSE;
break;
}
case CW_MSG_TYPE_VALUE_ECHO_RESPONSE:
CWLog("Echo Response received");
if (!CWParseEchoResponse(pm, len))
return CW_FALSE;
break;
default:
/*
* We can't recognize the received Request so
* we have to send a corresponding response
* containing a failure result code
*/
CWLog("--> invalid Request %d (0x%04x) in Run State... we send a failure Response",
controlVal.messageTypeValue, controlVal.messageTypeValue);
if (!CWAssembleUnrecognizedMessageResponse(&tm, gWTPPathMTU, controlVal.seqNum, controlVal.messageTypeValue + 1))
return CW_FALSE;
/* return CWErrorRaise(CW_ERROR_INVALID_FORMAT,
* "Received Message not valid in Run State");
*/
}
if (tm.count > 0) {
int i;
for (i = 0; i < tm.count; i++) {
#ifdef CW_NO_DTLS
if (!CWNetworkSendUnsafeConnected(gWTPSocket, tm.parts[i].data, tm.parts[i].pos))
#else
if (!CWSecuritySend(gWTPSession, tm.parts[i].data, tm.parts[i].pos))
#endif
{
CWLog("Error sending message");
CWReleaseTransportMessage(&tm);
return CW_FALSE;
}
}
CWLog("Message Sent\n");
CWReleaseTransportMessage(&tm);
/*
* Check if we have to exit due to an update commit request.
*/
if (WTPExitOnUpdateCommit)
exit(EXIT_SUCCESS);
}
} else { /* we have received a Response */
switch (controlVal.messageTypeValue) {
case CW_MSG_TYPE_VALUE_CHANGE_STATE_EVENT_RESPONSE:
CWLog("Change State Event Response received");
break;
case CW_MSG_TYPE_VALUE_WTP_EVENT_RESPONSE:
CWLog("WTP Event Response received");
break;
case CW_MSG_TYPE_VALUE_DATA_TRANSFER_RESPONSE:
CWLog("Data Transfer Response received");
break;
default:
/*
* We can't recognize the received Response: we
* ignore the message and log the event.
*/
return CWErrorRaise(CW_ERROR_INVALID_FORMAT, "Received Message not valid in Run State");
}
CWResetPendingMsgBox(gPendingRequestMsgs + pendingMsgIndex);
}
CWReleaseMessage(pm);
return CW_TRUE;
}
/*______________________________________________________________*/
/* *******************___TIMER HANDLERS___******************* */
void CWWTPHeartBeatTimerExpiredHandler(void *arg)
{
CWList msgElemList = NULL;
CWTransportMessage tm;
int seqNum;
CWLog("WTP HeartBeat Timer Expired... we send an ECHO Request");
CWLog("\n");
CWLog("#________ Echo Request Message (Run) ________#");
/* Send WTP Event Request */
seqNum = CWGetSeqNum();
if (!CWAssembleEchoRequest(&tm, gWTPPathMTU, seqNum, msgElemList)) {
CWDebugLog("Failure Assembling Echo Request");
CWReleaseTransportMessage(&tm);
return;
}
int i;
for (i = 0; i < tm.count; i++) {
#ifdef CW_NO_DTLS
if (!CWNetworkSendUnsafeConnected(gWTPSocket, tm.parts[i].data, tm.parts[i].pos))
#else
if (!CWSecuritySend(gWTPSession, tm.parts[i].data, tm.parts[i].pos))
#endif
{
CWLog("Failure sending Request");
CWReleaseTransportMessage(&tm);
break;
}
}
CWReleaseTransportMessage(&tm);
if (!CWStartHeartbeatTimer())
return;
}
void CWWTPKeepAliveDataTimerExpiredHandler(void *arg)
{
int k;
CWTransportMessage tm;
CWProtocolMessage sessionIDmsgElem;
CWLog("WTP KeepAliveDataTimer Expired... we send an Data Channel Keep-Alive");
CWLog("\n");
CWLog("#________ Keep-Alive Message (Run) ________#");
if (!CWResetDataChannelKeepAlive()) {
setRunChannelState(CS_FAILED);
return;
}
CW_ZERO_MEMORY(&sessionIDmsgElem, sizeof(CWProtocolMessage));
CWAssembleMsgElemSessionID(NULL, &sessionIDmsgElem, &gWTPSessionID[0]);
/* Send WTP Event Request */
if (!CWAssembleDataMessage(&tm, gWTPPathMTU, 1, BINDING_IEEE_802_3, CW_TRUE, CW_FALSE, NULL, NULL, &sessionIDmsgElem)) {
CWDebugLog("Failure Assembling KeepAlive Message");
CWReleaseMessage(&sessionIDmsgElem);
setRunChannelState(CS_FAILED);
return;
}
for (k = 0; k < tm.count; k++) {
if (!CWNetworkSendUnsafeConnected(gWTPDataSocket, tm.parts[k].data, tm.parts[k].pos)) {
CWLog("Failure sending KeepAlive Message");
setRunChannelState(CS_FAILED);
break;
}
}
CWReleaseTransportMessage(&tm);
}
void CWWTPNeighborDeadTimerExpired(void *arg)
{
CWLog("WTP NeighborDead Timer Expired... we consider Peer Dead.");
setRunChannelState(CS_TIMEOUT);
}
CWBool CWStartHeartbeatTimer()
{
gCWHeartBeatTimerID = timer_add(gEchoInterval, 0, &CWWTPHeartBeatTimerExpiredHandler, NULL);
if (gCWHeartBeatTimerID == -1)
return CW_FALSE;
CWDebugLog("Echo Heartbeat Timer Started with %d seconds", gEchoInterval);
return CW_TRUE;
}
CWBool CWStopHeartbeatTimer()
{
timer_rem(gCWHeartBeatTimerID, NULL);
CWDebugLog("Echo Heartbeat Timer Stopped");
return CW_TRUE;
}
CWBool CWResetHeartbeatTimer()
{
timer_rem(gCWHeartBeatTimerID, NULL);
gCWHeartBeatTimerID = timer_add(gEchoInterval, 0, &CWWTPHeartBeatTimerExpiredHandler, NULL);
if (gCWHeartBeatTimerID == -1)
return CW_FALSE;
CWDebugLog("Echo Heartbeat Timer Reset with %d seconds", gEchoInterval);
return CW_TRUE;
}
CWBool CWStopDataChannelKeepAlive()
{
timer_rem(gCWKeepAliveTimerID, NULL);
CWDebugLog("DataChannelKeepAlive Timer Stopped");
return CW_TRUE;
}
CWBool CWResetDataChannelKeepAlive()
{
timer_rem(gCWKeepAliveTimerID, NULL);
gCWKeepAliveTimerID = timer_add(gDataChannelKeepAliveInterval, 0, &CWWTPKeepAliveDataTimerExpiredHandler, NULL);
if (gCWKeepAliveTimerID == -1)
return CW_FALSE;
CWDebugLog("DataChannelKeepAlive Timer Reset with %d seconds", gDataChannelKeepAliveInterval);
return CW_TRUE;
}
CWBool CWStartNeighborDeadTimer()
{
gCWNeighborDeadTimerID = timer_add(gCWNeighborDeadInterval, 0, &CWWTPNeighborDeadTimerExpired, NULL);
if (gCWNeighborDeadTimerID == -1)
return CW_FALSE;
CWDebugLog("NeighborDead Timer Started with %d seconds", gCWNeighborDeadInterval);
gNeighborDeadTimerSet = CW_TRUE;
return CW_TRUE;
}
CWBool CWStopNeighborDeadTimer()
{
timer_rem(gCWNeighborDeadTimerID, NULL);
CWDebugLog("NeighborDead Timer Stopped");
gNeighborDeadTimerSet = CW_FALSE;
return CW_TRUE;
}
CWBool CWResetNeighborDeadTimer()
{
timer_rem(gCWNeighborDeadTimerID, NULL);
gCWNeighborDeadTimerID = timer_add(gCWNeighborDeadInterval, 0, &CWWTPNeighborDeadTimerExpired, NULL);
if (gCWNeighborDeadTimerID == -1)
return CW_FALSE;
CWDebugLog("NeighborDead Timer Reset with %d seconds", gCWNeighborDeadInterval);
gNeighborDeadTimerSet = CW_FALSE;
return CW_TRUE;
}
/*__________________________________________________________________*/
/* *******************___ASSEMBLE FUNCTIONS___******************* */
CWBool CWAssembleEchoRequest(CWTransportMessage *tm, int PMTU, int seqNum, CWList msgElemList)
{
struct timeval tv;
CWProtocolMessage msg;
assert(tm != NULL);
gettimeofday(&tv, NULL);
CWLog("Assembling Echo Request...");
if (!CWInitMessage(NULL, &msg, CW_MSG_TYPE_VALUE_ECHO_REQUEST, seqNum) ||
!CWAssembleMsgElemVendorTPWTPTimestamp(NULL, &msg, &tv))
goto cw_assemble_error;
CWFinalizeMessage(&msg);
if (!CWAssembleMessage(tm, PMTU, &msg))
goto cw_assemble_error;
CWLog("Echo Request Assembled");
return CW_TRUE;
cw_assemble_error:
CWReleaseMessage(&msg);
return CW_FALSE;
}
CWBool CWAssembleWTPDataTransferRequest(CWTransportMessage *tm, int PMTU, int seqNum, CWList msgElemList)
{
CWProtocolMessage msg;
CWListElement *current;
assert(tm != NULL);
assert(msgElemList != NULL);
CWLog("Assembling WTP Data Transfer Request...");
if (!CWInitMessage(NULL, &msg, CW_MSG_TYPE_VALUE_DATA_TRANSFER_REQUEST, seqNum))
goto cw_assemble_error;
CWListForeach(msgElemList, current) {
switch (((CWMsgElemData *) current->data)->type) {
case CW_MSG_ELEMENT_DATA_TRANSFER_DATA_CW_TYPE:
if (!CWAssembleMsgElemDataTransferData
(NULL, &msg, ((CWMsgElemData *) current->data)->value))
goto cw_assemble_error;
break;
#if 0
case CW_MSG_ELEMENT_DATA_TRANSFER_MODE_CW_TYPE:
if (!CWAssembleMsgElemDataTansferMode(NULL, &msg))
goto cw_assemble_error;
break;
#endif
default:
goto cw_assemble_error;
break;
}
}
CWFinalizeMessage(&msg);
if (!CWAssembleMessage(tm, PMTU, &msg))
goto cw_assemble_error;
CWLog("WTP Data Transfer Request Assembled");
return CW_TRUE;
cw_assemble_error:
CWReleaseMessage(&msg);
return CW_FALSE; // error will be handled by the caller
}
CWBool CWAssembleWTPEventRequest(CWTransportMessage *tm, int PMTU, int seqNum, CWList msgElemList)
{
CWProtocolMessage msg;
CWListElement *current;
assert(tm != NULL);
assert(msgElemList != NULL);
CWLog("Assembling WTP Event Request...");
if (!CWInitMessage(NULL, &msg, CW_MSG_TYPE_VALUE_WTP_EVENT_REQUEST, seqNum))
goto cw_assemble_error;
CWListForeach(msgElemList, current) {
switch (((CWMsgElemData *) current->data)->type) {
case CW_MSG_ELEMENT_CW_DECRYPT_ER_REPORT_CW_TYPE:
if (!CWAssembleMsgElemDecryptErrorReport
(NULL, &msg, ((CWMsgElemData *) current->data)->value))
goto cw_assemble_error;
break;
case CW_MSG_ELEMENT_DUPLICATE_IPV4_ADDRESS_CW_TYPE:
if (!CWAssembleMsgElemDuplicateIPv4Address(NULL, &msg))
goto cw_assemble_error;
break;
case CW_MSG_ELEMENT_DUPLICATE_IPV6_ADDRESS_CW_TYPE:
if (!CWAssembleMsgElemDuplicateIPv6Address(NULL, &msg))
goto cw_assemble_error;
break;
case CW_MSG_ELEMENT_WTP_OPERAT_STATISTICS_CW_TYPE:
if (!CWAssembleMsgElemWTPOperationalStatistics
(NULL, &msg, ((CWMsgElemData *) current->data)->value))
goto cw_assemble_error;
break;
case CW_MSG_ELEMENT_WTP_RADIO_STATISTICS_CW_TYPE:
if (!CWAssembleMsgElemWTPRadioStatistics
(NULL, &msg, ((CWMsgElemData *) current->data)->value))
goto cw_assemble_error;
break;
case CW_MSG_ELEMENT_WTP_REBOOT_STATISTICS_CW_TYPE:
if (!CWAssembleMsgElemWTPRebootStatistics(NULL, &msg))
goto cw_assemble_error;
break;
default:
goto cw_assemble_error;
break;
}
}
CWFinalizeMessage(&msg);
if (!CWAssembleMessage(tm, PMTU, &msg))
goto cw_assemble_error;
CWLog("WTP Event Request Assembled");
return CW_TRUE;
cw_assemble_error:
CWReleaseMessage(&msg);
return CW_FALSE;
}
CWBool CWAssembleConfigurationUpdateResponse(CWTransportMessage *tm, int PMTU, int seqNum,
CWProtocolResultCode resultCode,
CWProtocolConfigurationUpdateRequestValues values)
{
CWProtocolMessage msg;
CWProtocolVendorSpecificValues *protoValues = NULL;
assert(tm != NULL);
/*Get protocol data if we have it */
if (values.protocolValues)
protoValues = (CWProtocolVendorSpecificValues *) values.protocolValues;
CWLog("Assembling Configuration Update Response...");
if (!CWInitMessage(NULL, &msg, CW_MSG_TYPE_VALUE_CONFIGURE_UPDATE_RESPONSE, seqNum))
goto cw_assemble_error;
if (protoValues) {
switch (protoValues->vendorPayloadType) {
case CW_MSG_ELEMENT_VENDOR_SPEC_PAYLOAD_UCI:
case CW_MSG_ELEMENT_VENDOR_SPEC_PAYLOAD_WUM:
if (!CWAssembleVendorMsgElemResultCodeWithPayload(NULL, &msg, resultCode, protoValues))
goto cw_assemble_error;
break;
default:
/*Result Code only */
if (!CWAssembleMsgElemResultCode(NULL, &msg, resultCode))
goto cw_assemble_error;
break;
}
} else {
/*Result Code only */
if (!CWAssembleMsgElemResultCode(NULL, &msg, resultCode))
goto cw_assemble_error;
}
CWFinalizeMessage(&msg);
if (!CWAssembleMessage(tm, PMTU, &msg))
goto cw_assemble_error;
CWLog("Configuration Update Response Assembled");
return CW_TRUE;
cw_assemble_error:
CWReleaseMessage(&msg);
return CW_FALSE;
}
CWBool CWAssembleClearConfigurationResponse(CWTransportMessage *tm, int PMTU,
int seqNum, CWProtocolResultCode resultCode)
{
CWProtocolMessage msg;
assert(tm != NULL);
CWLog("Assembling Clear Configuration Response...");
if (!CWInitMessage(NULL, &msg, CW_MSG_TYPE_VALUE_CLEAR_CONFIGURATION_RESPONSE, seqNum) ||
!CWAssembleMsgElemResultCode(NULL, &msg, resultCode))
goto cw_assemble_error;
CWFinalizeMessage(&msg);
if (!CWAssembleMessage(tm, PMTU, &msg))
goto cw_assemble_error;
CWLog("Clear Configuration Response Assembled");
return CW_TRUE;
cw_assemble_error:
CWReleaseMessage(&msg);
return CW_FALSE;
}
CWBool CWAssembleStationConfigurationResponse(CWTransportMessage *tm, int PMTU,
int seqNum, CWProtocolResultCode resultCode)
{
CWProtocolMessage msg;
assert(tm != NULL);
CWLog("Assembling Sattion Configuration Response...");
if (!CWInitMessage(NULL, &msg, CW_MSG_TYPE_VALUE_STATION_CONFIGURATION_RESPONSE, seqNum) ||
!CWAssembleMsgElemResultCode(NULL, &msg, resultCode))
goto cw_assemble_error;
CWFinalizeMessage(&msg);
if (!CWAssembleMessage(tm, PMTU, &msg))
goto cw_assemble_error;
CWLog("Station Configuration Response Assembled");
return CW_TRUE;
cw_assemble_error:
CWReleaseMessage(&msg);
return CW_FALSE;
}
CWBool CWAssembleWLANConfigurationResponse(CWTransportMessage *tm, int PMTU, int seqNum,
CWProtocolResultCode resultCode)
{
CWProtocolMessage msg;