-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnl80211_control.c
632 lines (516 loc) · 16.3 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
/*
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"
#ifdef SYS_LINUX
#ifdef HAVE_LINUX_NETLINK
#include <sys/types.h>
#include <asm/types.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include <netlink/netlink.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 "lorcon.h"
#include "nl80211.h"
#include "nl80211_control.h"
// Libnl1->Libnl2 compatability mode since the API changed, cribbed from 'iw'
#if !defined(HAVE_LIBNL2PLUS) && defined(HAVE_LINUX_NETLINK)
#define nl_sock nl_handle
static inline struct nl_handle *nl_socket_alloc(void) {
return (struct nl_handle *) nl_handle_alloc();
}
static inline void nl_socket_free(struct nl_sock *h) {
nl_handle_destroy(h);
}
static inline int __genl_ctrl_alloc_cache(struct nl_sock *h, struct nl_cache **cache) {
struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
if (!tmp)
return -1;
*cache = tmp;
return 0;
}
#define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
#endif
int IEEE80211Freq[][2] = {
{1, 2412},
{2, 2417},
{3, 2422},
{4, 2427},
{5, 2432},
{6, 2437},
{7, 2442},
{8, 2447},
{9, 2452},
{10, 2457},
{11, 2462},
{12, 2467},
{13, 2472},
{14, 2484},
// We could do the math here, but what about 4ghz nonsense?
// We'll do table lookups for now.
{36, 5180},
{37, 5185},
{38, 5190},
{39, 5195},
{40, 5200},
{41, 5205},
{42, 5210},
{43, 5215},
{44, 5220},
{45, 5225},
{46, 5230},
{47, 5235},
{48, 5240},
{52, 5260},
{53, 5265},
{54, 5270},
{55, 5275},
{56, 5280},
{57, 5285},
{58, 5290},
{59, 5295},
{60, 5300},
{64, 5320},
{149, 5745},
{150, 5750},
{152, 5760},
{153, 5765},
{157, 5785},
{160, 5800},
{161, 5805},
{165, 5825},
{0, 0}
};
int ChanToFreq(int in_chan) {
int x = 0;
// 80211b frequencies to channels
while (IEEE80211Freq[x][0] != 0) {
if (IEEE80211Freq[x][0] == in_chan) {
return IEEE80211Freq[x][1];
}
x++;
}
return in_chan;
}
int FreqToChan(int in_freq) {
int x = 0;
// 80211b frequencies to channels
while (IEEE80211Freq[x][1] != 0) {
if (IEEE80211Freq[x][1] == in_freq) {
return IEEE80211Freq[x][0];
}
x++;
}
return in_freq;
}
int nl80211_connect(const char *interface, void **handle, void **cache,
void **family, char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "LORCON was not compiled with netlink/nl80211 "
"support, check the output of ./configure for why");
return -1;
#else
struct nl_sock *nl_handle;
struct nl_cache *nl_cache;
struct genl_family *nl80211;
if ((nl_handle = nl_socket_alloc()) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX, "%s failed to allocate nlhandle",
__FUNCTION__);
return -1;
}
if (genl_connect(nl_handle)) {
snprintf(errstr, LORCON_STATUS_MAX, "%s failed to connect to generic netlink",
__FUNCTION__);
nl_socket_free(nl_handle);
return -1;
}
if (genl_ctrl_alloc_cache(nl_handle, &nl_cache) != 0) {
snprintf(errstr, LORCON_STATUS_MAX, "%s failed to allocate "
"generic netlink cache", __FUNCTION__);
nl_socket_free(nl_handle);
return -1;
}
if ((nl80211 = genl_ctrl_search_by_name(nl_cache, "nl80211")) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX, "%s failed to find "
"nl80211 controls, kernel may be too old", __FUNCTION__);
nl_socket_free(nl_handle);
return -1;
}
(*handle) = (void *) nl_handle;
(*cache) = (void *) nl_cache;
(*family) = (void *) nl80211;
return 1;
#endif
}
void nl80211_disconnect(void *handle) {
#ifdef HAVE_LINUX_NETLINK
nl_socket_free((struct nl_sock *) handle);
#endif
}
int nl80211_createvap(const char *interface, const char *newinterface, char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "LORCON was not compiled with "
"netlink/nl80211 support, check the output of ./configure for why");
return -1;
#else
struct nl_sock *nl_handle;
struct nl_cache *nl_cache;
struct genl_family *nl80211;
struct nl_msg *msg;
if (if_nametoindex(newinterface) > 0)
return 1;
if (nl80211_connect(interface, (void **) &nl_handle,
(void **) &nl_cache, (void **) &nl80211, errstr) < 0)
return -1;
if ((msg = nlmsg_alloc()) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX, "nl80211_createvap() failed to allocate "
"message");
nl80211_disconnect(nl_handle);
return -1;
}
genlmsg_put(msg, 0, 0, genl_family_get_id(nl80211), 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);
if (nl_send_auto_complete(nl_handle, msg) < 0 || nl_wait_for_ack(nl_handle) < 0) {
nla_put_failure:
snprintf(errstr, LORCON_STATUS_MAX, "nl80211_createvap() failed to create "
"interface '%s'", newinterface);
nlmsg_free(msg);
nl80211_disconnect(nl_handle);
return -1;
}
nlmsg_free(msg);
nl80211_disconnect(nl_handle);
if (if_nametoindex(newinterface) <= 0) {
snprintf(errstr, LORCON_STATUS_MAX, "nl80211_createvap() thought we made a "
"vap, but it wasn't there when we looked");
return -1;
}
return 0;
#endif
}
// Has to be a separate function because of gotos, ew
void nl80211_parseflags(int nflags, int *in_flags, struct nl_msg *msg) {
#ifdef HAVE_LINUX_NETLINK
struct nl_msg *flags;
unsigned int x;
enum nl80211_mntr_flags flag = NL80211_MNTR_FLAG_MAX;
if ((flags = nlmsg_alloc()) == NULL) {
return;
}
for (x = 0; x < nflags; x++) {
switch (in_flags[x]) {
case nl80211_mntr_flag_none:
continue;
break;
case nl80211_mntr_flag_fcsfail:
flag = NL80211_MNTR_FLAG_FCSFAIL;
break;
case nl80211_mntr_flag_plcpfail:
flag = NL80211_MNTR_FLAG_PLCPFAIL;
break;
case nl80211_mntr_flag_control:
flag = NL80211_MNTR_FLAG_CONTROL;
break;
case nl80211_mntr_flag_otherbss:
flag = NL80211_MNTR_FLAG_OTHER_BSS;
break;
case nl80211_mntr_flag_cookframe:
flag = NL80211_MNTR_FLAG_COOK_FRAMES;
break;
}
NLA_PUT_FLAG(flags, flag);
}
nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
nla_put_failure:
nlmsg_free(flags);
#endif
}
int nl80211_setvapflag(const char *interface, char *errstr, int nflags, int *in_flags) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "LORCON was not compiled with netlink/nl80211 "
"support, check the output of ./configure for why");
return -1;
#else
struct nl_sock *nl_handle;
struct nl_cache *nl_cache;
struct genl_family *nl80211;
struct nl_msg *msg;
if (nl80211_connect(interface, (void **) &nl_handle,
(void **) &nl_cache, (void **) &nl80211, errstr) < 0)
return -1;
if ((msg = nlmsg_alloc()) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX, "%s failed to allocate message",
__FUNCTION__);
nl80211_disconnect(nl_handle);
return -1;
}
genlmsg_put(msg, 0, 0, genl_family_get_id(nl80211), 0, 0,
NL80211_CMD_SET_INTERFACE, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(interface));
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
nl80211_parseflags(nflags, in_flags, msg);
if (nl_send_auto_complete(nl_handle, msg) >= 0) {
if (nl_wait_for_ack(nl_handle) < 0) {
goto nla_put_failure;
}
} else {
nla_put_failure:
snprintf(errstr, LORCON_STATUS_MAX, "%s failed to set flags on "
"interface '%s': %s", __FUNCTION__, interface,
strerror(errno));
nlmsg_free(msg);
nl80211_disconnect(nl_handle);
return -1;
}
nlmsg_free(msg);
nl80211_disconnect(nl_handle);
return 0;
#endif
}
int nl80211_setchannel_cache(const char *interface, void *handle,
void *family, int channel,
unsigned int chmode, char *errstr) {
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "LORCON was not compiled with netlink/nl80211 "
"support, check the output of ./configure for why");
// Return the same error as we get if the device doesn't support nlfreq
return -22;
#else
struct nl_sock *nl_handle = (struct nl_sock *) handle;
struct genl_family *nl80211 = (struct genl_family *) family;
struct nl_msg *msg;
int ret = 0;
int chanmode[] = {
NL80211_CHAN_NO_HT, NL80211_CHAN_HT20,
NL80211_CHAN_HT40PLUS, NL80211_CHAN_HT40MINUS
};
if (chmode > 4) {
snprintf(errstr, LORCON_STATUS_MAX, "Invalid channel mode\n");
return -1;
}
if ((msg = nlmsg_alloc()) == NULL) {
snprintf(errstr, LORCON_STATUS_MAX, "nl80211_setchannel() failed to allocate "
"message");
return -1;
}
genlmsg_put(msg, 0, 0, genl_family_get_id(nl80211), 0, 0,
NL80211_CMD_SET_WIPHY, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(interface));
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, ChanToFreq(channel));
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, chanmode[chmode]);
if ((ret = nl_send_auto_complete(nl_handle, msg)) >= 0) {
if ((ret = nl_wait_for_ack(nl_handle)) < 0)
goto nla_put_failure;
}
nlmsg_free(msg);
return 0;
nla_put_failure:
snprintf(errstr, LORCON_STATUS_MAX, "nl80211_setchannel() could not set channel "
"%d/%d on interface '%s' err %d", channel, ChanToFreq(channel),
interface, ret);
nlmsg_free(msg);
return ret;
#endif
}
int nl80211_setchannel(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/nl80211 "
"support, check the output of ./configure for why");
// Return the same error as if the device doesn't support nl freq control
// so we catch it elsewhere
return -22;
#else
struct nl_sock *nl_handle;
struct nl_cache *nl_cache;
struct genl_family *nl80211;
if (nl80211_connect(interface, (void **) &nl_handle,
(void **) &nl_cache, (void **) &nl80211, errstr) < 0)
return -1;
int ret =
nl80211_setchannel_cache(interface, (void *) nl_handle,
(void *) nl80211, channel, chmode, errstr);
nl80211_disconnect(nl_handle);
return ret;
#endif
}
#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, num_freq = 0;
uint32_t freq;
struct nl80211_channel_block *chanb = (struct nl80211_channel_block *) arg;
nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
if (!tb_msg[NL80211_ATTR_WIPHY_BANDS]) {
return NL_SKIP;
}
if (tb_msg[NL80211_ATTR_WIPHY_NAME]) {
if (strcmp(nla_get_string(tb_msg[NL80211_ATTR_WIPHY_NAME]),
chanb->phyname) != 0) {
return NL_SKIP;
}
}
// Count the number of channels
for (nl_band = (struct nlattr *) nla_data(tb_msg[NL80211_ATTR_WIPHY_BANDS]),
rem_band = nla_len(tb_msg[NL80211_ATTR_WIPHY_BANDS]);
nla_ok(nl_band, rem_band);
nl_band = (struct nlattr *) nla_next(nl_band, &rem_band)) {
nla_parse(tb_band, NL80211_BAND_ATTR_MAX, (struct nlattr *) nla_data(nl_band),
nla_len(nl_band), NULL);
for (nl_freq = (struct nlattr *) nla_data(tb_band[NL80211_BAND_ATTR_FREQS]),
rem_freq = nla_len(tb_band[NL80211_BAND_ATTR_FREQS]);
nla_ok(nl_freq, rem_freq);
nl_freq = (struct nlattr *) nla_next(nl_freq, &rem_freq)) {
nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
(struct nlattr *) 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;
num_freq++;
}
}
chanb->nfreqs = num_freq;
chanb->channel_list = malloc(sizeof(int) * num_freq);
num_freq = 0;
// Assemble a return
for (nl_band = (struct nlattr *) nla_data(tb_msg[NL80211_ATTR_WIPHY_BANDS]),
rem_band = nla_len(tb_msg[NL80211_ATTR_WIPHY_BANDS]);
nla_ok(nl_band, rem_band);
nl_band = (struct nlattr *) nla_next(nl_band, &rem_band)) {
nla_parse(tb_band, NL80211_BAND_ATTR_MAX, (struct nlattr *) nla_data(nl_band),
nla_len(nl_band), NULL);
for (nl_freq = (struct nlattr *) nla_data(tb_band[NL80211_BAND_ATTR_FREQS]),
rem_freq = nla_len(tb_band[NL80211_BAND_ATTR_FREQS]);
nla_ok(nl_freq, rem_freq);
nl_freq = (struct nlattr *) nla_next(nl_freq, &rem_freq)) {
nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
(struct nlattr *) 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;
freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
chanb->channel_list[num_freq++] = FreqToChan(freq);
}
}
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;
}
#endif
int nl80211_get_chanlist(const char *interface, int *ret_num_chans, int **ret_chan_list,
char *errstr) {
nl80211_channel_block_t cblock;
#ifndef HAVE_LINUX_NETLINK
snprintf(errstr, LORCON_STATUS_MAX, "LORCON was not compiled with netlink/nl80211 "
"support, check the output of ./configure for why");
return NL80211_CHANLIST_NOT_NL80211;
#else
void *handle, *cache, *family;
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, "Interface %s doesn't exist", interface);
return NL80211_CHANLIST_NO_INTERFACE;
}
snprintf(errstr, LORCON_STATUS_MAX, "LORCON could not find a parent phy device "
"for interface %s, it isn't nl80211?", interface);
return NL80211_CHANLIST_NOT_NL80211;
}
if (nl80211_connect(interface, &handle, &cache, &family, errstr) < 0) {
return NL80211_CHANLIST_GENERIC;
}
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_FINISH, NL_CB_CUSTOM, nl80211_finish_cb, &err);
nl_cb_err(cb, NL_CB_CUSTOM, nl80211_error_cb, &err);
genlmsg_put(msg, 0, 0, genl_family_get_id((struct genl_family *) family),
0, NLM_F_DUMP, NL80211_CMD_GET_WIPHY, 0);
if (nl_send_auto_complete((struct nl_sock *) handle, msg) < 0) {
snprintf(errstr, LORCON_STATUS_MAX, "%s: Failed to write nl80211 message",
__FUNCTION__);
nl80211_disconnect(handle);
return NL80211_CHANLIST_GENERIC;
}
while (err)
nl_recvmsgs((struct nl_sock *) handle, cb);
nl80211_disconnect(handle);
(*ret_num_chans) = cblock.nfreqs;
(*ret_chan_list) = (int *) malloc(sizeof(int) * cblock.nfreqs);
memcpy(*ret_chan_list, cblock.channel_list, sizeof(int) * cblock.nfreqs);
free(cblock.channel_list);
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;
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) < 13)
continue;
if (strncmp("ieee80211:phy", devfile->d_name, 13) == 0) {
ret = strdup(devfile->d_name + 10);
closedir(devdir);
return ret;
}
}
closedir(devdir);
return NULL;
}
#endif /* linux */