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