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