diff options
author | jow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73> | 2012-02-23 12:18:46 +0000 |
---|---|---|
committer | jow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73> | 2012-02-23 12:18:46 +0000 |
commit | 250113e1c6c0ad9498f03b4e58d2aaaf7588fffd (patch) | |
tree | 658dfc140736c5db0ece9e6cdda5b23580a78e38 /package | |
parent | 2f20008d73b76b05007ed54fe813a564ae6c3c98 (diff) |
[package] iwinfo: implement netlink scanning code, rework IE parsing code
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@30691 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package')
-rw-r--r-- | package/iwinfo/Makefile | 2 | ||||
-rw-r--r-- | package/iwinfo/src/include/iwinfo/nl80211.h | 11 | ||||
-rw-r--r-- | package/iwinfo/src/include/iwinfo/utils.h | 3 | ||||
-rw-r--r-- | package/iwinfo/src/iwinfo_cli.c | 6 | ||||
-rw-r--r-- | package/iwinfo/src/iwinfo_nl80211.c | 471 | ||||
-rw-r--r-- | package/iwinfo/src/iwinfo_utils.c | 105 | ||||
-rw-r--r-- | package/iwinfo/src/iwinfo_wext_scan.c | 152 |
7 files changed, 510 insertions, 240 deletions
diff --git a/package/iwinfo/Makefile b/package/iwinfo/Makefile index 63ccec9b1..079fe682f 100644 --- a/package/iwinfo/Makefile +++ b/package/iwinfo/Makefile @@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=libiwinfo -PKG_RELEASE:=29 +PKG_RELEASE:=30 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) PKG_CONFIG_DEPENDS := \ diff --git a/package/iwinfo/src/include/iwinfo/nl80211.h b/package/iwinfo/src/include/iwinfo/nl80211.h index fd0ddb43a..7bbf0ef7b 100644 --- a/package/iwinfo/src/include/iwinfo/nl80211.h +++ b/package/iwinfo/src/include/iwinfo/nl80211.h @@ -39,6 +39,7 @@ struct nl80211_state { struct nl_sock *nl_sock; struct nl_cache *nl_cache; struct genl_family *nl80211; + struct genl_family *nlctrl; }; struct nl80211_msg_conveyor { @@ -46,6 +47,16 @@ struct nl80211_msg_conveyor { struct nl_cb *cb; }; +struct nl80211_event_conveyor { + int wait; + int recv; +}; + +struct nl80211_group_conveyor { + const char *name; + int id; +}; + struct nl80211_rssi_rate { int16_t rate; int8_t rssi; diff --git a/package/iwinfo/src/include/iwinfo/utils.h b/package/iwinfo/src/include/iwinfo/utils.h index 10246a8e3..d58ec5fb7 100644 --- a/package/iwinfo/src/include/iwinfo/utils.h +++ b/package/iwinfo/src/include/iwinfo/utils.h @@ -41,4 +41,7 @@ struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id); int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id); +void iwinfo_parse_rsn(struct iwinfo_crypto_entry *c, uint8_t *data, uint8_t len, + uint8_t defcipher, uint8_t defauth); + #endif diff --git a/package/iwinfo/src/iwinfo_cli.c b/package/iwinfo/src/iwinfo_cli.c index dc8ed8b14..186cce143 100644 --- a/package/iwinfo/src/iwinfo_cli.c +++ b/package/iwinfo/src/iwinfo_cli.c @@ -230,19 +230,19 @@ static char * format_encryption(struct iwinfo_crypto_entry *c) case 3: snprintf(buf, sizeof(buf), "mixed WPA/WPA2 %s (%s)", format_enc_suites(c->auth_suites), - format_enc_ciphers(c->pair_ciphers & c->group_ciphers)); + format_enc_ciphers(c->pair_ciphers | c->group_ciphers)); break; case 2: snprintf(buf, sizeof(buf), "WPA2 %s (%s)", format_enc_suites(c->auth_suites), - format_enc_ciphers(c->pair_ciphers & c->group_ciphers)); + format_enc_ciphers(c->pair_ciphers | c->group_ciphers)); break; case 1: snprintf(buf, sizeof(buf), "WPA %s (%s)", format_enc_suites(c->auth_suites), - format_enc_ciphers(c->pair_ciphers & c->group_ciphers)); + format_enc_ciphers(c->pair_ciphers | c->group_ciphers)); break; } } diff --git a/package/iwinfo/src/iwinfo_nl80211.c b/package/iwinfo/src/iwinfo_nl80211.c index 05d820349..528615079 100644 --- a/package/iwinfo/src/iwinfo_nl80211.c +++ b/package/iwinfo/src/iwinfo_nl80211.c @@ -47,26 +47,30 @@ static int nl80211_init(void) goto err; } - if( genl_connect(nls->nl_sock)) { + if (genl_connect(nls->nl_sock)) { err = -ENOLINK; goto err; } fd = nl_socket_get_fd(nls->nl_sock); - if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) - { + if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) { err = -EINVAL; goto err; } - if( genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) { + if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) { err = -ENOMEM; goto err; } nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211"); - if (!nls->nl80211) - { + if (!nls->nl80211) { + err = -ENOENT; + goto err; + } + + nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl"); + if (!nls->nlctrl) { err = -ENOENT; goto err; } @@ -80,6 +84,7 @@ err: return err; } + static int nl80211_msg_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg) { @@ -122,29 +127,14 @@ static void nl80211_free(struct nl80211_msg_conveyor *cv) } } -static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, int flags) +static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family, + int cmd, int flags) { static struct nl80211_msg_conveyor cv; - int ifidx = -1, phyidx = -1; struct nl_msg *req = NULL; struct nl_cb *cb = NULL; - if (nl80211_init() < 0) - goto err; - - if (!strncmp(ifname, "phy", 3)) - phyidx = atoi(&ifname[3]); - else if (!strncmp(ifname, "radio", 5)) - phyidx = atoi(&ifname[5]); - else if (!strncmp(ifname, "mon.", 4)) - ifidx = if_nametoindex(&ifname[4]); - else - ifidx = if_nametoindex(ifname); - - if ((ifidx < 0) && (phyidx < 0)) - return NULL; - req = nlmsg_alloc(); if (!req) goto err; @@ -153,14 +143,7 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, in if (!cb) goto err; - genlmsg_put(req, 0, 0, genl_family_get_id(nls->nl80211), 0, - flags, cmd, 0); - - if (ifidx > -1) - NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx); - - if (phyidx > -1) - NLA_PUT_U32(req, NL80211_ATTR_WIPHY, phyidx); + genlmsg_put(req, 0, 0, genl_family_get_id(family), 0, flags, cmd, 0); cv.msg = req; cv.cb = cb; @@ -178,6 +161,52 @@ nla_put_failure: return NULL; } +static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags) +{ + if (nl80211_init() < 0) + return NULL; + + return nl80211_new(nls->nlctrl, cmd, flags); +} + +static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, + int cmd, int flags) +{ + int ifidx = -1, phyidx = -1; + struct nl80211_msg_conveyor *cv; + + if (nl80211_init() < 0) + return NULL; + + if (!strncmp(ifname, "phy", 3)) + phyidx = atoi(&ifname[3]); + else if (!strncmp(ifname, "radio", 5)) + phyidx = atoi(&ifname[5]); + else if (!strncmp(ifname, "mon.", 4)) + ifidx = if_nametoindex(&ifname[4]); + else + ifidx = if_nametoindex(ifname); + + if ((ifidx < 0) && (phyidx < 0)) + return NULL; + + cv = nl80211_new(nls->nl80211, cmd, flags); + if (!cv) + return NULL; + + if (ifidx > -1) + NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx); + + if (phyidx > -1) + NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx); + + return cv; + +nla_put_failure: + nl80211_free(cv); + return NULL; +} + static struct nl80211_msg_conveyor * nl80211_send( struct nl80211_msg_conveyor *cv, int (*cb_func)(struct nl_msg *, void *), void *cb_arg @@ -215,20 +244,111 @@ static struct nlattr ** nl80211_parse(struct nl_msg *msg) static struct nlattr *attr[NL80211_ATTR_MAX + 1]; nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), - genlmsg_attrlen(gnlh, 0), NULL); + genlmsg_attrlen(gnlh, 0), NULL); return attr; } + +static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg) +{ + struct nl80211_group_conveyor *cv = arg; + + struct nlattr **attr = nl80211_parse(msg); + struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1]; + struct nlattr *mgrp; + int mgrpidx; + + if (!attr[CTRL_ATTR_MCAST_GROUPS]) + return NL_SKIP; + + nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx) + { + nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX, + nla_data(mgrp), nla_len(mgrp), NULL); + + if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] && + mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] && + !strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]), + cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]))) + { + cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]); + break; + } + } + + return NL_SKIP; +} + +static int nl80211_subscribe(const char *family, const char *group) +{ + struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT }; + struct nl80211_msg_conveyor *req; + + req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0); + if (req) + { + NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family); + nl80211_send(req, nl80211_subscribe_cb, &cv); + +nla_put_failure: + nl80211_free(req); + } + + return nl_socket_add_membership(nls->nl_sock, cv.id); +} + + +static int nl80211_wait_cb(struct nl_msg *msg, void *arg) +{ + struct nl80211_event_conveyor *cv = arg; + struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); + + if (gnlh->cmd == cv->wait) + cv->recv = gnlh->cmd; + + return NL_SKIP; +} + +static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg) +{ + return NL_OK; +} + +static int nl80211_wait(const char *family, const char *group, int cmd) +{ + struct nl80211_event_conveyor cv = { .wait = cmd }; + struct nl_cb *cb; + + if (nl80211_subscribe(family, group)) + return -ENOENT; + + cb = nl_cb_alloc(NL_CB_DEFAULT); + + if (!cb) + return -ENOMEM; + + nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv ); + + while (!cv.recv) + nl_recvmsgs(nls->nl_sock, cb); + + nl_cb_put(cb); + + return 0; +} + + static int nl80211_freq2channel(int freq) { - if (freq == 2484) - return 14; + if (freq == 2484) + return 14; - if (freq < 2484) - return (freq - 2407) / 5; + if (freq < 2484) + return (freq - 2407) / 5; - return (freq / 5) - 1000; + return (freq / 5) - 1000; } static char * nl80211_getval(const char *ifname, const char *buf, const char *key) @@ -285,7 +405,8 @@ static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg) struct nlattr **attr = nl80211_parse(msg); if (attr[NL80211_ATTR_WIPHY_NAME]) - sprintf(buf, "%s", nla_data(attr[NL80211_ATTR_WIPHY_NAME])); + memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]), + nla_len(attr[NL80211_ATTR_WIPHY_NAME])); else buf[0] = 0; @@ -380,8 +501,8 @@ static char * nl80211_wpactl_info(const char *ifname, const char *cmd, goto out; local.sun_family = AF_UNIX; - local_length = sizeof(local.sun_family) + sprintf(local.sun_path, - "/var/run/iwinfo-%s-%d", ifname, getpid()); + local_length = sizeof(local.sun_family) + + sprintf(local.sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid()); if (bind(sock, (struct sockaddr *) &local, local_length)) goto out; @@ -462,18 +583,18 @@ static char * nl80211_phy2ifname(const char *ifname) { if ((d = opendir("/sys/class/net")) != NULL) { - while( (e = readdir(d)) != NULL ) + while ((e = readdir(d)) != NULL) { snprintf(buffer, sizeof(buffer), - "/sys/class/net/%s/phy80211/index", e->d_name); + "/sys/class/net/%s/phy80211/index", e->d_name); if (nl80211_readint(buffer) == phyidx) { snprintf(buffer, sizeof(buffer), - "/sys/class/net/%s/ifindex", e->d_name); + "/sys/class/net/%s/ifindex", e->d_name); - if( (cifidx = nl80211_readint(buffer)) >= 0 && - ((ifidx < 0) || (cifidx < ifidx)) ) + if ((cifidx = nl80211_readint(buffer)) >= 0 && + ((ifidx < 0) || (cifidx < ifidx))) { ifidx = cifidx; strncpy(nif, e->d_name, sizeof(nif)); @@ -562,6 +683,9 @@ void nl80211_close(void) { if (nls) { + if (nls->nlctrl) + genl_family_put(nls->nlctrl); + if (nls->nl80211) genl_family_put(nls->nl80211); @@ -589,8 +713,8 @@ int nl80211_get_ssid(const char *ifname, char *buf) { return 0; } - else if( (ssid = nl80211_hostapd_info(ifname)) && - (ssid = nl80211_getval(ifname, ssid, "ssid")) ) + else if ((ssid = nl80211_hostapd_info(ifname)) && + (ssid = nl80211_getval(ifname, ssid, "ssid"))) { memcpy(buf, ssid, strlen(ssid)); return 0; @@ -608,7 +732,7 @@ int nl80211_get_bssid(const char *ifname, char *buf) { return 0; } - else if((bssid = nl80211_hostapd_info(ifname)) && + else if ((bssid = nl80211_hostapd_info(ifname)) && (bssid = nl80211_getval(ifname, bssid, "bssid"))) { mac[0] = strtol(&bssid[0], NULL, 16); @@ -619,7 +743,7 @@ int nl80211_get_bssid(const char *ifname, char *buf) mac[5] = strtol(&bssid[15], NULL, 16); sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return 0; } @@ -690,8 +814,8 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg) if (attr[NL80211_ATTR_STA_INFO]) { - if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, - attr[NL80211_ATTR_STA_INFO], stats_policy) ) + if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, + attr[NL80211_ATTR_STA_INFO], stats_policy)) { if (sinfo[NL80211_STA_INFO_SIGNAL]) { @@ -701,8 +825,9 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg) if (sinfo[NL80211_STA_INFO_TX_BITRATE]) { - if( !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, - sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy) ) + if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, + sinfo[NL80211_STA_INFO_TX_BITRATE], + rate_policy)) { if (rinfo[NL80211_RATE_INFO_BITRATE]) { @@ -732,11 +857,11 @@ static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r) while ((de = readdir(d)) != NULL) { if (!strncmp(de->d_name, ifname, strlen(ifname)) && - (!de->d_name[strlen(ifname)] || - !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) + (!de->d_name[strlen(ifname)] || + !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) { req = nl80211_msg(de->d_name, NL80211_CMD_GET_STATION, - NLM_F_DUMP); + NLM_F_DUMP); if (req) { @@ -801,7 +926,7 @@ static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg) return NL_SKIP; if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX, - tb[NL80211_ATTR_SURVEY_INFO], sp)) + tb[NL80211_ATTR_SURVEY_INFO], sp)) return NL_SKIP; if (!si[NL80211_SURVEY_INFO_NOISE]) @@ -890,8 +1015,8 @@ int nl80211_get_encryption(const char *ifname, char *buf) struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf; /* WPA supplicant */ - if( (res = nl80211_wpactl_info(ifname, "STATUS", NULL)) && - (val = nl80211_getval(NULL, res, "pairwise_cipher")) ) + if ((res = nl80211_wpactl_info(ifname, "STATUS", NULL)) && + (val = nl80211_getval(NULL, res, "pairwise_cipher"))) { /* WEP */ if (strstr(val, "WEP")) @@ -1021,7 +1146,7 @@ int nl80211_get_encryption(const char *ifname, char *buf) break; } - for( i = 0; i < 4; i++ ) + for (i = 0; i < 4; i++) { snprintf(k, sizeof(k), "wep_key%d", i); @@ -1078,8 +1203,8 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6); if (attr[NL80211_ATTR_STA_INFO] && - !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, - attr[NL80211_ATTR_STA_INFO], stats_policy)) + !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, + attr[NL80211_ATTR_STA_INFO], stats_policy)) { if (sinfo[NL80211_STA_INFO_SIGNAL]) e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); @@ -1094,8 +1219,8 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]); if (sinfo[NL80211_STA_INFO_RX_BITRATE] && - !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, - sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) + !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, + sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) { if (rinfo[NL80211_RATE_INFO_BITRATE]) e->rx_rate.rate = @@ -1112,8 +1237,8 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) } if (sinfo[NL80211_STA_INFO_TX_BITRATE] && - !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, - sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) + !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, + sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) { if (rinfo[NL80211_RATE_INFO_BITRATE]) e->tx_rate.rate = @@ -1150,11 +1275,11 @@ int nl80211_get_assoclist(const char *ifname, char *buf, int *len) while ((de = readdir(d)) != NULL) { if (!strncmp(de->d_name, ifname, strlen(ifname)) && - (!de->d_name[strlen(ifname)] || - !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) + (!de->d_name[strlen(ifname)] || + !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) { req = nl80211_msg(de->d_name, NL80211_CMD_GET_STATION, - NLM_F_DUMP); + NLM_F_DUMP); if (req) { @@ -1202,19 +1327,18 @@ static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg) nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) { nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band), - nla_len(band), NULL); + nla_len(band), NULL); - nla_for_each_nested(freq, - bands[NL80211_BAND_ATTR_FREQS], freqs_remain) + nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain) { nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, - nla_data(freq), nla_len(freq), freq_policy); + nla_data(freq), nla_len(freq), freq_policy); - ch_cmp = nl80211_freq2channel( - nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ])); + ch_cmp = nl80211_freq2channel(nla_get_u32( + freqs[NL80211_FREQUENCY_ATTR_FREQ])); - if( (!ch_cur || (ch_cmp == ch_cur)) && - freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] ) + if ((!ch_cur || (ch_cmp == ch_cur)) && + freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) { *dbm_max = (int)(0.01 * nla_get_u32( freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])); @@ -1322,6 +1446,157 @@ static void nl80211_get_scancrypto(const char *spec, } } + +struct nl80211_scanlist { + struct iwinfo_scanlist_entry *e; + int len; +}; + + +static void nl80211_get_scanlist_ie(struct nlattr **bss, + struct iwinfo_scanlist_entry *e) +{ + int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); + unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); + static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 }; + + while (ielen >= 2 && ielen >= ie[1]) + { + switch (ie[0]) + { + case 0: /* SSID */ + memcpy(e->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE)); + break; + + case 48: /* RSN */ + iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1], + IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x); + break; + + case 221: /* Vendor */ + if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1) + iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4, + IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK); + break; + } + + ielen -= ie[1] + 2; + ie += ie[1] + 2; + } +} + +static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg) +{ + int8_t rssi; + uint16_t caps; + + struct nl80211_scanlist *sl = arg; + struct nlattr **tb = nl80211_parse(msg); + struct nlattr *bss[NL80211_BSS_MAX + 1]; + + static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { + [NL80211_BSS_TSF] = { .type = NLA_U64 }, + [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, + [NL80211_BSS_BSSID] = { }, + [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, + [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, + [NL80211_BSS_INFORMATION_ELEMENTS] = { }, + [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, + [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, + [NL80211_BSS_STATUS] = { .type = NLA_U32 }, + [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, + [NL80211_BSS_BEACON_IES] = { }, + }; + + if (!tb[NL80211_ATTR_BSS] || + nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], + bss_policy) || + !bss[NL80211_BSS_BSSID]) + { + return NL_SKIP; + } + + if (bss[NL80211_BSS_CAPABILITY]) + caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); + else + caps = 0; + + memset(sl->e, 0, sizeof(*sl->e)); + memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6); + + if (caps & (1<<1)) + memcpy(sl->e->mode, "Ad-Hoc", 6); + else + memcpy(sl->e->mode, "Master", 6); + + if (caps & (1<<4)) + sl->e->crypto.enabled = 1; + + if (bss[NL80211_BSS_FREQUENCY]) + sl->e->channel = nl80211_freq2channel(nla_get_u32( + bss[NL80211_BSS_FREQUENCY])); + + if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) + nl80211_get_scanlist_ie(bss, sl->e); + + if (bss[NL80211_BSS_SIGNAL_MBM]) + { + sl->e->signal = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100; + + rssi = sl->e->signal - 0x100; + + if (rssi < -110) + rssi = -110; + else if (rssi > -40) + rssi = -40; + + sl->e->quality = (rssi + 110); + sl->e->quality_max = 70; + } + + if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version) + { + sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; + sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104; + } + + sl->e++; + sl->len++; + + return NL_SKIP; +} + +static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len) +{ + struct nl_msg *ssids = NULL; + struct nl80211_msg_conveyor *req; + struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf }; + + req = nl80211_msg(ifname, NL80211_CMD_TRIGGER_SCAN, 0); + if (req) + { + nl80211_send(req, NULL, NULL); + nl80211_free(req); + } + + nl80211_wait("nl80211", "scan", NL80211_CMD_NEW_SCAN_RESULTS); + + req = nl80211_msg(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP); + if (req) + { + nl80211_send(req, nl80211_get_scanlist_cb, &sl); + nl80211_free(req); + } + + *len = sl.len * sizeof(struct iwinfo_scanlist_entry); + return *len ? 0 : -1; + +nla_put_failure: + if (ssids) + nlmsg_free(ssids); + return -1; +} + int nl80211_get_scanlist(const char *ifname, char *buf, int *len) { int freq, rssi, qmax, count; @@ -1358,12 +1633,12 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len) nl80211_get_quality_max(ifname, &qmax); /* skip header line */ - while( *res++ != '\n' ); + while (*res++ != '\n'); count = 0; - while( sscanf(res, "%17s %d %d %255s%*[ \t]%127[^\n]\n", - bssid, &freq, &rssi, cipher, ssid) > 0 ) + while (sscanf(res, "%17s %d %d %255s%*[ \t]%127[^\n]\n", + bssid, &freq, &rssi, cipher, ssid) > 0) { /* BSSID */ e->mac[0] = strtol(&bssid[0], NULL, 16); @@ -1374,8 +1649,7 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len) e->mac[5] = strtol(&bssid[15], NULL, 16); /* SSID */ - memcpy(e->ssid, ssid, - min(strlen(ssid), sizeof(e->ssid) - 1)); + memcpy(e->ssid, ssid, min(strlen(ssid), sizeof(e->ssid) - 1)); /* Mode (assume master) */ sprintf((char *)e->mode, "Master"); @@ -1435,7 +1709,7 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len) if (!iwinfo_ifup(ifname)) return -1; - wext_get_scanlist(ifname, buf, len); + nl80211_get_scanlist_nl(ifname, buf, len); iwinfo_ifdown(ifname); return 0; } @@ -1453,7 +1727,7 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len) * additional interface and there's no need to tear down the ap */ if (iwinfo_ifup(res)) { - wext_get_scanlist(res, buf, len); + nl80211_get_scanlist_nl(res, buf, len); iwinfo_ifdown(res); } @@ -1461,7 +1735,7 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len) * during scan */ else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res)) { - wext_get_scanlist(res, buf, len); + nl80211_get_scanlist_nl(res, buf, len); iwinfo_ifdown(res); iwinfo_ifup(ifname); nl80211_hostapd_hup(ifname); @@ -1499,17 +1773,16 @@ static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg) nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) { - nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band), - nla_len(band), NULL); + nla_parse(bands, NL80211_BAND_ATTR_MAX, + nla_data(band), nla_len(band), NULL); - nla_for_each_nested(freq, - bands[NL80211_BAND_ATTR_FREQS], freqs_remain) + nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain) { nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, - nla_data(freq), nla_len(freq), NULL); + nla_data(freq), nla_len(freq), NULL); - if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] || - freqs[NL80211_FREQUENCY_ATTR_DISABLED] ) + if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] || + freqs[NL80211_FREQUENCY_ATTR_DISABLED]) continue; e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]); @@ -1587,7 +1860,7 @@ int nl80211_get_countrylist(const char *ifname, char *buf, int *len) struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf; const struct iwinfo_iso3166_label *l; - for( l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++ ) + for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++) { e->iso3166 = l->iso3166; e->ccode[0] = (l->iso3166 / 256); @@ -1614,8 +1887,8 @@ static int nl80211_get_hwmodelist_cb(struct nl_msg *msg, void *arg) { nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) { - nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band), - nla_len(band), NULL); + nla_parse(bands, NL80211_BAND_ATTR_MAX, + nla_data(band), nla_len(band), NULL); if (bands[NL80211_BAND_ATTR_HT_CAPA]) caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]); @@ -1624,11 +1897,11 @@ static int nl80211_get_hwmodelist_cb(struct nl_msg *msg, void *arg) if (caps > 0) *modes |= IWINFO_80211_N; - nla_for_each_nested(freq, - bands[NL80211_BAND_ATTR_FREQS], freqs_remain) + nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], + freqs_remain) { nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, - nla_data(freq), nla_len(freq), NULL); + nla_data(freq), nla_len(freq), NULL); if (!freqs[NL80211_FREQUENCY_ATTR_FREQ]) continue; @@ -1710,7 +1983,7 @@ int nl80211_get_hardware_id(const char *ifname, char *buf) /* Failed to obtain hardware IDs, search board config */ if (rv) { - rv = iwinfo_hardware_id_from_mtd(buf); + rv = iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf); } return rv; diff --git a/package/iwinfo/src/iwinfo_utils.c b/package/iwinfo/src/iwinfo_utils.c index b21f791a6..514f34bf9 100644 --- a/package/iwinfo/src/iwinfo_utils.c +++ b/package/iwinfo/src/iwinfo_utils.c @@ -145,7 +145,7 @@ struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id) (e->subsystem_device_id != id->subsystem_device_id)) continue; - return e; + return (struct iwinfo_hardware_entry *)e; } return NULL; @@ -221,3 +221,106 @@ int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id) return (id->vendor_id && id->device_id) ? 0 : -1; } + +void iwinfo_parse_rsn(struct iwinfo_crypto_entry *c, uint8_t *data, uint8_t len, + uint8_t defcipher, uint8_t defauth) +{ + uint16_t i, count; + + static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 }; + static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac }; + + data += 2; + len -= 2; + + if (!memcmp(data, ms_oui, 3)) + c->wpa_version += 1; + else if (!memcmp(data, ieee80211_oui, 3)) + c->wpa_version += 2; + + if (len < 4) + { + c->group_ciphers |= defcipher; + c->pair_ciphers |= defcipher; + c->auth_suites |= defauth; + return; + } + + if (!memcmp(data, ms_oui, 3) || !memcmp(data, ieee80211_oui, 3)) + { + switch (data[3]) + { + case 1: c->group_ciphers |= IWINFO_CIPHER_WEP40; break; + case 2: c->group_ciphers |= IWINFO_CIPHER_TKIP; break; + case 4: c->group_ciphers |= IWINFO_CIPHER_CCMP; break; + case 5: c->group_ciphers |= IWINFO_CIPHER_WEP104; break; + case 6: /* AES-128-CMAC */ break; + default: /* proprietary */ break; + } + } + + data += 4; + len -= 4; + + if (len < 2) + { + c->pair_ciphers |= defcipher; + c->auth_suites |= defauth; + return; + } + + count = data[0] | (data[1] << 8); + if (2 + (count * 4) > len) + return; + + for (i = 0; i < count; i++) + { + if (!memcmp(data + 2 + (i * 4), ms_oui, 3) || + !memcmp(data + 2 + (i * 4), ieee80211_oui, 3)) + { + switch (data[2 + (i * 4) + 3]) + { + case 1: c->pair_ciphers |= IWINFO_CIPHER_WEP40; break; + case 2: c->pair_ciphers |= IWINFO_CIPHER_TKIP; break; + case 4: c->pair_ciphers |= IWINFO_CIPHER_CCMP; break; + case 5: c->pair_ciphers |= IWINFO_CIPHER_WEP104; break; + case 6: /* AES-128-CMAC */ break; + default: /* proprietary */ break; + } + } + } + + data += 2 + (count * 4); + len -= 2 + (count * 4); + + if (len < 2) + { + c->auth_suites |= defauth; + return; + } + + count = data[0] | (data[1] << 8); + if (2 + (count * 4) > len) + return; + + for (i = 0; i < count; i++) + { + if (!memcmp(data + 2 + (i * 4), ms_oui, 3) || + !memcmp(data + 2 + (i * 4), ieee80211_oui, 3)) + { + switch (data[2 + (i * 4) + 3]) + { + case 1: c->auth_suites |= IWINFO_KMGMT_8021x; break; + case 2: c->auth_suites |= IWINFO_KMGMT_PSK; break; + case 3: /* FT/IEEE 802.1X */ break; + case 4: /* FT/PSK */ break; + case 5: /* IEEE 802.1X/SHA-256 */ break; + case 6: /* PSK/SHA-256 */ break; + default: /* proprietary */ break; + } + } + } + + data += 2 + (count * 4); + len -= 2 + (count * 4); +} diff --git a/package/iwinfo/src/iwinfo_wext_scan.c b/package/iwinfo/src/iwinfo_wext_scan.c index de0258799..0a0093305 100644 --- a/package/iwinfo/src/iwinfo_wext_scan.c +++ b/package/iwinfo/src/iwinfo_wext_scan.c @@ -213,132 +213,28 @@ static inline int wext_extract_event(struct stream_descr *stream, struct iw_even return 1; } -static inline void wext_fill_wpa(unsigned char *iebuf, int buflen, struct iwinfo_scanlist_entry *e) +static inline void wext_fill_wpa(unsigned char *iebuf, int ielen, struct iwinfo_scanlist_entry *e) { - int ielen = iebuf[1] + 2; - int offset = 2; /* Skip the IE id, and the length. */ - unsigned char wpa1_oui[3] = {0x00, 0x50, 0xf2}; - unsigned char wpa2_oui[3] = {0x00, 0x0f, 0xac}; - unsigned char *wpa_oui; - int i; - uint16_t ver = 0; - uint16_t cnt = 0; - int wpa1 = 0, wpa2 = 0; - char buf[256]; - - struct iwinfo_crypto_entry *ce = &e->crypto; - - if(ielen > buflen) - ielen = buflen; + static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 }; - switch(iebuf[0]) + while (ielen >= 2 && ielen >= iebuf[1]) { - case 0x30: /* WPA2 */ - /* Check if we have enough data */ - if(ielen < 4) - return; - - wpa_oui = wpa2_oui; + switch (iebuf[0]) + { + case 48: /* RSN */ + iwinfo_parse_rsn(&e->crypto, iebuf + 2, iebuf[1], + IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x); break; - case 0xdd: /* WPA or else */ - wpa_oui = wpa1_oui; - /* Not all IEs that start with 0xdd are WPA. - * * So check that the OUI is valid. */ - if((ielen < 8) || ((memcmp(&iebuf[offset], wpa_oui, 3) != 0) - || (iebuf[offset+3] != 0x01))) - return; - - offset += 4; + case 221: /* Vendor */ + if (iebuf[1] >= 4 && !memcmp(iebuf + 2, ms_oui, 3) && iebuf[5] == 1) + iwinfo_parse_rsn(&e->crypto, iebuf + 6, iebuf[1] - 4, + IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK); break; + } - default: - return; - } - - /* Pick version number (little endian) */ - ver = iebuf[offset] | (iebuf[offset + 1] << 8); - offset += 2; - - if(iebuf[0] == 0xdd) - wpa1 = 1; - - if(iebuf[0] == 0x30) - wpa2 = 1; - - if( wpa1 && (ce->wpa_version == 2) ) - ce->wpa_version = 3; - else if( wpa2 && (ce->wpa_version == 1) ) - ce->wpa_version = 3; - else if( wpa1 && !ce->wpa_version ) - ce->wpa_version = 1; - else if( wpa2 && !ce->wpa_version ) - ce->wpa_version = 2; - - if(ielen < (offset + 4)) - { - ce->group_ciphers |= (1<<2); /* TKIP */ - ce->pair_ciphers |= (1<<2); /* TKIP */ - ce->auth_suites |= (1<<2); /* PSK */ - return; - } - - if(memcmp(&iebuf[offset], wpa_oui, 3) != 0) - ce->group_ciphers |= (1<<7); /* Proprietary */ - else - ce->group_ciphers |= (1<<iebuf[offset+3]); - - offset += 4; - - if(ielen < (offset + 2)) - { - ce->pair_ciphers |= (1<<2); /* TKIP */ - ce->auth_suites |= (1<<2); /* PSK */ - return; - } - - /* Otherwise, we have some number of pairwise ciphers. */ - cnt = iebuf[offset] | (iebuf[offset + 1] << 8); - offset += 2; - - if(ielen < (offset + 4*cnt)) - return; - - *buf = '\0'; - for(i = 0; i < cnt; i++) - { - if(memcmp(&iebuf[offset], wpa_oui, 3) != 0) - ce->pair_ciphers |= (1<<7); /* Proprietary */ - else if(iebuf[offset+3] <= IW_IE_CYPHER_NUM) - ce->pair_ciphers |= (1<<iebuf[offset+3]); - //else - // ce->pair_ciphers[ce->pair_cipher_num++] = 255; /* Unknown */ - - offset += 4; - } - - /* Check if we are done */ - if(ielen < (offset + 2)) - return; - - /* Now, we have authentication suites. */ - cnt = iebuf[offset] | (iebuf[offset + 1] << 8); - offset += 2; - *buf = '\0'; - - if(ielen < (offset + 4*cnt)) - return; - - for(i = 0; i < cnt; i++) - { - if(memcmp(&iebuf[offset], wpa_oui, 3) != 0) - ce->auth_suites |= (1<<7); /* Proprietary */ - else if(iebuf[offset+3] <= IW_IE_KEY_MGMT_NUM) - ce->auth_suites |= (1<<iebuf[offset+3]); - //else - // ce->auth_suites[ce->auth_suite_num++] = 255; /* Unknown */ - - offset += 4; + ielen -= iebuf[1] + 2; + iebuf += iebuf[1] + 2; } } @@ -433,23 +329,7 @@ static inline void wext_fill_entry(struct stream_descr *stream, struct iw_event break; #endif case IWEVGENIE: - i = 0; - - while(i <= (event->u.data.length - 2)) - { - switch(((unsigned char *)event->u.data.pointer)[i]) - { - case 0xdd: /* WPA1 (and other) */ - case 0x30: /* WPA2 */ - wext_fill_wpa((unsigned char *)event->u.data.pointer + i, - event->u.data.length, e); - - break; - } - - i += ((unsigned char *)event->u.data.pointer)[i+1] + 2; - } - + wext_fill_wpa(event->u.data.pointer, event->u.data.length, e); break; } } |