assoc.c 62 KB

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