assoc.c 50 KB

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