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