assoc.c 51 KB

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