assoc.c 50 KB

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