assoc.c 50 KB

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