-
Notifications
You must be signed in to change notification settings - Fork 39
/
iwspy.c
376 lines (332 loc) · 10 KB
/
iwspy.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
/*
* Wireless Tools
*
* Jean II - HPLB '99 - HPL 99->04
*
* This tool can manipulate the spy list : add addresses and display stat
* You need to link this code against "iwlib.c" and "-lm".
*
* This file is released under the GPL license.
* Copyright (c) 1997-2004 Jean Tourrilhes <jt@hpl.hp.com>
*/
#include "iwlib-private.h" /* Private header */
/************************* DISPLAY ROUTINES **************************/
/*------------------------------------------------------------------*/
/*
* Display the spy list of addresses and the associated stats
*/
static int
print_spy_info(int skfd,
char * ifname,
char * args[],
int count)
{
struct iwreq wrq;
char buffer[(sizeof(struct iw_quality) +
sizeof(struct sockaddr)) * IW_MAX_SPY];
char temp[128];
struct sockaddr * hwa;
struct iw_quality * qual;
iwrange range;
int has_range = 0;
int n;
int i;
/* Avoid "Unused parameter" warning */
args = args; count = count;
/* Collect stats */
wrq.u.data.pointer = (caddr_t) buffer;
wrq.u.data.length = IW_MAX_SPY;
wrq.u.data.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
{
fprintf(stderr, "%-8.16s Interface doesn't support wireless statistic collection\n\n", ifname);
return(-1);
}
/* Number of addresses */
n = wrq.u.data.length;
/* Check if we have valid mac address type */
if(iw_check_mac_addr_type(skfd, ifname) < 0)
{
fprintf(stderr, "%-8.16s Interface doesn't support MAC addresses\n\n", ifname);
return(-2);
}
/* Get range info if we can */
if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
has_range = 1;
/* Display it */
if(n == 0)
printf("%-8.16s No statistics to collect\n", ifname);
else
printf("%-8.16s Statistics collected:\n", ifname);
/* The two lists */
hwa = (struct sockaddr *) buffer;
qual = (struct iw_quality *) (buffer + (sizeof(struct sockaddr) * n));
for(i = 0; i < n; i++)
{
/* Print stats for each address */
printf(" %s : ", iw_saether_ntop(&hwa[i], temp));
iw_print_stats(temp, sizeof(temp), &qual[i], &range, has_range);
printf("%s\n", temp);
}
if((n > 0) && (has_range) && (range.we_version_compiled > 11))
{
iwstats stats;
/* Get /proc/net/wireless */
if(iw_get_stats(skfd, ifname, &stats, &range, has_range) >= 0)
{
iw_print_stats(temp, sizeof(temp), &stats.qual, &range, has_range);
printf(" Link/Cell/AP : %s\n", temp);
/* Display the static data */
iw_print_stats(temp, sizeof(temp),
&range.avg_qual, &range, has_range);
printf(" Typical/Reference : %s\n", temp);
}
}
printf("\n");
return(0);
}
/*------------------------------------------------------------------*/
/*
* Get spy thresholds from the driver and display
*/
static int
get_spy_threshold(int skfd, /* The socket */
char * ifname, /* Dev name */
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
struct iw_thrspy threshold;
iwrange range;
int has_range = 0;
/* Avoid "Unused parameter" warning */
args = args; count = count;
/* Time to send thresholds to the driver */
wrq.u.data.pointer = (caddr_t) &threshold;
wrq.u.data.length = 1;
wrq.u.data.flags = 0;
if(iw_set_ext(skfd, ifname, SIOCGIWTHRSPY, &wrq) < 0)
{
fprintf(stderr, "Interface doesn't support thresholds...\n");
fprintf(stderr, "SIOCGIWTHRSPY: %s\n", strerror(errno));
return(-1);
}
/* Get range info if we can */
if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
has_range = 1;
/* Display thresholds */
if((has_range) && (threshold.low.level))
{
/* If the statistics are in dBm */
if(threshold.low.level > range.max_qual.level)
{
/* Statistics are in dBm (absolute power measurement) */
printf("%-8.16s Low threshold:%d dBm High threshold:%d dBm\n\n",
ifname,
threshold.low.level - 0x100, threshold.high.level - 0x100);
}
else
{
/* Statistics are relative values (0 -> max) */
printf("%-8.16s Low threshold:%d/%d High threshold:%d/%d\n\n",
ifname,
threshold.low.level, range.max_qual.level,
threshold.high.level, range.max_qual.level);
}
}
else
{
/* We can't read the range, so we don't know... */
printf("%-8.16s Low threshold:%d High threshold:%d\n\n",
ifname,
threshold.low.level, threshold.high.level);
}
return(0);
}
/************************* SETTING ROUTINES **************************/
/*------------------------------------------------------------------*/
/*
* Set list of addresses specified on command line in the driver.
*/
static int
set_spy_info(int skfd, /* The socket */
char * ifname, /* Dev name */
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
int i;
int nbr; /* Number of valid addresses */
struct sockaddr hw_address[IW_MAX_SPY];
/* Read command line */
i = 0; /* first arg to read */
nbr = 0; /* Number of args read so far */
/* "off" : disable functionality (set 0 addresses) */
if(!strcmp(args[0], "off"))
i = 1; /* skip the "off" */
else
{
/* "+" : add all addresses already in the driver */
if(!strcmp(args[0], "+"))
{
char buffer[(sizeof(struct iw_quality) +
sizeof(struct sockaddr)) * IW_MAX_SPY];
/* Check if we have valid mac address type */
if(iw_check_mac_addr_type(skfd, ifname) < 0)
{
fprintf(stderr, "%-8.16s Interface doesn't support MAC addresses\n", ifname);
return(-1);
}
wrq.u.data.pointer = (caddr_t) buffer;
wrq.u.data.length = IW_MAX_SPY;
wrq.u.data.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
{
fprintf(stderr, "Interface doesn't accept reading addresses...\n");
fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
return(-1);
}
/* Copy old addresses */
nbr = wrq.u.data.length;
memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
i = 1; /* skip the "+" */
}
/* Read other args on command line */
while((i < count) && (nbr < IW_MAX_SPY))
{
/* Get the address and check if the interface supports it */
if(iw_in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
continue;
nbr++;
}
/* Check the number of addresses */
if(nbr == 0)
{
fprintf(stderr, "No valid addresses found : exiting...\n");
return(-1);
}
}
/* Check if there is some remaining arguments */
if(i < count)
{
fprintf(stderr, "Got only the first %d arguments, remaining discarded\n", i);
}
/* Time to do send addresses to the driver */
wrq.u.data.pointer = (caddr_t) hw_address;
wrq.u.data.length = nbr;
wrq.u.data.flags = 0;
if(iw_set_ext(skfd, ifname, SIOCSIWSPY, &wrq) < 0)
{
fprintf(stderr, "Interface doesn't accept addresses...\n");
fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno));
return(-1);
}
return(0);
}
/*------------------------------------------------------------------*/
/*
* Set spy thresholds in the driver from command line
*/
static int
set_spy_threshold(int skfd, /* The socket */
char * ifname, /* Dev name */
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
struct iw_thrspy threshold;
int low_thr;
int high_thr;
/* Init */
memset(&threshold, '\0', sizeof(threshold));
/* "off" : disable functionality (set 0 addresses) */
if(!strcmp(args[0], "off"))
{
/* Just send null threshold, will disable it */
}
else
{
/* Try to get our threshold */
if(count < 2)
{
fprintf(stderr, "%-8.16s Need two threshold values\n", ifname);
return(-1);
}
if((sscanf(args[0], "%i", &low_thr) != 1) ||
(sscanf(args[1], "%i", &high_thr) != 1))
{
fprintf(stderr, "%-8.16s Invalid threshold values\n", ifname);
return(-1);
}
/* Basic sanity check */
if(high_thr < low_thr)
{
fprintf(stderr, "%-8.16s Inverted threshold range\n", ifname);
return(-1);
}
/* Copy thresholds */
threshold.low.level = low_thr;
threshold.low.updated = 0x2;
threshold.high.level = high_thr;
threshold.high.updated = 0x2;
}
/* Time to send thresholds to the driver */
wrq.u.data.pointer = (caddr_t) &threshold;
wrq.u.data.length = 1;
wrq.u.data.flags = 0;
if(iw_set_ext(skfd, ifname, SIOCSIWTHRSPY, &wrq) < 0)
{
fprintf(stderr, "Interface doesn't accept thresholds...\n");
fprintf(stderr, "SIOCSIWTHRSPY: %s\n", strerror(errno));
return(-1);
}
return(0);
}
/******************************* MAIN ********************************/
/*------------------------------------------------------------------*/
/*
* The main !
*/
int
main(int argc,
char ** argv)
{
int skfd; /* generic raw socket desc. */
int goterr = 0;
/* Create a channel to the NET kernel. */
if((skfd = iw_sockets_open()) < 0)
{
perror("socket");
return(-1);
}
/* No argument : show the list of all device + info */
if(argc == 1)
iw_enum_devices(skfd, &print_spy_info, NULL, 0);
else
/* Special cases take one... */
/* Help */
if((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help")))
fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n");
else
/* Version */
if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))
goterr = iw_print_version_info("iwspy");
else
/* The device name must be the first argument */
/* Name only : show spy list for that device only */
if(argc == 2)
goterr = print_spy_info(skfd, argv[1], NULL, 0);
else
/* Special commands */
if(!strcmp(argv[2], "setthr"))
goterr = set_spy_threshold(skfd, argv[1], argv + 3, argc - 3);
else
if(!strcmp(argv[2], "getthr"))
goterr = get_spy_threshold(skfd, argv[1], argv + 3, argc - 3);
else
/* Otherwise, it's a list of address to set in the spy list */
goterr = set_spy_info(skfd, argv[1], argv + 2, argc - 2);
/* Close the socket. */
iw_sockets_close(skfd);
return(goterr);
}