assoc.c 52 KB

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