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 		memset(&iwe, 0, sizeof(iwe));
191 		iwe.cmd = IWEVCUSTOM;
192 		iwe.u.data.length = strlen(buf);
193 		start = iwe_stream_add_point(info, start, stop, &iwe, buf);
194 	}
195 
196 	if (ieee->wpa_enabled && network->rsn_ie_len) {
197 		char buf[MAX_WPA_IE_LEN * 2 + 30];
198 
199 		u8 *p = buf;
200 		p += sprintf(p, "rsn_ie=");
201 		for (i = 0; i < network->rsn_ie_len; i++)
202 			p += sprintf(p, "%02x", network->rsn_ie[i]);
203 
204 		memset(&iwe, 0, sizeof(iwe));
205 		iwe.cmd = IWEVCUSTOM;
206 		iwe.u.data.length = strlen(buf);
207 		start = iwe_stream_add_point(info, start, stop, &iwe, buf);
208 	}
209 
210 
211 	/* Add EXTRA: Age to display seconds since last beacon/probe response
212 	 * for given network. */
213 	iwe.cmd = IWEVCUSTOM;
214 	p = custom;
215 	p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom),
216 		      " Last beacon: %lums ago", (jiffies - network->last_scanned) / (HZ / 100));
217 	iwe.u.data.length = p - custom;
218 	if (iwe.u.data.length)
219 		start = iwe_stream_add_point(info, start, stop, &iwe, custom);
220 
221 	return start;
222 }
223 
ieee80211_wx_get_scan(struct ieee80211_device * ieee,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)224 int ieee80211_wx_get_scan(struct ieee80211_device *ieee,
225 			  struct iw_request_info *info,
226 			  union iwreq_data *wrqu, char *extra)
227 {
228 	struct ieee80211_network *network;
229 	unsigned long flags;
230 
231 	char *ev = extra;
232 //	char *stop = ev + IW_SCAN_MAX_DATA;
233 	char *stop = ev + wrqu->data.length;//IW_SCAN_MAX_DATA;
234 	//char *stop = ev + IW_SCAN_MAX_DATA;
235 	int i = 0;
236 	int err = 0;
237 	IEEE80211_DEBUG_WX("Getting scan\n");
238 	mutex_lock(&ieee->wx_mutex);
239 	spin_lock_irqsave(&ieee->lock, flags);
240 
241 	list_for_each_entry(network, &ieee->network_list, list) {
242 		i++;
243 		if ((stop - ev) < 200) {
244 			err = -E2BIG;
245 			break;
246 		}
247 		if (ieee->scan_age == 0 ||
248 		    time_after(network->last_scanned + ieee->scan_age, jiffies))
249 			ev = rtl819x_translate_scan(ieee, ev, stop, network, info);
250 		else
251 			IEEE80211_DEBUG_SCAN(
252 				"Not showing network '%s ("
253 				"%pM)' due to age (%lums).\n",
254 				escape_essid(network->ssid,
255 					     network->ssid_len),
256 				network->bssid,
257 				(jiffies - network->last_scanned) / (HZ / 100));
258 	}
259 
260 	spin_unlock_irqrestore(&ieee->lock, flags);
261 	mutex_unlock(&ieee->wx_mutex);
262 	wrqu->data.length = ev -  extra;
263 	wrqu->data.flags = 0;
264 
265 	IEEE80211_DEBUG_WX("exit: %d networks returned.\n", i);
266 
267 	return err;
268 }
269 EXPORT_SYMBOL(ieee80211_wx_get_scan);
270 
ieee80211_wx_set_encode(struct ieee80211_device * ieee,struct iw_request_info * info,union iwreq_data * wrqu,char * keybuf)271 int ieee80211_wx_set_encode(struct ieee80211_device *ieee,
272 			    struct iw_request_info *info,
273 			    union iwreq_data *wrqu, char *keybuf)
274 {
275 	struct iw_point *erq = &(wrqu->encoding);
276 	struct net_device *dev = ieee->dev;
277 	struct ieee80211_security sec = {
278 		.flags = 0
279 	};
280 	int i, key, key_provided, len;
281 	struct ieee80211_crypt_data **crypt;
282 
283 	IEEE80211_DEBUG_WX("SET_ENCODE\n");
284 
285 	key = erq->flags & IW_ENCODE_INDEX;
286 	if (key) {
287 		if (key > WEP_KEYS)
288 			return -EINVAL;
289 		key--;
290 		key_provided = 1;
291 	} else {
292 		key_provided = 0;
293 		key = ieee->tx_keyidx;
294 	}
295 
296 	IEEE80211_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
297 			   "provided" : "default");
298 	crypt = &ieee->crypt[key];
299 
300 	if (erq->flags & IW_ENCODE_DISABLED) {
301 		if (key_provided && *crypt) {
302 			IEEE80211_DEBUG_WX("Disabling encryption on key %d.\n",
303 					   key);
304 			ieee80211_crypt_delayed_deinit(ieee, crypt);
305 		} else
306 			IEEE80211_DEBUG_WX("Disabling encryption.\n");
307 
308 		/* Check all the keys to see if any are still configured,
309 		 * and if no key index was provided, de-init them all */
310 		for (i = 0; i < WEP_KEYS; i++) {
311 			if (ieee->crypt[i]) {
312 				if (key_provided)
313 					break;
314 				ieee80211_crypt_delayed_deinit(
315 					ieee, &ieee->crypt[i]);
316 			}
317 		}
318 
319 		if (i == WEP_KEYS) {
320 			sec.enabled = 0;
321 			sec.level = SEC_LEVEL_0;
322 			sec.flags |= SEC_ENABLED | SEC_LEVEL;
323 		}
324 
325 		goto done;
326 	}
327 
328 
329 
330 	sec.enabled = 1;
331 	sec.flags |= SEC_ENABLED;
332 
333 	if (*crypt && (*crypt)->ops &&
334 	    strcmp((*crypt)->ops->name, "WEP") != 0) {
335 		/* changing to use WEP; deinit previously used algorithm
336 		 * on this key */
337 		ieee80211_crypt_delayed_deinit(ieee, crypt);
338 	}
339 
340 	if (!*crypt) {
341 		struct ieee80211_crypt_data *new_crypt;
342 
343 		/* take WEP into use */
344 		new_crypt = kzalloc(sizeof(struct ieee80211_crypt_data),
345 				    GFP_KERNEL);
346 		if (!new_crypt)
347 			return -ENOMEM;
348 		new_crypt->ops = try_then_request_module(ieee80211_get_crypto_ops("WEP"),
349 							 "ieee80211_crypt_wep");
350 		if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
351 			new_crypt->priv = new_crypt->ops->init(key);
352 
353 		if (!new_crypt->ops || !new_crypt->priv) {
354 			kfree(new_crypt);
355 			new_crypt = NULL;
356 
357 			netdev_warn(dev, "could not initialize WEP: "
358 				    "load module ieee80211_crypt_wep\n");
359 			return -EOPNOTSUPP;
360 		}
361 		*crypt = new_crypt;
362 	}
363 
364 	/* If a new key was provided, set it up */
365 	if (erq->length > 0) {
366 		len = erq->length <= 5 ? 5 : 13;
367 		memcpy(sec.keys[key], keybuf, erq->length);
368 		if (len > erq->length)
369 			memset(sec.keys[key] + erq->length, 0,
370 			       len - erq->length);
371 		IEEE80211_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
372 				   key, escape_essid(sec.keys[key], len),
373 				   erq->length, len);
374 		sec.key_sizes[key] = len;
375 		(*crypt)->ops->set_key(sec.keys[key], len, NULL,
376 				       (*crypt)->priv);
377 		sec.flags |= BIT(key);
378 		/* This ensures a key will be activated if no key is
379 		 * explicitly set
380 		 */
381 		if (key == sec.active_key)
382 			sec.flags |= SEC_ACTIVE_KEY;
383 		ieee->tx_keyidx = key;
384 
385 	} else {
386 		len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
387 					     NULL, (*crypt)->priv);
388 		if (len == 0) {
389 			/* Set a default key of all 0 */
390 			printk("Setting key %d to all zero.\n",
391 					   key);
392 
393 			IEEE80211_DEBUG_WX("Setting key %d to all zero.\n",
394 					   key);
395 			memset(sec.keys[key], 0, 13);
396 			(*crypt)->ops->set_key(sec.keys[key], 13, NULL,
397 					       (*crypt)->priv);
398 			sec.key_sizes[key] = 13;
399 			sec.flags |= BIT(key);
400 		}
401 
402 		/* No key data - just set the default TX key index */
403 		if (key_provided) {
404 			IEEE80211_DEBUG_WX(
405 				"Setting key %d to default Tx key.\n", key);
406 			ieee->tx_keyidx = key;
407 			sec.active_key = key;
408 			sec.flags |= SEC_ACTIVE_KEY;
409 		}
410 	}
411 
412  done:
413 	ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
414 	ieee->auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
415 	sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
416 	sec.flags |= SEC_AUTH_MODE;
417 	IEEE80211_DEBUG_WX("Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
418 			   "OPEN" : "SHARED KEY");
419 
420 	/* For now we just support WEP, so only set that security level...
421 	 * TODO: When WPA is added this is one place that needs to change */
422 	sec.flags |= SEC_LEVEL;
423 	sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
424 
425 	if (ieee->set_security)
426 		ieee->set_security(dev, &sec);
427 
428 	/* Do not reset port if card is in Managed mode since resetting will
429 	 * generate new IEEE 802.11 authentication which may end up in looping
430 	 * with IEEE 802.1X.  If your hardware requires a reset after WEP
431 	 * configuration (for example... Prism2), implement the reset_port in
432 	 * the callbacks structures used to initialize the 802.11 stack. */
433 	if (ieee->reset_on_keychange &&
434 	    ieee->iw_mode != IW_MODE_INFRA &&
435 	    ieee->reset_port && ieee->reset_port(dev)) {
436 		netdev_dbg(ieee->dev, "reset_port failed\n");
437 		return -EINVAL;
438 	}
439 	return 0;
440 }
441 EXPORT_SYMBOL(ieee80211_wx_set_encode);
442 
ieee80211_wx_get_encode(struct ieee80211_device * ieee,struct iw_request_info * info,union iwreq_data * wrqu,char * keybuf)443 int ieee80211_wx_get_encode(struct ieee80211_device *ieee,
444 			    struct iw_request_info *info,
445 			    union iwreq_data *wrqu, char *keybuf)
446 {
447 	struct iw_point *erq = &(wrqu->encoding);
448 	int len, key;
449 	struct ieee80211_crypt_data *crypt;
450 
451 	IEEE80211_DEBUG_WX("GET_ENCODE\n");
452 
453 	if (ieee->iw_mode == IW_MODE_MONITOR)
454 		return -1;
455 
456 	key = erq->flags & IW_ENCODE_INDEX;
457 	if (key) {
458 		if (key > WEP_KEYS)
459 			return -EINVAL;
460 		key--;
461 	} else
462 		key = ieee->tx_keyidx;
463 
464 	crypt = ieee->crypt[key];
465 	erq->flags = key + 1;
466 
467 	if (!crypt || !crypt->ops) {
468 		erq->length = 0;
469 		erq->flags |= IW_ENCODE_DISABLED;
470 		return 0;
471 	}
472 	len = crypt->ops->get_key(keybuf, SCM_KEY_LEN, NULL, crypt->priv);
473 	if (len < 0)
474 		len = 0;
475 	erq->length = len;
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") == 0)
692 			ext->alg = IW_ENCODE_ALG_TKIP;
693 		else if (strcmp(crypt->ops->name, "CCMP") == 0)
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