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