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