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