1 /******************************************************************************
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  *****************************************************************************/
28 #include <linux/wireless.h>
29 #include <linux/kmod.h>
30 #include <linux/module.h>
31 #include <linux/etherdevice.h>
32 #include "rtllib.h"
33 struct modes_unit {
34 	char *mode_string;
35 	int mode_size;
36 };
37 static struct modes_unit rtllib_modes[] = {
38 	{"a", 1},
39 	{"b", 1},
40 	{"g", 1},
41 	{"?", 1},
42 	{"N-24G", 5},
43 	{"N-5G", 4},
44 };
45 
46 #define MAX_CUSTOM_LEN 64
47 static inline char *rtl819x_translate_scan(struct rtllib_device *ieee,
48 					   char *start, char *stop,
49 					   struct rtllib_network *network,
50 					   struct iw_request_info *info)
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;
64 	ether_addr_copy(iwe.u.ap_addr.sa_data, network->bssid);
65 	start = iwe_stream_add_event_rsl(info, start, stop,
66 					 &iwe, IW_EV_ADDR_LEN);
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;
72 	if (network->ssid_len > 0) {
73 		iwe.u.data.length = min_t(u8, network->ssid_len, 32);
74 		start = iwe_stream_add_point_rsl(info, start, stop, &iwe,
75 						 network->ssid);
76 	} else if (network->hidden_ssid_len == 0) {
77 		iwe.u.data.length = sizeof("<hidden>");
78 		start = iwe_stream_add_point_rsl(info, start, stop,
79 						 &iwe, "<hidden>");
80 	} else {
81 		iwe.u.data.length = min_t(u8, network->hidden_ssid_len, 32);
82 		start = iwe_stream_add_point_rsl(info, start, stop, &iwe,
83 						 network->hidden_ssid);
84 	}
85 	/* Add the protocol name */
86 	iwe.cmd = SIOCGIWNAME;
87 	for (i = 0; i < ARRAY_SIZE(rtllib_modes); i++) {
88 		if (network->mode&(1<<i)) {
89 			sprintf(pname, rtllib_modes[i].mode_string,
90 				rtllib_modes[i].mode_size);
91 			pname += rtllib_modes[i].mode_size;
92 		}
93 	}
94 	*pname = '\0';
95 	snprintf(iwe.u.name, IFNAMSIZ, "IEEE802.11%s", proto_name);
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 &
101 	    (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
102 		if (network->capability & WLAN_CAPABILITY_ESS)
103 			iwe.u.mode = IW_MODE_MASTER;
104 		else
105 			iwe.u.mode = IW_MODE_ADHOC;
106 		start = iwe_stream_add_event_rsl(info, start, stop,
107 						 &iwe, IW_EV_UINT_LEN);
108 	}
109 
110 	/* Add frequency/channel */
111 	iwe.cmd = SIOCGIWFREQ;
112 	iwe.u.freq.m = network->channel;
113 	iwe.u.freq.e = 0;
114 	iwe.u.freq.i = 0;
115 	start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
116 					 IW_EV_FREQ_LEN);
117 
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;
125 	start = iwe_stream_add_point_rsl(info, start, stop,
126 					 &iwe, network->ssid);
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): ");
131 	for (i = 0, j = 0; i < network->rates_len;) {
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 
151 	if (network->mode >= IEEE_N_24G) {
152 		struct ht_capab_ele *ht_cap = NULL;
153 		bool is40M = false, isShortGI = false;
154 		u8 max_mcs = 0;
155 
156 		if (!memcmp(network->bssht.bdHTCapBuf, EWC11NHTCap, 4))
157 			ht_cap = (struct ht_capab_ele *)
158 				 &network->bssht.bdHTCapBuf[4];
159 		else
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];
170 		if (rate > max_rate)
171 			max_rate = rate;
172 	}
173 	iwe.cmd = SIOCGIWRATE;
174 	iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
175 	iwe.u.bitrate.value = max_rate * 500000;
176 	start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
177 				     IW_EV_PARAM_LEN);
178 	iwe.cmd = IWEVCUSTOM;
179 	iwe.u.data.length = p - custom;
180 	if (iwe.u.data.length)
181 		start = iwe_stream_add_point_rsl(info, start, stop,
182 						 &iwe, custom);
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;
189 	iwe.u.qual.updated = network->stats.mask & RTLLIB_STATMASK_WEMASK;
190 	if (!(network->stats.mask & RTLLIB_STATMASK_RSSI))
191 		iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
192 	if (!(network->stats.mask & RTLLIB_STATMASK_NOISE))
193 		iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
194 	if (!(network->stats.mask & RTLLIB_STATMASK_SIGNAL))
195 		iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID;
196 	iwe.u.qual.updated = 7;
197 	start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
198 					 IW_EV_QUAL_LEN);
199 
200 	iwe.cmd = IWEVCUSTOM;
201 	p = custom;
202 	iwe.u.data.length = p - custom;
203 	if (iwe.u.data.length)
204 		start = iwe_stream_add_point_rsl(info, start, stop,
205 						 &iwe, custom);
206 
207 	memset(&iwe, 0, sizeof(iwe));
208 	if (network->wpa_ie_len) {
209 		char buf[MAX_WPA_IE_LEN];
210 
211 		memcpy(buf, network->wpa_ie, network->wpa_ie_len);
212 		iwe.cmd = IWEVGENIE;
213 		iwe.u.data.length = network->wpa_ie_len;
214 		start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
215 	}
216 	memset(&iwe, 0, sizeof(iwe));
217 	if (network->rsn_ie_len) {
218 		char buf[MAX_WPA_IE_LEN];
219 
220 		memcpy(buf, network->rsn_ie, network->rsn_ie_len);
221 		iwe.cmd = IWEVGENIE;
222 		iwe.u.data.length = network->rsn_ie_len;
223 		start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
224 	}
225 
226 	/* add info for WZC */
227 	memset(&iwe, 0, sizeof(iwe));
228 	if (network->wzc_ie_len) {
229 		char buf[MAX_WZC_IE_LEN];
230 
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);
235 	}
236 
237 	/* Add EXTRA: Age to display seconds since last beacon/probe response
238 	 * for given network.
239 	 */
240 	iwe.cmd = IWEVCUSTOM;
241 	p = custom;
242 	p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
243 		      " Last beacon: %lums ago",
244 		      (jiffies - network->last_scanned) / (HZ / 100));
245 	iwe.u.data.length = p - custom;
246 	if (iwe.u.data.length)
247 		start = iwe_stream_add_point_rsl(info, start, stop,
248 						 &iwe, custom);
249 
250 	return start;
251 }
252 
253 int rtllib_wx_get_scan(struct rtllib_device *ieee,
254 			  struct iw_request_info *info,
255 			  union iwreq_data *wrqu, char *extra)
256 {
257 	struct rtllib_network *network;
258 	unsigned long flags;
259 
260 	char *ev = extra;
261 	char *stop = ev + wrqu->data.length;
262 	int i = 0;
263 	int err = 0;
264 
265 	netdev_dbg(ieee->dev, "Getting scan\n");
266 	mutex_lock(&ieee->wx_mutex);
267 	spin_lock_irqsave(&ieee->lock, flags);
268 
269 	list_for_each_entry(network, &ieee->network_list, list) {
270 		i++;
271 		if ((stop - ev) < 200) {
272 			err = -E2BIG;
273 			break;
274 		}
275 		if (ieee->scan_age == 0 ||
276 		    time_after(network->last_scanned + ieee->scan_age, jiffies))
277 			ev = rtl819x_translate_scan(ieee, ev, stop, network,
278 						    info);
279 		else
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));
287 	}
288 
289 	spin_unlock_irqrestore(&ieee->lock, flags);
290 	mutex_unlock(&ieee->wx_mutex);
291 	wrqu->data.length = ev -  extra;
292 	wrqu->data.flags = 0;
293 
294 	netdev_dbg(ieee->dev, "%s(): %d networks returned.\n", __func__, i);
295 
296 	return err;
297 }
298 EXPORT_SYMBOL(rtllib_wx_get_scan);
299 
300 int rtllib_wx_set_encode(struct rtllib_device *ieee,
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;
306 	struct rtllib_security sec = {
307 		.flags = 0
308 	};
309 	int i, key, key_provided, len;
310 	struct lib80211_crypt_data **crypt;
311 
312 	netdev_dbg(ieee->dev, "%s()\n", __func__);
313 
314 	key = erq->flags & IW_ENCODE_INDEX;
315 	if (key) {
316 		if (key > NUM_WEP_KEYS)
317 			return -EINVAL;
318 		key--;
319 		key_provided = 1;
320 	} else {
321 		key_provided = 0;
322 		key = ieee->crypt_info.tx_keyidx;
323 	}
324 
325 	netdev_dbg(ieee->dev, "Key: %d [%s]\n", key, key_provided ?
326 			   "provided" : "default");
327 	crypt = &ieee->crypt_info.crypt[key];
328 	if (erq->flags & IW_ENCODE_DISABLED) {
329 		if (key_provided && *crypt) {
330 			netdev_dbg(ieee->dev,
331 				   "Disabling encryption on key %d.\n", key);
332 			lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
333 		} else
334 			netdev_dbg(ieee->dev, "Disabling encryption.\n");
335 
336 		/* Check all the keys to see if any are still configured,
337 		 * and if no key index was provided, de-init them all
338 		 */
339 		for (i = 0; i < NUM_WEP_KEYS; i++) {
340 			if (ieee->crypt_info.crypt[i] != NULL) {
341 				if (key_provided)
342 					break;
343 				lib80211_crypt_delayed_deinit(&ieee->crypt_info,
344 						    &ieee->crypt_info.crypt[i]);
345 			}
346 		}
347 
348 		if (i == NUM_WEP_KEYS) {
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 &&
363 	    strcmp((*crypt)->ops->name, "R-WEP") != 0) {
364 		/* changing to use WEP; deinit previously used algorithm
365 		 * on this key
366 		 */
367 		lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
368 	}
369 
370 	if (*crypt == NULL) {
371 		struct lib80211_crypt_data *new_crypt;
372 
373 		/* take WEP into use */
374 		new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
375 		if (new_crypt == NULL)
376 			return -ENOMEM;
377 		new_crypt->ops = lib80211_get_crypto_ops("R-WEP");
378 		if (!new_crypt->ops) {
379 			request_module("rtllib_crypt_wep");
380 			new_crypt->ops = lib80211_get_crypto_ops("R-WEP");
381 		}
382 
383 		if (new_crypt->ops)
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 
390 			netdev_warn(dev,
391 				    "%s: could not initialize WEP: load module rtllib_crypt_wep\n",
392 				    dev->name);
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);
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);
408 		sec.key_sizes[key] = len;
409 		(*crypt)->ops->set_key(sec.keys[key], len, NULL,
410 				       (*crypt)->priv);
411 		sec.flags |= (1 << key);
412 		/* This ensures a key will be activated if no key is
413 		 * explicitly set
414 		 */
415 		if (key == sec.active_key)
416 			sec.flags |= SEC_ACTIVE_KEY;
417 		ieee->crypt_info.tx_keyidx = key;
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 */
424 			netdev_info(ieee->dev, "Setting key %d to all zero.\n",
425 					   key);
426 
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) {
436 			netdev_dbg(ieee->dev,
437 				   "Setting key %d as default Tx key.\n", key);
438 			ieee->crypt_info.tx_keyidx = key;
439 			sec.active_key = key;
440 			sec.flags |= SEC_ACTIVE_KEY;
441 		}
442 	}
443  done:
444 	ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
445 	ieee->auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
446 			  WLAN_AUTH_SHARED_KEY;
447 	sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
448 	sec.flags |= SEC_AUTH_MODE;
449 	netdev_dbg(ieee->dev, "Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
450 			   "OPEN" : "SHARED KEY");
451 
452 	/* For now we just support WEP, so only set that security level...
453 	 * TODO: When WPA is added this is one place that needs to change
454 	 */
455 	sec.flags |= SEC_LEVEL;
456 	sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
457 
458 	if (ieee->set_security)
459 		ieee->set_security(dev, &sec);
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
465 	 * the callbacks structures used to initialize the 802.11 stack.
466 	 */
467 	if (ieee->reset_on_keychange &&
468 	    ieee->iw_mode != IW_MODE_INFRA &&
469 	    ieee->reset_port && ieee->reset_port(dev)) {
470 		netdev_dbg(dev, "%s: reset_port failed\n", dev->name);
471 		return -EINVAL;
472 	}
473 	return 0;
474 }
475 EXPORT_SYMBOL(rtllib_wx_set_encode);
476 
477 int rtllib_wx_get_encode(struct rtllib_device *ieee,
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;
483 	struct lib80211_crypt_data *crypt;
484 
485 	netdev_dbg(ieee->dev, "%s()\n", __func__);
486 
487 	if (ieee->iw_mode == IW_MODE_MONITOR)
488 		return -1;
489 
490 	key = erq->flags & IW_ENCODE_INDEX;
491 	if (key) {
492 		if (key > NUM_WEP_KEYS)
493 			return -EINVAL;
494 		key--;
495 	} else {
496 		key = ieee->crypt_info.tx_keyidx;
497 	}
498 	crypt = ieee->crypt_info.crypt[key];
499 
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 	}
507 	len = crypt->ops->get_key(keybuf, SCM_KEY_LEN, NULL, crypt->priv);
508 
509 	erq->length = max(len, 0);
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 }
520 EXPORT_SYMBOL(rtllib_wx_get_encode);
521 
522 int rtllib_wx_set_encode_ext(struct rtllib_device *ieee,
523 			       struct iw_request_info *info,
524 			       union iwreq_data *wrqu, char *extra)
525 {
526 	int ret = 0;
527 	struct net_device *dev = ieee->dev;
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;
533 	struct lib80211_crypto_ops *ops;
534 	struct lib80211_crypt_data **crypt;
535 
536 	struct rtllib_security sec = {
537 		.flags = 0,
538 	};
539 	idx = encoding->flags & IW_ENCODE_INDEX;
540 	if (idx) {
541 		if (idx < 1 || idx > NUM_WEP_KEYS)
542 			return -EINVAL;
543 		idx--;
544 	} else{
545 			idx = ieee->crypt_info.tx_keyidx;
546 	}
547 	if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
548 		crypt = &ieee->crypt_info.crypt[idx];
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)
553 			return -EINVAL;
554 		if (ieee->iw_mode == IW_MODE_INFRA)
555 			crypt = &ieee->crypt_info.crypt[idx];
556 		else
557 			return -EINVAL;
558 	}
559 
560 	sec.flags |= SEC_ENABLED;
561 	if ((encoding->flags & IW_ENCODE_DISABLED) ||
562 	    ext->alg == IW_ENCODE_ALG_NONE) {
563 		if (*crypt)
564 			lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
565 
566 		for (i = 0; i < NUM_WEP_KEYS; i++) {
567 			if (ieee->crypt_info.crypt[i] != NULL)
568 				break;
569 		}
570 		if (i == NUM_WEP_KEYS) {
571 			sec.enabled = 0;
572 			sec.level = SEC_LEVEL_0;
573 			sec.flags |= SEC_LEVEL;
574 		}
575 		goto done;
576 	}
577 
578 	sec.enabled = 1;
579 	switch (ext->alg) {
580 	case IW_ENCODE_ALG_WEP:
581 		alg = "R-WEP";
582 		module = "rtllib_crypt_wep";
583 		break;
584 	case IW_ENCODE_ALG_TKIP:
585 		alg = "R-TKIP";
586 		module = "rtllib_crypt_tkip";
587 		break;
588 	case IW_ENCODE_ALG_CCMP:
589 		alg = "R-CCMP";
590 		module = "rtllib_crypt_ccmp";
591 		break;
592 	default:
593 		netdev_dbg(ieee->dev, "Unknown crypto alg %d\n", ext->alg);
594 		ret = -EINVAL;
595 		goto done;
596 	}
597 	netdev_dbg(dev, "alg name:%s\n", alg);
598 
599 	ops = lib80211_get_crypto_ops(alg);
600 	if (ops == NULL) {
601 		char tempbuf[100];
602 
603 		memset(tempbuf, 0x00, 100);
604 		sprintf(tempbuf, "%s", module);
605 		request_module("%s", tempbuf);
606 		ops = lib80211_get_crypto_ops(alg);
607 	}
608 	if (ops == NULL) {
609 		netdev_info(dev, "========>unknown crypto alg %d\n", ext->alg);
610 		ret = -EINVAL;
611 		goto done;
612 	}
613 
614 	if (*crypt == NULL || (*crypt)->ops != ops) {
615 		struct lib80211_crypt_data *new_crypt;
616 
617 		lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
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;
625 		if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
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) {
640 		netdev_info(dev, "key setting failed\n");
641 		ret = -EINVAL;
642 		goto done;
643 	}
644 	if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
645 		ieee->crypt_info.tx_keyidx = idx;
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 	}
666 done:
667 	if (ieee->set_security)
668 		ieee->set_security(ieee->dev, &sec);
669 
670 	if (ieee->reset_on_keychange &&
671 	    ieee->iw_mode != IW_MODE_INFRA &&
672 	    ieee->reset_port && ieee->reset_port(dev)) {
673 		netdev_dbg(ieee->dev, "Port reset failed\n");
674 		return -EINVAL;
675 	}
676 	return ret;
677 }
678 EXPORT_SYMBOL(rtllib_wx_set_encode_ext);
679 
680 int rtllib_wx_set_mlme(struct rtllib_device *ieee,
681 			       struct iw_request_info *info,
682 			       union iwreq_data *wrqu, char *extra)
683 {
684 	u8 i = 0;
685 	bool deauth = false;
686 	struct iw_mlme *mlme = (struct iw_mlme *) extra;
687 
688 	if (ieee->state != RTLLIB_LINKED)
689 		return -ENOLINK;
690 
691 	mutex_lock(&ieee->wx_mutex);
692 
693 	switch (mlme->cmd) {
694 	case IW_MLME_DEAUTH:
695 		deauth = true;
696 		/* fall through */
697 	case IW_MLME_DISASSOC:
698 		if (deauth)
699 			netdev_info(ieee->dev, "disauth packet !\n");
700 		else
701 			netdev_info(ieee->dev, "dis associate packet!\n");
702 
703 		ieee->cannot_notify = true;
704 
705 		SendDisassociation(ieee, deauth, mlme->reason_code);
706 		rtllib_disassociate(ieee);
707 
708 		ieee->wap_set = 0;
709 		for (i = 0; i < 6; i++)
710 			ieee->current_network.bssid[i] = 0x55;
711 
712 		ieee->ssid_set = 0;
713 		ieee->current_network.ssid[0] = '\0';
714 		ieee->current_network.ssid_len = 0;
715 		break;
716 	default:
717 		mutex_unlock(&ieee->wx_mutex);
718 		return -EOPNOTSUPP;
719 	}
720 
721 	mutex_unlock(&ieee->wx_mutex);
722 
723 	return 0;
724 }
725 EXPORT_SYMBOL(rtllib_wx_set_mlme);
726 
727 int rtllib_wx_set_auth(struct rtllib_device *ieee,
728 			       struct iw_request_info *info,
729 			       struct iw_param *data, char *extra)
730 {
731 	switch (data->flags & IW_AUTH_INDEX) {
732 	case IW_AUTH_WPA_VERSION:
733 		break;
734 	case IW_AUTH_CIPHER_PAIRWISE:
735 	case IW_AUTH_CIPHER_GROUP:
736 	case IW_AUTH_KEY_MGMT:
737 		/* Host AP driver does not use these parameters and allows
738 		 * wpa_supplicant to control them internally.
739 		 */
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;
746 		break;
747 
748 	case IW_AUTH_80211_AUTH_ALG:
749 		if (data->value & IW_AUTH_ALG_SHARED_KEY) {
750 			ieee->open_wep = 0;
751 			ieee->auth_mode = 1;
752 		} else if (data->value & IW_AUTH_ALG_OPEN_SYSTEM) {
753 			ieee->open_wep = 1;
754 			ieee->auth_mode = 0;
755 		} else if (data->value & IW_AUTH_ALG_LEAP) {
756 			ieee->open_wep = 1;
757 			ieee->auth_mode = 2;
758 		} else
759 			return -EINVAL;
760 		break;
761 
762 	case IW_AUTH_WPA_ENABLED:
763 		ieee->wpa_enabled = (data->value) ? 1 : 0;
764 		break;
765 
766 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
767 		ieee->ieee802_1x = data->value;
768 		break;
769 	case IW_AUTH_PRIVACY_INVOKED:
770 		ieee->privacy_invoked = data->value;
771 		break;
772 	default:
773 		return -EOPNOTSUPP;
774 	}
775 	return 0;
776 }
777 EXPORT_SYMBOL(rtllib_wx_set_auth);
778 
779 int rtllib_wx_set_gen_ie(struct rtllib_device *ieee, u8 *ie, size_t len)
780 {
781 	u8 *buf;
782 	u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
783 
784 	if (len > MAX_WPA_IE_LEN || (len && ie == NULL))
785 		return -EINVAL;
786 
787 	if (len) {
788 		eid = ie[0];
789 		if ((eid == MFIE_TYPE_GENERIC) && (!memcmp(&ie[2],
790 		     wps_oui, 4))) {
791 
792 			ieee->wps_ie_len = min_t(size_t, len, MAX_WZC_IE_LEN);
793 			buf = kmemdup(ie, ieee->wps_ie_len, GFP_KERNEL);
794 			if (buf == NULL)
795 				return -ENOMEM;
796 			ieee->wps_ie = buf;
797 			return 0;
798 		}
799 	}
800 	ieee->wps_ie_len = 0;
801 	kfree(ieee->wps_ie);
802 	ieee->wps_ie = NULL;
803 	if (len) {
804 		if (len != ie[1]+2)
805 			return -EINVAL;
806 		buf = kmemdup(ie, len, GFP_KERNEL);
807 		if (buf == NULL)
808 			return -ENOMEM;
809 		kfree(ieee->wpa_ie);
810 		ieee->wpa_ie = buf;
811 		ieee->wpa_ie_len = len;
812 	} else {
813 		kfree(ieee->wpa_ie);
814 		ieee->wpa_ie = NULL;
815 		ieee->wpa_ie_len = 0;
816 	}
817 	return 0;
818 }
819 EXPORT_SYMBOL(rtllib_wx_set_gen_ie);
820