iwlwifi: add bad length check for WEP keys
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-sta.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29
30 #include <net/mac80211.h>
31 #include <linux/etherdevice.h>
32
33 #include "iwl-eeprom.h"
34 #include "iwl-dev.h"
35 #include "iwl-core.h"
36 #include "iwl-sta.h"
37 #include "iwl-io.h"
38 #include "iwl-helpers.h"
39
40
41 #define IWL_STA_DRIVER_ACTIVE           0x1     /* ucode entry is active */
42 #define IWL_STA_UCODE_ACTIVE            0x2     /* ucode entry is active */
43
44 u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
45 {
46         int i;
47         int start = 0;
48         int ret = IWL_INVALID_STATION;
49         unsigned long flags;
50         DECLARE_MAC_BUF(mac);
51
52         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
53             (priv->iw_mode == IEEE80211_IF_TYPE_AP))
54                 start = IWL_STA_ID;
55
56         if (is_broadcast_ether_addr(addr))
57                 return priv->hw_params.bcast_sta_id;
58
59         spin_lock_irqsave(&priv->sta_lock, flags);
60         for (i = start; i < priv->hw_params.max_stations; i++)
61                 if (priv->stations[i].used &&
62                     (!compare_ether_addr(priv->stations[i].sta.sta.addr,
63                                          addr))) {
64                         ret = i;
65                         goto out;
66                 }
67
68         IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
69                               print_mac(mac, addr), priv->num_stations);
70
71  out:
72         spin_unlock_irqrestore(&priv->sta_lock, flags);
73         return ret;
74 }
75 EXPORT_SYMBOL(iwl_find_station);
76
77 static int iwl_add_sta_callback(struct iwl_priv *priv,
78                                    struct iwl_cmd *cmd, struct sk_buff *skb)
79 {
80         struct iwl_rx_packet *res = NULL;
81
82         if (!skb) {
83                 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
84                 return 1;
85         }
86
87         res = (struct iwl_rx_packet *)skb->data;
88         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
89                 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
90                           res->hdr.flags);
91                 return 1;
92         }
93
94         switch (res->u.add_sta.status) {
95         case ADD_STA_SUCCESS_MSK:
96                 /* FIXME: implement iwl_sta_ucode_activate(priv, addr); */
97                 /* fail through */
98         default:
99                 IWL_DEBUG_HC("Received REPLY_ADD_STA:(0x%08X)\n",
100                              res->u.add_sta.status);
101                 break;
102         }
103
104         /* We didn't cache the SKB; let the caller free it */
105         return 1;
106 }
107
108
109
110 int iwl_send_add_sta(struct iwl_priv *priv,
111                      struct iwl_addsta_cmd *sta, u8 flags)
112 {
113         struct iwl_rx_packet *res = NULL;
114         int ret = 0;
115         u8 data[sizeof(*sta)];
116         struct iwl_host_cmd cmd = {
117                 .id = REPLY_ADD_STA,
118                 .meta.flags = flags,
119                 .data = data,
120         };
121
122         if (flags & CMD_ASYNC)
123                 cmd.meta.u.callback = iwl_add_sta_callback;
124         else
125                 cmd.meta.flags |= CMD_WANT_SKB;
126
127         cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
128         ret = iwl_send_cmd(priv, &cmd);
129
130         if (ret || (flags & CMD_ASYNC))
131                 return ret;
132
133         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
134         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
135                 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
136                           res->hdr.flags);
137                 ret = -EIO;
138         }
139
140         if (ret == 0) {
141                 switch (res->u.add_sta.status) {
142                 case ADD_STA_SUCCESS_MSK:
143                         IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
144                         break;
145                 default:
146                         ret = -EIO;
147                         IWL_WARNING("REPLY_ADD_STA failed\n");
148                         break;
149                 }
150         }
151
152         priv->alloc_rxb_skb--;
153         dev_kfree_skb_any(cmd.meta.u.skb);
154
155         return ret;
156 }
157 EXPORT_SYMBOL(iwl_send_add_sta);
158
159 static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
160                                    struct ieee80211_ht_info *sta_ht_inf)
161 {
162         __le32 sta_flags;
163         u8 mimo_ps_mode;
164
165         if (!sta_ht_inf || !sta_ht_inf->ht_supported)
166                 goto done;
167
168         mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
169
170         sta_flags = priv->stations[index].sta.station_flags;
171
172         sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
173
174         switch (mimo_ps_mode) {
175         case WLAN_HT_CAP_MIMO_PS_STATIC:
176                 sta_flags |= STA_FLG_MIMO_DIS_MSK;
177                 break;
178         case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
179                 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
180                 break;
181         case WLAN_HT_CAP_MIMO_PS_DISABLED:
182                 break;
183         default:
184                 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
185                 break;
186         }
187
188         sta_flags |= cpu_to_le32(
189               (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
190
191         sta_flags |= cpu_to_le32(
192               (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
193
194         if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
195                 sta_flags |= STA_FLG_FAT_EN_MSK;
196         else
197                 sta_flags &= ~STA_FLG_FAT_EN_MSK;
198
199         priv->stations[index].sta.station_flags = sta_flags;
200  done:
201         return;
202 }
203
204 /**
205  * iwl_add_station_flags - Add station to tables in driver and device
206  */
207 u8 iwl_add_station_flags(struct iwl_priv *priv, const u8 *addr, int is_ap,
208                          u8 flags, struct ieee80211_ht_info *ht_info)
209 {
210         int i;
211         int index = IWL_INVALID_STATION;
212         struct iwl_station_entry *station;
213         unsigned long flags_spin;
214         DECLARE_MAC_BUF(mac);
215
216         spin_lock_irqsave(&priv->sta_lock, flags_spin);
217         if (is_ap)
218                 index = IWL_AP_ID;
219         else if (is_broadcast_ether_addr(addr))
220                 index = priv->hw_params.bcast_sta_id;
221         else
222                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
223                         if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
224                                                 addr)) {
225                                 index = i;
226                                 break;
227                         }
228
229                         if (!priv->stations[i].used &&
230                             index == IWL_INVALID_STATION)
231                                 index = i;
232                 }
233
234
235         /* These two conditions have the same outcome, but keep them separate
236           since they have different meanings */
237         if (unlikely(index == IWL_INVALID_STATION)) {
238                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
239                 return index;
240         }
241
242         if (priv->stations[index].used &&
243             !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
244                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
245                 return index;
246         }
247
248
249         IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
250         station = &priv->stations[index];
251         station->used = 1;
252         priv->num_stations++;
253
254         /* Set up the REPLY_ADD_STA command to send to device */
255         memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
256         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
257         station->sta.mode = 0;
258         station->sta.sta.sta_id = index;
259         station->sta.station_flags = 0;
260
261         /* BCAST station and IBSS stations do not work in HT mode */
262         if (index != priv->hw_params.bcast_sta_id &&
263             priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
264                 iwl_set_ht_add_station(priv, index, ht_info);
265
266         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
267
268         /* Add station to device's station table */
269         iwl_send_add_sta(priv, &station->sta, flags);
270         return index;
271
272 }
273 EXPORT_SYMBOL(iwl_add_station_flags);
274
275
276 static int iwl_sta_ucode_deactivate(struct iwl_priv *priv, const char *addr)
277 {
278         unsigned long flags;
279         u8 sta_id;
280         DECLARE_MAC_BUF(mac);
281
282         sta_id = iwl_find_station(priv, addr);
283         if (sta_id != IWL_INVALID_STATION) {
284                 IWL_DEBUG_ASSOC("Removed STA from Ucode: %s\n",
285                                 print_mac(mac, addr));
286                 spin_lock_irqsave(&priv->sta_lock, flags);
287                 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
288                 memset(&priv->stations[sta_id], 0,
289                         sizeof(struct iwl_station_entry));
290                 spin_unlock_irqrestore(&priv->sta_lock, flags);
291                 return 0;
292         }
293         return -EINVAL;
294 }
295
296 static int iwl_remove_sta_callback(struct iwl_priv *priv,
297                                    struct iwl_cmd *cmd, struct sk_buff *skb)
298 {
299         struct iwl_rx_packet *res = NULL;
300         const char *addr = cmd->cmd.rm_sta.addr;
301
302         if (!skb) {
303                 IWL_ERROR("Error: Response NULL in REPLY_REMOVE_STA.\n");
304                 return 1;
305         }
306
307         res = (struct iwl_rx_packet *)skb->data;
308         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
309                 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
310                 res->hdr.flags);
311                 return 1;
312         }
313
314         switch (res->u.rem_sta.status) {
315         case REM_STA_SUCCESS_MSK:
316                 iwl_sta_ucode_deactivate(priv, addr);
317                 break;
318         default:
319                 break;
320         }
321
322         /* We didn't cache the SKB; let the caller free it */
323         return 1;
324 }
325
326 static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
327                                    u8 flags)
328 {
329         struct iwl_rx_packet *res = NULL;
330         int ret;
331
332         struct iwl_rem_sta_cmd rm_sta_cmd;
333
334         struct iwl_host_cmd cmd = {
335                 .id = REPLY_REMOVE_STA,
336                 .len = sizeof(struct iwl_rem_sta_cmd),
337                 .meta.flags = flags,
338                 .data = &rm_sta_cmd,
339         };
340
341         memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
342         rm_sta_cmd.num_sta = 1;
343         memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
344
345         if (flags & CMD_ASYNC)
346                 cmd.meta.u.callback = iwl_remove_sta_callback;
347         else
348                 cmd.meta.flags |= CMD_WANT_SKB;
349         ret = iwl_send_cmd(priv, &cmd);
350
351         if (ret || (flags & CMD_ASYNC))
352                 return ret;
353
354         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
355         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
356                 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
357                           res->hdr.flags);
358                 ret = -EIO;
359         }
360
361         if (!ret) {
362                 switch (res->u.rem_sta.status) {
363                 case REM_STA_SUCCESS_MSK:
364                         iwl_sta_ucode_deactivate(priv, addr);
365                         IWL_DEBUG_ASSOC("REPLY_REMOVE_STA PASSED\n");
366                         break;
367                 default:
368                         ret = -EIO;
369                         IWL_ERROR("REPLY_REMOVE_STA failed\n");
370                         break;
371                 }
372         }
373
374         priv->alloc_rxb_skb--;
375         dev_kfree_skb_any(cmd.meta.u.skb);
376
377         return ret;
378 }
379 /**
380  * iwl_remove_station - Remove driver's knowledge of station.
381  *
382  */
383 u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
384 {
385         int index = IWL_INVALID_STATION;
386         int i;
387         unsigned long flags;
388
389         spin_lock_irqsave(&priv->sta_lock, flags);
390
391         if (is_ap)
392                 index = IWL_AP_ID;
393         else if (is_broadcast_ether_addr(addr))
394                 index = priv->hw_params.bcast_sta_id;
395         else
396                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
397                         if (priv->stations[i].used &&
398                             !compare_ether_addr(priv->stations[i].sta.sta.addr,
399                                                 addr)) {
400                                 index = i;
401                                 break;
402                         }
403
404         if (unlikely(index == IWL_INVALID_STATION))
405                 goto out;
406
407         if (priv->stations[index].used) {
408                 priv->stations[index].used = 0;
409                 priv->num_stations--;
410         }
411
412         BUG_ON(priv->num_stations < 0);
413         spin_unlock_irqrestore(&priv->sta_lock, flags);
414         iwl_send_remove_station(priv, addr, CMD_ASYNC);
415         return index;
416 out:
417         spin_unlock_irqrestore(&priv->sta_lock, flags);
418         return 0;
419 }
420 EXPORT_SYMBOL(iwl_remove_station);
421 int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
422 {
423         int i;
424
425         for (i = 0; i < STA_KEY_MAX_NUM; i++)
426                 if (!test_and_set_bit(i, &priv->ucode_key_table))
427                         return i;
428
429         return -1;
430 }
431
432 int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
433 {
434         int i, not_empty = 0;
435         u8 buff[sizeof(struct iwl_wep_cmd) +
436                 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
437         struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
438         size_t cmd_size  = sizeof(struct iwl_wep_cmd);
439         struct iwl_host_cmd cmd = {
440                 .id = REPLY_WEPKEY,
441                 .data = wep_cmd,
442                 .meta.flags = CMD_ASYNC,
443         };
444
445         memset(wep_cmd, 0, cmd_size +
446                         (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
447
448         for (i = 0; i < WEP_KEYS_MAX ; i++) {
449                 wep_cmd->key[i].key_index = i;
450                 if (priv->wep_keys[i].key_size) {
451                         wep_cmd->key[i].key_offset = i;
452                         not_empty = 1;
453                 } else {
454                         wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
455                 }
456
457                 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
458                 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
459                                 priv->wep_keys[i].key_size);
460         }
461
462         wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
463         wep_cmd->num_keys = WEP_KEYS_MAX;
464
465         cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
466
467         cmd.len = cmd_size;
468
469         if (not_empty || send_if_empty)
470                 return iwl_send_cmd(priv, &cmd);
471         else
472                 return 0;
473 }
474 EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
475
476 int iwl_remove_default_wep_key(struct iwl_priv *priv,
477                                struct ieee80211_key_conf *keyconf)
478 {
479         int ret;
480         unsigned long flags;
481
482         spin_lock_irqsave(&priv->sta_lock, flags);
483
484         if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
485                 IWL_ERROR("index %d not used in uCode key table.\n",
486                           keyconf->keyidx);
487
488         priv->default_wep_key--;
489         memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
490         ret = iwl_send_static_wepkey_cmd(priv, 1);
491         IWL_DEBUG_WEP("Remove default WEP key: idx=%d ret=%d\n",
492                       keyconf->keyidx, ret);
493         spin_unlock_irqrestore(&priv->sta_lock, flags);
494
495         return ret;
496 }
497 EXPORT_SYMBOL(iwl_remove_default_wep_key);
498
499 int iwl_set_default_wep_key(struct iwl_priv *priv,
500                             struct ieee80211_key_conf *keyconf)
501 {
502         int ret;
503         unsigned long flags;
504
505         if (keyconf->keylen != WEP_KEY_LEN_128 &&
506             keyconf->keylen != WEP_KEY_LEN_64) {
507                 IWL_DEBUG_WEP("Bad WEP key length %d\n", keyconf->keylen);
508                 return -EINVAL;
509         }
510
511         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
512         keyconf->hw_key_idx = HW_KEY_DEFAULT;
513         priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
514
515         spin_lock_irqsave(&priv->sta_lock, flags);
516         priv->default_wep_key++;
517
518         if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
519                 IWL_ERROR("index %d already used in uCode key table.\n",
520                         keyconf->keyidx);
521
522         priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
523         memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
524                                                         keyconf->keylen);
525
526         ret = iwl_send_static_wepkey_cmd(priv, 0);
527         IWL_DEBUG_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
528                 keyconf->keylen, keyconf->keyidx, ret);
529         spin_unlock_irqrestore(&priv->sta_lock, flags);
530
531         return ret;
532 }
533 EXPORT_SYMBOL(iwl_set_default_wep_key);
534
535 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
536                                 struct ieee80211_key_conf *keyconf,
537                                 u8 sta_id)
538 {
539         unsigned long flags;
540         __le16 key_flags = 0;
541         int ret;
542
543         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
544
545         key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
546         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
547         key_flags &= ~STA_KEY_FLG_INVALID;
548
549         if (keyconf->keylen == WEP_KEY_LEN_128)
550                 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
551
552         if (sta_id == priv->hw_params.bcast_sta_id)
553                 key_flags |= STA_KEY_MULTICAST_MSK;
554
555         spin_lock_irqsave(&priv->sta_lock, flags);
556
557         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
558         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
559         priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
560
561         memcpy(priv->stations[sta_id].keyinfo.key,
562                                 keyconf->key, keyconf->keylen);
563
564         memcpy(&priv->stations[sta_id].sta.key.key[3],
565                                 keyconf->key, keyconf->keylen);
566
567         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
568                         == STA_KEY_FLG_NO_ENC)
569                 priv->stations[sta_id].sta.key.key_offset =
570                                  iwl_get_free_ucode_key_index(priv);
571         /* else, we are overriding an existing key => no need to allocated room
572          * in uCode. */
573
574         priv->stations[sta_id].sta.key.key_flags = key_flags;
575         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
576         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
577
578         ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
579
580         spin_unlock_irqrestore(&priv->sta_lock, flags);
581
582         return ret;
583 }
584
585 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
586                                    struct ieee80211_key_conf *keyconf,
587                                    u8 sta_id)
588 {
589         unsigned long flags;
590         __le16 key_flags = 0;
591
592         key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
593         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
594         key_flags &= ~STA_KEY_FLG_INVALID;
595
596         if (sta_id == priv->hw_params.bcast_sta_id)
597                 key_flags |= STA_KEY_MULTICAST_MSK;
598
599         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
600
601         spin_lock_irqsave(&priv->sta_lock, flags);
602         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
603         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
604
605         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
606                keyconf->keylen);
607
608         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
609                keyconf->keylen);
610
611         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
612                         == STA_KEY_FLG_NO_ENC)
613                 priv->stations[sta_id].sta.key.key_offset =
614                                  iwl_get_free_ucode_key_index(priv);
615         /* else, we are overriding an existing key => no need to allocated room
616          * in uCode. */
617
618         priv->stations[sta_id].sta.key.key_flags = key_flags;
619         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
620         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
621
622         spin_unlock_irqrestore(&priv->sta_lock, flags);
623
624         IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
625         return iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
626 }
627
628 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
629                                    struct ieee80211_key_conf *keyconf,
630                                    u8 sta_id)
631 {
632         unsigned long flags;
633         int ret = 0;
634
635         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
636         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
637
638         spin_lock_irqsave(&priv->sta_lock, flags);
639
640         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
641         priv->stations[sta_id].keyinfo.keylen = 16;
642
643         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
644                         == STA_KEY_FLG_NO_ENC)
645                 priv->stations[sta_id].sta.key.key_offset =
646                                  iwl_get_free_ucode_key_index(priv);
647         /* else, we are overriding an existing key => no need to allocated room
648          * in uCode. */
649
650         /* This copy is acutally not needed: we get the key with each TX */
651         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
652
653         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
654
655         spin_unlock_irqrestore(&priv->sta_lock, flags);
656
657         return ret;
658 }
659
660 int iwl_remove_dynamic_key(struct iwl_priv *priv,
661                                 struct ieee80211_key_conf *keyconf,
662                                 u8 sta_id)
663 {
664         unsigned long flags;
665         int ret = 0;
666         u16 key_flags;
667         u8 keyidx;
668
669         priv->key_mapping_key--;
670
671         spin_lock_irqsave(&priv->sta_lock, flags);
672         key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
673         keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
674
675         IWL_DEBUG_WEP("Remove dynamic key: idx=%d sta=%d\n",
676                       keyconf->keyidx, sta_id);
677
678         if (keyconf->keyidx != keyidx) {
679                 /* We need to remove a key with index different that the one
680                  * in the uCode. This means that the key we need to remove has
681                  * been replaced by another one with different index.
682                  * Don't do anything and return ok
683                  */
684                 spin_unlock_irqrestore(&priv->sta_lock, flags);
685                 return 0;
686         }
687
688         if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
689                 &priv->ucode_key_table))
690                 IWL_ERROR("index %d not used in uCode key table.\n",
691                         priv->stations[sta_id].sta.key.key_offset);
692         memset(&priv->stations[sta_id].keyinfo, 0,
693                                         sizeof(struct iwl_hw_key));
694         memset(&priv->stations[sta_id].sta.key, 0,
695                                         sizeof(struct iwl4965_keyinfo));
696         priv->stations[sta_id].sta.key.key_flags =
697                         STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
698         priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
699         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
700         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
701
702         ret =  iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
703         spin_unlock_irqrestore(&priv->sta_lock, flags);
704         return ret;
705 }
706 EXPORT_SYMBOL(iwl_remove_dynamic_key);
707
708 int iwl_set_dynamic_key(struct iwl_priv *priv,
709                                 struct ieee80211_key_conf *keyconf, u8 sta_id)
710 {
711         int ret;
712
713         priv->key_mapping_key++;
714         keyconf->hw_key_idx = HW_KEY_DYNAMIC;
715
716         switch (keyconf->alg) {
717         case ALG_CCMP:
718                 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
719                 break;
720         case ALG_TKIP:
721                 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
722                 break;
723         case ALG_WEP:
724                 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
725                 break;
726         default:
727                 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
728                 ret = -EINVAL;
729         }
730
731         IWL_DEBUG_WEP("Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
732                       keyconf->alg, keyconf->keylen, keyconf->keyidx,
733                       sta_id, ret);
734
735         return ret;
736 }
737 EXPORT_SYMBOL(iwl_set_dynamic_key);
738
739 #ifdef CONFIG_IWLWIFI_DEBUG
740 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
741                            struct iwl_link_quality_cmd *lq)
742 {
743         int i;
744         IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
745         IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
746                        lq->general_params.single_stream_ant_msk,
747                        lq->general_params.dual_stream_ant_msk);
748
749         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
750                 IWL_DEBUG_RATE("lq index %d 0x%X\n",
751                                i, lq->rs_table[i].rate_n_flags);
752 }
753 #else
754 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
755                                    struct iwl_link_quality_cmd *lq)
756 {
757 }
758 #endif
759
760 int iwl_send_lq_cmd(struct iwl_priv *priv,
761                     struct iwl_link_quality_cmd *lq, u8 flags)
762 {
763         struct iwl_host_cmd cmd = {
764                 .id = REPLY_TX_LINK_QUALITY_CMD,
765                 .len = sizeof(struct iwl_link_quality_cmd),
766                 .meta.flags = flags,
767                 .data = lq,
768         };
769
770         if ((lq->sta_id == 0xFF) &&
771             (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
772                 return -EINVAL;
773
774         if (lq->sta_id == 0xFF)
775                 lq->sta_id = IWL_AP_ID;
776
777         iwl_dump_lq_cmd(priv,lq);
778
779         if (iwl_is_associated(priv) && priv->assoc_station_added &&
780             priv->lq_mngr.lq_ready)
781                 return  iwl_send_cmd(priv, &cmd);
782
783         return 0;
784 }
785 EXPORT_SYMBOL(iwl_send_lq_cmd);
786
787 /**
788  * iwl_sta_init_lq - Initialize a station's hardware rate table
789  *
790  * The uCode's station table contains a table of fallback rates
791  * for automatic fallback during transmission.
792  *
793  * NOTE: This sets up a default set of values.  These will be replaced later
794  *       if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
795  *       rc80211_simple.
796  *
797  * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
798  *       calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
799  *       which requires station table entry to exist).
800  */
801 static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, int is_ap)
802 {
803         int i, r;
804         struct iwl_link_quality_cmd link_cmd = {
805                 .reserved1 = 0,
806         };
807         u16 rate_flags;
808
809         /* Set up the rate scaling to start at selected rate, fall back
810          * all the way down to 1M in IEEE order, and then spin on 1M */
811         if (is_ap)
812                 r = IWL_RATE_54M_INDEX;
813         else if (priv->band == IEEE80211_BAND_5GHZ)
814                 r = IWL_RATE_6M_INDEX;
815         else
816                 r = IWL_RATE_1M_INDEX;
817
818         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
819                 rate_flags = 0;
820                 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
821                         rate_flags |= RATE_MCS_CCK_MSK;
822
823                 /* Use Tx antenna B only */
824                 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
825
826                 link_cmd.rs_table[i].rate_n_flags =
827                         iwl4965_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
828                 r = iwl4965_get_prev_ieee_rate(r);
829         }
830
831         link_cmd.general_params.single_stream_ant_msk = 2;
832         link_cmd.general_params.dual_stream_ant_msk = 3;
833         link_cmd.agg_params.agg_dis_start_th = 3;
834         link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
835
836         /* Update the rate scaling for control frame Tx to AP */
837         link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
838
839         iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
840                                sizeof(link_cmd), &link_cmd, NULL);
841 }
842 /**
843  * iwl_rxon_add_station - add station into station table.
844  *
845  * there is only one AP station with id= IWL_AP_ID
846  * NOTE: mutex must be held before calling this fnction
847  */
848 int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
849 {
850         u8 sta_id;
851
852         /* Add station to device's station table */
853         struct ieee80211_conf *conf = &priv->hw->conf;
854         struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
855
856         if ((is_ap) &&
857             (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
858             (priv->iw_mode == IEEE80211_IF_TYPE_STA))
859                 sta_id = iwl_add_station_flags(priv, addr, is_ap,
860                                                    0, cur_ht_config);
861         else
862                 sta_id = iwl_add_station_flags(priv, addr, is_ap,
863                                                    0, NULL);
864
865         /* Set up default rate scaling table in device's station table */
866         iwl_sta_init_lq(priv, addr, is_ap);
867
868         return sta_id;
869 }
870 EXPORT_SYMBOL(iwl_rxon_add_station);
871
872
873 /**
874  * iwl_get_sta_id - Find station's index within station table
875  *
876  * If new IBSS station, create new entry in station table
877  */
878 int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
879 {
880         int sta_id;
881         u16 fc = le16_to_cpu(hdr->frame_control);
882         DECLARE_MAC_BUF(mac);
883
884         /* If this frame is broadcast or management, use broadcast station id */
885         if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
886             is_multicast_ether_addr(hdr->addr1))
887                 return priv->hw_params.bcast_sta_id;
888
889         switch (priv->iw_mode) {
890
891         /* If we are a client station in a BSS network, use the special
892          * AP station entry (that's the only station we communicate with) */
893         case IEEE80211_IF_TYPE_STA:
894                 return IWL_AP_ID;
895
896         /* If we are an AP, then find the station, or use BCAST */
897         case IEEE80211_IF_TYPE_AP:
898                 sta_id = iwl_find_station(priv, hdr->addr1);
899                 if (sta_id != IWL_INVALID_STATION)
900                         return sta_id;
901                 return priv->hw_params.bcast_sta_id;
902
903         /* If this frame is going out to an IBSS network, find the station,
904          * or create a new station table entry */
905         case IEEE80211_IF_TYPE_IBSS:
906                 sta_id = iwl_find_station(priv, hdr->addr1);
907                 if (sta_id != IWL_INVALID_STATION)
908                         return sta_id;
909
910                 /* Create new station table entry */
911                 sta_id = iwl_add_station_flags(priv, hdr->addr1,
912                                                    0, CMD_ASYNC, NULL);
913
914                 if (sta_id != IWL_INVALID_STATION)
915                         return sta_id;
916
917                 IWL_DEBUG_DROP("Station %s not in station map. "
918                                "Defaulting to broadcast...\n",
919                                print_mac(mac, hdr->addr1));
920                 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
921                 return priv->hw_params.bcast_sta_id;
922
923         default:
924                 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
925                 return priv->hw_params.bcast_sta_id;
926         }
927 }
928 EXPORT_SYMBOL(iwl_get_sta_id);
929
930
931 /**
932  * iwl_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
933  */
934 void iwl_sta_modify_enable_tid_tx(struct iwl_priv *priv, int sta_id, int tid)
935 {
936         unsigned long flags;
937
938         /* Remove "disable" flag, to enable Tx for this TID */
939         spin_lock_irqsave(&priv->sta_lock, flags);
940         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
941         priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
942         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
943         spin_unlock_irqrestore(&priv->sta_lock, flags);
944
945         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
946 }
947 EXPORT_SYMBOL(iwl_sta_modify_enable_tid_tx);
948
949