assoc.c 50 KB

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