assoc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
  187. memcpy(&cmd.bss.ibss, &bss->ss.ibss,
  188. sizeof(struct ieee_ie_ibss_param_set));
  189. cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  190. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  191. bss->capability, CAPINFO_MASK);
  192. /* information on BSSID descriptor passed to FW */
  193. lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
  194. cmd.bss.bssid, cmd.bss.ssid);
  195. /* Only v8 and below support setting these */
  196. if (priv->fwrelease < 0x09000000) {
  197. /* failtimeout */
  198. cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  199. /* probedelay */
  200. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  201. }
  202. /* Copy Data rates from the rates recorded in scan response */
  203. memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
  204. ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
  205. memcpy(cmd.bss.rates, bss->rates, ratesize);
  206. if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
  207. lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
  208. ret = -1;
  209. goto out;
  210. }
  211. /* Copy the ad-hoc creation rates into Current BSS state structure */
  212. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  213. memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
  214. /* Set MSB on basic rates as the firmware requires, but _after_
  215. * copying to current bss rates.
  216. */
  217. lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
  218. cmd.bss.ibss.atimwindow = bss->atimwindow;
  219. if (assoc_req->secinfo.wep_enabled) {
  220. u16 tmp = le16_to_cpu(cmd.bss.capability);
  221. tmp |= WLAN_CAPABILITY_PRIVACY;
  222. cmd.bss.capability = cpu_to_le16(tmp);
  223. }
  224. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  225. __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
  226. /* wake up first */
  227. ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  228. CMD_ACT_SET, 0, 0,
  229. &local_ps_mode);
  230. if (ret) {
  231. ret = -1;
  232. goto out;
  233. }
  234. }
  235. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  236. ret = -1;
  237. goto out;
  238. }
  239. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
  240. if (ret == 0)
  241. ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
  242. out:
  243. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  244. return ret;
  245. }
  246. /**
  247. * @brief Start an Adhoc Network
  248. *
  249. * @param priv A pointer to struct lbs_private structure
  250. * @param assoc_req The association request describing the BSS to start
  251. *
  252. * @return 0 on success, error on failure
  253. */
  254. static int lbs_adhoc_start(struct lbs_private *priv,
  255. struct assoc_request *assoc_req)
  256. {
  257. struct cmd_ds_802_11_ad_hoc_start cmd;
  258. u8 preamble = RADIO_PREAMBLE_LONG;
  259. size_t ratesize = 0;
  260. u16 tmpcap = 0;
  261. int ret = 0;
  262. DECLARE_SSID_BUF(ssid);
  263. lbs_deb_enter(LBS_DEB_ASSOC);
  264. if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
  265. lbs_deb_join("ADHOC_START: Will use short preamble\n");
  266. preamble = RADIO_PREAMBLE_SHORT;
  267. }
  268. ret = lbs_set_radio(priv, preamble, 1);
  269. if (ret)
  270. goto out;
  271. /* Build the start command */
  272. memset(&cmd, 0, sizeof(cmd));
  273. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  274. memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
  275. lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
  276. print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
  277. assoc_req->ssid_len);
  278. cmd.bsstype = CMD_BSS_TYPE_IBSS;
  279. if (priv->beacon_period == 0)
  280. priv->beacon_period = MRVDRV_BEACON_INTERVAL;
  281. cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
  282. WARN_ON(!assoc_req->channel);
  283. /* set Physical parameter set */
  284. cmd.ds.header.id = WLAN_EID_DS_PARAMS;
  285. cmd.ds.header.len = 1;
  286. cmd.ds.channel = assoc_req->channel;
  287. /* set IBSS parameter set */
  288. cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  289. cmd.ibss.header.len = 2;
  290. cmd.ibss.atimwindow = cpu_to_le16(0);
  291. /* set capability info */
  292. tmpcap = WLAN_CAPABILITY_IBSS;
  293. if (assoc_req->secinfo.wep_enabled) {
  294. lbs_deb_join("ADHOC_START: WEP enabled, setting privacy on\n");
  295. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  296. } else
  297. lbs_deb_join("ADHOC_START: WEP disabled, setting privacy off\n");
  298. cmd.capability = cpu_to_le16(tmpcap);
  299. /* Only v8 and below support setting probe delay */
  300. if (priv->fwrelease < 0x09000000)
  301. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  302. ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
  303. memcpy(cmd.rates, lbs_bg_rates, ratesize);
  304. /* Copy the ad-hoc creating rates into Current BSS state structure */
  305. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  306. memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
  307. /* Set MSB on basic rates as the firmware requires, but _after_
  308. * copying to current bss rates.
  309. */
  310. lbs_set_basic_rate_flags(cmd.rates, ratesize);
  311. lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
  312. cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
  313. if (lbs_create_dnld_countryinfo_11d(priv)) {
  314. lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
  315. ret = -1;
  316. goto out;
  317. }
  318. lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
  319. assoc_req->channel, assoc_req->band);
  320. priv->adhoccreate = 1;
  321. priv->mode = IW_MODE_ADHOC;
  322. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
  323. if (ret == 0)
  324. ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
  325. out:
  326. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  327. return ret;
  328. }
  329. /**
  330. * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
  331. *
  332. * @param priv A pointer to struct lbs_private structure
  333. * @return 0 on success, or an error
  334. */
  335. int lbs_adhoc_stop(struct lbs_private *priv)
  336. {
  337. struct cmd_ds_802_11_ad_hoc_stop cmd;
  338. int ret;
  339. lbs_deb_enter(LBS_DEB_JOIN);
  340. memset(&cmd, 0, sizeof (cmd));
  341. cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
  342. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
  343. /* Clean up everything even if there was an error */
  344. lbs_mac_event_disconnected(priv);
  345. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  346. return ret;
  347. }
  348. static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
  349. struct bss_descriptor *match_bss)
  350. {
  351. if (!secinfo->wep_enabled && !secinfo->WPAenabled
  352. && !secinfo->WPA2enabled
  353. && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
  354. && match_bss->rsn_ie[0] != WLAN_EID_RSN
  355. && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  356. return 1;
  357. else
  358. return 0;
  359. }
  360. static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
  361. struct bss_descriptor *match_bss)
  362. {
  363. if (secinfo->wep_enabled && !secinfo->WPAenabled
  364. && !secinfo->WPA2enabled
  365. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  366. return 1;
  367. else
  368. return 0;
  369. }
  370. static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
  371. struct bss_descriptor *match_bss)
  372. {
  373. if (!secinfo->wep_enabled && secinfo->WPAenabled
  374. && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
  375. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  376. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
  377. )
  378. return 1;
  379. else
  380. return 0;
  381. }
  382. static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
  383. struct bss_descriptor *match_bss)
  384. {
  385. if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
  386. (match_bss->rsn_ie[0] == WLAN_EID_RSN)
  387. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  388. (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
  389. )
  390. return 1;
  391. else
  392. return 0;
  393. }
  394. static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
  395. struct bss_descriptor *match_bss)
  396. {
  397. if (!secinfo->wep_enabled && !secinfo->WPAenabled
  398. && !secinfo->WPA2enabled
  399. && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
  400. && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
  401. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  402. return 1;
  403. else
  404. return 0;
  405. }
  406. /**
  407. * @brief Check if a scanned network compatible with the driver settings
  408. *
  409. * WEP WPA WPA2 ad-hoc encrypt Network
  410. * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
  411. * 0 0 0 0 NONE 0 0 0 yes No security
  412. * 1 0 0 0 NONE 1 0 0 yes Static WEP
  413. * 0 1 0 0 x 1x 1 x yes WPA
  414. * 0 0 1 0 x 1x x 1 yes WPA2
  415. * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
  416. * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
  417. *
  418. *
  419. * @param priv A pointer to struct lbs_private
  420. * @param index Index in scantable to check against current driver settings
  421. * @param mode Network mode: Infrastructure or IBSS
  422. *
  423. * @return Index in scantable, or error code if negative
  424. */
  425. static int is_network_compatible(struct lbs_private *priv,
  426. struct bss_descriptor *bss, uint8_t mode)
  427. {
  428. int matched = 0;
  429. lbs_deb_enter(LBS_DEB_SCAN);
  430. if (bss->mode != mode)
  431. goto done;
  432. matched = match_bss_no_security(&priv->secinfo, bss);
  433. if (matched)
  434. goto done;
  435. matched = match_bss_static_wep(&priv->secinfo, bss);
  436. if (matched)
  437. goto done;
  438. matched = match_bss_wpa(&priv->secinfo, bss);
  439. if (matched) {
  440. lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
  441. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
  442. "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  443. priv->secinfo.wep_enabled ? "e" : "d",
  444. priv->secinfo.WPAenabled ? "e" : "d",
  445. priv->secinfo.WPA2enabled ? "e" : "d",
  446. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  447. goto done;
  448. }
  449. matched = match_bss_wpa2(&priv->secinfo, bss);
  450. if (matched) {
  451. lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
  452. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
  453. "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  454. priv->secinfo.wep_enabled ? "e" : "d",
  455. priv->secinfo.WPAenabled ? "e" : "d",
  456. priv->secinfo.WPA2enabled ? "e" : "d",
  457. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  458. goto done;
  459. }
  460. matched = match_bss_dynamic_wep(&priv->secinfo, bss);
  461. if (matched) {
  462. lbs_deb_scan("is_network_compatible() dynamic WEP: "
  463. "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
  464. bss->wpa_ie[0], bss->rsn_ie[0],
  465. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  466. goto done;
  467. }
  468. /* bss security settings don't match those configured on card */
  469. lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
  470. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
  471. bss->wpa_ie[0], bss->rsn_ie[0],
  472. priv->secinfo.wep_enabled ? "e" : "d",
  473. priv->secinfo.WPAenabled ? "e" : "d",
  474. priv->secinfo.WPA2enabled ? "e" : "d",
  475. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  476. done:
  477. lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
  478. return matched;
  479. }
  480. /**
  481. * @brief This function finds a specific compatible BSSID in the scan list
  482. *
  483. * Used in association code
  484. *
  485. * @param priv A pointer to struct lbs_private
  486. * @param bssid BSSID to find in the scan list
  487. * @param mode Network mode: Infrastructure or IBSS
  488. *
  489. * @return index in BSSID list, or error return code (< 0)
  490. */
  491. static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
  492. uint8_t *bssid, uint8_t mode)
  493. {
  494. struct bss_descriptor *iter_bss;
  495. struct bss_descriptor *found_bss = NULL;
  496. lbs_deb_enter(LBS_DEB_SCAN);
  497. if (!bssid)
  498. goto out;
  499. lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
  500. /* Look through the scan table for a compatible match. The loop will
  501. * continue past a matched bssid that is not compatible in case there
  502. * is an AP with multiple SSIDs assigned to the same BSSID
  503. */
  504. mutex_lock(&priv->lock);
  505. list_for_each_entry(iter_bss, &priv->network_list, list) {
  506. if (compare_ether_addr(iter_bss->bssid, bssid))
  507. continue; /* bssid doesn't match */
  508. switch (mode) {
  509. case IW_MODE_INFRA:
  510. case IW_MODE_ADHOC:
  511. if (!is_network_compatible(priv, iter_bss, mode))
  512. break;
  513. found_bss = iter_bss;
  514. break;
  515. default:
  516. found_bss = iter_bss;
  517. break;
  518. }
  519. }
  520. mutex_unlock(&priv->lock);
  521. out:
  522. lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
  523. return found_bss;
  524. }
  525. /**
  526. * @brief This function finds ssid in ssid list.
  527. *
  528. * Used in association code
  529. *
  530. * @param priv A pointer to struct lbs_private
  531. * @param ssid SSID to find in the list
  532. * @param bssid BSSID to qualify the SSID selection (if provided)
  533. * @param mode Network mode: Infrastructure or IBSS
  534. *
  535. * @return index in BSSID list
  536. */
  537. static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
  538. uint8_t *ssid, uint8_t ssid_len,
  539. uint8_t *bssid, uint8_t mode,
  540. int channel)
  541. {
  542. u32 bestrssi = 0;
  543. struct bss_descriptor *iter_bss = NULL;
  544. struct bss_descriptor *found_bss = NULL;
  545. struct bss_descriptor *tmp_oldest = NULL;
  546. lbs_deb_enter(LBS_DEB_SCAN);
  547. mutex_lock(&priv->lock);
  548. list_for_each_entry(iter_bss, &priv->network_list, list) {
  549. if (!tmp_oldest ||
  550. (iter_bss->last_scanned < tmp_oldest->last_scanned))
  551. tmp_oldest = iter_bss;
  552. if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
  553. ssid, ssid_len) != 0)
  554. continue; /* ssid doesn't match */
  555. if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
  556. continue; /* bssid doesn't match */
  557. if ((channel > 0) && (iter_bss->channel != channel))
  558. continue; /* channel doesn't match */
  559. switch (mode) {
  560. case IW_MODE_INFRA:
  561. case IW_MODE_ADHOC:
  562. if (!is_network_compatible(priv, iter_bss, mode))
  563. break;
  564. if (bssid) {
  565. /* Found requested BSSID */
  566. found_bss = iter_bss;
  567. goto out;
  568. }
  569. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  570. bestrssi = SCAN_RSSI(iter_bss->rssi);
  571. found_bss = iter_bss;
  572. }
  573. break;
  574. case IW_MODE_AUTO:
  575. default:
  576. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  577. bestrssi = SCAN_RSSI(iter_bss->rssi);
  578. found_bss = iter_bss;
  579. }
  580. break;
  581. }
  582. }
  583. out:
  584. mutex_unlock(&priv->lock);
  585. lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
  586. return found_bss;
  587. }
  588. static int assoc_helper_essid(struct lbs_private *priv,
  589. struct assoc_request * assoc_req)
  590. {
  591. int ret = 0;
  592. struct bss_descriptor * bss;
  593. int channel = -1;
  594. DECLARE_SSID_BUF(ssid);
  595. lbs_deb_enter(LBS_DEB_ASSOC);
  596. /* FIXME: take channel into account when picking SSIDs if a channel
  597. * is set.
  598. */
  599. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  600. channel = assoc_req->channel;
  601. lbs_deb_assoc("SSID '%s' requested\n",
  602. print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
  603. if (assoc_req->mode == IW_MODE_INFRA) {
  604. lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
  605. assoc_req->ssid_len);
  606. bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
  607. assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
  608. if (bss != NULL) {
  609. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  610. ret = lbs_associate(priv, assoc_req);
  611. } else {
  612. lbs_deb_assoc("SSID not found; cannot associate\n");
  613. }
  614. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  615. /* Scan for the network, do not save previous results. Stale
  616. * scan data will cause us to join a non-existant adhoc network
  617. */
  618. lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
  619. assoc_req->ssid_len);
  620. /* Search for the requested SSID in the scan table */
  621. bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
  622. assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
  623. if (bss != NULL) {
  624. lbs_deb_assoc("SSID found, will join\n");
  625. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  626. lbs_adhoc_join(priv, assoc_req);
  627. } else {
  628. /* else send START command */
  629. lbs_deb_assoc("SSID not found, creating adhoc network\n");
  630. memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
  631. IW_ESSID_MAX_SIZE);
  632. assoc_req->bss.ssid_len = assoc_req->ssid_len;
  633. lbs_adhoc_start(priv, assoc_req);
  634. }
  635. }
  636. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  637. return ret;
  638. }
  639. static int assoc_helper_bssid(struct lbs_private *priv,
  640. struct assoc_request * assoc_req)
  641. {
  642. int ret = 0;
  643. struct bss_descriptor * bss;
  644. lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
  645. /* Search for index position in list for requested MAC */
  646. bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
  647. assoc_req->mode);
  648. if (bss == NULL) {
  649. lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
  650. "cannot associate.\n", assoc_req->bssid);
  651. goto out;
  652. }
  653. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  654. if (assoc_req->mode == IW_MODE_INFRA) {
  655. ret = lbs_associate(priv, assoc_req);
  656. lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
  657. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  658. lbs_adhoc_join(priv, assoc_req);
  659. }
  660. out:
  661. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  662. return ret;
  663. }
  664. static int assoc_helper_associate(struct lbs_private *priv,
  665. struct assoc_request * assoc_req)
  666. {
  667. int ret = 0, done = 0;
  668. lbs_deb_enter(LBS_DEB_ASSOC);
  669. /* If we're given and 'any' BSSID, try associating based on SSID */
  670. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  671. if (compare_ether_addr(bssid_any, assoc_req->bssid)
  672. && compare_ether_addr(bssid_off, assoc_req->bssid)) {
  673. ret = assoc_helper_bssid(priv, assoc_req);
  674. done = 1;
  675. }
  676. }
  677. if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  678. ret = assoc_helper_essid(priv, assoc_req);
  679. }
  680. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  681. return ret;
  682. }
  683. static int assoc_helper_mode(struct lbs_private *priv,
  684. struct assoc_request * assoc_req)
  685. {
  686. int ret = 0;
  687. lbs_deb_enter(LBS_DEB_ASSOC);
  688. if (assoc_req->mode == priv->mode)
  689. goto done;
  690. if (assoc_req->mode == IW_MODE_INFRA) {
  691. if (priv->psstate != PS_STATE_FULL_POWER)
  692. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  693. priv->psmode = LBS802_11POWERMODECAM;
  694. }
  695. priv->mode = assoc_req->mode;
  696. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
  697. done:
  698. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  699. return ret;
  700. }
  701. static int assoc_helper_channel(struct lbs_private *priv,
  702. struct assoc_request * assoc_req)
  703. {
  704. int ret = 0;
  705. lbs_deb_enter(LBS_DEB_ASSOC);
  706. ret = lbs_update_channel(priv);
  707. if (ret) {
  708. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  709. goto done;
  710. }
  711. if (assoc_req->channel == priv->curbssparams.channel)
  712. goto done;
  713. if (priv->mesh_dev) {
  714. /* Change mesh channel first; 21.p21 firmware won't let
  715. you change channel otherwise (even though it'll return
  716. an error to this */
  717. lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
  718. assoc_req->channel);
  719. }
  720. lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
  721. priv->curbssparams.channel, assoc_req->channel);
  722. ret = lbs_set_channel(priv, assoc_req->channel);
  723. if (ret < 0)
  724. lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
  725. /* FIXME: shouldn't need to grab the channel _again_ after setting
  726. * it since the firmware is supposed to return the new channel, but
  727. * whatever... */
  728. ret = lbs_update_channel(priv);
  729. if (ret) {
  730. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  731. goto done;
  732. }
  733. if (assoc_req->channel != priv->curbssparams.channel) {
  734. lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
  735. assoc_req->channel);
  736. goto restore_mesh;
  737. }
  738. if ( assoc_req->secinfo.wep_enabled
  739. && (assoc_req->wep_keys[0].len
  740. || assoc_req->wep_keys[1].len
  741. || assoc_req->wep_keys[2].len
  742. || assoc_req->wep_keys[3].len)) {
  743. /* Make sure WEP keys are re-sent to firmware */
  744. set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
  745. }
  746. /* Must restart/rejoin adhoc networks after channel change */
  747. set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
  748. restore_mesh:
  749. if (priv->mesh_dev)
  750. lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  751. priv->curbssparams.channel);
  752. done:
  753. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  754. return ret;
  755. }
  756. static int assoc_helper_wep_keys(struct lbs_private *priv,
  757. struct assoc_request *assoc_req)
  758. {
  759. int i;
  760. int ret = 0;
  761. lbs_deb_enter(LBS_DEB_ASSOC);
  762. /* Set or remove WEP keys */
  763. if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
  764. assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
  765. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
  766. else
  767. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
  768. if (ret)
  769. goto out;
  770. /* enable/disable the MAC's WEP packet filter */
  771. if (assoc_req->secinfo.wep_enabled)
  772. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  773. else
  774. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  775. lbs_set_mac_control(priv);
  776. mutex_lock(&priv->lock);
  777. /* Copy WEP keys into priv wep key fields */
  778. for (i = 0; i < 4; i++) {
  779. memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
  780. sizeof(struct enc_key));
  781. }
  782. priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
  783. mutex_unlock(&priv->lock);
  784. out:
  785. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  786. return ret;
  787. }
  788. static int assoc_helper_secinfo(struct lbs_private *priv,
  789. struct assoc_request * assoc_req)
  790. {
  791. int ret = 0;
  792. uint16_t do_wpa;
  793. uint16_t rsn = 0;
  794. lbs_deb_enter(LBS_DEB_ASSOC);
  795. memcpy(&priv->secinfo, &assoc_req->secinfo,
  796. sizeof(struct lbs_802_11_security));
  797. lbs_set_mac_control(priv);
  798. /* If RSN is already enabled, don't try to enable it again, since
  799. * ENABLE_RSN resets internal state machines and will clobber the
  800. * 4-way WPA handshake.
  801. */
  802. /* Get RSN enabled/disabled */
  803. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
  804. if (ret) {
  805. lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
  806. goto out;
  807. }
  808. /* Don't re-enable RSN if it's already enabled */
  809. do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
  810. if (do_wpa == rsn)
  811. goto out;
  812. /* Set RSN enabled/disabled */
  813. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
  814. out:
  815. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  816. return ret;
  817. }
  818. static int assoc_helper_wpa_keys(struct lbs_private *priv,
  819. struct assoc_request * assoc_req)
  820. {
  821. int ret = 0;
  822. unsigned int flags = assoc_req->flags;
  823. lbs_deb_enter(LBS_DEB_ASSOC);
  824. /* Work around older firmware bug where WPA unicast and multicast
  825. * keys must be set independently. Seen in SDIO parts with firmware
  826. * version 5.0.11p0.
  827. */
  828. if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  829. clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
  830. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  831. assoc_req->flags = flags;
  832. }
  833. if (ret)
  834. goto out;
  835. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  836. clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
  837. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  838. assoc_req->flags = flags;
  839. }
  840. out:
  841. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  842. return ret;
  843. }
  844. static int assoc_helper_wpa_ie(struct lbs_private *priv,
  845. struct assoc_request * assoc_req)
  846. {
  847. int ret = 0;
  848. lbs_deb_enter(LBS_DEB_ASSOC);
  849. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  850. memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
  851. priv->wpa_ie_len = assoc_req->wpa_ie_len;
  852. } else {
  853. memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
  854. priv->wpa_ie_len = 0;
  855. }
  856. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  857. return ret;
  858. }
  859. static int should_deauth_infrastructure(struct lbs_private *priv,
  860. struct assoc_request * assoc_req)
  861. {
  862. int ret = 0;
  863. if (priv->connect_status != LBS_CONNECTED)
  864. return 0;
  865. lbs_deb_enter(LBS_DEB_ASSOC);
  866. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  867. lbs_deb_assoc("Deauthenticating due to new SSID\n");
  868. ret = 1;
  869. goto out;
  870. }
  871. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  872. if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
  873. lbs_deb_assoc("Deauthenticating due to new security\n");
  874. ret = 1;
  875. goto out;
  876. }
  877. }
  878. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  879. lbs_deb_assoc("Deauthenticating due to new BSSID\n");
  880. ret = 1;
  881. goto out;
  882. }
  883. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  884. lbs_deb_assoc("Deauthenticating due to channel switch\n");
  885. ret = 1;
  886. goto out;
  887. }
  888. /* FIXME: deal with 'auto' mode somehow */
  889. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  890. if (assoc_req->mode != IW_MODE_INFRA) {
  891. lbs_deb_assoc("Deauthenticating due to leaving "
  892. "infra mode\n");
  893. ret = 1;
  894. goto out;
  895. }
  896. }
  897. out:
  898. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  899. return ret;
  900. }
  901. static int should_stop_adhoc(struct lbs_private *priv,
  902. struct assoc_request * assoc_req)
  903. {
  904. lbs_deb_enter(LBS_DEB_ASSOC);
  905. if (priv->connect_status != LBS_CONNECTED)
  906. return 0;
  907. if (lbs_ssid_cmp(priv->curbssparams.ssid,
  908. priv->curbssparams.ssid_len,
  909. assoc_req->ssid, assoc_req->ssid_len) != 0)
  910. return 1;
  911. /* FIXME: deal with 'auto' mode somehow */
  912. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  913. if (assoc_req->mode != IW_MODE_ADHOC)
  914. return 1;
  915. }
  916. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  917. if (assoc_req->channel != priv->curbssparams.channel)
  918. return 1;
  919. }
  920. lbs_deb_leave(LBS_DEB_ASSOC);
  921. return 0;
  922. }
  923. /**
  924. * @brief This function finds the best SSID in the Scan List
  925. *
  926. * Search the scan table for the best SSID that also matches the current
  927. * adapter network preference (infrastructure or adhoc)
  928. *
  929. * @param priv A pointer to struct lbs_private
  930. *
  931. * @return index in BSSID list
  932. */
  933. static struct bss_descriptor *lbs_find_best_ssid_in_list(
  934. struct lbs_private *priv, uint8_t mode)
  935. {
  936. uint8_t bestrssi = 0;
  937. struct bss_descriptor *iter_bss;
  938. struct bss_descriptor *best_bss = NULL;
  939. lbs_deb_enter(LBS_DEB_SCAN);
  940. mutex_lock(&priv->lock);
  941. list_for_each_entry(iter_bss, &priv->network_list, list) {
  942. switch (mode) {
  943. case IW_MODE_INFRA:
  944. case IW_MODE_ADHOC:
  945. if (!is_network_compatible(priv, iter_bss, mode))
  946. break;
  947. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  948. break;
  949. bestrssi = SCAN_RSSI(iter_bss->rssi);
  950. best_bss = iter_bss;
  951. break;
  952. case IW_MODE_AUTO:
  953. default:
  954. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  955. break;
  956. bestrssi = SCAN_RSSI(iter_bss->rssi);
  957. best_bss = iter_bss;
  958. break;
  959. }
  960. }
  961. mutex_unlock(&priv->lock);
  962. lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
  963. return best_bss;
  964. }
  965. /**
  966. * @brief Find the best AP
  967. *
  968. * Used from association worker.
  969. *
  970. * @param priv A pointer to struct lbs_private structure
  971. * @param pSSID A pointer to AP's ssid
  972. *
  973. * @return 0--success, otherwise--fail
  974. */
  975. static int lbs_find_best_network_ssid(struct lbs_private *priv,
  976. uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
  977. uint8_t *out_mode)
  978. {
  979. int ret = -1;
  980. struct bss_descriptor *found;
  981. lbs_deb_enter(LBS_DEB_SCAN);
  982. priv->scan_ssid_len = 0;
  983. lbs_scan_networks(priv, 1);
  984. if (priv->surpriseremoved)
  985. goto out;
  986. found = lbs_find_best_ssid_in_list(priv, preferred_mode);
  987. if (found && (found->ssid_len > 0)) {
  988. memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
  989. *out_ssid_len = found->ssid_len;
  990. *out_mode = found->mode;
  991. ret = 0;
  992. }
  993. out:
  994. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  995. return ret;
  996. }
  997. void lbs_association_worker(struct work_struct *work)
  998. {
  999. struct lbs_private *priv = container_of(work, struct lbs_private,
  1000. assoc_work.work);
  1001. struct assoc_request * assoc_req = NULL;
  1002. int ret = 0;
  1003. int find_any_ssid = 0;
  1004. DECLARE_SSID_BUF(ssid);
  1005. lbs_deb_enter(LBS_DEB_ASSOC);
  1006. mutex_lock(&priv->lock);
  1007. assoc_req = priv->pending_assoc_req;
  1008. priv->pending_assoc_req = NULL;
  1009. priv->in_progress_assoc_req = assoc_req;
  1010. mutex_unlock(&priv->lock);
  1011. if (!assoc_req)
  1012. goto done;
  1013. lbs_deb_assoc(
  1014. "Association Request:\n"
  1015. " flags: 0x%08lx\n"
  1016. " SSID: '%s'\n"
  1017. " chann: %d\n"
  1018. " band: %d\n"
  1019. " mode: %d\n"
  1020. " BSSID: %pM\n"
  1021. " secinfo: %s%s%s\n"
  1022. " auth_mode: %d\n",
  1023. assoc_req->flags,
  1024. print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
  1025. assoc_req->channel, assoc_req->band, assoc_req->mode,
  1026. assoc_req->bssid,
  1027. assoc_req->secinfo.WPAenabled ? " WPA" : "",
  1028. assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
  1029. assoc_req->secinfo.wep_enabled ? " WEP" : "",
  1030. assoc_req->secinfo.auth_mode);
  1031. /* If 'any' SSID was specified, find an SSID to associate with */
  1032. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
  1033. && !assoc_req->ssid_len)
  1034. find_any_ssid = 1;
  1035. /* But don't use 'any' SSID if there's a valid locked BSSID to use */
  1036. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  1037. if (compare_ether_addr(assoc_req->bssid, bssid_any)
  1038. && compare_ether_addr(assoc_req->bssid, bssid_off))
  1039. find_any_ssid = 0;
  1040. }
  1041. if (find_any_ssid) {
  1042. u8 new_mode = assoc_req->mode;
  1043. ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
  1044. &assoc_req->ssid_len, assoc_req->mode, &new_mode);
  1045. if (ret) {
  1046. lbs_deb_assoc("Could not find best network\n");
  1047. ret = -ENETUNREACH;
  1048. goto out;
  1049. }
  1050. /* Ensure we switch to the mode of the AP */
  1051. if (assoc_req->mode == IW_MODE_AUTO) {
  1052. set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
  1053. assoc_req->mode = new_mode;
  1054. }
  1055. }
  1056. /*
  1057. * Check if the attributes being changing require deauthentication
  1058. * from the currently associated infrastructure access point.
  1059. */
  1060. if (priv->mode == IW_MODE_INFRA) {
  1061. if (should_deauth_infrastructure(priv, assoc_req)) {
  1062. ret = lbs_cmd_80211_deauthenticate(priv,
  1063. priv->curbssparams.bssid,
  1064. WLAN_REASON_DEAUTH_LEAVING);
  1065. if (ret) {
  1066. lbs_deb_assoc("Deauthentication due to new "
  1067. "configuration request failed: %d\n",
  1068. ret);
  1069. }
  1070. }
  1071. } else if (priv->mode == IW_MODE_ADHOC) {
  1072. if (should_stop_adhoc(priv, assoc_req)) {
  1073. ret = lbs_adhoc_stop(priv);
  1074. if (ret) {
  1075. lbs_deb_assoc("Teardown of AdHoc network due to "
  1076. "new configuration request failed: %d\n",
  1077. ret);
  1078. }
  1079. }
  1080. }
  1081. /* Send the various configuration bits to the firmware */
  1082. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  1083. ret = assoc_helper_mode(priv, assoc_req);
  1084. if (ret)
  1085. goto out;
  1086. }
  1087. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  1088. ret = assoc_helper_channel(priv, assoc_req);
  1089. if (ret)
  1090. goto out;
  1091. }
  1092. if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
  1093. || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
  1094. ret = assoc_helper_wep_keys(priv, assoc_req);
  1095. if (ret)
  1096. goto out;
  1097. }
  1098. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  1099. ret = assoc_helper_secinfo(priv, assoc_req);
  1100. if (ret)
  1101. goto out;
  1102. }
  1103. if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  1104. ret = assoc_helper_wpa_ie(priv, assoc_req);
  1105. if (ret)
  1106. goto out;
  1107. }
  1108. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
  1109. || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  1110. ret = assoc_helper_wpa_keys(priv, assoc_req);
  1111. if (ret)
  1112. goto out;
  1113. }
  1114. /* SSID/BSSID should be the _last_ config option set, because they
  1115. * trigger the association attempt.
  1116. */
  1117. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
  1118. || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  1119. int success = 1;
  1120. ret = assoc_helper_associate(priv, assoc_req);
  1121. if (ret) {
  1122. lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
  1123. ret);
  1124. success = 0;
  1125. }
  1126. if (priv->connect_status != LBS_CONNECTED) {
  1127. lbs_deb_assoc("ASSOC: association unsuccessful, "
  1128. "not connected\n");
  1129. success = 0;
  1130. }
  1131. if (success) {
  1132. lbs_deb_assoc("associated to %pM\n",
  1133. priv->curbssparams.bssid);
  1134. lbs_prepare_and_send_command(priv,
  1135. CMD_802_11_RSSI,
  1136. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  1137. } else {
  1138. ret = -1;
  1139. }
  1140. }
  1141. out:
  1142. if (ret) {
  1143. lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
  1144. ret);
  1145. }
  1146. mutex_lock(&priv->lock);
  1147. priv->in_progress_assoc_req = NULL;
  1148. mutex_unlock(&priv->lock);
  1149. kfree(assoc_req);
  1150. done:
  1151. lbs_deb_leave(LBS_DEB_ASSOC);
  1152. }
  1153. /*
  1154. * Caller MUST hold any necessary locks
  1155. */
  1156. struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
  1157. {
  1158. struct assoc_request * assoc_req;
  1159. lbs_deb_enter(LBS_DEB_ASSOC);
  1160. if (!priv->pending_assoc_req) {
  1161. priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
  1162. GFP_KERNEL);
  1163. if (!priv->pending_assoc_req) {
  1164. lbs_pr_info("Not enough memory to allocate association"
  1165. " request!\n");
  1166. return NULL;
  1167. }
  1168. }
  1169. /* Copy current configuration attributes to the association request,
  1170. * but don't overwrite any that are already set.
  1171. */
  1172. assoc_req = priv->pending_assoc_req;
  1173. if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  1174. memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
  1175. IW_ESSID_MAX_SIZE);
  1176. assoc_req->ssid_len = priv->curbssparams.ssid_len;
  1177. }
  1178. if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  1179. assoc_req->channel = priv->curbssparams.channel;
  1180. if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
  1181. assoc_req->band = priv->curbssparams.band;
  1182. if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
  1183. assoc_req->mode = priv->mode;
  1184. if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  1185. memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
  1186. ETH_ALEN);
  1187. }
  1188. if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
  1189. int i;
  1190. for (i = 0; i < 4; i++) {
  1191. memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
  1192. sizeof(struct enc_key));
  1193. }
  1194. }
  1195. if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
  1196. assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
  1197. if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  1198. memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
  1199. sizeof(struct enc_key));
  1200. }
  1201. if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  1202. memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
  1203. sizeof(struct enc_key));
  1204. }
  1205. if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  1206. memcpy(&assoc_req->secinfo, &priv->secinfo,
  1207. sizeof(struct lbs_802_11_security));
  1208. }
  1209. if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  1210. memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
  1211. MAX_WPA_IE_LEN);
  1212. assoc_req->wpa_ie_len = priv->wpa_ie_len;
  1213. }
  1214. lbs_deb_leave(LBS_DEB_ASSOC);
  1215. return assoc_req;
  1216. }
  1217. /**
  1218. * @brief This function prepares command of authenticate.
  1219. *
  1220. * @param priv A pointer to struct lbs_private structure
  1221. * @param cmd A pointer to cmd_ds_command structure
  1222. * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
  1223. *
  1224. * @return 0 or -1
  1225. */
  1226. int lbs_cmd_80211_authenticate(struct lbs_private *priv,
  1227. struct cmd_ds_command *cmd,
  1228. void *pdata_buf)
  1229. {
  1230. struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
  1231. int ret = -1;
  1232. u8 *bssid = pdata_buf;
  1233. lbs_deb_enter(LBS_DEB_JOIN);
  1234. cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
  1235. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
  1236. + S_DS_GEN);
  1237. /* translate auth mode to 802.11 defined wire value */
  1238. switch (priv->secinfo.auth_mode) {
  1239. case IW_AUTH_ALG_OPEN_SYSTEM:
  1240. pauthenticate->authtype = 0x00;
  1241. break;
  1242. case IW_AUTH_ALG_SHARED_KEY:
  1243. pauthenticate->authtype = 0x01;
  1244. break;
  1245. case IW_AUTH_ALG_LEAP:
  1246. pauthenticate->authtype = 0x80;
  1247. break;
  1248. default:
  1249. lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
  1250. priv->secinfo.auth_mode);
  1251. goto out;
  1252. }
  1253. memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
  1254. lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
  1255. bssid, pauthenticate->authtype);
  1256. ret = 0;
  1257. out:
  1258. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1259. return ret;
  1260. }
  1261. /**
  1262. * @brief Deauthenticate from a specific BSS
  1263. *
  1264. * @param priv A pointer to struct lbs_private structure
  1265. * @param bssid The specific BSS to deauthenticate from
  1266. * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
  1267. *
  1268. * @return 0 on success, error on failure
  1269. */
  1270. int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
  1271. u16 reason)
  1272. {
  1273. struct cmd_ds_802_11_deauthenticate cmd;
  1274. int ret;
  1275. lbs_deb_enter(LBS_DEB_JOIN);
  1276. memset(&cmd, 0, sizeof(cmd));
  1277. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1278. memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
  1279. cmd.reasoncode = cpu_to_le16(reason);
  1280. ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
  1281. /* Clean up everything even if there was an error; can't assume that
  1282. * we're still authenticated to the AP after trying to deauth.
  1283. */
  1284. lbs_mac_event_disconnected(priv);
  1285. lbs_deb_leave(LBS_DEB_JOIN);
  1286. return ret;
  1287. }
  1288. int lbs_cmd_80211_associate(struct lbs_private *priv,
  1289. struct cmd_ds_command *cmd, void *pdata_buf)
  1290. {
  1291. struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
  1292. int ret = 0;
  1293. struct assoc_request *assoc_req = pdata_buf;
  1294. struct bss_descriptor *bss = &assoc_req->bss;
  1295. u8 *pos;
  1296. u16 tmpcap, tmplen;
  1297. struct mrvl_ie_ssid_param_set *ssid;
  1298. struct mrvl_ie_ds_param_set *ds;
  1299. struct mrvl_ie_cf_param_set *cf;
  1300. struct mrvl_ie_rates_param_set *rates;
  1301. struct mrvl_ie_rsn_param_set *rsn;
  1302. lbs_deb_enter(LBS_DEB_ASSOC);
  1303. pos = (u8 *) passo;
  1304. if (!priv) {
  1305. ret = -1;
  1306. goto done;
  1307. }
  1308. cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  1309. memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
  1310. pos += sizeof(passo->peerstaaddr);
  1311. /* set the listen interval */
  1312. passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  1313. pos += sizeof(passo->capability);
  1314. pos += sizeof(passo->listeninterval);
  1315. pos += sizeof(passo->bcnperiod);
  1316. pos += sizeof(passo->dtimperiod);
  1317. ssid = (struct mrvl_ie_ssid_param_set *) pos;
  1318. ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
  1319. tmplen = bss->ssid_len;
  1320. ssid->header.len = cpu_to_le16(tmplen);
  1321. memcpy(ssid->ssid, bss->ssid, tmplen);
  1322. pos += sizeof(ssid->header) + tmplen;
  1323. ds = (struct mrvl_ie_ds_param_set *) pos;
  1324. ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  1325. ds->header.len = cpu_to_le16(1);
  1326. ds->channel = bss->phy.ds.channel;
  1327. pos += sizeof(ds->header) + 1;
  1328. cf = (struct mrvl_ie_cf_param_set *) pos;
  1329. cf->header.type = cpu_to_le16(TLV_TYPE_CF);
  1330. tmplen = sizeof(*cf) - sizeof (cf->header);
  1331. cf->header.len = cpu_to_le16(tmplen);
  1332. /* IE payload should be zeroed, firmware fills it in for us */
  1333. pos += sizeof(*cf);
  1334. rates = (struct mrvl_ie_rates_param_set *) 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 mrvl_ie_rsn_param_set *) 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->phy.ds.channel;
  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 ieee_assoc_response *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 ieee_assoc_response *) &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_SSID_BUF(ssid);
  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 %pM, channel %d\n",
  1513. print_ssid(ssid, bss->ssid, bss->ssid_len),
  1514. priv->curbssparams.bssid,
  1515. priv->curbssparams.channel);
  1516. done:
  1517. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1518. return ret;
  1519. }