cfg.c 54 KB

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