mac80211: clean up sta_info_destroy() users wrt. RCU/locking
[linux-2.6] / net / mac80211 / ieee80211_sta.c
1 /*
2  * BSS client mode implementation
3  * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4  * Copyright 2004, Instant802 Networks, Inc.
5  * Copyright 2005, Devicescape Software, Inc.
6  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
7  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 /* TODO:
15  * order BSS list by RSSI(?) ("quality of AP")
16  * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17  *    SSID)
18  */
19 #include <linux/delay.h>
20 #include <linux/if_ether.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/wireless.h>
25 #include <linux/random.h>
26 #include <linux/etherdevice.h>
27 #include <linux/rtnetlink.h>
28 #include <net/iw_handler.h>
29 #include <asm/types.h>
30
31 #include <net/mac80211.h>
32 #include "ieee80211_i.h"
33 #include "ieee80211_rate.h"
34 #include "ieee80211_led.h"
35 #include "mesh.h"
36
37 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38 #define IEEE80211_AUTH_MAX_TRIES 3
39 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40 #define IEEE80211_ASSOC_MAX_TRIES 3
41 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
42 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
43 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
44 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
46 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47 #define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
48
49 #define IEEE80211_PROBE_DELAY (HZ / 33)
50 #define IEEE80211_CHANNEL_TIME (HZ / 33)
51 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
55 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
56
57 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
60 #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
61
62 #define ERP_INFO_USE_PROTECTION BIT(1)
63
64 /* mgmt header + 1 byte action code */
65 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
66
67 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
68 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
69 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
70 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
71 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
72
73 /* next values represent the buffer size for A-MPDU frame.
74  * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
75 #define IEEE80211_MIN_AMPDU_BUF 0x8
76 #define IEEE80211_MAX_AMPDU_BUF 0x40
77
78 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
79                                      u8 *ssid, size_t ssid_len);
80 static struct ieee80211_sta_bss *
81 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
82                      u8 *ssid, u8 ssid_len);
83 static void ieee80211_rx_bss_put(struct net_device *dev,
84                                  struct ieee80211_sta_bss *bss);
85 static int ieee80211_sta_find_ibss(struct net_device *dev,
86                                    struct ieee80211_if_sta *ifsta);
87 static int ieee80211_sta_wep_configured(struct net_device *dev);
88 static int ieee80211_sta_start_scan(struct net_device *dev,
89                                     u8 *ssid, size_t ssid_len);
90 static int ieee80211_sta_config_auth(struct net_device *dev,
91                                      struct ieee80211_if_sta *ifsta);
92
93
94 void ieee802_11_parse_elems(u8 *start, size_t len,
95                             struct ieee802_11_elems *elems)
96 {
97         size_t left = len;
98         u8 *pos = start;
99
100         memset(elems, 0, sizeof(*elems));
101
102         while (left >= 2) {
103                 u8 id, elen;
104
105                 id = *pos++;
106                 elen = *pos++;
107                 left -= 2;
108
109                 if (elen > left)
110                         return;
111
112                 switch (id) {
113                 case WLAN_EID_SSID:
114                         elems->ssid = pos;
115                         elems->ssid_len = elen;
116                         break;
117                 case WLAN_EID_SUPP_RATES:
118                         elems->supp_rates = pos;
119                         elems->supp_rates_len = elen;
120                         break;
121                 case WLAN_EID_FH_PARAMS:
122                         elems->fh_params = pos;
123                         elems->fh_params_len = elen;
124                         break;
125                 case WLAN_EID_DS_PARAMS:
126                         elems->ds_params = pos;
127                         elems->ds_params_len = elen;
128                         break;
129                 case WLAN_EID_CF_PARAMS:
130                         elems->cf_params = pos;
131                         elems->cf_params_len = elen;
132                         break;
133                 case WLAN_EID_TIM:
134                         elems->tim = pos;
135                         elems->tim_len = elen;
136                         break;
137                 case WLAN_EID_IBSS_PARAMS:
138                         elems->ibss_params = pos;
139                         elems->ibss_params_len = elen;
140                         break;
141                 case WLAN_EID_CHALLENGE:
142                         elems->challenge = pos;
143                         elems->challenge_len = elen;
144                         break;
145                 case WLAN_EID_WPA:
146                         if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
147                             pos[2] == 0xf2) {
148                                 /* Microsoft OUI (00:50:F2) */
149                                 if (pos[3] == 1) {
150                                         /* OUI Type 1 - WPA IE */
151                                         elems->wpa = pos;
152                                         elems->wpa_len = elen;
153                                 } else if (elen >= 5 && pos[3] == 2) {
154                                         if (pos[4] == 0) {
155                                                 elems->wmm_info = pos;
156                                                 elems->wmm_info_len = elen;
157                                         } else if (pos[4] == 1) {
158                                                 elems->wmm_param = pos;
159                                                 elems->wmm_param_len = elen;
160                                         }
161                                 }
162                         }
163                         break;
164                 case WLAN_EID_RSN:
165                         elems->rsn = pos;
166                         elems->rsn_len = elen;
167                         break;
168                 case WLAN_EID_ERP_INFO:
169                         elems->erp_info = pos;
170                         elems->erp_info_len = elen;
171                         break;
172                 case WLAN_EID_EXT_SUPP_RATES:
173                         elems->ext_supp_rates = pos;
174                         elems->ext_supp_rates_len = elen;
175                         break;
176                 case WLAN_EID_HT_CAPABILITY:
177                         elems->ht_cap_elem = pos;
178                         elems->ht_cap_elem_len = elen;
179                         break;
180                 case WLAN_EID_HT_EXTRA_INFO:
181                         elems->ht_info_elem = pos;
182                         elems->ht_info_elem_len = elen;
183                         break;
184                 case WLAN_EID_MESH_ID:
185                         elems->mesh_id = pos;
186                         elems->mesh_id_len = elen;
187                         break;
188                 case WLAN_EID_MESH_CONFIG:
189                         elems->mesh_config = pos;
190                         elems->mesh_config_len = elen;
191                         break;
192                 case WLAN_EID_PEER_LINK:
193                         elems->peer_link = pos;
194                         elems->peer_link_len = elen;
195                         break;
196                 case WLAN_EID_PREQ:
197                         elems->preq = pos;
198                         elems->preq_len = elen;
199                         break;
200                 case WLAN_EID_PREP:
201                         elems->prep = pos;
202                         elems->prep_len = elen;
203                         break;
204                 case WLAN_EID_PERR:
205                         elems->perr = pos;
206                         elems->perr_len = elen;
207                         break;
208                 default:
209                         break;
210                 }
211
212                 left -= elen;
213                 pos += elen;
214         }
215 }
216
217
218 static int ecw2cw(int ecw)
219 {
220         return (1 << ecw) - 1;
221 }
222
223
224 static void ieee80211_sta_def_wmm_params(struct net_device *dev,
225                                          struct ieee80211_sta_bss *bss,
226                                          int ibss)
227 {
228         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
229         struct ieee80211_local *local = sdata->local;
230         int i, have_higher_than_11mbit = 0;
231
232
233         /* cf. IEEE 802.11 9.2.12 */
234         for (i = 0; i < bss->supp_rates_len; i++)
235                 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
236                         have_higher_than_11mbit = 1;
237
238         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
239             have_higher_than_11mbit)
240                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
241         else
242                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
243
244
245         if (local->ops->conf_tx) {
246                 struct ieee80211_tx_queue_params qparam;
247                 int i;
248
249                 memset(&qparam, 0, sizeof(qparam));
250
251                 qparam.aifs = 2;
252
253                 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
254                     !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
255                         qparam.cw_min = 31;
256                 else
257                         qparam.cw_min = 15;
258
259                 qparam.cw_max = 1023;
260                 qparam.txop = 0;
261
262                 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
263                         local->ops->conf_tx(local_to_hw(local),
264                                            i + IEEE80211_TX_QUEUE_DATA0,
265                                            &qparam);
266
267                 if (ibss) {
268                         /* IBSS uses different parameters for Beacon sending */
269                         qparam.cw_min++;
270                         qparam.cw_min *= 2;
271                         qparam.cw_min--;
272                         local->ops->conf_tx(local_to_hw(local),
273                                            IEEE80211_TX_QUEUE_BEACON, &qparam);
274                 }
275         }
276 }
277
278 static void ieee80211_sta_wmm_params(struct net_device *dev,
279                                      struct ieee80211_if_sta *ifsta,
280                                      u8 *wmm_param, size_t wmm_param_len)
281 {
282         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
283         struct ieee80211_tx_queue_params params;
284         size_t left;
285         int count;
286         u8 *pos;
287
288         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
289                 return;
290         count = wmm_param[6] & 0x0f;
291         if (count == ifsta->wmm_last_param_set)
292                 return;
293         ifsta->wmm_last_param_set = count;
294
295         pos = wmm_param + 8;
296         left = wmm_param_len - 8;
297
298         memset(&params, 0, sizeof(params));
299
300         if (!local->ops->conf_tx)
301                 return;
302
303         local->wmm_acm = 0;
304         for (; left >= 4; left -= 4, pos += 4) {
305                 int aci = (pos[0] >> 5) & 0x03;
306                 int acm = (pos[0] >> 4) & 0x01;
307                 int queue;
308
309                 switch (aci) {
310                 case 1:
311                         queue = IEEE80211_TX_QUEUE_DATA3;
312                         if (acm) {
313                                 local->wmm_acm |= BIT(0) | BIT(3);
314                         }
315                         break;
316                 case 2:
317                         queue = IEEE80211_TX_QUEUE_DATA1;
318                         if (acm) {
319                                 local->wmm_acm |= BIT(4) | BIT(5);
320                         }
321                         break;
322                 case 3:
323                         queue = IEEE80211_TX_QUEUE_DATA0;
324                         if (acm) {
325                                 local->wmm_acm |= BIT(6) | BIT(7);
326                         }
327                         break;
328                 case 0:
329                 default:
330                         queue = IEEE80211_TX_QUEUE_DATA2;
331                         if (acm) {
332                                 local->wmm_acm |= BIT(1) | BIT(2);
333                         }
334                         break;
335                 }
336
337                 params.aifs = pos[0] & 0x0f;
338                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
339                 params.cw_min = ecw2cw(pos[1] & 0x0f);
340                 params.txop = pos[2] | (pos[3] << 8);
341 #ifdef CONFIG_MAC80211_DEBUG
342                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
343                        "cWmin=%d cWmax=%d txop=%d\n",
344                        dev->name, queue, aci, acm, params.aifs, params.cw_min,
345                        params.cw_max, params.txop);
346 #endif
347                 /* TODO: handle ACM (block TX, fallback to next lowest allowed
348                  * AC for now) */
349                 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
350                         printk(KERN_DEBUG "%s: failed to set TX queue "
351                                "parameters for queue %d\n", dev->name, queue);
352                 }
353         }
354 }
355
356
357 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
358                                    u8 erp_value)
359 {
360         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
361         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
362         bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
363         bool preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
364         DECLARE_MAC_BUF(mac);
365         u32 changed = 0;
366
367         if (use_protection != bss_conf->use_cts_prot) {
368                 if (net_ratelimit()) {
369                         printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
370                                "%s)\n",
371                                sdata->dev->name,
372                                use_protection ? "enabled" : "disabled",
373                                print_mac(mac, ifsta->bssid));
374                 }
375                 bss_conf->use_cts_prot = use_protection;
376                 changed |= BSS_CHANGED_ERP_CTS_PROT;
377         }
378
379         if (preamble_mode != bss_conf->use_short_preamble) {
380                 if (net_ratelimit()) {
381                         printk(KERN_DEBUG "%s: switched to %s barker preamble"
382                                " (BSSID=%s)\n",
383                                sdata->dev->name,
384                                (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
385                                         "short" : "long",
386                                print_mac(mac, ifsta->bssid));
387                 }
388                 bss_conf->use_short_preamble = preamble_mode;
389                 changed |= BSS_CHANGED_ERP_PREAMBLE;
390         }
391
392         return changed;
393 }
394
395 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
396                                    struct ieee80211_ht_info *ht_info)
397 {
398
399         if (ht_info == NULL)
400                 return -EINVAL;
401
402         memset(ht_info, 0, sizeof(*ht_info));
403
404         if (ht_cap_ie) {
405                 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
406
407                 ht_info->ht_supported = 1;
408                 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
409                 ht_info->ampdu_factor =
410                         ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
411                 ht_info->ampdu_density =
412                         (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
413                 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
414         } else
415                 ht_info->ht_supported = 0;
416
417         return 0;
418 }
419
420 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
421                         struct ieee80211_ht_addt_info *ht_add_info_ie,
422                         struct ieee80211_ht_bss_info *bss_info)
423 {
424         if (bss_info == NULL)
425                 return -EINVAL;
426
427         memset(bss_info, 0, sizeof(*bss_info));
428
429         if (ht_add_info_ie) {
430                 u16 op_mode;
431                 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
432
433                 bss_info->primary_channel = ht_add_info_ie->control_chan;
434                 bss_info->bss_cap = ht_add_info_ie->ht_param;
435                 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
436         }
437
438         return 0;
439 }
440
441 static void ieee80211_sta_send_associnfo(struct net_device *dev,
442                                          struct ieee80211_if_sta *ifsta)
443 {
444         char *buf;
445         size_t len;
446         int i;
447         union iwreq_data wrqu;
448
449         if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
450                 return;
451
452         buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
453                                 ifsta->assocresp_ies_len), GFP_KERNEL);
454         if (!buf)
455                 return;
456
457         len = sprintf(buf, "ASSOCINFO(");
458         if (ifsta->assocreq_ies) {
459                 len += sprintf(buf + len, "ReqIEs=");
460                 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
461                         len += sprintf(buf + len, "%02x",
462                                        ifsta->assocreq_ies[i]);
463                 }
464         }
465         if (ifsta->assocresp_ies) {
466                 if (ifsta->assocreq_ies)
467                         len += sprintf(buf + len, " ");
468                 len += sprintf(buf + len, "RespIEs=");
469                 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
470                         len += sprintf(buf + len, "%02x",
471                                        ifsta->assocresp_ies[i]);
472                 }
473         }
474         len += sprintf(buf + len, ")");
475
476         if (len > IW_CUSTOM_MAX) {
477                 len = sprintf(buf, "ASSOCRESPIE=");
478                 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
479                         len += sprintf(buf + len, "%02x",
480                                        ifsta->assocresp_ies[i]);
481                 }
482         }
483
484         memset(&wrqu, 0, sizeof(wrqu));
485         wrqu.data.length = len;
486         wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
487
488         kfree(buf);
489 }
490
491
492 static void ieee80211_set_associated(struct net_device *dev,
493                                      struct ieee80211_if_sta *ifsta,
494                                      bool assoc)
495 {
496         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
497         struct ieee80211_local *local = sdata->local;
498         union iwreq_data wrqu;
499         u32 changed = BSS_CHANGED_ASSOC;
500
501         if (assoc) {
502                 struct ieee80211_sta_bss *bss;
503
504                 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
505
506                 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
507                         return;
508
509                 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
510                                            local->hw.conf.channel->center_freq,
511                                            ifsta->ssid, ifsta->ssid_len);
512                 if (bss) {
513                         if (bss->has_erp_value)
514                                 changed |= ieee80211_handle_erp_ie(
515                                                 sdata, bss->erp_value);
516                         ieee80211_rx_bss_put(dev, bss);
517                 }
518
519                 netif_carrier_on(dev);
520                 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
521                 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
522                 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
523                 ieee80211_sta_send_associnfo(dev, ifsta);
524         } else {
525                 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
526                 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
527                 netif_carrier_off(dev);
528                 ieee80211_reset_erp_info(dev);
529                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
530         }
531         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
532         wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
533         ifsta->last_probe = jiffies;
534         ieee80211_led_assoc(local, assoc);
535
536         sdata->bss_conf.assoc = assoc;
537         ieee80211_bss_info_change_notify(sdata, changed);
538 }
539
540 static void ieee80211_set_disassoc(struct net_device *dev,
541                                    struct ieee80211_if_sta *ifsta, int deauth)
542 {
543         if (deauth)
544                 ifsta->auth_tries = 0;
545         ifsta->assoc_tries = 0;
546         ieee80211_set_associated(dev, ifsta, 0);
547 }
548
549 void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
550                       int encrypt)
551 {
552         struct ieee80211_sub_if_data *sdata;
553         struct ieee80211_tx_packet_data *pkt_data;
554
555         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
556         skb->dev = sdata->local->mdev;
557         skb_set_mac_header(skb, 0);
558         skb_set_network_header(skb, 0);
559         skb_set_transport_header(skb, 0);
560
561         pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
562         memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
563         pkt_data->ifindex = sdata->dev->ifindex;
564         if (!encrypt)
565                 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
566
567         dev_queue_xmit(skb);
568 }
569
570
571 static void ieee80211_send_auth(struct net_device *dev,
572                                 struct ieee80211_if_sta *ifsta,
573                                 int transaction, u8 *extra, size_t extra_len,
574                                 int encrypt)
575 {
576         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
577         struct sk_buff *skb;
578         struct ieee80211_mgmt *mgmt;
579
580         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
581                             sizeof(*mgmt) + 6 + extra_len);
582         if (!skb) {
583                 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
584                        "frame\n", dev->name);
585                 return;
586         }
587         skb_reserve(skb, local->hw.extra_tx_headroom);
588
589         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
590         memset(mgmt, 0, 24 + 6);
591         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
592                                            IEEE80211_STYPE_AUTH);
593         if (encrypt)
594                 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
595         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
596         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
597         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
598         mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
599         mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
600         ifsta->auth_transaction = transaction + 1;
601         mgmt->u.auth.status_code = cpu_to_le16(0);
602         if (extra)
603                 memcpy(skb_put(skb, extra_len), extra, extra_len);
604
605         ieee80211_sta_tx(dev, skb, encrypt);
606 }
607
608
609 static void ieee80211_authenticate(struct net_device *dev,
610                                    struct ieee80211_if_sta *ifsta)
611 {
612         DECLARE_MAC_BUF(mac);
613
614         ifsta->auth_tries++;
615         if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
616                 printk(KERN_DEBUG "%s: authentication with AP %s"
617                        " timed out\n",
618                        dev->name, print_mac(mac, ifsta->bssid));
619                 ifsta->state = IEEE80211_DISABLED;
620                 return;
621         }
622
623         ifsta->state = IEEE80211_AUTHENTICATE;
624         printk(KERN_DEBUG "%s: authenticate with AP %s\n",
625                dev->name, print_mac(mac, ifsta->bssid));
626
627         ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
628
629         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
630 }
631
632
633 static void ieee80211_send_assoc(struct net_device *dev,
634                                  struct ieee80211_if_sta *ifsta)
635 {
636         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
637         struct sk_buff *skb;
638         struct ieee80211_mgmt *mgmt;
639         u8 *pos, *ies;
640         int i, len;
641         u16 capab;
642         struct ieee80211_sta_bss *bss;
643         int wmm = 0;
644         struct ieee80211_supported_band *sband;
645
646         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
647                             sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
648                             ifsta->ssid_len);
649         if (!skb) {
650                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
651                        "frame\n", dev->name);
652                 return;
653         }
654         skb_reserve(skb, local->hw.extra_tx_headroom);
655
656         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
657
658         capab = ifsta->capab;
659
660         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
661                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
662                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
663                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
664                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
665         }
666
667         bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
668                                    local->hw.conf.channel->center_freq,
669                                    ifsta->ssid, ifsta->ssid_len);
670         if (bss) {
671                 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
672                         capab |= WLAN_CAPABILITY_PRIVACY;
673                 if (bss->wmm_ie) {
674                         wmm = 1;
675                 }
676                 ieee80211_rx_bss_put(dev, bss);
677         }
678
679         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
680         memset(mgmt, 0, 24);
681         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
682         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
683         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
684
685         if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
686                 skb_put(skb, 10);
687                 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
688                                                    IEEE80211_STYPE_REASSOC_REQ);
689                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
690                 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
691                 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
692                        ETH_ALEN);
693         } else {
694                 skb_put(skb, 4);
695                 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
696                                                    IEEE80211_STYPE_ASSOC_REQ);
697                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
698                 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
699         }
700
701         /* SSID */
702         ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
703         *pos++ = WLAN_EID_SSID;
704         *pos++ = ifsta->ssid_len;
705         memcpy(pos, ifsta->ssid, ifsta->ssid_len);
706
707         len = sband->n_bitrates;
708         if (len > 8)
709                 len = 8;
710         pos = skb_put(skb, len + 2);
711         *pos++ = WLAN_EID_SUPP_RATES;
712         *pos++ = len;
713         for (i = 0; i < len; i++) {
714                 int rate = sband->bitrates[i].bitrate;
715                 *pos++ = (u8) (rate / 5);
716         }
717
718         if (sband->n_bitrates > len) {
719                 pos = skb_put(skb, sband->n_bitrates - len + 2);
720                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
721                 *pos++ = sband->n_bitrates - len;
722                 for (i = len; i < sband->n_bitrates; i++) {
723                         int rate = sband->bitrates[i].bitrate;
724                         *pos++ = (u8) (rate / 5);
725                 }
726         }
727
728         if (ifsta->extra_ie) {
729                 pos = skb_put(skb, ifsta->extra_ie_len);
730                 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
731         }
732
733         if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
734                 pos = skb_put(skb, 9);
735                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
736                 *pos++ = 7; /* len */
737                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
738                 *pos++ = 0x50;
739                 *pos++ = 0xf2;
740                 *pos++ = 2; /* WME */
741                 *pos++ = 0; /* WME info */
742                 *pos++ = 1; /* WME ver */
743                 *pos++ = 0;
744         }
745         /* wmm support is a must to HT */
746         if (wmm && sband->ht_info.ht_supported) {
747                 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
748                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
749                 *pos++ = WLAN_EID_HT_CAPABILITY;
750                 *pos++ = sizeof(struct ieee80211_ht_cap);
751                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
752                 memcpy(pos, &tmp, sizeof(u16));
753                 pos += sizeof(u16);
754                 /* TODO: needs a define here for << 2 */
755                 *pos++ = sband->ht_info.ampdu_factor |
756                          (sband->ht_info.ampdu_density << 2);
757                 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
758         }
759
760         kfree(ifsta->assocreq_ies);
761         ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
762         ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
763         if (ifsta->assocreq_ies)
764                 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
765
766         ieee80211_sta_tx(dev, skb, 0);
767 }
768
769
770 static void ieee80211_send_deauth(struct net_device *dev,
771                                   struct ieee80211_if_sta *ifsta, u16 reason)
772 {
773         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
774         struct sk_buff *skb;
775         struct ieee80211_mgmt *mgmt;
776
777         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
778         if (!skb) {
779                 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
780                        "frame\n", dev->name);
781                 return;
782         }
783         skb_reserve(skb, local->hw.extra_tx_headroom);
784
785         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
786         memset(mgmt, 0, 24);
787         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
788         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
789         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
790         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
791                                            IEEE80211_STYPE_DEAUTH);
792         skb_put(skb, 2);
793         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
794
795         ieee80211_sta_tx(dev, skb, 0);
796 }
797
798
799 static void ieee80211_send_disassoc(struct net_device *dev,
800                                     struct ieee80211_if_sta *ifsta, u16 reason)
801 {
802         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
803         struct sk_buff *skb;
804         struct ieee80211_mgmt *mgmt;
805
806         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
807         if (!skb) {
808                 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
809                        "frame\n", dev->name);
810                 return;
811         }
812         skb_reserve(skb, local->hw.extra_tx_headroom);
813
814         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
815         memset(mgmt, 0, 24);
816         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
817         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
818         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
819         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
820                                            IEEE80211_STYPE_DISASSOC);
821         skb_put(skb, 2);
822         mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
823
824         ieee80211_sta_tx(dev, skb, 0);
825 }
826
827
828 static int ieee80211_privacy_mismatch(struct net_device *dev,
829                                       struct ieee80211_if_sta *ifsta)
830 {
831         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
832         struct ieee80211_sta_bss *bss;
833         int bss_privacy;
834         int wep_privacy;
835         int privacy_invoked;
836
837         if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
838                 return 0;
839
840         bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
841                                    local->hw.conf.channel->center_freq,
842                                    ifsta->ssid, ifsta->ssid_len);
843         if (!bss)
844                 return 0;
845
846         bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
847         wep_privacy = !!ieee80211_sta_wep_configured(dev);
848         privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
849
850         ieee80211_rx_bss_put(dev, bss);
851
852         if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
853                 return 0;
854
855         return 1;
856 }
857
858
859 static void ieee80211_associate(struct net_device *dev,
860                                 struct ieee80211_if_sta *ifsta)
861 {
862         DECLARE_MAC_BUF(mac);
863
864         ifsta->assoc_tries++;
865         if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
866                 printk(KERN_DEBUG "%s: association with AP %s"
867                        " timed out\n",
868                        dev->name, print_mac(mac, ifsta->bssid));
869                 ifsta->state = IEEE80211_DISABLED;
870                 return;
871         }
872
873         ifsta->state = IEEE80211_ASSOCIATE;
874         printk(KERN_DEBUG "%s: associate with AP %s\n",
875                dev->name, print_mac(mac, ifsta->bssid));
876         if (ieee80211_privacy_mismatch(dev, ifsta)) {
877                 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
878                        "mixed-cell disabled - abort association\n", dev->name);
879                 ifsta->state = IEEE80211_DISABLED;
880                 return;
881         }
882
883         ieee80211_send_assoc(dev, ifsta);
884
885         mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
886 }
887
888
889 static void ieee80211_associated(struct net_device *dev,
890                                  struct ieee80211_if_sta *ifsta)
891 {
892         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
893         struct sta_info *sta;
894         int disassoc;
895         DECLARE_MAC_BUF(mac);
896
897         /* TODO: start monitoring current AP signal quality and number of
898          * missed beacons. Scan other channels every now and then and search
899          * for better APs. */
900         /* TODO: remove expired BSSes */
901
902         ifsta->state = IEEE80211_ASSOCIATED;
903
904         rcu_read_lock();
905
906         sta = sta_info_get(local, ifsta->bssid);
907         if (!sta) {
908                 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
909                        dev->name, print_mac(mac, ifsta->bssid));
910                 disassoc = 1;
911         } else {
912                 disassoc = 0;
913                 if (time_after(jiffies,
914                                sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
915                         if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
916                                 printk(KERN_DEBUG "%s: No ProbeResp from "
917                                        "current AP %s - assume out of "
918                                        "range\n",
919                                        dev->name, print_mac(mac, ifsta->bssid));
920                                 disassoc = 1;
921                                 sta_info_unlink(&sta);
922                         } else
923                                 ieee80211_send_probe_req(dev, ifsta->bssid,
924                                                          local->scan_ssid,
925                                                          local->scan_ssid_len);
926                         ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
927                 } else {
928                         ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
929                         if (time_after(jiffies, ifsta->last_probe +
930                                        IEEE80211_PROBE_INTERVAL)) {
931                                 ifsta->last_probe = jiffies;
932                                 ieee80211_send_probe_req(dev, ifsta->bssid,
933                                                          ifsta->ssid,
934                                                          ifsta->ssid_len);
935                         }
936                 }
937         }
938
939         rcu_read_unlock();
940
941         if (disassoc && sta) {
942                 rtnl_lock();
943                 sta_info_destroy(sta);
944                 rtnl_unlock();
945         }
946
947         if (disassoc) {
948                 ifsta->state = IEEE80211_DISABLED;
949                 ieee80211_set_associated(dev, ifsta, 0);
950         } else {
951                 mod_timer(&ifsta->timer, jiffies +
952                                       IEEE80211_MONITORING_INTERVAL);
953         }
954 }
955
956
957 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
958                                      u8 *ssid, size_t ssid_len)
959 {
960         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
961         struct ieee80211_supported_band *sband;
962         struct sk_buff *skb;
963         struct ieee80211_mgmt *mgmt;
964         u8 *pos, *supp_rates, *esupp_rates = NULL;
965         int i;
966
967         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
968         if (!skb) {
969                 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
970                        "request\n", dev->name);
971                 return;
972         }
973         skb_reserve(skb, local->hw.extra_tx_headroom);
974
975         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
976         memset(mgmt, 0, 24);
977         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
978                                            IEEE80211_STYPE_PROBE_REQ);
979         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
980         if (dst) {
981                 memcpy(mgmt->da, dst, ETH_ALEN);
982                 memcpy(mgmt->bssid, dst, ETH_ALEN);
983         } else {
984                 memset(mgmt->da, 0xff, ETH_ALEN);
985                 memset(mgmt->bssid, 0xff, ETH_ALEN);
986         }
987         pos = skb_put(skb, 2 + ssid_len);
988         *pos++ = WLAN_EID_SSID;
989         *pos++ = ssid_len;
990         memcpy(pos, ssid, ssid_len);
991
992         supp_rates = skb_put(skb, 2);
993         supp_rates[0] = WLAN_EID_SUPP_RATES;
994         supp_rates[1] = 0;
995         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
996
997         for (i = 0; i < sband->n_bitrates; i++) {
998                 struct ieee80211_rate *rate = &sband->bitrates[i];
999                 if (esupp_rates) {
1000                         pos = skb_put(skb, 1);
1001                         esupp_rates[1]++;
1002                 } else if (supp_rates[1] == 8) {
1003                         esupp_rates = skb_put(skb, 3);
1004                         esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1005                         esupp_rates[1] = 1;
1006                         pos = &esupp_rates[2];
1007                 } else {
1008                         pos = skb_put(skb, 1);
1009                         supp_rates[1]++;
1010                 }
1011                 *pos = rate->bitrate / 5;
1012         }
1013
1014         ieee80211_sta_tx(dev, skb, 0);
1015 }
1016
1017
1018 static int ieee80211_sta_wep_configured(struct net_device *dev)
1019 {
1020         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1021         if (!sdata || !sdata->default_key ||
1022             sdata->default_key->conf.alg != ALG_WEP)
1023                 return 0;
1024         return 1;
1025 }
1026
1027
1028 static void ieee80211_auth_completed(struct net_device *dev,
1029                                      struct ieee80211_if_sta *ifsta)
1030 {
1031         printk(KERN_DEBUG "%s: authenticated\n", dev->name);
1032         ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1033         ieee80211_associate(dev, ifsta);
1034 }
1035
1036
1037 static void ieee80211_auth_challenge(struct net_device *dev,
1038                                      struct ieee80211_if_sta *ifsta,
1039                                      struct ieee80211_mgmt *mgmt,
1040                                      size_t len)
1041 {
1042         u8 *pos;
1043         struct ieee802_11_elems elems;
1044
1045         printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1046         pos = mgmt->u.auth.variable;
1047         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1048         if (!elems.challenge) {
1049                 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1050                        "frame\n", dev->name);
1051                 return;
1052         }
1053         ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1054                             elems.challenge_len + 2, 1);
1055 }
1056
1057 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1058                                         u8 dialog_token, u16 status, u16 policy,
1059                                         u16 buf_size, u16 timeout)
1060 {
1061         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1062         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1063         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1064         struct sk_buff *skb;
1065         struct ieee80211_mgmt *mgmt;
1066         u16 capab;
1067
1068         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1069                                         sizeof(mgmt->u.action.u.addba_resp));
1070         if (!skb) {
1071                 printk(KERN_DEBUG "%s: failed to allocate buffer "
1072                        "for addba resp frame\n", dev->name);
1073                 return;
1074         }
1075
1076         skb_reserve(skb, local->hw.extra_tx_headroom);
1077         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1078         memset(mgmt, 0, 24);
1079         memcpy(mgmt->da, da, ETH_ALEN);
1080         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1081         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1082                 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1083         else
1084                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1085         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1086                                            IEEE80211_STYPE_ACTION);
1087
1088         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1089         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1090         mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1091         mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1092
1093         capab = (u16)(policy << 1);     /* bit 1 aggregation policy */
1094         capab |= (u16)(tid << 2);       /* bit 5:2 TID number */
1095         capab |= (u16)(buf_size << 6);  /* bit 15:6 max size of aggregation */
1096
1097         mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1098         mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1099         mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1100
1101         ieee80211_sta_tx(dev, skb, 0);
1102
1103         return;
1104 }
1105
1106 void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1107                                 u16 tid, u8 dialog_token, u16 start_seq_num,
1108                                 u16 agg_size, u16 timeout)
1109 {
1110         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1111         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1112         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1113         struct sk_buff *skb;
1114         struct ieee80211_mgmt *mgmt;
1115         u16 capab;
1116
1117         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1118                                 sizeof(mgmt->u.action.u.addba_req));
1119
1120
1121         if (!skb) {
1122                 printk(KERN_ERR "%s: failed to allocate buffer "
1123                                 "for addba request frame\n", dev->name);
1124                 return;
1125         }
1126         skb_reserve(skb, local->hw.extra_tx_headroom);
1127         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1128         memset(mgmt, 0, 24);
1129         memcpy(mgmt->da, da, ETH_ALEN);
1130         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1131         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1132                 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1133         else
1134                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1135
1136         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1137                                         IEEE80211_STYPE_ACTION);
1138
1139         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1140
1141         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1142         mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1143
1144         mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1145         capab = (u16)(1 << 1);          /* bit 1 aggregation policy */
1146         capab |= (u16)(tid << 2);       /* bit 5:2 TID number */
1147         capab |= (u16)(agg_size << 6);  /* bit 15:6 max size of aggergation */
1148
1149         mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1150
1151         mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1152         mgmt->u.action.u.addba_req.start_seq_num =
1153                                         cpu_to_le16(start_seq_num << 4);
1154
1155         ieee80211_sta_tx(dev, skb, 0);
1156 }
1157
1158 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1159                                                 struct ieee80211_mgmt *mgmt,
1160                                                 size_t len)
1161 {
1162         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1163         struct ieee80211_hw *hw = &local->hw;
1164         struct ieee80211_conf *conf = &hw->conf;
1165         struct sta_info *sta;
1166         struct tid_ampdu_rx *tid_agg_rx;
1167         u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1168         u8 dialog_token;
1169         int ret = -EOPNOTSUPP;
1170         DECLARE_MAC_BUF(mac);
1171
1172         rcu_read_lock();
1173
1174         sta = sta_info_get(local, mgmt->sa);
1175         if (!sta) {
1176                 rcu_read_unlock();
1177                 return;
1178         }
1179
1180         /* extract session parameters from addba request frame */
1181         dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1182         timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1183         start_seq_num =
1184                 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1185
1186         capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1187         ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1188         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1189         buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1190
1191         status = WLAN_STATUS_REQUEST_DECLINED;
1192
1193         /* sanity check for incoming parameters:
1194          * check if configuration can support the BA policy
1195          * and if buffer size does not exceeds max value */
1196         if (((ba_policy != 1)
1197                 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1198                 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1199                 status = WLAN_STATUS_INVALID_QOS_PARAM;
1200 #ifdef CONFIG_MAC80211_HT_DEBUG
1201                 if (net_ratelimit())
1202                         printk(KERN_DEBUG "AddBA Req with bad params from "
1203                                 "%s on tid %u. policy %d, buffer size %d\n",
1204                                 print_mac(mac, mgmt->sa), tid, ba_policy,
1205                                 buf_size);
1206 #endif /* CONFIG_MAC80211_HT_DEBUG */
1207                 goto end_no_lock;
1208         }
1209         /* determine default buffer size */
1210         if (buf_size == 0) {
1211                 struct ieee80211_supported_band *sband;
1212
1213                 sband = local->hw.wiphy->bands[conf->channel->band];
1214                 buf_size = IEEE80211_MIN_AMPDU_BUF;
1215                 buf_size = buf_size << sband->ht_info.ampdu_factor;
1216         }
1217
1218
1219         /* examine state machine */
1220         spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1221
1222         if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1223 #ifdef CONFIG_MAC80211_HT_DEBUG
1224                 if (net_ratelimit())
1225                         printk(KERN_DEBUG "unexpected AddBA Req from "
1226                                 "%s on tid %u\n",
1227                                 print_mac(mac, mgmt->sa), tid);
1228 #endif /* CONFIG_MAC80211_HT_DEBUG */
1229                 goto end;
1230         }
1231
1232         /* prepare A-MPDU MLME for Rx aggregation */
1233         sta->ampdu_mlme.tid_rx[tid] =
1234                         kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1235         if (!sta->ampdu_mlme.tid_rx[tid]) {
1236                 if (net_ratelimit())
1237                         printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1238                                         tid);
1239                 goto end;
1240         }
1241         /* rx timer */
1242         sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1243                                 sta_rx_agg_session_timer_expired;
1244         sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1245                                 (unsigned long)&sta->timer_to_tid[tid];
1246         init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1247
1248         tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1249
1250         /* prepare reordering buffer */
1251         tid_agg_rx->reorder_buf =
1252                 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
1253         if (!tid_agg_rx->reorder_buf) {
1254                 if (net_ratelimit())
1255                         printk(KERN_ERR "can not allocate reordering buffer "
1256                                "to tid %d\n", tid);
1257                 kfree(sta->ampdu_mlme.tid_rx[tid]);
1258                 goto end;
1259         }
1260         memset(tid_agg_rx->reorder_buf, 0,
1261                 buf_size * sizeof(struct sk_buf *));
1262
1263         if (local->ops->ampdu_action)
1264                 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1265                                                sta->addr, tid, &start_seq_num);
1266 #ifdef CONFIG_MAC80211_HT_DEBUG
1267         printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1268 #endif /* CONFIG_MAC80211_HT_DEBUG */
1269
1270         if (ret) {
1271                 kfree(tid_agg_rx->reorder_buf);
1272                 kfree(tid_agg_rx);
1273                 sta->ampdu_mlme.tid_rx[tid] = NULL;
1274                 goto end;
1275         }
1276
1277         /* change state and send addba resp */
1278         sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1279         tid_agg_rx->dialog_token = dialog_token;
1280         tid_agg_rx->ssn = start_seq_num;
1281         tid_agg_rx->head_seq_num = start_seq_num;
1282         tid_agg_rx->buf_size = buf_size;
1283         tid_agg_rx->timeout = timeout;
1284         tid_agg_rx->stored_mpdu_num = 0;
1285         status = WLAN_STATUS_SUCCESS;
1286 end:
1287         spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1288
1289 end_no_lock:
1290         ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1291                                   dialog_token, status, 1, buf_size, timeout);
1292         rcu_read_unlock();
1293 }
1294
1295 static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1296                                              struct ieee80211_mgmt *mgmt,
1297                                              size_t len)
1298 {
1299         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1300         struct ieee80211_hw *hw = &local->hw;
1301         struct sta_info *sta;
1302         u16 capab;
1303         u16 tid;
1304         u8 *state;
1305
1306         rcu_read_lock();
1307
1308         sta = sta_info_get(local, mgmt->sa);
1309         if (!sta) {
1310                 rcu_read_unlock();
1311                 return;
1312         }
1313
1314         capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1315         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1316
1317         state = &sta->ampdu_mlme.tid_state_tx[tid];
1318
1319         spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1320
1321         if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1322                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1323                 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1324                         "%d\n", *state);
1325                 goto addba_resp_exit;
1326         }
1327
1328         if (mgmt->u.action.u.addba_resp.dialog_token !=
1329                 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1330                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1331 #ifdef CONFIG_MAC80211_HT_DEBUG
1332                 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1333 #endif /* CONFIG_MAC80211_HT_DEBUG */
1334                 goto addba_resp_exit;
1335         }
1336
1337         del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1338 #ifdef CONFIG_MAC80211_HT_DEBUG
1339         printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1340 #endif /* CONFIG_MAC80211_HT_DEBUG */
1341         if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1342                         == WLAN_STATUS_SUCCESS) {
1343                 if (*state & HT_ADDBA_RECEIVED_MSK)
1344                         printk(KERN_DEBUG "double addBA response\n");
1345
1346                 *state |= HT_ADDBA_RECEIVED_MSK;
1347                 sta->ampdu_mlme.addba_req_num[tid] = 0;
1348
1349                 if (*state == HT_AGG_STATE_OPERATIONAL) {
1350                         printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1351                         ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1352                 }
1353
1354                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1355                 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1356         } else {
1357                 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1358
1359                 sta->ampdu_mlme.addba_req_num[tid]++;
1360                 /* this will allow the state check in stop_BA_session */
1361                 *state = HT_AGG_STATE_OPERATIONAL;
1362                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1363                 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1364                                              WLAN_BACK_INITIATOR);
1365         }
1366
1367 addba_resp_exit:
1368         rcu_read_unlock();
1369 }
1370
1371 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1372                           u16 initiator, u16 reason_code)
1373 {
1374         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1375         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1376         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1377         struct sk_buff *skb;
1378         struct ieee80211_mgmt *mgmt;
1379         u16 params;
1380
1381         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1382                                         sizeof(mgmt->u.action.u.delba));
1383
1384         if (!skb) {
1385                 printk(KERN_ERR "%s: failed to allocate buffer "
1386                                         "for delba frame\n", dev->name);
1387                 return;
1388         }
1389
1390         skb_reserve(skb, local->hw.extra_tx_headroom);
1391         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1392         memset(mgmt, 0, 24);
1393         memcpy(mgmt->da, da, ETH_ALEN);
1394         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1395         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1396                 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1397         else
1398                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1399         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1400                                         IEEE80211_STYPE_ACTION);
1401
1402         skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1403
1404         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1405         mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1406         params = (u16)(initiator << 11);        /* bit 11 initiator */
1407         params |= (u16)(tid << 12);             /* bit 15:12 TID number */
1408
1409         mgmt->u.action.u.delba.params = cpu_to_le16(params);
1410         mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1411
1412         ieee80211_sta_tx(dev, skb, 0);
1413 }
1414
1415 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1416                                         u16 initiator, u16 reason)
1417 {
1418         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1419         struct ieee80211_hw *hw = &local->hw;
1420         struct sta_info *sta;
1421         int ret, i;
1422
1423         rcu_read_lock();
1424
1425         sta = sta_info_get(local, ra);
1426         if (!sta) {
1427                 rcu_read_unlock();
1428                 return;
1429         }
1430
1431         /* check if TID is in operational state */
1432         spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1433         if (sta->ampdu_mlme.tid_state_rx[tid]
1434                                 != HT_AGG_STATE_OPERATIONAL) {
1435                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1436                 rcu_read_unlock();
1437                 return;
1438         }
1439         sta->ampdu_mlme.tid_state_rx[tid] =
1440                 HT_AGG_STATE_REQ_STOP_BA_MSK |
1441                 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1442                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1443
1444         /* stop HW Rx aggregation. ampdu_action existence
1445          * already verified in session init so we add the BUG_ON */
1446         BUG_ON(!local->ops->ampdu_action);
1447
1448         ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1449                                         ra, tid, NULL);
1450         if (ret)
1451                 printk(KERN_DEBUG "HW problem - can not stop rx "
1452                                 "aggergation for tid %d\n", tid);
1453
1454         /* shutdown timer has not expired */
1455         if (initiator != WLAN_BACK_TIMER)
1456                 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1457
1458         /* check if this is a self generated aggregation halt */
1459         if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1460                 ieee80211_send_delba(dev, ra, tid, 0, reason);
1461
1462         /* free the reordering buffer */
1463         for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1464                 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1465                         /* release the reordered frames */
1466                         dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1467                         sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1468                         sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1469                 }
1470         }
1471         /* free resources */
1472         kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1473         kfree(sta->ampdu_mlme.tid_rx[tid]);
1474         sta->ampdu_mlme.tid_rx[tid] = NULL;
1475         sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1476
1477         rcu_read_unlock();
1478 }
1479
1480
1481 static void ieee80211_sta_process_delba(struct net_device *dev,
1482                         struct ieee80211_mgmt *mgmt, size_t len)
1483 {
1484         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1485         struct sta_info *sta;
1486         u16 tid, params;
1487         u16 initiator;
1488         DECLARE_MAC_BUF(mac);
1489
1490         rcu_read_lock();
1491
1492         sta = sta_info_get(local, mgmt->sa);
1493         if (!sta) {
1494                 rcu_read_unlock();
1495                 return;
1496         }
1497
1498         params = le16_to_cpu(mgmt->u.action.u.delba.params);
1499         tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1500         initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1501
1502 #ifdef CONFIG_MAC80211_HT_DEBUG
1503         if (net_ratelimit())
1504                 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1505                         print_mac(mac, mgmt->sa),
1506                         initiator ? "initiator" : "recipient", tid,
1507                         mgmt->u.action.u.delba.reason_code);
1508 #endif /* CONFIG_MAC80211_HT_DEBUG */
1509
1510         if (initiator == WLAN_BACK_INITIATOR)
1511                 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1512                                                  WLAN_BACK_INITIATOR, 0);
1513         else { /* WLAN_BACK_RECIPIENT */
1514                 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1515                 sta->ampdu_mlme.tid_state_tx[tid] =
1516                                 HT_AGG_STATE_OPERATIONAL;
1517                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1518                 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1519                                              WLAN_BACK_RECIPIENT);
1520         }
1521         rcu_read_unlock();
1522 }
1523
1524 /*
1525  * After sending add Block Ack request we activated a timer until
1526  * add Block Ack response will arrive from the recipient.
1527  * If this timer expires sta_addba_resp_timer_expired will be executed.
1528  */
1529 void sta_addba_resp_timer_expired(unsigned long data)
1530 {
1531         /* not an elegant detour, but there is no choice as the timer passes
1532          * only one argument, and both sta_info and TID are needed, so init
1533          * flow in sta_info_create gives the TID as data, while the timer_to_id
1534          * array gives the sta through container_of */
1535         u16 tid = *(int *)data;
1536         struct sta_info *temp_sta = container_of((void *)data,
1537                 struct sta_info, timer_to_tid[tid]);
1538
1539         struct ieee80211_local *local = temp_sta->local;
1540         struct ieee80211_hw *hw = &local->hw;
1541         struct sta_info *sta;
1542         u8 *state;
1543
1544         rcu_read_lock();
1545
1546         sta = sta_info_get(local, temp_sta->addr);
1547         if (!sta) {
1548                 rcu_read_unlock();
1549                 return;
1550         }
1551
1552         state = &sta->ampdu_mlme.tid_state_tx[tid];
1553         /* check if the TID waits for addBA response */
1554         spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1555         if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1556                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1557                 *state = HT_AGG_STATE_IDLE;
1558                 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1559                                 "expecting addBA response there", tid);
1560                 goto timer_expired_exit;
1561         }
1562
1563         printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1564
1565         /* go through the state check in stop_BA_session */
1566         *state = HT_AGG_STATE_OPERATIONAL;
1567         spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1568         ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1569                                      WLAN_BACK_INITIATOR);
1570
1571 timer_expired_exit:
1572         rcu_read_unlock();
1573 }
1574
1575 /*
1576  * After accepting the AddBA Request we activated a timer,
1577  * resetting it after each frame that arrives from the originator.
1578  * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1579  */
1580 void sta_rx_agg_session_timer_expired(unsigned long data)
1581 {
1582         /* not an elegant detour, but there is no choice as the timer passes
1583          * only one argument, and verious sta_info are needed here, so init
1584          * flow in sta_info_create gives the TID as data, while the timer_to_id
1585          * array gives the sta through container_of */
1586         u8 *ptid = (u8 *)data;
1587         u8 *timer_to_id = ptid - *ptid;
1588         struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1589                                          timer_to_tid[0]);
1590
1591         printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1592         ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1593                                          (u16)*ptid, WLAN_BACK_TIMER,
1594                                          WLAN_REASON_QSTA_TIMEOUT);
1595 }
1596
1597 void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1598 {
1599         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1600         int i;
1601
1602         for (i = 0; i <  STA_TID_NUM; i++) {
1603                 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1604                                              WLAN_BACK_INITIATOR);
1605                 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1606                                                  WLAN_BACK_RECIPIENT,
1607                                                  WLAN_REASON_QSTA_LEAVE_QBSS);
1608         }
1609 }
1610
1611 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1612                                    struct ieee80211_if_sta *ifsta,
1613                                    struct ieee80211_mgmt *mgmt,
1614                                    size_t len)
1615 {
1616         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1617         u16 auth_alg, auth_transaction, status_code;
1618         DECLARE_MAC_BUF(mac);
1619
1620         if (ifsta->state != IEEE80211_AUTHENTICATE &&
1621             sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
1622                 printk(KERN_DEBUG "%s: authentication frame received from "
1623                        "%s, but not in authenticate state - ignored\n",
1624                        dev->name, print_mac(mac, mgmt->sa));
1625                 return;
1626         }
1627
1628         if (len < 24 + 6) {
1629                 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
1630                        "received from %s - ignored\n",
1631                        dev->name, len, print_mac(mac, mgmt->sa));
1632                 return;
1633         }
1634
1635         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1636             memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1637                 printk(KERN_DEBUG "%s: authentication frame received from "
1638                        "unknown AP (SA=%s BSSID=%s) - "
1639                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1640                        print_mac(mac, mgmt->bssid));
1641                 return;
1642         }
1643
1644         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1645             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1646                 printk(KERN_DEBUG "%s: authentication frame received from "
1647                        "unknown BSSID (SA=%s BSSID=%s) - "
1648                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1649                        print_mac(mac, mgmt->bssid));
1650                 return;
1651         }
1652
1653         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1654         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1655         status_code = le16_to_cpu(mgmt->u.auth.status_code);
1656
1657         printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
1658                "transaction=%d status=%d)\n",
1659                dev->name, print_mac(mac, mgmt->sa), auth_alg,
1660                auth_transaction, status_code);
1661
1662         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1663                 /* IEEE 802.11 standard does not require authentication in IBSS
1664                  * networks and most implementations do not seem to use it.
1665                  * However, try to reply to authentication attempts if someone
1666                  * has actually implemented this.
1667                  * TODO: Could implement shared key authentication. */
1668                 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1669                         printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1670                                "frame (alg=%d transaction=%d)\n",
1671                                dev->name, auth_alg, auth_transaction);
1672                         return;
1673                 }
1674                 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1675         }
1676
1677         if (auth_alg != ifsta->auth_alg ||
1678             auth_transaction != ifsta->auth_transaction) {
1679                 printk(KERN_DEBUG "%s: unexpected authentication frame "
1680                        "(alg=%d transaction=%d)\n",
1681                        dev->name, auth_alg, auth_transaction);
1682                 return;
1683         }
1684
1685         if (status_code != WLAN_STATUS_SUCCESS) {
1686                 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1687                        "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1688                 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1689                         u8 algs[3];
1690                         const int num_algs = ARRAY_SIZE(algs);
1691                         int i, pos;
1692                         algs[0] = algs[1] = algs[2] = 0xff;
1693                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1694                                 algs[0] = WLAN_AUTH_OPEN;
1695                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1696                                 algs[1] = WLAN_AUTH_SHARED_KEY;
1697                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1698                                 algs[2] = WLAN_AUTH_LEAP;
1699                         if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1700                                 pos = 0;
1701                         else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1702                                 pos = 1;
1703                         else
1704                                 pos = 2;
1705                         for (i = 0; i < num_algs; i++) {
1706                                 pos++;
1707                                 if (pos >= num_algs)
1708                                         pos = 0;
1709                                 if (algs[pos] == ifsta->auth_alg ||
1710                                     algs[pos] == 0xff)
1711                                         continue;
1712                                 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1713                                     !ieee80211_sta_wep_configured(dev))
1714                                         continue;
1715                                 ifsta->auth_alg = algs[pos];
1716                                 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1717                                        "next try\n",
1718                                        dev->name, ifsta->auth_alg);
1719                                 break;
1720                         }
1721                 }
1722                 return;
1723         }
1724
1725         switch (ifsta->auth_alg) {
1726         case WLAN_AUTH_OPEN:
1727         case WLAN_AUTH_LEAP:
1728                 ieee80211_auth_completed(dev, ifsta);
1729                 break;
1730         case WLAN_AUTH_SHARED_KEY:
1731                 if (ifsta->auth_transaction == 4)
1732                         ieee80211_auth_completed(dev, ifsta);
1733                 else
1734                         ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1735                 break;
1736         }
1737 }
1738
1739
1740 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1741                                      struct ieee80211_if_sta *ifsta,
1742                                      struct ieee80211_mgmt *mgmt,
1743                                      size_t len)
1744 {
1745         u16 reason_code;
1746         DECLARE_MAC_BUF(mac);
1747
1748         if (len < 24 + 2) {
1749                 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1750                        "received from %s - ignored\n",
1751                        dev->name, len, print_mac(mac, mgmt->sa));
1752                 return;
1753         }
1754
1755         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1756                 printk(KERN_DEBUG "%s: deauthentication frame received from "
1757                        "unknown AP (SA=%s BSSID=%s) - "
1758                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1759                        print_mac(mac, mgmt->bssid));
1760                 return;
1761         }
1762
1763         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1764
1765         printk(KERN_DEBUG "%s: RX deauthentication from %s"
1766                " (reason=%d)\n",
1767                dev->name, print_mac(mac, mgmt->sa), reason_code);
1768
1769         if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1770                 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1771         }
1772
1773         if (ifsta->state == IEEE80211_AUTHENTICATE ||
1774             ifsta->state == IEEE80211_ASSOCIATE ||
1775             ifsta->state == IEEE80211_ASSOCIATED) {
1776                 ifsta->state = IEEE80211_AUTHENTICATE;
1777                 mod_timer(&ifsta->timer, jiffies +
1778                                       IEEE80211_RETRY_AUTH_INTERVAL);
1779         }
1780
1781         ieee80211_set_disassoc(dev, ifsta, 1);
1782         ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1783 }
1784
1785
1786 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1787                                        struct ieee80211_if_sta *ifsta,
1788                                        struct ieee80211_mgmt *mgmt,
1789                                        size_t len)
1790 {
1791         u16 reason_code;
1792         DECLARE_MAC_BUF(mac);
1793
1794         if (len < 24 + 2) {
1795                 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1796                        "received from %s - ignored\n",
1797                        dev->name, len, print_mac(mac, mgmt->sa));
1798                 return;
1799         }
1800
1801         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1802                 printk(KERN_DEBUG "%s: disassociation frame received from "
1803                        "unknown AP (SA=%s BSSID=%s) - "
1804                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1805                        print_mac(mac, mgmt->bssid));
1806                 return;
1807         }
1808
1809         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1810
1811         printk(KERN_DEBUG "%s: RX disassociation from %s"
1812                " (reason=%d)\n",
1813                dev->name, print_mac(mac, mgmt->sa), reason_code);
1814
1815         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1816                 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1817
1818         if (ifsta->state == IEEE80211_ASSOCIATED) {
1819                 ifsta->state = IEEE80211_ASSOCIATE;
1820                 mod_timer(&ifsta->timer, jiffies +
1821                                       IEEE80211_RETRY_AUTH_INTERVAL);
1822         }
1823
1824         ieee80211_set_disassoc(dev, ifsta, 0);
1825 }
1826
1827
1828 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1829                                          struct ieee80211_if_sta *ifsta,
1830                                          struct ieee80211_mgmt *mgmt,
1831                                          size_t len,
1832                                          int reassoc)
1833 {
1834         struct ieee80211_local *local = sdata->local;
1835         struct net_device *dev = sdata->dev;
1836         struct ieee80211_supported_band *sband;
1837         struct sta_info *sta;
1838         u64 rates, basic_rates;
1839         u16 capab_info, status_code, aid;
1840         struct ieee802_11_elems elems;
1841         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1842         u8 *pos;
1843         int i, j;
1844         DECLARE_MAC_BUF(mac);
1845         bool have_higher_than_11mbit = false;
1846
1847         /* AssocResp and ReassocResp have identical structure, so process both
1848          * of them in this function. */
1849
1850         if (ifsta->state != IEEE80211_ASSOCIATE) {
1851                 printk(KERN_DEBUG "%s: association frame received from "
1852                        "%s, but not in associate state - ignored\n",
1853                        dev->name, print_mac(mac, mgmt->sa));
1854                 return;
1855         }
1856
1857         if (len < 24 + 6) {
1858                 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1859                        "received from %s - ignored\n",
1860                        dev->name, len, print_mac(mac, mgmt->sa));
1861                 return;
1862         }
1863
1864         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1865                 printk(KERN_DEBUG "%s: association frame received from "
1866                        "unknown AP (SA=%s BSSID=%s) - "
1867                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1868                        print_mac(mac, mgmt->bssid));
1869                 return;
1870         }
1871
1872         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1873         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1874         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1875
1876         printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1877                "status=%d aid=%d)\n",
1878                dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1879                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1880
1881         if (status_code != WLAN_STATUS_SUCCESS) {
1882                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1883                        dev->name, status_code);
1884                 /* if this was a reassociation, ensure we try a "full"
1885                  * association next time. This works around some broken APs
1886                  * which do not correctly reject reassociation requests. */
1887                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1888                 return;
1889         }
1890
1891         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1892                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1893                        "set\n", dev->name, aid);
1894         aid &= ~(BIT(15) | BIT(14));
1895
1896         pos = mgmt->u.assoc_resp.variable;
1897         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1898
1899         if (!elems.supp_rates) {
1900                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1901                        dev->name);
1902                 return;
1903         }
1904
1905         printk(KERN_DEBUG "%s: associated\n", dev->name);
1906         ifsta->aid = aid;
1907         ifsta->ap_capab = capab_info;
1908
1909         kfree(ifsta->assocresp_ies);
1910         ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1911         ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1912         if (ifsta->assocresp_ies)
1913                 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1914
1915         rcu_read_lock();
1916
1917         /* Add STA entry for the AP */
1918         sta = sta_info_get(local, ifsta->bssid);
1919         if (!sta) {
1920                 struct ieee80211_sta_bss *bss;
1921                 int err;
1922
1923                 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1924                 if (!sta) {
1925                         printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1926                                " the AP\n", dev->name);
1927                         rcu_read_unlock();
1928                         return;
1929                 }
1930                 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
1931                                            local->hw.conf.channel->center_freq,
1932                                            ifsta->ssid, ifsta->ssid_len);
1933                 if (bss) {
1934                         sta->last_rssi = bss->rssi;
1935                         sta->last_signal = bss->signal;
1936                         sta->last_noise = bss->noise;
1937                         ieee80211_rx_bss_put(dev, bss);
1938                 }
1939
1940                 err = sta_info_insert(sta);
1941                 if (err) {
1942                         printk(KERN_DEBUG "%s: failed to insert STA entry for"
1943                                " the AP (error %d)\n", dev->name, err);
1944                         rcu_read_unlock();
1945                         return;
1946                 }
1947         }
1948
1949         /*
1950          * FIXME: Do we really need to update the sta_info's information here?
1951          *        We already know about the AP (we found it in our list) so it
1952          *        should already be filled with the right info, no?
1953          *        As is stands, all this is racy because typically we assume
1954          *        the information that is filled in here (except flags) doesn't
1955          *        change while a STA structure is alive. As such, it should move
1956          *        to between the sta_info_alloc() and sta_info_insert() above.
1957          */
1958
1959         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1960                       WLAN_STA_AUTHORIZED;
1961
1962         rates = 0;
1963         basic_rates = 0;
1964         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1965
1966         for (i = 0; i < elems.supp_rates_len; i++) {
1967                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1968
1969                 if (rate > 110)
1970                         have_higher_than_11mbit = true;
1971
1972                 for (j = 0; j < sband->n_bitrates; j++) {
1973                         if (sband->bitrates[j].bitrate == rate)
1974                                 rates |= BIT(j);
1975                         if (elems.supp_rates[i] & 0x80)
1976                                 basic_rates |= BIT(j);
1977                 }
1978         }
1979
1980         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1981                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1982
1983                 if (rate > 110)
1984                         have_higher_than_11mbit = true;
1985
1986                 for (j = 0; j < sband->n_bitrates; j++) {
1987                         if (sband->bitrates[j].bitrate == rate)
1988                                 rates |= BIT(j);
1989                         if (elems.ext_supp_rates[i] & 0x80)
1990                                 basic_rates |= BIT(j);
1991                 }
1992         }
1993
1994         sta->supp_rates[local->hw.conf.channel->band] = rates;
1995         sdata->basic_rates = basic_rates;
1996
1997         /* cf. IEEE 802.11 9.2.12 */
1998         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1999             have_higher_than_11mbit)
2000                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2001         else
2002                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2003
2004         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2005             local->ops->conf_ht) {
2006                 struct ieee80211_ht_bss_info bss_info;
2007
2008                 ieee80211_ht_cap_ie_to_ht_info(
2009                                 (struct ieee80211_ht_cap *)
2010                                 elems.ht_cap_elem, &sta->ht_info);
2011                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2012                                 (struct ieee80211_ht_addt_info *)
2013                                 elems.ht_info_elem, &bss_info);
2014                 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
2015         }
2016
2017         rate_control_rate_init(sta, local);
2018
2019         if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2020                 sta->flags |= WLAN_STA_WME;
2021                 rcu_read_unlock();
2022                 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2023                                          elems.wmm_param_len);
2024         } else
2025                 rcu_read_unlock();
2026
2027         /* set AID, ieee80211_set_associated() will tell the driver */
2028         bss_conf->aid = aid;
2029         ieee80211_set_associated(dev, ifsta, 1);
2030
2031         ieee80211_associated(dev, ifsta);
2032 }
2033
2034
2035 /* Caller must hold local->sta_bss_lock */
2036 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2037                                         struct ieee80211_sta_bss *bss)
2038 {
2039         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2040         u8 hash_idx;
2041
2042         if (bss_mesh_cfg(bss))
2043                 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2044                                         bss_mesh_id_len(bss));
2045         else
2046                 hash_idx = STA_HASH(bss->bssid);
2047
2048         bss->hnext = local->sta_bss_hash[hash_idx];
2049         local->sta_bss_hash[hash_idx] = bss;
2050 }
2051
2052
2053 /* Caller must hold local->sta_bss_lock */
2054 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2055                                         struct ieee80211_sta_bss *bss)
2056 {
2057         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2058         struct ieee80211_sta_bss *b, *prev = NULL;
2059         b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2060         while (b) {
2061                 if (b == bss) {
2062                         if (!prev)
2063                                 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2064                                         bss->hnext;
2065                         else
2066                                 prev->hnext = bss->hnext;
2067                         break;
2068                 }
2069                 prev = b;
2070                 b = b->hnext;
2071         }
2072 }
2073
2074
2075 static struct ieee80211_sta_bss *
2076 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
2077                      u8 *ssid, u8 ssid_len)
2078 {
2079         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2080         struct ieee80211_sta_bss *bss;
2081
2082         bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2083         if (!bss)
2084                 return NULL;
2085         atomic_inc(&bss->users);
2086         atomic_inc(&bss->users);
2087         memcpy(bss->bssid, bssid, ETH_ALEN);
2088         bss->freq = freq;
2089         if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2090                 memcpy(bss->ssid, ssid, ssid_len);
2091                 bss->ssid_len = ssid_len;
2092         }
2093
2094         spin_lock_bh(&local->sta_bss_lock);
2095         /* TODO: order by RSSI? */
2096         list_add_tail(&bss->list, &local->sta_bss_list);
2097         __ieee80211_rx_bss_hash_add(dev, bss);
2098         spin_unlock_bh(&local->sta_bss_lock);
2099         return bss;
2100 }
2101
2102 static struct ieee80211_sta_bss *
2103 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
2104                      u8 *ssid, u8 ssid_len)
2105 {
2106         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2107         struct ieee80211_sta_bss *bss;
2108
2109         spin_lock_bh(&local->sta_bss_lock);
2110         bss = local->sta_bss_hash[STA_HASH(bssid)];
2111         while (bss) {
2112                 if (!bss_mesh_cfg(bss) &&
2113                     !memcmp(bss->bssid, bssid, ETH_ALEN) &&
2114                     bss->freq == freq &&
2115                     bss->ssid_len == ssid_len &&
2116                     (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
2117                         atomic_inc(&bss->users);
2118                         break;
2119                 }
2120                 bss = bss->hnext;
2121         }
2122         spin_unlock_bh(&local->sta_bss_lock);
2123         return bss;
2124 }
2125
2126 #ifdef CONFIG_MAC80211_MESH
2127 static struct ieee80211_sta_bss *
2128 ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2129                           u8 *mesh_cfg, int freq)
2130 {
2131         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2132         struct ieee80211_sta_bss *bss;
2133
2134         spin_lock_bh(&local->sta_bss_lock);
2135         bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2136         while (bss) {
2137                 if (bss_mesh_cfg(bss) &&
2138                     !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
2139                     bss->freq == freq &&
2140                     mesh_id_len == bss->mesh_id_len &&
2141                     (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2142                                                  mesh_id_len))) {
2143                         atomic_inc(&bss->users);
2144                         break;
2145                 }
2146                 bss = bss->hnext;
2147         }
2148         spin_unlock_bh(&local->sta_bss_lock);
2149         return bss;
2150 }
2151
2152 static struct ieee80211_sta_bss *
2153 ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2154                           u8 *mesh_cfg, int freq)
2155 {
2156         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2157         struct ieee80211_sta_bss *bss;
2158
2159         bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2160         if (!bss)
2161                 return NULL;
2162
2163         bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2164         if (!bss->mesh_cfg) {
2165                 kfree(bss);
2166                 return NULL;
2167         }
2168
2169         if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2170                 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2171                 if (!bss->mesh_id) {
2172                         kfree(bss->mesh_cfg);
2173                         kfree(bss);
2174                         return NULL;
2175                 }
2176                 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2177         }
2178
2179         atomic_inc(&bss->users);
2180         atomic_inc(&bss->users);
2181         memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2182         bss->mesh_id_len = mesh_id_len;
2183         bss->freq = freq;
2184         spin_lock_bh(&local->sta_bss_lock);
2185         /* TODO: order by RSSI? */
2186         list_add_tail(&bss->list, &local->sta_bss_list);
2187         __ieee80211_rx_bss_hash_add(dev, bss);
2188         spin_unlock_bh(&local->sta_bss_lock);
2189         return bss;
2190 }
2191 #endif
2192
2193 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2194 {
2195         kfree(bss->wpa_ie);
2196         kfree(bss->rsn_ie);
2197         kfree(bss->wmm_ie);
2198         kfree(bss->ht_ie);
2199         kfree(bss_mesh_id(bss));
2200         kfree(bss_mesh_cfg(bss));
2201         kfree(bss);
2202 }
2203
2204
2205 static void ieee80211_rx_bss_put(struct net_device *dev,
2206                                  struct ieee80211_sta_bss *bss)
2207 {
2208         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2209         if (!atomic_dec_and_test(&bss->users))
2210                 return;
2211
2212         spin_lock_bh(&local->sta_bss_lock);
2213         __ieee80211_rx_bss_hash_del(dev, bss);
2214         list_del(&bss->list);
2215         spin_unlock_bh(&local->sta_bss_lock);
2216         ieee80211_rx_bss_free(bss);
2217 }
2218
2219
2220 void ieee80211_rx_bss_list_init(struct net_device *dev)
2221 {
2222         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2223         spin_lock_init(&local->sta_bss_lock);
2224         INIT_LIST_HEAD(&local->sta_bss_list);
2225 }
2226
2227
2228 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2229 {
2230         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2231         struct ieee80211_sta_bss *bss, *tmp;
2232
2233         list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2234                 ieee80211_rx_bss_put(dev, bss);
2235 }
2236
2237
2238 static int ieee80211_sta_join_ibss(struct net_device *dev,
2239                                    struct ieee80211_if_sta *ifsta,
2240                                    struct ieee80211_sta_bss *bss)
2241 {
2242         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2243         int res, rates, i, j;
2244         struct sk_buff *skb;
2245         struct ieee80211_mgmt *mgmt;
2246         struct ieee80211_tx_control control;
2247         struct rate_selection ratesel;
2248         u8 *pos;
2249         struct ieee80211_sub_if_data *sdata;
2250         struct ieee80211_supported_band *sband;
2251
2252         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2253
2254         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2255
2256         /* Remove possible STA entries from other IBSS networks. */
2257         sta_info_flush(local, sdata);
2258
2259         if (local->ops->reset_tsf) {
2260                 /* Reset own TSF to allow time synchronization work. */
2261                 local->ops->reset_tsf(local_to_hw(local));
2262         }
2263         memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2264         res = ieee80211_if_config(dev);
2265         if (res)
2266                 return res;
2267
2268         local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2269
2270         sdata->drop_unencrypted = bss->capability &
2271                 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2272
2273         res = ieee80211_set_freq(local, bss->freq);
2274
2275         if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2276                 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2277                        "%d MHz\n", dev->name, local->oper_channel->center_freq);
2278                 return -1;
2279         }
2280
2281         /* Set beacon template */
2282         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2283         do {
2284                 if (!skb)
2285                         break;
2286
2287                 skb_reserve(skb, local->hw.extra_tx_headroom);
2288
2289                 mgmt = (struct ieee80211_mgmt *)
2290                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2291                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2292                 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2293                                                    IEEE80211_STYPE_BEACON);
2294                 memset(mgmt->da, 0xff, ETH_ALEN);
2295                 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2296                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2297                 mgmt->u.beacon.beacon_int =
2298                         cpu_to_le16(local->hw.conf.beacon_int);
2299                 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2300
2301                 pos = skb_put(skb, 2 + ifsta->ssid_len);
2302                 *pos++ = WLAN_EID_SSID;
2303                 *pos++ = ifsta->ssid_len;
2304                 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2305
2306                 rates = bss->supp_rates_len;
2307                 if (rates > 8)
2308                         rates = 8;
2309                 pos = skb_put(skb, 2 + rates);
2310                 *pos++ = WLAN_EID_SUPP_RATES;
2311                 *pos++ = rates;
2312                 memcpy(pos, bss->supp_rates, rates);
2313
2314                 if (bss->band == IEEE80211_BAND_2GHZ) {
2315                         pos = skb_put(skb, 2 + 1);
2316                         *pos++ = WLAN_EID_DS_PARAMS;
2317                         *pos++ = 1;
2318                         *pos++ = ieee80211_frequency_to_channel(bss->freq);
2319                 }
2320
2321                 pos = skb_put(skb, 2 + 2);
2322                 *pos++ = WLAN_EID_IBSS_PARAMS;
2323                 *pos++ = 2;
2324                 /* FIX: set ATIM window based on scan results */
2325                 *pos++ = 0;
2326                 *pos++ = 0;
2327
2328                 if (bss->supp_rates_len > 8) {
2329                         rates = bss->supp_rates_len - 8;
2330                         pos = skb_put(skb, 2 + rates);
2331                         *pos++ = WLAN_EID_EXT_SUPP_RATES;
2332                         *pos++ = rates;
2333                         memcpy(pos, &bss->supp_rates[8], rates);
2334                 }
2335
2336                 memset(&control, 0, sizeof(control));
2337                 rate_control_get_rate(dev, sband, skb, &ratesel);
2338                 if (!ratesel.rate) {
2339                         printk(KERN_DEBUG "%s: Failed to determine TX rate "
2340                                "for IBSS beacon\n", dev->name);
2341                         break;
2342                 }
2343                 control.vif = &sdata->vif;
2344                 control.tx_rate = ratesel.rate;
2345                 if (sdata->bss_conf.use_short_preamble &&
2346                     ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2347                         control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2348                 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2349                 control.flags |= IEEE80211_TXCTL_NO_ACK;
2350                 control.retry_limit = 1;
2351
2352                 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2353                 if (ifsta->probe_resp) {
2354                         mgmt = (struct ieee80211_mgmt *)
2355                                 ifsta->probe_resp->data;
2356                         mgmt->frame_control =
2357                                 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2358                                              IEEE80211_STYPE_PROBE_RESP);
2359                 } else {
2360                         printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2361                                "template for IBSS\n", dev->name);
2362                 }
2363
2364                 if (local->ops->beacon_update &&
2365                     local->ops->beacon_update(local_to_hw(local),
2366                                              skb, &control) == 0) {
2367                         printk(KERN_DEBUG "%s: Configured IBSS beacon "
2368                                "template\n", dev->name);
2369                         skb = NULL;
2370                 }
2371
2372                 rates = 0;
2373                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2374                 for (i = 0; i < bss->supp_rates_len; i++) {
2375                         int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2376                         for (j = 0; j < sband->n_bitrates; j++)
2377                                 if (sband->bitrates[j].bitrate == bitrate)
2378                                         rates |= BIT(j);
2379                 }
2380                 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2381
2382                 ieee80211_sta_def_wmm_params(dev, bss, 1);
2383         } while (0);
2384
2385         if (skb) {
2386                 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2387                        "template\n", dev->name);
2388                 dev_kfree_skb(skb);
2389         }
2390
2391         ifsta->state = IEEE80211_IBSS_JOINED;
2392         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2393
2394         ieee80211_rx_bss_put(dev, bss);
2395
2396         return res;
2397 }
2398
2399 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2400                             struct ieee802_11_elems *elems,
2401                             enum ieee80211_band band)
2402 {
2403         struct ieee80211_supported_band *sband;
2404         struct ieee80211_rate *bitrates;
2405         size_t num_rates;
2406         u64 supp_rates;
2407         int i, j;
2408         sband = local->hw.wiphy->bands[band];
2409
2410         if (!sband) {
2411                 WARN_ON(1);
2412                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2413         }
2414
2415         bitrates = sband->bitrates;
2416         num_rates = sband->n_bitrates;
2417         supp_rates = 0;
2418         for (i = 0; i < elems->supp_rates_len +
2419                      elems->ext_supp_rates_len; i++) {
2420                 u8 rate = 0;
2421                 int own_rate;
2422                 if (i < elems->supp_rates_len)
2423                         rate = elems->supp_rates[i];
2424                 else if (elems->ext_supp_rates)
2425                         rate = elems->ext_supp_rates
2426                                 [i - elems->supp_rates_len];
2427                 own_rate = 5 * (rate & 0x7f);
2428                 for (j = 0; j < num_rates; j++)
2429                         if (bitrates[j].bitrate == own_rate)
2430                                 supp_rates |= BIT(j);
2431         }
2432         return supp_rates;
2433 }
2434
2435
2436 static void ieee80211_rx_bss_info(struct net_device *dev,
2437                                   struct ieee80211_mgmt *mgmt,
2438                                   size_t len,
2439                                   struct ieee80211_rx_status *rx_status,
2440                                   int beacon)
2441 {
2442         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2443         struct ieee802_11_elems elems;
2444         size_t baselen;
2445         int freq, clen;
2446         struct ieee80211_sta_bss *bss;
2447         struct sta_info *sta;
2448         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2449         u64 beacon_timestamp, rx_timestamp;
2450         struct ieee80211_channel *channel;
2451         DECLARE_MAC_BUF(mac);
2452         DECLARE_MAC_BUF(mac2);
2453
2454         if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2455                 return; /* ignore ProbeResp to foreign address */
2456
2457 #if 0
2458         printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
2459                dev->name, beacon ? "Beacon" : "Probe Response",
2460                print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
2461 #endif
2462
2463         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2464         if (baselen > len)
2465                 return;
2466
2467         beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2468         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2469
2470         if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2471             elems.mesh_config && mesh_matches_local(&elems, dev)) {
2472                 u64 rates = ieee80211_sta_get_rates(local, &elems,
2473                                                 rx_status->band);
2474
2475                 mesh_neighbour_update(mgmt->sa, rates, dev,
2476                                       mesh_peer_accepts_plinks(&elems, dev));
2477         }
2478
2479         rcu_read_lock();
2480
2481         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
2482             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2483             (sta = sta_info_get(local, mgmt->sa))) {
2484                 u64 prev_rates;
2485                 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2486                                                         rx_status->band);
2487
2488                 prev_rates = sta->supp_rates[rx_status->band];
2489                 sta->supp_rates[rx_status->band] &= supp_rates;
2490                 if (sta->supp_rates[rx_status->band] == 0) {
2491                         /* No matching rates - this should not really happen.
2492                          * Make sure that at least one rate is marked
2493                          * supported to avoid issues with TX rate ctrl. */
2494                         sta->supp_rates[rx_status->band] =
2495                                 sdata->u.sta.supp_rates_bits[rx_status->band];
2496                 }
2497                 if (sta->supp_rates[rx_status->band] != prev_rates) {
2498                         printk(KERN_DEBUG "%s: updated supp_rates set for "
2499                                "%s based on beacon info (0x%llx & 0x%llx -> "
2500                                "0x%llx)\n",
2501                                dev->name, print_mac(mac, sta->addr),
2502                                (unsigned long long) prev_rates,
2503                                (unsigned long long) supp_rates,
2504                                (unsigned long long) sta->supp_rates[rx_status->band]);
2505                 }
2506         }
2507
2508         rcu_read_unlock();
2509
2510         if (elems.ds_params && elems.ds_params_len == 1)
2511                 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
2512         else
2513                 freq = rx_status->freq;
2514
2515         channel = ieee80211_get_channel(local->hw.wiphy, freq);
2516
2517         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2518                 return;
2519
2520 #ifdef CONFIG_MAC80211_MESH
2521         if (elems.mesh_config)
2522                 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2523                                 elems.mesh_id_len, elems.mesh_config, freq);
2524         else
2525 #endif
2526                 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
2527                                            elems.ssid, elems.ssid_len);
2528         if (!bss) {
2529 #ifdef CONFIG_MAC80211_MESH
2530                 if (elems.mesh_config)
2531                         bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2532                                 elems.mesh_id_len, elems.mesh_config, freq);
2533                 else
2534 #endif
2535                         bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2536                                                    elems.ssid, elems.ssid_len);
2537                 if (!bss)
2538                         return;
2539         } else {
2540 #if 0
2541                 /* TODO: order by RSSI? */
2542                 spin_lock_bh(&local->sta_bss_lock);
2543                 list_move_tail(&bss->list, &local->sta_bss_list);
2544                 spin_unlock_bh(&local->sta_bss_lock);
2545 #endif
2546         }
2547
2548         bss->band = rx_status->band;
2549
2550         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2551             bss->probe_resp && beacon) {
2552                 /* STA mode:
2553                  * Do not allow beacon to override data from Probe Response. */
2554                 ieee80211_rx_bss_put(dev, bss);
2555                 return;
2556         }
2557
2558         /* save the ERP value so that it is available at association time */
2559         if (elems.erp_info && elems.erp_info_len >= 1) {
2560                 bss->erp_value = elems.erp_info[0];
2561                 bss->has_erp_value = 1;
2562         }
2563
2564         bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2565         bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2566
2567         bss->supp_rates_len = 0;
2568         if (elems.supp_rates) {
2569                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2570                 if (clen > elems.supp_rates_len)
2571                         clen = elems.supp_rates_len;
2572                 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2573                        clen);
2574                 bss->supp_rates_len += clen;
2575         }
2576         if (elems.ext_supp_rates) {
2577                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2578                 if (clen > elems.ext_supp_rates_len)
2579                         clen = elems.ext_supp_rates_len;
2580                 memcpy(&bss->supp_rates[bss->supp_rates_len],
2581                        elems.ext_supp_rates, clen);
2582                 bss->supp_rates_len += clen;
2583         }
2584
2585         if (elems.wpa &&
2586             (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2587              memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2588                 kfree(bss->wpa_ie);
2589                 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2590                 if (bss->wpa_ie) {
2591                         memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2592                         bss->wpa_ie_len = elems.wpa_len + 2;
2593                 } else
2594                         bss->wpa_ie_len = 0;
2595         } else if (!elems.wpa && bss->wpa_ie) {
2596                 kfree(bss->wpa_ie);
2597                 bss->wpa_ie = NULL;
2598                 bss->wpa_ie_len = 0;
2599         }
2600
2601         if (elems.rsn &&
2602             (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2603              memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2604                 kfree(bss->rsn_ie);
2605                 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2606                 if (bss->rsn_ie) {
2607                         memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2608                         bss->rsn_ie_len = elems.rsn_len + 2;
2609                 } else
2610                         bss->rsn_ie_len = 0;
2611         } else if (!elems.rsn && bss->rsn_ie) {
2612                 kfree(bss->rsn_ie);
2613                 bss->rsn_ie = NULL;
2614                 bss->rsn_ie_len = 0;
2615         }
2616
2617         if (elems.wmm_param &&
2618             (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2619              memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2620                 kfree(bss->wmm_ie);
2621                 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2622                 if (bss->wmm_ie) {
2623                         memcpy(bss->wmm_ie, elems.wmm_param - 2,
2624                                elems.wmm_param_len + 2);
2625                         bss->wmm_ie_len = elems.wmm_param_len + 2;
2626                 } else
2627                         bss->wmm_ie_len = 0;
2628         } else if (!elems.wmm_param && bss->wmm_ie) {
2629                 kfree(bss->wmm_ie);
2630                 bss->wmm_ie = NULL;
2631                 bss->wmm_ie_len = 0;
2632         }
2633         if (elems.ht_cap_elem &&
2634             (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2635              memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2636                 kfree(bss->ht_ie);
2637                 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2638                 if (bss->ht_ie) {
2639                         memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2640                                elems.ht_cap_elem_len + 2);
2641                         bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2642                 } else
2643                         bss->ht_ie_len = 0;
2644         } else if (!elems.ht_cap_elem && bss->ht_ie) {
2645                 kfree(bss->ht_ie);
2646                 bss->ht_ie = NULL;
2647                 bss->ht_ie_len = 0;
2648         }
2649
2650         bss->timestamp = beacon_timestamp;
2651         bss->last_update = jiffies;
2652         bss->rssi = rx_status->ssi;
2653         bss->signal = rx_status->signal;
2654         bss->noise = rx_status->noise;
2655         if (!beacon)
2656                 bss->probe_resp++;
2657
2658         /* check if we need to merge IBSS */
2659         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2660             !local->sta_sw_scanning && !local->sta_hw_scanning &&
2661             bss->capability & WLAN_CAPABILITY_IBSS &&
2662             bss->freq == local->oper_channel->center_freq &&
2663             elems.ssid_len == sdata->u.sta.ssid_len &&
2664             memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2665                 if (rx_status->flag & RX_FLAG_TSFT) {
2666                         /* in order for correct IBSS merging we need mactime
2667                          *
2668                          * since mactime is defined as the time the first data
2669                          * symbol of the frame hits the PHY, and the timestamp
2670                          * of the beacon is defined as "the time that the data
2671                          * symbol containing the first bit of the timestamp is
2672                          * transmitted to the PHY plus the transmitting STA’s
2673                          * delays through its local PHY from the MAC-PHY
2674                          * interface to its interface with the WM"
2675                          * (802.11 11.1.2) - equals the time this bit arrives at
2676                          * the receiver - we have to take into account the
2677                          * offset between the two.
2678                          * e.g: at 1 MBit that means mactime is 192 usec earlier
2679                          * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2680                          */
2681                         int rate = local->hw.wiphy->bands[rx_status->band]->
2682                                         bitrates[rx_status->rate_idx].bitrate;
2683                         rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2684                 } else if (local && local->ops && local->ops->get_tsf)
2685                         /* second best option: get current TSF */
2686                         rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2687                 else
2688                         /* can't merge without knowing the TSF */
2689                         rx_timestamp = -1LLU;
2690 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2691                 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2692                        "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2693                        print_mac(mac, mgmt->sa),
2694                        print_mac(mac2, mgmt->bssid),
2695                        (unsigned long long)rx_timestamp,
2696                        (unsigned long long)beacon_timestamp,
2697                        (unsigned long long)(rx_timestamp - beacon_timestamp),
2698                        jiffies);
2699 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2700                 if (beacon_timestamp > rx_timestamp) {
2701 #ifndef CONFIG_MAC80211_IBSS_DEBUG
2702                         if (net_ratelimit())
2703 #endif
2704                                 printk(KERN_DEBUG "%s: beacon TSF higher than "
2705                                        "local TSF - IBSS merge with BSSID %s\n",
2706                                        dev->name, print_mac(mac, mgmt->bssid));
2707                         ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2708                         ieee80211_ibss_add_sta(dev, NULL,
2709                                                mgmt->bssid, mgmt->sa);
2710                 }
2711         }
2712
2713         ieee80211_rx_bss_put(dev, bss);
2714 }
2715
2716
2717 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2718                                          struct ieee80211_mgmt *mgmt,
2719                                          size_t len,
2720                                          struct ieee80211_rx_status *rx_status)
2721 {
2722         ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2723 }
2724
2725
2726 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2727                                      struct ieee80211_mgmt *mgmt,
2728                                      size_t len,
2729                                      struct ieee80211_rx_status *rx_status)
2730 {
2731         struct ieee80211_sub_if_data *sdata;
2732         struct ieee80211_if_sta *ifsta;
2733         size_t baselen;
2734         struct ieee802_11_elems elems;
2735         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2736         struct ieee80211_conf *conf = &local->hw.conf;
2737         u32 changed = 0;
2738
2739         ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2740
2741         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2742         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2743                 return;
2744         ifsta = &sdata->u.sta;
2745
2746         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2747             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2748                 return;
2749
2750         /* Process beacon from the current BSS */
2751         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2752         if (baselen > len)
2753                 return;
2754
2755         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2756
2757         if (elems.erp_info && elems.erp_info_len >= 1)
2758                 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2759
2760         if (elems.ht_cap_elem && elems.ht_info_elem &&
2761             elems.wmm_param && local->ops->conf_ht &&
2762             conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2763                 struct ieee80211_ht_bss_info bss_info;
2764
2765                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2766                                 (struct ieee80211_ht_addt_info *)
2767                                 elems.ht_info_elem, &bss_info);
2768                 /* check if AP changed bss inforamation */
2769                 if ((conf->ht_bss_conf.primary_channel !=
2770                      bss_info.primary_channel) ||
2771                     (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2772                     (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2773                         ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2774                                                 &bss_info);
2775         }
2776
2777         if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2778                 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2779                                          elems.wmm_param_len);
2780         }
2781
2782         ieee80211_bss_info_change_notify(sdata, changed);
2783 }
2784
2785
2786 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2787                                         struct ieee80211_if_sta *ifsta,
2788                                         struct ieee80211_mgmt *mgmt,
2789                                         size_t len,
2790                                         struct ieee80211_rx_status *rx_status)
2791 {
2792         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2793         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2794         int tx_last_beacon;
2795         struct sk_buff *skb;
2796         struct ieee80211_mgmt *resp;
2797         u8 *pos, *end;
2798         DECLARE_MAC_BUF(mac);
2799 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2800         DECLARE_MAC_BUF(mac2);
2801         DECLARE_MAC_BUF(mac3);
2802 #endif
2803
2804         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2805             ifsta->state != IEEE80211_IBSS_JOINED ||
2806             len < 24 + 2 || !ifsta->probe_resp)
2807                 return;
2808
2809         if (local->ops->tx_last_beacon)
2810                 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2811         else
2812                 tx_last_beacon = 1;
2813
2814 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2815         printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2816                "%s (tx_last_beacon=%d)\n",
2817                dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2818                print_mac(mac3, mgmt->bssid), tx_last_beacon);
2819 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2820
2821         if (!tx_last_beacon)
2822                 return;
2823
2824         if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2825             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2826                 return;
2827
2828         end = ((u8 *) mgmt) + len;
2829         pos = mgmt->u.probe_req.variable;
2830         if (pos[0] != WLAN_EID_SSID ||
2831             pos + 2 + pos[1] > end) {
2832                 if (net_ratelimit()) {
2833                         printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2834                                "from %s\n",
2835                                dev->name, print_mac(mac, mgmt->sa));
2836                 }
2837                 return;
2838         }
2839         if (pos[1] != 0 &&
2840             (pos[1] != ifsta->ssid_len ||
2841              memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2842                 /* Ignore ProbeReq for foreign SSID */
2843                 return;
2844         }
2845
2846         /* Reply with ProbeResp */
2847         skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2848         if (!skb)
2849                 return;
2850
2851         resp = (struct ieee80211_mgmt *) skb->data;
2852         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2853 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2854         printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2855                dev->name, print_mac(mac, resp->da));
2856 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2857         ieee80211_sta_tx(dev, skb, 0);
2858 }
2859
2860 static void ieee80211_rx_mgmt_action(struct net_device *dev,
2861                                      struct ieee80211_if_sta *ifsta,
2862                                      struct ieee80211_mgmt *mgmt,
2863                                      size_t len,
2864                                      struct ieee80211_rx_status *rx_status)
2865 {
2866         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2867
2868         if (len < IEEE80211_MIN_ACTION_SIZE)
2869                 return;
2870
2871         switch (mgmt->u.action.category) {
2872         case WLAN_CATEGORY_BACK:
2873                 switch (mgmt->u.action.u.addba_req.action_code) {
2874                 case WLAN_ACTION_ADDBA_REQ:
2875                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2876                                    sizeof(mgmt->u.action.u.addba_req)))
2877                                 break;
2878                         ieee80211_sta_process_addba_request(dev, mgmt, len);
2879                         break;
2880                 case WLAN_ACTION_ADDBA_RESP:
2881                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2882                                    sizeof(mgmt->u.action.u.addba_resp)))
2883                                 break;
2884                         ieee80211_sta_process_addba_resp(dev, mgmt, len);
2885                         break;
2886                 case WLAN_ACTION_DELBA:
2887                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2888                                    sizeof(mgmt->u.action.u.delba)))
2889                                 break;
2890                         ieee80211_sta_process_delba(dev, mgmt, len);
2891                         break;
2892                 default:
2893                         if (net_ratelimit())
2894                            printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
2895                                         dev->name);
2896                         break;
2897                 }
2898                 break;
2899         case PLINK_CATEGORY:
2900                 if (ieee80211_vif_is_mesh(&sdata->vif))
2901                         mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2902                 break;
2903         case MESH_PATH_SEL_CATEGORY:
2904                 if (ieee80211_vif_is_mesh(&sdata->vif))
2905                         mesh_rx_path_sel_frame(dev, mgmt, len);
2906                 break;
2907         default:
2908                 if (net_ratelimit())
2909                         printk(KERN_DEBUG "%s: Rx unknown action frame - "
2910                         "category=%d\n", dev->name, mgmt->u.action.category);
2911                 break;
2912         }
2913 }
2914
2915 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2916                            struct ieee80211_rx_status *rx_status)
2917 {
2918         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2919         struct ieee80211_sub_if_data *sdata;
2920         struct ieee80211_if_sta *ifsta;
2921         struct ieee80211_mgmt *mgmt;
2922         u16 fc;
2923
2924         if (skb->len < 24)
2925                 goto fail;
2926
2927         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2928         ifsta = &sdata->u.sta;
2929
2930         mgmt = (struct ieee80211_mgmt *) skb->data;
2931         fc = le16_to_cpu(mgmt->frame_control);
2932
2933         switch (fc & IEEE80211_FCTL_STYPE) {
2934         case IEEE80211_STYPE_PROBE_REQ:
2935         case IEEE80211_STYPE_PROBE_RESP:
2936         case IEEE80211_STYPE_BEACON:
2937         case IEEE80211_STYPE_ACTION:
2938                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2939         case IEEE80211_STYPE_AUTH:
2940         case IEEE80211_STYPE_ASSOC_RESP:
2941         case IEEE80211_STYPE_REASSOC_RESP:
2942         case IEEE80211_STYPE_DEAUTH:
2943         case IEEE80211_STYPE_DISASSOC:
2944                 skb_queue_tail(&ifsta->skb_queue, skb);
2945                 queue_work(local->hw.workqueue, &ifsta->work);
2946                 return;
2947         default:
2948                 printk(KERN_DEBUG "%s: received unknown management frame - "
2949                        "stype=%d\n", dev->name,
2950                        (fc & IEEE80211_FCTL_STYPE) >> 4);
2951                 break;
2952         }
2953
2954  fail:
2955         kfree_skb(skb);
2956 }
2957
2958
2959 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2960                                          struct sk_buff *skb)
2961 {
2962         struct ieee80211_rx_status *rx_status;
2963         struct ieee80211_sub_if_data *sdata;
2964         struct ieee80211_if_sta *ifsta;
2965         struct ieee80211_mgmt *mgmt;
2966         u16 fc;
2967
2968         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2969         ifsta = &sdata->u.sta;
2970
2971         rx_status = (struct ieee80211_rx_status *) skb->cb;
2972         mgmt = (struct ieee80211_mgmt *) skb->data;
2973         fc = le16_to_cpu(mgmt->frame_control);
2974
2975         switch (fc & IEEE80211_FCTL_STYPE) {
2976         case IEEE80211_STYPE_PROBE_REQ:
2977                 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2978                                             rx_status);
2979                 break;
2980         case IEEE80211_STYPE_PROBE_RESP:
2981                 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2982                 break;
2983         case IEEE80211_STYPE_BEACON:
2984                 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2985                 break;
2986         case IEEE80211_STYPE_AUTH:
2987                 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2988                 break;
2989         case IEEE80211_STYPE_ASSOC_RESP:
2990                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
2991                 break;
2992         case IEEE80211_STYPE_REASSOC_RESP:
2993                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
2994                 break;
2995         case IEEE80211_STYPE_DEAUTH:
2996                 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2997                 break;
2998         case IEEE80211_STYPE_DISASSOC:
2999                 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3000                 break;
3001         case IEEE80211_STYPE_ACTION:
3002                 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
3003                 break;
3004         }
3005
3006         kfree_skb(skb);
3007 }
3008
3009
3010 ieee80211_rx_result
3011 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3012                       struct ieee80211_rx_status *rx_status)
3013 {
3014         struct ieee80211_mgmt *mgmt;
3015         u16 fc;
3016
3017         if (skb->len < 2)
3018                 return RX_DROP_UNUSABLE;
3019
3020         mgmt = (struct ieee80211_mgmt *) skb->data;
3021         fc = le16_to_cpu(mgmt->frame_control);
3022
3023         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
3024                 return RX_CONTINUE;
3025
3026         if (skb->len < 24)
3027                 return RX_DROP_MONITOR;
3028
3029         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3030                 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3031                         ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3032                                                      skb->len, rx_status);
3033                         dev_kfree_skb(skb);
3034                         return RX_QUEUED;
3035                 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3036                         ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3037                                                  rx_status);
3038                         dev_kfree_skb(skb);
3039                         return RX_QUEUED;
3040                 }
3041         }
3042         return RX_CONTINUE;
3043 }
3044
3045
3046 static int ieee80211_sta_active_ibss(struct net_device *dev)
3047 {
3048         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3049         int active = 0;
3050         struct sta_info *sta;
3051         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3052
3053         rcu_read_lock();
3054
3055         list_for_each_entry_rcu(sta, &local->sta_list, list) {
3056                 if (sta->sdata == sdata &&
3057                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3058                                jiffies)) {
3059                         active++;
3060                         break;
3061                 }
3062         }
3063
3064         rcu_read_unlock();
3065
3066         return active;
3067 }
3068
3069
3070 static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
3071 {
3072         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3073         struct sta_info *sta, *tmp;
3074         LIST_HEAD(tmp_list);
3075         DECLARE_MAC_BUF(mac);
3076         unsigned long flags;
3077
3078         spin_lock_irqsave(&local->sta_lock, flags);
3079         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3080                 if (time_after(jiffies, sta->last_rx + exp_time)) {
3081                         printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3082                                dev->name, print_mac(mac, sta->addr));
3083                         sta_info_unlink(&sta);
3084                         if (sta)
3085                                 list_add(&sta->list, &tmp_list);
3086                 }
3087         spin_unlock_irqrestore(&local->sta_lock, flags);
3088
3089         synchronize_rcu();
3090
3091         rtnl_lock();
3092         list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3093                 sta_info_destroy(sta);
3094         rtnl_unlock();
3095 }
3096
3097
3098 static void ieee80211_sta_merge_ibss(struct net_device *dev,
3099                                      struct ieee80211_if_sta *ifsta)
3100 {
3101         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3102
3103         ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
3104         if (ieee80211_sta_active_ibss(dev))
3105                 return;
3106
3107         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3108                "IBSS networks with same SSID (merge)\n", dev->name);
3109         ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3110 }
3111
3112
3113 #ifdef CONFIG_MAC80211_MESH
3114 static void ieee80211_mesh_housekeeping(struct net_device *dev,
3115                            struct ieee80211_if_sta *ifsta)
3116 {
3117         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3118         bool free_plinks;
3119
3120         ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3121         mesh_path_expire(dev);
3122
3123         free_plinks = mesh_plink_availables(sdata);
3124         if (free_plinks != sdata->u.sta.accepting_plinks)
3125                 ieee80211_if_config_beacon(dev);
3126
3127         mod_timer(&ifsta->timer, jiffies +
3128                         IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3129 }
3130
3131
3132 void ieee80211_start_mesh(struct net_device *dev)
3133 {
3134         struct ieee80211_if_sta *ifsta;
3135         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3136         ifsta = &sdata->u.sta;
3137         ifsta->state = IEEE80211_MESH_UP;
3138         ieee80211_sta_timer((unsigned long)sdata);
3139 }
3140 #endif
3141
3142
3143 void ieee80211_sta_timer(unsigned long data)
3144 {
3145         struct ieee80211_sub_if_data *sdata =
3146                 (struct ieee80211_sub_if_data *) data;
3147         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3148         struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3149
3150         set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3151         queue_work(local->hw.workqueue, &ifsta->work);
3152 }
3153
3154 void ieee80211_sta_work(struct work_struct *work)
3155 {
3156         struct ieee80211_sub_if_data *sdata =
3157                 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3158         struct net_device *dev = sdata->dev;
3159         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3160         struct ieee80211_if_sta *ifsta;
3161         struct sk_buff *skb;
3162
3163         if (!netif_running(dev))
3164                 return;
3165
3166         if (local->sta_sw_scanning || local->sta_hw_scanning)
3167                 return;
3168
3169         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3170             sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3171             sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
3172                 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
3173                        "(type=%d)\n", dev->name, sdata->vif.type);
3174                 return;
3175         }
3176         ifsta = &sdata->u.sta;
3177
3178         while ((skb = skb_dequeue(&ifsta->skb_queue)))
3179                 ieee80211_sta_rx_queued_mgmt(dev, skb);
3180
3181 #ifdef CONFIG_MAC80211_MESH
3182         if (ifsta->preq_queue_len &&
3183             time_after(jiffies,
3184                        ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3185                 mesh_path_start_discovery(dev);
3186 #endif
3187
3188         if (ifsta->state != IEEE80211_AUTHENTICATE &&
3189             ifsta->state != IEEE80211_ASSOCIATE &&
3190             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3191                 if (ifsta->scan_ssid_len)
3192                         ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3193                 else
3194                         ieee80211_sta_start_scan(dev, NULL, 0);
3195                 return;
3196         }
3197
3198         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3199                 if (ieee80211_sta_config_auth(dev, ifsta))
3200                         return;
3201                 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3202         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3203                 return;
3204
3205         switch (ifsta->state) {
3206         case IEEE80211_DISABLED:
3207                 break;
3208         case IEEE80211_AUTHENTICATE:
3209                 ieee80211_authenticate(dev, ifsta);
3210                 break;
3211         case IEEE80211_ASSOCIATE:
3212                 ieee80211_associate(dev, ifsta);
3213                 break;
3214         case IEEE80211_ASSOCIATED:
3215                 ieee80211_associated(dev, ifsta);
3216                 break;
3217         case IEEE80211_IBSS_SEARCH:
3218                 ieee80211_sta_find_ibss(dev, ifsta);
3219                 break;
3220         case IEEE80211_IBSS_JOINED:
3221                 ieee80211_sta_merge_ibss(dev, ifsta);
3222                 break;
3223 #ifdef CONFIG_MAC80211_MESH
3224         case IEEE80211_MESH_UP:
3225                 ieee80211_mesh_housekeeping(dev, ifsta);
3226                 break;
3227 #endif
3228         default:
3229                 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3230                        ifsta->state);
3231                 break;
3232         }
3233
3234         if (ieee80211_privacy_mismatch(dev, ifsta)) {
3235                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3236                        "mixed-cell disabled - disassociate\n", dev->name);
3237
3238                 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3239                 ieee80211_set_disassoc(dev, ifsta, 0);
3240         }
3241 }
3242
3243
3244 static void ieee80211_sta_reset_auth(struct net_device *dev,
3245                                      struct ieee80211_if_sta *ifsta)
3246 {
3247         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3248
3249         if (local->ops->reset_tsf) {
3250                 /* Reset own TSF to allow time synchronization work. */
3251                 local->ops->reset_tsf(local_to_hw(local));
3252         }
3253
3254         ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3255
3256
3257         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3258                 ifsta->auth_alg = WLAN_AUTH_OPEN;
3259         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3260                 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3261         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3262                 ifsta->auth_alg = WLAN_AUTH_LEAP;
3263         else
3264                 ifsta->auth_alg = WLAN_AUTH_OPEN;
3265         printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3266                ifsta->auth_alg);
3267         ifsta->auth_transaction = -1;
3268         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3269         ifsta->auth_tries = ifsta->assoc_tries = 0;
3270         netif_carrier_off(dev);
3271 }
3272
3273
3274 void ieee80211_sta_req_auth(struct net_device *dev,
3275                             struct ieee80211_if_sta *ifsta)
3276 {
3277         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3278         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3279
3280         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3281                 return;
3282
3283         if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3284                                 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3285             (ifsta->flags & (IEEE80211_STA_SSID_SET |
3286                                 IEEE80211_STA_AUTO_SSID_SEL))) {
3287                 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3288                 queue_work(local->hw.workqueue, &ifsta->work);
3289         }
3290 }
3291
3292 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3293                                     const char *ssid, int ssid_len)
3294 {
3295         int tmp, hidden_ssid;
3296
3297         if (ssid_len == ifsta->ssid_len &&
3298             !memcmp(ifsta->ssid, ssid, ssid_len))
3299                 return 1;
3300
3301         if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3302                 return 0;
3303
3304         hidden_ssid = 1;
3305         tmp = ssid_len;
3306         while (tmp--) {
3307                 if (ssid[tmp] != '\0') {
3308                         hidden_ssid = 0;
3309                         break;
3310                 }
3311         }
3312
3313         if (hidden_ssid && ifsta->ssid_len == ssid_len)
3314                 return 1;
3315
3316         if (ssid_len == 1 && ssid[0] == ' ')
3317                 return 1;
3318
3319         return 0;
3320 }
3321
3322 static int ieee80211_sta_config_auth(struct net_device *dev,
3323                                      struct ieee80211_if_sta *ifsta)
3324 {
3325         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3326         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3327         struct ieee80211_sta_bss *bss, *selected = NULL;
3328         int top_rssi = 0, freq;
3329
3330         if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3331             IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
3332                 ifsta->state = IEEE80211_AUTHENTICATE;
3333                 ieee80211_sta_reset_auth(dev, ifsta);
3334                 return 0;
3335         }
3336
3337         spin_lock_bh(&local->sta_bss_lock);
3338         freq = local->oper_channel->center_freq;
3339         list_for_each_entry(bss, &local->sta_bss_list, list) {
3340                 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3341                         continue;
3342
3343                 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3344                     !!sdata->default_key)
3345                         continue;
3346
3347                 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3348                     bss->freq != freq)
3349                         continue;
3350
3351                 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
3352                     memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3353                         continue;
3354
3355                 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
3356                     !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3357                         continue;
3358
3359                 if (!selected || top_rssi < bss->rssi) {
3360                         selected = bss;
3361                         top_rssi = bss->rssi;
3362                 }
3363         }
3364         if (selected)
3365                 atomic_inc(&selected->users);
3366         spin_unlock_bh(&local->sta_bss_lock);
3367
3368         if (selected) {
3369                 ieee80211_set_freq(local, selected->freq);
3370                 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
3371                         ieee80211_sta_set_ssid(dev, selected->ssid,
3372                                                selected->ssid_len);
3373                 ieee80211_sta_set_bssid(dev, selected->bssid);
3374                 ieee80211_sta_def_wmm_params(dev, selected, 0);
3375                 ieee80211_rx_bss_put(dev, selected);
3376                 ifsta->state = IEEE80211_AUTHENTICATE;
3377                 ieee80211_sta_reset_auth(dev, ifsta);
3378                 return 0;
3379         } else {
3380                 if (ifsta->state != IEEE80211_AUTHENTICATE) {
3381                         if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
3382                                 ieee80211_sta_start_scan(dev, NULL, 0);
3383                         else
3384                                 ieee80211_sta_start_scan(dev, ifsta->ssid,
3385                                                          ifsta->ssid_len);
3386                         ifsta->state = IEEE80211_AUTHENTICATE;
3387                         set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3388                 } else
3389                         ifsta->state = IEEE80211_DISABLED;
3390         }
3391         return -1;
3392 }
3393
3394
3395 static int ieee80211_sta_create_ibss(struct net_device *dev,
3396                                      struct ieee80211_if_sta *ifsta)
3397 {
3398         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3399         struct ieee80211_sta_bss *bss;
3400         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3401         struct ieee80211_supported_band *sband;
3402         u8 bssid[ETH_ALEN], *pos;
3403         int i;
3404         DECLARE_MAC_BUF(mac);
3405
3406 #if 0
3407         /* Easier testing, use fixed BSSID. */
3408         memset(bssid, 0xfe, ETH_ALEN);
3409 #else
3410         /* Generate random, not broadcast, locally administered BSSID. Mix in
3411          * own MAC address to make sure that devices that do not have proper
3412          * random number generator get different BSSID. */
3413         get_random_bytes(bssid, ETH_ALEN);
3414         for (i = 0; i < ETH_ALEN; i++)
3415                 bssid[i] ^= dev->dev_addr[i];
3416         bssid[0] &= ~0x01;
3417         bssid[0] |= 0x02;
3418 #endif
3419
3420         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3421                dev->name, print_mac(mac, bssid));
3422
3423         bss = ieee80211_rx_bss_add(dev, bssid,
3424                                    local->hw.conf.channel->center_freq,
3425                                    sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3426         if (!bss)
3427                 return -ENOMEM;
3428
3429         bss->band = local->hw.conf.channel->band;
3430         sband = local->hw.wiphy->bands[bss->band];
3431
3432         if (local->hw.conf.beacon_int == 0)
3433                 local->hw.conf.beacon_int = 10000;
3434         bss->beacon_int = local->hw.conf.beacon_int;
3435         bss->last_update = jiffies;
3436         bss->capability = WLAN_CAPABILITY_IBSS;
3437         if (sdata->default_key) {
3438                 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3439         } else
3440                 sdata->drop_unencrypted = 0;
3441         bss->supp_rates_len = sband->n_bitrates;
3442         pos = bss->supp_rates;
3443         for (i = 0; i < sband->n_bitrates; i++) {
3444                 int rate = sband->bitrates[i].bitrate;
3445                 *pos++ = (u8) (rate / 5);
3446         }
3447
3448         return ieee80211_sta_join_ibss(dev, ifsta, bss);
3449 }
3450
3451
3452 static int ieee80211_sta_find_ibss(struct net_device *dev,
3453                                    struct ieee80211_if_sta *ifsta)
3454 {
3455         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3456         struct ieee80211_sta_bss *bss;
3457         int found = 0;
3458         u8 bssid[ETH_ALEN];
3459         int active_ibss;
3460         DECLARE_MAC_BUF(mac);
3461         DECLARE_MAC_BUF(mac2);
3462
3463         if (ifsta->ssid_len == 0)
3464                 return -EINVAL;
3465
3466         active_ibss = ieee80211_sta_active_ibss(dev);
3467 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3468         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3469                dev->name, active_ibss);
3470 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3471         spin_lock_bh(&local->sta_bss_lock);
3472         list_for_each_entry(bss, &local->sta_bss_list, list) {
3473                 if (ifsta->ssid_len != bss->ssid_len ||
3474                     memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3475                     || !(bss->capability & WLAN_CAPABILITY_IBSS))
3476                         continue;
3477 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3478                 printk(KERN_DEBUG "   bssid=%s found\n",
3479                        print_mac(mac, bss->bssid));
3480 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3481                 memcpy(bssid, bss->bssid, ETH_ALEN);
3482                 found = 1;
3483                 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3484                         break;
3485         }
3486         spin_unlock_bh(&local->sta_bss_lock);
3487
3488 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3489         printk(KERN_DEBUG "   sta_find_ibss: selected %s current "
3490                "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
3491 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3492         if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
3493             (bss = ieee80211_rx_bss_get(dev, bssid,
3494                                         local->hw.conf.channel->center_freq,
3495                                         ifsta->ssid, ifsta->ssid_len))) {
3496                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3497                        " based on configured SSID\n",
3498                        dev->name, print_mac(mac, bssid));
3499                 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3500         }
3501 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3502         printk(KERN_DEBUG "   did not try to join ibss\n");
3503 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3504
3505         /* Selected IBSS not found in current scan results - try to scan */
3506         if (ifsta->state == IEEE80211_IBSS_JOINED &&
3507             !ieee80211_sta_active_ibss(dev)) {
3508                 mod_timer(&ifsta->timer, jiffies +
3509                                       IEEE80211_IBSS_MERGE_INTERVAL);
3510         } else if (time_after(jiffies, local->last_scan_completed +
3511                               IEEE80211_SCAN_INTERVAL)) {
3512                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3513                        "join\n", dev->name);
3514                 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3515                                               ifsta->ssid_len);
3516         } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3517                 int interval = IEEE80211_SCAN_INTERVAL;
3518
3519                 if (time_after(jiffies, ifsta->ibss_join_req +
3520                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
3521                         if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3522                             (!(local->oper_channel->flags &
3523                                         IEEE80211_CHAN_NO_IBSS)))
3524                                 return ieee80211_sta_create_ibss(dev, ifsta);
3525                         if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3526                                 printk(KERN_DEBUG "%s: IBSS not allowed on"
3527                                        " %d MHz\n", dev->name,
3528                                        local->hw.conf.channel->center_freq);
3529                         }
3530
3531                         /* No IBSS found - decrease scan interval and continue
3532                          * scanning. */
3533                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
3534                 }
3535
3536                 ifsta->state = IEEE80211_IBSS_SEARCH;
3537                 mod_timer(&ifsta->timer, jiffies + interval);
3538                 return 0;
3539         }
3540
3541         return 0;
3542 }
3543
3544
3545 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3546 {
3547         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3548         struct ieee80211_if_sta *ifsta;
3549
3550         if (len > IEEE80211_MAX_SSID_LEN)
3551                 return -EINVAL;
3552
3553         ifsta = &sdata->u.sta;
3554
3555         if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
3556                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3557         memcpy(ifsta->ssid, ssid, len);
3558         memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3559         ifsta->ssid_len = len;
3560
3561         if (len)
3562                 ifsta->flags |= IEEE80211_STA_SSID_SET;
3563         else
3564                 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3565         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3566             !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3567                 ifsta->ibss_join_req = jiffies;
3568                 ifsta->state = IEEE80211_IBSS_SEARCH;
3569                 return ieee80211_sta_find_ibss(dev, ifsta);
3570         }
3571         return 0;
3572 }
3573
3574
3575 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3576 {
3577         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3578         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3579         memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3580         *len = ifsta->ssid_len;
3581         return 0;
3582 }
3583
3584
3585 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3586 {
3587         struct ieee80211_sub_if_data *sdata;
3588         struct ieee80211_if_sta *ifsta;
3589         int res;
3590
3591         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3592         ifsta = &sdata->u.sta;
3593
3594         if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3595                 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3596                 res = ieee80211_if_config(dev);
3597                 if (res) {
3598                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3599                                "the low-level driver\n", dev->name);
3600                         return res;
3601                 }
3602         }
3603
3604         if (is_valid_ether_addr(bssid))
3605                 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3606         else
3607                 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3608
3609         return 0;
3610 }
3611
3612
3613 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3614                                     struct ieee80211_sub_if_data *sdata,
3615                                     int powersave)
3616 {
3617         struct sk_buff *skb;
3618         struct ieee80211_hdr *nullfunc;
3619         u16 fc;
3620
3621         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3622         if (!skb) {
3623                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3624                        "frame\n", sdata->dev->name);
3625                 return;
3626         }
3627         skb_reserve(skb, local->hw.extra_tx_headroom);
3628
3629         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3630         memset(nullfunc, 0, 24);
3631         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3632              IEEE80211_FCTL_TODS;
3633         if (powersave)
3634                 fc |= IEEE80211_FCTL_PM;
3635         nullfunc->frame_control = cpu_to_le16(fc);
3636         memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3637         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3638         memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3639
3640         ieee80211_sta_tx(sdata->dev, skb, 0);
3641 }
3642
3643
3644 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3645 {
3646         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3647             ieee80211_vif_is_mesh(&sdata->vif))
3648                 ieee80211_sta_timer((unsigned long)sdata);
3649 }
3650
3651 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3652 {
3653         struct ieee80211_local *local = hw_to_local(hw);
3654         struct net_device *dev = local->scan_dev;
3655         struct ieee80211_sub_if_data *sdata;
3656         union iwreq_data wrqu;
3657
3658         local->last_scan_completed = jiffies;
3659         memset(&wrqu, 0, sizeof(wrqu));
3660         wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3661
3662         if (local->sta_hw_scanning) {
3663                 local->sta_hw_scanning = 0;
3664                 if (ieee80211_hw_config(local))
3665                         printk(KERN_DEBUG "%s: failed to restore operational "
3666                                "channel after scan\n", dev->name);
3667                 /* Restart STA timer for HW scan case */
3668                 rcu_read_lock();
3669                 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3670                         ieee80211_restart_sta_timer(sdata);
3671                 rcu_read_unlock();
3672
3673                 goto done;
3674         }
3675
3676         local->sta_sw_scanning = 0;
3677         if (ieee80211_hw_config(local))
3678                 printk(KERN_DEBUG "%s: failed to restore operational "
3679                        "channel after scan\n", dev->name);
3680
3681
3682         netif_tx_lock_bh(local->mdev);
3683         local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3684         local->ops->configure_filter(local_to_hw(local),
3685                                      FIF_BCN_PRBRESP_PROMISC,
3686                                      &local->filter_flags,
3687                                      local->mdev->mc_count,
3688                                      local->mdev->mc_list);
3689
3690         netif_tx_unlock_bh(local->mdev);
3691
3692         rcu_read_lock();
3693         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3694
3695                 /* No need to wake the master device. */
3696                 if (sdata->dev == local->mdev)
3697                         continue;
3698
3699                 /* Tell AP we're back */
3700                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3701                     sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3702                         ieee80211_send_nullfunc(local, sdata, 0);
3703
3704                 ieee80211_restart_sta_timer(sdata);
3705
3706                 netif_wake_queue(sdata->dev);
3707         }
3708         rcu_read_unlock();
3709
3710 done:
3711         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3712         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3713                 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3714                 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3715                     (!ifsta->state == IEEE80211_IBSS_JOINED &&
3716                     !ieee80211_sta_active_ibss(dev)))
3717                         ieee80211_sta_find_ibss(dev, ifsta);
3718         }
3719 }
3720 EXPORT_SYMBOL(ieee80211_scan_completed);
3721
3722 void ieee80211_sta_scan_work(struct work_struct *work)
3723 {
3724         struct ieee80211_local *local =
3725                 container_of(work, struct ieee80211_local, scan_work.work);
3726         struct net_device *dev = local->scan_dev;
3727         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3728         struct ieee80211_supported_band *sband;
3729         struct ieee80211_channel *chan;
3730         int skip;
3731         unsigned long next_delay = 0;
3732
3733         if (!local->sta_sw_scanning)
3734                 return;
3735
3736         switch (local->scan_state) {
3737         case SCAN_SET_CHANNEL:
3738                 /*
3739                  * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3740                  * after we successfully scanned the last channel of the last
3741                  * band (and the last band is supported by the hw)
3742                  */
3743                 if (local->scan_band < IEEE80211_NUM_BANDS)
3744                         sband = local->hw.wiphy->bands[local->scan_band];
3745                 else
3746                         sband = NULL;
3747
3748                 /*
3749                  * If we are at an unsupported band and have more bands
3750                  * left to scan, advance to the next supported one.
3751                  */
3752                 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3753                         local->scan_band++;
3754                         sband = local->hw.wiphy->bands[local->scan_band];
3755                         local->scan_channel_idx = 0;
3756                 }
3757
3758                 /* if no more bands/channels left, complete scan */
3759                 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3760                         ieee80211_scan_completed(local_to_hw(local));
3761                         return;
3762                 }
3763                 skip = 0;
3764                 chan = &sband->channels[local->scan_channel_idx];
3765
3766                 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3767                     (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3768                      chan->flags & IEEE80211_CHAN_NO_IBSS))
3769                         skip = 1;
3770
3771                 if (!skip) {
3772                         local->scan_channel = chan;
3773                         if (ieee80211_hw_config(local)) {
3774                                 printk(KERN_DEBUG "%s: failed to set freq to "
3775                                        "%d MHz for scan\n", dev->name,
3776                                        chan->center_freq);
3777                                 skip = 1;
3778                         }
3779                 }
3780
3781                 /* advance state machine to next channel/band */
3782                 local->scan_channel_idx++;
3783                 if (local->scan_channel_idx >= sband->n_channels) {
3784                         /*
3785                          * scan_band may end up == IEEE80211_NUM_BANDS, but
3786                          * we'll catch that case above and complete the scan
3787                          * if that is the case.
3788                          */
3789                         local->scan_band++;
3790                         local->scan_channel_idx = 0;
3791                 }
3792
3793                 if (skip)
3794                         break;
3795
3796                 next_delay = IEEE80211_PROBE_DELAY +
3797                              usecs_to_jiffies(local->hw.channel_change_time);
3798                 local->scan_state = SCAN_SEND_PROBE;
3799                 break;
3800         case SCAN_SEND_PROBE:
3801                 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3802                 local->scan_state = SCAN_SET_CHANNEL;
3803
3804                 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3805                         break;
3806                 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3807                                          local->scan_ssid_len);
3808                 next_delay = IEEE80211_CHANNEL_TIME;
3809                 break;
3810         }
3811
3812         if (local->sta_sw_scanning)
3813                 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3814                                    next_delay);
3815 }
3816
3817
3818 static int ieee80211_sta_start_scan(struct net_device *dev,
3819                                     u8 *ssid, size_t ssid_len)
3820 {
3821         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3822         struct ieee80211_sub_if_data *sdata;
3823
3824         if (ssid_len > IEEE80211_MAX_SSID_LEN)
3825                 return -EINVAL;
3826
3827         /* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
3828          * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3829          * BSSID: MACAddress
3830          * SSID
3831          * ScanType: ACTIVE, PASSIVE
3832          * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3833          *    a Probe frame during active scanning
3834          * ChannelList
3835          * MinChannelTime (>= ProbeDelay), in TU
3836          * MaxChannelTime: (>= MinChannelTime), in TU
3837          */
3838
3839          /* MLME-SCAN.confirm
3840           * BSSDescriptionSet
3841           * ResultCode: SUCCESS, INVALID_PARAMETERS
3842          */
3843
3844         if (local->sta_sw_scanning || local->sta_hw_scanning) {
3845                 if (local->scan_dev == dev)
3846                         return 0;
3847                 return -EBUSY;
3848         }
3849
3850         if (local->ops->hw_scan) {
3851                 int rc = local->ops->hw_scan(local_to_hw(local),
3852                                              ssid, ssid_len);
3853                 if (!rc) {
3854                         local->sta_hw_scanning = 1;
3855                         local->scan_dev = dev;
3856                 }
3857                 return rc;
3858         }
3859
3860         local->sta_sw_scanning = 1;
3861
3862         rcu_read_lock();
3863         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3864
3865                 /* Don't stop the master interface, otherwise we can't transmit
3866                  * probes! */
3867                 if (sdata->dev == local->mdev)
3868                         continue;
3869
3870                 netif_stop_queue(sdata->dev);
3871                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3872                     (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
3873                         ieee80211_send_nullfunc(local, sdata, 1);
3874         }
3875         rcu_read_unlock();
3876
3877         if (ssid) {
3878                 local->scan_ssid_len = ssid_len;
3879                 memcpy(local->scan_ssid, ssid, ssid_len);
3880         } else
3881                 local->scan_ssid_len = 0;
3882         local->scan_state = SCAN_SET_CHANNEL;
3883         local->scan_channel_idx = 0;
3884         local->scan_band = IEEE80211_BAND_2GHZ;
3885         local->scan_dev = dev;
3886
3887         netif_tx_lock_bh(local->mdev);
3888         local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3889         local->ops->configure_filter(local_to_hw(local),
3890                                      FIF_BCN_PRBRESP_PROMISC,
3891                                      &local->filter_flags,
3892                                      local->mdev->mc_count,
3893                                      local->mdev->mc_list);
3894         netif_tx_unlock_bh(local->mdev);
3895
3896         /* TODO: start scan as soon as all nullfunc frames are ACKed */
3897         queue_delayed_work(local->hw.workqueue, &local->scan_work,
3898                            IEEE80211_CHANNEL_TIME);
3899
3900         return 0;
3901 }
3902
3903
3904 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3905 {
3906         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3907         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3908         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3909
3910         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3911                 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3912
3913         if (local->sta_sw_scanning || local->sta_hw_scanning) {
3914                 if (local->scan_dev == dev)
3915                         return 0;
3916                 return -EBUSY;
3917         }
3918
3919         ifsta->scan_ssid_len = ssid_len;
3920         if (ssid_len)
3921                 memcpy(ifsta->scan_ssid, ssid, ssid_len);
3922         set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3923         queue_work(local->hw.workqueue, &ifsta->work);
3924         return 0;
3925 }
3926
3927 static char *
3928 ieee80211_sta_scan_result(struct net_device *dev,
3929                           struct ieee80211_sta_bss *bss,
3930                           char *current_ev, char *end_buf)
3931 {
3932         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3933         struct iw_event iwe;
3934
3935         if (time_after(jiffies,
3936                        bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3937                 return current_ev;
3938
3939         memset(&iwe, 0, sizeof(iwe));
3940         iwe.cmd = SIOCGIWAP;
3941         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3942         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3943         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3944                                           IW_EV_ADDR_LEN);
3945
3946         memset(&iwe, 0, sizeof(iwe));
3947         iwe.cmd = SIOCGIWESSID;
3948         if (bss_mesh_cfg(bss)) {
3949                 iwe.u.data.length = bss_mesh_id_len(bss);
3950                 iwe.u.data.flags = 1;
3951                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3952                                                   bss_mesh_id(bss));
3953         } else {
3954                 iwe.u.data.length = bss->ssid_len;
3955                 iwe.u.data.flags = 1;
3956                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3957                                                   bss->ssid);
3958         }
3959
3960         if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3961             || bss_mesh_cfg(bss)) {
3962                 memset(&iwe, 0, sizeof(iwe));
3963                 iwe.cmd = SIOCGIWMODE;
3964                 if (bss_mesh_cfg(bss))
3965                         iwe.u.mode = IW_MODE_MESH;
3966                 else if (bss->capability & WLAN_CAPABILITY_ESS)
3967                         iwe.u.mode = IW_MODE_MASTER;
3968                 else
3969                         iwe.u.mode = IW_MODE_ADHOC;
3970                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3971                                                   IW_EV_UINT_LEN);
3972         }
3973
3974         memset(&iwe, 0, sizeof(iwe));
3975         iwe.cmd = SIOCGIWFREQ;
3976         iwe.u.freq.m = bss->freq;
3977         iwe.u.freq.e = 6;
3978         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3979                                           IW_EV_FREQ_LEN);
3980
3981         memset(&iwe, 0, sizeof(iwe));
3982         iwe.cmd = SIOCGIWFREQ;
3983         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3984         iwe.u.freq.e = 0;
3985         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3986                                           IW_EV_FREQ_LEN);
3987
3988         memset(&iwe, 0, sizeof(iwe));
3989         iwe.cmd = IWEVQUAL;
3990         iwe.u.qual.qual = bss->signal;
3991         iwe.u.qual.level = bss->rssi;
3992         iwe.u.qual.noise = bss->noise;
3993         iwe.u.qual.updated = local->wstats_flags;
3994         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3995                                           IW_EV_QUAL_LEN);
3996
3997         memset(&iwe, 0, sizeof(iwe));
3998         iwe.cmd = SIOCGIWENCODE;
3999         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4000                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4001         else
4002                 iwe.u.data.flags = IW_ENCODE_DISABLED;
4003         iwe.u.data.length = 0;
4004         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4005
4006         if (bss && bss->wpa_ie) {
4007                 memset(&iwe, 0, sizeof(iwe));
4008                 iwe.cmd = IWEVGENIE;
4009                 iwe.u.data.length = bss->wpa_ie_len;
4010                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4011                                                   bss->wpa_ie);
4012         }
4013
4014         if (bss && bss->rsn_ie) {
4015                 memset(&iwe, 0, sizeof(iwe));
4016                 iwe.cmd = IWEVGENIE;
4017                 iwe.u.data.length = bss->rsn_ie_len;
4018                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4019                                                   bss->rsn_ie);
4020         }
4021
4022         if (bss && bss->supp_rates_len > 0) {
4023                 /* display all supported rates in readable format */
4024                 char *p = current_ev + IW_EV_LCP_LEN;
4025                 int i;
4026
4027                 memset(&iwe, 0, sizeof(iwe));
4028                 iwe.cmd = SIOCGIWRATE;
4029                 /* Those two flags are ignored... */
4030                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4031
4032                 for (i = 0; i < bss->supp_rates_len; i++) {
4033                         iwe.u.bitrate.value = ((bss->supp_rates[i] &
4034                                                         0x7f) * 500000);
4035                         p = iwe_stream_add_value(current_ev, p,
4036                                         end_buf, &iwe, IW_EV_PARAM_LEN);
4037                 }
4038                 current_ev = p;
4039         }
4040
4041         if (bss) {
4042                 char *buf;
4043                 buf = kmalloc(30, GFP_ATOMIC);
4044                 if (buf) {
4045                         memset(&iwe, 0, sizeof(iwe));
4046                         iwe.cmd = IWEVCUSTOM;
4047                         sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4048                         iwe.u.data.length = strlen(buf);
4049                         current_ev = iwe_stream_add_point(current_ev, end_buf,
4050                                                           &iwe, buf);
4051                         kfree(buf);
4052                 }
4053         }
4054
4055         if (bss_mesh_cfg(bss)) {
4056                 char *buf;
4057                 u8 *cfg = bss_mesh_cfg(bss);
4058                 buf = kmalloc(50, GFP_ATOMIC);
4059                 if (buf) {
4060                         memset(&iwe, 0, sizeof(iwe));
4061                         iwe.cmd = IWEVCUSTOM;
4062                         sprintf(buf, "Mesh network (version %d)", cfg[0]);
4063                         iwe.u.data.length = strlen(buf);
4064                         current_ev = iwe_stream_add_point(current_ev, end_buf,
4065                                                           &iwe, buf);
4066                         sprintf(buf, "Path Selection Protocol ID: "
4067                                 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4068                                                         cfg[4]);
4069                         iwe.u.data.length = strlen(buf);
4070                         current_ev = iwe_stream_add_point(current_ev, end_buf,
4071                                                           &iwe, buf);
4072                         sprintf(buf, "Path Selection Metric ID: "
4073                                 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4074                                                         cfg[8]);
4075                         iwe.u.data.length = strlen(buf);
4076                         current_ev = iwe_stream_add_point(current_ev, end_buf,
4077                                                           &iwe, buf);
4078                         sprintf(buf, "Congestion Control Mode ID: "
4079                                 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4080                                                         cfg[11], cfg[12]);
4081                         iwe.u.data.length = strlen(buf);
4082                         current_ev = iwe_stream_add_point(current_ev, end_buf,
4083                                                           &iwe, buf);
4084                         sprintf(buf, "Channel Precedence: "
4085                                 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4086                                                         cfg[15], cfg[16]);
4087                         iwe.u.data.length = strlen(buf);
4088                         current_ev = iwe_stream_add_point(current_ev, end_buf,
4089                                                           &iwe, buf);
4090                         kfree(buf);
4091                 }
4092         }
4093
4094         return current_ev;
4095 }
4096
4097
4098 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4099 {
4100         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4101         char *current_ev = buf;
4102         char *end_buf = buf + len;
4103         struct ieee80211_sta_bss *bss;
4104
4105         spin_lock_bh(&local->sta_bss_lock);
4106         list_for_each_entry(bss, &local->sta_bss_list, list) {
4107                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4108                         spin_unlock_bh(&local->sta_bss_lock);
4109                         return -E2BIG;
4110                 }
4111                 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4112                                                        end_buf);
4113         }
4114         spin_unlock_bh(&local->sta_bss_lock);
4115         return current_ev - buf;
4116 }
4117
4118
4119 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4120 {
4121         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4122         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4123         kfree(ifsta->extra_ie);
4124         if (len == 0) {
4125                 ifsta->extra_ie = NULL;
4126                 ifsta->extra_ie_len = 0;
4127                 return 0;
4128         }
4129         ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4130         if (!ifsta->extra_ie) {
4131                 ifsta->extra_ie_len = 0;
4132                 return -ENOMEM;
4133         }
4134         memcpy(ifsta->extra_ie, ie, len);
4135         ifsta->extra_ie_len = len;
4136         return 0;
4137 }
4138
4139
4140 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4141                                          struct sk_buff *skb, u8 *bssid,
4142                                          u8 *addr)
4143 {
4144         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4145         struct sta_info *sta;
4146         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4147         DECLARE_MAC_BUF(mac);
4148
4149         /* TODO: Could consider removing the least recently used entry and
4150          * allow new one to be added. */
4151         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4152                 if (net_ratelimit()) {
4153                         printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4154                                "entry %s\n", dev->name, print_mac(mac, addr));
4155                 }
4156                 return NULL;
4157         }
4158
4159         printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4160                wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
4161
4162         sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4163         if (!sta)
4164                 return NULL;
4165
4166         sta->flags |= WLAN_STA_AUTHORIZED;
4167
4168         sta->supp_rates[local->hw.conf.channel->band] =
4169                 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
4170
4171         rate_control_rate_init(sta, local);
4172
4173         if (sta_info_insert(sta))
4174                 return NULL;
4175
4176         return sta;
4177 }
4178
4179
4180 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4181 {
4182         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4183         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4184
4185         printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4186                dev->name, reason);
4187
4188         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4189             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4190                 return -EINVAL;
4191
4192         ieee80211_send_deauth(dev, ifsta, reason);
4193         ieee80211_set_disassoc(dev, ifsta, 1);
4194         return 0;
4195 }
4196
4197
4198 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4199 {
4200         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4201         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4202
4203         printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4204                dev->name, reason);
4205
4206         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4207                 return -EINVAL;
4208
4209         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4210                 return -1;
4211
4212         ieee80211_send_disassoc(dev, ifsta, reason);
4213         ieee80211_set_disassoc(dev, ifsta, 0);
4214         return 0;
4215 }