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