cfg.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227
  1. /*
  2. * Implement cfg80211 ("iw") support.
  3. *
  4. * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
  5. * Holger Schurig <hs4233@mail.mn-solutions.de>
  6. *
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/hardirq.h>
  10. #include <linux/sched.h>
  11. #include <linux/wait.h>
  12. #include <linux/slab.h>
  13. #include <linux/ieee80211.h>
  14. #include <net/cfg80211.h>
  15. #include <asm/unaligned.h>
  16. #include "decl.h"
  17. #include "cfg.h"
  18. #include "cmd.h"
  19. #include "mesh.h"
  20. #define CHAN2G(_channel, _freq, _flags) { \
  21. .band = IEEE80211_BAND_2GHZ, \
  22. .center_freq = (_freq), \
  23. .hw_value = (_channel), \
  24. .flags = (_flags), \
  25. .max_antenna_gain = 0, \
  26. .max_power = 30, \
  27. }
  28. static struct ieee80211_channel lbs_2ghz_channels[] = {
  29. CHAN2G(1, 2412, 0),
  30. CHAN2G(2, 2417, 0),
  31. CHAN2G(3, 2422, 0),
  32. CHAN2G(4, 2427, 0),
  33. CHAN2G(5, 2432, 0),
  34. CHAN2G(6, 2437, 0),
  35. CHAN2G(7, 2442, 0),
  36. CHAN2G(8, 2447, 0),
  37. CHAN2G(9, 2452, 0),
  38. CHAN2G(10, 2457, 0),
  39. CHAN2G(11, 2462, 0),
  40. CHAN2G(12, 2467, 0),
  41. CHAN2G(13, 2472, 0),
  42. CHAN2G(14, 2484, 0),
  43. };
  44. #define RATETAB_ENT(_rate, _hw_value, _flags) { \
  45. .bitrate = (_rate), \
  46. .hw_value = (_hw_value), \
  47. .flags = (_flags), \
  48. }
  49. /* Table 6 in section 3.2.1.1 */
  50. static struct ieee80211_rate lbs_rates[] = {
  51. RATETAB_ENT(10, 0, 0),
  52. RATETAB_ENT(20, 1, 0),
  53. RATETAB_ENT(55, 2, 0),
  54. RATETAB_ENT(110, 3, 0),
  55. RATETAB_ENT(60, 9, 0),
  56. RATETAB_ENT(90, 6, 0),
  57. RATETAB_ENT(120, 7, 0),
  58. RATETAB_ENT(180, 8, 0),
  59. RATETAB_ENT(240, 9, 0),
  60. RATETAB_ENT(360, 10, 0),
  61. RATETAB_ENT(480, 11, 0),
  62. RATETAB_ENT(540, 12, 0),
  63. };
  64. static struct ieee80211_supported_band lbs_band_2ghz = {
  65. .channels = lbs_2ghz_channels,
  66. .n_channels = ARRAY_SIZE(lbs_2ghz_channels),
  67. .bitrates = lbs_rates,
  68. .n_bitrates = ARRAY_SIZE(lbs_rates),
  69. };
  70. static const u32 cipher_suites[] = {
  71. WLAN_CIPHER_SUITE_WEP40,
  72. WLAN_CIPHER_SUITE_WEP104,
  73. WLAN_CIPHER_SUITE_TKIP,
  74. WLAN_CIPHER_SUITE_CCMP,
  75. };
  76. /* Time to stay on the channel */
  77. #define LBS_DWELL_PASSIVE 100
  78. #define LBS_DWELL_ACTIVE 40
  79. /***************************************************************************
  80. * Misc utility functions
  81. *
  82. * TLVs are Marvell specific. They are very similar to IEs, they have the
  83. * same structure: type, length, data*. The only difference: for IEs, the
  84. * type and length are u8, but for TLVs they're __le16.
  85. */
  86. /*
  87. * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
  88. * in the firmware spec
  89. */
  90. static u8 lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
  91. {
  92. int ret = -ENOTSUPP;
  93. switch (auth_type) {
  94. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  95. case NL80211_AUTHTYPE_SHARED_KEY:
  96. ret = auth_type;
  97. break;
  98. case NL80211_AUTHTYPE_AUTOMATIC:
  99. ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
  100. break;
  101. case NL80211_AUTHTYPE_NETWORK_EAP:
  102. ret = 0x80;
  103. break;
  104. default:
  105. /* silence compiler */
  106. break;
  107. }
  108. return ret;
  109. }
  110. /*
  111. * Various firmware commands need the list of supported rates, but with
  112. * the hight-bit set for basic rates
  113. */
  114. static int lbs_add_rates(u8 *rates)
  115. {
  116. size_t i;
  117. for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
  118. u8 rate = lbs_rates[i].bitrate / 5;
  119. if (rate == 0x02 || rate == 0x04 ||
  120. rate == 0x0b || rate == 0x16)
  121. rate |= 0x80;
  122. rates[i] = rate;
  123. }
  124. return ARRAY_SIZE(lbs_rates);
  125. }
  126. /***************************************************************************
  127. * TLV utility functions
  128. *
  129. * TLVs are Marvell specific. They are very similar to IEs, they have the
  130. * same structure: type, length, data*. The only difference: for IEs, the
  131. * type and length are u8, but for TLVs they're __le16.
  132. */
  133. /*
  134. * Add ssid TLV
  135. */
  136. #define LBS_MAX_SSID_TLV_SIZE \
  137. (sizeof(struct mrvl_ie_header) \
  138. + IEEE80211_MAX_SSID_LEN)
  139. static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
  140. {
  141. struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
  142. /*
  143. * TLV-ID SSID 00 00
  144. * length 06 00
  145. * ssid 4d 4e 54 45 53 54
  146. */
  147. ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
  148. ssid_tlv->header.len = cpu_to_le16(ssid_len);
  149. memcpy(ssid_tlv->ssid, ssid, ssid_len);
  150. return sizeof(ssid_tlv->header) + ssid_len;
  151. }
  152. /*
  153. * Add channel list TLV (section 8.4.2)
  154. *
  155. * Actual channel data comes from priv->wdev->wiphy->channels.
  156. */
  157. #define LBS_MAX_CHANNEL_LIST_TLV_SIZE \
  158. (sizeof(struct mrvl_ie_header) \
  159. + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
  160. static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
  161. int last_channel, int active_scan)
  162. {
  163. int chanscanparamsize = sizeof(struct chanscanparamset) *
  164. (last_channel - priv->scan_channel);
  165. struct mrvl_ie_header *header = (void *) tlv;
  166. /*
  167. * TLV-ID CHANLIST 01 01
  168. * length 0e 00
  169. * channel 00 01 00 00 00 64 00
  170. * radio type 00
  171. * channel 01
  172. * scan type 00
  173. * min scan time 00 00
  174. * max scan time 64 00
  175. * channel 2 00 02 00 00 00 64 00
  176. *
  177. */
  178. header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
  179. header->len = cpu_to_le16(chanscanparamsize);
  180. tlv += sizeof(struct mrvl_ie_header);
  181. /* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
  182. last_channel); */
  183. memset(tlv, 0, chanscanparamsize);
  184. while (priv->scan_channel < last_channel) {
  185. struct chanscanparamset *param = (void *) tlv;
  186. param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
  187. param->channumber =
  188. priv->scan_req->channels[priv->scan_channel]->hw_value;
  189. if (active_scan) {
  190. param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
  191. } else {
  192. param->chanscanmode.passivescan = 1;
  193. param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
  194. }
  195. tlv += sizeof(struct chanscanparamset);
  196. priv->scan_channel++;
  197. }
  198. return sizeof(struct mrvl_ie_header) + chanscanparamsize;
  199. }
  200. /*
  201. * Add rates TLV
  202. *
  203. * The rates are in lbs_bg_rates[], but for the 802.11b
  204. * rates the high bit is set. We add this TLV only because
  205. * there's a firmware which otherwise doesn't report all
  206. * APs in range.
  207. */
  208. #define LBS_MAX_RATES_TLV_SIZE \
  209. (sizeof(struct mrvl_ie_header) \
  210. + (ARRAY_SIZE(lbs_rates)))
  211. /* Adds a TLV with all rates the hardware supports */
  212. static int lbs_add_supported_rates_tlv(u8 *tlv)
  213. {
  214. size_t i;
  215. struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
  216. /*
  217. * TLV-ID RATES 01 00
  218. * length 0e 00
  219. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
  220. */
  221. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  222. tlv += sizeof(rate_tlv->header);
  223. i = lbs_add_rates(tlv);
  224. tlv += i;
  225. rate_tlv->header.len = cpu_to_le16(i);
  226. return sizeof(rate_tlv->header) + i;
  227. }
  228. /* Add common rates from a TLV and return the new end of the TLV */
  229. static u8 *
  230. add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
  231. {
  232. int hw, ap, ap_max = ie[1];
  233. u8 hw_rate;
  234. /* Advance past IE header */
  235. ie += 2;
  236. lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
  237. for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
  238. hw_rate = lbs_rates[hw].bitrate / 5;
  239. for (ap = 0; ap < ap_max; ap++) {
  240. if (hw_rate == (ie[ap] & 0x7f)) {
  241. *tlv++ = ie[ap];
  242. *nrates = *nrates + 1;
  243. }
  244. }
  245. }
  246. return tlv;
  247. }
  248. /*
  249. * Adds a TLV with all rates the hardware *and* BSS supports.
  250. */
  251. static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
  252. {
  253. struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
  254. const u8 *rates_eid, *ext_rates_eid;
  255. int n = 0;
  256. rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  257. ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  258. /*
  259. * 01 00 TLV_TYPE_RATES
  260. * 04 00 len
  261. * 82 84 8b 96 rates
  262. */
  263. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  264. tlv += sizeof(rate_tlv->header);
  265. /* Add basic rates */
  266. if (rates_eid) {
  267. tlv = add_ie_rates(tlv, rates_eid, &n);
  268. /* Add extended rates, if any */
  269. if (ext_rates_eid)
  270. tlv = add_ie_rates(tlv, ext_rates_eid, &n);
  271. } else {
  272. lbs_deb_assoc("assoc: bss had no basic rate IE\n");
  273. /* Fallback: add basic 802.11b rates */
  274. *tlv++ = 0x82;
  275. *tlv++ = 0x84;
  276. *tlv++ = 0x8b;
  277. *tlv++ = 0x96;
  278. n = 4;
  279. }
  280. rate_tlv->header.len = cpu_to_le16(n);
  281. return sizeof(rate_tlv->header) + n;
  282. }
  283. /*
  284. * Add auth type TLV.
  285. *
  286. * This is only needed for newer firmware (V9 and up).
  287. */
  288. #define LBS_MAX_AUTH_TYPE_TLV_SIZE \
  289. sizeof(struct mrvl_ie_auth_type)
  290. static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
  291. {
  292. struct mrvl_ie_auth_type *auth = (void *) tlv;
  293. /*
  294. * 1f 01 TLV_TYPE_AUTH_TYPE
  295. * 01 00 len
  296. * 01 auth type
  297. */
  298. auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
  299. auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
  300. auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
  301. return sizeof(*auth);
  302. }
  303. /*
  304. * Add channel (phy ds) TLV
  305. */
  306. #define LBS_MAX_CHANNEL_TLV_SIZE \
  307. sizeof(struct mrvl_ie_header)
  308. static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
  309. {
  310. struct mrvl_ie_ds_param_set *ds = (void *) tlv;
  311. /*
  312. * 03 00 TLV_TYPE_PHY_DS
  313. * 01 00 len
  314. * 06 channel
  315. */
  316. ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  317. ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
  318. ds->channel = channel;
  319. return sizeof(*ds);
  320. }
  321. /*
  322. * Add (empty) CF param TLV of the form:
  323. */
  324. #define LBS_MAX_CF_PARAM_TLV_SIZE \
  325. sizeof(struct mrvl_ie_header)
  326. static int lbs_add_cf_param_tlv(u8 *tlv)
  327. {
  328. struct mrvl_ie_cf_param_set *cf = (void *)tlv;
  329. /*
  330. * 04 00 TLV_TYPE_CF
  331. * 06 00 len
  332. * 00 cfpcnt
  333. * 00 cfpperiod
  334. * 00 00 cfpmaxduration
  335. * 00 00 cfpdurationremaining
  336. */
  337. cf->header.type = cpu_to_le16(TLV_TYPE_CF);
  338. cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
  339. return sizeof(*cf);
  340. }
  341. /*
  342. * Add WPA TLV
  343. */
  344. #define LBS_MAX_WPA_TLV_SIZE \
  345. (sizeof(struct mrvl_ie_header) \
  346. + 128 /* TODO: I guessed the size */)
  347. static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
  348. {
  349. size_t tlv_len;
  350. /*
  351. * We need just convert an IE to an TLV. IEs use u8 for the header,
  352. * u8 type
  353. * u8 len
  354. * u8[] data
  355. * but TLVs use __le16 instead:
  356. * __le16 type
  357. * __le16 len
  358. * u8[] data
  359. */
  360. *tlv++ = *ie++;
  361. *tlv++ = 0;
  362. tlv_len = *tlv++ = *ie++;
  363. *tlv++ = 0;
  364. while (tlv_len--)
  365. *tlv++ = *ie++;
  366. /* the TLV is two bytes larger than the IE */
  367. return ie_len + 2;
  368. }
  369. /*
  370. * Set Channel
  371. */
  372. static int lbs_cfg_set_channel(struct wiphy *wiphy,
  373. struct net_device *netdev,
  374. struct ieee80211_channel *channel,
  375. enum nl80211_channel_type channel_type)
  376. {
  377. struct lbs_private *priv = wiphy_priv(wiphy);
  378. int ret = -ENOTSUPP;
  379. lbs_deb_enter_args(LBS_DEB_CFG80211, "iface %s freq %d, type %d",
  380. netdev_name(netdev), channel->center_freq, channel_type);
  381. if (channel_type != NL80211_CHAN_NO_HT)
  382. goto out;
  383. if (netdev == priv->mesh_dev)
  384. ret = lbs_mesh_set_channel(priv, channel->hw_value);
  385. else
  386. ret = lbs_set_channel(priv, channel->hw_value);
  387. out:
  388. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  389. return ret;
  390. }
  391. /*
  392. * Scanning
  393. */
  394. /*
  395. * When scanning, the firmware doesn't send a nul packet with the power-safe
  396. * bit to the AP. So we cannot stay away from our current channel too long,
  397. * otherwise we loose data. So take a "nap" while scanning every other
  398. * while.
  399. */
  400. #define LBS_SCAN_BEFORE_NAP 4
  401. /*
  402. * When the firmware reports back a scan-result, it gives us an "u8 rssi",
  403. * which isn't really an RSSI, as it becomes larger when moving away from
  404. * the AP. Anyway, we need to convert that into mBm.
  405. */
  406. #define LBS_SCAN_RSSI_TO_MBM(rssi) \
  407. ((-(int)rssi + 3)*100)
  408. static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
  409. struct cmd_header *resp)
  410. {
  411. struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
  412. int bsssize;
  413. const u8 *pos;
  414. const u8 *tsfdesc;
  415. int tsfsize;
  416. int i;
  417. int ret = -EILSEQ;
  418. lbs_deb_enter(LBS_DEB_CFG80211);
  419. bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
  420. lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
  421. scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
  422. if (scanresp->nr_sets == 0) {
  423. ret = 0;
  424. goto done;
  425. }
  426. /*
  427. * The general layout of the scan response is described in chapter
  428. * 5.7.1. Basically we have a common part, then any number of BSS
  429. * descriptor sections. Finally we have section with the same number
  430. * of TSFs.
  431. *
  432. * cmd_ds_802_11_scan_rsp
  433. * cmd_header
  434. * pos_size
  435. * nr_sets
  436. * bssdesc 1
  437. * bssid
  438. * rssi
  439. * timestamp
  440. * intvl
  441. * capa
  442. * IEs
  443. * bssdesc 2
  444. * bssdesc n
  445. * MrvlIEtypes_TsfFimestamp_t
  446. * TSF for BSS 1
  447. * TSF for BSS 2
  448. * TSF for BSS n
  449. */
  450. pos = scanresp->bssdesc_and_tlvbuffer;
  451. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
  452. scanresp->bssdescriptsize);
  453. tsfdesc = pos + bsssize;
  454. tsfsize = 4 + 8 * scanresp->nr_sets;
  455. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
  456. /* Validity check: we expect a Marvell-Local TLV */
  457. i = get_unaligned_le16(tsfdesc);
  458. tsfdesc += 2;
  459. if (i != TLV_TYPE_TSFTIMESTAMP) {
  460. lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
  461. goto done;
  462. }
  463. /*
  464. * Validity check: the TLV holds TSF values with 8 bytes each, so
  465. * the size in the TLV must match the nr_sets value
  466. */
  467. i = get_unaligned_le16(tsfdesc);
  468. tsfdesc += 2;
  469. if (i / 8 != scanresp->nr_sets) {
  470. lbs_deb_scan("scan response: invalid number of TSF timestamp "
  471. "sets (expected %d got %d)\n", scanresp->nr_sets,
  472. i / 8);
  473. goto done;
  474. }
  475. for (i = 0; i < scanresp->nr_sets; i++) {
  476. const u8 *bssid;
  477. const u8 *ie;
  478. int left;
  479. int ielen;
  480. int rssi;
  481. u16 intvl;
  482. u16 capa;
  483. int chan_no = -1;
  484. const u8 *ssid = NULL;
  485. u8 ssid_len = 0;
  486. DECLARE_SSID_BUF(ssid_buf);
  487. int len = get_unaligned_le16(pos);
  488. pos += 2;
  489. /* BSSID */
  490. bssid = pos;
  491. pos += ETH_ALEN;
  492. /* RSSI */
  493. rssi = *pos++;
  494. /* Packet time stamp */
  495. pos += 8;
  496. /* Beacon interval */
  497. intvl = get_unaligned_le16(pos);
  498. pos += 2;
  499. /* Capabilities */
  500. capa = get_unaligned_le16(pos);
  501. pos += 2;
  502. /* To find out the channel, we must parse the IEs */
  503. ie = pos;
  504. /*
  505. * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
  506. * interval, capabilities
  507. */
  508. ielen = left = len - (6 + 1 + 8 + 2 + 2);
  509. while (left >= 2) {
  510. u8 id, elen;
  511. id = *pos++;
  512. elen = *pos++;
  513. left -= 2;
  514. if (elen > left || elen == 0) {
  515. lbs_deb_scan("scan response: invalid IE fmt\n");
  516. goto done;
  517. }
  518. if (id == WLAN_EID_DS_PARAMS)
  519. chan_no = *pos;
  520. if (id == WLAN_EID_SSID) {
  521. ssid = pos;
  522. ssid_len = elen;
  523. }
  524. left -= elen;
  525. pos += elen;
  526. }
  527. /* No channel, no luck */
  528. if (chan_no != -1) {
  529. struct wiphy *wiphy = priv->wdev->wiphy;
  530. int freq = ieee80211_channel_to_frequency(chan_no,
  531. IEEE80211_BAND_2GHZ);
  532. struct ieee80211_channel *channel =
  533. ieee80211_get_channel(wiphy, freq);
  534. lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
  535. "%d dBm\n",
  536. bssid, capa, chan_no,
  537. print_ssid(ssid_buf, ssid, ssid_len),
  538. LBS_SCAN_RSSI_TO_MBM(rssi)/100);
  539. if (channel &&
  540. !(channel->flags & IEEE80211_CHAN_DISABLED))
  541. cfg80211_inform_bss(wiphy, channel,
  542. bssid, le64_to_cpu(*(__le64 *)tsfdesc),
  543. capa, intvl, ie, ielen,
  544. LBS_SCAN_RSSI_TO_MBM(rssi),
  545. GFP_KERNEL);
  546. } else
  547. lbs_deb_scan("scan response: missing BSS channel IE\n");
  548. tsfdesc += 8;
  549. }
  550. ret = 0;
  551. done:
  552. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  553. return ret;
  554. }
  555. /*
  556. * Our scan command contains a TLV, consting of a SSID TLV, a channel list
  557. * TLV and a rates TLV. Determine the maximum size of them:
  558. */
  559. #define LBS_SCAN_MAX_CMD_SIZE \
  560. (sizeof(struct cmd_ds_802_11_scan) \
  561. + LBS_MAX_SSID_TLV_SIZE \
  562. + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
  563. + LBS_MAX_RATES_TLV_SIZE)
  564. /*
  565. * Assumes priv->scan_req is initialized and valid
  566. * Assumes priv->scan_channel is initialized
  567. */
  568. static void lbs_scan_worker(struct work_struct *work)
  569. {
  570. struct lbs_private *priv =
  571. container_of(work, struct lbs_private, scan_work.work);
  572. struct cmd_ds_802_11_scan *scan_cmd;
  573. u8 *tlv; /* pointer into our current, growing TLV storage area */
  574. int last_channel;
  575. int running, carrier;
  576. lbs_deb_enter(LBS_DEB_SCAN);
  577. scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
  578. if (scan_cmd == NULL)
  579. goto out_no_scan_cmd;
  580. /* prepare fixed part of scan command */
  581. scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
  582. /* stop network while we're away from our main channel */
  583. running = !netif_queue_stopped(priv->dev);
  584. carrier = netif_carrier_ok(priv->dev);
  585. if (running)
  586. netif_stop_queue(priv->dev);
  587. if (carrier)
  588. netif_carrier_off(priv->dev);
  589. /* prepare fixed part of scan command */
  590. tlv = scan_cmd->tlvbuffer;
  591. /* add SSID TLV */
  592. if (priv->scan_req->n_ssids && priv->scan_req->ssids[0].ssid_len > 0)
  593. tlv += lbs_add_ssid_tlv(tlv,
  594. priv->scan_req->ssids[0].ssid,
  595. priv->scan_req->ssids[0].ssid_len);
  596. /* add channel TLVs */
  597. last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
  598. if (last_channel > priv->scan_req->n_channels)
  599. last_channel = priv->scan_req->n_channels;
  600. tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
  601. priv->scan_req->n_ssids);
  602. /* add rates TLV */
  603. tlv += lbs_add_supported_rates_tlv(tlv);
  604. if (priv->scan_channel < priv->scan_req->n_channels) {
  605. cancel_delayed_work(&priv->scan_work);
  606. if (netif_running(priv->dev))
  607. queue_delayed_work(priv->work_thread, &priv->scan_work,
  608. msecs_to_jiffies(300));
  609. }
  610. /* This is the final data we are about to send */
  611. scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
  612. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
  613. sizeof(*scan_cmd));
  614. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
  615. tlv - scan_cmd->tlvbuffer);
  616. __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
  617. le16_to_cpu(scan_cmd->hdr.size),
  618. lbs_ret_scan, 0);
  619. if (priv->scan_channel >= priv->scan_req->n_channels)
  620. /* Mark scan done */
  621. lbs_scan_done(priv);
  622. /* Restart network */
  623. if (carrier)
  624. netif_carrier_on(priv->dev);
  625. if (running && !priv->tx_pending_len)
  626. netif_wake_queue(priv->dev);
  627. kfree(scan_cmd);
  628. /* Wake up anything waiting on scan completion */
  629. if (priv->scan_req == NULL) {
  630. lbs_deb_scan("scan: waking up waiters\n");
  631. wake_up_all(&priv->scan_q);
  632. }
  633. out_no_scan_cmd:
  634. lbs_deb_leave(LBS_DEB_SCAN);
  635. }
  636. static void _internal_start_scan(struct lbs_private *priv, bool internal,
  637. struct cfg80211_scan_request *request)
  638. {
  639. lbs_deb_enter(LBS_DEB_CFG80211);
  640. lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
  641. request->n_ssids, request->n_channels, request->ie_len);
  642. priv->scan_channel = 0;
  643. queue_delayed_work(priv->work_thread, &priv->scan_work,
  644. msecs_to_jiffies(50));
  645. priv->scan_req = request;
  646. priv->internal_scan = internal;
  647. lbs_deb_leave(LBS_DEB_CFG80211);
  648. }
  649. /*
  650. * Clean up priv->scan_req. Should be used to handle the allocation details.
  651. */
  652. void lbs_scan_done(struct lbs_private *priv)
  653. {
  654. WARN_ON(!priv->scan_req);
  655. if (priv->internal_scan)
  656. kfree(priv->scan_req);
  657. else
  658. cfg80211_scan_done(priv->scan_req, false);
  659. priv->scan_req = NULL;
  660. }
  661. static int lbs_cfg_scan(struct wiphy *wiphy,
  662. struct net_device *dev,
  663. struct cfg80211_scan_request *request)
  664. {
  665. struct lbs_private *priv = wiphy_priv(wiphy);
  666. int ret = 0;
  667. lbs_deb_enter(LBS_DEB_CFG80211);
  668. if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
  669. /* old scan request not yet processed */
  670. ret = -EAGAIN;
  671. goto out;
  672. }
  673. _internal_start_scan(priv, false, request);
  674. if (priv->surpriseremoved)
  675. ret = -EIO;
  676. out:
  677. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  678. return ret;
  679. }
  680. /*
  681. * Events
  682. */
  683. void lbs_send_disconnect_notification(struct lbs_private *priv)
  684. {
  685. lbs_deb_enter(LBS_DEB_CFG80211);
  686. cfg80211_disconnected(priv->dev,
  687. 0,
  688. NULL, 0,
  689. GFP_KERNEL);
  690. lbs_deb_leave(LBS_DEB_CFG80211);
  691. }
  692. void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
  693. {
  694. lbs_deb_enter(LBS_DEB_CFG80211);
  695. cfg80211_michael_mic_failure(priv->dev,
  696. priv->assoc_bss,
  697. event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
  698. NL80211_KEYTYPE_GROUP :
  699. NL80211_KEYTYPE_PAIRWISE,
  700. -1,
  701. NULL,
  702. GFP_KERNEL);
  703. lbs_deb_leave(LBS_DEB_CFG80211);
  704. }
  705. /*
  706. * Connect/disconnect
  707. */
  708. /*
  709. * This removes all WEP keys
  710. */
  711. static int lbs_remove_wep_keys(struct lbs_private *priv)
  712. {
  713. struct cmd_ds_802_11_set_wep cmd;
  714. int ret;
  715. lbs_deb_enter(LBS_DEB_CFG80211);
  716. memset(&cmd, 0, sizeof(cmd));
  717. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  718. cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
  719. cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
  720. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  721. lbs_deb_leave(LBS_DEB_CFG80211);
  722. return ret;
  723. }
  724. /*
  725. * Set WEP keys
  726. */
  727. static int lbs_set_wep_keys(struct lbs_private *priv)
  728. {
  729. struct cmd_ds_802_11_set_wep cmd;
  730. int i;
  731. int ret;
  732. lbs_deb_enter(LBS_DEB_CFG80211);
  733. /*
  734. * command 13 00
  735. * size 50 00
  736. * sequence xx xx
  737. * result 00 00
  738. * action 02 00 ACT_ADD
  739. * transmit key 00 00
  740. * type for key 1 01 WEP40
  741. * type for key 2 00
  742. * type for key 3 00
  743. * type for key 4 00
  744. * key 1 39 39 39 39 39 00 00 00
  745. * 00 00 00 00 00 00 00 00
  746. * key 2 00 00 00 00 00 00 00 00
  747. * 00 00 00 00 00 00 00 00
  748. * key 3 00 00 00 00 00 00 00 00
  749. * 00 00 00 00 00 00 00 00
  750. * key 4 00 00 00 00 00 00 00 00
  751. */
  752. if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
  753. priv->wep_key_len[2] || priv->wep_key_len[3]) {
  754. /* Only set wep keys if we have at least one of them */
  755. memset(&cmd, 0, sizeof(cmd));
  756. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  757. cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
  758. cmd.action = cpu_to_le16(CMD_ACT_ADD);
  759. for (i = 0; i < 4; i++) {
  760. switch (priv->wep_key_len[i]) {
  761. case WLAN_KEY_LEN_WEP40:
  762. cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
  763. break;
  764. case WLAN_KEY_LEN_WEP104:
  765. cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
  766. break;
  767. default:
  768. cmd.keytype[i] = 0;
  769. break;
  770. }
  771. memcpy(cmd.keymaterial[i], priv->wep_key[i],
  772. priv->wep_key_len[i]);
  773. }
  774. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  775. } else {
  776. /* Otherwise remove all wep keys */
  777. ret = lbs_remove_wep_keys(priv);
  778. }
  779. lbs_deb_leave(LBS_DEB_CFG80211);
  780. return ret;
  781. }
  782. /*
  783. * Enable/Disable RSN status
  784. */
  785. static int lbs_enable_rsn(struct lbs_private *priv, int enable)
  786. {
  787. struct cmd_ds_802_11_enable_rsn cmd;
  788. int ret;
  789. lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", enable);
  790. /*
  791. * cmd 2f 00
  792. * size 0c 00
  793. * sequence xx xx
  794. * result 00 00
  795. * action 01 00 ACT_SET
  796. * enable 01 00
  797. */
  798. memset(&cmd, 0, sizeof(cmd));
  799. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  800. cmd.action = cpu_to_le16(CMD_ACT_SET);
  801. cmd.enable = cpu_to_le16(enable);
  802. ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
  803. lbs_deb_leave(LBS_DEB_CFG80211);
  804. return ret;
  805. }
  806. /*
  807. * Set WPA/WPA key material
  808. */
  809. /*
  810. * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
  811. * get rid of WEXT, this should go into host.h
  812. */
  813. struct cmd_key_material {
  814. struct cmd_header hdr;
  815. __le16 action;
  816. struct MrvlIEtype_keyParamSet param;
  817. } __packed;
  818. static int lbs_set_key_material(struct lbs_private *priv,
  819. int key_type,
  820. int key_info,
  821. u8 *key, u16 key_len)
  822. {
  823. struct cmd_key_material cmd;
  824. int ret;
  825. lbs_deb_enter(LBS_DEB_CFG80211);
  826. /*
  827. * Example for WPA (TKIP):
  828. *
  829. * cmd 5e 00
  830. * size 34 00
  831. * sequence xx xx
  832. * result 00 00
  833. * action 01 00
  834. * TLV type 00 01 key param
  835. * length 00 26
  836. * key type 01 00 TKIP
  837. * key info 06 00 UNICAST | ENABLED
  838. * key len 20 00
  839. * key 32 bytes
  840. */
  841. memset(&cmd, 0, sizeof(cmd));
  842. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  843. cmd.action = cpu_to_le16(CMD_ACT_SET);
  844. cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
  845. cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
  846. cmd.param.keytypeid = cpu_to_le16(key_type);
  847. cmd.param.keyinfo = cpu_to_le16(key_info);
  848. cmd.param.keylen = cpu_to_le16(key_len);
  849. if (key && key_len)
  850. memcpy(cmd.param.key, key, key_len);
  851. ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
  852. lbs_deb_leave(LBS_DEB_CFG80211);
  853. return ret;
  854. }
  855. /*
  856. * Sets the auth type (open, shared, etc) in the firmware. That
  857. * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
  858. * command doesn't send an authentication frame at all, it just
  859. * stores the auth_type.
  860. */
  861. static int lbs_set_authtype(struct lbs_private *priv,
  862. struct cfg80211_connect_params *sme)
  863. {
  864. struct cmd_ds_802_11_authenticate cmd;
  865. int ret;
  866. lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", sme->auth_type);
  867. /*
  868. * cmd 11 00
  869. * size 19 00
  870. * sequence xx xx
  871. * result 00 00
  872. * BSS id 00 13 19 80 da 30
  873. * auth type 00
  874. * reserved 00 00 00 00 00 00 00 00 00 00
  875. */
  876. memset(&cmd, 0, sizeof(cmd));
  877. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  878. if (sme->bssid)
  879. memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
  880. /* convert auth_type */
  881. ret = lbs_auth_to_authtype(sme->auth_type);
  882. if (ret < 0)
  883. goto done;
  884. cmd.authtype = ret;
  885. ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
  886. done:
  887. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  888. return ret;
  889. }
  890. /*
  891. * Create association request
  892. */
  893. #define LBS_ASSOC_MAX_CMD_SIZE \
  894. (sizeof(struct cmd_ds_802_11_associate) \
  895. - 512 /* cmd_ds_802_11_associate.iebuf */ \
  896. + LBS_MAX_SSID_TLV_SIZE \
  897. + LBS_MAX_CHANNEL_TLV_SIZE \
  898. + LBS_MAX_CF_PARAM_TLV_SIZE \
  899. + LBS_MAX_AUTH_TYPE_TLV_SIZE \
  900. + LBS_MAX_WPA_TLV_SIZE)
  901. static int lbs_associate(struct lbs_private *priv,
  902. struct cfg80211_bss *bss,
  903. struct cfg80211_connect_params *sme)
  904. {
  905. struct cmd_ds_802_11_associate_response *resp;
  906. struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
  907. GFP_KERNEL);
  908. const u8 *ssid_eid;
  909. size_t len, resp_ie_len;
  910. int status;
  911. int ret;
  912. u8 *pos = &(cmd->iebuf[0]);
  913. u8 *tmp;
  914. lbs_deb_enter(LBS_DEB_CFG80211);
  915. if (!cmd) {
  916. ret = -ENOMEM;
  917. goto done;
  918. }
  919. /*
  920. * cmd 50 00
  921. * length 34 00
  922. * sequence xx xx
  923. * result 00 00
  924. * BSS id 00 13 19 80 da 30
  925. * capabilities 11 00
  926. * listen interval 0a 00
  927. * beacon interval 00 00
  928. * DTIM period 00
  929. * TLVs xx (up to 512 bytes)
  930. */
  931. cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  932. /* Fill in static fields */
  933. memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
  934. cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  935. cmd->capability = cpu_to_le16(bss->capability);
  936. /* add SSID TLV */
  937. ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
  938. if (ssid_eid)
  939. pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
  940. else
  941. lbs_deb_assoc("no SSID\n");
  942. /* add DS param TLV */
  943. if (bss->channel)
  944. pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
  945. else
  946. lbs_deb_assoc("no channel\n");
  947. /* add (empty) CF param TLV */
  948. pos += lbs_add_cf_param_tlv(pos);
  949. /* add rates TLV */
  950. tmp = pos + 4; /* skip Marvell IE header */
  951. pos += lbs_add_common_rates_tlv(pos, bss);
  952. lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
  953. /* add auth type TLV */
  954. if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
  955. pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
  956. /* add WPA/WPA2 TLV */
  957. if (sme->ie && sme->ie_len)
  958. pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
  959. len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
  960. (u16)(pos - (u8 *) &cmd->iebuf);
  961. cmd->hdr.size = cpu_to_le16(len);
  962. lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
  963. le16_to_cpu(cmd->hdr.size));
  964. /* store for later use */
  965. memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
  966. ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
  967. if (ret)
  968. goto done;
  969. /* generate connect message to cfg80211 */
  970. resp = (void *) cmd; /* recast for easier field access */
  971. status = le16_to_cpu(resp->statuscode);
  972. /* Older FW versions map the IEEE 802.11 Status Code in the association
  973. * response to the following values returned in resp->statuscode:
  974. *
  975. * IEEE Status Code Marvell Status Code
  976. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  977. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  978. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  979. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  980. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  981. * others -> 0x0003 ASSOC_RESULT_REFUSED
  982. *
  983. * Other response codes:
  984. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  985. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  986. * association response from the AP)
  987. */
  988. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
  989. switch (status) {
  990. case 0:
  991. break;
  992. case 1:
  993. lbs_deb_assoc("invalid association parameters\n");
  994. status = WLAN_STATUS_CAPS_UNSUPPORTED;
  995. break;
  996. case 2:
  997. lbs_deb_assoc("timer expired while waiting for AP\n");
  998. status = WLAN_STATUS_AUTH_TIMEOUT;
  999. break;
  1000. case 3:
  1001. lbs_deb_assoc("association refused by AP\n");
  1002. status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
  1003. break;
  1004. case 4:
  1005. lbs_deb_assoc("authentication refused by AP\n");
  1006. status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  1007. break;
  1008. default:
  1009. lbs_deb_assoc("association failure %d\n", status);
  1010. /* v5 OLPC firmware does return the AP status code if
  1011. * it's not one of the values above. Let that through.
  1012. */
  1013. break;
  1014. }
  1015. }
  1016. lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
  1017. "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
  1018. le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
  1019. resp_ie_len = le16_to_cpu(resp->hdr.size)
  1020. - sizeof(resp->hdr)
  1021. - 6;
  1022. cfg80211_connect_result(priv->dev,
  1023. priv->assoc_bss,
  1024. sme->ie, sme->ie_len,
  1025. resp->iebuf, resp_ie_len,
  1026. status,
  1027. GFP_KERNEL);
  1028. if (status == 0) {
  1029. /* TODO: get rid of priv->connect_status */
  1030. priv->connect_status = LBS_CONNECTED;
  1031. netif_carrier_on(priv->dev);
  1032. if (!priv->tx_pending_len)
  1033. netif_tx_wake_all_queues(priv->dev);
  1034. }
  1035. done:
  1036. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1037. return ret;
  1038. }
  1039. static struct cfg80211_scan_request *
  1040. _new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
  1041. {
  1042. struct cfg80211_scan_request *creq = NULL;
  1043. int i, n_channels = 0;
  1044. enum ieee80211_band band;
  1045. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1046. if (wiphy->bands[band])
  1047. n_channels += wiphy->bands[band]->n_channels;
  1048. }
  1049. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  1050. n_channels * sizeof(void *),
  1051. GFP_ATOMIC);
  1052. if (!creq)
  1053. return NULL;
  1054. /* SSIDs come after channels */
  1055. creq->ssids = (void *)&creq->channels[n_channels];
  1056. creq->n_channels = n_channels;
  1057. creq->n_ssids = 1;
  1058. /* Scan all available channels */
  1059. i = 0;
  1060. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1061. int j;
  1062. if (!wiphy->bands[band])
  1063. continue;
  1064. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  1065. /* ignore disabled channels */
  1066. if (wiphy->bands[band]->channels[j].flags &
  1067. IEEE80211_CHAN_DISABLED)
  1068. continue;
  1069. creq->channels[i] = &wiphy->bands[band]->channels[j];
  1070. i++;
  1071. }
  1072. }
  1073. if (i) {
  1074. /* Set real number of channels specified in creq->channels[] */
  1075. creq->n_channels = i;
  1076. /* Scan for the SSID we're going to connect to */
  1077. memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
  1078. creq->ssids[0].ssid_len = sme->ssid_len;
  1079. } else {
  1080. /* No channels found... */
  1081. kfree(creq);
  1082. creq = NULL;
  1083. }
  1084. return creq;
  1085. }
  1086. static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
  1087. struct cfg80211_connect_params *sme)
  1088. {
  1089. struct lbs_private *priv = wiphy_priv(wiphy);
  1090. struct cfg80211_bss *bss = NULL;
  1091. int ret = 0;
  1092. u8 preamble = RADIO_PREAMBLE_SHORT;
  1093. if (dev == priv->mesh_dev)
  1094. return -EOPNOTSUPP;
  1095. lbs_deb_enter(LBS_DEB_CFG80211);
  1096. if (!sme->bssid) {
  1097. struct cfg80211_scan_request *creq;
  1098. /*
  1099. * Scan for the requested network after waiting for existing
  1100. * scans to finish.
  1101. */
  1102. lbs_deb_assoc("assoc: waiting for existing scans\n");
  1103. wait_event_interruptible_timeout(priv->scan_q,
  1104. (priv->scan_req == NULL),
  1105. (15 * HZ));
  1106. creq = _new_connect_scan_req(wiphy, sme);
  1107. if (!creq) {
  1108. ret = -EINVAL;
  1109. goto done;
  1110. }
  1111. lbs_deb_assoc("assoc: scanning for compatible AP\n");
  1112. _internal_start_scan(priv, true, creq);
  1113. lbs_deb_assoc("assoc: waiting for scan to complete\n");
  1114. wait_event_interruptible_timeout(priv->scan_q,
  1115. (priv->scan_req == NULL),
  1116. (15 * HZ));
  1117. lbs_deb_assoc("assoc: scanning competed\n");
  1118. }
  1119. /* Find the BSS we want using available scan results */
  1120. bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
  1121. sme->ssid, sme->ssid_len,
  1122. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  1123. if (!bss) {
  1124. wiphy_err(wiphy, "assoc: bss %pM not in scan results\n",
  1125. sme->bssid);
  1126. ret = -ENOENT;
  1127. goto done;
  1128. }
  1129. lbs_deb_assoc("trying %pM\n", bss->bssid);
  1130. lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
  1131. sme->crypto.cipher_group,
  1132. sme->key_idx, sme->key_len);
  1133. /* As this is a new connection, clear locally stored WEP keys */
  1134. priv->wep_tx_key = 0;
  1135. memset(priv->wep_key, 0, sizeof(priv->wep_key));
  1136. memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
  1137. /* set/remove WEP keys */
  1138. switch (sme->crypto.cipher_group) {
  1139. case WLAN_CIPHER_SUITE_WEP40:
  1140. case WLAN_CIPHER_SUITE_WEP104:
  1141. /* Store provided WEP keys in priv-> */
  1142. priv->wep_tx_key = sme->key_idx;
  1143. priv->wep_key_len[sme->key_idx] = sme->key_len;
  1144. memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
  1145. /* Set WEP keys and WEP mode */
  1146. lbs_set_wep_keys(priv);
  1147. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  1148. lbs_set_mac_control(priv);
  1149. /* No RSN mode for WEP */
  1150. lbs_enable_rsn(priv, 0);
  1151. break;
  1152. case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
  1153. /*
  1154. * If we don't have no WEP, no WPA and no WPA2,
  1155. * we remove all keys like in the WPA/WPA2 setup,
  1156. * we just don't set RSN.
  1157. *
  1158. * Therefore: fall-through
  1159. */
  1160. case WLAN_CIPHER_SUITE_TKIP:
  1161. case WLAN_CIPHER_SUITE_CCMP:
  1162. /* Remove WEP keys and WEP mode */
  1163. lbs_remove_wep_keys(priv);
  1164. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  1165. lbs_set_mac_control(priv);
  1166. /* clear the WPA/WPA2 keys */
  1167. lbs_set_key_material(priv,
  1168. KEY_TYPE_ID_WEP, /* doesn't matter */
  1169. KEY_INFO_WPA_UNICAST,
  1170. NULL, 0);
  1171. lbs_set_key_material(priv,
  1172. KEY_TYPE_ID_WEP, /* doesn't matter */
  1173. KEY_INFO_WPA_MCAST,
  1174. NULL, 0);
  1175. /* RSN mode for WPA/WPA2 */
  1176. lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
  1177. break;
  1178. default:
  1179. wiphy_err(wiphy, "unsupported cipher group 0x%x\n",
  1180. sme->crypto.cipher_group);
  1181. ret = -ENOTSUPP;
  1182. goto done;
  1183. }
  1184. lbs_set_authtype(priv, sme);
  1185. lbs_set_radio(priv, preamble, 1);
  1186. /* Do the actual association */
  1187. ret = lbs_associate(priv, bss, sme);
  1188. done:
  1189. if (bss)
  1190. cfg80211_put_bss(bss);
  1191. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1192. return ret;
  1193. }
  1194. int lbs_disconnect(struct lbs_private *priv, u16 reason)
  1195. {
  1196. struct cmd_ds_802_11_deauthenticate cmd;
  1197. int ret;
  1198. memset(&cmd, 0, sizeof(cmd));
  1199. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1200. /* Mildly ugly to use a locally store my own BSSID ... */
  1201. memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
  1202. cmd.reasoncode = cpu_to_le16(reason);
  1203. ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
  1204. if (ret)
  1205. return ret;
  1206. cfg80211_disconnected(priv->dev,
  1207. reason,
  1208. NULL, 0,
  1209. GFP_KERNEL);
  1210. priv->connect_status = LBS_DISCONNECTED;
  1211. return 0;
  1212. }
  1213. static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1214. u16 reason_code)
  1215. {
  1216. struct lbs_private *priv = wiphy_priv(wiphy);
  1217. if (dev == priv->mesh_dev)
  1218. return -EOPNOTSUPP;
  1219. lbs_deb_enter_args(LBS_DEB_CFG80211, "reason_code %d", reason_code);
  1220. /* store for lbs_cfg_ret_disconnect() */
  1221. priv->disassoc_reason = reason_code;
  1222. return lbs_disconnect(priv, reason_code);
  1223. }
  1224. static int lbs_cfg_set_default_key(struct wiphy *wiphy,
  1225. struct net_device *netdev,
  1226. u8 key_index, bool unicast,
  1227. bool multicast)
  1228. {
  1229. struct lbs_private *priv = wiphy_priv(wiphy);
  1230. if (netdev == priv->mesh_dev)
  1231. return -EOPNOTSUPP;
  1232. lbs_deb_enter(LBS_DEB_CFG80211);
  1233. if (key_index != priv->wep_tx_key) {
  1234. lbs_deb_assoc("set_default_key: to %d\n", key_index);
  1235. priv->wep_tx_key = key_index;
  1236. lbs_set_wep_keys(priv);
  1237. }
  1238. return 0;
  1239. }
  1240. static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1241. u8 idx, bool pairwise, const u8 *mac_addr,
  1242. struct key_params *params)
  1243. {
  1244. struct lbs_private *priv = wiphy_priv(wiphy);
  1245. u16 key_info;
  1246. u16 key_type;
  1247. int ret = 0;
  1248. if (netdev == priv->mesh_dev)
  1249. return -EOPNOTSUPP;
  1250. lbs_deb_enter(LBS_DEB_CFG80211);
  1251. lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
  1252. params->cipher, mac_addr);
  1253. lbs_deb_assoc("add_key: key index %d, key len %d\n",
  1254. idx, params->key_len);
  1255. if (params->key_len)
  1256. lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
  1257. params->key, params->key_len);
  1258. lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
  1259. if (params->seq_len)
  1260. lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
  1261. params->seq, params->seq_len);
  1262. switch (params->cipher) {
  1263. case WLAN_CIPHER_SUITE_WEP40:
  1264. case WLAN_CIPHER_SUITE_WEP104:
  1265. /* actually compare if something has changed ... */
  1266. if ((priv->wep_key_len[idx] != params->key_len) ||
  1267. memcmp(priv->wep_key[idx],
  1268. params->key, params->key_len) != 0) {
  1269. priv->wep_key_len[idx] = params->key_len;
  1270. memcpy(priv->wep_key[idx],
  1271. params->key, params->key_len);
  1272. lbs_set_wep_keys(priv);
  1273. }
  1274. break;
  1275. case WLAN_CIPHER_SUITE_TKIP:
  1276. case WLAN_CIPHER_SUITE_CCMP:
  1277. key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
  1278. ? KEY_INFO_WPA_UNICAST
  1279. : KEY_INFO_WPA_MCAST);
  1280. key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
  1281. ? KEY_TYPE_ID_TKIP
  1282. : KEY_TYPE_ID_AES;
  1283. lbs_set_key_material(priv,
  1284. key_type,
  1285. key_info,
  1286. params->key, params->key_len);
  1287. break;
  1288. default:
  1289. wiphy_err(wiphy, "unhandled cipher 0x%x\n", params->cipher);
  1290. ret = -ENOTSUPP;
  1291. break;
  1292. }
  1293. return ret;
  1294. }
  1295. static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
  1296. u8 key_index, bool pairwise, const u8 *mac_addr)
  1297. {
  1298. lbs_deb_enter(LBS_DEB_CFG80211);
  1299. lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
  1300. key_index, mac_addr);
  1301. #ifdef TODO
  1302. struct lbs_private *priv = wiphy_priv(wiphy);
  1303. /*
  1304. * I think can keep this a NO-OP, because:
  1305. * - we clear all keys whenever we do lbs_cfg_connect() anyway
  1306. * - neither "iw" nor "wpa_supplicant" won't call this during
  1307. * an ongoing connection
  1308. * - TODO: but I have to check if this is still true when
  1309. * I set the AP to periodic re-keying
  1310. * - we've not kzallec() something when we've added a key at
  1311. * lbs_cfg_connect() or lbs_cfg_add_key().
  1312. *
  1313. * This causes lbs_cfg_del_key() only called at disconnect time,
  1314. * where we'd just waste time deleting a key that is not going
  1315. * to be used anyway.
  1316. */
  1317. if (key_index < 3 && priv->wep_key_len[key_index]) {
  1318. priv->wep_key_len[key_index] = 0;
  1319. lbs_set_wep_keys(priv);
  1320. }
  1321. #endif
  1322. return 0;
  1323. }
  1324. /*
  1325. * Get station
  1326. */
  1327. static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
  1328. u8 *mac, struct station_info *sinfo)
  1329. {
  1330. struct lbs_private *priv = wiphy_priv(wiphy);
  1331. s8 signal, noise;
  1332. int ret;
  1333. size_t i;
  1334. lbs_deb_enter(LBS_DEB_CFG80211);
  1335. sinfo->filled |= STATION_INFO_TX_BYTES |
  1336. STATION_INFO_TX_PACKETS |
  1337. STATION_INFO_RX_BYTES |
  1338. STATION_INFO_RX_PACKETS;
  1339. sinfo->tx_bytes = priv->dev->stats.tx_bytes;
  1340. sinfo->tx_packets = priv->dev->stats.tx_packets;
  1341. sinfo->rx_bytes = priv->dev->stats.rx_bytes;
  1342. sinfo->rx_packets = priv->dev->stats.rx_packets;
  1343. /* Get current RSSI */
  1344. ret = lbs_get_rssi(priv, &signal, &noise);
  1345. if (ret == 0) {
  1346. sinfo->signal = signal;
  1347. sinfo->filled |= STATION_INFO_SIGNAL;
  1348. }
  1349. /* Convert priv->cur_rate from hw_value to NL80211 value */
  1350. for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
  1351. if (priv->cur_rate == lbs_rates[i].hw_value) {
  1352. sinfo->txrate.legacy = lbs_rates[i].bitrate;
  1353. sinfo->filled |= STATION_INFO_TX_BITRATE;
  1354. break;
  1355. }
  1356. }
  1357. return 0;
  1358. }
  1359. /*
  1360. * "Site survey", here just current channel and noise level
  1361. */
  1362. static int lbs_get_survey(struct wiphy *wiphy, struct net_device *dev,
  1363. int idx, struct survey_info *survey)
  1364. {
  1365. struct lbs_private *priv = wiphy_priv(wiphy);
  1366. s8 signal, noise;
  1367. int ret;
  1368. if (dev == priv->mesh_dev)
  1369. return -EOPNOTSUPP;
  1370. if (idx != 0)
  1371. ret = -ENOENT;
  1372. lbs_deb_enter(LBS_DEB_CFG80211);
  1373. survey->channel = ieee80211_get_channel(wiphy,
  1374. ieee80211_channel_to_frequency(priv->channel,
  1375. IEEE80211_BAND_2GHZ));
  1376. ret = lbs_get_rssi(priv, &signal, &noise);
  1377. if (ret == 0) {
  1378. survey->filled = SURVEY_INFO_NOISE_DBM;
  1379. survey->noise = noise;
  1380. }
  1381. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1382. return ret;
  1383. }
  1384. /*
  1385. * Change interface
  1386. */
  1387. static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
  1388. enum nl80211_iftype type, u32 *flags,
  1389. struct vif_params *params)
  1390. {
  1391. struct lbs_private *priv = wiphy_priv(wiphy);
  1392. int ret = 0;
  1393. if (dev == priv->mesh_dev)
  1394. return -EOPNOTSUPP;
  1395. switch (type) {
  1396. case NL80211_IFTYPE_MONITOR:
  1397. case NL80211_IFTYPE_STATION:
  1398. case NL80211_IFTYPE_ADHOC:
  1399. break;
  1400. default:
  1401. return -EOPNOTSUPP;
  1402. }
  1403. lbs_deb_enter(LBS_DEB_CFG80211);
  1404. if (priv->iface_running)
  1405. ret = lbs_set_iface_type(priv, type);
  1406. if (!ret)
  1407. priv->wdev->iftype = type;
  1408. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1409. return ret;
  1410. }
  1411. /*
  1412. * IBSS (Ad-Hoc)
  1413. */
  1414. /*
  1415. * The firmware needs the following bits masked out of the beacon-derived
  1416. * capability field when associating/joining to a BSS:
  1417. * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
  1418. */
  1419. #define CAPINFO_MASK (~(0xda00))
  1420. static void lbs_join_post(struct lbs_private *priv,
  1421. struct cfg80211_ibss_params *params,
  1422. u8 *bssid, u16 capability)
  1423. {
  1424. u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
  1425. 2 + 4 + /* basic rates */
  1426. 2 + 1 + /* DS parameter */
  1427. 2 + 2 + /* atim */
  1428. 2 + 8]; /* extended rates */
  1429. u8 *fake = fake_ie;
  1430. lbs_deb_enter(LBS_DEB_CFG80211);
  1431. /*
  1432. * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
  1433. * the real IE from the firmware. So we fabricate a fake IE based on
  1434. * what the firmware actually sends (sniffed with wireshark).
  1435. */
  1436. /* Fake SSID IE */
  1437. *fake++ = WLAN_EID_SSID;
  1438. *fake++ = params->ssid_len;
  1439. memcpy(fake, params->ssid, params->ssid_len);
  1440. fake += params->ssid_len;
  1441. /* Fake supported basic rates IE */
  1442. *fake++ = WLAN_EID_SUPP_RATES;
  1443. *fake++ = 4;
  1444. *fake++ = 0x82;
  1445. *fake++ = 0x84;
  1446. *fake++ = 0x8b;
  1447. *fake++ = 0x96;
  1448. /* Fake DS channel IE */
  1449. *fake++ = WLAN_EID_DS_PARAMS;
  1450. *fake++ = 1;
  1451. *fake++ = params->channel->hw_value;
  1452. /* Fake IBSS params IE */
  1453. *fake++ = WLAN_EID_IBSS_PARAMS;
  1454. *fake++ = 2;
  1455. *fake++ = 0; /* ATIM=0 */
  1456. *fake++ = 0;
  1457. /* Fake extended rates IE, TODO: don't add this for 802.11b only,
  1458. * but I don't know how this could be checked */
  1459. *fake++ = WLAN_EID_EXT_SUPP_RATES;
  1460. *fake++ = 8;
  1461. *fake++ = 0x0c;
  1462. *fake++ = 0x12;
  1463. *fake++ = 0x18;
  1464. *fake++ = 0x24;
  1465. *fake++ = 0x30;
  1466. *fake++ = 0x48;
  1467. *fake++ = 0x60;
  1468. *fake++ = 0x6c;
  1469. lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
  1470. cfg80211_inform_bss(priv->wdev->wiphy,
  1471. params->channel,
  1472. bssid,
  1473. 0,
  1474. capability,
  1475. params->beacon_interval,
  1476. fake_ie, fake - fake_ie,
  1477. 0, GFP_KERNEL);
  1478. memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
  1479. priv->wdev->ssid_len = params->ssid_len;
  1480. cfg80211_ibss_joined(priv->dev, bssid, GFP_KERNEL);
  1481. /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
  1482. priv->connect_status = LBS_CONNECTED;
  1483. netif_carrier_on(priv->dev);
  1484. if (!priv->tx_pending_len)
  1485. netif_wake_queue(priv->dev);
  1486. lbs_deb_leave(LBS_DEB_CFG80211);
  1487. }
  1488. static int lbs_ibss_join_existing(struct lbs_private *priv,
  1489. struct cfg80211_ibss_params *params,
  1490. struct cfg80211_bss *bss)
  1491. {
  1492. const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1493. struct cmd_ds_802_11_ad_hoc_join cmd;
  1494. u8 preamble = RADIO_PREAMBLE_SHORT;
  1495. int ret = 0;
  1496. lbs_deb_enter(LBS_DEB_CFG80211);
  1497. /* TODO: set preamble based on scan result */
  1498. ret = lbs_set_radio(priv, preamble, 1);
  1499. if (ret)
  1500. goto out;
  1501. /*
  1502. * Example CMD_802_11_AD_HOC_JOIN command:
  1503. *
  1504. * command 2c 00 CMD_802_11_AD_HOC_JOIN
  1505. * size 65 00
  1506. * sequence xx xx
  1507. * result 00 00
  1508. * bssid 02 27 27 97 2f 96
  1509. * ssid 49 42 53 53 00 00 00 00
  1510. * 00 00 00 00 00 00 00 00
  1511. * 00 00 00 00 00 00 00 00
  1512. * 00 00 00 00 00 00 00 00
  1513. * type 02 CMD_BSS_TYPE_IBSS
  1514. * beacon period 64 00
  1515. * dtim period 00
  1516. * timestamp 00 00 00 00 00 00 00 00
  1517. * localtime 00 00 00 00 00 00 00 00
  1518. * IE DS 03
  1519. * IE DS len 01
  1520. * IE DS channel 01
  1521. * reserveed 00 00 00 00
  1522. * IE IBSS 06
  1523. * IE IBSS len 02
  1524. * IE IBSS atim 00 00
  1525. * reserved 00 00 00 00
  1526. * capability 02 00
  1527. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
  1528. * fail timeout ff 00
  1529. * probe delay 00 00
  1530. */
  1531. memset(&cmd, 0, sizeof(cmd));
  1532. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1533. memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
  1534. memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
  1535. cmd.bss.type = CMD_BSS_TYPE_IBSS;
  1536. cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
  1537. cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
  1538. cmd.bss.ds.header.len = 1;
  1539. cmd.bss.ds.channel = params->channel->hw_value;
  1540. cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  1541. cmd.bss.ibss.header.len = 2;
  1542. cmd.bss.ibss.atimwindow = 0;
  1543. cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  1544. /* set rates to the intersection of our rates and the rates in the
  1545. bss */
  1546. if (!rates_eid) {
  1547. lbs_add_rates(cmd.bss.rates);
  1548. } else {
  1549. int hw, i;
  1550. u8 rates_max = rates_eid[1];
  1551. u8 *rates = cmd.bss.rates;
  1552. for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
  1553. u8 hw_rate = lbs_rates[hw].bitrate / 5;
  1554. for (i = 0; i < rates_max; i++) {
  1555. if (hw_rate == (rates_eid[i+2] & 0x7f)) {
  1556. u8 rate = rates_eid[i+2];
  1557. if (rate == 0x02 || rate == 0x04 ||
  1558. rate == 0x0b || rate == 0x16)
  1559. rate |= 0x80;
  1560. *rates++ = rate;
  1561. }
  1562. }
  1563. }
  1564. }
  1565. /* Only v8 and below support setting this */
  1566. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
  1567. cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  1568. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1569. }
  1570. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
  1571. if (ret)
  1572. goto out;
  1573. /*
  1574. * This is a sample response to CMD_802_11_AD_HOC_JOIN:
  1575. *
  1576. * response 2c 80
  1577. * size 09 00
  1578. * sequence xx xx
  1579. * result 00 00
  1580. * reserved 00
  1581. */
  1582. lbs_join_post(priv, params, bss->bssid, bss->capability);
  1583. out:
  1584. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1585. return ret;
  1586. }
  1587. static int lbs_ibss_start_new(struct lbs_private *priv,
  1588. struct cfg80211_ibss_params *params)
  1589. {
  1590. struct cmd_ds_802_11_ad_hoc_start cmd;
  1591. struct cmd_ds_802_11_ad_hoc_result *resp =
  1592. (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
  1593. u8 preamble = RADIO_PREAMBLE_SHORT;
  1594. int ret = 0;
  1595. u16 capability;
  1596. lbs_deb_enter(LBS_DEB_CFG80211);
  1597. ret = lbs_set_radio(priv, preamble, 1);
  1598. if (ret)
  1599. goto out;
  1600. /*
  1601. * Example CMD_802_11_AD_HOC_START command:
  1602. *
  1603. * command 2b 00 CMD_802_11_AD_HOC_START
  1604. * size b1 00
  1605. * sequence xx xx
  1606. * result 00 00
  1607. * ssid 54 45 53 54 00 00 00 00
  1608. * 00 00 00 00 00 00 00 00
  1609. * 00 00 00 00 00 00 00 00
  1610. * 00 00 00 00 00 00 00 00
  1611. * bss type 02
  1612. * beacon period 64 00
  1613. * dtim period 00
  1614. * IE IBSS 06
  1615. * IE IBSS len 02
  1616. * IE IBSS atim 00 00
  1617. * reserved 00 00 00 00
  1618. * IE DS 03
  1619. * IE DS len 01
  1620. * IE DS channel 01
  1621. * reserved 00 00 00 00
  1622. * probe delay 00 00
  1623. * capability 02 00
  1624. * rates 82 84 8b 96 (basic rates with have bit 7 set)
  1625. * 0c 12 18 24 30 48 60 6c
  1626. * padding 100 bytes
  1627. */
  1628. memset(&cmd, 0, sizeof(cmd));
  1629. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1630. memcpy(cmd.ssid, params->ssid, params->ssid_len);
  1631. cmd.bsstype = CMD_BSS_TYPE_IBSS;
  1632. cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
  1633. cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  1634. cmd.ibss.header.len = 2;
  1635. cmd.ibss.atimwindow = 0;
  1636. cmd.ds.header.id = WLAN_EID_DS_PARAMS;
  1637. cmd.ds.header.len = 1;
  1638. cmd.ds.channel = params->channel->hw_value;
  1639. /* Only v8 and below support setting probe delay */
  1640. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
  1641. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1642. /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
  1643. capability = WLAN_CAPABILITY_IBSS;
  1644. cmd.capability = cpu_to_le16(capability);
  1645. lbs_add_rates(cmd.rates);
  1646. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
  1647. if (ret)
  1648. goto out;
  1649. /*
  1650. * This is a sample response to CMD_802_11_AD_HOC_JOIN:
  1651. *
  1652. * response 2b 80
  1653. * size 14 00
  1654. * sequence xx xx
  1655. * result 00 00
  1656. * reserved 00
  1657. * bssid 02 2b 7b 0f 86 0e
  1658. */
  1659. lbs_join_post(priv, params, resp->bssid, capability);
  1660. out:
  1661. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1662. return ret;
  1663. }
  1664. static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1665. struct cfg80211_ibss_params *params)
  1666. {
  1667. struct lbs_private *priv = wiphy_priv(wiphy);
  1668. int ret = 0;
  1669. struct cfg80211_bss *bss;
  1670. DECLARE_SSID_BUF(ssid_buf);
  1671. if (dev == priv->mesh_dev)
  1672. return -EOPNOTSUPP;
  1673. lbs_deb_enter(LBS_DEB_CFG80211);
  1674. if (!params->channel) {
  1675. ret = -ENOTSUPP;
  1676. goto out;
  1677. }
  1678. ret = lbs_set_channel(priv, params->channel->hw_value);
  1679. if (ret)
  1680. goto out;
  1681. /* Search if someone is beaconing. This assumes that the
  1682. * bss list is populated already */
  1683. bss = cfg80211_get_bss(wiphy, params->channel, params->bssid,
  1684. params->ssid, params->ssid_len,
  1685. WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
  1686. if (bss) {
  1687. ret = lbs_ibss_join_existing(priv, params, bss);
  1688. cfg80211_put_bss(bss);
  1689. } else
  1690. ret = lbs_ibss_start_new(priv, params);
  1691. out:
  1692. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1693. return ret;
  1694. }
  1695. static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1696. {
  1697. struct lbs_private *priv = wiphy_priv(wiphy);
  1698. struct cmd_ds_802_11_ad_hoc_stop cmd;
  1699. int ret = 0;
  1700. if (dev == priv->mesh_dev)
  1701. return -EOPNOTSUPP;
  1702. lbs_deb_enter(LBS_DEB_CFG80211);
  1703. memset(&cmd, 0, sizeof(cmd));
  1704. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1705. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
  1706. /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
  1707. lbs_mac_event_disconnected(priv);
  1708. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1709. return ret;
  1710. }
  1711. /*
  1712. * Initialization
  1713. */
  1714. static struct cfg80211_ops lbs_cfg80211_ops = {
  1715. .set_channel = lbs_cfg_set_channel,
  1716. .scan = lbs_cfg_scan,
  1717. .connect = lbs_cfg_connect,
  1718. .disconnect = lbs_cfg_disconnect,
  1719. .add_key = lbs_cfg_add_key,
  1720. .del_key = lbs_cfg_del_key,
  1721. .set_default_key = lbs_cfg_set_default_key,
  1722. .get_station = lbs_cfg_get_station,
  1723. .dump_survey = lbs_get_survey,
  1724. .change_virtual_intf = lbs_change_intf,
  1725. .join_ibss = lbs_join_ibss,
  1726. .leave_ibss = lbs_leave_ibss,
  1727. };
  1728. /*
  1729. * At this time lbs_private *priv doesn't even exist, so we just allocate
  1730. * memory and don't initialize the wiphy further. This is postponed until we
  1731. * can talk to the firmware and happens at registration time in
  1732. * lbs_cfg_wiphy_register().
  1733. */
  1734. struct wireless_dev *lbs_cfg_alloc(struct device *dev)
  1735. {
  1736. int ret = 0;
  1737. struct wireless_dev *wdev;
  1738. lbs_deb_enter(LBS_DEB_CFG80211);
  1739. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  1740. if (!wdev) {
  1741. dev_err(dev, "cannot allocate wireless device\n");
  1742. return ERR_PTR(-ENOMEM);
  1743. }
  1744. wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
  1745. if (!wdev->wiphy) {
  1746. dev_err(dev, "cannot allocate wiphy\n");
  1747. ret = -ENOMEM;
  1748. goto err_wiphy_new;
  1749. }
  1750. lbs_deb_leave(LBS_DEB_CFG80211);
  1751. return wdev;
  1752. err_wiphy_new:
  1753. kfree(wdev);
  1754. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1755. return ERR_PTR(ret);
  1756. }
  1757. static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
  1758. {
  1759. struct region_code_mapping {
  1760. const char *cn;
  1761. int code;
  1762. };
  1763. /* Section 5.17.2 */
  1764. static const struct region_code_mapping regmap[] = {
  1765. {"US ", 0x10}, /* US FCC */
  1766. {"CA ", 0x20}, /* Canada */
  1767. {"EU ", 0x30}, /* ETSI */
  1768. {"ES ", 0x31}, /* Spain */
  1769. {"FR ", 0x32}, /* France */
  1770. {"JP ", 0x40}, /* Japan */
  1771. };
  1772. size_t i;
  1773. lbs_deb_enter(LBS_DEB_CFG80211);
  1774. for (i = 0; i < ARRAY_SIZE(regmap); i++)
  1775. if (regmap[i].code == priv->regioncode) {
  1776. regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
  1777. break;
  1778. }
  1779. lbs_deb_leave(LBS_DEB_CFG80211);
  1780. }
  1781. /*
  1782. * This function get's called after lbs_setup_firmware() determined the
  1783. * firmware capabities. So we can setup the wiphy according to our
  1784. * hardware/firmware.
  1785. */
  1786. int lbs_cfg_register(struct lbs_private *priv)
  1787. {
  1788. struct wireless_dev *wdev = priv->wdev;
  1789. int ret;
  1790. lbs_deb_enter(LBS_DEB_CFG80211);
  1791. wdev->wiphy->max_scan_ssids = 1;
  1792. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  1793. wdev->wiphy->interface_modes =
  1794. BIT(NL80211_IFTYPE_STATION) |
  1795. BIT(NL80211_IFTYPE_ADHOC);
  1796. if (lbs_rtap_supported(priv))
  1797. wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  1798. if (lbs_mesh_activated(priv))
  1799. wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MESH_POINT);
  1800. wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &lbs_band_2ghz;
  1801. /*
  1802. * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
  1803. * never seen a firmware without WPA
  1804. */
  1805. wdev->wiphy->cipher_suites = cipher_suites;
  1806. wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  1807. wdev->wiphy->reg_notifier = lbs_reg_notifier;
  1808. ret = wiphy_register(wdev->wiphy);
  1809. if (ret < 0)
  1810. pr_err("cannot register wiphy device\n");
  1811. priv->wiphy_registered = true;
  1812. ret = register_netdev(priv->dev);
  1813. if (ret)
  1814. pr_err("cannot register network device\n");
  1815. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  1816. lbs_cfg_set_regulatory_hint(priv);
  1817. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1818. return ret;
  1819. }
  1820. int lbs_reg_notifier(struct wiphy *wiphy,
  1821. struct regulatory_request *request)
  1822. {
  1823. struct lbs_private *priv = wiphy_priv(wiphy);
  1824. int ret;
  1825. lbs_deb_enter_args(LBS_DEB_CFG80211, "cfg80211 regulatory domain "
  1826. "callback for domain %c%c\n", request->alpha2[0],
  1827. request->alpha2[1]);
  1828. ret = lbs_set_11d_domain_info(priv, request, wiphy->bands);
  1829. lbs_deb_leave(LBS_DEB_CFG80211);
  1830. return ret;
  1831. }
  1832. void lbs_scan_deinit(struct lbs_private *priv)
  1833. {
  1834. lbs_deb_enter(LBS_DEB_CFG80211);
  1835. cancel_delayed_work_sync(&priv->scan_work);
  1836. }
  1837. void lbs_cfg_free(struct lbs_private *priv)
  1838. {
  1839. struct wireless_dev *wdev = priv->wdev;
  1840. lbs_deb_enter(LBS_DEB_CFG80211);
  1841. if (!wdev)
  1842. return;
  1843. if (priv->wiphy_registered)
  1844. wiphy_unregister(wdev->wiphy);
  1845. if (wdev->wiphy)
  1846. wiphy_free(wdev->wiphy);
  1847. kfree(wdev);
  1848. }