/* * Auxiliary declarations and functions imported from iwlib in order to * process and parse scan events. This code is copied with little change * from wireless tools 30. It remains here until the wext code will be * replaced by corresponding netlink calls. */ #include "iw_if.h" #define MAX_SCAN_WAIT 10000 /* maximum milliseconds spent waiting */ /* * Meta-data about all the additional standard Wireless Extension events * we know about. */ /* Type of headers we know about (basically union iwreq_data) */ #define IW_HEADER_TYPE_NULL 0 /* Not available */ #define IW_HEADER_TYPE_CHAR 2 /* char [IFNAMSIZ] */ #define IW_HEADER_TYPE_UINT 4 /* __u32 */ #define IW_HEADER_TYPE_FREQ 5 /* struct iw_freq */ #define IW_HEADER_TYPE_ADDR 6 /* struct sockaddr */ #define IW_HEADER_TYPE_POINT 8 /* struct iw_point */ #define IW_HEADER_TYPE_PARAM 9 /* struct iw_param */ #define IW_HEADER_TYPE_QUAL 10 /* struct iw_quality */ /* Size (in bytes) of various events */ static const int event_type_size[] = { [IW_HEADER_TYPE_NULL] = IW_EV_LCP_PK_LEN, [IW_HEADER_TYPE_CHAR] = IW_EV_CHAR_PK_LEN, [IW_HEADER_TYPE_UINT] = IW_EV_UINT_PK_LEN, [IW_HEADER_TYPE_FREQ] = IW_EV_FREQ_PK_LEN, [IW_HEADER_TYPE_ADDR] = IW_EV_ADDR_PK_LEN, /* * Fix IW_EV_POINT_PK_LEN: some wireless.h versions define this * erroneously as IW_EV_LCP_LEN + 4 (e.g. ESSID will disappear). * The value below is from wireless tools 30. */ [IW_HEADER_TYPE_POINT] = IW_EV_LCP_PK_LEN + 4, [IW_HEADER_TYPE_PARAM] = IW_EV_PARAM_PK_LEN, [IW_HEADER_TYPE_QUAL] = IW_EV_QUAL_PK_LEN }; /* Handling flags */ #define IW_DESCR_FLAG_NONE 0x0000 /* Obvious */ /* Wrapper level flags */ #define IW_DESCR_FLAG_DUMP 0x0001 /* Not part of the dump command */ #define IW_DESCR_FLAG_EVENT 0x0002 /* Generate an event on SET */ #define IW_DESCR_FLAG_RESTRICT 0x0004 /* GET : request is ROOT only */ /* SET : Omit payload from generated iwevent */ #define IW_DESCR_FLAG_NOMAX 0x0008 /* GET : no limit on request size */ /* Driver level flags */ #define IW_DESCR_FLAG_WAIT 0x0100 /* Wait for driver event */ struct iw_ioctl_description { __u8 header_type; /* NULL, iw_point or other */ __u8 token_type; /* Future */ __u16 token_size; /* Granularity of payload */ __u16 min_tokens; /* Min acceptable token number */ __u16 max_tokens; /* Max acceptable token number */ __u32 flags; /* Special handling of the request */ }; /* * Meta-data about all the standard Wireless Extension request we * know about. */ static const struct iw_ioctl_description standard_ioctl_descr[] = { [SIOCSIWCOMMIT - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_NULL, }, [SIOCGIWNAME - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_CHAR, .flags = IW_DESCR_FLAG_DUMP, }, [SIOCSIWNWID - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, .flags = IW_DESCR_FLAG_EVENT, }, [SIOCGIWNWID - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, .flags = IW_DESCR_FLAG_DUMP, }, [SIOCSIWFREQ - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_FREQ, .flags = IW_DESCR_FLAG_EVENT, }, [SIOCGIWFREQ - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_FREQ, .flags = IW_DESCR_FLAG_DUMP, }, [SIOCSIWMODE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_UINT, .flags = IW_DESCR_FLAG_EVENT, }, [SIOCGIWMODE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_UINT, .flags = IW_DESCR_FLAG_DUMP, }, [SIOCSIWSENS - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCGIWSENS - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCSIWRANGE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_NULL, }, [SIOCGIWRANGE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = sizeof(struct iw_range), .flags = IW_DESCR_FLAG_DUMP, }, [SIOCSIWPRIV - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_NULL, }, [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */ .header_type = IW_HEADER_TYPE_NULL, }, [SIOCSIWSTATS - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_NULL, }, [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */ .header_type = IW_HEADER_TYPE_NULL, .flags = IW_DESCR_FLAG_DUMP, }, [SIOCSIWSPY - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = sizeof(struct sockaddr), .max_tokens = IW_MAX_SPY, }, [SIOCGIWSPY - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = sizeof(struct sockaddr) + sizeof(struct iw_quality), .max_tokens = IW_MAX_SPY, }, [SIOCSIWTHRSPY - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = sizeof(struct iw_thrspy), .min_tokens = 1, .max_tokens = 1, }, [SIOCGIWTHRSPY - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = sizeof(struct iw_thrspy), .min_tokens = 1, .max_tokens = 1, }, [SIOCSIWAP - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_ADDR, }, [SIOCGIWAP - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_ADDR, .flags = IW_DESCR_FLAG_DUMP, }, [SIOCSIWMLME - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .min_tokens = sizeof(struct iw_mlme), .max_tokens = sizeof(struct iw_mlme), }, [SIOCGIWAPLIST - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = sizeof(struct sockaddr) + sizeof(struct iw_quality), .max_tokens = IW_MAX_AP, .flags = IW_DESCR_FLAG_NOMAX, }, [SIOCSIWSCAN - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .min_tokens = 0, .max_tokens = sizeof(struct iw_scan_req), }, [SIOCGIWSCAN - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_SCAN_MAX_DATA, .flags = IW_DESCR_FLAG_NOMAX, }, [SIOCSIWESSID - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_ESSID_MAX_SIZE + 1, .flags = IW_DESCR_FLAG_EVENT, }, [SIOCGIWESSID - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_ESSID_MAX_SIZE + 1, .flags = IW_DESCR_FLAG_DUMP, }, [SIOCSIWNICKN - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_ESSID_MAX_SIZE + 1, }, [SIOCGIWNICKN - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_ESSID_MAX_SIZE + 1, }, [SIOCSIWRATE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCGIWRATE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCSIWRTS - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCGIWRTS - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCSIWFRAG - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCGIWFRAG - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCSIWTXPOW - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCGIWTXPOW - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCSIWRETRY - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCGIWRETRY - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCSIWENCODE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_ENCODING_TOKEN_MAX, .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT, }, [SIOCGIWENCODE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_ENCODING_TOKEN_MAX, .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT, }, [SIOCSIWPOWER - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCGIWPOWER - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, #ifdef SIOCSIWMODUL [SIOCSIWMODUL - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, #endif #ifdef SIOCGIWMODUL [SIOCGIWMODUL - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, #endif [SIOCSIWGENIE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_GENERIC_IE_MAX, }, [SIOCGIWGENIE - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_GENERIC_IE_MAX, }, [SIOCSIWAUTH - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCGIWAUTH - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_PARAM, }, [SIOCSIWENCODEEXT - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .min_tokens = sizeof(struct iw_encode_ext), .max_tokens = sizeof(struct iw_encode_ext) + IW_ENCODING_TOKEN_MAX, }, [SIOCGIWENCODEEXT - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .min_tokens = sizeof(struct iw_encode_ext), .max_tokens = sizeof(struct iw_encode_ext) + IW_ENCODING_TOKEN_MAX, }, [SIOCSIWPMKSA - SIOCIWFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .min_tokens = sizeof(struct iw_pmksa), .max_tokens = sizeof(struct iw_pmksa), }, }; static const struct iw_ioctl_description standard_event_descr[] = { [IWEVTXDROP - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_ADDR, }, [IWEVQUAL - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_QUAL, }, [IWEVCUSTOM - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_CUSTOM_MAX, }, [IWEVREGISTERED - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_ADDR, }, [IWEVEXPIRED - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_ADDR, }, [IWEVGENIE - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_GENERIC_IE_MAX, }, [IWEVMICHAELMICFAILURE - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = sizeof(struct iw_michaelmicfailure), }, [IWEVASSOCREQIE - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_GENERIC_IE_MAX, }, [IWEVASSOCRESPIE - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = IW_GENERIC_IE_MAX, }, [IWEVPMKIDCAND - IWEVFIRST] = { .header_type = IW_HEADER_TYPE_POINT, .token_size = 1, .max_tokens = sizeof(struct iw_pmkid_cand), }, }; struct stream_descr { char *current; /* Current event in stream of events */ char *value; /* Current value in event */ char *end; /* End of the stream */ }; /* * Extract the next event from the event stream. */ static int iw_extract_event_stream(struct stream_descr *stream, struct iw_event *iwe, int we_version) { const struct iw_ioctl_description *descr = NULL; int event_type; unsigned int event_len = 1; /* Invalid */ unsigned cmd_index; /* *MUST* be unsigned */ char *pointer; if (stream->current + IW_EV_LCP_PK_LEN > stream->end) return 0; /* Extract the event header to get the event id. * Note : the event may be unaligned, therefore copy... */ memcpy((char *)iwe, stream->current, IW_EV_LCP_PK_LEN); if (iwe->len <= IW_EV_LCP_PK_LEN) return -1; /* Get the type and length of that event */ if (iwe->cmd <= SIOCIWLAST) { cmd_index = iwe->cmd - SIOCIWFIRST; if (cmd_index < ARRAY_SIZE(standard_ioctl_descr)) descr = standard_ioctl_descr + cmd_index; } else { cmd_index = iwe->cmd - IWEVFIRST; if (cmd_index < ARRAY_SIZE(standard_event_descr)) descr = standard_event_descr + cmd_index; } /* Unknown events -> event_type = 0 => IW_EV_LCP_PK_LEN */ event_type = descr ? descr->header_type : 0; event_len = event_type_size[event_type]; /* Check if we know about this event */ if (event_len <= IW_EV_LCP_PK_LEN) { stream->current += iwe->len; /* Skip to next event */ return 2; } event_len -= IW_EV_LCP_PK_LEN; /* Fixup for earlier version of WE */ if (we_version <= 18 && event_type == IW_HEADER_TYPE_POINT) event_len += IW_EV_POINT_OFF; if (stream->value != NULL) pointer = stream->value; /* Next value in event */ else pointer = stream->current + IW_EV_LCP_PK_LEN; /* First value in event */ /* Copy the rest of the event (at least, fixed part) */ if (pointer + event_len > stream->end) { stream->current += iwe->len; /* Skip to next event */ return -2; } /* Fixup for WE-19 and later: pointer no longer in the stream */ /* Beware of alignment. Dest has local alignment, not packed */ if (we_version > 18 && event_type == IW_HEADER_TYPE_POINT) memcpy((char *)iwe + IW_EV_LCP_LEN + IW_EV_POINT_OFF, pointer, event_len); else memcpy((char *)iwe + IW_EV_LCP_LEN, pointer, event_len); /* Skip event in the stream */ pointer += event_len; /* Special processing for iw_point events */ if (event_type == IW_HEADER_TYPE_POINT) { unsigned int extra_len = iwe->len - (event_len + IW_EV_LCP_PK_LEN); if (extra_len > 0) { /* Set pointer on variable part (warning : non aligned) */ iwe->u.data.pointer = pointer; /* Check that we have a descriptor for the command */ if (descr == NULL) { /* Can't check payload -> unsafe... */ iwe->u.data.pointer = NULL; /* Discard paylod */ } else { unsigned int token_len = iwe->u.data.length * descr->token_size; /* * Ugly fixup for alignment issues. * If the kernel is 64 bits and userspace 32 bits, we have an extra 4 + 4 * bytes. Fixing that in the kernel would break 64 bits userspace. */ if (token_len != extra_len && extra_len >= 4) { union iw_align_u16 { __u16 value; unsigned char byte[2]; } alt_dlen; unsigned int alt_token_len; /* Userspace seems to not always like unaligned access, * so be careful and make sure to align value. * I hope gcc won't play any of its aliasing tricks... */ alt_dlen.byte[0] = *(pointer); alt_dlen.byte[1] = *(pointer + 1); alt_token_len = alt_dlen.value * descr->token_size; /* Verify that data is consistent if assuming 64 bit alignment... */ if (alt_token_len + 8 == extra_len) { /* Ok, let's redo everything */ pointer -= event_len; pointer += 4; /* Dest has local alignment, not packed */ memcpy((char *)iwe + IW_EV_LCP_LEN + IW_EV_POINT_OFF, pointer, event_len); pointer += event_len + 4; token_len = alt_token_len; /* We may have no payload */ if (alt_token_len) iwe->u.data.pointer = pointer; else iwe->u.data.pointer = NULL; } } /* Discard bogus events which advertise more tokens than they carry ... */ if (token_len > extra_len) iwe->u.data.pointer = NULL; /* Discard paylod */ /* Check that the advertised token size is not going to * produce buffer overflow to our caller... */ if (iwe->u.data.length > descr->max_tokens && !(descr->flags & IW_DESCR_FLAG_NOMAX)) iwe->u.data.pointer = NULL; /* Discard payload */ /* Same for underflows... */ if (iwe->u.data.length < descr->min_tokens) iwe->u.data.pointer = NULL; /* Discard paylod */ } } else { /* No data */ iwe->u.data.pointer = NULL; } stream->current += iwe->len; /* Go to next event */ } else { /* * Ugly fixup for alignment issues. * If the kernel is 64 bits and userspace 32 bits, we have an extra 4 bytes. * Fixing that in the kernel would break 64 bits userspace. */ if (stream->value == NULL && ((iwe->len - IW_EV_LCP_PK_LEN) % event_len == 4 || (iwe->len == 12 && (event_type == IW_HEADER_TYPE_UINT || event_type == IW_HEADER_TYPE_QUAL)))) { pointer -= event_len; pointer += 4; /* Beware of alignment. Dest has local alignment, not packed */ memcpy((char *)iwe + IW_EV_LCP_LEN, pointer, event_len); pointer += event_len; } if (pointer + event_len <= stream->current + iwe->len) { stream->value = pointer; /* Go to next value */ } else { stream->value = NULL; stream->current += iwe->len; /* Go to next event */ } } return 1; } static void iw_extract_ie(struct iw_event *iwe, struct scan_result *sr) { const uint8_t wpa1_oui[3] = { 0x00, 0x50, 0xf2 }; uint8_t *buffer = iwe->u.data.pointer; int ielen = 0, ietype, i; /* Loop on each IE, each is min. 2 bytes TLV: IE-ID - Length - Value */ for (i = 0; i <= iwe->u.data.length - 2; i += ielen + 2) { ietype = buffer[i]; ielen = buffer[i + 1]; switch (ietype) { case 0x30: if (ielen < 4) /* make sure we have enough data */ continue; sr->flags |= IW_ENC_CAPA_WPA2; break; case 0xdd: /* Not all IEs that start with 0xdd are WPA1 */ if (ielen < 8 || memcmp(buffer + i + 2, wpa1_oui, 3) || buffer[i + 5] != 1) continue; sr->flags |= IW_ENC_CAPA_WPA; break; } } } /*----------------- End of code copied from iwlib -----------------------*/ /* Order by descending signal strength. */ extern int cmp_sig(const struct scan_result *a, const struct scan_result *b) { return a->qual.level - b->qual.level; } /* Order by ascending frequency first, then by descending signal strength. */ extern int cmp_freq_sig(const struct scan_result *a, const struct scan_result *b) { return a->freq == b->freq ? cmp_sig(a, b) : a->freq < b->freq; } struct scan_result *get_scan_list(int skfd, const char *ifname, int we_version, scan_cmp_func cmp_scan_result) { struct scan_result *head = NULL; struct iwreq wrq; int wait, waited = 0; /* * Some drivers may return very large scan results, either because there * are many cells, or there are many large elements. Do not bother to * guess buffer size, use maximum u16 wrq.u.data.length size. */ char scan_buf[0xffff]; /* We are checking errno when returning NULL, so reset it here */ errno = 0; memset(&wrq, 0, sizeof(wrq)); strncpy(wrq.ifr_ifrn.ifrn_name, ifname, IFNAMSIZ); if (ioctl(skfd, SIOCSIWSCAN, &wrq) < 0) return NULL; /* Larger initial timeout of 250ms between set and first get */ for (wait = 250; (waited += wait) < MAX_SCAN_WAIT; wait = 100) { struct timeval tv = { 0, wait * 1000 }; while (select(0, NULL, NULL, NULL, &tv) < 0) if (errno != EINTR && errno != EAGAIN) return NULL; wrq.u.data.pointer = scan_buf; wrq.u.data.length = sizeof(scan_buf); wrq.u.data.flags = 0; if (ioctl(skfd, SIOCGIWSCAN, &wrq) == 0) break; } if (wrq.u.data.length) { struct iw_event iwe; struct stream_descr stream; struct scan_result *new = NULL; int f = 0; /* Idea taken from waproamd */ memset(&stream, 0, sizeof(stream)); stream.current = scan_buf; stream.end = scan_buf + wrq.u.data.length; while (iw_extract_event_stream(&stream, &iwe, we_version) > 0) { if (!new) new = calloc(1, sizeof(*new)); switch (iwe.cmd) { case SIOCGIWAP: f = 1; memcpy(&new->ap_addr, &iwe.u.ap_addr.sa_data, sizeof(new->ap_addr)); break; case SIOCGIWESSID: f |= 2; memset(new->essid, 0, sizeof(new->essid)); if (iwe.u.essid.flags && iwe.u.essid.pointer && iwe.u.essid.length) memcpy(new->essid, iwe.u.essid.pointer, iwe.u.essid.length); break; case SIOCGIWMODE: new->mode = iwe.u.mode; f |= 4; break; case SIOCGIWFREQ: f |= 8; new->freq = freq_to_hz(&iwe.u.freq); break; case SIOCGIWENCODE: f |= 16; new->has_key = !(iwe.u.data.flags & IW_ENCODE_DISABLED); break; case IWEVQUAL: f |= 32; memcpy(&new->qual, &iwe.u.qual, sizeof(struct iw_quality)); break; case IWEVGENIE: f |= 64; iw_extract_ie(&iwe, new); break; } if (f == 127) { struct scan_result *cur = head, **prev = &head; f = 0; while (cur && cmp_scan_result(cur, new) > 0) prev = &cur->next, cur = cur->next; *prev = new; new->next = cur; new = NULL; } } free(new); /* may have been allocated but not filled in */ } return head; } void free_scan_result(struct scan_result *head) { if (head) { free_scan_result(head->next); free(head); } } ef='#n505'>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
/*
* wavemon - a wireless network monitoring aplication
*
* Copyright (c) 2001-2002 Jan Morgenstern <jan@jm-music.de>
*
* wavemon 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, or (at your option) any later
* version.
*
* wavemon 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 wavemon; see the file COPYING. If not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "iw_if.h"
/* Determine the artificial spreading of random samples (best: 1..10) */
#define WAVE_RAND_SPREAD 1
/* Fallback maximum quality level when using random samples. */
#define WAVE_RAND_QUAL_MAX 100
/*
* Obtain network device information
*/
static int if_get_flags(int skfd, const char *ifname)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
err_sys("can not get interface flags for %s", ifname);
return ifr.ifr_flags;
}
/* Return true if @ifname is known to be up */
bool if_is_up(int skfd, const char *ifname)
{
return if_get_flags(skfd, ifname) & IFF_UP;
}
/** Bring @ifname up if not already up. Return 0 if ok, < 0 on error. */
int if_set_up(int skfd, const char *ifname)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
ifr.ifr_flags = if_get_flags(skfd, ifname);
if (ifr.ifr_flags & IFF_UP)
return 0;
ifr.ifr_flags |= IFF_UP;
return ioctl(skfd, SIOCSIFFLAGS, &ifr);
}
/* Interface information */
void if_getinf(const char *ifname, struct if_info *info)
{
struct ifreq ifr;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
memset(&ifr, 0, sizeof(struct ifreq));
memset(info, 0, sizeof(struct if_info));
info->flags = if_get_flags(skfd, ifname);
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(skfd, SIOCGIFMTU, &ifr) == 0)
info->mtu = ifr.ifr_mtu;
if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
info->txqlen = ifr.ifr_qlen;
/* Copy the 6 byte Ethernet address and the 4 byte struct in_addrs */
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
memcpy(&info->hwaddr, &ifr.ifr_hwaddr.sa_data, 6);
if (ioctl(skfd, SIOCGIFADDR, &ifr) >= 0)
memcpy(&info->addr, &ifr.ifr_addr.sa_data[2], 4);
if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
memcpy(&info->netmask, &ifr.ifr_netmask.sa_data[2], 4);
if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
memcpy(&info->bcast, &ifr.ifr_broadaddr.sa_data[2], 4);
close(skfd);
}
static FILE *open_proc_net(const char *filename)
{
char path[128];
FILE *fp;
snprintf(path, sizeof(path), "/proc/net/%s", filename);
if (access(path, F_OK) != 0)
err_quit("'%s' not accessible - not compiled in?", path);
fp = fopen(path, "r");
if (fp == NULL)
err_sys("can not open %s", path);
return fp;
}
/**
* iw_get_interface_list - Return NULL-terminated array of WiFi interfaces.
*/
char **iw_get_interface_list(void)
{
char **if_list = NULL, *p, tmp[BUFSIZ];
int nifs = 1; /* if_list[nifs-1] = NULL */
FILE *fp = open_proc_net("wireless");
while (fgets(tmp, sizeof(tmp), fp))
if ((p = strchr(tmp, ':'))) {
if_list = realloc(if_list, sizeof(char *) * (nifs + 1));
for (*p = '\0', p = tmp; isspace(*p); )
p++;
if_list[nifs-1] = strdup(p);
if_list[nifs++] = NULL;
}
fclose(fp);
return if_list;
}
void if_getstat(const char *ifname, struct if_stat *stat)
{
char line[0x100];
unsigned long d;
char *lp;
FILE *fp = open_proc_net("dev");
/*
* Inter-| Receive | Transmit
* face |bytes packets errs drop fifo frame compressed multicast|bytes packets
*/
while (fgets(line, sizeof(line), fp)) {
lp = line + strspn(line, " ");
if (!strncmp(lp, ifname, strlen(ifname))) {
lp += strlen(ifname) + 1;
lp += strspn(lp, " ");
sscanf(lp, "%llu %llu %lu %lu %lu %lu %lu %lu %llu %llu",
&stat->rx_bytes, &stat->rx_packets, &d, &d, &d, &d, &d, &d,
&stat->tx_bytes, &stat->tx_packets);
}
}
fclose(fp);
}
/**
* iw_dyn_info_get - populate dynamic information
* @info: information to populate
* @ifname: interface name
* @if: range information to use (number of encryption keys)
*/
void dyn_info_get(struct iw_dyn_info *info,
const char *ifname, struct iw_range *ir)
{
struct iwreq iwr;
int i, skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
memset(info, 0, sizeof(struct iw_dyn_info));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(skfd, SIOCGIWNAME, &iwr) < 0)
err_sys("can not open device '%s'", ifname);
strncpy(info->name, iwr.u.name, IFNAMSIZ);
iwr.u.essid.pointer = (caddr_t) info->essid;
iwr.u.essid.length = sizeof(info->essid);
iwr.u.essid.flags = 0;
if (ioctl(skfd, SIOCGIWESSID, &iwr) >= 0) {
info->cap_essid = 1;
/* Convert potential ESSID index to count > 0 */
info->essid_ct = iwr.u.essid.flags & IW_ENCODE_INDEX ? : 1;
info->essid[iwr.u.essid.length] = '\0';
}
if (ioctl(skfd, SIOCGIWNWID, &iwr) >= 0) {
info->cap_nwid = 1;
memcpy(&info->nwid, &iwr.u.nwid, sizeof(info->nwid));
}
iwr.u.essid.pointer = (caddr_t) info->nickname;
iwr.u.essid.length = sizeof(info->nickname);
iwr.u.essid.flags = 0;
if (ioctl(skfd, SIOCGIWNICKN, &iwr) >= 0 &&
iwr.u.data.length > 1)
info->cap_nickname = 1;
if (ioctl(skfd, SIOCGIWFREQ, &iwr) >= 0) {
info->cap_freq = 1;
info->freq = freq_to_hz(&iwr.u.freq);
}
if (ioctl(skfd, SIOCGIWSENS, &iwr) >= 0) {
info->cap_sens = 1;
info->sens = iwr.u.sens.value;
}
if (ioctl(skfd, SIOCGIWRATE, &iwr) >= 0)
info->bitrate = iwr.u.bitrate.value;
if (ioctl(skfd, SIOCGIWTXPOW, &iwr) >= 0) {
info->cap_txpower = 1;
memcpy(&info->txpower, &iwr.u.txpower, sizeof(info->txpower));
}
if (ioctl(skfd, SIOCGIWPOWER, &iwr) >= 0) {
info->cap_power = 1;
memcpy(&info->power, &iwr.u.power, sizeof(info->power));
}
if (ioctl(skfd, SIOCGIWRETRY, &iwr) >= 0) {
info->cap_retry = 1;
memcpy(&info->retry, &iwr.u.retry, sizeof(info->retry));
}
if (ioctl(skfd, SIOCGIWRTS, &iwr) >= 0) {
info->cap_rts = 1;
memcpy(&info->rts, &iwr.u.rts, sizeof(info->rts));
}
if (ioctl(skfd, SIOCGIWFRAG, &iwr) >= 0) {
info->cap_frag = 1;
memcpy(&info->frag, &iwr.u.frag, sizeof(info->frag));
}
if (ioctl(skfd, SIOCGIWMODE, &iwr) >= 0) {
info->cap_mode = 1;
info->mode = iwr.u.mode;
}
info->nkeys = ir->max_encoding_tokens;
if (info->nkeys) {
info->keys = calloc(info->nkeys, sizeof(*info->keys));
if (info->keys == NULL)
err_sys("malloc(key array)");
/* Get index of default key first */
iwr.u.data.pointer = info->keys[0].key;
iwr.u.data.length = sizeof(info->keys[0].key);
iwr.u.data.flags = 0;
if (ioctl(skfd, SIOCGIWENCODE, &iwr) < 0) {
free(info->keys);
info->keys = NULL;
info->nkeys = 0;
} else {
info->active_key = iwr.u.data.flags & IW_ENCODE_INDEX;
}
}
/* If successful, populate the key array */
for (i = 0; i < info->nkeys; i++) {
iwr.u.data.pointer = info->keys[i].key;
iwr.u.data.length = sizeof(info->keys->key);
iwr.u.data.flags = i + 1; /* counts 1..n instead of 0..n-1 */
if (ioctl(skfd, SIOCGIWENCODE, &iwr) < 0) {
free(info->keys);
info->nkeys = 0;
break;
}
info->keys[i].size = iwr.u.data.length;
info->keys[i].flags = iwr.u.data.flags;
/* Validate whether the current key is indeed active */
if (i + 1 == info->active_key && (info->keys[i].size == 0 ||
(info->keys[i].flags & IW_ENCODE_DISABLED)))
info->active_key = 0;
}
if (ioctl(skfd, SIOCGIWAP, &iwr) >= 0) {
info->cap_ap = 1;
memcpy(&info->ap_addr, &iwr.u.ap_addr, sizeof(struct sockaddr));
}
close(skfd);
}
void dyn_info_cleanup(struct iw_dyn_info *info)
{
if (info)
free(info->keys);
}
/*
* get range information
*/
void iw_getinf_range(const char *ifname, struct iw_range *range)
{
struct iwreq iwr;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
memset(range, 0, sizeof(struct iw_range));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
iwr.u.data.pointer = (caddr_t) range;
iwr.u.data.length = sizeof(struct iw_range);
iwr.u.data.flags = 0;
if (ioctl(skfd, SIOCGIWRANGE, &iwr) < 0)
err_sys("can not get range information");
close(skfd);
}
/*
* Obtain periodic IW statistics
*/
static int rand_wave(float *rlvl, float *step, float *rlvl_next, float range)
{
int i;
for (i = 0; i < WAVE_RAND_SPREAD; i++)
if (*rlvl < *rlvl_next) {
if (*rlvl_next - *rlvl < *step)
*step /= 2.0;
*rlvl += *step;
} else if (*rlvl > *rlvl_next) {
if (*rlvl - *rlvl_next < *step)
*step /= 2.0;
*rlvl -= *step;
}
*step += (random() / (float)RAND_MAX) - 0.5;
if (*rlvl == *rlvl_next || *step < 0.05) {
*rlvl_next = (range * random()) / RAND_MAX;
*step = random() / (float)RAND_MAX;
}
return *rlvl;
}
/* Random signal/noise/quality levels */
static void iw_getstat_random(struct iw_stat *iw)
{
static float rnd_sig, snext, sstep = 1.0, rnd_noise, nnext, nstep = 1.0;
rand_wave(&rnd_sig, &sstep, &snext, conf.sig_max - conf.sig_min);
rand_wave(&rnd_noise, &nstep, &nnext, conf.noise_max - conf.noise_min);
if (iw->range.max_qual.qual == 0)
iw->range.max_qual.qual = WAVE_RAND_QUAL_MAX;
iw->stat.qual.level = dbm_to_u8(conf.sig_min + rnd_sig);
iw->stat.qual.noise = dbm_to_u8(conf.noise_min + rnd_noise);
iw->stat.qual.updated = IW_QUAL_DBM;
iw->stat.qual.qual = map_range(conf.sig_min + rnd_sig,
conf.sig_min, conf.sig_max,
0, iw->range.max_qual.qual);
}
static void iw_getstat_real(struct iw_statistics *stat)
{
struct iwreq wrq;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0)
err_sys("%s: can not open socket", __func__);
wrq.u.data.pointer = (caddr_t) stat;
wrq.u.data.length = sizeof(*stat);
wrq.u.data.flags = 0;
strncpy(wrq.ifr_name, conf_ifname(), IFNAMSIZ);
if (ioctl(skfd, SIOCGIWSTATS, &wrq) < 0) {
/*
* iw_handler_get_iwstats() returns EOPNOTSUPP if
* there are no statistics. Bail out in this case.
*/
if (errno != EOPNOTSUPP)
err_sys("can not obtain iw statistics");
errno = 0;
memset(&wrq, 0, sizeof(wrq));
}
close(skfd);
}
/*
* Generate dBm values and perform sanity checks on values.
* Code in part taken from wireless extensions #30
* @range: range information, read-only
* @qual: wireless statistics, read-write
* @dbm: dBm level information, write-only
*/
void iw_sanitize(struct iw_range *range, struct iw_quality *qual,
struct iw_levelstat *dbm)
{
memset(dbm, 0, sizeof(*dbm));
if (qual->level != 0 || (qual->updated & (IW_QUAL_DBM | IW_QUAL_RCPI))) {
/*
* RCPI (IEEE 802.11k) statistics:
* RCPI = int{(Power in dBm +110)*2}
* for 0 dBm > Power > -110 dBm
*/
if (qual->updated & IW_QUAL_RCPI) {
if (!(qual->updated & IW_QUAL_LEVEL_INVALID))
dbm->signal = (double)(qual->level / 2.0) - 110.0;
if (!(qual->updated & IW_QUAL_NOISE_INVALID))
dbm->noise = (double)(qual->noise / 2.0) - 110.0;
} else if ((qual->updated & IW_QUAL_DBM) ||
qual->level > range->max_qual.level) {
if (!(qual->updated & IW_QUAL_LEVEL_INVALID))
dbm->signal = u8_to_dbm(qual->level);
if (!(qual->updated & IW_QUAL_NOISE_INVALID))
dbm->noise = u8_to_dbm(qual->noise);
} else {
/*
* Relative values (0 -> max)
*/
if (!(qual->updated & IW_QUAL_LEVEL_INVALID))
dbm->signal = mw2dbm(qual->level);
if (!(qual->updated & IW_QUAL_NOISE_INVALID))
dbm->noise = mw2dbm(qual->noise);
}
} else {
qual->updated |= IW_QUAL_ALL_INVALID;
}
/*
* Value sanity checks
*
* These rules serve to avoid "insensible" level displays. Please do send
* comments and/or bug reports if you encounter room for improvement.
*
* 1) if noise level is valid, but signal level is not, displaying just
* the noise level does not reveal very much - can be omitted;
* 2) if the noise level is below an "invalid" magic value (see iw_if.h),
* declare the noise value to be invalid;
* 3) SNR is only displayed if both signal and noise values are valid.
*/
if (qual->updated & IW_QUAL_LEVEL_INVALID)
qual->updated |= IW_QUAL_NOISE_INVALID;
if (dbm->noise <= NOISE_DBM_SANE_MIN)
qual->updated |= IW_QUAL_NOISE_INVALID;
}
void iw_getstat(struct iw_stat *iw)
{
memset(&iw->stat, 0, sizeof(iw->stat));
if (conf.random)
iw_getstat_random(iw);
else
iw_getstat_real(&iw->stat);
iw_sanitize(&iw->range, &iw->stat.qual, &iw->dbm);
}
void dump_parameters(void)
{
struct iw_dyn_info info;
struct iw_stat iw;
struct if_stat nstat;
int i;
iw_getinf_range(conf_ifname(), &iw.range);
dyn_info_get(&info, conf_ifname(), &iw.range);
iw_getstat(&iw);
if_getstat(conf_ifname(), &nstat);
printf("\n");
printf("Configured device: %s (%s)\n", conf_ifname(), info.name);
printf(" Security: %s\n", iw.range.enc_capa ?
format_enc_capab(iw.range.enc_capa, ", ") : "WEP");
if (iw.range.num_encoding_sizes &&
iw.range.num_encoding_sizes < IW_MAX_ENCODING_SIZES) {
printf(" Key sizes: ");
for (i = 0; i < iw.range.num_encoding_sizes; i++) {
if (i)
printf(", ");
if (iw.range.encoding_size[i] == 5)
printf("WEP-40");
else if (iw.range.encoding_size[i] == 13)
printf("WEP-104");
else
printf("%u bits",
iw.range.encoding_size[i] * 8);
}
printf("\n");
}
printf(" WE version: %d (source version %d)\n\n",
iw.range.we_version_compiled, iw.range.we_version_source);
if (info.cap_essid) {
if (info.essid_ct > 1)
printf(" essid: \"%s\" [%d]\n",
info.essid, info.essid_ct);
else if (info.essid_ct)
printf(" essid: \"%s\"\n", info.essid);
else
printf(" essid: off/any\n");
}
if (info.cap_nickname)
printf(" nick: \"%s\"\n", info.nickname);
if (info.cap_nwid) {
if (info.nwid.disabled)
printf(" nwid: off/any\n");
else
printf(" nwid: %X\n", info.nwid.value);
}
/* Some drivers only return the channel (e.g. ipw2100) */
if (info.cap_freq && info.freq < 256)
info.freq = channel_to_freq(info.freq, &iw.range);
if (info.cap_freq && info.freq > 1e3) {
i = freq_to_channel(info.freq, &iw.range);
if (i >= 0)
printf(" channel: %d\n", i);
printf(" frequency: %g GHz\n", info.freq / 1.0e9);
} else
printf(" frequency: n/a\n");
if (info.cap_sens) {
if (info.sens < 0)
printf(" sensitivity: %d dBm\n", info.sens);
else
printf(" sensitivity: %d/%d\n", info.sens,
iw.range.sensitivity);
}
if (info.cap_txpower && info.txpower.disabled)
printf(" tx-power: off\n");
else if (info.cap_txpower && info.txpower.fixed)
printf(" tx-power: %s\n", format_txpower(&info.txpower));
else if (info.cap_txpower)
printf(" TX-power: %s\n", format_txpower(&info.txpower));
printf(" mode: %s\n", iw_opmode(info.mode));
if (info.mode != 1 && info.cap_ap)
printf(" access point: %s\n", format_bssid(&info.ap_addr));
if (info.bitrate)
printf(" bitrate: %g Mbit/s\n", info.bitrate / 1.0e6);
else
printf(" bitrate: n/a\n");
printf(" retry: ");
if (info.cap_retry)
printf("%s\n", format_retry(&info.retry, &iw.range));
else
printf("n/a\n");
printf(" rts thr: ");
if (info.cap_rts) {
if (info.rts.disabled)
printf("off\n");
else
printf("%d B %s\n", info.rts.value,
info.rts.fixed ? "" : "(auto-select)");
} else
printf("n/a\n");
printf(" frag thr: ");
if (info.cap_frag) {
if (info.frag.disabled)
printf("off\n");
else
printf("%d B %s\n", info.frag.value,
info.frag.fixed ? "" : "(auto-select)");
} else {
printf("n/a\n");
}
printf(" encryption: ");
if (!info.nkeys && has_net_admin_capability())
printf("no information available\n");
else if (!info.nkeys)
printf("n/a (requires CAP_NET_ADMIN permissions)\n");
for (i = 0; i < info.nkeys; i++) {
if (i)
printf(" ");
/* Current key is marked by `=' sign */
printf("[%u]%s ", i + 1, i + 1 == info.active_key ? "=" : ":");
if (info.keys[i].flags & IW_ENCODE_DISABLED || !info.keys[i].size) {
printf("off\n");
} else {
printf("%s", format_key(info.keys + i));
if (info.keys[i].flags & IW_ENCODE_RESTRICTED)
printf(", restricted");
if (info.keys[i].flags & IW_ENCODE_OPEN)
printf(", open");
printf("\n");
}
}
printf(" power management: ");
if (info.cap_power)
printf("%s\n", format_power(&info.power, &iw.range));
else
printf("n/a\n");
printf("\n");
printf(" link quality: %d/%d\n", iw.stat.qual.qual,
iw.range.max_qual.qual);
printf(" signal level: %.0f dBm (%s)\n", iw.dbm.signal,
dbm2units(iw.dbm.signal));
printf(" noise level: %.0f dBm (%s)\n", iw.dbm.noise,
dbm2units(iw.dbm.noise));
printf(" SNR: %.0f dB\n", iw.dbm.signal - iw.dbm.noise);
/* RX stats */
printf(" RX total: %'llu packets (%s)\n", nstat.rx_packets,
byte_units(nstat.rx_bytes));
printf(" invalid nwid: %'u\n", iw.stat.discard.nwid);
printf(" invalid key: %'u\n", iw.stat.discard.code);
printf(" invalid fragm.: %'u\n", iw.stat.discard.fragment);
printf(" missed beacons: %'u\n", iw.stat.miss.beacon);
printf(" misc errors: %'u\n", iw.stat.discard.misc);
/* TX stats */
printf(" TX total: %'llu packets (%s)\n", nstat.tx_packets,
byte_units(nstat.tx_bytes));
printf(" exc. MAC retries: %'u\n", iw.stat.discard.retries);
printf("\n");
dyn_info_cleanup(&info);
}