assoc.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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_send_deauthentication(priv);
  859. if (ret) {
  860. lbs_deb_assoc("Deauthentication due to new "
  861. "configuration request failed: %d\n",
  862. ret);
  863. }
  864. }
  865. } else if (priv->mode == IW_MODE_ADHOC) {
  866. if (should_stop_adhoc(priv, assoc_req)) {
  867. ret = lbs_stop_adhoc_network(priv);
  868. if (ret) {
  869. lbs_deb_assoc("Teardown of AdHoc network due to "
  870. "new configuration request failed: %d\n",
  871. ret);
  872. }
  873. }
  874. }
  875. /* Send the various configuration bits to the firmware */
  876. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  877. ret = assoc_helper_mode(priv, assoc_req);
  878. if (ret)
  879. goto out;
  880. }
  881. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  882. ret = assoc_helper_channel(priv, assoc_req);
  883. if (ret)
  884. goto out;
  885. }
  886. if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
  887. || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
  888. ret = assoc_helper_wep_keys(priv, assoc_req);
  889. if (ret)
  890. goto out;
  891. }
  892. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  893. ret = assoc_helper_secinfo(priv, assoc_req);
  894. if (ret)
  895. goto out;
  896. }
  897. if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  898. ret = assoc_helper_wpa_ie(priv, assoc_req);
  899. if (ret)
  900. goto out;
  901. }
  902. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
  903. || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  904. ret = assoc_helper_wpa_keys(priv, assoc_req);
  905. if (ret)
  906. goto out;
  907. }
  908. /* SSID/BSSID should be the _last_ config option set, because they
  909. * trigger the association attempt.
  910. */
  911. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
  912. || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  913. int success = 1;
  914. ret = assoc_helper_associate(priv, assoc_req);
  915. if (ret) {
  916. lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
  917. ret);
  918. success = 0;
  919. }
  920. if (priv->connect_status != LBS_CONNECTED) {
  921. lbs_deb_assoc("ASSOC: association unsuccessful, "
  922. "not connected\n");
  923. success = 0;
  924. }
  925. if (success) {
  926. lbs_deb_assoc("associated to %s\n",
  927. print_mac(mac, priv->curbssparams.bssid));
  928. lbs_prepare_and_send_command(priv,
  929. CMD_802_11_RSSI,
  930. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  931. } else {
  932. ret = -1;
  933. }
  934. }
  935. out:
  936. if (ret) {
  937. lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
  938. ret);
  939. }
  940. mutex_lock(&priv->lock);
  941. priv->in_progress_assoc_req = NULL;
  942. mutex_unlock(&priv->lock);
  943. kfree(assoc_req);
  944. done:
  945. lbs_deb_leave(LBS_DEB_ASSOC);
  946. }
  947. /*
  948. * Caller MUST hold any necessary locks
  949. */
  950. struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
  951. {
  952. struct assoc_request * assoc_req;
  953. lbs_deb_enter(LBS_DEB_ASSOC);
  954. if (!priv->pending_assoc_req) {
  955. priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
  956. GFP_KERNEL);
  957. if (!priv->pending_assoc_req) {
  958. lbs_pr_info("Not enough memory to allocate association"
  959. " request!\n");
  960. return NULL;
  961. }
  962. }
  963. /* Copy current configuration attributes to the association request,
  964. * but don't overwrite any that are already set.
  965. */
  966. assoc_req = priv->pending_assoc_req;
  967. if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  968. memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
  969. IW_ESSID_MAX_SIZE);
  970. assoc_req->ssid_len = priv->curbssparams.ssid_len;
  971. }
  972. if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  973. assoc_req->channel = priv->curbssparams.channel;
  974. if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
  975. assoc_req->band = priv->curbssparams.band;
  976. if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
  977. assoc_req->mode = priv->mode;
  978. if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  979. memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
  980. ETH_ALEN);
  981. }
  982. if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
  983. int i;
  984. for (i = 0; i < 4; i++) {
  985. memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
  986. sizeof(struct enc_key));
  987. }
  988. }
  989. if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
  990. assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
  991. if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  992. memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
  993. sizeof(struct enc_key));
  994. }
  995. if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  996. memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
  997. sizeof(struct enc_key));
  998. }
  999. if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  1000. memcpy(&assoc_req->secinfo, &priv->secinfo,
  1001. sizeof(struct lbs_802_11_security));
  1002. }
  1003. if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  1004. memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
  1005. MAX_WPA_IE_LEN);
  1006. assoc_req->wpa_ie_len = priv->wpa_ie_len;
  1007. }
  1008. lbs_deb_leave(LBS_DEB_ASSOC);
  1009. return assoc_req;
  1010. }
  1011. /**
  1012. * @brief This function finds common rates between rate1 and card rates.
  1013. *
  1014. * It will fill common rates in rate1 as output if found.
  1015. *
  1016. * NOTE: Setting the MSB of the basic rates need to be taken
  1017. * care, either before or after calling this function
  1018. *
  1019. * @param priv A pointer to struct lbs_private structure
  1020. * @param rate1 the buffer which keeps input and output
  1021. * @param rate1_size the size of rate1 buffer; new size of buffer on return
  1022. *
  1023. * @return 0 or -1
  1024. */
  1025. static int get_common_rates(struct lbs_private *priv,
  1026. u8 *rates,
  1027. u16 *rates_size)
  1028. {
  1029. u8 *card_rates = lbs_bg_rates;
  1030. size_t num_card_rates = sizeof(lbs_bg_rates);
  1031. int ret = 0, i, j;
  1032. u8 tmp[30];
  1033. size_t tmp_size = 0;
  1034. /* For each rate in card_rates that exists in rate1, copy to tmp */
  1035. for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
  1036. for (j = 0; rates[j] && (j < *rates_size); j++) {
  1037. if (rates[j] == card_rates[i])
  1038. tmp[tmp_size++] = card_rates[i];
  1039. }
  1040. }
  1041. lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
  1042. lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
  1043. lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
  1044. lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
  1045. if (!priv->enablehwauto) {
  1046. for (i = 0; i < tmp_size; i++) {
  1047. if (tmp[i] == priv->cur_rate)
  1048. goto done;
  1049. }
  1050. lbs_pr_alert("Previously set fixed data rate %#x isn't "
  1051. "compatible with the network.\n", priv->cur_rate);
  1052. ret = -1;
  1053. goto done;
  1054. }
  1055. ret = 0;
  1056. done:
  1057. memset(rates, 0, *rates_size);
  1058. *rates_size = min_t(int, tmp_size, *rates_size);
  1059. memcpy(rates, tmp, *rates_size);
  1060. return ret;
  1061. }
  1062. /**
  1063. * @brief Sets the MSB on basic rates as the firmware requires
  1064. *
  1065. * Scan through an array and set the MSB for basic data rates.
  1066. *
  1067. * @param rates buffer of data rates
  1068. * @param len size of buffer
  1069. */
  1070. static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
  1071. {
  1072. int i;
  1073. for (i = 0; i < len; i++) {
  1074. if (rates[i] == 0x02 || rates[i] == 0x04 ||
  1075. rates[i] == 0x0b || rates[i] == 0x16)
  1076. rates[i] |= 0x80;
  1077. }
  1078. }
  1079. /**
  1080. * @brief Send Deauthentication Request
  1081. *
  1082. * @param priv A pointer to struct lbs_private structure
  1083. * @return 0--success, -1--fail
  1084. */
  1085. int lbs_send_deauthentication(struct lbs_private *priv)
  1086. {
  1087. return lbs_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
  1088. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  1089. }
  1090. /**
  1091. * @brief This function prepares command of authenticate.
  1092. *
  1093. * @param priv A pointer to struct lbs_private structure
  1094. * @param cmd A pointer to cmd_ds_command structure
  1095. * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
  1096. *
  1097. * @return 0 or -1
  1098. */
  1099. int lbs_cmd_80211_authenticate(struct lbs_private *priv,
  1100. struct cmd_ds_command *cmd,
  1101. void *pdata_buf)
  1102. {
  1103. struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
  1104. int ret = -1;
  1105. u8 *bssid = pdata_buf;
  1106. DECLARE_MAC_BUF(mac);
  1107. lbs_deb_enter(LBS_DEB_JOIN);
  1108. cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
  1109. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
  1110. + S_DS_GEN);
  1111. /* translate auth mode to 802.11 defined wire value */
  1112. switch (priv->secinfo.auth_mode) {
  1113. case IW_AUTH_ALG_OPEN_SYSTEM:
  1114. pauthenticate->authtype = 0x00;
  1115. break;
  1116. case IW_AUTH_ALG_SHARED_KEY:
  1117. pauthenticate->authtype = 0x01;
  1118. break;
  1119. case IW_AUTH_ALG_LEAP:
  1120. pauthenticate->authtype = 0x80;
  1121. break;
  1122. default:
  1123. lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
  1124. priv->secinfo.auth_mode);
  1125. goto out;
  1126. }
  1127. memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
  1128. lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
  1129. print_mac(mac, bssid), pauthenticate->authtype);
  1130. ret = 0;
  1131. out:
  1132. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1133. return ret;
  1134. }
  1135. int lbs_cmd_80211_deauthenticate(struct lbs_private *priv,
  1136. struct cmd_ds_command *cmd)
  1137. {
  1138. struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
  1139. lbs_deb_enter(LBS_DEB_JOIN);
  1140. cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
  1141. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
  1142. S_DS_GEN);
  1143. /* set AP MAC address */
  1144. memmove(dauth->macaddr, priv->curbssparams.bssid, ETH_ALEN);
  1145. /* Reason code 3 = Station is leaving */
  1146. #define REASON_CODE_STA_LEAVING 3
  1147. dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
  1148. lbs_deb_leave(LBS_DEB_JOIN);
  1149. return 0;
  1150. }
  1151. int lbs_cmd_80211_associate(struct lbs_private *priv,
  1152. struct cmd_ds_command *cmd, void *pdata_buf)
  1153. {
  1154. struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
  1155. int ret = 0;
  1156. struct assoc_request *assoc_req = pdata_buf;
  1157. struct bss_descriptor *bss = &assoc_req->bss;
  1158. u8 *pos;
  1159. u16 tmpcap, tmplen;
  1160. struct mrvlietypes_ssidparamset *ssid;
  1161. struct mrvlietypes_phyparamset *phy;
  1162. struct mrvlietypes_ssparamset *ss;
  1163. struct mrvlietypes_ratesparamset *rates;
  1164. struct mrvlietypes_rsnparamset *rsn;
  1165. lbs_deb_enter(LBS_DEB_ASSOC);
  1166. pos = (u8 *) passo;
  1167. if (!priv) {
  1168. ret = -1;
  1169. goto done;
  1170. }
  1171. cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  1172. memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
  1173. pos += sizeof(passo->peerstaaddr);
  1174. /* set the listen interval */
  1175. passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  1176. pos += sizeof(passo->capability);
  1177. pos += sizeof(passo->listeninterval);
  1178. pos += sizeof(passo->bcnperiod);
  1179. pos += sizeof(passo->dtimperiod);
  1180. ssid = (struct mrvlietypes_ssidparamset *) pos;
  1181. ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
  1182. tmplen = bss->ssid_len;
  1183. ssid->header.len = cpu_to_le16(tmplen);
  1184. memcpy(ssid->ssid, bss->ssid, tmplen);
  1185. pos += sizeof(ssid->header) + tmplen;
  1186. phy = (struct mrvlietypes_phyparamset *) pos;
  1187. phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  1188. tmplen = sizeof(phy->fh_ds.dsparamset);
  1189. phy->header.len = cpu_to_le16(tmplen);
  1190. memcpy(&phy->fh_ds.dsparamset,
  1191. &bss->phyparamset.dsparamset.currentchan,
  1192. tmplen);
  1193. pos += sizeof(phy->header) + tmplen;
  1194. ss = (struct mrvlietypes_ssparamset *) pos;
  1195. ss->header.type = cpu_to_le16(TLV_TYPE_CF);
  1196. tmplen = sizeof(ss->cf_ibss.cfparamset);
  1197. ss->header.len = cpu_to_le16(tmplen);
  1198. pos += sizeof(ss->header) + tmplen;
  1199. rates = (struct mrvlietypes_ratesparamset *) pos;
  1200. rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
  1201. memcpy(&rates->rates, &bss->rates, MAX_RATES);
  1202. tmplen = MAX_RATES;
  1203. if (get_common_rates(priv, rates->rates, &tmplen)) {
  1204. ret = -1;
  1205. goto done;
  1206. }
  1207. pos += sizeof(rates->header) + tmplen;
  1208. rates->header.len = cpu_to_le16(tmplen);
  1209. lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
  1210. /* Copy the infra. association rates into Current BSS state structure */
  1211. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1212. memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
  1213. /* Set MSB on basic rates as the firmware requires, but _after_
  1214. * copying to current bss rates.
  1215. */
  1216. lbs_set_basic_rate_flags(rates->rates, tmplen);
  1217. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  1218. rsn = (struct mrvlietypes_rsnparamset *) pos;
  1219. /* WPA_IE or WPA2_IE */
  1220. rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
  1221. tmplen = (u16) assoc_req->wpa_ie[1];
  1222. rsn->header.len = cpu_to_le16(tmplen);
  1223. memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
  1224. lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
  1225. sizeof(rsn->header) + tmplen);
  1226. pos += sizeof(rsn->header) + tmplen;
  1227. }
  1228. /* update curbssparams */
  1229. priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
  1230. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  1231. ret = -1;
  1232. goto done;
  1233. }
  1234. cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
  1235. /* set the capability info */
  1236. tmpcap = (bss->capability & CAPINFO_MASK);
  1237. if (bss->mode == IW_MODE_INFRA)
  1238. tmpcap |= WLAN_CAPABILITY_ESS;
  1239. passo->capability = cpu_to_le16(tmpcap);
  1240. lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
  1241. done:
  1242. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1243. return ret;
  1244. }
  1245. int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
  1246. struct cmd_ds_command *cmd, void *pdata_buf)
  1247. {
  1248. struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
  1249. int ret = 0;
  1250. int cmdappendsize = 0;
  1251. struct assoc_request *assoc_req = pdata_buf;
  1252. u16 tmpcap = 0;
  1253. size_t ratesize = 0;
  1254. lbs_deb_enter(LBS_DEB_JOIN);
  1255. if (!priv) {
  1256. ret = -1;
  1257. goto done;
  1258. }
  1259. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
  1260. /*
  1261. * Fill in the parameters for 2 data structures:
  1262. * 1. cmd_ds_802_11_ad_hoc_start command
  1263. * 2. priv->scantable[i]
  1264. *
  1265. * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
  1266. * probe delay, and cap info.
  1267. *
  1268. * Firmware will fill up beacon period, DTIM, Basic rates
  1269. * and operational rates.
  1270. */
  1271. memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
  1272. memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
  1273. lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
  1274. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  1275. assoc_req->ssid_len);
  1276. /* set the BSS type */
  1277. adhs->bsstype = CMD_BSS_TYPE_IBSS;
  1278. priv->mode = IW_MODE_ADHOC;
  1279. if (priv->beacon_period == 0)
  1280. priv->beacon_period = MRVDRV_BEACON_INTERVAL;
  1281. adhs->beaconperiod = cpu_to_le16(priv->beacon_period);
  1282. /* set Physical param set */
  1283. #define DS_PARA_IE_ID 3
  1284. #define DS_PARA_IE_LEN 1
  1285. adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
  1286. adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
  1287. WARN_ON(!assoc_req->channel);
  1288. lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
  1289. assoc_req->channel);
  1290. adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
  1291. /* set IBSS param set */
  1292. #define IBSS_PARA_IE_ID 6
  1293. #define IBSS_PARA_IE_LEN 2
  1294. adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
  1295. adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
  1296. adhs->ssparamset.ibssparamset.atimwindow = 0;
  1297. /* set capability info */
  1298. tmpcap = WLAN_CAPABILITY_IBSS;
  1299. if (assoc_req->secinfo.wep_enabled) {
  1300. lbs_deb_join("ADHOC_S_CMD: WEP enabled, "
  1301. "setting privacy on\n");
  1302. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  1303. } else {
  1304. lbs_deb_join("ADHOC_S_CMD: WEP disabled, "
  1305. "setting privacy off\n");
  1306. }
  1307. adhs->capability = cpu_to_le16(tmpcap);
  1308. /* probedelay */
  1309. adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1310. memset(adhs->rates, 0, sizeof(adhs->rates));
  1311. ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
  1312. memcpy(adhs->rates, lbs_bg_rates, ratesize);
  1313. /* Copy the ad-hoc creating rates into Current BSS state structure */
  1314. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1315. memcpy(&priv->curbssparams.rates, &adhs->rates, ratesize);
  1316. /* Set MSB on basic rates as the firmware requires, but _after_
  1317. * copying to current bss rates.
  1318. */
  1319. lbs_set_basic_rate_flags(adhs->rates, ratesize);
  1320. lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
  1321. adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
  1322. lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
  1323. if (lbs_create_dnld_countryinfo_11d(priv)) {
  1324. lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
  1325. ret = -1;
  1326. goto done;
  1327. }
  1328. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
  1329. S_DS_GEN + cmdappendsize);
  1330. ret = 0;
  1331. done:
  1332. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1333. return ret;
  1334. }
  1335. int lbs_cmd_80211_ad_hoc_stop(struct cmd_ds_command *cmd)
  1336. {
  1337. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
  1338. cmd->size = cpu_to_le16(S_DS_GEN);
  1339. return 0;
  1340. }
  1341. int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
  1342. struct cmd_ds_command *cmd, void *pdata_buf)
  1343. {
  1344. struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
  1345. struct assoc_request *assoc_req = pdata_buf;
  1346. struct bss_descriptor *bss = &assoc_req->bss;
  1347. int cmdappendsize = 0;
  1348. int ret = 0;
  1349. u16 ratesize = 0;
  1350. DECLARE_MAC_BUF(mac);
  1351. lbs_deb_enter(LBS_DEB_JOIN);
  1352. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
  1353. join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
  1354. join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
  1355. memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
  1356. memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
  1357. memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
  1358. sizeof(union ieeetypes_phyparamset));
  1359. memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
  1360. sizeof(union IEEEtypes_ssparamset));
  1361. join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  1362. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  1363. bss->capability, CAPINFO_MASK);
  1364. /* information on BSSID descriptor passed to FW */
  1365. lbs_deb_join(
  1366. "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
  1367. print_mac(mac, join_cmd->bss.bssid),
  1368. join_cmd->bss.ssid);
  1369. /* failtimeout */
  1370. join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  1371. /* probedelay */
  1372. join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1373. priv->curbssparams.channel = bss->channel;
  1374. /* Copy Data rates from the rates recorded in scan response */
  1375. memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
  1376. ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
  1377. memcpy(join_cmd->bss.rates, bss->rates, ratesize);
  1378. if (get_common_rates(priv, join_cmd->bss.rates, &ratesize)) {
  1379. lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
  1380. ret = -1;
  1381. goto done;
  1382. }
  1383. /* Copy the ad-hoc creating rates into Current BSS state structure */
  1384. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1385. memcpy(&priv->curbssparams.rates, join_cmd->bss.rates, ratesize);
  1386. /* Set MSB on basic rates as the firmware requires, but _after_
  1387. * copying to current bss rates.
  1388. */
  1389. lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
  1390. join_cmd->bss.ssparamset.ibssparamset.atimwindow =
  1391. cpu_to_le16(bss->atimwindow);
  1392. if (assoc_req->secinfo.wep_enabled) {
  1393. u16 tmp = le16_to_cpu(join_cmd->bss.capability);
  1394. tmp |= WLAN_CAPABILITY_PRIVACY;
  1395. join_cmd->bss.capability = cpu_to_le16(tmp);
  1396. }
  1397. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  1398. /* wake up first */
  1399. __le32 Localpsmode;
  1400. Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
  1401. ret = lbs_prepare_and_send_command(priv,
  1402. CMD_802_11_PS_MODE,
  1403. CMD_ACT_SET,
  1404. 0, 0, &Localpsmode);
  1405. if (ret) {
  1406. ret = -1;
  1407. goto done;
  1408. }
  1409. }
  1410. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  1411. ret = -1;
  1412. goto done;
  1413. }
  1414. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
  1415. S_DS_GEN + cmdappendsize);
  1416. done:
  1417. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1418. return ret;
  1419. }
  1420. int lbs_ret_80211_associate(struct lbs_private *priv,
  1421. struct cmd_ds_command *resp)
  1422. {
  1423. int ret = 0;
  1424. union iwreq_data wrqu;
  1425. struct ieeetypes_assocrsp *passocrsp;
  1426. struct bss_descriptor *bss;
  1427. u16 status_code;
  1428. lbs_deb_enter(LBS_DEB_ASSOC);
  1429. if (!priv->in_progress_assoc_req) {
  1430. lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
  1431. ret = -1;
  1432. goto done;
  1433. }
  1434. bss = &priv->in_progress_assoc_req->bss;
  1435. passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
  1436. /*
  1437. * Older FW versions map the IEEE 802.11 Status Code in the association
  1438. * response to the following values returned in passocrsp->statuscode:
  1439. *
  1440. * IEEE Status Code Marvell Status Code
  1441. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  1442. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1443. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1444. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1445. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1446. * others -> 0x0003 ASSOC_RESULT_REFUSED
  1447. *
  1448. * Other response codes:
  1449. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  1450. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  1451. * association response from the AP)
  1452. */
  1453. status_code = le16_to_cpu(passocrsp->statuscode);
  1454. switch (status_code) {
  1455. case 0x00:
  1456. break;
  1457. case 0x01:
  1458. lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
  1459. break;
  1460. case 0x02:
  1461. lbs_deb_assoc("ASSOC_RESP: internal timer "
  1462. "expired while waiting for the AP\n");
  1463. break;
  1464. case 0x03:
  1465. lbs_deb_assoc("ASSOC_RESP: association "
  1466. "refused by AP\n");
  1467. break;
  1468. case 0x04:
  1469. lbs_deb_assoc("ASSOC_RESP: authentication "
  1470. "refused by AP\n");
  1471. break;
  1472. default:
  1473. lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
  1474. " unknown\n", status_code);
  1475. break;
  1476. }
  1477. if (status_code) {
  1478. lbs_mac_event_disconnected(priv);
  1479. ret = -1;
  1480. goto done;
  1481. }
  1482. lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
  1483. le16_to_cpu(resp->size) - S_DS_GEN);
  1484. /* Send a Media Connected event, according to the Spec */
  1485. priv->connect_status = LBS_CONNECTED;
  1486. /* Update current SSID and BSSID */
  1487. memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  1488. priv->curbssparams.ssid_len = bss->ssid_len;
  1489. memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
  1490. priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  1491. priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
  1492. memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
  1493. memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
  1494. priv->nextSNRNF = 0;
  1495. priv->numSNRNF = 0;
  1496. netif_carrier_on(priv->dev);
  1497. if (!priv->tx_pending_len)
  1498. netif_wake_queue(priv->dev);
  1499. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
  1500. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1501. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1502. done:
  1503. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1504. return ret;
  1505. }
  1506. int lbs_ret_80211_disassociate(struct lbs_private *priv)
  1507. {
  1508. lbs_deb_enter(LBS_DEB_JOIN);
  1509. lbs_mac_event_disconnected(priv);
  1510. lbs_deb_leave(LBS_DEB_JOIN);
  1511. return 0;
  1512. }
  1513. int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
  1514. struct cmd_ds_command *resp)
  1515. {
  1516. int ret = 0;
  1517. u16 command = le16_to_cpu(resp->command);
  1518. u16 result = le16_to_cpu(resp->result);
  1519. struct cmd_ds_802_11_ad_hoc_result *padhocresult;
  1520. union iwreq_data wrqu;
  1521. struct bss_descriptor *bss;
  1522. DECLARE_MAC_BUF(mac);
  1523. lbs_deb_enter(LBS_DEB_JOIN);
  1524. padhocresult = &resp->params.result;
  1525. lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
  1526. lbs_deb_join("ADHOC_RESP: command = %x\n", command);
  1527. lbs_deb_join("ADHOC_RESP: result = %x\n", result);
  1528. if (!priv->in_progress_assoc_req) {
  1529. lbs_deb_join("ADHOC_RESP: no in-progress association "
  1530. "request\n");
  1531. ret = -1;
  1532. goto done;
  1533. }
  1534. bss = &priv->in_progress_assoc_req->bss;
  1535. /*
  1536. * Join result code 0 --> SUCCESS
  1537. */
  1538. if (result) {
  1539. lbs_deb_join("ADHOC_RESP: failed\n");
  1540. if (priv->connect_status == LBS_CONNECTED)
  1541. lbs_mac_event_disconnected(priv);
  1542. ret = -1;
  1543. goto done;
  1544. }
  1545. /*
  1546. * Now the join cmd should be successful
  1547. * If BSSID has changed use SSID to compare instead of BSSID
  1548. */
  1549. lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
  1550. escape_essid(bss->ssid, bss->ssid_len));
  1551. /* Send a Media Connected event, according to the Spec */
  1552. priv->connect_status = LBS_CONNECTED;
  1553. if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
  1554. /* Update the created network descriptor with the new BSSID */
  1555. memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
  1556. }
  1557. /* Set the BSSID from the joined/started descriptor */
  1558. memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
  1559. /* Set the new SSID to current SSID */
  1560. memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  1561. priv->curbssparams.ssid_len = bss->ssid_len;
  1562. netif_carrier_on(priv->dev);
  1563. if (!priv->tx_pending_len)
  1564. netif_wake_queue(priv->dev);
  1565. memset(&wrqu, 0, sizeof(wrqu));
  1566. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
  1567. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1568. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1569. lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
  1570. lbs_deb_join("ADHOC_RESP: channel = %d\n", priv->curbssparams.channel);
  1571. lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
  1572. print_mac(mac, padhocresult->bssid));
  1573. done:
  1574. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1575. return ret;
  1576. }
  1577. int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv)
  1578. {
  1579. lbs_deb_enter(LBS_DEB_JOIN);
  1580. lbs_mac_event_disconnected(priv);
  1581. lbs_deb_leave(LBS_DEB_JOIN);
  1582. return 0;
  1583. }