blob: 319b4f931a62e021afa154275a2e3a3fcb487ead [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/* Copyright (C) 2006, Red Hat, Inc. */
2
Dan Williams3cf209312007-05-25 17:28:30 -04003#include <linux/etherdevice.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02004
5#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02006#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02007#include "host.h"
Holger Schurig245bf202008-04-02 16:27:42 +02008#include "scan.h"
Dan Williams2dd4b262007-12-11 16:54:15 -05009#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010
11
Ihar Hrachyshka5a6e0432008-01-25 14:15:00 +010012static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
13 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
14static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
15 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020016
Holger Schurig697900a2008-04-02 16:27:10 +020017/* The firmware needs certain bits masked out of the beacon-derviced capability
18 * field when associating/joining to BSSs.
19 */
20#define CAPINFO_MASK (~(0xda00))
21
22
23
24/**
25 * @brief Associate to a specific BSS discovered in a scan
26 *
27 * @param priv A pointer to struct lbs_private structure
28 * @param pbssdesc Pointer to the BSS descriptor to associate with.
29 *
30 * @return 0-success, otherwise fail
31 */
32static int lbs_associate(struct lbs_private *priv,
33 struct assoc_request *assoc_req)
34{
35 int ret;
36
37 lbs_deb_enter(LBS_DEB_ASSOC);
38
39 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
40 0, CMD_OPTION_WAITFORRSP,
41 0, assoc_req->bss.bssid);
42
43 if (ret)
44 goto done;
45
46 /* set preamble to firmware */
47 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
48 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
49 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
50 else
51 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
52
53 lbs_set_radio_control(priv);
54
55 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
56 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
57
58done:
59 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
60 return ret;
61}
62
63/**
64 * @brief Join an adhoc network found in a previous scan
65 *
66 * @param priv A pointer to struct lbs_private structure
67 * @param pbssdesc Pointer to a BSS descriptor found in a previous scan
68 * to attempt to join
69 *
70 * @return 0--success, -1--fail
71 */
72static int lbs_join_adhoc_network(struct lbs_private *priv,
73 struct assoc_request *assoc_req)
74{
75 struct bss_descriptor *bss = &assoc_req->bss;
76 int ret = 0;
77
78 lbs_deb_join("current SSID '%s', ssid length %u\n",
79 escape_essid(priv->curbssparams.ssid,
80 priv->curbssparams.ssid_len),
81 priv->curbssparams.ssid_len);
82 lbs_deb_join("requested ssid '%s', ssid length %u\n",
83 escape_essid(bss->ssid, bss->ssid_len),
84 bss->ssid_len);
85
86 /* check if the requested SSID is already joined */
87 if (priv->curbssparams.ssid_len &&
88 !lbs_ssid_cmp(priv->curbssparams.ssid,
89 priv->curbssparams.ssid_len,
90 bss->ssid, bss->ssid_len) &&
91 (priv->mode == IW_MODE_ADHOC) &&
92 (priv->connect_status == LBS_CONNECTED)) {
93 union iwreq_data wrqu;
94
95 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
96 "current, not attempting to re-join");
97
98 /* Send the re-association event though, because the association
99 * request really was successful, even if just a null-op.
100 */
101 memset(&wrqu, 0, sizeof(wrqu));
102 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
103 ETH_ALEN);
104 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
105 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
106 goto out;
107 }
108
109 /* Use shortpreamble only when both creator and card supports
110 short preamble */
111 if (!(bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) ||
112 !(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
113 lbs_deb_join("AdhocJoin: Long preamble\n");
114 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
115 } else {
116 lbs_deb_join("AdhocJoin: Short preamble\n");
117 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
118 }
119
120 lbs_set_radio_control(priv);
121
122 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
123 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
124
125 priv->adhoccreate = 0;
126
127 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_JOIN,
128 0, CMD_OPTION_WAITFORRSP,
129 OID_802_11_SSID, assoc_req);
130
131out:
132 return ret;
133}
134
135/**
136 * @brief Start an Adhoc Network
137 *
138 * @param priv A pointer to struct lbs_private structure
139 * @param adhocssid The ssid of the Adhoc Network
140 * @return 0--success, -1--fail
141 */
142static int lbs_start_adhoc_network(struct lbs_private *priv,
143 struct assoc_request *assoc_req)
144{
145 int ret = 0;
146
147 priv->adhoccreate = 1;
148
149 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
150 lbs_deb_join("AdhocStart: Short preamble\n");
151 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
152 } else {
153 lbs_deb_join("AdhocStart: Long preamble\n");
154 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
155 }
156
157 lbs_set_radio_control(priv);
158
159 lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
160 lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
161
162 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
163 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
164
165 return ret;
166}
167
168int lbs_stop_adhoc_network(struct lbs_private *priv)
169{
170 return lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
171 0, CMD_OPTION_WAITFORRSP, 0, NULL);
172}
Dan Williamse76850d2007-05-25 17:09:41 -0400173
Holger Schurig245bf202008-04-02 16:27:42 +0200174static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
175 struct bss_descriptor *match_bss)
176{
177 if (!secinfo->wep_enabled && !secinfo->WPAenabled
178 && !secinfo->WPA2enabled
179 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
180 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
181 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
182 return 1;
183 else
184 return 0;
185}
186
187static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
188 struct bss_descriptor *match_bss)
189{
190 if (secinfo->wep_enabled && !secinfo->WPAenabled
191 && !secinfo->WPA2enabled
192 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
193 return 1;
194 else
195 return 0;
196}
197
198static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
199 struct bss_descriptor *match_bss)
200{
201 if (!secinfo->wep_enabled && secinfo->WPAenabled
202 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
203 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
204 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
205 )
206 return 1;
207 else
208 return 0;
209}
210
211static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
212 struct bss_descriptor *match_bss)
213{
214 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
215 (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
216 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
217 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
218 )
219 return 1;
220 else
221 return 0;
222}
223
224static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
225 struct bss_descriptor *match_bss)
226{
227 if (!secinfo->wep_enabled && !secinfo->WPAenabled
228 && !secinfo->WPA2enabled
229 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
230 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
231 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
232 return 1;
233 else
234 return 0;
235}
236
237/**
238 * @brief Check if a scanned network compatible with the driver settings
239 *
240 * WEP WPA WPA2 ad-hoc encrypt Network
241 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
242 * 0 0 0 0 NONE 0 0 0 yes No security
243 * 1 0 0 0 NONE 1 0 0 yes Static WEP
244 * 0 1 0 0 x 1x 1 x yes WPA
245 * 0 0 1 0 x 1x x 1 yes WPA2
246 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
247 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
248 *
249 *
250 * @param priv A pointer to struct lbs_private
251 * @param index Index in scantable to check against current driver settings
252 * @param mode Network mode: Infrastructure or IBSS
253 *
254 * @return Index in scantable, or error code if negative
255 */
256static int is_network_compatible(struct lbs_private *priv,
257 struct bss_descriptor *bss, uint8_t mode)
258{
259 int matched = 0;
260
261 lbs_deb_enter(LBS_DEB_SCAN);
262
263 if (bss->mode != mode)
264 goto done;
265
266 matched = match_bss_no_security(&priv->secinfo, bss);
267 if (matched)
268 goto done;
269 matched = match_bss_static_wep(&priv->secinfo, bss);
270 if (matched)
271 goto done;
272 matched = match_bss_wpa(&priv->secinfo, bss);
273 if (matched) {
274 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
275 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
276 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
277 priv->secinfo.wep_enabled ? "e" : "d",
278 priv->secinfo.WPAenabled ? "e" : "d",
279 priv->secinfo.WPA2enabled ? "e" : "d",
280 (bss->capability & WLAN_CAPABILITY_PRIVACY));
281 goto done;
282 }
283 matched = match_bss_wpa2(&priv->secinfo, bss);
284 if (matched) {
285 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
286 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
287 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
288 priv->secinfo.wep_enabled ? "e" : "d",
289 priv->secinfo.WPAenabled ? "e" : "d",
290 priv->secinfo.WPA2enabled ? "e" : "d",
291 (bss->capability & WLAN_CAPABILITY_PRIVACY));
292 goto done;
293 }
294 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
295 if (matched) {
296 lbs_deb_scan("is_network_compatible() dynamic WEP: "
297 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
298 bss->wpa_ie[0], bss->rsn_ie[0],
299 (bss->capability & WLAN_CAPABILITY_PRIVACY));
300 goto done;
301 }
302
303 /* bss security settings don't match those configured on card */
304 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
305 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
306 bss->wpa_ie[0], bss->rsn_ie[0],
307 priv->secinfo.wep_enabled ? "e" : "d",
308 priv->secinfo.WPAenabled ? "e" : "d",
309 priv->secinfo.WPA2enabled ? "e" : "d",
310 (bss->capability & WLAN_CAPABILITY_PRIVACY));
311
312done:
313 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
314 return matched;
315}
316
317/**
318 * @brief This function finds a specific compatible BSSID in the scan list
319 *
320 * Used in association code
321 *
322 * @param priv A pointer to struct lbs_private
323 * @param bssid BSSID to find in the scan list
324 * @param mode Network mode: Infrastructure or IBSS
325 *
326 * @return index in BSSID list, or error return code (< 0)
327 */
328static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
329 uint8_t *bssid, uint8_t mode)
330{
331 struct bss_descriptor *iter_bss;
332 struct bss_descriptor *found_bss = NULL;
333
334 lbs_deb_enter(LBS_DEB_SCAN);
335
336 if (!bssid)
337 goto out;
338
339 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
340
341 /* Look through the scan table for a compatible match. The loop will
342 * continue past a matched bssid that is not compatible in case there
343 * is an AP with multiple SSIDs assigned to the same BSSID
344 */
345 mutex_lock(&priv->lock);
346 list_for_each_entry(iter_bss, &priv->network_list, list) {
347 if (compare_ether_addr(iter_bss->bssid, bssid))
348 continue; /* bssid doesn't match */
349 switch (mode) {
350 case IW_MODE_INFRA:
351 case IW_MODE_ADHOC:
352 if (!is_network_compatible(priv, iter_bss, mode))
353 break;
354 found_bss = iter_bss;
355 break;
356 default:
357 found_bss = iter_bss;
358 break;
359 }
360 }
361 mutex_unlock(&priv->lock);
362
363out:
364 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
365 return found_bss;
366}
367
368/**
369 * @brief This function finds ssid in ssid list.
370 *
371 * Used in association code
372 *
373 * @param priv A pointer to struct lbs_private
374 * @param ssid SSID to find in the list
375 * @param bssid BSSID to qualify the SSID selection (if provided)
376 * @param mode Network mode: Infrastructure or IBSS
377 *
378 * @return index in BSSID list
379 */
380static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
381 uint8_t *ssid, uint8_t ssid_len,
382 uint8_t *bssid, uint8_t mode,
383 int channel)
384{
385 u32 bestrssi = 0;
386 struct bss_descriptor *iter_bss = NULL;
387 struct bss_descriptor *found_bss = NULL;
388 struct bss_descriptor *tmp_oldest = NULL;
389
390 lbs_deb_enter(LBS_DEB_SCAN);
391
392 mutex_lock(&priv->lock);
393
394 list_for_each_entry(iter_bss, &priv->network_list, list) {
395 if (!tmp_oldest ||
396 (iter_bss->last_scanned < tmp_oldest->last_scanned))
397 tmp_oldest = iter_bss;
398
399 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
400 ssid, ssid_len) != 0)
401 continue; /* ssid doesn't match */
402 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
403 continue; /* bssid doesn't match */
404 if ((channel > 0) && (iter_bss->channel != channel))
405 continue; /* channel doesn't match */
406
407 switch (mode) {
408 case IW_MODE_INFRA:
409 case IW_MODE_ADHOC:
410 if (!is_network_compatible(priv, iter_bss, mode))
411 break;
412
413 if (bssid) {
414 /* Found requested BSSID */
415 found_bss = iter_bss;
416 goto out;
417 }
418
419 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
420 bestrssi = SCAN_RSSI(iter_bss->rssi);
421 found_bss = iter_bss;
422 }
423 break;
424 case IW_MODE_AUTO:
425 default:
426 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
427 bestrssi = SCAN_RSSI(iter_bss->rssi);
428 found_bss = iter_bss;
429 }
430 break;
431 }
432 }
433
434out:
435 mutex_unlock(&priv->lock);
436 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
437 return found_bss;
438}
439
Holger Schurig69f90322007-11-23 15:43:44 +0100440static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200441 struct assoc_request * assoc_req)
442{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200443 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400444 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400445 int channel = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446
Holger Schurig9012b282007-05-25 11:27:16 -0400447 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448
Dan Williamsef9a2642007-05-25 16:46:33 -0400449 /* FIXME: take channel into account when picking SSIDs if a channel
450 * is set.
451 */
452
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400453 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
454 channel = assoc_req->channel;
455
Holger Schurig0765af42007-10-15 12:55:56 +0200456 lbs_deb_assoc("SSID '%s' requested\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400457 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -0400458 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500459 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100460 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200461
David Woodhouseaa21c002007-12-08 20:04:36 +0000462 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400463 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400464 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -0400465 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -0500466 ret = lbs_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200467 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -0400468 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400470 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200471 /* Scan for the network, do not save previous results. Stale
472 * scan data will cause us to join a non-existant adhoc network
473 */
Holger Schurig10078322007-11-15 18:05:47 -0500474 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100475 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476
477 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000478 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400479 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400480 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -0400481 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400482 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -0500483 lbs_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484 } else {
485 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -0400486 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400487 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400488 IW_ESSID_MAX_SIZE);
489 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Holger Schurig10078322007-11-15 18:05:47 -0500490 lbs_start_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200492 }
493
Holger Schurig9012b282007-05-25 11:27:16 -0400494 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 return ret;
496}
497
498
Holger Schurig69f90322007-11-23 15:43:44 +0100499static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500 struct assoc_request * assoc_req)
501{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400502 int ret = 0;
503 struct bss_descriptor * bss;
Joe Perches0795af52007-10-03 17:59:30 -0700504 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200505
Joe Perches0795af52007-10-03 17:59:30 -0700506 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
507 print_mac(mac, assoc_req->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200508
509 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +0000510 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400512 if (bss == NULL) {
Joe Perches0795af52007-10-03 17:59:30 -0700513 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
514 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200515 goto out;
516 }
517
Dan Williamse76850d2007-05-25 17:09:41 -0400518 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400519 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500520 ret = lbs_associate(priv, assoc_req);
521 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400522 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Holger Schurig10078322007-11-15 18:05:47 -0500523 lbs_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525
526out:
Holger Schurig9012b282007-05-25 11:27:16 -0400527 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528 return ret;
529}
530
531
Holger Schurig69f90322007-11-23 15:43:44 +0100532static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 struct assoc_request * assoc_req)
534{
535 int ret = 0, done = 0;
536
Holger Schurig0765af42007-10-15 12:55:56 +0200537 lbs_deb_enter(LBS_DEB_ASSOC);
538
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539 /* If we're given and 'any' BSSID, try associating based on SSID */
540
541 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400542 if (compare_ether_addr(bssid_any, assoc_req->bssid)
543 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544 ret = assoc_helper_bssid(priv, assoc_req);
545 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546 }
547 }
548
549 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
550 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551 }
552
Holger Schurig0765af42007-10-15 12:55:56 +0200553 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554 return ret;
555}
556
557
Holger Schurig69f90322007-11-23 15:43:44 +0100558static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 struct assoc_request * assoc_req)
560{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561 int ret = 0;
562
Holger Schurig9012b282007-05-25 11:27:16 -0400563 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200564
David Woodhouseaa21c002007-12-08 20:04:36 +0000565 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -0400566 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567
Dan Williams0dc5a292007-05-10 22:58:02 -0400568 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000569 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -0500570 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +0000571 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572 }
573
David Woodhouseaa21c002007-12-08 20:04:36 +0000574 priv->mode = assoc_req->mode;
Holger Schurig10078322007-11-15 18:05:47 -0500575 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400576 CMD_802_11_SNMP_MIB,
577 0, CMD_OPTION_WAITFORRSP,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578 OID_802_11_INFRASTRUCTURE_MODE,
David Woodhouse981f1872007-05-25 23:36:54 -0400579 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200580
Holger Schurig9012b282007-05-25 11:27:16 -0400581done:
582 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583 return ret;
584}
585
586
David Woodhouse9f462572007-12-12 22:50:21 -0500587int lbs_update_channel(struct lbs_private *priv)
Dan Williamsef9a2642007-05-25 16:46:33 -0400588{
Holger Schurig0765af42007-10-15 12:55:56 +0200589 int ret;
Dan Williams2dd4b262007-12-11 16:54:15 -0500590
David Woodhoused1a469f2007-12-16 17:21:00 -0500591 /* the channel in f/w could be out of sync; get the current channel */
Holger Schurig0765af42007-10-15 12:55:56 +0200592 lbs_deb_enter(LBS_DEB_ASSOC);
Dan Williams2dd4b262007-12-11 16:54:15 -0500593
594 ret = lbs_get_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500595 if (ret > 0) {
596 priv->curbssparams.channel = ret;
597 ret = 0;
598 }
Holger Schurig0765af42007-10-15 12:55:56 +0200599 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
600 return ret;
Dan Williamsef9a2642007-05-25 16:46:33 -0400601}
602
Holger Schurig69f90322007-11-23 15:43:44 +0100603static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -0400604 struct assoc_request * assoc_req)
605{
Dan Williamsef9a2642007-05-25 16:46:33 -0400606 int ret = 0;
607
608 lbs_deb_enter(LBS_DEB_ASSOC);
609
David Woodhouse9f462572007-12-12 22:50:21 -0500610 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500611 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500612 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500613 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -0400614 }
615
David Woodhouseaa21c002007-12-08 20:04:36 +0000616 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400617 goto done;
618
David Woodhouse8642f1f2007-12-11 20:03:01 -0500619 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -0500620 /* Change mesh channel first; 21.p21 firmware won't let
621 you change channel otherwise (even though it'll return
622 an error to this */
623 lbs_mesh_config(priv, 0, assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500624 }
625
Dan Williamsef9a2642007-05-25 16:46:33 -0400626 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -0500627 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -0400628
Dan Williams2dd4b262007-12-11 16:54:15 -0500629 ret = lbs_set_channel(priv, assoc_req->channel);
630 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -0500631 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -0400632
Dan Williams2dd4b262007-12-11 16:54:15 -0500633 /* FIXME: shouldn't need to grab the channel _again_ after setting
634 * it since the firmware is supposed to return the new channel, but
635 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -0500636 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500637 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500638 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500639 goto done;
640 }
Dan Williamsef9a2642007-05-25 16:46:33 -0400641
David Woodhouseaa21c002007-12-08 20:04:36 +0000642 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -0500643 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -0400644 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500645 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -0400646 }
647
648 if ( assoc_req->secinfo.wep_enabled
649 && (assoc_req->wep_keys[0].len
650 || assoc_req->wep_keys[1].len
651 || assoc_req->wep_keys[2].len
652 || assoc_req->wep_keys[3].len)) {
653 /* Make sure WEP keys are re-sent to firmware */
654 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
655 }
656
657 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -0500658 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -0400659
David Woodhouse8642f1f2007-12-11 20:03:01 -0500660 restore_mesh:
661 if (priv->mesh_dev)
David Woodhouse86062132007-12-13 00:32:36 -0500662 lbs_mesh_config(priv, 1, priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500663
664 done:
Dan Williamsef9a2642007-05-25 16:46:33 -0400665 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
666 return ret;
667}
668
669
Holger Schurig69f90322007-11-23 15:43:44 +0100670static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -0500671 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200672{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200673 int i;
674 int ret = 0;
675
Holger Schurig9012b282007-05-25 11:27:16 -0400676 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200677
678 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -0500679 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
680 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
681 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
682 else
683 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200684
685 if (ret)
686 goto out;
687
688 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400689 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +0100690 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691 else
Holger Schurigd9e97782008-03-12 16:06:43 +0100692 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -0500693
Holger Schurigc97329e2008-03-18 11:20:21 +0100694 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695
David Woodhouseaa21c002007-12-08 20:04:36 +0000696 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697
David Woodhouseaa21c002007-12-08 20:04:36 +0000698 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000700 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -0500701 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000703 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704
David Woodhouseaa21c002007-12-08 20:04:36 +0000705 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706
707out:
Holger Schurig9012b282007-05-25 11:27:16 -0400708 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709 return ret;
710}
711
Holger Schurig69f90322007-11-23 15:43:44 +0100712static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200713 struct assoc_request * assoc_req)
714{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -0500716 uint16_t do_wpa;
717 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200718
Holger Schurig9012b282007-05-25 11:27:16 -0400719 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720
David Woodhouseaa21c002007-12-08 20:04:36 +0000721 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -0500722 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200723
Holger Schurigc97329e2008-03-18 11:20:21 +0100724 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200725
Dan Williams18c96c342007-06-18 12:01:12 -0400726 /* If RSN is already enabled, don't try to enable it again, since
727 * ENABLE_RSN resets internal state machines and will clobber the
728 * 4-way WPA handshake.
729 */
730
731 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500732 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -0400733 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500734 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -0400735 goto out;
736 }
737
738 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500739 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -0400740 if (do_wpa == rsn)
741 goto out;
742
743 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500744 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -0400745
746out:
Holger Schurig9012b282007-05-25 11:27:16 -0400747 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748 return ret;
749}
750
751
Holger Schurig69f90322007-11-23 15:43:44 +0100752static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753 struct assoc_request * assoc_req)
754{
755 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -0400756 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200757
Holger Schurig9012b282007-05-25 11:27:16 -0400758 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759
Dan Williams2bcde512007-10-03 10:37:45 -0400760 /* Work around older firmware bug where WPA unicast and multicast
761 * keys must be set independently. Seen in SDIO parts with firmware
762 * version 5.0.11p0.
763 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764
Dan Williams2bcde512007-10-03 10:37:45 -0400765 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
766 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +0100767 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -0400768 assoc_req->flags = flags;
769 }
770
771 if (ret)
772 goto out;
773
774 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
775 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
776
David Woodhouse9e1228d2008-03-03 12:15:39 +0100777 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -0400778 assoc_req->flags = flags;
779 }
780
781out:
Holger Schurig9012b282007-05-25 11:27:16 -0400782 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200783 return ret;
784}
785
786
Holger Schurig69f90322007-11-23 15:43:44 +0100787static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200788 struct assoc_request * assoc_req)
789{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790 int ret = 0;
791
Holger Schurig9012b282007-05-25 11:27:16 -0400792 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200793
794 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000795 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
796 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000798 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
799 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200800 }
801
Holger Schurig9012b282007-05-25 11:27:16 -0400802 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803 return ret;
804}
805
806
David Woodhouseaa21c002007-12-08 20:04:36 +0000807static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200808 struct assoc_request * assoc_req)
809{
Holger Schurig0765af42007-10-15 12:55:56 +0200810 int ret = 0;
811
David Woodhouseaa21c002007-12-08 20:04:36 +0000812 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813 return 0;
814
Holger Schurig52507c22008-01-28 17:25:53 +0100815 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200816 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200817 lbs_deb_assoc("Deauthenticating due to new SSID\n");
818 ret = 1;
819 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200820 }
821
822 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000823 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +0200824 lbs_deb_assoc("Deauthenticating due to new security\n");
825 ret = 1;
826 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200827 }
828 }
829
830 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200831 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
832 ret = 1;
833 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200834 }
835
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -0400836 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200837 lbs_deb_assoc("Deauthenticating due to channel switch\n");
838 ret = 1;
839 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -0400840 }
841
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200842 /* FIXME: deal with 'auto' mode somehow */
843 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200844 if (assoc_req->mode != IW_MODE_INFRA) {
845 lbs_deb_assoc("Deauthenticating due to leaving "
846 "infra mode\n");
847 ret = 1;
848 goto out;
849 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200850 }
851
Holger Schurig0765af42007-10-15 12:55:56 +0200852out:
853 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +0100854 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200855}
856
857
David Woodhouseaa21c002007-12-08 20:04:36 +0000858static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200859 struct assoc_request * assoc_req)
860{
Holger Schurig0765af42007-10-15 12:55:56 +0200861 lbs_deb_enter(LBS_DEB_ASSOC);
862
David Woodhouseaa21c002007-12-08 20:04:36 +0000863 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200864 return 0;
865
David Woodhouseaa21c002007-12-08 20:04:36 +0000866 if (lbs_ssid_cmp(priv->curbssparams.ssid,
867 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -0400868 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200869 return 1;
870
871 /* FIXME: deal with 'auto' mode somehow */
872 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400873 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200874 return 1;
875 }
876
Dan Williamsef9a2642007-05-25 16:46:33 -0400877 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000878 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400879 return 1;
880 }
881
Holger Schurig0765af42007-10-15 12:55:56 +0200882 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200883 return 0;
884}
885
886
Holger Schurig245bf202008-04-02 16:27:42 +0200887/**
888 * @brief This function finds the best SSID in the Scan List
889 *
890 * Search the scan table for the best SSID that also matches the current
891 * adapter network preference (infrastructure or adhoc)
892 *
893 * @param priv A pointer to struct lbs_private
894 *
895 * @return index in BSSID list
896 */
897static struct bss_descriptor *lbs_find_best_ssid_in_list(
898 struct lbs_private *priv, uint8_t mode)
899{
900 uint8_t bestrssi = 0;
901 struct bss_descriptor *iter_bss;
902 struct bss_descriptor *best_bss = NULL;
903
904 lbs_deb_enter(LBS_DEB_SCAN);
905
906 mutex_lock(&priv->lock);
907
908 list_for_each_entry(iter_bss, &priv->network_list, list) {
909 switch (mode) {
910 case IW_MODE_INFRA:
911 case IW_MODE_ADHOC:
912 if (!is_network_compatible(priv, iter_bss, mode))
913 break;
914 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
915 break;
916 bestrssi = SCAN_RSSI(iter_bss->rssi);
917 best_bss = iter_bss;
918 break;
919 case IW_MODE_AUTO:
920 default:
921 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
922 break;
923 bestrssi = SCAN_RSSI(iter_bss->rssi);
924 best_bss = iter_bss;
925 break;
926 }
927 }
928
929 mutex_unlock(&priv->lock);
930 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
931 return best_bss;
932}
933
934/**
935 * @brief Find the best AP
936 *
937 * Used from association worker.
938 *
939 * @param priv A pointer to struct lbs_private structure
940 * @param pSSID A pointer to AP's ssid
941 *
942 * @return 0--success, otherwise--fail
943 */
944static int lbs_find_best_network_ssid(struct lbs_private *priv,
945 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
946 uint8_t *out_mode)
947{
948 int ret = -1;
949 struct bss_descriptor *found;
950
951 lbs_deb_enter(LBS_DEB_SCAN);
952
953 priv->scan_ssid_len = 0;
954 lbs_scan_networks(priv, 1);
955 if (priv->surpriseremoved)
956 goto out;
957
958 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
959 if (found && (found->ssid_len > 0)) {
960 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
961 *out_ssid_len = found->ssid_len;
962 *out_mode = found->mode;
963 ret = 0;
964 }
965
966out:
967 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
968 return ret;
969}
970
971
Holger Schurig10078322007-11-15 18:05:47 -0500972void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200973{
Holger Schurig69f90322007-11-23 15:43:44 +0100974 struct lbs_private *priv = container_of(work, struct lbs_private,
975 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976 struct assoc_request * assoc_req = NULL;
977 int ret = 0;
978 int find_any_ssid = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700979 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200980
Holger Schurig9012b282007-05-25 11:27:16 -0400981 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200982
David Woodhouseaa21c002007-12-08 20:04:36 +0000983 mutex_lock(&priv->lock);
984 assoc_req = priv->pending_assoc_req;
985 priv->pending_assoc_req = NULL;
986 priv->in_progress_assoc_req = assoc_req;
987 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988
Holger Schurig9012b282007-05-25 11:27:16 -0400989 if (!assoc_req)
990 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200991
Holger Schurig0765af42007-10-15 12:55:56 +0200992 lbs_deb_assoc(
993 "Association Request:\n"
994 " flags: 0x%08lx\n"
995 " SSID: '%s'\n"
996 " chann: %d\n"
997 " band: %d\n"
998 " mode: %d\n"
999 " BSSID: %s\n"
1000 " secinfo: %s%s%s\n"
1001 " auth_mode: %d\n",
1002 assoc_req->flags,
1003 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
1004 assoc_req->channel, assoc_req->band, assoc_req->mode,
1005 print_mac(mac, assoc_req->bssid),
1006 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1007 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1008 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1009 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010
1011 /* If 'any' SSID was specified, find an SSID to associate with */
1012 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001013 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014 find_any_ssid = 1;
1015
1016 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1017 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -04001018 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1019 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 find_any_ssid = 0;
1021 }
1022
1023 if (find_any_ssid) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001024 u8 new_mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001025
Holger Schurig10078322007-11-15 18:05:47 -05001026 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001027 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001029 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001030 ret = -ENETUNREACH;
1031 goto out;
1032 }
1033
1034 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001035 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1037 assoc_req->mode = new_mode;
1038 }
1039 }
1040
1041 /*
1042 * Check if the attributes being changing require deauthentication
1043 * from the currently associated infrastructure access point.
1044 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001045 if (priv->mode == IW_MODE_INFRA) {
1046 if (should_deauth_infrastructure(priv, assoc_req)) {
Holger Schurig10078322007-11-15 18:05:47 -05001047 ret = lbs_send_deauthentication(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001048 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001049 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050 "configuration request failed: %d\n",
1051 ret);
1052 }
1053 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001054 } else if (priv->mode == IW_MODE_ADHOC) {
1055 if (should_stop_adhoc(priv, assoc_req)) {
Holger Schurig10078322007-11-15 18:05:47 -05001056 ret = lbs_stop_adhoc_network(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001057 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001058 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059 "new configuration request failed: %d\n",
1060 ret);
1061 }
1062
1063 }
1064 }
1065
1066 /* Send the various configuration bits to the firmware */
1067 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1068 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001069 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001070 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001071 }
1072
Dan Williamsef9a2642007-05-25 16:46:33 -04001073 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1074 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001075 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001076 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001077 }
1078
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001079 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1080 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1081 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001082 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001084 }
1085
1086 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1087 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001088 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001089 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090 }
1091
1092 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1093 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001094 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001096 }
1097
1098 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1099 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1100 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001101 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001102 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 }
1104
1105 /* SSID/BSSID should be the _last_ config option set, because they
1106 * trigger the association attempt.
1107 */
1108 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1109 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1110 int success = 1;
1111
1112 ret = assoc_helper_associate(priv, assoc_req);
1113 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001114 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115 ret);
1116 success = 0;
1117 }
1118
David Woodhouseaa21c002007-12-08 20:04:36 +00001119 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001120 lbs_deb_assoc("ASSOC: association unsuccessful, "
1121 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001122 success = 0;
1123 }
1124
1125 if (success) {
Holger Schurig52507c22008-01-28 17:25:53 +01001126 lbs_deb_assoc("associated to %s\n",
David Woodhouseaa21c002007-12-08 20:04:36 +00001127 print_mac(mac, priv->curbssparams.bssid));
Holger Schurig10078322007-11-15 18:05:47 -05001128 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001129 CMD_802_11_RSSI,
1130 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001131 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001132 ret = -1;
1133 }
1134 }
1135
1136out:
1137 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001138 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001139 ret);
1140 }
Dan Williamse76850d2007-05-25 17:09:41 -04001141
David Woodhouseaa21c002007-12-08 20:04:36 +00001142 mutex_lock(&priv->lock);
1143 priv->in_progress_assoc_req = NULL;
1144 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001145 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001146
1147done:
1148 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149}
1150
1151
1152/*
1153 * Caller MUST hold any necessary locks
1154 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001155struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001156{
1157 struct assoc_request * assoc_req;
1158
Holger Schurig0765af42007-10-15 12:55:56 +02001159 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001160 if (!priv->pending_assoc_req) {
1161 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001162 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001163 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 lbs_pr_info("Not enough memory to allocate association"
1165 " request!\n");
1166 return NULL;
1167 }
1168 }
1169
1170 /* Copy current configuration attributes to the association request,
1171 * but don't overwrite any that are already set.
1172 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001173 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001174 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001175 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001176 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001177 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001178 }
1179
1180 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001181 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001182
Dan Williamse76850d2007-05-25 17:09:41 -04001183 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001184 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001185
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001186 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001187 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001188
1189 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001190 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001191 ETH_ALEN);
1192 }
1193
1194 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1195 int i;
1196 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001197 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001198 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001199 }
1200 }
1201
1202 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001203 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001204
1205 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001206 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001207 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001208 }
1209
1210 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001211 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001212 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001213 }
1214
1215 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001216 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001217 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001218 }
1219
1220 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001221 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001222 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001223 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001224 }
1225
Holger Schurig0765af42007-10-15 12:55:56 +02001226 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001227 return assoc_req;
1228}
Holger Schurig697900a2008-04-02 16:27:10 +02001229
1230
1231/**
1232 * @brief This function finds common rates between rate1 and card rates.
1233 *
1234 * It will fill common rates in rate1 as output if found.
1235 *
1236 * NOTE: Setting the MSB of the basic rates need to be taken
1237 * care, either before or after calling this function
1238 *
1239 * @param priv A pointer to struct lbs_private structure
1240 * @param rate1 the buffer which keeps input and output
1241 * @param rate1_size the size of rate1 buffer; new size of buffer on return
1242 *
1243 * @return 0 or -1
1244 */
1245static int get_common_rates(struct lbs_private *priv,
1246 u8 *rates,
1247 u16 *rates_size)
1248{
1249 u8 *card_rates = lbs_bg_rates;
1250 size_t num_card_rates = sizeof(lbs_bg_rates);
1251 int ret = 0, i, j;
1252 u8 tmp[30];
1253 size_t tmp_size = 0;
1254
1255 /* For each rate in card_rates that exists in rate1, copy to tmp */
1256 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
1257 for (j = 0; rates[j] && (j < *rates_size); j++) {
1258 if (rates[j] == card_rates[i])
1259 tmp[tmp_size++] = card_rates[i];
1260 }
1261 }
1262
1263 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
1264 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
1265 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
1266 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
1267
1268 if (!priv->auto_rate) {
1269 for (i = 0; i < tmp_size; i++) {
1270 if (tmp[i] == priv->cur_rate)
1271 goto done;
1272 }
1273 lbs_pr_alert("Previously set fixed data rate %#x isn't "
1274 "compatible with the network.\n", priv->cur_rate);
1275 ret = -1;
1276 goto done;
1277 }
1278 ret = 0;
1279
1280done:
1281 memset(rates, 0, *rates_size);
1282 *rates_size = min_t(int, tmp_size, *rates_size);
1283 memcpy(rates, tmp, *rates_size);
1284 return ret;
1285}
1286
1287
1288/**
1289 * @brief Sets the MSB on basic rates as the firmware requires
1290 *
1291 * Scan through an array and set the MSB for basic data rates.
1292 *
1293 * @param rates buffer of data rates
1294 * @param len size of buffer
1295 */
1296static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
1297{
1298 int i;
1299
1300 for (i = 0; i < len; i++) {
1301 if (rates[i] == 0x02 || rates[i] == 0x04 ||
1302 rates[i] == 0x0b || rates[i] == 0x16)
1303 rates[i] |= 0x80;
1304 }
1305}
1306
1307/**
1308 * @brief Send Deauthentication Request
1309 *
1310 * @param priv A pointer to struct lbs_private structure
1311 * @return 0--success, -1--fail
1312 */
1313int lbs_send_deauthentication(struct lbs_private *priv)
1314{
1315 return lbs_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
1316 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1317}
1318
1319/**
1320 * @brief This function prepares command of authenticate.
1321 *
1322 * @param priv A pointer to struct lbs_private structure
1323 * @param cmd A pointer to cmd_ds_command structure
1324 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
1325 *
1326 * @return 0 or -1
1327 */
1328int lbs_cmd_80211_authenticate(struct lbs_private *priv,
1329 struct cmd_ds_command *cmd,
1330 void *pdata_buf)
1331{
1332 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
1333 int ret = -1;
1334 u8 *bssid = pdata_buf;
1335 DECLARE_MAC_BUF(mac);
1336
1337 lbs_deb_enter(LBS_DEB_JOIN);
1338
1339 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
1340 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
1341 + S_DS_GEN);
1342
1343 /* translate auth mode to 802.11 defined wire value */
1344 switch (priv->secinfo.auth_mode) {
1345 case IW_AUTH_ALG_OPEN_SYSTEM:
1346 pauthenticate->authtype = 0x00;
1347 break;
1348 case IW_AUTH_ALG_SHARED_KEY:
1349 pauthenticate->authtype = 0x01;
1350 break;
1351 case IW_AUTH_ALG_LEAP:
1352 pauthenticate->authtype = 0x80;
1353 break;
1354 default:
1355 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
1356 priv->secinfo.auth_mode);
1357 goto out;
1358 }
1359
1360 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
1361
1362 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
1363 print_mac(mac, bssid), pauthenticate->authtype);
1364 ret = 0;
1365
1366out:
1367 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1368 return ret;
1369}
1370
1371int lbs_cmd_80211_deauthenticate(struct lbs_private *priv,
1372 struct cmd_ds_command *cmd)
1373{
1374 struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
1375
1376 lbs_deb_enter(LBS_DEB_JOIN);
1377
1378 cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
1379 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
1380 S_DS_GEN);
1381
1382 /* set AP MAC address */
1383 memmove(dauth->macaddr, priv->curbssparams.bssid, ETH_ALEN);
1384
1385 /* Reason code 3 = Station is leaving */
1386#define REASON_CODE_STA_LEAVING 3
1387 dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
1388
1389 lbs_deb_leave(LBS_DEB_JOIN);
1390 return 0;
1391}
1392
1393int lbs_cmd_80211_associate(struct lbs_private *priv,
1394 struct cmd_ds_command *cmd, void *pdata_buf)
1395{
1396 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1397 int ret = 0;
1398 struct assoc_request *assoc_req = pdata_buf;
1399 struct bss_descriptor *bss = &assoc_req->bss;
1400 u8 *pos;
1401 u16 tmpcap, tmplen;
1402 struct mrvlietypes_ssidparamset *ssid;
1403 struct mrvlietypes_phyparamset *phy;
1404 struct mrvlietypes_ssparamset *ss;
1405 struct mrvlietypes_ratesparamset *rates;
1406 struct mrvlietypes_rsnparamset *rsn;
1407
1408 lbs_deb_enter(LBS_DEB_ASSOC);
1409
1410 pos = (u8 *) passo;
1411
1412 if (!priv) {
1413 ret = -1;
1414 goto done;
1415 }
1416
1417 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1418
1419 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1420 pos += sizeof(passo->peerstaaddr);
1421
1422 /* set the listen interval */
1423 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1424
1425 pos += sizeof(passo->capability);
1426 pos += sizeof(passo->listeninterval);
1427 pos += sizeof(passo->bcnperiod);
1428 pos += sizeof(passo->dtimperiod);
1429
1430 ssid = (struct mrvlietypes_ssidparamset *) pos;
1431 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1432 tmplen = bss->ssid_len;
1433 ssid->header.len = cpu_to_le16(tmplen);
1434 memcpy(ssid->ssid, bss->ssid, tmplen);
1435 pos += sizeof(ssid->header) + tmplen;
1436
1437 phy = (struct mrvlietypes_phyparamset *) pos;
1438 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1439 tmplen = sizeof(phy->fh_ds.dsparamset);
1440 phy->header.len = cpu_to_le16(tmplen);
1441 memcpy(&phy->fh_ds.dsparamset,
1442 &bss->phyparamset.dsparamset.currentchan,
1443 tmplen);
1444 pos += sizeof(phy->header) + tmplen;
1445
1446 ss = (struct mrvlietypes_ssparamset *) pos;
1447 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
1448 tmplen = sizeof(ss->cf_ibss.cfparamset);
1449 ss->header.len = cpu_to_le16(tmplen);
1450 pos += sizeof(ss->header) + tmplen;
1451
1452 rates = (struct mrvlietypes_ratesparamset *) pos;
1453 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1454 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1455 tmplen = MAX_RATES;
1456 if (get_common_rates(priv, rates->rates, &tmplen)) {
1457 ret = -1;
1458 goto done;
1459 }
1460 pos += sizeof(rates->header) + tmplen;
1461 rates->header.len = cpu_to_le16(tmplen);
1462 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1463
1464 /* Copy the infra. association rates into Current BSS state structure */
1465 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1466 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1467
1468 /* Set MSB on basic rates as the firmware requires, but _after_
1469 * copying to current bss rates.
1470 */
1471 lbs_set_basic_rate_flags(rates->rates, tmplen);
1472
1473 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1474 rsn = (struct mrvlietypes_rsnparamset *) pos;
1475 /* WPA_IE or WPA2_IE */
1476 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1477 tmplen = (u16) assoc_req->wpa_ie[1];
1478 rsn->header.len = cpu_to_le16(tmplen);
1479 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1480 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1481 sizeof(rsn->header) + tmplen);
1482 pos += sizeof(rsn->header) + tmplen;
1483 }
1484
1485 /* update curbssparams */
1486 priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
1487
1488 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1489 ret = -1;
1490 goto done;
1491 }
1492
1493 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1494
1495 /* set the capability info */
1496 tmpcap = (bss->capability & CAPINFO_MASK);
1497 if (bss->mode == IW_MODE_INFRA)
1498 tmpcap |= WLAN_CAPABILITY_ESS;
1499 passo->capability = cpu_to_le16(tmpcap);
1500 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1501
1502done:
1503 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1504 return ret;
1505}
1506
1507int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
1508 struct cmd_ds_command *cmd, void *pdata_buf)
1509{
1510 struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
1511 int ret = 0;
1512 int cmdappendsize = 0;
1513 struct assoc_request *assoc_req = pdata_buf;
1514 u16 tmpcap = 0;
1515 size_t ratesize = 0;
1516
1517 lbs_deb_enter(LBS_DEB_JOIN);
1518
1519 if (!priv) {
1520 ret = -1;
1521 goto done;
1522 }
1523
1524 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
1525
1526 /*
1527 * Fill in the parameters for 2 data structures:
1528 * 1. cmd_ds_802_11_ad_hoc_start command
1529 * 2. priv->scantable[i]
1530 *
1531 * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
1532 * probe delay, and cap info.
1533 *
1534 * Firmware will fill up beacon period, DTIM, Basic rates
1535 * and operational rates.
1536 */
1537
1538 memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
1539 memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
1540
1541 lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
1542 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
1543 assoc_req->ssid_len);
1544
1545 /* set the BSS type */
1546 adhs->bsstype = CMD_BSS_TYPE_IBSS;
1547 priv->mode = IW_MODE_ADHOC;
1548 if (priv->beacon_period == 0)
1549 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
1550 adhs->beaconperiod = cpu_to_le16(priv->beacon_period);
1551
1552 /* set Physical param set */
1553#define DS_PARA_IE_ID 3
1554#define DS_PARA_IE_LEN 1
1555
1556 adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
1557 adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
1558
1559 WARN_ON(!assoc_req->channel);
1560
1561 lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
1562 assoc_req->channel);
1563
1564 adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
1565
1566 /* set IBSS param set */
1567#define IBSS_PARA_IE_ID 6
1568#define IBSS_PARA_IE_LEN 2
1569
1570 adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
1571 adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
1572 adhs->ssparamset.ibssparamset.atimwindow = 0;
1573
1574 /* set capability info */
1575 tmpcap = WLAN_CAPABILITY_IBSS;
1576 if (assoc_req->secinfo.wep_enabled) {
1577 lbs_deb_join("ADHOC_S_CMD: WEP enabled, "
1578 "setting privacy on\n");
1579 tmpcap |= WLAN_CAPABILITY_PRIVACY;
1580 } else {
1581 lbs_deb_join("ADHOC_S_CMD: WEP disabled, "
1582 "setting privacy off\n");
1583 }
1584 adhs->capability = cpu_to_le16(tmpcap);
1585
1586 /* probedelay */
1587 adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1588
1589 memset(adhs->rates, 0, sizeof(adhs->rates));
1590 ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
1591 memcpy(adhs->rates, lbs_bg_rates, ratesize);
1592
1593 /* Copy the ad-hoc creating rates into Current BSS state structure */
1594 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1595 memcpy(&priv->curbssparams.rates, &adhs->rates, ratesize);
1596
1597 /* Set MSB on basic rates as the firmware requires, but _after_
1598 * copying to current bss rates.
1599 */
1600 lbs_set_basic_rate_flags(adhs->rates, ratesize);
1601
1602 lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
1603 adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
1604
1605 lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
1606
1607 if (lbs_create_dnld_countryinfo_11d(priv)) {
1608 lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
1609 ret = -1;
1610 goto done;
1611 }
1612
1613 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
1614 S_DS_GEN + cmdappendsize);
1615
1616 ret = 0;
1617done:
1618 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1619 return ret;
1620}
1621
1622int lbs_cmd_80211_ad_hoc_stop(struct cmd_ds_command *cmd)
1623{
1624 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
1625 cmd->size = cpu_to_le16(S_DS_GEN);
1626
1627 return 0;
1628}
1629
1630int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
1631 struct cmd_ds_command *cmd, void *pdata_buf)
1632{
1633 struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
1634 struct assoc_request *assoc_req = pdata_buf;
1635 struct bss_descriptor *bss = &assoc_req->bss;
1636 int cmdappendsize = 0;
1637 int ret = 0;
1638 u16 ratesize = 0;
1639 DECLARE_MAC_BUF(mac);
1640
1641 lbs_deb_enter(LBS_DEB_JOIN);
1642
1643 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
1644
1645 join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
1646 join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
1647
1648 memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
1649 memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
1650
1651 memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
1652 sizeof(union ieeetypes_phyparamset));
1653
1654 memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
1655 sizeof(union IEEEtypes_ssparamset));
1656
1657 join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1658 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
1659 bss->capability, CAPINFO_MASK);
1660
1661 /* information on BSSID descriptor passed to FW */
1662 lbs_deb_join(
1663 "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
1664 print_mac(mac, join_cmd->bss.bssid),
1665 join_cmd->bss.ssid);
1666
1667 /* failtimeout */
1668 join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1669
1670 /* probedelay */
1671 join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1672
1673 priv->curbssparams.channel = bss->channel;
1674
1675 /* Copy Data rates from the rates recorded in scan response */
1676 memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
1677 ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
1678 memcpy(join_cmd->bss.rates, bss->rates, ratesize);
1679 if (get_common_rates(priv, join_cmd->bss.rates, &ratesize)) {
1680 lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
1681 ret = -1;
1682 goto done;
1683 }
1684
1685 /* Copy the ad-hoc creating rates into Current BSS state structure */
1686 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1687 memcpy(&priv->curbssparams.rates, join_cmd->bss.rates, ratesize);
1688
1689 /* Set MSB on basic rates as the firmware requires, but _after_
1690 * copying to current bss rates.
1691 */
1692 lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
1693
1694 join_cmd->bss.ssparamset.ibssparamset.atimwindow =
1695 cpu_to_le16(bss->atimwindow);
1696
1697 if (assoc_req->secinfo.wep_enabled) {
1698 u16 tmp = le16_to_cpu(join_cmd->bss.capability);
1699 tmp |= WLAN_CAPABILITY_PRIVACY;
1700 join_cmd->bss.capability = cpu_to_le16(tmp);
1701 }
1702
1703 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1704 /* wake up first */
1705 __le32 Localpsmode;
1706
1707 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1708 ret = lbs_prepare_and_send_command(priv,
1709 CMD_802_11_PS_MODE,
1710 CMD_ACT_SET,
1711 0, 0, &Localpsmode);
1712
1713 if (ret) {
1714 ret = -1;
1715 goto done;
1716 }
1717 }
1718
1719 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1720 ret = -1;
1721 goto done;
1722 }
1723
1724 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
1725 S_DS_GEN + cmdappendsize);
1726
1727done:
1728 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1729 return ret;
1730}
1731
1732int lbs_ret_80211_associate(struct lbs_private *priv,
1733 struct cmd_ds_command *resp)
1734{
1735 int ret = 0;
1736 union iwreq_data wrqu;
1737 struct ieeetypes_assocrsp *passocrsp;
1738 struct bss_descriptor *bss;
1739 u16 status_code;
1740
1741 lbs_deb_enter(LBS_DEB_ASSOC);
1742
1743 if (!priv->in_progress_assoc_req) {
1744 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1745 ret = -1;
1746 goto done;
1747 }
1748 bss = &priv->in_progress_assoc_req->bss;
1749
1750 passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
1751
1752 /*
1753 * Older FW versions map the IEEE 802.11 Status Code in the association
1754 * response to the following values returned in passocrsp->statuscode:
1755 *
1756 * IEEE Status Code Marvell Status Code
1757 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1758 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1759 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1760 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1761 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1762 * others -> 0x0003 ASSOC_RESULT_REFUSED
1763 *
1764 * Other response codes:
1765 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1766 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1767 * association response from the AP)
1768 */
1769
1770 status_code = le16_to_cpu(passocrsp->statuscode);
1771 switch (status_code) {
1772 case 0x00:
1773 break;
1774 case 0x01:
1775 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1776 break;
1777 case 0x02:
1778 lbs_deb_assoc("ASSOC_RESP: internal timer "
1779 "expired while waiting for the AP\n");
1780 break;
1781 case 0x03:
1782 lbs_deb_assoc("ASSOC_RESP: association "
1783 "refused by AP\n");
1784 break;
1785 case 0x04:
1786 lbs_deb_assoc("ASSOC_RESP: authentication "
1787 "refused by AP\n");
1788 break;
1789 default:
1790 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1791 " unknown\n", status_code);
1792 break;
1793 }
1794
1795 if (status_code) {
1796 lbs_mac_event_disconnected(priv);
1797 ret = -1;
1798 goto done;
1799 }
1800
1801 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1802 le16_to_cpu(resp->size) - S_DS_GEN);
1803
1804 /* Send a Media Connected event, according to the Spec */
1805 priv->connect_status = LBS_CONNECTED;
1806
1807 /* Update current SSID and BSSID */
1808 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1809 priv->curbssparams.ssid_len = bss->ssid_len;
1810 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1811
1812 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1813 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1814
1815 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1816 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1817 priv->nextSNRNF = 0;
1818 priv->numSNRNF = 0;
1819
1820 netif_carrier_on(priv->dev);
1821 if (!priv->tx_pending_len)
1822 netif_wake_queue(priv->dev);
1823
1824 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1825 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1826 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1827
1828done:
1829 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1830 return ret;
1831}
1832
1833int lbs_ret_80211_disassociate(struct lbs_private *priv)
1834{
1835 lbs_deb_enter(LBS_DEB_JOIN);
1836
1837 lbs_mac_event_disconnected(priv);
1838
1839 lbs_deb_leave(LBS_DEB_JOIN);
1840 return 0;
1841}
1842
1843int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
1844 struct cmd_ds_command *resp)
1845{
1846 int ret = 0;
1847 u16 command = le16_to_cpu(resp->command);
1848 u16 result = le16_to_cpu(resp->result);
1849 struct cmd_ds_802_11_ad_hoc_result *padhocresult;
1850 union iwreq_data wrqu;
1851 struct bss_descriptor *bss;
1852 DECLARE_MAC_BUF(mac);
1853
1854 lbs_deb_enter(LBS_DEB_JOIN);
1855
1856 padhocresult = &resp->params.result;
1857
1858 lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
1859 lbs_deb_join("ADHOC_RESP: command = %x\n", command);
1860 lbs_deb_join("ADHOC_RESP: result = %x\n", result);
1861
1862 if (!priv->in_progress_assoc_req) {
1863 lbs_deb_join("ADHOC_RESP: no in-progress association "
1864 "request\n");
1865 ret = -1;
1866 goto done;
1867 }
1868 bss = &priv->in_progress_assoc_req->bss;
1869
1870 /*
1871 * Join result code 0 --> SUCCESS
1872 */
1873 if (result) {
1874 lbs_deb_join("ADHOC_RESP: failed\n");
1875 if (priv->connect_status == LBS_CONNECTED)
1876 lbs_mac_event_disconnected(priv);
1877 ret = -1;
1878 goto done;
1879 }
1880
1881 /*
1882 * Now the join cmd should be successful
1883 * If BSSID has changed use SSID to compare instead of BSSID
1884 */
1885 lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
1886 escape_essid(bss->ssid, bss->ssid_len));
1887
1888 /* Send a Media Connected event, according to the Spec */
1889 priv->connect_status = LBS_CONNECTED;
1890
1891 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1892 /* Update the created network descriptor with the new BSSID */
1893 memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
1894 }
1895
1896 /* Set the BSSID from the joined/started descriptor */
1897 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1898
1899 /* Set the new SSID to current SSID */
1900 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1901 priv->curbssparams.ssid_len = bss->ssid_len;
1902
1903 netif_carrier_on(priv->dev);
1904 if (!priv->tx_pending_len)
1905 netif_wake_queue(priv->dev);
1906
1907 memset(&wrqu, 0, sizeof(wrqu));
1908 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1909 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1910 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1911
1912 lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
1913 lbs_deb_join("ADHOC_RESP: channel = %d\n", priv->curbssparams.channel);
1914 lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
1915 print_mac(mac, padhocresult->bssid));
1916
1917done:
1918 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1919 return ret;
1920}
1921
1922int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv)
1923{
1924 lbs_deb_enter(LBS_DEB_JOIN);
1925
1926 lbs_mac_event_disconnected(priv);
1927
1928 lbs_deb_leave(LBS_DEB_JOIN);
1929 return 0;
1930}