blob: d004170ec417e4d76f9d32fd99a2db91edf67b74 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/* Copyright (C) 2006, Red Hat, Inc. */
2
John W. Linville7e272fc2008-09-24 18:13:14 -04003#include <linux/types.h>
Dan Williams3cf20932007-05-25 17:28:30 -04004#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +01005#include <linux/ieee80211.h>
6#include <linux/if_arp.h>
John W. Linville7e272fc2008-09-24 18:13:14 -04007#include <net/lib80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02008
9#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "host.h"
Holger Schurig245bf202008-04-02 16:27:42 +020012#include "scan.h"
Dan Williams2dd4b262007-12-11 16:54:15 -050013#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040015static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020016
Ihar Hrachyshka5a6e0432008-01-25 14:15:00 +010017static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
18 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
19static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
20 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020021
Dan Williamsbe0d76e2009-05-22 20:05:25 -040022/* The firmware needs the following bits masked out of the beacon-derived
23 * capability field when associating/joining to a BSS:
24 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
Holger Schurig697900a2008-04-02 16:27:10 +020025 */
26#define CAPINFO_MASK (~(0xda00))
27
28
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040029/**
30 * @brief This function finds common rates between rates and card rates.
31 *
32 * It will fill common rates in rates as output if found.
33 *
34 * NOTE: Setting the MSB of the basic rates need to be taken
35 * care, either before or after calling this function
36 *
37 * @param priv A pointer to struct lbs_private structure
38 * @param rates the buffer which keeps input and output
39 * @param rates_size the size of rate1 buffer; new size of buffer on return
40 *
41 * @return 0 on success, or -1 on error
42 */
43static int get_common_rates(struct lbs_private *priv,
44 u8 *rates,
45 u16 *rates_size)
46{
47 u8 *card_rates = lbs_bg_rates;
48 size_t num_card_rates = sizeof(lbs_bg_rates);
49 int ret = 0, i, j;
50 u8 tmp[30];
51 size_t tmp_size = 0;
52
53 /* For each rate in card_rates that exists in rate1, copy to tmp */
54 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
55 for (j = 0; rates[j] && (j < *rates_size); j++) {
56 if (rates[j] == card_rates[i])
57 tmp[tmp_size++] = card_rates[i];
58 }
59 }
60
61 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
62 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
63 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
64 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
65
66 if (!priv->enablehwauto) {
67 for (i = 0; i < tmp_size; i++) {
68 if (tmp[i] == priv->cur_rate)
69 goto done;
70 }
71 lbs_pr_alert("Previously set fixed data rate %#x isn't "
72 "compatible with the network.\n", priv->cur_rate);
73 ret = -1;
74 goto done;
75 }
76 ret = 0;
77
78done:
79 memset(rates, 0, *rates_size);
80 *rates_size = min_t(int, tmp_size, *rates_size);
81 memcpy(rates, tmp, *rates_size);
82 return ret;
83}
84
85
86/**
87 * @brief Sets the MSB on basic rates as the firmware requires
88 *
89 * Scan through an array and set the MSB for basic data rates.
90 *
91 * @param rates buffer of data rates
92 * @param len size of buffer
93 */
94static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
95{
96 int i;
97
98 for (i = 0; i < len; i++) {
99 if (rates[i] == 0x02 || rates[i] == 0x04 ||
100 rates[i] == 0x0b || rates[i] == 0x16)
101 rates[i] |= 0x80;
102 }
103}
104
Holger Schurig697900a2008-04-02 16:27:10 +0200105
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400106static u8 iw_auth_to_ieee_auth(u8 auth)
107{
108 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
109 return 0x00;
110 else if (auth == IW_AUTH_ALG_SHARED_KEY)
111 return 0x01;
112 else if (auth == IW_AUTH_ALG_LEAP)
113 return 0x80;
114
115 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
116 return 0;
117}
118
119/**
120 * @brief This function prepares the authenticate command. AUTHENTICATE only
121 * sets the authentication suite for future associations, as the firmware
122 * handles authentication internally during the ASSOCIATE command.
123 *
124 * @param priv A pointer to struct lbs_private structure
125 * @param bssid The peer BSSID with which to authenticate
126 * @param auth The authentication mode to use (from wireless.h)
127 *
128 * @return 0 or -1
129 */
130static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
131{
132 struct cmd_ds_802_11_authenticate cmd;
133 int ret = -1;
134 DECLARE_MAC_BUF(mac);
135
136 lbs_deb_enter(LBS_DEB_JOIN);
137
138 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
139 memcpy(cmd.bssid, bssid, ETH_ALEN);
140
141 cmd.authtype = iw_auth_to_ieee_auth(auth);
142
143 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
144 print_mac(mac, bssid), cmd.authtype);
145
146 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
147
148 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
149 return ret;
150}
151
Holger Schurig697900a2008-04-02 16:27:10 +0200152/**
153 * @brief Associate to a specific BSS discovered in a scan
154 *
155 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400156 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200157 *
158 * @return 0-success, otherwise fail
159 */
160static int lbs_associate(struct lbs_private *priv,
161 struct assoc_request *assoc_req)
162{
163 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400164 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200165
166 lbs_deb_enter(LBS_DEB_ASSOC);
167
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400168 /* FW v9 and higher indicate authentication suites as a TLV in the
169 * association command, not as a separate authentication command.
170 */
171 if (priv->fwrelease < 0x09000000) {
172 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
173 priv->secinfo.auth_mode);
174 if (ret)
175 goto out;
176 }
Holger Schurig697900a2008-04-02 16:27:10 +0200177
Dan Williamsd5db2df2008-08-21 17:51:07 -0400178 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200179 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
180 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400181 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200182
Dan Williamsd5db2df2008-08-21 17:51:07 -0400183 ret = lbs_set_radio(priv, preamble, 1);
184 if (ret)
185 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200186
187 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
188 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
189
Dan Williamsd5db2df2008-08-21 17:51:07 -0400190out:
Holger Schurig697900a2008-04-02 16:27:10 +0200191 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
192 return ret;
193}
194
195/**
196 * @brief Join an adhoc network found in a previous scan
197 *
198 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400199 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200200 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400201 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200202 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400203static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200204 struct assoc_request *assoc_req)
205{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400206 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200207 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400208 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400209 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400210 u16 ratesize = 0;
211 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400212
213 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200214
215 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400216 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200217 priv->curbssparams.ssid_len),
218 priv->curbssparams.ssid_len);
219 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400220 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200221 bss->ssid_len);
222
223 /* check if the requested SSID is already joined */
224 if (priv->curbssparams.ssid_len &&
225 !lbs_ssid_cmp(priv->curbssparams.ssid,
226 priv->curbssparams.ssid_len,
227 bss->ssid, bss->ssid_len) &&
228 (priv->mode == IW_MODE_ADHOC) &&
229 (priv->connect_status == LBS_CONNECTED)) {
230 union iwreq_data wrqu;
231
232 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
233 "current, not attempting to re-join");
234
235 /* Send the re-association event though, because the association
236 * request really was successful, even if just a null-op.
237 */
238 memset(&wrqu, 0, sizeof(wrqu));
239 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
240 ETH_ALEN);
241 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
242 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
243 goto out;
244 }
245
Dan Williamsd5db2df2008-08-21 17:51:07 -0400246 /* Use short preamble only when both the BSS and firmware support it */
247 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
248 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200249 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400250 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200251 }
252
Dan Williamsd5db2df2008-08-21 17:51:07 -0400253 ret = lbs_set_radio(priv, preamble, 1);
254 if (ret)
255 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200256
257 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
258 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
259
260 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400261 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200262
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400263 /* Build the join command */
264 memset(&cmd, 0, sizeof(cmd));
265 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
266
267 cmd.bss.type = CMD_BSS_TYPE_IBSS;
268 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
269
270 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
271 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
272
Dan Williams5fd164e2009-05-22 20:01:21 -0400273 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400274
Dan Williams5fd164e2009-05-22 20:01:21 -0400275 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
276 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400277
278 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
279 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
280 bss->capability, CAPINFO_MASK);
281
282 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700283 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
284 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400285
286 /* Only v8 and below support setting these */
287 if (priv->fwrelease < 0x09000000) {
288 /* failtimeout */
289 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
290 /* probedelay */
291 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
292 }
293
294 /* Copy Data rates from the rates recorded in scan response */
295 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
296 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
297 memcpy(cmd.bss.rates, bss->rates, ratesize);
298 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
299 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
300 ret = -1;
301 goto out;
302 }
303
304 /* Copy the ad-hoc creation rates into Current BSS state structure */
305 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
306 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
307
308 /* Set MSB on basic rates as the firmware requires, but _after_
309 * copying to current bss rates.
310 */
311 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
312
Dan Williams5fd164e2009-05-22 20:01:21 -0400313 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400314
315 if (assoc_req->secinfo.wep_enabled) {
316 u16 tmp = le16_to_cpu(cmd.bss.capability);
317 tmp |= WLAN_CAPABILITY_PRIVACY;
318 cmd.bss.capability = cpu_to_le16(tmp);
319 }
320
321 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
322 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
323
324 /* wake up first */
325 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
326 CMD_ACT_SET, 0, 0,
327 &local_ps_mode);
328 if (ret) {
329 ret = -1;
330 goto out;
331 }
332 }
333
334 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
335 ret = -1;
336 goto out;
337 }
338
339 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
340 if (ret == 0)
341 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200342
343out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400344 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200345 return ret;
346}
347
348/**
349 * @brief Start an Adhoc Network
350 *
351 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400352 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400353 *
354 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200355 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400356static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200357 struct assoc_request *assoc_req)
358{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400359 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400360 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400361 size_t ratesize = 0;
362 u16 tmpcap = 0;
363 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400364 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -0400365
366 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200367
Holger Schurig697900a2008-04-02 16:27:10 +0200368 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400369 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400370 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200371 }
372
Dan Williamsd5db2df2008-08-21 17:51:07 -0400373 ret = lbs_set_radio(priv, preamble, 1);
374 if (ret)
375 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200376
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400377 /* Build the start command */
378 memset(&cmd, 0, sizeof(cmd));
379 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200380
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400381 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
382
383 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400384 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400385 assoc_req->ssid_len);
386
387 cmd.bsstype = CMD_BSS_TYPE_IBSS;
388
389 if (priv->beacon_period == 0)
390 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
391 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
392
393 WARN_ON(!assoc_req->channel);
394
395 /* set Physical parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400396 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
397 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -0400398 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400399
400 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400401 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
402 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -0400403 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400404
405 /* set capability info */
406 tmpcap = WLAN_CAPABILITY_IBSS;
407 if (assoc_req->secinfo.wep_enabled) {
408 lbs_deb_join("ADHOC_START: WEP enabled, setting privacy on\n");
409 tmpcap |= WLAN_CAPABILITY_PRIVACY;
410 } else
411 lbs_deb_join("ADHOC_START: WEP disabled, setting privacy off\n");
412
413 cmd.capability = cpu_to_le16(tmpcap);
414
415 /* Only v8 and below support setting probe delay */
416 if (priv->fwrelease < 0x09000000)
417 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
418
419 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
420 memcpy(cmd.rates, lbs_bg_rates, ratesize);
421
422 /* Copy the ad-hoc creating rates into Current BSS state structure */
423 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
424 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
425
426 /* Set MSB on basic rates as the firmware requires, but _after_
427 * copying to current bss rates.
428 */
429 lbs_set_basic_rate_flags(cmd.rates, ratesize);
430
431 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
432 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
433
434 if (lbs_create_dnld_countryinfo_11d(priv)) {
435 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
436 ret = -1;
437 goto out;
438 }
439
440 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
441 assoc_req->channel, assoc_req->band);
442
443 priv->adhoccreate = 1;
444 priv->mode = IW_MODE_ADHOC;
445
446 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
447 if (ret == 0)
448 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200449
Dan Williamsd5db2df2008-08-21 17:51:07 -0400450out:
451 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200452 return ret;
453}
454
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400455/**
456 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
457 *
458 * @param priv A pointer to struct lbs_private structure
459 * @return 0 on success, or an error
460 */
461int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200462{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400463 struct cmd_ds_802_11_ad_hoc_stop cmd;
464 int ret;
465
466 lbs_deb_enter(LBS_DEB_JOIN);
467
468 memset(&cmd, 0, sizeof (cmd));
469 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
470
471 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
472
473 /* Clean up everything even if there was an error */
474 lbs_mac_event_disconnected(priv);
475
476 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
477 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200478}
Dan Williamse76850d2007-05-25 17:09:41 -0400479
Holger Schurig245bf202008-04-02 16:27:42 +0200480static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
481 struct bss_descriptor *match_bss)
482{
483 if (!secinfo->wep_enabled && !secinfo->WPAenabled
484 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100485 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
486 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +0200487 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
488 return 1;
489 else
490 return 0;
491}
492
493static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
494 struct bss_descriptor *match_bss)
495{
496 if (secinfo->wep_enabled && !secinfo->WPAenabled
497 && !secinfo->WPA2enabled
498 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
499 return 1;
500 else
501 return 0;
502}
503
504static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
505 struct bss_descriptor *match_bss)
506{
507 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100508 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +0200509 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
510 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
511 )
512 return 1;
513 else
514 return 0;
515}
516
517static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
518 struct bss_descriptor *match_bss)
519{
520 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +0100521 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200522 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
523 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
524 )
525 return 1;
526 else
527 return 0;
528}
529
530static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
531 struct bss_descriptor *match_bss)
532{
533 if (!secinfo->wep_enabled && !secinfo->WPAenabled
534 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100535 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
536 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200537 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
538 return 1;
539 else
540 return 0;
541}
542
543/**
544 * @brief Check if a scanned network compatible with the driver settings
545 *
546 * WEP WPA WPA2 ad-hoc encrypt Network
547 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
548 * 0 0 0 0 NONE 0 0 0 yes No security
549 * 1 0 0 0 NONE 1 0 0 yes Static WEP
550 * 0 1 0 0 x 1x 1 x yes WPA
551 * 0 0 1 0 x 1x x 1 yes WPA2
552 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
553 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
554 *
555 *
556 * @param priv A pointer to struct lbs_private
557 * @param index Index in scantable to check against current driver settings
558 * @param mode Network mode: Infrastructure or IBSS
559 *
560 * @return Index in scantable, or error code if negative
561 */
562static int is_network_compatible(struct lbs_private *priv,
563 struct bss_descriptor *bss, uint8_t mode)
564{
565 int matched = 0;
566
567 lbs_deb_enter(LBS_DEB_SCAN);
568
569 if (bss->mode != mode)
570 goto done;
571
572 matched = match_bss_no_security(&priv->secinfo, bss);
573 if (matched)
574 goto done;
575 matched = match_bss_static_wep(&priv->secinfo, bss);
576 if (matched)
577 goto done;
578 matched = match_bss_wpa(&priv->secinfo, bss);
579 if (matched) {
580 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
581 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
582 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
583 priv->secinfo.wep_enabled ? "e" : "d",
584 priv->secinfo.WPAenabled ? "e" : "d",
585 priv->secinfo.WPA2enabled ? "e" : "d",
586 (bss->capability & WLAN_CAPABILITY_PRIVACY));
587 goto done;
588 }
589 matched = match_bss_wpa2(&priv->secinfo, bss);
590 if (matched) {
591 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
592 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
593 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
594 priv->secinfo.wep_enabled ? "e" : "d",
595 priv->secinfo.WPAenabled ? "e" : "d",
596 priv->secinfo.WPA2enabled ? "e" : "d",
597 (bss->capability & WLAN_CAPABILITY_PRIVACY));
598 goto done;
599 }
600 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
601 if (matched) {
602 lbs_deb_scan("is_network_compatible() dynamic WEP: "
603 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
604 bss->wpa_ie[0], bss->rsn_ie[0],
605 (bss->capability & WLAN_CAPABILITY_PRIVACY));
606 goto done;
607 }
608
609 /* bss security settings don't match those configured on card */
610 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
611 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
612 bss->wpa_ie[0], bss->rsn_ie[0],
613 priv->secinfo.wep_enabled ? "e" : "d",
614 priv->secinfo.WPAenabled ? "e" : "d",
615 priv->secinfo.WPA2enabled ? "e" : "d",
616 (bss->capability & WLAN_CAPABILITY_PRIVACY));
617
618done:
619 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
620 return matched;
621}
622
623/**
624 * @brief This function finds a specific compatible BSSID in the scan list
625 *
626 * Used in association code
627 *
628 * @param priv A pointer to struct lbs_private
629 * @param bssid BSSID to find in the scan list
630 * @param mode Network mode: Infrastructure or IBSS
631 *
632 * @return index in BSSID list, or error return code (< 0)
633 */
634static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
635 uint8_t *bssid, uint8_t mode)
636{
637 struct bss_descriptor *iter_bss;
638 struct bss_descriptor *found_bss = NULL;
639
640 lbs_deb_enter(LBS_DEB_SCAN);
641
642 if (!bssid)
643 goto out;
644
645 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
646
647 /* Look through the scan table for a compatible match. The loop will
648 * continue past a matched bssid that is not compatible in case there
649 * is an AP with multiple SSIDs assigned to the same BSSID
650 */
651 mutex_lock(&priv->lock);
652 list_for_each_entry(iter_bss, &priv->network_list, list) {
653 if (compare_ether_addr(iter_bss->bssid, bssid))
654 continue; /* bssid doesn't match */
655 switch (mode) {
656 case IW_MODE_INFRA:
657 case IW_MODE_ADHOC:
658 if (!is_network_compatible(priv, iter_bss, mode))
659 break;
660 found_bss = iter_bss;
661 break;
662 default:
663 found_bss = iter_bss;
664 break;
665 }
666 }
667 mutex_unlock(&priv->lock);
668
669out:
670 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
671 return found_bss;
672}
673
674/**
675 * @brief This function finds ssid in ssid list.
676 *
677 * Used in association code
678 *
679 * @param priv A pointer to struct lbs_private
680 * @param ssid SSID to find in the list
681 * @param bssid BSSID to qualify the SSID selection (if provided)
682 * @param mode Network mode: Infrastructure or IBSS
683 *
684 * @return index in BSSID list
685 */
686static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
687 uint8_t *ssid, uint8_t ssid_len,
688 uint8_t *bssid, uint8_t mode,
689 int channel)
690{
691 u32 bestrssi = 0;
692 struct bss_descriptor *iter_bss = NULL;
693 struct bss_descriptor *found_bss = NULL;
694 struct bss_descriptor *tmp_oldest = NULL;
695
696 lbs_deb_enter(LBS_DEB_SCAN);
697
698 mutex_lock(&priv->lock);
699
700 list_for_each_entry(iter_bss, &priv->network_list, list) {
701 if (!tmp_oldest ||
702 (iter_bss->last_scanned < tmp_oldest->last_scanned))
703 tmp_oldest = iter_bss;
704
705 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
706 ssid, ssid_len) != 0)
707 continue; /* ssid doesn't match */
708 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
709 continue; /* bssid doesn't match */
710 if ((channel > 0) && (iter_bss->channel != channel))
711 continue; /* channel doesn't match */
712
713 switch (mode) {
714 case IW_MODE_INFRA:
715 case IW_MODE_ADHOC:
716 if (!is_network_compatible(priv, iter_bss, mode))
717 break;
718
719 if (bssid) {
720 /* Found requested BSSID */
721 found_bss = iter_bss;
722 goto out;
723 }
724
725 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
726 bestrssi = SCAN_RSSI(iter_bss->rssi);
727 found_bss = iter_bss;
728 }
729 break;
730 case IW_MODE_AUTO:
731 default:
732 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
733 bestrssi = SCAN_RSSI(iter_bss->rssi);
734 found_bss = iter_bss;
735 }
736 break;
737 }
738 }
739
740out:
741 mutex_unlock(&priv->lock);
742 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
743 return found_bss;
744}
745
Holger Schurig69f90322007-11-23 15:43:44 +0100746static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747 struct assoc_request * assoc_req)
748{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400750 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400751 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -0400752 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753
Holger Schurig9012b282007-05-25 11:27:16 -0400754 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200755
Dan Williamsef9a2642007-05-25 16:46:33 -0400756 /* FIXME: take channel into account when picking SSIDs if a channel
757 * is set.
758 */
759
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400760 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
761 channel = assoc_req->channel;
762
Holger Schurig0765af42007-10-15 12:55:56 +0200763 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400764 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -0400765 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500766 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100767 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768
David Woodhouseaa21c002007-12-08 20:04:36 +0000769 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400770 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400771 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -0400772 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -0500773 ret = lbs_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200774 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -0400775 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400777 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200778 /* Scan for the network, do not save previous results. Stale
779 * scan data will cause us to join a non-existant adhoc network
780 */
Holger Schurig10078322007-11-15 18:05:47 -0500781 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100782 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200783
784 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000785 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400786 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400787 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -0400788 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400789 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400790 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200791 } else {
792 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -0400793 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400794 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400795 IW_ESSID_MAX_SIZE);
796 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400797 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200798 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200799 }
800
Holger Schurig9012b282007-05-25 11:27:16 -0400801 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200802 return ret;
803}
804
805
Holger Schurig69f90322007-11-23 15:43:44 +0100806static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807 struct assoc_request * assoc_req)
808{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400809 int ret = 0;
810 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811
Johannes Berge1749612008-10-27 15:59:26 -0700812 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813
814 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +0000815 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200816 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400817 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -0700818 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
819 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200820 goto out;
821 }
822
Dan Williamse76850d2007-05-25 17:09:41 -0400823 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400824 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500825 ret = lbs_associate(priv, assoc_req);
826 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400827 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400828 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200830
831out:
Holger Schurig9012b282007-05-25 11:27:16 -0400832 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200833 return ret;
834}
835
836
Holger Schurig69f90322007-11-23 15:43:44 +0100837static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200838 struct assoc_request * assoc_req)
839{
840 int ret = 0, done = 0;
841
Holger Schurig0765af42007-10-15 12:55:56 +0200842 lbs_deb_enter(LBS_DEB_ASSOC);
843
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200844 /* If we're given and 'any' BSSID, try associating based on SSID */
845
846 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -0400847 if (compare_ether_addr(bssid_any, assoc_req->bssid)
848 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200849 ret = assoc_helper_bssid(priv, assoc_req);
850 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200851 }
852 }
853
854 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
855 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200856 }
857
Holger Schurig0765af42007-10-15 12:55:56 +0200858 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200859 return ret;
860}
861
862
Holger Schurig69f90322007-11-23 15:43:44 +0100863static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200864 struct assoc_request * assoc_req)
865{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200866 int ret = 0;
867
Holger Schurig9012b282007-05-25 11:27:16 -0400868 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200869
David Woodhouseaa21c002007-12-08 20:04:36 +0000870 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -0400871 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200872
Dan Williams0dc5a292007-05-10 22:58:02 -0400873 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000874 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -0500875 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +0000876 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877 }
878
David Woodhouseaa21c002007-12-08 20:04:36 +0000879 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400880 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200881
Holger Schurig9012b282007-05-25 11:27:16 -0400882done:
883 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200884 return ret;
885}
886
Holger Schurig69f90322007-11-23 15:43:44 +0100887static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -0400888 struct assoc_request * assoc_req)
889{
Dan Williamsef9a2642007-05-25 16:46:33 -0400890 int ret = 0;
891
892 lbs_deb_enter(LBS_DEB_ASSOC);
893
David Woodhouse9f462572007-12-12 22:50:21 -0500894 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500895 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500896 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500897 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -0400898 }
899
David Woodhouseaa21c002007-12-08 20:04:36 +0000900 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400901 goto done;
902
David Woodhouse8642f1f2007-12-11 20:03:01 -0500903 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -0500904 /* Change mesh channel first; 21.p21 firmware won't let
905 you change channel otherwise (even though it'll return
906 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700907 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
908 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500909 }
910
Dan Williamsef9a2642007-05-25 16:46:33 -0400911 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -0500912 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -0400913
Dan Williams2dd4b262007-12-11 16:54:15 -0500914 ret = lbs_set_channel(priv, assoc_req->channel);
915 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -0500916 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -0400917
Dan Williams2dd4b262007-12-11 16:54:15 -0500918 /* FIXME: shouldn't need to grab the channel _again_ after setting
919 * it since the firmware is supposed to return the new channel, but
920 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -0500921 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500922 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500923 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500924 goto done;
925 }
Dan Williamsef9a2642007-05-25 16:46:33 -0400926
David Woodhouseaa21c002007-12-08 20:04:36 +0000927 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -0500928 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -0400929 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500930 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -0400931 }
932
933 if ( assoc_req->secinfo.wep_enabled
934 && (assoc_req->wep_keys[0].len
935 || assoc_req->wep_keys[1].len
936 || assoc_req->wep_keys[2].len
937 || assoc_req->wep_keys[3].len)) {
938 /* Make sure WEP keys are re-sent to firmware */
939 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
940 }
941
942 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -0500943 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -0400944
David Woodhouse8642f1f2007-12-11 20:03:01 -0500945 restore_mesh:
946 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700947 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
948 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500949
950 done:
Dan Williamsef9a2642007-05-25 16:46:33 -0400951 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
952 return ret;
953}
954
955
Holger Schurig69f90322007-11-23 15:43:44 +0100956static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -0500957 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200958{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200959 int i;
960 int ret = 0;
961
Holger Schurig9012b282007-05-25 11:27:16 -0400962 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200963
964 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -0500965 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
966 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
967 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
968 else
969 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200970
971 if (ret)
972 goto out;
973
974 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400975 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +0100976 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200977 else
Holger Schurigd9e97782008-03-12 16:06:43 +0100978 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -0500979
Holger Schurigc97329e2008-03-18 11:20:21 +0100980 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981
David Woodhouseaa21c002007-12-08 20:04:36 +0000982 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983
David Woodhouseaa21c002007-12-08 20:04:36 +0000984 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200985 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000986 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -0500987 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000989 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200990
David Woodhouseaa21c002007-12-08 20:04:36 +0000991 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200992
993out:
Holger Schurig9012b282007-05-25 11:27:16 -0400994 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995 return ret;
996}
997
Holger Schurig69f90322007-11-23 15:43:44 +0100998static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200999 struct assoc_request * assoc_req)
1000{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001002 uint16_t do_wpa;
1003 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004
Holger Schurig9012b282007-05-25 11:27:16 -04001005 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001006
David Woodhouseaa21c002007-12-08 20:04:36 +00001007 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001008 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001009
Holger Schurigc97329e2008-03-18 11:20:21 +01001010 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001011
Dan Williams18c96c342007-06-18 12:01:12 -04001012 /* If RSN is already enabled, don't try to enable it again, since
1013 * ENABLE_RSN resets internal state machines and will clobber the
1014 * 4-way WPA handshake.
1015 */
1016
1017 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001018 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001019 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001020 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001021 goto out;
1022 }
1023
1024 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001025 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001026 if (do_wpa == rsn)
1027 goto out;
1028
1029 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001030 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001031
1032out:
Holger Schurig9012b282007-05-25 11:27:16 -04001033 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001034 return ret;
1035}
1036
1037
Holger Schurig69f90322007-11-23 15:43:44 +01001038static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001039 struct assoc_request * assoc_req)
1040{
1041 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001042 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001043
Holger Schurig9012b282007-05-25 11:27:16 -04001044 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045
Dan Williams2bcde512007-10-03 10:37:45 -04001046 /* Work around older firmware bug where WPA unicast and multicast
1047 * keys must be set independently. Seen in SDIO parts with firmware
1048 * version 5.0.11p0.
1049 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050
Dan Williams2bcde512007-10-03 10:37:45 -04001051 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1052 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001053 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001054 assoc_req->flags = flags;
1055 }
1056
1057 if (ret)
1058 goto out;
1059
1060 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1061 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1062
David Woodhouse9e1228d2008-03-03 12:15:39 +01001063 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001064 assoc_req->flags = flags;
1065 }
1066
1067out:
Holger Schurig9012b282007-05-25 11:27:16 -04001068 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069 return ret;
1070}
1071
1072
Holger Schurig69f90322007-11-23 15:43:44 +01001073static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001074 struct assoc_request * assoc_req)
1075{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 int ret = 0;
1077
Holger Schurig9012b282007-05-25 11:27:16 -04001078 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001079
1080 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001081 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1082 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001084 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1085 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001086 }
1087
Holger Schurig9012b282007-05-25 11:27:16 -04001088 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001089 return ret;
1090}
1091
1092
David Woodhouseaa21c002007-12-08 20:04:36 +00001093static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001094 struct assoc_request * assoc_req)
1095{
Holger Schurig0765af42007-10-15 12:55:56 +02001096 int ret = 0;
1097
David Woodhouseaa21c002007-12-08 20:04:36 +00001098 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001099 return 0;
1100
Holger Schurig52507c22008-01-28 17:25:53 +01001101 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001102 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001103 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1104 ret = 1;
1105 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106 }
1107
1108 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001109 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001110 lbs_deb_assoc("Deauthenticating due to new security\n");
1111 ret = 1;
1112 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001113 }
1114 }
1115
1116 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001117 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1118 ret = 1;
1119 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001120 }
1121
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001122 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001123 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1124 ret = 1;
1125 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001126 }
1127
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001128 /* FIXME: deal with 'auto' mode somehow */
1129 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001130 if (assoc_req->mode != IW_MODE_INFRA) {
1131 lbs_deb_assoc("Deauthenticating due to leaving "
1132 "infra mode\n");
1133 ret = 1;
1134 goto out;
1135 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136 }
1137
Holger Schurig0765af42007-10-15 12:55:56 +02001138out:
1139 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001140 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141}
1142
1143
David Woodhouseaa21c002007-12-08 20:04:36 +00001144static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001145 struct assoc_request * assoc_req)
1146{
Holger Schurig0765af42007-10-15 12:55:56 +02001147 lbs_deb_enter(LBS_DEB_ASSOC);
1148
David Woodhouseaa21c002007-12-08 20:04:36 +00001149 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001150 return 0;
1151
David Woodhouseaa21c002007-12-08 20:04:36 +00001152 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1153 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001154 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001155 return 1;
1156
1157 /* FIXME: deal with 'auto' mode somehow */
1158 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001159 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001160 return 1;
1161 }
1162
Dan Williamsef9a2642007-05-25 16:46:33 -04001163 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001164 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001165 return 1;
1166 }
1167
Holger Schurig0765af42007-10-15 12:55:56 +02001168 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001169 return 0;
1170}
1171
1172
Holger Schurig245bf202008-04-02 16:27:42 +02001173/**
1174 * @brief This function finds the best SSID in the Scan List
1175 *
1176 * Search the scan table for the best SSID that also matches the current
1177 * adapter network preference (infrastructure or adhoc)
1178 *
1179 * @param priv A pointer to struct lbs_private
1180 *
1181 * @return index in BSSID list
1182 */
1183static struct bss_descriptor *lbs_find_best_ssid_in_list(
1184 struct lbs_private *priv, uint8_t mode)
1185{
1186 uint8_t bestrssi = 0;
1187 struct bss_descriptor *iter_bss;
1188 struct bss_descriptor *best_bss = NULL;
1189
1190 lbs_deb_enter(LBS_DEB_SCAN);
1191
1192 mutex_lock(&priv->lock);
1193
1194 list_for_each_entry(iter_bss, &priv->network_list, list) {
1195 switch (mode) {
1196 case IW_MODE_INFRA:
1197 case IW_MODE_ADHOC:
1198 if (!is_network_compatible(priv, iter_bss, mode))
1199 break;
1200 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1201 break;
1202 bestrssi = SCAN_RSSI(iter_bss->rssi);
1203 best_bss = iter_bss;
1204 break;
1205 case IW_MODE_AUTO:
1206 default:
1207 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1208 break;
1209 bestrssi = SCAN_RSSI(iter_bss->rssi);
1210 best_bss = iter_bss;
1211 break;
1212 }
1213 }
1214
1215 mutex_unlock(&priv->lock);
1216 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1217 return best_bss;
1218}
1219
1220/**
1221 * @brief Find the best AP
1222 *
1223 * Used from association worker.
1224 *
1225 * @param priv A pointer to struct lbs_private structure
1226 * @param pSSID A pointer to AP's ssid
1227 *
1228 * @return 0--success, otherwise--fail
1229 */
1230static int lbs_find_best_network_ssid(struct lbs_private *priv,
1231 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1232 uint8_t *out_mode)
1233{
1234 int ret = -1;
1235 struct bss_descriptor *found;
1236
1237 lbs_deb_enter(LBS_DEB_SCAN);
1238
1239 priv->scan_ssid_len = 0;
1240 lbs_scan_networks(priv, 1);
1241 if (priv->surpriseremoved)
1242 goto out;
1243
1244 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1245 if (found && (found->ssid_len > 0)) {
1246 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1247 *out_ssid_len = found->ssid_len;
1248 *out_mode = found->mode;
1249 ret = 0;
1250 }
1251
1252out:
1253 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1254 return ret;
1255}
1256
1257
Holger Schurig10078322007-11-15 18:05:47 -05001258void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001259{
Holger Schurig69f90322007-11-23 15:43:44 +01001260 struct lbs_private *priv = container_of(work, struct lbs_private,
1261 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001262 struct assoc_request * assoc_req = NULL;
1263 int ret = 0;
1264 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001265 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266
Holger Schurig9012b282007-05-25 11:27:16 -04001267 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001268
David Woodhouseaa21c002007-12-08 20:04:36 +00001269 mutex_lock(&priv->lock);
1270 assoc_req = priv->pending_assoc_req;
1271 priv->pending_assoc_req = NULL;
1272 priv->in_progress_assoc_req = assoc_req;
1273 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001274
Holger Schurig9012b282007-05-25 11:27:16 -04001275 if (!assoc_req)
1276 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001277
Holger Schurig0765af42007-10-15 12:55:56 +02001278 lbs_deb_assoc(
1279 "Association Request:\n"
1280 " flags: 0x%08lx\n"
1281 " SSID: '%s'\n"
1282 " chann: %d\n"
1283 " band: %d\n"
1284 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001285 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001286 " secinfo: %s%s%s\n"
1287 " auth_mode: %d\n",
1288 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001289 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001290 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001291 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001292 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1293 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1294 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1295 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001296
1297 /* If 'any' SSID was specified, find an SSID to associate with */
1298 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001299 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001300 find_any_ssid = 1;
1301
1302 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1303 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001304 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1305 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001306 find_any_ssid = 0;
1307 }
1308
1309 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001310 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311
Holger Schurig10078322007-11-15 18:05:47 -05001312 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001313 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001314 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001315 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001316 ret = -ENETUNREACH;
1317 goto out;
1318 }
1319
1320 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001321 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001322 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1323 assoc_req->mode = new_mode;
1324 }
1325 }
1326
1327 /*
1328 * Check if the attributes being changing require deauthentication
1329 * from the currently associated infrastructure access point.
1330 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001331 if (priv->mode == IW_MODE_INFRA) {
1332 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001333 ret = lbs_cmd_80211_deauthenticate(priv,
1334 priv->curbssparams.bssid,
1335 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001336 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001337 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001338 "configuration request failed: %d\n",
1339 ret);
1340 }
1341 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001342 } else if (priv->mode == IW_MODE_ADHOC) {
1343 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001344 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001345 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001346 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347 "new configuration request failed: %d\n",
1348 ret);
1349 }
1350
1351 }
1352 }
1353
1354 /* Send the various configuration bits to the firmware */
1355 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1356 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001357 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001358 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001359 }
1360
Dan Williamsef9a2642007-05-25 16:46:33 -04001361 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1362 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001363 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001364 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001365 }
1366
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001367 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1368 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1369 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001370 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001371 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001372 }
1373
1374 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1375 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001376 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001377 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001378 }
1379
1380 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1381 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001382 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001383 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001384 }
1385
1386 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1387 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1388 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001389 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001390 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001391 }
1392
1393 /* SSID/BSSID should be the _last_ config option set, because they
1394 * trigger the association attempt.
1395 */
1396 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1397 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1398 int success = 1;
1399
1400 ret = assoc_helper_associate(priv, assoc_req);
1401 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001402 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001403 ret);
1404 success = 0;
1405 }
1406
David Woodhouseaa21c002007-12-08 20:04:36 +00001407 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001408 lbs_deb_assoc("ASSOC: association unsuccessful, "
1409 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001410 success = 0;
1411 }
1412
1413 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001414 lbs_deb_assoc("associated to %pM\n",
1415 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001416 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001417 CMD_802_11_RSSI,
1418 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001419 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001420 ret = -1;
1421 }
1422 }
1423
1424out:
1425 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001426 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001427 ret);
1428 }
Dan Williamse76850d2007-05-25 17:09:41 -04001429
David Woodhouseaa21c002007-12-08 20:04:36 +00001430 mutex_lock(&priv->lock);
1431 priv->in_progress_assoc_req = NULL;
1432 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001433 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001434
1435done:
1436 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001437}
1438
1439
1440/*
1441 * Caller MUST hold any necessary locks
1442 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001443struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001444{
1445 struct assoc_request * assoc_req;
1446
Holger Schurig0765af42007-10-15 12:55:56 +02001447 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001448 if (!priv->pending_assoc_req) {
1449 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001450 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001451 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001452 lbs_pr_info("Not enough memory to allocate association"
1453 " request!\n");
1454 return NULL;
1455 }
1456 }
1457
1458 /* Copy current configuration attributes to the association request,
1459 * but don't overwrite any that are already set.
1460 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001461 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001462 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001463 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001464 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001465 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001466 }
1467
1468 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001469 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001470
Dan Williamse76850d2007-05-25 17:09:41 -04001471 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001472 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001473
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001474 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001475 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001476
1477 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001478 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001479 ETH_ALEN);
1480 }
1481
1482 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1483 int i;
1484 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001485 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001486 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001487 }
1488 }
1489
1490 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001491 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001492
1493 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001494 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001495 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001496 }
1497
1498 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001499 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001500 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001501 }
1502
1503 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001504 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001505 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001506 }
1507
1508 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001509 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001510 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001511 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001512 }
1513
Holger Schurig0765af42007-10-15 12:55:56 +02001514 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001515 return assoc_req;
1516}
Holger Schurig697900a2008-04-02 16:27:10 +02001517
1518
1519/**
Dan Williams191bb402008-08-21 17:46:18 -04001520 * @brief Deauthenticate from a specific BSS
1521 *
1522 * @param priv A pointer to struct lbs_private structure
1523 * @param bssid The specific BSS to deauthenticate from
1524 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1525 *
1526 * @return 0 on success, error on failure
1527 */
1528int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1529 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001530{
Dan Williams191bb402008-08-21 17:46:18 -04001531 struct cmd_ds_802_11_deauthenticate cmd;
1532 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001533
1534 lbs_deb_enter(LBS_DEB_JOIN);
1535
Dan Williams191bb402008-08-21 17:46:18 -04001536 memset(&cmd, 0, sizeof(cmd));
1537 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1538 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1539 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001540
Dan Williams191bb402008-08-21 17:46:18 -04001541 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001542
Dan Williams191bb402008-08-21 17:46:18 -04001543 /* Clean up everything even if there was an error; can't assume that
1544 * we're still authenticated to the AP after trying to deauth.
1545 */
1546 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001547
1548 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001549 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001550}
1551
1552int lbs_cmd_80211_associate(struct lbs_private *priv,
1553 struct cmd_ds_command *cmd, void *pdata_buf)
1554{
1555 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1556 int ret = 0;
1557 struct assoc_request *assoc_req = pdata_buf;
1558 struct bss_descriptor *bss = &assoc_req->bss;
1559 u8 *pos;
Dan Williamsbe0d76e2009-05-22 20:05:25 -04001560 u16 tmpcap, tmplen, tmpauth;
Dan Williams75b6a612009-05-22 20:03:09 -04001561 struct mrvl_ie_ssid_param_set *ssid;
1562 struct mrvl_ie_ds_param_set *ds;
1563 struct mrvl_ie_cf_param_set *cf;
1564 struct mrvl_ie_rates_param_set *rates;
1565 struct mrvl_ie_rsn_param_set *rsn;
Dan Williamsbe0d76e2009-05-22 20:05:25 -04001566 struct mrvl_ie_auth_type *auth;
Holger Schurig697900a2008-04-02 16:27:10 +02001567
1568 lbs_deb_enter(LBS_DEB_ASSOC);
1569
1570 pos = (u8 *) passo;
1571
1572 if (!priv) {
1573 ret = -1;
1574 goto done;
1575 }
1576
1577 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1578
1579 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1580 pos += sizeof(passo->peerstaaddr);
1581
1582 /* set the listen interval */
1583 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1584
1585 pos += sizeof(passo->capability);
1586 pos += sizeof(passo->listeninterval);
1587 pos += sizeof(passo->bcnperiod);
1588 pos += sizeof(passo->dtimperiod);
1589
Dan Williams75b6a612009-05-22 20:03:09 -04001590 ssid = (struct mrvl_ie_ssid_param_set *) pos;
Holger Schurig697900a2008-04-02 16:27:10 +02001591 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1592 tmplen = bss->ssid_len;
1593 ssid->header.len = cpu_to_le16(tmplen);
1594 memcpy(ssid->ssid, bss->ssid, tmplen);
1595 pos += sizeof(ssid->header) + tmplen;
1596
Dan Williams75b6a612009-05-22 20:03:09 -04001597 ds = (struct mrvl_ie_ds_param_set *) pos;
Dan Williams5fd164e2009-05-22 20:01:21 -04001598 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1599 ds->header.len = cpu_to_le16(1);
1600 ds->channel = bss->phy.ds.channel;
1601 pos += sizeof(ds->header) + 1;
Holger Schurig697900a2008-04-02 16:27:10 +02001602
Dan Williams75b6a612009-05-22 20:03:09 -04001603 cf = (struct mrvl_ie_cf_param_set *) pos;
Dan Williams5fd164e2009-05-22 20:01:21 -04001604 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
1605 tmplen = sizeof(*cf) - sizeof (cf->header);
1606 cf->header.len = cpu_to_le16(tmplen);
1607 /* IE payload should be zeroed, firmware fills it in for us */
1608 pos += sizeof(*cf);
Holger Schurig697900a2008-04-02 16:27:10 +02001609
Dan Williams75b6a612009-05-22 20:03:09 -04001610 rates = (struct mrvl_ie_rates_param_set *) pos;
Holger Schurig697900a2008-04-02 16:27:10 +02001611 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1612 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1613 tmplen = MAX_RATES;
1614 if (get_common_rates(priv, rates->rates, &tmplen)) {
1615 ret = -1;
1616 goto done;
1617 }
1618 pos += sizeof(rates->header) + tmplen;
1619 rates->header.len = cpu_to_le16(tmplen);
1620 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1621
1622 /* Copy the infra. association rates into Current BSS state structure */
1623 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1624 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1625
1626 /* Set MSB on basic rates as the firmware requires, but _after_
1627 * copying to current bss rates.
1628 */
1629 lbs_set_basic_rate_flags(rates->rates, tmplen);
1630
Dan Williamsbe0d76e2009-05-22 20:05:25 -04001631 /* Firmware v9+ indicate authentication suites as a TLV */
1632 if (priv->fwrelease >= 0x09000000) {
1633 DECLARE_MAC_BUF(mac);
1634
1635 auth = (struct mrvl_ie_auth_type *) pos;
1636 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
1637 auth->header.len = cpu_to_le16(2);
1638 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
1639 auth->auth = cpu_to_le16(tmpauth);
1640 pos += sizeof(auth->header) + 2;
1641
1642 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
1643 print_mac(mac, bss->bssid), priv->secinfo.auth_mode);
1644 }
1645
Holger Schurig697900a2008-04-02 16:27:10 +02001646 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
Dan Williams75b6a612009-05-22 20:03:09 -04001647 rsn = (struct mrvl_ie_rsn_param_set *) pos;
Holger Schurig697900a2008-04-02 16:27:10 +02001648 /* WPA_IE or WPA2_IE */
1649 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1650 tmplen = (u16) assoc_req->wpa_ie[1];
1651 rsn->header.len = cpu_to_le16(tmplen);
1652 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1653 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1654 sizeof(rsn->header) + tmplen);
1655 pos += sizeof(rsn->header) + tmplen;
1656 }
1657
1658 /* update curbssparams */
Dan Williams5fd164e2009-05-22 20:01:21 -04001659 priv->curbssparams.channel = bss->phy.ds.channel;
Holger Schurig697900a2008-04-02 16:27:10 +02001660
1661 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1662 ret = -1;
1663 goto done;
1664 }
1665
1666 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1667
1668 /* set the capability info */
1669 tmpcap = (bss->capability & CAPINFO_MASK);
1670 if (bss->mode == IW_MODE_INFRA)
1671 tmpcap |= WLAN_CAPABILITY_ESS;
1672 passo->capability = cpu_to_le16(tmpcap);
1673 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1674
1675done:
1676 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1677 return ret;
1678}
1679
Holger Schurig697900a2008-04-02 16:27:10 +02001680int lbs_ret_80211_associate(struct lbs_private *priv,
1681 struct cmd_ds_command *resp)
1682{
1683 int ret = 0;
1684 union iwreq_data wrqu;
Dan Williams5fd164e2009-05-22 20:01:21 -04001685 struct ieee_assoc_response *passocrsp;
Holger Schurig697900a2008-04-02 16:27:10 +02001686 struct bss_descriptor *bss;
1687 u16 status_code;
1688
1689 lbs_deb_enter(LBS_DEB_ASSOC);
1690
1691 if (!priv->in_progress_assoc_req) {
1692 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1693 ret = -1;
1694 goto done;
1695 }
1696 bss = &priv->in_progress_assoc_req->bss;
1697
Dan Williams5fd164e2009-05-22 20:01:21 -04001698 passocrsp = (struct ieee_assoc_response *) &resp->params;
Holger Schurig697900a2008-04-02 16:27:10 +02001699
1700 /*
1701 * Older FW versions map the IEEE 802.11 Status Code in the association
1702 * response to the following values returned in passocrsp->statuscode:
1703 *
1704 * IEEE Status Code Marvell Status Code
1705 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1706 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1707 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1708 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1709 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1710 * others -> 0x0003 ASSOC_RESULT_REFUSED
1711 *
1712 * Other response codes:
1713 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1714 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1715 * association response from the AP)
1716 */
1717
1718 status_code = le16_to_cpu(passocrsp->statuscode);
1719 switch (status_code) {
1720 case 0x00:
1721 break;
1722 case 0x01:
1723 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1724 break;
1725 case 0x02:
1726 lbs_deb_assoc("ASSOC_RESP: internal timer "
1727 "expired while waiting for the AP\n");
1728 break;
1729 case 0x03:
1730 lbs_deb_assoc("ASSOC_RESP: association "
1731 "refused by AP\n");
1732 break;
1733 case 0x04:
1734 lbs_deb_assoc("ASSOC_RESP: authentication "
1735 "refused by AP\n");
1736 break;
1737 default:
1738 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1739 " unknown\n", status_code);
1740 break;
1741 }
1742
1743 if (status_code) {
1744 lbs_mac_event_disconnected(priv);
1745 ret = -1;
1746 goto done;
1747 }
1748
1749 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1750 le16_to_cpu(resp->size) - S_DS_GEN);
1751
1752 /* Send a Media Connected event, according to the Spec */
1753 priv->connect_status = LBS_CONNECTED;
1754
1755 /* Update current SSID and BSSID */
1756 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1757 priv->curbssparams.ssid_len = bss->ssid_len;
1758 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1759
1760 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1761 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1762
1763 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1764 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1765 priv->nextSNRNF = 0;
1766 priv->numSNRNF = 0;
1767
1768 netif_carrier_on(priv->dev);
1769 if (!priv->tx_pending_len)
1770 netif_wake_queue(priv->dev);
1771
1772 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1773 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1774 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1775
1776done:
1777 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1778 return ret;
1779}
1780
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001781static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp)
Holger Schurig697900a2008-04-02 16:27:10 +02001782{
1783 int ret = 0;
1784 u16 command = le16_to_cpu(resp->command);
1785 u16 result = le16_to_cpu(resp->result);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001786 struct cmd_ds_802_11_ad_hoc_result *adhoc_resp;
Holger Schurig697900a2008-04-02 16:27:10 +02001787 union iwreq_data wrqu;
1788 struct bss_descriptor *bss;
John W. Linville9387b7c2008-09-30 20:59:05 -04001789 DECLARE_SSID_BUF(ssid);
Holger Schurig697900a2008-04-02 16:27:10 +02001790
1791 lbs_deb_enter(LBS_DEB_JOIN);
1792
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001793 adhoc_resp = (struct cmd_ds_802_11_ad_hoc_result *) resp;
Holger Schurig697900a2008-04-02 16:27:10 +02001794
1795 if (!priv->in_progress_assoc_req) {
1796 lbs_deb_join("ADHOC_RESP: no in-progress association "
1797 "request\n");
1798 ret = -1;
1799 goto done;
1800 }
1801 bss = &priv->in_progress_assoc_req->bss;
1802
1803 /*
1804 * Join result code 0 --> SUCCESS
1805 */
1806 if (result) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001807 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
Holger Schurig697900a2008-04-02 16:27:10 +02001808 if (priv->connect_status == LBS_CONNECTED)
1809 lbs_mac_event_disconnected(priv);
1810 ret = -1;
1811 goto done;
1812 }
1813
Holger Schurig697900a2008-04-02 16:27:10 +02001814 /* Send a Media Connected event, according to the Spec */
1815 priv->connect_status = LBS_CONNECTED;
1816
1817 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1818 /* Update the created network descriptor with the new BSSID */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001819 memcpy(bss->bssid, adhoc_resp->bssid, ETH_ALEN);
Holger Schurig697900a2008-04-02 16:27:10 +02001820 }
1821
1822 /* Set the BSSID from the joined/started descriptor */
1823 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1824
1825 /* Set the new SSID to current SSID */
1826 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1827 priv->curbssparams.ssid_len = bss->ssid_len;
1828
1829 netif_carrier_on(priv->dev);
1830 if (!priv->tx_pending_len)
1831 netif_wake_queue(priv->dev);
1832
1833 memset(&wrqu, 0, sizeof(wrqu));
1834 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1835 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1836 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1837
Johannes Berge1749612008-10-27 15:59:26 -07001838 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001839 print_ssid(ssid, bss->ssid, bss->ssid_len),
Johannes Berge1749612008-10-27 15:59:26 -07001840 priv->curbssparams.bssid,
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001841 priv->curbssparams.channel);
Holger Schurig697900a2008-04-02 16:27:10 +02001842
1843done:
1844 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1845 return ret;
1846}
1847