xref: /linux/drivers/net/wireless/intel/ipw2x00/libipw_wx.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 
4   Copyright(c) 2004-2005 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   <j@w1.fi>
10   Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
11 
12 
13   Contact Information:
14   Intel Linux Wireless <ilw@linux.intel.com>
15   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
16 
17 ******************************************************************************/
18 
19 #include <linux/hardirq.h>
20 #include <linux/kmod.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/jiffies.h>
24 #include <linux/wireless.h>
25 #include "libipw.h"
26 
27 static const char *libipw_modes[] = {
28 	"?", "a", "b", "ab", "g", "ag", "bg", "abg"
29 };
30 
31 static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
32 {
33 	unsigned long end = jiffies;
34 
35 	if (end >= start)
36 		return jiffies_to_msecs(end - start);
37 
38 	return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
39 }
40 
41 #define MAX_CUSTOM_LEN 64
42 static char *libipw_translate_scan(struct libipw_device *ieee,
43 				      char *start, char *stop,
44 				      struct libipw_network *network,
45 				      struct iw_request_info *info)
46 {
47 	char custom[MAX_CUSTOM_LEN];
48 	char *p;
49 	struct iw_event iwe;
50 	int i, j;
51 	char *current_val;	/* For rates */
52 	u8 rate;
53 
54 	/* First entry *MUST* be the AP MAC address */
55 	iwe.cmd = SIOCGIWAP;
56 	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
57 	memcpy(iwe.u.ap_addr.sa_data, network->bssid, ETH_ALEN);
58 	start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
59 
60 	/* Remaining entries will be displayed in the order we provide them */
61 
62 	/* Add the ESSID */
63 	iwe.cmd = SIOCGIWESSID;
64 	iwe.u.data.flags = 1;
65 	iwe.u.data.length = min(network->ssid_len, (u8) 32);
66 	start = iwe_stream_add_point(info, start, stop,
67 				     &iwe, network->ssid);
68 
69 	/* Add the protocol name */
70 	iwe.cmd = SIOCGIWNAME;
71 	snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11%s",
72 		 libipw_modes[network->mode]);
73 	start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_CHAR_LEN);
74 
75 	/* Add mode */
76 	iwe.cmd = SIOCGIWMODE;
77 	if (network->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
78 		if (network->capability & WLAN_CAPABILITY_ESS)
79 			iwe.u.mode = IW_MODE_MASTER;
80 		else
81 			iwe.u.mode = IW_MODE_ADHOC;
82 
83 		start = iwe_stream_add_event(info, start, stop,
84 					     &iwe, IW_EV_UINT_LEN);
85 	}
86 
87 	/* Add channel and frequency */
88 	/* Note : userspace automatically computes channel using iwrange */
89 	iwe.cmd = SIOCGIWFREQ;
90 	iwe.u.freq.m = libipw_channel_to_freq(ieee, network->channel);
91 	iwe.u.freq.e = 6;
92 	iwe.u.freq.i = 0;
93 	start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
94 
95 	/* Add encryption capability */
96 	iwe.cmd = SIOCGIWENCODE;
97 	if (network->capability & WLAN_CAPABILITY_PRIVACY)
98 		iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
99 	else
100 		iwe.u.data.flags = IW_ENCODE_DISABLED;
101 	iwe.u.data.length = 0;
102 	start = iwe_stream_add_point(info, start, stop,
103 				     &iwe, network->ssid);
104 
105 	/* Add basic and extended rates */
106 	/* Rate : stuffing multiple values in a single event require a bit
107 	 * more of magic - Jean II */
108 	current_val = start + iwe_stream_lcp_len(info);
109 	iwe.cmd = SIOCGIWRATE;
110 	/* Those two flags are ignored... */
111 	iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
112 
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 		/* Bit rate given in 500 kb/s units (+ 0x80) */
121 		iwe.u.bitrate.value = ((rate & 0x7f) * 500000);
122 		/* Add new value to event */
123 		current_val = iwe_stream_add_value(info, start, current_val,
124 						   stop, &iwe, IW_EV_PARAM_LEN);
125 	}
126 	for (; j < network->rates_ex_len; j++) {
127 		rate = network->rates_ex[j] & 0x7F;
128 		/* Bit rate given in 500 kb/s units (+ 0x80) */
129 		iwe.u.bitrate.value = ((rate & 0x7f) * 500000);
130 		/* Add new value to event */
131 		current_val = iwe_stream_add_value(info, start, current_val,
132 						   stop, &iwe, IW_EV_PARAM_LEN);
133 	}
134 	/* Check if we added any rate */
135 	if ((current_val - start) > iwe_stream_lcp_len(info))
136 		start = current_val;
137 
138 	/* Add quality statistics */
139 	iwe.cmd = IWEVQUAL;
140 	iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
141 	    IW_QUAL_NOISE_UPDATED;
142 
143 	if (!(network->stats.mask & LIBIPW_STATMASK_RSSI)) {
144 		iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID |
145 		    IW_QUAL_LEVEL_INVALID;
146 		iwe.u.qual.qual = 0;
147 	} else {
148 		if (ieee->perfect_rssi == ieee->worst_rssi)
149 			iwe.u.qual.qual = 100;
150 		else
151 			iwe.u.qual.qual =
152 			    (100 *
153 			     (ieee->perfect_rssi - ieee->worst_rssi) *
154 			     (ieee->perfect_rssi - ieee->worst_rssi) -
155 			     (ieee->perfect_rssi - network->stats.rssi) *
156 			     (15 * (ieee->perfect_rssi - ieee->worst_rssi) +
157 			      62 * (ieee->perfect_rssi -
158 				    network->stats.rssi))) /
159 			    ((ieee->perfect_rssi -
160 			      ieee->worst_rssi) * (ieee->perfect_rssi -
161 						   ieee->worst_rssi));
162 		if (iwe.u.qual.qual > 100)
163 			iwe.u.qual.qual = 100;
164 		else if (iwe.u.qual.qual < 1)
165 			iwe.u.qual.qual = 0;
166 	}
167 
168 	if (!(network->stats.mask & LIBIPW_STATMASK_NOISE)) {
169 		iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
170 		iwe.u.qual.noise = 0;
171 	} else {
172 		iwe.u.qual.noise = network->stats.noise;
173 	}
174 
175 	if (!(network->stats.mask & LIBIPW_STATMASK_SIGNAL)) {
176 		iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
177 		iwe.u.qual.level = 0;
178 	} else {
179 		iwe.u.qual.level = network->stats.signal;
180 	}
181 
182 	start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
183 
184 	iwe.cmd = IWEVCUSTOM;
185 	p = custom;
186 
187 	iwe.u.data.length = p - custom;
188 	if (iwe.u.data.length)
189 		start = iwe_stream_add_point(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 		memcpy(buf, network->wpa_ie, network->wpa_ie_len);
195 		iwe.cmd = IWEVGENIE;
196 		iwe.u.data.length = network->wpa_ie_len;
197 		start = iwe_stream_add_point(info, start, stop, &iwe, buf);
198 	}
199 
200 	memset(&iwe, 0, sizeof(iwe));
201 	if (network->rsn_ie_len) {
202 		char buf[MAX_WPA_IE_LEN];
203 		memcpy(buf, network->rsn_ie, network->rsn_ie_len);
204 		iwe.cmd = IWEVGENIE;
205 		iwe.u.data.length = network->rsn_ie_len;
206 		start = iwe_stream_add_point(info, start, stop, &iwe, buf);
207 	}
208 
209 	/* Add EXTRA: Age to display seconds since last beacon/probe response
210 	 * for given network. */
211 	iwe.cmd = IWEVCUSTOM;
212 	p = custom;
213 	p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom),
214 		      " Last beacon: %ums ago",
215 		      elapsed_jiffies_msecs(network->last_scanned));
216 	iwe.u.data.length = p - custom;
217 	if (iwe.u.data.length)
218 		start = iwe_stream_add_point(info, start, stop, &iwe, custom);
219 
220 	/* Add spectrum management information */
221 	iwe.cmd = -1;
222 	p = custom;
223 	p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom), " Channel flags: ");
224 
225 	if (libipw_get_channel_flags(ieee, network->channel) &
226 	    LIBIPW_CH_INVALID) {
227 		iwe.cmd = IWEVCUSTOM;
228 		p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom), "INVALID ");
229 	}
230 
231 	if (libipw_get_channel_flags(ieee, network->channel) &
232 	    LIBIPW_CH_RADAR_DETECT) {
233 		iwe.cmd = IWEVCUSTOM;
234 		p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom), "DFS ");
235 	}
236 
237 	if (iwe.cmd == IWEVCUSTOM) {
238 		iwe.u.data.length = p - custom;
239 		start = iwe_stream_add_point(info, start, stop, &iwe, custom);
240 	}
241 
242 	return start;
243 }
244 
245 #define SCAN_ITEM_SIZE 128
246 
247 int libipw_wx_get_scan(struct libipw_device *ieee,
248 			  struct iw_request_info *info,
249 			  union iwreq_data *wrqu, char *extra)
250 {
251 	struct libipw_network *network;
252 	unsigned long flags;
253 	int err = 0;
254 
255 	char *ev = extra;
256 	char *stop = ev + wrqu->data.length;
257 	int i = 0;
258 
259 	LIBIPW_DEBUG_WX("Getting scan\n");
260 
261 	spin_lock_irqsave(&ieee->lock, flags);
262 
263 	list_for_each_entry(network, &ieee->network_list, list) {
264 		i++;
265 		if (stop - ev < SCAN_ITEM_SIZE) {
266 			err = -E2BIG;
267 			break;
268 		}
269 
270 		if (ieee->scan_age == 0 ||
271 		    time_after(network->last_scanned + ieee->scan_age, jiffies))
272 			ev = libipw_translate_scan(ieee, ev, stop, network,
273 						      info);
274 		else {
275 			LIBIPW_DEBUG_SCAN("Not showing network '%*pE (%pM)' due to age (%ums).\n",
276 					  network->ssid_len, network->ssid,
277 					  network->bssid,
278 					  elapsed_jiffies_msecs(
279 					               network->last_scanned));
280 		}
281 	}
282 
283 	spin_unlock_irqrestore(&ieee->lock, flags);
284 
285 	wrqu->data.length = ev - extra;
286 	wrqu->data.flags = 0;
287 
288 	LIBIPW_DEBUG_WX("exit: %d networks returned.\n", i);
289 
290 	return err;
291 }
292 
293 int libipw_wx_set_encode(struct libipw_device *ieee,
294 			    struct iw_request_info *info,
295 			    union iwreq_data *wrqu, char *keybuf)
296 {
297 	struct iw_point *erq = &(wrqu->encoding);
298 	struct net_device *dev = ieee->dev;
299 	struct libipw_security sec = {
300 		.flags = 0
301 	};
302 	int i, key, key_provided, len;
303 	struct libipw_crypt_data **crypt;
304 	int host_crypto = ieee->host_encrypt || ieee->host_decrypt;
305 
306 	LIBIPW_DEBUG_WX("SET_ENCODE\n");
307 
308 	key = erq->flags & IW_ENCODE_INDEX;
309 	if (key) {
310 		if (key > WEP_KEYS)
311 			return -EINVAL;
312 		key--;
313 		key_provided = 1;
314 	} else {
315 		key_provided = 0;
316 		key = ieee->crypt_info.tx_keyidx;
317 	}
318 
319 	LIBIPW_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
320 			   "provided" : "default");
321 
322 	crypt = &ieee->crypt_info.crypt[key];
323 
324 	if (erq->flags & IW_ENCODE_DISABLED) {
325 		if (key_provided && *crypt) {
326 			LIBIPW_DEBUG_WX("Disabling encryption on key %d.\n",
327 					   key);
328 			libipw_crypt_delayed_deinit(&ieee->crypt_info, crypt);
329 		} else
330 			LIBIPW_DEBUG_WX("Disabling encryption.\n");
331 
332 		/* Check all the keys to see if any are still configured,
333 		 * and if no key index was provided, de-init them all */
334 		for (i = 0; i < WEP_KEYS; i++) {
335 			if (ieee->crypt_info.crypt[i] != NULL) {
336 				if (key_provided)
337 					break;
338 				libipw_crypt_delayed_deinit(&ieee->crypt_info,
339 							       &ieee->crypt_info.crypt[i]);
340 			}
341 		}
342 
343 		if (i == WEP_KEYS) {
344 			sec.enabled = 0;
345 			sec.encrypt = 0;
346 			sec.level = SEC_LEVEL_0;
347 			sec.flags |= SEC_ENABLED | SEC_LEVEL | SEC_ENCRYPT;
348 		}
349 
350 		goto done;
351 	}
352 
353 	sec.enabled = 1;
354 	sec.encrypt = 1;
355 	sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
356 
357 	if (*crypt != NULL && (*crypt)->ops != NULL &&
358 	    strcmp((*crypt)->ops->name, "WEP") != 0) {
359 		/* changing to use WEP; deinit previously used algorithm
360 		 * on this key */
361 		libipw_crypt_delayed_deinit(&ieee->crypt_info, crypt);
362 	}
363 
364 	if (*crypt == NULL && host_crypto) {
365 		struct libipw_crypt_data *new_crypt;
366 
367 		/* take WEP into use */
368 		new_crypt = kzalloc_obj(struct libipw_crypt_data);
369 		if (new_crypt == NULL)
370 			return -ENOMEM;
371 		new_crypt->ops = libipw_get_crypto_ops("WEP");
372 		if (!new_crypt->ops) {
373 			request_module("libipw_crypt_wep");
374 			new_crypt->ops = libipw_get_crypto_ops("WEP");
375 		}
376 
377 		if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
378 			new_crypt->priv = new_crypt->ops->init(key);
379 
380 		if (!new_crypt->ops || !new_crypt->priv) {
381 			kfree(new_crypt);
382 			new_crypt = NULL;
383 
384 			printk(KERN_WARNING "%s: could not initialize WEP: "
385 			       "load module libipw_crypt_wep\n", dev->name);
386 			return -EOPNOTSUPP;
387 		}
388 		*crypt = new_crypt;
389 	}
390 
391 	/* If a new key was provided, set it up */
392 	if (erq->length > 0) {
393 		len = erq->length <= 5 ? 5 : 13;
394 		memcpy(sec.keys[key], keybuf, erq->length);
395 		if (len > erq->length)
396 			memset(sec.keys[key] + erq->length, 0,
397 			       len - erq->length);
398 		LIBIPW_DEBUG_WX("Setting key %d to '%*pE' (%d:%d bytes)\n",
399 				   key, len, sec.keys[key],
400 				   erq->length, len);
401 		sec.key_sizes[key] = len;
402 		if (*crypt)
403 			(*crypt)->ops->set_key(sec.keys[key], len, NULL,
404 					       (*crypt)->priv);
405 		sec.flags |= (1 << key);
406 		/* This ensures a key will be activated if no key is
407 		 * explicitly set */
408 		if (key == sec.active_key)
409 			sec.flags |= SEC_ACTIVE_KEY;
410 
411 	} else {
412 		if (host_crypto) {
413 			len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
414 						     NULL, (*crypt)->priv);
415 			if (len == 0) {
416 				/* Set a default key of all 0 */
417 				LIBIPW_DEBUG_WX("Setting key %d to all "
418 						   "zero.\n", key);
419 				memset(sec.keys[key], 0, 13);
420 				(*crypt)->ops->set_key(sec.keys[key], 13, NULL,
421 						       (*crypt)->priv);
422 				sec.key_sizes[key] = 13;
423 				sec.flags |= (1 << key);
424 			}
425 		}
426 		/* No key data - just set the default TX key index */
427 		if (key_provided) {
428 			LIBIPW_DEBUG_WX("Setting key %d to default Tx "
429 					   "key.\n", key);
430 			ieee->crypt_info.tx_keyidx = key;
431 			sec.active_key = key;
432 			sec.flags |= SEC_ACTIVE_KEY;
433 		}
434 	}
435 	if (erq->flags & (IW_ENCODE_OPEN | IW_ENCODE_RESTRICTED)) {
436 		ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
437 		sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
438 		    WLAN_AUTH_SHARED_KEY;
439 		sec.flags |= SEC_AUTH_MODE;
440 		LIBIPW_DEBUG_WX("Auth: %s\n",
441 				   sec.auth_mode == WLAN_AUTH_OPEN ?
442 				   "OPEN" : "SHARED KEY");
443 	}
444 
445 	/* For now we just support WEP, so only set that security level...
446 	 * TODO: When WPA is added this is one place that needs to change */
447 	sec.flags |= SEC_LEVEL;
448 	sec.level = SEC_LEVEL_1;	/* 40 and 104 bit WEP */
449 	sec.encode_alg[key] = SEC_ALG_WEP;
450 
451       done:
452 	if (ieee->set_security)
453 		ieee->set_security(dev, &sec);
454 
455 	return 0;
456 }
457 
458 int libipw_wx_get_encode(struct libipw_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 libipw_security *sec = &ieee->sec;
465 
466 	LIBIPW_DEBUG_WX("GET_ENCODE\n");
467 
468 	key = erq->flags & IW_ENCODE_INDEX;
469 	if (key) {
470 		if (key > WEP_KEYS)
471 			return -EINVAL;
472 		key--;
473 	} else
474 		key = ieee->crypt_info.tx_keyidx;
475 
476 	erq->flags = key + 1;
477 
478 	if (!sec->enabled) {
479 		erq->length = 0;
480 		erq->flags |= IW_ENCODE_DISABLED;
481 		return 0;
482 	}
483 
484 	len = sec->key_sizes[key];
485 	memcpy(keybuf, sec->keys[key], len);
486 
487 	erq->length = len;
488 	erq->flags |= IW_ENCODE_ENABLED;
489 
490 	if (ieee->open_wep)
491 		erq->flags |= IW_ENCODE_OPEN;
492 	else
493 		erq->flags |= IW_ENCODE_RESTRICTED;
494 
495 	return 0;
496 }
497 
498 int libipw_wx_set_encodeext(struct libipw_device *ieee,
499 			       struct iw_request_info *info,
500 			       union iwreq_data *wrqu, char *extra)
501 {
502 	struct net_device *dev = ieee->dev;
503 	struct iw_point *encoding = &wrqu->encoding;
504 	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
505 	int i, idx, ret = 0;
506 	int group_key = 0;
507 	const char *alg, *module;
508 	const struct libipw_crypto_ops *ops;
509 	struct libipw_crypt_data **crypt;
510 
511 	struct libipw_security sec = {
512 		.flags = 0,
513 	};
514 
515 	idx = encoding->flags & IW_ENCODE_INDEX;
516 	if (idx) {
517 		if (idx < 1 || idx > WEP_KEYS)
518 			return -EINVAL;
519 		idx--;
520 	} else
521 		idx = ieee->crypt_info.tx_keyidx;
522 
523 	if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
524 		crypt = &ieee->crypt_info.crypt[idx];
525 		group_key = 1;
526 	} else {
527 		/* some Cisco APs use idx>0 for unicast in dynamic WEP */
528 		if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
529 			return -EINVAL;
530 		if (ieee->iw_mode == IW_MODE_INFRA)
531 			crypt = &ieee->crypt_info.crypt[idx];
532 		else
533 			return -EINVAL;
534 	}
535 
536 	sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
537 	if ((encoding->flags & IW_ENCODE_DISABLED) ||
538 	    ext->alg == IW_ENCODE_ALG_NONE) {
539 		if (*crypt)
540 			libipw_crypt_delayed_deinit(&ieee->crypt_info, crypt);
541 
542 		for (i = 0; i < WEP_KEYS; i++)
543 			if (ieee->crypt_info.crypt[i] != NULL)
544 				break;
545 
546 		if (i == WEP_KEYS) {
547 			sec.enabled = 0;
548 			sec.encrypt = 0;
549 			sec.level = SEC_LEVEL_0;
550 			sec.flags |= SEC_LEVEL;
551 		}
552 		goto done;
553 	}
554 
555 	sec.enabled = 1;
556 	sec.encrypt = 1;
557 
558 	if (group_key ? !ieee->host_mc_decrypt :
559 	    !(ieee->host_encrypt || ieee->host_decrypt ||
560 	      ieee->host_encrypt_msdu))
561 		goto skip_host_crypt;
562 
563 	switch (ext->alg) {
564 	case IW_ENCODE_ALG_WEP:
565 		alg = "WEP";
566 		module = "libipw_crypt_wep";
567 		break;
568 	case IW_ENCODE_ALG_TKIP:
569 		alg = "TKIP";
570 		module = "libipw_crypt_tkip";
571 		break;
572 	case IW_ENCODE_ALG_CCMP:
573 		alg = "CCMP";
574 		module = "libipw_crypt_ccmp";
575 		break;
576 	default:
577 		LIBIPW_DEBUG_WX("%s: unknown crypto alg %d\n",
578 				   dev->name, ext->alg);
579 		ret = -EINVAL;
580 		goto done;
581 	}
582 
583 	ops = libipw_get_crypto_ops(alg);
584 	if (ops == NULL) {
585 		request_module(module);
586 		ops = libipw_get_crypto_ops(alg);
587 	}
588 	if (ops == NULL) {
589 		LIBIPW_DEBUG_WX("%s: unknown crypto alg %d\n",
590 				   dev->name, ext->alg);
591 		ret = -EINVAL;
592 		goto done;
593 	}
594 
595 	if (*crypt == NULL || (*crypt)->ops != ops) {
596 		struct libipw_crypt_data *new_crypt;
597 
598 		libipw_crypt_delayed_deinit(&ieee->crypt_info, crypt);
599 
600 		new_crypt = kzalloc_obj(*new_crypt);
601 		if (new_crypt == NULL) {
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 		if (new_crypt->priv == NULL) {
609 			kfree(new_crypt);
610 			ret = -EINVAL;
611 			goto done;
612 		}
613 		*crypt = new_crypt;
614 	}
615 
616 	if (ext->key_len > 0 && (*crypt)->ops->set_key &&
617 	    (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
618 				   (*crypt)->priv) < 0) {
619 		LIBIPW_DEBUG_WX("%s: key setting failed\n", dev->name);
620 		ret = -EINVAL;
621 		goto done;
622 	}
623 
624       skip_host_crypt:
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 
631 	if (ext->alg != IW_ENCODE_ALG_NONE) {
632 		int key_len = clamp_val(ext->key_len, 0, SCM_KEY_LEN);
633 
634 		memcpy(sec.keys[idx], ext->key, key_len);
635 		sec.key_sizes[idx] = key_len;
636 		sec.flags |= (1 << idx);
637 		if (ext->alg == IW_ENCODE_ALG_WEP) {
638 			sec.encode_alg[idx] = SEC_ALG_WEP;
639 			sec.flags |= SEC_LEVEL;
640 			sec.level = SEC_LEVEL_1;
641 		} else if (ext->alg == IW_ENCODE_ALG_TKIP) {
642 			sec.encode_alg[idx] = SEC_ALG_TKIP;
643 			sec.flags |= SEC_LEVEL;
644 			sec.level = SEC_LEVEL_2;
645 		} else if (ext->alg == IW_ENCODE_ALG_CCMP) {
646 			sec.encode_alg[idx] = SEC_ALG_CCMP;
647 			sec.flags |= SEC_LEVEL;
648 			sec.level = SEC_LEVEL_3;
649 		}
650 		/* Don't set sec level for group keys. */
651 		if (group_key)
652 			sec.flags &= ~SEC_LEVEL;
653 	}
654       done:
655 	if (ieee->set_security)
656 		ieee->set_security(dev, &sec);
657 
658 	return ret;
659 }
660 
661 int libipw_wx_get_encodeext(struct libipw_device *ieee,
662 			       struct iw_request_info *info,
663 			       union iwreq_data *wrqu, char *extra)
664 {
665 	struct iw_point *encoding = &wrqu->encoding;
666 	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
667 	struct libipw_security *sec = &ieee->sec;
668 	int idx, max_key_len;
669 
670 	max_key_len = encoding->length - sizeof(*ext);
671 	if (max_key_len < 0)
672 		return -EINVAL;
673 
674 	idx = encoding->flags & IW_ENCODE_INDEX;
675 	if (idx) {
676 		if (idx < 1 || idx > WEP_KEYS)
677 			return -EINVAL;
678 		idx--;
679 	} else
680 		idx = ieee->crypt_info.tx_keyidx;
681 
682 	if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) &&
683 	    ext->alg != IW_ENCODE_ALG_WEP)
684 		if (idx != 0 || ieee->iw_mode != IW_MODE_INFRA)
685 			return -EINVAL;
686 
687 	encoding->flags = idx + 1;
688 	memset(ext, 0, sizeof(*ext));
689 
690 	if (!sec->enabled) {
691 		ext->alg = IW_ENCODE_ALG_NONE;
692 		ext->key_len = 0;
693 		encoding->flags |= IW_ENCODE_DISABLED;
694 	} else {
695 		if (sec->encode_alg[idx] == SEC_ALG_WEP)
696 			ext->alg = IW_ENCODE_ALG_WEP;
697 		else if (sec->encode_alg[idx] == SEC_ALG_TKIP)
698 			ext->alg = IW_ENCODE_ALG_TKIP;
699 		else if (sec->encode_alg[idx] == SEC_ALG_CCMP)
700 			ext->alg = IW_ENCODE_ALG_CCMP;
701 		else
702 			return -EINVAL;
703 
704 		ext->key_len = sec->key_sizes[idx];
705 		memcpy(ext->key, sec->keys[idx], ext->key_len);
706 		encoding->flags |= IW_ENCODE_ENABLED;
707 		if (ext->key_len &&
708 		    (ext->alg == IW_ENCODE_ALG_TKIP ||
709 		     ext->alg == IW_ENCODE_ALG_CCMP))
710 			ext->ext_flags |= IW_ENCODE_EXT_TX_SEQ_VALID;
711 
712 	}
713 
714 	return 0;
715 }
716 
717 EXPORT_SYMBOL(libipw_wx_set_encodeext);
718 EXPORT_SYMBOL(libipw_wx_get_encodeext);
719 
720 EXPORT_SYMBOL(libipw_wx_get_scan);
721 EXPORT_SYMBOL(libipw_wx_set_encode);
722 EXPORT_SYMBOL(libipw_wx_get_encode);
723