73909ec85f2a3cb015285f10d52dd67491e409c6
[cascardo/linux.git] / net / mac80211 / ieee80211_ioctl.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/etherdevice.h>
17 #include <linux/if_arp.h>
18 #include <linux/wireless.h>
19 #include <net/iw_handler.h>
20 #include <asm/uaccess.h>
21
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "hostapd_ioctl.h"
25 #include "ieee80211_rate.h"
26 #include "wpa.h"
27 #include "aes_ccm.h"
28
29 static int ieee80211_regdom = 0x10; /* FCC */
30 module_param(ieee80211_regdom, int, 0444);
31 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain; 64=MKK");
32
33 /*
34  * If firmware is upgraded by the vendor, additional channels can be used based
35  * on the new Japanese regulatory rules. This is indicated by setting
36  * ieee80211_japan_5ghz module parameter to one when loading the 80211 kernel
37  * module.
38  */
39 static int ieee80211_japan_5ghz /* = 0 */;
40 module_param(ieee80211_japan_5ghz, int, 0444);
41 MODULE_PARM_DESC(ieee80211_japan_5ghz, "Vendor-updated firmware for 5 GHz");
42
43 static void ieee80211_set_hw_encryption(struct net_device *dev,
44                                         struct sta_info *sta, u8 addr[ETH_ALEN],
45                                         struct ieee80211_key *key)
46 {
47         struct ieee80211_key_conf *keyconf = NULL;
48         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
49
50         /* default to sw encryption; this will be cleared by low-level
51          * driver if the hw supports requested encryption */
52         if (key)
53                 key->force_sw_encrypt = 1;
54
55         if (key && local->ops->set_key &&
56             (keyconf = ieee80211_key_data2conf(local, key))) {
57                 if (local->ops->set_key(local_to_hw(local), SET_KEY, addr,
58                                        keyconf, sta ? sta->aid : 0)) {
59                         key->force_sw_encrypt = 1;
60                         key->hw_key_idx = HW_KEY_IDX_INVALID;
61                 } else {
62                         key->force_sw_encrypt =
63                                 !!(keyconf->flags & IEEE80211_KEY_FORCE_SW_ENCRYPT);
64                         key->hw_key_idx =
65                                 keyconf->hw_key_idx;
66
67                 }
68         }
69         kfree(keyconf);
70 }
71
72
73 static int ieee80211_set_encryption(struct net_device *dev, u8 *sta_addr,
74                                     int idx, int alg, int set_tx_key,
75                                     const u8 *_key, size_t key_len)
76 {
77         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
78         int ret = 0;
79         struct sta_info *sta;
80         struct ieee80211_key *key, *old_key;
81         int try_hwaccel = 1;
82         struct ieee80211_key_conf *keyconf;
83         struct ieee80211_sub_if_data *sdata;
84
85         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
86
87         if (is_broadcast_ether_addr(sta_addr)) {
88                 sta = NULL;
89                 if (idx >= NUM_DEFAULT_KEYS) {
90                         printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
91                                dev->name, idx);
92                         return -EINVAL;
93                 }
94                 key = sdata->keys[idx];
95
96                 /* TODO: consider adding hwaccel support for these; at least
97                  * Atheros key cache should be able to handle this since AP is
98                  * only transmitting frames with default keys. */
99                 /* FIX: hw key cache can be used when only one virtual
100                  * STA is associated with each AP. If more than one STA
101                  * is associated to the same AP, software encryption
102                  * must be used. This should be done automatically
103                  * based on configured station devices. For the time
104                  * being, this can be only set at compile time. */
105         } else {
106                 set_tx_key = 0;
107                 if (idx != 0) {
108                         printk(KERN_DEBUG "%s: set_encrypt - non-zero idx for "
109                                "individual key\n", dev->name);
110                         return -EINVAL;
111                 }
112
113                 sta = sta_info_get(local, sta_addr);
114                 if (!sta) {
115 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
116                         printk(KERN_DEBUG "%s: set_encrypt - unknown addr "
117                                MAC_FMT "\n",
118                                dev->name, MAC_ARG(sta_addr));
119 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
120
121                         return -ENOENT;
122                 }
123
124                 key = sta->key;
125         }
126
127         /* FIX:
128          * Cannot configure default hwaccel keys with WEP algorithm, if
129          * any of the virtual interfaces is using static WEP
130          * configuration because hwaccel would otherwise try to decrypt
131          * these frames.
132          *
133          * For now, just disable WEP hwaccel for broadcast when there is
134          * possibility of conflict with default keys. This can maybe later be
135          * optimized by using non-default keys (at least with Atheros ar521x).
136          */
137         if (!sta && alg == ALG_WEP && !local->default_wep_only &&
138             sdata->type != IEEE80211_IF_TYPE_IBSS &&
139             sdata->type != IEEE80211_IF_TYPE_AP) {
140                 try_hwaccel = 0;
141         }
142
143         if (local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP) {
144                 /* Software encryption cannot be used with devices that hide
145                  * encryption from the host system, so always try to use
146                  * hardware acceleration with such devices. */
147                 try_hwaccel = 1;
148         }
149
150         if ((local->hw.flags & IEEE80211_HW_NO_TKIP_WMM_HWACCEL) &&
151             alg == ALG_TKIP) {
152                 if (sta && (sta->flags & WLAN_STA_WME)) {
153                 /* Hardware does not support hwaccel with TKIP when using WMM.
154                  */
155                         try_hwaccel = 0;
156                 }
157                 else if (sdata->type == IEEE80211_IF_TYPE_STA) {
158                         sta = sta_info_get(local, sdata->u.sta.bssid);
159                         if (sta) {
160                                 if (sta->flags & WLAN_STA_WME) {
161                                         try_hwaccel = 0;
162                                 }
163                                 sta_info_put(sta);
164                                 sta = NULL;
165                         }
166                 }
167         }
168
169         if (alg == ALG_NONE) {
170                 keyconf = NULL;
171                 if (try_hwaccel && key &&
172                     key->hw_key_idx != HW_KEY_IDX_INVALID &&
173                     local->ops->set_key &&
174                     (keyconf = ieee80211_key_data2conf(local, key)) != NULL &&
175                     local->ops->set_key(local_to_hw(local), DISABLE_KEY,
176                                        sta_addr, keyconf, sta ? sta->aid : 0)) {
177                         printk(KERN_DEBUG "%s: set_encrypt - low-level disable"
178                                " failed\n", dev->name);
179                         ret = -EINVAL;
180                 }
181                 kfree(keyconf);
182
183                 if (set_tx_key || sdata->default_key == key)
184                         sdata->default_key = NULL;
185                 if (sta)
186                         sta->key = NULL;
187                 else
188                         sdata->keys[idx] = NULL;
189                 ieee80211_key_free(key);
190                 key = NULL;
191         } else {
192                 old_key = key;
193                 key = ieee80211_key_alloc(sta ? NULL : sdata, idx, key_len,
194                                           GFP_KERNEL);
195                 if (!key) {
196                         ret = -ENOMEM;
197                         goto err_out;
198                 }
199
200                 /* default to sw encryption; low-level driver sets these if the
201                  * requested encryption is supported */
202                 key->hw_key_idx = HW_KEY_IDX_INVALID;
203                 key->force_sw_encrypt = 1;
204
205                 key->alg = alg;
206                 key->keyidx = idx;
207                 key->keylen = key_len;
208                 memcpy(key->key, _key, key_len);
209                 if (set_tx_key)
210                         key->default_tx_key = 1;
211
212                 if (alg == ALG_CCMP) {
213                         /* Initialize AES key state here as an optimization
214                          * so that it does not need to be initialized for every
215                          * packet. */
216                         key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
217                                 key->key);
218                         if (!key->u.ccmp.tfm) {
219                                 ret = -ENOMEM;
220                                 goto err_free;
221                         }
222                 }
223
224                 if (set_tx_key || sdata->default_key == old_key)
225                         sdata->default_key = NULL;
226                 if (sta)
227                         sta->key = key;
228                 else
229                         sdata->keys[idx] = key;
230                 ieee80211_key_free(old_key);
231
232                 if (try_hwaccel &&
233                     (alg == ALG_WEP || alg == ALG_TKIP || alg == ALG_CCMP))
234                         ieee80211_set_hw_encryption(dev, sta, sta_addr, key);
235         }
236
237         if (set_tx_key || (!sta && !sdata->default_key && key)) {
238                 sdata->default_key = key;
239
240                 if (local->ops->set_key_idx &&
241                     local->ops->set_key_idx(local_to_hw(local), idx))
242                         printk(KERN_DEBUG "%s: failed to set TX key idx for "
243                                "low-level driver\n", dev->name);
244         }
245
246         if (sta)
247                 sta_info_put(sta);
248
249         return 0;
250
251 err_free:
252         ieee80211_key_free(key);
253 err_out:
254         if (sta)
255                 sta_info_put(sta);
256         return ret;
257 }
258
259 static int ieee80211_ioctl_siwgenie(struct net_device *dev,
260                                     struct iw_request_info *info,
261                                     struct iw_point *data, char *extra)
262 {
263         struct ieee80211_sub_if_data *sdata;
264         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
265
266         if (local->user_space_mlme)
267                 return -EOPNOTSUPP;
268
269         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
270         if (sdata->type == IEEE80211_IF_TYPE_STA ||
271             sdata->type == IEEE80211_IF_TYPE_IBSS) {
272                 int ret = ieee80211_sta_set_extra_ie(dev, extra, data->length);
273                 if (ret)
274                         return ret;
275                 sdata->u.sta.auto_bssid_sel = 0;
276                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
277                 return 0;
278         }
279
280         if (sdata->type == IEEE80211_IF_TYPE_AP) {
281                 kfree(sdata->u.ap.generic_elem);
282                 sdata->u.ap.generic_elem = kmalloc(data->length, GFP_KERNEL);
283                 if (!sdata->u.ap.generic_elem)
284                         return -ENOMEM;
285                 memcpy(sdata->u.ap.generic_elem, extra, data->length);
286                 sdata->u.ap.generic_elem_len = data->length;
287                 return ieee80211_if_config(dev);
288         }
289         return -EOPNOTSUPP;
290 }
291
292 static int ieee80211_ioctl_set_radio_enabled(struct net_device *dev,
293                                              int val)
294 {
295         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
296         struct ieee80211_conf *conf = &local->hw.conf;
297
298         conf->radio_enabled = val;
299         return ieee80211_hw_config(wdev_priv(dev->ieee80211_ptr));
300 }
301
302 static int ieee80211_ioctl_giwname(struct net_device *dev,
303                                    struct iw_request_info *info,
304                                    char *name, char *extra)
305 {
306         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
307
308         switch (local->hw.conf.phymode) {
309         case MODE_IEEE80211A:
310                 strcpy(name, "IEEE 802.11a");
311                 break;
312         case MODE_IEEE80211B:
313                 strcpy(name, "IEEE 802.11b");
314                 break;
315         case MODE_IEEE80211G:
316                 strcpy(name, "IEEE 802.11g");
317                 break;
318         case MODE_ATHEROS_TURBO:
319                 strcpy(name, "5GHz Turbo");
320                 break;
321         default:
322                 strcpy(name, "IEEE 802.11");
323                 break;
324         }
325
326         return 0;
327 }
328
329
330 static int ieee80211_ioctl_giwrange(struct net_device *dev,
331                                  struct iw_request_info *info,
332                                  struct iw_point *data, char *extra)
333 {
334         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
335         struct iw_range *range = (struct iw_range *) extra;
336
337         data->length = sizeof(struct iw_range);
338         memset(range, 0, sizeof(struct iw_range));
339
340         range->we_version_compiled = WIRELESS_EXT;
341         range->we_version_source = 21;
342         range->retry_capa = IW_RETRY_LIMIT;
343         range->retry_flags = IW_RETRY_LIMIT;
344         range->min_retry = 0;
345         range->max_retry = 255;
346         range->min_rts = 0;
347         range->max_rts = 2347;
348         range->min_frag = 256;
349         range->max_frag = 2346;
350
351         range->encoding_size[0] = 5;
352         range->encoding_size[1] = 13;
353         range->num_encoding_sizes = 2;
354         range->max_encoding_tokens = NUM_DEFAULT_KEYS;
355
356         range->max_qual.qual = local->hw.max_signal;
357         range->max_qual.level = local->hw.max_rssi;
358         range->max_qual.noise = local->hw.max_noise;
359         range->max_qual.updated = local->wstats_flags;
360
361         range->avg_qual.qual = local->hw.max_signal/2;
362         range->avg_qual.level = 0;
363         range->avg_qual.noise = 0;
364         range->avg_qual.updated = local->wstats_flags;
365
366         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
367                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
368
369         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
370         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
371         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
372         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
373
374         return 0;
375 }
376
377
378 struct ieee80211_channel_range {
379         short start_freq;
380         short end_freq;
381         unsigned char power_level;
382         unsigned char antenna_max;
383 };
384
385 static const struct ieee80211_channel_range ieee80211_fcc_channels[] = {
386         { 2412, 2462, 27, 6 } /* IEEE 802.11b/g, channels 1..11 */,
387         { 5180, 5240, 17, 6 } /* IEEE 802.11a, channels 36..48 */,
388         { 5260, 5320, 23, 6 } /* IEEE 802.11a, channels 52..64 */,
389         { 5745, 5825, 30, 6 } /* IEEE 802.11a, channels 149..165, outdoor */,
390         { 0 }
391 };
392
393 static const struct ieee80211_channel_range ieee80211_mkk_channels[] = {
394         { 2412, 2472, 20, 6 } /* IEEE 802.11b/g, channels 1..13 */,
395         { 5170, 5240, 20, 6 } /* IEEE 802.11a, channels 34..48 */,
396         { 5260, 5320, 20, 6 } /* IEEE 802.11a, channels 52..64 */,
397         { 0 }
398 };
399
400
401 static const struct ieee80211_channel_range *channel_range =
402         ieee80211_fcc_channels;
403
404
405 static void ieee80211_unmask_channel(struct net_device *dev, int mode,
406                                      struct ieee80211_channel *chan)
407 {
408         int i;
409
410         chan->flag = 0;
411
412         if (ieee80211_regdom == 64 &&
413             (mode == MODE_ATHEROS_TURBO || mode == MODE_ATHEROS_TURBOG)) {
414                 /* Do not allow Turbo modes in Japan. */
415                 return;
416         }
417
418         for (i = 0; channel_range[i].start_freq; i++) {
419                 const struct ieee80211_channel_range *r = &channel_range[i];
420                 if (r->start_freq <= chan->freq && r->end_freq >= chan->freq) {
421                         if (ieee80211_regdom == 64 && !ieee80211_japan_5ghz &&
422                             chan->freq >= 5260 && chan->freq <= 5320) {
423                                 /*
424                                  * Skip new channels in Japan since the
425                                  * firmware was not marked having been upgraded
426                                  * by the vendor.
427                                  */
428                                 continue;
429                         }
430
431                         if (ieee80211_regdom == 0x10 &&
432                             (chan->freq == 5190 || chan->freq == 5210 ||
433                              chan->freq == 5230)) {
434                                     /* Skip MKK channels when in FCC domain. */
435                                     continue;
436                         }
437
438                         chan->flag |= IEEE80211_CHAN_W_SCAN |
439                                 IEEE80211_CHAN_W_ACTIVE_SCAN |
440                                 IEEE80211_CHAN_W_IBSS;
441                         chan->power_level = r->power_level;
442                         chan->antenna_max = r->antenna_max;
443
444                         if (ieee80211_regdom == 64 &&
445                             (chan->freq == 5170 || chan->freq == 5190 ||
446                              chan->freq == 5210 || chan->freq == 5230)) {
447                                 /*
448                                  * New regulatory rules in Japan have backwards
449                                  * compatibility with old channels in 5.15-5.25
450                                  * GHz band, but the station is not allowed to
451                                  * use active scan on these old channels.
452                                  */
453                                 chan->flag &= ~IEEE80211_CHAN_W_ACTIVE_SCAN;
454                         }
455
456                         if (ieee80211_regdom == 64 &&
457                             (chan->freq == 5260 || chan->freq == 5280 ||
458                              chan->freq == 5300 || chan->freq == 5320)) {
459                                 /*
460                                  * IBSS is not allowed on 5.25-5.35 GHz band
461                                  * due to radar detection requirements.
462                                  */
463                                 chan->flag &= ~IEEE80211_CHAN_W_IBSS;
464                         }
465
466                         break;
467                 }
468         }
469 }
470
471
472 static int ieee80211_unmask_channels(struct net_device *dev)
473 {
474         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
475         struct ieee80211_hw_mode *mode;
476         int c;
477
478         list_for_each_entry(mode, &local->modes_list, list) {
479                 for (c = 0; c < mode->num_channels; c++) {
480                         ieee80211_unmask_channel(dev, mode->mode,
481                                                  &mode->channels[c]);
482                 }
483         }
484         return 0;
485 }
486
487
488 int ieee80211_init_client(struct net_device *dev)
489 {
490         if (ieee80211_regdom == 0x40)
491                 channel_range = ieee80211_mkk_channels;
492         ieee80211_unmask_channels(dev);
493         return 0;
494 }
495
496
497 static int ieee80211_ioctl_siwmode(struct net_device *dev,
498                                    struct iw_request_info *info,
499                                    __u32 *mode, char *extra)
500 {
501         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
502         int type;
503
504         if (sdata->type == IEEE80211_IF_TYPE_VLAN)
505                 return -EOPNOTSUPP;
506
507         switch (*mode) {
508         case IW_MODE_INFRA:
509                 type = IEEE80211_IF_TYPE_STA;
510                 break;
511         case IW_MODE_ADHOC:
512                 type = IEEE80211_IF_TYPE_IBSS;
513                 break;
514         case IW_MODE_MONITOR:
515                 type = IEEE80211_IF_TYPE_MNTR;
516                 break;
517         default:
518                 return -EINVAL;
519         }
520
521         if (type == sdata->type)
522                 return 0;
523         if (netif_running(dev))
524                 return -EBUSY;
525
526         ieee80211_if_reinit(dev);
527         ieee80211_if_set_type(dev, type);
528
529         return 0;
530 }
531
532
533 static int ieee80211_ioctl_giwmode(struct net_device *dev,
534                                    struct iw_request_info *info,
535                                    __u32 *mode, char *extra)
536 {
537         struct ieee80211_sub_if_data *sdata;
538
539         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
540         switch (sdata->type) {
541         case IEEE80211_IF_TYPE_AP:
542                 *mode = IW_MODE_MASTER;
543                 break;
544         case IEEE80211_IF_TYPE_STA:
545                 *mode = IW_MODE_INFRA;
546                 break;
547         case IEEE80211_IF_TYPE_IBSS:
548                 *mode = IW_MODE_ADHOC;
549                 break;
550         case IEEE80211_IF_TYPE_MNTR:
551                 *mode = IW_MODE_MONITOR;
552                 break;
553         case IEEE80211_IF_TYPE_WDS:
554                 *mode = IW_MODE_REPEAT;
555                 break;
556         case IEEE80211_IF_TYPE_VLAN:
557                 *mode = IW_MODE_SECOND;         /* FIXME */
558                 break;
559         default:
560                 *mode = IW_MODE_AUTO;
561                 break;
562         }
563         return 0;
564 }
565
566 int ieee80211_set_channel(struct ieee80211_local *local, int channel, int freq)
567 {
568         struct ieee80211_hw_mode *mode;
569         int c, set = 0;
570         int ret = -EINVAL;
571
572         list_for_each_entry(mode, &local->modes_list, list) {
573                 if (!(local->enabled_modes & (1 << mode->mode)))
574                         continue;
575                 for (c = 0; c < mode->num_channels; c++) {
576                         struct ieee80211_channel *chan = &mode->channels[c];
577                         if (chan->flag & IEEE80211_CHAN_W_SCAN &&
578                             ((chan->chan == channel) || (chan->freq == freq))) {
579                                 /* Use next_mode as the mode preference to
580                                  * resolve non-unique channel numbers. */
581                                 if (set && mode->mode != local->next_mode)
582                                         continue;
583
584                                 local->oper_channel = chan;
585                                 local->oper_hw_mode = mode;
586                                 set++;
587                         }
588                 }
589         }
590
591         if (set) {
592                 if (local->sta_scanning)
593                         ret = 0;
594                 else
595                         ret = ieee80211_hw_config(local);
596
597                 rate_control_clear(local);
598         }
599
600         return ret;
601 }
602
603 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
604                                    struct iw_request_info *info,
605                                    struct iw_freq *freq, char *extra)
606 {
607         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
608         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
609
610         if (sdata->type == IEEE80211_IF_TYPE_STA)
611                 sdata->u.sta.auto_channel_sel = 0;
612
613         /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
614         if (freq->e == 0) {
615                 if (freq->m < 0) {
616                         if (sdata->type == IEEE80211_IF_TYPE_STA)
617                                 sdata->u.sta.auto_channel_sel = 1;
618                         return 0;
619                 } else
620                         return ieee80211_set_channel(local, freq->m, -1);
621         } else {
622                 int i, div = 1000000;
623                 for (i = 0; i < freq->e; i++)
624                         div /= 10;
625                 if (div > 0)
626                         return ieee80211_set_channel(local, -1, freq->m / div);
627                 else
628                         return -EINVAL;
629         }
630 }
631
632
633 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
634                                    struct iw_request_info *info,
635                                    struct iw_freq *freq, char *extra)
636 {
637         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
638
639         /* TODO: in station mode (Managed/Ad-hoc) might need to poll low-level
640          * driver for the current channel with firmware-based management */
641
642         freq->m = local->hw.conf.freq;
643         freq->e = 6;
644
645         return 0;
646 }
647
648
649 static int ieee80211_ioctl_siwessid(struct net_device *dev,
650                                     struct iw_request_info *info,
651                                     struct iw_point *data, char *ssid)
652 {
653         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
654         struct ieee80211_sub_if_data *sdata;
655         size_t len = data->length;
656
657         /* iwconfig uses nul termination in SSID.. */
658         if (len > 0 && ssid[len - 1] == '\0')
659                 len--;
660
661         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
662         if (sdata->type == IEEE80211_IF_TYPE_STA ||
663             sdata->type == IEEE80211_IF_TYPE_IBSS) {
664                 int ret;
665                 if (local->user_space_mlme) {
666                         if (len > IEEE80211_MAX_SSID_LEN)
667                                 return -EINVAL;
668                         memcpy(sdata->u.sta.ssid, ssid, len);
669                         sdata->u.sta.ssid_len = len;
670                         return 0;
671                 }
672                 sdata->u.sta.auto_ssid_sel = !data->flags;
673                 ret = ieee80211_sta_set_ssid(dev, ssid, len);
674                 if (ret)
675                         return ret;
676                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
677                 return 0;
678         }
679
680         if (sdata->type == IEEE80211_IF_TYPE_AP) {
681                 memcpy(sdata->u.ap.ssid, ssid, len);
682                 memset(sdata->u.ap.ssid + len, 0,
683                        IEEE80211_MAX_SSID_LEN - len);
684                 sdata->u.ap.ssid_len = len;
685                 return ieee80211_if_config(dev);
686         }
687         return -EOPNOTSUPP;
688 }
689
690
691 static int ieee80211_ioctl_giwessid(struct net_device *dev,
692                                     struct iw_request_info *info,
693                                     struct iw_point *data, char *ssid)
694 {
695         size_t len;
696
697         struct ieee80211_sub_if_data *sdata;
698         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
699         if (sdata->type == IEEE80211_IF_TYPE_STA ||
700             sdata->type == IEEE80211_IF_TYPE_IBSS) {
701                 int res = ieee80211_sta_get_ssid(dev, ssid, &len);
702                 if (res == 0) {
703                         data->length = len;
704                         data->flags = 1;
705                 } else
706                         data->flags = 0;
707                 return res;
708         }
709
710         if (sdata->type == IEEE80211_IF_TYPE_AP) {
711                 len = sdata->u.ap.ssid_len;
712                 if (len > IW_ESSID_MAX_SIZE)
713                         len = IW_ESSID_MAX_SIZE;
714                 memcpy(ssid, sdata->u.ap.ssid, len);
715                 data->length = len;
716                 data->flags = 1;
717                 return 0;
718         }
719         return -EOPNOTSUPP;
720 }
721
722
723 static int ieee80211_ioctl_siwap(struct net_device *dev,
724                                  struct iw_request_info *info,
725                                  struct sockaddr *ap_addr, char *extra)
726 {
727         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
728         struct ieee80211_sub_if_data *sdata;
729
730         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
731         if (sdata->type == IEEE80211_IF_TYPE_STA ||
732             sdata->type == IEEE80211_IF_TYPE_IBSS) {
733                 int ret;
734                 if (local->user_space_mlme) {
735                         memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
736                                ETH_ALEN);
737                         return 0;
738                 }
739                 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data)) {
740                         sdata->u.sta.auto_bssid_sel = 1;
741                         sdata->u.sta.auto_channel_sel = 1;
742                 } else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
743                         sdata->u.sta.auto_bssid_sel = 1;
744                 else
745                         sdata->u.sta.auto_bssid_sel = 0;
746                 ret = ieee80211_sta_set_bssid(dev, (u8 *) &ap_addr->sa_data);
747                 if (ret)
748                         return ret;
749                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
750                 return 0;
751         } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
752                 if (memcmp(sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
753                            ETH_ALEN) == 0)
754                         return 0;
755                 return ieee80211_if_update_wds(dev, (u8 *) &ap_addr->sa_data);
756         }
757
758         return -EOPNOTSUPP;
759 }
760
761
762 static int ieee80211_ioctl_giwap(struct net_device *dev,
763                                  struct iw_request_info *info,
764                                  struct sockaddr *ap_addr, char *extra)
765 {
766         struct ieee80211_sub_if_data *sdata;
767
768         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
769         if (sdata->type == IEEE80211_IF_TYPE_STA ||
770             sdata->type == IEEE80211_IF_TYPE_IBSS) {
771                 ap_addr->sa_family = ARPHRD_ETHER;
772                 memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
773                 return 0;
774         } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
775                 ap_addr->sa_family = ARPHRD_ETHER;
776                 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
777                 return 0;
778         }
779
780         return -EOPNOTSUPP;
781 }
782
783
784 static int ieee80211_ioctl_siwscan(struct net_device *dev,
785                                    struct iw_request_info *info,
786                                    struct iw_point *data, char *extra)
787 {
788         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
789         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
790         u8 *ssid = NULL;
791         size_t ssid_len = 0;
792
793         if (!netif_running(dev))
794                 return -ENETDOWN;
795
796         if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID) {
797                 if (sdata->type == IEEE80211_IF_TYPE_STA ||
798                     sdata->type == IEEE80211_IF_TYPE_IBSS) {
799                         ssid = sdata->u.sta.ssid;
800                         ssid_len = sdata->u.sta.ssid_len;
801                 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
802                         ssid = sdata->u.ap.ssid;
803                         ssid_len = sdata->u.ap.ssid_len;
804                 } else
805                         return -EINVAL;
806         }
807         return ieee80211_sta_req_scan(dev, ssid, ssid_len);
808 }
809
810
811 static int ieee80211_ioctl_giwscan(struct net_device *dev,
812                                    struct iw_request_info *info,
813                                    struct iw_point *data, char *extra)
814 {
815         int res;
816         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
817         if (local->sta_scanning)
818                 return -EAGAIN;
819         res = ieee80211_sta_scan_results(dev, extra, data->length);
820         if (res >= 0) {
821                 data->length = res;
822                 return 0;
823         }
824         data->length = 0;
825         return res;
826 }
827
828
829 static int ieee80211_ioctl_siwrts(struct net_device *dev,
830                                   struct iw_request_info *info,
831                                   struct iw_param *rts, char *extra)
832 {
833         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
834
835         if (rts->disabled)
836                 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
837         else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
838                 return -EINVAL;
839         else
840                 local->rts_threshold = rts->value;
841
842         /* If the wlan card performs RTS/CTS in hardware/firmware,
843          * configure it here */
844
845         if (local->ops->set_rts_threshold)
846                 local->ops->set_rts_threshold(local_to_hw(local),
847                                              local->rts_threshold);
848
849         return 0;
850 }
851
852 static int ieee80211_ioctl_giwrts(struct net_device *dev,
853                                   struct iw_request_info *info,
854                                   struct iw_param *rts, char *extra)
855 {
856         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
857
858         rts->value = local->rts_threshold;
859         rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
860         rts->fixed = 1;
861
862         return 0;
863 }
864
865
866 static int ieee80211_ioctl_siwfrag(struct net_device *dev,
867                                    struct iw_request_info *info,
868                                    struct iw_param *frag, char *extra)
869 {
870         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
871
872         if (frag->disabled)
873                 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
874         else if (frag->value < 256 ||
875                  frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
876                 return -EINVAL;
877         else {
878                 /* Fragment length must be even, so strip LSB. */
879                 local->fragmentation_threshold = frag->value & ~0x1;
880         }
881
882         /* If the wlan card performs fragmentation in hardware/firmware,
883          * configure it here */
884
885         if (local->ops->set_frag_threshold)
886                 local->ops->set_frag_threshold(
887                         local_to_hw(local),
888                         local->fragmentation_threshold);
889
890         return 0;
891 }
892
893 static int ieee80211_ioctl_giwfrag(struct net_device *dev,
894                                    struct iw_request_info *info,
895                                    struct iw_param *frag, char *extra)
896 {
897         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
898
899         frag->value = local->fragmentation_threshold;
900         frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
901         frag->fixed = 1;
902
903         return 0;
904 }
905
906
907 static int ieee80211_ioctl_siwretry(struct net_device *dev,
908                                     struct iw_request_info *info,
909                                     struct iw_param *retry, char *extra)
910 {
911         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
912
913         if (retry->disabled ||
914             (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
915                 return -EINVAL;
916
917         if (retry->flags & IW_RETRY_MAX)
918                 local->long_retry_limit = retry->value;
919         else if (retry->flags & IW_RETRY_MIN)
920                 local->short_retry_limit = retry->value;
921         else {
922                 local->long_retry_limit = retry->value;
923                 local->short_retry_limit = retry->value;
924         }
925
926         if (local->ops->set_retry_limit) {
927                 return local->ops->set_retry_limit(
928                         local_to_hw(local),
929                         local->short_retry_limit,
930                         local->long_retry_limit);
931         }
932
933         return 0;
934 }
935
936
937 static int ieee80211_ioctl_giwretry(struct net_device *dev,
938                                     struct iw_request_info *info,
939                                     struct iw_param *retry, char *extra)
940 {
941         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
942
943         retry->disabled = 0;
944         if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
945                 /* first return min value, iwconfig will ask max value
946                  * later if needed */
947                 retry->flags |= IW_RETRY_LIMIT;
948                 retry->value = local->short_retry_limit;
949                 if (local->long_retry_limit != local->short_retry_limit)
950                         retry->flags |= IW_RETRY_MIN;
951                 return 0;
952         }
953         if (retry->flags & IW_RETRY_MAX) {
954                 retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
955                 retry->value = local->long_retry_limit;
956         }
957
958         return 0;
959 }
960
961 static int ieee80211_ioctl_clear_keys(struct net_device *dev)
962 {
963         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
964         struct ieee80211_key_conf key;
965         int i;
966         u8 addr[ETH_ALEN];
967         struct ieee80211_key_conf *keyconf;
968         struct ieee80211_sub_if_data *sdata;
969         struct sta_info *sta;
970
971         memset(addr, 0xff, ETH_ALEN);
972         read_lock(&local->sub_if_lock);
973         list_for_each_entry(sdata, &local->sub_if_list, list) {
974                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
975                         keyconf = NULL;
976                         if (sdata->keys[i] &&
977                             !sdata->keys[i]->force_sw_encrypt &&
978                             local->ops->set_key &&
979                             (keyconf = ieee80211_key_data2conf(local,
980                                                                sdata->keys[i])))
981                                 local->ops->set_key(local_to_hw(local),
982                                                    DISABLE_KEY, addr,
983                                                    keyconf, 0);
984                         kfree(keyconf);
985                         ieee80211_key_free(sdata->keys[i]);
986                         sdata->keys[i] = NULL;
987                 }
988                 sdata->default_key = NULL;
989         }
990         read_unlock(&local->sub_if_lock);
991
992         spin_lock_bh(&local->sta_lock);
993         list_for_each_entry(sta, &local->sta_list, list) {
994                 keyconf = NULL;
995                 if (sta->key && !sta->key->force_sw_encrypt &&
996                     local->ops->set_key &&
997                     (keyconf = ieee80211_key_data2conf(local, sta->key)))
998                         local->ops->set_key(local_to_hw(local), DISABLE_KEY,
999                                            sta->addr, keyconf, sta->aid);
1000                 kfree(keyconf);
1001                 ieee80211_key_free(sta->key);
1002                 sta->key = NULL;
1003         }
1004         spin_unlock_bh(&local->sta_lock);
1005
1006         memset(&key, 0, sizeof(key));
1007         if (local->ops->set_key &&
1008                     local->ops->set_key(local_to_hw(local), REMOVE_ALL_KEYS,
1009                                        NULL, &key, 0))
1010                 printk(KERN_DEBUG "%s: failed to remove hwaccel keys\n",
1011                        dev->name);
1012
1013         return 0;
1014 }
1015
1016
1017 static int
1018 ieee80211_ioctl_force_unicast_rate(struct net_device *dev,
1019                                    struct ieee80211_sub_if_data *sdata,
1020                                    int rate)
1021 {
1022         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1023         struct ieee80211_hw_mode *mode;
1024         int i;
1025
1026         if (sdata->type != IEEE80211_IF_TYPE_AP)
1027                 return -ENOENT;
1028
1029         if (rate == 0) {
1030                 sdata->u.ap.force_unicast_rateidx = -1;
1031                 return 0;
1032         }
1033
1034         mode = local->oper_hw_mode;
1035         for (i = 0; i < mode->num_rates; i++) {
1036                 if (mode->rates[i].rate == rate) {
1037                         sdata->u.ap.force_unicast_rateidx = i;
1038                         return 0;
1039                 }
1040         }
1041         return -EINVAL;
1042 }
1043
1044
1045 static int
1046 ieee80211_ioctl_max_ratectrl_rate(struct net_device *dev,
1047                                   struct ieee80211_sub_if_data *sdata,
1048                                   int rate)
1049 {
1050         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1051         struct ieee80211_hw_mode *mode;
1052         int i;
1053
1054         if (sdata->type != IEEE80211_IF_TYPE_AP)
1055                 return -ENOENT;
1056
1057         if (rate == 0) {
1058                 sdata->u.ap.max_ratectrl_rateidx = -1;
1059                 return 0;
1060         }
1061
1062         mode = local->oper_hw_mode;
1063         for (i = 0; i < mode->num_rates; i++) {
1064                 if (mode->rates[i].rate == rate) {
1065                         sdata->u.ap.max_ratectrl_rateidx = i;
1066                         return 0;
1067                 }
1068         }
1069         return -EINVAL;
1070 }
1071
1072
1073 static void ieee80211_key_enable_hwaccel(struct ieee80211_local *local,
1074                                          struct ieee80211_key *key)
1075 {
1076         struct ieee80211_key_conf *keyconf;
1077         u8 addr[ETH_ALEN];
1078
1079         if (!key || key->alg != ALG_WEP || !key->force_sw_encrypt ||
1080             (local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP))
1081                 return;
1082
1083         memset(addr, 0xff, ETH_ALEN);
1084         keyconf = ieee80211_key_data2conf(local, key);
1085         if (keyconf && local->ops->set_key &&
1086             local->ops->set_key(local_to_hw(local),
1087                                SET_KEY, addr, keyconf, 0) == 0) {
1088                 key->force_sw_encrypt =
1089                         !!(keyconf->flags & IEEE80211_KEY_FORCE_SW_ENCRYPT);
1090                 key->hw_key_idx = keyconf->hw_key_idx;
1091         }
1092         kfree(keyconf);
1093 }
1094
1095
1096 static void ieee80211_key_disable_hwaccel(struct ieee80211_local *local,
1097                                           struct ieee80211_key *key)
1098 {
1099         struct ieee80211_key_conf *keyconf;
1100         u8 addr[ETH_ALEN];
1101
1102         if (!key || key->alg != ALG_WEP || key->force_sw_encrypt ||
1103             (local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP))
1104                 return;
1105
1106         memset(addr, 0xff, ETH_ALEN);
1107         keyconf = ieee80211_key_data2conf(local, key);
1108         if (keyconf && local->ops->set_key)
1109                 local->ops->set_key(local_to_hw(local), DISABLE_KEY,
1110                                    addr, keyconf, 0);
1111         kfree(keyconf);
1112         key->force_sw_encrypt = 1;
1113 }
1114
1115
1116 static int ieee80211_ioctl_default_wep_only(struct ieee80211_local *local,
1117                                             int value)
1118 {
1119         int i;
1120         struct ieee80211_sub_if_data *sdata;
1121
1122         local->default_wep_only = value;
1123         read_lock(&local->sub_if_lock);
1124         list_for_each_entry(sdata, &local->sub_if_list, list)
1125                 for (i = 0; i < NUM_DEFAULT_KEYS; i++)
1126                         if (value)
1127                                 ieee80211_key_enable_hwaccel(local,
1128                                                              sdata->keys[i]);
1129                         else
1130                                 ieee80211_key_disable_hwaccel(local,
1131                                                               sdata->keys[i]);
1132         read_unlock(&local->sub_if_lock);
1133
1134         return 0;
1135 }
1136
1137
1138 void ieee80211_update_default_wep_only(struct ieee80211_local *local)
1139 {
1140         int i = 0;
1141         struct ieee80211_sub_if_data *sdata;
1142
1143         read_lock(&local->sub_if_lock);
1144         list_for_each_entry(sdata, &local->sub_if_list, list) {
1145
1146                 if (sdata->dev == local->mdev)
1147                         continue;
1148
1149                 /* If there is an AP interface then depend on userspace to
1150                    set default_wep_only correctly. */
1151                 if (sdata->type == IEEE80211_IF_TYPE_AP) {
1152                         read_unlock(&local->sub_if_lock);
1153                         return;
1154                 }
1155
1156                 i++;
1157         }
1158
1159         read_unlock(&local->sub_if_lock);
1160
1161         if (i <= 1)
1162                 ieee80211_ioctl_default_wep_only(local, 1);
1163         else
1164                 ieee80211_ioctl_default_wep_only(local, 0);
1165 }
1166
1167
1168 static int ieee80211_ioctl_prism2_param(struct net_device *dev,
1169                                         struct iw_request_info *info,
1170                                         void *wrqu, char *extra)
1171 {
1172         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1173         struct ieee80211_sub_if_data *sdata;
1174         int *i = (int *) extra;
1175         int param = *i;
1176         int value = *(i + 1);
1177         int ret = 0;
1178
1179         if (!capable(CAP_NET_ADMIN))
1180                 return -EPERM;
1181
1182         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1183
1184         switch (param) {
1185         case PRISM2_PARAM_IEEE_802_1X:
1186                 if (local->ops->set_ieee8021x)
1187                         ret = local->ops->set_ieee8021x(local_to_hw(local),
1188                                                         value);
1189                 if (ret)
1190                         printk(KERN_DEBUG "%s: failed to set IEEE 802.1X (%d) "
1191                                "for low-level driver\n", dev->name, value);
1192                 else
1193                         sdata->ieee802_1x = value;
1194                 break;
1195
1196         case PRISM2_PARAM_ANTSEL_TX:
1197                 local->hw.conf.antenna_sel_tx = value;
1198                 if (ieee80211_hw_config(local))
1199                         ret = -EINVAL;
1200                 break;
1201
1202         case PRISM2_PARAM_ANTSEL_RX:
1203                 local->hw.conf.antenna_sel_rx = value;
1204                 if (ieee80211_hw_config(local))
1205                         ret = -EINVAL;
1206                 break;
1207
1208         case PRISM2_PARAM_CTS_PROTECT_ERP_FRAMES:
1209                 local->cts_protect_erp_frames = value;
1210                 break;
1211
1212         case PRISM2_PARAM_DROP_UNENCRYPTED:
1213                 sdata->drop_unencrypted = value;
1214                 break;
1215
1216         case PRISM2_PARAM_PREAMBLE:
1217                 local->short_preamble = value;
1218                 break;
1219
1220         case PRISM2_PARAM_STAT_TIME:
1221                 if (!local->stat_time && value) {
1222                         local->stat_timer.expires = jiffies + HZ * value / 100;
1223                         add_timer(&local->stat_timer);
1224                 } else if (local->stat_time && !value) {
1225                         del_timer_sync(&local->stat_timer);
1226                 }
1227                 local->stat_time = value;
1228                 break;
1229         case PRISM2_PARAM_SHORT_SLOT_TIME:
1230                 if (value)
1231                         local->hw.conf.flags |= IEEE80211_CONF_SHORT_SLOT_TIME;
1232                 else
1233                         local->hw.conf.flags &= ~IEEE80211_CONF_SHORT_SLOT_TIME;
1234                 if (ieee80211_hw_config(local))
1235                         ret = -EINVAL;
1236                 break;
1237
1238         case PRISM2_PARAM_NEXT_MODE:
1239                 local->next_mode = value;
1240                 break;
1241
1242         case PRISM2_PARAM_CLEAR_KEYS:
1243                 ret = ieee80211_ioctl_clear_keys(dev);
1244                 break;
1245
1246         case PRISM2_PARAM_RADIO_ENABLED:
1247                 ret = ieee80211_ioctl_set_radio_enabled(dev, value);
1248                 break;
1249
1250         case PRISM2_PARAM_ANTENNA_MODE:
1251                 local->hw.conf.antenna_mode = value;
1252                 if (ieee80211_hw_config(local))
1253                         ret = -EINVAL;
1254                 break;
1255
1256         case PRISM2_PARAM_STA_ANTENNA_SEL:
1257                 local->sta_antenna_sel = value;
1258                 break;
1259
1260         case PRISM2_PARAM_FORCE_UNICAST_RATE:
1261                 ret = ieee80211_ioctl_force_unicast_rate(dev, sdata, value);
1262                 break;
1263
1264         case PRISM2_PARAM_MAX_RATECTRL_RATE:
1265                 ret = ieee80211_ioctl_max_ratectrl_rate(dev, sdata, value);
1266                 break;
1267
1268         case PRISM2_PARAM_RATE_CTRL_NUM_UP:
1269                 local->rate_ctrl_num_up = value;
1270                 break;
1271
1272         case PRISM2_PARAM_RATE_CTRL_NUM_DOWN:
1273                 local->rate_ctrl_num_down = value;
1274                 break;
1275
1276         case PRISM2_PARAM_TX_POWER_REDUCTION:
1277                 if (value < 0)
1278                         ret = -EINVAL;
1279                 else
1280                         local->hw.conf.tx_power_reduction = value;
1281                 break;
1282
1283         case PRISM2_PARAM_KEY_TX_RX_THRESHOLD:
1284                 local->key_tx_rx_threshold = value;
1285                 break;
1286
1287         case PRISM2_PARAM_DEFAULT_WEP_ONLY:
1288                 ret = ieee80211_ioctl_default_wep_only(local, value);
1289                 break;
1290
1291         case PRISM2_PARAM_WIFI_WME_NOACK_TEST:
1292                 local->wifi_wme_noack_test = value;
1293                 break;
1294
1295         case PRISM2_PARAM_SCAN_FLAGS:
1296                 local->scan_flags = value;
1297                 break;
1298
1299         case PRISM2_PARAM_MIXED_CELL:
1300                 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1301                     sdata->type != IEEE80211_IF_TYPE_IBSS)
1302                         ret = -EINVAL;
1303                 else
1304                         sdata->u.sta.mixed_cell = !!value;
1305                 break;
1306
1307         case PRISM2_PARAM_HW_MODES:
1308                 local->enabled_modes = value;
1309                 break;
1310
1311         case PRISM2_PARAM_CREATE_IBSS:
1312                 if (sdata->type != IEEE80211_IF_TYPE_IBSS)
1313                         ret = -EINVAL;
1314                 else
1315                         sdata->u.sta.create_ibss = !!value;
1316                 break;
1317         case PRISM2_PARAM_WMM_ENABLED:
1318                 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1319                     sdata->type != IEEE80211_IF_TYPE_IBSS)
1320                         ret = -EINVAL;
1321                 else
1322                         sdata->u.sta.wmm_enabled = !!value;
1323                 break;
1324         case PRISM2_PARAM_RADAR_DETECT:
1325                 local->hw.conf.radar_detect = value;
1326                 break;
1327         case PRISM2_PARAM_SPECTRUM_MGMT:
1328                 local->hw.conf.spect_mgmt = value;
1329                 break;
1330         default:
1331                 ret = -EOPNOTSUPP;
1332                 break;
1333         }
1334
1335         return ret;
1336 }
1337
1338
1339 static int ieee80211_ioctl_get_prism2_param(struct net_device *dev,
1340                                             struct iw_request_info *info,
1341                                             void *wrqu, char *extra)
1342 {
1343         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1344         struct ieee80211_sub_if_data *sdata;
1345         int *param = (int *) extra;
1346         int ret = 0;
1347
1348         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1349
1350         switch (*param) {
1351         case PRISM2_PARAM_IEEE_802_1X:
1352                 *param = sdata->ieee802_1x;
1353                 break;
1354
1355         case PRISM2_PARAM_ANTSEL_TX:
1356                 *param = local->hw.conf.antenna_sel_tx;
1357                 break;
1358
1359         case PRISM2_PARAM_ANTSEL_RX:
1360                 *param = local->hw.conf.antenna_sel_rx;
1361                 break;
1362
1363         case PRISM2_PARAM_CTS_PROTECT_ERP_FRAMES:
1364                 *param = local->cts_protect_erp_frames;
1365                 break;
1366
1367         case PRISM2_PARAM_DROP_UNENCRYPTED:
1368                 *param = sdata->drop_unencrypted;
1369                 break;
1370
1371         case PRISM2_PARAM_PREAMBLE:
1372                 *param = local->short_preamble;
1373                 break;
1374
1375         case PRISM2_PARAM_STAT_TIME:
1376                 *param = local->stat_time;
1377                 break;
1378         case PRISM2_PARAM_SHORT_SLOT_TIME:
1379                 *param = !!(local->hw.conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME);
1380                 break;
1381
1382         case PRISM2_PARAM_NEXT_MODE:
1383                 *param = local->next_mode;
1384                 break;
1385
1386         case PRISM2_PARAM_ANTENNA_MODE:
1387                 *param = local->hw.conf.antenna_mode;
1388                 break;
1389
1390         case PRISM2_PARAM_STA_ANTENNA_SEL:
1391                 *param = local->sta_antenna_sel;
1392                 break;
1393
1394         case PRISM2_PARAM_RATE_CTRL_NUM_UP:
1395                 *param = local->rate_ctrl_num_up;
1396                 break;
1397
1398         case PRISM2_PARAM_RATE_CTRL_NUM_DOWN:
1399                 *param = local->rate_ctrl_num_down;
1400                 break;
1401
1402         case PRISM2_PARAM_TX_POWER_REDUCTION:
1403                 *param = local->hw.conf.tx_power_reduction;
1404                 break;
1405
1406         case PRISM2_PARAM_KEY_TX_RX_THRESHOLD:
1407                 *param = local->key_tx_rx_threshold;
1408                 break;
1409
1410         case PRISM2_PARAM_DEFAULT_WEP_ONLY:
1411                 *param = local->default_wep_only;
1412                 break;
1413
1414         case PRISM2_PARAM_WIFI_WME_NOACK_TEST:
1415                 *param = local->wifi_wme_noack_test;
1416                 break;
1417
1418         case PRISM2_PARAM_SCAN_FLAGS:
1419                 *param = local->scan_flags;
1420                 break;
1421
1422         case PRISM2_PARAM_HW_MODES:
1423                 *param = local->enabled_modes;
1424                 break;
1425
1426         case PRISM2_PARAM_CREATE_IBSS:
1427                 if (sdata->type != IEEE80211_IF_TYPE_IBSS)
1428                         ret = -EINVAL;
1429                 else
1430                         *param = !!sdata->u.sta.create_ibss;
1431                 break;
1432
1433         case PRISM2_PARAM_MIXED_CELL:
1434                 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1435                     sdata->type != IEEE80211_IF_TYPE_IBSS)
1436                         ret = -EINVAL;
1437                 else
1438                         *param = !!sdata->u.sta.mixed_cell;
1439                 break;
1440         case PRISM2_PARAM_WMM_ENABLED:
1441                 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1442                     sdata->type != IEEE80211_IF_TYPE_IBSS)
1443                         ret = -EINVAL;
1444                 else
1445                         *param = !!sdata->u.sta.wmm_enabled;
1446                 break;
1447         default:
1448                 ret = -EOPNOTSUPP;
1449                 break;
1450         }
1451
1452         return ret;
1453 }
1454
1455 static int ieee80211_ioctl_siwmlme(struct net_device *dev,
1456                                    struct iw_request_info *info,
1457                                    struct iw_point *data, char *extra)
1458 {
1459         struct ieee80211_sub_if_data *sdata;
1460         struct iw_mlme *mlme = (struct iw_mlme *) extra;
1461
1462         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1463         if (sdata->type != IEEE80211_IF_TYPE_STA &&
1464             sdata->type != IEEE80211_IF_TYPE_IBSS)
1465                 return -EINVAL;
1466
1467         switch (mlme->cmd) {
1468         case IW_MLME_DEAUTH:
1469                 /* TODO: mlme->addr.sa_data */
1470                 return ieee80211_sta_deauthenticate(dev, mlme->reason_code);
1471         case IW_MLME_DISASSOC:
1472                 /* TODO: mlme->addr.sa_data */
1473                 return ieee80211_sta_disassociate(dev, mlme->reason_code);
1474         default:
1475                 return -EOPNOTSUPP;
1476         }
1477 }
1478
1479
1480 static int ieee80211_ioctl_siwencode(struct net_device *dev,
1481                                      struct iw_request_info *info,
1482                                      struct iw_point *erq, char *keybuf)
1483 {
1484         struct ieee80211_sub_if_data *sdata;
1485         int idx, i, alg = ALG_WEP;
1486         u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1487
1488         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1489
1490         idx = erq->flags & IW_ENCODE_INDEX;
1491         if (idx == 0) {
1492                 if (sdata->default_key)
1493                         for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1494                                 if (sdata->default_key == sdata->keys[i]) {
1495                                         idx = i;
1496                                         break;
1497                                 }
1498                         }
1499         } else if (idx < 1 || idx > 4)
1500                 return -EINVAL;
1501         else
1502                 idx--;
1503
1504         if (erq->flags & IW_ENCODE_DISABLED)
1505                 alg = ALG_NONE;
1506         else if (erq->length == 0) {
1507                 /* No key data - just set the default TX key index */
1508                 if (sdata->default_key != sdata->keys[idx])
1509                         sdata->default_key = sdata->keys[idx];
1510                 return 0;
1511         }
1512
1513         return ieee80211_set_encryption(
1514                 dev, bcaddr,
1515                 idx, alg,
1516                 !sdata->default_key,
1517                 keybuf, erq->length);
1518 }
1519
1520
1521 static int ieee80211_ioctl_giwencode(struct net_device *dev,
1522                                      struct iw_request_info *info,
1523                                      struct iw_point *erq, char *key)
1524 {
1525         struct ieee80211_sub_if_data *sdata;
1526         int idx, i;
1527
1528         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1529
1530         idx = erq->flags & IW_ENCODE_INDEX;
1531         if (idx < 1 || idx > 4) {
1532                 idx = -1;
1533                 if (!sdata->default_key)
1534                         idx = 0;
1535                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1536                         if (sdata->default_key == sdata->keys[i]) {
1537                                 idx = i;
1538                                 break;
1539                         }
1540                 }
1541                 if (idx < 0)
1542                         return -EINVAL;
1543         } else
1544                 idx--;
1545
1546         erq->flags = idx + 1;
1547
1548         if (!sdata->keys[idx]) {
1549                 erq->length = 0;
1550                 erq->flags |= IW_ENCODE_DISABLED;
1551                 return 0;
1552         }
1553
1554         memcpy(key, sdata->keys[idx]->key,
1555                min((int)erq->length, sdata->keys[idx]->keylen));
1556         erq->length = sdata->keys[idx]->keylen;
1557         erq->flags |= IW_ENCODE_ENABLED;
1558
1559         return 0;
1560 }
1561
1562 static int ieee80211_ioctl_siwauth(struct net_device *dev,
1563                                    struct iw_request_info *info,
1564                                    struct iw_param *data, char *extra)
1565 {
1566         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1567         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1568         int ret = 0;
1569
1570         switch (data->flags & IW_AUTH_INDEX) {
1571         case IW_AUTH_WPA_VERSION:
1572         case IW_AUTH_CIPHER_PAIRWISE:
1573         case IW_AUTH_CIPHER_GROUP:
1574         case IW_AUTH_WPA_ENABLED:
1575         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1576                 break;
1577         case IW_AUTH_KEY_MGMT:
1578                 if (sdata->type != IEEE80211_IF_TYPE_STA)
1579                         ret = -EINVAL;
1580                 else {
1581                         /*
1582                          * TODO: sdata->u.sta.key_mgmt does not match with WE18
1583                          * value completely; could consider modifying this to
1584                          * be closer to WE18. For now, this value is not really
1585                          * used for anything else than Privacy matching, so the
1586                          * current code here should be more or less OK.
1587                          */
1588                         if (data->value & IW_AUTH_KEY_MGMT_802_1X) {
1589                                 sdata->u.sta.key_mgmt =
1590                                         IEEE80211_KEY_MGMT_WPA_EAP;
1591                         } else if (data->value & IW_AUTH_KEY_MGMT_PSK) {
1592                                 sdata->u.sta.key_mgmt =
1593                                         IEEE80211_KEY_MGMT_WPA_PSK;
1594                         } else {
1595                                 sdata->u.sta.key_mgmt =
1596                                         IEEE80211_KEY_MGMT_NONE;
1597                         }
1598                 }
1599                 break;
1600         case IW_AUTH_80211_AUTH_ALG:
1601                 if (sdata->type == IEEE80211_IF_TYPE_STA ||
1602                     sdata->type == IEEE80211_IF_TYPE_IBSS)
1603                         sdata->u.sta.auth_algs = data->value;
1604                 else
1605                         ret = -EOPNOTSUPP;
1606                 break;
1607         case IW_AUTH_PRIVACY_INVOKED:
1608                 if (local->ops->set_privacy_invoked)
1609                         ret = local->ops->set_privacy_invoked(
1610                                         local_to_hw(local), data->value);
1611                 break;
1612         default:
1613                 ret = -EOPNOTSUPP;
1614                 break;
1615         }
1616         return ret;
1617 }
1618
1619 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
1620 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
1621 {
1622         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1623         struct iw_statistics *wstats = &local->wstats;
1624         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1625         struct sta_info *sta = NULL;
1626
1627         if (sdata->type == IEEE80211_IF_TYPE_STA ||
1628             sdata->type == IEEE80211_IF_TYPE_IBSS)
1629                 sta = sta_info_get(local, sdata->u.sta.bssid);
1630         if (!sta) {
1631                 wstats->discard.fragment = 0;
1632                 wstats->discard.misc = 0;
1633                 wstats->qual.qual = 0;
1634                 wstats->qual.level = 0;
1635                 wstats->qual.noise = 0;
1636                 wstats->qual.updated = IW_QUAL_ALL_INVALID;
1637         } else {
1638                 wstats->qual.level = sta->last_rssi;
1639                 wstats->qual.qual = sta->last_signal;
1640                 wstats->qual.noise = sta->last_noise;
1641                 wstats->qual.updated = local->wstats_flags;
1642                 sta_info_put(sta);
1643         }
1644         return wstats;
1645 }
1646
1647 static int ieee80211_ioctl_giwauth(struct net_device *dev,
1648                                    struct iw_request_info *info,
1649                                    struct iw_param *data, char *extra)
1650 {
1651         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1652         int ret = 0;
1653
1654         switch (data->flags & IW_AUTH_INDEX) {
1655         case IW_AUTH_80211_AUTH_ALG:
1656                 if (sdata->type == IEEE80211_IF_TYPE_STA ||
1657                     sdata->type == IEEE80211_IF_TYPE_IBSS)
1658                         data->value = sdata->u.sta.auth_algs;
1659                 else
1660                         ret = -EOPNOTSUPP;
1661                 break;
1662         default:
1663                 ret = -EOPNOTSUPP;
1664                 break;
1665         }
1666         return ret;
1667 }
1668
1669
1670 static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
1671                                         struct iw_request_info *info,
1672                                         struct iw_point *erq, char *extra)
1673 {
1674         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1675         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
1676         int alg, idx, i;
1677
1678         switch (ext->alg) {
1679         case IW_ENCODE_ALG_NONE:
1680                 alg = ALG_NONE;
1681                 break;
1682         case IW_ENCODE_ALG_WEP:
1683                 alg = ALG_WEP;
1684                 break;
1685         case IW_ENCODE_ALG_TKIP:
1686                 alg = ALG_TKIP;
1687                 break;
1688         case IW_ENCODE_ALG_CCMP:
1689                 alg = ALG_CCMP;
1690                 break;
1691         default:
1692                 return -EOPNOTSUPP;
1693         }
1694
1695         if (erq->flags & IW_ENCODE_DISABLED)
1696                 alg = ALG_NONE;
1697
1698         idx = erq->flags & IW_ENCODE_INDEX;
1699         if (idx < 1 || idx > 4) {
1700                 idx = -1;
1701                 if (!sdata->default_key)
1702                         idx = 0;
1703                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1704                         if (sdata->default_key == sdata->keys[i]) {
1705                                 idx = i;
1706                                 break;
1707                         }
1708                 }
1709                 if (idx < 0)
1710                         return -EINVAL;
1711         } else
1712                 idx--;
1713
1714         return ieee80211_set_encryption(dev, ext->addr.sa_data, idx, alg,
1715                                         ext->ext_flags &
1716                                         IW_ENCODE_EXT_SET_TX_KEY,
1717                                         ext->key, ext->key_len);
1718 }
1719
1720
1721 static const struct iw_priv_args ieee80211_ioctl_priv[] = {
1722         { PRISM2_IOCTL_PRISM2_PARAM,
1723           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "param" },
1724         { PRISM2_IOCTL_GET_PRISM2_PARAM,
1725           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1726           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "get_param" },
1727 };
1728
1729 /* Structures to export the Wireless Handlers */
1730
1731 static const iw_handler ieee80211_handler[] =
1732 {
1733         (iw_handler) NULL,                              /* SIOCSIWCOMMIT */
1734         (iw_handler) ieee80211_ioctl_giwname,           /* SIOCGIWNAME */
1735         (iw_handler) NULL,                              /* SIOCSIWNWID */
1736         (iw_handler) NULL,                              /* SIOCGIWNWID */
1737         (iw_handler) ieee80211_ioctl_siwfreq,           /* SIOCSIWFREQ */
1738         (iw_handler) ieee80211_ioctl_giwfreq,           /* SIOCGIWFREQ */
1739         (iw_handler) ieee80211_ioctl_siwmode,           /* SIOCSIWMODE */
1740         (iw_handler) ieee80211_ioctl_giwmode,           /* SIOCGIWMODE */
1741         (iw_handler) NULL,                              /* SIOCSIWSENS */
1742         (iw_handler) NULL,                              /* SIOCGIWSENS */
1743         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE */
1744         (iw_handler) ieee80211_ioctl_giwrange,          /* SIOCGIWRANGE */
1745         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV */
1746         (iw_handler) NULL /* kernel code */,            /* SIOCGIWPRIV */
1747         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS */
1748         (iw_handler) NULL /* kernel code */,            /* SIOCGIWSTATS */
1749         iw_handler_set_spy,                             /* SIOCSIWSPY */
1750         iw_handler_get_spy,                             /* SIOCGIWSPY */
1751         iw_handler_set_thrspy,                          /* SIOCSIWTHRSPY */
1752         iw_handler_get_thrspy,                          /* SIOCGIWTHRSPY */
1753         (iw_handler) ieee80211_ioctl_siwap,             /* SIOCSIWAP */
1754         (iw_handler) ieee80211_ioctl_giwap,             /* SIOCGIWAP */
1755         (iw_handler) ieee80211_ioctl_siwmlme,           /* SIOCSIWMLME */
1756         (iw_handler) NULL,                              /* SIOCGIWAPLIST */
1757         (iw_handler) ieee80211_ioctl_siwscan,           /* SIOCSIWSCAN */
1758         (iw_handler) ieee80211_ioctl_giwscan,           /* SIOCGIWSCAN */
1759         (iw_handler) ieee80211_ioctl_siwessid,          /* SIOCSIWESSID */
1760         (iw_handler) ieee80211_ioctl_giwessid,          /* SIOCGIWESSID */
1761         (iw_handler) NULL,                              /* SIOCSIWNICKN */
1762         (iw_handler) NULL,                              /* SIOCGIWNICKN */
1763         (iw_handler) NULL,                              /* -- hole -- */
1764         (iw_handler) NULL,                              /* -- hole -- */
1765         (iw_handler) NULL,                              /* SIOCSIWRATE */
1766         (iw_handler) NULL,                              /* SIOCGIWRATE */
1767         (iw_handler) ieee80211_ioctl_siwrts,            /* SIOCSIWRTS */
1768         (iw_handler) ieee80211_ioctl_giwrts,            /* SIOCGIWRTS */
1769         (iw_handler) ieee80211_ioctl_siwfrag,           /* SIOCSIWFRAG */
1770         (iw_handler) ieee80211_ioctl_giwfrag,           /* SIOCGIWFRAG */
1771         (iw_handler) NULL,                              /* SIOCSIWTXPOW */
1772         (iw_handler) NULL,                              /* SIOCGIWTXPOW */
1773         (iw_handler) ieee80211_ioctl_siwretry,          /* SIOCSIWRETRY */
1774         (iw_handler) ieee80211_ioctl_giwretry,          /* SIOCGIWRETRY */
1775         (iw_handler) ieee80211_ioctl_siwencode,         /* SIOCSIWENCODE */
1776         (iw_handler) ieee80211_ioctl_giwencode,         /* SIOCGIWENCODE */
1777         (iw_handler) NULL,                              /* SIOCSIWPOWER */
1778         (iw_handler) NULL,                              /* SIOCGIWPOWER */
1779         (iw_handler) NULL,                              /* -- hole -- */
1780         (iw_handler) NULL,                              /* -- hole -- */
1781         (iw_handler) ieee80211_ioctl_siwgenie,          /* SIOCSIWGENIE */
1782         (iw_handler) NULL,                              /* SIOCGIWGENIE */
1783         (iw_handler) ieee80211_ioctl_siwauth,           /* SIOCSIWAUTH */
1784         (iw_handler) ieee80211_ioctl_giwauth,           /* SIOCGIWAUTH */
1785         (iw_handler) ieee80211_ioctl_siwencodeext,      /* SIOCSIWENCODEEXT */
1786         (iw_handler) NULL,                              /* SIOCGIWENCODEEXT */
1787         (iw_handler) NULL,                              /* SIOCSIWPMKSA */
1788         (iw_handler) NULL,                              /* -- hole -- */
1789 };
1790
1791 static const iw_handler ieee80211_private_handler[] =
1792 {                                                       /* SIOCIWFIRSTPRIV + */
1793         (iw_handler) ieee80211_ioctl_prism2_param,      /* 0 */
1794         (iw_handler) ieee80211_ioctl_get_prism2_param,  /* 1 */
1795 };
1796
1797 const struct iw_handler_def ieee80211_iw_handler_def =
1798 {
1799         .num_standard   = ARRAY_SIZE(ieee80211_handler),
1800         .num_private    = ARRAY_SIZE(ieee80211_private_handler),
1801         .num_private_args = ARRAY_SIZE(ieee80211_ioctl_priv),
1802         .standard       = (iw_handler *) ieee80211_handler,
1803         .private        = (iw_handler *) ieee80211_private_handler,
1804         .private_args   = (struct iw_priv_args *) ieee80211_ioctl_priv,
1805         .get_wireless_stats = ieee80211_get_wireless_stats,
1806 };