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