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