-
Notifications
You must be signed in to change notification settings - Fork 17
/
iwinfo_nl80211.c
3716 lines (2967 loc) · 86.1 KB
/
iwinfo_nl80211.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
/*
* iwinfo - Wireless Information Library - NL80211 Backend
*
* Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
*
* The iwinfo library is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* The iwinfo library 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 the iwinfo library. If not, see http://www.gnu.org/licenses/.
*
* The signal handling code is derived from the official madwifi tools,
* wlanconfig.c in particular. The encryption property handling was
* inspired by the hostapd madwifi driver.
*
* Parts of this code are derived from the Linux iw utility.
*/
#include <sys/stat.h>
#include <limits.h>
#include <glob.h>
#include <fnmatch.h>
#include <stdarg.h>
#include <stdlib.h>
#include "iwinfo_nl80211.h"
#define min(x, y) ((x) < (y)) ? (x) : (y)
#define BIT(x) (1ULL<<(x))
static struct nl80211_state *nls = NULL;
static void nl80211_close(void)
{
if (nls)
{
if (nls->nlctrl)
genl_family_put(nls->nlctrl);
if (nls->nl80211)
genl_family_put(nls->nl80211);
if (nls->nl_sock)
nl_socket_free(nls->nl_sock);
if (nls->nl_cache)
nl_cache_free(nls->nl_cache);
free(nls);
nls = NULL;
}
}
static int nl80211_init(void)
{
int err, fd;
if (!nls)
{
nls = malloc(sizeof(struct nl80211_state));
if (!nls) {
err = -ENOMEM;
goto err;
}
memset(nls, 0, sizeof(*nls));
nls->nl_sock = nl_socket_alloc();
if (!nls->nl_sock) {
err = -ENOMEM;
goto err;
}
if (genl_connect(nls->nl_sock)) {
err = -ENOLINK;
goto err;
}
fd = nl_socket_get_fd(nls->nl_sock);
if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) {
err = -EINVAL;
goto err;
}
if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
err = -ENOMEM;
goto err;
}
nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
if (!nls->nl80211) {
err = -ENOENT;
goto err;
}
nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl");
if (!nls->nlctrl) {
err = -ENOENT;
goto err;
}
}
return 0;
err:
nl80211_close();
return err;
}
static int nl80211_readint(const char *path)
{
int fd;
int rv = -1;
char buffer[16];
if ((fd = open(path, O_RDONLY)) > -1)
{
if (read(fd, buffer, sizeof(buffer)) > 0)
rv = atoi(buffer);
close(fd);
}
return rv;
}
static int nl80211_readstr(const char *path, char *buffer, int length)
{
int fd;
int rv = -1;
if ((fd = open(path, O_RDONLY)) > -1)
{
if ((rv = read(fd, buffer, length - 1)) > 0)
{
if (buffer[rv - 1] == '\n')
rv--;
buffer[rv] = 0;
}
close(fd);
}
return rv;
}
static int nl80211_get_band(int nla_type)
{
switch (nla_type)
{
case NL80211_BAND_2GHZ:
return IWINFO_BAND_24;
case NL80211_BAND_5GHZ:
return IWINFO_BAND_5;
case NL80211_BAND_6GHZ:
return IWINFO_BAND_6;
case NL80211_BAND_60GHZ:
return IWINFO_BAND_60;
}
return 0;
}
static int nl80211_msg_error(struct sockaddr_nl *nla,
struct nlmsgerr *err, void *arg)
{
int *ret = arg;
*ret = err->error;
return NL_STOP;
}
static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
{
int *ret = arg;
*ret = 0;
return NL_SKIP;
}
static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
{
int *ret = arg;
*ret = 0;
return NL_STOP;
}
static int nl80211_msg_response(struct nl_msg *msg, void *arg)
{
return NL_SKIP;
}
static void nl80211_free(struct nl80211_msg_conveyor *cv)
{
if (cv)
{
if (cv->cb)
nl_cb_put(cv->cb);
if (cv->msg)
nlmsg_free(cv->msg);
cv->cb = NULL;
cv->msg = NULL;
}
}
static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family,
int cmd, int flags)
{
static struct nl80211_msg_conveyor cv;
struct nl_msg *req = NULL;
struct nl_cb *cb = NULL;
req = nlmsg_alloc();
if (!req)
goto err;
cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!cb)
goto err;
genlmsg_put(req, 0, 0, genl_family_get_id(family), 0, flags, cmd, 0);
cv.msg = req;
cv.cb = cb;
return &cv;
err:
if (req)
nlmsg_free(req);
return NULL;
}
static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags)
{
if (nl80211_init() < 0)
return NULL;
return nl80211_new(nls->nlctrl, cmd, flags);
}
static const char *nl80211_phy_path_str(const char *phyname)
{
static char path[PATH_MAX];
const char *prefix = "/sys/devices/";
int prefix_len = strlen(prefix);
int buf_len, offset;
struct dirent *e;
char buf[512], *link;
int phy_idx;
int seq = 0;
DIR *d;
snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", phyname);
phy_idx = nl80211_readint(buf);
if (phy_idx < 0)
return NULL;
buf_len = snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", phyname);
link = realpath(buf, path);
if (!link)
return NULL;
if (strncmp(link, prefix, prefix_len) != 0)
return NULL;
link += prefix_len;
prefix = "platform/";
prefix_len = strlen(prefix);
if (!strncmp(link, prefix, prefix_len) && strstr(link, "/pci"))
link += prefix_len;
snprintf(buf + buf_len, sizeof(buf) - buf_len, "/ieee80211");
d = opendir(buf);
if (!d)
return link;
while ((e = readdir(d)) != NULL) {
int cur_idx;
snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
cur_idx = nl80211_readint(buf);
if (cur_idx < 0)
continue;
if (cur_idx >= phy_idx)
continue;
seq++;
}
closedir(d);
if (!seq)
return link;
offset = link - path + strlen(link);
snprintf(path + offset, sizeof(path) - offset, "+%d", seq);
return link;
}
static int nl80211_phy_idx_from_path(const char *path)
{
char buf[512];
struct dirent *e;
const char *cur_path;
int cur_path_len;
int path_len;
int idx = -1;
DIR *d;
if (!path)
return -1;
path_len = strlen(path);
if (!path_len)
return -1;
d = opendir("/sys/class/ieee80211");
if (!d)
return -1;
while ((e = readdir(d)) != NULL) {
cur_path = nl80211_phy_path_str(e->d_name);
if (!cur_path)
continue;
cur_path_len = strlen(cur_path);
if (cur_path_len < path_len)
continue;
if (strcmp(cur_path + cur_path_len - path_len, path) != 0)
continue;
snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
idx = nl80211_readint(buf);
if (idx >= 0)
break;
}
closedir(d);
return idx;
}
static int nl80211_phy_idx_from_macaddr(const char *opt)
{
char buf[128];
int i, idx = -1;
glob_t gl;
if (!opt)
return -1;
snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*");
if (glob(buf, 0, NULL, &gl))
return -1;
for (i = 0; i < gl.gl_pathc; i++)
{
snprintf(buf, sizeof(buf), "%s/macaddress", gl.gl_pathv[i]);
if (nl80211_readstr(buf, buf, sizeof(buf)) <= 0)
continue;
if (fnmatch(opt, buf, FNM_CASEFOLD))
continue;
snprintf(buf, sizeof(buf), "%s/index", gl.gl_pathv[i]);
if ((idx = nl80211_readint(buf)) > -1)
break;
}
globfree(&gl);
return idx;
}
static int nl80211_phy_idx_from_phy(const char *opt)
{
char buf[128];
if (!opt)
return -1;
snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", opt);
return nl80211_readint(buf);
}
static int nl80211_phy_idx_from_uci(const char *name)
{
struct uci_section *s;
const char *opt;
int idx = -1;
s = iwinfo_uci_get_radio(name, "mac80211");
if (!s)
goto out;
opt = uci_lookup_option_string(uci_ctx, s, "path");
idx = nl80211_phy_idx_from_path(opt);
if (idx >= 0)
goto out;
opt = uci_lookup_option_string(uci_ctx, s, "macaddr");
idx = nl80211_phy_idx_from_macaddr(opt);
if (idx >= 0)
goto out;
opt = uci_lookup_option_string(uci_ctx, s, "phy");
idx = nl80211_phy_idx_from_phy(opt);
out:
iwinfo_uci_free();
return idx;
}
static bool nl80211_is_ifname(const char *name)
{
struct stat st;
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "/sys/class/net/%s", name);
return !lstat(buffer, &st);
}
static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
int cmd, int flags)
{
unsigned int ifidx = 0;
int phyidx = -1;
struct nl80211_msg_conveyor *cv;
if (ifname == NULL)
return NULL;
if (nl80211_init() < 0)
return NULL;
if (!strncmp(ifname, "mon.", 4))
ifidx = if_nametoindex(&ifname[4]);
else if (nl80211_is_ifname(ifname))
ifidx = if_nametoindex(ifname);
else
{
phyidx = nl80211_phy_idx_from_phy(ifname);
if (phyidx < 0)
phyidx = nl80211_phy_idx_from_uci(ifname);
}
/* Valid ifidx must be greater than 0 */
if ((ifidx <= 0) && (phyidx < 0))
return NULL;
cv = nl80211_new(nls->nl80211, cmd, flags);
if (!cv)
return NULL;
if (ifidx > 0)
NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx);
else if (phyidx > -1)
NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx);
return cv;
nla_put_failure:
nl80211_free(cv);
return NULL;
}
static int nl80211_send(struct nl80211_msg_conveyor *cv,
int (*cb_func)(struct nl_msg *, void *),
void *cb_arg)
{
static struct nl80211_msg_conveyor rcv;
int err;
if (cb_func)
nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg);
else
nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
err = nl_send_auto_complete(nls->nl_sock, cv->msg);
if (err < 0)
goto out;
err = 1;
nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
while (err > 0)
nl_recvmsgs(nls->nl_sock, cv->cb);
out:
nl80211_free(cv);
return err;
}
static int nl80211_request(const char *ifname, int cmd, int flags,
int (*cb_func)(struct nl_msg *, void *),
void *cb_arg)
{
struct nl80211_msg_conveyor *cv;
cv = nl80211_msg(ifname, cmd, flags);
if (!cv)
return -ENOMEM;
return nl80211_send(cv, cb_func, cb_arg);
}
static struct nlattr ** nl80211_parse(struct nl_msg *msg)
{
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
static struct nlattr *attr[NL80211_ATTR_MAX + 1];
nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
return attr;
}
static int nl80211_get_protocol_features_cb(struct nl_msg *msg, void *arg)
{
uint32_t *features = arg;
struct nlattr **attr = nl80211_parse(msg);
if (attr[NL80211_ATTR_PROTOCOL_FEATURES])
*features = nla_get_u32(attr[NL80211_ATTR_PROTOCOL_FEATURES]);
return NL_SKIP;
}
static int nl80211_get_protocol_features(const char *ifname)
{
struct nl80211_msg_conveyor *req;
uint32_t features = 0;
req = nl80211_msg(ifname, NL80211_CMD_GET_PROTOCOL_FEATURES, 0);
if (req) {
nl80211_send(req, nl80211_get_protocol_features_cb, &features);
nl80211_free(req);
}
return features;
}
static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg)
{
struct nl80211_group_conveyor *cv = arg;
struct nlattr **attr = nl80211_parse(msg);
struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1];
struct nlattr *mgrp;
int mgrpidx;
if (!attr[CTRL_ATTR_MCAST_GROUPS])
return NL_SKIP;
nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx)
{
nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX,
nla_data(mgrp), nla_len(mgrp), NULL);
if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] &&
mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] &&
!strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]),
cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME])))
{
cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]);
break;
}
}
return NL_SKIP;
}
static int nl80211_subscribe(const char *family, const char *group)
{
struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT };
struct nl80211_msg_conveyor *req;
int err;
req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0);
if (req)
{
NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family);
err = nl80211_send(req, nl80211_subscribe_cb, &cv);
if (err)
return err;
return nl_socket_add_membership(nls->nl_sock, cv.id);
nla_put_failure:
nl80211_free(req);
}
return -ENOMEM;
}
static int nl80211_wait_cb(struct nl_msg *msg, void *arg)
{
struct nl80211_event_conveyor *cv = arg;
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
if (cv->wait[gnlh->cmd / 32] & (1 << (gnlh->cmd % 32)))
cv->recv = gnlh->cmd;
return NL_SKIP;
}
static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg)
{
return NL_OK;
}
static int __nl80211_wait(const char *family, const char *group, ...)
{
struct nl80211_event_conveyor cv = { };
struct nl_cb *cb;
int err = 0;
int cmd;
va_list ap;
if (nl80211_subscribe(family, group))
return -ENOENT;
cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!cb)
return -ENOMEM;
nl_cb_err(cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL);
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv );
va_start(ap, group);
for (cmd = va_arg(ap, int); cmd != 0; cmd = va_arg(ap, int))
cv.wait[cmd / 32] |= (1 << (cmd % 32));
va_end(ap);
while (!cv.recv && !err)
nl_recvmsgs(nls->nl_sock, cb);
nl_cb_put(cb);
return err;
}
#define nl80211_wait(family, group, ...) \
__nl80211_wait(family, group, __VA_ARGS__, 0)
/* This is linux's ieee80211_freq_khz_to_channel() which is:
* SPDX-License-Identifier: GPL-2.0
* Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
* Copyright 2013-2014 Intel Mobile Communications GmbH
* Copyright 2017 Intel Deutschland GmbH
* Copyright (C) 2018-2022 Intel Corporation
*/
static int nl80211_freq2channel(int freq)
{
if (freq == 2484)
return 14;
else if (freq < 2484)
return (freq - 2407) / 5;
else if (freq >= 4910 && freq <= 4980)
return (freq - 4000) / 5;
else if (freq < 5925)
return (freq - 5000) / 5;
else if (freq == 5935)
return 2;
else if (freq <= 45000) /* DMG band lower limit */
/* see 802.11ax D6.1 27.3.22.2 */
return (freq - 5950) / 5;
else if (freq >= 58320 && freq <= 70200)
return (freq - 56160) / 2160;
else
return 0;
}
/* This is linux's ieee80211_channel_to_freq_khz() which is:
* SPDX-License-Identifier: GPL-2.0
* Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
* Copyright 2013-2014 Intel Mobile Communications GmbH
* Copyright 2017 Intel Deutschland GmbH
* Copyright (C) 2018-2022 Intel Corporation
*/
static int nl80211_channel2freq(int channel, const char *band, bool ax)
{
if (channel < 1)
return 0;
if (!band || band[0] != 'a')
{
if (channel == 14)
return 2484;
else if (channel < 14)
return (channel * 5) + 2407;
}
else if (strcmp(band, "ad") == 0)
{
if (channel < 7)
return 56160 + 2160 * channel;
}
else if (ax)
{
if (channel == 2)
return 5935;
if (channel < 233)
return (channel * 5) + 5950;
}
else
{
if (channel >= 182 && channel <= 196)
return (channel * 5) + 4000;
else
return (channel * 5) + 5000;
}
return 0;
}
static uint8_t nl80211_freq2band(int freq)
{
if (freq >= 2412 && freq <= 2484)
return IWINFO_BAND_24;
else if (freq >= 5160 && freq <= 5885)
return IWINFO_BAND_5;
else if (freq >= 5925 && freq <= 7125)
return IWINFO_BAND_6;
else if (freq >= 58320 && freq <= 69120)
return IWINFO_BAND_60;
return 0;
}
static int nl80211_phyname_cb(struct nl_msg *msg, void *arg)
{
char *buf = arg;
struct nlattr **attr = nl80211_parse(msg);
if (attr[NL80211_ATTR_WIPHY_NAME])
memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]),
nla_len(attr[NL80211_ATTR_WIPHY_NAME]));
else
buf[0] = 0;
return NL_SKIP;
}
static char * nl80211_ifname2phy(const char *ifname)
{
static char phy[32] = { 0 };
memset(phy, 0, sizeof(phy));
nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
nl80211_phyname_cb, phy);
return phy[0] ? phy : NULL;
}
static char * nl80211_phyidx2name(unsigned int idx)
{
struct nl80211_msg_conveyor *cv;
static char phy[32] = { 0 };
if (nl80211_init() < 0)
return NULL;
cv = nl80211_new(nls->nl80211, NL80211_CMD_GET_WIPHY, 0);
if (!cv)
return NULL;
NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, idx);
memset(phy, 0, sizeof(phy));
nl80211_send(cv, nl80211_phyname_cb, phy);
return phy[0] ? phy : NULL;
nla_put_failure:
return NULL;
}
static char * nl80211_phy2ifname(const char *ifname)
{
int ifidx = -1, cifidx, lmode = 1, clmode, phyidx;
char buffer[512];
static char nif[IFNAMSIZ] = { 0 };
DIR *d;
struct dirent *e;
/* Only accept phy name in the form of phy%d or radio%d */
if (!ifname)
return NULL;
phyidx = nl80211_phy_idx_from_phy(ifname);
if (phyidx < 0)
phyidx = nl80211_phy_idx_from_uci(ifname);;
if (phyidx < 0)
return NULL;
memset(nif, 0, sizeof(nif));
if ((d = opendir("/sys/class/net")) != NULL)
{
while ((e = readdir(d)) != NULL)
{
snprintf(buffer, sizeof(buffer),
"/sys/class/net/%s/phy80211/index", e->d_name);
if (nl80211_readint(buffer) != phyidx)
continue;
snprintf(buffer, sizeof(buffer),
"/sys/class/net/%s/ifindex", e->d_name);
cifidx = nl80211_readint(buffer);
if (cifidx < 0)
continue;
snprintf(buffer, sizeof(buffer),
"/sys/class/net/%s/link_mode", e->d_name);
clmode = nl80211_readint(buffer);
/* prefer non-supplicant-based devices */
if ((ifidx < 0) || (cifidx < ifidx) || ((lmode == 1) && (clmode != 1)))
{
ifidx = cifidx;
lmode = clmode;
strncpy(nif, e->d_name, sizeof(nif) - 1);
}
}
closedir(d);
}
return nif[0] ? nif : NULL;
}
static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
{
int *mode = arg;
struct nlattr **tb = nl80211_parse(msg);
const int ifmodes[NL80211_IFTYPE_MAX + 1] = {
IWINFO_OPMODE_UNKNOWN, /* unspecified */
IWINFO_OPMODE_ADHOC, /* IBSS */
IWINFO_OPMODE_CLIENT, /* managed */
IWINFO_OPMODE_MASTER, /* AP */
IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */
IWINFO_OPMODE_WDS, /* WDS */
IWINFO_OPMODE_MONITOR, /* monitor */
IWINFO_OPMODE_MESHPOINT, /* mesh point */
IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */
IWINFO_OPMODE_P2P_GO, /* P2P-GO */
};
if (tb[NL80211_ATTR_IFTYPE])
*mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])];
return NL_SKIP;
}
static int nl80211_get_mode(const char *ifname, int *buf)
{
char *res;
*buf = IWINFO_OPMODE_UNKNOWN;
res = nl80211_phy2ifname(ifname);
nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
nl80211_get_mode_cb, buf);
return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0;
}
static int __nl80211_hostapd_query(const char *ifname, ...)
{
va_list ap, ap_cur;
char *phy, *search, *dest, *key, *val, buf[128];
int len, mode, found = 0, match = 1;
FILE *fp;
if (nl80211_get_mode(ifname, &mode))
return 0;
if (mode != IWINFO_OPMODE_MASTER && mode != IWINFO_OPMODE_AP_VLAN)
return 0;
phy = nl80211_ifname2phy(ifname);
if (!phy)
return 0;
snprintf(buf, sizeof(buf), "/var/run/hostapd-%s.conf", phy);
fp = fopen(buf, "r");
if (!fp)
return 0;
va_start(ap, ifname);
/* clear all destination buffers */
va_copy(ap_cur, ap);
while ((search = va_arg(ap_cur, char *)) != NULL)
{
dest = va_arg(ap_cur, char *);
len = va_arg(ap_cur, int);
memset(dest, 0, len);
}
va_end(ap_cur);
/* iterate applicable lines and copy found values into dest buffers */
while (fgets(buf, sizeof(buf), fp))
{
key = strtok(buf, " =\t\n");
val = strtok(NULL, "\n");
if (!key || !val || !*key || *key == '#')
continue;
if (!strcmp(key, "interface") || !strcmp(key, "bss"))
match = !strcmp(ifname, val);
if (!match)
continue;
va_copy(ap_cur, ap);
while ((search = va_arg(ap_cur, char *)) != NULL)
{
dest = va_arg(ap_cur, char *);
len = va_arg(ap_cur, int);
if (!strcmp(search, key))
{
strncpy(dest, val, len - 1);
found++;
break;
}
}
va_end(ap_cur);
}
fclose(fp);
va_end(ap);
return found;
}
#define nl80211_hostapd_query(ifname, ...) \
__nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
{
fd_set rfds;
struct timeval tv = { 0, 256000 };
FD_ZERO(&rfds);
FD_SET(sock, &rfds);
memset(buf, 0, blen);
if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0)
return -1;
if (!FD_ISSET(sock, &rfds))
return -1;
return recv(sock, buf, blen - 1, 0);