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