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
|
/*
* prismstumbler wireless lan analyzing tool
*
* Copyright (C) 2000 and created by Jan Fernquist <Jan.B.Fernquist@telia.com>
* Copyright (C) 2003 Florian Boor <florian.boor@kernelconcepts.de>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Wireless extensions scan interface.
*
* Most stuff is taken from wireless tools,
* Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include "iface-scan.h"
#include "iwlib.h"
#include "prismstumbler.h"
static ScanResult_t *results;
static int num_nets = 0;
extern psconfig_t cfg;
/*
* Print one element from the scanning results
*/
static inline int
print_scanning_token(struct iw_event * event, /* Extracted token */
int ap_num, /* AP number */
struct iw_range * iwrange, /* Range info */
int has_range)
{
char buffer[128]; /* Temporary buffer */
/* Now, let's decode the event */
switch(event->cmd)
{
case SIOCGIWAP:
#ifdef DEBUG
printf(" Cell %02d - Address: %s\n", ap_num,
iw_pr_ether(buffer, event->u.ap_addr.sa_data));
#endif
ap_num++;
num_nets++;
results = realloc(results,num_nets * sizeof(ScanResult_t));
memset(&results[num_nets-1],0,sizeof(ScanResult_t));
snprintf(results[num_nets-1].BssId,20,
iw_pr_ether(buffer, event->u.ap_addr.sa_data));
time(&results[num_nets-1].when);
break;
#ifdef DEBUG
case SIOCGIWNWID:
if(event->u.nwid.disabled)
printf(" NWID:off/any\n");
else
printf(" NWID:%X\n", event->u.nwid.value);
break;
#endif
case SIOCGIWFREQ:
{
double freq; /* Frequency/channel */
freq = iw_freq2float(&(event->u.freq));
results[num_nets-1].Channel = iw_freq_to_channel(freq,iwrange);
#ifdef DEBUG
iw_print_freq(buffer, freq);
printf(" %s\n", buffer);
#endif
}
break;
case SIOCGIWMODE:
#ifdef DEBUG
printf(" Mode:%s\n",
iw_operation_mode[event->u.mode]);
#endif
switch (event->u.mode)
{
case 1: /* ad-hoc */
results[num_nets-1].isAdHoc = 1;
break;
case 3: /* master */
results[num_nets-1].isAp = 1;
break;
case 4:
case 5: /* secondary, repeater */
results[num_nets-1].isAp = 2;
break;
}
break;
#ifdef DEBUG
case SIOCGIWNAME:
printf(" Protocol:%-1.16s\n", event->u.name);
break;
#endif
case SIOCGIWESSID:
{
char essid[IW_ESSID_MAX_SIZE+1];
if((event->u.essid.pointer) && (event->u.essid.length))
memcpy(essid, event->u.essid.pointer, event->u.essid.length);
essid[event->u.essid.length] = '\0';
if(event->u.essid.flags)
{
/* Does it have an ESSID index ? */
#ifdef DEBUG
if((event->u.essid.flags & IW_ENCODE_INDEX) > 1)
printf(" ESSID:\"%s\" [%d]\n", essid,
(event->u.essid.flags & IW_ENCODE_INDEX));
else
printf(" ESSID:\"%s\"\n", essid);
#endif
snprintf(results[num_nets-1].SSID,32,essid);
}
else
{
#ifdef DEBUG
printf(" ESSID:off/any\n");
#endif
snprintf(results[num_nets-1].SSID,32,"<off>");
}
}
break;
case SIOCGIWENCODE:
{
unsigned char key[IW_ENCODING_TOKEN_MAX];
if(event->u.data.pointer)
memcpy(key, event->u.essid.pointer, event->u.data.length);
else
event->u.data.flags |= IW_ENCODE_NOKEY;
#ifdef DEBUG
printf(" Encryption key:");
if(event->u.data.flags & IW_ENCODE_DISABLED)
printf("off\n");
else
{
/* Display the key */
iw_print_key(buffer, key, event->u.data.length,
event->u.data.flags);
printf("%s", buffer);
/* Other info... */
if((event->u.data.flags & IW_ENCODE_INDEX) > 1)
printf(" [%d]", event->u.data.flags & IW_ENCODE_INDEX);
if(event->u.data.flags & IW_ENCODE_RESTRICTED)
printf(" Security mode:restricted");
if(event->u.data.flags & IW_ENCODE_OPEN)
printf(" Security mode:open");
printf("\n");
results[num_nets-1].hasWep = 1;
}
#endif
if(!(event->u.data.flags & IW_ENCODE_DISABLED))
results[num_nets-1].hasWep = 1;
}
break;
case SIOCGIWRATE:
#ifdef DEBUG
iw_print_bitrate(buffer, event->u.bitrate.value);
printf(" Bit Rate:%s\n", buffer);
#endif
if (event->u.bitrate.value/KILO > results[num_nets-1].speed)
results[num_nets-1].speed = event->u.bitrate.value/KILO;
break;
case IWEVQUAL:
{
// event->u.qual.updated = 0x0; /* Not that reliable, disable */
results[num_nets-1].Signal = event->u.qual.updated;
#ifdef DEBUG
iw_print_stats(buffer, &event->u.qual, iwrange, has_range);
printf(" %s\n", buffer);
#endif
break;
}
#ifdef DEBUG
#if WIRELESS_EXT > 14
case IWEVCUSTOM:
{
char custom[IW_CUSTOM_MAX+1];
if((event->u.data.pointer) && (event->u.data.length))
memcpy(custom, event->u.data.pointer, event->u.data.length);
custom[event->u.data.length] = '\0';
printf(" Extra:%s\n", custom);
}
break;
#endif /* WIRELESS_EXT > 14 */
#endif
default:
printf(" (Unknown Wireless Token 0x%04X)\n",
event->cmd);
} /* switch(event->cmd) */
/* May have changed */
return(ap_num);
}
/*
* Perform a scanning on one device
*/
static int
print_scanning_info(int skfd,
char * ifname,
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
unsigned char buffer[IW_SCAN_MAX_DATA]; /* Results */
struct timeval tv; /* Select timeout */
int timeout = 5000000; /* 5s */
/* Avoid "Unused parameter" warning */
args = args; count = count;
/* Init timeout value -> 250ms*/
tv.tv_sec = 0;
tv.tv_usec = 250000;
/*
* Here we should look at the command line args and set the IW_SCAN_ flags
* properly
*/
wrq.u.param.flags = IW_SCAN_DEFAULT;
wrq.u.param.value = 0; /* Later */
/* Initiate Scanning */
if(iw_set_ext(skfd, ifname, SIOCSIWSCAN, &wrq) < 0)
{
if(errno != EPERM)
{
fprintf(stderr, "%-8.8s Interface doesn't support scanning : %s\n\n",
ifname, strerror(errno));
return(-1);
}
/* If we don't have the permission to initiate the scan, we may
* still have permission to read left-over results.
* But, don't wait !!! */
tv.tv_usec = 0;
}
timeout -= tv.tv_usec;
/* Forever */
while(1)
{
fd_set rfds; /* File descriptors for select */
int last_fd; /* Last fd */
int ret;
/* Guess what ? We must re-generate rfds each time */
FD_ZERO(&rfds);
last_fd = -1;
/* In here, add the rtnetlink fd in the list */
/* Wait until something happens */
ret = select(last_fd + 1, &rfds, NULL, NULL, &tv);
/* Check if there was an error */
if(ret < 0)
{
if(errno == EAGAIN || errno == EINTR)
continue;
fprintf(stderr, "Unhandled signal - exiting...\n");
return(-1);
}
/* Check if there was a timeout */
if(ret == 0)
{
/* Try to read the results */
wrq.u.data.pointer = buffer;
wrq.u.data.flags = 0;
wrq.u.data.length = sizeof(buffer);
if(iw_get_ext(skfd, ifname, SIOCGIWSCAN, &wrq) < 0)
{
/* Check if results not available yet */
if(errno == EAGAIN)
{
/* Restart timer for only 100ms*/
tv.tv_sec = 0;
tv.tv_usec = 100000;
timeout -= tv.tv_usec;
if(timeout > 0)
continue; /* Try again later */
}
/* Bad error */
fprintf(stderr, "%-8.8s Failed to read scan data : %s\n\n",
ifname, strerror(errno));
return(-2);
}
else
/* We have the results, go to process them */
break;
}
/* In here, check if event and event type
* if scan event, read results. All errors bad & no reset timeout */
}
if(wrq.u.data.length)
{
struct iw_event iwe;
struct stream_descr stream;
int ap_num = 1;
int ret;
struct iw_range range;
int has_range;
has_range = (iw_get_range_info(skfd, ifname, &range) >= 0);
#ifdef DEBUG
printf("%-8.8s Scan completed :\n", ifname);
#endif
iw_init_event_stream(&stream, buffer, wrq.u.data.length);
do
{
/* Extract an event and print it */
ret = iw_extract_event_stream(&stream, &iwe);
if(ret > 0)
ap_num = print_scanning_token(&iwe, ap_num, &range, has_range);
}
while(ret > 0);
#ifdef DEBUG
printf("\n");
#endif
}
#ifdef DEBUG
else
printf("%-8.8s No scan results\n", ifname);
#endif
return(0);
}
/*
Perform scan on given device.
Returns TRUE on sucess, FALSE if scanning is not available.
*/
int
do_scan_interface(char *ifname, int ps_socket, float longitude, float latitude)
{
int i, result;
int cardfd;
/* Create a channel to the NET kernel. */
if((cardfd = iw_sockets_open()) < 0)
{
perror("socket");
return 0;
}
result = print_scanning_info(cardfd,ifname, NULL, 0);
for (i = 0;i < num_nets; i++)
{
results[i].longitude = longitude;
results[i].latitude = latitude;
update_all (&results[i], ps_socket);
send_netlist (ps_socket, 0);
}
free(results);
results = NULL;
num_nets = 0;
close(cardfd);
return (result == 0);
}
|