blob: 63f36e9d1af8714f82ea8f6d9ba503ad5c9179d4 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/netdevice.h>
13#include <linux/types.h>
14#include <linux/slab.h>
15#include <linux/skbuff.h>
16#include <linux/etherdevice.h>
17#include <linux/if_arp.h>
18#include <linux/wireless.h>
19#include <net/iw_handler.h>
20#include <asm/uaccess.h>
21
22#include <net/mac80211.h>
23#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040024#include "led.h"
25#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070026#include "wpa.h"
27#include "aes_ccm.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070028
Johannes Bergb708e612007-09-14 11:10:25 -040029
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120030static int ieee80211_set_encryption(struct ieee80211_sub_if_data *sdata, u8 *sta_addr,
Johannes Berg628a1402007-09-26 17:53:17 +020031 int idx, int alg, int remove,
32 int set_tx_key, const u8 *_key,
33 size_t key_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070034{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120035 struct ieee80211_local *local = sdata->local;
Johannes Bergd0709a62008-02-25 16:27:46 +010036 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -040037 struct ieee80211_key *key;
Johannes Berg3b967662008-04-08 17:56:52 +020038 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070039
Volker Braun139c3a02007-09-14 11:10:25 -040040 if (idx < 0 || idx >= NUM_DEFAULT_KEYS) {
41 printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120042 sdata->dev->name, idx);
Volker Braun139c3a02007-09-14 11:10:25 -040043 return -EINVAL;
44 }
45
Johannes Berg628a1402007-09-26 17:53:17 +020046 if (remove) {
Johannes Berg3b967662008-04-08 17:56:52 +020047 rcu_read_lock();
48
49 err = 0;
50
Johannes Bergdb4d1162008-02-25 16:27:45 +010051 if (is_broadcast_ether_addr(sta_addr)) {
52 key = sdata->keys[idx];
53 } else {
54 sta = sta_info_get(local, sta_addr);
Johannes Berg3b967662008-04-08 17:56:52 +020055 if (!sta) {
56 err = -ENOENT;
57 goto out_unlock;
58 }
Johannes Bergdb4d1162008-02-25 16:27:45 +010059 key = sta->key;
Jiri Bencf0706e82007-05-05 11:45:53 -070060 }
Johannes Bergdb4d1162008-02-25 16:27:45 +010061
Johannes Bergd0709a62008-02-25 16:27:46 +010062 ieee80211_key_free(key);
Johannes Bergdb4d1162008-02-25 16:27:45 +010063 } else {
64 key = ieee80211_key_alloc(alg, idx, key_len, _key);
65 if (!key)
66 return -ENOMEM;
67
Johannes Bergd0709a62008-02-25 16:27:46 +010068 sta = NULL;
Johannes Berg3b967662008-04-08 17:56:52 +020069 err = 0;
70
71 rcu_read_lock();
Johannes Bergd0709a62008-02-25 16:27:46 +010072
Johannes Bergdb4d1162008-02-25 16:27:45 +010073 if (!is_broadcast_ether_addr(sta_addr)) {
74 set_tx_key = 0;
75 /*
76 * According to the standard, the key index of a
77 * pairwise key must be zero. However, some AP are
78 * broken when it comes to WEP key indices, so we
79 * work around this.
80 */
81 if (idx != 0 && alg != ALG_WEP) {
Johannes Bergd0709a62008-02-25 16:27:46 +010082 ieee80211_key_free(key);
Johannes Berg3b967662008-04-08 17:56:52 +020083 err = -EINVAL;
84 goto out_unlock;
Johannes Bergdb4d1162008-02-25 16:27:45 +010085 }
86
87 sta = sta_info_get(local, sta_addr);
88 if (!sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +010089 ieee80211_key_free(key);
Johannes Berg3b967662008-04-08 17:56:52 +020090 err = -ENOENT;
91 goto out_unlock;
Johannes Bergdb4d1162008-02-25 16:27:45 +010092 }
93 }
94
Emmanuel Grumbach23976ef2008-06-28 02:50:13 +030095 if (alg == ALG_WEP &&
96 key_len != LEN_WEP40 && key_len != LEN_WEP104) {
97 ieee80211_key_free(key);
98 err = -EINVAL;
99 goto out_unlock;
100 }
101
Johannes Bergdb4d1162008-02-25 16:27:45 +0100102 ieee80211_key_link(key, sdata, sta);
103
104 if (set_tx_key || (!sta && !sdata->default_key && key))
105 ieee80211_set_default_key(sdata, idx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700106 }
107
Johannes Berg3b967662008-04-08 17:56:52 +0200108 out_unlock:
109 rcu_read_unlock();
110
111 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700112}
113
114static int ieee80211_ioctl_siwgenie(struct net_device *dev,
115 struct iw_request_info *info,
116 struct iw_point *data, char *extra)
117{
118 struct ieee80211_sub_if_data *sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700119
120 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200121
122 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME)
123 return -EOPNOTSUPP;
124
Johannes Berg05c914f2008-09-11 00:01:58 +0200125 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
126 sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200127 int ret = ieee80211_sta_set_extra_ie(sdata, extra, data->length);
Jiri Bencf0706e82007-05-05 11:45:53 -0700128 if (ret)
129 return ret;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400130 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200131 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700132 return 0;
133 }
134
Jiri Bencf0706e82007-05-05 11:45:53 -0700135 return -EOPNOTSUPP;
136}
137
Jiri Bencf0706e82007-05-05 11:45:53 -0700138static int ieee80211_ioctl_giwname(struct net_device *dev,
139 struct iw_request_info *info,
140 char *name, char *extra)
141{
Tomas Winklerf37d08b2008-06-24 15:50:17 +0300142 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
143 struct ieee80211_supported_band *sband;
144 u8 is_ht = 0, is_a = 0, is_b = 0, is_g = 0;
145
146
147 sband = local->hw.wiphy->bands[IEEE80211_BAND_5GHZ];
148 if (sband) {
149 is_a = 1;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200150 is_ht |= sband->ht_cap.ht_supported;
Tomas Winklerf37d08b2008-06-24 15:50:17 +0300151 }
152
153 sband = local->hw.wiphy->bands[IEEE80211_BAND_2GHZ];
154 if (sband) {
155 int i;
156 /* Check for mandatory rates */
157 for (i = 0; i < sband->n_bitrates; i++) {
158 if (sband->bitrates[i].bitrate == 10)
159 is_b = 1;
160 if (sband->bitrates[i].bitrate == 60)
161 is_g = 1;
162 }
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200163 is_ht |= sband->ht_cap.ht_supported;
Tomas Winklerf37d08b2008-06-24 15:50:17 +0300164 }
165
Johannes Berg8318d782008-01-24 19:38:38 +0100166 strcpy(name, "IEEE 802.11");
Tomas Winklerf37d08b2008-06-24 15:50:17 +0300167 if (is_a)
168 strcat(name, "a");
169 if (is_b)
170 strcat(name, "b");
171 if (is_g)
172 strcat(name, "g");
173 if (is_ht)
174 strcat(name, "n");
Jiri Bencf0706e82007-05-05 11:45:53 -0700175
176 return 0;
177}
178
179
180static int ieee80211_ioctl_giwrange(struct net_device *dev,
181 struct iw_request_info *info,
182 struct iw_point *data, char *extra)
183{
184 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
185 struct iw_range *range = (struct iw_range *) extra;
Johannes Berg8318d782008-01-24 19:38:38 +0100186 enum ieee80211_band band;
Hong Liu333af2f2007-07-10 19:32:08 +0200187 int c = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700188
189 data->length = sizeof(struct iw_range);
190 memset(range, 0, sizeof(struct iw_range));
191
192 range->we_version_compiled = WIRELESS_EXT;
193 range->we_version_source = 21;
194 range->retry_capa = IW_RETRY_LIMIT;
195 range->retry_flags = IW_RETRY_LIMIT;
196 range->min_retry = 0;
197 range->max_retry = 255;
198 range->min_rts = 0;
199 range->max_rts = 2347;
200 range->min_frag = 256;
201 range->max_frag = 2346;
202
203 range->encoding_size[0] = 5;
204 range->encoding_size[1] = 13;
205 range->num_encoding_sizes = 2;
206 range->max_encoding_tokens = NUM_DEFAULT_KEYS;
207
Bruno Randolf566bfe52008-05-08 19:15:40 +0200208 if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC ||
209 local->hw.flags & IEEE80211_HW_SIGNAL_DB)
210 range->max_qual.level = local->hw.max_signal;
211 else if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
212 range->max_qual.level = -110;
213 else
214 range->max_qual.level = 0;
215
216 if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
217 range->max_qual.noise = -110;
218 else
219 range->max_qual.noise = 0;
220
221 range->max_qual.qual = 100;
Jiri Bencf0706e82007-05-05 11:45:53 -0700222 range->max_qual.updated = local->wstats_flags;
223
Bruno Randolf566bfe52008-05-08 19:15:40 +0200224 range->avg_qual.qual = 50;
225 /* not always true but better than nothing */
226 range->avg_qual.level = range->max_qual.level / 2;
227 range->avg_qual.noise = range->max_qual.noise / 2;
Jiri Bencf0706e82007-05-05 11:45:53 -0700228 range->avg_qual.updated = local->wstats_flags;
229
230 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
231 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
232
Hong Liu333af2f2007-07-10 19:32:08 +0200233
Johannes Berg8318d782008-01-24 19:38:38 +0100234 for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
235 int i;
236 struct ieee80211_supported_band *sband;
237
238 sband = local->hw.wiphy->bands[band];
239
240 if (!sband)
Hong Liu333af2f2007-07-10 19:32:08 +0200241 continue;
242
Johannes Berg8318d782008-01-24 19:38:38 +0100243 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
244 struct ieee80211_channel *chan = &sband->channels[i];
Hong Liu333af2f2007-07-10 19:32:08 +0200245
Johannes Berg8318d782008-01-24 19:38:38 +0100246 if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
247 range->freq[c].i =
248 ieee80211_frequency_to_channel(
249 chan->center_freq);
250 range->freq[c].m = chan->center_freq;
251 range->freq[c].e = 6;
Hong Liu333af2f2007-07-10 19:32:08 +0200252 c++;
253 }
Hong Liu333af2f2007-07-10 19:32:08 +0200254 }
255 }
256 range->num_channels = c;
257 range->num_frequency = c;
258
Jiri Bencf0706e82007-05-05 11:45:53 -0700259 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
Jiri Bencf0706e82007-05-05 11:45:53 -0700260 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
261 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
262
Dan Williams374fdfb2007-12-12 10:25:07 -0500263 range->scan_capa |= IW_SCAN_CAPA_ESSID;
264
Jiri Bencf0706e82007-05-05 11:45:53 -0700265 return 0;
266}
267
268
Jiri Bencf0706e82007-05-05 11:45:53 -0700269static int ieee80211_ioctl_siwmode(struct net_device *dev,
270 struct iw_request_info *info,
271 __u32 *mode, char *extra)
272{
273 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
274 int type;
275
Johannes Berg05c914f2008-09-11 00:01:58 +0200276 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Jiri Bencf0706e82007-05-05 11:45:53 -0700277 return -EOPNOTSUPP;
278
279 switch (*mode) {
280 case IW_MODE_INFRA:
Johannes Berg05c914f2008-09-11 00:01:58 +0200281 type = NL80211_IFTYPE_STATION;
Jiri Bencf0706e82007-05-05 11:45:53 -0700282 break;
283 case IW_MODE_ADHOC:
Johannes Berg05c914f2008-09-11 00:01:58 +0200284 type = NL80211_IFTYPE_ADHOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700285 break;
Johannes Bergb4540482008-04-14 15:37:03 +0200286 case IW_MODE_REPEAT:
Johannes Berg05c914f2008-09-11 00:01:58 +0200287 type = NL80211_IFTYPE_WDS;
Johannes Bergb4540482008-04-14 15:37:03 +0200288 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700289 case IW_MODE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +0200290 type = NL80211_IFTYPE_MONITOR;
Jiri Bencf0706e82007-05-05 11:45:53 -0700291 break;
292 default:
293 return -EINVAL;
294 }
295
Johannes Bergf3947e22008-07-09 14:40:36 +0200296 return ieee80211_if_change_type(sdata, type);
Jiri Bencf0706e82007-05-05 11:45:53 -0700297}
298
299
300static int ieee80211_ioctl_giwmode(struct net_device *dev,
301 struct iw_request_info *info,
302 __u32 *mode, char *extra)
303{
304 struct ieee80211_sub_if_data *sdata;
305
306 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100307 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200308 case NL80211_IFTYPE_AP:
Jiri Bencf0706e82007-05-05 11:45:53 -0700309 *mode = IW_MODE_MASTER;
310 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200311 case NL80211_IFTYPE_STATION:
Jiri Bencf0706e82007-05-05 11:45:53 -0700312 *mode = IW_MODE_INFRA;
313 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200314 case NL80211_IFTYPE_ADHOC:
Jiri Bencf0706e82007-05-05 11:45:53 -0700315 *mode = IW_MODE_ADHOC;
316 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200317 case NL80211_IFTYPE_MONITOR:
Jiri Bencf0706e82007-05-05 11:45:53 -0700318 *mode = IW_MODE_MONITOR;
319 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200320 case NL80211_IFTYPE_WDS:
Jiri Bencf0706e82007-05-05 11:45:53 -0700321 *mode = IW_MODE_REPEAT;
322 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200323 case NL80211_IFTYPE_AP_VLAN:
Jiri Bencf0706e82007-05-05 11:45:53 -0700324 *mode = IW_MODE_SECOND; /* FIXME */
325 break;
326 default:
327 *mode = IW_MODE_AUTO;
328 break;
329 }
330 return 0;
331}
332
Jiri Bencf0706e82007-05-05 11:45:53 -0700333static int ieee80211_ioctl_siwfreq(struct net_device *dev,
334 struct iw_request_info *info,
335 struct iw_freq *freq, char *extra)
336{
Jiri Bencf0706e82007-05-05 11:45:53 -0700337 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
338
Johannes Berg05c914f2008-09-11 00:01:58 +0200339 if (sdata->vif.type == NL80211_IFTYPE_STATION)
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400340 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700341
342 /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
343 if (freq->e == 0) {
344 if (freq->m < 0) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200345 if (sdata->vif.type == NL80211_IFTYPE_STATION)
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400346 sdata->u.sta.flags |=
347 IEEE80211_STA_AUTO_CHANNEL_SEL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700348 return 0;
349 } else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200350 return ieee80211_set_freq(sdata,
Johannes Berg8318d782008-01-24 19:38:38 +0100351 ieee80211_channel_to_frequency(freq->m));
Jiri Bencf0706e82007-05-05 11:45:53 -0700352 } else {
353 int i, div = 1000000;
354 for (i = 0; i < freq->e; i++)
355 div /= 10;
356 if (div > 0)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200357 return ieee80211_set_freq(sdata, freq->m / div);
Jiri Bencf0706e82007-05-05 11:45:53 -0700358 else
359 return -EINVAL;
360 }
361}
362
363
364static int ieee80211_ioctl_giwfreq(struct net_device *dev,
365 struct iw_request_info *info,
366 struct iw_freq *freq, char *extra)
367{
368 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
369
Johannes Berg8318d782008-01-24 19:38:38 +0100370 freq->m = local->hw.conf.channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -0700371 freq->e = 6;
372
373 return 0;
374}
375
376
377static int ieee80211_ioctl_siwessid(struct net_device *dev,
378 struct iw_request_info *info,
379 struct iw_point *data, char *ssid)
380{
Jiri Bencf0706e82007-05-05 11:45:53 -0700381 struct ieee80211_sub_if_data *sdata;
382 size_t len = data->length;
383
384 /* iwconfig uses nul termination in SSID.. */
385 if (len > 0 && ssid[len - 1] == '\0')
386 len--;
387
388 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg05c914f2008-09-11 00:01:58 +0200389 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
390 sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700391 int ret;
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200392 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700393 if (len > IEEE80211_MAX_SSID_LEN)
394 return -EINVAL;
395 memcpy(sdata->u.sta.ssid, ssid, len);
396 sdata->u.sta.ssid_len = len;
397 return 0;
398 }
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400399 if (data->flags)
400 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
401 else
402 sdata->u.sta.flags |= IEEE80211_STA_AUTO_SSID_SEL;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200403 ret = ieee80211_sta_set_ssid(sdata, ssid, len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700404 if (ret)
405 return ret;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200406 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700407 return 0;
408 }
409
Jiri Bencf0706e82007-05-05 11:45:53 -0700410 return -EOPNOTSUPP;
411}
412
413
414static int ieee80211_ioctl_giwessid(struct net_device *dev,
415 struct iw_request_info *info,
416 struct iw_point *data, char *ssid)
417{
418 size_t len;
419
420 struct ieee80211_sub_if_data *sdata;
421 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg05c914f2008-09-11 00:01:58 +0200422 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
423 sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200424 int res = ieee80211_sta_get_ssid(sdata, ssid, &len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700425 if (res == 0) {
426 data->length = len;
427 data->flags = 1;
428 } else
429 data->flags = 0;
430 return res;
431 }
432
Jiri Bencf0706e82007-05-05 11:45:53 -0700433 return -EOPNOTSUPP;
434}
435
436
437static int ieee80211_ioctl_siwap(struct net_device *dev,
438 struct iw_request_info *info,
439 struct sockaddr *ap_addr, char *extra)
440{
Jiri Bencf0706e82007-05-05 11:45:53 -0700441 struct ieee80211_sub_if_data *sdata;
442
443 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg05c914f2008-09-11 00:01:58 +0200444 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
445 sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700446 int ret;
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200447 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700448 memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
449 ETH_ALEN);
450 return 0;
451 }
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400452 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
453 sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
454 IEEE80211_STA_AUTO_CHANNEL_SEL;
455 else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
456 sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700457 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400458 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200459 ret = ieee80211_sta_set_bssid(sdata, (u8 *) &ap_addr->sa_data);
Jiri Bencf0706e82007-05-05 11:45:53 -0700460 if (ret)
461 return ret;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200462 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700463 return 0;
Johannes Berg05c914f2008-09-11 00:01:58 +0200464 } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
Johannes Berg44213b52008-02-25 16:27:49 +0100465 /*
466 * If it is necessary to update the WDS peer address
467 * while the interface is running, then we need to do
468 * more work here, namely if it is running we need to
469 * add a new and remove the old STA entry, this is
470 * normally handled by _open() and _stop().
471 */
472 if (netif_running(dev))
473 return -EBUSY;
474
475 memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
476 ETH_ALEN);
477
478 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700479 }
480
481 return -EOPNOTSUPP;
482}
483
484
485static int ieee80211_ioctl_giwap(struct net_device *dev,
486 struct iw_request_info *info,
487 struct sockaddr *ap_addr, char *extra)
488{
489 struct ieee80211_sub_if_data *sdata;
490
491 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg05c914f2008-09-11 00:01:58 +0200492 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
493 sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300494 if (sdata->u.sta.state == IEEE80211_STA_MLME_ASSOCIATED ||
495 sdata->u.sta.state == IEEE80211_STA_MLME_IBSS_JOINED) {
Abhijeet Kolekard4231ca2008-05-23 10:15:26 -0700496 ap_addr->sa_family = ARPHRD_ETHER;
497 memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
498 return 0;
499 } else {
500 memset(&ap_addr->sa_data, 0, ETH_ALEN);
501 return 0;
502 }
Johannes Berg05c914f2008-09-11 00:01:58 +0200503 } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700504 ap_addr->sa_family = ARPHRD_ETHER;
505 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
506 return 0;
507 }
508
509 return -EOPNOTSUPP;
510}
511
512
513static int ieee80211_ioctl_siwscan(struct net_device *dev,
514 struct iw_request_info *info,
Bill Moss107acb22007-10-10 16:23:55 -0400515 union iwreq_data *wrqu, char *extra)
Jiri Bencf0706e82007-05-05 11:45:53 -0700516{
Jiri Bencf0706e82007-05-05 11:45:53 -0700517 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bill Moss107acb22007-10-10 16:23:55 -0400518 struct iw_scan_req *req = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700519 u8 *ssid = NULL;
520 size_t ssid_len = 0;
521
522 if (!netif_running(dev))
523 return -ENETDOWN;
524
Johannes Berg05c914f2008-09-11 00:01:58 +0200525 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
526 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
527 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
528 sdata->vif.type != NL80211_IFTYPE_AP)
John W. Linvilled114f392007-10-17 21:16:16 -0700529 return -EOPNOTSUPP;
John W. Linvilled114f392007-10-17 21:16:16 -0700530
531 /* if SSID was specified explicitly then use that */
Bill Moss107acb22007-10-10 16:23:55 -0400532 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
533 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
534 req = (struct iw_scan_req *)extra;
535 ssid = req->essid;
536 ssid_len = req->essid_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700537 }
Daniel Drakef27b62d2007-07-27 15:43:24 +0200538
Johannes Bergc2b13452008-09-11 00:01:55 +0200539 return ieee80211_request_scan(sdata, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700540}
541
542
543static int ieee80211_ioctl_giwscan(struct net_device *dev,
544 struct iw_request_info *info,
545 struct iw_point *data, char *extra)
546{
547 int res;
548 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200549 struct ieee80211_sub_if_data *sdata;
550
551 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Zhu Yiece8edd2007-11-22 10:53:21 +0800552
Johannes Bergc2b13452008-09-11 00:01:55 +0200553 if (local->sw_scanning || local->hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -0700554 return -EAGAIN;
Zhu Yiece8edd2007-11-22 10:53:21 +0800555
Johannes Bergc2b13452008-09-11 00:01:55 +0200556 res = ieee80211_scan_results(local, info, extra, data->length);
Jiri Bencf0706e82007-05-05 11:45:53 -0700557 if (res >= 0) {
558 data->length = res;
559 return 0;
560 }
561 data->length = 0;
562 return res;
563}
564
565
Larry Finger1fd5e582007-07-10 19:32:10 +0200566static int ieee80211_ioctl_siwrate(struct net_device *dev,
567 struct iw_request_info *info,
568 struct iw_param *rate, char *extra)
569{
570 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100571 int i, err = -EINVAL;
Larry Finger1fd5e582007-07-10 19:32:10 +0200572 u32 target_rate = rate->value / 100000;
573 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +0100574 struct ieee80211_supported_band *sband;
Larry Finger1fd5e582007-07-10 19:32:10 +0200575
576 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +0100577
578 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
579
Larry Finger1fd5e582007-07-10 19:32:10 +0200580 /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
581 * target_rate = X, rate->fixed = 1 means only rate X
582 * target_rate = X, rate->fixed = 0 means all rates <= X */
Johannes Berg3e122be2008-07-09 14:40:34 +0200583 sdata->max_ratectrl_rateidx = -1;
584 sdata->force_unicast_rateidx = -1;
Larry Finger1fd5e582007-07-10 19:32:10 +0200585 if (rate->value < 0)
586 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100587
588 for (i=0; i< sband->n_bitrates; i++) {
589 struct ieee80211_rate *brate = &sband->bitrates[i];
590 int this_rate = brate->bitrate;
Larry Finger1fd5e582007-07-10 19:32:10 +0200591
Larry Finger1fd5e582007-07-10 19:32:10 +0200592 if (target_rate == this_rate) {
Johannes Berg3e122be2008-07-09 14:40:34 +0200593 sdata->max_ratectrl_rateidx = i;
Larry Finger1fd5e582007-07-10 19:32:10 +0200594 if (rate->fixed)
Johannes Berg3e122be2008-07-09 14:40:34 +0200595 sdata->force_unicast_rateidx = i;
Johannes Berg8318d782008-01-24 19:38:38 +0100596 err = 0;
597 break;
Larry Finger1fd5e582007-07-10 19:32:10 +0200598 }
599 }
Johannes Berg8318d782008-01-24 19:38:38 +0100600 return err;
Larry Finger1fd5e582007-07-10 19:32:10 +0200601}
602
Larry Fingerb3d88ad2007-06-10 17:57:33 -0700603static int ieee80211_ioctl_giwrate(struct net_device *dev,
604 struct iw_request_info *info,
605 struct iw_param *rate, char *extra)
606{
607 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
608 struct sta_info *sta;
609 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +0100610 struct ieee80211_supported_band *sband;
Larry Fingerb3d88ad2007-06-10 17:57:33 -0700611
612 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +0100613
Johannes Berg05c914f2008-09-11 00:01:58 +0200614 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Larry Fingerb3d88ad2007-06-10 17:57:33 -0700615 return -EOPNOTSUPP;
Johannes Berg8318d782008-01-24 19:38:38 +0100616
617 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
618
Johannes Berg380a9422008-04-04 23:40:35 +0200619 rcu_read_lock();
620
621 sta = sta_info_get(local, sdata->u.sta.bssid);
622
Johannes Berge6a98542008-10-21 12:40:02 +0200623 if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
624 rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
Larry Fingerb3d88ad2007-06-10 17:57:33 -0700625 else
626 rate->value = 0;
Johannes Berg380a9422008-04-04 23:40:35 +0200627
628 rcu_read_unlock();
629
630 if (!sta)
631 return -ENODEV;
632
Johannes Berg8318d782008-01-24 19:38:38 +0100633 rate->value *= 100000;
Johannes Bergd0709a62008-02-25 16:27:46 +0100634
Larry Fingerb3d88ad2007-06-10 17:57:33 -0700635 return 0;
636}
637
Michael Buesch61609bc2007-09-20 22:06:39 +0200638static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
639 struct iw_request_info *info,
640 union iwreq_data *data, char *extra)
641{
642 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis R. Rodriguezd9d29252008-10-22 13:13:53 -0700643 struct ieee80211_channel* chan = local->hw.conf.channel;
Johannes Berge8975582008-10-09 12:18:51 +0200644 u32 reconf_flags = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100645 int new_power_level;
Michael Buesch61609bc2007-09-20 22:06:39 +0200646
647 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
648 return -EINVAL;
649 if (data->txpower.flags & IW_TXPOW_RANGE)
650 return -EINVAL;
Luis R. Rodriguezd9d29252008-10-22 13:13:53 -0700651 if (!chan)
652 return -EINVAL;
Michael Buesch61609bc2007-09-20 22:06:39 +0200653
Luis R. Rodriguezd9d29252008-10-22 13:13:53 -0700654 if (data->txpower.fixed)
655 new_power_level = min(data->txpower.value, chan->max_power);
656 else /* Automatic power level setting */
Johannes Berg8318d782008-01-24 19:38:38 +0100657 new_power_level = chan->max_power;
Mattias Nissler6a432952007-10-24 23:30:36 +0200658
659 if (local->hw.conf.power_level != new_power_level) {
660 local->hw.conf.power_level = new_power_level;
Johannes Berge8975582008-10-09 12:18:51 +0200661 reconf_flags |= IEEE80211_CONF_CHANGE_POWER;
Michael Buesch61609bc2007-09-20 22:06:39 +0200662 }
Mattias Nissler6a432952007-10-24 23:30:36 +0200663
Michael Buesch61609bc2007-09-20 22:06:39 +0200664 if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
665 local->hw.conf.radio_enabled = !(data->txpower.disabled);
Johannes Berge8975582008-10-09 12:18:51 +0200666 reconf_flags |= IEEE80211_CONF_CHANGE_RADIO_ENABLED;
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100667 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
Michael Buesch61609bc2007-09-20 22:06:39 +0200668 }
Mattias Nissler6a432952007-10-24 23:30:36 +0200669
Johannes Berge8975582008-10-09 12:18:51 +0200670 if (reconf_flags)
671 ieee80211_hw_config(local, reconf_flags);
Michael Buesch61609bc2007-09-20 22:06:39 +0200672
673 return 0;
674}
675
Larry Fingerfe6aa302007-08-10 11:23:20 -0500676static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
677 struct iw_request_info *info,
678 union iwreq_data *data, char *extra)
679{
680 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
681
682 data->txpower.fixed = 1;
683 data->txpower.disabled = !(local->hw.conf.radio_enabled);
684 data->txpower.value = local->hw.conf.power_level;
685 data->txpower.flags = IW_TXPOW_DBM;
686
687 return 0;
688}
689
Jiri Bencf0706e82007-05-05 11:45:53 -0700690static int ieee80211_ioctl_siwrts(struct net_device *dev,
691 struct iw_request_info *info,
692 struct iw_param *rts, char *extra)
693{
694 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
695
696 if (rts->disabled)
697 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
Emmanuel Grumbachfa6adfe2008-06-24 13:37:58 +0300698 else if (!rts->fixed)
699 /* if the rts value is not fixed, then take default */
700 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
Jiri Bencf0706e82007-05-05 11:45:53 -0700701 else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
702 return -EINVAL;
703 else
704 local->rts_threshold = rts->value;
705
706 /* If the wlan card performs RTS/CTS in hardware/firmware,
707 * configure it here */
708
709 if (local->ops->set_rts_threshold)
710 local->ops->set_rts_threshold(local_to_hw(local),
711 local->rts_threshold);
712
713 return 0;
714}
715
716static int ieee80211_ioctl_giwrts(struct net_device *dev,
717 struct iw_request_info *info,
718 struct iw_param *rts, char *extra)
719{
720 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
721
722 rts->value = local->rts_threshold;
723 rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
724 rts->fixed = 1;
725
726 return 0;
727}
728
729
730static int ieee80211_ioctl_siwfrag(struct net_device *dev,
731 struct iw_request_info *info,
732 struct iw_param *frag, char *extra)
733{
734 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
735
736 if (frag->disabled)
737 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
Emmanuel Grumbache4abd4d2008-07-03 18:02:27 +0300738 else if (!frag->fixed)
739 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
Jiri Bencf0706e82007-05-05 11:45:53 -0700740 else if (frag->value < 256 ||
741 frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
742 return -EINVAL;
743 else {
744 /* Fragment length must be even, so strip LSB. */
745 local->fragmentation_threshold = frag->value & ~0x1;
746 }
747
748 /* If the wlan card performs fragmentation in hardware/firmware,
749 * configure it here */
750
751 if (local->ops->set_frag_threshold)
Johannes Berg4233df62008-10-13 13:35:05 +0200752 return local->ops->set_frag_threshold(
Jiri Bencf0706e82007-05-05 11:45:53 -0700753 local_to_hw(local),
754 local->fragmentation_threshold);
755
756 return 0;
757}
758
759static int ieee80211_ioctl_giwfrag(struct net_device *dev,
760 struct iw_request_info *info,
761 struct iw_param *frag, char *extra)
762{
763 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
764
765 frag->value = local->fragmentation_threshold;
766 frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
767 frag->fixed = 1;
768
769 return 0;
770}
771
772
773static int ieee80211_ioctl_siwretry(struct net_device *dev,
774 struct iw_request_info *info,
775 struct iw_param *retry, char *extra)
776{
777 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
778
779 if (retry->disabled ||
780 (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
781 return -EINVAL;
782
Johannes Berg9124b072008-10-14 19:17:54 +0200783 if (retry->flags & IW_RETRY_MAX) {
784 local->hw.conf.long_frame_max_tx_count = retry->value;
785 } else if (retry->flags & IW_RETRY_MIN) {
786 local->hw.conf.short_frame_max_tx_count = retry->value;
787 } else {
788 local->hw.conf.long_frame_max_tx_count = retry->value;
789 local->hw.conf.short_frame_max_tx_count = retry->value;
Jiri Bencf0706e82007-05-05 11:45:53 -0700790 }
791
Johannes Berg9124b072008-10-14 19:17:54 +0200792 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
Jiri Bencf0706e82007-05-05 11:45:53 -0700793
794 return 0;
795}
796
797
798static int ieee80211_ioctl_giwretry(struct net_device *dev,
799 struct iw_request_info *info,
800 struct iw_param *retry, char *extra)
801{
802 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
803
804 retry->disabled = 0;
805 if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
806 /* first return min value, iwconfig will ask max value
807 * later if needed */
808 retry->flags |= IW_RETRY_LIMIT;
Johannes Berg9124b072008-10-14 19:17:54 +0200809 retry->value = local->hw.conf.short_frame_max_tx_count;
810 if (local->hw.conf.long_frame_max_tx_count !=
811 local->hw.conf.short_frame_max_tx_count)
Jiri Bencf0706e82007-05-05 11:45:53 -0700812 retry->flags |= IW_RETRY_MIN;
813 return 0;
814 }
815 if (retry->flags & IW_RETRY_MAX) {
816 retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
Johannes Berg9124b072008-10-14 19:17:54 +0200817 retry->value = local->hw.conf.long_frame_max_tx_count;
Jiri Bencf0706e82007-05-05 11:45:53 -0700818 }
819
820 return 0;
821}
822
Jiri Bencf0706e82007-05-05 11:45:53 -0700823static int ieee80211_ioctl_siwmlme(struct net_device *dev,
824 struct iw_request_info *info,
825 struct iw_point *data, char *extra)
826{
827 struct ieee80211_sub_if_data *sdata;
828 struct iw_mlme *mlme = (struct iw_mlme *) extra;
829
830 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg05c914f2008-09-11 00:01:58 +0200831 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
832 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Jiri Bencf0706e82007-05-05 11:45:53 -0700833 return -EINVAL;
834
835 switch (mlme->cmd) {
836 case IW_MLME_DEAUTH:
837 /* TODO: mlme->addr.sa_data */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200838 return ieee80211_sta_deauthenticate(sdata, mlme->reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -0700839 case IW_MLME_DISASSOC:
840 /* TODO: mlme->addr.sa_data */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200841 return ieee80211_sta_disassociate(sdata, mlme->reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -0700842 default:
843 return -EOPNOTSUPP;
844 }
845}
846
847
848static int ieee80211_ioctl_siwencode(struct net_device *dev,
849 struct iw_request_info *info,
850 struct iw_point *erq, char *keybuf)
851{
852 struct ieee80211_sub_if_data *sdata;
853 int idx, i, alg = ALG_WEP;
854 u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Johannes Berg628a1402007-09-26 17:53:17 +0200855 int remove = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700856
857 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
858
859 idx = erq->flags & IW_ENCODE_INDEX;
860 if (idx == 0) {
861 if (sdata->default_key)
862 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
863 if (sdata->default_key == sdata->keys[i]) {
864 idx = i;
865 break;
866 }
867 }
868 } else if (idx < 1 || idx > 4)
869 return -EINVAL;
870 else
871 idx--;
872
873 if (erq->flags & IW_ENCODE_DISABLED)
Johannes Berg628a1402007-09-26 17:53:17 +0200874 remove = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700875 else if (erq->length == 0) {
876 /* No key data - just set the default TX key index */
Johannes Berg11a843b2007-08-28 17:01:55 -0400877 ieee80211_set_default_key(sdata, idx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700878 return 0;
879 }
880
881 return ieee80211_set_encryption(
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200882 sdata, bcaddr,
Johannes Berg628a1402007-09-26 17:53:17 +0200883 idx, alg, remove,
Jiri Bencf0706e82007-05-05 11:45:53 -0700884 !sdata->default_key,
885 keybuf, erq->length);
886}
887
888
889static int ieee80211_ioctl_giwencode(struct net_device *dev,
890 struct iw_request_info *info,
891 struct iw_point *erq, char *key)
892{
893 struct ieee80211_sub_if_data *sdata;
894 int idx, i;
895
896 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
897
898 idx = erq->flags & IW_ENCODE_INDEX;
899 if (idx < 1 || idx > 4) {
900 idx = -1;
901 if (!sdata->default_key)
902 idx = 0;
903 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
904 if (sdata->default_key == sdata->keys[i]) {
905 idx = i;
906 break;
907 }
908 }
909 if (idx < 0)
910 return -EINVAL;
911 } else
912 idx--;
913
914 erq->flags = idx + 1;
915
916 if (!sdata->keys[idx]) {
917 erq->length = 0;
918 erq->flags |= IW_ENCODE_DISABLED;
919 return 0;
920 }
921
Johannes Berg8f20fc22007-08-28 17:01:54 -0400922 memcpy(key, sdata->keys[idx]->conf.key,
Johannes Berg11a843b2007-08-28 17:01:55 -0400923 min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400924 erq->length = sdata->keys[idx]->conf.keylen;
Jiri Bencf0706e82007-05-05 11:45:53 -0700925 erq->flags |= IW_ENCODE_ENABLED;
926
Johannes Berg05c914f2008-09-11 00:01:58 +0200927 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Emmanuel Grumbachb9fcc4f2008-06-24 13:37:59 +0300928 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
929 switch (ifsta->auth_alg) {
930 case WLAN_AUTH_OPEN:
931 case WLAN_AUTH_LEAP:
932 erq->flags |= IW_ENCODE_OPEN;
933 break;
934 case WLAN_AUTH_SHARED_KEY:
935 erq->flags |= IW_ENCODE_RESTRICTED;
936 break;
937 }
938 }
939
Jiri Bencf0706e82007-05-05 11:45:53 -0700940 return 0;
941}
942
Samuel Ortiz49292d52008-07-04 10:49:31 +0200943static int ieee80211_ioctl_siwpower(struct net_device *dev,
944 struct iw_request_info *info,
945 struct iw_param *wrq,
946 char *extra)
947{
948 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
949 struct ieee80211_conf *conf = &local->hw.conf;
950
951 if (wrq->disabled) {
952 conf->flags &= ~IEEE80211_CONF_PS;
Johannes Berge8975582008-10-09 12:18:51 +0200953 return ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Samuel Ortiz49292d52008-07-04 10:49:31 +0200954 }
955
956 switch (wrq->flags & IW_POWER_MODE) {
957 case IW_POWER_ON: /* If not specified */
958 case IW_POWER_MODE: /* If set all mask */
959 case IW_POWER_ALL_R: /* If explicitely state all */
960 conf->flags |= IEEE80211_CONF_PS;
961 break;
962 default: /* Otherwise we don't support it */
963 return -EINVAL;
964 }
965
Johannes Berge8975582008-10-09 12:18:51 +0200966 return ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Samuel Ortiz49292d52008-07-04 10:49:31 +0200967}
968
969static int ieee80211_ioctl_giwpower(struct net_device *dev,
970 struct iw_request_info *info,
971 union iwreq_data *wrqu,
972 char *extra)
973{
974 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
975 struct ieee80211_conf *conf = &local->hw.conf;
976
977 wrqu->power.disabled = !(conf->flags & IEEE80211_CONF_PS);
978
979 return 0;
980}
981
Jiri Bencf0706e82007-05-05 11:45:53 -0700982static int ieee80211_ioctl_siwauth(struct net_device *dev,
983 struct iw_request_info *info,
984 struct iw_param *data, char *extra)
985{
Jiri Bencf0706e82007-05-05 11:45:53 -0700986 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
987 int ret = 0;
988
989 switch (data->flags & IW_AUTH_INDEX) {
990 case IW_AUTH_WPA_VERSION:
991 case IW_AUTH_CIPHER_PAIRWISE:
992 case IW_AUTH_CIPHER_GROUP:
993 case IW_AUTH_WPA_ENABLED:
994 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
Jiri Bencf0706e82007-05-05 11:45:53 -0700995 case IW_AUTH_KEY_MGMT:
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000996 break;
Johannes Bergb1357a82007-11-28 11:04:21 +0100997 case IW_AUTH_DROP_UNENCRYPTED:
998 sdata->drop_unencrypted = !!data->value;
999 break;
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001000 case IW_AUTH_PRIVACY_INVOKED:
Johannes Berg05c914f2008-09-11 00:01:58 +02001001 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Jiri Bencf0706e82007-05-05 11:45:53 -07001002 ret = -EINVAL;
1003 else {
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001004 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001005 /*
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001006 * Privacy invoked by wpa_supplicant, store the
1007 * value and allow associating to a protected
1008 * network without having a key up front.
Jiri Bencf0706e82007-05-05 11:45:53 -07001009 */
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001010 if (data->value)
1011 sdata->u.sta.flags |=
1012 IEEE80211_STA_PRIVACY_INVOKED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001013 }
1014 break;
1015 case IW_AUTH_80211_AUTH_ALG:
Johannes Berg05c914f2008-09-11 00:01:58 +02001016 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
1017 sdata->vif.type == NL80211_IFTYPE_ADHOC)
Jiri Bencf0706e82007-05-05 11:45:53 -07001018 sdata->u.sta.auth_algs = data->value;
1019 else
1020 ret = -EOPNOTSUPP;
1021 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07001022 default:
1023 ret = -EOPNOTSUPP;
1024 break;
1025 }
1026 return ret;
1027}
1028
1029/* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
1030static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
1031{
1032 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1033 struct iw_statistics *wstats = &local->wstats;
1034 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1035 struct sta_info *sta = NULL;
1036
Johannes Berg98dd6a52008-04-10 15:36:09 +02001037 rcu_read_lock();
1038
Johannes Berg05c914f2008-09-11 00:01:58 +02001039 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
1040 sdata->vif.type == NL80211_IFTYPE_ADHOC)
Jiri Bencf0706e82007-05-05 11:45:53 -07001041 sta = sta_info_get(local, sdata->u.sta.bssid);
1042 if (!sta) {
1043 wstats->discard.fragment = 0;
1044 wstats->discard.misc = 0;
1045 wstats->qual.qual = 0;
1046 wstats->qual.level = 0;
1047 wstats->qual.noise = 0;
1048 wstats->qual.updated = IW_QUAL_ALL_INVALID;
1049 } else {
Bruno Randolf566bfe52008-05-08 19:15:40 +02001050 wstats->qual.level = sta->last_signal;
1051 wstats->qual.qual = sta->last_qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07001052 wstats->qual.noise = sta->last_noise;
1053 wstats->qual.updated = local->wstats_flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07001054 }
Johannes Berg98dd6a52008-04-10 15:36:09 +02001055
1056 rcu_read_unlock();
1057
Jiri Bencf0706e82007-05-05 11:45:53 -07001058 return wstats;
1059}
1060
1061static int ieee80211_ioctl_giwauth(struct net_device *dev,
1062 struct iw_request_info *info,
1063 struct iw_param *data, char *extra)
1064{
1065 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1066 int ret = 0;
1067
1068 switch (data->flags & IW_AUTH_INDEX) {
1069 case IW_AUTH_80211_AUTH_ALG:
Johannes Berg05c914f2008-09-11 00:01:58 +02001070 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
1071 sdata->vif.type == NL80211_IFTYPE_ADHOC)
Jiri Bencf0706e82007-05-05 11:45:53 -07001072 data->value = sdata->u.sta.auth_algs;
1073 else
1074 ret = -EOPNOTSUPP;
1075 break;
1076 default:
1077 ret = -EOPNOTSUPP;
1078 break;
1079 }
1080 return ret;
1081}
1082
1083
1084static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
1085 struct iw_request_info *info,
1086 struct iw_point *erq, char *extra)
1087{
1088 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1089 struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
Johannes Berg628a1402007-09-26 17:53:17 +02001090 int uninitialized_var(alg), idx, i, remove = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001091
1092 switch (ext->alg) {
1093 case IW_ENCODE_ALG_NONE:
Johannes Berg628a1402007-09-26 17:53:17 +02001094 remove = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001095 break;
1096 case IW_ENCODE_ALG_WEP:
1097 alg = ALG_WEP;
1098 break;
1099 case IW_ENCODE_ALG_TKIP:
1100 alg = ALG_TKIP;
1101 break;
1102 case IW_ENCODE_ALG_CCMP:
1103 alg = ALG_CCMP;
1104 break;
1105 default:
1106 return -EOPNOTSUPP;
1107 }
1108
1109 if (erq->flags & IW_ENCODE_DISABLED)
Johannes Berg628a1402007-09-26 17:53:17 +02001110 remove = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001111
1112 idx = erq->flags & IW_ENCODE_INDEX;
1113 if (idx < 1 || idx > 4) {
1114 idx = -1;
1115 if (!sdata->default_key)
1116 idx = 0;
1117 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1118 if (sdata->default_key == sdata->keys[i]) {
1119 idx = i;
1120 break;
1121 }
1122 }
1123 if (idx < 0)
1124 return -EINVAL;
1125 } else
1126 idx--;
1127
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001128 return ieee80211_set_encryption(sdata, ext->addr.sa_data, idx, alg,
Johannes Berg628a1402007-09-26 17:53:17 +02001129 remove,
Jiri Bencf0706e82007-05-05 11:45:53 -07001130 ext->ext_flags &
1131 IW_ENCODE_EXT_SET_TX_KEY,
1132 ext->key, ext->key_len);
1133}
1134
1135
Jiri Bencf0706e82007-05-05 11:45:53 -07001136/* Structures to export the Wireless Handlers */
1137
1138static const iw_handler ieee80211_handler[] =
1139{
1140 (iw_handler) NULL, /* SIOCSIWCOMMIT */
1141 (iw_handler) ieee80211_ioctl_giwname, /* SIOCGIWNAME */
1142 (iw_handler) NULL, /* SIOCSIWNWID */
1143 (iw_handler) NULL, /* SIOCGIWNWID */
1144 (iw_handler) ieee80211_ioctl_siwfreq, /* SIOCSIWFREQ */
1145 (iw_handler) ieee80211_ioctl_giwfreq, /* SIOCGIWFREQ */
1146 (iw_handler) ieee80211_ioctl_siwmode, /* SIOCSIWMODE */
1147 (iw_handler) ieee80211_ioctl_giwmode, /* SIOCGIWMODE */
1148 (iw_handler) NULL, /* SIOCSIWSENS */
1149 (iw_handler) NULL, /* SIOCGIWSENS */
1150 (iw_handler) NULL /* not used */, /* SIOCSIWRANGE */
1151 (iw_handler) ieee80211_ioctl_giwrange, /* SIOCGIWRANGE */
1152 (iw_handler) NULL /* not used */, /* SIOCSIWPRIV */
1153 (iw_handler) NULL /* kernel code */, /* SIOCGIWPRIV */
1154 (iw_handler) NULL /* not used */, /* SIOCSIWSTATS */
1155 (iw_handler) NULL /* kernel code */, /* SIOCGIWSTATS */
Johannes Berg5d4ecd92007-09-14 11:10:24 -04001156 (iw_handler) NULL, /* SIOCSIWSPY */
1157 (iw_handler) NULL, /* SIOCGIWSPY */
1158 (iw_handler) NULL, /* SIOCSIWTHRSPY */
1159 (iw_handler) NULL, /* SIOCGIWTHRSPY */
Jiri Bencf0706e82007-05-05 11:45:53 -07001160 (iw_handler) ieee80211_ioctl_siwap, /* SIOCSIWAP */
1161 (iw_handler) ieee80211_ioctl_giwap, /* SIOCGIWAP */
1162 (iw_handler) ieee80211_ioctl_siwmlme, /* SIOCSIWMLME */
1163 (iw_handler) NULL, /* SIOCGIWAPLIST */
1164 (iw_handler) ieee80211_ioctl_siwscan, /* SIOCSIWSCAN */
1165 (iw_handler) ieee80211_ioctl_giwscan, /* SIOCGIWSCAN */
1166 (iw_handler) ieee80211_ioctl_siwessid, /* SIOCSIWESSID */
1167 (iw_handler) ieee80211_ioctl_giwessid, /* SIOCGIWESSID */
1168 (iw_handler) NULL, /* SIOCSIWNICKN */
1169 (iw_handler) NULL, /* SIOCGIWNICKN */
1170 (iw_handler) NULL, /* -- hole -- */
1171 (iw_handler) NULL, /* -- hole -- */
Larry Finger1fd5e582007-07-10 19:32:10 +02001172 (iw_handler) ieee80211_ioctl_siwrate, /* SIOCSIWRATE */
Larry Fingerb3d88ad2007-06-10 17:57:33 -07001173 (iw_handler) ieee80211_ioctl_giwrate, /* SIOCGIWRATE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001174 (iw_handler) ieee80211_ioctl_siwrts, /* SIOCSIWRTS */
1175 (iw_handler) ieee80211_ioctl_giwrts, /* SIOCGIWRTS */
1176 (iw_handler) ieee80211_ioctl_siwfrag, /* SIOCSIWFRAG */
1177 (iw_handler) ieee80211_ioctl_giwfrag, /* SIOCGIWFRAG */
Michael Buesch61609bc2007-09-20 22:06:39 +02001178 (iw_handler) ieee80211_ioctl_siwtxpower, /* SIOCSIWTXPOW */
Larry Fingerfe6aa302007-08-10 11:23:20 -05001179 (iw_handler) ieee80211_ioctl_giwtxpower, /* SIOCGIWTXPOW */
Jiri Bencf0706e82007-05-05 11:45:53 -07001180 (iw_handler) ieee80211_ioctl_siwretry, /* SIOCSIWRETRY */
1181 (iw_handler) ieee80211_ioctl_giwretry, /* SIOCGIWRETRY */
1182 (iw_handler) ieee80211_ioctl_siwencode, /* SIOCSIWENCODE */
1183 (iw_handler) ieee80211_ioctl_giwencode, /* SIOCGIWENCODE */
Samuel Ortiz49292d52008-07-04 10:49:31 +02001184 (iw_handler) ieee80211_ioctl_siwpower, /* SIOCSIWPOWER */
1185 (iw_handler) ieee80211_ioctl_giwpower, /* SIOCGIWPOWER */
Jiri Bencf0706e82007-05-05 11:45:53 -07001186 (iw_handler) NULL, /* -- hole -- */
1187 (iw_handler) NULL, /* -- hole -- */
1188 (iw_handler) ieee80211_ioctl_siwgenie, /* SIOCSIWGENIE */
1189 (iw_handler) NULL, /* SIOCGIWGENIE */
1190 (iw_handler) ieee80211_ioctl_siwauth, /* SIOCSIWAUTH */
1191 (iw_handler) ieee80211_ioctl_giwauth, /* SIOCGIWAUTH */
1192 (iw_handler) ieee80211_ioctl_siwencodeext, /* SIOCSIWENCODEEXT */
1193 (iw_handler) NULL, /* SIOCGIWENCODEEXT */
1194 (iw_handler) NULL, /* SIOCSIWPMKSA */
1195 (iw_handler) NULL, /* -- hole -- */
1196};
1197
Jiri Bencf0706e82007-05-05 11:45:53 -07001198const struct iw_handler_def ieee80211_iw_handler_def =
1199{
1200 .num_standard = ARRAY_SIZE(ieee80211_handler),
Jiri Bencf0706e82007-05-05 11:45:53 -07001201 .standard = (iw_handler *) ieee80211_handler,
Jiri Bencf0706e82007-05-05 11:45:53 -07001202 .get_wireless_stats = ieee80211_get_wireless_stats,
1203};