1 /******************************************************************************
3 Copyright(c) 2004 Intel Corporation. All rights reserved.
5 Portions of this file are based on the WEP enablement code provided by the
6 Host AP project hostap-drivers v0.1.3
7 Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
9 Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
11 This program is free software; you can redistribute it and/or modify it
12 under the terms of version 2 of the GNU General Public License as
13 published by the Free Software Foundation.
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59
22 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 The full GNU General Public License is included in this distribution in the
28 James P. Ketrenos <ipw2100-admin@linux.intel.com>
29 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 ******************************************************************************/
32 #include <linux/wireless.h>
33 #include <linux/version.h>
34 #include <linux/kmod.h>
35 #include <linux/module.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 memcpy(iwe.u.ap_addr.sa_data, network->bssid, ETH_ALEN);
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 //YJ,modified,080903,for hidden ap
66 //if (network->flags & NETWORK_EMPTY_ESSID) {
67 if (network->ssid_len == 0) {
68 //YJ,modified,080903,end
69 iwe.u.data.length = sizeof("<hidden>");
70 start = iwe_stream_add_point(info, start, stop, &iwe, "<hidden>");
72 iwe.u.data.length = min(network->ssid_len, (u8)32);
73 start = iwe_stream_add_point(info, start, stop, &iwe, network->ssid);
75 //printk("ESSID: %s\n",network->ssid);
76 /* Add the protocol name */
77 iwe.cmd = SIOCGIWNAME;
78 snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11%s", ieee80211_modes[network->mode]);
79 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_CHAR_LEN);
82 iwe.cmd = SIOCGIWMODE;
83 if (network->capability &
84 (WLAN_CAPABILITY_BSS | WLAN_CAPABILITY_IBSS)) {
85 if (network->capability & WLAN_CAPABILITY_BSS)
86 iwe.u.mode = IW_MODE_MASTER;
88 iwe.u.mode = IW_MODE_ADHOC;
90 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
93 /* Add frequency/channel */
94 iwe.cmd = SIOCGIWFREQ;
95 /* iwe.u.freq.m = ieee80211_frequency(network->channel, network->mode);
97 iwe.u.freq.m = network->channel;
100 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
102 /* Add encryption capability */
103 iwe.cmd = SIOCGIWENCODE;
104 if (network->capability & WLAN_CAPABILITY_PRIVACY)
105 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
107 iwe.u.data.flags = IW_ENCODE_DISABLED;
108 iwe.u.data.length = 0;
109 start = iwe_stream_add_point(info, start, stop, &iwe, network->ssid);
111 /* Add basic and extended rates */
114 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): ");
115 for (i = 0, j = 0; i < network->rates_len; ) {
116 if (j < network->rates_ex_len &&
117 ((network->rates_ex[j] & 0x7F) <
118 (network->rates[i] & 0x7F)))
119 rate = network->rates_ex[j++] & 0x7F;
121 rate = network->rates[i++] & 0x7F;
124 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
125 "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
127 for (; j < network->rates_ex_len; j++) {
128 rate = network->rates_ex[j] & 0x7F;
129 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
130 "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
135 iwe.cmd = SIOCGIWRATE;
136 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
137 iwe.u.bitrate.value = max_rate * 500000;
138 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_PARAM_LEN);
140 iwe.cmd = IWEVCUSTOM;
141 iwe.u.data.length = p - custom;
142 if (iwe.u.data.length)
143 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
145 /* Add quality statistics */
146 /* TODO: Fix these values... */
147 if (network->stats.signal == 0 || network->stats.rssi == 0)
148 printk("========>signal:%d, rssi:%d\n", network->stats.signal, network->stats.rssi);
150 // printk("SIGNAL: %d,RSSI: %d,NOISE: %d\n",network->stats.signal,network->stats.rssi,network->stats.noise);
151 iwe.u.qual.qual = network->stats.signalstrength;
152 iwe.u.qual.level = network->stats.signal;
153 iwe.u.qual.noise = network->stats.noise;
154 iwe.u.qual.updated = network->stats.mask & IEEE80211_STATMASK_WEMASK;
155 if (!(network->stats.mask & IEEE80211_STATMASK_RSSI))
156 iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
157 if (!(network->stats.mask & IEEE80211_STATMASK_NOISE))
158 iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
159 if (!(network->stats.mask & IEEE80211_STATMASK_SIGNAL))
160 iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID;
161 iwe.u.qual.updated = 7;
162 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
164 iwe.cmd = IWEVCUSTOM;
167 iwe.u.data.length = p - custom;
168 if (iwe.u.data.length)
169 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
171 memset(&iwe, 0, sizeof(iwe));
172 if (network->wpa_ie_len) {
173 // printk("wpa_ie_len:%d\n", network->wpa_ie_len);
174 char buf[MAX_WPA_IE_LEN];
175 memcpy(buf, network->wpa_ie, network->wpa_ie_len);
177 iwe.u.data.length = network->wpa_ie_len;
178 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
181 memset(&iwe, 0, sizeof(iwe));
182 if (network->rsn_ie_len) {
183 // printk("=====>rsn_ie_len:\n", network->rsn_ie_len);
184 char buf[MAX_WPA_IE_LEN];
185 memcpy(buf, network->rsn_ie, network->rsn_ie_len);
187 iwe.u.data.length = network->rsn_ie_len;
188 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
191 /* Add EXTRA: Age to display seconds since last beacon/probe response
192 * for given network. */
193 iwe.cmd = IWEVCUSTOM;
195 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
196 " Last beacon: %lums ago", (jiffies - network->last_scanned) / (HZ / 100));
197 iwe.u.data.length = p - custom;
198 if (iwe.u.data.length)
199 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
204 int ieee80211_wx_get_scan(struct ieee80211_device *ieee,
205 struct iw_request_info *info,
206 union iwreq_data *wrqu, char *extra)
208 struct ieee80211_network *network;
212 char *stop = ev + wrqu->data.length;//IW_SCAN_MAX_DATA;
213 //char *stop = ev + IW_SCAN_MAX_DATA;
216 IEEE80211_DEBUG_WX("Getting scan\n");
218 spin_lock_irqsave(&ieee->lock, flags);
220 if(!ieee->bHwRadioOff)
222 list_for_each_entry(network, &ieee->network_list, list) {
230 if (ieee->scan_age == 0 ||
231 time_after(network->last_scanned + ieee->scan_age, jiffies))
233 ev = rtl818x_translate_scan(ieee, ev, stop, network, info);
236 IEEE80211_DEBUG_SCAN(
237 "Not showing network '%s ("
238 MAC_FMT ")' due to age (%lums).\n",
239 escape_essid(network->ssid,
241 MAC_ARG(network->bssid),
242 (jiffies - network->last_scanned) / (HZ / 100));
245 spin_unlock_irqrestore(&ieee->lock, flags);
247 wrqu->data.length = ev - extra;
248 wrqu->data.flags = 0;
249 IEEE80211_DEBUG_WX("exit: %d networks returned.\n", i);
254 int ieee80211_wx_set_encode(struct ieee80211_device *ieee,
255 struct iw_request_info *info,
256 union iwreq_data *wrqu, char *keybuf)
258 struct iw_point *erq = &(wrqu->encoding);
259 struct net_device *dev = ieee->dev;
260 struct ieee80211_security sec = {
263 int i, key, key_provided, len;
264 struct ieee80211_crypt_data **crypt;
266 IEEE80211_DEBUG_WX("SET_ENCODE\n");
268 key = erq->flags & IW_ENCODE_INDEX;
276 key = ieee->tx_keyidx;
279 IEEE80211_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
280 "provided" : "default");
282 crypt = &ieee->crypt[key];
284 if (erq->flags & IW_ENCODE_DISABLED) {
285 if (key_provided && *crypt) {
286 IEEE80211_DEBUG_WX("Disabling encryption on key %d.\n",
288 ieee80211_crypt_delayed_deinit(ieee, crypt);
290 IEEE80211_DEBUG_WX("Disabling encryption.\n");
292 /* Check all the keys to see if any are still configured,
293 * and if no key index was provided, de-init them all */
294 for (i = 0; i < WEP_KEYS; i++) {
295 if (ieee->crypt[i] != NULL) {
298 ieee80211_crypt_delayed_deinit(
299 ieee, &ieee->crypt[i]);
305 sec.level = SEC_LEVEL_0;
306 sec.flags |= SEC_ENABLED | SEC_LEVEL;
315 sec.flags |= SEC_ENABLED;
317 if (*crypt != NULL && (*crypt)->ops != NULL &&
318 strcmp((*crypt)->ops->name, "WEP") != 0) {
319 /* changing to use WEP; deinit previously used algorithm
321 ieee80211_crypt_delayed_deinit(ieee, crypt);
324 if (*crypt == NULL) {
325 struct ieee80211_crypt_data *new_crypt;
327 /* take WEP into use */
328 new_crypt = kmalloc(sizeof(struct ieee80211_crypt_data),
330 if (new_crypt == NULL)
332 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
333 new_crypt->ops = ieee80211_get_crypto_ops("WEP");
335 new_crypt->ops = ieee80211_get_crypto_ops("WEP");
338 new_crypt->priv = new_crypt->ops->init(key);
340 if (!new_crypt->ops || !new_crypt->priv) {
344 printk(KERN_WARNING "%s: could not initialize WEP: "
345 "load module ieee80211_crypt_wep\n",
352 /* If a new key was provided, set it up */
353 if (erq->length > 0) {
354 len = erq->length <= 5 ? 5 : 13;
355 memcpy(sec.keys[key], keybuf, erq->length);
356 if (len > erq->length)
357 memset(sec.keys[key] + erq->length, 0,
359 IEEE80211_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
360 key, escape_essid(sec.keys[key], len),
362 sec.key_sizes[key] = len;
363 (*crypt)->ops->set_key(sec.keys[key], len, NULL,
365 sec.flags |= (1 << key);
366 /* This ensures a key will be activated if no key is
368 if (key == sec.active_key)
369 sec.flags |= SEC_ACTIVE_KEY;
370 ieee->tx_keyidx = key;//by wb 080312
372 len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
373 NULL, (*crypt)->priv);
375 /* Set a default key of all 0 */
376 IEEE80211_DEBUG_WX("Setting key %d to all zero.\n",
378 memset(sec.keys[key], 0, 13);
379 (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
381 sec.key_sizes[key] = 13;
382 sec.flags |= (1 << key);
385 /* No key data - just set the default TX key index */
388 "Setting key %d to default Tx key.\n", key);
389 ieee->tx_keyidx = key;
390 sec.active_key = key;
391 sec.flags |= SEC_ACTIVE_KEY;
396 ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
397 sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
398 sec.flags |= SEC_AUTH_MODE;
399 IEEE80211_DEBUG_WX("Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
400 "OPEN" : "SHARED KEY");
402 /* For now we just support WEP, so only set that security level...
403 * TODO: When WPA is added this is one place that needs to change */
404 sec.flags |= SEC_LEVEL;
405 sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
407 if (ieee->set_security)
408 ieee->set_security(dev, &sec);
410 /* Do not reset port if card is in Managed mode since resetting will
411 * generate new IEEE 802.11 authentication which may end up in looping
412 * with IEEE 802.1X. If your hardware requires a reset after WEP
413 * configuration (for example... Prism2), implement the reset_port in
414 * the callbacks structures used to initialize the 802.11 stack. */
415 if (ieee->reset_on_keychange &&
416 ieee->iw_mode != IW_MODE_INFRA &&
417 ieee->reset_port && ieee->reset_port(dev)) {
418 printk(KERN_DEBUG "%s: reset_port failed\n", dev->name);
424 int ieee80211_wx_get_encode(struct ieee80211_device *ieee,
425 struct iw_request_info *info,
426 union iwreq_data *wrqu, char *keybuf)
428 struct iw_point *erq = &(wrqu->encoding);
430 struct ieee80211_crypt_data *crypt;
432 IEEE80211_DEBUG_WX("GET_ENCODE\n");
434 if(ieee->iw_mode == IW_MODE_MONITOR)
437 key = erq->flags & IW_ENCODE_INDEX;
443 key = ieee->tx_keyidx;
445 crypt = ieee->crypt[key];
446 erq->flags = key + 1;
448 if (crypt == NULL || crypt->ops == NULL) {
450 erq->flags |= IW_ENCODE_DISABLED;
454 if (strcmp(crypt->ops->name, "WEP") != 0) {
455 /* only WEP is supported with wireless extensions, so just
456 * report that encryption is used */
458 erq->flags |= IW_ENCODE_ENABLED;
462 len = crypt->ops->get_key(keybuf, WEP_KEY_LEN, NULL, crypt->priv);
463 erq->length = (len >= 0 ? len : 0);
465 erq->flags |= IW_ENCODE_ENABLED;
468 erq->flags |= IW_ENCODE_OPEN;
470 erq->flags |= IW_ENCODE_RESTRICTED;
475 int ieee80211_wx_set_encode_ext(struct ieee80211_device *ieee,
476 struct iw_request_info *info,
477 union iwreq_data *wrqu, char *extra)
479 struct net_device *dev = ieee->dev;
480 struct iw_point *encoding = &wrqu->encoding;
481 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
485 struct ieee80211_crypto_ops *ops;
486 struct ieee80211_crypt_data **crypt;
488 struct ieee80211_security sec = {
491 //printk("======>encoding flag:%x,ext flag:%x, ext alg:%d\n", encoding->flags,ext->ext_flags, ext->alg);
492 idx = encoding->flags & IW_ENCODE_INDEX;
494 if (idx < 1 || idx > WEP_KEYS)
498 idx = ieee->tx_keyidx;
500 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
501 crypt = &ieee->crypt[idx];
504 /* some Cisco APs use idx>0 for unicast in dynamic WEP */
505 //printk("not group key, flags:%x, ext->alg:%d\n", ext->ext_flags, ext->alg);
506 if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
508 if (ieee->iw_mode == IW_MODE_INFRA)
509 crypt = &ieee->crypt[idx];
514 sec.flags |= SEC_ENABLED;// | SEC_ENCRYPT;
515 if ((encoding->flags & IW_ENCODE_DISABLED) ||
516 ext->alg == IW_ENCODE_ALG_NONE) {
518 ieee80211_crypt_delayed_deinit(ieee, crypt);
520 for (i = 0; i < WEP_KEYS; i++)
521 if (ieee->crypt[i] != NULL)
527 sec.level = SEC_LEVEL_0;
528 sec.flags |= SEC_LEVEL;
530 //printk("disabled: flag:%x\n", encoding->flags);
538 case IW_ENCODE_ALG_WEP:
541 case IW_ENCODE_ALG_TKIP:
544 case IW_ENCODE_ALG_CCMP:
548 IEEE80211_DEBUG_WX("%s: unknown crypto alg %d\n",
549 dev->name, ext->alg);
553 // printk("8-09-08-9=====>%s, alg name:%s\n",__func__, alg);
555 ops = ieee80211_get_crypto_ops(alg);
557 ops = ieee80211_get_crypto_ops(alg);
559 IEEE80211_DEBUG_WX("%s: unknown crypto alg %d\n",
560 dev->name, ext->alg);
561 printk("========>unknown crypto alg %d\n", ext->alg);
566 if (*crypt == NULL || (*crypt)->ops != ops) {
567 struct ieee80211_crypt_data *new_crypt;
569 ieee80211_crypt_delayed_deinit(ieee, crypt);
571 new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
572 if (new_crypt == NULL) {
576 new_crypt->ops = ops;
578 new_crypt->priv = new_crypt->ops->init(idx);
579 if (new_crypt->priv == NULL) {
588 if (ext->key_len > 0 && (*crypt)->ops->set_key &&
589 (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
590 (*crypt)->priv) < 0) {
591 IEEE80211_DEBUG_WX("%s: key setting failed\n", dev->name);
592 printk("key setting failed\n");
598 //printk("skip_host_crypt:ext_flags:%x\n", ext->ext_flags);
599 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
600 ieee->tx_keyidx = idx;
601 sec.active_key = idx;
602 sec.flags |= SEC_ACTIVE_KEY;
605 if (ext->alg != IW_ENCODE_ALG_NONE) {
606 memcpy(sec.keys[idx], ext->key, ext->key_len);
607 sec.key_sizes[idx] = ext->key_len;
608 sec.flags |= (1 << idx);
609 if (ext->alg == IW_ENCODE_ALG_WEP) {
610 // sec.encode_alg[idx] = SEC_ALG_WEP;
611 sec.flags |= SEC_LEVEL;
612 sec.level = SEC_LEVEL_1;
613 } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
614 // sec.encode_alg[idx] = SEC_ALG_TKIP;
615 sec.flags |= SEC_LEVEL;
616 sec.level = SEC_LEVEL_2;
617 } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
618 // sec.encode_alg[idx] = SEC_ALG_CCMP;
619 sec.flags |= SEC_LEVEL;
620 sec.level = SEC_LEVEL_3;
622 /* Don't set sec level for group keys. */
624 sec.flags &= ~SEC_LEVEL;
628 if (ieee->set_security)
629 ieee->set_security(ieee->dev, &sec);
631 if (ieee->reset_on_keychange &&
632 ieee->iw_mode != IW_MODE_INFRA &&
633 ieee->reset_port && ieee->reset_port(dev)) {
634 IEEE80211_DEBUG_WX("%s: reset_port failed\n", dev->name);
640 int ieee80211_wx_set_mlme(struct ieee80211_device *ieee,
641 struct iw_request_info *info,
642 union iwreq_data *wrqu, char *extra)
644 struct iw_mlme *mlme = (struct iw_mlme *) extra;
645 // printk("\ndkgadfslkdjgalskdf===============>%s(), cmd:%x\n", __func__, mlme->cmd);
649 case IW_MLME_DISASSOC:
650 // printk("disassoc now\n");
651 ieee80211_disassociate(ieee);
660 int ieee80211_wx_set_auth(struct ieee80211_device *ieee,
661 struct iw_request_info *info,
662 struct iw_param *data, char *extra)
665 struct ieee80211_security sec = {
666 .flags = SEC_AUTH_MODE,
669 //printk("set auth:flag:%x, data value:%x\n", data->flags, data->value);
670 switch (data->flags & IW_AUTH_INDEX) {
671 case IW_AUTH_WPA_VERSION:
672 /*need to support wpa2 here*/
673 //printk("wpa version:%x\n", data->value);
675 case IW_AUTH_CIPHER_PAIRWISE:
676 case IW_AUTH_CIPHER_GROUP:
677 case IW_AUTH_KEY_MGMT:
679 * * Host AP driver does not use these parameters and allows
680 * * wpa_supplicant to control them internally.
683 case IW_AUTH_TKIP_COUNTERMEASURES:
684 ieee->tkip_countermeasures = data->value;
686 case IW_AUTH_DROP_UNENCRYPTED:
687 ieee->drop_unencrypted = data->value;
690 case IW_AUTH_80211_AUTH_ALG:
691 ieee->open_wep = (data->value&IW_AUTH_ALG_OPEN_SYSTEM)?1:0;
692 //printk("open_wep:%d\n", ieee->open_wep);
696 case IW_AUTH_WPA_ENABLED:
697 ieee->wpa_enabled = (data->value)?1:0;
698 //printk("enalbe wpa:%d\n", ieee->wpa_enabled);
702 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
703 ieee->ieee802_1x = data->value;
705 case IW_AUTH_PRIVACY_INVOKED:
706 ieee->privacy_invoked = data->value;
715 int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len)
719 if (len>MAX_WPA_IE_LEN || (len && ie == NULL))
721 printk("return error out, len:%zu\n", len);
728 printk("len:%zu, ie:%d\n", len, ie[1]);
731 buf = kmalloc(len, GFP_KERNEL);
734 memcpy(buf, ie, len);
737 ieee->wpa_ie_len = len;
743 ieee->wpa_ie_len = 0;
745 // printk("<=====out %s()\n", __func__);