Merge branch 'work.thaw' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-block.git] / drivers / staging / rtl8192e / rtllib_wx.c
CommitLineData
ecdfa446 1/******************************************************************************
a959dec1
DR
2 *
3 * Copyright(c) 2004 Intel Corporation. All rights reserved.
4 *
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
8 * <jkmaline@cc.hut.fi>
9 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
10 *
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.
14 *
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
18 * more details.
19 *
20 * The full GNU General Public License is included in this distribution in the
21 * file called LICENSE.
22 *
23 * Contact Information:
24 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
25 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *
27 *****************************************************************************/
ecdfa446 28#include <linux/wireless.h>
ecdfa446
GKH
29#include <linux/kmod.h>
30#include <linux/module.h>
c63eee54 31#include <linux/etherdevice.h>
94a79942 32#include "rtllib.h"
ecdfa446
GKH
33struct modes_unit {
34 char *mode_string;
35 int mode_size;
36};
94a79942 37static struct modes_unit rtllib_modes[] = {
62f27cc4
LF
38 {"a", 1},
39 {"b", 1},
40 {"g", 1},
41 {"?", 1},
42 {"N-24G", 5},
43 {"N-5G", 4},
ecdfa446
GKH
44};
45
ecdfa446 46#define MAX_CUSTOM_LEN 64
94a79942
LF
47static inline char *rtl819x_translate_scan(struct rtllib_device *ieee,
48 char *start, char *stop,
49 struct rtllib_network *network,
62f27cc4 50 struct iw_request_info *info)
ecdfa446
GKH
51{
52 char custom[MAX_CUSTOM_LEN];
53 char proto_name[IFNAMSIZ];
54 char *pname = proto_name;
55 char *p;
56 struct iw_event iwe;
57 int i, j;
58 u16 max_rate, rate;
59 static u8 EWC11NHTCap[] = {0x00, 0x90, 0x4c, 0x33};
60
61 /* First entry *MUST* be the AP MAC address */
62 iwe.cmd = SIOCGIWAP;
63 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
c63eee54 64 ether_addr_copy(iwe.u.ap_addr.sa_data, network->bssid);
62f27cc4
LF
65 start = iwe_stream_add_event_rsl(info, start, stop,
66 &iwe, IW_EV_ADDR_LEN);
ecdfa446
GKH
67 /* Remaining entries will be displayed in the order we provide them */
68
69 /* Add the ESSID */
70 iwe.cmd = SIOCGIWESSID;
71 iwe.u.data.flags = 1;
62f27cc4 72 if (network->ssid_len > 0) {
d009f0d7 73 iwe.u.data.length = min_t(u8, network->ssid_len, 32);
62f27cc4
LF
74 start = iwe_stream_add_point_rsl(info, start, stop, &iwe,
75 network->ssid);
76 } else if (network->hidden_ssid_len == 0) {
94a79942 77 iwe.u.data.length = sizeof("<hidden>");
62f27cc4
LF
78 start = iwe_stream_add_point_rsl(info, start, stop,
79 &iwe, "<hidden>");
80 } else {
d009f0d7 81 iwe.u.data.length = min_t(u8, network->hidden_ssid_len, 32);
62f27cc4
LF
82 start = iwe_stream_add_point_rsl(info, start, stop, &iwe,
83 network->hidden_ssid);
94a79942 84 }
ecdfa446
GKH
85 /* Add the protocol name */
86 iwe.cmd = SIOCGIWNAME;
b330f606 87 for (i = 0; i < ARRAY_SIZE(rtllib_modes); i++) {
94a79942 88 if (network->mode&(1<<i)) {
62f27cc4
LF
89 sprintf(pname, rtllib_modes[i].mode_string,
90 rtllib_modes[i].mode_size);
91 pname += rtllib_modes[i].mode_size;
ecdfa446
GKH
92 }
93 }
94 *pname = '\0';
95 snprintf(iwe.u.name, IFNAMSIZ, "IEEE802.11%s", proto_name);
62f27cc4
LF
96 start = iwe_stream_add_event_rsl(info, start, stop,
97 &iwe, IW_EV_CHAR_LEN);
98 /* Add mode */
99 iwe.cmd = SIOCGIWMODE;
100 if (network->capability &
94a79942
LF
101 (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
102 if (network->capability & WLAN_CAPABILITY_ESS)
ecdfa446
GKH
103 iwe.u.mode = IW_MODE_MASTER;
104 else
105 iwe.u.mode = IW_MODE_ADHOC;
62f27cc4
LF
106 start = iwe_stream_add_event_rsl(info, start, stop,
107 &iwe, IW_EV_UINT_LEN);
108 }
ecdfa446 109
62f27cc4 110 /* Add frequency/channel */
ecdfa446 111 iwe.cmd = SIOCGIWFREQ;
ecdfa446
GKH
112 iwe.u.freq.m = network->channel;
113 iwe.u.freq.e = 0;
114 iwe.u.freq.i = 0;
62f27cc4
LF
115 start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
116 IW_EV_FREQ_LEN);
94a79942 117
ecdfa446
GKH
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;
122 else
123 iwe.u.data.flags = IW_ENCODE_DISABLED;
124 iwe.u.data.length = 0;
62f27cc4
LF
125 start = iwe_stream_add_point_rsl(info, start, stop,
126 &iwe, network->ssid);
ecdfa446
GKH
127 /* Add basic and extended rates */
128 max_rate = 0;
129 p = custom;
130 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): ");
62f27cc4 131 for (i = 0, j = 0; i < network->rates_len;) {
ecdfa446
GKH
132 if (j < network->rates_ex_len &&
133 ((network->rates_ex[j] & 0x7F) <
134 (network->rates[i] & 0x7F)))
135 rate = network->rates_ex[j++] & 0x7F;
136 else
137 rate = network->rates[i++] & 0x7F;
138 if (rate > max_rate)
139 max_rate = rate;
140 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
141 "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
142 }
143 for (; j < network->rates_ex_len; j++) {
144 rate = network->rates_ex[j] & 0x7F;
145 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
146 "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
147 if (rate > max_rate)
148 max_rate = rate;
149 }
150
62f27cc4 151 if (network->mode >= IEEE_N_24G) {
e92b71d5 152 struct ht_capab_ele *ht_cap = NULL;
ecdfa446
GKH
153 bool is40M = false, isShortGI = false;
154 u8 max_mcs = 0;
3a6b70c3 155
ecdfa446 156 if (!memcmp(network->bssht.bdHTCapBuf, EWC11NHTCap, 4))
62f27cc4
LF
157 ht_cap = (struct ht_capab_ele *)
158 &network->bssht.bdHTCapBuf[4];
ecdfa446 159 else
62f27cc4
LF
160 ht_cap = (struct ht_capab_ele *)
161 &network->bssht.bdHTCapBuf[0];
162 is40M = (ht_cap->ChlWidth) ? 1 : 0;
163 isShortGI = (ht_cap->ChlWidth) ?
164 ((ht_cap->ShortGI40Mhz) ? 1 : 0) :
165 ((ht_cap->ShortGI20Mhz) ? 1 : 0);
166
167 max_mcs = HTGetHighestMCSRate(ieee, ht_cap->MCS,
168 MCS_FILTER_ALL);
169 rate = MCS_DATA_RATE[is40M][isShortGI][max_mcs & 0x7f];
ecdfa446
GKH
170 if (rate > max_rate)
171 max_rate = rate;
172 }
ecdfa446
GKH
173 iwe.cmd = SIOCGIWRATE;
174 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
175 iwe.u.bitrate.value = max_rate * 500000;
94a79942 176 start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
ecdfa446 177 IW_EV_PARAM_LEN);
ecdfa446
GKH
178 iwe.cmd = IWEVCUSTOM;
179 iwe.u.data.length = p - custom;
180 if (iwe.u.data.length)
62f27cc4
LF
181 start = iwe_stream_add_point_rsl(info, start, stop,
182 &iwe, custom);
ecdfa446
GKH
183 /* Add quality statistics */
184 /* TODO: Fix these values... */
185 iwe.cmd = IWEVQUAL;
186 iwe.u.qual.qual = network->stats.signal;
187 iwe.u.qual.level = network->stats.rssi;
188 iwe.u.qual.noise = network->stats.noise;
94a79942
LF
189 iwe.u.qual.updated = network->stats.mask & RTLLIB_STATMASK_WEMASK;
190 if (!(network->stats.mask & RTLLIB_STATMASK_RSSI))
ecdfa446 191 iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
94a79942 192 if (!(network->stats.mask & RTLLIB_STATMASK_NOISE))
ecdfa446 193 iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
94a79942 194 if (!(network->stats.mask & RTLLIB_STATMASK_SIGNAL))
ecdfa446
GKH
195 iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID;
196 iwe.u.qual.updated = 7;
62f27cc4
LF
197 start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
198 IW_EV_QUAL_LEN);
94a79942 199
ecdfa446
GKH
200 iwe.cmd = IWEVCUSTOM;
201 p = custom;
ecdfa446
GKH
202 iwe.u.data.length = p - custom;
203 if (iwe.u.data.length)
62f27cc4
LF
204 start = iwe_stream_add_point_rsl(info, start, stop,
205 &iwe, custom);
ecdfa446 206
ecdfa446 207 memset(&iwe, 0, sizeof(iwe));
62f27cc4 208 if (network->wpa_ie_len) {
ecdfa446 209 char buf[MAX_WPA_IE_LEN];
3a6b70c3 210
ecdfa446
GKH
211 memcpy(buf, network->wpa_ie, network->wpa_ie_len);
212 iwe.cmd = IWEVGENIE;
213 iwe.u.data.length = network->wpa_ie_len;
94a79942 214 start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
62f27cc4 215 }
ecdfa446 216 memset(&iwe, 0, sizeof(iwe));
62f27cc4 217 if (network->rsn_ie_len) {
ecdfa446 218 char buf[MAX_WPA_IE_LEN];
3a6b70c3 219
ecdfa446
GKH
220 memcpy(buf, network->rsn_ie, network->rsn_ie_len);
221 iwe.cmd = IWEVGENIE;
222 iwe.u.data.length = network->rsn_ie_len;
94a79942 223 start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
62f27cc4 224 }
ecdfa446 225
94a79942
LF
226 /* add info for WZC */
227 memset(&iwe, 0, sizeof(iwe));
62f27cc4 228 if (network->wzc_ie_len) {
94a79942 229 char buf[MAX_WZC_IE_LEN];
3a6b70c3 230
94a79942
LF
231 memcpy(buf, network->wzc_ie, network->wzc_ie_len);
232 iwe.cmd = IWEVGENIE;
233 iwe.u.data.length = network->wzc_ie_len;
234 start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
62f27cc4 235 }
ecdfa446
GKH
236
237 /* Add EXTRA: Age to display seconds since last beacon/probe response
14b40d92
MK
238 * for given network.
239 */
ecdfa446
GKH
240 iwe.cmd = IWEVCUSTOM;
241 p = custom;
242 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
62f27cc4
LF
243 " Last beacon: %lums ago",
244 (jiffies - network->last_scanned) / (HZ / 100));
ecdfa446
GKH
245 iwe.u.data.length = p - custom;
246 if (iwe.u.data.length)
62f27cc4
LF
247 start = iwe_stream_add_point_rsl(info, start, stop,
248 &iwe, custom);
ecdfa446
GKH
249
250 return start;
251}
252
94a79942 253int rtllib_wx_get_scan(struct rtllib_device *ieee,
ecdfa446
GKH
254 struct iw_request_info *info,
255 union iwreq_data *wrqu, char *extra)
256{
94a79942 257 struct rtllib_network *network;
ecdfa446
GKH
258 unsigned long flags;
259
260 char *ev = extra;
94a79942 261 char *stop = ev + wrqu->data.length;
ecdfa446
GKH
262 int i = 0;
263 int err = 0;
3a6b70c3 264
c6a91aba 265 netdev_dbg(ieee->dev, "Getting scan\n");
9afa9370 266 mutex_lock(&ieee->wx_mutex);
ecdfa446
GKH
267 spin_lock_irqsave(&ieee->lock, flags);
268
269 list_for_each_entry(network, &ieee->network_list, list) {
270 i++;
62f27cc4 271 if ((stop - ev) < 200) {
ecdfa446
GKH
272 err = -E2BIG;
273 break;
62f27cc4 274 }
ecdfa446
GKH
275 if (ieee->scan_age == 0 ||
276 time_after(network->last_scanned + ieee->scan_age, jiffies))
62f27cc4
LF
277 ev = rtl819x_translate_scan(ieee, ev, stop, network,
278 info);
ecdfa446 279 else
521a9cbd
MK
280 netdev_dbg(ieee->dev,
281 "Network '%s ( %pM)' hidden due to age (%lums).\n",
282 escape_essid(network->ssid,
283 network->ssid_len),
284 network->bssid,
285 (jiffies - network->last_scanned) /
286 (HZ / 100));
ecdfa446
GKH
287 }
288
289 spin_unlock_irqrestore(&ieee->lock, flags);
9afa9370 290 mutex_unlock(&ieee->wx_mutex);
ecdfa446
GKH
291 wrqu->data.length = ev - extra;
292 wrqu->data.flags = 0;
293
c6a91aba 294 netdev_dbg(ieee->dev, "%s(): %d networks returned.\n", __func__, i);
ecdfa446
GKH
295
296 return err;
297}
3b28499c 298EXPORT_SYMBOL(rtllib_wx_get_scan);
ecdfa446 299
94a79942 300int rtllib_wx_set_encode(struct rtllib_device *ieee,
ecdfa446
GKH
301 struct iw_request_info *info,
302 union iwreq_data *wrqu, char *keybuf)
303{
304 struct iw_point *erq = &(wrqu->encoding);
305 struct net_device *dev = ieee->dev;
94a79942 306 struct rtllib_security sec = {
ecdfa446
GKH
307 .flags = 0
308 };
309 int i, key, key_provided, len;
32c44cb5 310 struct lib80211_crypt_data **crypt;
ecdfa446 311
c6a91aba 312 netdev_dbg(ieee->dev, "%s()\n", __func__);
ecdfa446
GKH
313
314 key = erq->flags & IW_ENCODE_INDEX;
315 if (key) {
184f1938 316 if (key > NUM_WEP_KEYS)
ecdfa446
GKH
317 return -EINVAL;
318 key--;
319 key_provided = 1;
320 } else {
321 key_provided = 0;
0ddcf5fd 322 key = ieee->crypt_info.tx_keyidx;
ecdfa446
GKH
323 }
324
c6a91aba 325 netdev_dbg(ieee->dev, "Key: %d [%s]\n", key, key_provided ?
ecdfa446 326 "provided" : "default");
0ddcf5fd 327 crypt = &ieee->crypt_info.crypt[key];
ecdfa446
GKH
328 if (erq->flags & IW_ENCODE_DISABLED) {
329 if (key_provided && *crypt) {
c6a91aba
MK
330 netdev_dbg(ieee->dev,
331 "Disabling encryption on key %d.\n", key);
3b148be0 332 lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
ecdfa446 333 } else
c6a91aba 334 netdev_dbg(ieee->dev, "Disabling encryption.\n");
ecdfa446
GKH
335
336 /* Check all the keys to see if any are still configured,
14b40d92
MK
337 * and if no key index was provided, de-init them all
338 */
184f1938 339 for (i = 0; i < NUM_WEP_KEYS; i++) {
0ddcf5fd 340 if (ieee->crypt_info.crypt[i] != NULL) {
ecdfa446
GKH
341 if (key_provided)
342 break;
3b148be0 343 lib80211_crypt_delayed_deinit(&ieee->crypt_info,
a3ed22ea 344 &ieee->crypt_info.crypt[i]);
ecdfa446
GKH
345 }
346 }
347
184f1938 348 if (i == NUM_WEP_KEYS) {
ecdfa446
GKH
349 sec.enabled = 0;
350 sec.level = SEC_LEVEL_0;
351 sec.flags |= SEC_ENABLED | SEC_LEVEL;
352 }
353
354 goto done;
355 }
356
357
358
359 sec.enabled = 1;
360 sec.flags |= SEC_ENABLED;
361
362 if (*crypt != NULL && (*crypt)->ops != NULL &&
3b148be0 363 strcmp((*crypt)->ops->name, "R-WEP") != 0) {
ecdfa446 364 /* changing to use WEP; deinit previously used algorithm
14b40d92
MK
365 * on this key
366 */
3b148be0 367 lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
ecdfa446
GKH
368 }
369
370 if (*crypt == NULL) {
32c44cb5 371 struct lib80211_crypt_data *new_crypt;
ecdfa446
GKH
372
373 /* take WEP into use */
3294a9c5 374 new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
ecdfa446
GKH
375 if (new_crypt == NULL)
376 return -ENOMEM;
3b148be0 377 new_crypt->ops = lib80211_get_crypto_ops("R-WEP");
94a79942
LF
378 if (!new_crypt->ops) {
379 request_module("rtllib_crypt_wep");
3b148be0 380 new_crypt->ops = lib80211_get_crypto_ops("R-WEP");
94a79942
LF
381 }
382
a010a337 383 if (new_crypt->ops)
ecdfa446
GKH
384 new_crypt->priv = new_crypt->ops->init(key);
385
386 if (!new_crypt->ops || !new_crypt->priv) {
387 kfree(new_crypt);
388 new_crypt = NULL;
389
d69d2054
MK
390 netdev_warn(dev,
391 "%s: could not initialize WEP: load module rtllib_crypt_wep\n",
392 dev->name);
ecdfa446
GKH
393 return -EOPNOTSUPP;
394 }
395 *crypt = new_crypt;
396 }
397
398 /* If a new key was provided, set it up */
399 if (erq->length > 0) {
400 len = erq->length <= 5 ? 5 : 13;
401 memcpy(sec.keys[key], keybuf, erq->length);
402 if (len > erq->length)
403 memset(sec.keys[key] + erq->length, 0,
404 len - erq->length);
c6a91aba
MK
405 netdev_dbg(ieee->dev, "Setting key %d to '%s' (%d:%d bytes)\n",
406 key, escape_essid(sec.keys[key], len), erq->length,
407 len);
ecdfa446 408 sec.key_sizes[key] = len;
94a79942 409 (*crypt)->ops->set_key(sec.keys[key], len, NULL,
ecdfa446
GKH
410 (*crypt)->priv);
411 sec.flags |= (1 << key);
412 /* This ensures a key will be activated if no key is
14b40d92
MK
413 * explicitly set
414 */
ecdfa446
GKH
415 if (key == sec.active_key)
416 sec.flags |= SEC_ACTIVE_KEY;
0ddcf5fd 417 ieee->crypt_info.tx_keyidx = key;
ecdfa446
GKH
418
419 } else {
420 len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
421 NULL, (*crypt)->priv);
422 if (len == 0) {
423 /* Set a default key of all 0 */
c60cfc8e 424 netdev_info(ieee->dev, "Setting key %d to all zero.\n",
ecdfa446
GKH
425 key);
426
ecdfa446
GKH
427 memset(sec.keys[key], 0, 13);
428 (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
429 (*crypt)->priv);
430 sec.key_sizes[key] = 13;
431 sec.flags |= (1 << key);
432 }
433
434 /* No key data - just set the default TX key index */
435 if (key_provided) {
c6a91aba
MK
436 netdev_dbg(ieee->dev,
437 "Setting key %d as default Tx key.\n", key);
0ddcf5fd 438 ieee->crypt_info.tx_keyidx = key;
ecdfa446
GKH
439 sec.active_key = key;
440 sec.flags |= SEC_ACTIVE_KEY;
441 }
442 }
ecdfa446
GKH
443 done:
444 ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
62f27cc4
LF
445 ieee->auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
446 WLAN_AUTH_SHARED_KEY;
ecdfa446
GKH
447 sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
448 sec.flags |= SEC_AUTH_MODE;
c6a91aba 449 netdev_dbg(ieee->dev, "Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
ecdfa446
GKH
450 "OPEN" : "SHARED KEY");
451
452 /* For now we just support WEP, so only set that security level...
14b40d92
MK
453 * TODO: When WPA is added this is one place that needs to change
454 */
ecdfa446
GKH
455 sec.flags |= SEC_LEVEL;
456 sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
457
458 if (ieee->set_security)
94a79942 459 ieee->set_security(dev, &sec);
ecdfa446
GKH
460
461 /* Do not reset port if card is in Managed mode since resetting will
462 * generate new IEEE 802.11 authentication which may end up in looping
463 * with IEEE 802.1X. If your hardware requires a reset after WEP
464 * configuration (for example... Prism2), implement the reset_port in
14b40d92
MK
465 * the callbacks structures used to initialize the 802.11 stack.
466 */
ecdfa446
GKH
467 if (ieee->reset_on_keychange &&
468 ieee->iw_mode != IW_MODE_INFRA &&
94a79942 469 ieee->reset_port && ieee->reset_port(dev)) {
c60cfc8e 470 netdev_dbg(dev, "%s: reset_port failed\n", dev->name);
ecdfa446
GKH
471 return -EINVAL;
472 }
473 return 0;
474}
3b28499c 475EXPORT_SYMBOL(rtllib_wx_set_encode);
ecdfa446 476
94a79942 477int rtllib_wx_get_encode(struct rtllib_device *ieee,
ecdfa446
GKH
478 struct iw_request_info *info,
479 union iwreq_data *wrqu, char *keybuf)
480{
481 struct iw_point *erq = &(wrqu->encoding);
482 int len, key;
32c44cb5 483 struct lib80211_crypt_data *crypt;
ecdfa446 484
c6a91aba 485 netdev_dbg(ieee->dev, "%s()\n", __func__);
ecdfa446 486
94a79942 487 if (ieee->iw_mode == IW_MODE_MONITOR)
ecdfa446
GKH
488 return -1;
489
490 key = erq->flags & IW_ENCODE_INDEX;
491 if (key) {
184f1938 492 if (key > NUM_WEP_KEYS)
ecdfa446
GKH
493 return -EINVAL;
494 key--;
94a79942 495 } else {
0ddcf5fd 496 key = ieee->crypt_info.tx_keyidx;
94a79942 497 }
0ddcf5fd 498 crypt = ieee->crypt_info.crypt[key];
94a79942 499
ecdfa446
GKH
500 erq->flags = key + 1;
501
502 if (crypt == NULL || crypt->ops == NULL) {
503 erq->length = 0;
504 erq->flags |= IW_ENCODE_DISABLED;
505 return 0;
506 }
ecdfa446 507 len = crypt->ops->get_key(keybuf, SCM_KEY_LEN, NULL, crypt->priv);
622fd494
MK
508
509 erq->length = max(len, 0);
ecdfa446
GKH
510
511 erq->flags |= IW_ENCODE_ENABLED;
512
513 if (ieee->open_wep)
514 erq->flags |= IW_ENCODE_OPEN;
515 else
516 erq->flags |= IW_ENCODE_RESTRICTED;
517
518 return 0;
519}
3b28499c 520EXPORT_SYMBOL(rtllib_wx_get_encode);
3591733d 521
94a79942 522int rtllib_wx_set_encode_ext(struct rtllib_device *ieee,
62f27cc4
LF
523 struct iw_request_info *info,
524 union iwreq_data *wrqu, char *extra)
ecdfa446
GKH
525{
526 int ret = 0;
ecdfa446 527 struct net_device *dev = ieee->dev;
62f27cc4
LF
528 struct iw_point *encoding = &wrqu->encoding;
529 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
530 int i, idx;
531 int group_key = 0;
532 const char *alg, *module;
32c44cb5
SM
533 struct lib80211_crypto_ops *ops;
534 struct lib80211_crypt_data **crypt;
62f27cc4
LF
535
536 struct rtllib_security sec = {
537 .flags = 0,
538 };
539 idx = encoding->flags & IW_ENCODE_INDEX;
540 if (idx) {
184f1938 541 if (idx < 1 || idx > NUM_WEP_KEYS)
62f27cc4
LF
542 return -EINVAL;
543 idx--;
94a79942 544 } else{
0ddcf5fd 545 idx = ieee->crypt_info.tx_keyidx;
94a79942
LF
546 }
547 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
0ddcf5fd 548 crypt = &ieee->crypt_info.crypt[idx];
94a79942
LF
549 group_key = 1;
550 } else {
551 /* some Cisco APs use idx>0 for unicast in dynamic WEP */
552 if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
62f27cc4 553 return -EINVAL;
94a79942 554 if (ieee->iw_mode == IW_MODE_INFRA)
0ddcf5fd 555 crypt = &ieee->crypt_info.crypt[idx];
94a79942
LF
556 else
557 return -EINVAL;
558 }
ecdfa446 559
94a79942
LF
560 sec.flags |= SEC_ENABLED;
561 if ((encoding->flags & IW_ENCODE_DISABLED) ||
62f27cc4 562 ext->alg == IW_ENCODE_ALG_NONE) {
94a79942 563 if (*crypt)
3b148be0 564 lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
ecdfa446 565
184f1938 566 for (i = 0; i < NUM_WEP_KEYS; i++) {
0ddcf5fd 567 if (ieee->crypt_info.crypt[i] != NULL)
94a79942
LF
568 break;
569 }
184f1938 570 if (i == NUM_WEP_KEYS) {
94a79942
LF
571 sec.enabled = 0;
572 sec.level = SEC_LEVEL_0;
573 sec.flags |= SEC_LEVEL;
574 }
575 goto done;
576 }
ecdfa446
GKH
577
578 sec.enabled = 1;
62f27cc4
LF
579 switch (ext->alg) {
580 case IW_ENCODE_ALG_WEP:
3b148be0 581 alg = "R-WEP";
62f27cc4
LF
582 module = "rtllib_crypt_wep";
583 break;
584 case IW_ENCODE_ALG_TKIP:
3b148be0 585 alg = "R-TKIP";
62f27cc4
LF
586 module = "rtllib_crypt_tkip";
587 break;
588 case IW_ENCODE_ALG_CCMP:
3b148be0 589 alg = "R-CCMP";
62f27cc4
LF
590 module = "rtllib_crypt_ccmp";
591 break;
592 default:
c6a91aba 593 netdev_dbg(ieee->dev, "Unknown crypto alg %d\n", ext->alg);
62f27cc4
LF
594 ret = -EINVAL;
595 goto done;
596 }
6b675ad8 597 netdev_dbg(dev, "alg name:%s\n", alg);
ecdfa446 598
3b148be0 599 ops = lib80211_get_crypto_ops(alg);
62f27cc4
LF
600 if (ops == NULL) {
601 char tempbuf[100];
602
603 memset(tempbuf, 0x00, 100);
604 sprintf(tempbuf, "%s", module);
605 request_module("%s", tempbuf);
3b148be0 606 ops = lib80211_get_crypto_ops(alg);
62f27cc4
LF
607 }
608 if (ops == NULL) {
c60cfc8e 609 netdev_info(dev, "========>unknown crypto alg %d\n", ext->alg);
62f27cc4
LF
610 ret = -EINVAL;
611 goto done;
612 }
613
614 if (*crypt == NULL || (*crypt)->ops != ops) {
32c44cb5 615 struct lib80211_crypt_data *new_crypt;
62f27cc4 616
3b148be0 617 lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
62f27cc4
LF
618
619 new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
620 if (new_crypt == NULL) {
621 ret = -ENOMEM;
622 goto done;
623 }
624 new_crypt->ops = ops;
0bd35534 625 if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
62f27cc4
LF
626 new_crypt->priv = new_crypt->ops->init(idx);
627
628 if (new_crypt->priv == NULL) {
629 kfree(new_crypt);
630 ret = -EINVAL;
631 goto done;
632 }
633 *crypt = new_crypt;
634
635 }
636
637 if (ext->key_len > 0 && (*crypt)->ops->set_key &&
638 (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
639 (*crypt)->priv) < 0) {
c60cfc8e 640 netdev_info(dev, "key setting failed\n");
62f27cc4
LF
641 ret = -EINVAL;
642 goto done;
643 }
644 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
0ddcf5fd 645 ieee->crypt_info.tx_keyidx = idx;
62f27cc4
LF
646 sec.active_key = idx;
647 sec.flags |= SEC_ACTIVE_KEY;
648 }
649 if (ext->alg != IW_ENCODE_ALG_NONE) {
650 sec.key_sizes[idx] = ext->key_len;
651 sec.flags |= (1 << idx);
652 if (ext->alg == IW_ENCODE_ALG_WEP) {
653 sec.flags |= SEC_LEVEL;
654 sec.level = SEC_LEVEL_1;
655 } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
656 sec.flags |= SEC_LEVEL;
657 sec.level = SEC_LEVEL_2;
658 } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
659 sec.flags |= SEC_LEVEL;
660 sec.level = SEC_LEVEL_3;
661 }
662 /* Don't set sec level for group keys. */
663 if (group_key)
664 sec.flags &= ~SEC_LEVEL;
665 }
ecdfa446 666done:
62f27cc4
LF
667 if (ieee->set_security)
668 ieee->set_security(ieee->dev, &sec);
ecdfa446 669
8ad835b9 670 if (ieee->reset_on_keychange &&
62f27cc4
LF
671 ieee->iw_mode != IW_MODE_INFRA &&
672 ieee->reset_port && ieee->reset_port(dev)) {
c6a91aba 673 netdev_dbg(ieee->dev, "Port reset failed\n");
62f27cc4
LF
674 return -EINVAL;
675 }
676 return ret;
ecdfa446 677}
3b28499c 678EXPORT_SYMBOL(rtllib_wx_set_encode_ext);
ecdfa446 679
94a79942 680int rtllib_wx_set_mlme(struct rtllib_device *ieee,
62f27cc4
LF
681 struct iw_request_info *info,
682 union iwreq_data *wrqu, char *extra)
ecdfa446 683{
94a79942
LF
684 u8 i = 0;
685 bool deauth = false;
ecdfa446 686 struct iw_mlme *mlme = (struct iw_mlme *) extra;
94a79942
LF
687
688 if (ieee->state != RTLLIB_LINKED)
689 return -ENOLINK;
690
9afa9370 691 mutex_lock(&ieee->wx_mutex);
94a79942 692
ecdfa446 693 switch (mlme->cmd) {
62f27cc4
LF
694 case IW_MLME_DEAUTH:
695 deauth = true;
f67ffd61 696 /* fall through */
62f27cc4 697 case IW_MLME_DISASSOC:
4bb01423 698 if (deauth)
c60cfc8e 699 netdev_info(ieee->dev, "disauth packet !\n");
62f27cc4 700 else
c60cfc8e 701 netdev_info(ieee->dev, "dis associate packet!\n");
94a79942 702
62f27cc4 703 ieee->cannot_notify = true;
94a79942 704
62f27cc4
LF
705 SendDisassociation(ieee, deauth, mlme->reason_code);
706 rtllib_disassociate(ieee);
94a79942 707
62f27cc4
LF
708 ieee->wap_set = 0;
709 for (i = 0; i < 6; i++)
710 ieee->current_network.bssid[i] = 0x55;
94a79942 711
62f27cc4
LF
712 ieee->ssid_set = 0;
713 ieee->current_network.ssid[0] = '\0';
714 ieee->current_network.ssid_len = 0;
ecdfa446 715 break;
94a79942 716 default:
9afa9370 717 mutex_unlock(&ieee->wx_mutex);
94a79942
LF
718 return -EOPNOTSUPP;
719 }
720
9afa9370 721 mutex_unlock(&ieee->wx_mutex);
94a79942 722
ecdfa446
GKH
723 return 0;
724}
3b28499c 725EXPORT_SYMBOL(rtllib_wx_set_mlme);
ecdfa446 726
94a79942 727int rtllib_wx_set_auth(struct rtllib_device *ieee,
62f27cc4
LF
728 struct iw_request_info *info,
729 struct iw_param *data, char *extra)
ecdfa446 730{
ecdfa446 731 switch (data->flags & IW_AUTH_INDEX) {
62f27cc4 732 case IW_AUTH_WPA_VERSION:
ecdfa446 733 break;
62f27cc4
LF
734 case IW_AUTH_CIPHER_PAIRWISE:
735 case IW_AUTH_CIPHER_GROUP:
736 case IW_AUTH_KEY_MGMT:
14b40d92 737 /* Host AP driver does not use these parameters and allows
94a79942
LF
738 * wpa_supplicant to control them internally.
739 */
62f27cc4
LF
740 break;
741 case IW_AUTH_TKIP_COUNTERMEASURES:
742 ieee->tkip_countermeasures = data->value;
743 break;
744 case IW_AUTH_DROP_UNENCRYPTED:
745 ieee->drop_unencrypted = data->value;
ecdfa446
GKH
746 break;
747
748 case IW_AUTH_80211_AUTH_ALG:
62f27cc4 749 if (data->value & IW_AUTH_ALG_SHARED_KEY) {
ecdfa446
GKH
750 ieee->open_wep = 0;
751 ieee->auth_mode = 1;
62f27cc4 752 } else if (data->value & IW_AUTH_ALG_OPEN_SYSTEM) {
ecdfa446
GKH
753 ieee->open_wep = 1;
754 ieee->auth_mode = 0;
62f27cc4 755 } else if (data->value & IW_AUTH_ALG_LEAP) {
ecdfa446
GKH
756 ieee->open_wep = 1;
757 ieee->auth_mode = 2;
62f27cc4 758 } else
ecdfa446 759 return -EINVAL;
ecdfa446
GKH
760 break;
761
ecdfa446 762 case IW_AUTH_WPA_ENABLED:
62f27cc4 763 ieee->wpa_enabled = (data->value) ? 1 : 0;
ecdfa446
GKH
764 break;
765
ecdfa446 766 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
62f27cc4 767 ieee->ieee802_1x = data->value;
ecdfa446
GKH
768 break;
769 case IW_AUTH_PRIVACY_INVOKED:
770 ieee->privacy_invoked = data->value;
771 break;
772 default:
62f27cc4 773 return -EOPNOTSUPP;
ecdfa446 774 }
ecdfa446
GKH
775 return 0;
776}
3b28499c 777EXPORT_SYMBOL(rtllib_wx_set_auth);
94a79942
LF
778
779int rtllib_wx_set_gen_ie(struct rtllib_device *ieee, u8 *ie, size_t len)
ecdfa446 780{
ecdfa446 781 u8 *buf;
62f27cc4 782 u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
ecdfa446 783
62f27cc4 784 if (len > MAX_WPA_IE_LEN || (len && ie == NULL))
94a79942 785 return -EINVAL;
ecdfa446 786
94a79942
LF
787 if (len) {
788 eid = ie[0];
62f27cc4
LF
789 if ((eid == MFIE_TYPE_GENERIC) && (!memcmp(&ie[2],
790 wps_oui, 4))) {
94a79942 791
a5c06ad8 792 ieee->wps_ie_len = min_t(size_t, len, MAX_WZC_IE_LEN);
23226977 793 buf = kmemdup(ie, ieee->wps_ie_len, GFP_KERNEL);
94a79942
LF
794 if (buf == NULL)
795 return -ENOMEM;
94a79942
LF
796 ieee->wps_ie = buf;
797 return 0;
798 }
799 }
800 ieee->wps_ie_len = 0;
62f27cc4 801 kfree(ieee->wps_ie);
94a79942
LF
802 ieee->wps_ie = NULL;
803 if (len) {
62f27cc4 804 if (len != ie[1]+2)
ecdfa446 805 return -EINVAL;
23226977 806 buf = kmemdup(ie, len, GFP_KERNEL);
ecdfa446
GKH
807 if (buf == NULL)
808 return -ENOMEM;
ecdfa446
GKH
809 kfree(ieee->wpa_ie);
810 ieee->wpa_ie = buf;
811 ieee->wpa_ie_len = len;
94a79942 812 } else {
ecdfa446
GKH
813 kfree(ieee->wpa_ie);
814 ieee->wpa_ie = NULL;
815 ieee->wpa_ie_len = 0;
816 }
ecdfa446 817 return 0;
ecdfa446 818}
3b28499c 819EXPORT_SYMBOL(rtllib_wx_set_gen_ie);