cfg.c 53 KB

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