-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnl80211_control.c
789 lines (628 loc) · 24.6 KB
/
nl80211_control.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
/*
This file is part of LORCON
The majority of the functions in this file were derived directly from the
'iw' tool (and are marked as such), (c) Johannes Berg
LOROCN 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.
LORCON 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 LORCON; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "lorcon.h"
#if defined(HAVE_LIBNL20) || defined(HAVE_LIBNL30) || defined(HAVE_LIBNLTINY)
#define HAVE_LIBNL_NG
#endif
#ifdef SYS_LINUX
#ifdef HAVE_LINUX_NETLINK
#include <sys/types.h>
#include <asm/types.h>
#ifdef HAVE_LIBNLTINY
#define _GNU_SOURCE
#endif
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include "nl80211.h"
#include <net/if.h>
#endif
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "nl80211_control.h"
#include "wifi_ht_channels.h"
// Libnl1->Libnl2 compatability mode since the API changed, cribbed from 'iw'
#if defined(HAVE_LIBNL10)
#define nl_sock nl_handle
static inline struct nl_handle *nl_socket_alloc(void) {
#ifdef HAVE_LINUX_NETLINK
return nl_handle_alloc();
#else
return NULL;
#endif
}
static inline void nl_socket_free(struct nl_sock *h) {
#ifdef HAVE_LINUX_NETLINK
nl_handle_destroy(h);
#else
return;
#endif
}
#endif
int ChanToFreq(int in_chan) {
/* 802.11 channels to frequency; if it looks like a frequency, return as
* pure frequency; derived from iwconfig */
if (in_chan > 250)
return in_chan;
if (in_chan == 14)
return 2484;
else if (in_chan < 14)
return 2407 + in_chan * 5;
else if (in_chan >= 182 && in_chan <= 196)
return 4000 + in_chan * 5;
else
return 5000 + in_chan * 5;
return in_chan;
}
int FreqToChan(int in_freq) {
if (in_freq < 250)
return in_freq;
/* revamped from iw */
if (in_freq == 2484)
return 14;
if (in_freq < 2484)
return (in_freq - 2407) / 5;
return in_freq / 5 - 1000;
}
int nl80211_connect(const char *interface, void **nl_sock,
int *nl80211_id, int *if_index, char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX,
"cannot connect to netlink; not compiled with netlink "
"support. Check the output of ./configure for more information");
return -1;
#else
if ((*if_index = if_nametoindex(interface)) < 0) {
snprintf(errstr, LORCON_STATUS_MAX,
"cannot connect to netlink: Could not find interface '%s'", interface);
return -1;
}
*nl_sock = nl_socket_alloc();
if (!nl_sock) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to connect to netlink: could not allocate netlink socket");
return -1;
}
if (genl_connect(*nl_sock)) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to connect to netlink: could not connect to generic netlink");
return -1;
nl_socket_free(*nl_sock);
}
*nl80211_id = genl_ctrl_resolve(*nl_sock, "nl80211");
if (nl80211_id < 0) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to connect to netlink: could not resolve nl80211");
nl_socket_free(*nl_sock);
}
return 0;
#endif
}
void nl80211_disconnect(void *nl_sock) {
nl_socket_free(nl_sock);
}
int nl80211_createvif(const char *interface, const char *newinterface,
unsigned int *in_flags, unsigned int flags_sz, char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "Lorcon was not compiled with netlink/mac80211 "
"support, check the output of ./configure for why");
return -1;
#else
void *nl_sock;
int nl80211_id;
struct nl_msg *msg;
struct nl_msg *flags;
unsigned int x;
if (if_nametoindex(newinterface) > 0)
return 1;
nl_sock = nl_socket_alloc();
if (!nl_sock) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to create monitor vif %s:%s, unable to allocate netlink socket",
interface, newinterface);
return -1;
}
if (genl_connect(nl_sock)) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to create monitor vif %s:%s, unable to connect generic netlink",
interface, newinterface);
nl_socket_free(nl_sock);
}
nl80211_id = genl_ctrl_resolve(nl_sock, "nl80211");
if (nl80211_id < 0) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to create monitor vif %s:%s, unable to resolve nl80211",
interface, newinterface);
nl_socket_free(nl_sock);
}
if ((msg = nlmsg_alloc()) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to create monitor vif %s:%s, unable to allocate nl80211 "
"message", interface, newinterface);
nl_socket_free(nl_sock);
return -1;
}
if ((flags = nlmsg_alloc()) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to create monitor vif %s:%s, unable to allocate nl80211 flags",
interface, newinterface);
nl_socket_free(nl_sock);
return -1;
}
genlmsg_put(msg, 0, 0, nl80211_id, 0, 0, NL80211_CMD_NEW_INTERFACE, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(interface));
NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, newinterface);
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
for (x = 0; x < flags_sz; x++) {
NLA_PUT_FLAG(flags, in_flags[x]);
}
if (flags_sz > 0)
nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
if (nl_send_auto_complete(nl_sock, msg) < 0 || nl_wait_for_ack(nl_sock) < 0) {
nla_put_failure:
snprintf(errstr, LORCON_STATUS_MAX, "failed to create monitor interface %s:%s",
interface, newinterface);
nl_socket_free(nl_sock);
nlmsg_free(msg);
nlmsg_free(flags);
return -1;
}
nl_socket_free(nl_sock);
nlmsg_free(msg);
nlmsg_free(flags);
if (if_nametoindex(newinterface) <= 0) {
snprintf(errstr, LORCON_STATUS_MAX,
"creating a monitor interface for %s:%s worked, but couldn't"
"find that interface after creation.", interface, newinterface);
return -1;
}
return 0;
#endif
}
int nl80211_setchannel_cache(int ifindex, void *nl_sock,
int nl80211_id, int channel, unsigned int chmode, char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "Lorcon was not compiled with netlink/mac80211 "
"support, check the output of ./configure for why");
return -1;
#else
struct nl_msg *msg;
int ret = 0;
if (chmode >= 4) {
snprintf(errstr, LORCON_STATUS_MAX, "unable to set channel: invalid channel mode");
return -1;
}
if ((msg = nlmsg_alloc()) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to set channel: unable to allocate mac80211 control message.");
return -1;
}
genlmsg_put(msg, 0, 0, nl80211_id, 0, 0, NL80211_CMD_SET_WIPHY, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, ChanToFreq(channel));
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, chmode);
if ((ret = nl_send_auto_complete(nl_sock, msg)) >= 0) {
if ((ret = nl_wait_for_ack(nl_sock)) < 0)
goto nla_put_failure;
}
nlmsg_free(msg);
return 0;
nla_put_failure:
snprintf(errstr, LORCON_STATUS_MAX,
"unable to set channel %u/%u mode %u via mac80211: "
"error code %d", channel, ChanToFreq(channel), chmode, ret);
nlmsg_free(msg);
return ret;
#endif
}
int nl80211_set_channel(const char *interface, int channel,
unsigned int chmode, char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "Lorcon was not compiled with netlink/mac80211 "
"support, check the output of ./configure for why");
return -1;
#else
void *nl_sock;
int nl80211_id;
int ifidx;
if (nl80211_connect(interface, &nl_sock, &nl80211_id, &ifidx, errstr) < 0)
return -1;
int ret =
nl80211_setchannel_cache(ifidx, nl_sock, nl80211_id, channel, chmode, errstr);
nl80211_disconnect(nl_sock);
return ret;
#endif
}
int nl80211_setfrequency_cache(int ifindex, void *nl_sock, int nl80211_id,
unsigned int control_freq, unsigned int chan_width,
unsigned int center_freq1, unsigned int center_freq2,
char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "Lorcon was not compiled with netlink/mac80211 "
"support, check the output of ./configure for why");
return -1;
#else
struct nl_msg *msg;
int ret = 0;
if ((msg = nlmsg_alloc()) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX,
"unable to set channel/frequency: unable to allocate "
"mac80211 control message.");
return -1;
}
genlmsg_put(msg, 0, 0, nl80211_id, 0, 0, NL80211_CMD_SET_WIPHY, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, ChanToFreq(control_freq));
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, chan_width);
if (center_freq1 != 0) {
NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, ChanToFreq(center_freq1));
}
if ((ret = nl_send_auto_complete(nl_sock, msg)) >= 0) {
if ((ret = nl_wait_for_ack(nl_sock)) < 0)
goto nla_put_failure;
}
nlmsg_free(msg);
return 0;
nla_put_failure:
snprintf(errstr, LORCON_STATUS_MAX,
"unable to set frequency %u %u %u via mac80211: error code %d",
control_freq, chan_width, center_freq1, ret);
nlmsg_free(msg);
return ret;
#endif
}
int nl80211_setfrequency(const char *interface,
unsigned int control_freq, unsigned int chan_width,
unsigned int center_freq1, unsigned int center_freq2,
char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "Lorcon was not compiled with netlink/mac80211 "
"support, check the output of ./configure for why");
return -1;
#else
void *nl_sock;
int ifidx;
int nl80211_id;
if (nl80211_connect(interface, &nl_sock, &nl80211_id, &ifidx, errstr) < 0) {
return -1;
}
int ret =
nl80211_setfrequency_cache(ifidx, nl_sock, nl80211_id,
control_freq, chan_width, center_freq1, center_freq2, errstr);
nl80211_disconnect(nl_sock);
return ret;
#endif
}
struct nl80211_channel_list {
/* Should be a string once we support ht/vht */
int channel;
struct nl80211_channel_list *next;
};
struct nl80211_channel_block {
char *phyname;
int nfreqs;
struct nl80211_channel_list *channel_list;
struct nl80211_channel_list *chan_list_last;
};
#ifdef HAVE_LINUX_NETLINK
static int nl80211_freqlist_cb(struct nl_msg *msg, void *arg) {
struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
struct genlmsghdr *gnlh = (struct genlmsghdr *) nlmsg_data(nlmsg_hdr(msg));
struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
struct nlattr *nl_band, *nl_freq;
int rem_band, rem_freq;
uint32_t freq;
struct nl80211_channel_block *chanb = (struct nl80211_channel_block *) arg;
char channel_str[32];
int band_ht40, band_ht80, band_ht160;
unsigned int hti;
struct nl80211_channel_list *chan_list_new;
nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
if (tb_msg[NL80211_ATTR_WIPHY_NAME]) {
if (strcmp(nla_get_string(tb_msg[NL80211_ATTR_WIPHY_NAME]), chanb->phyname) != 0) {
return NL_SKIP;
}
}
if (tb_msg[NL80211_ATTR_WIPHY_BANDS]) {
nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
band_ht40 = band_ht80 = band_ht160 = 0;
nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
nla_len(nl_band), NULL);
/* If we have a HT capability field, examine it for HT40 */
if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
__u16 cap = nla_get_u16(tb_band[NL80211_BAND_ATTR_HT_CAPA]);
/* bit 1 is the HT40 bit */
if (cap & (1 << 1))
band_ht40 = 1;
}
/* If we have a VHT field, we can assume we have HT80 and then we need
* to examine ht160.
* TODO: figure out 160 80+80; do all devices that support 80+80 support
* 160? For now we assume they do...
*/
if (tb_band[NL80211_BAND_ATTR_VHT_CAPA]) {
band_ht80 = 1;
__u16 cap = nla_get_u32(tb_band[NL80211_BAND_ATTR_VHT_CAPA]);
if (((cap >> 2) & 3) == 1) {
band_ht160 = 1;
} else if (((cap >> 2) & 3) == 2) {
band_ht160 = 1;
}
}
// fprintf(stderr, "debug - %u %u %u\n", band_ht40, band_ht80, band_ht160);
if (tb_band[NL80211_BAND_ATTR_FREQS]) {
nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
nla_len(nl_freq), NULL);
if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
continue;
if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
continue;
/* We've got at least one actual frequency */
freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
chan_list_new = (struct nl80211_channel_list *) malloc(sizeof(struct nl80211_channel_list));
/* Ultimately channels will be strings to support HT/VHT
snprintf(channel_str, 32, "%u", mac80211_freq_to_chan(freq));
chan_list_new->channel = strdup(channel_str);
*/
chan_list_new->channel = ChanToFreq(freq);
chan_list_new->next = NULL;
chanb->nfreqs++;
chanb->chan_list_last->next = chan_list_new;
chanb->chan_list_last = chan_list_new;
#if 0
/* This code from Kismet handles HT/VHT channels but we don't have
* the infrastructure yet in lorcon to treat channels as strings,
* so just def it out for now */
/* Look us up in the wifi_ht_channels list and add channels if we
* need to add HT capabilities. We could convert this to a channel
* but it's better to do a frequency lookup */
for (hti = 0; hti < MAX_WIFI_HT_CHANNEL; hti++) {
if (wifi_ht_channels[hti].freq == freq) {
if (band_ht40) {
if (wifi_ht_channels[hti].flags & WIFI_HT_HT40MINUS) {
chan_list_new = (struct nl80211_channel_list *) malloc(sizeof(struct nl80211_channel_list));
snprintf(channel_str, 32,
"%uHT40-", mac80211_freq_to_chan(freq));
chan_list_new->channel = strdup(channel_str);
chan_list_new->next = NULL;
chanb->nfreqs++;
chanb->chan_list_last->next = chan_list_new;
chanb->chan_list_last = chan_list_new;
}
if (wifi_ht_channels[hti].flags & WIFI_HT_HT40PLUS) {
chan_list_new = (struct nl80211_channel_list *) malloc(sizeof(struct nl80211_channel_list));
snprintf(channel_str, 32,
"%uHT40+", mac80211_freq_to_chan(freq));
chan_list_new->channel = strdup(channel_str);
chan_list_new->next = NULL;
chanb->nfreqs++;
chanb->chan_list_last->next = chan_list_new;
chanb->chan_list_last = chan_list_new;
}
}
if (band_ht80 && wifi_ht_channels[hti].flags & WIFI_HT_HT80) {
chan_list_new = (struct nl80211_channel_list *) malloc(sizeof(struct nl80211_channel_list));
snprintf(channel_str, 32,
"%uVHT80", mac80211_freq_to_chan(freq));
chan_list_new->channel = strdup(channel_str);
chan_list_new->next = NULL;
chanb->nfreqs++;
chanb->chan_list_last->next = chan_list_new;
chanb->chan_list_last = chan_list_new;
}
if (band_ht160 && wifi_ht_channels[hti].flags & WIFI_HT_HT160) {
chan_list_new = (struct nl80211_channel_list *) malloc(sizeof(struct nl80211_channel_list));
snprintf(channel_str, 32,
"%uVHT160", mac80211_freq_to_chan(freq));
chan_list_new->channel = strdup(channel_str);
chan_list_new->next = NULL;
chanb->nfreqs++;
chanb->chan_list_last->next = chan_list_new;
chanb->chan_list_last = chan_list_new;
}
break;
}
}
#endif
}
}
}
}
return NL_SKIP;
}
#endif
#ifdef HAVE_LINUX_NETLINK
static int nl80211_error_cb(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg) {
int *ret = (int *) arg;
*ret = err->error;
return NL_STOP;
}
static int nl80211_finish_cb(struct nl_msg *msg, void *arg) {
int *ret = (int *) arg;
*ret = 0;
return NL_SKIP;
}
static int nl80211_ack_cb(struct nl_msg *msg, void *arg) {
int *ret = arg;
*ret = 0;
return NL_STOP;
}
#endif
int nl80211_get_chanlist(const char *interface, int *ret_num_chans, int **ret_chan_list,
char *errstr) {
struct nl80211_channel_block cblock = {
.phyname = NULL,
.nfreqs = 0,
.channel_list = NULL,
.chan_list_last = NULL
};
unsigned int num_freq;
struct nl80211_channel_list *chan_list_cur, *chan_list_old;
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "Kismet was not compiled with netlink/nl80211 "
"support, check the output of ./configure for why");
return -1;
#else
void *nl_sock;
int nl80211_id;
struct nl_cb *cb;
int err;
struct nl_msg *msg;
cblock.phyname = nl80211_find_parent(interface);
if (strlen(cblock.phyname) == 0) {
if (if_nametoindex(interface) <= 0) {
snprintf(errstr, LORCON_STATUS_MAX,
"failed to get channels from interface '%s': interface does "
"not exist.", interface);
return -1;
}
snprintf(errstr, LORCON_STATUS_MAX,
"failed to find parent phy interface for interface '%s': interface "
"may not be a mac80211 wifi device?", interface);
return -1;
}
nl_sock = nl_socket_alloc();
if (!nl_sock) {
snprintf(errstr, LORCON_STATUS_MAX, "FATAL: Failed to allocate netlink socket");
return -1;
}
if (genl_connect(nl_sock)) {
snprintf(errstr, LORCON_STATUS_MAX, "FATAL: Failed to connect to generic netlink");
nl_socket_free(nl_sock);
}
nl80211_id = genl_ctrl_resolve(nl_sock, "nl80211");
if (nl80211_id < 0) {
snprintf(errstr, LORCON_STATUS_MAX, "FATAL: Failed to resolve nl80211");
nl_socket_free(nl_sock);
}
msg = nlmsg_alloc();
cb = nl_cb_alloc(NL_CB_DEFAULT);
err = 1;
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_freqlist_cb, &cblock);
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_ack_cb, &err);
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_finish_cb, &err);
nl_cb_err(cb, NL_CB_CUSTOM, nl80211_error_cb, &err);
genlmsg_put(msg, 0, 0, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_WIPHY, 0);
/* Initialize the empty first channel list item */
cblock.channel_list = (struct nl80211_channel_list *) malloc(sizeof(struct nl80211_channel_list));
cblock.channel_list->channel = 0;
/*
cblock.channel_list->channel = NULL;
*/
cblock.channel_list->next = NULL;
cblock.chan_list_last = cblock.channel_list;
if (nl_send_auto_complete((struct nl_sock *) nl_sock, msg) < 0) {
snprintf(errstr, LORCON_STATUS_MAX,
"failed to fetch channels from interface '%s': failed to "
"write netlink command", interface);
nlmsg_free(msg);
nl_cb_put(cb);
nl_socket_free(nl_sock);
return -1;
}
while (err)
nl_recvmsgs((struct nl_sock *) nl_sock, cb);
nl_cb_put(cb);
nlmsg_free(msg);
nl_socket_free(nl_sock);
/* Convert our linked list into a channel block */
(*ret_num_chans) = cblock.nfreqs;
(*ret_chan_list) = malloc(sizeof(char *) * cblock.nfreqs);
num_freq = 0;
/* Skip the first item which is our placeholder */
chan_list_cur = cblock.channel_list->next;
while (chan_list_cur != NULL && num_freq < cblock.nfreqs) {
/* Use the dup'd string directly */
(*ret_chan_list)[num_freq++] = chan_list_cur->channel;
// fprintf(stderr, "debug - %u %s\n", num_freq, chan_list_cur->channel);
/* Shuffle the pointers */
chan_list_old = chan_list_cur;
chan_list_cur = chan_list_cur->next;
free(chan_list_old);
}
/* If we didn't process all the channels before we hit the end of the list... */
if (chan_list_cur != NULL || num_freq != cblock.nfreqs) {
fprintf(stderr, "ERROR - linux_netlink_control miscalculated the number of "
"channels somehow...\n");
/* Clean up list overrun */
while (chan_list_cur != NULL) {
chan_list_old = chan_list_cur;
chan_list_cur = chan_list_cur->next;
free(chan_list_old);
}
/* Clean up list underrun */
for ( ; num_freq < cblock.nfreqs; num_freq++) {
/*
(*ret_chan_list)[num_freq] = NULL;
*/
(*ret_chan_list)[num_freq] = 0;
}
}
/* remove the list head ptr */
free(cblock.channel_list);
/* Remove the phyname */
free(cblock.phyname);
return (*ret_num_chans);
#endif
}
char *nl80211_find_parent(const char *interface) {
DIR *devdir;
struct dirent *devfile;
char dirpath[1024];
char *ret;
DIR *ieeedir;
struct dirent *ieeefile;
char ieeedirpath[1024];
snprintf(dirpath, 1024, "/sys/class/net/%s/phy80211/device", interface);
if ((devdir = opendir(dirpath)) == NULL)
return NULL;
while ((devfile = readdir(devdir)) != NULL) {
if (strlen(devfile->d_name) < 9)
continue;
if (strncmp("ieee80211:phy", devfile->d_name, 13) == 0) {
ret = strdup(devfile->d_name + 10);
closedir(devdir);
return ret;
}
if (strncmp("ieee80211", devfile->d_name, 9) == 0) {
snprintf(ieeedirpath, 1024, "%s/ieee80211", dirpath);
if ((ieeedir = opendir(ieeedirpath)) != NULL) {
while ((ieeefile = readdir(ieeedir)) != NULL) {
if (strncmp("phy", ieeefile->d_name, 3) == 0) {
ret = strdup(ieeefile->d_name);
closedir(ieeedir);
closedir(devdir);
return ret;
}
}
}
closedir(ieeedir);
}
}
closedir(devdir);
return NULL;
}
#endif /* SYS_LINUX */