2 * Copyright(c) 2004 Intel Corporation. All rights reserved.
4 * Portions of this file are based on the WEP enablement code provided by the
5 * Host AP project hostap-drivers v0.1.3
6 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
8 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The full GNU General Public License is included in this distribution in the
24 * file called LICENSE.
26 * Contact Information:
27 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
28 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 #include <linux/wireless.h>
32 #include <linux/kmod.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/etherdevice.h>
37 #include "ieee80211.h"
38 static const char *ieee80211_modes[] = {
39 "?", "a", "b", "ab", "g", "ag", "bg", "abg"
42 #define MAX_CUSTOM_LEN 64
43 static inline char *rtl818x_translate_scan(struct ieee80211_device *ieee,
44 char *start, char *stop,
45 struct ieee80211_network *network,
46 struct iw_request_info *info)
48 char custom[MAX_CUSTOM_LEN];
54 /* First entry *MUST* be the AP MAC address */
56 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
57 ether_addr_copy(iwe.u.ap_addr.sa_data, network->bssid);
58 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
60 /* Remaining entries will be displayed in the order we provide them */
63 iwe.cmd = SIOCGIWESSID;
65 if (network->ssid_len == 0) {
66 iwe.u.data.length = sizeof("<hidden>");
67 start = iwe_stream_add_point(info, start, stop, &iwe, "<hidden>");
69 iwe.u.data.length = min_t(u8, network->ssid_len, 32);
70 start = iwe_stream_add_point(info, start, stop, &iwe, network->ssid);
72 /* Add the protocol name */
73 iwe.cmd = SIOCGIWNAME;
74 snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11%s", ieee80211_modes[network->mode]);
75 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_CHAR_LEN);
78 iwe.cmd = SIOCGIWMODE;
79 if (network->capability &
80 (WLAN_CAPABILITY_BSS | WLAN_CAPABILITY_IBSS)) {
81 if (network->capability & WLAN_CAPABILITY_BSS)
82 iwe.u.mode = IW_MODE_MASTER;
84 iwe.u.mode = IW_MODE_ADHOC;
86 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
89 /* Add frequency/channel */
90 iwe.cmd = SIOCGIWFREQ;
91 iwe.u.freq.m = network->channel;
94 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
96 /* Add encryption capability */
97 iwe.cmd = SIOCGIWENCODE;
98 if (network->capability & WLAN_CAPABILITY_PRIVACY)
99 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
101 iwe.u.data.flags = IW_ENCODE_DISABLED;
102 iwe.u.data.length = 0;
103 start = iwe_stream_add_point(info, start, stop, &iwe, network->ssid);
105 /* Add basic and extended rates */
108 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): ");
109 for (i = 0, j = 0; i < network->rates_len; ) {
110 if (j < network->rates_ex_len &&
111 ((network->rates_ex[j] & 0x7F) <
112 (network->rates[i] & 0x7F)))
113 rate = network->rates_ex[j++] & 0x7F;
115 rate = network->rates[i++] & 0x7F;
118 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
119 "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
121 for (; j < network->rates_ex_len; j++) {
122 rate = network->rates_ex[j] & 0x7F;
123 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
124 "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
129 iwe.cmd = SIOCGIWRATE;
130 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
131 iwe.u.bitrate.value = max_rate * 500000;
132 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_PARAM_LEN);
134 iwe.cmd = IWEVCUSTOM;
135 iwe.u.data.length = p - custom;
136 if (iwe.u.data.length)
137 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
139 /* Add quality statistics */
140 /* TODO: Fix these values... */
141 if (network->stats.signal == 0 || network->stats.rssi == 0)
142 netdev_info(ieee->dev, "========>signal:%d, rssi:%d\n",
143 network->stats.signal, network->stats.rssi);
145 iwe.u.qual.qual = network->stats.signalstrength;
146 iwe.u.qual.level = network->stats.signal;
147 iwe.u.qual.noise = network->stats.noise;
148 iwe.u.qual.updated = network->stats.mask & IEEE80211_STATMASK_WEMASK;
149 if (!(network->stats.mask & IEEE80211_STATMASK_RSSI))
150 iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
151 if (!(network->stats.mask & IEEE80211_STATMASK_NOISE))
152 iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
153 if (!(network->stats.mask & IEEE80211_STATMASK_SIGNAL))
154 iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID;
155 iwe.u.qual.updated = 7;
156 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
158 iwe.cmd = IWEVCUSTOM;
161 iwe.u.data.length = p - custom;
162 if (iwe.u.data.length)
163 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
165 memset(&iwe, 0, sizeof(iwe));
166 if (network->wpa_ie_len) {
167 char buf[MAX_WPA_IE_LEN];
168 memcpy(buf, network->wpa_ie, network->wpa_ie_len);
170 iwe.u.data.length = network->wpa_ie_len;
171 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
174 memset(&iwe, 0, sizeof(iwe));
175 if (network->rsn_ie_len) {
176 char buf[MAX_WPA_IE_LEN];
177 memcpy(buf, network->rsn_ie, network->rsn_ie_len);
179 iwe.u.data.length = network->rsn_ie_len;
180 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
183 /* Add EXTRA: Age to display seconds since last beacon/probe response
186 iwe.cmd = IWEVCUSTOM;
188 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
189 " Last beacon: %lums ago", (jiffies - network->last_scanned) / (HZ / 100));
190 iwe.u.data.length = p - custom;
191 if (iwe.u.data.length)
192 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
197 int ieee80211_wx_get_scan(struct ieee80211_device *ieee,
198 struct iw_request_info *info,
199 union iwreq_data *wrqu, char *extra)
201 struct ieee80211_network *network;
205 char *stop = ev + wrqu->data.length;
208 IEEE80211_DEBUG_WX("Getting scan\n");
210 spin_lock_irqsave(&ieee->lock, flags);
212 if (!ieee->bHwRadioOff) {
213 list_for_each_entry(network, &ieee->network_list, list) {
216 if ((stop-ev) < 200) {
220 if (ieee->scan_age == 0 ||
221 time_after(network->last_scanned + ieee->scan_age, jiffies)) {
222 ev = rtl818x_translate_scan(ieee, ev, stop, network, info);
224 IEEE80211_DEBUG_SCAN(
225 "Not showing network '%s ("
226 "%pM)' due to age (%lums).\n",
227 escape_essid(network->ssid,
230 (jiffies - network->last_scanned) / (HZ / 100));
233 spin_unlock_irqrestore(&ieee->lock, flags);
235 wrqu->data.length = ev - extra;
236 wrqu->data.flags = 0;
237 IEEE80211_DEBUG_WX("exit: %d networks returned.\n", i);
242 int ieee80211_wx_set_encode(struct ieee80211_device *ieee,
243 struct iw_request_info *info,
244 union iwreq_data *wrqu, char *keybuf)
246 struct iw_point *erq = &(wrqu->encoding);
247 struct net_device *dev = ieee->dev;
248 struct ieee80211_security sec = {
251 int i, key, key_provided, len;
252 struct ieee80211_crypt_data **crypt;
254 IEEE80211_DEBUG_WX("SET_ENCODE\n");
256 key = erq->flags & IW_ENCODE_INDEX;
264 key = ieee->tx_keyidx;
267 IEEE80211_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
268 "provided" : "default");
270 crypt = &ieee->crypt[key];
272 if (erq->flags & IW_ENCODE_DISABLED) {
273 if (key_provided && *crypt) {
274 IEEE80211_DEBUG_WX("Disabling encryption on key %d.\n",
276 ieee80211_crypt_delayed_deinit(ieee, crypt);
278 IEEE80211_DEBUG_WX("Disabling encryption.\n");
280 /* Check all the keys to see if any are still configured,
281 * and if no key index was provided, de-init them all.
283 for (i = 0; i < WEP_KEYS; i++) {
284 if (ieee->crypt[i] != NULL) {
287 ieee80211_crypt_delayed_deinit(
288 ieee, &ieee->crypt[i]);
294 sec.level = SEC_LEVEL_0;
295 sec.flags |= SEC_ENABLED | SEC_LEVEL;
302 sec.flags |= SEC_ENABLED;
304 if (*crypt != NULL && (*crypt)->ops != NULL &&
305 strcmp((*crypt)->ops->name, "WEP") != 0) {
306 /* changing to use WEP; deinit previously used algorithm
309 ieee80211_crypt_delayed_deinit(ieee, crypt);
312 if (*crypt == NULL) {
313 struct ieee80211_crypt_data *new_crypt;
315 /* take WEP into use */
316 new_crypt = kzalloc(sizeof(struct ieee80211_crypt_data),
318 if (new_crypt == NULL)
320 new_crypt->ops = ieee80211_get_crypto_ops("WEP");
322 new_crypt->ops = ieee80211_get_crypto_ops("WEP");
325 new_crypt->priv = new_crypt->ops->init(key);
327 if (!new_crypt->ops || !new_crypt->priv) {
331 netdev_warn(ieee->dev,
332 "could not initialize WEP: load module ieee80211_crypt_wep\n");
338 /* If a new key was provided, set it up */
339 if (erq->length > 0) {
340 len = erq->length <= 5 ? 5 : 13;
341 memcpy(sec.keys[key], keybuf, erq->length);
342 if (len > erq->length)
343 memset(sec.keys[key] + erq->length, 0,
345 IEEE80211_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
346 key, escape_essid(sec.keys[key], len),
348 sec.key_sizes[key] = len;
349 (*crypt)->ops->set_key(sec.keys[key], len, NULL,
351 sec.flags |= (1 << key);
352 /* This ensures a key will be activated if no key is
355 if (key == sec.active_key)
356 sec.flags |= SEC_ACTIVE_KEY;
357 ieee->tx_keyidx = key;
359 len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
360 NULL, (*crypt)->priv);
362 /* Set a default key of all 0 */
363 IEEE80211_DEBUG_WX("Setting key %d to all zero.\n",
365 memset(sec.keys[key], 0, 13);
366 (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
368 sec.key_sizes[key] = 13;
369 sec.flags |= (1 << key);
372 /* No key data - just set the default TX key index */
375 "Setting key %d to default Tx key.\n", key);
376 ieee->tx_keyidx = key;
377 sec.active_key = key;
378 sec.flags |= SEC_ACTIVE_KEY;
383 ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
384 sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
385 sec.flags |= SEC_AUTH_MODE;
386 IEEE80211_DEBUG_WX("Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
387 "OPEN" : "SHARED KEY");
389 /* For now we just support WEP, so only set that security level...
390 * TODO: When WPA is added this is one place that needs to change
392 sec.flags |= SEC_LEVEL;
393 sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
395 if (ieee->set_security)
396 ieee->set_security(dev, &sec);
398 /* Do not reset port if card is in Managed mode since resetting will
399 * generate new IEEE 802.11 authentication which may end up in looping
400 * with IEEE 802.1X. If your hardware requires a reset after WEP
401 * configuration (for example... Prism2), implement the reset_port in
402 * the callbacks structures used to initialize the 802.11 stack.
404 if (ieee->reset_on_keychange &&
405 ieee->iw_mode != IW_MODE_INFRA &&
406 ieee->reset_port && ieee->reset_port(dev)) {
407 netdev_dbg(ieee->dev, "reset_port failed\n");
413 int ieee80211_wx_get_encode(struct ieee80211_device *ieee,
414 struct iw_request_info *info,
415 union iwreq_data *wrqu, char *keybuf)
417 struct iw_point *erq = &(wrqu->encoding);
419 struct ieee80211_crypt_data *crypt;
421 IEEE80211_DEBUG_WX("GET_ENCODE\n");
423 if (ieee->iw_mode == IW_MODE_MONITOR)
426 key = erq->flags & IW_ENCODE_INDEX;
432 key = ieee->tx_keyidx;
434 crypt = ieee->crypt[key];
435 erq->flags = key + 1;
437 if (crypt == NULL || crypt->ops == NULL) {
439 erq->flags |= IW_ENCODE_DISABLED;
443 if (strcmp(crypt->ops->name, "WEP") != 0) {
444 /* only WEP is supported with wireless extensions, so just
445 * report that encryption is used.
448 erq->flags |= IW_ENCODE_ENABLED;
452 len = crypt->ops->get_key(keybuf, WEP_KEY_LEN, NULL, crypt->priv);
453 erq->length = (len >= 0 ? len : 0);
455 erq->flags |= IW_ENCODE_ENABLED;
458 erq->flags |= IW_ENCODE_OPEN;
460 erq->flags |= IW_ENCODE_RESTRICTED;
465 int ieee80211_wx_set_encode_ext(struct ieee80211_device *ieee,
466 struct iw_request_info *info,
467 union iwreq_data *wrqu, char *extra)
469 struct net_device *dev = ieee->dev;
470 struct iw_point *encoding = &wrqu->encoding;
471 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
475 struct ieee80211_crypto_ops *ops;
476 struct ieee80211_crypt_data **crypt;
478 struct ieee80211_security sec = {
481 idx = encoding->flags & IW_ENCODE_INDEX;
483 if (idx < 1 || idx > WEP_KEYS)
487 idx = ieee->tx_keyidx;
489 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
490 crypt = &ieee->crypt[idx];
493 /* some Cisco APs use idx>0 for unicast in dynamic WEP */
494 if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
496 if (ieee->iw_mode == IW_MODE_INFRA)
497 crypt = &ieee->crypt[idx];
502 sec.flags |= SEC_ENABLED;
503 if ((encoding->flags & IW_ENCODE_DISABLED) ||
504 ext->alg == IW_ENCODE_ALG_NONE) {
506 ieee80211_crypt_delayed_deinit(ieee, crypt);
508 for (i = 0; i < WEP_KEYS; i++)
509 if (ieee->crypt[i] != NULL)
514 sec.level = SEC_LEVEL_0;
515 sec.flags |= SEC_LEVEL;
523 case IW_ENCODE_ALG_WEP:
526 case IW_ENCODE_ALG_TKIP:
529 case IW_ENCODE_ALG_CCMP:
533 IEEE80211_DEBUG_WX("%s: unknown crypto alg %d\n",
534 dev->name, ext->alg);
539 ops = ieee80211_get_crypto_ops(alg);
541 ops = ieee80211_get_crypto_ops(alg);
543 IEEE80211_DEBUG_WX("%s: unknown crypto alg %d\n",
544 dev->name, ext->alg);
545 netdev_err(ieee->dev, "========>unknown crypto alg %d\n",
551 if (*crypt == NULL || (*crypt)->ops != ops) {
552 struct ieee80211_crypt_data *new_crypt;
554 ieee80211_crypt_delayed_deinit(ieee, crypt);
556 new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
557 if (new_crypt == NULL) {
561 new_crypt->ops = ops;
563 new_crypt->priv = new_crypt->ops->init(idx);
564 if (new_crypt->priv == NULL) {
573 if (ext->key_len > 0 && (*crypt)->ops->set_key &&
574 (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
575 (*crypt)->priv) < 0) {
576 IEEE80211_DEBUG_WX("%s: key setting failed\n", dev->name);
577 netdev_err(ieee->dev, "key setting failed\n");
582 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
583 ieee->tx_keyidx = idx;
584 sec.active_key = idx;
585 sec.flags |= SEC_ACTIVE_KEY;
588 if (ext->alg != IW_ENCODE_ALG_NONE) {
589 memcpy(sec.keys[idx], ext->key, ext->key_len);
590 sec.key_sizes[idx] = ext->key_len;
591 sec.flags |= (1 << idx);
592 if (ext->alg == IW_ENCODE_ALG_WEP) {
593 sec.flags |= SEC_LEVEL;
594 sec.level = SEC_LEVEL_1;
595 } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
596 sec.flags |= SEC_LEVEL;
597 sec.level = SEC_LEVEL_2;
598 } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
599 sec.flags |= SEC_LEVEL;
600 sec.level = SEC_LEVEL_3;
602 /* Don't set sec level for group keys. */
604 sec.flags &= ~SEC_LEVEL;
608 if (ieee->set_security)
609 ieee->set_security(ieee->dev, &sec);
611 if (ieee->reset_on_keychange &&
612 ieee->iw_mode != IW_MODE_INFRA &&
613 ieee->reset_port && ieee->reset_port(dev)) {
614 IEEE80211_DEBUG_WX("%s: reset_port failed\n", dev->name);
621 int ieee80211_wx_set_mlme(struct ieee80211_device *ieee,
622 struct iw_request_info *info,
623 union iwreq_data *wrqu, char *extra)
625 struct iw_mlme *mlme = (struct iw_mlme *) extra;
629 case IW_MLME_DISASSOC:
630 ieee80211_disassociate(ieee);
639 int ieee80211_wx_set_auth(struct ieee80211_device *ieee,
640 struct iw_request_info *info,
641 struct iw_param *data, char *extra)
643 switch (data->flags & IW_AUTH_INDEX) {
644 case IW_AUTH_WPA_VERSION:
645 /* need to support wpa2 here */
647 case IW_AUTH_CIPHER_PAIRWISE:
648 case IW_AUTH_CIPHER_GROUP:
649 case IW_AUTH_KEY_MGMT:
650 /* Host AP driver does not use these parameters and allows
651 * wpa_supplicant to control them internally.
654 case IW_AUTH_TKIP_COUNTERMEASURES:
655 ieee->tkip_countermeasures = data->value;
657 case IW_AUTH_DROP_UNENCRYPTED:
658 ieee->drop_unencrypted = data->value;
661 case IW_AUTH_80211_AUTH_ALG:
662 ieee->open_wep = (data->value&IW_AUTH_ALG_OPEN_SYSTEM) ? 1 : 0;
666 case IW_AUTH_WPA_ENABLED:
667 ieee->wpa_enabled = (data->value) ? 1 : 0;
671 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
672 ieee->ieee802_1x = data->value;
674 case IW_AUTH_PRIVACY_INVOKED:
675 ieee->privacy_invoked = data->value;
684 int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len)
688 if (len > MAX_WPA_IE_LEN || (len && ie == NULL)) {
689 netdev_err(ieee->dev, "return error out, len:%zu\n", len);
694 if (len != ie[1]+2) {
695 netdev_err(ieee->dev, "len:%zu, ie:%d\n", len, ie[1]);
698 buf = kmemdup(ie, len, GFP_KERNEL);
703 ieee->wpa_ie_len = len;
707 ieee->wpa_ie_len = 0;