b8bdba67ad17c5a540b14a1333a735f486f25543
[safe/jmp/linux-2.6] / drivers / net / wireless / libertas / assoc.c
1 /* Copyright (C) 2006, Red Hat, Inc. */
2
3 #include <linux/types.h>
4 #include <linux/etherdevice.h>
5 #include <linux/ieee80211.h>
6 #include <linux/if_arp.h>
7 #include <net/lib80211.h>
8
9 #include "assoc.h"
10 #include "decl.h"
11 #include "host.h"
12 #include "scan.h"
13 #include "cmd.h"
14
15 static const u8 bssid_any[ETH_ALEN]  __attribute__ ((aligned (2))) =
16         { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
17 static const u8 bssid_off[ETH_ALEN]  __attribute__ ((aligned (2))) =
18         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19
20 /* The firmware needs the following bits masked out of the beacon-derived
21  * capability field when associating/joining to a BSS:
22  *  9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
23  */
24 #define CAPINFO_MASK    (~(0xda00))
25
26 /**
27  * 802.11b/g supported bitrates (in 500Kb/s units)
28  */
29 u8 lbs_bg_rates[MAX_RATES] =
30     { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
31 0x00, 0x00 };
32
33
34 /**
35  *  @brief This function finds common rates between rates and card rates.
36  *
37  * It will fill common rates in rates as output if found.
38  *
39  * NOTE: Setting the MSB of the basic rates need to be taken
40  *   care, either before or after calling this function
41  *
42  *  @param priv     A pointer to struct lbs_private structure
43  *  @param rates       the buffer which keeps input and output
44  *  @param rates_size  the size of rates buffer; new size of buffer on return,
45  *                     which will be less than or equal to original rates_size
46  *
47  *  @return            0 on success, or -1 on error
48  */
49 static int get_common_rates(struct lbs_private *priv,
50         u8 *rates,
51         u16 *rates_size)
52 {
53         int i, j;
54         u8 intersection[MAX_RATES];
55         u16 intersection_size;
56         u16 num_rates = 0;
57
58         intersection_size = min_t(u16, *rates_size, ARRAY_SIZE(intersection));
59
60         /* Allow each rate from 'rates' that is supported by the hardware */
61         for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && lbs_bg_rates[i]; i++) {
62                 for (j = 0; j < intersection_size && rates[j]; j++) {
63                         if (rates[j] == lbs_bg_rates[i])
64                                 intersection[num_rates++] = rates[j];
65                 }
66         }
67
68         lbs_deb_hex(LBS_DEB_JOIN, "AP rates    ", rates, *rates_size);
69         lbs_deb_hex(LBS_DEB_JOIN, "card rates  ", lbs_bg_rates,
70                         ARRAY_SIZE(lbs_bg_rates));
71         lbs_deb_hex(LBS_DEB_JOIN, "common rates", intersection, num_rates);
72         lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
73
74         if (!priv->enablehwauto) {
75                 for (i = 0; i < num_rates; i++) {
76                         if (intersection[i] == priv->cur_rate)
77                                 goto done;
78                 }
79                 lbs_pr_alert("Previously set fixed data rate %#x isn't "
80                        "compatible with the network.\n", priv->cur_rate);
81                 return -1;
82         }
83
84 done:
85         memset(rates, 0, *rates_size);
86         *rates_size = num_rates;
87         memcpy(rates, intersection, num_rates);
88         return 0;
89 }
90
91
92 /**
93  *  @brief Sets the MSB on basic rates as the firmware requires
94  *
95  * Scan through an array and set the MSB for basic data rates.
96  *
97  *  @param rates     buffer of data rates
98  *  @param len       size of buffer
99  */
100 static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
101 {
102         int i;
103
104         for (i = 0; i < len; i++) {
105                 if (rates[i] == 0x02 || rates[i] == 0x04 ||
106                     rates[i] == 0x0b || rates[i] == 0x16)
107                         rates[i] |= 0x80;
108         }
109 }
110
111
112 static u8 iw_auth_to_ieee_auth(u8 auth)
113 {
114         if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
115                 return 0x00;
116         else if (auth == IW_AUTH_ALG_SHARED_KEY)
117                 return 0x01;
118         else if (auth == IW_AUTH_ALG_LEAP)
119                 return 0x80;
120
121         lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
122         return 0;
123 }
124
125 /**
126  *  @brief This function prepares the authenticate command.  AUTHENTICATE only
127  *  sets the authentication suite for future associations, as the firmware
128  *  handles authentication internally during the ASSOCIATE command.
129  *
130  *  @param priv      A pointer to struct lbs_private structure
131  *  @param bssid     The peer BSSID with which to authenticate
132  *  @param auth      The authentication mode to use (from wireless.h)
133  *
134  *  @return         0 or -1
135  */
136 static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
137 {
138         struct cmd_ds_802_11_authenticate cmd;
139         int ret = -1;
140
141         lbs_deb_enter(LBS_DEB_JOIN);
142
143         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
144         memcpy(cmd.bssid, bssid, ETH_ALEN);
145
146         cmd.authtype = iw_auth_to_ieee_auth(auth);
147
148         lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
149
150         ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
151
152         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
153         return ret;
154 }
155
156
157 static int lbs_assoc_post(struct lbs_private *priv,
158                           struct cmd_ds_802_11_associate_response *resp)
159 {
160         int ret = 0;
161         union iwreq_data wrqu;
162         struct bss_descriptor *bss;
163         u16 status_code;
164
165         lbs_deb_enter(LBS_DEB_ASSOC);
166
167         if (!priv->in_progress_assoc_req) {
168                 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
169                 ret = -1;
170                 goto done;
171         }
172         bss = &priv->in_progress_assoc_req->bss;
173
174         /*
175          * Older FW versions map the IEEE 802.11 Status Code in the association
176          * response to the following values returned in resp->statuscode:
177          *
178          *    IEEE Status Code                Marvell Status Code
179          *    0                       ->      0x0000 ASSOC_RESULT_SUCCESS
180          *    13                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
181          *    14                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
182          *    15                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
183          *    16                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
184          *    others                  ->      0x0003 ASSOC_RESULT_REFUSED
185          *
186          * Other response codes:
187          *    0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
188          *    0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
189          *                                    association response from the AP)
190          */
191
192         status_code = le16_to_cpu(resp->statuscode);
193         if (priv->fwrelease < 0x09000000) {
194                 switch (status_code) {
195                 case 0x00:
196                         break;
197                 case 0x01:
198                         lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
199                         break;
200                 case 0x02:
201                         lbs_deb_assoc("ASSOC_RESP: internal timer "
202                                 "expired while waiting for the AP\n");
203                         break;
204                 case 0x03:
205                         lbs_deb_assoc("ASSOC_RESP: association "
206                                 "refused by AP\n");
207                         break;
208                 case 0x04:
209                         lbs_deb_assoc("ASSOC_RESP: authentication "
210                                 "refused by AP\n");
211                         break;
212                 default:
213                         lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
214                                 " unknown\n", status_code);
215                         break;
216                 }
217         } else {
218                 /* v9+ returns the AP's association response */
219                 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
220         }
221
222         if (status_code) {
223                 lbs_mac_event_disconnected(priv);
224                 ret = -1;
225                 goto done;
226         }
227
228         lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
229                     (void *) (resp + sizeof (resp->hdr)),
230                     le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
231
232         /* Send a Media Connected event, according to the Spec */
233         priv->connect_status = LBS_CONNECTED;
234
235         /* Update current SSID and BSSID */
236         memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
237         priv->curbssparams.ssid_len = bss->ssid_len;
238         memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
239
240         priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
241         priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
242
243         memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
244         memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
245         priv->nextSNRNF = 0;
246         priv->numSNRNF = 0;
247
248         netif_carrier_on(priv->dev);
249         if (!priv->tx_pending_len)
250                 netif_wake_queue(priv->dev);
251
252         memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
253         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
254         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
255
256 done:
257         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
258         return ret;
259 }
260
261 /**
262  *  @brief This function prepares an association-class command.
263  *
264  *  @param priv      A pointer to struct lbs_private structure
265  *  @param assoc_req The association request describing the BSS to associate
266  *                   or reassociate with
267  *  @param command   The actual command, either CMD_802_11_ASSOCIATE or
268  *                   CMD_802_11_REASSOCIATE
269  *
270  *  @return         0 or -1
271  */
272 static int lbs_associate(struct lbs_private *priv,
273                          struct assoc_request *assoc_req,
274                          u16 command)
275 {
276         struct cmd_ds_802_11_associate cmd;
277         int ret = 0;
278         struct bss_descriptor *bss = &assoc_req->bss;
279         u8 *pos = &(cmd.iebuf[0]);
280         u16 tmpcap, tmplen, tmpauth;
281         struct mrvl_ie_ssid_param_set *ssid;
282         struct mrvl_ie_ds_param_set *ds;
283         struct mrvl_ie_cf_param_set *cf;
284         struct mrvl_ie_rates_param_set *rates;
285         struct mrvl_ie_rsn_param_set *rsn;
286         struct mrvl_ie_auth_type *auth;
287
288         lbs_deb_enter(LBS_DEB_ASSOC);
289
290         BUG_ON((command != CMD_802_11_ASSOCIATE) &&
291                 (command != CMD_802_11_REASSOCIATE));
292
293         memset(&cmd, 0, sizeof(cmd));
294         cmd.hdr.command = cpu_to_le16(command);
295
296         /* Fill in static fields */
297         memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
298         cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
299
300         /* Capability info */
301         tmpcap = (bss->capability & CAPINFO_MASK);
302         if (bss->mode == IW_MODE_INFRA)
303                 tmpcap |= WLAN_CAPABILITY_ESS;
304         cmd.capability = cpu_to_le16(tmpcap);
305         lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
306
307         /* SSID */
308         ssid = (struct mrvl_ie_ssid_param_set *) pos;
309         ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
310         tmplen = bss->ssid_len;
311         ssid->header.len = cpu_to_le16(tmplen);
312         memcpy(ssid->ssid, bss->ssid, tmplen);
313         pos += sizeof(ssid->header) + tmplen;
314
315         ds = (struct mrvl_ie_ds_param_set *) pos;
316         ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
317         ds->header.len = cpu_to_le16(1);
318         ds->channel = bss->phy.ds.channel;
319         pos += sizeof(ds->header) + 1;
320
321         cf = (struct mrvl_ie_cf_param_set *) pos;
322         cf->header.type = cpu_to_le16(TLV_TYPE_CF);
323         tmplen = sizeof(*cf) - sizeof (cf->header);
324         cf->header.len = cpu_to_le16(tmplen);
325         /* IE payload should be zeroed, firmware fills it in for us */
326         pos += sizeof(*cf);
327
328         rates = (struct mrvl_ie_rates_param_set *) pos;
329         rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
330         tmplen = min_t(u16, ARRAY_SIZE(bss->rates), MAX_RATES);
331         memcpy(&rates->rates, &bss->rates, tmplen);
332         if (get_common_rates(priv, rates->rates, &tmplen)) {
333                 ret = -1;
334                 goto done;
335         }
336         pos += sizeof(rates->header) + tmplen;
337         rates->header.len = cpu_to_le16(tmplen);
338         lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
339
340         /* Copy the infra. association rates into Current BSS state structure */
341         memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
342         memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
343
344         /* Set MSB on basic rates as the firmware requires, but _after_
345          * copying to current bss rates.
346          */
347         lbs_set_basic_rate_flags(rates->rates, tmplen);
348
349         /* Firmware v9+ indicate authentication suites as a TLV */
350         if (priv->fwrelease >= 0x09000000) {
351                 auth = (struct mrvl_ie_auth_type *) pos;
352                 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
353                 auth->header.len = cpu_to_le16(2);
354                 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
355                 auth->auth = cpu_to_le16(tmpauth);
356                 pos += sizeof(auth->header) + 2;
357
358                 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
359                         bss->bssid, priv->secinfo.auth_mode);
360         }
361
362         /* WPA/WPA2 IEs */
363         if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
364                 rsn = (struct mrvl_ie_rsn_param_set *) pos;
365                 /* WPA_IE or WPA2_IE */
366                 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
367                 tmplen = (u16) assoc_req->wpa_ie[1];
368                 rsn->header.len = cpu_to_le16(tmplen);
369                 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
370                 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
371                         sizeof(rsn->header) + tmplen);
372                 pos += sizeof(rsn->header) + tmplen;
373         }
374
375         cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
376                                    (u16)(pos - (u8 *) &cmd.iebuf));
377
378         /* update curbssparams */
379         priv->channel = bss->phy.ds.channel;
380
381         ret = lbs_cmd_with_response(priv, command, &cmd);
382         if (ret == 0) {
383                 ret = lbs_assoc_post(priv,
384                         (struct cmd_ds_802_11_associate_response *) &cmd);
385         }
386
387 done:
388         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
389         return ret;
390 }
391
392 /**
393  *  @brief Associate to a specific BSS discovered in a scan
394  *
395  *  @param priv      A pointer to struct lbs_private structure
396  *  @param assoc_req The association request describing the BSS to associate with
397  *
398  *  @return          0-success, otherwise fail
399  */
400 static int lbs_try_associate(struct lbs_private *priv,
401         struct assoc_request *assoc_req)
402 {
403         int ret;
404         u8 preamble = RADIO_PREAMBLE_LONG;
405
406         lbs_deb_enter(LBS_DEB_ASSOC);
407
408         /* FW v9 and higher indicate authentication suites as a TLV in the
409          * association command, not as a separate authentication command.
410          */
411         if (priv->fwrelease < 0x09000000) {
412                 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
413                                              priv->secinfo.auth_mode);
414                 if (ret)
415                         goto out;
416         }
417
418         /* Use short preamble only when both the BSS and firmware support it */
419         if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
420             (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
421                 preamble = RADIO_PREAMBLE_SHORT;
422
423         ret = lbs_set_radio(priv, preamble, 1);
424         if (ret)
425                 goto out;
426
427         ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
428
429 out:
430         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
431         return ret;
432 }
433
434 static int lbs_adhoc_post(struct lbs_private *priv,
435                           struct cmd_ds_802_11_ad_hoc_result *resp)
436 {
437         int ret = 0;
438         u16 command = le16_to_cpu(resp->hdr.command);
439         u16 result = le16_to_cpu(resp->hdr.result);
440         union iwreq_data wrqu;
441         struct bss_descriptor *bss;
442         DECLARE_SSID_BUF(ssid);
443
444         lbs_deb_enter(LBS_DEB_JOIN);
445
446         if (!priv->in_progress_assoc_req) {
447                 lbs_deb_join("ADHOC_RESP: no in-progress association "
448                         "request\n");
449                 ret = -1;
450                 goto done;
451         }
452         bss = &priv->in_progress_assoc_req->bss;
453
454         /*
455          * Join result code 0 --> SUCCESS
456          */
457         if (result) {
458                 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
459                 if (priv->connect_status == LBS_CONNECTED)
460                         lbs_mac_event_disconnected(priv);
461                 ret = -1;
462                 goto done;
463         }
464
465         /* Send a Media Connected event, according to the Spec */
466         priv->connect_status = LBS_CONNECTED;
467
468         if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
469                 /* Update the created network descriptor with the new BSSID */
470                 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
471         }
472
473         /* Set the BSSID from the joined/started descriptor */
474         memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
475
476         /* Set the new SSID to current SSID */
477         memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
478         priv->curbssparams.ssid_len = bss->ssid_len;
479
480         netif_carrier_on(priv->dev);
481         if (!priv->tx_pending_len)
482                 netif_wake_queue(priv->dev);
483
484         memset(&wrqu, 0, sizeof(wrqu));
485         memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
486         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
487         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
488
489         lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
490                      print_ssid(ssid, bss->ssid, bss->ssid_len),
491                      priv->curbssparams.bssid,
492                      priv->channel);
493
494 done:
495         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
496         return ret;
497 }
498
499 /**
500  *  @brief Join an adhoc network found in a previous scan
501  *
502  *  @param priv         A pointer to struct lbs_private structure
503  *  @param assoc_req    The association request describing the BSS to join
504  *
505  *  @return             0 on success, error on failure
506  */
507 static int lbs_adhoc_join(struct lbs_private *priv,
508         struct assoc_request *assoc_req)
509 {
510         struct cmd_ds_802_11_ad_hoc_join cmd;
511         struct bss_descriptor *bss = &assoc_req->bss;
512         u8 preamble = RADIO_PREAMBLE_LONG;
513         DECLARE_SSID_BUF(ssid);
514         u16 ratesize = 0;
515         int ret = 0;
516
517         lbs_deb_enter(LBS_DEB_ASSOC);
518
519         lbs_deb_join("current SSID '%s', ssid length %u\n",
520                 print_ssid(ssid, priv->curbssparams.ssid,
521                 priv->curbssparams.ssid_len),
522                 priv->curbssparams.ssid_len);
523         lbs_deb_join("requested ssid '%s', ssid length %u\n",
524                 print_ssid(ssid, bss->ssid, bss->ssid_len),
525                 bss->ssid_len);
526
527         /* check if the requested SSID is already joined */
528         if (priv->curbssparams.ssid_len &&
529             !lbs_ssid_cmp(priv->curbssparams.ssid,
530                         priv->curbssparams.ssid_len,
531                         bss->ssid, bss->ssid_len) &&
532             (priv->mode == IW_MODE_ADHOC) &&
533             (priv->connect_status == LBS_CONNECTED)) {
534                 union iwreq_data wrqu;
535
536                 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
537                         "current, not attempting to re-join");
538
539                 /* Send the re-association event though, because the association
540                  * request really was successful, even if just a null-op.
541                  */
542                 memset(&wrqu, 0, sizeof(wrqu));
543                 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
544                        ETH_ALEN);
545                 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
546                 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
547                 goto out;
548         }
549
550         /* Use short preamble only when both the BSS and firmware support it */
551         if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
552             (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
553                 lbs_deb_join("AdhocJoin: Short preamble\n");
554                 preamble = RADIO_PREAMBLE_SHORT;
555         }
556
557         ret = lbs_set_radio(priv, preamble, 1);
558         if (ret)
559                 goto out;
560
561         lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
562         lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
563
564         priv->adhoccreate = 0;
565         priv->channel = bss->channel;
566
567         /* Build the join command */
568         memset(&cmd, 0, sizeof(cmd));
569         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
570
571         cmd.bss.type = CMD_BSS_TYPE_IBSS;
572         cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
573
574         memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
575         memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
576
577         memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
578
579         memcpy(&cmd.bss.ibss, &bss->ss.ibss,
580                sizeof(struct ieee_ie_ibss_param_set));
581
582         cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
583         lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
584                bss->capability, CAPINFO_MASK);
585
586         /* information on BSSID descriptor passed to FW */
587         lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
588                         cmd.bss.bssid, cmd.bss.ssid);
589
590         /* Only v8 and below support setting these */
591         if (priv->fwrelease < 0x09000000) {
592                 /* failtimeout */
593                 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
594                 /* probedelay */
595                 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
596         }
597
598         /* Copy Data rates from the rates recorded in scan response */
599         memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
600         ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), ARRAY_SIZE (bss->rates));
601         memcpy(cmd.bss.rates, bss->rates, ratesize);
602         if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
603                 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
604                 ret = -1;
605                 goto out;
606         }
607
608         /* Copy the ad-hoc creation rates into Current BSS state structure */
609         memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
610         memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
611
612         /* Set MSB on basic rates as the firmware requires, but _after_
613          * copying to current bss rates.
614          */
615         lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
616
617         cmd.bss.ibss.atimwindow = bss->atimwindow;
618
619         if (assoc_req->secinfo.wep_enabled) {
620                 u16 tmp = le16_to_cpu(cmd.bss.capability);
621                 tmp |= WLAN_CAPABILITY_PRIVACY;
622                 cmd.bss.capability = cpu_to_le16(tmp);
623         }
624
625         if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
626                 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
627
628                 /* wake up first */
629                 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
630                                                    CMD_ACT_SET, 0, 0,
631                                                    &local_ps_mode);
632                 if (ret) {
633                         ret = -1;
634                         goto out;
635                 }
636         }
637
638         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
639         if (ret == 0) {
640                 ret = lbs_adhoc_post(priv,
641                                      (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
642         }
643
644 out:
645         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
646         return ret;
647 }
648
649 /**
650  *  @brief Start an Adhoc Network
651  *
652  *  @param priv         A pointer to struct lbs_private structure
653  *  @param assoc_req    The association request describing the BSS to start
654  *
655  *  @return             0 on success, error on failure
656  */
657 static int lbs_adhoc_start(struct lbs_private *priv,
658         struct assoc_request *assoc_req)
659 {
660         struct cmd_ds_802_11_ad_hoc_start cmd;
661         u8 preamble = RADIO_PREAMBLE_LONG;
662         size_t ratesize = 0;
663         u16 tmpcap = 0;
664         int ret = 0;
665         DECLARE_SSID_BUF(ssid);
666
667         lbs_deb_enter(LBS_DEB_ASSOC);
668
669         if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
670                 lbs_deb_join("ADHOC_START: Will use short preamble\n");
671                 preamble = RADIO_PREAMBLE_SHORT;
672         }
673
674         ret = lbs_set_radio(priv, preamble, 1);
675         if (ret)
676                 goto out;
677
678         /* Build the start command */
679         memset(&cmd, 0, sizeof(cmd));
680         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
681
682         memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
683
684         lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
685                 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
686                 assoc_req->ssid_len);
687
688         cmd.bsstype = CMD_BSS_TYPE_IBSS;
689
690         if (priv->beacon_period == 0)
691                 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
692         cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
693
694         WARN_ON(!assoc_req->channel);
695
696         /* set Physical parameter set */
697         cmd.ds.header.id = WLAN_EID_DS_PARAMS;
698         cmd.ds.header.len = 1;
699         cmd.ds.channel = assoc_req->channel;
700
701         /* set IBSS parameter set */
702         cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
703         cmd.ibss.header.len = 2;
704         cmd.ibss.atimwindow = cpu_to_le16(0);
705
706         /* set capability info */
707         tmpcap = WLAN_CAPABILITY_IBSS;
708         if (assoc_req->secinfo.wep_enabled ||
709             assoc_req->secinfo.WPAenabled ||
710             assoc_req->secinfo.WPA2enabled) {
711                 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
712                 tmpcap |= WLAN_CAPABILITY_PRIVACY;
713         } else
714                 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
715
716         cmd.capability = cpu_to_le16(tmpcap);
717
718         /* Only v8 and below support setting probe delay */
719         if (priv->fwrelease < 0x09000000)
720                 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
721
722         ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
723         memcpy(cmd.rates, lbs_bg_rates, ratesize);
724
725         /* Copy the ad-hoc creating rates into Current BSS state structure */
726         memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
727         memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
728
729         /* Set MSB on basic rates as the firmware requires, but _after_
730          * copying to current bss rates.
731          */
732         lbs_set_basic_rate_flags(cmd.rates, ratesize);
733
734         lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
735                cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
736
737         lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
738                      assoc_req->channel, assoc_req->band);
739
740         priv->adhoccreate = 1;
741         priv->mode = IW_MODE_ADHOC;
742
743         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
744         if (ret == 0)
745                 ret = lbs_adhoc_post(priv,
746                                      (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
747
748 out:
749         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
750         return ret;
751 }
752
753 /**
754  *  @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
755  *
756  *  @param priv         A pointer to struct lbs_private structure
757  *  @return             0 on success, or an error
758  */
759 int lbs_adhoc_stop(struct lbs_private *priv)
760 {
761         struct cmd_ds_802_11_ad_hoc_stop cmd;
762         int ret;
763
764         lbs_deb_enter(LBS_DEB_JOIN);
765
766         memset(&cmd, 0, sizeof (cmd));
767         cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
768
769         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
770
771         /* Clean up everything even if there was an error */
772         lbs_mac_event_disconnected(priv);
773
774         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
775         return ret;
776 }
777
778 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
779                                         struct bss_descriptor *match_bss)
780 {
781         if (!secinfo->wep_enabled  && !secinfo->WPAenabled
782             && !secinfo->WPA2enabled
783             && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
784             && match_bss->rsn_ie[0] != WLAN_EID_RSN
785             && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
786                 return 1;
787         else
788                 return 0;
789 }
790
791 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
792                                        struct bss_descriptor *match_bss)
793 {
794         if (secinfo->wep_enabled && !secinfo->WPAenabled
795             && !secinfo->WPA2enabled
796             && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
797                 return 1;
798         else
799                 return 0;
800 }
801
802 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
803                                 struct bss_descriptor *match_bss)
804 {
805         if (!secinfo->wep_enabled && secinfo->WPAenabled
806             && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
807             /* privacy bit may NOT be set in some APs like LinkSys WRT54G
808             && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
809            )
810                 return 1;
811         else
812                 return 0;
813 }
814
815 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
816                                  struct bss_descriptor *match_bss)
817 {
818         if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
819             (match_bss->rsn_ie[0] == WLAN_EID_RSN)
820             /* privacy bit may NOT be set in some APs like LinkSys WRT54G
821             (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
822            )
823                 return 1;
824         else
825                 return 0;
826 }
827
828 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
829                                         struct bss_descriptor *match_bss)
830 {
831         if (!secinfo->wep_enabled && !secinfo->WPAenabled
832             && !secinfo->WPA2enabled
833             && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
834             && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
835             && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
836                 return 1;
837         else
838                 return 0;
839 }
840
841 /**
842  *  @brief Check if a scanned network compatible with the driver settings
843  *
844  *   WEP     WPA     WPA2    ad-hoc  encrypt                      Network
845  * enabled enabled  enabled   AES     mode   privacy  WPA  WPA2  Compatible
846  *    0       0        0       0      NONE      0      0    0   yes No security
847  *    1       0        0       0      NONE      1      0    0   yes Static WEP
848  *    0       1        0       0       x        1x     1    x   yes WPA
849  *    0       0        1       0       x        1x     x    1   yes WPA2
850  *    0       0        0       1      NONE      1      0    0   yes Ad-hoc AES
851  *    0       0        0       0     !=NONE     1      0    0   yes Dynamic WEP
852  *
853  *
854  *  @param priv A pointer to struct lbs_private
855  *  @param index   Index in scantable to check against current driver settings
856  *  @param mode    Network mode: Infrastructure or IBSS
857  *
858  *  @return        Index in scantable, or error code if negative
859  */
860 static int is_network_compatible(struct lbs_private *priv,
861                                  struct bss_descriptor *bss, uint8_t mode)
862 {
863         int matched = 0;
864
865         lbs_deb_enter(LBS_DEB_SCAN);
866
867         if (bss->mode != mode)
868                 goto done;
869
870         matched = match_bss_no_security(&priv->secinfo, bss);
871         if (matched)
872                 goto done;
873         matched = match_bss_static_wep(&priv->secinfo, bss);
874         if (matched)
875                 goto done;
876         matched = match_bss_wpa(&priv->secinfo, bss);
877         if (matched) {
878                 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
879                              "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
880                              "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
881                              priv->secinfo.wep_enabled ? "e" : "d",
882                              priv->secinfo.WPAenabled ? "e" : "d",
883                              priv->secinfo.WPA2enabled ? "e" : "d",
884                              (bss->capability & WLAN_CAPABILITY_PRIVACY));
885                 goto done;
886         }
887         matched = match_bss_wpa2(&priv->secinfo, bss);
888         if (matched) {
889                 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
890                              "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
891                              "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
892                              priv->secinfo.wep_enabled ? "e" : "d",
893                              priv->secinfo.WPAenabled ? "e" : "d",
894                              priv->secinfo.WPA2enabled ? "e" : "d",
895                              (bss->capability & WLAN_CAPABILITY_PRIVACY));
896                 goto done;
897         }
898         matched = match_bss_dynamic_wep(&priv->secinfo, bss);
899         if (matched) {
900                 lbs_deb_scan("is_network_compatible() dynamic WEP: "
901                              "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
902                              bss->wpa_ie[0], bss->rsn_ie[0],
903                              (bss->capability & WLAN_CAPABILITY_PRIVACY));
904                 goto done;
905         }
906
907         /* bss security settings don't match those configured on card */
908         lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
909                      "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
910                      bss->wpa_ie[0], bss->rsn_ie[0],
911                      priv->secinfo.wep_enabled ? "e" : "d",
912                      priv->secinfo.WPAenabled ? "e" : "d",
913                      priv->secinfo.WPA2enabled ? "e" : "d",
914                      (bss->capability & WLAN_CAPABILITY_PRIVACY));
915
916 done:
917         lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
918         return matched;
919 }
920
921 /**
922  *  @brief This function finds a specific compatible BSSID in the scan list
923  *
924  *  Used in association code
925  *
926  *  @param priv  A pointer to struct lbs_private
927  *  @param bssid    BSSID to find in the scan list
928  *  @param mode     Network mode: Infrastructure or IBSS
929  *
930  *  @return         index in BSSID list, or error return code (< 0)
931  */
932 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
933                                               uint8_t *bssid, uint8_t mode)
934 {
935         struct bss_descriptor *iter_bss;
936         struct bss_descriptor *found_bss = NULL;
937
938         lbs_deb_enter(LBS_DEB_SCAN);
939
940         if (!bssid)
941                 goto out;
942
943         lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
944
945         /* Look through the scan table for a compatible match.  The loop will
946          *   continue past a matched bssid that is not compatible in case there
947          *   is an AP with multiple SSIDs assigned to the same BSSID
948          */
949         mutex_lock(&priv->lock);
950         list_for_each_entry(iter_bss, &priv->network_list, list) {
951                 if (compare_ether_addr(iter_bss->bssid, bssid))
952                         continue; /* bssid doesn't match */
953                 switch (mode) {
954                 case IW_MODE_INFRA:
955                 case IW_MODE_ADHOC:
956                         if (!is_network_compatible(priv, iter_bss, mode))
957                                 break;
958                         found_bss = iter_bss;
959                         break;
960                 default:
961                         found_bss = iter_bss;
962                         break;
963                 }
964         }
965         mutex_unlock(&priv->lock);
966
967 out:
968         lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
969         return found_bss;
970 }
971
972 /**
973  *  @brief This function finds ssid in ssid list.
974  *
975  *  Used in association code
976  *
977  *  @param priv  A pointer to struct lbs_private
978  *  @param ssid     SSID to find in the list
979  *  @param bssid    BSSID to qualify the SSID selection (if provided)
980  *  @param mode     Network mode: Infrastructure or IBSS
981  *
982  *  @return         index in BSSID list
983  */
984 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
985                                              uint8_t *ssid, uint8_t ssid_len,
986                                              uint8_t *bssid, uint8_t mode,
987                                              int channel)
988 {
989         u32 bestrssi = 0;
990         struct bss_descriptor *iter_bss = NULL;
991         struct bss_descriptor *found_bss = NULL;
992         struct bss_descriptor *tmp_oldest = NULL;
993
994         lbs_deb_enter(LBS_DEB_SCAN);
995
996         mutex_lock(&priv->lock);
997
998         list_for_each_entry(iter_bss, &priv->network_list, list) {
999                 if (!tmp_oldest ||
1000                     (iter_bss->last_scanned < tmp_oldest->last_scanned))
1001                         tmp_oldest = iter_bss;
1002
1003                 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1004                                  ssid, ssid_len) != 0)
1005                         continue; /* ssid doesn't match */
1006                 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1007                         continue; /* bssid doesn't match */
1008                 if ((channel > 0) && (iter_bss->channel != channel))
1009                         continue; /* channel doesn't match */
1010
1011                 switch (mode) {
1012                 case IW_MODE_INFRA:
1013                 case IW_MODE_ADHOC:
1014                         if (!is_network_compatible(priv, iter_bss, mode))
1015                                 break;
1016
1017                         if (bssid) {
1018                                 /* Found requested BSSID */
1019                                 found_bss = iter_bss;
1020                                 goto out;
1021                         }
1022
1023                         if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1024                                 bestrssi = SCAN_RSSI(iter_bss->rssi);
1025                                 found_bss = iter_bss;
1026                         }
1027                         break;
1028                 case IW_MODE_AUTO:
1029                 default:
1030                         if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1031                                 bestrssi = SCAN_RSSI(iter_bss->rssi);
1032                                 found_bss = iter_bss;
1033                         }
1034                         break;
1035                 }
1036         }
1037
1038 out:
1039         mutex_unlock(&priv->lock);
1040         lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1041         return found_bss;
1042 }
1043
1044 static int assoc_helper_essid(struct lbs_private *priv,
1045                               struct assoc_request * assoc_req)
1046 {
1047         int ret = 0;
1048         struct bss_descriptor * bss;
1049         int channel = -1;
1050         DECLARE_SSID_BUF(ssid);
1051
1052         lbs_deb_enter(LBS_DEB_ASSOC);
1053
1054         /* FIXME: take channel into account when picking SSIDs if a channel
1055          * is set.
1056          */
1057
1058         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1059                 channel = assoc_req->channel;
1060
1061         lbs_deb_assoc("SSID '%s' requested\n",
1062                       print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
1063         if (assoc_req->mode == IW_MODE_INFRA) {
1064                 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1065                         assoc_req->ssid_len);
1066
1067                 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1068                                 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
1069                 if (bss != NULL) {
1070                         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1071                         ret = lbs_try_associate(priv, assoc_req);
1072                 } else {
1073                         lbs_deb_assoc("SSID not found; cannot associate\n");
1074                 }
1075         } else if (assoc_req->mode == IW_MODE_ADHOC) {
1076                 /* Scan for the network, do not save previous results.  Stale
1077                  *   scan data will cause us to join a non-existant adhoc network
1078                  */
1079                 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1080                         assoc_req->ssid_len);
1081
1082                 /* Search for the requested SSID in the scan table */
1083                 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1084                                 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
1085                 if (bss != NULL) {
1086                         lbs_deb_assoc("SSID found, will join\n");
1087                         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1088                         lbs_adhoc_join(priv, assoc_req);
1089                 } else {
1090                         /* else send START command */
1091                         lbs_deb_assoc("SSID not found, creating adhoc network\n");
1092                         memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
1093                                 IEEE80211_MAX_SSID_LEN);
1094                         assoc_req->bss.ssid_len = assoc_req->ssid_len;
1095                         lbs_adhoc_start(priv, assoc_req);
1096                 }
1097         }
1098
1099         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1100         return ret;
1101 }
1102
1103
1104 static int assoc_helper_bssid(struct lbs_private *priv,
1105                               struct assoc_request * assoc_req)
1106 {
1107         int ret = 0;
1108         struct bss_descriptor * bss;
1109
1110         lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
1111
1112         /* Search for index position in list for requested MAC */
1113         bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
1114                             assoc_req->mode);
1115         if (bss == NULL) {
1116                 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1117                         "cannot associate.\n", assoc_req->bssid);
1118                 goto out;
1119         }
1120
1121         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1122         if (assoc_req->mode == IW_MODE_INFRA) {
1123                 ret = lbs_try_associate(priv, assoc_req);
1124                 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1125                               ret);
1126         } else if (assoc_req->mode == IW_MODE_ADHOC) {
1127                 lbs_adhoc_join(priv, assoc_req);
1128         }
1129
1130 out:
1131         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1132         return ret;
1133 }
1134
1135
1136 static int assoc_helper_associate(struct lbs_private *priv,
1137                                   struct assoc_request * assoc_req)
1138 {
1139         int ret = 0, done = 0;
1140
1141         lbs_deb_enter(LBS_DEB_ASSOC);
1142
1143         /* If we're given and 'any' BSSID, try associating based on SSID */
1144
1145         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1146                 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1147                     && compare_ether_addr(bssid_off, assoc_req->bssid)) {
1148                         ret = assoc_helper_bssid(priv, assoc_req);
1149                         done = 1;
1150                 }
1151         }
1152
1153         if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1154                 ret = assoc_helper_essid(priv, assoc_req);
1155         }
1156
1157         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1158         return ret;
1159 }
1160
1161
1162 static int assoc_helper_mode(struct lbs_private *priv,
1163                              struct assoc_request * assoc_req)
1164 {
1165         int ret = 0;
1166
1167         lbs_deb_enter(LBS_DEB_ASSOC);
1168
1169         if (assoc_req->mode == priv->mode)
1170                 goto done;
1171
1172         if (assoc_req->mode == IW_MODE_INFRA) {
1173                 if (priv->psstate != PS_STATE_FULL_POWER)
1174                         lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1175                 priv->psmode = LBS802_11POWERMODECAM;
1176         }
1177
1178         priv->mode = assoc_req->mode;
1179         ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
1180
1181 done:
1182         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1183         return ret;
1184 }
1185
1186 static int assoc_helper_channel(struct lbs_private *priv,
1187                                 struct assoc_request * assoc_req)
1188 {
1189         int ret = 0;
1190
1191         lbs_deb_enter(LBS_DEB_ASSOC);
1192
1193         ret = lbs_update_channel(priv);
1194         if (ret) {
1195                 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1196                 goto done;
1197         }
1198
1199         if (assoc_req->channel == priv->channel)
1200                 goto done;
1201
1202         if (priv->mesh_dev) {
1203                 /* Change mesh channel first; 21.p21 firmware won't let
1204                    you change channel otherwise (even though it'll return
1205                    an error to this */
1206                 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1207                                 assoc_req->channel);
1208         }
1209
1210         lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
1211                       priv->channel, assoc_req->channel);
1212
1213         ret = lbs_set_channel(priv, assoc_req->channel);
1214         if (ret < 0)
1215                 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
1216
1217         /* FIXME: shouldn't need to grab the channel _again_ after setting
1218          * it since the firmware is supposed to return the new channel, but
1219          * whatever... */
1220         ret = lbs_update_channel(priv);
1221         if (ret) {
1222                 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1223                 goto done;
1224         }
1225
1226         if (assoc_req->channel != priv->channel) {
1227                 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
1228                               assoc_req->channel);
1229                 goto restore_mesh;
1230         }
1231
1232         if (   assoc_req->secinfo.wep_enabled
1233             &&   (assoc_req->wep_keys[0].len
1234                || assoc_req->wep_keys[1].len
1235                || assoc_req->wep_keys[2].len
1236                || assoc_req->wep_keys[3].len)) {
1237                 /* Make sure WEP keys are re-sent to firmware */
1238                 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1239         }
1240
1241         /* Must restart/rejoin adhoc networks after channel change */
1242         set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
1243
1244  restore_mesh:
1245         if (priv->mesh_dev)
1246                 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1247                                 priv->channel);
1248
1249  done:
1250         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1251         return ret;
1252 }
1253
1254
1255 static int assoc_helper_wep_keys(struct lbs_private *priv,
1256                                  struct assoc_request *assoc_req)
1257 {
1258         int i;
1259         int ret = 0;
1260
1261         lbs_deb_enter(LBS_DEB_ASSOC);
1262
1263         /* Set or remove WEP keys */
1264         if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1265             assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1266                 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1267         else
1268                 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
1269
1270         if (ret)
1271                 goto out;
1272
1273         /* enable/disable the MAC's WEP packet filter */
1274         if (assoc_req->secinfo.wep_enabled)
1275                 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1276         else
1277                 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1278
1279         lbs_set_mac_control(priv);
1280
1281         mutex_lock(&priv->lock);
1282
1283         /* Copy WEP keys into priv wep key fields */
1284         for (i = 0; i < 4; i++) {
1285                 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
1286                        sizeof(struct enc_key));
1287         }
1288         priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
1289
1290         mutex_unlock(&priv->lock);
1291
1292 out:
1293         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1294         return ret;
1295 }
1296
1297 static int assoc_helper_secinfo(struct lbs_private *priv,
1298                                 struct assoc_request * assoc_req)
1299 {
1300         int ret = 0;
1301         uint16_t do_wpa;
1302         uint16_t rsn = 0;
1303
1304         lbs_deb_enter(LBS_DEB_ASSOC);
1305
1306         memcpy(&priv->secinfo, &assoc_req->secinfo,
1307                 sizeof(struct lbs_802_11_security));
1308
1309         lbs_set_mac_control(priv);
1310
1311         /* If RSN is already enabled, don't try to enable it again, since
1312          * ENABLE_RSN resets internal state machines and will clobber the
1313          * 4-way WPA handshake.
1314          */
1315
1316         /* Get RSN enabled/disabled */
1317         ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
1318         if (ret) {
1319                 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
1320                 goto out;
1321         }
1322
1323         /* Don't re-enable RSN if it's already enabled */
1324         do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
1325         if (do_wpa == rsn)
1326                 goto out;
1327
1328         /* Set RSN enabled/disabled */
1329         ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
1330
1331 out:
1332         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1333         return ret;
1334 }
1335
1336
1337 static int assoc_helper_wpa_keys(struct lbs_private *priv,
1338                                  struct assoc_request * assoc_req)
1339 {
1340         int ret = 0;
1341         unsigned int flags = assoc_req->flags;
1342
1343         lbs_deb_enter(LBS_DEB_ASSOC);
1344
1345         /* Work around older firmware bug where WPA unicast and multicast
1346          * keys must be set independently.  Seen in SDIO parts with firmware
1347          * version 5.0.11p0.
1348          */
1349
1350         if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1351                 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1352                 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1353                 assoc_req->flags = flags;
1354         }
1355
1356         if (ret)
1357                 goto out;
1358
1359         memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1360                         sizeof(struct enc_key));
1361
1362         if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1363                 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1364
1365                 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1366                 assoc_req->flags = flags;
1367
1368                 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1369                                 sizeof(struct enc_key));
1370         }
1371
1372 out:
1373         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1374         return ret;
1375 }
1376
1377
1378 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1379                                struct assoc_request * assoc_req)
1380 {
1381         int ret = 0;
1382
1383         lbs_deb_enter(LBS_DEB_ASSOC);
1384
1385         if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1386                 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1387                 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1388         } else {
1389                 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1390                 priv->wpa_ie_len = 0;
1391         }
1392
1393         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1394         return ret;
1395 }
1396
1397
1398 static int should_deauth_infrastructure(struct lbs_private *priv,
1399                                         struct assoc_request * assoc_req)
1400 {
1401         int ret = 0;
1402
1403         if (priv->connect_status != LBS_CONNECTED)
1404                 return 0;
1405
1406         lbs_deb_enter(LBS_DEB_ASSOC);
1407         if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1408                 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1409                 ret = 1;
1410                 goto out;
1411         }
1412
1413         if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1414                 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1415                         lbs_deb_assoc("Deauthenticating due to new security\n");
1416                         ret = 1;
1417                         goto out;
1418                 }
1419         }
1420
1421         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1422                 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1423                 ret = 1;
1424                 goto out;
1425         }
1426
1427         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1428                 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1429                 ret = 1;
1430                 goto out;
1431         }
1432
1433         /* FIXME: deal with 'auto' mode somehow */
1434         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1435                 if (assoc_req->mode != IW_MODE_INFRA) {
1436                         lbs_deb_assoc("Deauthenticating due to leaving "
1437                                 "infra mode\n");
1438                         ret = 1;
1439                         goto out;
1440                 }
1441         }
1442
1443 out:
1444         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1445         return ret;
1446 }
1447
1448
1449 static int should_stop_adhoc(struct lbs_private *priv,
1450                              struct assoc_request * assoc_req)
1451 {
1452         lbs_deb_enter(LBS_DEB_ASSOC);
1453
1454         if (priv->connect_status != LBS_CONNECTED)
1455                 return 0;
1456
1457         if (lbs_ssid_cmp(priv->curbssparams.ssid,
1458                               priv->curbssparams.ssid_len,
1459                               assoc_req->ssid, assoc_req->ssid_len) != 0)
1460                 return 1;
1461
1462         /* FIXME: deal with 'auto' mode somehow */
1463         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1464                 if (assoc_req->mode != IW_MODE_ADHOC)
1465                         return 1;
1466         }
1467
1468         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1469                 if (assoc_req->channel != priv->channel)
1470                         return 1;
1471         }
1472
1473         lbs_deb_leave(LBS_DEB_ASSOC);
1474         return 0;
1475 }
1476
1477
1478 /**
1479  *  @brief This function finds the best SSID in the Scan List
1480  *
1481  *  Search the scan table for the best SSID that also matches the current
1482  *   adapter network preference (infrastructure or adhoc)
1483  *
1484  *  @param priv  A pointer to struct lbs_private
1485  *
1486  *  @return         index in BSSID list
1487  */
1488 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1489         struct lbs_private *priv, uint8_t mode)
1490 {
1491         uint8_t bestrssi = 0;
1492         struct bss_descriptor *iter_bss;
1493         struct bss_descriptor *best_bss = NULL;
1494
1495         lbs_deb_enter(LBS_DEB_SCAN);
1496
1497         mutex_lock(&priv->lock);
1498
1499         list_for_each_entry(iter_bss, &priv->network_list, list) {
1500                 switch (mode) {
1501                 case IW_MODE_INFRA:
1502                 case IW_MODE_ADHOC:
1503                         if (!is_network_compatible(priv, iter_bss, mode))
1504                                 break;
1505                         if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1506                                 break;
1507                         bestrssi = SCAN_RSSI(iter_bss->rssi);
1508                         best_bss = iter_bss;
1509                         break;
1510                 case IW_MODE_AUTO:
1511                 default:
1512                         if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1513                                 break;
1514                         bestrssi = SCAN_RSSI(iter_bss->rssi);
1515                         best_bss = iter_bss;
1516                         break;
1517                 }
1518         }
1519
1520         mutex_unlock(&priv->lock);
1521         lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1522         return best_bss;
1523 }
1524
1525 /**
1526  *  @brief Find the best AP
1527  *
1528  *  Used from association worker.
1529  *
1530  *  @param priv         A pointer to struct lbs_private structure
1531  *  @param pSSID        A pointer to AP's ssid
1532  *
1533  *  @return             0--success, otherwise--fail
1534  */
1535 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1536         uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1537         uint8_t *out_mode)
1538 {
1539         int ret = -1;
1540         struct bss_descriptor *found;
1541
1542         lbs_deb_enter(LBS_DEB_SCAN);
1543
1544         priv->scan_ssid_len = 0;
1545         lbs_scan_networks(priv, 1);
1546         if (priv->surpriseremoved)
1547                 goto out;
1548
1549         found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1550         if (found && (found->ssid_len > 0)) {
1551                 memcpy(out_ssid, &found->ssid, IEEE80211_MAX_SSID_LEN);
1552                 *out_ssid_len = found->ssid_len;
1553                 *out_mode = found->mode;
1554                 ret = 0;
1555         }
1556
1557 out:
1558         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1559         return ret;
1560 }
1561
1562
1563 void lbs_association_worker(struct work_struct *work)
1564 {
1565         struct lbs_private *priv = container_of(work, struct lbs_private,
1566                 assoc_work.work);
1567         struct assoc_request * assoc_req = NULL;
1568         int ret = 0;
1569         int find_any_ssid = 0;
1570         DECLARE_SSID_BUF(ssid);
1571
1572         lbs_deb_enter(LBS_DEB_ASSOC);
1573
1574         mutex_lock(&priv->lock);
1575         assoc_req = priv->pending_assoc_req;
1576         priv->pending_assoc_req = NULL;
1577         priv->in_progress_assoc_req = assoc_req;
1578         mutex_unlock(&priv->lock);
1579
1580         if (!assoc_req)
1581                 goto done;
1582
1583         lbs_deb_assoc(
1584                 "Association Request:\n"
1585                 "    flags:     0x%08lx\n"
1586                 "    SSID:      '%s'\n"
1587                 "    chann:     %d\n"
1588                 "    band:      %d\n"
1589                 "    mode:      %d\n"
1590                 "    BSSID:     %pM\n"
1591                 "    secinfo:  %s%s%s\n"
1592                 "    auth_mode: %d\n",
1593                 assoc_req->flags,
1594                 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1595                 assoc_req->channel, assoc_req->band, assoc_req->mode,
1596                 assoc_req->bssid,
1597                 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1598                 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1599                 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1600                 assoc_req->secinfo.auth_mode);
1601
1602         /* If 'any' SSID was specified, find an SSID to associate with */
1603         if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1604             && !assoc_req->ssid_len)
1605                 find_any_ssid = 1;
1606
1607         /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1608         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1609                 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1610                     && compare_ether_addr(assoc_req->bssid, bssid_off))
1611                         find_any_ssid = 0;
1612         }
1613
1614         if (find_any_ssid) {
1615                 u8 new_mode = assoc_req->mode;
1616
1617                 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1618                                 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1619                 if (ret) {
1620                         lbs_deb_assoc("Could not find best network\n");
1621                         ret = -ENETUNREACH;
1622                         goto out;
1623                 }
1624
1625                 /* Ensure we switch to the mode of the AP */
1626                 if (assoc_req->mode == IW_MODE_AUTO) {
1627                         set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1628                         assoc_req->mode = new_mode;
1629                 }
1630         }
1631
1632         /*
1633          * Check if the attributes being changing require deauthentication
1634          * from the currently associated infrastructure access point.
1635          */
1636         if (priv->mode == IW_MODE_INFRA) {
1637                 if (should_deauth_infrastructure(priv, assoc_req)) {
1638                         ret = lbs_cmd_80211_deauthenticate(priv,
1639                                                            priv->curbssparams.bssid,
1640                                                            WLAN_REASON_DEAUTH_LEAVING);
1641                         if (ret) {
1642                                 lbs_deb_assoc("Deauthentication due to new "
1643                                         "configuration request failed: %d\n",
1644                                         ret);
1645                         }
1646                 }
1647         } else if (priv->mode == IW_MODE_ADHOC) {
1648                 if (should_stop_adhoc(priv, assoc_req)) {
1649                         ret = lbs_adhoc_stop(priv);
1650                         if (ret) {
1651                                 lbs_deb_assoc("Teardown of AdHoc network due to "
1652                                         "new configuration request failed: %d\n",
1653                                         ret);
1654                         }
1655
1656                 }
1657         }
1658
1659         /* Send the various configuration bits to the firmware */
1660         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1661                 ret = assoc_helper_mode(priv, assoc_req);
1662                 if (ret)
1663                         goto out;
1664         }
1665
1666         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1667                 ret = assoc_helper_channel(priv, assoc_req);
1668                 if (ret)
1669                         goto out;
1670         }
1671
1672         if (   test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1673             || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1674                 ret = assoc_helper_wep_keys(priv, assoc_req);
1675                 if (ret)
1676                         goto out;
1677         }
1678
1679         if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1680                 ret = assoc_helper_secinfo(priv, assoc_req);
1681                 if (ret)
1682                         goto out;
1683         }
1684
1685         if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1686                 ret = assoc_helper_wpa_ie(priv, assoc_req);
1687                 if (ret)
1688                         goto out;
1689         }
1690
1691         if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1692             || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1693                 ret = assoc_helper_wpa_keys(priv, assoc_req);
1694                 if (ret)
1695                         goto out;
1696         }
1697
1698         /* SSID/BSSID should be the _last_ config option set, because they
1699          * trigger the association attempt.
1700          */
1701         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1702             || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1703                 int success = 1;
1704
1705                 ret = assoc_helper_associate(priv, assoc_req);
1706                 if (ret) {
1707                         lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1708                                 ret);
1709                         success = 0;
1710                 }
1711
1712                 if (priv->connect_status != LBS_CONNECTED) {
1713                         lbs_deb_assoc("ASSOC: association unsuccessful, "
1714                                 "not connected\n");
1715                         success = 0;
1716                 }
1717
1718                 if (success) {
1719                         lbs_deb_assoc("associated to %pM\n",
1720                                 priv->curbssparams.bssid);
1721                         lbs_prepare_and_send_command(priv,
1722                                 CMD_802_11_RSSI,
1723                                 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1724                 } else {
1725                         ret = -1;
1726                 }
1727         }
1728
1729 out:
1730         if (ret) {
1731                 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1732                         ret);
1733         }
1734
1735         mutex_lock(&priv->lock);
1736         priv->in_progress_assoc_req = NULL;
1737         mutex_unlock(&priv->lock);
1738         kfree(assoc_req);
1739
1740 done:
1741         lbs_deb_leave(LBS_DEB_ASSOC);
1742 }
1743
1744
1745 /*
1746  * Caller MUST hold any necessary locks
1747  */
1748 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1749 {
1750         struct assoc_request * assoc_req;
1751
1752         lbs_deb_enter(LBS_DEB_ASSOC);
1753         if (!priv->pending_assoc_req) {
1754                 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1755                                                      GFP_KERNEL);
1756                 if (!priv->pending_assoc_req) {
1757                         lbs_pr_info("Not enough memory to allocate association"
1758                                 " request!\n");
1759                         return NULL;
1760                 }
1761         }
1762
1763         /* Copy current configuration attributes to the association request,
1764          * but don't overwrite any that are already set.
1765          */
1766         assoc_req = priv->pending_assoc_req;
1767         if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1768                 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1769                        IEEE80211_MAX_SSID_LEN);
1770                 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1771         }
1772
1773         if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1774                 assoc_req->channel = priv->channel;
1775
1776         if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1777                 assoc_req->band = priv->curbssparams.band;
1778
1779         if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1780                 assoc_req->mode = priv->mode;
1781
1782         if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1783                 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1784                         ETH_ALEN);
1785         }
1786
1787         if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1788                 int i;
1789                 for (i = 0; i < 4; i++) {
1790                         memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1791                                 sizeof(struct enc_key));
1792                 }
1793         }
1794
1795         if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1796                 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1797
1798         if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1799                 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1800                         sizeof(struct enc_key));
1801         }
1802
1803         if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1804                 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1805                         sizeof(struct enc_key));
1806         }
1807
1808         if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1809                 memcpy(&assoc_req->secinfo, &priv->secinfo,
1810                         sizeof(struct lbs_802_11_security));
1811         }
1812
1813         if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1814                 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1815                         MAX_WPA_IE_LEN);
1816                 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1817         }
1818
1819         lbs_deb_leave(LBS_DEB_ASSOC);
1820         return assoc_req;
1821 }
1822
1823
1824 /**
1825  *  @brief Deauthenticate from a specific BSS
1826  *
1827  *  @param priv        A pointer to struct lbs_private structure
1828  *  @param bssid       The specific BSS to deauthenticate from
1829  *  @param reason      The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1830  *
1831  *  @return            0 on success, error on failure
1832  */
1833 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1834                                  u16 reason)
1835 {
1836         struct cmd_ds_802_11_deauthenticate cmd;
1837         int ret;
1838
1839         lbs_deb_enter(LBS_DEB_JOIN);
1840
1841         memset(&cmd, 0, sizeof(cmd));
1842         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1843         memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1844         cmd.reasoncode = cpu_to_le16(reason);
1845
1846         ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1847
1848         /* Clean up everything even if there was an error; can't assume that
1849          * we're still authenticated to the AP after trying to deauth.
1850          */
1851         lbs_mac_event_disconnected(priv);
1852
1853         lbs_deb_leave(LBS_DEB_JOIN);
1854         return ret;
1855 }
1856