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