assoc.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  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. int lbs_update_channel(struct lbs_private *priv)
  498. {
  499. int ret;
  500. /* the channel in f/w could be out of sync; get the current channel */
  501. lbs_deb_enter(LBS_DEB_ASSOC);
  502. ret = lbs_get_channel(priv);
  503. if (ret > 0) {
  504. priv->curbssparams.channel = ret;
  505. ret = 0;
  506. }
  507. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  508. return ret;
  509. }
  510. static int assoc_helper_channel(struct lbs_private *priv,
  511. struct assoc_request * assoc_req)
  512. {
  513. int ret = 0;
  514. lbs_deb_enter(LBS_DEB_ASSOC);
  515. ret = lbs_update_channel(priv);
  516. if (ret) {
  517. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  518. goto done;
  519. }
  520. if (assoc_req->channel == priv->curbssparams.channel)
  521. goto done;
  522. if (priv->mesh_dev) {
  523. /* Change mesh channel first; 21.p21 firmware won't let
  524. you change channel otherwise (even though it'll return
  525. an error to this */
  526. lbs_mesh_config(priv, 0, assoc_req->channel);
  527. }
  528. lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
  529. priv->curbssparams.channel, assoc_req->channel);
  530. ret = lbs_set_channel(priv, assoc_req->channel);
  531. if (ret < 0)
  532. lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
  533. /* FIXME: shouldn't need to grab the channel _again_ after setting
  534. * it since the firmware is supposed to return the new channel, but
  535. * whatever... */
  536. ret = lbs_update_channel(priv);
  537. if (ret) {
  538. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  539. goto done;
  540. }
  541. if (assoc_req->channel != priv->curbssparams.channel) {
  542. lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
  543. assoc_req->channel);
  544. goto restore_mesh;
  545. }
  546. if ( assoc_req->secinfo.wep_enabled
  547. && (assoc_req->wep_keys[0].len
  548. || assoc_req->wep_keys[1].len
  549. || assoc_req->wep_keys[2].len
  550. || assoc_req->wep_keys[3].len)) {
  551. /* Make sure WEP keys are re-sent to firmware */
  552. set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
  553. }
  554. /* Must restart/rejoin adhoc networks after channel change */
  555. set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
  556. restore_mesh:
  557. if (priv->mesh_dev)
  558. lbs_mesh_config(priv, 1, priv->curbssparams.channel);
  559. done:
  560. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  561. return ret;
  562. }
  563. static int assoc_helper_wep_keys(struct lbs_private *priv,
  564. struct assoc_request *assoc_req)
  565. {
  566. int i;
  567. int ret = 0;
  568. lbs_deb_enter(LBS_DEB_ASSOC);
  569. /* Set or remove WEP keys */
  570. if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
  571. assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
  572. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
  573. else
  574. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
  575. if (ret)
  576. goto out;
  577. /* enable/disable the MAC's WEP packet filter */
  578. if (assoc_req->secinfo.wep_enabled)
  579. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  580. else
  581. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  582. lbs_set_mac_control(priv);
  583. mutex_lock(&priv->lock);
  584. /* Copy WEP keys into priv wep key fields */
  585. for (i = 0; i < 4; i++) {
  586. memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
  587. sizeof(struct enc_key));
  588. }
  589. priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
  590. mutex_unlock(&priv->lock);
  591. out:
  592. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  593. return ret;
  594. }
  595. static int assoc_helper_secinfo(struct lbs_private *priv,
  596. struct assoc_request * assoc_req)
  597. {
  598. int ret = 0;
  599. uint16_t do_wpa;
  600. uint16_t rsn = 0;
  601. lbs_deb_enter(LBS_DEB_ASSOC);
  602. memcpy(&priv->secinfo, &assoc_req->secinfo,
  603. sizeof(struct lbs_802_11_security));
  604. lbs_set_mac_control(priv);
  605. /* If RSN is already enabled, don't try to enable it again, since
  606. * ENABLE_RSN resets internal state machines and will clobber the
  607. * 4-way WPA handshake.
  608. */
  609. /* Get RSN enabled/disabled */
  610. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
  611. if (ret) {
  612. lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
  613. goto out;
  614. }
  615. /* Don't re-enable RSN if it's already enabled */
  616. do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
  617. if (do_wpa == rsn)
  618. goto out;
  619. /* Set RSN enabled/disabled */
  620. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
  621. out:
  622. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  623. return ret;
  624. }
  625. static int assoc_helper_wpa_keys(struct lbs_private *priv,
  626. struct assoc_request * assoc_req)
  627. {
  628. int ret = 0;
  629. unsigned int flags = assoc_req->flags;
  630. lbs_deb_enter(LBS_DEB_ASSOC);
  631. /* Work around older firmware bug where WPA unicast and multicast
  632. * keys must be set independently. Seen in SDIO parts with firmware
  633. * version 5.0.11p0.
  634. */
  635. if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  636. clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
  637. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  638. assoc_req->flags = flags;
  639. }
  640. if (ret)
  641. goto out;
  642. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  643. clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
  644. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  645. assoc_req->flags = flags;
  646. }
  647. out:
  648. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  649. return ret;
  650. }
  651. static int assoc_helper_wpa_ie(struct lbs_private *priv,
  652. struct assoc_request * assoc_req)
  653. {
  654. int ret = 0;
  655. lbs_deb_enter(LBS_DEB_ASSOC);
  656. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  657. memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
  658. priv->wpa_ie_len = assoc_req->wpa_ie_len;
  659. } else {
  660. memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
  661. priv->wpa_ie_len = 0;
  662. }
  663. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  664. return ret;
  665. }
  666. static int should_deauth_infrastructure(struct lbs_private *priv,
  667. struct assoc_request * assoc_req)
  668. {
  669. int ret = 0;
  670. if (priv->connect_status != LBS_CONNECTED)
  671. return 0;
  672. lbs_deb_enter(LBS_DEB_ASSOC);
  673. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  674. lbs_deb_assoc("Deauthenticating due to new SSID\n");
  675. ret = 1;
  676. goto out;
  677. }
  678. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  679. if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
  680. lbs_deb_assoc("Deauthenticating due to new security\n");
  681. ret = 1;
  682. goto out;
  683. }
  684. }
  685. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  686. lbs_deb_assoc("Deauthenticating due to new BSSID\n");
  687. ret = 1;
  688. goto out;
  689. }
  690. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  691. lbs_deb_assoc("Deauthenticating due to channel switch\n");
  692. ret = 1;
  693. goto out;
  694. }
  695. /* FIXME: deal with 'auto' mode somehow */
  696. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  697. if (assoc_req->mode != IW_MODE_INFRA) {
  698. lbs_deb_assoc("Deauthenticating due to leaving "
  699. "infra mode\n");
  700. ret = 1;
  701. goto out;
  702. }
  703. }
  704. out:
  705. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  706. return ret;
  707. }
  708. static int should_stop_adhoc(struct lbs_private *priv,
  709. struct assoc_request * assoc_req)
  710. {
  711. lbs_deb_enter(LBS_DEB_ASSOC);
  712. if (priv->connect_status != LBS_CONNECTED)
  713. return 0;
  714. if (lbs_ssid_cmp(priv->curbssparams.ssid,
  715. priv->curbssparams.ssid_len,
  716. assoc_req->ssid, assoc_req->ssid_len) != 0)
  717. return 1;
  718. /* FIXME: deal with 'auto' mode somehow */
  719. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  720. if (assoc_req->mode != IW_MODE_ADHOC)
  721. return 1;
  722. }
  723. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  724. if (assoc_req->channel != priv->curbssparams.channel)
  725. return 1;
  726. }
  727. lbs_deb_leave(LBS_DEB_ASSOC);
  728. return 0;
  729. }
  730. /**
  731. * @brief This function finds the best SSID in the Scan List
  732. *
  733. * Search the scan table for the best SSID that also matches the current
  734. * adapter network preference (infrastructure or adhoc)
  735. *
  736. * @param priv A pointer to struct lbs_private
  737. *
  738. * @return index in BSSID list
  739. */
  740. static struct bss_descriptor *lbs_find_best_ssid_in_list(
  741. struct lbs_private *priv, uint8_t mode)
  742. {
  743. uint8_t bestrssi = 0;
  744. struct bss_descriptor *iter_bss;
  745. struct bss_descriptor *best_bss = NULL;
  746. lbs_deb_enter(LBS_DEB_SCAN);
  747. mutex_lock(&priv->lock);
  748. list_for_each_entry(iter_bss, &priv->network_list, list) {
  749. switch (mode) {
  750. case IW_MODE_INFRA:
  751. case IW_MODE_ADHOC:
  752. if (!is_network_compatible(priv, iter_bss, mode))
  753. break;
  754. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  755. break;
  756. bestrssi = SCAN_RSSI(iter_bss->rssi);
  757. best_bss = iter_bss;
  758. break;
  759. case IW_MODE_AUTO:
  760. default:
  761. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  762. break;
  763. bestrssi = SCAN_RSSI(iter_bss->rssi);
  764. best_bss = iter_bss;
  765. break;
  766. }
  767. }
  768. mutex_unlock(&priv->lock);
  769. lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
  770. return best_bss;
  771. }
  772. /**
  773. * @brief Find the best AP
  774. *
  775. * Used from association worker.
  776. *
  777. * @param priv A pointer to struct lbs_private structure
  778. * @param pSSID A pointer to AP's ssid
  779. *
  780. * @return 0--success, otherwise--fail
  781. */
  782. static int lbs_find_best_network_ssid(struct lbs_private *priv,
  783. uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
  784. uint8_t *out_mode)
  785. {
  786. int ret = -1;
  787. struct bss_descriptor *found;
  788. lbs_deb_enter(LBS_DEB_SCAN);
  789. priv->scan_ssid_len = 0;
  790. lbs_scan_networks(priv, 1);
  791. if (priv->surpriseremoved)
  792. goto out;
  793. found = lbs_find_best_ssid_in_list(priv, preferred_mode);
  794. if (found && (found->ssid_len > 0)) {
  795. memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
  796. *out_ssid_len = found->ssid_len;
  797. *out_mode = found->mode;
  798. ret = 0;
  799. }
  800. out:
  801. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  802. return ret;
  803. }
  804. void lbs_association_worker(struct work_struct *work)
  805. {
  806. struct lbs_private *priv = container_of(work, struct lbs_private,
  807. assoc_work.work);
  808. struct assoc_request * assoc_req = NULL;
  809. int ret = 0;
  810. int find_any_ssid = 0;
  811. DECLARE_MAC_BUF(mac);
  812. lbs_deb_enter(LBS_DEB_ASSOC);
  813. mutex_lock(&priv->lock);
  814. assoc_req = priv->pending_assoc_req;
  815. priv->pending_assoc_req = NULL;
  816. priv->in_progress_assoc_req = assoc_req;
  817. mutex_unlock(&priv->lock);
  818. if (!assoc_req)
  819. goto done;
  820. lbs_deb_assoc(
  821. "Association Request:\n"
  822. " flags: 0x%08lx\n"
  823. " SSID: '%s'\n"
  824. " chann: %d\n"
  825. " band: %d\n"
  826. " mode: %d\n"
  827. " BSSID: %s\n"
  828. " secinfo: %s%s%s\n"
  829. " auth_mode: %d\n",
  830. assoc_req->flags,
  831. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  832. assoc_req->channel, assoc_req->band, assoc_req->mode,
  833. print_mac(mac, assoc_req->bssid),
  834. assoc_req->secinfo.WPAenabled ? " WPA" : "",
  835. assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
  836. assoc_req->secinfo.wep_enabled ? " WEP" : "",
  837. assoc_req->secinfo.auth_mode);
  838. /* If 'any' SSID was specified, find an SSID to associate with */
  839. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
  840. && !assoc_req->ssid_len)
  841. find_any_ssid = 1;
  842. /* But don't use 'any' SSID if there's a valid locked BSSID to use */
  843. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  844. if (compare_ether_addr(assoc_req->bssid, bssid_any)
  845. && compare_ether_addr(assoc_req->bssid, bssid_off))
  846. find_any_ssid = 0;
  847. }
  848. if (find_any_ssid) {
  849. u8 new_mode;
  850. ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
  851. &assoc_req->ssid_len, assoc_req->mode, &new_mode);
  852. if (ret) {
  853. lbs_deb_assoc("Could not find best network\n");
  854. ret = -ENETUNREACH;
  855. goto out;
  856. }
  857. /* Ensure we switch to the mode of the AP */
  858. if (assoc_req->mode == IW_MODE_AUTO) {
  859. set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
  860. assoc_req->mode = new_mode;
  861. }
  862. }
  863. /*
  864. * Check if the attributes being changing require deauthentication
  865. * from the currently associated infrastructure access point.
  866. */
  867. if (priv->mode == IW_MODE_INFRA) {
  868. if (should_deauth_infrastructure(priv, assoc_req)) {
  869. ret = lbs_send_deauthentication(priv);
  870. if (ret) {
  871. lbs_deb_assoc("Deauthentication due to new "
  872. "configuration request failed: %d\n",
  873. ret);
  874. }
  875. }
  876. } else if (priv->mode == IW_MODE_ADHOC) {
  877. if (should_stop_adhoc(priv, assoc_req)) {
  878. ret = lbs_stop_adhoc_network(priv);
  879. if (ret) {
  880. lbs_deb_assoc("Teardown of AdHoc network due to "
  881. "new configuration request failed: %d\n",
  882. ret);
  883. }
  884. }
  885. }
  886. /* Send the various configuration bits to the firmware */
  887. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  888. ret = assoc_helper_mode(priv, assoc_req);
  889. if (ret)
  890. goto out;
  891. }
  892. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  893. ret = assoc_helper_channel(priv, assoc_req);
  894. if (ret)
  895. goto out;
  896. }
  897. if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
  898. || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
  899. ret = assoc_helper_wep_keys(priv, assoc_req);
  900. if (ret)
  901. goto out;
  902. }
  903. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  904. ret = assoc_helper_secinfo(priv, assoc_req);
  905. if (ret)
  906. goto out;
  907. }
  908. if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  909. ret = assoc_helper_wpa_ie(priv, assoc_req);
  910. if (ret)
  911. goto out;
  912. }
  913. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
  914. || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  915. ret = assoc_helper_wpa_keys(priv, assoc_req);
  916. if (ret)
  917. goto out;
  918. }
  919. /* SSID/BSSID should be the _last_ config option set, because they
  920. * trigger the association attempt.
  921. */
  922. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
  923. || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  924. int success = 1;
  925. ret = assoc_helper_associate(priv, assoc_req);
  926. if (ret) {
  927. lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
  928. ret);
  929. success = 0;
  930. }
  931. if (priv->connect_status != LBS_CONNECTED) {
  932. lbs_deb_assoc("ASSOC: association unsuccessful, "
  933. "not connected\n");
  934. success = 0;
  935. }
  936. if (success) {
  937. lbs_deb_assoc("associated to %s\n",
  938. print_mac(mac, priv->curbssparams.bssid));
  939. lbs_prepare_and_send_command(priv,
  940. CMD_802_11_RSSI,
  941. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  942. } else {
  943. ret = -1;
  944. }
  945. }
  946. out:
  947. if (ret) {
  948. lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
  949. ret);
  950. }
  951. mutex_lock(&priv->lock);
  952. priv->in_progress_assoc_req = NULL;
  953. mutex_unlock(&priv->lock);
  954. kfree(assoc_req);
  955. done:
  956. lbs_deb_leave(LBS_DEB_ASSOC);
  957. }
  958. /*
  959. * Caller MUST hold any necessary locks
  960. */
  961. struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
  962. {
  963. struct assoc_request * assoc_req;
  964. lbs_deb_enter(LBS_DEB_ASSOC);
  965. if (!priv->pending_assoc_req) {
  966. priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
  967. GFP_KERNEL);
  968. if (!priv->pending_assoc_req) {
  969. lbs_pr_info("Not enough memory to allocate association"
  970. " request!\n");
  971. return NULL;
  972. }
  973. }
  974. /* Copy current configuration attributes to the association request,
  975. * but don't overwrite any that are already set.
  976. */
  977. assoc_req = priv->pending_assoc_req;
  978. if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  979. memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
  980. IW_ESSID_MAX_SIZE);
  981. assoc_req->ssid_len = priv->curbssparams.ssid_len;
  982. }
  983. if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  984. assoc_req->channel = priv->curbssparams.channel;
  985. if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
  986. assoc_req->band = priv->curbssparams.band;
  987. if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
  988. assoc_req->mode = priv->mode;
  989. if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  990. memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
  991. ETH_ALEN);
  992. }
  993. if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
  994. int i;
  995. for (i = 0; i < 4; i++) {
  996. memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
  997. sizeof(struct enc_key));
  998. }
  999. }
  1000. if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
  1001. assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
  1002. if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  1003. memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
  1004. sizeof(struct enc_key));
  1005. }
  1006. if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  1007. memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
  1008. sizeof(struct enc_key));
  1009. }
  1010. if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  1011. memcpy(&assoc_req->secinfo, &priv->secinfo,
  1012. sizeof(struct lbs_802_11_security));
  1013. }
  1014. if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  1015. memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
  1016. MAX_WPA_IE_LEN);
  1017. assoc_req->wpa_ie_len = priv->wpa_ie_len;
  1018. }
  1019. lbs_deb_leave(LBS_DEB_ASSOC);
  1020. return assoc_req;
  1021. }
  1022. /**
  1023. * @brief This function finds common rates between rate1 and card rates.
  1024. *
  1025. * It will fill common rates in rate1 as output if found.
  1026. *
  1027. * NOTE: Setting the MSB of the basic rates need to be taken
  1028. * care, either before or after calling this function
  1029. *
  1030. * @param priv A pointer to struct lbs_private structure
  1031. * @param rate1 the buffer which keeps input and output
  1032. * @param rate1_size the size of rate1 buffer; new size of buffer on return
  1033. *
  1034. * @return 0 or -1
  1035. */
  1036. static int get_common_rates(struct lbs_private *priv,
  1037. u8 *rates,
  1038. u16 *rates_size)
  1039. {
  1040. u8 *card_rates = lbs_bg_rates;
  1041. size_t num_card_rates = sizeof(lbs_bg_rates);
  1042. int ret = 0, i, j;
  1043. u8 tmp[30];
  1044. size_t tmp_size = 0;
  1045. /* For each rate in card_rates that exists in rate1, copy to tmp */
  1046. for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
  1047. for (j = 0; rates[j] && (j < *rates_size); j++) {
  1048. if (rates[j] == card_rates[i])
  1049. tmp[tmp_size++] = card_rates[i];
  1050. }
  1051. }
  1052. lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
  1053. lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
  1054. lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
  1055. lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
  1056. if (!priv->auto_rate) {
  1057. for (i = 0; i < tmp_size; i++) {
  1058. if (tmp[i] == priv->cur_rate)
  1059. goto done;
  1060. }
  1061. lbs_pr_alert("Previously set fixed data rate %#x isn't "
  1062. "compatible with the network.\n", priv->cur_rate);
  1063. ret = -1;
  1064. goto done;
  1065. }
  1066. ret = 0;
  1067. done:
  1068. memset(rates, 0, *rates_size);
  1069. *rates_size = min_t(int, tmp_size, *rates_size);
  1070. memcpy(rates, tmp, *rates_size);
  1071. return ret;
  1072. }
  1073. /**
  1074. * @brief Sets the MSB on basic rates as the firmware requires
  1075. *
  1076. * Scan through an array and set the MSB for basic data rates.
  1077. *
  1078. * @param rates buffer of data rates
  1079. * @param len size of buffer
  1080. */
  1081. static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
  1082. {
  1083. int i;
  1084. for (i = 0; i < len; i++) {
  1085. if (rates[i] == 0x02 || rates[i] == 0x04 ||
  1086. rates[i] == 0x0b || rates[i] == 0x16)
  1087. rates[i] |= 0x80;
  1088. }
  1089. }
  1090. /**
  1091. * @brief Send Deauthentication Request
  1092. *
  1093. * @param priv A pointer to struct lbs_private structure
  1094. * @return 0--success, -1--fail
  1095. */
  1096. int lbs_send_deauthentication(struct lbs_private *priv)
  1097. {
  1098. return lbs_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
  1099. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  1100. }
  1101. /**
  1102. * @brief This function prepares command of authenticate.
  1103. *
  1104. * @param priv A pointer to struct lbs_private structure
  1105. * @param cmd A pointer to cmd_ds_command structure
  1106. * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
  1107. *
  1108. * @return 0 or -1
  1109. */
  1110. int lbs_cmd_80211_authenticate(struct lbs_private *priv,
  1111. struct cmd_ds_command *cmd,
  1112. void *pdata_buf)
  1113. {
  1114. struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
  1115. int ret = -1;
  1116. u8 *bssid = pdata_buf;
  1117. DECLARE_MAC_BUF(mac);
  1118. lbs_deb_enter(LBS_DEB_JOIN);
  1119. cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
  1120. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
  1121. + S_DS_GEN);
  1122. /* translate auth mode to 802.11 defined wire value */
  1123. switch (priv->secinfo.auth_mode) {
  1124. case IW_AUTH_ALG_OPEN_SYSTEM:
  1125. pauthenticate->authtype = 0x00;
  1126. break;
  1127. case IW_AUTH_ALG_SHARED_KEY:
  1128. pauthenticate->authtype = 0x01;
  1129. break;
  1130. case IW_AUTH_ALG_LEAP:
  1131. pauthenticate->authtype = 0x80;
  1132. break;
  1133. default:
  1134. lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
  1135. priv->secinfo.auth_mode);
  1136. goto out;
  1137. }
  1138. memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
  1139. lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
  1140. print_mac(mac, bssid), pauthenticate->authtype);
  1141. ret = 0;
  1142. out:
  1143. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1144. return ret;
  1145. }
  1146. int lbs_cmd_80211_deauthenticate(struct lbs_private *priv,
  1147. struct cmd_ds_command *cmd)
  1148. {
  1149. struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
  1150. lbs_deb_enter(LBS_DEB_JOIN);
  1151. cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
  1152. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
  1153. S_DS_GEN);
  1154. /* set AP MAC address */
  1155. memmove(dauth->macaddr, priv->curbssparams.bssid, ETH_ALEN);
  1156. /* Reason code 3 = Station is leaving */
  1157. #define REASON_CODE_STA_LEAVING 3
  1158. dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
  1159. lbs_deb_leave(LBS_DEB_JOIN);
  1160. return 0;
  1161. }
  1162. int lbs_cmd_80211_associate(struct lbs_private *priv,
  1163. struct cmd_ds_command *cmd, void *pdata_buf)
  1164. {
  1165. struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
  1166. int ret = 0;
  1167. struct assoc_request *assoc_req = pdata_buf;
  1168. struct bss_descriptor *bss = &assoc_req->bss;
  1169. u8 *pos;
  1170. u16 tmpcap, tmplen;
  1171. struct mrvlietypes_ssidparamset *ssid;
  1172. struct mrvlietypes_phyparamset *phy;
  1173. struct mrvlietypes_ssparamset *ss;
  1174. struct mrvlietypes_ratesparamset *rates;
  1175. struct mrvlietypes_rsnparamset *rsn;
  1176. lbs_deb_enter(LBS_DEB_ASSOC);
  1177. pos = (u8 *) passo;
  1178. if (!priv) {
  1179. ret = -1;
  1180. goto done;
  1181. }
  1182. cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  1183. memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
  1184. pos += sizeof(passo->peerstaaddr);
  1185. /* set the listen interval */
  1186. passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  1187. pos += sizeof(passo->capability);
  1188. pos += sizeof(passo->listeninterval);
  1189. pos += sizeof(passo->bcnperiod);
  1190. pos += sizeof(passo->dtimperiod);
  1191. ssid = (struct mrvlietypes_ssidparamset *) pos;
  1192. ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
  1193. tmplen = bss->ssid_len;
  1194. ssid->header.len = cpu_to_le16(tmplen);
  1195. memcpy(ssid->ssid, bss->ssid, tmplen);
  1196. pos += sizeof(ssid->header) + tmplen;
  1197. phy = (struct mrvlietypes_phyparamset *) pos;
  1198. phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  1199. tmplen = sizeof(phy->fh_ds.dsparamset);
  1200. phy->header.len = cpu_to_le16(tmplen);
  1201. memcpy(&phy->fh_ds.dsparamset,
  1202. &bss->phyparamset.dsparamset.currentchan,
  1203. tmplen);
  1204. pos += sizeof(phy->header) + tmplen;
  1205. ss = (struct mrvlietypes_ssparamset *) pos;
  1206. ss->header.type = cpu_to_le16(TLV_TYPE_CF);
  1207. tmplen = sizeof(ss->cf_ibss.cfparamset);
  1208. ss->header.len = cpu_to_le16(tmplen);
  1209. pos += sizeof(ss->header) + tmplen;
  1210. rates = (struct mrvlietypes_ratesparamset *) pos;
  1211. rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
  1212. memcpy(&rates->rates, &bss->rates, MAX_RATES);
  1213. tmplen = MAX_RATES;
  1214. if (get_common_rates(priv, rates->rates, &tmplen)) {
  1215. ret = -1;
  1216. goto done;
  1217. }
  1218. pos += sizeof(rates->header) + tmplen;
  1219. rates->header.len = cpu_to_le16(tmplen);
  1220. lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
  1221. /* Copy the infra. association rates into Current BSS state structure */
  1222. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1223. memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
  1224. /* Set MSB on basic rates as the firmware requires, but _after_
  1225. * copying to current bss rates.
  1226. */
  1227. lbs_set_basic_rate_flags(rates->rates, tmplen);
  1228. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  1229. rsn = (struct mrvlietypes_rsnparamset *) pos;
  1230. /* WPA_IE or WPA2_IE */
  1231. rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
  1232. tmplen = (u16) assoc_req->wpa_ie[1];
  1233. rsn->header.len = cpu_to_le16(tmplen);
  1234. memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
  1235. lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
  1236. sizeof(rsn->header) + tmplen);
  1237. pos += sizeof(rsn->header) + tmplen;
  1238. }
  1239. /* update curbssparams */
  1240. priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
  1241. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  1242. ret = -1;
  1243. goto done;
  1244. }
  1245. cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
  1246. /* set the capability info */
  1247. tmpcap = (bss->capability & CAPINFO_MASK);
  1248. if (bss->mode == IW_MODE_INFRA)
  1249. tmpcap |= WLAN_CAPABILITY_ESS;
  1250. passo->capability = cpu_to_le16(tmpcap);
  1251. lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
  1252. done:
  1253. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1254. return ret;
  1255. }
  1256. int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
  1257. struct cmd_ds_command *cmd, void *pdata_buf)
  1258. {
  1259. struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
  1260. int ret = 0;
  1261. int cmdappendsize = 0;
  1262. struct assoc_request *assoc_req = pdata_buf;
  1263. u16 tmpcap = 0;
  1264. size_t ratesize = 0;
  1265. lbs_deb_enter(LBS_DEB_JOIN);
  1266. if (!priv) {
  1267. ret = -1;
  1268. goto done;
  1269. }
  1270. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
  1271. /*
  1272. * Fill in the parameters for 2 data structures:
  1273. * 1. cmd_ds_802_11_ad_hoc_start command
  1274. * 2. priv->scantable[i]
  1275. *
  1276. * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
  1277. * probe delay, and cap info.
  1278. *
  1279. * Firmware will fill up beacon period, DTIM, Basic rates
  1280. * and operational rates.
  1281. */
  1282. memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
  1283. memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
  1284. lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
  1285. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  1286. assoc_req->ssid_len);
  1287. /* set the BSS type */
  1288. adhs->bsstype = CMD_BSS_TYPE_IBSS;
  1289. priv->mode = IW_MODE_ADHOC;
  1290. if (priv->beacon_period == 0)
  1291. priv->beacon_period = MRVDRV_BEACON_INTERVAL;
  1292. adhs->beaconperiod = cpu_to_le16(priv->beacon_period);
  1293. /* set Physical param set */
  1294. #define DS_PARA_IE_ID 3
  1295. #define DS_PARA_IE_LEN 1
  1296. adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
  1297. adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
  1298. WARN_ON(!assoc_req->channel);
  1299. lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
  1300. assoc_req->channel);
  1301. adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
  1302. /* set IBSS param set */
  1303. #define IBSS_PARA_IE_ID 6
  1304. #define IBSS_PARA_IE_LEN 2
  1305. adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
  1306. adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
  1307. adhs->ssparamset.ibssparamset.atimwindow = 0;
  1308. /* set capability info */
  1309. tmpcap = WLAN_CAPABILITY_IBSS;
  1310. if (assoc_req->secinfo.wep_enabled) {
  1311. lbs_deb_join("ADHOC_S_CMD: WEP enabled, "
  1312. "setting privacy on\n");
  1313. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  1314. } else {
  1315. lbs_deb_join("ADHOC_S_CMD: WEP disabled, "
  1316. "setting privacy off\n");
  1317. }
  1318. adhs->capability = cpu_to_le16(tmpcap);
  1319. /* probedelay */
  1320. adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1321. memset(adhs->rates, 0, sizeof(adhs->rates));
  1322. ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
  1323. memcpy(adhs->rates, lbs_bg_rates, ratesize);
  1324. /* Copy the ad-hoc creating rates into Current BSS state structure */
  1325. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1326. memcpy(&priv->curbssparams.rates, &adhs->rates, ratesize);
  1327. /* Set MSB on basic rates as the firmware requires, but _after_
  1328. * copying to current bss rates.
  1329. */
  1330. lbs_set_basic_rate_flags(adhs->rates, ratesize);
  1331. lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
  1332. adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
  1333. lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
  1334. if (lbs_create_dnld_countryinfo_11d(priv)) {
  1335. lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
  1336. ret = -1;
  1337. goto done;
  1338. }
  1339. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
  1340. S_DS_GEN + cmdappendsize);
  1341. ret = 0;
  1342. done:
  1343. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1344. return ret;
  1345. }
  1346. int lbs_cmd_80211_ad_hoc_stop(struct cmd_ds_command *cmd)
  1347. {
  1348. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
  1349. cmd->size = cpu_to_le16(S_DS_GEN);
  1350. return 0;
  1351. }
  1352. int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
  1353. struct cmd_ds_command *cmd, void *pdata_buf)
  1354. {
  1355. struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
  1356. struct assoc_request *assoc_req = pdata_buf;
  1357. struct bss_descriptor *bss = &assoc_req->bss;
  1358. int cmdappendsize = 0;
  1359. int ret = 0;
  1360. u16 ratesize = 0;
  1361. DECLARE_MAC_BUF(mac);
  1362. lbs_deb_enter(LBS_DEB_JOIN);
  1363. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
  1364. join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
  1365. join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
  1366. memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
  1367. memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
  1368. memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
  1369. sizeof(union ieeetypes_phyparamset));
  1370. memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
  1371. sizeof(union IEEEtypes_ssparamset));
  1372. join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  1373. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  1374. bss->capability, CAPINFO_MASK);
  1375. /* information on BSSID descriptor passed to FW */
  1376. lbs_deb_join(
  1377. "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
  1378. print_mac(mac, join_cmd->bss.bssid),
  1379. join_cmd->bss.ssid);
  1380. /* failtimeout */
  1381. join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  1382. /* probedelay */
  1383. join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1384. priv->curbssparams.channel = bss->channel;
  1385. /* Copy Data rates from the rates recorded in scan response */
  1386. memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
  1387. ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
  1388. memcpy(join_cmd->bss.rates, bss->rates, ratesize);
  1389. if (get_common_rates(priv, join_cmd->bss.rates, &ratesize)) {
  1390. lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
  1391. ret = -1;
  1392. goto done;
  1393. }
  1394. /* Copy the ad-hoc creating rates into Current BSS state structure */
  1395. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1396. memcpy(&priv->curbssparams.rates, join_cmd->bss.rates, ratesize);
  1397. /* Set MSB on basic rates as the firmware requires, but _after_
  1398. * copying to current bss rates.
  1399. */
  1400. lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
  1401. join_cmd->bss.ssparamset.ibssparamset.atimwindow =
  1402. cpu_to_le16(bss->atimwindow);
  1403. if (assoc_req->secinfo.wep_enabled) {
  1404. u16 tmp = le16_to_cpu(join_cmd->bss.capability);
  1405. tmp |= WLAN_CAPABILITY_PRIVACY;
  1406. join_cmd->bss.capability = cpu_to_le16(tmp);
  1407. }
  1408. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  1409. /* wake up first */
  1410. __le32 Localpsmode;
  1411. Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
  1412. ret = lbs_prepare_and_send_command(priv,
  1413. CMD_802_11_PS_MODE,
  1414. CMD_ACT_SET,
  1415. 0, 0, &Localpsmode);
  1416. if (ret) {
  1417. ret = -1;
  1418. goto done;
  1419. }
  1420. }
  1421. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  1422. ret = -1;
  1423. goto done;
  1424. }
  1425. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
  1426. S_DS_GEN + cmdappendsize);
  1427. done:
  1428. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1429. return ret;
  1430. }
  1431. int lbs_ret_80211_associate(struct lbs_private *priv,
  1432. struct cmd_ds_command *resp)
  1433. {
  1434. int ret = 0;
  1435. union iwreq_data wrqu;
  1436. struct ieeetypes_assocrsp *passocrsp;
  1437. struct bss_descriptor *bss;
  1438. u16 status_code;
  1439. lbs_deb_enter(LBS_DEB_ASSOC);
  1440. if (!priv->in_progress_assoc_req) {
  1441. lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
  1442. ret = -1;
  1443. goto done;
  1444. }
  1445. bss = &priv->in_progress_assoc_req->bss;
  1446. passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
  1447. /*
  1448. * Older FW versions map the IEEE 802.11 Status Code in the association
  1449. * response to the following values returned in passocrsp->statuscode:
  1450. *
  1451. * IEEE Status Code Marvell Status Code
  1452. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  1453. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1454. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1455. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1456. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1457. * others -> 0x0003 ASSOC_RESULT_REFUSED
  1458. *
  1459. * Other response codes:
  1460. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  1461. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  1462. * association response from the AP)
  1463. */
  1464. status_code = le16_to_cpu(passocrsp->statuscode);
  1465. switch (status_code) {
  1466. case 0x00:
  1467. break;
  1468. case 0x01:
  1469. lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
  1470. break;
  1471. case 0x02:
  1472. lbs_deb_assoc("ASSOC_RESP: internal timer "
  1473. "expired while waiting for the AP\n");
  1474. break;
  1475. case 0x03:
  1476. lbs_deb_assoc("ASSOC_RESP: association "
  1477. "refused by AP\n");
  1478. break;
  1479. case 0x04:
  1480. lbs_deb_assoc("ASSOC_RESP: authentication "
  1481. "refused by AP\n");
  1482. break;
  1483. default:
  1484. lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
  1485. " unknown\n", status_code);
  1486. break;
  1487. }
  1488. if (status_code) {
  1489. lbs_mac_event_disconnected(priv);
  1490. ret = -1;
  1491. goto done;
  1492. }
  1493. lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
  1494. le16_to_cpu(resp->size) - S_DS_GEN);
  1495. /* Send a Media Connected event, according to the Spec */
  1496. priv->connect_status = LBS_CONNECTED;
  1497. /* Update current SSID and BSSID */
  1498. memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  1499. priv->curbssparams.ssid_len = bss->ssid_len;
  1500. memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
  1501. priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  1502. priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
  1503. memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
  1504. memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
  1505. priv->nextSNRNF = 0;
  1506. priv->numSNRNF = 0;
  1507. netif_carrier_on(priv->dev);
  1508. if (!priv->tx_pending_len)
  1509. netif_wake_queue(priv->dev);
  1510. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
  1511. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1512. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1513. done:
  1514. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1515. return ret;
  1516. }
  1517. int lbs_ret_80211_disassociate(struct lbs_private *priv)
  1518. {
  1519. lbs_deb_enter(LBS_DEB_JOIN);
  1520. lbs_mac_event_disconnected(priv);
  1521. lbs_deb_leave(LBS_DEB_JOIN);
  1522. return 0;
  1523. }
  1524. int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
  1525. struct cmd_ds_command *resp)
  1526. {
  1527. int ret = 0;
  1528. u16 command = le16_to_cpu(resp->command);
  1529. u16 result = le16_to_cpu(resp->result);
  1530. struct cmd_ds_802_11_ad_hoc_result *padhocresult;
  1531. union iwreq_data wrqu;
  1532. struct bss_descriptor *bss;
  1533. DECLARE_MAC_BUF(mac);
  1534. lbs_deb_enter(LBS_DEB_JOIN);
  1535. padhocresult = &resp->params.result;
  1536. lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
  1537. lbs_deb_join("ADHOC_RESP: command = %x\n", command);
  1538. lbs_deb_join("ADHOC_RESP: result = %x\n", result);
  1539. if (!priv->in_progress_assoc_req) {
  1540. lbs_deb_join("ADHOC_RESP: no in-progress association "
  1541. "request\n");
  1542. ret = -1;
  1543. goto done;
  1544. }
  1545. bss = &priv->in_progress_assoc_req->bss;
  1546. /*
  1547. * Join result code 0 --> SUCCESS
  1548. */
  1549. if (result) {
  1550. lbs_deb_join("ADHOC_RESP: failed\n");
  1551. if (priv->connect_status == LBS_CONNECTED)
  1552. lbs_mac_event_disconnected(priv);
  1553. ret = -1;
  1554. goto done;
  1555. }
  1556. /*
  1557. * Now the join cmd should be successful
  1558. * If BSSID has changed use SSID to compare instead of BSSID
  1559. */
  1560. lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
  1561. escape_essid(bss->ssid, bss->ssid_len));
  1562. /* Send a Media Connected event, according to the Spec */
  1563. priv->connect_status = LBS_CONNECTED;
  1564. if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
  1565. /* Update the created network descriptor with the new BSSID */
  1566. memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
  1567. }
  1568. /* Set the BSSID from the joined/started descriptor */
  1569. memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
  1570. /* Set the new SSID to current SSID */
  1571. memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  1572. priv->curbssparams.ssid_len = bss->ssid_len;
  1573. netif_carrier_on(priv->dev);
  1574. if (!priv->tx_pending_len)
  1575. netif_wake_queue(priv->dev);
  1576. memset(&wrqu, 0, sizeof(wrqu));
  1577. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
  1578. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1579. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1580. lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
  1581. lbs_deb_join("ADHOC_RESP: channel = %d\n", priv->curbssparams.channel);
  1582. lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
  1583. print_mac(mac, padhocresult->bssid));
  1584. done:
  1585. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1586. return ret;
  1587. }
  1588. int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv)
  1589. {
  1590. lbs_deb_enter(LBS_DEB_JOIN);
  1591. lbs_mac_event_disconnected(priv);
  1592. lbs_deb_leave(LBS_DEB_JOIN);
  1593. return 0;
  1594. }