assoc.c 62 KB

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