forked from boundary/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airpcap_loader.c
2554 lines (2157 loc) · 75.1 KB
/
airpcap_loader.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
/* airpcap_loader.c
*
* $Id$
*
* Giorgio Tino <giorgio.tino@cacetech.com>
* Copyright (c) CACE Technologies, LLC 2006
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 2000 Gerald Combs
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#ifdef HAVE_AIRPCAP
#ifdef HAVE_LIBPCAP
#include <glib.h>
#include <gmodule.h>
#include <wtap.h>
#include <pcap.h>
#endif
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/prefs-int.h>
#include <epan/uat-int.h>
#include <epan/dissectors/packet-ieee80211.h>
#include <epan/crypt/wep-wpadefs.h>
#include <epan/crypt/airpdcap_ws.h>
#include <epan/strutil.h>
#include <epan/frequency-utils.h>
#include "capture_ui_utils.h"
#include <wsutil/file_util.h>
#include "ui/simple_dialog.h"
#include <airpcap.h>
#include "airpcap_loader.h"
/*
* Set to TRUE if the DLL was successfully loaded AND all functions
* are present.
*/
static gboolean AirpcapLoaded = FALSE;
#ifdef _WIN32
/*
* We load dynamically the dag library in order link it only when
* it's present on the system
*/
static void * AirpcapLib = NULL;
static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
static AirpcapSetFilterHandler g_PAirpcapSetFilter;
static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
static AirpcapReadHandler g_PAirpcapRead;
static AirpcapGetStatsHandler g_PAirpcapGetStats;
#endif
static int AirpcapVersion = 3;
static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
static AirpcapOpenHandler g_PAirpcapOpen;
static AirpcapCloseHandler g_PAirpcapClose;
static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
static AirpcapGetVersionHandler g_PAirpcapGetVersion;
static AirpcapSetDeviceChannelExHandler g_PAirpcapSetDeviceChannelEx;
static AirpcapGetDeviceChannelExHandler g_PAirpcapGetDeviceChannelEx;
static AirpcapGetDeviceSupportedChannelsHandler g_PAirpcapGetDeviceSupportedChannels;
/* Airpcap interface list */
GList *airpcap_if_list = NULL;
/* Airpcap current selected interface */
airpcap_if_info_t *airpcap_if_selected = NULL;
/* Airpcap current active interface */
airpcap_if_info_t *airpcap_if_active = NULL;
/* WLAN preferences pointer */
module_t *wlan_prefs = NULL;
Dot11Channel *pSupportedChannels;
guint numSupportedChannels;
static AirpcapChannelInfo LegacyChannels[] =
{
{2412, 0, {0,0,0}},
{2417, 0, {0,0,0}},
{2422, 0, {0,0,0}},
{2427, 0, {0,0,0}},
{2432, 0, {0,0,0}},
{2437, 0, {0,0,0}},
{2442, 0, {0,0,0}},
{2447, 0, {0,0,0}},
{2452, 0, {0,0,0}},
{2457, 0, {0,0,0}},
{2462, 0, {0,0,0}},
{2467, 0, {0,0,0}},
{2472, 0, {0,0,0}},
{2484, 0, {0,0,0}},
};
static guint num_legacy_channels = 14;
/*
* Callback used by the load_wlan_keys() routine in order to read a WEP decryption key
*/
static guint
get_wep_key(pref_t *pref, gpointer ud)
{
gchar *key_string = NULL;
guint8 key_type = AIRPDCAP_KEY_TYPE_WEP;
keys_cb_data_t* user_data;
uat_t *uat;
guint i;
const char* err = NULL;
uat_wep_key_record_t* wep_keys;
decryption_key_t* new_key;
/* Retrieve user data info */
user_data = (keys_cb_data_t*)ud;
if (g_ascii_strcasecmp(pref->name, "wep_key_table") == 0 && pref->type == PREF_UAT)
{
uat = pref->varp.uat;
/* This is just a sanity check. UAT should be loaded */
if (!uat->loaded)
{
uat_load(uat, &err);
if (err != NULL)
return 1;
}
for (i = 0, wep_keys = (uat_wep_key_record_t*)*uat->user_ptr; i < *uat->nrows_p; i++, wep_keys++)
{
/* strip out key type if present */
if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WEP ":", 4) == 0) {
key_type = AIRPDCAP_KEY_TYPE_WEP;
key_string = (gchar*)wep_keys->string+4;
}
else if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WPA_PWD ":", 8) == 0) {
key_string = (gchar*)wep_keys->string+8;
key_type = AIRPDCAP_KEY_TYPE_WPA_PWD;
}
else if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WPA_PSK ":", 8) == 0) {
key_string = (gchar*)wep_keys->string+8;
key_type = AIRPDCAP_KEY_TYPE_WPA_PSK;
}
else {
key_type = wep_keys->key;
key_string = (gchar*)wep_keys->string;
}
/* Here we have the string describing the key... */
new_key = parse_key_string(key_string, key_type);
if (new_key != NULL)
{
/* Key is added only if not null ... */
user_data->list = g_list_append(user_data->list,new_key);
user_data->number_of_keys++;
user_data->current_index++;
}
}
}
return 0;
}
/* Returs TRUE if the WEP key is valid, false otherwise */
gboolean
wep_key_is_valid(char* key)
{
GString *new_key_string;
guint i=0;
if (key == NULL)
return FALSE;
new_key_string = g_string_new(key);
if ( ((new_key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((new_key_string->len) < 2))
{
g_string_free(new_key_string,FALSE);
return FALSE;
}
if ((new_key_string->len % 2) != 0)
{
g_string_free(new_key_string,FALSE);
return FALSE;
}
for(i = 0; i < new_key_string->len; i++)
{
if (!g_ascii_isxdigit(new_key_string->str[i]))
{
g_string_free(new_key_string,FALSE);
return FALSE;
}
}
g_string_free(new_key_string,FALSE);
return TRUE;
}
/* Callback used by the save_wlan_keys() routine in order to write a decryption key */
static guint
set_wep_key(pref_t *pref, gpointer ud _U_)
{
keys_cb_data_t* user_data;
uat_t *uat;
gint i;
const char* err = NULL;
uat_wep_key_record_t uat_key;
decryption_key_t* new_key;
/* Retrieve user data info */
user_data = (keys_cb_data_t*)ud;
if (g_ascii_strcasecmp(pref->name, "wep_key_table") == 0 && pref->type == PREF_UAT)
{
uat = pref->varp.uat;
if (!uat->loaded)
{
/* UAT will only be loaded if previous keys exist, so it may need
to be loaded now */
uat_load(uat, &err);
if (err != NULL)
return 1;
uat->loaded = 1;
}
/* Free the old records */
uat_clear(uat);
for (i = 0; i < user_data->number_of_keys; i++)
{
new_key = (decryption_key_t*)g_list_nth_data(user_data->list,i);
uat_key.string = get_key_string(new_key);
uat_key.key = new_key->type;
uat_add_record(uat, &uat_key, TRUE);
}
uat_save(uat, &err);
if (err != NULL)
return 1;
}
return 0;
}
/*
* Function used to read the Decryption Keys from the preferences and store them
* properly into the airpcap adapter.
*/
gboolean
load_wlan_driver_wep_keys(void)
{
keys_cb_data_t* user_data;
guint i;
/* Retrieve the wlan preferences */
wlan_prefs = prefs_find_module("wlan");
/* Allocate a structure used to keep infos between the callbacks */
user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
/* Fill the structure */
user_data->list = NULL;
user_data->current_index = 0;
user_data->number_of_keys= 0; /* Still unknown */
/* Run the callback on each 802.11 preference */
prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
/* Now the key list should be filled */
/*
* Signal that we've changed things, and run the 802.11 dissector's
* callback
*/
wlan_prefs->prefs_changed = TRUE;
prefs_apply(wlan_prefs);
write_wlan_driver_wep_keys_to_registry(user_data->list);
/* FREE MEMORY */
/* free the WEP key string */
for(i=0;i<g_list_length(user_data->list);i++)
{
g_free(g_list_nth(user_data->list,i)->data);
}
/* free the (empty) list */
g_list_free(user_data->list);
/* free the user_data structure */
g_free(user_data);
/* airpcap_if_info_free(fake_info_if); */
return TRUE;
}
/*
* This function will tell the airpcap driver the key list to use
* This will be stored into the registry...
*/
gboolean
write_wlan_wep_keys_to_registry(airpcap_if_info_t* info_if, GList* key_list)
{
guint i,j;
GString *new_key;
gchar s[3];
PAirpcapKeysCollection KeysCollection;
guint KeysCollectionSize;
guint8 KeyByte;
guint keys_in_list = 0;
decryption_key_t* key_item = NULL;
keys_in_list = g_list_length(key_list);
/*
* Calculate the size of the keys collection
*/
KeysCollectionSize = (guint)AirpcapKeysCollectionSize(keys_in_list);
/*
* Allocate the collection
*/
KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
if (!KeysCollection)
{
return FALSE;
}
/*
* Populate the key collection
*/
KeysCollection->nKeys = keys_in_list;
for(i = 0; i < keys_in_list; i++)
{
KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
/* Retrieve the Item corresponding to the i-th key */
key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
new_key = g_string_new(key_item->key->str);
KeysCollection->Keys[i].KeyLen = (guint) new_key->len / 2;
memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
for(j = 0 ; j < new_key->len; j += 2)
{
s[0] = new_key->str[j];
s[1] = new_key->str[j+1];
s[2] = '\0';
KeyByte = (guint8)strtol(s, NULL, 16);
KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
}
g_string_free(new_key,TRUE);
}
/*
* Free the old adapter key collection!
*/
if (info_if->keysCollection != NULL)
g_free(info_if->keysCollection);
/*
* Set this collection ad the new one
*/
info_if->keysCollection = KeysCollection;
info_if->keysCollectionSize = KeysCollectionSize;
/*
* Configuration must be saved
*/
info_if->saved = FALSE;
/*
* Write down the changes to the registry
*/
airpcap_save_selected_if_configuration(info_if);
return TRUE;
}
/*
* This function will tell the airpcap driver the key list to use
* This will be stored into the registry...
*/
gboolean
write_wlan_driver_wep_keys_to_registry(GList* key_list)
{
guint i,j,k,n,y;
GString *new_key;
gchar s[3];
PAirpcapKeysCollection KeysCollection;
guint KeysCollectionSize;
guint8 KeyByte;
guint keys_in_list = 0;
decryption_key_t* key_item = NULL;
airpcap_if_info_t* fake_info_if = NULL;
/* Create the fake_info_if from the first adapter of the list */
fake_info_if = airpcap_driver_fake_if_info_new();
if (fake_info_if == NULL)
return FALSE;
/*
* XXX - When WPA will be supported, change this to: keys_in_list = g_list_length(key_list);
* but right now we will have to count only the WEP keys (or we will have a malloc-mess :-) )
*/
n = g_list_length(key_list);
for(k = 0; k < n; k++ )
if (((decryption_key_t*)g_list_nth_data(key_list,k))->type == AIRPDCAP_KEY_TYPE_WEP)
keys_in_list++;
/*
* Calculate the size of the keys collection
*/
KeysCollectionSize = (guint)AirpcapKeysCollectionSize(keys_in_list);
/*
* Allocate the collection
*/
KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
if (!KeysCollection)
{
return FALSE;
}
/*
* Populate the key collection
*/
KeysCollection->nKeys = keys_in_list;
/*
* XXX - If we have, let's say, six keys, the first three are WEP, then two are WPA, and the
* last is WEP, we have to scroll the whole list (n) but increment the array counter only
* when a WEP key is found (y) .. When WPA will be supported by the driver, I'll have to change
* this
*/
y = 0; /* Current position in the key list */
for(i = 0; i < n; i++)
{
/* Retrieve the Item corresponding to the i-th key */
key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
/*
* XXX - The AIRPDCAP_KEY_TYPE_WEP is the only supported right now!
* We will have to modify the AirpcapKey structure in order to
* support the other two types! What happens now, is that simply the
* not supported keys will just be discarded (they will be saved in Wireshark though)
*/
if (key_item->type == AIRPDCAP_KEY_TYPE_WEP)
{
KeysCollection->Keys[y].KeyType = AIRPDCAP_KEY_TYPE_WEP;
new_key = g_string_new(key_item->key->str);
KeysCollection->Keys[y].KeyLen = (guint) new_key->len / 2;
memset(&KeysCollection->Keys[y].KeyData, 0, sizeof(KeysCollection->Keys[y].KeyData));
for(j = 0 ; j < new_key->len; j += 2)
{
s[0] = new_key->str[j];
s[1] = new_key->str[j+1];
s[2] = '\0';
KeyByte = (guint8)strtol(s, NULL, 16);
KeysCollection->Keys[y].KeyData[j / 2] = KeyByte;
}
/* XXX - Change when WPA will be supported!!! */
y++;
g_string_free(new_key,TRUE);
}
else if (key_item->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
{
/* XXX - The driver cannot deal with this kind of key yet... */
}
else if (key_item->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
{
/* XXX - The driver cannot deal with this kind of key yet... */
}
}
/*
* Free the old adapter key collection!
*/
if (fake_info_if->keysCollection != NULL)
g_free(fake_info_if->keysCollection);
/*
* Set this collection ad the new one
*/
fake_info_if->keysCollection = KeysCollection;
fake_info_if->keysCollectionSize = KeysCollectionSize;
/*
* Configuration must be saved
*/
fake_info_if->saved = FALSE;
/*
* Write down the changes to the registry
*/
airpcap_save_driver_if_configuration(fake_info_if);
airpcap_if_info_free(fake_info_if);
return TRUE;
}
/*
* Function used to save to the preference file the Decryption Keys.
*/
int
save_wlan_driver_wep_keys(void)
{
GList* key_list = NULL;
char* tmp_key = NULL;
guint keys_in_list,i;
keys_cb_data_t* user_data;
airpcap_if_info_t* fake_info_if = NULL;
/* Create the fake_info_if from the first adapter of the list */
fake_info_if = airpcap_driver_fake_if_info_new();
if (fake_info_if == NULL)
return 0;
/* Retrieve the wlan preferences */
wlan_prefs = prefs_find_module("wlan");
/* Allocate a structure used to keep infos between the callbacks */
user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
/* Number of keys in key list */
if (fake_info_if->keysCollectionSize != 0)
keys_in_list = AirpcapKeysCollectionSizeToKeyCount(fake_info_if->keysCollectionSize);
else
keys_in_list = 0;
for(i=0; i<keys_in_list; i++)
{
/* Only if it is a WEP key... */
if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
{
tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
key_list = g_list_append(key_list,g_strdup(tmp_key));
g_free(tmp_key);
}
}
/* Now we know the exact number of WEP keys in the list, so store it ... */
keys_in_list = g_list_length(key_list);
/* Fill the structure */
user_data->list = key_list;
user_data->current_index = 0;
user_data->number_of_keys= keys_in_list;
/* Retrieve the wlan preferences */
wlan_prefs = prefs_find_module("wlan");
/* Run the callback on each 802.11 preference */
prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
/* Signal that we've changed things, and run the 802.11 dissector's
* callback */
wlan_prefs->prefs_changed = TRUE;
/* Apply changes for the specified preference */
prefs_apply(wlan_prefs);
/* FREE MEMORY */
/* free the WEP key string */
for(i=0;i<g_list_length(user_data->list);i++)
{
g_free(g_list_nth(user_data->list,i)->data);
}
/* free the (empty) list */
g_list_free(user_data->list);
/* free the user_data structure */
g_free(user_data);
airpcap_if_info_free(fake_info_if);
return keys_in_list;
}
/*
* Function used to save to the preference file the Decryption Keys.
*/
int
save_wlan_wireshark_wep_keys(GList* key_ls)
{
GList* key_list = NULL;
guint keys_in_list,i;
keys_cb_data_t* user_data;
decryption_key_t* tmp_dk;
/* Retrieve the wlan preferences */
wlan_prefs = prefs_find_module("wlan");
/* Allocate a structure used to keep infos between the callbacks */
user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
keys_in_list = g_list_length(key_ls);
key_list = key_ls;
/* Fill the structure */
user_data->list = key_list;
user_data->current_index = 0;
user_data->number_of_keys= keys_in_list;
/* Retrieve the wlan preferences */
wlan_prefs = prefs_find_module("wlan");
/* Run the callback on each 802.11 preference */
prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
/* Signal that we've changed things, and run the 802.11 dissector's
* callback */
wlan_prefs->prefs_changed = TRUE;
/* Apply changes for the specified preference */
prefs_apply(wlan_prefs);
/* FREE MEMORY */
/* free the WEP key string */
for(i=0;i<g_list_length(user_data->list);i++)
{
tmp_dk = (decryption_key_t*)g_list_nth(user_data->list,i)->data;
g_string_free(tmp_dk->key,TRUE);
if (tmp_dk->ssid != NULL) g_byte_array_free(tmp_dk->ssid,TRUE);
}
/* free the (empty) list */
g_list_free(user_data->list);
/* free the user_data structure */
g_free(user_data);
return keys_in_list;
}
/*
* Get an error message string for a CANT_GET_INTERFACE_LIST error from
* "get_airpcap_interface_list()".
*/
static gchar *
cant_get_airpcap_if_list_error_message(const char *err_str)
{
return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
}
/*
* Airpcap wrapper, used to store the current settings for the selected adapter
*/
gboolean
airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
{
if (!AirpcapLoaded) return FALSE;
return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
}
/*
* Airpcap wrapper, used to open an airpcap adapter
*/
PAirpcapHandle
airpcap_if_open(gchar * name, gchar * err)
{
if (!AirpcapLoaded) return NULL;
if (name == NULL) return NULL;
return g_PAirpcapOpen(name,err);
}
/*
* Airpcap wrapper, used to close an airpcap adapter
*/
void
airpcap_if_close(PAirpcapHandle handle)
{
if (!AirpcapLoaded) return;
g_PAirpcapClose(handle);
}
/*
* Retrieve the state of the Airpcap DLL
*/
int
airpcap_get_dll_state(void)
{
return AirpcapVersion;
}
/*
* Airpcap wrapper, used to turn on the led of an airpcap adapter
*/
gboolean
airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, guint LedNumber)
{
if (!AirpcapLoaded) return FALSE;
return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
}
/*
* Airpcap wrapper, used to turn off the led of an airpcap adapter
*/
gboolean
airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, guint LedNumber)
{
if (!AirpcapLoaded) return FALSE;
return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
}
/*
* Airpcap wrapper, used to get the channel of an airpcap adapter
*/
gboolean
airpcap_if_get_device_channel(PAirpcapHandle ah, guint * ch)
{
if (!AirpcapLoaded) return FALSE;
return g_PAirpcapGetDeviceChannel(ah,ch);
}
/*
* Airpcap wrapper, used to get the supported channels of an airpcap adapter
*/
gboolean
airpcap_if_get_device_supported_channels(PAirpcapHandle ah, AirpcapChannelInfo **cInfo, guint * nInfo)
{
if (!AirpcapLoaded) return FALSE;
if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD) {
*nInfo = num_legacy_channels;
*cInfo = (AirpcapChannelInfo*)&LegacyChannels;
return TRUE;
} else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK) {
return g_PAirpcapGetDeviceSupportedChannels(ah, cInfo, nInfo);
}
return FALSE;
}
/*
* Airpcap wrapper, used to get the supported channels of an airpcap adapter
*/
Dot11Channel*
airpcap_if_get_device_supported_channels_array(PAirpcapHandle ah, guint * pNumSupportedChannels)
{
AirpcapChannelInfo *chanInfo;
guint i=0, j=0, numInfo = 0;
if (!AirpcapLoaded)
return NULL;
if (airpcap_if_get_device_supported_channels(ah, &chanInfo, &numInfo) == FALSE)
return NULL;
numSupportedChannels = 0;
/*
* allocate a bigger array
*/
if (numInfo == 0)
return NULL;
pSupportedChannels = (Dot11Channel *)g_malloc(numInfo * (sizeof *pSupportedChannels));
for (i = 0; i < numInfo; i++)
{
guint supportedChannel = G_MAXUINT;
/*
* search if we have it already
*/
for (j = 0; j < numSupportedChannels; j++)
{
if (pSupportedChannels[j].Frequency == chanInfo[i].Frequency)
{
supportedChannel = j;
break;
}
}
if (supportedChannel == G_MAXUINT)
{
/*
* not found, create a new item
*/
pSupportedChannels[numSupportedChannels].Frequency = chanInfo[i].Frequency;
switch(chanInfo[i].ExtChannel)
{
case -1:
pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_LOW;
break;
case +1:
pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_HIGH;
break;
case 0:
default:
pSupportedChannels[numSupportedChannels].Flags = 0;
}
/*
* Gather channel information
*/
pSupportedChannels[numSupportedChannels].Flags |=
FREQ_IS_BG(pSupportedChannels[numSupportedChannels].Frequency) ?
FLAG_IS_BG_CHANNEL : FLAG_IS_A_CHANNEL;
pSupportedChannels[numSupportedChannels].Channel =
ieee80211_mhz_to_chan(pSupportedChannels[numSupportedChannels].Frequency);
numSupportedChannels++;
}
else
{
/*
* just update the ext channel flags
*/
switch(chanInfo[i].ExtChannel)
{
case -1:
pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_LOW;
break;
case +1:
pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_HIGH;
break;
case 0:
default:
break;
}
}
}
if (numSupportedChannels < 1)
return NULL;
/*
* Now sort the list by frequency
*/
for (i = 0 ; i < numSupportedChannels - 1; i++)
{
for (j = i + 1; j < numSupportedChannels; j++)
{
if (pSupportedChannels[i].Frequency > pSupportedChannels[j].Frequency)
{
Dot11Channel temp = pSupportedChannels[i];
pSupportedChannels[i] = pSupportedChannels[j];
pSupportedChannels[j] = temp;
}
}
}
*pNumSupportedChannels = numSupportedChannels;
return pSupportedChannels;
}
/*
* Airpcap wrapper, used to set the channel of an airpcap adapter
*/
gboolean
airpcap_if_set_device_channel(PAirpcapHandle ah, guint ch)
{
if (!AirpcapLoaded) return FALSE;
return g_PAirpcapSetDeviceChannel(ah,ch);
}
/*
* Airpcap wrapper, used to set the frequency of an airpcap adapter
*/
gboolean
airpcap_if_set_device_channel_ex(PAirpcapHandle ah, AirpcapChannelInfo ChannelInfo)
{
if (!AirpcapLoaded) return FALSE;
if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
gint channel = 0;
channel = ieee80211_mhz_to_chan(ChannelInfo.Frequency);
if (channel < 0){
return FALSE;
} else {
return airpcap_if_set_device_channel(ah, channel);
}
} else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
return g_PAirpcapSetDeviceChannelEx (ah, ChannelInfo);
}
return FALSE;
}
/*
* Airpcap wrapper, used to get the frequency of an airpcap adapter
*/
gboolean
airpcap_if_get_device_channel_ex(PAirpcapHandle ah, PAirpcapChannelInfo pChannelInfo)
{
if (!AirpcapLoaded) return FALSE;
pChannelInfo->Frequency = 0;
pChannelInfo->ExtChannel = 0;
pChannelInfo->Reserved[0] = 0;
pChannelInfo->Reserved[1] = 0;
pChannelInfo->Reserved[2] = 0;
if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
guint channel = 0;
guint chan_freq = 0;
if (!airpcap_if_get_device_channel(ah, &channel)) return FALSE;
chan_freq = ieee80211_chan_to_mhz(channel, TRUE);
if (chan_freq == 0) return FALSE;
pChannelInfo->Frequency = chan_freq;
return TRUE;
} else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
return g_PAirpcapGetDeviceChannelEx (ah, pChannelInfo);
}
return FALSE;
}
/*
* Airpcap wrapper, used to get the link type of an airpcap adapter
*/
gboolean
airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
{
if (!AirpcapLoaded) return FALSE;
return g_PAirpcapGetLinkType(ah,lt);
}
/*
* Airpcap wrapper, used to set the link type of an airpcap adapter
*/
gboolean
airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
{
if (!AirpcapLoaded) return FALSE;
return g_PAirpcapSetLinkType(ah,lt);
}
/*
* Airpcap wrapper, used to get the fcs presence of an airpcap adapter
*/
gboolean
airpcap_if_get_fcs_presence(PAirpcapHandle ah, gboolean * fcs)
{
if (!AirpcapLoaded) return FALSE;
return g_PAirpcapGetFcsPresence(ah,fcs);
}