blob: d47e4d5734cd565fc07323141c25559a6cbd3abe [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
Dan Williamsd5db2df2008-08-21 17:51:07 -040028 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +020029 *
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;
Dan Williamsd5db2df2008-08-21 17:51:07 -040036 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +020037
38 lbs_deb_enter(LBS_DEB_ASSOC);
39
40 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
41 0, CMD_OPTION_WAITFORRSP,
42 0, assoc_req->bss.bssid);
Holger Schurig697900a2008-04-02 16:27:10 +020043 if (ret)
Dan Williamsd5db2df2008-08-21 17:51:07 -040044 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +020045
Dan Williamsd5db2df2008-08-21 17:51:07 -040046 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +020047 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
48 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -040049 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +020050
Dan Williamsd5db2df2008-08-21 17:51:07 -040051 ret = lbs_set_radio(priv, preamble, 1);
52 if (ret)
53 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +020054
55 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
56 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
57
Dan Williamsd5db2df2008-08-21 17:51:07 -040058out:
Holger Schurig697900a2008-04-02 16:27:10 +020059 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
Dan Williamsd5db2df2008-08-21 17:51:07 -040067 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +020068 *
69 * @return 0--success, -1--fail
70 */
71static int lbs_join_adhoc_network(struct lbs_private *priv,
72 struct assoc_request *assoc_req)
73{
74 struct bss_descriptor *bss = &assoc_req->bss;
75 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -040076 u8 preamble = RADIO_PREAMBLE_LONG;
77
78 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +020079
80 lbs_deb_join("current SSID '%s', ssid length %u\n",
81 escape_essid(priv->curbssparams.ssid,
82 priv->curbssparams.ssid_len),
83 priv->curbssparams.ssid_len);
84 lbs_deb_join("requested ssid '%s', ssid length %u\n",
85 escape_essid(bss->ssid, bss->ssid_len),
86 bss->ssid_len);
87
88 /* check if the requested SSID is already joined */
89 if (priv->curbssparams.ssid_len &&
90 !lbs_ssid_cmp(priv->curbssparams.ssid,
91 priv->curbssparams.ssid_len,
92 bss->ssid, bss->ssid_len) &&
93 (priv->mode == IW_MODE_ADHOC) &&
94 (priv->connect_status == LBS_CONNECTED)) {
95 union iwreq_data wrqu;
96
97 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
98 "current, not attempting to re-join");
99
100 /* Send the re-association event though, because the association
101 * request really was successful, even if just a null-op.
102 */
103 memset(&wrqu, 0, sizeof(wrqu));
104 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
105 ETH_ALEN);
106 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
107 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
108 goto out;
109 }
110
Dan Williamsd5db2df2008-08-21 17:51:07 -0400111 /* Use short preamble only when both the BSS and firmware support it */
112 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
113 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200114 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400115 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200116 }
117
Dan Williamsd5db2df2008-08-21 17:51:07 -0400118 ret = lbs_set_radio(priv, preamble, 1);
119 if (ret)
120 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200121
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:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400132 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200133 return ret;
134}
135
136/**
137 * @brief Start an Adhoc Network
138 *
139 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400140 * @param assoc_req The association request describing the BSS to start
Holger Schurig697900a2008-04-02 16:27:10 +0200141 * @return 0--success, -1--fail
142 */
143static int lbs_start_adhoc_network(struct lbs_private *priv,
144 struct assoc_request *assoc_req)
145{
146 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400147 u8 preamble = RADIO_PREAMBLE_LONG;
148
149 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200150
151 priv->adhoccreate = 1;
152
153 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
154 lbs_deb_join("AdhocStart: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400155 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200156 }
157
Dan Williamsd5db2df2008-08-21 17:51:07 -0400158 ret = lbs_set_radio(priv, preamble, 1);
159 if (ret)
160 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200161
162 lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
163 lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
164
165 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
166 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
167
Dan Williamsd5db2df2008-08-21 17:51:07 -0400168out:
169 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200170 return ret;
171}
172
173int lbs_stop_adhoc_network(struct lbs_private *priv)
174{
175 return lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
176 0, CMD_OPTION_WAITFORRSP, 0, NULL);
177}
Dan Williamse76850d2007-05-25 17:09:41 -0400178
Holger Schurig245bf202008-04-02 16:27:42 +0200179static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
180 struct bss_descriptor *match_bss)
181{
182 if (!secinfo->wep_enabled && !secinfo->WPAenabled
183 && !secinfo->WPA2enabled
184 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
185 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
186 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
187 return 1;
188 else
189 return 0;
190}
191
192static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
193 struct bss_descriptor *match_bss)
194{
195 if (secinfo->wep_enabled && !secinfo->WPAenabled
196 && !secinfo->WPA2enabled
197 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
198 return 1;
199 else
200 return 0;
201}
202
203static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
204 struct bss_descriptor *match_bss)
205{
206 if (!secinfo->wep_enabled && secinfo->WPAenabled
207 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
208 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
209 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
210 )
211 return 1;
212 else
213 return 0;
214}
215
216static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
217 struct bss_descriptor *match_bss)
218{
219 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
220 (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
221 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
222 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
223 )
224 return 1;
225 else
226 return 0;
227}
228
229static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
230 struct bss_descriptor *match_bss)
231{
232 if (!secinfo->wep_enabled && !secinfo->WPAenabled
233 && !secinfo->WPA2enabled
234 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
235 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
236 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
237 return 1;
238 else
239 return 0;
240}
241
242/**
243 * @brief Check if a scanned network compatible with the driver settings
244 *
245 * WEP WPA WPA2 ad-hoc encrypt Network
246 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
247 * 0 0 0 0 NONE 0 0 0 yes No security
248 * 1 0 0 0 NONE 1 0 0 yes Static WEP
249 * 0 1 0 0 x 1x 1 x yes WPA
250 * 0 0 1 0 x 1x x 1 yes WPA2
251 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
252 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
253 *
254 *
255 * @param priv A pointer to struct lbs_private
256 * @param index Index in scantable to check against current driver settings
257 * @param mode Network mode: Infrastructure or IBSS
258 *
259 * @return Index in scantable, or error code if negative
260 */
261static int is_network_compatible(struct lbs_private *priv,
262 struct bss_descriptor *bss, uint8_t mode)
263{
264 int matched = 0;
265
266 lbs_deb_enter(LBS_DEB_SCAN);
267
268 if (bss->mode != mode)
269 goto done;
270
271 matched = match_bss_no_security(&priv->secinfo, bss);
272 if (matched)
273 goto done;
274 matched = match_bss_static_wep(&priv->secinfo, bss);
275 if (matched)
276 goto done;
277 matched = match_bss_wpa(&priv->secinfo, bss);
278 if (matched) {
279 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
280 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
281 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
282 priv->secinfo.wep_enabled ? "e" : "d",
283 priv->secinfo.WPAenabled ? "e" : "d",
284 priv->secinfo.WPA2enabled ? "e" : "d",
285 (bss->capability & WLAN_CAPABILITY_PRIVACY));
286 goto done;
287 }
288 matched = match_bss_wpa2(&priv->secinfo, bss);
289 if (matched) {
290 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
291 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
292 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
293 priv->secinfo.wep_enabled ? "e" : "d",
294 priv->secinfo.WPAenabled ? "e" : "d",
295 priv->secinfo.WPA2enabled ? "e" : "d",
296 (bss->capability & WLAN_CAPABILITY_PRIVACY));
297 goto done;
298 }
299 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
300 if (matched) {
301 lbs_deb_scan("is_network_compatible() dynamic WEP: "
302 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
303 bss->wpa_ie[0], bss->rsn_ie[0],
304 (bss->capability & WLAN_CAPABILITY_PRIVACY));
305 goto done;
306 }
307
308 /* bss security settings don't match those configured on card */
309 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
310 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
311 bss->wpa_ie[0], bss->rsn_ie[0],
312 priv->secinfo.wep_enabled ? "e" : "d",
313 priv->secinfo.WPAenabled ? "e" : "d",
314 priv->secinfo.WPA2enabled ? "e" : "d",
315 (bss->capability & WLAN_CAPABILITY_PRIVACY));
316
317done:
318 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
319 return matched;
320}
321
322/**
323 * @brief This function finds a specific compatible BSSID in the scan list
324 *
325 * Used in association code
326 *
327 * @param priv A pointer to struct lbs_private
328 * @param bssid BSSID to find in the scan list
329 * @param mode Network mode: Infrastructure or IBSS
330 *
331 * @return index in BSSID list, or error return code (< 0)
332 */
333static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
334 uint8_t *bssid, uint8_t mode)
335{
336 struct bss_descriptor *iter_bss;
337 struct bss_descriptor *found_bss = NULL;
338
339 lbs_deb_enter(LBS_DEB_SCAN);
340
341 if (!bssid)
342 goto out;
343
344 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
345
346 /* Look through the scan table for a compatible match. The loop will
347 * continue past a matched bssid that is not compatible in case there
348 * is an AP with multiple SSIDs assigned to the same BSSID
349 */
350 mutex_lock(&priv->lock);
351 list_for_each_entry(iter_bss, &priv->network_list, list) {
352 if (compare_ether_addr(iter_bss->bssid, bssid))
353 continue; /* bssid doesn't match */
354 switch (mode) {
355 case IW_MODE_INFRA:
356 case IW_MODE_ADHOC:
357 if (!is_network_compatible(priv, iter_bss, mode))
358 break;
359 found_bss = iter_bss;
360 break;
361 default:
362 found_bss = iter_bss;
363 break;
364 }
365 }
366 mutex_unlock(&priv->lock);
367
368out:
369 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
370 return found_bss;
371}
372
373/**
374 * @brief This function finds ssid in ssid list.
375 *
376 * Used in association code
377 *
378 * @param priv A pointer to struct lbs_private
379 * @param ssid SSID to find in the list
380 * @param bssid BSSID to qualify the SSID selection (if provided)
381 * @param mode Network mode: Infrastructure or IBSS
382 *
383 * @return index in BSSID list
384 */
385static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
386 uint8_t *ssid, uint8_t ssid_len,
387 uint8_t *bssid, uint8_t mode,
388 int channel)
389{
390 u32 bestrssi = 0;
391 struct bss_descriptor *iter_bss = NULL;
392 struct bss_descriptor *found_bss = NULL;
393 struct bss_descriptor *tmp_oldest = NULL;
394
395 lbs_deb_enter(LBS_DEB_SCAN);
396
397 mutex_lock(&priv->lock);
398
399 list_for_each_entry(iter_bss, &priv->network_list, list) {
400 if (!tmp_oldest ||
401 (iter_bss->last_scanned < tmp_oldest->last_scanned))
402 tmp_oldest = iter_bss;
403
404 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
405 ssid, ssid_len) != 0)
406 continue; /* ssid doesn't match */
407 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
408 continue; /* bssid doesn't match */
409 if ((channel > 0) && (iter_bss->channel != channel))
410 continue; /* channel doesn't match */
411
412 switch (mode) {
413 case IW_MODE_INFRA:
414 case IW_MODE_ADHOC:
415 if (!is_network_compatible(priv, iter_bss, mode))
416 break;
417
418 if (bssid) {
419 /* Found requested BSSID */
420 found_bss = iter_bss;
421 goto out;
422 }
423
424 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
425 bestrssi = SCAN_RSSI(iter_bss->rssi);
426 found_bss = iter_bss;
427 }
428 break;
429 case IW_MODE_AUTO:
430 default:
431 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
432 bestrssi = SCAN_RSSI(iter_bss->rssi);
433 found_bss = iter_bss;
434 }
435 break;
436 }
437 }
438
439out:
440 mutex_unlock(&priv->lock);
441 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
442 return found_bss;
443}
444
Holger Schurig69f90322007-11-23 15:43:44 +0100445static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446 struct assoc_request * assoc_req)
447{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400449 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400450 int channel = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451
Holger Schurig9012b282007-05-25 11:27:16 -0400452 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200453
Dan Williamsef9a2642007-05-25 16:46:33 -0400454 /* FIXME: take channel into account when picking SSIDs if a channel
455 * is set.
456 */
457
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400458 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
459 channel = assoc_req->channel;
460
Holger Schurig0765af42007-10-15 12:55:56 +0200461 lbs_deb_assoc("SSID '%s' requested\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400462 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -0400463 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500464 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100465 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200466
David Woodhouseaa21c002007-12-08 20:04:36 +0000467 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400468 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400469 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -0400470 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -0500471 ret = lbs_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -0400473 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400475 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476 /* Scan for the network, do not save previous results. Stale
477 * scan data will cause us to join a non-existant adhoc network
478 */
Holger Schurig10078322007-11-15 18:05:47 -0500479 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100480 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481
482 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000483 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400484 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400485 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -0400486 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400487 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -0500488 lbs_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200489 } else {
490 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -0400491 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400492 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400493 IW_ESSID_MAX_SIZE);
494 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Holger Schurig10078322007-11-15 18:05:47 -0500495 lbs_start_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200496 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 }
498
Holger Schurig9012b282007-05-25 11:27:16 -0400499 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500 return ret;
501}
502
503
Holger Schurig69f90322007-11-23 15:43:44 +0100504static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200505 struct assoc_request * assoc_req)
506{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400507 int ret = 0;
508 struct bss_descriptor * bss;
Joe Perches0795af52007-10-03 17:59:30 -0700509 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510
Joe Perches0795af52007-10-03 17:59:30 -0700511 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
512 print_mac(mac, assoc_req->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513
514 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +0000515 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400517 if (bss == NULL) {
Joe Perches0795af52007-10-03 17:59:30 -0700518 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
519 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 goto out;
521 }
522
Dan Williamse76850d2007-05-25 17:09:41 -0400523 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400524 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500525 ret = lbs_associate(priv, assoc_req);
526 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400527 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Holger Schurig10078322007-11-15 18:05:47 -0500528 lbs_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530
531out:
Holger Schurig9012b282007-05-25 11:27:16 -0400532 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 return ret;
534}
535
536
Holger Schurig69f90322007-11-23 15:43:44 +0100537static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 struct assoc_request * assoc_req)
539{
540 int ret = 0, done = 0;
541
Holger Schurig0765af42007-10-15 12:55:56 +0200542 lbs_deb_enter(LBS_DEB_ASSOC);
543
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544 /* If we're given and 'any' BSSID, try associating based on SSID */
545
546 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400547 if (compare_ether_addr(bssid_any, assoc_req->bssid)
548 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 ret = assoc_helper_bssid(priv, assoc_req);
550 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551 }
552 }
553
554 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
555 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200556 }
557
Holger Schurig0765af42007-10-15 12:55:56 +0200558 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 return ret;
560}
561
562
Holger Schurig69f90322007-11-23 15:43:44 +0100563static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200564 struct assoc_request * assoc_req)
565{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200566 int ret = 0;
567
Holger Schurig9012b282007-05-25 11:27:16 -0400568 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200569
David Woodhouseaa21c002007-12-08 20:04:36 +0000570 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -0400571 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572
Dan Williams0dc5a292007-05-10 22:58:02 -0400573 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000574 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -0500575 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +0000576 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577 }
578
David Woodhouseaa21c002007-12-08 20:04:36 +0000579 priv->mode = assoc_req->mode;
Holger Schurig10078322007-11-15 18:05:47 -0500580 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400581 CMD_802_11_SNMP_MIB,
582 0, CMD_OPTION_WAITFORRSP,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583 OID_802_11_INFRASTRUCTURE_MODE,
David Woodhouse981f1872007-05-25 23:36:54 -0400584 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585
Holger Schurig9012b282007-05-25 11:27:16 -0400586done:
587 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588 return ret;
589}
590
Holger Schurig69f90322007-11-23 15:43:44 +0100591static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -0400592 struct assoc_request * assoc_req)
593{
Dan Williamsef9a2642007-05-25 16:46:33 -0400594 int ret = 0;
595
596 lbs_deb_enter(LBS_DEB_ASSOC);
597
David Woodhouse9f462572007-12-12 22:50:21 -0500598 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500599 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500600 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500601 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -0400602 }
603
David Woodhouseaa21c002007-12-08 20:04:36 +0000604 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400605 goto done;
606
David Woodhouse8642f1f2007-12-11 20:03:01 -0500607 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -0500608 /* Change mesh channel first; 21.p21 firmware won't let
609 you change channel otherwise (even though it'll return
610 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700611 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
612 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500613 }
614
Dan Williamsef9a2642007-05-25 16:46:33 -0400615 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -0500616 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -0400617
Dan Williams2dd4b262007-12-11 16:54:15 -0500618 ret = lbs_set_channel(priv, assoc_req->channel);
619 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -0500620 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -0400621
Dan Williams2dd4b262007-12-11 16:54:15 -0500622 /* FIXME: shouldn't need to grab the channel _again_ after setting
623 * it since the firmware is supposed to return the new channel, but
624 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -0500625 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500626 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500627 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500628 goto done;
629 }
Dan Williamsef9a2642007-05-25 16:46:33 -0400630
David Woodhouseaa21c002007-12-08 20:04:36 +0000631 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -0500632 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -0400633 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500634 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -0400635 }
636
637 if ( assoc_req->secinfo.wep_enabled
638 && (assoc_req->wep_keys[0].len
639 || assoc_req->wep_keys[1].len
640 || assoc_req->wep_keys[2].len
641 || assoc_req->wep_keys[3].len)) {
642 /* Make sure WEP keys are re-sent to firmware */
643 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
644 }
645
646 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -0500647 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -0400648
David Woodhouse8642f1f2007-12-11 20:03:01 -0500649 restore_mesh:
650 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700651 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
652 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500653
654 done:
Dan Williamsef9a2642007-05-25 16:46:33 -0400655 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
656 return ret;
657}
658
659
Holger Schurig69f90322007-11-23 15:43:44 +0100660static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -0500661 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663 int i;
664 int ret = 0;
665
Holger Schurig9012b282007-05-25 11:27:16 -0400666 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667
668 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -0500669 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
670 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
671 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
672 else
673 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200674
675 if (ret)
676 goto out;
677
678 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400679 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +0100680 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 else
Holger Schurigd9e97782008-03-12 16:06:43 +0100682 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -0500683
Holger Schurigc97329e2008-03-18 11:20:21 +0100684 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685
David Woodhouseaa21c002007-12-08 20:04:36 +0000686 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687
David Woodhouseaa21c002007-12-08 20:04:36 +0000688 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000690 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -0500691 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200692 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000693 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694
David Woodhouseaa21c002007-12-08 20:04:36 +0000695 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200696
697out:
Holger Schurig9012b282007-05-25 11:27:16 -0400698 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 return ret;
700}
701
Holger Schurig69f90322007-11-23 15:43:44 +0100702static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703 struct assoc_request * assoc_req)
704{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -0500706 uint16_t do_wpa;
707 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708
Holger Schurig9012b282007-05-25 11:27:16 -0400709 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710
David Woodhouseaa21c002007-12-08 20:04:36 +0000711 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -0500712 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200713
Holger Schurigc97329e2008-03-18 11:20:21 +0100714 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715
Dan Williams18c96c342007-06-18 12:01:12 -0400716 /* If RSN is already enabled, don't try to enable it again, since
717 * ENABLE_RSN resets internal state machines and will clobber the
718 * 4-way WPA handshake.
719 */
720
721 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500722 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -0400723 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500724 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -0400725 goto out;
726 }
727
728 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500729 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -0400730 if (do_wpa == rsn)
731 goto out;
732
733 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500734 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -0400735
736out:
Holger Schurig9012b282007-05-25 11:27:16 -0400737 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738 return ret;
739}
740
741
Holger Schurig69f90322007-11-23 15:43:44 +0100742static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743 struct assoc_request * assoc_req)
744{
745 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -0400746 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747
Holger Schurig9012b282007-05-25 11:27:16 -0400748 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749
Dan Williams2bcde512007-10-03 10:37:45 -0400750 /* Work around older firmware bug where WPA unicast and multicast
751 * keys must be set independently. Seen in SDIO parts with firmware
752 * version 5.0.11p0.
753 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754
Dan Williams2bcde512007-10-03 10:37:45 -0400755 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
756 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +0100757 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -0400758 assoc_req->flags = flags;
759 }
760
761 if (ret)
762 goto out;
763
764 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
765 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
766
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
771out:
Holger Schurig9012b282007-05-25 11:27:16 -0400772 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773 return ret;
774}
775
776
Holger Schurig69f90322007-11-23 15:43:44 +0100777static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200778 struct assoc_request * assoc_req)
779{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200780 int ret = 0;
781
Holger Schurig9012b282007-05-25 11:27:16 -0400782 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200783
784 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000785 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
786 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200787 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000788 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
789 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790 }
791
Holger Schurig9012b282007-05-25 11:27:16 -0400792 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200793 return ret;
794}
795
796
David Woodhouseaa21c002007-12-08 20:04:36 +0000797static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200798 struct assoc_request * assoc_req)
799{
Holger Schurig0765af42007-10-15 12:55:56 +0200800 int ret = 0;
801
David Woodhouseaa21c002007-12-08 20:04:36 +0000802 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803 return 0;
804
Holger Schurig52507c22008-01-28 17:25:53 +0100805 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200806 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200807 lbs_deb_assoc("Deauthenticating due to new SSID\n");
808 ret = 1;
809 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200810 }
811
812 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000813 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +0200814 lbs_deb_assoc("Deauthenticating due to new security\n");
815 ret = 1;
816 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200817 }
818 }
819
820 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200821 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
822 ret = 1;
823 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824 }
825
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -0400826 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200827 lbs_deb_assoc("Deauthenticating due to channel switch\n");
828 ret = 1;
829 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -0400830 }
831
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200832 /* FIXME: deal with 'auto' mode somehow */
833 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200834 if (assoc_req->mode != IW_MODE_INFRA) {
835 lbs_deb_assoc("Deauthenticating due to leaving "
836 "infra mode\n");
837 ret = 1;
838 goto out;
839 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200840 }
841
Holger Schurig0765af42007-10-15 12:55:56 +0200842out:
843 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +0100844 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200845}
846
847
David Woodhouseaa21c002007-12-08 20:04:36 +0000848static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200849 struct assoc_request * assoc_req)
850{
Holger Schurig0765af42007-10-15 12:55:56 +0200851 lbs_deb_enter(LBS_DEB_ASSOC);
852
David Woodhouseaa21c002007-12-08 20:04:36 +0000853 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200854 return 0;
855
David Woodhouseaa21c002007-12-08 20:04:36 +0000856 if (lbs_ssid_cmp(priv->curbssparams.ssid,
857 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -0400858 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200859 return 1;
860
861 /* FIXME: deal with 'auto' mode somehow */
862 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400863 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200864 return 1;
865 }
866
Dan Williamsef9a2642007-05-25 16:46:33 -0400867 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000868 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400869 return 1;
870 }
871
Holger Schurig0765af42007-10-15 12:55:56 +0200872 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200873 return 0;
874}
875
876
Holger Schurig245bf202008-04-02 16:27:42 +0200877/**
878 * @brief This function finds the best SSID in the Scan List
879 *
880 * Search the scan table for the best SSID that also matches the current
881 * adapter network preference (infrastructure or adhoc)
882 *
883 * @param priv A pointer to struct lbs_private
884 *
885 * @return index in BSSID list
886 */
887static struct bss_descriptor *lbs_find_best_ssid_in_list(
888 struct lbs_private *priv, uint8_t mode)
889{
890 uint8_t bestrssi = 0;
891 struct bss_descriptor *iter_bss;
892 struct bss_descriptor *best_bss = NULL;
893
894 lbs_deb_enter(LBS_DEB_SCAN);
895
896 mutex_lock(&priv->lock);
897
898 list_for_each_entry(iter_bss, &priv->network_list, list) {
899 switch (mode) {
900 case IW_MODE_INFRA:
901 case IW_MODE_ADHOC:
902 if (!is_network_compatible(priv, iter_bss, mode))
903 break;
904 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
905 break;
906 bestrssi = SCAN_RSSI(iter_bss->rssi);
907 best_bss = iter_bss;
908 break;
909 case IW_MODE_AUTO:
910 default:
911 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
912 break;
913 bestrssi = SCAN_RSSI(iter_bss->rssi);
914 best_bss = iter_bss;
915 break;
916 }
917 }
918
919 mutex_unlock(&priv->lock);
920 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
921 return best_bss;
922}
923
924/**
925 * @brief Find the best AP
926 *
927 * Used from association worker.
928 *
929 * @param priv A pointer to struct lbs_private structure
930 * @param pSSID A pointer to AP's ssid
931 *
932 * @return 0--success, otherwise--fail
933 */
934static int lbs_find_best_network_ssid(struct lbs_private *priv,
935 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
936 uint8_t *out_mode)
937{
938 int ret = -1;
939 struct bss_descriptor *found;
940
941 lbs_deb_enter(LBS_DEB_SCAN);
942
943 priv->scan_ssid_len = 0;
944 lbs_scan_networks(priv, 1);
945 if (priv->surpriseremoved)
946 goto out;
947
948 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
949 if (found && (found->ssid_len > 0)) {
950 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
951 *out_ssid_len = found->ssid_len;
952 *out_mode = found->mode;
953 ret = 0;
954 }
955
956out:
957 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
958 return ret;
959}
960
961
Holger Schurig10078322007-11-15 18:05:47 -0500962void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200963{
Holger Schurig69f90322007-11-23 15:43:44 +0100964 struct lbs_private *priv = container_of(work, struct lbs_private,
965 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200966 struct assoc_request * assoc_req = NULL;
967 int ret = 0;
968 int find_any_ssid = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700969 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200970
Holger Schurig9012b282007-05-25 11:27:16 -0400971 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200972
David Woodhouseaa21c002007-12-08 20:04:36 +0000973 mutex_lock(&priv->lock);
974 assoc_req = priv->pending_assoc_req;
975 priv->pending_assoc_req = NULL;
976 priv->in_progress_assoc_req = assoc_req;
977 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200978
Holger Schurig9012b282007-05-25 11:27:16 -0400979 if (!assoc_req)
980 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981
Holger Schurig0765af42007-10-15 12:55:56 +0200982 lbs_deb_assoc(
983 "Association Request:\n"
984 " flags: 0x%08lx\n"
985 " SSID: '%s'\n"
986 " chann: %d\n"
987 " band: %d\n"
988 " mode: %d\n"
989 " BSSID: %s\n"
990 " secinfo: %s%s%s\n"
991 " auth_mode: %d\n",
992 assoc_req->flags,
993 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
994 assoc_req->channel, assoc_req->band, assoc_req->mode,
995 print_mac(mac, assoc_req->bssid),
996 assoc_req->secinfo.WPAenabled ? " WPA" : "",
997 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
998 assoc_req->secinfo.wep_enabled ? " WEP" : "",
999 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001000
1001 /* If 'any' SSID was specified, find an SSID to associate with */
1002 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001003 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004 find_any_ssid = 1;
1005
1006 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1007 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -04001008 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1009 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010 find_any_ssid = 0;
1011 }
1012
1013 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001014 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001015
Holger Schurig10078322007-11-15 18:05:47 -05001016 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001017 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001018 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001019 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 ret = -ENETUNREACH;
1021 goto out;
1022 }
1023
1024 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001025 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1027 assoc_req->mode = new_mode;
1028 }
1029 }
1030
1031 /*
1032 * Check if the attributes being changing require deauthentication
1033 * from the currently associated infrastructure access point.
1034 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001035 if (priv->mode == IW_MODE_INFRA) {
1036 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001037 ret = lbs_cmd_80211_deauthenticate(priv,
1038 priv->curbssparams.bssid,
1039 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001040 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001041 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042 "configuration request failed: %d\n",
1043 ret);
1044 }
1045 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001046 } else if (priv->mode == IW_MODE_ADHOC) {
1047 if (should_stop_adhoc(priv, assoc_req)) {
Holger Schurig10078322007-11-15 18:05:47 -05001048 ret = lbs_stop_adhoc_network(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001049 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001050 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051 "new configuration request failed: %d\n",
1052 ret);
1053 }
1054
1055 }
1056 }
1057
1058 /* Send the various configuration bits to the firmware */
1059 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1060 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001061 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001062 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001063 }
1064
Dan Williamsef9a2642007-05-25 16:46:33 -04001065 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1066 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001067 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001068 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001069 }
1070
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001071 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1072 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1073 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001074 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001075 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 }
1077
1078 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1079 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001080 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001081 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001082 }
1083
1084 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1085 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001086 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001087 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001088 }
1089
1090 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1091 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1092 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001093 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001094 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095 }
1096
1097 /* SSID/BSSID should be the _last_ config option set, because they
1098 * trigger the association attempt.
1099 */
1100 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1101 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1102 int success = 1;
1103
1104 ret = assoc_helper_associate(priv, assoc_req);
1105 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001106 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001107 ret);
1108 success = 0;
1109 }
1110
David Woodhouseaa21c002007-12-08 20:04:36 +00001111 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001112 lbs_deb_assoc("ASSOC: association unsuccessful, "
1113 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001114 success = 0;
1115 }
1116
1117 if (success) {
Holger Schurig52507c22008-01-28 17:25:53 +01001118 lbs_deb_assoc("associated to %s\n",
David Woodhouseaa21c002007-12-08 20:04:36 +00001119 print_mac(mac, priv->curbssparams.bssid));
Holger Schurig10078322007-11-15 18:05:47 -05001120 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001121 CMD_802_11_RSSI,
1122 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001123 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001124 ret = -1;
1125 }
1126 }
1127
1128out:
1129 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001130 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001131 ret);
1132 }
Dan Williamse76850d2007-05-25 17:09:41 -04001133
David Woodhouseaa21c002007-12-08 20:04:36 +00001134 mutex_lock(&priv->lock);
1135 priv->in_progress_assoc_req = NULL;
1136 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001137 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001138
1139done:
1140 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141}
1142
1143
1144/*
1145 * Caller MUST hold any necessary locks
1146 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001147struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001148{
1149 struct assoc_request * assoc_req;
1150
Holger Schurig0765af42007-10-15 12:55:56 +02001151 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001152 if (!priv->pending_assoc_req) {
1153 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001154 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001155 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001156 lbs_pr_info("Not enough memory to allocate association"
1157 " request!\n");
1158 return NULL;
1159 }
1160 }
1161
1162 /* Copy current configuration attributes to the association request,
1163 * but don't overwrite any that are already set.
1164 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001165 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001166 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001167 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001168 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001169 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001170 }
1171
1172 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001173 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001174
Dan Williamse76850d2007-05-25 17:09:41 -04001175 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001176 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001177
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001178 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001179 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180
1181 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001182 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001183 ETH_ALEN);
1184 }
1185
1186 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1187 int i;
1188 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001189 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001190 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001191 }
1192 }
1193
1194 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001195 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001196
1197 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001198 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001199 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001200 }
1201
1202 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001203 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001204 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001205 }
1206
1207 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001208 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001209 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001210 }
1211
1212 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001213 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001214 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001215 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001216 }
1217
Holger Schurig0765af42007-10-15 12:55:56 +02001218 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001219 return assoc_req;
1220}
Holger Schurig697900a2008-04-02 16:27:10 +02001221
1222
1223/**
1224 * @brief This function finds common rates between rate1 and card rates.
1225 *
1226 * It will fill common rates in rate1 as output if found.
1227 *
1228 * NOTE: Setting the MSB of the basic rates need to be taken
1229 * care, either before or after calling this function
1230 *
1231 * @param priv A pointer to struct lbs_private structure
1232 * @param rate1 the buffer which keeps input and output
1233 * @param rate1_size the size of rate1 buffer; new size of buffer on return
1234 *
1235 * @return 0 or -1
1236 */
1237static int get_common_rates(struct lbs_private *priv,
1238 u8 *rates,
1239 u16 *rates_size)
1240{
1241 u8 *card_rates = lbs_bg_rates;
1242 size_t num_card_rates = sizeof(lbs_bg_rates);
1243 int ret = 0, i, j;
1244 u8 tmp[30];
1245 size_t tmp_size = 0;
1246
1247 /* For each rate in card_rates that exists in rate1, copy to tmp */
1248 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
1249 for (j = 0; rates[j] && (j < *rates_size); j++) {
1250 if (rates[j] == card_rates[i])
1251 tmp[tmp_size++] = card_rates[i];
1252 }
1253 }
1254
1255 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
1256 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
1257 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
1258 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
1259
Javier Cardona85319f92008-05-24 10:59:49 +01001260 if (!priv->enablehwauto) {
Holger Schurig697900a2008-04-02 16:27:10 +02001261 for (i = 0; i < tmp_size; i++) {
1262 if (tmp[i] == priv->cur_rate)
1263 goto done;
1264 }
1265 lbs_pr_alert("Previously set fixed data rate %#x isn't "
1266 "compatible with the network.\n", priv->cur_rate);
1267 ret = -1;
1268 goto done;
1269 }
1270 ret = 0;
1271
1272done:
1273 memset(rates, 0, *rates_size);
1274 *rates_size = min_t(int, tmp_size, *rates_size);
1275 memcpy(rates, tmp, *rates_size);
1276 return ret;
1277}
1278
1279
1280/**
1281 * @brief Sets the MSB on basic rates as the firmware requires
1282 *
1283 * Scan through an array and set the MSB for basic data rates.
1284 *
1285 * @param rates buffer of data rates
1286 * @param len size of buffer
1287 */
1288static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
1289{
1290 int i;
1291
1292 for (i = 0; i < len; i++) {
1293 if (rates[i] == 0x02 || rates[i] == 0x04 ||
1294 rates[i] == 0x0b || rates[i] == 0x16)
1295 rates[i] |= 0x80;
1296 }
1297}
1298
1299/**
Holger Schurig697900a2008-04-02 16:27:10 +02001300 * @brief This function prepares command of authenticate.
1301 *
1302 * @param priv A pointer to struct lbs_private structure
1303 * @param cmd A pointer to cmd_ds_command structure
1304 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
1305 *
1306 * @return 0 or -1
1307 */
1308int lbs_cmd_80211_authenticate(struct lbs_private *priv,
1309 struct cmd_ds_command *cmd,
1310 void *pdata_buf)
1311{
1312 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
1313 int ret = -1;
1314 u8 *bssid = pdata_buf;
1315 DECLARE_MAC_BUF(mac);
1316
1317 lbs_deb_enter(LBS_DEB_JOIN);
1318
1319 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
1320 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
1321 + S_DS_GEN);
1322
1323 /* translate auth mode to 802.11 defined wire value */
1324 switch (priv->secinfo.auth_mode) {
1325 case IW_AUTH_ALG_OPEN_SYSTEM:
1326 pauthenticate->authtype = 0x00;
1327 break;
1328 case IW_AUTH_ALG_SHARED_KEY:
1329 pauthenticate->authtype = 0x01;
1330 break;
1331 case IW_AUTH_ALG_LEAP:
1332 pauthenticate->authtype = 0x80;
1333 break;
1334 default:
1335 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
1336 priv->secinfo.auth_mode);
1337 goto out;
1338 }
1339
1340 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
1341
1342 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
1343 print_mac(mac, bssid), pauthenticate->authtype);
1344 ret = 0;
1345
1346out:
1347 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1348 return ret;
1349}
1350
Dan Williams191bb402008-08-21 17:46:18 -04001351/**
1352 * @brief Deauthenticate from a specific BSS
1353 *
1354 * @param priv A pointer to struct lbs_private structure
1355 * @param bssid The specific BSS to deauthenticate from
1356 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1357 *
1358 * @return 0 on success, error on failure
1359 */
1360int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1361 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001362{
Dan Williams191bb402008-08-21 17:46:18 -04001363 struct cmd_ds_802_11_deauthenticate cmd;
1364 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001365
1366 lbs_deb_enter(LBS_DEB_JOIN);
1367
Dan Williams191bb402008-08-21 17:46:18 -04001368 memset(&cmd, 0, sizeof(cmd));
1369 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1370 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1371 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001372
Dan Williams191bb402008-08-21 17:46:18 -04001373 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001374
Dan Williams191bb402008-08-21 17:46:18 -04001375 /* Clean up everything even if there was an error; can't assume that
1376 * we're still authenticated to the AP after trying to deauth.
1377 */
1378 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001379
1380 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001381 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001382}
1383
1384int lbs_cmd_80211_associate(struct lbs_private *priv,
1385 struct cmd_ds_command *cmd, void *pdata_buf)
1386{
1387 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1388 int ret = 0;
1389 struct assoc_request *assoc_req = pdata_buf;
1390 struct bss_descriptor *bss = &assoc_req->bss;
1391 u8 *pos;
1392 u16 tmpcap, tmplen;
1393 struct mrvlietypes_ssidparamset *ssid;
1394 struct mrvlietypes_phyparamset *phy;
1395 struct mrvlietypes_ssparamset *ss;
1396 struct mrvlietypes_ratesparamset *rates;
1397 struct mrvlietypes_rsnparamset *rsn;
1398
1399 lbs_deb_enter(LBS_DEB_ASSOC);
1400
1401 pos = (u8 *) passo;
1402
1403 if (!priv) {
1404 ret = -1;
1405 goto done;
1406 }
1407
1408 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1409
1410 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1411 pos += sizeof(passo->peerstaaddr);
1412
1413 /* set the listen interval */
1414 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1415
1416 pos += sizeof(passo->capability);
1417 pos += sizeof(passo->listeninterval);
1418 pos += sizeof(passo->bcnperiod);
1419 pos += sizeof(passo->dtimperiod);
1420
1421 ssid = (struct mrvlietypes_ssidparamset *) pos;
1422 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1423 tmplen = bss->ssid_len;
1424 ssid->header.len = cpu_to_le16(tmplen);
1425 memcpy(ssid->ssid, bss->ssid, tmplen);
1426 pos += sizeof(ssid->header) + tmplen;
1427
1428 phy = (struct mrvlietypes_phyparamset *) pos;
1429 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1430 tmplen = sizeof(phy->fh_ds.dsparamset);
1431 phy->header.len = cpu_to_le16(tmplen);
1432 memcpy(&phy->fh_ds.dsparamset,
1433 &bss->phyparamset.dsparamset.currentchan,
1434 tmplen);
1435 pos += sizeof(phy->header) + tmplen;
1436
1437 ss = (struct mrvlietypes_ssparamset *) pos;
1438 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
1439 tmplen = sizeof(ss->cf_ibss.cfparamset);
1440 ss->header.len = cpu_to_le16(tmplen);
1441 pos += sizeof(ss->header) + tmplen;
1442
1443 rates = (struct mrvlietypes_ratesparamset *) pos;
1444 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1445 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1446 tmplen = MAX_RATES;
1447 if (get_common_rates(priv, rates->rates, &tmplen)) {
1448 ret = -1;
1449 goto done;
1450 }
1451 pos += sizeof(rates->header) + tmplen;
1452 rates->header.len = cpu_to_le16(tmplen);
1453 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1454
1455 /* Copy the infra. association rates into Current BSS state structure */
1456 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1457 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1458
1459 /* Set MSB on basic rates as the firmware requires, but _after_
1460 * copying to current bss rates.
1461 */
1462 lbs_set_basic_rate_flags(rates->rates, tmplen);
1463
1464 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1465 rsn = (struct mrvlietypes_rsnparamset *) pos;
1466 /* WPA_IE or WPA2_IE */
1467 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1468 tmplen = (u16) assoc_req->wpa_ie[1];
1469 rsn->header.len = cpu_to_le16(tmplen);
1470 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1471 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1472 sizeof(rsn->header) + tmplen);
1473 pos += sizeof(rsn->header) + tmplen;
1474 }
1475
1476 /* update curbssparams */
1477 priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
1478
1479 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1480 ret = -1;
1481 goto done;
1482 }
1483
1484 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1485
1486 /* set the capability info */
1487 tmpcap = (bss->capability & CAPINFO_MASK);
1488 if (bss->mode == IW_MODE_INFRA)
1489 tmpcap |= WLAN_CAPABILITY_ESS;
1490 passo->capability = cpu_to_le16(tmpcap);
1491 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1492
1493done:
1494 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1495 return ret;
1496}
1497
1498int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
1499 struct cmd_ds_command *cmd, void *pdata_buf)
1500{
1501 struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
1502 int ret = 0;
1503 int cmdappendsize = 0;
1504 struct assoc_request *assoc_req = pdata_buf;
1505 u16 tmpcap = 0;
1506 size_t ratesize = 0;
1507
1508 lbs_deb_enter(LBS_DEB_JOIN);
1509
1510 if (!priv) {
1511 ret = -1;
1512 goto done;
1513 }
1514
1515 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
1516
1517 /*
1518 * Fill in the parameters for 2 data structures:
1519 * 1. cmd_ds_802_11_ad_hoc_start command
1520 * 2. priv->scantable[i]
1521 *
1522 * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
1523 * probe delay, and cap info.
1524 *
1525 * Firmware will fill up beacon period, DTIM, Basic rates
1526 * and operational rates.
1527 */
1528
1529 memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
1530 memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
1531
1532 lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
1533 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
1534 assoc_req->ssid_len);
1535
1536 /* set the BSS type */
1537 adhs->bsstype = CMD_BSS_TYPE_IBSS;
1538 priv->mode = IW_MODE_ADHOC;
1539 if (priv->beacon_period == 0)
1540 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
1541 adhs->beaconperiod = cpu_to_le16(priv->beacon_period);
1542
1543 /* set Physical param set */
1544#define DS_PARA_IE_ID 3
1545#define DS_PARA_IE_LEN 1
1546
1547 adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
1548 adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
1549
1550 WARN_ON(!assoc_req->channel);
1551
1552 lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
1553 assoc_req->channel);
1554
1555 adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
1556
1557 /* set IBSS param set */
1558#define IBSS_PARA_IE_ID 6
1559#define IBSS_PARA_IE_LEN 2
1560
1561 adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
1562 adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
1563 adhs->ssparamset.ibssparamset.atimwindow = 0;
1564
1565 /* set capability info */
1566 tmpcap = WLAN_CAPABILITY_IBSS;
1567 if (assoc_req->secinfo.wep_enabled) {
1568 lbs_deb_join("ADHOC_S_CMD: WEP enabled, "
1569 "setting privacy on\n");
1570 tmpcap |= WLAN_CAPABILITY_PRIVACY;
1571 } else {
1572 lbs_deb_join("ADHOC_S_CMD: WEP disabled, "
1573 "setting privacy off\n");
1574 }
1575 adhs->capability = cpu_to_le16(tmpcap);
1576
1577 /* probedelay */
1578 adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1579
1580 memset(adhs->rates, 0, sizeof(adhs->rates));
1581 ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
1582 memcpy(adhs->rates, lbs_bg_rates, ratesize);
1583
1584 /* Copy the ad-hoc creating rates into Current BSS state structure */
1585 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1586 memcpy(&priv->curbssparams.rates, &adhs->rates, ratesize);
1587
1588 /* Set MSB on basic rates as the firmware requires, but _after_
1589 * copying to current bss rates.
1590 */
1591 lbs_set_basic_rate_flags(adhs->rates, ratesize);
1592
1593 lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
1594 adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
1595
1596 lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
1597
1598 if (lbs_create_dnld_countryinfo_11d(priv)) {
1599 lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
1600 ret = -1;
1601 goto done;
1602 }
1603
1604 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
1605 S_DS_GEN + cmdappendsize);
1606
1607 ret = 0;
1608done:
1609 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1610 return ret;
1611}
1612
1613int lbs_cmd_80211_ad_hoc_stop(struct cmd_ds_command *cmd)
1614{
1615 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
1616 cmd->size = cpu_to_le16(S_DS_GEN);
1617
1618 return 0;
1619}
1620
1621int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
1622 struct cmd_ds_command *cmd, void *pdata_buf)
1623{
1624 struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
1625 struct assoc_request *assoc_req = pdata_buf;
1626 struct bss_descriptor *bss = &assoc_req->bss;
1627 int cmdappendsize = 0;
1628 int ret = 0;
1629 u16 ratesize = 0;
1630 DECLARE_MAC_BUF(mac);
1631
1632 lbs_deb_enter(LBS_DEB_JOIN);
1633
1634 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
1635
1636 join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
1637 join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
1638
1639 memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
1640 memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
1641
1642 memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
1643 sizeof(union ieeetypes_phyparamset));
1644
1645 memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
1646 sizeof(union IEEEtypes_ssparamset));
1647
1648 join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1649 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
1650 bss->capability, CAPINFO_MASK);
1651
1652 /* information on BSSID descriptor passed to FW */
1653 lbs_deb_join(
1654 "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
1655 print_mac(mac, join_cmd->bss.bssid),
1656 join_cmd->bss.ssid);
1657
1658 /* failtimeout */
1659 join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1660
1661 /* probedelay */
1662 join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1663
1664 priv->curbssparams.channel = bss->channel;
1665
1666 /* Copy Data rates from the rates recorded in scan response */
1667 memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
1668 ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
1669 memcpy(join_cmd->bss.rates, bss->rates, ratesize);
1670 if (get_common_rates(priv, join_cmd->bss.rates, &ratesize)) {
1671 lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
1672 ret = -1;
1673 goto done;
1674 }
1675
1676 /* Copy the ad-hoc creating rates into Current BSS state structure */
1677 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1678 memcpy(&priv->curbssparams.rates, join_cmd->bss.rates, ratesize);
1679
1680 /* Set MSB on basic rates as the firmware requires, but _after_
1681 * copying to current bss rates.
1682 */
1683 lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
1684
1685 join_cmd->bss.ssparamset.ibssparamset.atimwindow =
1686 cpu_to_le16(bss->atimwindow);
1687
1688 if (assoc_req->secinfo.wep_enabled) {
1689 u16 tmp = le16_to_cpu(join_cmd->bss.capability);
1690 tmp |= WLAN_CAPABILITY_PRIVACY;
1691 join_cmd->bss.capability = cpu_to_le16(tmp);
1692 }
1693
1694 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1695 /* wake up first */
1696 __le32 Localpsmode;
1697
1698 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1699 ret = lbs_prepare_and_send_command(priv,
1700 CMD_802_11_PS_MODE,
1701 CMD_ACT_SET,
1702 0, 0, &Localpsmode);
1703
1704 if (ret) {
1705 ret = -1;
1706 goto done;
1707 }
1708 }
1709
1710 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1711 ret = -1;
1712 goto done;
1713 }
1714
1715 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
1716 S_DS_GEN + cmdappendsize);
1717
1718done:
1719 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1720 return ret;
1721}
1722
1723int lbs_ret_80211_associate(struct lbs_private *priv,
1724 struct cmd_ds_command *resp)
1725{
1726 int ret = 0;
1727 union iwreq_data wrqu;
1728 struct ieeetypes_assocrsp *passocrsp;
1729 struct bss_descriptor *bss;
1730 u16 status_code;
1731
1732 lbs_deb_enter(LBS_DEB_ASSOC);
1733
1734 if (!priv->in_progress_assoc_req) {
1735 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1736 ret = -1;
1737 goto done;
1738 }
1739 bss = &priv->in_progress_assoc_req->bss;
1740
1741 passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
1742
1743 /*
1744 * Older FW versions map the IEEE 802.11 Status Code in the association
1745 * response to the following values returned in passocrsp->statuscode:
1746 *
1747 * IEEE Status Code Marvell Status Code
1748 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1749 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1750 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1751 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1752 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1753 * others -> 0x0003 ASSOC_RESULT_REFUSED
1754 *
1755 * Other response codes:
1756 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1757 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1758 * association response from the AP)
1759 */
1760
1761 status_code = le16_to_cpu(passocrsp->statuscode);
1762 switch (status_code) {
1763 case 0x00:
1764 break;
1765 case 0x01:
1766 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1767 break;
1768 case 0x02:
1769 lbs_deb_assoc("ASSOC_RESP: internal timer "
1770 "expired while waiting for the AP\n");
1771 break;
1772 case 0x03:
1773 lbs_deb_assoc("ASSOC_RESP: association "
1774 "refused by AP\n");
1775 break;
1776 case 0x04:
1777 lbs_deb_assoc("ASSOC_RESP: authentication "
1778 "refused by AP\n");
1779 break;
1780 default:
1781 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1782 " unknown\n", status_code);
1783 break;
1784 }
1785
1786 if (status_code) {
1787 lbs_mac_event_disconnected(priv);
1788 ret = -1;
1789 goto done;
1790 }
1791
1792 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1793 le16_to_cpu(resp->size) - S_DS_GEN);
1794
1795 /* Send a Media Connected event, according to the Spec */
1796 priv->connect_status = LBS_CONNECTED;
1797
1798 /* Update current SSID and BSSID */
1799 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1800 priv->curbssparams.ssid_len = bss->ssid_len;
1801 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1802
1803 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1804 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1805
1806 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1807 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1808 priv->nextSNRNF = 0;
1809 priv->numSNRNF = 0;
1810
1811 netif_carrier_on(priv->dev);
1812 if (!priv->tx_pending_len)
1813 netif_wake_queue(priv->dev);
1814
1815 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1816 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1817 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1818
1819done:
1820 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1821 return ret;
1822}
1823
Holger Schurig697900a2008-04-02 16:27:10 +02001824int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
1825 struct cmd_ds_command *resp)
1826{
1827 int ret = 0;
1828 u16 command = le16_to_cpu(resp->command);
1829 u16 result = le16_to_cpu(resp->result);
1830 struct cmd_ds_802_11_ad_hoc_result *padhocresult;
1831 union iwreq_data wrqu;
1832 struct bss_descriptor *bss;
1833 DECLARE_MAC_BUF(mac);
1834
1835 lbs_deb_enter(LBS_DEB_JOIN);
1836
1837 padhocresult = &resp->params.result;
1838
1839 lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
1840 lbs_deb_join("ADHOC_RESP: command = %x\n", command);
1841 lbs_deb_join("ADHOC_RESP: result = %x\n", result);
1842
1843 if (!priv->in_progress_assoc_req) {
1844 lbs_deb_join("ADHOC_RESP: no in-progress association "
1845 "request\n");
1846 ret = -1;
1847 goto done;
1848 }
1849 bss = &priv->in_progress_assoc_req->bss;
1850
1851 /*
1852 * Join result code 0 --> SUCCESS
1853 */
1854 if (result) {
1855 lbs_deb_join("ADHOC_RESP: failed\n");
1856 if (priv->connect_status == LBS_CONNECTED)
1857 lbs_mac_event_disconnected(priv);
1858 ret = -1;
1859 goto done;
1860 }
1861
1862 /*
1863 * Now the join cmd should be successful
1864 * If BSSID has changed use SSID to compare instead of BSSID
1865 */
1866 lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
1867 escape_essid(bss->ssid, bss->ssid_len));
1868
1869 /* Send a Media Connected event, according to the Spec */
1870 priv->connect_status = LBS_CONNECTED;
1871
1872 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1873 /* Update the created network descriptor with the new BSSID */
1874 memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
1875 }
1876
1877 /* Set the BSSID from the joined/started descriptor */
1878 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1879
1880 /* Set the new SSID to current SSID */
1881 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1882 priv->curbssparams.ssid_len = bss->ssid_len;
1883
1884 netif_carrier_on(priv->dev);
1885 if (!priv->tx_pending_len)
1886 netif_wake_queue(priv->dev);
1887
1888 memset(&wrqu, 0, sizeof(wrqu));
1889 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1890 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1891 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1892
1893 lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
1894 lbs_deb_join("ADHOC_RESP: channel = %d\n", priv->curbssparams.channel);
1895 lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
1896 print_mac(mac, padhocresult->bssid));
1897
1898done:
1899 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1900 return ret;
1901}
1902
1903int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv)
1904{
1905 lbs_deb_enter(LBS_DEB_JOIN);
1906
1907 lbs_mac_event_disconnected(priv);
1908
1909 lbs_deb_leave(LBS_DEB_JOIN);
1910 return 0;
1911}