-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathairpcap-nl.c
1420 lines (1219 loc) · 43.4 KB
/
airpcap-nl.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
/* -*- mode: C; c-file-style: "k&r"; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
* Airpcap library implementation for nl80211
*
* Copyright 2011-2012 Harry Bock <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/stat.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/route/addr.h>
#include <netlink/route/link.h>
#include <linux/nl80211.h>
#include "airpcap.h"
#include "airpcap-nl.h"
#include "util.h"
static PAirpcapHandle airpcap_handle_new(void);
static void airpcap_handle_free(PAirpcapHandle handle);
static int wiphy_match_handler(struct nl_msg *msg, void *data);
VOID AirpcapGetVersion(PUINT VersionMajor,
PUINT VersionMinor,
PUINT VersionRev,
PUINT VersionBuild)
{
*VersionMajor = AIRPCAP_VERSION_MAJOR;
*VersionMinor = AIRPCAP_VERSION_MINOR;
*VersionRev = AIRPCAP_VERSION_REV;
*VersionBuild = AIRPCAP_VERSION_BUILD;
}
static int
nl80211_state_init(PAirpcapHandle handle, PCHAR Ebuf)
{
handle->nl_socket = nl_handle_alloc();
/* Allocate the netlink socket.
*/
if (NULL == handle->nl_socket) {
setebuf(Ebuf, "Failed to allocate netlink socket.");
return -1;
}
/* Connect to the generic netlink.
*/
if (genl_connect(handle->nl_socket)) {
setebuf(Ebuf, "Failed to connect to generic netlink.");
goto err;
}
if (genl_ctrl_alloc_cache(handle->nl_socket,
&handle->nl_cache)) {
setebuf(Ebuf, "Failed to allocate generic netlink cache.");
goto err;
}
/* Find and get a reference to the nl80211 family.
* Must hand back the reference via genl_family_put. */
handle->nl80211 = genl_ctrl_search_by_name(handle->nl_cache,
"nl80211");
if (NULL == handle->nl80211) {
setebuf(Ebuf, "Netlink module nl80211 not found.");
goto err;
}
if (0 != ifconfig_get_hwaddr(handle->master_ifname,
(uint8_t *)handle->mac.Address)) {
setebuf(Ebuf, "Failed to get hardware address: %s",
strerror(errno));
goto err;
}
return 0;
err:
if (handle->nl80211)
genl_family_put(handle->nl80211);
if (handle->nl_cache)
nl_cache_free(handle->nl_cache);
if (handle->nl_socket)
nl_handle_destroy(handle->nl_socket);
return -1;
}
static int
error_handler(struct sockaddr_nl *nla UNUSED,
struct nlmsgerr *err,
void *arg)
{
int *ret = arg;
*ret = err->error;
/* should this be STOP or SKIP? */
return NL_STOP;
}
static int
finish_handler(struct nl_msg *msg UNUSED,
void *arg)
{
int *ret = arg;
*ret = 0;
return NL_SKIP;
}
static int
ack_handler(struct nl_msg *msg UNUSED,
void *arg)
{
int *ret = arg;
*ret = 0;
return NL_STOP;
}
struct airpcap_interface_list {
unsigned ifindex;
unsigned int phyindex;
struct airpcap_interface_list *next;
};
struct airpcap_interface_dump_data {
struct airpcap_interface_list *start, *current;
};
static int
nl80211_get_wiphy(struct nl_handle *sock,
struct genl_family *family,
PAirpcapHandle handle);
static int
interface_dump_handler(struct nl_msg *msg, void *arg)
{
struct airpcap_interface_dump_data *data = (struct airpcap_interface_dump_data *)arg;
struct genlmsghdr *header = nlmsg_data(nlmsg_hdr(msg));
struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(header, 0),
genlmsg_attrlen(header, 0), NULL);
if (tb_msg[NL80211_ATTR_WIPHY]) {
struct airpcap_interface_list *interface;
interface = (struct airpcap_interface_list *)malloc(sizeof(struct airpcap_interface_list));
interface->next = NULL;
interface->phyindex = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
interface->ifindex = nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]);
if (NULL == data->start) {
data->start = data->current = interface;
} else {
data->current->next = interface;
data->current = interface;
}
}
return NL_OK;
}
static
int wiphy_match_handler(struct nl_msg *msg, void *data)
{
PAirpcapHandle handle = (PAirpcapHandle)data;
struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
struct nlattr *nl_band;
struct nlattr *nl_freq;
struct nlattr *nl_rate;
/* struct nlattr *nl_mode; */
/* struct nlattr *nl_cmd; */
/* Policy for parsing NL80211_FREQUENCY attribute */
static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
[NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
[NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
};
/* Policy for parsing NL80211_BITRATE attribute */
static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
[NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
};
/* remaining items for nla_for_each_nested */
int band_rem, freq_rem, rate_rem;//, mode_rem, cmd_rem;
/* parse the generic netlink reply. */
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
nla_parse(tb_msg, NL80211_ATTR_MAX,
genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0),
NULL);
if (NULL == tb_msg[NL80211_ATTR_WIPHY]) {
return NL_SKIP;
}
unsigned int match_index = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
if (match_index != handle->phyindex) {
printf("not this wiphy! expected %d, matched %d\n",
handle->phyindex,
match_index);
return NL_SKIP;
}
/* Ignore this result if there is no band data.
* Why we'd hit this condition... no idea. */
if (NULL == tb_msg[NL80211_ATTR_WIPHY_BANDS]) {
return NL_SKIP;
}
handle->channel_info_count = 0;
int freq_count = 0;
nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], band_rem) {
struct nlattr *tb_band_freqs;
nla_parse(tb_band, NL80211_BAND_ATTR_MAX,
nla_data(nl_band),
nla_len(nl_band),
NULL);
/* Loop through NL80211_BAND_ATTR_FREQS once to
* get AirpcapChannelInfo array allocation size.
* Inefficient, but we only do it once. */
tb_band_freqs = tb_band[NL80211_BAND_ATTR_FREQS];
nla_for_each_nested(nl_freq, tb_band_freqs, freq_rem) {
nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
nla_data(nl_freq),
nla_len(nl_freq),
freq_policy);
/* Ignore disabled frequencies (e.g., due to regulatory
* domain issues. */
if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
continue;
handle->channel_info_count++;
}
}
handle->channel_info = \
(AirpcapChannelInfo *)malloc(sizeof(AirpcapChannelInfo) * handle->channel_info_count);
/* FIXME: how to set error from here? */
if (NULL == handle->channel_info) {
fprintf(stderr, "Unable to allocate AirpcapChannelInfo\n");
return NL_SKIP;
}
nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], band_rem) {
uint32_t frequency;
struct nlattr *tb_band_freqs;
nla_parse(tb_band, NL80211_BAND_ATTR_MAX,
nla_data(nl_band),
nla_len(nl_band),
NULL);
/* If we have an BAND_ATTR_HT_CAPA attribute, then
* we are 802.11n capable. */
if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
handle->cap.SupportedMedia |= AIRPCAP_MEDIUM_802_11_N;
}
nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rate_rem) {
uint32_t rate;
nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
nla_data(nl_rate),
nla_len(nl_rate),
rate_policy);
if (NULL == tb_rate[NL80211_BITRATE_ATTR_RATE])
continue;
/* If we support bit rates > 11.0 Kbps, we definitely
* support 802.11g. Not quite sure how to be sure
* that 802.11a or 802.11b modulations are supported... */
rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
if (rate > 110) { /* kbps */
handle->cap.SupportedMedia |= AIRPCAP_MEDIUM_802_11_G;
}
}
/* HT capable? */
handle->cap.SupportedMedia |= AIRPCAP_MEDIUM_802_11_B;
tb_band_freqs = tb_band[NL80211_BAND_ATTR_FREQS];
nla_for_each_nested(nl_freq, tb_band_freqs, freq_rem) {
PAirpcapChannelInfo info;
uint32_t max_tx_power;
nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
nla_data(nl_freq),
nla_len(nl_freq),
freq_policy);
if (NULL == tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
continue;
/* Ignore disabled frequencies (e.g., due to regulatory
* domain issues. */
if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
continue;
frequency = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
info = &handle->channel_info[freq_count];
info->Frequency = (UINT)frequency;
/* TODO */
info->ExtChannel = 0;
info->Flags = 0;
/* Must be {0, 0} according to airpcap docs */
memset(info->Reserved, 0, sizeof(info->Reserved));
info->_Private = (struct AirpcapAdapterChannelInfoPrivate *)malloc(sizeof(struct AirpcapAdapterChannelInfoPrivate));
info->_Private->max_tx_power = max_tx_power = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]);
freq_count++;
/* Add SupportedBands based on parsed frequencies. */
if (frequency >= 2412 && frequency <= 2484) {
handle->cap.SupportedBands |= AIRPCAP_BAND_2GHZ;
} else if (frequency >= 4915 && frequency <= 5825) {
handle->cap.SupportedBands |= AIRPCAP_BAND_5GHZ;
}
}
}
handle->cap.AdapterModelName = "nl80211-compatible PHY";
/* TODO: how to figure this out from the driver?
* Do we really need to fill this information in? */
handle->cap.AdapterBus = AIRPCAP_BUS_PCI_EXPRESS;
/* Exposed through PF_PACKET sockets. ALL mac80211 drivers
* support injection.
* TODO: Set this to FALSE for now, since we do not implement
* AirpcapWrite(). */
handle->cap.CanTransmit = FALSE;
handle->cap.CanSetTransmitPower = TRUE;
handle->cap.ExternalAntennaPlug = FALSE; // unknown
/* Identify what kind of "Airpcap" we are by our existing
* capabilities (e.g., transmit and 802.11n support).
*/
if (handle->cap.SupportedMedia & AIRPCAP_MEDIUM_802_11_N) {
handle->cap.AdapterId = \
handle->cap.CanTransmit ? AIRPCAP_ID_NX : AIRPCAP_ID_N;
} else {
handle->cap.AdapterId = \
handle->cap.CanTransmit ? AIRPCAP_ID_TX : AIRPCAP_ID_CLASSIC;
}
return NL_SKIP;
}
/* Adapted from hostapd/src/drivers/driver_nl80211.c. */
static int
nl_send_and_recv(struct nl_handle *sock,
struct nl_msg *msg,
nl_recvmsg_msg_cb_t valid_handler,
//int (*valid_handler)(struct nl_msg *, void *),
void *valid_data_ptr)
{
struct nl_cb *cb;
int err = 0;
cb = nl_cb_alloc(NL_CB_DEFAULT);
if (NULL == cb) {
goto send_and_recv_done;
}
err = nl_send_auto_complete(sock, msg);
if (err < 0)
goto send_and_recv_done;
err = 1;
/* Register a handler for error events. */
nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
/* Register a handler for finish events. */
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
/* Register a handler for ACK events. */
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
if (valid_handler) {
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
valid_handler, valid_data_ptr);
}
while (err > 0) {
nl_recvmsgs(sock, cb);
}
send_and_recv_done:
nl_cb_put(cb);
nlmsg_free(msg);
return err;
}
static int
nl80211_get_wiphy(struct nl_handle *sock,
struct genl_family *family,
PAirpcapHandle handle)
{
struct nl_msg *msg = nlmsg_alloc();
if (!msg) {
setebuf(handle->last_error, "Error allocating netlink message.");
return -1;
}
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ,
genl_family_get_id(family), 0,
/* Get ALL wireless PHY information. */
0, NL80211_CMD_GET_WIPHY, 0);
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, handle->phyindex);
int err = nl_send_and_recv(sock, msg, wiphy_match_handler, handle);
if (err < 0) {
setebuf(handle->last_error, "Error getting wiphy information from netlink.");
return -1;
}
return 0;
nla_put_failure:
setebuf(handle->last_error, "NL80211_GET_WIPHY: Error in NLA_PUT_U32.");
return -1;
}
static int
nl80211_device_init(PAirpcapHandle handle, PCHAR Ebuf)
{
int err;
struct nl_msg *msg;
msg = nlmsg_alloc();
if (!msg) {
setebuf(Ebuf, "Error allocating netlink message.");
return -1;
}
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ,
genl_family_get_id(handle->nl80211), 0,
/* Get wireless PHY information. */
/* Why does iw set NLM_F_DUMP here and
* still get only one interface?
* Even if I do NLM_F_MATCH with
* NL80211_ATTR_IFINDEX, I get every
* PHY. Only 0 works here... */
0, NL80211_CMD_GET_INTERFACE, 0);
/* We refer to the device by its interface index, not by
* the PHY interface. */
nla_put_u32(msg, NL80211_ATTR_IFINDEX, handle->ifindex);
struct airpcap_interface_dump_data data;
data.start = NULL;
data.current = NULL;
err = nl_send_and_recv(handle->nl_socket, msg,
interface_dump_handler, &data);
if (err < 0) {
setebuf(Ebuf, "Error getting device information from netlink: %s",
strerror(-err));
}
if (NULL == data.start) {
printf("No matching wiphy...\n");
} else {
struct airpcap_interface_list *iface = data.start;
handle->phyindex = iface->phyindex;
if (0 != nl80211_get_wiphy(handle->nl_socket,
handle->nl80211,
handle)) {
/* TODO: free memory */
printf("error getting wiphy: %s\n", handle->last_error);
}
}
struct airpcap_interface_list *iface = data.start;
while (NULL != iface) {
struct airpcap_interface_list *next = iface->next;
free(iface);
iface = next;
}
/* TODO : NL80211_CMD_GET_STATION for NL80211_ATTR_WIPHY_FREQ ? */
return err;
}
#ifdef USE_VIRTUAL_INTERFACES
static
int cmd_new_interface_handler(struct nl_msg *msg UNUSED, void *data UNUSED)
{
return NL_SKIP;
}
static
int cmd_del_interface_handler(struct nl_msg *msg UNUSED, void *data UNUSED)
{
return NL_SKIP;
}
/* adapted (lifted) from lorcon2 nl80211_create_vap */
static
int nl80211_create_monitor(PAirpcapHandle handle, PCHAR Ebuf)
{
struct nl_msg *msg;
int err;
/* Check if this interface already exists. */
handle->monitor_ifindex = if_nametoindex(handle->monitor_ifname);
if (0 != handle->monitor_ifindex) {
/* TODO: Check to make sure it is the correct wiphy and
* already in IFTYPE_MONITOR */
return 0;
}
msg = nlmsg_alloc();
if (NULL == msg) {
setebuf(Ebuf, "Failed to allocate netlink message.");
return -1;
}
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ,
genl_family_get_id(handle->nl80211), 0, 0,
NL80211_CMD_NEW_INTERFACE, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, handle->ifindex);
NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, handle->monitor_ifname);
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
err = nl_send_and_recv(handle->nl_socket, msg,
cmd_new_interface_handler,
NULL);
/* PROTIP: If you get -ENFILE (-23 / Too many open files in system),
* it's (likely) because the interface already exists.
* Why doesn't it return -EEXIST?!
*/
if (err < 0) {
nla_put_failure:
setebuf(Ebuf, "Failed to create monitor interface %s from %s: %s",
handle->monitor_ifname,
handle->master_ifname,
strerror(-err));
return -1;
}
/* Save this ifindex */
handle->monitor_ifindex = if_nametoindex(handle->monitor_ifname);
if (0 == handle->monitor_ifindex) {
setebuf(Ebuf,
"nl80211_create_monitor() thought we made a "
"monitor interface, but it wasn't there when we looked");
return -1;
}
return 0;
}
static
int nl80211_destroy_monitor(PAirpcapHandle handle)
{
unsigned monitor_ifindex = if_nametoindex(handle->monitor_ifname);
if (0 != monitor_ifindex) {
/* NL80211_CMD_DEL_INTERFACE
* - NL80211_ATTR_IFINDEX
*/
struct nl_msg *msg;
int err;
msg = nlmsg_alloc();
if (NULL == msg) {
setebuf(handle->last_error, "Failed to allocate netlink message.");
return -1;
}
genlmsg_put(msg, 0, 0,//NL_AUTO_PID, NL_AUTO_SEQ,
genl_family_get_id(handle->nl80211), 0, 0,
NL80211_CMD_DEL_INTERFACE, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, monitor_ifindex);
err = nl_send_and_recv(handle->nl_socket, msg,
cmd_del_interface_handler,
NULL);
if (err < 0) {
nla_put_failure:
/* -ESRCH == "No such process"...? if ifindex cannot be found */
setebuf(handle->last_error,
"Failed to delete monitor interface %s(%u): %s",
handle->monitor_ifname, monitor_ifindex,
strerror(-err));
return -1;
}
handle->monitor_ifindex = 0;
return 0;
}
return -2;
}
#endif
static
int cmd_set_monitor_handler(struct nl_msg *msg UNUSED, void *data UNUSED)
{
return NL_SKIP;
}
static
int nl80211_set_monitor(PAirpcapHandle handle, AirpcapValidationType validation, PCHAR Ebuf)
{
struct nl_msg *msg, *flags;
int err;
msg = nlmsg_alloc();
if (NULL == msg) {
setebuf(Ebuf, "Failed to allocate netlink message.");
return -1;
}
flags = nlmsg_alloc();
if (NULL == flags) {
setebuf(Ebuf, "Failed to allocate flags netlink message.");
return -1;
}
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ,
genl_family_get_id(handle->nl80211), 0, 0,
NL80211_CMD_SET_INTERFACE, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, handle->ifindex);
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
/* We always want other BSS frames and control frames. */
NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_CONTROL);
NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_OTHER_BSS);
/* If we want to accept EVERYTHING, we need to accept FCS-failed
* frames. Not sure if we should accept PLCP-failed frames, but
* everything is everything, right? */
switch (validation) {
case AIRPCAP_VT_ACCEPT_EVERYTHING:
NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_FCSFAIL);
NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_PLCPFAIL);
break;
case AIRPCAP_VT_ACCEPT_CORRECT_FRAMES:
/* No flags */
break;
case AIRPCAP_VT_ACCEPT_CORRUPT_FRAMES:
/* TODO: this may be complicated - FLAG_COOK_FRAMES means frames
* after processing (i.e., not consumed by any other interface), so
* we COULD hack this in by creating an interface that does
* AIRPCAP_VT_ACCEPT_CORRECT and make this MNTR_FLAG_COOK_FRAMES with
* MNTR_FLAG_FCSFAIL/PLCPFAIL. That MAY let us only see corrupt frames.
*
* For now, do nothing.
*/
break;
default:
setebuf(Ebuf, "Invalid validation type %d used - bug!", validation);
goto nla_put_failure;
}
nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
err = nl_send_and_recv(handle->nl_socket, msg,
cmd_set_monitor_handler,
NULL);
if (err < 0) {
setebuf(Ebuf, "Failed to set interface %s to monitor mode: %s",
handle->master_ifname,
strerror(-err));
return -1;
}
handle->validation = validation;
return 0;
nla_put_failure:
nlmsg_free(msg);
nlmsg_free(flags);
return -1;
}
/** INTERNAL struct _AirpcapHandle allocator. */
static PAirpcapHandle
airpcap_handle_new(void)
{
PAirpcapHandle handle;
handle = (PAirpcapHandle)malloc(sizeof(struct _AirpcapHandle));
if (NULL == handle) {
return NULL;
}
memset(handle, 0, sizeof(*handle));
return handle;
}
/** INTERNAL struct _AirpcapHandle destructor. */
static void
airpcap_handle_free(PAirpcapHandle handle)
{
if (NULL != handle->channel_info) {
UINT i;
for (i = 0; i < handle->channel_info_count; i++) {
free(handle->channel_info[i]._Private);
handle->channel_info[i]._Private = NULL;
}
free(handle->channel_info);
handle->channel_info = NULL;
}
free(handle);
}
/* TODO:
* - Check if interface supports monitor mode (NL80211_ATTR_SUPPORTED_IFTYPES)
* at all - in GET_WIPHY? seems to make sense for it to be there.
* - Check if interface is NL80211_ATTR_IFTYPE
* NL80211_IFTYPE_MONITOR (use NL80211_CMD_GET_INTERFACE)
* - If interface is NOT IFTYPE_MONITOR, search all interfaces
* for the same WIPHY index, maybe with NLM_F_DUMP and try to find
* an existing one.
* Add a flag in AirpcapHandle internally to make this easier?
* - If no monitor interface is defined, create one.
* Attempt to destroy monitor interface on close? onexit()?
*/
PAirpcapHandle AirpcapOpen(PCHAR DeviceName, PCHAR Ebuf)
{
PAirpcapHandle handle;
if (NULL != Ebuf) {
Ebuf[0] = 0;
}
unsigned ifindex = if_nametoindex((char *)DeviceName);
if (ifindex <= 0) {
setebuf(Ebuf, "Invalid device specified.");
return NULL;
}
handle = airpcap_handle_new();
if (NULL == handle) {
setebuf(Ebuf, "Error allocating handle.");
return NULL;
}
/* Assign interface index after allocation. */
handle->ifindex = ifindex;
/* FIXME: handle if name + "mon" is too long for IF_NAMESIZE. */
strncpy(handle->master_ifname, DeviceName, IF_NAMESIZE);
/* Initialize unique netlink/nl80211 connection and
* state for this handle. */
if (-1 == nl80211_state_init(handle, Ebuf)) {
return NULL;
}
/* FIXME: proper deallocation. */
if (-1 == nl80211_device_init(handle, Ebuf)) {
return NULL;
}
/* if (-1 == nl80211_create_monitor(handle, Ebuf)) { */
/* return NULL; */
/* } */
/* By default, we must set to accept everything, as noted is the default.
*/
if (-1 == nl80211_set_monitor(handle, AIRPCAP_VT_ACCEPT_EVERYTHING, Ebuf)) {
/* We might as well just call close here, since
* all state is essentially allocated and ready.
*/
AirpcapClose(handle);
return NULL;
}
/* Finally, bring the device up (and leave it up - do not bring
* down the interface in AirpcapClose!). */
if (-1 == ifconfig_ifupdown(handle->master_ifname, 1)) {
setebuf(Ebuf, "Unable to bring interface up: %s", strerror(errno));
AirpcapClose(handle);
return NULL;
}
if (FALSE == AirpcapSetDeviceChannel(handle, 6)) {
if (NULL != Ebuf) {
strncpy(Ebuf, handle->last_error, AIRPCAP_ERRBUF_SIZE);
Ebuf[AIRPCAP_ERRBUF_SIZE] = 0;
}
AirpcapClose(handle);
return NULL;
}
return handle;
}
static
void nl80211_state_free(PAirpcapHandle handle)
{
if (handle) {
nl_cb_put(handle->nl_cb);
genl_family_put(handle->nl80211);
nl_cache_free(handle->nl_cache);
nl_handle_destroy(handle->nl_socket);
}
}
VOID AirpcapClose(PAirpcapHandle AdapterHandle)
{
if (NULL != AdapterHandle) {
/* For now, don't destroy the monitor, to allow phyNmon interfaces
* to persist after AirpcapClose. Perhaps add a libairpcap-nl API call
* to destroy the VIF? lorcon does not destroy the interface, in any
* case. */
//nl80211_destroy_monitor(AdapterHandle);
nl80211_state_free(AdapterHandle);
if (AdapterHandle->rtnl_link_cache)
nl_cache_free(AdapterHandle->rtnl_link_cache);
airpcap_handle_free(AdapterHandle);
}
}
PCHAR AirpcapGetLastError(PAirpcapHandle AdapterHandle)
{
PCHAR ret = NULL;
if (AdapterHandle) {
ret = AdapterHandle->last_error;
}
return ret;
}
static PAirpcapDeviceDescription
nl80211_get_all_devices(PCHAR Ebuf)
{
int err = 0;
struct nl_msg *msg;
struct nl_handle *sock = NULL;
struct nl_cache *cache = NULL;
struct genl_family *nl80211 = NULL;
PAirpcapDeviceDescription desc_start = NULL, desc_current;
sock = nl_handle_alloc();
/* Allocate the netlink socket.
*/
if (NULL == sock) {
setebuf(Ebuf, "Failed to allocate netlink socket.");
goto Lerr;
}
/* Connect to the generic netlink.
*/
if (genl_connect(sock)) {
setebuf(Ebuf, "Failed to connect to generic netlink.");
goto Lerr;
}
if (genl_ctrl_alloc_cache(sock, &cache)) {
setebuf(Ebuf, "Failed to allocate generic netlink cache.");
goto Lerr;
}
/* Find and get a reference to the nl80211 family.
* Must hand back the reference via genl_family_put. */
nl80211 = genl_ctrl_search_by_name(cache, "nl80211");
if (NULL == nl80211) {
setebuf(Ebuf, "Netlink module nl80211 not found.");
goto Lerr;
}
msg = nlmsg_alloc();
if (!msg) {
setebuf(Ebuf, "Error allocating netlink message.");
goto Lerr;
}
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ,
genl_family_get_id(nl80211), 0,
/* Get ALL wireless PHY information. */
NLM_F_DUMP, NL80211_CMD_GET_INTERFACE, 0);
/* Build up the list */
struct airpcap_interface_dump_data data;
data.start = NULL;
data.current = NULL;
err = nl_send_and_recv(sock, msg, interface_dump_handler, &data);
if (err < 0) {
setebuf(Ebuf, "Error getting interface information from netlink: %s",
strerror(-err));
}
for (struct airpcap_interface_list *iface = data.start; NULL != iface; iface = iface->next) {
PAirpcapDeviceDescription desc;
PAirpcapHandle temp_handle = NULL;
char ifname[IF_NAMESIZE];
PCHAR d;
if (NULL == if_indextoname(iface->ifindex, ifname)) {
printf("BAD!!!\n");
continue;
}
desc = (PAirpcapDeviceDescription)malloc(sizeof(*desc));
desc->next = NULL;
/* Update the list */
if (NULL == desc_start) {
desc_start = desc_current = desc;
} else {
desc_current->next = desc;
desc_current = desc;
}
temp_handle = airpcap_handle_new();
temp_handle->phyindex = iface->phyindex;
if (0 != nl80211_get_wiphy(sock, nl80211, temp_handle)) {
/* TODO: free memory, etc... */
printf("error getting wiphy: %s\n", temp_handle->last_error);
strncpy(Ebuf, temp_handle->last_error, AIRPCAP_ERRBUF_SIZE);
return NULL;
}
desc->Name = strndup(ifname, IF_NAMESIZE);
desc->Description = (PCHAR)malloc(512);
/* Assign Description member based on what
* Airpcap device we are going to "emulate".
*
* This should hopefully someday be filled in with better
* information about the adapter or driver from the
* mac80211 / nl80211 layer. */
switch (temp_handle->cap.AdapterId) {
case AIRPCAP_ID_N:
case AIRPCAP_ID_NX:
d = "Airpcap NX emulation (802.11n)";
break;
case AIRPCAP_ID_TX:
d = "Airpcap TX emulation (802.11bg)";
break;
case AIRPCAP_ID_CLASSIC:
d = "Airpcap Classic emulation (802.11bg)";
break;
default:
d = "BUG: Unspecified Airpcap emulation";
break;
}
strncpy(desc->Description, d, 512);
if (temp_handle->cap.SupportedBands & AIRPCAP_BAND_5GHZ) {
size_t s = strlen(desc->Description);
strncat(desc->Description,
" (5 GHz)", 512 - s);
}
/* Free the temporary handle. */
airpcap_handle_free(temp_handle);
}
struct airpcap_interface_list *iface = data.start;
while (NULL != iface) {
struct airpcap_interface_list *next = iface->next;
free(iface);
iface = next;
}
Lerr:
if (nl80211)
genl_family_put(nl80211);
if (cache)