xref: /openbmc/linux/net/wireless/wext-compat.c (revision 801543b2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cfg80211 - wext compat code
4  *
5  * This is temporary code until all wireless functionality is migrated
6  * into cfg80211, when that happens all the exports here go away and
7  * we directly assign the wireless handlers of wireless interfaces.
8  *
9  * Copyright 2008-2009	Johannes Berg <johannes@sipsolutions.net>
10  * Copyright (C) 2019-2022 Intel Corporation
11  */
12 
13 #include <linux/export.h>
14 #include <linux/wireless.h>
15 #include <linux/nl80211.h>
16 #include <linux/if_arp.h>
17 #include <linux/etherdevice.h>
18 #include <linux/slab.h>
19 #include <net/iw_handler.h>
20 #include <net/cfg80211.h>
21 #include <net/cfg80211-wext.h>
22 #include "wext-compat.h"
23 #include "core.h"
24 #include "rdev-ops.h"
25 
26 int cfg80211_wext_giwname(struct net_device *dev,
27 			  struct iw_request_info *info,
28 			  char *name, char *extra)
29 {
30 	strcpy(name, "IEEE 802.11");
31 	return 0;
32 }
33 EXPORT_WEXT_HANDLER(cfg80211_wext_giwname);
34 
35 int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
36 			  u32 *mode, char *extra)
37 {
38 	struct wireless_dev *wdev = dev->ieee80211_ptr;
39 	struct cfg80211_registered_device *rdev;
40 	struct vif_params vifparams;
41 	enum nl80211_iftype type;
42 	int ret;
43 
44 	rdev = wiphy_to_rdev(wdev->wiphy);
45 
46 	switch (*mode) {
47 	case IW_MODE_INFRA:
48 		type = NL80211_IFTYPE_STATION;
49 		break;
50 	case IW_MODE_ADHOC:
51 		type = NL80211_IFTYPE_ADHOC;
52 		break;
53 	case IW_MODE_MONITOR:
54 		type = NL80211_IFTYPE_MONITOR;
55 		break;
56 	default:
57 		return -EINVAL;
58 	}
59 
60 	if (type == wdev->iftype)
61 		return 0;
62 
63 	memset(&vifparams, 0, sizeof(vifparams));
64 
65 	wiphy_lock(wdev->wiphy);
66 	ret = cfg80211_change_iface(rdev, dev, type, &vifparams);
67 	wiphy_unlock(wdev->wiphy);
68 
69 	return ret;
70 }
71 EXPORT_WEXT_HANDLER(cfg80211_wext_siwmode);
72 
73 int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
74 			  u32 *mode, char *extra)
75 {
76 	struct wireless_dev *wdev = dev->ieee80211_ptr;
77 
78 	if (!wdev)
79 		return -EOPNOTSUPP;
80 
81 	switch (wdev->iftype) {
82 	case NL80211_IFTYPE_AP:
83 		*mode = IW_MODE_MASTER;
84 		break;
85 	case NL80211_IFTYPE_STATION:
86 		*mode = IW_MODE_INFRA;
87 		break;
88 	case NL80211_IFTYPE_ADHOC:
89 		*mode = IW_MODE_ADHOC;
90 		break;
91 	case NL80211_IFTYPE_MONITOR:
92 		*mode = IW_MODE_MONITOR;
93 		break;
94 	case NL80211_IFTYPE_WDS:
95 		*mode = IW_MODE_REPEAT;
96 		break;
97 	case NL80211_IFTYPE_AP_VLAN:
98 		*mode = IW_MODE_SECOND;		/* FIXME */
99 		break;
100 	default:
101 		*mode = IW_MODE_AUTO;
102 		break;
103 	}
104 	return 0;
105 }
106 EXPORT_WEXT_HANDLER(cfg80211_wext_giwmode);
107 
108 
109 int cfg80211_wext_giwrange(struct net_device *dev,
110 			   struct iw_request_info *info,
111 			   struct iw_point *data, char *extra)
112 {
113 	struct wireless_dev *wdev = dev->ieee80211_ptr;
114 	struct iw_range *range = (struct iw_range *) extra;
115 	enum nl80211_band band;
116 	int i, c = 0;
117 
118 	if (!wdev)
119 		return -EOPNOTSUPP;
120 
121 	data->length = sizeof(struct iw_range);
122 	memset(range, 0, sizeof(struct iw_range));
123 
124 	range->we_version_compiled = WIRELESS_EXT;
125 	range->we_version_source = 21;
126 	range->retry_capa = IW_RETRY_LIMIT;
127 	range->retry_flags = IW_RETRY_LIMIT;
128 	range->min_retry = 0;
129 	range->max_retry = 255;
130 	range->min_rts = 0;
131 	range->max_rts = 2347;
132 	range->min_frag = 256;
133 	range->max_frag = 2346;
134 
135 	range->max_encoding_tokens = 4;
136 
137 	range->max_qual.updated = IW_QUAL_NOISE_INVALID;
138 
139 	switch (wdev->wiphy->signal_type) {
140 	case CFG80211_SIGNAL_TYPE_NONE:
141 		break;
142 	case CFG80211_SIGNAL_TYPE_MBM:
143 		range->max_qual.level = (u8)-110;
144 		range->max_qual.qual = 70;
145 		range->avg_qual.qual = 35;
146 		range->max_qual.updated |= IW_QUAL_DBM;
147 		range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
148 		range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
149 		break;
150 	case CFG80211_SIGNAL_TYPE_UNSPEC:
151 		range->max_qual.level = 100;
152 		range->max_qual.qual = 100;
153 		range->avg_qual.qual = 50;
154 		range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
155 		range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
156 		break;
157 	}
158 
159 	range->avg_qual.level = range->max_qual.level / 2;
160 	range->avg_qual.noise = range->max_qual.noise / 2;
161 	range->avg_qual.updated = range->max_qual.updated;
162 
163 	for (i = 0; i < wdev->wiphy->n_cipher_suites; i++) {
164 		switch (wdev->wiphy->cipher_suites[i]) {
165 		case WLAN_CIPHER_SUITE_TKIP:
166 			range->enc_capa |= (IW_ENC_CAPA_CIPHER_TKIP |
167 					    IW_ENC_CAPA_WPA);
168 			break;
169 
170 		case WLAN_CIPHER_SUITE_CCMP:
171 			range->enc_capa |= (IW_ENC_CAPA_CIPHER_CCMP |
172 					    IW_ENC_CAPA_WPA2);
173 			break;
174 
175 		case WLAN_CIPHER_SUITE_WEP40:
176 			range->encoding_size[range->num_encoding_sizes++] =
177 				WLAN_KEY_LEN_WEP40;
178 			break;
179 
180 		case WLAN_CIPHER_SUITE_WEP104:
181 			range->encoding_size[range->num_encoding_sizes++] =
182 				WLAN_KEY_LEN_WEP104;
183 			break;
184 		}
185 	}
186 
187 	for (band = 0; band < NUM_NL80211_BANDS; band ++) {
188 		struct ieee80211_supported_band *sband;
189 
190 		sband = wdev->wiphy->bands[band];
191 
192 		if (!sband)
193 			continue;
194 
195 		for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
196 			struct ieee80211_channel *chan = &sband->channels[i];
197 
198 			if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
199 				range->freq[c].i =
200 					ieee80211_frequency_to_channel(
201 						chan->center_freq);
202 				range->freq[c].m = chan->center_freq;
203 				range->freq[c].e = 6;
204 				c++;
205 			}
206 		}
207 	}
208 	range->num_channels = c;
209 	range->num_frequency = c;
210 
211 	IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
212 	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
213 	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
214 
215 	if (wdev->wiphy->max_scan_ssids > 0)
216 		range->scan_capa |= IW_SCAN_CAPA_ESSID;
217 
218 	return 0;
219 }
220 EXPORT_WEXT_HANDLER(cfg80211_wext_giwrange);
221 
222 
223 /**
224  * cfg80211_wext_freq - get wext frequency for non-"auto"
225  * @freq: the wext freq encoding
226  *
227  * Returns a frequency, or a negative error code, or 0 for auto.
228  */
229 int cfg80211_wext_freq(struct iw_freq *freq)
230 {
231 	/*
232 	 * Parse frequency - return 0 for auto and
233 	 * -EINVAL for impossible things.
234 	 */
235 	if (freq->e == 0) {
236 		enum nl80211_band band = NL80211_BAND_2GHZ;
237 		if (freq->m < 0)
238 			return 0;
239 		if (freq->m > 14)
240 			band = NL80211_BAND_5GHZ;
241 		return ieee80211_channel_to_frequency(freq->m, band);
242 	} else {
243 		int i, div = 1000000;
244 		for (i = 0; i < freq->e; i++)
245 			div /= 10;
246 		if (div <= 0)
247 			return -EINVAL;
248 		return freq->m / div;
249 	}
250 }
251 
252 int cfg80211_wext_siwrts(struct net_device *dev,
253 			 struct iw_request_info *info,
254 			 struct iw_param *rts, char *extra)
255 {
256 	struct wireless_dev *wdev = dev->ieee80211_ptr;
257 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
258 	u32 orts = wdev->wiphy->rts_threshold;
259 	int err;
260 
261 	wiphy_lock(&rdev->wiphy);
262 	if (rts->disabled || !rts->fixed) {
263 		wdev->wiphy->rts_threshold = (u32) -1;
264 	} else if (rts->value < 0) {
265 		err = -EINVAL;
266 		goto out;
267 	} else {
268 		wdev->wiphy->rts_threshold = rts->value;
269 	}
270 
271 	err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_RTS_THRESHOLD);
272 
273 	if (err)
274 		wdev->wiphy->rts_threshold = orts;
275 
276 out:
277 	wiphy_unlock(&rdev->wiphy);
278 	return err;
279 }
280 EXPORT_WEXT_HANDLER(cfg80211_wext_siwrts);
281 
282 int cfg80211_wext_giwrts(struct net_device *dev,
283 			 struct iw_request_info *info,
284 			 struct iw_param *rts, char *extra)
285 {
286 	struct wireless_dev *wdev = dev->ieee80211_ptr;
287 
288 	rts->value = wdev->wiphy->rts_threshold;
289 	rts->disabled = rts->value == (u32) -1;
290 	rts->fixed = 1;
291 
292 	return 0;
293 }
294 EXPORT_WEXT_HANDLER(cfg80211_wext_giwrts);
295 
296 int cfg80211_wext_siwfrag(struct net_device *dev,
297 			  struct iw_request_info *info,
298 			  struct iw_param *frag, char *extra)
299 {
300 	struct wireless_dev *wdev = dev->ieee80211_ptr;
301 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
302 	u32 ofrag = wdev->wiphy->frag_threshold;
303 	int err;
304 
305 	wiphy_lock(&rdev->wiphy);
306 	if (frag->disabled || !frag->fixed) {
307 		wdev->wiphy->frag_threshold = (u32) -1;
308 	} else if (frag->value < 256) {
309 		err = -EINVAL;
310 		goto out;
311 	} else {
312 		/* Fragment length must be even, so strip LSB. */
313 		wdev->wiphy->frag_threshold = frag->value & ~0x1;
314 	}
315 
316 	err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_FRAG_THRESHOLD);
317 	if (err)
318 		wdev->wiphy->frag_threshold = ofrag;
319 out:
320 	wiphy_unlock(&rdev->wiphy);
321 
322 	return err;
323 }
324 EXPORT_WEXT_HANDLER(cfg80211_wext_siwfrag);
325 
326 int cfg80211_wext_giwfrag(struct net_device *dev,
327 			  struct iw_request_info *info,
328 			  struct iw_param *frag, char *extra)
329 {
330 	struct wireless_dev *wdev = dev->ieee80211_ptr;
331 
332 	frag->value = wdev->wiphy->frag_threshold;
333 	frag->disabled = frag->value == (u32) -1;
334 	frag->fixed = 1;
335 
336 	return 0;
337 }
338 EXPORT_WEXT_HANDLER(cfg80211_wext_giwfrag);
339 
340 static int cfg80211_wext_siwretry(struct net_device *dev,
341 				  struct iw_request_info *info,
342 				  struct iw_param *retry, char *extra)
343 {
344 	struct wireless_dev *wdev = dev->ieee80211_ptr;
345 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
346 	u32 changed = 0;
347 	u8 olong = wdev->wiphy->retry_long;
348 	u8 oshort = wdev->wiphy->retry_short;
349 	int err;
350 
351 	if (retry->disabled || retry->value < 1 || retry->value > 255 ||
352 	    (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
353 		return -EINVAL;
354 
355 	wiphy_lock(&rdev->wiphy);
356 	if (retry->flags & IW_RETRY_LONG) {
357 		wdev->wiphy->retry_long = retry->value;
358 		changed |= WIPHY_PARAM_RETRY_LONG;
359 	} else if (retry->flags & IW_RETRY_SHORT) {
360 		wdev->wiphy->retry_short = retry->value;
361 		changed |= WIPHY_PARAM_RETRY_SHORT;
362 	} else {
363 		wdev->wiphy->retry_short = retry->value;
364 		wdev->wiphy->retry_long = retry->value;
365 		changed |= WIPHY_PARAM_RETRY_LONG;
366 		changed |= WIPHY_PARAM_RETRY_SHORT;
367 	}
368 
369 	err = rdev_set_wiphy_params(rdev, changed);
370 	if (err) {
371 		wdev->wiphy->retry_short = oshort;
372 		wdev->wiphy->retry_long = olong;
373 	}
374 	wiphy_unlock(&rdev->wiphy);
375 
376 	return err;
377 }
378 
379 int cfg80211_wext_giwretry(struct net_device *dev,
380 			   struct iw_request_info *info,
381 			   struct iw_param *retry, char *extra)
382 {
383 	struct wireless_dev *wdev = dev->ieee80211_ptr;
384 
385 	retry->disabled = 0;
386 
387 	if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
388 		/*
389 		 * First return short value, iwconfig will ask long value
390 		 * later if needed
391 		 */
392 		retry->flags |= IW_RETRY_LIMIT | IW_RETRY_SHORT;
393 		retry->value = wdev->wiphy->retry_short;
394 		if (wdev->wiphy->retry_long == wdev->wiphy->retry_short)
395 			retry->flags |= IW_RETRY_LONG;
396 
397 		return 0;
398 	}
399 
400 	if (retry->flags & IW_RETRY_LONG) {
401 		retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
402 		retry->value = wdev->wiphy->retry_long;
403 	}
404 
405 	return 0;
406 }
407 EXPORT_WEXT_HANDLER(cfg80211_wext_giwretry);
408 
409 static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
410 				     struct net_device *dev, bool pairwise,
411 				     const u8 *addr, bool remove, bool tx_key,
412 				     int idx, struct key_params *params)
413 {
414 	struct wireless_dev *wdev = dev->ieee80211_ptr;
415 	int err, i;
416 	bool rejoin = false;
417 
418 	if (wdev->valid_links)
419 		return -EINVAL;
420 
421 	if (pairwise && !addr)
422 		return -EINVAL;
423 
424 	/*
425 	 * In many cases we won't actually need this, but it's better
426 	 * to do it first in case the allocation fails. Don't use wext.
427 	 */
428 	if (!wdev->wext.keys) {
429 		wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
430 					  GFP_KERNEL);
431 		if (!wdev->wext.keys)
432 			return -ENOMEM;
433 		for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
434 			wdev->wext.keys->params[i].key =
435 				wdev->wext.keys->data[i];
436 	}
437 
438 	if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
439 	    wdev->iftype != NL80211_IFTYPE_STATION)
440 		return -EOPNOTSUPP;
441 
442 	if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
443 		if (!wdev->connected)
444 			return -ENOLINK;
445 
446 		if (!rdev->ops->set_default_mgmt_key)
447 			return -EOPNOTSUPP;
448 
449 		if (idx < 4 || idx > 5)
450 			return -EINVAL;
451 	} else if (idx < 0 || idx > 3)
452 		return -EINVAL;
453 
454 	if (remove) {
455 		err = 0;
456 		if (wdev->connected ||
457 		    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
458 		     wdev->u.ibss.current_bss)) {
459 			/*
460 			 * If removing the current TX key, we will need to
461 			 * join a new IBSS without the privacy bit clear.
462 			 */
463 			if (idx == wdev->wext.default_key &&
464 			    wdev->iftype == NL80211_IFTYPE_ADHOC) {
465 				__cfg80211_leave_ibss(rdev, wdev->netdev, true);
466 				rejoin = true;
467 			}
468 
469 			if (!pairwise && addr &&
470 			    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
471 				err = -ENOENT;
472 			else
473 				err = rdev_del_key(rdev, dev, idx, pairwise,
474 						   addr);
475 		}
476 		wdev->wext.connect.privacy = false;
477 		/*
478 		 * Applications using wireless extensions expect to be
479 		 * able to delete keys that don't exist, so allow that.
480 		 */
481 		if (err == -ENOENT)
482 			err = 0;
483 		if (!err) {
484 			if (!addr && idx < 4) {
485 				memset(wdev->wext.keys->data[idx], 0,
486 				       sizeof(wdev->wext.keys->data[idx]));
487 				wdev->wext.keys->params[idx].key_len = 0;
488 				wdev->wext.keys->params[idx].cipher = 0;
489 			}
490 			if (idx == wdev->wext.default_key)
491 				wdev->wext.default_key = -1;
492 			else if (idx == wdev->wext.default_mgmt_key)
493 				wdev->wext.default_mgmt_key = -1;
494 		}
495 
496 		if (!err && rejoin)
497 			err = cfg80211_ibss_wext_join(rdev, wdev);
498 
499 		return err;
500 	}
501 
502 	if (addr)
503 		tx_key = false;
504 
505 	if (cfg80211_validate_key_settings(rdev, params, idx, pairwise, addr))
506 		return -EINVAL;
507 
508 	err = 0;
509 	if (wdev->connected ||
510 	    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
511 	     wdev->u.ibss.current_bss))
512 		err = rdev_add_key(rdev, dev, idx, pairwise, addr, params);
513 	else if (params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
514 		 params->cipher != WLAN_CIPHER_SUITE_WEP104)
515 		return -EINVAL;
516 	if (err)
517 		return err;
518 
519 	/*
520 	 * We only need to store WEP keys, since they're the only keys that
521 	 * can be set before a connection is established and persist after
522 	 * disconnecting.
523 	 */
524 	if (!addr && (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
525 		      params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
526 		wdev->wext.keys->params[idx] = *params;
527 		memcpy(wdev->wext.keys->data[idx],
528 			params->key, params->key_len);
529 		wdev->wext.keys->params[idx].key =
530 			wdev->wext.keys->data[idx];
531 	}
532 
533 	if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
534 	     params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
535 	    (tx_key || (!addr && wdev->wext.default_key == -1))) {
536 		if (wdev->connected ||
537 		    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
538 		     wdev->u.ibss.current_bss)) {
539 			/*
540 			 * If we are getting a new TX key from not having
541 			 * had one before we need to join a new IBSS with
542 			 * the privacy bit set.
543 			 */
544 			if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
545 			    wdev->wext.default_key == -1) {
546 				__cfg80211_leave_ibss(rdev, wdev->netdev, true);
547 				rejoin = true;
548 			}
549 			err = rdev_set_default_key(rdev, dev, idx, true, true);
550 		}
551 		if (!err) {
552 			wdev->wext.default_key = idx;
553 			if (rejoin)
554 				err = cfg80211_ibss_wext_join(rdev, wdev);
555 		}
556 		return err;
557 	}
558 
559 	if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
560 	    (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
561 		if (wdev->connected ||
562 		    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
563 		     wdev->u.ibss.current_bss))
564 			err = rdev_set_default_mgmt_key(rdev, dev, idx);
565 		if (!err)
566 			wdev->wext.default_mgmt_key = idx;
567 		return err;
568 	}
569 
570 	return 0;
571 }
572 
573 static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
574 				   struct net_device *dev, bool pairwise,
575 				   const u8 *addr, bool remove, bool tx_key,
576 				   int idx, struct key_params *params)
577 {
578 	int err;
579 
580 	wdev_lock(dev->ieee80211_ptr);
581 	err = __cfg80211_set_encryption(rdev, dev, pairwise, addr,
582 					remove, tx_key, idx, params);
583 	wdev_unlock(dev->ieee80211_ptr);
584 
585 	return err;
586 }
587 
588 static int cfg80211_wext_siwencode(struct net_device *dev,
589 				   struct iw_request_info *info,
590 				   struct iw_point *erq, char *keybuf)
591 {
592 	struct wireless_dev *wdev = dev->ieee80211_ptr;
593 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
594 	int idx, err;
595 	bool remove = false;
596 	struct key_params params;
597 
598 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
599 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
600 		return -EOPNOTSUPP;
601 
602 	/* no use -- only MFP (set_default_mgmt_key) is optional */
603 	if (!rdev->ops->del_key ||
604 	    !rdev->ops->add_key ||
605 	    !rdev->ops->set_default_key)
606 		return -EOPNOTSUPP;
607 
608 	wiphy_lock(&rdev->wiphy);
609 	if (wdev->valid_links) {
610 		err = -EOPNOTSUPP;
611 		goto out;
612 	}
613 
614 	idx = erq->flags & IW_ENCODE_INDEX;
615 	if (idx == 0) {
616 		idx = wdev->wext.default_key;
617 		if (idx < 0)
618 			idx = 0;
619 	} else if (idx < 1 || idx > 4) {
620 		err = -EINVAL;
621 		goto out;
622 	} else {
623 		idx--;
624 	}
625 
626 	if (erq->flags & IW_ENCODE_DISABLED)
627 		remove = true;
628 	else if (erq->length == 0) {
629 		/* No key data - just set the default TX key index */
630 		err = 0;
631 		wdev_lock(wdev);
632 		if (wdev->connected ||
633 		    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
634 		     wdev->u.ibss.current_bss))
635 			err = rdev_set_default_key(rdev, dev, idx, true,
636 						   true);
637 		if (!err)
638 			wdev->wext.default_key = idx;
639 		wdev_unlock(wdev);
640 		goto out;
641 	}
642 
643 	memset(&params, 0, sizeof(params));
644 	params.key = keybuf;
645 	params.key_len = erq->length;
646 	if (erq->length == 5) {
647 		params.cipher = WLAN_CIPHER_SUITE_WEP40;
648 	} else if (erq->length == 13) {
649 		params.cipher = WLAN_CIPHER_SUITE_WEP104;
650 	} else if (!remove) {
651 		err = -EINVAL;
652 		goto out;
653 	}
654 
655 	err = cfg80211_set_encryption(rdev, dev, false, NULL, remove,
656 				      wdev->wext.default_key == -1,
657 				      idx, &params);
658 out:
659 	wiphy_unlock(&rdev->wiphy);
660 
661 	return err;
662 }
663 
664 static int cfg80211_wext_siwencodeext(struct net_device *dev,
665 				      struct iw_request_info *info,
666 				      struct iw_point *erq, char *extra)
667 {
668 	struct wireless_dev *wdev = dev->ieee80211_ptr;
669 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
670 	struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
671 	const u8 *addr;
672 	int idx;
673 	bool remove = false;
674 	struct key_params params;
675 	u32 cipher;
676 	int ret;
677 
678 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
679 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
680 		return -EOPNOTSUPP;
681 
682 	/* no use -- only MFP (set_default_mgmt_key) is optional */
683 	if (!rdev->ops->del_key ||
684 	    !rdev->ops->add_key ||
685 	    !rdev->ops->set_default_key)
686 		return -EOPNOTSUPP;
687 
688 	switch (ext->alg) {
689 	case IW_ENCODE_ALG_NONE:
690 		remove = true;
691 		cipher = 0;
692 		break;
693 	case IW_ENCODE_ALG_WEP:
694 		if (ext->key_len == 5)
695 			cipher = WLAN_CIPHER_SUITE_WEP40;
696 		else if (ext->key_len == 13)
697 			cipher = WLAN_CIPHER_SUITE_WEP104;
698 		else
699 			return -EINVAL;
700 		break;
701 	case IW_ENCODE_ALG_TKIP:
702 		cipher = WLAN_CIPHER_SUITE_TKIP;
703 		break;
704 	case IW_ENCODE_ALG_CCMP:
705 		cipher = WLAN_CIPHER_SUITE_CCMP;
706 		break;
707 	case IW_ENCODE_ALG_AES_CMAC:
708 		cipher = WLAN_CIPHER_SUITE_AES_CMAC;
709 		break;
710 	default:
711 		return -EOPNOTSUPP;
712 	}
713 
714 	if (erq->flags & IW_ENCODE_DISABLED)
715 		remove = true;
716 
717 	idx = erq->flags & IW_ENCODE_INDEX;
718 	if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
719 		if (idx < 4 || idx > 5) {
720 			idx = wdev->wext.default_mgmt_key;
721 			if (idx < 0)
722 				return -EINVAL;
723 		} else
724 			idx--;
725 	} else {
726 		if (idx < 1 || idx > 4) {
727 			idx = wdev->wext.default_key;
728 			if (idx < 0)
729 				return -EINVAL;
730 		} else
731 			idx--;
732 	}
733 
734 	addr = ext->addr.sa_data;
735 	if (is_broadcast_ether_addr(addr))
736 		addr = NULL;
737 
738 	memset(&params, 0, sizeof(params));
739 	params.key = ext->key;
740 	params.key_len = ext->key_len;
741 	params.cipher = cipher;
742 
743 	if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
744 		params.seq = ext->rx_seq;
745 		params.seq_len = 6;
746 	}
747 
748 	wiphy_lock(wdev->wiphy);
749 	ret = cfg80211_set_encryption(
750 			rdev, dev,
751 			!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
752 			addr, remove,
753 			ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
754 			idx, &params);
755 	wiphy_unlock(wdev->wiphy);
756 
757 	return ret;
758 }
759 
760 static int cfg80211_wext_giwencode(struct net_device *dev,
761 				   struct iw_request_info *info,
762 				   struct iw_point *erq, char *keybuf)
763 {
764 	struct wireless_dev *wdev = dev->ieee80211_ptr;
765 	int idx;
766 
767 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
768 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
769 		return -EOPNOTSUPP;
770 
771 	idx = erq->flags & IW_ENCODE_INDEX;
772 	if (idx == 0) {
773 		idx = wdev->wext.default_key;
774 		if (idx < 0)
775 			idx = 0;
776 	} else if (idx < 1 || idx > 4)
777 		return -EINVAL;
778 	else
779 		idx--;
780 
781 	erq->flags = idx + 1;
782 
783 	if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
784 		erq->flags |= IW_ENCODE_DISABLED;
785 		erq->length = 0;
786 		return 0;
787 	}
788 
789 	erq->length = min_t(size_t, erq->length,
790 			    wdev->wext.keys->params[idx].key_len);
791 	memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
792 	erq->flags |= IW_ENCODE_ENABLED;
793 
794 	return 0;
795 }
796 
797 static int cfg80211_wext_siwfreq(struct net_device *dev,
798 				 struct iw_request_info *info,
799 				 struct iw_freq *wextfreq, char *extra)
800 {
801 	struct wireless_dev *wdev = dev->ieee80211_ptr;
802 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
803 	struct cfg80211_chan_def chandef = {
804 		.width = NL80211_CHAN_WIDTH_20_NOHT,
805 	};
806 	int freq, ret;
807 
808 	wiphy_lock(&rdev->wiphy);
809 
810 	switch (wdev->iftype) {
811 	case NL80211_IFTYPE_STATION:
812 		ret = cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
813 		break;
814 	case NL80211_IFTYPE_ADHOC:
815 		ret = cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
816 		break;
817 	case NL80211_IFTYPE_MONITOR:
818 		freq = cfg80211_wext_freq(wextfreq);
819 		if (freq < 0) {
820 			ret = freq;
821 			break;
822 		}
823 		if (freq == 0) {
824 			ret = -EINVAL;
825 			break;
826 		}
827 		chandef.center_freq1 = freq;
828 		chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
829 		if (!chandef.chan) {
830 			ret = -EINVAL;
831 			break;
832 		}
833 		ret = cfg80211_set_monitor_channel(rdev, &chandef);
834 		break;
835 	case NL80211_IFTYPE_MESH_POINT:
836 		freq = cfg80211_wext_freq(wextfreq);
837 		if (freq < 0) {
838 			ret = freq;
839 			break;
840 		}
841 		if (freq == 0) {
842 			ret = -EINVAL;
843 			break;
844 		}
845 		chandef.center_freq1 = freq;
846 		chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
847 		if (!chandef.chan) {
848 			ret = -EINVAL;
849 			break;
850 		}
851 		ret = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
852 		break;
853 	default:
854 		ret = -EOPNOTSUPP;
855 		break;
856 	}
857 
858 	wiphy_unlock(&rdev->wiphy);
859 
860 	return ret;
861 }
862 
863 static int cfg80211_wext_giwfreq(struct net_device *dev,
864 				 struct iw_request_info *info,
865 				 struct iw_freq *freq, char *extra)
866 {
867 	struct wireless_dev *wdev = dev->ieee80211_ptr;
868 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
869 	struct cfg80211_chan_def chandef = {};
870 	int ret;
871 
872 	wiphy_lock(&rdev->wiphy);
873 	switch (wdev->iftype) {
874 	case NL80211_IFTYPE_STATION:
875 		ret = cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
876 		break;
877 	case NL80211_IFTYPE_ADHOC:
878 		ret = cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
879 		break;
880 	case NL80211_IFTYPE_MONITOR:
881 		if (!rdev->ops->get_channel) {
882 			ret = -EINVAL;
883 			break;
884 		}
885 
886 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
887 		if (ret)
888 			break;
889 		freq->m = chandef.chan->center_freq;
890 		freq->e = 6;
891 		ret = 0;
892 		break;
893 	default:
894 		ret = -EINVAL;
895 		break;
896 	}
897 
898 	wiphy_unlock(&rdev->wiphy);
899 
900 	return ret;
901 }
902 
903 static int cfg80211_wext_siwtxpower(struct net_device *dev,
904 				    struct iw_request_info *info,
905 				    union iwreq_data *data, char *extra)
906 {
907 	struct wireless_dev *wdev = dev->ieee80211_ptr;
908 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
909 	enum nl80211_tx_power_setting type;
910 	int dbm = 0;
911 	int ret;
912 
913 	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
914 		return -EINVAL;
915 	if (data->txpower.flags & IW_TXPOW_RANGE)
916 		return -EINVAL;
917 
918 	if (!rdev->ops->set_tx_power)
919 		return -EOPNOTSUPP;
920 
921 	/* only change when not disabling */
922 	if (!data->txpower.disabled) {
923 		rfkill_set_sw_state(rdev->wiphy.rfkill, false);
924 
925 		if (data->txpower.fixed) {
926 			/*
927 			 * wext doesn't support negative values, see
928 			 * below where it's for automatic
929 			 */
930 			if (data->txpower.value < 0)
931 				return -EINVAL;
932 			dbm = data->txpower.value;
933 			type = NL80211_TX_POWER_FIXED;
934 			/* TODO: do regulatory check! */
935 		} else {
936 			/*
937 			 * Automatic power level setting, max being the value
938 			 * passed in from userland.
939 			 */
940 			if (data->txpower.value < 0) {
941 				type = NL80211_TX_POWER_AUTOMATIC;
942 			} else {
943 				dbm = data->txpower.value;
944 				type = NL80211_TX_POWER_LIMITED;
945 			}
946 		}
947 	} else {
948 		if (rfkill_set_sw_state(rdev->wiphy.rfkill, true))
949 			schedule_work(&rdev->rfkill_block);
950 		return 0;
951 	}
952 
953 	wiphy_lock(&rdev->wiphy);
954 	ret = rdev_set_tx_power(rdev, wdev, type, DBM_TO_MBM(dbm));
955 	wiphy_unlock(&rdev->wiphy);
956 
957 	return ret;
958 }
959 
960 static int cfg80211_wext_giwtxpower(struct net_device *dev,
961 				    struct iw_request_info *info,
962 				    union iwreq_data *data, char *extra)
963 {
964 	struct wireless_dev *wdev = dev->ieee80211_ptr;
965 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
966 	int err, val;
967 
968 	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
969 		return -EINVAL;
970 	if (data->txpower.flags & IW_TXPOW_RANGE)
971 		return -EINVAL;
972 
973 	if (!rdev->ops->get_tx_power)
974 		return -EOPNOTSUPP;
975 
976 	wiphy_lock(&rdev->wiphy);
977 	err = rdev_get_tx_power(rdev, wdev, &val);
978 	wiphy_unlock(&rdev->wiphy);
979 	if (err)
980 		return err;
981 
982 	/* well... oh well */
983 	data->txpower.fixed = 1;
984 	data->txpower.disabled = rfkill_blocked(rdev->wiphy.rfkill);
985 	data->txpower.value = val;
986 	data->txpower.flags = IW_TXPOW_DBM;
987 
988 	return 0;
989 }
990 
991 static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
992 				 s32 auth_alg)
993 {
994 	int nr_alg = 0;
995 
996 	if (!auth_alg)
997 		return -EINVAL;
998 
999 	if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
1000 			 IW_AUTH_ALG_SHARED_KEY |
1001 			 IW_AUTH_ALG_LEAP))
1002 		return -EINVAL;
1003 
1004 	if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
1005 		nr_alg++;
1006 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1007 	}
1008 
1009 	if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
1010 		nr_alg++;
1011 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
1012 	}
1013 
1014 	if (auth_alg & IW_AUTH_ALG_LEAP) {
1015 		nr_alg++;
1016 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
1017 	}
1018 
1019 	if (nr_alg > 1)
1020 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1021 
1022 	return 0;
1023 }
1024 
1025 static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
1026 {
1027 	if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
1028 			     IW_AUTH_WPA_VERSION_WPA2|
1029 		             IW_AUTH_WPA_VERSION_DISABLED))
1030 		return -EINVAL;
1031 
1032 	if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
1033 	    (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
1034 			     IW_AUTH_WPA_VERSION_WPA2)))
1035 		return -EINVAL;
1036 
1037 	if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
1038 		wdev->wext.connect.crypto.wpa_versions &=
1039 			~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
1040 
1041 	if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
1042 		wdev->wext.connect.crypto.wpa_versions |=
1043 			NL80211_WPA_VERSION_1;
1044 
1045 	if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
1046 		wdev->wext.connect.crypto.wpa_versions |=
1047 			NL80211_WPA_VERSION_2;
1048 
1049 	return 0;
1050 }
1051 
1052 static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
1053 {
1054 	if (cipher & IW_AUTH_CIPHER_WEP40)
1055 		wdev->wext.connect.crypto.cipher_group =
1056 			WLAN_CIPHER_SUITE_WEP40;
1057 	else if (cipher & IW_AUTH_CIPHER_WEP104)
1058 		wdev->wext.connect.crypto.cipher_group =
1059 			WLAN_CIPHER_SUITE_WEP104;
1060 	else if (cipher & IW_AUTH_CIPHER_TKIP)
1061 		wdev->wext.connect.crypto.cipher_group =
1062 			WLAN_CIPHER_SUITE_TKIP;
1063 	else if (cipher & IW_AUTH_CIPHER_CCMP)
1064 		wdev->wext.connect.crypto.cipher_group =
1065 			WLAN_CIPHER_SUITE_CCMP;
1066 	else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
1067 		wdev->wext.connect.crypto.cipher_group =
1068 			WLAN_CIPHER_SUITE_AES_CMAC;
1069 	else if (cipher & IW_AUTH_CIPHER_NONE)
1070 		wdev->wext.connect.crypto.cipher_group = 0;
1071 	else
1072 		return -EINVAL;
1073 
1074 	return 0;
1075 }
1076 
1077 static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
1078 {
1079 	int nr_ciphers = 0;
1080 	u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
1081 
1082 	if (cipher & IW_AUTH_CIPHER_WEP40) {
1083 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
1084 		nr_ciphers++;
1085 	}
1086 
1087 	if (cipher & IW_AUTH_CIPHER_WEP104) {
1088 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
1089 		nr_ciphers++;
1090 	}
1091 
1092 	if (cipher & IW_AUTH_CIPHER_TKIP) {
1093 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
1094 		nr_ciphers++;
1095 	}
1096 
1097 	if (cipher & IW_AUTH_CIPHER_CCMP) {
1098 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
1099 		nr_ciphers++;
1100 	}
1101 
1102 	if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
1103 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
1104 		nr_ciphers++;
1105 	}
1106 
1107 	BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
1108 
1109 	wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1110 
1111 	return 0;
1112 }
1113 
1114 
1115 static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
1116 {
1117 	int nr_akm_suites = 0;
1118 
1119 	if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1120 			IW_AUTH_KEY_MGMT_PSK))
1121 		return -EINVAL;
1122 
1123 	if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1124 		wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1125 			WLAN_AKM_SUITE_8021X;
1126 		nr_akm_suites++;
1127 	}
1128 
1129 	if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1130 		wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1131 			WLAN_AKM_SUITE_PSK;
1132 		nr_akm_suites++;
1133 	}
1134 
1135 	wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1136 
1137 	return 0;
1138 }
1139 
1140 static int cfg80211_wext_siwauth(struct net_device *dev,
1141 				 struct iw_request_info *info,
1142 				 struct iw_param *data, char *extra)
1143 {
1144 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1145 
1146 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1147 		return -EOPNOTSUPP;
1148 
1149 	switch (data->flags & IW_AUTH_INDEX) {
1150 	case IW_AUTH_PRIVACY_INVOKED:
1151 		wdev->wext.connect.privacy = data->value;
1152 		return 0;
1153 	case IW_AUTH_WPA_VERSION:
1154 		return cfg80211_set_wpa_version(wdev, data->value);
1155 	case IW_AUTH_CIPHER_GROUP:
1156 		return cfg80211_set_cipher_group(wdev, data->value);
1157 	case IW_AUTH_KEY_MGMT:
1158 		return cfg80211_set_key_mgt(wdev, data->value);
1159 	case IW_AUTH_CIPHER_PAIRWISE:
1160 		return cfg80211_set_cipher_pairwise(wdev, data->value);
1161 	case IW_AUTH_80211_AUTH_ALG:
1162 		return cfg80211_set_auth_alg(wdev, data->value);
1163 	case IW_AUTH_WPA_ENABLED:
1164 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1165 	case IW_AUTH_DROP_UNENCRYPTED:
1166 	case IW_AUTH_MFP:
1167 		return 0;
1168 	default:
1169 		return -EOPNOTSUPP;
1170 	}
1171 }
1172 
1173 static int cfg80211_wext_giwauth(struct net_device *dev,
1174 				 struct iw_request_info *info,
1175 				 struct iw_param *data, char *extra)
1176 {
1177 	/* XXX: what do we need? */
1178 
1179 	return -EOPNOTSUPP;
1180 }
1181 
1182 static int cfg80211_wext_siwpower(struct net_device *dev,
1183 				  struct iw_request_info *info,
1184 				  struct iw_param *wrq, char *extra)
1185 {
1186 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1187 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1188 	bool ps;
1189 	int timeout = wdev->ps_timeout;
1190 	int err;
1191 
1192 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1193 		return -EINVAL;
1194 
1195 	if (!rdev->ops->set_power_mgmt)
1196 		return -EOPNOTSUPP;
1197 
1198 	if (wrq->disabled) {
1199 		ps = false;
1200 	} else {
1201 		switch (wrq->flags & IW_POWER_MODE) {
1202 		case IW_POWER_ON:       /* If not specified */
1203 		case IW_POWER_MODE:     /* If set all mask */
1204 		case IW_POWER_ALL_R:    /* If explicitely state all */
1205 			ps = true;
1206 			break;
1207 		default:                /* Otherwise we ignore */
1208 			return -EINVAL;
1209 		}
1210 
1211 		if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1212 			return -EINVAL;
1213 
1214 		if (wrq->flags & IW_POWER_TIMEOUT)
1215 			timeout = wrq->value / 1000;
1216 	}
1217 
1218 	wiphy_lock(&rdev->wiphy);
1219 	err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
1220 	wiphy_unlock(&rdev->wiphy);
1221 	if (err)
1222 		return err;
1223 
1224 	wdev->ps = ps;
1225 	wdev->ps_timeout = timeout;
1226 
1227 	return 0;
1228 
1229 }
1230 
1231 static int cfg80211_wext_giwpower(struct net_device *dev,
1232 				  struct iw_request_info *info,
1233 				  struct iw_param *wrq, char *extra)
1234 {
1235 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1236 
1237 	wrq->disabled = !wdev->ps;
1238 
1239 	return 0;
1240 }
1241 
1242 static int cfg80211_wext_siwrate(struct net_device *dev,
1243 				 struct iw_request_info *info,
1244 				 struct iw_param *rate, char *extra)
1245 {
1246 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1247 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1248 	struct cfg80211_bitrate_mask mask;
1249 	u32 fixed, maxrate;
1250 	struct ieee80211_supported_band *sband;
1251 	int band, ridx, ret;
1252 	bool match = false;
1253 
1254 	if (!rdev->ops->set_bitrate_mask)
1255 		return -EOPNOTSUPP;
1256 
1257 	memset(&mask, 0, sizeof(mask));
1258 	fixed = 0;
1259 	maxrate = (u32)-1;
1260 
1261 	if (rate->value < 0) {
1262 		/* nothing */
1263 	} else if (rate->fixed) {
1264 		fixed = rate->value / 100000;
1265 	} else {
1266 		maxrate = rate->value / 100000;
1267 	}
1268 
1269 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1270 		sband = wdev->wiphy->bands[band];
1271 		if (sband == NULL)
1272 			continue;
1273 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
1274 			struct ieee80211_rate *srate = &sband->bitrates[ridx];
1275 			if (fixed == srate->bitrate) {
1276 				mask.control[band].legacy = 1 << ridx;
1277 				match = true;
1278 				break;
1279 			}
1280 			if (srate->bitrate <= maxrate) {
1281 				mask.control[band].legacy |= 1 << ridx;
1282 				match = true;
1283 			}
1284 		}
1285 	}
1286 
1287 	if (!match)
1288 		return -EINVAL;
1289 
1290 	wiphy_lock(&rdev->wiphy);
1291 	if (dev->ieee80211_ptr->valid_links)
1292 		ret = -EOPNOTSUPP;
1293 	else
1294 		ret = rdev_set_bitrate_mask(rdev, dev, 0, NULL, &mask);
1295 	wiphy_unlock(&rdev->wiphy);
1296 
1297 	return ret;
1298 }
1299 
1300 static int cfg80211_wext_giwrate(struct net_device *dev,
1301 				 struct iw_request_info *info,
1302 				 struct iw_param *rate, char *extra)
1303 {
1304 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1305 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1306 	struct station_info sinfo = {};
1307 	u8 addr[ETH_ALEN];
1308 	int err;
1309 
1310 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1311 		return -EOPNOTSUPP;
1312 
1313 	if (!rdev->ops->get_station)
1314 		return -EOPNOTSUPP;
1315 
1316 	err = 0;
1317 	wdev_lock(wdev);
1318 	if (!wdev->valid_links && wdev->links[0].client.current_bss)
1319 		memcpy(addr, wdev->links[0].client.current_bss->pub.bssid,
1320 		       ETH_ALEN);
1321 	else
1322 		err = -EOPNOTSUPP;
1323 	wdev_unlock(wdev);
1324 	if (err)
1325 		return err;
1326 
1327 	wiphy_lock(&rdev->wiphy);
1328 	err = rdev_get_station(rdev, dev, addr, &sinfo);
1329 	wiphy_unlock(&rdev->wiphy);
1330 	if (err)
1331 		return err;
1332 
1333 	if (!(sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
1334 		err = -EOPNOTSUPP;
1335 		goto free;
1336 	}
1337 
1338 	rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
1339 
1340 free:
1341 	cfg80211_sinfo_release_content(&sinfo);
1342 	return err;
1343 }
1344 
1345 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
1346 static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
1347 {
1348 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1349 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1350 	/* we are under RTNL - globally locked - so can use static structs */
1351 	static struct iw_statistics wstats;
1352 	static struct station_info sinfo = {};
1353 	u8 bssid[ETH_ALEN];
1354 	int ret;
1355 
1356 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
1357 		return NULL;
1358 
1359 	if (!rdev->ops->get_station)
1360 		return NULL;
1361 
1362 	/* Grab BSSID of current BSS, if any */
1363 	wdev_lock(wdev);
1364 	if (wdev->valid_links || !wdev->links[0].client.current_bss) {
1365 		wdev_unlock(wdev);
1366 		return NULL;
1367 	}
1368 	memcpy(bssid, wdev->links[0].client.current_bss->pub.bssid, ETH_ALEN);
1369 	wdev_unlock(wdev);
1370 
1371 	memset(&sinfo, 0, sizeof(sinfo));
1372 
1373 	wiphy_lock(&rdev->wiphy);
1374 	ret = rdev_get_station(rdev, dev, bssid, &sinfo);
1375 	wiphy_unlock(&rdev->wiphy);
1376 
1377 	if (ret)
1378 		return NULL;
1379 
1380 	memset(&wstats, 0, sizeof(wstats));
1381 
1382 	switch (rdev->wiphy.signal_type) {
1383 	case CFG80211_SIGNAL_TYPE_MBM:
1384 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
1385 			int sig = sinfo.signal;
1386 			wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1387 			wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1388 			wstats.qual.updated |= IW_QUAL_DBM;
1389 			wstats.qual.level = sig;
1390 			if (sig < -110)
1391 				sig = -110;
1392 			else if (sig > -40)
1393 				sig = -40;
1394 			wstats.qual.qual = sig + 110;
1395 			break;
1396 		}
1397 		fallthrough;
1398 	case CFG80211_SIGNAL_TYPE_UNSPEC:
1399 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
1400 			wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1401 			wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1402 			wstats.qual.level = sinfo.signal;
1403 			wstats.qual.qual = sinfo.signal;
1404 			break;
1405 		}
1406 		fallthrough;
1407 	default:
1408 		wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1409 		wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1410 	}
1411 
1412 	wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
1413 	if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC))
1414 		wstats.discard.misc = sinfo.rx_dropped_misc;
1415 	if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))
1416 		wstats.discard.retries = sinfo.tx_failed;
1417 
1418 	cfg80211_sinfo_release_content(&sinfo);
1419 
1420 	return &wstats;
1421 }
1422 
1423 static int cfg80211_wext_siwap(struct net_device *dev,
1424 			       struct iw_request_info *info,
1425 			       struct sockaddr *ap_addr, char *extra)
1426 {
1427 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1428 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1429 	int ret;
1430 
1431 	wiphy_lock(&rdev->wiphy);
1432 	switch (wdev->iftype) {
1433 	case NL80211_IFTYPE_ADHOC:
1434 		ret = cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1435 		break;
1436 	case NL80211_IFTYPE_STATION:
1437 		ret = cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1438 		break;
1439 	default:
1440 		ret = -EOPNOTSUPP;
1441 		break;
1442 	}
1443 	wiphy_unlock(&rdev->wiphy);
1444 
1445 	return ret;
1446 }
1447 
1448 static int cfg80211_wext_giwap(struct net_device *dev,
1449 			       struct iw_request_info *info,
1450 			       struct sockaddr *ap_addr, char *extra)
1451 {
1452 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1453 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1454 	int ret;
1455 
1456 	wiphy_lock(&rdev->wiphy);
1457 	switch (wdev->iftype) {
1458 	case NL80211_IFTYPE_ADHOC:
1459 		ret = cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1460 		break;
1461 	case NL80211_IFTYPE_STATION:
1462 		ret = cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1463 		break;
1464 	default:
1465 		ret = -EOPNOTSUPP;
1466 		break;
1467 	}
1468 	wiphy_unlock(&rdev->wiphy);
1469 
1470 	return ret;
1471 }
1472 
1473 static int cfg80211_wext_siwessid(struct net_device *dev,
1474 				  struct iw_request_info *info,
1475 				  struct iw_point *data, char *ssid)
1476 {
1477 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1478 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1479 	int ret;
1480 
1481 	wiphy_lock(&rdev->wiphy);
1482 	switch (wdev->iftype) {
1483 	case NL80211_IFTYPE_ADHOC:
1484 		ret = cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1485 		break;
1486 	case NL80211_IFTYPE_STATION:
1487 		ret = cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
1488 		break;
1489 	default:
1490 		ret = -EOPNOTSUPP;
1491 		break;
1492 	}
1493 	wiphy_unlock(&rdev->wiphy);
1494 
1495 	return ret;
1496 }
1497 
1498 static int cfg80211_wext_giwessid(struct net_device *dev,
1499 				  struct iw_request_info *info,
1500 				  struct iw_point *data, char *ssid)
1501 {
1502 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1503 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1504 	int ret;
1505 
1506 	data->flags = 0;
1507 	data->length = 0;
1508 
1509 	wiphy_lock(&rdev->wiphy);
1510 	switch (wdev->iftype) {
1511 	case NL80211_IFTYPE_ADHOC:
1512 		ret = cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1513 		break;
1514 	case NL80211_IFTYPE_STATION:
1515 		ret = cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1516 		break;
1517 	default:
1518 		ret = -EOPNOTSUPP;
1519 		break;
1520 	}
1521 	wiphy_unlock(&rdev->wiphy);
1522 
1523 	return ret;
1524 }
1525 
1526 static int cfg80211_wext_siwpmksa(struct net_device *dev,
1527 				  struct iw_request_info *info,
1528 				  struct iw_point *data, char *extra)
1529 {
1530 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1531 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1532 	struct cfg80211_pmksa cfg_pmksa;
1533 	struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
1534 	int ret;
1535 
1536 	memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
1537 
1538 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1539 		return -EINVAL;
1540 
1541 	cfg_pmksa.bssid = pmksa->bssid.sa_data;
1542 	cfg_pmksa.pmkid = pmksa->pmkid;
1543 
1544 	wiphy_lock(&rdev->wiphy);
1545 	switch (pmksa->cmd) {
1546 	case IW_PMKSA_ADD:
1547 		if (!rdev->ops->set_pmksa) {
1548 			ret = -EOPNOTSUPP;
1549 			break;
1550 		}
1551 
1552 		ret = rdev_set_pmksa(rdev, dev, &cfg_pmksa);
1553 		break;
1554 	case IW_PMKSA_REMOVE:
1555 		if (!rdev->ops->del_pmksa) {
1556 			ret = -EOPNOTSUPP;
1557 			break;
1558 		}
1559 
1560 		ret = rdev_del_pmksa(rdev, dev, &cfg_pmksa);
1561 		break;
1562 	case IW_PMKSA_FLUSH:
1563 		if (!rdev->ops->flush_pmksa) {
1564 			ret = -EOPNOTSUPP;
1565 			break;
1566 		}
1567 
1568 		ret = rdev_flush_pmksa(rdev, dev);
1569 		break;
1570 	default:
1571 		ret = -EOPNOTSUPP;
1572 		break;
1573 	}
1574 	wiphy_unlock(&rdev->wiphy);
1575 
1576 	return ret;
1577 }
1578 
1579 #define DEFINE_WEXT_COMPAT_STUB(func, type)			\
1580 	static int __ ## func(struct net_device *dev,		\
1581 			      struct iw_request_info *info,	\
1582 			      union iwreq_data *wrqu,		\
1583 			      char *extra)			\
1584 	{							\
1585 		return func(dev, info, (type *)wrqu, extra);	\
1586 	}
1587 
1588 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwname, char)
1589 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwfreq, struct iw_freq)
1590 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwfreq, struct iw_freq)
1591 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwmode, u32)
1592 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwmode, u32)
1593 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwrange, struct iw_point)
1594 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwap, struct sockaddr)
1595 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwap, struct sockaddr)
1596 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwmlme, struct iw_point)
1597 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwscan, struct iw_point)
1598 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwessid, struct iw_point)
1599 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwessid, struct iw_point)
1600 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwrate, struct iw_param)
1601 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwrate, struct iw_param)
1602 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwrts, struct iw_param)
1603 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwrts, struct iw_param)
1604 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwfrag, struct iw_param)
1605 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwfrag, struct iw_param)
1606 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwretry, struct iw_param)
1607 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwretry, struct iw_param)
1608 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwencode, struct iw_point)
1609 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwencode, struct iw_point)
1610 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwpower, struct iw_param)
1611 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwpower, struct iw_param)
1612 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwgenie, struct iw_point)
1613 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_giwauth, struct iw_param)
1614 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwauth, struct iw_param)
1615 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwencodeext, struct iw_point)
1616 DEFINE_WEXT_COMPAT_STUB(cfg80211_wext_siwpmksa, struct iw_point)
1617 
1618 static const iw_handler cfg80211_handlers[] = {
1619 	[IW_IOCTL_IDX(SIOCGIWNAME)]	= __cfg80211_wext_giwname,
1620 	[IW_IOCTL_IDX(SIOCSIWFREQ)]	= __cfg80211_wext_siwfreq,
1621 	[IW_IOCTL_IDX(SIOCGIWFREQ)]	= __cfg80211_wext_giwfreq,
1622 	[IW_IOCTL_IDX(SIOCSIWMODE)]	= __cfg80211_wext_siwmode,
1623 	[IW_IOCTL_IDX(SIOCGIWMODE)]	= __cfg80211_wext_giwmode,
1624 	[IW_IOCTL_IDX(SIOCGIWRANGE)]	= __cfg80211_wext_giwrange,
1625 	[IW_IOCTL_IDX(SIOCSIWAP)]	= __cfg80211_wext_siwap,
1626 	[IW_IOCTL_IDX(SIOCGIWAP)]	= __cfg80211_wext_giwap,
1627 	[IW_IOCTL_IDX(SIOCSIWMLME)]	= __cfg80211_wext_siwmlme,
1628 	[IW_IOCTL_IDX(SIOCSIWSCAN)]	= cfg80211_wext_siwscan,
1629 	[IW_IOCTL_IDX(SIOCGIWSCAN)]	= __cfg80211_wext_giwscan,
1630 	[IW_IOCTL_IDX(SIOCSIWESSID)]	= __cfg80211_wext_siwessid,
1631 	[IW_IOCTL_IDX(SIOCGIWESSID)]	= __cfg80211_wext_giwessid,
1632 	[IW_IOCTL_IDX(SIOCSIWRATE)]	= __cfg80211_wext_siwrate,
1633 	[IW_IOCTL_IDX(SIOCGIWRATE)]	= __cfg80211_wext_giwrate,
1634 	[IW_IOCTL_IDX(SIOCSIWRTS)]	= __cfg80211_wext_siwrts,
1635 	[IW_IOCTL_IDX(SIOCGIWRTS)]	= __cfg80211_wext_giwrts,
1636 	[IW_IOCTL_IDX(SIOCSIWFRAG)]	= __cfg80211_wext_siwfrag,
1637 	[IW_IOCTL_IDX(SIOCGIWFRAG)]	= __cfg80211_wext_giwfrag,
1638 	[IW_IOCTL_IDX(SIOCSIWTXPOW)]	= cfg80211_wext_siwtxpower,
1639 	[IW_IOCTL_IDX(SIOCGIWTXPOW)]	= cfg80211_wext_giwtxpower,
1640 	[IW_IOCTL_IDX(SIOCSIWRETRY)]	= __cfg80211_wext_siwretry,
1641 	[IW_IOCTL_IDX(SIOCGIWRETRY)]	= __cfg80211_wext_giwretry,
1642 	[IW_IOCTL_IDX(SIOCSIWENCODE)]	= __cfg80211_wext_siwencode,
1643 	[IW_IOCTL_IDX(SIOCGIWENCODE)]	= __cfg80211_wext_giwencode,
1644 	[IW_IOCTL_IDX(SIOCSIWPOWER)]	= __cfg80211_wext_siwpower,
1645 	[IW_IOCTL_IDX(SIOCGIWPOWER)]	= __cfg80211_wext_giwpower,
1646 	[IW_IOCTL_IDX(SIOCSIWGENIE)]	= __cfg80211_wext_siwgenie,
1647 	[IW_IOCTL_IDX(SIOCSIWAUTH)]	= __cfg80211_wext_siwauth,
1648 	[IW_IOCTL_IDX(SIOCGIWAUTH)]	= __cfg80211_wext_giwauth,
1649 	[IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= __cfg80211_wext_siwencodeext,
1650 	[IW_IOCTL_IDX(SIOCSIWPMKSA)]	= __cfg80211_wext_siwpmksa,
1651 };
1652 
1653 const struct iw_handler_def cfg80211_wext_handler = {
1654 	.num_standard		= ARRAY_SIZE(cfg80211_handlers),
1655 	.standard		= cfg80211_handlers,
1656 	.get_wireless_stats = cfg80211_wireless_stats,
1657 };
1658