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
|
/*
* Copyright (C) 2008 Fred Chien <fred@lxde.org>
* 2008 Jim Huang <jserv.tw@gmail.com>
*
* This file is a part of LXPanel project.
*
* 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 <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <sys/time.h>
#include <iwlib.h>
#include "netstat.h"
#include "wireless.h"
/*
static const char * iw_ie_cypher_name[] = {
"none",
"WEP-40",
"TKIP",
"WRAP",
"CCMP",
"WEP-104",
};
static const char * iw_ie_key_mgmt_name[] = {
"none",
"802.1x",
"PSK",
};
*/
void wireless_aplist_free(void *aplist, GObject *dummy)
{
APLIST *ptr;
APLIST *delptr;
if (aplist!=NULL) {
ptr = aplist;
do {
g_free(ptr->info->essid);
g_free(ptr->info->apaddr);
g_free(ptr->info);
delptr = ptr;
ptr = ptr->next;
g_free(delptr);
} while(ptr!=NULL);
}
}
void wireless_gen_ie(ap_info *info, unsigned char *buffer, int ielen)
{
int offset = 2;
int count;
int i;
unsigned char wpa1_oui[3] = {0x00, 0x50, 0xf2};
unsigned char wpa2_oui[3] = {0x00, 0x0f, 0xac};
unsigned char *wpa_oui;
/* check IE type */
switch(buffer[0]) {
case 0xdd: /* WPA or else */
wpa_oui = wpa1_oui;
if((ielen < 8)
|| (memcmp(&buffer[offset], wpa_oui, 3) != 0)
|| (buffer[offset + 3] != 0x01)) {
if (info->haskey)
info->en_method = NS_WIRELESS_AUTH_WEP;
else
info->en_method = NS_WIRELESS_AUTH_OFF;
info->key_mgmt = NS_IW_IE_KEY_MGMT_NONE;
info->group = NS_IW_IE_CIPHER_NONE;
info->pairwise = NS_IW_IE_CIPHER_NONE;
return;
}
/* OUI and 0x01 */
offset += 4;
break;
case 0x30: /* IEEE 802.11i/WPA2 */
wpa_oui = wpa2_oui;
break;
default: /* Unknown */
if (info->haskey)
info->en_method = NS_WIRELESS_AUTH_WEP;
else
info->en_method = NS_WIRELESS_AUTH_OFF;
info->key_mgmt = NS_IW_IE_KEY_MGMT_NONE;
info->group = NS_IW_IE_CIPHER_NONE;
info->pairwise = NS_IW_IE_CIPHER_NONE;
return;
}
/* assume TKIP */
info->en_method = NS_WIRELESS_AUTH_WPA;
info->key_mgmt = NS_IW_IE_KEY_MGMT_NONE;
info->group = NS_IW_IE_CIPHER_TKIP;
info->pairwise = NS_IW_IE_CIPHER_TKIP;
/* 2 bytes for version number (little endian) */
offset += 2;
/* check group cipher for short IE */
if ((offset+4) > ielen) {
/* this is a short IE, we can assume TKIP/TKIP. */
info->group = NS_IW_IE_CIPHER_TKIP;
info->pairwise = NS_IW_IE_CIPHER_TKIP;
return;
}
/* 4 Bytes for group cipher information [3 bytes][1 Byte] */
if(memcmp(&buffer[offset], wpa_oui, 3)!=0) {
/* the group cipher is proprietary */
info->group = NS_IW_IE_CIPHER_NONE;
} else {
/* pick a byte for type of group cipher */
info->group = buffer[offset+3];
}
offset += 4;
/* check pairwise cipher for short IE */
if ((offset+2) > ielen) {
/* this is a short IE, we can assume TKIP. */
info->pairwise = NS_IW_IE_CIPHER_TKIP;
return;
}
/* 2 bytes for number of pairwise ciphers (little endian) */
count = buffer[offset] | (buffer[offset + 1] << 8);
offset += 2;
/* if we are done */
if ((offset+4*count) > ielen) {
return;
}
/* choose first cipher of pairwise ciphers to use,
* FIXME: Let user decide the cipher is the best way. */
for(i=0;i<count;i++) {
if(memcmp(&buffer[offset], wpa_oui, 3)==0) {
/* pick a byte for type of group cipher */
info->pairwise = buffer[offset+3];
}
offset += 4;
}
/* check authentication suites */
if ((offset+2) > ielen) {
/* this is a short IE, we can assume TKIP. */
info->key_mgmt = NS_IW_IE_KEY_MGMT_NONE;
return;
}
/* 2 bytes for number of authentication suites (little endian) */
count = buffer[offset] | (buffer[offset + 1] << 8);
offset += 2;
/* if we are done */
if ((offset+4*count) > ielen) {
return;
}
/* choose first key_mgmt of authentication suites to use,
* FIXME: Let user decide the key_mgmt is the best way. */
for(i=0;i<count;i++) {
if(memcmp(&buffer[offset], wpa_oui, 3)==0) {
/* pick a byte for type of key_mgmt */
info->key_mgmt = buffer[offset+3];
}
offset += 4;
}
}
ap_info *
wireless_parse_scanning_event(struct iw_event *event, ap_info *oldinfo)
{
ap_info *info;
/* found a new AP */
if (event->cmd==SIOCGIWAP) {
char buf[128];
info = g_new0(ap_info, 1);
info->apaddr = g_strdup(iw_sawap_ntop(&event->u.ap_addr, buf));
info->en_method = NS_WIRELESS_AUTH_OFF;
info->haskey = FALSE;
info->key_mgmt = NS_IW_IE_KEY_MGMT_NONE;
info->group = NS_IW_IE_CIPHER_TKIP;
info->pairwise = NS_IW_IE_CIPHER_TKIP;
} else {
info = oldinfo;
}
switch (event->cmd) {
case SIOCGIWESSID: /* ESSID */
if (!event->u.essid.flags
|| event->u.essid.length==0
|| strlen(event->u.essid.pointer)==0) {
info->essid = NULL;
} else {
info->essid = g_strndup(event->u.essid.pointer, event->u.essid.length);
}
break;
case IWEVQUAL: /* Signal Quality */
info->quality = (int)rint((log (event->u.qual.qual) / log (92)) * 100.0);
break;
case SIOCGIWENCODE: /* Encryption */
if (!event->u.data.pointer)
event->u.data.flags |= IW_ENCODE_NOKEY;
if (!(event->u.data.flags & IW_ENCODE_DISABLED)) {
info->haskey = TRUE;
/* assume WEP */
info->en_method = NS_WIRELESS_AUTH_WEP;
} else {
info->haskey = FALSE;
info->en_method = NS_WIRELESS_AUTH_OFF;
}
break;
case IWEVGENIE: /* Extra information */
{
int offset = 0;
int ielen = event->u.data.length;
unsigned char *iebuf = event->u.data.pointer;
while(offset <= (ielen - 2)) {
/* check IE type */
switch(iebuf[offset]) {
case 0xdd: /* WPA or else */
case 0x30: /* IEEE 802.11i/WPA2 */
wireless_gen_ie(info, iebuf, ielen);
break;
}
offset += iebuf[offset+1] + 2;
}
}
break;
}
return info;
}
/* when we have some workaround problems,
* we need this function to rescanning access-point.
* */
gboolean wireless_refresh(int iwsockfd, const char *ifname)
{
struct iwreq wrq;
struct iw_range range;
struct timeval tv;
fd_set rfds; /* File descriptors for select */
int selfd;
char buffer[IW_SCAN_MAX_DATA];
/* setting interfaces name */
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
/* Getting range */
iw_get_range_info(iwsockfd, ifname, &range);
/* check scanning support */
if (range.we_version_compiled < 14)
return FALSE;
/* Initiate Scanning */
wrq.u.data.pointer = buffer;
wrq.u.data.length = IW_SCAN_MAX_DATA;
wrq.u.data.flags = 0;
if (ioctl(iwsockfd, SIOCSIWSCAN, &wrq) < 0) {
if (errno!=EPERM)
return FALSE;
}
/* Init timeout value -> 250ms */
tv.tv_sec = 0;
tv.tv_usec = 250000;
/* Scanning APs */
while(1) {
if (ioctl(iwsockfd, SIOCGIWSCAN, &wrq) < 0) {
if (errno == EAGAIN) { /* not yet ready */
FD_ZERO(&rfds);
selfd = -1;
if (select(selfd + 1, &rfds, NULL, NULL, &tv)==0)
continue; /* timeout */
} else {
break;
}
}
if (wrq.u.data.length <= 0)
break;
}
return TRUE;
}
APLIST *wireless_scanning(int iwsockfd, const char *ifname)
{
APLIST *ap = NULL;
APLIST *newap;
struct iwreq wrq;
int scanflags = 0; /* Flags for scan */
unsigned char * buffer = NULL; /* Results */
int buflen = IW_SCAN_MAX_DATA; /* Min for compat WE < 17 */
struct iw_range range;
int has_range;
struct timeval tv; /* select timeout */
int timeout = 15000000; /* 15s */
/* Get range stuff */
has_range = (iw_get_range_info(iwsockfd, ifname, &range) >= 0);
/* Check if the interface could support scanning. */
if ((!has_range) || (range.we_version_compiled < 14)) {
fprintf(stderr, "%-8.16s Interface doesn't support scanning.\n\n",
ifname);
return NULL;
}
/* Init timeout value -> 250ms between set and first get */
tv.tv_sec = 0;
tv.tv_usec = 250000;
wrq.u.data.pointer = NULL;
wrq.u.data.flags = 0;
wrq.u.data.length = 0;
/* Initiate Scanning */
if (iw_set_ext(iwsockfd, ifname, SIOCSIWSCAN, &wrq) < 0) {
if ((errno != EPERM) || (scanflags != 0)) {
fprintf(stderr, "%-8.16s Interface doesn't support "
"scanning : %s\n\n", ifname, strerror(errno));
return NULL;
}
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 NULL;
}
/* Check if there was a timeout */
if (ret == 0) {
unsigned char *newbuf;
realloc:
/* (Re)allocate the buffer - realloc(NULL, len) == malloc(len) */
newbuf = realloc(buffer, buflen);
if (newbuf == NULL) {
if (buffer)
free(buffer);
fprintf(stderr, "%s: Allocation failed\n", __FUNCTION__);
return NULL;
}
buffer = newbuf;
/* Try to read the results */
wrq.u.data.pointer = buffer;
wrq.u.data.flags = 0;
wrq.u.data.length = buflen;
if (iw_get_ext(iwsockfd, ifname, SIOCGIWSCAN, &wrq) < 0) {
/* Check if buffer was too small (WE-17 only) */
if ((errno == E2BIG) &&
(range.we_version_compiled > 16)) {
/* Check if the driver gave us any hints. */
if (wrq.u.data.length > buflen)
buflen = wrq.u.data.length;
else
buflen *= 2;
/* Try again */
goto realloc;
}
/* 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 */
free(buffer);
fprintf(stderr,
"%-8.16s Failed to read scan data : %s\n\n",
ifname, strerror(errno));
return NULL;
}
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 ret;
iw_init_event_stream(&stream, (char *) buffer, wrq.u.data.length);
do {
/* Extract an event and print it */
ret = iw_extract_event_stream(&stream, &iwe, range.we_version_compiled);
if (iwe.cmd==SIOCGIWAP) {
newap = malloc(sizeof(APLIST));
newap->info = NULL;
newap->next = ap;
ap = newap;
}
ap->info = wireless_parse_scanning_event(&iwe, ap->info);
}
while (ret > 0);
printf("\n");
}
else
printf("%-8.16s No scan results\n\n", ifname);
free(buffer);
return ap;
}
|