assoc.c 52 KB

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