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"
43 struct modes_unit ieee80211_modes[] = {
52 #define iwe_stream_add_event_rsl iwe_stream_add_event
54 #define MAX_CUSTOM_LEN 64
55 static inline char *rtl819x_translate_scan(struct ieee80211_device *ieee,
56 char *start, char *stop,
57 struct ieee80211_network *network,
58 struct iw_request_info *info)
60 char custom[MAX_CUSTOM_LEN];
61 char proto_name[IFNAMSIZ];
62 char *pname = proto_name;
67 static u8 EWC11NHTCap[] = {0x00, 0x90, 0x4c, 0x33};
69 /* First entry *MUST* be the AP MAC address */
71 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
72 memcpy(iwe.u.ap_addr.sa_data, network->bssid, ETH_ALEN);
73 start = iwe_stream_add_event_rsl(info, start, stop, &iwe, IW_EV_ADDR_LEN);
75 /* Remaining entries will be displayed in the order we provide them */
78 iwe.cmd = SIOCGIWESSID;
80 // if (network->flags & NETWORK_EMPTY_ESSID) {
81 if (network->ssid_len == 0) {
82 iwe.u.data.length = sizeof("<hidden>");
83 start = iwe_stream_add_point(info, start, stop, &iwe, "<hidden>");
85 iwe.u.data.length = min(network->ssid_len, (u8)32);
86 start = iwe_stream_add_point(info, start, stop, &iwe, network->ssid);
88 /* Add the protocol name */
89 iwe.cmd = SIOCGIWNAME;
90 for(i=0; i<ARRAY_SIZE(ieee80211_modes); i++) {
91 if(network->mode&(1<<i)) {
92 sprintf(pname,ieee80211_modes[i].mode_string,ieee80211_modes[i].mode_size);
93 pname +=ieee80211_modes[i].mode_size;
97 snprintf(iwe.u.name, IFNAMSIZ, "IEEE802.11%s", proto_name);
98 start = iwe_stream_add_event_rsl(info, start, stop, &iwe, IW_EV_CHAR_LEN);
100 iwe.cmd = SIOCGIWMODE;
101 if (network->capability &
102 (WLAN_CAPABILITY_BSS | WLAN_CAPABILITY_IBSS)) {
103 if (network->capability & WLAN_CAPABILITY_BSS)
104 iwe.u.mode = IW_MODE_MASTER;
106 iwe.u.mode = IW_MODE_ADHOC;
107 start = iwe_stream_add_event_rsl(info, start, stop, &iwe, IW_EV_UINT_LEN);
110 /* Add frequency/channel */
111 iwe.cmd = SIOCGIWFREQ;
112 /* iwe.u.freq.m = ieee80211_frequency(network->channel, network->mode);
114 iwe.u.freq.m = network->channel;
117 start = iwe_stream_add_event_rsl(info, start, stop, &iwe, IW_EV_FREQ_LEN);
118 /* Add encryption capability */
119 iwe.cmd = SIOCGIWENCODE;
120 if (network->capability & WLAN_CAPABILITY_PRIVACY)
121 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
123 iwe.u.data.flags = IW_ENCODE_DISABLED;
124 iwe.u.data.length = 0;
125 start = iwe_stream_add_point(info, start, stop, &iwe, network->ssid);
126 /* Add basic and extended rates */
129 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): ");
130 for (i = 0, j = 0; i < network->rates_len; ) {
131 if (j < network->rates_ex_len &&
132 ((network->rates_ex[j] & 0x7F) <
133 (network->rates[i] & 0x7F)))
134 rate = network->rates_ex[j++] & 0x7F;
136 rate = network->rates[i++] & 0x7F;
139 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
140 "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
142 for (; j < network->rates_ex_len; j++) {
143 rate = network->rates_ex[j] & 0x7F;
144 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
145 "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
150 if (network->mode >= IEEE_N_24G)//add N rate here;
152 PHT_CAPABILITY_ELE ht_cap = NULL;
153 bool is40M = false, isShortGI = false;
155 if (!memcmp(network->bssht.bdHTCapBuf, EWC11NHTCap, 4))
156 ht_cap = (PHT_CAPABILITY_ELE)&network->bssht.bdHTCapBuf[4];
158 ht_cap = (PHT_CAPABILITY_ELE)&network->bssht.bdHTCapBuf[0];
159 is40M = (ht_cap->ChlWidth)?1:0;
160 isShortGI = (ht_cap->ChlWidth)?
161 ((ht_cap->ShortGI40Mhz)?1:0):
162 ((ht_cap->ShortGI20Mhz)?1:0);
164 max_mcs = HTGetHighestMCSRate(ieee, ht_cap->MCS, MCS_FILTER_ALL);
165 rate = MCS_DATA_RATE[is40M][isShortGI][max_mcs&0x7f];
170 iwe.cmd = SIOCGIWRATE;
171 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
172 iwe.u.bitrate.value = max_rate * 500000;
173 start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
176 iwe.cmd = IWEVCUSTOM;
177 iwe.u.data.length = p - custom;
178 if (iwe.u.data.length)
179 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
181 /* Add quality statistics */
182 /* TODO: Fix these values... */
184 iwe.u.qual.qual = network->stats.signal;
185 iwe.u.qual.level = network->stats.rssi;
186 iwe.u.qual.noise = network->stats.noise;
187 iwe.u.qual.updated = network->stats.mask & IEEE80211_STATMASK_WEMASK;
188 if (!(network->stats.mask & IEEE80211_STATMASK_RSSI))
189 iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
190 if (!(network->stats.mask & IEEE80211_STATMASK_NOISE))
191 iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
192 if (!(network->stats.mask & IEEE80211_STATMASK_SIGNAL))
193 iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID;
194 iwe.u.qual.updated = 7;
195 start = iwe_stream_add_event_rsl(info, start, stop, &iwe, IW_EV_QUAL_LEN);
196 iwe.cmd = IWEVCUSTOM;
199 iwe.u.data.length = p - custom;
200 if (iwe.u.data.length)
201 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
203 memset(&iwe, 0, sizeof(iwe));
204 if (network->wpa_ie_len)
206 char buf[MAX_WPA_IE_LEN];
207 memcpy(buf, network->wpa_ie, network->wpa_ie_len);
209 iwe.u.data.length = network->wpa_ie_len;
210 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
212 memset(&iwe, 0, sizeof(iwe));
213 if (network->rsn_ie_len)
215 char buf[MAX_WPA_IE_LEN];
216 memcpy(buf, network->rsn_ie, network->rsn_ie_len);
218 iwe.u.data.length = network->rsn_ie_len;
219 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
222 /* Add EXTRA: Age to display seconds since last beacon/probe response
223 * for given network. */
224 iwe.cmd = IWEVCUSTOM;
226 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
227 " Last beacon: %lums ago", (jiffies - network->last_scanned) / (HZ / 100));
228 iwe.u.data.length = p - custom;
229 if (iwe.u.data.length)
230 start = iwe_stream_add_point(info, start, stop, &iwe, custom);
235 int ieee80211_wx_get_scan(struct ieee80211_device *ieee,
236 struct iw_request_info *info,
237 union iwreq_data *wrqu, char *extra)
239 struct ieee80211_network *network;
243 // char *stop = ev + IW_SCAN_MAX_DATA;
244 char *stop = ev + wrqu->data.length;//IW_SCAN_MAX_DATA;
245 //char *stop = ev + IW_SCAN_MAX_DATA;
248 IEEE80211_DEBUG_WX("Getting scan\n");
250 spin_lock_irqsave(&ieee->lock, flags);
252 list_for_each_entry(network, &ieee->network_list, list) {
259 if (ieee->scan_age == 0 ||
260 time_after(network->last_scanned + ieee->scan_age, jiffies))
261 ev = rtl819x_translate_scan(ieee, ev, stop, network, info);
263 IEEE80211_DEBUG_SCAN(
264 "Not showing network '%s ("
265 MAC_FMT ")' due to age (%lums).\n",
266 escape_essid(network->ssid,
268 MAC_ARG(network->bssid),
269 (jiffies - network->last_scanned) / (HZ / 100));
272 spin_unlock_irqrestore(&ieee->lock, flags);
274 wrqu->data.length = ev - extra;
275 wrqu->data.flags = 0;
277 IEEE80211_DEBUG_WX("exit: %d networks returned.\n", i);
282 int ieee80211_wx_set_encode(struct ieee80211_device *ieee,
283 struct iw_request_info *info,
284 union iwreq_data *wrqu, char *keybuf)
286 struct iw_point *erq = &(wrqu->encoding);
287 struct net_device *dev = ieee->dev;
288 struct ieee80211_security sec = {
291 int i, key, key_provided, len;
292 struct ieee80211_crypt_data **crypt;
294 IEEE80211_DEBUG_WX("SET_ENCODE\n");
296 key = erq->flags & IW_ENCODE_INDEX;
304 key = ieee->tx_keyidx;
307 IEEE80211_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
308 "provided" : "default");
309 crypt = &ieee->crypt[key];
311 if (erq->flags & IW_ENCODE_DISABLED) {
312 if (key_provided && *crypt) {
313 IEEE80211_DEBUG_WX("Disabling encryption on key %d.\n",
315 ieee80211_crypt_delayed_deinit(ieee, crypt);
317 IEEE80211_DEBUG_WX("Disabling encryption.\n");
319 /* Check all the keys to see if any are still configured,
320 * and if no key index was provided, de-init them all */
321 for (i = 0; i < WEP_KEYS; i++) {
322 if (ieee->crypt[i] != NULL) {
325 ieee80211_crypt_delayed_deinit(
326 ieee, &ieee->crypt[i]);
332 sec.level = SEC_LEVEL_0;
333 sec.flags |= SEC_ENABLED | SEC_LEVEL;
342 sec.flags |= SEC_ENABLED;
344 if (*crypt != NULL && (*crypt)->ops != NULL &&
345 strcmp((*crypt)->ops->name, "WEP") != 0) {
346 /* changing to use WEP; deinit previously used algorithm
348 ieee80211_crypt_delayed_deinit(ieee, crypt);
351 if (*crypt == NULL) {
352 struct ieee80211_crypt_data *new_crypt;
354 /* take WEP into use */
355 new_crypt = kmalloc(sizeof(struct ieee80211_crypt_data),
357 if (new_crypt == NULL)
359 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
360 new_crypt->ops = ieee80211_get_crypto_ops("WEP");
362 new_crypt->ops = ieee80211_get_crypto_ops("WEP");
364 new_crypt->priv = new_crypt->ops->init(key);
366 if (!new_crypt->ops || !new_crypt->priv) {
370 printk(KERN_WARNING "%s: could not initialize WEP: "
371 "load module ieee80211_crypt_wep\n",
378 /* If a new key was provided, set it up */
379 if (erq->length > 0) {
380 len = erq->length <= 5 ? 5 : 13;
381 memcpy(sec.keys[key], keybuf, erq->length);
382 if (len > erq->length)
383 memset(sec.keys[key] + erq->length, 0,
385 IEEE80211_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
386 key, escape_essid(sec.keys[key], len),
388 sec.key_sizes[key] = len;
389 (*crypt)->ops->set_key(sec.keys[key], len, NULL,
391 sec.flags |= (1 << key);
392 /* This ensures a key will be activated if no key is
394 if (key == sec.active_key)
395 sec.flags |= SEC_ACTIVE_KEY;
396 ieee->tx_keyidx = key;
399 len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
400 NULL, (*crypt)->priv);
402 /* Set a default key of all 0 */
403 printk("Setting key %d to all zero.\n",
406 IEEE80211_DEBUG_WX("Setting key %d to all zero.\n",
408 memset(sec.keys[key], 0, 13);
409 (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
411 sec.key_sizes[key] = 13;
412 sec.flags |= (1 << key);
415 /* No key data - just set the default TX key index */
418 "Setting key %d to default Tx key.\n", key);
419 ieee->tx_keyidx = key;
420 sec.active_key = key;
421 sec.flags |= SEC_ACTIVE_KEY;
426 ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
427 ieee->auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
428 sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
429 sec.flags |= SEC_AUTH_MODE;
430 IEEE80211_DEBUG_WX("Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
431 "OPEN" : "SHARED KEY");
433 /* For now we just support WEP, so only set that security level...
434 * TODO: When WPA is added this is one place that needs to change */
435 sec.flags |= SEC_LEVEL;
436 sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
438 if (ieee->set_security)
439 ieee->set_security(dev, &sec);
441 /* Do not reset port if card is in Managed mode since resetting will
442 * generate new IEEE 802.11 authentication which may end up in looping
443 * with IEEE 802.1X. If your hardware requires a reset after WEP
444 * configuration (for example... Prism2), implement the reset_port in
445 * the callbacks structures used to initialize the 802.11 stack. */
446 if (ieee->reset_on_keychange &&
447 ieee->iw_mode != IW_MODE_INFRA &&
448 ieee->reset_port && ieee->reset_port(dev)) {
449 printk(KERN_DEBUG "%s: reset_port failed\n", dev->name);
455 int ieee80211_wx_get_encode(struct ieee80211_device *ieee,
456 struct iw_request_info *info,
457 union iwreq_data *wrqu, char *keybuf)
459 struct iw_point *erq = &(wrqu->encoding);
461 struct ieee80211_crypt_data *crypt;
463 IEEE80211_DEBUG_WX("GET_ENCODE\n");
465 if(ieee->iw_mode == IW_MODE_MONITOR)
468 key = erq->flags & IW_ENCODE_INDEX;
474 key = ieee->tx_keyidx;
476 crypt = ieee->crypt[key];
477 erq->flags = key + 1;
479 if (crypt == NULL || crypt->ops == NULL) {
481 erq->flags |= IW_ENCODE_DISABLED;
485 len = crypt->ops->get_key(keybuf, SCM_KEY_LEN, NULL, crypt->priv);
486 erq->length = (len >= 0 ? len : 0);
488 erq->flags |= IW_ENCODE_ENABLED;
491 erq->flags |= IW_ENCODE_OPEN;
493 erq->flags |= IW_ENCODE_RESTRICTED;
498 int ieee80211_wx_set_encode_ext(struct ieee80211_device *ieee,
499 struct iw_request_info *info,
500 union iwreq_data *wrqu, char *extra)
503 struct net_device *dev = ieee->dev;
504 struct iw_point *encoding = &wrqu->encoding;
505 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
509 struct ieee80211_crypto_ops *ops;
510 struct ieee80211_crypt_data **crypt;
512 struct ieee80211_security sec = {
515 //printk("======>encoding flag:%x,ext flag:%x, ext alg:%d\n", encoding->flags,ext->ext_flags, ext->alg);
516 idx = encoding->flags & IW_ENCODE_INDEX;
518 if (idx < 1 || idx > WEP_KEYS)
522 idx = ieee->tx_keyidx;
524 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
526 crypt = &ieee->crypt[idx];
530 /* some Cisco APs use idx>0 for unicast in dynamic WEP */
531 //printk("not group key, flags:%x, ext->alg:%d\n", ext->ext_flags, ext->alg);
532 if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
534 if (ieee->iw_mode == IW_MODE_INFRA)
536 crypt = &ieee->crypt[idx];
542 sec.flags |= SEC_ENABLED;
544 if ((encoding->flags & IW_ENCODE_DISABLED) ||
545 ext->alg == IW_ENCODE_ALG_NONE) {
547 ieee80211_crypt_delayed_deinit(ieee, crypt);
549 for (i = 0; i < WEP_KEYS; i++)
551 if (ieee->crypt[i] != NULL)
558 sec.level = SEC_LEVEL_0;
559 sec.flags |= SEC_LEVEL;
561 //printk("disabled: flag:%x\n", encoding->flags);
569 case IW_ENCODE_ALG_WEP:
572 case IW_ENCODE_ALG_TKIP:
575 case IW_ENCODE_ALG_CCMP:
579 IEEE80211_DEBUG_WX("%s: unknown crypto alg %d\n",
580 dev->name, ext->alg);
584 printk("alg name:%s\n",alg);
586 ops = ieee80211_get_crypto_ops(alg);
588 ops = ieee80211_get_crypto_ops(alg);
590 IEEE80211_DEBUG_WX("%s: unknown crypto alg %d\n",
591 dev->name, ext->alg);
592 printk("========>unknown crypto alg %d\n", ext->alg);
597 if (*crypt == NULL || (*crypt)->ops != ops) {
598 struct ieee80211_crypt_data *new_crypt;
600 ieee80211_crypt_delayed_deinit(ieee, crypt);
602 new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
603 if (new_crypt == NULL) {
607 new_crypt->ops = ops;
609 new_crypt->priv = new_crypt->ops->init(idx);
610 if (new_crypt->priv == NULL) {
619 if (ext->key_len > 0 && (*crypt)->ops->set_key &&
620 (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
621 (*crypt)->priv) < 0) {
622 IEEE80211_DEBUG_WX("%s: key setting failed\n", dev->name);
623 printk("key setting failed\n");
629 //printk("skip_host_crypt:ext_flags:%x\n", ext->ext_flags);
630 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
631 ieee->tx_keyidx = idx;
632 sec.active_key = idx;
633 sec.flags |= SEC_ACTIVE_KEY;
636 if (ext->alg != IW_ENCODE_ALG_NONE) {
637 //memcpy(sec.keys[idx], ext->key, ext->key_len);
638 sec.key_sizes[idx] = ext->key_len;
639 sec.flags |= (1 << idx);
640 if (ext->alg == IW_ENCODE_ALG_WEP) {
641 sec.flags |= SEC_LEVEL;
642 sec.level = SEC_LEVEL_1;
643 } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
644 sec.flags |= SEC_LEVEL;
645 sec.level = SEC_LEVEL_2;
646 } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
647 sec.flags |= SEC_LEVEL;
648 sec.level = SEC_LEVEL_3;
650 /* Don't set sec level for group keys. */
652 sec.flags &= ~SEC_LEVEL;
656 if (ieee->set_security)
657 ieee->set_security(ieee->dev, &sec);
659 if (ieee->reset_on_keychange &&
660 ieee->iw_mode != IW_MODE_INFRA &&
661 ieee->reset_port && ieee->reset_port(dev)) {
662 IEEE80211_DEBUG_WX("%s: reset_port failed\n", dev->name);
669 int ieee80211_wx_set_mlme(struct ieee80211_device *ieee,
670 struct iw_request_info *info,
671 union iwreq_data *wrqu, char *extra)
673 struct iw_mlme *mlme = (struct iw_mlme *) extra;
677 case IW_MLME_DISASSOC:
678 ieee80211_disassociate(ieee);
687 int ieee80211_wx_set_auth(struct ieee80211_device *ieee,
688 struct iw_request_info *info,
689 struct iw_param *data, char *extra)
691 switch (data->flags & IW_AUTH_INDEX) {
692 case IW_AUTH_WPA_VERSION:
693 /*need to support wpa2 here*/
694 //printk("wpa version:%x\n", data->value);
696 case IW_AUTH_CIPHER_PAIRWISE:
697 case IW_AUTH_CIPHER_GROUP:
698 case IW_AUTH_KEY_MGMT:
700 * * Host AP driver does not use these parameters and allows
701 * * wpa_supplicant to control them internally.
704 case IW_AUTH_TKIP_COUNTERMEASURES:
705 ieee->tkip_countermeasures = data->value;
707 case IW_AUTH_DROP_UNENCRYPTED:
708 ieee->drop_unencrypted = data->value;
711 case IW_AUTH_80211_AUTH_ALG:
712 //printk("======>%s():data->value is %d\n",__FUNCTION__,data->value);
713 // ieee->open_wep = (data->value&IW_AUTH_ALG_OPEN_SYSTEM)?1:0;
714 if(data->value & IW_AUTH_ALG_SHARED_KEY){
718 else if(data->value & IW_AUTH_ALG_OPEN_SYSTEM){
722 else if(data->value & IW_AUTH_ALG_LEAP){
725 //printk("hahahaa:LEAP\n");
729 //printk("open_wep:%d\n", ieee->open_wep);
733 case IW_AUTH_WPA_ENABLED:
734 ieee->wpa_enabled = (data->value)?1:0;
735 //printk("enalbe wpa:%d\n", ieee->wpa_enabled);
739 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
740 ieee->ieee802_1x = data->value;
742 case IW_AUTH_PRIVACY_INVOKED:
743 ieee->privacy_invoked = data->value;
753 int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len)
757 if (len>MAX_WPA_IE_LEN || (len && ie == NULL))
759 // printk("return error out, len:%d\n", len);
768 printk("len: %Zd, ie:%d\n", len, ie[1]);
771 buf = kmalloc(len, GFP_KERNEL);
774 memcpy(buf, ie, len);
777 ieee->wpa_ie_len = len;
783 ieee->wpa_ie_len = 0;