]> err.no Git - linux-2.6/blob - net/mac80211/ieee80211_ioctl.c
the scheduled ieee80211 softmac removal
[linux-2.6] / net / mac80211 / ieee80211_ioctl.c
1 /*
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"
24 #include "ieee80211_led.h"
25 #include "ieee80211_rate.h"
26 #include "wpa.h"
27 #include "aes_ccm.h"
28
29
30 static int ieee80211_set_encryption(struct net_device *dev, u8 *sta_addr,
31                                     int idx, int alg, int remove,
32                                     int set_tx_key, const u8 *_key,
33                                     size_t key_len)
34 {
35         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
36         struct sta_info *sta;
37         struct ieee80211_key *key;
38         struct ieee80211_sub_if_data *sdata;
39
40         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
41
42         if (idx < 0 || idx >= NUM_DEFAULT_KEYS) {
43                 printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
44                        dev->name, idx);
45                 return -EINVAL;
46         }
47
48         if (remove) {
49                 if (is_broadcast_ether_addr(sta_addr)) {
50                         key = sdata->keys[idx];
51                 } else {
52                         sta = sta_info_get(local, sta_addr);
53                         if (!sta)
54                                 return -ENOENT;
55                         key = sta->key;
56                 }
57
58                 if (!key)
59                         return -ENOENT;
60
61                 ieee80211_key_free(key);
62                 return 0;
63         } else {
64                 key = ieee80211_key_alloc(alg, idx, key_len, _key);
65                 if (!key)
66                         return -ENOMEM;
67
68                 sta = NULL;
69
70                 if (!is_broadcast_ether_addr(sta_addr)) {
71                         set_tx_key = 0;
72                         /*
73                          * According to the standard, the key index of a
74                          * pairwise key must be zero. However, some AP are
75                          * broken when it comes to WEP key indices, so we
76                          * work around this.
77                          */
78                         if (idx != 0 && alg != ALG_WEP) {
79                                 ieee80211_key_free(key);
80                                 return -EINVAL;
81                         }
82
83                         sta = sta_info_get(local, sta_addr);
84                         if (!sta) {
85                                 ieee80211_key_free(key);
86                                 return -ENOENT;
87                         }
88                 }
89
90                 ieee80211_key_link(key, sdata, sta);
91
92                 if (set_tx_key || (!sta && !sdata->default_key && key))
93                         ieee80211_set_default_key(sdata, idx);
94         }
95
96         return 0;
97 }
98
99 static int ieee80211_ioctl_siwgenie(struct net_device *dev,
100                                     struct iw_request_info *info,
101                                     struct iw_point *data, char *extra)
102 {
103         struct ieee80211_sub_if_data *sdata;
104
105         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
106
107         if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME)
108                 return -EOPNOTSUPP;
109
110         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
111             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
112                 int ret = ieee80211_sta_set_extra_ie(dev, extra, data->length);
113                 if (ret)
114                         return ret;
115                 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
116                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
117                 return 0;
118         }
119
120         return -EOPNOTSUPP;
121 }
122
123 static int ieee80211_ioctl_giwname(struct net_device *dev,
124                                    struct iw_request_info *info,
125                                    char *name, char *extra)
126 {
127         strcpy(name, "IEEE 802.11");
128
129         return 0;
130 }
131
132
133 static int ieee80211_ioctl_giwrange(struct net_device *dev,
134                                  struct iw_request_info *info,
135                                  struct iw_point *data, char *extra)
136 {
137         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
138         struct iw_range *range = (struct iw_range *) extra;
139         enum ieee80211_band band;
140         int c = 0;
141
142         data->length = sizeof(struct iw_range);
143         memset(range, 0, sizeof(struct iw_range));
144
145         range->we_version_compiled = WIRELESS_EXT;
146         range->we_version_source = 21;
147         range->retry_capa = IW_RETRY_LIMIT;
148         range->retry_flags = IW_RETRY_LIMIT;
149         range->min_retry = 0;
150         range->max_retry = 255;
151         range->min_rts = 0;
152         range->max_rts = 2347;
153         range->min_frag = 256;
154         range->max_frag = 2346;
155
156         range->encoding_size[0] = 5;
157         range->encoding_size[1] = 13;
158         range->num_encoding_sizes = 2;
159         range->max_encoding_tokens = NUM_DEFAULT_KEYS;
160
161         range->max_qual.qual = local->hw.max_signal;
162         range->max_qual.level = local->hw.max_rssi;
163         range->max_qual.noise = local->hw.max_noise;
164         range->max_qual.updated = local->wstats_flags;
165
166         range->avg_qual.qual = local->hw.max_signal/2;
167         range->avg_qual.level = 0;
168         range->avg_qual.noise = 0;
169         range->avg_qual.updated = local->wstats_flags;
170
171         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
172                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
173
174
175         for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
176                 int i;
177                 struct ieee80211_supported_band *sband;
178
179                 sband = local->hw.wiphy->bands[band];
180
181                 if (!sband)
182                         continue;
183
184                 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
185                         struct ieee80211_channel *chan = &sband->channels[i];
186
187                         if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
188                                 range->freq[c].i =
189                                         ieee80211_frequency_to_channel(
190                                                 chan->center_freq);
191                                 range->freq[c].m = chan->center_freq;
192                                 range->freq[c].e = 6;
193                                 c++;
194                         }
195                 }
196         }
197         range->num_channels = c;
198         range->num_frequency = c;
199
200         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
201         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
202         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
203         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
204
205         range->scan_capa |= IW_SCAN_CAPA_ESSID;
206
207         return 0;
208 }
209
210
211 static int ieee80211_ioctl_siwmode(struct net_device *dev,
212                                    struct iw_request_info *info,
213                                    __u32 *mode, char *extra)
214 {
215         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
216         int type;
217
218         if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
219                 return -EOPNOTSUPP;
220
221         switch (*mode) {
222         case IW_MODE_INFRA:
223                 type = IEEE80211_IF_TYPE_STA;
224                 break;
225         case IW_MODE_ADHOC:
226                 type = IEEE80211_IF_TYPE_IBSS;
227                 break;
228         case IW_MODE_MONITOR:
229                 type = IEEE80211_IF_TYPE_MNTR;
230                 break;
231         default:
232                 return -EINVAL;
233         }
234
235         if (type == sdata->vif.type)
236                 return 0;
237         if (netif_running(dev))
238                 return -EBUSY;
239
240         ieee80211_if_reinit(dev);
241         ieee80211_if_set_type(dev, type);
242
243         return 0;
244 }
245
246
247 static int ieee80211_ioctl_giwmode(struct net_device *dev,
248                                    struct iw_request_info *info,
249                                    __u32 *mode, char *extra)
250 {
251         struct ieee80211_sub_if_data *sdata;
252
253         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
254         switch (sdata->vif.type) {
255         case IEEE80211_IF_TYPE_AP:
256                 *mode = IW_MODE_MASTER;
257                 break;
258         case IEEE80211_IF_TYPE_STA:
259                 *mode = IW_MODE_INFRA;
260                 break;
261         case IEEE80211_IF_TYPE_IBSS:
262                 *mode = IW_MODE_ADHOC;
263                 break;
264         case IEEE80211_IF_TYPE_MNTR:
265                 *mode = IW_MODE_MONITOR;
266                 break;
267         case IEEE80211_IF_TYPE_WDS:
268                 *mode = IW_MODE_REPEAT;
269                 break;
270         case IEEE80211_IF_TYPE_VLAN:
271                 *mode = IW_MODE_SECOND;         /* FIXME */
272                 break;
273         default:
274                 *mode = IW_MODE_AUTO;
275                 break;
276         }
277         return 0;
278 }
279
280 int ieee80211_set_freq(struct ieee80211_local *local, int freqMHz)
281 {
282         int set = 0;
283         int ret = -EINVAL;
284         enum ieee80211_band band;
285         struct ieee80211_supported_band *sband;
286         int i;
287
288         for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
289                 sband = local->hw.wiphy->bands[band];
290
291                 if (!sband)
292                         continue;
293
294                 for (i = 0; i < sband->n_channels; i++) {
295                         struct ieee80211_channel *chan = &sband->channels[i];
296
297                         if (chan->flags & IEEE80211_CHAN_DISABLED)
298                                 continue;
299
300                         if (chan->center_freq == freqMHz) {
301                                 set = 1;
302                                 local->oper_channel = chan;
303                                 break;
304                         }
305                 }
306                 if (set)
307                         break;
308         }
309
310         if (set) {
311                 if (local->sta_sw_scanning)
312                         ret = 0;
313                 else
314                         ret = ieee80211_hw_config(local);
315
316                 rate_control_clear(local);
317         }
318
319         return ret;
320 }
321
322 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
323                                    struct iw_request_info *info,
324                                    struct iw_freq *freq, char *extra)
325 {
326         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
327         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
328
329         if (sdata->vif.type == IEEE80211_IF_TYPE_STA)
330                 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
331
332         /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
333         if (freq->e == 0) {
334                 if (freq->m < 0) {
335                         if (sdata->vif.type == IEEE80211_IF_TYPE_STA)
336                                 sdata->u.sta.flags |=
337                                         IEEE80211_STA_AUTO_CHANNEL_SEL;
338                         return 0;
339                 } else
340                         return ieee80211_set_freq(local,
341                                 ieee80211_channel_to_frequency(freq->m));
342         } else {
343                 int i, div = 1000000;
344                 for (i = 0; i < freq->e; i++)
345                         div /= 10;
346                 if (div > 0)
347                         return ieee80211_set_freq(local, freq->m / div);
348                 else
349                         return -EINVAL;
350         }
351 }
352
353
354 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
355                                    struct iw_request_info *info,
356                                    struct iw_freq *freq, char *extra)
357 {
358         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
359
360         freq->m = local->hw.conf.channel->center_freq;
361         freq->e = 6;
362
363         return 0;
364 }
365
366
367 static int ieee80211_ioctl_siwessid(struct net_device *dev,
368                                     struct iw_request_info *info,
369                                     struct iw_point *data, char *ssid)
370 {
371         struct ieee80211_sub_if_data *sdata;
372         size_t len = data->length;
373
374         /* iwconfig uses nul termination in SSID.. */
375         if (len > 0 && ssid[len - 1] == '\0')
376                 len--;
377
378         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
379         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
380             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
381                 int ret;
382                 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
383                         if (len > IEEE80211_MAX_SSID_LEN)
384                                 return -EINVAL;
385                         memcpy(sdata->u.sta.ssid, ssid, len);
386                         sdata->u.sta.ssid_len = len;
387                         return 0;
388                 }
389                 if (data->flags)
390                         sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
391                 else
392                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_SSID_SEL;
393                 ret = ieee80211_sta_set_ssid(dev, ssid, len);
394                 if (ret)
395                         return ret;
396                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
397                 return 0;
398         }
399
400         if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
401                 memcpy(sdata->u.ap.ssid, ssid, len);
402                 memset(sdata->u.ap.ssid + len, 0,
403                        IEEE80211_MAX_SSID_LEN - len);
404                 sdata->u.ap.ssid_len = len;
405                 return ieee80211_if_config(dev);
406         }
407         return -EOPNOTSUPP;
408 }
409
410
411 static int ieee80211_ioctl_giwessid(struct net_device *dev,
412                                     struct iw_request_info *info,
413                                     struct iw_point *data, char *ssid)
414 {
415         size_t len;
416
417         struct ieee80211_sub_if_data *sdata;
418         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
419         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
420             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
421                 int res = ieee80211_sta_get_ssid(dev, ssid, &len);
422                 if (res == 0) {
423                         data->length = len;
424                         data->flags = 1;
425                 } else
426                         data->flags = 0;
427                 return res;
428         }
429
430         if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
431                 len = sdata->u.ap.ssid_len;
432                 if (len > IW_ESSID_MAX_SIZE)
433                         len = IW_ESSID_MAX_SIZE;
434                 memcpy(ssid, sdata->u.ap.ssid, len);
435                 data->length = len;
436                 data->flags = 1;
437                 return 0;
438         }
439         return -EOPNOTSUPP;
440 }
441
442
443 static int ieee80211_ioctl_siwap(struct net_device *dev,
444                                  struct iw_request_info *info,
445                                  struct sockaddr *ap_addr, char *extra)
446 {
447         struct ieee80211_sub_if_data *sdata;
448
449         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
450         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
451             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
452                 int ret;
453                 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
454                         memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
455                                ETH_ALEN);
456                         return 0;
457                 }
458                 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
459                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
460                                 IEEE80211_STA_AUTO_CHANNEL_SEL;
461                 else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
462                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
463                 else
464                         sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
465                 ret = ieee80211_sta_set_bssid(dev, (u8 *) &ap_addr->sa_data);
466                 if (ret)
467                         return ret;
468                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
469                 return 0;
470         } else if (sdata->vif.type == IEEE80211_IF_TYPE_WDS) {
471                 /*
472                  * If it is necessary to update the WDS peer address
473                  * while the interface is running, then we need to do
474                  * more work here, namely if it is running we need to
475                  * add a new and remove the old STA entry, this is
476                  * normally handled by _open() and _stop().
477                  */
478                 if (netif_running(dev))
479                         return -EBUSY;
480
481                 memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
482                        ETH_ALEN);
483
484                 return 0;
485         }
486
487         return -EOPNOTSUPP;
488 }
489
490
491 static int ieee80211_ioctl_giwap(struct net_device *dev,
492                                  struct iw_request_info *info,
493                                  struct sockaddr *ap_addr, char *extra)
494 {
495         struct ieee80211_sub_if_data *sdata;
496
497         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
498         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
499             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
500                 ap_addr->sa_family = ARPHRD_ETHER;
501                 memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
502                 return 0;
503         } else if (sdata->vif.type == IEEE80211_IF_TYPE_WDS) {
504                 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
513 static int ieee80211_ioctl_siwscan(struct net_device *dev,
514                                    struct iw_request_info *info,
515                                    union iwreq_data *wrqu, char *extra)
516 {
517         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
518         struct iw_scan_req *req = NULL;
519         u8 *ssid = NULL;
520         size_t ssid_len = 0;
521
522         if (!netif_running(dev))
523                 return -ENETDOWN;
524
525         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
526             sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
527             sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT &&
528             sdata->vif.type != IEEE80211_IF_TYPE_AP)
529                 return -EOPNOTSUPP;
530
531         /* if SSID was specified explicitly then use that */
532         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;
537         }
538
539         return ieee80211_sta_req_scan(dev, ssid, ssid_len);
540 }
541
542
543 static 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);
549
550         if (local->sta_sw_scanning || local->sta_hw_scanning)
551                 return -EAGAIN;
552
553         res = ieee80211_sta_scan_results(dev, extra, data->length);
554         if (res >= 0) {
555                 data->length = res;
556                 return 0;
557         }
558         data->length = 0;
559         return res;
560 }
561
562
563 static int ieee80211_ioctl_siwrate(struct net_device *dev,
564                                   struct iw_request_info *info,
565                                   struct iw_param *rate, char *extra)
566 {
567         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
568         int i, err = -EINVAL;
569         u32 target_rate = rate->value / 100000;
570         struct ieee80211_sub_if_data *sdata;
571         struct ieee80211_supported_band *sband;
572
573         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
574         if (!sdata->bss)
575                 return -ENODEV;
576
577         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
578
579         /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
580          * target_rate = X, rate->fixed = 1 means only rate X
581          * target_rate = X, rate->fixed = 0 means all rates <= X */
582         sdata->bss->max_ratectrl_rateidx = -1;
583         sdata->bss->force_unicast_rateidx = -1;
584         if (rate->value < 0)
585                 return 0;
586
587         for (i=0; i< sband->n_bitrates; i++) {
588                 struct ieee80211_rate *brate = &sband->bitrates[i];
589                 int this_rate = brate->bitrate;
590
591                 if (target_rate == this_rate) {
592                         sdata->bss->max_ratectrl_rateidx = i;
593                         if (rate->fixed)
594                                 sdata->bss->force_unicast_rateidx = i;
595                         err = 0;
596                         break;
597                 }
598         }
599         return err;
600 }
601
602 static int ieee80211_ioctl_giwrate(struct net_device *dev,
603                                   struct iw_request_info *info,
604                                   struct iw_param *rate, char *extra)
605 {
606         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
607         struct sta_info *sta;
608         struct ieee80211_sub_if_data *sdata;
609         struct ieee80211_supported_band *sband;
610
611         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
612
613         if (sdata->vif.type == IEEE80211_IF_TYPE_STA)
614                 sta = sta_info_get(local, sdata->u.sta.bssid);
615         else
616                 return -EOPNOTSUPP;
617         if (!sta)
618                 return -ENODEV;
619
620         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
621
622         if (sta->txrate_idx < sband->n_bitrates)
623                 rate->value = sband->bitrates[sta->txrate_idx].bitrate;
624         else
625                 rate->value = 0;
626         rate->value *= 100000;
627
628         return 0;
629 }
630
631 static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
632                                       struct iw_request_info *info,
633                                       union iwreq_data *data, char *extra)
634 {
635         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
636         bool need_reconfig = 0;
637         int new_power_level;
638
639         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
640                 return -EINVAL;
641         if (data->txpower.flags & IW_TXPOW_RANGE)
642                 return -EINVAL;
643
644         if (data->txpower.fixed) {
645                 new_power_level = data->txpower.value;
646         } else {
647                 /*
648                  * Automatic power level. Use maximum power for the current
649                  * channel. Should be part of rate control.
650                  */
651                 struct ieee80211_channel* chan = local->hw.conf.channel;
652                 if (!chan)
653                         return -EINVAL;
654
655                 new_power_level = chan->max_power;
656         }
657
658         if (local->hw.conf.power_level != new_power_level) {
659                 local->hw.conf.power_level = new_power_level;
660                 need_reconfig = 1;
661         }
662
663         if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
664                 local->hw.conf.radio_enabled = !(data->txpower.disabled);
665                 need_reconfig = 1;
666                 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
667         }
668
669         if (need_reconfig) {
670                 ieee80211_hw_config(local);
671                 /* The return value of hw_config is not of big interest here,
672                  * as it doesn't say that it failed because of _this_ config
673                  * change or something else. Ignore it. */
674         }
675
676         return 0;
677 }
678
679 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
680                                    struct iw_request_info *info,
681                                    union iwreq_data *data, char *extra)
682 {
683         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
684
685         data->txpower.fixed = 1;
686         data->txpower.disabled = !(local->hw.conf.radio_enabled);
687         data->txpower.value = local->hw.conf.power_level;
688         data->txpower.flags = IW_TXPOW_DBM;
689
690         return 0;
691 }
692
693 static int ieee80211_ioctl_siwrts(struct net_device *dev,
694                                   struct iw_request_info *info,
695                                   struct iw_param *rts, char *extra)
696 {
697         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
698
699         if (rts->disabled)
700                 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
701         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
716 static 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
730 static 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;
738         else if (frag->value < 256 ||
739                  frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
740                 return -EINVAL;
741         else {
742                 /* Fragment length must be even, so strip LSB. */
743                 local->fragmentation_threshold = frag->value & ~0x1;
744         }
745
746         /* If the wlan card performs fragmentation in hardware/firmware,
747          * configure it here */
748
749         if (local->ops->set_frag_threshold)
750                 local->ops->set_frag_threshold(
751                         local_to_hw(local),
752                         local->fragmentation_threshold);
753
754         return 0;
755 }
756
757 static int ieee80211_ioctl_giwfrag(struct net_device *dev,
758                                    struct iw_request_info *info,
759                                    struct iw_param *frag, char *extra)
760 {
761         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
762
763         frag->value = local->fragmentation_threshold;
764         frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
765         frag->fixed = 1;
766
767         return 0;
768 }
769
770
771 static int ieee80211_ioctl_siwretry(struct net_device *dev,
772                                     struct iw_request_info *info,
773                                     struct iw_param *retry, char *extra)
774 {
775         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
776
777         if (retry->disabled ||
778             (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
779                 return -EINVAL;
780
781         if (retry->flags & IW_RETRY_MAX)
782                 local->long_retry_limit = retry->value;
783         else if (retry->flags & IW_RETRY_MIN)
784                 local->short_retry_limit = retry->value;
785         else {
786                 local->long_retry_limit = retry->value;
787                 local->short_retry_limit = retry->value;
788         }
789
790         if (local->ops->set_retry_limit) {
791                 return local->ops->set_retry_limit(
792                         local_to_hw(local),
793                         local->short_retry_limit,
794                         local->long_retry_limit);
795         }
796
797         return 0;
798 }
799
800
801 static int ieee80211_ioctl_giwretry(struct net_device *dev,
802                                     struct iw_request_info *info,
803                                     struct iw_param *retry, char *extra)
804 {
805         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
806
807         retry->disabled = 0;
808         if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
809                 /* first return min value, iwconfig will ask max value
810                  * later if needed */
811                 retry->flags |= IW_RETRY_LIMIT;
812                 retry->value = local->short_retry_limit;
813                 if (local->long_retry_limit != local->short_retry_limit)
814                         retry->flags |= IW_RETRY_MIN;
815                 return 0;
816         }
817         if (retry->flags & IW_RETRY_MAX) {
818                 retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
819                 retry->value = local->long_retry_limit;
820         }
821
822         return 0;
823 }
824
825 static int ieee80211_ioctl_siwmlme(struct net_device *dev,
826                                    struct iw_request_info *info,
827                                    struct iw_point *data, char *extra)
828 {
829         struct ieee80211_sub_if_data *sdata;
830         struct iw_mlme *mlme = (struct iw_mlme *) extra;
831
832         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
833         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
834             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
835                 return -EINVAL;
836
837         switch (mlme->cmd) {
838         case IW_MLME_DEAUTH:
839                 /* TODO: mlme->addr.sa_data */
840                 return ieee80211_sta_deauthenticate(dev, mlme->reason_code);
841         case IW_MLME_DISASSOC:
842                 /* TODO: mlme->addr.sa_data */
843                 return ieee80211_sta_disassociate(dev, mlme->reason_code);
844         default:
845                 return -EOPNOTSUPP;
846         }
847 }
848
849
850 static int ieee80211_ioctl_siwencode(struct net_device *dev,
851                                      struct iw_request_info *info,
852                                      struct iw_point *erq, char *keybuf)
853 {
854         struct ieee80211_sub_if_data *sdata;
855         int idx, i, alg = ALG_WEP;
856         u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
857         int remove = 0;
858
859         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
860
861         idx = erq->flags & IW_ENCODE_INDEX;
862         if (idx == 0) {
863                 if (sdata->default_key)
864                         for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
865                                 if (sdata->default_key == sdata->keys[i]) {
866                                         idx = i;
867                                         break;
868                                 }
869                         }
870         } else if (idx < 1 || idx > 4)
871                 return -EINVAL;
872         else
873                 idx--;
874
875         if (erq->flags & IW_ENCODE_DISABLED)
876                 remove = 1;
877         else if (erq->length == 0) {
878                 /* No key data - just set the default TX key index */
879                 ieee80211_set_default_key(sdata, idx);
880                 return 0;
881         }
882
883         return ieee80211_set_encryption(
884                 dev, bcaddr,
885                 idx, alg, remove,
886                 !sdata->default_key,
887                 keybuf, erq->length);
888 }
889
890
891 static int ieee80211_ioctl_giwencode(struct net_device *dev,
892                                      struct iw_request_info *info,
893                                      struct iw_point *erq, char *key)
894 {
895         struct ieee80211_sub_if_data *sdata;
896         int idx, i;
897
898         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
899
900         idx = erq->flags & IW_ENCODE_INDEX;
901         if (idx < 1 || idx > 4) {
902                 idx = -1;
903                 if (!sdata->default_key)
904                         idx = 0;
905                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
906                         if (sdata->default_key == sdata->keys[i]) {
907                                 idx = i;
908                                 break;
909                         }
910                 }
911                 if (idx < 0)
912                         return -EINVAL;
913         } else
914                 idx--;
915
916         erq->flags = idx + 1;
917
918         if (!sdata->keys[idx]) {
919                 erq->length = 0;
920                 erq->flags |= IW_ENCODE_DISABLED;
921                 return 0;
922         }
923
924         memcpy(key, sdata->keys[idx]->conf.key,
925                min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
926         erq->length = sdata->keys[idx]->conf.keylen;
927         erq->flags |= IW_ENCODE_ENABLED;
928
929         return 0;
930 }
931
932 static int ieee80211_ioctl_siwauth(struct net_device *dev,
933                                    struct iw_request_info *info,
934                                    struct iw_param *data, char *extra)
935 {
936         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
937         int ret = 0;
938
939         switch (data->flags & IW_AUTH_INDEX) {
940         case IW_AUTH_WPA_VERSION:
941         case IW_AUTH_CIPHER_PAIRWISE:
942         case IW_AUTH_CIPHER_GROUP:
943         case IW_AUTH_WPA_ENABLED:
944         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
945         case IW_AUTH_KEY_MGMT:
946                 break;
947         case IW_AUTH_DROP_UNENCRYPTED:
948                 sdata->drop_unencrypted = !!data->value;
949                 break;
950         case IW_AUTH_PRIVACY_INVOKED:
951                 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
952                         ret = -EINVAL;
953                 else {
954                         sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
955                         /*
956                          * Privacy invoked by wpa_supplicant, store the
957                          * value and allow associating to a protected
958                          * network without having a key up front.
959                          */
960                         if (data->value)
961                                 sdata->u.sta.flags |=
962                                         IEEE80211_STA_PRIVACY_INVOKED;
963                 }
964                 break;
965         case IW_AUTH_80211_AUTH_ALG:
966                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
967                     sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
968                         sdata->u.sta.auth_algs = data->value;
969                 else
970                         ret = -EOPNOTSUPP;
971                 break;
972         default:
973                 ret = -EOPNOTSUPP;
974                 break;
975         }
976         return ret;
977 }
978
979 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
980 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
981 {
982         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
983         struct iw_statistics *wstats = &local->wstats;
984         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
985         struct sta_info *sta = NULL;
986
987         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
988             sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
989                 sta = sta_info_get(local, sdata->u.sta.bssid);
990         if (!sta) {
991                 wstats->discard.fragment = 0;
992                 wstats->discard.misc = 0;
993                 wstats->qual.qual = 0;
994                 wstats->qual.level = 0;
995                 wstats->qual.noise = 0;
996                 wstats->qual.updated = IW_QUAL_ALL_INVALID;
997         } else {
998                 wstats->qual.level = sta->last_rssi;
999                 wstats->qual.qual = sta->last_signal;
1000                 wstats->qual.noise = sta->last_noise;
1001                 wstats->qual.updated = local->wstats_flags;
1002         }
1003         return wstats;
1004 }
1005
1006 static int ieee80211_ioctl_giwauth(struct net_device *dev,
1007                                    struct iw_request_info *info,
1008                                    struct iw_param *data, char *extra)
1009 {
1010         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1011         int ret = 0;
1012
1013         switch (data->flags & IW_AUTH_INDEX) {
1014         case IW_AUTH_80211_AUTH_ALG:
1015                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
1016                     sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
1017                         data->value = sdata->u.sta.auth_algs;
1018                 else
1019                         ret = -EOPNOTSUPP;
1020                 break;
1021         default:
1022                 ret = -EOPNOTSUPP;
1023                 break;
1024         }
1025         return ret;
1026 }
1027
1028
1029 static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
1030                                         struct iw_request_info *info,
1031                                         struct iw_point *erq, char *extra)
1032 {
1033         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1034         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
1035         int uninitialized_var(alg), idx, i, remove = 0;
1036
1037         switch (ext->alg) {
1038         case IW_ENCODE_ALG_NONE:
1039                 remove = 1;
1040                 break;
1041         case IW_ENCODE_ALG_WEP:
1042                 alg = ALG_WEP;
1043                 break;
1044         case IW_ENCODE_ALG_TKIP:
1045                 alg = ALG_TKIP;
1046                 break;
1047         case IW_ENCODE_ALG_CCMP:
1048                 alg = ALG_CCMP;
1049                 break;
1050         default:
1051                 return -EOPNOTSUPP;
1052         }
1053
1054         if (erq->flags & IW_ENCODE_DISABLED)
1055                 remove = 1;
1056
1057         idx = erq->flags & IW_ENCODE_INDEX;
1058         if (idx < 1 || idx > 4) {
1059                 idx = -1;
1060                 if (!sdata->default_key)
1061                         idx = 0;
1062                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1063                         if (sdata->default_key == sdata->keys[i]) {
1064                                 idx = i;
1065                                 break;
1066                         }
1067                 }
1068                 if (idx < 0)
1069                         return -EINVAL;
1070         } else
1071                 idx--;
1072
1073         return ieee80211_set_encryption(dev, ext->addr.sa_data, idx, alg,
1074                                         remove,
1075                                         ext->ext_flags &
1076                                         IW_ENCODE_EXT_SET_TX_KEY,
1077                                         ext->key, ext->key_len);
1078 }
1079
1080
1081 /* Structures to export the Wireless Handlers */
1082
1083 static const iw_handler ieee80211_handler[] =
1084 {
1085         (iw_handler) NULL,                              /* SIOCSIWCOMMIT */
1086         (iw_handler) ieee80211_ioctl_giwname,           /* SIOCGIWNAME */
1087         (iw_handler) NULL,                              /* SIOCSIWNWID */
1088         (iw_handler) NULL,                              /* SIOCGIWNWID */
1089         (iw_handler) ieee80211_ioctl_siwfreq,           /* SIOCSIWFREQ */
1090         (iw_handler) ieee80211_ioctl_giwfreq,           /* SIOCGIWFREQ */
1091         (iw_handler) ieee80211_ioctl_siwmode,           /* SIOCSIWMODE */
1092         (iw_handler) ieee80211_ioctl_giwmode,           /* SIOCGIWMODE */
1093         (iw_handler) NULL,                              /* SIOCSIWSENS */
1094         (iw_handler) NULL,                              /* SIOCGIWSENS */
1095         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE */
1096         (iw_handler) ieee80211_ioctl_giwrange,          /* SIOCGIWRANGE */
1097         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV */
1098         (iw_handler) NULL /* kernel code */,            /* SIOCGIWPRIV */
1099         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS */
1100         (iw_handler) NULL /* kernel code */,            /* SIOCGIWSTATS */
1101         (iw_handler) NULL,                              /* SIOCSIWSPY */
1102         (iw_handler) NULL,                              /* SIOCGIWSPY */
1103         (iw_handler) NULL,                              /* SIOCSIWTHRSPY */
1104         (iw_handler) NULL,                              /* SIOCGIWTHRSPY */
1105         (iw_handler) ieee80211_ioctl_siwap,             /* SIOCSIWAP */
1106         (iw_handler) ieee80211_ioctl_giwap,             /* SIOCGIWAP */
1107         (iw_handler) ieee80211_ioctl_siwmlme,           /* SIOCSIWMLME */
1108         (iw_handler) NULL,                              /* SIOCGIWAPLIST */
1109         (iw_handler) ieee80211_ioctl_siwscan,           /* SIOCSIWSCAN */
1110         (iw_handler) ieee80211_ioctl_giwscan,           /* SIOCGIWSCAN */
1111         (iw_handler) ieee80211_ioctl_siwessid,          /* SIOCSIWESSID */
1112         (iw_handler) ieee80211_ioctl_giwessid,          /* SIOCGIWESSID */
1113         (iw_handler) NULL,                              /* SIOCSIWNICKN */
1114         (iw_handler) NULL,                              /* SIOCGIWNICKN */
1115         (iw_handler) NULL,                              /* -- hole -- */
1116         (iw_handler) NULL,                              /* -- hole -- */
1117         (iw_handler) ieee80211_ioctl_siwrate,           /* SIOCSIWRATE */
1118         (iw_handler) ieee80211_ioctl_giwrate,           /* SIOCGIWRATE */
1119         (iw_handler) ieee80211_ioctl_siwrts,            /* SIOCSIWRTS */
1120         (iw_handler) ieee80211_ioctl_giwrts,            /* SIOCGIWRTS */
1121         (iw_handler) ieee80211_ioctl_siwfrag,           /* SIOCSIWFRAG */
1122         (iw_handler) ieee80211_ioctl_giwfrag,           /* SIOCGIWFRAG */
1123         (iw_handler) ieee80211_ioctl_siwtxpower,        /* SIOCSIWTXPOW */
1124         (iw_handler) ieee80211_ioctl_giwtxpower,        /* SIOCGIWTXPOW */
1125         (iw_handler) ieee80211_ioctl_siwretry,          /* SIOCSIWRETRY */
1126         (iw_handler) ieee80211_ioctl_giwretry,          /* SIOCGIWRETRY */
1127         (iw_handler) ieee80211_ioctl_siwencode,         /* SIOCSIWENCODE */
1128         (iw_handler) ieee80211_ioctl_giwencode,         /* SIOCGIWENCODE */
1129         (iw_handler) NULL,                              /* SIOCSIWPOWER */
1130         (iw_handler) NULL,                              /* SIOCGIWPOWER */
1131         (iw_handler) NULL,                              /* -- hole -- */
1132         (iw_handler) NULL,                              /* -- hole -- */
1133         (iw_handler) ieee80211_ioctl_siwgenie,          /* SIOCSIWGENIE */
1134         (iw_handler) NULL,                              /* SIOCGIWGENIE */
1135         (iw_handler) ieee80211_ioctl_siwauth,           /* SIOCSIWAUTH */
1136         (iw_handler) ieee80211_ioctl_giwauth,           /* SIOCGIWAUTH */
1137         (iw_handler) ieee80211_ioctl_siwencodeext,      /* SIOCSIWENCODEEXT */
1138         (iw_handler) NULL,                              /* SIOCGIWENCODEEXT */
1139         (iw_handler) NULL,                              /* SIOCSIWPMKSA */
1140         (iw_handler) NULL,                              /* -- hole -- */
1141 };
1142
1143 const struct iw_handler_def ieee80211_iw_handler_def =
1144 {
1145         .num_standard   = ARRAY_SIZE(ieee80211_handler),
1146         .standard       = (iw_handler *) ieee80211_handler,
1147         .get_wireless_stats = ieee80211_get_wireless_stats,
1148 };