blob: c449174922108efe61c3ba6b96b6d243ee2b2633 [file] [log] [blame]
Johannes Bergfee52672008-11-26 22:36:31 +01001/*
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 *
Johannes Berg08645122009-05-11 13:54:58 +02008 * Copyright 2008-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergfee52672008-11-26 22:36:31 +01009 */
10
11#include <linux/wireless.h>
12#include <linux/nl80211.h>
Johannes Berg691597c2009-04-19 19:57:45 +020013#include <linux/if_arp.h>
Johannes Berg08645122009-05-11 13:54:58 +020014#include <linux/etherdevice.h>
Johannes Bergfee52672008-11-26 22:36:31 +010015#include <net/iw_handler.h>
Johannes Bergfee52672008-11-26 22:36:31 +010016#include <net/cfg80211.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020017#include "wext-compat.h"
Johannes Bergfee52672008-11-26 22:36:31 +010018#include "core.h"
19
20int cfg80211_wext_giwname(struct net_device *dev,
21 struct iw_request_info *info,
22 char *name, char *extra)
23{
24 struct wireless_dev *wdev = dev->ieee80211_ptr;
25 struct ieee80211_supported_band *sband;
26 bool is_ht = false, is_a = false, is_b = false, is_g = false;
27
28 if (!wdev)
29 return -EOPNOTSUPP;
30
31 sband = wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
32 if (sband) {
33 is_a = true;
34 is_ht |= sband->ht_cap.ht_supported;
35 }
36
37 sband = wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
38 if (sband) {
39 int i;
40 /* Check for mandatory rates */
41 for (i = 0; i < sband->n_bitrates; i++) {
42 if (sband->bitrates[i].bitrate == 10)
43 is_b = true;
44 if (sband->bitrates[i].bitrate == 60)
45 is_g = true;
46 }
47 is_ht |= sband->ht_cap.ht_supported;
48 }
49
50 strcpy(name, "IEEE 802.11");
51 if (is_a)
52 strcat(name, "a");
53 if (is_b)
54 strcat(name, "b");
55 if (is_g)
56 strcat(name, "g");
57 if (is_ht)
58 strcat(name, "n");
59
60 return 0;
61}
Johannes Bergba44cb72009-04-20 18:49:39 +020062EXPORT_SYMBOL_GPL(cfg80211_wext_giwname);
Johannes Berge60c7742008-11-26 23:31:40 +010063
64int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
65 u32 *mode, char *extra)
66{
67 struct wireless_dev *wdev = dev->ieee80211_ptr;
68 struct cfg80211_registered_device *rdev;
69 struct vif_params vifparams;
70 enum nl80211_iftype type;
Johannes Bergac7f9cf2009-03-21 17:07:59 +010071 int ret;
Johannes Berge60c7742008-11-26 23:31:40 +010072
73 if (!wdev)
74 return -EOPNOTSUPP;
75
76 rdev = wiphy_to_dev(wdev->wiphy);
77
78 if (!rdev->ops->change_virtual_intf)
79 return -EOPNOTSUPP;
80
81 /* don't support changing VLANs, you just re-create them */
82 if (wdev->iftype == NL80211_IFTYPE_AP_VLAN)
83 return -EOPNOTSUPP;
84
85 switch (*mode) {
86 case IW_MODE_INFRA:
87 type = NL80211_IFTYPE_STATION;
88 break;
89 case IW_MODE_ADHOC:
90 type = NL80211_IFTYPE_ADHOC;
91 break;
92 case IW_MODE_REPEAT:
93 type = NL80211_IFTYPE_WDS;
94 break;
95 case IW_MODE_MONITOR:
96 type = NL80211_IFTYPE_MONITOR;
97 break;
98 default:
99 return -EINVAL;
100 }
101
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100102 if (type == wdev->iftype)
103 return 0;
104
Johannes Berge60c7742008-11-26 23:31:40 +0100105 memset(&vifparams, 0, sizeof(vifparams));
106
Johannes Berge36d56b2009-06-09 21:04:43 +0200107 ret = rdev->ops->change_virtual_intf(wdev->wiphy, dev, type,
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100108 NULL, &vifparams);
109 WARN_ON(!ret && wdev->iftype != type);
110
111 return ret;
Johannes Berge60c7742008-11-26 23:31:40 +0100112}
Johannes Bergba44cb72009-04-20 18:49:39 +0200113EXPORT_SYMBOL_GPL(cfg80211_wext_siwmode);
Johannes Berge60c7742008-11-26 23:31:40 +0100114
115int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
116 u32 *mode, char *extra)
117{
118 struct wireless_dev *wdev = dev->ieee80211_ptr;
119
120 if (!wdev)
121 return -EOPNOTSUPP;
122
123 switch (wdev->iftype) {
124 case NL80211_IFTYPE_AP:
125 *mode = IW_MODE_MASTER;
126 break;
127 case NL80211_IFTYPE_STATION:
128 *mode = IW_MODE_INFRA;
129 break;
130 case NL80211_IFTYPE_ADHOC:
131 *mode = IW_MODE_ADHOC;
132 break;
133 case NL80211_IFTYPE_MONITOR:
134 *mode = IW_MODE_MONITOR;
135 break;
136 case NL80211_IFTYPE_WDS:
137 *mode = IW_MODE_REPEAT;
138 break;
139 case NL80211_IFTYPE_AP_VLAN:
140 *mode = IW_MODE_SECOND; /* FIXME */
141 break;
142 default:
143 *mode = IW_MODE_AUTO;
144 break;
145 }
146 return 0;
147}
Johannes Bergba44cb72009-04-20 18:49:39 +0200148EXPORT_SYMBOL_GPL(cfg80211_wext_giwmode);
Johannes Berg4aa188e2009-02-18 19:32:08 +0100149
150
151int cfg80211_wext_giwrange(struct net_device *dev,
152 struct iw_request_info *info,
153 struct iw_point *data, char *extra)
154{
155 struct wireless_dev *wdev = dev->ieee80211_ptr;
156 struct iw_range *range = (struct iw_range *) extra;
157 enum ieee80211_band band;
Johannes Berg9834c072009-07-06 19:40:51 +0200158 int i, c = 0;
Johannes Berg4aa188e2009-02-18 19:32:08 +0100159
160 if (!wdev)
161 return -EOPNOTSUPP;
162
163 data->length = sizeof(struct iw_range);
164 memset(range, 0, sizeof(struct iw_range));
165
166 range->we_version_compiled = WIRELESS_EXT;
167 range->we_version_source = 21;
168 range->retry_capa = IW_RETRY_LIMIT;
169 range->retry_flags = IW_RETRY_LIMIT;
170 range->min_retry = 0;
171 range->max_retry = 255;
172 range->min_rts = 0;
173 range->max_rts = 2347;
174 range->min_frag = 256;
175 range->max_frag = 2346;
176
Johannes Berg4aa188e2009-02-18 19:32:08 +0100177 range->max_encoding_tokens = 4;
178
179 range->max_qual.updated = IW_QUAL_NOISE_INVALID;
180
181 switch (wdev->wiphy->signal_type) {
182 case CFG80211_SIGNAL_TYPE_NONE:
183 break;
184 case CFG80211_SIGNAL_TYPE_MBM:
185 range->max_qual.level = -110;
186 range->max_qual.qual = 70;
187 range->avg_qual.qual = 35;
188 range->max_qual.updated |= IW_QUAL_DBM;
189 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
190 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
191 break;
192 case CFG80211_SIGNAL_TYPE_UNSPEC:
193 range->max_qual.level = 100;
194 range->max_qual.qual = 100;
195 range->avg_qual.qual = 50;
196 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
197 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
198 break;
199 }
200
201 range->avg_qual.level = range->max_qual.level / 2;
202 range->avg_qual.noise = range->max_qual.noise / 2;
203 range->avg_qual.updated = range->max_qual.updated;
204
Johannes Berg9834c072009-07-06 19:40:51 +0200205 for (i = 0; i < wdev->wiphy->n_cipher_suites; i++) {
206 switch (wdev->wiphy->cipher_suites[i]) {
David Kilroy3daf0972009-06-18 23:21:14 +0100207 case WLAN_CIPHER_SUITE_TKIP:
David Kilroy27bea662009-06-18 23:21:17 +0100208 range->enc_capa |= (IW_ENC_CAPA_CIPHER_TKIP |
209 IW_ENC_CAPA_WPA);
David Kilroy3daf0972009-06-18 23:21:14 +0100210 break;
211
212 case WLAN_CIPHER_SUITE_CCMP:
David Kilroy27bea662009-06-18 23:21:17 +0100213 range->enc_capa |= (IW_ENC_CAPA_CIPHER_CCMP |
214 IW_ENC_CAPA_WPA2);
David Kilroy3daf0972009-06-18 23:21:14 +0100215 break;
David Kilroy2ab658f2009-06-18 23:21:16 +0100216
217 case WLAN_CIPHER_SUITE_WEP40:
218 range->encoding_size[range->num_encoding_sizes++] =
219 WLAN_KEY_LEN_WEP40;
220 break;
221
222 case WLAN_CIPHER_SUITE_WEP104:
223 range->encoding_size[range->num_encoding_sizes++] =
224 WLAN_KEY_LEN_WEP104;
225 break;
David Kilroy3daf0972009-06-18 23:21:14 +0100226 }
227 }
Johannes Berg4aa188e2009-02-18 19:32:08 +0100228
Johannes Berg4aa188e2009-02-18 19:32:08 +0100229 for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
Johannes Berg4aa188e2009-02-18 19:32:08 +0100230 struct ieee80211_supported_band *sband;
231
232 sband = wdev->wiphy->bands[band];
233
234 if (!sband)
235 continue;
236
237 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
238 struct ieee80211_channel *chan = &sband->channels[i];
239
240 if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
241 range->freq[c].i =
242 ieee80211_frequency_to_channel(
243 chan->center_freq);
244 range->freq[c].m = chan->center_freq;
245 range->freq[c].e = 6;
246 c++;
247 }
248 }
249 }
250 range->num_channels = c;
251 range->num_frequency = c;
252
253 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
254 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
255 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
256
David Kilroy51cd4aa2009-06-18 23:21:15 +0100257 if (wdev->wiphy->max_scan_ssids > 0)
258 range->scan_capa |= IW_SCAN_CAPA_ESSID;
Johannes Berg4aa188e2009-02-18 19:32:08 +0100259
260 return 0;
261}
Johannes Bergba44cb72009-04-20 18:49:39 +0200262EXPORT_SYMBOL_GPL(cfg80211_wext_giwrange);
Johannes Berg691597c2009-04-19 19:57:45 +0200263
Johannes Berg04a773a2009-04-19 21:24:32 +0200264
265/**
266 * cfg80211_wext_freq - get wext frequency for non-"auto"
267 * @wiphy: the wiphy
268 * @freq: the wext freq encoding
269 *
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200270 * Returns a frequency, or a negative error code, or 0 for auto.
Johannes Berg04a773a2009-04-19 21:24:32 +0200271 */
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200272int cfg80211_wext_freq(struct wiphy *wiphy, struct iw_freq *freq)
Johannes Berg04a773a2009-04-19 21:24:32 +0200273{
Johannes Berg0b258582009-05-08 09:42:33 +0200274 /*
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200275 * Parse frequency - return 0 for auto and
Johannes Berg0b258582009-05-08 09:42:33 +0200276 * -EINVAL for impossible things.
277 */
Johannes Berg04a773a2009-04-19 21:24:32 +0200278 if (freq->e == 0) {
279 if (freq->m < 0)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200280 return 0;
281 return ieee80211_channel_to_frequency(freq->m);
Johannes Berg04a773a2009-04-19 21:24:32 +0200282 } else {
283 int i, div = 1000000;
284 for (i = 0; i < freq->e; i++)
285 div /= 10;
Johannes Berg0b258582009-05-08 09:42:33 +0200286 if (div <= 0)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200287 return -EINVAL;
288 return freq->m / div;
Johannes Berg04a773a2009-04-19 21:24:32 +0200289 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200290}
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200291
292int cfg80211_wext_siwrts(struct net_device *dev,
293 struct iw_request_info *info,
294 struct iw_param *rts, char *extra)
295{
296 struct wireless_dev *wdev = dev->ieee80211_ptr;
297 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
298 u32 orts = wdev->wiphy->rts_threshold;
299 int err;
300
301 if (rts->disabled || !rts->fixed)
302 wdev->wiphy->rts_threshold = (u32) -1;
303 else if (rts->value < 0)
304 return -EINVAL;
305 else
306 wdev->wiphy->rts_threshold = rts->value;
307
308 err = rdev->ops->set_wiphy_params(wdev->wiphy,
309 WIPHY_PARAM_RTS_THRESHOLD);
310 if (err)
311 wdev->wiphy->rts_threshold = orts;
312
313 return err;
314}
Johannes Bergba44cb72009-04-20 18:49:39 +0200315EXPORT_SYMBOL_GPL(cfg80211_wext_siwrts);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200316
317int cfg80211_wext_giwrts(struct net_device *dev,
318 struct iw_request_info *info,
319 struct iw_param *rts, char *extra)
320{
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
322
323 rts->value = wdev->wiphy->rts_threshold;
324 rts->disabled = rts->value == (u32) -1;
325 rts->fixed = 1;
326
327 return 0;
328}
Johannes Bergba44cb72009-04-20 18:49:39 +0200329EXPORT_SYMBOL_GPL(cfg80211_wext_giwrts);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200330
331int cfg80211_wext_siwfrag(struct net_device *dev,
332 struct iw_request_info *info,
333 struct iw_param *frag, char *extra)
334{
335 struct wireless_dev *wdev = dev->ieee80211_ptr;
336 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
337 u32 ofrag = wdev->wiphy->frag_threshold;
338 int err;
339
340 if (frag->disabled || !frag->fixed)
341 wdev->wiphy->frag_threshold = (u32) -1;
342 else if (frag->value < 256)
343 return -EINVAL;
344 else {
345 /* Fragment length must be even, so strip LSB. */
346 wdev->wiphy->frag_threshold = frag->value & ~0x1;
347 }
348
349 err = rdev->ops->set_wiphy_params(wdev->wiphy,
350 WIPHY_PARAM_FRAG_THRESHOLD);
351 if (err)
352 wdev->wiphy->frag_threshold = ofrag;
353
354 return err;
355}
Johannes Bergba44cb72009-04-20 18:49:39 +0200356EXPORT_SYMBOL_GPL(cfg80211_wext_siwfrag);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200357
358int cfg80211_wext_giwfrag(struct net_device *dev,
359 struct iw_request_info *info,
360 struct iw_param *frag, char *extra)
361{
362 struct wireless_dev *wdev = dev->ieee80211_ptr;
363
364 frag->value = wdev->wiphy->frag_threshold;
365 frag->disabled = frag->value == (u32) -1;
366 frag->fixed = 1;
367
368 return 0;
369}
Johannes Bergba44cb72009-04-20 18:49:39 +0200370EXPORT_SYMBOL_GPL(cfg80211_wext_giwfrag);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200371
372int cfg80211_wext_siwretry(struct net_device *dev,
373 struct iw_request_info *info,
374 struct iw_param *retry, char *extra)
375{
376 struct wireless_dev *wdev = dev->ieee80211_ptr;
377 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
378 u32 changed = 0;
379 u8 olong = wdev->wiphy->retry_long;
380 u8 oshort = wdev->wiphy->retry_short;
381 int err;
382
383 if (retry->disabled ||
384 (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
385 return -EINVAL;
386
387 if (retry->flags & IW_RETRY_LONG) {
388 wdev->wiphy->retry_long = retry->value;
389 changed |= WIPHY_PARAM_RETRY_LONG;
390 } else if (retry->flags & IW_RETRY_SHORT) {
391 wdev->wiphy->retry_short = retry->value;
392 changed |= WIPHY_PARAM_RETRY_SHORT;
393 } else {
394 wdev->wiphy->retry_short = retry->value;
395 wdev->wiphy->retry_long = retry->value;
396 changed |= WIPHY_PARAM_RETRY_LONG;
397 changed |= WIPHY_PARAM_RETRY_SHORT;
398 }
399
400 if (!changed)
401 return 0;
402
403 err = rdev->ops->set_wiphy_params(wdev->wiphy, changed);
404 if (err) {
405 wdev->wiphy->retry_short = oshort;
406 wdev->wiphy->retry_long = olong;
407 }
408
409 return err;
410}
Johannes Bergba44cb72009-04-20 18:49:39 +0200411EXPORT_SYMBOL_GPL(cfg80211_wext_siwretry);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200412
413int cfg80211_wext_giwretry(struct net_device *dev,
414 struct iw_request_info *info,
415 struct iw_param *retry, char *extra)
416{
417 struct wireless_dev *wdev = dev->ieee80211_ptr;
418
419 retry->disabled = 0;
420
421 if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
422 /*
423 * First return short value, iwconfig will ask long value
424 * later if needed
425 */
426 retry->flags |= IW_RETRY_LIMIT;
427 retry->value = wdev->wiphy->retry_short;
428 if (wdev->wiphy->retry_long != wdev->wiphy->retry_short)
429 retry->flags |= IW_RETRY_LONG;
430
431 return 0;
432 }
433
434 if (retry->flags & IW_RETRY_LONG) {
435 retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
436 retry->value = wdev->wiphy->retry_long;
437 }
438
439 return 0;
440}
Johannes Bergba44cb72009-04-20 18:49:39 +0200441EXPORT_SYMBOL_GPL(cfg80211_wext_giwretry);
Johannes Berg08645122009-05-11 13:54:58 +0200442
Johannes Bergfffd0932009-07-08 14:22:54 +0200443static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
444 struct net_device *dev, const u8 *addr,
445 bool remove, bool tx_key, int idx,
446 struct key_params *params)
Johannes Berg08645122009-05-11 13:54:58 +0200447{
448 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200449 int err, i;
450
451 if (!wdev->wext.keys) {
452 wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
453 GFP_KERNEL);
454 if (!wdev->wext.keys)
455 return -ENOMEM;
456 for (i = 0; i < 6; i++)
457 wdev->wext.keys->params[i].key =
458 wdev->wext.keys->data[i];
459 }
460
461 if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
462 wdev->iftype != NL80211_IFTYPE_STATION)
463 return -EOPNOTSUPP;
Johannes Berg08645122009-05-11 13:54:58 +0200464
465 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200466 if (!wdev->current_bss)
467 return -ENOLINK;
468
Johannes Berg08645122009-05-11 13:54:58 +0200469 if (!rdev->ops->set_default_mgmt_key)
470 return -EOPNOTSUPP;
471
472 if (idx < 4 || idx > 5)
473 return -EINVAL;
474 } else if (idx < 0 || idx > 3)
475 return -EINVAL;
476
477 if (remove) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200478 err = 0;
479 if (wdev->current_bss)
480 err = rdev->ops->del_key(&rdev->wiphy, dev, idx, addr);
Johannes Berg08645122009-05-11 13:54:58 +0200481 if (!err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200482 if (!addr) {
483 wdev->wext.keys->params[idx].key_len = 0;
484 wdev->wext.keys->params[idx].cipher = 0;
485 }
Johannes Berg08645122009-05-11 13:54:58 +0200486 if (idx == wdev->wext.default_key)
487 wdev->wext.default_key = -1;
488 else if (idx == wdev->wext.default_mgmt_key)
489 wdev->wext.default_mgmt_key = -1;
490 }
Johannes Berge3da5742009-05-18 19:56:36 +0200491 /*
492 * Applications using wireless extensions expect to be
493 * able to delete keys that don't exist, so allow that.
494 */
495 if (err == -ENOENT)
496 return 0;
497
Johannes Berg08645122009-05-11 13:54:58 +0200498 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200499 }
Johannes Berg08645122009-05-11 13:54:58 +0200500
Johannes Bergfffd0932009-07-08 14:22:54 +0200501 if (addr)
502 tx_key = false;
Johannes Berg08645122009-05-11 13:54:58 +0200503
Johannes Bergfffd0932009-07-08 14:22:54 +0200504 if (cfg80211_validate_key_settings(rdev, params, idx, addr))
505 return -EINVAL;
506
507 err = 0;
508 if (wdev->current_bss)
Johannes Berg08645122009-05-11 13:54:58 +0200509 err = rdev->ops->add_key(&rdev->wiphy, dev, idx, addr, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200510 if (err)
511 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200512
Johannes Bergfffd0932009-07-08 14:22:54 +0200513 if (!addr) {
514 wdev->wext.keys->params[idx] = *params;
515 memcpy(wdev->wext.keys->data[idx],
516 params->key, params->key_len);
517 wdev->wext.keys->params[idx].key =
518 wdev->wext.keys->data[idx];
519 }
520
Zhu Yi1f00fca2009-07-20 11:47:43 +0800521 if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
522 params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
Johannes Bergfffd0932009-07-08 14:22:54 +0200523 (tx_key || (!addr && wdev->wext.default_key == -1))) {
524 if (wdev->current_bss)
Johannes Berg08645122009-05-11 13:54:58 +0200525 err = rdev->ops->set_default_key(&rdev->wiphy,
526 dev, idx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200527 if (!err)
528 wdev->wext.default_key = idx;
529 return err;
530 }
Johannes Berg08645122009-05-11 13:54:58 +0200531
Johannes Bergfffd0932009-07-08 14:22:54 +0200532 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
533 (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
534 if (wdev->current_bss)
Johannes Berg08645122009-05-11 13:54:58 +0200535 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
536 dev, idx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200537 if (!err)
538 wdev->wext.default_mgmt_key = idx;
539 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200540 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200541
542 return 0;
543}
544
545static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
546 struct net_device *dev, const u8 *addr,
547 bool remove, bool tx_key, int idx,
548 struct key_params *params)
549{
550 int err;
551
552 wdev_lock(dev->ieee80211_ptr);
553 err = __cfg80211_set_encryption(rdev, dev, addr, remove,
554 tx_key, idx, params);
555 wdev_unlock(dev->ieee80211_ptr);
556
557 return err;
Johannes Berg08645122009-05-11 13:54:58 +0200558}
559
560int cfg80211_wext_siwencode(struct net_device *dev,
561 struct iw_request_info *info,
562 struct iw_point *erq, char *keybuf)
563{
564 struct wireless_dev *wdev = dev->ieee80211_ptr;
565 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
566 int idx, err;
567 bool remove = false;
568 struct key_params params;
569
Johannes Bergfffd0932009-07-08 14:22:54 +0200570 if (wdev->iftype != NL80211_IFTYPE_STATION &&
571 wdev->iftype != NL80211_IFTYPE_ADHOC)
572 return -EOPNOTSUPP;
573
Johannes Berg08645122009-05-11 13:54:58 +0200574 /* no use -- only MFP (set_default_mgmt_key) is optional */
575 if (!rdev->ops->del_key ||
576 !rdev->ops->add_key ||
577 !rdev->ops->set_default_key)
578 return -EOPNOTSUPP;
579
580 idx = erq->flags & IW_ENCODE_INDEX;
581 if (idx == 0) {
582 idx = wdev->wext.default_key;
583 if (idx < 0)
584 idx = 0;
585 } else if (idx < 1 || idx > 4)
586 return -EINVAL;
587 else
588 idx--;
589
590 if (erq->flags & IW_ENCODE_DISABLED)
591 remove = true;
592 else if (erq->length == 0) {
593 /* No key data - just set the default TX key index */
Johannes Bergfffd0932009-07-08 14:22:54 +0200594 err = 0;
595 wdev_lock(wdev);
596 if (wdev->current_bss)
597 err = rdev->ops->set_default_key(&rdev->wiphy,
598 dev, idx);
Johannes Berg08645122009-05-11 13:54:58 +0200599 if (!err)
600 wdev->wext.default_key = idx;
Johannes Bergfffd0932009-07-08 14:22:54 +0200601 wdev_unlock(wdev);
Johannes Berg08645122009-05-11 13:54:58 +0200602 return err;
603 }
604
605 memset(&params, 0, sizeof(params));
606 params.key = keybuf;
607 params.key_len = erq->length;
608 if (erq->length == 5)
609 params.cipher = WLAN_CIPHER_SUITE_WEP40;
610 else if (erq->length == 13)
611 params.cipher = WLAN_CIPHER_SUITE_WEP104;
612 else if (!remove)
613 return -EINVAL;
614
615 return cfg80211_set_encryption(rdev, dev, NULL, remove,
616 wdev->wext.default_key == -1,
617 idx, &params);
618}
619EXPORT_SYMBOL_GPL(cfg80211_wext_siwencode);
620
621int cfg80211_wext_siwencodeext(struct net_device *dev,
622 struct iw_request_info *info,
623 struct iw_point *erq, char *extra)
624{
625 struct wireless_dev *wdev = dev->ieee80211_ptr;
626 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
627 struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
628 const u8 *addr;
629 int idx;
630 bool remove = false;
631 struct key_params params;
632 u32 cipher;
633
Johannes Bergfffd0932009-07-08 14:22:54 +0200634 if (wdev->iftype != NL80211_IFTYPE_STATION &&
635 wdev->iftype != NL80211_IFTYPE_ADHOC)
636 return -EOPNOTSUPP;
637
Johannes Berg08645122009-05-11 13:54:58 +0200638 /* no use -- only MFP (set_default_mgmt_key) is optional */
639 if (!rdev->ops->del_key ||
640 !rdev->ops->add_key ||
641 !rdev->ops->set_default_key)
642 return -EOPNOTSUPP;
643
644 switch (ext->alg) {
645 case IW_ENCODE_ALG_NONE:
646 remove = true;
647 cipher = 0;
648 break;
649 case IW_ENCODE_ALG_WEP:
650 if (ext->key_len == 5)
651 cipher = WLAN_CIPHER_SUITE_WEP40;
652 else if (ext->key_len == 13)
653 cipher = WLAN_CIPHER_SUITE_WEP104;
654 else
655 return -EINVAL;
656 break;
657 case IW_ENCODE_ALG_TKIP:
658 cipher = WLAN_CIPHER_SUITE_TKIP;
659 break;
660 case IW_ENCODE_ALG_CCMP:
661 cipher = WLAN_CIPHER_SUITE_CCMP;
662 break;
663 case IW_ENCODE_ALG_AES_CMAC:
664 cipher = WLAN_CIPHER_SUITE_AES_CMAC;
665 break;
666 default:
667 return -EOPNOTSUPP;
668 }
669
670 if (erq->flags & IW_ENCODE_DISABLED)
671 remove = true;
672
673 idx = erq->flags & IW_ENCODE_INDEX;
674 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
675 if (idx < 4 || idx > 5) {
676 idx = wdev->wext.default_mgmt_key;
677 if (idx < 0)
678 return -EINVAL;
679 } else
680 idx--;
681 } else {
682 if (idx < 1 || idx > 4) {
683 idx = wdev->wext.default_key;
684 if (idx < 0)
685 return -EINVAL;
686 } else
687 idx--;
688 }
689
690 addr = ext->addr.sa_data;
691 if (is_broadcast_ether_addr(addr))
692 addr = NULL;
693
694 memset(&params, 0, sizeof(params));
695 params.key = ext->key;
696 params.key_len = ext->key_len;
697 params.cipher = cipher;
698
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300699 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
700 params.seq = ext->rx_seq;
701 params.seq_len = 6;
702 }
703
Johannes Berg08645122009-05-11 13:54:58 +0200704 return cfg80211_set_encryption(
705 rdev, dev, addr, remove,
706 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
707 idx, &params);
708}
709EXPORT_SYMBOL_GPL(cfg80211_wext_siwencodeext);
710
Johannes Berg08645122009-05-11 13:54:58 +0200711int cfg80211_wext_giwencode(struct net_device *dev,
712 struct iw_request_info *info,
713 struct iw_point *erq, char *keybuf)
714{
715 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200716 int idx;
Johannes Berg08645122009-05-11 13:54:58 +0200717
Johannes Bergfffd0932009-07-08 14:22:54 +0200718 if (wdev->iftype != NL80211_IFTYPE_STATION &&
719 wdev->iftype != NL80211_IFTYPE_ADHOC)
Johannes Berg08645122009-05-11 13:54:58 +0200720 return -EOPNOTSUPP;
721
722 idx = erq->flags & IW_ENCODE_INDEX;
723 if (idx == 0) {
724 idx = wdev->wext.default_key;
725 if (idx < 0)
726 idx = 0;
727 } else if (idx < 1 || idx > 4)
728 return -EINVAL;
729 else
730 idx--;
731
732 erq->flags = idx + 1;
733
Johannes Bergfffd0932009-07-08 14:22:54 +0200734 if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
Johannes Berg08645122009-05-11 13:54:58 +0200735 erq->flags |= IW_ENCODE_DISABLED;
736 erq->length = 0;
737 return 0;
738 }
739
Johannes Bergfffd0932009-07-08 14:22:54 +0200740 erq->length = min_t(size_t, erq->length,
741 wdev->wext.keys->params[idx].key_len);
742 memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
743 erq->flags |= IW_ENCODE_ENABLED;
744
745 return 0;
Johannes Berg08645122009-05-11 13:54:58 +0200746}
747EXPORT_SYMBOL_GPL(cfg80211_wext_giwencode);
Johannes Berg7643a2c2009-06-02 13:01:39 +0200748
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200749int cfg80211_wext_siwfreq(struct net_device *dev,
750 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200751 struct iw_freq *wextfreq, char *extra)
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200752{
753 struct wireless_dev *wdev = dev->ieee80211_ptr;
754 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200755 int freq, err;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200756
757 switch (wdev->iftype) {
758 case NL80211_IFTYPE_STATION:
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200759 return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200760 case NL80211_IFTYPE_ADHOC:
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200761 return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200762 default:
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200763 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
764 if (freq < 0)
765 return freq;
766 if (freq == 0)
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200767 return -EINVAL;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200768 mutex_lock(&rdev->devlist_mtx);
Johannes Berg4b181142009-08-08 11:03:58 +0200769 err = rdev_set_freq(rdev, NULL, freq, NL80211_CHAN_NO_HT);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200770 mutex_unlock(&rdev->devlist_mtx);
771 return err;
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200772 }
773}
Johannes Berg0e82ffe2009-07-27 12:01:50 +0200774
775int cfg80211_wext_giwfreq(struct net_device *dev,
776 struct iw_request_info *info,
777 struct iw_freq *freq, char *extra)
778{
779 struct wireless_dev *wdev = dev->ieee80211_ptr;
780 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
781
782 switch (wdev->iftype) {
783 case NL80211_IFTYPE_STATION:
784 return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
785 case NL80211_IFTYPE_ADHOC:
786 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
787 default:
788 if (!rdev->channel)
789 return -EINVAL;
790 freq->m = rdev->channel->center_freq;
791 freq->e = 6;
792 return 0;
793 }
794}
795EXPORT_SYMBOL_GPL(cfg80211_wext_giwfreq);
796
Johannes Berg7643a2c2009-06-02 13:01:39 +0200797int cfg80211_wext_siwtxpower(struct net_device *dev,
798 struct iw_request_info *info,
799 union iwreq_data *data, char *extra)
800{
801 struct wireless_dev *wdev = dev->ieee80211_ptr;
802 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
803 enum tx_power_setting type;
804 int dbm = 0;
805
806 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
807 return -EINVAL;
808 if (data->txpower.flags & IW_TXPOW_RANGE)
809 return -EINVAL;
810
811 if (!rdev->ops->set_tx_power)
812 return -EOPNOTSUPP;
813
814 /* only change when not disabling */
815 if (!data->txpower.disabled) {
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200816 rfkill_set_sw_state(rdev->rfkill, false);
817
Johannes Berg7643a2c2009-06-02 13:01:39 +0200818 if (data->txpower.fixed) {
819 /*
820 * wext doesn't support negative values, see
821 * below where it's for automatic
822 */
823 if (data->txpower.value < 0)
824 return -EINVAL;
825 dbm = data->txpower.value;
826 type = TX_POWER_FIXED;
827 /* TODO: do regulatory check! */
828 } else {
829 /*
830 * Automatic power level setting, max being the value
831 * passed in from userland.
832 */
833 if (data->txpower.value < 0) {
834 type = TX_POWER_AUTOMATIC;
835 } else {
836 dbm = data->txpower.value;
837 type = TX_POWER_LIMITED;
838 }
839 }
840 } else {
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200841 rfkill_set_sw_state(rdev->rfkill, true);
842 schedule_work(&rdev->rfkill_sync);
843 return 0;
Johannes Berg7643a2c2009-06-02 13:01:39 +0200844 }
845
846 return rdev->ops->set_tx_power(wdev->wiphy, type, dbm);;
847}
848EXPORT_SYMBOL_GPL(cfg80211_wext_siwtxpower);
849
850int cfg80211_wext_giwtxpower(struct net_device *dev,
851 struct iw_request_info *info,
852 union iwreq_data *data, char *extra)
853{
854 struct wireless_dev *wdev = dev->ieee80211_ptr;
855 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
856 int err, val;
857
858 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
859 return -EINVAL;
860 if (data->txpower.flags & IW_TXPOW_RANGE)
861 return -EINVAL;
862
863 if (!rdev->ops->get_tx_power)
864 return -EOPNOTSUPP;
865
866 err = rdev->ops->get_tx_power(wdev->wiphy, &val);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200867 if (err)
Johannes Berg7643a2c2009-06-02 13:01:39 +0200868 return err;
869
870 /* well... oh well */
871 data->txpower.fixed = 1;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200872 data->txpower.disabled = rfkill_blocked(rdev->rfkill);
Johannes Berg7643a2c2009-06-02 13:01:39 +0200873 data->txpower.value = val;
874 data->txpower.flags = IW_TXPOW_DBM;
875
876 return 0;
877}
878EXPORT_SYMBOL_GPL(cfg80211_wext_giwtxpower);
Johannes Bergf2129352009-07-01 21:26:56 +0200879
880static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
881 s32 auth_alg)
882{
883 int nr_alg = 0;
884
885 if (!auth_alg)
886 return -EINVAL;
887
888 if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
889 IW_AUTH_ALG_SHARED_KEY |
890 IW_AUTH_ALG_LEAP))
891 return -EINVAL;
892
893 if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
894 nr_alg++;
895 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
896 }
897
898 if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
899 nr_alg++;
900 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
901 }
902
903 if (auth_alg & IW_AUTH_ALG_LEAP) {
904 nr_alg++;
905 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
906 }
907
908 if (nr_alg > 1)
909 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
910
911 return 0;
912}
913
914static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
915{
916 wdev->wext.connect.crypto.wpa_versions = 0;
917
918 if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
Gábor Stefanik323d5662009-07-12 02:03:48 +0200919 IW_AUTH_WPA_VERSION_WPA2|
920 IW_AUTH_WPA_VERSION_DISABLED))
Johannes Bergf2129352009-07-01 21:26:56 +0200921 return -EINVAL;
922
Gábor Stefanik323d5662009-07-12 02:03:48 +0200923 if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
924 (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
925 IW_AUTH_WPA_VERSION_WPA2)))
926 return -EINVAL;
927
928 if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
929 wdev->wext.connect.crypto.wpa_versions &=
930 ~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
931
Johannes Bergf2129352009-07-01 21:26:56 +0200932 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
933 wdev->wext.connect.crypto.wpa_versions |=
934 NL80211_WPA_VERSION_1;
935
936 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
937 wdev->wext.connect.crypto.wpa_versions |=
938 NL80211_WPA_VERSION_2;
939
940 return 0;
941}
942
Johannes Berg4f5dadc2009-07-07 03:56:10 +0200943static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
Johannes Bergf2129352009-07-01 21:26:56 +0200944{
945 wdev->wext.connect.crypto.cipher_group = 0;
946
947 if (cipher & IW_AUTH_CIPHER_WEP40)
948 wdev->wext.connect.crypto.cipher_group =
949 WLAN_CIPHER_SUITE_WEP40;
950 else if (cipher & IW_AUTH_CIPHER_WEP104)
951 wdev->wext.connect.crypto.cipher_group =
952 WLAN_CIPHER_SUITE_WEP104;
953 else if (cipher & IW_AUTH_CIPHER_TKIP)
954 wdev->wext.connect.crypto.cipher_group =
955 WLAN_CIPHER_SUITE_TKIP;
956 else if (cipher & IW_AUTH_CIPHER_CCMP)
957 wdev->wext.connect.crypto.cipher_group =
958 WLAN_CIPHER_SUITE_CCMP;
959 else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
960 wdev->wext.connect.crypto.cipher_group =
961 WLAN_CIPHER_SUITE_AES_CMAC;
962 else
963 return -EINVAL;
964
965 return 0;
966}
967
Johannes Berg4f5dadc2009-07-07 03:56:10 +0200968static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
Johannes Bergf2129352009-07-01 21:26:56 +0200969{
970 int nr_ciphers = 0;
971 u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
972
973 if (cipher & IW_AUTH_CIPHER_WEP40) {
974 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
975 nr_ciphers++;
976 }
977
978 if (cipher & IW_AUTH_CIPHER_WEP104) {
979 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
980 nr_ciphers++;
981 }
982
983 if (cipher & IW_AUTH_CIPHER_TKIP) {
984 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
985 nr_ciphers++;
986 }
987
988 if (cipher & IW_AUTH_CIPHER_CCMP) {
989 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
990 nr_ciphers++;
991 }
992
993 if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
994 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
995 nr_ciphers++;
996 }
997
998 BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
999
1000 wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1001
1002 return 0;
1003}
1004
1005
Johannes Berg4f5dadc2009-07-07 03:56:10 +02001006static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
Johannes Bergf2129352009-07-01 21:26:56 +02001007{
1008 int nr_akm_suites = 0;
1009
1010 if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1011 IW_AUTH_KEY_MGMT_PSK))
1012 return -EINVAL;
1013
1014 if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1015 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1016 WLAN_AKM_SUITE_8021X;
1017 nr_akm_suites++;
1018 }
1019
1020 if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1021 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1022 WLAN_AKM_SUITE_PSK;
1023 nr_akm_suites++;
1024 }
1025
1026 wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1027
1028 return 0;
1029}
1030
1031int cfg80211_wext_siwauth(struct net_device *dev,
1032 struct iw_request_info *info,
1033 struct iw_param *data, char *extra)
1034{
1035 struct wireless_dev *wdev = dev->ieee80211_ptr;
1036
1037 if (wdev->iftype != NL80211_IFTYPE_STATION)
1038 return -EOPNOTSUPP;
1039
1040 switch (data->flags & IW_AUTH_INDEX) {
1041 case IW_AUTH_PRIVACY_INVOKED:
1042 wdev->wext.connect.privacy = data->value;
1043 return 0;
1044 case IW_AUTH_WPA_VERSION:
1045 return cfg80211_set_wpa_version(wdev, data->value);
1046 case IW_AUTH_CIPHER_GROUP:
1047 return cfg80211_set_cipher_group(wdev, data->value);
1048 case IW_AUTH_KEY_MGMT:
1049 return cfg80211_set_key_mgt(wdev, data->value);
1050 case IW_AUTH_CIPHER_PAIRWISE:
1051 return cfg80211_set_cipher_pairwise(wdev, data->value);
1052 case IW_AUTH_80211_AUTH_ALG:
1053 return cfg80211_set_auth_alg(wdev, data->value);
1054 case IW_AUTH_WPA_ENABLED:
1055 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1056 case IW_AUTH_DROP_UNENCRYPTED:
1057 case IW_AUTH_MFP:
1058 return 0;
1059 default:
1060 return -EOPNOTSUPP;
1061 }
1062}
1063EXPORT_SYMBOL_GPL(cfg80211_wext_siwauth);
1064
1065int cfg80211_wext_giwauth(struct net_device *dev,
1066 struct iw_request_info *info,
1067 struct iw_param *data, char *extra)
1068{
1069 /* XXX: what do we need? */
1070
1071 return -EOPNOTSUPP;
1072}
1073EXPORT_SYMBOL_GPL(cfg80211_wext_giwauth);
Johannes Bergbc92afd2009-07-01 21:26:57 +02001074
1075int cfg80211_wext_siwpower(struct net_device *dev,
1076 struct iw_request_info *info,
1077 struct iw_param *wrq, char *extra)
1078{
1079 struct wireless_dev *wdev = dev->ieee80211_ptr;
1080 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1081 bool ps = wdev->wext.ps;
1082 int timeout = wdev->wext.ps_timeout;
1083 int err;
1084
1085 if (wdev->iftype != NL80211_IFTYPE_STATION)
1086 return -EINVAL;
1087
1088 if (!rdev->ops->set_power_mgmt)
1089 return -EOPNOTSUPP;
1090
1091 if (wrq->disabled) {
1092 ps = false;
1093 } else {
1094 switch (wrq->flags & IW_POWER_MODE) {
1095 case IW_POWER_ON: /* If not specified */
1096 case IW_POWER_MODE: /* If set all mask */
1097 case IW_POWER_ALL_R: /* If explicitely state all */
1098 ps = true;
1099 break;
1100 default: /* Otherwise we ignore */
1101 return -EINVAL;
1102 }
1103
1104 if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1105 return -EINVAL;
1106
1107 if (wrq->flags & IW_POWER_TIMEOUT)
1108 timeout = wrq->value / 1000;
1109 }
1110
1111 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, ps, timeout);
1112 if (err)
1113 return err;
1114
1115 wdev->wext.ps = ps;
1116 wdev->wext.ps_timeout = timeout;
1117
1118 return 0;
1119
1120}
1121EXPORT_SYMBOL_GPL(cfg80211_wext_siwpower);
1122
1123int cfg80211_wext_giwpower(struct net_device *dev,
1124 struct iw_request_info *info,
1125 struct iw_param *wrq, char *extra)
1126{
1127 struct wireless_dev *wdev = dev->ieee80211_ptr;
1128
1129 wrq->disabled = !wdev->wext.ps;
1130
1131 return 0;
1132}
1133EXPORT_SYMBOL_GPL(cfg80211_wext_giwpower);
Johannes Bergab737a42009-07-01 21:26:58 +02001134
Johannes Berg562e4822009-07-27 12:01:51 +02001135static int cfg80211_wds_wext_siwap(struct net_device *dev,
1136 struct iw_request_info *info,
1137 struct sockaddr *addr, char *extra)
Johannes Bergab737a42009-07-01 21:26:58 +02001138{
1139 struct wireless_dev *wdev = dev->ieee80211_ptr;
1140 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1141 int err;
1142
1143 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1144 return -EINVAL;
1145
1146 if (addr->sa_family != ARPHRD_ETHER)
1147 return -EINVAL;
1148
1149 if (netif_running(dev))
1150 return -EBUSY;
1151
1152 if (!rdev->ops->set_wds_peer)
1153 return -EOPNOTSUPP;
1154
1155 err = rdev->ops->set_wds_peer(wdev->wiphy, dev, (u8 *) &addr->sa_data);
1156 if (err)
1157 return err;
1158
1159 memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);
1160
1161 return 0;
1162}
Johannes Bergab737a42009-07-01 21:26:58 +02001163
Johannes Berg562e4822009-07-27 12:01:51 +02001164static int cfg80211_wds_wext_giwap(struct net_device *dev,
1165 struct iw_request_info *info,
1166 struct sockaddr *addr, char *extra)
Johannes Bergab737a42009-07-01 21:26:58 +02001167{
1168 struct wireless_dev *wdev = dev->ieee80211_ptr;
1169
1170 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1171 return -EINVAL;
1172
1173 addr->sa_family = ARPHRD_ETHER;
1174 memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);
1175
1176 return 0;
1177}
Johannes Berg99303802009-07-01 21:26:59 +02001178
1179int cfg80211_wext_siwrate(struct net_device *dev,
1180 struct iw_request_info *info,
1181 struct iw_param *rate, char *extra)
1182{
1183 struct wireless_dev *wdev = dev->ieee80211_ptr;
1184 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1185 struct cfg80211_bitrate_mask mask;
1186
1187 if (!rdev->ops->set_bitrate_mask)
1188 return -EOPNOTSUPP;
1189
1190 mask.fixed = 0;
1191 mask.maxrate = 0;
1192
1193 if (rate->value < 0) {
1194 /* nothing */
1195 } else if (rate->fixed) {
1196 mask.fixed = rate->value / 1000; /* kbps */
1197 } else {
1198 mask.maxrate = rate->value / 1000; /* kbps */
1199 }
1200
1201 return rdev->ops->set_bitrate_mask(wdev->wiphy, dev, NULL, &mask);
1202}
1203EXPORT_SYMBOL_GPL(cfg80211_wext_siwrate);
1204
1205int cfg80211_wext_giwrate(struct net_device *dev,
1206 struct iw_request_info *info,
1207 struct iw_param *rate, char *extra)
1208{
1209 struct wireless_dev *wdev = dev->ieee80211_ptr;
1210 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1211 /* we are under RTNL - globally locked - so can use a static struct */
1212 static struct station_info sinfo;
Johannes Berga71d62d2009-07-07 23:41:27 +02001213 u8 addr[ETH_ALEN];
Johannes Berg99303802009-07-01 21:26:59 +02001214 int err;
1215
1216 if (wdev->iftype != NL80211_IFTYPE_STATION)
1217 return -EOPNOTSUPP;
1218
1219 if (!rdev->ops->get_station)
1220 return -EOPNOTSUPP;
1221
Johannes Berga71d62d2009-07-07 23:41:27 +02001222 err = 0;
1223 wdev_lock(wdev);
Samuel Ortiz6c230c02009-07-03 02:00:48 +02001224 if (wdev->current_bss)
Johannes Berga71d62d2009-07-07 23:41:27 +02001225 memcpy(addr, wdev->current_bss->pub.bssid, ETH_ALEN);
Samuel Ortiz6c230c02009-07-03 02:00:48 +02001226 else
Johannes Berga71d62d2009-07-07 23:41:27 +02001227 err = -EOPNOTSUPP;
1228 wdev_unlock(wdev);
1229 if (err)
1230 return err;
Johannes Berg99303802009-07-01 21:26:59 +02001231
1232 err = rdev->ops->get_station(&rdev->wiphy, dev, addr, &sinfo);
1233 if (err)
1234 return err;
1235
1236 if (!(sinfo.filled & STATION_INFO_TX_BITRATE))
1237 return -EOPNOTSUPP;
1238
1239 rate->value = 0;
1240
1241 if (!(sinfo.txrate.flags & RATE_INFO_FLAGS_MCS))
1242 rate->value = 100000 * sinfo.txrate.legacy;
1243
1244 return 0;
1245}
1246EXPORT_SYMBOL_GPL(cfg80211_wext_giwrate);
Johannes Berg89906462009-07-01 21:27:00 +02001247
1248/* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
1249struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
1250{
1251 struct wireless_dev *wdev = dev->ieee80211_ptr;
1252 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1253 /* we are under RTNL - globally locked - so can use static structs */
1254 static struct iw_statistics wstats;
1255 static struct station_info sinfo;
Johannes Bergc56c5712009-07-10 16:54:07 +02001256 u8 bssid[ETH_ALEN];
Johannes Berg89906462009-07-01 21:27:00 +02001257
1258 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
1259 return NULL;
1260
1261 if (!rdev->ops->get_station)
1262 return NULL;
1263
Johannes Bergc56c5712009-07-10 16:54:07 +02001264 /* Grab BSSID of current BSS, if any */
1265 wdev_lock(wdev);
1266 if (!wdev->current_bss) {
1267 wdev_unlock(wdev);
Johannes Berg89906462009-07-01 21:27:00 +02001268 return NULL;
Johannes Bergc56c5712009-07-10 16:54:07 +02001269 }
1270 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
1271 wdev_unlock(wdev);
Johannes Berg89906462009-07-01 21:27:00 +02001272
Johannes Bergc56c5712009-07-10 16:54:07 +02001273 if (rdev->ops->get_station(&rdev->wiphy, dev, bssid, &sinfo))
Johannes Berg89906462009-07-01 21:27:00 +02001274 return NULL;
1275
1276 memset(&wstats, 0, sizeof(wstats));
1277
1278 switch (rdev->wiphy.signal_type) {
1279 case CFG80211_SIGNAL_TYPE_MBM:
1280 if (sinfo.filled & STATION_INFO_SIGNAL) {
1281 int sig = sinfo.signal;
1282 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1283 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1284 wstats.qual.updated |= IW_QUAL_DBM;
1285 wstats.qual.level = sig;
1286 if (sig < -110)
1287 sig = -110;
1288 else if (sig > -40)
1289 sig = -40;
1290 wstats.qual.qual = sig + 110;
1291 break;
1292 }
1293 case CFG80211_SIGNAL_TYPE_UNSPEC:
1294 if (sinfo.filled & STATION_INFO_SIGNAL) {
1295 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1296 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1297 wstats.qual.level = sinfo.signal;
1298 wstats.qual.qual = sinfo.signal;
1299 break;
1300 }
1301 default:
1302 wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1303 wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1304 }
1305
1306 wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
1307
1308 return &wstats;
1309}
1310EXPORT_SYMBOL_GPL(cfg80211_wireless_stats);
Johannes Berg562e4822009-07-27 12:01:51 +02001311
1312int cfg80211_wext_siwap(struct net_device *dev,
1313 struct iw_request_info *info,
1314 struct sockaddr *ap_addr, char *extra)
1315{
1316 struct wireless_dev *wdev = dev->ieee80211_ptr;
1317
1318 switch (wdev->iftype) {
1319 case NL80211_IFTYPE_ADHOC:
1320 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1321 case NL80211_IFTYPE_STATION:
1322 return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1323 case NL80211_IFTYPE_WDS:
1324 return cfg80211_wds_wext_siwap(dev, info, ap_addr, extra);
1325 default:
1326 return -EOPNOTSUPP;
1327 }
1328}
1329EXPORT_SYMBOL_GPL(cfg80211_wext_siwap);
1330
1331int cfg80211_wext_giwap(struct net_device *dev,
1332 struct iw_request_info *info,
1333 struct sockaddr *ap_addr, char *extra)
1334{
1335 struct wireless_dev *wdev = dev->ieee80211_ptr;
1336
1337 switch (wdev->iftype) {
1338 case NL80211_IFTYPE_ADHOC:
1339 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1340 case NL80211_IFTYPE_STATION:
1341 return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1342 case NL80211_IFTYPE_WDS:
1343 return cfg80211_wds_wext_giwap(dev, info, ap_addr, extra);
1344 default:
1345 return -EOPNOTSUPP;
1346 }
1347}
1348EXPORT_SYMBOL_GPL(cfg80211_wext_giwap);
Johannes Berg1f9298f2009-07-27 12:01:52 +02001349
1350int cfg80211_wext_siwessid(struct net_device *dev,
1351 struct iw_request_info *info,
1352 struct iw_point *data, char *ssid)
1353{
1354 struct wireless_dev *wdev = dev->ieee80211_ptr;
1355
1356 switch (wdev->iftype) {
1357 case NL80211_IFTYPE_ADHOC:
1358 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1359 case NL80211_IFTYPE_STATION:
1360 return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
1361 default:
1362 return -EOPNOTSUPP;
1363 }
1364}
1365EXPORT_SYMBOL_GPL(cfg80211_wext_siwessid);
1366
1367int cfg80211_wext_giwessid(struct net_device *dev,
1368 struct iw_request_info *info,
1369 struct iw_point *data, char *ssid)
1370{
1371 struct wireless_dev *wdev = dev->ieee80211_ptr;
1372
1373 switch (wdev->iftype) {
1374 case NL80211_IFTYPE_ADHOC:
1375 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1376 case NL80211_IFTYPE_STATION:
1377 return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1378 default:
1379 return -EOPNOTSUPP;
1380 }
1381}
1382EXPORT_SYMBOL_GPL(cfg80211_wext_giwessid);
Johannes Berga9a11622009-07-27 12:01:53 +02001383
1384static const iw_handler cfg80211_handlers[] = {
1385 [IW_IOCTL_IDX(SIOCGIWNAME)] = (iw_handler) cfg80211_wext_giwname,
1386 [IW_IOCTL_IDX(SIOCSIWFREQ)] = (iw_handler) cfg80211_wext_siwfreq,
1387 [IW_IOCTL_IDX(SIOCGIWFREQ)] = (iw_handler) cfg80211_wext_giwfreq,
1388 [IW_IOCTL_IDX(SIOCSIWMODE)] = (iw_handler) cfg80211_wext_siwmode,
1389 [IW_IOCTL_IDX(SIOCGIWMODE)] = (iw_handler) cfg80211_wext_giwmode,
1390 [IW_IOCTL_IDX(SIOCGIWRANGE)] = (iw_handler) cfg80211_wext_giwrange,
1391 [IW_IOCTL_IDX(SIOCSIWAP)] = (iw_handler) cfg80211_wext_siwap,
1392 [IW_IOCTL_IDX(SIOCGIWAP)] = (iw_handler) cfg80211_wext_giwap,
1393 [IW_IOCTL_IDX(SIOCSIWMLME)] = (iw_handler) cfg80211_wext_siwmlme,
1394 [IW_IOCTL_IDX(SIOCSIWSCAN)] = (iw_handler) cfg80211_wext_siwscan,
1395 [IW_IOCTL_IDX(SIOCGIWSCAN)] = (iw_handler) cfg80211_wext_giwscan,
1396 [IW_IOCTL_IDX(SIOCSIWESSID)] = (iw_handler) cfg80211_wext_siwessid,
1397 [IW_IOCTL_IDX(SIOCGIWESSID)] = (iw_handler) cfg80211_wext_giwessid,
1398 [IW_IOCTL_IDX(SIOCSIWRATE)] = (iw_handler) cfg80211_wext_siwrate,
1399 [IW_IOCTL_IDX(SIOCGIWRATE)] = (iw_handler) cfg80211_wext_giwrate,
1400 [IW_IOCTL_IDX(SIOCSIWRTS)] = (iw_handler) cfg80211_wext_siwrts,
1401 [IW_IOCTL_IDX(SIOCGIWRTS)] = (iw_handler) cfg80211_wext_giwrts,
1402 [IW_IOCTL_IDX(SIOCSIWFRAG)] = (iw_handler) cfg80211_wext_siwfrag,
1403 [IW_IOCTL_IDX(SIOCGIWFRAG)] = (iw_handler) cfg80211_wext_giwfrag,
1404 [IW_IOCTL_IDX(SIOCSIWTXPOW)] = (iw_handler) cfg80211_wext_siwtxpower,
1405 [IW_IOCTL_IDX(SIOCGIWTXPOW)] = (iw_handler) cfg80211_wext_giwtxpower,
1406 [IW_IOCTL_IDX(SIOCSIWRETRY)] = (iw_handler) cfg80211_wext_siwretry,
1407 [IW_IOCTL_IDX(SIOCGIWRETRY)] = (iw_handler) cfg80211_wext_giwretry,
1408 [IW_IOCTL_IDX(SIOCSIWENCODE)] = (iw_handler) cfg80211_wext_siwencode,
1409 [IW_IOCTL_IDX(SIOCGIWENCODE)] = (iw_handler) cfg80211_wext_giwencode,
1410 [IW_IOCTL_IDX(SIOCSIWPOWER)] = (iw_handler) cfg80211_wext_siwpower,
1411 [IW_IOCTL_IDX(SIOCGIWPOWER)] = (iw_handler) cfg80211_wext_giwpower,
1412 [IW_IOCTL_IDX(SIOCSIWGENIE)] = (iw_handler) cfg80211_wext_siwgenie,
1413 [IW_IOCTL_IDX(SIOCSIWAUTH)] = (iw_handler) cfg80211_wext_siwauth,
1414 [IW_IOCTL_IDX(SIOCGIWAUTH)] = (iw_handler) cfg80211_wext_giwauth,
1415 [IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= (iw_handler) cfg80211_wext_siwencodeext,
1416};
1417
1418const struct iw_handler_def cfg80211_wext_handler = {
1419 .num_standard = ARRAY_SIZE(cfg80211_handlers),
1420 .standard = cfg80211_handlers,
1421 .get_wireless_stats = cfg80211_wireless_stats,
1422};