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