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