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