assoc.c 61 KB

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