blob: 7b6529d81c61e4e58f45c654740f26deccbeae19 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Bergfee52672008-11-26 22:36:31 +01002/*
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 *
Johannes Berg08645122009-05-11 13:54:58 +02009 * Copyright 2008-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg3cfe91c2019-08-30 14:24:44 +030010 * Copyright (C) 2019 Intel Corporation
Johannes Bergfee52672008-11-26 22:36:31 +010011 */
12
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040013#include <linux/export.h>
Johannes Bergfee52672008-11-26 22:36:31 +010014#include <linux/wireless.h>
15#include <linux/nl80211.h>
Johannes Berg691597c2009-04-19 19:57:45 +020016#include <linux/if_arp.h>
Johannes Berg08645122009-05-11 13:54:58 +020017#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Johannes Bergfee52672008-11-26 22:36:31 +010019#include <net/iw_handler.h>
Johannes Bergfee52672008-11-26 22:36:31 +010020#include <net/cfg80211.h>
Johannes Berg262eb9b22011-07-13 10:39:09 +020021#include <net/cfg80211-wext.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020022#include "wext-compat.h"
Johannes Bergfee52672008-11-26 22:36:31 +010023#include "core.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030024#include "rdev-ops.h"
Johannes Bergfee52672008-11-26 22:36:31 +010025
26int cfg80211_wext_giwname(struct net_device *dev,
27 struct iw_request_info *info,
28 char *name, char *extra)
29{
Johannes Bergfee52672008-11-26 22:36:31 +010030 strcpy(name, "IEEE 802.11");
Johannes Bergfee52672008-11-26 22:36:31 +010031 return 0;
32}
Johannes Berg2afe38d2015-01-06 14:00:53 +010033EXPORT_WEXT_HANDLER(cfg80211_wext_giwname);
Johannes Berge60c7742008-11-26 23:31:40 +010034
35int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
36 u32 *mode, char *extra)
37{
38 struct wireless_dev *wdev = dev->ieee80211_ptr;
39 struct cfg80211_registered_device *rdev;
40 struct vif_params vifparams;
41 enum nl80211_iftype type;
42
Zhao, Gangf26cbf42014-04-21 12:53:03 +080043 rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berge60c7742008-11-26 23:31:40 +010044
Johannes Berge60c7742008-11-26 23:31:40 +010045 switch (*mode) {
46 case IW_MODE_INFRA:
47 type = NL80211_IFTYPE_STATION;
48 break;
49 case IW_MODE_ADHOC:
50 type = NL80211_IFTYPE_ADHOC;
51 break;
52 case IW_MODE_REPEAT:
53 type = NL80211_IFTYPE_WDS;
54 break;
55 case IW_MODE_MONITOR:
56 type = NL80211_IFTYPE_MONITOR;
57 break;
58 default:
59 return -EINVAL;
60 }
61
Johannes Bergac7f9cf2009-03-21 17:07:59 +010062 if (type == wdev->iftype)
63 return 0;
64
Johannes Berge60c7742008-11-26 23:31:40 +010065 memset(&vifparams, 0, sizeof(vifparams));
66
Johannes Berg818a9862017-04-12 11:23:28 +020067 return cfg80211_change_iface(rdev, dev, type, &vifparams);
Johannes Berge60c7742008-11-26 23:31:40 +010068}
Johannes Berg2afe38d2015-01-06 14:00:53 +010069EXPORT_WEXT_HANDLER(cfg80211_wext_siwmode);
Johannes Berge60c7742008-11-26 23:31:40 +010070
71int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
72 u32 *mode, char *extra)
73{
74 struct wireless_dev *wdev = dev->ieee80211_ptr;
75
76 if (!wdev)
77 return -EOPNOTSUPP;
78
79 switch (wdev->iftype) {
80 case NL80211_IFTYPE_AP:
81 *mode = IW_MODE_MASTER;
82 break;
83 case NL80211_IFTYPE_STATION:
84 *mode = IW_MODE_INFRA;
85 break;
86 case NL80211_IFTYPE_ADHOC:
87 *mode = IW_MODE_ADHOC;
88 break;
89 case NL80211_IFTYPE_MONITOR:
90 *mode = IW_MODE_MONITOR;
91 break;
92 case NL80211_IFTYPE_WDS:
93 *mode = IW_MODE_REPEAT;
94 break;
95 case NL80211_IFTYPE_AP_VLAN:
96 *mode = IW_MODE_SECOND; /* FIXME */
97 break;
98 default:
99 *mode = IW_MODE_AUTO;
100 break;
101 }
102 return 0;
103}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100104EXPORT_WEXT_HANDLER(cfg80211_wext_giwmode);
Johannes Berg4aa188e2009-02-18 19:32:08 +0100105
106
107int cfg80211_wext_giwrange(struct net_device *dev,
108 struct iw_request_info *info,
109 struct iw_point *data, char *extra)
110{
111 struct wireless_dev *wdev = dev->ieee80211_ptr;
112 struct iw_range *range = (struct iw_range *) extra;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200113 enum nl80211_band band;
Johannes Berg9834c072009-07-06 19:40:51 +0200114 int i, c = 0;
Johannes Berg4aa188e2009-02-18 19:32:08 +0100115
116 if (!wdev)
117 return -EOPNOTSUPP;
118
119 data->length = sizeof(struct iw_range);
120 memset(range, 0, sizeof(struct iw_range));
121
122 range->we_version_compiled = WIRELESS_EXT;
123 range->we_version_source = 21;
124 range->retry_capa = IW_RETRY_LIMIT;
125 range->retry_flags = IW_RETRY_LIMIT;
126 range->min_retry = 0;
127 range->max_retry = 255;
128 range->min_rts = 0;
129 range->max_rts = 2347;
130 range->min_frag = 256;
131 range->max_frag = 2346;
132
Johannes Berg4aa188e2009-02-18 19:32:08 +0100133 range->max_encoding_tokens = 4;
134
135 range->max_qual.updated = IW_QUAL_NOISE_INVALID;
136
137 switch (wdev->wiphy->signal_type) {
138 case CFG80211_SIGNAL_TYPE_NONE:
139 break;
140 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berge7d83ed2012-12-07 11:58:26 +0100141 range->max_qual.level = (u8)-110;
Johannes Berg4aa188e2009-02-18 19:32:08 +0100142 range->max_qual.qual = 70;
143 range->avg_qual.qual = 35;
144 range->max_qual.updated |= IW_QUAL_DBM;
145 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
146 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
147 break;
148 case CFG80211_SIGNAL_TYPE_UNSPEC:
149 range->max_qual.level = 100;
150 range->max_qual.qual = 100;
151 range->avg_qual.qual = 50;
152 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
153 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
154 break;
155 }
156
157 range->avg_qual.level = range->max_qual.level / 2;
158 range->avg_qual.noise = range->max_qual.noise / 2;
159 range->avg_qual.updated = range->max_qual.updated;
160
Johannes Berg9834c072009-07-06 19:40:51 +0200161 for (i = 0; i < wdev->wiphy->n_cipher_suites; i++) {
162 switch (wdev->wiphy->cipher_suites[i]) {
David Kilroy3daf0972009-06-18 23:21:14 +0100163 case WLAN_CIPHER_SUITE_TKIP:
David Kilroy27bea662009-06-18 23:21:17 +0100164 range->enc_capa |= (IW_ENC_CAPA_CIPHER_TKIP |
165 IW_ENC_CAPA_WPA);
David Kilroy3daf0972009-06-18 23:21:14 +0100166 break;
167
168 case WLAN_CIPHER_SUITE_CCMP:
David Kilroy27bea662009-06-18 23:21:17 +0100169 range->enc_capa |= (IW_ENC_CAPA_CIPHER_CCMP |
170 IW_ENC_CAPA_WPA2);
David Kilroy3daf0972009-06-18 23:21:14 +0100171 break;
David Kilroy2ab658f2009-06-18 23:21:16 +0100172
173 case WLAN_CIPHER_SUITE_WEP40:
174 range->encoding_size[range->num_encoding_sizes++] =
175 WLAN_KEY_LEN_WEP40;
176 break;
177
178 case WLAN_CIPHER_SUITE_WEP104:
179 range->encoding_size[range->num_encoding_sizes++] =
180 WLAN_KEY_LEN_WEP104;
181 break;
David Kilroy3daf0972009-06-18 23:21:14 +0100182 }
183 }
Johannes Berg4aa188e2009-02-18 19:32:08 +0100184
Johannes Berg57fbcce2016-04-12 15:56:15 +0200185 for (band = 0; band < NUM_NL80211_BANDS; band ++) {
Johannes Berg4aa188e2009-02-18 19:32:08 +0100186 struct ieee80211_supported_band *sband;
187
188 sband = wdev->wiphy->bands[band];
189
190 if (!sband)
191 continue;
192
193 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
194 struct ieee80211_channel *chan = &sband->channels[i];
195
196 if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
197 range->freq[c].i =
198 ieee80211_frequency_to_channel(
199 chan->center_freq);
200 range->freq[c].m = chan->center_freq;
201 range->freq[c].e = 6;
202 c++;
203 }
204 }
205 }
206 range->num_channels = c;
207 range->num_frequency = c;
208
209 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
210 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
211 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
212
David Kilroy51cd4aa2009-06-18 23:21:15 +0100213 if (wdev->wiphy->max_scan_ssids > 0)
214 range->scan_capa |= IW_SCAN_CAPA_ESSID;
Johannes Berg4aa188e2009-02-18 19:32:08 +0100215
216 return 0;
217}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100218EXPORT_WEXT_HANDLER(cfg80211_wext_giwrange);
Johannes Berg691597c2009-04-19 19:57:45 +0200219
Johannes Berg04a773a2009-04-19 21:24:32 +0200220
221/**
222 * cfg80211_wext_freq - get wext frequency for non-"auto"
Zhao, Gang96998e32014-04-09 09:28:06 +0800223 * @dev: the net device
Johannes Berg04a773a2009-04-19 21:24:32 +0200224 * @freq: the wext freq encoding
225 *
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200226 * Returns a frequency, or a negative error code, or 0 for auto.
Johannes Berg04a773a2009-04-19 21:24:32 +0200227 */
Zhao, Gang96998e32014-04-09 09:28:06 +0800228int cfg80211_wext_freq(struct iw_freq *freq)
Johannes Berg04a773a2009-04-19 21:24:32 +0200229{
Johannes Berg0b258582009-05-08 09:42:33 +0200230 /*
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200231 * Parse frequency - return 0 for auto and
Johannes Berg0b258582009-05-08 09:42:33 +0200232 * -EINVAL for impossible things.
233 */
Johannes Berg04a773a2009-04-19 21:24:32 +0200234 if (freq->e == 0) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200235 enum nl80211_band band = NL80211_BAND_2GHZ;
Johannes Berg04a773a2009-04-19 21:24:32 +0200236 if (freq->m < 0)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200237 return 0;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900238 if (freq->m > 14)
Johannes Berg57fbcce2016-04-12 15:56:15 +0200239 band = NL80211_BAND_5GHZ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900240 return ieee80211_channel_to_frequency(freq->m, band);
Johannes Berg04a773a2009-04-19 21:24:32 +0200241 } else {
242 int i, div = 1000000;
243 for (i = 0; i < freq->e; i++)
244 div /= 10;
Johannes Berg0b258582009-05-08 09:42:33 +0200245 if (div <= 0)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200246 return -EINVAL;
247 return freq->m / div;
Johannes Berg04a773a2009-04-19 21:24:32 +0200248 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200249}
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200250
251int cfg80211_wext_siwrts(struct net_device *dev,
252 struct iw_request_info *info,
253 struct iw_param *rts, char *extra)
254{
255 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800256 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200257 u32 orts = wdev->wiphy->rts_threshold;
258 int err;
259
260 if (rts->disabled || !rts->fixed)
261 wdev->wiphy->rts_threshold = (u32) -1;
262 else if (rts->value < 0)
263 return -EINVAL;
264 else
265 wdev->wiphy->rts_threshold = rts->value;
266
Hila Gonene35e4d22012-06-27 17:19:42 +0300267 err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_RTS_THRESHOLD);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200268 if (err)
269 wdev->wiphy->rts_threshold = orts;
270
271 return err;
272}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100273EXPORT_WEXT_HANDLER(cfg80211_wext_siwrts);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200274
275int cfg80211_wext_giwrts(struct net_device *dev,
276 struct iw_request_info *info,
277 struct iw_param *rts, char *extra)
278{
279 struct wireless_dev *wdev = dev->ieee80211_ptr;
280
281 rts->value = wdev->wiphy->rts_threshold;
282 rts->disabled = rts->value == (u32) -1;
283 rts->fixed = 1;
284
285 return 0;
286}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100287EXPORT_WEXT_HANDLER(cfg80211_wext_giwrts);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200288
289int cfg80211_wext_siwfrag(struct net_device *dev,
290 struct iw_request_info *info,
291 struct iw_param *frag, char *extra)
292{
293 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800294 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200295 u32 ofrag = wdev->wiphy->frag_threshold;
296 int err;
297
298 if (frag->disabled || !frag->fixed)
299 wdev->wiphy->frag_threshold = (u32) -1;
300 else if (frag->value < 256)
301 return -EINVAL;
302 else {
303 /* Fragment length must be even, so strip LSB. */
304 wdev->wiphy->frag_threshold = frag->value & ~0x1;
305 }
306
Hila Gonene35e4d22012-06-27 17:19:42 +0300307 err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_FRAG_THRESHOLD);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200308 if (err)
309 wdev->wiphy->frag_threshold = ofrag;
310
311 return err;
312}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100313EXPORT_WEXT_HANDLER(cfg80211_wext_siwfrag);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200314
315int cfg80211_wext_giwfrag(struct net_device *dev,
316 struct iw_request_info *info,
317 struct iw_param *frag, char *extra)
318{
319 struct wireless_dev *wdev = dev->ieee80211_ptr;
320
321 frag->value = wdev->wiphy->frag_threshold;
322 frag->disabled = frag->value == (u32) -1;
323 frag->fixed = 1;
324
325 return 0;
326}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100327EXPORT_WEXT_HANDLER(cfg80211_wext_giwfrag);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200328
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200329static int cfg80211_wext_siwretry(struct net_device *dev,
330 struct iw_request_info *info,
331 struct iw_param *retry, char *extra)
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200332{
333 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800334 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200335 u32 changed = 0;
336 u8 olong = wdev->wiphy->retry_long;
337 u8 oshort = wdev->wiphy->retry_short;
338 int err;
339
Ujjal Royf5aa0d22014-01-09 21:16:14 +0530340 if (retry->disabled || retry->value < 1 || retry->value > 255 ||
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200341 (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
342 return -EINVAL;
343
344 if (retry->flags & IW_RETRY_LONG) {
345 wdev->wiphy->retry_long = retry->value;
346 changed |= WIPHY_PARAM_RETRY_LONG;
347 } else if (retry->flags & IW_RETRY_SHORT) {
348 wdev->wiphy->retry_short = retry->value;
349 changed |= WIPHY_PARAM_RETRY_SHORT;
350 } else {
351 wdev->wiphy->retry_short = retry->value;
352 wdev->wiphy->retry_long = retry->value;
353 changed |= WIPHY_PARAM_RETRY_LONG;
354 changed |= WIPHY_PARAM_RETRY_SHORT;
355 }
356
Hila Gonene35e4d22012-06-27 17:19:42 +0300357 err = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200358 if (err) {
359 wdev->wiphy->retry_short = oshort;
360 wdev->wiphy->retry_long = olong;
361 }
362
363 return err;
364}
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200365
366int cfg80211_wext_giwretry(struct net_device *dev,
367 struct iw_request_info *info,
368 struct iw_param *retry, char *extra)
369{
370 struct wireless_dev *wdev = dev->ieee80211_ptr;
371
372 retry->disabled = 0;
373
374 if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
375 /*
376 * First return short value, iwconfig will ask long value
377 * later if needed
378 */
Ujjal Roy60a4fe02014-01-06 21:56:11 +0530379 retry->flags |= IW_RETRY_LIMIT | IW_RETRY_SHORT;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200380 retry->value = wdev->wiphy->retry_short;
Ujjal Roy60a4fe02014-01-06 21:56:11 +0530381 if (wdev->wiphy->retry_long == wdev->wiphy->retry_short)
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200382 retry->flags |= IW_RETRY_LONG;
383
384 return 0;
385 }
386
387 if (retry->flags & IW_RETRY_LONG) {
388 retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
389 retry->value = wdev->wiphy->retry_long;
390 }
391
392 return 0;
393}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100394EXPORT_WEXT_HANDLER(cfg80211_wext_giwretry);
Johannes Berg08645122009-05-11 13:54:58 +0200395
Johannes Bergfffd0932009-07-08 14:22:54 +0200396static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
Johannes Berge31b8212010-10-05 19:39:30 +0200397 struct net_device *dev, bool pairwise,
398 const u8 *addr, bool remove, bool tx_key,
399 int idx, struct key_params *params)
Johannes Berg08645122009-05-11 13:54:58 +0200400{
401 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200402 int err, i;
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100403 bool rejoin = false;
Johannes Bergfffd0932009-07-08 14:22:54 +0200404
Johannes Berge31b8212010-10-05 19:39:30 +0200405 if (pairwise && !addr)
406 return -EINVAL;
407
Johannes Berg8f7d99b2016-09-28 23:44:57 +0200408 /*
409 * In many cases we won't actually need this, but it's better
410 * to do it first in case the allocation fails. Don't use wext.
411 */
Johannes Bergfffd0932009-07-08 14:22:54 +0200412 if (!wdev->wext.keys) {
413 wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
Johannes Berg89b706f2016-09-13 16:39:38 +0200414 GFP_KERNEL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200415 if (!wdev->wext.keys)
416 return -ENOMEM;
David Spinadelb8676222016-09-22 23:16:50 +0300417 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
Johannes Bergfffd0932009-07-08 14:22:54 +0200418 wdev->wext.keys->params[i].key =
419 wdev->wext.keys->data[i];
420 }
421
422 if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
423 wdev->iftype != NL80211_IFTYPE_STATION)
424 return -EOPNOTSUPP;
Johannes Berg08645122009-05-11 13:54:58 +0200425
426 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200427 if (!wdev->current_bss)
428 return -ENOLINK;
429
Johannes Berg08645122009-05-11 13:54:58 +0200430 if (!rdev->ops->set_default_mgmt_key)
431 return -EOPNOTSUPP;
432
433 if (idx < 4 || idx > 5)
434 return -EINVAL;
435 } else if (idx < 0 || idx > 3)
436 return -EINVAL;
437
438 if (remove) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200439 err = 0;
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100440 if (wdev->current_bss) {
441 /*
442 * If removing the current TX key, we will need to
443 * join a new IBSS without the privacy bit clear.
444 */
445 if (idx == wdev->wext.default_key &&
446 wdev->iftype == NL80211_IFTYPE_ADHOC) {
447 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
448 rejoin = true;
449 }
Johannes Berge31b8212010-10-05 19:39:30 +0200450
451 if (!pairwise && addr &&
452 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
453 err = -ENOENT;
454 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300455 err = rdev_del_key(rdev, dev, idx, pairwise,
456 addr);
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100457 }
Vivek Natarajand55fb892009-11-24 11:54:10 -0500458 wdev->wext.connect.privacy = false;
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100459 /*
460 * Applications using wireless extensions expect to be
461 * able to delete keys that don't exist, so allow that.
462 */
463 if (err == -ENOENT)
464 err = 0;
Johannes Berg08645122009-05-11 13:54:58 +0200465 if (!err) {
Johannes Berg89b706f2016-09-13 16:39:38 +0200466 if (!addr && idx < 4) {
Johannes Berg538c9eb2014-09-10 13:39:56 +0300467 memset(wdev->wext.keys->data[idx], 0,
468 sizeof(wdev->wext.keys->data[idx]));
Johannes Bergfffd0932009-07-08 14:22:54 +0200469 wdev->wext.keys->params[idx].key_len = 0;
470 wdev->wext.keys->params[idx].cipher = 0;
471 }
Johannes Berg08645122009-05-11 13:54:58 +0200472 if (idx == wdev->wext.default_key)
473 wdev->wext.default_key = -1;
474 else if (idx == wdev->wext.default_mgmt_key)
475 wdev->wext.default_mgmt_key = -1;
476 }
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100477
478 if (!err && rejoin)
479 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Berge3da5742009-05-18 19:56:36 +0200480
Johannes Berg08645122009-05-11 13:54:58 +0200481 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200482 }
Johannes Berg08645122009-05-11 13:54:58 +0200483
Johannes Bergfffd0932009-07-08 14:22:54 +0200484 if (addr)
485 tx_key = false;
Johannes Berg08645122009-05-11 13:54:58 +0200486
Johannes Berge31b8212010-10-05 19:39:30 +0200487 if (cfg80211_validate_key_settings(rdev, params, idx, pairwise, addr))
Johannes Bergfffd0932009-07-08 14:22:54 +0200488 return -EINVAL;
489
490 err = 0;
491 if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300492 err = rdev_add_key(rdev, dev, idx, pairwise, addr, params);
Johannes Berg9381e262016-09-13 16:11:32 +0200493 else if (params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
494 params->cipher != WLAN_CIPHER_SUITE_WEP104)
495 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200496 if (err)
497 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200498
Johannes Berg8f7d99b2016-09-28 23:44:57 +0200499 /*
500 * We only need to store WEP keys, since they're the only keys that
501 * can be be set before a connection is established and persist after
502 * disconnecting.
503 */
504 if (!addr && (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
505 params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200506 wdev->wext.keys->params[idx] = *params;
507 memcpy(wdev->wext.keys->data[idx],
508 params->key, params->key_len);
509 wdev->wext.keys->params[idx].key =
510 wdev->wext.keys->data[idx];
511 }
512
Zhu Yi1f00fca2009-07-20 11:47:43 +0800513 if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
514 params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
Johannes Bergfffd0932009-07-08 14:22:54 +0200515 (tx_key || (!addr && wdev->wext.default_key == -1))) {
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100516 if (wdev->current_bss) {
517 /*
518 * If we are getting a new TX key from not having
519 * had one before we need to join a new IBSS with
520 * the privacy bit set.
521 */
522 if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
523 wdev->wext.default_key == -1) {
524 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
525 rejoin = true;
526 }
Hila Gonene35e4d22012-06-27 17:19:42 +0300527 err = rdev_set_default_key(rdev, dev, idx, true, true);
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100528 }
529 if (!err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200530 wdev->wext.default_key = idx;
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100531 if (rejoin)
532 err = cfg80211_ibss_wext_join(rdev, wdev);
533 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200534 return err;
535 }
Johannes Berg08645122009-05-11 13:54:58 +0200536
Johannes Bergfffd0932009-07-08 14:22:54 +0200537 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
538 (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
539 if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300540 err = rdev_set_default_mgmt_key(rdev, dev, idx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200541 if (!err)
542 wdev->wext.default_mgmt_key = idx;
543 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200544 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200545
546 return 0;
547}
548
549static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
Johannes Berge31b8212010-10-05 19:39:30 +0200550 struct net_device *dev, bool pairwise,
551 const u8 *addr, bool remove, bool tx_key,
552 int idx, struct key_params *params)
Johannes Bergfffd0932009-07-08 14:22:54 +0200553{
554 int err;
555
556 wdev_lock(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +0200557 err = __cfg80211_set_encryption(rdev, dev, pairwise, addr,
558 remove, tx_key, idx, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200559 wdev_unlock(dev->ieee80211_ptr);
560
561 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200562}
563
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200564static int cfg80211_wext_siwencode(struct net_device *dev,
565 struct iw_request_info *info,
566 struct iw_point *erq, char *keybuf)
Johannes Berg08645122009-05-11 13:54:58 +0200567{
568 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800569 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg08645122009-05-11 13:54:58 +0200570 int idx, err;
571 bool remove = false;
572 struct key_params params;
573
Johannes Bergfffd0932009-07-08 14:22:54 +0200574 if (wdev->iftype != NL80211_IFTYPE_STATION &&
575 wdev->iftype != NL80211_IFTYPE_ADHOC)
576 return -EOPNOTSUPP;
577
Johannes Berg08645122009-05-11 13:54:58 +0200578 /* no use -- only MFP (set_default_mgmt_key) is optional */
579 if (!rdev->ops->del_key ||
580 !rdev->ops->add_key ||
581 !rdev->ops->set_default_key)
582 return -EOPNOTSUPP;
583
584 idx = erq->flags & IW_ENCODE_INDEX;
585 if (idx == 0) {
586 idx = wdev->wext.default_key;
587 if (idx < 0)
588 idx = 0;
589 } else if (idx < 1 || idx > 4)
590 return -EINVAL;
591 else
592 idx--;
593
594 if (erq->flags & IW_ENCODE_DISABLED)
595 remove = true;
596 else if (erq->length == 0) {
597 /* No key data - just set the default TX key index */
Johannes Bergfffd0932009-07-08 14:22:54 +0200598 err = 0;
599 wdev_lock(wdev);
600 if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300601 err = rdev_set_default_key(rdev, dev, idx, true,
602 true);
Johannes Berg08645122009-05-11 13:54:58 +0200603 if (!err)
604 wdev->wext.default_key = idx;
Johannes Bergfffd0932009-07-08 14:22:54 +0200605 wdev_unlock(wdev);
Johannes Berg08645122009-05-11 13:54:58 +0200606 return err;
607 }
608
609 memset(&params, 0, sizeof(params));
610 params.key = keybuf;
611 params.key_len = erq->length;
612 if (erq->length == 5)
613 params.cipher = WLAN_CIPHER_SUITE_WEP40;
614 else if (erq->length == 13)
615 params.cipher = WLAN_CIPHER_SUITE_WEP104;
616 else if (!remove)
617 return -EINVAL;
618
Johannes Berge31b8212010-10-05 19:39:30 +0200619 return cfg80211_set_encryption(rdev, dev, false, NULL, remove,
Johannes Berg08645122009-05-11 13:54:58 +0200620 wdev->wext.default_key == -1,
621 idx, &params);
622}
Johannes Berg08645122009-05-11 13:54:58 +0200623
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200624static int cfg80211_wext_siwencodeext(struct net_device *dev,
625 struct iw_request_info *info,
626 struct iw_point *erq, char *extra)
Johannes Berg08645122009-05-11 13:54:58 +0200627{
628 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800629 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg08645122009-05-11 13:54:58 +0200630 struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
631 const u8 *addr;
632 int idx;
633 bool remove = false;
634 struct key_params params;
635 u32 cipher;
636
Johannes Bergfffd0932009-07-08 14:22:54 +0200637 if (wdev->iftype != NL80211_IFTYPE_STATION &&
638 wdev->iftype != NL80211_IFTYPE_ADHOC)
639 return -EOPNOTSUPP;
640
Johannes Berg08645122009-05-11 13:54:58 +0200641 /* no use -- only MFP (set_default_mgmt_key) is optional */
642 if (!rdev->ops->del_key ||
643 !rdev->ops->add_key ||
644 !rdev->ops->set_default_key)
645 return -EOPNOTSUPP;
646
647 switch (ext->alg) {
648 case IW_ENCODE_ALG_NONE:
649 remove = true;
650 cipher = 0;
651 break;
652 case IW_ENCODE_ALG_WEP:
653 if (ext->key_len == 5)
654 cipher = WLAN_CIPHER_SUITE_WEP40;
655 else if (ext->key_len == 13)
656 cipher = WLAN_CIPHER_SUITE_WEP104;
657 else
658 return -EINVAL;
659 break;
660 case IW_ENCODE_ALG_TKIP:
661 cipher = WLAN_CIPHER_SUITE_TKIP;
662 break;
663 case IW_ENCODE_ALG_CCMP:
664 cipher = WLAN_CIPHER_SUITE_CCMP;
665 break;
666 case IW_ENCODE_ALG_AES_CMAC:
667 cipher = WLAN_CIPHER_SUITE_AES_CMAC;
668 break;
669 default:
670 return -EOPNOTSUPP;
671 }
672
673 if (erq->flags & IW_ENCODE_DISABLED)
674 remove = true;
675
676 idx = erq->flags & IW_ENCODE_INDEX;
677 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
678 if (idx < 4 || idx > 5) {
679 idx = wdev->wext.default_mgmt_key;
680 if (idx < 0)
681 return -EINVAL;
682 } else
683 idx--;
684 } else {
685 if (idx < 1 || idx > 4) {
686 idx = wdev->wext.default_key;
687 if (idx < 0)
688 return -EINVAL;
689 } else
690 idx--;
691 }
692
693 addr = ext->addr.sa_data;
694 if (is_broadcast_ether_addr(addr))
695 addr = NULL;
696
697 memset(&params, 0, sizeof(params));
698 params.key = ext->key;
699 params.key_len = ext->key_len;
700 params.cipher = cipher;
701
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300702 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
703 params.seq = ext->rx_seq;
704 params.seq_len = 6;
705 }
706
Johannes Berg08645122009-05-11 13:54:58 +0200707 return cfg80211_set_encryption(
Johannes Berge31b8212010-10-05 19:39:30 +0200708 rdev, dev,
709 !(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
710 addr, remove,
Johannes Berg08645122009-05-11 13:54:58 +0200711 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
712 idx, &params);
713}
Johannes Berg08645122009-05-11 13:54:58 +0200714
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200715static int cfg80211_wext_giwencode(struct net_device *dev,
716 struct iw_request_info *info,
717 struct iw_point *erq, char *keybuf)
Johannes Berg08645122009-05-11 13:54:58 +0200718{
719 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200720 int idx;
Johannes Berg08645122009-05-11 13:54:58 +0200721
Johannes Bergfffd0932009-07-08 14:22:54 +0200722 if (wdev->iftype != NL80211_IFTYPE_STATION &&
723 wdev->iftype != NL80211_IFTYPE_ADHOC)
Johannes Berg08645122009-05-11 13:54:58 +0200724 return -EOPNOTSUPP;
725
726 idx = erq->flags & IW_ENCODE_INDEX;
727 if (idx == 0) {
728 idx = wdev->wext.default_key;
729 if (idx < 0)
730 idx = 0;
731 } else if (idx < 1 || idx > 4)
732 return -EINVAL;
733 else
734 idx--;
735
736 erq->flags = idx + 1;
737
Johannes Bergfffd0932009-07-08 14:22:54 +0200738 if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
Johannes Berg08645122009-05-11 13:54:58 +0200739 erq->flags |= IW_ENCODE_DISABLED;
740 erq->length = 0;
741 return 0;
742 }
743
Johannes Bergfffd0932009-07-08 14:22:54 +0200744 erq->length = min_t(size_t, erq->length,
745 wdev->wext.keys->params[idx].key_len);
746 memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
747 erq->flags |= IW_ENCODE_ENABLED;
748
749 return 0;
Johannes Berg08645122009-05-11 13:54:58 +0200750}
Johannes Berg7643a2c2009-06-02 13:01:39 +0200751
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200752static int cfg80211_wext_siwfreq(struct net_device *dev,
753 struct iw_request_info *info,
754 struct iw_freq *wextfreq, char *extra)
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200755{
756 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800757 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg683b6d32012-11-08 21:25:48 +0100758 struct cfg80211_chan_def chandef = {
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100759 .width = NL80211_CHAN_WIDTH_20_NOHT,
Johannes Berg683b6d32012-11-08 21:25:48 +0100760 };
Johannes Berg5fe231e2013-05-08 21:45:15 +0200761 int freq;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200762
763 switch (wdev->iftype) {
764 case NL80211_IFTYPE_STATION:
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200765 return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200766 case NL80211_IFTYPE_ADHOC:
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200767 return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
Johannes Bergf444de02010-05-05 15:25:02 +0200768 case NL80211_IFTYPE_MONITOR:
Zhao, Gang96998e32014-04-09 09:28:06 +0800769 freq = cfg80211_wext_freq(wextfreq);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200770 if (freq < 0)
771 return freq;
772 if (freq == 0)
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200773 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100774 chandef.center_freq1 = freq;
Johannes Berg683b6d32012-11-08 21:25:48 +0100775 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
776 if (!chandef.chan)
777 return -EINVAL;
Johannes Berg5fe231e2013-05-08 21:45:15 +0200778 return cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +0200779 case NL80211_IFTYPE_MESH_POINT:
Zhao, Gang96998e32014-04-09 09:28:06 +0800780 freq = cfg80211_wext_freq(wextfreq);
Johannes Bergcc1d2802012-05-16 23:50:20 +0200781 if (freq < 0)
782 return freq;
783 if (freq == 0)
784 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100785 chandef.center_freq1 = freq;
Johannes Berg683b6d32012-11-08 21:25:48 +0100786 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
787 if (!chandef.chan)
788 return -EINVAL;
Johannes Berg5fe231e2013-05-08 21:45:15 +0200789 return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergf444de02010-05-05 15:25:02 +0200790 default:
791 return -EOPNOTSUPP;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200792 }
793}
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200794
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200795static int cfg80211_wext_giwfreq(struct net_device *dev,
796 struct iw_request_info *info,
797 struct iw_freq *freq, char *extra)
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200798{
799 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800800 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg683b6d32012-11-08 21:25:48 +0100801 struct cfg80211_chan_def chandef;
802 int ret;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200803
804 switch (wdev->iftype) {
805 case NL80211_IFTYPE_STATION:
806 return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
807 case NL80211_IFTYPE_ADHOC:
808 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
Johannes Berge9998822011-11-09 10:30:21 +0100809 case NL80211_IFTYPE_MONITOR:
Johannes Berg5b7ccaf2012-07-12 19:45:08 +0200810 if (!rdev->ops->get_channel)
Johannes Berge9998822011-11-09 10:30:21 +0100811 return -EINVAL;
812
Johannes Berg683b6d32012-11-08 21:25:48 +0100813 ret = rdev_get_channel(rdev, wdev, &chandef);
814 if (ret)
815 return ret;
816 freq->m = chandef.chan->center_freq;
Johannes Berge9998822011-11-09 10:30:21 +0100817 freq->e = 6;
818 return 0;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200819 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +0200820 return -EINVAL;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200821 }
822}
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200823
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200824static int cfg80211_wext_siwtxpower(struct net_device *dev,
825 struct iw_request_info *info,
826 union iwreq_data *data, char *extra)
Johannes Berg7643a2c2009-06-02 13:01:39 +0200827{
828 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800829 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Juuso Oikarinenfa61cf72010-06-23 12:12:37 +0300830 enum nl80211_tx_power_setting type;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200831 int dbm = 0;
832
833 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
834 return -EINVAL;
835 if (data->txpower.flags & IW_TXPOW_RANGE)
836 return -EINVAL;
837
838 if (!rdev->ops->set_tx_power)
839 return -EOPNOTSUPP;
840
841 /* only change when not disabling */
842 if (!data->txpower.disabled) {
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200843 rfkill_set_sw_state(rdev->rfkill, false);
844
Johannes Berg7643a2c2009-06-02 13:01:39 +0200845 if (data->txpower.fixed) {
846 /*
847 * wext doesn't support negative values, see
848 * below where it's for automatic
849 */
850 if (data->txpower.value < 0)
851 return -EINVAL;
852 dbm = data->txpower.value;
Juuso Oikarinenfa61cf72010-06-23 12:12:37 +0300853 type = NL80211_TX_POWER_FIXED;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200854 /* TODO: do regulatory check! */
855 } else {
856 /*
857 * Automatic power level setting, max being the value
858 * passed in from userland.
859 */
860 if (data->txpower.value < 0) {
Juuso Oikarinenfa61cf72010-06-23 12:12:37 +0300861 type = NL80211_TX_POWER_AUTOMATIC;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200862 } else {
863 dbm = data->txpower.value;
Juuso Oikarinenfa61cf72010-06-23 12:12:37 +0300864 type = NL80211_TX_POWER_LIMITED;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200865 }
866 }
867 } else {
Johannes Berg3cfe91c2019-08-30 14:24:44 +0300868 if (rfkill_set_sw_state(rdev->rfkill, true))
869 schedule_work(&rdev->rfkill_block);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200870 return 0;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200871 }
872
Johannes Bergc8442112012-10-24 10:17:18 +0200873 return rdev_set_tx_power(rdev, wdev, type, DBM_TO_MBM(dbm));
Johannes Berg7643a2c2009-06-02 13:01:39 +0200874}
Johannes Berg7643a2c2009-06-02 13:01:39 +0200875
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200876static int cfg80211_wext_giwtxpower(struct net_device *dev,
877 struct iw_request_info *info,
878 union iwreq_data *data, char *extra)
Johannes Berg7643a2c2009-06-02 13:01:39 +0200879{
880 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800881 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg7643a2c2009-06-02 13:01:39 +0200882 int err, val;
883
884 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
885 return -EINVAL;
886 if (data->txpower.flags & IW_TXPOW_RANGE)
887 return -EINVAL;
888
889 if (!rdev->ops->get_tx_power)
890 return -EOPNOTSUPP;
891
Johannes Bergc8442112012-10-24 10:17:18 +0200892 err = rdev_get_tx_power(rdev, wdev, &val);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200893 if (err)
Johannes Berg7643a2c2009-06-02 13:01:39 +0200894 return err;
895
896 /* well... oh well */
897 data->txpower.fixed = 1;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200898 data->txpower.disabled = rfkill_blocked(rdev->rfkill);
Johannes Berg7643a2c2009-06-02 13:01:39 +0200899 data->txpower.value = val;
900 data->txpower.flags = IW_TXPOW_DBM;
901
902 return 0;
903}
Johannes Bergf2129352009-07-01 21:26:56 +0200904
905static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
906 s32 auth_alg)
907{
908 int nr_alg = 0;
909
910 if (!auth_alg)
911 return -EINVAL;
912
913 if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
914 IW_AUTH_ALG_SHARED_KEY |
915 IW_AUTH_ALG_LEAP))
916 return -EINVAL;
917
918 if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
919 nr_alg++;
920 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
921 }
922
923 if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
924 nr_alg++;
925 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
926 }
927
928 if (auth_alg & IW_AUTH_ALG_LEAP) {
929 nr_alg++;
930 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
931 }
932
933 if (nr_alg > 1)
934 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
935
936 return 0;
937}
938
939static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
940{
Johannes Bergf2129352009-07-01 21:26:56 +0200941 if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
Gábor Stefanik323d5662009-07-12 02:03:48 +0200942 IW_AUTH_WPA_VERSION_WPA2|
943 IW_AUTH_WPA_VERSION_DISABLED))
Johannes Bergf2129352009-07-01 21:26:56 +0200944 return -EINVAL;
945
Gábor Stefanik323d5662009-07-12 02:03:48 +0200946 if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
947 (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
948 IW_AUTH_WPA_VERSION_WPA2)))
949 return -EINVAL;
950
951 if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
952 wdev->wext.connect.crypto.wpa_versions &=
953 ~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
954
Johannes Bergf2129352009-07-01 21:26:56 +0200955 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
956 wdev->wext.connect.crypto.wpa_versions |=
957 NL80211_WPA_VERSION_1;
958
959 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
960 wdev->wext.connect.crypto.wpa_versions |=
961 NL80211_WPA_VERSION_2;
962
963 return 0;
964}
965
Johannes Berg4f5dadc2009-07-07 03:56:10 +0200966static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
Johannes Bergf2129352009-07-01 21:26:56 +0200967{
Johannes Bergf2129352009-07-01 21:26:56 +0200968 if (cipher & IW_AUTH_CIPHER_WEP40)
969 wdev->wext.connect.crypto.cipher_group =
970 WLAN_CIPHER_SUITE_WEP40;
971 else if (cipher & IW_AUTH_CIPHER_WEP104)
972 wdev->wext.connect.crypto.cipher_group =
973 WLAN_CIPHER_SUITE_WEP104;
974 else if (cipher & IW_AUTH_CIPHER_TKIP)
975 wdev->wext.connect.crypto.cipher_group =
976 WLAN_CIPHER_SUITE_TKIP;
977 else if (cipher & IW_AUTH_CIPHER_CCMP)
978 wdev->wext.connect.crypto.cipher_group =
979 WLAN_CIPHER_SUITE_CCMP;
980 else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
981 wdev->wext.connect.crypto.cipher_group =
982 WLAN_CIPHER_SUITE_AES_CMAC;
Jouni Malinenc5f82892009-11-01 11:18:49 +0200983 else if (cipher & IW_AUTH_CIPHER_NONE)
984 wdev->wext.connect.crypto.cipher_group = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200985 else
986 return -EINVAL;
987
988 return 0;
989}
990
Johannes Berg4f5dadc2009-07-07 03:56:10 +0200991static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
Johannes Bergf2129352009-07-01 21:26:56 +0200992{
993 int nr_ciphers = 0;
994 u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
995
996 if (cipher & IW_AUTH_CIPHER_WEP40) {
997 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
998 nr_ciphers++;
999 }
1000
1001 if (cipher & IW_AUTH_CIPHER_WEP104) {
1002 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
1003 nr_ciphers++;
1004 }
1005
1006 if (cipher & IW_AUTH_CIPHER_TKIP) {
1007 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
1008 nr_ciphers++;
1009 }
1010
1011 if (cipher & IW_AUTH_CIPHER_CCMP) {
1012 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
1013 nr_ciphers++;
1014 }
1015
1016 if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
1017 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
1018 nr_ciphers++;
1019 }
1020
1021 BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
1022
1023 wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1024
1025 return 0;
1026}
1027
1028
Johannes Berg4f5dadc2009-07-07 03:56:10 +02001029static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
Johannes Bergf2129352009-07-01 21:26:56 +02001030{
1031 int nr_akm_suites = 0;
1032
1033 if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1034 IW_AUTH_KEY_MGMT_PSK))
1035 return -EINVAL;
1036
1037 if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1038 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1039 WLAN_AKM_SUITE_8021X;
1040 nr_akm_suites++;
1041 }
1042
1043 if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1044 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1045 WLAN_AKM_SUITE_PSK;
1046 nr_akm_suites++;
1047 }
1048
1049 wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1050
1051 return 0;
1052}
1053
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001054static int cfg80211_wext_siwauth(struct net_device *dev,
1055 struct iw_request_info *info,
1056 struct iw_param *data, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +02001057{
1058 struct wireless_dev *wdev = dev->ieee80211_ptr;
1059
1060 if (wdev->iftype != NL80211_IFTYPE_STATION)
1061 return -EOPNOTSUPP;
1062
1063 switch (data->flags & IW_AUTH_INDEX) {
1064 case IW_AUTH_PRIVACY_INVOKED:
1065 wdev->wext.connect.privacy = data->value;
1066 return 0;
1067 case IW_AUTH_WPA_VERSION:
1068 return cfg80211_set_wpa_version(wdev, data->value);
1069 case IW_AUTH_CIPHER_GROUP:
1070 return cfg80211_set_cipher_group(wdev, data->value);
1071 case IW_AUTH_KEY_MGMT:
1072 return cfg80211_set_key_mgt(wdev, data->value);
1073 case IW_AUTH_CIPHER_PAIRWISE:
1074 return cfg80211_set_cipher_pairwise(wdev, data->value);
1075 case IW_AUTH_80211_AUTH_ALG:
1076 return cfg80211_set_auth_alg(wdev, data->value);
1077 case IW_AUTH_WPA_ENABLED:
1078 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1079 case IW_AUTH_DROP_UNENCRYPTED:
1080 case IW_AUTH_MFP:
1081 return 0;
1082 default:
1083 return -EOPNOTSUPP;
1084 }
1085}
Johannes Bergf2129352009-07-01 21:26:56 +02001086
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001087static int cfg80211_wext_giwauth(struct net_device *dev,
1088 struct iw_request_info *info,
1089 struct iw_param *data, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +02001090{
1091 /* XXX: what do we need? */
1092
1093 return -EOPNOTSUPP;
1094}
Johannes Bergbc92afd2009-07-01 21:26:57 +02001095
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001096static int cfg80211_wext_siwpower(struct net_device *dev,
1097 struct iw_request_info *info,
1098 struct iw_param *wrq, char *extra)
Johannes Bergbc92afd2009-07-01 21:26:57 +02001099{
1100 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001101 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Kalle Valoffb9eb32010-02-17 17:58:10 +02001102 bool ps = wdev->ps;
1103 int timeout = wdev->ps_timeout;
Johannes Bergbc92afd2009-07-01 21:26:57 +02001104 int err;
1105
1106 if (wdev->iftype != NL80211_IFTYPE_STATION)
1107 return -EINVAL;
1108
1109 if (!rdev->ops->set_power_mgmt)
1110 return -EOPNOTSUPP;
1111
1112 if (wrq->disabled) {
1113 ps = false;
1114 } else {
1115 switch (wrq->flags & IW_POWER_MODE) {
1116 case IW_POWER_ON: /* If not specified */
1117 case IW_POWER_MODE: /* If set all mask */
1118 case IW_POWER_ALL_R: /* If explicitely state all */
1119 ps = true;
1120 break;
1121 default: /* Otherwise we ignore */
1122 return -EINVAL;
1123 }
1124
1125 if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1126 return -EINVAL;
1127
1128 if (wrq->flags & IW_POWER_TIMEOUT)
1129 timeout = wrq->value / 1000;
1130 }
1131
Hila Gonene35e4d22012-06-27 17:19:42 +03001132 err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
Johannes Bergbc92afd2009-07-01 21:26:57 +02001133 if (err)
1134 return err;
1135
Kalle Valoffb9eb32010-02-17 17:58:10 +02001136 wdev->ps = ps;
1137 wdev->ps_timeout = timeout;
Johannes Bergbc92afd2009-07-01 21:26:57 +02001138
1139 return 0;
1140
1141}
Johannes Bergbc92afd2009-07-01 21:26:57 +02001142
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001143static int cfg80211_wext_giwpower(struct net_device *dev,
1144 struct iw_request_info *info,
1145 struct iw_param *wrq, char *extra)
Johannes Bergbc92afd2009-07-01 21:26:57 +02001146{
1147 struct wireless_dev *wdev = dev->ieee80211_ptr;
1148
Kalle Valoffb9eb32010-02-17 17:58:10 +02001149 wrq->disabled = !wdev->ps;
Johannes Bergbc92afd2009-07-01 21:26:57 +02001150
1151 return 0;
1152}
Johannes Bergab737a42009-07-01 21:26:58 +02001153
Johannes Berg562e4822009-07-27 12:01:51 +02001154static int cfg80211_wds_wext_siwap(struct net_device *dev,
1155 struct iw_request_info *info,
1156 struct sockaddr *addr, char *extra)
Johannes Bergab737a42009-07-01 21:26:58 +02001157{
1158 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001159 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergab737a42009-07-01 21:26:58 +02001160 int err;
1161
1162 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1163 return -EINVAL;
1164
1165 if (addr->sa_family != ARPHRD_ETHER)
1166 return -EINVAL;
1167
1168 if (netif_running(dev))
1169 return -EBUSY;
1170
1171 if (!rdev->ops->set_wds_peer)
1172 return -EOPNOTSUPP;
1173
Hila Gonene35e4d22012-06-27 17:19:42 +03001174 err = rdev_set_wds_peer(rdev, dev, (u8 *)&addr->sa_data);
Johannes Bergab737a42009-07-01 21:26:58 +02001175 if (err)
1176 return err;
1177
1178 memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);
1179
1180 return 0;
1181}
Johannes Bergab737a42009-07-01 21:26:58 +02001182
Johannes Berg562e4822009-07-27 12:01:51 +02001183static int cfg80211_wds_wext_giwap(struct net_device *dev,
1184 struct iw_request_info *info,
1185 struct sockaddr *addr, char *extra)
Johannes Bergab737a42009-07-01 21:26:58 +02001186{
1187 struct wireless_dev *wdev = dev->ieee80211_ptr;
1188
1189 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1190 return -EINVAL;
1191
1192 addr->sa_family = ARPHRD_ETHER;
1193 memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);
1194
1195 return 0;
1196}
Johannes Berg99303802009-07-01 21:26:59 +02001197
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001198static int cfg80211_wext_siwrate(struct net_device *dev,
1199 struct iw_request_info *info,
1200 struct iw_param *rate, char *extra)
Johannes Berg99303802009-07-01 21:26:59 +02001201{
1202 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001203 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg99303802009-07-01 21:26:59 +02001204 struct cfg80211_bitrate_mask mask;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001205 u32 fixed, maxrate;
1206 struct ieee80211_supported_band *sband;
1207 int band, ridx;
1208 bool match = false;
Johannes Berg99303802009-07-01 21:26:59 +02001209
1210 if (!rdev->ops->set_bitrate_mask)
1211 return -EOPNOTSUPP;
1212
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001213 memset(&mask, 0, sizeof(mask));
1214 fixed = 0;
John W. Linville54233262010-01-27 09:44:48 -05001215 maxrate = (u32)-1;
Johannes Berg99303802009-07-01 21:26:59 +02001216
1217 if (rate->value < 0) {
1218 /* nothing */
1219 } else if (rate->fixed) {
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001220 fixed = rate->value / 100000;
Johannes Berg99303802009-07-01 21:26:59 +02001221 } else {
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001222 maxrate = rate->value / 100000;
Johannes Berg99303802009-07-01 21:26:59 +02001223 }
1224
Johannes Berg57fbcce2016-04-12 15:56:15 +02001225 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001226 sband = wdev->wiphy->bands[band];
1227 if (sband == NULL)
1228 continue;
1229 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
1230 struct ieee80211_rate *srate = &sband->bitrates[ridx];
1231 if (fixed == srate->bitrate) {
1232 mask.control[band].legacy = 1 << ridx;
1233 match = true;
1234 break;
1235 }
1236 if (srate->bitrate <= maxrate) {
1237 mask.control[band].legacy |= 1 << ridx;
1238 match = true;
1239 }
1240 }
1241 }
1242
1243 if (!match)
1244 return -EINVAL;
1245
Hila Gonene35e4d22012-06-27 17:19:42 +03001246 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Johannes Berg99303802009-07-01 21:26:59 +02001247}
Johannes Berg99303802009-07-01 21:26:59 +02001248
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001249static int cfg80211_wext_giwrate(struct net_device *dev,
1250 struct iw_request_info *info,
1251 struct iw_param *rate, char *extra)
Johannes Berg99303802009-07-01 21:26:59 +02001252{
1253 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001254 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg5762d7d2018-01-16 23:20:22 +01001255 struct station_info sinfo = {};
Johannes Berga71d62d2009-07-07 23:41:27 +02001256 u8 addr[ETH_ALEN];
Johannes Berg99303802009-07-01 21:26:59 +02001257 int err;
1258
1259 if (wdev->iftype != NL80211_IFTYPE_STATION)
1260 return -EOPNOTSUPP;
1261
1262 if (!rdev->ops->get_station)
1263 return -EOPNOTSUPP;
1264
Johannes Berga71d62d2009-07-07 23:41:27 +02001265 err = 0;
1266 wdev_lock(wdev);
Samuel Ortiz6c230c02009-07-03 02:00:48 +02001267 if (wdev->current_bss)
Johannes Berga71d62d2009-07-07 23:41:27 +02001268 memcpy(addr, wdev->current_bss->pub.bssid, ETH_ALEN);
Samuel Ortiz6c230c02009-07-03 02:00:48 +02001269 else
Johannes Berga71d62d2009-07-07 23:41:27 +02001270 err = -EOPNOTSUPP;
1271 wdev_unlock(wdev);
1272 if (err)
1273 return err;
Johannes Berg99303802009-07-01 21:26:59 +02001274
Hila Gonene35e4d22012-06-27 17:19:42 +03001275 err = rdev_get_station(rdev, dev, addr, &sinfo);
Johannes Berg99303802009-07-01 21:26:59 +02001276 if (err)
1277 return err;
1278
Stefan Seyfried848e6162018-09-30 12:53:00 +02001279 if (!(sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
1280 err = -EOPNOTSUPP;
1281 goto free;
1282 }
Johannes Berg99303802009-07-01 21:26:59 +02001283
John W. Linville254416a2009-12-09 16:43:52 -05001284 rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
Johannes Berg99303802009-07-01 21:26:59 +02001285
Stefan Seyfried848e6162018-09-30 12:53:00 +02001286free:
1287 cfg80211_sinfo_release_content(&sinfo);
1288 return err;
Johannes Berg99303802009-07-01 21:26:59 +02001289}
Johannes Berg89906462009-07-01 21:27:00 +02001290
1291/* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001292static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
Johannes Berg89906462009-07-01 21:27:00 +02001293{
1294 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001295 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg89906462009-07-01 21:27:00 +02001296 /* we are under RTNL - globally locked - so can use static structs */
1297 static struct iw_statistics wstats;
Stefan Seyfried848e6162018-09-30 12:53:00 +02001298 static struct station_info sinfo = {};
Johannes Bergc56c5712009-07-10 16:54:07 +02001299 u8 bssid[ETH_ALEN];
Johannes Berg89906462009-07-01 21:27:00 +02001300
1301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
1302 return NULL;
1303
1304 if (!rdev->ops->get_station)
1305 return NULL;
1306
Johannes Bergc56c5712009-07-10 16:54:07 +02001307 /* Grab BSSID of current BSS, if any */
1308 wdev_lock(wdev);
1309 if (!wdev->current_bss) {
1310 wdev_unlock(wdev);
Johannes Berg89906462009-07-01 21:27:00 +02001311 return NULL;
Johannes Bergc56c5712009-07-10 16:54:07 +02001312 }
1313 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
1314 wdev_unlock(wdev);
Johannes Berg89906462009-07-01 21:27:00 +02001315
Johannes Berg9c5a18a2015-06-09 21:35:44 +02001316 memset(&sinfo, 0, sizeof(sinfo));
1317
Hila Gonene35e4d22012-06-27 17:19:42 +03001318 if (rdev_get_station(rdev, dev, bssid, &sinfo))
Johannes Berg89906462009-07-01 21:27:00 +02001319 return NULL;
1320
1321 memset(&wstats, 0, sizeof(wstats));
1322
1323 switch (rdev->wiphy.signal_type) {
1324 case CFG80211_SIGNAL_TYPE_MBM:
Omer Efrat397c6572018-06-17 13:06:14 +03001325 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
Johannes Berg89906462009-07-01 21:27:00 +02001326 int sig = sinfo.signal;
1327 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1328 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1329 wstats.qual.updated |= IW_QUAL_DBM;
1330 wstats.qual.level = sig;
1331 if (sig < -110)
1332 sig = -110;
1333 else if (sig > -40)
1334 sig = -40;
1335 wstats.qual.qual = sig + 110;
1336 break;
1337 }
Gustavo A. R. Silva4af21752019-01-23 01:03:32 -06001338 /* fall through */
Johannes Berg89906462009-07-01 21:27:00 +02001339 case CFG80211_SIGNAL_TYPE_UNSPEC:
Omer Efrat397c6572018-06-17 13:06:14 +03001340 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
Johannes Berg89906462009-07-01 21:27:00 +02001341 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1342 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1343 wstats.qual.level = sinfo.signal;
1344 wstats.qual.qual = sinfo.signal;
1345 break;
1346 }
Gustavo A. R. Silva4af21752019-01-23 01:03:32 -06001347 /* fall through */
Johannes Berg89906462009-07-01 21:27:00 +02001348 default:
1349 wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1350 wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1351 }
1352
1353 wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
Omer Efrat397c6572018-06-17 13:06:14 +03001354 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC))
Ben Greear5a5c7312010-10-07 16:39:20 -07001355 wstats.discard.misc = sinfo.rx_dropped_misc;
Omer Efrat397c6572018-06-17 13:06:14 +03001356 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))
Ben Greear5a5c7312010-10-07 16:39:20 -07001357 wstats.discard.retries = sinfo.tx_failed;
Johannes Berg89906462009-07-01 21:27:00 +02001358
Stefan Seyfried848e6162018-09-30 12:53:00 +02001359 cfg80211_sinfo_release_content(&sinfo);
1360
Johannes Berg89906462009-07-01 21:27:00 +02001361 return &wstats;
1362}
Johannes Berg562e4822009-07-27 12:01:51 +02001363
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001364static int cfg80211_wext_siwap(struct net_device *dev,
1365 struct iw_request_info *info,
1366 struct sockaddr *ap_addr, char *extra)
Johannes Berg562e4822009-07-27 12:01:51 +02001367{
1368 struct wireless_dev *wdev = dev->ieee80211_ptr;
1369
1370 switch (wdev->iftype) {
1371 case NL80211_IFTYPE_ADHOC:
1372 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1373 case NL80211_IFTYPE_STATION:
1374 return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1375 case NL80211_IFTYPE_WDS:
1376 return cfg80211_wds_wext_siwap(dev, info, ap_addr, extra);
1377 default:
1378 return -EOPNOTSUPP;
1379 }
1380}
Johannes Berg562e4822009-07-27 12:01:51 +02001381
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001382static int cfg80211_wext_giwap(struct net_device *dev,
1383 struct iw_request_info *info,
1384 struct sockaddr *ap_addr, char *extra)
Johannes Berg562e4822009-07-27 12:01:51 +02001385{
1386 struct wireless_dev *wdev = dev->ieee80211_ptr;
1387
1388 switch (wdev->iftype) {
1389 case NL80211_IFTYPE_ADHOC:
1390 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1391 case NL80211_IFTYPE_STATION:
1392 return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1393 case NL80211_IFTYPE_WDS:
1394 return cfg80211_wds_wext_giwap(dev, info, ap_addr, extra);
1395 default:
1396 return -EOPNOTSUPP;
1397 }
1398}
Johannes Berg1f9298f2009-07-27 12:01:52 +02001399
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001400static int cfg80211_wext_siwessid(struct net_device *dev,
1401 struct iw_request_info *info,
1402 struct iw_point *data, char *ssid)
Johannes Berg1f9298f2009-07-27 12:01:52 +02001403{
1404 struct wireless_dev *wdev = dev->ieee80211_ptr;
1405
1406 switch (wdev->iftype) {
1407 case NL80211_IFTYPE_ADHOC:
1408 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1409 case NL80211_IFTYPE_STATION:
1410 return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
1411 default:
1412 return -EOPNOTSUPP;
1413 }
1414}
Johannes Berg1f9298f2009-07-27 12:01:52 +02001415
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001416static int cfg80211_wext_giwessid(struct net_device *dev,
1417 struct iw_request_info *info,
1418 struct iw_point *data, char *ssid)
Johannes Berg1f9298f2009-07-27 12:01:52 +02001419{
1420 struct wireless_dev *wdev = dev->ieee80211_ptr;
1421
Johannes Berg42da2f92010-08-30 12:24:54 +02001422 data->flags = 0;
1423 data->length = 0;
1424
Johannes Berg1f9298f2009-07-27 12:01:52 +02001425 switch (wdev->iftype) {
1426 case NL80211_IFTYPE_ADHOC:
1427 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1428 case NL80211_IFTYPE_STATION:
1429 return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1430 default:
1431 return -EOPNOTSUPP;
1432 }
1433}
Johannes Berga9a11622009-07-27 12:01:53 +02001434
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001435static int cfg80211_wext_siwpmksa(struct net_device *dev,
1436 struct iw_request_info *info,
1437 struct iw_point *data, char *extra)
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001438{
1439 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001440 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001441 struct cfg80211_pmksa cfg_pmksa;
1442 struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
1443
1444 memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
1445
1446 if (wdev->iftype != NL80211_IFTYPE_STATION)
1447 return -EINVAL;
1448
1449 cfg_pmksa.bssid = pmksa->bssid.sa_data;
1450 cfg_pmksa.pmkid = pmksa->pmkid;
1451
1452 switch (pmksa->cmd) {
1453 case IW_PMKSA_ADD:
1454 if (!rdev->ops->set_pmksa)
1455 return -EOPNOTSUPP;
1456
Hila Gonene35e4d22012-06-27 17:19:42 +03001457 return rdev_set_pmksa(rdev, dev, &cfg_pmksa);
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001458
1459 case IW_PMKSA_REMOVE:
1460 if (!rdev->ops->del_pmksa)
1461 return -EOPNOTSUPP;
1462
Hila Gonene35e4d22012-06-27 17:19:42 +03001463 return rdev_del_pmksa(rdev, dev, &cfg_pmksa);
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001464
1465 case IW_PMKSA_FLUSH:
1466 if (!rdev->ops->flush_pmksa)
1467 return -EOPNOTSUPP;
1468
Hila Gonene35e4d22012-06-27 17:19:42 +03001469 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001470
1471 default:
1472 return -EOPNOTSUPP;
1473 }
1474}
1475
Johannes Berga9a11622009-07-27 12:01:53 +02001476static const iw_handler cfg80211_handlers[] = {
1477 [IW_IOCTL_IDX(SIOCGIWNAME)] = (iw_handler) cfg80211_wext_giwname,
1478 [IW_IOCTL_IDX(SIOCSIWFREQ)] = (iw_handler) cfg80211_wext_siwfreq,
1479 [IW_IOCTL_IDX(SIOCGIWFREQ)] = (iw_handler) cfg80211_wext_giwfreq,
1480 [IW_IOCTL_IDX(SIOCSIWMODE)] = (iw_handler) cfg80211_wext_siwmode,
1481 [IW_IOCTL_IDX(SIOCGIWMODE)] = (iw_handler) cfg80211_wext_giwmode,
1482 [IW_IOCTL_IDX(SIOCGIWRANGE)] = (iw_handler) cfg80211_wext_giwrange,
1483 [IW_IOCTL_IDX(SIOCSIWAP)] = (iw_handler) cfg80211_wext_siwap,
1484 [IW_IOCTL_IDX(SIOCGIWAP)] = (iw_handler) cfg80211_wext_giwap,
1485 [IW_IOCTL_IDX(SIOCSIWMLME)] = (iw_handler) cfg80211_wext_siwmlme,
1486 [IW_IOCTL_IDX(SIOCSIWSCAN)] = (iw_handler) cfg80211_wext_siwscan,
1487 [IW_IOCTL_IDX(SIOCGIWSCAN)] = (iw_handler) cfg80211_wext_giwscan,
1488 [IW_IOCTL_IDX(SIOCSIWESSID)] = (iw_handler) cfg80211_wext_siwessid,
1489 [IW_IOCTL_IDX(SIOCGIWESSID)] = (iw_handler) cfg80211_wext_giwessid,
1490 [IW_IOCTL_IDX(SIOCSIWRATE)] = (iw_handler) cfg80211_wext_siwrate,
1491 [IW_IOCTL_IDX(SIOCGIWRATE)] = (iw_handler) cfg80211_wext_giwrate,
1492 [IW_IOCTL_IDX(SIOCSIWRTS)] = (iw_handler) cfg80211_wext_siwrts,
1493 [IW_IOCTL_IDX(SIOCGIWRTS)] = (iw_handler) cfg80211_wext_giwrts,
1494 [IW_IOCTL_IDX(SIOCSIWFRAG)] = (iw_handler) cfg80211_wext_siwfrag,
1495 [IW_IOCTL_IDX(SIOCGIWFRAG)] = (iw_handler) cfg80211_wext_giwfrag,
1496 [IW_IOCTL_IDX(SIOCSIWTXPOW)] = (iw_handler) cfg80211_wext_siwtxpower,
1497 [IW_IOCTL_IDX(SIOCGIWTXPOW)] = (iw_handler) cfg80211_wext_giwtxpower,
1498 [IW_IOCTL_IDX(SIOCSIWRETRY)] = (iw_handler) cfg80211_wext_siwretry,
1499 [IW_IOCTL_IDX(SIOCGIWRETRY)] = (iw_handler) cfg80211_wext_giwretry,
1500 [IW_IOCTL_IDX(SIOCSIWENCODE)] = (iw_handler) cfg80211_wext_siwencode,
1501 [IW_IOCTL_IDX(SIOCGIWENCODE)] = (iw_handler) cfg80211_wext_giwencode,
1502 [IW_IOCTL_IDX(SIOCSIWPOWER)] = (iw_handler) cfg80211_wext_siwpower,
1503 [IW_IOCTL_IDX(SIOCGIWPOWER)] = (iw_handler) cfg80211_wext_giwpower,
1504 [IW_IOCTL_IDX(SIOCSIWGENIE)] = (iw_handler) cfg80211_wext_siwgenie,
1505 [IW_IOCTL_IDX(SIOCSIWAUTH)] = (iw_handler) cfg80211_wext_siwauth,
1506 [IW_IOCTL_IDX(SIOCGIWAUTH)] = (iw_handler) cfg80211_wext_giwauth,
1507 [IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= (iw_handler) cfg80211_wext_siwencodeext,
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001508 [IW_IOCTL_IDX(SIOCSIWPMKSA)] = (iw_handler) cfg80211_wext_siwpmksa,
Johannes Berga9a11622009-07-27 12:01:53 +02001509};
1510
1511const struct iw_handler_def cfg80211_wext_handler = {
1512 .num_standard = ARRAY_SIZE(cfg80211_handlers),
1513 .standard = cfg80211_handlers,
1514 .get_wireless_stats = cfg80211_wireless_stats,
1515};