blob: aa918d7ff6bd0c55fb4a523b78b9bafc43204458 [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"
Johannes Berg04a773a2009-04-19 21:24:32 +0200223 * @freq: the wext freq encoding
224 *
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200225 * Returns a frequency, or a negative error code, or 0 for auto.
Johannes Berg04a773a2009-04-19 21:24:32 +0200226 */
Zhao, Gang96998e32014-04-09 09:28:06 +0800227int cfg80211_wext_freq(struct iw_freq *freq)
Johannes Berg04a773a2009-04-19 21:24:32 +0200228{
Johannes Berg0b258582009-05-08 09:42:33 +0200229 /*
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200230 * Parse frequency - return 0 for auto and
Johannes Berg0b258582009-05-08 09:42:33 +0200231 * -EINVAL for impossible things.
232 */
Johannes Berg04a773a2009-04-19 21:24:32 +0200233 if (freq->e == 0) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200234 enum nl80211_band band = NL80211_BAND_2GHZ;
Johannes Berg04a773a2009-04-19 21:24:32 +0200235 if (freq->m < 0)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200236 return 0;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900237 if (freq->m > 14)
Johannes Berg57fbcce2016-04-12 15:56:15 +0200238 band = NL80211_BAND_5GHZ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900239 return ieee80211_channel_to_frequency(freq->m, band);
Johannes Berg04a773a2009-04-19 21:24:32 +0200240 } else {
241 int i, div = 1000000;
242 for (i = 0; i < freq->e; i++)
243 div /= 10;
Johannes Berg0b258582009-05-08 09:42:33 +0200244 if (div <= 0)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200245 return -EINVAL;
246 return freq->m / div;
Johannes Berg04a773a2009-04-19 21:24:32 +0200247 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200248}
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200249
250int cfg80211_wext_siwrts(struct net_device *dev,
251 struct iw_request_info *info,
252 struct iw_param *rts, char *extra)
253{
254 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800255 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200256 u32 orts = wdev->wiphy->rts_threshold;
257 int err;
258
259 if (rts->disabled || !rts->fixed)
260 wdev->wiphy->rts_threshold = (u32) -1;
261 else if (rts->value < 0)
262 return -EINVAL;
263 else
264 wdev->wiphy->rts_threshold = rts->value;
265
Hila Gonene35e4d22012-06-27 17:19:42 +0300266 err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_RTS_THRESHOLD);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200267 if (err)
268 wdev->wiphy->rts_threshold = orts;
269
270 return err;
271}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100272EXPORT_WEXT_HANDLER(cfg80211_wext_siwrts);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200273
274int cfg80211_wext_giwrts(struct net_device *dev,
275 struct iw_request_info *info,
276 struct iw_param *rts, char *extra)
277{
278 struct wireless_dev *wdev = dev->ieee80211_ptr;
279
280 rts->value = wdev->wiphy->rts_threshold;
281 rts->disabled = rts->value == (u32) -1;
282 rts->fixed = 1;
283
284 return 0;
285}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100286EXPORT_WEXT_HANDLER(cfg80211_wext_giwrts);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200287
288int cfg80211_wext_siwfrag(struct net_device *dev,
289 struct iw_request_info *info,
290 struct iw_param *frag, char *extra)
291{
292 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800293 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200294 u32 ofrag = wdev->wiphy->frag_threshold;
295 int err;
296
297 if (frag->disabled || !frag->fixed)
298 wdev->wiphy->frag_threshold = (u32) -1;
299 else if (frag->value < 256)
300 return -EINVAL;
301 else {
302 /* Fragment length must be even, so strip LSB. */
303 wdev->wiphy->frag_threshold = frag->value & ~0x1;
304 }
305
Hila Gonene35e4d22012-06-27 17:19:42 +0300306 err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_FRAG_THRESHOLD);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200307 if (err)
308 wdev->wiphy->frag_threshold = ofrag;
309
310 return err;
311}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100312EXPORT_WEXT_HANDLER(cfg80211_wext_siwfrag);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200313
314int cfg80211_wext_giwfrag(struct net_device *dev,
315 struct iw_request_info *info,
316 struct iw_param *frag, char *extra)
317{
318 struct wireless_dev *wdev = dev->ieee80211_ptr;
319
320 frag->value = wdev->wiphy->frag_threshold;
321 frag->disabled = frag->value == (u32) -1;
322 frag->fixed = 1;
323
324 return 0;
325}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100326EXPORT_WEXT_HANDLER(cfg80211_wext_giwfrag);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200327
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200328static int cfg80211_wext_siwretry(struct net_device *dev,
329 struct iw_request_info *info,
330 struct iw_param *retry, char *extra)
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200331{
332 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800333 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200334 u32 changed = 0;
335 u8 olong = wdev->wiphy->retry_long;
336 u8 oshort = wdev->wiphy->retry_short;
337 int err;
338
Ujjal Royf5aa0d22014-01-09 21:16:14 +0530339 if (retry->disabled || retry->value < 1 || retry->value > 255 ||
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200340 (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
341 return -EINVAL;
342
343 if (retry->flags & IW_RETRY_LONG) {
344 wdev->wiphy->retry_long = retry->value;
345 changed |= WIPHY_PARAM_RETRY_LONG;
346 } else if (retry->flags & IW_RETRY_SHORT) {
347 wdev->wiphy->retry_short = retry->value;
348 changed |= WIPHY_PARAM_RETRY_SHORT;
349 } else {
350 wdev->wiphy->retry_short = retry->value;
351 wdev->wiphy->retry_long = retry->value;
352 changed |= WIPHY_PARAM_RETRY_LONG;
353 changed |= WIPHY_PARAM_RETRY_SHORT;
354 }
355
Hila Gonene35e4d22012-06-27 17:19:42 +0300356 err = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200357 if (err) {
358 wdev->wiphy->retry_short = oshort;
359 wdev->wiphy->retry_long = olong;
360 }
361
362 return err;
363}
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200364
365int cfg80211_wext_giwretry(struct net_device *dev,
366 struct iw_request_info *info,
367 struct iw_param *retry, char *extra)
368{
369 struct wireless_dev *wdev = dev->ieee80211_ptr;
370
371 retry->disabled = 0;
372
373 if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
374 /*
375 * First return short value, iwconfig will ask long value
376 * later if needed
377 */
Ujjal Roy60a4fe02014-01-06 21:56:11 +0530378 retry->flags |= IW_RETRY_LIMIT | IW_RETRY_SHORT;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200379 retry->value = wdev->wiphy->retry_short;
Ujjal Roy60a4fe02014-01-06 21:56:11 +0530380 if (wdev->wiphy->retry_long == wdev->wiphy->retry_short)
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200381 retry->flags |= IW_RETRY_LONG;
382
383 return 0;
384 }
385
386 if (retry->flags & IW_RETRY_LONG) {
387 retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
388 retry->value = wdev->wiphy->retry_long;
389 }
390
391 return 0;
392}
Johannes Berg2afe38d2015-01-06 14:00:53 +0100393EXPORT_WEXT_HANDLER(cfg80211_wext_giwretry);
Johannes Berg08645122009-05-11 13:54:58 +0200394
Johannes Bergfffd0932009-07-08 14:22:54 +0200395static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
Johannes Berge31b8212010-10-05 19:39:30 +0200396 struct net_device *dev, bool pairwise,
397 const u8 *addr, bool remove, bool tx_key,
398 int idx, struct key_params *params)
Johannes Berg08645122009-05-11 13:54:58 +0200399{
400 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200401 int err, i;
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100402 bool rejoin = false;
Johannes Bergfffd0932009-07-08 14:22:54 +0200403
Johannes Berge31b8212010-10-05 19:39:30 +0200404 if (pairwise && !addr)
405 return -EINVAL;
406
Johannes Berg8f7d99b2016-09-28 23:44:57 +0200407 /*
408 * In many cases we won't actually need this, but it's better
409 * to do it first in case the allocation fails. Don't use wext.
410 */
Johannes Bergfffd0932009-07-08 14:22:54 +0200411 if (!wdev->wext.keys) {
412 wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
Johannes Berg89b706f2016-09-13 16:39:38 +0200413 GFP_KERNEL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200414 if (!wdev->wext.keys)
415 return -ENOMEM;
David Spinadelb8676222016-09-22 23:16:50 +0300416 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
Johannes Bergfffd0932009-07-08 14:22:54 +0200417 wdev->wext.keys->params[i].key =
418 wdev->wext.keys->data[i];
419 }
420
421 if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
422 wdev->iftype != NL80211_IFTYPE_STATION)
423 return -EOPNOTSUPP;
Johannes Berg08645122009-05-11 13:54:58 +0200424
425 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200426 if (!wdev->current_bss)
427 return -ENOLINK;
428
Johannes Berg08645122009-05-11 13:54:58 +0200429 if (!rdev->ops->set_default_mgmt_key)
430 return -EOPNOTSUPP;
431
432 if (idx < 4 || idx > 5)
433 return -EINVAL;
434 } else if (idx < 0 || idx > 3)
435 return -EINVAL;
436
437 if (remove) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200438 err = 0;
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100439 if (wdev->current_bss) {
440 /*
441 * If removing the current TX key, we will need to
442 * join a new IBSS without the privacy bit clear.
443 */
444 if (idx == wdev->wext.default_key &&
445 wdev->iftype == NL80211_IFTYPE_ADHOC) {
446 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
447 rejoin = true;
448 }
Johannes Berge31b8212010-10-05 19:39:30 +0200449
450 if (!pairwise && addr &&
451 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
452 err = -ENOENT;
453 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300454 err = rdev_del_key(rdev, dev, idx, pairwise,
455 addr);
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100456 }
Vivek Natarajand55fb892009-11-24 11:54:10 -0500457 wdev->wext.connect.privacy = false;
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100458 /*
459 * Applications using wireless extensions expect to be
460 * able to delete keys that don't exist, so allow that.
461 */
462 if (err == -ENOENT)
463 err = 0;
Johannes Berg08645122009-05-11 13:54:58 +0200464 if (!err) {
Johannes Berg89b706f2016-09-13 16:39:38 +0200465 if (!addr && idx < 4) {
Johannes Berg538c9eb2014-09-10 13:39:56 +0300466 memset(wdev->wext.keys->data[idx], 0,
467 sizeof(wdev->wext.keys->data[idx]));
Johannes Bergfffd0932009-07-08 14:22:54 +0200468 wdev->wext.keys->params[idx].key_len = 0;
469 wdev->wext.keys->params[idx].cipher = 0;
470 }
Johannes Berg08645122009-05-11 13:54:58 +0200471 if (idx == wdev->wext.default_key)
472 wdev->wext.default_key = -1;
473 else if (idx == wdev->wext.default_mgmt_key)
474 wdev->wext.default_mgmt_key = -1;
475 }
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100476
477 if (!err && rejoin)
478 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Berge3da5742009-05-18 19:56:36 +0200479
Johannes Berg08645122009-05-11 13:54:58 +0200480 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200481 }
Johannes Berg08645122009-05-11 13:54:58 +0200482
Johannes Bergfffd0932009-07-08 14:22:54 +0200483 if (addr)
484 tx_key = false;
Johannes Berg08645122009-05-11 13:54:58 +0200485
Johannes Berge31b8212010-10-05 19:39:30 +0200486 if (cfg80211_validate_key_settings(rdev, params, idx, pairwise, addr))
Johannes Bergfffd0932009-07-08 14:22:54 +0200487 return -EINVAL;
488
489 err = 0;
490 if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300491 err = rdev_add_key(rdev, dev, idx, pairwise, addr, params);
Johannes Berg9381e262016-09-13 16:11:32 +0200492 else if (params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
493 params->cipher != WLAN_CIPHER_SUITE_WEP104)
494 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200495 if (err)
496 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200497
Johannes Berg8f7d99b2016-09-28 23:44:57 +0200498 /*
499 * We only need to store WEP keys, since they're the only keys that
500 * can be be set before a connection is established and persist after
501 * disconnecting.
502 */
503 if (!addr && (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
504 params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200505 wdev->wext.keys->params[idx] = *params;
506 memcpy(wdev->wext.keys->data[idx],
507 params->key, params->key_len);
508 wdev->wext.keys->params[idx].key =
509 wdev->wext.keys->data[idx];
510 }
511
Zhu Yi1f00fca2009-07-20 11:47:43 +0800512 if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
513 params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
Johannes Bergfffd0932009-07-08 14:22:54 +0200514 (tx_key || (!addr && wdev->wext.default_key == -1))) {
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100515 if (wdev->current_bss) {
516 /*
517 * If we are getting a new TX key from not having
518 * had one before we need to join a new IBSS with
519 * the privacy bit set.
520 */
521 if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
522 wdev->wext.default_key == -1) {
523 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
524 rejoin = true;
525 }
Hila Gonene35e4d22012-06-27 17:19:42 +0300526 err = rdev_set_default_key(rdev, dev, idx, true, true);
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100527 }
528 if (!err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200529 wdev->wext.default_key = idx;
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100530 if (rejoin)
531 err = cfg80211_ibss_wext_join(rdev, wdev);
532 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200533 return err;
534 }
Johannes Berg08645122009-05-11 13:54:58 +0200535
Johannes Bergfffd0932009-07-08 14:22:54 +0200536 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
537 (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
538 if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300539 err = rdev_set_default_mgmt_key(rdev, dev, idx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200540 if (!err)
541 wdev->wext.default_mgmt_key = idx;
542 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200543 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200544
545 return 0;
546}
547
548static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
Johannes Berge31b8212010-10-05 19:39:30 +0200549 struct net_device *dev, bool pairwise,
550 const u8 *addr, bool remove, bool tx_key,
551 int idx, struct key_params *params)
Johannes Bergfffd0932009-07-08 14:22:54 +0200552{
553 int err;
554
555 wdev_lock(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +0200556 err = __cfg80211_set_encryption(rdev, dev, pairwise, addr,
557 remove, tx_key, idx, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200558 wdev_unlock(dev->ieee80211_ptr);
559
560 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200561}
562
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200563static int cfg80211_wext_siwencode(struct net_device *dev,
564 struct iw_request_info *info,
565 struct iw_point *erq, char *keybuf)
Johannes Berg08645122009-05-11 13:54:58 +0200566{
567 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800568 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg08645122009-05-11 13:54:58 +0200569 int idx, err;
570 bool remove = false;
571 struct key_params params;
572
Johannes Bergfffd0932009-07-08 14:22:54 +0200573 if (wdev->iftype != NL80211_IFTYPE_STATION &&
574 wdev->iftype != NL80211_IFTYPE_ADHOC)
575 return -EOPNOTSUPP;
576
Johannes Berg08645122009-05-11 13:54:58 +0200577 /* no use -- only MFP (set_default_mgmt_key) is optional */
578 if (!rdev->ops->del_key ||
579 !rdev->ops->add_key ||
580 !rdev->ops->set_default_key)
581 return -EOPNOTSUPP;
582
583 idx = erq->flags & IW_ENCODE_INDEX;
584 if (idx == 0) {
585 idx = wdev->wext.default_key;
586 if (idx < 0)
587 idx = 0;
588 } else if (idx < 1 || idx > 4)
589 return -EINVAL;
590 else
591 idx--;
592
593 if (erq->flags & IW_ENCODE_DISABLED)
594 remove = true;
595 else if (erq->length == 0) {
596 /* No key data - just set the default TX key index */
Johannes Bergfffd0932009-07-08 14:22:54 +0200597 err = 0;
598 wdev_lock(wdev);
599 if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300600 err = rdev_set_default_key(rdev, dev, idx, true,
601 true);
Johannes Berg08645122009-05-11 13:54:58 +0200602 if (!err)
603 wdev->wext.default_key = idx;
Johannes Bergfffd0932009-07-08 14:22:54 +0200604 wdev_unlock(wdev);
Johannes Berg08645122009-05-11 13:54:58 +0200605 return err;
606 }
607
608 memset(&params, 0, sizeof(params));
609 params.key = keybuf;
610 params.key_len = erq->length;
611 if (erq->length == 5)
612 params.cipher = WLAN_CIPHER_SUITE_WEP40;
613 else if (erq->length == 13)
614 params.cipher = WLAN_CIPHER_SUITE_WEP104;
615 else if (!remove)
616 return -EINVAL;
617
Johannes Berge31b8212010-10-05 19:39:30 +0200618 return cfg80211_set_encryption(rdev, dev, false, NULL, remove,
Johannes Berg08645122009-05-11 13:54:58 +0200619 wdev->wext.default_key == -1,
620 idx, &params);
621}
Johannes Berg08645122009-05-11 13:54:58 +0200622
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200623static int cfg80211_wext_siwencodeext(struct net_device *dev,
624 struct iw_request_info *info,
625 struct iw_point *erq, char *extra)
Johannes Berg08645122009-05-11 13:54:58 +0200626{
627 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800628 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg08645122009-05-11 13:54:58 +0200629 struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
630 const u8 *addr;
631 int idx;
632 bool remove = false;
633 struct key_params params;
634 u32 cipher;
635
Johannes Bergfffd0932009-07-08 14:22:54 +0200636 if (wdev->iftype != NL80211_IFTYPE_STATION &&
637 wdev->iftype != NL80211_IFTYPE_ADHOC)
638 return -EOPNOTSUPP;
639
Johannes Berg08645122009-05-11 13:54:58 +0200640 /* no use -- only MFP (set_default_mgmt_key) is optional */
641 if (!rdev->ops->del_key ||
642 !rdev->ops->add_key ||
643 !rdev->ops->set_default_key)
644 return -EOPNOTSUPP;
645
646 switch (ext->alg) {
647 case IW_ENCODE_ALG_NONE:
648 remove = true;
649 cipher = 0;
650 break;
651 case IW_ENCODE_ALG_WEP:
652 if (ext->key_len == 5)
653 cipher = WLAN_CIPHER_SUITE_WEP40;
654 else if (ext->key_len == 13)
655 cipher = WLAN_CIPHER_SUITE_WEP104;
656 else
657 return -EINVAL;
658 break;
659 case IW_ENCODE_ALG_TKIP:
660 cipher = WLAN_CIPHER_SUITE_TKIP;
661 break;
662 case IW_ENCODE_ALG_CCMP:
663 cipher = WLAN_CIPHER_SUITE_CCMP;
664 break;
665 case IW_ENCODE_ALG_AES_CMAC:
666 cipher = WLAN_CIPHER_SUITE_AES_CMAC;
667 break;
668 default:
669 return -EOPNOTSUPP;
670 }
671
672 if (erq->flags & IW_ENCODE_DISABLED)
673 remove = true;
674
675 idx = erq->flags & IW_ENCODE_INDEX;
676 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
677 if (idx < 4 || idx > 5) {
678 idx = wdev->wext.default_mgmt_key;
679 if (idx < 0)
680 return -EINVAL;
681 } else
682 idx--;
683 } else {
684 if (idx < 1 || idx > 4) {
685 idx = wdev->wext.default_key;
686 if (idx < 0)
687 return -EINVAL;
688 } else
689 idx--;
690 }
691
692 addr = ext->addr.sa_data;
693 if (is_broadcast_ether_addr(addr))
694 addr = NULL;
695
696 memset(&params, 0, sizeof(params));
697 params.key = ext->key;
698 params.key_len = ext->key_len;
699 params.cipher = cipher;
700
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300701 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
702 params.seq = ext->rx_seq;
703 params.seq_len = 6;
704 }
705
Johannes Berg08645122009-05-11 13:54:58 +0200706 return cfg80211_set_encryption(
Johannes Berge31b8212010-10-05 19:39:30 +0200707 rdev, dev,
708 !(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
709 addr, remove,
Johannes Berg08645122009-05-11 13:54:58 +0200710 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
711 idx, &params);
712}
Johannes Berg08645122009-05-11 13:54:58 +0200713
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200714static int cfg80211_wext_giwencode(struct net_device *dev,
715 struct iw_request_info *info,
716 struct iw_point *erq, char *keybuf)
Johannes Berg08645122009-05-11 13:54:58 +0200717{
718 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200719 int idx;
Johannes Berg08645122009-05-11 13:54:58 +0200720
Johannes Bergfffd0932009-07-08 14:22:54 +0200721 if (wdev->iftype != NL80211_IFTYPE_STATION &&
722 wdev->iftype != NL80211_IFTYPE_ADHOC)
Johannes Berg08645122009-05-11 13:54:58 +0200723 return -EOPNOTSUPP;
724
725 idx = erq->flags & IW_ENCODE_INDEX;
726 if (idx == 0) {
727 idx = wdev->wext.default_key;
728 if (idx < 0)
729 idx = 0;
730 } else if (idx < 1 || idx > 4)
731 return -EINVAL;
732 else
733 idx--;
734
735 erq->flags = idx + 1;
736
Johannes Bergfffd0932009-07-08 14:22:54 +0200737 if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
Johannes Berg08645122009-05-11 13:54:58 +0200738 erq->flags |= IW_ENCODE_DISABLED;
739 erq->length = 0;
740 return 0;
741 }
742
Johannes Bergfffd0932009-07-08 14:22:54 +0200743 erq->length = min_t(size_t, erq->length,
744 wdev->wext.keys->params[idx].key_len);
745 memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
746 erq->flags |= IW_ENCODE_ENABLED;
747
748 return 0;
Johannes Berg08645122009-05-11 13:54:58 +0200749}
Johannes Berg7643a2c2009-06-02 13:01:39 +0200750
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200751static int cfg80211_wext_siwfreq(struct net_device *dev,
752 struct iw_request_info *info,
753 struct iw_freq *wextfreq, char *extra)
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200754{
755 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800756 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg683b6d32012-11-08 21:25:48 +0100757 struct cfg80211_chan_def chandef = {
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100758 .width = NL80211_CHAN_WIDTH_20_NOHT,
Johannes Berg683b6d32012-11-08 21:25:48 +0100759 };
Johannes Berg5fe231e2013-05-08 21:45:15 +0200760 int freq;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200761
762 switch (wdev->iftype) {
763 case NL80211_IFTYPE_STATION:
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200764 return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200765 case NL80211_IFTYPE_ADHOC:
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200766 return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
Johannes Bergf444de02010-05-05 15:25:02 +0200767 case NL80211_IFTYPE_MONITOR:
Zhao, Gang96998e32014-04-09 09:28:06 +0800768 freq = cfg80211_wext_freq(wextfreq);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200769 if (freq < 0)
770 return freq;
771 if (freq == 0)
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200772 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100773 chandef.center_freq1 = freq;
Johannes Berg683b6d32012-11-08 21:25:48 +0100774 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
775 if (!chandef.chan)
776 return -EINVAL;
Johannes Berg5fe231e2013-05-08 21:45:15 +0200777 return cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +0200778 case NL80211_IFTYPE_MESH_POINT:
Zhao, Gang96998e32014-04-09 09:28:06 +0800779 freq = cfg80211_wext_freq(wextfreq);
Johannes Bergcc1d2802012-05-16 23:50:20 +0200780 if (freq < 0)
781 return freq;
782 if (freq == 0)
783 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100784 chandef.center_freq1 = freq;
Johannes Berg683b6d32012-11-08 21:25:48 +0100785 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
786 if (!chandef.chan)
787 return -EINVAL;
Johannes Berg5fe231e2013-05-08 21:45:15 +0200788 return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergf444de02010-05-05 15:25:02 +0200789 default:
790 return -EOPNOTSUPP;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200791 }
792}
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200793
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200794static int cfg80211_wext_giwfreq(struct net_device *dev,
795 struct iw_request_info *info,
796 struct iw_freq *freq, char *extra)
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200797{
798 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800799 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf43e5212019-09-23 13:51:16 +0200800 struct cfg80211_chan_def chandef = {};
Johannes Berg683b6d32012-11-08 21:25:48 +0100801 int ret;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200802
803 switch (wdev->iftype) {
804 case NL80211_IFTYPE_STATION:
805 return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
806 case NL80211_IFTYPE_ADHOC:
807 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
Johannes Berge9998822011-11-09 10:30:21 +0100808 case NL80211_IFTYPE_MONITOR:
Johannes Berg5b7ccaf2012-07-12 19:45:08 +0200809 if (!rdev->ops->get_channel)
Johannes Berge9998822011-11-09 10:30:21 +0100810 return -EINVAL;
811
Johannes Berg683b6d32012-11-08 21:25:48 +0100812 ret = rdev_get_channel(rdev, wdev, &chandef);
813 if (ret)
814 return ret;
815 freq->m = chandef.chan->center_freq;
Johannes Berge9998822011-11-09 10:30:21 +0100816 freq->e = 6;
817 return 0;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200818 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +0200819 return -EINVAL;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200820 }
821}
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200822
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200823static int cfg80211_wext_siwtxpower(struct net_device *dev,
824 struct iw_request_info *info,
825 union iwreq_data *data, char *extra)
Johannes Berg7643a2c2009-06-02 13:01:39 +0200826{
827 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800828 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Juuso Oikarinenfa61cf72010-06-23 12:12:37 +0300829 enum nl80211_tx_power_setting type;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200830 int dbm = 0;
831
832 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
833 return -EINVAL;
834 if (data->txpower.flags & IW_TXPOW_RANGE)
835 return -EINVAL;
836
837 if (!rdev->ops->set_tx_power)
838 return -EOPNOTSUPP;
839
840 /* only change when not disabling */
841 if (!data->txpower.disabled) {
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200842 rfkill_set_sw_state(rdev->rfkill, false);
843
Johannes Berg7643a2c2009-06-02 13:01:39 +0200844 if (data->txpower.fixed) {
845 /*
846 * wext doesn't support negative values, see
847 * below where it's for automatic
848 */
849 if (data->txpower.value < 0)
850 return -EINVAL;
851 dbm = data->txpower.value;
Juuso Oikarinenfa61cf72010-06-23 12:12:37 +0300852 type = NL80211_TX_POWER_FIXED;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200853 /* TODO: do regulatory check! */
854 } else {
855 /*
856 * Automatic power level setting, max being the value
857 * passed in from userland.
858 */
859 if (data->txpower.value < 0) {
Juuso Oikarinenfa61cf72010-06-23 12:12:37 +0300860 type = NL80211_TX_POWER_AUTOMATIC;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200861 } else {
862 dbm = data->txpower.value;
Juuso Oikarinenfa61cf72010-06-23 12:12:37 +0300863 type = NL80211_TX_POWER_LIMITED;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200864 }
865 }
866 } else {
Johannes Berg3cfe91c2019-08-30 14:24:44 +0300867 if (rfkill_set_sw_state(rdev->rfkill, true))
868 schedule_work(&rdev->rfkill_block);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200869 return 0;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200870 }
871
Johannes Bergc8442112012-10-24 10:17:18 +0200872 return rdev_set_tx_power(rdev, wdev, type, DBM_TO_MBM(dbm));
Johannes Berg7643a2c2009-06-02 13:01:39 +0200873}
Johannes Berg7643a2c2009-06-02 13:01:39 +0200874
Johannes Berg04b0c5c2011-07-15 13:01:38 +0200875static int cfg80211_wext_giwtxpower(struct net_device *dev,
876 struct iw_request_info *info,
877 union iwreq_data *data, char *extra)
Johannes Berg7643a2c2009-06-02 13:01:39 +0200878{
879 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800880 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg7643a2c2009-06-02 13:01:39 +0200881 int err, val;
882
883 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
884 return -EINVAL;
885 if (data->txpower.flags & IW_TXPOW_RANGE)
886 return -EINVAL;
887
888 if (!rdev->ops->get_tx_power)
889 return -EOPNOTSUPP;
890
Johannes Bergc8442112012-10-24 10:17:18 +0200891 err = rdev_get_tx_power(rdev, wdev, &val);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200892 if (err)
Johannes Berg7643a2c2009-06-02 13:01:39 +0200893 return err;
894
895 /* well... oh well */
896 data->txpower.fixed = 1;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200897 data->txpower.disabled = rfkill_blocked(rdev->rfkill);
Johannes Berg7643a2c2009-06-02 13:01:39 +0200898 data->txpower.value = val;
899 data->txpower.flags = IW_TXPOW_DBM;
900
901 return 0;
902}
Johannes Bergf2129352009-07-01 21:26:56 +0200903
904static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
905 s32 auth_alg)
906{
907 int nr_alg = 0;
908
909 if (!auth_alg)
910 return -EINVAL;
911
912 if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
913 IW_AUTH_ALG_SHARED_KEY |
914 IW_AUTH_ALG_LEAP))
915 return -EINVAL;
916
917 if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
918 nr_alg++;
919 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
920 }
921
922 if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
923 nr_alg++;
924 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
925 }
926
927 if (auth_alg & IW_AUTH_ALG_LEAP) {
928 nr_alg++;
929 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
930 }
931
932 if (nr_alg > 1)
933 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
934
935 return 0;
936}
937
938static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
939{
Johannes Bergf2129352009-07-01 21:26:56 +0200940 if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
Gábor Stefanik323d5662009-07-12 02:03:48 +0200941 IW_AUTH_WPA_VERSION_WPA2|
942 IW_AUTH_WPA_VERSION_DISABLED))
Johannes Bergf2129352009-07-01 21:26:56 +0200943 return -EINVAL;
944
Gábor Stefanik323d5662009-07-12 02:03:48 +0200945 if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
946 (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
947 IW_AUTH_WPA_VERSION_WPA2)))
948 return -EINVAL;
949
950 if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
951 wdev->wext.connect.crypto.wpa_versions &=
952 ~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
953
Johannes Bergf2129352009-07-01 21:26:56 +0200954 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
955 wdev->wext.connect.crypto.wpa_versions |=
956 NL80211_WPA_VERSION_1;
957
958 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
959 wdev->wext.connect.crypto.wpa_versions |=
960 NL80211_WPA_VERSION_2;
961
962 return 0;
963}
964
Johannes Berg4f5dadc2009-07-07 03:56:10 +0200965static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
Johannes Bergf2129352009-07-01 21:26:56 +0200966{
Johannes Bergf2129352009-07-01 21:26:56 +0200967 if (cipher & IW_AUTH_CIPHER_WEP40)
968 wdev->wext.connect.crypto.cipher_group =
969 WLAN_CIPHER_SUITE_WEP40;
970 else if (cipher & IW_AUTH_CIPHER_WEP104)
971 wdev->wext.connect.crypto.cipher_group =
972 WLAN_CIPHER_SUITE_WEP104;
973 else if (cipher & IW_AUTH_CIPHER_TKIP)
974 wdev->wext.connect.crypto.cipher_group =
975 WLAN_CIPHER_SUITE_TKIP;
976 else if (cipher & IW_AUTH_CIPHER_CCMP)
977 wdev->wext.connect.crypto.cipher_group =
978 WLAN_CIPHER_SUITE_CCMP;
979 else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
980 wdev->wext.connect.crypto.cipher_group =
981 WLAN_CIPHER_SUITE_AES_CMAC;
Jouni Malinenc5f82892009-11-01 11:18:49 +0200982 else if (cipher & IW_AUTH_CIPHER_NONE)
983 wdev->wext.connect.crypto.cipher_group = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200984 else
985 return -EINVAL;
986
987 return 0;
988}
989
Johannes Berg4f5dadc2009-07-07 03:56:10 +0200990static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
Johannes Bergf2129352009-07-01 21:26:56 +0200991{
992 int nr_ciphers = 0;
993 u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
994
995 if (cipher & IW_AUTH_CIPHER_WEP40) {
996 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
997 nr_ciphers++;
998 }
999
1000 if (cipher & IW_AUTH_CIPHER_WEP104) {
1001 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
1002 nr_ciphers++;
1003 }
1004
1005 if (cipher & IW_AUTH_CIPHER_TKIP) {
1006 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
1007 nr_ciphers++;
1008 }
1009
1010 if (cipher & IW_AUTH_CIPHER_CCMP) {
1011 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
1012 nr_ciphers++;
1013 }
1014
1015 if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
1016 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
1017 nr_ciphers++;
1018 }
1019
1020 BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
1021
1022 wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1023
1024 return 0;
1025}
1026
1027
Johannes Berg4f5dadc2009-07-07 03:56:10 +02001028static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
Johannes Bergf2129352009-07-01 21:26:56 +02001029{
1030 int nr_akm_suites = 0;
1031
1032 if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1033 IW_AUTH_KEY_MGMT_PSK))
1034 return -EINVAL;
1035
1036 if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1037 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1038 WLAN_AKM_SUITE_8021X;
1039 nr_akm_suites++;
1040 }
1041
1042 if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1043 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1044 WLAN_AKM_SUITE_PSK;
1045 nr_akm_suites++;
1046 }
1047
1048 wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1049
1050 return 0;
1051}
1052
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001053static int cfg80211_wext_siwauth(struct net_device *dev,
1054 struct iw_request_info *info,
1055 struct iw_param *data, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +02001056{
1057 struct wireless_dev *wdev = dev->ieee80211_ptr;
1058
1059 if (wdev->iftype != NL80211_IFTYPE_STATION)
1060 return -EOPNOTSUPP;
1061
1062 switch (data->flags & IW_AUTH_INDEX) {
1063 case IW_AUTH_PRIVACY_INVOKED:
1064 wdev->wext.connect.privacy = data->value;
1065 return 0;
1066 case IW_AUTH_WPA_VERSION:
1067 return cfg80211_set_wpa_version(wdev, data->value);
1068 case IW_AUTH_CIPHER_GROUP:
1069 return cfg80211_set_cipher_group(wdev, data->value);
1070 case IW_AUTH_KEY_MGMT:
1071 return cfg80211_set_key_mgt(wdev, data->value);
1072 case IW_AUTH_CIPHER_PAIRWISE:
1073 return cfg80211_set_cipher_pairwise(wdev, data->value);
1074 case IW_AUTH_80211_AUTH_ALG:
1075 return cfg80211_set_auth_alg(wdev, data->value);
1076 case IW_AUTH_WPA_ENABLED:
1077 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1078 case IW_AUTH_DROP_UNENCRYPTED:
1079 case IW_AUTH_MFP:
1080 return 0;
1081 default:
1082 return -EOPNOTSUPP;
1083 }
1084}
Johannes Bergf2129352009-07-01 21:26:56 +02001085
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001086static int cfg80211_wext_giwauth(struct net_device *dev,
1087 struct iw_request_info *info,
1088 struct iw_param *data, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +02001089{
1090 /* XXX: what do we need? */
1091
1092 return -EOPNOTSUPP;
1093}
Johannes Bergbc92afd2009-07-01 21:26:57 +02001094
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001095static int cfg80211_wext_siwpower(struct net_device *dev,
1096 struct iw_request_info *info,
1097 struct iw_param *wrq, char *extra)
Johannes Bergbc92afd2009-07-01 21:26:57 +02001098{
1099 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001100 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Kalle Valoffb9eb32010-02-17 17:58:10 +02001101 bool ps = wdev->ps;
1102 int timeout = wdev->ps_timeout;
Johannes Bergbc92afd2009-07-01 21:26:57 +02001103 int err;
1104
1105 if (wdev->iftype != NL80211_IFTYPE_STATION)
1106 return -EINVAL;
1107
1108 if (!rdev->ops->set_power_mgmt)
1109 return -EOPNOTSUPP;
1110
1111 if (wrq->disabled) {
1112 ps = false;
1113 } else {
1114 switch (wrq->flags & IW_POWER_MODE) {
1115 case IW_POWER_ON: /* If not specified */
1116 case IW_POWER_MODE: /* If set all mask */
1117 case IW_POWER_ALL_R: /* If explicitely state all */
1118 ps = true;
1119 break;
1120 default: /* Otherwise we ignore */
1121 return -EINVAL;
1122 }
1123
1124 if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1125 return -EINVAL;
1126
1127 if (wrq->flags & IW_POWER_TIMEOUT)
1128 timeout = wrq->value / 1000;
1129 }
1130
Hila Gonene35e4d22012-06-27 17:19:42 +03001131 err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
Johannes Bergbc92afd2009-07-01 21:26:57 +02001132 if (err)
1133 return err;
1134
Kalle Valoffb9eb32010-02-17 17:58:10 +02001135 wdev->ps = ps;
1136 wdev->ps_timeout = timeout;
Johannes Bergbc92afd2009-07-01 21:26:57 +02001137
1138 return 0;
1139
1140}
Johannes Bergbc92afd2009-07-01 21:26:57 +02001141
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001142static int cfg80211_wext_giwpower(struct net_device *dev,
1143 struct iw_request_info *info,
1144 struct iw_param *wrq, char *extra)
Johannes Bergbc92afd2009-07-01 21:26:57 +02001145{
1146 struct wireless_dev *wdev = dev->ieee80211_ptr;
1147
Kalle Valoffb9eb32010-02-17 17:58:10 +02001148 wrq->disabled = !wdev->ps;
Johannes Bergbc92afd2009-07-01 21:26:57 +02001149
1150 return 0;
1151}
Johannes Bergab737a42009-07-01 21:26:58 +02001152
Johannes Berg562e4822009-07-27 12:01:51 +02001153static int cfg80211_wds_wext_siwap(struct net_device *dev,
1154 struct iw_request_info *info,
1155 struct sockaddr *addr, char *extra)
Johannes Bergab737a42009-07-01 21:26:58 +02001156{
1157 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001158 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergab737a42009-07-01 21:26:58 +02001159 int err;
1160
1161 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1162 return -EINVAL;
1163
1164 if (addr->sa_family != ARPHRD_ETHER)
1165 return -EINVAL;
1166
1167 if (netif_running(dev))
1168 return -EBUSY;
1169
1170 if (!rdev->ops->set_wds_peer)
1171 return -EOPNOTSUPP;
1172
Hila Gonene35e4d22012-06-27 17:19:42 +03001173 err = rdev_set_wds_peer(rdev, dev, (u8 *)&addr->sa_data);
Johannes Bergab737a42009-07-01 21:26:58 +02001174 if (err)
1175 return err;
1176
1177 memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);
1178
1179 return 0;
1180}
Johannes Bergab737a42009-07-01 21:26:58 +02001181
Johannes Berg562e4822009-07-27 12:01:51 +02001182static int cfg80211_wds_wext_giwap(struct net_device *dev,
1183 struct iw_request_info *info,
1184 struct sockaddr *addr, char *extra)
Johannes Bergab737a42009-07-01 21:26:58 +02001185{
1186 struct wireless_dev *wdev = dev->ieee80211_ptr;
1187
1188 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1189 return -EINVAL;
1190
1191 addr->sa_family = ARPHRD_ETHER;
1192 memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);
1193
1194 return 0;
1195}
Johannes Berg99303802009-07-01 21:26:59 +02001196
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001197static int cfg80211_wext_siwrate(struct net_device *dev,
1198 struct iw_request_info *info,
1199 struct iw_param *rate, char *extra)
Johannes Berg99303802009-07-01 21:26:59 +02001200{
1201 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001202 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg99303802009-07-01 21:26:59 +02001203 struct cfg80211_bitrate_mask mask;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001204 u32 fixed, maxrate;
1205 struct ieee80211_supported_band *sband;
1206 int band, ridx;
1207 bool match = false;
Johannes Berg99303802009-07-01 21:26:59 +02001208
1209 if (!rdev->ops->set_bitrate_mask)
1210 return -EOPNOTSUPP;
1211
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001212 memset(&mask, 0, sizeof(mask));
1213 fixed = 0;
John W. Linville54233262010-01-27 09:44:48 -05001214 maxrate = (u32)-1;
Johannes Berg99303802009-07-01 21:26:59 +02001215
1216 if (rate->value < 0) {
1217 /* nothing */
1218 } else if (rate->fixed) {
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001219 fixed = rate->value / 100000;
Johannes Berg99303802009-07-01 21:26:59 +02001220 } else {
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001221 maxrate = rate->value / 100000;
Johannes Berg99303802009-07-01 21:26:59 +02001222 }
1223
Johannes Berg57fbcce2016-04-12 15:56:15 +02001224 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001225 sband = wdev->wiphy->bands[band];
1226 if (sband == NULL)
1227 continue;
1228 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
1229 struct ieee80211_rate *srate = &sband->bitrates[ridx];
1230 if (fixed == srate->bitrate) {
1231 mask.control[band].legacy = 1 << ridx;
1232 match = true;
1233 break;
1234 }
1235 if (srate->bitrate <= maxrate) {
1236 mask.control[band].legacy |= 1 << ridx;
1237 match = true;
1238 }
1239 }
1240 }
1241
1242 if (!match)
1243 return -EINVAL;
1244
Hila Gonene35e4d22012-06-27 17:19:42 +03001245 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Johannes Berg99303802009-07-01 21:26:59 +02001246}
Johannes Berg99303802009-07-01 21:26:59 +02001247
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001248static int cfg80211_wext_giwrate(struct net_device *dev,
1249 struct iw_request_info *info,
1250 struct iw_param *rate, char *extra)
Johannes Berg99303802009-07-01 21:26:59 +02001251{
1252 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001253 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg5762d7d2018-01-16 23:20:22 +01001254 struct station_info sinfo = {};
Johannes Berga71d62d2009-07-07 23:41:27 +02001255 u8 addr[ETH_ALEN];
Johannes Berg99303802009-07-01 21:26:59 +02001256 int err;
1257
1258 if (wdev->iftype != NL80211_IFTYPE_STATION)
1259 return -EOPNOTSUPP;
1260
1261 if (!rdev->ops->get_station)
1262 return -EOPNOTSUPP;
1263
Johannes Berga71d62d2009-07-07 23:41:27 +02001264 err = 0;
1265 wdev_lock(wdev);
Samuel Ortiz6c230c02009-07-03 02:00:48 +02001266 if (wdev->current_bss)
Johannes Berga71d62d2009-07-07 23:41:27 +02001267 memcpy(addr, wdev->current_bss->pub.bssid, ETH_ALEN);
Samuel Ortiz6c230c02009-07-03 02:00:48 +02001268 else
Johannes Berga71d62d2009-07-07 23:41:27 +02001269 err = -EOPNOTSUPP;
1270 wdev_unlock(wdev);
1271 if (err)
1272 return err;
Johannes Berg99303802009-07-01 21:26:59 +02001273
Hila Gonene35e4d22012-06-27 17:19:42 +03001274 err = rdev_get_station(rdev, dev, addr, &sinfo);
Johannes Berg99303802009-07-01 21:26:59 +02001275 if (err)
1276 return err;
1277
Stefan Seyfried848e6162018-09-30 12:53:00 +02001278 if (!(sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
1279 err = -EOPNOTSUPP;
1280 goto free;
1281 }
Johannes Berg99303802009-07-01 21:26:59 +02001282
John W. Linville254416a2009-12-09 16:43:52 -05001283 rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
Johannes Berg99303802009-07-01 21:26:59 +02001284
Stefan Seyfried848e6162018-09-30 12:53:00 +02001285free:
1286 cfg80211_sinfo_release_content(&sinfo);
1287 return err;
Johannes Berg99303802009-07-01 21:26:59 +02001288}
Johannes Berg89906462009-07-01 21:27:00 +02001289
1290/* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001291static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
Johannes Berg89906462009-07-01 21:27:00 +02001292{
1293 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001294 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg89906462009-07-01 21:27:00 +02001295 /* we are under RTNL - globally locked - so can use static structs */
1296 static struct iw_statistics wstats;
Stefan Seyfried848e6162018-09-30 12:53:00 +02001297 static struct station_info sinfo = {};
Johannes Bergc56c5712009-07-10 16:54:07 +02001298 u8 bssid[ETH_ALEN];
Johannes Berg89906462009-07-01 21:27:00 +02001299
1300 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
1301 return NULL;
1302
1303 if (!rdev->ops->get_station)
1304 return NULL;
1305
Johannes Bergc56c5712009-07-10 16:54:07 +02001306 /* Grab BSSID of current BSS, if any */
1307 wdev_lock(wdev);
1308 if (!wdev->current_bss) {
1309 wdev_unlock(wdev);
Johannes Berg89906462009-07-01 21:27:00 +02001310 return NULL;
Johannes Bergc56c5712009-07-10 16:54:07 +02001311 }
1312 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
1313 wdev_unlock(wdev);
Johannes Berg89906462009-07-01 21:27:00 +02001314
Johannes Berg9c5a18a2015-06-09 21:35:44 +02001315 memset(&sinfo, 0, sizeof(sinfo));
1316
Hila Gonene35e4d22012-06-27 17:19:42 +03001317 if (rdev_get_station(rdev, dev, bssid, &sinfo))
Johannes Berg89906462009-07-01 21:27:00 +02001318 return NULL;
1319
1320 memset(&wstats, 0, sizeof(wstats));
1321
1322 switch (rdev->wiphy.signal_type) {
1323 case CFG80211_SIGNAL_TYPE_MBM:
Omer Efrat397c6572018-06-17 13:06:14 +03001324 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
Johannes Berg89906462009-07-01 21:27:00 +02001325 int sig = sinfo.signal;
1326 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1327 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1328 wstats.qual.updated |= IW_QUAL_DBM;
1329 wstats.qual.level = sig;
1330 if (sig < -110)
1331 sig = -110;
1332 else if (sig > -40)
1333 sig = -40;
1334 wstats.qual.qual = sig + 110;
1335 break;
1336 }
Gustavo A. R. Silva4af21752019-01-23 01:03:32 -06001337 /* fall through */
Johannes Berg89906462009-07-01 21:27:00 +02001338 case CFG80211_SIGNAL_TYPE_UNSPEC:
Omer Efrat397c6572018-06-17 13:06:14 +03001339 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
Johannes Berg89906462009-07-01 21:27:00 +02001340 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1341 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1342 wstats.qual.level = sinfo.signal;
1343 wstats.qual.qual = sinfo.signal;
1344 break;
1345 }
Gustavo A. R. Silva4af21752019-01-23 01:03:32 -06001346 /* fall through */
Johannes Berg89906462009-07-01 21:27:00 +02001347 default:
1348 wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1349 wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1350 }
1351
1352 wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
Omer Efrat397c6572018-06-17 13:06:14 +03001353 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC))
Ben Greear5a5c7312010-10-07 16:39:20 -07001354 wstats.discard.misc = sinfo.rx_dropped_misc;
Omer Efrat397c6572018-06-17 13:06:14 +03001355 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))
Ben Greear5a5c7312010-10-07 16:39:20 -07001356 wstats.discard.retries = sinfo.tx_failed;
Johannes Berg89906462009-07-01 21:27:00 +02001357
Stefan Seyfried848e6162018-09-30 12:53:00 +02001358 cfg80211_sinfo_release_content(&sinfo);
1359
Johannes Berg89906462009-07-01 21:27:00 +02001360 return &wstats;
1361}
Johannes Berg562e4822009-07-27 12:01:51 +02001362
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001363static int cfg80211_wext_siwap(struct net_device *dev,
1364 struct iw_request_info *info,
1365 struct sockaddr *ap_addr, char *extra)
Johannes Berg562e4822009-07-27 12:01:51 +02001366{
1367 struct wireless_dev *wdev = dev->ieee80211_ptr;
1368
1369 switch (wdev->iftype) {
1370 case NL80211_IFTYPE_ADHOC:
1371 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1372 case NL80211_IFTYPE_STATION:
1373 return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1374 case NL80211_IFTYPE_WDS:
1375 return cfg80211_wds_wext_siwap(dev, info, ap_addr, extra);
1376 default:
1377 return -EOPNOTSUPP;
1378 }
1379}
Johannes Berg562e4822009-07-27 12:01:51 +02001380
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001381static int cfg80211_wext_giwap(struct net_device *dev,
1382 struct iw_request_info *info,
1383 struct sockaddr *ap_addr, char *extra)
Johannes Berg562e4822009-07-27 12:01:51 +02001384{
1385 struct wireless_dev *wdev = dev->ieee80211_ptr;
1386
1387 switch (wdev->iftype) {
1388 case NL80211_IFTYPE_ADHOC:
1389 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1390 case NL80211_IFTYPE_STATION:
1391 return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1392 case NL80211_IFTYPE_WDS:
1393 return cfg80211_wds_wext_giwap(dev, info, ap_addr, extra);
1394 default:
1395 return -EOPNOTSUPP;
1396 }
1397}
Johannes Berg1f9298f2009-07-27 12:01:52 +02001398
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001399static int cfg80211_wext_siwessid(struct net_device *dev,
1400 struct iw_request_info *info,
1401 struct iw_point *data, char *ssid)
Johannes Berg1f9298f2009-07-27 12:01:52 +02001402{
1403 struct wireless_dev *wdev = dev->ieee80211_ptr;
1404
1405 switch (wdev->iftype) {
1406 case NL80211_IFTYPE_ADHOC:
1407 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1408 case NL80211_IFTYPE_STATION:
1409 return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
1410 default:
1411 return -EOPNOTSUPP;
1412 }
1413}
Johannes Berg1f9298f2009-07-27 12:01:52 +02001414
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001415static int cfg80211_wext_giwessid(struct net_device *dev,
1416 struct iw_request_info *info,
1417 struct iw_point *data, char *ssid)
Johannes Berg1f9298f2009-07-27 12:01:52 +02001418{
1419 struct wireless_dev *wdev = dev->ieee80211_ptr;
1420
Johannes Berg42da2f92010-08-30 12:24:54 +02001421 data->flags = 0;
1422 data->length = 0;
1423
Johannes Berg1f9298f2009-07-27 12:01:52 +02001424 switch (wdev->iftype) {
1425 case NL80211_IFTYPE_ADHOC:
1426 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1427 case NL80211_IFTYPE_STATION:
1428 return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1429 default:
1430 return -EOPNOTSUPP;
1431 }
1432}
Johannes Berga9a11622009-07-27 12:01:53 +02001433
Johannes Berg04b0c5c2011-07-15 13:01:38 +02001434static int cfg80211_wext_siwpmksa(struct net_device *dev,
1435 struct iw_request_info *info,
1436 struct iw_point *data, char *extra)
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001437{
1438 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001439 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001440 struct cfg80211_pmksa cfg_pmksa;
1441 struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
1442
1443 memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
1444
1445 if (wdev->iftype != NL80211_IFTYPE_STATION)
1446 return -EINVAL;
1447
1448 cfg_pmksa.bssid = pmksa->bssid.sa_data;
1449 cfg_pmksa.pmkid = pmksa->pmkid;
1450
1451 switch (pmksa->cmd) {
1452 case IW_PMKSA_ADD:
1453 if (!rdev->ops->set_pmksa)
1454 return -EOPNOTSUPP;
1455
Hila Gonene35e4d22012-06-27 17:19:42 +03001456 return rdev_set_pmksa(rdev, dev, &cfg_pmksa);
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001457
1458 case IW_PMKSA_REMOVE:
1459 if (!rdev->ops->del_pmksa)
1460 return -EOPNOTSUPP;
1461
Hila Gonene35e4d22012-06-27 17:19:42 +03001462 return rdev_del_pmksa(rdev, dev, &cfg_pmksa);
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001463
1464 case IW_PMKSA_FLUSH:
1465 if (!rdev->ops->flush_pmksa)
1466 return -EOPNOTSUPP;
1467
Hila Gonene35e4d22012-06-27 17:19:42 +03001468 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001469
1470 default:
1471 return -EOPNOTSUPP;
1472 }
1473}
1474
Johannes Berga9a11622009-07-27 12:01:53 +02001475static const iw_handler cfg80211_handlers[] = {
1476 [IW_IOCTL_IDX(SIOCGIWNAME)] = (iw_handler) cfg80211_wext_giwname,
1477 [IW_IOCTL_IDX(SIOCSIWFREQ)] = (iw_handler) cfg80211_wext_siwfreq,
1478 [IW_IOCTL_IDX(SIOCGIWFREQ)] = (iw_handler) cfg80211_wext_giwfreq,
1479 [IW_IOCTL_IDX(SIOCSIWMODE)] = (iw_handler) cfg80211_wext_siwmode,
1480 [IW_IOCTL_IDX(SIOCGIWMODE)] = (iw_handler) cfg80211_wext_giwmode,
1481 [IW_IOCTL_IDX(SIOCGIWRANGE)] = (iw_handler) cfg80211_wext_giwrange,
1482 [IW_IOCTL_IDX(SIOCSIWAP)] = (iw_handler) cfg80211_wext_siwap,
1483 [IW_IOCTL_IDX(SIOCGIWAP)] = (iw_handler) cfg80211_wext_giwap,
1484 [IW_IOCTL_IDX(SIOCSIWMLME)] = (iw_handler) cfg80211_wext_siwmlme,
1485 [IW_IOCTL_IDX(SIOCSIWSCAN)] = (iw_handler) cfg80211_wext_siwscan,
1486 [IW_IOCTL_IDX(SIOCGIWSCAN)] = (iw_handler) cfg80211_wext_giwscan,
1487 [IW_IOCTL_IDX(SIOCSIWESSID)] = (iw_handler) cfg80211_wext_siwessid,
1488 [IW_IOCTL_IDX(SIOCGIWESSID)] = (iw_handler) cfg80211_wext_giwessid,
1489 [IW_IOCTL_IDX(SIOCSIWRATE)] = (iw_handler) cfg80211_wext_siwrate,
1490 [IW_IOCTL_IDX(SIOCGIWRATE)] = (iw_handler) cfg80211_wext_giwrate,
1491 [IW_IOCTL_IDX(SIOCSIWRTS)] = (iw_handler) cfg80211_wext_siwrts,
1492 [IW_IOCTL_IDX(SIOCGIWRTS)] = (iw_handler) cfg80211_wext_giwrts,
1493 [IW_IOCTL_IDX(SIOCSIWFRAG)] = (iw_handler) cfg80211_wext_siwfrag,
1494 [IW_IOCTL_IDX(SIOCGIWFRAG)] = (iw_handler) cfg80211_wext_giwfrag,
1495 [IW_IOCTL_IDX(SIOCSIWTXPOW)] = (iw_handler) cfg80211_wext_siwtxpower,
1496 [IW_IOCTL_IDX(SIOCGIWTXPOW)] = (iw_handler) cfg80211_wext_giwtxpower,
1497 [IW_IOCTL_IDX(SIOCSIWRETRY)] = (iw_handler) cfg80211_wext_siwretry,
1498 [IW_IOCTL_IDX(SIOCGIWRETRY)] = (iw_handler) cfg80211_wext_giwretry,
1499 [IW_IOCTL_IDX(SIOCSIWENCODE)] = (iw_handler) cfg80211_wext_siwencode,
1500 [IW_IOCTL_IDX(SIOCGIWENCODE)] = (iw_handler) cfg80211_wext_giwencode,
1501 [IW_IOCTL_IDX(SIOCSIWPOWER)] = (iw_handler) cfg80211_wext_siwpower,
1502 [IW_IOCTL_IDX(SIOCGIWPOWER)] = (iw_handler) cfg80211_wext_giwpower,
1503 [IW_IOCTL_IDX(SIOCSIWGENIE)] = (iw_handler) cfg80211_wext_siwgenie,
1504 [IW_IOCTL_IDX(SIOCSIWAUTH)] = (iw_handler) cfg80211_wext_siwauth,
1505 [IW_IOCTL_IDX(SIOCGIWAUTH)] = (iw_handler) cfg80211_wext_giwauth,
1506 [IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= (iw_handler) cfg80211_wext_siwencodeext,
Samuel Ortiz2944b2c2009-11-25 00:01:01 +01001507 [IW_IOCTL_IDX(SIOCSIWPMKSA)] = (iw_handler) cfg80211_wext_siwpmksa,
Johannes Berga9a11622009-07-27 12:01:53 +02001508};
1509
1510const struct iw_handler_def cfg80211_wext_handler = {
1511 .num_standard = ARRAY_SIZE(cfg80211_handlers),
1512 .standard = cfg80211_handlers,
1513 .get_wireless_stats = cfg80211_wireless_stats,
1514};