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