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