-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwpal.c
2222 lines (1862 loc) · 69.3 KB
/
dwpal.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
/* SPDX-License-Identifier: BSD-2-Clause-Patent
*
* Copyright (c) 2016-2019 Intel Corporation
*
* This code is subject to the terms of the BSD+Patent license.
* See LICENSE file for more details.
*/
/* *****************************************************************************
* File Name : dwpal.c *
* Description : D-WPAL control interface *
* *
* *****************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <linux/types.h>
#include <netlink/socket.h>
#include <netlink/genl/genl.h> /* for "struct nl_msg" */
#include <netlink/genl/ctrl.h>
#include <linux/netlink.h>
#include <net/if.h>
#if defined YOCTO
#include <slibc/string.h>
#else
#include "safe_str_lib.h"
#endif
#include "dwpal.h"
#include "dwpal_log.h" //Logging
#define DWPAL_MAX_NUM_OF_ELEMENTS 512
#ifndef SOL_NETLINK
#define SOL_NETLINK 270
#endif
#ifndef NETLINK_EXT_ACK
#define NETLINK_EXT_ACK 11
#endif
#define OUI_LTQ 0xAC9A96
typedef struct
{
union
{
struct
{
char VAPName[DWPAL_VAP_NAME_STRING_LENGTH]; /* "wlan0", "wlan0.1", "wlan1", "wlan2.2", ..., "wlan5", ... */
char operationMode[DWPAL_OPERATING_MODE_STRING_LENGTH];
char wpaCtrlName[DWPAL_WPA_CTRL_STRING_LENGTH];
struct wpa_ctrl *wpaCtrlPtr;
struct wpa_ctrl *listenerWpaCtrlPtr; /*needed when closing it*/
int fd;
DWPAL_wpaCtrlEventCallback wpaCtrlEventCallback; /* callback function for hostapd received events while command is being sent; can be NULL */
} hostapd;
struct
{
struct nl_sock *nlSocketEvent, *nlSocketCmdGet;
int fd, fdCmdGet, nl80211_id;
DWPAL_nlEventCallback nlEventCallback, nlCmdGetCallback;
} driver;
} interface;
} DWPAL_Context;
/* Local static functions */
static int no_seq_check(struct nl_msg *msg, void *arg)
{
(void)msg;
(void)arg;
return NL_OK;
}
static DWPAL_Ret command_get_ended_msg_send(void)
{
int fd = -1, byte;
struct sockaddr_un un;
size_t len;
char socketName[SOCKET_NAME_LENGTH] = "\0";
pid_t pid = getpid();
console_printf("%s Entry\n", __FUNCTION__);
/* create a UNIX domain stream socket */
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
console_printf("%s; create socket fail; pid= %d; errno= %d ('%s')\n", __FUNCTION__, pid, errno, strerror(errno));
return DWPAL_FAILURE;
}
/* fill socket address structure with server's address */
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
snprintf(socketName, sizeof(socketName) - 1, "%s_%d", COMMAND_ENDED_SOCKET, pid);
strcpy_s(un.sun_path, SOCKET_NAME_LENGTH, socketName);
len = offsetof(struct sockaddr_un, sun_path) + strnlen_s(socketName, sizeof(socketName));
if (connect(fd, (struct sockaddr *)&un, len) < 0)
{
console_printf("%s; connect() fail; pid= %d; errno= %d ('%s')\n",
__FUNCTION__, pid, errno, strerror(errno));
if (close(fd) == (-1))
{
console_printf("%s; close() fail; pid= %d; errno= %d ('%s')\n",
__FUNCTION__, pid, errno, strerror(errno));
}
return DWPAL_FAILURE;
}
if ((byte = write(fd, NULL, 0)) == -1)
{
console_printf("%s; write() fail; pid= %d; errno= %d ('%s')\n",
__FUNCTION__, pid, errno, strerror(errno));
if (close(fd) == (-1))
{
console_printf("%s; close() fail; pid= %d; errno= %d ('%s')\n",
__FUNCTION__, pid, errno, strerror(errno));
}
return DWPAL_FAILURE;
}
if (close(fd) == (-1))
{
console_printf("%s; close() fail; pid= %d; errno= %d ('%s')\n",
__FUNCTION__, pid, errno, strerror(errno));
}
return DWPAL_SUCCESS;
}
static DWPAL_Ret nlInternalNlCallback(DWPAL_NlEventType nlEventType, struct nl_msg *msg, void *arg)
{
DWPAL_Context *localContext = (DWPAL_Context *)(arg);
struct nlattr *attr;
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct nlattr *tb[NL80211_ATTR_MAX + 1];
unsigned char *data;
int len, vendor_subcmd = -1;
char ifname[DWPAL_VAP_NAME_STRING_LENGTH] = "\0";
console_printf("%s Entry; nlEventType= %d (DWPAL_NL_EVENT_GET=0, DWPAL_NL_CMD_GET=1)\n", __FUNCTION__, nlEventType);
if ( (nlEventType == DWPAL_NL_EVENT_GET) && (localContext->interface.driver.nlEventCallback == NULL) )
{
console_printf("%s; 'DWPAL_NL_EVENT_GET' and nlEventCallback=NULL ==> exit\n", __FUNCTION__);
return (int)DWPAL_SUCCESS;
}
nla_parse(tb,
NL80211_ATTR_MAX,
genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0),
NULL);
attr = nla_find(genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0),
NL80211_ATTR_VENDOR_DATA);
if (!attr)
{
console_printf("%s; vendor data attribute missing ==> Abort!\n", __FUNCTION__);
return (int)DWPAL_FAILURE;
}
data = (unsigned char *)nla_data(attr);
len = nla_len(attr);
if ( (gnlh->cmd == NL80211_CMD_VENDOR) && (tb[NL80211_ATTR_VENDOR_SUBCMD] != NULL) )
{
vendor_subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
}
if (tb[NL80211_ATTR_IFINDEX] != NULL)
{
if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
}
if (nlEventType == DWPAL_NL_CMD_GET)
{
/* Call the NL 'get command' callback function */
localContext->interface.driver.nlCmdGetCallback(ifname, gnlh->cmd, vendor_subcmd, (size_t)len, data);
console_printf("%s; 'get command' received ==> notify dwpal_ext\n", __FUNCTION__);
if (command_get_ended_msg_send() == DWPAL_FAILURE)
{
console_printf("%s; command_get_ended_msg_send failed ==> cont...\n", __FUNCTION__);
}
}
else if (nlEventType == DWPAL_NL_EVENT_GET)
{
/* Call the NL callback function */
localContext->interface.driver.nlEventCallback(ifname, gnlh->cmd, vendor_subcmd, (size_t)len, data);
}
else
{
console_printf("%s; invalid nlEventType (%d) ==> Abort!\n", __FUNCTION__, nlEventType);
return DWPAL_FAILURE;
}
return (int)DWPAL_SUCCESS;
}
static int nlInternalCmdGetCallback(struct nl_msg *msg, void *arg)
{
console_printf("%s Entry\n", __FUNCTION__);
if (nlInternalNlCallback(DWPAL_NL_CMD_GET, msg, arg) == DWPAL_FAILURE)
{
console_printf("%s; nlInternalNlCallback ERROR ==> Abort!\n", __FUNCTION__);
return (int)DWPAL_FAILURE;
}
return (int)DWPAL_SUCCESS;
}
static int nlInternalEventCallback(struct nl_msg *msg, void *arg)
{
console_printf("%s Entry\n", __FUNCTION__);
if (nlInternalNlCallback(DWPAL_NL_EVENT_GET, msg, arg) == DWPAL_FAILURE)
{
console_printf("%s; nlInternalNlCallback ERROR ==> Abort!\n", __FUNCTION__);
return (int)DWPAL_FAILURE;
}
return (int)DWPAL_SUCCESS;
}
static DWPAL_Ret nlSocketCreate(struct nl_sock **nlSocket, int *fd)
{
int res = 1;
*nlSocket = nl_socket_alloc();
if (*nlSocket == NULL)
{
console_printf("%s; nl_socket_alloc ERROR ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
/* Connect to generic netlink socket on kernel side */
if (genl_connect(*nlSocket) < 0)
{
console_printf("%s; genl_connect ERROR ==> Abort!\n", __FUNCTION__);
nl_socket_free(*nlSocket);
return DWPAL_FAILURE;
}
if (nl_socket_set_buffer_size(*nlSocket, 8192, 8192) != 0)
{
console_printf("%s; nl_socket_set_buffer_size ERROR ==> Abort!\n", __FUNCTION__);
nl_socket_free(*nlSocket);
return DWPAL_FAILURE;
}
*fd = nl_socket_get_fd(*nlSocket);
if (*fd == -1)
{
console_printf("%s; nl_socket_get_fd ERROR ==> Abort!\n", __FUNCTION__);
nl_socket_free(*nlSocket);
return DWPAL_FAILURE;
}
console_printf("%s; driver.fd= %d\n", __FUNCTION__, *fd);
/* manipulate options for the socket referred to by the file descriptor - driver.fd */
setsockopt(*fd, SOL_NETLINK /*option level argument*/,
NETLINK_EXT_ACK, &res, sizeof(res));
return DWPAL_SUCCESS;
}
static bool mandatoryFieldValueGet(char *buf, size_t *bufLen, char **p2str, int totalSizeOfArg, char fieldValue[] /*OUT*/)
{
char *param = strtok_s(buf, bufLen, " ", p2str);
if (param == NULL)
{
console_printf("%s; param is NULL ==> Abort!\n", __FUNCTION__);
return false;
}
if (fieldValue != NULL)
{
if (strnlen_s(param, HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH) > (size_t)(totalSizeOfArg - 1))
{
console_printf("%s; param ('%s') length (%d) is higher than allocated size (%d) ==> Abort!\n", __FUNCTION__, param, strnlen_s(param, totalSizeOfArg), totalSizeOfArg-1);
return false;
}
strcpy_s(fieldValue, strnlen_s(param, HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH) + 1, param);
}
return true;
}
static bool arrayValuesGet(char *stringOfValues, size_t totalSizeOfArg, ParamParsingType paramParsingType, size_t *numOfValidArgs /*OUT*/, void *array /*OUT*/)
{
/* fill in the output array with list of integer elements (from decimal/hex base), for example:
"SupportedRates=2 4 11 22 12 18 24 36 48 72 96 108" or "HT_MCS=FF FF FF 00 00 00 00 00 00 00 C2 01 01 00 00 00"
also, in case of "DWPAL_STR_ARRAY_PARAM", handle multiple repetitive field, for example:
"... non_pref_chan=81:200:1:5 non_pref_chan=81:100:2:9 non_pref_chan=81:200:1:7 non_pref_chan=81:100:2:5 ..." or
"... non_pref_chan=81:200:1:5 81:100:2:9 81:200:1:7 81:100:2:5 ..." */
int idx = 0;
char *p2str, *param, *tokenString;
rsize_t dmaxLen = strnlen_s(stringOfValues, DWPAL_TO_HOSTAPD_MSG_LENGTH);
tokenString = stringOfValues;
do
{
param = strtok_s(tokenString, &dmaxLen, " ", &p2str);
if (param == NULL)
{
((int *)array)[idx] = 0;
break;
}
if (idx < (int)totalSizeOfArg)
{
if (numOfValidArgs != NULL)
{
(*numOfValidArgs)++;
}
if (paramParsingType == DWPAL_INT_HEX_ARRAY_PARAM)
{
((int *)array)[idx] = strtol(param, NULL, 16);
}
else if (paramParsingType == DWPAL_INT_ARRAY_PARAM)
{
((int *)array)[idx] = atoi(param);
}
else if (paramParsingType == DWPAL_STR_ARRAY_PARAM)
{
strcpy_s(&(((char *)array)[idx * HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH]), strnlen_s(param, HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH) + 1, param);
}
}
tokenString = NULL;
idx++;
} while (idx < DWPAL_MAX_NUM_OF_ELEMENTS); /* allow up to 512 elements per field (array) */
if (idx >= (int)totalSizeOfArg)
{
console_printf("%s; actual number of arguments (%d) is bigger/equal then totalSizeOfArg (%d) ==> Abort!\n", __FUNCTION__, idx, totalSizeOfArg);
return false;
}
return true;
}
static bool fieldValuesGet(char *buf, size_t bufLen, const char *stringToSearch, char *endFieldName[], char *stringOfValues /*OUT*/)
{
/* handles list of fields, one by one in the same row, for example: "... btm_supported=1 ..." or
"... SupportedRates=2 4 11 22 12 18 24 36 48 72 96 108 ..." */
char *stringStart, *stringEnd, *restOfStringStart, *closerStringEnd = NULL;
char *localBuf = NULL;
char *localStringToSearch = NULL;
int i, idx=0, numOfCharacters = 0, numOfCharactersToCopy = 0;
bool isFirstEndOfString = true, ret = false;
char tempStringOfValues[HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH], localEndFieldName[HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH];
localBuf = (char *)malloc(bufLen + 2 /* '\0' & 'blank' */);
if (localBuf == NULL)
{
console_printf("%s; malloc failed ==> Abort!\n", __FUNCTION__);
return false;
}
/* Add ' ' at the beginning of a string - to handle a case in which the buf starts with the
value of stringToSearch, like buf= 'candidate=d8:fe:e3:3e:bd:14,2178,83,5,7,255 candidate=...' */
snprintf(localBuf, bufLen + 2, " %s", buf);
/* localStringToSearch set to stringToSearch with addition of " " at the beginning -
it is a MUST in order to differentiate between "ssid" and "bssid" */
localStringToSearch = (char *)malloc(strnlen_s(stringToSearch, DWPAL_FIELD_NAME_LENGTH) + 2 /*'\0' & 'blank' */);
if (localStringToSearch == NULL)
{
console_printf("%s; localStringToSearch is NULL ==> Abort!\n", __FUNCTION__);
free((void *)localBuf);
return false;
}
snprintf(localStringToSearch, DWPAL_FIELD_NAME_LENGTH, " %s", stringToSearch);
restOfStringStart = localBuf;
while ( (stringStart = strstr(restOfStringStart, localStringToSearch)) != NULL )
{
ret = true; /* mark that at least one fiels was found */
/* move the string pointer to the beginning of the field's value */
restOfStringStart = stringStart + strnlen_s(localStringToSearch, HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH);
//console_printf("%s; stringStart= 0x%x, strlen of ('%s')= %d ==> restOfStringStart= 0x%x\n",
//__FUNCTION__, (unsigned int)stringStart, localStringToSearch, strnlen_s(localStringToSearch, HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH), (unsigned int)restOfStringStart);
/* find all beginning of all other fields (and get the closest to the current field) in order to know where the field's value ends */
i = 0;
while (strncmp(endFieldName[i], "\n", 1))
{ /* run over all field names in the string */
snprintf(localEndFieldName, HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH, " %s", endFieldName[i]); /* in order to differentiate between VHT_MCS and HT_MCS */
stringEnd = strstr(restOfStringStart, localEndFieldName);
if (stringEnd != NULL)
{
stringEnd++; /* move one character ahead due to the ' ' at the beginning of localEndFieldName */
//console_printf("%s; localEndFieldName= '%s' FOUND! (i= %d)\n", __FUNCTION__, localEndFieldName, i);
if (isFirstEndOfString)
{
isFirstEndOfString = false;
closerStringEnd = stringEnd;
}
else
{ /* Make sure that closerStringEnd will point to the closest field ahead */
closerStringEnd = (stringEnd < closerStringEnd)? stringEnd : closerStringEnd;
}
//console_printf("%s; [0] closerStringEnd= 0x%x\n", __FUNCTION__, (unsigned int)closerStringEnd);
}
i++;
}
//console_printf("%s; [1] closerStringEnd= 0x%x\n", __FUNCTION__, (unsigned int)closerStringEnd);
if (closerStringEnd == NULL)
{ /* Meaning, this is the last parameter in the string */
//console_printf("%s; closerStringEnd is NULL; restOfStringStart= '%s'\n", __FUNCTION__, restOfStringStart);
closerStringEnd = restOfStringStart + strnlen_s(restOfStringStart, HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH) + 1 /* for '\0' */;
//console_printf("%s; [2] closerStringEnd= 0x%x\n", __FUNCTION__, (unsigned int)closerStringEnd);
//console_printf("%s; String end did NOT found ==> set closerStringEnd to the end of buf; closerStringEnd= 0x%x\n", __FUNCTION__, (unsigned int)closerStringEnd);
}
//console_printf("%s; stringToSearch= '%s'; restOfStringStart= '%s'; buf= '%s'\n", __FUNCTION__, stringToSearch, restOfStringStart, buf);
//console_printf("%s; restOfStringStart= 0x%x, closerStringEnd= 0x%x ==> characters to copy = %d\n", __FUNCTION__, (unsigned int)restOfStringStart, (unsigned int)closerStringEnd, closerStringEnd - restOfStringStart);
/* set 'numOfCharacters' with the number of characters to copy (including the blank or end-of-string at the end) */
numOfCharacters = closerStringEnd - restOfStringStart;
if (numOfCharacters <= 0)
{
console_printf("%s; numOfCharacters= %d ==> Abort!\n", __FUNCTION__, numOfCharacters);
free((void *)localBuf);
free((void *)localStringToSearch);
return false;
}
/* Copy the characters of the value, and set the last one to '\0' */
strncpy_s(tempStringOfValues, sizeof(tempStringOfValues), restOfStringStart, numOfCharacters);
tempStringOfValues[numOfCharacters - 1] = '\0';
//console_printf("%s; stringToSearch= '%s'; tempStringOfValues= '%s'\n", __FUNCTION__, stringToSearch, tempStringOfValues);
/* Check if all elements are valid; if an element contains "=", it is NOT valid ==> do NOT copy it! */
for (i=0; i < numOfCharacters; i++)
{
if ( (tempStringOfValues[i] == ' ') || (tempStringOfValues[i] == '\0') )
{
numOfCharactersToCopy = i + 1 /* convert index to number-of */;
}
else if (tempStringOfValues[i] == '=')
{
break;
}
}
strncpy(&stringOfValues[idx], restOfStringStart, numOfCharactersToCopy);
idx += numOfCharactersToCopy;
stringOfValues[idx] = '\0';
//console_printf("%s; stringToSearch= '%s'; stringOfValues= '%s'\n", __FUNCTION__, stringToSearch, stringOfValues);
closerStringEnd = NULL;
}
/* Remove all ' ' from the end of the string */
for (i= idx-1; i > 0; i--)
{
if (stringOfValues[i] != ' ')
{
break; /* Stop removing the ' ' characters when the first non-blank character was found! */
}
else if (stringOfValues[i] == ' ')
{ /* stringOfValues[i] == ' ' */
stringOfValues[i] = '\0';
}
}
//console_printf("%s; stringToSearch= '%s'; stringOfValues= '%s'\n", __FUNCTION__, stringToSearch, stringOfValues);
free((void *)localBuf);
free((void *)localStringToSearch);
//console_printf("%s; ret= %d, stringToSearch= '%s'; stringOfValues= '%s'\n", __FUNCTION__, ret, stringToSearch, stringOfValues);
return ret;
}
static bool isColumnOfFields(char *msg, char *endFieldName[])
{
int i = 0, numOfFieldsInLine = 0;
//console_printf("%s; line= '%s'\n", __FUNCTION__, msg);
if (endFieldName == NULL)
{
console_printf("%s; endFieldName= 'NULL' ==> not a column!\n", __FUNCTION__);
return false;
}
while (strncmp(endFieldName[i], "\n", 1))
{ /* run over all field names in the string */
if (strstr(msg, endFieldName[i]) != NULL)
{
numOfFieldsInLine++;
if (numOfFieldsInLine > 1)
{
//console_printf("%s; Not a column (numOfFieldsInLine= %d) ==> return!\n", __FUNCTION__, numOfFieldsInLine);
return false;
}
/* Move ahead inside the line, to avoid double recognition (like "PacketsSent" and "DiscardPacketsSent") */
msg += strnlen_s(endFieldName[i], HOSTAPD_TO_DWPAL_MSG_LENGTH);
}
i++;
}
//console_printf("%s; It is a column (numOfFieldsInLine= %d)\n", __FUNCTION__, numOfFieldsInLine);
return true;
}
static bool columnOfParamsToRowConvert(char *msg, size_t msgLen, char *endFieldName[])
{
char *localMsg = strdup(msg), *lineMsg, *p2str;
rsize_t dmaxLen = (rsize_t)msgLen;
bool isColumn = true;
int i;
if (localMsg == NULL)
{
console_printf("%s; strdup error ==> Abort!\n", __FUNCTION__);
return false;
}
lineMsg = strtok_s(localMsg, (rsize_t *)&dmaxLen, "\n", &p2str);
while (lineMsg != NULL)
{
isColumn = isColumnOfFields(lineMsg, endFieldName);
if (isColumn == false)
{
//console_printf("%s; Not a column ==> break!\n", __FUNCTION__);
break;
}
lineMsg = strtok_s(NULL, (rsize_t *)&dmaxLen, "\n", &p2str);
}
free ((void *)localMsg);
if (isColumn)
{
/* Modify the column string to be in ONE raw */
for (i=0; i < (int)msgLen; i++)
{
if (msg[i] == '\n')
{
msg[i] = ' ';
}
}
msg[msgLen] = '\0';
}
return true;
}
/* Low Level APIs */
DWPAL_Ret dwpal_driver_nl_cmd_send(void *context,
DWPAL_NlEventType nlEventType,
char *ifname,
enum nl80211_commands nl80211Command,
CmdIdType cmdIdType,
enum ltq_nl80211_vendor_subcmds subCommand,
unsigned char *vendorData,
size_t vendorDataSize)
{
int i, res;
struct nl_msg *msg;
DWPAL_Context *localContext = (DWPAL_Context *)(context);
signed long long devidx = 0;
struct nl_sock *nlSocket = NULL;
console_printf("%s Entry\n", __FUNCTION__);
if (nl80211Command != NL80211_CMD_VENDOR /*0x67*/)
{
console_printf("%s; non supported command (0x%x); currently we support ONLY NL80211_CMD_VENDOR (0x67) ==> Abort!\n", __FUNCTION__, (unsigned int)nl80211Command);
return DWPAL_FAILURE;
}
for (i=0; i < (int)vendorDataSize; i++)
{
console_printf("%s; vendorData[%d]= 0x%x\n", __FUNCTION__, i, vendorData[i]);
}
if (nlEventType == DWPAL_NL_EVENT_GET)
{
nlSocket = localContext->interface.driver.nlSocketEvent;
}
else if (nlEventType == DWPAL_NL_CMD_GET)
{
nlSocket = localContext->interface.driver.nlSocketCmdGet;
}
else
{
console_printf("%s; invalid nlEventType (%d) ==> Abort!\n", __FUNCTION__, nlEventType);
return DWPAL_FAILURE;
}
if (localContext == NULL)
{
console_printf("%s; context is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
if (nlSocket == NULL)
{
console_printf("%s; nlSocket is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
msg = nlmsg_alloc();
if (msg == NULL)
{
console_printf("%s; nlmsg_alloc returned NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
console_printf("%s; nl80211_id= %d\n", __FUNCTION__, localContext->interface.driver.nl80211_id);
/* calling genlmsg_put() is a must! without it, the callback won't be called! */
genlmsg_put(msg, 0, 0, localContext->interface.driver.nl80211_id, 0,0, nl80211Command /* NL80211_CMD_VENDOR=0x67*/, 0);
//iw dev wlan0 vendor recv 0xAC9A96 0x69 0x00 ==> send "0xAC9A96 0x69 0x00"
devidx = if_nametoindex(ifname);
if (devidx < 0)
{
console_printf("%s; devidx ERROR (devidx= %lld) ==> Abort!\n", __FUNCTION__, devidx);
nlmsg_free(msg);
return DWPAL_FAILURE;
}
switch (cmdIdType)
{
case DWPAL_NETDEV_ID:
res = nla_put_u32(msg, NL80211_ATTR_IFINDEX, devidx);
//NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
break;
case DWPAL_PHY_ID:
res = nla_put_u32(msg, NL80211_ATTR_WIPHY, devidx);
//NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
break;
case DWPAL_WDEV_ID:
res = nla_put_u64(msg, NL80211_ATTR_WDEV, devidx);
//NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
break;
default:
console_printf("%s; cmdIdType ERROR (cmdIdType= %d) ==> Abort!\n", __FUNCTION__, cmdIdType);
nlmsg_free(msg);
return DWPAL_FAILURE;
}
if (res < 0)
{
console_printf("%s; building message failed ==> Abort!\n", __FUNCTION__);
nlmsg_free(msg);
return DWPAL_FAILURE;
}
res = nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_LTQ /*0xAC9A96*/);
//NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_LTQ /*0xAC9A96*/);
if (res < 0)
{
console_printf("%s; building message failed ==> Abort!\n", __FUNCTION__);
nlmsg_free(msg);
return DWPAL_FAILURE;
}
res = nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subCommand);
//NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subCommand);
if (res < 0)
{
console_printf("%s; building message failed ==> Abort!\n", __FUNCTION__);
nlmsg_free(msg);
return DWPAL_FAILURE;
}
if ( (vendorDataSize > 0) && (vendorData != NULL) )
{
//NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, count, buf);
res = nla_put(msg, NL80211_ATTR_VENDOR_DATA, (int)vendorDataSize, (void *)vendorData);
if (res < 0)
{
console_printf("%s; building message failed ==> Abort!\n", __FUNCTION__);
nlmsg_free(msg);
return DWPAL_FAILURE;
}
}
/* will trigger nlEventCallback() function call */
res = nl_send_auto(nlSocket, msg); // can use nl_send_auto_complete(nlSocket, msg) instead
if (res < 0)
{
console_printf("%s; nl_send_auto returned ERROR (res= %d) ==> Abort!\n", __FUNCTION__, res);
nlmsg_free(msg);
return DWPAL_FAILURE;
}
nlmsg_free(msg);
return DWPAL_SUCCESS;
}
DWPAL_Ret dwpal_driver_nl_msg_get(void *context, DWPAL_NlEventType nlEventType, DWPAL_nlEventCallback nlEventCallback)
{
int res;
struct nl_cb *cb;
DWPAL_Context *localContext = (DWPAL_Context *)(context);
console_printf("%s Entry; nlEventType= %d (DWPAL_NL_EVENT_GET=0, DWPAL_NL_CMD_GET=1)\n", __FUNCTION__, nlEventType);
if (localContext == NULL)
{
console_printf("%s; localContext is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
if ( ( (nlEventType == DWPAL_NL_EVENT_GET) && (localContext->interface.driver.nlSocketEvent == NULL) ) ||
( (nlEventType == DWPAL_NL_CMD_GET) && (localContext->interface.driver.nlSocketCmdGet == NULL) ) )
{
console_printf("%s; nlSocket is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
/* Connect the nl socket to its message callback function */
cb = nl_cb_alloc(NL_CB_DEFAULT);
if (cb == NULL)
{
console_printf("%s; failed to allocate netlink callbacks ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
if (nlEventType == DWPAL_NL_EVENT_GET)
{
/* nlEventCallback can be NULL; in that case, the D-WPAL client's callback function won't be called */
localContext->interface.driver.nlEventCallback = nlEventCallback;
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nlInternalEventCallback, context);
/* will trigger nlEventCallback() function call */
res = nl_recvmsgs(localContext->interface.driver.nlSocketEvent, cb);
}
else if (nlEventType == DWPAL_NL_CMD_GET)
{
/* nlEventCallback can be NULL; in that case, the D-WPAL client's callback function won't be called */
localContext->interface.driver.nlCmdGetCallback = nlEventCallback;
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nlInternalCmdGetCallback, context);
/* will trigger nlEventCallback() function call */
res = nl_recvmsgs(localContext->interface.driver.nlSocketCmdGet, cb);
}
else
{
console_printf("%s; invalid nlEventType (%d) ==> Abort!\n", __FUNCTION__, nlEventType);
return DWPAL_FAILURE;
}
if (res < 0)
{
console_printf("%s; nl_recvmsgs_default returned ERROR (res= %d) ==> Abort!\n", __FUNCTION__, res);
return DWPAL_FAILURE;
}
return DWPAL_SUCCESS;
}
DWPAL_Ret dwpal_driver_nl_fd_get(void *context, int *fd /*OUT*/, int *fdCmdGet /*OUT*/)
{
if ( (context == NULL) || (fd == NULL) || (fdCmdGet == NULL) )
{
console_printf("%s; context and/or fd, fdCmdGet is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
*fd = ((DWPAL_Context *)context)->interface.driver.fd;
if (*fd == (-1))
{
console_printf("%s; fd value is (-1) ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
*fdCmdGet = ((DWPAL_Context *)context)->interface.driver.fdCmdGet;
if (*fdCmdGet == (-1))
{
console_printf("%s; fdCmdGet value is (-1) ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
return DWPAL_SUCCESS;
}
DWPAL_Ret dwpal_driver_nl_detach(void **context /*IN/OUT*/)
{
DWPAL_Context *localContext;
if (context == NULL)
{
console_printf("%s; context is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
localContext = (DWPAL_Context *)(*context);
if (localContext == NULL)
{
console_printf("%s; context is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
if (localContext->interface.driver.nlSocketEvent == NULL)
{
console_printf("%s; nlSocketEvent is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
/* Note: calling nl_close() is NOT needed - The socket is closed automatically when using nl_socket_free() */
nl_socket_free(localContext->interface.driver.nlSocketEvent);
nl_socket_free(localContext->interface.driver.nlSocketCmdGet);
localContext->interface.driver.nlSocketEvent = NULL;
localContext->interface.driver.nlSocketCmdGet = NULL;
localContext->interface.driver.fd = -1;
localContext->interface.driver.fdCmdGet = -1;
localContext->interface.driver.nlEventCallback = NULL;
localContext->interface.driver.nlCmdGetCallback = NULL;
*context = NULL;
return DWPAL_SUCCESS;
}
DWPAL_Ret dwpal_driver_nl_attach(void **context /*OUT*/)
{
int mcid;
DWPAL_Context *localContext;
console_printf("%s Entry\n", __FUNCTION__);
if (context == NULL)
{
console_printf("%s; context is NULL ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
*context = malloc(sizeof(DWPAL_Context));
if (*context == NULL)
{
console_printf("%s; malloc for context failed ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
localContext = (DWPAL_Context *)(*context);
/* Create the NL socket for the events (unsolicited events) */
if (nlSocketCreate(&localContext->interface.driver.nlSocketEvent, &localContext->interface.driver.fd) == DWPAL_FAILURE)
{
console_printf("%s; nlSocketCreate failed ==> Abort!\n", __FUNCTION__);
free(*context);
*context = NULL;
return DWPAL_FAILURE;
}
mcid = genl_ctrl_resolve_grp(localContext->interface.driver.nlSocketEvent, "nl80211", "vendor");
console_printf("%s; mcid= %d\n", __FUNCTION__, mcid);
if (nl_socket_add_membership(localContext->interface.driver.nlSocketEvent, mcid) < 0)
{
console_printf("%s; nl_socket_add_membership ERROR ==> Abort!\n", __FUNCTION__);
free(*context);
*context = NULL;
return DWPAL_FAILURE;
}
/* Create the NL socket for the 'get commands' (solicited events) */
if (nlSocketCreate(&localContext->interface.driver.nlSocketCmdGet, &localContext->interface.driver.fdCmdGet) == DWPAL_FAILURE)
{
console_printf("%s; nlSocketCreate failed ==> Abort!\n", __FUNCTION__);
free(*context);
*context = NULL;
return DWPAL_FAILURE;
}
/* Ask kernel to resolve nl80211_id name to nl80211_id id; nl80211_id is being used only for command send */
localContext->interface.driver.nl80211_id = genl_ctrl_resolve(localContext->interface.driver.nlSocketCmdGet, "nl80211");
if (localContext->interface.driver.nl80211_id < 0)
{
console_printf("%s; genl_ctrl_resolve ERROR ==> Abort!\n", __FUNCTION__);
nl_socket_free(localContext->interface.driver.nlSocketEvent);
nl_socket_free(localContext->interface.driver.nlSocketCmdGet);
free(*context);
*context = NULL;
return DWPAL_FAILURE;
}
console_printf("%s; driver.nl80211_id= %d\n", __FUNCTION__, localContext->interface.driver.nl80211_id);
console_printf("%s; driver.nlSocketEvent= 0x%x, fd= %d, driver.nlEventCallback= 0x%x, driver.nl80211_id= %d; nlSocketCmdGet= 0x%x, fdCmdGet= %d\n",
__FUNCTION__, (unsigned int)localContext->interface.driver.nlSocketEvent, localContext->interface.driver.fd,
(unsigned int)localContext->interface.driver.nlEventCallback, localContext->interface.driver.nl80211_id,
(unsigned int)localContext->interface.driver.nlSocketCmdGet, localContext->interface.driver.fdCmdGet);
return DWPAL_SUCCESS;
}
DWPAL_Ret dwpal_string_to_struct_parse(char *msg, size_t msgLen, FieldsToParse fieldsToParse[])
{
DWPAL_Ret ret = DWPAL_SUCCESS;
int i = 0, idx = 0, numOfNameArrayArgs = 0, lineIdx = 0;
bool isEndFieldNameAllocated = false, isMissingParam = false;
char stringOfValues[HOSTAPD_TO_DWPAL_VALUE_STRING_LENGTH];
char *lineMsg, *localMsg, *p2str = NULL, *p2strMandatory = NULL;
rsize_t dmaxLen, dmaxLenMandatory;
size_t sizeOfStruct = 0, msgStringLen;
char **endFieldName = NULL;
if ( (msg == NULL) || (msgLen == 0) || (fieldsToParse == NULL) )
{
console_printf("%s; input params error ==> Abort!\n", __FUNCTION__);
return DWPAL_FAILURE;
}
if ( (msgStringLen = strnlen_s(msg, HOSTAPD_TO_DWPAL_MSG_LENGTH)) > msgLen )
{
console_printf("%s; msgStringLen (%d) is bigger than msgLen (%d) ==> Abort!\n", __FUNCTION__, msgStringLen, msgLen);
return DWPAL_FAILURE;
}
//console_printf("%s; [0] msgLen= %d\n", __FUNCTION__, msgLen);
/* Convert msgLen to string length format (without the '\0' character) */
msgLen = dmaxLen = msgStringLen;
//console_printf("%s; [1] msgLen= %d\n", __FUNCTION__, msgLen);
//console_printf("%s; sizeOfStruct= %d\n", __FUNCTION__, sizeOfStruct);